Mercurial > projects > ddmd
annotate dmd/AsmStatement.d @ 132:c494af1dba80
Fixes for dmd 2.037
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Fri, 10 Sep 2010 19:14:09 +0100 |
parents | 60bb0fe4563e |
children | af724d3510d7 |
rev | line source |
---|---|
0 | 1 module dmd.AsmStatement; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.Loc; |
5 import dmd.Statement; | |
6 import dmd.Token; | |
7 import dmd.Scope; | |
8 import dmd.OutBuffer; | |
9 import dmd.HdrGenState; | |
10 import dmd.IRState; | |
11 import dmd.BE; | |
12 import dmd.LabelDsymbol; | |
13 import dmd.Dsymbol; | |
14 import dmd.Id; | |
15 import dmd.TOK; | |
16 import dmd.Global; | |
17 import dmd.FuncDeclaration; | |
18 import dmd.Declaration; | |
19 import dmd.LabelStatement; | |
20 import dmd.Util; | |
21 | |
22 import dmd.backend.code; | |
23 import dmd.backend.iasm; | |
24 import dmd.backend.block; | |
25 import dmd.backend.Blockx; | |
26 import dmd.backend.Util; | |
27 import dmd.codegen.Util; | |
28 import dmd.backend.BC; | |
29 import dmd.backend.FL; | |
30 import dmd.backend.SFL; | |
31 import dmd.backend.SC; | |
32 import dmd.backend.mTY; | |
33 import dmd.backend.Symbol; | |
34 import dmd.backend.LIST; | |
35 | |
36 import core.stdc.string : memset; | |
37 import core.stdc.stdlib : exit, EXIT_FAILURE; | |
38 | |
39 class AsmStatement : Statement | |
40 { | |
41 Token* tokens; | |
42 code* asmcode; | |
43 uint asmalign; // alignment of this statement | |
44 bool refparam; // true if function parameter is referenced | |
45 bool naked; // true if function is to be naked | |
46 uint regs; // mask of registers modified | |
47 | |
48 this(Loc loc, Token* tokens) | |
49 { | |
50 super(loc); | |
51 this.tokens = tokens; | |
52 } | |
53 | |
72 | 54 override Statement syntaxCopy() |
0 | 55 { |
56 assert(false); | |
57 } | |
58 | |
72 | 59 override Statement semantic(Scope sc) |
0 | 60 { |
61 //printf("AsmStatement.semantic()\n"); | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
62 //static if (true) { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
63 if (sc.func && sc.func.isSafe()) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
64 error("inline assembler not allowed in @safe function %s", sc.func.toChars()); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
65 //} else { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
66 // if (global.params.safe && !sc.module_.safe) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
67 // { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
68 // error("inline assembler not allowed in safe mode"); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
69 // } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
70 //} |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
71 |
0 | 72 OP* o; |
73 OPND* o1 = null; | |
74 OPND* o2 = null; | |
75 OPND* o3 = null; | |
76 | |
77 PTRNTAB ptb; | |
78 uint usNumops; | |
79 ubyte uchPrefix = 0; | |
80 ubyte bAsmseen; | |
81 char* pszLabel = null; | |
82 code* c; | |
83 FuncDeclaration fd = sc.parent.isFuncDeclaration(); | |
84 | |
85 assert(fd); | |
86 fd.inlineAsm = 1; | |
87 | |
88 if (!tokens) | |
89 return null; | |
90 | |
91 memset(&asmstate, 0, asmstate.sizeof); | |
92 | |
93 asmstate.statement = this; | |
94 asmstate.sc = sc; | |
95 | |
96 static if (false) { | |
97 // don't use bReturnax anymore, and will fail anyway if we use return type inference | |
98 // Scalar return values will always be in AX. So if it is a scalar | |
99 // then asm block sets return value if it modifies AX, if it is non-scalar | |
100 // then always assume that the ASM block sets up an appropriate return | |
101 // value. | |
102 | |
103 asmstate.bReturnax = 1; | |
104 if (sc.func.type.nextOf().isscalar()) | |
105 asmstate.bReturnax = 0; | |
106 } | |
107 | |
108 // Assume assembler code takes care of setting the return value | |
109 sc.func.hasReturnExp |= 8; | |
110 | |
111 if (!asmstate.bInit) | |
112 { | |
113 asmstate.bInit = true; | |
114 init_optab(); | |
115 asmstate.psDollar = new LabelDsymbol(Id.__dollar); | |
116 //asmstate.psLocalsize = new VarDeclaration(0, Type.tint32, Id.__LOCAL_SIZE, null); | |
117 asmstate.psLocalsize = new Dsymbol(Id.__LOCAL_SIZE); | |
118 cod3_set386(); | |
119 } | |
120 | |
121 asmstate.loc = loc; | |
122 | |
123 asmtok = tokens; | |
124 asm_token_trans(asmtok); | |
125 if (setjmp(asmstate.env)) | |
126 { | |
127 asmtok = null; // skip rest of line | |
128 tok_value = TOK.TOKeof; | |
129 exit(EXIT_FAILURE); | |
130 goto AFTER_EMIT; | |
131 } | |
132 | |
133 switch (cast(int)tok_value) | |
134 { | |
135 case ASMTK.ASMTKnaked: | |
136 naked = true; | |
137 sc.func.naked = true; | |
138 asm_token(); | |
139 break; | |
140 | |
141 case ASMTK.ASMTKeven: | |
142 asm_token(); | |
143 asmalign = 2; | |
144 break; | |
145 | |
146 case TOK.TOKalign: | |
147 { | |
148 asm_token(); | |
149 uint align_ = asm_getnum(); | |
150 if (ispow2(align_) == -1) | |
151 asmerr(ASMERRMSGS.EM_align, align_); // power of 2 expected | |
152 else | |
153 asmalign = align_; | |
154 break; | |
155 } | |
156 | |
157 // The following three convert the keywords 'int', 'in', 'out' | |
158 // to identifiers, since they are x86 instructions. | |
159 case TOK.TOKint32: | |
160 o = asm_op_lookup(Id.__int.toChars()); | |
161 goto Lopcode; | |
162 | |
163 case TOK.TOKin: | |
164 o = asm_op_lookup(Id.___in.toChars()); | |
165 goto Lopcode; | |
166 | |
167 case TOK.TOKout: | |
168 o = asm_op_lookup(Id.___out.toChars()); | |
169 goto Lopcode; | |
170 | |
171 case TOK.TOKidentifier: | |
172 o = asm_op_lookup(asmtok.ident.toChars()); | |
173 if (!o) | |
174 goto OPCODE_EXPECTED; | |
175 | |
176 Lopcode: | |
177 asmstate.ucItype = o.usNumops & IT.ITMASK; | |
178 asm_token(); | |
179 if (o.usNumops > 3) | |
180 { | |
181 switch (asmstate.ucItype) | |
182 { | |
183 case IT.ITdata: | |
184 asmcode = asm_db_parse(o); | |
185 goto AFTER_EMIT; | |
186 | |
187 case IT.ITaddr: | |
188 asmcode = asm_da_parse(o); | |
189 goto AFTER_EMIT; | |
190 | |
191 default: | |
192 break; | |
193 } | |
194 } | |
195 // get the first part of an expr | |
196 o1 = asm_cond_exp(); | |
197 if (tok_value == TOK.TOKcomma) | |
198 { | |
199 asm_token(); | |
200 o2 = asm_cond_exp(); | |
201 } | |
202 if (tok_value == TOK.TOKcomma) | |
203 { | |
204 asm_token(); | |
205 o3 = asm_cond_exp(); | |
206 } | |
207 // match opcode and operands in ptrntab to verify legal inst and | |
208 // generate | |
209 | |
210 ptb = asm_classify(o, o1, o2, o3, &usNumops); | |
211 assert(ptb.pptb0); | |
212 | |
213 // | |
214 // The Multiply instruction takes 3 operands, but if only 2 are seen | |
215 // then the third should be the second and the second should | |
216 // be a duplicate of the first. | |
217 // | |
218 | |
219 if (asmstate.ucItype == IT.ITopt && | |
220 (usNumops == 2) && | |
221 (ASM_GET_aopty(o2.usFlags) == ASM_OPERAND_TYPE._imm) && | |
222 ((o.usNumops & IT.ITSIZE) == 3)) | |
223 { | |
224 o3 = o2; | |
225 o2 = opnd_calloc(); | |
226 *o2 = *o1; | |
227 | |
228 // Re-classify the opcode because the first classification | |
229 // assumed 2 operands. | |
230 | |
231 ptb = asm_classify(o, o1, o2, o3, &usNumops); | |
232 } | |
233 else | |
234 { | |
235 static if (false) { | |
236 if (asmstate.ucItype == IT.ITshift && (ptb.pptb2.usOp2 == 0 || | |
237 (ptb.pptb2.usOp2 & _cl))) { | |
238 opnd_free(o2); | |
239 o2 = null; | |
240 usNumops = 1; | |
241 } | |
242 } | |
243 } | |
244 | |
245 asmcode = asm_emit(loc, usNumops, ptb, o, o1, o2, o3); | |
246 break; | |
247 | |
248 default: | |
249 OPCODE_EXPECTED: | |
250 asmerr(ASMERRMSGS.EM_opcode_exp, asmtok.toChars()); // assembler opcode expected | |
251 break; | |
252 } | |
253 | |
254 AFTER_EMIT: | |
255 opnd_free(o1); | |
256 opnd_free(o2); | |
257 opnd_free(o3); | |
258 o1 = o2 = o3 = null; | |
259 | |
260 if (tok_value != TOK.TOKeof) | |
261 asmerr(ASMERRMSGS.EM_eol); // end of line expected | |
262 | |
263 //return asmstate.bReturnax; | |
264 return this; | |
265 } | |
266 | |
72 | 267 override BE blockExit() |
0 | 268 { |
269 assert(false); | |
270 } | |
271 | |
72 | 272 override bool comeFrom() |
0 | 273 { |
274 assert(false); | |
275 } | |
276 | |
72 | 277 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 278 { |
279 assert(false); | |
280 } | |
281 | |
72 | 282 override AsmStatement isAsmStatement() { return this; } |
0 | 283 |
72 | 284 override void toIR(IRState *irs) |
0 | 285 { |
286 block* bpre; | |
287 block* basm; | |
288 Declaration d; | |
289 Symbol* s; | |
290 Blockx* blx = irs.blx; | |
291 | |
292 // dumpCode(asmcode); | |
293 | |
294 //printf("AsmStatement::toIR(asmcode = %x)\n", asmcode); | |
295 bpre = blx.curblock; | |
296 block_next(blx,BCgoto,null); | |
297 basm = blx.curblock; | |
298 list_append(&bpre.Bsucc, basm); | |
299 basm.Bcode = asmcode; | |
300 basm.Balign = cast(ubyte)asmalign; | |
301 static if (false) { | |
302 if (label) | |
303 { | |
304 block* b = labelToBlock(loc, blx, label); | |
305 printf("AsmStatement::toIR() %p\n", b); | |
306 if (b) | |
307 list_append(&basm.Bsucc, b); | |
308 } | |
309 } | |
310 // Loop through each instruction, fixing Dsymbols into Symbol's | |
311 for (code* c = asmcode; c; c = c.next) | |
312 { | |
313 LabelDsymbol label; | |
314 block* b; | |
315 | |
316 switch (c.IFL1) | |
317 { | |
318 case FLblockoff: | |
319 case FLblock: | |
320 // FLblock and FLblockoff have LabelDsymbol's - convert to blocks | |
321 label = c.IEVlsym1; | |
322 b = labelToBlock(loc, blx, label); | |
323 list_append(&basm.Bsucc, b); | |
324 c.IEV1.Vblock = b; | |
325 break; | |
326 | |
327 case FLdsymbol: | |
328 case FLfunc: | |
329 s = c.IEVdsym1.toSymbol(); | |
330 if (s.Sclass == SCauto && s.Ssymnum == -1) | |
331 symbol_add(s); | |
332 c.IEVsym1() = s; | |
333 c.IFL1 = s.Sfl ? s.Sfl : FLauto; | |
334 break; | |
335 default: | |
336 break; | |
337 } | |
338 | |
339 // Repeat for second operand | |
340 switch (c.IFL2) | |
341 { | |
342 case FLblockoff: | |
343 case FLblock: | |
344 label = c.IEVlsym2; | |
345 b = labelToBlock(loc, blx, label); | |
346 list_append(&basm.Bsucc, b); | |
347 c.IEV2.Vblock = b; | |
348 break; | |
349 | |
350 case FLdsymbol: | |
351 case FLfunc: | |
352 d = c.IEVdsym2; | |
353 s = d.toSymbol(); | |
354 if (s.Sclass == SCauto && s.Ssymnum == -1) | |
355 symbol_add(s); | |
356 c.IEVsym2() = s; | |
357 c.IFL2 = s.Sfl ? s.Sfl : FLauto; | |
358 if (d.isDataseg()) | |
359 s.Sflags |= SFLlivexit; | |
360 break; | |
361 default: | |
362 break; | |
363 } | |
364 //c.print(); | |
365 } | |
366 | |
367 basm.bIasmrefparam = refparam; // are parameters reference? | |
368 basm.usIasmregs = cast(ushort)regs; // registers modified | |
369 | |
370 block_next(blx,BCasm, null); | |
371 list_prepend(&basm.Bsucc, blx.curblock); | |
372 | |
373 if (naked) | |
374 { | |
375 blx.funcsym.Stype.Tty |= mTYnaked; | |
376 } | |
377 } | |
72 | 378 } |