shithub: scc

Download patch

ref: 048182fd990731f3ca1bc6fd9c551bae62e9eda0
parent: bdcd33e06eb729c36cf21ec559556ba6ddaad6a0
author: Roberto E. Vargas Caballero <[email protected]>
date: Fri Sep 8 14:04:59 EDT 2017

[as] Add support for multi sections

This is a very basic support, where we only printout all the
content of the different sections in sequential order.

--- a/as/as.h
+++ b/as/as.h
@@ -42,11 +42,13 @@
 	TUINT max;
 	TUINT curpc;
 	TUINT pc;
+	struct section *next;
 };
 
 extern void isections(void);
 extern void writeout(char *name);
 extern void emit(Section *sec, char *bytes, int nbytes);
+extern Section *section(char *name);
 
 extern Section *cursec;
 extern int nr_ins;
--- a/as/emit.c
+++ b/as/emit.c
@@ -6,20 +6,34 @@
 #include "../inc/scc.h"
 #include "as.h"
 
-Section text = (Section) {.name = "text", .flags = SRELOC|SEXEC|SFILE};
-Section data = (Section) {.name = "data", .flags = SRELOC|SREAD|SWRITE|SFILE};
-Section bss = (Section) {.name = "bss", .flags = SRELOC|SREAD|SWRITE};
-Section *cursec = &text;
+static Section bss = {
+	.name = "bss",
+	.flags = SRELOC|SREAD|SWRITE
+};
 
+static Section data = {
+	.name = "data",
+	.next = &bss,
+	.flags = SRELOC|SREAD|SWRITE|SFILE
+};
+
+static Section text = {
+	.name = "text",
+	.next = &data,
+	.flags = SRELOC|SEXEC|SFILE
+};
+
+Section *cursec = &text, *headp = &text;
+
 int pass;
 
 static void
-isec(Section *sec)
+isect(Section *sec)
 {
 	TUINT siz;
 
 	sec->curpc = sec->pc = sec->base;
-	if (pass == 1 || sec->flags & SFILE)
+	if (pass == 1 || !(sec->flags & SFILE))
 		return;
 
 	siz = sec->max - sec->base;
@@ -28,12 +42,39 @@
 	sec->mem = xmalloc(sec->max - sec->base);
 }
 
+Section *
+addsect(char *name, TUINT base, int flags)
+{
+	Section *sec;
+
+}
+
+Section *
+section(char *name)
+{
+	Section *sec;
+
+	for (sec = headp; sec; sec = sec->next) {
+		if (!strcmp(sec->name, name))
+			break;
+	}
+	if (!sec) {
+		sec = xmalloc(sizeof(*sec));
+		sec->name = xstrdup(name);
+		sec->base = sec->max = sec->pc = sec->curpc = 0;
+		sec->next = headp;
+		sec->flags = SRELOC|SREAD|SWRITE|SFILE;
+	}
+	cursec = sec;
+}
+
 void
 isections(void)
 {
-	isec(&text);
-	isec(&data);
-	isec(&bss);
+	Section *sec;
+
+	for (sec = headp; sec; sec = sec->next)
+		isect(sec);
 }
 
 void
@@ -52,11 +93,16 @@
 writeout(char *name)
 {
 	FILE *fp;
+	Section *secp;
 
 	if ((fp = fopen(name, "wb")) == NULL)
 		die("error opening output file '%s'\n", name);
 
-	fwrite(text.mem, text.max - text.base, 1, fp);
+	for (secp = headp; secp; secp = secp->next) {
+		if (!secp->mem)
+			continue;
+		fwrite(secp->mem, secp->max - secp->base, 1, fp);
+	}
 
 	if (fclose(fp))
 		die("error writing the output file");