From 99c29343de5315ea903a116329fd8bbb235687e5 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Mon, 1 Jan 2024 22:48:19 +0100 Subject: [PATCH] added -printast1 and -printast2 command line options --- compiler/src/prog8/CompilerMain.kt | 6 ++++++ compiler/src/prog8/compiler/Compiler.kt | 18 ++++++++++++++---- compiler/test/TestCompilerOnExamples.kt | 2 ++ compiler/test/TestCompilerOptionLibdirs.kt | 2 ++ compiler/test/helpers/compileXyz.kt | 4 +++- docs/source/compiling.rst | 7 +++++++ httpCompilerService/src/prog8/http/TestHttp.kt | 2 ++ 7 files changed, 36 insertions(+), 5 deletions(-) diff --git a/compiler/src/prog8/CompilerMain.kt b/compiler/src/prog8/CompilerMain.kt index e4501f63a..5f957d4e5 100644 --- a/compiler/src/prog8/CompilerMain.kt +++ b/compiler/src/prog8/CompilerMain.kt @@ -51,6 +51,8 @@ private fun compileMain(args: Array): Boolean { 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 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 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") @@ -159,6 +161,8 @@ private fun compileMain(args: Array): Boolean { compilationTarget!!, splitWordArrays == true, breakpointCpuInstruction = false, + printAst1 == true, + printAst2 == true, processedSymbols, srcdirs, outputPath @@ -236,6 +240,8 @@ private fun compileMain(args: Array): Boolean { compilationTarget!!, splitWordArrays == true, breakpointCpuInstruction == true, + printAst1 == true, + printAst2 == true, processedSymbols, srcdirs, outputPath diff --git a/compiler/src/prog8/compiler/Compiler.kt b/compiler/src/prog8/compiler/Compiler.kt index 2118743a2..2c9503064 100644 --- a/compiler/src/prog8/compiler/Compiler.kt +++ b/compiler/src/prog8/compiler/Compiler.kt @@ -6,9 +6,11 @@ import prog8.ast.Program import prog8.ast.base.AstException import prog8.ast.expressions.Expression import prog8.ast.expressions.NumericLiteral +import prog8.ast.printProgram import prog8.ast.statements.Directive import prog8.code.SymbolTableMaker import prog8.code.ast.PtProgram +import prog8.code.ast.printAst import prog8.code.core.* import prog8.code.optimize.optimizeIntermediateAst import prog8.code.target.* @@ -44,6 +46,8 @@ class CompilerArguments(val filepath: Path, val compilationTarget: String, val splitWordArrays: Boolean, val breakpointCpuInstruction: Boolean, + val printAst1: Boolean, + val printAst2: Boolean, val symbolDefs: Map, val sourceDirs: List = emptyList(), val outputDir: Path = Path(""), @@ -121,15 +125,21 @@ fun compileProgram(args: CompilerArguments): CompilationResult? { program.processAstBeforeAsmGeneration(compilationOptions, args.errors) args.errors.report() -// println("*********** COMPILER AST RIGHT BEFORE ASM GENERATION *************") -// printProgram(program) + if(args.printAst1) { + println("\n*********** COMPILER AST *************") + printProgram(program) + println("*********** COMPILER AST END *************\n") + } val intermediateAst = IntermediateAstMaker(program, args.errors).transform() optimizeIntermediateAst(intermediateAst, compilationOptions, args.errors) args.errors.report() -// println("*********** AST RIGHT BEFORE ASM GENERATION *************") -// printAst(intermediateAst, true, ::println) + if(args.printAst2) { + println("\n*********** INTERMEDIATE AST *************") + printAst(intermediateAst, true, ::println) + println("*********** INTERMEDIATE AST END *************\n") + } if(!createAssemblyAndAssemble(intermediateAst, args.errors, compilationOptions)) { System.err.println("Error in codegeneration or assembler") diff --git a/compiler/test/TestCompilerOnExamples.kt b/compiler/test/TestCompilerOnExamples.kt index 6743654e8..5c35237f6 100644 --- a/compiler/test/TestCompilerOnExamples.kt +++ b/compiler/test/TestCompilerOnExamples.kt @@ -40,6 +40,8 @@ private fun compileTheThing(filepath: Path, optimize: Boolean, target: ICompilat compilationTarget = target.name, splitWordArrays = false, breakpointCpuInstruction = false, + printAst1 = false, + printAst2 = false, symbolDefs = emptyMap(), outputDir = outputDir ) diff --git a/compiler/test/TestCompilerOptionLibdirs.kt b/compiler/test/TestCompilerOptionLibdirs.kt index 4e26a1e56..1c001a840 100644 --- a/compiler/test/TestCompilerOptionLibdirs.kt +++ b/compiler/test/TestCompilerOptionLibdirs.kt @@ -38,6 +38,8 @@ class TestCompilerOptionSourcedirs: FunSpec({ compilationTarget = Cx16Target.NAME, splitWordArrays = false, breakpointCpuInstruction = false, + printAst1 = false, + printAst2 = false, symbolDefs = emptyMap(), sourceDirs, outputDir diff --git a/compiler/test/helpers/compileXyz.kt b/compiler/test/helpers/compileXyz.kt index 6f1785251..0540dc7b9 100644 --- a/compiler/test/helpers/compileXyz.kt +++ b/compiler/test/helpers/compileXyz.kt @@ -39,7 +39,9 @@ internal fun compileFile( outputDir = outputDir, errors = errors ?: ErrorReporterForTests(), splitWordArrays = false, - breakpointCpuInstruction = false + breakpointCpuInstruction = false, + printAst1 = false, + printAst2 = false ) return compileProgram(args) } diff --git a/docs/source/compiling.rst b/docs/source/compiling.rst index 1d56c16b8..48e29340c 100644 --- a/docs/source/compiling.rst +++ b/docs/source/compiling.rst @@ -185,6 +185,13 @@ One or more .p8 module files ``-expericodegen`` 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`` Also include the original prog8 source code lines as comments in the generated assembly code file, mixed in between the actual generated assembly code. diff --git a/httpCompilerService/src/prog8/http/TestHttp.kt b/httpCompilerService/src/prog8/http/TestHttp.kt index ac98a539a..d40797fa0 100644 --- a/httpCompilerService/src/prog8/http/TestHttp.kt +++ b/httpCompilerService/src/prog8/http/TestHttp.kt @@ -44,6 +44,8 @@ class RequestParser : Take { experimentalCodegen = false, splitWordArrays = false, breakpointCpuInstruction = false, + printAst1 = false, + printAst2 = false, varsHighBank = null, varsGolden = false, slabsHighBank = null,