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 e7e5dee..3f4c40d 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 df64e48..42e8264 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 56048fe..778a5c1 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);