From a1795d9b1fb90a7409903639c98de73f3f6160b5 Mon Sep 17 00:00:00 2001 From: slederer Date: Tue, 1 Apr 2025 00:14:20 +0200 Subject: [PATCH] implement outward calling of nested procedures, fix standalone mode --- pcomp/emit.pas | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/pcomp/emit.pas b/pcomp/emit.pas index 3c908c4..a201714 100644 --- a/pcomp/emit.pas +++ b/pcomp/emit.pas @@ -111,7 +111,7 @@ begin emitIns2('BRANCH','@+2'); (* NOP, to make alignment explicit *) emitIns('.CPOOL'); (* header/prologue + 2 constants is 32 bytes *) - if useStdlib then + if useStdlib and not useStandalone then begin writeln(outfile, '%include "stdlib.lsym"'); writeln(outfile, '%incbin "stdlib.lib"'); @@ -312,7 +312,12 @@ begin emitArrayConsts; if useStandalone then - emitInclude('corelib.s') + begin + emitInclude('corelib.s'); + emitInclude('runtime.s'); + emitInclude('float32.s'); + emitInclude('stdlib.s'); + end else emitInclude('coreloader.lsym'); @@ -1059,6 +1064,7 @@ end; *) procedure emitProcedureCall(aProc: ProcRef); var procLabel: IdentString; + i:integer; begin (* pass pointer to stackframe of caller for nested procedures *) if aProc^.isNested then @@ -1069,9 +1075,17 @@ begin if aProc^.level > curProcedure^.level then emitIns2('LOADREG','FP') else - (* TODO: calling nested aProc with a lower nesting level. - need to chase a chain of old BP pointers. *) - errorExit2('internal error: outward call of nested aProc not implemented', ''); + begin + (* Calling nested aProc with a lower nesting level. + Need to chase a chain of old BP pointers. *) + emitIns2('LOADREG', 'BP'); + (* BP points to the stackframe of the outer procedure, + @BP contains the stackframe of the procedure one step further + outwards, and so on. + *) + for i := aProc^.level + 1 to curProcedure^.level do + emitIns('LOADI'); + end; end; emitFpAdjust(-curProcedure^.tempsSize);