changeset 132:07eb1249f07a

*** empty log message ***
author kono
date Fri, 11 Apr 2003 14:04:40 +0900
parents 09379ec2a74b
children 7d436c08e949
files Changes mc-code-mips.c mc-code-powerpc.c mc-codegen.c mc-parse.c test/int.c
diffstat 6 files changed, 145 insertions(+), 240 deletions(-) [+]
line wrap: on
line diff
--- a/Changes	Mon Apr 07 22:40:24 2003 +0900
+++ b/Changes	Fri Apr 11 14:04:40 2003 +0900
@@ -2771,3 +2771,28 @@
 .include を使うと良いらしい。(そんなんでいいのか?!)
 
 csvalue() で取って来たregiserを誰がfreeするの?
+
+Tue Apr  8 14:52:06 JST 2003
+
+MIPSには bgez とか bgtz とかいう命令があるのね。なんかに
+使えるの? 使えるけどさ。今のコンパイラで使う方法は?
+
+gccが浮動小数点レジスタを避けるようにコンパイルしている
+のは何故だろう? 遅いから? jal dcmp とかは、やっぱり変。
+
+double と float の差が良くわからない。
+
+Wed Apr  9 16:20:00 JST 2003
+
+LOR LAND の定数の扱いを追加
+
+配列の処理があるから、変数*定数ぐらいのh最適化は
+入れた方が良いかも。その方がレジスタも少なく使うし。
+(また?)
+
+授業で使うように、また、最適化を落したものが必要かな。
+
+MIPSの浮動小数点ってさ、single precision しか機械語命令が
+ない? dpsub とかを呼び出すのっておかしくない? (まぁ、
+いいんだけどさ)
+
--- a/mc-code-mips.c	Mon Apr 07 22:40:24 2003 +0900
+++ b/mc-code-mips.c	Fri Apr 11 14:04:40 2003 +0900
@@ -827,29 +827,37 @@
     fwddef(e3);
 }
 
+/* slt $2,$2,$3; beq $2,$0,L1 */
+/* slt $2,$2,$3; xori $2,$2,,0x1,beq $2,$0,L1 */
+/* beq $2,$3,L1 */
+
 char *
 code_gt(int cond) {
-    return (cond?"gt":"le");
+    return (cond?"\tslt  %s,%s,%s\n\tbeq %s,$0,%s\n":
+                 "\tslt  %s,%s,%s\n\tbne %s,$0,%s\n");
 }
 
 char *
 code_ugt(int cond) {
-    return (cond?"gt":"le");
+    return (cond?"\tsltu %s,%s,%s\n\tbeq %s,$0,%s\n":
+                 "\tsltu %s,%s,%s\n\tbne %s,$0,%s\n");
 }
 
 char *
 code_ge(int cond) {
-    return (cond?"ge":"lt");
+    return (cond?"\tslt  %s,%s,%s\n\txori %s,%s,0x1\n\tbeq %s,$0,%s\n":
+                 "\tslt  %s,%s,%s\n\txori %s,%s,0x1\n\tbne %s,$0,%s\n");
 }
 
 char *
 code_uge(int cond) {
-    return (cond?"ge":"lt");
+    return (cond?"\tsltu %s,%s,%s\n\txori %s,%s,0x1\n\tbeq %s,$0,%s\n":
+                 "\tsltu %s,%s,%s\n\txori %s,%s,0x1\n\tbne %s,$0,%s\n");
 }
 
 char *
 code_eq(int cond) {
-    return (cond?"eq":"ne");
+    return (cond?"\tbeq %s,%s,%s\n":"\tbne %s,%s,%s\n");
 }
 
 void
@@ -1553,14 +1561,36 @@
         }
     }
     asi = fopen(p,"w");
