revert & to untyped pointer, added && for typed pointer address-of

This commit is contained in:
Irmen de Jong
2025-07-07 15:53:33 +02:00
parent f9fbfe30e3
commit 9f6106452e
12 changed files with 81 additions and 64 deletions

View File

@@ -280,10 +280,17 @@ class Antlr2KotlinVisitor(val source: SourceCode): AbstractParseTreeVisitor<Node
val msb = ctx.ADDRESS_OF_MSB()!=null
// note: &< (ADDRESS_OF_LSB) is equivalent to a regular &.
val index = ctx.arrayindex()?.accept(this) as? ArrayIndex
return if(index!=null) {
AddressOf(identifier, index, null, msb, ctx.toPosition())
var typed = false
if(ctx.TYPED_ADDRESS_OF()!=null) {
// new typed AddressOf
if(msb)
throw SyntaxError("typed address of not allowed with msb", ctx.toPosition())
typed = true
}
return if (index != null) {
AddressOf(identifier, index, null, msb, typed, ctx.toPosition())
} else {
AddressOf(identifier,null, null, msb, ctx.toPosition())
AddressOf(identifier, null, null, msb, typed, ctx.toPosition())
}
}

View File

@@ -502,7 +502,8 @@ class TypecastExpression(var expression: Expression, var type: DataType, val imp
}
}
data class AddressOf(var identifier: IdentifierReference?, var arrayIndex: ArrayIndex?, var dereference: PtrDereference?, val msb: Boolean, override val position: Position) : Expression() {
data class AddressOf(var identifier: IdentifierReference?, var arrayIndex: ArrayIndex?, var dereference: PtrDereference?,
val msb: Boolean, val typed: Boolean, override val position: Position) : Expression() {
override lateinit var parent: Node
override fun linkParents(parent: Node) {
@@ -538,7 +539,7 @@ data class AddressOf(var identifier: IdentifierReference?, var arrayIndex: Array
replacement.parent = this
}
override fun copy() = AddressOf(identifier?.copy(), arrayIndex?.copy(), dereference?.copy(), msb, position)
override fun copy() = AddressOf(identifier?.copy(), arrayIndex?.copy(), dereference?.copy(), msb, typed, position)
override fun constValue(program: Program): NumericLiteral? {
if(msb)
return null
@@ -574,8 +575,9 @@ data class AddressOf(var identifier: IdentifierReference?, var arrayIndex: Array
return null
}
override fun referencesIdentifier(nameInSource: List<String>) = identifier?.nameInSource==nameInSource || arrayIndex?.referencesIdentifier(nameInSource)==true || dereference?.referencesIdentifier(nameInSource)==true
// override fun inferType(program: Program): InferredTypes.InferredType = InferredTypes.knownFor(BaseDataType.UWORD) // TODO orignal behavior
override fun inferType(program: Program): InferredTypes.InferredType {
if(!typed)
return InferredTypes.knownFor(BaseDataType.UWORD) // orignal pre-v12 untyped AddressOf
if(identifier!=null) {
val type = identifier!!.inferType(program).getOrUndef()
val addrofDt = type.typeForAddressOf(msb)

View File

@@ -270,7 +270,7 @@ class VarDecl(
// parameter variable memory mapped to a R0-R15 virtual register
val regname = param.registerOrPair.asScopedNameVirtualReg(param.type)
decltype = VarDeclType.MEMORY
value = AddressOf(IdentifierReference(regname, param.position), null, null, false, param.position)
value = AddressOf(IdentifierReference(regname, param.position), null, null, false, false,param.position)
}
val dt = if(param.type.isArray) DataType.UWORD else param.type
return VarDecl(decltype, VarDeclOrigin.SUBROUTINEPARAM, dt, param.zp, SplitWish.DONTCARE, null, param.name, emptyList(), value,