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

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

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

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

static struct labels {
	struct vector v;
	struct label l[];
} *labels;

uint16_t addr;

static int labels_init(void)
{
	return !(labels = vector_init(8, sizeof labels->l[0], sizeof *labels));
}

static struct label *labels_append(void)
{
	struct vector *v = &labels->v;
	struct label  *l = vector_append(&v);
	labels = (struct labels *)v;
	return l;
}

void add_instruction(const struct instruction *inst)
{
	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++) {
		bits |= args[i].value << ops[i].shift;
	}

	printf("%04x\n", bits);
	addr += 2;
}

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
	};
}

int main(int argc, char **argv)
{
	labels_init();

	yyparse();
	return 0;
}