+    printf("\.include \"%s\"\n",p);
     if (!asi) error(-1);
 }
 
 void
 rexpr(int e1, int l1, char *s,int t)
 {       
-    g_expr(list3(CMP,cadr(e1),caddr(e1)));
-    printf("\tb%s cr0,L_%d\n",s,l1);
+    char *crn,*drn;
+    if (dreg>=0) { free_register(dreg); dreg = -1; }
+
+    g_expr(cadr(e1));
+    emit_push();
+    g_expr(caddr(e1));
+    dreg = pop_register();
+    crn = register_name(creg);
+    drn = register_name(dreg);
+
+    if (s[1] == 's') {
+	if (s[16]=='x') {
+	    /* slt $2,$2,$3; xroi $2,$2,0x1; beq $2,$0,L1 */
+	    printf(s,crn,drn,drn,crn,crn,crn,l1);
+	} else {
+	    /* slt $2,$2,$3; beq $2,$0,L1 */
+	    printf(s,crn,drn,drn,crn,l1);
+	}
+    } else {
+	/* beq $2,$3,L1 */
+	printf(s,crn,drn,l1);
+    }
+    free_register(dreg) ; dreg = -1;
 }
 
 void
@@ -1572,7 +1602,7 @@
 	cond?"ne":"eq",l);
     if (regv[dreg]==1) {
 	free_register(dreg); dreg = -1;
-    }
+    } 
     regv[creg]=0;
 }
 
@@ -1597,18 +1627,18 @@
     if (output_mode!=TEXT_EMIT_MODE) 
 	text_mode();
     else
-	printf("\t.align 2\n");
+	printf("\t.align 3\n");
     if (stmode!=STATIC)
 	printf(".globl %s\n",name);
-#ifdef DOT_SIZE
-    printf("\t.type\t%s,@function\n",name);
-#endif
+    printf(".ent %s\n",name);
     printf("_%s:\n",name);
     code_disp_label=fwdlabel();
-    printf("\tla r1,lo16(L_%d)(r30)\n",code_disp_label);
-    printf("\tbcl 20,31,L_%d\n",code_base = fwdlabel());
-    fwddef(code_base);
-    printf("\tmflr r31\n");
+
+    printf("\t.set noreorder\n");
+    printf("\t.cpload $25\n");
+    printf("\t.set reorder\n");
+    printf("\tsubu $sp,$sp,L_%d\n",code_disp_label);
+    printf("\t.cprestore 16\n");
     max_func_args = 0;
 }
 
@@ -1627,7 +1657,7 @@
     disp&= -size_of_int;
     r1_offsetv = -disp+max_func_args*size_of_int+code_disp_offset;
 
-    printf(".set L_%d,%d\n",code_disp_label,-r1_offsetv);
+    fprintf(asi,"L_%d=%d\n",code_disp_label,-r1_offsetv);
     local_table();
     free_all_register();
 }
@@ -1641,20 +1671,25 @@
 	printf("\t.align 2\n");
     if (stmode!=STATIC)
 	printf(".globl %s\n",name);
-/*
-    printf("\t.type\t%s,@function\n",name);
- */
+    printf(".ent %s\n",name);
     printf("_%s:\n",name);
-    code_setup=fwdlabel();
-    printf("\tmflr r0\n");
-    printf("\tbl L_%d\n",code_setup);
-    code_base=fwdlabel();
-    fwddef(code_base);
+
     r1_offset_label = fwdlabel();
     lvar_offset_label = fwdlabel();
-    printf("\tstwu r1,lo16(-L_%d)(r1)\n",r1_offset_label);
-    printf("\tmr r30,r1\n");
-    printf("\tmflr r31\n");
+    mask_label = fwdlabel();
+    mask1_label = fwdlabel();
+    fmask_label = fwdlabel();
+    fmask1_label = fwdlabel();
+
+    printf("\t.frame $fp,L_%d,$31\n",r1_offset_label);
+    printf("\t.mask L_%d,L_%d\n",mask_label,mask1_label);
+    printf("\t.fmask L_%d,L_%d\n",fmask_label,fmask1_label);
+
+    printf("\t.set noreorder\n");
+    printf("\t.cpload $25\n");
+    printf("\t.set reorder\n");
+    printf("\tsubu $sp,$sp,L_%d\n",code_disp_label);
+    printf("\t.cprestore 16\n");
     max_func_args = 0;
 }
 
