From 1803dedae88457520f6c8edbb42f2508b9c30c0e Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Mon, 9 Jun 2008 17:32:43 -0700 Subject: [PATCH] Move all the SAA code out of nasmlib Move all the SAA code out of nasmlib; it's not used by anything than nasm itself. Cleaning out the kitchen sink known as nasmlib is a good thing, too. --- Makefile.in | 19 +-- Mkfiles/msvc.mak | 19 +-- Mkfiles/netware.mak | 19 +-- Mkfiles/openwcom.mak | 19 +-- Mkfiles/owlinux.mak | 19 +-- lib/vsnprintf.c | 2 - nasm.c | 1 + nasmlib.c | 305 ----------------------------------- nasmlib.h | 48 +----- output/outaout.c | 1 + output/outas86.c | 1 + output/outbin.c | 1 + output/outcoff.c | 1 + output/outelf32.c | 2 +- output/outelf64.c | 2 +- output/outmacho.c | 1 + output/outrdf2.c | 1 + saa.c | 369 +++++++++++++++++++++++++++++++++++++++++++ saa.h | 60 +++++++ wsaa.c | 64 -------- wsaa.h | 12 -- 21 files changed, 489 insertions(+), 477 deletions(-) create mode 100644 saa.c create mode 100644 saa.h delete mode 100644 wsaa.c delete mode 100644 wsaa.h diff --git a/Makefile.in b/Makefile.in index 6e510a99..3b22abbe 100644 --- a/Makefile.in +++ b/Makefile.in @@ -58,7 +58,7 @@ X = @EXEEXT@ .1.man: $(NROFF) -man $< > $@ -NASM = nasm.$(O) nasmlib.$(O) wsaa.$(O) \ +NASM = nasm.$(O) nasmlib.$(O) saa.$(O) \ float.$(O) insnsa.$(O) insnsb.$(O) \ assemble.$(O) labels.$(O) hashtbl.$(O) crc64.$(O) parser.$(O) \ outform.$(O) output/outbin.$(O) \ @@ -269,29 +269,29 @@ ndisasm.$(O): ndisasm.c compiler.h config.h disasm.h insns.h insnsi.h nasm.h \ outform.$(O): outform.c compiler.h config.h insnsi.h nasm.h nasmlib.h \ outform.h regs.h version.h output/outaout.$(O): output/outaout.c compiler.h config.h insnsi.h nasm.h \ - nasmlib.h outform.h regs.h stdscan.h version.h + nasmlib.h outform.h regs.h saa.h stdscan.h version.h output/outas86.$(O): output/outas86.c compiler.h config.h insnsi.h nasm.h \ - nasmlib.h outform.h regs.h version.h + nasmlib.h outform.h regs.h saa.h version.h output/outbin.$(O): output/outbin.c compiler.h config.h eval.h insnsi.h \ - labels.h nasm.h nasmlib.h outform.h regs.h stdscan.h version.h + labels.h nasm.h nasmlib.h outform.h regs.h saa.h stdscan.h version.h output/outcoff.$(O): output/outcoff.c compiler.h config.h insnsi.h nasm.h \ - nasmlib.h outform.h regs.h version.h + nasmlib.h outform.h regs.h saa.h version.h output/outdbg.$(O): output/outdbg.c compiler.h config.h insnsi.h nasm.h \ nasmlib.h outform.h regs.h version.h output/outelf32.$(O): output/outelf32.c compiler.h config.h insnsi.h nasm.h \ - nasmlib.h outform.h regs.h stdscan.h version.h wsaa.h + nasmlib.h outform.h regs.h saa.h stdscan.h version.h output/outelf64.$(O): output/outelf64.c compiler.h config.h insnsi.h nasm.h \ - nasmlib.h outform.h regs.h stdscan.h version.h wsaa.h + nasmlib.h outform.h regs.h saa.h stdscan.h version.h output/outieee.$(O): output/outieee.c compiler.h config.h insnsi.h nasm.h \ nasmlib.h outform.h regs.h version.h output/outmacho.$(O): output/outmacho.c compiler.h config.h insnsi.h nasm.h \ - nasmlib.h outform.h regs.h version.h + nasmlib.h outform.h regs.h saa.h version.h output/outobj.$(O): output/outobj.c compiler.h config.h insnsi.h nasm.h \ nasmlib.h outform.h regs.h stdscan.h version.h output/outrdf.$(O): output/outrdf.c compiler.h config.h insnsi.h nasm.h \ nasmlib.h outform.h regs.h version.h output/outrdf2.$(O): output/outrdf2.c compiler.h config.h insnsi.h nasm.h \ - nasmlib.h outform.h rdoff/rdoff.h regs.h version.h + nasmlib.h outform.h rdoff/rdoff.h regs.h saa.h version.h parser.$(O): parser.c compiler.h config.h float.h insns.h insnsi.h nasm.h \ nasmlib.h parser.h regs.h stdscan.h tables.h tokens.h version.h pptok.$(O): pptok.c compiler.h config.h hashtbl.h nasmlib.h pptok.h \ @@ -305,6 +305,7 @@ regflags.$(O): regflags.c compiler.h config.h insnsi.h nasm.h nasmlib.h \ regs.h tables.h version.h regs.$(O): regs.c compiler.h config.h insnsi.h tables.h regvals.$(O): regvals.c compiler.h config.h insnsi.h tables.h +saa.$(O): saa.c compiler.h config.h nasmlib.h saa.h stdscan.$(O): stdscan.c compiler.h config.h insns.h insnsi.h nasm.h \ nasmlib.h quote.h regs.h stdscan.h tokens.h version.h sync.$(O): sync.c compiler.h config.h nasmlib.h sync.h diff --git a/Mkfiles/msvc.mak b/Mkfiles/msvc.mak index beb15ccb..73977806 100644 --- a/Mkfiles/msvc.mak +++ b/Mkfiles/msvc.mak @@ -32,7 +32,7 @@ X = .exe .c.obj: $(CC) /c $(ALL_CFLAGS) /Fo$@ $< -NASM = nasm.$(O) nasmlib.$(O) wsaa.$(O) \ +NASM = nasm.$(O) nasmlib.$(O) saa.$(O) \ float.$(O) insnsa.$(O) insnsb.$(O) \ assemble.$(O) labels.$(O) hashtbl.$(O) crc64.$(O) parser.$(O) \ outform.$(O) output/outbin.$(O) \ @@ -215,29 +215,29 @@ ndisasm.$(O): ndisasm.c compiler.h disasm.h insns.h insnsi.h nasm.h \ outform.$(O): outform.c compiler.h insnsi.h nasm.h nasmlib.h outform.h \ regs.h version.h output/outaout.$(O): output/outaout.c compiler.h insnsi.h nasm.h nasmlib.h \ - outform.h regs.h stdscan.h version.h + outform.h regs.h saa.h stdscan.h version.h output/outas86.$(O): output/outas86.c compiler.h insnsi.h nasm.h nasmlib.h \ - outform.h regs.h version.h + outform.h regs.h saa.h version.h output/outbin.$(O): output/outbin.c compiler.h eval.h insnsi.h labels.h \ - nasm.h nasmlib.h outform.h regs.h stdscan.h version.h + nasm.h nasmlib.h outform.h regs.h saa.h stdscan.h version.h output/outcoff.$(O): output/outcoff.c compiler.h insnsi.h nasm.h nasmlib.h \ - outform.h regs.h version.h + outform.h regs.h saa.h version.h output/outdbg.$(O): output/outdbg.c compiler.h insnsi.h nasm.h nasmlib.h \ outform.h regs.h version.h output/outelf32.$(O): output/outelf32.c compiler.h insnsi.h nasm.h nasmlib.h \ - outform.h regs.h stdscan.h version.h wsaa.h + outform.h regs.h saa.h stdscan.h version.h output/outelf64.$(O): output/outelf64.c compiler.h insnsi.h nasm.h nasmlib.h \ - outform.h regs.h stdscan.h version.h wsaa.h + outform.h regs.h saa.h stdscan.h version.h output/outieee.$(O): output/outieee.c compiler.h insnsi.h nasm.h nasmlib.h \ outform.h regs.h version.h output/outmacho.$(O): output/outmacho.c compiler.h insnsi.h nasm.h nasmlib.h \ - outform.h regs.h version.h + outform.h regs.h saa.h version.h output/outobj.$(O): output/outobj.c compiler.h insnsi.h nasm.h nasmlib.h \ outform.h regs.h stdscan.h version.h output/outrdf.$(O): output/outrdf.c compiler.h insnsi.h nasm.h nasmlib.h \ outform.h regs.h version.h output/outrdf2.$(O): output/outrdf2.c compiler.h insnsi.h nasm.h nasmlib.h \ - outform.h rdoff/rdoff.h regs.h version.h + outform.h rdoff/rdoff.h regs.h saa.h version.h parser.$(O): parser.c compiler.h float.h insns.h insnsi.h nasm.h nasmlib.h \ parser.h regs.h stdscan.h tables.h tokens.h version.h pptok.$(O): pptok.c compiler.h hashtbl.h nasmlib.h pptok.h preproc.h @@ -249,6 +249,7 @@ regflags.$(O): regflags.c compiler.h insnsi.h nasm.h nasmlib.h regs.h \ tables.h version.h regs.$(O): regs.c compiler.h insnsi.h tables.h regvals.$(O): regvals.c compiler.h insnsi.h tables.h +saa.$(O): saa.c compiler.h nasmlib.h saa.h stdscan.$(O): stdscan.c compiler.h insns.h insnsi.h nasm.h nasmlib.h quote.h \ regs.h stdscan.h tokens.h version.h sync.$(O): sync.c compiler.h nasmlib.h sync.h diff --git a/Mkfiles/netware.mak b/Mkfiles/netware.mak index 6de408bc..f2d76b57 100644 --- a/Mkfiles/netware.mak +++ b/Mkfiles/netware.mak @@ -28,7 +28,7 @@ LDFLAGS+=-s O = o -NASM = nasm.$(O) nasmlib.$(O) wsaa.$(O) \ +NASM = nasm.$(O) nasmlib.$(O) saa.$(O) \ float.$(O) insnsa.$(O) insnsb.$(O) \ assemble.$(O) labels.$(O) hashtbl.$(O) crc64.$(O) parser.$(O) \ outform.$(O) output/outbin.$(O) \ @@ -155,29 +155,29 @@ ndisasm.o: ndisasm.c compiler.h config.h disasm.h insns.h insnsi.h nasm.h \ outform.o: outform.c compiler.h config.h insnsi.h nasm.h nasmlib.h outform.h \ regs.h version.h outaout.o: outaout.c compiler.h config.h insnsi.h nasm.h nasmlib.h outform.h \ - regs.h stdscan.h version.h + regs.h saa.h stdscan.h version.h outas86.o: outas86.c compiler.h config.h insnsi.h nasm.h nasmlib.h outform.h \ - regs.h version.h + regs.h saa.h version.h outbin.o: outbin.c compiler.h config.h eval.h insnsi.h labels.h nasm.h \ - nasmlib.h outform.h regs.h stdscan.h version.h + nasmlib.h outform.h regs.h saa.h stdscan.h version.h outcoff.o: outcoff.c compiler.h config.h insnsi.h nasm.h nasmlib.h outform.h \ - regs.h version.h + regs.h saa.h version.h outdbg.o: outdbg.c compiler.h config.h insnsi.h nasm.h nasmlib.h outform.h \ regs.h version.h outelf32.o: outelf32.c compiler.h config.h insnsi.h nasm.h nasmlib.h \ - outform.h regs.h stdscan.h version.h wsaa.h + outform.h regs.h saa.h stdscan.h version.h outelf64.o: outelf64.c compiler.h config.h insnsi.h nasm.h nasmlib.h \ - outform.h regs.h stdscan.h version.h wsaa.h + outform.h regs.h saa.h stdscan.h version.h outieee.o: outieee.c compiler.h config.h insnsi.h nasm.h nasmlib.h outform.h \ regs.h version.h outmacho.o: outmacho.c compiler.h config.h insnsi.h nasm.h nasmlib.h \ - outform.h regs.h version.h + outform.h regs.h saa.h version.h outobj.o: outobj.c compiler.h config.h insnsi.h nasm.h nasmlib.h outform.h \ regs.h stdscan.h version.h outrdf.o: outrdf.c compiler.h config.h insnsi.h nasm.h nasmlib.h outform.h \ regs.h version.h outrdf2.o: outrdf2.c compiler.h config.h insnsi.h nasm.h nasmlib.h outform.h \ - rdoff.h regs.h version.h + rdoff.h regs.h saa.h version.h parser.o: parser.c compiler.h config.h float.h insns.h insnsi.h nasm.h \ nasmlib.h parser.h regs.h stdscan.h tables.h tokens.h version.h pptok.o: pptok.c compiler.h config.h hashtbl.h nasmlib.h pptok.h preproc.h @@ -189,6 +189,7 @@ regflags.o: regflags.c compiler.h config.h insnsi.h nasm.h nasmlib.h regs.h \ tables.h version.h regs.o: regs.c compiler.h config.h insnsi.h tables.h regvals.o: regvals.c compiler.h config.h insnsi.h tables.h +saa.o: saa.c compiler.h config.h nasmlib.h saa.h stdscan.o: stdscan.c compiler.h config.h insns.h insnsi.h nasm.h nasmlib.h \ quote.h regs.h stdscan.h tokens.h version.h sync.o: sync.c compiler.h config.h nasmlib.h sync.h diff --git a/Mkfiles/openwcom.mak b/Mkfiles/openwcom.mak index bf518089..fe2e46d3 100644 --- a/Mkfiles/openwcom.mak +++ b/Mkfiles/openwcom.mak @@ -44,7 +44,7 @@ X = .exe $(CC) -c $(ALL_CFLAGS) -fo=$^@ $[@ # Note: wcl386 is broken if forward slashes are used as path separators. -NASM = nasm.$(O) nasmlib.$(O) wsaa.$(O) & +NASM = nasm.$(O) nasmlib.$(O) saa.$(O) & float.$(O) insnsa.$(O) insnsb.$(O) & assemble.$(O) labels.$(O) hashtbl.$(O) crc64.$(O) parser.$(O) & outform.$(O) output\outbin.$(O) & @@ -244,29 +244,29 @@ ndisasm.$(O): ndisasm.c compiler.h disasm.h insns.h insnsi.h nasm.h & outform.$(O): outform.c compiler.h insnsi.h nasm.h nasmlib.h outform.h & regs.h version.h output\outaout.$(O): output\outaout.c compiler.h insnsi.h nasm.h nasmlib.h & - outform.h regs.h stdscan.h version.h + outform.h regs.h saa.h stdscan.h version.h output\outas86.$(O): output\outas86.c compiler.h insnsi.h nasm.h nasmlib.h & - outform.h regs.h version.h + outform.h regs.h saa.h version.h output\outbin.$(O): output\outbin.c compiler.h eval.h insnsi.h labels.h & - nasm.h nasmlib.h outform.h regs.h stdscan.h version.h + nasm.h nasmlib.h outform.h regs.h saa.h stdscan.h version.h output\outcoff.$(O): output\outcoff.c compiler.h insnsi.h nasm.h nasmlib.h & - outform.h regs.h version.h + outform.h regs.h saa.h version.h output\outdbg.$(O): output\outdbg.c compiler.h insnsi.h nasm.h nasmlib.h & outform.h regs.h version.h output\outelf32.$(O): output\outelf32.c compiler.h insnsi.h nasm.h nasmlib.h & - outform.h regs.h stdscan.h version.h wsaa.h + outform.h regs.h saa.h stdscan.h version.h output\outelf64.$(O): output\outelf64.c compiler.h insnsi.h nasm.h nasmlib.h & - outform.h regs.h stdscan.h version.h wsaa.h + outform.h regs.h saa.h stdscan.h version.h output\outieee.$(O): output\outieee.c compiler.h insnsi.h nasm.h nasmlib.h & outform.h regs.h version.h output\outmacho.$(O): output\outmacho.c compiler.h insnsi.h nasm.h nasmlib.h & - outform.h regs.h version.h + outform.h regs.h saa.h version.h output\outobj.$(O): output\outobj.c compiler.h insnsi.h nasm.h nasmlib.h & outform.h regs.h stdscan.h version.h output\outrdf.$(O): output\outrdf.c compiler.h insnsi.h nasm.h nasmlib.h & outform.h regs.h version.h output\outrdf2.$(O): output\outrdf2.c compiler.h insnsi.h nasm.h nasmlib.h & - outform.h rdoff\rdoff.h regs.h version.h + outform.h rdoff\rdoff.h regs.h saa.h version.h parser.$(O): parser.c compiler.h float.h insns.h insnsi.h nasm.h nasmlib.h & parser.h regs.h stdscan.h tables.h tokens.h version.h pptok.$(O): pptok.c compiler.h hashtbl.h nasmlib.h pptok.h preproc.h @@ -278,6 +278,7 @@ regflags.$(O): regflags.c compiler.h insnsi.h nasm.h nasmlib.h regs.h & tables.h version.h regs.$(O): regs.c compiler.h insnsi.h tables.h regvals.$(O): regvals.c compiler.h insnsi.h tables.h +saa.$(O): saa.c compiler.h nasmlib.h saa.h stdscan.$(O): stdscan.c compiler.h insns.h insnsi.h nasm.h nasmlib.h quote.h & regs.h stdscan.h tokens.h version.h sync.$(O): sync.c compiler.h nasmlib.h sync.h diff --git a/Mkfiles/owlinux.mak b/Mkfiles/owlinux.mak index f1d6f3fd..7045d527 100644 --- a/Mkfiles/owlinux.mak +++ b/Mkfiles/owlinux.mak @@ -55,7 +55,7 @@ X = .exe .c.$(O): $(CC) -c $(ALL_CFLAGS) -fo=$@ $< -NASM = nasm.$(O) nasmlib.$(O) wsaa.$(O) \ +NASM = nasm.$(O) nasmlib.$(O) saa.$(O) \ float.$(O) insnsa.$(O) insnsb.$(O) \ assemble.$(O) labels.$(O) hashtbl.$(O) crc64.$(O) parser.$(O) \ outform.$(O) output/outbin.$(O) \ @@ -254,29 +254,29 @@ ndisasm.$(O): ndisasm.c compiler.h disasm.h insns.h insnsi.h nasm.h \ outform.$(O): outform.c compiler.h insnsi.h nasm.h nasmlib.h outform.h \ regs.h version.h output/outaout.$(O): output/outaout.c compiler.h insnsi.h nasm.h nasmlib.h \ - outform.h regs.h stdscan.h version.h + outform.h regs.h saa.h stdscan.h version.h output/outas86.$(O): output/outas86.c compiler.h insnsi.h nasm.h nasmlib.h \ - outform.h regs.h version.h + outform.h regs.h saa.h version.h output/outbin.$(O): output/outbin.c compiler.h eval.h insnsi.h labels.h \ - nasm.h nasmlib.h outform.h regs.h stdscan.h version.h + nasm.h nasmlib.h outform.h regs.h saa.h stdscan.h version.h output/outcoff.$(O): output/outcoff.c compiler.h insnsi.h nasm.h nasmlib.h \ - outform.h regs.h version.h + outform.h regs.h saa.h version.h output/outdbg.$(O): output/outdbg.c compiler.h insnsi.h nasm.h nasmlib.h \ outform.h regs.h version.h output/outelf32.$(O): output/outelf32.c compiler.h insnsi.h nasm.h nasmlib.h \ - outform.h regs.h stdscan.h version.h wsaa.h + outform.h regs.h saa.h stdscan.h version.h output/outelf64.$(O): output/outelf64.c compiler.h insnsi.h nasm.h nasmlib.h \ - outform.h regs.h stdscan.h version.h wsaa.h + outform.h regs.h saa.h stdscan.h version.h output/outieee.$(O): output/outieee.c compiler.h insnsi.h nasm.h nasmlib.h \ outform.h regs.h version.h output/outmacho.$(O): output/outmacho.c compiler.h insnsi.h nasm.h nasmlib.h \ - outform.h regs.h version.h + outform.h regs.h saa.h version.h output/outobj.$(O): output/outobj.c compiler.h insnsi.h nasm.h nasmlib.h \ outform.h regs.h stdscan.h version.h output/outrdf.$(O): output/outrdf.c compiler.h insnsi.h nasm.h nasmlib.h \ outform.h regs.h version.h output/outrdf2.$(O): output/outrdf2.c compiler.h insnsi.h nasm.h nasmlib.h \ - outform.h rdoff/rdoff.h regs.h version.h + outform.h rdoff/rdoff.h regs.h saa.h version.h parser.$(O): parser.c compiler.h float.h insns.h insnsi.h nasm.h nasmlib.h \ parser.h regs.h stdscan.h tables.h tokens.h version.h pptok.$(O): pptok.c compiler.h hashtbl.h nasmlib.h pptok.h preproc.h @@ -288,6 +288,7 @@ regflags.$(O): regflags.c compiler.h insnsi.h nasm.h nasmlib.h regs.h \ tables.h version.h regs.$(O): regs.c compiler.h insnsi.h tables.h regvals.$(O): regvals.c compiler.h insnsi.h tables.h +saa.$(O): saa.c compiler.h nasmlib.h saa.h stdscan.$(O): stdscan.c compiler.h insns.h insnsi.h nasm.h nasmlib.h quote.h \ regs.h stdscan.h tokens.h version.h sync.$(O): sync.c compiler.h nasmlib.h sync.h diff --git a/lib/vsnprintf.c b/lib/vsnprintf.c index 976b0eac..f5ae2feb 100644 --- a/lib/vsnprintf.c +++ b/lib/vsnprintf.c @@ -14,8 +14,6 @@ #include "nasmlib.h" -extern efunc nasm_malloc_error; - #define BUFFER_SIZE 65536 /* Bigger than any string we might print... */ static char snprintf_buffer[BUFFER_SIZE]; diff --git a/nasm.c b/nasm.c index 1eb5e4f7..31c16fa7 100644 --- a/nasm.c +++ b/nasm.c @@ -19,6 +19,7 @@ #include "nasm.h" #include "nasmlib.h" +#include "saa.h" #include "float.h" #include "stdscan.h" #include "insns.h" diff --git a/nasmlib.c b/nasmlib.c index a4e7915e..cf19024e 100644 --- a/nasmlib.c +++ b/nasmlib.c @@ -562,311 +562,6 @@ struct RAA *raa_write(struct RAA *r, int32_t posn, int64_t value) return result; } -/* Aggregate SAA components smaller than this */ -#define SAA_BLKLEN 65536 - -struct SAA *saa_init(size_t elem_len) -{ - struct SAA *s; - char *data; - - s = nasm_zalloc(sizeof(struct SAA)); - - if (elem_len >= SAA_BLKLEN) - s->blk_len = elem_len; - else - s->blk_len = SAA_BLKLEN - (SAA_BLKLEN % elem_len); - - s->elem_len = elem_len; - s->length = s->blk_len; - data = nasm_malloc(s->blk_len); - s->nblkptrs = s->nblks = 1; - s->blk_ptrs = nasm_malloc(sizeof(char *)); - s->blk_ptrs[0] = data; - s->wblk = s->rblk = &s->blk_ptrs[0]; - - return s; -} - -void saa_free(struct SAA *s) -{ - char **p; - size_t n; - - for (p = s->blk_ptrs, n = s->nblks; n; p++, n--) - nasm_free(*p); - - nasm_free(s->blk_ptrs); - nasm_free(s); -} - -/* Add one allocation block to an SAA */ -static void saa_extend(struct SAA *s) -{ - size_t blkn = s->nblks++; - - if (blkn >= s->nblkptrs) { - size_t rindex = s->rblk - s->blk_ptrs; - size_t windex = s->wblk - s->blk_ptrs; - - s->nblkptrs <<= 1; - s->blk_ptrs = nasm_realloc(s->blk_ptrs, s->nblkptrs*sizeof(char *)); - - s->rblk = s->blk_ptrs + rindex; - s->wblk = s->blk_ptrs + windex; - } - - s->blk_ptrs[blkn] = nasm_malloc(s->blk_len); - s->length += s->blk_len; -} - -void *saa_wstruct(struct SAA *s) -{ - void *p; - - if (s->wpos % s->elem_len) - nasm_malloc_error(ERR_PANIC|ERR_NOFILE, - "misaligned wpos in saa_wstruct"); - - if (s->wpos + s->elem_len > s->blk_len) { - if (s->wpos != s->blk_len) - nasm_malloc_error(ERR_PANIC|ERR_NOFILE, - "unfilled block in saa_wstruct"); - - if (s->wptr + s->elem_len > s->length) - saa_extend(s); - s->wblk++; - s->wpos = 0; - } - - p = *s->wblk + s->wpos; - s->wpos += s->elem_len; - s->wptr += s->elem_len; - - if (s->wptr > s->datalen) - s->datalen = s->wptr; - - return p; -} - -void saa_wbytes(struct SAA *s, const void *data, size_t len) -{ - const char *d = data; - - while (len) { - size_t l = s->blk_len - s->wpos; - if (l > len) - l = len; - if (l) { - if (d) { - memcpy(*s->wblk + s->wpos, d, l); - d += l; - } else - memset(*s->wblk + s->wpos, 0, l); - s->wpos += l; - s->wptr += l; - len -= l; - - if (s->datalen < s->wptr) - s->datalen = s->wptr; - } - if (len) { - if (s->wptr >= s->length) - saa_extend(s); - s->wblk++; - s->wpos = 0; - } - } -} - -/* write unsigned LEB128 value to SAA */ -void saa_wleb128u(struct SAA *psaa, int value) -{ - char temp[64], *ptemp; - uint8_t byte; - int len; - - ptemp = temp; - len = 0; - do - { - byte = value & 127; - value >>= 7; - if (value != 0) /* more bytes to come */ - byte |= 0x80; - *ptemp = byte; - ptemp++; - len++; - } while (value != 0); - saa_wbytes(psaa, temp, len); -} - -/* write signed LEB128 value to SAA */ -void saa_wleb128s(struct SAA *psaa, int value) -{ - char temp[64], *ptemp; - uint8_t byte; - bool more, negative; - int size, len; - - ptemp = temp; - more = 1; - negative = (value < 0); - size = sizeof(int) * 8; - len = 0; - while(more) - { - byte = value & 0x7f; - value >>= 7; - if (negative) - /* sign extend */ - value |= - (1 <<(size - 7)); - /* sign bit of byte is second high order bit (0x40) */ - if ((value == 0 && ! (byte & 0x40)) || - ((value == -1) && (byte & 0x40))) - more = 0; - else - byte |= 0x80; - *ptemp = byte; - ptemp++; - len++; - } - saa_wbytes(psaa, temp, len); -} - -void saa_rewind(struct SAA *s) -{ - s->rblk = s->blk_ptrs; - s->rpos = s->rptr = 0; -} - -void *saa_rstruct(struct SAA *s) -{ - void *p; - - if (s->rptr + s->elem_len > s->datalen) - return NULL; - - if (s->rpos % s->elem_len) - nasm_malloc_error(ERR_PANIC|ERR_NOFILE, - "misaligned rpos in saa_rstruct"); - - if (s->rpos + s->elem_len > s->blk_len) { - s->rblk++; - s->rpos = 0; - } - - p = *s->rblk + s->rpos; - s->rpos += s->elem_len; - s->rptr += s->elem_len; - - return p; -} - -const void *saa_rbytes(struct SAA *s, size_t *lenp) -{ - const void *p; - size_t len; - - if (s->rptr >= s->datalen) { - *lenp = 0; - return NULL; - } - - if (s->rpos >= s->blk_len) { - s->rblk++; - s->rpos = 0; - } - - len = *lenp; - if (len > s->datalen - s->rptr) - len = s->datalen - s->rptr; - if (len > s->blk_len - s->rpos) - len = s->blk_len - s->rpos; - - *lenp = len; - p = *s->rblk + s->rpos; - - s->rpos += len; - s->rptr += len; - - return p; -} - -void saa_rnbytes(struct SAA *s, void *data, size_t len) -{ - char *d = data; - - if (s->rptr + len > s->datalen) { - nasm_malloc_error(ERR_PANIC|ERR_NOFILE, "overrun in saa_rnbytes"); - return; - } - - while (len) { - size_t l; - const void *p; - - l = len; - p = saa_rbytes(s, &l); - - memcpy(d, p, l); - d += l; - len -= l; - } -} - -/* Same as saa_rnbytes, except position the counter first */ -void saa_fread(struct SAA *s, size_t posn, void *data, size_t len) -{ - size_t ix; - - if (posn+len > s->datalen) { - nasm_malloc_error(ERR_PANIC|ERR_NOFILE, "overrun in saa_fread"); - return; - } - - ix = posn / s->blk_len; - s->rptr = posn; - s->rpos = posn % s->blk_len; - s->rblk = &s->blk_ptrs[ix]; - - saa_rnbytes(s, data, len); -} - -/* Same as saa_wbytes, except position the counter first */ -void saa_fwrite(struct SAA *s, size_t posn, const void *data, size_t len) -{ - size_t ix; - - if (posn > s->datalen) { - /* Seek beyond the end of the existing array not supported */ - nasm_malloc_error(ERR_PANIC|ERR_NOFILE, "overrun in saa_fwrite"); - return; - } - - ix = posn / s->blk_len; - s->wptr = posn; - s->wpos = posn % s->blk_len; - s->wblk = &s->blk_ptrs[ix]; - - if (!s->wpos) { - s->wpos = s->blk_len; - s->wblk--; - } - - saa_wbytes(s, data, len); -} - -void saa_fpwrite(struct SAA *s, FILE * fp) -{ - const char *data; - size_t len; - - saa_rewind(s); - while (len = s->datalen, (data = saa_rbytes(s, &len)) != NULL) - fwrite(data, 1, len, fp); -} - /* * Common list of prefix names */ diff --git a/nasmlib.h b/nasmlib.h index 7eb5c47f..3c69a662 100644 --- a/nasmlib.h +++ b/nasmlib.h @@ -37,6 +37,7 @@ * An error reporting function should look like this. */ typedef void (*efunc) (int severity, const char *fmt, ...); +extern efunc nasm_malloc_error; /* * These are the error severity codes which get passed as the first @@ -329,53 +330,6 @@ void raa_free(struct RAA *); int64_t raa_read(struct RAA *, int32_t); struct RAA *raa_write(struct RAA *r, int32_t posn, int64_t value); -/* - * Routines to manage a dynamic sequential-access array, under the - * same restriction on maximum mallocable block. This array may be - * written to in two ways: a contiguous chunk can be reserved of a - * given size with a pointer returned OR single-byte data may be - * written. The array can also be read back in the same two ways: - * as a series of big byte-data blocks or as a list of structures - * of a given size. - */ - -struct SAA { - /* - * members `end' and `elem_len' are only valid in first link in - * list; `rptr' and `rpos' are used for reading - */ - size_t elem_len; /* Size of each element */ - size_t blk_len; /* Size of each allocation block */ - size_t nblks; /* Total number of allocated blocks */ - size_t nblkptrs; /* Total number of allocation block pointers */ - size_t length; /* Total allocated length of the array */ - size_t datalen; /* Total data length of the array */ - char **wblk; /* Write block pointer */ - size_t wpos; /* Write position inside block */ - size_t wptr; /* Absolute write position */ - char **rblk; /* Read block pointer */ - size_t rpos; /* Read position inside block */ - size_t rptr; /* Absolute read position */ - char **blk_ptrs; /* Pointer to pointer blocks */ -}; - -struct SAA *saa_init(size_t elem_len); /* 1 == byte */ -void saa_free(struct SAA *); -void *saa_wstruct(struct SAA *); /* return a structure of elem_len */ -void saa_wbytes(struct SAA *, const void *, size_t); /* write arbitrary bytes */ -void saa_wleb128u(struct SAA *, int); /* write unsigned LEB128 value */ -void saa_wleb128s(struct SAA *, int); /* write signed LEB128 value */ -void saa_rewind(struct SAA *); /* for reading from beginning */ -void *saa_rstruct(struct SAA *); /* return NULL on EOA */ -const void *saa_rbytes(struct SAA *, size_t *); /* return 0 on EOA */ -void saa_rnbytes(struct SAA *, void *, size_t); /* read a given no. of bytes */ -/* random access */ -void saa_fread(struct SAA *, size_t, void *, size_t); -void saa_fwrite(struct SAA *, size_t, const void *, size_t); - -/* dump to file */ -void saa_fpwrite(struct SAA *, FILE *); - /* * Binary search routine. Returns index into `array' of an entry * matching `string', or <0 if no match. `array' is taken to diff --git a/output/outaout.c b/output/outaout.c index 0cfa3d4c..d0f29a64 100644 --- a/output/outaout.c +++ b/output/outaout.c @@ -17,6 +17,7 @@ #include "nasm.h" #include "nasmlib.h" +#include "saa.h" #include "stdscan.h" #include "outform.h" diff --git a/output/outas86.c b/output/outas86.c index 84acc53e..f4d7bed5 100644 --- a/output/outas86.c +++ b/output/outas86.c @@ -17,6 +17,7 @@ #include "nasm.h" #include "nasmlib.h" +#include "saa.h" #include "outform.h" #ifdef OF_AS86 diff --git a/output/outbin.c b/output/outbin.c index 66a0b0b9..d2b57426 100644 --- a/output/outbin.c +++ b/output/outbin.c @@ -54,6 +54,7 @@ #include "nasm.h" #include "nasmlib.h" +#include "saa.h" #include "stdscan.h" #include "labels.h" #include "eval.h" diff --git a/output/outcoff.c b/output/outcoff.c index 4655e21f..d8a9c8ee 100644 --- a/output/outcoff.c +++ b/output/outcoff.c @@ -18,6 +18,7 @@ #include "nasm.h" #include "nasmlib.h" +#include "saa.h" #include "outform.h" #if defined(OF_COFF) || defined(OF_WIN32) || defined(OF_WIN64) diff --git a/output/outelf32.c b/output/outelf32.c index fc507f88..2ae387b3 100644 --- a/output/outelf32.c +++ b/output/outelf32.c @@ -19,7 +19,7 @@ #include "nasmlib.h" #include "stdscan.h" #include "outform.h" -#include "wsaa.h" +#include "saa.h" #ifdef OF_ELF32 diff --git a/output/outelf64.c b/output/outelf64.c index 76672930..cd568c16 100644 --- a/output/outelf64.c +++ b/output/outelf64.c @@ -18,7 +18,7 @@ #include "nasmlib.h" #include "stdscan.h" #include "outform.h" -#include "wsaa.h" +#include "saa.h" /* Definitions in lieu of elf.h */ #define SHT_NULL 0 /* Inactive section header */ diff --git a/output/outmacho.c b/output/outmacho.c index 420afcf9..50cd0b42 100644 --- a/output/outmacho.c +++ b/output/outmacho.c @@ -20,6 +20,7 @@ #include "nasm.h" #include "nasmlib.h" +#include "saa.h" #include "outform.h" #include "compiler.h" diff --git a/output/outrdf2.c b/output/outrdf2.c index a1e0b9a7..05c7ce1b 100644 --- a/output/outrdf2.c +++ b/output/outrdf2.c @@ -20,6 +20,7 @@ #include "nasm.h" #include "nasmlib.h" +#include "saa.h" #include "outform.h" /* VERBOSE_WARNINGS: define this to add some extra warnings... */ diff --git a/saa.c b/saa.c new file mode 100644 index 00000000..340068a8 --- /dev/null +++ b/saa.c @@ -0,0 +1,369 @@ +#include "compiler.h" +#include "nasmlib.h" +#include "saa.h" + +/* Aggregate SAA components smaller than this */ +#define SAA_BLKLEN 65536 + +struct SAA *saa_init(size_t elem_len) +{ + struct SAA *s; + char *data; + + s = nasm_zalloc(sizeof(struct SAA)); + + if (elem_len >= SAA_BLKLEN) + s->blk_len = elem_len; + else + s->blk_len = SAA_BLKLEN - (SAA_BLKLEN % elem_len); + + s->elem_len = elem_len; + s->length = s->blk_len; + data = nasm_malloc(s->blk_len); + s->nblkptrs = s->nblks = 1; + s->blk_ptrs = nasm_malloc(sizeof(char *)); + s->blk_ptrs[0] = data; + s->wblk = s->rblk = &s->blk_ptrs[0]; + + return s; +} + +void saa_free(struct SAA *s) +{ + char **p; + size_t n; + + for (p = s->blk_ptrs, n = s->nblks; n; p++, n--) + nasm_free(*p); + + nasm_free(s->blk_ptrs); + nasm_free(s); +} + +/* Add one allocation block to an SAA */ +static void saa_extend(struct SAA *s) +{ + size_t blkn = s->nblks++; + + if (blkn >= s->nblkptrs) { + size_t rindex = s->rblk - s->blk_ptrs; + size_t windex = s->wblk - s->blk_ptrs; + + s->nblkptrs <<= 1; + s->blk_ptrs = nasm_realloc(s->blk_ptrs, s->nblkptrs*sizeof(char *)); + + s->rblk = s->blk_ptrs + rindex; + s->wblk = s->blk_ptrs + windex; + } + + s->blk_ptrs[blkn] = nasm_malloc(s->blk_len); + s->length += s->blk_len; +} + +void *saa_wstruct(struct SAA *s) +{ + void *p; + + if (s->wpos % s->elem_len) + nasm_malloc_error(ERR_PANIC|ERR_NOFILE, + "misaligned wpos in saa_wstruct"); + + if (s->wpos + s->elem_len > s->blk_len) { + if (s->wpos != s->blk_len) + nasm_malloc_error(ERR_PANIC|ERR_NOFILE, + "unfilled block in saa_wstruct"); + + if (s->wptr + s->elem_len > s->length) + saa_extend(s); + s->wblk++; + s->wpos = 0; + } + + p = *s->wblk + s->wpos; + s->wpos += s->elem_len; + s->wptr += s->elem_len; + + if (s->wptr > s->datalen) + s->datalen = s->wptr; + + return p; +} + +void saa_wbytes(struct SAA *s, const void *data, size_t len) +{ + const char *d = data; + + while (len) { + size_t l = s->blk_len - s->wpos; + if (l > len) + l = len; + if (l) { + if (d) { + memcpy(*s->wblk + s->wpos, d, l); + d += l; + } else + memset(*s->wblk + s->wpos, 0, l); + s->wpos += l; + s->wptr += l; + len -= l; + + if (s->datalen < s->wptr) + s->datalen = s->wptr; + } + if (len) { + if (s->wptr >= s->length) + saa_extend(s); + s->wblk++; + s->wpos = 0; + } + } +} + +void saa_rewind(struct SAA *s) +{ + s->rblk = s->blk_ptrs; + s->rpos = s->rptr = 0; +} + +void *saa_rstruct(struct SAA *s) +{ + void *p; + + if (s->rptr + s->elem_len > s->datalen) + return NULL; + + if (s->rpos % s->elem_len) + nasm_malloc_error(ERR_PANIC|ERR_NOFILE, + "misaligned rpos in saa_rstruct"); + + if (s->rpos + s->elem_len > s->blk_len) { + s->rblk++; + s->rpos = 0; + } + + p = *s->rblk + s->rpos; + s->rpos += s->elem_len; + s->rptr += s->elem_len; + + return p; +} + +const void *saa_rbytes(struct SAA *s, size_t *lenp) +{ + const void *p; + size_t len; + + if (s->rptr >= s->datalen) { + *lenp = 0; + return NULL; + } + + if (s->rpos >= s->blk_len) { + s->rblk++; + s->rpos = 0; + } + + len = *lenp; + if (len > s->datalen - s->rptr) + len = s->datalen - s->rptr; + if (len > s->blk_len - s->rpos) + len = s->blk_len - s->rpos; + + *lenp = len; + p = *s->rblk + s->rpos; + + s->rpos += len; + s->rptr += len; + + return p; +} + +void saa_rnbytes(struct SAA *s, void *data, size_t len) +{ + char *d = data; + + if (s->rptr + len > s->datalen) { + nasm_malloc_error(ERR_PANIC|ERR_NOFILE, "overrun in saa_rnbytes"); + return; + } + + while (len) { + size_t l; + const void *p; + + l = len; + p = saa_rbytes(s, &l); + + memcpy(d, p, l); + d += l; + len -= l; + } +} + +/* Same as saa_rnbytes, except position the counter first */ +void saa_fread(struct SAA *s, size_t posn, void *data, size_t len) +{ + size_t ix; + + if (posn+len > s->datalen) { + nasm_malloc_error(ERR_PANIC|ERR_NOFILE, "overrun in saa_fread"); + return; + } + + ix = posn / s->blk_len; + s->rptr = posn; + s->rpos = posn % s->blk_len; + s->rblk = &s->blk_ptrs[ix]; + + saa_rnbytes(s, data, len); +} + +/* Same as saa_wbytes, except position the counter first */ +void saa_fwrite(struct SAA *s, size_t posn, const void *data, size_t len) +{ + size_t ix; + + if (posn > s->datalen) { + /* Seek beyond the end of the existing array not supported */ + nasm_malloc_error(ERR_PANIC|ERR_NOFILE, "overrun in saa_fwrite"); + return; + } + + ix = posn / s->blk_len; + s->wptr = posn; + s->wpos = posn % s->blk_len; + s->wblk = &s->blk_ptrs[ix]; + + if (!s->wpos) { + s->wpos = s->blk_len; + s->wblk--; + } + + saa_wbytes(s, data, len); +} + +void saa_fpwrite(struct SAA *s, FILE * fp) +{ + const char *data; + size_t len; + + saa_rewind(s); + while (len = s->datalen, (data = saa_rbytes(s, &len)) != NULL) + fwrite(data, 1, len, fp); +} + +void saa_write8(struct SAA *s, uint8_t v) +{ + saa_wbytes(s, &v, 1); +} + +#ifdef WORDS_LITTEENDIAN + +void saa_write16(struct SAA *s, uint16_t v) +{ + saa_wbytes(s, &v, 2); +} + +void saa_write32(struct SAA *s, uint32_t v) +{ + saa_wbytes(s, &v, 4); +} + +void saa_write64(struct SAA *s, uint64_t v) +{ + saa_wbytes(s, &v, 8); +} + +#else /* not WORDS_LITTLEENDIAN */ + +void saa_write16(struct SAA *s, uint16_t v) +{ + uint8_t b[2]; + + b[0] = v; + b[1] = v >> 8; + saa_wbytes(s, b, 2); +} + +void saa_write32(struct SAA *s, uint32_t v) +{ + uint8_t b[4]; + + b[0] = v; + b[1] = v >> 8; + b[2] = v >> 16; + b[3] = v >> 24; + saa_wbytes(s, b, 4); +} + +void saa_write64(struct SAA *s, uint64_t v) +{ + uint8_t b[8]; + + b[0] = v; + b[1] = v >> 8; + b[2] = v >> 16; + b[3] = v >> 24; + b[4] = v >> 32; + b[5] = v >> 40; + b[6] = v >> 48; + b[7] = v >> 56; + saa_wbytes(s, b, 8); +} + +#endif /* WORDS_LITTLEENDIAN */ + +/* write unsigned LEB128 value to SAA */ +void saa_wleb128u(struct SAA *psaa, int value) +{ + char temp[64], *ptemp; + uint8_t byte; + int len; + + ptemp = temp; + len = 0; + do + { + byte = value & 127; + value >>= 7; + if (value != 0) /* more bytes to come */ + byte |= 0x80; + *ptemp = byte; + ptemp++; + len++; + } while (value != 0); + saa_wbytes(psaa, temp, len); +} + +/* write signed LEB128 value to SAA */ +void saa_wleb128s(struct SAA *psaa, int value) +{ + char temp[64], *ptemp; + uint8_t byte; + bool more, negative; + int size, len; + + ptemp = temp; + more = 1; + negative = (value < 0); + size = sizeof(int) * 8; + len = 0; + while(more) + { + byte = value & 0x7f; + value >>= 7; + if (negative) + /* sign extend */ + value |= - (1 <<(size - 7)); + /* sign bit of byte is second high order bit (0x40) */ + if ((value == 0 && ! (byte & 0x40)) || + ((value == -1) && (byte & 0x40))) + more = 0; + else + byte |= 0x80; + *ptemp = byte; + ptemp++; + len++; + } + saa_wbytes(psaa, temp, len); +} diff --git a/saa.h b/saa.h new file mode 100644 index 00000000..773a4b62 --- /dev/null +++ b/saa.h @@ -0,0 +1,60 @@ +#ifndef NASM_SAA_H +#define NASM_SAA_H + +#include "compiler.h" +#include "nasmlib.h" + +/* + * Routines to manage a dynamic sequential-access array, under the + * same restriction on maximum mallocable block. This array may be + * written to in two ways: a contiguous chunk can be reserved of a + * given size with a pointer returned OR single-byte data may be + * written. The array can also be read back in the same two ways: + * as a series of big byte-data blocks or as a list of structures + * of a given size. + */ + +struct SAA { + /* + * members `end' and `elem_len' are only valid in first link in + * list; `rptr' and `rpos' are used for reading + */ + size_t elem_len; /* Size of each element */ + size_t blk_len; /* Size of each allocation block */ + size_t nblks; /* Total number of allocated blocks */ + size_t nblkptrs; /* Total number of allocation block pointers */ + size_t length; /* Total allocated length of the array */ + size_t datalen; /* Total data length of the array */ + char **wblk; /* Write block pointer */ + size_t wpos; /* Write position inside block */ + size_t wptr; /* Absolute write position */ + char **rblk; /* Read block pointer */ + size_t rpos; /* Read position inside block */ + size_t rptr; /* Absolute read position */ + char **blk_ptrs; /* Pointer to pointer blocks */ +}; + +struct SAA *saa_init(size_t elem_len); /* 1 == byte */ +void saa_free(struct SAA *); +void *saa_wstruct(struct SAA *); /* return a structure of elem_len */ +void saa_wbytes(struct SAA *, const void *, size_t); /* write arbitrary bytes */ +void saa_rewind(struct SAA *); /* for reading from beginning */ +void *saa_rstruct(struct SAA *); /* return NULL on EOA */ +const void *saa_rbytes(struct SAA *, size_t *); /* return 0 on EOA */ +void saa_rnbytes(struct SAA *, void *, size_t); /* read a given no. of bytes */ +/* random access */ +void saa_fread(struct SAA *, size_t, void *, size_t); +void saa_fwrite(struct SAA *, size_t, const void *, size_t); + +/* dump to file */ +void saa_fpwrite(struct SAA *, FILE *); + +/* Write specific-sized values */ +void saa_write8(struct SAA *s, uint8_t v); +void saa_write16(struct SAA *s, uint16_t v); +void saa_write32(struct SAA *s, uint32_t v); +void saa_write64(struct SAA *s, uint64_t v); +void saa_wleb128u(struct SAA *, int); /* write unsigned LEB128 value */ +void saa_wleb128s(struct SAA *, int); /* write signed LEB128 value */ + +#endif /* NASM_SAA_H */ diff --git a/wsaa.c b/wsaa.c deleted file mode 100644 index a45df701..00000000 --- a/wsaa.c +++ /dev/null @@ -1,64 +0,0 @@ -#include "compiler.h" -#include "nasmlib.h" -#include "wsaa.h" - -void saa_write8(struct SAA *s, uint8_t v) -{ - saa_wbytes(s, &v, 1); -} - -#ifdef WORDS_LITTEENDIAN - -void saa_write16(struct SAA *s, uint16_t v) -{ - saa_wbytes(s, &v, 2); -} - -void saa_write32(struct SAA *s, uint32_t v) -{ - saa_wbytes(s, &v, 4); -} - -void saa_write64(struct SAA *s, uint64_t v) -{ - saa_wbytes(s, &v, 8); -} - -#else /* not WORDS_LITTLEENDIAN */ - -void saa_write16(struct SAA *s, uint16_t v) -{ - uint8_t b[2]; - - b[0] = v; - b[1] = v >> 8; - saa_wbytes(s, b, 2); -} - -void saa_write32(struct SAA *s, uint32_t v) -{ - uint8_t b[4]; - - b[0] = v; - b[1] = v >> 8; - b[2] = v >> 16; - b[3] = v >> 24; - saa_wbytes(s, b, 4); -} - -void saa_write64(struct SAA *s, uint64_t v) -{ - uint8_t b[8]; - - b[0] = v; - b[1] = v >> 8; - b[2] = v >> 16; - b[3] = v >> 24; - b[4] = v >> 32; - b[5] = v >> 40; - b[6] = v >> 48; - b[7] = v >> 56; - saa_wbytes(s, b, 8); -} - -#endif /* WORDS_LITTLEENDIAN */ diff --git a/wsaa.h b/wsaa.h deleted file mode 100644 index 09801745..00000000 --- a/wsaa.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef NASM_WSAA_H -#define NASM_WSAA_H - -#include "compiler.h" -#include "nasmlib.h" - -void saa_write8(struct SAA *s, uint8_t v); -void saa_write16(struct SAA *s, uint16_t v); -void saa_write32(struct SAA *s, uint32_t v); -void saa_write64(struct SAA *s, uint64_t v); - -#endif /* wsaa.h */