Phasor 3.3.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
PhasorIR.cpp
Go to the documentation of this file.
1#include "PhasorIR.hpp"
2#include "../../ISA/map.hpp"
3#include <cstring>
4#include <fstream>
5#include <iomanip>
6#include <iostream>
7#include <sstream>
8#include <stdexcept>
9#include <utility>
10
11namespace Phasor
12{
13
15{
16 switch (op)
17 {
18 // 0 operands
19 case OpCode::POP:
20 case OpCode::IADD:
23 case OpCode::IDIVIDE:
24 case OpCode::IMODULO:
25 case OpCode::FLADD:
30 case OpCode::SQRT:
31 case OpCode::POW:
32 case OpCode::LOG:
33 case OpCode::EXP:
34 case OpCode::SIN:
35 case OpCode::COS:
36 case OpCode::TAN:
37 case OpCode::NEGATE:
38 case OpCode::NOT:
39 case OpCode::IAND:
40 case OpCode::IOR:
41 case OpCode::IEQUAL:
47 case OpCode::FLEQUAL:
53 case OpCode::PRINT:
56 case OpCode::HALT:
57 case OpCode::RETURN:
58 case OpCode::TRUE_P:
59 case OpCode::FALSE_P:
61 case OpCode::LEN:
62 case OpCode::CHAR_AT:
63 case OpCode::SUBSTR:
64 return 0;
65
66 // 1 operand
68 case OpCode::JUMP:
74 case OpCode::IMPORT:
76 case OpCode::CALL:
77 case OpCode::SYSTEM:
80 case OpCode::PUSH_R:
81 case OpCode::POP_R:
82 case OpCode::PRINT_R:
92 return 1;
93
94 // 2 operands
95 case OpCode::MOV:
99 case OpCode::SQRT_R:
100 case OpCode::LOG_R:
101 case OpCode::EXP_R:
102 case OpCode::SIN_R:
103 case OpCode::COS_R:
104 case OpCode::TAN_R:
105 case OpCode::NEG_R:
106 case OpCode::NOT_R:
107 case OpCode::PUSH2_R:
108 case OpCode::POP2_R:
111 return 2;
112
113 // 3 operands
114 case OpCode::IADD_R:
115 case OpCode::ISUB_R:
116 case OpCode::IMUL_R:
117 case OpCode::IDIV_R:
118 case OpCode::IMOD_R:
119 case OpCode::FLADD_R:
120 case OpCode::FLSUB_R:
121 case OpCode::FLMUL_R:
122 case OpCode::FLDIV_R:
123 case OpCode::FLMOD_R:
124 case OpCode::POW_R:
125 case OpCode::IAND_R:
126 case OpCode::IOR_R:
127 case OpCode::IEQ_R:
128 case OpCode::INE_R:
129 case OpCode::ILT_R:
130 case OpCode::IGT_R:
131 case OpCode::ILE_R:
132 case OpCode::IGE_R:
133 case OpCode::FLAND_R:
134 case OpCode::FLOR_R:
135 case OpCode::FLEQ_R:
136 case OpCode::FLNE_R:
137 case OpCode::FLLT_R:
138 case OpCode::FLGT_R:
139 case OpCode::FLLE_R:
140 case OpCode::FLGE_R:
141 return 3;
142
143 default:
144 return 0;
145 }
146}
147
149{
150 // Stack operations with special indices
151 if (op == OpCode::PUSH_CONST && operandIndex == 0)
152 {
154 }
155 if (op == OpCode::STORE_VAR && operandIndex == 0)
156 {
158 }
159 if (op == OpCode::LOAD_VAR && operandIndex == 0)
160 {
162 }
163 if (op == OpCode::IMPORT && operandIndex == 0)
164 {
166 }
167 if (op == OpCode::CALL_NATIVE && operandIndex == 0)
168 {
170 }
171 if (op == OpCode::CALL && operandIndex == 0)
172 {
174 }
175 if (op == OpCode::SYSTEM && operandIndex == 0)
176 {
178 }
179
180 // Register operations with mixed types
181 if (op == OpCode::LOAD_CONST_R)
182 {
183 if (operandIndex == 0)
184 {
186 }
187 if (operandIndex == 1)
188 {
190 }
191 }
192 if (op == OpCode::LOAD_VAR_R)
193 {
194 if (operandIndex == 0)
195 {
197 }
198 if (operandIndex == 1)
199 {
201 }
202 }
203 if (op == OpCode::STORE_VAR_R)
204 {
205 if (operandIndex == 0)
206 {
208 }
209 if (operandIndex == 1)
210 {
212 }
213 }
214
215 // JUMP instructions take an offset (INT)
217 {
218 return OperandType::INT;
219 }
220
221 // Register ops use REGISTER for all operands
222 if (static_cast<int>(op) >= static_cast<int>(OpCode::MOV))
223 {
225 }
226
227 return OperandType::INT;
228}
229
230std::string PhasorIR::escapeString(const std::string &str)
231{
232 std::stringstream ss;
233 for (char c : str)
234 {
235 switch (c)
236 {
237 case '\n':
238 ss << "\\n";
239 break;
240 case '\r':
241 ss << "\\r";
242 break;
243 case '\t':
244 ss << "\\t";
245 break;
246 case '\\':
247 ss << "\\\\";
248 break;
249 case '"':
250 ss << "\\\"";
251 break;
252 default:
253 ss << c;
254 break;
255 }
256 }
257 return ss.str();
258}
259
260std::string PhasorIR::unescapeString(const std::string &str)
261{
262 std::string result;
263 for (size_t i = 0; i < str.length(); ++i)
264 {
265 if (str[i] == '\\' && i + 1 < str.length())
266 {
267 switch (str[i + 1])
268 {
269 case 'n':
270 result += '\n';
271 break;
272 case 'r':
273 result += '\r';
274 break;
275 case 't':
276 result += '\t';
277 break;
278 case '\\':
279 result += '\\';
280 break;
281 case '"':
282 result += '"';
283 break;
284 default:
285 result += str[i];
286 result += str[i + 1];
287 break;
288 }
289 i++;
290 }
291 else
292 {
293 result += str[i];
294 }
295 }
296 return result;
297}
298
299std::vector<uint8_t> PhasorIR::serialize(const Bytecode &bytecode)
300{
301 std::stringstream ss;
302
303 // Write Header
304 ss << ".PHIR 3.0.0" << "\n";
305
306 // Build reverse lookup maps for inline comments
307 std::map<int, std::string> indexToVarName;
308 for (const auto &[name, index] : bytecode.variables)
309 {
310 indexToVarName[index] = name;
311 }
312 std::map<int, std::string> addressToFuncName;
313 for (const auto &[name, address] : bytecode.functionEntries)
314 {
315 addressToFuncName[address] = name;
316 }
317
318 // Constants Section
319 ss << ".CONSTANTS " << bytecode.constants.size() << "\n";
320 for (const auto &val : bytecode.constants)
321 {
322 switch (val.getType())
323 {
324 case ValueType::Null:
325 ss << "NULL\n";
326 break;
327 case ValueType::Bool:
328 ss << "BOOL " << (val.asBool() ? "true" : "false") << "\n";
329 break;
330 case ValueType::Int:
331 ss << "INT " << val.asInt() << "\n";
332 break;
333 case ValueType::Float:
334 ss << "FLOAT " << val.asFloat() << "\n";
335 break;
337 ss << "STRING \"" << escapeString(val.asString()) << "\"\n";
338 break;
340#warning "Warning: PHS_01 Structs have not been fully implemented!"
341 throw std::runtime_error("Structs not implemented!");
342 break;
343 case ValueType::Array:
344#warning "Warning: PHS_02 Arrays have not been implemented!"
345 throw std::runtime_error("Arrays not implemented!");
346 }
347 }
348
349 // Variables Section
350 ss << ".VARIABLES " << bytecode.variables.size() << " " << bytecode.nextVarIndex << "\n";
351 for (const auto &[name, index] : bytecode.variables)
352 {
353 ss << name << " " << index << "\n";
354 }
355
356 // Functions Section
357 ss << ".FUNCTIONS " << bytecode.functionEntries.size() << "\n";
358 for (const auto &[name, address] : bytecode.functionEntries)
359 {
360 ss << name << " " << address << "\n";
361 }
362
363 // Structs Section
364 ss << ".STRUCTS " << bytecode.structs.size() << "\n";
365 for (const auto &info : bytecode.structs)
366 {
367 // name firstConstIndex fieldCount fieldName0 fieldName1 ...
368 ss << info.name << " " << info.firstConstIndex << " " << info.fieldCount;
369 for (const auto &fieldName : info.fieldNames)
370 {
371 ss << " " << fieldName;
372 }
373 ss << "\n";
374 }
375
376 // Instructions Section
377 ss << ".INSTRUCTIONS " << bytecode.instructions.size() << "\n";
378 for (const auto &instr : bytecode.instructions)
379 {
380 std::stringstream instrLine;
381 instrLine << opCodeToString(instr.op);
382
383 int operandCount = getOperandCount(instr.op);
384 int32_t operands[3] = {instr.operand1, instr.operand2, instr.operand3};
385
386 std::string comment;
387
388 for (int i = 0; i < operandCount; ++i)
389 {
390 OperandType type = getOperandType(instr.op, i);
391
392 if (i > 0)
393 {
394 instrLine << ",";
395 }
396 instrLine << " ";
397
398 switch (type)
399 {
401 instrLine << "r" << operands[i];
402 break;
404 instrLine << operands[i];
405 if (operands[i] >= 0 && std::cmp_less(operands[i], bytecode.constants.size()))
406 {
407 const Value &val = bytecode.constants[operands[i]];
408 if (val.getType() == ValueType::String)
409 {
410 std::string str = val.asString();
411 if (str.length() > 20)
412 {
413 str = str.substr(0, 20) + "...";
414 }
415 comment = "const[" + std::to_string(operands[i]) + "]=\"" + escapeString(str) + "\"";
416 }
417 else if (val.getType() == ValueType::Int)
418 {
419 comment = "const[" + std::to_string(operands[i]) + "]=" + std::to_string(val.asInt());
420 }
421 else if (val.getType() == ValueType::Float)
422 {
423 comment = "const[" + std::to_string(operands[i]) + "]=" + std::to_string(val.asFloat());
424 }
425 }
426 break;
428 instrLine << operands[i];
429 if (indexToVarName.contains(operands[i]) != 0u)
430 {
431 comment = "var=" + indexToVarName[operands[i]];
432 }
433 break;
435 instrLine << operands[i];
436 if (addressToFuncName.contains(operands[i]) != 0u)
437 {
438 comment = "func=" + addressToFuncName[operands[i]];
439 }
440 break;
441 default:
442 instrLine << operands[i];
443 break;
444 }
445 }
446
447 std::string lineStr = instrLine.str();
448 if (!comment.empty())
449 {
450 const size_t commentColumn = 40;
451 if (lineStr.length() < commentColumn)
452 {
453 lineStr.append(commentColumn - lineStr.length(), ' ');
454 }
455 else
456 {
457 lineStr += " ";
458 }
459
460 lineStr += "; " + comment;
461 }
462 ss << lineStr << "\n";
463 }
464
465 std::string textData = ss.str();
466 std::vector<uint8_t> buffer;
467
468 // Append text data
469 buffer.insert(buffer.end(), textData.begin(), textData.end());
470
471 return buffer;
472}
473
474Bytecode PhasorIR::deserialize(const std::vector<uint8_t> &data)
475{
476 if (data.size() < 8)
477 {
478 throw std::runtime_error("Invalid Phasor IR file: too small");
479 }
480
481 // Parse text data
482 std::string textData(data.begin() + 8, data.end());
483 std::stringstream ss(textData);
484 std::string line;
485 Bytecode bytecode;
486
487 std::string section;
488 while (ss >> section)
489 {
490 if (section == ".PHIR")
491 {
492 std::string version;
493 ss >> version;
494 if (version < "3.0.0")
495 {
496 throw std::runtime_error("Incompatible Phasor IR version");
497 }
498 }
499 if (section == ".CONSTANTS")
500 {
501 int count;
502 ss >> count;
503 bytecode.constants.reserve(count);
504 for (int i = 0; i < count; ++i)
505 {
506 std::string type;
507 ss >> type;
508 if (type == "NULL")
509 {
510 bytecode.constants.emplace_back();
511 }
512 else if (type == "BOOL")
513 {
514 std::string valStr;
515 ss >> valStr;
516 bytecode.constants.emplace_back(valStr == "true");
517 }
518 else if (type == "INT")
519 {
520 int64_t val;
521 ss >> val;
522 bytecode.constants.emplace_back(val);
523 }
524 else if (type == "FLOAT")
525 {
526 double val;
527 ss >> val;
528 bytecode.constants.emplace_back(val);
529 }
530 else if (type == "STRING")
531 {
532 std::string valStr;
533 // Read quoted string, potentially with spaces
534 char c;
535 while (ss.get(c) && c != '"')
536 {
537 ; // Skip until opening quote
538 }
539 std::getline(ss, valStr, '"'); // Read until closing quote
540 bytecode.constants.emplace_back(unescapeString(valStr));
541 }
542 }
543 }
544 else if (section == ".VARIABLES")
545 {
546 int count;
547 ss >> count >> bytecode.nextVarIndex;
548 for (int i = 0; i < count; ++i)
549 {
550 std::string name;
551 int index;
552 ss >> name >> index;
553 bytecode.variables[name] = index;
554 }
555 }
556 else if (section == ".FUNCTIONS")
557 {
558 int count;
559 ss >> count;
560 for (int i = 0; i < count; ++i)
561 {
562 std::string name;
563 int address;
564 ss >> name >> address;
565 bytecode.functionEntries[name] = address;
566 }
567 }
568 else if (section == ".STRUCTS")
569 {
570 int count;
571 ss >> count;
572 for (int i = 0; i < count; ++i)
573 {
574 StructInfo info;
575 ss >> info.name >> info.firstConstIndex >> info.fieldCount;
576 info.fieldNames.clear();
577 for (int f = 0; f < info.fieldCount; ++f)
578 {
579 std::string fieldName;
580 ss >> fieldName;
581 info.fieldNames.push_back(fieldName);
582 }
583 int index = static_cast<int>(bytecode.structs.size());
584 bytecode.structs.push_back(std::move(info));
585 bytecode.structEntries[bytecode.structs.back().name] = index;
586 }
587 }
588 else if (section == ".INSTRUCTIONS")
589 {
590 int count;
591 ss >> count;
592 bytecode.instructions.reserve(count);
593 for (int i = 0; i < count; ++i)
594 {
595 std::string opStr;
596 ss >> opStr;
597
598 OpCode op = stringToOpCode(opStr);
599 int operandCount = getOperandCount(op);
600 int32_t operands[3] = {0, 0, 0};
601
602 for (int j = 0; j < operandCount; ++j)
603 {
604 std::string token;
605 ss >> token;
606
607 // Skip if we hit a comment
608 if (token.empty() || token[0] == ';')
609 {
610 // Skip rest of line
611 std::getline(ss, token);
612 break;
613 }
614
615 // Strip trailing comma if present
616 if (!token.empty() && token.back() == ',')
617 {
618 token.pop_back();
619 }
620
621 // Parse register format "rN" or plain integer
622 if (!token.empty() && token[0] == 'r')
623 {
624 operands[j] = std::stoi(token.substr(1));
625 }
626 else
627 {
628 operands[j] = std::stoi(token);
629 }
630 }
631
632 // Skip any remaining content on the line (comments)
633 char c;
634 while (ss.get(c) && c != '\n')
635 {
636 ;
637 }
638
639 bytecode.instructions.emplace_back(op, operands[0], operands[1], operands[2]);
640 }
641 }
642 }
643
644 return bytecode;
645}
646
647bool PhasorIR::saveToFile(const Bytecode &bytecode, const std::filesystem::path &filename)
648{
649 try
650 {
651 std::vector<uint8_t> data = serialize(bytecode);
652 std::ofstream file(filename, std::ios::binary);
653 if (!file.is_open())
654 {
655 return false;
656 }
657 file.write(reinterpret_cast<const char *>(data.data()), (std::streamsize)data.size());
658 return true;
659 }
660 catch (...)
661 {
662 return false;
663 }
664}
665
666Bytecode PhasorIR::loadFromFile(const std::filesystem::path &filename)
667{
668 std::ifstream file(filename, std::ios::binary | std::ios::ate);
669 if (!file.is_open())
670 {
671 throw std::runtime_error("Cannot open file");
672 }
673 std::streamsize size = file.tellg();
674 file.seekg(0, std::ios::beg);
675 std::vector<uint8_t> buffer(size);
676 if (!file.read(reinterpret_cast<char *>(buffer.data()), size))
677 {
678 throw std::runtime_error("Cannot read file");
679 }
680 return deserialize(buffer);
681}
682} // namespace Phasor
static std::vector< uint8_t > serialize(const Bytecode &bytecode)
Serialize bytecode to Phasor IR format.
Definition PhasorIR.cpp:299
static std::string unescapeString(const std::string &str)
Helper to unescape strings from text format.
Definition PhasorIR.cpp:260
static Bytecode deserialize(const std::vector< uint8_t > &data)
Deserialize Phasor IR format to bytecode.
Definition PhasorIR.cpp:474
OperandType
Operand types for instructions.
Definition PhasorIR.hpp:41
@ VARIABLE_IDX
Index into variable mapping.
Definition PhasorIR.hpp:46
@ CONSTANT_IDX
Index into constant pool.
Definition PhasorIR.hpp:45
@ FUNCTION_IDX
Index into function entries.
Definition PhasorIR.hpp:47
@ REGISTER
Register operand.
Definition PhasorIR.hpp:44
static OperandType getOperandType(OpCode op, int operandIndex)
Definition PhasorIR.cpp:148
static Bytecode loadFromFile(const std::filesystem::path &filename)
Load bytecode from .phir file.
Definition PhasorIR.cpp:666
static int getOperandCount(OpCode op)
Definition PhasorIR.cpp:14
static std::string escapeString(const std::string &str)
Helper to escape strings for text format.
Definition PhasorIR.cpp:230
static bool saveToFile(const Bytecode &bytecode, const std::filesystem::path &filename)
Save bytecode to .phir file.
Definition PhasorIR.cpp:647
A value in the Phasor VM.
Definition Value.hpp:58
ValueType getType() const noexcept
Get the type of the value.
Definition Value.hpp:113
int64_t asInt() const noexcept
Get the value as an integer.
Definition Value.hpp:159
double asFloat() const noexcept
Get the value as a double.
Definition Value.hpp:172
std::string asString() const noexcept
Get the value as a string.
Definition Value.hpp:185
The Phasor Programming Language and Runtime.
Definition AST.hpp:12
OpCode
Definition ISA.hpp:12
@ IGREATER_THAN
Pop b, pop a, push a > b.
Definition ISA.hpp:50
@ IEQUAL
Pop b, pop a, push a == b.
Definition ISA.hpp:47
@ SYSTEM_OUT
Call system function and push stdout.
Definition ISA.hpp:79
@ LOG_R
R[rA] = log(R[rB]).
Definition ISA.hpp:125
@ FLMOD_R
R[rA] = R[rB] % R[rC].
Definition ISA.hpp:122
@ SUBSTR
Pop len, pop start, pop s, push s.substr(start, len).
Definition ISA.hpp:91
@ IAND
Pop b, pop a, push a && b.
Definition ISA.hpp:41
@ NOT
Pop a, push !a.
Definition ISA.hpp:38
@ SET_FIELD_STATIC
Pop value and struct instance, set field by static offset.
Definition ISA.hpp:99
@ NULL_VAL
Push null.
Definition ISA.hpp:86
@ MOV
Copy register to register: R[rA] = R[rB].
Definition ISA.hpp:103
@ POW
pow()
Definition ISA.hpp:29
@ PRINTERROR_R
Print register to stderr: printerror(R[rA]).
Definition ISA.hpp:155
@ IAND_R
R[rA] = R[rB] && R[rC].
Definition ISA.hpp:132
@ FLMUL_R
R[rA] = R[rB] * R[rC].
Definition ISA.hpp:120
@ IADD
Pop b, pop a, push a + b.
Definition ISA.hpp:18
@ PUSH2_R
Push 2 registers to stack: push2(R[rA], R[rB]).
Definition ISA.hpp:108
@ FLGE_R
R[rA] = R[rB] >= R[rC].
Definition ISA.hpp:147
@ SYSTEM_R
Run an operating system shell command: system(R[rA]).
Definition ISA.hpp:157
@ PUSH_CONST
Push constant from constant pool.
Definition ISA.hpp:14
@ JUMP_IF_TRUE
Jump if top of stack is true (pops value).
Definition ISA.hpp:63
@ FLGT_R
R[rA] = R[rB] > R[rC].
Definition ISA.hpp:145
@ PUSH_R
Push register to stack: push(R[rA]).
Definition ISA.hpp:107
@ POP_R
Pop stack to register: R[rA] = pop().
Definition ISA.hpp:109
@ FLEQUAL
Pop b, pop a, push a == b.
Definition ISA.hpp:53
@ GET_FIELD_STATIC
Pop struct instance, push field by static offset (structIndex, fieldOffset).
Definition ISA.hpp:98
@ FLNOT_EQUAL
Pop b, pop a, push a != b.
Definition ISA.hpp:54
@ FLADD_R
R[rA] = R[rB] + R[rC].
Definition ISA.hpp:118
@ POP2_R
Pop 2 values from stack to registers: pop2(R[rA], R[rB]).
Definition ISA.hpp:110
@ LOAD_CONST_R
Load constant to register: R[rA] = constants[immediate].
Definition ISA.hpp:104
@ SQRT
sqrt()
Definition ISA.hpp:28
@ FLADD
Pop b, pop a, push a + b.
Definition ISA.hpp:23
@ JUMP
Unconditional jump to offset.
Definition ISA.hpp:61
@ FLLT_R
R[rA] = R[rB] < R[rC].
Definition ISA.hpp:144
@ SET_FIELD
Pop struct, pop field name, pop value, set field value.
Definition ISA.hpp:95
@ NOT_R
R[rA] = !R[rB].
Definition ISA.hpp:151
@ IMULTIPLY
Pop b, pop a, push a * b.
Definition ISA.hpp:20
@ READLINE_R
Read line into register: readline(R[rA]).
Definition ISA.hpp:156
@ LOG
log()
Definition ISA.hpp:30
@ CHAR_AT
Pop index, pop s, push s[index].
Definition ISA.hpp:90
@ IMUL_R
R[rA] = R[rB] * R[rC].
Definition ISA.hpp:115
@ FLGREATER_EQUAL
Pop b, pop a, push a >= b.
Definition ISA.hpp:58
@ FLLE_R
R[rA] = R[rB] <= R[rC].
Definition ISA.hpp:146
@ IGREATER_EQUAL
Pop b, pop a, push a >= b.
Definition ISA.hpp:52
@ SIN
sin()
Definition ISA.hpp:32
@ SYSTEM_ERR
Call system function and push stderr.
Definition ISA.hpp:80
@ SQRT_R
R[rA] = sqrt(R[rB]).
Definition ISA.hpp:123
@ ISUB_R
R[rA] = R[rB] - R[rC].
Definition ISA.hpp:114
@ FLDIV_R
R[rA] = R[rB] / R[rC].
Definition ISA.hpp:121
@ COS_R
R[rA] = cos(R[rB]).
Definition ISA.hpp:128
@ CALL_NATIVE
Call a native function: operand is index of function name in constants.
Definition ISA.hpp:76
@ ISUBTRACT
Pop b, pop a, push a - b.
Definition ISA.hpp:19
@ LEN
Pop s, push len(s).
Definition ISA.hpp:89
@ TAN
tan()
Definition ISA.hpp:34
@ FLMODULO
Pop b, pop a, push a % b.
Definition ISA.hpp:27
@ FALSE_P
Push false.
Definition ISA.hpp:85
@ NEW_STRUCT_INSTANCE_STATIC
Create new struct instance using struct section metadata (structIndex).
Definition ISA.hpp:97
@ NEG_R
R[rA] = -R[rB].
Definition ISA.hpp:150
@ IDIVIDE
Pop b, pop a, push a / b.
Definition ISA.hpp:21
@ STORE_VAR
Pop top of stack, store in variable slot.
Definition ISA.hpp:67
@ ILE_R
R[rA] = R[rB] <= R[rC].
Definition ISA.hpp:138
@ FLDIVIDE
Pop b, pop a, push a / b.
Definition ISA.hpp:26
@ POW_R
R[rA] = pow(R[rB], R[rC]).
Definition ISA.hpp:124
@ EXP
exp()
Definition ISA.hpp:31
@ IMODULO
Pop b, pop a, push a % b.
Definition ISA.hpp:22
@ FLAND_R
R[rA] = R[rB] && R[rC].
Definition ISA.hpp:140
@ JUMP_IF_FALSE
Jump if top of stack is false (pops value).
Definition ISA.hpp:62
@ INOT_EQUAL
Pop b, pop a, push a != b.
Definition ISA.hpp:48
@ FLEQ_R
R[rA] = R[rB] == R[rC].
Definition ISA.hpp:142
@ LOAD_VAR
Push variable value onto stack.
Definition ISA.hpp:68
@ IOR
Pop b, pop a, push a || b.
Definition ISA.hpp:42
@ EXP_R
R[rA] = exp(R[rB]).
Definition ISA.hpp:126
@ RETURN
Return from function.
Definition ISA.hpp:81
@ STORE_VAR_R
Store register to varible: variables[immediate] = R[rA].
Definition ISA.hpp:106
@ READLINE
Read line from input and push onto stack.
Definition ISA.hpp:73
@ IGT_R
R[rA] = R[rB] > R[rC].
Definition ISA.hpp:137
@ FLSUBTRACT
Pop b, pop a, push a - b.
Definition ISA.hpp:24
@ IADD_R
R[rA] = R[rB] + R[rC].
Definition ISA.hpp:113
@ IDIV_R
R[rA] = R[rB] / R[rC].
Definition ISA.hpp:116
@ FLLESS_EQUAL
Pop b, pop a, push a <= b.
Definition ISA.hpp:57
@ SYSTEM_ERR_R
Run shell command and get output: system_out(R[rA], R[rB]).
Definition ISA.hpp:159
@ FLMULTIPLY
Pop b, pop a, push a * b.
Definition ISA.hpp:25
@ ILESS_EQUAL
Pop b, pop a, push a <= b.
Definition ISA.hpp:51
@ HALT
Stop execution.
Definition ISA.hpp:75
@ PRINT_R
Print register: print(R[rA]).
Definition ISA.hpp:154
@ PRINTERROR
Pop top of stack and print to stderr.
Definition ISA.hpp:72
@ TAN_R
R[rA] = tan(R[rB]).
Definition ISA.hpp:129
@ FLSUB_R
R[rA] = R[rB] - R[rC].
Definition ISA.hpp:119
@ JUMP_BACK
Jump backwards (for loops).
Definition ISA.hpp:64
@ FLOR_R
R[rA] = R[rB] || R[rC].
Definition ISA.hpp:141
@ CALL
Call a user function: operand is index of function name in constants.
Definition ISA.hpp:77
@ INE_R
R[rA] = R[rB] != R[rC].
Definition ISA.hpp:135
@ NEGATE
Pop a, push -a.
Definition ISA.hpp:37
@ FLNE_R
R[rA] = R[rB] != R[rC].
Definition ISA.hpp:143
@ FLGREATER_THAN
Pop b, pop a, push a > b.
Definition ISA.hpp:56
@ ILESS_THAN
Pop b, pop a, push a < b.
Definition ISA.hpp:49
@ GET_FIELD
Pop struct, pop field name, push field value.
Definition ISA.hpp:94
@ SIN_R
R[rA] = sin(R[rB]).
Definition ISA.hpp:127
@ IMPORT
Import a module: operand is index of module path in constants.
Definition ISA.hpp:74
@ LOAD_VAR_R
Load variable to register: R[rA] = variables[immediate].
Definition ISA.hpp:105
@ FLLESS_THAN
Pop b, pop a, push a < b.
Definition ISA.hpp:55
@ NEW_STRUCT
Create new struct: operand is index of struct name in constants.
Definition ISA.hpp:93
@ IOR_R
R[rA] = R[rB] || R[rC].
Definition ISA.hpp:133
@ ILT_R
R[rA] = R[rB] < R[rC].
Definition ISA.hpp:136
@ TRUE_P
Push true.
Definition ISA.hpp:84
@ COS
cos()
Definition ISA.hpp:33
@ POP
Pop top of stack.
Definition ISA.hpp:15
@ IMOD_R
R[rA] = R[rB] % R[rC].
Definition ISA.hpp:117
@ PRINT
Pop top of stack and print.
Definition ISA.hpp:71
@ SYSTEM
Call a system function: operand is index of function name in constants.
Definition ISA.hpp:78
@ IEQ_R
R[rA] = R[rB] == R[rC].
Definition ISA.hpp:134
@ IGE_R
R[rA] = R[rB] >= R[rC].
Definition ISA.hpp:139
OpCode stringToOpCode(const std::string &str)
Definition map.cpp:135
std::string opCodeToString(OpCode op)
Definition map.cpp:125
Complete bytecode structure.
Definition CodeGen.hpp:50
std::unordered_map< std::string, int > variables
Variable name -> index mapping.
Definition CodeGen.hpp:53
std::vector< Instruction > instructions
List of instructions.
Definition CodeGen.hpp:51
std::unordered_map< std::string, int > functionEntries
Function name -> instruction index mapping.
Definition CodeGen.hpp:54
int nextVarIndex
Next available variable index.
Definition CodeGen.hpp:56
std::vector< Value > constants
Constant pool.
Definition CodeGen.hpp:52
std::vector< StructInfo > structs
List of struct descriptors.
Definition CodeGen.hpp:59
std::unordered_map< std::string, int > structEntries
Struct name -> index in structs.
Definition CodeGen.hpp:60
Struct metadata stored alongside bytecode (struct section).
Definition CodeGen.hpp:41
int firstConstIndex
Index into constants for the first default value.
Definition CodeGen.hpp:43
std::vector< std::string > fieldNames
Field names in declaration order.
Definition CodeGen.hpp:45
int fieldCount
Number of fields in this struct.
Definition CodeGen.hpp:44
std::string name
Struct name.
Definition CodeGen.hpp:42