1 /*
2  * Copyright (c) 2005 - 2010, Nils R. Weller
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 #ifndef REG_H
28 #define REG_H
29 
30 struct decl;
31 struct token;
32 struct icode_list;
33 struct stack_block;
34 struct function;
35 struct reg;
36 
37 #include <stddef.h>
38 
39 /*
40  * Physical register
41  */
42 struct reg {
43 	char		*name;
44 	int		type;
45 #define REG_GPR		1
46 #define REG_FPR		2
47 #define REG_SP		3
48 #define REG_BP		4
49 /*
50 #define REG_RP	5 return pointer
51 */
52 	size_t		size; /* size of register in bytes */
53 	int		used; /* in use? */
54 	int		allocatable; /* if in use - may be reallocated? */
55 	/*
56 	 * 12/27/08: New flag to mark register as dedicated (not
57 	 * allocatable or otherwise generally usable - e.g. temp
58 	 * GPR or stack/frame pointer). used=0 and allocatable=0
59 	 * is not enough because that can sometimes happen with
60 	 * undedicated registers too
61 	 */
62 	int		dedicated;
63 	struct reg	**composed_of;
64 	struct vreg	*vreg; /* backing object */
65 /*#ifdef DEBUG6*/
66 	int		line;
67 	int		nallocs;
68 /*#endif*/
69 };
70 
71 
72 /*
73  * Virtual register
74  */
75 struct vreg {
76 	struct decl		*var_backed;
77 	struct token		*from_const;
78 	struct vreg		*from_ptr;
79 	struct stack_block	*stack_addr;
80 	struct type		*type;
81 	size_t			size;
82 	int			on_var;
83 	int			is_nullptr_const;
84 
85 	/*
86 	 * if is_multi_reg_obj is nonzero, this object occupies multiple
87 	 * registers. It is absolutely essential to remember that this
88 	 * is not just a boolean flag - it must contain the NUMBER of
89 	 * registers required too!!! (e.g. 2 for ``long long'' on x86)
90 	 */
91 	int			is_multi_reg_obj;
92 	int			struct_ret;
93 	struct decl		*memberdecl;
94 	struct vreg		*parent;
95 	long			addr_offset;
96 #define VREG_SEQNO	1
97 #if VREG_SEQNO
98 	int			seqno;
99 #endif
100 
101 #if 0
102 	struct reg		*preg;
103 	struct reg		*preg2;
104 #endif
105 	struct reg		*pregs[2];
106 };
107 
108 int
109 reg_unused(struct reg *r);
110 
111 int
112 reg_allocatable(struct reg *r);
113 
114 void
115 reg_set_allocatable(struct reg *r);
116 
117 
118 void
119 reg_set_unallocatable(struct reg *r);
120 
121 void
122 vreg_set_allocatable(struct vreg *r);
123 
124 void
125 vreg_set_unallocatable(struct vreg *r);
126 
127 void
128 reg_set_dedicated(struct reg *r);
129 
130 int
131 is_x87_trash(struct vreg *vr);
132 
133 #if 0
134 void
135 reg_save(struct function *f, struct reg *r, struct icode_list *il);
136 #endif
137 
138 struct vreg *
139 dup_vreg(struct vreg *);
140 
141 struct vreg *
142 vreg_alloc(struct decl *dec, struct token *constant, struct vreg *from_ptr,
143 	struct type *t);
144 
145 struct reg *
146 vreg_faultin(struct reg *r, struct reg *r2,
147 struct vreg *vr, struct icode_list *il, int whatfor);
148 
149 struct reg *
150 vreg_faultin_dedicated(struct reg *r, struct reg *r2,
151 struct vreg *vr, struct icode_list *il, int whatfor);
152 
153 struct reg *
154 vreg_faultin_x87(struct reg *r, struct reg *r2,
155 struct vreg *vr, struct icode_list *il, int whatfor);
156 
157 struct reg *
158 vreg_faultin_protected(struct vreg *prot, struct reg *r, struct reg *r2,
159 struct vreg *vr, struct icode_list *il, int whatfor);
160 
161 struct reg *
162 vreg_faultin_ptr(struct vreg *vr, struct icode_list *il);
163 
164 void
165 vreg_anonymify(struct vreg **vr, struct reg *r, struct reg *r2,
166 	struct icode_list *il);
167 
168 void
169 vreg_do_anonymify(struct vreg *vr);
170 
171 struct vreg *
172 vreg_disconnect(struct vreg *);
173 
174 void
175 vreg_freetmpvrs(void);
176 
177 void
178 allow_vreg_map_preg(void);
179 void
180 forbid_vreg_map_preg(void);
181 
182 void
183 backend_vreg_map_preg(struct vreg *, struct reg *);
184 void
185 backend_vreg_unmap_preg(struct reg *);
186 void
187 backend_vreg_map_preg2(struct vreg *, struct reg *);
188 void
189 backend_vreg_unmap_preg2(struct reg *);
190 
191 void
192 vreg_map_preg(struct vreg *vr, struct reg *r);
193 void
194 vreg_map_preg2(struct vreg *vr, struct reg *r);
195 
196 void
197 vreg_map_preg_dedicated(struct vreg *vr, struct reg *r);
198 
199 void
200 reg_set_unused(struct reg *);
201 
202 void
203 free_preg(struct reg *r, struct icode_list *il, int invalidate, int savereg);
204 void
205 free_pregs_vreg(struct vreg *vr, struct icode_list *il,
206 	int invalidate, int savereg);
207 
208 void
209 free_preg_dedicated(struct reg *r, struct icode_list *il, int invalidate, int savereg);
210 
211 
212 void
213 save_reg(struct reg *r, struct icode_list *il);
214 
215 void
216 free_physreg(struct reg *r);
217 
218 int
219 is_member_of_reg(struct reg *r, struct reg *member);
220 
221 void
222 free_other_members(struct reg *r, struct reg *member);
223 
224 void
225 vreg_set_new_type(struct vreg *vr, struct type *ty);
226 
227 void
228 vreg_reinterpret_as(struct vreg **vr, struct type *from,
229 	struct type *to, struct icode_list *il);
230 
231 struct vreg *
232 vreg_back_by_ptr(struct vreg *vr, struct reg *ptrreg, int is_backend);
233 
234 int
235 vreg_needs_pic_reloc(struct vreg *);
236 
237 struct vreg *
238 copy_vreg(struct vreg *vr);
239 
240 
241 #endif
242 
243