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