From 435b9d89737acc8da63b2b4b0cd74ad05e248907 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Mon, 20 Feb 2023 23:29:16 +0100 Subject: [PATCH] get rid of 'noreinit' option for now, because it resulted in unreliable code --- .../src/prog8/code/core/CompilationOptions.kt | 1 - .../codegen/cpu6502/ProgramAndVarsGen.kt | 9 +------- .../prog8/codegen/intermediate/IRCodeGen.kt | 16 +++++++------- compiler/src/prog8/CompilerMain.kt | 3 --- compiler/src/prog8/compiler/Compiler.kt | 2 -- .../astprocessing/BeforeAsmAstChanger.kt | 21 ++----------------- compiler/test/TestCompilerOnExamples.kt | 1 - compiler/test/TestCompilerOptionLibdirs.kt | 1 - compiler/test/helpers/compileXyz.kt | 1 - docs/source/building.rst | 5 ----- .../src/prog8/http/TestHttp.kt | 1 - .../src/prog8/intermediate/IRFileReader.kt | 5 +---- .../src/prog8/intermediate/IRFileWriter.kt | 6 +----- intermediate/test/TestIRFileInOut.kt | 1 - .../src/prog8/vm/VmProgramLoader.kt | 20 +++++------------- 15 files changed, 17 insertions(+), 76 deletions(-) diff --git a/codeCore/src/prog8/code/core/CompilationOptions.kt b/codeCore/src/prog8/code/core/CompilationOptions.kt index 751d0422a..a971a8055 100644 --- a/codeCore/src/prog8/code/core/CompilationOptions.kt +++ b/codeCore/src/prog8/code/core/CompilationOptions.kt @@ -16,7 +16,6 @@ class CompilationOptions(val output: OutputType, var slowCodegenWarnings: Boolean = false, var optimize: Boolean = false, var optimizeFloatExpressions: Boolean = false, - var reinitGlobals: Boolean = true, var asmQuiet: Boolean = false, var asmListfile: Boolean = false, var experimentalCodegen: Boolean = false, diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt index 6ebca3e60..14de30269 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt @@ -205,14 +205,7 @@ internal class ProgramAndVarsGen( if (initializers.isNotEmpty()) { asmgen.out("prog8_init_vars\t.block") initializers.forEach { assign -> - if(options.reinitGlobals) { - asmgen.translate(assign) - } else { - // only re-init zeropage vars because non-zeropage vars will have a statically defined value - if(allocator.isZpVar(assign.target.identifier!!.name)) { - asmgen.translate(assign) - } - } + asmgen.translate(assign) } asmgen.out(" rts\n .bend") } diff --git a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt index 19a39a35c..fdcff9aa1 100644 --- a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt +++ b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt @@ -33,15 +33,13 @@ class IRCodeGen( if(options.evalStackBaseAddress!=null) throw AssemblyError("IR doesn't use eval-stack") - if(options.reinitGlobals) { - // collect global variables initializers - program.allBlocks().forEach { - val result = mutableListOf() - it.children.filterIsInstance().forEach { assign -> result += assignmentGen.translate(assign) } - result.forEach { chunk -> - if (chunk is IRCodeChunk) irProg.addGlobalInits(chunk) - else throw AssemblyError("only expect code chunk for global inits") - } + // collect global variables initializers + program.allBlocks().forEach { + val result = mutableListOf() + it.children.filterIsInstance().forEach { assign -> result += assignmentGen.translate(assign) } + result.forEach { chunk -> + if (chunk is IRCodeChunk) irProg.addGlobalInits(chunk) + else throw AssemblyError("only expect code chunk for global inits") } } diff --git a/compiler/src/prog8/CompilerMain.kt b/compiler/src/prog8/CompilerMain.kt index 5f05fd2e8..cc15de348 100644 --- a/compiler/src/prog8/CompilerMain.kt +++ b/compiler/src/prog8/CompilerMain.kt @@ -43,7 +43,6 @@ private fun compileMain(args: Array): Boolean { val experimentalCodegen by cli.option(ArgType.Boolean, fullName = "expericodegen", description = "use experimental/alternative codegen") val dontWriteAssembly by cli.option(ArgType.Boolean, fullName = "noasm", description="don't create assembly code") val dontOptimize by cli.option(ArgType.Boolean, fullName = "noopt", description = "don't perform any optimizations") - val dontReinitGlobals by cli.option(ArgType.Boolean, fullName = "noreinit", description = "don't create code to reinitialize globals on multiple runs of the program") val outputDir by cli.option(ArgType.String, fullName = "out", description = "directory for output files instead of current directory").default(".") val optimizeFloatExpressions by cli.option(ArgType.Boolean, fullName = "optfloatx", description = "optimize float expressions (warning: can increase program size)") val quietAssembler by cli.option(ArgType.Boolean, fullName = "quietasm", description = "don't print assembler output results") @@ -120,7 +119,6 @@ private fun compileMain(args: Array): Boolean { filepath, dontOptimize != true, optimizeFloatExpressions == true, - dontReinitGlobals != true, dontWriteAssembly != true, slowCodegenWarnings == true, quietAssembler == true, @@ -184,7 +182,6 @@ private fun compileMain(args: Array): Boolean { filepath, dontOptimize != true, optimizeFloatExpressions == true, - dontReinitGlobals != true, dontWriteAssembly != true, slowCodegenWarnings == true, quietAssembler == true, diff --git a/compiler/src/prog8/compiler/Compiler.kt b/compiler/src/prog8/compiler/Compiler.kt index 8f8c27592..7c04a46dd 100644 --- a/compiler/src/prog8/compiler/Compiler.kt +++ b/compiler/src/prog8/compiler/Compiler.kt @@ -30,7 +30,6 @@ class CompilationResult(val compilerAst: Program, // deprecated, use codegenAs class CompilerArguments(val filepath: Path, val optimize: Boolean, val optimizeFloatExpressions: Boolean, - val reinitGlobals: Boolean, val writeAssembly: Boolean, val slowCodegenWarnings: Boolean, val quietAssembler: Boolean, @@ -75,7 +74,6 @@ fun compileProgram(args: CompilerArguments): CompilationResult? { slowCodegenWarnings = args.slowCodegenWarnings optimize = args.optimize optimizeFloatExpressions = optimizeFloatExpr - reinitGlobals = args.reinitGlobals asmQuiet = args.quietAssembler asmListfile = args.asmListfile experimentalCodegen = args.experimentalCodegen diff --git a/compiler/src/prog8/compiler/astprocessing/BeforeAsmAstChanger.kt b/compiler/src/prog8/compiler/astprocessing/BeforeAsmAstChanger.kt index 27048e598..bcbc62e23 100644 --- a/compiler/src/prog8/compiler/astprocessing/BeforeAsmAstChanger.kt +++ b/compiler/src/prog8/compiler/astprocessing/BeforeAsmAstChanger.kt @@ -36,26 +36,9 @@ internal class BeforeAsmAstChanger(val program: Program, return noModifications } - override fun before(block: Block, parent: Node): Iterable { - // adjust global variables initialization - if(!options.reinitGlobals) { - block.statements.asSequence().filterIsInstance().forEach { - if(it.type==VarDeclType.VAR) { - it.findInitializer(program)?.let { initializer -> - it.value = initializer.value // put the init value back into the vardecl - } - } - } - } - - return noModifications - } - override fun after(decl: VarDecl, parent: Node): Iterable { - if(options.reinitGlobals) { - if (decl.type == VarDeclType.VAR && decl.value != null && decl.datatype in NumericDatatypes) - throw InternalCompilerException("vardecls for variables, with initial numerical value, should have been rewritten as plain vardecl + assignment $decl") - } + if (decl.type == VarDeclType.VAR && decl.value != null && decl.datatype in NumericDatatypes) + throw InternalCompilerException("vardecls for variables, with initial numerical value, should have been rewritten as plain vardecl + assignment $decl") return noModifications } diff --git a/compiler/test/TestCompilerOnExamples.kt b/compiler/test/TestCompilerOnExamples.kt index d5d587dac..5f5f61bfc 100644 --- a/compiler/test/TestCompilerOnExamples.kt +++ b/compiler/test/TestCompilerOnExamples.kt @@ -27,7 +27,6 @@ private fun compileTheThing(filepath: Path, optimize: Boolean, target: ICompilat filepath, optimize, optimizeFloatExpressions = true, - reinitGlobals = true, writeAssembly = true, slowCodegenWarnings = false, quietAssembler = true, diff --git a/compiler/test/TestCompilerOptionLibdirs.kt b/compiler/test/TestCompilerOptionLibdirs.kt index f858c89e8..040f6864c 100644 --- a/compiler/test/TestCompilerOptionLibdirs.kt +++ b/compiler/test/TestCompilerOptionLibdirs.kt @@ -44,7 +44,6 @@ class TestCompilerOptionSourcedirs: FunSpec({ filepath = filePath, optimize = false, optimizeFloatExpressions = false, - reinitGlobals = true, writeAssembly = true, slowCodegenWarnings = false, quietAssembler = true, diff --git a/compiler/test/helpers/compileXyz.kt b/compiler/test/helpers/compileXyz.kt index 92720bfc6..fff933890 100644 --- a/compiler/test/helpers/compileXyz.kt +++ b/compiler/test/helpers/compileXyz.kt @@ -25,7 +25,6 @@ internal fun compileFile( filepath, optimize, optimizeFloatExpressions = optFloatExpr, - reinitGlobals = true, writeAssembly = writeAssembly, slowCodegenWarnings = false, quietAssembler = true, diff --git a/docs/source/building.rst b/docs/source/building.rst index 77c07f01a..418ac8cdf 100644 --- a/docs/source/building.rst +++ b/docs/source/building.rst @@ -136,11 +136,6 @@ One or more .p8 module files Don't perform any code optimizations. Useful for debugging or faster compilation cycles. -``-noreinit`` - Don't create code to reinitialize the global (block level) variables on every run of the program. - When using this option, it is often no longer be possible to run the program correctly more than once, - however, the program may be a bit shorter as a result. - ``-optfloatx`` Also optimize float expressions if optimizations are enabled. Warning: can increase program size significantly if a lot of floating point expressions are used. diff --git a/httpCompilerService/src/prog8/http/TestHttp.kt b/httpCompilerService/src/prog8/http/TestHttp.kt index 8d9a8a47c..a1c58495d 100644 --- a/httpCompilerService/src/prog8/http/TestHttp.kt +++ b/httpCompilerService/src/prog8/http/TestHttp.kt @@ -34,7 +34,6 @@ class RequestParser : Take { Path(a), optimize = true, optimizeFloatExpressions = false, - reinitGlobals = true, writeAssembly = true, slowCodegenWarnings = true, compilationTarget = "c64", diff --git a/intermediate/src/prog8/intermediate/IRFileReader.kt b/intermediate/src/prog8/intermediate/IRFileReader.kt index 13158cedc..872eb6a76 100644 --- a/intermediate/src/prog8/intermediate/IRFileReader.kt +++ b/intermediate/src/prog8/intermediate/IRFileReader.kt @@ -82,7 +82,6 @@ class IRFileReader { var zeropage = ZeropageType.FULL val zpReserved = mutableListOf() var loadAddress = target.machine.PROGRAM_LOAD_ADDRESS - var reInitGlobals = true var optimize = true var evalStackBaseAddress: UInt? = null var outputDir = Path("") @@ -105,7 +104,6 @@ class IRFileReader { "launcher" -> launcher = CbmPrgLauncherType.valueOf(value) "zeropage" -> zeropage = ZeropageType.valueOf(value) "loadAddress" -> loadAddress = value.toUInt() - "reinitGlobals" -> reInitGlobals = value.toBoolean() "evalStackBaseAddress" -> evalStackBaseAddress = if(value=="null") null else parseIRValue(value).toUInt() "zpReserved" -> { val (zpstart, zpend) = value.split(',') @@ -129,8 +127,7 @@ class IRFileReader { loadAddress, evalStackBaseAddress = evalStackBaseAddress, outputDir = outputDir, - optimize = optimize, - reinitGlobals = reInitGlobals + optimize = optimize ) } diff --git a/intermediate/src/prog8/intermediate/IRFileWriter.kt b/intermediate/src/prog8/intermediate/IRFileWriter.kt index 4b0004ee2..8b8f8217d 100644 --- a/intermediate/src/prog8/intermediate/IRFileWriter.kt +++ b/intermediate/src/prog8/intermediate/IRFileWriter.kt @@ -22,10 +22,7 @@ class IRFileWriter(private val irProgram: IRProgram, outfileOverride: Path?) { writeVariables() out.write("\n\n") - if(irProgram.options.reinitGlobals) { - // this a code that re-loads startup values into all global (block-level) variables. - writeCodeChunk(irProgram.globalInits) - } + writeCodeChunk(irProgram.globalInits) out.write("\n") writeBlocks() out.write("\n") @@ -130,7 +127,6 @@ class IRFileWriter(private val irProgram: IRProgram, outfileOverride: Path?) { } out.write("loadAddress=${irProgram.options.loadAddress.toHex()}\n") out.write("optimize=${irProgram.options.optimize}\n") - out.write("reinitGlobals=${irProgram.options.reinitGlobals}\n") out.write("evalStackBaseAddress=${irProgram.options.evalStackBaseAddress?.toHex()}\n") out.write("outputDir=${irProgram.options.outputDir.toAbsolutePath()}\n") // other options not yet useful here? diff --git a/intermediate/test/TestIRFileInOut.kt b/intermediate/test/TestIRFileInOut.kt index 51f9a7fac..975db62a7 100644 --- a/intermediate/test/TestIRFileInOut.kt +++ b/intermediate/test/TestIRFileInOut.kt @@ -48,7 +48,6 @@ output=PRG launcher=BASIC zeropage=KERNALSAFE loadAddress=0 -reinitGlobals=false evalStackBaseAddress=null diff --git a/virtualmachine/src/prog8/vm/VmProgramLoader.kt b/virtualmachine/src/prog8/vm/VmProgramLoader.kt index b4942d208..6a67c946c 100644 --- a/virtualmachine/src/prog8/vm/VmProgramLoader.kt +++ b/virtualmachine/src/prog8/vm/VmProgramLoader.kt @@ -1,6 +1,8 @@ package prog8.vm -import prog8.code.core.* +import prog8.code.core.ArrayDatatypes +import prog8.code.core.AssemblyError +import prog8.code.core.DataType import prog8.intermediate.* class VmProgramLoader { @@ -15,10 +17,8 @@ class VmProgramLoader { varsToMemory(irProgram, allocations, variableAddresses, memory) - if(irProgram.options.reinitGlobals) { - if(irProgram.globalInits.isNotEmpty()) - programChunks += irProgram.globalInits - } + if(irProgram.globalInits.isNotEmpty()) + programChunks += irProgram.globalInits // make sure that if there is a "main.start" entrypoint, we jump to it irProgram.blocks.firstOrNull()?.let { @@ -196,16 +196,6 @@ class VmProgramLoader { else -> throw IRParseException("invalid dt") } } - } else { - if(!program.options.reinitGlobals) { - // variable is not initialized as part of a global reinitialization, so we clear it here - when(variable.dt) { - in ByteDatatypes -> memory.setUB(addr, 0u) - in WordDatatypes -> memory.setUW(addr, 0u) - DataType.FLOAT -> memory.setFloat(addr, 0.0f) - else -> throw IRParseException("invalid dt") - } - } } }