2007-02-25 12:42:04 +00:00
|
|
|
/*
|
|
|
|
(c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
|
|
|
See the copyright notice in the ACK home directory, in the file "Copyright".
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
2018-09-10 20:42:30 +00:00
|
|
|
/*
|
2007-02-25 12:42:04 +00:00
|
|
|
#define CODE_GENERATOR for code generator
|
|
|
|
#define CODE_EXPANDER for code expander
|
|
|
|
|
|
|
|
#define IEEEFLOAT for machines using IEEE floating point format
|
|
|
|
#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.
|
|
|
|
|
|
|
|
Unfortunately, the IEEE standard does not define the byte-order.
|
|
|
|
depends on the #defines
|
2018-09-10 20:42:30 +00:00
|
|
|
FL_MSL_AT_LOW_ADDRESS 1 if most significant long is at low address
|
|
|
|
FL_MSW_AT_LOW_ADDRESS 1 if most significant word is at low address
|
|
|
|
FL_MSB_AT_LOW_ADDRESS 1 if most significant byte is at low address
|
2007-02-25 12:42:04 +00:00
|
|
|
*/
|
|
|
|
#ifdef IEEEFLOAT
|
|
|
|
#define USE_FLT
|
|
|
|
#endif
|
|
|
|
#ifdef PDPFLOAT
|
|
|
|
#define USE_FLT
|
|
|
|
#undef FL_MSL_AT_LOW_ADDRESS
|
|
|
|
#define FL_MSL_AT_LOW_ADDRESS 1
|
|
|
|
#undef FL_MSW_AT_LOW_ADDRESS
|
|
|
|
#define FL_MSW_AT_LOW_ADDRESS 1
|
|
|
|
#undef FL_MSB_AT_LOW_ADDRESS
|
|
|
|
#define FL_MSB_AT_LOW_ADDRESS 0
|
|
|
|
#endif
|
|
|
|
|
2018-09-10 20:42:30 +00:00
|
|
|
#define I0 \
|
|
|
|
((FL_MSL_AT_LOW_ADDRESS ? 0 : 4) + (FL_MSW_AT_LOW_ADDRESS ? 0 : 2) \
|
|
|
|
+ (FL_MSB_AT_LOW_ADDRESS ? 0 : 1))
|
|
|
|
#define I1 \
|
|
|
|
((FL_MSL_AT_LOW_ADDRESS ? 0 : 4) + (FL_MSW_AT_LOW_ADDRESS ? 0 : 2) \
|
|
|
|
+ (FL_MSB_AT_LOW_ADDRESS ? 1 : 0))
|
|
|
|
#define I2 \
|
|
|
|
((FL_MSL_AT_LOW_ADDRESS ? 0 : 4) + (FL_MSW_AT_LOW_ADDRESS ? 2 : 0) \
|
|
|
|
+ (FL_MSB_AT_LOW_ADDRESS ? 0 : 1))
|
|
|
|
#define I3 \
|
|
|
|
((FL_MSL_AT_LOW_ADDRESS ? 0 : 4) + (FL_MSW_AT_LOW_ADDRESS ? 2 : 0) \
|
|
|
|
+ (FL_MSB_AT_LOW_ADDRESS ? 1 : 0))
|
|
|
|
#define I4 \
|
|
|
|
((FL_MSL_AT_LOW_ADDRESS ? 4 : 0) + (FL_MSW_AT_LOW_ADDRESS ? 0 : 2) \
|
|
|
|
+ (FL_MSB_AT_LOW_ADDRESS ? 0 : 1))
|
|
|
|
#define I5 \
|
|
|
|
((FL_MSL_AT_LOW_ADDRESS ? 4 : 0) + (FL_MSW_AT_LOW_ADDRESS ? 0 : 2) \
|
|
|
|
+ (FL_MSB_AT_LOW_ADDRESS ? 1 : 0))
|
|
|
|
#define I6 \
|
|
|
|
((FL_MSL_AT_LOW_ADDRESS ? 4 : 0) + (FL_MSW_AT_LOW_ADDRESS ? 2 : 0) \
|
|
|
|
+ (FL_MSB_AT_LOW_ADDRESS ? 0 : 1))
|
|
|
|
#define I7 \
|
|
|
|
((FL_MSL_AT_LOW_ADDRESS ? 4 : 0) + (FL_MSW_AT_LOW_ADDRESS ? 2 : 0) \
|
|
|
|
+ (FL_MSB_AT_LOW_ADDRESS ? 1 : 0))
|
2007-02-25 12:42:04 +00:00
|
|
|
|
|
|
|
#ifndef USE_FLT
|
2018-09-10 20:42:30 +00:00
|
|
|
static int float_cst(str, sz, buf) char *str, *buf;
|
|
|
|
int sz;
|
2007-02-25 12:42:04 +00:00
|
|
|
{
|
|
|
|
int i;
|
2018-09-10 20:42:30 +00:00
|
|
|
char* p;
|
2007-02-25 12:42:04 +00:00
|
|
|
float fl;
|
|
|
|
double f;
|
|
|
|
double atof();
|
|
|
|
|
2018-09-10 20:42:30 +00:00
|
|
|
if (sz != 4 && sz != 8)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
f = atof(str);
|
2018-09-10 20:42:30 +00:00
|
|
|
if (sz == 4)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
fl = f;
|
2018-09-10 20:42:30 +00:00
|
|
|
p = (char*)&fl;
|
2007-02-25 12:42:04 +00:00
|
|
|
}
|
2018-09-10 20:42:30 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
p = (char*)&f;
|
2007-02-25 12:42:04 +00:00
|
|
|
}
|
2018-09-10 20:42:30 +00:00
|
|
|
for (i = sz; i; i--)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
*buf++ = *p++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else /* USE_FLT */
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <flt_arith.h>
|
|
|
|
|
Cut down some clang warnings
Edit C code to reduce warnings from clang. Most warnings are for
implicit declarations of functions, but some warnings want me to add
parentheses or curly braces, or to cast arguments for printf().
Make a few other changes, like declaring float_cst() in h/con_float to
be static, and using C99 bool in ego/ra/makeitems.c and
ego/share/makecldef.c. Such changes don't silence warnings; I make
such changes while I silence warnings in the same file. In
float_cst(), rename parameter `str` to `float_str`, so it doesn't
share a name with the global variable `str`.
Remove `const` from `newmodule(const char *)` in mach/proto/as to
silence a warning. I wrongly added the `const` in d347207.
For warnings about implicit declarations of functions, the fix is to
declare the function before calling it. For example, my OpenBSD
system needs <sys/wait.h> to declare wait().
In util/int, add "whatever.h" to declare more functions. Remove old
declarations from "mem.h", to prefer the newer declarations of the
same functions in "data.h" and "stack.h".
2019-10-23 20:06:36 +00:00
|
|
|
static int float_cst(const char *float_str, int sz, char *buf)
|
2007-02-25 12:42:04 +00:00
|
|
|
{
|
|
|
|
int overflow = 0;
|
|
|
|
flt_arith e;
|
|
|
|
|
2018-09-10 20:42:30 +00:00
|
|
|
if (sz != 4 && sz != 8)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
return 1;
|
|
|
|
}
|
Cut down some clang warnings
Edit C code to reduce warnings from clang. Most warnings are for
implicit declarations of functions, but some warnings want me to add
parentheses or curly braces, or to cast arguments for printf().
Make a few other changes, like declaring float_cst() in h/con_float to
be static, and using C99 bool in ego/ra/makeitems.c and
ego/share/makecldef.c. Such changes don't silence warnings; I make
such changes while I silence warnings in the same file. In
float_cst(), rename parameter `str` to `float_str`, so it doesn't
share a name with the global variable `str`.
Remove `const` from `newmodule(const char *)` in mach/proto/as to
silence a warning. I wrongly added the `const` in d347207.
For warnings about implicit declarations of functions, the fix is to
declare the function before calling it. For example, my OpenBSD
system needs <sys/wait.h> to declare wait().
In util/int, add "whatever.h" to declare more functions. Remove old
declarations from "mem.h", to prefer the newer declarations of the
same functions in "data.h" and "stack.h".
2019-10-23 20:06:36 +00:00
|
|
|
flt_str2flt(float_str, &e);
|
2007-02-25 12:42:04 +00:00
|
|
|
#ifdef IEEEFLOAT
|
2018-09-10 20:42:30 +00:00
|
|
|
if (sz == 4)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
#endif
|
|
|
|
#ifdef PDPFLOAT
|
|
|
|
e.flt_exp += 129;
|
|
|
|
#else
|
Cut down some clang warnings
Edit C code to reduce warnings from clang. Most warnings are for
implicit declarations of functions, but some warnings want me to add
parentheses or curly braces, or to cast arguments for printf().
Make a few other changes, like declaring float_cst() in h/con_float to
be static, and using C99 bool in ego/ra/makeitems.c and
ego/share/makecldef.c. Such changes don't silence warnings; I make
such changes while I silence warnings in the same file. In
float_cst(), rename parameter `str` to `float_str`, so it doesn't
share a name with the global variable `str`.
Remove `const` from `newmodule(const char *)` in mach/proto/as to
silence a warning. I wrongly added the `const` in d347207.
For warnings about implicit declarations of functions, the fix is to
declare the function before calling it. For example, my OpenBSD
system needs <sys/wait.h> to declare wait().
In util/int, add "whatever.h" to declare more functions. Remove old
declarations from "mem.h", to prefer the newer declarations of the
same functions in "data.h" and "stack.h".
2019-10-23 20:06:36 +00:00
|
|
|
e.flt_exp += 127;
|
2007-02-25 12:42:04 +00:00
|
|
|
#endif
|
2018-09-10 20:42:30 +00:00
|
|
|
if (e.flt_mantissa.flt_h_32 == 0)
|
|
|
|
e.flt_exp = 0;
|
2007-02-25 12:42:04 +00:00
|
|
|
#ifdef IEEEFLOAT
|
2018-09-10 20:42:30 +00:00
|
|
|
if (e.flt_mantissa.flt_h_32 & 0x80)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
/* rounding */
|
2018-09-10 20:42:30 +00:00
|
|
|
if ((e.flt_mantissa.flt_h_32 & 0xffffff00) == 0xffffff00)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
e.flt_exp++;
|
|
|
|
e.flt_mantissa.flt_h_32 = 0x80000000;
|
|
|
|
}
|
2018-09-10 20:42:30 +00:00
|
|
|
else
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
e.flt_mantissa.flt_h_32 += 0x80;
|
|
|
|
}
|
|
|
|
}
|
2018-09-10 20:42:30 +00:00
|
|
|
if (e.flt_exp >= 255)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
overflow = 1;
|
|
|
|
e.flt_exp = 255;
|
|
|
|
e.flt_mantissa.flt_h_32 = e.flt_mantissa.flt_l_32 = 0;
|
|
|
|
}
|
2018-09-10 20:42:30 +00:00
|
|
|
if (e.flt_exp <= 0)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
flt_b64_sft(&(e.flt_mantissa), 1);
|
2018-09-10 20:42:30 +00:00
|
|
|
if (e.flt_exp < 0)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
flt_b64_sft(&(e.flt_mantissa), -e.flt_exp);
|
|
|
|
e.flt_exp = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifndef IEEEFLOAT
|
2018-09-10 20:42:30 +00:00
|
|
|
if (sz == 4 && (e.flt_mantissa.flt_h_32 & 0x80))
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
/* rounding */
|
2018-09-10 20:42:30 +00:00
|
|
|
if ((e.flt_mantissa.flt_h_32 & 0xffffff00) == 0xffffff00)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
e.flt_exp++;
|
|
|
|
e.flt_mantissa.flt_h_32 = 0x80000000;
|
|
|
|
}
|
2018-09-10 20:42:30 +00:00
|
|
|
else
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
e.flt_mantissa.flt_h_32 += 0x80;
|
|
|
|
}
|
|
|
|
}
|
2018-09-10 20:42:30 +00:00
|
|
|
if (sz == 8 && (e.flt_mantissa.flt_l_32 & 0x80))
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
/* rounding */
|
2018-09-10 20:42:30 +00:00
|
|
|
if ((e.flt_mantissa.flt_l_32 & 0xffffff00) == 0xffffff00)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
e.flt_mantissa.flt_l_32 = 0;
|
2018-09-10 20:42:30 +00:00
|
|
|
if (e.flt_mantissa.flt_h_32 == 0xffffffff)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
e.flt_exp++;
|
|
|
|
e.flt_mantissa.flt_h_32 = 0x80000000;
|
|
|
|
}
|
2018-09-10 20:42:30 +00:00
|
|
|
else
|
|
|
|
e.flt_mantissa.flt_h_32++;
|
2007-02-25 12:42:04 +00:00
|
|
|
}
|
2018-09-10 20:42:30 +00:00
|
|
|
else
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
e.flt_mantissa.flt_l_32 += 0x80;
|
|
|
|
}
|
|
|
|
}
|
2018-09-10 20:42:30 +00:00
|
|
|
if (e.flt_exp > 255)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
overflow = 1;
|
|
|
|
e.flt_exp = 255;
|
|
|
|
e.flt_mantissa.flt_h_32 = e.flt_mantissa.flt_l_32 = 0xffffffff;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
buf[I0] = (e.flt_sign << 7) | (e.flt_exp >> 1);
|
2018-09-10 20:42:30 +00:00
|
|
|
buf[I1] = ((e.flt_exp & 1) << 7) | ((e.flt_mantissa.flt_h_32 & 0x7fffffff) >> 24);
|
2007-02-25 12:42:04 +00:00
|
|
|
buf[I2] = e.flt_mantissa.flt_h_32 >> 16;
|
|
|
|
buf[I3] = e.flt_mantissa.flt_h_32 >> 8;
|
|
|
|
#ifndef IEEEFLOAT
|
2018-09-10 20:42:30 +00:00
|
|
|
if (sz == 8)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
buf[I4] = e.flt_mantissa.flt_h_32;
|
|
|
|
buf[I5] = e.flt_mantissa.flt_l_32 >> 24;
|
|
|
|
buf[I6] = e.flt_mantissa.flt_l_32 >> 16;
|
|
|
|
buf[I7] = e.flt_mantissa.flt_l_32 >> 8;
|
|
|
|
flt_b64_sft(&(e.flt_mantissa), -56);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
flt_b64_sft(&(e.flt_mantissa), -24);
|
|
|
|
#ifdef IEEEFLOAT
|
|
|
|
}
|
2018-09-10 20:42:30 +00:00
|
|
|
else
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
e.flt_exp += 1023;
|
2018-09-10 20:42:30 +00:00
|
|
|
if (e.flt_mantissa.flt_h_32 == 0)
|
|
|
|
e.flt_exp = 0;
|
|
|
|
if (e.flt_mantissa.flt_l_32 & 0x400)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
/* rounding */
|
2018-09-10 20:42:30 +00:00
|
|
|
if ((e.flt_mantissa.flt_l_32 & 0xfffff800) == 0xfffff800)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
e.flt_mantissa.flt_l_32 = 0;
|
2018-09-10 20:42:30 +00:00
|
|
|
if (e.flt_mantissa.flt_h_32 == 0xffffffff)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
e.flt_exp++;
|
|
|
|
e.flt_mantissa.flt_h_32 = 0x80000000;
|
|
|
|
}
|
2018-09-10 20:42:30 +00:00
|
|
|
else
|
|
|
|
e.flt_mantissa.flt_h_32++;
|
2007-02-25 12:42:04 +00:00
|
|
|
}
|
2018-09-10 20:42:30 +00:00
|
|
|
else
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
e.flt_mantissa.flt_l_32 += 0x400;
|
|
|
|
}
|
|
|
|
}
|
2018-09-10 20:42:30 +00:00
|
|
|
if (e.flt_exp >= 2047)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
overflow = 1;
|
|
|
|
e.flt_exp = 2047;
|
|
|
|
e.flt_mantissa.flt_h_32 = e.flt_mantissa.flt_l_32 = 0;
|
|
|
|
}
|
2018-09-10 20:42:30 +00:00
|
|
|
if (e.flt_exp <= 0)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
flt_b64_sft(&(e.flt_mantissa), 1);
|
2018-09-10 20:42:30 +00:00
|
|
|
if (e.flt_exp < 0)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
flt_b64_sft(&(e.flt_mantissa), -e.flt_exp);
|
|
|
|
e.flt_exp = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf[I0] = (e.flt_sign << 7) | (e.flt_exp >> 4);
|
2018-09-10 20:42:30 +00:00
|
|
|
buf[I1] = ((e.flt_exp & 017) << 4) | ((e.flt_mantissa.flt_h_32 >> 27) & 017);
|
2007-02-25 12:42:04 +00:00
|
|
|
buf[I2] = e.flt_mantissa.flt_h_32 >> 19;
|
|
|
|
buf[I3] = e.flt_mantissa.flt_h_32 >> 11;
|
|
|
|
buf[I4] = e.flt_mantissa.flt_h_32 >> 3;
|
|
|
|
buf[I5] = (e.flt_mantissa.flt_h_32 << 5) | ((e.flt_mantissa.flt_l_32 >> 27) & 037);
|
|
|
|
buf[I6] = e.flt_mantissa.flt_l_32 >> 19;
|
|
|
|
buf[I7] = e.flt_mantissa.flt_l_32 >> 11;
|
|
|
|
flt_b64_sft(&(e.flt_mantissa), -53);
|
|
|
|
}
|
|
|
|
#endif
|
2018-09-10 20:42:30 +00:00
|
|
|
#if !FL_MSL_AT_LOW_ADDRESS
|
|
|
|
if (sz == 4)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
buf[I4] = buf[I0];
|
|
|
|
buf[I5] = buf[I1];
|
|
|
|
buf[I6] = buf[I2];
|
|
|
|
buf[I7] = buf[I3];
|
|
|
|
}
|
|
|
|
#endif
|
2018-09-10 20:42:30 +00:00
|
|
|
if (overflow)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* USE_FLT */
|
|
|
|
|
|
|
|
#ifdef CODE_GENERATOR
|
2018-09-10 20:55:05 +00:00
|
|
|
void con_float(void)
|
2007-02-25 12:42:04 +00:00
|
|
|
{
|
|
|
|
char buf[8];
|
|
|
|
int rval = float_cst(str, (int)argval, buf);
|
|
|
|
int i;
|
|
|
|
|
2018-09-10 20:42:30 +00:00
|
|
|
if (rval == 1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "float constant size = %d\n", (int)argval);
|
2007-02-25 12:42:04 +00:00
|
|
|
fatal("bad fcon size");
|
|
|
|
}
|
2018-09-10 20:42:30 +00:00
|
|
|
fprintf(codefile, "!float %s sz %d\n", str, (int)argval);
|
|
|
|
if (rval == 2)
|
|
|
|
{
|
2022-08-01 20:08:13 +00:00
|
|
|
/* If this is enabled, it causes huge amounts of build system spam, so
|
|
|
|
* I've silenced it. dtrg 2022-08-01 */
|
|
|
|
|
|
|
|
/* fprintf(stderr, "Warning: overflow in floating point constant %s\n", str);*/
|
2007-02-25 12:42:04 +00:00
|
|
|
}
|
|
|
|
fprintf(codefile, ".data1 0%o", buf[0] & 0377);
|
2018-09-10 20:42:30 +00:00
|
|
|
for (i = 1; i < (int)argval; i++)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
fprintf(codefile, ",0%o", buf[i] & 0377);
|
|
|
|
}
|
|
|
|
putc('\n', codefile);
|
|
|
|
}
|
|
|
|
#endif /* CODE_GENERATOR */
|
|
|
|
|
|
|
|
#ifdef CODE_EXPANDER
|
2018-09-10 20:55:05 +00:00
|
|
|
void con_float(const char* str, arith argval)
|
2007-02-25 12:42:04 +00:00
|
|
|
{
|
|
|
|
char buf[8];
|
|
|
|
int rval = float_cst(str, (int)argval, buf);
|
|
|
|
int i;
|
|
|
|
|
2018-09-10 20:42:30 +00:00
|
|
|
if (rval == 1)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
argval = 8;
|
|
|
|
rval = float_cst(str, 8, buf);
|
|
|
|
}
|
2018-09-10 20:42:30 +00:00
|
|
|
for (i = 0; i < (int)argval; i++)
|
|
|
|
{
|
2007-02-25 12:42:04 +00:00
|
|
|
gen1(buf[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CODE_EXPANDER */
|