comparison Bison-Flex/BasicCompiler-StackBase/compiler.cpp @ 1:a3ea4c73696b

move files
author nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
date Tue, 10 May 2011 06:26:08 +0900
parents Bison-Flex/EUC/compiler.cpp@3f4ade70b4d2
children fbe42292d479
comparison
equal deleted inserted replaced
0:3f4ade70b4d2 1:a3ea4c73696b
1 #include <iostream>
2 #include <iomanip>
3 #include "script-parser.hh"
4 #include "compiler.h"
5
6 #ifdef _MSC_VER
7 #pragma warning(disable: 4996)
8 #endif
9
10 // コンストラクタ
11
12 compiler::compiler()
13 : error_count(0)
14 {
15 }
16
17 // デストラクタ
18
19 compiler::~compiler()
20 {
21 }
22
23 // コンパイル
24
25 bool compiler::compile(const std::string &f, vm::data &data)
26 {
27 // システムコールの設定
28
29 file = f;
30 scan_begin(); // スキャナー初期化
31 yy::script_parser parser(*this); // パーサー構築
32 int result = parser.parse(); // 構文解析
33 scan_end(); // スキャナー終了
34
35 if (result != 0)
36 return false; // パーサーエラー
37
38 // ステートスタックが空ではないならば、if, for, whileが
39 // 閉じていない
40 while (!state_stack.empty()) {
41 CState &state = state_stack.top();
42 switch (state.state_) {
43 case STATE_IF:
44 error(state.l_, "ifに対応するendifが有りません");
45 break;
46
47 case STATE_FOR:
48 error(state.l_, "forに対応するnextが有りません");
49 delete state.start_;
50 delete state.end_;
51 delete state.step_;
52 break;
53
54 case STATE_WHILE:
55 error(state.l_, "whileに対応するwendが有りません");
56 break;
57 }
58 state_stack.pop();
59 }
60
61 // 番兵用HALTコマンドを追加
62 const CVMCode &code = statement.back();
63 if (code.op_ != VM_HALT) // haltが無いならば
64 OpHalt(); // haltを追加
65
66 int code_size = LabelSetting(); // ラベルにアドレスを設定
67 CraeteData(data, code_size); // バイナリ生成
68
69 return error_count == 0;
70 }
71
72 // エラーメッセージを出力
73
74 void compiler::error(const yy::location& l, const std::string& m)
75 {
76 std::cerr << l << ": " << m << std::endl;
77 error_count++;
78 }
79
80 // エラーメッセージを出力
81
82 void compiler::error(const std::string& m)
83 {
84 std::cerr << m << std::endl;
85 error_count++;
86 }
87
88 // 命令文の登録
89
90 // 代入文
91 void compiler::AssignStatement(const yy::location& l, CAssign *assign)
92 {
93 assign->analyze(this);
94 delete assign;
95 }
96
97 // if文
98 //
99 // if expr then
100 // A
101 // endif
102 //
103 // > push expr
104 // > jmp_nc L1
105 // > A
106 // > L1:
107 //
108 // if expr then
109 // A
110 // else
111 // B
112 // endif
113 //
114 // > push expr
115 // > jmp_nc L1
116 // > A
117 // > jmp L2
118 // > L1:
119 // > B
120 // > L2:
121 //
122 void compiler::IfStatement(const yy::location& l, CNode *expr)
123 {
124 expr->push(this);
125 int label = MakeLabel();
126 OpJmpNC(label); // 偽の時の飛び先
127
128 state_stack.push(CState(l, STATE_IF, label));
129
130 delete expr;
131 }
132
133 void compiler::ElseStatement(const yy::location& l)
134 {
135 if (state_stack.empty() || state_stack.top().state_ != STATE_IF) {
136 error(l, "if文と対応していないelse文が有りました。");
137 }
138 else {
139 CState &state = state_stack.top();
140 int label = MakeLabel();
141 OpJmp(label);
142 SetLabel(state.label1_); // 偽の時の飛び先をここにする
143 state.label1_ = label; // endifの飛び先を再設定
144 }
145 }
146
147 void compiler::EndifStatement(const yy::location& l)
148 {
149 if (state_stack.empty() || state_stack.top().state_ != STATE_IF) {
150 error(l, "if文と対応していないendif文が有りました。");
151 }
152 else {
153 CState &state = state_stack.top();
154 SetLabel(state.label1_); // endifの飛び先をここにする
155 state_stack.pop(); // ステートスタックをpop
156 }
157 }
158
159 // FOR文
160 //
161 // for value=X to Y step Z
162 // A
163 // next
164 //
165 // > push X : value = X
166 // > pop value
167 // > L1:
168 // > A
169 // > push value : if (value == Y) goto L2
170 // > push Y
171 // > eq
172 // > jmp_c L2
173 // > push value : value = value + Z
174 // > push Z
175 // > add
176 // > pop value
177 // > jmp L1
178 // > L2:
179 //
180 void compiler::ForStatement(const yy::location& l, CAssign *start, CNode *end, CNode *step)
181 {
182 int label = MakeLabel();
183
184 start->analyze(this);
185 SetLabel(label);
186
187 state_stack.push(CState(l, STATE_FOR, label, start, end, step));
188 }
189
190 void compiler::NextStatement(const yy::location& l)
191 {
192 if (state_stack.empty() || state_stack.top().state_ != STATE_FOR) {
193 error(l, "for文と対応していないnext文が有りました。");
194 }
195 else {
196 CState &state = state_stack.top();
197 int label = MakeLabel();
198
199 // ループ終了のチェック
200 state.start_->push_value(this);
201 state.end_->push(this);
202 OpEq();
203 OpJmpC(label); // 終了時飛び先
204
205 // カウンター増分
206 state.start_->push_value(this);
207 if (state.step_)
208 state.step_->push(this);
209 else
210 PushConst(1);
211
212 OpAdd();
213 state.start_->pop_value(this);
214
215 // ループ
216 OpJmp(state.label1_);
217
218 // ループ終了
219 SetLabel(label);
220
221 // 後始末
222 delete state.start_;
223 delete state.end_;
224 delete state.step_;
225
226 state_stack.pop();
227 }
228 }
229
230 // while文
231 //
232 // while (expr) A
233 // > L1:
234 // > push expr
235 // > jmp_nc L2
236 // > A
237 // > jmp L1
238 // > L2:
239 //
240 void compiler::WhileStatement(const yy::location& l, CNode *expr)
241 {
242 int label1 = MakeLabel();
243 int label2 = MakeLabel();
244
245 SetLabel(label1);
246 expr->push(this);
247 OpJmpNC(label2);
248
249 state_stack.push(CState(l, STATE_WHILE, label1, label2));
250
251 delete expr;
252 }
253
254 void compiler::WendStatement(const yy::location& l)
255 {
256 if (state_stack.empty() || state_stack.top().state_ != STATE_WHILE) {
257 error(l, "while文と対応していないwend文が有りました。");
258 }
259 else {
260 CState &state = state_stack.top();
261 OpJmp(state.label1_);
262 SetLabel(state.label2_);
263 state_stack.pop();
264 }
265 }
266
267 // end文
268 //
269 // > halt
270 //
271 void compiler::EndStatement(const yy::location& l)
272 {
273 OpHalt();
274 }
275
276 // print文
277 //
278 // print a, b, c
279 //
280 // > push c
281 // > push b
282 // > push a
283 // > print 3
284 //
285 void compiler::PrintStatement(const yy::location& l, CArgs *args)
286 {
287 int arg_count = 0;
288 if (args) {
289 args->for_each_rev(std::bind2nd(std::mem_fun(&CNode::push), this));
290 arg_count = args->size();
291 }
292
293 OpPrint(arg_count);
294
295 delete args;
296 }
297
298 // ラベル生成
299
300 int compiler::MakeLabel()
301 {
302 int index = (int)labels.size();
303 labels.push_back(CLabel(index));
304 return index;
305 }
306
307 // ラベルのダミーコマンドをステートメントリストに登録する
308
309 void compiler::SetLabel(int label)
310 {
311 statement.push_back(CVMCode(VM_MAXCOMMAND, label));
312 }
313
314 // ラベル解決
315 //
316 // 1.アドレスを生成する
317 // 2.ダミーのラベルコマンドが有ったアドレスを、ラベルテーブルに登録する
318 // 3.Jmpコマンドの飛び先をラベルテーブルに登録されたアドレスにする
319
320 // note:
321 // GCCでは、関数オブジェクトを関数内に書けないので、ここに記述する。
322 // VC++9.0(VS2008)では関数内に書くことで、スコープを封じ込める事が可能。
323
324 // アドレス計算関数オブジェクト
325 struct calc_addr {
326 std::vector<CLabel> &labels_;
327 int &pos_;
328 calc_addr(std::vector<CLabel> &labels, int &pos): labels_(labels), pos_(pos)
329 {
330 }
331 void operator()(const CVMCode &code)
332 {
333 if (code.op_ == VM_MAXCOMMAND) { // ラベルのダミーコマンド
334 labels_[code.arg1_].pos_ = pos_;
335 }
336 else {
337 pos_ += code.size_;
338 }
339 }
340 } ;
341
342 // ジャンプアドレス設定関数オブジェクト
343 struct set_addr {
344 std::vector<CLabel> &labels_;
345 set_addr(std::vector<CLabel> &labels): labels_(labels)
346 {
347 }
348 void operator()(CVMCode &code)
349 {
350 switch (code.op_) {
351 case VM_JMP:
352 case VM_JMPC:
353 case VM_JMPNC:
354 code.arg1_ = labels_[code.arg1_].pos_;
355 break;
356 }
357 }
358 } ;
359
360 int compiler::LabelSetting()
361 {
362 // アドレス計算
363 int pos = 0;
364 std::for_each(statement.begin(), statement.end(), calc_addr(labels, pos));
365 // ジャンプアドレス設定
366 std::for_each(statement.begin(), statement.end(), set_addr(labels));
367
368 return pos;
369 }
370
371 // バイナリデータ生成
372
373 // 関数オブジェクト
374 struct copy_code {
375 unsigned char *p;
376 copy_code(unsigned char *code): p(code)
377 {
378 }
379 void operator()(const CVMCode &code)
380 {
381 p = code.Get(p);
382 }
383 } ;
384
385 bool compiler::CraeteData(vm::data &data, int code_size)
386 {
387 data.command_ = new unsigned char[code_size];
388 data.command_size_ = code_size;
389 data.value_size_ = (int)variables.size();
390
391 std::for_each(statement.begin(), statement.end(), copy_code(data.command_));
392
393 return true;
394 }
395
396 // デバッグダンプ
397 #ifdef _DEBUG
398 void compiler::debug_dump()
399 {
400 std::cout << "---variables---" << std::endl;
401 variables.dump();
402
403 static const char *op_name[] = {
404 #define VM_NAMETABLE
405 #include "vm_code.h"
406 #undef VM_NAMETABLE
407 "LABEL",
408 } ;
409 std::cout << "---code---" << std::endl;
410
411 int pos = 0;
412 size_t size = statement.size();
413 for (size_t i=0; i < size; i++) {
414 std::cout << std::setw(6) << pos << ": " << op_name[statement[i].op_];
415 if (statement[i].size_ > 1) {
416 std::cout << ", " << statement[i].arg1_;
417 }
418 std::cout << std::endl;
419
420 if (statement[i].op_ != VM_MAXCOMMAND) {
421 pos += statement[i].size_;
422 }
423 }
424 std::cout << "---" << std::endl;
425 }
426 #endif