summaryrefslogtreecommitdiff
path: root/nqasm.c
blob: 15b32385fec72e56041ba49297932bd554d0e9a4 (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
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
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>

#include "common.h"
#include "mnemonics.h"
#include "nqasm.h"

#include "lexer.h"
#include "parser.h"
#include "vector.h"

const char *progname;

struct label {
	const char *name;
	uint16_t addr;
};

uint16_t addr;

static int cmp_label(const void *a, const void *b)
{
	const struct label *la = a, *lb = b;
	return strcmp(la->name, lb->name);
}

#define DEFINE_VECTOR(name, arraytype, arrayname, initsize) \
	static struct name { \
		struct vector v; \
		arraytype arrayname[]; \
	} *name; \
	\
	static int name ## _init(void) \
	{ \
		return !(name = vector_init(initsize, sizeof (arraytype), sizeof *name)); \
	} \
	\
	static arraytype *name ## _append(void) \
	{ \
		struct vector *v = &name->v; \
		arraytype     *e = vector_append(&v); \
		name = (struct name *)v; \
		return e; \
	}

DEFINE_VECTOR(labels, struct label, l, 8)
DEFINE_VECTOR(instrs, struct instruction, i, 256)

void add_instruction(const struct instruction *inst)
{
	struct instruction *i = instrs_append();
	if (!i) {
		fprintf(stderr, "unable to allocate instruction\n");
		exit(1);
	}

	*i = *inst;
	addr += 2;
}

struct argument *argdup(const struct argument *arg)
{
	struct argument *ret = malloc(sizeof *ret);
	if (!ret) {
		fprintf(stderr, "unable to allocate argument\n");
		exit(1);
	}
	*ret = *arg;
	return ret;
}

static long eval_argument(const struct argument *arg, uint16_t pc)
{
	struct label *l;
	switch (arg->type) {
	case ARG_INTEGER:   return  arg->value;
	case ARG_PC:        return  pc;
	case ARG_UNARY_ADD: return  eval_argument(arg->children[0], pc);
	case ARG_UNARY_SUB: return -eval_argument(arg->children[0], pc);
	case ARG_UNARY_NOT: return !eval_argument(arg->children[0], pc);
	case ARG_UNARY_INV: return ~eval_argument(arg->children[0], pc);

	case ARG_ADD: return eval_argument(arg->children[0], pc) + eval_argument(arg->children[1], pc);
	case ARG_SUB: return eval_argument(arg->children[0], pc) - eval_argument(arg->children[1], pc);
	case ARG_MUL: return eval_argument(arg->children[0], pc) * eval_argument(arg->children[1], pc);
	case ARG_DIV: return eval_argument(arg->children[0], pc) / eval_argument(arg->children[1], pc);

	case ARG_LABEL:
		l = vector_search(&labels->v, &(struct label) { .name = arg->label }, cmp_label);
		if (l) return l->addr;
		fprintf(stderr, "unknown label '%s'\n", arg->label);
		exit(1);

	default:
		assert(0);
	}
}

static void assemble_instruction(FILE *out, const struct instruction *inst, uint16_t pc)
{
	const struct mnemonic *mnem = &mnemonics[inst->mnem];
	uint16_t bits = mnem->bits;

	const struct operand  *ops  = mnem->operands;
	const struct argument *args = inst->args.args;
	for (int i = 0; i < 3 && ops[i].type; i++) {
		long value = eval_argument(args + i, pc);

		switch (ops[i].type) {
		case REG:
		case REGPTR:
			if (0 > value || value >= 8) {
				fprintf(stderr, "invalid register\n");
				exit(1);
			}
			break;
		case IMM:
			if (-0x80 > value || value > 0xff) {
				fprintf(stderr, "immediate operand out of range\n");
				exit(1);
			}
			break;
		case PCOFF:
			value -= pc;
			if (-0x80 > value || value > 0x7f) {
				fprintf(stderr, "pc offset operand out of range\n");
				exit(1);
			}
			break;
		default:
			assert(0);
		}

		bits |= (value & 255) << ops[i].shift;
	}

	fprintf(out, "%04x\n", bits);
}

void add_label(const char *name)
{
	struct label *l = labels_append();
	if (!l) {
		fprintf(stderr, "unable to allocate label\n");
		exit(1);
	}

	*l = (struct label) {
		.name = name,
		.addr = addr
	};
}

static void usage(void)
{
	printf("usage: %s [-o output] [input]\n", progname);
	printf("       %s -h\n", progname);
	printf("\n");
	printf("\t%-16s %s\n", "-h",        "display this help text and exit");
	printf("\t%-16s %s\n", "-o output", "write output to 'output' (default=stdout)");
	printf("\t%-16s %s\n", "input",     "read input from 'input' (default=stdin)");
}

int main(int argc, char **argv)
{
	const char *outname = NULL;
	int error = 0;

	extern int   optind;
	extern char *optarg;

	progname = argv[0];
	for (int opt; (opt = getopt(argc, argv, "+ho:")) != -1;) {
		switch (opt) {
		case 'h':
			usage();
			return 0;
		case 'o':
			outname = optarg;
			break;
		default:
			error = 1;
		}
	}

	if (optind < argc - 1) {
		ERROR("Too many input files");
		return 1;
	}
	if (error) return 1;

	FILE *out = stdout;

	if (optind < argc && !(yyin = fopen(argv[optind], "r"))) {
		PERROR("Error opening input file");
		return 1;
	}
	if (outname && !(out = fopen(outname, "w"))) {
		PERROR("Error opening output file");
		fclose(yyin);
		return 1;
	}

	labels_init();
	instrs_init();

	yyparse();

	vector_sort(&labels->v, cmp_label);

	for (size_t i = 0; i < instrs->v.count; i++) {
		assemble_instruction(out, instrs->i + i, 2 * i);
	}

	fclose(yyin);
	fclose(out);
	return 0;
}