make string-assignment actually work (using strcpy)

This commit is contained in:
Irmen de Jong 2020-10-04 18:18:58 +02:00
parent 22eac159e5
commit ec8cfe1591
5 changed files with 45 additions and 5 deletions

View File

@ -5,5 +5,5 @@
; indent format: TABS, size=8
prog8_lib {
%asminclude "library:prog8lib.asm", ""
%asminclude "library:prog8_lib.asm", ""
}

View File

@ -107,9 +107,9 @@ private fun parseImports(filepath: Path, errors: ErrorReporter): Triple<Program,
// depending on the machine and compiler options we may have to include some libraries
CompilationTarget.instance.machine.importLibs(compilerOptions, importer, programAst)
// always import prog8lib and math
// always import prog8_lib and math
importer.importLibraryModule(programAst, "math")
importer.importLibraryModule(programAst, "prog8lib")
importer.importLibraryModule(programAst, "prog8_lib")
errors.handle()
return Triple(programAst, compilerOptions, importedFiles)
}

View File

@ -48,6 +48,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
DataType.UBYTE, DataType.BYTE -> assignVariableByte(assign.target, variable)
DataType.UWORD, DataType.WORD -> assignVariableWord(assign.target, variable)
DataType.FLOAT -> assignVariableFloat(assign.target, variable)
DataType.STR -> assignVariableString(assign.target, variable)
in PassByReferenceDatatypes -> assignAddressOf(assign.target, variable)
else -> throw AssemblyError("unsupported assignment target type ${assign.target.datatype}")
}
@ -357,6 +358,45 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
}
}
private fun assignVariableString(target: AsmAssignTarget, variable: IdentifierReference) {
val sourceName = asmgen.asmVariableName(variable)
when(target.kind) {
TargetStorageKind.VARIABLE -> {
when(target.datatype) {
DataType.UWORD -> {
asmgen.out("""
lda #<$sourceName
sta ${target.asmVarname}
lda #>$sourceName
sta ${target.asmVarname}+1
""")
}
DataType.STR, DataType.ARRAY_UB, DataType.ARRAY_B -> {
asmgen.out("""
; copy a string (must be 0-terminated) from A/Y to (P8ZP_SCRATCH_W1)
lda #<${target.asmVarname}
sta P8ZP_SCRATCH_W1
lda #>${target.asmVarname}
sta P8ZP_SCRATCH_W1+1
lda #<$sourceName
ldy #>$sourceName
jsr prog8_lib.strcpy""")
}
else -> throw AssemblyError("assign string to incompatible variable type")
}
}
TargetStorageKind.STACK -> {
asmgen.out("""
lda #<$sourceName
sta P8ESTACK_LO,x
lda #>$sourceName+1
sta P8ESTACK_HI,x
dex""")
}
else -> throw AssemblyError("string-assign to weird target")
}
}
private fun assignVariableWord(target: AsmAssignTarget, variable: IdentifierReference) {
val sourceName = asmgen.asmVariableName(variable)
when(target.kind) {

View File

@ -8,8 +8,8 @@ main {
str planet_name = "12345678"
sub start() {
txt.print(planet_name)
txt.chrout('\n')
; txt.print(planet_name)
; txt.chrout('\n')
planet_name = "saturn" ; TODO make strcpy() actually work it now sets the address in the first two bytes...