Mercurial > projects > ddmd
annotate dmd/backend/iasm.d @ 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.
author | Robert Clipsham <robert@octarineparrot.com> |
---|---|
date | Sun, 04 Apr 2010 02:06:32 +0100 |
parents | 63623152e82a |
children | 1628b221808d |
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 { | |
1038 asm_make_modrm_byte( | |
1039 ///debug { | |
1040 auchOpcode, &usIdx, | |
1041 ///} | |
1042 pc, | |
1043 ptb.pptb1.usFlags, | |
1044 popnd1, null); | |
1045 } | |
1046 | |
1047 popndTmp = popnd1; | |
1048 aoptyTmp = aoptyTable1; | |
1049 uSizemaskTmp = cast(ushort)uSizemaskTable1; | |
1050 L1: | |
1051 if (aoptyTmp == ASM_OPERAND_TYPE._imm) | |
1052 { | |
1053 Declaration d = popndTmp.s ? popndTmp.s.isDeclaration() : null; | |
1054 if (popndTmp.bSeg) | |
1055 { | |
1056 if (!(d && d.isDataseg())) | |
1057 asmerr(ASMERRMSGS.EM_bad_addr_mode); // illegal addressing mode | |
1058 } | |
1059 switch (uSizemaskTmp) | |
1060 { | |
1061 case _8: | |
1062 case _16: | |
1063 case _32: | |
1064 if (popndTmp.s is asmstate.psLocalsize) | |
1065 { | |
1066 pc.IFL2 = FL.FLlocalsize; | |
1067 pc.IEVdsym2() = null; | |
1068 pc.Iflags |= CF.CFoff; | |
1069 pc.IEVoffset2() = popndTmp.disp; | |
1070 } | |
1071 else if (d) | |
1072 { | |
1073 static if (false) { | |
1074 if ((pc.IFL2 = d.Sfl) == 0) | |
1075 pc.IFL2 = FL.FLdsymbol; | |
1076 } else { | |
1077 pc.IFL2 = FL.FLdsymbol; | |
1078 } | |
1079 pc.Iflags &= ~(CF.CFseg | CF.CFoff); | |
1080 if (popndTmp.bSeg) | |
1081 pc.Iflags |= CF.CFseg; | |
1082 else | |
1083 pc.Iflags |= CF.CFoff; | |
1084 | |
1085 pc.IEVoffset2() = popndTmp.disp; | |
1086 pc.IEVdsym2() = d; | |
1087 } | |
1088 else | |
1089 { | |
1090 pc.IEVint2() = popndTmp.disp; | |
1091 pc.IFL2 = FL.FLconst; | |
1092 } | |
1093 break; | |
1094 } | |
1095 } | |
1096 | |
1097 break; | |
1098 case 2: | |
1099 // | |
1100 // If there are two immediate operands then | |
1101 // | |
1102 if (aoptyTable1 == ASM_OPERAND_TYPE._imm && aoptyTable2 == ASM_OPERAND_TYPE._imm) | |
1103 { | |
1104 pc.IEVint1() = popnd1.disp; | |
1105 pc.IFL1 = FL.FLconst; | |
1106 pc.IEVint2() = popnd2.disp; | |
1107 pc.IFL2 = FL.FLconst; | |
1108 break; | |
1109 } | |
1110 if (aoptyTable2 == ASM_OPERAND_TYPE._m || | |
1111 aoptyTable2 == ASM_OPERAND_TYPE._rel || | |
1112 // If not MMX register (_mm) or XMM register (_xmm) | |
1113 (amodTable1 == ASM_MODIFIERS._rspecial && !(uRegmaskTable1 & (0x08 | 0x10)) && !uSizemaskTable1) || | |
1114 aoptyTable2 == ASM_OPERAND_TYPE._rm || | |
1115 (popnd1.usFlags == _r32 && popnd2.usFlags == _xmm) || | |
1116 (popnd1.usFlags == _r32 && popnd2.usFlags == _mm)) | |
1117 { | |
1118 static if (false) { | |
1119 printf("test4 %d,%d,%d,%d\n", | |
1120 (aoptyTable2 == ASM_OPERAND_TYPE._m), | |
1121 (aoptyTable2 == ASM_OPERAND_TYPE._rel), | |
1122 (amodTable1 == ASM_MODIFIERS._rspecial && !(uRegmaskTable1 & (0x08 | 0x10))), | |
1123 (aoptyTable2 == ASM_OPERAND_TYPE._rm) | |
1124 ); | |
1125 printf("usOpcode = %x\n", usOpcode); | |
1126 } | |
1127 if (ptb.pptb0.usOpcode == 0x0F7E || // MOVD _rm32,_mm | |
1128 ptb.pptb0.usOpcode == 0x660F7E // MOVD _rm32,_xmm | |
1129 ) | |
1130 { | |
1131 asm_make_modrm_byte( | |
1132 ///debug { | |
1133 auchOpcode, &usIdx, | |
1134 ///} | |
1135 pc, | |
1136 ptb.pptb1.usFlags, | |
1137 popnd1, popnd2); | |
1138 } | |
1139 else | |
1140 { | |
1141 asm_make_modrm_byte( | |
1142 ///debug { | |
1143 auchOpcode, &usIdx, | |
1144 ///} | |
1145 pc, | |
1146 ptb.pptb1.usFlags, | |
1147 popnd2, popnd1); | |
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 { | |
1191 asm_make_modrm_byte( | |
1192 ///debug { | |
1193 auchOpcode, &usIdx, | |
1194 ///} | |
1195 pc, | |
1196 ptb.pptb1.usFlags, | |
1197 popnd2, popnd1); | |
1198 } | |
1199 else | |
1200 { | |
1201 asm_make_modrm_byte( | |
1202 ///debug { | |
1203 auchOpcode, &usIdx, | |
1204 ///} | |
1205 pc, | |
1206 ptb.pptb1.usFlags, | |
1207 popnd1, popnd2); | |
1208 | |
1209 } | |
1210 if (aoptyTable1 == ASM_OPERAND_TYPE._imm) | |
1211 { | |
1212 popndTmp = popnd1; | |
1213 aoptyTmp = aoptyTable1; | |
1214 uSizemaskTmp = cast(ushort)uSizemaskTable1; | |
1215 } | |
1216 else | |
1217 { | |
1218 popndTmp = popnd2; | |
1219 aoptyTmp = aoptyTable2; | |
1220 uSizemaskTmp = cast(ushort)uSizemaskTable2; | |
1221 } | |
1222 } | |
1223 goto L1; | |
1224 | |
1225 case 3: | |
1226 if (aoptyTable2 == ASM_OPERAND_TYPE._m || aoptyTable2 == ASM_OPERAND_TYPE._rm || | |
1227 usOpcode == 0x0FC5) // PEXTRW | |
1228 { | |
1229 asm_make_modrm_byte( | |
1230 ///debug { | |
1231 auchOpcode, &usIdx, | |
1232 ///} | |
1233 pc, | |
1234 ptb.pptb1.usFlags, | |
1235 popnd2, popnd1); | |
1236 popndTmp = popnd3; | |
1237 aoptyTmp = aoptyTable3; | |
1238 uSizemaskTmp = cast(ushort)uSizemaskTable3; | |
1239 } else { | |
1240 if (((aoptyTable1 == ASM_OPERAND_TYPE._reg || aoptyTable1 == ASM_OPERAND_TYPE._float) && | |
1241 amodTable1 == ASM_MODIFIERS._normal && | |
1242 (uRegmaskTable1 &_rplus_r))) | |
1243 { | |
1244 if (asmstate.ucItype == IT.ITfloat) | |
1245 pc.Irm += popnd1.base.val; | |
1246 else if (pc.Iop == 0x0f) | |
1247 pc.Iop2 += popnd1.base.val; | |
1248 else | |
1249 pc.Iop += popnd1.base.val; | |
1250 debug { | |
1251 auchOpcode[usIdx-1] += popnd1.base.val; | |
1252 } | |
1253 } | |
1254 else if (((aoptyTable2 == ASM_OPERAND_TYPE._reg || aoptyTable2 == ASM_OPERAND_TYPE._float) && | |
1255 amodTable2 == ASM_MODIFIERS._normal && | |
1256 (uRegmaskTable2 &_rplus_r))) | |
1257 { | |
1258 if (asmstate.ucItype == IT.ITfloat) | |
1259 pc.Irm += popnd1.base.val; | |
1260 else if (pc.Iop == 0x0f) | |
1261 pc.Iop2 += popnd1.base.val; | |
1262 else | |
1263 pc.Iop += popnd2.base.val; | |
1264 debug { | |
1265 auchOpcode[usIdx-1] += popnd2.base.val; | |
1266 } | |
1267 } | |
1268 else | |
1269 asm_make_modrm_byte( | |
1270 ///debug { | |
1271 auchOpcode, &usIdx, | |
1272 ///} | |
1273 pc, | |
1274 ptb.pptb1.usFlags, | |
1275 popnd1, popnd2); | |
1276 | |
1277 popndTmp = popnd3; | |
1278 aoptyTmp = aoptyTable3; | |
1279 uSizemaskTmp = cast(ushort)uSizemaskTable3; | |
1280 } | |
1281 goto L1; | |
1282 } | |
1283 L2: | |
1284 | |
1285 if ((pc.Iop & 0xF8) == 0xD8 && | |
1286 ADDFWAIT() && | |
1287 !(ptb.pptb0.usFlags & _nfwait)) | |
1288 pc.Iflags |= CF.CFwait; | |
1289 else if ((ptb.pptb0.usFlags & _fwait) && | |
1290 config.target_cpu >= TARGET.TARGET_80386) | |
1291 pc.Iflags |= CF.CFwait; | |
1292 | |
1293 debug { | |
1294 if (debuga) | |
1295 { | |
1296 uint u; | |
1297 | |
1298 for (u = 0; u < usIdx; u++) | |
1299 printf(" %02X", auchOpcode[u]); | |
1300 | |
1301 printf("\t%s\t", asm_opstr(pop)); | |
1302 if (popnd1) | |
1303 asm_output_popnd(popnd1); | |
1304 if (popnd2) { | |
1305 printf(","); | |
1306 asm_output_popnd(popnd2); | |
1307 } | |
1308 if (popnd3) { | |
1309 printf(","); | |
1310 asm_output_popnd(popnd3); | |
1311 } | |
1312 printf("\n"); | |
1313 } | |
1314 } | |
1315 | |
1316 pc = cat(pcPrefix, pc); | |
1317 pc = asm_genloc(loc, pc); | |
1318 return pc; | |
1319 } | |
1320 | |
1321 void asm_token_trans(Token* tok) | |
1322 { | |
1323 tok_value = TOK.TOKeof; | |
1324 | |
1325 if (tok) | |
1326 { | |
1327 tok_value = tok.value; | |
1328 if (tok_value == TOK.TOKidentifier) | |
1329 { | |
1330 string id = tok.ident.toChars(); | |
1331 size_t len = id.length; | |
1332 if (len < 20) | |
1333 { | |
1334 ASMTK asmtk = cast(ASMTK) binary(toStringz(id), apszAsmtk.ptr, ASMTK.ASMTKmax); | |
1335 if (cast(int)asmtk >= 0) | |
1336 tok_value = cast(TOK)(asmtk + TOK.TOKMAX + 1); | |
1337 } | |
1338 } | |
1339 } | |
1340 } | |
1341 | |
1342 void asm_token() | |
1343 { | |
1344 if (asmtok) | |
1345 asmtok = asmtok.next; | |
1346 | |
1347 asm_token_trans(asmtok); | |
1348 } | |
1349 | |
1350 | |
1351 /********************** | |
1352 * If c is a power of 2, return that power else -1. | |
1353 */ | |
1354 | |
1355 int ispow2(ulong c) | |
1356 { | |
1357 int i; | |
1358 | |
1359 if (c == 0 || (c & (c - 1))) | |
1360 i = -1; | |
1361 else | |
1362 for (i = 0; c >>= 1; i++) { | |
1363 ; | |
1364 } | |
1365 | |
1366 return i; | |
1367 } | |
1368 | |
1369 // Error numbers | |
1370 enum ASMERRMSGS | |
1371 { | |
1372 EM_bad_float_op, | |
1373 EM_bad_addr_mode, | |
1374 EM_align, | |
1375 EM_opcode_exp, | |
1376 EM_prefix, | |
1377 EM_eol, | |
1378 EM_bad_operand, | |
1379 EM_bad_integral_operand, | |
1380 EM_ident_exp, | |
1381 EM_not_struct, | |
1382 EM_nops_expected, | |
1383 EM_bad_op, | |
1384 EM_const_init, | |
1385 EM_undefined, | |
1386 EM_pointer, | |
1387 EM_colon, | |
1388 EM_rbra, | |
1389 EM_rpar, | |
1390 EM_ptr_exp, | |
1391 EM_num, | |
1392 EM_float, | |
1393 EM_char, | |
1394 EM_label_expected, | |
1395 EM_uplevel, | |
1396 EM_type_as_operand, | |
1397 } | |
1398 | |
1399 mixin(BringToCurrentScope!(ASMERRMSGS)); | |
1400 | |
1401 string[] asmerrmsgs = | |
1402 [ | |
1403 "unknown operand for floating point instruction", | |
1404 "bad addr mode", | |
1405 "align %d must be a power of 2", | |
1406 "opcode expected, not %s", | |
1407 "prefix", | |
1408 "end of instruction", | |
1409 "bad operand", | |
1410 "bad integral operand", | |
1411 "identifier expected", | |
1412 "not struct", | |
1413 "nops expected", | |
1414 "bad type/size of operands '%s'", | |
1415 "constant initializer expected", | |
1416 "undefined identifier '%s'", | |
1417 "pointer", | |
1418 "colon", | |
1419 "] expected instead of '%s'", | |
1420 ") expected instead of '%s'", | |
1421 "ptr expected", | |
1422 "integer expected", | |
1423 "floating point expected", | |
1424 "character is truncated", | |
1425 "label expected", | |
1426 "uplevel nested reference to variable %s", | |
1427 "cannot use type %s as an operand" | |
1428 ]; | |
1429 | |
1430 void asmerr(T...)(int errnum, T t) | |
1431 { | |
1432 string format = asmerrmsgs[errnum]; | |
1433 asmerr(format, t); | |
1434 } | |
1435 | |
1436 void asmerr(T...)(string format, T t) | |
1437 { | |
1438 string p = asmstate.loc.toChars(); | |
1439 if (p.length != 0) | |
1440 writef("%s: ", p); | |
1441 | |
1442 writefln(format, t); | |
1443 | |
1444 longjmp(asmstate.env,1); | |
1445 } | |
1446 | |
1447 PTRNTAB asm_classify(OP* pop, OPND* popnd1, OPND* popnd2, OPND* popnd3, uint* pusNumops) | |
1448 { | |
1449 uint usNumops; | |
1450 uint usActual; | |
1451 PTRNTAB ptbRet = { null }; | |
1452 opflag_t usFlags1 = 0 ; | |
1453 opflag_t usFlags2 = 0; | |
1454 opflag_t usFlags3 = 0; | |
1455 | |
1456 PTRNTAB1 *pptb1; | |
1457 PTRNTAB2 *pptb2; | |
1458 PTRNTAB3 *pptb3; | |
1459 | |
1460 char bFake = false; | |
1461 | |
1462 ubyte bMatch1, bMatch2, bMatch3, bRetry = false; | |
1463 | |
1464 // How many arguments are there? the parser is strictly left to right | |
1465 // so this should work. | |
1466 | |
1467 if (!popnd1) | |
1468 usNumops = 0; | |
1469 else | |
1470 { | |
1471 popnd1.usFlags = usFlags1 = asm_determine_operand_flags(popnd1); | |
1472 if (!popnd2) | |
1473 usNumops = 1; | |
1474 else | |
1475 { | |
1476 popnd2.usFlags = usFlags2 = asm_determine_operand_flags(popnd2); | |
1477 if (!popnd3) | |
1478 usNumops = 2; | |
1479 else | |
1480 { | |
1481 popnd3.usFlags = usFlags3 = asm_determine_operand_flags(popnd3); | |
1482 usNumops = 3; | |
1483 } | |
1484 } | |
1485 } | |
1486 | |
1487 // Now check to insure that the number of operands is correct | |
1488 usActual = (pop.usNumops & IT.ITSIZE); | |
1489 if (usActual != usNumops && asmstate.ucItype != IT.ITopt && | |
1490 asmstate.ucItype != IT.ITfloat) | |
1491 { | |
1492 PARAM_ERROR: | |
1493 asmerr(ASMERRMSGS.EM_nops_expected, usActual, asm_opstr(pop), usNumops); | |
1494 } | |
1495 *pusNumops = usNumops; | |
1496 // | |
1497 // The number of arguments matches, now check to find the opcode | |
1498 // in the associated opcode table | |
1499 // | |
1500 RETRY: | |
1501 //printf("usActual = %d\n", usActual); | |
1502 switch (usActual) | |
1503 { | |
1504 case 0: | |
1505 ptbRet = pop.ptb ; | |
1506 goto RETURN_IT; | |
1507 | |
1508 case 1: | |
1509 //printf("usFlags1 = "); asm_output_flags(usFlags1); printf("\n"); | |
1510 for (pptb1 = pop.ptb.pptb1; pptb1.usOpcode != ASM_END; | |
1511 pptb1++) | |
1512 { | |
1513 //printf("table = "); asm_output_flags(pptb1.usOp1); printf("\n"); | |
1514 bMatch1 = asm_match_flags(usFlags1, pptb1.usOp1); | |
1515 if (bMatch1) | |
1516 { if (pptb1.usOpcode == 0x68 && | |
1517 I32 && | |
1518 pptb1.usOp1 == _imm16 | |
1519 ) | |
1520 // Don't match PUSH imm16 in 32 bit code | |
1521 continue; | |
1522 break; | |
1523 } | |
1524 if ((asmstate.ucItype == IT.ITimmed) && | |
1525 asm_match_flags(usFlags1, | |
1526 CONSTRUCT_FLAGS(_8 | _16 | _32, ASM_OPERAND_TYPE._imm, ASM_MODIFIERS._normal, | |
1527 0)) && | |
1528 popnd1.disp == pptb1.usFlags) | |
1529 break; | |
1530 if ((asmstate.ucItype == IT.ITopt || | |
1531 asmstate.ucItype == IT.ITfloat) && | |
1532 !usNumops && | |
1533 !pptb1.usOp1) | |
1534 { | |
1535 if (usNumops > 1) | |
1536 goto PARAM_ERROR; | |
1537 break; | |
1538 } | |
1539 } | |
1540 if (pptb1.usOpcode == ASM_END) | |
1541 { | |
1542 debug { | |
1543 if (debuga) | |
1544 { | |
1545 printf("\t%s\t", asm_opstr(pop)); | |
1546 if (popnd1) | |
1547 asm_output_popnd(popnd1); | |
1548 if (popnd2) { | |
1549 printf(","); | |
1550 asm_output_popnd(popnd2); | |
1551 } | |
1552 if (popnd3) { | |
1553 printf(","); | |
1554 asm_output_popnd(popnd3); | |
1555 } | |
1556 printf("\n"); | |
1557 | |
1558 printf("OPCODE mism = "); | |
1559 if (popnd1) | |
1560 asm_output_flags(popnd1.usFlags); | |
1561 else | |
1562 printf("NONE"); | |
1563 printf("\n"); | |
1564 } | |
1565 } | |
1566 TYPE_SIZE_ERROR: | |
1567 if (popnd1 && ASM_GET_aopty(popnd1.usFlags) != ASM_OPERAND_TYPE._reg) | |
1568 { | |
1569 usFlags1 = popnd1.usFlags |= _anysize; | |
1570 if (asmstate.ucItype == IT.ITjump) | |
1571 { | |
1572 if (bRetry && popnd1.s && !popnd1.s.isLabel()) | |
1573 { | |
1574 asmerr(ASMERRMSGS.EM_label_expected, popnd1.s.toChars()); | |
1575 } | |
1576 | |
1577 popnd1.usFlags |= CONSTRUCT_FLAGS(0, 0, 0, _fanysize); | |
1578 } | |
1579 } | |
1580 if (popnd2 && ASM_GET_aopty(popnd2.usFlags) != ASM_OPERAND_TYPE._reg) { | |
1581 usFlags2 = popnd2.usFlags |= (_anysize); | |
1582 if (asmstate.ucItype == IT.ITjump) | |
1583 popnd2.usFlags |= CONSTRUCT_FLAGS(0, 0, 0, | |
1584 _fanysize); | |
1585 } | |
1586 if (popnd3 && ASM_GET_aopty(popnd3.usFlags) != ASM_OPERAND_TYPE._reg) { | |
1587 usFlags3 = popnd3.usFlags |= (_anysize); | |
1588 if (asmstate.ucItype == IT.ITjump) | |
1589 popnd3.usFlags |= CONSTRUCT_FLAGS(0, 0, 0, | |
1590 _fanysize); | |
1591 } | |
1592 if (bRetry) | |
1593 { | |
1594 asmerr(ASMERRMSGS.EM_bad_op, fromStringz(asm_opstr(pop))); // illegal type/size of operands | |
1595 } | |
1596 bRetry = true; | |
1597 goto RETRY; | |
1598 } | |
1599 ptbRet.pptb1 = pptb1; | |
1600 goto RETURN_IT; | |
1601 | |
1602 case 2: | |
1603 //printf("usFlags1 = "); asm_output_flags(usFlags1); printf(" "); | |
1604 //printf("usFlags2 = "); asm_output_flags(usFlags2); printf("\n"); | |
1605 for (pptb2 = pop.ptb.pptb2; | |
1606 pptb2.usOpcode != ASM_END; | |
1607 pptb2++) | |
1608 { | |
1609 //printf("table1 = "); asm_output_flags(pptb2.usOp1); printf(" "); | |
1610 //printf("table2 = "); asm_output_flags(pptb2.usOp2); printf("\n"); | |
1611 bMatch1 = asm_match_flags(usFlags1, pptb2.usOp1); | |
1612 bMatch2 = asm_match_flags(usFlags2, pptb2.usOp2); | |
1613 //printf("match1 = %d, match2 = %d\n",bMatch1,bMatch2); | |
1614 if (bMatch1 && bMatch2) { | |
1615 | |
1616 //printf("match\n"); | |
1617 | |
1618 // OK, if they both match and the first op in the table is not AL | |
1619 // or size of 8 and the second is immediate 8, | |
1620 // then check to see if the constant | |
1621 // is a signed 8 bit constant. If so, then do not match, otherwise match | |
1622 // | |
1623 if (!bRetry && | |
1624 !((ASM_GET_uSizemask(pptb2.usOp1) & _8) || | |
1625 (ASM_GET_uRegmask(pptb2.usOp1) & _al)) && | |
1626 (ASM_GET_aopty(pptb2.usOp2) == ASM_OPERAND_TYPE._imm) && | |
1627 (ASM_GET_uSizemask(pptb2.usOp2) & _8)) | |
1628 { | |
1629 if (popnd2.disp <= char.max) | |
1630 break; | |
1631 else | |
1632 bFake = true; | |
1633 } | |
1634 else | |
1635 break; | |
1636 } | |
1637 if (asmstate.ucItype == IT.ITopt || asmstate.ucItype == IT.ITfloat) | |
1638 { | |
1639 switch (usNumops) | |
1640 { | |
1641 case 0: | |
1642 if (!pptb2.usOp1) | |
1643 goto Lfound2; | |
1644 break; | |
1645 case 1: | |
1646 if (bMatch1 && !pptb2.usOp2) | |
1647 goto Lfound2; | |
1648 break; | |
1649 case 2: | |
1650 break; | |
1651 default: | |
1652 goto PARAM_ERROR; | |
1653 } | |
1654 } | |
1655 static if (false) { | |
1656 if (asmstate.ucItype == IT.ITshift && | |
1657 !pptb2.usOp2 && | |
1658 bMatch1 && popnd2.disp == 1 && | |
1659 asm_match_flags(usFlags2, | |
1660 CONSTRUCT_FLAGS(_8|_16|_32, ASM_OPERAND_TYPE._imm,ASM_MODIFIERS._normal,0)) | |
1661 ) | |
1662 { | |
1663 break; | |
1664 } | |
1665 } | |
1666 } | |
1667 Lfound2: | |
1668 if (pptb2.usOpcode == ASM_END) | |
1669 { | |
1670 debug { | |
1671 if (debuga) | |
1672 { | |
1673 printf("\t%s\t", asm_opstr(pop)); | |
1674 if (popnd1) | |
1675 asm_output_popnd(popnd1); | |
1676 if (popnd2) { | |
1677 printf(","); | |
1678 asm_output_popnd(popnd2); | |
1679 } | |
1680 if (popnd3) { | |
1681 printf(","); | |
1682 asm_output_popnd(popnd3); | |
1683 } | |
1684 printf("\n"); | |
1685 | |
1686 printf("OPCODE mismatch = "); | |
1687 if (popnd1) | |
1688 asm_output_flags(popnd1.usFlags); | |
1689 else | |
1690 printf("NONE"); | |
1691 printf( " Op2 = "); | |
1692 if (popnd2) | |
1693 asm_output_flags(popnd2.usFlags); | |
1694 else | |
1695 printf("NONE"); | |
1696 printf("\n"); | |
1697 } | |
1698 } | |
1699 goto TYPE_SIZE_ERROR; | |
1700 } | |
1701 ptbRet.pptb2 = pptb2; | |
1702 goto RETURN_IT; | |
1703 | |
1704 case 3: | |
1705 for (pptb3 = pop.ptb.pptb3; | |
1706 pptb3.usOpcode != ASM_END; | |
1707 pptb3++) | |
1708 { | |
1709 bMatch1 = asm_match_flags(usFlags1, pptb3.usOp1); | |
1710 bMatch2 = asm_match_flags(usFlags2, pptb3.usOp2); | |
1711 bMatch3 = asm_match_flags(usFlags3, pptb3.usOp3); | |
1712 if (bMatch1 && bMatch2 && bMatch3) | |
1713 goto Lfound3; | |
1714 | |
1715 if (asmstate.ucItype == IT.ITopt) | |
1716 { | |
1717 switch (usNumops) | |
1718 { | |
1719 case 0: | |
1720 if (!pptb3.usOp1) | |
1721 goto Lfound3; | |
1722 break; | |
1723 case 1: | |
1724 if (bMatch1 && !pptb3.usOp2) | |
1725 goto Lfound3; | |
1726 break; | |
1727 case 2: | |
1728 if (bMatch1 && bMatch2 && !pptb3.usOp3) | |
1729 goto Lfound3; | |
1730 break; | |
1731 case 3: | |
1732 break; | |
1733 default: | |
1734 goto PARAM_ERROR; | |
1735 } | |
1736 } | |
1737 } | |
1738 Lfound3: | |
1739 if (pptb3.usOpcode == ASM_END) | |
1740 { | |
1741 debug { | |
1742 if (debuga) | |
1743 { | |
1744 printf("\t%s\t", asm_opstr(pop)); | |
1745 if (popnd1) | |
1746 asm_output_popnd(popnd1); | |
1747 if (popnd2) { | |
1748 printf(","); | |
1749 asm_output_popnd(popnd2); | |
1750 } | |
1751 if (popnd3) { | |
1752 printf(","); | |
1753 asm_output_popnd(popnd3); | |
1754 } | |
1755 printf("\n"); | |
1756 | |
1757 printf("OPCODE mismatch = "); | |
1758 if (popnd1) | |
1759 asm_output_flags(popnd1.usFlags); | |
1760 else | |
1761 printf("NONE"); | |
1762 printf( " Op2 = "); | |
1763 if (popnd2) | |
1764 asm_output_flags(popnd2.usFlags); | |
1765 else | |
1766 printf("NONE"); | |
1767 if (popnd3) | |
1768 asm_output_flags(popnd3.usFlags); | |
1769 printf("\n"); | |
1770 } | |
1771 } | |
1772 goto TYPE_SIZE_ERROR; | |
1773 } | |
1774 | |
1775 ptbRet.pptb3 = pptb3; | |
1776 goto RETURN_IT; | |
1777 } | |
1778 | |
1779 RETURN_IT: | |
1780 if (bRetry && !bFake) | |
1781 { | |
1782 asmerr(ASMERRMSGS.EM_bad_op, fromStringz(asm_opstr(pop))); | |
1783 } | |
1784 return ptbRet; | |
1785 } | |
1786 | |
1787 /******************************* | |
1788 * start of inline assemblers expression parser | |
1789 * NOTE: functions in call order instead of alphabetical | |
1790 */ | |
1791 | |
1792 /******************************************* | |
1793 * Parse DA expression | |
1794 * | |
1795 * Very limited define address to place a code | |
1796 * address in the assembly | |
1797 * Problems: | |
1798 * o Should use dw offset and dd offset instead, | |
1799 * for near/far support. | |
1800 * o Should be able to add an offset to the label address. | |
1801 * o Blocks addressed by DA should get their Bpred set correctly | |
1802 * for optimizer. | |
1803 */ | |
1804 | |
1805 code* asm_da_parse(OP* pop) | |
1806 { | |
1807 code* clst = null; | |
1808 elem* e; | |
1809 | |
1810 while (1) | |
1811 { | |
1812 code* c; | |
1813 | |
1814 if (tok_value == TOK.TOKidentifier) | |
1815 { | |
1816 LabelDsymbol label = asmstate.sc.func.searchLabel(asmtok.ident); | |
1817 if (!label) | |
1818 error(asmstate.loc, "label '%s' not found\n", asmtok.ident.toChars()); | |
1819 | |
1820 c = code_calloc(); | |
1821 c.Iop = ASM; | |
1822 c.Iflags = CF.CFaddrsize; | |
1823 c.IFL1 = FL.FLblockoff; | |
1824 c.IEVlsym1() = label; | |
1825 c = asm_genloc(asmstate.loc, c); | |
1826 clst = cat(clst,c); | |
1827 } | |
1828 else | |
1829 asmerr(ASMERRMSGS.EM_bad_addr_mode); // illegal addressing mode | |
1830 | |
1831 asm_token(); | |
1832 if (tok_value != TOK.TOKcomma) | |
1833 break; | |
1834 | |
1835 asm_token(); | |
1836 } | |
1837 | |
1838 asmstate.statement.regs |= mES | ALLREGS; | |
1839 asmstate.bReturnax = true; | |
1840 | |
1841 return clst; | |
1842 } | |
1843 | |
1844 /******************************************* | |
1845 * Parse DB, DW, DD, DQ and DT expressions. | |
1846 */ | |
1847 | |
1848 code* asm_db_parse(OP* pop) | |
1849 { | |
1850 uint usSize; | |
1851 uint usMaxbytes; | |
1852 uint usBytes; | |
1853 | |
1854 union DT | |
1855 { | |
1856 targ_ullong ul; | |
1857 targ_float f; | |
1858 targ_double d; | |
1859 targ_ldouble ld; | |
1860 char value[10]; | |
1861 } | |
1862 | |
1863 DT dt; | |
1864 | |
1865 code* c; | |
1866 uint op; | |
1867 static ubyte[7] opsize = [ 1,2,4,8,4,8,10 ]; | |
1868 | |
1869 op = pop.usNumops & IT.ITSIZE; | |
1870 usSize = opsize[op]; | |
1871 | |
1872 usBytes = 0; | |
1873 usMaxbytes = 0; | |
1874 c = code_calloc(); | |
1875 c.Iop = ASM; | |
1876 | |
1877 while (1) | |
1878 { | |
1879 size_t len; | |
1880 ubyte* q; | |
1881 | |
1882 if (usBytes+usSize > usMaxbytes) | |
1883 { | |
1884 usMaxbytes = usBytes + usSize + 10; | |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
1885 c.IEV1.as.bytes = cast(char*)realloc(c.IEV1.as.bytes,usMaxbytes); |
0 | 1886 } |
1887 switch (tok_value) | |
1888 { | |
1889 case TOK.TOKint32v: | |
1890 dt.ul = asmtok.int32value; | |
1891 goto L1; | |
1892 case TOK.TOKuns32v: | |
1893 dt.ul = asmtok.uns32value; | |
1894 goto L1; | |
1895 case TOK.TOKint64v: | |
1896 dt.ul = asmtok.int64value; | |
1897 goto L1; | |
1898 case TOK.TOKuns64v: | |
1899 dt.ul = asmtok.uns64value; | |
1900 goto L1; | |
1901 L1: | |
1902 switch (op) | |
1903 { | |
1904 case OP_DB.OPdb: | |
1905 case OP_DB.OPds: | |
1906 case OP_DB.OPdi: | |
1907 case OP_DB.OPdl: | |
1908 break; | |
1909 default: | |
1910 asmerr(ASMERRMSGS.EM_float); | |
1911 } | |
1912 goto L2; | |
1913 | |
1914 case TOK.TOKfloat32v: | |
1915 case TOK.TOKfloat64v: | |
1916 case TOK.TOKfloat80v: | |
1917 switch (op) | |
1918 { | |
1919 case OP_DB.OPdf: | |
1920 dt.f = asmtok.float80value; | |
1921 break; | |
1922 case OP_DB.OPdd: | |
1923 dt.d = asmtok.float80value; | |
1924 break; | |
1925 case OP_DB.OPde: | |
1926 dt.ld = asmtok.float80value; | |
1927 break; | |
1928 default: | |
1929 asmerr(ASMERRMSGS.EM_num); | |
1930 } | |
1931 goto L2; | |
1932 | |
1933 L2: | |
1934 memcpy(c.IEV1.as.bytes + usBytes,&dt,usSize); | |
1935 usBytes += usSize; | |
1936 break; | |
1937 | |
1938 case TOK.TOKstring: | |
1939 len = asmtok.len; | |
1940 q = cast(ubyte*)asmtok.ustring; | |
1941 L3: | |
1942 if (len) | |
1943 { | |
1944 usMaxbytes += len * usSize; | |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
1945 c.IEV1.as.bytes = cast(char*)realloc(c.IEV1.as.bytes,usMaxbytes); |
0 | 1946 memcpy(c.IEV1.as.bytes + usBytes,asmtok.ustring,len); |
1947 | |
1948 char* p = c.IEV1.as.bytes + usBytes; | |
1949 for (size_t i = 0; i < len; i++) | |
1950 { | |
1951 // Be careful that this works | |
1952 memset(p, 0, usSize); | |
1953 switch (op) | |
1954 { | |
1955 case OP_DB.OPdb: | |
1956 *p = cast(ubyte)*q; | |
1957 if (*p != *q) | |
1958 asmerr(ASMERRMSGS.EM_char); | |
1959 break; | |
1960 | |
1961 case OP_DB.OPds: | |
1962 *cast(short*)p = *cast(ubyte*)q; | |
1963 if (*cast(short*)p != *q) | |
1964 asmerr(ASMERRMSGS.EM_char); | |
1965 break; | |
1966 | |
1967 case OP_DB.OPdi: | |
1968 case OP_DB.OPdl: | |
1969 *cast(int*)p = *q; | |
1970 break; | |
1971 | |
1972 default: | |
1973 asmerr(ASMERRMSGS.EM_float); | |
1974 } | |
1975 q++; | |
1976 p += usSize; | |
1977 } | |
1978 | |
1979 usBytes += len * usSize; | |
1980 } | |
1981 break; | |
1982 | |
1983 case TOK.TOKidentifier: | |
1984 { | |
1985 Expression e = new IdentifierExp(asmstate.loc, asmtok.ident); | |
1986 e = e.semantic(asmstate.sc); | |
1987 e = e.optimize(WANT.WANTvalue | WANT.WANTinterpret); | |
1988 if (e.op == TOK.TOKint64) | |
1989 { | |
1990 dt.ul = e.toInteger(); | |
1991 goto L2; | |
1992 } | |
1993 else if (e.op == TOK.TOKfloat64) | |
1994 { | |
1995 switch (op) | |
1996 { | |
1997 case OP_DB.OPdf: | |
1998 dt.f = e.toReal(); | |
1999 break; | |
2000 case OP_DB.OPdd: | |
2001 dt.d = e.toReal(); | |
2002 break; | |
2003 case OP_DB.OPde: | |
2004 dt.ld = e.toReal(); | |
2005 break; | |
2006 default: | |
2007 asmerr(ASMERRMSGS.EM_num); | |
2008 } | |
2009 goto L2; | |
2010 } | |
2011 else if (e.op == TOK.TOKstring) | |
2012 { | |
2013 StringExp se = cast(StringExp)e; | |
2014 q = cast(ubyte*)se.string_; | |
2015 len = se.len; | |
2016 goto L3; | |
2017 } | |
2018 goto Ldefault; | |
2019 } | |
2020 | |
2021 default: | |
2022 Ldefault: | |
2023 asmerr(ASMERRMSGS.EM_const_init); // constant initializer | |
2024 break; | |
2025 } | |
2026 c.IEV1.as.len = usBytes; | |
2027 | |
2028 asm_token(); | |
2029 if (tok_value != TOK.TOKcomma) | |
2030 break; | |
2031 | |
2032 asm_token(); | |
2033 } | |
2034 | |
2035 c = asm_genloc(asmstate.loc, c); | |
2036 | |
2037 asmstate.statement.regs |= /* mES| */ ALLREGS; | |
2038 asmstate.bReturnax = true; | |
2039 | |
2040 return c; | |
2041 } | |
2042 | |
2043 /********************************** | |
2044 * Parse and get integer expression. | |
2045 */ | |
2046 | |
2047 int asm_getnum() | |
2048 { | |
2049 int v; | |
2050 long i; | |
2051 | |
2052 switch (tok_value) | |
2053 { | |
2054 case TOK.TOKint32v: | |
2055 v = asmtok.int32value; | |
2056 break; | |
2057 | |
2058 case TOK.TOKuns32v: | |
2059 v = asmtok.uns32value; | |
2060 break; | |
2061 | |
2062 case TOK.TOKidentifier: | |
2063 Expression e = new IdentifierExp(asmstate.loc, asmtok.ident); | |
2064 e = e.semantic(asmstate.sc); | |
2065 e = e.optimize(WANT.WANTvalue | WANT.WANTinterpret); | |
2066 i = e.toInteger(); | |
2067 v = cast(int) i; | |
2068 if (v != i) | |
2069 asmerr(ASMERRMSGS.EM_num); | |
2070 break; | |
2071 | |
2072 default: | |
2073 asmerr(ASMERRMSGS.EM_num); | |
2074 break; | |
2075 } | |
2076 | |
2077 asm_token(); | |
2078 | |
2079 return v; | |
2080 } | |
2081 | |
2082 | |
2083 /******************************* | |
2084 */ | |
2085 | |
2086 OPND* asm_cond_exp() | |
2087 { | |
2088 OPND* o1; | |
2089 OPND* o2; | |
2090 OPND* o3; | |
2091 | |
2092 //printf("asm_cond_exp()\n"); | |
2093 o1 = asm_log_or_exp(); | |
2094 if (tok_value == TOK.TOKquestion) | |
2095 { | |
2096 asm_token(); | |
2097 o2 = asm_cond_exp(); | |
2098 asm_token(); | |
2099 asm_chktok(TOK.TOKcolon, ASMERRMSGS.EM_colon); | |
2100 o3 = asm_cond_exp(); | |
2101 o1 = (o1.disp) ? o2 : o3; | |
2102 } | |
2103 | |
2104 return o1; | |
2105 } | |
2106 | |
2107 regm_t asm_modify_regs(PTRNTAB ptb, OPND* popnd1, OPND* popnd2) | |
2108 { | |
2109 regm_t usRet = 0; | |
2110 | |
2111 switch (ptb.pptb0.usFlags & MOD_MASK) { | |
2112 case _modsi: | |
2113 usRet |= mSI; | |
2114 break; | |
2115 case _moddx: | |
2116 usRet |= mDX; | |
2117 break; | |
2118 case _mod2: | |
2119 if (popnd2) | |
2120 usRet |= asm_modify_regs(ptb, popnd2, null); | |
2121 break; | |
2122 case _modax: | |
2123 usRet |= mAX; | |
2124 break; | |
2125 case _modnot1: | |
2126 popnd1 = null; | |
2127 break; | |
2128 case _modaxdx: | |
2129 usRet |= (mAX | mDX); | |
2130 break; | |
2131 case _moddi: | |
2132 usRet |= mDI; | |
2133 break; | |
2134 case _modsidi: | |
2135 usRet |= (mSI | mDI); | |
2136 break; | |
2137 case _modcx: | |
2138 usRet |= mCX; | |
2139 break; | |
2140 case _modes: | |
2141 /*usRet |= mES;*/ | |
2142 break; | |
2143 case _modall: | |
2144 asmstate.bReturnax = true; | |
2145 return /*mES |*/ ALLREGS; | |
2146 case _modsiax: | |
2147 usRet |= (mSI | mAX); | |
2148 break; | |
2149 case _modsinot1: | |
2150 usRet |= mSI; | |
2151 popnd1 = null; | |
2152 break; | |
2153 default: | |
2154 break; /// | |
2155 } | |
2156 if (popnd1 && ASM_GET_aopty(popnd1.usFlags) == ASM_OPERAND_TYPE._reg) { | |
2157 switch (ASM_GET_amod(popnd1.usFlags)) { | |
2158 default: | |
2159 if (ASM_GET_uSizemask(popnd1.usFlags) == _8) { | |
2160 switch(popnd1.base.val) { | |
2161 case _AL: | |
2162 case _AH: | |
2163 usRet |= mAX; | |
2164 break; | |
2165 case _BL: | |
2166 case _BH: | |
2167 usRet |= mBX; | |
2168 break; | |
2169 case _CL: | |
2170 case _CH: | |
2171 usRet |= mCX; | |
2172 break; | |
2173 case _DL: | |
2174 case _DH: | |
2175 usRet |= mDX; | |
2176 break; | |
2177 } | |
2178 } | |
2179 else { | |
2180 switch (popnd1.base.val) { | |
2181 case _AX: | |
2182 usRet |= mAX; | |
2183 break; | |
2184 case _BX: | |
2185 usRet |= mBX; | |
2186 break; | |
2187 case _CX: | |
2188 usRet |= mCX; | |
2189 break; | |
2190 case _DX: | |
2191 usRet |= mDX; | |
2192 break; | |
2193 case _SI: | |
2194 usRet |= mSI; | |
2195 break; | |
2196 case _DI: | |
2197 usRet |= mDI; | |
2198 break; | |
2199 default: | |
2200 break; /// | |
2201 } | |
2202 } | |
2203 break; | |
2204 | |
2205 case ASM_MODIFIERS._rseg: | |
2206 //if (popnd1.base.val == _ES) | |
2207 //usRet |= mES; | |
2208 break; | |
2209 | |
2210 case ASM_MODIFIERS._rspecial: | |
2211 break; | |
2212 } | |
2213 } | |
2214 if (usRet & mAX) | |
2215 asmstate.bReturnax = true; | |
2216 | |
2217 return usRet; | |
2218 } | |
2219 | |
2220 | |
2221 /**************************** | |
2222 * Fill in the modregrm and sib bytes of code. | |
2223 */ | |
2224 | |
2225 uint X(ubyte r1, ubyte r2) { | |
2226 return (((r1) * 16) + (r2)); | |
2227 } | |
2228 | |
2229 uint Y(ubyte r1) { | |
2230 return X(r1, 9); | |
2231 } | |
2232 | |
2233 void asm_make_modrm_byte( | |
2234 ///debug { | |
2235 ubyte[] puchOpcode, uint* pusIdx, | |
2236 ///} | |
2237 code *pc, | |
2238 ushort usFlags, | |
2239 OPND *popnd, OPND *popnd2) | |
2240 { | |
2241 /// #undef modregrm | |
2242 | |
2243 union MODRM_BYTE // mrmb | |
2244 { | |
2245 struct MODRM | |
2246 { | |
2247 mixin(bitfields!( | |
2248 uint, "rm", 3, | |
2249 uint, "reg", 3, | |
2250 uint, "mod", 2)); | |
2251 } | |
2252 | |
2253 ubyte uchOpcode; | |
2254 MODRM modregrm; | |
2255 } | |
2256 | |
2257 union SIB_BYTE | |
2258 { | |
2259 struct SIB | |
2260 { | |
2261 | |
2262 mixin(bitfields!( | |
2263 uint, "base", 3, | |
2264 uint, "index", 3, | |
2265 uint, "ss", 2)); | |
2266 } | |
2267 | |
2268 ubyte uchOpcode; | |
2269 SIB sib; | |
2270 } | |
2271 | |
2272 | |
2273 MODRM_BYTE mrmb; | |
2274 SIB_BYTE sib; | |
2275 char bSib = false; | |
2276 char bDisp = false; | |
2277 char b32bit = false; | |
2278 ubyte* puc; | |
2279 char bModset = false; | |
2280 Dsymbol s; | |
2281 | |
2282 uint uSizemask = 0; | |
2283 ASM_OPERAND_TYPE aopty; | |
2284 ASM_MODIFIERS amod; | |
2285 ushort uRegmask; | |
2286 ubyte bOffsetsym = false; | |
2287 | |
2288 static if (false) { | |
2289 printf("asm_make_modrm_byte(usFlags = x%x)\n", usFlags); | |
2290 printf("op1: "); | |
2291 asm_output_flags(popnd.usFlags); | |
2292 if (popnd2) | |
2293 { | |
2294 printf(" op2: "); | |
2295 asm_output_flags(popnd2.usFlags); | |
2296 } | |
2297 printf("\n"); | |
2298 } | |
2299 | |
2300 uSizemask = ASM_GET_uSizemask(popnd.usFlags); | |
2301 aopty = ASM_GET_aopty(popnd.usFlags); | |
2302 amod = ASM_GET_amod(popnd.usFlags); | |
2303 uRegmask = ASM_GET_uRegmask(popnd.usFlags); | |
2304 s = popnd.s; | |
2305 if (s) | |
2306 { | |
2307 Declaration d = s.isDeclaration(); | |
2308 | |
2309 if (amod == ASM_MODIFIERS._fn16 && aopty == ASM_OPERAND_TYPE._rel && popnd2) | |
2310 { | |
2311 aopty = ASM_OPERAND_TYPE._m; | |
2312 goto L1; | |
2313 } | |
2314 | |
2315 if (amod == ASM_MODIFIERS._fn16 || amod == ASM_MODIFIERS._fn32) | |
2316 { | |
2317 pc.Iflags |= CF.CFoff; | |
2318 debug { | |
2319 puchOpcode[(*pusIdx)++] = 0; | |
2320 puchOpcode[(*pusIdx)++] = 0; | |
2321 } | |
2322 if (aopty == ASM_OPERAND_TYPE._m || aopty == ASM_OPERAND_TYPE._mnoi) | |
2323 { | |
2324 pc.IFL1 = FL.FLdata; | |
2325 pc.IEVdsym1() = d; | |
2326 pc.IEVoffset1() = 0; | |
2327 } | |
2328 else | |
2329 { | |
2330 if (aopty == ASM_OPERAND_TYPE._p) | |
2331 pc.Iflags |= CF.CFseg; | |
2332 | |
2333 debug if (aopty == ASM_OPERAND_TYPE._p || aopty == ASM_OPERAND_TYPE._rel) | |
2334 { | |
2335 puchOpcode[(*pusIdx)++] = 0; | |
2336 puchOpcode[(*pusIdx)++] = 0; | |
2337 } | |
2338 | |
2339 pc.IFL2 = FL.FLfunc; | |
2340 pc.IEVdsym2() = d; | |
2341 pc.IEVoffset2() = 0; | |
2342 //return; | |
2343 } | |
2344 } | |
2345 else | |
2346 { | |
2347 L1: | |
2348 LabelDsymbol label = s.isLabel(); | |
2349 if (label) | |
2350 { | |
2351 if (s == asmstate.psDollar) | |
2352 { | |
2353 pc.IFL1 = FL.FLconst; | |
2354 if (uSizemask & (_8 | _16)) | |
2355 pc.IEVint1() = popnd.disp; | |
2356 else if (uSizemask & _32) | |
2357 pc.IEVpointer1() = cast(targ_size_t) popnd.disp; | |
2358 } | |
2359 else | |
2360 { | |
2361 pc.IFL1 = FL.FLblockoff; | |
2362 pc.IEVlsym1() = label; | |
2363 } | |
2364 } | |
2365 else if (s == asmstate.psLocalsize) | |
2366 { | |
2367 pc.IFL1 = FL.FLlocalsize; | |
2368 pc.IEVdsym1() = null; | |
2369 pc.Iflags |= CF.CFoff; | |
2370 pc.IEVoffset1() = popnd.disp; | |
2371 } | |
2372 else if (s.isFuncDeclaration()) | |
2373 { | |
2374 pc.IFL1 = FL.FLfunc; | |
2375 pc.IEVdsym1() = d; | |
2376 pc.IEVoffset1() = popnd.disp; | |
2377 } | |
2378 else | |
2379 { | |
2380 debug if (debuga) | |
2381 printf("Setting up symbol %s\n", d.ident.toChars()); | |
2382 | |
2383 pc.IFL1 = FL.FLdsymbol; | |
2384 pc.IEVdsym1() = d; | |
2385 pc.Iflags |= CF.CFoff; | |
2386 pc.IEVoffset1() = popnd.disp; | |
2387 } | |
2388 } | |
2389 } | |
2390 | |
2391 mrmb.modregrm.reg = usFlags & NUM_MASK; | |
2392 | |
2393 if (s && (aopty == ASM_OPERAND_TYPE._m || aopty == ASM_OPERAND_TYPE._mnoi) && !s.isLabel()) | |
2394 { | |
2395 if (s == asmstate.psLocalsize) | |
2396 { | |
2397 DATA_REF: | |
2398 mrmb.modregrm.rm = BPRM; | |
2399 if (amod == ASM_MODIFIERS._addr16 || amod == ASM_MODIFIERS._addr32) | |
2400 mrmb.modregrm.mod = 0x2; | |
2401 else | |
2402 mrmb.modregrm.mod = 0x0; | |
2403 } | |
2404 else | |
2405 { | |
2406 Declaration d = s.isDeclaration(); | |
2407 assert(d); | |
2408 if (d.isDataseg() || d.isCodeseg()) | |
2409 { | |
2410 if (( I32 && amod == ASM_MODIFIERS._addr16) || | |
2411 (!I32 && amod == ASM_MODIFIERS._addr32)) | |
2412 asmerr(ASMERRMSGS.EM_bad_addr_mode); // illegal addressing mode | |
2413 goto DATA_REF; | |
2414 } | |
2415 mrmb.modregrm.rm = BPRM; | |
2416 mrmb.modregrm.mod = 0x2; | |
2417 } | |
2418 } | |
2419 | |
2420 if (aopty == ASM_OPERAND_TYPE._reg || amod == ASM_MODIFIERS._rspecial) { | |
2421 mrmb.modregrm.mod = 0x3; | |
2422 mrmb.modregrm.rm = mrmb.modregrm.rm | popnd.base.val; | |
2423 } | |
2424 else if (amod == ASM_MODIFIERS._addr16 || (amod == ASM_MODIFIERS._flbl && !I32)) | |
2425 { | |
2426 uint rm; | |
2427 | |
2428 debug if (debuga) | |
2429 printf("This is an ADDR16\n"); | |
2430 | |
2431 if (!popnd.pregDisp1) | |
2432 { rm = 0x6; | |
2433 if (!s) | |
2434 bDisp = true; | |
2435 } | |
2436 else | |
2437 { | |
2438 uint r1r2; | |
2439 | |
2440 if (popnd.pregDisp2) | |
2441 r1r2 = X(popnd.pregDisp1.val,popnd.pregDisp2.val); | |
2442 else | |
2443 r1r2 = Y(popnd.pregDisp1.val); | |
2444 | |
2445 switch (r1r2) | |
2446 { | |
2447 case X(_BX,_SI): rm = 0; break; | |
2448 case X(_BX,_DI): rm = 1; break; | |
2449 case Y(_BX): rm = 7; break; | |
2450 | |
2451 case X(_BP,_SI): rm = 2; break; | |
2452 case X(_BP,_DI): rm = 3; break; | |
2453 case Y(_BP): rm = 6; bDisp = true; break; | |
2454 | |
2455 case X(_SI,_BX): rm = 0; break; | |
2456 case X(_SI,_BP): rm = 2; break; | |
2457 case Y(_SI): rm = 4; break; | |
2458 | |
2459 case X(_DI,_BX): rm = 1; break; | |
2460 case X(_DI,_BP): rm = 3; break; | |
2461 case Y(_DI): rm = 5; break; | |
2462 | |
2463 default: | |
2464 asmerr(ASMERRMSGS.EM_bad_addr_mode); // illegal addressing mode | |
2465 } | |
2466 } | |
2467 mrmb.modregrm.rm = rm; | |
2468 | |
2469 debug if (debuga) | |
2470 printf("This is an mod = %d, popnd.s =%ld, popnd.disp = %ld\n", | |
2471 mrmb.modregrm.mod, s, popnd.disp); | |
2472 | |
2473 if (!s || (!mrmb.modregrm.mod && popnd.disp)) | |
2474 { | |
2475 if ((!popnd.disp && !bDisp) || !popnd.pregDisp1) | |
2476 mrmb.modregrm.mod = 0x0; | |
2477 else if (popnd.disp >= CHAR_MIN && popnd.disp <= SCHAR_MAX) | |
2478 mrmb.modregrm.mod = 0x1; | |
2479 else | |
2480 mrmb.modregrm.mod = 0X2; | |
2481 } | |
2482 else | |
2483 bOffsetsym = true; | |
2484 | |
2485 } | |
2486 else if (amod == ASM_MODIFIERS._addr32 || (amod == ASM_MODIFIERS._flbl && I32)) | |
2487 { | |
2488 debug if (debuga) | |
2489 printf("This is an ADDR32\n"); | |
2490 | |
2491 if (!popnd.pregDisp1) | |
2492 mrmb.modregrm.rm = 0x5; | |
2493 else if (popnd.pregDisp2 || | |
2494 popnd.uchMultiplier || | |
2495 popnd.pregDisp1.val == _ESP) | |
2496 { | |
2497 if (popnd.pregDisp2) | |
2498 { | |
2499 if (popnd.pregDisp2.val == _ESP) | |
2500 asmerr(ASMERRMSGS.EM_bad_addr_mode); // illegal addressing mode | |
2501 } | |
2502 else | |
2503 { | |
2504 if (popnd.uchMultiplier && popnd.pregDisp1.val ==_ESP) | |
2505 asmerr(ASMERRMSGS.EM_bad_addr_mode); // illegal addressing mode | |
2506 bDisp = true; | |
2507 } | |
2508 | |
2509 mrmb.modregrm.rm = 0x4; | |
2510 bSib = true; | |
2511 if (bDisp) | |
2512 { | |
2513 if (!popnd.uchMultiplier && | |
2514 popnd.pregDisp1.val==_ESP) | |
2515 { | |
2516 sib.sib.base = popnd.pregDisp1.val; | |
2517 sib.sib.index = 0x4; | |
2518 } | |
2519 else | |
2520 { | |
2521 debug if (debuga) | |
2522 printf("Resetting the mod to 0\n"); | |
2523 | |
2524 if (popnd.pregDisp2) | |
2525 { | |
2526 if (popnd.pregDisp2.val != _EBP) | |
2527 asmerr(ASMERRMSGS.EM_bad_addr_mode); // illegal addressing mode | |
2528 } | |
2529 else | |
2530 { | |
2531 mrmb.modregrm.mod = 0x0; | |
2532 bModset = true; | |
2533 } | |
2534 | |
2535 sib.sib.base = 0x5; | |
2536 sib.sib.index = popnd.pregDisp1.val; | |
2537 } | |
2538 } | |
2539 else | |
2540 { | |
2541 sib.sib.base = popnd.pregDisp1.val; | |
2542 // | |
2543 // This is to handle the special case | |
2544 // of using the EBP register and no | |
2545 // displacement. You must put in an | |
2546 // 8 byte displacement in order to | |
2547 // get the correct opcodes. | |
2548 // | |
2549 if (popnd.pregDisp1.val == _EBP && (!popnd.disp && !s)) | |
2550 { | |
2551 debug if (debuga) | |
2552 printf("Setting the mod to 1 in the _EBP case\n"); | |
2553 | |
2554 mrmb.modregrm.mod = 0x1; | |
2555 bDisp = true; // Need a | |
2556 // displacement | |
2557 bModset = true; | |
2558 } | |
2559 | |
2560 sib.sib.index = popnd.pregDisp2.val; | |
2561 } | |
2562 switch (popnd.uchMultiplier) | |
2563 { | |
2564 case 0: sib.sib.ss = 0; break; | |
2565 case 1: sib.sib.ss = 0; break; | |
2566 case 2: sib.sib.ss = 1; break; | |
2567 case 4: sib.sib.ss = 2; break; | |
2568 case 8: sib.sib.ss = 3; break; | |
2569 | |
2570 default: | |
2571 asmerr(ASMERRMSGS.EM_bad_addr_mode); // illegal addressing mode | |
2572 break; | |
2573 } | |
2574 if (bDisp && sib.sib.base == 0x5) | |
2575 b32bit = true; | |
2576 } | |
2577 else | |
2578 { | |
2579 uint rm; | |
2580 | |
2581 if (popnd.uchMultiplier) | |
2582 asmerr(ASMERRMSGS.EM_bad_addr_mode); // illegal addressing mode | |
2583 switch (popnd.pregDisp1.val) | |
2584 { | |
2585 case _EAX: rm = 0; break; | |
2586 case _ECX: rm = 1; break; | |
2587 case _EDX: rm = 2; break; | |
2588 case _EBX: rm = 3; break; | |
2589 case _ESI: rm = 6; break; | |
2590 case _EDI: rm = 7; break; | |
2591 | |
2592 case _EBP: | |
2593 if (!popnd.disp && !s) | |
2594 { | |
2595 mrmb.modregrm.mod = 0x1; | |
2596 bDisp = true; // Need a displacement | |
2597 bModset = true; | |
2598 } | |
2599 rm = 5; | |
2600 break; | |
2601 | |
2602 default: | |
2603 asmerr(ASMERRMSGS.EM_bad_addr_mode); // illegal addressing mode | |
2604 break; | |
2605 } | |
2606 mrmb.modregrm.rm = rm; | |
2607 } | |
2608 if (!bModset && (!s || (!mrmb.modregrm.mod && popnd.disp))) | |
2609 { | |
2610 if ((!popnd.disp && !mrmb.modregrm.mod) || | |
2611 (!popnd.pregDisp1 && !popnd.pregDisp2)) | |
2612 { | |
2613 mrmb.modregrm.mod = 0x0; | |
2614 bDisp = true; | |
2615 } | |
2616 else if (popnd.disp >= CHAR_MIN && popnd.disp <= SCHAR_MAX) | |
2617 mrmb.modregrm.mod = 0x1; | |
2618 else | |
2619 mrmb.modregrm.mod = 0x2; | |
2620 } | |
2621 else | |
2622 bOffsetsym = true; | |
2623 } | |
2624 if (popnd2 && !mrmb.modregrm.reg && | |
2625 asmstate.ucItype != IT.ITshift && | |
2626 (ASM_GET_aopty(popnd2.usFlags) == ASM_OPERAND_TYPE._reg || | |
2627 ASM_GET_amod(popnd2.usFlags) == ASM_MODIFIERS._rseg || | |
2628 ASM_GET_amod(popnd2.usFlags) == ASM_MODIFIERS._rspecial)) | |
2629 { | |
2630 mrmb.modregrm.reg = popnd2.base.val; | |
2631 } | |
2632 debug puchOpcode[ (*pusIdx)++ ] = mrmb.uchOpcode; | |
2633 | |
2634 pc.Irm = mrmb.uchOpcode; | |
2635 //printf("Irm = %02x\n", pc.Irm); | |
2636 if (bSib) | |
2637 { | |
2638 debug puchOpcode[ (*pusIdx)++ ] = sib.uchOpcode; | |
2639 pc.Isib= sib.uchOpcode; | |
2640 } | |
2641 if ((!s || (popnd.pregDisp1 && !bOffsetsym)) && | |
2642 aopty != ASM_OPERAND_TYPE._imm && | |
2643 (popnd.disp || bDisp)) | |
2644 { | |
2645 if (popnd.usFlags & _a16) | |
2646 { | |
2647 debug { | |
2648 puc = (cast(ubyte*) &(popnd.disp)); | |
2649 puchOpcode[(*pusIdx)++] = puc[1]; | |
2650 puchOpcode[(*pusIdx)++] = puc[0]; | |
2651 } | |
2652 if (usFlags & (_modrm | NUM_MASK)) { | |
2653 debug if (debuga) | |
2654 printf("Setting up value %ld\n", popnd.disp); | |
2655 | |
2656 pc.IEVint1() = popnd.disp; | |
2657 pc.IFL1 = FL.FLconst; | |
2658 } | |
2659 else { | |
2660 pc.IEVint2() = popnd.disp; | |
2661 pc.IFL2 = FL.FLconst; | |
2662 } | |
2663 | |
2664 } | |
2665 else | |
2666 { | |
2667 debug { | |
2668 puc = (cast(ubyte*) &(popnd.disp)); | |
2669 puchOpcode[(*pusIdx)++] = puc[3]; | |
2670 puchOpcode[(*pusIdx)++] = puc[2]; | |
2671 puchOpcode[(*pusIdx)++] = puc[1]; | |
2672 puchOpcode[(*pusIdx)++] = puc[0]; | |
2673 } | |
2674 if (usFlags & (_modrm | NUM_MASK)) { | |
2675 debug if (debuga) | |
2676 printf("Setting up value %ld\n", popnd.disp); | |
2677 | |
2678 pc.IEVpointer1() = cast(targ_size_t) popnd.disp; | |
2679 pc.IFL1 = FL.FLconst; | |
2680 } else { | |
2681 pc.IEVpointer2() = cast(targ_size_t) popnd.disp; | |
2682 pc.IFL2 = FL.FLconst; | |
2683 } | |
2684 } | |
2685 } | |
2686 } | |
2687 | |
2688 void asm_output_popnd(OPND* popnd) | |
2689 { | |
2690 if (popnd.segreg) | |
2691 printf("%s:", popnd.segreg.regstr); | |
2692 | |
2693 if (popnd.s) | |
2694 writef("%s", popnd.s.ident.toChars()); | |
2695 | |
2696 if (popnd.base) | |
2697 printf("%s", popnd.base.regstr); | |
2698 | |
2699 if (popnd.pregDisp1) { | |
2700 if (popnd.pregDisp2) { | |
2701 if (popnd.usFlags & _a32) | |
2702 if (popnd.uchMultiplier) | |
2703 printf("[%s][%s*%d]", | |
2704 popnd.pregDisp1.regstr, | |
2705 popnd.pregDisp2.regstr, | |
2706 popnd.uchMultiplier); | |
2707 else | |
2708 printf("[%s][%s]", | |
2709 popnd.pregDisp1.regstr, | |
2710 popnd.pregDisp2.regstr); | |
2711 else | |
2712 printf("[%s+%s]", | |
2713 popnd.pregDisp1.regstr, | |
2714 popnd.pregDisp2.regstr); | |
2715 } | |
2716 else { | |
2717 if (popnd.uchMultiplier) | |
2718 printf("[%s*%d]", | |
2719 popnd.pregDisp1.regstr, | |
2720 popnd.uchMultiplier); | |
2721 else | |
2722 printf("[%s]", | |
2723 popnd.pregDisp1.regstr); | |
2724 } | |
2725 } | |
2726 if (ASM_GET_aopty(popnd.usFlags) == ASM_OPERAND_TYPE._imm) | |
2727 printf("%lxh", popnd.disp); | |
2728 else if (popnd.disp) | |
2729 printf("+%lxh", popnd.disp); | |
2730 } | |
2731 | |
2732 /******************************* | |
2733 * Prepend line number to c. | |
2734 */ | |
2735 | |
2736 code* asm_genloc(Loc loc, code* c) | |
2737 { | |
2738 if (global.params.symdebug) | |
2739 { | |
2740 code* pcLin; | |
2741 Srcpos srcpos; | |
2742 | |
2743 srcpos.Slinnum = loc.linnum; | |
2744 srcpos.Sfilename = cast(char*)toStringz(loc.filename); | |
2745 pcLin = genlinnum(null, srcpos); | |
2746 | |
2747 c = cat(pcLin, c); | |
2748 } | |
2749 | |
2750 return c; | |
2751 } | |
2752 | |
2753 opflag_t asm_determine_operand_flags(OPND* popnd) | |
2754 { | |
2755 Dsymbol ps; | |
2756 int ty; | |
2757 opflag_t us; | |
2758 opflag_t sz; | |
2759 ASM_OPERAND_TYPE opty; | |
2760 ASM_MODIFIERS amod; | |
2761 | |
2762 // If specified 'offset' or 'segment' but no symbol | |
2763 if ((popnd.bOffset || popnd.bSeg) && !popnd.s) | |
2764 asmerr(ASMERRMSGS.EM_bad_addr_mode); // illegal addressing mode | |
2765 | |
2766 if (asmstate.ucItype == IT.ITfloat) | |
2767 return asm_determine_float_flags(popnd); | |
2768 | |
2769 // If just a register | |
2770 if (popnd.base && !popnd.s && !popnd.disp && !popnd.real_) | |
2771 return popnd.base.ty; | |
2772 | |
2773 debug if (debuga) | |
2774 printf("popnd.base = %s\n, popnd.pregDisp1 = %ld\n", popnd.base ? popnd.base.regstr : "NONE", popnd.pregDisp1); | |
2775 | |
2776 ps = popnd.s; | |
2777 Declaration ds = ps ? ps.isDeclaration() : null; | |
2778 if (ds && ds.storage_class & STC.STClazy) | |
2779 sz = _anysize; | |
2780 else | |
2781 sz = cast(ushort)asm_type_size((ds && ds.storage_class & (STC.STCout | STC.STCref)) ? popnd.ptype.pointerTo() : popnd.ptype); | |
2782 if (popnd.pregDisp1 && !popnd.base) | |
2783 { | |
2784 if (ps && ps.isLabel() && sz == _anysize) | |
2785 sz = I32 ? _32 : _16; | |
2786 return (popnd.pregDisp1.ty & _r32) | |
2787 ? CONSTRUCT_FLAGS(sz, ASM_OPERAND_TYPE._m, ASM_MODIFIERS._addr32, 0) | |
2788 : CONSTRUCT_FLAGS(sz, ASM_OPERAND_TYPE._m, ASM_MODIFIERS._addr16, 0); | |
2789 } | |
2790 else if (ps) | |
2791 { | |
2792 if (popnd.bOffset || popnd.bSeg || ps == asmstate.psLocalsize) | |
2793 return I32 | |
2794 ? CONSTRUCT_FLAGS(_32, ASM_OPERAND_TYPE._imm, ASM_MODIFIERS._normal, 0) | |
2795 : CONSTRUCT_FLAGS(_16, ASM_OPERAND_TYPE._imm, ASM_MODIFIERS._normal, 0); | |
2796 | |
2797 if (ps.isLabel()) | |
2798 { | |
2799 switch (popnd.ajt) | |
2800 { | |
2801 case ASM_JUMPTYPE.ASM_JUMPTYPE_UNSPECIFIED: | |
2802 if (ps == asmstate.psDollar) | |
2803 { | |
2804 if (popnd.disp >= CHAR_MIN && | |
2805 popnd.disp <= CHAR_MAX) | |
2806 us = CONSTRUCT_FLAGS(_8, _rel, ASM_MODIFIERS._flbl,0); | |
2807 else | |
2808 if (popnd.disp >= SHRT_MIN && | |
2809 popnd.disp <= SHRT_MIN) | |
2810 us = CONSTRUCT_FLAGS(_16, _rel, ASM_MODIFIERS._flbl,0); | |
2811 else | |
2812 us = CONSTRUCT_FLAGS(_32, _rel, ASM_MODIFIERS._flbl,0); | |
2813 } | |
2814 else if (asmstate.ucItype != ITjump) | |
2815 { | |
2816 if (sz == _8) | |
2817 { us = CONSTRUCT_FLAGS(_8,ASM_OPERAND_TYPE._rel,ASM_MODIFIERS._flbl,0); | |
2818 break; | |
2819 } | |
2820 goto case_near; | |
2821 } | |
2822 else | |
2823 us = I32 | |
2824 ? CONSTRUCT_FLAGS(_8|_32, ASM_OPERAND_TYPE._rel, ASM_MODIFIERS._flbl, 0) | |
2825 : CONSTRUCT_FLAGS(_8|_16, ASM_OPERAND_TYPE._rel, ASM_MODIFIERS._flbl, 0); | |
2826 break; | |
2827 | |
2828 case ASM_JUMPTYPE.ASM_JUMPTYPE_NEAR: | |
2829 case_near: | |
2830 us = I32 | |
2831 ? CONSTRUCT_FLAGS(_32, _rel, _flbl, 0) | |
2832 : CONSTRUCT_FLAGS(_16, _rel, _flbl, 0); | |
2833 break; | |
2834 case ASM_JUMPTYPE.ASM_JUMPTYPE_SHORT: | |
2835 us = CONSTRUCT_FLAGS(_8, _rel, _flbl, 0); | |
2836 break; | |
2837 case ASM_JUMPTYPE.ASM_JUMPTYPE_FAR: | |
2838 us = I32 | |
2839 ? CONSTRUCT_FLAGS(_48, _rel, _flbl, 0) | |
2840 : CONSTRUCT_FLAGS(_32, _rel, _flbl, 0); | |
2841 break; | |
2842 default: | |
2843 assert(0); | |
2844 } | |
2845 return us; | |
2846 } | |
2847 if (!popnd.ptype) | |
2848 return CONSTRUCT_FLAGS(sz, _m, _normal, 0); | |
2849 ty = popnd.ptype.ty; | |
2850 if (ty == Tpointer && popnd.ptype.nextOf().ty == Tfunction && | |
2851 !ps.isVarDeclaration()) | |
2852 { | |
2853 static if (true) { | |
2854 return CONSTRUCT_FLAGS(_32, _m, _fn16, 0); | |
2855 } else { | |
2856 ty = popnd.ptype.Tnext.Tty; | |
2857 if (tyfarfunc(tybasic(ty))) { | |
2858 return I32 | |
2859 ? CONSTRUCT_FLAGS(_48, _mnoi, _fn32, 0) | |
2860 : CONSTRUCT_FLAGS(_32, _mnoi, _fn32, 0); | |
2861 } | |
2862 else { | |
2863 return I32 | |
2864 ? CONSTRUCT_FLAGS(_32, _m, _fn16, 0) | |
2865 : CONSTRUCT_FLAGS(_16, _m, _fn16, 0); | |
2866 } | |
2867 } | |
2868 } | |
2869 else if (ty == TY.Tfunction) | |
2870 { | |
2871 static if (true) { | |
2872 return CONSTRUCT_FLAGS(_32, _rel, _fn16, 0); | |
2873 } else { | |
2874 if (tyfarfunc(tybasic(ty))) | |
2875 return I32 | |
2876 ? CONSTRUCT_FLAGS(_48, _p, _fn32, 0) | |
2877 : CONSTRUCT_FLAGS(_32, _p, _fn32, 0); | |
2878 else | |
2879 return I32 | |
2880 ? CONSTRUCT_FLAGS(_32, _rel, _fn16, 0) | |
2881 : CONSTRUCT_FLAGS(_16, _rel, _fn16, 0); | |
2882 } | |
2883 } | |
2884 else if (asmstate.ucItype == IT.ITjump) | |
2885 { | |
2886 amod = _normal; | |
2887 goto L1; | |
2888 } | |
2889 else | |
2890 return CONSTRUCT_FLAGS(sz, _m, _normal, 0); | |
2891 } | |
2892 if (popnd.segreg /*|| popnd.bPtr*/) | |
2893 { | |
2894 amod = I32 ? _addr32 : _addr16; | |
2895 if (asmstate.ucItype == ITjump) | |
2896 { | |
2897 L1: | |
2898 opty = _m; | |
2899 if (I32) | |
2900 { if (sz == _48) | |
2901 opty = _mnoi; | |
2902 } | |
2903 else | |
2904 { | |
2905 if (sz == _32) | |
2906 opty = _mnoi; | |
2907 } | |
2908 us = CONSTRUCT_FLAGS(sz,opty,amod,0); | |
2909 } | |
2910 else | |
2911 us = CONSTRUCT_FLAGS(sz, | |
2912 // _rel, amod, 0); | |
2913 _m, amod, 0); | |
2914 } | |
2915 | |
2916 else if (popnd.ptype) | |
2917 us = CONSTRUCT_FLAGS(sz, _imm, _normal, 0); | |
2918 | |
2919 else if (popnd.disp >= CHAR_MIN && popnd.disp <= UCHAR_MAX) | |
2920 us = CONSTRUCT_FLAGS(_8 | _16 | _32, _imm, _normal, 0); | |
2921 else if (popnd.disp >= SHRT_MIN && popnd.disp <= USHRT_MAX) | |
2922 us = CONSTRUCT_FLAGS(_16 | _32, _imm, _normal, 0); | |
2923 else | |
2924 us = CONSTRUCT_FLAGS(_32, _imm, _normal, 0); | |
2925 return us; | |
2926 } | |
2927 | |
2928 /******************************* | |
2929 * Match flags in operand against flags in opcode table. | |
2930 * Returns: | |
2931 * !=0 if match | |
2932 */ | |
2933 | |
2934 ubyte asm_match_flags(opflag_t usOp, opflag_t usTable) | |
2935 { | |
2936 ASM_OPERAND_TYPE aoptyTable; | |
2937 ASM_OPERAND_TYPE aoptyOp; | |
2938 ASM_MODIFIERS amodTable; | |
2939 ASM_MODIFIERS amodOp; | |
2940 uint uRegmaskTable; | |
2941 uint uRegmaskOp; | |
2942 ubyte bRegmatch; | |
2943 ubyte bRetval = false; | |
2944 uint uSizemaskOp; | |
2945 uint uSizemaskTable; | |
2946 ubyte bSizematch; | |
2947 | |
2948 //printf("asm_match_flags(usOp = x%x, usTable = x%x)\n", usOp, usTable); | |
2949 if (asmstate.ucItype == IT.ITfloat) | |
2950 { | |
2951 bRetval = asm_match_float_flags(usOp, usTable); | |
2952 goto EXIT; | |
2953 } | |
2954 | |
2955 uSizemaskOp = ASM_GET_uSizemask(usOp); | |
2956 uSizemaskTable = ASM_GET_uSizemask(usTable); | |
2957 | |
2958 // Check #1, if the sizes do not match, NO match | |
2959 bSizematch = (uSizemaskOp & uSizemaskTable) != 0; | |
2960 | |
2961 amodOp = ASM_GET_amod(usOp); | |
2962 | |
2963 aoptyTable = ASM_GET_aopty(usTable); | |
2964 aoptyOp = ASM_GET_aopty(usOp); | |
2965 | |
2966 // _mmm64 matches with a 64 bit mem or an MMX register | |
2967 if (usTable == _mmm64) | |
2968 { | |
2969 if (usOp == _mm) | |
2970 goto Lmatch; | |
2971 if (aoptyOp == _m && (bSizematch || uSizemaskOp == _anysize)) | |
2972 goto Lmatch; | |
2973 goto EXIT; | |
2974 } | |
2975 | |
2976 // _xmm_m32, _xmm_m64, _xmm_m128 match with XMM register or memory | |
2977 if (usTable == _xmm_m32 || | |
2978 usTable == _xmm_m64 || | |
2979 usTable == _xmm_m128) | |
2980 { | |
2981 if (usOp == _xmm) | |
2982 goto Lmatch; | |
2983 if (aoptyOp == _m && (bSizematch || uSizemaskOp == _anysize)) | |
2984 goto Lmatch; | |
2985 } | |
2986 | |
2987 if (!bSizematch && uSizemaskTable) | |
2988 { | |
2989 //printf("no size match\n"); | |
2990 goto EXIT; | |
2991 } | |
2992 | |
2993 | |
2994 // | |
2995 // The operand types must match, otherwise return false. | |
2996 // There is one exception for the _rm which is a table entry which matches | |
2997 // _reg or _m | |
2998 // | |
2999 if (aoptyTable != aoptyOp) | |
3000 { | |
3001 if (aoptyTable == _rm && (aoptyOp == _reg || | |
3002 aoptyOp == _m || | |
3003 aoptyOp == _rel)) | |
3004 goto Lok; | |
3005 | |
3006 if (aoptyTable == _mnoi && aoptyOp == _m && | |
3007 (uSizemaskOp == _32 && amodOp == _addr16 || | |
3008 uSizemaskOp == _48 && amodOp == _addr32 || | |
3009 uSizemaskOp == _48 && amodOp == _normal) | |
3010 ) | |
3011 goto Lok; | |
3012 goto EXIT; | |
3013 } | |
3014 Lok: | |
3015 | |
3016 // | |
3017 // Looks like a match so far, check to see if anything special is going on | |
3018 // | |
3019 amodTable = ASM_GET_amod(usTable); | |
3020 uRegmaskOp = ASM_GET_uRegmask(usOp); | |
3021 uRegmaskTable = ASM_GET_uRegmask(usTable); | |
3022 bRegmatch = ((!uRegmaskTable && !uRegmaskOp) || | |
3023 (uRegmaskTable & uRegmaskOp)); | |
3024 | |
3025 switch (amodTable) | |
3026 { | |
3027 case _normal: // Normal's match with normals | |
3028 switch(amodOp) { | |
3029 case _normal: | |
3030 case _addr16: | |
3031 case _addr32: | |
3032 case _fn16: | |
3033 case _fn32: | |
3034 case _flbl: | |
3035 bRetval = (bSizematch || bRegmatch); | |
3036 goto EXIT; | |
3037 default: | |
3038 goto EXIT; | |
3039 } | |
3040 case _rseg: | |
3041 case _rspecial: | |
3042 bRetval = (amodOp == amodTable && bRegmatch); | |
3043 goto EXIT; | |
3044 } | |
3045 EXIT: | |
3046 static if (false) { | |
3047 printf("OP : "); | |
3048 asm_output_flags(usOp); | |
3049 printf("\nTBL: "); | |
3050 asm_output_flags(usTable); | |
3051 writef(": %s\n", bRetval ? "MATCH" : "NOMATCH"); | |
3052 } | |
3053 return bRetval; | |
3054 | |
3055 Lmatch: | |
3056 //printf("match\n"); | |
3057 return 1; | |
3058 } | |
3059 | |
3060 void asm_output_flags(opflag_t usFlags) | |
3061 { | |
3062 ASM_OPERAND_TYPE aopty = ASM_GET_aopty(usFlags); | |
3063 ASM_MODIFIERS amod = ASM_GET_amod(usFlags); | |
3064 uint uRegmask = ASM_GET_uRegmask(usFlags); | |
3065 uint uSizemask = ASM_GET_uSizemask(usFlags); | |
3066 | |
3067 if (uSizemask == _anysize) | |
3068 printf("_anysize "); | |
3069 else if (uSizemask == 0) | |
3070 printf("0 "); | |
3071 else | |
3072 { | |
3073 if (uSizemask & _8) | |
3074 printf("_8 "); | |
3075 if (uSizemask & _16) | |
3076 printf("_16 "); | |
3077 if (uSizemask & _32) | |
3078 printf("_32 "); | |
3079 if (uSizemask & _48) | |
3080 printf("_48 "); | |
3081 } | |
3082 | |
3083 printf("_"); | |
3084 switch (aopty) { | |
3085 case ASM_OPERAND_TYPE._reg: | |
3086 printf("reg "); | |
3087 break; | |
3088 case ASM_OPERAND_TYPE._m: | |
3089 printf("m "); | |
3090 break; | |
3091 case ASM_OPERAND_TYPE._imm: | |
3092 printf("imm "); | |
3093 break; | |
3094 case ASM_OPERAND_TYPE._rel: | |
3095 printf("rel "); | |
3096 break; | |
3097 case ASM_OPERAND_TYPE._mnoi: | |
3098 printf("mnoi "); | |
3099 break; | |
3100 case ASM_OPERAND_TYPE._p: | |
3101 printf("p "); | |
3102 break; | |
3103 case ASM_OPERAND_TYPE._rm: | |
3104 printf("rm "); | |
3105 break; | |
3106 case ASM_OPERAND_TYPE._float: | |
3107 printf("float "); | |
3108 break; | |
3109 default: | |
3110 printf(" UNKNOWN "); | |
3111 } | |
3112 | |
3113 printf("_"); | |
3114 switch (amod) { | |
3115 case ASM_MODIFIERS._normal: | |
3116 printf("normal "); | |
3117 if (uRegmask & 1) printf("_al "); | |
3118 if (uRegmask & 2) printf("_ax "); | |
3119 if (uRegmask & 4) printf("_eax "); | |
3120 if (uRegmask & 8) printf("_dx "); | |
3121 if (uRegmask & 0x10) printf("_cl "); | |
3122 return; | |
3123 case ASM_MODIFIERS._rseg: | |
3124 printf("rseg "); | |
3125 break; | |
3126 case ASM_MODIFIERS._rspecial: | |
3127 printf("rspecial "); | |
3128 break; | |
3129 case ASM_MODIFIERS._addr16: | |
3130 printf("addr16 "); | |
3131 break; | |
3132 case ASM_MODIFIERS._addr32: | |
3133 printf("addr32 "); | |
3134 break; | |
3135 case ASM_MODIFIERS._fn16: | |
3136 printf("fn16 "); | |
3137 break; | |
3138 case ASM_MODIFIERS._fn32: | |
3139 printf("fn32 "); | |
3140 break; | |
3141 case ASM_MODIFIERS._flbl: | |
3142 printf("flbl "); | |
3143 break; | |
3144 default: | |
3145 printf("UNKNOWN "); | |
3146 break; | |
3147 } | |
3148 | |
3149 printf("uRegmask=x%02x", uRegmask); | |
3150 } | |
3151 | |
3152 OPND* asm_log_or_exp() | |
3153 { | |
3154 OPND* o1 = asm_log_and_exp(); | |
3155 OPND* o2; | |
3156 | |
3157 while (tok_value == TOK.TOKoror) | |
3158 { | |
3159 asm_token(); | |
3160 o2 = asm_log_and_exp(); | |
3161 if (asm_isint(o1) && asm_isint(o2)) | |
3162 o1.disp = o1.disp || o2.disp; | |
3163 else | |
3164 asmerr(ASMERRMSGS.EM_bad_integral_operand); // illegal operand | |
3165 o2.disp = 0; | |
3166 o1 = asm_merge_opnds(o1, o2); | |
3167 } | |
3168 return o1; | |
3169 } | |
3170 | |
3171 void asm_chktok(TOK toknum, uint errnum) | |
3172 { | |
3173 if (tok_value == toknum) | |
3174 asm_token(); // scan past token | |
3175 else | |
3176 /* When we run out of tokens, asmtok is null. | |
3177 * But when this happens when a ';' was hit. | |
3178 */ | |
3179 asmerr(errnum, asmtok ? asmtok.toChars() : ";"); | |
3180 } | |
3181 | |
3182 opflag_t asm_determine_float_flags(OPND* popnd) | |
3183 { | |
3184 //printf("asm_determine_float_flags()\n"); | |
3185 | |
3186 opflag_t us; | |
3187 opflag_t usFloat; | |
3188 | |
3189 // Insure that if it is a register, that it is not a normal processor | |
3190 // register. | |
3191 | |
3192 if (popnd.base && | |
3193 !popnd.s && !popnd.disp && !popnd.real_ | |
3194 && !(popnd.base.ty & (_r8 | _r16 | _r32))) | |
3195 { | |
3196 return popnd.base.ty; | |
3197 } | |
3198 if (popnd.pregDisp1 && !popnd.base) | |
3199 { | |
3200 us = asm_float_type_size(popnd.ptype, &usFloat); | |
3201 //printf("us = x%x, usFloat = x%x\n", us, usFloat); | |
3202 if (popnd.pregDisp1.ty & _r32) | |
3203 return(CONSTRUCT_FLAGS(us, _m, _addr32, usFloat)); | |
3204 else if (popnd.pregDisp1.ty & _r16) | |
3205 return(CONSTRUCT_FLAGS(us, _m, _addr16, usFloat)); | |
3206 } | |
3207 else if (popnd.s !is null) | |
3208 { | |
3209 us = asm_float_type_size(popnd.ptype, &usFloat); | |
3210 return CONSTRUCT_FLAGS(us, _m, _normal, usFloat); | |
3211 } | |
3212 | |
3213 if (popnd.segreg) | |
3214 { | |
3215 us = asm_float_type_size(popnd.ptype, &usFloat); | |
3216 if (I32) | |
3217 return(CONSTRUCT_FLAGS(us, _m, _addr32, usFloat)); | |
3218 else | |
3219 return(CONSTRUCT_FLAGS(us, _m, _addr16, usFloat)); | |
3220 } | |
3221 | |
3222 static if (false) { | |
3223 if (popnd.real_) | |
3224 { | |
3225 switch (popnd.ptype.ty) | |
3226 { | |
3227 case TY.Tfloat32: | |
3228 popnd.s = fconst(popnd.real_); | |
3229 return(CONSTRUCT_FLAGS(_32, _m, _normal, 0)); | |
3230 | |
3231 case TY.Tfloat64: | |
3232 popnd.s = dconst(popnd.real_); | |
3233 return(CONSTRUCT_FLAGS(0, _m, _normal, _64)); | |
3234 | |
3235 case TY.Tfloat80: | |
3236 popnd.s = ldconst(popnd.real_); | |
3237 return(CONSTRUCT_FLAGS(0, _m, _normal, _80)); | |
3238 } | |
3239 } | |
3240 } | |
3241 | |
3242 asmerr(ASMERRMSGS.EM_bad_float_op); // unknown operand for floating point instruction | |
3243 return 0; | |
3244 } | |
3245 | |
3246 uint asm_type_size(Type ptype) | |
3247 { | |
3248 //if (ptype) printf("asm_type_size('%s') = %d\n", ptype.toChars(), (int)ptype.size()); | |
3249 uint u = _anysize; | |
3250 if (ptype && ptype.ty != TY.Tfunction /*&& ptype.isscalar()*/) | |
3251 { | |
3252 switch (cast(int)ptype.size()) | |
3253 { | |
3254 case 0: asmerr(ASMERRMSGS.EM_bad_op, "0 size"); break; | |
3255 case 1: u = _8; break; | |
3256 case 2: u = _16; break; | |
3257 case 4: u = _32; break; | |
3258 case 6: u = _48; break; | |
3259 default: | |
3260 break; /// | |
3261 } | |
3262 } | |
3263 return u; | |
3264 } | |
3265 | |
3266 bool asm_match_float_flags(opflag_t usOp, opflag_t usTable) | |
3267 { | |
3268 ASM_OPERAND_TYPE aoptyTable; | |
3269 ASM_OPERAND_TYPE aoptyOp; | |
3270 ASM_MODIFIERS amodTable; | |
3271 ASM_MODIFIERS amodOp; | |
3272 uint uRegmaskTable; | |
3273 uint uRegmaskOp; | |
3274 ubyte bRegmatch; | |
3275 | |
3276 | |
3277 // | |
3278 // Check #1, if the sizes do not match, NO match | |
3279 // | |
3280 uRegmaskOp = ASM_GET_uRegmask(usOp); | |
3281 uRegmaskTable = ASM_GET_uRegmask(usTable); | |
3282 bRegmatch = (uRegmaskTable & uRegmaskOp) != 0; | |
3283 | |
3284 if (!(ASM_GET_uSizemask(usTable) & ASM_GET_uSizemask(usOp) || bRegmatch)) | |
3285 return false; | |
3286 | |
3287 aoptyTable = ASM_GET_aopty(usTable); | |
3288 aoptyOp = ASM_GET_aopty(usOp); | |
3289 // | |
3290 // The operand types must match, otherwise return false. | |
3291 // There is one exception for the _rm which is a table entry which matches | |
3292 // _reg or _m | |
3293 // | |
3294 if (aoptyTable != aoptyOp) | |
3295 { | |
3296 if (aoptyOp != _float) | |
3297 return false; | |
3298 } | |
3299 | |
3300 // | |
3301 // Looks like a match so far, check to see if anything special is going on | |
3302 // | |
3303 amodOp = ASM_GET_amod(usOp); | |
3304 amodTable = ASM_GET_amod(usTable); | |
3305 switch (amodTable) | |
3306 { | |
3307 // Normal's match with normals | |
3308 case _normal: | |
3309 switch(amodOp) | |
3310 { | |
3311 case _normal: | |
3312 case _addr16: | |
3313 case _addr32: | |
3314 case _fn16: | |
3315 case _fn32: | |
3316 case _flbl: | |
3317 return true; | |
3318 | |
3319 default: | |
3320 return false; | |
3321 } | |
3322 | |
3323 case _rseg: | |
3324 case _rspecial: | |
3325 return false; | |
3326 default: | |
3327 assert(0); | |
3328 } | |
3329 } | |
3330 | |
3331 OPND* asm_log_and_exp() | |
3332 { | |
3333 OPND* o1 = asm_inc_or_exp(); | |
3334 OPND* o2; | |
3335 | |
3336 while (tok_value == TOKandand) | |
3337 { | |
3338 asm_token(); | |
3339 o2 = asm_inc_or_exp(); | |
3340 if (asm_isint(o1) && asm_isint(o2)) | |
3341 o1.disp = o1.disp && o2.disp; | |
3342 else { | |
3343 asmerr(EM_bad_integral_operand); // illegal operand | |
3344 } | |
3345 o2.disp = 0; | |
3346 o1 = asm_merge_opnds(o1, o2); | |
3347 } | |
3348 return o1; | |
3349 } | |
3350 | |
3351 bool asm_isint(OPND *o) | |
3352 { | |
3353 if (!o || o.base || o.s) | |
3354 return false; | |
3355 | |
3356 //return o.disp != 0; | |
3357 return true; | |
3358 } | |
3359 | |
3360 /******************************* | |
3361 * Merge operands o1 and o2 into a single operand. | |
3362 */ | |
3363 | |
3364 OPND* asm_merge_opnds(OPND* o1, OPND* o2) | |
3365 { | |
3366 debug immutable(char)* psz; | |
3367 debug if (debuga) | |
3368 { | |
3369 printf("asm_merge_opnds(o1 = "); | |
3370 if (o1) asm_output_popnd(o1); | |
3371 printf(", o2 = "); | |
3372 if (o2) asm_output_popnd(o2); | |
3373 printf(")\n"); | |
3374 } | |
3375 | |
3376 if (!o1) | |
3377 return o2; | |
3378 if (!o2) | |
3379 return o1; | |
3380 version (EXTRA_DEBUG) { | |
3381 printf("Combining Operands: mult1 = %d, mult2 = %d", | |
3382 o1.uchMultiplier, o2.uchMultiplier); | |
3383 } | |
3384 /* combine the OPND's disp field */ | |
3385 if (o2.segreg) { | |
3386 if (o1.segreg) { | |
3387 debug psz = "o1.segment && o2.segreg".ptr; | |
3388 goto ILLEGAL_ADDRESS_ERROR; | |
3389 } | |
3390 else | |
3391 o1.segreg = o2.segreg; | |
3392 } | |
3393 | |
3394 // combine the OPND's symbol field | |
3395 if (o1.s && o2.s) | |
3396 { | |
3397 debug psz = "o1.s && os.s"; | |
3398 ILLEGAL_ADDRESS_ERROR: | |
3399 debug printf("Invalid addr because /%s/\n", psz); | |
3400 | |
3401 asmerr(EM_bad_addr_mode); // illegal addressing mode | |
3402 } | |
3403 else if (o2.s) | |
3404 o1.s = o2.s; | |
3405 else if (o1.s && o1.s.isTupleDeclaration()) | |
3406 { | |
3407 TupleDeclaration tup = o1.s.isTupleDeclaration(); | |
3408 | |
3409 size_t index = o2.disp; | |
3410 if (index >= tup.objects.dim) | |
3411 error(asmstate.loc, "tuple index %u exceeds %u", index, tup.objects.dim); | |
3412 else | |
3413 { | |
3414 Object o = cast(Object)tup.objects.data[index]; | |
3415 if (auto d = cast(Dsymbol)o) | |
3416 { | |
3417 o1.s = d; | |
3418 return o1; | |
3419 } | |
3420 else if (auto e = cast(Expression)o) | |
3421 { | |
3422 if (e.op == TOKvar) | |
3423 { | |
3424 o1.s = (cast(VarExp)e).var; | |
3425 return o1; | |
3426 } | |
3427 else if (e.op == TOKfunction) | |
3428 { | |
3429 o1.s = (cast(FuncExp)e).fd; | |
3430 return o1; | |
3431 } | |
3432 } | |
3433 error(asmstate.loc, "invalid asm operand %s", o1.s.toChars()); | |
3434 } | |
3435 } | |
3436 | |
3437 if (o1.disp && o2.disp) | |
3438 o1.disp += o2.disp; | |
3439 else if (o2.disp) | |
3440 o1.disp = o2.disp; | |
3441 | |
3442 /* combine the OPND's base field */ | |
3443 if (o1.base !is null && o2.base !is null) { | |
3444 debug psz = "o1.base != null && o2.base != null".ptr; | |
3445 goto ILLEGAL_ADDRESS_ERROR; | |
3446 } | |
3447 else if (o2.base) | |
3448 o1.base = o2.base; | |
3449 | |
3450 /* Combine the displacement register fields */ | |
3451 if (o2.pregDisp1) | |
3452 { | |
3453 if (o1.pregDisp2) | |
3454 { | |
3455 debug psz = "o2.pregDisp1 && o1.pregDisp2"; | |
3456 goto ILLEGAL_ADDRESS_ERROR; | |
3457 } | |
3458 else if (o1.pregDisp1) | |
3459 { | |
3460 if (o1.uchMultiplier || | |
3461 (o2.pregDisp1.val == _ESP && | |
3462 (o2.pregDisp1.ty & _r32) && | |
3463 !o2.uchMultiplier)) | |
3464 { | |
3465 o1.pregDisp2 = o1.pregDisp1; | |
3466 o1.pregDisp1 = o2.pregDisp1; | |
3467 } | |
3468 else | |
3469 o1.pregDisp2 = o2.pregDisp1; | |
3470 } | |
3471 else | |
3472 o1.pregDisp1 = o2.pregDisp1; | |
3473 } | |
3474 if (o2.pregDisp2) { | |
3475 if (o1.pregDisp2) { | |
3476 debug psz = "o1.pregDisp2 && o2.pregDisp2"; | |
3477 goto ILLEGAL_ADDRESS_ERROR; | |
3478 } | |
3479 else | |
3480 o1.pregDisp2 = o2.pregDisp2; | |
3481 } | |
3482 if (o2.uchMultiplier) | |
3483 { | |
3484 if (o1.uchMultiplier) | |
3485 { | |
3486 debug psz = "o1.uchMultiplier && o2.uchMultiplier"; | |
3487 goto ILLEGAL_ADDRESS_ERROR; | |
3488 } | |
3489 else | |
3490 o1.uchMultiplier = o2.uchMultiplier; | |
3491 } | |
3492 if (o2.ptype && !o1.ptype) | |
3493 o1.ptype = o2.ptype; | |
3494 if (o2.bOffset) | |
3495 o1.bOffset = o2.bOffset; | |
3496 if (o2.bSeg) | |
3497 o1.bSeg = o2.bSeg; | |
3498 | |
3499 if (o2.ajt && !o1.ajt) | |
3500 o1.ajt = o2.ajt; | |
3501 | |
3502 opnd_free(o2); | |
3503 version (EXTRA_DEBUG) { | |
3504 printf("Result = %d\n", o1.uchMultiplier); | |
3505 } | |
3506 debug if (debuga) | |
3507 { printf("Merged result = /"); | |
3508 asm_output_popnd(o1); | |
3509 printf("/\n"); | |
3510 } | |
3511 | |
3512 return o1; | |
3513 } | |
3514 | |
3515 opflag_t asm_float_type_size(Type ptype, opflag_t* pusFloat) | |
3516 { | |
3517 *pusFloat = 0; | |
3518 | |
3519 //printf("asm_float_type_size('%s')\n", ptype.toChars()); | |
3520 if (ptype && ptype.isscalar()) | |
3521 { | |
3522 int sz = cast(int)ptype.size(); | |
3523 if (sz == REALSIZE) | |
3524 { | |
3525 *pusFloat = _80; | |
3526 return 0; | |
3527 } | |
3528 switch (sz) | |
3529 { | |
3530 case 2: | |
3531 return _16; | |
3532 case 4: | |
3533 return _32; | |
3534 case 8: | |
3535 *pusFloat = _64; | |
3536 return 0; | |
3537 default: | |
3538 break; | |
3539 } | |
3540 } | |
3541 | |
3542 *pusFloat = _fanysize; | |
3543 return _anysize; | |
3544 } | |
3545 | |
3546 OPND* asm_inc_or_exp() | |
3547 { | |
3548 OPND* o1 = asm_xor_exp(); | |
3549 OPND* o2; | |
3550 | |
3551 while (tok_value == TOKor) | |
3552 { | |
3553 asm_token(); | |
3554 o2 = asm_xor_exp(); | |
3555 if (asm_isint(o1) && asm_isint(o2)) | |
3556 o1.disp |= o2.disp; | |
3557 else { | |
3558 asmerr(EM_bad_integral_operand); // illegal operand | |
3559 } | |
3560 o2.disp = 0; | |
3561 o1 = asm_merge_opnds(o1, o2); | |
3562 } | |
3563 return o1; | |
3564 } | |
3565 | |
3566 OPND* asm_xor_exp() | |
3567 { | |
3568 OPND* o1 = asm_and_exp(); | |
3569 OPND* o2; | |
3570 | |
3571 while (tok_value == TOKxor) | |
3572 { | |
3573 asm_token(); | |
3574 o2 = asm_and_exp(); | |
3575 if (asm_isint(o1) && asm_isint(o2)) | |
3576 o1.disp ^= o2.disp; | |
3577 else { | |
3578 asmerr(EM_bad_integral_operand); // illegal operand | |
3579 } | |
3580 o2.disp = 0; | |
3581 o1 = asm_merge_opnds(o1, o2); | |
3582 } | |
3583 return o1; | |
3584 } | |
3585 | |
3586 OPND* asm_and_exp() | |
3587 { | |
3588 OPND* o1 = asm_equal_exp(); | |
3589 OPND* o2; | |
3590 | |
3591 while (tok_value == TOKand) | |
3592 { | |
3593 asm_token(); | |
3594 o2 = asm_equal_exp(); | |
3595 if (asm_isint(o1) && asm_isint(o2)) | |
3596 o1.disp &= o2.disp; | |
3597 else { | |
3598 asmerr(EM_bad_integral_operand); // illegal operand | |
3599 } | |
3600 o2.disp = 0; | |
3601 o1 = asm_merge_opnds(o1, o2); | |
3602 } | |
3603 return o1; | |
3604 } | |
3605 | |
3606 OPND* asm_equal_exp() | |
3607 { | |
3608 OPND* o1 = asm_rel_exp(); | |
3609 OPND* o2; | |
3610 | |
3611 while (1) | |
3612 { | |
3613 switch (tok_value) | |
3614 { | |
3615 case TOKequal: | |
3616 asm_token(); | |
3617 o2 = asm_rel_exp(); | |
3618 if (asm_isint(o1) && asm_isint(o2)) | |
3619 o1.disp = o1.disp == o2.disp; | |
3620 else { | |
3621 asmerr(EM_bad_integral_operand); // illegal operand | |
3622 } | |
3623 o2.disp = 0; | |
3624 o1 = asm_merge_opnds(o1, o2); | |
3625 break; | |
3626 | |
3627 case TOKnotequal: | |
3628 asm_token(); | |
3629 o2 = asm_rel_exp(); | |
3630 if (asm_isint(o1) && asm_isint(o2)) | |
3631 o1.disp = o1.disp != o2.disp; | |
3632 else { | |
3633 asmerr(EM_bad_integral_operand); | |
3634 } | |
3635 o2.disp = 0; | |
3636 o1 = asm_merge_opnds(o1, o2); | |
3637 break; | |
3638 | |
3639 default: | |
3640 return o1; | |
3641 } | |
3642 } | |
3643 | |
3644 assert(false); | |
3645 } | |
3646 | |
3647 OPND* asm_rel_exp() | |
3648 { | |
3649 OPND* o1 = asm_shift_exp(); | |
3650 OPND* o2; | |
3651 | |
3652 TOK tok_save; | |
3653 | |
3654 while (1) | |
3655 { | |
3656 switch (tok_value) | |
3657 { | |
3658 case TOKgt: | |
3659 case TOKge: | |
3660 case TOKlt: | |
3661 case TOKle: | |
3662 tok_save = tok_value; | |
3663 asm_token(); | |
3664 o2 = asm_shift_exp(); | |
3665 if (asm_isint(o1) && asm_isint(o2)) | |
3666 { | |
3667 switch (tok_save) | |
3668 { | |
3669 case TOKgt: | |
3670 o1.disp = o1.disp > o2.disp; | |
3671 break; | |
3672 case TOKge: | |
3673 o1.disp = o1.disp >= o2.disp; | |
3674 break; | |
3675 case TOKlt: | |
3676 o1.disp = o1.disp < o2.disp; | |
3677 break; | |
3678 case TOKle: | |
3679 o1.disp = o1.disp <= o2.disp; | |
3680 break; | |
3681 } | |
3682 } | |
3683 else | |
3684 asmerr(EM_bad_integral_operand); | |
3685 o2.disp = 0; | |
3686 o1 = asm_merge_opnds(o1, o2); | |
3687 break; | |
3688 | |
3689 default: | |
3690 return o1; | |
3691 } | |
3692 } | |
3693 | |
3694 assert(false); | |
3695 } | |
3696 | |
3697 OPND* asm_shift_exp() | |
3698 { | |
3699 OPND* o1 = asm_add_exp(); | |
3700 OPND* o2; | |
3701 | |
3702 int op; | |
3703 TOK tk; | |
3704 | |
3705 while (tok_value == TOKshl || tok_value == TOKshr || tok_value == TOKushr) | |
3706 { | |
3707 tk = tok_value; | |
3708 asm_token(); | |
3709 o2 = asm_add_exp(); | |
3710 if (asm_isint(o1) && asm_isint(o2)) | |
3711 { if (tk == TOKshl) | |
3712 o1.disp <<= o2.disp; | |
3713 else if (tk == TOKushr) | |
3714 o1.disp = cast(uint)o1.disp >> o2.disp; | |
3715 else | |
3716 o1.disp >>= o2.disp; | |
3717 } | |
3718 else | |
3719 asmerr(EM_bad_integral_operand); | |
3720 o2.disp = 0; | |
3721 o1 = asm_merge_opnds(o1, o2); | |
3722 } | |
3723 return o1; | |
3724 } | |
3725 | |
3726 /******************************* | |
3727 */ | |
3728 | |
3729 OPND* asm_add_exp() | |
3730 { | |
3731 OPND* o1 = asm_mul_exp(); | |
3732 OPND* o2; | |
3733 | |
3734 while (1) | |
3735 { | |
3736 switch (tok_value) | |
3737 { | |
3738 case TOKadd: | |
3739 asm_token(); | |
3740 o2 = asm_mul_exp(); | |
3741 o1 = asm_merge_opnds(o1, o2); | |
3742 break; | |
3743 | |
3744 case TOKmin: | |
3745 asm_token(); | |
3746 o2 = asm_mul_exp(); | |
3747 if (asm_isint(o1) && asm_isint(o2)) | |
3748 { | |
3749 o1.disp -= o2.disp; | |
3750 o2.disp = 0; | |
3751 } | |
3752 else | |
3753 o2.disp = - o2.disp; | |
3754 o1 = asm_merge_opnds(o1, o2); | |
3755 break; | |
3756 | |
3757 default: | |
3758 return o1; | |
3759 } | |
3760 } | |
3761 | |
3762 assert(false); | |
3763 } | |
3764 | |
3765 /******************************* | |
3766 */ | |
3767 | |
3768 OPND* asm_mul_exp() | |
3769 { | |
3770 OPND* o1; | |
3771 OPND* o2; | |
3772 OPND* popndTmp; | |
3773 | |
3774 //printf("+asm_mul_exp()\n"); | |
3775 o1 = asm_br_exp(); | |
3776 while (1) | |
3777 { | |
3778 switch (tok_value) | |
3779 { | |
3780 case TOKmul: | |
3781 asm_token(); | |
3782 o2 = asm_br_exp(); | |
3783 version (EXTRA_DEBUG) { | |
3784 printf("Star o1.isint=%d, o2.isint=%d, lbra_seen=%d\n", | |
3785 asm_isint(o1), asm_isint(o2), asm_TKlbra_seen ); | |
3786 } | |
3787 if (asm_isNonZeroInt(o1) && asm_isNonZeroInt(o2)) | |
3788 o1.disp *= o2.disp; | |
3789 else if (asm_TKlbra_seen && o1.pregDisp1 && asm_isNonZeroInt(o2)) | |
3790 { | |
3791 o1.uchMultiplier = o2.disp; | |
3792 version (EXTRA_DEBUG) { | |
3793 printf("Multiplier: %d\n", o1.uchMultiplier); | |
3794 } | |
3795 } | |
3796 else if (asm_TKlbra_seen && o2.pregDisp1 && asm_isNonZeroInt(o1)) | |
3797 { | |
3798 popndTmp = o2; | |
3799 o2 = o1; | |
3800 o1 = popndTmp; | |
3801 o1.uchMultiplier = o2.disp; | |
3802 version (EXTRA_DEBUG) { | |
3803 printf("Multiplier: %d\n", | |
3804 o1.uchMultiplier); | |
3805 } | |
3806 } | |
3807 else if (asm_isint(o1) && asm_isint(o2)) | |
3808 o1.disp *= o2.disp; | |
3809 else | |
3810 asmerr(EM_bad_operand); | |
3811 o2.disp = 0; | |
3812 o1 = asm_merge_opnds(o1, o2); | |
3813 break; | |
3814 | |
3815 case TOKdiv: | |
3816 asm_token(); | |
3817 o2 = asm_br_exp(); | |
3818 if (asm_isint(o1) && asm_isint(o2)) | |
3819 o1.disp /= o2.disp; | |
3820 else | |
3821 asmerr(EM_bad_integral_operand); | |
3822 o2.disp = 0; | |
3823 o1 = asm_merge_opnds(o1, o2); | |
3824 break; | |
3825 | |
3826 case TOKmod: | |
3827 asm_token(); | |
3828 o2 = asm_br_exp(); | |
3829 if (asm_isint(o1) && asm_isint(o2)) | |
3830 o1.disp %= o2.disp; | |
3831 else | |
3832 asmerr(EM_bad_integral_operand); | |
3833 o2.disp = 0; | |
3834 o1 = asm_merge_opnds(o1, o2); | |
3835 break; | |
3836 | |
3837 default: | |
3838 return o1; | |
3839 } | |
3840 } | |
3841 | |
3842 return o1; | |
3843 } | |
3844 | |
3845 OPND* asm_br_exp() | |
3846 { | |
3847 //printf("asm_br_exp()\n"); | |
3848 | |
3849 OPND* o1 = asm_una_exp(); | |
3850 OPND* o2; | |
3851 Declaration s; | |
3852 | |
3853 while (1) | |
3854 { | |
3855 switch (tok_value) | |
3856 { | |
3857 case TOKlbracket: | |
3858 { | |
3859 version (EXTRA_DEBUG) { | |
3860 printf("Saw a left bracket\n"); | |
3861 } | |
3862 asm_token(); | |
3863 asm_TKlbra_seen++; | |
3864 o2 = asm_cond_exp(); | |
3865 asm_TKlbra_seen--; | |
3866 asm_chktok(TOKrbracket,EM_rbra); | |
3867 version (EXTRA_DEBUG) { | |
3868 printf("Saw a right bracket\n"); | |
3869 } | |
3870 o1 = asm_merge_opnds(o1, o2); | |
3871 if (tok_value == TOKidentifier) | |
3872 { o2 = asm_una_exp(); | |
3873 o1 = asm_merge_opnds(o1, o2); | |
3874 } | |
3875 break; | |
3876 } | |
3877 default: | |
3878 return o1; | |
3879 } | |
3880 } | |
3881 | |
3882 assert(false); | |
3883 } | |
3884 | |
3885 /******************************* | |
3886 */ | |
3887 | |
3888 OPND* asm_una_exp() | |
3889 { | |
3890 OPND* o1; | |
3891 int op; | |
3892 Type ptype; | |
3893 Type ptypeSpec; | |
3894 ASM_JUMPTYPE ajt = ASM_JUMPTYPE_UNSPECIFIED; | |
3895 char bPtr = 0; | |
3896 | |
3897 switch (cast(int)tok_value) | |
3898 { | |
3899 static if (false) { | |
3900 case TOKand: | |
3901 asm_token(); | |
3902 o1 = asm_una_exp(); | |
3903 break; | |
3904 | |
3905 case TOKmul: | |
3906 asm_token(); | |
3907 o1 = asm_una_exp(); | |
3908 ++o1.indirect; | |
3909 break; | |
3910 } | |
3911 case TOKadd: | |
3912 asm_token(); | |
3913 o1 = asm_una_exp(); | |
3914 break; | |
3915 | |
3916 case TOKmin: | |
3917 asm_token(); | |
3918 o1 = asm_una_exp(); | |
3919 if (asm_isint(o1)) | |
3920 o1.disp = -o1.disp; | |
3921 break; | |
3922 | |
3923 case TOKnot: | |
3924 asm_token(); | |
3925 o1 = asm_una_exp(); | |
3926 if (asm_isint(o1)) | |
3927 o1.disp = !o1.disp; | |
3928 break; | |
3929 | |
3930 case TOKtilde: | |
3931 asm_token(); | |
3932 o1 = asm_una_exp(); | |
3933 if (asm_isint(o1)) | |
3934 o1.disp = ~o1.disp; | |
3935 break; | |
3936 | |
3937 static if (false) { | |
3938 case TOKlparen: | |
3939 // stoken() is called directly here because we really | |
3940 // want the INT token to be an INT. | |
3941 stoken(); | |
3942 if (type_specifier(&ptypeSpec)) /* if type_name */ | |
3943 { | |
3944 ptype = declar_abstract(ptypeSpec); | |
3945 /* read abstract_declarator */ | |
3946 fixdeclar(ptype);/* fix declarator */ | |
3947 type_free(ptypeSpec);/* the declar() function | |
3948 allocates the typespec again */ | |
3949 chktok(TOKrparen,EM_rpar); | |
3950 ptype.Tcount--; | |
3951 goto CAST_REF; | |
3952 } | |
3953 else | |
3954 { | |
3955 type_free(ptypeSpec); | |
3956 o1 = asm_cond_exp(); | |
3957 chktok(TOKrparen, EM_rpar); | |
3958 } | |
3959 break; | |
3960 } | |
3961 | |
3962 case TOKidentifier: | |
3963 // Check for offset keyword | |
3964 if (asmtok.ident == Id.offset) | |
3965 { | |
3966 if (!global.params.useDeprecated) | |
3967 error(asmstate.loc, "offset deprecated, use offsetof"); | |
3968 goto Loffset; | |
3969 } | |
3970 if (asmtok.ident == Id.offsetof) | |
3971 { | |
3972 Loffset: | |
3973 asm_token(); | |
3974 o1 = asm_cond_exp(); | |
3975 if (!o1) | |
3976 o1 = opnd_calloc(); | |
3977 o1.bOffset= true; | |
3978 } | |
3979 else | |
3980 o1 = asm_primary_exp(); | |
3981 break; | |
3982 | |
3983 case ASMTK.ASMTKseg: | |
3984 asm_token(); | |
3985 o1 = asm_cond_exp(); | |
3986 if (!o1) | |
3987 o1 = opnd_calloc(); | |
3988 o1.bSeg= true; | |
3989 break; | |
3990 | |
3991 case TOKint16: | |
3992 if (asmstate.ucItype != ITjump) | |
3993 { | |
3994 ptype = Type.tint16; | |
3995 goto TYPE_REF; | |
3996 } | |
3997 ajt = ASM_JUMPTYPE_SHORT; | |
3998 asm_token(); | |
3999 goto JUMP_REF2; | |
4000 | |
4001 case ASMTKnear: | |
4002 ajt = ASM_JUMPTYPE_NEAR; | |
4003 goto JUMP_REF; | |
4004 | |
4005 case ASMTKfar: | |
4006 ajt = ASM_JUMPTYPE_FAR; | |
4007 JUMP_REF: | |
4008 asm_token(); | |
4009 asm_chktok(cast(TOK) ASMTKptr, EM_ptr_exp); | |
4010 JUMP_REF2: | |
4011 o1 = asm_cond_exp(); | |
4012 if (!o1) | |
4013 o1 = opnd_calloc(); | |
4014 o1.ajt= ajt; | |
4015 break; | |
4016 | |
4017 case TOKint8: | |
4018 ptype = Type.tint8; | |
4019 goto TYPE_REF; | |
4020 case TOKint32: | |
4021 case ASMTKdword: | |
4022 ptype = Type.tint32; | |
4023 goto TYPE_REF; | |
4024 case TOKfloat32: | |
4025 ptype = Type.tfloat32; | |
4026 goto TYPE_REF; | |
4027 case ASMTKqword: | |
4028 case TOKfloat64: | |
4029 ptype = Type.tfloat64; | |
4030 goto TYPE_REF; | |
4031 case TOKfloat80: | |
4032 ptype = Type.tfloat80; | |
4033 goto TYPE_REF; | |
4034 case ASMTKword: | |
4035 ptype = Type.tint16; | |
4036 TYPE_REF: | |
4037 bPtr = 1; | |
4038 asm_token(); | |
4039 asm_chktok(cast(TOK) ASMTKptr, EM_ptr_exp); | |
4040 CAST_REF: | |
4041 o1 = asm_cond_exp(); | |
4042 if (!o1) | |
4043 o1 = opnd_calloc(); | |
4044 o1.ptype = ptype; | |
4045 o1.bPtr = bPtr; | |
4046 break; | |
4047 | |
4048 default: | |
4049 o1 = asm_primary_exp(); | |
4050 break; | |
4051 } | |
4052 return o1; | |
4053 } | |
4054 | |
4055 bool asm_isNonZeroInt(OPND* o) | |
4056 { | |
4057 if (!o || o.base || o.s) | |
4058 return false; | |
4059 | |
4060 return o.disp != 0; | |
4061 } | |
4062 | |
4063 OPND* asm_primary_exp() | |
4064 { | |
4065 OPND* o1 = null; | |
4066 OPND* o2 = null; | |
4067 Type ptype; | |
4068 Dsymbol s; | |
4069 Dsymbol scopesym; | |
4070 | |
4071 TOK tkOld; | |
4072 int global; | |
4073 REG* regp; | |
4074 | |
4075 global = 0; | |
4076 switch (cast(int)tok_value) | |
4077 { | |
4078 case TOKdollar: | |
4079 o1 = opnd_calloc(); | |
4080 o1.s = asmstate.psDollar; | |
4081 asm_token(); | |
4082 break; | |
4083 | |
4084 static if (false) { | |
4085 case TOKthis: | |
4086 strcpy(tok.TKid,cpp_name_this); | |
4087 } | |
4088 case TOKidentifier: | |
4089 case_ident: | |
4090 o1 = opnd_calloc(); | |
4091 regp = asm_reg_lookup(asmtok.ident.toChars()); | |
4092 if (regp !is null) | |
4093 { | |
4094 asm_token(); | |
4095 // see if it is segment override (like SS:) | |
4096 if (!asm_TKlbra_seen && | |
4097 (regp.ty & _seg) && | |
4098 tok_value == TOKcolon) | |
4099 { | |
4100 o1.segreg = regp; | |
4101 asm_token(); | |
4102 o2 = asm_cond_exp(); | |
4103 o1 = asm_merge_opnds(o1, o2); | |
4104 } | |
4105 else if (asm_TKlbra_seen) | |
4106 { // should be a register | |
4107 if (o1.pregDisp1) | |
4108 asmerr(EM_bad_operand); | |
4109 else | |
4110 o1.pregDisp1 = regp; | |
4111 } | |
4112 else | |
4113 { if (o1.base == null) | |
4114 o1.base = regp; | |
4115 else | |
4116 asmerr(EM_bad_operand); | |
4117 } | |
4118 break; | |
4119 } | |
4120 // If floating point instruction and id is a floating register | |
4121 else if (asmstate.ucItype == ITfloat && | |
4122 asm_is_fpreg(asmtok.ident.toChars())) | |
4123 { | |
4124 asm_token(); | |
4125 if (tok_value == TOKlparen) | |
4126 { | |
4127 uint n; | |
4128 | |
4129 asm_token(); | |
4130 asm_chktok(TOKint32v, EM_num); | |
4131 n = cast(uint)asmtok.uns64value; | |
4132 if (n > 7) | |
4133 asmerr(EM_bad_operand); | |
4134 o1.base = &(aregFp[n]); | |
4135 asm_chktok(TOKrparen, EM_rpar); | |
4136 } | |
4137 else | |
4138 o1.base = ®Fp; | |
4139 } | |
4140 else | |
4141 { | |
4142 if (asmstate.ucItype == ITjump) | |
4143 { | |
4144 s = null; | |
4145 if (asmstate.sc.func.labtab) | |
4146 s = asmstate.sc.func.labtab.lookup(asmtok.ident); | |
4147 if (!s) | |
4148 s = asmstate.sc.search(Loc(0), asmtok.ident, &scopesym); | |
4149 if (!s) | |
4150 { // Assume it is a label, and define that label | |
4151 s = asmstate.sc.func.searchLabel(asmtok.ident); | |
4152 } | |
4153 } | |
4154 else | |
4155 s = asmstate.sc.search(Loc(0), asmtok.ident, &scopesym); | |
4156 | |
4157 if (!s) | |
4158 asmerr(EM_undefined, asmtok.toChars()); | |
4159 | |
4160 Identifier id = asmtok.ident; | |
4161 asm_token(); | |
4162 if (tok_value == TOKdot) | |
4163 { | |
4164 Expression e; | |
4165 VarExp v; | |
4166 | |
4167 e = new IdentifierExp(asmstate.loc, id); | |
4168 while (1) | |
4169 { | |
4170 asm_token(); | |
4171 if (tok_value == TOKidentifier) | |
4172 { | |
4173 e = new DotIdExp(asmstate.loc, e, asmtok.ident); | |
4174 asm_token(); | |
4175 if (tok_value != TOKdot) | |
4176 break; | |
4177 } | |
4178 else | |
4179 { | |
4180 asmerr(EM_ident_exp); | |
4181 break; | |
4182 } | |
4183 } | |
4184 e = e.semantic(asmstate.sc); | |
4185 e = e.optimize(WANTvalue | WANTinterpret); | |
4186 if (e.isConst()) | |
4187 { | |
4188 if (e.type.isintegral()) | |
4189 { | |
4190 o1.disp = cast(int)e.toInteger(); | |
4191 goto Lpost; | |
4192 } | |
4193 else if (e.type.isreal()) | |
4194 { | |
4195 o1.real_ = e.toReal(); | |
4196 o1.ptype = e.type; | |
4197 goto Lpost; | |
4198 } | |
4199 else | |
4200 { | |
4201 asmerr(EM_bad_op, e.toChars()); | |
4202 } | |
4203 } | |
4204 else if (e.op == TOKvar) | |
4205 { | |
4206 v = cast(VarExp)e; | |
4207 s = v.var; | |
4208 } | |
4209 else | |
4210 { | |
4211 asmerr(EM_bad_op, e.toChars()); | |
4212 } | |
4213 } | |
4214 | |
4215 asm_merge_symbol(o1,s); | |
4216 | |
4217 /* This attempts to answer the question: is | |
4218 * char[8] foo; | |
4219 * of size 1 or size 8? Presume it is 8 if foo | |
4220 * is the last token of the operand. | |
4221 */ | |
4222 if (o1.ptype && tok_value != TOKcomma && tok_value != TOKeof) | |
4223 { | |
4224 for (; | |
4225 o1.ptype.ty == Tsarray; | |
4226 o1.ptype = o1.ptype.nextOf()) | |
4227 { | |
4228 ; | |
4229 } | |
4230 } | |
4231 | |
4232 Lpost: | |
4233 static if (false) { | |
4234 // for [] | |
4235 if (tok_value == TOKlbracket) | |
4236 o1 = asm_prim_post(o1); | |
4237 } | |
4238 goto Lret; | |
4239 } | |
4240 break; | |
4241 | |
4242 case TOKint32v: | |
4243 case TOKuns32v: | |
4244 o1 = opnd_calloc(); | |
4245 o1.disp = asmtok.int32value; | |
4246 asm_token(); | |
4247 break; | |
4248 | |
4249 case TOKfloat32v: | |
4250 o1 = opnd_calloc(); | |
4251 o1.real_ = asmtok.float80value; | |
4252 o1.ptype = Type.tfloat32; | |
4253 asm_token(); | |
4254 break; | |
4255 | |
4256 case TOKfloat64v: | |
4257 o1 = opnd_calloc(); | |
4258 o1.real_ = asmtok.float80value; | |
4259 o1.ptype = Type.tfloat64; | |
4260 asm_token(); | |
4261 break; | |
4262 | |
4263 case TOKfloat80v: | |
4264 o1 = opnd_calloc(); | |
4265 o1.real_ = asmtok.float80value; | |
4266 o1.ptype = Type.tfloat80; | |
4267 asm_token(); | |
4268 break; | |
4269 | |
4270 case ASMTKlocalsize: | |
4271 o1 = opnd_calloc(); | |
4272 o1.s = asmstate.psLocalsize; | |
4273 o1.ptype = Type.tint32; | |
4274 asm_token(); | |
4275 break; | |
4276 | |
4277 default: | |
4278 break; /// | |
4279 } | |
4280 Lret: | |
4281 return o1; | |
4282 } | |
4283 | |
4284 void asm_merge_symbol(OPND* o1, Dsymbol s) | |
4285 { | |
4286 Type ptype; | |
4287 VarDeclaration v; | |
4288 EnumMember em; | |
4289 | |
4290 //printf("asm_merge_symbol(s = %s %s)\n", s.kind(), s.toChars()); | |
4291 s = s.toAlias(); | |
4292 //printf("s = %s %s\n", s.kind(), s.toChars()); | |
4293 if (s.isLabel()) | |
4294 { | |
4295 o1.s = s; | |
4296 return; | |
4297 } | |
4298 | |
4299 v = s.isVarDeclaration(); | |
4300 if (v) | |
4301 { | |
4302 if (v.isParameter()) | |
4303 asmstate.statement.refparam = true; | |
4304 | |
4305 v.checkNestedReference(asmstate.sc, asmstate.loc); | |
4306 static if (false) { | |
4307 if (!v.isDataseg() && v.parent != asmstate.sc.parent && v.parent) | |
4308 { | |
4309 asmerr(EM_uplevel, v.toChars()); | |
4310 } | |
4311 } | |
4312 if (v.storage_class & STCfield) | |
4313 { | |
4314 o1.disp += v.offset; | |
4315 goto L2; | |
4316 } | |
4317 if ((v.isConst() | |
4318 ///version (DMDV2) { | |
4319 || v.isInvariant() || v.storage_class & STCmanifest | |
4320 ///} | |
4321 ) && !v.type.isfloating() && v.init) | |
4322 { | |
4323 ExpInitializer ei = v.init.isExpInitializer(); | |
4324 | |
4325 if (ei) | |
4326 { | |
4327 o1.disp = cast(int)ei.exp.toInteger(); | |
4328 return; | |
4329 } | |
4330 } | |
4331 } | |
4332 em = s.isEnumMember(); | |
4333 if (em) | |
4334 { | |
4335 o1.disp = cast(int)em.value.toInteger(); | |
4336 return; | |
4337 } | |
4338 o1.s = s; // a C identifier | |
4339 L2: | |
4340 Declaration d = s.isDeclaration(); | |
4341 if (!d) | |
4342 { | |
4343 asmerr("%s %s is not a declaration", s.kind(), s.toChars()); | |
4344 } | |
4345 else if (d.getType()) | |
4346 asmerr(EM_type_as_operand, d.getType().toChars()); | |
4347 else if (d.isTupleDeclaration()) { | |
4348 ; | |
4349 } else | |
4350 o1.ptype = d.type.toBasetype(); | |
4351 } | |
4352 | |
4353 REG[63] regtab = | |
4354 [ | |
4355 {"AL", _AL, _r8 | _al,}, | |
4356 {"AH", _AH, _r8,}, | |
4357 {"AX", _AX, _r16 | _ax,}, | |
4358 {"EAX", _EAX, _r32 | _eax,}, | |
4359 {"BL", _BL, _r8,}, | |
4360 {"BH", _BH, _r8,}, | |
4361 {"BX", _BX, _r16,}, | |
4362 {"EBX", _EBX, _r32,}, | |
4363 {"CL", _CL, _r8 | _cl,}, | |
4364 {"CH", _CH, _r8,}, | |
4365 {"CX", _CX, _r16,}, | |
4366 {"ECX", _ECX, _r32,}, | |
4367 {"DL", _DL, _r8,}, | |
4368 {"DH", _DH, _r8,}, | |
4369 {"DX", _DX, _r16 | _dx,}, | |
4370 {"EDX", _EDX, _r32,}, | |
4371 {"BP", _BP, _r16,}, | |
4372 {"EBP", _EBP, _r32,}, | |
4373 {"SP", _SP, _r16,}, | |
4374 {"ESP", _ESP, _r32,}, | |
4375 {"DI", _DI, _r16,}, | |
4376 {"EDI", _EDI, _r32,}, | |
4377 {"SI", _SI, _r16,}, | |
4378 {"ESI", _ESI, _r32,}, | |
4379 {"ES", _ES, _seg | _es,}, | |
4380 {"CS", _CS, _seg | _cs,}, | |
4381 {"SS", _SS, _seg | _ss,}, | |
4382 {"DS", _DS, _seg | _ds,}, | |
4383 {"GS", _GS, _seg | _gs,}, | |
4384 {"FS", _FS, _seg | _fs,}, | |
4385 {"CR0", 0, _special | _crn,}, | |
4386 {"CR2", 2, _special | _crn,}, | |
4387 {"CR3", 3, _special | _crn,}, | |
4388 {"CR4", 4, _special | _crn,}, | |
4389 {"DR0", 0, _special | _drn,}, | |
4390 {"DR1", 1, _special | _drn,}, | |
4391 {"DR2", 2, _special | _drn,}, | |
4392 {"DR3", 3, _special | _drn,}, | |
4393 {"DR4", 4, _special | _drn,}, | |
4394 {"DR5", 5, _special | _drn,}, | |
4395 {"DR6", 6, _special | _drn,}, | |
4396 {"DR7", 7, _special | _drn,}, | |
4397 {"TR3", 3, _special | _trn,}, | |
4398 {"TR4", 4, _special | _trn,}, | |
4399 {"TR5", 5, _special | _trn,}, | |
4400 {"TR6", 6, _special | _trn,}, | |
4401 {"TR7", 7, _special | _trn,}, | |
4402 {"MM0", 0, _mm,}, | |
4403 {"MM1", 1, _mm,}, | |
4404 {"MM2", 2, _mm,}, | |
4405 {"MM3", 3, _mm,}, | |
4406 {"MM4", 4, _mm,}, | |
4407 {"MM5", 5, _mm,}, | |
4408 {"MM6", 6, _mm,}, | |
4409 {"MM7", 7, _mm,}, | |
4410 {"XMM0", 0, _xmm,}, | |
4411 {"XMM1", 1, _xmm,}, | |
4412 {"XMM2", 2, _xmm,}, | |
4413 {"XMM3", 3, _xmm,}, | |
4414 {"XMM4", 4, _xmm,}, | |
4415 {"XMM5", 5, _xmm,}, | |
4416 {"XMM6", 6, _xmm,}, | |
4417 {"XMM7", 7, _xmm,}, | |
4418 ]; | |
4419 | |
4420 REG* asm_reg_lookup(string s) | |
4421 { | |
4422 //dbg_printf("asm_reg_lookup('%s')\n",s); | |
4423 | |
4424 for (int i = 0; i < regtab.length; i++) | |
4425 { | |
4426 if (regtab[i].regstr[0..min(s.length, $)] == s) | |
4427 { | |
4428 return ®tab[i]; | |
4429 } | |
4430 } | |
4431 | |
4432 return null; | |
4433 } | |
4434 | |
4435 int asm_is_fpreg(string szReg) | |
4436 { | |
4437 static if (true) { | |
4438 return(szReg.length == 2 && szReg[0] == 'S' && | |
4439 szReg[1] == 'T'); | |
4440 } else { | |
4441 return(szReg.length == 2 && (szReg[0] == 's' || szReg[0] == 'S') && | |
4442 (szReg[1] == 't' || szReg[1] == 'T')); | |
4443 } | |
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
|
4444 } |