Skip to content

Commit 6f02866

Browse files
committed
begin JIT
1 parent aa25c5a commit 6f02866

File tree

3 files changed

+756
-2
lines changed

3 files changed

+756
-2
lines changed

include/rift/jit.hpp

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#pragma once
2+
#ifndef RIFT_JIT_HPP
3+
#define RIFT_JIT_HPP
4+
5+
#include <cstdint>
6+
#include <memory>
7+
#include <span>
8+
#include <string>
9+
#include <string_view>
10+
#include <vector>
11+
12+
#include <Geode/Result.hpp>
13+
14+
#include "value.hpp"
15+
#include "nodes/node.hpp"
16+
17+
namespace rift::jit {
18+
/// @brief Represents the different opcodes used in the custom bytecode format.
19+
enum class Opcode : uint8_t {
20+
NOP, // No operation
21+
RET, // Return the value immediately
22+
23+
LD_NULL, // Load a null constant
24+
LD_TRUE, // Load a true constant
25+
LD_FALSE, // Load a false constant
26+
27+
LD_INT64, // Load a 64-bit integer constant
28+
LD_INT32, // Load a 32-bit integer constant
29+
LD_INT16, // Load a 16-bit integer constant
30+
LD_INT8, // Load an 8-bit integer constant
31+
32+
LD_FLT, // Load a float constant as 64-bit double
33+
LD_FLT_T64, // Load a float constant as 64-bit integer
34+
LD_FLT_T32, // Load a float constant as 32-bit integer
35+
LD_FLT_T16, // Load a float constant as 16-bit integer
36+
LD_FLT_T8, // Load a float constant as 8-bit integer
37+
38+
LD_STR, // Load a string constant
39+
LD_ID, // Load an identifier constant
40+
41+
ADD, // Add two values
42+
SUB, // Subtract two values
43+
MUL, // Multiply two values
44+
DIV, // Divide two values
45+
MOD, // Modulus operation
46+
POW, // Power operation
47+
48+
NOT, // Logical NOT operation
49+
AND, // Logical AND operation
50+
OR, // Logical OR operation
51+
BOOL_CAST, // Cast to boolean
52+
53+
GT, // Greater than operation
54+
GE, // Greater than or equal to operation
55+
LT, // Less than operation
56+
LE, // Less than or equal to operation
57+
EQ, // Equal to operation
58+
NE, // Not equal to operation
59+
60+
JMP, // Skip N bytes (8-bit)
61+
JMP16, // Skip N bytes (16-bit)
62+
JMP32, // Skip N bytes (32-bit)
63+
64+
JNZ, // Jump if the top of the stack is true (8-bit)
65+
JNZ16, // Jump if the top of the stack is true (16-bit)
66+
JNZ32, // Jump if the top of the stack is true (32-bit)
67+
68+
JZ, // Jump if the top of the stack is false (8-bit)
69+
JZ16, // Jump if the top of the stack is false (16-bit)
70+
JZ32, // Jump if the top of the stack is false (32-bit)
71+
72+
JGT, // Jump if greater than (8-bit)
73+
JGT16, // Jump if greater than (16-bit)
74+
JGT32, // Jump if greater than (32-bit)
75+
76+
JGE, // Jump if greater than or equal to (8-bit)
77+
JGE16, // Jump if greater than or equal to (16-bit)
78+
JGE32, // Jump if greater than or equal to (32-bit)
79+
80+
JLT, // Jump if less than (8-bit)
81+
JLT16, // Jump if less than (16-bit)
82+
JLT32, // Jump if less than (32-bit)
83+
84+
JLE, // Jump if less than or equal to (8-bit)
85+
JLE16, // Jump if less than or equal to (16-bit)
86+
JLE32, // Jump if less than or equal to (32-bit)
87+
88+
JEQ, // Jump if equal (8-bit)
89+
JEQ16, // Jump if equal (16-bit)
90+
JEQ32, // Jump if equal (32-bit)
91+
92+
JNE, // Jump if not equal (8-bit)
93+
JNE16, // Jump if not equal (16-bit)
94+
JNE32, // Jump if not equal (32-bit)
95+
96+
CALL, // Call a function with N arguments
97+
CALL_ID, // Call a function with an identifier
98+
99+
PUSH, // Append the value to the output
100+
PUSH_STR, // Append the string to the output
101+
};
102+
103+
/// @brief Convert an Opcode to a string representation.
104+
/// @param op The opcode to convert.
105+
/// @return The string representation of the opcode.
106+
std::string_view opcodeToString(Opcode op) noexcept;
107+
108+
/// @brief Compile a node into bytecode.
109+
/// @param node The node to compile.
110+
/// @param rootCall Used for recursive calls to determine if the node is the root call.
111+
/// @return The compiled bytecode as a vector of bytes.
112+
geode::Result<std::vector<uint8_t>> compile(
113+
std::unique_ptr<Node> const& node,
114+
bool rootCall = true
115+
) noexcept;
116+
117+
/// @brief Run the compiled bytecode with the given variables.
118+
/// @param bytecode The compiled bytecode to run.
119+
/// @param vars The variables to use during execution.
120+
/// @return The result of the execution as a string.
121+
geode::Result<std::string> run(
122+
std::span<uint8_t> bytecode,
123+
Object const& vars = {}
124+
) noexcept;
125+
}
126+
127+
#endif // RIFT_JIT_HPP

0 commit comments

Comments
 (0)