fixing array indexing on pointers

This commit is contained in:
Irmen de Jong
2025-07-29 23:41:38 +02:00
parent c39d570b72
commit c4c5636a81
4 changed files with 17 additions and 14 deletions

View File

@@ -111,7 +111,7 @@ class IRCodeGen(
if('.' !in node.name) { if('.' !in node.name) {
// there is 1 case where the identifier is not scoped: if it's the value field name after a pointer array indexing. // there is 1 case where the identifier is not scoped: if it's the value field name after a pointer array indexing.
val expr = node.parent as? PtBinaryExpression val expr = node.parent as? PtBinaryExpression
if (expr?.operator != "." || expr.right !== node || expr.left !is PtArrayIndexer || !expr.left.type.isPointer) if (expr?.operator != "." || expr.right !== node || expr.left !is PtArrayIndexer || (!expr.left.type.isPointer && !expr.left.type.isStructInstance))
require('.' in node.name) { "node $node name is not scoped: ${node.name}" } require('.' in node.name) { "node $node name is not scoped: ${node.name}" }
} }
} }

View File

@@ -241,16 +241,11 @@ class BinaryExpression(
} else if (leftIndexer != null && rightIdentifier.nameInSource.size == 1) { } else if (leftIndexer != null && rightIdentifier.nameInSource.size == 1) {
// ARRAY[x].NAME --> maybe it's a pointer dereference // ARRAY[x].NAME --> maybe it's a pointer dereference
val dt = leftIndexer.inferType(program).getOrUndef() val dt = leftIndexer.inferType(program).getOrUndef()
if (dt.isPointer) { if (dt.isPointer) dt.dereference().subType else dt.subType
dt.dereference().subType
} else null
} else if (leftExpr != null) { } else if (leftExpr != null) {
// SOMEEXPRESSION . NAME // SOMEEXPRESSION . NAME
val leftDt = leftExpr.inferType(program) val leftDt = leftExpr.inferType(program)
if (leftDt.isPointer) if (leftDt.isPointer) leftDt.getOrUndef().subType else null
leftDt.getOrUndef().subType
else
null
} else null } else null
if (struct == null) if (struct == null)
InferredTypes.unknown() InferredTypes.unknown()
@@ -1733,9 +1728,12 @@ class ArrayIndexedPtrDereference(
val arrayIdentifier = chain.map { it.first } val arrayIdentifier = chain.map { it.first }
val symbol = definingScope.lookup(arrayIdentifier) as? VarDecl val symbol = definingScope.lookup(arrayIdentifier) as? VarDecl
if(symbol!=null) { if(symbol!=null) {
require(symbol.datatype.isArray) if(symbol.datatype.isArray)
return InferredTypes.knownFor(symbol.datatype.sub!!) return InferredTypes.knownFor(symbol.datatype.sub!!)
else if(symbol.datatype.isPointer)
return InferredTypes.knownFor(symbol.datatype.dereference())
} }
} }
// too hard to determine the type....? // too hard to determine the type....?
return InferredTypes.unknown() return InferredTypes.unknown()

View File

@@ -1,8 +1,9 @@
TODO TODO
==== ====
fix ^^Node nodes / cx16.r0L = nodes[2].weight
don't write pointer types into P8IR files, just write uword as the type? (actually breaks the VARIABLESWITHINIT now that zp vars get initialized to 0 again; all the pointer examples won't compile anymore) don't write pointer types into P8IR files, just write uword as the type? (actually breaks the VARIABLESWITHINIT now that zp vars get initialized to 0 again; all the pointer examples won't compile anymore)
fix ^^Node nodes / cx16.r0L = nodes[2].weight (TODO("IR datatype for struct instances")
fix bool bb2 = bptr[2]^^ ... peekbool(bptr[2]) gives a arg 1 type error... just omit peekbool() here?
fix countries[2]^^ = 0 compiler crash fix countries[2]^^ = 0 compiler crash
fix passing array of structptrs to subroutine , arg type mismatches fix passing array of structptrs to subroutine , arg type mismatches
disallow ^^str disallow ^^str

View File

@@ -1,12 +1,16 @@
main { main {
sub start() { sub start() {
struct Node { struct Node {
ubyte weight ubyte weight
} }
^^Node nodes ^^Node nodes
nodes^^.zzz = 99 ^^Node[10] array
cx16.r0L = nodes^^.zzz ^^bool bptr
cx16.r0L = nodes[2].value bool bb1 = bptr[2]
bool bb2 = bptr[2]^^ ; TODO fix this ... peekbool(bptr[2]) gives a arg 1 type error... just omit peekbool() here?
cx16.r0L = array[2].weight
cx16.r1L = nodes[2].weight ; TODO implement support for this one
} }
} }