changeset 739:0c7a6ae119ba

i64 continue... compiler compiled.
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 08 Nov 2010 22:22:46 +0900
parents 1ba0d5c4d277
children 8cd40338aee6
files mc-code-i64.c mc-code-powerpc.c
diffstat 2 files changed, 224 insertions(+), 152 deletions(-) [+]
line wrap: on
line diff
--- a/mc-code-i64.c	Mon Nov 08 20:16:43 2010 +0900
+++ b/mc-code-i64.c	Mon Nov 08 22:22:46 2010 +0900
@@ -264,7 +264,7 @@
 #define REAL_MAX_REGISTER (1+16)    /* intel386のレジスタが8つということ*/
 int MAX_REGISTER_VAR=6;    
 #define REAL_MAX_DREGISTER (1+16)    /* intel386のレジスタが8つということ*/
-static int MAX_DREGISTER=16;
+
 int MAX_DREGISTER_VAR=8;
 
 #define MIN_TMP_FREG 6
@@ -309,9 +309,30 @@
 
 static int code_disp_label;
 
-#ifdef __APPLE__
-static int goffset_label;
-#endif
+static int float_one_lib_used=0;
+static int float_one_f_lib_used=0;
+
+static
+NMTBL float_one = {"_float_one",0,STATIC,FLOAT,0};
+static 
+NMTBL float_one_f = {"_float_one_f",0,STATIC,FLOAT,0};
+
+static char *float_one_lib[] = {
+  "        .literal8",
+"        .align 3",
+"	_float_one",
+"        .long   0",
+"        .long   1072693248",
+        0
+};
+
+static char *float_one_f_lib[] = {
+  "        .literal4",
+  "        .align 2",
+  "	_float_one_f:",
+  "        .long   1065353216",
+        0
+};
 
 
 /*
@@ -339,7 +360,7 @@
 //    2 (REG_VAR) register variable
 //    3 pointer cache (not used in ia32)
 
-#define FREG_START   18
+#define FREG_OFFSET   18
 
 #define REG_ESP   1
 #define REG_EBP   2
@@ -478,13 +499,13 @@
 static char * fload(int d);
 static int code_d1(double d);
 static int code_d2(double d);
-static void code_save_fstacks();
+
 #endif
 static void jcond(int l, char cond);
-#if LONGLONG_CODE
-static int code_l1(long long d);
-static int code_l2(long long d);
-#endif
+
+
+
+
 
 #define round16(i)   align(i,16)
 #define round4(i)    align(i,4)
@@ -673,7 +694,7 @@
     return i;
 }
 
-#define fregister_name(reg)  reg_name_d[reg-FREG_START]
+#define fregister_name(reg)  reg_name_d[reg-FREG_OFFSET]
 
 static void
 set_freg(int reg,int mode)
@@ -720,8 +741,39 @@
 static void
 set_lreg(int reg,int mode)
 {
-    use_longlong(reg);
-}
+  set_ireg(reg,mode);
+}
+
+#if FLOAT_CODE
+#define  use_float(d,reg) if (reg==USE_CREG) reg=d?use_double0():use_float0()
+static
+int use_float0() { 
+  int i = creg;
+  if (!is_float_reg(i)) {
+    if (lreg) { if (regs[lreg]!=REG_VAR) free_register(lreg); lreg = 0; }
+    if (!freg) freg = get_dregister(0);
+    else if (freg!=i) if (regs[i]!=REG_VAR) free_register(i);
+    i = freg;
+  }
+  if (!regs[i]) regs[i]=USING_REG;
+  creg = i;
+  return i;
+}
+static
+int use_double0() { 
+  int i = creg;
+  if (!is_float_reg(i)) {
+    if (lreg) { if (regs[lreg]!=REG_VAR) free_register(lreg); lreg = 0; }
+    if (!freg) freg = get_dregister(1);
+    else if (freg!=i) if (regs[i]!=REG_VAR) free_register(i);
+    i = freg;
+  }
+  if (!regs[i]) regs[i]=USING_REG;
+  creg = i;
+  return i;
+}
+#endif
+
 
 extern void
 code_init(void)
@@ -758,7 +810,7 @@
 char *
 register_name(int i,int byte)
 {
-    if (i<=0 || i>=FREG_START) {
+    if (i<=0 || i>=FREG_OFFSET) {
 	error(REG_ERR);
 	return "%rax";
     }
@@ -851,8 +903,6 @@
 
 void 
 free_register(int i) {    /* いらなくなったレジスタを開放 */
-    if (regs[i]==REG_VAR) { reg_var--;
-    }
     regs[i]=0;
 }
 
