aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 61475dcc0937602cce96fedb843529cec2b15437 (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
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include "assemble.h"
#include "compiler.h"
#include "node.h"
#include "regalloc.h"
#include "to_assembly.h"


extern FILE *yyin;
extern int yyparse(void);
extern struct node *root_node;


int main(int argc, char **argv) {
	if (argc != 2) {
		fprintf(stderr, "Usage: %s <source.c>\n", argv[0]);
		return 1;
	}
	yyin = fopen(argv[1], "r");
	if (!yyin) {
		fprintf(stderr, "Cannot open file '%s'\n", argv[1]);
		return 1;
	}

	int result = yyparse();
	if(!feof(yyin)) {
		assert(false);
	}

#ifdef DEBUG
	node_print(root_node, stdout, 0);
	printf("\n");
#endif

	struct ir *ir = compile(root_node);

#ifdef DEBUG
	ir_print(ir, stdout); printf("\n");
#endif

	struct allocation *alloc = regalloc(ir);
	to_assembly(ir, alloc);
	allocation_delete(alloc);

#ifdef DEBUG
	ir_print(ir, stdout); printf("\n");
#endif

	assemble(ir, stdout);

	return result;
}