Mercurial > hg > CbC > old > device
diff mc-code-ia32.c @ 89:917947ffeb7c
power pc version
author | kono |
---|---|
date | Thu, 06 Mar 2003 23:47:42 +0900 |
parents | 1738f313f98b |
children | e7f8515ba882 |
line wrap: on
line diff
--- a/mc-code-ia32.c Wed Mar 05 23:07:00 2003 +0900 +++ b/mc-code-ia32.c Thu Mar 06 23:47:42 2003 +0900 @@ -100,8 +100,7 @@ void code_leave(char *name); int lvar(int l); void global_table(void); - -char * fstore(int d); +int virtual(int real); char * fload(int d); int code_d1(double d); int code_d2(double d); @@ -163,6 +162,170 @@ return virtual(r+REG_ESI); } + + +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) { /* いらなくなったレジスタを開放 */ + regv[i]=regs[i]=0; +} + +int +register_full(void) +{ + int i; + for(i=0;i<MAX_REGISTER;i++) { + if (! regs[i]) { + return 0; + } + } + return 1; +} + +int +free_register_count(void) +{ + int i,count; + count = 0; + for(i=0;i<MAX_REGISTER;i++) { + if (! regs[i] && ! regv[i]) count++; + } + return count; +} + +void +free_all_register(void) +{ + int i; + for(i=0;i<MAX_REGISTER;i++) { + regs[i]=regv[i]=0; + } + creg = get_register(); + dreg = get_register(); + return; +} + +void +use_register_var(int i) { + regv[i]=1; +} + +void +set_register_var() { +} + +void +code_arg_register(int args) +{ + NMTBL *n; + if (args) { + /* process in reverse order */ + n = (NMTBL*)caddr(args); + if(n->sc==REGISTER) { + if ((n->dsp = get_register_var()) <0) { + error(-1); return; + } + use_register_var(n->dsp); /* it has now value in it */ + } + code_arg_register(cadr(args)); + } +} + + +void +register_usage(char *s) +{ + int i; + if (chk) return; + printf("# %d: %s:",lineno,s); + printf(" creg=%s dreg=%s ",register_name(creg,0),register_name(dreg,0)); + for(i=0;i<MAX_REGISTER;i++) { + printf("%d",regs[i]); + } + printf(":"); + for(i=0;i<MAX_REGISTER;i++) { + printf("%d",regv[i]); + } +#if 0 + printf(" regs_stack",register_name(creg,0),register_name(dreg,0)); + for(i=reg_sp;i>=0;i--) { + if(reg_stack[i]>=0) + printf(" %s",register_name(reg_stack[i],0)); + } +#endif + printf("\n"); +} + +void +gexpr_init(void) +{ + while(reg_sp > 0) { + free_register(reg_stack[--reg_sp]); + } + text_mode(); + gexpr_code_init(); + register_usage("gexpr_init"); +} + + +void +emit_init(void) +{ + int i; + for(i=0;i<MAX_REGISTER;i++) { regs[i]=0; regv[i]=0;rname[i]=i;} + free_all_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; + } + } + return real_v; +} + +int +pop_register(void) +{ /* レジスタから値を取り出す */ + return reg_stack[--reg_sp]; +} + +int +stack_used(void) { + return reg_stack[--reg_sp]<0; +} + +void +emit_pop_free(int xreg) +{ + if (xreg==dreg) { + regv[dreg]=0; + } else if (xreg!=-1) { + free_register(xreg); + } +} + + int get_register_var(void) { @@ -374,7 +537,8 @@ code_cpostinc(int e1,int e2) { char *xrn; if (car(e2)==REGISTER) { - printf("\tmovl %s,%s\n",register_name(cadr(e2),0),register_name(creg,0)); + printf("\tmovbl (%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; } @@ -391,7 +555,8 @@ code_cpreinc(int e1,int e2) { 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)); + printf("\tmovsbl (%s),%s\n",register_name(cadr(e2),0), + register_name(creg,0)); return; } g_expr(e2); @@ -403,7 +568,8 @@ void code_cpostdec(int e1,int e2) { if (car(e2)==REGISTER) { - printf("\tmovl %s,%s\n",register_name(cadr(e2),0),register_name(creg,0)); + printf("\tmovsbl (%s),%s\n",register_name(cadr(e2),0), + register_name(creg,0)); printf("\tdecl %s\n",register_name(cadr(e2),0)); return; } @@ -417,8 +583,8 @@ code_cpredec(int e1,int e2) { 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; + printf("\tmovsbl (%s),%s\n",register_name(cadr(e2),0), + register_name(creg,0)); } g_expr(e2); emit_push(); @@ -894,7 +1060,7 @@ case ADD: printf("\taddl %s,%s\n",orn,crn); break; - case SUB: + case SUB: case CMP: printf("\tsubl %s,%s\n",orn,crn); break; case BAND: @@ -1023,9 +1189,9 @@ } void -rexpr(int e1, int l1, char *s) +rexpr(int e1, int l1, char *s,int t) { - g_expr(list3(SUB,cadr(e1),caddr(e1))); + g_expr(list3(CMP,cadr(e1),caddr(e1))); printf("\tj%s\t_%d\n",s,l1); } @@ -1472,8 +1638,7 @@ case DSUB: printf("\tfsubp %%st,%%st(1)\n"); break; case DDIV: printf("\tfdivp %%st,%%st(1)\n"); break; case DMUL: printf("\tfmulp %%st,%%st(1)\n"); break; - case DCOMP: - /* printf("\tfxch\t%%st(1)\n"); */ + case DCMP: printf("\tfucompp\n"); printf("\tfnstsw\t%%ax\n"); break; @@ -1517,7 +1682,7 @@ void drexpr(int e1, int e2,int l1, int op) { - g_expr(list3(DCOMP,e1,e2)); + g_expr(list3(DCMP,e1,e2)); switch(op) { case DOP+GE: printf("\ttestb\t$5,%%ah\n");