shithub: scc

Download patch

ref: 8dd361f8446d298a2e1d6d925da63c81143ddb4a
parent: 57c4da4b743642fc37c6705e0b7cf470e958c328
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon Sep 18 21:09:26 EDT 2017

[as] Restructure the distribution of the functions

--- a/as/as.h
+++ b/as/as.h
@@ -88,22 +88,28 @@
 	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[];
--- a/as/emit.c
+++ b/as/emit.c
@@ -126,6 +126,31 @@
 }
 
 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;
--- a/as/main.c
+++ b/as/main.c
@@ -8,44 +8,20 @@
 #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;
+	const Ins *ins = f2;
 
-	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");
-	}
+	return strcmp(f1, ins->str);
 }
 
-void
+static void
 as(char *text, char *xargs)
 {
 	Ins *ins;
@@ -53,7 +29,6 @@
 	Arg *args;
 	
 	ins = bsearch(text, instab, nr_ins, sizeof(Ins), cmp);
-
 	if (!ins) {
 		error("invalid instruction");
 		return;
@@ -72,7 +47,7 @@
 	(*op->format)(op, args);
 }
 
-int
+static int
 dopass(char *fname)
 {
 	struct line line;
@@ -83,7 +58,7 @@
 		die("as: error opening '%s'", fname);
 
 	isections();
-	while (next(fp, &line)) {
+	while (nextline(fp, &line)) {
 		linesym = NULL;
 
 		if (line.label)
@@ -114,7 +89,7 @@
 		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");
 
--- a/as/parser.c
+++ b/as/parser.c
@@ -32,7 +32,7 @@
 		die("as: too many errors");
 }
 
-Arg
+static Arg
 number(char *s, int base)
 {
 	Arg arg;
@@ -97,7 +97,7 @@
 	for (s = begin; ; s++) {
 		switch (c = *s) {
 		case '\t':
-			*s++ = '\0';
+			*s = '\0';
 			*oldp = s;
 			goto out_loop;
 		case ';':
@@ -148,7 +148,7 @@
 }
 
 int
-next(FILE *fp, struct line *lp)
+nextline(FILE *fp, struct line *lp)
 {
 	size_t n;
 	static char buff[MAXLINE];