aboutsummaryrefslogtreecommitdiff
path: root/src/c8asm/assembler.c
blob: eaf2929d8c51531005c87a74ed085e1f9e97da01 (plain) (blame)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <arpa/inet.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include <da.h>
#include <mbstring.h>

#include "assembler.h"
#include "cerr.h"
#include "common.h"
#include "macros.h"
#include "parser.h"

#define E_LEXISTS  "label ‘%.*s’ has already been declared"
#define E_LNEXISTS "label ‘%.*s’ hasn’t been declared"

struct label {
	uint16_t addr;
	struct u8view sv;
};

struct labels {
	struct label *buf;
	size_t len, cap;
};

static void pushlabel(struct labels *, struct label);
static uint16_t getaddr(struct raw_addr);
static struct label *getlabel(struct u8view);

static size_t i;
static struct labels locals, globals;

struct label *
getlabel(struct u8view sv)
{
	struct labels *xs = sv.p[0] == '.' ? &locals : &globals;
	da_foreach (xs, x) {
		if (u8eq(x->sv, sv))
			return x;
	}
	return nullptr;
}

uint16_t
getaddr(struct raw_addr a)
{
	struct label *lbl;
	if (!a.label)
		return a.val;
	/* The first 0x200 is reserved for the interpreter, so we need to offset
	   labels that aren’t integer-literals by that. */
	if (lbl = getlabel(a.sv))
		return lbl->addr + 0x200;
	die_with_off(filename, a.sv.p - baseptr, E_LNEXISTS, U8_PRI_ARGS(a.sv));
}

void
pushlabel(struct labels *dst, struct label lbl)
{
	if (getlabel(lbl.sv)) {
		die_with_off(filename, lbl.sv.p - baseptr, E_LEXISTS,
		             U8_PRI_ARGS(lbl.sv));
	}
	dapush(dst, lbl);
}

void
assemble(FILE *stream, struct ast ast)
{
#define PUT(X) \
	do { \
		uint16_t __x = htons(X); \
		fwrite(&__x, 1, sizeof(__x), stream); \
	} while (false)

	bool pad = false;

	da_foreach (&ast, node) {
		if (node->kind == D_LABEL) {
			struct label lbl = {
				.addr = i,
				.sv = node->name,
			};
			pushlabel(node->name.p[0] == '.' ? &locals : &globals, lbl);
		} else if (node->kind == D_INSTR)
			i += node->instr.kind == I_DB ? node->instr.len : 2;
		else
			unreachable();
	}

	da_foreach (&ast, node) {
		if (node->kind == D_LABEL)
			continue;
		if (node->kind != D_INSTR)
			unreachable();

		/* Instructions need to be 0-padded so they appear on an even byte
		   boundary. */
		if (node->instr.kind != I_DB && pad) {
			putchar(0);
			pad = false;
		}

		switch (node->instr.kind) {
		case I_ADD_I_VX:
			PUT(0xF01E | (node->instr.args[0].val << 8));
			break;
		case I_ADD_VX_B:
			PUT(0x7000 | (node->instr.args[0].val << 8)
			    | node->instr.args[1].val);
			break;
		case I_ADD_VX_VY:
			PUT(0x8004 | (node->instr.args[0].val << 8)
			    | node->instr.args[1].val << 4);
			break;
		case I_AND:
			PUT(0x8002 | (node->instr.args[0].val << 8)
			    | node->instr.args[1].val << 4);
			break;
		case I_BCD:
			PUT(0xF033 | (node->instr.args[0].val << 8));
			break;
		case I_CALL:
			PUT(0x2000 | getaddr(node->instr.args[0]));
			break;
		case I_CLS:
			PUT(0x00E0);
			break;
		case I_DB:
			da_foreach (&node->instr, byte)
				fputc(*byte, stream);
			if (node->instr.len & 1)
				pad = !pad;
			break;
		case I_DRW:
			PUT(0xD000 | (node->instr.args[0].val << 8)
			    | (node->instr.args[1].val << 4) | node->instr.args[2].val);
			break;
		case I_HEX:
			PUT(0xF029 | (node->instr.args[0].val << 8));
			break;
		case I_JP_A:
			PUT(0x1000 | getaddr(node->instr.args[0]));
			break;
		case I_JP_V0_A:
			PUT(0xB000 | getaddr(node->instr.args[0]));
			break;
		case I_LD_DT:
			PUT(0xF015 | (node->instr.args[0].val << 8));
			break;
		case I_LD_I:
			PUT(0xA000 | getaddr(node->instr.args[0]));
			break;
		case I_LD_ST:
			PUT(0xF018 | (node->instr.args[0].val << 8));
			break;
		case I_LD_VX_B:
			PUT(0x6000 | (node->instr.args[0].val << 8)
			    | node->instr.args[1].val);
			break;
		case I_LD_VX_DT:
			PUT(0xF007 | (node->instr.args[0].val << 8));
			break;
		case I_LD_VX_K:
			PUT(0xF00A | (node->instr.args[0].val << 8));
			break;
		case I_LD_VX_VY:
			PUT(0x8000 | (node->instr.args[0].val << 8)
			    | node->instr.args[1].val << 4);
			break;
		case I_OR:
			PUT(0x8001 | (node->instr.args[0].val << 8)
			    | node->instr.args[1].val << 4);
			break;
		case I_RET:
			PUT(0x00EE);
			break;
		case I_RND:
			PUT(0xC000 | (node->instr.args[0].val << 8)
			    | node->instr.args[1].val);
			break;
		case I_RSTR:
			PUT(0xF065 | (node->instr.args[0].val << 8));
			break;
		case I_SE_VX_B:
			PUT(0x3000 | (node->instr.args[0].val << 8)
			    | node->instr.args[1].val);
			break;
		case I_SE_VX_VY:
			PUT(0x5000 | (node->instr.args[0].val << 8)
			    | node->instr.args[1].val << 4);
			break;
		case I_SHL:
			PUT(0x800E | (node->instr.args[0].val << 8));
			break;
		case I_SHR:
			PUT(0x8006 | (node->instr.args[0].val << 8));
			break;
		case I_SKNP:
			PUT(0xE0A1 | (node->instr.args[0].val << 8));
			break;
		case I_SKP:
			PUT(0xE09E | (node->instr.args[0].val << 8));
			break;
		case I_SNE_VX_B:
			PUT(0x4000 | (node->instr.args[0].val << 8)
			    | node->instr.args[1].val);
			break;
		case I_SNE_VX_VY:
			PUT(0x9000 | (node->instr.args[0].val << 8)
			    | node->instr.args[1].val << 4);
			break;
		case I_STOR:
			PUT(0xF055 | (node->instr.args[0].val << 8));
			break;
		case I_SUB:
			PUT(0x8005 | (node->instr.args[0].val << 8)
			    | node->instr.args[1].val << 4);
			break;
		case I_SUBN:
			PUT(0x8007 | (node->instr.args[0].val << 8)
			    | node->instr.args[1].val << 4);
			break;
		case I_SYS:
			PUT(getaddr(node->instr.args[0]));
			break;
		case I_XOR:
			PUT(0x8003 | (node->instr.args[0].val << 8)
			    | node->instr.args[1].val << 4);
			break;
		default:
			unreachable();
		}
	}

	locals.len = 0;

#undef PUT
}