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 * @version 0.5
6 * @date 2020-10-27
7 *
8 * @copyright Copyright (c) 2020-2024
9 *
10 */
11
12#ifndef ARK_COMPILER_BYTECODEREADER_HPP
13#define ARK_COMPILER_BYTECODEREADER_HPP
14
15#include <vector>
16#include <string>
17#include <cinttypes>
18#include <optional>
19
20#include <Ark/Platform.hpp>
22#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,
35 };
36
37 struct Version
38 {
39 uint16_t major;
40 uint16_t minor;
41 uint16_t patch;
42 };
43
44 struct Symbols
45 {
46 std::vector<std::string> symbols {};
47 std::size_t start {}; ///< Point to the SYM_TABLE_START byte in the bytecode
48 std::size_t end {}; ///< Point to the byte following the last byte of the table in the bytecode
49 };
50
51 struct Values
52 {
53 std::vector<Value> values {};
54 std::size_t start {}; ///< Point to the VAL_TABLE_START byte in the bytecode
55 std::size_t end {}; ///< Point to the byte following the last byte of the table in the bytecode
56 };
57
58 struct Code
59 {
60 std::vector<bytecode_t> pages {};
61 std::size_t start {}; ///< Point to the CODE_SEGMENT_START byte in the bytecode
62 };
63
64 /**
65 * @brief This class is just a helper to
66 * - check if a bytecode is valid
67 * - display it in a human readable way by using the opcode names
68 *
69 */
71 {
72 public:
73 /**
74 * @brief Construct a new Bytecode Reader object
75 *
76 */
77 BytecodeReader() = default;
78
79 /**
80 * @brief Construct needed data before displaying information about a given file
81 *
82 * @param file filename of the bytecode file
83 */
84 void feed(const std::string& file);
85
86 /**
87 * @brief Construct needed data before displaying information about a given bytecode
88 *
89 * @param bytecode
90 */
91 void feed(const bytecode_t& bytecode);
92
93 /**
94 * Check for the presence of the magic header
95 * @return true if the magic 'ark\0' was found
96 */
97 [[nodiscard]] bool checkMagic() const;
98
99 /**
100 * @brief Return the bytecode object constructed
101 *
102 * @return const bytecode_t&
103 */
104 [[nodiscard]] const bytecode_t& bytecode() noexcept;
105
106 /**
107 *
108 * @return Version compiler version used to create the given bytecode file
109 */
110 [[nodiscard]] Version version() const;
111
112 /**
113 * @brief Return the read timestamp from the bytecode file
114 *
115 * @return unsigned long long
116 */
117 [[nodiscard]] unsigned long long timestamp() const;
118
119 /**
120 *
121 * @return std::vector<unsigned char> bytecode sha
122 */
123 [[nodiscard]] std::vector<unsigned char> sha256() const;
124
125 /**
126 *
127 * @return Symbols
128 */
129 [[nodiscard]] Symbols symbols() const;
130
131 /**
132 * @param symbols
133 * @return Values
134 */
135 [[nodiscard]] Values values(const Symbols& symbols) const;
136
137 /**
138 * @param values
139 * @return Code
140 */
141 [[nodiscard]] Code code(const Values& values) const;
142
143 /**
144 * @brief Display the bytecode opcode in a human friendly way.
145 *
146 * @param segment selected bytecode segment that will be displayed
147 * @param sStart start of the segment slice to display (Ignored in code segment if no page is available)
148 * @param sEnd end of the segment slice to display (Ignored in code segment if no page is available)
149 * @param cPage selected page of the code segment (Used only for the code segment)
150 */
151 void display(BytecodeSegment segment = BytecodeSegment::All,
152 std::optional<uint16_t> sStart = std::nullopt,
153 std::optional<uint16_t> sEnd = std::nullopt,
154 std::optional<uint16_t> cPage = std::nullopt) const;
155
156 friend class Ark::State;
157
158 private:
159 bytecode_t m_bytecode;
160
161 /**
162 * @brief Read a number from the bytecode, under the instruction pointer i
163 *
164 * @param i this parameter is being modified to point to the next value
165 * @return uint16_t the number we read (big endian)
166 */
167 [[nodiscard]] uint16_t readNumber(std::size_t& i) const;
168 };
169}
170
171#endif
Common code for the compiler.
#define ARK_API
Definition Module.hpp:28
ArkScript configuration macros.
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::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.