2004-12-01 19:48:08 -05:00
|
|
|
! :folding=indent:collapseFolds=1:
|
|
|
|
|
|
|
|
! $Id$
|
|
|
|
!
|
|
|
|
! Copyright (C) 2004 Slava Pestov.
|
|
|
|
!
|
|
|
|
! Redistribution and use in source and binary forms, with or without
|
|
|
|
! modification, are permitted provided that the following conditions are met:
|
|
|
|
!
|
|
|
|
! 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
! this list of conditions and the following disclaimer.
|
|
|
|
!
|
|
|
|
! 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
! this list of conditions and the following disclaimer in the documentation
|
|
|
|
! and/or other materials provided with the distribution.
|
|
|
|
!
|
|
|
|
! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2004-12-02 22:44:36 -05:00
|
|
|
IN: compiler
|
2004-12-01 19:48:08 -05:00
|
|
|
USE: alien
|
2004-12-02 22:44:36 -05:00
|
|
|
USE: inference
|
2004-12-01 19:48:08 -05:00
|
|
|
USE: kernel
|
|
|
|
USE: namespaces
|
|
|
|
USE: words
|
2004-12-18 20:24:46 -05:00
|
|
|
USE: lists
|
2004-12-25 02:55:03 -05:00
|
|
|
USE: math
|
2004-12-01 19:48:08 -05:00
|
|
|
|
2004-12-13 15:37:50 -05:00
|
|
|
: DS ( -- address ) "ds" dlsym-self ;
|
|
|
|
|
2004-12-25 02:55:03 -05:00
|
|
|
: absolute-ds ( -- )
|
|
|
|
#! Add an entry to the relocation table for the 32-bit
|
|
|
|
#! immediate just compiled.
|
|
|
|
"ds" f rel-dlsym-self ;
|
|
|
|
|
2004-12-01 19:48:08 -05:00
|
|
|
: POP-DS ( -- )
|
2004-12-13 15:37:50 -05:00
|
|
|
#! Pop datastack to EAX.
|
2004-12-25 02:55:03 -05:00
|
|
|
DS ECX [I]>R absolute-ds
|
2004-12-13 15:37:50 -05:00
|
|
|
ECX EAX [R]>R
|
|
|
|
4 ECX R-I
|
2004-12-25 02:55:03 -05:00
|
|
|
ECX DS R>[I] absolute-ds ;
|
2004-12-13 15:37:50 -05:00
|
|
|
|
2004-12-10 17:27:07 -05:00
|
|
|
#push-immediate [
|
2004-12-25 02:55:03 -05:00
|
|
|
DS ECX [I]>R absolute-ds
|
2004-12-13 15:37:50 -05:00
|
|
|
4 ECX R+I
|
|
|
|
address ECX I>[R]
|
2004-12-25 02:55:03 -05:00
|
|
|
ECX DS R>[I] absolute-ds
|
2004-12-10 17:27:07 -05:00
|
|
|
] "generator" set-word-property
|
|
|
|
|
|
|
|
#push-indirect [
|
2004-12-25 02:55:03 -05:00
|
|
|
DS ECX [I]>R absolute-ds
|
2004-12-13 15:37:50 -05:00
|
|
|
4 ECX R+I
|
2004-12-25 02:55:03 -05:00
|
|
|
intern-literal EAX [I]>R rel-address
|
2004-12-13 15:37:50 -05:00
|
|
|
EAX ECX R>[R]
|
2004-12-25 02:55:03 -05:00
|
|
|
ECX DS R>[I] absolute-ds
|
2004-12-10 17:27:07 -05:00
|
|
|
] "generator" set-word-property
|
2004-12-04 23:45:41 -05:00
|
|
|
|
2004-12-18 20:24:46 -05:00
|
|
|
#replace-immediate [
|
2004-12-25 02:55:03 -05:00
|
|
|
DS ECX [I]>R absolute-ds
|
2004-12-18 20:24:46 -05:00
|
|
|
address ECX I>[R]
|
2004-12-25 02:55:03 -05:00
|
|
|
ECX DS R>[I] absolute-ds
|
2004-12-18 20:24:46 -05:00
|
|
|
] "generator" set-word-property
|
|
|
|
|
|
|
|
#replace-indirect [
|
2004-12-25 02:55:03 -05:00
|
|
|
DS ECX [I]>R absolute-ds
|
|
|
|
intern-literal EAX [I]>R rel-address
|
2004-12-18 20:24:46 -05:00
|
|
|
EAX ECX R>[R]
|
2004-12-25 02:55:03 -05:00
|
|
|
ECX DS R>[I] absolute-ds
|
2004-12-18 20:24:46 -05:00
|
|
|
] "generator" set-word-property
|
|
|
|
|
2004-12-04 23:45:41 -05:00
|
|
|
#call [
|
2004-12-25 02:55:03 -05:00
|
|
|
dup dup postpone-word
|
2004-12-04 23:45:41 -05:00
|
|
|
CALL compiled-offset defer-xt
|
2004-12-25 02:55:03 -05:00
|
|
|
t rel-word
|
2004-12-04 23:45:41 -05:00
|
|
|
] "generator" set-word-property
|
|
|
|
|
2004-12-13 18:40:21 -05:00
|
|
|
#jump [
|
2004-12-25 02:55:03 -05:00
|
|
|
dup dup postpone-word
|
2004-12-16 19:57:03 -05:00
|
|
|
JUMP compiled-offset defer-xt
|
2004-12-25 02:55:03 -05:00
|
|
|
t rel-word
|
2004-12-16 19:57:03 -05:00
|
|
|
] "generator" set-word-property
|
|
|
|
|
|
|
|
#call-label [
|
|
|
|
CALL compiled-offset defer-xt
|
|
|
|
] "generator" set-word-property
|
|
|
|
|
|
|
|
#jump-label [
|
2004-12-04 23:45:41 -05:00
|
|
|
JUMP compiled-offset defer-xt
|
|
|
|
] "generator" set-word-property
|
|
|
|
|
2004-12-13 18:40:21 -05:00
|
|
|
#jump-t [
|
2004-12-04 23:45:41 -05:00
|
|
|
POP-DS
|
|
|
|
! condition is now in EAX
|
|
|
|
f address EAX CMP-I-R
|
|
|
|
! jump w/ address added later
|
|
|
|
JNE compiled-offset defer-xt
|
|
|
|
] "generator" set-word-property
|
|
|
|
|
2004-12-05 23:00:52 -05:00
|
|
|
#return-to [
|
2004-12-25 02:55:03 -05:00
|
|
|
PUSH-I/PARTIAL 0 defer-xt rel-address
|
2004-12-05 23:00:52 -05:00
|
|
|
] "generator" set-word-property
|
|
|
|
|
2004-12-04 23:45:41 -05:00
|
|
|
#return [ drop RET ] "generator" set-word-property
|
|
|
|
|
2004-12-13 16:28:28 -05:00
|
|
|
#dispatch [
|
2004-12-05 21:17:09 -05:00
|
|
|
#! Compile a piece of code that jumps to an offset in a
|
2004-12-13 16:28:28 -05:00
|
|
|
#! jump table indexed by the fixnum at the top of the stack.
|
2004-12-05 21:17:09 -05:00
|
|
|
#! The jump table must immediately follow this macro.
|
2004-12-13 16:28:28 -05:00
|
|
|
drop
|
|
|
|
POP-DS
|
2004-12-25 02:55:03 -05:00
|
|
|
1 EAX R>>I
|
|
|
|
EAX+/PARTIAL ( -- fixup ) rel-address
|
2004-12-05 21:17:09 -05:00
|
|
|
EAX JUMP-[R]
|
2004-12-25 02:55:03 -05:00
|
|
|
compile-aligned
|
2004-12-13 16:28:28 -05:00
|
|
|
compiled-offset swap set-compiled-cell ( fixup -- )
|
|
|
|
] "generator" set-word-property
|
2004-12-05 21:17:09 -05:00
|
|
|
|
2004-12-13 16:28:28 -05:00
|
|
|
#target [
|
2004-12-05 21:17:09 -05:00
|
|
|
#! Jump table entries are absolute addresses.
|
2004-12-25 02:55:03 -05:00
|
|
|
compiled-offset 0 compile-cell 0 defer-xt rel-address
|
2004-12-13 16:28:28 -05:00
|
|
|
] "generator" set-word-property
|
2004-12-13 19:16:33 -05:00
|
|
|
|
2004-12-16 19:57:03 -05:00
|
|
|
#c-call [ CALL JUMP-FIXUP ] "generator" set-word-property
|
|
|
|
|
|
|
|
#unbox [
|
|
|
|
CALL JUMP-FIXUP
|
|
|
|
EAX PUSH-R
|
|
|
|
] "generator" set-word-property
|
|
|
|
|
|
|
|
#box [
|
|
|
|
EAX PUSH-R
|
|
|
|
CALL JUMP-FIXUP
|
|
|
|
4 ESP R+I
|
|
|
|
] "generator" set-word-property
|
|
|
|
|
|
|
|
#cleanup [
|
|
|
|
dup 0 = [ drop ] [ ESP R+I ] ifte
|
|
|
|
] "generator" set-word-property
|
2004-12-18 20:24:46 -05:00
|
|
|
|
|
|
|
[
|
|
|
|
[ #drop drop ]
|
|
|
|
[ #dup dup ]
|
|
|
|
[ #swap swap ]
|
|
|
|
[ #over over ]
|
|
|
|
[ #pick pick ]
|
|
|
|
[ #>r >r ]
|
|
|
|
[ #r> r> ]
|
|
|
|
] [
|
|
|
|
uncons
|
2004-12-25 02:55:03 -05:00
|
|
|
[
|
|
|
|
car dup CALL compiled-offset defer-xt t rel-word drop
|
|
|
|
] cons
|
2004-12-18 20:24:46 -05:00
|
|
|
"generator" set-word-property
|
|
|
|
] each
|