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,
80 // operand2 = count
81 LeaveFrame, // Leave function frame (restore locals) - operand1 = start addr,
82 // operand2 = count
83 Halt, // Halt execution
84
85 // ============== Loop Control ==============
86 BeginFor, // Begin for loop
87 EndFor, // End for loop
88
89 // ============== Concurrency - Process ==============
90 Cobegin, // Begin concurrent block
91 Coend, // End concurrent block (operand1 = expected thread count)
92 Create, // Create new process (operand1 = start PC)
93 ThreadEnd, // Terminate current thread
94 Suspend, // Suspend current process
95 Revive, // Revive suspended process
96 WhichProc, // Get current process ID
97
98 // ============== Concurrency - Semaphores ==============
99 Wait, // Wait on semaphore (P operation)
100 Signal, // Signal semaphore (V operation)
101 StoreSemaphore, // Initialize semaphore
102
103 // ============== Concurrency - Monitors ==============
104 EnterMonitor, // Enter monitor
105 ExitMonitor, // Exit monitor
106 CallMonitorInit, // Call monitor initialization
107 ReturnMonitorInit, // Return from monitor init
108 WaitCondition, // Wait on condition variable
109 SignalCondition, // Signal condition variable
110 Empty, // Check if condition queue is empty
111
112 // ============== I/O Operations ==============
113 Read, // Read integer
114 Readln, // Read line
115 Write, // Write value
116 Writeln, // Write newline
117 WriteString, // Write string
118 WriteRawString, // Write raw string literal
119 EolEof, // Check end of line/file
120 Sprintf, // Format string
121 Sscanf, // Scan string
122
123 // ============== String Operations ==============
124 CopyString, // Copy string
125 CopyRawString, // Copy raw string
126 ConcatString, // Concatenate strings
127 ConcatRawString, // Concatenate raw string
128 CompareString, // Compare strings
129 CompareRawString, // Compare raw strings
130 LengthString, // Get string length
131
132 // ============== Graphics/Drawing Operations ==============
133 // Canvas operations
134 DrawClear, // Clear the canvas (optional: r, g, b on stack)
135 DrawRefresh, // Force canvas refresh
136
137 // Color/state operations
138 DrawSetColor, // Set drawing color (r, g, b on stack)
139 DrawSetColorAlpha, // Set drawing color with alpha (r, g, b, a on stack)
140 DrawSetLineWidth, // Set line width (width on stack)
141 DrawSetPosition, // Set current position (x, y on stack)
142
143 // Shape drawing operations (outline only)
144 DrawCircle, // Draw circle outline (x, y, radius on stack)
145 DrawRectangle, // Draw rectangle outline (x, y, width, height on stack)
146 DrawTriangle, // Draw triangle outline (x1, y1, x2, y2, x3, y3 on stack)
147 DrawLine, // Draw line (x1, y1, x2, y2 on stack)
148 DrawEllipse, // Draw ellipse outline (x, y, radiusX, radiusY on stack)
149 DrawPixel, // Draw pixel (x, y on stack)
150 DrawText, // Draw text (x, y, fontSize on stack, string in operand)
151
152 // Shape fill operations (filled shapes)
153 FillCircle, // Fill circle (x, y, radius on stack)
154 FillRectangle, // Fill rectangle (x, y, width, height on stack)
155 FillTriangle, // Fill triangle (x1, y1, x2, y2, x3, y3 on stack)
156 FillEllipse, // Fill ellipse (x, y, radiusX, radiusY on stack)
157
158 // Legacy graphics (keeping for compatibility)
159 MoveTo, // Move to absolute position
160 MoveBy, // Move by relative offset
161 ChangeColor, // Change drawing color
162 MakeVisible, // Make object visible
163 Remove, // Remove object
164
165 // ============== Miscellaneous ==============
166 Random, // Generate random number
167 Test, // Generic test instruction
168
169 // ============== Total count ==============
170 _Count // Number of opcodes (keep last)
171};
172
176using Operand = std::variant<std::monostate, // No operand
177 int32_t, // Integer literal or offset
178 uint32_t, // Unsigned value or address
179 std::string // String literal
180 >;
181
186struct Instruction {
187 Opcode opcode;
188 Operand operand1;
189 Operand operand2;
190
191 // Convenience constructors
192 Instruction() : opcode(Opcode::Halt), operand1(), operand2() {}
193
194 explicit Instruction(Opcode op) : opcode(op), operand1(), operand2() {}
195
196 Instruction(Opcode op, int32_t op1) : opcode(op), operand1(op1), operand2() {}
197
198 Instruction(Opcode op, uint32_t op1)
199 : opcode(op), operand1(op1), operand2() {}
200
201 Instruction(Opcode op, std::string op1)
202 : opcode(op), operand1(std::move(op1)), operand2() {}
203
204 Instruction(Opcode op, int32_t op1, int32_t op2)
205 : opcode(op), operand1(op1), operand2(op2) {}
206
207 Instruction(Opcode op, uint32_t op1, int32_t op2)
208 : opcode(op), operand1(op1), operand2(op2) {}
209};
210
212using InstructionStream = std::vector<Instruction>;
213
219const char* opcodeName(Opcode op);
220
221} // namespace nsbaci::compiler
222
223#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:176
std::vector< Instruction > InstructionStream
Vector of instructions representing a compiled program.
Definition instruction.h:212
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