ref: e49dedd1f69232439b3823ec52334baf33734efe
parent: e53dfc6f0afefa8191033a3a467a67846060c6e9
author: Roberto E. Vargas Caballero <[email protected]>
date: Sat Sep 9 17:09:37 EDT 2017
[as] Add symbol type
--- a/as/as.h
+++ b/as/as.h
@@ -5,6 +5,14 @@
SFILE,
};
+enum symflags {
+ FUNDEF = 'U',
+ FABS = 'A',
+ FCOMMON = 'C',
+ FBSS = 'B',
+ FDATA = 'D',
+};
+
enum args {
AIMM = 1,
AMAX,
@@ -21,6 +29,7 @@
typedef struct arg Arg;
typedef void Format(Op *, Arg *);
typedef struct section Section;
+typedef struct symbol Symbol;
struct line {
char *label;
@@ -57,6 +66,14 @@
struct section *next;
};
+struct symbol {
+ char *name;
+ char type;
+ short desc;
+ TUINT value;
+ struct symbol *next;
+};
+
extern void isections(void);
extern void writeout(char *name);
extern void emit(Section *sec, char *bytes, int nbytes);
@@ -65,6 +82,7 @@
extern char *pack(TUINT v, int n, int inc);
extern void error(char *msg, ...);
extern Arg *getargs(char *s);
+extern Symbol *lookup(char *name);
/* Avoid errors in files where stdio is not included */
#ifdef stdin
--- a/as/emit.c
+++ b/as/emit.c
@@ -6,6 +6,8 @@
#include "../inc/scc.h"
#include "as.h"
+#define HASHSIZ 64
+
static Section abss = {
.name = "abs",
.flags = SREAD|SWRITE
@@ -32,6 +34,39 @@
Section *cursec = &text, *headp = &text;
int pass;
+
+static Symbol *hashtbl[HASHSIZ];
+
+Symbol *
+lookup(char *name)
+{
+ unsigned h;
+ Symbol *sym;
+ int c;
+ char *t, *s;
+
+ s = name;
+ for (h = 0; c = *s; ++s)
+ h = h*33 ^ c;
+ h &= HASHSIZ-1;
+
+ c = *name;
+ for (sym = hashtbl[h]; sym; sym = sym->next) {
+ t = sym->name;
+ if (c == *t && !strcmp(t, name))
+ return sym;
+ }
+
+ sym = xmalloc(sizeof(sym));
+ sym->name = xstrdup(name);
+ sym->type = FUNDEF;
+ sym->desc = 0;
+ sym->value = 0;
+ sym->next = hashtbl[h];
+ hashtbl[h] = sym;
+
+ return sym;
+}
char *
pack(TUINT v, int n, int inc)