changeset 616:2ba903c8e749

builtin_fabs builtin_inf()
author kono
date Wed, 06 Sep 2006 20:58:03 +0900
parents 2dee957ef988
children efc99e397413
files .gdbinit Changes mc-code-arm.c mc-code-ia32.c mc-code-mips.c mc-code-powerpc.c mc-code.h mc-codegen.c mc-inline.c mc-parse.c mc.h
diffstat 11 files changed, 400 insertions(+), 125 deletions(-) [+]
line wrap: on
line diff
--- a/.gdbinit	Wed Sep 06 15:21:06 2006 +0900
+++ b/.gdbinit	Wed Sep 06 20:58:03 2006 +0900
@@ -52,8 +52,9 @@
 # run -s test/linux_kernel.c.--- 
 # run -s test/stralign.c
 # run -s test/putenemy.c
+run -s test/float_gcc.c
 # run -s test/func_conv_err.c
-run -DINLINE=inline test/tmp7.c
+# run -DINLINE=inline test/tmp7.c
 # run -DINLINE=inline test/code-gen-all.c
 # run -s throw.c
-run -s test/simp.c
+# run -s test/simp.c
--- a/Changes	Wed Sep 06 15:21:06 2006 +0900
+++ b/Changes	Wed Sep 06 20:58:03 2006 +0900
@@ -8766,3 +8766,44 @@
     S式表現のparser
 を書かないと。
 
+Wed Sep  6 16:42:43 JST 2006
+
+gcc の C99 だと、
+
+ __builtin_fabsf(__x)   // float
+ __builtin_fabs(__x)    // double
+ __builtin_fabsl(__x)   // long double
+ __builtin_inf()
+ __builtin_inff()
+
+とかいうのがあるみたいだね。
+
+cheat するより、実装するのが簡単か?
+
+    __DBL_MIN__  1.17549435e-38F
+    __DBL_MIN__ 2.2250738585072014e-308
+    __LDBL_MIN__ 2.00416836000897277799610805135016e-292L
+
+も内蔵なのか。まぁ、これは書けば良いだけだが。
+
+long double ねぇ。こんなのやるのやだ ...
+
+     int foo asm ("myfoo") = 2;
+
+This specifies that the name to be used for the variable `foo' in the
+assembler code should be `myfoo' rather than the usual `_foo'.
+
+ You cannot use `asm' in this way in a function _definition_; but you
+can get the same effect by writing a declaration for the function
+before its definition and putting `asm' there, like this:
+
+     extern func () asm ("FUNC");
+
+こんな brain dead があるのか。困ったものだ。
+
+これ、どうやって実現するのかな。attribute でしょうね。ASM_NAME か、なんか?
+とりあえず無視?
+
+__asm になっているからだめなので、__asm も 処理すると「無視する」モードに
+なっていることがわかりました。
+
--- a/mc-code-arm.c	Wed Sep 06 15:21:06 2006 +0900
+++ b/mc-code-arm.c	Wed Sep 06 20:58:03 2006 +0900
@@ -4109,6 +4109,23 @@
 
 
 void
+code_builtin_fabsf(int e)
+{
+}
+void
+code_builtin_fabs(int e)
+{
+}
+void
+code_builtin_inff()
+{
+}
+void
+code_builtin_inf(int op)
+{
+}
+
+void
 code_dneg(int freg,int d)
 { 
     char *frn;
--- a/mc-code-ia32.c	Wed Sep 06 15:21:06 2006 +0900
+++ b/mc-code-ia32.c	Wed Sep 06 20:58:03 2006 +0900
@@ -2420,6 +2420,23 @@
     printf("\tfldl _%d\n",lb);
 }
 
