Mercurial > projects > ddmd
annotate dmd/backend/iasm.d @ 114:e28b18c23469
added a module dmd.common for commonly used stuff
it currently holds code for consistency checking of predefined versions
also added a VisualD project file
author | Trass3r |
---|---|
date | Wed, 01 Sep 2010 18:21:58 +0200 |
parents | 3482c73a991b |
children | af1bebfd96a4 |
rev | line source |
---|---|
0 | 1 module dmd.backend.iasm; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.Dsymbol; |
5 import dmd.LabelDsymbol; | |
6 import dmd.AsmStatement; | |
7 import dmd.Type; | |
8 import dmd.Scope; | |
9 import dmd.Loc; | |
10 import dmd.Token; | |
11 import dmd.TOK; | |
12 import dmd.Identifier; | |
13 import dmd.Declaration; | |
14 import dmd.VarDeclaration; | |
15 import dmd.EnumMember; | |
16 import dmd.ExpInitializer; | |
17 import dmd.Expression; | |
18 import dmd.IdentifierExp; | |
19 import dmd.StringExp; | |
20 import dmd.Global; | |
21 import dmd.WANT; | |
22 import dmd.STC; | |
23 import dmd.TY; | |
24 import dmd.EnumUtils; | |
25 import dmd.TupleDeclaration; | |
26 import dmd.VarExp; | |
27 import dmd.Id; | |
28 import dmd.FuncExp; | |
29 import dmd.DotIdExp; | |
30 | |
31 import dmd.backend.code; | |
32 import dmd.backend.Srcpos; | |
33 import dmd.backend.FL; | |
34 import dmd.backend.Util; | |
35 import dmd.backend.regm_t; | |
36 import dmd.backend.Config; | |
37 import dmd.backend.targ_types; | |
38 import dmd.backend.elem; | |
39 import dmd.Util; | |
40 | |
41 import std.stdio : writef, writefln; | |
42 import std.string : toStringz; | |
43 import std.algorithm : min; | |
44 | |
4 | 45 import core.memory; |
46 | |
47 import core.stdc.stdio : printf; | |
0 | 48 import core.stdc.string : strlen; |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
49 import core.stdc.stdlib : realloc; |
0 | 50 import core.stdc.limits; |
51 | |
52 import std.bitmanip; | |
53 | |
54 alias int[10] jmp_buf; | |
55 | |
56 extern (C) extern | |
57 { | |
28
c369e9924151
Updated setjmp/longjmp prototypes to reflect recent static arrays change (they are now passed by value by default).
korDen
parents:
25
diff
changeset
|
58 int setjmp(ref jmp_buf env); |
c369e9924151
Updated setjmp/longjmp prototypes to reflect recent static arrays change (they are now passed by value by default).
korDen
parents:
25
diff
changeset
|
59 void longjmp(ref jmp_buf env, int value); |
0 | 60 |
61 void cod3_set386(); | |
62 code* genlinnum(code*, Srcpos); | |
63 code *code_calloc(); | |
64 | |
65 __gshared int BPRM; | |
66 } | |
67 | |
68 const(char)*[ASMTK.ASMTKmax] apszAsmtk = [ | |
69 "__LOCAL_SIZE", | |
70 "dword".ptr, | |
71 "even".ptr, | |
72 "far".ptr, | |
73 "naked".ptr, | |
74 "near".ptr, | |
75 "ptr".ptr, | |
76 "qword".ptr, | |
77 "seg".ptr, | |
78 "word".ptr, | |
79 ]; | |
80 | |
25 | 81 version (Windows) { |
82 extern (Pascal) code * cat(code *c1,code *c2); | |
83 } else { | |
84 extern (C) code * cat(code *c1,code *c2); | |
85 } | |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
86 |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
87 version (Bug4059) |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
88 { |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
89 private extern(C) OP* _Z13asm_op_lookupPKc(const(char)* s); |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
90 private extern(C) int _Z6binaryPKcPS0_i(const(char)* p , const(char)** tab, int high); |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
91 OP* asm_op_lookup(const(char)* s) { return _Z13asm_op_lookupPKc(s); } |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
92 int binary(const(char)* p , const(char)** tab, int high) { return _Z6binaryPKcPS0_i(p, tab, high); } |
0 | 93 } |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
94 else |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
95 { |
25 | 96 extern (C++) { |
97 OP* asm_op_lookup(const(char)* s); | |
98 int binary(const(char)* p , const(char)** tab, int high); | |
99 } | |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
100 } |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
101 |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
102 extern (C++) //extern |
0 | 103 { |
104 void init_optab(); | |
105 const(char)* asm_opstr(OP* pop); | |
106 } | |
107 | |
108 static ubyte asm_TKlbra_seen = false; | |
109 | |
110 struct REG | |
111 { | |
54 | 112 string regstr; |
0 | 113 ubyte val; |
114 opflag_t ty; | |
115 } | |
116 | |
117 OP* asm_op_lookup(string s) | |
118 { | |
119 return asm_op_lookup(toStringz(s)); | |
120 } | |
121 | |
122 // For amod (3 bits) | |
123 enum ASM_MODIFIERS : ubyte | |
124 { | |
125 _normal, // Normal register value | |
126 _rseg, // Segment registers | |
127 _rspecial, // Special registers | |
128 _addr16, // 16 bit address | |
129 _addr32, // 32 bit address | |
130 _fn16, // 16 bit function call | |
131 _fn32, // 32 bit function call | |
132 _flbl // Label | |
133 } | |
134 | |
135 mixin(BringToCurrentScope!(ASM_MODIFIERS)); | |
136 | |
137 // For aopty (3 bits) | |
138 enum ASM_OPERAND_TYPE : ubyte | |
139 { | |
140 _reg, // _r8, _r16, _r32 | |
141 _m, // _m8, _m16, _m32, _m48 | |
142 _imm, // _imm8, _imm16, _imm32 | |
143 _rel, // _rel8, _rel16, _rel32 | |
144 _mnoi, // _m1616, _m1632 | |
145 _p, // _p1616, _p1632 | |
146 _rm, // _rm8, _rm16, _rm32 | |
147 _float // Floating point operand, look at cRegmask for the | |
148 // actual size | |
149 } | |
150 | |
151 mixin(BringToCurrentScope!(ASM_OPERAND_TYPE)); | |
152 | |
153 /* Register definitions */ | |
154 | |
155 enum AX = 0; | |
156 enum CX = 1; | |
157 enum DX = 2; | |
158 enum BX = 3; | |
159 enum SP = 4; | |
160 enum BP = 5; | |
161 enum SI = 6; | |
162 enum DI = 7; | |
163 | |
164 enum ES = 9; | |
165 enum PSW = 10; | |
166 enum STACK = 11; // top of stack | |
167 enum MEM = 12; // memory | |
168 enum OTHER = 13; // other things | |
169 enum ST0 = 14; // 8087 top of stack register | |
170 enum ST01 = 15; // top two 8087 registers; for complex types | |
171 | |
172 enum NOREG = 100; // no register | |
173 | |
174 enum AL = 0; | |
175 enum CL = 1; | |
176 enum DL = 2; | |
177 enum BL = 3; | |
178 enum AH = 4; | |
179 enum CH = 5; | |
180 enum DH = 6; | |
181 enum BH = 7; | |
182 | |
183 enum mAX = 1; | |
184 enum mCX = 2; | |
185 enum mDX = 4; | |
186 enum mBX = 8; | |
187 enum mSP = 0x10; | |
188 enum mBP = 0x20; | |
189 enum mSI = 0x40; | |
190 enum mDI = 0x80; | |
191 enum mES = (1 << ES); // 0x200 | |
192 enum mPSW = (1 << PSW); // 0x400 | |
193 | |
194 enum mSTACK = (1 << STACK); // 0x800 | |
195 enum mMEM = (1 << MEM); // 0x1000 | |
196 enum mOTHER = (1 << OTHER); // 0x2000 | |
197 | |
198 enum mST0 = (1 << ST0); // 0x4000 | |
199 enum mST01 = (1 << ST01); // 0x8000 | |
200 | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
20
diff
changeset
|
201 version (POSIX) { ///TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_SOLARIS |
0 | 202 // To support positional independent code, |
203 // must be able to remove BX from available registers | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
20
diff
changeset
|
204 extern (C) extern __gshared regm_t ALLREGS; |
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
20
diff
changeset
|
205 enum ALLREGS_INIT = (mAX|mBX|mCX|mDX|mSI|mDI); |
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
20
diff
changeset
|
206 enum ALLREGS_INIT_PIC = (mAX|mCX|mDX|mSI|mDI); |
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
20
diff
changeset
|
207 extern (C) extern regm_t BYTEREGS; |
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
20
diff
changeset
|
208 enum BYTEREGS_INIT = (mAX|mBX|mCX|mDX); |
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
20
diff
changeset
|
209 enum BYTEREGS_INIT_PIC = (mAX|mCX|mDX); |
0 | 210 } else { |
211 enum ALLREGS = (mAX|mBX|mCX|mDX|mSI|mDI); | |
212 ///#define ALLREGS_INIT ALLREGS | |
213 ///#undef BYTEREGS | |
214 ///#define BYTEREGS (mAX|mBX|mCX|mDX) | |
215 } | |
216 | |
217 //#define NPTRSIZE tysize[TYnptr] | |
218 enum NPTRSIZE = 4; | |
219 | |
220 uint ADDFWAIT() { return 0; } | |
221 | |
222 enum I16 = 0; // no 16 bit code for D | |
223 enum I32 = (NPTRSIZE == 4); | |
224 enum I64 = (NPTRSIZE == 8); // true if generating 64 bit code | |
225 | |
226 // For uRegmask (6 bits) | |
227 | |
228 // uRegmask flags when aopty == _float | |
229 enum _rst = 0x1; | |
230 enum _rsti = 0x2; | |
231 enum _64 = 0x4; | |
232 enum _80 = 0x8; | |
233 enum _128 = 0x40; | |
234 enum _112 = 0x10; | |
235 enum _224 = 0x20; | |
236 | |
237 ushort CONSTRUCT_FLAGS(ushort uSizemask, ubyte aopty, ubyte amod, ushort uRegmask ) { | |
238 return cast(ushort)( (uSizemask) | (aopty) << 4 | (amod) << 7 | (uRegmask) << 10); | |
239 } | |
240 | |
241 // _seg register values (amod == _rseg) | |
242 // | |
243 enum _ds = CONSTRUCT_FLAGS( 0, 0, _rseg, 0x01 ); | |
244 enum _es = CONSTRUCT_FLAGS( 0, 0, _rseg, 0x02 ); | |
245 enum _ss = CONSTRUCT_FLAGS( 0, 0, _rseg, 0x04 ); | |
246 enum _fs = CONSTRUCT_FLAGS( 0, 0, _rseg, 0x08 ); | |
247 enum _gs = CONSTRUCT_FLAGS( 0, 0, _rseg, 0x10 ); | |
248 enum _cs = CONSTRUCT_FLAGS( 0, 0, _rseg, 0x20 ); | |
249 | |
250 // | |
251 // _special register values | |
252 // | |
253 enum _crn = CONSTRUCT_FLAGS( 0, 0, ASM_MODIFIERS._rspecial, 0x01 ); // CRn register (0,2,3) | |
254 enum _drn = CONSTRUCT_FLAGS( 0, 0, ASM_MODIFIERS._rspecial, 0x02 ); // DRn register (0-3,6-7) | |
255 enum _trn = CONSTRUCT_FLAGS( 0, 0, ASM_MODIFIERS._rspecial, 0x04 ); // TRn register (3-7) | |
256 enum _mm = CONSTRUCT_FLAGS( 0, 0, ASM_MODIFIERS._rspecial, 0x08 ); // MMn register (0-7) | |
257 enum _xmm = CONSTRUCT_FLAGS( 0, 0, ASM_MODIFIERS._rspecial, 0x10 ); // XMMn register (0-7) | |
258 | |
259 // | |
260 // Default register values | |
261 // | |
262 enum _al = CONSTRUCT_FLAGS( 0, 0, ASM_MODIFIERS._normal, 0x01 ); // AL register | |
263 enum _ax = CONSTRUCT_FLAGS( 0, 0, ASM_MODIFIERS._normal, 0x02 ); // AX register | |
264 enum _eax = CONSTRUCT_FLAGS( 0, 0, ASM_MODIFIERS._normal, 0x04 ); // EAX register | |
265 enum _dx = CONSTRUCT_FLAGS( 0, 0, ASM_MODIFIERS._normal, 0x08 ); // DX register | |
266 enum _cl = CONSTRUCT_FLAGS( 0, 0, ASM_MODIFIERS._normal, 0x10 ); // CL register | |
267 | |
268 enum _rplus_r = 0x20; | |
269 //#define _plus_r CONSTRUCT_FLAGS( 0, 0, 0, _rplus_r ) | |
270 // Add the register to the opcode (no mod r/m) | |
271 | |
272 ubyte ASM_GET_uSizemask(uint us) { | |
273 return ((us) & 0x0F); | |
274 } | |
275 | |
276 ASM_OPERAND_TYPE ASM_GET_aopty(uint us) { | |
277 return cast(ASM_OPERAND_TYPE)(((us) & 0x70) >> 4); | |
278 } | |
279 | |
280 ASM_MODIFIERS ASM_GET_amod(uint us) { | |
281 return (cast(ASM_MODIFIERS)(((us) & 0x380) >> 7)); | |
282 } | |
283 | |
284 ubyte ASM_GET_uRegmask(uint us) { | |
285 return (((us) & 0xFC00) >> 10); | |
286 } | |
287 | |
288 enum _st = CONSTRUCT_FLAGS( 0, _float, 0, _rst ); // stack register 0 | |
289 enum _m112 = CONSTRUCT_FLAGS( 0, _m, 0, _112 ); | |
290 enum _m224 = CONSTRUCT_FLAGS( 0, _m, 0, _224 ); | |
291 enum _m512 = _m224; | |
292 enum _sti = CONSTRUCT_FLAGS( 0, _float, 0, _rsti ); | |
293 | |
294 REG regFp = { "ST", 0, _st }; | |
295 | |
296 REG[8] aregFp = [ | |
297 { "ST(0)", 0, _sti }, | |
298 { "ST(1)", 1, _sti }, | |
299 { "ST(2)", 2, _sti }, | |
300 { "ST(3)", 3, _sti }, | |
301 { "ST(4)", 4, _sti }, | |
302 { "ST(5)", 5, _sti }, | |
303 { "ST(6)", 6, _sti }, | |
304 { "ST(7)", 7, _sti } | |
305 ]; | |
306 | |
307 // For uSizemask (4 bits) | |
308 enum _8 = 0x1; | |
309 enum _16 = 0x2; | |
310 enum _32 = 0x4; | |
311 enum _48 = 0x8; | |
312 enum _anysize = (_8 | _16 | _32 | _48 ); | |
313 | |
314 enum _modrm = 0x10; | |
315 | |
316 //// This is for when the reg field of modregrm specifies which instruction it is | |
317 enum NUM_MASK = 0x7; | |
318 //#define _0 (0x0 | _modrm) // insure that some _modrm bit is set | |
319 //#define _1 0x1 // with _0 | |
320 //#define _2 0x2 | |
321 //#define _3 0x3 | |
322 //#define _4 0x4 | |
323 //#define _5 0x5 | |
324 //#define _6 0x6 | |
325 //#define _7 0x7 | |
326 // | |
327 //#define _modrm 0x10 | |
328 // | |
329 //#define _r _modrm | |
330 //#define _cb _modrm | |
331 //#define _cw _modrm | |
332 //#define _cd _modrm | |
333 //#define _cp _modrm | |
334 //#define _ib 0 | |
335 //#define _iw 0 | |
336 //#define _id 0 | |
337 //#define _rb 0 | |
338 //#define _rw 0 | |
339 //#define _rd 0 | |
340 enum _16_bit = 0x20; | |
341 enum _32_bit = 0x40; | |
342 enum _I386 = 0x80; // opcode is only for 386 and later | |
343 enum _16_bit_addr = 0x100; | |
344 enum _32_bit_addr = 0x200; | |
345 enum _fwait = 0x400; // Add an FWAIT prior to the instruction opcode | |
346 enum _nfwait = 0x800; // Do not add an FWAIT prior to the instruction | |
347 | |
348 enum MOD_MASK = 0xF000; // Mod mask | |
349 enum _modsi = 0x1000; // Instruction modifies SI | |
350 enum _moddx = 0x2000; // Instruction modifies DX | |
351 enum _mod2 = 0x3000; // Instruction modifies second operand | |
352 enum _modax = 0x4000; // Instruction modifies AX | |
353 enum _modnot1 = 0x5000; // Instruction does not modify first operand | |
354 enum _modaxdx = 0x6000; // instruction modifies AX and DX | |
355 enum _moddi = 0x7000; // Instruction modifies DI | |
356 enum _modsidi = 0x8000; // Instruction modifies SI and DI | |
357 enum _modcx = 0x9000; // Instruction modifies CX | |
358 enum _modes = 0xa000; // Instruction modifies ES | |
359 enum _modall = 0xb000; // Instruction modifies all register values | |
360 enum _modsiax = 0xc000; // Instruction modifies AX and SI | |
361 enum _modsinot1 = 0xd000; // Instruction modifies SI and not first param | |
362 | |
363 ///////////////////////////////////////////////// | |
364 // Operand flags - usOp1, usOp2, usOp3 | |
365 // | |
366 | |
367 alias ushort opflag_t; | |
368 | |
369 // Operand flags for normal opcodes | |
370 | |
371 enum _r8 = CONSTRUCT_FLAGS( _8, ASM_OPERAND_TYPE._reg, ASM_MODIFIERS._normal, 0 ); | |
372 enum _r16 = CONSTRUCT_FLAGS(_16, ASM_OPERAND_TYPE._reg, ASM_MODIFIERS._normal, 0 ); | |
373 enum _r32 = CONSTRUCT_FLAGS(_32, ASM_OPERAND_TYPE._reg, ASM_MODIFIERS._normal, 0 ); | |
374 enum _m8 = CONSTRUCT_FLAGS(_8, ASM_OPERAND_TYPE._m, ASM_MODIFIERS._normal, 0 ); | |
375 enum _m16 = CONSTRUCT_FLAGS(_16, ASM_OPERAND_TYPE._m, ASM_MODIFIERS._normal, 0 ); | |
376 enum _m32 = CONSTRUCT_FLAGS(_32, ASM_OPERAND_TYPE._m, ASM_MODIFIERS._normal, 0 ); | |
377 enum _m48 = CONSTRUCT_FLAGS( _48, ASM_OPERAND_TYPE._m, ASM_MODIFIERS._normal, 0 ); | |
378 enum _m64 = CONSTRUCT_FLAGS( _anysize, ASM_OPERAND_TYPE._m, ASM_MODIFIERS._normal, 0 ); | |
379 enum _m128 = CONSTRUCT_FLAGS( _anysize, ASM_OPERAND_TYPE._m, ASM_MODIFIERS._normal, 0 ); | |
380 enum _rm8 = CONSTRUCT_FLAGS(_8, ASM_OPERAND_TYPE._rm, ASM_MODIFIERS._normal, 0 ); | |
381 enum _rm16 = CONSTRUCT_FLAGS(_16, ASM_OPERAND_TYPE._rm, ASM_MODIFIERS._normal, 0 ); | |
382 enum _rm32 = CONSTRUCT_FLAGS(_32, ASM_OPERAND_TYPE._rm, ASM_MODIFIERS._normal, 0); | |
383 enum _r32m16 = CONSTRUCT_FLAGS(_32|_16, ASM_OPERAND_TYPE._rm, ASM_MODIFIERS._normal, 0); | |
384 enum _imm8 = CONSTRUCT_FLAGS(_8, ASM_OPERAND_TYPE._imm, ASM_MODIFIERS._normal, 0 ); | |
385 enum _imm16 = CONSTRUCT_FLAGS(_16, ASM_OPERAND_TYPE._imm, ASM_MODIFIERS._normal, 0); | |
386 enum _imm32 = CONSTRUCT_FLAGS(_32, ASM_OPERAND_TYPE._imm, ASM_MODIFIERS._normal, 0); | |
387 enum _rel8 = CONSTRUCT_FLAGS(_8, ASM_OPERAND_TYPE._rel, ASM_MODIFIERS._normal, 0); | |
388 enum _rel16 = CONSTRUCT_FLAGS(_16, ASM_OPERAND_TYPE._rel, ASM_MODIFIERS._normal, 0); | |
389 enum _rel32 = CONSTRUCT_FLAGS(_32, ASM_OPERAND_TYPE._rel, ASM_MODIFIERS._normal, 0); | |
390 enum _p1616 = CONSTRUCT_FLAGS(_32, ASM_OPERAND_TYPE._p, ASM_MODIFIERS._normal, 0); | |
391 enum _m1616 = CONSTRUCT_FLAGS(_32, ASM_OPERAND_TYPE._mnoi, ASM_MODIFIERS._normal, 0); | |
392 enum _p1632 = CONSTRUCT_FLAGS(_48, ASM_OPERAND_TYPE._p, ASM_MODIFIERS._normal, 0 ); | |
393 enum _m1632 = CONSTRUCT_FLAGS(_48, ASM_OPERAND_TYPE._mnoi, ASM_MODIFIERS._normal, 0); | |
394 enum _special = CONSTRUCT_FLAGS( 0, 0, ASM_MODIFIERS._rspecial, 0 ); | |
395 enum _seg = CONSTRUCT_FLAGS( 0, 0, ASM_MODIFIERS._rseg, 0 ); | |
396 enum _a16 = CONSTRUCT_FLAGS( 0, 0, ASM_MODIFIERS._addr16, 0 ); | |
397 enum _a32 = CONSTRUCT_FLAGS( 0, 0, ASM_MODIFIERS._addr32, 0 ); | |
398 enum _f16 = CONSTRUCT_FLAGS( 0, 0, ASM_MODIFIERS._fn16, 0); | |
399 // Near function pointer | |
400 enum _f32 = CONSTRUCT_FLAGS( 0, 0, ASM_MODIFIERS._fn32, 0); | |
401 // Far function pointer | |
402 enum _lbl = CONSTRUCT_FLAGS( 0, 0, ASM_MODIFIERS._flbl, 0 ); | |
403 // Label (in current function) | |
404 | |
405 enum _mmm32 = CONSTRUCT_FLAGS( 0, ASM_OPERAND_TYPE._m, 0, _32); | |
406 enum _mmm64 = CONSTRUCT_FLAGS( 0, ASM_OPERAND_TYPE._m, 0, _64); | |
407 enum _mmm128 = CONSTRUCT_FLAGS( 0, ASM_OPERAND_TYPE._m, 0, _128); | |
408 | |
409 enum _xmm_m32 = CONSTRUCT_FLAGS( _32, ASM_OPERAND_TYPE._m, ASM_MODIFIERS._rspecial, 0); | |
410 enum _xmm_m64 =CONSTRUCT_FLAGS( _anysize, ASM_OPERAND_TYPE._m, ASM_MODIFIERS._rspecial, 0); | |
411 enum _xmm_m128 =CONSTRUCT_FLAGS( _anysize, ASM_OPERAND_TYPE._m, ASM_MODIFIERS._rspecial, 0); | |
412 | |
413 enum _moffs8 = (_rel8); | |
414 enum _moffs16 = (_rel16 ); | |
415 enum _moffs32 = (_rel32 ); | |
416 | |
417 //////////////////////////////////////////////////////////////////// | |
418 // Operand flags for floating point opcodes are all just aliases for | |
419 // normal opcode variants and only asm_determine_operator_flags should | |
420 // need to care. | |
421 // | |
422 enum _fm80 = CONSTRUCT_FLAGS( 0, ASM_OPERAND_TYPE._m, 0, _80 ); | |
423 enum _fm64 = CONSTRUCT_FLAGS( 0, ASM_OPERAND_TYPE._m, 0, _64 ); | |
424 enum _fm128 = CONSTRUCT_FLAGS( 0, ASM_OPERAND_TYPE._m, 0, _128 ); | |
425 enum _fanysize = (_64 | _80 | _112 | _224); | |
426 | |
427 enum _AL =0; | |
428 enum _AH =4; | |
429 enum _AX =0; | |
430 enum _EAX =0; | |
431 enum _BL =3; | |
432 enum _BH =7; | |
433 enum _BX =3; | |
434 enum _EBX =3; | |
435 enum _CL =1; | |
436 enum _CH =5; | |
437 enum _CX =1; | |
438 enum _ECX =1; | |
439 enum _DL =2; | |
440 enum _DH =6; | |
441 enum _DX =2; | |
442 enum _EDX =2; | |
443 enum _BP =5; | |
444 enum _EBP =5; | |
445 enum _SP =4; | |
446 enum _ESP =4; | |
447 enum _DI =7; | |
448 enum _EDI =7; | |
449 enum _SI =6; | |
450 enum _ESI =6; | |
451 enum _ES =0; | |
452 enum _CS =1; | |
453 enum _SS =2; | |
454 enum _DS =3; | |
455 enum _GS =5; | |
456 enum _FS =4; | |
457 | |
458 enum ASM = 0x36; // string of asm bytes, actually an SS: opcode | |
459 enum ASM_END = 0xffff; // special opcode meaning end of table | |
460 | |
461 struct PTRNTAB0 | |
462 { | |
463 uint usOpcode; | |
464 // #define ASM_END 0xffff // special opcode meaning end of table | |
465 ushort usFlags; | |
466 } | |
467 | |
468 struct PTRNTAB1 | |
469 { | |
470 uint usOpcode; | |
471 ushort usFlags; | |
472 opflag_t usOp1; | |
473 } | |
474 | |
475 struct PTRNTAB2 | |
476 { | |
477 uint usOpcode; | |
478 ushort usFlags; | |
479 opflag_t usOp1; | |
480 opflag_t usOp2; | |
481 } | |
482 | |
483 struct PTRNTAB3 | |
484 { | |
485 uint usOpcode; | |
486 ushort usFlags; | |
487 opflag_t usOp1; | |
488 opflag_t usOp2; | |
489 opflag_t usOp3; | |
490 } | |
491 | |
492 union PTRNTAB | |
493 { | |
494 PTRNTAB0 *pptb0; | |
495 PTRNTAB1 *pptb1; | |
496 PTRNTAB2 *pptb2; | |
497 PTRNTAB3 *pptb3; | |
498 } | |
499 | |
500 struct OP | |
501 { | |
502 ubyte usNumops; | |
503 PTRNTAB ptb; | |
504 } | |
505 | |
506 enum ASM_JUMPTYPE | |
507 { | |
508 ASM_JUMPTYPE_UNSPECIFIED, | |
509 ASM_JUMPTYPE_SHORT, | |
510 ASM_JUMPTYPE_NEAR, | |
511 ASM_JUMPTYPE_FAR | |
512 } // ajt | |
513 | |
514 mixin(BringToCurrentScope!(ASM_JUMPTYPE)); | |
515 | |
516 struct OPND | |
517 { | |
518 REG* base; // if plain register | |
519 REG* pregDisp1; // if [register1] | |
520 REG* pregDisp2; | |
521 REG* segreg; // if segment override | |
522 char indirect = 0; // if had a '*' or '.' | |
523 char bOffset = 0; // if 'offset' keyword | |
524 char bSeg = 0; // if 'segment' keyword | |
525 char bPtr = 0; // if 'ptr' keyword | |
526 uint uchMultiplier; // register multiplier; valid values are 0,1,2,4,8 | |
527 opflag_t usFlags; | |
528 Dsymbol s; | |
529 int disp; | |
530 real real_ = 0; | |
531 Type ptype; | |
532 ASM_JUMPTYPE ajt; | |
533 } | |
534 | |
535 struct ASM_STATE | |
536 { | |
537 ubyte ucItype; // Instruction type | |
538 Loc loc; | |
539 ubyte bInit; | |
540 LabelDsymbol psDollar; | |
541 Dsymbol psLocalsize; | |
542 jmp_buf env; | |
543 ubyte bReturnax; | |
544 AsmStatement statement; | |
545 Scope sc; | |
546 } | |
547 | |
548 enum IT | |
549 { | |
550 ITprefix = 0x10, // special prefix | |
551 ITjump = 0x20, // jump instructions CALL, Jxx and LOOPxx | |
552 ITimmed = 0x30, // value of an immediate operand controls | |
553 // code generation | |
554 ITopt = 0x40, // not all operands are required | |
555 ITshift = 0x50, // rotate and shift instructions | |
556 ITfloat = 0x60, // floating point coprocessor instructions | |
557 ITdata = 0x70, // DB, DW, DD, DQ, DT pseudo-ops | |
558 ITaddr = 0x80, // DA (define addresss) pseudo-op | |
559 ITMASK = 0xF0, | |
560 ITSIZE = 0x0F, // mask for size | |
561 } | |
562 | |
563 alias IT.ITprefix ITprefix; | |
564 alias IT.ITjump ITjump; | |
565 alias IT.ITimmed ITimmed; | |
566 alias IT.ITopt ITopt; | |
567 alias IT.ITshift ITshift; | |
568 alias IT.ITfloat ITfloat; | |
569 alias IT.ITdata ITdata; | |
570 alias IT.ITaddr ITaddr; | |
571 alias IT.ITMASK ITMASK; | |
572 alias IT.ITSIZE ITSIZE; | |
573 | |
574 __gshared ASM_STATE asmstate; | |
575 __gshared Token* asmtok; | |
576 __gshared TOK tok_value; | |
577 | |
578 // Additional tokens for the inline assembler | |
579 enum ASMTK | |
580 { | |
581 ASMTKlocalsize = TOKMAX + 1, | |
582 ASMTKdword, | |
583 ASMTKeven, | |
584 ASMTKfar, | |
585 ASMTKnaked, | |
586 ASMTKnear, | |
587 ASMTKptr, | |
588 ASMTKqword, | |
589 ASMTKseg, | |
590 ASMTKword, | |
591 ASMTKmax = ASMTKword - (TOK.TOKMAX + 1) + 1 | |
592 } | |
593 | |
594 mixin(BringToCurrentScope!(ASMTK)); | |
595 | |
596 enum OP_DB | |
597 { | |
598 ///version (SCPP) { | |
599 /// // These are the number of bytes | |
600 /// OPdb = 1, | |
601 /// OPdw = 2, | |
602 /// OPdd = 4, | |
603 /// OPdq = 8, | |
604 /// OPdt = 10, | |
605 /// OPdf = 4, | |
606 /// OPde = 10, | |
607 /// OPds = 2, | |
608 /// OPdi = 4, | |
609 /// OPdl = 8, | |
610 ///} | |
611 ///version (MARS) { | |
612 // Integral types | |
613 OPdb, | |
614 OPds, | |
615 OPdi, | |
616 OPdl, | |
617 | |
618 // Float types | |
619 OPdf, | |
620 OPdd, | |
621 OPde, | |
622 | |
623 // Deprecated | |
624 OPdw = OPds, | |
625 OPdq = OPdl, | |
626 OPdt = OPde, | |
627 ///} | |
628 } | |
629 | |
630 OPND* opnd_calloc() | |
631 { | |
632 return new OPND(); | |
633 } | |
634 | |
635 void opnd_free(OPND* o) | |
636 { | |
637 delete o; | |
638 } | |
639 | |
640 /****************************** | |
641 * Convert assembly instruction into a code, and append | |
642 * it to the code generated for this block. | |
643 */ | |
644 | |
645 code* asm_emit(Loc loc, uint usNumops, PTRNTAB ptb, OP* pop, OPND* popnd1, OPND* popnd2, OPND* popnd3) | |
646 { | |
647 debug { | |
648 ubyte auchOpcode[16]; | |
649 uint usIdx = 0; | |
650 void emit(ubyte op) { | |
651 auchOpcode[usIdx++] = op; | |
652 } | |
653 } else { | |
654 void emit(ubyte op) {} | |
655 } | |
656 Identifier id; | |
657 // ushort us; | |
658 ubyte* puc; | |
659 uint usDefaultseg; | |
660 code* pc = null; | |
661 OPND* popndTmp; | |
662 ASM_OPERAND_TYPE aoptyTmp; | |
663 ushort uSizemaskTmp; | |
664 REG* pregSegment; | |
665 code* pcPrefix = null; | |
666 | |
667 uint uSizemask1 = 0; | |
668 uint uSizemask2 = 0; | |
669 uint uSizemask3 = 0; | |
670 | |
671 //ASM_OPERAND_TYPE aopty1 = ASM_OPERAND_TYPE._reg , aopty2 = 0, aopty3 = 0; | |
672 ASM_MODIFIERS amod1 = ASM_MODIFIERS._normal; | |
673 ASM_MODIFIERS amod2 = ASM_MODIFIERS._normal; | |
674 ASM_MODIFIERS amod3 = ASM_MODIFIERS._normal; | |
675 | |
676 uint uRegmask1 = 0; | |
677 uint uRegmask2 = 0; | |
678 uint uRegmask3 = 0; | |
679 | |
680 uint uSizemaskTable1 = 0; | |
681 uint uSizemaskTable2 = 0; | |
682 uint uSizemaskTable3 = 0; | |
683 | |
684 ASM_OPERAND_TYPE aoptyTable1 = ASM_OPERAND_TYPE._reg; | |
685 ASM_OPERAND_TYPE aoptyTable2 = ASM_OPERAND_TYPE._reg; | |
686 ASM_OPERAND_TYPE aoptyTable3 = ASM_OPERAND_TYPE._reg; | |
687 | |
688 ASM_MODIFIERS amodTable1 = ASM_MODIFIERS._normal; | |
689 ASM_MODIFIERS amodTable2 = ASM_MODIFIERS._normal; | |
690 ASM_MODIFIERS amodTable3 = ASM_MODIFIERS._normal; | |
691 | |
692 uint uRegmaskTable1 = 0; | |
693 uint uRegmaskTable2 = 0; | |
694 uint uRegmaskTable3 = 0; | |
695 | |
696 pc = code_calloc(); | |
697 pc.Iflags |= CF.CFpsw; // assume we want to keep the flags | |
698 | |
699 if (popnd1) | |
700 { | |
701 uSizemask1 = ASM_GET_uSizemask(popnd1.usFlags); | |
702 //aopty1 = ASM_GET_aopty(popnd1.usFlags); | |
703 amod1 = ASM_GET_amod(popnd1.usFlags); | |
704 uRegmask1 = ASM_GET_uRegmask(popnd1.usFlags); | |
705 | |
706 uSizemaskTable1 = ASM_GET_uSizemask(ptb.pptb1.usOp1); | |
707 aoptyTable1 = ASM_GET_aopty(ptb.pptb1.usOp1); | |
708 amodTable1 = ASM_GET_amod(ptb.pptb1.usOp1); | |
709 uRegmaskTable1 = ASM_GET_uRegmask(ptb.pptb1.usOp1); | |
710 | |
711 } | |
712 | |
713 if (popnd2) | |
714 { | |
715 static if (false) { | |
716 printf("\nasm_emit:\nop: "); | |
717 asm_output_flags(popnd2.usFlags); | |
718 printf("\ntb: "); | |
719 asm_output_flags(ptb.pptb2.usOp2); | |
720 printf("\n"); | |
721 } | |
722 uSizemask2 = ASM_GET_uSizemask(popnd2.usFlags); | |
723 //aopty2 = ASM_GET_aopty(popnd2.usFlags); | |
724 amod2 = ASM_GET_amod(popnd2.usFlags); | |
725 uRegmask2 = ASM_GET_uRegmask(popnd2.usFlags); | |
726 | |
727 uSizemaskTable2 = ASM_GET_uSizemask(ptb.pptb2.usOp2); | |
728 aoptyTable2 = ASM_GET_aopty(ptb.pptb2.usOp2); | |
729 amodTable2 = ASM_GET_amod(ptb.pptb2.usOp2); | |
730 uRegmaskTable2 = ASM_GET_uRegmask(ptb.pptb2.usOp2); | |
731 } | |
732 if (popnd3) | |
733 { | |
734 uSizemask3 = ASM_GET_uSizemask(popnd3.usFlags); | |
735 //aopty3 = ASM_GET_aopty(popnd3.usFlags); | |
736 amod3 = ASM_GET_amod(popnd3.usFlags); | |
737 uRegmask3 = ASM_GET_uRegmask(popnd3.usFlags); | |
738 | |
739 uSizemaskTable3 = ASM_GET_uSizemask(ptb.pptb3.usOp3); | |
740 aoptyTable3 = ASM_GET_aopty(ptb.pptb3.usOp3); | |
741 amodTable3 = ASM_GET_amod(ptb.pptb3.usOp3); | |
742 uRegmaskTable3 = ASM_GET_uRegmask(ptb.pptb3.usOp3); | |
743 } | |
744 | |
745 asmstate.statement.regs |= asm_modify_regs(ptb, popnd1, popnd2); | |
746 | |
747 if (!I32 && ptb.pptb0.usFlags & _I386) | |
748 { | |
749 switch (usNumops) | |
750 { | |
751 case 0: | |
752 break; | |
753 | |
754 case 1: | |
755 if (popnd1 && popnd1.s) | |
756 { | |
757 L386_WARNING: | |
758 id = popnd1.s.ident; | |
759 L386_WARNING2: | |
760 if (config.target_cpu < TARGET.TARGET_80386) | |
761 { | |
762 // Reference to %s caused a 386 instruction to be generated | |
763 //warerr(WM_386_op, id.toChars()); | |
764 } | |
765 } | |
766 break; | |
767 | |
768 case 2: | |
769 case 3: // The third operand is always an ASM_OPERAND_TYPE._imm | |
770 if (popnd1 && popnd1.s) | |
771 goto L386_WARNING; | |
772 if (popnd2 && popnd2.s) | |
773 { | |
774 id = popnd2.s.ident; | |
775 goto L386_WARNING2; | |
776 } | |
777 break; | |
778 } | |
779 } | |
780 | |
781 switch (usNumops) | |
782 { | |
783 case 0: | |
784 if ((I32 && (ptb.pptb0.usFlags & _16_bit)) || (!I32 && (ptb.pptb0.usFlags & _32_bit))) | |
785 { | |
786 emit(0x66); | |
787 pc.Iflags |= CF.CFopsize; | |
788 } | |
789 break; | |
790 | |
791 // 3 and 2 are the same because the third operand is always | |
792 // an immediate and does not affect operation size | |
793 case 3: | |
794 case 2: | |
795 if ((I32 && | |
796 (amod2 == ASM_MODIFIERS._addr16 || | |
797 (uSizemaskTable2 & _16 && aoptyTable2 == ASM_OPERAND_TYPE._rel) || | |
798 (uSizemaskTable2 & _32 && aoptyTable2 == ASM_OPERAND_TYPE._mnoi) || | |
799 (ptb.pptb2.usFlags & _16_bit_addr) | |
800 ) | |
801 ) || | |
802 (!I32 && | |
803 (amod2 == ASM_MODIFIERS._addr32 || | |
804 (uSizemaskTable2 & _32 && aoptyTable2 == ASM_OPERAND_TYPE._rel) || | |
805 (uSizemaskTable2 & _48 && aoptyTable2 == ASM_OPERAND_TYPE._mnoi) || | |
806 (ptb.pptb2.usFlags & _32_bit_addr))) | |
807 ) | |
808 { | |
809 emit(0x67); | |
810 pc.Iflags |= CF.CFaddrsize; | |
811 | |
812 if (I32) | |
813 amod2 = ASM_MODIFIERS._addr16; | |
814 else | |
815 amod2 = ASM_MODIFIERS._addr32; | |
816 | |
817 popnd2.usFlags &= ~CONSTRUCT_FLAGS(0,0,7,0); | |
818 popnd2.usFlags |= CONSTRUCT_FLAGS(0,0,amod2,0); | |
819 } | |
820 | |
821 | |
822 /* Fall through, operand 1 controls the opsize, but the | |
823 address size can be in either operand 1 or operand 2, | |
824 hence the extra checking the flags tested for SHOULD | |
825 be mutex on operand 1 and operand 2 because there is | |
826 only one MOD R/M byte | |
827 */ | |
828 | |
829 case 1: | |
830 if ((I32 && | |
831 (amod1 == ASM_MODIFIERS._addr16 || | |
832 (uSizemaskTable1 & _16 && aoptyTable1 == ASM_OPERAND_TYPE._rel) || | |
833 (uSizemaskTable1 & _32 && aoptyTable1 == ASM_OPERAND_TYPE._mnoi) || | |
834 (ptb.pptb1.usFlags & _16_bit_addr))) || | |
835 (!I32 && | |
836 (amod1 == ASM_MODIFIERS._addr32 || | |
837 (uSizemaskTable1 & _32 && aoptyTable1 == ASM_OPERAND_TYPE._rel) || | |
838 (uSizemaskTable1 & _48 && aoptyTable1 == ASM_OPERAND_TYPE._mnoi) || | |
839 (ptb.pptb1.usFlags & _32_bit_addr)))) | |
840 { | |
841 emit(0x67); // address size prefix | |
842 pc.Iflags |= CF.CFaddrsize; | |
843 if (I32) | |
844 amod1 = ASM_MODIFIERS._addr16; | |
845 else | |
846 amod1 = ASM_MODIFIERS._addr32; | |
847 popnd1.usFlags &= ~CONSTRUCT_FLAGS(0,0,7,0); | |
848 popnd1.usFlags |= CONSTRUCT_FLAGS(0,0,amod1,0); | |
849 } | |
850 | |
851 // If the size of the operand is unknown, assume that it is | |
852 // the default size | |
853 if ((I32 && (ptb.pptb0.usFlags & _16_bit)) || | |
854 (!I32 && (ptb.pptb0.usFlags & _32_bit))) | |
855 { | |
856 //if (asmstate.ucItype != ITjump) | |
857 { emit(0x66); | |
858 pc.Iflags |= CF.CFopsize; | |
859 } | |
860 } | |
861 if (((pregSegment = (popndTmp = popnd1).segreg) != null) || | |
862 ((popndTmp = popnd2) != null && | |
863 (pregSegment = popndTmp.segreg) != null) | |
864 ) | |
865 { | |
866 if ((popndTmp.pregDisp1 && | |
867 popndTmp.pregDisp1.val == _BP) || | |
868 popndTmp.pregDisp2 && | |
869 popndTmp.pregDisp2.val == _BP) | |
870 usDefaultseg = _SS; | |
871 else | |
872 usDefaultseg = _DS; | |
873 if (pregSegment.val != usDefaultseg) | |
874 switch (pregSegment.val) { | |
875 case _CS: | |
876 emit(0x2e); | |
877 pc.Iflags |= CF.CFcs; | |
878 break; | |
879 case _SS: | |
880 emit(0x36); | |
881 pc.Iflags |= CF.CFss; | |
882 break; | |
883 case _DS: | |
884 emit(0x3e); | |
885 pc.Iflags |= CF.CFds; | |
886 break; | |
887 case _ES: | |
888 emit(0x26); | |
889 pc.Iflags |= CF.CFes; | |
890 break; | |
891 case _FS: | |
892 emit(0x64); | |
893 pc.Iflags |= CF.CFfs; | |
894 break; | |
895 case _GS: | |
896 emit(0x65); | |
897 pc.Iflags |= CF.CFgs; | |
898 break; | |
899 default: | |
900 assert(0); | |
901 } | |
902 } | |
903 break; | |
904 } | |
905 uint usOpcode = ptb.pptb0.usOpcode; | |
906 | |
907 if ((usOpcode & 0xFFFFFF00) == 0x660F3A00 || // SSE4 | |
908 (usOpcode & 0xFFFFFF00) == 0x660F3800) // SSE4 | |
909 { | |
910 pc.Iflags |= CF.CFopsize; | |
911 pc.Iop = 0x0F; | |
912 pc.Iop2 = (usOpcode >> 8) & 0xFF; | |
913 pc.Iop3 = usOpcode & 0xFF; | |
914 goto L3; | |
915 } | |
916 switch (usOpcode & 0xFF0000) | |
917 { | |
918 case 0: | |
919 break; | |
920 | |
921 case 0x660000: | |
922 pc.Iflags |= CF.CFopsize; | |
923 usOpcode &= 0xFFFF; | |
924 break; | |
925 | |
926 case 0xF20000: // REPNE | |
927 case 0xF30000: // REP/REPE | |
928 // BUG: What if there's an address size prefix or segment | |
929 // override prefix? Must the REP be adjacent to the rest | |
930 // of the opcode? | |
931 pcPrefix = code_calloc(); | |
932 pcPrefix.Iop = cast(ubyte)(usOpcode >> 16); | |
933 usOpcode &= 0xFFFF; | |
934 break; | |
935 | |
936 case 0x0F0000: // an AMD instruction | |
937 puc = (cast(ubyte*) &usOpcode); | |
938 if (puc[1] != 0x0F) // if not AMD instruction 0x0F0F | |
939 goto L4; | |
940 emit(puc[2]); | |
941 emit(puc[1]); | |
942 emit(puc[0]); | |
943 pc.Iop = puc[2]; | |
944 pc.Iop2 = puc[1]; | |
945 pc.IEVint2() = puc[0]; | |
946 pc.IFL2 = FL.FLconst; | |
947 goto L3; | |
948 | |
949 default: | |
950 puc = (cast(ubyte*) &usOpcode); | |
951 L4: | |
952 emit(puc[2]); | |
953 emit(puc[1]); | |
954 emit(puc[0]); | |
955 pc.Iop = puc[2]; | |
956 pc.Iop2 = puc[1]; | |
957 pc.Irm = puc[0]; | |
958 goto L3; | |
959 } | |
960 if (usOpcode & 0xff00) | |
961 { | |
962 puc = (cast(ubyte*) &(usOpcode)); | |
963 emit(puc[1]); | |
964 emit(puc[0]); | |
965 pc.Iop = puc[1]; | |
966 if (pc.Iop == 0x0f) | |
967 pc.Iop2 = puc[0]; | |
968 else | |
969 { | |
970 if (usOpcode == 0xDFE0) // FSTSW AX | |
971 { pc.Irm = puc[0]; | |
972 goto L2; | |
973 } | |
974 if (asmstate.ucItype == IT.ITfloat) | |
975 pc.Irm = puc[0]; | |
976 else | |
977 { | |
978 pc.IEVint2() = puc[0]; | |
979 pc.IFL2 = FL.FLconst; | |
980 } | |
981 } | |
982 } | |
983 else | |
984 { | |
985 emit(cast(ubyte)usOpcode); | |
986 pc.Iop = cast(ubyte)usOpcode; | |
987 } | |
988 L3: ; | |
989 | |
990 // If CALL, Jxx or LOOPx to a symbolic location | |
991 if (/*asmstate.ucItype == ITjump &&*/ | |
992 popnd1 && popnd1.s && popnd1.s.isLabel()) | |
993 { | |
994 Dsymbol s = popnd1.s; | |
995 if (s == asmstate.psDollar) | |
996 { | |
997 pc.IFL2 = FL.FLconst; | |
998 if (uSizemaskTable1 & (_8 | _16)) | |
999 pc.IEVint2() = popnd1.disp; | |
1000 else if (uSizemaskTable1 & _32) | |
1001 pc.IEVpointer2() = cast(targ_size_t) popnd1.disp; | |
1002 } | |
1003 else | |
1004 { | |
1005 LabelDsymbol label = s.isLabel(); | |
1006 if (label) | |
1007 { | |
1008 if ((pc.Iop & 0xF0) == 0x70) | |
1009 pc.Iflags |= CF.CFjmp16; | |
1010 if (usNumops == 1) | |
1011 { | |
1012 pc.IFL2 = FL.FLblock; | |
1013 pc.IEVlsym2() = label; | |
1014 } | |
1015 else | |
1016 { | |
1017 pc.IFL1 = FL.FLblock; | |
1018 pc.IEVlsym1() = label; | |
1019 } | |
1020 } | |
1021 } | |
1022 } | |
1023 | |
1024 switch (usNumops) | |
1025 { | |
1026 case 0: | |
1027 break; | |
1028 | |
1029 case 1: | |
1030 if (((aoptyTable1 == ASM_OPERAND_TYPE._reg || aoptyTable1 == ASM_OPERAND_TYPE._float) && | |
1031 amodTable1 == ASM_MODIFIERS._normal && (uRegmaskTable1 & _rplus_r))) | |
1032 { | |
1033 if (asmstate.ucItype == IT.ITfloat) | |
1034 pc.Irm += popnd1.base.val; | |
1035 else if (pc.Iop == 0x0f) | |
1036 pc.Iop2 += popnd1.base.val; | |
1037 else | |
1038 pc.Iop += popnd1.base.val; | |
1039 debug { | |
1040 auchOpcode[usIdx-1] += popnd1.base.val; | |
1041 } | |
1042 } | |
1043 else | |
1044 { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1045 debug asm_make_modrm_byte( |
0 | 1046 auchOpcode, &usIdx, |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1047 pc, |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1048 ptb.pptb1.usFlags, |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1049 popnd1, null); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1050 else asm_make_modrm_byte( |
0 | 1051 pc, |
1052 ptb.pptb1.usFlags, | |
1053 popnd1, null); | |
1054 } | |
1055 | |
1056 popndTmp = popnd1; | |
1057 aoptyTmp = aoptyTable1; | |
1058 uSizemaskTmp = cast(ushort)uSizemaskTable1; | |
1059 L1: | |
1060 if (aoptyTmp == ASM_OPERAND_TYPE._imm) | |
1061 { | |
1062 Declaration d = popndTmp.s ? popndTmp.s.isDeclaration() : null; | |
1063 if (popndTmp.bSeg) | |
1064 { | |
1065 if (!(d && d.isDataseg())) | |
1066 asmerr(ASMERRMSGS.EM_bad_addr_mode); // illegal addressing mode | |
1067 } | |
1068 switch (uSizemaskTmp) | |
1069 { | |
1070 case _8: | |
1071 case _16: | |
1072 case _32: | |
1073 if (popndTmp.s is asmstate.psLocalsize) | |
1074 { | |
1075 pc.IFL2 = FL.FLlocalsize; | |
1076 pc.IEVdsym2() = null; | |
1077 pc.Iflags |= CF.CFoff; | |
1078 pc.IEVoffset2() = popndTmp.disp; | |
1079 } | |
1080 else if (d) | |
1081 { | |
1082 static if (false) { | |
1083 if ((pc.IFL2 = d.Sfl) == 0) | |
1084 pc.IFL2 = FL.FLdsymbol; | |
1085 } else { | |
1086 pc.IFL2 = FL.FLdsymbol; | |
1087 } | |
1088 pc.Iflags &= ~(CF.CFseg | CF.CFoff); | |
1089 if (popndTmp.bSeg) | |
1090 pc.Iflags |= CF.CFseg; | |
1091 else | |
1092 pc.Iflags |= CF.CFoff; | |
1093 | |
1094 pc.IEVoffset2() = popndTmp.disp; | |
1095 pc.IEVdsym2() = d; | |
1096 } | |
1097 else | |
1098 { | |
1099 pc.IEVint2() = popndTmp.disp; | |
1100 pc.IFL2 = FL.FLconst; | |
1101 } | |
1102 break; | |
1103 } | |
1104 } | |
1105 | |
1106 break; | |
1107 case 2: | |
1108 // | |
1109 // If there are two immediate operands then | |
1110 // | |
1111 if (aoptyTable1 == ASM_OPERAND_TYPE._imm && aoptyTable2 == ASM_OPERAND_TYPE._imm) | |
1112 { | |
1113 pc.IEVint1() = popnd1.disp; | |
1114 pc.IFL1 = FL.FLconst; | |
1115 pc.IEVint2() = popnd2.disp; | |
1116 pc.IFL2 = FL.FLconst; | |
1117 break; | |
1118 } | |
1119 if (aoptyTable2 == ASM_OPERAND_TYPE._m || | |
1120 aoptyTable2 == ASM_OPERAND_TYPE._rel || | |
1121 // If not MMX register (_mm) or XMM register (_xmm) | |
1122 (amodTable1 == ASM_MODIFIERS._rspecial && !(uRegmaskTable1 & (0x08 | 0x10)) && !uSizemaskTable1) || | |
1123 aoptyTable2 == ASM_OPERAND_TYPE._rm || | |
1124 (popnd1.usFlags == _r32 && popnd2.usFlags == _xmm) || | |
1125 (popnd1.usFlags == _r32 && popnd2.usFlags == _mm)) | |
1126 { | |
1127 static if (false) { | |
1128 printf("test4 %d,%d,%d,%d\n", | |
1129 (aoptyTable2 == ASM_OPERAND_TYPE._m), | |
1130 (aoptyTable2 == ASM_OPERAND_TYPE._rel), | |
1131 (amodTable1 == ASM_MODIFIERS._rspecial && !(uRegmaskTable1 & (0x08 | 0x10))), | |
1132 (aoptyTable2 == ASM_OPERAND_TYPE._rm) | |
1133 ); | |
1134 printf("usOpcode = %x\n", usOpcode); | |
1135 } | |
1136 if (ptb.pptb0.usOpcode == 0x0F7E || // MOVD _rm32,_mm | |
1137 ptb.pptb0.usOpcode == 0x660F7E // MOVD _rm32,_xmm | |
1138 ) | |
1139 { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1140 debug asm_make_modrm_byte( |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1141 auchOpcode, &usIdx, |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1142 pc, |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1143 ptb.pptb1.usFlags, |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1144 popnd1, popnd2); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1145 else asm_make_modrm_byte(pc, ptb.pptb1.usFlags, popnd1, popnd2); |
0 | 1146 } |
1147 else | |
1148 { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1149 debug asm_make_modrm_byte( |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1150 auchOpcode, &usIdx, |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1151 pc, |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1152 ptb.pptb1.usFlags, |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1153 popnd2, popnd1); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1154 else asm_make_modrm_byte(pc, ptb.pptb1.usFlags, popnd2, popnd1); |
0 | 1155 } |
1156 popndTmp = popnd1; | |
1157 aoptyTmp = aoptyTable1; | |
1158 uSizemaskTmp = cast(ushort)uSizemaskTable1; | |
1159 } | |
1160 else | |
1161 { | |
1162 if (((aoptyTable1 == ASM_OPERAND_TYPE._reg || aoptyTable1 == ASM_OPERAND_TYPE._float) && | |
1163 amodTable1 == ASM_MODIFIERS._normal && | |
1164 (uRegmaskTable1 & _rplus_r))) | |
1165 { | |
1166 if (asmstate.ucItype == IT.ITfloat) | |
1167 pc.Irm += popnd1.base.val; | |
1168 else if (pc.Iop == 0x0f) | |
1169 pc.Iop2 += popnd1.base.val; | |
1170 else | |
1171 pc.Iop += popnd1.base.val; | |
1172 debug { | |
1173 auchOpcode[usIdx-1] += popnd1.base.val; | |
1174 } | |
1175 } | |
1176 else if (((aoptyTable2 == ASM_OPERAND_TYPE._reg || aoptyTable2 == ASM_OPERAND_TYPE._float) && | |
1177 amodTable2 == ASM_MODIFIERS._normal && | |
1178 (uRegmaskTable2 & _rplus_r))) | |
1179 { | |
1180 if (asmstate.ucItype == IT.ITfloat) | |
1181 pc.Irm += popnd2.base.val; | |
1182 else if (pc.Iop == 0x0f) | |
1183 pc.Iop2 += popnd2.base.val; | |
1184 else | |
1185 pc.Iop += popnd2.base.val; | |
1186 debug { | |
1187 auchOpcode[usIdx-1] += popnd2.base.val; | |
1188 } | |
1189 } | |
1190 else if (ptb.pptb0.usOpcode == 0xF30FD6 || | |
1191 ptb.pptb0.usOpcode == 0x0F12 || | |
1192 ptb.pptb0.usOpcode == 0x0F16 || | |
1193 ptb.pptb0.usOpcode == 0x660F50 || | |
1194 ptb.pptb0.usOpcode == 0x0F50 || | |
1195 ptb.pptb0.usOpcode == 0x660FD7 || | |
1196 ptb.pptb0.usOpcode == 0x0FD7) | |
1197 { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1198 debug asm_make_modrm_byte( |
0 | 1199 ///debug { |
1200 auchOpcode, &usIdx, | |
1201 ///} | |
1202 pc, | |
1203 ptb.pptb1.usFlags, | |
1204 popnd2, popnd1); | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1205 else asm_make_modrm_byte( |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1206 pc, |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1207 ptb.pptb1.usFlags, |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1208 popnd2, popnd1); |
0 | 1209 } |
1210 else | |
1211 { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1212 debug asm_make_modrm_byte( |
0 | 1213 ///debug { |
1214 auchOpcode, &usIdx, | |
1215 ///} | |
1216 pc, | |
1217 ptb.pptb1.usFlags, | |
1218 popnd1, popnd2); | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1219 else asm_make_modrm_byte( |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1220 pc, |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1221 ptb.pptb1.usFlags, |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1222 popnd1, popnd2); |
0 | 1223 |
1224 } | |
1225 if (aoptyTable1 == ASM_OPERAND_TYPE._imm) | |
1226 { | |
1227 popndTmp = popnd1; | |
1228 aoptyTmp = aoptyTable1; | |
1229 uSizemaskTmp = cast(ushort)uSizemaskTable1; | |
1230 } | |
1231 else | |
1232 { | |
1233 popndTmp = popnd2; | |
1234 aoptyTmp = aoptyTable2; | |
1235 uSizemaskTmp = cast(ushort)uSizemaskTable2; | |
1236 } | |
1237 } | |
1238 goto L1; | |
1239 | |
1240 case 3: | |
1241 if (aoptyTable2 == ASM_OPERAND_TYPE._m || aoptyTable2 == ASM_OPERAND_TYPE._rm || | |
1242 usOpcode == 0x0FC5) // PEXTRW | |
1243 { | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1244 debug asm_make_modrm_byte( |
0 | 1245 ///debug { |
1246 auchOpcode, &usIdx, | |
1247 ///} | |
1248 pc, | |
1249 ptb.pptb1.usFlags, | |
1250 popnd2, popnd1); | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1251 else asm_make_modrm_byte( |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1252 pc, |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1253 ptb.pptb1.usFlags, |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1254 popnd2, popnd1); |
0 | 1255 popndTmp = popnd3; |
1256 aoptyTmp = aoptyTable3; | |
1257 uSizemaskTmp = cast(ushort)uSizemaskTable3; | |
1258 } else { | |
1259 if (((aoptyTable1 == ASM_OPERAND_TYPE._reg || aoptyTable1 == ASM_OPERAND_TYPE._float) && | |
1260 amodTable1 == ASM_MODIFIERS._normal && | |
1261 (uRegmaskTable1 &_rplus_r))) | |
1262 { | |
1263 if (asmstate.ucItype == IT.ITfloat) | |
1264 pc.Irm += popnd1.base.val; | |
1265 else if (pc.Iop == 0x0f) | |
1266 pc.Iop2 += popnd1.base.val; | |
1267 else | |
1268 pc.Iop += popnd1.base.val; | |
1269 debug { | |
1270 auchOpcode[usIdx-1] += popnd1.base.val; | |
1271 } | |
1272 } | |
1273 else if (((aoptyTable2 == ASM_OPERAND_TYPE._reg || aoptyTable2 == ASM_OPERAND_TYPE._float) && | |
1274 amodTable2 == ASM_MODIFIERS._normal && | |
1275 (uRegmaskTable2 &_rplus_r))) | |
1276 { | |
1277 if (asmstate.ucItype == IT.ITfloat) | |
1278 pc.Irm += popnd1.base.val; | |
1279 else if (pc.Iop == 0x0f) | |
1280 pc.Iop2 += popnd1.base.val; | |
1281 else | |
1282 pc.Iop += popnd2.base.val; | |
1283 debug { | |
1284 auchOpcode[usIdx-1] += popnd2.base.val; | |
1285 } | |
1286 } | |
1287 else | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1288 { |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1289 debug asm_make_modrm_byte( |
0 | 1290 ///debug { |
1291 auchOpcode, &usIdx, | |
1292 ///} | |
1293 pc, | |
1294 ptb.pptb1.usFlags, | |
1295 popnd1, popnd2); | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1296 else asm_make_modrm_byte( |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1297 pc, |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1298 ptb.pptb1.usFlags, |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1299 popnd1, popnd2); |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
1300 } |
0 | 1301 popndTmp = popnd3; |
1302 aoptyTmp = aoptyTable3; | |
1303 uSizemaskTmp = cast(ushort)uSizemaskTable3; | |
1304 } | |
1305 goto L1; | |
1306 } | |
1307 L2: | |
1308 | |
1309 if ((pc.Iop & 0xF8) == 0xD8 && | |
1310 ADDFWAIT() && | |
1311 !(ptb.pptb0.usFlags & _nfwait)) | |
1312 pc.Iflags |= CF.CFwait; | |
1313 else if ((ptb.pptb0.usFlags & _fwait) && | |
1314 config.target_cpu >= TARGET.TARGET_80386) | |
1315 pc.Iflags |= CF.CFwait; | |
1316 | |
1317 debug { | |
1318 if (debuga) | |
1319 { | |
1320 uint u; | |
1321 | |
1322 for (u = 0; u < usIdx; u++) | |
1323 printf(" %02X", auchOpcode[u]); | |
1324 | |
1325 printf("\t%s\t", asm_opstr(pop)); | |
1326 if (popnd1) | |
1327 asm_output_popnd(popnd1); | |
1328 if (popnd2) { | |
1329 printf(","); | |
1330 asm_output_popnd(popnd2); | |
1331 } | |
1332 if (popnd3) { | |
1333 printf(","); | |
1334 asm_output_popnd(popnd3); | |
1335 } | |
1336 printf("\n"); | |
1337 } | |
1338 } | |
1339 | |
1340 pc = cat(pcPrefix, pc); | |
1341 pc = asm_genloc(loc, pc); | |
1342 return pc; | |
1343 } | |
1344 | |
1345 void asm_token_trans(Token* tok) | |
1346 { | |
1347 tok_value = TOK.TOKeof; | |
1348 | |
1349 if (tok) | |
1350 { | |
1351 tok_value = tok.value; | |
1352 if (tok_value == TOK.TOKidentifier) | |
1353 { | |
1354 string id = tok.ident.toChars(); | |
1355 size_t len = id.length; | |
1356 if (len < 20) | |
1357 { | |
1358 ASMTK asmtk = cast(ASMTK) binary(toStringz(id), apszAsmtk.ptr, ASMTK.ASMTKmax); | |
1359 if (cast(int)asmtk >= 0) | |
1360 tok_value = cast(TOK)(asmtk + TOK.TOKMAX + 1); | |
1361 } | |
1362 } | |
1363 } | |
1364 } | |
1365 | |
1366 void asm_token() | |
1367 { | |
1368 if (asmtok) | |
1369 asmtok = asmtok.next; | |
1370 | |
1371 asm_token_trans(asmtok); | |
1372 } | |
1373 | |
1374 | |
1375 /********************** | |
1376 * If c is a power of 2, return that power else -1. | |
1377 */ | |
1378 | |
1379 int ispow2(ulong c) | |
1380 { | |
1381 int i; | |
1382 | |
1383 if (c == 0 || (c & (c - 1))) | |
1384 i = -1; | |
1385 else | |
1386 for (i = 0; c >>= 1; i++) { | |
1387 ; | |
1388 } | |
1389 | |
1390 return i; | |
1391 } | |
1392 | |
1393 // Error numbers | |
1394 enum ASMERRMSGS | |
1395 { | |
1396 EM_bad_float_op, | |
1397 EM_bad_addr_mode, | |
1398 EM_align, | |
1399 EM_opcode_exp, | |
1400 EM_prefix, | |
1401 EM_eol, | |
1402 EM_bad_operand, | |
1403 EM_bad_integral_operand, | |
1404 EM_ident_exp, | |
1405 EM_not_struct, | |
1406 EM_nops_expected, | |
1407 EM_bad_op, | |
1408 EM_const_init, | |
1409 EM_undefined, | |
1410 EM_pointer, | |
1411 EM_colon, | |
1412 EM_rbra, | |
1413 EM_rpar, | |
1414 EM_ptr_exp, | |
1415 EM_num, | |
1416 EM_float, | |
1417 EM_char, | |
1418 EM_label_expected, | |
1419 EM_uplevel, | |
1420 EM_type_as_operand, | |
1421 } | |
1422 | |
1423 mixin(BringToCurrentScope!(ASMERRMSGS)); | |
1424 | |
1425 string[] asmerrmsgs = | |
1426 [ | |
1427 "unknown operand for floating point instruction", | |
1428 "bad addr mode", | |
1429 "align %d must be a power of 2", | |
1430 "opcode expected, not %s", | |
1431 "prefix", | |
1432 "end of instruction", | |
1433 "bad operand", | |
1434 "bad integral operand", | |
1435 "identifier expected", | |
1436 "not struct", | |
1437 "nops expected", | |
1438 "bad type/size of operands '%s'", | |
1439 "constant initializer expected", | |
1440 "undefined identifier '%s'", | |
1441 "pointer", | |
1442 "colon", | |
1443 "] expected instead of '%s'", | |
1444 ") expected instead of '%s'", | |
1445 "ptr expected", | |
1446 "integer expected", | |
1447 "floating point expected", | |
1448 "character is truncated", | |
1449 "label expected", | |
1450 "uplevel nested reference to variable %s", | |
1451 "cannot use type %s as an operand" | |
1452 ]; | |
1453 | |
1454 void asmerr(T...)(int errnum, T t) | |
1455 { | |
1456 string format = asmerrmsgs[errnum]; | |
1457 asmerr(format, t); | |
1458 } | |
1459 | |
1460 void asmerr(T...)(string format, T t) | |
1461 { | |
1462 string p = asmstate.loc.toChars(); | |
1463 if (p.length != 0) | |
1464 writef("%s: ", p); | |
1465 | |
1466 writefln(format, t); | |
1467 | |
1468 longjmp(asmstate.env,1); | |
1469 } | |
1470 | |
1471 PTRNTAB asm_classify(OP* pop, OPND* popnd1, OPND* popnd2, OPND* popnd3, uint* pusNumops) | |
1472 { | |
1473 uint usNumops; | |
1474 uint usActual; | |
1475 PTRNTAB ptbRet = { null }; | |
1476 opflag_t usFlags1 = 0 ; | |
1477 opflag_t usFlags2 = 0; | |
1478 opflag_t usFlags3 = 0; | |
1479 | |
1480 PTRNTAB1 *pptb1; | |
1481 PTRNTAB2 *pptb2; | |
1482 PTRNTAB3 *pptb3; | |
1483 | |
1484 char bFake = false; | |
1485 | |
1486 ubyte bMatch1, bMatch2, bMatch3, bRetry = false; | |
1487 | |
1488 // How many arguments are there? the parser is strictly left to right | |
1489 // so this should work. | |
1490 | |
1491 if (!popnd1) | |
1492 usNumops = 0; | |
1493 else | |
1494 { | |
1495 popnd1.usFlags = usFlags1 = asm_determine_operand_flags(popnd1); | |
1496 if (!popnd2) | |
1497 usNumops = 1; | |
1498 else | |
1499 { | |
1500 popnd2.usFlags = usFlags2 = asm_determine_operand_flags(popnd2); | |
1501 if (!popnd3) | |
1502 usNumops = 2; | |
1503 else | |
1504 { | |
1505 popnd3.usFlags = usFlags3 = asm_determine_operand_flags(popnd3); | |
1506 usNumops = 3; | |
1507 } | |
1508 } | |
1509 } | |
1510 | |
1511 // Now check to insure that the number of operands is correct | |
1512 usActual = (pop.usNumops & IT.ITSIZE); | |
1513 if (usActual != usNumops && asmstate.ucItype != IT.ITopt && | |
1514 asmstate.ucItype != IT.ITfloat) | |
1515 { | |
1516 PARAM_ERROR: | |
1517 asmerr(ASMERRMSGS.EM_nops_expected, usActual, asm_opstr(pop), usNumops); | |
1518 } | |
1519 *pusNumops = usNumops; | |
1520 // | |
1521 // The number of arguments matches, now check to find the opcode | |
1522 // in the associated opcode table | |
1523 // | |
1524 RETRY: | |
1525 //printf("usActual = %d\n", usActual); | |
1526 switch (usActual) | |
1527 { | |
1528 case 0: | |
1529 ptbRet = pop.ptb ; | |
1530 goto RETURN_IT; | |
1531 | |
1532 case 1: | |
1533 //printf("usFlags1 = "); asm_output_flags(usFlags1); printf("\n"); | |
1534 for (pptb1 = pop.ptb.pptb1; pptb1.usOpcode != ASM_END; | |
1535 pptb1++) | |
1536 { | |
1537 //printf("table = "); asm_output_flags(pptb1.usOp1); printf("\n"); | |
1538 bMatch1 = asm_match_flags(usFlags1, pptb1.usOp1); | |
1539 if (bMatch1) | |
1540 { if (pptb1.usOpcode == 0x68 && | |
1541 I32 && | |
1542 pptb1.usOp1 == _imm16 | |
1543 ) | |
1544 // Don't match PUSH imm16 in 32 bit code | |
1545 continue; | |
1546 break; | |
1547 } | |
1548 if ((asmstate.ucItype == IT.ITimmed) && | |
1549 asm_match_flags(usFlags1, | |
1550 CONSTRUCT_FLAGS(_8 | _16 | _32, ASM_OPERAND_TYPE._imm, ASM_MODIFIERS._normal, | |
1551 0)) && | |
1552 popnd1.disp == pptb1.usFlags) | |
1553 break; | |
1554 if ((asmstate.ucItype == IT.ITopt || | |
1555 asmstate.ucItype == IT.ITfloat) && | |
1556 !usNumops && | |
1557 !pptb1.usOp1) | |
1558 { | |
1559 if (usNumops > 1) | |
1560 goto PARAM_ERROR; | |
1561 break; | |
1562 } | |
1563 } | |
1564 if (pptb1.usOpcode == ASM_END) | |
1565 { | |
1566 debug { | |
1567 if (debuga) | |
1568 { | |
1569 printf("\t%s\t", asm_opstr(pop)); | |
1570 if (popnd1) | |
1571 asm_output_popnd(popnd1); | |
1572 if (popnd2) { | |
1573 printf(","); | |
1574 asm_output_popnd(popnd2); | |
1575 } | |
1576 if (popnd3) { | |
1577 printf(","); | |
1578 asm_output_popnd(popnd3); | |
1579 } | |
1580 printf("\n"); | |
1581 | |
1582 printf("OPCODE mism = "); | |
1583 if (popnd1) | |
1584 asm_output_flags(popnd1.usFlags); | |
1585 else | |
1586 printf("NONE"); | |
1587 printf("\n"); | |
1588 } | |
1589 } | |
1590 TYPE_SIZE_ERROR: | |
1591 if (popnd1 && ASM_GET_aopty(popnd1.usFlags) != ASM_OPERAND_TYPE._reg) | |
1592 { | |
1593 usFlags1 = popnd1.usFlags |= _anysize; | |
1594 if (asmstate.ucItype == IT.ITjump) | |
1595 { | |
1596 if (bRetry && popnd1.s && !popnd1.s.isLabel()) | |
1597 { | |
1598 asmerr(ASMERRMSGS.EM_label_expected, popnd1.s.toChars()); | |
1599 } | |
1600 | |
1601 popnd1.usFlags |= CONSTRUCT_FLAGS(0, 0, 0, _fanysize); | |
1602 } | |
1603 } | |
1604 if (popnd2 && ASM_GET_aopty(popnd2.usFlags) != ASM_OPERAND_TYPE._reg) { | |
1605 usFlags2 = popnd2.usFlags |= (_anysize); | |
1606 if (asmstate.ucItype == IT.ITjump) | |
1607 popnd2.usFlags |= CONSTRUCT_FLAGS(0, 0, 0, | |
1608 _fanysize); | |
1609 } | |
1610 if (popnd3 && ASM_GET_aopty(popnd3.usFlags) != ASM_OPERAND_TYPE._reg) { | |
1611 usFlags3 = popnd3.usFlags |= (_anysize); | |
1612 if (asmstate.ucItype == IT.ITjump) | |
1613 popnd3.usFlags |= CONSTRUCT_FLAGS(0, 0, 0, | |
1614 _fanysize); | |
1615 } | |
1616 if (bRetry) | |
1617 { | |
1618 asmerr(ASMERRMSGS.EM_bad_op, fromStringz(asm_opstr(pop))); // illegal type/size of operands | |
1619 } | |
1620 bRetry = true; | |
1621 goto RETRY; | |
1622 } | |
1623 ptbRet.pptb1 = pptb1; | |
1624 goto RETURN_IT; | |
1625 | |
1626 case 2: | |
1627 //printf("usFlags1 = "); asm_output_flags(usFlags1); printf(" "); | |
1628 //printf("usFlags2 = "); asm_output_flags(usFlags2); printf("\n"); | |
1629 for (pptb2 = pop.ptb.pptb2; | |
1630 pptb2.usOpcode != ASM_END; | |
1631 pptb2++) | |
1632 { | |
1633 //printf("table1 = "); asm_output_flags(pptb2.usOp1); printf(" "); | |
1634 //printf("table2 = "); asm_output_flags(pptb2.usOp2); printf("\n"); | |
1635 bMatch1 = asm_match_flags(usFlags1, pptb2.usOp1); | |
1636 bMatch2 = asm_match_flags(usFlags2, pptb2.usOp2); | |
1637 //printf("match1 = %d, match2 = %d\n",bMatch1,bMatch2); | |
1638 if (bMatch1 && bMatch2) { | |
1639 | |
1640 //printf("match\n"); | |
1641 | |
1642 // OK, if they both match and the first op in the table is not AL | |
1643 // or size of 8 and the second is immediate 8, | |
1644 // then check to see if the constant | |
1645 // is a signed 8 bit constant. If so, then do not match, otherwise match | |
1646 // | |
1647 if (!bRetry && | |
1648 !((ASM_GET_uSizemask(pptb2.usOp1) & _8) || | |
1649 (ASM_GET_uRegmask(pptb2.usOp1) & _al)) && | |
1650 (ASM_GET_aopty(pptb2.usOp2) == ASM_OPERAND_TYPE._imm) && | |
1651 (ASM_GET_uSizemask(pptb2.usOp2) & _8)) | |
1652 { | |
1653 if (popnd2.disp <= char.max) | |
1654 break; | |
1655 else | |
1656 bFake = true; | |
1657 } | |
1658 else | |
1659 break; | |
1660 } | |
1661 if (asmstate.ucItype == IT.ITopt || asmstate.ucItype == IT.ITfloat) | |
1662 { | |
1663 switch (usNumops) | |
1664 { | |
1665 case 0: | |
1666 if (!pptb2.usOp1) | |
1667 goto Lfound2; | |
1668 break; | |
1669 case 1: | |
1670 if (bMatch1 && !pptb2.usOp2) | |
1671 goto Lfound2; | |
1672 break; | |
1673 case 2: | |
1674 break; | |
1675 default: | |
1676 goto PARAM_ERROR; | |
1677 } | |
1678 } | |
1679 static if (false) { | |
1680 if (asmstate.ucItype == IT.ITshift && | |
1681 !pptb2.usOp2 && | |
1682 bMatch1 && popnd2.disp == 1 && | |
1683 asm_match_flags(usFlags2, | |
1684 CONSTRUCT_FLAGS(_8|_16|_32, ASM_OPERAND_TYPE._imm,ASM_MODIFIERS._normal,0)) | |
1685 ) | |
1686 { | |
1687 break; | |
1688 } | |
1689 } | |
1690 } | |
1691 Lfound2: | |
1692 if (pptb2.usOpcode == ASM_END) | |
1693 { | |
1694 debug { | |
1695 if (debuga) | |
1696 { | |
1697 printf("\t%s\t", asm_opstr(pop)); | |
1698 if (popnd1) | |
1699 asm_output_popnd(popnd1); | |
1700 if (popnd2) { | |
1701 printf(","); | |
1702 asm_output_popnd(popnd2); | |
1703 } | |
1704 if (popnd3) { | |
1705 printf(","); | |
1706 asm_output_popnd(popnd3); | |
1707 } | |
1708 printf("\n"); | |
1709 | |
1710 printf("OPCODE mismatch = "); | |
1711 if (popnd1) | |
1712 asm_output_flags(popnd1.usFlags); | |
1713 else | |
1714 printf("NONE"); | |
1715 printf( " Op2 = "); | |
1716 if (popnd2) | |
1717 asm_output_flags(popnd2.usFlags); | |
1718 else | |
1719 printf("NONE"); | |
1720 printf("\n"); | |
1721 } | |
1722 } | |
1723 goto TYPE_SIZE_ERROR; | |
1724 } | |
1725 ptbRet.pptb2 = pptb2; | |
1726 goto RETURN_IT; | |
1727 | |
1728 case 3: | |
1729 for (pptb3 = pop.ptb.pptb3; | |
1730 pptb3.usOpcode != ASM_END; | |
1731 pptb3++) | |
1732 { | |
1733 bMatch1 = asm_match_flags(usFlags1, pptb3.usOp1); | |
1734 bMatch2 = asm_match_flags(usFlags2, pptb3.usOp2); | |
1735 bMatch3 = asm_match_flags(usFlags3, pptb3.usOp3); | |
1736 if (bMatch1 && bMatch2 && bMatch3) | |
1737 goto Lfound3; | |
1738 | |
1739 if (asmstate.ucItype == IT.ITopt) | |
1740 { | |
1741 switch (usNumops) | |
1742 { | |
1743 case 0: | |
1744 if (!pptb3.usOp1) | |
1745 goto Lfound3; | |
1746 break; | |
1747 case 1: | |
1748 if (bMatch1 && !pptb3.usOp2) | |
1749 goto Lfound3; | |
1750 break; | |
1751 case 2: | |
1752 if (bMatch1 && bMatch2 && !pptb3.usOp3) | |
1753 goto Lfound3; | |
1754 break; | |
1755 case 3: | |
1756 break; | |
1757 default: | |
1758 goto PARAM_ERROR; | |
1759 } | |
1760 } | |
1761 } | |
1762 Lfound3: | |
1763 if (pptb3.usOpcode == ASM_END) | |
1764 { | |
1765 debug { | |
1766 if (debuga) | |
1767 { | |
1768 printf("\t%s\t", asm_opstr(pop)); | |
1769 if (popnd1) | |
1770 asm_output_popnd(popnd1); | |
1771 if (popnd2) { | |
1772 printf(","); | |
1773 asm_output_popnd(popnd2); | |
1774 } | |
1775 if (popnd3) { | |
1776 printf(","); | |
1777 asm_output_popnd(popnd3); | |
1778 } | |
1779 printf("\n"); | |
1780 | |
1781 printf("OPCODE mismatch = "); | |
1782 if (popnd1) | |
1783 asm_output_flags(popnd1.usFlags); | |
1784 else | |
1785 printf("NONE"); | |
1786 printf( " Op2 = "); | |
1787 if (popnd2) | |
1788 asm_output_flags(popnd2.usFlags); | |
1789 else | |
1790 printf("NONE"); | |
1791 if (popnd3) | |
1792 asm_output_flags(popnd3.usFlags); | |
1793 printf("\n"); | |
1794 } | |
1795 } | |
1796 goto TYPE_SIZE_ERROR; | |
1797 } | |
1798 | |
1799 ptbRet.pptb3 = pptb3; | |
1800 goto RETURN_IT; | |
1801 } | |
1802 | |
1803 RETURN_IT: | |
1804 if (bRetry && !bFake) | |
1805 { | |
1806 asmerr(ASMERRMSGS.EM_bad_op, fromStringz(asm_opstr(pop))); | |
1807 } | |
1808 return ptbRet; | |
1809 } | |
1810 | |
1811 /******************************* | |
1812 * start of inline assemblers expression parser | |
1813 * NOTE: functions in call order instead of alphabetical | |
1814 */ | |
1815 | |
1816 /******************************************* | |
1817 * Parse DA expression | |
1818 * | |
1819 * Very limited define address to place a code | |
1820 * address in the assembly | |
1821 * Problems: | |
1822 * o Should use dw offset and dd offset instead, | |
1823 * for near/far support. | |
1824 * o Should be able to add an offset to the label address. | |
1825 * o Blocks addressed by DA should get their Bpred set correctly | |
1826 * for optimizer. | |
1827 */ | |
1828 | |
1829 code* asm_da_parse(OP* pop) | |
1830 { | |
1831 code* clst = null; | |
1832 elem* e; | |
1833 | |
1834 while (1) | |
1835 { | |
1836 code* c; | |
1837 | |
1838 if (tok_value == TOK.TOKidentifier) | |
1839 { | |
1840 LabelDsymbol label = asmstate.sc.func.searchLabel(asmtok.ident); | |
1841 if (!label) | |
1842 error(asmstate.loc, "label '%s' not found\n", asmtok.ident.toChars()); | |
1843 | |
1844 c = code_calloc(); | |
1845 c.Iop = ASM; | |
1846 c.Iflags = CF.CFaddrsize; | |
1847 c.IFL1 = FL.FLblockoff; | |
1848 c.IEVlsym1() = label; | |
1849 c = asm_genloc(asmstate.loc, c); | |
1850 clst = cat(clst,c); | |
1851 } | |
1852 else | |
1853 asmerr(ASMERRMSGS.EM_bad_addr_mode); // illegal addressing mode | |
1854 | |
1855 asm_token(); | |
1856 if (tok_value != TOK.TOKcomma) | |
1857 break; | |
1858 | |
1859 asm_token(); | |
1860 } | |
1861 | |
1862 asmstate.statement.regs |= mES | ALLREGS; | |
1863 asmstate.bReturnax = true; | |
1864 | |
1865 return clst; | |
1866 } | |
1867 | |
1868 /******************************************* | |
1869 * Parse DB, DW, DD, DQ and DT expressions. | |
1870 */ | |
1871 | |
1872 code* asm_db_parse(OP* pop) | |
1873 { | |
1874 uint usSize; | |
1875 uint usMaxbytes; | |
1876 uint usBytes; | |
1877 | |
1878 union DT | |
1879 { | |
1880 targ_ullong ul; | |
1881 targ_float f; | |
1882 targ_double d; | |
1883 targ_ldouble ld; | |
1884 char value[10]; | |
1885 } | |
1886 | |
1887 DT dt; | |
1888 | |
1889 code* c; | |
1890 uint op; | |
1891 static ubyte[7] opsize = [ 1,2,4,8,4,8,10 ]; | |
1892 | |
1893 op = pop.usNumops & IT.ITSIZE; | |
1894 usSize = opsize[op]; | |
1895 | |
1896 usBytes = 0; | |
1897 usMaxbytes = 0; | |
1898 c = code_calloc(); | |
1899 c.Iop = ASM; | |
1900 | |
1901 while (1) | |
1902 { | |
1903 size_t len; | |
1904 ubyte* q; | |
1905 | |
1906 if (usBytes+usSize > usMaxbytes) | |
1907 { | |
1908 usMaxbytes = usBytes + usSize + 10; | |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
1909 c.IEV1.as.bytes = cast(char*)realloc(c.IEV1.as.bytes,usMaxbytes); |
0 | 1910 } |
1911 switch (tok_value) | |
1912 { | |
1913 case TOK.TOKint32v: | |
1914 dt.ul = asmtok.int32value; | |
1915 goto L1; | |
1916 case TOK.TOKuns32v: | |
1917 dt.ul = asmtok.uns32value; | |
1918 goto L1; | |
1919 case TOK.TOKint64v: | |
1920 dt.ul = asmtok.int64value; | |
1921 goto L1; | |
1922 case TOK.TOKuns64v: | |
1923 dt.ul = asmtok.uns64value; | |
1924 goto L1; | |
1925 L1: | |
1926 switch (op) | |
1927 { | |
1928 case OP_DB.OPdb: | |
1929 case OP_DB.OPds: | |
1930 case OP_DB.OPdi: | |
1931 case OP_DB.OPdl: | |
1932 break; | |
1933 default: | |
1934 asmerr(ASMERRMSGS.EM_float); | |
1935 } | |
1936 goto L2; | |
1937 | |
1938 case TOK.TOKfloat32v: | |
1939 case TOK.TOKfloat64v: | |
1940 case TOK.TOKfloat80v: | |
1941 switch (op) | |
1942 { | |
1943 case OP_DB.OPdf: | |
1944 dt.f = asmtok.float80value; | |
1945 break; | |
1946 case OP_DB.OPdd: | |
1947 dt.d = asmtok.float80value; | |
1948 break; | |
1949 case OP_DB.OPde: | |
1950 dt.ld = asmtok.float80value; | |
1951 break; | |
1952 default: | |
1953 asmerr(ASMERRMSGS.EM_num); | |
1954 } | |
1955 goto L2; | |
1956 | |
1957 L2: | |
1958 memcpy(c.IEV1.as.bytes + usBytes,&dt,usSize); | |
1959 usBytes += usSize; | |
1960 break; | |
1961 | |
1962 case TOK.TOKstring: | |
1963 len = asmtok.len; | |
1964 q = cast(ubyte*)asmtok.ustring; | |
1965 L3: | |
1966 if (len) | |
1967 { | |
1968 usMaxbytes += len * usSize; | |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
1969 c.IEV1.as.bytes = cast(char*)realloc(c.IEV1.as.bytes,usMaxbytes); |
0 | 1970 memcpy(c.IEV1.as.bytes + usBytes,asmtok.ustring,len); |
1971 | |
1972 char* p = c.IEV1.as.bytes + usBytes; | |
1973 for (size_t i = 0; i < len; i++) | |
1974 { | |
1975 // Be careful that this works | |
1976 memset(p, 0, usSize); | |
1977 switch (op) | |
1978 { | |
1979 case OP_DB.OPdb: | |
1980 *p = cast(ubyte)*q; | |
1981 if (*p != *q) | |
1982 asmerr(ASMERRMSGS.EM_char); | |
1983 break; | |
1984 | |
1985 case OP_DB.OPds: | |
1986 *cast(short*)p = *cast(ubyte*)q; | |
1987 if (*cast(short*)p != *q) | |
1988 asmerr(ASMERRMSGS.EM_char); | |
1989 break; | |
1990 | |
1991 case OP_DB.OPdi: | |
1992 case OP_DB.OPdl: | |
1993 *cast(int*)p = *q; | |
1994 break; | |
1995 | |
1996 default: | |
1997 asmerr(ASMERRMSGS.EM_float); | |
1998 } | |
1999 q++; | |
2000 p += usSize; | |
2001 } | |
2002 | |
2003 usBytes += len * usSize; | |
2004 } | |
2005 break; | |
2006 | |
2007 case TOK.TOKidentifier: | |
2008 { | |
2009 Expression e = new IdentifierExp(asmstate.loc, asmtok.ident); | |
2010 e = e.semantic(asmstate.sc); | |
2011 e = e.optimize(WANT.WANTvalue | WANT.WANTinterpret); | |
2012 if (e.op == TOK.TOKint64) | |
2013 { | |
2014 dt.ul = e.toInteger(); | |
2015 goto L2; | |
2016 } | |
2017 else if (e.op == TOK.TOKfloat64) | |
2018 { | |
2019 switch (op) | |
2020 { | |
2021 case OP_DB.OPdf: | |
2022 dt.f = e.toReal(); | |
2023 break; | |
2024 case OP_DB.OPdd: | |
2025 dt.d = e.toReal(); | |
2026 break; | |
2027 case OP_DB.OPde: | |
2028 dt.ld = e.toReal(); | |
2029 break; | |
2030 default: | |
2031 asmerr(ASMERRMSGS.EM_num); | |
2032 } | |
2033 goto L2; | |
2034 } | |
2035 else if (e.op == TOK.TOKstring) | |
2036 { | |
2037 StringExp se = cast(StringExp)e; | |
2038 q = cast(ubyte*)se.string_; | |
2039 len = se.len; | |
2040 goto L3; | |
2041 } | |
2042 goto Ldefault; | |
2043 } | |
2044 | |
2045 default: | |
2046 Ldefault: | |
2047 asmerr(ASMERRMSGS.EM_const_init); // constant initializer | |
2048 break; | |
2049 } | |
2050 c.IEV1.as.len = usBytes; | |
2051 | |
2052 asm_token(); | |
2053 if (tok_value != TOK.TOKcomma) | |
2054 break; | |
2055 | |
2056 asm_token(); | |
2057 } | |
2058 | |
2059 c = asm_genloc(asmstate.loc, c); | |
2060 | |
2061 asmstate.statement.regs |= /* mES| */ ALLREGS; | |
2062 asmstate.bReturnax = true; | |
2063 | |
2064 return c; | |
2065 } | |
2066 | |
2067 /********************************** | |
2068 * Parse and get integer expression. | |
2069 */ | |
2070 | |
2071 int asm_getnum() | |
2072 { | |
2073 int v; | |
2074 long i; | |
2075 | |
2076 switch (tok_value) | |
2077 { | |
2078 case TOK.TOKint32v: | |
2079 v = asmtok.int32value; | |
2080 break; | |
2081 | |
2082 case TOK.TOKuns32v: | |
2083 v = asmtok.uns32value; | |
2084 break; | |
2085 | |
2086 case TOK.TOKidentifier: | |
2087 Expression e = new IdentifierExp(asmstate.loc, asmtok.ident); | |
2088 e = e.semantic(asmstate.sc); | |
2089 e = e.optimize(WANT.WANTvalue | WANT.WANTinterpret); | |
2090 i = e.toInteger(); | |
2091 v = cast(int) i; | |
2092 if (v != i) | |
2093 asmerr(ASMERRMSGS.EM_num); | |
2094 break; | |
2095 | |
2096 default: | |
2097 asmerr(ASMERRMSGS.EM_num); | |
2098 break; | |
2099 } | |
2100 | |
2101 asm_token(); | |
2102 | |
2103 return v; | |
2104 } | |
2105 | |
2106 | |
2107 /******************************* | |
2108 */ | |
2109 | |
2110 OPND* asm_cond_exp() | |
2111 { | |
2112 OPND* o1; | |
2113 OPND* o2; | |
2114 OPND* o3; | |
2115 | |
2116 //printf("asm_cond_exp()\n"); | |
2117 o1 = asm_log_or_exp(); | |
2118 if (tok_value == TOK.TOKquestion) | |
2119 { | |
2120 asm_token(); | |
2121 o2 = asm_cond_exp(); | |
2122 asm_token(); | |
2123 asm_chktok(TOK.TOKcolon, ASMERRMSGS.EM_colon); | |
2124 o3 = asm_cond_exp(); | |
2125 o1 = (o1.disp) ? o2 : o3; | |
2126 } | |
2127 | |
2128 return o1; | |
2129 } | |
2130 | |
2131 regm_t asm_modify_regs(PTRNTAB ptb, OPND* popnd1, OPND* popnd2) | |
2132 { | |
2133 regm_t usRet = 0; | |
2134 | |
2135 switch (ptb.pptb0.usFlags & MOD_MASK) { | |
2136 case _modsi: | |
2137 usRet |= mSI; | |
2138 break; | |
2139 case _moddx: | |
2140 usRet |= mDX; | |
2141 break; | |
2142 case _mod2: | |
2143 if (popnd2) | |
2144 usRet |= asm_modify_regs(ptb, popnd2, null); | |
2145 break; | |
2146 case _modax: | |
2147 usRet |= mAX; | |
2148 break; | |
2149 case _modnot1: | |
2150 popnd1 = null; | |
2151 break; | |
2152 case _modaxdx: | |
2153 usRet |= (mAX | mDX); | |
2154 break; | |
2155 case _moddi: | |
2156 usRet |= mDI; | |
2157 break; | |
2158 case _modsidi: | |
2159 usRet |= (mSI | mDI); | |
2160 break; | |
2161 case _modcx: | |
2162 usRet |= mCX; | |
2163 break; | |
2164 case _modes: | |
2165 /*usRet |= mES;*/ | |
2166 break; | |
2167 case _modall: | |
2168 asmstate.bReturnax = true; | |
2169 return /*mES |*/ ALLREGS; | |
2170 case _modsiax: | |
2171 usRet |= (mSI | mAX); | |
2172 break; | |
2173 case _modsinot1: | |
2174 usRet |= mSI; | |
2175 popnd1 = null; | |
2176 break; | |
2177 default: | |
2178 break; /// | |
2179 } | |
2180 if (popnd1 && ASM_GET_aopty(popnd1.usFlags) == ASM_OPERAND_TYPE._reg) { | |
2181 switch (ASM_GET_amod(popnd1.usFlags)) { | |
2182 default: | |
2183 if (ASM_GET_uSizemask(popnd1.usFlags) == _8) { | |
2184 switch(popnd1.base.val) { | |
2185 case _AL: | |
2186 case _AH: | |
2187 usRet |= mAX; | |
2188 break; | |
2189 case _BL: | |
2190 case _BH: | |
2191 usRet |= mBX; | |
2192 break; | |
2193 case _CL: | |
2194 case _CH: | |
2195 usRet |= mCX; | |
2196 break; | |
2197 case _DL: | |
2198 case _DH: | |
2199 usRet |= mDX; | |
2200 break; | |
2201 } | |
2202 } | |
2203 else { | |
2204 switch (popnd1.base.val) { | |
2205 case _AX: | |
2206 usRet |= mAX; | |
2207 break; | |
2208 case _BX: | |
2209 usRet |= mBX; | |
2210 break; | |
2211 case _CX: | |
2212 usRet |= mCX; | |
2213 break; | |
2214 case _DX: | |
2215 usRet |= mDX; | |
2216 break; | |
2217 case _SI: | |
2218 usRet |= mSI; | |
2219 break; | |
2220 case _DI: | |
2221 usRet |= mDI; | |
2222 break; | |
2223 default: | |
2224 break; /// | |
2225 } | |
2226 } | |
2227 break; | |
2228 | |
2229 case ASM_MODIFIERS._rseg: | |
2230 //if (popnd1.base.val == _ES) | |
2231 //usRet |= mES; | |
2232 break; | |
2233 | |
2234 case ASM_MODIFIERS._rspecial: | |
2235 break; | |
2236 } | |
2237 } | |
2238 if (usRet & mAX) | |
2239 asmstate.bReturnax = true; | |
2240 | |
2241 return usRet; | |
2242 } | |
2243 | |
2244 | |
2245 /**************************** | |
2246 * Fill in the modregrm and sib bytes of code. | |
2247 */ | |
2248 | |
2249 uint X(ubyte r1, ubyte r2) { | |
2250 return (((r1) * 16) + (r2)); | |
2251 } | |
2252 | |
2253 uint Y(ubyte r1) { | |
2254 return X(r1, 9); | |
2255 } | |
2256 | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
2257 // Save a copy/pasted function |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
2258 template Tuple(T...) { alias T Tuple; } |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
2259 debug alias Tuple!(ubyte[], uint*) asm_make_modrm_args; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
2260 else alias Tuple!() asm_make_modrm_args; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
2261 |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
2262 void asm_make_modrm_byte(asm_make_modrm_args ocidx, code *pc, ushort usFlags, OPND *popnd, OPND *popnd2) |
0 | 2263 { |
2264 /// #undef modregrm | |
20
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
2265 debug alias ocidx[0] puchOpcode; |
1628b221808d
Fleshed out more unimplemented methods.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
2266 debug alias ocidx[1] pusIdx; |
0 | 2267 union MODRM_BYTE // mrmb |
2268 { | |
2269 struct MODRM | |
2270 { | |
2271 mixin(bitfields!( | |
2272 uint, "rm", 3, | |
2273 uint, "reg", 3, | |
2274 uint, "mod", 2)); | |
2275 } | |
2276 | |
2277 ubyte uchOpcode; | |
2278 MODRM modregrm; | |
2279 } | |
2280 | |
2281 union SIB_BYTE | |
2282 { | |
2283 struct SIB | |
2284 { | |
2285 | |
2286 mixin(bitfields!( | |
2287 uint, "base", 3, | |
2288 uint, "index", 3, | |
2289 uint, "ss", 2)); | |
2290 } | |
2291 | |
2292 ubyte uchOpcode; | |
2293 SIB sib; | |
2294 } | |
2295 | |
2296 | |
2297 MODRM_BYTE mrmb; | |
2298 SIB_BYTE sib; | |
2299 char bSib = false; | |
2300 char bDisp = false; | |
2301 char b32bit = false; | |
2302 ubyte* puc; | |
2303 char bModset = false; | |
2304 Dsymbol s; | |
2305 | |
2306 uint uSizemask = 0; | |
2307 ASM_OPERAND_TYPE aopty; | |
2308 ASM_MODIFIERS amod; | |
2309 ushort uRegmask; | |
2310 ubyte bOffsetsym = false; | |
2311 | |
2312 static if (false) { | |
2313 printf("asm_make_modrm_byte(usFlags = x%x)\n", usFlags); | |
2314 printf("op1: "); | |
2315 asm_output_flags(popnd.usFlags); | |
2316 if (popnd2) | |
2317 { | |
2318 printf(" op2: "); | |
2319 asm_output_flags(popnd2.usFlags); | |
2320 } | |
2321 printf("\n"); | |
2322 } | |
2323 | |
2324 uSizemask = ASM_GET_uSizemask(popnd.usFlags); | |
2325 aopty = ASM_GET_aopty(popnd.usFlags); | |
2326 amod = ASM_GET_amod(popnd.usFlags); | |
2327 uRegmask = ASM_GET_uRegmask(popnd.usFlags); | |
2328 s = popnd.s; | |
2329 if (s) | |
2330 { | |
2331 Declaration d = s.isDeclaration(); | |
2332 | |
2333 if (amod == ASM_MODIFIERS._fn16 && aopty == ASM_OPERAND_TYPE._rel && popnd2) | |
2334 { | |
2335 aopty = ASM_OPERAND_TYPE._m; | |
2336 goto L1; | |
2337 } | |
2338 | |
2339 if (amod == ASM_MODIFIERS._fn16 || amod == ASM_MODIFIERS._fn32) | |
2340 { | |
2341 pc.Iflags |= CF.CFoff; | |
2342 debug { | |
2343 puchOpcode[(*pusIdx)++] = 0; | |
2344 puchOpcode[(*pusIdx)++] = 0; | |
2345 } | |
2346 if (aopty == ASM_OPERAND_TYPE._m || aopty == ASM_OPERAND_TYPE._mnoi) | |
2347 { | |
2348 pc.IFL1 = FL.FLdata; | |
2349 pc.IEVdsym1() = d; | |
2350 pc.IEVoffset1() = 0; | |
2351 } | |
2352 else | |
2353 { | |
2354 if (aopty == ASM_OPERAND_TYPE._p) | |
2355 pc.Iflags |= CF.CFseg; | |
2356 | |
2357 debug if (aopty == ASM_OPERAND_TYPE._p || aopty == ASM_OPERAND_TYPE._rel) | |
2358 { | |
2359 puchOpcode[(*pusIdx)++] = 0; | |
2360 puchOpcode[(*pusIdx)++] = 0; | |
2361 } | |
2362 | |
2363 pc.IFL2 = FL.FLfunc; | |
2364 pc.IEVdsym2() = d; | |
2365 pc.IEVoffset2() = 0; | |
2366 //return; | |
2367 } | |
2368 } | |
2369 else | |
2370 { | |
2371 L1: | |
2372 LabelDsymbol label = s.isLabel(); | |
2373 if (label) | |
2374 { | |
2375 if (s == asmstate.psDollar) | |
2376 { | |
2377 pc.IFL1 = FL.FLconst; | |
2378 if (uSizemask & (_8 | _16)) | |
2379 pc.IEVint1() = popnd.disp; | |
2380 else if (uSizemask & _32) | |
2381 pc.IEVpointer1() = cast(targ_size_t) popnd.disp; | |
2382 } | |
2383 else | |
2384 { | |
2385 pc.IFL1 = FL.FLblockoff; | |
2386 pc.IEVlsym1() = label; | |
2387 } | |
2388 } | |
2389 else if (s == asmstate.psLocalsize) | |
2390 { | |
2391 pc.IFL1 = FL.FLlocalsize; | |
2392 pc.IEVdsym1() = null; | |
2393 pc.Iflags |= CF.CFoff; | |
2394 pc.IEVoffset1() = popnd.disp; | |
2395 } | |
2396 else if (s.isFuncDeclaration()) | |
2397 { | |
2398 pc.IFL1 = FL.FLfunc; | |
2399 pc.IEVdsym1() = d; | |
2400 pc.IEVoffset1() = popnd.disp; | |
2401 } | |
2402 else | |
2403 { | |
2404 debug if (debuga) | |
2405 printf("Setting up symbol %s\n", d.ident.toChars()); | |
2406 | |
2407 pc.IFL1 = FL.FLdsymbol; | |
2408 pc.IEVdsym1() = d; | |
2409 pc.Iflags |= CF.CFoff; | |
2410 pc.IEVoffset1() = popnd.disp; | |
2411 } | |
2412 } | |
2413 } | |
2414 | |
2415 mrmb.modregrm.reg = usFlags & NUM_MASK; | |
2416 | |
2417 if (s && (aopty == ASM_OPERAND_TYPE._m || aopty == ASM_OPERAND_TYPE._mnoi) && !s.isLabel()) | |
2418 { | |
2419 if (s == asmstate.psLocalsize) | |
2420 { | |
2421 DATA_REF: | |
2422 mrmb.modregrm.rm = BPRM; | |
2423 if (amod == ASM_MODIFIERS._addr16 || amod == ASM_MODIFIERS._addr32) | |
2424 mrmb.modregrm.mod = 0x2; | |
2425 else | |
2426 mrmb.modregrm.mod = 0x0; | |
2427 } | |
2428 else | |
2429 { | |
2430 Declaration d = s.isDeclaration(); | |
2431 assert(d); | |
2432 if (d.isDataseg() || d.isCodeseg()) | |
2433 { | |
2434 if (( I32 && amod == ASM_MODIFIERS._addr16) || | |
2435 (!I32 && amod == ASM_MODIFIERS._addr32)) | |
2436 asmerr(ASMERRMSGS.EM_bad_addr_mode); // illegal addressing mode | |
2437 goto DATA_REF; | |
2438 } | |
2439 mrmb.modregrm.rm = BPRM; | |
2440 mrmb.modregrm.mod = 0x2; | |
2441 } | |
2442 } | |
2443 | |
2444 if (aopty == ASM_OPERAND_TYPE._reg || amod == ASM_MODIFIERS._rspecial) { | |
2445 mrmb.modregrm.mod = 0x3; | |
2446 mrmb.modregrm.rm = mrmb.modregrm.rm | popnd.base.val; | |
2447 } | |
2448 else if (amod == ASM_MODIFIERS._addr16 || (amod == ASM_MODIFIERS._flbl && !I32)) | |
2449 { | |
2450 uint rm; | |
2451 | |
2452 debug if (debuga) | |
2453 printf("This is an ADDR16\n"); | |
2454 | |
2455 if (!popnd.pregDisp1) | |
2456 { rm = 0x6; | |
2457 if (!s) | |
2458 bDisp = true; | |
2459 } | |
2460 else | |
2461 { | |
2462 uint r1r2; | |
2463 | |
2464 if (popnd.pregDisp2) | |
2465 r1r2 = X(popnd.pregDisp1.val,popnd.pregDisp2.val); | |
2466 else | |
2467 r1r2 = Y(popnd.pregDisp1.val); | |
2468 | |
2469 switch (r1r2) | |
2470 { | |
2471 case X(_BX,_SI): rm = 0; break; | |
2472 case X(_BX,_DI): rm = 1; break; | |
2473 case Y(_BX): rm = 7; break; | |
2474 | |
2475 case X(_BP,_SI): rm = 2; break; | |
2476 case X(_BP,_DI): rm = 3; break; | |
2477 case Y(_BP): rm = 6; bDisp = true; break; | |
2478 | |
2479 case X(_SI,_BX): rm = 0; break; | |
2480 case X(_SI,_BP): rm = 2; break; | |
2481 case Y(_SI): rm = 4; break; | |
2482 | |
2483 case X(_DI,_BX): rm = 1; break; | |
2484 case X(_DI,_BP): rm = 3; break; | |
2485 case Y(_DI): rm = 5; break; | |
2486 | |
2487 default: | |
2488 asmerr(ASMERRMSGS.EM_bad_addr_mode); // illegal addressing mode | |
2489 } | |
2490 } | |
2491 mrmb.modregrm.rm = rm; | |
2492 | |
2493 debug if (debuga) | |
2494 printf("This is an mod = %d, popnd.s =%ld, popnd.disp = %ld\n", | |
2495 mrmb.modregrm.mod, s, popnd.disp); | |
2496 | |
2497 if (!s || (!mrmb.modregrm.mod && popnd.disp)) | |
2498 { | |
2499 if ((!popnd.disp && !bDisp) || !popnd.pregDisp1) | |
2500 mrmb.modregrm.mod = 0x0; | |
2501 else if (popnd.disp >= CHAR_MIN && popnd.disp <= SCHAR_MAX) | |
2502 mrmb.modregrm.mod = 0x1; | |
2503 else | |
2504 mrmb.modregrm.mod = 0X2; | |
2505 } | |
2506 else | |
2507 bOffsetsym = true; | |
2508 | |
2509 } | |
2510 else if (amod == ASM_MODIFIERS._addr32 || (amod == ASM_MODIFIERS._flbl && I32)) | |
2511 { | |
2512 debug if (debuga) | |
2513 printf("This is an ADDR32\n"); | |
2514 | |
2515 if (!popnd.pregDisp1) | |
2516 mrmb.modregrm.rm = 0x5; | |
2517 else if (popnd.pregDisp2 || | |
2518 popnd.uchMultiplier || | |
2519 popnd.pregDisp1.val == _ESP) | |
2520 { | |
2521 if (popnd.pregDisp2) | |
2522 { | |
2523 if (popnd.pregDisp2.val == _ESP) | |
2524 asmerr(ASMERRMSGS.EM_bad_addr_mode); // illegal addressing mode | |
2525 } | |
2526 else | |
2527 { | |
2528 if (popnd.uchMultiplier && popnd.pregDisp1.val ==_ESP) | |
2529 asmerr(ASMERRMSGS.EM_bad_addr_mode); // illegal addressing mode | |
2530 bDisp = true; | |
2531 } | |
2532 | |
2533 mrmb.modregrm.rm = 0x4; | |
2534 bSib = true; | |
2535 if (bDisp) | |
2536 { | |
2537 if (!popnd.uchMultiplier && | |
2538 popnd.pregDisp1.val==_ESP) | |
2539 { | |
2540 sib.sib.base = popnd.pregDisp1.val; | |
2541 sib.sib.index = 0x4; | |
2542 } | |
2543 else | |
2544 { | |
2545 debug if (debuga) | |
2546 printf("Resetting the mod to 0\n"); | |
2547 | |
2548 if (popnd.pregDisp2) | |
2549 { | |
2550 if (popnd.pregDisp2.val != _EBP) | |
2551 asmerr(ASMERRMSGS.EM_bad_addr_mode); // illegal addressing mode | |
2552 } | |
2553 else | |
2554 { | |
2555 mrmb.modregrm.mod = 0x0; | |
2556 bModset = true; | |
2557 } | |
2558 | |
2559 sib.sib.base = 0x5; | |
2560 sib.sib.index = popnd.pregDisp1.val; | |
2561 } | |
2562 } | |
2563 else | |
2564 { | |
2565 sib.sib.base = popnd.pregDisp1.val; | |
2566 // | |
2567 // This is to handle the special case | |
2568 // of using the EBP register and no | |
2569 // displacement. You must put in an | |
2570 // 8 byte displacement in order to | |
2571 // get the correct opcodes. | |
2572 // | |
2573 if (popnd.pregDisp1.val == _EBP && (!popnd.disp && !s)) | |
2574 { | |
2575 debug if (debuga) | |
2576 printf("Setting the mod to 1 in the _EBP case\n"); | |
2577 | |
2578 mrmb.modregrm.mod = 0x1; | |
2579 bDisp = true; // Need a | |
2580 // displacement | |
2581 bModset = true; | |
2582 } | |
2583 | |
2584 sib.sib.index = popnd.pregDisp2.val; | |
2585 } | |
2586 switch (popnd.uchMultiplier) | |
2587 { | |
2588 case 0: sib.sib.ss = 0; break; | |
2589 case 1: sib.sib.ss = 0; break; | |
2590 case 2: sib.sib.ss = 1; break; | |
2591 case 4: sib.sib.ss = 2; break; | |
2592 case 8: sib.sib.ss = 3; break; | |
2593 | |
2594 default: | |
2595 asmerr(ASMERRMSGS.EM_bad_addr_mode); // illegal addressing mode | |
2596 break; | |
2597 } | |
2598 if (bDisp && sib.sib.base == 0x5) | |
2599 b32bit = true; | |
2600 } | |
2601 else | |
2602 { | |
2603 uint rm; | |
2604 | |
2605 if (popnd.uchMultiplier) | |
2606 asmerr(ASMERRMSGS.EM_bad_addr_mode); // illegal addressing mode | |
2607 switch (popnd.pregDisp1.val) | |
2608 { | |
2609 case _EAX: rm = 0; break; | |
2610 case _ECX: rm = 1; break; | |
2611 case _EDX: rm = 2; break; | |
2612 case _EBX: rm = 3; break; | |
2613 case _ESI: rm = 6; break; | |
2614 case _EDI: rm = 7; break; | |
2615 | |
2616 case _EBP: | |
2617 if (!popnd.disp && !s) | |
2618 { | |
2619 mrmb.modregrm.mod = 0x1; | |
2620 bDisp = true; // Need a displacement | |
2621 bModset = true; | |
2622 } | |
2623 rm = 5; | |
2624 break; | |
2625 | |
2626 default: | |
2627 asmerr(ASMERRMSGS.EM_bad_addr_mode); // illegal addressing mode | |
2628 break; | |
2629 } | |
2630 mrmb.modregrm.rm = rm; | |
2631 } | |
2632 if (!bModset && (!s || (!mrmb.modregrm.mod && popnd.disp))) | |
2633 { | |
2634 if ((!popnd.disp && !mrmb.modregrm.mod) || | |
2635 (!popnd.pregDisp1 && !popnd.pregDisp2)) | |
2636 { | |
2637 mrmb.modregrm.mod = 0x0; | |
2638 bDisp = true; | |
2639 } | |
2640 else if (popnd.disp >= CHAR_MIN && popnd.disp <= SCHAR_MAX) | |
2641 mrmb.modregrm.mod = 0x1; | |
2642 else | |
2643 mrmb.modregrm.mod = 0x2; | |
2644 } | |
2645 else | |
2646 bOffsetsym = true; | |
2647 } | |
2648 if (popnd2 && !mrmb.modregrm.reg && | |
2649 asmstate.ucItype != IT.ITshift && | |
2650 (ASM_GET_aopty(popnd2.usFlags) == ASM_OPERAND_TYPE._reg || | |
2651 ASM_GET_amod(popnd2.usFlags) == ASM_MODIFIERS._rseg || | |
2652 ASM_GET_amod(popnd2.usFlags) == ASM_MODIFIERS._rspecial)) | |
2653 { | |
2654 mrmb.modregrm.reg = popnd2.base.val; | |
2655 } | |
2656 debug puchOpcode[ (*pusIdx)++ ] = mrmb.uchOpcode; | |
2657 | |
2658 pc.Irm = mrmb.uchOpcode; | |
2659 //printf("Irm = %02x\n", pc.Irm); | |
2660 if (bSib) | |
2661 { | |
2662 debug puchOpcode[ (*pusIdx)++ ] = sib.uchOpcode; | |
2663 pc.Isib= sib.uchOpcode; | |
2664 } | |
2665 if ((!s || (popnd.pregDisp1 && !bOffsetsym)) && | |
2666 aopty != ASM_OPERAND_TYPE._imm && | |
2667 (popnd.disp || bDisp)) | |
2668 { | |
2669 if (popnd.usFlags & _a16) | |
2670 { | |
2671 debug { | |
2672 puc = (cast(ubyte*) &(popnd.disp)); | |
2673 puchOpcode[(*pusIdx)++] = puc[1]; | |
2674 puchOpcode[(*pusIdx)++] = puc[0]; | |
2675 } | |
2676 if (usFlags & (_modrm | NUM_MASK)) { | |
2677 debug if (debuga) | |
2678 printf("Setting up value %ld\n", popnd.disp); | |
2679 | |
2680 pc.IEVint1() = popnd.disp; | |
2681 pc.IFL1 = FL.FLconst; | |
2682 } | |
2683 else { | |
2684 pc.IEVint2() = popnd.disp; | |
2685 pc.IFL2 = FL.FLconst; | |
2686 } | |
2687 | |
2688 } | |
2689 else | |
2690 { | |
2691 debug { | |
2692 puc = (cast(ubyte*) &(popnd.disp)); | |
2693 puchOpcode[(*pusIdx)++] = puc[3]; | |
2694 puchOpcode[(*pusIdx)++] = puc[2]; | |
2695 puchOpcode[(*pusIdx)++] = puc[1]; | |
2696 puchOpcode[(*pusIdx)++] = puc[0]; | |
2697 } | |
2698 if (usFlags & (_modrm | NUM_MASK)) { | |
2699 debug if (debuga) | |
2700 printf("Setting up value %ld\n", popnd.disp); | |
2701 | |
2702 pc.IEVpointer1() = cast(targ_size_t) popnd.disp; | |
2703 pc.IFL1 = FL.FLconst; | |
2704 } else { | |
2705 pc.IEVpointer2() = cast(targ_size_t) popnd.disp; | |
2706 pc.IFL2 = FL.FLconst; | |
2707 } | |
2708 } | |
2709 } | |
2710 } | |
2711 | |
2712 void asm_output_popnd(OPND* popnd) | |
2713 { | |
2714 if (popnd.segreg) | |
2715 printf("%s:", popnd.segreg.regstr); | |
2716 | |
2717 if (popnd.s) | |
2718 writef("%s", popnd.s.ident.toChars()); | |
2719 | |
2720 if (popnd.base) | |
2721 printf("%s", popnd.base.regstr); | |
2722 | |
2723 if (popnd.pregDisp1) { | |
2724 if (popnd.pregDisp2) { | |
2725 if (popnd.usFlags & _a32) | |
2726 if (popnd.uchMultiplier) | |
2727 printf("[%s][%s*%d]", | |
2728 popnd.pregDisp1.regstr, | |
2729 popnd.pregDisp2.regstr, | |
2730 popnd.uchMultiplier); | |
2731 else | |
2732 printf("[%s][%s]", | |
2733 popnd.pregDisp1.regstr, | |
2734 popnd.pregDisp2.regstr); | |
2735 else | |
2736 printf("[%s+%s]", | |
2737 popnd.pregDisp1.regstr, | |
2738 popnd.pregDisp2.regstr); | |
2739 } | |
2740 else { | |
2741 if (popnd.uchMultiplier) | |
2742 printf("[%s*%d]", | |
2743 popnd.pregDisp1.regstr, | |
2744 popnd.uchMultiplier); | |
2745 else | |
2746 printf("[%s]", | |
2747 popnd.pregDisp1.regstr); | |
2748 } | |
2749 } | |
2750 if (ASM_GET_aopty(popnd.usFlags) == ASM_OPERAND_TYPE._imm) | |
2751 printf("%lxh", popnd.disp); | |
2752 else if (popnd.disp) | |
2753 printf("+%lxh", popnd.disp); | |
2754 } | |
2755 | |
2756 /******************************* | |
2757 * Prepend line number to c. | |
2758 */ | |
2759 | |
2760 code* asm_genloc(Loc loc, code* c) | |
2761 { | |
2762 if (global.params.symdebug) | |
2763 { | |
2764 code* pcLin; | |
2765 Srcpos srcpos; | |
2766 | |
2767 srcpos.Slinnum = loc.linnum; | |
2768 srcpos.Sfilename = cast(char*)toStringz(loc.filename); | |
2769 pcLin = genlinnum(null, srcpos); | |
2770 | |
2771 c = cat(pcLin, c); | |
2772 } | |
2773 | |
2774 return c; | |
2775 } | |
2776 | |
2777 opflag_t asm_determine_operand_flags(OPND* popnd) | |
2778 { | |
2779 Dsymbol ps; | |
2780 int ty; | |
2781 opflag_t us; | |
2782 opflag_t sz; | |
2783 ASM_OPERAND_TYPE opty; | |
2784 ASM_MODIFIERS amod; | |
2785 | |
2786 // If specified 'offset' or 'segment' but no symbol | |
2787 if ((popnd.bOffset || popnd.bSeg) && !popnd.s) | |
2788 asmerr(ASMERRMSGS.EM_bad_addr_mode); // illegal addressing mode | |
2789 | |
2790 if (asmstate.ucItype == IT.ITfloat) | |
2791 return asm_determine_float_flags(popnd); | |
2792 | |
2793 // If just a register | |
2794 if (popnd.base && !popnd.s && !popnd.disp && !popnd.real_) | |
2795 return popnd.base.ty; | |
2796 | |
2797 debug if (debuga) | |
2798 printf("popnd.base = %s\n, popnd.pregDisp1 = %ld\n", popnd.base ? popnd.base.regstr : "NONE", popnd.pregDisp1); | |
2799 | |
2800 ps = popnd.s; | |
2801 Declaration ds = ps ? ps.isDeclaration() : null; | |
2802 if (ds && ds.storage_class & STC.STClazy) | |
2803 sz = _anysize; | |
2804 else | |
2805 sz = cast(ushort)asm_type_size((ds && ds.storage_class & (STC.STCout | STC.STCref)) ? popnd.ptype.pointerTo() : popnd.ptype); | |
2806 if (popnd.pregDisp1 && !popnd.base) | |
2807 { | |
2808 if (ps && ps.isLabel() && sz == _anysize) | |
2809 sz = I32 ? _32 : _16; | |
2810 return (popnd.pregDisp1.ty & _r32) | |
2811 ? CONSTRUCT_FLAGS(sz, ASM_OPERAND_TYPE._m, ASM_MODIFIERS._addr32, 0) | |
2812 : CONSTRUCT_FLAGS(sz, ASM_OPERAND_TYPE._m, ASM_MODIFIERS._addr16, 0); | |
2813 } | |
2814 else if (ps) | |
2815 { | |
2816 if (popnd.bOffset || popnd.bSeg || ps == asmstate.psLocalsize) | |
2817 return I32 | |
2818 ? CONSTRUCT_FLAGS(_32, ASM_OPERAND_TYPE._imm, ASM_MODIFIERS._normal, 0) | |
2819 : CONSTRUCT_FLAGS(_16, ASM_OPERAND_TYPE._imm, ASM_MODIFIERS._normal, 0); | |
2820 | |
2821 if (ps.isLabel()) | |
2822 { | |
2823 switch (popnd.ajt) | |
2824 { | |
2825 case ASM_JUMPTYPE.ASM_JUMPTYPE_UNSPECIFIED: | |
2826 if (ps == asmstate.psDollar) | |
2827 { | |
2828 if (popnd.disp >= CHAR_MIN && | |
2829 popnd.disp <= CHAR_MAX) | |
2830 us = CONSTRUCT_FLAGS(_8, _rel, ASM_MODIFIERS._flbl,0); | |
2831 else | |
2832 if (popnd.disp >= SHRT_MIN && | |
2833 popnd.disp <= SHRT_MIN) | |
2834 us = CONSTRUCT_FLAGS(_16, _rel, ASM_MODIFIERS._flbl,0); | |
2835 else | |
2836 us = CONSTRUCT_FLAGS(_32, _rel, ASM_MODIFIERS._flbl,0); | |
2837 } | |
2838 else if (asmstate.ucItype != ITjump) | |
2839 { | |
2840 if (sz == _8) | |
2841 { us = CONSTRUCT_FLAGS(_8,ASM_OPERAND_TYPE._rel,ASM_MODIFIERS._flbl,0); | |
2842 break; | |
2843 } | |
2844 goto case_near; | |
2845 } | |
2846 else | |
2847 us = I32 | |
2848 ? CONSTRUCT_FLAGS(_8|_32, ASM_OPERAND_TYPE._rel, ASM_MODIFIERS._flbl, 0) | |
2849 : CONSTRUCT_FLAGS(_8|_16, ASM_OPERAND_TYPE._rel, ASM_MODIFIERS._flbl, 0); | |
2850 break; | |
2851 | |
2852 case ASM_JUMPTYPE.ASM_JUMPTYPE_NEAR: | |
2853 case_near: | |
2854 us = I32 | |
2855 ? CONSTRUCT_FLAGS(_32, _rel, _flbl, 0) | |
2856 : CONSTRUCT_FLAGS(_16, _rel, _flbl, 0); | |
2857 break; | |
2858 case ASM_JUMPTYPE.ASM_JUMPTYPE_SHORT: | |
2859 us = CONSTRUCT_FLAGS(_8, _rel, _flbl, 0); | |
2860 break; | |
2861 case ASM_JUMPTYPE.ASM_JUMPTYPE_FAR: | |
2862 us = I32 | |
2863 ? CONSTRUCT_FLAGS(_48, _rel, _flbl, 0) | |
2864 : CONSTRUCT_FLAGS(_32, _rel, _flbl, 0); | |
2865 break; | |
2866 default: | |
2867 assert(0); | |
2868 } | |
2869 return us; | |
2870 } | |
2871 if (!popnd.ptype) | |
2872 return CONSTRUCT_FLAGS(sz, _m, _normal, 0); | |
2873 ty = popnd.ptype.ty; | |
2874 if (ty == Tpointer && popnd.ptype.nextOf().ty == Tfunction && | |
2875 !ps.isVarDeclaration()) | |
2876 { | |
2877 static if (true) { | |
2878 return CONSTRUCT_FLAGS(_32, _m, _fn16, 0); | |
2879 } else { | |
2880 ty = popnd.ptype.Tnext.Tty; | |
2881 if (tyfarfunc(tybasic(ty))) { | |
2882 return I32 | |
2883 ? CONSTRUCT_FLAGS(_48, _mnoi, _fn32, 0) | |
2884 : CONSTRUCT_FLAGS(_32, _mnoi, _fn32, 0); | |
2885 } | |
2886 else { | |
2887 return I32 | |
2888 ? CONSTRUCT_FLAGS(_32, _m, _fn16, 0) | |
2889 : CONSTRUCT_FLAGS(_16, _m, _fn16, 0); | |
2890 } | |
2891 } | |
2892 } | |
2893 else if (ty == TY.Tfunction) | |
2894 { | |
2895 static if (true) { | |
2896 return CONSTRUCT_FLAGS(_32, _rel, _fn16, 0); | |
2897 } else { | |
2898 if (tyfarfunc(tybasic(ty))) | |
2899 return I32 | |
2900 ? CONSTRUCT_FLAGS(_48, _p, _fn32, 0) | |
2901 : CONSTRUCT_FLAGS(_32, _p, _fn32, 0); | |
2902 else | |
2903 return I32 | |
2904 ? CONSTRUCT_FLAGS(_32, _rel, _fn16, 0) | |
2905 : CONSTRUCT_FLAGS(_16, _rel, _fn16, 0); | |
2906 } | |
2907 } | |
2908 else if (asmstate.ucItype == IT.ITjump) | |
2909 { | |
2910 amod = _normal; | |
2911 goto L1; | |
2912 } | |
2913 else | |
2914 return CONSTRUCT_FLAGS(sz, _m, _normal, 0); | |
2915 } | |
2916 if (popnd.segreg /*|| popnd.bPtr*/) | |
2917 { | |
2918 amod = I32 ? _addr32 : _addr16; | |
2919 if (asmstate.ucItype == ITjump) | |
2920 { | |
2921 L1: | |
2922 opty = _m; | |
2923 if (I32) | |
2924 { if (sz == _48) | |
2925 opty = _mnoi; | |
2926 } | |
2927 else | |
2928 { | |
2929 if (sz == _32) | |
2930 opty = _mnoi; | |
2931 } | |
2932 us = CONSTRUCT_FLAGS(sz,opty,amod,0); | |
2933 } | |
2934 else | |
2935 us = CONSTRUCT_FLAGS(sz, | |
2936 // _rel, amod, 0); | |
2937 _m, amod, 0); | |
2938 } | |
2939 | |
2940 else if (popnd.ptype) | |
2941 us = CONSTRUCT_FLAGS(sz, _imm, _normal, 0); | |
2942 | |
2943 else if (popnd.disp >= CHAR_MIN && popnd.disp <= UCHAR_MAX) | |
2944 us = CONSTRUCT_FLAGS(_8 | _16 | _32, _imm, _normal, 0); | |
2945 else if (popnd.disp >= SHRT_MIN && popnd.disp <= USHRT_MAX) | |
2946 us = CONSTRUCT_FLAGS(_16 | _32, _imm, _normal, 0); | |
2947 else | |
2948 us = CONSTRUCT_FLAGS(_32, _imm, _normal, 0); | |
2949 return us; | |
2950 } | |
2951 | |
2952 /******************************* | |
2953 * Match flags in operand against flags in opcode table. | |
2954 * Returns: | |
2955 * !=0 if match | |
2956 */ | |
2957 | |
2958 ubyte asm_match_flags(opflag_t usOp, opflag_t usTable) | |
2959 { | |
2960 ASM_OPERAND_TYPE aoptyTable; | |
2961 ASM_OPERAND_TYPE aoptyOp; | |
2962 ASM_MODIFIERS amodTable; | |
2963 ASM_MODIFIERS amodOp; | |
2964 uint uRegmaskTable; | |
2965 uint uRegmaskOp; | |
2966 ubyte bRegmatch; | |
2967 ubyte bRetval = false; | |
2968 uint uSizemaskOp; | |
2969 uint uSizemaskTable; | |
2970 ubyte bSizematch; | |
2971 | |
2972 //printf("asm_match_flags(usOp = x%x, usTable = x%x)\n", usOp, usTable); | |
2973 if (asmstate.ucItype == IT.ITfloat) | |
2974 { | |
2975 bRetval = asm_match_float_flags(usOp, usTable); | |
2976 goto EXIT; | |
2977 } | |
2978 | |
2979 uSizemaskOp = ASM_GET_uSizemask(usOp); | |
2980 uSizemaskTable = ASM_GET_uSizemask(usTable); | |
2981 | |
2982 // Check #1, if the sizes do not match, NO match | |
2983 bSizematch = (uSizemaskOp & uSizemaskTable) != 0; | |
2984 | |
2985 amodOp = ASM_GET_amod(usOp); | |
2986 | |
2987 aoptyTable = ASM_GET_aopty(usTable); | |
2988 aoptyOp = ASM_GET_aopty(usOp); | |
2989 | |
2990 // _mmm64 matches with a 64 bit mem or an MMX register | |
2991 if (usTable == _mmm64) | |
2992 { | |
2993 if (usOp == _mm) | |
2994 goto Lmatch; | |
2995 if (aoptyOp == _m && (bSizematch || uSizemaskOp == _anysize)) | |
2996 goto Lmatch; | |
2997 goto EXIT; | |
2998 } | |
2999 | |
3000 // _xmm_m32, _xmm_m64, _xmm_m128 match with XMM register or memory | |
3001 if (usTable == _xmm_m32 || | |
3002 usTable == _xmm_m64 || | |
3003 usTable == _xmm_m128) | |
3004 { | |
3005 if (usOp == _xmm) | |
3006 goto Lmatch; | |
3007 if (aoptyOp == _m && (bSizematch || uSizemaskOp == _anysize)) | |
3008 goto Lmatch; | |
3009 } | |
3010 | |
3011 if (!bSizematch && uSizemaskTable) | |
3012 { | |
3013 //printf("no size match\n"); | |
3014 goto EXIT; | |
3015 } | |
3016 | |
3017 | |
3018 // | |
3019 // The operand types must match, otherwise return false. | |
3020 // There is one exception for the _rm which is a table entry which matches | |
3021 // _reg or _m | |
3022 // | |
3023 if (aoptyTable != aoptyOp) | |
3024 { | |
3025 if (aoptyTable == _rm && (aoptyOp == _reg || | |
3026 aoptyOp == _m || | |
3027 aoptyOp == _rel)) | |
3028 goto Lok; | |
3029 | |
3030 if (aoptyTable == _mnoi && aoptyOp == _m && | |
3031 (uSizemaskOp == _32 && amodOp == _addr16 || | |
3032 uSizemaskOp == _48 && amodOp == _addr32 || | |
3033 uSizemaskOp == _48 && amodOp == _normal) | |
3034 ) | |
3035 goto Lok; | |
3036 goto EXIT; | |
3037 } | |
3038 Lok: | |
3039 | |
3040 // | |
3041 // Looks like a match so far, check to see if anything special is going on | |
3042 // | |
3043 amodTable = ASM_GET_amod(usTable); | |
3044 uRegmaskOp = ASM_GET_uRegmask(usOp); | |
3045 uRegmaskTable = ASM_GET_uRegmask(usTable); | |
3046 bRegmatch = ((!uRegmaskTable && !uRegmaskOp) || | |
3047 (uRegmaskTable & uRegmaskOp)); | |
3048 | |
3049 switch (amodTable) | |
3050 { | |
3051 case _normal: // Normal's match with normals | |
3052 switch(amodOp) { | |
3053 case _normal: | |
3054 case _addr16: | |
3055 case _addr32: | |
3056 case _fn16: | |
3057 case _fn32: | |
3058 case _flbl: | |
3059 bRetval = (bSizematch || bRegmatch); | |
3060 goto EXIT; | |
3061 default: | |
3062 goto EXIT; | |
3063 } | |
3064 case _rseg: | |
3065 case _rspecial: | |
3066 bRetval = (amodOp == amodTable && bRegmatch); | |
3067 goto EXIT; | |
3068 } | |
3069 EXIT: | |
3070 static if (false) { | |
3071 printf("OP : "); | |
3072 asm_output_flags(usOp); | |
3073 printf("\nTBL: "); | |
3074 asm_output_flags(usTable); | |
3075 writef(": %s\n", bRetval ? "MATCH" : "NOMATCH"); | |
3076 } | |
3077 return bRetval; | |
3078 | |
3079 Lmatch: | |
3080 //printf("match\n"); | |
3081 return 1; | |
3082 } | |
3083 | |
3084 void asm_output_flags(opflag_t usFlags) | |
3085 { | |
3086 ASM_OPERAND_TYPE aopty = ASM_GET_aopty(usFlags); | |
3087 ASM_MODIFIERS amod = ASM_GET_amod(usFlags); | |
3088 uint uRegmask = ASM_GET_uRegmask(usFlags); | |
3089 uint uSizemask = ASM_GET_uSizemask(usFlags); | |
3090 | |
3091 if (uSizemask == _anysize) | |
3092 printf("_anysize "); | |
3093 else if (uSizemask == 0) | |
3094 printf("0 "); | |
3095 else | |
3096 { | |
3097 if (uSizemask & _8) | |
3098 printf("_8 "); | |
3099 if (uSizemask & _16) | |
3100 printf("_16 "); | |
3101 if (uSizemask & _32) | |
3102 printf("_32 "); | |
3103 if (uSizemask & _48) | |
3104 printf("_48 "); | |
3105 } | |
3106 | |
3107 printf("_"); | |
3108 switch (aopty) { | |
3109 case ASM_OPERAND_TYPE._reg: | |
3110 printf("reg "); | |
3111 break; | |
3112 case ASM_OPERAND_TYPE._m: | |
3113 printf("m "); | |
3114 break; | |
3115 case ASM_OPERAND_TYPE._imm: | |
3116 printf("imm "); | |
3117 break; | |
3118 case ASM_OPERAND_TYPE._rel: | |
3119 printf("rel "); | |
3120 break; | |
3121 case ASM_OPERAND_TYPE._mnoi: | |
3122 printf("mnoi "); | |
3123 break; | |
3124 case ASM_OPERAND_TYPE._p: | |
3125 printf("p "); | |
3126 break; | |
3127 case ASM_OPERAND_TYPE._rm: | |
3128 printf("rm "); | |
3129 break; | |
3130 case ASM_OPERAND_TYPE._float: | |
3131 printf("float "); | |
3132 break; | |
3133 default: | |
3134 printf(" UNKNOWN "); | |
3135 } | |
3136 | |
3137 printf("_"); | |
3138 switch (amod) { | |
3139 case ASM_MODIFIERS._normal: | |
3140 printf("normal "); | |
3141 if (uRegmask & 1) printf("_al "); | |
3142 if (uRegmask & 2) printf("_ax "); | |
3143 if (uRegmask & 4) printf("_eax "); | |
3144 if (uRegmask & 8) printf("_dx "); | |
3145 if (uRegmask & 0x10) printf("_cl "); | |
3146 return; | |
3147 case ASM_MODIFIERS._rseg: | |
3148 printf("rseg "); | |
3149 break; | |
3150 case ASM_MODIFIERS._rspecial: | |
3151 printf("rspecial "); | |
3152 break; | |
3153 case ASM_MODIFIERS._addr16: | |
3154 printf("addr16 "); | |
3155 break; | |
3156 case ASM_MODIFIERS._addr32: | |
3157 printf("addr32 "); | |
3158 break; | |
3159 case ASM_MODIFIERS._fn16: | |
3160 printf("fn16 "); | |
3161 break; | |
3162 case ASM_MODIFIERS._fn32: | |
3163 printf("fn32 "); | |
3164 break; | |
3165 case ASM_MODIFIERS._flbl: | |
3166 printf("flbl "); | |
3167 break; | |
3168 default: | |
3169 printf("UNKNOWN "); | |
3170 break; | |
3171 } | |
3172 | |
3173 printf("uRegmask=x%02x", uRegmask); | |
3174 } | |
3175 | |
3176 OPND* asm_log_or_exp() | |
3177 { | |
3178 OPND* o1 = asm_log_and_exp(); | |
3179 OPND* o2; | |
3180 | |
3181 while (tok_value == TOK.TOKoror) | |
3182 { | |
3183 asm_token(); | |
3184 o2 = asm_log_and_exp(); | |
3185 if (asm_isint(o1) && asm_isint(o2)) | |
3186 o1.disp = o1.disp || o2.disp; | |
3187 else | |
3188 asmerr(ASMERRMSGS.EM_bad_integral_operand); // illegal operand | |
3189 o2.disp = 0; | |
3190 o1 = asm_merge_opnds(o1, o2); | |
3191 } | |
3192 return o1; | |
3193 } | |
3194 | |
3195 void asm_chktok(TOK toknum, uint errnum) | |
3196 { | |
3197 if (tok_value == toknum) | |
3198 asm_token(); // scan past token | |
3199 else | |
3200 /* When we run out of tokens, asmtok is null. | |
3201 * But when this happens when a ';' was hit. | |
3202 */ | |
3203 asmerr(errnum, asmtok ? asmtok.toChars() : ";"); | |
3204 } | |
3205 | |
3206 opflag_t asm_determine_float_flags(OPND* popnd) | |
3207 { | |
3208 //printf("asm_determine_float_flags()\n"); | |
3209 | |
3210 opflag_t us; | |
3211 opflag_t usFloat; | |
3212 | |
3213 // Insure that if it is a register, that it is not a normal processor | |
3214 // register. | |
3215 | |
3216 if (popnd.base && | |
3217 !popnd.s && !popnd.disp && !popnd.real_ | |
3218 && !(popnd.base.ty & (_r8 | _r16 | _r32))) | |
3219 { | |
3220 return popnd.base.ty; | |
3221 } | |
3222 if (popnd.pregDisp1 && !popnd.base) | |
3223 { | |
3224 us = asm_float_type_size(popnd.ptype, &usFloat); | |
3225 //printf("us = x%x, usFloat = x%x\n", us, usFloat); | |
3226 if (popnd.pregDisp1.ty & _r32) | |
3227 return(CONSTRUCT_FLAGS(us, _m, _addr32, usFloat)); | |
3228 else if (popnd.pregDisp1.ty & _r16) | |
3229 return(CONSTRUCT_FLAGS(us, _m, _addr16, usFloat)); | |
3230 } | |
3231 else if (popnd.s !is null) | |
3232 { | |
3233 us = asm_float_type_size(popnd.ptype, &usFloat); | |
3234 return CONSTRUCT_FLAGS(us, _m, _normal, usFloat); | |
3235 } | |
3236 | |
3237 if (popnd.segreg) | |
3238 { | |
3239 us = asm_float_type_size(popnd.ptype, &usFloat); | |
3240 if (I32) | |
3241 return(CONSTRUCT_FLAGS(us, _m, _addr32, usFloat)); | |
3242 else | |
3243 return(CONSTRUCT_FLAGS(us, _m, _addr16, usFloat)); | |
3244 } | |
3245 | |
3246 static if (false) { | |
3247 if (popnd.real_) | |
3248 { | |
3249 switch (popnd.ptype.ty) | |
3250 { | |
3251 case TY.Tfloat32: | |
3252 popnd.s = fconst(popnd.real_); | |
3253 return(CONSTRUCT_FLAGS(_32, _m, _normal, 0)); | |
3254 | |
3255 case TY.Tfloat64: | |
3256 popnd.s = dconst(popnd.real_); | |
3257 return(CONSTRUCT_FLAGS(0, _m, _normal, _64)); | |
3258 | |
3259 case TY.Tfloat80: | |
3260 popnd.s = ldconst(popnd.real_); | |
3261 return(CONSTRUCT_FLAGS(0, _m, _normal, _80)); | |
3262 } | |
3263 } | |
3264 } | |
3265 | |
3266 asmerr(ASMERRMSGS.EM_bad_float_op); // unknown operand for floating point instruction | |
3267 return 0; | |
3268 } | |
3269 | |
3270 uint asm_type_size(Type ptype) | |
3271 { | |
3272 //if (ptype) printf("asm_type_size('%s') = %d\n", ptype.toChars(), (int)ptype.size()); | |
3273 uint u = _anysize; | |
3274 if (ptype && ptype.ty != TY.Tfunction /*&& ptype.isscalar()*/) | |
3275 { | |
3276 switch (cast(int)ptype.size()) | |
3277 { | |
3278 case 0: asmerr(ASMERRMSGS.EM_bad_op, "0 size"); break; | |
3279 case 1: u = _8; break; | |
3280 case 2: u = _16; break; | |
3281 case 4: u = _32; break; | |
3282 case 6: u = _48; break; | |
3283 default: | |
3284 break; /// | |
3285 } | |
3286 } | |
3287 return u; | |
3288 } | |
3289 | |
3290 bool asm_match_float_flags(opflag_t usOp, opflag_t usTable) | |
3291 { | |
3292 ASM_OPERAND_TYPE aoptyTable; | |
3293 ASM_OPERAND_TYPE aoptyOp; | |
3294 ASM_MODIFIERS amodTable; | |
3295 ASM_MODIFIERS amodOp; | |
3296 uint uRegmaskTable; | |
3297 uint uRegmaskOp; | |
3298 ubyte bRegmatch; | |
3299 | |
3300 | |
3301 // | |
3302 // Check #1, if the sizes do not match, NO match | |
3303 // | |
3304 uRegmaskOp = ASM_GET_uRegmask(usOp); | |
3305 uRegmaskTable = ASM_GET_uRegmask(usTable); | |
3306 bRegmatch = (uRegmaskTable & uRegmaskOp) != 0; | |
3307 | |
3308 if (!(ASM_GET_uSizemask(usTable) & ASM_GET_uSizemask(usOp) || bRegmatch)) | |
3309 return false; | |
3310 | |
3311 aoptyTable = ASM_GET_aopty(usTable); | |
3312 aoptyOp = ASM_GET_aopty(usOp); | |
3313 // | |
3314 // The operand types must match, otherwise return false. | |
3315 // There is one exception for the _rm which is a table entry which matches | |
3316 // _reg or _m | |
3317 // | |
3318 if (aoptyTable != aoptyOp) | |
3319 { | |
3320 if (aoptyOp != _float) | |
3321 return false; | |
3322 } | |
3323 | |
3324 // | |
3325 // Looks like a match so far, check to see if anything special is going on | |
3326 // | |
3327 amodOp = ASM_GET_amod(usOp); | |
3328 amodTable = ASM_GET_amod(usTable); | |
3329 switch (amodTable) | |
3330 { | |
3331 // Normal's match with normals | |
3332 case _normal: | |
3333 switch(amodOp) | |
3334 { | |
3335 case _normal: | |
3336 case _addr16: | |
3337 case _addr32: | |
3338 case _fn16: | |
3339 case _fn32: | |
3340 case _flbl: | |
3341 return true; | |
3342 | |
3343 default: | |
3344 return false; | |
3345 } | |
3346 | |
3347 case _rseg: | |
3348 case _rspecial: | |
3349 return false; | |
3350 default: | |
3351 assert(0); | |
3352 } | |
3353 } | |
3354 | |
3355 OPND* asm_log_and_exp() | |
3356 { | |
3357 OPND* o1 = asm_inc_or_exp(); | |
3358 OPND* o2; | |
3359 | |
3360 while (tok_value == TOKandand) | |
3361 { | |
3362 asm_token(); | |
3363 o2 = asm_inc_or_exp(); | |
3364 if (asm_isint(o1) && asm_isint(o2)) | |
3365 o1.disp = o1.disp && o2.disp; | |
3366 else { | |
3367 asmerr(EM_bad_integral_operand); // illegal operand | |
3368 } | |
3369 o2.disp = 0; | |
3370 o1 = asm_merge_opnds(o1, o2); | |
3371 } | |
3372 return o1; | |
3373 } | |
3374 | |
3375 bool asm_isint(OPND *o) | |
3376 { | |
3377 if (!o || o.base || o.s) | |
3378 return false; | |
3379 | |
3380 //return o.disp != 0; | |
3381 return true; | |
3382 } | |
3383 | |
3384 /******************************* | |
3385 * Merge operands o1 and o2 into a single operand. | |
3386 */ | |
3387 | |
3388 OPND* asm_merge_opnds(OPND* o1, OPND* o2) | |
3389 { | |
3390 debug immutable(char)* psz; | |
3391 debug if (debuga) | |
3392 { | |
3393 printf("asm_merge_opnds(o1 = "); | |
3394 if (o1) asm_output_popnd(o1); | |
3395 printf(", o2 = "); | |
3396 if (o2) asm_output_popnd(o2); | |
3397 printf(")\n"); | |
3398 } | |
3399 | |
3400 if (!o1) | |
3401 return o2; | |
3402 if (!o2) | |
3403 return o1; | |
3404 version (EXTRA_DEBUG) { | |
3405 printf("Combining Operands: mult1 = %d, mult2 = %d", | |
3406 o1.uchMultiplier, o2.uchMultiplier); | |
3407 } | |
3408 /* combine the OPND's disp field */ | |
3409 if (o2.segreg) { | |
3410 if (o1.segreg) { | |
3411 debug psz = "o1.segment && o2.segreg".ptr; | |
3412 goto ILLEGAL_ADDRESS_ERROR; | |
3413 } | |
3414 else | |
3415 o1.segreg = o2.segreg; | |
3416 } | |
3417 | |
3418 // combine the OPND's symbol field | |
3419 if (o1.s && o2.s) | |
3420 { | |
3421 debug psz = "o1.s && os.s"; | |
3422 ILLEGAL_ADDRESS_ERROR: | |
3423 debug printf("Invalid addr because /%s/\n", psz); | |
3424 | |
3425 asmerr(EM_bad_addr_mode); // illegal addressing mode | |
3426 } | |
3427 else if (o2.s) | |
3428 o1.s = o2.s; | |
3429 else if (o1.s && o1.s.isTupleDeclaration()) | |
3430 { | |
3431 TupleDeclaration tup = o1.s.isTupleDeclaration(); | |
3432 | |
3433 size_t index = o2.disp; | |
3434 if (index >= tup.objects.dim) | |
3435 error(asmstate.loc, "tuple index %u exceeds %u", index, tup.objects.dim); | |
3436 else | |
3437 { | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
54
diff
changeset
|
3438 Object o = tup.objects[index]; |
0 | 3439 if (auto d = cast(Dsymbol)o) |
3440 { | |
3441 o1.s = d; | |
3442 return o1; | |
3443 } | |
3444 else if (auto e = cast(Expression)o) | |
3445 { | |
3446 if (e.op == TOKvar) | |
3447 { | |
3448 o1.s = (cast(VarExp)e).var; | |
3449 return o1; | |
3450 } | |
3451 else if (e.op == TOKfunction) | |
3452 { | |
3453 o1.s = (cast(FuncExp)e).fd; | |
3454 return o1; | |
3455 } | |
3456 } | |
3457 error(asmstate.loc, "invalid asm operand %s", o1.s.toChars()); | |
3458 } | |
3459 } | |
3460 | |
3461 if (o1.disp && o2.disp) | |
3462 o1.disp += o2.disp; | |
3463 else if (o2.disp) | |
3464 o1.disp = o2.disp; | |
3465 | |
3466 /* combine the OPND's base field */ | |
3467 if (o1.base !is null && o2.base !is null) { | |
3468 debug psz = "o1.base != null && o2.base != null".ptr; | |
3469 goto ILLEGAL_ADDRESS_ERROR; | |
3470 } | |
3471 else if (o2.base) | |
3472 o1.base = o2.base; | |
3473 | |
3474 /* Combine the displacement register fields */ | |
3475 if (o2.pregDisp1) | |
3476 { | |
3477 if (o1.pregDisp2) | |
3478 { | |
3479 debug psz = "o2.pregDisp1 && o1.pregDisp2"; | |
3480 goto ILLEGAL_ADDRESS_ERROR; | |
3481 } | |
3482 else if (o1.pregDisp1) | |
3483 { | |
3484 if (o1.uchMultiplier || | |
3485 (o2.pregDisp1.val == _ESP && | |
3486 (o2.pregDisp1.ty & _r32) && | |
3487 !o2.uchMultiplier)) | |
3488 { | |
3489 o1.pregDisp2 = o1.pregDisp1; | |
3490 o1.pregDisp1 = o2.pregDisp1; | |
3491 } | |
3492 else | |
3493 o1.pregDisp2 = o2.pregDisp1; | |
3494 } | |
3495 else | |
3496 o1.pregDisp1 = o2.pregDisp1; | |
3497 } | |
3498 if (o2.pregDisp2) { | |
3499 if (o1.pregDisp2) { | |
3500 debug psz = "o1.pregDisp2 && o2.pregDisp2"; | |
3501 goto ILLEGAL_ADDRESS_ERROR; | |
3502 } | |
3503 else | |
3504 o1.pregDisp2 = o2.pregDisp2; | |
3505 } | |
3506 if (o2.uchMultiplier) | |
3507 { | |
3508 if (o1.uchMultiplier) | |
3509 { | |
3510 debug psz = "o1.uchMultiplier && o2.uchMultiplier"; | |
3511 goto ILLEGAL_ADDRESS_ERROR; | |
3512 } | |
3513 else | |
3514 o1.uchMultiplier = o2.uchMultiplier; | |
3515 } | |
3516 if (o2.ptype && !o1.ptype) | |
3517 o1.ptype = o2.ptype; | |
3518 if (o2.bOffset) | |
3519 o1.bOffset = o2.bOffset; | |
3520 if (o2.bSeg) | |
3521 o1.bSeg = o2.bSeg; | |
3522 | |
3523 if (o2.ajt && !o1.ajt) | |
3524 o1.ajt = o2.ajt; | |
3525 | |
3526 opnd_free(o2); | |
3527 version (EXTRA_DEBUG) { | |
3528 printf("Result = %d\n", o1.uchMultiplier); | |
3529 } | |
3530 debug if (debuga) | |
3531 { printf("Merged result = /"); | |
3532 asm_output_popnd(o1); | |
3533 printf("/\n"); | |
3534 } | |
3535 | |
3536 return o1; | |
3537 } | |
3538 | |
3539 opflag_t asm_float_type_size(Type ptype, opflag_t* pusFloat) | |
3540 { | |
3541 *pusFloat = 0; | |
3542 | |
3543 //printf("asm_float_type_size('%s')\n", ptype.toChars()); | |
3544 if (ptype && ptype.isscalar()) | |
3545 { | |
3546 int sz = cast(int)ptype.size(); | |
3547 if (sz == REALSIZE) | |
3548 { | |
3549 *pusFloat = _80; | |
3550 return 0; | |
3551 } | |
3552 switch (sz) | |
3553 { | |
3554 case 2: | |
3555 return _16; | |
3556 case 4: | |
3557 return _32; | |
3558 case 8: | |
3559 *pusFloat = _64; | |
3560 return 0; | |
3561 default: | |
3562 break; | |
3563 } | |
3564 } | |
3565 | |
3566 *pusFloat = _fanysize; | |
3567 return _anysize; | |
3568 } | |
3569 | |
3570 OPND* asm_inc_or_exp() | |
3571 { | |
3572 OPND* o1 = asm_xor_exp(); | |
3573 OPND* o2; | |
3574 | |
3575 while (tok_value == TOKor) | |
3576 { | |
3577 asm_token(); | |
3578 o2 = asm_xor_exp(); | |
3579 if (asm_isint(o1) && asm_isint(o2)) | |
3580 o1.disp |= o2.disp; | |
3581 else { | |
3582 asmerr(EM_bad_integral_operand); // illegal operand | |
3583 } | |
3584 o2.disp = 0; | |
3585 o1 = asm_merge_opnds(o1, o2); | |
3586 } | |
3587 return o1; | |
3588 } | |
3589 | |
3590 OPND* asm_xor_exp() | |
3591 { | |
3592 OPND* o1 = asm_and_exp(); | |
3593 OPND* o2; | |
3594 | |
3595 while (tok_value == TOKxor) | |
3596 { | |
3597 asm_token(); | |
3598 o2 = asm_and_exp(); | |
3599 if (asm_isint(o1) && asm_isint(o2)) | |
3600 o1.disp ^= o2.disp; | |
3601 else { | |
3602 asmerr(EM_bad_integral_operand); // illegal operand | |
3603 } | |
3604 o2.disp = 0; | |
3605 o1 = asm_merge_opnds(o1, o2); | |
3606 } | |
3607 return o1; | |
3608 } | |
3609 | |
3610 OPND* asm_and_exp() | |
3611 { | |
3612 OPND* o1 = asm_equal_exp(); | |
3613 OPND* o2; | |
3614 | |
3615 while (tok_value == TOKand) | |
3616 { | |
3617 asm_token(); | |
3618 o2 = asm_equal_exp(); | |
3619 if (asm_isint(o1) && asm_isint(o2)) | |
3620 o1.disp &= o2.disp; | |
3621 else { | |
3622 asmerr(EM_bad_integral_operand); // illegal operand | |
3623 } | |
3624 o2.disp = 0; | |
3625 o1 = asm_merge_opnds(o1, o2); | |
3626 } | |
3627 return o1; | |
3628 } | |
3629 | |
3630 OPND* asm_equal_exp() | |
3631 { | |
3632 OPND* o1 = asm_rel_exp(); | |
3633 OPND* o2; | |
3634 | |
3635 while (1) | |
3636 { | |
3637 switch (tok_value) | |
3638 { | |
3639 case TOKequal: | |
3640 asm_token(); | |
3641 o2 = asm_rel_exp(); | |
3642 if (asm_isint(o1) && asm_isint(o2)) | |
3643 o1.disp = o1.disp == o2.disp; | |
3644 else { | |
3645 asmerr(EM_bad_integral_operand); // illegal operand | |
3646 } | |
3647 o2.disp = 0; | |
3648 o1 = asm_merge_opnds(o1, o2); | |
3649 break; | |
3650 | |
3651 case TOKnotequal: | |
3652 asm_token(); | |
3653 o2 = asm_rel_exp(); | |
3654 if (asm_isint(o1) && asm_isint(o2)) | |
3655 o1.disp = o1.disp != o2.disp; | |
3656 else { | |
3657 asmerr(EM_bad_integral_operand); | |
3658 } | |
3659 o2.disp = 0; | |
3660 o1 = asm_merge_opnds(o1, o2); | |
3661 break; | |
3662 | |
3663 default: | |
3664 return o1; | |
3665 } | |
3666 } | |
3667 | |
3668 assert(false); | |
3669 } | |
3670 | |
3671 OPND* asm_rel_exp() | |
3672 { | |
3673 OPND* o1 = asm_shift_exp(); | |
3674 OPND* o2; | |
3675 | |
3676 TOK tok_save; | |
3677 | |
3678 while (1) | |
3679 { | |
3680 switch (tok_value) | |
3681 { | |
3682 case TOKgt: | |
3683 case TOKge: | |
3684 case TOKlt: | |
3685 case TOKle: | |
3686 tok_save = tok_value; | |
3687 asm_token(); | |
3688 o2 = asm_shift_exp(); | |
3689 if (asm_isint(o1) && asm_isint(o2)) | |
3690 { | |
3691 switch (tok_save) | |
3692 { | |
3693 case TOKgt: | |
3694 o1.disp = o1.disp > o2.disp; | |
3695 break; | |
3696 case TOKge: | |
3697 o1.disp = o1.disp >= o2.disp; | |
3698 break; | |
3699 case TOKlt: | |
3700 o1.disp = o1.disp < o2.disp; | |
3701 break; | |
3702 case TOKle: | |
3703 o1.disp = o1.disp <= o2.disp; | |
3704 break; | |
3705 } | |
3706 } | |
3707 else | |
3708 asmerr(EM_bad_integral_operand); | |
3709 o2.disp = 0; | |
3710 o1 = asm_merge_opnds(o1, o2); | |
3711 break; | |
3712 | |
3713 default: | |
3714 return o1; | |
3715 } | |
3716 } | |
3717 | |
3718 assert(false); | |
3719 } | |
3720 | |
3721 OPND* asm_shift_exp() | |
3722 { | |
3723 OPND* o1 = asm_add_exp(); | |
3724 OPND* o2; | |
3725 | |
3726 int op; | |
3727 TOK tk; | |
3728 | |
3729 while (tok_value == TOKshl || tok_value == TOKshr || tok_value == TOKushr) | |
3730 { | |
3731 tk = tok_value; | |
3732 asm_token(); | |
3733 o2 = asm_add_exp(); | |
3734 if (asm_isint(o1) && asm_isint(o2)) | |
3735 { if (tk == TOKshl) | |
3736 o1.disp <<= o2.disp; | |
3737 else if (tk == TOKushr) | |
3738 o1.disp = cast(uint)o1.disp >> o2.disp; | |
3739 else | |
3740 o1.disp >>= o2.disp; | |
3741 } | |
3742 else | |
3743 asmerr(EM_bad_integral_operand); | |
3744 o2.disp = 0; | |
3745 o1 = asm_merge_opnds(o1, o2); | |
3746 } | |
3747 return o1; | |
3748 } | |
3749 | |
3750 /******************************* | |
3751 */ | |
3752 | |
3753 OPND* asm_add_exp() | |
3754 { | |
3755 OPND* o1 = asm_mul_exp(); | |
3756 OPND* o2; | |
3757 | |
3758 while (1) | |
3759 { | |
3760 switch (tok_value) | |
3761 { | |
3762 case TOKadd: | |
3763 asm_token(); | |
3764 o2 = asm_mul_exp(); | |
3765 o1 = asm_merge_opnds(o1, o2); | |
3766 break; | |
3767 | |
3768 case TOKmin: | |
3769 asm_token(); | |
3770 o2 = asm_mul_exp(); | |
3771 if (asm_isint(o1) && asm_isint(o2)) | |
3772 { | |
3773 o1.disp -= o2.disp; | |
3774 o2.disp = 0; | |
3775 } | |
3776 else | |
3777 o2.disp = - o2.disp; | |
3778 o1 = asm_merge_opnds(o1, o2); | |
3779 break; | |
3780 | |
3781 default: | |
3782 return o1; | |
3783 } | |
3784 } | |
3785 | |
3786 assert(false); | |
3787 } | |
3788 | |
3789 /******************************* | |
3790 */ | |
3791 | |
3792 OPND* asm_mul_exp() | |
3793 { | |
3794 OPND* o1; | |
3795 OPND* o2; | |
3796 OPND* popndTmp; | |
3797 | |
3798 //printf("+asm_mul_exp()\n"); | |
3799 o1 = asm_br_exp(); | |
3800 while (1) | |
3801 { | |
3802 switch (tok_value) | |
3803 { | |
3804 case TOKmul: | |
3805 asm_token(); | |
3806 o2 = asm_br_exp(); | |
3807 version (EXTRA_DEBUG) { | |
3808 printf("Star o1.isint=%d, o2.isint=%d, lbra_seen=%d\n", | |
3809 asm_isint(o1), asm_isint(o2), asm_TKlbra_seen ); | |
3810 } | |
3811 if (asm_isNonZeroInt(o1) && asm_isNonZeroInt(o2)) | |
3812 o1.disp *= o2.disp; | |
3813 else if (asm_TKlbra_seen && o1.pregDisp1 && asm_isNonZeroInt(o2)) | |
3814 { | |
3815 o1.uchMultiplier = o2.disp; | |
3816 version (EXTRA_DEBUG) { | |
3817 printf("Multiplier: %d\n", o1.uchMultiplier); | |
3818 } | |
3819 } | |
3820 else if (asm_TKlbra_seen && o2.pregDisp1 && asm_isNonZeroInt(o1)) | |
3821 { | |
3822 popndTmp = o2; | |
3823 o2 = o1; | |
3824 o1 = popndTmp; | |
3825 o1.uchMultiplier = o2.disp; | |
3826 version (EXTRA_DEBUG) { | |
3827 printf("Multiplier: %d\n", | |
3828 o1.uchMultiplier); | |
3829 } | |
3830 } | |
3831 else if (asm_isint(o1) && asm_isint(o2)) | |
3832 o1.disp *= o2.disp; | |
3833 else | |
3834 asmerr(EM_bad_operand); | |
3835 o2.disp = 0; | |
3836 o1 = asm_merge_opnds(o1, o2); | |
3837 break; | |
3838 | |
3839 case TOKdiv: | |
3840 asm_token(); | |
3841 o2 = asm_br_exp(); | |
3842 if (asm_isint(o1) && asm_isint(o2)) | |
3843 o1.disp /= o2.disp; | |
3844 else | |
3845 asmerr(EM_bad_integral_operand); | |
3846 o2.disp = 0; | |
3847 o1 = asm_merge_opnds(o1, o2); | |
3848 break; | |
3849 | |
3850 case TOKmod: | |
3851 asm_token(); | |
3852 o2 = asm_br_exp(); | |
3853 if (asm_isint(o1) && asm_isint(o2)) | |
3854 o1.disp %= o2.disp; | |
3855 else | |
3856 asmerr(EM_bad_integral_operand); | |
3857 o2.disp = 0; | |
3858 o1 = asm_merge_opnds(o1, o2); | |
3859 break; | |
3860 | |
3861 default: | |
3862 return o1; | |
3863 } | |
3864 } | |
3865 | |
3866 return o1; | |
3867 } | |
3868 | |
3869 OPND* asm_br_exp() | |
3870 { | |
3871 //printf("asm_br_exp()\n"); | |
3872 | |
3873 OPND* o1 = asm_una_exp(); | |
3874 OPND* o2; | |
3875 Declaration s; | |
3876 | |
3877 while (1) | |
3878 { | |
3879 switch (tok_value) | |
3880 { | |
3881 case TOKlbracket: | |
3882 { | |
3883 version (EXTRA_DEBUG) { | |
3884 printf("Saw a left bracket\n"); | |
3885 } | |
3886 asm_token(); | |
3887 asm_TKlbra_seen++; | |
3888 o2 = asm_cond_exp(); | |
3889 asm_TKlbra_seen--; | |
3890 asm_chktok(TOKrbracket,EM_rbra); | |
3891 version (EXTRA_DEBUG) { | |
3892 printf("Saw a right bracket\n"); | |
3893 } | |
3894 o1 = asm_merge_opnds(o1, o2); | |
3895 if (tok_value == TOKidentifier) | |
3896 { o2 = asm_una_exp(); | |
3897 o1 = asm_merge_opnds(o1, o2); | |
3898 } | |
3899 break; | |
3900 } | |
3901 default: | |
3902 return o1; | |
3903 } | |
3904 } | |
3905 | |
3906 assert(false); | |
3907 } | |
3908 | |
3909 /******************************* | |
3910 */ | |
3911 | |
3912 OPND* asm_una_exp() | |
3913 { | |
3914 OPND* o1; | |
3915 int op; | |
3916 Type ptype; | |
3917 Type ptypeSpec; | |
3918 ASM_JUMPTYPE ajt = ASM_JUMPTYPE_UNSPECIFIED; | |
3919 char bPtr = 0; | |
3920 | |
3921 switch (cast(int)tok_value) | |
3922 { | |
3923 static if (false) { | |
3924 case TOKand: | |
3925 asm_token(); | |
3926 o1 = asm_una_exp(); | |
3927 break; | |
3928 | |
3929 case TOKmul: | |
3930 asm_token(); | |
3931 o1 = asm_una_exp(); | |
3932 ++o1.indirect; | |
3933 break; | |
3934 } | |
3935 case TOKadd: | |
3936 asm_token(); | |
3937 o1 = asm_una_exp(); | |
3938 break; | |
3939 | |
3940 case TOKmin: | |
3941 asm_token(); | |
3942 o1 = asm_una_exp(); | |
3943 if (asm_isint(o1)) | |
3944 o1.disp = -o1.disp; | |
3945 break; | |
3946 | |
3947 case TOKnot: | |
3948 asm_token(); | |
3949 o1 = asm_una_exp(); | |
3950 if (asm_isint(o1)) | |
3951 o1.disp = !o1.disp; | |
3952 break; | |
3953 | |
3954 case TOKtilde: | |
3955 asm_token(); | |
3956 o1 = asm_una_exp(); | |
3957 if (asm_isint(o1)) | |
3958 o1.disp = ~o1.disp; | |
3959 break; | |
3960 | |
3961 static if (false) { | |
3962 case TOKlparen: | |
3963 // stoken() is called directly here because we really | |
3964 // want the INT token to be an INT. | |
3965 stoken(); | |
3966 if (type_specifier(&ptypeSpec)) /* if type_name */ | |
3967 { | |
3968 ptype = declar_abstract(ptypeSpec); | |
3969 /* read abstract_declarator */ | |
3970 fixdeclar(ptype);/* fix declarator */ | |
3971 type_free(ptypeSpec);/* the declar() function | |
3972 allocates the typespec again */ | |
3973 chktok(TOKrparen,EM_rpar); | |
3974 ptype.Tcount--; | |
3975 goto CAST_REF; | |
3976 } | |
3977 else | |
3978 { | |
3979 type_free(ptypeSpec); | |
3980 o1 = asm_cond_exp(); | |
3981 chktok(TOKrparen, EM_rpar); | |
3982 } | |
3983 break; | |
3984 } | |
3985 | |
3986 case TOKidentifier: | |
3987 // Check for offset keyword | |
3988 if (asmtok.ident == Id.offset) | |
3989 { | |
3990 if (!global.params.useDeprecated) | |
3991 error(asmstate.loc, "offset deprecated, use offsetof"); | |
3992 goto Loffset; | |
3993 } | |
3994 if (asmtok.ident == Id.offsetof) | |
3995 { | |
3996 Loffset: | |
3997 asm_token(); | |
3998 o1 = asm_cond_exp(); | |
3999 if (!o1) | |
4000 o1 = opnd_calloc(); | |
4001 o1.bOffset= true; | |
4002 } | |
4003 else | |
4004 o1 = asm_primary_exp(); | |
4005 break; | |
4006 | |
4007 case ASMTK.ASMTKseg: | |
4008 asm_token(); | |
4009 o1 = asm_cond_exp(); | |
4010 if (!o1) | |
4011 o1 = opnd_calloc(); | |
4012 o1.bSeg= true; | |
4013 break; | |
4014 | |
4015 case TOKint16: | |
4016 if (asmstate.ucItype != ITjump) | |
4017 { | |
4018 ptype = Type.tint16; | |
4019 goto TYPE_REF; | |
4020 } | |
4021 ajt = ASM_JUMPTYPE_SHORT; | |
4022 asm_token(); | |
4023 goto JUMP_REF2; | |
4024 | |
4025 case ASMTKnear: | |
4026 ajt = ASM_JUMPTYPE_NEAR; | |
4027 goto JUMP_REF; | |
4028 | |
4029 case ASMTKfar: | |
4030 ajt = ASM_JUMPTYPE_FAR; | |
4031 JUMP_REF: | |
4032 asm_token(); | |
4033 asm_chktok(cast(TOK) ASMTKptr, EM_ptr_exp); | |
4034 JUMP_REF2: | |
4035 o1 = asm_cond_exp(); | |
4036 if (!o1) | |
4037 o1 = opnd_calloc(); | |
4038 o1.ajt= ajt; | |
4039 break; | |
4040 | |
4041 case TOKint8: | |
4042 ptype = Type.tint8; | |
4043 goto TYPE_REF; | |
4044 case TOKint32: | |
4045 case ASMTKdword: | |
4046 ptype = Type.tint32; | |
4047 goto TYPE_REF; | |
4048 case TOKfloat32: | |
4049 ptype = Type.tfloat32; | |
4050 goto TYPE_REF; | |
4051 case ASMTKqword: | |
4052 case TOKfloat64: | |
4053 ptype = Type.tfloat64; | |
4054 goto TYPE_REF; | |
4055 case TOKfloat80: | |
4056 ptype = Type.tfloat80; | |
4057 goto TYPE_REF; | |
4058 case ASMTKword: | |
4059 ptype = Type.tint16; | |
4060 TYPE_REF: | |
4061 bPtr = 1; | |
4062 asm_token(); | |
4063 asm_chktok(cast(TOK) ASMTKptr, EM_ptr_exp); | |
4064 CAST_REF: | |
4065 o1 = asm_cond_exp(); | |
4066 if (!o1) | |
4067 o1 = opnd_calloc(); | |
4068 o1.ptype = ptype; | |
4069 o1.bPtr = bPtr; | |
4070 break; | |
4071 | |
4072 default: | |
4073 o1 = asm_primary_exp(); | |
4074 break; | |
4075 } | |
4076 return o1; | |
4077 } | |
4078 | |
4079 bool asm_isNonZeroInt(OPND* o) | |
4080 { | |
4081 if (!o || o.base || o.s) | |
4082 return false; | |
4083 | |
4084 return o.disp != 0; | |
4085 } | |
4086 | |
4087 OPND* asm_primary_exp() | |
4088 { | |
4089 OPND* o1 = null; | |
4090 OPND* o2 = null; | |
4091 Type ptype; | |
4092 Dsymbol s; | |
4093 Dsymbol scopesym; | |
4094 | |
4095 TOK tkOld; | |
4096 int global; | |
4097 REG* regp; | |
4098 | |
4099 global = 0; | |
4100 switch (cast(int)tok_value) | |
4101 { | |
4102 case TOKdollar: | |
4103 o1 = opnd_calloc(); | |
4104 o1.s = asmstate.psDollar; | |
4105 asm_token(); | |
4106 break; | |
4107 | |
4108 static if (false) { | |
4109 case TOKthis: | |
4110 strcpy(tok.TKid,cpp_name_this); | |
4111 } | |
4112 case TOKidentifier: | |
4113 case_ident: | |
4114 o1 = opnd_calloc(); | |
4115 regp = asm_reg_lookup(asmtok.ident.toChars()); | |
4116 if (regp !is null) | |
4117 { | |
4118 asm_token(); | |
4119 // see if it is segment override (like SS:) | |
4120 if (!asm_TKlbra_seen && | |
4121 (regp.ty & _seg) && | |
4122 tok_value == TOKcolon) | |
4123 { | |
4124 o1.segreg = regp; | |
4125 asm_token(); | |
4126 o2 = asm_cond_exp(); | |
4127 o1 = asm_merge_opnds(o1, o2); | |
4128 } | |
4129 else if (asm_TKlbra_seen) | |
4130 { // should be a register | |
4131 if (o1.pregDisp1) | |
4132 asmerr(EM_bad_operand); | |
4133 else | |
4134 o1.pregDisp1 = regp; | |
4135 } | |
4136 else | |
4137 { if (o1.base == null) | |
4138 o1.base = regp; | |
4139 else | |
4140 asmerr(EM_bad_operand); | |
4141 } | |
4142 break; | |
4143 } | |
4144 // If floating point instruction and id is a floating register | |
4145 else if (asmstate.ucItype == ITfloat && | |
4146 asm_is_fpreg(asmtok.ident.toChars())) | |
4147 { | |
4148 asm_token(); | |
4149 if (tok_value == TOKlparen) | |
4150 { | |
4151 uint n; | |
4152 | |
4153 asm_token(); | |
4154 asm_chktok(TOKint32v, EM_num); | |
4155 n = cast(uint)asmtok.uns64value; | |
4156 if (n > 7) | |
4157 asmerr(EM_bad_operand); | |
4158 o1.base = &(aregFp[n]); | |
4159 asm_chktok(TOKrparen, EM_rpar); | |
4160 } | |
4161 else | |
4162 o1.base = ®Fp; | |
4163 } | |
4164 else | |
4165 { | |
4166 if (asmstate.ucItype == ITjump) | |
4167 { | |
4168 s = null; | |
4169 if (asmstate.sc.func.labtab) | |
4170 s = asmstate.sc.func.labtab.lookup(asmtok.ident); | |
4171 if (!s) | |
4172 s = asmstate.sc.search(Loc(0), asmtok.ident, &scopesym); | |
4173 if (!s) | |
4174 { // Assume it is a label, and define that label | |
4175 s = asmstate.sc.func.searchLabel(asmtok.ident); | |
4176 } | |
4177 } | |
4178 else | |
4179 s = asmstate.sc.search(Loc(0), asmtok.ident, &scopesym); | |
4180 | |
4181 if (!s) | |
4182 asmerr(EM_undefined, asmtok.toChars()); | |
4183 | |
4184 Identifier id = asmtok.ident; | |
4185 asm_token(); | |
4186 if (tok_value == TOKdot) | |
4187 { | |
4188 Expression e; | |
4189 VarExp v; | |
4190 | |
4191 e = new IdentifierExp(asmstate.loc, id); | |
4192 while (1) | |
4193 { | |
4194 asm_token(); | |
4195 if (tok_value == TOKidentifier) | |
4196 { | |
4197 e = new DotIdExp(asmstate.loc, e, asmtok.ident); | |
4198 asm_token(); | |
4199 if (tok_value != TOKdot) | |
4200 break; | |
4201 } | |
4202 else | |
4203 { | |
4204 asmerr(EM_ident_exp); | |
4205 break; | |
4206 } | |
4207 } | |
4208 e = e.semantic(asmstate.sc); | |
4209 e = e.optimize(WANTvalue | WANTinterpret); | |
4210 if (e.isConst()) | |
4211 { | |
4212 if (e.type.isintegral()) | |
4213 { | |
4214 o1.disp = cast(int)e.toInteger(); | |
4215 goto Lpost; | |
4216 } | |
4217 else if (e.type.isreal()) | |
4218 { | |
4219 o1.real_ = e.toReal(); | |
4220 o1.ptype = e.type; | |
4221 goto Lpost; | |
4222 } | |
4223 else | |
4224 { | |
4225 asmerr(EM_bad_op, e.toChars()); | |
4226 } | |
4227 } | |
4228 else if (e.op == TOKvar) | |
4229 { | |
4230 v = cast(VarExp)e; | |
4231 s = v.var; | |
4232 } | |
4233 else | |
4234 { | |
4235 asmerr(EM_bad_op, e.toChars()); | |
4236 } | |
4237 } | |
4238 | |
4239 asm_merge_symbol(o1,s); | |
4240 | |
4241 /* This attempts to answer the question: is | |
4242 * char[8] foo; | |
4243 * of size 1 or size 8? Presume it is 8 if foo | |
4244 * is the last token of the operand. | |
4245 */ | |
4246 if (o1.ptype && tok_value != TOKcomma && tok_value != TOKeof) | |
4247 { | |
4248 for (; | |
4249 o1.ptype.ty == Tsarray; | |
4250 o1.ptype = o1.ptype.nextOf()) | |
4251 { | |
4252 ; | |
4253 } | |
4254 } | |
4255 | |
4256 Lpost: | |
4257 static if (false) { | |
4258 // for [] | |
4259 if (tok_value == TOKlbracket) | |
4260 o1 = asm_prim_post(o1); | |
4261 } | |
4262 goto Lret; | |
4263 } | |
4264 break; | |
4265 | |
4266 case TOKint32v: | |
4267 case TOKuns32v: | |
4268 o1 = opnd_calloc(); | |
4269 o1.disp = asmtok.int32value; | |
4270 asm_token(); | |
4271 break; | |
4272 | |
4273 case TOKfloat32v: | |
4274 o1 = opnd_calloc(); | |
4275 o1.real_ = asmtok.float80value; | |
4276 o1.ptype = Type.tfloat32; | |
4277 asm_token(); | |
4278 break; | |
4279 | |
4280 case TOKfloat64v: | |
4281 o1 = opnd_calloc(); | |
4282 o1.real_ = asmtok.float80value; | |
4283 o1.ptype = Type.tfloat64; | |
4284 asm_token(); | |
4285 break; | |
4286 | |
4287 case TOKfloat80v: | |
4288 o1 = opnd_calloc(); | |
4289 o1.real_ = asmtok.float80value; | |
4290 o1.ptype = Type.tfloat80; | |
4291 asm_token(); | |
4292 break; | |
4293 | |
4294 case ASMTKlocalsize: | |
4295 o1 = opnd_calloc(); | |
4296 o1.s = asmstate.psLocalsize; | |
4297 o1.ptype = Type.tint32; | |
4298 asm_token(); | |
4299 break; | |
4300 | |
4301 default: | |
4302 break; /// | |
4303 } | |
4304 Lret: | |
4305 return o1; | |
4306 } | |
4307 | |
4308 void asm_merge_symbol(OPND* o1, Dsymbol s) | |
4309 { | |
4310 Type ptype; | |
4311 VarDeclaration v; | |
4312 EnumMember em; | |
4313 | |
4314 //printf("asm_merge_symbol(s = %s %s)\n", s.kind(), s.toChars()); | |
4315 s = s.toAlias(); | |
4316 //printf("s = %s %s\n", s.kind(), s.toChars()); | |
4317 if (s.isLabel()) | |
4318 { | |
4319 o1.s = s; | |
4320 return; | |
4321 } | |
4322 | |
4323 v = s.isVarDeclaration(); | |
4324 if (v) | |
4325 { | |
4326 if (v.isParameter()) | |
4327 asmstate.statement.refparam = true; | |
4328 | |
4329 v.checkNestedReference(asmstate.sc, asmstate.loc); | |
4330 static if (false) { | |
4331 if (!v.isDataseg() && v.parent != asmstate.sc.parent && v.parent) | |
4332 { | |
4333 asmerr(EM_uplevel, v.toChars()); | |
4334 } | |
4335 } | |
4336 if (v.storage_class & STCfield) | |
4337 { | |
4338 o1.disp += v.offset; | |
4339 goto L2; | |
4340 } | |
4341 if ((v.isConst() | |
4342 ///version (DMDV2) { | |
4343 || v.isInvariant() || v.storage_class & STCmanifest | |
4344 ///} | |
4345 ) && !v.type.isfloating() && v.init) | |
4346 { | |
4347 ExpInitializer ei = v.init.isExpInitializer(); | |
4348 | |
4349 if (ei) | |
4350 { | |
4351 o1.disp = cast(int)ei.exp.toInteger(); | |
4352 return; | |
4353 } | |
4354 } | |
4355 } | |
4356 em = s.isEnumMember(); | |
4357 if (em) | |
4358 { | |
4359 o1.disp = cast(int)em.value.toInteger(); | |
4360 return; | |
4361 } | |
4362 o1.s = s; // a C identifier | |
4363 L2: | |
4364 Declaration d = s.isDeclaration(); | |
4365 if (!d) | |
4366 { | |
4367 asmerr("%s %s is not a declaration", s.kind(), s.toChars()); | |
4368 } | |
4369 else if (d.getType()) | |
4370 asmerr(EM_type_as_operand, d.getType().toChars()); | |
4371 else if (d.isTupleDeclaration()) { | |
4372 ; | |
4373 } else | |
4374 o1.ptype = d.type.toBasetype(); | |
4375 } | |
4376 | |
4377 REG[63] regtab = | |
4378 [ | |
4379 {"AL", _AL, _r8 | _al,}, | |
4380 {"AH", _AH, _r8,}, | |
4381 {"AX", _AX, _r16 | _ax,}, | |
4382 {"EAX", _EAX, _r32 | _eax,}, | |
4383 {"BL", _BL, _r8,}, | |
4384 {"BH", _BH, _r8,}, | |
4385 {"BX", _BX, _r16,}, | |
4386 {"EBX", _EBX, _r32,}, | |
4387 {"CL", _CL, _r8 | _cl,}, | |
4388 {"CH", _CH, _r8,}, | |
4389 {"CX", _CX, _r16,}, | |
4390 {"ECX", _ECX, _r32,}, | |
4391 {"DL", _DL, _r8,}, | |
4392 {"DH", _DH, _r8,}, | |
4393 {"DX", _DX, _r16 | _dx,}, | |
4394 {"EDX", _EDX, _r32,}, | |
4395 {"BP", _BP, _r16,}, | |
4396 {"EBP", _EBP, _r32,}, | |
4397 {"SP", _SP, _r16,}, | |
4398 {"ESP", _ESP, _r32,}, | |
4399 {"DI", _DI, _r16,}, | |
4400 {"EDI", _EDI, _r32,}, | |
4401 {"SI", _SI, _r16,}, | |
4402 {"ESI", _ESI, _r32,}, | |
4403 {"ES", _ES, _seg | _es,}, | |
4404 {"CS", _CS, _seg | _cs,}, | |
4405 {"SS", _SS, _seg | _ss,}, | |
4406 {"DS", _DS, _seg | _ds,}, | |
4407 {"GS", _GS, _seg | _gs,}, | |
4408 {"FS", _FS, _seg | _fs,}, | |
4409 {"CR0", 0, _special | _crn,}, | |
4410 {"CR2", 2, _special | _crn,}, | |
4411 {"CR3", 3, _special | _crn,}, | |
4412 {"CR4", 4, _special | _crn,}, | |
4413 {"DR0", 0, _special | _drn,}, | |
4414 {"DR1", 1, _special | _drn,}, | |
4415 {"DR2", 2, _special | _drn,}, | |
4416 {"DR3", 3, _special | _drn,}, | |
4417 {"DR4", 4, _special | _drn,}, | |
4418 {"DR5", 5, _special | _drn,}, | |
4419 {"DR6", 6, _special | _drn,}, | |
4420 {"DR7", 7, _special | _drn,}, | |
4421 {"TR3", 3, _special | _trn,}, | |
4422 {"TR4", 4, _special | _trn,}, | |
4423 {"TR5", 5, _special | _trn,}, | |
4424 {"TR6", 6, _special | _trn,}, | |
4425 {"TR7", 7, _special | _trn,}, | |
4426 {"MM0", 0, _mm,}, | |
4427 {"MM1", 1, _mm,}, | |
4428 {"MM2", 2, _mm,}, | |
4429 {"MM3", 3, _mm,}, | |
4430 {"MM4", 4, _mm,}, | |
4431 {"MM5", 5, _mm,}, | |
4432 {"MM6", 6, _mm,}, | |
4433 {"MM7", 7, _mm,}, | |
4434 {"XMM0", 0, _xmm,}, | |
4435 {"XMM1", 1, _xmm,}, | |
4436 {"XMM2", 2, _xmm,}, | |
4437 {"XMM3", 3, _xmm,}, | |
4438 {"XMM4", 4, _xmm,}, | |
4439 {"XMM5", 5, _xmm,}, | |
4440 {"XMM6", 6, _xmm,}, | |
4441 {"XMM7", 7, _xmm,}, | |
4442 ]; | |
4443 | |
4444 REG* asm_reg_lookup(string s) | |
4445 { | |
4446 //dbg_printf("asm_reg_lookup('%s')\n",s); | |
4447 | |
4448 for (int i = 0; i < regtab.length; i++) | |
4449 { | |
54 | 4450 if (regtab[i].regstr == s) |
0 | 4451 { |
4452 return ®tab[i]; | |
4453 } | |
4454 } | |
4455 | |
4456 return null; | |
4457 } | |
4458 | |
4459 int asm_is_fpreg(string szReg) | |
4460 { | |
4461 static if (true) { | |
4462 return(szReg.length == 2 && szReg[0] == 'S' && | |
4463 szReg[1] == 'T'); | |
4464 } else { | |
4465 return(szReg.length == 2 && (szReg[0] == 's' || szReg[0] == 'S') && | |
4466 (szReg[1] == 't' || szReg[1] == 'T')); | |
4467 } | |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
4468 } |