]> gitweb.factorcode.org Git - factor.git/blob - vm/instruction_operands.hpp
Merge branch 'require-when' of git://github.com/littledan/Factor
[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         /* address of exception_handler -- this exists as a separate relocation
30         type since its used in a situation where relocation arguments cannot
31         be passed in, and so RT_DLSYM is inappropriate (Windows only) */
32         RT_EXCEPTION_HANDLER,
33         /* pointer to a float's payload */
34         RT_FLOAT,
35
36 };
37
38 enum relocation_class {
39         /* absolute address in a pointer-width location */
40         RC_ABSOLUTE_CELL,
41         /* absolute address in a 4 byte location */
42         RC_ABSOLUTE,
43         /* relative address in a 4 byte location */
44         RC_RELATIVE,
45         /* absolute address in a PowerPC LIS/ORI sequence */
46         RC_ABSOLUTE_PPC_2_2,
47         /* absolute address in a PowerPC LWZ instruction */
48         RC_ABSOLUTE_PPC_2,
49         /* relative address in a PowerPC LWZ/STW/BC instruction */
50         RC_RELATIVE_PPC_2,
51         /* relative address in a PowerPC B/BL instruction */
52         RC_RELATIVE_PPC_3,
53         /* relative address in an ARM B/BL instruction */
54         RC_RELATIVE_ARM_3,
55         /* pointer to address in an ARM LDR/STR instruction */
56         RC_INDIRECT_ARM,
57         /* pointer to address in an ARM LDR/STR instruction offset by 8 bytes */
58         RC_INDIRECT_ARM_PC,
59         /* absolute address in a 2 byte location */
60         RC_ABSOLUTE_2,
61         /* absolute address in a 1 byte location */
62         RC_ABSOLUTE_1,
63 };
64
65 static const cell rel_absolute_ppc_2_mask = 0xffff;
66 static const cell rel_relative_ppc_2_mask = 0xfffc;
67 static const cell rel_relative_ppc_3_mask = 0x3fffffc;
68 static const cell rel_indirect_arm_mask = 0xfff;
69 static const cell rel_relative_arm_3_mask = 0xffffff;
70
71 /* code relocation table consists of a table of entries for each fixup */
72 struct relocation_entry {
73         u32 value;
74
75         explicit relocation_entry(u32 value_) : value(value_) {}
76
77         relocation_entry(relocation_type rel_type,
78                 relocation_class rel_class,
79                 cell offset)
80         {
81                 value = (u32)((rel_type << 28) | (rel_class << 24) | offset);
82         }
83
84         relocation_type rel_type()
85         {
86                 return (relocation_type)((value & 0xf0000000) >> 28);
87         }
88
89         relocation_class rel_class()
90         {
91                 return (relocation_class)((value & 0x0f000000) >> 24);
92         }
93
94         cell rel_offset()
95         {
96                 return (value & 0x00ffffff);
97         }
98
99         int number_of_parameters()
100         {
101                 switch(rel_type())
102                 {
103                 case RT_VM:
104                         return 1;
105                 case RT_DLSYM:
106                         return 2;
107                 case RT_ENTRY_POINT:
108                 case RT_ENTRY_POINT_PIC:
109                 case RT_ENTRY_POINT_PIC_TAIL:
110                 case RT_LITERAL:
111                 case RT_HERE:
112                 case RT_UNTAGGED:
113                 case RT_THIS:
114                 case RT_MEGAMORPHIC_CACHE_HITS:
115                 case RT_CARDS_OFFSET:
116                 case RT_DECKS_OFFSET:
117                 case RT_EXCEPTION_HANDLER:
118                 case RT_FLOAT:
119                         return 0;
120                 default:
121                         critical_error("Bad rel type",rel_type());
122                         return -1; /* Can't happen */
123                 }
124         }
125 };
126
127 struct instruction_operand {
128         relocation_entry rel;
129         code_block *compiled;
130         cell index;
131         cell pointer;
132
133         instruction_operand(relocation_entry rel_, code_block *compiled_, cell index_);
134
135         relocation_type rel_type()
136         {
137                 return rel.rel_type();
138         }
139
140         cell rel_offset()
141         {
142                 return rel.rel_offset();
143         }
144
145         cell parameter_index()
146         {
147                 return index;
148         }
149
150         code_block *parent_code_block()
151         {
152                 return compiled;
153         }
154
155         fixnum load_value_2_2();
156         fixnum load_value_masked(cell mask, cell bits, cell shift);
157         fixnum load_value(cell relative_to);
158         fixnum load_value();
159         cell load_float(cell relative_to);
160         cell load_float();
161         code_block *load_code_block(cell relative_to);
162         code_block *load_code_block();
163
164         void store_value_2_2(fixnum value);
165         void store_value_masked(fixnum value, cell mask, cell shift);
166         void store_value(fixnum value);
167         void store_float(cell value);
168         void store_code_block(code_block *compiled);
169 };
170
171 }