+void
+code_builtin_fabsf(int e)
+{
+}
+void
+code_builtin_fabs(int e)
+{
+}
+void
+code_builtin_inff()
+{
+}
+void
+code_builtin_inf()
+{
+}
+
 void code_dneg(int freg,int d)
 { 
     printf("\tfchs\n");
--- a/mc-code-mips.c	Wed Sep 06 15:21:06 2006 +0900
+++ b/mc-code-mips.c	Wed Sep 06 20:58:03 2006 +0900
@@ -3682,6 +3682,23 @@
 
 
 void
+code_builtin_fabsf(int e)
+{
+}
+void
+code_builtin_fabs(int e)
+{
+}
+void
+code_builtin_inff()
+{
+}
+void
+code_builtin_inf()
+{
+}
+
+void
 code_dneg(int freg,int d)
 { 
     char *frn;
--- a/mc-code-powerpc.c	Wed Sep 06 15:21:06 2006 +0900
+++ b/mc-code-powerpc.c	Wed Sep 06 20:58:03 2006 +0900
@@ -43,6 +43,9 @@
 #define __builtin_va_start(ap,arg) ap=(((int)(&arg))+sizeof(arg))\n\
 #define __builtin_va_arg(ap,type)  (*((type *)ap)++)\n\
 #define alloca __builtin_alloca\n\
+#define    __FLT_MIN__  1.17549435e-38F\n\
+#define    __DBL_MIN__ 2.2250738585072014e-308\n\
+#define    __LDBL_MIN__ 2.00416836000897277799610805135016e-292L\n\
 ";
 
 #define TEXT_EMIT_MODE 0
@@ -1327,7 +1330,6 @@
     }
 }
 
-
 void
 code_neg(int creg) {
     use_int(creg);
@@ -3455,7 +3457,6 @@
 }
 
 void
-
 code_dconst(int e2,int freg,int d)
 { 
     int lb;
@@ -3508,6 +3509,78 @@
     free_register(r);
 }
 
+void
+code_builtin_fabsf(int e)
+{
+    char *frn;
+    g_expr0(e);
+    use_float(0,freg);
+    frn = fregister_name(freg);
+    printf("\tfabs %s,%s\n",frn,frn);
+}
+
+void
+code_builtin_fabs(int e)
+{
+    char *frn;
+    g_expr0(e);
+    use_float(1,freg);
+    frn = fregister_name(freg);
+    printf("\tfabs %s,%s\n",frn,frn);
+}
+
+static void
+code_inf(int d)
+{
+    int lb;
+    int r;
+    char *rrn,*frn;
+    int freg = USE_CREG;
+
+    use_float(d,freg);
+    frn = fregister_name(freg);
+    rrn = register_name((r=get_register()));
+    use_reg(r); // to clear ptr cache
+    printf(" \t.data\n\t.align 3\n");
+    lb=fwdlabel();
+    printf("L_%d:\n",lb);
+    if (d) {
+#if ENDIAN_D==0
+	printf("\t.long\t0x%x,0x%x\n",0,0x7ff00000);
+#else
+	printf("\t.long\t0x%x,0x%x\n",0x7ff00000,0);
+#endif
+    } else {
+	printf("\t.long\t0x%x\n",0x7f800000);
+    }
+    if (output_mode==TEXT_EMIT_MODE) {
+	printf(".text\n");
+    } else {
+	text_mode(0);
+    }
+    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);
+    if (d) {
+	printf("\tlfd %s,0(%s)\n",frn,rrn);
+    } else {
+	printf("\tlfs %s,0(%s)\n",frn,rrn);
+    }
+    free_register(r);
+}
+
+void
+code_builtin_inff()
+{
+    code_inf(0);
+}
+
+void
+code_builtin_inf()
+{
+    code_inf(1);
+}
+
+
 
 void
 code_dneg(int freg,int d)
--- a/mc-code.h	Wed Sep 06 15:21:06 2006 +0900
+++ b/mc-code.h	Wed Sep 06 20:58:03 2006 +0900
@@ -278,6 +278,10 @@
 #endif
 
 extern void code_alloca(int e,int reg);
+extern void code_builtin_fabsf(int e);
+extern void code_builtin_fabs(int e);
+extern void code_builtin_inff();
+extern void code_builtin_inf();
 extern void code_arg_register(NMTBL *fnptr);
 
 extern int get_register();
--- a/mc-codegen.c	Wed Sep 06 15:21:06 2006 +0900
+++ b/mc-codegen.c	Wed Sep 06 20:58:03 2006 +0900
@@ -548,6 +548,20 @@
 	/* Too late. Should be evaluated in pexpr. */
 	code_const(is_const(e2),USE_CREG);
 	return INT;
