diff --git a/compiler/test/ast/TestConst.kt b/compiler/test/ast/TestConst.kt index 00d75addc..82287b33b 100644 --- a/compiler/test/ast/TestConst.kt +++ b/compiler/test/ast/TestConst.kt @@ -306,26 +306,34 @@ main { (waDeclV as NumericLiteral).number shouldBe 40960.0 } - test("address of a const uword pointer array expression") { + test("address of a const integer pointer array expression") { val src = """ main { sub start() { const uword buffer = 2000 + const long bufferl = 999999 uword @shared addr = &buffer[2] - - const ubyte width = 100 - ubyte @shared i - ubyte @shared j - uword @shared addr2 = &buffer[i * width + j] + long @shared addr2 = &bufferl[2] + const uword width = 100 + uword @shared addr3 = &buffer[width] + long @shared addr4 = &bufferl[width] } }""" val result = compileText(Cx16Target(), true, src, outputDir, writeAssembly = true)!! val st = result.compilerAst.entrypoint.statements - st.size shouldBe 11 - val assignAddr = (st[2] as Assignment).value - (assignAddr as NumericLiteral).number shouldBe 2002.0 - val assignAddr2 = (st[9] as Assignment).value as BinaryExpression - assignAddr2.operator shouldBe "+" + st.size shouldBe 12 + val assignAddr = (st[3] as Assignment).value as NumericLiteral + assignAddr.number shouldBe 2002.0 + assignAddr.type shouldBe BaseDataType.UWORD + val assignAddr2 = (st[5] as Assignment).value as NumericLiteral + assignAddr2.number shouldBe 1000001.0 + assignAddr2.type shouldBe BaseDataType.LONG + val assignAddr3 = (st[8] as Assignment).value as NumericLiteral + assignAddr3.number shouldBe 2100 + assignAddr3.type shouldBe BaseDataType.UWORD + val assignAddr4 = (st[10] as Assignment).value as NumericLiteral + assignAddr4.number shouldBe 1000099.0 + assignAddr4.type shouldBe BaseDataType.LONG } test("out of range const byte and word give correct error") { diff --git a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt index fb384ae89..3084d5185 100644 --- a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt +++ b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt @@ -572,14 +572,15 @@ data class AddressOf(var identifier: IdentifierReference?, var arrayIndex: Array val index = arrayIndex?.constIndex() if (index != null) { address += when { - target.datatype.isUnsignedWord -> index + target.datatype.isInteger -> index target.datatype.isArray -> program.memsizer.memorySize(targetVar.datatype, index) - else -> throw FatalAstException("need array or uword ptr") + else -> throw FatalAstException("need array or ptr") } } else return null } - return NumericLiteral(BaseDataType.UWORD, address, position) + val addressType = if(targetVar.datatype.isLong) BaseDataType.LONG else BaseDataType.UWORD + return NumericLiteral(addressType, address, position) } } } diff --git a/examples/test.p8 b/examples/test.p8 index 734ac9623..c4f833a29 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,33 +1,11 @@ -%import textio - main { - sub start() { - ubyte[] array = ['h', 'e', 'l', 'l', 'o', 0] - str name = "hello" - - name[5] = '!' ; don't do this in real code... - name[5] = 0 - name[6] = 99 ; out of bounds - name[-1] = 99 ; ok - name[-5] = 99 ; ok - - cx16.r0L = name[5] - cx16.r1L = name[6] ; out of bounds - cx16.r1L = name[-1] ; ok - cx16.r1L = name[-5] ; ok - - array[5] = '!' - array[5] = 0 - array[6] = 99 ; out of bounds - array[-1] = 99 ; ok - array[-5] = 99 ; ok - array[-6] = 99 ; ok - - cx16.r0L = array[5] - cx16.r1L = array[6] ; out of bounds - cx16.r1L = array[-1] ; ok - cx16.r1L = array[-5] ; ok - cx16.r1L = array[-6] ; ok + const long buffer = 2000 + const long bufferl = 999999 + uword @shared addr = &buffer[2] + long @shared addr2 = &bufferl[2] + const long width = 100 + uword @shared addr3 = &buffer[width] + long @shared addr4 = &bufferl[width] } }