281 lines
11 KiB
C++
281 lines
11 KiB
C++
#include "codegen.h"
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <vector>
|
|
|
|
// 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<std::string, int>& 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<std::string, int> 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<std::string> 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;
|
|
}
|