mirror of
https://github.com/irmen/prog8.git
synced 2025-02-16 07:31:48 +00:00
Merge branch 'next-version'
This commit is contained in:
commit
b6ffb81909
20
.idea/libraries/KotlinJavaRuntime.xml
generated
20
.idea/libraries/KotlinJavaRuntime.xml
generated
@ -1,23 +1,23 @@
|
||||
<component name="libraryTable">
|
||||
<library name="KotlinJavaRuntime" type="repository">
|
||||
<properties maven-id="org.jetbrains.kotlin:kotlin-stdlib-jdk8:2.0.20" />
|
||||
<properties maven-id="org.jetbrains.kotlin:kotlin-stdlib-jdk8:2.0.21" />
|
||||
<CLASSES>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/2.0.20/kotlin-stdlib-jdk8-2.0.20.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/2.0.20/kotlin-stdlib-2.0.20.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/2.0.21/kotlin-stdlib-jdk8-2.0.21.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/2.0.21/kotlin-stdlib-2.0.21.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.0.20/kotlin-stdlib-jdk7-2.0.20.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/2.0.21/kotlin-stdlib-jdk7-2.0.21.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/2.0.20/kotlin-stdlib-jdk8-2.0.20-javadoc.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/2.0.20/kotlin-stdlib-2.0.20-javadoc.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/2.0.21/kotlin-stdlib-jdk8-2.0.21-javadoc.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/2.0.21/kotlin-stdlib-2.0.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/kotlin/kotlin-stdlib-jdk7/2.0.20/kotlin-stdlib-jdk7-2.0.20-javadoc.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/2.0.21/kotlin-stdlib-jdk7-2.0.21-javadoc.jar!/" />
|
||||
</JAVADOC>
|
||||
<SOURCES>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/2.0.20/kotlin-stdlib-jdk8-2.0.20-sources.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/2.0.20/kotlin-stdlib-2.0.20-sources.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/2.0.21/kotlin-stdlib-jdk8-2.0.21-sources.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/2.0.21/kotlin-stdlib-2.0.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/kotlin/kotlin-stdlib-jdk7/2.0.20/kotlin-stdlib-jdk7-2.0.20-sources.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/2.0.21/kotlin-stdlib-jdk7-2.0.21-sources.jar!/" />
|
||||
</SOURCES>
|
||||
</library>
|
||||
</component>
|
@ -131,7 +131,6 @@ class SymbolTableMaker(private val program: PtProgram, private val options: Comp
|
||||
TODO("address-of array element $it in initial array value")
|
||||
StArrayElement(null, it.identifier.name, null)
|
||||
}
|
||||
is PtIdentifier -> StArrayElement(null, it.name, null)
|
||||
is PtNumber -> StArrayElement(it.number, null, null)
|
||||
is PtBool -> StArrayElement(null, null, it.value)
|
||||
else -> throw AssemblyError("invalid array element $it")
|
||||
|
@ -163,6 +163,7 @@ class PtArrayIndexer(elementType: DataType, position: Position): PtExpression(el
|
||||
|
||||
|
||||
class PtArray(type: DataType, position: Position): PtExpression(type, position) {
|
||||
// children are always one of 3 types: PtBool, PtNumber or PtAddressOf.
|
||||
override fun hashCode(): Int = Objects.hash(children, type)
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if(other==null || other !is PtArray)
|
||||
|
@ -24,6 +24,7 @@ fun printAst(root: PtNode, skipLibraries: Boolean, output: (text: String) -> Uni
|
||||
is PtArray -> {
|
||||
val valuelist = node.children.map {
|
||||
when (it) {
|
||||
is PtBool -> it.toString()
|
||||
is PtNumber -> it.number.toString()
|
||||
is PtIdentifier -> it.name
|
||||
else -> "?"
|
||||
|
@ -79,7 +79,6 @@ val BuiltinFunctions: Map<String, FSignature> = mapOf(
|
||||
// cmp returns a status in the carry flag, but not a proper return value
|
||||
"cmp" to FSignature(false, listOf(FParam("value1", IntegerDatatypes), FParam("value2", NumericDatatypes)), null),
|
||||
"prog8_lib_stringcompare" to FSignature(true, listOf(FParam("str1", arrayOf(DataType.STR)), FParam("str2", arrayOf(DataType.STR))), DataType.BYTE),
|
||||
"prog8_lib_arraycopy" to FSignature(false, listOf(FParam("source", ArrayDatatypes), FParam("target", ArrayDatatypes)), null),
|
||||
"prog8_lib_square_byte" to FSignature(true, listOf(FParam("value", arrayOf(DataType.BYTE, DataType.UBYTE))), DataType.UBYTE),
|
||||
"prog8_lib_square_word" to FSignature(true, listOf(FParam("value", arrayOf(DataType.WORD, DataType.UWORD))), DataType.UWORD),
|
||||
"prog8_ifelse_bittest_set" to FSignature(true, listOf(FParam("variable", ByteDatatypes), FParam("bitnumber", arrayOf(DataType.UBYTE))), DataType.BOOL),
|
||||
|
@ -145,7 +145,6 @@ private fun PtVariable.prefix(parent: PtNode, st: SymbolTable): PtVariable {
|
||||
val newValue = PtArray(arrayValue.type, arrayValue.position)
|
||||
arrayValue.children.forEach { elt ->
|
||||
when(elt) {
|
||||
is PtIdentifier -> newValue.add(elt.prefix(arrayValue, st))
|
||||
is PtBool -> newValue.add(elt)
|
||||
is PtNumber -> newValue.add(elt)
|
||||
is PtAddressOf -> {
|
||||
|
@ -1,7 +1,5 @@
|
||||
package prog8.codegen.cpu6502
|
||||
|
||||
import prog8.code.StMemVar
|
||||
import prog8.code.StStaticVariable
|
||||
import prog8.code.ast.*
|
||||
import prog8.code.core.*
|
||||
import prog8.codegen.cpu6502.assignment.*
|
||||
@ -71,101 +69,12 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
|
||||
"prog8_lib_stringcompare" -> funcStringCompare(fcall, resultRegister)
|
||||
"prog8_lib_square_byte" -> funcSquare(fcall, DataType.UBYTE, resultRegister)
|
||||
"prog8_lib_square_word" -> funcSquare(fcall, DataType.UWORD, resultRegister)
|
||||
"prog8_lib_arraycopy" -> funcArrayCopy(fcall)
|
||||
else -> throw AssemblyError("missing asmgen for builtin func ${fcall.name}")
|
||||
}
|
||||
|
||||
return BuiltinFunctions.getValue(fcall.name).returnType
|
||||
}
|
||||
|
||||
private fun funcArrayCopy(fcall: PtBuiltinFunctionCall) {
|
||||
val source = fcall.args[0] as PtIdentifier
|
||||
val target = fcall.args[1] as PtIdentifier
|
||||
|
||||
val numElements = when(val sourceSymbol = asmgen.symbolTable.lookup(source.name)) {
|
||||
is StStaticVariable -> sourceSymbol.length!!
|
||||
is StMemVar -> sourceSymbol.length!!
|
||||
else -> 0
|
||||
}
|
||||
val sourceAsm = asmgen.asmVariableName(source)
|
||||
val targetAsm = asmgen.asmVariableName(target)
|
||||
|
||||
if(source.type in SplitWordArrayTypes && target.type in SplitWordArrayTypes) {
|
||||
// split -> split words (copy lsb and msb arrays separately)
|
||||
asmgen.out("""
|
||||
lda #<${sourceAsm}_lsb
|
||||
ldy #>${sourceAsm}_lsb
|
||||
sta P8ZP_SCRATCH_W1
|
||||
sty P8ZP_SCRATCH_W1+1
|
||||
lda #<${targetAsm}_lsb
|
||||
ldy #>${targetAsm}_lsb
|
||||
sta P8ZP_SCRATCH_W2
|
||||
sty P8ZP_SCRATCH_W2+1
|
||||
ldy #${numElements and 255}
|
||||
jsr prog8_lib.memcopy_small
|
||||
lda #<${sourceAsm}_msb
|
||||
ldy #>${sourceAsm}_msb
|
||||
sta P8ZP_SCRATCH_W1
|
||||
sty P8ZP_SCRATCH_W1+1
|
||||
lda #<${targetAsm}_msb
|
||||
ldy #>${targetAsm}_msb
|
||||
sta P8ZP_SCRATCH_W2
|
||||
sty P8ZP_SCRATCH_W2+1
|
||||
ldy #${numElements and 255}
|
||||
jsr prog8_lib.memcopy_small""")
|
||||
}
|
||||
else if(source.type in SplitWordArrayTypes) {
|
||||
// split word array to normal word array (copy lsb and msb arrays separately)
|
||||
require(target.type==DataType.ARRAY_UW || target.type==DataType.ARRAY_W)
|
||||
asmgen.out("""
|
||||
lda #<${sourceAsm}_lsb
|
||||
ldy #>${sourceAsm}_lsb
|
||||
sta P8ZP_SCRATCH_W1
|
||||
sty P8ZP_SCRATCH_W1+1
|
||||
lda #<${sourceAsm}_msb
|
||||
ldy #>${sourceAsm}_msb
|
||||
sta P8ZP_SCRATCH_W2
|
||||
sty P8ZP_SCRATCH_W2+1
|
||||
lda #<${targetAsm}
|
||||
ldy #>${targetAsm}
|
||||
ldx #${numElements and 255}
|
||||
jsr prog8_lib.arraycopy_split_to_normal_words""")
|
||||
}
|
||||
else if(target.type in SplitWordArrayTypes) {
|
||||
// normal word array to split array
|
||||
require(source.type==DataType.ARRAY_UW || source.type==DataType.ARRAY_W)
|
||||
asmgen.out("""
|
||||
lda #<${targetAsm}_lsb
|
||||
ldy #>${targetAsm}_lsb
|
||||
sta P8ZP_SCRATCH_W1
|
||||
sty P8ZP_SCRATCH_W1+1
|
||||
lda #<${targetAsm}_msb
|
||||
ldy #>${targetAsm}_msb
|
||||
sta P8ZP_SCRATCH_W2
|
||||
sty P8ZP_SCRATCH_W2+1
|
||||
lda #<${sourceAsm}
|
||||
ldy #>${sourceAsm}
|
||||
ldx #${numElements and 255}
|
||||
jsr prog8_lib.arraycopy_normal_to_split_words""")
|
||||
}
|
||||
else {
|
||||
// normal array to array copy, various element types
|
||||
val eltsize = asmgen.options.compTarget.memorySize(ArrayToElementTypes.getValue(source.type))
|
||||
val numBytes = numElements * eltsize
|
||||
asmgen.out("""
|
||||
lda #<${sourceAsm}
|
||||
ldy #>${sourceAsm}
|
||||
sta P8ZP_SCRATCH_W1
|
||||
sty P8ZP_SCRATCH_W1+1
|
||||
lda #<${targetAsm}
|
||||
ldy #>${targetAsm}
|
||||
sta P8ZP_SCRATCH_W2
|
||||
sty P8ZP_SCRATCH_W2+1
|
||||
ldy #${numBytes and 255}
|
||||
jsr prog8_lib.memcopy_small""")
|
||||
}
|
||||
}
|
||||
|
||||
private fun funcSquare(fcall: PtBuiltinFunctionCall, resultType: DataType, resultRegister: RegisterOrPair?) {
|
||||
// square of word value is faster with dedicated routine, square of byte just use the regular multiplication routine.
|
||||
when (resultType) {
|
||||
@ -1357,22 +1266,6 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
|
||||
}
|
||||
}
|
||||
|
||||
private fun outputAddressAndLengthOfArray(arg: PtIdentifier) {
|
||||
// address goes in P8ZP_SCRATCH_W1, number of elements in A
|
||||
val numElements = when(val symbol = asmgen.symbolTable.lookup(arg.name)) {
|
||||
is StStaticVariable -> symbol.length!!
|
||||
is StMemVar -> symbol.length!!
|
||||
else -> 0
|
||||
}
|
||||
val identifierName = asmgen.asmVariableName(arg)
|
||||
asmgen.out("""
|
||||
lda #<$identifierName
|
||||
ldy #>$identifierName
|
||||
sta P8ZP_SCRATCH_W1
|
||||
sty P8ZP_SCRATCH_W1+1
|
||||
lda #${numElements and 255}""")
|
||||
}
|
||||
|
||||
private fun translateArguments(call: PtBuiltinFunctionCall, scope: IPtSubroutine?) {
|
||||
val signature = BuiltinFunctions.getValue(call.name)
|
||||
val callConv = signature.callConvention(call.args.map { it.type})
|
||||
|
@ -44,72 +44,10 @@ internal class BuiltinFuncGen(private val codeGen: IRCodeGen, private val exprGe
|
||||
"prog8_lib_stringcompare" -> funcStringCompare(call)
|
||||
"prog8_lib_square_byte" -> funcSquare(call, IRDataType.BYTE)
|
||||
"prog8_lib_square_word" -> funcSquare(call, IRDataType.WORD)
|
||||
"prog8_lib_arraycopy" -> funcArrayCopy(call)
|
||||
else -> throw AssemblyError("missing builtinfunc for ${call.name}")
|
||||
}
|
||||
}
|
||||
|
||||
private fun funcArrayCopy(call: PtBuiltinFunctionCall): ExpressionCodeResult {
|
||||
val source = call.args[0] as PtIdentifier
|
||||
val target = call.args[1] as PtIdentifier
|
||||
val sourceLength = codeGen.symbolTable.getLength(source.name)!!
|
||||
val targetLength = codeGen.symbolTable.getLength(target.name)!!
|
||||
require(sourceLength==targetLength)
|
||||
val result = mutableListOf<IRCodeChunkBase>()
|
||||
val fromReg = codeGen.registers.nextFree()
|
||||
val toReg = codeGen.registers.nextFree()
|
||||
val countReg = codeGen.registers.nextFree()
|
||||
if(source.type in SplitWordArrayTypes && target.type in SplitWordArrayTypes) {
|
||||
// split words -> split words, copy lsb and msb arrays separately
|
||||
result += IRCodeChunk(null, null).also {
|
||||
it += IRInstruction(Opcode.LOAD, IRDataType.WORD, reg1=fromReg, labelSymbol = source.name+"_lsb")
|
||||
it += IRInstruction(Opcode.LOAD, IRDataType.WORD, reg1=toReg, labelSymbol = target.name+"_lsb")
|
||||
it += IRInstruction(Opcode.LOAD, IRDataType.WORD, reg1=countReg, immediate = sourceLength)
|
||||
it += codeGen.makeSyscall(IMSyscall.MEMCOPY_SMALL, listOf(IRDataType.WORD to fromReg, IRDataType.WORD to toReg, IRDataType.BYTE to (countReg and 255)), returns = null)
|
||||
it += IRInstruction(Opcode.LOAD, IRDataType.WORD, reg1=fromReg, labelSymbol = source.name+"_msb")
|
||||
it += IRInstruction(Opcode.LOAD, IRDataType.WORD, reg1=toReg, labelSymbol = target.name+"_msb")
|
||||
it += IRInstruction(Opcode.LOAD, IRDataType.WORD, reg1=countReg, immediate = sourceLength)
|
||||
it += codeGen.makeSyscall(IMSyscall.MEMCOPY_SMALL, listOf(IRDataType.WORD to fromReg, IRDataType.WORD to toReg, IRDataType.BYTE to (countReg and 255)), returns = null)
|
||||
}
|
||||
}
|
||||
else if(source.type in SplitWordArrayTypes) {
|
||||
// split -> normal words
|
||||
require(target.type==DataType.ARRAY_UW || target.type==DataType.ARRAY_W)
|
||||
val fromRegMsb = codeGen.registers.nextFree()
|
||||
result += IRCodeChunk(null, null).also {
|
||||
it += IRInstruction(Opcode.LOAD, IRDataType.WORD, reg1=fromReg, labelSymbol = source.name+"_lsb")
|
||||
it += IRInstruction(Opcode.LOAD, IRDataType.WORD, reg1=fromRegMsb, labelSymbol = source.name+"_msb")
|
||||
it += IRInstruction(Opcode.LOAD, IRDataType.WORD, reg1=toReg, labelSymbol = target.name)
|
||||
it += IRInstruction(Opcode.LOAD, IRDataType.BYTE, reg1=countReg, immediate = sourceLength)
|
||||
}
|
||||
result += codeGen.makeSyscall(IMSyscall.ARRAYCOPY_SPLITW_TO_NORMAL, listOf(IRDataType.WORD to fromReg, IRDataType.WORD to fromRegMsb, IRDataType.WORD to toReg, IRDataType.BYTE to countReg), returns = null)
|
||||
}
|
||||
else if(target.type in SplitWordArrayTypes) {
|
||||
// normal -> split words
|
||||
require(source.type==DataType.ARRAY_UW || source.type==DataType.ARRAY_W)
|
||||
val toRegMsb = codeGen.registers.nextFree()
|
||||
result += IRCodeChunk(null, null).also {
|
||||
it += IRInstruction(Opcode.LOAD, IRDataType.WORD, reg1=fromReg, labelSymbol = source.name)
|
||||
it += IRInstruction(Opcode.LOAD, IRDataType.WORD, reg1=toReg, labelSymbol = target.name+"_lsb")
|
||||
it += IRInstruction(Opcode.LOAD, IRDataType.WORD, reg1=toRegMsb, labelSymbol = target.name+"_msb")
|
||||
it += IRInstruction(Opcode.LOAD, IRDataType.BYTE, reg1=countReg, immediate = sourceLength)
|
||||
}
|
||||
result += codeGen.makeSyscall(IMSyscall.ARRAYCOPY_NORMAL_TO_SPLITW, listOf(IRDataType.WORD to fromReg, IRDataType.WORD to toReg, IRDataType.WORD to toRegMsb, IRDataType.BYTE to countReg), returns = null)
|
||||
}
|
||||
else {
|
||||
// normal array to array copy (various element types)
|
||||
val eltsize = codeGen.options.compTarget.memorySize(ArrayToElementTypes.getValue(source.type))
|
||||
result += IRCodeChunk(null, null).also {
|
||||
it += IRInstruction(Opcode.LOAD, IRDataType.WORD, reg1=fromReg, labelSymbol = source.name)
|
||||
it += IRInstruction(Opcode.LOAD, IRDataType.WORD, reg1=toReg, labelSymbol = target.name)
|
||||
it += IRInstruction(Opcode.LOAD, IRDataType.WORD, reg1=countReg, immediate = sourceLength * eltsize)
|
||||
}
|
||||
result += codeGen.makeSyscall(IMSyscall.MEMCOPY_SMALL, listOf(IRDataType.WORD to fromReg, IRDataType.WORD to toReg, IRDataType.BYTE to (countReg and 255)), returns = null)
|
||||
}
|
||||
|
||||
return ExpressionCodeResult(result, IRDataType.BYTE, -1, -1)
|
||||
}
|
||||
|
||||
private fun funcSquare(call: PtBuiltinFunctionCall, resultType: IRDataType): ExpressionCodeResult {
|
||||
val result = mutableListOf<IRCodeChunkBase>()
|
||||
val valueTr = exprGen.translateExpression(call.args[0])
|
||||
|
@ -2,6 +2,7 @@ package prog8.optimizer
|
||||
|
||||
import prog8.ast.Node
|
||||
import prog8.ast.Program
|
||||
import prog8.ast.base.FatalAstException
|
||||
import prog8.ast.expressions.*
|
||||
import prog8.ast.maySwapOperandOrder
|
||||
import prog8.ast.statements.*
|
||||
@ -112,6 +113,28 @@ class ConstantFoldingOptimizer(private val program: Program, private val errors:
|
||||
}
|
||||
}
|
||||
|
||||
if(expr.left.inferType(program).isArray) {
|
||||
if (expr.operator=="*" && rightconst!=null) {
|
||||
if (expr.left is ArrayLiteral) {
|
||||
// concatenate array literal.
|
||||
val part = expr.left as ArrayLiteral
|
||||
if(part.value.isEmpty())
|
||||
errors.warn("resulting array has length zero", part.position)
|
||||
val tmp = mutableListOf<Expression>()
|
||||
repeat(rightconst.number.toInt()) {
|
||||
part.value.forEach { tmp += it.copy() }
|
||||
}
|
||||
val newArray = ArrayLiteral(part.type, tmp.toTypedArray(), part.position)
|
||||
return listOf(IAstModification.ReplaceNode(expr, newArray, parent))
|
||||
}
|
||||
else {
|
||||
val leftTarget = (expr.left as? IdentifierReference)?.targetVarDecl(program)
|
||||
if(leftTarget!=null && leftTarget.origin==VarDeclOrigin.ARRAYLITERAL)
|
||||
throw FatalAstException("shouldn't see an array literal converted to an autovar here")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(expr.operator=="==" && rightconst!=null) {
|
||||
val leftExpr = expr.left as? BinaryExpression
|
||||
// only do this shuffling when the LHS is not a constant itself (otherwise problematic nested replacements)
|
||||
|
@ -268,7 +268,6 @@ class VarConstantValueTypeAdjuster(
|
||||
|
||||
|
||||
// Replace all constant identifiers with their actual value,
|
||||
// and the array var initializer values and sizes.
|
||||
// This is needed because further constant optimizations depend on those.
|
||||
internal class ConstantIdentifierReplacer(
|
||||
private val program: Program,
|
||||
@ -421,104 +420,41 @@ internal class ConstantIdentifierReplacer(
|
||||
return null
|
||||
}
|
||||
|
||||
// convert the initializer range expression from a range or int, to an actual array.
|
||||
val rangeExpr = decl.value as? RangeExpression ?: return null
|
||||
|
||||
// convert the initializer range expression from a range, to an actual array literal.
|
||||
val declArraySize = decl.arraysize?.constIndex()
|
||||
val constRange = rangeExpr.toConstantIntegerRange()
|
||||
if(constRange?.isEmpty()==true) {
|
||||
if(constRange.first>constRange.last && constRange.step>=0)
|
||||
errors.err("descending range with positive step", decl.value?.position!!)
|
||||
else if(constRange.first<constRange.last && constRange.step<=0)
|
||||
errors.err("ascending range with negative step", decl.value?.position!!)
|
||||
}
|
||||
when(decl.datatype) {
|
||||
DataType.ARRAY_UB, DataType.ARRAY_B, DataType.ARRAY_UW, DataType.ARRAY_W, DataType.ARRAY_W_SPLIT, DataType.ARRAY_UW_SPLIT -> {
|
||||
val rangeExpr = decl.value as? RangeExpression
|
||||
if(rangeExpr!=null) {
|
||||
val constRange = rangeExpr.toConstantIntegerRange()
|
||||
if(constRange?.isEmpty()==true) {
|
||||
if(constRange.first>constRange.last && constRange.step>=0)
|
||||
errors.err("descending range with positive step", decl.value?.position!!)
|
||||
else if(constRange.first<constRange.last && constRange.step<=0)
|
||||
errors.err("ascending range with negative step", decl.value?.position!!)
|
||||
}
|
||||
val declArraySize = decl.arraysize?.constIndex()
|
||||
if(declArraySize!=null && declArraySize!=rangeExpr.size())
|
||||
errors.err("range expression size (${rangeExpr.size()}) doesn't match declared array size ($declArraySize)", decl.value?.position!!)
|
||||
if(constRange!=null) {
|
||||
val eltType = rangeExpr.inferType(program).getOr(DataType.UBYTE)
|
||||
return if(eltType in ByteDatatypes) {
|
||||
ArrayLiteral(InferredTypes.InferredType.known(decl.datatype),
|
||||
constRange.map { NumericLiteral(eltType, it.toDouble(), decl.value!!.position) }.toTypedArray(),
|
||||
position = decl.value!!.position)
|
||||
} else {
|
||||
ArrayLiteral(InferredTypes.InferredType.known(decl.datatype),
|
||||
constRange.map { NumericLiteral(eltType, it.toDouble(), decl.value!!.position) }.toTypedArray(),
|
||||
position = decl.value!!.position)
|
||||
}
|
||||
}
|
||||
}
|
||||
val numericLv = decl.value as? NumericLiteral
|
||||
if(numericLv!=null && numericLv.type== DataType.FLOAT)
|
||||
errors.err("arraysize requires only integers here", numericLv.position)
|
||||
val size = decl.arraysize?.constIndex() ?: return null
|
||||
if (rangeExpr==null && numericLv!=null) {
|
||||
// arraysize initializer is empty or a single int, and we know the size; create the arraysize.
|
||||
val fillvalue = numericLv.number.toInt()
|
||||
when(decl.datatype){
|
||||
DataType.ARRAY_UB -> {
|
||||
if(fillvalue !in 0..255)
|
||||
errors.err("ubyte value overflow", numericLv.position)
|
||||
}
|
||||
DataType.ARRAY_B -> {
|
||||
if(fillvalue !in -128..127)
|
||||
errors.err("byte value overflow", numericLv.position)
|
||||
}
|
||||
DataType.ARRAY_UW -> {
|
||||
if(fillvalue !in 0..65535)
|
||||
errors.err("uword value overflow", numericLv.position)
|
||||
}
|
||||
DataType.ARRAY_W -> {
|
||||
if(fillvalue !in -32768..32767)
|
||||
errors.err("word value overflow", numericLv.position)
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
// create the array itself, filled with the fillvalue.
|
||||
val array = Array(size) {fillvalue}.map { NumericLiteral(ArrayToElementTypes.getValue(decl.datatype), it.toDouble(), numericLv.position) }.toTypedArray<Expression>()
|
||||
return ArrayLiteral(InferredTypes.InferredType.known(decl.datatype), array, position = numericLv.position)
|
||||
}
|
||||
}
|
||||
DataType.ARRAY_F -> {
|
||||
val rangeExpr = decl.value as? RangeExpression
|
||||
if(rangeExpr!=null) {
|
||||
// convert the initializer range expression to an actual array of floats
|
||||
val declArraySize = decl.arraysize?.constIndex()
|
||||
if(declArraySize!=null && declArraySize!=rangeExpr.size())
|
||||
errors.err("range expression size (${rangeExpr.size()}) doesn't match declared array size ($declArraySize)", decl.value?.position!!)
|
||||
val constRange = rangeExpr.toConstantIntegerRange()
|
||||
if(constRange!=null) {
|
||||
return ArrayLiteral(InferredTypes.InferredType.known(DataType.ARRAY_F),
|
||||
constRange.map { NumericLiteral(DataType.FLOAT, it.toDouble(), decl.value!!.position) }.toTypedArray(),
|
||||
if(declArraySize!=null && declArraySize!=rangeExpr.size())
|
||||
errors.err("range expression size (${rangeExpr.size()}) doesn't match declared array size ($declArraySize)", decl.value?.position!!)
|
||||
if(constRange!=null) {
|
||||
val eltType = rangeExpr.inferType(program).getOr(DataType.UBYTE)
|
||||
return if(eltType in ByteDatatypes) {
|
||||
ArrayLiteral(InferredTypes.InferredType.known(decl.datatype),
|
||||
constRange.map { NumericLiteral(eltType, it.toDouble(), decl.value!!.position) }.toTypedArray(),
|
||||
position = decl.value!!.position)
|
||||
} else {
|
||||
ArrayLiteral(InferredTypes.InferredType.known(decl.datatype),
|
||||
constRange.map { NumericLiteral(eltType, it.toDouble(), decl.value!!.position) }.toTypedArray(),
|
||||
position = decl.value!!.position)
|
||||
}
|
||||
}
|
||||
|
||||
val numericLv = decl.value as? NumericLiteral
|
||||
val size = decl.arraysize?.constIndex() ?: return null
|
||||
if(rangeExpr==null && numericLv!=null) {
|
||||
// arraysize initializer is a single int, and we know the array size.
|
||||
val fillvalue = numericLv.number
|
||||
if (fillvalue < options.compTarget.machine.FLOAT_MAX_NEGATIVE || fillvalue > options.compTarget.machine.FLOAT_MAX_POSITIVE)
|
||||
errors.err("float value overflow", numericLv.position)
|
||||
else {
|
||||
val array = Array(size) {fillvalue}.map { NumericLiteral(DataType.FLOAT, it, numericLv.position) }.toTypedArray<Expression>()
|
||||
return ArrayLiteral(InferredTypes.InferredType.known(DataType.ARRAY_F), array, position = numericLv.position)
|
||||
}
|
||||
}
|
||||
}
|
||||
DataType.ARRAY_BOOL -> {
|
||||
val numericLv = decl.value as? NumericLiteral
|
||||
val size = decl.arraysize?.constIndex() ?: return null
|
||||
if(numericLv!=null) {
|
||||
// arraysize initializer is a single value, and we know the array size.
|
||||
if(numericLv.type!=DataType.BOOL) {
|
||||
errors.err("initializer value is not a boolean", numericLv.position)
|
||||
return null
|
||||
}
|
||||
val array = Array(size) {numericLv.number}.map { NumericLiteral(DataType.BOOL, it, numericLv.position) }.toTypedArray<Expression>()
|
||||
return ArrayLiteral(InferredTypes.InferredType.known(DataType.ARRAY_BOOL), array, position = numericLv.position)
|
||||
DataType.ARRAY_F -> {
|
||||
if(declArraySize!=null && declArraySize!=rangeExpr.size())
|
||||
errors.err("range expression size (${rangeExpr.size()}) doesn't match declared array size ($declArraySize)", decl.value?.position!!)
|
||||
if(constRange!=null) {
|
||||
return ArrayLiteral(InferredTypes.InferredType.known(DataType.ARRAY_F),
|
||||
constRange.map { NumericLiteral(DataType.FLOAT, it.toDouble(), decl.value!!.position) }.toTypedArray(),
|
||||
position = decl.value!!.position)
|
||||
}
|
||||
}
|
||||
else -> return null
|
||||
|
@ -716,12 +716,8 @@ internal class AstChecker(private val program: Program,
|
||||
if(decl.isArray && decl.arraysize==null) {
|
||||
if(decl.type== VarDeclType.MEMORY)
|
||||
err("memory mapped array must have a size specification")
|
||||
if(decl.value==null) {
|
||||
valueerr("array variable is missing a size specification or an initialization value")
|
||||
return
|
||||
}
|
||||
if(decl.value is NumericLiteral) {
|
||||
valueerr("unsized array declaration cannot use a single literal initialization value")
|
||||
if(decl.value==null || decl.value is NumericLiteral) {
|
||||
err("array variable is missing a size specification")
|
||||
return
|
||||
}
|
||||
if(decl.value is RangeExpression)
|
||||
@ -802,6 +798,30 @@ internal class AstChecker(private val program: Program,
|
||||
|
||||
// array length limits and constant lenghts
|
||||
if(decl.isArray) {
|
||||
|
||||
if(decl.type!=VarDeclType.MEMORY) {
|
||||
// memory-mapped arrays are initialized with their address, but any other array needs a range or array literal value.
|
||||
|
||||
if (decl.value!=null && decl.value !is ArrayLiteral && decl.value !is RangeExpression) {
|
||||
var suggestion: String? = null
|
||||
val arraysize = decl.arraysize?.constIndex()
|
||||
val numericvalue = decl.value?.constValue(program)
|
||||
if (numericvalue != null && arraysize != null) {
|
||||
when (numericvalue.type) {
|
||||
in IntegerDatatypes -> suggestion = "[${numericvalue.number.toInt()}] * $arraysize"
|
||||
DataType.FLOAT -> suggestion = "[${numericvalue.number}] * $arraysize"
|
||||
DataType.BOOL -> suggestion = "[${numericvalue.asBooleanValue}] * $arraysize"
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
|
||||
if (suggestion != null)
|
||||
valueerr("array initialization value must be a range value or an array literal (suggestion: use '$suggestion' here)")
|
||||
else
|
||||
valueerr("array initialization value must be a range value or an array literal")
|
||||
}
|
||||
}
|
||||
|
||||
val length = decl.arraysize?.constIndex()
|
||||
if(length==null)
|
||||
err("array length must be known at compile-time")
|
||||
@ -990,19 +1010,8 @@ internal class AstChecker(private val program: Program,
|
||||
checkValueTypeAndRangeArray(array.type.getOr(DataType.UNDEFINED), arrayspec, array)
|
||||
}
|
||||
|
||||
fun isPassByReferenceElement(e: Expression): Boolean {
|
||||
if(e is IdentifierReference) {
|
||||
val decl = e.targetVarDecl(program)
|
||||
return if(decl!=null)
|
||||
decl.datatype in PassByReferenceDatatypes
|
||||
else
|
||||
true // is probably a symbol that needs addr-of
|
||||
}
|
||||
return e is StringLiteral
|
||||
}
|
||||
|
||||
if(array.parent is VarDecl) {
|
||||
if (!array.value.all { it is NumericLiteral || it is AddressOf || isPassByReferenceElement(it) })
|
||||
if (!array.value.all { it is NumericLiteral || it is AddressOf })
|
||||
errors.err("array literal for variable initialization contains non-constant elements", array.position)
|
||||
} else if(array.parent is ForLoop) {
|
||||
if (!array.value.all { it.constValue(program) != null })
|
||||
@ -1016,6 +1025,7 @@ internal class AstChecker(private val program: Program,
|
||||
if(arraydt!=targetDt)
|
||||
errors.err("value has incompatible type ($arraydt) for the variable ($targetDt)", array.position)
|
||||
}
|
||||
|
||||
super.visit(array)
|
||||
}
|
||||
|
||||
@ -1803,14 +1813,9 @@ internal class AstChecker(private val program: Program,
|
||||
sourceValue: Expression) : Boolean {
|
||||
val position = sourceValue.position
|
||||
|
||||
if(sourceValue is ArrayLiteral && targetDatatype in ArrayDatatypes) {
|
||||
val vardecl=target.identifier?.targetVarDecl(program)
|
||||
val targetSize = vardecl?.arraysize?.constIndex()
|
||||
if(targetSize!=null) {
|
||||
if(sourceValue.value.size != targetSize) {
|
||||
errors.err("array size mismatch (expecting $targetSize, got ${sourceValue.value.size})", sourceValue.position)
|
||||
}
|
||||
}
|
||||
if(sourceValue is ArrayLiteral || targetDatatype in ArrayDatatypes) {
|
||||
errors.err("cannot assign arrays directly. Maybe use sys.memcopy(src, tgt, sizeof(tgt)) instead.", target.position)
|
||||
return false
|
||||
}
|
||||
|
||||
if(sourceValue is RangeExpression) {
|
||||
@ -1831,14 +1836,7 @@ internal class AstChecker(private val program: Program,
|
||||
DataType.UWORD -> sourceDatatype == DataType.UBYTE || sourceDatatype == DataType.UWORD
|
||||
DataType.FLOAT -> sourceDatatype in NumericDatatypes
|
||||
DataType.STR -> sourceDatatype == DataType.STR
|
||||
else -> {
|
||||
if(targetDatatype in ArrayDatatypes && sourceValue is ArrayLiteral)
|
||||
true // assigning array literal to an array variable is allowed, size and type are checked elsewhere
|
||||
else {
|
||||
errors.err("cannot assign this value to variable of type $targetDatatype", position)
|
||||
false
|
||||
}
|
||||
}
|
||||
else -> false
|
||||
}
|
||||
|
||||
if(result)
|
||||
|
@ -23,22 +23,6 @@ internal class CodeDesugarer(val program: Program, private val errors: IErrorRep
|
||||
// - pointer[word] replaced by @(pointer+word)
|
||||
// - @(&var) and @(&var+1) replaced by lsb(var) and msb(var) if var is a word
|
||||
// - flatten chained assignments
|
||||
// - replace array assignments by a call to the builtin function that does this: prog8_lib_arraycopy
|
||||
|
||||
override fun after(assignment: Assignment, parent: Node): Iterable<IAstModification> {
|
||||
val targetArray = assignment.target.identifier?.targetVarDecl(program)
|
||||
val sourceArray = (assignment.value as? IdentifierReference)?.targetVarDecl(program)
|
||||
if(targetArray?.isArray==true && sourceArray?.isArray==true) {
|
||||
val copy = FunctionCallStatement(
|
||||
IdentifierReference(listOf("prog8_lib_arraycopy"), assignment.position),
|
||||
mutableListOf(
|
||||
IdentifierReference(sourceArray.scopedName, assignment.position),
|
||||
IdentifierReference(targetArray.scopedName, assignment.position)
|
||||
), false, assignment.position)
|
||||
return listOf(IAstModification.ReplaceNode(assignment, copy, parent))
|
||||
}
|
||||
return noModifications
|
||||
}
|
||||
|
||||
override fun before(breakStmt: Break, parent: Node): Iterable<IAstModification> {
|
||||
fun jumpAfter(stmt: Statement): Iterable<IAstModification> {
|
||||
|
@ -566,8 +566,11 @@ class IntermediateAstMaker(private val program: Program, private val errors: IEr
|
||||
|
||||
private fun transform(srcArr: ArrayLiteral): PtArray {
|
||||
val arr = PtArray(srcArr.inferType(program).getOrElse { throw FatalAstException("array must know its type") }, srcArr.position)
|
||||
for (elt in srcArr.value)
|
||||
arr.add(transformExpression(elt))
|
||||
for (elt in srcArr.value) {
|
||||
val child = transformExpression(elt)
|
||||
require(child is PtAddressOf || child is PtBool || child is PtNumber) { "array element invalid type $child" }
|
||||
arr.add(child)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
|
@ -62,17 +62,19 @@ internal class LiteralsToAutoVars(private val program: Program, private val erro
|
||||
return noModifications
|
||||
}
|
||||
if(arrayDt.isKnown) {
|
||||
val parentAssign = parent as? Assignment
|
||||
val targetDt = parentAssign?.target?.inferType(program) ?: arrayDt
|
||||
// turn the array literal it into an identifier reference
|
||||
val litval2 = array.cast(targetDt.getOr(DataType.UNDEFINED))
|
||||
if(litval2!=null) {
|
||||
val vardecl2 = VarDecl.createAuto(litval2, targetDt.getOr(DataType.UNDEFINED) in SplitWordArrayTypes)
|
||||
val identifier = IdentifierReference(listOf(vardecl2.name), vardecl2.position)
|
||||
return listOf(
|
||||
IAstModification.ReplaceNode(array, identifier, parent),
|
||||
IAstModification.InsertFirst(vardecl2, array.definingScope)
|
||||
)
|
||||
if((array.parent as? BinaryExpression)?.operator!="*") {
|
||||
val parentAssign = parent as? Assignment
|
||||
val targetDt = parentAssign?.target?.inferType(program) ?: arrayDt
|
||||
// turn the array literal it into an identifier reference
|
||||
val litval2 = array.cast(targetDt.getOr(DataType.UNDEFINED))
|
||||
if (litval2 != null) {
|
||||
val vardecl2 = VarDecl.createAuto(litval2, targetDt.getOr(DataType.UNDEFINED) in SplitWordArrayTypes)
|
||||
val identifier = IdentifierReference(listOf(vardecl2.name), vardecl2.position)
|
||||
return listOf(
|
||||
IAstModification.ReplaceNode(array, identifier, parent),
|
||||
IAstModification.InsertFirst(vardecl2, array.definingScope)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package prog8.compiler.astprocessing
|
||||
|
||||
import prog8.ast.IFunctionCall
|
||||
import prog8.ast.INameScope
|
||||
import prog8.ast.Node
|
||||
import prog8.ast.Program
|
||||
import prog8.ast.base.FatalAstException
|
||||
@ -363,6 +364,25 @@ class TypecastsAdder(val program: Program, val options: CompilationOptions, val
|
||||
return adjustRangeDts(range, fromConst, fromDt, toConst, toDt, varDt.getOr(DataType.UNDEFINED), parent)
|
||||
}
|
||||
|
||||
override fun after(array: ArrayLiteral, parent: Node): Iterable<IAstModification> {
|
||||
// Arrays can contain booleans, numbers, or address-ofs.
|
||||
// if there is an identifier here (that is of a pass-by-reference type), take its address explicitly.
|
||||
|
||||
for((index, elt) in array.value.withIndex()) {
|
||||
if (elt is IdentifierReference) {
|
||||
val eltType = elt.inferType(program)
|
||||
val tgt = elt.targetStatement(program)
|
||||
if(eltType.isPassByReference || tgt is Subroutine || tgt is Label || tgt is Block) {
|
||||
val addressof = AddressOf(elt, null, elt.position)
|
||||
addressof.linkParents(array)
|
||||
array.value[index] = addressof
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return noModifications
|
||||
}
|
||||
|
||||
private fun adjustRangeDts(
|
||||
range: RangeExpression,
|
||||
fromConst: NumericLiteral?,
|
||||
|
@ -104,7 +104,7 @@ class TestAstChecks: FunSpec({
|
||||
val text = """
|
||||
main {
|
||||
sub start() {
|
||||
const ubyte[5] a = 5
|
||||
const ubyte[5] a = [1,2,3,4,5]
|
||||
a[2]=42
|
||||
}
|
||||
}
|
||||
@ -133,22 +133,6 @@ class TestAstChecks: FunSpec({
|
||||
errors.errors[0] shouldContain "indexing requires"
|
||||
}
|
||||
|
||||
test("array decl with expression as size can be initialized with a single value") {
|
||||
val text = """
|
||||
main {
|
||||
sub start() {
|
||||
const ubyte n = 40
|
||||
const ubyte half = n / 2
|
||||
ubyte[half] @shared a = 5
|
||||
}
|
||||
}
|
||||
"""
|
||||
val errors = ErrorReporterForTests(keepMessagesAfterReporting = true)
|
||||
compileText(C64Target(), true, text, writeAssembly = true, errors=errors) shouldNotBe null
|
||||
errors.errors.size shouldBe 0
|
||||
errors.warnings.size shouldBe 0
|
||||
}
|
||||
|
||||
test("unicode in identifier names is working") {
|
||||
val text = """
|
||||
%import floats
|
||||
|
@ -4,7 +4,8 @@ import io.kotest.core.spec.style.FunSpec
|
||||
import io.kotest.matchers.shouldBe
|
||||
import io.kotest.matchers.shouldNotBe
|
||||
import io.kotest.matchers.string.shouldContain
|
||||
import prog8.code.ast.PtBuiltinFunctionCall
|
||||
import io.kotest.matchers.types.instanceOf
|
||||
import prog8.code.ast.*
|
||||
import prog8.code.target.C64Target
|
||||
import prog8.code.target.VMTarget
|
||||
import prog8tests.helpers.ErrorReporterForTests
|
||||
@ -152,25 +153,6 @@ main {
|
||||
compileText(VMTarget(), false, text, writeAssembly = true) shouldNotBe null
|
||||
}
|
||||
|
||||
test("split array assignments") {
|
||||
val text = """
|
||||
main {
|
||||
sub start() {
|
||||
str name1 = "name1"
|
||||
str name2 = "name2"
|
||||
uword[] @split names = [name1, name2, "name3"]
|
||||
uword[] @split names2 = [name1, name2, "name3"]
|
||||
uword[] addresses = [0,0,0]
|
||||
names = [1111,2222,3333]
|
||||
addresses = names
|
||||
names = addresses
|
||||
names2 = names
|
||||
}
|
||||
}"""
|
||||
compileText(C64Target(), false, text, writeAssembly = true) shouldNotBe null
|
||||
compileText(VMTarget(), false, text, writeAssembly = true) shouldNotBe null
|
||||
}
|
||||
|
||||
test("array target with expression for index") {
|
||||
val text = """
|
||||
main {
|
||||
@ -329,79 +311,94 @@ main {
|
||||
errors.errors[2] shouldContain "out of bounds"
|
||||
}
|
||||
|
||||
test("array assignments should check for number of elements and element type correctness") {
|
||||
test("array and string initializer with multiplication") {
|
||||
val src="""
|
||||
%option enable_floats
|
||||
|
||||
main {
|
||||
sub start() {
|
||||
ubyte[] array = 1 to 4
|
||||
ubyte[] array2 = [1,2,3,4]
|
||||
str[] names = ["apple", "banana", "tomato"]
|
||||
str name = "xyz" * 3
|
||||
bool[3] boolarray = [true] * 3
|
||||
ubyte[3] bytearray = [42] * 3
|
||||
uword[3] wordarray = [5555] * 3
|
||||
float[3] floatarray = [123.45] * 3
|
||||
}
|
||||
}"""
|
||||
val result = compileText(C64Target(), false, src, writeAssembly = true)!!
|
||||
val x = result.codegenAst!!.entrypoint()!!
|
||||
x.children.size shouldBe 6
|
||||
((x.children[0] as PtVariable).value as PtString).value shouldBe "xyzxyzxyz"
|
||||
val array1 = (x.children[1] as PtVariable).value as PtArray
|
||||
val array2 = (x.children[2] as PtVariable).value as PtArray
|
||||
val array3 = (x.children[3] as PtVariable).value as PtArray
|
||||
val array4 = (x.children[4] as PtVariable).value as PtArray
|
||||
array1.children.map { (it as PtBool).value } shouldBe listOf(true, true, true)
|
||||
array2.children.map { (it as PtNumber).number } shouldBe listOf(42, 42, 42)
|
||||
array3.children.map { (it as PtNumber).number } shouldBe listOf(5555, 5555, 5555)
|
||||
array4.children.map { (it as PtNumber).number } shouldBe listOf(123.45, 123.45, 123.45)
|
||||
}
|
||||
|
||||
array = [10,11,12,13] ; ok!
|
||||
array = 20 to 23 ; ok!
|
||||
names = ["x1", "x2", "x3"] ; ok!
|
||||
test("array initializer with range") {
|
||||
val src="""
|
||||
%option enable_floats
|
||||
|
||||
ubyte[] array3 = [1,2,3,4000] ; error: element type
|
||||
array = 10 to 15 ; error: array size
|
||||
array = 1000 to 1003 ; error: element type
|
||||
names = ["x1", "x2", "x3", "x4"] ; error: array size
|
||||
names = [1.1, 2.2, 3.3, 4.4] ; error: array size AND element type
|
||||
names = [1.1, 2.2, 999999.9] ; error: element type
|
||||
names = [1.1, 2.2, 9.9] ; error: element type
|
||||
main {
|
||||
sub start() {
|
||||
ubyte[3] bytearray2 = 10 to 12
|
||||
uword[3] wordarray2 = 5000 to 5002
|
||||
float[3] floatarray2 = 100 to 102
|
||||
}
|
||||
}"""
|
||||
val result = compileText(C64Target(), false, src, writeAssembly = true)!!
|
||||
val x = result.codegenAst!!.entrypoint()!!
|
||||
x.children.size shouldBe 4
|
||||
val array1 = (x.children[0] as PtVariable).value as PtArray
|
||||
val array2 = (x.children[1] as PtVariable).value as PtArray
|
||||
val array3 = (x.children[2] as PtVariable).value as PtArray
|
||||
array1.children.map { (it as PtNumber).number } shouldBe listOf(10, 11, 12)
|
||||
array2.children.map { (it as PtNumber).number } shouldBe listOf(5000, 5001, 5002)
|
||||
array3.children.map { (it as PtNumber).number } shouldBe listOf(100, 101, 102)
|
||||
}
|
||||
|
||||
test("identifiers in array literals getting implicit address-of") {
|
||||
val src="""
|
||||
main {
|
||||
sub start() {
|
||||
label:
|
||||
str @shared name = "name"
|
||||
uword[] @shared array1 = [name, label, start, main]
|
||||
uword[] @shared array2 = [&name, &label, &start, &main]
|
||||
}
|
||||
}"""
|
||||
val result = compileText(C64Target(), false, src, writeAssembly = true)!!
|
||||
val x = result.codegenAst!!.entrypoint()!!
|
||||
x.children.size shouldBe 5
|
||||
val array1 = (x.children[1] as PtVariable).value as PtArray
|
||||
val array2 = (x.children[2] as PtVariable).value as PtArray
|
||||
array1.children.forEach {
|
||||
it shouldBe instanceOf<PtAddressOf>()
|
||||
}
|
||||
array2.children.forEach {
|
||||
it shouldBe instanceOf<PtAddressOf>()
|
||||
}
|
||||
}
|
||||
|
||||
test("variable identifiers in array literals not getting implicit address-of") {
|
||||
val src="""
|
||||
main {
|
||||
sub start() {
|
||||
label:
|
||||
str @shared name = "name"
|
||||
ubyte @shared bytevar
|
||||
uword[] @shared array1 = [cx16.r0] ; error, is variables
|
||||
uword[] @shared array2 = [bytevar] ; error, is variables
|
||||
}
|
||||
}"""
|
||||
val errors = ErrorReporterForTests()
|
||||
compileText(C64Target(), false, src, writeAssembly = true, errors = errors) shouldBe null
|
||||
errors.errors.size shouldBe 8
|
||||
errors.errors[0] shouldContain "incompatible type"
|
||||
errors.errors[1] shouldContain "array size mismatch"
|
||||
errors.errors[2] shouldContain "array element out of range"
|
||||
errors.errors[3] shouldContain "array size mismatch"
|
||||
errors.errors[4] shouldContain "array size mismatch"
|
||||
errors.errors[5] shouldContain "value has incompatible type"
|
||||
errors.errors[6] shouldContain "value has incompatible type"
|
||||
errors.errors[7] shouldContain "value has incompatible type"
|
||||
}
|
||||
|
||||
test("array assignments should work via array copy call") {
|
||||
val src="""
|
||||
%option enable_floats
|
||||
|
||||
main {
|
||||
sub start() {
|
||||
ubyte[] array = [1,2,3]
|
||||
ubyte[3] array2
|
||||
float[] flarray = [1.1, 2.2, 3.3]
|
||||
float[3] flarray2
|
||||
word[] warray = [-2222,42,3333]
|
||||
word[3] warray2
|
||||
str[] names = ["apple", "banana", "tomato"]
|
||||
str[3] names2
|
||||
|
||||
; 8 array assignments -> 8 arraycopies:
|
||||
array = [8,7,6]
|
||||
array = array2
|
||||
flarray = [99.9, 88.8, 77.7]
|
||||
flarray = flarray2
|
||||
warray = [4444,5555,6666]
|
||||
warray = warray2
|
||||
names = ["x1", "x2", "x3"]
|
||||
names = names2
|
||||
}
|
||||
}"""
|
||||
compileText(VMTarget(), false, src, writeAssembly = true) shouldNotBe null
|
||||
val result = compileText(C64Target(), false, src, writeAssembly = true)!!
|
||||
val x = result.codegenAst!!.entrypoint()!!
|
||||
(x.children[12] as PtBuiltinFunctionCall).name shouldBe "prog8_lib_arraycopy"
|
||||
(x.children[13] as PtBuiltinFunctionCall).name shouldBe "prog8_lib_arraycopy"
|
||||
(x.children[14] as PtBuiltinFunctionCall).name shouldBe "prog8_lib_arraycopy"
|
||||
(x.children[15] as PtBuiltinFunctionCall).name shouldBe "prog8_lib_arraycopy"
|
||||
(x.children[16] as PtBuiltinFunctionCall).name shouldBe "prog8_lib_arraycopy"
|
||||
(x.children[17] as PtBuiltinFunctionCall).name shouldBe "prog8_lib_arraycopy"
|
||||
(x.children[18] as PtBuiltinFunctionCall).name shouldBe "prog8_lib_arraycopy"
|
||||
(x.children[19] as PtBuiltinFunctionCall).name shouldBe "prog8_lib_arraycopy"
|
||||
compileText(C64Target(), false, src, writeAssembly = true, errors=errors) shouldBe null
|
||||
errors.errors.size shouldBe 2
|
||||
errors.errors[0] shouldContain "contains non-constant"
|
||||
errors.errors[1] shouldContain "contains non-constant"
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -5,7 +5,6 @@ import io.kotest.matchers.shouldBe
|
||||
import io.kotest.matchers.shouldNotBe
|
||||
import io.kotest.matchers.string.shouldContain
|
||||
import prog8.code.target.C64Target
|
||||
import prog8.code.target.VMTarget
|
||||
import prog8tests.helpers.ErrorReporterForTests
|
||||
import prog8tests.helpers.compileText
|
||||
|
||||
@ -42,23 +41,6 @@ class TestVariables: FunSpec({
|
||||
compileText(C64Target(), true, text, writeAssembly = true) shouldNotBe null
|
||||
}
|
||||
|
||||
test("array initialization with array var assignment") {
|
||||
val text = """
|
||||
main {
|
||||
sub start() {
|
||||
ubyte[3] @shared arrayvar=main.values1
|
||||
arrayvar = main.values2
|
||||
}
|
||||
|
||||
ubyte[] values1 = [1,2,3]
|
||||
ubyte[] values2 = [1,2,3]
|
||||
}
|
||||
"""
|
||||
compileText(VMTarget(), false, text, writeAssembly = true) shouldNotBe null
|
||||
compileText(C64Target(), false, text, writeAssembly = true) shouldNotBe null
|
||||
}
|
||||
|
||||
|
||||
test("pipe character in string literal") {
|
||||
val text = """
|
||||
main {
|
||||
@ -113,32 +95,4 @@ class TestVariables: FunSpec({
|
||||
errors.errors[0] shouldContain "value has incompatible type"
|
||||
errors.errors[1] shouldContain "value has incompatible type"
|
||||
}
|
||||
|
||||
test("initialization of boolean array with single value") {
|
||||
val text = """
|
||||
main {
|
||||
sub start() {
|
||||
bool[10] sieve0 = false
|
||||
bool[10] sieve1 = true
|
||||
sieve0[0] = true
|
||||
sieve1[0] = true
|
||||
}
|
||||
}
|
||||
"""
|
||||
compileText(C64Target(), false, text, writeAssembly = true) shouldNotBe null
|
||||
}
|
||||
|
||||
test("initialization of boolean array with single value of wrong type fails") {
|
||||
val text = """
|
||||
main {
|
||||
sub start() {
|
||||
bool[10] sieve2 = 42
|
||||
}
|
||||
}
|
||||
"""
|
||||
val errors = ErrorReporterForTests()
|
||||
compileText(C64Target(), false, text, writeAssembly = true, errors=errors) shouldBe null
|
||||
errors.errors.size shouldBe 1
|
||||
errors.errors[0] shouldContain "initializer value is not a boolean"
|
||||
}
|
||||
})
|
||||
|
@ -290,7 +290,7 @@ always have to be constants. Here are some examples of arrays::
|
||||
|
||||
byte[10] array ; array of 10 bytes, initially set to 0
|
||||
byte[] array = [1, 2, 3, 4] ; initialize the array, size taken from value
|
||||
ubyte[99] array = 255 ; initialize array with 99 times 255 [255, 255, 255, 255, ...]
|
||||
ubyte[99] array = [255]*99 ; initialize array with 99 times 255 [255, 255, 255, 255, ...]
|
||||
byte[] array = 100 to 199 ; initialize array with [100, 101, ..., 198, 199]
|
||||
str[] names = ["ally", "pete"] ; array of string pointers/addresses (equivalent to array of uwords)
|
||||
uword[] others = [names, array] ; array of pointers/addresses to other arrays
|
||||
@ -299,23 +299,25 @@ always have to be constants. Here are some examples of arrays::
|
||||
value = array[3] ; the fourth value in the array (index is 0-based)
|
||||
char = string[4] ; the fifth character (=byte) in the string
|
||||
char = string[-2] ; the second-to-last character in the string (Python-style indexing from the end)
|
||||
flags = [false, true] ; reset all flags in the array
|
||||
|
||||
.. note::
|
||||
Right now, the array should be small enough to be indexable by a single byte index.
|
||||
This means byte arrays should be <= 256 elements, word arrays <= 128 elements (256 if
|
||||
it's a split array - see below), and float arrays <= 51 elements.
|
||||
|
||||
You can write out an array initializer list over several lines if you want to improve readability.
|
||||
Arrays can be initialized with a range expression or an array literal value.
|
||||
You can write out such an initializer value over several lines if you want to improve readability.
|
||||
|
||||
You can assign a new value to an element in the array, but you can't assign a whole
|
||||
new array to another array at once. This is usually a costly operation. If you really
|
||||
need this you have to write it out depending on the use case: you can copy the memory using
|
||||
``sys.memcopy(sourcearray, targetarray, sizeof(targetarray))``. Or perhaps use ``sys.memset`` instead to
|
||||
set it all to the same value, or maybe even simply assign the individual elements.
|
||||
|
||||
Note that the various keywords for the data type and variable type (``byte``, ``word``, ``const``, etc.)
|
||||
can't be used as *identifiers* elsewhere. You can't make a variable, block or subroutine with the name ``byte``
|
||||
for instance.
|
||||
|
||||
It is possible to assign an array (variable or array literal) to another array; this will overwrite all elements in the target
|
||||
array with those in the source array. The number of elements in the arrays and the data types have to match.
|
||||
For large arrays this is a slow operation because all values are copied over.
|
||||
|
||||
Using the ``in`` operator you can easily check if a value is present in an array,
|
||||
example: ``if choice in [1,2,3,4] {....}``
|
||||
|
||||
@ -377,8 +379,8 @@ You can concatenate two string literals using '+', which can be useful to
|
||||
split long strings over separate lines. But remember that the length
|
||||
of the total string still cannot exceed 255 characters.
|
||||
A string literal can also be repeated a given number of times using '*', where the repeat number must be a constant value.
|
||||
And a new string value can be assigned to another string, but no bounds check is done
|
||||
so be sure the destination string is large enough to contain the new value (it is overwritten in memory)::
|
||||
And a new string value can be assigned to another string, but no bounds check is done!
|
||||
So be sure the destination string is large enough to contain the new value (it is overwritten in memory)::
|
||||
|
||||
str string1 = "first part" + "second part"
|
||||
str string2 = "hello!" * 10
|
||||
|
@ -363,7 +363,7 @@ Various examples::
|
||||
bool flag = true
|
||||
byte[] values = [11, 22, 33, 44, 55]
|
||||
byte[5] values ; array of 5 bytes, initially set to zero
|
||||
byte[5] values = 255 ; initialize with five 255 bytes
|
||||
byte[5] values = [255]*5 ; initialize with five 255 bytes
|
||||
|
||||
word @zp zpword = 9999 ; prioritize this when selecting vars for zeropage storage
|
||||
uword @requirezp zpaddr = $3000 ; we require this variable in zeropage
|
||||
@ -403,8 +403,11 @@ type identifier type storage size example var declara
|
||||
implicitly terminated by a 0-byte
|
||||
=============== ======================= ================= =========================================
|
||||
|
||||
**arrays:** you can split an array initializer list over several lines if you want. When an initialization
|
||||
value is given, the array size in the declaration can be omitted.
|
||||
**arrays:**
|
||||
Arrays can be initialized with a range expression or an array literal value.
|
||||
You can write out such an initializer value over several lines if you want to improve readability.
|
||||
When an initialization value is given, you are allowed to omit the array size in the declaration,
|
||||
because it can be inferred from the initialization value.
|
||||
|
||||
**numbers:** unless prefixed for hex or binary as described below, all numbers are decimal numbers. There is no octal notation.
|
||||
|
||||
@ -508,8 +511,6 @@ the downto variant to avoid having to specify the step as well::
|
||||
xx = 10
|
||||
aa to xx ; range of 5, 6, 7, 8, 9, 10
|
||||
|
||||
byte[] array = 10 to 13 ; sets the array to [10, 11, 12, 13]
|
||||
|
||||
for i in 0 to 127 {
|
||||
; i loops 0, 1, 2, ... 127
|
||||
}
|
||||
@ -542,9 +543,10 @@ memory at the given index (and allows index values of word size). See :ref:`poin
|
||||
String
|
||||
^^^^^^
|
||||
A string literal can occur with or without an encoding prefix (encoding followed by ':' followed by the string itself).
|
||||
String length is limited to 255 characters.
|
||||
You can use '+' and '*' to concatenate or repeat string fragments to build up a larger string literal.
|
||||
When this is omitted, the string is stored in the machine's default character encoding (which is PETSCII on the CBM machines).
|
||||
You can choose to store the string in other encodings such as ``sc`` (screencodes) or ``iso`` (iso-8859-15).
|
||||
String length is limited to 255 characters.
|
||||
Here are examples of the various encodings:
|
||||
|
||||
- ``"hello"`` a string translated into the default character encoding (PETSCII on the CBM machines)
|
||||
|
@ -1,6 +1,8 @@
|
||||
TODO
|
||||
====
|
||||
|
||||
- fixup syscall list UNUSED_SYSCALL_1 and 2 (numbers shift!)
|
||||
|
||||
Improve register load order in subroutine call args assignments:
|
||||
in certain situations, the "wrong" order of evaluation of function call arguments is done which results
|
||||
in overwriting registers that already got their value, which requires a lot of stack juggling (especially on plain 6502 cpu!)
|
||||
@ -9,11 +11,7 @@ Maybe this routine can be made more intelligent. See usesOtherRegistersWhileEva
|
||||
|
||||
Future Things and Ideas
|
||||
^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
- remove support for array variable initialization with a single value, just require explicitly creating the value array [42] * 10 (which is what the compiler now does for you implicitly)
|
||||
- should the array-to-array assignment support be removed and instead require an explicit copy function call? What prog8_lib_arraycopy() now does. Or just use memcopy.
|
||||
- should we add a cleararray builtin function that can efficiently set every element in the array to the given value
|
||||
|
||||
- keep boolean array intact in IR so that it might be represented as a bitmask in the resulting code (8 times storage improvement)
|
||||
- improve detection that a variable is not read before being written so that initializing it to zero can be omitted (only happens now if a vardecl is immediately followed by a for loop for instance)
|
||||
- Improve the SublimeText syntax file for prog8, you can also install this for 'bat': https://github.com/sharkdp/bat?tab=readme-ov-file#adding-new-syntaxes--language-definitions
|
||||
- Can we support signed % (remainder) somehow?
|
||||
|
@ -12,9 +12,9 @@ main {
|
||||
float[] zcoor = [ -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 ]
|
||||
|
||||
; storage for rotated coordinates
|
||||
float[len(xcoor)] rotatedx=0.0
|
||||
float[len(ycoor)] rotatedy=0.0
|
||||
float[len(zcoor)] rotatedz=-1.0
|
||||
float[len(xcoor)] rotatedx
|
||||
float[len(ycoor)] rotatedy
|
||||
float[len(zcoor)] rotatedz
|
||||
|
||||
sub start() {
|
||||
float time=0.0
|
||||
|
@ -25,7 +25,7 @@ main {
|
||||
for y in 32 to 199+32 {
|
||||
cx16.FB_cursor_position((320-len(cells))/2,y)
|
||||
cx16.FB_set_pixels(cells, len(cells))
|
||||
cells_previous = cells
|
||||
sys.memcopy(cells, cells_previous, sizeof(cells))
|
||||
ubyte @zp x
|
||||
for x in 0 to len(cells)-1 {
|
||||
cells[x] = generate(x) ; next generation
|
||||
|
@ -1,42 +1,30 @@
|
||||
%import monogfx
|
||||
%import floats
|
||||
%import textio
|
||||
%import math
|
||||
|
||||
%option no_sysinit
|
||||
%zeropage basicsafe
|
||||
|
||||
|
||||
main {
|
||||
|
||||
sub start() {
|
||||
monogfx.lores()
|
||||
demofill()
|
||||
}
|
||||
uword[4] words1 = [1,2,3,4]
|
||||
uword[4] words2 = [99,88,77,66]
|
||||
|
||||
sub demofill() {
|
||||
const uword offsetx = 0
|
||||
const uword offsety = 0
|
||||
|
||||
monogfx.circle(offsetx+160, offsety+120, 110, true)
|
||||
monogfx.rect(offsetx+180, offsety+5, 25, 190, true)
|
||||
monogfx.line(offsetx+100, offsety+150, offsetx+240, offsety+10, true)
|
||||
monogfx.line(offsetx+101, offsety+150, offsetx+241, offsety+10, true)
|
||||
monogfx.rect(offsetx+150, offsety+130, 10, 100, true)
|
||||
|
||||
sys.wait(30)
|
||||
|
||||
cbm.SETTIM(0,0,0)
|
||||
monogfx.fill(offsetx+100,offsety+100,true)
|
||||
monogfx.fill(offsetx+100,offsety+100,false)
|
||||
uword duration = cbm.RDTIM16()
|
||||
sys.wait(30)
|
||||
|
||||
monogfx.textmode()
|
||||
for cx16.r0 in words1 {
|
||||
txt.print_uw(cx16.r0)
|
||||
txt.spc()
|
||||
}
|
||||
txt.nl()
|
||||
sys.memcopy(words2, words1, sizeof(words1))
|
||||
for cx16.r0 in words1 {
|
||||
txt.print_uw(cx16.r0)
|
||||
txt.spc()
|
||||
}
|
||||
txt.nl()
|
||||
sys.memcopy([2222,3333,4444,5555], words1, sizeof(words1))
|
||||
for cx16.r0 in words1 {
|
||||
txt.print_uw(cx16.r0)
|
||||
txt.spc()
|
||||
}
|
||||
txt.nl()
|
||||
txt.print_uw(duration)
|
||||
txt.print(" jiffies\n")
|
||||
|
||||
; before optimizations: ~166 jiffies
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -4,5 +4,5 @@ org.gradle.parallel=true
|
||||
org.gradle.daemon=true
|
||||
kotlin.code.style=official
|
||||
javaVersion=11
|
||||
kotlinVersion=2.0.20
|
||||
version=10.4.2
|
||||
kotlinVersion=2.0.21
|
||||
version=10.5-SNAPSHOT
|
||||
|
@ -18,7 +18,5 @@ enum class IMSyscall(val number: Int) {
|
||||
CALLFAR(0x1017),
|
||||
CALLFAR2(0x1018),
|
||||
MEMCOPY(0x1019),
|
||||
MEMCOPY_SMALL(0x101a),
|
||||
ARRAYCOPY_SPLITW_TO_NORMAL(0x101b),
|
||||
ARRAYCOPY_NORMAL_TO_SPLITW(0x101c),
|
||||
MEMCOPY_SMALL(0x101a)
|
||||
}
|
||||
|
@ -50,8 +50,8 @@ SYSCALLS:
|
||||
37 = memset
|
||||
38 = memsetw
|
||||
39 = stringcopy
|
||||
40 = ARRAYCOPY_SPLITW_TO_NORMAL
|
||||
41 = ARRAYCOPY_NORMAL_TO_SPLITW
|
||||
40 = ...unused...
|
||||
41 = ...unused...
|
||||
42 = memcopy_small
|
||||
43 = load
|
||||
44 = load_raw
|
||||
@ -103,8 +103,8 @@ enum class Syscall {
|
||||
MEMSET,
|
||||
MEMSETW,
|
||||
STRINGCOPY,
|
||||
ARRAYCOPY_SPLITW_TO_NORMAL,
|
||||
ARRAYCOPY_NORMAL_TO_SPLITW,
|
||||
UNUSED_SYSCALL_1, // TODO fixup
|
||||
UNUSED_SYSCALL_2, // TODO fixup
|
||||
MEMCOPY_SMALL,
|
||||
LOAD,
|
||||
LOAD_RAW,
|
||||
@ -444,29 +444,6 @@ object SysCalls {
|
||||
vm.memory.setString(target, string, true)
|
||||
returnValue(callspec.returns.single(), string.length, vm)
|
||||
}
|
||||
Syscall.ARRAYCOPY_SPLITW_TO_NORMAL -> {
|
||||
val (fromLsbA, fromMsbA, targetA, bytecountA) = getArgValues(callspec.arguments, vm)
|
||||
val fromLsb = (fromLsbA as UShort).toInt()
|
||||
val fromMsb = (fromMsbA as UShort).toInt()
|
||||
val target = (targetA as UShort).toInt()
|
||||
val bytecount = (bytecountA as UByte).toInt()
|
||||
for(offset in 0..<bytecount) {
|
||||
vm.memory.setUB(target+offset*2, vm.memory.getUB(fromLsb+offset))
|
||||
vm.memory.setUB(target+offset*2+1, vm.memory.getUB(fromMsb+offset))
|
||||
}
|
||||
}
|
||||
Syscall.ARRAYCOPY_NORMAL_TO_SPLITW -> {
|
||||
val (fromA, targetLsbA, targetMsbA, bytecountA) = getArgValues(callspec.arguments, vm)
|
||||
val from = (fromA as UShort).toInt()
|
||||
val targetLsb = (targetLsbA as UShort).toInt()
|
||||
val targetMsb = (targetMsbA as UShort).toInt()
|
||||
val bytecount = (bytecountA as UByte).toInt()
|
||||
for(offset in 0..<bytecount) {
|
||||
vm.memory.setUB(targetLsb+offset, vm.memory.getUB(from+offset*2))
|
||||
vm.memory.setUB(targetMsb+offset, vm.memory.getUB(from+offset*2+1))
|
||||
}
|
||||
}
|
||||
|
||||
Syscall.LOAD -> {
|
||||
val (filenameA, addrA) = getArgValues(callspec.arguments, vm)
|
||||
val filename = vm.memory.getString((filenameA as UShort).toInt())
|
||||
@ -572,6 +549,9 @@ object SysCalls {
|
||||
}
|
||||
return returnValue(callspec.returns.single(), 30*256 + 80, vm) // just return some defaults in this case 80*30
|
||||
}
|
||||
|
||||
Syscall.UNUSED_SYSCALL_1 -> TODO("remove this")
|
||||
Syscall.UNUSED_SYSCALL_2 -> TODO("remove this")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -117,8 +117,6 @@ class VmProgramLoader {
|
||||
IMSyscall.CALLFAR2.number -> throw IRParseException("vm doesn't support the callfar2() syscall")
|
||||
IMSyscall.MEMCOPY.number -> Syscall.MEMCOPY
|
||||
IMSyscall.MEMCOPY_SMALL.number -> Syscall.MEMCOPY_SMALL
|
||||
IMSyscall.ARRAYCOPY_SPLITW_TO_NORMAL.number -> Syscall.ARRAYCOPY_SPLITW_TO_NORMAL
|
||||
IMSyscall.ARRAYCOPY_NORMAL_TO_SPLITW.number -> Syscall.ARRAYCOPY_NORMAL_TO_SPLITW
|
||||
else -> null
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user