#include "codegen.h" #include #include #include // Pomocnicza funkcja: sprawdza czy string to czysta liczba bool isNumber(const std::string& s) { if (s.empty()) return false; size_t start = (s[0] == '-') ? 1 : 0; for (size_t i = start; i < s.length(); i++) { if (!isdigit(s[i])) return false; } return true; } // Zamienia nazwę zmiennej na adres pamięci [rbp-X] lub liczbę std::string getVarLocation(const std::string& name, const std::map& locals) { std::string cleanName = name; // Usuwamy ewentualne spacje size_t first = cleanName.find_first_not_of(" \t"); if (first != std::string::npos) cleanName = cleanName.substr(first); size_t last = cleanName.find_last_not_of(" \t"); if (last != std::string::npos) cleanName = cleanName.substr(0, last + 1); if (cleanName.empty()) return "0"; if (isNumber(cleanName)) return cleanName; if (cleanName == "RAX") return "eax"; if (locals.count(cleanName)) { int offset = locals.at(cleanName); return "[rbp-" + std::to_string(offset) + "]"; } // Zwracamy oryginał (jeśli to np. nazwa etykiety), ale to zazwyczaj błąd dla zmiennych return cleanName; } std::string generateAssembly(const CompilerState& state) { std::string result; result += "global main\n"; result += "extern printf\n"; result += "extern getchar\n"; result += "extern _getch\n"; // ZMIANA: Dodajemy _getch (zamiast lub obok getchar) 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) { const Function& func = pair.second; result += func.name + ":\n"; result += " push rbp\n"; result += " mov rbp, rsp\n"; result += " sub rsp, 256\n"; std::map stackMap; int currentStack = 8; // 1. ARGUMENTY if (func.args.size() > 0) { stackMap[func.args[0]] = currentStack; result += " mov [rbp-" + std::to_string(currentStack) + "], rcx ; arg " + func.args[0] + "\n"; currentStack += 8; } if (func.args.size() > 1) { stackMap[func.args[1]] = currentStack; result += " mov [rbp-" + std::to_string(currentStack) + "], rdx ; arg " + func.args[1] + "\n"; currentStack += 8; } // 2. INSTRUKCJE for (const auto& instr : func.instructions) { // 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 = 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: { 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 += " add eax, " + op2 + "\n"; 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); std::string dst = getVarLocation(instr.arg1, stackMap); result += " mov eax, " + op1 + "\n"; result += " cmp eax, " + op2 + "\n"; result += " sete al\n"; result += " movzx eax, al\n"; result += " mov " + dst + ", eax\n"; break; } case OpType::JMP_FALSE: { // PARSOWANIE WARUNKU: np. "suma == 30" lub "test" std::string condRaw = instr.arg2; size_t eqPos = condRaw.find("=="); if (eqPos != std::string::npos) { // Mamy porównanie w IFie (a == b) std::string leftStr = condRaw.substr(0, eqPos); std::string rightStr = condRaw.substr(eqPos + 2); std::string op1 = getVarLocation(leftStr, stackMap); std::string op2 = getVarLocation(rightStr, stackMap); result += " mov eax, " + op1 + "\n"; result += " cmp eax, " + op2 + "\n"; result += " jne " + instr.arg1 + " ; jump if NOT equal\n"; } else { // Zwykła zmienna boolowska (if test) std::string cond = getVarLocation(condRaw, stackMap); result += " mov eax, " + cond + "\n"; result += " test eax, eax\n"; result += " jz " + instr.arg1 + " ; jump if zero\n"; } break; } case OpType::LABEL: { result += instr.arg1 + ":\n"; break; } case OpType::RETURN: { std::string val = getVarLocation(instr.arg1, stackMap); if (val.empty() || val == ";"); else { result += " mov eax, " + val + " ; return value\n"; } result += " leave\n"; result += " ret\n"; break; } case OpType::PRINT: { 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: { // Parsowanie argumentów std::string argsRaw = instr.arg2; std::vector callArgs; // --- SPECJALNE FUNKCJE SYSTEMOWE --- // 1. input() - czeka na ENTER (stare) if (instr.arg1 == "input") { result += " call getchar\n"; break; } // 2. read_key() - zwraca kod wciśniętego klawisza (NOWOŚĆ) if (instr.arg1 == "read_key") { result += " call _getch\n"; // Zwraca kod znaku w EAX // Jeśli to klawisz specjalny (strzałki), _getch zwraca 0 lub 224, // a potem trzeba wywołać go drugi raz. // Na razie zróbmy prosto: zwracamy to co zwrócił pierwszy _getch. break; } if (!argsRaw.empty()) { size_t comma = argsRaw.find(','); if (comma != std::string::npos) { callArgs.push_back(argsRaw.substr(0, comma)); callArgs.push_back(argsRaw.substr(comma + 1)); } else { callArgs.push_back(argsRaw); } } // Obsługa RDX (arg 2) if (callArgs.size() > 1) { std::string val = getVarLocation(callArgs[1], stackMap); if (isNumber(val)) { // Jeśli liczba: mov rdx, 100 result += " mov rdx, " + val + "\n"; } else { // Jeśli zmienna/pamięć: movsxd rdx, dword [rbp-8] result += " movsxd rdx, dword " + val + "\n"; } } // Obsługa RCX (arg 1) if (callArgs.size() > 0) { std::string val = getVarLocation(callArgs[0], stackMap); if (isNumber(val)) { result += " mov rcx, " + val + "\n"; } else { result += " movsxd rcx, dword " + val + "\n"; } } result += " call " + instr.arg1 + "\n"; break; } } } if (func.returnType == "void") { result += " leave\n ret\n"; } result += "\n"; } return result; }