Mercurial > hg > CbC > old > device
view mc-nop-386.c @ 0:d35df41eac69
Initial revision
author | kono |
---|---|
date | Thu, 13 Jan 2000 02:41:41 +0900 |
parents | |
children | 0529f5abe9d0 |
line wrap: on
line source
/* Micro-C Code Generatation Part for intel386 */ #define EXTERN extern #include "mc.h" #define REGISTERS6 0 void code_init(void); void free_register(int i); void emit_init(void); void emit_push(void); void gexpr(int e1); void g_expr(int e1); void bexpr(int e1, char cond, int l1); void b_expr(int e1, char cond, int l1); void string(int e1); void function(int e1); void machinop(int e1); void rindirect(int e1); void assign(int e1); void sassign(int e1); void assop(int e1); void tosop(int op); char *div_setup(int oreg) ; void shift(char *op, int reg) ; void ld_indexx(int byte, int n, char *xrn); void st_indexx(int byte, int n, char *xrn); void indexy(char *op, char *gvar); void cmpdimm(int e, int csreg); void global_table(void) ; void local_table(void) ; void opening(char *filename); void closing(void); void gen_gdecl(char *n, int gpc) ; void jmp_label(int l); void jmp_eq_label(int l); void rexpr(int e1, int l1, char *s); void jcond(int l, char cond); void jmp(int l); void def_label(int cslabel, int dlabel) ; void gen_comment(char *s); void gen_source(char *s); void code_enter(char *name) ; void code_leave(char *name) ; void enter(char *name) ; void enter1(int disp) ; void ret(void); void emit_data(int e, int t, NMTBL *n); void text_mode(void); void data_mode(char *name); int get_register_var(void); void jump(int e1, int env); int lvar(int l); int cadr(int e); int car(int e); int caddr(int e); int fwdlabel(void); void fwddef(int l); int cadddr(int e); int backdef(void); int error(int n); int size(int t); int list3(int e1, int e2, int e3); #define TEXT_EMIT_MODE 0 #define DATA_EMIT_MODE 1 #define RODATA_EMIT_MODE 2 static output_mode = TEXT_EMIT_MODE; static 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 disp_offset = -20; int func_disp_offset = -20; int code_disp_offset = 0; int int_size = 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 char *crn; /* current register name */ static int lreg; /* operand register */ static char *lrn; /* operand register name */ static int dreg; /* temporary register */ static char *drn; /* temporary register name */ static int xreg; /* pointer 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]; /* 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; int_size = 4; endian = 0; MAX_REGISTER=6; MAX_DATA_REG=4; MAX_POINTER=3; } else { arg_offset = 8; func_disp_offset = -12; int_size = 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"; } char * register_name(int i) { 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) { int i; 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(); crn = reg_name[rname[creg]]; dreg = get_register(); drn = reg_name[rname[dreg]]; reg_sp = 0; text_mode(); } void set_crn(int i) { creg = i; crn = reg_name[rname[creg]]; } void set_drn(int i) { dreg = i; drn = reg_name[rname[dreg]]; } 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; crn = reg_name[rname[creg]]; drn = reg_name[rname[dreg]]; lrn = reg_name[rname[lreg]]; } 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; lrn = reg_name[rname[lreg]]; 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",crn); } else { reg_stack[reg_sp++] = creg; /* push するかわりにレジスタを使う */ set_crn(new_reg); } } char * emit_pop(int type) { char *xrn; 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",drn); xrn = drn; xreg = dreg; } else { xrn = lrn; xreg = lreg; } return xrn; } 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",caddr(e1),crn); return; case RGVAR: /* use_pointer(creg,0); */ printf("\tmovl %s,%s\n",caddr(e1),crn); return; case CRGVAR: printf("\tmovsbl %s,%s\n",caddr(e1),crn); return; case LVAR: /* use_pointer(creg,0); */ printf("\tlea %d(%%ebp),%s\n",lvar(e2),crn); return; case REGISTER: /* this is of course redundant... */ printf("\tmovl %s,%s\n",register_name(e2),crn); return; case RLVAR: printf("\tmovl %d(%%ebp),%s\n",lvar(e2),crn); return; case CRLVAR: printf("\tmovsbl %d(%%ebp),%s\n",lvar(e2),crn); return; case FNAME: printf("\tmovl $%s,%s\n",((NMTBL *)e2)->nm,crn); return; case CONST: /* 代入する値が0でも特別な処理はしない */ printf("\tmovl $%d,%s\n",e2,crn); 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", crn); return; case BNOT: /* ~ */ g_expr(e2); /* use_data_reg(creg,1); */ printf("\tnotl %s\n", crn); return; case LNOT: /* ! */ g_expr(e2); use_data_reg(creg,1); printf("\tcmpl $0,%s\n", crn); printf("\tsete %s\n", reg_name_l[rname[creg]]); printf("\tmovzbl %s,%s\n", reg_name_l[rname[creg]],crn); return; case PREINC: if (car(e2)==REGISTER) { printf("\taddl $%d,%s\n",caddr(e1),register_name(cadr(e2))); printf("\tmovl %s,%s\n",register_name(cadr(e2)),crn); return; } g_expr(e2); printf("\taddl $%d,(%s)\n",caddr(e1),crn); printf("\tmovl (%s),%s\n",crn,crn); return; case POSTINC: if (car(e2)==REGISTER) { printf("\tmovl %s,%s\n",register_name(cadr(e2)),crn); printf("\taddl $%d,%s\n",caddr(e1),register_name(cadr(e2))); return; } g_expr(e2); emit_push(); /* in case of register full we should copy crn to drn */ xrn = emit_pop(0); printf("\tmovl (%s),%s\n",xrn,crn); 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)),crn); printf("\taddl $%d,%s\n",caddr(e1),register_name(cadr(e2))); return; } g_expr(e2); emit_push(); /* in case of register full we should copy crn to drn */ xrn = emit_pop(0); printf("\tmovsbl (%s),%s\n",xrn,crn); printf("\tincl (%s)\n",xrn); return; case CPREINC: if (car(e2)==REGISTER) { printf("\taddl $%d,%s\n",caddr(e1),register_name(cadr(e2))); printf("\tmovl %s,%s\n",register_name(cadr(e2)),crn); return; } g_expr(e2); printf("\tincl (%s)\n",crn); printf("\tmovsbl (%s),%s\n",crn,crn); return; case CPOSTDEC: if (car(e2)==REGISTER) { printf("\tmovl %s,%s\n",register_name(cadr(e2)),crn); printf("\tdecl %s\n",register_name(cadr(e2))); return; } g_expr(e2); printf("\tmovsbl (%s),%s\n",crn,crn); printf("\tdecl (%s)\n",crn); return; case CPREDEC: if (car(e2)==REGISTER) { printf("\tdecl %s\n",register_name(cadr(e2))); printf("\tmovl %s,%s\n",register_name(cadr(e2)),crn); return; } g_expr(e2); emit_push(); xrn = emit_pop(0); printf("\tdecl (%s)\n",xrn); printf("\tmovsbl (%s),%s\n",xrn,crn); 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,crn); return; case ENVIRONMENT: printf("\tmovl %%ebp,%s\n",crn); return; default: b_expr(e1,1,e2=fwdlabel()); /* including > < ... */ printf("\txorl %s,%s\n",crn,crn); jmp(e3=fwdlabel()); fwddef(e2); printf("\tmovl $1,%s\n",crn); 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",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",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)); jcond(l1,cond); return; case CONST: if((cond&&e2)||(!cond&&!e2)) jmp(l1); return; default: g_expr(e1); printf("\tcmpl $0,%s\n",crn); 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,crn); } 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,crn); } } void function(int e1) { int e2,e3,e4,e5,nargs; NMTBL *n; e2 = cadr(e1); nargs = 0; for (e3 = caddr(e1); e3; e3 = cadr(e3)) { n=(NMTBL *)(e5=(cadr(e4 = car(e3)))); switch(car(e4)) { case FNAME: printf("\tlea %s,%s\n",n->nm,crn); printf("\tpushl %s\n",crn); break; case ADDRESS: g_expr(e5); printf("\tpushl %s\n",crn); break; default:g_expr(e4); printf("\tpushl %s\n",crn); } ++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",crn); } if (nargs) printf("\taddl $%d,%%esp\n",4*nargs); } void jump(int e1, int env) { int i,e2,e3,e4,e5,nargs,regs; NMTBL *n,*code0; int new_disp; char *xrn; /* count number of args */ nargs = 0; for (e3 = caddr(e1); e3;e3 =cadr(e3)) nargs++; new_disp = -(nargs-MAX_REGISTER_VAR)*int_size; if (new_disp > 0) new_disp=0; /* printf("# new_disp %d,disp %d\n",new_disp,disp); */ if (new_disp < disp) { printf("\tlea %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 */ regs = nargs-1; i=0; for (e3 = caddr(e1); e3;e3 =cadr(e3),i++) { n=(NMTBL *)(e5=(cadr(e4 = car(e3)))); switch(car(e4)) { case FNAME: printf("\tlea %s,%s\n",n->nm,crn); emit_push(); break; case ADDRESS: g_expr(e5); emit_push(); break; /* printf("# cadr(e4) %d ==disp_offset+(-1-i)*int_size %d\n", cadr(e4), disp_offset+(-2-i)*int_size); */ case RLVAR: case CRLVAR: if (env==0 && cadr(e4)==disp_offset+(-1-i)*int_size) { /* Same positioned local variable. No need to copy */ reg_stack[reg_sp++] = -2; --regs; continue; } case REGISTER: if (rname[cadr(e4)]==REG_ESI+regs) { /* Same register variable. No need to copy */ reg_stack[reg_sp++] = cadr(e4); --regs; continue; } default: g_expr(e4); emit_push(); } --regs; } /* save frame pointer before jump in fuction */ if (fnptr->sc==FUNCTION) { printf("\tmovl %%ebp,%d(%%ebp)\n",disp_offset); } if (env) { /* change frame pointer */ g_expr(env); printf("\tmovl %s,%%ebp\n",crn); } /* copy arguments to destination environment if necessary */ for(i=0;i<nargs;i++) { if (i>=MAX_REGISTGER_VAR) { if (reg_stack[reg_sp-1]== -2) { /* same positioned variable */ reg_sp--; } else { xrn=emit_pop(0); printf("\tmovl %s,%d(%%ebp)\n",xrn, disp_offset+(-(nargs-i))*int_size); } } else 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]]); } else { printf("\tmovl %s,%s\n", reg_name[rname[reg_stack[reg_sp]]], reg_name[rname[REG_ESI+i]]); free_register(reg_stack[reg_sp]); } } } if (car(e2) != FNAME) { xrn=emit_pop(0); } /* printf("# new_disp %d,disp %d\n",new_disp,disp); */ if (new_disp>disp) { /* shrink stack if necessary */ printf("\tlea %d(%%ebp),%%esp\n",new_disp+disp_offset); } if (fnptr->sc==FUNCTION) { /* link operation for leave, %ebp must point saved old %ebp */ printf("\tlea %d(%%ebp),%%ebp\n",disp_offset); } if (car(e2) == FNAME) { printf("\tjmp %s\n",code0->nm); } else { printf("\tjmp *%s\n",xrn); } } 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,crn,crn); } void sassign(int e1) { int e2,e3,e4,sz; char *xrn; 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 = emit_pop(0); return; } void assign(int e1) { char *op; int e2,e3,e4,byte; char *xrn; 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,crn,caddr(e2)); return; case LVAR: g_expr(e4); if (byte) use_data_reg(creg,1); printf("\t%s %s,%d(%%ebp)\n",op,crn,lvar(cadr(e2))); return; case REGISTER: g_expr(e4); if (creg!=cadr(e2)) printf("\t%s %s,%s\n",op,crn,register_name(cadr(e2))); return; } g_expr(e4); emit_push(); g_expr(e2); xrn = emit_pop(0); if (byte) use_data_reg(creg,1); printf("\t%s %s,(%s)\n",op,xrn,crn); printf("\tmovl %s,%s\n",xrn,crn); return; } void assop(int e1) { int e2,e3,byte,op,new_reg; char *xrn; /* 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; set_crn(cadr(e2)); tosop(op); set_crn(new_reg); printf("\tmovl %s,%s\n",register_name(cadr(e2)),crn); return; } g_expr(e2); xrn = emit_pop(0); /* pop e3 value */ printf("\tpushl %s # assop \n",crn); /* push e2 address */ ld_indexx(byte,0,crn); 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",drn); printf("\t%s %s,(%s)\n",byte ? "movb" : "movl",crn,drn); return; } void tosop(int op) { int oreg; char *orn; 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",drn); oreg = dreg; } orn = reg_name[rname[oreg]]; 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; crn=reg_name[REG_EDX]; rname[dreg]=REG_EAX; drn=reg_name[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",reg_name[rname[oreg]]); 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 reg_name[rname[oreg]]; } 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,crn); } void ld_indexx(int byte, int n, char *xrn) { char *op; op = byte ? "movsbl" : "movl"; if (n) printf("\t%s %d(%s),%s\n",op,n,xrn,crn); else printf("\t%s (%s),%s\n",op,xrn,crn); } void st_indexx(int byte, int n, char *xrn) { char *op; op = byte ? "movb" : "movl"; if (n) printf("\t%s %s,%d(%s)\n",op,crn,n,xrn); else printf("\t%s %s,(%s)\n",op,crn,xrn); } void indexy(char *op, char *gvar) { printf("\t%s %s,%s\n",op,crn,gvar); } void cmpdimm(int e, int csreg) { /* used in dosiwtch() */ use_register(creg,csreg,0); printf("\tcmpl $%d,%s\n",e,crn); } 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) { disp_offset = code_disp_offset; emit_init(); 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 disp) { if(disp) { printf("\tsubl $%d,%%esp\n",-disp); } } void code_leave(char *name) { local_table(); printf("_%d:\n",labelno); printf("\t.size\t%s,_%d-%s\n",name,labelno,name); labelno++; } void enter(char *name) { disp_offset = func_disp_offset; emit_init(); 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],crn); /* 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"); local_table(); printf("_%d:\n",labelno); printf("\t.size\t%s,_%d-%s\n",name,labelno,name); 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) { /* global */ 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 += int_size; } } else if(t!=CHAR) { gpc += int_size; if(car(e)==ADDRESS&&car(cadr(e))==GVAR) { printf("\t.long %s\n",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); } } else { /* if (n->sc!=LVAR) { error(TYERR); return; } */ if(car(e)==CONST) { printf("\tmovl $%d,%s\n",cadr(e),crn); printf("\t%s %s,%d(%%ebp)\n",t==CHAR?"movsbl":"movl",crn,n->dsp); } else if(t!=CHAR) { if(car(e)==ADDRESS&&car(cadr(e))==GVAR) printf("\tlea %s,%s\n",caddr(e),crn); else if(car(e)==FNAME) printf("\tlea %s,%s\n",((NMTBL *)cadr(e))->nm,crn); else if(car(e)==STRING) { string(e); } else error(TYERR); printf("\tmovl %s,%d(%%ebp)\n",crn,n->dsp); } } } 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; for(n=ntable;n < &ntable[GSYMS];n++) { if (n->sc == GVAR && n->dsp != -1) { /* n->dsp = -1 means initialized global */ printf(".comm %s,%d\n",n->nm,size(n->ty)); } } } void local_table(void) { NMTBL *n; /* static local variables */ for(n=ntable+GSYMS;n < &ntable[GSYMS+LSYMS];n++) { if (n->sc == GVAR) { 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 { return l+arg_offset; } } /* end */