@@ -908,10 +958,10 @@
 {
   if (is_code) {
     if(!(i<MAX_INPUT_DREGISTER_VAR)) return 0;
-    i = FREG_START+i;
+    i = FREG_OFFSET+i;
   } else {
     if(!(i<MAX_INPUT_DREGISTER_VAR)) return 0;
-    i = FREG_START+i;
+    i = FREG_OFFSET+i;
   }
   return list3n(FREGISTER,i,nptr);
 }
@@ -927,7 +977,7 @@
 get_dregister(int d)
 {    /* 使われていないレジスタを調べる */
     int i,reg;
-    for(i=MAX_TMP_FREG+FREG_START;i>MIN_TMP_FREG+FREG_START;i--) {
+    for(i=MAX_TMP_FREG+FREG_OFFSET;i>MIN_TMP_FREG+FREG_OFFSET;i--) {
         if (regs[i]) continue;    /* 使われている */
         regs[i]=USING_REG;      /* そのレジスタを使うことを宣言し */
         return i;   /* その場所を表す番号を返す */
@@ -942,7 +992,7 @@
         }
     }
     for(i=0;i<FREG_VAR_BASE-REG_VAR_MIN;i++) {
-        reg =FREG_VAR_BASE-i+FREG_START;
+        reg =FREG_VAR_BASE-i+FREG_OFFSET;
         if (! regs[reg]) {       /* 使われていないなら */
             regs[reg]=USING_REG; /* そのレジスタを使うことを宣言し */
             if (i>max_freg_var) max_freg_var=i;
@@ -960,7 +1010,11 @@
 }
 #endif
 
-#define get_lregister_var(n) get_register_var(n)
+int
+get_lregister_var(NMTBL *n) {
+    return get_register_var(n);
+}
+
 #define get_lregister() get_register()
 
 int
@@ -990,11 +1044,10 @@
 free_all_register(void)
 {
     int i;
-    for(i=1;i<REAL_MAX_REGISTER;i++) {
+    for(i=1;i<REAL_MAX_REGISTER+REAL_MAX_DREGISTER;i++) {
 	regs[i]=0;
     }
     lreg = creg = ireg = 0;
-    reg_var = 0;
     return;
 }
 
@@ -1062,7 +1115,7 @@
             if ((reg = get_input_dregister_var(freg_var,n,is_code0,1))) {
                 n->sc = DREGISTER;
                 n->dsp = cadr(reg);
-                fregs[n->dsp]= INPUT_REG;
+                regs[n->dsp]= INPUT_REG;
                 freg_var++;
                 caddr(args)=size(type); /* why we need this? */
             }
@@ -1124,7 +1177,7 @@
             regs[REG_VAR_BASE-i]=REG_VAR; 
             if (i>max_reg_var) max_reg_var=i;
             /* その場所を表す番号を返す */
-            return list3n(REGISTER,REG_VAR_BASE-i,n); 
+            return list3n(REGISTER,REG_VAR_BASE-i,nptr); 
         }
     }
     return list3n(LVAR,new_lvar(SIZE_OF_INT),0);
@@ -1140,7 +1193,7 @@
             if (i>max_freg_var) max_freg_var=i;
             /* その場所を表す番号を返す */
             return list3n(DREGISTER,
-                FREG_VAR_BASE-i+FREG_OFFSET,n); 
+                FREG_VAR_BASE-i+FREG_OFFSET,nptr); 
         }
     }
     return list3n(LVAR,new_lvar(SIZE_OF_DOUBLE),0);
