added -printast1 and -printast2 command line options

This commit is contained in:
Irmen de Jong 2024-01-01 22:48:19 +01:00
parent 892fa76883
commit 99c29343de
7 changed files with 36 additions and 5 deletions

View File

@ -51,6 +51,8 @@ private fun compileMain(args: Array<String>): Boolean {
val includeSourcelines by cli.option(ArgType.Boolean, fullName = "sourcelines", description = "include original Prog8 source lines in generated asm code") val includeSourcelines by cli.option(ArgType.Boolean, fullName = "sourcelines", description = "include original Prog8 source lines in generated asm code")
val splitWordArrays by cli.option(ArgType.Boolean, fullName = "splitarrays", description = "treat all word arrays as tagged with @split to make them lsb/msb split in memory") val splitWordArrays by cli.option(ArgType.Boolean, fullName = "splitarrays", description = "treat all word arrays as tagged with @split to make them lsb/msb split in memory")
val noShortCircuit by cli.option(ArgType.Boolean, fullName = "noshortcircuit", description = "do not apply McCarthy/short-circuit evaluation to boolean expressions") val noShortCircuit by cli.option(ArgType.Boolean, fullName = "noshortcircuit", description = "do not apply McCarthy/short-circuit evaluation to boolean expressions")
val printAst1 by cli.option(ArgType.Boolean, fullName = "printast1", description = "print out the compiler AST")
val printAst2 by cli.option(ArgType.Boolean, fullName = "printast2", description = "print out the intermediate AST that is used for code generation")
val breakpointCpuInstruction by cli.option(ArgType.Boolean, fullName = "breakinstr", description = "also use a CPU instruction for %breakpoint") val breakpointCpuInstruction by cli.option(ArgType.Boolean, fullName = "breakinstr", description = "also use a CPU instruction for %breakpoint")
val compilationTarget by cli.option(ArgType.String, fullName = "target", description = "target output of the compiler (one of '${C64Target.NAME}', '${C128Target.NAME}', '${Cx16Target.NAME}', '${AtariTarget.NAME}', '${PETTarget.NAME}', '${VMTarget.NAME}') (required)") val compilationTarget by cli.option(ArgType.String, fullName = "target", description = "target output of the compiler (one of '${C64Target.NAME}', '${C128Target.NAME}', '${Cx16Target.NAME}', '${AtariTarget.NAME}', '${PETTarget.NAME}', '${VMTarget.NAME}') (required)")
val startVm by cli.option(ArgType.Boolean, fullName = "vm", description = "load and run a .p8ir IR source file in the VM") val startVm by cli.option(ArgType.Boolean, fullName = "vm", description = "load and run a .p8ir IR source file in the VM")
@ -159,6 +161,8 @@ private fun compileMain(args: Array<String>): Boolean {
compilationTarget!!, compilationTarget!!,
splitWordArrays == true, splitWordArrays == true,
breakpointCpuInstruction = false, breakpointCpuInstruction = false,
printAst1 == true,
printAst2 == true,
processedSymbols, processedSymbols,
srcdirs, srcdirs,
outputPath outputPath
@ -236,6 +240,8 @@ private fun compileMain(args: Array<String>): Boolean {
compilationTarget!!, compilationTarget!!,
splitWordArrays == true, splitWordArrays == true,
breakpointCpuInstruction == true, breakpointCpuInstruction == true,
printAst1 == true,
printAst2 == true,
processedSymbols, processedSymbols,
srcdirs, srcdirs,
outputPath outputPath

View File

@ -6,9 +6,11 @@ import prog8.ast.Program
import prog8.ast.base.AstException import prog8.ast.base.AstException
import prog8.ast.expressions.Expression import prog8.ast.expressions.Expression
import prog8.ast.expressions.NumericLiteral import prog8.ast.expressions.NumericLiteral
import prog8.ast.printProgram
import prog8.ast.statements.Directive import prog8.ast.statements.Directive
import prog8.code.SymbolTableMaker import prog8.code.SymbolTableMaker
import prog8.code.ast.PtProgram import prog8.code.ast.PtProgram
import prog8.code.ast.printAst
import prog8.code.core.* import prog8.code.core.*
import prog8.code.optimize.optimizeIntermediateAst import prog8.code.optimize.optimizeIntermediateAst
import prog8.code.target.* import prog8.code.target.*
@ -44,6 +46,8 @@ class CompilerArguments(val filepath: Path,
val compilationTarget: String, val compilationTarget: String,
val splitWordArrays: Boolean, val splitWordArrays: Boolean,
val breakpointCpuInstruction: Boolean, val breakpointCpuInstruction: Boolean,
val printAst1: Boolean,
val printAst2: Boolean,
val symbolDefs: Map<String, String>, val symbolDefs: Map<String, String>,
val sourceDirs: List<String> = emptyList(), val sourceDirs: List<String> = emptyList(),
val outputDir: Path = Path(""), val outputDir: Path = Path(""),
@ -121,15 +125,21 @@ fun compileProgram(args: CompilerArguments): CompilationResult? {
program.processAstBeforeAsmGeneration(compilationOptions, args.errors) program.processAstBeforeAsmGeneration(compilationOptions, args.errors)
args.errors.report() args.errors.report()
// println("*********** COMPILER AST RIGHT BEFORE ASM GENERATION *************") if(args.printAst1) {
// printProgram(program) println("\n*********** COMPILER AST *************")
printProgram(program)
println("*********** COMPILER AST END *************\n")
}
val intermediateAst = IntermediateAstMaker(program, args.errors).transform() val intermediateAst = IntermediateAstMaker(program, args.errors).transform()
optimizeIntermediateAst(intermediateAst, compilationOptions, args.errors) optimizeIntermediateAst(intermediateAst, compilationOptions, args.errors)
args.errors.report() args.errors.report()
// println("*********** AST RIGHT BEFORE ASM GENERATION *************") if(args.printAst2) {
// printAst(intermediateAst, true, ::println) println("\n*********** INTERMEDIATE AST *************")
printAst(intermediateAst, true, ::println)
println("*********** INTERMEDIATE AST END *************\n")
}
if(!createAssemblyAndAssemble(intermediateAst, args.errors, compilationOptions)) { if(!createAssemblyAndAssemble(intermediateAst, args.errors, compilationOptions)) {
System.err.println("Error in codegeneration or assembler") System.err.println("Error in codegeneration or assembler")

View File

@ -40,6 +40,8 @@ private fun compileTheThing(filepath: Path, optimize: Boolean, target: ICompilat
compilationTarget = target.name, compilationTarget = target.name,
splitWordArrays = false, splitWordArrays = false,
breakpointCpuInstruction = false, breakpointCpuInstruction = false,
printAst1 = false,
printAst2 = false,
symbolDefs = emptyMap(), symbolDefs = emptyMap(),
outputDir = outputDir outputDir = outputDir
) )

View File

@ -38,6 +38,8 @@ class TestCompilerOptionSourcedirs: FunSpec({
compilationTarget = Cx16Target.NAME, compilationTarget = Cx16Target.NAME,
splitWordArrays = false, splitWordArrays = false,
breakpointCpuInstruction = false, breakpointCpuInstruction = false,
printAst1 = false,
printAst2 = false,
symbolDefs = emptyMap(), symbolDefs = emptyMap(),
sourceDirs, sourceDirs,
outputDir outputDir

View File

@ -39,7 +39,9 @@ internal fun compileFile(
outputDir = outputDir, outputDir = outputDir,
errors = errors ?: ErrorReporterForTests(), errors = errors ?: ErrorReporterForTests(),
splitWordArrays = false, splitWordArrays = false,
breakpointCpuInstruction = false breakpointCpuInstruction = false,
printAst1 = false,
printAst2 = false
) )
return compileProgram(args) return compileProgram(args)
} }

View File

@ -185,6 +185,13 @@ One or more .p8 module files
``-expericodegen`` ``-expericodegen``
Use experimental code generation backend (*incomplete*). Use experimental code generation backend (*incomplete*).
``-printast1``
Prints the "compiler AST" (the internal representation of the program) after all processing steps.
``-printast2``
Prints the "intermediate AST" which is the reduced representation of the program.
This is what is used in the code generators, to generate the executable code from.
``-sourcelines`` ``-sourcelines``
Also include the original prog8 source code lines as comments in the generated assembly code file, Also include the original prog8 source code lines as comments in the generated assembly code file,
mixed in between the actual generated assembly code. mixed in between the actual generated assembly code.

View File

@ -44,6 +44,8 @@ class RequestParser : Take {
experimentalCodegen = false, experimentalCodegen = false,
splitWordArrays = false, splitWordArrays = false,
breakpointCpuInstruction = false, breakpointCpuInstruction = false,
printAst1 = false,
printAst2 = false,
varsHighBank = null, varsHighBank = null,
varsGolden = false, varsGolden = false,
slabsHighBank = null, slabsHighBank = null,