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