commit 9097ba1b790c0387f2d6fc6d3a99ec2b98f97679
parent 605e945e14e16e7a8d9855a8fc49749a48a0c6db
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Fri, 22 Sep 2017 08:43:16 +0200
[as] Remove Arg type
We only need an array of Node* because this is what the expression
evaluator is returning.
Diffstat:
4 files changed, 29 insertions(+), 55 deletions(-)
diff --git a/as/as.h b/as/as.h
@@ -39,11 +39,10 @@ enum endianess {
typedef struct ins Ins;
typedef struct op Op;
-typedef struct arg Arg;
-typedef void Format(Op *, Arg *);
typedef struct section Section;
typedef struct symbol Symbol;
typedef struct node Node;
+typedef void Format(Op *, Node **);
struct line {
char *label;
@@ -59,16 +58,11 @@ struct ins {
struct op {
unsigned char flags;
char size;
- void (*format)(Op *, Arg *);
+ void (*format)(Op *, Node **);
char *bytes;
unsigned char *args;
};
-struct arg {
- unsigned char type;
- TUINT val;
-};
-
struct section {
char *name;
char *mem;
@@ -110,7 +104,7 @@ extern Symbol *lookup(char *name);
extern Symbol *deflabel(char *name);
/* parser.c */
-extern Arg *getargs(char *s);
+extern Node **getargs(char *s);
extern void error(char *msg, ...);
/* Avoid errors in files where stdio is not included */
#ifdef stdin
diff --git a/as/ins.c b/as/ins.c
@@ -4,47 +4,49 @@ static char sccsid[] = "@(#) ./as/ins.c";
#include "as.h"
void
-direct(Op *op, Arg *args)
+direct(Op *op, Node **args)
{
emit(cursec, op->bytes, op->size);
}
void
-def(Arg *args, int siz)
+def(Node **args, int siz)
{
- for ( ; args->type; ++args)
- emit(cursec, pack(args->val, siz, endian), siz);
+ Node *np;
+
+ for ( ; np = *args; ++args)
+ emit(cursec, pack(np->sym->value, siz, endian), siz);
}
void
-defb(Op *op, Arg *args)
+defb(Op *op, Node **args)
{
def(args, 1);
}
void
-defw(Op *op, Arg *args)
+defw(Op *op, Node **args)
{
def(args, 2);
}
void
-defd(Op *op, Arg *args)
+defd(Op *op, Node **args)
{
def(args, 4);
}
void
-defq(Op *op, Arg *args)
+defq(Op *op, Node **args)
{
def(args, 8);
}
void
-equ(Op *op, Arg *args)
+equ(Op *op, Node **args)
{
if (!linesym)
error("label definition lacks a label");
else
- linesym->value = args->val;
+ linesym->value = (*args)->sym->value;
}
diff --git a/as/main.c b/as/main.c
@@ -8,7 +8,7 @@ static char sccsid[] = "@(#) ./as/main.c";
#include "as.h"
int
-match(Op *op, Arg *args)
+match(Op *op, Node **args)
{
return 1;
}
@@ -26,8 +26,8 @@ as(char *text, char *xargs)
{
Ins *ins;
Op *op, *lim;
- Arg *args;
-
+ Node **args;
+
ins = bsearch(text, instab, nr_ins, sizeof(Ins), cmp);
if (!ins) {
error("invalid instruction");
diff --git a/as/parser.c b/as/parser.c
@@ -32,55 +32,33 @@ error(char *msg, ...)
die("as: too many errors");
}
-static Arg
-number(char *s, int base)
-{
- Arg arg;
- TUINT n;
-
- /* TODO: Check overflow here */
- arg.type = AIMM;
- for (n = 0; *s; n += *s++ - '0')
- n *= base;
- arg.val = n;
-
- return arg;
-}
-
-Arg *
+Node **
getargs(char *s)
{
char *t;
int ch, len;
- Arg *ap;
- static Arg args[NARGS];
+ Node **ap;
+ static Node *args[NARGS];
- for (ap = args; s; ++ap) {
+ if (!s)
+ return NULL;
+
+ for (ap = args; ; *ap++ = expr(t)) {
while (isspace(*s))
++s;
if (*s == '\0')
break;
if (ap == &args[NARGS-1])
- die("too many arguments in one instruction");
+ error("too many arguments in one instruction");
for (t = s; *s && *s != ','; s++)
/* nothing */;
+ *s++ = '\0';
len = t - s;
if (len == 0)
- goto wrong_operand;
-
- if (*s)
- *s++ = '\0';
-
- ch = *t;
- if (isdigit(ch)) {
- *ap = number(t, (s[len-1] == 'H') ? 16 : 10);
- continue;
- }
-wrong_operand:
- error("wrong operand '%s'", t);
+ error("wrong operand '%s'", t);
}
- ap->type = 0;
+ *ap = NULL;
return args;
}