changeset 102:3cf2f8c120b9

*** empty log message ***
author kono
date Sat, 15 Mar 2003 21:55:14 +0900
parents 05f197a4573a
children f849af4b5ea9
files .gdbinit Changes mc-code-ia32.c mc-code-powerpc.c mc-code.h mc-codegen.c mc-codegen.h mc-parse.c
diffstat 8 files changed, 85 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/.gdbinit	Sat Mar 15 15:13:27 2003 +0900
+++ b/.gdbinit	Sat Mar 15 21:55:14 2003 +0900
@@ -13,4 +13,4 @@
 x/1i $eip
 end
 b errmsg
-r -s test/float.c
+r -s mc-parse.c
--- a/Changes	Sat Mar 15 15:13:27 2003 +0900
+++ b/Changes	Sat Mar 15 21:55:14 2003 +0900
@@ -2180,3 +2180,7 @@
 
 うーん、これで、全部書いた気がする。でも、ほとんど全部の
 関数にバグがあるだろうなぁ。一つ一つ取っていくしかないか。
+
+Sat Mar 15 16:04:09 JST 2003
+
+やぁ...  まぁ.... バグだらけだな。
--- a/mc-code-ia32.c	Sat Mar 15 15:13:27 2003 +0900
+++ b/mc-code-ia32.c	Sat Mar 15 21:55:14 2003 +0900
@@ -1717,6 +1717,7 @@
 void
 code_dassop(int op,int d) {
     /* we have lvalue in creg, applied floating value is in %st(0) */
+    emit_dpop(d);                            /* do nothing for 387 */
     printf("\t%s (%s)\n",fload(d),register_name(creg,0));
     dtosop(op,0);
     printf("\t%s (%s)\n",fstore(d),register_name(creg,0));
--- a/mc-code-powerpc.c	Sat Mar 15 15:13:27 2003 +0900
+++ b/mc-code-powerpc.c	Sat Mar 15 21:55:14 2003 +0900
@@ -134,6 +134,7 @@
     disp_offset = -12;
     size_of_int = 4;
     endian = 1;
+    init_ptr_cache();
 }
 
 #define register_name(i)  reg_name[i]