@@ -1729,17 +1764,9 @@
     }
 
     disp &= -size_of_int;
-    fwddef(code_setup);
-    printf("\tstmw r%d,%d(r1)\n",
-		    REG_VAR_BASE-max_reg_var,reg_save);
-    printf("\tstw r0,8(r1)\n");
-    if (max_freg_var>=0)
-	printf("\tb saveFP+%d ; save f%d-f31\n",
-			freg_save,
-			FREG_VAR_BASE-max_freg_var);
-    else {
-	printf("\tblr\n");
-    }
+
+    printf("\tj $31\n");
+    printf("\t.end %s\n",fnptr->nm);
 
     code_offset_set();
     local_table();
@@ -1930,65 +1957,35 @@
 	printf(".data\n");
 	output_mode = DATA_EMIT_MODE;
     }
-/*
     if (name)
 	printf("\t.type\t%s,@object\n",name);
- */
 }
 
 /* floating point */
 
-static int float_one_lib_used=0;
-static char *float_one_lib[] = {
-".data",
-/* ".literal8", */
-"        .align 3",
-"__float_one:",
-"        .long   1065353216",
-".text",
-/* ".set L__float_one$non_lazy_ptr,__float_one", */
-0
-};
-static int float_zero_lib_used=0;
-static char *float_zero_lib[] = {
-".data",
-/* ".literal8", */
-"        .align 3",
-"__float_zero:",
-"        .long   0",
-".text",
-/* ".set L__float_zero$non_lazy_ptr,__float_zero", */
-0
-};
-
-
-
 char *
 fstore(int d)
 {
-    return (d?"stfd":"stfs");
+    return (d?"s.d":"s.s");
 }
 
 char *
 fload(int d)
 {
-    return d?"lfd":"lfs";
+    return d?"l.d":"l.s";
 }
 
 void
 code_cmp_fregister(int e2)
 {
+    /* not tested.. */
     char *frn,*rrn,*grn;
     int greg,r;
     grn =  register_name(greg = get_fregister());
-    frn = register_name(freg);
-    float_zero_lib_used=1;
-    r = get_ptr_cache(&float_zero);
-    rrn = register_name(r);
-    printf("\tlfs %s,0(%s)\n",grn,rrn);
-    printf("\tfcmpu cr0,%s,%s\n",grn,frn);
+    frn = register_name(e2);
+    printf("\tli.s %s,%g\n",grn,0.0);
+    printf("\tc.eq.s %s,%s\n",grn,frn);
     free_fregister(greg);
-    fregv[freg]=0;
     return;
 }
 
@@ -1996,15 +1993,15 @@
 code_fregister(int e2,int freg)
 {
     if (freg!=e2)
-	printf("\tfmr %s,%s\n",fregister_name(freg),fregister_name(e2));
+	printf("\tmov.s %s,%s\n",fregister_name(freg),fregister_name(e2));
     fregv[freg]=1;
 }
 
 void code_dassign_gvar(int e2,int freg,int d)
 { 
     int r;
-    r = get_ptr_cache((NMTBL*)cadr(e2));
-    printf("\t%s %s,0(%s)\n",fstore(d),fregister_name(freg),register_name(r));
+    NMTBL *n = (NMTBL*)cadr(e2);
+    printf("\t%s %s,0(%s)\n",fstore(d),fregister_name(freg),n->nm);
     fregv[freg]=1;
 }
 
