2 * Copyright © 2010 Intel Corporation
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:
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
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.
26 #include "glsl_types.h"
28 #include "hash_table.h"
32 * Duplicate an IR variable
35 * This will probably be made \c virtual and moved to the base class
39 ir_variable::clone(struct hash_table
*ht
) const
41 void *ctx
= talloc_parent(this);
42 ir_variable
*var
= new(ctx
) ir_variable(type
, name
);
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
;
52 hash_table_insert(ht
, var
, (void *)const_cast<ir_variable
*>(this));
59 ir_swizzle::clone(struct hash_table
*ht
) const
61 void *ctx
= talloc_parent(this);
62 return new(ctx
) ir_swizzle((ir_rvalue
*)this->val
->clone(ht
), this->mask
);
66 ir_return::clone(struct hash_table
*ht
) const
68 void *ctx
= talloc_parent(this);
69 ir_rvalue
*new_value
= NULL
;
72 new_value
= (ir_rvalue
*)this->value
->clone(ht
);
74 return new(ctx
) ir_return(new_value
);
78 ir_discard::clone(struct hash_table
*ht
) const
80 void *ctx
= talloc_parent(this);
81 ir_rvalue
*new_condition
= NULL
;
83 if (this->condition
!= NULL
)
84 new_condition
= (ir_rvalue
*) this->condition
->clone(ht
);
86 return new(ctx
) ir_discard(new_condition
);
90 ir_loop_jump::clone(struct hash_table
*ht
) const
92 void *ctx
= talloc_parent(this);
95 return new(ctx
) ir_loop_jump(this->mode
);
99 ir_if::clone(struct hash_table
*ht
) const
101 void *ctx
= talloc_parent(this);
102 ir_if
*new_if
= new(ctx
) ir_if((ir_rvalue
*)this->condition
->clone(ht
));
104 foreach_iter(exec_list_iterator
, iter
, this->then_instructions
) {
105 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
106 new_if
->then_instructions
.push_tail(ir
->clone(ht
));
109 foreach_iter(exec_list_iterator
, iter
, this->else_instructions
) {
110 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
111 new_if
->else_instructions
.push_tail(ir
->clone(ht
));
118 ir_loop::clone(struct hash_table
*ht
) const
120 void *ctx
= talloc_parent(this);
121 ir_loop
*new_loop
= new(ctx
) ir_loop();
124 new_loop
->from
= (ir_rvalue
*)this->from
->clone(ht
);
126 new_loop
->to
= (ir_rvalue
*)this->to
->clone(ht
);
128 new_loop
->increment
= (ir_rvalue
*)this->increment
->clone(ht
);
129 new_loop
->counter
= counter
;
131 foreach_iter(exec_list_iterator
, iter
, this->body_instructions
) {
132 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
133 new_loop
->body_instructions
.push_tail(ir
->clone(ht
));
140 ir_call::clone(struct hash_table
*ht
) const
142 void *ctx
= talloc_parent(this);
143 exec_list new_parameters
;
145 foreach_iter(exec_list_iterator
, iter
, this->actual_parameters
) {
146 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
147 new_parameters
.push_tail(ir
->clone(ht
));
150 return new(ctx
) ir_call(this->callee
, &new_parameters
);
154 ir_expression::clone(struct hash_table
*ht
) const
156 void *ctx
= talloc_parent(this);
157 ir_rvalue
*op
[2] = {NULL
, NULL
};
160 for (i
= 0; i
< get_num_operands(); i
++) {
161 op
[i
] = (ir_rvalue
*)this->operands
[i
]->clone(ht
);
164 return new(ctx
) ir_expression(this->operation
, this->type
, op
[0], op
[1]);
167 ir_dereference_variable
*
168 ir_dereference_variable::clone(struct hash_table
*ht
) const
170 void *ctx
= talloc_parent(this);
171 ir_variable
*new_var
;
174 new_var
= (ir_variable
*)hash_table_find(ht
, this->var
);
181 return new(ctx
) ir_dereference_variable(new_var
);
184 ir_dereference_array
*
185 ir_dereference_array::clone(struct hash_table
*ht
) const
187 void *ctx
= talloc_parent(this);
188 return new(ctx
) ir_dereference_array((ir_rvalue
*)this->array
->clone(ht
),
189 (ir_rvalue
*)this->array_index
->clone(ht
));
192 ir_dereference_record
*
193 ir_dereference_record::clone(struct hash_table
*ht
) const
195 void *ctx
= talloc_parent(this);
196 return new(ctx
) ir_dereference_record((ir_rvalue
*)this->record
->clone(ht
),
201 ir_texture::clone(struct hash_table
*ht
) const
203 void *ctx
= talloc_parent(this);
204 ir_texture
*new_tex
= new(ctx
) ir_texture(this->op
);
206 new_tex
->sampler
= (ir_dereference
*)this->sampler
->clone(ht
);
207 new_tex
->coordinate
= (ir_rvalue
*)this->coordinate
->clone(ht
);
209 new_tex
->projector
= (ir_rvalue
*)this->projector
->clone(ht
);
210 if (this->shadow_comparitor
) {
211 new_tex
->shadow_comparitor
=
212 (ir_rvalue
*)this->shadow_comparitor
->clone(ht
);
215 for (int i
= 0; i
< 3; i
++)
216 new_tex
->offsets
[i
] = this->offsets
[i
];
222 new_tex
->lod_info
.bias
= (ir_rvalue
*)this->lod_info
.bias
->clone(ht
);
226 new_tex
->lod_info
.lod
= (ir_rvalue
*)this->lod_info
.lod
->clone(ht
);
229 new_tex
->lod_info
.grad
.dPdx
=
230 (ir_rvalue
*)this->lod_info
.grad
.dPdx
->clone(ht
);
231 new_tex
->lod_info
.grad
.dPdy
=
232 (ir_rvalue
*)this->lod_info
.grad
.dPdy
->clone(ht
);
240 ir_assignment::clone(struct hash_table
*ht
) const
242 ir_rvalue
*new_condition
= NULL
;
245 new_condition
= (ir_rvalue
*)this->condition
->clone(ht
);
247 void *ctx
= talloc_parent(this);
248 return new(ctx
) ir_assignment((ir_rvalue
*)this->lhs
->clone(ht
),
249 (ir_rvalue
*)this->rhs
->clone(ht
),
254 ir_function::clone(struct hash_table
*ht
) const
261 ir_function_signature
*
262 ir_function_signature::clone(struct hash_table
*ht
) const
270 ir_constant::clone(struct hash_table
*ht
) const
272 void *ctx
= talloc_parent(this);
275 switch (this->type
->base_type
) {
278 case GLSL_TYPE_FLOAT
:
280 return new(ctx
) ir_constant(this->type
, &this->value
);
282 case GLSL_TYPE_STRUCT
: {
283 ir_constant
*c
= new(ctx
) ir_constant
;
285 c
->type
= this->type
;
286 for (exec_node
*node
= this->components
.head
287 ; !node
->is_tail_sentinal()
288 ; node
= node
->next
) {
289 ir_constant
*const orig
= (ir_constant
*) node
;
291 c
->components
.push_tail(orig
->clone(NULL
));
298 assert(!"Should not get here."); break;