factor/basis/compiler/codegen/fixup/fixup.factor

92 lines
2.4 KiB
Factor
Raw Normal View History

2009-03-19 21:02:43 -04:00
! Copyright (C) 2007, 2009 Slava Pestov.
2008-09-10 23:11:03 -04:00
! See http://factorcode.org/license.txt for BSD license.
2008-12-08 15:58:00 -05:00
USING: arrays byte-arrays byte-vectors generic assocs hashtables
io.binary kernel kernel.private math namespaces make sequences
words quotations strings alien.accessors alien.strings layouts
2009-04-26 03:42:37 -04:00
system combinators math.bitwise math.order
2008-12-08 15:58:00 -05:00
accessors 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
2008-12-05 10:04:16 -05:00
: code-format ( -- n ) 22 getenv ;
2008-09-10 23:11:03 -04:00
: 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
2009-04-25 23:35:30 -04:00
[ class>> ] [ label>> ] bi compiled-offset 4 - swap
2008-09-10 23:11:03 -04:00
3array label-table get push ;
2009-03-19 21:02:43 -04:00
TUPLE: rel-fixup class type ;
2008-09-10 23:11:03 -04:00
2009-03-19 21:02:43 -04:00
: rel-fixup ( class type -- ) \ rel-fixup boa , ;
2008-09-10 23:11:03 -04:00
: push-4 ( value vector -- )
[ length ] [ B{ 0 0 0 0 } swap push-all ] [ underlying>> ] tri
swap set-alien-unsigned-4 ;
M: rel-fixup fixup*
2009-03-19 21:02:43 -04:00
[ type>> ]
[ class>> ]
[ class>> rc-absolute-cell = cell 4 ? compiled-offset swap - ] tri
{ 0 24 28 } bitfield
relocation-table get push-4 ;
2008-09-10 23:11:03 -04:00
M: integer fixup* , ;
SYMBOL: literal-table
2009-03-19 21:02:43 -04:00
: add-literal ( obj -- ) literal-table get push ;
2008-09-10 23:11:03 -04:00
: add-dlsym-literals ( symbol dll -- )
2009-03-19 21:02:43 -04:00
[ string>symbol add-literal ] [ add-literal ] bi* ;
2008-09-10 23:11:03 -04:00
: rel-dlsym ( name dll class -- )
2009-03-19 21:02:43 -04:00
[ add-dlsym-literals ] 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 -- )
2009-03-19 21:02:43 -04:00
[ def>> first add-literal ] 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 -- )
2009-03-19 21:02:43 -04:00
rt-this rel-fixup ;
2008-09-10 23:11:03 -04:00
: rel-here ( offset class -- )
2009-03-19 21:02:43 -04:00
[ add-literal ] dip 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 ;