added more functions

This commit is contained in:
mmichlol
2026-02-07 11:33:50 +01:00
parent 5fbd0f98a2
commit aa6d16bc52
8 changed files with 264 additions and 17 deletions

View File

@@ -40,9 +40,19 @@ std::string generateAssembly(const CompilerState& state) {
std::string result;
result += "global main\n";
result += "extern printf\n";
result += "extern getchar\n"; // <--- DODAJ TO
result += "extern getchar\n";
result += "section .data\n";
result += " fmt db '%d', 10, 0\n";
result += "section .data\n";
result += " fmt_int db '%d', 10, 0\n"; // Format dla liczb
result += " fmt_str db '%s', 10, 0\n"; // NOWOŒÆ: Format dla stringów
// --- WYPISYWANIE STRINGÓW ---
for (const auto& pair : state.stringLiterals) {
// Nazwa etykiety: db 'Tresc', 0
// Uwaga: ASM nie lubi pewnych znaków, ale zak³adamy proste litery
result += " " + pair.second + " db '" + pair.first + "', 0\n";
}
result += "section .text\n\n";
for (const auto& pair : state.functions) {
@@ -71,19 +81,33 @@ std::string generateAssembly(const CompilerState& state) {
// 2. INSTRUKCJE
for (const auto& instr : func.instructions) {
// Rezerwacja miejsca dla nowych zmiennych
if ((instr.type == OpType::ASSIGN || instr.type == OpType::ADD || instr.type == OpType::EQ)
&& stackMap.find(instr.arg1) == stackMap.end() && instr.arg1 != "RAX") {
// Rezerwacja miejsca dla nowych zmiennych (wynikowych)
// Dodajemy tu OpType::SUB i OpType::MUL
bool isWriteOp = (instr.type == OpType::ASSIGN ||
instr.type == OpType::ADD ||
instr.type == OpType::EQ ||
instr.type == OpType::SUB ||
instr.type == OpType::MUL);
if (isWriteOp && stackMap.find(instr.arg1) == stackMap.end() && instr.arg1 != "RAX") {
stackMap[instr.arg1] = currentStack;
currentStack += 8;
}
switch (instr.type) {
case OpType::ASSIGN: {
std::string src = getVarLocation(instr.arg2, stackMap);
std::string dst = getVarLocation(instr.arg1, stackMap);
result += " mov eax, " + src + "\n";
result += " mov " + dst + ", eax\n";
std::string src = instr.arg2;
if (instr.arg3 == "STRING") {
result += " lea rax, [rel " + src + "]\n";
std::string dst = getVarLocation(instr.arg1, stackMap);
result += " mov qword " + dst + ", rax\n";
}
else {
std::string srcLoc = getVarLocation(instr.arg2, stackMap);
std::string dst = getVarLocation(instr.arg1, stackMap);
result += " mov eax, " + srcLoc + "\n";
result += " mov " + dst + ", eax\n";
}
break;
}
case OpType::ADD: {
@@ -95,6 +119,30 @@ std::string generateAssembly(const CompilerState& state) {
result += " mov " + dst + ", eax\n";
break;
}
case OpType::SUB: {
// a = b - c
std::string op1 = getVarLocation(instr.arg2, stackMap);
std::string op2 = getVarLocation(instr.arg3, stackMap);
std::string dst = getVarLocation(instr.arg1, stackMap);
result += " mov eax, " + op1 + "\n";
result += " sub eax, " + op2 + "\n"; // sub = odejmowanie
result += " mov " + dst + ", eax\n";
break;
}
case OpType::MUL: {
// a = b * c
std::string op1 = getVarLocation(instr.arg2, stackMap);
std::string op2 = getVarLocation(instr.arg3, stackMap);
std::string dst = getVarLocation(instr.arg1, stackMap);
result += " mov eax, " + op1 + "\n";
// Mno¿enie w x86 jest specyficzne: imul eax, operand
// Wynik l¹duje w eax (i edx jeœli du¿y, ale ignorujemy nadmiar dla prostoty)
result += " imul eax, " + op2 + "\n";
result += " mov " + dst + ", eax\n";
break;
}
case OpType::EQ: {
std::string op1 = getVarLocation(instr.arg2, stackMap);
std::string op2 = getVarLocation(instr.arg3, stackMap);
@@ -144,10 +192,17 @@ std::string generateAssembly(const CompilerState& state) {
break;
}
case OpType::PRINT: {
std::string val = getVarLocation(instr.arg1, stackMap);
result += " mov edx, " + val + "\n";
result += " lea rcx, [rel fmt]\n";
result += " call printf\n";
if (instr.arg2 == "STRING") {
result += " lea rdx, [rel " + instr.arg1 + "]\n";
result += " lea rcx, [rel fmt_str]\n";
result += " call printf\n";
}
else {
std::string val = getVarLocation(instr.arg1, stackMap);
result += " mov edx, " + val + "\n";
result += " lea rcx, [rel fmt_int]\n";
result += " call printf\n";
}
break;
}
case OpType::CALL: {