Incomplete, but I want to make sure the ideas I have here don't get wiped. Sadly this commit won't compile.
This commit is contained in:
+82
-86
@@ -10,8 +10,7 @@ int CurrentToken = 0;
|
||||
//int HeapStart = 4; // zero indexed, used for declared variables.
|
||||
//int BinaryEnd = 4; // Zero indexed (binary starts with a 4 byte header)
|
||||
|
||||
IROpcode* CreateIROpcode(TokenType mnemonic);
|
||||
Symbol* AddSymbol(Token* token, int length, int address, int resolved);
|
||||
IROpcode* CreateIROpcode(Mnemonic mnemonic);
|
||||
void PrintSymbols(void);
|
||||
void HandleOperation(void);
|
||||
void HandleAssemblerDirective(void);
|
||||
@@ -19,7 +18,6 @@ void AdvanceParser(void);
|
||||
Token* PeekToken(void);
|
||||
int ParserAtEnd(void);
|
||||
int Expect(int, ...);
|
||||
Symbol* GetSymbol(char*);
|
||||
//List* SymbolsTable;
|
||||
unsigned int HeapTop = 4;
|
||||
unsigned int ProgramCounter = 0;
|
||||
@@ -35,24 +33,22 @@ IRState* ParseTokens(List* tokens) {
|
||||
|
||||
TokensList = tokens;
|
||||
|
||||
MachineState.SymbolsTable = CreateList();
|
||||
MachineState.SymbolsTable = CreateSymbolTable();
|
||||
MachineState.Opcodes = CreateList();
|
||||
|
||||
while(!ParserAtEnd()) {
|
||||
Token* t = PeekToken();
|
||||
|
||||
switch(t->token_class) {
|
||||
case Directive:
|
||||
switch(t->Class) {
|
||||
case DirectiveClass:
|
||||
HandleAssemblerDirective();
|
||||
break;
|
||||
case Mnemonic:
|
||||
case MnemonicClass:
|
||||
HandleOperation();
|
||||
break;
|
||||
case Address:
|
||||
if (t->type == LABEL) {
|
||||
printf("%s: \n", t->lexeme);
|
||||
AddSymbol(t, 2, ProgramCounter, 1);
|
||||
}
|
||||
case LabelClass:
|
||||
printf("%s: \n", t->Lemexe);
|
||||
AddSymbol(t, 2, ProgramCounter, 1);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -61,7 +57,7 @@ IRState* ParseTokens(List* tokens) {
|
||||
AdvanceParser();
|
||||
}
|
||||
|
||||
if (MachineState.SymbolsTable->size > 0) PrintSymbols();
|
||||
if (MachineState.SymbolsTable->Size > 0) PrintSymbols();
|
||||
|
||||
//DestroyList(SymbolsTable);
|
||||
|
||||
@@ -85,22 +81,22 @@ IRState* ParseTokens(List* tokens) {
|
||||
|
||||
void HandleAssemblerDirective(void) {
|
||||
|
||||
if (PeekToken()->type == DB) {
|
||||
if (PeekToken()->Value.Directive == DB) {
|
||||
AdvanceParser();
|
||||
|
||||
Token* identifier = PeekToken();
|
||||
|
||||
if (!Expect(1, IDENTIFIER)) {
|
||||
fprintf(stderr, "Expected identifier on line %d\n", identifier->line);
|
||||
if (!Expect(1, IdentifierClass)) {
|
||||
fprintf(stderr, "Expected identifier on line %d\n", identifier->LineNumber);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
Symbol* symbol = AddSymbol(identifier, 0, HeapTop, 1);
|
||||
Symbol* symbol = AddSymbolToTable(identifier->Lemexe, NULL, strlen(identifier->Lemexe), MachineState.SymbolsTable);
|
||||
|
||||
identifier = PeekToken();
|
||||
|
||||
switch(identifier->type) {
|
||||
case STRING:
|
||||
switch(identifier->Class) {
|
||||
case CharacterClass:
|
||||
//printf("[INFO] %s = '%s' (%#04X)\n", identifier->lexeme, identifier->lexeme, HeapTop);
|
||||
|
||||
// for(int i = 0; i < strlen(identifier->lexeme); i++) {
|
||||
@@ -109,16 +105,16 @@ void HandleAssemblerDirective(void) {
|
||||
// }
|
||||
|
||||
//ProgramCounter += strlen(PeekToken()->lexeme);
|
||||
HeapTop += strlen(identifier->lexeme);
|
||||
symbol->length = strlen(identifier->lexeme);
|
||||
HeapTop += strlen(identifier->Lemexe);
|
||||
symbol->Length = strlen(identifier->Lemexe);
|
||||
|
||||
AdvanceParser();
|
||||
|
||||
if (PeekToken()->type == COMMA) {
|
||||
if (PeekToken()->Class == PunctuationClass && PeekToken()->Value.Punctuation == Comma) {
|
||||
AdvanceParser();
|
||||
|
||||
if (PeekToken()-> type != NUMBER) {
|
||||
fprintf(stderr, "Expected string termination byte on line %d\n", PeekToken()->line);
|
||||
if (PeekToken()-> Class != NumberClass) {
|
||||
fprintf(stderr, "Expected string termination byte on line %d\n", PeekToken()->LineNumber);
|
||||
exit(1);
|
||||
}
|
||||
else {
|
||||
@@ -127,15 +123,15 @@ void HandleAssemblerDirective(void) {
|
||||
// identifier->lexeme = realloc(identifier->lexeme, oldLength + 2);
|
||||
// identifier->value = identifier->lexeme;
|
||||
// identifier->lexeme[oldLength + 1] = '\0';//*PeekToken()->lexeme & 0x0F;
|
||||
symbol->length++; //Basically, read in the NULL byte at the end of the string.
|
||||
symbol->Length++; //Basically, read in the NULL byte at the end of the string.
|
||||
HeapTop++;
|
||||
return;
|
||||
}
|
||||
}
|
||||
case NUMBER:
|
||||
case NumberClass:
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "[Line: %d] Expected either a number or string after identifier '%s'.\n", PeekToken()->line, identifier->lexeme);
|
||||
fprintf(stderr, "[Line: %d] Expected either a number or string after identifier '%s'.\n", PeekToken()->LineNumber, identifier->Lemexe);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
@@ -148,42 +144,42 @@ void HandleRegisterBasedOpcode(unsigned char mask) {
|
||||
|
||||
Token* token = PeekToken();
|
||||
|
||||
IROpcode* opcode = CreateIROpcode(token->type);
|
||||
IROpcode* opcode = CreateIROpcode(token->Value.Mnemonic);
|
||||
|
||||
AdvanceParser();
|
||||
|
||||
token = PeekToken();
|
||||
|
||||
if (token->token_class == Reg) {
|
||||
if (token->Class == RegisterClass) {
|
||||
//Memory[ProgramCounter - 1] |= (PeekToken()->type - R1) & 0x07;
|
||||
opcode->ParameterOneType = Reg;
|
||||
opcode->ParameterOneType = RegisterClass;
|
||||
AdvanceParser();
|
||||
}
|
||||
else if (token->token_class == Address && token->type == COPY) {
|
||||
opcode->ParameterOneType = Address;
|
||||
else if (token->Class & AddressClass && token->Value.Mnemonic == COPY) {
|
||||
//opcode->ParameterOneType = Address;
|
||||
|
||||
Symbol* symbol = GetSymbol(token->lexeme);
|
||||
Symbol* symbol = GetSymbol(token->Lemexe);
|
||||
|
||||
if (!symbol) opcode->ParameterOne.Address = AddSymbol(token, 2, 0, 0);
|
||||
else opcode->ParameterOne.Address = symbol;//CreateSymbol(token, symbol->address, symbol->resolved);
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "[Error] Line %d: Expected register operand.\n", token->line);
|
||||
fprintf(stderr, "[Error] Line %d: Expected register operand.\n", token->LineNumber);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
token = PeekToken();
|
||||
|
||||
if (!Expect(1, COMMA)) {
|
||||
fprintf(stderr, "[Error] Expected comma on line %d.\n", token->line);
|
||||
if (!Expect(1, Comma)) {
|
||||
fprintf(stderr, "[Error] Expected comma on line %d.\n", token->LineNumber);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
token = PeekToken();
|
||||
|
||||
if (token->type == IDENTIFIER || token->type == LABEL) {
|
||||
opcode->ParameterTwoType = Address;
|
||||
Symbol* symbol = GetSymbol(token->lexeme);
|
||||
if (token->Class & AddressClass) {
|
||||
opcode->ParameterTwoType = IdentifierClass;
|
||||
Symbol* symbol = GetSymbol(token->Lemexe);
|
||||
|
||||
// if (!symbol) {
|
||||
// fprintf(stderr, "[Error] Line %d: %s is undefined.\n", token->line, token->lexeme);
|
||||
@@ -199,10 +195,10 @@ void HandleRegisterBasedOpcode(unsigned char mask) {
|
||||
|
||||
AdvanceParser();
|
||||
}
|
||||
else if (token->type == NUMBER) {
|
||||
opcode->ParameterTwoType = Constant;
|
||||
else if (token->Class == NumberClass) {
|
||||
//opcode->ParameterTwoType = ;
|
||||
|
||||
opcode->ParameterTwo.Constant = *(int*)PeekToken()->value;
|
||||
opcode->ParameterTwo.Constant = PeekToken()->Value.Number;
|
||||
// Memory[ProgramCounter - 1] |= 0x08; //0b00001000;
|
||||
|
||||
// int* value = PeekToken()->value;
|
||||
@@ -212,9 +208,9 @@ void HandleRegisterBasedOpcode(unsigned char mask) {
|
||||
|
||||
AdvanceParser();
|
||||
}
|
||||
else if (token->token_class == Reg) {
|
||||
opcode->ParameterTwoType = Reg;
|
||||
opcode->ParameterTwo.Constant = (PeekToken()->type - R1) &0x07;
|
||||
else if (token->Class == RegisterClass) {
|
||||
//opcode->ParameterTwoType = ;
|
||||
opcode->ParameterTwo.Constant = PeekToken()->Value.Register &0x07;
|
||||
//Memory[ProgramCounter] = (PeekToken()->type - R1) & 0x07;
|
||||
|
||||
AdvanceParser();
|
||||
@@ -223,7 +219,7 @@ void HandleRegisterBasedOpcode(unsigned char mask) {
|
||||
return;
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "[Error] Expected operand, got %s\n", token->lexeme);
|
||||
fprintf(stderr, "[Error] Expected operand, got %s\n", token->Lemexe);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -241,22 +237,22 @@ void HandleOperation(void) {
|
||||
|
||||
// AdvanceParser();
|
||||
|
||||
unsigned char mask = GetInstructionMask(token->type);
|
||||
unsigned char mask = GetInstructionMask(token->Value.Mnemonic);
|
||||
|
||||
if (mask & registerBasedOpcodeMask) {
|
||||
HandleRegisterBasedOpcode(mask);
|
||||
return;
|
||||
}
|
||||
|
||||
IROpcode* opcode = CreateIROpcode(token->type);
|
||||
opcode->ParameterTwoType = None;
|
||||
IROpcode* opcode = CreateIROpcode(token->Value.Mnemonic);
|
||||
//opcode->ParameterTwoType = None;
|
||||
|
||||
AdvanceParser();
|
||||
|
||||
switch (mask) {
|
||||
case 0: //NOP
|
||||
//Memory[ProgramCounter] = 0x00;
|
||||
opcode->ParameterOneType = None;
|
||||
//opcode->ParameterOneType = None;
|
||||
|
||||
ProgramCounter++;
|
||||
return;
|
||||
@@ -265,9 +261,9 @@ void HandleOperation(void) {
|
||||
|
||||
ProgramCounter++;
|
||||
|
||||
if (PeekToken()->type == IDENTIFIER || PeekToken()->type == LABEL) {
|
||||
Symbol* symbol = GetSymbol(PeekToken()->lexeme);
|
||||
opcode->ParameterOneType = Address;
|
||||
if (PeekToken()->Class == IdentifierClass || PeekToken()->Class == LabelClass) {
|
||||
Symbol* symbol = GetSymbol(PeekToken()->Lemexe);
|
||||
//opcode->ParameterOneType = Address;
|
||||
|
||||
if (!symbol) opcode->ParameterOne.Address = AddSymbol(PeekToken(), 2, 0, 0);
|
||||
else opcode->ParameterOne.Address = symbol;
|
||||
@@ -279,16 +275,16 @@ void HandleOperation(void) {
|
||||
// Memory[ProgramCounter] = 2 >> symbol->address & 0xFF;
|
||||
// Memory[ProgramCounter + 1] = symbol->address & 0xFF;
|
||||
}
|
||||
else if (PeekToken()->type == NUMBER) {
|
||||
opcode->ParameterOneType = Constant;
|
||||
opcode->ParameterOne.Constant = *(int*)PeekToken()->value;
|
||||
else if (PeekToken()->Class == NumberClass) {
|
||||
//opcode->ParameterOneType = Constant;
|
||||
opcode->ParameterOne.Constant = PeekToken()->Value.Number;
|
||||
// int* value = PeekToken()->value;
|
||||
|
||||
// Memory[ProgramCounter] = 2 >> *value & 0xFF;
|
||||
// Memory[ProgramCounter + 1] = *value & 0xFF;
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "[Error] Line: %d: Expected address after jump if zero (JZ) instruction.\n", ((Token*) TokensList->content[CurrentToken - 1])->line);
|
||||
fprintf(stderr, "[Error] Line: %d: Expected address after jump if zero (JZ) instruction.\n", ((Token*) TokensList->content[CurrentToken - 1])->LineNumber);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -300,16 +296,16 @@ void HandleOperation(void) {
|
||||
|
||||
ProgramCounter++;
|
||||
|
||||
if (PeekToken()->type == NUMBER) {
|
||||
if (PeekToken()->Class == NumberClass) {
|
||||
// int* value = PeekToken()->value;
|
||||
opcode->ParameterOneType = Constant;
|
||||
opcode->ParameterOne.Constant = *(int*)PeekToken()->value;
|
||||
//opcode->ParameterOneType = ;
|
||||
opcode->ParameterOne.Constant = PeekToken()->Value.Number;
|
||||
|
||||
// Memory[ProgramCounter] = 2 >> *value & 0xFF;
|
||||
// Memory[ProgramCounter + 1] = *value & 0xFF;
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "[Error] Line %d: Expected Interrupt vector.\n", PeekToken()->line);
|
||||
fprintf(stderr, "[Error] Line %d: Expected Interrupt vector.\n", PeekToken()->LineNumber);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -318,13 +314,13 @@ void HandleOperation(void) {
|
||||
return;
|
||||
case 3: //YLD
|
||||
//Memory[ProgramCounter] = 0x03;
|
||||
opcode->ParameterOneType = None;
|
||||
//opcode->ParameterOneType = ;
|
||||
|
||||
ProgramCounter++;
|
||||
break;
|
||||
case 4: //RET
|
||||
//Memory[ProgramCounter] = 0x04;
|
||||
opcode->ParameterOneType = None;
|
||||
//opcode->ParameterOneType = ;
|
||||
|
||||
ProgramCounter++;
|
||||
break;
|
||||
@@ -333,9 +329,9 @@ void HandleOperation(void) {
|
||||
|
||||
ProgramCounter++;
|
||||
|
||||
if (PeekToken()->type == IDENTIFIER || PeekToken()->type == LABEL) {
|
||||
Symbol* symbol = GetSymbol(PeekToken()->lexeme);
|
||||
opcode->ParameterOneType = Address;
|
||||
if (PeekToken()->Class == IdentifierClass || PeekToken()->Class == LabelClass) {
|
||||
Symbol* symbol = GetSymbol(PeekToken()->Lemexe);
|
||||
//opcode->ParameterOneType = ;
|
||||
|
||||
// if (!symbol) {
|
||||
// fprintf(stderr, "[Error] Line %d: %s is undefined.\n", PeekToken()->line, PeekToken()->lexeme);
|
||||
@@ -347,7 +343,7 @@ void HandleOperation(void) {
|
||||
//Memory[ProgramCounter + 1] = symbol->address & 0xFF;
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "[Error] Line %d: Expected address or label.\n", PeekToken()->line);
|
||||
fprintf(stderr, "[Error] Line %d: Expected address or label.\n", PeekToken()->LineNumber);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -358,9 +354,9 @@ void HandleOperation(void) {
|
||||
|
||||
ProgramCounter++;
|
||||
|
||||
if (PeekToken()->type == IDENTIFIER || PeekToken()->type == LABEL) {
|
||||
Symbol* symbol = GetSymbol(PeekToken()->lexeme);
|
||||
opcode->ParameterOneType = Address;
|
||||
if (PeekToken()->Class == IdentifierClass || PeekToken()->Class == LabelClass) {
|
||||
Symbol* symbol = GetSymbol(PeekToken()->Lemexe);
|
||||
//opcode->ParameterOneType = Address;
|
||||
|
||||
// if (!symbol) {
|
||||
// fprintf(stderr, "[Error] Line %d: %s is undefined.\n", PeekToken()->line, PeekToken()->lexeme);
|
||||
@@ -373,7 +369,7 @@ void HandleOperation(void) {
|
||||
// Memory[ProgramCounter + 1] = symbol->address & 0xFF;
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "[Error] Line %d: Expected address or label.\n", PeekToken()->line);
|
||||
fprintf(stderr, "[Error] Line %d: Expected address or label.\n", PeekToken()->LineNumber);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -384,16 +380,16 @@ void HandleOperation(void) {
|
||||
|
||||
ProgramCounter++;
|
||||
|
||||
if (PeekToken()->type == NUMBER) {
|
||||
opcode->ParameterOneType = Constant;
|
||||
opcode->ParameterOne.Constant = *(int*)PeekToken()->value;
|
||||
if (PeekToken()->Class == NumberClass) {
|
||||
//opcode->ParameterOneType = ;
|
||||
opcode->ParameterOne.Constant = PeekToken()->Value.Number;
|
||||
// int* value = PeekToken()->value;
|
||||
|
||||
// Memory[ProgramCounter] = 2 >> *value & 0xFF;
|
||||
// Memory[ProgramCounter + 1] = *value & 0xFF;
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "[Error] Line %d: Expected port number.\n", PeekToken()->line);
|
||||
fprintf(stderr, "[Error] Line %d: Expected port number.\n", PeekToken()->LineNumber);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -404,16 +400,16 @@ void HandleOperation(void) {
|
||||
|
||||
ProgramCounter++;
|
||||
|
||||
if (PeekToken()->type == NUMBER) {
|
||||
opcode->ParameterOneType = Constant;
|
||||
opcode->ParameterOne.Constant = *(int*)PeekToken()->value;
|
||||
if (PeekToken()->Class == NumberClass) {
|
||||
//opcode->ParameterOneType = ;
|
||||
opcode->ParameterOne.Constant = PeekToken()->Value.Number;
|
||||
// int* value = PeekToken()->value;
|
||||
|
||||
// Memory[ProgramCounter] = 2 >> *value & 0xFF;
|
||||
// Memory[ProgramCounter + 1] = *value & 0xFF;
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "[Error] Line %d: Expected port number.\n", PeekToken()->line);
|
||||
fprintf(stderr, "[Error] Line %d: Expected port number.\n", PeekToken()->LineNumber);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -433,7 +429,7 @@ int Expect(int count, ...) {
|
||||
va_start(list, count);
|
||||
|
||||
for(int i = 0; i < count; i++) {
|
||||
if (va_arg(list, TokenType) == token->type) {
|
||||
if (va_arg(list, TokenClass) == token->Class) {
|
||||
va_end(list);
|
||||
AdvanceParser();
|
||||
return 1;
|
||||
@@ -449,21 +445,21 @@ void PrintSymbols(void) {
|
||||
printf("-----SYMBOLS-----\n");
|
||||
for(int i = 0; i < MachineState.SymbolsTable->size; i++) {
|
||||
Symbol* symbol = MachineState.SymbolsTable->content[i];
|
||||
printf("[%#06X] %s\n", symbol->address, symbol->token->lexeme);
|
||||
printf("[%#06X] %s\n", symbol->address, symbol->token->Lemexe);
|
||||
}
|
||||
printf("-----SYMBOLS-----\n");
|
||||
}
|
||||
|
||||
Symbol* AddSymbol(Token* token, int length, int address, int resolved) {
|
||||
if (!token) return NULL;
|
||||
if (token->type != IDENTIFIER && token->type != LABEL) return NULL;
|
||||
if (token->Class != IdentifierClass && token->Class != LabelClass) return NULL;
|
||||
|
||||
for(int i = 0; i < MachineState.SymbolsTable->size; i++) {
|
||||
Symbol* s = MachineState.SymbolsTable->content[i];
|
||||
|
||||
if (strcmp(s->token->lexeme, token->lexeme) == 0) {
|
||||
if (strcmp(s->token->Lemexe, token->Lemexe) == 0) {
|
||||
if (s->resolved) {
|
||||
fprintf(stderr, "[Error] Line %d: Attempted to redeclare %s.\n", token->line, token->lexeme);
|
||||
fprintf(stderr, "[Error] Line %d: Attempted to redeclare %s.\n", token->LineNumber, token->Lemexe);
|
||||
exit(1);
|
||||
}
|
||||
s->resolved = resolved;
|
||||
@@ -486,7 +482,7 @@ Symbol* GetSymbol(char* name) {
|
||||
for(int i = 0; i < MachineState.SymbolsTable->size; i++) {
|
||||
Symbol* s = MachineState.SymbolsTable->content[i];
|
||||
|
||||
if (strcmp(s->token->lexeme, name) == 0) return s;
|
||||
if (strcmp(s->token->Lemexe, name) == 0) return s;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
@@ -504,7 +500,7 @@ Symbol* CreateSymbol(Token* token, int length, int address, int resolved) {
|
||||
return symbol;
|
||||
}
|
||||
|
||||
IROpcode* CreateIROpcode(TokenType mnemonic) {
|
||||
IROpcode* CreateIROpcode(Mnemonic mnemonic) {
|
||||
IROpcode* opcode = calloc(1, sizeof(IROpcode));
|
||||
|
||||
opcode->Mnemonic = mnemonic;
|
||||
|
||||
Reference in New Issue
Block a user