-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdead_code.c
More file actions
106 lines (85 loc) · 2.01 KB
/
dead_code.c
File metadata and controls
106 lines (85 loc) · 2.01 KB
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
#include <stdio.h>
#include "expressions.h"
#include "dead_code.h"
statement_t *dead_code_elimination(statement_t *root, scope_t *scope)
{
statement_t *p, *t;
p = root;
while ( p ) {
switch ( p->type ) {
case ST_If:
{
statement_if_t n = p->_if;
constant_t constant;
// Attempt to evaluate the condition at compile time
if ( expression_evaluate(n.condition, &constant, scope)
== Success ) {
// if the condition is always true replace IF with TRUE block
if ( constant.bconst == True ) {
if ( p->prev )
p->prev->next = n._true;
n._true->prev = p->prev;
for ( t=n._true; t->next; t=t->next );
t->next = p->next;
if ( p->next )
p->next->prev = t;
p = n._true;
continue;
} else {
// if the condition is always false replace IF with FALSE block
if ( p->prev )
p->prev->next = n._false;
n._false->prev = p->prev;
p = n._false;
continue;
}
} else {
dead_code_elimination(n._true, scope);
dead_code_elimination(n._false, scope);
}
}
break;
case ST_While:
{
statement_while_t n = p->_while;
constant_t constant;
// Attempt to evaluate the condition at compile time
if ( expression_evaluate(n.condition, &constant, scope)
== Success ) {
// this while's loop will never be executed, remove it
if ( constant.bconst == False ) {
if ( p->prev )
p->prev->next = p->next;
if ( p->next )
p->next->prev = p->prev;
} else {
// this while will always execute
// TODO this will be done later
}
} else {
dead_code_elimination(n.loop, scope);
}
}
break;
case ST_For:
{
statement_for_t n = p->_for;
dead_code_elimination(n.loop, scope);
}
break;
case ST_With:
{
dead_code_elimination(p->with.statement ,scope);
}
break;
case ST_Assignment:
break;
case ST_Call:
break;
case ST_Case:
break;
}
p = p->next;
}
return root;
}