-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssembler.cpp
More file actions
171 lines (154 loc) · 6.57 KB
/
Copy pathAssembler.cpp
File metadata and controls
171 lines (154 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// ============================================================
// Assembler processes
// ============================================================
// Purpose: ASSEMBLY PHASE
// Translates TAC instructions into simulated x86-like
// assembly mnemonics. Each TAC instruction maps to
// one or more assembly instructions.
// ============================================================
#include "Assembler.h"
#include "Linker.h"
#include "Loader.h"
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cctype>
// ASSEMBLY INSTRUCTION TO STRING
std::string AsmInstruction::toString() const {
std::ostringstream ss;
// Pad opcode to 8 chars for alignment
ss << std::setw(8) << std::left << opcode;
if (!dest.empty()) ss << " " << dest;
if (!src.empty()) ss << ", " << src;
if (!comment.empty()) ss << " ; " << comment;
return ss.str();
}
// TOREG() – map TAC operand to assembly operand
std::string Assembler::toReg(const std::string& operand) {
// Temporaries (t0, t1…) → simulated registers EAX, EBX, ECX…
static const char* regs[] = {
"EAX","EBX","ECX","EDX","ESI","EDI","R8","R9","R10","R11"
};
bool isTemp = operand.size() >= 2 && operand[0] == 't';
for (size_t i = 1; isTemp && i < operand.size(); ++i) {
if (!std::isdigit(static_cast<unsigned char>(operand[i]))) {
isTemp = false;
}
}
if (isTemp) {
int idx = std::stoi(operand.substr(1)); // Extract number
if (idx < 10) return regs[idx];
return operand; // Spill to VM variable storage
}
// Labels, variables, immediates keep their names
return operand;
}
// EMIT() – add an assembly instruction to asmCode_ (helper for assemble)
void Assembler::emit(const std::string& opcode,
const std::string& dest,
const std::string& src,
const std::string& cmt) {
asmCode_.push_back(AsmInstruction{opcode, dest, src, cmt});
}
// ASSEMBLE() – translate TAC to assembly instructions
std::vector<AsmInstruction>
Assembler::assemble(const std::vector<TACInstruction>& tac) {
// Section header
emit(".section", ".text", "", "Code segment");
emit(".global", "main", "", "Entry point");
emit("main:", "", "", "");
for (const auto& instr : tac) {
// LABEL : result is label name, no opcode
if (instr.op == "LABEL") {
emit(instr.result + ":", "", "", "");
continue;
}
// HALT – emit code to exit program gracefully
if (instr.op == "HALT") {
emit("MOV", "EAX", "0", "exit code 0");
emit("INT", "0x80","", "system call - exit");
continue;
}
// PRINT – emit code to print value in arg1
if (instr.op == "PRINT") {
std::string src = toReg(instr.arg1);
emit("MOV", "EAX", src, "load value to print");
emit("CALL", "print_val", "", "runtime print function");
continue;
}
// RETURN – emit code to return from function, optionally with value in arg1
if (instr.op == "RETURN") {
if (!instr.arg1.empty()) {
emit("MOV", "EAX", toReg(instr.arg1), "return value");
}
emit("RET", "", "", "return from function");
continue;
}
// UNCONDITIONAL JUMP: GOTO label
if (instr.op == "GOTO") {
emit("JMP", instr.result, "", "unconditional jump");
continue;
}
// CONDITIONAL JUMP: IF_FALSE arg1 GOTO result
if (instr.op == "IF_FALSE") {
std::string reg = toReg(instr.arg1);
emit("CMP", reg, "0", "test condition");
emit("JE", instr.result, "", "jump if false (zero)");
continue;
}
// ASSIGNMENT: result = arg1 (unary) or result = arg1 op arg2 (binary)
if (instr.arg2.empty() && instr.op == "=") {
std::string dst = toReg(instr.result);
std::string src = toReg(instr.arg1);
emit("MOV", dst, src, instr.result + " = " + instr.arg1);
continue;
}
// BINARY OPERATION: result = arg1 op arg2
if (!instr.arg2.empty()) {
std::string dst = toReg(instr.result);
std::string lhs = toReg(instr.arg1);
std::string rhs = toReg(instr.arg2);
if (instr.op == "+") {
emit("MOV", dst, lhs, "load left operand");
emit("ADD", dst, rhs, "add right operand");
} else if (instr.op == "-") {
emit("MOV", dst, lhs, "load left operand");
emit("SUB", dst, rhs, "subtract right");
} else if (instr.op == "*") {
emit("MOV", "EAX", lhs, "load into EAX for MUL");
emit("IMUL","EAX", rhs, "multiply");
emit("MOV", dst, "EAX", "store result");
} else if (instr.op == "/") {
emit("MOV", "EAX", lhs, "dividend");
emit("CDQ", "", "", "sign-extend EAX->EDX:EAX");
emit("IDIV", rhs, "", "divide EDX:EAX by rhs");
emit("MOV", dst, "EAX", "quotient in EAX");
} else if (instr.op == "==" || instr.op == "!=" ||
instr.op == "<" || instr.op == ">" ||
instr.op == "<=" || instr.op == ">=") {
// Comparison: set dst to 0 or 1
emit("CMP", lhs, rhs, "compare operands");
std::string setOp;
if (instr.op == "==") setOp = "SETE";
else if (instr.op == "!=") setOp = "SETNE";
else if (instr.op == "<") setOp = "SETL";
else if (instr.op == ">") setOp = "SETG";
else if (instr.op == "<=") setOp = "SETLE";
else setOp = "SETGE";
emit(setOp, "AL", "", "set byte on condition");
emit("MOVZX", dst, "AL", "zero-extend to 32 bits");
}
}
}
return asmCode_;
}
// PRINTASSEMBLY() – print generated assembly instructions
void Assembler::printAssembly() const {
for (const auto& instr : asmCode_) {
// Labels are not indented; instructions are
bool isLabel = (!instr.opcode.empty() &&
instr.opcode.back() == ':');
std::cout << (isLabel ? "" : " ") << instr.toString() << "\n";
}
std::cout << "\n";
}