slightly optimized assembly file handling

This commit is contained in:
Irmen de Jong 2022-01-10 22:48:20 +01:00
parent b29c3152db
commit 789e39c719

View File

@ -61,6 +61,9 @@ class AsmGen(private val program: Program,
println("Generating assembly code... ")
val allInitializers = blockVariableInitializers.asSequence().flatMap { it.value }
require(allInitializers.all { it.origin==AssignmentOrigin.VARINIT }) {"all block-level assignments must be a variable initializer"}
header()
val allBlocks = program.allBlocks
if(allBlocks.first().name != "main")
@ -78,19 +81,18 @@ class AsmGen(private val program: Program,
slaballocations()
footer()
val output = outputDir.resolve("${program.name}.asm")
if(options.optimize) {
val separateLines = assemblyLines.flatMap { it.split('\n') }
val separateLines = assemblyLines.flatMapTo(mutableListOf()) { it.split('\n') }
assemblyLines.clear()
assemblyLines.addAll(separateLines)
require(assemblyLines.all { !it.contains('\n') })
var optimizationsDone = 1
while (optimizationsDone > 0) {
optimizationsDone = optimizeAssembly(assemblyLines, options.compTarget.machine, program)
while(optimizeAssembly(separateLines, options.compTarget.machine, program)>0) {
// optimize the assembly source code
}
output.writeLines(separateLines)
} else {
output.writeLines(assemblyLines)
}
outputDir.resolve("${program.name}.asm").writeLines(assemblyLines)
return if(errors.noErrors())
AssemblyProgram(true, program.name, outputDir, compTarget.name)
else {