@@ -1188,12 +1241,12 @@
 code_gvar(int e1,int creg) {
     use_int(creg);
 #ifdef __APPLE__
-    NMTBL nptr = ncaddr(e1);
+    NMTBL *nptr = ncaddr(e1);
     if (nptr->sc==STATIC && !(is_code(nptr)||is_function(nptr))) {
-	printf("\tleaq _%s+%d(%%rip),%s\n", ntpr->nm,cadr(e1),register_name(creg,0));
+	printf("\tleaq _%s+%d(%%rip),%s\n", nptr->nm,cadr(e1),register_name(creg,0));
 	return;
     }
-    int r = get_ptr_cache(ntpr);
+    int r = get_ptr_cache(nptr);
     if (cadr(e1)) {
 	printf("\tleaq %d(%s),%s\n", cadr(e1),register_name(r,0),
 		register_name(creg,0));
@@ -1219,9 +1272,9 @@
         return;
     }
 #ifdef __APPLE__
-    NMTBL nptr = ncaddr(e1);
+    NMTBL *nptr = ncaddr(e1);
     if (nptr->sc==STATIC && !(is_code(nptr)||is_function(nptr))) {
-	printf("\tmovl _%s+%d(%%rip),%s\n", ntpr->nm,cadr(e1),register_name(creg,SIZE_OF_INT));
+	printf("\tmovl _%s+%d(%%rip),%s\n", nptr->nm,cadr(e1),register_name(creg,SIZE_OF_INT));
 	return;
     }
     int r = get_ptr_cache(ncaddr(e1));
@@ -1252,9 +1305,9 @@
 code_crgvar(int e1,int creg,int sign,int sz){
     use_int(creg);
 #ifdef __APPLE__
-    NMTBL nptr = ncaddr(e1);
+    NMTBL *nptr = ncaddr(e1);
     if (nptr->sc==STATIC && !(is_code(nptr)||is_function(nptr))) {
-	printf("\t%s _%s+%d(%%rip),%s\n", cload(sign,sz),ntpr->nm,cadr(e1),register_name(creg,0));
+	printf("\t%s _%s+%d(%%rip),%s\n", cload(sign,sz),nptr->nm,cadr(e1),register_name(creg,0));
 	return;
     }
     int r = get_ptr_cache(ncaddr(e1));
@@ -1296,8 +1349,8 @@
 void
 code_rlvar(int e2,int reg) {
     use_int(reg);
-    if (car(e1)==URLVAR) {
-        code_crlvar(e1,creg,0,SIZE_OF_INT);
+    if (car(e2)==URLVAR) {
+        code_crlvar(e2,creg,0,SIZE_OF_INT);
         return;
     }
     printf("\tmovl "); lvar(e2);
@@ -2066,8 +2119,6 @@
     code_call(e2,fn,jmp);
     free_register_var(reg_arg_list);
     if (ret_type==DOUBLE||ret_type==FLOAT) {
-    } else if (ret_type==LONGLONG||ret_type==ULONGLONG) {
-        use_longlong0(USE_CREG);
     } else if (ret_type==VOID) {
     } else {
         if (use)
@@ -2109,7 +2160,7 @@
 int
 code_frame_pointer_register()
 {
-    return list2(REGISTER,REG_fp);
+    return list2(REGISTER,REG_EBP);
 }
 
 void
@@ -2324,7 +2375,7 @@
 	break;
     case MUL:
     case UMUL:
-	printf("\t%s %s,%s\n","imul%s",q,orn,crn);
+	printf("\timul%s %s,%s\n",q,orn,crn);
 	break;
     case DIV:
     case UDIV:
@@ -2384,7 +2435,7 @@
     long orn ;
     if (car(orn1)==CONST) orn = cadr(orn1);
     else if (car(orn1)==LCONST) orn = lcadr(orn1);
-    else error(-1);
+    else { orn =0; error(-1); }
 
     datareg=is_data_reg(reg);
 
@@ -2435,7 +2486,7 @@
     case MUL:
     case UMUL:
 	if (ilog(orn)) {
-	    printf("\tsal%s $%ld,%s\n",q,ilog(orn),crn);
+	    printf("\tsal%s $%d,%s\n",q,ilog(orn),crn);
 	} else
 	    printf("\t%s%s $%ld,%s\n","imul",q,orn,crn);
 	break;
@@ -2502,9 +2553,19 @@
     // printf(".text\n");
 }
 
+static void
+emit_lib(char *p[])
+{
+  while(*p) {
+    printf("%s\n",*p++);
+  }
+}
+
 void
 code_closing()
 {
+  if (float_one_lib_used) emit_lib(float_one_lib);
+  if (float_one_f_lib_used) emit_lib(float_one_f_lib);
     global_table();
     printf("\t.ident \"Micro-C compiled\"\n");
 }
@@ -2710,7 +2771,7 @@
     code_offset_set(fnptr);
 
     printf("\tlea %d(%%ebp),%%esp\n",-12);
-    printf("\tpopq %%rbx\n");  <--- register save
+    printf("\tpopq %%rbx\n"); // <--- register save
     printf("\tleave\n");
     printf("\tret\n");
 #ifndef __APPLE__
@@ -2757,7 +2818,7 @@
         set_freg(RET_FREGISTER,mode);
 	return 0;
     } else if (cadr(fnptr->ty)==DOUBLE) {
-        set_dreg(RET_DREGISTER,mode);
+        set_freg(RET_DREGISTER,mode);
 	return 0;
     } else if (cadr(fnptr->ty)==LONGLONG||cadr(fnptr->ty)==ULONGLONG) {
         set_lreg(RET_LREGISTER,mode);
@@ -2967,23 +3028,10 @@
     }
 }
 
-#if LONGLONG_CODE
-static long long ll0 = 1LL;
-
-static int 
-code_l1(long long d)
-{
-    int *i = (int *)&ll0; int *j = (int *)&d;
-    return (i[1] == 1)?j[1]:j[0];
-}
-
-static int 
-code_l2(long long d)
-{
-    int *i = (int *)&ll0; int *j = (int *)&d;
-    return (i[1] == 1)?j[0]:j[1];
-}
-#endif
+
+
+
+
 
 void
 global_table(void)
@@ -3140,39 +3188,42 @@
 {
     return d?"movsd":"movss";
 }
-
+#define fstore(d) fload(d)
 
 void code_dassign_gvar(int e2,int freg,int d)
 { 
 #ifdef __APPLE__
     int r = get_ptr_cache(ncaddr(e2));
     if (cadr(e2)) 
-	printf("\t%s %s,%d(%s)\n",fstore(d),register_name(freg,0),cadr(e2),register_name(r,0));
+	printf("\t%s %s,%d(%s)\n",fstore(d),fregister_name(freg),cadr(e2),register_name(r,0));
     else
-	printf("\t%s %s,(%s)\n",fstore(d),register_name(freg,0),register_name(r,0));
+	printf("\t%s %s,(%s)\n",fstore(d),fregister_name(freg),register_name(r,0));
 #else
     if (cadr(e2)) 
-	printf("\t%s %s,%s+%d\n",fstore(d),register_name(freg,0),(ncaddr(e2))->nm,cadr(e2));
+	printf("\t%s %s,%s+%d\n",fstore(d),fregister_name(freg),(ncaddr(e2))->nm,cadr(e2));
     else
-	printf("\t%s %s,%s\n",fstore(d),register_name(freg,0),(ncaddr(e2))->nm);
+	printf("\t%s %s,%s\n",fstore(d),fregister_name(freg),(ncaddr(e2))->nm);
 #endif
 }
 
 void code_dassign_lvar(int e2,int freg,int d)
 { 
-    printf("\t%s %s,",fstore(d),register_name(freg,0)); lvar(e2); printf("\n");
+    printf("\t%s %s,",fstore(d),fregister_name(freg)); lvar(e2); printf("\n");
 }
 
 void code_dassign_dregister(int e,int d,int freg)
 {
     int reg = cadr(e);
-    printf("\tmovapd %s,%s",register_name(freg,0),regsiter_name(reg,0)); 
+    printf("\tmovapd %s,%s",fregister_name(freg),register_name(reg,0)); 
 }
 
 void code_dassign(int e2,int freg,int d)
 { 
-    printf("\t%s %s,(%s)\n",fstore(d),register_name(freg,0),register_name(e2,0));
-}
+    printf("\t%s %s,(%s)\n",fstore(d),fregister_name(freg),register_name(e2,0));
+}
+
+static int
+code_dload_1(int d);
 
 static double d0 = 1.0;
 
@@ -3190,18 +3241,28 @@
     return (i[1] == 0x3ff00000)?j[1]:j[0];
 }
 
