]> gitweb.factorcode.org Git - factor.git/blob - vm/instruction_operands.hpp
dc8aa9d841d24a2f47b275a29aaa5b1ef61565f6
[factor.git] / vm / instruction_operands.hpp
1 namespace factor
2 {
3
4 enum relocation_type {
5         /* arg is a literal table index, holding a pair (symbol/dll) */
6         RT_DLSYM,
7         /* a word or quotation's general entry point */
8         RT_ENTRY_POINT,
9         /* a word's PIC entry point */
10         RT_ENTRY_POINT_PIC,
11         /* a word's tail-call PIC entry point */
12         RT_ENTRY_POINT_PIC_TAIL,
13         /* current offset */
14         RT_HERE,
15         /* current code block */
16         RT_THIS,
17         /* data heap literal */
18         RT_LITERAL,
19         /* untagged fixnum literal */
20         RT_UNTAGGED,
21         /* address of megamorphic_cache_hits var */
22         RT_MEGAMORPHIC_CACHE_HITS,
23         /* address of vm object */
24         RT_VM,
25         /* value of vm->cards_offset */
26         RT_CARDS_OFFSET,
27         /* value of vm->decks_offset */
28         RT_DECKS_OFFSET,
29 };
30
31 enum relocation_class {
32         /* absolute address in a 64-bit location */
33         RC_ABSOLUTE_CELL,
34         /* absolute address in a 32-bit location */
35         RC_ABSOLUTE,
36         /* relative address in a 32-bit location */
37         RC_RELATIVE,
38         /* absolute address in a PowerPC LIS/ORI sequence */
39         RC_ABSOLUTE_PPC_2_2,
40         /* absolute address in a PowerPC LWZ instruction */
41         RC_ABSOLUTE_PPC_2,
42         /* relative address in a PowerPC LWZ/STW/BC instruction */
43         RC_RELATIVE_PPC_2,
44         /* relative address in a PowerPC B/BL instruction */
45         RC_RELATIVE_PPC_3,
46         /* relative address in an ARM B/BL instruction */
47         RC_RELATIVE_ARM_3,
48         /* pointer to address in an ARM LDR/STR instruction */
49         RC_INDIRECT_ARM,
50         /* pointer to address in an ARM LDR/STR instruction offset by 8 bytes */
51         RC_INDIRECT_ARM_PC,
52         /* absolute address in a 16-bit location */
53         RC_ABSOLUTE_2
54 };
55
56 static const cell rel_absolute_ppc_2_mask = 0xffff;
57 static const cell rel_relative_ppc_2_mask = 0xfffc;
58 static const cell rel_relative_ppc_3_mask = 0x3fffffc;
59 static const cell rel_indirect_arm_mask = 0xfff;
60 static const cell rel_relative_arm_3_mask = 0xffffff;
61
62 /* code relocation table consists of a table of entries for each fixup */
63 struct relocation_entry {
64         u32 value;
65
66         explicit relocation_entry(u32 value_) : value(value_) {}
67
68         relocation_entry(relocation_type rel_type,
69                 relocation_class rel_class,
70                 cell offset)
71         {
72                 value = (u32)((rel_type << 28) | (rel_class << 24) | offset);
73         }
74
75         relocation_type rel_type()
76         {
77                 return (relocation_type)((value & 0xf0000000) >> 28);
78         }
79
80         relocation_class rel_class()
81         {
82                 return (relocation_class)((value & 0x0f000000) >> 24);
83         }
84
85         cell rel_offset()
86         {
87                 return (value & 0x00ffffff);
88         }
89
90         int number_of_parameters()
91         {
92                 switch(rel_type())
93                 {
94                 case RT_VM:
95                         return 1;
96                 case RT_DLSYM:
97                         return 2;
98                 case RT_ENTRY_POINT:
99                 case RT_ENTRY_POINT_PIC:
100                 case RT_ENTRY_POINT_PIC_TAIL:
101                 case RT_LITERAL:
102                 case RT_HERE:
103                 case RT_UNTAGGED:
104                 case RT_THIS:
105                 case RT_MEGAMORPHIC_CACHE_HITS:
106                 case RT_CARDS_OFFSET:
107                 case RT_DECKS_OFFSET:
108                         return 0;
109                 default:
110                         critical_error("Bad rel type",rel_type());
111                         return -1; /* Can't happen */
112                 }
113         }
114 };
115
116 struct instruction_operand {
117         relocation_entry rel;
118         code_block *compiled;
119         cell index;
120         cell pointer;
121
122         instruction_operand(relocation_entry rel_, code_block *compiled_, cell index_);
123
124         relocation_type rel_type()
125         {
126                 return rel.rel_type();
127         }
128
129         cell rel_offset()
130         {
131                 return rel.rel_offset();
132         }
133
134         cell parameter_index()
135         {
136                 return index;
137         }
138
139         code_block *parent_code_block()
140         {
141                 return compiled;
142         }
143
144         fixnum load_value_2_2();
145         fixnum load_value_masked(cell mask, cell bits, cell shift);
146         fixnum load_value(cell relative_to);
147         fixnum load_value();
148         code_block *load_code_block(cell relative_to);
149         code_block *load_code_block();
150
151         void store_value_2_2(fixnum value);
152         void store_value_masked(fixnum value, cell mask, cell shift);
153         void store_value(fixnum value);
154         void store_code_block(code_block *compiled);
155 };
156
157 }