commit 25f7bc06e3c7ac93e1b2b06a9828c24471a5e7d2
parent bf3e6753f3ee28c33aa16dd9b0a65636e612eac1
Author: Quentin Carbonneaux <quentin.carbonneaux@yale.edu>
Date: Thu, 8 Oct 2015 14:49:00 -0400
emit alignment directives properly
Diffstat:
3 files changed, 27 insertions(+), 20 deletions(-)
diff --git a/lisc/emit.c b/lisc/emit.c
@@ -315,7 +315,6 @@ emitfn(Fn *fn, FILE *f)
int *r, c, fs;
fprintf(f,
- "\n"
".text\n"
".globl %s\n"
".type %s, @function\n"
@@ -371,34 +370,34 @@ emitfn(Fn *fn, FILE *f)
void
emitdat(Dat *d, FILE *f)
{
+ static char *dtoa[] = {
+ [DAlign] = ".align",
+ [DB] = "\t.byte",
+ [DH] = "\t.value",
+ [DW] = "\t.long",
+ [DL] = "\t.quad"
+ };
+
switch (d->type) {
+ case DStart:
+ fprintf(f, ".data\n");
+ break;
+ case DEnd:
+ fprintf(f, "\n");
+ break;
case DName:
fprintf(f,
- "\n"
- ".data\n"
".globl %s\n"
".type %s, @object\n"
"%s:\n",
d->u.str, d->u.str, d->u.str
);
break;
- case DAlign:
- fprintf(f, ".align %"PRId64"\n", d->u.num);
- break;
case DA:
fprintf(f, "\t.string \"%s\"\n", d->u.str);
break;
- case DB:
- fprintf(f, "\t.byte %"PRId64"\n", d->u.num);
- break;
- case DH:
- fprintf(f, "\t.value %"PRId64"\n", d->u.num);
- break;
- case DW:
- fprintf(f, "\t.long %"PRId64"\n", d->u.num);
- break;
- case DL:
- fprintf(f, "\t.quad %"PRId64"\n", d->u.num);
+ default:
+ fprintf(f, "%s %"PRId64"\n", dtoa[d->type], d->u.num);
break;
}
}
diff --git a/lisc/lisc.h b/lisc/lisc.h
@@ -266,6 +266,8 @@ struct Typ {
struct Dat {
enum {
+ DStart,
+ DEnd,
DName,
DAlign,
DA,
diff --git a/lisc/parse.c b/lisc/parse.c
@@ -728,14 +728,15 @@ parsetyp()
static void
parsedat(void cb(Dat *))
{
+ char s[NString];
int t;
Dat d;
+ d.type = DStart;
+ cb(&d);
if (nextnl() != TGlo || nextnl() != TEq)
err("data name, then = expected");
- d.type = DName;
- d.u.str = tokval.str;
- cb(&d);
+ strcpy(s, tokval.str);
t = nextnl();
if (t == TAlign) {
if (nextnl() != TNum)
@@ -745,6 +746,9 @@ parsedat(void cb(Dat *))
cb(&d);
t = nextnl();
}
+ d.type = DName;
+ d.u.str = s;
+ cb(&d);
if (t == TStr) {
d.type = DA;
d.u.str = tokval.str;
@@ -770,6 +774,8 @@ parsedat(void cb(Dat *))
if (t != TComma)
err(", or } expected");
}
+ d.type = DEnd;
+ cb(&d);
}
Fn *