aboutsummaryrefslogtreecommitdiff
path: root/src/c8asm/main.c
blob: b77c4690a81acaea8b9adc5e3dec889ce0573fb1 (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
#include <sys/stat.h>

#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <builder.h>
#include <da.h>

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

static void asmfile(int, const char *);

size_t filesize;
const char *filename;
const char8_t *baseptr;

int
main(int argc, char **argv)
{
	int opt;
	const struct option longopts[] = {
		{"help",  no_argument, nullptr, 'h'},
		{nullptr, no_argument, nullptr, 0  },
	};

	cerrinit(*argv);
	while ((opt = getopt_long(argc, argv, "h", longopts, nullptr)) != -1) {
		switch (opt) {
		case 'h':
			execlp("man", "man", "1", argv[0], nullptr);
			die("execlp: man 1 %s", argv[0]);
		default:
			fprintf(stderr, "Usage: %s [file ...]\n", argv[0]);
			exit(EXIT_FAILURE);
		}
	}

	argc -= optind;
	argv += optind;

	if (!argc)
		asmfile(STDIN_FILENO, "-");
	for (int i = 0; i < argc; i++) {
		if (streq("-", argv[i]))
			asmfile(STDIN_FILENO, "-");
		else {
			int fd;
			if ((fd = open(argv[i], O_RDONLY)) == -1)
				die("open: %s", argv[i]);
			asmfile(fd, argv[i]);
			close(fd);
		}
	}

	return EXIT_SUCCESS;
}

void
asmfile(int fd, const char *fn)
{
	char *buf;
	size_t blksize;
	ssize_t nr;
	struct ast ast;
	struct stat st;
	struct u8str sb;
	struct tokens toks;

	filename = fn;

	if (fstat(fd, &st) == -1)
		die("fstat: %s", filename);
	blksize = MAX(st.st_blksize, BUFSIZ);
	if (!(buf = malloc(blksize)))
		die("malloc");

	/* Load the contents of the file into sb */
	u8strinit(&sb, S_ISREG(st.st_mode) ? (size_t)st.st_size : blksize);
	while ((nr = read(fd, buf, blksize)) > 0) {
		struct u8view v = {
			.p = buf,
			.len = nr,
		};
		if (!u8strpush(&sb, v))
			die("u8strpush");
	}
	if (nr == -1)
		die("read: %s", filename);

	free(buf);
	filesize = sb.len;
	baseptr = u8strfit(&sb)->p;
	assemble(stdout, ast = parsefile(toks = lexfile(u8strtou8(sb))));

	da_foreach (&ast, node) {
		if (node->kind == D_INSTR && node->instr.kind == I_DB)
			free(node->instr.buf);
	}

	free(toks.buf);
	free(ast.buf);
	u8strfree(sb);
}