summaryrefslogtreecommitdiff
path: root/masm/parse.h
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2024-09-11 12:06:09 -0400
committerFreya Murphy <freya@freyacat.org>2024-09-11 12:06:09 -0400
commite3d2e31377030e84066bed4bbc04bf6c56206305 (patch)
treed5e7428212606a0f64d5b2e628b3f507948b8e2f /masm/parse.h
parentjoe (diff)
downloadmips-e3d2e31377030e84066bed4bbc04bf6c56206305.tar.gz
mips-e3d2e31377030e84066bed4bbc04bf6c56206305.tar.bz2
mips-e3d2e31377030e84066bed4bbc04bf6c56206305.zip
refactor
Diffstat (limited to '')
-rw-r--r--masm/parse.h141
1 files changed, 37 insertions, 104 deletions
diff --git a/masm/parse.h b/masm/parse.h
index ea8f929..9181899 100644
--- a/masm/parse.h
+++ b/masm/parse.h
@@ -9,135 +9,68 @@
#include <mips.h>
#include <stdint.h>
+///
+/// reference
+///
+
+enum reference_type {
+ REF_NONE,
+ REF_OFFESET,
+ REF_TARGET,
+};
+
+struct reference {
+ enum reference_type type;
+
+ /// symbol name
+ char name[MAX_LEX_LENGTH];
+
+ /// integer addend
+ int64_t addend;
+};
+
struct const_expr {
char name[MAX_LEX_LENGTH];
uint32_t value;
};
+struct ins_expr {
+ /// pesudo instructions can return
+ /// more than one instruction
+ size_t ins_len;
+ struct mips_instruction ins[2];
+
+ /// instructions can reference symbols.
+ /// instruction `n` will be paried with reference `n`
+ struct reference ref[2];
+};
+
enum expr_type {
- EXPR_INS,
EXPR_DIRECTIVE,
EXPR_CONSTANT,
+ EXPR_INS,
EXPR_LABEL,
};
struct expr {
enum expr_type type;
union {
- // instruction
- union mips_instruction ins;
// directive
- union mips_directive directive;
+ struct mips_directive directive;
// constant
struct const_expr constant;
- // segment or label
- char text[MAX_LEX_LENGTH];
- };
-};
-
-enum section_entry_type {
- ENT_INS,
- ENT_WORD,
- ENT_HALF,
- ENT_BYTE,
- ENT_NO_DATA,
-};
-
-struct section_entry {
- enum section_entry_type type;
- size_t size;
-
- union {
- char data; // to get memory address
- union mips_instruction ins;
- int32_t word;
- int16_t half;
- int8_t byte;
+ // instruction
+ struct ins_expr ins;
+ // label
+ char label[MAX_LEX_LENGTH];
};
};
-struct section {
- uint32_t count;
- uint32_t len;
- uint32_t alignment;
- uint32_t index; // what index is my section
- char name[MAX_LEX_LENGTH];
- bool read;
- bool write;
- bool execute;
- struct section_entry *entries;
-};
-
-struct section_table {
- uint32_t count;
- uint32_t len;
- struct section *sections;
- struct section *current;
- char name[MAX_LEX_LENGTH];
-};
-
-int sectbl_init(struct section_table *sec_tbl);
-void sectbl_free(struct section_table *sec_tbl);
-
-int sectbl_alloc(struct section_table *sec_tbl, struct section **sec,
- const char name[MAX_LEX_LENGTH]);
-int sectbl_get(struct section_table *sec_tbl, struct section **sec,
- const char name[MAX_LEX_LENGTH]);
-int sec_push(struct section *section, struct section_entry entry);
-size_t sec_size(struct section *section);
-size_t sec_index(struct section *section, uint32_t index);
-
-enum reference_type {
- REF_OFFESET,
- REF_TARGET,
-};
-
-struct reference {
- enum reference_type type;
- struct section *section;
- uint32_t index;
- char name[MAX_LEX_LENGTH];
-};
-
-struct reference_table {
- uint32_t count;
- uint32_t len;
- struct reference *references;
-};
-
-int reftbl_init(struct reference_table *ref_tbl);
-void reftbl_free(struct reference_table *ref_tbl);
-int reftbl_push(struct reference_table *ref_tbl, struct reference reference);
-
struct parser {
struct lexer *lexer;
struct token peek;
-
- // sections
- struct section_table sec_tbl;
-
- // references
- struct reference_table ref_tbl;
-
- int (*parse_instruction)(struct parser *, union mips_instruction *,
- struct token);
- int (*parse_directive)(struct parser *, union mips_directive *);
- int (*is_instruction)(const char *ident);
};
-/* get the next token in the parser */
-int next_token(struct parser *parser, struct token *tok);
-
-/* peek the next token in the parser */
-int peek_token(struct parser *parser, struct token *tok);
-
-/* assert the next token is a specific type */
-int assert_token(struct parser *parser, enum token_type type,
- struct token *tok);
-
-/* assert the next token is EOF or NL */
-int assert_eol(struct parser *parser);
-
/* get the next expression in the parser */
int parser_next(struct parser *parser, struct expr *expr);