0
|
1 //
|
|
2 // 仮想CPU
|
|
3 //
|
|
4 // switch-caseでの実装例
|
|
5 //
|
|
6 // (c)2008 Chihiro.SAKAMOTO HyperWorks
|
|
7 //
|
|
8 #include <exception>
|
|
9 #include "vm.h"
|
|
10
|
|
11 // 実行
|
|
12 int vm::vcpu::run()
|
|
13 {
|
|
14 command_ = data_.command_; // プログラム格納位置
|
|
15 text_buffer_ = data_.text_buffer_; // テキストデータ格納位置
|
|
16 command_size_ = data_.command_size_; // プログラムの大きさ
|
|
17 text_size_ = data_.text_size_; // データの大きさ
|
|
18
|
|
19 global_value.resize(data_.value_size_); // 外部変数テーブル確保
|
|
20 command_ptr_ = command_ + data_.entry_point_; // プログラムカウンター初期化
|
|
21
|
|
22 // mainをcall
|
|
23 push(0); // mainへの引数カウントをpush
|
|
24 push(0); // stack_baseの初期値をpush
|
|
25 push(0); // プログラム終了位置をpush
|
|
26 stack_base = stack.size(); // スタック参照位置初期化
|
|
27
|
|
28 try {
|
|
29 int op;
|
|
30 while ((op = *command_ptr_++) != VM_HALT) { // Haltするまでループ
|
|
31 switch (op) {
|
|
32 #define VM_SWITCHTABLE
|
|
33 #include "vm_code.h"
|
|
34 #undef VM_SWITCHTABLE
|
|
35 }
|
|
36 }
|
|
37 }
|
|
38 catch (const std::exception &e) {
|
|
39 std::cerr << "例外発生(" << e.what() << ")" << std::endl;
|
|
40 return -1;
|
|
41 }
|
|
42 return top().i_; // main関数戻り値
|
|
43 }
|