+    case BUILTIN_FABSF:
+	code_builtin_fabsf(e2);
+        return FLOAT;
+    case BUILTIN_FABS:
+    case BUILTIN_FABSL:
+	code_builtin_fabs(e2);
+        return DOUBLE;
+    case BUILTIN_INFF:
+	code_builtin_inff();
+        return FLOAT;
+    case BUILTIN_INF:
+    case BUILTIN_INFL:
+	code_builtin_inf();
+        return DOUBLE;
     case COMMA:
 	g_expr_u(e2);
 	return g_expr0(caddr(e1));
--- a/mc-inline.c	Wed Sep 06 15:21:06 2006 +0900
+++ b/mc-inline.c	Wed Sep 06 20:58:03 2006 +0900
@@ -928,6 +928,9 @@
     case STRING:
     case FNAME:
     case FLABEL:
+    case BUILTIN_INF:
+    case BUILTIN_INFF:
+    case BUILTIN_INFL:
 	return e1;
     case RSTRUCT: 
 	// list3(RSTRUCT,e,size)
@@ -1107,6 +1110,17 @@
 	return pdassign(e1);
     case DASSOP: case FASSOP:
 	return pdassop(e1);
+
+    case BUILTIN_FABS:
+    case BUILTIN_FABSF:
+    case BUILTIN_FABSL:
+	e2 = pexpr(e2);
+	if (is_const(e2)) {
+	    if (dcadr(e2) >= 0.0 )  return e2;
+	    return  pexpr(list2(car(e1)==BUILTIN_FABSF?
+		FMINUS:DMINUS,e2));
+	}
+	return list2(car(e1),e2);
 #endif
 #if LONGLONG_CODE
     case LASS: 
--- a/mc-parse.c	Wed Sep 06 15:21:06 2006 +0900
+++ b/mc-parse.c	Wed Sep 06 20:58:03 2006 +0900
@@ -610,6 +610,12 @@
     reserve("__builtin_alloca",ALLOCA);
     reserve("__builtin_constant_p",BUILTINP);
     reserve("__builtin_expect",BUILTIN_EXPECT);
+    reserve("__builtin_fabs",BUILTIN_FABS);
+    reserve("__builtin_fabsf",BUILTIN_FABSF);
+    reserve("__builtin_fabsl",BUILTIN_FABSL);
+    reserve("__builtin_inf",BUILTIN_INF);
+    reserve("__builtin_inff",BUILTIN_INFF);
+    reserve("__builtin_infl",BUILTIN_INFL);
     reserve("__attribute__",ATTRIBUTE);
     reserve("__attribute",ATTRIBUTE);
     reserve("__label__",LABEL);
@@ -619,6 +625,7 @@
     reserve("__LINE__",C_LINE);
 #if ASM_CODE
     reserve("asm",ASM);
+    reserve("__asm",ASM);  // ?
     reserve("__asm__",ASM);
 #endif
 
@@ -1497,8 +1504,10 @@
 	return offset;
     }
     t1 = caddr(type0);  /* list of fields */
