History

separated i386 code generator

bellard committed 24 years ago

Commit 39173891 parent 9cebd061 file · +531 −0Browse files

Unified · Split

i386-gen.c+531−0
@@ -0,0 +1,531 @@
1/******************************************************/
2/* X86 code generator */
3
4
5/* number of available registers */
6#define NB_REGS 4
7
8#define NB_REG_CLASSES 2
9
10/* a register can belong to several classes */
11#define REG_CLASS_INT 0x0001
12#define REG_CLASS_FLOAT 0x0002
13
14/* pretty names for the registers */
15enum {
16 REG_EAX = 0,
17 REG_ECX,
18 REG_EDX,
19 REG_ST0,
20};
21
22int reg_classes[NB_REGS] = {
23 REG_CLASS_INT, /* eax */
24 REG_CLASS_INT, /* ecx */
25 REG_CLASS_INT, /* edx */
26 REG_CLASS_FLOAT, /* st0 */
27};
28
29/* integer return register for functions */
30#define FUNC_RET_REG 0
31/* float return register for functions */
32#define FUNC_RET_FREG 3
33
34/* defined if function parameters must be evaluated in reverse order */
35#define INVERT_FUNC_PARAMS
36
37/* defined if structures are passed as pointers. Otherwise structures
38 are directly pushed on stack. */
39//#define FUNC_STRUCT_PARAM_AS_PTR
40
41/* function call context */
42typedef struct GFuncContext {
43 int args_size;
44} GFuncContext;
45
46/******************************************************/
47
48void g(int c)
49{
50 *(char *)ind++ = c;
51}
52
53void o(int c)
54{
55 while (c) {
56 g(c);
57 c = c / 256;
58 }
59}
60
61void gen_le32(int c)
62{
63 g(c);
64 g(c >> 8);
65 g(c >> 16);
66 g(c >> 24);
67}
68
69/* add a new relocation entry to symbol 's' */
70void greloc(Sym *s, int addr, int type)
71{
72 Reloc *p;
73 p = malloc(sizeof(Reloc));
74 if (!p)
75 error("memory full");
76 p->type = type;
77 p->addr = addr;
78 p->next = (Reloc *)s->c;
79 s->c = (int)p;
80}
81
82/* patch each relocation entry with value 'val' */
83void greloc_patch(Sym *s, int val)
84{
85 Reloc *p, *p1;
86
87 p = (Reloc *)s->c;
88 while (p != NULL) {
89 p1 = p->next;
90 switch(p->type) {
91 case RELOC_ADDR32:
92 *(int *)p->addr = val;
93 break;
94 case RELOC_REL32:
95 *(int *)p->addr = val - p->addr - 4;
96 break;
97 }
98 free(p);
99 p = p1;
100 }
101 s->c = val;
102 s->t &= ~VT_FORWARD;
103}
104
105/* output a symbol and patch all calls to it */
106void gsym_addr(t, a)
107{
108 int n;
109 while (t) {
110 n = *(int *)t; /* next value */
111 *(int *)t = a - t - 4;
112 t = n;
113 }
114}
115
116void gsym(t)
117{
118 gsym_addr(t, ind);
119}
120
121/* psym is used to put an instruction with a data field which is a
122 reference to a symbol. It is in fact the same as oad ! */
123#define psym oad
124
125/* instruction + 4 bytes data. Return the address of the data */
126int oad(int c, int s)
127{
128 o(c);
129 *(int *)ind = s;
130 s = ind;
131 ind = ind + 4;
132 return s;
133}
134
135/* output constant with relocation if 't & VT_FORWARD' is true */
136void gen_addr32(int c, int t)
137{
138 if (!(t & VT_FORWARD)) {
139 gen_le32(c);
140 } else {
141 greloc((Sym *)c, ind, RELOC_ADDR32);
142 gen_le32(0);
143 }
144}
145
146/* XXX: generate correct pointer for forward references to functions */
147/* r = (ft, fc) */
148void load(int r, int ft, int fc)
149{
150 int v, t;
151
152 v = ft & VT_VALMASK;
153 if (ft & VT_LVAL) {
154 if (v == VT_LLOCAL) {
155 load(r, VT_LOCAL | VT_LVAL, fc);
156 v = r;
157 }
158 if ((ft & VT_BTYPE) == VT_FLOAT) {
159 o(0xd9); /* flds */
160 r = 0;
161 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
162 o(0xdd); /* fldl */
163 r = 0;
164 } else if ((ft & VT_TYPE) == VT_BYTE)
165 o(0xbe0f); /* movsbl */
166 else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED))
167 o(0xb60f); /* movzbl */
168 else if ((ft & VT_TYPE) == VT_SHORT)
169 o(0xbf0f); /* movswl */
170 else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED))
171 o(0xb70f); /* movzwl */
172 else
173 o(0x8b); /* movl */
174
175 if (v == VT_CONST) {
176 o(0x05 + r * 8); /* 0xXX, r */
177 gen_addr32(fc, ft);
178 } else if (v == VT_LOCAL) {
179 oad(0x85 + r * 8, fc); /* xx(%ebp), r */
180 } else {
181 g(0x00 + r * 8 + v); /* (v), r */
182 }
183 } else {
184 if (v == VT_CONST) {
185 o(0xb8 + r); /* mov $xx, r */
186 gen_addr32(fc, ft);
187 } else if (v == VT_LOCAL) {
188 o(0x8d);
189 oad(0x85 + r * 8, fc); /* lea xxx(%ebp), r */
190 } else if (v == VT_CMP) {
191 oad(0xb8 + r, 0); /* mov $0, r */
192 o(0x0f); /* setxx %br */
193 o(fc);
194 o(0xc0 + r);
195 } else if (v == VT_JMP || v == VT_JMPI) {
196 t = v & 1;
197 oad(0xb8 + r, t); /* mov $1, r */
198 oad(0xe9, 5); /* jmp after */
199 gsym(fc);
200 oad(0xb8 + r, t ^ 1); /* mov $0, r */
201 } else if (v != r) {
202 o(0x89);
203 o(0xc0 + r + v * 8); /* mov v, r */
204 }
205 }
206}
207
208/* (ft, fc) = r */
209/* WARNING: r must not be allocated on the stack */
210void store(r, ft, fc)
211{
212 int fr, bt;
213
214 fr = ft & VT_VALMASK;
215 bt = ft & VT_BTYPE;
216 /* XXX: incorrect if reg to reg */
217 if (bt == VT_FLOAT) {
218 o(0xd9); /* fstps */
219 r = 3;
220 } else if (bt == VT_DOUBLE) {
221 o(0xdd); /* fstpl */
222 r = 3;
223 } else {
224 if (bt == VT_SHORT)
225 o(0x66);
226 if (bt == VT_BYTE)
227 o(0x88);
228 else
229 o(0x89);
230 }
231 if (fr == VT_CONST) {
232 o(0x05 + r * 8); /* mov r,xxx */
233 gen_addr32(fc, ft);
234 } else if (fr == VT_LOCAL) {
235 oad(0x85 + r * 8, fc); /* mov r,xxx(%ebp) */
236 } else if (ft & VT_LVAL) {
237 g(fr + r * 8); /* mov r, (fr) */
238 } else if (fr != r) {
239 o(0xc0 + fr + r * 8); /* mov r, fr */
240 }
241}
242
243/* start function call and return function call context */
244void gfunc_start(GFuncContext *c)
245{
246 c->args_size = 0;
247}
248
249/* push function parameter which is in (vt, vc) */
250void gfunc_param(GFuncContext *c)
251{
252 int size, align, ft, fc, r;
253
254 if ((vt & (VT_BTYPE | VT_LVAL)) == (VT_STRUCT | VT_LVAL)) {
255 size = type_size(vt, &align);
256 /* align to stack align size */
257 size = (size + 3) & ~3;
258 /* allocate the necessary size on stack */
259 oad(0xec81, size); /* sub $xxx, %esp */
260 /* generate structure store */
261 r = get_reg(REG_CLASS_INT);
262 o(0x89); /* mov %esp, r */
263 o(0xe0 + r);
264 ft = vt;
265 fc = vc;
266 vset(VT_INT | r, 0);
267 vpush();
268 vt = ft;
269 vc = fc;
270 vstore();
271 c->args_size += size;
272 } else if ((vt & VT_BTYPE) == VT_DOUBLE ||
273 (vt & VT_BTYPE) == VT_FLOAT) {
274 gv(); /* only one float register */
275 if ((vt & VT_BTYPE) == VT_FLOAT)
276 size = 4;
277 else
278 size = 8;
279 oad(0xec81, size); /* sub $xxx, %esp */
280 o(0x245cd9 + size - 4); /* fstp[s|l] 0(%esp) */
281 g(0x00);
282 c->args_size += 8;
283 } else {
284 /* simple type (currently always same size) */
285 /* XXX: implicit cast ? */
286 r = gv();
287 o(0x50 + r); /* push r */
288 c->args_size += 4;
289 }
290}
291
292/* generate function call with address in (vt, vc) and free function
293 context */
294void gfunc_call(GFuncContext *c)
295{
296 int r;
297 if ((vt & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
298 /* constant case */
299 /* forward reference */
300 if (vt & VT_FORWARD) {
301 greloc((Sym *)vc, ind + 1, RELOC_REL32);
302 oad(0xe8, 0);
303 } else {
304 oad(0xe8, vc - ind - 5);
305 }
306 } else {
307 /* otherwise, indirect call */
308 r = gv();
309 o(0xff); /* call *r */
310 o(0xd0 + r);
311 }
312 if (c->args_size)
313 oad(0xc481, c->args_size); /* add $xxx, %esp */
314}
315
316int gjmp(int t)
317{
318 return psym(0xe9, t);
319}
320
321/* generate a test. set 'inv' to invert test */
322int gtst(int inv, int t)
323{
324 int v, *p;
325 v = vt & VT_VALMASK;
326 if (v == VT_CMP) {
327 /* fast case : can jump directly since flags are set */
328 g(0x0f);
329 t = psym((vc - 16) ^ inv, t);
330 } else if (v == VT_JMP || v == VT_JMPI) {
331 /* && or || optimization */
332 if ((v & 1) == inv) {
333 /* insert vc jump list in t */
334 p = &vc;
335 while (*p != 0)
336 p = (int *)*p;
337 *p = t;
338 t = vc;
339 } else {
340 t = gjmp(t);
341 gsym(vc);
342 }
343 } else if ((vt & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
344 /* constant jmp optimization */
345 if ((vc != 0) != inv)
346 t = gjmp(t);
347 } else {
348 v = gv();
349 o(0x85);
350 o(0xc0 + v * 9);
351 g(0x0f);
352 t = psym(0x85 ^ inv, t);
353 }
354 return t;
355}
356
357/* generate an integer binary operation 'v = r op fr' instruction and
358 modifies (vt,vc) if needed */
359void gen_opi(int op, int r, int fr)
360{
361 int t;
362 if (op == '+') {
363 o(0x01);
364 o(0xc0 + r + fr * 8);
365 } else if (op == '-') {
366 o(0x29);
367 o(0xc0 + r + fr * 8);
368 } else if (op == '&') {
369 o(0x21);
370 o(0xc0 + r + fr * 8);
371 } else if (op == '^') {
372 o(0x31);
373 o(0xc0 + r + fr * 8);
374 } else if (op == '|') {
375 o(0x09);
376 o(0xc0 + r + fr * 8);
377 } else if (op == '*') {
378 o(0xaf0f); /* imul fr, r */
379 o(0xc0 + fr + r * 8);
380 } else if (op == TOK_SHL | op == TOK_SHR | op == TOK_SAR) {
381 /* op2 is %ecx */
382 if (fr != 1) {
383 if (r == 1) {
384 r = fr;
385 fr = 1;
386 o(0x87); /* xchg r, %ecx */
387 o(0xc1 + r * 8);
388 } else
389 move_reg(1, fr);
390 }
391 o(0xd3); /* shl/shr/sar %cl, r */
392 if (op == TOK_SHL)
393 o(0xe0 + r);
394 else if (op == TOK_SHR)
395 o(0xe8 + r);
396 else
397 o(0xf8 + r);
398 vt = (vt & VT_TYPE) | r;
399 } else if (op == '/' | op == TOK_UDIV | op == TOK_PDIV |
400 op == '%' | op == TOK_UMOD) {
401 save_reg(2); /* save edx */
402 t = save_reg_forced(fr); /* save fr and get op2 location */
403 move_reg(0, r); /* op1 is %eax */
404 if (op == TOK_UDIV | op == TOK_UMOD) {
405 o(0xf7d231); /* xor %edx, %edx, div t(%ebp), %eax */
406 oad(0xb5, t);
407 } else {
408 o(0xf799); /* cltd, idiv t(%ebp), %eax */
409 oad(0xbd, t);
410 }
411 if (op == '%' | op == TOK_UMOD)
412 r = 2;
413 else
414 r = 0;
415 vt = (vt & VT_TYPE) | r;
416 } else {
417 o(0x39);
418 o(0xc0 + r + fr * 8); /* cmp fr, r */
419 vset(VT_CMP, op);
420 }
421}
422
423/* generate a floating point operation 'v = t1 op t2' instruction and
424 modifies (vt,vc) if needed */
425/* NOTE: floats can only be lvalues */
426void gen_opf(int op)
427{
428 int a, ft, fc, swapped, r;
429
430 /* must put at least one value in the floating point register */
431 if ((vstack_ptr[-4] & VT_LVAL) &&
432 (vstack_ptr[-2] & VT_LVAL)) {
433 vswap();
434 vpop(&vt, &vc);
435 gv();
436 vpush();
437 vswap();
438 }
439 if (op >= TOK_EQ && op <= TOK_GT) {
440 /* load on stack second operand */
441 load(REG_ST0, vstack_ptr[-2], vstack_ptr[-1]);
442 if (op == TOK_GE || op == TOK_GT)
443 o(0xc9d9); /* fxch %st(1) */
444 o(0xe9da); /* fucompp */
445 o(0xe0df); /* fnstsw %ax */
446 if (op == TOK_EQ) {
447 o(0x45e480); /* and $0x45, %ah */
448 o(0x40fC80); /* cmp $0x40, %ah */
449 } else if (op == TOK_NE) {
450 o(0x45e480); /* and $0x45, %ah */
451 o(0x40f480); /* xor $0x40, %ah */
452 op = TOK_NE;
453 } else if (op == TOK_GE || op == TOK_LE) {
454 o(0x05c4f6); /* test $0x05, %ah */
455 op = TOK_EQ;
456 } else {
457 o(0x45c4f6); /* test $0x45, %ah */
458 op = TOK_EQ;
459 }
460 vstack_ptr[-4] = (vstack_ptr[-4] & VT_TYPE) | VT_CMP;
461 vstack_ptr[-3] = op;
462 } else {
463 /* swap the stack if needed so that t1 is the register and t2 is
464 the memory reference */
465 swapped = 0;
466 if (vstack_ptr[-4] & VT_LVAL) {
467 vswap();
468 swapped = 1;
469 }
470
471 switch(op) {
472 default:
473 case '+':
474 a = 0;
475 break;
476 case '-':
477 a = 0x20;
478 if (swapped)
479 a += 8;
480 break;
481 case '*':
482 a = 0x08;
483 break;
484 case '/':
485 a = 0x30;
486 if (swapped)
487 a += 8;
488 break;
489 }
490 ft = vstack_ptr[-2];
491 fc = vstack_ptr[-1];
492 if ((ft & VT_BTYPE) == VT_DOUBLE)
493 o(0xdc);
494 else
495 o(0xd8);
496
497 r = ft & VT_VALMASK;
498 if (r == VT_CONST) {
499 o(0x05 + a);
500 gen_addr32(fc, ft);
501 } else if (r == VT_LOCAL) {
502 oad(0x85 + a, fc);
503 } else {
504 g(0x00 + a + r);
505 }
506 }
507 vstack_ptr -= 2;
508 vpop(&vt, &vc);
509}
510
511/* convert integers to floating point (float or double) */
512void gen_cvtf(int t)
513{
514 if ((vt & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED)) {
515 /* unsigned int to float/double */
516 o(0x6a); /* push $0 */
517 g(0x00);
518 o(0x50 + (vt & VT_VALMASK)); /* push r */
519 o(0x242cdf); /* fildll (%esp) */
520 o(0x08c483); /* add $8, %esp */
521 } else {
522 /* int to float/double */
523 o(0x50 + (vt & VT_VALMASK)); /* push r */
524 o(0x2404db); /* fildl (%esp) */
525 o(0x04c483); /* add $4, %esp */
526 }
527}
528
529/* end of X86 code generator */
530/*************************************************************/
531

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t