Path: blob/21.2-virgl/src/compiler/glsl/glsl_symbol_table.cpp
4545 views
/* -*- c++ -*- */1/*2* Copyright © 2010 Intel Corporation3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER21* DEALINGS IN THE SOFTWARE.22*/2324#include "glsl_symbol_table.h"25#include "ast.h"2627class symbol_table_entry {28public:29DECLARE_LINEAR_ALLOC_CXX_OPERATORS(symbol_table_entry);3031bool add_interface(const glsl_type *i, enum ir_variable_mode mode)32{33const glsl_type **dest;3435switch (mode) {36case ir_var_uniform:37dest = &ibu;38break;39case ir_var_shader_storage:40dest = &iss;41break;42case ir_var_shader_in:43dest = &ibi;44break;45case ir_var_shader_out:46dest = &ibo;47break;48default:49assert(!"Unsupported interface variable mode!");50return false;51}5253if (*dest != NULL) {54return false;55} else {56*dest = i;57return true;58}59}6061const glsl_type *get_interface(enum ir_variable_mode mode)62{63switch (mode) {64case ir_var_uniform:65return ibu;66case ir_var_shader_storage:67return iss;68case ir_var_shader_in:69return ibi;70case ir_var_shader_out:71return ibo;72default:73assert(!"Unsupported interface variable mode!");74return NULL;75}76}7778symbol_table_entry(ir_variable *v) :79v(v), f(0), t(0), ibu(0), iss(0), ibi(0), ibo(0), a(0) {}80symbol_table_entry(ir_function *f) :81v(0), f(f), t(0), ibu(0), iss(0), ibi(0), ibo(0), a(0) {}82symbol_table_entry(const glsl_type *t) :83v(0), f(0), t(t), ibu(0), iss(0), ibi(0), ibo(0), a(0) {}84symbol_table_entry(const glsl_type *t, enum ir_variable_mode mode) :85v(0), f(0), t(0), ibu(0), iss(0), ibi(0), ibo(0), a(0)86{87assert(t->is_interface());88add_interface(t, mode);89}90symbol_table_entry(const class ast_type_specifier *a):91v(0), f(0), t(0), ibu(0), iss(0), ibi(0), ibo(0), a(a) {}9293ir_variable *v;94ir_function *f;95const glsl_type *t;96const glsl_type *ibu;97const glsl_type *iss;98const glsl_type *ibi;99const glsl_type *ibo;100const class ast_type_specifier *a;101};102103glsl_symbol_table::glsl_symbol_table()104{105this->separate_function_namespace = false;106this->table = _mesa_symbol_table_ctor();107this->mem_ctx = ralloc_context(NULL);108this->linalloc = linear_alloc_parent(this->mem_ctx, 0);109}110111glsl_symbol_table::~glsl_symbol_table()112{113_mesa_symbol_table_dtor(table);114ralloc_free(mem_ctx);115}116117void glsl_symbol_table::push_scope()118{119_mesa_symbol_table_push_scope(table);120}121122void glsl_symbol_table::pop_scope()123{124_mesa_symbol_table_pop_scope(table);125}126127bool glsl_symbol_table::name_declared_this_scope(const char *name)128{129return _mesa_symbol_table_symbol_scope(table, name) == 0;130}131132bool glsl_symbol_table::add_variable(ir_variable *v)133{134assert(v->data.mode != ir_var_temporary);135136if (this->separate_function_namespace) {137/* In 1.10, functions and variables have separate namespaces. */138symbol_table_entry *existing = get_entry(v->name);139if (name_declared_this_scope(v->name)) {140/* If there's already an existing function (not a constructor!) in141* the current scope, just update the existing entry to include 'v'.142*/143if (existing->v == NULL && existing->t == NULL) {144existing->v = v;145return true;146}147} else {148/* If not declared at this scope, add a new entry. But if an existing149* entry includes a function, propagate that to this block - otherwise150* the new variable declaration would shadow the function.151*/152symbol_table_entry *entry = new(linalloc) symbol_table_entry(v);153if (existing != NULL)154entry->f = existing->f;155int added = _mesa_symbol_table_add_symbol(table, v->name, entry);156assert(added == 0);157(void)added;158return true;159}160return false;161}162163/* 1.20+ rules: */164symbol_table_entry *entry = new(linalloc) symbol_table_entry(v);165return _mesa_symbol_table_add_symbol(table, v->name, entry) == 0;166}167168bool glsl_symbol_table::add_type(const char *name, const glsl_type *t)169{170symbol_table_entry *entry = new(linalloc) symbol_table_entry(t);171return _mesa_symbol_table_add_symbol(table, name, entry) == 0;172}173174bool glsl_symbol_table::add_interface(const char *name, const glsl_type *i,175enum ir_variable_mode mode)176{177assert(i->is_interface());178symbol_table_entry *entry = get_entry(name);179if (entry == NULL) {180symbol_table_entry *entry =181new(linalloc) symbol_table_entry(i, mode);182bool add_interface_symbol_result =183_mesa_symbol_table_add_symbol(table, name, entry) == 0;184assert(add_interface_symbol_result);185return add_interface_symbol_result;186} else {187return entry->add_interface(i, mode);188}189}190191bool glsl_symbol_table::add_function(ir_function *f)192{193if (this->separate_function_namespace && name_declared_this_scope(f->name)) {194/* In 1.10, functions and variables have separate namespaces. */195symbol_table_entry *existing = get_entry(f->name);196if ((existing->f == NULL) && (existing->t == NULL)) {197existing->f = f;198return true;199}200}201symbol_table_entry *entry = new(linalloc) symbol_table_entry(f);202return _mesa_symbol_table_add_symbol(table, f->name, entry) == 0;203}204205bool glsl_symbol_table::add_default_precision_qualifier(const char *type_name,206int precision)207{208char *name = ralloc_asprintf(mem_ctx, "#default_precision_%s", type_name);209210ast_type_specifier *default_specifier = new(linalloc) ast_type_specifier(name);211default_specifier->default_precision = precision;212213symbol_table_entry *entry =214new(linalloc) symbol_table_entry(default_specifier);215216if (!get_entry(name))217return _mesa_symbol_table_add_symbol(table, name, entry) == 0;218219return _mesa_symbol_table_replace_symbol(table, name, entry) == 0;220}221222void glsl_symbol_table::add_global_function(ir_function *f)223{224symbol_table_entry *entry = new(linalloc) symbol_table_entry(f);225int added = _mesa_symbol_table_add_global_symbol(table, f->name, entry);226assert(added == 0);227(void)added;228}229230ir_variable *glsl_symbol_table::get_variable(const char *name)231{232symbol_table_entry *entry = get_entry(name);233return entry != NULL ? entry->v : NULL;234}235236const glsl_type *glsl_symbol_table::get_type(const char *name)237{238symbol_table_entry *entry = get_entry(name);239return entry != NULL ? entry->t : NULL;240}241242const glsl_type *glsl_symbol_table::get_interface(const char *name,243enum ir_variable_mode mode)244{245symbol_table_entry *entry = get_entry(name);246return entry != NULL ? entry->get_interface(mode) : NULL;247}248249ir_function *glsl_symbol_table::get_function(const char *name)250{251symbol_table_entry *entry = get_entry(name);252return entry != NULL ? entry->f : NULL;253}254255int glsl_symbol_table::get_default_precision_qualifier(const char *type_name)256{257char *name = ralloc_asprintf(mem_ctx, "#default_precision_%s", type_name);258symbol_table_entry *entry = get_entry(name);259if (!entry)260return ast_precision_none;261return entry->a->default_precision;262}263264symbol_table_entry *glsl_symbol_table::get_entry(const char *name)265{266return (symbol_table_entry *)267_mesa_symbol_table_find_symbol(table, name);268}269270void271glsl_symbol_table::disable_variable(const char *name)272{273/* Ideally we would remove the variable's entry from the symbol table, but274* that would be difficult. Fortunately, since this is only used for275* built-in variables, it won't be possible for the shader to re-introduce276* the variable later, so all we really need to do is to make sure that277* further attempts to access it using get_variable() will return NULL.278*/279symbol_table_entry *entry = get_entry(name);280if (entry != NULL) {281entry->v = NULL;282}283}284285void286glsl_symbol_table::replace_variable(const char *name,287ir_variable *v)288{289symbol_table_entry *entry = get_entry(name);290if (entry != NULL) {291entry->v = v;292}293}294295296