* instruction stream.
*/
-#define NULL 0
#include "ir.h"
#include "ir_visitor.h"
#include "ir_expression_flattening.h"
#include "glsl_types.h"
-class ir_expression_flattening_visitor : public ir_visitor {
+class ir_expression_flattening_visitor : public ir_hierarchical_visitor {
public:
ir_expression_flattening_visitor(ir_instruction *base_ir,
bool (*predicate)(ir_instruction *ir))
{
this->base_ir = base_ir;
this->predicate = predicate;
-
- /* empty */
}
virtual ~ir_expression_flattening_visitor()
/* empty */
}
- /**
- * \name Visit methods
- *
- * As typical for the visitor pattern, there must be one \c visit method for
- * each concrete subclass of \c ir_instruction. Virtual base classes within
- * the hierarchy should not have \c visit methods.
- */
- /*@{*/
- virtual void visit(ir_variable *);
- virtual void visit(ir_loop *);
- virtual void visit(ir_loop_jump *);
- virtual void visit(ir_function_signature *);
- virtual void visit(ir_function *);
- virtual void visit(ir_expression *);
- virtual void visit(ir_swizzle *);
- virtual void visit(ir_dereference *);
- virtual void visit(ir_assignment *);
- virtual void visit(ir_constant *);
- virtual void visit(ir_call *);
- virtual void visit(ir_return *);
- virtual void visit(ir_if *);
- /*@}*/
+ virtual ir_visitor_status visit_enter(ir_call *);
+ virtual ir_visitor_status visit_enter(ir_return *);
+ virtual ir_visitor_status visit_enter(ir_function_signature *);
+ virtual ir_visitor_status visit_enter(ir_if *);
+ virtual ir_visitor_status visit_enter(ir_loop *);
+ virtual ir_visitor_status visit_leave(ir_expression *);
+ virtual ir_visitor_status visit_leave(ir_swizzle *);
bool (*predicate)(ir_instruction *ir);
ir_instruction *base_ir;
}
}
-void
-ir_expression_flattening_visitor::visit(ir_variable *ir)
-{
- (void) ir;
-}
-void
-ir_expression_flattening_visitor::visit(ir_loop *ir)
+static ir_rvalue *
+operand_to_temp(ir_instruction *base_ir, ir_rvalue *ir)
{
- do_expression_flattening(&ir->body_instructions, this->predicate);
+ ir_variable *var;
+ ir_assignment *assign;
+
+ var = new ir_variable(ir->type, "flattening_tmp");
+ base_ir->insert_before(var);
+
+ assign = new ir_assignment(new ir_dereference_variable(var),
+ ir,
+ NULL);
+ base_ir->insert_before(assign);
+
+ return new ir_dereference_variable(var);
}
-void
-ir_expression_flattening_visitor::visit(ir_loop_jump *ir)
+ir_visitor_status
+ir_expression_flattening_visitor::visit_enter(ir_function_signature *ir)
{
- (void) ir;
-}
+ do_expression_flattening(&ir->body, this->predicate);
+ return visit_continue_with_parent;
+}
-void
-ir_expression_flattening_visitor::visit(ir_function_signature *ir)
+ir_visitor_status
+ir_expression_flattening_visitor::visit_enter(ir_loop *ir)
{
- do_expression_flattening(&ir->body, this->predicate);
+ do_expression_flattening(&ir->body_instructions, this->predicate);
+
+ return visit_continue_with_parent;
}
-void
-ir_expression_flattening_visitor::visit(ir_function *ir)
+ir_visitor_status
+ir_expression_flattening_visitor::visit_enter(ir_if *ir)
{
- (void) ir;
+ ir->condition->accept(this);
+
+ do_expression_flattening(&ir->then_instructions, this->predicate);
+ do_expression_flattening(&ir->else_instructions, this->predicate);
+
+ return visit_continue_with_parent;
}
-void
-ir_expression_flattening_visitor::visit(ir_expression *ir)
+ir_visitor_status
+ir_expression_flattening_visitor::visit_leave(ir_expression *ir)
{
unsigned int operand;
for (operand = 0; operand < ir->get_num_operands(); operand++) {
- ir->operands[operand]->accept(this);
-
/* If the operand matches the predicate, then we'll assign its
* value to a temporary and deref the temporary as the operand.
*/
if (this->predicate(ir->operands[operand])) {
- ir_variable *var;
- ir_assignment *assign;
-
- var = new ir_variable(ir->operands[operand]->type, "flattening_tmp");
- this->base_ir->insert_before(var);
-
- assign = new ir_assignment(new ir_dereference(var),
- ir->operands[operand],
- NULL);
- this->base_ir->insert_before(assign);
-
- ir->operands[operand] = new ir_dereference(var);
+ ir->operands[operand] = operand_to_temp(base_ir,
+ ir->operands[operand]);
}
}
-}
-
-void
-ir_expression_flattening_visitor::visit(ir_swizzle *ir)
-{
- ir->val->accept(this);
+ return visit_continue;
}
-
-void
-ir_expression_flattening_visitor::visit(ir_dereference *ir)
+ir_visitor_status
+ir_expression_flattening_visitor::visit_leave(ir_swizzle *ir)
{
- if (ir->mode == ir_dereference::ir_reference_array) {
- ir->selector.array_index->accept(this);
+ if (this->predicate(ir->val)) {
+ ir->val = operand_to_temp(this->base_ir, ir->val);
}
- ir->var->accept(this);
-}
-void
-ir_expression_flattening_visitor::visit(ir_assignment *ir)
-{
- ir->rhs->accept(this);
+ return visit_continue;
}
-
-void
-ir_expression_flattening_visitor::visit(ir_constant *ir)
-{
- (void) ir;
-}
-
-
-void
-ir_expression_flattening_visitor::visit(ir_call *ir)
+ir_visitor_status
+ir_expression_flattening_visitor::visit_enter(ir_call *ir)
{
+ /* FINISHME: Why not process the call parameters? (Same behavior as original
+ * FINISHME: code.)
+ */
(void) ir;
+ return visit_continue_with_parent;
}
-void
-ir_expression_flattening_visitor::visit(ir_return *ir)
+ir_visitor_status
+ir_expression_flattening_visitor::visit_enter(ir_return *ir)
{
+ /* FINISHME: Why not process the return value? (Same behavior as original
+ * FINISHME: code.)
+ */
(void) ir;
-}
-
-
-void
-ir_expression_flattening_visitor::visit(ir_if *ir)
-{
- ir->condition->accept(this);
-
- do_expression_flattening(&ir->then_instructions, this->predicate);
- do_expression_flattening(&ir->else_instructions, this->predicate);
+ return visit_continue_with_parent;
}