rewrite pointer[0] into @(pointer) if its ^^ubyte

This commit is contained in:
Irmen de Jong
2025-05-15 23:33:29 +02:00
parent bb1953267d
commit 4f0839f27e
6 changed files with 40 additions and 29 deletions

View File

@@ -1,23 +1,23 @@
<component name="libraryTable"> <component name="libraryTable">
<library name="KotlinJavaRuntime" type="repository"> <library name="KotlinJavaRuntime" type="repository">
<properties maven-id="org.jetbrains.kotlin:kotlin-stdlib-jdk8:2.1.20" /> <properties maven-id="org.jetbrains.kotlin:kotlin-stdlib-jdk8:2.1.21" />
<CLASSES> <CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/2.1.20/kotlin-stdlib-jdk8-2.1.20.jar!/" /> <root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/2.1.21/kotlin-stdlib-jdk8-2.1.21.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/2.1.20/kotlin-stdlib-2.1.20.jar!/" /> <root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/2.1.21/kotlin-stdlib-2.1.21.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0.jar!/" /> <root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/2.1.20/kotlin-stdlib-jdk7-2.1.20.jar!/" /> <root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/2.1.21/kotlin-stdlib-jdk7-2.1.21.jar!/" />
</CLASSES> </CLASSES>
<JAVADOC> <JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/2.1.20/kotlin-stdlib-jdk8-2.1.20-javadoc.jar!/" /> <root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/2.1.21/kotlin-stdlib-jdk8-2.1.21-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/2.1.20/kotlin-stdlib-2.1.20-javadoc.jar!/" /> <root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/2.1.21/kotlin-stdlib-2.1.21-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0-javadoc.jar!/" /> <root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/2.1.20/kotlin-stdlib-jdk7-2.1.20-javadoc.jar!/" /> <root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/2.1.21/kotlin-stdlib-jdk7-2.1.21-javadoc.jar!/" />
</JAVADOC> </JAVADOC>
<SOURCES> <SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/2.1.20/kotlin-stdlib-jdk8-2.1.20-sources.jar!/" /> <root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/2.1.21/kotlin-stdlib-jdk8-2.1.21-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/2.1.20/kotlin-stdlib-2.1.20-sources.jar!/" /> <root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/2.1.21/kotlin-stdlib-2.1.21-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0-sources.jar!/" /> <root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/2.1.20/kotlin-stdlib-jdk7-2.1.20-sources.jar!/" /> <root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/2.1.21/kotlin-stdlib-jdk7-2.1.21-sources.jar!/" />
</SOURCES> </SOURCES>
</library> </library>
</component> </component>

View File

