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