summaryrefslogtreecommitdiff
path: root/parse.h
blob: 5feb420d52bde3458f6b6503bb5506535bbec42e (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
#ifndef PARSE_H
#define PARSE_H

#include <stddef.h>
#include <stdbool.h>

#include "lex.h"
#include "instruction.h"

struct label {
	char *name;
	size_t byte_offset;
};

union immediate {
	const char *label;
	uint16_t value;
};

struct r_type {
	enum OPER oper;
	enum REG dest;
	enum REG left;
	enum REG right;
};

struct i_type {
	enum OPER oper;
	enum REG dest;
	enum REG left;
	bool imm_is_ident;
	union immediate imm;
};

struct jr_type {
	enum JCOND cond;
	enum REG reg;
};

struct ji_type {
	enum JCOND cond;
	bool imm_is_ident;
	union immediate imm;
};

struct b_type {
	enum JCOND cond;
	bool imm_is_ident;
	union immediate imm;
};

struct instruction {
	enum INST_TYPE type;
	union instruction_u {
		struct r_type r;   /* catch-all R-Type */
		struct i_type i;   /* I-type on immediate literal */
		struct jr_type jr; /* jump to register */
		struct ji_type ji; /* jump to immediate */
		struct b_type b;   /* branch to immediate literal */
	} inst;
};

int parse(const char *filename_local, FILE *fd, struct label **labels_local, size_t *labels_count_local, struct token *tokens, size_t tokens_count, struct instruction **instructions, size_t *instructions_count);

#endif /* PARSE_H */