@@ -3,7 +3,7 @@ import org.jetbrains.kotlin.gradle.internal.config.LanguageFeature
plugins { plugins {
kotlin("jvm") version "2.1.20" kotlin("jvm") version "2.1.21"
} }
allprojects { allprojects {

View File

@@ -209,7 +209,7 @@ _after:
// "array indexing is limited to byte size 0..255" error for pointervariables. // "array indexing is limited to byte size 0..255" error for pointervariables.
val indexExpr = arrayIndexedExpression.indexer.indexExpr val indexExpr = arrayIndexedExpression.indexer.indexExpr
val arrayVar = arrayIndexedExpression.arrayvar.targetVarDecl() val arrayVar = arrayIndexedExpression.arrayvar.targetVarDecl()
if(arrayVar!=null && arrayVar.datatype.isUnsignedWord) { if(arrayVar!=null && (arrayVar.datatype.isUnsignedWord || (arrayVar.datatype.isPointer && arrayVar.datatype.sub==BaseDataType.UBYTE))) {
val wordIndex = TypecastExpression(indexExpr, DataType.UWORD, true, indexExpr.position) val wordIndex = TypecastExpression(indexExpr, DataType.UWORD, true, indexExpr.position)
val address = BinaryExpression(arrayIndexedExpression.arrayvar.copy(), "+", wordIndex, arrayIndexedExpression.position) val address = BinaryExpression(arrayIndexedExpression.arrayvar.copy(), "+", wordIndex, arrayIndexedExpression.position)
return if(parent is AssignTarget) { return if(parent is AssignTarget) {

View File

@@ -280,26 +280,27 @@ class TypecastsAdder(val program: Program, val options: CompilationOptions, val
private fun afterFunctionCallArgs(call: IFunctionCall): Iterable<IAstModification> { private fun afterFunctionCallArgs(call: IFunctionCall): Iterable<IAstModification> {
// see if a typecast is needed to convert the arguments into the required parameter type // see if a typecast is needed to convert the arguments into the required parameter type
val modifications = mutableListOf<IAstModification>() val modifications = mutableListOf<IAstModification>()
val params = when(val sub = call.target.targetStatement(program)) { val paramsPossibleDatatypes = when(val sub = call.target.targetStatement(program)) {
is BuiltinFunctionPlaceholder -> BuiltinFunctions.getValue(sub.name).parameters.toList() is BuiltinFunctionPlaceholder -> BuiltinFunctions.getValue(sub.name).parameters.map { it.possibleDatatypes.map { dt -> DataType.forDt(dt)} }
is Subroutine -> sub.parameters.map { FParam(it.name, it.type.base) } is Subroutine -> sub.parameters.map { listOf(it.type) }
is StructDecl -> sub.fields.map { FParam(it.second, it.first.base) } is StructDecl -> sub.fields.map { listOf(it.first) }
else -> emptyList() else -> emptyList()
} }
params.zip(call.args).forEach { paramsPossibleDatatypes.zip(call.args).forEach {
val possibleTargetDt = it.first.possibleDatatypes.first() val possibleTargetDt = it.first.first()
val targetDt = if(possibleTargetDt.isPointer) BaseDataType.UWORD else possibleTargetDt // use UWORD instead of a pointer type (using words for pointers is allowed without further casting) val targetDt = if(possibleTargetDt.isPointer) DataType.UWORD else possibleTargetDt // use UWORD instead of a pointer type (using words for pointers is allowed without further casting)
val argIdt = it.second.inferType(program) val argIdt = it.second.inferType(program)
if (argIdt.isKnown && !targetDt.isStructInstance) { if (argIdt.isKnown && !targetDt.isStructInstance) {
val argDt = argIdt.getOrUndef() val argDt = argIdt.getOrUndef()
if (argDt.base !in it.first.possibleDatatypes) { if (argDt !in it.first) {
val identifier = it.second as? IdentifierReference val identifier = it.second as? IdentifierReference
val number = it.second as? NumericLiteral val number = it.second as? NumericLiteral
if(number!=null) { if(number!=null) {
addTypecastOrCastedValueModification(modifications, it.second, DataType.forDt(targetDt), call as Node) addTypecastOrCastedValueModification(modifications, it.second, targetDt, call as Node)
} else if(identifier!=null && targetDt==BaseDataType.UWORD && argDt.isPassByRef) { } else if(identifier!=null && targetDt.isUnsignedWord && argDt.isPassByRef) {
if(!identifier.isSubroutineParameter()) { if(!identifier.isSubroutineParameter()) {
// We allow STR/ARRAY values for UWORD parameters. // We allow STR/ARRAY values for UWORD parameters.
// If it's an array (not STR), take the address. // If it's an array (not STR), take the address.
@@ -311,16 +312,16 @@ class TypecastsAdder(val program: Program, val options: CompilationOptions, val
) )
} }
} }
} else if(targetDt==BaseDataType.BOOL) { } else if(targetDt.isBool) {
addTypecastOrCastedValueModification(modifications, it.second, DataType.BOOL, call as Node) addTypecastOrCastedValueModification(modifications, it.second, DataType.BOOL, call as Node)
} else if(!targetDt.isIterable && argDt isAssignableTo DataType.forDt(targetDt)) { } else if(!targetDt.isIterable && argDt isAssignableTo targetDt) {
if(!argDt.isString || targetDt!=BaseDataType.UWORD) if(!argDt.isString || !targetDt.isUnsignedWord)
addTypecastOrCastedValueModification(modifications, it.second, DataType.forDt(targetDt), call as Node) addTypecastOrCastedValueModification(modifications, it.second, targetDt, call as Node)
} }
} }
} else { } else {
val identifier = it.second as? IdentifierReference val identifier = it.second as? IdentifierReference
if(identifier!=null && targetDt==BaseDataType.UWORD) { if(identifier!=null && targetDt.isUnsignedWord) {
// take the address of the identifier // take the address of the identifier
modifications += IAstModification.ReplaceNode( modifications += IAstModification.ReplaceNode(
identifier, identifier,

View File

@@ -45,9 +45,9 @@ STRUCTS and TYPED POINTERS
- DONE: passing STR to a subroutine: parameter type becomes ^^UBYTE (rather than UWORD) (we still lose the bounds check) - DONE: passing STR to a subroutine: parameter type becomes ^^UBYTE (rather than UWORD) (we still lose the bounds check)
- DONE: passing ARRAY to a subroutine: parameter type becomes ^^ElementDt (rather than UWORD) (we still lose the bounds check) - DONE: passing ARRAY to a subroutine: parameter type becomes ^^ElementDt (rather than UWORD) (we still lose the bounds check)
- DONE: @(ptr) complains that ptr is not uword when ptr is ^^ubyte (should be allowed) - DONE: @(ptr) complains that ptr is not uword when ptr is ^^ubyte (should be allowed)
- DONE: pointer[0] should be replaced with @(pointer) if pointer is ^^ubyte, so these are now all identical: ptr[0], ptr^^, @(ptr) if ptr is ^^ubyte
- DONE: STR should be asssignment compatible with UBYTE^^ but local scoped STR should still be accessed directly using LDA str,Y instead of through the pointer, like arrays.
- fix actual _msb/_lsb storage of the split-words pointer-arrays - fix actual _msb/_lsb storage of the split-words pointer-arrays
- STR should be asssignment compatible with UBYTE^^ but local scoped STR should still be accessed directly using LDA str,Y instead of through the pointer, like arrays.
- FParam's class PossibleDatatypes should be able to include pointer types too such as ^^ubyte, but maybe not for builtin functions only for user defined subroutines?
- make typeForAddressOf() be even more specific about the typed pointers it returns for the address-of operator. + unit test. Needs fixes in 6502 codegen too though... (also recheck passing STR and ARRAY types to subroutines) - make typeForAddressOf() be even more specific about the typed pointers it returns for the address-of operator. + unit test. Needs fixes in 6502 codegen too though... (also recheck passing STR and ARRAY types to subroutines)
- fixing the pointer dereferencing issues (cursed hybrid beween IdentifierReference, PtrDereferece and PtrIndexedDereference) may require getting rid of scoped identifiers altogether and treat '.' as a "scope or pointer following operator" - fixing the pointer dereferencing issues (cursed hybrid beween IdentifierReference, PtrDereferece and PtrIndexedDereference) may require getting rid of scoped identifiers altogether and treat '.' as a "scope or pointer following operator"
- (later, nasty parser problem:) support chaining pointer dereference on function calls that return a pointer. (type checking now fails on stuff like func().field and func().next.field) - (later, nasty parser problem:) support chaining pointer dereference on function calls that return a pointer. (type checking now fails on stuff like func().field and func().next.field)

View File

@@ -2,6 +2,14 @@
main { main {
sub start() { sub start() {
^^ubyte @shared wptr
; three ways to write the exact same operation (getting the byte at the address this pointer points to)
cx16.r0L = wptr^^
cx16.r1L = wptr[0]
cx16.r2L = @(wptr)
^^thing.Node n1 = thing.Node(true, -42, 0) ^^thing.Node n1 = thing.Node(true, -42, 0)
^^thing.Node n2 = thing.Node(false, 99, 0) ^^thing.Node n2 = thing.Node(false, 99, 0)
n1.next = n2 n1.next = n2
@@ -14,6 +22,8 @@ main {
^^ubyte @shared ubptr ^^ubyte @shared ubptr
str name = "irmen" str name = "irmen"
bptr = &name
ubptr = &name
stringinfo1("hello") stringinfo1("hello")
stringinfo2(name) stringinfo2(name)
stringinfo3("apple") stringinfo3("apple")