factor/library/compiler/ppc/generator.factor

103 lines
2.4 KiB
Factor
Raw Normal View History

2005-03-14 13:20:57 -05:00
! Copyright (C) 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: compiler
2005-03-19 21:23:21 -05:00
USING: assembler inference kernel kernel-internals lists math
words ;
2005-03-14 13:20:57 -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.
#prologue [
drop
1 1 -16 STWU
0 MFLR
0 1 20 STW
] "generator" set-word-prop
! 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.
: compile-epilogue
0 1 20 LWZ
1 1 16 ADDI
0 MTLR ;
2005-03-19 21:23:21 -05:00
\ slot [
PEEK-DS
2unlist type-tag >r cell * r> - >r 18 18 r> LWZ
REPL-DS
] "generator" set-word-prop
#return-to [
0 18 LOAD32 absolute-16/16
1 1 -16 STWU
18 1 20 STW
] "generator" set-word-prop
2005-03-19 00:30:49 -05:00
#return [ drop compile-epilogue BLR ] "generator" set-word-prop
2005-03-15 22:23:52 -05:00
! 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 -- )
dup word-xt 19 LOAD32 rel-primitive-16/16
19 MTLR
BLRL ;
: compile-call-label ( label -- )
dup primitive? [
2005-03-22 21:20:58 -05:00
compile-call-far
] [
0 BL relative-24
] ifte ;
2005-03-21 20:53:26 -05:00
#call-label [
! Hack: length of instruction sequence that follows
2005-03-22 21:20:58 -05:00
compiled-offset 20 + 18 LOAD32 rel-address-16/16
2005-03-21 20:53:26 -05:00
1 1 -16 STWU
18 1 20 STW
0 B relative-24
] "generator" set-word-prop
2005-03-22 21:20:58 -05:00
: compile-jump-far ( word -- )
dup word-xt 19 LOAD32 rel-primitive-16/16
2005-03-19 00:30:49 -05:00
19 MTCTR
BCTR ;
: compile-jump-label ( label -- )
2005-03-19 00:30:49 -05:00
dup primitive? [
2005-03-22 21:20:58 -05:00
compile-jump-far
2005-03-19 00:30:49 -05:00
] [
0 B relative-24
] ifte ;
#jump [
dup postpone-word compile-epilogue compile-jump-label
] "generator" set-word-prop
: compile-jump-t ( label -- )
POP-DS
0 18 3 CMPI
0 BNE relative-14 ;
2005-03-19 00:30:49 -05:00
: compile-jump-f ( label -- )
POP-DS
0 18 3 CMPI
0 BEQ relative-14 ;
2005-03-19 21:23:21 -05:00
\ dispatch [
! 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
POP-DS
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-03-22 21:20:58 -05:00
compiled-offset 24 + 19 LOAD32 rel-address-16/16
2005-03-19 21:23:21 -05:00
18 18 19 ADD
18 18 0 LWZ
18 MTLR
BLR
] "generator" set-word-prop