From 8ae435549d73404f1c2d66040ea707027dca726f Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 23 Dec 2023 20:45:30 +0100 Subject: [PATCH] added -slabshigh N and -slabsgolden for memory() slabs --- .../src/prog8/code/core/CompilationOptions.kt | 2 + .../codegen/cpu6502/ProgramAndVarsGen.kt | 43 ++++++++++++++++--- compiler/src/prog8/CompilerMain.kt | 25 ++++++++++- compiler/src/prog8/compiler/Compiler.kt | 4 ++ compiler/test/TestCompilerOnExamples.kt | 2 + compiler/test/TestCompilerOptionLibdirs.kt | 2 + compiler/test/helpers/compileXyz.kt | 2 + docs/source/todo.rst | 1 - examples/test.p8 | 20 +++------ .../src/prog8/http/TestHttp.kt | 4 +- 10 files changed, 79 insertions(+), 26 deletions(-) diff --git a/codeCore/src/prog8/code/core/CompilationOptions.kt b/codeCore/src/prog8/code/core/CompilationOptions.kt index b81f642ed..a47deac2c 100644 --- a/codeCore/src/prog8/code/core/CompilationOptions.kt +++ b/codeCore/src/prog8/code/core/CompilationOptions.kt @@ -22,6 +22,8 @@ class CompilationOptions(val output: OutputType, var experimentalCodegen: Boolean = false, var varsHighBank: Int? = null, var varsGolden: Boolean = false, + var slabsHighBank: Int? = null, + var slabsGolden: Boolean = false, var splitWordArrays: Boolean = false, var breakpointCpuInstruction: Boolean = false, var outputDir: Path = Path(""), diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt index 88779dfad..273515355 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt @@ -174,6 +174,7 @@ internal class ProgramAndVarsGen( private fun footer() { var relocateBssVars = false + var relocateBssSlabs = false var relocatedBssStart = 0u var relocatedBssEnd = 0u @@ -193,33 +194,61 @@ internal class ProgramAndVarsGen( options.compTarget.machine.BSSHIGHRAM_END <= options.compTarget.machine.BSSHIGHRAM_START) { throw AssemblyError("current compilation target hasn't got the high ram area properly defined or it is simply not available") } + if(options.slabsHighBank!=null && options.varsHighBank!=options.slabsHighBank) + throw AssemblyError("slabs and vars high bank must be the same") relocateBssVars = true relocatedBssStart = options.compTarget.machine.BSSHIGHRAM_START relocatedBssEnd = options.compTarget.machine.BSSHIGHRAM_END } + if(options.slabsGolden) { + if(options.compTarget.machine.BSSGOLDENRAM_START == 0u || + options.compTarget.machine.BSSGOLDENRAM_END == 0u || + options.compTarget.machine.BSSGOLDENRAM_END <= options.compTarget.machine.BSSGOLDENRAM_START) { + throw AssemblyError("current compilation target hasn't got the golden ram area properly defined or it is simply not available") + } + relocateBssSlabs = true + relocatedBssStart = options.compTarget.machine.BSSGOLDENRAM_START + relocatedBssEnd = options.compTarget.machine.BSSGOLDENRAM_END + } + else if(options.slabsHighBank!=null) { + if(options.compTarget.machine.BSSHIGHRAM_START == 0u || + options.compTarget.machine.BSSHIGHRAM_END == 0u || + options.compTarget.machine.BSSHIGHRAM_END <= options.compTarget.machine.BSSHIGHRAM_START) { + throw AssemblyError("current compilation target hasn't got the high ram area properly defined or it is simply not available") + } + if(options.varsHighBank!=null && options.varsHighBank!=options.slabsHighBank) + throw AssemblyError("slabs and vars high bank must be the same") + relocateBssSlabs = true + relocatedBssStart = options.compTarget.machine.BSSHIGHRAM_START + relocatedBssEnd = options.compTarget.machine.BSSHIGHRAM_END + } + asmgen.out("; bss sections") asmgen.out("PROG8_VARSHIGH_RAMBANK = ${options.varsHighBank ?: 1}") if(relocateBssVars) { - // BSS vars in another ram area, memory() slabs just concatenated at the end of the program. - if(symboltable.allMemorySlabs.isNotEmpty()) { + if(!relocateBssSlabs) asmgen.out(" .dsection slabs_BSS") - } asmgen.out("prog8_program_end\t; end of program label for progend()") asmgen.out(" * = ${relocatedBssStart.toHex()}") asmgen.out("prog8_bss_section_start") asmgen.out(" .dsection BSS") - asmgen.out(" .cerror * > ${relocatedBssEnd.toHex()}, \"too many variables for BSS section\"") + if(relocateBssSlabs) + asmgen.out(" .dsection slabs_BSS") + asmgen.out(" .cerror * > ${relocatedBssEnd.toHex()}, \"too many variables/data for BSS section\"") asmgen.out("prog8_bss_section_size = * - prog8_bss_section_start") } else { - // just append BSS vars followed by memory() slabs, concatenated at the end of the program. asmgen.out("prog8_bss_section_start") asmgen.out(" .dsection BSS") asmgen.out("prog8_bss_section_size = * - prog8_bss_section_start") - if(symboltable.allMemorySlabs.isNotEmpty()) { + if(!relocateBssSlabs) asmgen.out(" .dsection slabs_BSS") - } asmgen.out("prog8_program_end\t; end of program label for progend()") + if(relocateBssSlabs) { + asmgen.out(" * = ${relocatedBssStart.toHex()}") + asmgen.out(" .dsection slabs_BSS") + asmgen.out(" .cerror * > ${relocatedBssEnd.toHex()}, \"too many data for slabs_BSS section\"") + } } } diff --git a/compiler/src/prog8/CompilerMain.kt b/compiler/src/prog8/CompilerMain.kt index 04fe2a5b7..cfb79679f 100644 --- a/compiler/src/prog8/CompilerMain.kt +++ b/compiler/src/prog8/CompilerMain.kt @@ -55,6 +55,8 @@ private fun compileMain(args: Array): Boolean { val watchMode by cli.option(ArgType.Boolean, fullName = "watch", description = "continuous compilation mode (watch for file changes)") val varsGolden by cli.option(ArgType.Boolean, fullName = "varsgolden", description = "put uninitialized variables in 'golden ram' memory area instead of at the end of the program. On the cx16 target this is $0400-07ff. This is unavailable on other systems.") val varsHighBank by cli.option(ArgType.Int, fullName = "varshigh", description = "put uninitialized variables in high memory area instead of at the end of the program. On the cx16 target the value specifies the HiRAM bank to use, on other systems this value is ignored.") + val slabsGolden by cli.option(ArgType.Boolean, fullName = "slabsgolden", description = "put memory() slabs in 'golden ram' memory area instead of at the end of the program. On the cx16 target this is $0400-07ff. This is unavailable on other systems.") + val slabsHighBank by cli.option(ArgType.Int, fullName = "slabshigh", description = "put memory() slabs in high memory area instead of at the end of the program. On the cx16 target the value specifies the HiRAM bank to use, on other systems this value is ignored.") val moduleFiles by cli.argument(ArgType.String, fullName = "modules", description = "main module file(s) to compile").multiple(999) try { @@ -102,11 +104,26 @@ private fun compileMain(args: Array): Boolean { System.err.println("Golden Ram is only available on the Commander X16 target.") return false } - if (varsHighBank!=null) { - System.err.println("Either use varsgolden or varshigh, not both.") + if (varsHighBank!=null || slabsHighBank!=null) { + System.err.println("Either use varsgolden or varshigh (and slabsgolden or slabshigh), not both or mixed.") return false } } + if (slabsGolden==true) { + if (compilationTarget != Cx16Target.NAME) { + System.err.println("Golden Ram is only available on the Commander X16 target.") + return false + } + if (varsHighBank!=null || slabsHighBank!=null) { + System.err.println("Either use golden or high ram, not both.") + return false + } + } + + if(varsHighBank!=null && slabsHighBank!=null && varsHighBank!=slabsHighBank) { + System.err.println("Vars and slabs high memory bank must be the same.") + return false + } if(startVm==true) { return runVm(moduleFiles.first()) @@ -134,6 +151,8 @@ private fun compileMain(args: Array): Boolean { experimentalCodegen == true, varsHighBank, varsGolden == true, + slabsHighBank, + slabsGolden == true, compilationTarget!!, splitWordArrays == true, breakpointCpuInstruction = false, @@ -204,6 +223,8 @@ private fun compileMain(args: Array): Boolean { experimentalCodegen == true, varsHighBank, varsGolden == true, + slabsHighBank, + slabsGolden == true, compilationTarget!!, splitWordArrays == true, breakpointCpuInstruction == true, diff --git a/compiler/src/prog8/compiler/Compiler.kt b/compiler/src/prog8/compiler/Compiler.kt index fa5ea70c6..3551ebbe1 100644 --- a/compiler/src/prog8/compiler/Compiler.kt +++ b/compiler/src/prog8/compiler/Compiler.kt @@ -37,6 +37,8 @@ class CompilerArguments(val filepath: Path, val experimentalCodegen: Boolean, val varsHighBank: Int?, val varsGolden: Boolean, + val slabsHighBank: Int?, + val slabsGolden: Boolean, val compilationTarget: String, val splitWordArrays: Boolean, val breakpointCpuInstruction: Boolean, @@ -79,6 +81,8 @@ fun compileProgram(args: CompilerArguments): CompilationResult? { breakpointCpuInstruction = args.breakpointCpuInstruction varsHighBank = args.varsHighBank varsGolden = args.varsGolden + slabsHighBank = args.slabsHighBank + slabsGolden = args.slabsGolden splitWordArrays = args.splitWordArrays outputDir = args.outputDir.normalize() symbolDefs = args.symbolDefs diff --git a/compiler/test/TestCompilerOnExamples.kt b/compiler/test/TestCompilerOnExamples.kt index f03c6536b..778212b25 100644 --- a/compiler/test/TestCompilerOnExamples.kt +++ b/compiler/test/TestCompilerOnExamples.kt @@ -34,6 +34,8 @@ private fun compileTheThing(filepath: Path, optimize: Boolean, target: ICompilat experimentalCodegen = false, varsHighBank = null, varsGolden = false, + slabsHighBank = null, + slabsGolden = false, compilationTarget = target.name, splitWordArrays = false, breakpointCpuInstruction = false, diff --git a/compiler/test/TestCompilerOptionLibdirs.kt b/compiler/test/TestCompilerOptionLibdirs.kt index ec613da6e..60c9d69dd 100644 --- a/compiler/test/TestCompilerOptionLibdirs.kt +++ b/compiler/test/TestCompilerOptionLibdirs.kt @@ -32,6 +32,8 @@ class TestCompilerOptionSourcedirs: FunSpec({ experimentalCodegen = false, varsHighBank = null, varsGolden = false, + slabsHighBank = null, + slabsGolden = false, compilationTarget = Cx16Target.NAME, splitWordArrays = false, breakpointCpuInstruction = false, diff --git a/compiler/test/helpers/compileXyz.kt b/compiler/test/helpers/compileXyz.kt index b46861de9..27fb66d4c 100644 --- a/compiler/test/helpers/compileXyz.kt +++ b/compiler/test/helpers/compileXyz.kt @@ -31,6 +31,8 @@ internal fun compileFile( experimentalCodegen = false, varsHighBank = null, varsGolden = false, + slabsHighBank = null, + slabsGolden = false, platform.name, symbolDefs = emptyMap(), outputDir = outputDir, diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 28a5a60b5..d3dd9dc1b 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -2,7 +2,6 @@ TODO ==== -- add -slabshigh and -slabsgold to also put the memory() slabs into these memory areas (they're now still always part of the prg itself) - add -nowarnunused, or %option ignore_unused (module scope), to suppress all warnings about unused symbols. Useful in libraries. put this in all library code, like %option no_symbol_prefixing, and get rid of the "trick" it uses currently to suppress unused symbol warnings for library modules. diff --git a/examples/test.p8 b/examples/test.p8 index eff3792b8..eef353224 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,24 +1,14 @@ %import textio -%zeropage basicsafe +%zeropage dontuse main { sub start() { - ubyte[] barr = [1,2,3,4,5,6,7,8,9] - uword[] warr = [111,222,333,444,555,666,777,888,999] - uword pointer = &barr - byte index = 2 + uword empty + uword block = memory("block", 500, 0) - txt.print_ub(barr[7]) + txt.print_uwhex(&empty, true) txt.nl() - txt.print_ub(barr[-2]) - txt.nl() - txt.print_ub(pointer[7]) - txt.nl() - txt.print_ub(pointer[index]) - txt.nl() - txt.print_uw(warr[7]) - txt.nl() - txt.print_uw(warr[-2]) + txt.print_uwhex(block, true) txt.nl() } } diff --git a/httpCompilerService/src/prog8/http/TestHttp.kt b/httpCompilerService/src/prog8/http/TestHttp.kt index b8e745edb..85b589d8e 100644 --- a/httpCompilerService/src/prog8/http/TestHttp.kt +++ b/httpCompilerService/src/prog8/http/TestHttp.kt @@ -44,7 +44,9 @@ class RequestParser : Take { splitWordArrays = false, breakpointCpuInstruction = false, varsHighBank = null, - varsGolden = false + varsGolden = false, + slabsHighBank = null, + slabsGolden = false ) compileProgram(args) return RsJson(Jsonding())