2005-03-14 13:20:57 -05:00
|
|
|
! Copyright (C) 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2005-05-24 01:26:45 -04:00
|
|
|
IN: compiler-backend
|
|
|
|
USING: assembler compiler inference kernel kernel-internals
|
|
|
|
lists math memory words ;
|
|
|
|
|
|
|
|
! PowerPC register assignments
|
|
|
|
! r14 data stack
|
|
|
|
! r15 call stack
|
|
|
|
! r16 callframe
|
|
|
|
! r17 executing
|
|
|
|
! r18-r30 vregs
|
|
|
|
|
|
|
|
GENERIC: v>operand
|
|
|
|
M: integer v>operand tag-bits shift ;
|
|
|
|
M: vreg v>operand vreg-n 18 + ;
|
2005-03-14 13:20:57 -05:00
|
|
|
|
2005-03-15 18:18:33 -05:00
|
|
|
! At the start of each word that calls a subroutine, we store
|
|
|
|
! the link register in r0, then push r0 on the C stack.
|
2005-05-24 01:26:45 -04:00
|
|
|
M: %prologue generate-node ( vop -- )
|
2005-03-15 18:18:33 -05:00
|
|
|
drop
|
2005-03-19 20:37:25 -05:00
|
|
|
1 1 -16 STWU
|
2005-03-15 18:18:33 -05:00
|
|
|
0 MFLR
|
2005-05-24 01:26:45 -04:00
|
|
|
0 1 20 STW ;
|
2005-03-15 18:18:33 -05:00
|
|
|
|
|
|
|
! At the end of each word that calls a subroutine, we store
|
|
|
|
! the previous link register value in r0 by popping it off the
|
|
|
|
! stack, set the link register to the contents of r0, and jump
|
|
|
|
! to the link register.
|
2005-03-17 23:29:08 -05:00
|
|
|
: compile-epilogue
|
2005-03-19 20:37:25 -05:00
|
|
|
0 1 20 LWZ
|
|
|
|
1 1 16 ADDI
|
2005-03-17 23:29:08 -05:00
|
|
|
0 MTLR ;
|
|
|
|
|
|
|
|
! Far calls are made to addresses already known when the
|
|
|
|
! IR node is being generated. No forward reference far
|
|
|
|
! calls are possible.
|
2005-03-22 21:20:58 -05:00
|
|
|
: compile-call-far ( word -- )
|
2005-04-23 19:34:06 -04:00
|
|
|
19 LOAD32
|
2005-03-17 23:29:08 -05:00
|
|
|
19 MTLR
|
|
|
|
BLRL ;
|
|
|
|
|
|
|
|
: compile-call-label ( label -- )
|
|
|
|
dup primitive? [
|
2005-05-13 18:27:18 -04:00
|
|
|
dup 1 rel-primitive word-xt compile-call-far
|
2005-03-17 23:29:08 -05:00
|
|
|
] [
|
2005-05-24 01:26:45 -04:00
|
|
|
BL
|
2005-03-17 23:29:08 -05:00
|
|
|
] ifte ;
|
|
|
|
|
2005-05-24 01:26:45 -04:00
|
|
|
: compile-call-label ( word -- )
|
|
|
|
#! Hack: length of instruction sequence that follows
|
2005-05-13 18:27:18 -04:00
|
|
|
0 1 rel-address compiled-offset 20 + 18 LOAD32
|
2005-03-21 20:53:26 -05:00
|
|
|
1 1 -16 STWU
|
|
|
|
18 1 20 STW
|
2005-05-24 01:26:45 -04:00
|
|
|
B ;
|
|
|
|
|
|
|
|
M: %call-label generate-node ( vop -- )
|
|
|
|
vop-label compile-call-label ;
|
|
|
|
|
|
|
|
M: %call generate-node ( vop -- )
|
|
|
|
vop-label dup postpone-word compile-call-label ;
|
2005-03-21 20:53:26 -05:00
|
|
|
|
2005-03-22 21:20:58 -05:00
|
|
|
: compile-jump-far ( word -- )
|
2005-04-23 19:34:06 -04:00
|
|
|
19 LOAD32
|
2005-03-19 00:30:49 -05:00
|
|
|
19 MTCTR
|
|
|
|
BCTR ;
|
|
|
|
|
2005-03-17 23:29:08 -05:00
|
|
|
: compile-jump-label ( label -- )
|
2005-03-19 00:30:49 -05:00
|
|
|
dup primitive? [
|
2005-05-13 18:27:18 -04:00
|
|
|
dup 1 rel-primitive word-xt compile-jump-far
|
2005-03-19 00:30:49 -05:00
|
|
|
] [
|
2005-05-24 01:26:45 -04:00
|
|
|
B
|
2005-03-19 00:30:49 -05:00
|
|
|
] ifte ;
|
|
|
|
|
2005-05-24 01:26:45 -04:00
|
|
|
M: %jump generate-node ( vop -- )
|
|
|
|
vop-label dup postpone-word compile-epilogue
|
|
|
|
compile-jump-label ;
|
|
|
|
|
|
|
|
M: %jump-label generate-node ( vop -- )
|
|
|
|
vop-label compile-jump-label ;
|
2005-03-17 23:29:08 -05:00
|
|
|
|
2005-05-24 01:26:45 -04:00
|
|
|
: conditional ( vop -- label )
|
|
|
|
dup vop-in-1 v>operand 0 swap f address CMPI vop-label ;
|
2005-03-19 00:30:49 -05:00
|
|
|
|
2005-05-24 01:26:45 -04:00
|
|
|
M: %jump-f generate-node ( vop -- )
|
|
|
|
conditional BEQ ;
|
2005-03-19 21:23:21 -05:00
|
|
|
|
2005-05-24 01:26:45 -04:00
|
|
|
M: %jump-t generate-node ( vop -- )
|
|
|
|
conditional BNE ;
|
|
|
|
|
|
|
|
M: %return-to generate-node ( vop -- )
|
|
|
|
vop-label 0 18 LOAD32 absolute-16/16
|
|
|
|
1 1 -16 STWU
|
|
|
|
18 1 20 STW ;
|
|
|
|
|
|
|
|
M: %return generate-node ( vop -- )
|
|
|
|
drop compile-epilogue BLR ;
|
|
|
|
|
|
|
|
M: %dispatch generate-node ( vop -- )
|
2005-03-19 21:23:21 -05:00
|
|
|
! Compile a piece of code that jumps to an offset in a
|
|
|
|
! jump table indexed by the fixnum at the top of the stack.
|
|
|
|
! The jump table must immediately follow this macro.
|
|
|
|
drop
|
2005-05-24 01:26:45 -04:00
|
|
|
! POP-DS
|
2005-03-19 21:23:21 -05:00
|
|
|
18 18 1 SRAWI
|
|
|
|
! The value 24 is a magic number. It is the length of the
|
|
|
|
! instruction sequence that follows to be generated.
|
2005-05-13 18:27:18 -04:00
|
|
|
0 1 rel-address compiled-offset 24 + 19 LOAD32
|
2005-03-19 21:23:21 -05:00
|
|
|
18 18 19 ADD
|
|
|
|
18 18 0 LWZ
|
|
|
|
18 MTLR
|
2005-05-24 01:26:45 -04:00
|
|
|
BLR ;
|
|
|
|
|
|
|
|
! \ slot [
|
|
|
|
! PEEK-DS
|
|
|
|
! 2unlist type-tag >r cell * r> - >r 18 18 r> LWZ
|
|
|
|
! REPL-DS
|
|
|
|
! ] "generator" set-word-prop
|