2008-09-10 23:11:03 -04:00
|
|
|
! Copyright (C) 2007, 2008 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
USING: arrays byte-arrays generic assocs hashtables io.binary
|
|
|
|
kernel kernel.private math namespaces make sequences words
|
|
|
|
quotations strings alien.accessors alien.strings layouts system
|
2008-09-17 01:46:38 -04:00
|
|
|
combinators math.bitwise words.private math.order accessors
|
2008-10-07 21:00:38 -04:00
|
|
|
growable cpu.architecture compiler.constants ;
|
2008-09-17 01:46:38 -04:00
|
|
|
IN: compiler.codegen.fixup
|
2008-09-10 23:11:03 -04:00
|
|
|
|
2008-09-17 01:46:38 -04:00
|
|
|
GENERIC: fixup* ( obj -- )
|
2008-09-10 23:11:03 -04:00
|
|
|
|
|
|
|
: code-format 22 getenv ;
|
|
|
|
|
|
|
|
: compiled-offset ( -- n ) building get length code-format * ;
|
|
|
|
|
|
|
|
SYMBOL: relocation-table
|
|
|
|
SYMBOL: label-table
|
|
|
|
|
2008-09-17 01:46:38 -04:00
|
|
|
M: label fixup* compiled-offset >>offset drop ;
|
2008-09-10 23:11:03 -04:00
|
|
|
|
|
|
|
TUPLE: label-fixup label class ;
|
|
|
|
|
|
|
|
: label-fixup ( label class -- ) \ label-fixup boa , ;
|
|
|
|
|
|
|
|
M: label-fixup fixup*
|
|
|
|
dup class>> rc-absolute?
|
|
|
|
[ "Absolute labels not supported" throw ] when
|
2008-09-17 01:46:38 -04:00
|
|
|
[ label>> ] [ class>> ] bi compiled-offset 4 - rot
|
2008-09-10 23:11:03 -04:00
|
|
|
3array label-table get push ;
|
|
|
|
|
|
|
|
TUPLE: rel-fixup arg class type ;
|
|
|
|
|
|
|
|
: rel-fixup ( arg class type -- ) \ rel-fixup boa , ;
|
|
|
|
|
|
|
|
: push-4 ( value vector -- )
|
|
|
|
[ length ] [ B{ 0 0 0 0 } swap push-all ] [ underlying>> ] tri
|
|
|
|
swap set-alien-unsigned-4 ;
|
|
|
|
|
|
|
|
M: rel-fixup fixup*
|
|
|
|
[ [ arg>> ] [ class>> ] [ type>> ] tri { 0 8 16 } bitfield ]
|
|
|
|
[ class>> rc-absolute-cell = cell 4 ? compiled-offset swap - ] bi
|
|
|
|
[ relocation-table get push-4 ] bi@ ;
|
|
|
|
|
|
|
|
M: integer fixup* , ;
|
|
|
|
|
2008-10-20 02:56:28 -04:00
|
|
|
: indq ( elt seq -- n ) [ eq? ] with find drop ;
|
|
|
|
|
2008-09-10 23:11:03 -04:00
|
|
|
: adjoin* ( obj table -- n )
|
2008-11-30 19:28:15 -05:00
|
|
|
2dup indq [ 2nip ] [ dup length [ push ] dip ] if* ;
|
2008-09-10 23:11:03 -04:00
|
|
|
|
|
|
|
SYMBOL: literal-table
|
|
|
|
|
|
|
|
: add-literal ( obj -- n ) literal-table get adjoin* ;
|
|
|
|
|
|
|
|
: add-dlsym-literals ( symbol dll -- )
|
2008-11-30 19:28:15 -05:00
|
|
|
[ string>symbol ] dip 2array literal-table get push-all ;
|
2008-09-10 23:11:03 -04:00
|
|
|
|
|
|
|
: rel-dlsym ( name dll class -- )
|
2008-11-30 19:28:15 -05:00
|
|
|
[ literal-table get length [ add-dlsym-literals ] dip ] dip
|
|
|
|
rt-dlsym rel-fixup ;
|
2008-09-10 23:11:03 -04:00
|
|
|
|
|
|
|
: rel-word ( word class -- )
|
2008-11-30 19:28:15 -05:00
|
|
|
[ add-literal ] dip rt-xt rel-fixup ;
|
2008-09-10 23:11:03 -04:00
|
|
|
|
|
|
|
: rel-primitive ( word class -- )
|
2008-11-30 19:28:15 -05:00
|
|
|
[ def>> first ] dip rt-primitive rel-fixup ;
|
2008-09-10 23:11:03 -04:00
|
|
|
|
2008-11-24 07:40:51 -05:00
|
|
|
: rel-immediate ( literal class -- )
|
2008-11-30 19:28:15 -05:00
|
|
|
[ add-literal ] dip rt-immediate rel-fixup ;
|
2008-11-24 07:40:51 -05:00
|
|
|
|
2008-09-10 23:11:03 -04:00
|
|
|
: rel-this ( class -- )
|
|
|
|
0 swap rt-label rel-fixup ;
|
|
|
|
|
2008-11-13 05:16:08 -05:00
|
|
|
: rel-here ( offset class -- )
|
|
|
|
rt-here rel-fixup ;
|
2008-09-10 23:11:03 -04:00
|
|
|
|
|
|
|
: init-fixup ( -- )
|
|
|
|
BV{ } clone relocation-table set
|
|
|
|
V{ } clone label-table set ;
|
|
|
|
|
|
|
|
: resolve-labels ( labels -- labels' )
|
|
|
|
[
|
|
|
|
first3 offset>>
|
|
|
|
[ "Unresolved label" throw ] unless*
|
|
|
|
3array
|
|
|
|
] map concat ;
|
|
|
|
|
2008-09-17 01:46:38 -04:00
|
|
|
: fixup ( fixup-directives -- code )
|
2008-09-10 23:11:03 -04:00
|
|
|
[
|
|
|
|
init-fixup
|
2008-09-17 01:46:38 -04:00
|
|
|
[ fixup* ] each
|
2008-09-10 23:11:03 -04:00
|
|
|
literal-table get >array
|
|
|
|
relocation-table get >byte-array
|
|
|
|
label-table get resolve-labels
|
2008-09-17 01:46:38 -04:00
|
|
|
] { } make 4array ;
|