vm: support noreinit option

This commit is contained in:
Irmen de Jong 2022-04-03 17:07:26 +02:00
parent 95f16c38a9
commit 5b3ccab7dc
4 changed files with 19 additions and 38 deletions

View File

@ -27,8 +27,10 @@ internal class AssemblyProgram(override val name: String,
}
out.write("------PROGRAM------\n")
out.write("; global var inits\n")
globalInits.forEach { out.writeLine(it) }
if(!options.dontReinitGlobals) {
out.write("; global var inits\n")
globalInits.forEach { out.writeLine(it) }
}
out.write("; actual program code\n")
blocks.asSequence().flatMap { it.lines }.forEach { line->out.writeLine(line) }

View File

@ -35,19 +35,16 @@ class CodeGen(internal val program: PtProgram,
private val builtinFuncGen = BuiltinFuncGen(this, expressionEval)
internal val vmRegisters = VmRegisterPool()
init {
if(options.dontReinitGlobals)
TODO("support no globals re-init in vm")
}
override fun compileToAssembly(): IAssemblyProgram? {
val vmprog = AssemblyProgram(program.name, allocations)
// collect global variables initializers
program.allBlocks().forEach {
val code = VmCodeChunk()
it.children.filterIsInstance<PtAssignment>().forEach { assign -> code += translate(assign) }
vmprog.addGlobalInits(code)
if(!options.dontReinitGlobals) {
// collect global variables initializers
program.allBlocks().forEach {
val code = VmCodeChunk()
it.children.filterIsInstance<PtAssignment>().forEach { assign -> code += translate(assign) }
vmprog.addGlobalInits(code)
}
}
for (block in program.allBlocks()) {

View File

@ -3,10 +3,10 @@ TODO
For next release
^^^^^^^^^^^^^^^^
- vm: support no globals re-init option
- vm: make registers typed? so that it's immediately obvious what type they represent. Much like regular variables in memory.
- vm: don't store symbol names in instructions to make optimizing the IR easier? but what about jumps to labels. And it's no longer readable by humans.
- vm codegen/assembler: variable memory locations should also be referenced by the variable name instead of just the address, to make the output more human-readable
- vm: make registers typed? so that it's immediately obvious what type they represent. Much like regular variables in memory.
so we have a set of byte registers, a set of word registers, and other sets if we introduce other types.
- vm: don't store symbol names in instructions to make optimizing the IR easier? but what about jumps to labels. And it's no longer readable by humans.
- vm: how to remove all unused subroutines? (in the assembly codegen, we let 64tass solve this for us)
- vm: rather than being able to jump to any 'address' (IPTR), use 'blocks' that have entry and exit points -> even better dead code elimination possible too
- when the vm is stable and *if* its language can get promoted to prog8 IL, the variable allocation should be changed.

View File

@ -1,34 +1,16 @@
%import textio
%zeropage basicsafe
%zeropage dontuse
; NOTE: meant to test to virtual machine output target (use -target vitual)
main {
sub func1(ubyte arg1) -> uword {
return arg1 * 3
}
sub func2(uword arg1) -> uword {
return arg1+1000
}
sub func3(uword arg1) {
txt.print_uw(arg1+2000)
txt.nl()
}
ubyte global = 42
sub start() {
ubyte source = 99
uword result = func2(func1(cos8u(sin8u(source))))
txt.print_uw(result) ; 1043
txt.nl()
result = source |> sin8u() |> cos8u() |> func1() |> func2()
txt.print_uw(result) ; 1043
txt.nl()
source |> sin8u() |> cos8u() |> func1() |> func3() ; 2043
txt.print_ub(global)
global++
; a "pixelshader":
; syscall1(8, 0) ; enable lo res creen