factor/library/compiler/ppc/generator.factor

122 lines
3.6 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-backend
2005-05-30 21:10:08 -04:00
USING: alien assembler compiler inference kernel
kernel-internals lists math memory namespaces words ;
: compile-dlsym ( symbol dll register -- )
>r 2dup dlsym r> LOAD32 0 1 rel-dlsym ;
2005-05-30 21:10:08 -04:00
: compile-c-call ( symbol dll -- )
11 [ compile-dlsym ] keep MTLR BLRL ;
2005-05-30 21:10:08 -04:00
2005-10-23 22:31:01 -04:00
: stack-increment \ stack-reserve get 32 max stack@ 16 align ;
M: %prologue generate-node ( vop -- )
drop
1 1 stack-increment neg STWU
0 MFLR
0 1 stack-increment lr@ STW ;
: compile-epilogue
#! 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.
0 1 stack-increment lr@ LWZ
1 1 stack-increment ADDI
0 MTLR ;
M: %call-label generate-node ( vop -- )
#! Near calling convention for inlined recursive combinators
#! Note: length of instruction sequence is hard-coded.
vop-label
2005-10-11 21:46:14 -04:00
compiled-offset 20 + 18 LOAD32 0 1 rel-address
1 1 stack-increment neg STWU
18 1 stack-increment lr@ STW
B ;
: word-addr ( word -- )
#! Load a word address into r3.
dup word-xt 3 LOAD32 0 1 rel-word ;
: compile-call ( label -- )
#! Far C call for primitives, near C call for compiled defs.
dup postpone-word
2005-09-24 15:21:17 -04:00
dup primitive? [ word-addr 3 MTLR BLRL ] [ BL ] if ;
2005-03-21 20:53:26 -05:00
M: %call generate-node ( vop -- )
vop-label compile-call ;
2005-03-19 00:30:49 -05:00
: compile-jump ( label -- )
#! For tail calls. IP not saved on C stack.
dup postpone-word
2005-09-24 15:21:17 -04:00
dup primitive? [ word-addr 3 MTCTR BCTR ] [ B ] if ;
2005-03-19 00:30:49 -05:00
M: %jump generate-node ( vop -- )
2005-12-07 21:46:54 -05:00
drop label compile-epilogue compile-jump ;
M: %jump-label generate-node ( vop -- )
2005-12-07 21:46:54 -05:00
drop label B ;
M: %jump-t generate-node ( vop -- )
2005-12-07 21:46:54 -05:00
drop 0 input-operand 0 swap f address CMPI vop-label BNE ;
M: %return-to generate-node ( vop -- )
2005-12-09 00:02:41 -05:00
drop label 0 3 LOAD32 absolute-2/2
1 1 stack-increment neg STWU
3 1 stack-increment lr@ STW ;
M: %return generate-node ( vop -- )
drop compile-epilogue BLR ;
2005-06-05 02:43:05 -04:00
: untag ( dest src -- ) 0 0 31 tag-bits - RLWINM ;
M: %untag generate-node ( vop -- )
2005-12-07 21:46:54 -05:00
drop dest/src untag ;
2005-09-05 17:14:15 -04:00
: tag-fixnum ( src dest -- ) tag-bits SLWI ;
: untag-fixnum ( src dest -- ) tag-bits SRAWI ;
M: %dispatch generate-node ( vop -- )
2005-12-07 21:46:54 -05:00
drop
0 input-operand dup 1 SRAWI
2005-03-19 21:23:21 -05:00
! The value 24 is a magic number. It is the length of the
! instruction sequence that follows to be generated.
2005-12-07 21:46:54 -05:00
compiled-offset 24 + 0 scratch LOAD32 0 1 rel-address
0 input-operand dup 0 scratch ADD
0 input-operand dup 0 LWZ
0 input-operand MTLR
BLR ;
2005-05-30 03:37:22 -04:00
2005-05-30 21:10:08 -04:00
M: %type generate-node ( vop -- )
2005-12-07 21:46:54 -05:00
drop
2005-05-30 21:10:08 -04:00
<label> "f" set
<label> "end" set
! Get the tag
2005-12-07 21:46:54 -05:00
0 input-operand 1 scratch tag-mask ANDI
! Tag the tag
2005-12-07 21:46:54 -05:00
1 scratch 0 scratch tag-fixnum
2005-05-30 21:10:08 -04:00
! Compare with object tag number (3).
2005-12-07 21:46:54 -05:00
0 1 scratch object-tag CMPI
2005-05-30 21:10:08 -04:00
! Jump if the object doesn't store type info in its header
"end" get BNE
! It does store type info in its header
! Is the pointer itself equal to 3? Then its F_TYPE (9).
2005-12-07 21:46:54 -05:00
0 0 input-operand object-tag CMPI
2005-05-30 21:10:08 -04:00
"f" get BEQ
! The pointer is not equal to 3. Load the object header.
2005-12-07 21:46:54 -05:00
0 scratch 0 input-operand object-tag neg LWZ
0 scratch dup untag
2005-05-30 21:10:08 -04:00
"end" get B
"f" get save-xt
! The pointer is equal to 3. Load F_TYPE (9).
2005-12-07 21:46:54 -05:00
f type tag-bits shift 0 scratch LI
2005-05-30 21:10:08 -04:00
"end" get save-xt
2005-12-07 21:46:54 -05:00
0 output-operand 0 scratch MR ;
M: %tag generate-node ( vop -- )
2005-12-07 21:46:54 -05:00
drop dest/src swap tag-mask ANDI
0 output-operand dup tag-fixnum ;