glsl2: Wrap includes of C interfaces with extern "C".
[mesa.git] / src / glsl / ir_clone.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <string.h>
25 #include "ir.h"
26 #include "glsl_types.h"
27 extern "C" {
28 #include "hash_table.h"
29 }
30
31 /**
32 * Duplicate an IR variable
33 *
34 * \note
35 * This will probably be made \c virtual and moved to the base class
36 * eventually.
37 */
38 ir_instruction *
39 ir_variable::clone(struct hash_table *ht) const
40 {
41 void *ctx = talloc_parent(this);
42 ir_variable *var = new(ctx) ir_variable(type, name);
43
44 var->max_array_access = this->max_array_access;
45 var->read_only = this->read_only;
46 var->centroid = this->centroid;
47 var->invariant = this->invariant;
48 var->mode = this->mode;
49 var->interpolation = this->interpolation;
50
51 if (ht) {
52 hash_table_insert(ht, var, (void *)const_cast<ir_variable *>(this));
53 }
54
55 return var;
56 }
57
58 ir_instruction *
59 ir_swizzle::clone(struct hash_table *ht) const
60 {
61 void *ctx = talloc_parent(this);
62 return new(ctx) ir_swizzle((ir_rvalue *)this->val->clone(ht), this->mask);
63 }
64
65 ir_instruction *
66 ir_return::clone(struct hash_table *ht) const
67 {
68 void *ctx = talloc_parent(this);
69 ir_rvalue *new_value = NULL;
70
71 if (this->value)
72 new_value = (ir_rvalue *)this->value->clone(ht);
73
74 return new(ctx) ir_return(new_value);
75 }
76
77 ir_instruction *
78 ir_loop_jump::clone(struct hash_table *ht) const
79 {
80 void *ctx = talloc_parent(this);
81 (void)ht;
82
83 return new(ctx) ir_loop_jump(this->mode);
84 }
85
86 ir_instruction *
87 ir_if::clone(struct hash_table *ht) const
88 {
89 void *ctx = talloc_parent(this);
90 ir_if *new_if = new(ctx) ir_if((ir_rvalue *)this->condition->clone(ht));
91
92 foreach_iter(exec_list_iterator, iter, this->then_instructions) {
93 ir_instruction *ir = (ir_instruction *)iter.get();
94 new_if->then_instructions.push_tail(ir->clone(ht));
95 }
96
97 foreach_iter(exec_list_iterator, iter, this->else_instructions) {
98 ir_instruction *ir = (ir_instruction *)iter.get();
99 new_if->else_instructions.push_tail(ir->clone(ht));
100 }
101
102 return new_if;
103 }
104
105 ir_instruction *
106 ir_loop::clone(struct hash_table *ht) const
107 {
108 void *ctx = talloc_parent(this);
109 ir_loop *new_loop = new(ctx) ir_loop();
110
111 if (this->from)
112 new_loop->from = (ir_rvalue *)this->from->clone(ht);
113 if (this->to)
114 new_loop->to = (ir_rvalue *)this->to->clone(ht);
115 if (this->increment)
116 new_loop->increment = (ir_rvalue *)this->increment->clone(ht);
117 new_loop->counter = counter;
118
119 foreach_iter(exec_list_iterator, iter, this->body_instructions) {
120 ir_instruction *ir = (ir_instruction *)iter.get();
121 new_loop->body_instructions.push_tail(ir->clone(ht));
122 }
123
124 return new_loop;
125 }
126
127 ir_instruction *
128 ir_call::clone(struct hash_table *ht) const
129 {
130 void *ctx = talloc_parent(this);
131 exec_list new_parameters;
132
133 foreach_iter(exec_list_iterator, iter, this->actual_parameters) {
134 ir_instruction *ir = (ir_instruction *)iter.get();
135 new_parameters.push_tail(ir->clone(ht));
136 }
137
138 return new(ctx) ir_call(this->callee, &new_parameters);
139 }
140
141 ir_instruction *
142 ir_expression::clone(struct hash_table *ht) const
143 {
144 void *ctx = talloc_parent(this);
145 ir_rvalue *op[2] = {NULL, NULL};
146 unsigned int i;
147
148 for (i = 0; i < get_num_operands(); i++) {
149 op[i] = (ir_rvalue *)this->operands[i]->clone(ht);
150 }
151
152 return new(ctx) ir_expression(this->operation, this->type, op[0], op[1]);
153 }
154
155 ir_instruction *
156 ir_dereference_variable::clone(struct hash_table *ht) const
157 {
158 void *ctx = talloc_parent(this);
159 ir_variable *new_var;
160
161 if (ht) {
162 new_var = (ir_variable *)hash_table_find(ht, this->var);
163 if (!new_var)
164 new_var = this->var;
165 } else {
166 new_var = this->var;
167 }
168
169 return new(ctx) ir_dereference_variable(new_var);
170 }
171
172 ir_instruction *
173 ir_dereference_array::clone(struct hash_table *ht) const
174 {
175 void *ctx = talloc_parent(this);
176 return new(ctx) ir_dereference_array((ir_rvalue *)this->array->clone(ht),
177 (ir_rvalue *)this->array_index->clone(ht));
178 }
179
180 ir_instruction *
181 ir_dereference_record::clone(struct hash_table *ht) const
182 {
183 void *ctx = talloc_parent(this);
184 return new(ctx) ir_dereference_record((ir_rvalue *)this->record->clone(ht),
185 this->field);
186 }
187
188 ir_instruction *
189 ir_texture::clone(struct hash_table *ht) const
190 {
191 void *ctx = talloc_parent(this);
192 ir_texture *new_tex = new(ctx) ir_texture(this->op);
193
194 new_tex->sampler = (ir_dereference *)this->sampler->clone(ht);
195 new_tex->coordinate = (ir_rvalue *)this->coordinate->clone(ht);
196 if (this->projector)
197 new_tex->projector = (ir_rvalue *)this->projector->clone(ht);
198 if (this->shadow_comparitor) {
199 new_tex->shadow_comparitor =
200 (ir_rvalue *)this->shadow_comparitor->clone(ht);
201 }
202
203 for (int i = 0; i < 3; i++)
204 new_tex->offsets[i] = this->offsets[i];
205
206 switch (this->op) {
207 case ir_tex:
208 break;
209 case ir_txb:
210 new_tex->lod_info.bias = (ir_rvalue *)this->lod_info.bias->clone(ht);
211 break;
212 case ir_txl:
213 case ir_txf:
214 new_tex->lod_info.lod = (ir_rvalue *)this->lod_info.lod->clone(ht);
215 break;
216 case ir_txd:
217 new_tex->lod_info.grad.dPdx =
218 (ir_rvalue *)this->lod_info.grad.dPdx->clone(ht);
219 new_tex->lod_info.grad.dPdy =
220 (ir_rvalue *)this->lod_info.grad.dPdy->clone(ht);
221 break;
222 }
223
224 return new_tex;
225 }
226
227 ir_instruction *
228 ir_assignment::clone(struct hash_table *ht) const
229 {
230 ir_rvalue *new_condition = NULL;
231
232 if (this->condition)
233 new_condition = (ir_rvalue *)this->condition->clone(ht);
234
235 void *ctx = talloc_parent(this);
236 return new(ctx) ir_assignment((ir_rvalue *)this->lhs->clone(ht),
237 (ir_rvalue *)this->rhs->clone(ht),
238 new_condition);
239 }
240
241 ir_instruction *
242 ir_function::clone(struct hash_table *ht) const
243 {
244 (void)ht;
245 /* FINISHME */
246 abort();
247 }
248
249 ir_instruction *
250 ir_function_signature::clone(struct hash_table *ht) const
251 {
252 (void)ht;
253 /* FINISHME */
254 abort();
255 }
256
257 ir_instruction *
258 ir_constant::clone(struct hash_table *ht) const
259 {
260 void *ctx = talloc_parent(this);
261 (void)ht;
262
263 switch (this->type->base_type) {
264 case GLSL_TYPE_UINT:
265 case GLSL_TYPE_INT:
266 case GLSL_TYPE_FLOAT:
267 case GLSL_TYPE_BOOL:
268 return new(ctx) ir_constant(this->type, &this->value);
269
270 case GLSL_TYPE_STRUCT: {
271 ir_constant *c = new(ctx) ir_constant;
272
273 c->type = this->type;
274 for (exec_node *node = this->components.head
275 ; !node->is_tail_sentinal()
276 ; node = node->next) {
277 ir_constant *const orig = (ir_constant *) node;
278
279 c->components.push_tail(orig->clone(NULL));
280 }
281
282 return c;
283 }
284
285 default:
286 assert(!"Should not get here."); break;
287 return NULL;
288 }
289 }