Mercurial > hg > CbC > old > device
view mc-nop-386.c @ 18:df7fa8cee67b
pass -Wall
add ifdef
author | kono |
---|---|
date | Sat, 01 Feb 2003 22:19:29 +0900 |
parents | 6667dbd4f718 |
children | f9f789aa7587 |
line wrap: on
line source
/* Micro-C Code Generatation Part for intel386 */ /* $Id$ */ #define EXTERN extern #include "mc.h" #define REGISTERS6 0 extern int get_register_var(void); extern void bexpr(int e1, char cond, int l1); extern void closing(void); extern void cmpdimm(int e, int csreg); extern void code_enter(char *name) ; extern void code_enter1(int disp0,int args); extern void code_init(void); extern void code_leave(char *name) ; extern void def_label(int cslabel, int dlabel) ; extern void emit_data(int e, int t, NMTBL *n); extern void emit_init(void); extern void enter(char *name) ; extern void enter1(int disp) ; extern void g_expr(int e1); extern void gen_comment(char *s); extern void gen_gdecl(char *n, int gpc) ; extern void gen_source(char *s); extern void gexpr(int e1); extern void jcond(int l, char cond); extern void jmp(int l); extern void opening(char *filename); extern void ret(void); static char *div_setup(int oreg) ; static int lvar(int l); static void jump(int e1, int env); static void data_mode(char *name); static void text_mode(void); static void assign(int e1); static void assop(int e1); static void b_expr(int e1, char cond, int l1); static void emit_push(void); static void free_register(int i); static void function(int e1); static void global_table(void) ; /* static void indexy(char *op, char *gvar); */ /* static void jmp_eq_label(int l); */ /* static void jmp_label(int l); */ static void ld_indexx(int byte, int n, int xreg); static void local_table(void) ; static void machinop(int e1); static void rexpr(int e1, int l1, char *s); static void rindirect(int e1); static void sassign(int e1); static void shift(char *op, int xreg) ; /* static void st_indexx(int byte, int n, int xreg); */ static void string(int e1); static void tosop(int op); void div_cleanup(char *orn); extern int cadr(int e); extern int car(int e); extern int caddr(int e); extern int cadddr(int e); extern int error(int n); extern int fwdlabel(void); extern void fwddef(int l); extern int backdef(void); extern int size(int t); extern int list3(int e1, int e2, int e3); extern int scalar(int); extern int reverse0(int); #define TEXT_EMIT_MODE 0 #define DATA_EMIT_MODE 1 #define RODATA_EMIT_MODE 2 static int output_mode = TEXT_EMIT_MODE; static int data_alignment = 0; /* local1 <----24 local variable %esi -20 <- disp_offset %edi -16 %edx -12 %ecx -8 %ebx -4 %ebp = %esp 0 %eip 4 <- arg_offset arg1 8 see enter/enter1/leave */ int arg_offset = 8; int code_arg_offset = -4; int disp_offset = -12; int func_disp_offset = -12; int code_disp_offset = -4; int size_of_int = 4; int endian = 0; int MAX_REGISTER=6; /* intel386のレジスタを4つまで使う*/ int REAL_MAX_REGISTER=8; /* intel386のレジスタが8つということ*/ int MAX_DATA_REG=4; int MAX_POINTER=3; int MAX_REGISTGER_VAR=2; static int creg; /* current register */ static int lreg; /* operand register */ static int dreg; /* temporary register */ static int reg_sp; /* REGister Stack-Pointer */ #define REG_EAX 0 #define REG_EBX 1 #define REG_ECX 2 #define REG_EDX 3 #define REG_ESI 4 #define REG_EDI 5 #define REG_EBP 6 #define REG_ESP 7 #define DATA_REG 0 #define POINTER_REG 3 static char *reg_name[8]; static char *reg_name_l[4]; static char *reg_name_w[4]; /* creg currrent virtual register lreg operand virtual register dreg spare virtual register rname[creg] currrent real register rname[lreg] operand real register rname[dreg] spare real register regs[] virtual register usage reg_name[rname[creg]] */ #define MAX_MAX 10 static int rname[MAX_MAX]; static int regs[MAX_MAX]; /* 使われているレジスタを示すフラグ */ static int reg_stack[MAX_MAX]; /* 実際のレジスタの領域 */ void code_init(void) { if (REGISTERS6) { arg_offset = 8; func_disp_offset = -20; disp_offset = -20; size_of_int = 4; endian = 0; MAX_REGISTER=6; MAX_DATA_REG=4; MAX_POINTER=3; } else { arg_offset = 8; func_disp_offset = -12; disp_offset = -12; size_of_int = 4; endian = 0; MAX_REGISTER=4; MAX_DATA_REG=4; MAX_POINTER=1; } MAX_REGISTER_VAR=2; reg_name[REG_EAX] = "%eax"; reg_name[REG_EBX] = "%ebx"; reg_name[REG_ECX] = "%ecx"; reg_name[REG_EDX] = "%edx"; reg_name[REG_ESI] = "%esi"; reg_name[REG_EDI] = "%edi"; reg_name[REG_EBP] = "%ebp"; reg_name[REG_ESP] = "%esp"; reg_name_l[REG_EAX] = "%al"; reg_name_l[REG_EBX] = "%bl"; reg_name_l[REG_ECX] = "%cl"; reg_name_l[REG_EDX] = "%dl"; reg_name_w[REG_EAX] = "%ax"; reg_name_w[REG_EBX] = "%bx"; reg_name_w[REG_ECX] = "%cx"; reg_name_w[REG_EDX] = "%dx"; } char * register_name(int i,int byte) { if (byte && i <= REG_EDX) { return reg_name_l[rname[i]]; } else { return reg_name[rname[i]]; } } int get_register(void) { /* 使われていないレジスタを調べる */ int i; for(i=0;i<MAX_REGISTER;i++) { if (! regs[i]) { /* 使われていないなら */ regs[i]=1; /* そのレジスタを使うことを宣言し */ return i; /* その場所を表す番号を返す */ } } return -1; /* 空いている場所がないなら、それを表す -1 を返す */ } void free_register(int i) { /* いらなくなったレジスタを開放 */ regs[i]=0; } int register_full(void) { int i; for(i=0;i<MAX_REGISTER;i++) { if (! regs[i]) { return 0; } } return 1; } void gexpr_init(void) { while(reg_sp > 0) { free_register(reg_stack[--reg_sp]); } text_mode(); } void emit_init(void) { int i; for(i=0;i<REAL_MAX_REGISTER;i++) regs[i]=0; for(i=0;i<REAL_MAX_REGISTER;i++) rname[i]=i; creg = get_register(); dreg = get_register(); reg_sp = 0; text_mode(); } int virtual(int real) { int real_v,i; real_v = -1; for(i=0;i<MAX_REGISTER;i++) { if (rname[i]==real) { real_v=i; break; } } if (real_v == -1) error(-1); return real_v; } void use_register(int virt, int real, int move) { int real_v; char *move_op; if (rname[virt]==real) return; real_v = virtual(real); move_op = regs[real_v]?"\txchg %s,%s\n":"\tmovl %s,%s\n"; if (move || regs[real_v]) { printf(move_op,reg_name[rname[virt]],reg_name[real]); } rname[real_v] = rname[virt]; rname[virt] = real; } void use_pointer(int virt, int move) { int i; if (rname[virt]>=POINTER_REG) return; for(i=POINTER_REG;i<MAX_REGISTER;i++) { if (!regs[virtual(i)]) { use_register(virt,i,move); return; } } /* we prefer EBX */ use_register(virt,REG_EBX,move); } void use_data_reg(int virt, int move) { int i; if (rname[virt]<MAX_DATA_REG) return; for(i=0;i<MAX_DATA_REG;i++) { if (!regs[virtual(i)]) { use_register(virt,i,move); return; } } /* we prefer EBX */ use_register(virt,REG_EBX,move); } int pop_register(void) { /* レジスタから値を取り出す */ int i,j; j = creg; i = reg_stack[--reg_sp]; if(i<0) { return i; } else { free_register(i); lreg = i; regs[i]=0; return lreg; } } int stack_used(void) { return reg_stack[--reg_sp]<0; } void emit_push(void) { int new_reg; new_reg = get_register(); if(new_reg<0) { /* もうレジスタがない */ reg_stack[reg_sp++] = -1; printf("\tpushl %s\n",register_name(creg,0)); } else { reg_stack[reg_sp++] = creg; /* push するかわりにレジスタを使う */ creg = new_reg; } } int emit_pop(int type) { int xreg; if (pop_register()==-1) { if (type==POINTER_REG) use_pointer(dreg,0); else if (type==DATA_REG) use_data_reg(dreg,0); printf("\tpopl %s\n",register_name(dreg,0)); xreg = dreg; } else { xreg = lreg; } return xreg; } int get_register_var(void) { int i; for(i=REG_ESI;i<REG_ESP;i++) { if (! regs[i]) { /* 使われていないなら */ regs[i]=1; /* そのレジスタを使うことを宣言し */ return i; /* その場所を表す番号を返す */ } } return -1; } void gexpr(int e1) { gexpr_init(); g_expr(e1); csvalue = rname[creg]; /* for siwtch value */ } void g_expr(int e1) { int e2,e3,e4; char *xrn; NMTBL *n; if (chk) return; e2 = cadr(e1); switch (car(e1)){ case GVAR: /* use_pointer(creg,0); */ printf("\tmovl $%s,%s\n",(char *)caddr(e1),register_name(creg,0)); return; case RGVAR: /* use_pointer(creg,0); */ printf("\tmovl %s,%s\n",(char *)caddr(e1),register_name(creg,0)); return; case CRGVAR: printf("\tmovsbl %s,%s\n",(char *)caddr(e1),register_name(creg,1)); return; case LVAR: /* use_pointer(creg,0); */ printf("\tlea %d(%%ebp),%s\n",lvar(e2),register_name(creg,0)); return; case REGISTER: /* this is of course redundant... */ /* we can use rname for this? */ /* or why not creg=e2? */ printf("\tmovl %s,%s\n",register_name(e2,0),register_name(creg,0)); return; case RLVAR: printf("\tmovl %d(%%ebp),%s\n",lvar(e2),register_name(creg,0)); return; case CRLVAR: printf("\tmovsbl %d(%%ebp),%s\n",lvar(e2),register_name(creg,1)); return; case FNAME: printf("\tmovl $%s,%s\n",((NMTBL *)e2)->nm,register_name(creg,0)); return; case CONST: /* 代入する値が0でも特別な処理はしない */ printf("\tmovl $%d,%s\n",e2,register_name(creg,0)); return; case STRING: string(e1); return; case FUNCTION: function(e1); return; case CODE: jump(e2,caddr(e1)); return; case INDIRECT: g_expr(e2); return; case RINDIRECT: case CRINDIRECT: rindirect(e1); return; case ADDRESS: g_expr(e2); return; case MINUS: /* レジスタに対し、neglを実行すれば実現可能 */ g_expr(e2); /* use_data_reg(creg,1); */ printf("\tnegl %s\n", register_name(creg,0)); return; case BNOT: /* ~ */ g_expr(e2); /* use_data_reg(creg,1); */ printf("\tnotl %s\n", register_name(creg,0)); return; case LNOT: /* ! */ g_expr(e2); use_data_reg(creg,1); xrn = register_name(creg,1); printf("\tcmpl $0,%s\n", register_name(creg,0)); printf("\tsete %s\n", xrn); printf("\tmovzbl %s,%s\n", xrn,register_name(creg,0)); return; case PREINC: if (car(e2)==REGISTER) { printf("\taddl $%d,%s\n",caddr(e1),register_name(cadr(e2),0)); printf("\tmovl %s,%s\n",register_name(cadr(e2),0),register_name(creg,0)); return; } g_expr(e2); xrn = register_name(creg,0); printf("\taddl $%d,(%s)\n",caddr(e1),xrn); printf("\tmovl (%s),%s\n",xrn,xrn); return; case POSTINC: if (car(e2)==REGISTER) { printf("\tmovl %s,%s\n",register_name(cadr(e2),0),register_name(creg,0)); printf("\taddl $%d,%s\n",caddr(e1),register_name(cadr(e2),0)); return; } g_expr(e2); emit_push(); /* in case of register full we should copy creg to dreg */ xrn = register_name(emit_pop(0),0); printf("\tmovl (%s),%s\n",xrn,register_name(creg,0)); printf("\taddl $%d,(%s)\n",caddr(e1),xrn); return; case CPOSTINC: /* char *p; *p++ */ if (car(e2)==REGISTER) { printf("\tmovl %s,%s\n",register_name(cadr(e2),0),register_name(creg,0)); printf("\taddl $%d,%s\n",caddr(e1),register_name(cadr(e2),0)); return; } g_expr(e2); emit_push(); /* in case of register full we should copy creg to dreg */ xrn = register_name(emit_pop(0),1); printf("\tmovsbl (%s),%s\n",xrn,register_name(creg,0)); printf("\tincl (%s)\n",xrn); return; case CPREINC: if (car(e2)==REGISTER) { printf("\taddl $%d,%s\n",caddr(e1),register_name(cadr(e2),0)); printf("\tmovl %s,%s\n",register_name(cadr(e2),0),register_name(creg,0)); return; } g_expr(e2); printf("\tincl (%s)\n",register_name(creg,0)); printf("\tmovsbl (%s),%s\n",register_name(creg,1),register_name(creg,0)); return; case CPOSTDEC: if (car(e2)==REGISTER) { printf("\tmovl %s,%s\n",register_name(cadr(e2),0),register_name(creg,0)); printf("\tdecl %s\n",register_name(cadr(e2),0)); return; } g_expr(e2); printf("\tmovsbl (%s),%s\n",register_name(creg,1),register_name(creg,0)); printf("\tdecl (%s)\n",register_name(creg,0)); return; case CPREDEC: if (car(e2)==REGISTER) { printf("\tdecl %s\n",register_name(cadr(e2),0)); printf("\tmovl %s,%s\n",register_name(cadr(e2),0),register_name(creg,0)); return; } g_expr(e2); emit_push(); e2 = emit_pop(0); printf("\tdecl (%s)\n",register_name(e2,0)); printf("\tmovsbl (%s),%s\n",register_name(e2,1),register_name(creg,0)); return; case MUL: case UMUL: case DIV: case UDIV: case MOD: case UMOD: case LSHIFT: case ULSHIFT: case RSHIFT: case URSHIFT: case ADD: case SUB: case BAND: case EOR: case BOR: machinop(e1); return; case COND: e2=fwdlabel(); b_expr(cadr(e1),0,e2); g_expr(caddr(e1)); e4 = rname[creg]; jmp(e3=fwdlabel()); fwddef(e2); g_expr(cadddr(e1)); use_register(creg,e4,1); fwddef(e3); return; case SASS: sassign(e1); return; case ASS: case CASS: assign(e1); return; case ASSOP: case CASSOP: assop(e1); return; case COMMA: g_expr(e2); g_expr(caddr(e1)); return; case RETURN: n = (NMTBL *)e2; if (retcont==0) retcont=fwdlabel(); printf("\tleal _%d,%s\n",retcont,register_name(creg,0)); return; case ENVIRONMENT: printf("\tmovl %%ebp,%s\n",register_name(creg,0)); return; default: b_expr(e1,1,e2=fwdlabel()); /* including > < ... */ xrn = register_name(creg,0); printf("\txorl %s,%s\n",xrn,xrn); jmp(e3=fwdlabel()); fwddef(e2); printf("\tmovl $1,%s\n",xrn); fwddef(e3); } } void bexpr(int e1, char cond, int l1) { gexpr_init(); b_expr(e1,cond,l1); } void b_expr(int e1, char cond, int l1) { int e2,l2; if (chk) return; e2=cadr(e1); switch(car(e1)) { case LNOT: b_expr(e2,!cond,l1); return; case GT: rexpr(e1,l1,cond?"g":"le"); return; case UGT: rexpr(e1,l1,cond?"a":"be"); return; case GE: rexpr(e1,l1,cond?"ge":"l"); return; case UGE: rexpr(e1,l1,cond?"ae":"b"); return; case LT: rexpr(e1,l1,cond?"l":"ge"); return; case ULT: rexpr(e1,l1,cond?"b":"ae"); return; case LE: rexpr(e1,l1,cond?"le":"g"); return; case ULE: rexpr(e1,l1,cond?"be":"a"); return; case EQ: rexpr(e1,l1,cond?"e":"ne"); return; case NEQ: rexpr(e1,l1,cond?"ne":"e"); return; case LAND: b_expr(e2,0,cond?(l2=fwdlabel()):l1); b_expr(caddr(e1),cond,l1); if(cond) fwddef(l2); return; case LOR: b_expr(e2,1,cond?l1:(l2=fwdlabel())); b_expr(caddr(e1),cond,l1); if(!cond) fwddef(l2); return; case CRGVAR: printf("\tcmpb $0,%s\n",(char *)caddr(e1)); jcond(l1,cond); return; case CRLVAR: printf("\tcmpb $0,%d(%%ebp)\n",lvar(e2)); jcond(l1,cond); return; case RGVAR: printf("\tcmpl $0,%s\n",(char *)caddr(e1)); jcond(l1,cond); return; case RLVAR: printf("\tcmpl $0,%d(%%ebp)\n",lvar(e2)); jcond(l1,cond); return; case REGISTER: printf("\tcmpl $0,%s\n",register_name(e2,0)); jcond(l1,cond); return; case CONST: if((cond&&e2)||(!cond&&!e2)) jmp(l1); return; default: g_expr(e1); printf("\tcmpl $0,%s\n",register_name(creg,0)); jcond(l1,cond); return; } } void ascii(char *s) { printf("\t.string \""); while(*s) { if (*s=='\n') printf("%cn",92); else if (*s<' ') printf("%c%03o",92,*s); else if (*s==34) printf("%c%c",92,34); else printf("%c",*s); s++; } printf("%c\n",34); } void string(int e1) { char *s; int i,lb; if (0) { s=(char *)cadr(e1); lb=fwdlabel(); printf("\tjmp _%d\n",lb); i=backdef(); ascii(s); printf("\t.align 2\n"); fwddef(lb); printf("\tlea _%d,%s\n",i,register_name(creg,0)); } else { s=(char *)cadr(e1); printf(".section\t.rodata\n"); lb=fwdlabel(); printf("_%d:\n",lb); ascii(s); if (output_mode==TEXT_EMIT_MODE) { printf(".text\n"); } else { text_mode(); } printf("\tlea _%d,%s\n",lb,register_name(creg,0)); } } #define MAX_COPY_LEN 20 void emit_copy(char *from,char *to,int length,int offset) { if (length<0) return; switch (length) { case 0: break; case 1: printf("\tmovb %d(%s),%s\n",offset,from, reg_name_l[rname[dreg]] ); printf("\tmovb %s,%d(%s)\n",reg_name_l[rname[dreg]] ,offset,to); break; case 2: printf("\tmovw %d(%s),%s\n",offset,from, reg_name_w[rname[dreg]] ); printf("\tmovw %s,%d(%s)\n",reg_name_w[rname[dreg]] ,offset,to); break; case 4: printf("\tmovl %d(%s),%s\n",offset,from, register_name(dreg,0)); printf("\tmovl %s,%d(%s)\n",register_name(dreg,0), offset,to); break; default: if (length <=MAX_COPY_LEN) { for(;length>=4;length-=4,offset+=4) emit_copy(from,to,4,offset); for(;length>=2;length-=2,offset+=2) emit_copy(from,to,2,offset); if(length>0) emit_copy(from,to,length,offset); return; } printf("\txchg %%edi,%s\n",from); printf("\txchg %%esi,%s\n",to); printf("\txchg %%ecx,%s\n",register_name(dreg,0)); printf("\tmovl $%d,%%ecx\n",length/4); printf("\tcld\n\trep\n\tmovsl\n"); printf("\txchg %%ecx,%s\n",register_name(dreg,0)); printf("\txchg %%esi,%s\n",to); printf("\txchg %%edi,%s\n",from); if(length%4) { emit_copy("%esi","%edi",length,offset+length/4); } } } int struct_push(int e4,int t) { int length; g_expr(e4); length=size(t); if(length%size_of_int) { length += size_of_int - (length%size_of_int); } printf("\tsubl $%d,%%esp\n",length); if (length<=MAX_COPY_LEN) emit_copy(register_name(creg,0),"%esp",length,0); else { printf("\tpushl %%edi\n"); printf("\tpushl %%esi\n"); printf("\tpushl %%ecx\n"); printf("\tleal 12(%%esp),%%di\n"); if (rname[creg]!=REG_ESI) printf("\tmovl %s,%%esi\n",register_name(creg,0)); printf("\tmovl $%d,%%ecx\n",length/size_of_int); printf("\tcld\n\trep\n\tmovsl\n"); printf("\tpopl %%ecx\n"); printf("\tpopl %%esi\n"); printf("\tpopl %%edi\n"); } return length/size_of_int; } void function(int e1) { int e2,e3,e4,e5,nargs,t; NMTBL *n; e2 = cadr(e1); nargs = 0; for (e3 = caddr(e1); e3; e3 = cadr(e3)) { t=caddr(e3); n=(NMTBL *)(e5=(cadr(e4 = car(e3)))); switch(car(e4)) { case FNAME: printf("\tlea %s,%s\n",n->nm,register_name(creg,0)); printf("\tpushl %s\n",register_name(creg,0)); break; case ADDRESS: g_expr(e5); printf("\tpushl %s\n",register_name(creg,0)); break; default: if(scalar(t)) { g_expr(e4); printf("\tpushl %s\n",register_name(creg,0)); } else if (car(t)==STRUCT||car(t)==UNION) { nargs += struct_push(e4,t); continue; } else { error(TYERR); } } ++nargs; } if (car(e2) == FNAME) { n=(NMTBL *)cadr(e2); use_register(creg,REG_EAX,0); printf("\tcall\t%s\n",n->nm); } else { g_expr(e2); use_register(creg,REG_EAX,0); printf("\tcall\t*%s\n",register_name(creg,0)); } if (nargs) printf("\taddl $%d,%%esp\n",size_of_int*nargs); } int arg_size(int e3,int *nargs0) { int i,nargs,offset_list,e,t; offset_list = 0; /* we should use prototypes's type */ for (i = nargs = 0; e3;e3 =cadr(e3)) { e = car(e3); t = caddr(e3); if (i < MAX_REGISTER_VAR && scalar(t)) { offset_list = list3(-(REG_ESI+i++),offset_list,e); } else { offset_list = list3(nargs,offset_list,e); nargs += (car(e3)==CHAR?size_of_int:size(t)); } } *nargs0 = -nargs; return offset_list; } int is_simple(int e1) { int e2 = car(e1); return ( e2==FNAME || e2==LVAR || e2==CRLVAR || e2==REGISTER ); } void jump_new(int e1, int env) { int e2; NMTBL *code0; /* jump アドレスを計算する */ /* compute jump address */ e2 = cadr(e1); if (car(e2) == FNAME) { code0=(NMTBL *)cadr(e2); if (code0->sc!=CODE) { error(STERR); return; } } else { /* indirect */ g_expr(e2); emit_push(); } /* まず、複雑な式を前もって単純な式に落す。 必要なら局所変数を用いる。 局所変数へのオフセットを覚えておく */ /* スタックの位置を修正する */ /* オフセットを用いて、 並べ変えを行う */ /* スタックの位置を修正する */ /* jump code を生成する */ } void jump(int e1, int env) { int i,args,e2,e3,e4,e5,nargs,regs; NMTBL *n,*code0; int new_disp,scode,disp1; char *xrn; /* We need three passes. Compute Stack size, Compute Arg, Copy it. */ /* count number of args */ args = caddr(e1); args = reverse0(args); nargs = arg_size(args,&new_disp); /* compute in normal order */ disp1 = (fnptr->sc==CODE)?0:-size_of_int; if (new_disp+disp1 < disp) { /* have to extend stack */ if (fnptr->sc==CODE) printf("\tleal %d(%%ebp),%%esp\n",new_disp-size_of_int); else printf("\tleal %d(%%ebp),%%esp\n",new_disp+disp_offset); } /* compute jump address */ e2 = cadr(e1); if (car(e2) == FNAME) { code0=(NMTBL *)cadr(e2); if (code0->sc!=CODE) { error(STERR); return; } } else { /* indirect */ g_expr(e2); emit_push(); } /* compute arguments in reverse order */ /* printf("# jump code_arg_offset=%d code_disp_offset=%d\n",code_arg_offset,code_disp_offset); */ regs = 0; i=MAX_REGISTER_VAR; for (e3=nargs; e3;e3 =cadr(e3)) { n=(NMTBL *)(e5=(cadr(e4 = caddr(e3)))); switch(car(e4)) { case FNAME: printf("\tlea %s,%s\n",n->nm,register_name(creg,0)); emit_push(); break; case ADDRESS: g_expr(e5); emit_push(); break; case RLVAR: case CRLVAR: if (env==0 && fnptr->sc==CODE) { /* printf("# e5=%d car(e3)=%d\n",e5,car(e3)); */ if (e5>=0 && e5==car(e3)) { /* The same positioned local variable. No need to copy */ reg_stack[reg_sp++] = -2; } break; } g_expr(e4); emit_push(); break; case REGISTER: /* printf("# i=%d rname[e5]=%d\n",i,rname[e5]); */ if (i>0 && rname[e5]==REG_ESI+ --i) { /* The same register variable. No need to copy */ reg_stack[reg_sp++] = e5; break; } default: g_expr(e4); emit_push(); } regs++; } if (env) { /* change the frame pointer */ g_expr(env); printf("\tmovl %s,%%ebp\n",register_name(creg,0)); } else if (fnptr->sc==FUNCTION) { printf("\tlea %d(%%ebp),%%ebp\n",disp_offset); } /* force lvar offset mode to CODE */ scode = fnptr->sc; fnptr->sc = CODE; /* printf("# jump2 code_arg_offset=%d code_disp_offset=%d\n",code_arg_offset,code_disp_offset); */ /* copy arguments to destination environment if necessary */ nargs = reverse0(nargs); /* pop in normal order */ i=0; for (e3=nargs; e3;e3 =cadr(e3)) { if ((e4=car(e3))<0) { /* register case */ if (reg_stack[--reg_sp]>=REG_ESI) { /* the same registger */ } else { if(reg_stack[reg_sp]<0) { printf("\tpopl %s\n",reg_name[rname[REG_ESI+i]]); /* e4? */ } else { printf("\tmovl %s,%s\n", reg_name[rname[reg_stack[reg_sp]]], reg_name[rname[REG_ESI+i]]); /* e4? */ free_register(reg_stack[reg_sp]); } i++; } } else { /* local variable case */ if (reg_stack[reg_sp-1]== -2) { /* same positioned variable */ reg_sp--; } else { printf("\tmovl %s,%d(%%ebp)\n",register_name(emit_pop(0),0), lvar(e4)); } } } if (car(e2) != FNAME) { xrn=register_name(emit_pop(0),0); } if (!env && new_disp+disp1>disp) { /* shrink stack if necessary */ printf("\tleal %d(%%ebp),%%esp\n",new_disp-size_of_int); } if (car(e2) == FNAME) { printf("\tjmp %s\n",code0->nm); } else { printf("\tjmp *%s\n",xrn); } fnptr->sc = scode; } void machinop(int e1) { int e2,e3,op; e2 = cadr(e1); op = car(e1); e3 = caddr(e1); g_expr(e3); emit_push(); g_expr(e2); tosop(car(e1)); return; } void rindirect(int e1) /* *(p +5 ) */ { char *op; int e2,e3,byte; op = ((byte = (car(e1) == CRINDIRECT)) ? "movsbl" : "movl"); e3 = cadr(e2 = cadr(e1)); g_expr(e2); printf("\t%s (%s),%s\n",op,register_name(creg,byte),register_name(creg,byte)); } void sassign(int e1) { int e2,e3,e4,sz; char *xrn; /* structure assignment */ e2 = cadr(e1); e3 = cadr(e2); e4 = caddr(e1); sz = size(cadr(e2)); /* cld rep movsb */ g_expr(e4); emit_push(); g_expr(e2); xrn = register_name(emit_pop(0),0); return; } void assign(int e1) { char *op; int e2,e3,e4,byte; op = ((byte=(car(e1) == CASS))? "movb" : "movl"); /* e2=e4 */ e2 = cadr(e1); e3 = cadr(e2); e4 = caddr(e1); switch(car(e2)) { case GVAR: /* i=3 */ g_expr(e4); if (byte) use_data_reg(creg,1); printf("\t%s %s,%s\n",op,register_name(creg,byte),(char *)caddr(e2)); return; case LVAR: g_expr(e4); if (byte) use_data_reg(creg,1); printf("\t%s %s,%d(%%ebp)\n",op,register_name(creg,byte),lvar(cadr(e2))); return; case REGISTER: g_expr(e4); if (creg!=cadr(e2)) printf("\t%s %s,%s\n",op,register_name(creg,byte),register_name(cadr(e2),0)); return; } g_expr(e4); emit_push(); g_expr(e2); e2 = emit_pop(0); if (byte) use_data_reg(creg,1); printf("\t%s %s,(%s)\n",op,register_name(e2,byte),register_name(creg,0)); printf("\tmovl %s,%s\n",register_name(e2,0),register_name(creg,0)); return; } void assop(int e1) { int e2,e3,byte,op,new_reg; char *xrn; int xreg; /* e2 op= e3 */ byte = (car(e1) == CASSOP); e2 = cadr(e1); if (car(e2)==INDIRECT) e2=cadr(e2); e3 = caddr(e1); op = cadddr(e1); g_expr(e3); emit_push(); if (car(e2)==REGISTER) { new_reg=creg; creg = cadr(e2); tosop(op); creg = new_reg; printf("\tmovl %s,%s\n",register_name(cadr(e2),0),register_name(creg,0)); return; } g_expr(e2); xrn = register_name(xreg = emit_pop(0),0); /* pop e3 value */ printf("\tpushl %s # assop \n",register_name(creg,0)); /* push e2 address */ ld_indexx(byte,0,creg); new_reg = get_register(); /* push e3 value */ if(new_reg<0) { /* もうレジスタがない */ reg_stack[reg_sp++] = -1; printf("\tpushl %s\n",xrn); } else { reg_stack[reg_sp++] = xreg; /* push するかわりにレジスタを使う */ } tosop(op); if(new_reg>=0) free_register(new_reg); printf("\tpopl %s # assop \n",register_name(dreg,0)); printf("\t%s %s,(%s)\n",byte ? "movb" : "movl",register_name(creg,byte),register_name(dreg,byte)); return; } void tosop(int op) { int oreg; char *orn,*crn; oreg = pop_register(); switch(op) { case LSHIFT: case ULSHIFT: shift("sall",oreg); return; case RSHIFT: shift("sarl",oreg); return; case URSHIFT: shift("shrl",oreg); return; } if(oreg==-1) { printf("\tpopl %s\n",register_name(dreg,0)); oreg = dreg; } orn = register_name(oreg,0); crn = register_name(creg,0); switch(op) { case ADD: printf("\taddl %s,%s\n",orn,crn); return; case SUB: printf("\tsubl %s,%s\n",orn,crn); return; case BAND: printf("\tandl %s,%s\n",orn,crn); return; case EOR: printf("\txorl %s,%s\n",orn,crn); return; case BOR: printf("\torl %s,%s\n",orn,crn); return; case MUL: case UMUL: /* use_register(dreg,REG_EDX,0); */ printf("\t%s %s,%s\n","imull",orn,crn); return; case DIV: case UDIV: orn = div_setup(oreg); if (op==DIV) printf("\tcltd\n\tdivl %s\n",orn); else printf("\txor %%edx,%%edx\n\tidivl %s\n",orn); div_cleanup(orn); return; case MOD: case UMOD: orn = div_setup(oreg); if (op==DIV) printf("\tcltd\n\tdivl %s\n",orn); else printf("\txor %%edx,%%edx\n\tidivl %s\n",orn); rname[creg]=REG_EDX; rname[dreg]=REG_EAX; div_cleanup(orn); return; } } static char *dvi_push = "(%esp)"; char * div_setup(int oreg) { if (oreg<0) { use_register(creg,REG_EAX,1); use_register(dreg,REG_EDX,0); return 0; } if (register_full()) { printf("\tpushl %s\n",register_name(oreg,0)); use_register(creg,REG_EAX,1); use_register(oreg,REG_EDX,0); return dvi_push; } regs[oreg]=1; use_register(creg,REG_EAX,1); use_register(dreg,REG_EDX,0); regs[oreg]=0; return register_name(oreg,0); } void div_cleanup(char *orn) { if (orn==dvi_push) printf("\taddl $4,%%esp\n"); } void shift(char *op, int reg) { if (reg>=0) { use_register(lreg,REG_ECX,1); } else { use_register(dreg,REG_ECX,0); printf("\tpopl %%ecx\n"); } printf("\t%s %%cl,%s\n",op,register_name(creg,0)); } void ld_indexx(int byte, int n, int xreg) { char *op; op = byte ? "movsbl" : "movl"; if (n) printf("\t%s %d(%s),%s\n",op,n,register_name(xreg,0),register_name(creg,byte)); else printf("\t%s (%s),%s\n",op,register_name(xreg,0),register_name(creg,byte)); } /* void st_indexx(int byte, int n, int xreg) { char *op; op = byte ? "movb" : "movl"; if (n) printf("\t%s %s,%d(%s)\n",op,register_name(creg,byte),n,register_name(xreg,0)); else printf("\t%s %s,(%s)\n",op,register_name(creg,byte),register_name(xreg,0)); } void indexy(char *op, char *gvar) { printf("\t%s %s,%s\n",op,register_name(creg,0),gvar); } */ void cmpdimm(int e, int csreg) { /* used in dosiwtch() */ use_register(creg,csreg,0); printf("\tcmpl $%d,%s\n",e,register_name(creg,0)); } void opening(char *filename) { printf("\t.file \"%s\"\n",filename); printf("\t.version\t\"01.01\"\n"); printf("gcc2_compiled.:\n"); printf(".text\n"); emit_init(); } void closing(void) { global_table(); printf("\t.ident \"Micro-C compiled\"\n"); } /* void jmp_label(int l) { printf("\tjmp\t_%d\n",l); } */ /* void jmp_eq_label(int l) { printf("\tje\t_%d\n",l); } */ void rexpr(int e1, int l1, char *s) { g_expr(list3(SUB,cadr(e1),caddr(e1))); printf("\tj%s\t_%d\n",s,l1); } void jcond(int l, char cond) { printf("\tj%s\t_%d\n",cond?"ne":"e",l); } void jmp(int l) { control=0; printf("\tjmp\t_%d\n",l); /* align? */ } int fwdlabel(void) { return labelno++; } void fwddef(int l) { control=1; printf("_%d:\n",l); } int backdef(void) { control=1; printf("_%d:\n",labelno); return labelno++; } void def_label(int cslabel, int dlabel) { int fl; fl = 0; if (control) { jmp(fl=fwdlabel()); } fwddef(cslabel); if (dlabel) jmp(dlabel); if (fl) { fwddef(fl); } } void gen_comment(char *s) { printf("# %s",s); } void gen_source(char *s) { printf("%s",s); } void code_enter(char *name) { printf("\t.align 4\n"); if (stmode!=STATIC) printf(".globl %s\n",name); printf("\t.type\t%s,@function\n",name); printf("%s:\n",name); } void code_enter1(int disp0,int args) { if(disp0) { printf("\tsubl $%d,%%esp\n",-disp0); } printf("# args %d disp %d code_arg_offset=%d code_disp_offset=%d\n",args,disp,code_arg_offset,code_disp_offset); } void code_leave(char *name) { printf("_%d:\n",labelno); printf("\t.size\t%s,_%d-%s\n",name,labelno,name); local_table(); labelno++; } void enter(char *name) { printf("\t.align 2\n"); if (stmode!=STATIC) printf(".globl %s\n",name); printf("%s:\n",name); printf("\t.type\t%s,@function\n",name); printf("\tpushl %%ebp\n"); printf("\tmovl %%esp,%%ebp\n"); printf("\tpushl %%ebx\n"); printf("\tpushl %%ecx\n"); printf("\tpushl %%edx\n"); if (REGISTERS6) { printf("\tpushl %%edi\n"); printf("\tpushl %%esi\n"); } } void enter1(int disp) { if(disp) printf("\tsubl $%d,%%esp\n",-disp); } void leave(int control, char *name) { if (control) use_register(creg,REG_EAX,1); if (retcont) { if (control) jmp(retlabel); fwddef(retcont); use_register(creg,REG_EAX,0); printf("\tmovl %s,%s\n",reg_name[REG_ESI],register_name(creg,0)); /* printf("\tleave\n"); */ } fwddef(retlabel); use_register(creg,REG_EAX,0); if (REGISTERS6) { printf("\tpopl %%esi\n"); printf("\tpopl %%edi\n"); } /* if(disp) printf("\taddl $%d,%%esp\n",-disp); */ printf("\tlea %d(%%ebp),%%esp\n",disp_offset); printf("\tpopl %%edx\n"); printf("\tpopl %%ecx\n"); printf("\tpopl %%ebx\n"); printf("\tleave\n"); printf("\tret\n"); printf("_%d:\n",labelno); printf("\t.size\t%s,_%d-%s\n",name,labelno,name); local_table(); labelno++; } void ret(void) { use_register(creg,REG_EAX,1); jmp(retlabel); } void gen_gdecl(char *n, int gpc) { /* if (stmode!=STATIC) printf(".globl %s\n",n); */ } void align(int t) { if (t!=CHAR) { if (data_alignment & 1) printf("\t.align 2\n"); data_alignment = 0; } } void emit_data(int e, int t, NMTBL *n) { int l; char *name; name = n->nm; if(mode!=GDECL) { error(-1); return; } if (n->dsp != -1) { n->dsp = -1; /* initiallized flag */ printf(".globl\t%s\n",name); data_mode(name); align(t); printf("%s:\n",name); } else { data_mode(0); } if(car(e)==CONST) { if (t==CHAR) { printf("\t.byte %d\n",cadr(e)); if (data_alignment>0) data_alignment++; gpc += 1; } else { printf("\t.long %d\n",cadr(e)); gpc += size_of_int; } } else if(t!=CHAR) { gpc += size_of_int; if(car(e)==ADDRESS&&car(cadr(e))==GVAR) { printf("\t.long %s\n",(char *)caddr(cadr(e))); } else if(car(e)==FNAME) { printf("\t.long %s\n",((NMTBL *)cadr(e))->nm); } else if(car(e)==STRING) { if (car(n->ty)!=ARRAY || cadr(n->ty)!=CHAR) { l = fwdlabel(); printf("\t.long _%d\n",l); printf(".section\t.rodata\n"); printf("_%d:\n",l); output_mode = RODATA_EMIT_MODE; } ascii((char *)cadr(e)); } else error(TYERR); } } void emit_data_closing(NMTBL *n) { int lb; if (mode==GDECL) { data_mode(0); lb=fwdlabel(); printf("_%d:\n",lb); printf("\t.size\t%s,_%d-%s\n",n->nm,lb,n->nm); } } void global_table(void) { NMTBL *n; int init; init=0; for(n=ntable;n < &ntable[GSYMS];n++) { if (n->sc == GVAR && n->dsp != -1) { /* n->dsp = -1 means initialized global */ if (init==0) { data_mode(0); init=1; } printf(".comm %s,%d\n",n->nm,size(n->ty)); } } } void local_table(void) { NMTBL *n; int init; init=0; /* static local variables */ for(n=ntable+GSYMS;n < &ntable[GSYMS+LSYMS];n++) { if (n->sc == GVAR) { if (init==0) { data_mode(0); init=1; } printf(".lcomm %s,%d\n",n->nm,size(n->ty)); } } } void text_mode(void) { if (output_mode!=TEXT_EMIT_MODE) { printf(".text\n"); printf("\t.align 2\n"); output_mode = TEXT_EMIT_MODE; } } void data_mode(char *name) { if (output_mode!=DATA_EMIT_MODE) { printf(".data\n"); output_mode = DATA_EMIT_MODE; } if (name) printf("\t.type\t%s,@object\n",name); } int lvar(int l) { if (l<0) { if (fnptr->sc==CODE) return l+code_disp_offset; else return l+disp_offset; } else { if (fnptr->sc==CODE) return -l+code_arg_offset; else return l+arg_offset; } } /* end */