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