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