From 78b51b0fdd61b58940f9043ef9046217552f2c70 Mon Sep 17 00:00:00 2001 From: Ian Romanick Date: Tue, 9 Mar 2010 16:23:37 -0800 Subject: [PATCH] IR visitor: Add initial version of ir_visitor classes The ir_visitor class is the abstract base class for all visitors. ir_print_visitor contains the beginnings of a concrete visitor class that will print out an IR sequence in a Lisp / Scheme-like syntax. --- Makefile.am | 3 +- ir.h | 43 +++++++++++++++++++++ ir_print_visitor.cpp | 89 ++++++++++++++++++++++++++++++++++++++++++++ ir_print_visitor.h | 65 ++++++++++++++++++++++++++++++++ ir_visitor.h | 57 ++++++++++++++++++++++++++++ 5 files changed, 256 insertions(+), 1 deletion(-) create mode 100644 ir_print_visitor.cpp create mode 100644 ir_print_visitor.h create mode 100644 ir_visitor.h diff --git a/Makefile.am b/Makefile.am index 77b401357a2..60317f8b4d3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -25,7 +25,8 @@ AUTOMAKE_OPTIONS = foreign bin_PROGRAMS = glsl glsl_SOURCES = symbol_table.c hash_table.c glsl_types.cpp \ glsl_parser.ypp glsl_lexer.lpp glsl_parser_extras.cpp \ - ast_expr.cpp ast_to_hir.cpp ir.cpp hir_field_selection.cpp + ast_expr.cpp ast_to_hir.cpp ir.cpp hir_field_selection.cpp \ + ir_print_visitor.cpp BUILT_SOURCES = glsl_parser.h builtin_types.h diff --git a/ir.h b/ir.h index b728631d785..8d59d21dcb3 100644 --- a/ir.h +++ b/ir.h @@ -22,6 +22,7 @@ */ #include "list.h" +#include "ir_visitor.h" struct ir_program { void *bong_hits; @@ -48,6 +49,8 @@ public: unsigned mode; const struct glsl_type *type; + virtual void accept(ir_visitor *) = 0; + protected: ir_instruction(int mode); @@ -81,6 +84,11 @@ class ir_variable : public ir_instruction { public: ir_variable(const struct glsl_type *, const char *); + virtual void accept(ir_visitor *v) + { + v->visit(this); + } + const char *name; unsigned read_only:1; @@ -96,6 +104,11 @@ class ir_label : public ir_instruction { public: ir_label(const char *label); + virtual void accept(ir_visitor *v) + { + v->visit(this); + } + const char *label; }; @@ -105,6 +118,11 @@ class ir_function_signature : public ir_instruction { public: ir_function_signature(void); + virtual void accept(ir_visitor *v) + { + v->visit(this); + } + /** * Function return type. * @@ -131,6 +149,11 @@ class ir_function : public ir_instruction { public: ir_function(void); + virtual void accept(ir_visitor *v) + { + v->visit(this); + } + /** * Name of the function. */ @@ -148,6 +171,11 @@ public: ir_assignment(ir_instruction *lhs, ir_instruction *rhs, ir_expression *condition); + virtual void accept(ir_visitor *v) + { + v->visit(this); + } + /** * Left-hand side of the assignment. */ @@ -234,6 +262,11 @@ public: ir_expression(int op, const struct glsl_type *type, ir_instruction *, ir_instruction *); + virtual void accept(ir_visitor *v) + { + v->visit(this); + } + ir_expression_operation operation; ir_instruction *operands[2]; }; @@ -262,6 +295,11 @@ class ir_dereference : public ir_instruction { public: ir_dereference(struct ir_instruction *); + virtual void accept(ir_visitor *v) + { + v->visit(this); + } + enum { ir_reference_variable, ir_reference_array, @@ -287,6 +325,11 @@ class ir_constant : public ir_instruction { public: ir_constant(const struct glsl_type *type, const void *data); + virtual void accept(ir_visitor *v) + { + v->visit(this); + } + /** * Value of the constant. * diff --git a/ir_print_visitor.cpp b/ir_print_visitor.cpp new file mode 100644 index 00000000000..8c4271e466a --- /dev/null +++ b/ir_print_visitor.cpp @@ -0,0 +1,89 @@ +/* + * Copyright © 2010 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ +#include +#include "ir_print_visitor.h" + +void ir_print_visitor::visit(ir_variable *ir) +{ + printf("(declare \n"); + + const char *const cent = (ir->centroid) ? "centroid " : ""; + const char *const inv = (ir->invariant) ? "invariant " : ""; + const char *const mode[] = { "", "uniform ", "in ", "out ", "inout " }; + const char *const interp[] = { "", "flat", "noperspective" }; + + printf(" (%s%s%s%s)\n", + cent, inv, mode[ir->mode], interp[ir->interpolation]); + + printf(" (FINISHME: type goes here)\n"); + printf(" (%s)\n", ir->name); + printf(")\n"); +} + + +void ir_print_visitor::visit(ir_label *ir) +{ + printf("(label %s)\n", ir->label); +} + + +void ir_print_visitor::visit(ir_function_signature *ir) +{ + printf("%s:%d:\n", __func__, __LINE__); + (void) ir; +} + + +void ir_print_visitor::visit(ir_function *ir) +{ + printf("(function %s\n", ir->name); + printf(")\n"); +} + + +void ir_print_visitor::visit(ir_expression *ir) +{ + printf("%s:%d:\n", __func__, __LINE__); + (void) ir; +} + + +void ir_print_visitor::visit(ir_dereference *ir) +{ + printf("%s:%d:\n", __func__, __LINE__); + (void) ir; +} + + +void ir_print_visitor::visit(ir_assignment *ir) +{ + printf("%s:%d:\n", __func__, __LINE__); + (void) ir; +} + + +void ir_print_visitor::visit(ir_constant *ir) +{ + printf("%s:%d:\n", __func__, __LINE__); + (void) ir; +} diff --git a/ir_print_visitor.h b/ir_print_visitor.h new file mode 100644 index 00000000000..a2004e43b2f --- /dev/null +++ b/ir_print_visitor.h @@ -0,0 +1,65 @@ +/* + * Copyright © 2010 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#pragma once +#ifndef IR_PRINT_VISITOR_H +#define IR_PRINT_VISITOR_H + +#include "ir.h" +#include "ir_visitor.h" + +/** + * Abstract base class of visitors of IR instruction trees + */ +class ir_print_visitor : public ir_visitor { +public: + ir_print_visitor() + { + /* empty */ + } + + virtual ~ir_print_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_label *); + virtual void visit(ir_function_signature *); + virtual void visit(ir_function *); + virtual void visit(ir_expression *); + virtual void visit(ir_dereference *); + virtual void visit(ir_assignment *); + virtual void visit(ir_constant *); + /*@}*/ +}; + +#endif /* IR_PRINT_VISITOR_H */ diff --git a/ir_visitor.h b/ir_visitor.h new file mode 100644 index 00000000000..4494074eca4 --- /dev/null +++ b/ir_visitor.h @@ -0,0 +1,57 @@ +/* + * Copyright © 2010 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#pragma once +#ifndef IR_VISITOR_H +#define IR_VISITOR_H + +/** + * Abstract base class of visitors of IR instruction trees + */ +class ir_visitor { +public: + virtual ~ir_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(class ir_variable *) = 0; + virtual void visit(class ir_label *) = 0; + virtual void visit(class ir_function_signature *) = 0; + virtual void visit(class ir_function *) = 0; + virtual void visit(class ir_expression *) = 0; + virtual void visit(class ir_dereference *) = 0; + virtual void visit(class ir_assignment *) = 0; + virtual void visit(class ir_constant *) = 0; + /*@}*/ +}; + +#endif /* IR_VISITOR_H */ -- 2.30.2