From 5522a305ab5972bdd9eec07bcff5f2091a89539f Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 10 Feb 2024 18:42:31 +0100 Subject: [PATCH] add -dumpvars option to dump all allocated variables (zp, normal ram, etc) --- .../src/prog8/code/core/CompilationOptions.kt | 1 + .../src/prog8/codegen/cpu6502/AsmGen.kt | 46 +++++++++++++++++++ compiler/src/prog8/CompilerMain.kt | 3 ++ compiler/src/prog8/compiler/Compiler.kt | 2 + compiler/test/TestCompilerOnExamples.kt | 1 + compiler/test/TestCompilerOptionLibdirs.kt | 1 + compiler/test/helpers/compileXyz.kt | 1 + docs/source/todo.rst | 2 - .../src/prog8/http/TestHttp.kt | 3 +- 9 files changed, 57 insertions(+), 3 deletions(-) diff --git a/codeCore/src/prog8/code/core/CompilationOptions.kt b/codeCore/src/prog8/code/core/CompilationOptions.kt index 726dfb53e..f0ea6a09b 100644 --- a/codeCore/src/prog8/code/core/CompilationOptions.kt +++ b/codeCore/src/prog8/code/core/CompilationOptions.kt @@ -19,6 +19,7 @@ class CompilationOptions(val output: OutputType, var asmQuiet: Boolean = false, var asmListfile: Boolean = false, var includeSourcelines: Boolean = false, + var dumpVariables: Boolean = false, var experimentalCodegen: Boolean = false, var varsHighBank: Int? = null, var varsGolden: Boolean = false, diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt index a9e367c2a..ac33f3879 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt @@ -262,6 +262,10 @@ class AsmGen6502Internal ( // write the unmodified code output.writeLines(assembly) } + + if(options.dumpVariables) + dumpVariables() + return AssemblyProgram(program.name, options.outputDir, options.compTarget) } else { errors.report() @@ -269,6 +273,48 @@ class AsmGen6502Internal ( } } + private fun dumpVariables() { + println("---- VARIABLES DUMP ----") + if(allocator.globalFloatConsts.isNotEmpty()) { + println("Floats:") + allocator.globalFloatConsts.forEach { (value, name) -> + println(" $name = $value") + } + } + if(symbolTable.allMemorySlabs.isNotEmpty()) { + println("Memory slabs:") + symbolTable.allMemorySlabs.sortedBy { it.name }.forEach { slab -> + println(" ${slab.name} ${slab.size} align ${slab.align}") + } + } + if(symbolTable.allMemMappedVariables.isNotEmpty()) { + println("Memory mapped:") + symbolTable.allMemMappedVariables + .sortedWith( compareBy( {it.address}, {it.scopedName} )) + .forEach { mvar -> + println(" ${'$'}${mvar.address.toString(16).padStart(4, '0')}\t${mvar.dt}\t${mvar.scopedName}") + } + } + if(allocator.zeropageVars.isNotEmpty()) { + println("ZeroPage:") + allocator.zeropageVars + .asSequence() + .sortedWith( compareBy( {it.value.address}, {it.key} )) + .forEach { (name, alloc) -> + println(" ${'$'}${alloc.address.toString(16).padStart(2, '0')}\t${alloc.dt}\t$name") + } + } + if(symbolTable.allVariables.isNotEmpty()) { + println("Static variables (not in ZeroPage):") + symbolTable.allVariables + .filterNot { allocator.isZpVar(it.scopedName) } + .sortedBy { it.scopedName }.forEach { + println(" ${it.dt}\t${it.scopedName}\t") + } + } + println("---- VARIABLES DUMP END ----") + } + private fun scanInvalid65816instructions(asmLines: MutableList) { // The CommanderX16 ships with a WDC 65C02 CPU or a WDC 65816 CPU // The latter is compatible with the 65C02 except for 4 instructions: RMB, SMB, BBS, BBR. diff --git a/compiler/src/prog8/CompilerMain.kt b/compiler/src/prog8/CompilerMain.kt index f3e86e557..0bdbbf32c 100644 --- a/compiler/src/prog8/CompilerMain.kt +++ b/compiler/src/prog8/CompilerMain.kt @@ -42,6 +42,7 @@ private fun compileMain(args: Array): Boolean { val startEmulator1 by cli.option(ArgType.Boolean, fullName = "emu", description = "auto-start emulator after successful compilation") val startEmulator2 by cli.option(ArgType.Boolean, fullName = "emu2", description = "auto-start alternative emulator after successful compilation") val experimentalCodegen by cli.option(ArgType.Boolean, fullName = "expericodegen", description = "use experimental/alternative codegen") + val dumpVariables by cli.option(ArgType.Boolean, fullName = "dumpvars", description = "print a dump of the variables in the program") val dontWriteAssembly by cli.option(ArgType.Boolean, fullName = "noasm", description="don't create assembly code") val dontOptimize by cli.option(ArgType.Boolean, fullName = "noopt", description = "don't perform code optimizations") val outputDir by cli.option(ArgType.String, fullName = "out", description = "directory for output files instead of current directory").default(".") @@ -152,6 +153,7 @@ private fun compileMain(args: Array): Boolean { asmListfile == true, includeSourcelines == true, experimentalCodegen == true, + dumpVariables == true, varsHighBank, varsGolden == true, slabsHighBank, @@ -230,6 +232,7 @@ private fun compileMain(args: Array): Boolean { asmListfile == true, includeSourcelines == true, experimentalCodegen == true, + dumpVariables == true, varsHighBank, varsGolden == true, slabsHighBank, diff --git a/compiler/src/prog8/compiler/Compiler.kt b/compiler/src/prog8/compiler/Compiler.kt index 33c2c26ce..81920b89f 100644 --- a/compiler/src/prog8/compiler/Compiler.kt +++ b/compiler/src/prog8/compiler/Compiler.kt @@ -38,6 +38,7 @@ class CompilerArguments(val filepath: Path, val asmListfile: Boolean, val includeSourcelines: Boolean, val experimentalCodegen: Boolean, + val dumpVariables: Boolean, val varsHighBank: Int?, val varsGolden: Boolean, val slabsHighBank: Int?, @@ -83,6 +84,7 @@ fun compileProgram(args: CompilerArguments): CompilationResult? { asmListfile = args.asmListfile includeSourcelines = args.includeSourcelines experimentalCodegen = args.experimentalCodegen + dumpVariables = args.dumpVariables breakpointCpuInstruction = args.breakpointCpuInstruction varsHighBank = args.varsHighBank varsGolden = args.varsGolden diff --git a/compiler/test/TestCompilerOnExamples.kt b/compiler/test/TestCompilerOnExamples.kt index 1b1037853..fde99cfd0 100644 --- a/compiler/test/TestCompilerOnExamples.kt +++ b/compiler/test/TestCompilerOnExamples.kt @@ -32,6 +32,7 @@ private fun compileTheThing(filepath: Path, optimize: Boolean, target: ICompilat asmListfile = false, includeSourcelines = false, experimentalCodegen = false, + dumpVariables = false, varsHighBank = null, varsGolden = false, slabsHighBank = null, diff --git a/compiler/test/TestCompilerOptionLibdirs.kt b/compiler/test/TestCompilerOptionLibdirs.kt index e4dfbc9d2..148db23d7 100644 --- a/compiler/test/TestCompilerOptionLibdirs.kt +++ b/compiler/test/TestCompilerOptionLibdirs.kt @@ -30,6 +30,7 @@ class TestCompilerOptionSourcedirs: FunSpec({ asmListfile = false, includeSourcelines = false, experimentalCodegen = false, + dumpVariables = false, varsHighBank = null, varsGolden = false, slabsHighBank = null, diff --git a/compiler/test/helpers/compileXyz.kt b/compiler/test/helpers/compileXyz.kt index b6ce0bc62..09d827a4f 100644 --- a/compiler/test/helpers/compileXyz.kt +++ b/compiler/test/helpers/compileXyz.kt @@ -29,6 +29,7 @@ internal fun compileFile( asmListfile = false, includeSourcelines = false, experimentalCodegen = false, + dumpVariables = false, varsHighBank = null, varsGolden = false, slabsHighBank = null, diff --git a/docs/source/todo.rst b/docs/source/todo.rst index a9e162a6f..4a0e0a6e5 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,8 +1,6 @@ TODO ==== -add an option to dump what variables ended up where (zp, normal ram, etc) - chess is larger than on 10.1 (other variables ended up in zeropage) rockrunner is larger than on 10.1 medemo is quite a bit larger than on 10.1 diff --git a/httpCompilerService/src/prog8/http/TestHttp.kt b/httpCompilerService/src/prog8/http/TestHttp.kt index 578a13d4a..cb0c50af0 100644 --- a/httpCompilerService/src/prog8/http/TestHttp.kt +++ b/httpCompilerService/src/prog8/http/TestHttp.kt @@ -48,7 +48,8 @@ class RequestParser : Take { varsHighBank = null, varsGolden = false, slabsHighBank = null, - slabsGolden = false + slabsGolden = false, + dumpVariables = false ) compileProgram(args) return RsJson(Jsonding())