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