MXVM 1.8.1
Virtual Machine, Compiler, and Pascal Frontend
Loading...
Searching...
No Matches
instruct.hpp
Go to the documentation of this file.
1
6#ifndef __INSTRUCT_HPP_X_
7#define __INSTRUCT_HPP_X_
8
9#include <cstdint>
10#include <iostream>
11#include <string>
12#include <vector>
13
80
82inline std::vector<std::string> IncType{
83 "NULL", // NULL_INC = 0
84 "mov", // MOV = 1
85 "load", // LOAD = 2
86 "store", // STORE = 3
87 "add", // ADD = 4
88 "sub", // SUB = 5
89 "mul", // MUL = 6
90 "div", // DIV = 7
91 "or", // OR = 8
92 "and", // AND = 9
93 "xor", // XOR = 10
94 "not", // NOT = 11
95 "mod", // MOD = 12
96 "cmp", // CMP = 13
97 "jmp", // JMP = 14
98 "je", // JE = 15
99 "jne", // JNE = 16
100 "jl", // JL = 17
101 "jle", // JLE = 18
102 "jg", // JG = 19
103 "jge", // JGE = 20
104 "jz", // JZ = 21
105 "jnz", // JNZ = 22
106 "ja", // JA = 23
107 "jb", // JB = 24
108 "print", // PRINT = 25
109 "exit", // EXIT = 26
110 "alloc", // ALLOC = 27
111 "free", // FREE = 28
112 "getline", // GETLINE = 29
113 "push", // PUSH = 30
114 "pop", // POP = 31
115 "stack_load", // STACK_LOAD = 32
116 "stack_store", // STACK_STORE = 33
117 "stack_sub", // STACK_SUB = 34
118 "call", // CALL = 35
119 "ret", // RET = 36
120 "string_print", // STRING_PRINT = 37
121 "done", // DONE = 38
122 "to_int", // TO_INT = 39
123 "to_float", // TO_FLOAT = 40
124 "invoke", // INVOKE = 41
125 "return", // RETURN = 42
126 "neg", // NEG = 43
127 "fcmp", // FCMP = 44 (floating-point compare)
128 "jae", // JAE = 45 (jump if above or equal, unsigned)
129 "jbe", // JBE = 46 (jump if below or equal, unsigned)
130 "jc", // JC = 47 (jump if carry)
131 "jnc", // JNC = 48 (jump if no carry)
132 "jp", // JP = 49 (jump if parity)
133 "jnp", // JNP = 50 (jump if no parity)
134 "jo", // JO = 51 (jump if overflow)
135 "jno", // JNO = 52 (jump if no overflow)
136 "js", // JS = 53 (jump if sign)
137 "jns", // JNS = 54 (jump if no sign)
138 "lea", // LEA = 55 (load effective address)
139 "realloc" // REALLOC = 56 (reallocate memory)
140};
141
148std::ostream &operator<<(std::ostream &out, const enum Inc &i);
149
150namespace mxvm {
151
157
159 struct Operand {
160 std::string label;
161 std::string op;
162 int op_value = 0;
164 std::string object;
165 };
166
168 struct Instruction {
171 std::vector<Operand> vop;
172 std::string label = "";
176 std::string toString() const;
177 };
178
191
200
201 extern std::vector<std::string> keywords;
202 // const char *keywords[] = {"program", "object", "module", "data" , "code", "section" };
203
211 std::string str_value;
212 std::string label_value;
213 union {
214 int64_t int_value;
217 };
218 uint64_t ptr_size = 0;
219 uint64_t ptr_count = 0;
221 uint64_t buffer_size;
222 bool owns = false;
223 bool released = false;
224
229 : str_value(other.str_value),
231 ptr_size(other.ptr_size),
232 ptr_count(other.ptr_count),
233 type(other.type),
235 owns(other.owns),
236 released(other.released) {
237 switch (type) {
240 int_value = other.int_value;
241 break;
243 float_value = other.float_value;
244 break;
246 ptr_value = other.ptr_value;
247 break;
248 default:
249 int_value = other.int_value;
250 break;
251 }
252 }
253
259 if (this != &other) {
260 str_value = other.str_value;
261 label_value = other.label_value;
262 ptr_size = other.ptr_size;
263 ptr_count = other.ptr_count;
264 type = other.type;
265 buffer_size = other.buffer_size;
266 owns = other.owns;
267 released = other.released;
268 switch (type) {
271 int_value = other.int_value;
272 break;
274 float_value = other.float_value;
275 break;
277 ptr_value = other.ptr_value;
278 break;
279 default:
280 int_value = other.int_value;
281 break;
282 }
283 }
284 return *this;
285 }
286
289 };
290
292 struct Variable {
294 std::string var_name;
296 bool is_global = false;
297 std::string obj_name;
300
306 Variable(const std::string &name, const VarType &vtype, const Variable_Value &value)
307 : type(vtype), var_name(name), var_value(value), is_global(false) {}
308
314
319 void setExtern(std::string name, void *value) {
322 var_value.ptr_value = value;
323 var_value.ptr_count = 0;
324 var_value.ptr_size = 0;
325 var_value.owns = false;
326 var_name = name;
327 }
328
334 if (this != &v) {
335 type = v.type;
336 var_name = v.var_name;
339 obj_name = v.obj_name;
340 }
341 return *this;
342 }
343
346 std::string toString() const;
347 };
348} // namespace mxvm
349
356std::ostream &operator<<(std::ostream &out, const mxvm::Instruction &inc);
357
364std::ostream &operator<<(std::ostream &out, const mxvm::VarType &type);
365
372std::ostream &operator<<(std::ostream &out, const mxvm::Keywords &key);
373#endif
std::vector< std::string > IncType
String representations of Inc opcodes, indexed by enum value.
Definition instruct.hpp:82
std::ostream & operator<<(std::ostream &out, const enum Inc &i)
Stream insertion operator for Inc opcodes.
Definition instruct.cpp:23
Inc
MXVM instruction set opcodes.
Definition instruct.hpp:21
@ JLE
Definition instruct.hpp:40
@ NOT
Definition instruct.hpp:33
@ POP
Definition instruct.hpp:53
@ JNO
Definition instruct.hpp:74
@ MUL
Definition instruct.hpp:28
@ JNZ
Definition instruct.hpp:44
@ SUB
Definition instruct.hpp:27
@ MOD
Definition instruct.hpp:34
@ ALLOC
Definition instruct.hpp:49
@ JMP
Definition instruct.hpp:36
@ GETLINE
Definition instruct.hpp:51
@ STRING_PRINT
Definition instruct.hpp:59
@ JBE
Definition instruct.hpp:68
@ JE
Definition instruct.hpp:37
@ LEA
Definition instruct.hpp:77
@ RETURN
Definition instruct.hpp:64
@ JNP
Definition instruct.hpp:72
@ JO
Definition instruct.hpp:73
@ JA
Definition instruct.hpp:45
@ JS
Definition instruct.hpp:75
@ STACK_LOAD
Definition instruct.hpp:54
@ EXIT
Definition instruct.hpp:48
@ CMP
Definition instruct.hpp:35
@ DIV
Definition instruct.hpp:29
@ AND
Definition instruct.hpp:31
@ JGE
Definition instruct.hpp:42
@ JL
Definition instruct.hpp:39
@ OR
Definition instruct.hpp:30
@ LOAD
Definition instruct.hpp:24
@ DONE
Definition instruct.hpp:60
@ JZ
Definition instruct.hpp:43
@ STACK_SUB
Definition instruct.hpp:56
@ MOV
Definition instruct.hpp:23
@ JNC
Definition instruct.hpp:70
@ JC
Definition instruct.hpp:69
@ PRINT
Definition instruct.hpp:47
@ RET
Definition instruct.hpp:58
@ JAE
Definition instruct.hpp:67
@ STACK_STORE
Definition instruct.hpp:55
@ CALL
Definition instruct.hpp:57
@ XOR
Definition instruct.hpp:32
@ INVOKE
Definition instruct.hpp:63
@ FREE
Definition instruct.hpp:50
@ TO_INT
Definition instruct.hpp:61
@ ADD
Definition instruct.hpp:26
@ PUSH
Definition instruct.hpp:52
@ JNS
Definition instruct.hpp:76
@ JG
Definition instruct.hpp:41
@ JB
Definition instruct.hpp:46
@ REALLOC
Reallocate a dynamic memory block: realloc dest, elemSize, count.
Definition instruct.hpp:78
@ TO_FLOAT
Definition instruct.hpp:62
@ NULL_INC
Definition instruct.hpp:22
@ JP
Definition instruct.hpp:71
@ JNE
Definition instruct.hpp:38
@ NEG
Definition instruct.hpp:65
@ FCMP
Definition instruct.hpp:66
@ STORE
Definition instruct.hpp:25
Definition ast.hpp:14
Keywords
MXVM language keywords.
Definition instruct.hpp:192
OperandType
Discriminates whether an operand is a compile-time constant or a variable reference.
Definition instruct.hpp:153
VarType
MXVM variable type discriminator.
Definition instruct.hpp:180
std::vector< std::string > keywords
String representations of Keywords enum values.
Definition instruct.cpp:10
A complete MXVM instruction with opcode, operands, and optional label.
Definition instruct.hpp:168
std::string toString() const
Convert this instruction to its assembly text representation.
Definition instruct.cpp:84
std::vector< Operand > vop
variable-length operand list (for print, invoke, etc.)
Definition instruct.hpp:171
Inc instruction
the opcode
Definition instruct.hpp:169
Operand op3
up to three fixed operands
Definition instruct.hpp:170
std::string label
label at this instruction address
Definition instruct.hpp:172
A single instruction operand (constant value or variable reference).
Definition instruct.hpp:159
std::string op
textual operand value
Definition instruct.hpp:161
std::string object
owning object name for member access
Definition instruct.hpp:164
std::string label
label target for jump/call operands
Definition instruct.hpp:160
int op_value
numeric operand value
Definition instruct.hpp:162
OperandType type
Definition instruct.hpp:163
Runtime value storage for a variable.
Definition instruct.hpp:210
uint64_t ptr_size
size of each element in allocated memory
Definition instruct.hpp:218
uint64_t buffer_size
declared buffer size
Definition instruct.hpp:221
bool owns
true if this value owns the allocated memory
Definition instruct.hpp:222
Variable_Value & operator=(const Variable_Value &other)
Copy-assignment operator — self-check then deep copy, dispatching on type.
Definition instruct.hpp:258
std::string label_value
Definition instruct.hpp:212
bool released
true if the owned memory has been freed
Definition instruct.hpp:223
std::string str_value
Definition instruct.hpp:211
Variable_Value(const Variable_Value &other)
Copy constructor — deep-copies all fields, dispatching on type for the union member.
Definition instruct.hpp:228
Variable_Value()
Default constructor — initialises to VAR_NULL with zeroed fields.
Definition instruct.hpp:288
uint64_t ptr_count
number of elements allocated
Definition instruct.hpp:219
std::string var_name
Definition instruct.hpp:294
void setExtern(std::string name, void *value)
Configure this variable as an external pointer reference.
Definition instruct.hpp:319
Variable & operator=(const Variable &v)
Copy-assignment operator.
Definition instruct.hpp:333
Variable_Value var_value
Definition instruct.hpp:295
Variable()
Default constructor — VAR_NULL type with empty name and value.
Definition instruct.hpp:299
Variable(const std::string &name, const VarType &vtype, const Variable_Value &value)
Construct a variable with the given name, type, and initial value.
Definition instruct.hpp:306
Variable(const Variable &v)
Copy constructor.
Definition instruct.hpp:312
std::string obj_name
Definition instruct.hpp:297
std::string toString() const
Convert this variable to a human-readable string.
Definition instruct.cpp:90