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:
2022-09-15 21:29:27 +00:00
parent 6d17dffcdf
commit 2ae60a607c
8 changed files with 238 additions and 210 deletions
+33 -17
View File
@@ -2,30 +2,46 @@
#define OPCODES_H
#include <string.h>
#include "token.h"
#include "symbols_table.h"
#define OPCODECOUNT 13
#define REGISTERCOUNT 8
//const unsigned char COPYADDRESSMASK = 0x80;
typedef enum {
R1, R2, R3, R4, R5, R6, R7, R8
} Registers;
typedef enum {
COPY, ADD, SUB, JZ, INT, YLD, RET,
CMP, NOP, JMP, CALL, IN, OUT,
} Mnemonic;
typedef enum {
None,
Constant,
Address
} OpcodeParameter;
typedef struct {
char* lexeme;
TokenType op;
TokenClass parameter_one;
TokenClass parameter_two;
Mnemonic Mnemonic;
OpcodeParameter ParameterOneType;
OpcodeParameter ParameterTwoType;
union {
int Constant;
Symbol* Symbol;
} ParameterOne;
union {
int Constant;
Symbol* Symbol;
} ParameterTwo;
} Instruction;
typedef struct {
char* lexeme;
TokenType type;
} Register;
extern Instruction instructions[OPCODECOUNT];
extern Register registers[REGISTERCOUNT];
int IsOpcode(const char*, TokenType*);
int IsRegister(const char*, TokenType*);
const Instruction* GetOpcodeDetails(TokenType);
unsigned char GetInstructionMask(TokenType type);
int IsOpcode(const char*, Mnemonic*);
int IsRegister(const char*, Registers*);
//const Instruction* GetOpcodeDetails(Mnemonic);
unsigned char GetInstructionMask(Mnemonic type);
#endif
+9 -8
View File
@@ -7,18 +7,19 @@
#include "list.h"
#include "token.h"
#include "opcodes.h"
#include "symbols_table.h"
#define HIGHMEMORY 65535 //64KiB - 1 AKA 0xFFFF
typedef struct {
Token* token;
int length;
int address;
int resolved;
} Symbol;
// typedef struct {
// Token* token;
// int length;
// int address;
// int resolved;
// } Symbol;
typedef struct {
TokenType Mnemonic;
Mnemonic Mnemonic;
TokenClass ParameterOneType;
TokenClass ParameterTwoType;
@@ -35,7 +36,7 @@ typedef struct {
typedef struct {
List* Opcodes;
List* SymbolsTable;
SymbolTable* SymbolsTable;
} IRState;
IRState* ParseTokens(List*);
+1
View File
@@ -12,6 +12,7 @@ typedef struct {
void* Value;
char* Name;
int Length;
int Resolved;
} Symbol;
typedef struct {
+40 -36
View File
@@ -5,50 +5,54 @@
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include "opcodes.h"
typedef enum {
PLUS,
MINUS,
STAR,
SLASH,
POWER,
LPARAM,
RPARAM,
LBRACKET,
RBracket,
COMMA,
STRING,
IDENTIFIER,
LABEL,
NUMBER,
LineEnd,
//Keywords
DB, ORG,
//Mnemonics
COPY, ADD, SUB, JZ, INT, YLD, RET,
CMP, NOP, JMP, CALL, IN, OUT,
//Registers
R1, R2, R3, R4, R5, R6, R7, R8
} TokenType;
Plus = '+',
Minus = '-',
Star = '*',
Slash = '/',
Power = '^',
LParan = '(',
RParan = ')',
LBracket = '[',
RBracket = ']',
Comma = ',',
NewLine = '\n'
} TokenPunctuation;
typedef enum {
None = 0,
Reg = 1,
Constant = 2,
Address = 4,
Mnemonic = 8,
Directive = 16
DB,
Origin
} Directive;
typedef enum {
RegisterClass = 0,
NumberClass = 1,
CharacterClass = 2,
MnemonicClass = 4,
DirectiveClass = 8,
PunctuationClass = 16,
IdentifierClass = 32,
LabelClass = 64,
AddressClass = LabelClass | IdentifierClass
} TokenClass;
typedef struct {
TokenType type;
char* lexeme;
void* value;
int line;
TokenClass token_class;
char* Lemexe;
TokenClass Class;
int LineNumber;
union {
TokenPunctuation Punctuation;
Registers Register;
Mnemonic Mnemonic;
Directive Directive;
int Number;
} Value;
} Token;
Token* CreateToken(char*, void*, int, TokenType);
void FreeToken(Token*);
Token* CreateToken(int lineNumber, TokenClass tokenClass);
void FreeToken(Token* token);
#endif