|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <bit> |
| 4 | +#include <string_view> |
| 5 | + |
| 6 | +#include "openvic-simulation/vm/Chunk.hpp" |
| 7 | +#include "openvic-simulation/vm/Function.hpp" |
| 8 | +#include "openvic-simulation/vm/Handler.hpp" |
| 9 | +#include "openvic-simulation/vm/Module.hpp" |
| 10 | + |
| 11 | +#include <lauf/asm/builder.h> |
| 12 | +#include <lauf/asm/module.h> |
| 13 | + |
| 14 | +namespace OpenVic::Vm { |
| 15 | + struct AsmBuilderRef; |
| 16 | + |
| 17 | + struct CodeBuilder : HandlerRef<lauf_asm_builder> { |
| 18 | + AsmBuilderRef asm_builder(); |
| 19 | + |
| 20 | + ModuleRef module() { |
| 21 | + return _module; |
| 22 | + } |
| 23 | + |
| 24 | + bool is_function() const { |
| 25 | + return _body.function.is_function; |
| 26 | + } |
| 27 | + |
| 28 | + bool is_chunk() const { |
| 29 | + return !_body.chunk.is_function && _body.chunk.value != nullptr; |
| 30 | + } |
| 31 | + |
| 32 | + bool can_build() const { |
| 33 | + return !_body.chunk.is_function && _body.chunk.value == nullptr; |
| 34 | + } |
| 35 | + |
| 36 | + Function function() const { |
| 37 | + assert(is_function()); |
| 38 | + return _body.function.value; |
| 39 | + } |
| 40 | + |
| 41 | + Chunk chunk() const { |
| 42 | + assert(is_chunk()); |
| 43 | + return _body.chunk.value; |
| 44 | + } |
| 45 | + |
| 46 | + void set_for(Function function); |
| 47 | + void set_for(ModuleRef module, Function function); |
| 48 | + void set_for(Chunk chunk); |
| 49 | + void set_for(ModuleRef module, Chunk chunk); |
| 50 | + void set_for(Chunk chunk, lauf_asm_signature signature); |
| 51 | + void set_for(ModuleRef module, Chunk chunk, lauf_asm_signature signature); |
| 52 | + |
| 53 | + Global build_literal(const std::span<unsigned char> data) { |
| 54 | + return lauf_asm_build_data_literal(*this, data.data(), data.size_bytes()); |
| 55 | + } |
| 56 | + |
| 57 | + Global build_string(std::string_view string) { |
| 58 | + return lauf_asm_build_data_literal( |
| 59 | + *this, std::bit_cast<unsigned char*>(string.data()), string.size() * sizeof(char) |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + Global build_cstring(const char* string) { |
| 64 | + return lauf_asm_build_string_literal(*this, string); |
| 65 | + } |
| 66 | + |
| 67 | + bool finish() { |
| 68 | + bool result = lauf_asm_build_finish(*this); |
| 69 | + _body.chunk.is_function = false; |
| 70 | + _body.chunk.value = nullptr; |
| 71 | + return result; |
| 72 | + } |
| 73 | + |
| 74 | + ~CodeBuilder() { |
| 75 | + if (_handle == nullptr || !can_build()) { |
| 76 | + return; |
| 77 | + } |
| 78 | + finish(); |
| 79 | + _handle = nullptr; |
| 80 | + } |
| 81 | + |
| 82 | + protected: |
| 83 | + friend struct AsmBuilderRef; |
| 84 | + CodeBuilder(AsmBuilderRef builder, ModuleRef module, Function function); |
| 85 | + CodeBuilder(AsmBuilderRef builder, ModuleRef module, Chunk chunk, lauf_asm_signature signature); |
| 86 | + |
| 87 | + ModuleRef _module; |
| 88 | + |
| 89 | + union { |
| 90 | + struct { |
| 91 | + bool is_function; |
| 92 | + lauf_asm_function* value; |
| 93 | + } function; |
| 94 | + struct { |
| 95 | + bool is_function; |
| 96 | + lauf_asm_chunk* value; |
| 97 | + } chunk; |
| 98 | + } _body; |
| 99 | + }; |
| 100 | + |
| 101 | + struct AsmBuilderRef : HandlerRef<lauf_asm_builder> { |
| 102 | + using HandlerRef::HandlerRef; |
| 103 | + |
| 104 | + CodeBuilder build(ModuleRef module, Function function) { |
| 105 | + return { *this, module, function }; |
| 106 | + } |
| 107 | + |
| 108 | + CodeBuilder build(ModuleRef module, Chunk chunk, lauf_asm_signature signature) { |
| 109 | + return { *this, module, chunk, signature }; |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + struct AsmBuilder : UniqueHandlerDerived<AsmBuilder, AsmBuilderRef> { |
| 114 | + using UniqueHandlerDerived::UniqueHandlerDerived; |
| 115 | + using UniqueHandlerDerived::operator=; |
| 116 | + |
| 117 | + AsmBuilder(lauf_asm_build_options options = lauf_asm_default_build_options) |
| 118 | + : UniqueHandlerDerived(lauf_asm_create_builder(options)) {} |
| 119 | + |
| 120 | + ~AsmBuilder() { |
| 121 | + if (_handle == nullptr) { |
| 122 | + return; |
| 123 | + } |
| 124 | + lauf_asm_destroy_builder(*this); |
| 125 | + _handle = nullptr; |
| 126 | + } |
| 127 | + }; |
| 128 | +} |
0 commit comments