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