]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/codegen/fixup/fixup.factor
Solution to Project Euler problem 65
[factor.git] / basis / compiler / codegen / fixup / fixup.factor
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
9
10 ! Literal table
11 SYMBOL: literal-table
12
13 : add-literal ( obj -- ) literal-table get push ;
14
15 ! Labels
16 SYMBOL: label-table
17
18 TUPLE: label offset ;
19
20 : <label> ( -- label ) label new ;
21 : define-label ( name -- ) <label> swap set ;
22
23 : compiled-offset ( -- n ) building get length ;
24
25 : resolve-label ( label/name -- )
26     dup label? [ get ] unless
27     compiled-offset >>offset drop ;
28
29 : offset-for-class ( class -- n )
30     rc-absolute-cell = cell 4 ? compiled-offset swap - ;
31
32 TUPLE: label-fixup { label label } { class integer } { offset integer } ;
33
34 : label-fixup ( label class -- )
35     dup offset-for-class \ label-fixup boa label-table get push ;
36
37 ! Relocation table
38 SYMBOL: relocation-table
39
40 : push-4 ( value vector -- )
41     [ length ] [ B{ 0 0 0 0 } swap push-all ] [ underlying>> ] tri
42     swap set-alien-unsigned-4 ;
43
44 : add-relocation-entry ( type class offset -- )
45     { 0 24 28 } bitfield relocation-table get push-4 ;
46
47 : rel-fixup ( class type -- )
48     swap dup offset-for-class add-relocation-entry ;
49
50 : add-dlsym-literals ( symbol dll -- )
51     [ string>symbol add-literal ] [ add-literal ] bi* ;
52
53 : rel-dlsym ( name dll class -- )
54     [ add-dlsym-literals ] dip rt-dlsym rel-fixup ;
55
56 : rel-word ( word class -- )
57     [ add-literal ] dip rt-xt rel-fixup ;
58
59 : rel-word-pic ( word class -- )
60     [ add-literal ] dip rt-xt-pic rel-fixup ;
61
62 : rel-word-pic-tail ( word class -- )
63     [ add-literal ] dip rt-xt-pic-tail rel-fixup ;
64
65 : rel-primitive ( word class -- )
66     [ def>> first add-literal ] dip rt-primitive rel-fixup ;
67
68 : rel-immediate ( literal class -- )
69     [ add-literal ] dip rt-immediate rel-fixup ;
70
71 : rel-this ( class -- )
72     rt-this rel-fixup ;
73
74 : rel-here ( offset class -- )
75     [ add-literal ] dip rt-here rel-fixup ;
76
77 ! And the rest
78 : resolve-offset ( label-fixup -- offset )
79     label>> offset>> [ "Unresolved label" throw ] unless* ;
80
81 : resolve-absolute-label ( label-fixup -- )
82     dup resolve-offset neg add-literal
83     [ rt-here ] dip [ class>> ] [ offset>> ] bi add-relocation-entry ;
84
85 : resolve-relative-label ( label-fixup -- label )
86     [ class>> ] [ offset>> ] [ resolve-offset ] tri 3array ;
87
88 : resolve-labels ( label-fixups -- labels' )
89     [ class>> rc-absolute? ] partition
90     [ [ resolve-absolute-label ] each ]
91     [ [ resolve-relative-label ] map concat ]
92     bi* ;
93
94 : init-fixup ( -- )
95     V{ } clone literal-table set
96     V{ } clone label-table set
97     BV{ } clone relocation-table set ;
98
99 : with-fixup ( quot -- code )
100     [
101         init-fixup
102         call
103         label-table [ resolve-labels ] change
104         literal-table get >array
105         relocation-table get >byte-array
106         label-table get
107     ] B{ } make 4array ; inline