Mercurial > hg > CbC > old > device
annotate mc-nop-386.c @ 52:ddaa1aa5b49b
parallel assignment done (not circular dependency)
author | kono |
---|---|
date | Mon, 17 Feb 2003 13:06:18 +0900 |
parents | c2ef3a2fbe88 |
children | 64a4e3789fd2 |
rev | line source |
---|---|
0 | 1 /* Micro-C Code Generatation Part for intel386 */ |
1 | 2 /* $Id$ */ |
0 | 3 |
4 #define EXTERN extern | |
5 #include "mc.h" | |
6 | |
18 | 7 extern int get_register_var(void); |
8 extern void bexpr(int e1, char cond, int l1); | |
9 extern void closing(void); | |
10 extern void cmpdimm(int e, int csreg); | |
11 extern void code_enter(char *name) ; | |
50 | 12 extern void code_enter1(int args); |
18 | 13 extern void code_init(void); |
50 | 14 extern void code_leave(char *name) ; |
15 extern void leave(int control,char *name) ; | |
18 | 16 extern void def_label(int cslabel, int dlabel) ; |
17 extern void emit_data(int e, int t, NMTBL *n); | |
18 extern void emit_init(void); | |
19 extern void enter(char *name) ; | |
50 | 20 extern void enter1() ; |
18 | 21 extern void g_expr(int e1); |
22 extern void gen_comment(char *s); | |
23 extern void gen_gdecl(char *n, int gpc) ; | |
24 extern void gen_source(char *s); | |
25 extern void gexpr(int e1); | |
26 extern void jcond(int l, char cond); | |
27 extern void jmp(int l); | |
28 extern void opening(char *filename); | |
29 extern void ret(void); | |
49 | 30 extern int assign_expr0(int e,int e1,int type,int type1); |
31 extern int integral(int t); | |
50 | 32 extern void tree_print_t(int e,int t); |
33 extern int rvalue_t(int e,int t); | |
0 | 34 |
42 | 35 static int edx_setup() ; |
18 | 36 static int lvar(int l); |
50 | 37 static int cvar(int l); |
18 | 38 static void jump(int e1, int env); |
39 static void data_mode(char *name); | |
40 static void text_mode(void); | |
41 static void assign(int e1); | |
42 static void assop(int e1); | |
43 static void b_expr(int e1, char cond, int l1); | |
44 static void emit_push(void); | |
45 static void free_register(int i); | |
46 static void function(int e1); | |
47 static void global_table(void) ; | |
48 /* static void indexy(char *op, char *gvar); */ | |
49 /* static void jmp_eq_label(int l); */ | |
50 /* static void jmp_label(int l); */ | |
51 static void ld_indexx(int byte, int n, int xreg); | |
52 static void local_table(void) ; | |
53 static void machinop(int e1); | |
54 static void rexpr(int e1, int l1, char *s); | |
55 static void rindirect(int e1); | |
56 static void sassign(int e1); | |
57 static void shift(char *op, int xreg) ; | |
58 /* static void st_indexx(int byte, int n, int xreg); */ | |
59 static void string(int e1); | |
43 | 60 static void tosop(int op,int reg); |
42 | 61 static void edx_cleanup(); |
26 | 62 static void use_register(int virt, int real, int move); |
47 | 63 static void emit_copy(int from,int to,int length,int offset,int value); |
51 | 64 static int is_same_type(int s,int t); |
18 | 65 |
66 extern int error(int n); | |
67 extern int fwdlabel(void); | |
68 extern void fwddef(int l); | |
69 extern int backdef(void); | |
70 extern int size(int t); | |
49 | 71 extern int list4(int e1, int e2, int e3,int e4); |
18 | 72 extern int list3(int e1, int e2, int e3); |
49 | 73 extern int list2(int e1, int e2); |
2 | 74 extern int scalar(int); |
75 extern int reverse0(int); | |
49 | 76 extern void remove0(int *list,int element); |
0 | 77 |
78 #define TEXT_EMIT_MODE 0 | |
79 #define DATA_EMIT_MODE 1 | |
80 #define RODATA_EMIT_MODE 2 | |
81 | |
18 | 82 static int output_mode = TEXT_EMIT_MODE; |
83 static int data_alignment = 0; | |
0 | 84 |
36 | 85 static int code_disp_label; |
86 static int func_disp_label; | |
87 | |
0 | 88 /* |
50 | 89 -16 local2 |
90 -12 local1 | |
91 -8 arg1 | |
92 -4 arg2 | |
93 0 arg3 | |
94 0 (%edi) | |
95 local1 <----14 local variable 0 (%esi) | |
96 %edi -12 <- disp_offset %ebp | |
97 %esi -8 | |
0 | 98 %ebx -4 |
99 %ebp = %esp 0 | |
100 %eip 4 <- arg_offset | |
101 arg1 8 | |
50 | 102 see enter/enter1/leave see code_enter |
0 | 103 */ |
104 int arg_offset = 8; | |
6 | 105 int code_arg_offset = -4; |
3 | 106 int disp_offset = -12; |
107 int func_disp_offset = -12; | |
108 int code_disp_offset = -4; | |
109 int size_of_int = 4; | |
0 | 110 int endian = 0; |
111 int MAX_REGISTER=6; /* intel386¤Î¥ì¥¸¥¹¥¿¤ò4¤Ä¤Þ¤Ç»È¤¦*/ | |
112 int REAL_MAX_REGISTER=8; /* intel386¤Î¥ì¥¸¥¹¥¿¤¬8¤Ä¤È¤¤¤¦¤³¤È*/ | |
113 int MAX_DATA_REG=4; | |
114 int MAX_POINTER=3; | |
115 int MAX_REGISTGER_VAR=2; | |
116 | |
117 static int creg; /* current register */ | |
118 static int dreg; /* temporary register */ | |
119 static int reg_sp; /* REGister Stack-Pointer */ | |
120 | |
121 | |
122 #define REG_EAX 0 | |
123 #define REG_EBX 1 | |
124 #define REG_ECX 2 | |
125 #define REG_EDX 3 | |
126 #define REG_ESI 4 | |
127 #define REG_EDI 5 | |
128 #define REG_EBP 6 | |
129 #define REG_ESP 7 | |
130 | |
131 | |
132 #define DATA_REG 0 | |
133 #define POINTER_REG 3 | |
134 static char *reg_name[8]; | |
135 static char *reg_name_l[4]; | |
13 | 136 static char *reg_name_w[4]; |
0 | 137 |
138 /* | |
139 creg currrent virtual register | |
140 dreg spare virtual register | |
141 | |
142 rname[creg] currrent real register | |
143 rname[dreg] spare real register | |
144 | |
145 regs[] virtual register usage | |
40 | 146 regv[] value in virtual register flag |
0 | 147 |
148 reg_name[rname[creg]] | |
149 */ | |
150 | |
151 #define MAX_MAX 10 | |
152 static int rname[MAX_MAX]; | |
153 static int regs[MAX_MAX]; /* »È¤ï¤ì¤Æ¤¤¤ë¥ì¥¸¥¹¥¿¤ò¼¨¤¹¥Õ¥é¥° */ | |
154 static int reg_stack[MAX_MAX]; /* ¼ÂºÝ¤Î¥ì¥¸¥¹¥¿¤ÎÎΰè */ | |
40 | 155 static int regv[MAX_MAX]; /* Ãͤ¬Æþ¤Ã¤Æ¤¤¤ë¤«¤É¤¦¤« */ |
0 | 156 |
157 void | |
158 code_init(void) | |
159 { | |
24 | 160 arg_offset = 8; |
161 func_disp_offset = -12; | |
162 disp_offset = -12; | |
163 size_of_int = 4; | |
164 endian = 0; | |
165 MAX_REGISTER=6; | |
166 MAX_DATA_REG=4; | |
167 MAX_POINTER=3; | |
0 | 168 MAX_REGISTER_VAR=2; |
169 | |
170 reg_name[REG_EAX] = "%eax"; | |
171 reg_name[REG_EBX] = "%ebx"; | |
172 reg_name[REG_ECX] = "%ecx"; | |
173 reg_name[REG_EDX] = "%edx"; | |
174 reg_name[REG_ESI] = "%esi"; | |
175 reg_name[REG_EDI] = "%edi"; | |
176 reg_name[REG_EBP] = "%ebp"; | |
177 reg_name[REG_ESP] = "%esp"; | |
178 reg_name_l[REG_EAX] = "%al"; | |
179 reg_name_l[REG_EBX] = "%bl"; | |
180 reg_name_l[REG_ECX] = "%cl"; | |
181 reg_name_l[REG_EDX] = "%dl"; | |
13 | 182 reg_name_w[REG_EAX] = "%ax"; |
183 reg_name_w[REG_EBX] = "%bx"; | |
184 reg_name_w[REG_ECX] = "%cx"; | |
185 reg_name_w[REG_EDX] = "%dx"; | |
0 | 186 |
187 } | |
188 | |
189 char * | |
18 | 190 register_name(int i,int byte) |
0 | 191 { |
38 | 192 if (i<0) { |
193 error(REG_ERR); | |
194 return "%eax"; | |
195 } | |
24 | 196 if (byte && rname[i] <= REG_EDX) { |
18 | 197 return reg_name_l[rname[i]]; |
198 } else { | |
24 | 199 return reg_name[rname[i]]; /* should be error */ |
18 | 200 } |
0 | 201 } |
202 | |
203 int | |
204 get_register(void) | |
205 { /* »È¤ï¤ì¤Æ¤¤¤Ê¤¤¥ì¥¸¥¹¥¿¤òÄ´¤Ù¤ë */ | |
206 int i; | |
207 for(i=0;i<MAX_REGISTER;i++) { | |
208 if (! regs[i]) { /* »È¤ï¤ì¤Æ¤¤¤Ê¤¤¤Ê¤é */ | |
209 regs[i]=1; /* ¤½¤Î¥ì¥¸¥¹¥¿¤ò»È¤¦¤³¤È¤òÀë¸À¤· */ | |
210 return i; /* ¤½¤Î¾ì½ê¤òɽ¤¹ÈÖ¹æ¤òÊÖ¤¹ */ | |
211 } | |
212 } | |
213 return -1; /* ¶õ¤¤¤Æ¤¤¤ë¾ì½ê¤¬¤Ê¤¤¤Ê¤é¡¢¤½¤ì¤òɽ¤¹ -1 ¤òÊÖ¤¹ */ | |
214 } | |
215 | |
216 void | |
217 free_register(int i) { /* ¤¤¤é¤Ê¤¯¤Ê¤Ã¤¿¥ì¥¸¥¹¥¿¤ò³«Êü */ | |
40 | 218 regv[i]=regs[i]=0; |
0 | 219 } |
220 | |
221 int | |
222 register_full(void) | |
223 { | |
224 int i; | |
225 for(i=0;i<MAX_REGISTER;i++) { | |
226 if (! regs[i]) { | |
227 return 0; | |
228 } | |
229 } | |
230 return 1; | |
231 } | |
232 | |
23 | 233 static |
234 int | |
235 free_register_count(void) | |
236 { | |
237 int i,count; | |
238 count = 0; | |
239 for(i=0;i<MAX_REGISTER;i++) { | |
42 | 240 if (! regs[i] && ! regv[i]) count++; |
23 | 241 } |
242 return count; | |
243 } | |
244 | |
42 | 245 static void |
246 free_all_register(void) | |
247 { | |
248 int i; | |
249 for(i=0;i<MAX_REGISTER;i++) { | |
250 regs[i]=regv[i]=0; | |
251 } | |
252 creg = get_register(); | |
253 dreg = get_register(); | |
254 return; | |
255 } | |
256 | |
40 | 257 void |
258 use_register_var(int i) { | |
259 regv[i]=1; | |
260 } | |
261 | |
262 static int creg_regvar = -1; | |
263 static int creg_regvar_back; | |
264 static int creg_back; | |
265 | |
266 void | |
267 creg_destroy() { | |
268 creg_back = creg; creg_regvar_back = creg_regvar; | |
269 if (creg_regvar>=0) | |
270 creg = creg_regvar; | |
271 creg_regvar=-1; | |
272 } | |
273 | |
274 void | |
275 creg_un_destroy() { | |
276 creg = creg_back; creg_regvar = creg_regvar_back; | |
277 } | |
278 | |
42 | 279 void |
280 register_usage(char *s) | |
281 { | |
282 int i; | |
283 printf("# %d: %s:",lineno,s); | |
284 printf(" creg=%s dreg=%s ",register_name(creg,0),register_name(dreg,0)); | |
285 for(i=0;i<MAX_REGISTER;i++) { | |
286 printf("%d",regs[i]); | |
287 } | |
288 printf(":"); | |
289 for(i=0;i<MAX_REGISTER;i++) { | |
290 printf("%d",regv[i]); | |
291 } | |
292 #if 0 | |
293 printf(" regs_stack",register_name(creg,0),register_name(dreg,0)); | |
294 for(i=reg_sp;i>=0;i--) { | |
295 if(reg_stack[i]>=0) | |
296 printf(" %s",register_name(reg_stack[i],0)); | |
297 } | |
298 #endif | |
299 printf("\n"); | |
300 } | |
23 | 301 |
0 | 302 void |
303 gexpr_init(void) | |
304 { | |
305 while(reg_sp > 0) { | |
306 free_register(reg_stack[--reg_sp]); | |
307 } | |
308 text_mode(); | |
26 | 309 use_register(creg,REG_EAX,0); |
42 | 310 regv[dreg]=0; |
40 | 311 creg_regvar = -1; |
42 | 312 register_usage("gexpr_init"); |
0 | 313 } |
314 | |
42 | 315 |
0 | 316 void |
317 emit_init(void) | |
318 { | |
319 int i; | |
40 | 320 for(i=0;i<REAL_MAX_REGISTER;i++) { regs[i]=0; regv[i]=0;rname[i]=i;} |
42 | 321 free_all_register(); |
0 | 322 reg_sp = 0; |
323 text_mode(); | |
324 } | |
325 | |
326 int | |
327 virtual(int real) | |
328 { | |
329 int real_v,i; | |
330 real_v = -1; | |
331 for(i=0;i<MAX_REGISTER;i++) { | |
332 if (rname[i]==real) { | |
333 real_v=i; | |
334 break; | |
335 } | |
336 } | |
23 | 337 /* if (real_v == -1) |
338 error(-1); */ | |
0 | 339 return real_v; |
340 } | |
341 | |
342 void | |
343 use_register(int virt, int real, int move) | |
344 { | |
345 int real_v; | |
346 char *move_op; | |
347 if (rname[virt]==real) | |
348 return; | |
349 real_v = virtual(real); | |
350 move_op = regs[real_v]?"\txchg %s,%s\n":"\tmovl %s,%s\n"; | |
41 | 351 if (move || (regv[real_v])) { |
0 | 352 printf(move_op,reg_name[rname[virt]],reg_name[real]); |
353 } | |
354 rname[real_v] = rname[virt]; | |
355 rname[virt] = real; | |
356 } | |
357 | |
358 void | |
359 use_pointer(int virt, int move) | |
360 { | |
361 int i; | |
362 if (rname[virt]>=POINTER_REG) | |
363 return; | |
364 for(i=POINTER_REG;i<MAX_REGISTER;i++) { | |
365 if (!regs[virtual(i)]) { | |
366 use_register(virt,i,move); | |
367 return; | |
368 } | |
369 } | |
370 /* we prefer EBX */ | |
371 use_register(virt,REG_EBX,move); | |
372 } | |
373 | |
374 void | |
375 use_data_reg(int virt, int move) | |
376 { | |
377 int i; | |
378 if (rname[virt]<MAX_DATA_REG) | |
379 return; | |
380 for(i=0;i<MAX_DATA_REG;i++) { | |
381 if (!regs[virtual(i)]) { | |
382 use_register(virt,i,move); | |
383 return; | |
384 } | |
385 } | |
386 /* we prefer EBX */ | |
387 use_register(virt,REG_EBX,move); | |
388 } | |
389 | |
390 int | |
391 pop_register(void) | |
392 { /* ¥ì¥¸¥¹¥¿¤«¤éÃͤò¼è¤ê½Ð¤¹ */ | |
42 | 393 return reg_stack[--reg_sp]; |
0 | 394 } |
395 | |
396 int | |
397 stack_used(void) { | |
398 return reg_stack[--reg_sp]<0; | |
399 } | |
400 | |
401 void | |
402 emit_push(void) | |
403 { | |
404 int new_reg; | |
405 new_reg = get_register(); | |
406 if(new_reg<0) { /* ¤â¤¦¥ì¥¸¥¹¥¿¤¬¤Ê¤¤ */ | |
407 reg_stack[reg_sp++] = -1; | |
18 | 408 printf("\tpushl %s\n",register_name(creg,0)); |
42 | 409 /* creg is used soon, don't regv[creg]=0 */ |
0 | 410 } else { |
411 reg_stack[reg_sp++] = creg; /* push ¤¹¤ë¤«¤ï¤ê¤Ë¥ì¥¸¥¹¥¿¤ò»È¤¦ */ | |
18 | 412 creg = new_reg; |
42 | 413 regv[creg]=1; |
0 | 414 } |
42 | 415 } |
416 | |
417 void | |
418 emit_push_x(int xreg) | |
419 { | |
420 int new_reg; | |
421 new_reg = get_register(); | |
422 if(new_reg<0) { /* ¤â¤¦¥ì¥¸¥¹¥¿¤¬¤Ê¤¤ */ | |
423 reg_stack[reg_sp++] = -1; | |
424 printf("\tpushl %s\n",register_name(xreg,0)); | |
425 /* creg is used soon, don't regv[xreg]=0 */ | |
426 } else { | |
427 reg_stack[reg_sp++] = xreg; /* push ¤¹¤ë¤«¤ï¤ê¤Ë¥ì¥¸¥¹¥¿¤ò»È¤¦ */ | |
428 xreg = new_reg; | |
429 regv[xreg]=1; | |
430 } | |
0 | 431 } |
432 | |
18 | 433 int |
0 | 434 emit_pop(int type) |
435 { | |
18 | 436 int xreg; |
42 | 437 if ((xreg=pop_register())==-1) { |
0 | 438 if (type==POINTER_REG) |
439 use_pointer(dreg,0); | |
440 else if (type==DATA_REG) | |
441 use_data_reg(dreg,0); | |
42 | 442 if (regv[dreg]) { |
443 printf("# emit_pop dreg conflict\n"); | |
444 } | |
18 | 445 printf("\tpopl %s\n",register_name(dreg,0)); |
0 | 446 xreg = dreg; |
42 | 447 regv[xreg]=1; |
448 } | |
449 return xreg; | |
450 } | |
451 | |
452 static void | |
453 emit_pop_free(int xreg) | |
454 { | |
455 if (xreg==dreg) { | |
456 regv[dreg]=0; | |
43 | 457 } else if (xreg!=-1) { |
42 | 458 free_register(xreg); |
0 | 459 } |
460 } | |
461 | |
462 int | |
463 get_register_var(void) | |
464 { | |
465 int i; | |
466 for(i=REG_ESI;i<REG_ESP;i++) { | |
467 if (! regs[i]) { /* »È¤ï¤ì¤Æ¤¤¤Ê¤¤¤Ê¤é */ | |
468 regs[i]=1; /* ¤½¤Î¥ì¥¸¥¹¥¿¤ò»È¤¦¤³¤È¤òÀë¸À¤· */ | |
41 | 469 regv[i]=0; |
0 | 470 return i; /* ¤½¤Î¾ì½ê¤òɽ¤¹ÈÖ¹æ¤òÊÖ¤¹ */ |
471 } | |
472 } | |
473 return -1; | |
474 } | |
475 | |
476 void | |
477 gexpr(int e1) | |
478 { | |
479 gexpr_init(); | |
24 | 480 #if 0 |
32 | 481 if(lineno==2862) { |
24 | 482 g_expr(e1); /*break here*/ |
483 return; | |
484 } | |
485 #endif | |
0 | 486 g_expr(e1); |
42 | 487 } |
488 | |
489 int | |
490 csvalue() | |
491 { | |
492 return rname[creg]; /* for siwtch value */ | |
0 | 493 } |
494 | |
495 void | |
496 g_expr(int e1) | |
497 { | |
22 | 498 int e2,e3/*,e4*/; |
0 | 499 char *xrn; |
500 NMTBL *n; | |
501 | |
502 if (chk) return; | |
503 e2 = cadr(e1); | |
504 switch (car(e1)){ | |
505 case GVAR: | |
506 /* use_pointer(creg,0); */ | |
18 | 507 printf("\tmovl $%s,%s\n",(char *)caddr(e1),register_name(creg,0)); |
42 | 508 regv[creg]=1; |
0 | 509 return; |
510 case RGVAR: | |
511 /* use_pointer(creg,0); */ | |
18 | 512 printf("\tmovl %s,%s\n",(char *)caddr(e1),register_name(creg,0)); |
42 | 513 regv[creg]=1; |
0 | 514 return; |
515 case CRGVAR: | |
27 | 516 printf("\tmovsbl %s,%s\n",(char *)caddr(e1),register_name(creg,0)); |
42 | 517 regv[creg]=1; |
0 | 518 return; |
519 case LVAR: | |
520 /* use_pointer(creg,0); */ | |
18 | 521 printf("\tlea %d(%%ebp),%s\n",lvar(e2),register_name(creg,0)); |
42 | 522 regv[creg]=1; |
0 | 523 return; |
524 case REGISTER: | |
525 /* this is of course redundant... */ | |
18 | 526 /* we can use rname for this? */ |
527 /* or why not creg=e2? */ | |
528 printf("\tmovl %s,%s\n",register_name(e2,0),register_name(creg,0)); | |
42 | 529 regv[creg]=1; |
0 | 530 return; |
531 case RLVAR: | |
18 | 532 printf("\tmovl %d(%%ebp),%s\n",lvar(e2),register_name(creg,0)); |
42 | 533 regv[creg]=1; |
0 | 534 return; |
535 case CRLVAR: | |
21 | 536 printf("\tmovsbl %d(%%ebp),%s\n",lvar(e2),register_name(creg,0)); |
42 | 537 regv[creg]=1; |
0 | 538 return; |
539 case FNAME: | |
18 | 540 printf("\tmovl $%s,%s\n",((NMTBL *)e2)->nm,register_name(creg,0)); |
42 | 541 regv[creg]=1; |
0 | 542 return; |
543 case CONST: /* ÂåÆþ¤¹¤ëÃͤ¬0¤Ç¤âÆÃÊ̤ʽèÍý¤Ï¤·¤Ê¤¤ */ | |
18 | 544 printf("\tmovl $%d,%s\n",e2,register_name(creg,0)); |
42 | 545 regv[creg]=1; |
0 | 546 return; |
547 case STRING: | |
548 string(e1); | |
42 | 549 regv[creg]=1; |
0 | 550 return; |
551 case FUNCTION: | |
552 function(e1); | |
42 | 553 regv[creg]=1; |
0 | 554 return; |
555 case CODE: | |
556 jump(e2,caddr(e1)); | |
557 return; | |
558 case INDIRECT: | |
559 g_expr(e2); | |
560 return; | |
561 case RINDIRECT: case CRINDIRECT: | |
562 rindirect(e1); | |
563 return; | |
564 case ADDRESS: | |
565 g_expr(e2); | |
566 return; | |
567 case MINUS: /* ¥ì¥¸¥¹¥¿¤ËÂФ·¡¢negl¤ò¼Â¹Ô¤¹¤ì¤Ð¼Â¸½²Äǽ */ | |
568 g_expr(e2); | |
569 /* use_data_reg(creg,1); */ | |
18 | 570 printf("\tnegl %s\n", register_name(creg,0)); |
0 | 571 return; |
572 case BNOT: /* ~ */ | |
573 g_expr(e2); | |
574 /* use_data_reg(creg,1); */ | |
18 | 575 printf("\tnotl %s\n", register_name(creg,0)); |
0 | 576 return; |
577 case LNOT: /* ! */ | |
578 g_expr(e2); | |
579 use_data_reg(creg,1); | |
18 | 580 xrn = register_name(creg,1); |
581 printf("\tcmpl $0,%s\n", register_name(creg,0)); | |
582 printf("\tsete %s\n", xrn); | |
583 printf("\tmovzbl %s,%s\n", xrn,register_name(creg,0)); | |
0 | 584 return; |
585 case PREINC: | |
586 if (car(e2)==REGISTER) { | |
18 | 587 printf("\taddl $%d,%s\n",caddr(e1),register_name(cadr(e2),0)); |
588 printf("\tmovl %s,%s\n",register_name(cadr(e2),0),register_name(creg,0)); | |
0 | 589 return; |
590 } | |
591 g_expr(e2); | |
18 | 592 xrn = register_name(creg,0); |
593 printf("\taddl $%d,(%s)\n",caddr(e1),xrn); | |
594 printf("\tmovl (%s),%s\n",xrn,xrn); | |
0 | 595 return; |
596 case POSTINC: | |
597 if (car(e2)==REGISTER) { | |
18 | 598 printf("\tmovl %s,%s\n",register_name(cadr(e2),0),register_name(creg,0)); |
599 printf("\taddl $%d,%s\n",caddr(e1),register_name(cadr(e2),0)); | |
0 | 600 return; |
601 } | |
602 g_expr(e2); | |
603 emit_push(); | |
41 | 604 xrn = register_name((e2=emit_pop(0)),0); |
18 | 605 printf("\tmovl (%s),%s\n",xrn,register_name(creg,0)); |
0 | 606 printf("\taddl $%d,(%s)\n",caddr(e1),xrn); |
42 | 607 emit_pop_free(e2); |
0 | 608 return; |
609 case CPOSTINC: | |
610 /* char *p; *p++ */ | |
611 if (car(e2)==REGISTER) { | |
18 | 612 printf("\tmovl %s,%s\n",register_name(cadr(e2),0),register_name(creg,0)); |
613 printf("\taddl $%d,%s\n",caddr(e1),register_name(cadr(e2),0)); | |
0 | 614 return; |
615 } | |
616 g_expr(e2); | |
42 | 617 emit_push(); |
41 | 618 xrn = register_name((e2=emit_pop(0)),1); |
18 | 619 printf("\tmovsbl (%s),%s\n",xrn,register_name(creg,0)); |
0 | 620 printf("\tincl (%s)\n",xrn); |
42 | 621 emit_pop_free(e2); |
0 | 622 return; |
623 case CPREINC: | |
624 if (car(e2)==REGISTER) { | |
18 | 625 printf("\taddl $%d,%s\n",caddr(e1),register_name(cadr(e2),0)); |
626 printf("\tmovl %s,%s\n",register_name(cadr(e2),0),register_name(creg,0)); | |
0 | 627 return; |
628 } | |
629 g_expr(e2); | |
18 | 630 printf("\tincl (%s)\n",register_name(creg,0)); |
21 | 631 printf("\tmovsbl (%s),%s\n",register_name(creg,0),register_name(creg,0)); |
0 | 632 return; |
633 case CPOSTDEC: | |
634 if (car(e2)==REGISTER) { | |
18 | 635 printf("\tmovl %s,%s\n",register_name(cadr(e2),0),register_name(creg,0)); |
636 printf("\tdecl %s\n",register_name(cadr(e2),0)); | |
0 | 637 return; |
638 } | |
639 g_expr(e2); | |
21 | 640 printf("\tmovsbl (%s),%s\n",register_name(creg,0),register_name(creg,0)); |
18 | 641 printf("\tdecl (%s)\n",register_name(creg,0)); |
0 | 642 return; |
643 case CPREDEC: | |
644 if (car(e2)==REGISTER) { | |
18 | 645 printf("\tdecl %s\n",register_name(cadr(e2),0)); |
646 printf("\tmovl %s,%s\n",register_name(cadr(e2),0),register_name(creg,0)); | |
0 | 647 return; |
648 } | |
649 g_expr(e2); | |
650 emit_push(); | |
18 | 651 e2 = emit_pop(0); |
652 printf("\tdecl (%s)\n",register_name(e2,0)); | |
21 | 653 printf("\tmovsbl (%s),%s\n",register_name(e2,0),register_name(creg,0)); |
42 | 654 emit_pop_free(e2); |
0 | 655 return; |
656 case MUL: case UMUL: | |
657 case DIV: case UDIV: | |
658 case MOD: case UMOD: | |
659 case LSHIFT: case ULSHIFT: case RSHIFT: case URSHIFT: | |
660 case ADD: case SUB: case BAND: case EOR: case BOR: | |
661 machinop(e1); | |
662 return; | |
663 case COND: | |
664 e2=fwdlabel(); | |
665 b_expr(cadr(e1),0,e2); | |
22 | 666 use_register(creg,REG_EAX,0); |
0 | 667 g_expr(caddr(e1)); |
22 | 668 /* e4 = rname[creg]; this is a bad idea */ |
669 use_register(creg,REG_EAX,1); | |
0 | 670 jmp(e3=fwdlabel()); |
671 fwddef(e2); | |
22 | 672 use_register(creg,REG_EAX,0); |
0 | 673 g_expr(cadddr(e1)); |
22 | 674 /* use_register(creg,e4,1); */ |
675 use_register(creg,REG_EAX,1); | |
0 | 676 fwddef(e3); |
677 return; | |
37 | 678 case RSTRUCT: |
679 g_expr(e2); | |
680 return; | |
0 | 681 case SASS: |
682 sassign(e1); | |
683 return; | |
684 case ASS: case CASS: | |
685 assign(e1); | |
686 return; | |
687 case ASSOP: case CASSOP: | |
688 assop(e1); | |
689 return; | |
690 case COMMA: | |
691 g_expr(e2); | |
692 g_expr(caddr(e1)); | |
693 return; | |
694 case RETURN: | |
695 n = (NMTBL *)e2; | |
696 if (retcont==0) | |
697 retcont=fwdlabel(); | |
18 | 698 printf("\tleal _%d,%s\n",retcont,register_name(creg,0)); |
42 | 699 regv[creg]=1; |
0 | 700 return; |
701 case ENVIRONMENT: | |
18 | 702 printf("\tmovl %%ebp,%s\n",register_name(creg,0)); |
42 | 703 regv[creg]=1; |
0 | 704 return; |
705 default: | |
706 b_expr(e1,1,e2=fwdlabel()); /* including > < ... */ | |
18 | 707 xrn = register_name(creg,0); |
708 printf("\txorl %s,%s\n",xrn,xrn); | |
0 | 709 jmp(e3=fwdlabel()); |
710 fwddef(e2); | |
18 | 711 printf("\tmovl $1,%s\n",xrn); |
0 | 712 fwddef(e3); |
42 | 713 regv[creg]=1; |
0 | 714 } |
715 } | |
716 | |
717 void | |
718 bexpr(int e1, char cond, int l1) | |
719 { | |
720 gexpr_init(); | |
721 b_expr(e1,cond,l1); | |
722 } | |
723 | |
724 void | |
725 b_expr(int e1, char cond, int l1) | |
726 { | |
727 int e2,l2; | |
728 if (chk) return; | |
729 e2=cadr(e1); | |
730 switch(car(e1)) { | |
731 case LNOT: | |
732 b_expr(e2,!cond,l1); | |
733 return; | |
734 case GT: | |
735 rexpr(e1,l1,cond?"g":"le"); | |
736 return; | |
737 case UGT: | |
738 rexpr(e1,l1,cond?"a":"be"); | |
739 return; | |
740 case GE: | |
741 rexpr(e1,l1,cond?"ge":"l"); | |
742 return; | |
743 case UGE: | |
744 rexpr(e1,l1,cond?"ae":"b"); | |
745 return; | |
746 case LT: | |
747 rexpr(e1,l1,cond?"l":"ge"); | |
748 return; | |
749 case ULT: | |
750 rexpr(e1,l1,cond?"b":"ae"); | |
751 return; | |
752 case LE: | |
753 rexpr(e1,l1,cond?"le":"g"); | |
754 return; | |
755 case ULE: | |
756 rexpr(e1,l1,cond?"be":"a"); | |
757 return; | |
758 case EQ: | |
759 rexpr(e1,l1,cond?"e":"ne"); | |
760 return; | |
761 case NEQ: | |
762 rexpr(e1,l1,cond?"ne":"e"); | |
763 return; | |
764 case LAND: | |
765 b_expr(e2,0,cond?(l2=fwdlabel()):l1); | |
766 b_expr(caddr(e1),cond,l1); | |
767 if(cond) fwddef(l2); | |
768 return; | |
769 case LOR: | |
770 b_expr(e2,1,cond?l1:(l2=fwdlabel())); | |
771 b_expr(caddr(e1),cond,l1); | |
772 if(!cond) fwddef(l2); | |
773 return; | |
774 case CRGVAR: | |
18 | 775 printf("\tcmpb $0,%s\n",(char *)caddr(e1)); |
0 | 776 jcond(l1,cond); |
777 return; | |
778 case CRLVAR: | |
779 printf("\tcmpb $0,%d(%%ebp)\n",lvar(e2)); | |
780 jcond(l1,cond); | |
781 return; | |
782 case RGVAR: | |
18 | 783 printf("\tcmpl $0,%s\n",(char *)caddr(e1)); |
0 | 784 jcond(l1,cond); |
785 return; | |
786 case RLVAR: | |
787 printf("\tcmpl $0,%d(%%ebp)\n",lvar(e2)); | |
788 jcond(l1,cond); | |
789 return; | |
790 case REGISTER: | |
18 | 791 printf("\tcmpl $0,%s\n",register_name(e2,0)); |
0 | 792 jcond(l1,cond); |
793 return; | |
794 case CONST: | |
795 if((cond&&e2)||(!cond&&!e2)) jmp(l1); | |
796 return; | |
797 default: | |
798 g_expr(e1); | |
18 | 799 printf("\tcmpl $0,%s\n",register_name(creg,0)); |
0 | 800 jcond(l1,cond); |
801 return; | |
802 } | |
803 } | |
804 | |
805 void | |
806 ascii(char *s) | |
807 { | |
808 printf("\t.string \""); | |
809 while(*s) { | |
810 if (*s=='\n') | |
811 printf("%cn",92); | |
812 else if (*s<' ') | |
813 printf("%c%03o",92,*s); | |
814 else if (*s==34) | |
815 printf("%c%c",92,34); | |
816 else | |
817 printf("%c",*s); | |
818 s++; | |
819 } | |
820 printf("%c\n",34); | |
821 } | |
822 | |
823 void | |
824 string(int e1) | |
825 { | |
826 char *s; | |
827 int i,lb; | |
828 | |
829 if (0) { | |
830 s=(char *)cadr(e1); | |
831 lb=fwdlabel(); | |
832 printf("\tjmp _%d\n",lb); | |
833 i=backdef(); | |
834 ascii(s); | |
835 printf("\t.align 2\n"); | |
836 fwddef(lb); | |
18 | 837 printf("\tlea _%d,%s\n",i,register_name(creg,0)); |
0 | 838 } else { |
839 s=(char *)cadr(e1); | |
840 printf(".section\t.rodata\n"); | |
841 lb=fwdlabel(); | |
842 printf("_%d:\n",lb); | |
843 ascii(s); | |
844 if (output_mode==TEXT_EMIT_MODE) { | |
845 printf(".text\n"); | |
846 } else { | |
847 text_mode(); | |
848 } | |
18 | 849 printf("\tlea _%d,%s\n",lb,register_name(creg,0)); |
0 | 850 } |
851 } | |
852 | |
13 | 853 #define MAX_COPY_LEN 20 |
854 | |
855 void | |
47 | 856 emit_copy(int from,int to,int length,int offset,int value) |
13 | 857 { |
39 | 858 int fix = 0; |
859 if (length<=0) return; | |
13 | 860 switch (length) { |
861 case 0: break; | |
862 case 1: | |
38 | 863 printf("\tmovb %d(%s),%s\n",offset,register_name(from,0), reg_name_l[rname[dreg]] ); |
864 printf("\tmovb %s,%d(%s)\n",reg_name_l[rname[dreg]] ,offset,register_name(to,0)); | |
13 | 865 break; |
866 case 2: | |
38 | 867 printf("\tmovw %d(%s),%s\n",offset,register_name(from,0), reg_name_w[rname[dreg]] ); |
868 printf("\tmovw %s,%d(%s)\n",reg_name_w[rname[dreg]] ,offset,register_name(to,0)); | |
13 | 869 break; |
870 case 4: | |
38 | 871 printf("\tmovl %d(%s),%s\n",offset,register_name(from,0), register_name(dreg,0)); |
872 printf("\tmovl %s,%d(%s)\n",register_name(dreg,0), offset,register_name(to,0)); | |
13 | 873 break; |
874 default: | |
875 if (length <=MAX_COPY_LEN) { | |
14 | 876 for(;length>=4;length-=4,offset+=4) |
47 | 877 emit_copy(from,to,4,offset,0); |
14 | 878 for(;length>=2;length-=2,offset+=2) |
47 | 879 emit_copy(from,to,2,offset,0); |
13 | 880 if(length>0) |
47 | 881 emit_copy(from,to,length,offset,0); |
39 | 882 break; |
13 | 883 } |
39 | 884 use_register(from,REG_ESI,1); |
885 use_register(to, REG_EDI,1); | |
886 use_register(dreg,REG_ECX,0); | |
14 | 887 printf("\tmovl $%d,%%ecx\n",length/4); |
39 | 888 fix = (length/4)*4; |
13 | 889 printf("\tcld\n\trep\n\tmovsl\n"); |
14 | 890 if(length%4) { |
47 | 891 emit_copy(from,to,length,offset+length/4,0); |
14 | 892 } |
13 | 893 } |
47 | 894 if (value) { |
39 | 895 /* this code is necessary for the value of assignment or function call */ |
896 /* otherwise we don't need this */ | |
47 | 897 if (fix) printf("\tsubl $%d,%s\n",fix,register_name(to,0)); |
898 if(creg!=to) { | |
899 if (to==dreg) | |
900 printf("\tmovl %s,%s\n",register_name(to,0),register_name(creg,0)); | |
901 else { | |
902 free_register(creg); creg=to; | |
903 } | |
45 | 904 } |
39 | 905 } |
41 | 906 regv[from]=regv[to]=regv[dreg]=0; |
42 | 907 regv[creg]=1; |
13 | 908 } |
909 | |
910 int | |
911 struct_push(int e4,int t) | |
912 { | |
42 | 913 int length,xreg,save,lreg; |
13 | 914 g_expr(e4); |
14 | 915 length=size(t); |
916 if(length%size_of_int) { | |
917 length += size_of_int - (length%size_of_int); | |
13 | 918 } |
40 | 919 if (length==4) { |
920 printf("\tpushl (%s)\n",register_name(creg,0)); return 1; | |
921 } | |
922 if (length==8) { | |
923 printf("\tpushl 4(%s)\n",register_name(creg,0)); | |
924 printf("\tpushl (%s)\n",register_name(creg,0)); return 2; | |
925 } | |
38 | 926 /* I think, after g_expr, at least two registers are free */ |
14 | 927 printf("\tsubl $%d,%%esp\n",length); |
38 | 928 if (register_full()) { |
929 /* this is wrong assumption */ | |
930 save = 1; | |
46
b1c8ac8c308d
fix cascading struct retrun. Now it should be compatible with gcc
kono
parents:
45
diff
changeset
|
931 for(lreg=0;lreg==creg||lreg==dreg;lreg++); |
38 | 932 printf("\tpushl %s\n",register_name(lreg,0)); |
46
b1c8ac8c308d
fix cascading struct retrun. Now it should be compatible with gcc
kono
parents:
45
diff
changeset
|
933 xreg = lreg; regv[xreg]=0; |
38 | 934 } else { |
935 save=0; | |
936 xreg = get_register(); | |
13 | 937 } |
46
b1c8ac8c308d
fix cascading struct retrun. Now it should be compatible with gcc
kono
parents:
45
diff
changeset
|
938 if (save) |
b1c8ac8c308d
fix cascading struct retrun. Now it should be compatible with gcc
kono
parents:
45
diff
changeset
|
939 printf("\tlea %d(%%esp),%s\n",size_of_int,register_name(xreg,0)); |
b1c8ac8c308d
fix cascading struct retrun. Now it should be compatible with gcc
kono
parents:
45
diff
changeset
|
940 else |
b1c8ac8c308d
fix cascading struct retrun. Now it should be compatible with gcc
kono
parents:
45
diff
changeset
|
941 printf("\tmovl %%esp,%s\n",register_name(xreg,0)); |
41 | 942 regv[xreg]=1; |
47 | 943 emit_copy(creg,xreg,length,0,0); |
46
b1c8ac8c308d
fix cascading struct retrun. Now it should be compatible with gcc
kono
parents:
45
diff
changeset
|
944 /* we have value in creg, it may be changed */ |
38 | 945 if (save) { |
46
b1c8ac8c308d
fix cascading struct retrun. Now it should be compatible with gcc
kono
parents:
45
diff
changeset
|
946 if(creg==xreg) { |
b1c8ac8c308d
fix cascading struct retrun. Now it should be compatible with gcc
kono
parents:
45
diff
changeset
|
947 creg = get_register(); /* creg is freed in emit_copy */ |
b1c8ac8c308d
fix cascading struct retrun. Now it should be compatible with gcc
kono
parents:
45
diff
changeset
|
948 } |
b1c8ac8c308d
fix cascading struct retrun. Now it should be compatible with gcc
kono
parents:
45
diff
changeset
|
949 printf("\tpopl %s\n",register_name(xreg,0)); |
b1c8ac8c308d
fix cascading struct retrun. Now it should be compatible with gcc
kono
parents:
45
diff
changeset
|
950 regv[xreg]=1; |
38 | 951 } else |
952 free_register(xreg); | |
14 | 953 return length/size_of_int; |
13 | 954 } |
955 | |
0 | 956 void |
957 function(int e1) | |
958 { | |
13 | 959 int e2,e3,e4,e5,nargs,t; |
0 | 960 NMTBL *n; |
23 | 961 int save,saved; |
962 if (free_register_count()<1) { | |
963 for(save = 0;save==dreg||save==creg;save++); | |
964 printf("\tpushl %s\n",register_name(save,0)); | |
965 saved = 1; | |
966 } else { | |
967 save = get_register(); | |
968 saved = 0; | |
969 } | |
41 | 970 regv[save]=0; |
0 | 971 e2 = cadr(e1); |
972 nargs = 0; | |
973 for (e3 = caddr(e1); e3; e3 = cadr(e3)) { | |
13 | 974 t=caddr(e3); |
0 | 975 n=(NMTBL *)(e5=(cadr(e4 = car(e3)))); |
976 switch(car(e4)) { | |
50 | 977 /* |
0 | 978 case FNAME: |
18 | 979 printf("\tlea %s,%s\n",n->nm,register_name(creg,0)); |
980 printf("\tpushl %s\n",register_name(creg,0)); | |
0 | 981 break; |
982 case ADDRESS: | |
983 g_expr(e5); | |
18 | 984 printf("\tpushl %s\n",register_name(creg,0)); |
0 | 985 break; |
50 | 986 */ |
13 | 987 default: |
988 if(scalar(t)) { | |
989 g_expr(e4); | |
18 | 990 printf("\tpushl %s\n",register_name(creg,0)); |
13 | 991 } else if (car(t)==STRUCT||car(t)==UNION) { |
992 nargs += struct_push(e4,t); | |
993 continue; | |
994 } else { | |
995 error(TYERR); | |
996 } | |
0 | 997 } |
998 ++nargs; | |
999 } | |
1000 if (car(e2) == FNAME) { | |
1001 n=(NMTBL *)cadr(e2); | |
23 | 1002 } else { |
44 | 1003 use_register(creg,REG_EAX,0); |
23 | 1004 g_expr(e2); |
1005 } | |
1006 | |
26 | 1007 /* we don't have to save creg nor dreg */ |
41 | 1008 regs[creg]=0; regs[dreg]=0; |
1009 regv[creg]= regv[dreg]= regv[save]= 0; | |
23 | 1010 use_register(creg,REG_EAX,0); |
1011 use_register(dreg,REG_EDX,0); /* will be destroyed */ | |
1012 use_register(save,REG_ECX,0); /* will be destroyed */ | |
41 | 1013 regs[creg]=1; regs[dreg]=1; |
23 | 1014 |
1015 if (car(e2) == FNAME) { | |
0 | 1016 printf("\tcall\t%s\n",n->nm); |
23 | 1017 } else { |
18 | 1018 printf("\tcall\t*%s\n",register_name(creg,0)); |
0 | 1019 } |
6 | 1020 if (nargs) printf("\taddl $%d,%%esp\n",size_of_int*nargs); |
23 | 1021 if (saved) { |
1022 printf("\tpopl %s\n",register_name(save,0)); | |
1023 } else { | |
1024 free_register(save); | |
1025 } | |
41 | 1026 regv[save]=0; |
1027 regv[creg]=1; | |
0 | 1028 } |
1029 | |
51 | 1030 /* goto arguments list */ |
52 | 1031 /* target list4(list2(tag,disp),cdr,ty,source_expr) */ |
51 | 1032 /* source expr=listn(tag,...) */ |
1033 /* source (after) list2(tag,disp) */ | |
52 | 1034 /* source list list3(e,cdr,sz) */ |
48 | 1035 |
1036 int | |
52 | 1037 overrap(int t,int sz,int source) |
48 | 1038 { |
52 | 1039 int s,s0,s1; |
1040 int t0=cadr(t); | |
1041 int t1=t0+sz; | |
48 | 1042 for(;source;source=cadr(source)) { |
52 | 1043 s=car(source); s0=cadr(s); |
1044 if(car(s)==REGISTER && car(t)==REGISTER) { | |
48 | 1045 if(s0==t0) return 1; |
52 | 1046 } else if (is_same_type(s,t)) { |
1047 s1=s0+caddr(source); | |
1048 #if 0 | |
51 | 1049 printf("# ovedrrap source %d t0 %d t1 %d\n",car(car(t)),t0,t1); |
1050 printf("# ovedrrap target %d s0 %d s1 %d\n",car(car(source)),s0,s1); | |
1051 printf("# ovedrrap equal = %d\n",((t0<=s0&&s0<=t1)||(t0<=s1&&s1<=t1))); | |
52 | 1052 #endif |
48 | 1053 if((t0<=s0&&s0<=t1)||(t0<=s1&&s1<=t1)) return 1; |
1054 } | |
1055 } | |
1056 return 0; | |
1057 } | |
1058 | |
51 | 1059 void |
1060 parallel_assign_post(int e1) { | |
1061 int ce1; | |
1062 while(e1) { | |
1063 ce1=car(e1); | |
1064 if (ce1) { | |
1065 if(car(ce1)) | |
1066 g_expr(car(ce1)); | |
52 | 1067 #if 1 |
1068 printf("# post process ty %d+%d\n",cadr(car(ce1)),caddr(car(ce1))); | |
1069 #endif | |
51 | 1070 if(cadr(ce1)>=0) { |
1071 free_register(cadr(ce1)); | |
1072 } | |
1073 } | |
1074 e1=cadr(e1); | |
1075 } | |
1076 } | |
1077 | |
48 | 1078 int |
51 | 1079 parallel_assign(int *target,int *source,int *processing) |
48 | 1080 { |
49 | 1081 int t,s,e1,p,sz,ty; |
48 | 1082 while(*target) { |
51 | 1083 t=car(*target); s=cadddr(*target); |
49 | 1084 sz=size(ty=caddr(*target)); |
52 | 1085 if(car(t)==car(s) && cadr(t)==cadr(s)) { |
1086 #if 1 | |
1087 printf("# duplicate same target check (you shouldn't see this) \n# %d ty %d+%d sz %d\n",car(t),ty,cadr(t),sz); | |
1088 #endif | |
1089 /*½ñ¤¹þ¤ßÀ褬¼«Ê¬¼«¿È*/ | |
1090 remove0(target,t); | |
1091 /* Ç˲õ¤µ¤ì¤Æ¤Ïº¤¤ë¤Î¤Ç¡¢source list¤«¤é¤Ï½ü¤«¤Ê¤¤ */ | |
1092 continue; | |
1093 } | |
48 | 1094 for(p=*processing;p;p=cadr(p)) { |
1095 if(car(p)==t) { /*¥¿¡¼¥²¥Ã¥È¤¬½èÍý¥ê¥¹¥ÈÃæ¤Ë¤¢¤ë*/ | |
51 | 1096 |
1097 /* ¤É¤±¤Æ¤·¤Þ¤¨¤Ð¡¢¤â¤¦´Ø·¸¤Ê¤¤¡£ÆóÅ٤ȽèÍý¤¹¤ëɬÍפâ¤Ê¤¤¡£*/ | |
49 | 1098 remove0(target,t); remove0(source,s); remove0(processing,p); |
52 | 1099 #if 1 |
1100 printf("# circular dependcy %d ty %d+%d sz %d\n",car(t),ty,cadr(t),sz); | |
1101 #endif | |
48 | 1102 /*¿·¤·¤¤¥ì¥¸¥¹¥¿(or ¥¹¥¿¥Ã¥¯)¤ò¼èÆÀ¤¹¤ë*/ |
49 | 1103 if (sz==size_of_int && (e1=get_register())!=-1) { |
48 | 1104 e1=list2(REGISTER,e1); |
49 | 1105 g_expr(assign_expr0(e1,s,ty,ty)); |
1106 return list2(assign_expr0(t,e1,ty,ty),e1); | |
48 | 1107 } else { |
50 | 1108 disp-=sz; |
1109 g_expr(assign_expr0((e1=list2(LVAR,cvar(disp))),s,ty,ty)); | |
49 | 1110 return list2(assign_expr0(t,e1,ty,ty),-1); |
48 | 1111 } |
1112 } | |
1113 } | |
52 | 1114 if (overrap(t,sz,*source)) { |
51 | 1115 remove0(target,t); |
49 | 1116 *processing=list2(t,*processing); |
51 | 1117 e1=0; |
52 | 1118 do { |
51 | 1119 /*½ñ¤¹þ¤ßÀ褬¥½¡¼¥¹¤È½Å¤Ê¤Ã¤Æ¤¤¤ë¤¢¤¤¤À*/ |
52 | 1120 /*¾¤Î¤òÀè¤Ë½èÍý¤¹¤ë*/ |
1121 #if 1 | |
1122 printf("# recursive%d %d ty %d+%d sz %d\n",e1,car(t),ty,cadr(t),sz); | |
1123 #endif | |
51 | 1124 e1=list2(parallel_assign(target,source,processing),e1); |
52 | 1125 } while (overrap(t,sz,*source)); |
1126 /* ¤³¤ì¤Ç¶õ¤¤¤¿ */ | |
1127 #if 1 | |
1128 printf("# recursive%d done %d ty %d+%d sz %d\n",e1,car(t),ty,cadr(t),sz); | |
1129 #endif | |
1130 g_expr(assign_expr0(t,s,ty,ty)); | |
51 | 1131 remove0(source,s); |
1132 remove0(processing,t); | |
48 | 1133 /* ²£¤Ë¤è¤±¤¿¤â¤Î¤¬¤¢¤ì¤Ð¡¢¸å»ÏËö¤ò¤¹¤ë*/ |
51 | 1134 parallel_assign_post(e1); |
48 | 1135 } else { |
52 | 1136 /* ½Å¤Ê¤Ã¤Æ¤Ê¤¤¤Î¤Ç°Â¿´¤·¤Æ½ñ¤¹þ¤á¤ë */ |
1137 #if 1 | |
1138 printf("# normal assign %d ty %d+%d sz %d\n",car(t),ty,cadr(t),sz); | |
1139 #endif | |
49 | 1140 g_expr(assign_expr0(t,s,ty,ty)); |
1141 remove0(target,t); remove0(source,s); | |
48 | 1142 } |
1143 } | |
1144 return 0; | |
1145 } | |
1146 | |
49 | 1147 void |
1148 remove0(int *parent,int e) | |
48 | 1149 { |
1150 int list; | |
49 | 1151 while ((list=*parent)) { |
48 | 1152 if (car(list)==e) { |
51 | 1153 *parent= cadr(list); return; |
1154 } else { | |
1155 parent=&cadr(list); | |
1156 } | |
1157 } | |
1158 } | |
1159 | |
1160 void | |
1161 remove0_all(int *parent,int e) | |
1162 { | |
1163 int list; | |
1164 while ((list=*parent)) { | |
1165 if (car(list)==e) { | |
48 | 1166 *parent= cadr(list); |
1167 } else { | |
1168 parent=&cadr(list); | |
1169 } | |
1170 } | |
1171 } | |
1172 | |
49 | 1173 int |
1174 is_simple(int e1) | |
1175 { | |
1176 return ( | |
1177 e1==CONST || e1==FNAME || e1==LVAR || e1==REGISTER || | |
50 | 1178 e1==GVAR || RGVAR || RLVAR || CRLVAR || CRGVAR |
49 | 1179 ); |
1180 } | |
1181 | |
1182 int | |
51 | 1183 is_same_type(int e1,int e2) |
49 | 1184 { |
51 | 1185 int ce1=car(e1); |
1186 int ce2=car(e2); | |
1187 return ( | |
1188 (ce1==LVAR && (ce2==RLVAR||ce2==CRLVAR)) | |
1189 || (ce2==LVAR && (ce1==RLVAR||ce1==CRLVAR)) | |
1190 || (ce1==GVAR && (ce2==RGVAR||ce2==CRGVAR)) | |
1191 || (ce2==GVAR && (ce1==RGVAR||ce1==CRGVAR)) | |
1192 ); | |
49 | 1193 } |
1194 | |
52 | 1195 int |
1196 is_memory(int e1) | |
1197 { | |
1198 int ce1=car(e1); | |
1199 return ( | |
1200 ce1==LVAR ||ce1==RLVAR||ce1==CRLVAR || | |
1201 ce1==GVAR ||ce1==RGVAR||ce1==CRGVAR || | |
1202 ce1==REGISTER | |
1203 ); | |
1204 } | |
1205 | |
48 | 1206 void |
1207 jump(int e1, int env) | |
1208 { | |
51 | 1209 int e2,e3,e4,sz,arg_size,ty,max_regs,regs; |
1210 int t0,s0; | |
48 | 1211 NMTBL *code0; |
1212 int target = 0; | |
1213 int source = 0; | |
1214 int processing = 0; | |
1215 | |
1216 /* ¤Þ¤º¡¢¥µ¥¤¥º¤ò·×»»¤·¤Ê¤¬¤é¡¢·è¤Þ¤Ã¤¿·Á¤ËÍ¡£ */ | |
51 | 1217 /* ¤³¤³¤Ç¡¢½ñ¹þÀ襢¥É¥ì¥¹¤ò·è¤á¤ë */ |
1218 | |
50 | 1219 arg_size = 0; regs = 0; max_regs = MAX_REGISTER_VAR-1; |
1220 for (e3 = reverse0(caddr(e1)); e3; e3 = cadr(e3)) { | |
49 | 1221 e2 = car(e3); sz = size(ty=caddr(e3)); |
48 | 1222 if (regs <= max_regs&&integral(ty)) { |
51 | 1223 target=list4(list2(REGISTER,virtual((regs++)+REG_ESI)), |
1224 target,ty,e2); | |
48 | 1225 } else { |
51 | 1226 target=list4(list2(LVAR,cvar(arg_size)), |
1227 target,ty,e2); | |
49 | 1228 arg_size += sz; |
48 | 1229 } |
52 | 1230 #if 1 |
1231 printf("# target %d ty %d+%d sz %d\n",car(car(target)),ty,cadr(car(target)),sz); | |
1232 #endif | |
48 | 1233 } |
1234 | |
1235 /* disp ¤òÈô¤ÓÀè»÷¹ç¤ï¤»¤Æ½¤Àµ */ | |
50 | 1236 if (fnptr->sc==CODE) { |
1237 if (-arg_size<disp) disp = -arg_size; | |
1238 } else { | |
1239 if (disp_offset-arg_size<disp) disp = disp_offset-arg_size; | |
1240 } | |
48 | 1241 |
1242 /* Ê£»¨¤Ê¼°¤òÁ°¤â¤Ã¤Æ·×»»¤·¤Æ¤ª¤¯ */ | |
1243 /* ɬÍפʤé¶É½êÊÑ¿ô¤òÍѤ¤¤ë¡£ */ | |
1244 /* ¶É½êÊÑ¿ô¤Ø¤Î¥ª¥Õ¥»¥Ã¥È¤ò³Ð¤¨¤Æ¤ª¤¯ */ | |
1245 | |
51 | 1246 for (e2 = target; e2; e2 = cadr(e2)) { |
1247 t0=car(e2); s0=cadddr(e2); | |
52 | 1248 sz=size(ty=caddr(e2)); |
51 | 1249 if (!is_simple(car(s0))) { |
52 | 1250 disp-=sz; |
51 | 1251 g_expr(assign_expr0((e4=list2(LVAR,cvar(disp))),s0,ty,ty)); |
1252 cadddr(e2)=e4; | |
1253 s0=e4; | |
52 | 1254 } else if (is_same_type(t0,s0)) { |
1255 if(cadr(t0)==cadr(s0)) { | |
1256 /* we should check size also (but currently useless */ | |
1257 remove0(&target,t0); | |
1258 /* still we have source to avoid overwrite */ | |
51 | 1259 } |
52 | 1260 } |
1261 if(is_memory(s0)) { | |
1262 source=list3(s0,source,sz); | |
1263 #if 1 | |
1264 printf("# source %d ty %d+%d sz %d\n",car(car(source)),ty,cadr(car(source)),sz); | |
51 | 1265 #endif |
48 | 1266 } |
1267 } | |
1268 | |
1269 /* compute jump address */ | |
1270 e2 = cadr(e1); | |
1271 if (car(e2) == FNAME) { | |
1272 code0=(NMTBL *)cadr(e2); | |
1273 if (code0->sc!=CODE) { | |
1274 error(TYERR); return; | |
1275 } | |
1276 } else { /* indirect */ | |
1277 g_expr(e2); | |
1278 emit_push(); | |
1279 } | |
50 | 1280 if (env) { |
1281 g_expr(env); | |
1282 emit_push(); | |
1283 } | |
48 | 1284 |
1285 /* ÊÂÎóÂåÆþ¤ò¼Â¹Ô */ | |
1286 | |
51 | 1287 e1=parallel_assign(&target,&source,&processing); |
1288 | |
1289 /* ¸å»ÏËö¤¬»Ä¤Ã¤Æ¤¤¤¿ */ | |
1290 parallel_assign_post(e1); | |
48 | 1291 |
50 | 1292 if (env) { |
1293 /* change the frame pointer */ | |
1294 e3 = emit_pop(0); | |
1295 printf("\tmovl %s,%%ebp\n",register_name(e3,0)); | |
1296 emit_pop_free(e3); | |
1297 } else if (fnptr->sc==FUNCTION) { | |
1298 printf("\tlea %d(%%ebp),%%ebp\n",disp_offset); | |
1299 } | |
1300 | |
48 | 1301 /* shrink stack if necessary */ |
50 | 1302 /* |
1303 if (!env && -arg_size<disp) { | |
1304 printf("\tleal %d(%%ebp),%%esp\n",-arg_size); | |
1305 } Why we need this? | |
1306 */ | |
1307 | |
48 | 1308 if (car(e2) == FNAME) { |
1309 printf("\tjmp %s\n",code0->nm); | |
1310 } else { | |
1311 e2 = emit_pop(0); | |
1312 printf("\tjmp *%s\n",register_name(e2,0)); | |
1313 emit_pop_free(e2); | |
1314 } | |
1315 } | |
1316 | |
1317 #if 0 | |
2 | 1318 int |
3 | 1319 arg_size(int e3,int *nargs0) |
2 | 1320 { |
1321 int i,nargs,offset_list,e,t; | |
1322 | |
1323 offset_list = 0; | |
1324 /* we should use prototypes's type */ | |
1325 for (i = nargs = 0; e3;e3 =cadr(e3)) { | |
1326 e = car(e3); t = caddr(e3); | |
3 | 1327 if (i < MAX_REGISTER_VAR && scalar(t)) { |
5 | 1328 offset_list = list3(-(REG_ESI+i++),offset_list,e); |
2 | 1329 } else { |
1330 offset_list = | |
5 | 1331 list3(nargs,offset_list,e); |
3 | 1332 nargs += (car(e3)==CHAR?size_of_int:size(t)); |
2 | 1333 } |
1334 } | |
3 | 1335 *nargs0 = -nargs; |
2 | 1336 return offset_list; |
1337 } | |
1338 | |
18 | 1339 |
1340 void | |
0 | 1341 jump(int e1, int env) |
1342 { | |
18 | 1343 int i,args,e2,e3,e4,e5,nargs,regs; |
0 | 1344 NMTBL *n,*code0; |
42 | 1345 int new_disp,scode,disp1,xreg; |
0 | 1346 char *xrn; |
1347 | |
2 | 1348 /* We need three passes. Compute Stack size, Compute Arg, Copy it. */ |
0 | 1349 /* count number of args */ |
2 | 1350 args = caddr(e1); |
3 | 1351 args = reverse0(args); |
1352 nargs = arg_size(args,&new_disp); /* compute in normal order */ | |
5 | 1353 disp1 = (fnptr->sc==CODE)?0:-size_of_int; |
1354 if (new_disp+disp1 < disp) { /* have to extend stack */ | |
6 | 1355 if (fnptr->sc==CODE) |
1356 printf("\tleal %d(%%ebp),%%esp\n",new_disp-size_of_int); | |
1357 else | |
1358 printf("\tleal %d(%%ebp),%%esp\n",new_disp+disp_offset); | |
0 | 1359 } |
1360 /* compute jump address */ | |
1361 e2 = cadr(e1); | |
1362 if (car(e2) == FNAME) { | |
1363 code0=(NMTBL *)cadr(e2); | |
1364 if (code0->sc!=CODE) { | |
1365 error(STERR); return; | |
1366 } | |
1367 } else { /* indirect */ | |
1368 g_expr(e2); | |
1369 emit_push(); | |
1370 } | |
2 | 1371 /* compute arguments in reverse order */ |
21 | 1372 /* printf("## jump code_arg_offset=%d code_disp_offset=%d\n",code_arg_offset,code_disp_offset); */ |
2 | 1373 regs = 0; |
4 | 1374 i=MAX_REGISTER_VAR; |
2 | 1375 for (e3=nargs; e3;e3 =cadr(e3)) { |
1376 n=(NMTBL *)(e5=(cadr(e4 = caddr(e3)))); | |
0 | 1377 switch(car(e4)) { |
1378 case FNAME: | |
18 | 1379 printf("\tlea %s,%s\n",n->nm,register_name(creg,0)); |
0 | 1380 emit_push(); |
1381 break; | |
1382 case ADDRESS: | |
1383 g_expr(e5); | |
1384 emit_push(); | |
1385 break; | |
1386 case RLVAR: | |
1387 case CRLVAR: | |
4 | 1388 if (env==0 && fnptr->sc==CODE) { |
21 | 1389 /* printf("## e5=%d car(e3)=%d\n",e5,car(e3)); */ |
5 | 1390 if (e5>=0 && e5==car(e3)) { |
2 | 1391 /* The same positioned local variable. No need to copy */ |
1392 reg_stack[reg_sp++] = -2; | |
1393 } | |
4 | 1394 break; |
0 | 1395 } |
3 | 1396 g_expr(e4); |
1397 emit_push(); | |
1398 break; | |
0 | 1399 case REGISTER: |
21 | 1400 /* printf("## i=%d rname[e5]=%d\n",i,rname[e5]); */ |
4 | 1401 if (i>0 && rname[e5]==REG_ESI+ --i) { |
3 | 1402 /* The same register variable. No need to copy */ |
2 | 1403 reg_stack[reg_sp++] = e5; |
4 | 1404 break; |
0 | 1405 } |
1406 default: | |
1407 g_expr(e4); | |
1408 emit_push(); | |
1409 } | |
2 | 1410 regs++; |
0 | 1411 } |
1412 if (env) { | |
2 | 1413 /* change the frame pointer */ |
0 | 1414 g_expr(env); |
18 | 1415 printf("\tmovl %s,%%ebp\n",register_name(creg,0)); |
3 | 1416 } else if (fnptr->sc==FUNCTION) { |
1417 printf("\tlea %d(%%ebp),%%ebp\n",disp_offset); | |
1418 } | |
1419 /* force lvar offset mode to CODE */ | |
1420 scode = fnptr->sc; fnptr->sc = CODE; | |
21 | 1421 /* printf("## jump2 code_arg_offset=%d code_disp_offset=%d\n",code_arg_offset,code_disp_offset); */ |
0 | 1422 /* copy arguments to destination environment if necessary */ |
3 | 1423 nargs = reverse0(nargs); /* pop in normal order */ |
1424 i=0; | |
1425 for (e3=nargs; e3;e3 =cadr(e3)) { | |
5 | 1426 if ((e4=car(e3))<0) { |
2 | 1427 /* register case */ |
1428 if (reg_stack[--reg_sp]>=REG_ESI) { | |
1429 /* the same registger */ | |
1430 } else { | |
1431 if(reg_stack[reg_sp]<0) { | |
5 | 1432 printf("\tpopl %s\n",reg_name[rname[REG_ESI+i]]); /* e4? */ |
2 | 1433 } else { |
1434 printf("\tmovl %s,%s\n", | |
1435 reg_name[rname[reg_stack[reg_sp]]], | |
5 | 1436 reg_name[rname[REG_ESI+i]]); /* e4? */ |
2 | 1437 free_register(reg_stack[reg_sp]); |
1438 } | |
3 | 1439 i++; |
2 | 1440 } |
1441 } else { | |
1442 /* local variable case */ | |
0 | 1443 if (reg_stack[reg_sp-1]== -2) { |
1444 /* same positioned variable */ | |
1445 reg_sp--; | |
1446 } else { | |
42 | 1447 printf("\tmovl %s,%d(%%ebp)\n",register_name((xreg=emit_pop(0)),0), lvar(e4)); |
0 | 1448 } |
1449 } | |
1450 } | |
42 | 1451 free_register(xreg); |
0 | 1452 if (car(e2) != FNAME) { |
42 | 1453 xrn=register_name((xreg=emit_pop(0)),0); |
0 | 1454 } |
42 | 1455 free_register(xreg); |
6 | 1456 if (!env && new_disp+disp1>disp) { |
0 | 1457 /* shrink stack if necessary */ |
5 | 1458 printf("\tleal %d(%%ebp),%%esp\n",new_disp-size_of_int); |
0 | 1459 } |
1460 if (car(e2) == FNAME) { | |
1461 printf("\tjmp %s\n",code0->nm); | |
1462 } else { | |
1463 printf("\tjmp *%s\n",xrn); | |
1464 } | |
3 | 1465 fnptr->sc = scode; |
0 | 1466 } |
48 | 1467 #endif |
0 | 1468 |
1469 void | |
1470 machinop(int e1) | |
1471 { | |
1472 int e2,e3,op; | |
1473 | |
1474 e2 = cadr(e1); | |
1475 op = car(e1); | |
1476 e3 = caddr(e1); | |
1477 g_expr(e3); | |
1478 emit_push(); | |
1479 g_expr(e2); | |
43 | 1480 tosop(car(e1),(e2=pop_register())); |
1481 emit_pop_free(e2); | |
42 | 1482 regv[creg]=1; |
0 | 1483 return; |
1484 } | |
1485 | |
1486 | |
1487 void | |
1488 rindirect(int e1) /* *(p +5 ) */ | |
1489 | |
1490 { | |
1491 char *op; | |
1492 int e2,e3,byte; | |
1493 | |
1494 op = ((byte = (car(e1) == CRINDIRECT)) ? "movsbl" : "movl"); | |
1495 e3 = cadr(e2 = cadr(e1)); | |
1496 g_expr(e2); | |
21 | 1497 printf("\t%s (%s),%s\n",op,register_name(creg,0),register_name(creg,0)); |
0 | 1498 } |
1499 | |
1500 void | |
1501 sassign(int e1) | |
1502 { | |
38 | 1503 int e2,e3,e4,sz,xreg; |
0 | 1504 |
18 | 1505 /* structure assignment */ |
37 | 1506 e2 = cadr(e1); /* pointer variable to the struct */ |
1507 e3 = cadr(e2); /* offset of the variable (distination) */ | |
39 | 1508 e4 = caddr(e1); /* right value (source) */ |
37 | 1509 sz = cadddr(e1); /* size of struct or union */ |
0 | 1510 g_expr(e4); |
1511 emit_push(); | |
1512 g_expr(e2); | |
38 | 1513 xreg = emit_pop(0); |
47 | 1514 emit_copy(xreg,creg,sz,0,1); |
42 | 1515 emit_pop_free(xreg); |
0 | 1516 return; |
1517 } | |
1518 | |
1519 void | |
1520 assign(int e1) | |
1521 { | |
1522 char *op; | |
1523 int e2,e3,e4,byte; | |
1524 | |
1525 op = ((byte=(car(e1) == CASS))? "movb" : "movl"); | |
1526 /* e2=e4 */ | |
1527 e2 = cadr(e1); | |
1528 e3 = cadr(e2); | |
1529 e4 = caddr(e1); | |
1530 switch(car(e2)) { | |
1531 case GVAR: /* i=3 */ | |
1532 g_expr(e4); | |
1533 if (byte) use_data_reg(creg,1); | |
18 | 1534 printf("\t%s %s,%s\n",op,register_name(creg,byte),(char *)caddr(e2)); |
0 | 1535 return; |
1536 case LVAR: | |
1537 g_expr(e4); | |
1538 if (byte) use_data_reg(creg,1); | |
18 | 1539 printf("\t%s %s,%d(%%ebp)\n",op,register_name(creg,byte),lvar(cadr(e2))); |
0 | 1540 return; |
1541 case REGISTER: | |
1542 g_expr(e4); | |
1543 if (creg!=cadr(e2)) | |
23 | 1544 printf("\tmovl %s,%s\n",register_name(creg,0),register_name(cadr(e2),0)); |
0 | 1545 return; |
1546 } | |
23 | 1547 g_expr(e2); |
0 | 1548 emit_push(); |
23 | 1549 use_data_reg(creg,0); |
1550 g_expr(e4); | |
0 | 1551 if (byte) use_data_reg(creg,1); |
23 | 1552 e2 = emit_pop(0); |
1553 printf("\t%s %s,(%s)\n",op,register_name(creg,byte),register_name(e2,0)); | |
42 | 1554 emit_pop_free(e2); |
1555 regv[creg]=1; | |
0 | 1556 return; |
1557 } | |
1558 | |
1559 void | |
1560 assop(int e1) | |
1561 { | |
43 | 1562 int e2,e3,byte,op,reg; |
0 | 1563 char *xrn; |
42 | 1564 int xreg,edx; |
0 | 1565 |
1566 /* e2 op= e3 */ | |
1567 byte = (car(e1) == CASSOP); | |
1568 e2 = cadr(e1); | |
1569 if (car(e2)==INDIRECT) e2=cadr(e2); | |
1570 e3 = caddr(e1); | |
1571 op = cadddr(e1); | |
1572 | |
1573 g_expr(e3); | |
1574 if (car(e2)==REGISTER) { | |
43 | 1575 xreg = creg; |
1576 creg = reg = cadr(e2); | |
1577 tosop(op,xreg); | |
1578 creg = xreg; | |
1579 printf("\tmovl %s,%s\n",register_name(reg,0),register_name(creg,0)); | |
42 | 1580 regv[creg]=1; |
0 | 1581 return; |
1582 } | |
43 | 1583 emit_push(); |
0 | 1584 g_expr(e2); |
42 | 1585 edx = edx_setup(); |
18 | 1586 xrn = register_name(xreg = emit_pop(0),0); /* pop e3 value */ |
42 | 1587 regv[xreg]=regs[xreg]=1; |
1588 printf("\tmovl %s,%s # assop \n",register_name(creg,0),register_name(edx,0)); | |
1589 regv[edx]=1; | |
1590 ld_indexx(byte,0,edx); | |
43 | 1591 tosop(op,xreg); |
42 | 1592 printf("\t%s %s,(%s)\n",byte ? "movb" : "movl",register_name(creg,byte),register_name(edx,0)); |
1593 edx_cleanup(); | |
1594 emit_pop_free(xreg); | |
1595 regv[creg]=1; | |
0 | 1596 return; |
1597 } | |
1598 | |
1599 | |
1600 void | |
43 | 1601 tosop(int op,int oreg) |
0 | 1602 { |
43 | 1603 int dx; |
18 | 1604 char *orn,*crn; |
0 | 1605 |
1606 switch(op) { | |
1607 case LSHIFT: | |
1608 case ULSHIFT: | |
1609 shift("sall",oreg); | |
1610 return; | |
1611 case RSHIFT: | |
1612 shift("sarl",oreg); | |
1613 return; | |
1614 case URSHIFT: | |
1615 shift("shrl",oreg); | |
1616 return; | |
1617 } | |
1618 if(oreg==-1) { | |
18 | 1619 printf("\tpopl %s\n",register_name(dreg,0)); |
0 | 1620 oreg = dreg; |
42 | 1621 regv[dreg]=1; |
0 | 1622 } |
42 | 1623 regv[oreg]=1; regs[oreg]=1; |
18 | 1624 orn = register_name(oreg,0); |
1625 crn = register_name(creg,0); | |
0 | 1626 switch(op) { |
1627 case ADD: | |
1628 printf("\taddl %s,%s\n",orn,crn); | |
41 | 1629 break; |
0 | 1630 case SUB: |
1631 printf("\tsubl %s,%s\n",orn,crn); | |
41 | 1632 break; |
0 | 1633 case BAND: |
1634 printf("\tandl %s,%s\n",orn,crn); | |
41 | 1635 break; |
0 | 1636 case EOR: |
1637 printf("\txorl %s,%s\n",orn,crn); | |
41 | 1638 break; |
0 | 1639 case BOR: |
1640 printf("\torl %s,%s\n",orn,crn); | |
41 | 1641 break; |
0 | 1642 case MUL: |
1643 case UMUL: | |
1644 printf("\t%s %s,%s\n","imull",orn,crn); | |
41 | 1645 break; |
0 | 1646 case DIV: |
1647 case UDIV: | |
42 | 1648 use_register(creg,REG_EAX,1); |
1649 edx_setup(); | |
1650 orn = register_name(oreg,0); | |
0 | 1651 if (op==DIV) |
1652 printf("\tcltd\n\tdivl %s\n",orn); | |
1653 else | |
1654 printf("\txor %%edx,%%edx\n\tidivl %s\n",orn); | |
42 | 1655 edx_cleanup(); |
41 | 1656 break; |
0 | 1657 case MOD: |
1658 case UMOD: | |
42 | 1659 use_register(creg,REG_EAX,1); |
1660 edx_setup(); | |
1661 orn = register_name(oreg,0); | |
0 | 1662 if (op==DIV) |
1663 printf("\tcltd\n\tdivl %s\n",orn); | |
1664 else | |
1665 printf("\txor %%edx,%%edx\n\tidivl %s\n",orn); | |
42 | 1666 dx = virtual(REG_EDX); |
1667 if (dx!=creg) { | |
1668 rname[dx]=rname[creg]; | |
1669 rname[creg]=REG_EDX; | |
1670 } | |
1671 edx_cleanup(); | |
41 | 1672 break; |
0 | 1673 } |
42 | 1674 if (oreg!=dreg&&oreg>=0) |
1675 free_register(oreg); | |
0 | 1676 } |
1677 | |
42 | 1678 static int edx_stack=0; |
0 | 1679 |
42 | 1680 int |
1681 edx_setup() | |
0 | 1682 { |
42 | 1683 int edx_save; |
1684 /* make real EDX register empty */ | |
1685 if (free_register_count()<1) { | |
1686 for(edx_save = 0;edx_save==dreg||edx_save==creg;edx_save++); | |
1687 printf("\tpushl %s\n",register_name(edx_save,0)); | |
1688 edx_stack = list3(edx_save,edx_stack,0); | |
1689 } else { | |
1690 edx_save = get_register(); | |
1691 edx_stack = list3(edx_save,edx_stack,1); | |
0 | 1692 } |
42 | 1693 regv[edx_save]=0; |
1694 use_register(edx_save,REG_EDX,0); | |
1695 return edx_save; | |
0 | 1696 } |
1697 | |
18 | 1698 |
1699 void | |
42 | 1700 edx_cleanup() |
0 | 1701 { |
42 | 1702 if (caddr(edx_stack)==0) { |
1703 printf("\tpopl %s\n",register_name(car(edx_stack),0)); | |
1704 } else | |
1705 free_register(car(edx_stack)); | |
1706 edx_stack = cadr(edx_stack); | |
0 | 1707 } |
1708 | |
1709 void | |
1710 shift(char *op, int reg) | |
1711 { | |
1712 if (reg>=0) { | |
42 | 1713 use_register(reg,REG_ECX,1); |
0 | 1714 } else { |
1715 use_register(dreg,REG_ECX,0); | |
1716 printf("\tpopl %%ecx\n"); | |
1717 } | |
18 | 1718 printf("\t%s %%cl,%s\n",op,register_name(creg,0)); |
0 | 1719 } |
1720 | |
1721 void | |
18 | 1722 ld_indexx(int byte, int n, int xreg) |
0 | 1723 { |
1724 char *op; | |
1725 | |
1726 op = byte ? "movsbl" : "movl"; | |
1727 if (n) | |
18 | 1728 printf("\t%s %d(%s),%s\n",op,n,register_name(xreg,0),register_name(creg,byte)); |
0 | 1729 else |
18 | 1730 printf("\t%s (%s),%s\n",op,register_name(xreg,0),register_name(creg,byte)); |
0 | 1731 } |
1732 | |
18 | 1733 /* |
0 | 1734 void |
18 | 1735 st_indexx(int byte, int n, int xreg) |
0 | 1736 { |
1737 char *op; | |
1738 | |
1739 op = byte ? "movb" : "movl"; | |
1740 if (n) | |
18 | 1741 printf("\t%s %s,%d(%s)\n",op,register_name(creg,byte),n,register_name(xreg,0)); |
0 | 1742 else |
18 | 1743 printf("\t%s %s,(%s)\n",op,register_name(creg,byte),register_name(xreg,0)); |
0 | 1744 } |
1745 | |
1746 void | |
1747 indexy(char *op, char *gvar) | |
1748 { | |
18 | 1749 printf("\t%s %s,%s\n",op,register_name(creg,0),gvar); |
0 | 1750 } |
18 | 1751 */ |
0 | 1752 |
1753 void | |
1754 cmpdimm(int e, int csreg) | |
1755 { | |
1756 /* used in dosiwtch() */ | |
1757 use_register(creg,csreg,0); | |
18 | 1758 printf("\tcmpl $%d,%s\n",e,register_name(creg,0)); |
0 | 1759 } |
1760 | |
1761 void | |
1762 opening(char *filename) | |
1763 { | |
1764 printf("\t.file \"%s\"\n",filename); | |
1765 printf("\t.version\t\"01.01\"\n"); | |
1766 printf("gcc2_compiled.:\n"); | |
1767 printf(".text\n"); | |
1768 emit_init(); | |
1769 } | |
1770 | |
1771 void | |
1772 closing(void) | |
1773 { | |
1774 global_table(); | |
1775 printf("\t.ident \"Micro-C compiled\"\n"); | |
1776 } | |
1777 | |
1778 void | |
1779 rexpr(int e1, int l1, char *s) | |
1780 { | |
1781 g_expr(list3(SUB,cadr(e1),caddr(e1))); | |
1782 printf("\tj%s\t_%d\n",s,l1); | |
1783 } | |
1784 | |
1785 void | |
1786 jcond(int l, char cond) | |
1787 { | |
1788 printf("\tj%s\t_%d\n",cond?"ne":"e",l); | |
1789 } | |
1790 | |
1791 void | |
1792 jmp(int l) | |
1793 { | |
1794 control=0; | |
1795 printf("\tjmp\t_%d\n",l); | |
1796 /* align? */ | |
42 | 1797 /* |
1798 this is not allowed because of ? operator | |
1799 regv[creg]=regv[dreg]=0; | |
1800 use_register(creg,REG_EAX,0); | |
1801 use_register(dreg,REG_EBX,0); | |
1802 */ | |
0 | 1803 } |
1804 | |
1805 int | |
1806 fwdlabel(void) | |
1807 { | |
1808 return labelno++; | |
1809 } | |
1810 | |
1811 void | |
1812 fwddef(int l) | |
1813 { | |
1814 control=1; | |
1815 printf("_%d:\n",l); | |
1816 } | |
1817 | |
1818 int | |
1819 backdef(void) | |
1820 { | |
1821 control=1; | |
1822 printf("_%d:\n",labelno); | |
1823 return labelno++; | |
1824 } | |
1825 | |
1826 void | |
1827 def_label(int cslabel, int dlabel) | |
1828 { | |
1829 int fl; | |
1830 | |
1831 fl = 0; | |
1832 if (control) { | |
1833 jmp(fl=fwdlabel()); | |
1834 } | |
1835 fwddef(cslabel); | |
1836 if (dlabel) | |
1837 jmp(dlabel); | |
1838 if (fl) { | |
1839 fwddef(fl); | |
1840 } | |
1841 } | |
1842 | |
1843 void | |
1844 gen_comment(char *s) | |
1845 { | |
21 | 1846 printf("## %s",s); |
0 | 1847 } |
1848 | |
1849 void | |
1850 gen_source(char *s) | |
1851 { | |
1852 printf("%s",s); | |
1853 } | |
1854 | |
1855 void | |
1856 code_enter(char *name) | |
1857 { | |
1858 printf("\t.align 4\n"); | |
1859 if (stmode!=STATIC) | |
1860 printf(".globl %s\n",name); | |
1861 printf("\t.type\t%s,@function\n",name); | |
1862 printf("%s:\n",name); | |
42 | 1863 free_all_register(); |
0 | 1864 } |
1865 | |
1866 void | |
50 | 1867 code_enter1(int args) |
0 | 1868 { |
36 | 1869 code_disp_label=fwdlabel(); |
50 | 1870 printf("\tlea _%d(%%ebp),%%esp\n",code_disp_label); |
36 | 1871 /* |
6 | 1872 if(disp0) { |
50 | 1873 printf("\tsubl $%d,%%esp\n",disp0); |
0 | 1874 } |
36 | 1875 */ |
21 | 1876 printf("## args %d disp %d code_arg_offset=%d code_disp_offset=%d\n",args,disp,code_arg_offset,code_disp_offset); |
0 | 1877 } |
1878 | |
1879 void | |
50 | 1880 code_leave(char *name) |
0 | 1881 { |
52 | 1882 printf("\t.set _%d,%d\n",code_disp_label,cvar(disp)); |
0 | 1883 printf("_%d:\n",labelno); |
1884 printf("\t.size\t%s,_%d-%s\n",name,labelno,name); | |
12 | 1885 local_table(); |
0 | 1886 labelno++; |
1887 } | |
1888 | |
1889 void | |
1890 enter(char *name) | |
1891 { | |
1892 printf("\t.align 2\n"); | |
1893 if (stmode!=STATIC) | |
1894 printf(".globl %s\n",name); | |
1895 printf("%s:\n",name); | |
1896 printf("\t.type\t%s,@function\n",name); | |
1897 printf("\tpushl %%ebp\n"); | |
1898 printf("\tmovl %%esp,%%ebp\n"); | |
1899 printf("\tpushl %%ebx\n"); | |
24 | 1900 printf("\tpushl %%esi\n"); |
1901 printf("\tpushl %%edi\n"); | |
42 | 1902 free_all_register(); |
0 | 1903 } |
1904 | |
1905 void | |
50 | 1906 enter1() |
0 | 1907 { |
36 | 1908 func_disp_label=fwdlabel(); |
50 | 1909 printf("\tlea _%d(%%ebp),%%esp\n",func_disp_label); |
36 | 1910 /* if(disp) printf("\tsubl $%d,%%esp\n",-disp); */ |
0 | 1911 } |
1912 | |
1913 void | |
50 | 1914 leave(int control, char *name) |
0 | 1915 { |
1916 if (control) | |
1917 use_register(creg,REG_EAX,1); | |
1918 if (retcont) { | |
1919 if (control) | |
1920 jmp(retlabel); | |
1921 fwddef(retcont); | |
1922 use_register(creg,REG_EAX,0); | |
18 | 1923 printf("\tmovl %s,%s\n",reg_name[REG_ESI],register_name(creg,0)); |
0 | 1924 /* printf("\tleave\n"); */ |
1925 } | |
1926 fwddef(retlabel); | |
42 | 1927 /* use_register(creg,REG_EAX,0); too late */ |
52 | 1928 /* if(disp) printf("\taddl $%d,%%esp\n",-disp); */ |
0 | 1929 printf("\tlea %d(%%ebp),%%esp\n",disp_offset); |
24 | 1930 printf("\tpopl %%edi\n"); |
1931 printf("\tpopl %%esi\n"); | |
0 | 1932 printf("\tpopl %%ebx\n"); |
1933 printf("\tleave\n"); | |
1934 printf("\tret\n"); | |
52 | 1935 printf("\t.set _%d,%d\n",func_disp_label,disp+disp_offset); |
0 | 1936 printf("_%d:\n",labelno); |
1937 printf("\t.size\t%s,_%d-%s\n",name,labelno,name); | |
12 | 1938 local_table(); |
0 | 1939 labelno++; |
1940 } | |
1941 | |
1942 | |
1943 void | |
1944 ret(void) | |
1945 { | |
1946 use_register(creg,REG_EAX,1); | |
1947 jmp(retlabel); | |
1948 } | |
1949 | |
1950 void | |
1951 gen_gdecl(char *n, int gpc) | |
1952 { | |
1953 /* | |
1954 if (stmode!=STATIC) | |
1955 printf(".globl %s\n",n); | |
1956 */ | |
1957 } | |
1958 | |
1959 void | |
1960 align(int t) | |
1961 { | |
1962 if (t!=CHAR) { | |
1963 if (data_alignment & 1) | |
1964 printf("\t.align 2\n"); | |
1965 data_alignment = 0; | |
1966 } | |
1967 } | |
1968 | |
1969 void | |
1970 emit_data(int e, int t, NMTBL *n) | |
1971 { | |
1972 int l; | |
1973 char *name; | |
1974 name = n->nm; | |
13 | 1975 if(mode!=GDECL) { |
1976 error(-1); return; | |
1977 } | |
1978 if (n->dsp != -1) { | |
1979 n->dsp = -1; /* initiallized flag */ | |
1980 printf(".globl\t%s\n",name); | |
1981 data_mode(name); | |
1982 align(t); | |
1983 printf("%s:\n",name); | |
1984 } else { | |
1985 data_mode(0); | |
1986 } | |
1987 if(car(e)==CONST) { | |
1988 if (t==CHAR) { | |
1989 printf("\t.byte %d\n",cadr(e)); | |
1990 if (data_alignment>0) | |
1991 data_alignment++; | |
1992 gpc += 1; | |
0 | 1993 } else { |
13 | 1994 printf("\t.long %d\n",cadr(e)); |
3 | 1995 gpc += size_of_int; |
13 | 1996 } |
1997 } else if(t!=CHAR) { | |
1998 gpc += size_of_int; | |
1999 if(car(e)==ADDRESS&&car(cadr(e))==GVAR) { | |
18 | 2000 printf("\t.long %s\n",(char *)caddr(cadr(e))); |
13 | 2001 } else if(car(e)==FNAME) { |
2002 printf("\t.long %s\n",((NMTBL *)cadr(e))->nm); | |
2003 } else if(car(e)==STRING) { | |
2004 if (car(n->ty)!=ARRAY || cadr(n->ty)!=CHAR) { | |
2005 l = fwdlabel(); | |
2006 printf("\t.long _%d\n",l); | |
2007 printf(".section\t.rodata\n"); | |
2008 printf("_%d:\n",l); | |
2009 output_mode = RODATA_EMIT_MODE; | |
2010 } | |
2011 ascii((char *)cadr(e)); | |
2012 } else error(TYERR); | |
0 | 2013 } |
2014 } | |
2015 | |
2016 void | |
2017 emit_data_closing(NMTBL *n) | |
2018 { | |
2019 int lb; | |
18 | 2020 if (mode==GDECL) { |
0 | 2021 data_mode(0); |
2022 lb=fwdlabel(); | |
2023 printf("_%d:\n",lb); | |
2024 printf("\t.size\t%s,_%d-%s\n",n->nm,lb,n->nm); | |
2025 } | |
2026 } | |
2027 | |
2028 void | |
2029 global_table(void) | |
2030 { | |
2031 NMTBL *n; | |
12 | 2032 int init; |
2033 init=0; | |
0 | 2034 for(n=ntable;n < &ntable[GSYMS];n++) { |
2035 if (n->sc == GVAR && n->dsp != -1) { | |
2036 /* n->dsp = -1 means initialized global */ | |
12 | 2037 if (init==0) { |
2038 data_mode(0); | |
2039 init=1; | |
2040 } | |
0 | 2041 printf(".comm %s,%d\n",n->nm,size(n->ty)); |
2042 } | |
2043 } | |
2044 } | |
2045 | |
2046 void | |
2047 local_table(void) | |
2048 { | |
2049 NMTBL *n; | |
12 | 2050 int init; |
2051 init=0; | |
0 | 2052 /* static local variables */ |
2053 for(n=ntable+GSYMS;n < &ntable[GSYMS+LSYMS];n++) { | |
2054 if (n->sc == GVAR) { | |
12 | 2055 if (init==0) { |
2056 data_mode(0); | |
2057 init=1; | |
2058 } | |
0 | 2059 printf(".lcomm %s,%d\n",n->nm,size(n->ty)); |
2060 } | |
2061 } | |
2062 } | |
2063 | |
2064 void | |
2065 text_mode(void) | |
2066 { | |
2067 if (output_mode!=TEXT_EMIT_MODE) { | |
2068 printf(".text\n"); | |
2069 printf("\t.align 2\n"); | |
2070 output_mode = TEXT_EMIT_MODE; | |
2071 } | |
2072 } | |
2073 | |
2074 void | |
2075 data_mode(char *name) | |
2076 { | |
2077 if (output_mode!=DATA_EMIT_MODE) { | |
2078 printf(".data\n"); | |
2079 output_mode = DATA_EMIT_MODE; | |
2080 } | |
2081 if (name) | |
2082 printf("\t.type\t%s,@object\n",name); | |
2083 } | |
2084 | |
2085 int | |
2086 lvar(int l) | |
2087 { | |
50 | 2088 if (fnptr->sc==CODE) { |
2089 return (l<0)? l+code_disp_offset: -l+code_arg_offset; | |
2090 } else if (l<0) { | |
2091 return l+disp_offset; | |
0 | 2092 } else { |
50 | 2093 return l+arg_offset; |
0 | 2094 } |
2095 } | |
2096 | |
50 | 2097 int |
2098 cvar(int l) | |
2099 { | |
2100 return (fnptr->sc==CODE)?l:l+disp_offset; | |
2101 } | |
2102 | |
0 | 2103 /* end */ |