forked from Tensor-Array/Tensor-Array
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvmop.cc
More file actions
317 lines (276 loc) · 5.62 KB
/
vmop.cc
File metadata and controls
317 lines (276 loc) · 5.62 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
Copyright 2024 TensorArray-Creators
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <stack>
#include <tensor-array/core/tensor.hh>
#include <iostream>
#include <cstring>
#include "sym_map.h"
#include "vm_instruction.h"
#include "vmop.h"
#include "vm_type.h"
std::stack<tensor_array::value::Tensor> tensor_stack;
std::stack<std::string> var_stack;
std::stack<std::pair<VM_INSTRUCTION*, scope>> call_stack;
tensor_array::value::Tensor ag;
void* aptr;
size_t any_value;
size_t any_type;
void new_int()
{
tensor_array::value::TensorArray<int> tmp2 = {any_value};
tensor_array::value::Tensor tmp1(tmp2);
ag = tmp1;
}
void new_ptr()
{
aptr = reinterpret_cast<void*>(any_value);
}
void new_string()
{
char* str = reinterpret_cast<char*>(any_value);
unsigned int s_len = std::strlen(str);
tensor_array::value::TensorBase tmp1(typeid(char),{s_len}, str);
ag = tmp1;
std::free(str);
}
void op_imm()
{
switch (any_type)
{
case TYPE_INT:
/* code */
new_int();
break;
case TYPE_STRING:
new_string();
break;
case TYPE_PTR:
new_ptr();
break;
default:
break;
}
}
void op_add()
{
if (tensor_stack.empty())
{
throw std::runtime_error("Tensor stack is empty");
}
ag += tensor_stack.top();
tensor_stack.pop();
}
void op_sub()
{
if (tensor_stack.empty())
{
throw std::runtime_error("Tensor stack is empty");
}
ag -= tensor_stack.top();
tensor_stack.pop();
}
void op_mul()
{
if (tensor_stack.empty())
{
throw std::runtime_error("Tensor stack is empty");
}
ag *= tensor_stack.top();
tensor_stack.pop();
}
void op_div()
{
if (tensor_stack.empty())
{
throw std::runtime_error("Tensor stack is empty");
}
ag /= tensor_stack.top();
tensor_stack.pop();
}
void op_matmul()
{
if (tensor_stack.empty())
{
throw std::runtime_error("Tensor stack is empty");
}
ag = tensor_array::value::matmul(ag, tensor_stack.top());
tensor_stack.pop();
}
void op_pos()
{
ag = +ag;
}
void op_neg()
{
ag = -ag;
}
void op_and()
{
ag = ag && tensor_stack.top();
tensor_stack.pop();
}
void op_or()
{
if (tensor_stack.empty())
{
throw std::runtime_error("Tensor stack is empty");
}
ag = ag || tensor_stack.top();
tensor_stack.pop();
}
void op_not()
{
if (tensor_stack.empty())
{
throw std::runtime_error("Tensor stack is empty");
}
ag = !ag;
}
void op_eq()
{
if (tensor_stack.empty())
{
throw std::runtime_error("Tensor stack is empty");
}
ag = ag == tensor_stack.top();
tensor_stack.pop();
}
void op_ne()
{
if (tensor_stack.empty())
{
throw std::runtime_error("Tensor stack is empty");
}
ag = ag != tensor_stack.top();
tensor_stack.pop();
}
void op_lt()
{
if (tensor_stack.empty())
{
throw std::runtime_error("Tensor stack is empty");
}
ag = ag < tensor_stack.top();
tensor_stack.pop();
}
void op_gt()
{
if (tensor_stack.empty())
{
throw std::runtime_error("Tensor stack is empty");
}
ag = ag > tensor_stack.top();
tensor_stack.pop();
}
void op_le()
{
if (tensor_stack.empty())
{
throw std::runtime_error("Tensor stack is empty");
}
ag = ag <= tensor_stack.top();
tensor_stack.pop();
}
void op_ge()
{
if (tensor_stack.empty())
{
throw std::runtime_error("Tensor stack is empty");
}
ag = ag >= tensor_stack.top();
tensor_stack.pop();
}
void op_shl()
{
// ag = ag << bg;
}
void op_shr()
{
// ag = ag >> bg;
}
void op_call()
{
VM_INSTRUCTION* pc1 = (VM_INSTRUCTION*)*pc++;
call_stack.push({std::move(pc), std::move(sym_map)});
pc = pc1;
}
void op_ret()
{
pc = call_stack.top().first;
sym_map = std::move(call_stack.top().second);
call_stack.pop();
}
void op_open()
{
// Implementation for opening a file or resource
}
void op_read()
{
// Implementation for reading from a file or resource
}
void op_close()
{
// Implementation for closing a file or resource
}
void op_prtf()
{
// Implementation for printing formatted output
}
void op_malc()
{
// Implementation for memory allocation
}
void op_mset()
{
// Implementation for setting memory
}
void op_mcmp()
{
// Implementation for memory comparison
}
void op_exit()
{
// Implementation for exiting the program
// std::cout << ag << std::endl;
}
void op_push()
{
tensor_stack.push(ag);
}
void op_ptr_push()
{
var_stack.push(reinterpret_cast<char*>(aptr));
std::free(aptr);
}
void op_get()
{
char *var_name = reinterpret_cast<char*>(aptr);
sym_data& temp = sym_map[var_name];
std::free(aptr);
ag = *reinterpret_cast<tensor_array::value::Tensor*>(temp.data);
}
void op_set()
{
if (!var_stack.empty())
{
std::string& var_name = var_stack.top();
sym_data& temp = sym_map[var_name];
delete temp.data; // Set the top of the stack to ag
temp.data = new tensor_array::value::Tensor(ag);
var_stack.pop();
}
else
{
throw std::runtime_error("Tensor stack is empty");
}
}