make IRFileReader's file source more general

This commit is contained in:
Irmen de Jong 2022-09-26 14:44:24 +02:00
parent 8883513b0e
commit 533c368e32
7 changed files with 31 additions and 29 deletions

View File

@ -22,7 +22,7 @@ class CodeGen(private val program: PtProgram,
val irProgram = irCodeGen.generate()
// this stub only writes the IR program to disk but doesn't generate anything else.
IRFileWriter(irProgram).writeFile()
IRFileWriter(irProgram, null).write()
println("** experimental codegen stub: no assembly generated **")
return null

View File

@ -9,7 +9,7 @@ import prog8.code.core.IErrorReporter
import prog8.codegen.intermediate.IRCodeGen
import prog8.intermediate.IRFileReader
import prog8.intermediate.IRFileWriter
import kotlin.io.path.Path
import java.nio.file.Path
class VmCodeGen(private val program: PtProgram,
private val symbolTable: SymbolTable,
@ -23,8 +23,8 @@ class VmCodeGen(private val program: PtProgram,
return if(options.keepIR) {
//create IR file on disk and read it back.
IRFileWriter(irProgram).writeFile()
val irProgram2 = IRFileReader(options.outputDir, irProgram.name).readFile()
val irFile = IRFileWriter(irProgram, null).write()
val irProgram2 = IRFileReader().read(irFile)
VmAssemblyProgram(irProgram2.name, irProgram2)
} else {
VmAssemblyProgram(irProgram.name, irProgram)
@ -32,8 +32,8 @@ class VmCodeGen(private val program: PtProgram,
}
companion object {
fun compileIR(listingFilename: String): IAssemblyProgram {
val irProgram = IRFileReader(Path(""), listingFilename).readFile()
fun compileIR(irFile: Path): IAssemblyProgram {
val irProgram = IRFileReader().read(irFile)
return VmAssemblyProgram(irProgram.name, irProgram)
}
}

View File

@ -245,10 +245,11 @@ private fun processSymbolDefs(symbolDefs: List<String>): Map<String, String>? {
return result
}
fun runVm(listingFilename: String): Boolean {
if(listingFilename.endsWith(".p8ir")) {
val withoutSuffix = listingFilename.substring(0, listingFilename.length-5)
val compiled = VmCodeGen.compileIR(withoutSuffix)
fun runVm(irFilename: String): Boolean {
val irFile = Path(irFilename)
if(irFilename.endsWith(".p8ir")) {
val withoutSuffix = irFilename.substring(0, irFilename.length-5)
val compiled = VmCodeGen.compileIR(irFile)
if (!compiled.assemble(CompilationOptions( // these are just dummy options, the actual options are inside the .p8ir file itself:
OutputType.PRG,
CbmPrgLauncherType.NONE,
@ -267,6 +268,6 @@ fun runVm(listingFilename: String): Boolean {
return true
}
val vmdef = VirtualMachineDefinition()
vmdef.launchEmulator(0, Paths.get(listingFilename))
vmdef.launchEmulator(0, irFile)
return true
}

View File

@ -3,6 +3,8 @@ TODO
For next release
^^^^^^^^^^^^^^^^
- vm: get rid of p8virt format and Assembler, run p8ir directly
...
@ -17,7 +19,6 @@ Future Things and Ideas
^^^^^^^^^^^^^^^^^^^^^^^
Compiler:
- vm: get rid of p8virt format and Assembler, run p8ir directly
- vm/ir: put variables and arrays in BSS section (unless -noreinit is specified)
- vm: Jumps go to a code block rather than a specific address(label) -> also helps future dead code elimination?
- vm: the above means that every label introduces a new code block. This eliminates the use of actual labels altogether.

View File

@ -6,17 +6,18 @@ import prog8.code.target.*
import java.nio.file.Path
import kotlin.io.path.Path
import kotlin.io.path.bufferedReader
import kotlin.io.path.div
class IRFileReader(outputDir: Path, programName: String) {
private val infile = outputDir / ("${programName}.p8ir")
class IRFileReader {
fun readFile(): IRProgram {
println("Reading intermediate representation from $infile")
infile.bufferedReader().use { reader ->
val lines = reader.readText().lines()
return parseProgram(lines.iterator())
fun read(irSourceCode: CharSequence): IRProgram {
return parseProgram(irSourceCode.lineSequence().iterator())
}
fun read(irSourceFile: Path): IRProgram {
println("Reading intermediate representation from $irSourceFile")
irSourceFile.bufferedReader().use { reader ->
return parseProgram(reader.lineSequence().iterator())
}
}

View File

@ -2,15 +2,16 @@ package prog8.intermediate
import prog8.code.core.*
import java.io.BufferedWriter
import java.nio.file.Path
import kotlin.io.path.bufferedWriter
import kotlin.io.path.div
class IRFileWriter(private val irProgram: IRProgram) {
private val outfile = irProgram.options.outputDir / ("${irProgram.name}.p8ir")
class IRFileWriter(private val irProgram: IRProgram, outfileOverride: Path?) {
private val outfile = outfileOverride ?: (irProgram.options.outputDir / ("${irProgram.name}.p8ir"))
private val out = outfile.bufferedWriter()
fun writeFile() {
fun write(): Path {
println("Writing intermediate representation to $outfile")
out.write("<PROGRAM NAME=${irProgram.name}>\n")
writeOptions()
@ -27,6 +28,7 @@ class IRFileWriter(private val irProgram: IRProgram) {
writeBlocks()
out.write("</PROGRAM>\n")
out.close()
return outfile
}
private fun writeBlocks() {

View File

@ -30,9 +30,8 @@ class TestIRFileInOut: FunSpec({
outputDir = tempdir
)
val program = IRProgram("unittest-irwriter", IRSymbolTable(null), options, target)
val writer = IRFileWriter(program)
writer.writeFile()
val generatedFile = tempdir.resolve("unittest-irwriter.p8ir")
val writer = IRFileWriter(program, null)
val generatedFile = writer.write()
val lines = generatedFile.readLines()
lines.first() shouldBe "<PROGRAM NAME=unittest-irwriter>"
lines.last() shouldBe "</PROGRAM>"
@ -97,9 +96,7 @@ return
"""
val tempfile = createTempFile(suffix = ".p8ir")
tempfile.writeText(source)
val filepart = tempfile.name.dropLast(5)
val reader = IRFileReader(tempfile.parent, filepart)
val program = reader.readFile()
val program = IRFileReader().read(tempfile)
tempfile.deleteExisting()
program.name shouldBe "test-ir-reader"
program.blocks.size shouldBe 2