blob: 386784f0e48089a9746558c2adc4f52a37727832 [file] [log] [blame]
// Copyright 2022 The Tint Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
#include <memory>
#include <string>
#include "src/tint/lang/core/constant/manager.h"
#include "src/tint/lang/core/ir/block.h"
#include "src/tint/lang/core/ir/constant.h"
#include "src/tint/lang/core/ir/function.h"
#include "src/tint/lang/core/ir/instruction.h"
#include "src/tint/lang/core/ir/value.h"
#include "src/tint/lang/core/type/manager.h"
#include "src/tint/utils/containers/vector.h"
#include "src/tint/utils/diagnostic/source.h"
#include "src/tint/utils/id/generation_id.h"
#include "src/tint/utils/memory/block_allocator.h"
#include "src/tint/utils/result/result.h"
#include "src/tint/utils/symbol/symbol_table.h"
namespace tint::core::ir {
/// Main module class for the IR.
class Module {
/// Program Id required to create other components
GenerationID prog_id_;
/// Map of value to name
Hashmap<Value*, Symbol, 32> value_to_name_;
/// Constructor
/// Move constructor
/// @param o the module to move from
Module(Module&& o);
/// Destructor
/// Move assign
/// @param o the module to assign from
/// @returns a reference to this module
Module& operator=(Module&& o);
/// @param inst the instruction
/// @return the name of the given instruction, or an invalid symbol if the instruction is not
/// named. Requires that the instruction only has a single return value.
Symbol NameOf(Instruction* inst);
/// @param value the value
/// @return the name of the given value, or an invalid symbol if the value is not named.
Symbol NameOf(Value* value);
/// @param inst the instruction to set the name of
/// @param name the desired name of the value. May be suffixed on collision.
/// @note requires the instruction be a single result instruction.
void SetName(Instruction* inst, std::string_view name);
/// @param value the value to name.
/// @param name the desired name of the value. May be suffixed on collision.
void SetName(Value* value, std::string_view name);
/// @param value the value to name
/// @param name the desired name of the value
void SetName(Value* value, Symbol name);
/// @return the type manager for the module
core::type::Manager& Types() { return constant_values.types; }
/// The block allocator
BlockAllocator<Block> blocks;
/// The constant value manager
core::constant::Manager constant_values;
/// The instruction allocator
BlockAllocator<Instruction> instructions;
/// The value allocator
BlockAllocator<Value> values;
/// List of functions in the program
Vector<Function*, 8> functions;
/// The block containing module level declarations, if any exist.
Block* root_block = nullptr;
/// The symbol table for the module
SymbolTable symbols{prog_id_};
/// The map of core::constant::Value to their ir::Constant.
Hashmap<const core::constant::Value*, ir::Constant*, 16> constants;
/// If the module generated a validation error, will store the file for the disassembly text.
std::unique_ptr<Source::File> disassembly_file;
} // namespace tint::core::ir