@@ -2024,59 +2021,14 @@
 void
 code_dassign_fregister(int e2,int d,int freg) {
     if (e2!=freg)
-	printf("\tfmr %s,%s\n",fregister_name(e2),fregister_name(freg));
-}
-
-static double d0 = 1.0;
-
-int
-code_d1(double d)
-{
-    int *i = (int *)&d0; int *j = (int *)&d;
-    return (i[1] == 0x3ff00000)?j[0]:j[1];
-}
-
-int
-code_d2(double d)
-{
-    int *i = (int *)&d0; int *j = (int *)&d;
-    return (i[1] == 0x3ff00000)?j[1]:j[0];
+	printf("\tmov.s %s,%s\n",fregister_name(e2),fregister_name(freg));
 }
 
 void code_dconst(int e2,int freg)
 { 
-    int lb;
     double d = dcadr(e2);
-    int r;
-    char *rrn,*frn;
-    frn = fregister_name(freg);
-    if (d==0.0) {
-	float_zero_lib_used=1;
-	r = get_ptr_cache(&float_zero);
-	rrn = register_name(r);
-	printf("\tlfs %s,0(%s)\n",frn,rrn);
-	return;
-    }
-    if (d==1.0) {
-	float_one_lib_used=1;
-	r = get_ptr_cache(&float_one);
-	rrn = register_name(r);
-	printf("\tlfs %s,0(%s)\n",frn,rrn);
-	return;
-    }
-    rrn = register_name((r=get_register()));
-    printf(" \t.data\n\t.align 3\n");
-    lb=fwdlabel();
-    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(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,0(%s)\n",frn,rrn);
+    char *frn = fregister_name(freg);
+    printf("\tli.s %s,%g\n",frn,d);
     free_register(r);
     fregv[freg]=1;
 }
@@ -2093,88 +2045,25 @@
     char *crn = register_name(creg);
     int e2 = new_lvar(size_of_double);
     free_lvar(e2);
-    printf("\tfctiwz  %s,%s\n",frn,frn);
+    printf("\tjal dptoli\n",frn,frn);
     printf("\tstfd  %s,",frn); lvar(e2);
     printf("\tlwz %s,",crn); lvar(e2+size_of_double-size_of_int);
     fregs[freg]=0;
     regs[creg]=1;
 }
 
-static int i2d_lib_used=0;
-static char *i2d_lib[] = {
-".data",
-/* ".literal8", */
-"        .align 3",
-"__i2dLC0:",
-"        .long   1127219200",
-"        .long   -2147483648",
-".text",
-"        .align 2",
-"i2d_:",
-"        mflr r0",
-"        bcl 20,31,__i2dL1$pb",
-"__i2dL1$pb:",
-"        mflr r10",
-"        mtlr r0",
-"        xoris r3,r3,0x8000",
-"        stw r3,-28(r30)",
-"        lis r0,0x4330",
-"        stw r0,-32(r30)",
-"        lfd f0,-32(r30)",
-"        addis r9,r10,ha16(__i2dLC0-__i2dL1$pb)",
-"        lfd f1,lo16(__i2dLC0-__i2dL1$pb)(r9)",
-"        fsub f1,f0,f1",
-"        blr",
-0
-};
-
 void code_i2d(int creg,int freg)
 { 
     i2d_lib_used = 1;
     clear_ptr_cache();
     code_save_stacks();
     set_creg(RET_REGISTER,1);
-    printf("\tbl i2d_\n");
+    printf("\tjal litodp\n",frn,frn);
     set_freg(RET_FREGISTER,0);
     fregs[freg]=1;
     regs[creg]=0;
 }
 
