summaryrefslogtreecommitdiff
path: root/assembler.c
blob: eaf4d38fed66412ae2eba8dfd49832e5483457d6 (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
#include <stdio.h>
#include <stdint.h>

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

#if 0
/**
 * Types for intermediate storage of instructions
 */
struct r_type {
	enum OPER operation;
	enum REG dest;
	enum REG left;
	enum REG right;
};

struct i_type { /* covers WI and NI */
	enum OPER operation;
	enum REG dest;
	enum REG left;
	int16_t immediate;
};

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

struct ji_type {
	enum JCOND condition;
	uint16_t immediate;
};

struct b_type { /* FIXME merge with ji_type? */
	enum JCOND condition;
	uint16_t immediate; /* capped to 10 bits by IS */
};

/* Union for bringing above together */
union instruction_union {
	struct r_type r;
	struct i_type i;
	struct jr_type jr;
	struct ji_type ji;
	struct b_type b;
};

struct instruction {
	enum INST_TYPE type;
	union instruction_union i;
};
/**/
#endif

int main(int argc, char **argv)
{
	int ret = 0;
	FILE *fin = NULL;
	FILE *fout = NULL;

	if (argc < 3) {
		fprintf(stderr, "Syntax: %s <in.asm> <out.bin>\n", argv[0]);
		return 1;
	}

	if ((fin = fopen(argv[1], "r")) == NULL) {
		fprintf(stderr, "Error opening %s: ", argv[1]);
		perror("fopen");
		return 1;
	}

	if ((fout = fopen(argv[2], "wb")) == NULL) {
		fprintf(stderr, "Error opening %s: ", argv[2]);
		perror("fopen");
		return 1;
	}
/****/
	struct token *tokens = NULL;
	size_t tok_count = 0;

	if ((tokens = lex(argv[1], fin, &tok_count)) == NULL)
		return 2;

	struct instruction *insts;
	size_t insts_count;
	struct label *labels;
	size_t labels_count;
	if (ret = parse(argv[1], fin, &labels, &labels_count, tokens, tok_count, &insts, &insts_count))
		return ret;

	if (ret = output(fout, labels, labels_count, insts, insts_count))
		return ret;

	return 0;
}