From 5ce5bc44b8feaa666e9f73d5416a6a772cba8594 Mon Sep 17 00:00:00 2001 From: slederer Date: Sat, 9 Nov 2024 19:03:09 +0100 Subject: [PATCH 1/3] sasm: use filesize() for incbin directive --- pcomp/sasm.pas | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pcomp/sasm.pas b/pcomp/sasm.pas index 077eebc..13af658 100644 --- a/pcomp/sasm.pas +++ b/pcomp/sasm.pas @@ -2041,17 +2041,20 @@ begin errorExit2('Unrecognized directive', lastToken.tokenText); end; -procedure readImage(filename:string;size:integer); +procedure readImage(filename:string); var f:InputFileType; c:char; i:integer; + size:integer; begin + openFileWithDefault(f, filename); + size := filesize(f); + pc := pc + size; bytesCount := bytesCount + size; if pass = 2 then begin - openFileWithDefault(f, filename); for i := 1 to size do begin @@ -2059,14 +2062,14 @@ begin write(outfile,c); end; - close(f); end; + + close(f); end; procedure parseMetaDirective; var filename:string; sym:TreeDataRef; - size:integer; begin readNextToken; if lastToken.tokenText = '%INCLUDE' then @@ -2099,8 +2102,7 @@ begin begin filename := curToken.tokenText; readNextToken; - size := parseExpression; - readImage(filename, size); + readImage(filename); end else errorExit2('Invalid meta directive', lastToken.tokenText); From 91cb059f38e21b195c89357015fba686403ee019 Mon Sep 17 00:00:00 2001 From: slederer Date: Sat, 9 Nov 2024 22:22:48 +0100 Subject: [PATCH 2/3] use precompiled standard library (not really a linker) --- .gitignore | 1 + lib/float32.s | 13 +++++++++++++ lib/runtime.s | 40 +++++++++++++++++++++++++++++++++++++--- lib/stdlibwrap.s | 11 +++++++++++ pcomp/emit.pas | 27 ++++++++++++++++----------- pcomp/lsymgen.pas | 24 ++++++++++++++++++++++-- pcomp/make.bat | 7 ++++--- utils/tdrimg.py | 4 +--- 8 files changed, 105 insertions(+), 22 deletions(-) create mode 100644 lib/stdlibwrap.s diff --git a/.gitignore b/.gitignore index 8c4319b..100c169 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ lib/stdlib.s tests/*.s examples/*.s !runtime.s +!stdlibwrap.s *.o *.exe *.bin diff --git a/lib/float32.s b/lib/float32.s index 4b0ad9c..880eaff 100644 --- a/lib/float32.s +++ b/lib/float32.s @@ -1051,3 +1051,16 @@ FLOAT32_ERR_TRUNC: .BYTE "integer overflow",0 FLOAT32_ERR_DIVZ: .BYTE "float division by zero",0 + +%export _ADDFLOAT32 +%export _CMPFLOAT32 +%export _CMPINTFLOAT32 +%export _DIVFLOAT32 +%export _FRACTFLOAT32 +%export _GETFLOAT32EXP +%export _INTFLOAT32 +%export _INTTOFLOAT32 +%export _MULFLOAT32 +%export _NEGFLOAT32 +%export _SUBFLOAT32 +%export _TRUNCFLOAT32 diff --git a/lib/runtime.s b/lib/runtime.s index e7c43ce..f4ad875 100644 --- a/lib/runtime.s +++ b/lib/runtime.s @@ -387,7 +387,7 @@ READCHANW_XT: ; --- check if a string is already initialized ; parameters: addr -; returns: 1 if it is initialied, 0 otherwise +; returns: 1 if it is initialized, 0 otherwise _STRINGISINITED: LOADI.S1.X2Y ; load length field, keep addr CBRANCH.NZ STRIN_XT1 ; if not zero, it is already inited @@ -1616,7 +1616,7 @@ MEM_FREE_XT: ; Since the return stack is no longer valid afterwards, directly ; jumps to _MAIN instead of using RET. -; parameters: [ start of heap address ] +; parameters: [ _MAIN entry point, start of heap address ] _MEM_INIT: ; initialize anchor chunk with start of heap address ; and heap size - header size @@ -1663,7 +1663,9 @@ _MEM_INIT: LOADCP _MEM_FREE ; add chunk to free list CALL - LOADCP _MAIN + ; get address of _MAIN from program header + LOADCP _MAIN_PTR + LOADI JUMP ; allocate a string with MEM_ALLOC. @@ -1937,3 +1939,35 @@ NEWLINESTR: .BYTE 13,10 .CPOOL + +%export _APPENDSTRING +%export _ARRAYTOSET +%export _BOUNDSCHECK +%export _CHARTOSTRING +%export _CHECK_ALLOC +%export _CLEARBIT +%export _CLEARMEM +%export _CMPSTRING +%export _CMPSTRINGL +%export _CMPWORDS +%export _COPYSTRING +%export _COPYWORDS +%export _ENUMCHECK +%export _INDEXSTRING +%export _INITSTRING +%export _INITSTRINGF +%export _INITSTRINGFROM +%export _ISCHARINSTRING +%export _ISINTINARRAY +%export _MEM_ALLOC +%export _MEM_INIT +%export _MEM_FREE +%export _RANGECHECK +%export _RUNTIME_ERR +%export _SETBIT +%export _SETSTRINGCHAR +%export _SETSTRINGLENGTH +%export _STRINGTOCHAR +%export _STRING_ALLOC +%export _TESTBIT +%export MEM_DUMP diff --git a/lib/stdlibwrap.s b/lib/stdlibwrap.s new file mode 100644 index 0000000..4a4269c --- /dev/null +++ b/lib/stdlibwrap.s @@ -0,0 +1,11 @@ + + .ORG 24576 + 32 + .EQU _HEAP_SZ_PTR 24576 + 4 + .EQU _STACK_SZ_PTR 24576 + 8 + .EQU _MAIN_PTR 24576 + 12 + + %include "coreloader.lsym" + %include "runtime.s" + %include "float32.s" + %include "stdlib.s" +_MAIN: diff --git a/pcomp/emit.pas b/pcomp/emit.pas index ed9fa33..2ede4f0 100644 --- a/pcomp/emit.pas +++ b/pcomp/emit.pas @@ -86,6 +86,12 @@ begin countIns(1); end; +procedure emitInclude(s:string); +begin + writeln(outfile, '%include "',s,'"'); + emitIns('.CPOOL'); +end; + procedure emitPrologue; begin writeln(outfile, #9, '.ORG ', startAddress); @@ -95,12 +101,21 @@ begin emitIns2Int('.WORD', defaultHeapSize); emitLabelRaw('_STACK_SZ_PTR'); emitIns2Int('.WORD', defaultStackSize); - emitIns2Int('.WORD', 0); + emitLabelRaw('_MAIN_PTR'); + emitIns2('.WORD', '_MAIN'); emitIns2('LOADCP','_END'); (* end of program is start of heap *) emitIns2('LOADCP', '_MEM_INIT'); (* MEM_INIT initializes heap and sets FP/RP *) (* since RP is not initialized yet, we cannot use CALL and MEM_INIT jumps to _MAIN after it is done *) emitIns('JUMP'); + emitIns2('BRANCH','@+2'); (* NOP, to make alignment explicit *) + emitIns('.CPOOL'); (* header/prologue + 2 constants is 32 bytes *) + + if useStdlib then + begin + writeln(outfile, '%include "stdlib.lsym"'); + writeln(outfile, '%incbin "stdlib.lib"'); + end; end; function bytes2words(size:integer):integer; @@ -274,12 +289,6 @@ begin end; end; -procedure emitInclude(s:string); -begin - writeln(outfile, '%include "',s,'"'); - emitIns('.CPOOL'); -end; - procedure emitUnitEpilogue; begin emitIns('.CPOOL'); @@ -307,10 +316,6 @@ begin else emitInclude('coreloader.lsym'); - emitInclude('float32.lib'); - emitInclude('runtime.lib'); - emitInclude('stdlib.lib'); - rewindStringList(usedUnits); while nextStringListItem(usedUnits, unitName) do emitInclude(unitName + UnitSuffix2); diff --git a/pcomp/lsymgen.pas b/pcomp/lsymgen.pas index 619129e..e120ca6 100644 --- a/pcomp/lsymgen.pas +++ b/pcomp/lsymgen.pas @@ -9,7 +9,8 @@ var outfile:TextFile; infile:TextFile; lineno:integer; - + infilePath,outfilePath:string; + procedure errorExit2(reason:string;arg:string); forward; {$I 'platfile+.pas'} @@ -78,6 +79,17 @@ begin clean := (not strcontains( name, '_')) and (name[1] <> '='); end; +procedure removeLeadingZeroes(var s:string); +begin + while length(s) > 1 do + begin + if s[1] = '0' then + delete(s,1,1) + else + break; + end; +end; + procedure processFile(inpath,outpath:string); var line:string; addr,name:string; @@ -94,6 +106,8 @@ begin begin readln(infile, line); splitLine(line, addr, name, clean); + removeLeadingZeroes(addr); + if clean then writeln(outfile, #9, '.EQU ', name, ' $', addr); end; @@ -104,7 +118,13 @@ end; begin if ParamCount > 0 then begin - processFile(ParamStr(1), getOutfileName(ParamStr(1))); + infilePath := ParamStr(1); + outfilePath := getOutfileName(infilePath); + + if ParamCount > 1 then + outfilePath := ParamStr(2); + + processFile(infilePath, outfilePath); end else writeln('No file name given.'); diff --git a/pcomp/make.bat b/pcomp/make.bat index 40e8eee..5cf30e9 100644 --- a/pcomp/make.bat +++ b/pcomp/make.bat @@ -9,9 +9,10 @@ fpc -gl lsymgen.pas sasm ..\lib\coreloader.s lsymgen ..\lib\coreloader.sym py pcomp.py -n ..\lib\stdlib.pas -libgen ..\lib\stdlib.s -libgen ..\lib\runtime.s -libgen ..\lib\float32.s +sasm ..\lib\stdlibwrap.s ..\lib\stdlib.lib +lsymgen ..\lib\stdlibwrap.sym ..\lib\stdlib.lsym + +rem exit /b py pcomp.py sasm.pas py pcomp.py pcomp.pas diff --git a/utils/tdrimg.py b/utils/tdrimg.py index bb380ca..38308ff 100644 --- a/utils/tdrimg.py +++ b/utils/tdrimg.py @@ -457,15 +457,13 @@ def create_image_with_stuff(): slotnr = putfile("../lib/coreloader.lsym", "coreloader.lsym", f, part, partstart, slotnr) slotnr = putfile("../lib/coreloader.prog", "coreloader.prog", f, part, partstart, slotnr) - slotnr = putfile("../lib/float32.lib", "float32.lib", f, part, partstart, slotnr) - slotnr = putfile("../lib/runtime.lib", "runtime.lib", f, part, partstart, slotnr) slotnr = putfile("../lib/stdlib.lib", None, f, part, partstart, slotnr) slotnr = putfile("../lib/stdlib.inc", None, f, part, partstart, slotnr) + slotnr = putfile("../lib/stdlib.lsym", None, f, part, partstart, slotnr) slotnr = putfile("../pcomp/sasm.prog", None , f, part, partstart, slotnr) slotnr = putfile("../pcomp/pcomp.prog", None , f, part, partstart, slotnr) slotnr = putfile("../pcomp/lsymgen.prog", None , f, part, partstart, slotnr) - slotnr = putfile("../pcomp/libgen.prog", None , f, part, partstart, slotnr) slotnr = putfile("../progs/reclaim.prog", None , f, part, partstart, slotnr) slotnr = putfile("../progs/dumpdir.prog", None , f, part, partstart, slotnr) From fbc72c54388ba9cd53f01ea678361e35c77ae02a Mon Sep 17 00:00:00 2001 From: slederer Date: Sat, 9 Nov 2024 23:02:39 +0100 Subject: [PATCH 3/3] sasm: show filename when processing INCBIN libgen: renamed --- pcomp/sasm.pas | 16 +++++++++++++--- pcomp/{libgen.pas => shortgen.pas} | 0 2 files changed, 13 insertions(+), 3 deletions(-) rename pcomp/{libgen.pas => shortgen.pas} (100%) diff --git a/pcomp/sasm.pas b/pcomp/sasm.pas index 13af658..ab5230b 100644 --- a/pcomp/sasm.pas +++ b/pcomp/sasm.pas @@ -372,12 +372,17 @@ procedure printPassNo; begin write('P', pass, ' '); end; - -procedure printCurrentLineno; +procedure printFilename(var f:string); begin write(#13); printPassNo; - write(filename, ' ', lineno); + write(f); +end; + +procedure printCurrentLineno; +begin + printFilename(filename); + write(' ', lineno); ClrEol; end; @@ -2047,6 +2052,9 @@ var f:InputFileType; i:integer; size:integer; begin + printFilename(filename); + ClrEol; + openFileWithDefault(f, filename); size := filesize(f); @@ -2065,6 +2073,8 @@ begin end; close(f); + + printCurrentLineno; end; procedure parseMetaDirective; diff --git a/pcomp/libgen.pas b/pcomp/shortgen.pas similarity index 100% rename from pcomp/libgen.pas rename to pcomp/shortgen.pas