commit 104a93bf817eb53ba8431209961c56fd945668be
parent b1748e736d4535cf4e81f196059b6ded96b599a1
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Tue, 19 Sep 2017 01:09:26 +0200
[as] Restructure the distribution of the functions
Diffstat:
4 files changed, 47 insertions(+), 41 deletions(-)
diff --git a/as/as.h b/as/as.h
@@ -88,22 +88,28 @@ struct symbol {
struct symbol *next;
};
+/* emit.c */
+extern char *pack(TUINT v, int n, int inc);
extern void isections(void);
extern void writeout(char *name);
extern void emit(Section *sec, char *bytes, int nbytes);
extern Section *section(char *name);
-extern void incpc(int siz);
-extern char *pack(TUINT v, int n, int inc);
-extern void error(char *msg, ...);
-extern Arg *getargs(char *s);
+
+/* main.c */
extern Symbol *lookup(char *name);
extern Symbol *deflabel(char *name);
+/* parser.c */
+extern Arg *getargs(char *s);
+extern void error(char *msg, ...);
/* Avoid errors in files where stdio is not included */
#ifdef stdin
-extern int next(FILE *fp, struct line *linep);
+extern int nextline(FILE *fp, struct line *linep);
#endif
+/*
+ * Definition of global variables
+ */
extern Section *cursec;
extern int nr_ins;
extern Ins instab[];
diff --git a/as/emit.c b/as/emit.c
@@ -126,6 +126,31 @@ pack(TUINT v, int n, int inc)
}
static void
+incpc(int siz)
+{
+ TUINT pc, curpc;
+ pc = cursec->pc;
+ curpc = cursec->curpc;
+
+ cursec->curpc += siz;
+ cursec->pc += siz;
+
+ if (pass == 2)
+ return;
+
+ if (cursec->pc > cursec->max)
+ cursec->max = cursec->pc;
+
+ if (pc > cursec->pc ||
+ curpc > cursec->curpc ||
+ cursec->curpc > maxaddr ||
+ cursec->pc > maxaddr) {
+ die("address overflow");
+ }
+}
+
+
+static void
isect(Section *sec)
{
TUINT siz;
diff --git a/as/main.c b/as/main.c
@@ -8,44 +8,20 @@ static char sccsid[] = "@(#) ./as/main.c";
#include "as.h"
int
-cmp(const void *f1, const void *f2)
-{
- const Ins *ins = f2;
-
- return strcmp(f1, ins->str);
-}
-
-int
match(Op *op, Arg *args)
{
return 1;
}
-void
-incpc(int siz)
+static int
+cmp(const void *f1, const void *f2)
{
- TUINT pc, curpc;
- pc = cursec->pc;
- curpc = cursec->curpc;
-
- cursec->curpc += siz;
- cursec->pc += siz;
-
- if (pass == 2)
- return;
-
- if (cursec->pc > cursec->max)
- cursec->max = cursec->pc;
+ const Ins *ins = f2;
- if (pc > cursec->pc ||
- curpc > cursec->curpc ||
- cursec->curpc > maxaddr ||
- cursec->pc > maxaddr) {
- die("address overflow");
- }
+ return strcmp(f1, ins->str);
}
-void
+static void
as(char *text, char *xargs)
{
Ins *ins;
@@ -53,7 +29,6 @@ as(char *text, char *xargs)
Arg *args;
ins = bsearch(text, instab, nr_ins, sizeof(Ins), cmp);
-
if (!ins) {
error("invalid instruction");
return;
@@ -72,7 +47,7 @@ as(char *text, char *xargs)
(*op->format)(op, args);
}
-int
+static int
dopass(char *fname)
{
struct line line;
@@ -83,7 +58,7 @@ dopass(char *fname)
die("as: error opening '%s'", fname);
isections();
- while (next(fp, &line)) {
+ while (nextline(fp, &line)) {
linesym = NULL;
if (line.label)
@@ -114,7 +89,7 @@ main(int argc, char *argv[])
usage();
filename = argv[1];
- for (pass = 1; pass <= 2 && dopass(argv[1]); pass++)
+ for (pass = 1; pass <= 2 && dopass(filename); pass++)
/* nothing */;
writeout("a.out");
diff --git a/as/parser.c b/as/parser.c
@@ -32,7 +32,7 @@ error(char *msg, ...)
die("as: too many errors");
}
-Arg
+static Arg
number(char *s, int base)
{
Arg arg;
@@ -97,7 +97,7 @@ field(char **oldp)
for (s = begin; ; s++) {
switch (c = *s) {
case '\t':
- *s++ = '\0';
+ *s = '\0';
*oldp = s;
goto out_loop;
case ';':
@@ -148,7 +148,7 @@ extract(char *s, struct line *lp)
}
int
-next(FILE *fp, struct line *lp)
+nextline(FILE *fp, struct line *lp)
{
size_t n;
static char buff[MAXLINE];