mirror of
https://github.com/irmen/prog8.git
synced 2024-09-10 09:54:32 +00:00
assigning string result from subroutine
This commit is contained in:
parent
8a6ef17fbf
commit
99f7d469f4
@ -22,6 +22,7 @@ import prog8.compiler.target.c64.codegen.assignment.TargetStorageKind
|
|||||||
import prog8.compiler.target.generatedLabelPrefix
|
import prog8.compiler.target.generatedLabelPrefix
|
||||||
import prog8.functions.BuiltinFunctions
|
import prog8.functions.BuiltinFunctions
|
||||||
import prog8.functions.FSignature
|
import prog8.functions.FSignature
|
||||||
|
import java.io.CharConversionException
|
||||||
import java.nio.file.Path
|
import java.nio.file.Path
|
||||||
import java.time.LocalDate
|
import java.time.LocalDate
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
@ -231,8 +232,12 @@ internal class AsmGen(private val program: Program,
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun encode(str: String, altEncoding: Boolean): List<Short> {
|
private fun encode(str: String, altEncoding: Boolean): List<Short> {
|
||||||
val bytes = if(altEncoding) Petscii.encodeScreencode(str, true) else Petscii.encodePetscii(str, true)
|
try {
|
||||||
return bytes.plus(0)
|
val bytes = if (altEncoding) Petscii.encodeScreencode(str, true) else Petscii.encodePetscii(str, true)
|
||||||
|
return bytes.plus(0)
|
||||||
|
} catch(x: CharConversionException) {
|
||||||
|
throw AssemblyError("There was a problem converting a string to the target machine's char encoding: ${x.message}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun zeropagevars2asm(statements: List<Statement>) {
|
private fun zeropagevars2asm(statements: List<Statement>) {
|
||||||
|
@ -132,7 +132,24 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
|
|||||||
asmgen.translateFunctionCall(value, preserveStatusRegisterAfterCall)
|
asmgen.translateFunctionCall(value, preserveStatusRegisterAfterCall)
|
||||||
val returnValue = sub.returntypes.zip(sub.asmReturnvaluesRegisters).single { it.second.registerOrPair!=null }
|
val returnValue = sub.returntypes.zip(sub.asmReturnvaluesRegisters).single { it.second.registerOrPair!=null }
|
||||||
if(returnValue.first==DataType.STR) {
|
if(returnValue.first==DataType.STR) {
|
||||||
TODO("assignment of string => copy string ${assign.position}")
|
when(assign.target.datatype) {
|
||||||
|
DataType.UWORD -> {
|
||||||
|
// assign the address of the string result value
|
||||||
|
assignRegisterpairWord(assign.target, RegisterOrPair.AY)
|
||||||
|
}
|
||||||
|
DataType.STR, DataType.ARRAY_UB, DataType.ARRAY_B -> {
|
||||||
|
// copy the actual string result into the target string variable
|
||||||
|
asmgen.out("""
|
||||||
|
pha
|
||||||
|
lda #<${assign.target.asmVarname}
|
||||||
|
sta P8ZP_SCRATCH_W1
|
||||||
|
lda #>${assign.target.asmVarname}
|
||||||
|
sta P8ZP_SCRATCH_W1+1
|
||||||
|
pla
|
||||||
|
jsr prog8_lib.strcpy""")
|
||||||
|
}
|
||||||
|
else -> throw AssemblyError("weird target dt")
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
when (returnValue.second.registerOrPair) {
|
when (returnValue.second.registerOrPair) {
|
||||||
RegisterOrPair.A -> assignRegisterByte(assign.target, CpuRegister.A)
|
RegisterOrPair.A -> assignRegisterByte(assign.target, CpuRegister.A)
|
||||||
@ -156,7 +173,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
|
|||||||
when(returntype.typeOrElse(DataType.STRUCT)) {
|
when(returntype.typeOrElse(DataType.STRUCT)) {
|
||||||
in ByteDatatypes -> assignRegisterByte(assign.target, CpuRegister.A) // function's byte result is in A
|
in ByteDatatypes -> assignRegisterByte(assign.target, CpuRegister.A) // function's byte result is in A
|
||||||
in WordDatatypes -> assignRegisterpairWord(assign.target, RegisterOrPair.AY) // function's word result is in AY
|
in WordDatatypes -> assignRegisterpairWord(assign.target, RegisterOrPair.AY) // function's word result is in AY
|
||||||
DataType.STR -> TODO("assign string => copy string")
|
DataType.STR -> TODO("assign string => copy string or assign string address")
|
||||||
DataType.FLOAT -> TODO("assign float result from ${sub.name}")
|
DataType.FLOAT -> TODO("assign float result from ${sub.name}")
|
||||||
else -> throw AssemblyError("weird result type")
|
else -> throw AssemblyError("weird result type")
|
||||||
}
|
}
|
||||||
|
@ -7,73 +7,26 @@
|
|||||||
; Note: this program is compatible with C64 and CX16.
|
; Note: this program is compatible with C64 and CX16.
|
||||||
|
|
||||||
main {
|
main {
|
||||||
sub start() {
|
|
||||||
|
|
||||||
uword ss
|
|
||||||
|
|
||||||
ss = %1000000110101010
|
|
||||||
ss <<= 8
|
|
||||||
txt.print_uwbin(ss, 1)
|
|
||||||
txt.chrout('\n')
|
|
||||||
|
|
||||||
ss = %1000000110101111
|
|
||||||
ss <<= 9
|
|
||||||
txt.print_uwbin(ss, 1)
|
|
||||||
txt.chrout('\n')
|
|
||||||
|
|
||||||
ss = %1000000110101111
|
|
||||||
ss <<= 14
|
|
||||||
txt.print_uwbin(ss, 1)
|
|
||||||
txt.chrout('\n')
|
|
||||||
|
|
||||||
ss = %1000000110101111
|
|
||||||
ss <<= 15
|
|
||||||
txt.print_uwbin(ss, 1)
|
|
||||||
txt.chrout('\n')
|
|
||||||
|
|
||||||
ss = %1000000110101111
|
|
||||||
ss <<= 16
|
|
||||||
txt.print_uwbin(ss, 1)
|
|
||||||
txt.chrout('\n')
|
|
||||||
|
|
||||||
ss = %1000000110101010
|
|
||||||
ss <<= 17
|
|
||||||
txt.print_uwbin(ss, 1)
|
|
||||||
txt.chrout('\n')
|
|
||||||
|
|
||||||
main22.testX()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
main22 {
|
|
||||||
|
|
||||||
sub start() {
|
sub start() {
|
||||||
ubyte char
|
str string1 = "abcdef"
|
||||||
uword ssss
|
str string2 = "%=&"
|
||||||
float fl
|
uword sa
|
||||||
|
|
||||||
;char = 1+(lsb(ssss) * 2)
|
txt.print(string1)
|
||||||
;fl = 2.0*(abs(fl) + 1.0)
|
|
||||||
|
|
||||||
char = abs(char)
|
|
||||||
char = lsb(ssss)
|
|
||||||
char++
|
|
||||||
char = msb(ssss)
|
|
||||||
char++
|
|
||||||
char = c64.CHRIN()
|
|
||||||
|
|
||||||
txt.print_ub(char)
|
|
||||||
txt.chrout('\n')
|
txt.chrout('\n')
|
||||||
|
string1=string2
|
||||||
char = chrin()
|
txt.print(string1)
|
||||||
|
|
||||||
txt.print_ub(char)
|
|
||||||
txt.chrout('\n')
|
txt.chrout('\n')
|
||||||
|
|
||||||
void getstr()
|
void getstr()
|
||||||
; TODO string assign ssss = getstr()
|
|
||||||
|
|
||||||
txt.print_uwhex(ssss, true)
|
sa = getstr()
|
||||||
|
txt.print_uwhex(sa, true)
|
||||||
|
txt.chrout('\n')
|
||||||
|
|
||||||
|
string1 = getstr()
|
||||||
|
txt.print(string1)
|
||||||
txt.chrout('\n')
|
txt.chrout('\n')
|
||||||
|
|
||||||
; fl = getfloat()
|
; fl = getfloat()
|
||||||
@ -82,7 +35,6 @@ main22 {
|
|||||||
; txt.chrout('\n')
|
; txt.chrout('\n')
|
||||||
|
|
||||||
testX()
|
testX()
|
||||||
;char=strlen(ssss)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sub chrin() -> ubyte {
|
sub chrin() -> ubyte {
|
||||||
@ -91,7 +43,7 @@ main22 {
|
|||||||
|
|
||||||
sub getstr() -> str {
|
sub getstr() -> str {
|
||||||
@($d020)++
|
@($d020)++
|
||||||
return "foo"
|
return "foobar"
|
||||||
}
|
}
|
||||||
|
|
||||||
; sub getfloat() -> float {
|
; sub getfloat() -> float {
|
||||||
|
Loading…
Reference in New Issue
Block a user