+/* load double / float const
+   we should keep what constant we have create
+ */
 void code_dconst(int e2,int freg,int d)
 { 
     int lb;
     double value = dcadr(e2);
-#if 0
-    if (value==0.0) {
-	printf("\tfldz\n"); return;
+    if (value==1.0) {
+        set_freg(code_dload_1(d),0);
+        return;
     }
-    if (value==1.0) {
-	printf("\tfld1\n"); return;
+    use_float(d,freg);
+    if (value==0.0) {
+        char *f = fregister_name(freg);
+        if (d) {
+            printf("\txorpd %s,%s\n",f,f);
+        } else {
+            printf("\txorps %s,%s\n",f,f);
+        }
+        return ;
     }
-#endif
+
 #ifdef __APPLE__
     printf(" \t.literal8\n\t.align 3\n");
 #else
@@ -3218,9 +3279,9 @@
 	text_mode(0);
     }
 #ifdef __APPLE__
-    printf("\tmovsd _%d(%%rip),%s\n",lb,register_name(freg,0));
+    printf("\tmovsd _%d(%%rip),%s\n",lb,fregister_name(freg));
 #else
-    printf("\tmovsd _%d,%s\n",lb,register_name(freg,0));
+    printf("\tmovsd _%d,%s\n",lb,fregister_name(freg));
 #endif
 }
 
@@ -3264,7 +3325,7 @@
 void code_i2d(int reg)
 { 
     int c = reg;
-    use_double(reg);
+    use_float(1,reg);
     printf("\tcvtsi2sd     %s,%s\n",register_name(c,0),register_name(creg,0));
 }
 
@@ -3275,33 +3336,34 @@
     printf("\tcvttsd2siq      %s,%s\n",register_name(f,0),register_name(reg,0));
 }
 
