Move array of operator strings out of ir_print_visitor.cpp.

Also implement a reverse-lookup function for use in the IR reader.
This commit is contained in:
Kenneth Graunke 2010-04-07 17:18:29 -07:00 committed by Ian Romanick
parent bff6013d46
commit 3b96996b7e
3 changed files with 77 additions and 53 deletions

64
ir.cpp
View File

@ -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)
{

14
ir.h
View File

@ -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);

View 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);