aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: db0132bea591ce8a500989882c0e10d419827230 (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
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdnoreturn.h>
#include "util.h"
#include "arg.h"
#include "decl.h"
#include "pp.h"
#include "scope.h"
#include "token.h"

static noreturn void
usage(void)
{
	fprintf(stderr, "usage: %s [input]\n", argv0);
	exit(2);
}

int
main(int argc, char *argv[])
{
	bool pponly = false;
	char *output = NULL;

	argv0 = progname(argv[0], "qbe");
	ARGBEGIN {
	case 'E':
		pponly = true;
		break;
	case 'o':
		output = EARGF(usage());
		break;
	default:
		usage();
	} ARGEND

	if (argc == 1) {
		if (!freopen(argv[0], "r", stdin))
			fatal("open %s:", argv[0]);
	} else if (argc) {
		usage();
	}
	if (output) {
		if (!freopen(output, "w", stdout))
			fatal("open %s:", output);
	}

	ppinit(argc ? argv[0] : "<stdin>");
	if (pponly) {
		while (tok.kind != TEOF) {
			tokprint(&tok);
			next();
		}
	} else {
		scopeputdecl(&filescope, "__builtin_va_list", &builtinvalist);
		scopeputdecl(&filescope, "__builtin_va_start", &builtinvastart);
		scopeputdecl(&filescope, "__builtin_va_copy", &builtinvacopy);
		scopeputdecl(&filescope, "__builtin_va_arg", &builtinvaarg);
		scopeputdecl(&filescope, "__builtin_va_end", &builtinvaend);
		scopeputdecl(&filescope, "__builtin_offsetof", &builtinoffsetof);
		while (tok.kind != TEOF) {
			if (!decl(&filescope, NULL))
				error(&tok.loc, "expected declaration or function definition");
		}
	}
	emittentativedefns();

	return 0;
}