aboutsummaryrefslogtreecommitdiff
path: root/vendor/cmilan/src/codegen.cpp
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 06:49:14 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 06:49:14 +0000
commit0e856ca15d8aa1499edc33c7302d4d97be404290 (patch)
treed2bb840d13c57aa17f643e6039af370255e38379 /vendor/cmilan/src/codegen.cpp
downloadlab-04-0e856ca15d8aa1499edc33c7302d4d97be404290.tar.gz
publishHEADmaster
Diffstat (limited to 'vendor/cmilan/src/codegen.cpp')
-rw-r--r--vendor/cmilan/src/codegen.cpp153
1 files changed, 153 insertions, 0 deletions
diff --git a/vendor/cmilan/src/codegen.cpp b/vendor/cmilan/src/codegen.cpp
new file mode 100644
index 0000000..4f11a97
--- /dev/null
+++ b/vendor/cmilan/src/codegen.cpp
@@ -0,0 +1,153 @@
1#include "codegen.h"
2
3void Command::print(int address, ostream& os)
4{
5 os << address << ":\t";
6 switch(instruction_) {
7 case NOP:
8 os << "NOP";
9 break;
10
11 case STOP:
12 os << "STOP";
13 break;
14
15 case LOAD:
16 os << "LOAD\t" << arg_;
17 break;
18
19 case STORE:
20 os << "STORE\t" << arg_;
21 break;
22
23 case BLOAD:
24 os << "BLOAD\t" << arg_;
25 break;
26
27 case BSTORE:
28 os << "BSTORE\t" << arg_;
29 break;
30
31 case PUSH:
32 os << "PUSH\t" << arg_;
33 break;
34
35 case POP:
36 os << "POP";
37 break;
38
39 case DUP:
40 os << "DUP";
41 break;
42
43 case ADD:
44 os << "ADD";
45 break;
46
47 case SUB:
48 os << "SUB";
49 break;
50
51 case MULT:
52 os << "MULT";
53 break;
54
55 case DIV:
56 os << "DIV";
57 break;
58
59 case INVERT:
60 os << "INVERT";
61 break;
62
63 case COMPARE:
64 os << "COMPARE\t" << arg_;
65 break;
66
67 case JUMP:
68 os << "JUMP\t" << arg_;
69 break;
70
71 case JUMP_YES:
72 os << "JUMP_YES\t" << arg_;
73 break;
74
75 case JUMP_NO:
76 os << "JUMP_NO\t" << arg_;
77 break;
78
79 case INPUT:
80 os << "INPUT";
81 break;
82
83 case PRINT:
84 os << "PRINT";
85 break;
86
87 case CALL:
88 os << "CALL\t" << arg_;
89 break;
90
91 case RETURN:
92 os << "RETURN\t" << arg_;
93 break;
94
95 case LOADB:
96 os << "LOADB\t" << arg_;
97 break;
98
99 case STOREB:
100 os << "STOREB\t" << arg_;
101 break;
102
103 case SHIFT:
104 os << "SHIFT\t" << arg_;
105 break;
106
107 default:
108 os << "INVALID\t" << arg_;
109 break;
110 }
111
112 os << endl;
113}
114
115void CodeGen::emit(Instruction instruction)
116{
117 commandBuffer_.push_back(Command(instruction));
118}
119
120void CodeGen::emit(Instruction instruction, int arg)
121{
122 commandBuffer_.push_back(Command(instruction, arg));
123}
124
125void CodeGen::emitAt(int address, Instruction instruction)
126{
127 commandBuffer_[address] = Command(instruction);
128}
129
130void CodeGen::emitAt(int address, Instruction instruction, int arg)
131{
132 commandBuffer_[address] = Command(instruction, arg);
133}
134
135int CodeGen::getCurrentAddress()
136{
137 return commandBuffer_.size();
138}
139
140int CodeGen::reserve()
141{
142 emit(NOP);
143 return commandBuffer_.size() - 1;
144}
145
146void CodeGen::flush()
147{
148 int count = commandBuffer_.size();
149 for(int address = 0; address < count; ++address) {
150 commandBuffer_[address].print(address, output_);
151 }
152 output_.flush();
153}