vm var allocator now also recognises the memory-mapped variables. no longer crashes

This commit is contained in:
Irmen de Jong 2022-06-30 21:48:42 +02:00
parent 2ad4fdbbb9
commit f675dbc726
5 changed files with 65 additions and 29 deletions

View File

@ -40,6 +40,20 @@ class SymbolTable : StNode("", StNodeType.GLOBAL, Position.DUMMY) {
vars
}
val allMemMappedVariables: Collection<StMemVar> by lazy {
val vars = mutableListOf<StMemVar>()
fun collect(node: StNode) {
for(child in node.children) {
if(child.value.type== StNodeType.MEMVAR)
vars.add(child.value as StMemVar)
else
collect(child.value)
}
}
collect(this)
vars
}
override fun lookup(scopedName: List<String>) = flat[scopedName]
}
@ -186,7 +200,11 @@ class StConstant(name: String, val dt: DataType, val value: Double, position: Po
}
class StMemVar(name: String, val dt: DataType, val address: UInt, position: Position) :
class StMemVar(name: String,
val dt: DataType,
val address: UInt,
val length: Int?, // for arrays: the number of elements, for strings: number of characters *including* the terminating 0-byte
position: Position) :
StNode(name, StNodeType.MEMVAR, position) {
override fun printProperties() {
print("$name dt=$dt address=${address.toHex()}")

View File

@ -26,6 +26,18 @@ class VariableAllocator(private val st: SymbolTable, private val program: PtProg
allocations[variable.scopedName] = nextLocation
nextLocation += memsize
}
for (memvar in st.allMemMappedVariables) {
// TODO virtual machine doesn't have memory mapped variables, so treat them as regular allocated variables for now
val memsize =
when (memvar.dt) {
in NumericDatatypes -> program.memsizer.memorySize(memvar.dt)
in ArrayDatatypes -> program.memsizer.memorySize(memvar.dt, memvar.length!!)
else -> throw InternalCompilerException("weird dt")
}
allocations[memvar.scopedName] = nextLocation
nextLocation += memsize
}
freeMemoryStart = nextLocation
}

View File

@ -72,7 +72,13 @@ internal class SymbolTableMaker: IAstVisitor {
StStaticVariable(decl.name, decl.datatype, initialNumeric, initialString, initialArray, numElements, decl.zeropage, decl.position)
}
VarDeclType.CONST -> StConstant(decl.name, decl.datatype, (decl.value as NumericLiteral).number, decl.position)
VarDeclType.MEMORY -> StMemVar(decl.name, decl.datatype, (decl.value as NumericLiteral).number.toUInt(), decl.position)
VarDeclType.MEMORY -> {
val numElements =
if(decl.datatype in ArrayDatatypes)
decl.arraysize!!.constIndex()
else null
StMemVar(decl.name, decl.datatype, (decl.value as NumericLiteral).number.toUInt(), numElements, decl.position)
}
}
scopestack.peek().add(node)
// st.origAstLinks[decl] = node

View File

@ -3,9 +3,9 @@ TODO
For next release
^^^^^^^^^^^^^^^^
- assembler incorrectly assembles hello.asm now (crash when run)
- petaxian roller.p8 line 49 (also see test.p8) generates large code compared to 8.2
- why can't while and until loops use NOT condition instead of Cond==0 ? Fix this!
- assembler incorrectly assembles hello.asm now (crash when run)
- code gen for if statements has become inefficient? vm/6502?
if not diskio.iteration_in_progress or not num_bytes

View File

@ -6,38 +6,38 @@
main {
sub start() {
ubyte a1 = 0
ubyte a2 = 42
ubyte a3 = 1
ubyte a2 = 128
if (a1==0)==0
a3 = (a1==0)==0
if (a1!=0)==0
a3 = (a1!=0)==0
if (a1==0)!=0
a3 = (a1==0)!=0
; petaxian roller.p8 line 49
; super large in new version, ok in 8.2....
; TODO cx16.r0 = a2 + 25 + (a1/40)
; txt.setcc( a1, a2 + 25 + (a1/40), 11,22)
if (a1!=0)!=0
a3 = (a1!=0)!=0
if a1 and a2 {
a1++
}
if (a1==0) or (a2==0)
a3 = (a1==0) or (a2==0)
if a1!=99 and not a2 {
a1++
}
if (a1==0) and (a2==0)
a3 = (a1==0) and (a2==0)
if not a1 or not a2 or not(not(a3))
a3=not a1 or not a2 or not(not(a3))
if (a1==0) or (a2==0)
a3 = (a1==0) or (a2==0)
if (a1==0) and (a2==0)
a3 = (a1==0) and (a2==0)
txt.print_ub(a3)
; while a1 != a2 {
; a1++
; }
;
; while a1!='\"' {
; a1++
; }
; do {
; a1++
; } until a1==99
;
;close_end:
; a1++
; ; a "pixelshader":