1f08c3bdfSopenharmony_ci#include <stdio.h>
2f08c3bdfSopenharmony_ci#include <assert.h>
3f08c3bdfSopenharmony_ci
4f08c3bdfSopenharmony_ci#include "symbol.h"
5f08c3bdfSopenharmony_ci#include "expression.h"
6f08c3bdfSopenharmony_ci#include "linearize.h"
7f08c3bdfSopenharmony_ci#include "flow.h"
8f08c3bdfSopenharmony_ci
9f08c3bdfSopenharmony_ci
10f08c3bdfSopenharmony_cistatic void output_bb(struct basic_block *bb, unsigned long generation)
11f08c3bdfSopenharmony_ci{
12f08c3bdfSopenharmony_ci	struct instruction *insn;
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci	bb->generation = generation;
15f08c3bdfSopenharmony_ci	printf("%s\n", show_label(bb));
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci	FOR_EACH_PTR(bb->insns, insn) {
18f08c3bdfSopenharmony_ci		if (!insn->bb)
19f08c3bdfSopenharmony_ci			continue;
20f08c3bdfSopenharmony_ci		printf("\t%s\n", show_instruction(insn));
21f08c3bdfSopenharmony_ci	}
22f08c3bdfSopenharmony_ci	END_FOR_EACH_PTR(insn);
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci	printf("\n");
25f08c3bdfSopenharmony_ci}
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_cistatic void output_fn(struct entrypoint *ep)
28f08c3bdfSopenharmony_ci{
29f08c3bdfSopenharmony_ci	struct basic_block *bb;
30f08c3bdfSopenharmony_ci	unsigned long generation = ++bb_generation;
31f08c3bdfSopenharmony_ci	struct symbol *sym = ep->name;
32f08c3bdfSopenharmony_ci	const char *name = show_ident(sym->ident);
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci	if (sym->ctype.modifiers & MOD_STATIC)
35f08c3bdfSopenharmony_ci		printf("\n\n%s:\n", name);
36f08c3bdfSopenharmony_ci	else
37f08c3bdfSopenharmony_ci		printf("\n\n.globl %s\n%s:\n", name, name);
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	unssa(ep);
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	FOR_EACH_PTR(ep->bbs, bb) {
42f08c3bdfSopenharmony_ci		if (bb->generation == generation)
43f08c3bdfSopenharmony_ci			continue;
44f08c3bdfSopenharmony_ci		output_bb(bb, generation);
45f08c3bdfSopenharmony_ci	}
46f08c3bdfSopenharmony_ci	END_FOR_EACH_PTR(bb);
47f08c3bdfSopenharmony_ci}
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_cistatic int output_data(struct symbol *sym)
50f08c3bdfSopenharmony_ci{
51f08c3bdfSopenharmony_ci	printf("symbol %s:\n", show_ident(sym->ident));
52f08c3bdfSopenharmony_ci	printf("\ttype = %d\n", sym->ctype.base_type->type);
53f08c3bdfSopenharmony_ci	printf("\tmodif= %lx\n", sym->ctype.modifiers);
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	return 0;
56f08c3bdfSopenharmony_ci}
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_cistatic int compile(struct symbol_list *list)
59f08c3bdfSopenharmony_ci{
60f08c3bdfSopenharmony_ci	struct symbol *sym;
61f08c3bdfSopenharmony_ci	FOR_EACH_PTR(list, sym) {
62f08c3bdfSopenharmony_ci		struct entrypoint *ep;
63f08c3bdfSopenharmony_ci		expand_symbol(sym);
64f08c3bdfSopenharmony_ci		ep = linearize_symbol(sym);
65f08c3bdfSopenharmony_ci		if (!(fdump_ir & PASS_FINAL))
66f08c3bdfSopenharmony_ci			continue;
67f08c3bdfSopenharmony_ci		if (ep)
68f08c3bdfSopenharmony_ci			output_fn(ep);
69f08c3bdfSopenharmony_ci		else
70f08c3bdfSopenharmony_ci			output_data(sym);
71f08c3bdfSopenharmony_ci	}
72f08c3bdfSopenharmony_ci	END_FOR_EACH_PTR(sym);
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci	return 0;
75f08c3bdfSopenharmony_ci}
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ciint main(int argc, char **argv)
78f08c3bdfSopenharmony_ci{
79f08c3bdfSopenharmony_ci	struct string_list * filelist = NULL;
80f08c3bdfSopenharmony_ci	char *file;
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci	compile(sparse_initialize(argc, argv, &filelist));
83f08c3bdfSopenharmony_ci	FOR_EACH_PTR(filelist, file) {
84f08c3bdfSopenharmony_ci		compile(sparse(file));
85f08c3bdfSopenharmony_ci	} END_FOR_EACH_PTR(file);
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_ci	report_stats();
88f08c3bdfSopenharmony_ci	return 0;
89f08c3bdfSopenharmony_ci}
90