-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchecklir.c
126 lines (115 loc) · 3.59 KB
/
checklir.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* This file is part of IRVM.
Copyright (C) 2014 Pablo de Oliveira
IRVM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
IRVM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with IRVM. If not, see <http://www.gnu.org/licenses/>. */
#include "checklir.h"
#include "error.h"
/* check that the input program is valid LIR */
static bool
_checklir (struct Node *n, struct Node *parent, int nb_seqs)
{
if (n == NULL)
return true;
switch (n->kind)
{
case T_BINOP:
{
struct Binop *b = container_of (n, struct Binop, node);
bool left = _checklir (b->left, n, nb_seqs);
bool right = _checklir (b->right, n, nb_seqs);
return left && right;
}
case T_MEM:
{
struct Mem *m = container_of (n, struct Mem, node);
return _checklir (m->exp, n, nb_seqs);
}
case T_CALL:
{
if (!parent ||
(parent->kind != T_MOVE_MEM &&
parent->kind != T_MOVE_TEMP && parent->kind != T_SXP))
{
warnl (&n->loc,
"calls must be wrapped in move or sxp nodes in LIR");
return false;
}
struct Call *c = container_of (n, struct Call, node);
struct Node *arg = NULL;
bool valid = true;
list_for_each (c->arguments, arg, list)
{
valid = _checklir (arg, n, nb_seqs) && valid;
}
return valid;
}
case T_ESEQ:
{
warnl (&n->loc, "eseq nodes are forbidden in LIR");
return false;
}
case T_MOVE_MEM:
{
struct MoveMem *m = container_of (n, struct MoveMem, node);
bool orig = _checklir (m->orig, n, nb_seqs);
bool dest = _checklir (m->dest, n, nb_seqs);
return orig && dest;
}
case T_MOVE_TEMP:
{
struct MoveTemp *m = container_of (n, struct MoveTemp, node);
return _checklir (m->orig, n, nb_seqs);
}
case T_SXP:
{
struct Sxp *s = container_of (n, struct Sxp, node);
return _checklir (s->exp, n, nb_seqs);
}
case T_CJUMP:
{
struct Cjump *c = container_of (n, struct Cjump, node);
bool left = _checklir (c->left, n, nb_seqs);
bool right = _checklir (c->right, n, nb_seqs);
if (n->next != c->belse_target)
{
warnl (&n->loc,
"cjump must be followed by its false label in LIR");
return false;
}
return left && right;
}
case T_SEQ:
{
if (nb_seqs > 0)
{
warnl (&n->loc, "no nested seq in LIR");
return false;
}
struct Seq *s = container_of (n, struct Seq, node);
struct Node *stm = NULL;
bool valid = true;
list_for_each (s->stms, stm, list)
{
valid = _checklir (stm, n, nb_seqs + 1) && valid;
}
return valid;
}
default:
return true;
}
assert (0);
}
/* check that the input program is valid LIR */
bool
checklir (void)
{
return _checklir (irtree, NULL, -1);
}