added int func(int x, int y), return int

This commit is contained in:
mmichlol
2026-02-06 21:01:12 +01:00
parent 196140f9b8
commit 5fbd0f98a2
3 changed files with 355 additions and 266 deletions

View File

@@ -1,102 +1,209 @@
#include "codegen.h"
#include <string>
#include <vector>
#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 GetAsyncKeyState\n\n";
result += "extern getchar\n"; // <--- DODAJ TO
result += "section .data\n";
for (const auto& v : state.variables) {
result += " " + v.first + " dd " + std::to_string(v.second) + "\n";
}
result += " fmt db '%d', 10, 0\n";
result += " pause_msg db 'Nacisnij ESC aby zamknac...', 10, 0\n\n";
result += " fmt db '%d', 10, 0\n";
result += "section .text\n\n";
result += "section .text\n";
for (const auto& pair : state.functions) {
const Function& func = pair.second;
result += func.name + ":\n";
// --- FUNKCJE ---
for (const auto& func : state.functions) {
result += func.first + ":\n";
result += " sub rsp, 40\n";
result += " push rbp\n";
result += " mov rbp, rsp\n";
result += " sub rsp, 256\n";
for (const std::string& cmd : func.second) {
std::map<std::string, int> stackMap;
int currentStack = 8;
result += " ; CMD: " + cmd + "\n";
// 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;
}
if (cmd.find("PRINT:") == 0) {
std::string varName = cmd.substr(6);
if (state.variables.count(varName)) {
result += " mov edx, [" + varName + "]\n";
result += " lea rcx, [rel fmt]\n";
result += " call printf\n";
}
// 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") {
stackMap[instr.arg1] = currentStack;
currentStack += 8;
}
else if (cmd.find("IF:") == 0) {
int ifIndex = std::stoi(cmd.substr(3));
if (ifIndex >= 0 && ifIndex < state.ifBlocks.size()) {
const auto& ifBlock = state.ifBlocks[ifIndex];
if (state.variables.count(ifBlock.conditionVar)) {
std::string labelSkip = "skip_if_" + std::to_string(ifIndex);
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";
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::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("==");
result += " ; --- IF START [" + ifBlock.conditionVar + "] ---\n";
result += " mov eax, [" + ifBlock.conditionVar + "]\n";
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);
result += " test eax, eax\n";
result += " jz " + labelSkip + "\n";
std::string op1 = getVarLocation(leftStr, stackMap);
std::string op2 = getVarLocation(rightStr, stackMap);
for (const std::string& innerPrint : ifBlock.prints) {
if (state.variables.count(innerPrint)) {
result += " mov edx, [" + innerPrint + "]\n";
result += " lea rcx, [rel fmt]\n";
result += " call printf\n";
}
}
result += labelSkip + ":\n";
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);
result += " mov eax, " + val + " ; return value\n";
result += " leave\n";
result += " ret\n";
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";
break;
}
case OpType::CALL: {
// Parsowanie argumentów
std::string argsRaw = instr.arg2;
std::vector<std::string> callArgs;
if (instr.arg1 == "input") {
result += " call getchar\n"; // Czeka na enter
break; // Wychodzimy, ¿eby nie robiæ standardowego call
}
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;
}
}
}
result += " add rsp, 40\n";
result += " ret\n\n";
}
// ==main==
result += "main:\n";
result += " sub rsp, 40\n";
for (const std::string& call : state.printCalls) {
if (call.find("CALL_") == 0) {
std::string funcName = call.substr(5);
if (state.functions.count(funcName)) {
result += " call " + funcName + "\n";
}
if (func.returnType == "void") {
result += " leave\n ret\n";
}
result += "\n";
}
for (const std::string& var : state.globalPrints) {
if (state.variables.count(var)) {
result += " mov edx, [" + var + "]\n";
result += " lea rcx, [rel fmt]\n";
result += " call printf\n";
}
}
result += " lea rcx, [rel pause_msg]\n";
result += " call printf\n";
result += "pause_loop:\n";
result += " mov ecx, 27\n";
result += " call GetAsyncKeyState\n";
result += " test ax, 8000h\n";
result += " jz pause_loop\n";
result += " add rsp, 40\n";
result += " ret\n";
return result;
}