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