-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement.hpp
210 lines (170 loc) · 5.33 KB
/
statement.hpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#ifndef STATEMENT_HPP
#define STATEMENT_HPP
#include <vector>
#include "ast.hpp"
#include "block_item.hpp"
#include "expression.hpp"
class stmt_node : public blk_item {
public:
virtual void dump_tree() override;
virtual void codegen() override = 0;
virtual bool has_labeled_stmt() override = 0;
};
class labeled_stmt : public stmt_node {};
// The type other than case or default
// TODO : Think of a better name
class prefix_labeled_stmt : public labeled_stmt {
std::string identifier;
stmt_node *statement;
public:
prefix_labeled_stmt(const char *ident, stmt_node *stmt);
void dump_tree() override;
void codegen() override;
static llvm::BasicBlock *codegen(std::string name, stmt_node *statement,
bool add_to_table = true);
bool has_labeled_stmt() override { return true; }
};
class switch_labeled_stmt : public labeled_stmt {
protected:
struct scope {
llvm::SwitchInst *switch_inst;
llvm::BasicBlock *default_block = nullptr;
scope(llvm::SwitchInst *s) : switch_inst(s){};
};
static std::vector<scope> switch_scopes;
static scope &top_scope() { return switch_scopes.back(); };
static void push_scope(llvm::SwitchInst *switch_inst) {
switch_scopes.emplace_back(switch_inst);
};
static void pop_scope() { switch_scopes.pop_back(); };
public:
class auto_scope {
public:
auto_scope(llvm::SwitchInst *switch_inst) { push_scope(switch_inst); };
~auto_scope() { pop_scope(); };
llvm::BasicBlock *get_default_block() { return top_scope().default_block; }
};
};
class case_labeled_stmt : public switch_labeled_stmt {
cond_expr *expression; // Using as a proxy for constant_expression class
stmt_node *statement;
public:
case_labeled_stmt(cond_expr *expression, stmt_node *statement);
void dump_tree() override;
void codegen() override;
bool has_labeled_stmt() override { return statement->has_labeled_stmt(); }
};
class default_labeled_stmt : public switch_labeled_stmt {
stmt_node *statement;
public:
default_labeled_stmt(stmt_node *statement);
void dump_tree() override;
void codegen() override;
bool has_labeled_stmt() override { return statement->has_labeled_stmt(); }
};
class compound_stmt : public stmt_node {
std::vector<blk_item *> block_items;
public:
compound_stmt *add(blk_item *item);
void dump_tree() override;
virtual void codegen() override;
bool has_labeled_stmt() override {
for (auto item : block_items) {
if (item->has_labeled_stmt()) return true;
}
return false;
};
};
class expression_stmt : public stmt_node {
expr *expression; // Optional
public:
expression_stmt(expr *expression = nullptr);
void dump_tree() override;
void codegen() override;
bool has_labeled_stmt() override { return false; }
};
class selection_stmt : public stmt_node {};
class if_stmt : public selection_stmt {
expr *condition;
stmt_node *then_stmt;
stmt_node *else_stmt; // Optional
public:
if_stmt(expr *cond, stmt_node *then_stmt, stmt_node *else_stmt = nullptr);
void dump_tree() override;
void codegen() override;
bool has_labeled_stmt() override {
return then_stmt->has_labeled_stmt() ||
(else_stmt && else_stmt->has_labeled_stmt());
};
};
class switch_stmt : public selection_stmt {
expr *expression;
stmt_node *statement;
public:
switch_stmt(expr *expression, stmt_node *statement);
void dump_tree() override;
void codegen() override;
bool has_labeled_stmt() override { return statement->has_labeled_stmt(); };
};
class iteration_stmt : public stmt_node {};
class while_stmt : public iteration_stmt {
expr *condition;
stmt_node *statement;
public:
while_stmt(expr *condition, stmt_node *statement);
void dump_tree() override;
void codegen() override;
bool has_labeled_stmt() override { return statement->has_labeled_stmt(); };
};
// TODO: Reduce code duplication with while by making a common base
class do_stmt : public iteration_stmt {
stmt_node *statement;
expr *condition;
public:
do_stmt(stmt_node *statement, expr *condition);
void dump_tree() override;
void codegen() override;
bool has_labeled_stmt() override { return statement->has_labeled_stmt(); }
};
class jump_stmt : public stmt_node {};
class goto_stmt : public jump_stmt {
std::string identifier;
public:
goto_stmt(const char *ident);
void dump_tree() override;
void codegen() override;
bool has_labeled_stmt() override { return false; }
};
class continue_stmt : public jump_stmt {
static std::vector<llvm::BasicBlock *> dest_blocks;
public:
void dump_tree() override;
void codegen() override;
bool has_labeled_stmt() override { return false; }
class scope {
public:
scope(llvm::BasicBlock *b) { dest_blocks.push_back(b); }
~scope() { dest_blocks.pop_back(); }
};
};
class break_stmt : public jump_stmt {
static std::vector<llvm::BasicBlock *> dest_blocks;
public:
void dump_tree() override;
void codegen() override;
bool has_labeled_stmt() override { return false; }
class scope {
public:
scope(llvm::BasicBlock *b) { dest_blocks.push_back(b); }
~scope() { dest_blocks.pop_back(); }
};
};
class return_stmt : public jump_stmt {
expr *expression; // Optional
public:
return_stmt(expr *expression = nullptr);
void dump_tree() override;
void codegen() override;
bool has_labeled_stmt() override { return false; }
};
#endif /* STATEMENT_HPP */