-
Notifications
You must be signed in to change notification settings - Fork 0
/
TinyCInterpreter.cpp
executable file
·218 lines (173 loc) · 5.29 KB
/
TinyCInterpreter.cpp
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
211
212
213
214
215
216
217
218
#include "TinyCInterpreter.h"
#include <stdio.h>
int c99Interpreter::tcc_state_used_ = 0;
c99Interpreter::c99Interpreter() {
tcc_sym_ = NULL;
scripts_.clear();
c99Interpreter::tcc_state_used_ = 0;
searchPath_ = "./";
}
c99Interpreter::~c99Interpreter() {
if (tcc_sym_) tcc_delete(tcc_sym_);
for (auto& script : scripts_) {
free(script.second.code_mem_);
script.second.funcs_.clear();
}
c99Interpreter::tcc_state_used_ = 0;
}
int
c99Interpreter::setSearchPath(const char* path) {
if (!path) return -1;
searchPath_ = path;
return 0;
}
int
c99Interpreter::compileScript(const char* scriptContent, const char* scriptName) {
if (!scriptContent || !scriptName) {
fprintf(stderr, "param null.\n");
return -1;
}
if (scripts_.count(scriptName) > 0) {
fprintf(stderr, "script name repeat.\n");
return -1;
}
if (c99Interpreter::tcc_state_used_ == 1) {
fprintf(stderr, "tcc state in use.\n");
return -1;
}
static char source_head[] = {"#define c99_export \n"};
c99Interpreter::tcc_state_used_ = 1;
std::string full_source;
int ret = 0;
tcc_sym_ = tcc_new();
if (!tcc_sym_) {
fprintf(stderr, "could not create tcc state\n");
ret = -1;
goto compile_err_exit;
}
{
tcc_set_lib_path(tcc_sym_, searchPath_.c_str());
tcc_set_output_type(tcc_sym_, TCC_OUTPUT_MEMORY);
}
full_source = std::string(source_head) + scriptContent;
if (tcc_compile_string(tcc_sym_, full_source.c_str()) == -1) {
fprintf(stderr, "compile error.\n");
ret = -1;
goto compile_err_exit;
}
{
//get all export symbol
scripts_.insert(std::make_pair(scriptName, tcc_func_list()));
auto& func_list = scripts_[scriptName];
func_list.scriptContent_ = scriptContent;
}
return ret;
compile_err_exit:
if (tcc_sym_) tcc_delete(tcc_sym_);
tcc_sym_ = NULL;
c99Interpreter::tcc_state_used_ = 0;
return ret;
}
int
c99Interpreter::addCustomSymbols(const char* symbol_name, void* func) {
if (!tcc_sym_) {
return -1;
}
tcc_add_symbol(tcc_sym_, symbol_name, (void*)func);
return 0;
}
int
c99Interpreter::buildSymbol(const char* scriptName) {
if (scripts_.count(scriptName) <= 0) {
return -1;
}
if (!tcc_sym_) {
return -1;
}
int ret = 0;
auto& func_list = scripts_[scriptName];
systemSymbols();
utilsSymbols();
int size = tcc_relocate(tcc_sym_, NULL);
if (size == -1) {
fprintf(stderr, "tcc relocate error.\n");
ret = -1;
goto build_exit;
}
func_list.code_mem_ = malloc(size);
tcc_relocate(tcc_sym_, func_list.code_mem_);
{
//analasy source code to get all export function
std::vector<std::string> funcs;
analyse_export_funcs(func_list.scriptContent_.c_str(), funcs);
for (auto& name : funcs) {
func_list.funcs_.insert(std::make_pair(name, tcc_get_symbol(tcc_sym_, name.c_str())));
}
}
build_exit:
tcc_delete(tcc_sym_);
tcc_sym_ = NULL;
c99Interpreter::tcc_state_used_ = 0;
return ret;
}
const tcc_func_list*
c99Interpreter::getScript(const char* scriptName) {
if (!scriptName)
return NULL;
if (scripts_.count(scriptName) <= 0)
return NULL;
return &(scripts_[scriptName]);
}
const void*
c99Interpreter::getFunInScript(const char* scriptName, const char* funcName) {
if (!scriptName || !funcName)
return NULL;
if (scripts_.count(scriptName) <= 0)
return NULL;
if (scripts_[scriptName].funcs_.count(funcName) <= 0) {
return NULL;
}
return scripts_[scriptName].funcs_[funcName];
}
int
c99Interpreter::analyse_export_funcs(const char* source, std::vector<std::string>& funcs) {
if (!source)
return -1;
//c99_export xxx_fun_name
static char expect[] = {'c', '9', '9', '_', 'e', 'x', 'p', 'o', 'r', 't', 1};
int state_index = 0;
std::string func_name = "";
for (int i = 0; source[i] != 0; i++) {
if (expect[state_index] == 1) {
if (source[i] == '(') {
if (func_name != "")
funcs.push_back(func_name);
state_index = 0;
func_name = "";
}
else if (!isalpha(source[i]) && !isalnum(source[i]) && source[i] != '_') {
if (source[i] == ' ' || source[i] == '\t' || source[i] == '\n') {
if (func_name == "")
continue;
}
else {
state_index = 0;
func_name = "";
}
}
else {
func_name.append(1, source[i]);
}
}
else if (source[i] == expect[state_index]) {
state_index++;
printf("%c %d\n", source[i], state_index-1);
continue;
}
}
return 0;
}
void c99Interpreter::systemSymbols() {
}
void c99Interpreter::utilsSymbols() {
}