Skip to content

Commit

Permalink
RISC-V: implement calls to (external) functions
Browse files Browse the repository at this point in the history
For now, calls are implemented by loading the address of callee into
a register (`ra`) and then using `jalr ra, ra, 0`. This is not optimal
but will serve for now.
  • Loading branch information
janvrany committed Jun 17, 2023
1 parent 61c43fd commit be354b5
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/Tinyrossa-RISCV/TRRV64GPSABILinkage.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ Class {
'framePreservedOffset'
],
#pools : [
'TRRV64GRegisters'
'TRRV64GRegisters',
'TRIntLimits'
],
#category : #'Tinyrossa-RISCV-Codegen'
}
Expand Down Expand Up @@ -43,7 +44,30 @@ TRRV64GPSABILinkage >> generateCall: node [
generate addi: paramReg , valReg, 0
].

generate jal: ra, node symbol.
"If the call a recursive call..."
node symbol == codegen compilation functionSymbol ifTrue: [
"...then generate 'jal ra, <function>'..."
generate jal: ra, node symbol.
] ifFalse: [
"...otherwise, load callee's address into register 'ra'
and generate 'jalr ra, ra, 0'.
Note, that here we load address directly into 'ra' (as opposite
to allocating new v-register) because will be clobbered anyways
by jalr storing return address there. This lowers the pressure on
RA.
Also note that rather than this, we should generate a trampoline
and call calle through it. Or relative jal if it's close enough.
But that's left as future work."

self assert: node symbol address notNil description: 'No address set for function symbol'.

codegen loadConstant64: node symbol address into: ra.
generate jalr: ra, ra, 0.
].



retVreg := codegen allocateRegister.
generate addi: retVreg , a0, 0.
Expand Down

0 comments on commit be354b5

Please sign in to comment.