nsbaci 1.0
Loading...
Searching...
No Matches
instruction.h
Go to the documentation of this file.
1
12
13#ifndef NSBACI_COMPILER_INSTRUCTION_H
14#define NSBACI_COMPILER_INSTRUCTION_H
15
16#include <cstdint>
17#include <string>
18#include <variant>
19#include <vector>
20
25namespace nsbaci::compiler {
26
31enum class Opcode : uint8_t {
32 // ============== Stack/Memory Operations ==============
33 LoadValue, // Load value from address onto stack
34 LoadAddress, // Load address onto stack
35 LoadIndirect, // Load value from address pointed to by top of stack
36 StoreIndirect, // Store value to address on stack (pops addr, pops value)
37 LoadBlock, // Load block of memory onto stack
38 Store, // Store top of stack to address
39 StoreKeep, // Store and keep value on stack
40 PushLiteral, // Push literal value onto stack
41 Swap, // Swap top two stack elements
42 RotateDown3, // Rotate top 3: [a,b,c] -> [b,c,a]
43 Index, // Array indexing
44 CopyBlock, // Copy block of memory
45 ValueAt, // Get value at address on stack
46 MarkStack, // Mark stack for procedure call
47 UpdateDisplay, // Update display register
48
49 // ============== Arithmetic Operations ==============
50 Add, // Addition
51 Sub, // Subtraction
52 Mult, // Multiplication
53 Div, // Integer division
54 Mod, // Modulo
55 Negate, // Unary negation
56 Complement, // Bitwise complement
57
58 // ============== Logical Operations ==============
59 And, // Logical AND
60 Or, // Logical OR
61
62 // ============== Comparison Operations ==============
63 TestEQ, // Test equal
64 TestNE, // Test not equal
65 TestLT, // Test less than
66 TestLE, // Test less or equal
67 TestGT, // Test greater than
68 TestGE, // Test greater or equal
69 TestEqualKeep, // Test equal, keep operands
70
71 // ============== Control Flow ==============
72 Jump, // Unconditional jump
73 JumpZero, // Jump if top of stack is zero
74 Call, // Call procedure
75 ShortCall, // Short call (no display update)
76 ShortReturn, // Short return
77 ExitProc, // Exit procedure
78 ExitFunction, // Exit function (with return value)
79 EnterFrame, // Enter function frame (save locals) - operand1 = start addr, operand2 = count
80 LeaveFrame, // Leave function frame (restore locals) - operand1 = start addr, operand2 = count
81 Halt, // Halt execution
82
83 // ============== Loop Control ==============
84 BeginFor, // Begin for loop
85 EndFor, // End for loop
86
87 // ============== Concurrency - Process ==============
88 Cobegin, // Begin concurrent block
89 Coend, // End concurrent block (operand1 = expected thread count)
90 Create, // Create new process (operand1 = start PC)
91 ThreadEnd, // Terminate current thread
92 Suspend, // Suspend current process
93 Revive, // Revive suspended process
94 WhichProc, // Get current process ID
95
96 // ============== Concurrency - Semaphores ==============
97 Wait, // Wait on semaphore (P operation)
98 Signal, // Signal semaphore (V operation)
99 StoreSemaphore, // Initialize semaphore
100
101 // ============== Concurrency - Monitors ==============
102 EnterMonitor, // Enter monitor
103 ExitMonitor, // Exit monitor
104 CallMonitorInit, // Call monitor initialization
105 ReturnMonitorInit, // Return from monitor init
106 WaitCondition, // Wait on condition variable
107 SignalCondition, // Signal condition variable
108 Empty, // Check if condition queue is empty
109
110 // ============== I/O Operations ==============
111 Read, // Read integer
112 Readln, // Read line
113 Write, // Write value
114 Writeln, // Write newline
115 WriteString, // Write string
116 WriteRawString, // Write raw string literal
117 EolEof, // Check end of line/file
118 Sprintf, // Format string
119 Sscanf, // Scan string
120
121 // ============== String Operations ==============
122 CopyString, // Copy string
123 CopyRawString, // Copy raw string
124 ConcatString, // Concatenate strings
125 ConcatRawString, // Concatenate raw string
126 CompareString, // Compare strings
127 CompareRawString, // Compare raw strings
128 LengthString, // Get string length
129
130 // ============== Graphics/Drawing Operations ==============
131 // Canvas operations
132 DrawClear, // Clear the canvas (optional: r, g, b on stack)
133 DrawRefresh, // Force canvas refresh
134
135 // Color/state operations
136 DrawSetColor, // Set drawing color (r, g, b on stack)
137 DrawSetColorAlpha, // Set drawing color with alpha (r, g, b, a on stack)
138 DrawSetLineWidth, // Set line width (width on stack)
139 DrawSetPosition, // Set current position (x, y on stack)
140
141 // Shape drawing operations (outline only)
142 DrawCircle, // Draw circle outline (x, y, radius on stack)
143 DrawRectangle, // Draw rectangle outline (x, y, width, height on stack)
144 DrawTriangle, // Draw triangle outline (x1, y1, x2, y2, x3, y3 on stack)
145 DrawLine, // Draw line (x1, y1, x2, y2 on stack)
146 DrawEllipse, // Draw ellipse outline (x, y, radiusX, radiusY on stack)
147 DrawPixel, // Draw pixel (x, y on stack)
148 DrawText, // Draw text (x, y, fontSize on stack, string in operand)
149
150 // Shape fill operations (filled shapes)
151 FillCircle, // Fill circle (x, y, radius on stack)
152 FillRectangle, // Fill rectangle (x, y, width, height on stack)
153 FillTriangle, // Fill triangle (x1, y1, x2, y2, x3, y3 on stack)
154 FillEllipse, // Fill ellipse (x, y, radiusX, radiusY on stack)
155
156 // Legacy graphics (keeping for compatibility)
157 MoveTo, // Move to absolute position
158 MoveBy, // Move by relative offset
159 ChangeColor, // Change drawing color
160 MakeVisible, // Make object visible
161 Remove, // Remove object
162
163 // ============== Miscellaneous ==============
164 Random, // Generate random number
165 Test, // Generic test instruction
166
167 // ============== Total count ==============
168 _Count // Number of opcodes (keep last)
169};
170
174using Operand = std::variant<std::monostate, // No operand
175 int32_t, // Integer literal or offset
176 uint32_t, // Unsigned value or address
177 std::string // String literal
178 >;
179
184struct Instruction {
185 Opcode opcode;
186 Operand operand1;
187 Operand operand2;
188
189 // Convenience constructors
190 Instruction() : opcode(Opcode::Halt), operand1(), operand2() {}
191
192 explicit Instruction(Opcode op) : opcode(op), operand1(), operand2() {}
193
194 Instruction(Opcode op, int32_t op1) : opcode(op), operand1(op1), operand2() {}
195
196 Instruction(Opcode op, uint32_t op1)
197 : opcode(op), operand1(op1), operand2() {}
198
199 Instruction(Opcode op, std::string op1)
200 : opcode(op), operand1(std::move(op1)), operand2() {}
201
202 Instruction(Opcode op, int32_t op1, int32_t op2)
203 : opcode(op), operand1(op1), operand2(op2) {}
204
205 Instruction(Opcode op, uint32_t op1, int32_t op2)
206 : opcode(op), operand1(op1), operand2(op2) {}
207};
208
210using InstructionStream = std::vector<Instruction>;
211
217const char* opcodeName(Opcode op);
218
219} // namespace nsbaci::compiler
220
221#endif // NSBACI_COMPILER_INSTRUCTION_H
Compiler namespace containing all compilation-related stuff.
std::variant< std::monostate, int32_t, uint32_t, std::string > Operand
Operand types that an instruction can have.
Definition instruction.h:174
std::vector< Instruction > InstructionStream
Vector of instructions representing a compiled program.
Definition instruction.h:210
Opcode
Opcodes for the BACI virtual machine instruction set.
Definition instruction.h:31
const char * opcodeName(Opcode op)
Get the string name of an opcode (for debugging/display).
Definition instruction.cpp:14