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