tweaks for string handling as arguments

This commit is contained in:
Irmen de Jong 2020-07-27 00:12:27 +02:00
parent 9735527062
commit 1c16bbb742
5 changed files with 41 additions and 49 deletions

View File

@ -304,9 +304,9 @@ internal class AstChecker(private val program: Program,
} else { } else {
// Pass-by-reference datatypes can not occur as parameters to a subroutine directly // Pass-by-reference datatypes can not occur as parameters to a subroutine directly
// Instead, their reference (address) should be passed (as an UWORD). // Instead, their reference (address) should be passed (as an UWORD).
// The language has no typed pointers at this time. // TODO The language has no typed pointers at this time! This should be handled better.
if(subroutine.parameters.any{it.type in PassByReferenceDatatypes }) { if(subroutine.parameters.any{it.type in PassByReferenceDatatypes }) {
err("Pass-by-reference types (str, array) cannot occur as a parameter type directly. Instead, use an uword for their address, or access the variable from the outer scope directly.") err("Pass-by-reference types (str, array) cannot occur as a parameter type directly. Instead, use an uword to receive their address, or access the variable from the outer scope directly.")
} }
} }

View File

@ -1048,6 +1048,9 @@ $endLabel""")
fun assignFromWordVariable(target: AssignTarget, variable: IdentifierReference) = fun assignFromWordVariable(target: AssignTarget, variable: IdentifierReference) =
assignmentAsmGen.assignFromWordVariable(target, variable) assignmentAsmGen.assignFromWordVariable(target, variable)
fun assignFromAddressOf(target: AssignTarget, variable: IdentifierReference) =
assignmentAsmGen.assignFromAddressOf(target, variable)
fun assignFromFloatVariable(target: AssignTarget, variable: IdentifierReference) = fun assignFromFloatVariable(target: AssignTarget, variable: IdentifierReference) =
assignmentAsmGen.assignFromFloatVariable(target, variable) assignmentAsmGen.assignFromFloatVariable(target, variable)

View File

@ -115,7 +115,6 @@ internal class FunctionCallAsmGen(private val program: Program, private val asmg
in ByteDatatypes -> asmgen.assignFromByteConstant(target, value.number.toShort()) in ByteDatatypes -> asmgen.assignFromByteConstant(target, value.number.toShort())
in WordDatatypes -> asmgen.assignFromWordConstant(target, value.number.toInt()) in WordDatatypes -> asmgen.assignFromWordConstant(target, value.number.toInt())
DataType.FLOAT -> asmgen.assignFromFloatConstant(target, value.number.toDouble()) DataType.FLOAT -> asmgen.assignFromFloatConstant(target, value.number.toDouble())
in PassByReferenceDatatypes -> throw AssemblyError("can't pass string/array as argument via a variable?") // TODO huh
else -> throw AssemblyError("weird parameter datatype") else -> throw AssemblyError("weird parameter datatype")
} }
} }
@ -125,7 +124,7 @@ internal class FunctionCallAsmGen(private val program: Program, private val asmg
in ByteDatatypes -> asmgen.assignFromByteVariable(target, value) in ByteDatatypes -> asmgen.assignFromByteVariable(target, value)
in WordDatatypes -> asmgen.assignFromWordVariable(target, value) in WordDatatypes -> asmgen.assignFromWordVariable(target, value)
DataType.FLOAT -> asmgen.assignFromFloatVariable(target, value) DataType.FLOAT -> asmgen.assignFromFloatVariable(target, value)
in PassByReferenceDatatypes -> throw AssemblyError("can't pass string/array as argument via a variable?") // TODO huh in PassByReferenceDatatypes -> asmgen.assignFromAddressOf(target, value)
else -> throw AssemblyError("weird parameter datatype") else -> throw AssemblyError("weird parameter datatype")
} }
} }

View File

@ -105,9 +105,10 @@ internal class StatementOptimizer(private val program: Program,
} }
if(stringVar!=null) { if(stringVar!=null) {
val vardecl = stringVar.targetVarDecl(program.namespace)!! val vardecl = stringVar.targetVarDecl(program.namespace)!!
val string = vardecl.value!! as StringLiteralValue val string = vardecl.value as? StringLiteralValue
if(string!=null) {
val pos = functionCallStatement.position val pos = functionCallStatement.position
if(string.value.length==1) { if (string.value.length == 1) {
val firstCharEncoded = CompilationTarget.encodeString(string.value, string.altEncoding)[0] val firstCharEncoded = CompilationTarget.encodeString(string.value, string.altEncoding)[0]
val chrout = FunctionCallStatement( val chrout = FunctionCallStatement(
IdentifierReference(listOf("c64", "CHROUT"), pos), IdentifierReference(listOf("c64", "CHROUT"), pos),
@ -115,7 +116,7 @@ internal class StatementOptimizer(private val program: Program,
functionCallStatement.void, pos functionCallStatement.void, pos
) )
return listOf(IAstModification.ReplaceNode(functionCallStatement, chrout, parent)) return listOf(IAstModification.ReplaceNode(functionCallStatement, chrout, parent))
} else if(string.value.length==2) { } else if (string.value.length == 2) {
val firstTwoCharsEncoded = CompilationTarget.encodeString(string.value.take(2), string.altEncoding) val firstTwoCharsEncoded = CompilationTarget.encodeString(string.value.take(2), string.altEncoding)
val chrout1 = FunctionCallStatement( val chrout1 = FunctionCallStatement(
IdentifierReference(listOf("c64", "CHROUT"), pos), IdentifierReference(listOf("c64", "CHROUT"), pos),
@ -134,6 +135,7 @@ internal class StatementOptimizer(private val program: Program,
} }
} }
} }
}
// if the first instruction in the called subroutine is a return statement, remove the jump altogeter // if the first instruction in the called subroutine is a return statement, remove the jump altogeter
val subroutine = functionCallStatement.target.targetSubroutine(program.namespace) val subroutine = functionCallStatement.target.targetSubroutine(program.namespace)

View File

@ -10,27 +10,15 @@ main {
float[] fa = [1,2,3,4] float[] fa = [1,2,3,4]
sub start() { sub start() {
float x = 9.9 wot("asdfasdf")
wot("asdfasdf")
fa[2] = 8.8 wot("asdfasdf1")
p()
ubyte b = 2
fa[b] = 9.8
p()
fa[b] = 9.9
p()
} }
sub p() { sub wot(uword text) {
byte i ;c64scr.print(text) ; TODO better type error
for i in 0 to len(fa)-1 { ;c64.CHROUT('\n')
c64flt.print_f(fa[i]) c64scr.print_uwhex(text, 1)
c64.CHROUT(',')
c64.CHROUT(' ')
}
c64.CHROUT('\n') c64.CHROUT('\n')
} }
} }