Add global variables

This commit is contained in:
ReinUsesLisp 2018-10-20 02:52:55 -03:00
parent a3022e4969
commit 54cc7d06ce
3 changed files with 28 additions and 18 deletions

View File

@ -62,6 +62,13 @@ class Module {
*/ */
Ref Emit(Ref op); Ref Emit(Ref op);
/**
* Adds a global variable
* @param variable Global variable to add.
* @return Returns variable.
*/
Ref AddGlobalVariable(Ref variable);
// Types // Types
/// Returns type void. /// Returns type void.
@ -233,6 +240,8 @@ class Module {
std::vector<std::unique_ptr<Op>> declarations; std::vector<std::unique_ptr<Op>> declarations;
std::vector<Ref> global_variables;
std::vector<Ref> code; std::vector<Ref> code;
std::vector<std::unique_ptr<Op>> code_store; std::vector<std::unique_ptr<Op>> code_store;

View File

@ -6,6 +6,7 @@
#include <cassert> #include <cassert>
#include <optional> #include <optional>
#include "sirit/sirit.h" #include "sirit/sirit.h"
#include "insts.h" #include "insts.h"

View File

@ -20,6 +20,13 @@ static void WriteEnum(Stream& stream, spv::Op opcode, T value) {
op.Write(stream); op.Write(stream);
} }
template <typename T>
static void WriteSet(Stream& stream, const T& set) {
for (const auto& item : set) {
item->Write(stream);
}
}
Module::Module() {} Module::Module() {}
Module::~Module() = default; Module::~Module() = default;
@ -37,9 +44,7 @@ std::vector<u8> Module::Assemble() const {
for (auto capability : capabilities) { for (auto capability : capabilities) {
WriteEnum(stream, spv::Op::OpCapability, capability); WriteEnum(stream, spv::Op::OpCapability, capability);
} }
// TODO write extensions // TODO write extensions
// TODO write ext inst imports // TODO write ext inst imports
Op memory_model_ref{spv::Op::OpMemoryModel}; Op memory_model_ref{spv::Op::OpMemoryModel};
@ -47,24 +52,13 @@ std::vector<u8> Module::Assemble() const {
memory_model_ref.Add(static_cast<u32>(memory_model)); memory_model_ref.Add(static_cast<u32>(memory_model));
memory_model_ref.Write(stream); memory_model_ref.Write(stream);
for (const auto& entry_point : entry_points) { WriteSet(stream, entry_points);
entry_point->Write(stream);
}
// TODO write execution mode // TODO write execution mode
WriteSet(stream, debug);
for (const auto& debug_symbol : debug) {
debug_symbol->Write(stream);
}
// TODO write annotations // TODO write annotations
WriteSet(stream, declarations);
for (const auto& decl : declarations) { WriteSet(stream, global_variables);
decl->Write(stream); WriteSet(stream, code);
}
for (const auto& line : code) {
line->Write(stream);
}
return bytes; return bytes;
} }
@ -98,6 +92,12 @@ Ref Module::Emit(Ref op) {
return op; return op;
} }
Ref Module::AddGlobalVariable(Ref variable) {
assert(variable);
global_variables.push_back(variable);
return variable;
}
Ref Module::AddCode(Op* op) { Ref Module::AddCode(Op* op) {
code_store.push_back(std::unique_ptr<Op>(op)); code_store.push_back(std::unique_ptr<Op>(op));
return op; return op;