commit 45d8d43dbefc75ce4b5d979a190071ea78527924
parent f1afc70c36f562fdb3cb9ae39a80e01c3dac1566
Author: Nihal Jere <nihal@nihaljere.xyz>
Date: Fri, 28 Jan 2022 19:18:04 -0600
cleanup, especially externs
Diffstat:
12 files changed, 34 insertions(+), 50 deletions(-)
diff --git a/blockstack.h b/blockstack.h
@@ -1,3 +1,6 @@
void blockpush(const struct block *const block);
struct block *blockpop();
struct block *blockpeek();
+
+extern const struct block *blockstack[BLOCKSTACKSIZE];
+extern size_t blocki;
diff --git a/ir.c b/ir.c
@@ -12,11 +12,7 @@
#include "ir.h"
#include "util.h"
#include "blockstack.h"
-
-extern struct types types;
-extern struct exprs exprs;
-extern struct assgns assgns;
-extern struct toplevel toplevel;
+#include "target.h"
#define STARTINS(op, val, valtype) putins((out), (op), (val), (valtype)) ; curi++ ;
#define LABEL(l) out->labels.data[l] = reali; STARTINS(IR_LABEL, l, VT_LABEL);
@@ -25,14 +21,9 @@ extern struct toplevel toplevel;
#define PTRSIZE 8
-extern struct target targ;
-
-static uint64_t tmpi, labeli, curi, reali, rblocki;
-static struct temp interval;
+static uint64_t tmpi, labeli, curi, reali, rblocki, out_index;
static uint16_t regs; // used register bitfield
-uint64_t out_index;
-
static uint8_t
regalloc()
{
@@ -61,7 +52,7 @@ static uint64_t
procindex(const struct slice *const s)
{
for (size_t i = 0; i < toplevel.code.len; i++) {
- struct iproc *iproc = &toplevel.code.data[i];
+ const struct iproc *const iproc = &toplevel.code.data[i];
if (slice_cmp(s, &iproc->s) == 0)
return i;
}
@@ -75,7 +66,7 @@ putins(struct iproc *const out, const int op, const uint64_t val, const int valt
{
assert(op);
assert(valtype);
- struct instr ins = {
+ const struct instr ins = {
.val = val,
.op = op,
.valtype = valtype
@@ -480,7 +471,10 @@ genproc(struct decl *const decl, const struct proc *const proc)
struct iproc *out = &iproc; // for macros to work, a bit hacky
// put a blank interval, since tmpi starts at 1
- array_add((&iproc.temps), interval);
+ {
+ struct temp temp = { 0 };
+ array_add((&iproc.temps), temp);
+ }
array_add((&iproc.labels), labeli);
size_t startlabel = bumplabel(out), endlabel = bumplabel(out);
diff --git a/ir.h b/ir.h
@@ -97,9 +97,3 @@ struct toplevel {
};
size_t genproc(struct decl *const decl, const struct proc *const proc);
-
-struct target {
- uint32_t reserved;
- size_t (*emitsyscall)(struct data *const text, const uint8_t paramcount);
- size_t (*emitproc)(struct data *const text, const struct iproc *const proc);
-};
diff --git a/main.c b/main.c
@@ -19,22 +19,13 @@
#include "type.h"
#include "map.h"
#include "blockstack.h"
+#include "target.h"
-extern const char *const tokenstr[];
-
-extern struct block *blockstack[BLOCKSTACKSIZE];
-extern size_t blocki;
-
-struct map *typesmap;
struct assgns assgns;
struct exprs exprs;
-extern struct types types;
-
-extern const struct target x64_target;
struct target targ;
-
struct toplevel toplevel;
-
+struct map *typesmap;
char *infile;
uint64_t
diff --git a/nooc.h b/nooc.h
@@ -251,3 +251,12 @@ struct exprs {
size_t len;
struct expr *data;
};
+
+extern const char *const tokenstr[];
+extern struct assgns assgns;
+extern struct exprs exprs;
+extern struct target targ;
+extern struct toplevel toplevel;
+extern struct map *typesmap;
+extern char *infile;
+extern struct types types;
diff --git a/parse.c b/parse.c
@@ -12,16 +12,6 @@
#include "map.h"
#include "blockstack.h"
-extern struct block *blockstack[BLOCKSTACKSIZE];
-extern size_t blocki;
-
-extern const char *const tokenstr[];
-
-extern struct assgns assgns;
-extern struct exprs exprs;
-extern struct types types;
-extern struct map *typesmap;
-
const struct token *tok;
static void parsenametypes(struct nametypes *const nametypes);
@@ -427,7 +417,6 @@ parseblock(struct block *const block)
expect(TOK_EQUAL);
tok = tok->next;
- // FIXME: scoping
if (finddecl(decl.s)) {
error(tok->line, tok->col, "repeat declaration!");
}
diff --git a/target.h b/target.h
@@ -0,0 +1,7 @@
+struct target {
+ uint32_t reserved;
+ size_t (*emitsyscall)(struct data *const text, const uint8_t paramcount);
+ size_t (*emitproc)(struct data *const text, const struct iproc *const proc);
+};
+
+extern const struct target x64_target;
diff --git a/type.c b/type.c
@@ -16,9 +16,6 @@
// hashtable based on cproc's map.c
struct types types;
-extern struct map *typesmap;
-extern struct assgns assgns;
-extern struct exprs exprs;
static struct typetable {
size_t cap, count;
diff --git a/type.h b/type.h
@@ -3,3 +3,5 @@ const size_t type_put(const struct type *const type);
void inittypes();
const size_t typeref(const size_t typei);
void typecheck(const struct block *const block);
+
+extern struct types types;
diff --git a/util.c b/util.c
@@ -10,9 +10,6 @@
#include "array.h"
#include "util.h"
-extern char *infile;
-extern struct exprs exprs;
-
const char *const tokenstr[] = {
[TOK_NONE] = "TOK_NONE",
[TOK_NAME] = "TOK_NAME",
diff --git a/util.h b/util.h
@@ -10,3 +10,5 @@ void die(const char *const error);
void *xmalloc(size_t size);
void *xrealloc(void *, size_t);
void *xcalloc(size_t, size_t);
+
+extern const char *const tokenstr[];
diff --git a/x64.c b/x64.c
@@ -8,6 +8,7 @@
#include "ir.h"
#include "util.h"
#include "array.h"
+#include "target.h"
enum reg {
RAX,
@@ -47,8 +48,6 @@ enum mod {
char abi_arg[] = {RAX, RDI, RSI, RDX, R10, R8, R9};
unsigned short used_reg;
-extern struct toplevel toplevel;
-
static size_t
add_r64_imm(struct data *const text, const enum reg dest, const uint64_t imm)
{