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::internal_call(data &program, int entry, int *arg, int narg)
|
|
13 {
|
|
14 if (program.entry_point_[entry] < 0) // エントリーポイントが無い
|
|
15 return 0;
|
|
16
|
|
17 command_ = program.command_; // プログラム格納位置
|
|
18 text_buffer_ = program.text_buffer_; // テキストデータ格納位置
|
|
19 command_size_ = program.command_size_; // プログラムの大きさ
|
|
20 text_size_ = program.text_size_; // データの大きさ
|
|
21
|
|
22 global_value.resize(program.value_size_); // 外部変数テーブル確保
|
|
23 command_ptr_ = command_ + program.entry_point_[entry]; // プログラムカウンター初期化
|
|
24
|
|
25 // スクリプトをcall
|
|
26 for (int i=0; i<narg; i++) {
|
|
27 push(arg[i]); // 引数をpush
|
|
28 }
|
|
29 push(narg); // 引数カウントをpush
|
|
30 push(0); // stack_baseの初期値をpush
|
|
31 push(0); // プログラム終了位置をpush
|
|
32 stack_base = stack.size(); // スタック参照位置初期化
|
|
33
|
|
34 try {
|
|
35 int op;
|
|
36 while ((op = *command_ptr_++) != VM_HALT) { // Haltするまでループ
|
|
37 switch (op) {
|
|
38 #define VM_SWITCHTABLE
|
|
39 #include "vm_code.h"
|
|
40 #undef VM_SWITCHTABLE
|
|
41 }
|
|
42 }
|
|
43 }
|
|
44 catch (const std::exception &e) {
|
|
45 std::cerr << "例外発生(" << e.what() << ")" << std::endl;
|
|
46 return -1;
|
|
47 }
|
|
48 if (stack.empty()) { // void型関数
|
|
49 return 0;
|
|
50 }
|
|
51
|
|
52 int result = top().i_; // 関数戻り値
|
|
53 pop();
|
|
54 return result;
|
|
55 }
|