-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprinttree.c
105 lines (98 loc) · 3.24 KB
/
printtree.c
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
/*
* printtree.c - functions to print out intermediate representation (IR) trees.
*
*/
#include <stdio.h>
#include "util.h"
#include "symbol.h"
#include "temp.h"
#include "tree.h"
#include "printtree.h"
/* local function prototype */
static void pr_tree_exp(FILE *out, T_exp exp, int d);
static void indent(FILE *out, int d) {
int i;
for (i = 0; i <= d; i++) fprintf(out, " ");
}
static char bin_oper[][12] = {
"PLUS", "MINUS", "TIMES", "DIVIDE",
"AND", "OR", "LSHIFT", "RSHIFT", "ARSHIFT", "XOR"};
static char rel_oper[][12] = {
"EQ", "NE", "LT", "GT", "LE", "GE", "ULT", "ULE", "UGT", "UGE"};
static void pr_stm(FILE *out, T_stm stm, int d)
{
switch (stm->kind) {
case T_SEQ:
indent(out,d);
fprintf(out, "SEQ(\n"); pr_stm(out, stm->u.SEQ.left,d+1); fprintf(out, ",\n");
pr_stm(out, stm->u.SEQ.right,d+1); fprintf(out, ")");
break;
case T_LABEL:
indent(out,d); fprintf(out, "LABEL %s", S_name(stm->u.LABEL));
break;
case T_JUMP:
indent(out,d); fprintf(out, "JUMP(\n"); pr_tree_exp(out, stm->u.JUMP.exp,d+1);
fprintf(out, ")");
break;
case T_CJUMP:
indent(out,d); fprintf(out, "CJUMP(%s,\n", rel_oper[stm->u.CJUMP.op]);
pr_tree_exp(out, stm->u.CJUMP.left,d+1); fprintf(out, ",\n");
pr_tree_exp(out, stm->u.CJUMP.right,d+1); fprintf(out, ",\n");
indent(out,d+1); fprintf(out, "%s,", S_name(stm->u.CJUMP.true));
fprintf(out, "%s", S_name(stm->u.CJUMP.false)); fprintf(out, ")");
break;
case T_MOVE:
indent(out,d); fprintf(out, "MOVE(\n"); pr_tree_exp(out, stm->u.MOVE.dst,d+1);
fprintf(out, ",\n");
pr_tree_exp(out, stm->u.MOVE.src,d+1); fprintf(out, ")");
break;
case T_EXP:
indent(out,d); fprintf(out, "EXP(\n"); pr_tree_exp(out, stm->u.EXP,d+1);
fprintf(out, ")");
break;
}
}
static void pr_tree_exp(FILE *out, T_exp exp, int d)
{
switch (exp->kind) {
case T_BINOP:
indent(out,d); fprintf(out, "BINOP(%s,\n", bin_oper[exp->u.BINOP.op]);
pr_tree_exp(out, exp->u.BINOP.left,d+1); fprintf(out, ",\n");
pr_tree_exp(out, exp->u.BINOP.right,d+1); fprintf(out, ")");
break;
case T_MEM:
indent(out,d); fprintf(out, "MEM");
fprintf(out, "(\n"); pr_tree_exp(out, exp->u.MEM,d+1); fprintf(out, ")");
break;
case T_TEMP:
indent(out,d); fprintf(out, "TEMP t%s",
Temp_look(Temp_name(), exp->u.TEMP));
break;
case T_ESEQ:
indent(out,d); fprintf(out, "ESEQ(\n"); pr_stm(out, exp->u.ESEQ.stm,d+1);
fprintf(out, ",\n");
pr_tree_exp(out, exp->u.ESEQ.exp,d+1); fprintf(out, ")");
break;
case T_NAME:
indent(out,d); fprintf(out, "NAME %s", S_name(exp->u.NAME));
break;
case T_CONST:
indent(out,d); fprintf(out, "CONST %d", exp->u.CONST);
break;
case T_CALL:
{T_expList args = exp->u.CALL.args;
indent(out,d); fprintf(out, "CALL(\n"); pr_tree_exp(out, exp->u.CALL.fun,d+1);
for (;args; args=args->tail) {
fprintf(out, ",\n"); pr_tree_exp(out, args->head,d+2);
}
fprintf(out, ")");
break;
}
} /* end of switch */
}
void printStmList (FILE *out, T_stmList stmList)
{
for (; stmList; stmList=stmList->tail) {
pr_stm(out, stmList->head,0); fprintf(out, "\n");
}
}