mirror of
https://github.com/irmen/prog8.git
synced 2024-10-07 00:55:33 +00:00
fixed a compiler ast crash and added -noopt command line flag
This commit is contained in:
parent
d6ca1e6a12
commit
8811d2f7c5
@ -46,6 +46,7 @@ private fun compileMain(args: Array<String>) {
|
|||||||
var moduleFile = ""
|
var moduleFile = ""
|
||||||
var writeVmCode = false
|
var writeVmCode = false
|
||||||
var writeAssembly = true
|
var writeAssembly = true
|
||||||
|
var optimize = true
|
||||||
for (arg in args) {
|
for (arg in args) {
|
||||||
if(arg=="-emu")
|
if(arg=="-emu")
|
||||||
emulatorToStart = "x64"
|
emulatorToStart = "x64"
|
||||||
@ -55,6 +56,8 @@ private fun compileMain(args: Array<String>) {
|
|||||||
writeVmCode = true
|
writeVmCode = true
|
||||||
else if(arg=="-noasm")
|
else if(arg=="-noasm")
|
||||||
writeAssembly = false
|
writeAssembly = false
|
||||||
|
else if(arg=="-noopt")
|
||||||
|
optimize = false
|
||||||
else if(!arg.startsWith("-"))
|
else if(!arg.startsWith("-"))
|
||||||
moduleFile = arg
|
moduleFile = arg
|
||||||
else
|
else
|
||||||
@ -101,6 +104,7 @@ private fun compileMain(args: Array<String>) {
|
|||||||
}
|
}
|
||||||
//println(" time4: $time4")
|
//println(" time4: $time4")
|
||||||
|
|
||||||
|
if(optimize) {
|
||||||
// optimize the parse tree
|
// optimize the parse tree
|
||||||
println("Optimizing...")
|
println("Optimizing...")
|
||||||
val allScopedSymbolDefinitions = moduleAst.checkIdentifiers(heap) // useful for checking symbol usage later?
|
val allScopedSymbolDefinitions = moduleAst.checkIdentifiers(heap) // useful for checking symbol usage later?
|
||||||
@ -111,6 +115,7 @@ private fun compileMain(args: Array<String>) {
|
|||||||
if (optsDone1 + optsDone2 == 0)
|
if (optsDone1 + optsDone2 == 0)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
namespace = moduleAst.definingScope() // create it again, it could have changed in the meantime
|
namespace = moduleAst.definingScope() // create it again, it could have changed in the meantime
|
||||||
moduleAst.checkValid(namespace, compilerOptions, heap) // check if final tree is valid
|
moduleAst.checkValid(namespace, compilerOptions, heap) // check if final tree is valid
|
||||||
@ -121,6 +126,7 @@ private fun compileMain(args: Array<String>) {
|
|||||||
// compile the syntax tree into stackvmProg form, and optimize that
|
// compile the syntax tree into stackvmProg form, and optimize that
|
||||||
val compiler = Compiler(moduleAst, namespace, heap)
|
val compiler = Compiler(moduleAst, namespace, heap)
|
||||||
val intermediate = compiler.compile(compilerOptions)
|
val intermediate = compiler.compile(compilerOptions)
|
||||||
|
if(optimize)
|
||||||
intermediate.optimize()
|
intermediate.optimize()
|
||||||
|
|
||||||
if(writeVmCode) {
|
if(writeVmCode) {
|
||||||
@ -134,7 +140,7 @@ private fun compileMain(args: Array<String>) {
|
|||||||
if(writeAssembly) {
|
if(writeAssembly) {
|
||||||
val zeropage = C64Zeropage(compilerOptions)
|
val zeropage = C64Zeropage(compilerOptions)
|
||||||
intermediate.allocateZeropage(zeropage)
|
intermediate.allocateZeropage(zeropage)
|
||||||
val assembly = AsmGen(compilerOptions, intermediate, heap, zeropage).compileToAssembly()
|
val assembly = AsmGen(compilerOptions, intermediate, heap, zeropage).compileToAssembly(optimize)
|
||||||
assembly.assemble(compilerOptions)
|
assembly.assemble(compilerOptions)
|
||||||
programname = assembly.name
|
programname = assembly.name
|
||||||
}
|
}
|
||||||
@ -211,6 +217,7 @@ private fun usage() {
|
|||||||
System.err.println(" [-writevm] write intermediate vm code to a file as well")
|
System.err.println(" [-writevm] write intermediate vm code to a file as well")
|
||||||
System.err.println(" [-noasm] don't create assembly code")
|
System.err.println(" [-noasm] don't create assembly code")
|
||||||
System.err.println(" [-vm] launch the prog8 virtual machine instead of the compiler")
|
System.err.println(" [-vm] launch the prog8 virtual machine instead of the compiler")
|
||||||
|
System.err.println(" [-noopt] don't perform optimizations")
|
||||||
System.err.println(" modulefile main module file to compile")
|
System.err.println(" modulefile main module file to compile")
|
||||||
exitProcess(1)
|
exitProcess(1)
|
||||||
}
|
}
|
||||||
|
@ -704,8 +704,8 @@ private class AstChecker(private val namespace: INameScope,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val leftDt = expr.left.resultingDatatype(namespace, heap)!!
|
val leftDt = expr.left.resultingDatatype(namespace, heap)
|
||||||
val rightDt = expr.right.resultingDatatype(namespace, heap)!!
|
val rightDt = expr.right.resultingDatatype(namespace, heap)
|
||||||
if(leftDt !in NumericDatatypes)
|
if(leftDt !in NumericDatatypes)
|
||||||
checkResult.add(ExpressionError("left operand is not numeric", expr.left.position))
|
checkResult.add(ExpressionError("left operand is not numeric", expr.left.position))
|
||||||
if(rightDt!in NumericDatatypes)
|
if(rightDt!in NumericDatatypes)
|
||||||
|
@ -73,7 +73,7 @@ class AsmGen(val options: CompilationOptions, val program: IntermediateProgram,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun compileToAssembly(): AssemblyProgram {
|
fun compileToAssembly(optimize: Boolean): AssemblyProgram {
|
||||||
println("Generating assembly code from intermediate code... ")
|
println("Generating assembly code from intermediate code... ")
|
||||||
|
|
||||||
assemblyLines.clear()
|
assemblyLines.clear()
|
||||||
@ -81,10 +81,12 @@ class AsmGen(val options: CompilationOptions, val program: IntermediateProgram,
|
|||||||
for(b in program.blocks)
|
for(b in program.blocks)
|
||||||
block2asm(b)
|
block2asm(b)
|
||||||
|
|
||||||
|
if(optimize) {
|
||||||
var optimizationsDone = 1
|
var optimizationsDone = 1
|
||||||
while (optimizationsDone > 0) {
|
while (optimizationsDone > 0) {
|
||||||
optimizationsDone = optimizeAssembly(assemblyLines)
|
optimizationsDone = optimizeAssembly(assemblyLines)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
File("${program.name}.asm").printWriter().use {
|
File("${program.name}.asm").printWriter().use {
|
||||||
for (line in assemblyLines) { it.println(line) }
|
for (line in assemblyLines) { it.println(line) }
|
||||||
|
Loading…
Reference in New Issue
Block a user