-static int d2u_lib_used=0;
-static char *d2u_lib[] = {
-/* ".literal8", */
-"        .align 3",
-"__d2uLC0:",
-"        .long   1105199104",
-"        .long   0",
-".text",
-"        .align 2",
-"d2u_:",
-"        mflr r0",
-"        bcl 20,31,__d2uL1$pb",
-"__d2uL1$pb:",
-"        mflr r10",
-"        mtlr r0",
-"        addis r9,r10,ha16(__d2uLC0-__d2uL1$pb)",
-"        lfd f0,lo16(__d2uLC0-__d2uL1$pb)(r9)",
-"        fcmpu cr0,f1,f0",
-"        cror 2,1,2",
-"        beq- cr0,__d2uL2",
-"        fctiwz f0,f1",
-"        stfd f0,-32(r30)",
-"        lwz r3,-28(r30)",
-"        blr",
-"__d2uL2:",
-"        addis r9,r10,ha16(__d2uLC0-__d2uL1$pb)",
-"        lfd f0,lo16(__d2uLC0-__d2uL1$pb)(r9)",
-"        fsub f0,f1,f0",
-"        fctiwz f0,f0",
-"        stfd f0,-24(r30)",
-"        lwz r3,-20(r30)",
-"        xoris r3,r3,0x8000",
-"        blr",
-0
-};
 
 void code_d2u(int freg,int creg)
 { 
@@ -2182,39 +2071,12 @@
     clear_ptr_cache();
     d2u_lib_used=1;
     set_freg(RET_FREGISTER,1);
-    printf("\tbl d2u_\n");
+    printf("\tjal dptoul\n");
     set_creg(RET_REGISTER,0);
     fregs[freg]=1;
     regs[creg]=0;
 }
 
-static int u2d_lib_used=0;
-static char *u2d_lib[] = {
-".data",
-/* ".literal8", */
-"        .align 3",
-"__u2dLC1:",
-"        .long   1127219200",
-"        .long   0",
-".text",
-"        .align 2",
-"u2d_:",
-"        mflr r0",
-"        bcl 20,31,__u2dL2$pb",
-"__u2dL2$pb:",
-"        mflr r10",
-"        mtlr r0",
-"        stw r3,-28(r30)",
-"        lis r0,0x4330",
-"        stw r0,-32(r30)",
-"        lfd f0,-32(r30)",
-"        addis r9,r10,ha16(__u2dLC1-__u2dL2$pb)",
-"        lfd f1,lo16(__u2dLC1-__u2dL2$pb)(r9)",
-"        fsub f1,f0,f1",
-"        blr",
-0
-};
-
 void code_u2d(int creg,int freg)
 { 
     char *frn;
@@ -2226,7 +2088,7 @@
     crn = register_name(creg);
 
     printf("\tmr r3,%s\n",crn);
-    printf("\tbl u2d_\n");
+    printf("\tjal litodp\n");
     printf("\tfmr %s,f1\n",frn);
     fregs[freg]=1;
     regs[creg]=0;
@@ -2447,13 +2309,9 @@
 void
 code_closing()
 {
-    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 (float_zero_lib_used) emit_lib(float_zero_lib);
-    if (i2d_lib_used) emit_lib(i2d_lib);
     global_table();
     /* printf("\t.ident \"Micro-C compiled\"\n"); */
+    fclose(ais);
 }
 
 /* end */
--- a/mc-code-powerpc.c	Mon Apr 07 22:40:24 2003 +0900
+++ b/mc-code-powerpc.c	Fri Apr 11 14:04:40 2003 +0900
@@ -2232,14 +2232,13 @@
     char *frn,*rrn,*grn;
     int greg,r;
     grn =  register_name(greg = get_fregister());
-    frn = register_name(freg);
+    frn = register_name(e2);
     float_zero_lib_used=1;
     r = get_ptr_cache(&float_zero);
     rrn = register_name(r);
     printf("\tlfs %s,0(%s)\n",grn,rrn);
     printf("\tfcmpu cr0,%s,%s\n",grn,frn);
     free_fregister(greg);
-    fregv[freg]=0;
     return;
 }
 
