fix subroutine calling convention for @Rx parameters: don't pass them via cpu registers

This commit is contained in:
Irmen de Jong 2025-01-14 23:02:17 +01:00
parent 09a17743ad
commit f9c7c7dab7
4 changed files with 53 additions and 27 deletions

View File

@ -15,12 +15,16 @@ internal class FunctionCallAsmGen(private val program: PtProgram, private val as
// just ignore any result values from the function call.
}
internal fun optimizeIntArgsViaCpuRegisters(params: List<PtSubroutineParameter>) =
when(params.size) {
1 -> params[0].type.isIntegerOrBool
2 -> params[0].type.isByteOrBool && params[1].type.isByteOrBool
internal fun optimizeIntArgsViaCpuRegisters(params: List<PtSubroutineParameter>): Boolean {
// When the parameter(s) are passed via an explicit register or register pair,
// we consider them NOT to be optimized into (possibly different) CPU registers.
// Just load them in whatever the register spec says.
return when (params.size) {
1 -> params[0].type.isIntegerOrBool && params[0].register == null
2 -> params[0].type.isByteOrBool && params[1].type.isByteOrBool && params[0].register == null && params[1].register == null
else -> false
}
}
internal fun translateFunctionCall(call: PtFunctionCall) {
// Output only the code to set up the parameters and perform the actual call

View File

@ -14,8 +14,9 @@ Future Things and Ideas
- Kotlin: can we use inline value classes in certain spots?
- allow multi-value variable initialization (var a,b,c = 1,2,3)
- 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
- Compiling Libraries: improve ability to create library files in prog8; for instance there's still stuff injected into the start of the start() routine AND there is separate setup logic going on before calling it.
Make up our mind! Maybe all setup does need to be put into start() ? because the program cannot function correctly when the variables aren't initialized properly bss is not cleared etc. etc.
- Compiling Libraries: improve ability to create library files in prog8; for instance there's still stuff injected into the start of the start() routine (see translateSubroutine function)
AND there is separate setup logic going on before calling it. Make up our mind!
Maybe all setup does need to be put into start() ? because the program cannot function correctly when the variables aren't initialized properly bss is not cleared etc. etc.
Add a -library $xxxx command line option (and/or some directive) to preselect every setting that is required to make a library at $xxxx rather than a normal loadable and runnable program?
Need to add some way to generate a stable jump table at a given address.
Need library to not call init_system AND init_system_phase2 not either.

View File

@ -1,34 +1,53 @@
%import floats
%import sprites
%import textio
%option no_sysinit
%zeropage basicsafe
main {
sub start() {
; uword @shared uw1 = 1001
; uword @shared uw2 = 1001
; ubyte @shared ub = 42
; ubyte @shared index = 2
;
; uword[] array1 = [999,1000,1001]
; uword[] @nosplit array2 = [999,1000,1001]
cx16.mouse_config2(1)
; func2(array1[index], ub) ; args via subroutine variables (R1, R2)
; func2(uw2, ub) ; args via subroutine variables (R1, R2)
; func1(1001) ; arg via AY? or R1?
; func1(array1[index]) ; arg via AY? or R1?
; func1(array2[index]) ; arg via AY? or R1?
sprites.init(1, 0, 0, sprites.SIZE_64, sprites.SIZE_64, sprites.COLORS_16, 0)
sprites.pos(1, 100, 100)
repeat {
word x,y
x,y = sprites.getxy(0)
sprites.pos(1, x, y)
}
float fz
cx16.r0L = single()
cx16.r0L, fz = multi()
main.func1.arg1 = 9999
%asm {{
lda #0
ldy #0
jsr p8s_func1
}}
cx16.r3 = 8888
%asm {{
lda #0
ldy #0
jsr p8s_func1
}}
}
sub single() -> ubyte {
return xx
}
; sub func2(uword arg1 @R1, ubyte arg2 @R2) { ; expected args via variables R1+R2
; txt.print_uw(arg1)
; txt.chrout('=')
; txt.print_uw(cx16.r1)
; txt.spc()
; txt.print_ub(arg2)
; txt.chrout('=')
; txt.print_ub(cx16.r2L)
; txt.nl()
; }
sub multi() -> ubyte, float {
return xx, 3.33
sub func1(uword arg1 @R3) { ; expected arg1 via R3, not via cpu regs AY
txt.print_uw(arg1)
txt.chrout('=')
txt.print_uw(cx16.r3)
txt.nl()
}
}

View File

@ -1030,6 +1030,8 @@ data class IRInstruction(
}
private fun determineReg2Type(): IRDataType? {
if(opcode==Opcode.LOADX || opcode==Opcode.LOADIX || opcode==Opcode.STOREX || opcode==Opcode.STOREIX)
return IRDataType.BYTE
if(opcode==Opcode.LOADI || opcode==Opcode.STOREI)
return IRDataType.WORD
if(opcode==Opcode.MSIG || opcode==Opcode.LSIG)