1 ! Copyright (C) 2007, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays byte-arrays byte-vectors generic assocs hashtables
4 io.binary kernel kernel.private math namespaces make sequences
5 words quotations strings alien.accessors alien.strings layouts
6 system combinators math.bitwise math.order
7 accessors growable compiler.constants ;
8 IN: compiler.codegen.fixup
13 : add-literal ( obj -- ) literal-table get push ;
20 : <label> ( -- label ) label new ;
21 : define-label ( name -- ) <label> swap set ;
23 : compiled-offset ( -- n ) building get length ;
25 : resolve-label ( label/name -- )
26 dup label? [ get ] unless
27 compiled-offset >>offset drop ;
29 : offset-for-class ( class -- n )
30 rc-absolute-cell = cell 4 ? compiled-offset swap - ;
32 TUPLE: label-fixup { label label } { class integer } { offset integer } ;
34 : label-fixup ( label class -- )
35 dup offset-for-class \ label-fixup boa label-table get push ;
38 SYMBOL: relocation-table
40 : push-4 ( value vector -- )
41 [ length ] [ B{ 0 0 0 0 } swap push-all ] [ underlying>> ] tri
42 swap set-alien-unsigned-4 ;
44 : add-relocation-entry ( type class offset -- )
45 { 0 24 28 } bitfield relocation-table get push-4 ;
47 : rel-fixup ( class type -- )
48 swap dup offset-for-class add-relocation-entry ;
50 : add-dlsym-literals ( symbol dll -- )
51 [ string>symbol add-literal ] [ add-literal ] bi* ;
53 : rel-dlsym ( name dll class -- )
54 [ add-dlsym-literals ] dip rt-dlsym rel-fixup ;
56 : rel-word ( word class -- )
57 [ add-literal ] dip rt-xt rel-fixup ;
59 : rel-word-pic ( word class -- )
60 [ add-literal ] dip rt-xt-pic rel-fixup ;
62 : rel-word-pic-tail ( word class -- )
63 [ add-literal ] dip rt-xt-pic-tail rel-fixup ;
65 : rel-primitive ( word class -- )
66 [ def>> first add-literal ] dip rt-primitive rel-fixup ;
68 : rel-immediate ( literal class -- )
69 [ add-literal ] dip rt-immediate rel-fixup ;
71 : rel-this ( class -- )
74 : rel-here ( offset class -- )
75 [ add-literal ] dip rt-here rel-fixup ;
78 : resolve-offset ( label-fixup -- offset )
79 label>> offset>> [ "Unresolved label" throw ] unless* ;
81 : resolve-absolute-label ( label-fixup -- )
82 dup resolve-offset neg add-literal
83 [ rt-here ] dip [ class>> ] [ offset>> ] bi add-relocation-entry ;
85 : resolve-relative-label ( label-fixup -- label )
86 [ class>> ] [ offset>> ] [ resolve-offset ] tri 3array ;
88 : resolve-labels ( label-fixups -- labels' )
89 [ class>> rc-absolute? ] partition
90 [ [ resolve-absolute-label ] each ]
91 [ [ resolve-relative-label ] map concat ]
95 V{ } clone literal-table set
96 V{ } clone label-table set
97 BV{ } clone relocation-table set ;
99 : with-fixup ( quot -- code )
103 label-table [ resolve-labels ] change
104 literal-table get >array
105 relocation-table get >byte-array
107 ] B{ } make 4array ; inline