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