diff --git a/compiler/src/prog8/CompilerMain.kt b/compiler/src/prog8/CompilerMain.kt index 796338b7c..fe0f37b43 100644 --- a/compiler/src/prog8/CompilerMain.kt +++ b/compiler/src/prog8/CompilerMain.kt @@ -46,6 +46,7 @@ private fun compileMain(args: Array) { var moduleFile = "" var writeVmCode = false var writeAssembly = true + var optimize = true for (arg in args) { if(arg=="-emu") emulatorToStart = "x64" @@ -55,6 +56,8 @@ private fun compileMain(args: Array) { writeVmCode = true else if(arg=="-noasm") writeAssembly = false + else if(arg=="-noopt") + optimize = false else if(!arg.startsWith("-")) moduleFile = arg else @@ -101,15 +104,17 @@ private fun compileMain(args: Array) { } //println(" time4: $time4") - // optimize the parse tree - println("Optimizing...") - val allScopedSymbolDefinitions = moduleAst.checkIdentifiers(heap) // useful for checking symbol usage later? - while (true) { - // keep optimizing expressions and statements until no more steps remain - val optsDone1 = moduleAst.simplifyExpressions(namespace, heap) - val optsDone2 = moduleAst.optimizeStatements(namespace, heap) - if (optsDone1 + optsDone2 == 0) - break + if(optimize) { + // optimize the parse tree + println("Optimizing...") + val allScopedSymbolDefinitions = moduleAst.checkIdentifiers(heap) // useful for checking symbol usage later? + while (true) { + // keep optimizing expressions and statements until no more steps remain + val optsDone1 = moduleAst.simplifyExpressions(namespace, heap) + val optsDone2 = moduleAst.optimizeStatements(namespace, heap) + if (optsDone1 + optsDone2 == 0) + break + } } namespace = moduleAst.definingScope() // create it again, it could have changed in the meantime @@ -121,7 +126,8 @@ private fun compileMain(args: Array) { // compile the syntax tree into stackvmProg form, and optimize that val compiler = Compiler(moduleAst, namespace, heap) val intermediate = compiler.compile(compilerOptions) - intermediate.optimize() + if(optimize) + intermediate.optimize() if(writeVmCode) { val stackVmFilename = intermediate.name + ".vm.txt" @@ -134,7 +140,7 @@ private fun compileMain(args: Array) { if(writeAssembly) { val zeropage = C64Zeropage(compilerOptions) intermediate.allocateZeropage(zeropage) - val assembly = AsmGen(compilerOptions, intermediate, heap, zeropage).compileToAssembly() + val assembly = AsmGen(compilerOptions, intermediate, heap, zeropage).compileToAssembly(optimize) assembly.assemble(compilerOptions) 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(" [-noasm] don't create assembly code") 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") exitProcess(1) } diff --git a/compiler/src/prog8/ast/AstChecker.kt b/compiler/src/prog8/ast/AstChecker.kt index ffe0c0327..0ddffaec9 100644 --- a/compiler/src/prog8/ast/AstChecker.kt +++ b/compiler/src/prog8/ast/AstChecker.kt @@ -704,8 +704,8 @@ private class AstChecker(private val namespace: INameScope, } } - val leftDt = expr.left.resultingDatatype(namespace, heap)!! - val rightDt = expr.right.resultingDatatype(namespace, heap)!! + val leftDt = expr.left.resultingDatatype(namespace, heap) + val rightDt = expr.right.resultingDatatype(namespace, heap) if(leftDt !in NumericDatatypes) checkResult.add(ExpressionError("left operand is not numeric", expr.left.position)) if(rightDt!in NumericDatatypes) diff --git a/compiler/src/prog8/compiler/target/c64/AsmGen.kt b/compiler/src/prog8/compiler/target/c64/AsmGen.kt index d4804c223..9f95e006e 100644 --- a/compiler/src/prog8/compiler/target/c64/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/AsmGen.kt @@ -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... ") assemblyLines.clear() @@ -81,9 +81,11 @@ class AsmGen(val options: CompilationOptions, val program: IntermediateProgram, for(b in program.blocks) block2asm(b) - var optimizationsDone=1 - while(optimizationsDone>0) { - optimizationsDone = optimizeAssembly(assemblyLines) + if(optimize) { + var optimizationsDone = 1 + while (optimizationsDone > 0) { + optimizationsDone = optimizeAssembly(assemblyLines) + } } File("${program.name}.asm").printWriter().use {