Move array of operator strings out of ir_print_visitor.cpp.
authorKenneth Graunke <kenneth@whitecape.org>
Thu, 8 Apr 2010 00:18:29 +0000 (17:18 -0700)
committerIan Romanick <ian.d.romanick@intel.com>
Wed, 28 Apr 2010 22:42:07 +0000 (15:42 -0700)
Also implement a reverse-lookup function for use in the IR reader.

ir.cpp
ir.h
ir_print_visitor.cpp

diff --git a/ir.cpp b/ir.cpp
index e7e5dee00cb8699dee63f5084f77d6609ab3919b..3f4c40d0c996e9e103d440dd6bfa2ca355815493 100644 (file)
--- 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 df64e48823595990e97074d078e61e5c71f26bed..42e8264d41da403a12b1163f28df820b428654e2 100644 (file)
--- 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);
index 56048fe40f208b6fd73bbf67e6f23fa90a937dd9..778a5c16460b86681ac5d946dd7053d34bda4811 100644 (file)
@@ -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);