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