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