Hooked up the program counter in the parser to set the memory offset of symbols, well labels to be more acurrate.
This commit is contained in:
+13
-1
@@ -9,6 +9,7 @@
|
||||
#include "../includes/parser.h"
|
||||
|
||||
List* LIST;
|
||||
SymbolTable* Symbols;
|
||||
|
||||
void print(void);
|
||||
|
||||
@@ -27,12 +28,23 @@ int main(int argc, char* args[]) {
|
||||
|
||||
LIST = GenerateTokenList(source_code);
|
||||
|
||||
ParseTokens(LIST);
|
||||
IRState* state = ParseTokens(LIST);
|
||||
|
||||
Symbols = state->SymbolsTable;
|
||||
|
||||
free(source_code);
|
||||
}
|
||||
|
||||
// void assemble() {
|
||||
// for(int i = 0; i < LIST->size; i++) {
|
||||
// Token* current = LIST->content[i];
|
||||
|
||||
// if (current->Class != MnemonicClass) continue;
|
||||
|
||||
|
||||
// }
|
||||
// }
|
||||
|
||||
void print(void) {
|
||||
char mnemonic[12];
|
||||
printf("printing tokens...\n");
|
||||
|
||||
+58
-7
@@ -26,7 +26,7 @@ Symbol* ExpectIdentifier(int resolved, ExpectOptions options);
|
||||
void ExpectCharacterClass(Symbol* symbol);
|
||||
void ExpectLineEndOrFileEnd(ExpectOptions options);
|
||||
|
||||
int HeapSize = 0;
|
||||
//int HeapSize = 0;
|
||||
int PC = 0;
|
||||
|
||||
IRState* ParseTokens(List* tokens) {
|
||||
@@ -58,8 +58,12 @@ IRState* ParseTokens(List* tokens) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (!found) AddSymbolToTable(t->Lemexe, HeapSize, 1, MachineState.SymbolsTable);
|
||||
else symbol->Resolved = 1;
|
||||
if (!found) AddSymbolToTable(t->Lemexe, PC, 1, MachineState.SymbolsTable);
|
||||
else
|
||||
{
|
||||
symbol->Address = PC;
|
||||
symbol->Resolved = 1;
|
||||
}
|
||||
|
||||
RemoveCurrentToken(); //label
|
||||
|
||||
@@ -101,11 +105,12 @@ void HandleAssemblerDirective() {
|
||||
}
|
||||
else if (PeekToken()->Class == IdentifierClass) {
|
||||
symbol = ExpectIdentifier(0, RemoveExpected);
|
||||
symbol->Length = 2;
|
||||
|
||||
ExpectLineEndOrFileEnd(RemoveExpected);
|
||||
}
|
||||
|
||||
HeapSize += symbol->Length;
|
||||
//HeapSize += symbol->Length;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -138,6 +143,8 @@ void HandleOpcode(void) {
|
||||
arg = PeekToken();
|
||||
}
|
||||
|
||||
PC++;
|
||||
|
||||
if (arg->Class == RegisterClass) {
|
||||
ExpectRegister();
|
||||
|
||||
@@ -149,6 +156,8 @@ void HandleOpcode(void) {
|
||||
opcode->Value.Mnemonic = isByte ? COPYB : COPYI;
|
||||
opcode->Lemexe = isByte ? "copyb" : "copyi";
|
||||
|
||||
isByte ? PC++ : (PC += 2);
|
||||
|
||||
AdvanceParser();
|
||||
}
|
||||
else if (arg->Class == RegisterClass) {
|
||||
@@ -162,10 +171,13 @@ void HandleOpcode(void) {
|
||||
|
||||
opcode->Value.Mnemonic = COPY;
|
||||
opcode->Lemexe = "copy";
|
||||
|
||||
PC++;
|
||||
}
|
||||
else if (arg->Class & IdentifierClass) {
|
||||
opcode->Value.Mnemonic = isByte ? COPYAB : COPYA;
|
||||
opcode->Lemexe = isByte ? "copyab" : "copya";
|
||||
PC += 2;
|
||||
|
||||
ExpectIdentifier(0, ForwardParser);
|
||||
}
|
||||
@@ -177,7 +189,8 @@ void HandleOpcode(void) {
|
||||
ExpectPuncuation(RBracket, ForwardParser);
|
||||
|
||||
opcode->Value.Mnemonic = isByte ? COPYRAB : COPYRA;
|
||||
opcode->Lemexe = isByte ? "copyrab" : "copyra";
|
||||
opcode->Lemexe = isByte ? "copyrab" : "copyra";
|
||||
PC++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -197,6 +210,8 @@ void HandleOpcode(void) {
|
||||
|
||||
opcode->Value.Mnemonic = isByte ? COPYRARAB : COPYRARA;
|
||||
opcode->Lemexe = isByte ? "copyrarab" : "copyrara";
|
||||
|
||||
PC++;
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -208,20 +223,28 @@ void HandleOpcode(void) {
|
||||
|
||||
TokenClass class = PeekToken()->Class;
|
||||
|
||||
PC++;
|
||||
|
||||
if (class == NumberClass) {
|
||||
opcode->Value.Mnemonic = CMPI;
|
||||
opcode->Lemexe = "CMPI";
|
||||
|
||||
AdvanceParser();
|
||||
|
||||
PC += 2;
|
||||
}
|
||||
else if (class & IdentifierClass) {
|
||||
opcode->Value.Mnemonic = CMPI;
|
||||
opcode->Lemexe = "CMPI";
|
||||
|
||||
ExpectIdentifier(0, ForwardParser);
|
||||
|
||||
PC += 2;
|
||||
}
|
||||
else {
|
||||
ExpectRegister();
|
||||
|
||||
PC++;
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -230,14 +253,19 @@ void HandleOpcode(void) {
|
||||
case AND:
|
||||
case OR:
|
||||
case XOR:
|
||||
PC++;
|
||||
|
||||
ExpectRegister();
|
||||
|
||||
ExpectPuncuation(Comma, ForwardParser);
|
||||
|
||||
ExpectRegister();
|
||||
|
||||
PC++;
|
||||
break;
|
||||
case SHL:
|
||||
case SHR:
|
||||
PC++;
|
||||
ExpectRegister();
|
||||
|
||||
ExpectPuncuation(Comma, ForwardParser);
|
||||
@@ -249,15 +277,20 @@ void HandleOpcode(void) {
|
||||
}
|
||||
|
||||
AdvanceParser();
|
||||
|
||||
PC += 2;
|
||||
break;
|
||||
case INC:
|
||||
case DEC:
|
||||
case PUSH:
|
||||
case POP:
|
||||
PC++;
|
||||
ExpectRegister();
|
||||
break;
|
||||
case JMP:
|
||||
{
|
||||
PC++;
|
||||
|
||||
Token* arg = PeekToken();
|
||||
|
||||
if (arg->Class & IdentifierClass) {
|
||||
@@ -265,6 +298,8 @@ void HandleOpcode(void) {
|
||||
|
||||
opcode->Value.Mnemonic = JMP;
|
||||
opcode->Lemexe = "jmp";
|
||||
|
||||
PC += 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -281,29 +316,44 @@ void HandleOpcode(void) {
|
||||
break;
|
||||
case JZ:
|
||||
{
|
||||
PC++;
|
||||
|
||||
ExpectIdentifier(0, ForwardParser);
|
||||
|
||||
PC += 2;
|
||||
|
||||
opcode->Value.Mnemonic = JZ;
|
||||
opcode->Lemexe = "jz";
|
||||
}
|
||||
break;
|
||||
case JG:
|
||||
{
|
||||
PC++;
|
||||
|
||||
ExpectIdentifier(0, ForwardParser);
|
||||
|
||||
PC += 2;
|
||||
|
||||
opcode->Value.Mnemonic = JG;
|
||||
opcode->Lemexe = "jg";
|
||||
}
|
||||
break;
|
||||
case JL:
|
||||
{
|
||||
PC++;
|
||||
|
||||
ExpectIdentifier(0, ForwardParser);
|
||||
|
||||
PC += 2;
|
||||
|
||||
opcode->Value.Mnemonic = JL;
|
||||
opcode->Lemexe = "jl";
|
||||
}
|
||||
break;
|
||||
case CALL:
|
||||
{
|
||||
PC++;
|
||||
|
||||
Token* arg = PeekToken();
|
||||
|
||||
if ((arg->Class & IdentifierClass) == 0) {
|
||||
@@ -313,6 +363,8 @@ void HandleOpcode(void) {
|
||||
}
|
||||
|
||||
ExpectIdentifier(0, ForwardParser);
|
||||
|
||||
PC += 2;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -363,11 +415,10 @@ Symbol* ExpectIdentifier(int resolved, ExpectOptions options) {
|
||||
}
|
||||
|
||||
if (found) {
|
||||
symbol->Address = HeapSize;
|
||||
symbol->Resolved = resolved || symbol->Resolved;
|
||||
}
|
||||
else {
|
||||
symbol = AddSymbolToTable(token->Lemexe, HeapSize, resolved, MachineState.SymbolsTable);
|
||||
symbol = AddSymbolToTable(token->Lemexe, PC, resolved, MachineState.SymbolsTable);
|
||||
}
|
||||
|
||||
if (options & RemoveExpected) RemoveCurrentToken();
|
||||
|
||||
@@ -283,14 +283,6 @@ Token* ParseIdentifier(void) {
|
||||
|
||||
return token;
|
||||
}
|
||||
// if (strcmp(lexeme, "load") == 0) {
|
||||
// Token* token = CreateToken(Line, DirectiveClass);
|
||||
|
||||
// token->Lemexe = lexeme;
|
||||
// token->Value.Directive = Load;
|
||||
|
||||
// return token;
|
||||
// }
|
||||
if (strcmp(lexeme, "byte") == 0) {
|
||||
Token* token = CreateToken(Line, DirectiveClass);
|
||||
|
||||
@@ -299,14 +291,6 @@ Token* ParseIdentifier(void) {
|
||||
|
||||
return token;
|
||||
}
|
||||
// if (strcmp(lexeme, "store") == 0) {
|
||||
// Token* token = CreateToken(Line, DirectiveClass);
|
||||
|
||||
// token->Lemexe = lexeme;
|
||||
// token->Value.Directive = Store;
|
||||
|
||||
// return token;
|
||||
// }
|
||||
|
||||
Token* token = CreateToken(Line, IdentifierClass);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user