-void code_u2d1(int reg,int db)
+void code_u2d1(int reg,int d)
 { 
     int i = reg;
-    char *u = get_regsiter_name(i);
+    char *u = register_name(i,0);
     int tmp = get_data_register();
-    char *t = get_regsiter_name(tmp);
-    use_int(reg);
-    char *d = get_regsiter_name(reg);
-    int td = get_dregsiter(d);
-    char *dbs = db?"d":"s";
+    char *t = register_name(tmp,0);
+    use_float(reg,d);
+    char *dn = fregister_name(reg);
+    int td = get_dregister(d);
+    char *dbs = d?"d":"s";
     printf("        cmpq    $0, %s\n",u);
     printf("        js      f1\n");
-    printf("        cvtsi2%sdq       %s,%s\n",dbs,u,d);
+    printf("        cvtsi2%sdq       %s,%s\n",dbs,u,dn);
     printf("        jmp     f2\n");
     printf("1:\n");
     printf("        movq    %s, %s\n",u,t);
-    printf("        shrq    %s\n",get_register_name(t));
-    printf("        andl    $1, %s\n",get_register_name(u,SIZE_OF_INT));
+    printf("        shrq    %s\n",t);
+    printf("        andl    $1, %s\n",register_name(i,SIZE_OF_INT));
     printf("        orq     %s, %s\n",t,u);
-    printf("        cvtsi2%sdq       %s, %s\n",dbs,u,d);
-    printf("        movap%s  %s, %s\n",db,d,get_register_name(td,0));
-    printf("        adds%s   %s, %s\n",db,d,get_register_name(td,0));
+    printf("        cvtsi2%sdq       %s, %s\n",dbs,u,dn);
+    printf("        movap%s  %s, %s\n",dbs,dn,register_name(td,0));
+    printf("        adds%s   %s, %s\n",dbs,dn,register_name(td,0));
     printf("2:\n");
     free_register(tmp);
     free_register(td);
 }
