11591693c7b415e9869157c711fe11263c95d74eDavid Li/*
21591693c7b415e9869157c711fe11263c95d74eDavid Li * Copyright © 2010 Intel Corporation
31591693c7b415e9869157c711fe11263c95d74eDavid Li *
41591693c7b415e9869157c711fe11263c95d74eDavid Li * Permission is hereby granted, free of charge, to any person obtaining a
51591693c7b415e9869157c711fe11263c95d74eDavid Li * copy of this software and associated documentation files (the "Software"),
61591693c7b415e9869157c711fe11263c95d74eDavid Li * to deal in the Software without restriction, including without limitation
71591693c7b415e9869157c711fe11263c95d74eDavid Li * the rights to use, copy, modify, merge, publish, distribute, sublicense,
81591693c7b415e9869157c711fe11263c95d74eDavid Li * and/or sell copies of the Software, and to permit persons to whom the
91591693c7b415e9869157c711fe11263c95d74eDavid Li * Software is furnished to do so, subject to the following conditions:
101591693c7b415e9869157c711fe11263c95d74eDavid Li *
111591693c7b415e9869157c711fe11263c95d74eDavid Li * The above copyright notice and this permission notice (including the next
121591693c7b415e9869157c711fe11263c95d74eDavid Li * paragraph) shall be included in all copies or substantial portions of the
131591693c7b415e9869157c711fe11263c95d74eDavid Li * Software.
141591693c7b415e9869157c711fe11263c95d74eDavid Li *
151591693c7b415e9869157c711fe11263c95d74eDavid Li * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161591693c7b415e9869157c711fe11263c95d74eDavid Li * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171591693c7b415e9869157c711fe11263c95d74eDavid Li * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
181591693c7b415e9869157c711fe11263c95d74eDavid Li * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
191591693c7b415e9869157c711fe11263c95d74eDavid Li * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
201591693c7b415e9869157c711fe11263c95d74eDavid Li * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
211591693c7b415e9869157c711fe11263c95d74eDavid Li * DEALINGS IN THE SOFTWARE.
221591693c7b415e9869157c711fe11263c95d74eDavid Li */
231591693c7b415e9869157c711fe11263c95d74eDavid Li
241591693c7b415e9869157c711fe11263c95d74eDavid Li/**
251591693c7b415e9869157c711fe11263c95d74eDavid Li * \file ir_import_prototypes.cpp
261591693c7b415e9869157c711fe11263c95d74eDavid Li * Import function prototypes from one IR tree into another.
271591693c7b415e9869157c711fe11263c95d74eDavid Li *
281591693c7b415e9869157c711fe11263c95d74eDavid Li * \author Ian Romanick
291591693c7b415e9869157c711fe11263c95d74eDavid Li */
301591693c7b415e9869157c711fe11263c95d74eDavid Li#include <cstdio>
311591693c7b415e9869157c711fe11263c95d74eDavid Li#include "ir.h"
321591693c7b415e9869157c711fe11263c95d74eDavid Li#include "glsl_symbol_table.h"
331591693c7b415e9869157c711fe11263c95d74eDavid Li
341591693c7b415e9869157c711fe11263c95d74eDavid Li/**
351591693c7b415e9869157c711fe11263c95d74eDavid Li * Visitor used to import function prototypes
361591693c7b415e9869157c711fe11263c95d74eDavid Li *
371591693c7b415e9869157c711fe11263c95d74eDavid Li * Normally the \c clone method of either \c ir_function or
381591693c7b415e9869157c711fe11263c95d74eDavid Li * \c ir_function_signature could be used.  However, we don't want a complete
391591693c7b415e9869157c711fe11263c95d74eDavid Li * clone of the \c ir_function_signature.  We want everything \b except the
401591693c7b415e9869157c711fe11263c95d74eDavid Li * body of the function.
411591693c7b415e9869157c711fe11263c95d74eDavid Li */
421591693c7b415e9869157c711fe11263c95d74eDavid Liclass import_prototype_visitor : public ir_hierarchical_visitor {
431591693c7b415e9869157c711fe11263c95d74eDavid Lipublic:
441591693c7b415e9869157c711fe11263c95d74eDavid Li   /**
451591693c7b415e9869157c711fe11263c95d74eDavid Li    */
461591693c7b415e9869157c711fe11263c95d74eDavid Li   import_prototype_visitor(exec_list *list, glsl_symbol_table *symbols,
471591693c7b415e9869157c711fe11263c95d74eDavid Li			    void *mem_ctx)
481591693c7b415e9869157c711fe11263c95d74eDavid Li   {
491591693c7b415e9869157c711fe11263c95d74eDavid Li      this->mem_ctx = mem_ctx;
501591693c7b415e9869157c711fe11263c95d74eDavid Li      this->list = list;
511591693c7b415e9869157c711fe11263c95d74eDavid Li      this->symbols = symbols;
521591693c7b415e9869157c711fe11263c95d74eDavid Li      this->function = NULL;
531591693c7b415e9869157c711fe11263c95d74eDavid Li   }
541591693c7b415e9869157c711fe11263c95d74eDavid Li
551591693c7b415e9869157c711fe11263c95d74eDavid Li   virtual ir_visitor_status visit_enter(ir_function *ir)
561591693c7b415e9869157c711fe11263c95d74eDavid Li   {
571591693c7b415e9869157c711fe11263c95d74eDavid Li      assert(this->function == NULL);
581591693c7b415e9869157c711fe11263c95d74eDavid Li
591591693c7b415e9869157c711fe11263c95d74eDavid Li      this->function = this->symbols->get_function(ir->name);
601591693c7b415e9869157c711fe11263c95d74eDavid Li      if (!this->function) {
611591693c7b415e9869157c711fe11263c95d74eDavid Li	 this->function = new(this->mem_ctx) ir_function(ir->name);
621591693c7b415e9869157c711fe11263c95d74eDavid Li
631591693c7b415e9869157c711fe11263c95d74eDavid Li	 list->push_tail(this->function);
641591693c7b415e9869157c711fe11263c95d74eDavid Li
651591693c7b415e9869157c711fe11263c95d74eDavid Li	 /* Add the new function to the symbol table.
661591693c7b415e9869157c711fe11263c95d74eDavid Li	  */
671591693c7b415e9869157c711fe11263c95d74eDavid Li	 this->symbols->add_function(this->function);
681591693c7b415e9869157c711fe11263c95d74eDavid Li      }
691591693c7b415e9869157c711fe11263c95d74eDavid Li      return visit_continue;
701591693c7b415e9869157c711fe11263c95d74eDavid Li   }
711591693c7b415e9869157c711fe11263c95d74eDavid Li
721591693c7b415e9869157c711fe11263c95d74eDavid Li   virtual ir_visitor_status visit_leave(ir_function *ir)
731591693c7b415e9869157c711fe11263c95d74eDavid Li   {
741591693c7b415e9869157c711fe11263c95d74eDavid Li      (void) ir;
751591693c7b415e9869157c711fe11263c95d74eDavid Li      assert(this->function != NULL);
761591693c7b415e9869157c711fe11263c95d74eDavid Li
771591693c7b415e9869157c711fe11263c95d74eDavid Li      this->function = NULL;
781591693c7b415e9869157c711fe11263c95d74eDavid Li      return visit_continue;
791591693c7b415e9869157c711fe11263c95d74eDavid Li   }
801591693c7b415e9869157c711fe11263c95d74eDavid Li
811591693c7b415e9869157c711fe11263c95d74eDavid Li   ir_visitor_status visit_enter(ir_function_signature *ir)
821591693c7b415e9869157c711fe11263c95d74eDavid Li   {
831591693c7b415e9869157c711fe11263c95d74eDavid Li      assert(this->function != NULL);
841591693c7b415e9869157c711fe11263c95d74eDavid Li
851591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_function_signature *copy = ir->clone_prototype(mem_ctx, NULL);
861591693c7b415e9869157c711fe11263c95d74eDavid Li
871591693c7b415e9869157c711fe11263c95d74eDavid Li      this->function->add_signature(copy);
881591693c7b415e9869157c711fe11263c95d74eDavid Li
891591693c7b415e9869157c711fe11263c95d74eDavid Li      /* Do not process child nodes of the ir_function_signature.  There can
901591693c7b415e9869157c711fe11263c95d74eDavid Li       * never be any nodes inside the ir_function_signature that we care
911591693c7b415e9869157c711fe11263c95d74eDavid Li       * about.  Instead continue with the next sibling.
921591693c7b415e9869157c711fe11263c95d74eDavid Li       */
931591693c7b415e9869157c711fe11263c95d74eDavid Li      return visit_continue_with_parent;
941591693c7b415e9869157c711fe11263c95d74eDavid Li   }
951591693c7b415e9869157c711fe11263c95d74eDavid Li
961591693c7b415e9869157c711fe11263c95d74eDavid Liprivate:
971591693c7b415e9869157c711fe11263c95d74eDavid Li   exec_list *list;
981591693c7b415e9869157c711fe11263c95d74eDavid Li   ir_function *function;
991591693c7b415e9869157c711fe11263c95d74eDavid Li   glsl_symbol_table *symbols;
1001591693c7b415e9869157c711fe11263c95d74eDavid Li   void *mem_ctx;
1011591693c7b415e9869157c711fe11263c95d74eDavid Li};
1021591693c7b415e9869157c711fe11263c95d74eDavid Li
1031591693c7b415e9869157c711fe11263c95d74eDavid Li
1041591693c7b415e9869157c711fe11263c95d74eDavid Li/**
1051591693c7b415e9869157c711fe11263c95d74eDavid Li * Import function prototypes from one IR tree into another
1061591693c7b415e9869157c711fe11263c95d74eDavid Li *
1071591693c7b415e9869157c711fe11263c95d74eDavid Li * \param source   Source instruction stream containing functions whose
1081591693c7b415e9869157c711fe11263c95d74eDavid Li *                 prototypes are to be imported
1091591693c7b415e9869157c711fe11263c95d74eDavid Li * \param dest     Destination instruction stream where new \c ir_function and
1101591693c7b415e9869157c711fe11263c95d74eDavid Li *                 \c ir_function_signature nodes will be stored
1111591693c7b415e9869157c711fe11263c95d74eDavid Li * \param symbols  Symbol table where new functions will be stored
112d50d9a90a0df4d706421850e17c0fbd85bf710eeDavid Li * \param mem_ctx  hieralloc memory context used for new allocations
1131591693c7b415e9869157c711fe11263c95d74eDavid Li */
1141591693c7b415e9869157c711fe11263c95d74eDavid Livoid
1151591693c7b415e9869157c711fe11263c95d74eDavid Liimport_prototypes(const exec_list *source, exec_list *dest,
1161591693c7b415e9869157c711fe11263c95d74eDavid Li		  glsl_symbol_table *symbols, void *mem_ctx)
1171591693c7b415e9869157c711fe11263c95d74eDavid Li{
1181591693c7b415e9869157c711fe11263c95d74eDavid Li   import_prototype_visitor v(dest, symbols, mem_ctx);
1191591693c7b415e9869157c711fe11263c95d74eDavid Li
1201591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Making source be const is just extra documentation.
1211591693c7b415e9869157c711fe11263c95d74eDavid Li    */
1221591693c7b415e9869157c711fe11263c95d74eDavid Li   v.run(const_cast<exec_list *>(source));
1231591693c7b415e9869157c711fe11263c95d74eDavid Li}
124