-    if (sym==LC) getsym(0);
-    else if (sym==LPAR) {
+    if (sym==LC) {
+	mode = STAT;
+	getsym(0);
+    } else if (sym==LPAR) {
 	// have to be a value, no comma cascading values
         //    .__tcp_lhash_lock = (rwlock_t) { },
         // We cannot this distinguish this case and cascading comma here.
@@ -1560,6 +1569,7 @@
     mode=STADECL;
     nptr0=new_static_name("__lstruct",'_');
     def(nptr0,0);
+    mode=smode;
     //nptr0->next = local_static_list; local_static_list = nptr0;
     //nptr0->sc = STATIC;
     //nptr0->ty = t = type;
@@ -2998,7 +3008,15 @@
     if (sym!=STRING) error(DCERR);
     asm0=list3(STRING,(int)nptr,nptr->dsp);
     getsym(0);
-    if (sym!=COLON) error(DCERR);
+    if (sym!=COLON) {
+#if 0
+	if (sym==RPAR) {
+	    // old type asm statement
+	    goto output;
+	}
+#endif
+	error(DCERR);
+    }
     do {
 	// output expression
 	getsym(0);
@@ -3478,6 +3496,52 @@
 	checksym(RPAR);
 	type=t;
 	return e;
+    case BUILTIN_FABSF:
+	conv->prefix_(sym);
+	getsym(0);
+	checksym(LPAR);
+	e=expr0();
+	checksym(RPAR);
+	e = list2(op,rvalue_t(e,FLOAT));
+	type = FLOAT;
+	if (is_const(e)) {
+#if FLOAT_CODE
+	    return (dcadr(e)>0.0) ? dcadr(e) : dlist2(FCONST,-dcadr(e));
+#endif
+	}
+	return e;
+    case BUILTIN_FABS:
+    case BUILTIN_FABSL:
+	conv->prefix_(sym);
+	getsym(0);
+	checksym(LPAR);
+	e=expr0();
+	checksym(RPAR);
+	e = list2(op,rvalue_t(e,DOUBLE));
+	type = DOUBLE;
+	if (is_const(e)) {
+#if FLOAT_CODE
+	    return (dcadr(e)>0.0) ? dcadr(e) : dlist2(DCONST,-dcadr(e));
+#endif
+	}
+	return e;
+    case BUILTIN_INFF:
+	conv->prefix_(sym);
+	getsym(0);
+	checksym(LPAR);
+	checksym(RPAR);
+	type = FLOAT;
+	e = list2(op,0);
+	return e;
+    case BUILTIN_INF:
+    case BUILTIN_INFL:
+	conv->prefix_(sym);
+	getsym(0);
+	checksym(LPAR);
+	checksym(RPAR);
+	type = DOUBLE;
+	e = list2(op,0);
+	return e;
     case SIZEOF:
 	conv->prefix_(sym);
 	if(getsym(0)==LPAR) {
@@ -3797,11 +3861,11 @@
 		mode=STADECL;
 		def(nptr0,0);
 		e1 = size(type);
+		mode = smode;
 		decl_data_field(type,nptr0,0);
 		conv->rc_();
 		checksym(RC);
 		e1 = list3(RSTRUCT,list3(GVAR,0,(int)nptr0),e1);
-		mode = smode;
 		return e1;
 	    }
 	    e1=expr13();
@@ -4471,6 +4535,13 @@
 		    getch();
 		}
 	    }
+	    /* C99 */
+	    if (ch=='F' || ch=='L') {
+		//   float or long double
+		*cheap->ptr = ch;
+		cheap = increment_cheap(cheap,&num);
+		getch();
+	    }
 	    *cheap->ptr = 0;
 	    cheap = increment_cheap(cheap,&num);
 	    dsymval = strtod(num,0);
--- a/mc.h	Wed Sep 06 15:21:06 2006 +0900
+++ b/mc.h	Wed Sep 06 20:58:03 2006 +0900
@@ -186,28 +186,31 @@
 #define COMP  	13
 #define IVAR  	14
 #define RIVAR	15
-#define LABEL  	16
+#define BUILTIN_INF	16
+#define BUILTIN_INFF	17
+#define BUILTIN_INFL	18
+#define LABEL  	19
 
 #define NULLARY_ARGS(i) (i==RETURN||i==ENVIRONMENT||i==LCALL||i==REGISTER||i==DREGISTER||i==FREGISTER||i==LREGISTER||(GVAR<=(i%SOP)&&(i%SOP)<=LABEL))
 
 /* unary  argments */
 
-#define ADDRESS	17
-#define MINUS  	18
-#define LNOT   	19
-#define BNOT   	20
-#define INC    	21
-#define PERIOD 	22
-#define ARROW  	23
-#define POSTINC	24
-#define UPOSTINC       	25
-#define PREINC 	26
-#define UPREINC	27
-#define POSTDEC	28
-#define UPOSTDEC       	29
-#define PREDEC 	30
-#define UPREDEC	31
-#define DEC    	32
+#define ADDRESS	20
+#define MINUS  	21
+#define LNOT   	22
+#define BNOT   	23
+#define INC    	24
+#define PERIOD 	25
+#define ARROW  	26
+#define POSTINC	27
+#define UPOSTINC       	28
+#define PREINC 	29
+#define UPREINC	30
+#define POSTDEC	31
+#define UPOSTDEC       	32
+#define PREDEC 	33
+#define UPREDEC	34
+#define DEC    	35
 #define CPOSTINC (COP+POSTINC)
 #define CUPOSTINC (COP+UPOSTINC)
 #define CPREINC (COP+PREINC)
@@ -232,9 +235,9 @@
 #define LPREINC (LOP+PREINC)
 #define LUPOSTINC       (LOP+UPOSTINC)
 #define LUPREINC        (LOP+UPREINC)
-#define INDIRECT       	33
-#define RINDIRECT      	34
-#define URINDIRECT     	35
+#define INDIRECT       	36
+#define RINDIRECT      	37
+#define URINDIRECT     	38
 #define CRINDIRECT      (COP+RINDIRECT)
 #define CURINDIRECT     (COP+URINDIRECT)
 #define SRINDIRECT      (SOP+RINDIRECT)
@@ -243,58 +246,61 @@
 #define DRINDIRECT      (DOP+RINDIRECT)
 #define LRINDIRECT      (LOP+RINDIRECT)
 #define LURINDIRECT     (LOP+URINDIRECT)
-#define RSTRUCT	36
-#define ALLOCA 	37
-#define BUILTINP 	38
-#define BUILTIN_EXPECT 	39
-#define ATTRIBUTE 	40
-#define BIT_FIELD 	41
-#define RBIT_FIELD 	42
-#define BPREINC 	43
-#define BPOSTINC 	44
-#define CONV   	45
+#define RSTRUCT	39
+#define ALLOCA 	40
+#define BUILTINP 	41
+#define BUILTIN_EXPECT 	42
+#define BUILTIN_FABS	43
+#define BUILTIN_FABSF	44
+#define BUILTIN_FABSL	45
+#define ATTRIBUTE 	46
+#define BIT_FIELD 	47
+#define RBIT_FIELD 	48
+#define BPREINC 	49
+#define BPOSTINC 	50
+#define CONV   	51
 
 #define UNARY_ARGS(i) (ADDRESS<=(i%SOP)&&(i%SOP)<=CONV)
 
 /* binary  argments */
 
-#define MUL    	46
-#define UMUL   	47
-#define DIV    	48
-#define UDIV   	49
-#define MOD    	50
-#define UMOD   	51
-#define ADD    	52
-#define SUB    	53
-#define CMP    	54      
-#define RSHIFT 	55
-#define URSHIFT	56
-#define LSHIFT 	57
-#define ULSHIFT	58
-#define GT     	59
-#define UGT    	60
-#define GE     	61
-#define UGE    	62
-#define LT     	63
-#define ULT    	64
-#define LE     	65
-#define ULE    	66
-#define EQ     	67
-#define NEQ    	68
-#define BAND   	69
-#define EOR    	70
-#define BOR    	71
-#define LAND   	72
-#define LOR    	73
-#define ASS    	74
-#define UCMP   	75
-#define UCMPGE 	76
-#define CMPGE  	77
-#define CMPEQ  	78
-#define CMPNEQ 	79
-#define ASSOP  	80
-#define UASSOP 	81
-#define COMMA  	82
+#define MUL    	52
+#define UMUL   	53
+#define DIV    	54
+#define UDIV   	55
+#define MOD    	56
+#define UMOD   	57
+#define ADD    	58
+#define SUB    	59
+#define CMP    	60      
+#define RSHIFT 	61
+#define URSHIFT	62
+#define LSHIFT 	63
+#define ULSHIFT	64
+#define GT     	65
+#define UGT    	66
+#define GE     	67
+#define UGE    	68
+#define LT     	69
+#define ULT    	70
+#define LE     	71
+#define ULE    	72
+#define EQ     	73
+#define NEQ    	74
+#define BAND   	75
+#define EOR    	76
+#define BOR    	77
+#define LAND   	78
+#define LOR    	79
+#define ASS    	80
+#define UCMP   	81
+#define UCMPGE 	82
+#define CMPGE  	83
+#define CMPEQ  	84
+#define CMPNEQ 	85
+#define ASSOP  	86
+#define UASSOP 	87
+#define COMMA  	88
 
 #define CASS   	(COP+ASS)
 #define CASSOP 	(COP+ASSOP)
@@ -354,21 +360,21 @@
 #define LEOR    (LOP+EOR)
 #define LBOR    (LOP+BOR)
 
-#define BASS   	83
-#define BASSOP 	84
-#define BFD_REPL 	85
+#define BASS   	89
+#define BASSOP 	90
+#define BFD_REPL 	91
 
-#define JUMP 	86
+#define JUMP 	92
 
-#define STASS  	87
+#define STASS  	93
 
 
 #define BINARY_ARGS(i) ((MUL<=(i%SOP)&&(i%SOP)<=STASS))
 
 /* ternary  argments */
 
-#define COND   	88
-#define UCOND  	89
+#define COND   	94
+#define UCOND  	95
 #define SCOND   (SOP+COND)
 #define SUCOND   (SOP+UCOND)
 #define DCOND   (DOP+COND)
@@ -380,33 +386,33 @@
 
 /* not appeared as tags */
 
-#define LPAR   	90
-#define RPAR   	91
-#define LBRA   	92
-#define RBRA   	93
-#define LC     	94
-#define RC     	95
-#define COLON  	96
-#define SM     	97
-#define CNAME  	98
+#define LPAR   	96
+#define RPAR   	97
+#define LBRA   	98
+#define RBRA   	99
+#define LC     	100
+#define RC     	101
+#define COLON  	102
+#define SM     	103
+#define CNAME  	104
 
-#define I2C  	99
-#define I2S  	100
-#define I2I    	101
-#define I2U    	102
-#define I2D    	103
-#define I2F    	104
-#define I2LL   	105
-#define I2ULL  	106
+#define I2C  	105
+#define I2S  	106
+#define I2I    	107
+#define I2U    	108
+#define I2D    	109
+#define I2F    	110
+#define I2LL   	111
+#define I2ULL  	112
 
-#define U2UC  	107
-#define U2US  	108
-#define U2I    	109
-#define U2U    	110
-#define U2D    	111
-#define U2F    	112
-#define U2LL   	113
-#define U2ULL  	114
+#define U2UC  	113
+#define U2US  	114
+#define U2I    	115
+#define U2U    	116
+#define U2D    	117
+#define U2F    	118
+#define U2LL   	119
+#define U2ULL  	120
 
 
 #define D2I     (DOP+I2I)
@@ -441,27 +447,27 @@
 
 /* statement start */
 
-#define ST_DECL		115
-#define ST_IF		116
-#define ST_DO		117
-#define ST_WHILE	118
-#define ST_FOR		119
-#define ST_SWITCH	120
-#define ST_COMP		121
-#define ST_BREAK	122
-#define ST_CONTINUE	123
-#define ST_CASE		124
-#define ST_DEFAULT	125
-#define ST_RETURN	126
-#define ST_GOTO		127
-#define ST_ASM		128
-#define ST_LABEL	129
-#define ST_OP		130
-#define ST_COMMENT	131
+#define ST_DECL		121
+#define ST_IF		122
+#define ST_DO		123
+#define ST_WHILE	124
+#define ST_FOR		125
+#define ST_SWITCH	126
+#define ST_COMP		127
+#define ST_BREAK	128
+#define ST_CONTINUE	129
+#define ST_CASE		130
+#define ST_DEFAULT	131
+#define ST_RETURN	132
+#define ST_GOTO		133
+#define ST_ASM		134
+#define ST_LABEL	135
+#define ST_OP		136
+#define ST_COMMENT	137
 
 #define IS_STATEMENT(i) (i==INLINE||(ST_DECL<=i&&i<=ST_COMMENT))
 
-#define HAS_ADDRESS	132
+#define HAS_ADDRESS	138
 
 /* statement end */