From 22538e2b6713d6e4e05fb82c6969320a519b4c93 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Wed, 25 May 2016 05:42:47 -0700 Subject: [PATCH] nasmlib: break up nasmlib.c into logical components nasmlib.c had basically turned into a garbage bin of various functions with very little in common. Break it up into logical components for isolation and manageability. Signed-off-by: H. Peter Anvin --- Makefile.in | 48 +++- Mkfiles/msvc.mak | 44 ++-- Mkfiles/netware.mak | 44 ++-- Mkfiles/openwcom.mak | 48 +++- Mkfiles/owlinux.mak | 44 ++-- common.c | 93 ++++++++ configure.ac | 2 + nasmlib.h | 4 + nasmlib/bsi.c | 77 +++++++ nasmlib/error.c | 81 +++++++ nasmlib/filename.c | 68 ++++++ nasmlib/malloc.c | 107 +++++++++ nasmlib/nasmlib.c | 635 --------------------------------------------------- nasmlib/readnum.c | 171 ++++++++++++++ nasmlib/string.c | 242 ++++++++++++++++++++ nasmlib/zerobuf.c | 42 ++++ rdstrnum.c | 68 ++++++ segalloc.c | 51 +++++ stdlib/strnlen.c | 46 ++++ 19 files changed, 1217 insertions(+), 698 deletions(-) create mode 100644 common.c create mode 100644 nasmlib/bsi.c create mode 100644 nasmlib/error.c create mode 100644 nasmlib/filename.c create mode 100644 nasmlib/malloc.c delete mode 100644 nasmlib/nasmlib.c create mode 100644 nasmlib/readnum.c create mode 100644 nasmlib/string.c create mode 100644 nasmlib/zerobuf.c create mode 100644 rdstrnum.c create mode 100644 segalloc.c create mode 100644 stdlib/strnlen.c diff --git a/Makefile.in b/Makefile.in index a2ac077d..ac7a553f 100644 --- a/Makefile.in +++ b/Makefile.in @@ -95,19 +95,23 @@ NASM = nasm.$(O) \ preproc.$(O) quote.$(O) pptok.$(O) \ macros.$(O) listing.$(O) eval.$(O) exprlib.$(O) stdscan.$(O) \ strfunc.$(O) tokhash.$(O) regvals.$(O) regflags.$(O) \ + segalloc.$(O) \ preproc-nop.$(O) \ - disp8.$(O) \ - iflag.$(O) + disp8.$(O) rdstrnum.$(O) \ + iflag.$(O) common.$(O) NDISASM = ndisasm.$(O) disasm.$(O) sync.$(O) \ insnsd.$(O) insnsb.$(O) insnsn.$(O) regs.$(O) regdis.$(O) \ - disp8.$(O) iflag.$(O) + disp8.$(O) iflag.$(O) common.$(O) LIBOBJ = stdlib/snprintf.$(O) stdlib/vsnprintf.$(O) stdlib/strlcpy.$(O) \ - nasmlib/nasmlib.$(O) nasmlib/ver.$(O) \ - nasmlib/file.$(O) nasmlib/realpath.$(O) \ - nasmlib/ilog2.$(O) nasmlib/md5c.$(O) nasmlib/crc64.$(O) \ - nasmlib/srcfile.$(O) + stdlib/strnlen.$(O) \ + nasmlib/ver.$(O) \ + nasmlib/crc64.$(O) nasmlib/malloc.$(O) \ + nasmlib/error.$(O) nasmlib/md5c.$(O) nasmlib/string.$(O) \ + nasmlib/file.$(O) nasmlib/ilog2.$(O) \ + nasmlib/realpath.$(O) nasmlib/filename.$(O) nasmlib/srcfile.$(O) \ + nasmlib/zerobuf.$(O) nasmlib/readnum.$(O) nasmlib/bsi.$(O) #-- End File Lists --# all: nasm$(X) ndisasm$(X) rdf @@ -337,6 +341,9 @@ alldeps: perlreq assemble.$(O): assemble.c assemble.h compiler.h config.h directiv.h disp8.h \ iflag.h iflaggen.h insns.h insnsi.h listing.h nasm.h nasmint.h nasmlib.h \ opflags.h pptok.h preproc.h regs.h tables.h tokens.h +common.$(O): common.c compiler.h config.h directiv.h iflag.h iflaggen.h \ + insns.h insnsi.h nasm.h nasmint.h nasmlib.h opflags.h pptok.h preproc.h \ + regs.h tables.h tokens.h directiv.$(O): directiv.c compiler.h config.h directiv.h hashtbl.h insnsi.h \ nasm.h nasmint.h nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h disasm.$(O): disasm.c compiler.h config.h directiv.h disasm.h disp8.h \ @@ -376,25 +383,36 @@ nasm.$(O): nasm.c assemble.h compiler.h config.h directiv.h eval.h float.h \ iflag.h iflaggen.h insns.h insnsi.h labels.h listing.h nasm.h nasmint.h \ nasmlib.h opflags.h output/outform.h parser.h pptok.h preproc.h raa.h \ regs.h saa.h stdscan.h tables.h tokens.h ver.h +nasmlib/bsi.$(O): nasmlib/bsi.c compiler.h config.h nasmint.h nasmlib.h nasmlib/crc64.$(O): nasmlib/crc64.c compiler.h config.h hashtbl.h nasmint.h \ nasmlib.h +nasmlib/error.$(O): nasmlib/error.c compiler.h config.h nasmint.h nasmlib.h nasmlib/file.$(O): nasmlib/file.c compiler.h config.h nasmint.h nasmlib.h +nasmlib/filename.$(O): nasmlib/filename.c compiler.h config.h nasmint.h \ + nasmlib.h nasmlib/ilog2.$(O): nasmlib/ilog2.c compiler.h config.h nasmint.h nasmlib.h +nasmlib/malloc.$(O): nasmlib/malloc.c compiler.h config.h nasmint.h \ + nasmlib.h nasmlib/md5c.$(O): nasmlib/md5c.c compiler.h config.h md5.h nasmint.h -nasmlib/nasmlib.$(O): nasmlib/nasmlib.c compiler.h config.h directiv.h \ - iflag.h iflaggen.h insns.h insnsi.h nasm.h nasmint.h nasmlib.h opflags.h \ - pptok.h preproc.h regs.h tables.h tokens.h +nasmlib/readnum.$(O): nasmlib/readnum.c compiler.h config.h directiv.h \ + insnsi.h nasm.h nasmint.h nasmlib.h opflags.h pptok.h preproc.h regs.h \ + tables.h nasmlib/realpath.$(O): nasmlib/realpath.c compiler.h config.h nasmint.h \ nasmlib.h nasmlib/srcfile.$(O): nasmlib/srcfile.c compiler.h config.h hashtbl.h \ nasmint.h nasmlib.h +nasmlib/string.$(O): nasmlib/string.c compiler.h config.h nasmint.h \ + nasmlib.h nasmlib/ver.$(O): nasmlib/ver.c ver.h version.h +nasmlib/zerobuf.$(O): nasmlib/zerobuf.c compiler.h config.h nasmint.h \ + nasmlib.h ndisasm.$(O): ndisasm.c compiler.h config.h directiv.h disasm.h iflag.h \ iflaggen.h insns.h insnsi.h nasm.h nasmint.h nasmlib.h opflags.h pptok.h \ preproc.h regs.h sync.h tables.h tokens.h ver.h output/codeview.$(O): output/codeview.c compiler.h config.h directiv.h \ - insnsi.h md5.h nasm.h nasmint.h nasmlib.h opflags.h output/outlib.h \ - output/pecoff.h pptok.h preproc.h regs.h saa.h tables.h version.h + hashtbl.h insnsi.h md5.h nasm.h nasmint.h nasmlib.h opflags.h \ + output/outlib.h output/pecoff.h pptok.h preproc.h regs.h saa.h tables.h \ + version.h output/nulldbg.$(O): output/nulldbg.c compiler.h config.h directiv.h \ insnsi.h nasm.h nasmint.h nasmlib.h opflags.h output/outlib.h pptok.h \ preproc.h regs.h tables.h @@ -453,15 +471,21 @@ preproc.$(O): preproc.c compiler.h config.h directiv.h eval.h hashtbl.h \ quote.$(O): quote.c compiler.h config.h nasmint.h nasmlib.h quote.h raa.$(O): raa.c compiler.h config.h nasmint.h nasmlib.h raa.h rbtree.$(O): rbtree.c compiler.h config.h nasmint.h rbtree.h +rdstrnum.$(O): rdstrnum.c compiler.h config.h directiv.h insnsi.h nasm.h \ + nasmint.h nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h regdis.$(O): regdis.c regdis.h regs.h regflags.$(O): regflags.c compiler.h config.h directiv.h insnsi.h nasm.h \ nasmint.h nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h regs.$(O): regs.c compiler.h config.h insnsi.h nasmint.h tables.h regvals.$(O): regvals.c compiler.h config.h insnsi.h nasmint.h tables.h saa.$(O): saa.c compiler.h config.h nasmint.h nasmlib.h saa.h +segalloc.$(O): segalloc.c compiler.h config.h directiv.h iflag.h iflaggen.h \ + insns.h insnsi.h nasm.h nasmint.h nasmlib.h opflags.h pptok.h preproc.h \ + regs.h tables.h tokens.h stdlib/snprintf.$(O): stdlib/snprintf.c compiler.h config.h nasmint.h \ nasmlib.h stdlib/strlcpy.$(O): stdlib/strlcpy.c compiler.h config.h nasmint.h +stdlib/strnlen.$(O): stdlib/strnlen.c compiler.h config.h nasmint.h stdlib/vsnprintf.$(O): stdlib/vsnprintf.c compiler.h config.h nasmint.h \ nasmlib.h stdscan.$(O): stdscan.c compiler.h config.h directiv.h iflag.h iflaggen.h \ diff --git a/Mkfiles/msvc.mak b/Mkfiles/msvc.mak index d3cdca99..0bb25340 100644 --- a/Mkfiles/msvc.mak +++ b/Mkfiles/msvc.mak @@ -64,19 +64,23 @@ NASM = nasm.$(O) \ preproc.$(O) quote.$(O) pptok.$(O) \ macros.$(O) listing.$(O) eval.$(O) exprlib.$(O) stdscan.$(O) \ strfunc.$(O) tokhash.$(O) regvals.$(O) regflags.$(O) \ + segalloc.$(O) \ preproc-nop.$(O) \ - disp8.$(O) \ - iflag.$(O) + disp8.$(O) rdstrnum.$(O) \ + iflag.$(O) common.$(O) NDISASM = ndisasm.$(O) disasm.$(O) sync.$(O) \ insnsd.$(O) insnsb.$(O) insnsn.$(O) regs.$(O) regdis.$(O) \ - disp8.$(O) iflag.$(O) + disp8.$(O) iflag.$(O) common.$(O) LIBOBJ = stdlib/snprintf.$(O) stdlib/vsnprintf.$(O) stdlib/strlcpy.$(O) \ - nasmlib/nasmlib.$(O) nasmlib/ver.$(O) \ - nasmlib/file.$(O) nasmlib/realpath.$(O) \ - nasmlib/ilog2.$(O) nasmlib/md5c.$(O) nasmlib/crc64.$(O) \ - nasmlib/srcfile.$(O) + stdlib/strnlen.$(O) \ + nasmlib/ver.$(O) \ + nasmlib/crc64.$(O) nasmlib/malloc.$(O) \ + nasmlib/error.$(O) nasmlib/md5c.$(O) nasmlib/string.$(O) \ + nasmlib/file.$(O) nasmlib/ilog2.$(O) \ + nasmlib/realpath.$(O) nasmlib/filename.$(O) nasmlib/srcfile.$(O) \ + nasmlib/zerobuf.$(O) nasmlib/readnum.$(O) nasmlib/bsi.$(O) #-- End File Lists --# all: nasm$(X) ndisasm$(X) @@ -252,6 +256,9 @@ everything: all doc rdf assemble.$(O): assemble.c assemble.h compiler.h directiv.h disp8.h iflag.h \ iflaggen.h insns.h insnsi.h listing.h nasm.h nasmint.h nasmlib.h opflags.h \ pptok.h preproc.h regs.h tables.h tokens.h +common.$(O): common.c compiler.h directiv.h iflag.h iflaggen.h insns.h \ + insnsi.h nasm.h nasmint.h nasmlib.h opflags.h pptok.h preproc.h regs.h \ + tables.h tokens.h directiv.$(O): directiv.c compiler.h directiv.h hashtbl.h insnsi.h nasm.h \ nasmint.h nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h disasm.$(O): disasm.c compiler.h directiv.h disasm.h disp8.h iflag.h \ @@ -289,23 +296,28 @@ nasm.$(O): nasm.c assemble.h compiler.h directiv.h eval.h float.h iflag.h \ iflaggen.h insns.h insnsi.h labels.h listing.h nasm.h nasmint.h nasmlib.h \ opflags.h output/outform.h parser.h pptok.h preproc.h raa.h regs.h saa.h \ stdscan.h tables.h tokens.h ver.h +nasmlib/bsi.$(O): nasmlib/bsi.c compiler.h nasmint.h nasmlib.h nasmlib/crc64.$(O): nasmlib/crc64.c compiler.h hashtbl.h nasmint.h nasmlib.h +nasmlib/error.$(O): nasmlib/error.c compiler.h nasmint.h nasmlib.h nasmlib/file.$(O): nasmlib/file.c compiler.h nasmint.h nasmlib.h +nasmlib/filename.$(O): nasmlib/filename.c compiler.h nasmint.h nasmlib.h nasmlib/ilog2.$(O): nasmlib/ilog2.c compiler.h nasmint.h nasmlib.h +nasmlib/malloc.$(O): nasmlib/malloc.c compiler.h nasmint.h nasmlib.h nasmlib/md5c.$(O): nasmlib/md5c.c compiler.h md5.h nasmint.h -nasmlib/nasmlib.$(O): nasmlib/nasmlib.c compiler.h directiv.h iflag.h \ - iflaggen.h insns.h insnsi.h nasm.h nasmint.h nasmlib.h opflags.h pptok.h \ - preproc.h regs.h tables.h tokens.h +nasmlib/readnum.$(O): nasmlib/readnum.c compiler.h directiv.h insnsi.h \ + nasm.h nasmint.h nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h nasmlib/realpath.$(O): nasmlib/realpath.c compiler.h nasmint.h nasmlib.h nasmlib/srcfile.$(O): nasmlib/srcfile.c compiler.h hashtbl.h nasmint.h \ nasmlib.h +nasmlib/string.$(O): nasmlib/string.c compiler.h nasmint.h nasmlib.h nasmlib/ver.$(O): nasmlib/ver.c ver.h version.h +nasmlib/zerobuf.$(O): nasmlib/zerobuf.c compiler.h nasmint.h nasmlib.h ndisasm.$(O): ndisasm.c compiler.h directiv.h disasm.h iflag.h iflaggen.h \ insns.h insnsi.h nasm.h nasmint.h nasmlib.h opflags.h pptok.h preproc.h \ regs.h sync.h tables.h tokens.h ver.h -output/codeview.$(O): output/codeview.c compiler.h directiv.h insnsi.h md5.h \ - nasm.h nasmint.h nasmlib.h opflags.h output/outlib.h output/pecoff.h \ - pptok.h preproc.h regs.h saa.h tables.h version.h +output/codeview.$(O): output/codeview.c compiler.h directiv.h hashtbl.h \ + insnsi.h md5.h nasm.h nasmint.h nasmlib.h opflags.h output/outlib.h \ + output/pecoff.h pptok.h preproc.h regs.h saa.h tables.h version.h output/nulldbg.$(O): output/nulldbg.c compiler.h directiv.h insnsi.h nasm.h \ nasmint.h nasmlib.h opflags.h output/outlib.h pptok.h preproc.h regs.h \ tables.h @@ -362,14 +374,20 @@ preproc.$(O): preproc.c compiler.h directiv.h eval.h hashtbl.h insnsi.h \ quote.$(O): quote.c compiler.h nasmint.h nasmlib.h quote.h raa.$(O): raa.c compiler.h nasmint.h nasmlib.h raa.h rbtree.$(O): rbtree.c compiler.h nasmint.h rbtree.h +rdstrnum.$(O): rdstrnum.c compiler.h directiv.h insnsi.h nasm.h nasmint.h \ + nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h regdis.$(O): regdis.c regdis.h regs.h regflags.$(O): regflags.c compiler.h directiv.h insnsi.h nasm.h nasmint.h \ nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h regs.$(O): regs.c compiler.h insnsi.h nasmint.h tables.h regvals.$(O): regvals.c compiler.h insnsi.h nasmint.h tables.h saa.$(O): saa.c compiler.h nasmint.h nasmlib.h saa.h +segalloc.$(O): segalloc.c compiler.h directiv.h iflag.h iflaggen.h insns.h \ + insnsi.h nasm.h nasmint.h nasmlib.h opflags.h pptok.h preproc.h regs.h \ + tables.h tokens.h stdlib/snprintf.$(O): stdlib/snprintf.c compiler.h nasmint.h nasmlib.h stdlib/strlcpy.$(O): stdlib/strlcpy.c compiler.h nasmint.h +stdlib/strnlen.$(O): stdlib/strnlen.c compiler.h nasmint.h stdlib/vsnprintf.$(O): stdlib/vsnprintf.c compiler.h nasmint.h nasmlib.h stdscan.$(O): stdscan.c compiler.h directiv.h iflag.h iflaggen.h insns.h \ insnsi.h nasm.h nasmint.h nasmlib.h opflags.h pptok.h preproc.h quote.h \ diff --git a/Mkfiles/netware.mak b/Mkfiles/netware.mak index c2dc8006..816cb1dd 100644 --- a/Mkfiles/netware.mak +++ b/Mkfiles/netware.mak @@ -45,19 +45,23 @@ NASM = nasm.o \ preproc.o quote.o pptok.o \ macros.o listing.o eval.o exprlib.o stdscan.o \ strfunc.o tokhash.o regvals.o regflags.o \ + segalloc.o \ preproc-nop.o \ - disp8.o \ - iflag.o + disp8.o rdstrnum.o \ + iflag.o common.o NDISASM = ndisasm.o disasm.o sync.o \ insnsd.o insnsb.o insnsn.o regs.o regdis.o \ - disp8.o iflag.o + disp8.o iflag.o common.o LIBOBJ = snprintf.o vsnprintf.o strlcpy.o \ - nasmlib.o ver.o \ - file.o realpath.o \ - ilog2.o md5c.o crc64.o \ - srcfile.o + strnlen.o \ + ver.o \ + crc64.o malloc.o \ + error.o md5c.o string.o \ + file.o ilog2.o \ + realpath.o filename.o srcfile.o \ + zerobuf.o readnum.o bsi.o #-- End File Lists --# NASM_OBJ = $(addprefix $(OBJDIR)/,$(notdir $(NASM))) $(EOLIST) @@ -138,6 +142,9 @@ $(OBJDIR)/version.mak: $(PROOT)/version $(PROOT)/version.pl $(OBJDIR) assemble.o: assemble.c assemble.h compiler.h config.h directiv.h disp8.h \ iflag.h iflaggen.h insns.h insnsi.h listing.h nasm.h nasmint.h nasmlib.h \ opflags.h pptok.h preproc.h regs.h tables.h tokens.h +common.o: common.c compiler.h config.h directiv.h iflag.h iflaggen.h insns.h \ + insnsi.h nasm.h nasmint.h nasmlib.h opflags.h pptok.h preproc.h regs.h \ + tables.h tokens.h directiv.o: directiv.c compiler.h config.h directiv.h hashtbl.h insnsi.h \ nasm.h nasmint.h nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h disasm.o: disasm.c compiler.h config.h directiv.h disasm.h disp8.h iflag.h \ @@ -176,22 +183,27 @@ nasm.o: nasm.c assemble.h compiler.h config.h directiv.h eval.h float.h \ iflag.h iflaggen.h insns.h insnsi.h labels.h listing.h nasm.h nasmint.h \ nasmlib.h opflags.h outform.h parser.h pptok.h preproc.h raa.h regs.h saa.h \ stdscan.h tables.h tokens.h ver.h +bsi.o: bsi.c compiler.h config.h nasmint.h nasmlib.h crc64.o: crc64.c compiler.h config.h hashtbl.h nasmint.h nasmlib.h +error.o: error.c compiler.h config.h nasmint.h nasmlib.h file.o: file.c compiler.h config.h nasmint.h nasmlib.h +filename.o: filename.c compiler.h config.h nasmint.h nasmlib.h ilog2.o: ilog2.c compiler.h config.h nasmint.h nasmlib.h +malloc.o: malloc.c compiler.h config.h nasmint.h nasmlib.h md5c.o: md5c.c compiler.h config.h md5.h nasmint.h -nasmlib.o: nasmlib.c compiler.h config.h directiv.h iflag.h iflaggen.h \ - insns.h insnsi.h nasm.h nasmint.h nasmlib.h opflags.h pptok.h preproc.h \ - regs.h tables.h tokens.h +readnum.o: readnum.c compiler.h config.h directiv.h insnsi.h nasm.h \ + nasmint.h nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h realpath.o: realpath.c compiler.h config.h nasmint.h nasmlib.h srcfile.o: srcfile.c compiler.h config.h hashtbl.h nasmint.h nasmlib.h +string.o: string.c compiler.h config.h nasmint.h nasmlib.h ver.o: ver.c ver.h version.h +zerobuf.o: zerobuf.c compiler.h config.h nasmint.h nasmlib.h ndisasm.o: ndisasm.c compiler.h config.h directiv.h disasm.h iflag.h \ iflaggen.h insns.h insnsi.h nasm.h nasmint.h nasmlib.h opflags.h pptok.h \ preproc.h regs.h sync.h tables.h tokens.h ver.h -codeview.o: codeview.c compiler.h config.h directiv.h insnsi.h md5.h nasm.h \ - nasmint.h nasmlib.h opflags.h outlib.h pecoff.h pptok.h preproc.h regs.h \ - saa.h tables.h version.h +codeview.o: codeview.c compiler.h config.h directiv.h hashtbl.h insnsi.h \ + md5.h nasm.h nasmint.h nasmlib.h opflags.h outlib.h pecoff.h pptok.h \ + preproc.h regs.h saa.h tables.h version.h nulldbg.o: nulldbg.c compiler.h config.h directiv.h insnsi.h nasm.h \ nasmint.h nasmlib.h opflags.h outlib.h pptok.h preproc.h regs.h tables.h nullout.o: nullout.c compiler.h config.h directiv.h insnsi.h nasm.h \ @@ -244,14 +256,20 @@ preproc.o: preproc.c compiler.h config.h directiv.h eval.h hashtbl.h \ quote.o: quote.c compiler.h config.h nasmint.h nasmlib.h quote.h raa.o: raa.c compiler.h config.h nasmint.h nasmlib.h raa.h rbtree.o: rbtree.c compiler.h config.h nasmint.h rbtree.h +rdstrnum.o: rdstrnum.c compiler.h config.h directiv.h insnsi.h nasm.h \ + nasmint.h nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h regdis.o: regdis.c regdis.h regs.h regflags.o: regflags.c compiler.h config.h directiv.h insnsi.h nasm.h \ nasmint.h nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h regs.o: regs.c compiler.h config.h insnsi.h nasmint.h tables.h regvals.o: regvals.c compiler.h config.h insnsi.h nasmint.h tables.h saa.o: saa.c compiler.h config.h nasmint.h nasmlib.h saa.h +segalloc.o: segalloc.c compiler.h config.h directiv.h iflag.h iflaggen.h \ + insns.h insnsi.h nasm.h nasmint.h nasmlib.h opflags.h pptok.h preproc.h \ + regs.h tables.h tokens.h snprintf.o: snprintf.c compiler.h config.h nasmint.h nasmlib.h strlcpy.o: strlcpy.c compiler.h config.h nasmint.h +strnlen.o: strnlen.c compiler.h config.h nasmint.h vsnprintf.o: vsnprintf.c compiler.h config.h nasmint.h nasmlib.h stdscan.o: stdscan.c compiler.h config.h directiv.h iflag.h iflaggen.h \ insns.h insnsi.h nasm.h nasmint.h nasmlib.h opflags.h pptok.h preproc.h \ diff --git a/Mkfiles/openwcom.mak b/Mkfiles/openwcom.mak index b25b80c0..ac73f148 100644 --- a/Mkfiles/openwcom.mak +++ b/Mkfiles/openwcom.mak @@ -61,19 +61,23 @@ NASM = nasm.$(O) & preproc.$(O) quote.$(O) pptok.$(O) & macros.$(O) listing.$(O) eval.$(O) exprlib.$(O) stdscan.$(O) & strfunc.$(O) tokhash.$(O) regvals.$(O) regflags.$(O) & + segalloc.$(O) & preproc-nop.$(O) & - disp8.$(O) & - iflag.$(O) + disp8.$(O) rdstrnum.$(O) & + iflag.$(O) common.$(O) NDISASM = ndisasm.$(O) disasm.$(O) sync.$(O) & insnsd.$(O) insnsb.$(O) insnsn.$(O) regs.$(O) regdis.$(O) & - disp8.$(O) iflag.$(O) + disp8.$(O) iflag.$(O) common.$(O) LIBOBJ = stdlib/snprintf.$(O) stdlib/vsnprintf.$(O) stdlib/strlcpy.$(O) & - nasmlib/nasmlib.$(O) nasmlib/ver.$(O) & - nasmlib/file.$(O) nasmlib/realpath.$(O) & - nasmlib/ilog2.$(O) nasmlib/md5c.$(O) nasmlib/crc64.$(O) & - nasmlib/srcfile.$(O) + stdlib/strnlen.$(O) & + nasmlib/ver.$(O) & + nasmlib/crc64.$(O) nasmlib/malloc.$(O) & + nasmlib/error.$(O) nasmlib/md5c.$(O) nasmlib/string.$(O) & + nasmlib/file.$(O) nasmlib/ilog2.$(O) & + nasmlib/realpath.$(O) nasmlib/filename.$(O) nasmlib/srcfile.$(O) & + nasmlib/zerobuf.$(O) nasmlib/readnum.$(O) nasmlib/bsi.$(O) #-- End File Lists --# what: .SYMBOLIC @@ -275,6 +279,9 @@ alldeps: perlreq .SYMBOLIC assemble.$(O): assemble.c assemble.h compiler.h config.h directiv.h disp8.h & iflag.h iflaggen.h insns.h insnsi.h listing.h nasm.h nasmint.h nasmlib.h & opflags.h pptok.h preproc.h regs.h tables.h tokens.h +common.$(O): common.c compiler.h config.h directiv.h iflag.h iflaggen.h & + insns.h insnsi.h nasm.h nasmint.h nasmlib.h opflags.h pptok.h preproc.h & + regs.h tables.h tokens.h directiv.$(O): directiv.c compiler.h config.h directiv.h hashtbl.h insnsi.h & nasm.h nasmint.h nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h disasm.$(O): disasm.c compiler.h config.h directiv.h disasm.h disp8.h & @@ -314,25 +321,36 @@ nasm.$(O): nasm.c assemble.h compiler.h config.h directiv.h eval.h float.h & iflag.h iflaggen.h insns.h insnsi.h labels.h listing.h nasm.h nasmint.h & nasmlib.h opflags.h output/outform.h parser.h pptok.h preproc.h raa.h & regs.h saa.h stdscan.h tables.h tokens.h ver.h +nasmlib/bsi.$(O): nasmlib/bsi.c compiler.h config.h nasmint.h nasmlib.h nasmlib/crc64.$(O): nasmlib/crc64.c compiler.h config.h hashtbl.h nasmint.h & nasmlib.h +nasmlib/error.$(O): nasmlib/error.c compiler.h config.h nasmint.h nasmlib.h nasmlib/file.$(O): nasmlib/file.c compiler.h config.h nasmint.h nasmlib.h +nasmlib/filename.$(O): nasmlib/filename.c compiler.h config.h nasmint.h & + nasmlib.h nasmlib/ilog2.$(O): nasmlib/ilog2.c compiler.h config.h nasmint.h nasmlib.h +nasmlib/malloc.$(O): nasmlib/malloc.c compiler.h config.h nasmint.h & + nasmlib.h nasmlib/md5c.$(O): nasmlib/md5c.c compiler.h config.h md5.h nasmint.h -nasmlib/nasmlib.$(O): nasmlib/nasmlib.c compiler.h config.h directiv.h & - iflag.h iflaggen.h insns.h insnsi.h nasm.h nasmint.h nasmlib.h opflags.h & - pptok.h preproc.h regs.h tables.h tokens.h +nasmlib/readnum.$(O): nasmlib/readnum.c compiler.h config.h directiv.h & + insnsi.h nasm.h nasmint.h nasmlib.h opflags.h pptok.h preproc.h regs.h & + tables.h nasmlib/realpath.$(O): nasmlib/realpath.c compiler.h config.h nasmint.h & nasmlib.h nasmlib/srcfile.$(O): nasmlib/srcfile.c compiler.h config.h hashtbl.h & nasmint.h nasmlib.h +nasmlib/string.$(O): nasmlib/string.c compiler.h config.h nasmint.h & + nasmlib.h nasmlib/ver.$(O): nasmlib/ver.c ver.h version.h +nasmlib/zerobuf.$(O): nasmlib/zerobuf.c compiler.h config.h nasmint.h & + nasmlib.h ndisasm.$(O): ndisasm.c compiler.h config.h directiv.h disasm.h iflag.h & iflaggen.h insns.h insnsi.h nasm.h nasmint.h nasmlib.h opflags.h pptok.h & preproc.h regs.h sync.h tables.h tokens.h ver.h output/codeview.$(O): output/codeview.c compiler.h config.h directiv.h & - insnsi.h md5.h nasm.h nasmint.h nasmlib.h opflags.h output/outlib.h & - output/pecoff.h pptok.h preproc.h regs.h saa.h tables.h version.h + hashtbl.h insnsi.h md5.h nasm.h nasmint.h nasmlib.h opflags.h & + output/outlib.h output/pecoff.h pptok.h preproc.h regs.h saa.h tables.h & + version.h output/nulldbg.$(O): output/nulldbg.c compiler.h config.h directiv.h & insnsi.h nasm.h nasmint.h nasmlib.h opflags.h output/outlib.h pptok.h & preproc.h regs.h tables.h @@ -391,15 +409,21 @@ preproc.$(O): preproc.c compiler.h config.h directiv.h eval.h hashtbl.h & quote.$(O): quote.c compiler.h config.h nasmint.h nasmlib.h quote.h raa.$(O): raa.c compiler.h config.h nasmint.h nasmlib.h raa.h rbtree.$(O): rbtree.c compiler.h config.h nasmint.h rbtree.h +rdstrnum.$(O): rdstrnum.c compiler.h config.h directiv.h insnsi.h nasm.h & + nasmint.h nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h regdis.$(O): regdis.c regdis.h regs.h regflags.$(O): regflags.c compiler.h config.h directiv.h insnsi.h nasm.h & nasmint.h nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h regs.$(O): regs.c compiler.h config.h insnsi.h nasmint.h tables.h regvals.$(O): regvals.c compiler.h config.h insnsi.h nasmint.h tables.h saa.$(O): saa.c compiler.h config.h nasmint.h nasmlib.h saa.h +segalloc.$(O): segalloc.c compiler.h config.h directiv.h iflag.h iflaggen.h & + insns.h insnsi.h nasm.h nasmint.h nasmlib.h opflags.h pptok.h preproc.h & + regs.h tables.h tokens.h stdlib/snprintf.$(O): stdlib/snprintf.c compiler.h config.h nasmint.h & nasmlib.h stdlib/strlcpy.$(O): stdlib/strlcpy.c compiler.h config.h nasmint.h +stdlib/strnlen.$(O): stdlib/strnlen.c compiler.h config.h nasmint.h stdlib/vsnprintf.$(O): stdlib/vsnprintf.c compiler.h config.h nasmint.h & nasmlib.h stdscan.$(O): stdscan.c compiler.h config.h directiv.h iflag.h iflaggen.h & diff --git a/Mkfiles/owlinux.mak b/Mkfiles/owlinux.mak index 812cbd18..227eba98 100644 --- a/Mkfiles/owlinux.mak +++ b/Mkfiles/owlinux.mak @@ -72,19 +72,23 @@ NASM = nasm.$(O) \ preproc.$(O) quote.$(O) pptok.$(O) \ macros.$(O) listing.$(O) eval.$(O) exprlib.$(O) stdscan.$(O) \ strfunc.$(O) tokhash.$(O) regvals.$(O) regflags.$(O) \ + segalloc.$(O) \ preproc-nop.$(O) \ - disp8.$(O) \ - iflag.$(O) + disp8.$(O) rdstrnum.$(O) \ + iflag.$(O) common.$(O) NDISASM = ndisasm.$(O) disasm.$(O) sync.$(O) \ insnsd.$(O) insnsb.$(O) insnsn.$(O) regs.$(O) regdis.$(O) \ - disp8.$(O) iflag.$(O) + disp8.$(O) iflag.$(O) common.$(O) LIBOBJ = stdlib/snprintf.$(O) stdlib/vsnprintf.$(O) stdlib/strlcpy.$(O) \ - nasmlib/nasmlib.$(O) nasmlib/ver.$(O) \ - nasmlib/file.$(O) nasmlib/realpath.$(O) \ - nasmlib/ilog2.$(O) nasmlib/md5c.$(O) nasmlib/crc64.$(O) \ - nasmlib/srcfile.$(O) + stdlib/strnlen.$(O) \ + nasmlib/ver.$(O) \ + nasmlib/crc64.$(O) nasmlib/malloc.$(O) \ + nasmlib/error.$(O) nasmlib/md5c.$(O) nasmlib/string.$(O) \ + nasmlib/file.$(O) nasmlib/ilog2.$(O) \ + nasmlib/realpath.$(O) nasmlib/filename.$(O) nasmlib/srcfile.$(O) \ + nasmlib/zerobuf.$(O) nasmlib/readnum.$(O) nasmlib/bsi.$(O) #-- End File Lists --# what: @@ -244,6 +248,9 @@ everything: all doc rdf assemble.$(O): assemble.c assemble.h compiler.h directiv.h disp8.h iflag.h \ iflaggen.h insns.h insnsi.h listing.h nasm.h nasmint.h nasmlib.h opflags.h \ pptok.h preproc.h regs.h tables.h tokens.h +common.$(O): common.c compiler.h directiv.h iflag.h iflaggen.h insns.h \ + insnsi.h nasm.h nasmint.h nasmlib.h opflags.h pptok.h preproc.h regs.h \ + tables.h tokens.h directiv.$(O): directiv.c compiler.h directiv.h hashtbl.h insnsi.h nasm.h \ nasmint.h nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h disasm.$(O): disasm.c compiler.h directiv.h disasm.h disp8.h iflag.h \ @@ -281,23 +288,28 @@ nasm.$(O): nasm.c assemble.h compiler.h directiv.h eval.h float.h iflag.h \ iflaggen.h insns.h insnsi.h labels.h listing.h nasm.h nasmint.h nasmlib.h \ opflags.h output/outform.h parser.h pptok.h preproc.h raa.h regs.h saa.h \ stdscan.h tables.h tokens.h ver.h +nasmlib/bsi.$(O): nasmlib/bsi.c compiler.h nasmint.h nasmlib.h nasmlib/crc64.$(O): nasmlib/crc64.c compiler.h hashtbl.h nasmint.h nasmlib.h +nasmlib/error.$(O): nasmlib/error.c compiler.h nasmint.h nasmlib.h nasmlib/file.$(O): nasmlib/file.c compiler.h nasmint.h nasmlib.h +nasmlib/filename.$(O): nasmlib/filename.c compiler.h nasmint.h nasmlib.h nasmlib/ilog2.$(O): nasmlib/ilog2.c compiler.h nasmint.h nasmlib.h +nasmlib/malloc.$(O): nasmlib/malloc.c compiler.h nasmint.h nasmlib.h nasmlib/md5c.$(O): nasmlib/md5c.c compiler.h md5.h nasmint.h -nasmlib/nasmlib.$(O): nasmlib/nasmlib.c compiler.h directiv.h iflag.h \ - iflaggen.h insns.h insnsi.h nasm.h nasmint.h nasmlib.h opflags.h pptok.h \ - preproc.h regs.h tables.h tokens.h +nasmlib/readnum.$(O): nasmlib/readnum.c compiler.h directiv.h insnsi.h \ + nasm.h nasmint.h nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h nasmlib/realpath.$(O): nasmlib/realpath.c compiler.h nasmint.h nasmlib.h nasmlib/srcfile.$(O): nasmlib/srcfile.c compiler.h hashtbl.h nasmint.h \ nasmlib.h +nasmlib/string.$(O): nasmlib/string.c compiler.h nasmint.h nasmlib.h nasmlib/ver.$(O): nasmlib/ver.c ver.h version.h +nasmlib/zerobuf.$(O): nasmlib/zerobuf.c compiler.h nasmint.h nasmlib.h ndisasm.$(O): ndisasm.c compiler.h directiv.h disasm.h iflag.h iflaggen.h \ insns.h insnsi.h nasm.h nasmint.h nasmlib.h opflags.h pptok.h preproc.h \ regs.h sync.h tables.h tokens.h ver.h -output/codeview.$(O): output/codeview.c compiler.h directiv.h insnsi.h md5.h \ - nasm.h nasmint.h nasmlib.h opflags.h output/outlib.h output/pecoff.h \ - pptok.h preproc.h regs.h saa.h tables.h version.h +output/codeview.$(O): output/codeview.c compiler.h directiv.h hashtbl.h \ + insnsi.h md5.h nasm.h nasmint.h nasmlib.h opflags.h output/outlib.h \ + output/pecoff.h pptok.h preproc.h regs.h saa.h tables.h version.h output/nulldbg.$(O): output/nulldbg.c compiler.h directiv.h insnsi.h nasm.h \ nasmint.h nasmlib.h opflags.h output/outlib.h pptok.h preproc.h regs.h \ tables.h @@ -354,14 +366,20 @@ preproc.$(O): preproc.c compiler.h directiv.h eval.h hashtbl.h insnsi.h \ quote.$(O): quote.c compiler.h nasmint.h nasmlib.h quote.h raa.$(O): raa.c compiler.h nasmint.h nasmlib.h raa.h rbtree.$(O): rbtree.c compiler.h nasmint.h rbtree.h +rdstrnum.$(O): rdstrnum.c compiler.h directiv.h insnsi.h nasm.h nasmint.h \ + nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h regdis.$(O): regdis.c regdis.h regs.h regflags.$(O): regflags.c compiler.h directiv.h insnsi.h nasm.h nasmint.h \ nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h regs.$(O): regs.c compiler.h insnsi.h nasmint.h tables.h regvals.$(O): regvals.c compiler.h insnsi.h nasmint.h tables.h saa.$(O): saa.c compiler.h nasmint.h nasmlib.h saa.h +segalloc.$(O): segalloc.c compiler.h directiv.h iflag.h iflaggen.h insns.h \ + insnsi.h nasm.h nasmint.h nasmlib.h opflags.h pptok.h preproc.h regs.h \ + tables.h tokens.h stdlib/snprintf.$(O): stdlib/snprintf.c compiler.h nasmint.h nasmlib.h stdlib/strlcpy.$(O): stdlib/strlcpy.c compiler.h nasmint.h +stdlib/strnlen.$(O): stdlib/strnlen.c compiler.h nasmint.h stdlib/vsnprintf.$(O): stdlib/vsnprintf.c compiler.h nasmint.h nasmlib.h stdscan.$(O): stdscan.c compiler.h directiv.h iflag.h iflaggen.h insns.h \ insnsi.h nasm.h nasmint.h nasmlib.h opflags.h pptok.h preproc.h quote.h \ diff --git a/common.c b/common.c new file mode 100644 index 00000000..9c91f910 --- /dev/null +++ b/common.c @@ -0,0 +1,93 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 1996-2016 The NASM Authors - All Rights Reserved + * See the file AUTHORS included with the NASM distribution for + * the specific copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following + * conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * ----------------------------------------------------------------------- */ + +/* + * common.c - code common to nasm and ndisasm + */ + +#include "compiler.h" +#include "nasm.h" +#include "nasmlib.h" +#include "insns.h" + +/* + * The current bit size of the CPU + */ +int globalbits = 0; +/* + * Common list of prefix names; ideally should be auto-generated + * from tokens.dat + */ +const char *prefix_name(int token) +{ + static const char *prefix_names[] = { + "a16", "a32", "a64", "asp", "lock", "o16", "o32", "o64", "osp", + "rep", "repe", "repne", "repnz", "repz", "times", "wait", + "xacquire", "xrelease", "bnd" + }; + unsigned int prefix = token-PREFIX_ENUM_START; + + if (prefix >= ARRAY_SIZE(prefix_names)) + return NULL; + + return prefix_names[prefix]; +} + +/* + * initialized data bytes length from opcode + */ +int idata_bytes(int opcode) +{ + switch (opcode) { + case I_DB: + return 1; + case I_DW: + return 2; + case I_DD: + return 4; + case I_DQ: + return 8; + case I_DT: + return 10; + case I_DO: + return 16; + case I_DY: + return 32; + case I_DZ: + return 64; + case I_none: + return -1; + default: + return 0; + } +} diff --git a/configure.ac b/configure.ac index 6ca319c9..d42d40e3 100644 --- a/configure.ac +++ b/configure.ac @@ -116,6 +116,7 @@ dnl Checks for library functions. AC_CHECK_FUNCS(strcasecmp stricmp) AC_CHECK_FUNCS(strncasecmp strnicmp) AC_CHECK_FUNCS(strsep) +AC_CHECK_FUNCS(strnlen) AC_CHECK_FUNCS(getuid) AC_CHECK_FUNCS(getgid) @@ -148,6 +149,7 @@ AC_CHECK_DECLS(strncasecmp) AC_CHECK_DECLS(strnicmp) AC_CHECK_DECLS(strsep) AC_CHECK_DECLS(strlcpy) +AC_CHECK_DECLS(strnlen) if test $ac_cv_prog_make_vpathok = no; then echo Copying generated srcs into build directory to compensate for VPATH breakage diff --git a/nasmlib.h b/nasmlib.h index cf4a919a..440aa8d7 100644 --- a/nasmlib.h +++ b/nasmlib.h @@ -205,6 +205,10 @@ int nasm_memicmp(const char *, const char *, size_t); char *nasm_strsep(char **stringp, const char *delim); #endif +#ifndef HAVE_DECL_STRNLEN +size_t strnlen(const char *, size_t); +#endif + /* This returns the numeric value of a given 'digit'. */ #define numvalue(c) ((c) >= 'a' ? (c) - 'a' + 10 : (c) >= 'A' ? (c) - 'A' + 10 : (c) - '0') diff --git a/nasmlib/bsi.c b/nasmlib/bsi.c new file mode 100644 index 00000000..a811e45e --- /dev/null +++ b/nasmlib/bsi.c @@ -0,0 +1,77 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 1996-2016 The NASM Authors - All Rights Reserved + * See the file AUTHORS included with the NASM distribution for + * the specific copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following + * conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * ----------------------------------------------------------------------- */ + +/* + * nasmlib.c library routines for the Netwide Assembler + */ + +#include "compiler.h" + +#include + +#include "nasmlib.h" + +/* + * Binary search. + */ +int bsi(const char *string, const char **array, int size) +{ + int i = -1, j = size; /* always, i < index < j */ + while (j - i >= 2) { + int k = (i + j) / 2; + int l = strcmp(string, array[k]); + if (l < 0) /* it's in the first half */ + j = k; + else if (l > 0) /* it's in the second half */ + i = k; + else /* we've got it :) */ + return k; + } + return -1; /* we haven't got it :( */ +} + +int bsii(const char *string, const char **array, int size) +{ + int i = -1, j = size; /* always, i < index < j */ + while (j - i >= 2) { + int k = (i + j) / 2; + int l = nasm_stricmp(string, array[k]); + if (l < 0) /* it's in the first half */ + j = k; + else if (l > 0) /* it's in the second half */ + i = k; + else /* we've got it :) */ + return k; + } + return -1; /* we haven't got it :( */ +} diff --git a/nasmlib/error.c b/nasmlib/error.c new file mode 100644 index 00000000..83838e19 --- /dev/null +++ b/nasmlib/error.c @@ -0,0 +1,81 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 1996-2016 The NASM Authors - All Rights Reserved + * See the file AUTHORS included with the NASM distribution for + * the specific copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following + * conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * ----------------------------------------------------------------------- */ + +/* + * nasmlib.c library routines for the Netwide Assembler + */ + +#include "compiler.h" + +#include + +#include "nasmlib.h" + +vefunc nasm_verror; /* Global error handling function */ + +void nasm_error(int severity, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + nasm_verror(severity, fmt, ap); + va_end(ap); +} + +no_return nasm_fatal(int flags, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + nasm_verror(flags | ERR_FATAL, fmt, ap); + abort(); /* We should never get here */ +} + +no_return nasm_panic(int flags, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + nasm_verror(flags | ERR_PANIC, fmt, ap); + abort(); /* We should never get here */ +} + +no_return nasm_panic_from_macro(const char *file, int line) +{ + nasm_panic(ERR_NOFILE, "Internal error at %s:%d\n", file, line); +} + +no_return nasm_assert_failed(const char *file, int line, const char *msg) +{ + nasm_fatal(0, "assertion %s failed at %s:%d", msg, file, line); +} diff --git a/nasmlib/filename.c b/nasmlib/filename.c new file mode 100644 index 00000000..5483bd7e --- /dev/null +++ b/nasmlib/filename.c @@ -0,0 +1,68 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 1996-2016 The NASM Authors - All Rights Reserved + * See the file AUTHORS included with the NASM distribution for + * the specific copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following + * conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * ----------------------------------------------------------------------- */ + +/* + * nasmlib.c library routines for the Netwide Assembler + */ + +#include "compiler.h" +#include "nasmlib.h" + +void standard_extension(char *inname, char *outname, char *extension) +{ + char *p, *q; + + if (*outname) /* file name already exists, */ + return; /* so do nothing */ + q = inname; + p = outname; + while (*q) + *p++ = *q++; /* copy, and find end of string */ + *p = '\0'; /* terminate it */ + while (p > outname && *--p != '.') ; /* find final period (or whatever) */ + if (*p != '.') + while (*p) + p++; /* go back to end if none found */ + if (!strcmp(p, extension)) { /* is the extension already there? */ + if (*extension) + nasm_error(ERR_WARNING | ERR_NOFILE, + "file name already ends in `%s': " + "output will be in `nasm.out'", extension); + else + nasm_error(ERR_WARNING | ERR_NOFILE, + "file name already has no extension: " + "output will be in `nasm.out'"); + strcpy(outname, "nasm.out"); + } else + strcpy(p, extension); +} diff --git a/nasmlib/malloc.c b/nasmlib/malloc.c new file mode 100644 index 00000000..7483760d --- /dev/null +++ b/nasmlib/malloc.c @@ -0,0 +1,107 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 1996-2016 The NASM Authors - All Rights Reserved + * See the file AUTHORS included with the NASM distribution for + * the specific copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following + * conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * ----------------------------------------------------------------------- */ + +/* + * nasmlib.c library routines for the Netwide Assembler + */ + +#include "compiler.h" + +#include + +#include "nasmlib.h" + +void *nasm_malloc(size_t size) +{ + void *p = malloc(size); + if (!p) + nasm_fatal(ERR_NOFILE, "out of memory"); + return p; +} + +void *nasm_calloc(size_t size, size_t nelem) +{ + void *p = calloc(size, nelem); + if (!p) + nasm_fatal(ERR_NOFILE, "out of memory"); + return p; +} + +void *nasm_zalloc(size_t size) +{ + return nasm_calloc(size, 1); +} + +void *nasm_realloc(void *q, size_t size) +{ + void *p = q ? realloc(q, size) : malloc(size); + if (!p) + nasm_fatal(ERR_NOFILE, "out of memory"); + return p; +} + +void nasm_free(void *q) +{ + if (q) + free(q); +} + +char *nasm_strdup(const char *s) +{ + char *p; + size_t size = strlen(s) + 1; + + p = nasm_malloc(size); + return memcpy(p, s, size); +} + +char *nasm_strndup(const char *s, size_t len) +{ + char *p; + + len = strnlen(s, len); + p = nasm_malloc(len+1); + p[len] = '\0'; + return memcpy(p, s, len); +} + +char *nasm_strcat(const char *one, const char *two) +{ + char *rslt; + size_t l1 = strlen(one); + size_t l2 = strlen(two); + rslt = nasm_malloc(l1 + l2 + 1); + memcpy(rslt, one, l1); + memcpy(rslt + l1, two, l2+1); + return rslt; +} diff --git a/nasmlib/nasmlib.c b/nasmlib/nasmlib.c deleted file mode 100644 index be48d7bd..00000000 --- a/nasmlib/nasmlib.c +++ /dev/null @@ -1,635 +0,0 @@ -/* ----------------------------------------------------------------------- * - * - * Copyright 1996-2016 The NASM Authors - All Rights Reserved - * See the file AUTHORS included with the NASM distribution for - * the specific copyright holders. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following - * conditions are met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * ----------------------------------------------------------------------- */ - -/* - * nasmlib.c library routines for the Netwide Assembler - */ - -#include "compiler.h" - -#include -#include -#include -#include -#include - -#include "nasm.h" -#include "nasmlib.h" -#include "insns.h" - -int globalbits = 0; /* defined in nasm.h, works better here for ASM+DISASM */ -vefunc nasm_verror; /* Global error handling function */ - -/* Uninitialized -> all zero by C spec */ -const uint8_t zero_buffer[ZERO_BUF_SIZE]; - -/* - * Prepare a table of tolower() results. This avoids function calls - * on some platforms. - */ - -unsigned char nasm_tolower_tab[256]; - -void tolower_init(void) -{ - int i; - - for (i = 0; i < 256; i++) - nasm_tolower_tab[i] = tolower(i); -} - -void nasm_error(int severity, const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - nasm_verror(severity, fmt, ap); - va_end(ap); -} - -no_return nasm_fatal(int flags, const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - nasm_verror(flags | ERR_FATAL, fmt, ap); - abort(); /* We should never get here */ -} - -no_return nasm_panic(int flags, const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - nasm_verror(flags | ERR_PANIC, fmt, ap); - abort(); /* We should never get here */ -} - -no_return nasm_panic_from_macro(const char *file, int line) -{ - nasm_panic(ERR_NOFILE, "Internal error at %s:%d\n", file, line); -} - -void *nasm_malloc(size_t size) -{ - void *p = malloc(size); - if (!p) - nasm_fatal(ERR_NOFILE, "out of memory"); - return p; -} - -void *nasm_calloc(size_t size, size_t nelem) -{ - void *p = calloc(size, nelem); - if (!p) - nasm_fatal(ERR_NOFILE, "out of memory"); - return p; -} - -void *nasm_zalloc(size_t size) -{ - return nasm_calloc(size, 1); -} - -void *nasm_realloc(void *q, size_t size) -{ - void *p = q ? realloc(q, size) : malloc(size); - if (!p) - nasm_fatal(ERR_NOFILE, "out of memory"); - return p; -} - -void nasm_free(void *q) -{ - if (q) - free(q); -} - -char *nasm_strdup(const char *s) -{ - char *p; - int size = strlen(s) + 1; - - p = malloc(size); - if (!p) - nasm_fatal(ERR_NOFILE, "out of memory"); - strcpy(p, s); - return p; -} - -char *nasm_strndup(const char *s, size_t len) -{ - char *p; - int size = len + 1; - - p = malloc(size); - if (!p) - nasm_fatal(ERR_NOFILE, "out of memory"); - strncpy(p, s, len); - p[len] = '\0'; - return p; -} - -no_return nasm_assert_failed(const char *file, int line, const char *msg) -{ - nasm_fatal(0, "assertion %s failed at %s:%d", msg, file, line); - exit(1); -} - -#ifndef nasm_stricmp -int nasm_stricmp(const char *s1, const char *s2) -{ - unsigned char c1, c2; - int d; - - while (1) { - c1 = nasm_tolower(*s1++); - c2 = nasm_tolower(*s2++); - d = c1-c2; - - if (d) - return d; - if (!c1) - break; - } - return 0; -} -#endif - -#ifndef nasm_strnicmp -int nasm_strnicmp(const char *s1, const char *s2, size_t n) -{ - unsigned char c1, c2; - int d; - - while (n--) { - c1 = nasm_tolower(*s1++); - c2 = nasm_tolower(*s2++); - d = c1-c2; - - if (d) - return d; - if (!c1) - break; - } - return 0; -} -#endif - -int nasm_memicmp(const char *s1, const char *s2, size_t n) -{ - unsigned char c1, c2; - int d; - - while (n--) { - c1 = nasm_tolower(*s1++); - c2 = nasm_tolower(*s2++); - d = c1-c2; - if (d) - return d; - } - return 0; -} - -#ifndef nasm_strsep -char *nasm_strsep(char **stringp, const char *delim) -{ - char *s = *stringp; - char *e; - - if (!s) - return NULL; - - e = strpbrk(s, delim); - if (e) - *e++ = '\0'; - - *stringp = e; - return s; -} -#endif - - -#define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_') - -static int radix_letter(char c) -{ - switch (c) { - case 'b': case 'B': - case 'y': case 'Y': - return 2; /* Binary */ - case 'o': case 'O': - case 'q': case 'Q': - return 8; /* Octal */ - case 'h': case 'H': - case 'x': case 'X': - return 16; /* Hexadecimal */ - case 'd': case 'D': - case 't': case 'T': - return 10; /* Decimal */ - default: - return 0; /* Not a known radix letter */ - } -} - -int64_t readnum(char *str, bool *error) -{ - char *r = str, *q; - int32_t pradix, sradix, radix; - int plen, slen, len; - uint64_t result, checklimit; - int digit, last; - bool warn = false; - int sign = 1; - - *error = false; - - while (nasm_isspace(*r)) - r++; /* find start of number */ - - /* - * If the number came from make_tok_num (as a result of an %assign), it - * might have a '-' built into it (rather than in a preceeding token). - */ - if (*r == '-') { - r++; - sign = -1; - } - - q = r; - - while (lib_isnumchar(*q)) - q++; /* find end of number */ - - len = q-r; - if (!len) { - /* Not numeric */ - *error = true; - return 0; - } - - /* - * Handle radix formats: - * - * 0 - * $ (hexadecimal) - * - */ - pradix = sradix = 0; - plen = slen = 0; - - if (len > 2 && *r == '0' && (pradix = radix_letter(r[1])) != 0) - plen = 2; - else if (len > 1 && *r == '$') - pradix = 16, plen = 1; - - if (len > 1 && (sradix = radix_letter(q[-1])) != 0) - slen = 1; - - if (pradix > sradix) { - radix = pradix; - r += plen; - } else if (sradix > pradix) { - radix = sradix; - q -= slen; - } else { - /* Either decimal, or invalid -- if invalid, we'll trip up - further down. */ - radix = 10; - } - - /* - * `checklimit' must be 2**64 / radix. We can't do that in - * 64-bit arithmetic, which we're (probably) using, so we - * cheat: since we know that all radices we use are even, we - * can divide 2**63 by radix/2 instead. - */ - checklimit = UINT64_C(0x8000000000000000) / (radix >> 1); - - /* - * Calculate the highest allowable value for the last digit of a - * 64-bit constant... in radix 10, it is 6, otherwise it is 0 - */ - last = (radix == 10 ? 6 : 0); - - result = 0; - while (*r && r < q) { - if (*r != '_') { - if (*r < '0' || (*r > '9' && *r < 'A') - || (digit = numvalue(*r)) >= radix) { - *error = true; - return 0; - } - if (result > checklimit || - (result == checklimit && digit >= last)) { - warn = true; - } - - result = radix * result + digit; - } - r++; - } - - if (warn) - nasm_error(ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV, - "numeric constant %s does not fit in 64 bits", - str); - - return result * sign; -} - -int64_t readstrnum(char *str, int length, bool *warn) -{ - int64_t charconst = 0; - int i; - - *warn = false; - - str += length; - if (globalbits == 64) { - for (i = 0; i < length; i++) { - if (charconst & UINT64_C(0xFF00000000000000)) - *warn = true; - charconst = (charconst << 8) + (uint8_t)*--str; - } - } else { - for (i = 0; i < length; i++) { - if (charconst & 0xFF000000UL) - *warn = true; - charconst = (charconst << 8) + (uint8_t)*--str; - } - } - return charconst; -} - -int32_t seg_alloc(void) -{ - static int32_t next_seg = 0; - int32_t this_seg = next_seg; - - next_seg += 2; - - return this_seg; -} - -void standard_extension(char *inname, char *outname, char *extension) -{ - char *p, *q; - - if (*outname) /* file name already exists, */ - return; /* so do nothing */ - q = inname; - p = outname; - while (*q) - *p++ = *q++; /* copy, and find end of string */ - *p = '\0'; /* terminate it */ - while (p > outname && *--p != '.') ; /* find final period (or whatever) */ - if (*p != '.') - while (*p) - p++; /* go back to end if none found */ - if (!strcmp(p, extension)) { /* is the extension already there? */ - if (*extension) - nasm_error(ERR_WARNING | ERR_NOFILE, - "file name already ends in `%s': " - "output will be in `nasm.out'", extension); - else - nasm_error(ERR_WARNING | ERR_NOFILE, - "file name already has no extension: " - "output will be in `nasm.out'"); - strcpy(outname, "nasm.out"); - } else - strcpy(p, extension); -} - -/* - * Common list of prefix names - */ -static const char *prefix_names[] = { - "a16", "a32", "a64", "asp", "lock", "o16", "o32", "o64", "osp", - "rep", "repe", "repne", "repnz", "repz", "times", "wait", - "xacquire", "xrelease", "bnd" -}; - -const char *prefix_name(int token) -{ - unsigned int prefix = token-PREFIX_ENUM_START; - if (prefix >= ARRAY_SIZE(prefix_names)) - return NULL; - - return prefix_names[prefix]; -} - -/* - * Binary search. - */ -int bsi(const char *string, const char **array, int size) -{ - int i = -1, j = size; /* always, i < index < j */ - while (j - i >= 2) { - int k = (i + j) / 2; - int l = strcmp(string, array[k]); - if (l < 0) /* it's in the first half */ - j = k; - else if (l > 0) /* it's in the second half */ - i = k; - else /* we've got it :) */ - return k; - } - return -1; /* we haven't got it :( */ -} - -int bsii(const char *string, const char **array, int size) -{ - int i = -1, j = size; /* always, i < index < j */ - while (j - i >= 2) { - int k = (i + j) / 2; - int l = nasm_stricmp(string, array[k]); - if (l < 0) /* it's in the first half */ - j = k; - else if (l > 0) /* it's in the second half */ - i = k; - else /* we've got it :) */ - return k; - } - return -1; /* we haven't got it :( */ -} - -char *nasm_strcat(const char *one, const char *two) -{ - char *rslt; - int l1 = strlen(one); - rslt = nasm_malloc(l1 + strlen(two) + 1); - strcpy(rslt, one); - strcpy(rslt + l1, two); - return rslt; -} - -/* skip leading spaces */ -char *nasm_skip_spaces(const char *p) -{ - if (p) - while (*p && nasm_isspace(*p)) - p++; - return (char *)p; -} - -/* skip leading non-spaces */ -char *nasm_skip_word(const char *p) -{ - if (p) - while (*p && !nasm_isspace(*p)) - p++; - return (char *)p; -} - -/* zap leading spaces with zero */ -char *nasm_zap_spaces_fwd(char *p) -{ - if (p) - while (*p && nasm_isspace(*p)) - *p++ = 0x0; - return p; -} - -/* zap spaces with zero in reverse order */ -char *nasm_zap_spaces_rev(char *p) -{ - if (p) - while (*p && nasm_isspace(*p)) - *p-- = 0x0; - return p; -} - -/* zap leading and trailing spaces */ -char *nasm_trim_spaces(char *p) -{ - p = nasm_zap_spaces_fwd(p); - nasm_zap_spaces_fwd(nasm_skip_word(p)); - - return p; -} - -/* - * return the word extracted from a stream - * or NULL if nothing left - */ -char *nasm_get_word(char *p, char **tail) -{ - char *word = nasm_skip_spaces(p); - char *next = nasm_skip_word(word); - - if (word && *word) { - if (*next) - *next++ = '\0'; - } else - word = next = NULL; - - /* NOTE: the tail may start with spaces */ - *tail = next; - - return word; -} - -/* - * Extract "opt=val" values from the stream and - * returns "opt" - * - * Exceptions: - * 1) If "=val" passed the NULL returned though - * you may continue handling the tail via "next" - * 2) If "=" passed the NULL is returned and "val" - * is set to NULL as well - */ -char *nasm_opt_val(char *p, char **val, char **next) -{ - char *q, *nxt; - - *val = *next = NULL; - - p = nasm_get_word(p, &nxt); - if (!p) - return NULL; - - q = strchr(p, '='); - if (q) { - if (q == p) - p = NULL; - *q++='\0'; - if (*q) { - *val = q; - } else { - q = nasm_get_word(q + 1, &nxt); - if (q) - *val = q; - } - } else { - q = nasm_skip_spaces(nxt); - if (q && *q == '=') { - q = nasm_get_word(q + 1, &nxt); - if (q) - *val = q; - } - } - - *next = nxt; - return p; -} - -/* - * initialized data bytes length from opcode - */ -int idata_bytes(int opcode) -{ - switch (opcode) { - case I_DB: - return 1; - case I_DW: - return 2; - case I_DD: - return 4; - case I_DQ: - return 8; - case I_DT: - return 10; - case I_DO: - return 16; - case I_DY: - return 32; - case I_DZ: - return 64; - case I_none: - return -1; - default: - return 0; - } -} diff --git a/nasmlib/readnum.c b/nasmlib/readnum.c new file mode 100644 index 00000000..7253c5b0 --- /dev/null +++ b/nasmlib/readnum.c @@ -0,0 +1,171 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 1996-2016 The NASM Authors - All Rights Reserved + * See the file AUTHORS included with the NASM distribution for + * the specific copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following + * conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * ----------------------------------------------------------------------- */ + +/* + * nasmlib.c library routines for the Netwide Assembler + */ + +#include "compiler.h" + +#include + +#include "nasmlib.h" +#include "nasm.h" /* For globalbits */ + +#define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_') + +static int radix_letter(char c) +{ + switch (c) { + case 'b': case 'B': + case 'y': case 'Y': + return 2; /* Binary */ + case 'o': case 'O': + case 'q': case 'Q': + return 8; /* Octal */ + case 'h': case 'H': + case 'x': case 'X': + return 16; /* Hexadecimal */ + case 'd': case 'D': + case 't': case 'T': + return 10; /* Decimal */ + default: + return 0; /* Not a known radix letter */ + } +} + +int64_t readnum(char *str, bool *error) +{ + char *r = str, *q; + int32_t pradix, sradix, radix; + int plen, slen, len; + uint64_t result, checklimit; + int digit, last; + bool warn = false; + int sign = 1; + + *error = false; + + while (nasm_isspace(*r)) + r++; /* find start of number */ + + /* + * If the number came from make_tok_num (as a result of an %assign), it + * might have a '-' built into it (rather than in a preceeding token). + */ + if (*r == '-') { + r++; + sign = -1; + } + + q = r; + + while (lib_isnumchar(*q)) + q++; /* find end of number */ + + len = q-r; + if (!len) { + /* Not numeric */ + *error = true; + return 0; + } + + /* + * Handle radix formats: + * + * 0 + * $ (hexadecimal) + * + */ + pradix = sradix = 0; + plen = slen = 0; + + if (len > 2 && *r == '0' && (pradix = radix_letter(r[1])) != 0) + plen = 2; + else if (len > 1 && *r == '$') + pradix = 16, plen = 1; + + if (len > 1 && (sradix = radix_letter(q[-1])) != 0) + slen = 1; + + if (pradix > sradix) { + radix = pradix; + r += plen; + } else if (sradix > pradix) { + radix = sradix; + q -= slen; + } else { + /* Either decimal, or invalid -- if invalid, we'll trip up + further down. */ + radix = 10; + } + + /* + * `checklimit' must be 2**64 / radix. We can't do that in + * 64-bit arithmetic, which we're (probably) using, so we + * cheat: since we know that all radices we use are even, we + * can divide 2**63 by radix/2 instead. + */ + checklimit = UINT64_C(0x8000000000000000) / (radix >> 1); + + /* + * Calculate the highest allowable value for the last digit of a + * 64-bit constant... in radix 10, it is 6, otherwise it is 0 + */ + last = (radix == 10 ? 6 : 0); + + result = 0; + while (*r && r < q) { + if (*r != '_') { + if (*r < '0' || (*r > '9' && *r < 'A') + || (digit = numvalue(*r)) >= radix) { + *error = true; + return 0; + } + if (result > checklimit || + (result == checklimit && digit >= last)) { + warn = true; + } + + result = radix * result + digit; + } + r++; + } + + if (warn) + nasm_error(ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV, + "numeric constant %s does not fit in 64 bits", + str); + + return result * sign; +} diff --git a/nasmlib/string.c b/nasmlib/string.c new file mode 100644 index 00000000..907df32f --- /dev/null +++ b/nasmlib/string.c @@ -0,0 +1,242 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 1996-2016 The NASM Authors - All Rights Reserved + * See the file AUTHORS included with the NASM distribution for + * the specific copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following + * conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * ----------------------------------------------------------------------- */ + +/* + * nasmlib.c library routines for the Netwide Assembler + */ + +#include "compiler.h" + +#include +#include + +#include "nasmlib.h" + +/* + * Prepare a table of tolower() results. This avoids function calls + * on some platforms. + */ + +unsigned char nasm_tolower_tab[256]; + +void tolower_init(void) +{ + int i; + + for (i = 0; i < 256; i++) + nasm_tolower_tab[i] = tolower(i); +} + +#ifndef nasm_stricmp +int nasm_stricmp(const char *s1, const char *s2) +{ + unsigned char c1, c2; + int d; + + while (1) { + c1 = nasm_tolower(*s1++); + c2 = nasm_tolower(*s2++); + d = c1-c2; + + if (d) + return d; + if (!c1) + break; + } + return 0; +} +#endif + +#ifndef nasm_strnicmp +int nasm_strnicmp(const char *s1, const char *s2, size_t n) +{ + unsigned char c1, c2; + int d; + + while (n--) { + c1 = nasm_tolower(*s1++); + c2 = nasm_tolower(*s2++); + d = c1-c2; + + if (d) + return d; + if (!c1) + break; + } + return 0; +} +#endif + +int nasm_memicmp(const char *s1, const char *s2, size_t n) +{ + unsigned char c1, c2; + int d; + + while (n--) { + c1 = nasm_tolower(*s1++); + c2 = nasm_tolower(*s2++); + d = c1-c2; + if (d) + return d; + } + return 0; +} + +#ifndef nasm_strsep +char *nasm_strsep(char **stringp, const char *delim) +{ + char *s = *stringp; + char *e; + + if (!s) + return NULL; + + e = strpbrk(s, delim); + if (e) + *e++ = '\0'; + + *stringp = e; + return s; +} +#endif + +/* skip leading spaces */ +char *nasm_skip_spaces(const char *p) +{ + if (p) + while (*p && nasm_isspace(*p)) + p++; + return (char *)p; +} + +/* skip leading non-spaces */ +char *nasm_skip_word(const char *p) +{ + if (p) + while (*p && !nasm_isspace(*p)) + p++; + return (char *)p; +} + +/* zap leading spaces with zero */ +char *nasm_zap_spaces_fwd(char *p) +{ + if (p) + while (*p && nasm_isspace(*p)) + *p++ = 0x0; + return p; +} + +/* zap spaces with zero in reverse order */ +char *nasm_zap_spaces_rev(char *p) +{ + if (p) + while (*p && nasm_isspace(*p)) + *p-- = 0x0; + return p; +} + +/* zap leading and trailing spaces */ +char *nasm_trim_spaces(char *p) +{ + p = nasm_zap_spaces_fwd(p); + nasm_zap_spaces_fwd(nasm_skip_word(p)); + + return p; +} + +/* + * return the word extracted from a stream + * or NULL if nothing left + */ +char *nasm_get_word(char *p, char **tail) +{ + char *word = nasm_skip_spaces(p); + char *next = nasm_skip_word(word); + + if (word && *word) { + if (*next) + *next++ = '\0'; + } else + word = next = NULL; + + /* NOTE: the tail may start with spaces */ + *tail = next; + + return word; +} + +/* + * Extract "opt=val" values from the stream and + * returns "opt" + * + * Exceptions: + * 1) If "=val" passed the NULL returned though + * you may continue handling the tail via "next" + * 2) If "=" passed the NULL is returned and "val" + * is set to NULL as well + */ +char *nasm_opt_val(char *p, char **val, char **next) +{ + char *q, *nxt; + + *val = *next = NULL; + + p = nasm_get_word(p, &nxt); + if (!p) + return NULL; + + q = strchr(p, '='); + if (q) { + if (q == p) + p = NULL; + *q++='\0'; + if (*q) { + *val = q; + } else { + q = nasm_get_word(q + 1, &nxt); + if (q) + *val = q; + } + } else { + q = nasm_skip_spaces(nxt); + if (q && *q == '=') { + q = nasm_get_word(q + 1, &nxt); + if (q) + *val = q; + } + } + + *next = nxt; + return p; +} diff --git a/nasmlib/zerobuf.c b/nasmlib/zerobuf.c new file mode 100644 index 00000000..651c0fd4 --- /dev/null +++ b/nasmlib/zerobuf.c @@ -0,0 +1,42 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 1996-2016 The NASM Authors - All Rights Reserved + * See the file AUTHORS included with the NASM distribution for + * the specific copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following + * conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * ----------------------------------------------------------------------- */ + +/* + * nasmlib.c library routines for the Netwide Assembler + */ + +#include "compiler.h" +#include "nasmlib.h" + +/* Uninitialized -> all zero by C spec */ +const uint8_t zero_buffer[ZERO_BUF_SIZE]; diff --git a/rdstrnum.c b/rdstrnum.c new file mode 100644 index 00000000..d9d2a69f --- /dev/null +++ b/rdstrnum.c @@ -0,0 +1,68 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 1996-2016 The NASM Authors - All Rights Reserved + * See the file AUTHORS included with the NASM distribution for + * the specific copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following + * conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * ----------------------------------------------------------------------- */ + +/* + * rdstrnum.c + * + * This converts a NASM string to an integer, used when a string + * is used in an integer constant context. This is a binary conversion, + * not a conversion from a numeric constant in text form. + */ + +#include "compiler.h" +#include "nasmlib.h" +#include "nasm.h" + +int64_t readstrnum(char *str, int length, bool *warn) +{ + int64_t charconst = 0; + int i; + + *warn = false; + + str += length; + if (globalbits == 64) { + for (i = 0; i < length; i++) { + if (charconst & UINT64_C(0xFF00000000000000)) + *warn = true; + charconst = (charconst << 8) + (uint8_t)*--str; + } + } else { + for (i = 0; i < length; i++) { + if (charconst & 0xFF000000UL) + *warn = true; + charconst = (charconst << 8) + (uint8_t)*--str; + } + } + return charconst; +} diff --git a/segalloc.c b/segalloc.c new file mode 100644 index 00000000..60596221 --- /dev/null +++ b/segalloc.c @@ -0,0 +1,51 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 1996-2016 The NASM Authors - All Rights Reserved + * See the file AUTHORS included with the NASM distribution for + * the specific copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following + * conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * ----------------------------------------------------------------------- */ + +/* + * nasmlib.c library routines for the Netwide Assembler + */ + +#include "compiler.h" +#include "nasm.h" +#include "nasmlib.h" +#include "insns.h" + +int32_t seg_alloc(void) +{ + static int32_t next_seg = 0; + int32_t this_seg = next_seg; + + next_seg += 2; + + return this_seg; +} diff --git a/stdlib/strnlen.c b/stdlib/strnlen.c new file mode 100644 index 00000000..40eacf54 --- /dev/null +++ b/stdlib/strnlen.c @@ -0,0 +1,46 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 1996-2016 The NASM Authors - All Rights Reserved + * See the file AUTHORS included with the NASM distribution for + * the specific copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following + * conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * ----------------------------------------------------------------------- */ + +#include "compiler.h" +#include + +#ifndef HAVE_STRNLEN + +size_t strnlen(const char *s, size_t maxlen) +{ + const char *end = memchr(s, 0, maxlen); + + return end ? (size_t)(end - s) : maxlen; +} + +#endif -- 2.11.4.GIT