@@ -171,8 +172,13 @@
 {    /* 使われていないレジスタを調べる */
     int i;
     for(i=MAX_TMP_REG;i>MIN_TMP_REG;i--) {
-	if (! regs[i]) {    /* 使われていないなら */
-	} else if (regs[i]==INPUT_REG) {
+	if (regs[i]) continue;  /* 使われている */
+	regs[i]=USING_REG;      /* そのレジスタを使うことを宣言し */
+	return i;               /* その場所を表す番号を返す */
+    }
+    /* INPUT_REG か PTR_CACHE をつぶす */
+    for(i=MAX_TMP_REG;i>MIN_TMP_REG;i--) {
+	if (regs[i]==INPUT_REG) {
 	    code_assign_lvar(lvar(replace_arg_var(i,INT)),0);
 	} else if (regs[i]==PTRC_REG) {
 	    clear_ptr_cache_reg(i);
@@ -191,7 +197,7 @@
 	}
     }
     /* 空いている場所がないなら、エラー (いったい誰が使ってるの?) */
-    error(REG_ERR); return creg;
+    error(-1); return creg;
 }
 
 int
@@ -205,8 +211,12 @@
 {    /* 使われていないレジスタを調べる */
     int i;
     for(i=MAX_TMP_FREG;i>MIN_TMP_FREG;i--) {
-	if (! fregs[i]) {    /* 使われていないなら */
-	} else if (fregs[i]==INPUT_REG) {
+	if (fregs[i]) continue;    /* 使われている */
+	fregs[i]=USING_REG;      /* そのレジスタを使うことを宣言し */
+	return i;               /* その場所を表す番号を返す */
+    }
+    for(i=MAX_TMP_FREG;i>MIN_TMP_FREG;i--) {
+	if (fregs[i]==INPUT_REG) {
 	    code_dassign_lvar(lvar(replace_arg_var(i,DOUBLE)),0);
 	} else 
 	    continue;
@@ -241,23 +251,27 @@
 
 void 
 free_register(int i) {    /* いらなくなったレジスタを開放 */
+    if (i<0||MAX_REGISTER<i) error(-1);
     regv[i]=regs[i]=0;
 }
 
 void 
 free_fregister(int i) {    /* いらなくなったレジスタを開放 */
+    if (i<0||MAX_FREGISTER<i) error(-1);
     fregv[i]=fregs[i]=0;
 }
 
 int
 get_input_fregister_var(int i)
 {
+    if (i<0||MAX_FREGISTER<i+MIN_TMP_FREG) error(-1);
     return i+MIN_TMP_FREG;
 }
 
 int
 get_input_register_var(int i)
 {
+    if (i<0||MAX_REGISTER<i+MIN_TMP_REG) error(-1);
     return i+MIN_TMP_REG;
 }
 
@@ -297,6 +311,8 @@
 void
 code_arg_register(int args)
 {
+    int r=0;
+    int f=0;
     NMTBL *n;
     /* all registers are freed at this time */
     /* set up input regsiters */
@@ -304,12 +320,12 @@
 	/* process in reverse order */
         n = (NMTBL*)caddr(args);
         if(n->sc==REGISTER) {
-            if ((n->dsp = get_register()) <0) {
+            if ((n->dsp = get_input_register_var(r++)) <0) {
                 error(-1); return;
             }
             use_register_var(n->dsp); /* it has now value in it */
         } else if(n->sc==DREGISTER) {
-            if ((n->dsp = get_fregister()) <0) {
+            if ((n->dsp = get_input_fregister_var(f++)) <0) {
                 error(-1); return;
             }
             use_fregister_var(n->dsp); /* it has now value in it */
@@ -365,7 +381,6 @@
 emit_init(void)
 {
     free_all_register();
-    init_ptr_cache();
     max_reg_var=0; max_freg_var=0;
     reg_sp = 0;
     text_mode();
@@ -416,6 +431,7 @@
 emit_push(void)
 {
     int new_reg;
+    if (reg_sp>MAX_MAX) error(-1);
     new_reg = get_register();
     reg_stack[reg_sp++] = creg;     /* push するかわりにレジスタを使う */
     creg = new_reg;
@@ -458,7 +474,7 @@
 	if(car(ptcptr)&&caddr(ptcptr)==r) {
 	    car(ptcptr)=0;
 	    caddr(ptcptr)=0;
-	    free_register(caddr(ptcptr));
+	    free_register(r);
 	    return;
 	}
 	ptcptr=cadr(ptcptr);
@@ -501,11 +517,12 @@
 	    caddr(p)=r; regs[r]=PTRC_REG;
 	} else
 	    r=creg; /* this can't happen */
+	car(p)=g;
     }
     rrn = register_name(r);
-    printf("\taddis %s,r31,ha16(L_%s$non_lazy_ptr-_%d)\n",
+    printf("\taddis %s,r31,ha16(L_%s$non_lazy_ptr-L_%d)\n",
 	     rrn,(char *)g,code_base);
-    printf("\tlzw %s,lo16(L_%s$non_lazy_ptr-_%d)(%s)\n",
+    printf("\tlzw %s,lo16(L_%s$non_lazy_ptr-L_%d)(%s)\n",
 	     rrn,(char *)g,code_base,rrn);
     return r;
 }
@@ -514,7 +531,7 @@
 code_label(int labelno)
 {
     clear_ptr_cache();
-    printf("_%d:\n",labelno);
+    printf("L_%d:\n",labelno);
 }
 
 void
@@ -771,8 +788,8 @@
 void
 code_return() {
     char *crn = register_name(creg);
-    printf("\taddis %s,r31,ha16(_%d-_%d)\n",crn,retcont,code_base);
-    printf("\tla %s,lo16(_%d-_%d)(%s)\n",crn,retcont,code_base,crn);
+    printf("\taddis %s,r31,ha16(L_%d-L_%d)\n",crn,retcont,code_base);
+    printf("\tla %s,lo16(L_%d-L_%d)(%s)\n",crn,retcont,code_base,crn);
 }
 
 
@@ -890,15 +907,15 @@
     s=(char *)cadr(e1);
     printf(".data\t.cstring\n\t.align 2\n");
     lb=fwdlabel();
-    printf("_%d:\n",lb);
+    printf("L_%d:\n",lb);
     ascii(s);
     if (output_mode==TEXT_EMIT_MODE) {
 	printf(".text\n");
     } else {
 	text_mode();
     }
-    printf("\taddis %s,r31,ha16(_%d-_%d)\n",crn,lb,code_base);
-    printf("\tla %s,lo16(_%d-_%d)(%s)\n",crn,lb,code_base,crn);
+    printf("\taddis %s,r31,ha16(L_%d-L_%d)\n",crn,lb,code_base);
+    printf("\tla %s,lo16(L_%d-L_%d)(%s)\n",crn,lb,code_base,crn);
 }
 
 #define MAX_COPY_LEN 20
@@ -1027,7 +1044,7 @@
 	t=caddr(e3);
 	n=(NMTBL *)(e5=(cadr(e4 = car(e3))));
 	if(scalar(t)) {
-	    if (reg_arg<max_input_register_var) {
+	    if (reg_arg>max_input_register_var) {
 		arg = list2(LVAR,arg_offset_v(nargs));
 	    } else if (contains(e3,FUNCTION)) {
 		arg = list2(REGISTER,get_register_var()); regv[cadr(arg)]=1;
@@ -1039,7 +1056,7 @@
 	    nargs ++ ; reg_arg++;
 	    continue;
 	} else if (t==DOUBLE||t==FLOAT) {
-	    if (freg_arg<max_input_fregister_var) {
+	    if (freg_arg>max_input_fregister_var) {
 		arg = list2(LVAR,arg_offset_v(nargs));
 	    } else if (contains(e3,FUNCTION)) {
 		arg = list2(DREGISTER,get_fregister_var()); fregv[cadr(arg)]=1;
@@ -1096,7 +1113,7 @@
 
 void
 code_frame_pointer(int e3) {
-    printf("\tmovl %s,r1\n",register_name(e3));
+    printf("\tmr %s,r1\n",register_name(e3));
 }
 
 
@@ -1203,7 +1220,7 @@
     int edx = get_register(); if(!edx) error(-1);
     xrn = register_name(xreg = emit_pop(0));       /* pop e3 value */
     regv[xreg]=regs[xreg]=1;
-    printf("\tmovl %s,%s  # assop \n",register_name(creg),register_name(edx));
+    printf("\tmr %s,%s  # assop \n",register_name(creg),register_name(edx));
     regv[edx]=1;
     ld_indexx(byte,0,edx);
     tosop(op,xreg);
@@ -1336,7 +1353,7 @@
 rexpr(int e1, int l1, char *s,int t)
 {       
     g_expr(list3(CMP,cadr(e1),caddr(e1)));
-    printf("\tb%s cr0,_%d\n",s,l1);
+    printf("\tb%s cr0,L_%d\n",s,l1);
 }
 
 
@@ -1344,7 +1361,7 @@
 jcond(int l, char cond)
 {       
     if (chk) return;
-    printf("\tb%s cr0,_%d\n",cond?"ne":"eq",l);
+    printf("\tb%s cr0,L_%d\n",cond?"ne":"eq",l);
 }
 
 void
@@ -1352,7 +1369,7 @@
 {       
     control=0;
     if (chk) return;
-    printf("\tb\t_%d\n",l);
+    printf("\tb\tL_%d\n",l);
 }
 
 void
@@ -1372,11 +1389,11 @@
     else
 	printf("\t.align 4\n");
     if (stmode!=STATIC)
-	printf(".globl %s\n",name);
+	printf(".globl _%s\n",name);
     printf("\t.type\t%s,@function\n",name);
     printf("_%s:\n",name);
     code_disp_label=fwdlabel();
-    printf("\tla r30,-_%d(r1)\n",code_disp_label);
+    printf("\tla r30,L_%d(r1)\n",code_disp_label);
 }
 
 
@@ -1390,9 +1407,9 @@
 code_leave(char *name)
 {
     disp&= -size_of_int;
-    printf("\t.set _%d,%d\n",code_disp_label,disp+code_disp_offset);
-    printf("_%d:\n",labelno);
-    printf("\t.size\t_%s,_%d-_%d\n",name,labelno,code_begin);
+    printf("\t.set L_%d,%d\n",code_disp_label,disp+code_disp_offset);
+    printf("L_%d:\n",labelno);
+    printf("\t.size\t_%s,L_%d-L_%d\n",name,labelno,code_begin);
     local_table();
     labelno++;
     free_all_register();
@@ -1413,7 +1430,7 @@
     printf("_%s:\n",name);
     code_setup=fwdlabel();
     printf("\tmflr r0\n");
-    printf("\tbl _%d\n",code_setup);
+    printf("\tbl L_%d\n",code_setup);
     code_base=fwdlabel();
     printf("\tmr r30,r1\n");
     printf("\tmflr r31\n");
@@ -1474,8 +1491,8 @@
     else
 	printf("\tblr\n");
 
-    printf("_%d:\n",labelno);
-    printf("\t.size\t%s,_%d-%s\n",name,labelno,name);
+    printf("L_%d:\n",labelno);
+    printf("\t.size\t%s,L_%d-%s\n",name,labelno,name);
     local_table();
     labelno++;
     free_all_register();
@@ -1490,8 +1507,10 @@
 void
 gen_gdecl(char *n, int gpc)
 {
+    /*
     if (stmode!=STATIC)
 	printf(".globl _%s\n",n); 
+     */
 }
 
 void 
@@ -1518,10 +1537,10 @@
     if (chk) return;
     if (n->dsp != -1) {
 	n->dsp = -1;   /* initiallized flag */
-	printf(".globl\t%s\n",name);
+	printf(".globl\t_%s\n",name);
 	data_mode(name);
 	align(t);
-	printf("%s:\n",name); 
+	printf("_%s:\n",name); 
     } else {
 	data_mode(0);
     }
@@ -1554,9 +1573,9 @@
 	} else if(car(e)==STRING) {       
 	    if (car(n->ty)!=ARRAY || cadr(n->ty)!=CHAR) {
 		l = fwdlabel();
-		printf("\t.long _%d\n",l);
+		printf("\t.long L_%d\n",l);
 		printf(".section\t.rodata\n");
-		printf("_%d:\n",l);
+		printf("L_%d:\n",l);
 		output_mode = RODATA_EMIT_MODE;
 	    }
 	    ascii((char *)cadr(e));
@@ -1572,8 +1591,8 @@
     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);
+	printf("L_%d:\n",lb);
+	printf("\t.size\t%s,L_%d-%s\n",n->nm,lb,n->nm);
     }
 }
 
@@ -1801,15 +1820,15 @@
     rrn = register_name((r=get_register()));
     printf(" \t.section\t.rodata\n\t.align 8\n");
     lb=fwdlabel();
-    printf("_%d:\n",lb);
+    printf("L_%d:\n",lb);
     printf("\t.long\t0x%x,0x%x\n",code_d2(d),code_d1(d));
     if (output_mode==TEXT_EMIT_MODE) {
 	printf(".text\n");
     } else {
 	text_mode();
     }
-    printf("\taddis %s,r31,ha16(_%d-_%d)\n",rrn,lb,code_base);
-    printf("\tla %s,lo16(_%d-_%d)(%s)\n",rrn,lb,code_base,rrn);
+    printf("\taddis %s,r31,ha16(L_%d-L_%d)\n",rrn,lb,code_base);
+    printf("\tla %s,lo16(L_%d-L_%d)(%s)\n",rrn,lb,code_base,rrn);
     printf("\tlfd %s,(%s)\n",frn,rrn);
     free_register(r);
 }
@@ -2022,13 +2041,13 @@
 code_dassop(int op,int d) {
     /* we have lvalue in creg, applied floating value is in freg */
     char *frn=fregister_name(freg);
-    int  e1=get_fregister();
-    char *grn=fregister_name(e1);
+    int  xreg=emit_dpop(0);
     char *crn=register_name(creg);
 
-    printf("\t%s %s,(%s)\n",fload(d),grn,crn);
-    dtosop(op,e1);
+    printf("\t%s %s,(%s)\n",fload(d),frn,crn);
+    dtosop(op,xreg);
     printf("\t%s %s,(%s)\n",fstore(d),frn,crn);
+    emit_dpop_free(xreg);
 }
 
 
@@ -2093,16 +2112,16 @@
     switch(op) {
 	case DOP+GE:
 	    printf("\tcror 2,29,30\n");
-	    printf("\tbeq\tcr0,_%d\n",l1);
+	    printf("\tbeq\tcr0,L_%d\n",l1);
 	    break;
 	case DOP+GT:
-	    printf("\tbgt\tcr0,_%d\n",l1);
+	    printf("\tbgt\tcr0,L_%d\n",l1);
 	    break;
 	case DOP+EQ:
-	    printf("\tbeq\tcr0,_%d\n",l1);
+	    printf("\tbeq\tcr0,L_%d\n",l1);
 	    break;
 	case DOP+NEQ:
-	    printf("\tbne\tcr0,_%d\n",l1);
+	    printf("\tbne\tcr0,L_%d\n",l1);
 	    break;
     }
 }
@@ -2127,6 +2146,7 @@
 void emit_dpush()
 { 
     int new_reg;
+    if (freg_sp>MAX_MAX) error(-1);
     new_reg = get_fregister();
     freg_stack[freg_sp++] = freg;     /* push するかわりにレジスタを使う */
     freg = new_reg;
--- a/mc-code.h	Sat Mar 15 15:13:27 2003 +0900
+++ b/mc-code.h	Sat Mar 15 21:55:14 2003 +0900
@@ -15,6 +15,7 @@
 
 #define REG_LVAR_OFFSET 2
 
+extern void code_init();
 extern char *register_name(int i,int byte);
 extern void gexpr_code_init(void);
 extern int register_var(int r);
--- a/mc-codegen.c	Sat Mar 15 15:13:27 2003 +0900
+++ b/mc-codegen.c	Sat Mar 15 21:55:14 2003 +0900
@@ -36,6 +36,12 @@
 void assop(int e1);
 int g_expr0(int e1);
 
+void
+codegen_init()
+{
+    code_init();
+}
+
 int
 gexpr(int e1,int use0)
 {
--- a/mc-codegen.h	Sat Mar 15 15:13:27 2003 +0900
+++ b/mc-codegen.h	Sat Mar 15 21:55:14 2003 +0900
@@ -22,6 +22,8 @@
 
 /* function provided by mc-codegen.c */
 
+extern void codegen_init();
+
 extern void def_label(int cslabel, int dlabel);
 extern int backdef(void);
 extern int free_register_count(void);
--- a/mc-parse.c	Sat Mar 15 15:13:27 2003 +0900
+++ b/mc-parse.c	Sat Mar 15 21:55:14 2003 +0900
@@ -280,7 +280,7 @@
     labelno=2;
     lfree=HEAPSIZE;
     filep=filestack;
-    code_init();
+    codegen_init();
     newfile();
 
     macro_define("__micro_c__ 1\n");