1
0
Fork 0
mirror of https://git.suyu.dev/suyu/sirit.git synced 2024-12-23 04:32:04 +00:00
sirit/src/sirit.cpp

125 lines
3.2 KiB
C++
Raw Normal View History

2018-08-23 08:59:57 +01:00
/* This file is part of the sirit project.
* Copyright (c) 2018 ReinUsesLisp
* This software may be used and distributed according to the terms of the GNU
2018-08-27 03:28:39 +01:00
* Lesser General Public License version 2.1 or any later version.
2018-08-23 08:59:57 +01:00
*/
2018-08-26 00:16:37 +01:00
#include "sirit/sirit.h"
#include "common_types.h"
2018-08-26 00:34:06 +01:00
#include "op.h"
2018-08-26 00:16:37 +01:00
#include "stream.h"
2018-10-03 04:32:45 +01:00
#include <algorithm>
#include <cassert>
2018-08-26 00:16:37 +01:00
namespace Sirit {
2018-10-03 04:32:45 +01:00
template <typename T>
static void WriteEnum(Stream& stream, spv::Op opcode, T value) {
2018-08-28 08:05:47 +01:00
Op op{opcode};
op.Add(static_cast<u32>(value));
op.Write(stream);
}
2018-10-20 06:52:55 +01:00
template <typename T>
static void WriteSet(Stream& stream, const T& set) {
for (const auto& item : set) {
item->Write(stream);
}
}
2018-08-26 00:16:37 +01:00
Module::Module() {}
Module::~Module() = default;
2018-08-31 07:41:30 +01:00
std::vector<u8> Module::Assemble() const {
2018-08-26 00:16:37 +01:00
std::vector<u8> bytes;
Stream stream{bytes};
stream.Write(spv::MagicNumber);
stream.Write(spv::Version);
2018-10-03 04:32:45 +01:00
stream.Write(GENERATOR_MAGIC_NUMBER);
2018-08-26 00:16:37 +01:00
stream.Write(bound);
stream.Write(static_cast<u32>(0));
for (auto capability : capabilities) {
WriteEnum(stream, spv::Op::OpCapability, capability);
}
// TODO write extensions
// TODO write ext inst imports
2018-08-26 00:34:06 +01:00
Op memory_model_ref{spv::Op::OpMemoryModel};
2018-08-26 00:16:37 +01:00
memory_model_ref.Add(static_cast<u32>(addressing_model));
memory_model_ref.Add(static_cast<u32>(memory_model));
memory_model_ref.Write(stream);
2018-10-20 06:52:55 +01:00
WriteSet(stream, entry_points);
2018-08-26 00:16:37 +01:00
// TODO write execution mode
2018-10-20 06:52:55 +01:00
WriteSet(stream, debug);
2018-08-26 00:16:37 +01:00
// TODO write annotations
2018-10-20 06:52:55 +01:00
WriteSet(stream, declarations);
WriteSet(stream, global_variables);
WriteSet(stream, code);
2018-08-26 00:16:37 +01:00
return bytes;
}
2018-10-03 04:32:45 +01:00
void Module::Optimize(int level) {}
2018-08-26 00:16:37 +01:00
void Module::AddCapability(spv::Capability capability) {
capabilities.insert(capability);
}
2018-10-03 04:32:45 +01:00
void Module::SetMemoryModel(spv::AddressingModel addressing_model,
spv::MemoryModel memory_model) {
2018-08-26 00:16:37 +01:00
this->addressing_model = addressing_model;
this->memory_model = memory_model;
}
2018-08-28 08:01:21 +01:00
void Module::AddEntryPoint(spv::ExecutionModel execution_model, Ref entry_point,
2018-10-03 04:32:45 +01:00
const std::string& name,
const std::vector<Ref>& interfaces) {
auto const op{new Op(spv::Op::OpEntryPoint)};
2018-08-26 00:16:37 +01:00
op->Add(static_cast<u32>(execution_model));
op->Add(entry_point);
op->Add(name);
op->Add(interfaces);
2018-08-26 00:34:06 +01:00
entry_points.push_back(std::unique_ptr<Op>(op));
2018-08-26 00:16:37 +01:00
}
2018-08-28 08:01:21 +01:00
Ref Module::Emit(Ref op) {
2018-08-26 00:34:06 +01:00
assert(op);
code.push_back(op);
return op;
2018-08-26 00:16:37 +01:00
}
2018-10-20 06:52:55 +01:00
Ref Module::AddGlobalVariable(Ref variable) {
assert(variable);
global_variables.push_back(variable);
return variable;
}
2018-08-28 08:01:21 +01:00
Ref Module::AddCode(Op* op) {
2018-08-26 00:34:06 +01:00
code_store.push_back(std::unique_ptr<Op>(op));
return op;
2018-08-26 00:16:37 +01:00
}
2018-10-03 04:32:45 +01:00
Ref Module::AddCode(spv::Op opcode, std::optional<u32> id) {
return AddCode(new Op(opcode, id));
2018-08-26 00:16:37 +01:00
}
2018-08-28 08:01:21 +01:00
Ref Module::AddDeclaration(Op* op) {
2018-10-03 04:32:45 +01:00
const auto& found{
std::find_if(declarations.begin(), declarations.end(),
[&op](const auto& other) { return *other == *op; })};
2018-08-26 00:16:37 +01:00
if (found != declarations.end()) {
2018-08-26 00:34:06 +01:00
delete op;
2018-08-26 00:16:37 +01:00
return found->get();
} else {
2018-08-26 00:34:06 +01:00
declarations.push_back(std::unique_ptr<Op>(op));
2018-08-26 00:16:37 +01:00
bound++;
2018-08-26 00:34:06 +01:00
return op;
2018-08-26 00:16:37 +01:00
}
}
} // namespace Sirit