annotate boost-spirit/Compiler-boost-spirit/compiler.cpp @ 0:db40c85cad7a default tip

upload sample source
author nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
date Mon, 09 May 2011 03:11:59 +0900
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 #include "stdafx.h"
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 #include <iostream>
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 #include <iomanip>
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 #include <memory>
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 #include "compiler.h"
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 #include "parser.h"
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 // コンストラクタ
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 compiler::compiler()
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 : break_index(-1), error_count(0)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 // デストラクタ
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 compiler::~compiler()
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 // コンパイル
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 bool compiler::compile(const std::string &file, vm::data &data)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 // システムコールの設定
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 add_function(vm::SYS_PRINT, TYPE_VOID, "print", "s");
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 add_function(vm::SYS_TOSTR, TYPE_STRING, "str", "i");
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
28
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 // グローバル変数用、変数テーブルをセット
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 variables.push_back(CValueTable());
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 variables[0].set_global();
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
32
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 // 先頭はHALT命令にしておく
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 OpHalt();
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
35
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 bool result = script_parser(file, this); // 構文解析
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
37
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 if (!result)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 return false; // パーサーエラー
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
40
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 int code_size = LabelSetting(); // ラベルにアドレスを設定
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 CraeteData(data, code_size); // バイナリ生成
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 return error_count == 0;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
45
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 // エラーメッセージを出力
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
47
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 void compiler::error(const std::string& m)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 std::cerr << m << std::endl;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 error_count++;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
53
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 // 内部関数の定義
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
55
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 bool compiler::add_function(int index, int type, const char *name, const char *args)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 CFunctionTag func(type);
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 if (!func.SetArgs(args)) // 引数を設定
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 return false;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
61
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 func.SetDeclaration(); // 宣言済み
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 func.SetSystem(); // Systemフラグセット
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 func.SetIndex(index); // システムコール番号を設定
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 if (functions.add(name, func) == 0) {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 return false;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 return true;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
70
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 // 外部変数の定義
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
72
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 struct define_value {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 compiler *comp_;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 int type_;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 define_value(compiler *comp, int type): comp_(comp), type_(type)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
79
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 void operator()(cnode_t node) const
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 comp_->AddValue(type_, node->string(), node->left());
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 } ;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
85
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 void compiler::DefineValue(int type, const std::vector<cnode_t> &node)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 std::for_each(node.begin(), node.end(), define_value(this, type));
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
90
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 // 関数宣言
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
92
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 void compiler::DefineFunction(int type, const std::string &name, const std::vector<int> &args)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 const CFunctionTag *tag = functions.find(name);
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 if (tag) { // 既に宣言済み
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 if (!tag->ChkArgList(args)) {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 error("関数 " + name + " に異なる型の引数が指定されています");
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 return;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 else {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 CFunctionTag func(type);
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 func.SetArgs(args); // 引数を設定
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 func.SetDeclaration(); // 宣言済み
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 func.SetIndex(MakeLabel()); // ラベル登録
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 if (functions.add(name, func) == 0) {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 error("内部エラー:関数テーブルに登録できません");
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
112
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 // 関数定義
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
114 //
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 // 関数が呼ばれた時点のスタック
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
116 //
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 // +--------------+
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 // | arg2 | -5
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 // +--------------+
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 // | arg1 | -4
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 // +--------------+
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 // | arg count | -3
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 // +--------------+
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 // | base_pointer | -2
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
125 // +--------------+
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
126 // | return addr | -1
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
127 // +--------------+
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
128 //
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
129 // したがって、引数の開始アドレスは-4となり、デクリメントしていく。
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
130
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
131 // 引数の変数名を登録
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 struct add_value {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 compiler *comp_;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
134 CValueTable &values_;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
135 mutable int addr_;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 add_value(compiler *comp, CValueTable &values): comp_(comp), values_(values), addr_(-4)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
137 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
138 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
139
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
140 void operator()(const cargdef &arg) const
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
141 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
142 if (!values_.add_arg(arg.type(), arg.name(), addr_)) {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
143 comp_->error("引数 " + arg.name() + " は既に登録されています。");
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
144 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
145 addr_--;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
146 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
147 } ;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
148
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 void compiler::AddFunction(int type, const std::string &name, const std::vector<cargdef> &args, cblock_t block)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
150 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
151 CFunctionTag *tag = functions.find(name);
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
152 if (tag) {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
153 if (tag->IsDefinition()) {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
154 error("関数 " + name + " は既に定義されています");
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
155 return;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
156 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
157 if (tag->IsDeclaration() && !tag->ChkArgList(args)) {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
158 error("関数 " + name + " に異なる型の引数が指定されています");
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
159 return;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
160 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
161 tag->SetDefinition(); // 定義済みに設定
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
162 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
163 else {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
164 CFunctionTag func(type);
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
165 func.SetArgs(args); // 引数を設定
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
166 func.SetDefinition(); // 定義済み
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
167 func.SetIndex(MakeLabel()); // ラベル登録
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
168 tag = functions.add(name, func);
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
169 if (tag == 0)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
170 error("内部エラー:関数テーブルに登録できません");
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
171 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
172
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
173 current_function_name = name; // 処理中の関数名を登録しておく
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
174 current_function_type = type; // 処理中の関数型を登録しておく
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
175 // 関数内関数(入れ子構造)は無いので、
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
176 // グローバル変数1つでよい
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
177
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
178 // 関数のエントリーポイントにラベルを置く
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
179
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
180 SetLabel(tag->GetIndex());
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
181
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
182 BlockIn(); // 変数スタックを増やす
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
183
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
184 // 引数リストを登録
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
185 std::for_each(args.rbegin(), args.rend(), add_value(this, variables.back()));
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
186
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
187 // 文があれば、文を登録
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
188 if (block) {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
189 block->analyze(this);
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
190 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
191
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
192 const CVMCode &code = statement.back();
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 if (type == TYPE_VOID) { // 戻り値無し
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
194 if (code.op_ != VM_RETURN) // returnが無いならば
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
195 OpReturn(); // returnを追加
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
196 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
197 else {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
198 if (code.op_ != VM_RETURNV) { // returnが無いならば
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
199 error("関数 " + name + " の最後にreturn文が有りません。");
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
200 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
201 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
202
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
203 BlockOut(); // 変数スタックを減らす
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
204
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
205 current_function_name.clear(); // 処理中の関数名を消去
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
206 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
207
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
208 // 変数の登録
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
209
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
210 void compiler::AddValue(int type, const std::string &name, cnode_t node)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
211 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
212 int size = 1;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
213 if (node) {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
214 if (node->op() != OP_NUMBER) {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
215 error("配列のサイズは定数で指定してください。");
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
216 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
217 else if (node->number() <= 0) {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
218 error("配列のサイズは1以上の定数が必要です。");
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
219 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
220 size = node->number();
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
221 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
222
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
223 CValueTable &values = variables.back();
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
224 if (!values.add(type, name, size)) {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
225 error("変数 " + name + " は既に登録されています。");
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
226 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
227 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
228
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
229 // ラベル生成
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
230
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
231 int compiler::MakeLabel()
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
232 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
233 int index = (int)labels.size();
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
234 labels.push_back(CLabel(index));
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
235 return index;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
236 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
237
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
238 // ラベルのダミーコマンドをステートメントリストに登録する
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
239
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
240 void compiler::SetLabel(int label)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
241 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
242 statement.push_back(CVMCode(VM_MAXCOMMAND, label));
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
243 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
244
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
245 // 文字列定数をpush
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
246
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
247 void compiler::PushString(const std::string &str)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
248 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
249 PushString((int)text_table.size());
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
250 text_table.insert(text_table.end(), str.begin(), str.end());
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
251 text_table.push_back('\0');
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
252 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
253
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
254 // break文に対応したJmpコマンド生成
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
255
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
256 bool compiler::JmpBreakLabel()
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
257 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
258 if (break_index < 0)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
259 return false;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
260 OpJmp(break_index);
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
261 return true;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
262 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
263
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
264 // ブロック内では、新しい変数セットに変数を登録する
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
265
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
266 void compiler::BlockIn()
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
267 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
268 int start_addr = 0; // 変数アドレスの開始位置
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
269 if (variables.size() > 1) { // ブロックの入れ子は、開始アドレスを続きからにする。
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
270 start_addr = variables.back().size();
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
271 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
272 variables.push_back(CValueTable(start_addr));
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
273 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
274
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
275 // ブロックの終了で、変数スコープが消える(変数セットを削除する)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
276
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
277 void compiler::BlockOut()
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
278 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
279 variables.pop_back();
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
280 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
281
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
282 // ローカル変数用にスタックを確保
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
283
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
284 void compiler::AllocStack()
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
285 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
286 OpAllocStack(variables.back().size());
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
287 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
288
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
289 // ラベル解決
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
290 //
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
291 // 1.アドレスを生成する
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
292 // 2.ダミーのラベルコマンドが有ったアドレスを、ラベルテーブルに登録する
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
293 // 3.Jmpコマンドの飛び先をラベルテーブルに登録されたアドレスにする
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
294
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
295 // アドレス計算
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
296 struct calc_addr {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
297 std::vector<CLabel> &labels_;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
298 int &pos_;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
299 calc_addr(std::vector<CLabel> &labels, int &pos): labels_(labels), pos_(pos)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
300 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
301 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
302 void operator()(const CVMCode &code)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
303 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
304 if (code.op_ == VM_MAXCOMMAND) { // ラベルのダミーコマンド
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
305 labels_[code.arg1_].pos_ = pos_;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
306 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
307 else {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
308 pos_ += code.size_;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
309 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
310 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
311 } ;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
312
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
313 // ジャンプアドレス設定
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
314 struct set_addr {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
315 std::vector<CLabel> &labels_;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
316 set_addr(std::vector<CLabel> &labels): labels_(labels)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
317 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
318 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
319 void operator()(CVMCode &code)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
320 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
321 switch (code.op_) {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
322 case VM_JMP:
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
323 case VM_JMPC:
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
324 case VM_JMPNC:
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
325 case VM_TEST:
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
326 case VM_CALL:
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
327 code.arg1_ = labels_[code.arg1_].pos_;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
328 break;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
329 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
330 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
331 } ;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
332
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
333 int compiler::LabelSetting()
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
334 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
335 // アドレス計算
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
336 int pos = 0;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
337 std::for_each(statement.begin(), statement.end(), calc_addr(labels, pos));
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
338 // ジャンプアドレス設定
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
339 std::for_each(statement.begin(), statement.end(), set_addr(labels));
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
340
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
341 return pos;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
342 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
343
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
344 // バイナリデータ生成
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
345
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
346 struct copy_code {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
347 unsigned char *p;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
348 copy_code(unsigned char *code): p(code)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
349 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
350 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
351 void operator()(const CVMCode &code)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
352 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
353 p = code.Get(p);
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
354 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
355 } ;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
356
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
357 bool compiler::CraeteData(vm::data &data, int code_size)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
358 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
359 const CFunctionTag *tag = GetFunctionTag("main"); // 開始位置
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
360 if (tag == 0) {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
361 error("関数 \"main\" が見つかりません。");
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
362 return false;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
363 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
364
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
365 data.command_ = new unsigned char[code_size];
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
366 data.text_buffer_ = new char[text_table.size()];
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
367 data.command_size_ = code_size;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
368 data.text_size_ = (int)text_table.size();
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
369 data.value_size_ = (int)variables[0].size();
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
370 data.entry_point_ = labels[tag->index_].pos_;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
371
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
372 if (data.text_size_ != 0)
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
373 memcpy(data.text_buffer_, &text_table[0], data.text_size_);
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
374
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
375 std::for_each(statement.begin(), statement.end(), copy_code(data.command_));
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
376
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
377 return true;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
378 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
379
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
380 // デバッグダンプ
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
381 #ifdef _DEBUG
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
382 void compiler::debug_dump()
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
383 {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
384 std::cout << "---variables---" << std::endl;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
385 size_t vsize = variables.size();
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
386 std::cout << "value stack = " << vsize << std::endl;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
387 for (size_t i=0; i<vsize; i++) {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
388 variables[i].dump();
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
389 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
390 std::cout << "---code---" << std::endl;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
391
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
392 static const char *op_name[] = {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
393 #define VM_NAMETABLE
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
394 #include "vm_code.h"
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
395 #undef VM_NAMETABLE
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
396 "LABEL",
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
397 } ;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
398
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
399 int pos = 0;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
400 size_t size = statement.size();
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
401 for (size_t i=0; i < size; i++) {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
402 std::cout << std::setw(6) << pos << ": " << op_name[statement[i].op_];
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
403 if (statement[i].size_ > 1) {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
404 std::cout << ", " << statement[i].arg1_;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
405 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
406 std::cout << std::endl;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
407
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
408 if (statement[i].op_ != VM_MAXCOMMAND) {
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
409 pos += statement[i].size_;
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
410 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
411 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
412 }
db40c85cad7a upload sample source
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
413 #endif