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