This commit is contained in:
Irmen de Jong 2023-02-05 12:19:03 +01:00
parent c75b1581d2
commit f117805129
5 changed files with 19 additions and 29 deletions

View File

@ -1,7 +1,6 @@
package prog8.compiler
import com.github.michaelbull.result.onFailure
import prog8.ast.AstToSourceTextConverter
import prog8.ast.IBuiltinFunctions
import prog8.ast.IStatementContainer
import prog8.ast.Program
@ -12,6 +11,7 @@ import prog8.ast.statements.Directive
import prog8.ast.statements.VarDecl
import prog8.ast.walk.IAstVisitor
import prog8.code.SymbolTableMaker
import prog8.code.ast.PtProgram
import prog8.code.core.*
import prog8.code.target.*
import prog8.codegen.vm.VmCodeGen
@ -402,7 +402,8 @@ private fun createAssemblyAndAssemble(program: Program,
// println("*********** COMPILER AST RIGHT BEFORE ASM GENERATION *************")
// printProgram(program)
val assembly = asmGeneratorFor(program, errors, compilerOptions).compileToAssembly()
val intermediateAst = IntermediateAstMaker(program, compilerOptions).transform()
val assembly = asmGeneratorFor(intermediateAst, compilerOptions, errors).compileToAssembly()
errors.report()
return if(assembly!=null && errors.noErrors()) {
@ -432,28 +433,19 @@ private fun removeAllVardeclsFromAst(program: Program) {
SearchAndRemove()
}
fun printProgram(program: Program) {
println()
val printer = AstToSourceTextConverter(::print, program)
printer.visit(program)
println()
}
internal fun asmGeneratorFor(program: Program,
errors: IErrorReporter,
options: CompilationOptions): IAssemblyGenerator
internal fun asmGeneratorFor(program: PtProgram,
options: CompilationOptions,
errors: IErrorReporter): IAssemblyGenerator
{
val intermediateAst = IntermediateAstMaker(program, options).transform()
val stMaker = SymbolTableMaker(intermediateAst, options)
val stMaker = SymbolTableMaker(program, options)
val symbolTable = stMaker.make()
if(options.experimentalCodegen)
return prog8.codegen.experimental.CodeGen(intermediateAst, symbolTable, options, errors)
return if(options.experimentalCodegen)
prog8.codegen.experimental.CodeGen(program, symbolTable, options, errors)
else if (options.compTarget.machine.cpu in arrayOf(CpuType.CPU6502, CpuType.CPU65c02))
return prog8.codegen.cpu6502.AsmGen(intermediateAst, symbolTable, options, errors)
prog8.codegen.cpu6502.AsmGen(program, symbolTable, options, errors)
else if (options.compTarget.name == VMTarget.NAME)
return VmCodeGen(intermediateAst, symbolTable, options, errors)
VmCodeGen(program, symbolTable, options, errors)
else
throw NotImplementedError("no asm generator for cpu ${options.compTarget.machine.cpu}")
}

View File

@ -15,7 +15,6 @@ import prog8.code.core.Position
import prog8.code.core.SourceCode
import prog8.code.core.ZeropageWish
import prog8.code.target.C64Target
import prog8.compiler.printProgram
import prog8tests.helpers.DummyFunctions
import prog8tests.helpers.DummyMemsizer
import prog8tests.helpers.DummyStringEncoder
@ -147,7 +146,6 @@ class TestMemory: FunSpec({
target = AssignTarget(null, null, DirectMemoryWrite(memexpr, Position.DUMMY), Position.DUMMY)
assign = Assignment(target, NumericLiteral.optimalInteger(0, Position.DUMMY), AssignmentOrigin.USERCODE, Position.DUMMY)
wrapWithProgram(listOf(assign))
printProgram(target.definingModule.program)
target.isIOAddress(c64target.machine) shouldBe true
}

View File

@ -14,7 +14,6 @@ import prog8.code.core.Position
import prog8.code.core.toHex
import prog8.code.target.C64Target
import prog8.code.target.cbm.Mflpt5
import prog8.compiler.printProgram
import prog8tests.helpers.ErrorReporterForTests
import prog8tests.helpers.compileText
@ -201,7 +200,6 @@ class TestNumbers: FunSpec({
val result = compileText(C64Target(), false, src, writeAssembly = false)!!
val statements = result.program.entrypoint.statements
statements.size shouldBe 8
printProgram(result.program)
(statements[1] as Assignment).value shouldBe NumericLiteral(DataType.UWORD, 32768.0, Position.DUMMY)
(statements[3] as Assignment).value shouldBe NumericLiteral(DataType.UWORD, 65535.0, Position.DUMMY)
(statements[5] as Assignment).value shouldBe NumericLiteral(DataType.UBYTE, 255.0, Position.DUMMY)

View File

@ -16,7 +16,6 @@ import prog8.code.core.DataType
import prog8.code.core.Position
import prog8.code.target.C64Target
import prog8.code.target.Cx16Target
import prog8.compiler.printProgram
import prog8tests.helpers.*
@ -265,7 +264,6 @@ class TestOptimization: FunSpec({
}
"""
val result = compileText(C64Target(), false, src, writeAssembly = true)!!
printProgram(result.program)
val stmts = result.program.entrypoint.statements
stmts.size shouldBe 8
@ -401,7 +399,6 @@ class TestOptimization: FunSpec({
}
}"""
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
printProgram(result.program)
/* expected:
ubyte z1
z1 = 10
@ -638,7 +635,6 @@ class TestOptimization: FunSpec({
}
}"""
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
printProgram(result.program)
/*
expected result:
ubyte[] auto_heap_var = [1,4,99,3]
@ -679,7 +675,6 @@ class TestOptimization: FunSpec({
}
}"""
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
printProgram(result.program)
val stmts = result.program.entrypoint.statements
stmts.size shouldBe 5
val ifStmt = stmts[4] as IfElse
@ -698,7 +693,6 @@ class TestOptimization: FunSpec({
}
}"""
val result = compileText(C64Target(), optimize=true, src, writeAssembly=false)!!
printProgram(result.program)
val stmts = result.program.entrypoint.statements
stmts.size shouldBe 5
val ifStmt = stmts[4] as IfElse

View File

@ -6,6 +6,14 @@ import prog8.ast.walk.IAstVisitor
import prog8.code.core.*
fun printProgram(program: Program) {
println()
val printer = AstToSourceTextConverter(::print, program)
printer.visit(program)
println()
}
/**
* Produces Prog8 source text from a [Program] (AST node),
* passing it as a String to the specified receiver function.