added classes and bug fix
This commit is contained in:
@@ -13,7 +13,6 @@ bool isNumber(const std::string& s) {
|
||||
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
|
||||
@@ -27,15 +26,19 @@ std::string getVarLocation(const std::string& name, const std::map<std::string,
|
||||
|
||||
if (cleanName == "RAX") return "eax";
|
||||
|
||||
// USUNIÊTO: if (cleanName == "this") return "[rbp+16]";
|
||||
// Teraz "this" wpadnie do bloku poni¿ej i zostanie znalezione w mapie locals.
|
||||
|
||||
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
|
||||
// Zwracamy orygina³ (jeœli to np. nazwa etykiety)
|
||||
return cleanName;
|
||||
}
|
||||
|
||||
|
||||
std::string generateAssembly(const CompilerState& state) {
|
||||
// 1. NAG£ÓWEK I DEKLARACJE EXTERN
|
||||
std::string result = "default rel\n";
|
||||
@@ -55,8 +58,6 @@ std::string generateAssembly(const CompilerState& state) {
|
||||
|
||||
// Zrzucamy stringi: ETYKIETA db "TRESC", 0
|
||||
for (const auto& p : state.stringLiterals) {
|
||||
// p.first -> Etykieta (np. str_0)
|
||||
// p.second -> TreϾ (np. Hello World)
|
||||
result += " " + p.first + " db \"" + p.second + "\", 0\n";
|
||||
}
|
||||
|
||||
@@ -69,27 +70,39 @@ std::string generateAssembly(const CompilerState& state) {
|
||||
|
||||
result += " push rbp\n";
|
||||
result += " mov rbp, rsp\n";
|
||||
result += " sub rsp, 288\n";
|
||||
result += " sub rsp, 512\n"; // Zwiêkszy³em stos dla bezpieczeñstwa obiektów
|
||||
|
||||
std::map<std::string, int> stackMap;
|
||||
int currentStack = 8;
|
||||
|
||||
// ARGUMENTY FUNKCJI
|
||||
// RCX, RDX, R8, R9 - konwencja Windows x64 (shadow space obs³uguje caller)
|
||||
// Jeœli funkcja jest metod¹, pierwszym argumentem jest 'this' (wskaŸnik na obiekt)
|
||||
|
||||
int argIdx = 0;
|
||||
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;
|
||||
argIdx++;
|
||||
}
|
||||
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;
|
||||
argIdx++;
|
||||
}
|
||||
if (func.args.size() > 2) {
|
||||
stackMap[func.args[2]] = currentStack;
|
||||
result += " mov [rbp-" + std::to_string(currentStack) + "], r8 ; arg " + func.args[2] + "\n";
|
||||
currentStack += 8;
|
||||
argIdx++;
|
||||
}
|
||||
|
||||
// GENEROWANIE INSTRUKCJI
|
||||
for (const auto& instr : func.instructions) {
|
||||
|
||||
// Rezerwacja miejsca na stosie
|
||||
// Rezerwacja miejsca na stosie dla zmiennych
|
||||
bool isWriteOp = (instr.type == OpType::ASSIGN ||
|
||||
instr.type == OpType::ADD ||
|
||||
instr.type == OpType::EQ ||
|
||||
@@ -101,63 +114,154 @@ std::string generateAssembly(const CompilerState& state) {
|
||||
instr.type == OpType::LOGIC_OR ||
|
||||
instr.type == OpType::MSGBOX ||
|
||||
instr.type == OpType::ARRAY_DECLARE ||
|
||||
instr.type == OpType::ARRAY_SET);
|
||||
instr.type == OpType::ARRAY_SET ||
|
||||
instr.type == OpType::ALLOC_OBJECT ||
|
||||
instr.type == OpType::LOAD_FIELD);
|
||||
|
||||
if (isWriteOp && stackMap.find(instr.arg1) == stackMap.end() && instr.arg1 != "RAX") {
|
||||
stackMap[instr.arg1] = currentStack;
|
||||
currentStack += 8;
|
||||
|
||||
// Dla obiektów i tablic alokujemy wiêcej miejsca
|
||||
if (instr.type == OpType::ALLOC_OBJECT) {
|
||||
int size = std::stoi(instr.arg2);
|
||||
currentStack += size;
|
||||
}
|
||||
else if (instr.type == OpType::ARRAY_DECLARE) {
|
||||
int size = std::stoi(instr.arg2);
|
||||
currentStack += (size * 8);
|
||||
}
|
||||
else {
|
||||
currentStack += 8; // domyœlnie zmienna int/ptr
|
||||
}
|
||||
}
|
||||
|
||||
switch (instr.type) {
|
||||
case OpType::ASSIGN: {
|
||||
std::string src = instr.arg2;
|
||||
// ODCZYT TABLICY: x = t[i]
|
||||
if (instr.arg3.find("ARRAY_IDX:") == 0) {
|
||||
std::string indexStr = instr.arg3.substr(10); // Pobierz "i"
|
||||
std::string arrName = instr.arg2; // Pobierz "t"
|
||||
// Jeœli Ÿród³em jest wynik funkcji (RAX), nie generujemy "mov rax, eax"
|
||||
if (instr.arg2 == "RAX") {
|
||||
std::string dst = getVarLocation(instr.arg1, stackMap);
|
||||
|
||||
int baseOffset = stackMap[arrName];
|
||||
|
||||
// £adujemy indeks do RCX
|
||||
if (isNumber(indexStr)) result += " mov rcx, " + indexStr + "\n";
|
||||
else result += " mov rcx, " + getVarLocation(indexStr, stackMap) + "\n";
|
||||
|
||||
result += " imul rcx, 8\n"; // index * 8
|
||||
|
||||
// Obliczamy adres
|
||||
result += " mov rdx, rbp\n";
|
||||
result += " sub rdx, " + std::to_string(baseOffset) + "\n";
|
||||
result += " sub rdx, rcx\n";
|
||||
|
||||
// Odczytujemy wartoϾ z tablicy do RAX
|
||||
result += " mov rax, [rdx]\n";
|
||||
|
||||
// Zapisujemy do zmiennej docelowej
|
||||
// POPRAWIONE: Tylko dwa backslashe, tak jak w reszcie kodu
|
||||
result += " mov " + dst + ", rax\n";
|
||||
}
|
||||
else if (instr.arg3 == "STRING") {
|
||||
// Przypisanie stringa: ³adujemy ADRES (LEA)
|
||||
result += " lea rax, [rel " + src + "]\n";
|
||||
else if (instr.arg3.find("ARRAY_IDX:") == 0) {
|
||||
std::string indexStr = instr.arg3.substr(10);
|
||||
std::string arrName = instr.arg2;
|
||||
std::string dst = getVarLocation(instr.arg1, stackMap);
|
||||
// Zapisujemy adres w zmiennej lokalnej (wskaŸnik 64-bit qword)
|
||||
result += " mov qword " + dst + ", rax\n";
|
||||
int baseOffset = stackMap[arrName];
|
||||
|
||||
if (isNumber(indexStr)) result += " mov rcx, " + indexStr + "\\n";
|
||||
else result += " mov rcx, " + getVarLocation(indexStr, stackMap) + "\\n";
|
||||
|
||||
result += " imul rcx, 8\\n";
|
||||
result += " mov rdx, rbp\\n";
|
||||
result += " sub rdx, " + std::to_string(baseOffset) + "\\n";
|
||||
result += " sub rdx, rcx\\n";
|
||||
result += " mov rax, [rdx]\\n";
|
||||
result += " mov " + dst + ", rax\\n";
|
||||
}
|
||||
else if (instr.arg3 == "STRING") {
|
||||
std::string src = instr.arg2;
|
||||
result += " lea rax, [rel " + src + "]\\n";
|
||||
std::string dst = getVarLocation(instr.arg1, stackMap);
|
||||
result += " mov qword " + dst + ", rax\\n";
|
||||
}
|
||||
else {
|
||||
// Zwyk³e przypisanie liczby
|
||||
std::string srcLoc = getVarLocation(instr.arg2, stackMap);
|
||||
std::string dst = getVarLocation(instr.arg1, stackMap);
|
||||
|
||||
if (isNumber(src)) {
|
||||
result += " mov eax, " + src + "\n";
|
||||
}
|
||||
else {
|
||||
result += " mov eax, " + srcLoc + "\n";
|
||||
}
|
||||
result += " mov " + dst + ", eax\n";
|
||||
std::string src = instr.arg2;
|
||||
if (isNumber(src)) result += " mov rax, " + src + "\\n";
|
||||
else result += " mov rax, " + srcLoc + "\\n";
|
||||
|
||||
result += " mov " + dst + ", rax\\n";
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case OpType::ALLOC_OBJECT: {
|
||||
// Miejsce na stosie zosta³o zarezerwowane wy¿ej w pêtli (currentStack += size)
|
||||
// Mo¿emy opcjonalnie wyzerowaæ pamiêæ (memset), ale na razie pomijamy dla prostoty
|
||||
// Komentarz w ASM
|
||||
result += " ; Alloc Object " + instr.arg1 + " size: " + instr.arg2 + "\n";
|
||||
break;
|
||||
}
|
||||
case OpType::STORE_FIELD: {
|
||||
// STORE_FIELD objName, offset, value
|
||||
std::string objName = instr.arg1;
|
||||
int offset = std::stoi(instr.arg2);
|
||||
std::string valStr = instr.arg3;
|
||||
|
||||
// 1. Gdzie jest obiekt? (jego baza)
|
||||
// Obiekt na stosie zaczyna siê pod [RBP - stackMap[objName]]
|
||||
// Pola s¹ kolejne w dó³ stosu (bo stos roœnie w dó³, ale struktura ma dodatnie offsety...
|
||||
// W C lokalne struktury: &obj to najni¿szy adres.
|
||||
// U nas stackMap[obj] to "górny" adres (pierwsze zarezerwowane 8 bajtów).
|
||||
// Przyjmijmy: adres_pola = (RBP - stackMap[objName]) - offset
|
||||
|
||||
// Pobierz wartoϾ do zapisania
|
||||
if (isNumber(valStr)) {
|
||||
result += " mov rax, " + valStr + "\n";
|
||||
}
|
||||
else {
|
||||
result += " mov rax, " + getVarLocation(valStr, stackMap) + "\n";
|
||||
}
|
||||
|
||||
int baseOffset = 0;
|
||||
|
||||
// SprawdŸ czy objName to "this"
|
||||
if (objName == "this") {
|
||||
// "this" jest wskaŸnikiem! Trzeba go za³adowaæ
|
||||
std::string thisPtrLoc = getVarLocation("this", stackMap);
|
||||
result += " mov rdx, " + thisPtrLoc + "\n"; // RDX = adres obiektu
|
||||
// Adres pola = RDX - offset (tutaj uwaga: jeœli alokujemy na stosie "w dó³", to pola maj¹ ujemne offsety wzglêdem bazy?)
|
||||
// Zróbmy proœciej: w 'ALLOC_OBJECT' rezerwujemy blok.
|
||||
// [RBP - base] to pocz¹tek (pole 0).
|
||||
// [RBP - base - 8] to pole 1 (offset 8).
|
||||
// Czyli adres = RBP - base - offset.
|
||||
|
||||
// ALE: "this" przekazany do funkcji to wskaŸnik na ten obszar w pamiêci.
|
||||
// Jeœli przekazujemy adres zmiennej lokalnej (LEA), to wskaŸnik pokazuje na [RBP-base].
|
||||
// Wiêc [RDX - offset] powinno zadzia³aæ.
|
||||
result += " sub rdx, " + std::to_string(offset) + "\n";
|
||||
result += " mov [rdx], rax\n";
|
||||
}
|
||||
else {
|
||||
// Obiekt lokalny na stosie
|
||||
baseOffset = stackMap[objName];
|
||||
result += " mov rdx, rbp\n";
|
||||
result += " sub rdx, " + std::to_string(baseOffset) + "\n";
|
||||
result += " sub rdx, " + std::to_string(offset) + "\n";
|
||||
result += " mov [rdx], rax\n";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OpType::LOAD_FIELD: {
|
||||
// LOAD_FIELD destVar, objName, offset
|
||||
std::string destVar = instr.arg1; // gdzie zapisaæ wynik
|
||||
std::string objName = instr.arg2; // sk¹d czytaæ
|
||||
int offset = std::stoi(instr.arg3); // offset pola
|
||||
|
||||
// 1. Oblicz adres pola
|
||||
if (objName == "this") {
|
||||
std::string thisPtrLoc = getVarLocation("this", stackMap);
|
||||
result += " mov rdx, " + thisPtrLoc + "\n";
|
||||
result += " sub rdx, " + std::to_string(offset) + "\n";
|
||||
}
|
||||
else {
|
||||
int baseOffset = stackMap[objName];
|
||||
result += " mov rdx, rbp\n";
|
||||
result += " sub rdx, " + std::to_string(baseOffset) + "\n";
|
||||
result += " sub rdx, " + std::to_string(offset) + "\n";
|
||||
}
|
||||
|
||||
// 2. Pobierz wartoϾ
|
||||
result += " mov rax, [rdx]\n";
|
||||
|
||||
// 3. Zapisz do zmiennej docelowej
|
||||
std::string destLoc = getVarLocation(destVar, stackMap);
|
||||
result += " mov " + destLoc + ", rax\n";
|
||||
break;
|
||||
}
|
||||
case OpType::ADD: {
|
||||
std::string op1 = getVarLocation(instr.arg2, stackMap);
|
||||
std::string op2 = getVarLocation(instr.arg3, stackMap);
|
||||
@@ -214,7 +318,7 @@ std::string generateAssembly(const CompilerState& state) {
|
||||
else {
|
||||
result += " idiv dword " + op2 + "\n";
|
||||
}
|
||||
result += " mov " + dst + ", edx\n"; // Reszta
|
||||
result += " mov " + dst + ", edx\n";
|
||||
break;
|
||||
}
|
||||
case OpType::EQ: {
|
||||
@@ -262,23 +366,10 @@ std::string generateAssembly(const CompilerState& state) {
|
||||
}
|
||||
case OpType::JMP_FALSE: {
|
||||
std::string condRaw = instr.arg2;
|
||||
size_t eqPos = condRaw.find("==");
|
||||
if (eqPos != std::string::npos) {
|
||||
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 + "\n";
|
||||
}
|
||||
else {
|
||||
std::string cond = getVarLocation(condRaw, stackMap);
|
||||
result += " mov eax, " + cond + "\n";
|
||||
result += " test eax, eax\n";
|
||||
result += " je " + instr.arg1 + "\n";
|
||||
|
||||
}
|
||||
std::string cond = getVarLocation(condRaw, stackMap);
|
||||
result += " mov eax, " + cond + "\n";
|
||||
result += " test eax, eax\n";
|
||||
result += " je " + instr.arg1 + "\n";
|
||||
break;
|
||||
}
|
||||
case OpType::JMP: {
|
||||
@@ -286,60 +377,32 @@ std::string generateAssembly(const CompilerState& state) {
|
||||
break;
|
||||
}
|
||||
case OpType::ARRAY_DECLARE: {
|
||||
std::string name = instr.arg1;
|
||||
int size = std::stoi(instr.arg2);
|
||||
|
||||
// Rezerwujemy miejsce dla ca³ej tablicy
|
||||
// t[0] bêdzie pod aktualnym currentStack
|
||||
stackMap[name] = currentStack;
|
||||
|
||||
// Przesuwamy wskaŸnik stosu o (rozmiar * 8 bajtów)
|
||||
// Zak³adamy, ¿e ka¿dy element to 64-bit (dla bezpieczeñstwa i prostoty assemblera)
|
||||
currentStack += (size * 8);
|
||||
|
||||
// W ASM nie musimy generowaæ ¿adnego kodu! (Miejsce ju¿ jest z sub rsp, 256)
|
||||
// O ile tablica mieœci siê w tych 256 bajtach.
|
||||
// Jeœli chcesz byæ PRO: dodaj na pocz¹tku funkcji "sub rsp, (currentStack + zapas)"
|
||||
// Obs³u¿one przy alokacji stosu
|
||||
break;
|
||||
}
|
||||
case OpType::ARRAY_SET: {
|
||||
std::string arrName = instr.arg1; // t
|
||||
std::string indexStr = instr.arg2; // i
|
||||
std::string valStr = instr.arg3; // val
|
||||
std::string arrName = instr.arg1;
|
||||
std::string indexStr = instr.arg2;
|
||||
std::string valStr = instr.arg3;
|
||||
|
||||
// 1. Obliczamy wartoϾ do wpisania
|
||||
if (isNumber(valStr)) {
|
||||
result += " mov rax, " + valStr + "\n";
|
||||
}
|
||||
if (isNumber(valStr)) result += " mov rax, " + valStr + "\n";
|
||||
else {
|
||||
std::string valLoc = getVarLocation(valStr, stackMap);
|
||||
// Jeœli to zmienna ze stosu, to movsxd (rozszerzenie znaku) lub mov
|
||||
result += " mov rax, " + valLoc + "\n"; // Zak³adamy 64-bit (lub eax dla 32)
|
||||
result += " mov rax, " + valLoc + "\n";
|
||||
}
|
||||
|
||||
int baseOffset = stackMap[arrName];
|
||||
|
||||
// £adujemy indeks do RCX
|
||||
if (isNumber(indexStr)) {
|
||||
result += " mov rcx, " + indexStr + "\n";
|
||||
}
|
||||
if (isNumber(indexStr)) result += " mov rcx, " + indexStr + "\n";
|
||||
else {
|
||||
std::string idxLoc = getVarLocation(indexStr, stackMap);
|
||||
result += " mov rcx, " + idxLoc + "\n"; // Pobierz indeks ze zmiennej
|
||||
result += " mov rcx, " + idxLoc + "\n";
|
||||
}
|
||||
|
||||
// Obliczamy przesuniêcie bajtowe: index * 8
|
||||
result += " imul rcx, 8\n";
|
||||
|
||||
// Poniewa¿ adres to RBP - (base + index*8) -> RBP - base - index*8
|
||||
// Musimy to sprytnie zmontowaæ.
|
||||
// Obliczmy finalny adres w RDX.
|
||||
|
||||
result += " mov rdx, rbp\n";
|
||||
result += " sub rdx, " + std::to_string(baseOffset) + "\n"; // RDX = adres t[0]
|
||||
result += " sub rdx, rcx\n"; // RDX = adres t[i]
|
||||
|
||||
// Zapisujemy wartoϾ (RAX) pod adres (RDX)
|
||||
result += " sub rdx, " + std::to_string(baseOffset) + "\n";
|
||||
result += " sub rdx, rcx\n";
|
||||
result += " mov [rdx], rax\n";
|
||||
break;
|
||||
}
|
||||
@@ -348,7 +411,6 @@ std::string generateAssembly(const CompilerState& state) {
|
||||
break;
|
||||
}
|
||||
case OpType::PRINT: {
|
||||
// Instrukcja PRINT zwyk³a (liczba)
|
||||
std::string val = getVarLocation(instr.arg1, stackMap);
|
||||
result += " mov edx, " + val + "\n";
|
||||
result += " lea rcx, [rel fmt_int]\n";
|
||||
@@ -371,11 +433,7 @@ std::string generateAssembly(const CompilerState& state) {
|
||||
break;
|
||||
}
|
||||
case OpType::CALL: {
|
||||
if (instr.arg1 == "input") {
|
||||
result += " call _getch\n";
|
||||
break;
|
||||
}
|
||||
if (instr.arg1 == "read_key") {
|
||||
if (instr.arg1 == "input" || instr.arg1 == "read_key") {
|
||||
result += " call _getch\n";
|
||||
break;
|
||||
}
|
||||
@@ -391,30 +449,56 @@ std::string generateAssembly(const CompilerState& state) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Standardowe wywo³anie funkcji
|
||||
// Call metody/funkcji
|
||||
std::string funcName = instr.arg1;
|
||||
std::string argsRaw = instr.arg2;
|
||||
std::vector<std::string> callArgs;
|
||||
|
||||
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);
|
||||
std::stringstream ss(argsRaw);
|
||||
std::string segment;
|
||||
while (std::getline(ss, segment, ',')) {
|
||||
callArgs.push_back(segment);
|
||||
}
|
||||
}
|
||||
|
||||
// Przygotowanie argumentów dla Windows x64 (RCX, RDX, R8, R9)
|
||||
// Argument 0 (RCX) - ewentualnie 'this'
|
||||
if (callArgs.size() > 0) {
|
||||
// Czy to 'this' (nazwa obiektu)?
|
||||
std::string arg0 = callArgs[0];
|
||||
if (state.varTypes.count(arg0) && state.classes.count(state.varTypes.at(arg0))) {
|
||||
// Przekazujemy ADRES obiektu (pointer)
|
||||
// Obiekt jest na stosie: [RBP - offset]
|
||||
// Adres to: RBP - offset
|
||||
int offset = stackMap.at(arg0);
|
||||
result += " lea rcx, [rbp-" + std::to_string(offset) + "]\n";
|
||||
}
|
||||
else if (arg0 == "this") {
|
||||
// Przekazujemy this dalej
|
||||
result += " mov rcx, [rbp+16]\n"; // zak³adaj¹c ¿e this jest w shadow space? nie, my go kopiujemy na stos
|
||||
// Wróæmy do logiki argumentów: argumenty funkcji s¹ kopiowane na stos lokalny.
|
||||
// arg0 ("this") jest w stackMap["this"].
|
||||
std::string loc = getVarLocation("this", stackMap);
|
||||
result += " mov rcx, " + loc + "\n";
|
||||
}
|
||||
else {
|
||||
// Zwyk³a zmienna / liczba
|
||||
std::string val = getVarLocation(arg0, stackMap);
|
||||
if (isNumber(val)) result += " mov rcx, " + val + "\n";
|
||||
else result += " movsxd rcx, dword " + val + "\n";
|
||||
}
|
||||
}
|
||||
|
||||
if (callArgs.size() > 1) {
|
||||
std::string val = getVarLocation(callArgs[1], stackMap);
|
||||
if (isNumber(val)) result += " mov rdx, " + val + "\n";
|
||||
else result += " movsxd rdx, dword " + val + "\n";
|
||||
}
|
||||
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";
|
||||
|
||||
// ... (dalsze argumenty R8, R9 jeœli potrzebujesz)
|
||||
|
||||
result += " call " + funcName + "\n";
|
||||
break;
|
||||
}
|
||||
case OpType::RETURN: {
|
||||
@@ -427,41 +511,23 @@ std::string generateAssembly(const CompilerState& state) {
|
||||
break;
|
||||
}
|
||||
case OpType::MSGBOX: {
|
||||
// Konwencja Windows x64:
|
||||
// RCX = HWND (0 = brak okna nadrzêdnego)
|
||||
// RDX = TreϾ (Text)
|
||||
// R8 = Tytu³ (Caption)
|
||||
// R9 = Typ (0 = przycisk OK)
|
||||
|
||||
std::string title = instr.arg1;
|
||||
std::string text = instr.arg2;
|
||||
|
||||
// --- 1. Ustawiamy RDX (TreϾ) ---
|
||||
if (text.find("str_") == 0) {
|
||||
// Jeœli to litera³ (np. str_5), ³adujemy jego adres (LEA)
|
||||
result += " lea rdx, [rel " + text + "]\n";
|
||||
}
|
||||
if (text.find("str_") == 0) result += " lea rdx, [rel " + text + "]\n";
|
||||
else {
|
||||
// Jeœli to zmienna, pobieramy jej wartoœæ ze stosu (która jest adresem)
|
||||
std::string loc = getVarLocation(text, stackMap);
|
||||
result += " mov rdx, " + loc + "\n";
|
||||
}
|
||||
|
||||
// --- 2. Ustawiamy R8 (Tytu³) ---
|
||||
if (title.find("str_") == 0) {
|
||||
result += " lea r8, [rel " + title + "]\n";
|
||||
}
|
||||
if (title.find("str_") == 0) result += " lea r8, [rel " + title + "]\n";
|
||||
else {
|
||||
std::string loc = getVarLocation(title, stackMap);
|
||||
result += " mov r8, " + loc + "\n";
|
||||
}
|
||||
|
||||
// --- 3. Pozosta³e argumenty (Sta³e) ---
|
||||
result += " mov rcx, 0\n"; // HWND = NULL
|
||||
result += " mov r9, 0\n"; // MB_OK
|
||||
|
||||
// --- 4. Wywo³anie ---
|
||||
// Stos (shadow space) jest ju¿ przygotowany na pocz¹tku funkcji (sub rsp, 256)
|
||||
result += " mov rcx, 0\n";
|
||||
result += " mov r9, 0\n";
|
||||
result += " call MessageBoxA\n";
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -16,11 +16,11 @@ enum class OpType {
|
||||
MOD, // a = b % c
|
||||
EQ, // a == b
|
||||
PRINT, // print(int)
|
||||
PRINT_STRING, // print(string) - NOWE
|
||||
PRINT_STRING, // print(string)
|
||||
JMP_FALSE, // if (false) skocz...
|
||||
ARRAY_DECLARE, // int t[10];
|
||||
ARRAY_SET, // t[0] = 5;
|
||||
ARRAY_GET, // x = t[0]; - to obs³u¿ymy w ASSIGN, ale warto mieæ typ
|
||||
ARRAY_GET, // x = t[0];
|
||||
JMP, // else / pêtla
|
||||
LOGIC_AND, // &&
|
||||
LOGIC_OR, // ||
|
||||
@@ -28,6 +28,9 @@ enum class OpType {
|
||||
CALL, // wywo³anie funkcji
|
||||
RETURN, // return x
|
||||
MSGBOX, // msg box
|
||||
ALLOC_OBJECT, // alokacja obiektu na stosie - NOWE
|
||||
STORE_FIELD, // zapis do pola obiektu - NOWE
|
||||
LOAD_FIELD, // odczyt z pola obiektu - NOWE
|
||||
NOP // pusta instrukcja
|
||||
};
|
||||
|
||||
@@ -47,11 +50,37 @@ struct Function {
|
||||
std::vector<Instruction> instructions;
|
||||
};
|
||||
|
||||
// Definicja pola klasy
|
||||
struct ClassField {
|
||||
std::string name;
|
||||
std::string type; // "int", "string", itp.
|
||||
int offset; // offset w bajtach od pocz¹tku obiektu
|
||||
};
|
||||
|
||||
// Definicja metody klasy
|
||||
struct ClassMethod {
|
||||
std::string name;
|
||||
std::string returnType;
|
||||
std::vector<std::string> args;
|
||||
std::string mangledName; // np. "User_setAge"
|
||||
};
|
||||
|
||||
// Definicja klasy
|
||||
struct ClassDef {
|
||||
std::string name;
|
||||
std::vector<ClassField> fields;
|
||||
std::vector<ClassMethod> methods;
|
||||
int totalSize; // rozmiar ca³ego obiektu w bajtach
|
||||
};
|
||||
|
||||
// G£ÓWNY STAN KOMPILATORA
|
||||
struct CompilerState {
|
||||
// Mapa funkcji
|
||||
std::map<std::string, Function> functions;
|
||||
|
||||
// Mapa klas (NOWE)
|
||||
std::map<std::string, ClassDef> classes;
|
||||
|
||||
// Zmienne globalne
|
||||
std::map<std::string, int> globals;
|
||||
|
||||
@@ -61,6 +90,7 @@ struct CompilerState {
|
||||
|
||||
// Stan parsera
|
||||
Function* currentFunction = nullptr;
|
||||
std::string currentClass = ""; // (NOWE) - nazwa aktualnie parsowanej klasy
|
||||
int labelCounter = 0;
|
||||
|
||||
// Stosy bloków
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user