Mercurial > hg > CbC > old > device
changeset 97:6d42fcac07af
*** empty log message ***
author | kono |
---|---|
date | Fri, 14 Mar 2003 01:07:37 +0900 |
parents | 7d8de41390d8 |
children | 07c2554e1cfa |
files | mc-code-ia32.c mc-code-powerpc.c mc-code.h mc-codegen.c mc-parse.c mc.h test/basic.c |
diffstat | 7 files changed, 195 insertions(+), 124 deletions(-) [+] |
line wrap: on
line diff
--- a/mc-code-ia32.c Thu Mar 13 22:48:57 2003 +0900 +++ b/mc-code-ia32.c Fri Mar 14 01:07:37 2003 +0900 @@ -1748,18 +1748,18 @@ } void -code_dregister(int e2) +code_fregister(int e2) { error(-1); } void -code_cmp_dregister(int r) +code_cmp_fregister(int r) { error(-1); } -int pop_dregister() +int pop_fregister() { if (freg_sp<0) { error(-1); return -1;} printf("# fpop: %d\n",freg_sp-1); @@ -1770,7 +1770,7 @@ emit_dpop(int type) { int xreg; - if ((xreg=pop_dregister())==-1) { + if ((xreg=pop_fregister())==-1) { } else if (xreg<= -REG_LVAR_OFFSET) { code_drlvar(lvar(REG_LVAR_OFFSET+xreg),1); /* pushed order is reversed. We don't need this for comutable
--- a/mc-code-powerpc.c Thu Mar 13 22:48:57 2003 +0900 +++ b/mc-code-powerpc.c Fri Mar 14 01:07:37 2003 +0900 @@ -39,10 +39,16 @@ arg2 12 4 see enter/enter1/leave see code_enter - r0 link register + r0 return value etc. + r3-r10 input register + r22-r29 saved register variable r30 stack pointer r31 0 r1 frame pointer + + f0 return value etc. + f1-r8 input register + f24-f31 saved register variable */ int arg_offset = 8; int disp_offset = -12; @@ -70,7 +76,7 @@ #define USING_REG 1 #define FREG_VAR_BASE 31 -#define FREG_VAR_MIN 22 +#define FREG_VAR_MIN 24 #define FREG_ARG_BASE 1 #define FREG_ARG_MAX 13 @@ -93,6 +99,8 @@ int *fregv = powerpc_fregv; int *fregs = powerpc_fregs; +static int max_reg_var, max_freg_var; + static char *reg_name[] = { "r0","r1","r2","r3","r4","r5","r6","r7","r8","r9", "r10","r11","r12","r13","r14","r15","r16","r17","r18","r19", @@ -130,7 +138,7 @@ } #define register_name(i) reg_name[i] -#define dregister_name(i) freg_name[i] +#define fregister_name(i) freg_name[i] void gexpr_code_init(void){ @@ -179,7 +187,7 @@ } int -get_dregister(void) +get_fregister(void) { /* 使われていないレジスタを調べる */ int i,n; for(i=MAX_TMP_FREG;i>MIN_TMP_FREG;i--) { @@ -206,16 +214,11 @@ int -pop_dregister(void) +pop_fregister(void) { /* レジスタから値を取り出す */ return freg_stack[--freg_sp]; } -int -stack_used(void) { - return reg_stack[--reg_sp]<0; -} - void emit_pop_free(int xreg) { @@ -228,7 +231,7 @@ } void -free_dregister(int i) { /* いらなくなったレジスタを開放 */ +free_fregister(int i) { /* いらなくなったレジスタを開放 */ fregv[i]=fregs[i]=0; } @@ -244,26 +247,14 @@ 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; - } + for(i=0;i<MAX_REGISTER;i++) { regs[i]=0; regv[i]=0; } + for(i=0;i<MAX_FREGISTER;i++) { fregs[i]=0; fregv[i]=0; } creg = get_register(); - freg = get_dregister(); + freg = get_fregister(); return; } @@ -289,10 +280,10 @@ } use_register_var(n->dsp); /* it has now value in it */ } else if(n->sc==DREGISTER) { - if ((n->dsp = get_dregister_var()) <0) { + if ((n->dsp = get_fregister_var()) <0) { error(-1); return; } - use_dregister_var(n->dsp); /* it has now value in it */ + use_fregister_var(n->dsp); /* it has now value in it */ } code_arg_register(cadr(args)); } @@ -339,9 +330,9 @@ emit_init(void) { int i; - for(i=0;i<MAX_REGISTER;i++) { regs[i]=0; regv[i]=0; } free_all_register(); init_ptr_cache(); + max_reg_var=0; max_freg_var=0; reg_sp = 0; text_mode(); } @@ -351,16 +342,16 @@ return REG_VAR_BASE-r; } + int get_register_var(void) { int i; for(i=0;i<MAX_REGISTGER_VAR;i++) { - if (REG_VAR_BASE-i<=REG_VAR_MIN) - return -1; if (! regs[REG_VAR_BASE-i]) { /* 使われていないなら */ regs[REG_VAR_BASE-i]=USING_REG; /* そのレジスタを使うことを宣言し */ regv[REG_VAR_BASE-i]=0; + if (i>max_reg_var) max_reg_var=i; return i; /* その場所を表す番号を返す */ } } @@ -368,20 +359,19 @@ } int -dregister_var(int r) { +fregister_var(int r) { return FREG_VAR_BASE-r; } int -get_dregister_var(void) +get_fregister_var(void) { int i; for(i=0;i<MAX_FREGISTGER_VAR;i++) { - if (FREG_VAR_BASE-i<=FREG_VAR_MIN) - return -1; if (! regs[FREG_VAR_BASE-i]) { /* 使われていないなら */ regs[FREG_VAR_BASE-i]=USING_REG; /* そのレジスタを使うことを宣言し */ regv[FREG_VAR_BASE-i]=0; + if (i>max_freg_var) max_freg_var=i; return i; /* その場所を表す番号を返す */ } } @@ -393,31 +383,21 @@ { int new_reg; new_reg = get_register(); - if(new_reg<0) { /* もうレジスタがない */ - if (reg_sp>=MAX_MAX) error(-1); - reg_stack[reg_sp++] = -1; - printf("\tstwu %s,-%d(%s)\n",register_name(creg), - size_of_int,register_name(reg_sp)); - /* creg is used soon, don't regv[creg]=0 */ - } else { - reg_stack[reg_sp++] = creg; /* push するかわりにレジスタを使う */ - creg = new_reg; - regv[creg]=1; - } + reg_stack[reg_sp++] = creg; /* push するかわりにレジスタを使う */ + creg = new_reg; + regv[creg]=1; } int emit_pop(int type) { int xreg; - if ((xreg=pop_register())==-1) { - xreg=get_register(); - if(!xreg) error(-1); - printf("\tlwz %s,(%s)\n",register_name(xreg),register_name(reg_sp)); - printf("\taddis %s,%s,%d\n", - register_name(reg_sp),register_name(reg_sp),size_of_int); + xreg=pop_register(); + if (xreg<= -REG_LVAR_OFFSET) { + xreg = get_register(); + code_rlvar(lvar(REG_LVAR_OFFSET+xreg),1); regv[xreg]=1; - } + } return xreg; } @@ -1116,7 +1096,7 @@ switch (car(e1)) { case FRINDIRECT: case DRINDIRECT: printf("\t%s %s,(%s)\n",fload(car(e1)==DRINDIRECT), - dregister_name(freg),crn); + fregister_name(freg),crn); regv[creg]=0; regv[freg]=1; return DOUBLE; case CRINDIRECT: @@ -1542,7 +1522,46 @@ data_mode(0); init=1; } - printf(".comm %s,%d\n",n->nm,size(n->ty)); + printf(".comm _%s,%d\n",n->nm,size(n->ty)); + } + } + for(n=ntable;n < &ntable[GSYMS];n++) { + if (n->dsp == EXTRN1 && n-ty>0 && + (car(n->ty)==FUNCTION||car(n->ty)==CODE)) { + extrn = n->nm; +printf(".data"); +printf(".picsymbol_stub"); +printf("L_%s$stub:\n",extrn); +printf("\t.indirect_symbol _%s\n",extrn); +printf("\tmflr r0\n"); +printf("\tbcl 20,31,L0$_%s\n",extrn); +printf("L0$_%s:\n",extrn); +printf("\tmflr r11\n"); +printf("\taddis r11,r11,ha16(L_%s$lazy_ptr-L0$_%s)\n",extrn,extrn); +printf("\tmtlr r0\n"); +printf("\tlwz r12,lo16(L_%s$lazy_ptr-L0$_%s)(r11)\n",extrn,extrn); +printf("\tmtctr r12\n"); +printf("\taddi r11,r11,lo16(L_%s$lazy_ptr-L0$_%s)\n",extrn,extrn); +printf("\tbctr\n"); +printf(".data\n"); +printf(".lazy_symbol_pointer\n"); +printf("L_%s$lazy_ptr:\n",extrn); +printf("\t.indirect_symbol _%s\n",extrn); +printf("\t.long dyld_stub_binding_helper\n"); + } + } + init = 0; + for(n=ntable;n < &ntable[GSYMS];n++) { + if (n->dsp == EXTRN1 && !(n-ty>0 && + (car(n->ty)==FUNCTION||car(n->ty)==CODE))) { + if(init==0) { +printf(".data\n"); +printf(".non_lazy_symbol_pointer\n"); + init=1; + } +printf("L_%s$non_lazy_ptr:\n",n->nm); +printf("\t.indirect_symbol _%s\n",n->nm); +printf("\t.long\t0\n"); } } } @@ -1560,7 +1579,7 @@ data_mode(0); init=1; } - printf(".lcomm %s,%d\n",n->nm,size(n->ty)); + printf(".lcomm _%s,%d\n",n->nm,size(n->ty)); } } } @@ -1610,6 +1629,17 @@ ".text", 0 }; +static int float_zero_lib_used=0; +static char *float_zero_lib[] = { +".data", +".literal8", +" .align 3", +"__float_zero:", +" .long 0", +".text", +0 +}; + char * @@ -1625,12 +1655,12 @@ } void -code_cmp_dregister(int e2) +code_cmp_fregister(int e2) { } void -code_dregister(int e2) +code_fregister(int e2) { } @@ -1638,17 +1668,17 @@ { int r; r = get_ptr_cache((char*)caddr(e2)); - printf("\t%s %s,(%s)\n",fstore(d),dregister_name(freg),register_name(r)); + printf("\t%s %s,(%s)\n",fstore(d),fregister_name(freg),register_name(r)); } void code_dassign_lvar(int e2,int d) { - printf("\t%s %s,%d(r1)\n",fstore(d),dregister_name(freg),e2); + printf("\t%s %s,%d(r1)\n",fstore(d),fregister_name(freg),e2); } void code_dassign(int e2,int d) { - printf("\t%s %s,(%s)\n",fstore(d),dregister_name(freg),register_name(e2)); + printf("\t%s %s,(%s)\n",fstore(d),fregister_name(freg),register_name(e2)); } static double d0 = 1.0; @@ -1657,14 +1687,14 @@ code_d1(double d) { int *i = (int *)&d0; int *j = (int *)&d; - return (i[1] == 0x3ff00000)?j[0]:j[1]; + return (i[1] == 0x3ff00000)?j[1]:j[0]; } int code_d2(double d) { int *i = (int *)&d0; int *j = (int *)&d; - return (i[1] == 0x3ff00000)?j[1]:j[0]; + return (i[1] == 0x3ff00000)?j[0]:j[1]; } void code_dconst(int e2) @@ -1672,15 +1702,21 @@ int lb; double d = dcadr(e2); int r; - char *rrn; - rrn = dregister_name((r=get_dregister())); - + char *rrn,*frn; + frn = fregister_name(freg); if (d==0.0) { - printf("\tfldz\n"); return; + float_zero_lib_used=1; + r = get_ptr_cache("_float_zero"); + printf("\tfls %s,(%s)\n",frn,drn); + return; } if (d==1.0) { - printf("\tfld1\n"); return; + float_one_lib_used=1; + r = get_ptr_cache("_float_one"); + printf("\tfls %s,(%s)\n",frn,drn); + return; } + rrn = register_name((r=get_register())); printf(" \t.section\t.rodata\n\t.align 8\n"); lb=fwdlabel(); printf("_%d:\n",lb); @@ -1692,19 +1728,19 @@ } printf("\taddis %s,r31,ha16(_%d-%s)\n",rrn,lb,code_base); printf("\tla %s,lo16(_%d-%s)(%s)\n",rrn,lb,code_base,rrn); - printf("\tlfd %s,(%s)\n",dregister_name(freg),rrn); + printf("\tlfd %s,(%s)\n",frn,rrn); free_register(r); } void code_dneg() { - char *frn = dregister_name(freg); + char *frn = fregister_name(freg); printf("\tfneg %s,%s\n",frn,frn); } void code_d2i() { - char *frn = dregister_name(freg); + char *frn = fregister_name(freg); char *crn = register_name(creg); disp-=size_of_double; printf("\tfctiwz %s,%s\n",frn,frn); @@ -1745,7 +1781,7 @@ void code_i2d() { i2d_lib_used = 1; - char *frn = dregister_name(freg); + char *frn = fregister_name(freg); char *crn = register_name(creg); printf("\tmr r3,%s\n",crn); printf("\tbl i2d_\n"); @@ -1793,7 +1829,7 @@ void code_d2u() { d2u_lib_used=1; - char *frn = dregister_name(freg); + char *frn = fregister_name(freg); char *crn = register_name(creg); printf("\tfmr f1,%s\n",frn); printf("\tbl d2u\n"); @@ -1832,7 +1868,7 @@ void code_u2d() { u2d_lib_used = 1; - char *frn = dregister_name(freg); + char *frn = fregister_name(freg); char *crn = register_name(creg); printf("\tmr r3,%s\n",crn); printf("\tbl u2d\n"); @@ -1845,21 +1881,21 @@ { int r; r = get_ptr_cache((char*)caddr(e2)); - printf("\t%s %s,(%s)\n",fload(d),dregister_name(freg),register_name(r)); + printf("\t%s %s,(%s)\n",fload(d),fregister_name(freg),register_name(r)); } void code_drlvar(int e2,int d) { - printf("\t%s %s,%d(r1)\n",fload(d),dregister_name(freg),e2); + printf("\t%s %s,%d(r1)\n",fload(d),fregister_name(freg),e2); } void code_cmp_drgvar(int e2) { int r; - char *frn=dregister_name(freg); - int g=get_dregister(); - char *grn=dregister_name(g); + char *frn=fregister_name(freg); + int g=get_fregister(); + char *grn=fregister_name(g); r = get_ptr_cache((char*)caddr(e2)); printf("\t%s %s,(%s)\n",fload(1),grn,register_name(r)); printf("\tfcmpu cr0,%s,%s\n",frn,grn); @@ -1869,9 +1905,9 @@ void code_cmp_drlvar(int e2) { printf("\tfcmpu %d(%%ebp)\n",e2); - char *frn=dregister_name(freg); - int g=get_dregister(); - char *grn=dregister_name(g); + char *frn=fregister_name(freg); + int g=get_fregister(); + char *grn=fregister_name(g); printf("\t%s %s,%d(r1)\n",fload(1),grn,e2); printf("\tfcmpu cr0,%s,%s\n",frn,grn); free_register(g); @@ -1880,8 +1916,8 @@ void dtosop(int op,int e1) { char *opn; - char *frn=dregister_name(freg); - char *grn=dregister_name(e1); + char *frn=fregister_name(freg); + char *grn=fregister_name(e1); switch(op) { case DADD: opn="fadd"; break; case DSUB: opn="fadd"; break; @@ -1903,9 +1939,9 @@ void code_dassop(int op,int d) { /* we have lvalue in creg, applied floating value is in freg */ - char *frn=dregister_name(freg); - int e1=get_dregister(); - char *grn=dregister_name(e1); + char *frn=fregister_name(freg); + int e1=get_fregister(); + char *grn=fregister_name(e1); char *crn=register_name(creg); printf("\t%s %s,(%s)\n",fload(d),grn,crn); @@ -1927,9 +1963,9 @@ g_expr(e2); crn=register_name(creg); - frn=dregister_name(freg); + frn=fregister_name(freg); drn=register_name(r); - grn=dregister_name(g=get_dregister()); + grn=fregister_name(g=get_fregister()); printf("\t%s %s,(%s)\n",fload(d),frn,crn); printf("\tfls %s,(%s)\n",grn,drn); @@ -1938,7 +1974,7 @@ else printf("\tfsub %s,%s,%s\n",frn,frn,grn); printf("\t%s %s,(%s)\n",fstore(d),frn,crn); - free_dregister(g); + free_fregister(g); } void @@ -1954,9 +1990,9 @@ g_expr(e2); crn=register_name(creg); - frn=dregister_name(freg); + frn=fregister_name(freg); drn=register_name(r); - grn=dregister_name(g=get_dregister()); + grn=fregister_name(g=get_fregister()); printf("\t%s %s,(%s)\n",fload(d),frn,crn); printf("\tfls %s,(%s)\n",grn,drn); @@ -1965,7 +2001,7 @@ else printf("\tfsub %s,%s,%s\n",grn,frn,grn); printf("\t%s %s,(%s)\n",fstore(d),grn,crn); - free_dregister(g); + free_fregister(g); } void @@ -1992,21 +2028,27 @@ int emit_dpop(int e1) { int xreg; - if ((xreg=pop_dregister())==-1) { - } else if (xreg<= -REG_LVAR_OFFSET) { - xreg = get_dregister(); + xreg=pop_fregister(); + if (xreg<= -REG_LVAR_OFFSET) { + xreg = get_fregister(); code_drlvar(lvar(REG_LVAR_OFFSET+xreg),1); + fregv[xreg]=1; } return xreg; } void emit_dpop_free(int e1) { - free_dregister(e1); + free_fregister(e1); } void emit_dpush() { + int new_reg; + new_reg = get_fregister(); + freg_stack[freg_sp++] = freg; /* push するかわりにレジスタを使う */ + freg = new_reg; + fregv[freg]=1; } void @@ -2035,12 +2077,12 @@ void code_closing() { - global_table(); - printf("\t.ident \"Micro-C compiled\"\n"); if (d2u_lib_used) emit_lib(d2u_lib); if (u2d_lib_used) emit_lib(u2d_lib); if (float_one_lib_used) emit_lib(float_one_lib); if (i2d_lib_used) emit_lib(i2d_lib); + global_table(); + printf("\t.ident \"Micro-C compiled\"\n"); } /* end */
--- a/mc-code.h Thu Mar 13 22:48:57 2003 +0900 +++ b/mc-code.h Fri Mar 14 01:07:37 2003 +0900 @@ -85,8 +85,8 @@ extern int dpop_register(); extern int emit_dpop(int); -extern void code_dregister(int e2); -extern void code_cmp_dregister(int); +extern void code_fregister(int e2); +extern void code_cmp_fregister(int); extern void code_cmp_drgvar(int); extern void code_cmp_drlvar(int); extern void code_dassign(int,int);
--- a/mc-codegen.c Thu Mar 13 22:48:57 2003 +0900 +++ b/mc-codegen.c Fri Mar 14 01:07:37 2003 +0900 @@ -103,7 +103,7 @@ case DREGISTER: /* this is of course redundant... */ /* or why not creg=e2? */ - code_dregister(e2); + code_fregister(e2); fregv[freg]=1; return DOUBLE; case RLVAR: @@ -413,7 +413,7 @@ jcond(l1,cond); return; case DREGISTER: - code_cmp_dregister(e2); + code_cmp_fregister(e2); jcond(l1,cond); return; case CONST: @@ -428,7 +428,7 @@ } t=g_expr(e1); if(t==FLOAT||t==DOUBLE) - code_cmp_dregister(freg); + code_cmp_fregister(freg); else code_cmp_register(creg); jcond(l1,cond);
--- a/mc-parse.c Thu Mar 13 22:48:57 2003 +0900 +++ b/mc-parse.c Fri Mar 14 01:07:37 2003 +0900 @@ -724,14 +724,14 @@ } nsc=ndsp=0; if(type>0&&(car(type)==FUNCTION || car(type)==CODE)) { - if (n->sc==EXTRN || (mode==GDECL)) { + if (/* stmode==EXTRN ||*/ (mode==GDECL)) { fcheck(n); return n; } } if (n->sc!=EMPTY && - !(n->sc==GVAR&&n->dsp==EXTRN) && - !(n->sc==FUNCTION&&n->dsp==EXTRN) && + !(n->sc==GVAR&&(n->dsp&~1)==EXTRN) && + !(n->sc==FUNCTION&&(n->dsp&~1)==EXTRN) && (mode!=ADECL || n->sc!=LVAR || n->ty!=INT) && (mode!=ADECL || n->sc!=REGISTER || n->ty!=INT) && ((mode!=GSDECL&&mode!=LSDECL) || n->sc!=FIELD || n->dsp!=disp) && @@ -2125,6 +2125,12 @@ return e; } +static void +extrn_use(NMTBL *nptr) +{ + if(nptr->dsp==EXTRN) nptr->dsp=EXTRN1; +} + static int expr14(void) { @@ -2138,6 +2144,7 @@ e1=list3(GVAR,nptr->dsp,(int)nptr->nm); type=nptr->ty; getsym(); + extrn_use(nptr); break; case LVAR: e1=list2(LVAR,nptr->dsp); @@ -2154,6 +2161,7 @@ e1=list2(FNAME,(int)nptr); type=list3(nptr->sc,nptr->ty,nptr->dsp); getsym(); + extrn_use(nptr); break; case EMPTY: if(getsym()==LPAR) {
--- a/mc.h Thu Mar 13 22:48:57 2003 +0900 +++ b/mc.h Fri Mar 14 01:07:37 2003 +0900 @@ -48,20 +48,21 @@ #define LONG (-34) #define SHORT (-35) #define EXTRN (-36) -#define VOID (-37) -#define REGISTER (-38) -#define CODE (-39) -#define ENVIRONMENT (-40) -#define DEFINED (-41) -#define KONST (-42) -#define FMACRO (-43) -#define LMACRO (-44) -#define DOTS (-45) +#define EXTRN1 (-37) +#define VOID (-38) +#define REGISTER (-39) +#define CODE (-40) +#define ENVIRONMENT (-41) +#define DEFINED (-42) +#define KONST (-43) +#define FMACRO (-44) +#define LMACRO (-45) +#define DOTS (-46) -#define FLOAT (-46) -#define DOUBLE (-47) -#define DREGISTER (-48) -#define LONGLONG (-49) +#define FLOAT (-47) +#define DOUBLE (-48) +#define DREGISTER (-49) +#define LONGLONG (-50) #define TOP 0 #define GDECL 1
--- a/test/basic.c Thu Mar 13 22:48:57 2003 +0900 +++ b/test/basic.c Fri Mar 14 01:07:37 2003 +0900 @@ -1,3 +1,18 @@ +double ggg = 0.134; +float fff = 0.0; + +extern double eggg; +double eggg = 0.134; +extern float efff; +float efff = 0.1345; + +double ggg0; +float fff0; + +#if 0 +extern double reggg; +extern float refff; +#endif int i; print(double d) @@ -5,6 +20,11 @@ float f; int *dd; +#if 0 + f = refff; + f = reggg; +#endif + f = d; dd = (int*) &d;