summaryrefslogtreecommitdiff
path: root/masm/parse.h
blob: 3052d51d8e2c257f2f4af1879b2670f2b3842bce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* Copyright (c) 2024 Freya Murphy */

#ifndef __PARSE_H__
#define __PARSE_H__

#include "lex.h"

#include <mlimits.h>
#include <mips.h>
#include <stdint.h>

/* mips directive types */
enum mips_directive_type {
	MIPS_DIRECTIVE_ALIGN,
	MIPS_DIRECTIVE_SPACE,
	MIPS_DIRECTIVE_WORD,
	MIPS_DIRECTIVE_HALF,
	MIPS_DIRECTIVE_BYTE,
	MIPS_DIRECTIVE_SECTION,
	MIPS_DIRECTIVE_EXTERN,
	MIPS_DIRECTIVE_GLOBL,
	MIPS_DIRECTIVE_ASCII,
	MIPS_DIRECTIVE_ASCIIZ,
};

/* mip32 directive */
struct mips_directive {
	enum mips_directive_type type;
	uint32_t len; // used for words, halfs, bytes
	union {
		uint16_t align;
		uint16_t space;
		uint32_t words[MAX_ARG_LENGTH];
		uint16_t halfs[MAX_ARG_LENGTH];
		uint8_t bytes[MAX_ARG_LENGTH];
		char name[MAX_ARG_LENGTH];
	};
};

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_DIRECTIVE,
	EXPR_CONSTANT,
	EXPR_INS,
	EXPR_LABEL,
};

struct expr {
	enum expr_type type;
	union {
		// directive
		struct mips_directive directive;
		// constant
		struct const_expr constant;
		// instruction
		struct ins_expr ins;
		// label
		char label[MAX_LEX_LENGTH];
	};
};

struct parser {
	struct lexer *lexer;
	struct token peek;
};

/* get the next expression in the parser */
int parser_next(struct parser *parser, struct expr *expr);

/* initalize the base parser */
int parser_init(struct lexer *lexer, struct parser *parser);

/* free the base parser */
void parser_free(struct parser *parser);

#endif /* __PARSE_H__ */