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