-void code_u2d1(int reg) {
+
+void code_u2d(int reg) {
     code_u2d1(reg,1);
 }
 
@@ -3321,7 +3383,7 @@
 
 void code_i2f(int reg) { 
     int c = reg;
-    use_double(reg);
+    use_float(0,reg);
     printf("\tcvtsi2ss     %s,%s\n",register_name(c,0),register_name(creg,0));
 }
 void code_u2f(int reg) { 
@@ -3353,7 +3415,8 @@
 void code_cmp_drgvar(int e2,int reg,int d,int label,int cond)
 { 
     char *db = d?"d":"s";
-    char *f = get_register_name(reg,0);
+    use_float(d,reg);
+    char *f = fregister_name(reg);
     int t = get_dregister(d);
 #ifdef __APPLE__
     int r = get_ptr_cache(ncaddr(e2));
@@ -3367,8 +3430,8 @@
     else
 	printf("\tmovs%s %s,%s\n",db,(ncaddr(e2))->nm,f);
 #endif
-    printf("\txorp%s %s,%s\n",db,f,register_name(t,0));
-    printf("\tucomis%s %s,%s\n",db,f,get_register_name(t,0));
+    printf("\txorp%s %s,%s\n",db,f,fregister_name(t));
+    printf("\tucomis%s %s,%s\n",db,f,fregister_name(t));
     free_register(t);
     jcond(label,cond);
 }
@@ -3379,6 +3442,7 @@
     jcond(label,cond);
 }
 
+void
 dtosop(int op,int reg,int oreg)
 { 
     char *opn="";
@@ -3387,9 +3451,15 @@
     int ox = -1;
 
     use_float(1,reg);
-    if (!(oreg<= -REG_LVAR_OFFSET)) {
-	grn=fregister_name(oreg);
+    if(oreg==-1) {
+        error(-1);
+    } else if (oreg<= -REG_LVAR_OFFSET) {
+        ox = get_dregister(1); if (ox<0) error(-1);
+        regs[ox] = USING_REG;
+        code_drlvar(oreg+REG_LVAR_OFFSET,1,ox);
+        oreg = ox;
     }
+    grn=fregister_name(oreg);
     frn=fregister_name(reg);
     switch(op) {
     case FADD: opn="addss"; break;
@@ -3460,19 +3530,24 @@
     emit_dpop_free(xreg,d);
 }
 
-#define fregister_name(a)  register_name(a,0)
-
 static int
 code_dload_1(int d)
 {
     int r,g;
     char *drn,*grn;
+    NMTBL *one;
     // load 1
-    float_one_lib_used=1;
-    r = get_ptr_cache(&float_one);
-    drn=register_name(r);
+    if (d) {
+        float_one_lib_used=1;
+        one = &float_one;
+    } else {
+        float_one_lib_used=1;
+        one = &float_one_f;
+    }
+    r = get_ptr_cache(one);
+    drn=register_name(r,0);
     grn=fregister_name(g=get_dregister(d));
-    printf("\tmovs%s %s,0(%s)\n",d?"d":"s",grn,drn);
+    printf("\tmovs%s 0(%s),%s\n",d?"d":"s",grn,drn);
     return g;
 }
 
@@ -3483,7 +3558,7 @@
     char *ops = (caddr(e1)>0)?(d?"addsd":"addss"):(d?"subsd":"addss");
 
     if (car(e2)==DREGISTER||car(e2)==FREGISTER) {
-        crn=register_name(cadr(e2));
+        crn=fregister_name(cadr(e2));
         grn = fregister_name(g = code_dload_1(d));
         if (reg==USE_CREG) {
             reg=get_dregister(d); if (!reg) error(-1);
@@ -3492,11 +3567,11 @@
         frn=fregister_name(reg);
         printf("\t%s %s,%s,%s\n",ops,crn,crn,grn);
         if (use && reg!=cadr(e2))
-            printf("\movap%s %s,%s\n",d?"d":"s",frn,crn);
+            printf("\tmovap%s %s,%s\n",d?"d":"s",frn,crn);
     } else {
         g_expr(e2);
         if (!is_int_reg(creg)) error(-1);
-        crn=register_name(ireg);
+        crn=register_name(ireg,0);
         if (reg==USE_CREG) {
             reg=get_dregister(d); if (!reg) error(-1);
             set_freg(reg,0);
@@ -3517,7 +3592,7 @@
     char *ops = (caddr(e1)>0)?(d?"addsd":"addss"):(d?"subsd":"addss");
 
     if (car(e2)==DREGISTER||car(e2)==FREGISTER) {
-        crn=register_name(cadr(e2));
+        crn=fregister_name(cadr(e2));
         grn = fregister_name(g = code_dload_1(d));
         if (reg==USE_CREG) {
             reg=get_dregister(d); if (!reg) error(-1);
@@ -3530,7 +3605,7 @@
     } else {
         g_expr(e2);
         if (!is_int_reg(creg)) error(-1);
-        crn=register_name(ireg);
+        crn=register_name(ireg,0);
         if (reg==USE_CREG) {
             reg=get_dregister(d); if (!reg) error(-1);
             set_freg(reg,0);
@@ -3644,20 +3719,14 @@
 	    clear_ptr_cache_reg(REG_EAX);
 #endif
     int tmp = get_dregister(d);
-    char *n = register_name(tmp,0);
-    char *c = register_name(creg,0);
+    char *n = fregister_name(tmp);
+    char *c = fregister_name(creg);
     char *sd = d?"d":"s";
     printf("\txorp%s %s,%s\n",sd,n,n); 
-    printf("\tucomis%s %s,%s\n",sd,register_name(ca),s);
+    printf("\tucomis%s %s,%s\n",sd,c,n);
     jcond(label,cond);
 }
 
-int pop_fregister()
-{ 
-    if (freg_sp<0) { error(-1); return -1;}
-    return freg_stack[--freg_sp];
-}
-
 int emit_dpop(int d)
 { 
     int xreg,reg;
@@ -3762,14 +3831,14 @@
 
 void code_cmp_lregister(int reg,int label,int cond)
 {
-    use_longlong(e2);
-    printf("\tcmpq $0,%s\n",register_name(e2,0));
+    use_longlong(reg);
+    printf("\tcmpq $0,%s\n",register_name(reg,0));
     jcond(label,cond);
 }
 
 void code_cmp_lrgvar(int e1,int e2,int label,int cond)
 {
-    use_longlong(reg);
+    use_longlong(e2);
 #ifdef __APPLE__
     int r = get_ptr_cache(ncaddr(e1));
     if (cadr(e1))
@@ -3787,7 +3856,7 @@
 
 void code_cmp_lrlvar(int e1,int e2,int label,int cond)
 {
-    use_longlong(reg);
+    use_longlong(e2);
     printf("\tcmpq $0,"); lvar(e1); printf("\n");
     jcond(label,cond);
 }
@@ -3806,19 +3875,19 @@
 
 void code_lassign_lvar(int e1,int e2)
 {
-    code_assign_lvar(e1,e2,SIZE_OF_LONGLONG)
+    code_assign_lvar(e1,e2,SIZE_OF_LONGLONG);
 }
 
 void code_lassign_lregister(int e2,int reg)
 {
-    code_lassign_lregister(e2,SIZE_OF_LONGLONG,reg)
+    code_assign_register(e2,SIZE_OF_LONGLONG,reg);
 }
 
 void
 code_lconst(int e1,int creg)
 {
     use_longlong(creg);
-    printf("\tmovq $%ld,%s\n",lcadr(e1),l_eax(creg));
+    printf("\tmovq $%lld,%s\n",lcadr(e1),register_name(creg,0));
 }
 
 void code_lneg(int e1)
@@ -3840,7 +3909,7 @@
 void
 ltosop(int op,int reg,int e2)
 {
-    tosop1(op,reg,e2,"q");
+    tosop1(op,reg,e2,SIZE_OF_LONGLONG);
 }
 
 int code_lconst_op_p(int op,int e) {
@@ -3872,7 +3941,7 @@
 }
 
 void loprtc(int op,int reg,int e) {
-    oprtc(op,reg,e,SIZE_OF_LONGLONG);
+    oprtc1(op,reg,e,SIZE_OF_LONGLONG);
 }
 
 void emit_lpop_free(int e1)
@@ -3904,7 +3973,7 @@
 
 void code_u2ll(int reg)
 {
-    printf("\tmovslq %s,%s",register_name(reg,SIZE_OF_INT),regisnter_name(reg,0));
+    printf("\tmovslq %s,%s",register_name(reg,SIZE_OF_INT),register_name(reg,0));
     //printf("\tshlq $32,%s",regisnter_name(reg,0));
     //printf("\tshrq $32,%s",regisnter_name(reg,0));
 }
@@ -3963,7 +4032,7 @@
 void code_ll2d(int reg)
 {
     char *f = register_name(reg,0);
-    use_float(reg,1);
+    use_float(1,reg);
     printf("\tcvtsi2sdq      %s,%s\n",f,register_name(reg,0));
 }
 
@@ -3987,7 +4056,8 @@
 
 void code_lpreinc(int e1,int e2,int reg)
 {
-    char *xrn;
+    char *xrn; 
+    int dir=caddr(e1);
     if (car(e2)==REGISTER) {
         use_int(reg);
         printf("\taddq $%d,%s\n",dir,register_name(cadr(e2),0));
@@ -4001,12 +4071,13 @@
     printf("\taddq $%d,(%s)\n",dir,xrn)
 ;
     if (use)
-        printf("\t%s (%s),%s\n",cload(sign,SIZE_OF_LONGLONG),xrn,register_name(reg,0));
+        printf("\t%s (%s),%s\n",cload(0,SIZE_OF_LONGLONG),xrn,register_name(reg,0));
 }
 
 void code_lpostinc(int e1,int e2,int reg)
 {
     char *xrn;
+    int dir=caddr(e1);
     if (car(e2)==REGISTER) {
         use_int(reg);
         if (use)
@@ -4020,8 +4091,8 @@
     xrn = register_name((e2=emit_pop(0)),0);
     use_int(reg);
     if (use)
-        printf("\t%s (%s),%s\n",cload(sign,SIZE_OF_LONGLONG),xrn,register_name(reg,0));
-    printf("\taddq $%d,(%s)\n",dir,xrn)
+        printf("\t%s (%s),%s\n",cload(0,SIZE_OF_LONGLONG),xrn,register_name(reg,0));
+    printf("\taddq $%d,(%s)\n",dir,xrn);
     emit_pop_free(e2);
 }
 
@@ -4034,7 +4105,7 @@
     use_longlong(creg);
     xreg = emit_pop(0);       /* pop e3 value */
     emit_push();
-    ld_indexx(byte,0,creg,ireg,0);
+    ld_indexx(SIZE_OF_LONGLONG,0,creg,ireg,0);
     tosop(op,ireg,xreg);
     emit_pop_free(xreg);
     xreg = emit_pop(0);       /* pop e3 value */
@@ -4046,7 +4117,9 @@
 void
 code_register_lassop(int reg,int op) {
     use_longlong(reg);
-    ltosop(op,e2,reg);
+    int xreg = emit_lpop();
+    ltosop(op,xreg,reg);
+    emit_lpop_free(xreg);
 }
 
 
@@ -4459,7 +4532,7 @@
 static void
 make_mask_and_or_l(long mask,int reg)
 {
-printf("## mask 0x%08x ~0x%08x\n",mask,~mask);
+        printf("## mask 0x%08lx ~0x%08lx\n",mask,~mask);
 	printf("\tpushq %s\n",register_name(reg,0));
 	/* make and-mask  */
 	loprtc(BOR,reg,llist2(CONST,~mask));
@@ -4485,7 +4558,6 @@
 // printf("## %d: bitpos=%d bitsize=%d bitsz=%d\n",lineno,bitpos,bitsize,bitsz);
     if (l) {
 #if LONGLONG_CODE
-	int push=0;
 	use_int(adr);
 	use_longlong(value);
 	/* shift left */
@@ -4496,11 +4568,10 @@
 	printf("\t%s %d(%s),%s\n",cload(sign,size),4,register_name(adr,0),
 		    register_name(adr,0));
 	mask = make_mask(64-bitpos-bitsize,63-bitpos);
-	make_mask_and_or_l(mask,value,adr);
-	printf("\tmovq 0(%%rsp),%s\n",register_name(adr,0));
+	make_mask_and_or_l(mask,value);
+	printf("\tpopq %s\n",register_name(adr,0));
 	printf("\t%s %s,(%s)\n",move(0),
-		value, register_name(adr,0));
-	printf("\taddq %%rsp,$8\n");
+               register_name(value,0), register_name(adr,0));
 #endif
     } else {
 	use_int(adr);
@@ -4513,7 +4584,7 @@
 	    oprtc(LSHIFT,value,list2(CONST,bitpos));
 	/* make and-mask */
 	mask = make_mask(32-bitpos-bitsize,31-bitpos);
-	make_mask_and_or(mask,value,adr);
+	make_mask_and_or(mask,value);
 	printf("\tpopl %s\n",register_name(adr,SIZE_OF_INT));
         code_assign(adr,size==4?0:size,value);
     }
@@ -4533,23 +4604,23 @@
 	/* do conjunction  */
       if (is_data_reg(reg) && ((a& ~0xffff)==~0xffff)) {
 	    if ((a& ~0xff)==~0xff)
-		printf("\tandb $%d,%s\n",a&0xff,register_name(reg,1));
+		printf("\tandb $%ld,%s\n",a&0xff,register_name(reg,1));
 	    else
-		printf("\tandw $%d,%s\n",a&0xffff,register_name(reg,2));
+		printf("\tandw $%ld,%s\n",a&0xffff,register_name(reg,2));
 	} else
-	    printf("\tandq $%d,%s\n",a,register_name(reg,0));
+	    printf("\tandq $%ld,%s\n",a,register_name(reg,0));
     }
     /* make or-mask  */
     c = mask&c;
     if (c!=0) {
 	/* do disjunction  */
-      if (is_data(reg) && (!(c& ~0xffff))) {
+      if (is_data_reg(reg) && (!(c& ~0xffff))) {
 	    if (!(c& ~0xff))
-		printf("\torb $%d,%s\n",c&0xff,register_name(reg,1));
+		printf("\torb $%ld,%s\n",c&0xff,register_name(reg,1));
 	    else
-		printf("\torw $%d,%s\n",c&0xffff,register_name(reg,2));
+		printf("\torw $%ld,%s\n",c&0xffff,register_name(reg,2));
 	} else
-	    printf("\torq $%d,%s\n",c,register_name(reg,0));
+	    printf("\torq $%ld,%s\n",c,register_name(reg,0));
     }
 }
 
--- a/mc-code-powerpc.c	Mon Nov 08 20:16:43 2010 +0900
+++ b/mc-code-powerpc.c	Mon Nov 08 22:22:46 2010 +0900
@@ -4118,8 +4118,7 @@
 0
 };
 
-char *
-fstore(int d)
+char * fstore(int d)
 {
     return (d?"stfd":"stfs");
 }
@@ -6105,7 +6104,7 @@
 #endif
 }
 
-void
+static void
 emit_lib(char *p[])
 {
     while(*p) {
@@ -6122,11 +6121,13 @@
     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);
+    d2u_lib_used = u2d_lib_used= float_one_lib_used= float_zero_lib_used = i2d_lib_used = 0;
 #endif
 #if LONGLONG_CODE
     if (asld_lib_used) emit_lib(asld_lib);
     if (lsrd_lib_used) emit_lib(lsrd_lib);
     if (asrd_lib_used) emit_lib(asrd_lib);
+    asld_lib_used = lsrd_lib_used = asrd_lib_used = 0;
 #if 0
     if (lumod_lib_used) emit_lib(lumod_lib);
     if (lmod_lib_used) emit_lib(lmod_lib);