ArkScript
A small, fast, functional and scripting language for video games
BytecodeReader.hpp
Go to the documentation of this file.
1/**
2 * @file BytecodeReader.hpp
3 * @author Alexandre Plateau ([email protected])
4 * @brief A bytecode disassembler for ArkScript
5 * @date 2020-10-27
6 *
7 * @copyright Copyright (c) 2020-2025
8 *
9 */
10
11#ifndef ARK_COMPILER_BYTECODEREADER_HPP
12#define ARK_COMPILER_BYTECODEREADER_HPP
13
14#include <vector>
15#include <string>
16#include <cinttypes>
17#include <optional>
18
19#include <Ark/Platform.hpp>
21#include <Ark/VM/Value.hpp>
23
24namespace Ark
25{
26 class State;
27
28 enum class BytecodeSegment
29 {
30 All,
31 Symbols,
32 Values,
33 Code,
36 };
37
38 struct Version
39 {
40 uint16_t major;
41 uint16_t minor;
42 uint16_t patch;
43 };
44
45 struct Symbols
46 {
47 std::vector<std::string> symbols {};
48 std::size_t start {}; ///< Point to the SYM_TABLE_START byte in the bytecode
49 std::size_t end {}; ///< Point to the byte following the last byte of the table in the bytecode
50 };
51
52 struct Values
53 {
54 std::vector<Value> values {};
55 std::size_t start {}; ///< Point to the VAL_TABLE_START byte in the bytecode
56 std::size_t end {}; ///< Point to the byte following the last byte of the table in the bytecode
57 };
58
59 struct Filenames
60 {
61 std::vector<std::string> filenames {};
62 std::size_t start {}; ///< Point to the FILENAMES_TABLE_START byte in the bytecode
63 std::size_t end {}; ///< Point to the byte following the last byte of the table in the bytecode
64 };
65
67 {
68 std::vector<internal::InstLoc> locations {};
69 std::size_t start {}; ///< Point to the INST_LOC_TABLE_START byte in the bytecode
70 std::size_t end {}; ///< Point to the byte following the last byte of the table in the bytecode
71 };
72
73 struct Code
74 {
75 std::vector<bytecode_t> pages {};
76 std::size_t start {}; ///< Point to the CODE_SEGMENT_START byte in the bytecode
77 };
78
79 /**
80 * @brief This class is just a helper to
81 * - check if a bytecode is valid
82 * - display it in a human readable way by using the opcode names
83 *
84 */
86 {
87 public:
88 /**
89 * @brief Construct a new Bytecode Reader object
90 *
91 */
92 BytecodeReader() = default;
93
94 /**
95 * @brief Construct needed data before displaying information about a given file
96 *
97 * @param file filename of the bytecode file
98 */
99 void feed(const std::string& file);
100
101 /**
102 * @brief Construct needed data before displaying information about a given bytecode
103 *
104 * @param bytecode
105 */
106 void feed(const bytecode_t& bytecode);
107
108 /**
109 * Check for the presence of the magic header
110 * @return true if the magic 'ark\0' was found
111 */
112 [[nodiscard]] bool checkMagic() const;
113
114 /**
115 *
116 * @return Version compiler version used to create the given bytecode file
117 */
118 [[nodiscard]] Version version() const;
119
120 /**
121 * @brief Return the read timestamp from the bytecode file
122 *
123 * @return unsigned long long
124 */
125 [[nodiscard]] unsigned long long timestamp() const;
126
127 /**
128 *
129 * @return std::vector<unsigned char> bytecode sha
130 */
131 [[nodiscard]] std::vector<unsigned char> sha256() const;
132
133 /**
134 *
135 * @return Symbols
136 */
137 [[nodiscard]] Symbols symbols() const;
138
139 /**
140 * @param symbols
141 * @return Values
142 */
143 [[nodiscard]] Values values(const Symbols& symbols) const;
144
145 /**
146 * @param values
147 * @return Filenames
148 */
149 [[nodiscard]] Filenames filenames(const Values& values) const;
150
151 /**
152 * @param filenames
153 * @return InstLocations
154 */
155 [[nodiscard]] InstLocations instLocations(const Filenames& filenames) const;
156
157 /**
158 * @param instLocations
159 * @return Code
160 */
161 [[nodiscard]] Code code(const InstLocations& instLocations) const;
162
163 /**
164 * @brief Display the bytecode opcode in a human friendly way.
165 *
166 * @param segment selected bytecode segment that will be displayed
167 * @param sStart start of the segment slice to display (Ignored in code segment if no page is available)
168 * @param sEnd end of the segment slice to display (Ignored in code segment if no page is available)
169 * @param cPage selected page of the code segment (Used only for the code segment)
170 */
171 void display(BytecodeSegment segment = BytecodeSegment::All,
172 std::optional<uint16_t> sStart = std::nullopt,
173 std::optional<uint16_t> sEnd = std::nullopt,
174 std::optional<uint16_t> cPage = std::nullopt) const;
175
176 friend class Ark::State;
177
178 private:
180
181 /**
182 * @brief Read a number from the bytecode, under the instruction pointer i
183 *
184 * @param i this parameter is being modified to point to the next value
185 * @return uint16_t the number we read (big endian)
186 */
187 [[nodiscard]] uint16_t readNumber(std::size_t& i) const;
188 };
189}
190
191#endif
Common code for the compiler.
#define ARK_API
Definition Module.hpp:28
ArkScript configuration macros.
Default value type handled by the virtual machine.
This class is just a helper to.
BytecodeReader()=default
Construct a new Bytecode Reader object.
Ark state to handle the dirty job of loading and compiling ArkScript code.
Definition State.hpp:32
std::vector< uint8_t > bytecode_t
Definition Common.hpp:22
std::vector< bytecode_t > pages
std::size_t start
Point to the CODE_SEGMENT_START byte in the bytecode.
std::size_t end
Point to the byte following the last byte of the table in the bytecode.
std::vector< std::string > filenames
std::size_t start
Point to the FILENAMES_TABLE_START byte in the bytecode.
std::size_t end
Point to the byte following the last byte of the table in the bytecode.
std::size_t start
Point to the INST_LOC_TABLE_START byte in the bytecode.
std::vector< internal::InstLoc > locations
std::vector< std::string > symbols
std::size_t end
Point to the byte following the last byte of the table in the bytecode.
std::size_t start
Point to the SYM_TABLE_START byte in the bytecode.
std::vector< Value > values
std::size_t end
Point to the byte following the last byte of the table in the bytecode.
std::size_t start
Point to the VAL_TABLE_START byte in the bytecode.