From 3b96996b7eb6e3603a5f138177867c3e856e0dfa Mon Sep 17 00:00:00 2001 From: Kenneth Graunke Date: Wed, 7 Apr 2010 17:18:29 -0700 Subject: [PATCH] Move array of operator strings out of ir_print_visitor.cpp. Also implement a reverse-lookup function for use in the IR reader. --- ir.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++ ir.h | 14 ++++++++-- ir_print_visitor.cpp | 52 +---------------------------------- 3 files changed, 77 insertions(+), 53 deletions(-) diff --git a/ir.cpp b/ir.cpp index e7e5dee00cb..3f4c40d0c99 100644 --- a/ir.cpp +++ b/ir.cpp @@ -108,6 +108,70 @@ ir_expression::get_num_operands(ir_expression_operation op) return num_operands[op]; } +static const char *const operator_strs[] = { + "~", + "!", + "-", + "abs", + "rcp", + "rsq", + "sqrt", + "exp", + "log", + "exp2", + "log2", + "f2i", + "i2f", + "f2b", + "b2f", + "i2b", + "b2i", + "u2f", + "trunc", + "ceil", + "floor", + "+", + "-", + "*", + "/", + "%", + "<", + ">", + "<=", + ">=", + "==", + "!=", + "<<", + ">>", + "&", + "^", + "|", + "&&", + "^^", + "||", + "dot", + "min", + "max", + "pow", +}; + +const char *ir_expression::operator_string() +{ + assert((unsigned int) operation <= + sizeof(operator_strs) / sizeof(operator_strs[0])); + return operator_strs[operation]; +} + +ir_expression_operation +ir_expression::get_operator(const char *str) +{ + const int operator_count = sizeof(operator_strs) / sizeof(operator_strs[0]); + for (int op = 0; op < operator_count; op++) { + if (strcmp(str, operator_strs[op]) == 0) + return (ir_expression_operation) op; + } + return (ir_expression_operation) -1; +} ir_constant::ir_constant(const struct glsl_type *type, const void *data) { diff --git a/ir.h b/ir.h index df64e488235..42e8264d41d 100644 --- a/ir.h +++ b/ir.h @@ -415,9 +415,9 @@ public: ir_rvalue *condition; }; -/* Update ir_expression::num_operands() and ir_print_visitor.cpp when +/* Update ir_expression::num_operands() and operator_strs when * updating this list. -*/ + */ enum ir_expression_operation { ir_unop_bit_not, ir_unop_logic_not, @@ -498,6 +498,16 @@ public: return get_num_operands(operation); } + /** + * Return a string representing this expression's operator. + */ + const char *operator_string(); + + /** + * Do a reverse-lookup to translate the given string into an operator. + */ + static ir_expression_operation get_operator(const char *); + virtual void accept(ir_visitor *v) { v->visit(this); diff --git a/ir_print_visitor.cpp b/ir_print_visitor.cpp index 56048fe40f2..778a5c16460 100644 --- a/ir_print_visitor.cpp +++ b/ir_print_visitor.cpp @@ -101,61 +101,11 @@ void ir_print_visitor::visit(ir_function *ir) void ir_print_visitor::visit(ir_expression *ir) { - static const char *const operators[] = { - "~", - "!", - "-", - "abs", - "rcp", - "rsq", - "sqrt", - "exp", - "log", - "exp2", - "log2", - "f2i", - "i2f", - "f2b", - "b2f", - "i2b", - "b2i", - "u2f", - "trunc", - "ceil", - "floor", - "+", - "-", - "*", - "/", - "%", - "<", - ">", - "<=", - ">=", - "==", - "!=", - "<<", - ">>", - "&", - "^", - "|", - "&&", - "^^", - "||", - "dot", - "min", - "max", - "pow", - }; - printf("(expression "); print_type(ir->type); - assert((unsigned int)ir->operation < - sizeof(operators) / sizeof(operators[0])); - - printf(" %s ", operators[ir->operation]); + printf(" %s ", ir->operator_string()); if (ir->operands[0]) ir->operands[0]->accept(this); -- 2.30.2