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_loop_jump::clone(struct hash_table
*ht
) const
80 void *ctx
= talloc_parent(this);
83 return new(ctx
) ir_loop_jump(this->mode
);
87 ir_if::clone(struct hash_table
*ht
) const
89 void *ctx
= talloc_parent(this);
90 ir_if
*new_if
= new(ctx
) ir_if((ir_rvalue
*)this->condition
->clone(ht
));
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
));
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
));
106 ir_loop::clone(struct hash_table
*ht
) const
108 void *ctx
= talloc_parent(this);
109 ir_loop
*new_loop
= new(ctx
) ir_loop();
112 new_loop
->from
= (ir_rvalue
*)this->from
->clone(ht
);
114 new_loop
->to
= (ir_rvalue
*)this->to
->clone(ht
);
116 new_loop
->increment
= (ir_rvalue
*)this->increment
->clone(ht
);
117 new_loop
->counter
= counter
;
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
));
128 ir_call::clone(struct hash_table
*ht
) const
130 void *ctx
= talloc_parent(this);
131 exec_list new_parameters
;
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
));
138 return new(ctx
) ir_call(this->callee
, &new_parameters
);
142 ir_expression::clone(struct hash_table
*ht
) const
144 void *ctx
= talloc_parent(this);
145 ir_rvalue
*op
[2] = {NULL
, NULL
};
148 for (i
= 0; i
< get_num_operands(); i
++) {
149 op
[i
] = (ir_rvalue
*)this->operands
[i
]->clone(ht
);
152 return new(ctx
) ir_expression(this->operation
, this->type
, op
[0], op
[1]);
156 ir_dereference_variable::clone(struct hash_table
*ht
) const
158 void *ctx
= talloc_parent(this);
159 ir_variable
*new_var
;
162 new_var
= (ir_variable
*)hash_table_find(ht
, this->var
);
169 return new(ctx
) ir_dereference_variable(new_var
);
173 ir_dereference_array::clone(struct hash_table
*ht
) const
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
));
181 ir_dereference_record::clone(struct hash_table
*ht
) const
183 void *ctx
= talloc_parent(this);
184 return new(ctx
) ir_dereference_record((ir_rvalue
*)this->record
->clone(ht
),
189 ir_texture::clone(struct hash_table
*ht
) const
191 void *ctx
= talloc_parent(this);
192 ir_texture
*new_tex
= new(ctx
) ir_texture(this->op
);
194 new_tex
->sampler
= (ir_dereference
*)this->sampler
->clone(ht
);
195 new_tex
->coordinate
= (ir_rvalue
*)this->coordinate
->clone(ht
);
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
);
203 for (int i
= 0; i
< 3; i
++)
204 new_tex
->offsets
[i
] = this->offsets
[i
];
210 new_tex
->lod_info
.bias
= (ir_rvalue
*)this->lod_info
.bias
->clone(ht
);
214 new_tex
->lod_info
.lod
= (ir_rvalue
*)this->lod_info
.lod
->clone(ht
);
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
);
228 ir_assignment::clone(struct hash_table
*ht
) const
230 ir_rvalue
*new_condition
= NULL
;
233 new_condition
= (ir_rvalue
*)this->condition
->clone(ht
);
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
),
242 ir_function::clone(struct hash_table
*ht
) const
250 ir_function_signature::clone(struct hash_table
*ht
) const
258 ir_constant::clone(struct hash_table
*ht
) const
260 void *ctx
= talloc_parent(this);
263 switch (this->type
->base_type
) {
266 case GLSL_TYPE_FLOAT
:
268 return new(ctx
) ir_constant(this->type
, &this->value
);
270 case GLSL_TYPE_STRUCT
: {
271 ir_constant
*c
= new(ctx
) ir_constant
;
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
;
279 c
->components
.push_tail(orig
->clone(NULL
));
286 assert(!"Should not get here."); break;