aboutsummaryrefslogtreecommitdiff
blob: 88ce025ff1bb55cf506863a846449aa1fd011a18 (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
#include <stdio.h>
#include <assert.h>

#include "symbol.h"
#include "expression.h"
#include "linearize.h"
#include "flow.h"


static void output_bb(struct basic_block *bb, unsigned long generation)
{
	struct instruction *insn;

	bb->generation = generation;
	printf(".L%p\n", bb);

	FOR_EACH_PTR(bb->insns, insn) {
		if (!insn->bb)
			continue;
		printf("\t%s\n", show_instruction(insn));
	}
	END_FOR_EACH_PTR(insn);

	printf("\n");
}

static void output_fn(struct entrypoint *ep)
{
	struct basic_block *bb;
	unsigned long generation = ++bb_generation;
	struct symbol *sym = ep->name;
	const char *name = show_ident(sym->ident);

	if (sym->ctype.modifiers & MOD_STATIC)
		printf("\n\n%s:\n", name);
	else
		printf("\n\n.globl %s\n%s:\n", name, name);

	unssa(ep);

	FOR_EACH_PTR(ep->bbs, bb) {
		if (bb->generation == generation)
			continue;
		output_bb(bb, generation);
	}
	END_FOR_EACH_PTR(bb);
}

static int output_data(struct symbol *sym)
{
	printf("symbol %s:\n", show_ident(sym->ident));
	printf("\ttype = %d\n", sym->ctype.base_type->type);
	printf("\tmodif= %lx\n", sym->ctype.modifiers);

	return 0;
}

static int compile(struct symbol_list *list)
{
	struct symbol *sym;
	FOR_EACH_PTR(list, sym) {
		struct entrypoint *ep;
		expand_symbol(sym);
		ep = linearize_symbol(sym);
		if (ep)
			output_fn(ep);
		else
			output_data(sym);
	}
	END_FOR_EACH_PTR(sym);

	return 0;
}

int main(int argc, char **argv)
{
	struct string_list * filelist = NULL;
	char *file;

	compile(sparse_initialize(argc, argv, &filelist));
	FOR_EACH_PTR_NOTAG(filelist, file) {
		compile(sparse(file));
	} END_FOR_EACH_PTR_NOTAG(file);

	return 0;
}