14ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick/*
24ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * Copyright © 2010 Intel Corporation
34ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick *
44ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * Permission is hereby granted, free of charge, to any person obtaining a
54ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * copy of this software and associated documentation files (the "Software"),
64ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * to deal in the Software without restriction, including without limitation
74ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * the rights to use, copy, modify, merge, publish, distribute, sublicense,
84ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * and/or sell copies of the Software, and to permit persons to whom the
94ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * Software is furnished to do so, subject to the following conditions:
104ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick *
114ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * The above copyright notice and this permission notice (including the next
124ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * paragraph) shall be included in all copies or substantial portions of the
134ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * Software.
144ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick *
154ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
164ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
174ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
184ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
194ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
204ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
214ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * DEALINGS IN THE SOFTWARE.
224ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick */
234ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick
244ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick/**
254ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * \file ir_import_prototypes.cpp
264ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * Import function prototypes from one IR tree into another.
274ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick *
284ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * \author Ian Romanick
294ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick */
304ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick#include "ir.h"
314ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick#include "glsl_symbol_table.h"
324ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick
334ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick/**
344ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * Visitor used to import function prototypes
354ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick *
364ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * Normally the \c clone method of either \c ir_function or
374ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * \c ir_function_signature could be used.  However, we don't want a complete
384ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * clone of the \c ir_function_signature.  We want everything \b except the
394ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * body of the function.
404ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick */
414ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanickclass import_prototype_visitor : public ir_hierarchical_visitor {
424ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanickpublic:
434ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick   /**
444ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick    */
454ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick   import_prototype_visitor(exec_list *list, glsl_symbol_table *symbols,
464ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick			    void *mem_ctx)
474ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick   {
484ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick      this->mem_ctx = mem_ctx;
494ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick      this->list = list;
504ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick      this->symbols = symbols;
514ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick      this->function = NULL;
524ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick   }
534ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick
544ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick   virtual ir_visitor_status visit_enter(ir_function *ir)
554ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick   {
564ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick      assert(this->function == NULL);
571bef4c8c4bc11e7f4150500def6e6a4291ceb587Eric Anholt
581bef4c8c4bc11e7f4150500def6e6a4291ceb587Eric Anholt      this->function = this->symbols->get_function(ir->name);
591bef4c8c4bc11e7f4150500def6e6a4291ceb587Eric Anholt      if (!this->function) {
601bef4c8c4bc11e7f4150500def6e6a4291ceb587Eric Anholt	 this->function = new(this->mem_ctx) ir_function(ir->name);
611bef4c8c4bc11e7f4150500def6e6a4291ceb587Eric Anholt
621bef4c8c4bc11e7f4150500def6e6a4291ceb587Eric Anholt	 list->push_tail(this->function);
631bef4c8c4bc11e7f4150500def6e6a4291ceb587Eric Anholt
641bef4c8c4bc11e7f4150500def6e6a4291ceb587Eric Anholt	 /* Add the new function to the symbol table.
651bef4c8c4bc11e7f4150500def6e6a4291ceb587Eric Anholt	  */
66e8f5ebf313da3ce33ccbbcf9b72946853035fbddEric Anholt	 this->symbols->add_function(this->function);
671bef4c8c4bc11e7f4150500def6e6a4291ceb587Eric Anholt      }
684ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick      return visit_continue;
694ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick   }
704ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick
714ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick   virtual ir_visitor_status visit_leave(ir_function *ir)
724ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick   {
734ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick      (void) ir;
744ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick      assert(this->function != NULL);
754ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick
764ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick      this->function = NULL;
774ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick      return visit_continue;
784ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick   }
794ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick
804ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick   ir_visitor_status visit_enter(ir_function_signature *ir)
814ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick   {
824ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick      assert(this->function != NULL);
834ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick
8401a25bb64ecae156729794320f9a39733ff8eeaaKenneth Graunke      ir_function_signature *copy = ir->clone_prototype(mem_ctx, NULL);
854ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick
864ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick      this->function->add_signature(copy);
874ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick
884ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick      /* Do not process child nodes of the ir_function_signature.  There can
894ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick       * never be any nodes inside the ir_function_signature that we care
904ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick       * about.  Instead continue with the next sibling.
914ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick       */
924ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick      return visit_continue_with_parent;
934ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick   }
944ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick
954ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanickprivate:
964ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick   exec_list *list;
974ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick   ir_function *function;
984ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick   glsl_symbol_table *symbols;
994ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick   void *mem_ctx;
1004ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick};
1014ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick
1024ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick
1034ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick/**
1044ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * Import function prototypes from one IR tree into another
1054ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick *
1064ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * \param source   Source instruction stream containing functions whose
1074ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick *                 prototypes are to be imported
1084ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * \param dest     Destination instruction stream where new \c ir_function and
1094ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick *                 \c ir_function_signature nodes will be stored
1104ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick * \param symbols  Symbol table where new functions will be stored
111d3073f58c17d8675a2ecdd5dfa83e5520c78e1a8Kenneth Graunke * \param mem_ctx  ralloc memory context used for new allocations
1124ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick */
1134ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanickvoid
1144ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanickimport_prototypes(const exec_list *source, exec_list *dest,
1154ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick		  glsl_symbol_table *symbols, void *mem_ctx)
1164ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick{
1174ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick   import_prototype_visitor v(dest, symbols, mem_ctx);
1184ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick
1194ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick   /* Making source be const is just extra documentation.
1204ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick    */
1214ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick   v.run(const_cast<exec_list *>(source));
1224ccd3c548b9b9a2fee79342445f68d73525bfcdbIan Romanick}
123