scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit d53555e55c3592a1331b3c3d2a3b0fbced35fcf5
parent 4d8c8842d85ac73f572a5cd009df21d042f052a4
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri,  8 Sep 2017 18:04:59 +0200

[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.

Diffstat:
Mas/as.h | 2++
Mas/emit.c | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------
2 files changed, 58 insertions(+), 10 deletions(-)

diff --git a/as/as.h b/as/as.h @@ -42,11 +42,13 @@ struct section { 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; diff --git 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 @@ isec(Section *sec) 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 @@ void 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");