Skip to content

Commit

Permalink
fix subroutine calling convention for @rx parameters: don't pass them…
Browse files Browse the repository at this point in the history
… via cpu registers
  • Loading branch information
irmen committed Jan 14, 2025
1 parent 09a1774 commit f9c7c7d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 30 deletions.
12 changes: 8 additions & 4 deletions codeGenCpu6502/src/prog8/codegen/cpu6502/FunctionCallAsmGen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions docs/source/todo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
67 changes: 43 additions & 24 deletions examples/test.p8
Original file line number Diff line number Diff line change
@@ -1,34 +1,53 @@
%import floats
%import sprites
%import textio
%option no_sysinit
%zeropage basicsafe


main {
sub start() {

cx16.mouse_config2(1)

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()
}

sub single() -> ubyte {
return xx
; 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]

; 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?

main.func1.arg1 = 9999
%asm {{
lda #0
ldy #0
jsr p8s_func1
}}
cx16.r3 = 8888
%asm {{
lda #0
ldy #0
jsr p8s_func1
}}
}

sub multi() -> ubyte, float {
return xx, 3.33
; 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 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()
}
}
2 changes: 2 additions & 0 deletions intermediate/src/prog8/intermediate/IRInstructions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f9c7c7d

Please sign in to comment.