2008-04-20 06:15:46 -04:00
|
|
|
! Copyright (C) 2006, 2008 Slava Pestov.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-10-06 01:20:00 -04:00
|
|
|
USING: accessors arrays generic kernel kernel.private math
|
|
|
|
memory namespaces make sequences layouts system hashtables
|
|
|
|
classes alien byte-arrays combinators words sets ;
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: cpu.architecture
|
|
|
|
|
2008-10-07 17:42:11 -04:00
|
|
|
! Labels
|
|
|
|
TUPLE: label offset ;
|
|
|
|
|
|
|
|
: <label> ( -- label ) label new ;
|
|
|
|
: define-label ( name -- ) <label> swap set ;
|
|
|
|
: resolve-label ( label/name -- ) dup label? [ get ] unless , ;
|
|
|
|
|
|
|
|
! Mapping from register class to machine registers
|
|
|
|
HOOK: machine-registers cpu ( -- assoc )
|
2008-04-20 06:15:46 -04:00
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
! A pseudo-register class for parameters spilled on the stack
|
2008-04-04 04:46:30 -04:00
|
|
|
SINGLETON: stack-params
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
! Return values of this class go here
|
|
|
|
GENERIC: return-reg ( register-class -- reg )
|
|
|
|
|
|
|
|
! Sequence of registers used for parameter passing in class
|
|
|
|
GENERIC: param-regs ( register-class -- regs )
|
|
|
|
|
|
|
|
GENERIC: param-reg ( n register-class -- reg )
|
|
|
|
|
|
|
|
M: object param-reg param-regs nth ;
|
|
|
|
|
|
|
|
! Sequence mapping vreg-n to native assembler registers
|
|
|
|
GENERIC: vregs ( register-class -- regs )
|
|
|
|
|
|
|
|
! Load a literal (immediate or indirect)
|
2008-10-07 17:42:11 -04:00
|
|
|
GENERIC# load-literal 1 ( obj reg -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: load-indirect cpu ( obj reg -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-10-06 01:20:00 -04:00
|
|
|
HOOK: stack-frame-size cpu ( frame-size -- n )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-10-06 01:20:00 -04:00
|
|
|
TUPLE: stack-frame total-size size params return ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
! Set up caller stack frame
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %prologue cpu ( n -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
! Tear down stack frame
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %epilogue cpu ( n -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-01-18 17:09:30 -05:00
|
|
|
! Call another word
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %call cpu ( word -- )
|
2008-01-13 17:07:59 -05:00
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
! Local jump for branches
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %jump-label cpu ( label -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
! Test if vreg is 'f' or not
|
2008-04-17 04:06:55 -04:00
|
|
|
HOOK: %jump-f cpu ( label -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-10-07 17:17:55 -04:00
|
|
|
! Test if vreg is 't' or not
|
|
|
|
HOOK: %jump-t cpu ( label -- )
|
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %dispatch cpu ( -- )
|
2008-01-13 17:07:59 -05:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %dispatch-label cpu ( word -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
! Return to caller
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %return cpu ( -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
! Change datastack height
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %inc-d cpu ( n -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
! Change callstack height
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %inc-r cpu ( n -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
! Load stack into vreg
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %peek cpu ( vreg loc -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
! Store vreg to stack
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %replace cpu ( vreg loc -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-10-07 17:17:55 -04:00
|
|
|
! Copy values between vregs
|
|
|
|
HOOK: %copy cpu ( dst src -- )
|
|
|
|
HOOK: %copy-float cpu ( dst src -- )
|
|
|
|
|
2007-09-27 21:23:24 -04:00
|
|
|
! Box and unbox floats
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %unbox-float cpu ( dst src -- )
|
|
|
|
HOOK: %box-float cpu ( dst src -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
! FFI stuff
|
|
|
|
|
|
|
|
! Is this integer small enough to appear in value template
|
|
|
|
! slots?
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: small-enough? cpu ( n -- ? )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
! Is this structure small enough to be returned in registers?
|
2008-09-13 21:28:13 -04:00
|
|
|
HOOK: struct-small-enough? cpu ( heap-size -- ? )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-10-07 17:42:11 -04:00
|
|
|
! Do we pass value structs by value or hidden reference?
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: value-structs? cpu ( -- ? )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
! If t, fp parameters are shadowed by dummy int parameters
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: fp-shadows-int? cpu ( -- ? )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %prepare-unbox cpu ( -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %unbox cpu ( n reg-class func -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %unbox-long-long cpu ( n func -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-09-13 21:28:13 -04:00
|
|
|
HOOK: %unbox-small-struct cpu ( c-type -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-09-13 21:28:13 -04:00
|
|
|
HOOK: %unbox-large-struct cpu ( n c-type -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %box cpu ( n reg-class func -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %box-long-long cpu ( n func -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-10-06 01:20:00 -04:00
|
|
|
HOOK: %prepare-box-struct cpu ( -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-09-13 21:28:13 -04:00
|
|
|
HOOK: %box-small-struct cpu ( c-type -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-09-13 21:28:13 -04:00
|
|
|
HOOK: %box-large-struct cpu ( n c-type -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
GENERIC: %save-param-reg ( stack reg reg-class -- )
|
|
|
|
|
|
|
|
GENERIC: %load-param-reg ( stack reg reg-class -- )
|
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %prepare-alien-invoke cpu ( -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %prepare-var-args cpu ( -- )
|
2007-10-30 01:46:41 -04:00
|
|
|
|
|
|
|
M: object %prepare-var-args ;
|
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %alien-invoke cpu ( function library -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %cleanup cpu ( alien-node -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %alien-callback cpu ( quot -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %callback-value cpu ( ctype -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
! Return to caller with stdcall unwinding (only for x86)
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %unwind cpu ( n -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %prepare-alien-indirect cpu ( -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %alien-indirect cpu ( -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
M: stack-params param-reg drop ;
|
|
|
|
|
2008-09-09 04:10:43 -04:00
|
|
|
M: stack-params param-regs drop f ;
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
M: object load-literal v>operand load-indirect ;
|
|
|
|
|
2008-03-26 19:23:19 -04:00
|
|
|
PREDICATE: small-slot < integer cells small-enough? ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-03-26 19:23:19 -04:00
|
|
|
PREDICATE: small-tagged < integer v>operand small-enough? ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: if-small-struct ( n size true false -- ? )
|
2008-08-22 04:12:15 -04:00
|
|
|
[ over not over struct-small-enough? and ] 2dip
|
|
|
|
[ [ nip ] prepose ] dip if ;
|
2007-09-20 18:09:08 -04:00
|
|
|
inline
|
|
|
|
|
2008-09-13 21:28:13 -04:00
|
|
|
: %unbox-struct ( n c-type -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
[
|
|
|
|
%unbox-small-struct
|
|
|
|
] [
|
|
|
|
%unbox-large-struct
|
|
|
|
] if-small-struct ;
|
|
|
|
|
2008-09-13 21:28:13 -04:00
|
|
|
: %box-struct ( n c-type -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
[
|
|
|
|
%box-small-struct
|
|
|
|
] [
|
|
|
|
%box-large-struct
|
|
|
|
] if-small-struct ;
|
|
|
|
|
|
|
|
! Alien accessors
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %unbox-byte-array cpu ( dst src -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %unbox-alien cpu ( dst src -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %unbox-f cpu ( dst src -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %unbox-any-c-ptr cpu ( dst src -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 18:07:38 -04:00
|
|
|
HOOK: %box-alien cpu ( dst src -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-10-07 17:17:55 -04:00
|
|
|
! Allocation
|
|
|
|
HOOK: %allot cpu ( dst size type tag temp -- )
|
|
|
|
|
|
|
|
HOOK: %write-barrier cpu ( src temp -- )
|
|
|
|
|
2008-04-19 05:52:34 -04:00
|
|
|
! GC check
|
2008-06-08 16:32:55 -04:00
|
|
|
HOOK: %gc cpu ( -- )
|