Nyxstone
Loading...
Searching...
No Matches
nyxstone.h
1#pragma once
2
3#include <expected.hpp>
4
5#include <llvm/MC/MCAsmInfo.h>
6#include <llvm/MC/MCInstPrinter.h>
7#include <llvm/MC/MCInstrInfo.h>
8#include <llvm/MC/MCRegisterInfo.h>
9#include <llvm/MC/MCSubtargetInfo.h>
10#include <llvm/MC/TargetRegistry.h>
11
12namespace nyxstone {
13
14using u8 = uint8_t;
15using u64 = uint64_t;
16
18class Nyxstone {
19public:
23 std::string name;
25 uint64_t address;
26 };
27
29 struct Instruction {
31 uint64_t address;
33 std::string assembly;
35 std::vector<uint8_t> bytes {};
36
37 bool operator==(const Instruction& other) const;
38 };
39
54 Nyxstone(llvm::Triple&& triple, const llvm::Target& target, llvm::MCTargetOptions&& target_options,
55 std::unique_ptr<llvm::MCRegisterInfo>&& register_info, std::unique_ptr<llvm::MCAsmInfo>&& assembler_info,
56 std::unique_ptr<llvm::MCInstrInfo>&& instruction_info, std::unique_ptr<llvm::MCSubtargetInfo>&& subtarget_info,
57 std::unique_ptr<llvm::MCInstPrinter>&& instruction_printer) noexcept
58 : triple(std::move(triple))
59 , target(target)
60 , target_options(std::move(target_options))
61 , register_info(std::move(register_info))
62 , assembler_info(std::move(assembler_info))
63 , instruction_info(std::move(instruction_info))
64 , subtarget_info(std::move(subtarget_info))
65 , instruction_printer(std::move(instruction_printer))
66 {
67 }
68
79 tl::expected<std::vector<u8>, std::string> assemble(
80 const std::string& assembly, uint64_t address, const std::vector<LabelDefinition>& labels) const;
81
92 tl::expected<std::vector<Instruction>, std::string> assemble_to_instructions(
93 const std::string& assembly, uint64_t address, const std::vector<LabelDefinition>& labels) const;
94
102 tl::expected<std::string, std::string> disassemble(
103 const std::vector<uint8_t>& bytes, uint64_t address, size_t count) const;
104
112 tl::expected<std::vector<Instruction>, std::string> disassemble_to_instructions(
113 const std::vector<uint8_t>& bytes, uint64_t address, size_t count) const;
114
115private:
116 // Uses LLVM to assemble instructions.
117 // Utilizes some custom overloads to import user-supplied label definitions and extract instruction details.
118 tl::expected<void, std::string> assemble_impl(const std::string& assembly, uint64_t address,
119 const std::vector<LabelDefinition>& labels, std::vector<uint8_t>& bytes,
120 std::vector<Instruction>* instructions) const;
121
122 // Uses LLVM to disassemble instructions.
123 tl::expected<void, std::string> disassemble_impl(const std::vector<uint8_t>& bytes, uint64_t address, size_t count,
124 std::string* disassembly, std::vector<Instruction>* instructions) const;
125
127 llvm::Triple triple;
128
129 // Target is a static object, thus it is safe to take its const reference.
130 const llvm::Target& target;
131
132 llvm::MCTargetOptions target_options;
133
134 std::unique_ptr<llvm::MCRegisterInfo> register_info;
135 std::unique_ptr<llvm::MCAsmInfo> assembler_info;
136 std::unique_ptr<llvm::MCInstrInfo> instruction_info;
137 std::unique_ptr<llvm::MCSubtargetInfo> subtarget_info;
138 std::unique_ptr<llvm::MCInstPrinter> instruction_printer;
139};
140
145public:
147 // This is a uint8_t for better interoperability with rust.
148 enum class IntegerBase : uint8_t {
150 Dec = 0,
152 HexPrefix = 1,
154 HexSuffix = 2,
155 };
156
169 explicit NyxstoneBuilder(std::string&& triple)
170 : m_triple(std::move(triple)) {};
171 NyxstoneBuilder(const NyxstoneBuilder&) = default;
172 NyxstoneBuilder(NyxstoneBuilder&&) = default;
173 NyxstoneBuilder& operator=(const NyxstoneBuilder&) = default;
174 NyxstoneBuilder& operator=(NyxstoneBuilder&&) = default;
175 ~NyxstoneBuilder() = default;
176
180 NyxstoneBuilder& with_cpu(std::string&& cpu) noexcept;
181
185 NyxstoneBuilder& with_features(std::string&& features) noexcept;
186
191
195 tl::expected<std::unique_ptr<Nyxstone>, std::string> build();
196
197private:
199 std::string m_triple;
201 std::string m_cpu;
203 std::string m_features;
205 IntegerBase m_imm_style = IntegerBase::Dec;
206};
207
209bool is_ArmT16_or_ArmT32(const llvm::Triple& triple);
210} // namespace nyxstone
Builder for Nyxstone instances.
Definition nyxstone.h:144
tl::expected< std::unique_ptr< Nyxstone >, std::string > build()
Builds a nyxstone instance from the builder.
Definition nyxstone.cpp:41
NyxstoneBuilder(std::string &&triple)
Creates a NyxstoneBuilder instance.
Definition nyxstone.h:169
NyxstoneBuilder & with_immediate_style(IntegerBase style) noexcept
Specify the style in which immediates should be represented.
Definition nyxstone.cpp:35
NyxstoneBuilder & with_cpu(std::string &&cpu) noexcept
Specifies the cpu for which to assemble/disassemble in nyxstone.
Definition nyxstone.cpp:23
IntegerBase
Configuration options for the immediate representation in disassembly.
Definition nyxstone.h:148
@ HexSuffix
Immediates are represented in hex format, suffixed with h, for example: 0ffh.
@ HexPrefix
Immediates are represented in hex format, prepended with 0x, for example: 0xff.
@ Dec
Immediates are represented in decimal format.
NyxstoneBuilder & with_features(std::string &&features) noexcept
Specify cpu features to en-/disable in nyxstone.
Definition nyxstone.cpp:29
Nyxstone class for assembling and disassembling for a given architecture.
Definition nyxstone.h:18
tl::expected< std::vector< u8 >, std::string > assemble(const std::string &assembly, uint64_t address, const std::vector< LabelDefinition > &labels) const
Translates assembly instructions at a given start address to bytes.
Definition nyxstone.cpp:124
Nyxstone(llvm::Triple &&triple, const llvm::Target &target, llvm::MCTargetOptions &&target_options, std::unique_ptr< llvm::MCRegisterInfo > &&register_info, std::unique_ptr< llvm::MCAsmInfo > &&assembler_info, std::unique_ptr< llvm::MCInstrInfo > &&instruction_info, std::unique_ptr< llvm::MCSubtargetInfo > &&subtarget_info, std::unique_ptr< llvm::MCInstPrinter > &&instruction_printer) noexcept
Nyxstone constructor called by NyxstoneBuilder::build.
Definition nyxstone.h:54
tl::expected< std::string, std::string > disassemble(const std::vector< uint8_t > &bytes, uint64_t address, size_t count) const
Translates bytes to disassembly text at given start address.
Definition nyxstone.cpp:154
tl::expected< std::vector< Instruction >, std::string > assemble_to_instructions(const std::string &assembly, uint64_t address, const std::vector< LabelDefinition > &labels) const
Translates assembly instructions at given start address to instruction details containing bytes.
Definition nyxstone.cpp:131
tl::expected< std::vector< Instruction >, std::string > disassemble_to_instructions(const std::vector< uint8_t > &bytes, uint64_t address, size_t count) const
Translates bytes to instruction details containing disassembly text at given start address.
Definition nyxstone.cpp:163
Complete instruction information.
Definition nyxstone.h:29
std::string assembly
The assembly string of the instruction.
Definition nyxstone.h:33
uint64_t address
The absolute address of the instruction.
Definition nyxstone.h:31
std::vector< uint8_t > bytes
The byte code of the instruction.
Definition nyxstone.h:35
Defines the location of a label by absolute address.
Definition nyxstone.h:21
uint64_t address
The absolute address of the label.
Definition nyxstone.h:25
std::string name
The label name.
Definition nyxstone.h:23