File Coverage

lib/SPVM/Builder/src/spvm_op.c
Criterion Covered Total %
statement 1421 1521 93.4
branch 323 412 78.4
condition n/a
subroutine n/a
pod n/a
total 1744 1933 90.2


line stmt bran cond sub pod time code
1             #include
2             #include
3             #include
4             #include
5             #include
6             #include
7             #include
8             #include "spvm_compiler.h"
9             #include "spvm_list.h"
10             #include "spvm_hash.h"
11             #include "spvm_op.h"
12             #include "spvm_sub.h"
13             #include "spvm_constant.h"
14             #include "spvm_field.h"
15             #include "spvm_my.h"
16             #include "spvm_var.h"
17             #include "spvm_enumeration_value.h"
18             #include "spvm_type.h"
19             #include "spvm_enumeration.h"
20             #include "spvm_package.h"
21             #include "spvm_field_access.h"
22             #include "spvm_call_sub.h"
23             #include "spvm_type.h"
24             #include "spvm_opcode_builder.h"
25             #include "spvm_op_checker.h"
26             #include "spvm_switch_info.h"
27             #include "spvm_descriptor.h"
28             #include "spvm_compiler_allocator.h"
29             #include "spvm_limit.h"
30             #include "spvm_use.h"
31             #include "spvm_package_var.h"
32             #include "spvm_package_var_access.h"
33             #include "spvm_csource_builder_precompile.h"
34             #include "spvm_block.h"
35             #include "spvm_basic_type.h"
36             #include "spvm_case_info.h"
37             #include "spvm_array_field_access.h"
38             #include "spvm_string_buffer.h"
39             #include "spvm_allow.h"
40              
41              
42              
43              
44              
45              
46              
47              
48              
49             const char* const SPVM_OP_C_ID_NAMES[] = {
50             "IF",
51             "UNLESS",
52             "ELSIF",
53             "ELSE",
54             "FOR",
55             "WHILE",
56             "NULL",
57             "LIST",
58             "PUSHMARK",
59             "GRAMMAR",
60             "NAME",
61             "PACKAGE",
62             "MY",
63             "FIELD",
64             "SUB",
65             "ENUM",
66             "DESCRIPTOR",
67             "ENUMERATION_VALUE",
68             "BLOCK",
69             "ENUM_BLOCK",
70             "CLASS_BLOCK",
71             "TYPE",
72             "CONSTANT",
73             "INC",
74             "DEC",
75             "PRE_INC",
76             "POST_INC",
77             "PRE_DEC",
78             "POST_DEC",
79             "MINUS",
80             "PLUS",
81             "EQ",
82             "NE",
83             "LT",
84             "LE",
85             "GT",
86             "GE",
87             "ADD",
88             "SUBTRACT",
89             "MULTIPLY",
90             "DIVIDE",
91             "BIT_AND",
92             "BIT_OR",
93             "BIT_XOR",
94             "BIT_NOT",
95             "REMAINDER",
96             "LEFT_SHIFT",
97             "RIGHT_ARITHMETIC_SHIFT",
98             "RIGHT_LOGICAL_SHIFT",
99             "LOGICAL_AND",
100             "LOGICAL_OR",
101             "LOGICAL_NOT",
102             "ARRAY_ACCESS",
103             "ASSIGN",
104             "CALL_SUB",
105             "FIELD_ACCESS",
106             "USE",
107             "RETURN",
108             "LAST",
109             "NEXT",
110             "LOOP",
111             "VAR",
112             "CONVERT",
113             "UNDEF",
114             "ARRAY_LENGTH",
115             "CONDITION",
116             "CONDITION_NOT",
117             "DIE",
118             "SWITCH",
119             "CASE",
120             "DEFAULT",
121             "SWITCH_CONDITION",
122             "VOID",
123             "EVAL",
124             "BLOCK_END",
125             "EXCEPTION_VAR",
126             "NEW",
127             "STAB",
128             "BYTE",
129             "SHORT",
130             "INT",
131             "LONG",
132             "FLOAT",
133             "DOUBLE",
134             "STRING",
135             "OBJECT",
136             "WEAKEN",
137             "WEAKEN_FIELD",
138             "UNWEAKEN",
139             "UNWEAKEN_FIELD",
140             "ISWEAK",
141             "ISWEAK_FIELD",
142             "SPECIAL_ASSIGN",
143             "CONCAT",
144             "SET",
145             "GET",
146             "PACKAGE_VAR",
147             "PACKAGE_VAR_ACCESS",
148             "ARRAY_INIT",
149             "BOOL",
150             "LOOP_INCREMENT",
151             "SELF",
152             "CHECK_CONVERT",
153             "STRING_EQ",
154             "STRING_NE",
155             "STRING_GT",
156             "STRING_GE",
157             "STRING_LT",
158             "STRING_LE",
159             "ISA",
160             "SEQUENCE",
161             "PRECOMPILE",
162             "SCALAR",
163             "ARRAY_FIELD_ACCESS",
164             "REF",
165             "DEREF",
166             "DOT3",
167             "STRING_LENGTH",
168             "RW",
169             "RO",
170             "WO",
171             "BEGIN",
172             "REQUIRE",
173             "IF_REQUIRE",
174             "CURRENT_PACKAGE",
175             "FREE_TMP",
176             "REFCNT",
177             "ALLOW",
178             "BREAK",
179             };
180              
181 6029           int32_t SPVM_OP_is_allowed(SPVM_COMPILER* compiler, SPVM_OP* op_package_current, SPVM_OP* op_package_dist) {
182            
183 6029           SPVM_LIST* op_allows = op_package_dist->uv.package->op_allows;
184            
185 6029           const char* current_package_name = op_package_current->uv.package->name;
186 6029           const char* dist_package_name = op_package_dist->uv.package->name;
187            
188 6029           int32_t is_allowed = 0;
189 6029 100         if (strcmp(current_package_name, dist_package_name) == 0) {
190 5841           is_allowed = 1;
191             }
192             else {
193 188 100         for (int32_t i = 0; i < op_allows->length; i++) {
194 180           SPVM_OP* op_allow = SPVM_LIST_fetch(op_allows, i);
195 180           SPVM_ALLOW* allow = op_allow->uv.allow;
196 180           SPVM_OP* op_type = allow->op_type;
197 180           const char* allow_basic_type_name = op_type->uv.type->basic_type->name;
198 180 50         if (strcmp(current_package_name, allow_basic_type_name) == 0) {
199 180           is_allowed = 1;
200 180           break;
201             }
202             }
203             }
204            
205 6029           return is_allowed;
206             }
207              
208 72966           SPVM_OP* SPVM_OP_new_op_assign_bool(SPVM_COMPILER* compiler, SPVM_OP* op_operand, const char* file, int32_t line) {
209 72966           SPVM_OP* op_bool = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_BOOL, file, line);
210 72966           SPVM_OP_insert_child(compiler, op_bool, op_bool->last, op_operand);
211              
212 72966           SPVM_OP* op_name_var = SPVM_OP_new_op_name(compiler, "@condition_flag", file, line);
213 72966           SPVM_OP* op_var = SPVM_OP_new_op_var(compiler, op_name_var);
214 72966           SPVM_OP* op_assign = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, file, line);
215 72966           SPVM_OP_build_assign(compiler, op_assign, op_var, op_bool);
216            
217 72966           return op_assign;
218             }
219              
220 621908           SPVM_OP* SPVM_OP_new_op_my(SPVM_COMPILER* compiler, SPVM_MY* my, const char* file, int32_t line) {
221 621908           SPVM_OP* op_my = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_MY, file, line);
222 621908           op_my->uv.my = my;
223 621908           my->op_my = op_my;
224            
225 621908           return op_my;
226             }
227              
228 1807886           SPVM_OP* SPVM_OP_new_op_type(SPVM_COMPILER* compiler, SPVM_TYPE* type, const char* file, int32_t line) {
229 1807886           SPVM_OP* op_type = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_TYPE, file, line);
230 1807886           op_type->uv.type = type;
231            
232 1807886           type->op_type = op_type;
233            
234 1807886           SPVM_LIST_push(compiler->op_types, op_type);
235            
236 1807886           return op_type;
237             }
238              
239 806           SPVM_OP* SPVM_OP_build_deref(SPVM_COMPILER* compiler, SPVM_OP* op_deref, SPVM_OP* op_var) {
240            
241 806           SPVM_OP_insert_child(compiler, op_deref, op_deref->last, op_var);
242              
243 806           return op_deref;
244             }
245              
246 483           SPVM_OP* SPVM_OP_build_ref(SPVM_COMPILER* compiler, SPVM_OP* op_ref, SPVM_OP* op_var) {
247            
248 483           SPVM_OP_insert_child(compiler, op_ref, op_ref->last, op_var);
249            
250 483           return op_ref;
251             }
252              
253 689668           int32_t SPVM_OP_is_mutable(SPVM_COMPILER* compiler, SPVM_OP* op) {
254             (void)compiler;
255            
256 689668 50         switch (op->id) {
257             case SPVM_OP_C_ID_VAR:
258             case SPVM_OP_C_ID_PACKAGE_VAR_ACCESS:
259             case SPVM_OP_C_ID_ARRAY_ACCESS:
260             case SPVM_OP_C_ID_FIELD_ACCESS:
261             case SPVM_OP_C_ID_DEREF:
262             case SPVM_OP_C_ID_EXCEPTION_VAR:
263             case SPVM_OP_C_ID_ARRAY_FIELD_ACCESS:
264 689668           return 1;
265             }
266            
267 0           return 0;
268             }
269              
270 55074           int32_t SPVM_OP_is_rel_op(SPVM_COMPILER* compiler, SPVM_OP* op) {
271             (void)compiler;
272            
273 55074 50         switch (op->id) {
274             case SPVM_OP_C_ID_NUMERIC_EQ:
275             case SPVM_OP_C_ID_NUMERIC_NE:
276             case SPVM_OP_C_ID_NUMERIC_GT:
277             case SPVM_OP_C_ID_NUMERIC_GE:
278             case SPVM_OP_C_ID_NUMERIC_LT:
279             case SPVM_OP_C_ID_NUMERIC_LE:
280             case SPVM_OP_C_ID_STRING_EQ:
281             case SPVM_OP_C_ID_STRING_NE:
282             case SPVM_OP_C_ID_STRING_GT:
283             case SPVM_OP_C_ID_STRING_GE:
284             case SPVM_OP_C_ID_STRING_LT:
285             case SPVM_OP_C_ID_STRING_LE:
286             case SPVM_OP_C_ID_ISA:
287 0           return 1;
288             }
289            
290 55074           return 0;
291             }
292              
293 796048           SPVM_OP* SPVM_OP_build_var(SPVM_COMPILER* compiler, SPVM_OP* op_var_name) {
294            
295 796048           SPVM_OP* op_var = SPVM_OP_new_op_var(compiler, op_var_name);
296            
297 796048           return op_var;
298             }
299              
300 646           SPVM_OP* SPVM_OP_build_package_var_access(SPVM_COMPILER* compiler, SPVM_OP* op_package_var_name) {
301            
302             // Package var op
303 646           SPVM_OP* op_package_var_access = SPVM_OP_new_op_package_var_access(compiler, op_package_var_name);
304            
305 646           return op_package_var_access;
306             }
307              
308 25321           SPVM_OP* SPVM_OP_new_op_descriptor(SPVM_COMPILER* compiler, int32_t id, const char* file, int32_t line) {
309 25321           SPVM_OP* op_descriptor = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_DESCRIPTOR, file, line);
310            
311 25321           SPVM_DESCRIPTOR* descriptor = SPVM_DESCRIPTOR_new(compiler);
312 25321           descriptor->id = id;
313 25321           op_descriptor->uv.descriptor = descriptor;
314            
315 25321           return op_descriptor;
316             }
317              
318 2724           SPVM_OP* SPVM_OP_new_op_undef(SPVM_COMPILER* compiler, const char* file, int32_t line) {
319 2724           SPVM_OP* op_undef = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_UNDEF, file, line);
320            
321 2724           return op_undef;
322             }
323              
324 164207           SPVM_OP* SPVM_OP_new_op_block(SPVM_COMPILER* compiler, const char* file, int32_t line) {
325 164207           SPVM_OP* op_block = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_BLOCK, file, line);
326            
327 164207           SPVM_BLOCK* block = SPVM_BLOCK_new(compiler);
328 164207           op_block->uv.block = block;
329            
330 164207           return op_block;
331             }
332              
333 525546           SPVM_OP* SPVM_OP_new_op_name(SPVM_COMPILER* compiler, const char* name, const char* file, int32_t line) {
334            
335 525546           SPVM_OP* op_name = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_NAME, file, line);
336            
337 525546           op_name->uv.name = name;
338            
339 525546           return op_name;
340             }
341              
342 956708           SPVM_OP* SPVM_OP_new_op_var(SPVM_COMPILER* compiler, SPVM_OP* op_name) {
343            
344 956708           SPVM_OP* op_var = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_VAR, op_name->file, op_name->line);
345              
346 956708           SPVM_VAR* var = SPVM_VAR_new(compiler);
347 956708           var->op_name = op_name;
348 956708           op_var->uv.var = var;
349            
350 956708           return op_var;
351             }
352              
353 646           SPVM_OP* SPVM_OP_new_op_package_var_access(SPVM_COMPILER* compiler, SPVM_OP* op_package_var_name) {
354            
355 646           SPVM_OP* op_package_var_access = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_PACKAGE_VAR_ACCESS, op_package_var_name->file, op_package_var_name->line);
356              
357 646           SPVM_PACKAGE_VAR_ACCESS* package_var_access = SPVM_PACKAGE_VAR_ACCESS_new(compiler);
358 646           package_var_access->op_name = op_package_var_name;
359 646           op_package_var_access->uv.package_var_access = package_var_access;
360            
361 646           return op_package_var_access;
362             }
363              
364 41046           SPVM_OP* SPVM_OP_new_op_var_clone(SPVM_COMPILER* compiler, SPVM_OP* original_op_var, const char* file, int32_t line) {
365             (void)compiler;
366            
367 41046           SPVM_VAR* var = SPVM_VAR_new(compiler);
368 41046           SPVM_OP* op_var = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_VAR, file, line);
369            
370 41046           SPVM_OP* op_name = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_NAME, file, line);
371 41046           op_name->uv.name = original_op_var->uv.var->my->op_name->uv.name;
372 41046           var->op_name = op_name;
373 41046           var->my = original_op_var->uv.var->my;
374 41046           op_var->uv.var = var;
375            
376 41046 50         assert(original_op_var->uv.var != op_var->uv.var);
377            
378 41046           return op_var;
379             }
380              
381 907           SPVM_OP* SPVM_OP_new_op_var_clone_var_or_assign(SPVM_COMPILER* compiler, SPVM_OP* original_op_var_or_assign) {
382             (void)compiler;
383            
384             SPVM_OP* original_op_var;
385 907 100         if (original_op_var_or_assign->id == SPVM_OP_C_ID_ASSIGN) {
386 263 50         if (original_op_var_or_assign->last->id == SPVM_OP_C_ID_VAR) {
387 263           original_op_var = original_op_var_or_assign->last;
388             }
389             else {
390 0           assert(0);
391             }
392             }
393 644 50         else if (original_op_var_or_assign->id == SPVM_OP_C_ID_VAR) {
394 644           original_op_var = original_op_var_or_assign;
395             }
396             else {
397 0           assert(0);
398             }
399            
400 907           SPVM_OP* op_var = SPVM_OP_new_op_var_clone(compiler, original_op_var, original_op_var_or_assign->file, original_op_var_or_assign->line);
401 907           return op_var;
402             }
403              
404              
405 171           SPVM_OP* SPVM_OP_new_op_field_access_clone(SPVM_COMPILER* compiler, SPVM_OP* original_op_field_access) {
406             (void)compiler;
407            
408 171           SPVM_OP* op_var_invoker = SPVM_OP_new_op_var_clone_var_or_assign(compiler, original_op_field_access->first);
409            
410 171           SPVM_OP* op_name_field = SPVM_OP_new_op_name(compiler, original_op_field_access->uv.field_access->op_name->uv.name, original_op_field_access->file, original_op_field_access->line);
411 171           SPVM_OP* op_field_access = SPVM_OP_build_field_access(compiler, op_var_invoker, op_name_field);
412            
413 171           op_field_access->uv.field_access = original_op_field_access->uv.field_access;
414            
415 171           return op_field_access;
416             }
417              
418 203           SPVM_OP* SPVM_OP_new_op_array_access_clone(SPVM_COMPILER* compiler, SPVM_OP* original_op_array_access) {
419             (void)compiler;
420            
421 203           SPVM_OP* op_var_array = SPVM_OP_new_op_var_clone_var_or_assign(compiler, original_op_array_access->first);
422 203           SPVM_OP* op_var_index = SPVM_OP_new_op_var_clone_var_or_assign(compiler, original_op_array_access->last);
423            
424 203           SPVM_OP* op_array_access = SPVM_OP_build_array_access(compiler, op_var_array, op_var_index);
425            
426 203           return op_array_access;
427             }
428              
429 105           SPVM_OP* SPVM_OP_new_op_array_field_access_clone(SPVM_COMPILER* compiler, SPVM_OP* original_op_array_field_access) {
430             (void)compiler;
431            
432 105           SPVM_OP* op_array_field_access = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ARRAY_FIELD_ACCESS, original_op_array_field_access->file, original_op_array_field_access->line);
433            
434 105           SPVM_OP* op_var_array = SPVM_OP_new_op_var_clone_var_or_assign(compiler, original_op_array_field_access->first);
435 105           SPVM_OP* op_var_index = SPVM_OP_new_op_var_clone_var_or_assign(compiler, original_op_array_field_access->last);
436              
437 105           SPVM_OP_insert_child(compiler, op_array_field_access, op_array_field_access->last, op_var_array);
438 105           SPVM_OP_insert_child(compiler, op_array_field_access, op_array_field_access->last, op_var_index);
439            
440 105           op_array_field_access->uv.array_field_access = original_op_array_field_access->uv.array_field_access;
441            
442 105           return op_array_field_access;
443             }
444              
445 91           SPVM_OP* SPVM_OP_new_op_package_var_access_clone(SPVM_COMPILER* compiler, SPVM_OP* original_op_package_var_access) {
446             (void)compiler;
447            
448 91           SPVM_OP* op_package_var_access = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_PACKAGE_VAR_ACCESS, original_op_package_var_access->file, original_op_package_var_access->line);
449            
450 91           op_package_var_access->uv.package_var_access = original_op_package_var_access->uv.package_var_access;
451            
452 91           return op_package_var_access;
453             }
454              
455 120           SPVM_OP* SPVM_OP_new_op_deref_clone(SPVM_COMPILER* compiler, SPVM_OP* original_op_deref) {
456             (void)compiler;
457            
458 120           SPVM_OP* op_deref = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_DEREF, original_op_deref->file, original_op_deref->line);
459            
460 120           SPVM_OP* op_var = SPVM_OP_new_op_var_clone_var_or_assign(compiler, original_op_deref->first);
461            
462 120           SPVM_OP_build_deref(compiler, op_deref, op_var);
463            
464 120           return op_deref;
465             }
466              
467 16481           SPVM_OP* SPVM_OP_new_op_term_mutable_clone(SPVM_COMPILER* compiler, SPVM_OP* original_op_term_mutable) {
468            
469             SPVM_OP* op_term_mutable;
470 16481           switch (original_op_term_mutable->id) {
471             case SPVM_OP_C_ID_VAR:
472 15791           op_term_mutable = SPVM_OP_new_op_var_clone(compiler, original_op_term_mutable, original_op_term_mutable->file, original_op_term_mutable->line);
473 15791           break;
474             case SPVM_OP_C_ID_PACKAGE_VAR_ACCESS:
475 91           op_term_mutable = SPVM_OP_new_op_package_var_access_clone(compiler, original_op_term_mutable);
476 91           break;
477             case SPVM_OP_C_ID_ARRAY_ACCESS:
478 203           op_term_mutable = SPVM_OP_new_op_array_access_clone(compiler, original_op_term_mutable);
479 203           break;
480             case SPVM_OP_C_ID_FIELD_ACCESS:
481 171           op_term_mutable = SPVM_OP_new_op_field_access_clone(compiler, original_op_term_mutable);
482 171           break;
483             case SPVM_OP_C_ID_ARRAY_FIELD_ACCESS:
484 105           op_term_mutable = SPVM_OP_new_op_array_field_access_clone(compiler, original_op_term_mutable);
485 105           break;
486             case SPVM_OP_C_ID_DEREF:
487 120           op_term_mutable = SPVM_OP_new_op_deref_clone(compiler, original_op_term_mutable);
488 120           break;
489             default:
490 0           assert(0);
491             }
492            
493 16481           return op_term_mutable;
494             }
495              
496 1972614           SPVM_OP* SPVM_OP_get_parent(SPVM_COMPILER* compiler, SPVM_OP* op_target) {
497             (void)compiler;
498            
499             SPVM_OP* op_parent;
500 1972614           SPVM_OP* op_cur = op_target;
501             while (1) {
502 4168360 100         if (op_cur->moresib) {
503 2195746           op_cur = op_cur->sibparent;
504             }
505             else {
506 1972614           op_parent = op_cur->sibparent;
507 1972614           break;
508             }
509 2195746           }
510            
511 1972614           return op_parent;
512             }
513              
514 986307           void SPVM_OP_get_before(SPVM_COMPILER* compiler, SPVM_OP* op_target, SPVM_OP** op_before_ptr, int32_t* next_is_child_ptr) {
515              
516             // Get parent
517 986307           SPVM_OP* op_parent = SPVM_OP_get_parent(compiler, op_target);
518            
519             SPVM_OP* op_before;
520 986307           int32_t next_is_child = 0;
521 986307 100         if (op_parent->first == op_target) {
522 518164           op_before = op_parent;
523 518164           next_is_child = 1;
524             }
525             else {
526 468143           op_before = op_parent->first;
527             while (1) {
528 1447094 100         if (op_before->sibparent == op_target) {
529 468143           break;
530             }
531             else {
532 978951           op_before = op_before->sibparent;
533             }
534 978951           }
535             }
536            
537 986307           *op_before_ptr = op_before;
538 986307           *next_is_child_ptr = next_is_child;
539 986307           }
540              
541             // Replace target op with replace op
542 478482           void SPVM_OP_replace_op(SPVM_COMPILER* compiler, SPVM_OP* op_target, SPVM_OP* op_replace) {
543             (void)compiler;
544            
545             // Get parent op
546 478482           SPVM_OP* op_parent = SPVM_OP_get_parent(compiler, op_target);
547            
548 478482           int32_t op_target_is_last_child = op_parent->last == op_target;
549              
550             // Get before op
551             int32_t next_is_child;
552             SPVM_OP* op_before;
553 478482           SPVM_OP_get_before(compiler, op_target, &op_before, &next_is_child);
554            
555             // Stab
556 478482 100         if (next_is_child) {
557             // One child
558 249051 100         if (op_before->first == op_before->last) {
559 118482           op_before->first = op_replace;
560 118482           op_before->last = op_replace;
561             }
562             // More
563             else {
564 249051           op_before->first = op_replace;
565             }
566             }
567             else {
568 229431           op_before->sibparent = op_replace;
569             }
570 478482           op_replace->moresib = op_target->moresib;
571 478482           op_replace->sibparent = op_target->sibparent;
572              
573 478482 100         if (op_target_is_last_child) {
574 267441           op_parent->last = op_replace;
575             }
576 478482           }
577              
578             // Cut op and insert stab into original position and return stab
579 507825           SPVM_OP* SPVM_OP_cut_op(SPVM_COMPILER* compiler, SPVM_OP* op_target) {
580             // Get parent op
581 507825           SPVM_OP* op_parent = SPVM_OP_get_parent(compiler, op_target);
582            
583 507825           int32_t op_target_is_last_child = op_parent->last == op_target;
584              
585             // Get before op
586             int32_t next_is_child;
587             SPVM_OP* op_before;
588 507825           SPVM_OP_get_before(compiler, op_target, &op_before, &next_is_child);
589            
590             // Stab
591 507825           SPVM_OP* op_stab = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_STAB, op_target->file, op_target->line);
592 507825 100         if (next_is_child) {
593            
594             // One child
595 269113 100         if (op_before->first == op_before->last) {
596 131488           op_before->first = op_stab;
597 131488           op_before->last = op_stab;
598             }
599             // More
600             else {
601 269113           op_before->first = op_stab;
602             }
603             }
604             else {
605 238712           op_before->sibparent = op_stab;
606             }
607 507825           op_stab->moresib = op_target->moresib;
608 507825           op_stab->sibparent = op_target->sibparent;
609              
610             // Clear target
611 507825           op_target->moresib = 0;
612 507825           op_target->sibparent = NULL;
613            
614 507825 100         if (op_target_is_last_child) {
615 288496           op_parent->last = op_stab;
616             }
617            
618 507825           return op_stab;
619             }
620              
621 5333           SPVM_OP* SPVM_OP_new_op_constant_byte(SPVM_COMPILER* compiler, int8_t value, const char* file, int32_t line) {
622 5333           SPVM_OP* op_constant = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_CONSTANT, file, line);
623 5333           SPVM_CONSTANT* constant = SPVM_CONSTANT_new(compiler);
624            
625 5333           constant->value.bval = value;
626 5333           SPVM_OP* op_constant_type = SPVM_OP_new_op_byte_type(compiler, file, line);
627 5333           constant->type = op_constant_type->uv.type;
628            
629 5333           op_constant->uv.constant = constant;
630              
631 5333           SPVM_LIST_push(compiler->op_constants, op_constant);
632            
633 5333           constant->op_constant = op_constant;
634            
635 5333           return op_constant;
636             }
637              
638 0           SPVM_OP* SPVM_OP_new_op_constant_short(SPVM_COMPILER* compiler, int16_t value, const char* file, int32_t line) {
639 0           SPVM_OP* op_constant = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_CONSTANT, file, line);
640 0           SPVM_CONSTANT* constant = SPVM_CONSTANT_new(compiler);
641            
642 0           constant->value.sval = value;
643 0           SPVM_OP* op_constant_type = SPVM_OP_new_op_short_type(compiler, file, line);
644 0           constant->type = op_constant_type->uv.type;
645            
646 0           op_constant->uv.constant = constant;
647              
648 0           SPVM_LIST_push(compiler->op_constants, op_constant);
649              
650 0           constant->op_constant = op_constant;
651            
652 0           return op_constant;
653             }
654              
655 95089           SPVM_OP* SPVM_OP_new_op_constant_int(SPVM_COMPILER* compiler, int32_t value, const char* file, int32_t line) {
656 95089           SPVM_OP* op_constant = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_CONSTANT, file, line);
657 95089           SPVM_CONSTANT* constant = SPVM_CONSTANT_new(compiler);
658            
659 95089           constant->value.ival = value;
660 95089           SPVM_OP* op_constant_type = SPVM_OP_new_op_int_type(compiler, file, line);
661 95089           constant->type = op_constant_type->uv.type;
662            
663 95089           op_constant->uv.constant = constant;
664              
665 95089           SPVM_LIST_push(compiler->op_constants, op_constant);
666              
667 95089           constant->op_constant = op_constant;
668            
669 95089           return op_constant;
670             }
671              
672 492           SPVM_OP* SPVM_OP_new_op_constant_long(SPVM_COMPILER* compiler, int64_t value, const char* file, int32_t line) {
673 492           SPVM_OP* op_constant = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_CONSTANT, file, line);
674 492           SPVM_CONSTANT* constant = SPVM_CONSTANT_new(compiler);
675            
676 492           constant->value.lval = value;
677 492           SPVM_OP* op_constant_type = SPVM_OP_new_op_long_type(compiler, file, line);
678 492           constant->type = op_constant_type->uv.type;
679            
680 492           op_constant->uv.constant = constant;
681              
682 492           SPVM_LIST_push(compiler->op_constants, op_constant);
683              
684 492           constant->op_constant = op_constant;
685            
686 492           return op_constant;
687             }
688              
689 749           SPVM_OP* SPVM_OP_new_op_constant_float(SPVM_COMPILER* compiler, float value, const char* file, int32_t line) {
690 749           SPVM_OP* op_constant = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_CONSTANT, file, line);
691 749           SPVM_CONSTANT* constant = SPVM_CONSTANT_new(compiler);
692            
693 749           constant->value.fval = value;
694 749           SPVM_OP* op_constant_type = SPVM_OP_new_op_float_type(compiler, file, line);
695 749           constant->type = op_constant_type->uv.type;
696            
697 749           op_constant->uv.constant = constant;
698              
699 749           SPVM_LIST_push(compiler->op_constants, op_constant);
700              
701 749           constant->op_constant = op_constant;
702            
703 749           return op_constant;
704             }
705              
706 1007           SPVM_OP* SPVM_OP_new_op_constant_double(SPVM_COMPILER* compiler, double value, const char* file, int32_t line) {
707 1007           SPVM_OP* op_constant = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_CONSTANT, file, line);
708 1007           SPVM_CONSTANT* constant = SPVM_CONSTANT_new(compiler);
709            
710 1007           constant->value.dval = value;
711 1007           SPVM_OP* op_constant_type = SPVM_OP_new_op_double_type(compiler, file, line);
712 1007           constant->type = op_constant_type->uv.type;
713            
714 1007           op_constant->uv.constant = constant;
715            
716 1007           SPVM_LIST_push(compiler->op_constants, op_constant);
717            
718 1007           constant->op_constant = op_constant;
719            
720 1007           return op_constant;
721             }
722              
723 16463           SPVM_OP* SPVM_OP_new_op_constant_string(SPVM_COMPILER* compiler, const char* string, int32_t length, const char* file, int32_t line) {
724              
725 16463           SPVM_OP* op_constant = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_CONSTANT, file, line);
726 16463           SPVM_CONSTANT* constant = SPVM_CONSTANT_new(compiler);
727 16463           constant->value.oval = (void*)string;
728 16463           SPVM_OP* op_constant_type = SPVM_OP_new_op_string_type(compiler, file, line);
729 16463           constant->type = op_constant_type->uv.type;
730 16463           constant->string_length = length;
731 16463           op_constant->uv.constant = constant;
732            
733 16463           SPVM_LIST_push(compiler->op_constants, op_constant);
734            
735 16463           constant->op_constant = op_constant;
736            
737 16463           return op_constant;
738             }
739              
740 6290           SPVM_OP* SPVM_OP_new_op_void_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
741 6290           SPVM_TYPE* type = SPVM_TYPE_create_void_type(compiler);
742 6290           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type, file, line);
743            
744 6290           return op_type;
745             }
746              
747 16307           SPVM_OP* SPVM_OP_new_op_byte_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
748 16307           SPVM_TYPE* type = SPVM_TYPE_create_byte_type(compiler);
749 16307           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type, file, line);
750            
751 16307           return op_type;
752             }
753              
754 5916           SPVM_OP* SPVM_OP_new_op_short_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
755 5916           SPVM_TYPE* type = SPVM_TYPE_create_short_type(compiler);
756 5916           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type, file, line);
757            
758 5916           return op_type;
759             }
760              
761 1084167           SPVM_OP* SPVM_OP_new_op_int_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
762 1084167           SPVM_TYPE* type = SPVM_TYPE_create_int_type(compiler);
763 1084167           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type, file, line);
764            
765 1084167           return op_type;
766             }
767              
768 9186           SPVM_OP* SPVM_OP_new_op_long_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
769 9186           SPVM_TYPE* type = SPVM_TYPE_create_long_type(compiler);
770 9186           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type, file, line);
771            
772 9186           return op_type;
773             }
774              
775 10239           SPVM_OP* SPVM_OP_new_op_float_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
776 10239           SPVM_TYPE* type = SPVM_TYPE_create_float_type(compiler);
777 10239           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type, file, line);
778            
779 10239           return op_type;
780             }
781              
782 11802           SPVM_OP* SPVM_OP_new_op_double_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
783 11802           SPVM_TYPE* type = SPVM_TYPE_create_double_type(compiler);
784 11802           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type, file, line);
785            
786 11802           return op_type;
787             }
788              
789 106396           SPVM_OP* SPVM_OP_new_op_string_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
790 106396           SPVM_TYPE* type = SPVM_TYPE_create_string_type(compiler);
791 106396           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type, file, line);
792            
793 106396           return op_type;
794             }
795              
796 5524           SPVM_OP* SPVM_OP_new_op_undef_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
797 5524           SPVM_TYPE* type = SPVM_TYPE_create_undef_type(compiler);
798 5524           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type, file, line);
799            
800 5524           return op_type;
801             }
802              
803 84           SPVM_OP* SPVM_OP_new_op_byte_ref_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
804 84           SPVM_TYPE* type = SPVM_TYPE_create_byte_ref_type(compiler);
805 84           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type, file, line);
806            
807 84           return op_type;
808             }
809              
810 84           SPVM_OP* SPVM_OP_new_op_short_ref_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
811 84           SPVM_TYPE* type = SPVM_TYPE_create_short_ref_type(compiler);
812 84           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type, file, line);
813            
814 84           return op_type;
815             }
816              
817 623           SPVM_OP* SPVM_OP_new_op_int_ref_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
818 623           SPVM_TYPE* type = SPVM_TYPE_create_int_ref_type(compiler);
819 623           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type, file, line);
820            
821 623           return op_type;
822             }
823              
824 88           SPVM_OP* SPVM_OP_new_op_long_ref_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
825 88           SPVM_TYPE* type = SPVM_TYPE_create_long_ref_type(compiler);
826 88           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type, file, line);
827            
828 88           return op_type;
829             }
830              
831 84           SPVM_OP* SPVM_OP_new_op_float_ref_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
832 84           SPVM_TYPE* type = SPVM_TYPE_create_float_ref_type(compiler);
833 84           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type, file, line);
834            
835 84           return op_type;
836             }
837              
838 86           SPVM_OP* SPVM_OP_new_op_double_ref_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
839 86           SPVM_TYPE* type = SPVM_TYPE_create_double_ref_type(compiler);
840 86           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type, file, line);
841            
842 86           return op_type;
843             }
844              
845 1396           SPVM_OP* SPVM_OP_new_op_any_object_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
846 1396           SPVM_TYPE* type = SPVM_TYPE_create_any_object_type(compiler);
847 1396           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type, file, line);
848            
849 1396           return op_type;
850             }
851              
852 202           SPVM_OP* SPVM_OP_build_eval(SPVM_COMPILER* compiler, SPVM_OP* op_eval, SPVM_OP* op_eval_block) {
853            
854 202           SPVM_OP_insert_child(compiler, op_eval, op_eval->last, op_eval_block);
855            
856             // eval block
857 202           op_eval_block->uv.block->id = SPVM_BLOCK_C_ID_EVAL;
858            
859 202           return op_eval;
860             }
861              
862 286           SPVM_OP* SPVM_OP_build_switch_statement(SPVM_COMPILER* compiler, SPVM_OP* op_switch, SPVM_OP* op_term_condition, SPVM_OP* op_switch_block) {
863            
864 286           SPVM_OP* op_switch_condition = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_SWITCH_CONDITION, op_term_condition->file, op_term_condition->line);
865 286           SPVM_OP_insert_child(compiler, op_switch_condition, op_switch_condition->last, op_term_condition);
866            
867             // Free tmp vars at end of condition
868 286           SPVM_OP* op_switch_condition_free_tmp = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_FREE_TMP, op_switch_condition->file, op_switch_condition->line);
869 286           SPVM_OP_insert_child(compiler, op_switch_condition_free_tmp, op_switch_condition_free_tmp->last, op_switch_condition);
870            
871 286           SPVM_OP_insert_child(compiler, op_switch, op_switch->last, op_switch_condition_free_tmp);
872 286           SPVM_OP_insert_child(compiler, op_switch, op_switch->last, op_switch_block);
873            
874 286           SPVM_SWITCH_INFO* switch_info = SPVM_SWITCH_INFO_new(compiler);
875 286           op_switch->uv.switch_info = switch_info;
876            
877 286           op_switch_condition->uv.switch_info = switch_info;
878            
879 286           return op_switch;
880             }
881              
882 286           SPVM_OP* SPVM_OP_build_switch_block(SPVM_COMPILER* compiler, SPVM_OP* op_switch_block, SPVM_OP* op_case_statements, SPVM_OP* op_default_statement) {
883            
884 286 100         if (op_case_statements->id != SPVM_OP_C_ID_LIST) {
885 9           SPVM_OP* op_list = SPVM_OP_new_op_list(compiler, op_case_statements->file, op_case_statements->line);
886 9           SPVM_OP_insert_child(compiler, op_list, op_list->last, op_case_statements);
887 9           op_case_statements = op_list;
888             }
889            
890 286           SPVM_OP_insert_child(compiler, op_switch_block, op_switch_block->last, op_case_statements);
891 286 100         if (op_default_statement) {
892 35           SPVM_OP_insert_child(compiler, op_switch_block, op_switch_block->last, op_default_statement);
893             }
894            
895 286           return op_switch_block;
896             }
897              
898 1347           SPVM_OP* SPVM_OP_build_case_statement(SPVM_COMPILER* compiler, SPVM_OP* op_case_info, SPVM_OP* op_term, SPVM_OP* op_block) {
899            
900 1347           SPVM_CASE_INFO* case_info = SPVM_CASE_INFO_new(compiler);
901            
902 1347           SPVM_OP_insert_child(compiler, op_case_info, op_case_info->last, op_term);
903 1347           op_term->flag = SPVM_OP_C_FLAG_CONSTANT_CASE;
904            
905 1347 100         if (op_block) {
906             // case block last statement must be break;
907 386           SPVM_OP* op_statements = op_block->first;
908 386 50         if (op_statements) {
909 386           SPVM_OP* op_last_statement = op_statements->last;
910 386 50         if (!(op_last_statement && (op_last_statement->id == SPVM_OP_C_ID_BREAK || op_last_statement->id == SPVM_OP_C_ID_RETURN))) {
    100          
    100          
911 2           SPVM_COMPILER_error(compiler, "Last statement of case block must be break or return statement at %s line %d\n", op_block->file, op_block->line);
912             }
913             }
914            
915 386           SPVM_OP_insert_child(compiler, op_case_info, op_case_info->last, op_block);
916             }
917            
918 1347           case_info->op_case_info = op_case_info;
919            
920 1347           op_case_info->uv.case_info = case_info;
921            
922 1347           return op_case_info;
923             }
924              
925 35           SPVM_OP* SPVM_OP_build_default_statement(SPVM_COMPILER* compiler, SPVM_OP* op_default, SPVM_OP* op_block) {
926            
927 35 100         if (op_block) {
928 17           SPVM_OP_insert_child(compiler, op_default, op_default->last, op_block);
929             }
930            
931 35           return op_default;
932             }
933              
934 55074           SPVM_OP* SPVM_OP_build_condition(SPVM_COMPILER* compiler, SPVM_OP* op_term_condition, int32_t is_not) {
935             // Condition
936             int32_t id;
937 55074 100         if (is_not) {
938 16561           id = SPVM_OP_C_ID_CONDITION_NOT;
939             }
940             else {
941 38513           id = SPVM_OP_C_ID_CONDITION;
942             }
943 55074           SPVM_OP* op_condition = SPVM_OP_new_op(compiler, id, op_term_condition->file, op_term_condition->line);
944            
945 55074 50         if (SPVM_OP_is_rel_op(compiler, op_term_condition)) {
946 0           SPVM_OP_insert_child(compiler, op_condition, op_condition->last, op_term_condition);
947             }
948             else {
949 55074           SPVM_OP* op_assign_bool = SPVM_OP_new_op_assign_bool(compiler, op_term_condition, op_term_condition->file, op_term_condition->line);
950 55074           SPVM_OP_insert_child(compiler, op_condition, op_condition->last, op_assign_bool);
951             }
952            
953 55074           return op_condition;
954             }
955              
956 10447           SPVM_OP* SPVM_OP_build_for_statement(SPVM_COMPILER* compiler, SPVM_OP* op_for, SPVM_OP* op_term_init, SPVM_OP* op_term_condition, SPVM_OP* op_term_increment, SPVM_OP* op_block_statements) {
957            
958             // Loop
959 10447           SPVM_OP* op_loop = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_LOOP, op_for->file, op_for->line);
960            
961             // Condition
962 10447           SPVM_OP* op_condition = SPVM_OP_build_condition(compiler, op_term_condition, 1);
963 10447           op_condition->flag |= SPVM_OP_C_FLAG_CONDITION_LOOP;
964              
965             // Free tmp vars at end of condition
966 10447           SPVM_OP* op_condition_free_tmp = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_FREE_TMP, op_condition->file, op_condition->line);
967 10447           SPVM_OP_insert_child(compiler, op_condition_free_tmp, op_condition_free_tmp->last, op_condition);
968            
969             // Set block flag
970 10447           op_block_statements->uv.block->id = SPVM_BLOCK_C_ID_LOOP_STATEMENTS;
971              
972             // Outer block for initialize loop variable
973 10447           SPVM_OP* op_block_init = SPVM_OP_new_op_block(compiler, op_for->file, op_for->line);
974 10447           op_block_init->uv.block->id = SPVM_BLOCK_C_ID_LOOP_INIT;
975            
976             // Block for increment
977 10447           SPVM_OP* op_loop_increment = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_LOOP_INCREMENT, op_for->file, op_for->line);
978              
979             // Free tmp vars at end of initialization statement
980 10447           SPVM_OP* op_term_increment_free_tmp = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_FREE_TMP, op_term_increment->file, op_term_increment->line);
981 10447           SPVM_OP_insert_child(compiler, op_term_increment_free_tmp, op_term_increment_free_tmp->last, op_term_increment);
982              
983 10447           SPVM_OP_insert_child(compiler, op_loop_increment, op_loop_increment->last, op_term_increment_free_tmp);
984              
985             // Free tmp vars at end of initialization statement
986 10447           SPVM_OP* op_term_init_free_tmp = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_FREE_TMP, op_term_init->file, op_term_init->line);
987 10447           SPVM_OP_insert_child(compiler, op_term_init_free_tmp, op_term_init_free_tmp->last, op_term_init);
988              
989 10447           SPVM_OP_insert_child(compiler, op_block_init, op_block_init->last, op_term_init_free_tmp);
990 10447           SPVM_OP_insert_child(compiler, op_block_init, op_block_init->last, op_condition_free_tmp);
991 10447           SPVM_OP_insert_child(compiler, op_block_init, op_block_init->last, op_block_statements);
992 10447           SPVM_OP_insert_child(compiler, op_block_init, op_block_init->last, op_loop_increment);
993            
994 10447           SPVM_OP_insert_child(compiler, op_loop, op_loop->last, op_block_init);
995            
996 10447           return op_loop;
997             }
998              
999 790           SPVM_OP* SPVM_OP_build_while_statement(SPVM_COMPILER* compiler, SPVM_OP* op_while, SPVM_OP* op_term_condition, SPVM_OP* op_block_statements) {
1000            
1001             // Loop
1002 790           SPVM_OP* op_loop = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_LOOP, op_while->file, op_while->line);
1003            
1004             // Init statement. This is null.
1005 790           SPVM_OP* op_term_init = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_NULL, op_while->file, op_while->line);
1006            
1007             // Condition
1008 790           SPVM_OP* op_condition = SPVM_OP_build_condition(compiler, op_term_condition, 1);
1009 790           op_condition->flag |= SPVM_OP_C_FLAG_CONDITION_LOOP;
1010              
1011             // Free tmp vars at end of condition
1012 790           SPVM_OP* op_condition_free_tmp = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_FREE_TMP, op_condition->file, op_condition->line);
1013 790           SPVM_OP_insert_child(compiler, op_condition_free_tmp, op_condition_free_tmp->last, op_condition);
1014            
1015             // Set block flag
1016 790           op_block_statements->uv.block->id = SPVM_BLOCK_C_ID_LOOP_STATEMENTS;
1017            
1018             // Next value. This is null.
1019 790           SPVM_OP* op_term_increment = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_NULL, op_while->file, op_while->line);
1020              
1021 790           SPVM_OP* op_block_init = SPVM_OP_new_op_block(compiler, op_while->file, op_while->line);
1022 790           op_block_init->uv.block->id = SPVM_BLOCK_C_ID_LOOP_INIT;
1023              
1024             // Block for increment
1025 790           SPVM_OP* op_loop_increment = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_LOOP_INCREMENT, op_while->file, op_while->line);
1026 790           SPVM_OP_insert_child(compiler, op_loop_increment, op_loop_increment->last, op_term_increment);
1027            
1028 790           SPVM_OP_insert_child(compiler, op_block_init, op_block_init->last, op_term_init);
1029 790           SPVM_OP_insert_child(compiler, op_block_init, op_block_init->last, op_condition_free_tmp);
1030 790           SPVM_OP_insert_child(compiler, op_block_init, op_block_init->last, op_block_statements);
1031 790           SPVM_OP_insert_child(compiler, op_block_init, op_block_init->last, op_loop_increment);
1032            
1033 790           SPVM_OP_insert_child(compiler, op_loop, op_loop->last, op_block_init);
1034            
1035 790           return op_loop;
1036             }
1037              
1038 0           SPVM_OP* SPVM_OP_build_if_require_statement(SPVM_COMPILER* compiler, SPVM_OP* op_if_require, SPVM_OP* op_use, SPVM_OP* op_block) {
1039            
1040 0           SPVM_OP_insert_child(compiler, op_if_require, op_if_require->last, op_use);
1041 0           SPVM_OP_insert_child(compiler, op_if_require, op_if_require->last, op_block);
1042            
1043 0           return op_if_require;
1044             }
1045              
1046 43837           SPVM_OP* SPVM_OP_build_if_statement(SPVM_COMPILER* compiler, SPVM_OP* op_if, SPVM_OP* op_term_condition, SPVM_OP* op_block_true, SPVM_OP* op_block_false) {
1047            
1048             // ELSIF is same as IF
1049 43837           int32_t not_condition = 0;
1050            
1051 43837 100         if (op_if->id == SPVM_OP_C_ID_UNLESS) {
1052 5324           op_if->id = SPVM_OP_C_ID_IF;
1053 5324           not_condition = 1;
1054             }
1055 38513 100         else if (op_if->id == SPVM_OP_C_ID_ELSIF) {
1056 781           op_if->id = SPVM_OP_C_ID_IF;
1057             }
1058            
1059             // Condition
1060 43837           SPVM_OP* op_condition = SPVM_OP_build_condition(compiler, op_term_condition, not_condition);
1061 43837           op_condition->flag |= SPVM_OP_C_FLAG_CONDITION_IF;
1062              
1063             // Free tmp vars at end of condition
1064 43837           SPVM_OP* op_condition_free_tmp = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_FREE_TMP, op_condition->file, op_condition->line);
1065 43837           SPVM_OP_insert_child(compiler, op_condition_free_tmp, op_condition_free_tmp->last, op_condition);
1066              
1067             // Create true block if needed
1068 43837 100         if (op_block_true->id != SPVM_OP_C_ID_BLOCK) {
1069 11523           SPVM_OP* op_not_block = op_block_true;
1070            
1071 11523           op_block_true = SPVM_OP_new_op_block(compiler, op_not_block->file, op_not_block->line);
1072 11523           SPVM_OP* op_list = SPVM_OP_new_op_list(compiler, op_not_block->file, op_not_block->line);
1073 11523           SPVM_OP_insert_child(compiler, op_list, op_list->last, op_not_block);
1074 11523           SPVM_OP_insert_child(compiler, op_block_true, op_block_true->last, op_list);
1075             }
1076 43837           op_block_true->uv.block->id = SPVM_BLOCK_C_ID_IF;
1077            
1078             // Create false block if needed
1079 43837 100         if (op_block_false->id != SPVM_OP_C_ID_BLOCK) {
1080 38862           SPVM_OP* op_not_block = op_block_false;
1081            
1082             // Create block
1083 38862           op_block_false = SPVM_OP_new_op_block(compiler, op_not_block->file, op_not_block->line);
1084 38862           SPVM_OP* op_list = SPVM_OP_new_op_list(compiler, op_not_block->file, op_not_block->line);
1085 38862           SPVM_OP_insert_child(compiler, op_list, op_list->last, op_not_block);
1086 38862           SPVM_OP_insert_child(compiler, op_block_false, op_block_false->last, op_list);
1087             }
1088 43837           op_block_false->uv.block->id = SPVM_BLOCK_C_ID_ELSE;
1089            
1090 43837           SPVM_OP_insert_child(compiler, op_if, op_if->last, op_condition_free_tmp);
1091 43837           SPVM_OP_insert_child(compiler, op_if, op_if->last, op_block_true);
1092 43837           SPVM_OP_insert_child(compiler, op_if, op_if->last, op_block_false);
1093            
1094 43837           return op_if;
1095             }
1096              
1097 15209           SPVM_OP* SPVM_OP_build_array_length(SPVM_COMPILER* compiler, SPVM_OP* op_array_length, SPVM_OP* op_term) {
1098            
1099 15209           SPVM_OP_insert_child(compiler, op_array_length, op_array_length->last, op_term);
1100            
1101 15209           return op_array_length;
1102             }
1103              
1104 4530           SPVM_OP* SPVM_OP_build_string_length(SPVM_COMPILER* compiler, SPVM_OP* op_string_length, SPVM_OP* op_term) {
1105            
1106 4530           SPVM_OP_insert_child(compiler, op_string_length, op_string_length->last, op_term);
1107            
1108 4530           return op_string_length;
1109             }
1110              
1111 7883           SPVM_OP* SPVM_OP_build_new(SPVM_COMPILER* compiler, SPVM_OP* op_new, SPVM_OP* op_type, SPVM_OP* op_list_elements) {
1112            
1113 7883           SPVM_OP_insert_child(compiler, op_new, op_new->last, op_type);
1114            
1115 7883 50         if (op_list_elements) {
1116 0           SPVM_OP* op_array_init = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ARRAY_INIT, op_list_elements->file, op_list_elements->line);
1117 0           SPVM_OP_insert_child(compiler, op_array_init, op_array_init->last, op_list_elements);
1118 0           SPVM_OP_insert_child(compiler, op_array_init, op_array_init->last, op_type);
1119            
1120 0           return op_array_init;
1121             }
1122            
1123 7883           return op_new;
1124             }
1125              
1126 378           SPVM_OP* SPVM_OP_build_array_init(SPVM_COMPILER* compiler, SPVM_OP* op_array_init, SPVM_OP* op_list_elements) {
1127            
1128 378           SPVM_OP_insert_child(compiler, op_array_init, op_array_init->last, op_list_elements);
1129            
1130 378           return op_array_init;
1131             }
1132              
1133 753685           SPVM_OP* SPVM_OP_get_target_op_var(SPVM_COMPILER* compiler, SPVM_OP* op) {
1134             (void)compiler;
1135            
1136             SPVM_OP* op_var;
1137 753685 100         if (op->id == SPVM_OP_C_ID_VAR) {
1138 586727           op_var = op;
1139             }
1140 166958 100         else if (op->id == SPVM_OP_C_ID_ASSIGN) {
1141 165668 100         if (op->first->id == SPVM_OP_C_ID_VAR || op->first->id == SPVM_OP_C_ID_ASSIGN) {
    100          
1142 3084           op_var = SPVM_OP_get_target_op_var(compiler, op->first);
1143             }
1144 162584 50         else if (op->last->id == SPVM_OP_C_ID_VAR || op->last->id == SPVM_OP_C_ID_ASSIGN) {
    0          
1145 162584           op_var = SPVM_OP_get_target_op_var(compiler, op->last);
1146             }
1147             else {
1148 165668           assert(0);
1149             }
1150             }
1151 1290 100         else if (op->id == SPVM_OP_C_ID_SEQUENCE) {
1152 184           op_var = SPVM_OP_get_target_op_var(compiler, op->last);
1153             }
1154 1106 100         else if (op->id == SPVM_OP_C_ID_REF) {
1155 375           op_var = SPVM_OP_get_target_op_var(compiler, op->first);
1156             }
1157 731 100         else if (op->id == SPVM_OP_C_ID_DEREF) {
1158 702           op_var = SPVM_OP_get_target_op_var(compiler, op->first);
1159             }
1160 29 50         else if (op->id == SPVM_OP_C_ID_REFCNT) {
1161 29           op_var = SPVM_OP_get_target_op_var(compiler, op->first);
1162             }
1163             else {
1164 0           assert(0);
1165             }
1166            
1167 753685           return op_var;
1168             }
1169              
1170 586727           int32_t SPVM_OP_get_mem_id(SPVM_COMPILER* compiler, SPVM_OP* op) {
1171             (void)compiler;
1172            
1173 586727 50         switch (op->id) {
1174             case SPVM_OP_C_ID_BOOL:
1175             case SPVM_OP_C_ID_NUMERIC_EQ:
1176             case SPVM_OP_C_ID_NUMERIC_NE:
1177             case SPVM_OP_C_ID_NUMERIC_GT:
1178             case SPVM_OP_C_ID_NUMERIC_GE:
1179             case SPVM_OP_C_ID_NUMERIC_LT:
1180             case SPVM_OP_C_ID_NUMERIC_LE:
1181             case SPVM_OP_C_ID_STRING_EQ:
1182             case SPVM_OP_C_ID_STRING_NE:
1183             case SPVM_OP_C_ID_STRING_GT:
1184             case SPVM_OP_C_ID_STRING_GE:
1185             case SPVM_OP_C_ID_STRING_LT:
1186             case SPVM_OP_C_ID_STRING_LE:
1187             case SPVM_OP_C_ID_ISA:
1188             case SPVM_OP_C_ID_ISWEAK_FIELD:
1189 0           return 0;
1190             default: {
1191 586727           SPVM_OP* op_var = SPVM_OP_get_target_op_var(compiler, op);
1192            
1193 586727           return op_var->uv.var->my->mem_id;
1194             }
1195             }
1196            
1197             return -1;
1198             }
1199              
1200 11017973           SPVM_TYPE* SPVM_OP_get_type(SPVM_COMPILER* compiler, SPVM_OP* op) {
1201            
1202 11017973           SPVM_TYPE* type = NULL;
1203            
1204 11017973           switch (op->id) {
1205             case SPVM_OP_C_ID_RETURN:
1206             case SPVM_OP_C_ID_LOOP_INCREMENT:
1207             case SPVM_OP_C_ID_CONDITION:
1208             case SPVM_OP_C_ID_CONDITION_NOT:
1209             case SPVM_OP_C_ID_FREE_TMP:
1210             case SPVM_OP_C_ID_SWITCH:
1211             case SPVM_OP_C_ID_DEFAULT:
1212             case SPVM_OP_C_ID_CASE:
1213             case SPVM_OP_C_ID_LAST:
1214             case SPVM_OP_C_ID_NEXT:
1215             case SPVM_OP_C_ID_BREAK:
1216             case SPVM_OP_C_ID_DIE:
1217             {
1218             // Dummy int variable
1219 261430           SPVM_OP* op_type = SPVM_OP_new_op_int_type(compiler, op->file, op->line);
1220 261430           type = op_type->uv.type;
1221 261430           break;
1222             }
1223             case SPVM_OP_C_ID_PACKAGE: {
1224 0           SPVM_PACKAGE* package = op->uv.package;
1225 0           type = package->op_type->uv.type;
1226 0           break;
1227             }
1228             case SPVM_OP_C_ID_NUMERIC_EQ:
1229             case SPVM_OP_C_ID_NUMERIC_NE:
1230             case SPVM_OP_C_ID_NUMERIC_GT:
1231             case SPVM_OP_C_ID_NUMERIC_GE:
1232             case SPVM_OP_C_ID_NUMERIC_LT:
1233             case SPVM_OP_C_ID_NUMERIC_LE:
1234             case SPVM_OP_C_ID_BOOL:
1235             case SPVM_OP_C_ID_STRING_EQ:
1236             case SPVM_OP_C_ID_STRING_NE:
1237             case SPVM_OP_C_ID_STRING_GT:
1238             case SPVM_OP_C_ID_STRING_GE:
1239             case SPVM_OP_C_ID_STRING_LT:
1240             case SPVM_OP_C_ID_STRING_LE:
1241             case SPVM_OP_C_ID_ISA:
1242             case SPVM_OP_C_ID_IF:
1243             case SPVM_OP_C_ID_ISWEAK_FIELD:
1244             {
1245 384143           SPVM_OP* op_type = SPVM_OP_new_op_int_type(compiler, op->file, op->line);
1246 384143           type = op_type->uv.type;
1247 384143           break;
1248             }
1249             case SPVM_OP_C_ID_CONCAT: {
1250 19866           SPVM_OP* op_type = SPVM_OP_new_op_string_type(compiler, op->file, op->line);
1251 19866           type = op_type->uv.type;
1252 19866           break;
1253             }
1254             case SPVM_OP_C_ID_ARRAY_LENGTH:
1255             case SPVM_OP_C_ID_STRING_LENGTH:
1256             case SPVM_OP_C_ID_REFCNT:
1257             {
1258 64946           SPVM_OP* op_type = SPVM_OP_new_op_int_type(compiler, op->file, op->line);
1259 64946           type = op_type->uv.type;
1260 64946           break;
1261             }
1262             case SPVM_OP_C_ID_ARRAY_ACCESS: {
1263 101344           SPVM_TYPE* first_type = SPVM_OP_get_type(compiler, op->first);
1264 101344           type = SPVM_TYPE_new(compiler);
1265 101344           SPVM_BASIC_TYPE* basic_type = SPVM_HASH_fetch(compiler->basic_type_symtable, first_type->basic_type->name, strlen(first_type->basic_type->name));
1266 101344 100         if (basic_type->id == SPVM_BASIC_TYPE_C_ID_STRING && first_type->dimension == 0) {
    100          
1267 19681           type->basic_type = SPVM_HASH_fetch(compiler->basic_type_symtable, "byte", strlen("byte"));
1268 19681           type->dimension = 0;
1269             }
1270 81663 100         else if (basic_type->id == SPVM_BASIC_TYPE_C_ID_OARRAY && first_type->dimension == 0) {
    50          
1271 2946           type->basic_type = SPVM_HASH_fetch(compiler->basic_type_symtable, "object", strlen("object"));
1272 2946           type->dimension = 0;
1273             }
1274             else {
1275 78717           type->basic_type = basic_type;
1276 78717 50         assert(first_type->dimension > 0);
1277 78717           type->dimension = first_type->dimension - 1;
1278             }
1279            
1280 101344           break;
1281             }
1282             case SPVM_OP_C_ID_ADD:
1283             case SPVM_OP_C_ID_SUBTRACT:
1284             case SPVM_OP_C_ID_MULTIPLY:
1285             case SPVM_OP_C_ID_DIVIDE:
1286             case SPVM_OP_C_ID_REMAINDER:
1287             case SPVM_OP_C_ID_INC:
1288             case SPVM_OP_C_ID_PRE_INC:
1289             case SPVM_OP_C_ID_POST_INC:
1290             case SPVM_OP_C_ID_DEC:
1291             case SPVM_OP_C_ID_PRE_DEC:
1292             case SPVM_OP_C_ID_POST_DEC:
1293             case SPVM_OP_C_ID_LEFT_SHIFT:
1294             case SPVM_OP_C_ID_RIGHT_ARITHMETIC_SHIFT:
1295             case SPVM_OP_C_ID_RIGHT_LOGICAL_SHIFT:
1296             case SPVM_OP_C_ID_BIT_XOR:
1297             case SPVM_OP_C_ID_BIT_OR:
1298             case SPVM_OP_C_ID_BIT_AND:
1299             case SPVM_OP_C_ID_BIT_NOT:
1300             case SPVM_OP_C_ID_PLUS:
1301             case SPVM_OP_C_ID_MINUS:
1302             case SPVM_OP_C_ID_NEW:
1303             case SPVM_OP_C_ID_CHECK_CONVERT:
1304             case SPVM_OP_C_ID_ARRAY_INIT:
1305             {
1306 123186           type = SPVM_OP_get_type(compiler, op->first);
1307 123186           break;
1308             }
1309             case SPVM_OP_C_ID_LIST:
1310             case SPVM_OP_C_ID_SEQUENCE:
1311 154524           type = SPVM_OP_get_type(compiler, op->last);
1312 154524           break;
1313             case SPVM_OP_C_ID_ASSIGN: {
1314 958826           type = SPVM_OP_get_type(compiler, op->last);
1315 958826           break;
1316             }
1317             case SPVM_OP_C_ID_CONVERT: {
1318 50176           SPVM_OP* op_type = op->last;
1319 50176           type = SPVM_OP_get_type(compiler, op_type);
1320 50176           break;
1321             }
1322             case SPVM_OP_C_ID_TYPE: {
1323 160795 50         if (op->uv.type) {
1324 160795           type = op->uv.type;
1325             }
1326 160795           break;
1327             }
1328             case SPVM_OP_C_ID_SWITCH_CONDITION : {
1329 524           type = SPVM_OP_get_type(compiler, op->first);
1330 524           break;
1331             }
1332             case SPVM_OP_C_ID_UNDEF : {
1333 5524           SPVM_OP* op_type = SPVM_OP_new_op_undef_type(compiler, op->file, op->line);
1334 5524           type = op_type->uv.type;
1335 5524           break;
1336             }
1337             case SPVM_OP_C_ID_CONSTANT: {
1338 489894           SPVM_CONSTANT* constant = op->uv.constant;
1339 489894           type = constant->type;
1340 489894           break;
1341             }
1342             case SPVM_OP_C_ID_VAR: {
1343 3924864           SPVM_VAR* var = op->uv.var;
1344 3924864           type = var->my->type;
1345 3924864           break;
1346             }
1347             case SPVM_OP_C_ID_PACKAGE_VAR_ACCESS: {
1348 3680           SPVM_PACKAGE_VAR* package_var = op->uv.package_var_access->package_var;
1349 3680 50         if (package_var->type) {
1350 3680           type = package_var->type;
1351             }
1352 3680           break;
1353             }
1354             case SPVM_OP_C_ID_PACKAGE_VAR: {
1355 213           SPVM_PACKAGE_VAR* package_var = op->uv.package_var;
1356 213 50         if (package_var->type) {
1357 213           type = package_var->type;
1358             }
1359 213           break;
1360             }
1361             case SPVM_OP_C_ID_EXCEPTION_VAR: {
1362 50144           SPVM_OP* op_type = SPVM_OP_new_op_string_type(compiler, op->file, op->line);
1363 50144           type = op_type->uv.type;
1364 50144           break;
1365             }
1366             case SPVM_OP_C_ID_MY: {
1367            
1368 3770688           SPVM_MY* my = op->uv.my;
1369 3770688           type = my->type;
1370 3770688           break;
1371             }
1372             case SPVM_OP_C_ID_CALL_SUB: {
1373 28167           SPVM_CALL_SUB* call_sub = op->uv.call_sub;
1374 28167           const char* call_sub_sub_name = call_sub->sub->name;
1375 28167           SPVM_PACKAGE* call_sub_sub_package = call_sub->sub->package;
1376 28167           SPVM_SUB* sub = SPVM_HASH_fetch(call_sub_sub_package->sub_symtable, call_sub_sub_name, strlen(call_sub_sub_name));
1377 28167           type = sub->return_type;
1378 28167           break;
1379             }
1380             case SPVM_OP_C_ID_FIELD_ACCESS: {
1381 39987 100         if (op->flag & (SPVM_OP_C_FLAG_FIELD_ACCESS_WEAKEN|SPVM_OP_C_FLAG_FIELD_ACCESS_UNWEAKEN|SPVM_OP_C_FLAG_FIELD_ACCESS_ISWEAK)) {
1382 87           SPVM_OP* op_type = SPVM_OP_new_op_int_type(compiler, op->file, op->line);
1383 87           type = op_type->uv.type;
1384             }
1385             else {
1386 39900           SPVM_FIELD_ACCESS* field_access = op->uv.field_access;
1387 39900           SPVM_FIELD* field = field_access->field;
1388 39900           type = field->type;
1389             }
1390 39987           break;
1391             }
1392             case SPVM_OP_C_ID_ARRAY_FIELD_ACCESS: {
1393 15758           SPVM_ARRAY_FIELD_ACCESS* array_field_access = op->uv.array_field_access;
1394 15758           SPVM_FIELD* field = array_field_access->field;
1395 15758           type = field->type;
1396 15758           break;
1397             }
1398             case SPVM_OP_C_ID_FIELD: {
1399 10293           SPVM_FIELD* field = op->uv.field;
1400 10293           type = field->type;
1401 10293           break;
1402             }
1403             case SPVM_OP_C_ID_REF: {
1404 1301           SPVM_TYPE* term_type = SPVM_OP_get_type(compiler, op->first);
1405 1301 50         assert(term_type->dimension == 0);
1406 1301           switch (term_type->basic_type->id) {
1407             case SPVM_BASIC_TYPE_C_ID_BYTE: {
1408 84           SPVM_OP* op_type = SPVM_OP_new_op_byte_ref_type(compiler, op->file, op->line);
1409 84           type = op_type->uv.type;
1410 84           break;
1411             }
1412             case SPVM_BASIC_TYPE_C_ID_SHORT: {
1413 84           SPVM_OP* op_type = SPVM_OP_new_op_short_ref_type(compiler, op->file, op->line);
1414 84           type = op_type->uv.type;
1415 84           break;
1416             }
1417             case SPVM_BASIC_TYPE_C_ID_INT: {
1418 623           SPVM_OP* op_type = SPVM_OP_new_op_int_ref_type(compiler, op->file, op->line);
1419 623           type = op_type->uv.type;
1420 623           break;
1421             }
1422             case SPVM_BASIC_TYPE_C_ID_LONG: {
1423 88           SPVM_OP* op_type = SPVM_OP_new_op_long_ref_type(compiler, op->file, op->line);
1424 88           type = op_type->uv.type;
1425 88           break;
1426             }
1427             case SPVM_BASIC_TYPE_C_ID_FLOAT: {
1428 84           SPVM_OP* op_type = SPVM_OP_new_op_float_ref_type(compiler, op->file, op->line);
1429 84           type = op_type->uv.type;
1430 84           break;
1431             }
1432             case SPVM_BASIC_TYPE_C_ID_DOUBLE: {
1433 86           SPVM_OP* op_type = SPVM_OP_new_op_double_ref_type(compiler, op->file, op->line);
1434 86           type = op_type->uv.type;
1435 86           break;
1436             }
1437             default:
1438 252 50         assert(SPVM_TYPE_is_multi_numeric_type(compiler, term_type->basic_type->id, term_type->dimension, term_type->flag));
1439 252           type = SPVM_TYPE_new(compiler);
1440 252           type->basic_type = term_type->basic_type;
1441 252           type->dimension = term_type->dimension;
1442 252           type->flag = term_type->flag | SPVM_TYPE_C_FLAG_REF;
1443             }
1444 1301           break;
1445             }
1446             case SPVM_OP_C_ID_DEREF: {
1447 3908           SPVM_TYPE* term_type = SPVM_OP_get_type(compiler, op->first);
1448 3908 50         assert(term_type->dimension == 0);
1449 3908           switch (term_type->basic_type->id) {
1450             case SPVM_BASIC_TYPE_C_ID_BYTE: {
1451 297           SPVM_OP* op_type = SPVM_OP_new_op_byte_type(compiler, op->file, op->line);
1452 297           type = op_type->uv.type;
1453 297           break;
1454             }
1455             case SPVM_BASIC_TYPE_C_ID_SHORT: {
1456 297           SPVM_OP* op_type = SPVM_OP_new_op_short_type(compiler, op->file, op->line);
1457 297           type = op_type->uv.type;
1458 297           break;
1459             }
1460             case SPVM_BASIC_TYPE_C_ID_INT: {
1461 2166           SPVM_OP* op_type = SPVM_OP_new_op_int_type(compiler, op->file, op->line);
1462 2166           type = op_type->uv.type;
1463 2166           break;
1464             }
1465             case SPVM_BASIC_TYPE_C_ID_LONG: {
1466 405           SPVM_OP* op_type = SPVM_OP_new_op_long_type(compiler, op->file, op->line);
1467 405           type = op_type->uv.type;
1468 405           break;
1469             }
1470             case SPVM_BASIC_TYPE_C_ID_FLOAT: {
1471 321           SPVM_OP* op_type = SPVM_OP_new_op_float_type(compiler, op->file, op->line);
1472 321           type = op_type->uv.type;
1473 321           break;
1474             }
1475             case SPVM_BASIC_TYPE_C_ID_DOUBLE: {
1476 332           SPVM_OP* op_type = SPVM_OP_new_op_double_type(compiler, op->file, op->line);
1477 332           type = op_type->uv.type;
1478 332           break;
1479             }
1480             default:
1481 90 50         assert(SPVM_TYPE_is_value_ref_type(compiler, term_type->basic_type->id, term_type->dimension, term_type->flag));
1482 90           type = SPVM_TYPE_new(compiler);
1483 90           type->basic_type = term_type->basic_type;
1484 90           type->dimension = term_type->dimension;
1485 90           type->flag = term_type->flag & ~SPVM_TYPE_C_FLAG_REF;
1486             }
1487 3908           break;
1488             }
1489             }
1490            
1491 11017973           return type;
1492             }
1493              
1494 28247           SPVM_OP* SPVM_OP_build_array_access(SPVM_COMPILER* compiler, SPVM_OP* op_term_array, SPVM_OP* op_term_index) {
1495            
1496 28247           SPVM_OP* op_array_access = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ARRAY_ACCESS, op_term_array->file, op_term_array->line);
1497 28247           SPVM_OP_insert_child(compiler, op_array_access, op_array_access->last, op_term_array);
1498 28247           SPVM_OP_insert_child(compiler, op_array_access, op_array_access->last, op_term_index);
1499            
1500 28247           return op_array_access;
1501             }
1502              
1503 14655           SPVM_OP* SPVM_OP_build_field_access(SPVM_COMPILER* compiler, SPVM_OP* op_term, SPVM_OP* op_name_field) {
1504 14655           SPVM_OP* op_field_access = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_FIELD_ACCESS, op_term->file, op_term->line);
1505 14655           SPVM_OP_insert_child(compiler, op_field_access, op_field_access->last, op_term);
1506            
1507 14655           SPVM_FIELD_ACCESS* field_access = SPVM_FIELD_ACCESS_new(compiler);
1508            
1509 14655           field_access->op_term = op_term;
1510 14655           field_access->op_name = op_name_field;
1511 14655           op_field_access->uv.field_access = field_access;
1512            
1513 14655           return op_field_access;
1514             }
1515              
1516 59           SPVM_OP* SPVM_OP_build_weaken_field(SPVM_COMPILER* compiler, SPVM_OP* op_weaken, SPVM_OP* op_field_access) {
1517            
1518 59           SPVM_OP* op_weaken_field = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_WEAKEN_FIELD, op_weaken->file, op_weaken->line);
1519 59           SPVM_OP_insert_child(compiler, op_weaken_field, op_weaken_field->last, op_field_access);
1520            
1521 59           op_field_access->flag |= SPVM_OP_C_FLAG_FIELD_ACCESS_WEAKEN;
1522            
1523 59           return op_weaken_field;
1524             }
1525              
1526 3           SPVM_OP* SPVM_OP_build_unweaken_field(SPVM_COMPILER* compiler, SPVM_OP* op_unweaken, SPVM_OP* op_field_access) {
1527            
1528 3           SPVM_OP* op_unweaken_field = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_UNWEAKEN_FIELD, op_unweaken->file, op_unweaken->line);
1529 3           SPVM_OP_insert_child(compiler, op_unweaken_field, op_unweaken_field->last, op_field_access);
1530            
1531 3           op_field_access->flag |= SPVM_OP_C_FLAG_FIELD_ACCESS_UNWEAKEN;
1532            
1533 3           return op_unweaken_field;
1534             }
1535              
1536 9           SPVM_OP* SPVM_OP_build_isweak_field(SPVM_COMPILER* compiler, SPVM_OP* op_isweak, SPVM_OP* op_field_access) {
1537            
1538 9           SPVM_OP* op_isweak_field = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ISWEAK_FIELD, op_isweak->file, op_isweak->line);
1539 9           SPVM_OP_insert_child(compiler, op_isweak_field, op_isweak_field->last, op_field_access);
1540 9           op_field_access->flag |= SPVM_OP_C_FLAG_FIELD_ACCESS_ISWEAK;
1541              
1542 9           SPVM_OP* op_name_var = SPVM_OP_new_op_name(compiler, "@condition_flag", op_field_access->file, op_field_access->line);
1543 9           SPVM_OP* op_var = SPVM_OP_new_op_var(compiler, op_name_var);
1544 9           SPVM_OP* op_assign = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_field_access->file, op_field_access->line);
1545 9           SPVM_OP_build_assign(compiler, op_assign, op_var, op_field_access);
1546            
1547 9           return op_assign;
1548             }
1549              
1550 29317           SPVM_OP* SPVM_OP_build_convert(SPVM_COMPILER* compiler, SPVM_OP* op_convert, SPVM_OP* op_type, SPVM_OP* op_term) {
1551            
1552 29317           SPVM_OP_insert_child(compiler, op_convert, op_convert->last, op_term);
1553 29317           SPVM_OP_insert_child(compiler, op_convert, op_convert->last, op_type);
1554            
1555 29317           op_convert->file = op_type->file;
1556 29317           op_convert->line = op_type->line;
1557            
1558 29317           return op_convert;
1559             }
1560              
1561 231           SPVM_OP* SPVM_OP_build_grammar(SPVM_COMPILER* compiler, SPVM_OP* op_packages) {
1562            
1563 231           SPVM_OP* op_grammar = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_GRAMMAR, op_packages->file, op_packages->line);
1564 231           SPVM_OP_insert_child(compiler, op_grammar, op_grammar->last, op_packages);
1565              
1566 231           return op_grammar;
1567             }
1568              
1569 2832           SPVM_OP* SPVM_OP_build_package(SPVM_COMPILER* compiler, SPVM_OP* op_package, SPVM_OP* op_type, SPVM_OP* op_block, SPVM_OP* op_list_descriptors) {
1570              
1571             // Package
1572 2832           SPVM_PACKAGE* package = SPVM_PACKAGE_new(compiler);
1573            
1574 2832           package->module_file = compiler->cur_file;
1575 2832           package->module_rel_file = compiler->cur_rel_file;
1576            
1577             int32_t is_anon;
1578 2832 100         if (op_type) {
1579 2793           is_anon = 0;
1580             }
1581             // Anon
1582             else {
1583             // Package is anon
1584 39           is_anon = 1;
1585            
1586             // Anon package name
1587 39           char* name_package = SPVM_COMPILER_ALLOCATOR_safe_malloc_zero(compiler, strlen("anon2147483647") + 1);
1588 39           sprintf(name_package, "anon%d", compiler->anon_package_length);
1589 39           compiler->anon_package_length++;
1590 39           SPVM_OP* op_name_package = SPVM_OP_new_op_name(compiler, name_package, op_package->file, op_package->line);
1591 39           op_type = SPVM_OP_build_basic_type(compiler, op_name_package);
1592             }
1593            
1594 2832           package->op_type = op_type;
1595            
1596 2832           const char* package_name = op_type->uv.type->basic_type->name;
1597            
1598 2832 100         if (!is_anon && islower(package_name[0])) {
    50          
1599 0           SPVM_COMPILER_error(compiler, "Package name must start with upper case \"%s\" at %s line %d\n", package_name, op_package->file, op_package->line);
1600             }
1601            
1602 2832           SPVM_HASH* package_symtable = compiler->package_symtable;
1603              
1604             // Redeclaration package error
1605 2832           SPVM_PACKAGE* found_package = SPVM_HASH_fetch(package_symtable, package_name, strlen(package_name));
1606 2832 50         if (found_package) {
1607 0           SPVM_COMPILER_error(compiler, "Redeclaration of package \"%s\" at %s line %d\n", package_name, op_package->file, op_package->line);
1608             }
1609             else {
1610             // Add package
1611 2832           SPVM_LIST_push(compiler->packages, package);
1612 2832           SPVM_HASH_insert(compiler->package_symtable, package_name, strlen(package_name), package);
1613             }
1614            
1615 2832           SPVM_OP* op_name_package = SPVM_OP_new_op_name(compiler, op_type->uv.type->basic_type->name, op_type->file, op_type->line);
1616 2832           package->op_name = op_name_package;
1617            
1618 2832           package->name = op_name_package->uv.name;
1619              
1620             // Package is callback
1621 2832           int32_t category_descriptors_count = 0;
1622 2832           int32_t access_control_descriptors_count = 0;
1623 2832 100         if (op_list_descriptors) {
1624 738           SPVM_OP* op_descriptor = op_list_descriptors->first;
1625 1476 100         while ((op_descriptor = SPVM_OP_sibling(compiler, op_descriptor))) {
1626 738           SPVM_DESCRIPTOR* descriptor = op_descriptor->uv.descriptor;
1627 738           switch (descriptor->id) {
1628             case SPVM_DESCRIPTOR_C_ID_CALLBACK_T:
1629 19           package->category = SPVM_PACKAGE_C_CATEGORY_CALLBACK;
1630 19           category_descriptors_count++;
1631 19           break;
1632             case SPVM_DESCRIPTOR_C_ID_POINTER_T:
1633 6           package->category = SPVM_PACKAGE_C_CATEGORY_CLASS;
1634 6           package->flag |= SPVM_PACKAGE_C_FLAG_POINTER;
1635 6           category_descriptors_count++;
1636 6           break;
1637             case SPVM_DESCRIPTOR_C_ID_MULNUM_T:
1638 586           package->category = SPVM_PACKAGE_C_CATEGORY_VALUE;
1639 586           category_descriptors_count++;
1640 586           break;
1641             case SPVM_DESCRIPTOR_C_ID_PRIVATE:
1642             // Default is private
1643 6           access_control_descriptors_count++;
1644 6           break;
1645             case SPVM_DESCRIPTOR_C_ID_PUBLIC:
1646 121           package->flag |= SPVM_PACKAGE_C_FLAG_PUBLIC;
1647 121           access_control_descriptors_count++;
1648 121           break;
1649             default:
1650 0           SPVM_COMPILER_error(compiler, "Invalid package descriptor %s at %s line %d\n", SPVM_DESCRIPTOR_C_ID_NAMES[descriptor->id], op_package->file, op_package->line);
1651             }
1652             }
1653 738 50         if (category_descriptors_count > 1) {
1654 0           SPVM_COMPILER_error(compiler, "callback, mulnum_t, pointer can be specified only one at %s line %d\n", op_list_descriptors->file, op_list_descriptors->line);
1655             }
1656 738 50         if (access_control_descriptors_count > 1) {
1657 0           SPVM_COMPILER_error(compiler, "private, public can be specified only one at %s line %d\n", op_list_descriptors->file, op_list_descriptors->line);
1658             }
1659             }
1660            
1661             // Declarations
1662 2832 50         if (op_block) {
1663 2832           SPVM_OP* op_decls = op_block->first;
1664 2832           SPVM_OP* op_decl = op_decls->first;
1665 39465 100         while ((op_decl = SPVM_OP_sibling(compiler, op_decl))) {
1666             // use declarations
1667 36633 100         if (op_decl->id == SPVM_OP_C_ID_USE) {
1668 600           SPVM_LIST_push(package->op_uses, op_decl);
1669            
1670 600           SPVM_LIST* sub_names = op_decl->uv.use->sub_names;
1671            
1672 600 100         if (sub_names) {
1673 625 100         for (int32_t i = 0; i < sub_names->length; i++) {
1674 25           const char* sub_name = SPVM_LIST_fetch(sub_names, i);
1675            
1676 25           const char* found_sub_name = SPVM_HASH_fetch(package->sub_name_symtable, sub_name, strlen(sub_name));
1677 25 50         if (found_sub_name) {
1678 0           SPVM_COMPILER_error(compiler, "Redeclaration of sub \"%s\" at %s line %d\n", sub_name, op_decl->file, op_decl->line);
1679             }
1680             // Unknown sub
1681             else {
1682 25           SPVM_HASH_insert(package->sub_name_symtable, sub_name, strlen(sub_name), (void*)sub_name);
1683             }
1684             }
1685             }
1686             }
1687             // allow declarations
1688 36033 100         else if (op_decl->id == SPVM_OP_C_ID_ALLOW) {
1689 7           SPVM_LIST_push(package->op_allows, op_decl);
1690             }
1691             // Package var declarations
1692 36026 100         else if (op_decl->id == SPVM_OP_C_ID_PACKAGE_VAR) {
1693 213           SPVM_PACKAGE_VAR* package_var = op_decl->uv.package_var;
1694              
1695 213 50         if (package->category == SPVM_PACKAGE_C_CATEGORY_CALLBACK) {
1696 0           SPVM_COMPILER_error(compiler, "Callback package can't have package variable at %s line %d\n", op_decl->file, op_decl->line);
1697             }
1698 213           SPVM_LIST_push(package->package_vars, op_decl->uv.package_var);
1699              
1700             // Getter
1701 213 100         if (package_var->has_getter) {
1702             // sub FOO : int () {
1703             // return $FOO;
1704             // }
1705              
1706 31           SPVM_OP* op_sub = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_SUB, op_decl->file, op_decl->line);
1707 31           SPVM_OP* op_name_sub = SPVM_OP_new_op_name(compiler, package_var->name + 1, op_decl->file, op_decl->line);
1708 31           SPVM_TYPE* return_type = SPVM_TYPE_new(compiler);
1709 31           return_type->basic_type = package_var->type->basic_type;
1710 31           return_type->dimension = package_var->type->dimension;
1711 31           return_type->flag = package_var->type->flag;
1712 31           SPVM_OP* op_return_type = SPVM_OP_new_op_type(compiler, return_type, op_decl->file, op_decl->line);
1713 31           SPVM_OP* op_args = SPVM_OP_new_op_list(compiler, op_decl->file, op_decl->line);
1714            
1715 31           SPVM_OP* op_block = SPVM_OP_new_op_block(compiler, op_decl->file, op_decl->line);
1716 31           SPVM_OP* op_statements = SPVM_OP_new_op_list(compiler, op_decl->file, op_decl->line);
1717 31           SPVM_OP* op_return = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_RETURN, op_decl->file, op_decl->line);
1718              
1719 31           SPVM_OP* op_name_package_var_access = SPVM_OP_new_op_name(compiler, package_var->name, op_decl->file, op_decl->line);
1720 31           SPVM_OP* op_package_var_access = SPVM_OP_build_package_var_access(compiler, op_name_package_var_access);
1721            
1722 31           SPVM_OP_insert_child(compiler, op_return, op_return->last, op_package_var_access);
1723 31           SPVM_OP_insert_child(compiler, op_statements, op_statements->last, op_return);
1724 31           SPVM_OP_insert_child(compiler, op_block, op_block->last, op_statements);
1725            
1726 31           SPVM_OP_build_sub(compiler, op_sub, op_name_sub, op_return_type, op_args, NULL, op_block, NULL, NULL, 0);
1727              
1728 31           op_sub->uv.sub->is_package_var_getter = 1;
1729 31           op_sub->uv.sub->accessor_original_name = package_var->name;
1730            
1731 31           SPVM_LIST_push(package->subs, op_sub->uv.sub);
1732             }
1733              
1734             // Setter
1735 213 100         if (package_var->has_setter) {
1736            
1737             // sub SET_FOO : void ($foo : int) {
1738             // $FOO = $foo;
1739             // }
1740 29           SPVM_OP* op_sub = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_SUB, op_decl->file, op_decl->line);
1741 29           char* sub_name = SPVM_COMPILER_ALLOCATOR_safe_malloc_zero(compiler, 4 + strlen(package_var->name) - 1 + 1);
1742 29           memcpy(sub_name, "SET_", 4);
1743 29           memcpy(sub_name + 4, package_var->name + 1, strlen(package_var->name) - 1);
1744 29           SPVM_OP* op_name_sub = SPVM_OP_new_op_name(compiler, sub_name, op_decl->file, op_decl->line);
1745 29           SPVM_OP* op_return_type = SPVM_OP_new_op_void_type(compiler, op_decl->file, op_decl->line);
1746 29           SPVM_OP* op_args = SPVM_OP_new_op_list(compiler, op_decl->file, op_decl->line);
1747              
1748 29           SPVM_TYPE* arg_multi_numeric_type = SPVM_TYPE_new(compiler);
1749 29           arg_multi_numeric_type->basic_type = package_var->type->basic_type;
1750 29           arg_multi_numeric_type->dimension = package_var->type->dimension;
1751 29           arg_multi_numeric_type->flag = package_var->type->flag;
1752 29           SPVM_OP* op_type_value = SPVM_OP_new_op_type(compiler, arg_multi_numeric_type, op_decl->file, op_decl->line);
1753 29           SPVM_OP* op_var_value_name = SPVM_OP_new_op_name(compiler, package_var->name, op_decl->file, op_decl->line);
1754 29           SPVM_OP* op_var_value = SPVM_OP_new_op_var(compiler, op_var_value_name);
1755 29           SPVM_OP* op_arg_value = SPVM_OP_build_arg(compiler, op_var_value, op_type_value);
1756              
1757 29           SPVM_OP_insert_child(compiler, op_args, op_args->last, op_arg_value);
1758            
1759 29           SPVM_OP* op_block = SPVM_OP_new_op_block(compiler, op_decl->file, op_decl->line);
1760 29           SPVM_OP* op_statements = SPVM_OP_new_op_list(compiler, op_decl->file, op_decl->line);
1761              
1762 29           SPVM_OP* op_name_package_var_access = SPVM_OP_new_op_name(compiler, package_var->name, op_decl->file, op_decl->line);
1763 29           SPVM_OP* op_package_var_access = SPVM_OP_build_package_var_access(compiler, op_name_package_var_access);
1764              
1765 29           SPVM_OP* op_var_assign_value_name = SPVM_OP_new_op_name(compiler, package_var->name, op_decl->file, op_decl->line);
1766 29           SPVM_OP* op_var_assign_value = SPVM_OP_new_op_var(compiler, op_var_assign_value_name);
1767            
1768 29           SPVM_OP* op_assign = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_decl->file, op_decl->line);
1769 29           SPVM_OP_build_assign(compiler, op_assign, op_package_var_access, op_var_assign_value);
1770            
1771 29           SPVM_OP_insert_child(compiler, op_statements, op_statements->last, op_assign);
1772 29           SPVM_OP_insert_child(compiler, op_block, op_block->last, op_statements);
1773            
1774 29           SPVM_OP_build_sub(compiler, op_sub, op_name_sub, op_return_type, op_args, NULL, op_block, NULL, NULL, 0);
1775            
1776 29           op_sub->uv.sub->is_package_var_setter = 1;
1777 29           op_sub->uv.sub->accessor_original_name = package_var->name;
1778            
1779 213           SPVM_LIST_push(package->subs, op_sub->uv.sub);
1780             }
1781             }
1782             // Field declarations
1783 35813 100         else if (op_decl->id == SPVM_OP_C_ID_FIELD) {
1784 3859           SPVM_FIELD* field = op_decl->uv.field;
1785            
1786 3859 50         if (package->category == SPVM_PACKAGE_C_CATEGORY_CALLBACK) {
1787 0           SPVM_COMPILER_error(compiler, "Callback package can't have field at %s line %d\n", op_decl->file, op_decl->line);
1788             }
1789 3859           SPVM_LIST_push(package->fields, field);
1790            
1791             // Getter
1792 3859 100         if (field->has_getter) {
1793             // sub foo : int ($self : self) {
1794             // return $self->{foo};
1795             // }
1796            
1797 2032           SPVM_OP* op_sub = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_SUB, op_decl->file, op_decl->line);
1798 2032           SPVM_OP* op_name_sub = SPVM_OP_new_op_name(compiler, field->name, op_decl->file, op_decl->line);
1799 2032           SPVM_TYPE* return_type = SPVM_TYPE_new(compiler);
1800 2032           return_type->basic_type = field->type->basic_type;
1801 2032           return_type->dimension = field->type->dimension;
1802 2032           return_type->flag = field->type->flag;
1803 2032           SPVM_OP* op_return_type = SPVM_OP_new_op_type(compiler, return_type, op_decl->file, op_decl->line);
1804 2032           SPVM_OP* op_args = SPVM_OP_new_op_list(compiler, op_decl->file, op_decl->line);
1805 2032           SPVM_OP* op_var_name = SPVM_OP_new_op_name(compiler, "$self", op_decl->file, op_decl->line);
1806 2032           SPVM_OP* op_var_self = SPVM_OP_new_op_var(compiler, op_var_name);
1807 2032           SPVM_TYPE* self_type = SPVM_TYPE_new(compiler);
1808 2032           self_type->is_self = 1;
1809 2032           SPVM_OP* op_self_type = SPVM_OP_new_op_type(compiler, self_type, op_decl->file, op_decl->line);
1810 2032           SPVM_OP* op_arg_self = SPVM_OP_build_arg(compiler, op_var_self, op_self_type);
1811 2032           SPVM_OP_insert_child(compiler, op_args, op_args->last, op_arg_self);
1812            
1813 2032           SPVM_OP* op_block = SPVM_OP_new_op_block(compiler, op_decl->file, op_decl->line);
1814 2032           SPVM_OP* op_statements = SPVM_OP_new_op_list(compiler, op_decl->file, op_decl->line);
1815 2032           SPVM_OP* op_return = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_RETURN, op_decl->file, op_decl->line);
1816              
1817 2032           SPVM_OP* op_var_name_invocant = SPVM_OP_new_op_name(compiler, "$self", op_decl->file, op_decl->line);
1818 2032           SPVM_OP* op_var_self_invocant = SPVM_OP_new_op_var(compiler, op_var_name_invocant);
1819 2032           SPVM_OP* op_name_field_access = SPVM_OP_new_op_name(compiler, field->name, op_decl->file, op_decl->line);
1820 2032           SPVM_OP* op_field_access = SPVM_OP_build_field_access(compiler, op_var_self_invocant, op_name_field_access);
1821            
1822 2032           SPVM_OP_insert_child(compiler, op_return, op_return->last, op_field_access);
1823 2032           SPVM_OP_insert_child(compiler, op_statements, op_statements->last, op_return);
1824 2032           SPVM_OP_insert_child(compiler, op_block, op_block->last, op_statements);
1825            
1826 2032           SPVM_OP_build_sub(compiler, op_sub, op_name_sub, op_return_type, op_args, NULL, op_block, NULL, NULL, 0);
1827            
1828 2032           op_sub->uv.sub->is_field_getter = 1;
1829 2032           op_sub->uv.sub->accessor_original_name = field->name;
1830            
1831 2032           SPVM_LIST_push(package->subs, op_sub->uv.sub);
1832             }
1833              
1834             // Setter
1835 3859 100         if (field->has_setter) {
1836             // sub set_foo : void ($self : self, $foo : int) {
1837             // $self->{foo} = $foo;
1838             // }
1839              
1840 590           SPVM_OP* op_sub = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_SUB, op_decl->file, op_decl->line);
1841 590           char* sub_name = SPVM_COMPILER_ALLOCATOR_safe_malloc_zero(compiler, 4 + strlen(field->name) + 1);
1842 590           memcpy(sub_name, "set_", 4);
1843 590           memcpy(sub_name + 4, field->name, strlen(field->name));
1844 590           SPVM_OP* op_name_sub = SPVM_OP_new_op_name(compiler, sub_name, op_decl->file, op_decl->line);
1845 590           SPVM_OP* op_return_type = SPVM_OP_new_op_void_type(compiler, op_decl->file, op_decl->line);
1846 590           SPVM_OP* op_args = SPVM_OP_new_op_list(compiler, op_decl->file, op_decl->line);
1847 590           SPVM_OP* op_arg_var_name_self = SPVM_OP_new_op_name(compiler, "$self", op_decl->file, op_decl->line);
1848 590           SPVM_OP* op_arg_var_self = SPVM_OP_new_op_var(compiler, op_arg_var_name_self);
1849 590           SPVM_TYPE* self_type = SPVM_TYPE_new(compiler);
1850 590           self_type->is_self = 1;
1851 590           SPVM_OP* op_self_type = SPVM_OP_new_op_type(compiler, self_type, op_decl->file, op_decl->line);
1852 590           SPVM_OP* op_arg_self = SPVM_OP_build_arg(compiler, op_arg_var_self, op_self_type);
1853              
1854 590           SPVM_TYPE* arg_multi_numeric_type = SPVM_TYPE_new(compiler);
1855 590           arg_multi_numeric_type->basic_type = field->type->basic_type;
1856 590           arg_multi_numeric_type->dimension = field->type->dimension;
1857 590           arg_multi_numeric_type->flag = field->type->flag;
1858 590           SPVM_OP* op_type_value = SPVM_OP_new_op_type(compiler, arg_multi_numeric_type, op_decl->file, op_decl->line);
1859 590           SPVM_OP* op_var_value_name = SPVM_OP_new_op_name(compiler, field->name, op_decl->file, op_decl->line);
1860 590           SPVM_OP* op_var_value = SPVM_OP_new_op_var(compiler, op_var_value_name);
1861 590           SPVM_OP* op_arg_value = SPVM_OP_build_arg(compiler, op_var_value, op_type_value);
1862              
1863 590           SPVM_OP_insert_child(compiler, op_args, op_args->last, op_arg_self);
1864 590           SPVM_OP_insert_child(compiler, op_args, op_args->last, op_arg_value);
1865            
1866 590           SPVM_OP* op_block = SPVM_OP_new_op_block(compiler, op_decl->file, op_decl->line);
1867 590           SPVM_OP* op_statements = SPVM_OP_new_op_list(compiler, op_decl->file, op_decl->line);
1868              
1869 590           SPVM_OP* op_var_name_invocant = SPVM_OP_new_op_name(compiler, "$self", op_decl->file, op_decl->line);
1870 590           SPVM_OP* op_var_self_invocant = SPVM_OP_new_op_var(compiler, op_var_name_invocant);
1871 590           SPVM_OP* op_name_field_access = SPVM_OP_new_op_name(compiler, field->name, op_decl->file, op_decl->line);
1872 590           SPVM_OP* op_field_access = SPVM_OP_build_field_access(compiler, op_var_self_invocant, op_name_field_access);
1873              
1874 590           SPVM_OP* op_var_assign_value_name = SPVM_OP_new_op_name(compiler, field->name, op_decl->file, op_decl->line);
1875 590           SPVM_OP* op_var_assign_value = SPVM_OP_new_op_var(compiler, op_var_assign_value_name);
1876            
1877 590           SPVM_OP* op_assign = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_decl->file, op_decl->line);
1878 590           SPVM_OP_build_assign(compiler, op_assign, op_field_access, op_var_assign_value);
1879            
1880 590           SPVM_OP_insert_child(compiler, op_statements, op_statements->last, op_assign);
1881 590           SPVM_OP_insert_child(compiler, op_block, op_block->last, op_statements);
1882            
1883 590           SPVM_OP_build_sub(compiler, op_sub, op_name_sub, op_return_type, op_args, NULL, op_block, NULL, NULL, 0);
1884            
1885 590           op_sub->uv.sub->is_field_setter = 1;
1886 590           op_sub->uv.sub->accessor_original_name = field->name;
1887            
1888 3859           SPVM_LIST_push(package->subs, op_sub->uv.sub);
1889             }
1890             }
1891             // Enum declarations
1892 31954 100         else if (op_decl->id == SPVM_OP_C_ID_ENUM) {
1893 19           SPVM_OP* op_enum_block = op_decl->first;
1894 19           SPVM_OP* op_enumeration_values = op_enum_block->first;
1895 19           SPVM_OP* op_sub = op_enumeration_values->first;
1896 77 100         while ((op_sub = SPVM_OP_sibling(compiler, op_sub))) {
1897 58           SPVM_LIST_push(package->subs, op_sub->uv.sub);
1898             }
1899             }
1900             // Sub declarations
1901 31935 50         else if (op_decl->id == SPVM_OP_C_ID_SUB) {
1902 31935           SPVM_LIST_push(package->subs, op_decl->uv.sub);
1903            
1904             // Captures is added to field
1905 31935           SPVM_LIST* captures = op_decl->uv.sub->captures;
1906 31954 100         for (int32_t i = 0; i < captures->length; i++) {
1907 19           SPVM_MY* capture_my = SPVM_LIST_fetch(captures, i);
1908            
1909 19           SPVM_OP* op_field = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_FIELD, capture_my->op_my->file, capture_my->op_my->line);
1910 19           SPVM_OP* op_name_field = SPVM_OP_new_op_name(compiler, capture_my->op_name->uv.name + 1, capture_my->op_my->file, capture_my->op_my->line);
1911            
1912 19           SPVM_OP_build_has(compiler, op_field, op_name_field, NULL, capture_my->type->op_type);
1913 19           SPVM_LIST_push(package->fields, op_field->uv.field);
1914 19           op_field->uv.field->is_captured = 1;
1915             }
1916            
1917             // Begin block
1918 31935 100         if (op_decl->uv.sub->is_begin) {
1919 31935           package->op_begin_sub = op_decl;
1920             }
1921             }
1922             else {
1923 0           assert(0);
1924             }
1925             }
1926            
1927             // Sort fields(except value package)
1928 2832 100         if (package->category != SPVM_PACKAGE_C_CATEGORY_VALUE) {
1929 2815 100         for (int32_t i = 0; i < (package->fields->length - 1); i++) {
1930 2730 100         for (int32_t j = (package->fields->length - 1); j > i; j--) {
1931 2161           SPVM_FIELD* field1 = SPVM_LIST_fetch(package->fields, j-1);
1932 2161           SPVM_FIELD* field2 = SPVM_LIST_fetch(package->fields, j);
1933            
1934 2161           void** values = package->fields->values;
1935              
1936 2161 100         if (strcmp(field1->name, field2->name) > 0) {
1937 1319           SPVM_FIELD* temp = values[j-1];
1938 1319           values[j-1] = values[j];
1939 1319           values[j] = temp;
1940             }
1941             }
1942             }
1943             }
1944            
1945             // Field declarations
1946 6710 100         for (int32_t i = 0; i < package->fields->length; i++) {
1947 3878           SPVM_FIELD* field = SPVM_LIST_fetch(package->fields, i);
1948              
1949 3878 50         if (package->flag & SPVM_PACKAGE_C_FLAG_POINTER) {
1950 0           SPVM_COMPILER_error(compiler, "package which has pointer_t descriptor can't have fields at %s line %d\n", field->op_field->file, field->op_field->line);
1951 0           continue;
1952             }
1953              
1954 3878           field->index = i;
1955 3878           const char* field_name = field->op_name->uv.name;
1956              
1957 3878           SPVM_FIELD* found_field = SPVM_HASH_fetch(package->field_symtable, field_name, strlen(field_name));
1958            
1959 3878 50         if (found_field) {
1960 0           SPVM_COMPILER_error(compiler, "Redeclaration of field \"%s::%s\" at %s line %d\n", package_name, field_name, field->op_field->file, field->op_field->line);
1961             }
1962 3878 50         else if (package->fields->length > SPVM_LIMIT_C_FIELDS_MAX_COUNT) {
1963 0           SPVM_COMPILER_error(compiler, "Too many field declarations at %s line %d\n", field->op_field->file, field->op_field->line);
1964             }
1965             else {
1966 3878           field->id = compiler->fields->length;
1967 3878           SPVM_LIST_push(compiler->fields, field);
1968 3878           SPVM_HASH_insert(package->field_symtable, field_name, strlen(field_name), field);
1969            
1970             // Add op package
1971 3878           field->package = package;
1972             }
1973             }
1974              
1975             // Sort package variables
1976 2975 100         for (int32_t i = 0; i < (package->package_vars->length - 1); i++) {
1977 521 100         for (int32_t j = (package->package_vars->length - 1); j > i; j--) {
1978 378           SPVM_PACKAGE_VAR* package_var1 = SPVM_LIST_fetch(package->package_vars, j-1);
1979 378           SPVM_PACKAGE_VAR* package_var2 = SPVM_LIST_fetch(package->package_vars, j);
1980            
1981 378           void** values = package->package_vars->values;
1982              
1983 378 100         if (strcmp(package_var1->name, package_var2->name) > 0) {
1984 124           SPVM_PACKAGE_VAR* temp = values[j-1];
1985 124           values[j-1] = values[j];
1986 124           values[j] = temp;
1987             }
1988             }
1989             }
1990              
1991             // Package variable declarations
1992             {
1993             int32_t i;
1994 3045 100         for (i = 0; i < package->package_vars->length; i++) {
1995 213           SPVM_PACKAGE_VAR* package_var = SPVM_LIST_fetch(package->package_vars, i);
1996 213           const char* package_var_name = package_var->name;
1997              
1998             // Add package var name to string pool
1999 213           int32_t found_string_pool_id = (intptr_t)SPVM_HASH_fetch(compiler->string_symtable, package_var_name, strlen(package_var_name) + 1);
2000 213 100         if (found_string_pool_id == 0) {
2001 200           int32_t string_pool_id = SPVM_STRING_BUFFER_add_len(compiler->string_pool, (char*)package_var_name, strlen(package_var_name) + 1);
2002 200           SPVM_HASH_insert(compiler->string_symtable, package_var_name, strlen(package_var_name) + 1, (void*)(intptr_t)string_pool_id);
2003             }
2004            
2005 213           SPVM_PACKAGE_VAR* found_package_var = SPVM_HASH_fetch(package->package_var_symtable, package_var_name, strlen(package_var_name));
2006            
2007 213 50         if (found_package_var) {
2008 0           SPVM_COMPILER_error(compiler, "Redeclaration of package variable \"%s::%s\" at %s line %d\n", package_name, package_var_name, package_var->op_package_var->file, package_var->op_package_var->line);
2009             }
2010 213 50         else if (package->package_vars->length > SPVM_LIMIT_C_PACKAGE_VARS_MAX_COUNT) {
2011 0           SPVM_COMPILER_error(compiler, "Too many package variable declarations at %s line %d\n", package_var->op_package_var->file, package_var->op_package_var->line);
2012             }
2013             else {
2014 213           package_var->id = compiler->package_vars->length;
2015 213           SPVM_LIST_push(compiler->package_vars, package_var);
2016 213           SPVM_HASH_insert(package->package_var_symtable, package_var_name, strlen(package_var_name), package_var);
2017            
2018             // Add op package
2019 213           package_var->package = package;
2020             }
2021             }
2022             }
2023            
2024             // Sort subs
2025 35267 100         for (int32_t i = 0; i < (package->subs->length - 1); i++) {
2026 1491904 100         for (int32_t j = (package->subs->length - 1); j > i; j--) {
2027 1459469           SPVM_SUB* sub1 = SPVM_LIST_fetch(package->subs, j-1);
2028 1459469           SPVM_SUB* sub2 = SPVM_LIST_fetch(package->subs, j);
2029            
2030 1459469           void** values = package->subs->values;
2031              
2032 1459469 100         if (strcmp(sub1->name, sub2->name) > 0) {
2033 1026965           SPVM_SUB* temp = values[j-1];
2034 1026965           values[j-1] = values[j];
2035 1026965           values[j] = temp;
2036             }
2037             }
2038             }
2039            
2040             // Subroutine declarations
2041             {
2042             int32_t i;
2043 37507 100         for (i = 0; i < package->subs->length; i++) {
2044 34675           SPVM_SUB* sub = SPVM_LIST_fetch(package->subs, i);
2045            
2046 34675 100         if (sub->flag & SPVM_SUB_C_FLAG_ANON) {
2047 39           package->flag |= SPVM_PACKAGE_C_FLAG_ANON_SUB_PACKAGE;
2048 39 50         assert(package->subs->length == 1);
2049 39 50         assert(is_anon);
2050             }
2051              
2052 34675           sub->rel_id = i;
2053            
2054 34675           SPVM_OP* op_name_sub = sub->op_name;
2055 34675           const char* sub_name = op_name_sub->uv.name;
2056              
2057             // Add sub name to string pool
2058 34675           int32_t found_string_pool_id = (intptr_t)SPVM_HASH_fetch(compiler->string_symtable, sub_name, strlen(sub_name) + 1);
2059 34675 100         if (found_string_pool_id == 0) {
2060 31660           int32_t string_pool_id = SPVM_STRING_BUFFER_add_len(compiler->string_pool, (char*)sub_name, strlen(sub_name) + 1);
2061 31660           SPVM_HASH_insert(compiler->string_symtable, sub_name, strlen(sub_name) + 1, (void*)(intptr_t)string_pool_id);
2062             }
2063              
2064             // Method check
2065            
2066             // Set first argument type if not set
2067 34675 100         if (sub->args->length > 0) {
2068 27232           SPVM_MY* arg_my_first = SPVM_LIST_fetch(sub->args, 0);
2069 27232           SPVM_OP* op_arg_first_type = NULL;
2070 27232 100         if (arg_my_first->type->is_self) {
2071 3284           SPVM_TYPE* arg_invocant_type = SPVM_TYPE_clone_type(compiler, op_type->uv.type);
2072 3284           op_arg_first_type = SPVM_OP_new_op_type(compiler, arg_invocant_type, sub->op_sub->file, sub->op_sub->line);
2073 3284           arg_my_first->type = op_arg_first_type->uv.type;
2074 3284           sub->call_type_id = SPVM_SUB_C_CALL_TYPE_ID_METHOD;
2075 3284 50         assert(arg_invocant_type->basic_type);
2076             }
2077             else {
2078 23948           op_arg_first_type = arg_my_first->type->op_type;
2079 23948 50         assert(op_arg_first_type->uv.type->basic_type);
2080             }
2081             }
2082            
2083             // Subroutine in callback package must be method
2084 34675 100         if (package->category == SPVM_PACKAGE_C_CATEGORY_CALLBACK && sub->call_type_id != SPVM_SUB_C_CALL_TYPE_ID_METHOD) {
    50          
2085 0           SPVM_COMPILER_error(compiler, "Subroutine in callback package must be method at %s line %d\n", sub->op_sub->file, sub->op_sub->line);
2086             }
2087            
2088             // If Subroutine is anon, sub must be method
2089 34675 100         if (strlen(sub_name) == 0 && sub->call_type_id != SPVM_SUB_C_CALL_TYPE_ID_METHOD) {
    50          
2090 0           SPVM_COMPILER_error(compiler, "Anon subroutine must be method at %s line %d\n", sub->op_sub->file, sub->op_sub->line);
2091             }
2092              
2093             // If package is callback, sub must not be native
2094 34675 100         if (package->category == SPVM_PACKAGE_C_CATEGORY_CALLBACK && (sub->flag & SPVM_SUB_C_FLAG_NATIVE)) {
    50          
2095 0           SPVM_COMPILER_error(compiler, "Subroutine of callback can't have native descriptor at %s line %d\n", sub->op_sub->file, sub->op_sub->line);
2096             }
2097              
2098             // If package is callback, sub must not be precompile
2099 34675 100         if (package->category == SPVM_PACKAGE_C_CATEGORY_CALLBACK && (sub->flag & SPVM_SUB_C_FLAG_PRECOMPILE)) {
    50          
2100 0           SPVM_COMPILER_error(compiler, "Subroutine of callback can't have precompile descriptor at %s line %d\n", sub->op_sub->file, sub->op_sub->line);
2101             }
2102              
2103             // If package is callback, sub must not be precompile
2104 34675 100         if (package->category == SPVM_PACKAGE_C_CATEGORY_CALLBACK && (sub->flag & SPVM_SUB_C_FLAG_PRECOMPILE)) {
    50          
2105 0           SPVM_COMPILER_error(compiler, "Subroutine of callback can't have precompile descriptor at %s line %d\n", sub->op_sub->file, sub->op_sub->line);
2106             }
2107              
2108             // If package is callback, sub must not be precompile
2109 34675 100         if (!sub->op_block) {
2110 13873 100         if (package->category == SPVM_PACKAGE_C_CATEGORY_CALLBACK) {
2111             // OK
2112             }
2113 13854 50         else if (sub->flag & SPVM_SUB_C_FLAG_NATIVE) {
2114             // OK
2115             }
2116             else {
2117 0           SPVM_COMPILER_error(compiler, "Subroutine must have implementation block at %s line %d\n", sub->op_sub->file, sub->op_sub->line);
2118             }
2119             }
2120            
2121 34675           SPVM_SUB* found_sub = SPVM_HASH_fetch(package->sub_symtable, sub_name, strlen(sub_name));
2122            
2123 34675 50         if (found_sub) {
2124 0           SPVM_COMPILER_error(compiler, "Redeclaration of sub \"%s\" at %s line %d\n", sub_name, sub->op_sub->file, sub->op_sub->line);
2125             }
2126 34675 50         else if (package->subs->length > SPVM_LIMIT_C_SUBS_MAX_COUNT) {
2127 0           SPVM_COMPILER_error(compiler, "Too many sub declarations at %s line %d\n", sub_name, sub->op_sub->file, sub->op_sub->line);
2128             }
2129             // Unknown sub
2130             else {
2131 34675           const char* found_sub_name = SPVM_HASH_fetch(package->sub_name_symtable, sub_name, strlen(sub_name));
2132 34675 50         if (found_sub_name) {
2133 0           SPVM_COMPILER_error(compiler, "Redeclaration of sub \"%s\" at %s line %d\n", sub_name, sub->op_sub->file, sub->op_sub->line);
2134             }
2135             else {
2136             // Bind standard functions
2137 34675           sub->package = package;
2138            
2139 34675 100         if (sub->flag & SPVM_SUB_C_FLAG_DESTRUCTOR) {
2140 11           package->sub_destructor = sub;
2141             }
2142            
2143 34675 50         assert(sub->op_sub->file);
2144            
2145 34675           sub->id = compiler->subs->length;
2146            
2147 34675           SPVM_LIST_push(compiler->subs, sub);
2148 34675           SPVM_HASH_insert(package->sub_symtable, sub->op_name->uv.name, strlen(sub->op_name->uv.name), sub);
2149             }
2150             }
2151             }
2152             }
2153            
2154             // Callback must have only one method
2155 2832 100         if (package->category == SPVM_PACKAGE_C_CATEGORY_CALLBACK) {
2156 19 50         if (package->subs->length != 1) {
2157 0           SPVM_COMPILER_error(compiler, "Callback must have only one method at %s line %d\n", op_package->file, op_package->line);
2158             }
2159             }
2160            
2161             // Set package
2162 2832           op_package->uv.package = package;
2163            
2164 2832           package->op_package = op_package;
2165             }
2166            
2167 2832           return op_package;
2168             }
2169              
2170 2994           SPVM_OP* SPVM_OP_build_use(SPVM_COMPILER* compiler, SPVM_OP* op_use, SPVM_OP* op_type, SPVM_OP* op_sub_names, int32_t is_require) {
2171            
2172 2994           SPVM_USE* use = SPVM_USE_new(compiler);
2173 2994           op_use->uv.use = use;
2174 2994           use->op_type = op_type;
2175 2994           use->is_require = is_require;
2176              
2177             // Check sub_names
2178 2994 100         if (op_sub_names) {
2179 12           SPVM_LIST* sub_names = SPVM_COMPILER_ALLOCATOR_alloc_list(compiler, 0);
2180 12           SPVM_OP* op_sub_name = op_sub_names->first;
2181 37 100         while ((op_sub_name = SPVM_OP_sibling(compiler, op_sub_name))) {
2182 25           const char* sub_name = op_sub_name->uv.name;
2183 25           SPVM_LIST_push(sub_names, (void*)sub_name);
2184             }
2185 12           use->sub_names = sub_names;
2186             }
2187              
2188 2994           SPVM_LIST_push(compiler->op_use_stack, op_use);
2189            
2190 2994           return op_use;
2191             }
2192              
2193 7           SPVM_OP* SPVM_OP_build_allow(SPVM_COMPILER* compiler, SPVM_OP* op_allow, SPVM_OP* op_type) {
2194            
2195 7           SPVM_ALLOW* allow = SPVM_ALLOW_new(compiler);
2196 7           allow->op_type = op_type;
2197 7           op_allow->uv.allow = allow;
2198            
2199 7           return op_allow;
2200             }
2201              
2202 213           SPVM_OP* SPVM_OP_build_our(SPVM_COMPILER* compiler, SPVM_OP* op_package_var, SPVM_OP* op_name, SPVM_OP* op_descriptors, SPVM_OP* op_type) {
2203            
2204 213           SPVM_PACKAGE_VAR* package_var = SPVM_PACKAGE_VAR_new(compiler);
2205            
2206 213           const char* name = op_name->uv.name;;
2207 213           package_var->name = op_name->uv.name;
2208            
2209 213           int32_t invalid_name = 0;
2210 213 50         if (strchr(name, ':')) {
2211 0           invalid_name = 1;
2212             }
2213            
2214 213 50         if (invalid_name) {
2215 0           SPVM_COMPILER_error(compiler, "Invalid package variable name %s at %s line %d\n", name, op_name->file, op_name->line);
2216             }
2217            
2218 213           package_var->op_name = op_name;
2219 213           package_var->type = op_type->uv.type;
2220 213           package_var->op_package_var = op_package_var;
2221              
2222 213           op_package_var->uv.package_var = package_var;
2223              
2224             // Check descriptors
2225 213 50         if (op_descriptors) {
2226 213           int32_t accessor_descriptors_count = 0;
2227 213           int32_t access_control_descriptors_count = 0;
2228 213           SPVM_OP* op_descriptor = op_descriptors->first;
2229 302 100         while ((op_descriptor = SPVM_OP_sibling(compiler, op_descriptor))) {
2230 89           SPVM_DESCRIPTOR* descriptor = op_descriptor->uv.descriptor;
2231            
2232 89           switch (descriptor->id) {
2233             case SPVM_DESCRIPTOR_C_ID_PRIVATE:
2234             // Default is private
2235 29           access_control_descriptors_count++;
2236 29           break;
2237             case SPVM_DESCRIPTOR_C_ID_PUBLIC:
2238 29           package_var->flag |= SPVM_PACKAGE_VAR_C_FLAG_PUBLIC;
2239 29           access_control_descriptors_count++;
2240 29           break;
2241             case SPVM_DESCRIPTOR_C_ID_RW:
2242 29           package_var->has_setter = 1;
2243 29           package_var->has_getter = 1;
2244 29           accessor_descriptors_count++;
2245 29           break;
2246             case SPVM_DESCRIPTOR_C_ID_RO:
2247 2           package_var->has_getter = 1;
2248 2           accessor_descriptors_count++;
2249 2           break;
2250             case SPVM_DESCRIPTOR_C_ID_WO:
2251 0           package_var->has_setter = 1;
2252 0           accessor_descriptors_count++;
2253 0           break;
2254             default:
2255 0           SPVM_COMPILER_error(compiler, "Invalid package variable descriptor in package variable declaration %s at %s line %d\n", SPVM_DESCRIPTOR_C_ID_NAMES[descriptor->id], op_descriptors->file, op_descriptors->line);
2256             }
2257 89 50         if (accessor_descriptors_count > 1) {
2258 0           SPVM_COMPILER_error(compiler, "rw, ro, wo can be specifed only one in package variable declaration at %s line %d\n", op_package_var->file, op_package_var->line);
2259             }
2260 89 50         if (access_control_descriptors_count > 1) {
2261 0           SPVM_COMPILER_error(compiler, "private, public can be specifed only one in package variable declaration at %s line %d\n", op_package_var->file, op_package_var->line);
2262             }
2263             }
2264             }
2265            
2266 213           return op_package_var;
2267             }
2268              
2269 3878           SPVM_OP* SPVM_OP_build_has(SPVM_COMPILER* compiler, SPVM_OP* op_field, SPVM_OP* op_name_field, SPVM_OP* op_descriptors, SPVM_OP* op_type) {
2270              
2271             // Create field information
2272 3878           SPVM_FIELD* field = SPVM_FIELD_new(compiler);
2273            
2274             // Name
2275 3878           field->op_name = op_name_field;
2276            
2277 3878           field->name = op_name_field->uv.name;
2278            
2279             // Type
2280 3878           field->type = op_type->uv.type;
2281            
2282             // Set field informaiton
2283 3878           op_field->uv.field = field;
2284            
2285             // Check descriptors
2286 3878 100         if (op_descriptors) {
2287 3859           SPVM_OP* op_descriptor = op_descriptors->first;
2288 3859           int32_t accessor_descriptors_count = 0;
2289 3859           int32_t access_control_descriptors_count = 0;
2290 6510 100         while ((op_descriptor = SPVM_OP_sibling(compiler, op_descriptor))) {
2291 2651           SPVM_DESCRIPTOR* descriptor = op_descriptor->uv.descriptor;
2292            
2293 2651           switch (descriptor->id) {
2294             case SPVM_DESCRIPTOR_C_ID_PRIVATE:
2295             // Default is private
2296 22           access_control_descriptors_count++;
2297 22           break;
2298             case SPVM_DESCRIPTOR_C_ID_PUBLIC:
2299 597           field->flag |= SPVM_FIELD_C_FLAG_PUBLIC;
2300 597           access_control_descriptors_count++;
2301 597           break;
2302             case SPVM_DESCRIPTOR_C_ID_RW:
2303 590           field->has_setter = 1;
2304 590           field->has_getter = 1;
2305 590           accessor_descriptors_count++;
2306 590           break;
2307             case SPVM_DESCRIPTOR_C_ID_RO:
2308 1442           field->has_getter = 1;
2309 1442           accessor_descriptors_count++;
2310 1442           break;
2311             case SPVM_DESCRIPTOR_C_ID_WO:
2312 0           field->has_setter = 1;
2313 0           accessor_descriptors_count++;
2314 0           break;
2315             default:
2316 0           SPVM_COMPILER_error(compiler, "Invalid field descriptor %s at %s line %d\n", SPVM_DESCRIPTOR_C_ID_NAMES[descriptor->id], op_descriptors->file, op_descriptors->line);
2317             }
2318            
2319 2651 50         if (accessor_descriptors_count > 1) {
2320 0           SPVM_COMPILER_error(compiler, "rw, ro, wo can be specifed only one in field declaration at %s line %d\n", op_field->file, op_field->line);
2321             }
2322 2651 50         if (access_control_descriptors_count > 1) {
2323 0           SPVM_COMPILER_error(compiler, "public, private can be specifed only one in field declaration at %s line %d\n", op_field->file, op_field->line);
2324             }
2325             }
2326             }
2327            
2328 3878           field->op_field = op_field;
2329            
2330 3878           return op_field;
2331             }
2332              
2333 34675           SPVM_OP* SPVM_OP_build_sub(SPVM_COMPILER* compiler, SPVM_OP* op_sub, SPVM_OP* op_name_sub, SPVM_OP* op_return_type, SPVM_OP* op_args, SPVM_OP* op_descriptors, SPVM_OP* op_block, SPVM_OP* op_captures, SPVM_OP* op_dot3, int32_t is_begin) {
2334 34675           SPVM_SUB* sub = SPVM_SUB_new(compiler);
2335            
2336             // Anon sub
2337 34675 100         if (!op_name_sub) {
2338 39           sub->flag |= SPVM_SUB_C_FLAG_ANON;
2339            
2340             // Anon sub name
2341 39           char* name_sub = SPVM_COMPILER_ALLOCATOR_safe_malloc_zero(compiler, 1);
2342 39           op_name_sub = SPVM_OP_new_op_name(compiler, name_sub, op_sub->file, op_sub->line);
2343             }
2344            
2345 34675           const char* sub_name = op_name_sub->uv.name;
2346            
2347             // Block is sub block
2348 34675 100         if (op_block) {
2349 20802           op_block->uv.block->id = SPVM_BLOCK_C_ID_SUB;
2350             }
2351            
2352             // Create sub information
2353 34675           sub->op_name = op_name_sub;
2354            
2355 34675           sub->file = op_sub->file;
2356 34675           sub->line = op_sub->line;
2357 34675           sub->name = sub->op_name->uv.name;
2358            
2359 34675 100         if (op_dot3) {
2360 12           sub->have_vaarg = 1;
2361             }
2362            
2363 34675           sub->is_begin = is_begin;
2364 34675 100         if (!is_begin && strcmp(sub_name, "BEGIN") == 0) {
    100          
2365 2           SPVM_COMPILER_error(compiler, "\"BEGIN\" is reserved for BEGIN block at %s line %d\n", op_name_sub->file, op_name_sub->line);
2366             }
2367            
2368             // Descriptors
2369 34675           int32_t access_control_descriptors_count = 0;
2370 34675 100         if (op_descriptors) {
2371 31924           SPVM_OP* op_descriptor = op_descriptors->first;
2372 53758 100         while ((op_descriptor = SPVM_OP_sibling(compiler, op_descriptor))) {
2373 21834           SPVM_DESCRIPTOR* descriptor = op_descriptor->uv.descriptor;
2374            
2375 21834           switch (descriptor->id) {
2376             case SPVM_DESCRIPTOR_C_ID_PRIVATE:
2377 42           sub->flag |= SPVM_SUB_C_FLAG_PRIVATE;
2378 42           access_control_descriptors_count++;
2379 42           break;
2380             case SPVM_DESCRIPTOR_C_ID_PUBLIC:
2381             // Default is public
2382 0           access_control_descriptors_count++;
2383 0           break;
2384             case SPVM_DESCRIPTOR_C_ID_NATIVE:
2385 13854           sub->flag |= SPVM_SUB_C_FLAG_NATIVE;
2386 13854           break;
2387             case SPVM_DESCRIPTOR_C_ID_PRECOMPILE:
2388 7938           sub->flag |= SPVM_SUB_C_FLAG_PRECOMPILE;
2389 7938           break;
2390             default:
2391 0           SPVM_COMPILER_error(compiler, "invalid subroutine descriptor %s", SPVM_DESCRIPTOR_C_ID_NAMES[descriptor->id], op_descriptors->file, op_descriptors->line);
2392             }
2393             }
2394            
2395 31924 100         if ((sub->flag & SPVM_SUB_C_FLAG_NATIVE) && (sub->flag & SPVM_SUB_C_FLAG_PRECOMPILE)) {
    50          
2396 0           SPVM_COMPILER_error(compiler, "native and compile descriptor can't be used together", op_descriptors->file, op_descriptors->line);
2397             }
2398 31924 50         if (access_control_descriptors_count > 1) {
2399 0           SPVM_COMPILER_error(compiler, "public, private can be specifed only one in sub declaration at %s line %d\n", op_sub->file, op_sub->line);
2400             }
2401             }
2402              
2403             // Native subroutine can't have block
2404 34675 100         if ((sub->flag & SPVM_SUB_C_FLAG_NATIVE) && op_block) {
    50          
2405 0           SPVM_COMPILER_error(compiler, "Native subroutine can't have block", op_block->file, op_block->line);
2406             }
2407            
2408             // sub args
2409 34675 100         if (op_args) {
2410 34606           int32_t sub_index = 0;
2411 34606           SPVM_OP* op_arg = op_args->first;
2412 89020 100         while ((op_arg = SPVM_OP_sibling(compiler, op_arg))) {
2413 54414 100         if (sub_index == 0) {
2414             // Call type
2415 27232           SPVM_TYPE* type = op_arg->uv.var->my->type;
2416 27232 50         if (type) {
2417 27232 100         if (type->is_self) {
2418 3284           sub->call_type_id = SPVM_SUB_C_CALL_TYPE_ID_METHOD;
2419             }
2420             else {
2421 23948           sub->call_type_id = SPVM_SUB_C_CALL_TYPE_ID_CLASS_METHOD;
2422             }
2423             }
2424             }
2425 54414           SPVM_LIST_push(sub->args, op_arg->uv.var->my);
2426 54414           sub_index++;
2427             }
2428             }
2429              
2430             // Capture variables
2431 34675 100         if (op_captures) {
2432 13           SPVM_OP* op_capture = op_captures->first;
2433 32 100         while ((op_capture = SPVM_OP_sibling(compiler, op_capture))) {
2434 19           SPVM_LIST_push(sub->captures, op_capture->uv.var->my);
2435             }
2436             }
2437            
2438             // Native my vars is same as arguments
2439 34675 100         if (sub->flag & SPVM_SUB_C_FLAG_NATIVE) {
2440 13854           SPVM_OP* op_arg = op_args->first;
2441 38240 100         while ((op_arg = SPVM_OP_sibling(compiler, op_arg))) {
2442 24386           SPVM_LIST_push(sub->mys, op_arg->uv.var->my);
2443             }
2444             }
2445              
2446             // return type
2447 34675           sub->return_type = op_return_type->uv.type;
2448            
2449 34675 100         if (strcmp(sub->op_name->uv.name, "DESTROY") == 0) {
2450 11           sub->flag |= SPVM_SUB_C_FLAG_DESTRUCTOR;
2451            
2452             // DESTROY return type must be void
2453 11 50         if (!(sub->return_type->dimension == 0 && sub->return_type->basic_type->id == SPVM_BASIC_TYPE_C_ID_VOID)) {
    50          
2454 0           SPVM_COMPILER_error(compiler, "DESTROY return type must be void\n", op_block->file, op_block->line);
2455             }
2456             }
2457            
2458 34675 100         if (op_block) {
2459              
2460 20802           SPVM_OP* op_list_statement = op_block->first;
2461              
2462             // 2. Add variable declaration to first of block
2463             {
2464             int32_t i;
2465 50775 100         for (i = sub->args->length - 1; i >= 0; i--) {
2466 29973           SPVM_MY* arg_my = SPVM_LIST_fetch(sub->args, i);
2467 29973 50         assert(arg_my);
2468 29973           SPVM_OP* op_my = SPVM_OP_new_op_my(compiler, arg_my, arg_my->op_my->file, arg_my->op_my->line);
2469 29973           SPVM_OP* op_var = SPVM_OP_new_op_var(compiler, op_my->uv.my->op_name);
2470 29973           op_var->uv.var->my = arg_my;
2471 29973           op_var->uv.var->is_declaration = 1;
2472 29973           op_var->uv.var->is_arg = 1;
2473              
2474 29973           SPVM_OP_insert_child(compiler, op_list_statement, op_list_statement->first, op_var);
2475             }
2476             }
2477              
2478             // 1. Add condition_flag variable to first of block
2479             {
2480 20802           char* name = "@condition_flag";
2481 20802           SPVM_OP* op_name = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_NAME, op_list_statement->file, op_list_statement->last->line + 1);
2482 20802           op_name->uv.name = name;
2483 20802           SPVM_OP* op_var = SPVM_OP_build_var(compiler, op_name);
2484 20802           SPVM_MY* my = SPVM_MY_new(compiler);
2485 20802           SPVM_OP* op_my = SPVM_OP_new_op_my(compiler, my, op_list_statement->file, op_list_statement->last->line + 1);
2486 20802           SPVM_OP* op_type = SPVM_OP_new_op_int_type(compiler, op_list_statement->file, op_list_statement->line);
2487 20802           op_var = SPVM_OP_build_my(compiler, op_my, op_var, op_type);
2488 20802           SPVM_OP_insert_child(compiler, op_list_statement, op_list_statement->first, op_var);
2489 20802           sub->op_my_condition_flag = op_my;
2490             }
2491              
2492            
2493             // 3. Add list of temporary variable declarations to first of block
2494             {
2495 20802           SPVM_OP* op_list_tmp_mys = SPVM_OP_new_op_list(compiler, sub->file, sub->line);
2496 20802           SPVM_OP_insert_child(compiler, op_list_statement, op_list_statement->last, op_list_tmp_mys);
2497 20802           sub->op_list_tmp_mys = op_list_tmp_mys;
2498             }
2499            
2500             // 4. Add return to last of statement
2501             {
2502 20802           SPVM_OP* op_return = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_RETURN, op_list_statement->file, op_list_statement->last->line + 1);
2503 20802           SPVM_TYPE* return_type = sub->return_type;
2504 20802 100         if (SPVM_TYPE_is_void_type(compiler, return_type->basic_type->id, return_type->dimension, return_type->flag)) {
2505 1006           SPVM_OP_insert_child(compiler, op_list_statement, op_list_statement->last, op_return);
2506             }
2507             else {
2508             // Return variable name
2509 19796           char* name = "@return";
2510 19796           SPVM_OP* op_name = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_NAME, op_list_statement->file, op_list_statement->last->line + 1);
2511 19796           op_name->uv.name = name;
2512 19796           SPVM_OP* op_var = SPVM_OP_build_var(compiler, op_name);
2513 19796           SPVM_MY* my = SPVM_MY_new(compiler);
2514 19796           SPVM_OP* op_my = SPVM_OP_new_op_my(compiler, my, op_list_statement->file, op_list_statement->last->line + 1);
2515 19796           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, return_type, op_list_statement->file, op_list_statement->last->line + 1);
2516 19796           op_var = SPVM_OP_build_my(compiler, op_my, op_var, op_type);
2517 19796           SPVM_OP_insert_child(compiler, op_return, op_return->last, op_var);
2518 19796           SPVM_OP_insert_child(compiler, op_list_statement, op_list_statement->last, op_return);
2519             }
2520             }
2521             }
2522            
2523             // Save block
2524 34675           sub->op_block = op_block;
2525            
2526 34675           sub->op_sub = op_sub;
2527            
2528 34675           op_sub->uv.sub = sub;
2529            
2530 34675           return op_sub;
2531             }
2532              
2533 58           SPVM_OP* SPVM_OP_build_enumeration_value(SPVM_COMPILER* compiler, SPVM_OP* op_name, SPVM_OP* op_constant) {
2534            
2535 58 100         if (op_constant) {
2536            
2537 21           SPVM_CONSTANT* constant = op_constant->uv.constant;
2538            
2539 21 50         if (constant->type->dimension == 0 && constant->type->basic_type->id == SPVM_BASIC_TYPE_C_ID_INT) {
    50          
2540 21           compiler->current_enum_value = constant->value.ival;
2541             }
2542             else {
2543 0           SPVM_COMPILER_error(compiler, "enum value must be int type at %s line %d\n", op_constant->file, op_constant->line);
2544             }
2545            
2546 21           compiler->current_enum_value++;
2547             }
2548             else {
2549 37           op_constant = SPVM_OP_new_op_constant_int(compiler, (int32_t)compiler->current_enum_value, op_name->file, op_name->line);
2550            
2551 37           compiler->current_enum_value++;
2552             }
2553            
2554             // Return
2555 58           SPVM_OP* op_return = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_RETURN, op_name->file, op_name->line);
2556 58           SPVM_OP_insert_child(compiler, op_return, op_return->last, op_constant);
2557            
2558             // Statements
2559 58           SPVM_OP* op_list_statements = SPVM_OP_new_op_list(compiler, op_name->file, op_name->line);
2560 58           SPVM_OP_insert_child(compiler, op_list_statements, op_list_statements->last, op_return);
2561              
2562             // Block
2563 58           SPVM_OP* op_block = SPVM_OP_new_op_block(compiler, op_name->file, op_name->line);
2564 58           SPVM_OP_insert_child(compiler, op_block, op_block->last, op_list_statements);
2565            
2566             // sub
2567 58           SPVM_OP* op_sub = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_SUB, op_name->file, op_name->line);
2568 58           op_sub->file = op_name->file;
2569 58           op_sub->line = op_name->line;
2570            
2571             // Type
2572 58           SPVM_OP* op_return_type = SPVM_OP_new_op_type(compiler, op_constant->uv.constant->type, op_name->file, op_name->line);
2573            
2574             // Build subroutine
2575 58           op_sub = SPVM_OP_build_sub(compiler, op_sub, op_name, op_return_type, NULL, NULL, op_block, NULL, NULL, 0);
2576            
2577             // Set constant
2578 58           op_sub->uv.sub->op_inline = op_constant;
2579            
2580             // Subroutine is constant
2581 58           op_sub->uv.sub->flag |= SPVM_SUB_C_FLAG_ENUM;
2582 58           op_sub->uv.sub->call_type_id = SPVM_SUB_C_CALL_TYPE_ID_CLASS_METHOD;
2583            
2584 58           return op_sub;
2585             }
2586              
2587 19           SPVM_OP* SPVM_OP_build_enumeration(SPVM_COMPILER* compiler, SPVM_OP* op_enumeration, SPVM_OP* op_enumeration_block, SPVM_OP* op_descriptors) {
2588            
2589 19           SPVM_OP_insert_child(compiler, op_enumeration, op_enumeration->last, op_enumeration_block);
2590            
2591 19           SPVM_OP* op_enumeration_values = op_enumeration_block->first;
2592 19           SPVM_OP* op_sub = op_enumeration_values->first;
2593 77 100         while ((op_sub = SPVM_OP_sibling(compiler, op_sub))) {
2594 58           SPVM_SUB* sub = op_sub->uv.sub;
2595              
2596             // Descriptors
2597 58           int32_t access_control_descriptors_count = 0;
2598 58 50         if (op_descriptors) {
2599 58           SPVM_OP* op_descriptor = op_descriptors->first;
2600 64 100         while ((op_descriptor = SPVM_OP_sibling(compiler, op_descriptor))) {
2601 6           SPVM_DESCRIPTOR* descriptor = op_descriptor->uv.descriptor;
2602            
2603 6           switch (descriptor->id) {
2604             case SPVM_DESCRIPTOR_C_ID_PRIVATE:
2605 6           sub->flag |= SPVM_SUB_C_FLAG_PRIVATE;
2606 6           access_control_descriptors_count++;
2607 6           break;
2608             case SPVM_DESCRIPTOR_C_ID_PUBLIC:
2609             // Default is public
2610 0           access_control_descriptors_count++;
2611 0           break;
2612             default:
2613 0           SPVM_COMPILER_error(compiler, "invalid subroutine descriptor %s", SPVM_DESCRIPTOR_C_ID_NAMES[descriptor->id], op_descriptors->file, op_descriptors->line);
2614             }
2615             }
2616 58 50         if (access_control_descriptors_count > 1) {
2617 0           SPVM_COMPILER_error(compiler, "public, private can be specifed only one in sub declaration at %s line %d\n", op_sub->file, op_sub->line);
2618             }
2619             }
2620             }
2621            
2622             // Reset enum information
2623 19           compiler->current_enum_value = 0;
2624            
2625 19           return op_enumeration;
2626             }
2627              
2628 54433           SPVM_OP* SPVM_OP_build_arg(SPVM_COMPILER* compiler, SPVM_OP* op_var, SPVM_OP* op_type) {
2629            
2630 54433           SPVM_MY* my = SPVM_MY_new(compiler);
2631 54433           SPVM_OP* op_my = SPVM_OP_new_op_my(compiler, my, op_var->file, op_var->line);
2632            
2633 54433           op_var = SPVM_OP_build_my(compiler, op_my, op_var, op_type);
2634            
2635 54433           op_var->uv.var->is_arg = 1;
2636            
2637 54433           return op_var;
2638             }
2639              
2640 591935           SPVM_OP* SPVM_OP_build_my(SPVM_COMPILER* compiler, SPVM_OP* op_my, SPVM_OP* op_var, SPVM_OP* op_type) {
2641            
2642             // Declaration
2643 591935           op_var->uv.var->is_declaration = 1;
2644            
2645             // Create my var information
2646 591935           SPVM_MY* my = op_my->uv.my;
2647 591935 100         if (op_type) {
2648 546978           my->type = op_type->uv.type;
2649             }
2650            
2651             // Name OP
2652 591935           SPVM_OP* op_name = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_NAME, op_var->file, op_var->line);
2653 591935           op_name->uv.name = op_var->uv.var->op_name->uv.name;
2654 591935           my->op_name = op_name;
2655            
2656 591935           op_var->uv.var->my = my;
2657              
2658 591935           return op_var;
2659             }
2660              
2661 10031           SPVM_OP* SPVM_OP_build_call_sub(SPVM_COMPILER* compiler, SPVM_OP* op_invocant, SPVM_OP* op_name_sub, SPVM_OP* op_list_terms) {
2662            
2663             // Build OP_SUB
2664 10031           SPVM_OP* op_call_sub = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_CALL_SUB, op_name_sub->file, op_name_sub->line);
2665 10031           SPVM_OP_insert_child(compiler, op_call_sub, op_call_sub->last, op_list_terms);
2666            
2667 10031           SPVM_CALL_SUB* call_sub = SPVM_CALL_SUB_new(compiler);
2668            
2669 10031           const char* sub_name = op_name_sub->uv.name;
2670            
2671 10031 50         if (strstr(sub_name, "::")) {
2672 0           SPVM_COMPILER_error(compiler, "subroutine name can't conatin :: at %s line %d\n", op_name_sub->file, op_name_sub->line);
2673             }
2674            
2675             // Method call
2676 10031 100         if (op_invocant && op_invocant->id != SPVM_OP_C_ID_TYPE) {
    100          
2677 1324           call_sub->call_type_id = SPVM_SUB_C_CALL_TYPE_ID_METHOD;
2678 1324           call_sub->op_invocant = op_invocant;
2679 1324           call_sub->op_name = op_name_sub;
2680            
2681 1324 100         if (op_invocant->id == SPVM_OP_C_ID_VAR) {
2682 1270           op_invocant->uv.var->call_sub = call_sub;
2683             }
2684            
2685 1324           SPVM_OP_insert_child(compiler, op_list_terms, op_list_terms->first, op_invocant);
2686             }
2687             // Class method call
2688             else {
2689 8707           call_sub->call_type_id = SPVM_SUB_C_CALL_TYPE_ID_CLASS_METHOD;
2690 8707           call_sub->op_invocant = op_invocant;
2691 8707           call_sub->op_name = op_name_sub;
2692             }
2693            
2694             // term is passed to subroutine
2695 10031           SPVM_OP* op_term = op_list_terms->first;
2696 22506 100         while ((op_term = SPVM_OP_sibling(compiler, op_term))) {
2697 12475           op_term->is_passed_to_sub = 1;
2698             }
2699            
2700 10031           op_call_sub->uv.call_sub = call_sub;
2701            
2702 10031           return op_call_sub;
2703             }
2704              
2705 200           SPVM_OP* SPVM_OP_build_unary_op(SPVM_COMPILER* compiler, SPVM_OP* op_unary, SPVM_OP* op_first) {
2706            
2707             // Build op
2708 200           SPVM_OP_insert_child(compiler, op_unary, op_unary->last, op_first);
2709            
2710 200           return op_unary;
2711             }
2712              
2713 44652           SPVM_OP* SPVM_OP_build_comparison_op(SPVM_COMPILER* compiler, SPVM_OP* op_comparison, SPVM_OP* op_first, SPVM_OP* op_last) {
2714              
2715 44652           SPVM_OP_insert_child(compiler, op_comparison, op_comparison->last, op_first);
2716 44652           SPVM_OP_insert_child(compiler, op_comparison, op_comparison->last, op_last);
2717            
2718 44652           SPVM_OP* op_name_var = SPVM_OP_new_op_name(compiler, "@condition_flag", op_comparison->file, op_comparison->line);
2719 44652           SPVM_OP* op_var = SPVM_OP_new_op_var(compiler, op_name_var);
2720 44652           SPVM_OP* op_assign = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_comparison->file, op_comparison->line);
2721 44652           SPVM_OP_build_assign(compiler, op_assign, op_var, op_comparison);
2722            
2723 44652           return op_assign;
2724             }
2725              
2726 33811           SPVM_OP* SPVM_OP_build_binary_op(SPVM_COMPILER* compiler, SPVM_OP* op_bin, SPVM_OP* op_first, SPVM_OP* op_last) {
2727            
2728             // Build op
2729 33811           SPVM_OP_insert_child(compiler, op_bin, op_bin->last, op_first);
2730 33811           SPVM_OP_insert_child(compiler, op_bin, op_bin->last, op_last);
2731            
2732 33811           return op_bin;
2733             }
2734              
2735 13675           SPVM_OP* SPVM_OP_build_inc(SPVM_COMPILER* compiler, SPVM_OP* op_inc, SPVM_OP* op_first) {
2736            
2737             // Build op
2738 13675           SPVM_OP_insert_child(compiler, op_inc, op_inc->last, op_first);
2739              
2740 13675 50         if (!SPVM_OP_is_mutable(compiler, op_first)) {
2741 0           SPVM_COMPILER_error(compiler, "Operand of ++ operator must be mutable at %s line %d\n", op_first->file, op_first->line);
2742             }
2743            
2744 13675           return op_inc;
2745             }
2746              
2747 563           SPVM_OP* SPVM_OP_build_dec(SPVM_COMPILER* compiler, SPVM_OP* op_dec, SPVM_OP* op_first) {
2748            
2749             // Build op
2750 563           SPVM_OP_insert_child(compiler, op_dec, op_dec->last, op_first);
2751              
2752 563 50         if (!SPVM_OP_is_mutable(compiler, op_first)) {
2753 0           SPVM_COMPILER_error(compiler, "Operand of -- operator must be mutable at %s line %d\n", op_first->file, op_first->line);
2754             }
2755            
2756 563           return op_dec;
2757             }
2758              
2759              
2760 459           SPVM_OP* SPVM_OP_build_isa(SPVM_COMPILER* compiler, SPVM_OP* op_isa, SPVM_OP* op_term, SPVM_OP* op_type) {
2761            
2762             // Build op
2763 459           SPVM_OP_insert_child(compiler, op_isa, op_isa->last, op_term);
2764 459           SPVM_OP_insert_child(compiler, op_isa, op_isa->last, op_type);
2765              
2766 459           SPVM_OP* op_name_var = SPVM_OP_new_op_name(compiler, "@condition_flag", op_isa->file, op_isa->line);
2767 459           SPVM_OP* op_var = SPVM_OP_new_op_var(compiler, op_name_var);
2768 459           SPVM_OP* op_assign = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_isa->file, op_isa->line);
2769 459           SPVM_OP_build_assign(compiler, op_assign, op_var, op_isa);
2770            
2771 459           return op_assign;
2772             }
2773              
2774 4928           SPVM_OP* SPVM_OP_build_concat(SPVM_COMPILER* compiler, SPVM_OP* op_cancat, SPVM_OP* op_first, SPVM_OP* op_last) {
2775            
2776             // Build op
2777 4928           SPVM_OP_insert_child(compiler, op_cancat, op_cancat->last, op_first);
2778 4928           SPVM_OP_insert_child(compiler, op_cancat, op_cancat->last, op_last);
2779            
2780 4928           return op_cancat;
2781             }
2782              
2783 3253           SPVM_OP* SPVM_OP_build_and(SPVM_COMPILER* compiler, SPVM_OP* op_and, SPVM_OP* op_first, SPVM_OP* op_last) {
2784            
2785             // Convert && to if statement
2786             /* before
2787             AND
2788             first
2789             last
2790             */
2791            
2792             /* after
2793             IF if1
2794             CONDITION
2795             first
2796             IF if2
2797             CONDITION
2798             last
2799             BOOL
2800             1 true1
2801             BOOL
2802             0 false1
2803             BOOL
2804             0 false2
2805             */
2806            
2807 3253           SPVM_OP* op_if1 = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_IF, op_and->file, op_and->line);
2808            
2809             // Constant true
2810 3253           SPVM_OP* op_constant_true = SPVM_OP_new_op_constant_int(compiler, 1, op_if1->file, op_if1->line);
2811 3253           SPVM_OP* op_assign_bool_true = SPVM_OP_new_op_assign_bool(compiler, op_constant_true, op_if1->file, op_if1->line);
2812            
2813             // Constant false 1
2814 3253           SPVM_OP* op_constant_false1 = SPVM_OP_new_op_constant_int(compiler, 0, op_if1->file, op_if1->line);
2815 3253           SPVM_OP* op_assign_bool_false1 = SPVM_OP_new_op_assign_bool(compiler, op_constant_false1, op_if1->file, op_if1->line);
2816            
2817             // Constant false 2
2818 3253           SPVM_OP* op_constant_false2 = SPVM_OP_new_op_constant_int(compiler, 0, op_if1->file, op_if1->line);
2819 3253           SPVM_OP* op_assign_bool_false2 = SPVM_OP_new_op_assign_bool(compiler, op_constant_false2, op_if1->file, op_if1->line);
2820            
2821             // if2
2822 3253           SPVM_OP* op_if2 = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_IF, op_if1->file, op_if1->line);
2823            
2824             // Build if tree
2825 3253           SPVM_OP_build_if_statement(compiler, op_if2, op_last, op_assign_bool_true, op_assign_bool_false1);
2826 3253           SPVM_OP_build_if_statement(compiler, op_if1, op_first, op_if2, op_assign_bool_false2);
2827              
2828 3253           SPVM_OP* op_name_var = SPVM_OP_new_op_name(compiler, "@condition_flag", op_and->file, op_and->line);
2829 3253           SPVM_OP* op_var = SPVM_OP_new_op_var(compiler, op_name_var);
2830 3253           SPVM_OP* op_assign = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_and->file, op_and->line);
2831 3253           SPVM_OP_build_assign(compiler, op_assign, op_var, op_if1);
2832            
2833 3253           return op_assign;
2834             }
2835              
2836 2208           SPVM_OP* SPVM_OP_build_or(SPVM_COMPILER* compiler, SPVM_OP* op_or, SPVM_OP* op_first, SPVM_OP* op_last) {
2837            
2838             // Convert || to if statement
2839             // before
2840             // OR
2841             // first
2842             // last
2843            
2844             // after
2845             // IF if1
2846             // first
2847             // BOOL
2848             // 1 true1
2849             // IF if2
2850             // last
2851             // BOOL
2852             // 1 true2
2853             // BOOL
2854             // 0 false
2855            
2856 2208           SPVM_OP* op_if1 = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_IF, op_or->file, op_or->line);
2857            
2858             // Constant true 1
2859 2208           SPVM_OP* op_constant_true1 = SPVM_OP_new_op_constant_int(compiler, 1, op_if1->file, op_if1->line);
2860 2208           SPVM_OP* op_bool_true1 = SPVM_OP_new_op_assign_bool(compiler, op_constant_true1, op_if1->file, op_if1->line);
2861            
2862             // Constant true 2
2863 2208           SPVM_OP* op_constant_true2 = SPVM_OP_new_op_constant_int(compiler, 1, op_if1->file, op_if1->line);
2864 2208           SPVM_OP* op_bool_true2 = SPVM_OP_new_op_assign_bool(compiler, op_constant_true2, op_if1->file, op_if1->line);
2865            
2866             // Constant false
2867 2208           SPVM_OP* op_constant_false = SPVM_OP_new_op_constant_int(compiler, 0, op_if1->file, op_if1->line);
2868 2208           SPVM_OP* op_bool_false = SPVM_OP_new_op_assign_bool(compiler, op_constant_false, op_if1->file, op_if1->line);
2869            
2870             // if2
2871 2208           SPVM_OP* op_if2 = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_IF, op_if1->file, op_if1->line);
2872            
2873             // Build if tree
2874 2208           SPVM_OP_build_if_statement(compiler, op_if2, op_last, op_bool_true2, op_bool_false);
2875 2208           SPVM_OP_build_if_statement(compiler, op_if1, op_first, op_bool_true1, op_if2);
2876            
2877 2208           SPVM_OP* op_name_var = SPVM_OP_new_op_name(compiler, "@condition_flag", op_or->file, op_or->line);
2878 2208           SPVM_OP* op_var = SPVM_OP_new_op_var(compiler, op_name_var);
2879 2208           SPVM_OP* op_assign = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_or->file, op_or->line);
2880 2208           SPVM_OP_build_assign(compiler, op_assign, op_var, op_if1);
2881            
2882 2208           return op_assign;
2883             }
2884              
2885 601           SPVM_OP* SPVM_OP_build_not(SPVM_COMPILER* compiler, SPVM_OP* op_not, SPVM_OP* op_first) {
2886            
2887             // Convert ! to if statement
2888             // before
2889             // LOGICAL_NOT
2890             // first
2891            
2892             // after
2893             // IF
2894             // first
2895             // BOOL
2896             // 0
2897             // BOOL
2898             // 1
2899            
2900 601           SPVM_OP* op_if = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_IF, op_not->file, op_not->line);
2901            
2902             // Constant false
2903 601           SPVM_OP* op_constant_false = SPVM_OP_new_op_constant_int(compiler, 0, op_if->file, op_if->line);
2904 601           SPVM_OP* op_assign_bool_false = SPVM_OP_new_op_assign_bool(compiler, op_constant_false, op_if->file, op_if->line);
2905              
2906             // Constant true
2907 601           SPVM_OP* op_constant_true = SPVM_OP_new_op_constant_int(compiler, 1, op_if->file, op_if->line);
2908 601           SPVM_OP* op_assign_bool_true = SPVM_OP_new_op_assign_bool(compiler, op_constant_true, op_if->file, op_if->line);
2909            
2910             // Build if tree
2911 601           SPVM_OP_build_if_statement(compiler, op_if, op_first, op_assign_bool_false, op_assign_bool_true);
2912              
2913 601           SPVM_OP* op_name_var = SPVM_OP_new_op_name(compiler, "@condition_flag", op_not->file, op_not->line);
2914 601           SPVM_OP* op_var = SPVM_OP_new_op_var(compiler, op_name_var);
2915 601           SPVM_OP* op_assign = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_not->file, op_not->line);
2916 601           SPVM_OP_build_assign(compiler, op_assign, op_var, op_if);
2917            
2918 601           return op_assign;
2919             }
2920              
2921 3783           SPVM_OP* SPVM_OP_build_special_assign(SPVM_COMPILER* compiler, SPVM_OP* op_special_assign, SPVM_OP* op_term_dist, SPVM_OP* op_term_src) {
2922            
2923 3783           SPVM_OP_insert_child(compiler, op_special_assign, op_special_assign->last, op_term_src);
2924 3783           SPVM_OP_insert_child(compiler, op_special_assign, op_special_assign->last, op_term_dist);
2925            
2926 3783 50         if (!SPVM_OP_is_mutable(compiler, op_term_dist)) {
2927 0           SPVM_COMPILER_error(compiler, "special assign operator left value must be mutable at %s line %d\n", op_term_dist->file, op_term_dist->line);
2928             }
2929            
2930 3783           return op_special_assign;
2931             }
2932              
2933 671647           SPVM_OP* SPVM_OP_build_assign(SPVM_COMPILER* compiler, SPVM_OP* op_assign, SPVM_OP* op_term_dist, SPVM_OP* op_term_src) {
2934            
2935             // Build op
2936             // Exchange left and right for excecution order
2937            
2938 671647           SPVM_OP_insert_child(compiler, op_assign, op_assign->last, op_term_src);
2939 671647           SPVM_OP_insert_child(compiler, op_assign, op_assign->last, op_term_dist);
2940            
2941 671647           op_term_dist->is_lvalue = 1;
2942            
2943 671647 50         if (!SPVM_OP_is_mutable(compiler, op_term_dist)) {
2944 0           SPVM_COMPILER_error(compiler, "assign operator left value must be mutable at %s line %d\n", op_term_dist->file, op_term_dist->line);
2945             }
2946            
2947 671647           return op_assign;
2948             }
2949              
2950 24602           SPVM_OP* SPVM_OP_build_return(SPVM_COMPILER* compiler, SPVM_OP* op_return, SPVM_OP* op_term) {
2951            
2952 24602 100         if (op_term) {
2953 24581           SPVM_OP_insert_child(compiler, op_return, op_return->last, op_term);
2954             }
2955            
2956 24602           return op_return;
2957             }
2958              
2959 68108           SPVM_OP* SPVM_OP_build_expression_statement(SPVM_COMPILER* compiler, SPVM_OP* op_expression) {
2960            
2961             // Free tmp vars at end of expression statement
2962 68108           SPVM_OP* op_free_tmp = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_FREE_TMP, op_expression->file, op_expression->line);
2963 68108           SPVM_OP_insert_child(compiler, op_free_tmp, op_free_tmp->last, op_expression);
2964            
2965 68108           return op_free_tmp;
2966             }
2967              
2968 29           SPVM_OP* SPVM_OP_build_refcnt(SPVM_COMPILER* compiler, SPVM_OP* op_refcnt, SPVM_OP* op_term) {
2969            
2970 29           SPVM_OP_insert_child(compiler, op_refcnt, op_refcnt->last, op_term);
2971            
2972 29           return op_refcnt;
2973             }
2974              
2975 10858           SPVM_OP* SPVM_OP_build_die(SPVM_COMPILER* compiler, SPVM_OP* op_die, SPVM_OP* op_term) {
2976            
2977 10858 50         if (!op_term) {
2978             // Default error message
2979 0           op_term = SPVM_OP_new_op_constant_string(compiler, "Error", strlen("Error"), op_die->file, op_die->line);;
2980             }
2981            
2982             // Exception variable
2983 10858           SPVM_OP* op_exception_var = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_EXCEPTION_VAR, op_term->file, op_term->line);
2984            
2985             // Assign
2986 10858           SPVM_OP* op_assign = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_term->file, op_term->line);
2987 10858           SPVM_OP_build_assign(compiler, op_assign, op_exception_var, op_term);
2988            
2989 10858           SPVM_OP_insert_child(compiler, op_die, op_die->last, op_assign);
2990              
2991             // Free tmp vars at end of die statement
2992 10858           SPVM_OP* op_free_tmp = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_FREE_TMP, op_die->file, op_die->line);
2993 10858           SPVM_OP_insert_child(compiler, op_free_tmp, op_free_tmp->last, op_die);
2994            
2995 10858           return op_free_tmp;
2996             }
2997              
2998 21357           SPVM_OP* SPVM_OP_build_basic_type(SPVM_COMPILER* compiler, SPVM_OP* op_name) {
2999            
3000 21357           const char* name = op_name->uv.name;
3001            
3002             // Type op
3003 21357           SPVM_TYPE* type = SPVM_TYPE_new(compiler);
3004 21357           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type, op_name->file, op_name->line);
3005 21357           SPVM_OP_insert_child(compiler, op_type, op_type->last, op_name);
3006            
3007             // Add basic type
3008 21357           SPVM_BASIC_TYPE* found_basic_type = SPVM_HASH_fetch(compiler->basic_type_symtable, name, strlen(name));
3009 21357 100         if (found_basic_type) {
3010 19929           type->basic_type = found_basic_type;
3011             }
3012             else {
3013 1428           SPVM_BASIC_TYPE* new_basic_type = SPVM_BASIC_TYPE_new(compiler);
3014 1428           new_basic_type->id = compiler->basic_types->length;
3015 1428           new_basic_type->name = name;
3016 1428           SPVM_LIST_push(compiler->basic_types, new_basic_type);
3017 1428           SPVM_HASH_insert(compiler->basic_type_symtable, new_basic_type->name, strlen(new_basic_type->name), new_basic_type);
3018 1428           type->basic_type = new_basic_type;
3019             }
3020            
3021 21357           type->op_type = op_type;
3022            
3023 21357           return op_type;
3024             }
3025              
3026 584           SPVM_OP* SPVM_OP_build_ref_type(SPVM_COMPILER* compiler, SPVM_OP* op_type_original) {
3027            
3028             // Type
3029 584           SPVM_TYPE* type = SPVM_TYPE_new(compiler);
3030 584           type->basic_type = op_type_original->uv.type->basic_type;
3031 584           type->dimension = op_type_original->uv.type->dimension;
3032 584           type->flag |= SPVM_TYPE_C_FLAG_REF;
3033            
3034             // Type OP
3035 584           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type, op_type_original->file, op_type_original->line);
3036 584           SPVM_OP_insert_child(compiler, op_type, op_type->last, op_type_original);
3037              
3038 584           type->op_type = op_type;
3039            
3040 584           return op_type;
3041             }
3042              
3043 29071           SPVM_OP* SPVM_OP_build_array_type(SPVM_COMPILER* compiler, SPVM_OP* op_type_child, SPVM_OP* op_term_length) {
3044            
3045             // Type
3046 29071           SPVM_TYPE* type = SPVM_TYPE_new(compiler);
3047 29071           type->dimension = op_type_child->uv.type->dimension + 1;
3048 29071           type->basic_type = op_type_child->uv.type->basic_type;
3049            
3050             // Type OP
3051 29071           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type, op_type_child->file, op_type_child->line);
3052 29071           SPVM_OP_insert_child(compiler, op_type, op_type->last, op_type_child);
3053            
3054 29071 100         if (op_term_length) {
3055 5957           SPVM_OP_insert_child(compiler, op_type, op_type->last, op_term_length);
3056             }
3057             else {
3058 23114           SPVM_OP* op_null = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_NULL, op_type_child->file, op_type_child->line);
3059 23114           SPVM_OP_insert_child(compiler, op_type, op_type->last, op_null);
3060             }
3061              
3062 29071           return op_type;
3063             }
3064              
3065 227383           SPVM_OP* SPVM_OP_new_op_list(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3066            
3067 227383           SPVM_OP* op_pushmark = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_PUSHMARK, file, line);
3068            
3069 227383           SPVM_OP* op_list = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_LIST, file, line);
3070 227383           SPVM_OP_insert_child(compiler, op_list, op_list->last, op_pushmark);
3071            
3072 227383           return op_list;
3073             }
3074              
3075              
3076 8722272           SPVM_OP* SPVM_OP_new_op(SPVM_COMPILER* compiler, int32_t id, const char* file, int32_t line) {
3077              
3078 8722272           SPVM_OP *op = SPVM_COMPILER_ALLOCATOR_safe_malloc_zero(compiler, sizeof(SPVM_OP));
3079            
3080 8722272           memset(op, 0, sizeof(SPVM_OP));
3081            
3082 8722272           op->id = id;
3083            
3084 8722272 50         assert(!op->file);
3085 8722272 50         assert(!op->line);
3086            
3087 8722272           op->file = file;
3088 8722272           op->line = line;
3089            
3090 8722272           return op;
3091             }
3092              
3093             // Insert child. Child must not have sibling.
3094 3186576           void SPVM_OP_insert_child(SPVM_COMPILER* compiler, SPVM_OP* parent, SPVM_OP* start, SPVM_OP* insert) {
3095             (void)compiler;
3096            
3097             // del_count not used
3098 3186576 50         assert(parent);
3099 3186576 50         assert(insert);
3100 3186576 50         assert(insert->moresib == 0);
3101            
3102 3186576 100         if (start) {
3103 1428726 100         if (start->moresib) {
3104 51695           insert->moresib = 1;
3105 51695           insert->sibparent = start->sibparent;
3106            
3107 51695           start->sibparent = insert;
3108             }
3109             else {
3110 1377031           parent->last = insert;
3111              
3112 1377031           insert->moresib = 0;
3113 1377031           insert->sibparent = parent;
3114            
3115 1377031           start->moresib = 1;
3116 1428726           start->sibparent = insert;
3117             }
3118             }
3119             else {
3120 1757850 50         if (parent->first) {
3121 0           insert->moresib = 1;
3122 0           insert->sibparent = parent->first;
3123            
3124 0           parent->first = insert;
3125             }
3126             else {
3127 1757850           insert->moresib = 0;
3128 1757850           insert->sibparent = parent;
3129            
3130 1757850           parent->first = insert;
3131 1757850           parent->last = insert;
3132             }
3133             }
3134 3186576           }
3135              
3136 6764404           SPVM_OP* SPVM_OP_sibling(SPVM_COMPILER* compiler, SPVM_OP* op) {
3137             (void)compiler;
3138            
3139 6764404 100         return op->moresib ? op->sibparent : NULL;
3140             }
3141              
3142 0           void SPVM_OP_insert_to_most_left_deep_child(SPVM_COMPILER* compiler, SPVM_OP* op_parent, SPVM_OP* op_child) {
3143            
3144 0 0         assert(op_parent);
3145 0 0         assert(op_parent->first);
3146            
3147 0           SPVM_OP* op_most_left_deep_child_of_parent = op_parent;
3148            
3149             while (1) {
3150 0 0         if (op_most_left_deep_child_of_parent->first) {
3151 0           op_most_left_deep_child_of_parent = op_most_left_deep_child_of_parent->first;
3152 0           continue;
3153             }
3154             else {
3155 0           break;
3156             }
3157 0           }
3158            
3159 0           SPVM_OP_insert_child(compiler, op_most_left_deep_child_of_parent, op_most_left_deep_child_of_parent->last, op_child);
3160 0           }
3161              
3162 39320           int32_t SPVM_OP_get_list_elements_count(SPVM_COMPILER* compiler, SPVM_OP* op_list) {
3163            
3164 39320           int32_t count = 0;
3165 39320           SPVM_OP* op_term = op_list->first;
3166 373380 100         while ((op_term = SPVM_OP_sibling(compiler, op_term))) {
3167 334060           count++;
3168             }
3169            
3170 39320           return count;
3171             }