shithub: scc

Download patch

ref: 4534415354538af9795b3387baac108ead851aab
parent: 0e0241ec76a8e8c87d092e9ea592d856bb010275
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Thu Nov 23 03:17:37 EST 2017

[as] Add String type

This type is needed because we will need offsets
in the strings stored in the string table of the
object file.

--- a/as/as.h
+++ b/as/as.h
@@ -54,8 +54,14 @@
 typedef struct section Section;
 typedef struct symbol Symbol;
 typedef struct node Node;
+typedef struct string String;
 typedef void Format(Op *, Node **);
 
+struct string {
+	char *buf;
+	size_t offset;
+};
+
 struct line {
 	char *label;
 	char *op;
@@ -76,7 +82,7 @@
 };
 
 struct section {
-	char *name;
+	String name;
 	char *mem;
 	unsigned char flags;
 	TUINT base;
@@ -87,7 +93,7 @@
 };
 
 struct symbol {
-	char *name;
+	String name;
 	unsigned char flags;
 	char pass;
 	char argtype;
@@ -113,6 +119,7 @@
 extern void killtmp(void);
 extern int toobig(Node *np, int type);
 extern void dumpstab(char *msg);
+extern String newstring(char *s);
 
 /* main.c */
 extern Symbol *lookup(char *name, int type);
--- a/as/symbol.c
+++ b/as/symbol.c
@@ -12,24 +12,24 @@
 #define NALLOC  10
 
 static Section abss = {
-	.name  = "abs",
+	.name  = (String) {"abs"},
 	.flags = TABS|SREAD|SWRITE,
 };
 
 static Section bss = {
-	.name  = "bss",
+	.name  = (String) {"bss"},
 	.flags = TBSS|SRELOC|SREAD|SWRITE,
 	.next  = &abss,
 };
 
 static Section data = {
-	.name  = "data",
+	.name  = (String) {"data"},
 	.flags = TDATA|SRELOC|SREAD|SWRITE|SFILE,
 	.next  = &bss,
 };
 
 static Section text = {
-	.name  = "text",
+	.name  = (String) {"text"},
 	.flags = TTEXT|SRELOC|SFILE,
 	.next  = &data,
 };
@@ -57,7 +57,7 @@
 		fprintf(stderr, "[%d]", (int) (bp - hashtbl));
 		for (sym = *bp; sym; sym = sym->next) {
 			fprintf(stderr, " -> %s:%0X:%0X",
-			       sym->name, sym->flags, sym->argtype);
+			       sym->name.buf, sym->flags, sym->argtype);
 		}
 		putc('\n', stderr);
 	}
@@ -80,7 +80,7 @@
 	c = toupper(*name);
 	list = &hashtbl[h];
 	for (sym = *list; sym; sym = sym->next) {
-		t = sym->name;
+		t = sym->name.buf;
 		if (c != toupper(*t) || casecmp(t, name))
 			continue;
 		symtype = sym->flags & TMASK;
@@ -90,7 +90,7 @@
 	}
 
 	sym = xmalloc(sizeof(*sym));
-	sym->name = xstrdup(name);
+	sym->name = newstring(name);
 	sym->flags = FLOCAL | FUNDEF | type;
 	sym->desc = 0;
 	sym->value = 0;
@@ -116,10 +116,10 @@
 		}
 		r = snprintf(label, sizeof(label),
 		             "%s%s",
-		             cursym->name, name);
+		             cursym->name.buf, name);
 		if (r == sizeof(label)) {
 			error("local label '%s' in '%s' produces too long symbol",
-			      name, cursym->name);
+			      name, cursym->name.buf);
 			return NULL;
 		}
 		name = label;
@@ -190,12 +190,12 @@
 	Section *sec;
 
 	for (sec = seclist; sec; sec = sec->next) {
-		if (!strcmp(sec->name, name))
+		if (!strcmp(sec->name.buf, name))
 			break;
 	}
 	if (!sec) {
 		sec = xmalloc(sizeof(*sec));
-		sec->name = xstrdup(name);
+		sec->name = newstring(name);
 		sec->base = sec->max = sec->pc = sec->curpc = 0;
 		sec->next = seclist;
 		sec->flags = SRELOC|SREAD|SWRITE|SFILE;
@@ -241,4 +241,16 @@
 		return;
 	dealloc(tmpalloc);
 	tmpalloc = NULL;
+}
+
+String
+newstring(char *s)
+{
+	size_t len = strlen(s) + 1;
+	String str;
+
+	str.offset = 0;
+	str.buf = xmalloc(len);
+	memcpy(str.buf, s, len);
+	return str;
 }