much improved version
authorceriel <none@none>
Tue, 1 Aug 1989 16:40:28 +0000 (16:40 +0000)
committerceriel <none@none>
Tue, 1 Aug 1989 16:40:28 +0000 (16:40 +0000)
mach/con_float

index f86c855..a686ad2 100644 (file)
@@ -5,93 +5,89 @@
 
 /* $Header$ */
 
-/* #define NOFLOAT for machines without floating point
-   #define ACKFLOAT for machines using the ACK floating point software
+/* 
+   #define CODE_GENERATOR for code generator
+   #define CODE_EXPANDER for code expander
+
    #define IEEEFLOAT for machines using IEEE floating point format
-   #define OWNFLOAT to use floating point format of machine on which
-                   the code generator runs
-   If none of these are defined, PDP-11 format is generated.
+   #define PDPFLOAT for machines using the PDP-11 floating point format
+   If none of these are defined, the format of the machine on which the
+   code generator runs is used.
+   Returns 1 if sz has an illegal value, 2 in case of overflow,
+   and 0 if all went well.
+   If neither IEEEFLOAT nor PDPFLOAT are defined, the return value is not
+   trustworthy.
 */
+#ifdef IEEEFLOAT
+#define USE_FLT
+#endif
+#ifdef PDPFLOAT
+#define USE_FLT
+#endif
 
