summaryrefslogtreecommitdiff
path: root/lisc
diff options
context:
space:
mode:
authorQuentin Carbonneaux <quentin.carbonneaux@yale.edu>2015-12-08 10:36:22 -0500
committerQuentin Carbonneaux <quentin.carbonneaux@yale.edu>2015-12-08 10:39:06 -0500
commit0791dd026e3c7e5c49282fe264a075dbcecb0607 (patch)
treed3f6ac83a4f5ff3f8b94c3b36b072cc33a76f751 /lisc
parent0298b6c4ca6e9434bf0df559ceee9b730002026a (diff)
downloadroux-0791dd026e3c7e5c49282fe264a075dbcecb0607.tar.gz
sanitize constants representation
Diffstat (limited to 'lisc')
-rw-r--r--lisc/lisc.h9
-rw-r--r--lisc/parse.c32
-rw-r--r--lisc/util.c6
3 files changed, 25 insertions, 22 deletions
diff --git a/lisc/lisc.h b/lisc/lisc.h
index 1d1f027..f03958d 100644
--- a/lisc/lisc.h
+++ b/lisc/lisc.h
@@ -312,12 +312,15 @@ struct Tmp {
struct Con {
enum {
CUndef,
- CNum,
+ CBits,
CAddr,
} type;
- char flt;
char label[NString];
- int64_t val;
+ union {
+ int64_t i;
+ double f;
+ } bits;
+ char flt;
};
typedef struct Addr Addr;
diff --git a/lisc/parse.c b/lisc/parse.c
index ab5a643..ad48a60 100644
--- a/lisc/parse.c
+++ b/lisc/parse.c
@@ -325,23 +325,26 @@ parseref()
Con c;
int i;
+ memset(&c, 0, sizeof c);
switch (next()) {
case TTmp:
return tmpref(tokval.str);
case TInt:
- c = (Con){.type = CNum, .val = tokval.num};
+ c.type = CBits;
+ c.bits.i = tokval.num;
goto Look;
case TFlt:
- c = (Con){.type = CNum, .flt = 1};
- memcpy(&c.val, &tokval.flt, sizeof c.val);
+ c.type = CBits;
+ c.bits.f = tokval.flt;
+ c.flt = 1;
goto Look;
case TGlo:
- c = (Con){.type = CAddr, .val = 0};
+ c.type = CAddr;
strcpy(c.label, tokval.str);
Look:
for (i=0; i<ncon; i++)
if (con[i].type == c.type
- && con[i].val == c.val
+ && con[i].bits.i == c.bits.i
&& strcmp(con[i].label, c.label) == 0)
return CON(i);
vgrow(&con, ++ncon);
@@ -606,7 +609,7 @@ parsefn()
curi = insb;
tmp = vnew(ntmp, sizeof tmp[0]);
con = vnew(ncon, sizeof con[0]);
- con[0].type = CNum;
+ con[0].type = CBits;
fn = alloc(sizeof *fn);
blink = &fn->start;
for (i=0; i<NBlk; i++)
@@ -829,22 +832,19 @@ parse(FILE *f, void data(Dat *), void func(Fn *))
static void
printcon(Con *c, FILE *f)
{
- double d;
-
switch (c->type) {
case CUndef:
break;
case CAddr:
fprintf(f, "$%s", c->label);
- if (c->val)
- fprintf(f, "%+"PRIi64, c->val);
+ if (c->bits.i)
+ fprintf(f, "%+"PRIi64, c->bits.i);
break;
- case CNum:
- if (c->flt) {
- memcpy(&d, &c->val, sizeof d);
- fprintf(f, "`%lf", d);
- } else
- fprintf(f, "%"PRIi64, c->val);
+ case CBits:
+ if (c->flt)
+ fprintf(f, "`%lf", c->bits.f);
+ else
+ fprintf(f, "%"PRIi64, c->bits.i);
break;
}
}
diff --git a/lisc/util.c b/lisc/util.c
index 0af81f9..4bbb4bd 100644
--- a/lisc/util.c
+++ b/lisc/util.c
@@ -209,11 +209,11 @@ getcon(int64_t val, Fn *fn)
int c;
for (c=0; c<fn->ncon; c++)
- if (fn->con[c].type == CNum && fn->con[c].val == val)
+ if (fn->con[c].type == CBits && fn->con[c].bits.i == val)
return CON(c);
fn->ncon++;
vgrow(&fn->con, fn->ncon);
- fn->con[c] = (Con){.type = CNum, .val = val};
+ fn->con[c] = (Con){.type = CBits, .bits.i = val};
return CON(c);
}
@@ -229,6 +229,6 @@ addcon(Con *c0, Con *c1)
c0->type = CAddr;
strcpy(c0->label, c1->label);
}
- c0->val += c1->val;
+ c0->bits.i += c1->bits.i;
}
}