1 ! Copyright (C) 2011 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.accessors alien.strings
4 compiler.constants kernel make math math.bitwise namespaces
6 IN: compiler.codegen.relocation
8 SYMBOL: extra-offset ! Only used by non-optimizing compiler
10 : compiled-offset ( -- n )
11 building get length extra-offset get + ;
13 : alignment ( align -- n )
14 [ compiled-offset dup ] dip align swap - ;
16 : (align-code) ( n -- )
20 alignment (align-code) ;
22 SYMBOL: parameter-table
24 : add-parameter ( obj -- ) parameter-table get push ;
29 : add-literal ( obj -- ) literal-table get push ;
31 SYMBOL: relocation-table
33 : push-uint ( value vector -- )
34 ! If we ever revive PowerPC support again, this needs to be
35 ! changed to reverse the byte order when bootstrapping from
36 ! x86 to PowerPC or vice versa
37 [ length ] [ B{ 0 0 0 0 } swap push-all ] [ underlying>> ] tri
38 swap set-alien-unsigned-4 ;
40 : add-relocation-at ( class type offset -- )
41 { 0 28 24 } bitfield relocation-table get push-uint ;
43 : add-relocation ( class type -- )
44 compiled-offset add-relocation-at ;
46 ! Caching common symbol names reduces image size a bit
47 MEMO: cached-string>symbol ( symbol -- obj ) string>symbol ;
49 : add-dlsym-parameters ( symbol dll -- )
50 [ cached-string>symbol add-parameter ] [ add-parameter ] bi* ;
52 : rel-dlsym ( name dll class -- )
53 [ add-dlsym-parameters ] dip rt-dlsym add-relocation ;
55 : rel-dlsym-toc ( name dll class -- )
56 [ add-dlsym-parameters ] dip rt-dlsym-toc add-relocation ;
58 : rel-word ( word class -- )
59 [ add-literal ] dip rt-entry-point add-relocation ;
61 : rel-word-pic ( word class -- )
62 [ add-literal ] dip rt-entry-point-pic add-relocation ;
64 : rel-word-pic-tail ( word class -- )
65 [ add-literal ] dip rt-entry-point-pic-tail add-relocation ;
67 : rel-literal ( literal class -- )
68 [ add-literal ] dip rt-literal add-relocation ;
70 : rel-untagged ( literal class -- )
71 [ add-literal ] dip rt-untagged add-relocation ;
73 : rel-this ( class -- )
74 rt-this add-relocation ;
76 : rel-here ( offset class -- )
77 [ add-literal ] dip rt-here add-relocation ;
79 : rel-vm ( offset class -- )
80 [ add-parameter ] dip rt-vm add-relocation ;
82 : rel-cards-offset ( class -- )
83 rt-cards-offset add-relocation ;
85 : rel-decks-offset ( class -- )
86 rt-decks-offset add-relocation ;
88 : rel-megamorphic-cache-hits ( class -- )
89 rt-megamorphic-cache-hits add-relocation ;
91 : rel-inline-cache-miss ( class -- )
92 rt-inline-cache-miss add-relocation ;
94 : rel-safepoint ( class -- )
95 rt-safepoint add-relocation ;
97 : init-relocation ( -- )
98 V{ } clone parameter-table set
99 V{ } clone literal-table set
100 BV{ } clone relocation-table set