-#ifdef NOFLOAT
-con_float() {
-
-static int been_here;
-       if (argval != 4 && argval != 8)
-               fatal("bad fcon size");
-       fputs(".data4\t", codefile);
-       if (argval == 8)
-               fputs("0,", codefile);
-       fputs("0 !dummy float\n", codefile);
-       if ( !been_here++) {
-               fputs("Warning : dummy float-constant(s)\n", stderr);
-       }
-}
-#else /* NOFLOAT */
-
-#include <ctype.h>
-#ifndef OWNFLOAT
-#include <flt_arith.h>
-#endif /* OWNFLOAT */
-
-con_float() {
-       int i, j;
+#ifndef USE_FLT
+static int
+float_cst(str, sz, buf)
+       char *str, *buf;
+       int sz;
+{
+       int i;
        char *p;
-#ifdef OWNFLOAT
        float fl;
        double f;
        double atof();
-#else /* OWNFLOAT */
-       int overflow = 0;
-       flt_arith e;
-       char buf[8];
-#endif /* OWNFLOAT */
 
-       if (argval!= 4 && argval!= 8)   {
-               fprintf(stderr,"float constant size = %d\n",argval);
-               fatal("bad fcon size");
+       if (sz!= 4 && sz!= 8)   {
+               return 1;
        }
-       fprintf(codefile,"!float %s sz %d\n", str, argval);
-#ifdef OWNFLOAT
        f = atof(str);
-       if (argval == 4) {
+       if (sz == 4) {
                fl = f;
                p = (char *) &fl;
        }
        else {
                p = (char *) &f;
        }
-       fprintf(codefile, ".data1 0%o", *p++ & 0377);
-       for (i = argval-1; i; i--) {
-               fprintf(codefile,",0%o", *p++ & 0377);
+       for (i = sz; i; i--) {
+               *buf++ = *p++;
+       }
+       return 0;
+}
+#else /* USE_FLT */
+
+#include <ctype.h>
+#include <flt_arith.h>
+
+int
+float_cst(str, sz, buf)
+       char *str, *buf;
+       int sz;
+{
+       int i;
+       int overflow = 0;
+       flt_arith e;
+
+       if (sz!= 4 && sz!= 8)   {
+               return 1;
        }
-#else /* OWNFLOAT */
        flt_str2flt(str, &e);
-#if IEEEFLOAT+ACKFLOAT
-       if (argval == 4) {
+#ifdef IEEEFLOAT
+       if (sz == 4) {
 #endif
                e.flt_exp += 127;
-#ifndef IEEEFLOAT
-               e.flt_exp += 2;
-#endif
                if (e.flt_mantissa.flt_h_32 == 0) e.flt_exp = 0;
-#if IEEEFLOAT+ACKFLOAT
+#ifdef IEEEFLOAT
                if (e.flt_exp >= 255) {
                        overflow = 1;
                        e.flt_exp = 255;
                        e.flt_mantissa.flt_h_32 = e.flt_mantissa.flt_l_32 = 0;
                }
                if (e.flt_exp <= 0) {
-#if IEEEFLOAT
                        flt_b64_sft(&(e.flt_mantissa), 1);
                        if (e.flt_exp < 0) {
                                flt_b64_sft(&(e.flt_mantissa), -e.flt_exp);
                                e.flt_exp = 0;
                        }
-#else
-                       e.flt_exp = 0;
-                       e.flt_mantissa.flt_h_32 = e.flt_mantissa.flt_l_32 = 0;
-#endif
                }
 #endif
                buf[0] = (e.flt_sign << 7) | (e.flt_exp >> 1);
@@ -99,21 +95,21 @@ con_float() {
                                ((e.flt_mantissa.flt_h_32 & 0x7fffffff) >> 24);
                buf[2] = e.flt_mantissa.flt_h_32 >> 16;
                buf[3] = e.flt_mantissa.flt_h_32 >> 8;
-               if (argval == 8) {
+#ifndef IEEEFLOAT
+               if (sz == 8) {
                        buf[4] = e.flt_mantissa.flt_h_32;
                        buf[5] = e.flt_mantissa.flt_l_32 >> 24;
                        buf[6] = e.flt_mantissa.flt_l_32 >> 16;
                        buf[7] = e.flt_mantissa.flt_l_32 >> 8;
                        flt_b64_sft(&(e.flt_mantissa), -56);
                }
-               else    flt_b64_sft(&(e.flt_mantissa), -24);
-#if IEEEFLOAT+ACKFLOAT
+               else
+#endif
+                       flt_b64_sft(&(e.flt_mantissa), -24);
+#ifdef IEEEFLOAT
        }
        else {
                e.flt_exp += 1023;
-#ifndef IEEEFLOAT
-               e.flt_exp += 2;
-#endif
                if (e.flt_mantissa.flt_h_32 == 0) e.flt_exp = 0;
                if (e.flt_exp >= 2047) {
                        overflow = 1;
@@ -121,16 +117,11 @@ con_float() {
                        e.flt_mantissa.flt_h_32 = e.flt_mantissa.flt_l_32 = 0;
                }
                if (e.flt_exp <= 0) {
-#if IEEEFLOAT
                        flt_b64_sft(&(e.flt_mantissa), 1);
                        if (e.flt_exp < 0) {
                                flt_b64_sft(&(e.flt_mantissa), -e.flt_exp);
                                e.flt_exp = 0;
                        }
-#else
-                       e.flt_exp = 0;
-                       e.flt_mantissa.flt_h_32 = e.flt_mantissa.flt_l_32 = 0;
-#endif
                }
                buf[0] = (e.flt_sign << 7) | (e.flt_exp >> 4);
                buf[1] = ((e.flt_exp & 017)<< 4) | ((e.flt_mantissa.flt_h_32 >> 27) & 017);
@@ -143,7 +134,7 @@ con_float() {
                flt_b64_sft(&(e.flt_mantissa), -53);
        }
 #endif
-       i = argval-1;
+       i = sz-1;
        if (! overflow && (e.flt_mantissa.flt_h_32 & 0x80000000)) for (; i>=0; i--) {
                if ((buf[i] &0377) != 0377) {
                        buf[i]++;
@@ -152,14 +143,49 @@ con_float() {
                else buf[i] = 0;
        }
        if (overflow || i < 0) {
+               return 2;
+       }
+       return 0;
+}
+#endif /* USE_FLT */
+
+#ifdef CODE_GENERATOR
+con_float()
+{
+       char buf[8];
+       int rval = float_cst(str, argval, buf);
+       int i;
+
+       if (rval == 1) {
+               fprintf(stderr,"float constant size = %d\n",argval);
+               fatal("bad fcon size");
+       }
+       fprintf(codefile,"!float %s sz %d\n", str, argval);
+       if (rval == 2) {
                fprintf(stderr, "Warning: overflow in floating point constant %s\n", str);
        }
-       for (i = 0; i < argval; i++) {
-               fprintf(codefile,
-                       i != 0 ? ",0%o" : ".data1 0%o", 
-                       buf[i]&0377);
+       fprintf(codefile, ".data1 0%o", buf[0] & 0377);
+       for (i = 1; i < argval; i++) {
+               fprintf(codefile, ",0%o", buf[i] & 0377);
        }
-#endif /* OWNFLOAT */
        putc('\n', codefile);
 }
-#endif /* NOFLOAT */
+#endif /* CODE_GENERATOR */
+
+#ifdef CODE_EXPANDER
+con_float(str, argval)
+       char *str;
+{
+       char buf[8];
+       int rval = float_cst(str, argval, buf);
+       int i;
+
+       if (rval == 1) {
+               argval = 8;
+               rval = float_cst(str, argval, buf);
+       }
+       for (i = 0; i < argval; i++) {
+               gen1(buf[i]);
+       }
+}
+#endif /* CODE_EXPANDER */