ref: 4ed832ac9312fdfae3dec4fceb3eb657b2687567
parent: a75727743999e6a738b4ecae6d1364ae02853e05
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri Sep 22 04:43:16 EDT 2017
[as] Remove Arg type We only need an array of Node* because this is what the expression evaluator is returning.
--- a/as/as.h
+++ b/as/as.h
@@ -39,11 +39,10 @@
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 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 *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
--- a/as/ins.c
+++ b/as/ins.c
@@ -4,47 +4,49 @@
#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;
}
--- a/as/main.c
+++ b/as/main.c
@@ -8,7 +8,7 @@
#include "as.h"
int
-match(Op *op, Arg *args)
+match(Op *op, Node **args)
{
return 1;
}
@@ -26,8 +26,8 @@
{
Ins *ins;
Op *op, *lim;
- Arg *args;
-
+ Node **args;
+
ins = bsearch(text, instab, nr_ins, sizeof(Ins), cmp);
if (!ins) {
error("invalid instruction");
--- a/as/parser.c
+++ b/as/parser.c
@@ -32,55 +32,33 @@
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;
}