--- a/mc-codegen.c	Mon Apr 07 22:40:24 2003 +0900
+++ b/mc-codegen.c	Fri Apr 11 14:04:40 2003 +0900
@@ -324,6 +324,7 @@
 b_expr(int e1, char cond, int l1,int err)
 {
     int e2,l2,t;
+    if (!control) return;
     l2 = 0;
     e2=cadr(e1);
     switch(car(e1)) {
@@ -423,10 +424,10 @@
 	jcond(l1,cond);
 	return;
     case CONST:
-	if((cond&&e2)||(!cond&&!e2)) jmp(l1);
+	if(control&&((cond&&e2)||(!cond&&!e2))) jmp(l1);
 	return;
     case DCONST:
-	if((dcadr(e2)!=0.0)^cond) jmp(l1);
+	if(control&&((dcadr(e2)!=0.0)^cond)) jmp(l1);
 	return;
     default:
 	if(err) {
--- a/mc-parse.c	Mon Apr 07 22:40:24 2003 +0900
+++ b/mc-parse.c	Fri Apr 11 14:04:40 2003 +0900
@@ -1904,15 +1904,18 @@
 static int
 expr3(void)
 {
-    int e;
+    int e,e1;
 
     e=expr4();
     while(sym==LOR) {
-	    conv->op_(sym);
-	    e=rvalue(e);
-	    getsym();
-	    e=list3(LOR,e,rvalue(expr4()));
-	    type= INT;
+	conv->op_(sym);
+	e=rvalue(e);
+	getsym();
+	e1=rvalue(expr4());
+	if(car(e)==CONST)       e =  cadr(e )?e:e1;
+	else if(car(e1)==CONST) e =  cadr(e1)?e1:e;
+	else e=list3(LOR,e,e1);
+	type = INT;
     }
     return(e);
 }
@@ -1920,15 +1923,18 @@
 static int
 expr4(void)
 {
-    int e;
+    int e,e1;
 
     e=expr5();
     while(sym==LAND) {
 	conv->op_(sym);
 	e=rvalue(e);
 	getsym();
-	e=list3(LAND,e,rvalue(expr5()));
-	type= INT;
+	e1=rvalue(expr5());
+	if(car(e)==CONST)       e =  cadr(e )?e1:e;
+	else if(car(e1)==CONST) e =  cadr(e1)?e:e1;
+	else e=list3(LAND,e,e1);
+	type = INT;
     }
     return(e);
 }
@@ -2584,10 +2590,12 @@
     if(op==GT||op==GE||op==LT||op==LE||op==EQ||op==NEQ) {
 	type=INT;
 	return(list3(op+DOP,e1,e2));
-    } else if(ADD||SUB||MUL||DIV)
+    } else if(op==ADD||op==SUB||op==MUL||op==DIV)
 	return(list3(op+DOP,e1,e2));
-    else
+    else {
 	error(-1);
+	return e1;
+    }
 }
 
 static int
--- a/test/int.c	Mon Apr 07 22:40:24 2003 +0900
+++ b/test/int.c	Fri Apr 11 14:04:40 2003 +0900
@@ -9,6 +9,7 @@
 unsigned f = 3;
 int d = -3;
 unsigned f1 = 3;
+unsigned f2 = 4;
 int d1 = -3;
 int d2 = -2;
 unsigned h = 65536*100;
@@ -66,6 +67,19 @@
    if(d<d2) printf("-8 ");
    if(d<=d1) printf("9 ");
    if(d<=d2) printf("-9 ");
+
+   printf("unsigned ");
+   if(f>f1) printf("5 ");
+   if(f>f2) printf("-5 ");
+   if(f>=f1) printf("6 ");
+   if(f>=f2) printf("-6 ");
+   if(f!=f1) printf("7 ");
+   if(f!=f2) printf("-7 ");
+   if(f<f1) printf("8 ");
+   if(f<f2) printf("-8 ");
+   if(f<=f1) printf("9 ");
+   if(f<=f2) printf("-9 ");
+
    d = 123; f=-234;
    i = d;
    d = i;