File Coverage

lib/SPVM/Builder/src/spvm_op.c
Criterion Covered Total %
statement 1802 1875 96.1
branch 446 488 91.3
condition n/a
subroutine n/a
pod n/a
total 2248 2363 95.1


line stmt bran cond sub pod time code
1             // Copyright (c) 2023 Yuki Kimoto
2             // MIT License
3              
4             #include
5             #include
6             #include
7             #include
8             #include
9             #include
10             #include
11             #include "spvm_compiler.h"
12             #include "spvm_list.h"
13             #include "spvm_hash.h"
14             #include "spvm_op.h"
15             #include "spvm_method.h"
16             #include "spvm_constant.h"
17             #include "spvm_field.h"
18             #include "spvm_var_decl.h"
19             #include "spvm_var.h"
20             #include "spvm_type.h"
21             #include "spvm_field_access.h"
22             #include "spvm_call_method.h"
23             #include "spvm_type.h"
24             #include "spvm_opcode_builder.h"
25             #include "spvm_check.h"
26             #include "spvm_switch_info.h"
27             #include "spvm_attribute.h"
28             #include "spvm_allocator.h"
29             #include "spvm_use.h"
30             #include "spvm_class_var.h"
31             #include "spvm_class_var_access.h"
32             #include "spvm_precompile.h"
33             #include "spvm_block.h"
34             #include "spvm_basic_type.h"
35             #include "spvm_case_info.h"
36             #include "spvm_array_field_access.h"
37             #include "spvm_string_buffer.h"
38             #include "spvm_allow.h"
39             #include "spvm_interface.h"
40             #include "spvm_string.h"
41             #include "spvm_dumper.h"
42              
43              
44              
45              
46              
47              
48              
49              
50              
51              
52              
53              
54              
55              
56              
57              
58              
59              
60              
61              
62              
63              
64              
65              
66              
67              
68              
69              
70              
71              
72              
73              
74              
75              
76              
77              
78              
79              
80              
81              
82              
83              
84              
85              
86              
87              
88              
89              
90              
91              
92              
93              
94              
95              
96 7630           const char* const* SPVM_OP_C_ID_NAMES(void) {
97              
98             static const char* const id_names[] = {
99             "GRAMMAR",
100             "LIST",
101             "PUSHMARK",
102             "DO_NOTHING",
103             "NAME",
104             "ATTRIBUTE",
105             "CURRENT_CLASS",
106             "CLASS",
107             "EXTENDS",
108             "CLASS_BLOCK",
109             "END_OF_FILE",
110             "IF",
111             "UNLESS",
112             "ELSIF",
113             "ELSE",
114             "CONDITION",
115             "CONDITION_NOT",
116             "SWITCH",
117             "CASE",
118             "DEFAULT",
119             "SWITCH_CONDITION",
120             "BREAK",
121             "FOR",
122             "WHILE",
123             "LOOP",
124             "LOOP_INCREMENT",
125             "LAST",
126             "NEXT",
127             "MY",
128             "FIELD",
129             "METHOD",
130             "ENUM",
131             "ENUMERATION_VALUE",
132             "ENUM_BLOCK",
133             "BLOCK",
134             "EVAL",
135             "TYPE",
136             "MUTABLE",
137             "VOID",
138             "BYTE",
139             "SHORT",
140             "INT",
141             "LONG",
142             "FLOAT",
143             "DOUBLE",
144             "STRING",
145             "OBJECT",
146             "DOT3",
147             "OF",
148             "USE",
149             "AS",
150             "ALIAS",
151             "REQUIRE",
152             "IF_REQUIRE",
153             "INIT",
154             "INTERFACE",
155             "RETURN",
156             "CONSTANT",
157             "INC",
158             "DEC",
159             "PRE_INC",
160             "POST_INC",
161             "PRE_DEC",
162             "POST_DEC",
163             "MINUS",
164             "PLUS",
165             "ADD",
166             "SUBTRACT",
167             "MULTIPLY",
168             "DIVIDE",
169             "BIT_AND",
170             "BIT_OR",
171             "BIT_XOR",
172             "BIT_NOT",
173             "REMAINDER",
174             "LEFT_SHIFT",
175             "RIGHT_ARITHMETIC_SHIFT",
176             "RIGHT_LOGICAL_SHIFT",
177             "LOGICAL_AND",
178             "LOGICAL_OR",
179             "LOGICAL_NOT",
180             "ARRAY_ACCESS",
181             "ASSIGN",
182             "CALL_METHOD",
183             "FIELD_ACCESS",
184             "VAR",
185             "TYPE_CAST",
186             "UNDEF",
187             "ARRAY_LENGTH",
188             "DIE",
189             "EXCEPTION_VAR",
190             "NEW",
191             "WEAKEN",
192             "WEAKEN_FIELD",
193             "UNWEAKEN",
194             "UNWEAKEN_FIELD",
195             "ISWEAK",
196             "ISWEAK_FIELD",
197             "SPECIAL_ASSIGN",
198             "CONCAT",
199             "CLASS_VAR",
200             "CLASS_VAR_ACCESS",
201             "ARRAY_INIT",
202             "BOOL",
203             "CHECK_CONVERT",
204             "NUMERIC_EQ",
205             "NUMERIC_NE",
206             "NUMERIC_LT",
207             "NUMERIC_LE",
208             "NUMERIC_GT",
209             "NUMERIC_GE",
210             "NUMERIC_CMP",
211             "STRING_EQ",
212             "STRING_NE",
213             "STRING_GT",
214             "STRING_GE",
215             "STRING_LT",
216             "STRING_LE",
217             "STRING_CMP",
218             "ISA",
219             "ISA_ERROR",
220             "IS_TYPE",
221             "IS_ERROR",
222             "IS_COMPILE_TYPE",
223             "SEQUENCE",
224             "SCALAR",
225             "ARRAY_FIELD_ACCESS",
226             "REFERENCE",
227             "DEREF",
228             "STRING_LENGTH",
229             "CURRENT_CLASS_NAME",
230             "ALLOW",
231             "WARN",
232             "PRINT",
233             "SAY",
234             "TYPE_NAME",
235             "COMPILE_TYPE_NAME",
236             "DUMP",
237             "TRUE",
238             "FALSE",
239             "DIVIDE_UNSIGNED_INT",
240             "DIVIDE_UNSIGNED_LONG",
241             "REMAINDER_UNSIGNED_INT",
242             "REMAINDER_UNSIGNED_LONG",
243             "NEW_STRING_LEN",
244             "IS_READ_ONLY",
245             "MAKE_READ_ONLY",
246             "COPY",
247             "CAN",
248             "BASIC_TYPE_ID",
249             "DIE_ERROR_ID",
250             "SET_DIE_ERROR_ID",
251             "EVAL_ERROR_ID",
252             "ARGS_WIDTH",
253             "VERSION",
254             };
255            
256 7630           return id_names;
257             }
258              
259 43086           SPVM_OP* SPVM_OP_build_class(SPVM_COMPILER* compiler, SPVM_OP* op_class, SPVM_OP* op_type, SPVM_OP* op_block, SPVM_OP* op_list_attributes, SPVM_OP* op_extends) {
260            
261 43086           const char* basic_type_name = op_type->uv.type->unresolved_basic_type_name;
262            
263 43086           SPVM_TYPE* type = op_type->uv.type;
264            
265 43086           SPVM_BASIC_TYPE* basic_type = SPVM_HASH_get(compiler->basic_type_symtable, basic_type_name, strlen(basic_type_name));
266 43086 100         if (!basic_type) {
267 43085           basic_type = SPVM_COMPILER_add_basic_type(compiler, basic_type_name);
268             }
269            
270 43086           type->basic_type = basic_type;
271            
272 43086           type->basic_type->op_class = op_class;
273 43086           type->basic_type->op_extends = op_extends;
274            
275 43086           type->basic_type->class_dir = compiler->current_class_dir;
276 43086           type->basic_type->class_rel_file = compiler->current_class_rel_file;
277 43086           type->basic_type->class_file = compiler->current_file;
278            
279 43086 100         if (op_extends) {
280 3131           SPVM_OP* op_type_parent_basic_type = op_extends->first;
281            
282 3131           SPVM_OP* op_name_parent_basic_type = SPVM_OP_new_op_name(compiler, op_type_parent_basic_type->uv.type->unresolved_basic_type_name, op_type_parent_basic_type->file, op_type_parent_basic_type->line);
283            
284 3131           type->basic_type->parent_name = op_name_parent_basic_type->uv.name;
285            
286             // add use stack
287 3131           SPVM_OP* op_use = SPVM_OP_new_op_use(compiler, op_name_parent_basic_type->file, op_name_parent_basic_type->line);
288 3131           SPVM_OP* op_name_alias = NULL;
289 3131           int32_t is_require = 0;
290 3131           SPVM_OP_build_use(compiler, op_use, op_type_parent_basic_type, op_name_alias, is_require);
291             }
292            
293 43086 100         if (type->basic_type->class_dir) {
294 25074           SPVM_STRING_new(compiler, type->basic_type->class_dir, strlen(type->basic_type->class_dir));
295             }
296 43086           SPVM_STRING_new(compiler, type->basic_type->class_rel_file, strlen(type->basic_type->class_rel_file));
297 43086           SPVM_STRING_new(compiler, type->basic_type->class_file, strlen(type->basic_type->class_file));
298            
299             // Assert
300 43086           SPVM_BASIC_TYPE* found_basic_type = SPVM_HASH_get(compiler->basic_type_symtable, basic_type_name, strlen(basic_type_name));
301            
302 43086 100         if (strstr(basic_type_name, "::anon::")) {
303 8213           type->basic_type->access_control_type = SPVM_ATTRIBUTE_C_ID_PUBLIC;
304 8213           basic_type->is_anon = 1;
305             }
306            
307 43086 100         if (!basic_type->is_anon) {
308 34873 50         assert(!islower(basic_type_name[0]));
309            
310             // If class name is different from the class name corresponding to the class file, compile error occur.
311 34873 100         if (strcmp(basic_type_name, compiler->current_outer_class_name) != 0) {
312 2           SPVM_COMPILER_error(compiler, "The class name \"%s\" must be \"%s\".\n at %s line %d", basic_type_name, compiler->current_outer_class_name, op_class->file, op_class->line);
313 2           return op_class;
314             }
315            
316 34871           SPVM_LIST* anon_op_types = compiler->current_anon_op_types;
317 43083 100         for (int32_t i = 0; i < anon_op_types->length; i++) {
318 8212           SPVM_OP* anon_op_type = SPVM_LIST_get(anon_op_types, i);
319 8212           const char* anon_unresolved_basic_type_name = anon_op_type->uv.type->unresolved_basic_type_name;
320 8212           SPVM_BASIC_TYPE* anon_basic_type = SPVM_HASH_get(compiler->basic_type_symtable, anon_unresolved_basic_type_name, strlen(anon_unresolved_basic_type_name));
321 8212 50         assert(anon_basic_type);
322 8212           SPVM_LIST_push(basic_type->anon_basic_types, anon_basic_type);
323             }
324             }
325            
326             // Module attributes
327 43084           int32_t class_attributes_count = 0;
328 43084           int32_t access_control_attributes_count = 0;
329 43084 100         if (op_list_attributes) {
330 9638           SPVM_OP* op_attribute = op_list_attributes->first;
331 19280 100         while ((op_attribute = SPVM_OP_sibling(compiler, op_attribute))) {
332 9642           SPVM_ATTRIBUTE* attribute = op_attribute->uv.attribute;
333 9642           switch (attribute->id) {
334             case SPVM_ATTRIBUTE_C_ID_POINTER: {
335 3022           type->basic_type->category = SPVM_NATIVE_C_BASIC_TYPE_CATEGORY_CLASS;
336 3022           basic_type->is_pointer = 1;
337 3022           class_attributes_count++;
338 3022           break;
339             }
340             case SPVM_ATTRIBUTE_C_ID_MULNUM_T: {
341 283           type->basic_type->category = SPVM_NATIVE_C_BASIC_TYPE_CATEGORY_MULNUM;
342 283           basic_type->category = SPVM_NATIVE_C_BASIC_TYPE_CATEGORY_MULNUM;
343 283           class_attributes_count++;
344 283           break;
345             }
346             case SPVM_ATTRIBUTE_C_ID_PRIVATE: {
347 2           basic_type->access_control_type = SPVM_ATTRIBUTE_C_ID_PRIVATE;
348 2           access_control_attributes_count++;
349 2           break;
350             }
351             case SPVM_ATTRIBUTE_C_ID_PROTECTED: {
352 1           basic_type->access_control_type = SPVM_ATTRIBUTE_C_ID_PROTECTED;
353 1           access_control_attributes_count++;
354 1           break;
355             }
356             case SPVM_ATTRIBUTE_C_ID_PUBLIC: {
357 209           basic_type->access_control_type = SPVM_ATTRIBUTE_C_ID_PUBLIC;
358 209           access_control_attributes_count++;
359 209           break;
360             }
361             case SPVM_ATTRIBUTE_C_ID_PRECOMPILE: {
362 261           basic_type->is_precompile = 1;
363 261           break;
364             }
365             case SPVM_ATTRIBUTE_C_ID_INTERFACE_T: {
366 5863           type->basic_type->category = SPVM_NATIVE_C_BASIC_TYPE_CATEGORY_INTERFACE;
367 5863           basic_type->category = SPVM_NATIVE_C_BASIC_TYPE_CATEGORY_INTERFACE;
368 5863           class_attributes_count++;
369 5863           break;
370             }
371             default: {
372 1           SPVM_COMPILER_error(compiler, "Invalid class attribute \"%s\".\n at %s line %d", SPVM_ATTRIBUTE_get_name(compiler, attribute->id), op_class->file, op_class->line);
373             }
374             }
375             }
376 9638 100         if (class_attributes_count > 1) {
377 1           SPVM_COMPILER_error(compiler, "Only one of class attributes \"mulnum_t\", \"pointer\" or \"interface_t\" can be specified.\n at %s line %d", op_list_attributes->file, op_list_attributes->line);
378             }
379 9638 100         if (access_control_attributes_count > 1) {
380 1           SPVM_COMPILER_error(compiler, "Only one of class attributes \"private\", \"protected\" or \"public\" can be specified.\n at %s line %d", op_list_attributes->file, op_list_attributes->line);
381             }
382             }
383            
384 43084 100         if (basic_type->category == 0) {
385 33918           basic_type->category = SPVM_NATIVE_C_BASIC_TYPE_CATEGORY_CLASS;
386             }
387            
388             // The default of the access controll is private
389 43084 100         if (basic_type->access_control_type == SPVM_ATTRIBUTE_C_ID_UNKNOWN) {
390 34660           basic_type->access_control_type = SPVM_ATTRIBUTE_C_ID_PRIVATE;
391             }
392            
393             // Declarations
394 43084 100         if (op_block) {
395 38523           SPVM_OP* op_decls = op_block->first;
396 38523           SPVM_OP* op_decl = op_decls->first;
397 354353 100         while ((op_decl = SPVM_OP_sibling(compiler, op_decl))) {
398             // version declaration
399 315837 100         if (op_decl->id == SPVM_OP_C_ID_VERSION_DECL) {
400 206 100         if (type->basic_type->version_string) {
401 1           SPVM_COMPILER_error(compiler, "The version has already been declared.\n at %s line %d", op_decl->file, op_decl->line);
402 1           break;
403             }
404            
405 205           SPVM_OP* op_version_string = op_decl->first;
406 205           SPVM_CONSTANT* version_string_constant = op_version_string->uv.constant;
407 205           const char* version_string = version_string_constant->value.oval;
408 205           int32_t version_string_length = version_string_constant->string_length;
409            
410             // Version string validation
411             {
412 205           int32_t dot_count = 0;
413 205           int32_t digits_after_dot = 0;
414 205           int32_t invalid_char = 0;
415 1631 100         for (int32_t version_global_string_address_id = 0; version_global_string_address_id < version_string_length; version_global_string_address_id++) {
416 1427           char ch = version_string[version_global_string_address_id];
417            
418 1427 100         if (!(ch == '.' || isdigit(ch))) {
    100          
419 1           invalid_char = 1;
420 1           break;
421             }
422            
423 1426 100         if (ch == '.') {
424 204           dot_count++;
425             }
426            
427 1426 100         if (dot_count > 0 && isdigit(ch)) {
    100          
428 1006           digits_after_dot++;
429             }
430             }
431            
432 205 100         if (invalid_char) {
433 1           SPVM_COMPILER_error(compiler, "A character in a version string must be a number or \".\".\n at %s line %d", op_decl->file, op_decl->line);
434 1           break;
435             }
436            
437 204 100         if (!isdigit(version_string[0])) {
438 1           SPVM_COMPILER_error(compiler, "A version string must begin with a number.\n at %s line %d", op_decl->file, op_decl->line);
439 1           break;
440             }
441            
442 203 100         if (!isdigit(version_string[version_string_length - 1])) {
443 1           SPVM_COMPILER_error(compiler, "A version string must end with a number.\n at %s line %d", op_decl->file, op_decl->line);
444 1           break;
445             }
446            
447 202 100         if (!(dot_count <= 1)) {
448 1           SPVM_COMPILER_error(compiler, "The number of \".\" in a version string must be less than or equal to 1.\n at %s line %d", op_decl->file, op_decl->line);
449 1           break;
450             }
451            
452 201 100         if (!(digits_after_dot % 3 == 0)) {
453 2           SPVM_COMPILER_error(compiler, "The length of characters after \".\" in a version string must be divisible by 3.\n at %s line %d", op_decl->file, op_decl->line);
454 2           break;
455             }
456            
457             {
458             char *end;
459 199           double double_value = strtod(version_string, &end);
460 199 50         if (*end != '\0') {
461 0           SPVM_COMPILER_error(compiler, "A version string must be able to be parsed by the \"strtod\" C function.\n at %s line %d", compiler->current_file, compiler->current_line);
462 0           break;
463             }
464             }
465             }
466            
467 199           SPVM_STRING_new(compiler, version_string, version_string_length);
468 199           type->basic_type->version_string = version_string;
469             }
470             // use statement
471 315631 100         else if (op_decl->id == SPVM_OP_C_ID_USE) {
472 29406           SPVM_OP* op_use = op_decl;
473            
474 29406           SPVM_LIST_push(type->basic_type->use_basic_type_names, (void*)op_use->uv.use->op_type->uv.type->unresolved_basic_type_name);
475            
476             // Class alias
477 29406           const char* alias_name = op_use->uv.use->alias_name;
478 29406 100         if (alias_name) {
479            
480             // Basic type name must begin with upper case, otherwise compiler error occur.
481             // (Invalid example) Foo::bar
482 16 100         if (islower(alias_name[0])) {
483 1           SPVM_COMPILER_error(compiler, "The alias name \"%s\" must begin with an upper case character.\n at %s line %d", alias_name, op_decl->file, op_decl->line);
484             }
485             else {
486 15           const char* use_basic_type_name = op_use->uv.use->op_type->uv.type->unresolved_basic_type_name;
487 15           const char* use_basic_type_name_exists = SPVM_HASH_get(type->basic_type->alias_symtable, alias_name, strlen(alias_name));
488 15 100         if (use_basic_type_name_exists) {
489 2           SPVM_COMPILER_error(compiler, "The alias name \"%s\" is already used.\n at %s line %d", alias_name, op_decl->file, op_decl->line);
490             }
491             else {
492 29406           SPVM_HASH_set(type->basic_type->alias_symtable, alias_name, strlen(alias_name), (void*)use_basic_type_name);
493             }
494             }
495             }
496             }
497             // allow statement
498 286225 100         else if (op_decl->id == SPVM_OP_C_ID_ALLOW) {
499 533           SPVM_LIST_push(basic_type->allows, op_decl->uv.allow);
500             }
501             // interface statement
502 285692 100         else if (op_decl->id == SPVM_OP_C_ID_INTERFACE) {
503 1386 100         if (type->basic_type->category == SPVM_NATIVE_C_BASIC_TYPE_CATEGORY_MULNUM) {
504 1           SPVM_COMPILER_error(compiler, "The interface statement cannnot be used in the definition of the multi-numeric type.\n at %s line %d", op_decl->file, op_decl->line);
505             }
506 1386           const char* interface_name = op_decl->uv.interface->op_type->uv.type->unresolved_basic_type_name;
507            
508 1386           SPVM_LIST_push(type->basic_type->interface_decls, op_decl->uv.interface);
509             }
510             // Class var declarations
511 284306 100         else if (op_decl->id == SPVM_OP_C_ID_CLASS_VAR) {
512 8179           SPVM_CLASS_VAR* class_var = op_decl->uv.class_var;
513            
514 8179 100         if (type->basic_type->category == SPVM_NATIVE_C_BASIC_TYPE_CATEGORY_INTERFACE) {
515 1           SPVM_COMPILER_error(compiler, "The interface cannnot have class variables.\n at %s line %d", op_decl->file, op_decl->line);
516             }
517 8179           SPVM_LIST_push(type->basic_type->class_vars, op_decl->uv.class_var);
518            
519             // Getter
520 8179 100         if (class_var->has_getter) {
521             // static method FOO : TYPE () {
522             // return $FOO;
523             // }
524            
525 7616           SPVM_OP* op_method = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_METHOD, op_decl->file, op_decl->line);
526 7616           SPVM_STRING* method_name_string = SPVM_STRING_new(compiler, class_var->name + 1, strlen(class_var->name) - 1);
527 7616           const char* method_name = method_name_string->value;
528 7616           SPVM_OP* op_name_method = SPVM_OP_new_op_name(compiler, method_name, op_decl->file, op_decl->line);
529            
530             // If the type of the class_var is byte or short, the return type becomes int
531 7616           SPVM_TYPE* class_var_type = class_var->type;
532             SPVM_TYPE* return_type;
533 7616 100         if (SPVM_TYPE_is_byte_type(compiler, class_var_type->basic_type->id, class_var_type->dimension, class_var_type->flag)
534 7614 100         || SPVM_TYPE_is_short_type(compiler, class_var_type->basic_type->id, class_var_type->dimension, class_var_type->flag))
535             {
536 4           return_type = SPVM_OP_new_op_int_type(compiler, op_decl->file, op_decl->line)->uv.type;
537             }
538             else {
539 7612           return_type = class_var->type;
540             }
541 7616           SPVM_OP* op_return_type = SPVM_OP_new_op_type(compiler, return_type->unresolved_basic_type_name, return_type->basic_type, return_type->dimension, return_type->flag, op_decl->file, op_decl->line);
542            
543 7616           SPVM_OP* op_args = SPVM_OP_new_op_list(compiler, op_decl->file, op_decl->line);
544            
545 7616           SPVM_OP* op_block = SPVM_OP_new_op_block(compiler, op_decl->file, op_decl->line);
546 7616           SPVM_OP* op_statements = SPVM_OP_new_op_list(compiler, op_decl->file, op_decl->line);
547 7616           SPVM_OP* op_return = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_RETURN, op_decl->file, op_decl->line);
548            
549 7616           SPVM_OP* op_name_basic_type_var = SPVM_OP_new_op_name(compiler, class_var->name, op_decl->file, op_decl->line);
550 7616           SPVM_OP* op_var_class_var = SPVM_OP_new_op_var(compiler, op_name_basic_type_var);
551            
552 7616           SPVM_OP_insert_child(compiler, op_return, op_return->last, op_var_class_var);
553 7616           SPVM_OP_insert_child(compiler, op_statements, op_statements->last, op_return);
554 7616           SPVM_OP_insert_child(compiler, op_block, op_block->last, op_statements);
555            
556 7616           SPVM_OP* op_list_attributes = SPVM_OP_new_op_list(compiler, compiler->current_file, compiler->current_line);
557 7616           SPVM_OP* op_attribute_static = SPVM_OP_new_op_attribute(compiler, SPVM_ATTRIBUTE_C_ID_STATIC, compiler->current_file, compiler->current_line);
558 7616           SPVM_OP_insert_child(compiler, op_list_attributes, op_list_attributes->first, op_attribute_static);
559            
560 7616           SPVM_OP_build_method_definition(compiler, op_method, op_name_method, op_return_type, op_args, op_list_attributes, op_block, NULL, 0, 0);
561            
562 7616           SPVM_LIST_push(type->basic_type->methods, op_method->uv.method);
563             }
564            
565             // Setter
566 8179 100         if (class_var->has_setter) {
567            
568             // method SET_FOO : void ($value : TYPE) {
569             // $FOO = $value;
570             // }
571            
572 89           SPVM_OP* op_method = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_METHOD, op_decl->file, op_decl->line);
573 89           char* method_name_tmp = SPVM_ALLOCATOR_alloc_memory_block_permanent(compiler->current_each_compile_allocator, 4 + strlen(class_var->name) - 1 + 1);
574 89           memcpy(method_name_tmp, "SET_", 4);
575 89           memcpy(method_name_tmp + 4, class_var->name + 1, strlen(class_var->name) - 1);
576            
577 89           SPVM_STRING* method_name_string = SPVM_STRING_new(compiler, method_name_tmp, strlen(method_name_tmp));
578 89           const char* method_name = method_name_string->value;
579            
580 89           SPVM_OP* op_name_method = SPVM_OP_new_op_name(compiler, method_name, op_decl->file, op_decl->line);
581 89           SPVM_OP* op_return_type = SPVM_OP_new_op_void_type(compiler, op_decl->file, op_decl->line);
582 89           SPVM_OP* op_args = SPVM_OP_new_op_list(compiler, op_decl->file, op_decl->line);
583            
584             // If the type of the class_var is byte or short, the arg type becomes int
585 89           SPVM_TYPE* class_var_type = class_var->type;
586             SPVM_TYPE* arg_type;
587 89 100         if (SPVM_TYPE_is_byte_type(compiler, class_var_type->basic_type->id, class_var_type->dimension, class_var_type->flag)
588 87 100         || SPVM_TYPE_is_short_type(compiler, class_var_type->basic_type->id, class_var_type->dimension, class_var_type->flag))
589             {
590 4           arg_type = SPVM_OP_new_op_int_type(compiler, op_decl->file, op_decl->line)->uv.type;
591             }
592             else {
593 85           arg_type = class_var->type;
594             }
595 89           SPVM_OP* op_type_value = SPVM_OP_new_op_type(compiler, arg_type->unresolved_basic_type_name, arg_type->basic_type, arg_type->dimension, arg_type->flag, op_decl->file, op_decl->line);
596            
597 89           SPVM_OP* op_var_value_name = SPVM_OP_new_op_name(compiler, "$value", op_decl->file, op_decl->line);
598 89           SPVM_OP* op_var_value = SPVM_OP_new_op_var(compiler, op_var_value_name);
599 89           SPVM_OP* op_arg_value = SPVM_OP_build_arg(compiler, op_var_value, op_type_value, NULL, NULL);
600            
601 89           SPVM_OP_insert_child(compiler, op_args, op_args->last, op_arg_value);
602            
603 89           SPVM_OP* op_block = SPVM_OP_new_op_block(compiler, op_decl->file, op_decl->line);
604 89           SPVM_OP* op_statements = SPVM_OP_new_op_list(compiler, op_decl->file, op_decl->line);
605            
606 89           SPVM_OP* op_name_basic_type_var = SPVM_OP_new_op_name(compiler, class_var->name, op_decl->file, op_decl->line);
607 89           SPVM_OP* op_var_class_var = SPVM_OP_new_op_var(compiler, op_name_basic_type_var);
608            
609 89           SPVM_OP* op_var_assign_value_name = SPVM_OP_new_op_name(compiler, "$value", op_decl->file, op_decl->line);
610 89           SPVM_OP* op_var_assign_value = SPVM_OP_new_op_var(compiler, op_var_assign_value_name);
611            
612 89           SPVM_OP* op_type_cast = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_TYPE_CAST, op_decl->file, op_decl->line);
613 89           SPVM_OP* op_type_for_cast = SPVM_OP_new_op_type(compiler, class_var_type->unresolved_basic_type_name, class_var_type->basic_type, class_var_type->dimension, class_var_type->flag, op_decl->file, op_decl->line);
614            
615 89           SPVM_OP_build_type_cast(compiler, op_type_cast, op_type_for_cast, op_var_assign_value, NULL);
616            
617 89           SPVM_OP* op_assign = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_decl->file, op_decl->line);
618 89           SPVM_OP_build_assign(compiler, op_assign, op_var_class_var, op_type_cast);
619            
620 89           SPVM_OP_insert_child(compiler, op_statements, op_statements->last, op_assign);
621 89           SPVM_OP_insert_child(compiler, op_block, op_block->last, op_statements);
622            
623 89           SPVM_OP* op_list_attributes = SPVM_OP_new_op_list(compiler, compiler->current_file, compiler->current_line);
624 89           SPVM_OP* op_attribute_static = SPVM_OP_new_op_attribute(compiler, SPVM_ATTRIBUTE_C_ID_STATIC, compiler->current_file, compiler->current_line);
625 89           SPVM_OP_insert_child(compiler, op_list_attributes, op_list_attributes->first, op_attribute_static);
626            
627 89           op_method = SPVM_OP_build_method_definition(compiler, op_method, op_name_method, op_return_type, op_args, op_list_attributes, op_block, NULL, 0, 0);
628            
629 8179           SPVM_LIST_push(type->basic_type->methods, op_method->uv.method);
630             }
631             }
632             // Field declarations
633 276127 100         else if (op_decl->id == SPVM_OP_C_ID_FIELD) {
634 27760           SPVM_FIELD* field = op_decl->uv.field;
635            
636 27760 100         if (type->basic_type->category == SPVM_NATIVE_C_BASIC_TYPE_CATEGORY_INTERFACE) {
637 1           SPVM_COMPILER_error(compiler, "The interface cannnot have fields.\n at %s line %d", op_decl->file, op_decl->line);
638             }
639 27760           SPVM_LIST_push(type->basic_type->unmerged_fields, field);
640            
641             // Getter
642 27760 100         if (field->has_getter) {
643             // method foo : TYPE () {
644             // return $self->{foo};
645             // }
646            
647 17511           SPVM_OP* op_method = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_METHOD, op_decl->file, op_decl->line);
648 17511           SPVM_OP* op_name_method = SPVM_OP_new_op_name(compiler, field->name, op_decl->file, op_decl->line);
649            
650             // If the type of the field is byte or short, the return type becomes int
651 17511           SPVM_TYPE* field_type = field->type;
652             SPVM_TYPE* return_type;
653 17511 100         if (SPVM_TYPE_is_byte_type(compiler, field_type->basic_type->id, field_type->dimension, field_type->flag)
654 15912 100         || SPVM_TYPE_is_short_type(compiler, field_type->basic_type->id, field_type->dimension, field_type->flag))
655             {
656 3198           return_type = SPVM_OP_new_op_int_type(compiler, op_decl->file, op_decl->line)->uv.type;
657             }
658             else {
659 14313           return_type = field->type;
660             }
661 17511           SPVM_OP* op_return_type = SPVM_OP_new_op_type(compiler, return_type->unresolved_basic_type_name, return_type->basic_type, return_type->dimension, return_type->flag, op_decl->file, op_decl->line);
662            
663            
664 17511           SPVM_OP* op_args = SPVM_OP_new_op_list(compiler, op_decl->file, op_decl->line);
665            
666 17511           SPVM_OP* op_block = SPVM_OP_new_op_block(compiler, op_decl->file, op_decl->line);
667 17511           SPVM_OP* op_statements = SPVM_OP_new_op_list(compiler, op_decl->file, op_decl->line);
668 17511           SPVM_OP* op_return = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_RETURN, op_decl->file, op_decl->line);
669            
670 17511           SPVM_OP* op_var_name_invocant = SPVM_OP_new_op_name(compiler, "$self", op_decl->file, op_decl->line);
671 17511           SPVM_OP* op_var_self_invocant = SPVM_OP_new_op_var(compiler, op_var_name_invocant);
672 17511           SPVM_OP* op_name_field_access = SPVM_OP_new_op_name(compiler, field->name, op_decl->file, op_decl->line);
673            
674 17511           SPVM_OP* op_field_access = SPVM_OP_new_op_field_access(compiler, op_decl->file, op_decl->line);
675            
676 17511           SPVM_OP_build_field_access(compiler, op_field_access, op_var_self_invocant, op_name_field_access);
677            
678 17511           SPVM_OP_insert_child(compiler, op_return, op_return->last, op_field_access);
679 17511           SPVM_OP_insert_child(compiler, op_statements, op_statements->last, op_return);
680 17511           SPVM_OP_insert_child(compiler, op_block, op_block->last, op_statements);
681            
682 17511           SPVM_OP_build_method_definition(compiler, op_method, op_name_method, op_return_type, op_args, NULL, op_block, NULL, 0, 0);
683            
684 17511           SPVM_LIST_push(type->basic_type->methods, op_method->uv.method);
685             }
686            
687             // Setter
688 27760 100         if (field->has_setter) {
689             // method set_foo : void ($foo : TYPE) {
690             // $self->{foo} = $foo;
691             // }
692            
693 1423           SPVM_OP* op_method = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_METHOD, op_decl->file, op_decl->line);
694 1423           char* method_name_tmp = SPVM_ALLOCATOR_alloc_memory_block_permanent(compiler->current_each_compile_allocator, 4 + strlen(field->name) + 1);
695 1423           memcpy(method_name_tmp, "set_", 4);
696 1423           memcpy(method_name_tmp + 4, field->name, strlen(field->name));
697 1423           SPVM_STRING* method_name_string = SPVM_STRING_new(compiler, method_name_tmp, strlen(method_name_tmp));
698 1423           const char* method_name = method_name_string->value;
699 1423           SPVM_OP* op_name_method = SPVM_OP_new_op_name(compiler, method_name, op_decl->file, op_decl->line);
700 1423           SPVM_OP* op_return_type = SPVM_OP_new_op_void_type(compiler, op_decl->file, op_decl->line);
701 1423           SPVM_OP* op_args = SPVM_OP_new_op_list(compiler, op_decl->file, op_decl->line);
702            
703             // If the type of the field is byte or short, the arg type becomes int
704 1423           SPVM_TYPE* field_type = field->type;
705             SPVM_TYPE* arg_type;
706 1423 100         if (SPVM_TYPE_is_byte_type(compiler, field_type->basic_type->id, field_type->dimension, field_type->flag)
707 1325 100         || SPVM_TYPE_is_short_type(compiler, field_type->basic_type->id, field_type->dimension, field_type->flag))
708             {
709 196           arg_type = SPVM_OP_new_op_int_type(compiler, op_decl->file, op_decl->line)->uv.type;
710             }
711             else {
712 1227           arg_type = field->type;
713             }
714 1423           SPVM_OP* op_type_value = SPVM_OP_new_op_type(compiler, arg_type->unresolved_basic_type_name, arg_type->basic_type, arg_type->dimension, arg_type->flag, op_decl->file, op_decl->line);
715            
716 1423           char* arg_name_tmp = SPVM_ALLOCATOR_alloc_memory_block_permanent(compiler->current_each_compile_allocator, 1 + strlen(field->name) + 1);
717 1423           memcpy(arg_name_tmp, "$", 1);
718 1423           memcpy(arg_name_tmp + 1, field->name, strlen(field->name));
719 1423           SPVM_STRING* arg_name_string = SPVM_STRING_new(compiler, arg_name_tmp, strlen(arg_name_tmp));
720 1423           const char* arg_name = arg_name_string->value;
721 1423           SPVM_OP* op_name_arg = SPVM_OP_new_op_name(compiler, arg_name, op_decl->file, op_decl->line);
722            
723 1423           SPVM_OP* op_var_value_name = SPVM_OP_new_op_name(compiler, arg_name, op_decl->file, op_decl->line);
724 1423           SPVM_OP* op_var_value = SPVM_OP_new_op_var(compiler, op_var_value_name);
725 1423           SPVM_OP* op_arg_value = SPVM_OP_build_arg(compiler, op_var_value, op_type_value, NULL, NULL);
726            
727 1423           SPVM_OP_insert_child(compiler, op_args, op_args->last, op_arg_value);
728            
729 1423           SPVM_OP* op_block = SPVM_OP_new_op_block(compiler, op_decl->file, op_decl->line);
730 1423           SPVM_OP* op_statements = SPVM_OP_new_op_list(compiler, op_decl->file, op_decl->line);
731            
732 1423           SPVM_OP* op_var_name_invocant = SPVM_OP_new_op_name(compiler, "$self", op_decl->file, op_decl->line);
733 1423           SPVM_OP* op_var_self_invocant = SPVM_OP_new_op_var(compiler, op_var_name_invocant);
734 1423           SPVM_OP* op_name_field_access = SPVM_OP_new_op_name(compiler, field->name, op_decl->file, op_decl->line);
735 1423           SPVM_OP* op_field_access = SPVM_OP_new_op_field_access(compiler, op_decl->file, op_decl->line);
736 1423           SPVM_OP_build_field_access(compiler, op_field_access, op_var_self_invocant, op_name_field_access);
737            
738 1423           SPVM_OP* op_var_assign_value_name = SPVM_OP_new_op_name(compiler, arg_name, op_decl->file, op_decl->line);
739 1423           SPVM_OP* op_var_assign_value = SPVM_OP_new_op_var(compiler, op_var_assign_value_name);
740            
741 1423           SPVM_OP* op_type_cast = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_TYPE_CAST, op_decl->file, op_decl->line);
742 1423           SPVM_OP* op_type_for_cast = SPVM_OP_new_op_type(compiler, field_type->unresolved_basic_type_name, field_type->basic_type, field_type->dimension, field_type->flag, op_decl->file, op_decl->line);
743            
744 1423           SPVM_OP_build_type_cast(compiler, op_type_cast, op_type_for_cast, op_var_assign_value, NULL);
745            
746 1423           SPVM_OP* op_assign = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_decl->file, op_decl->line);
747 1423           SPVM_OP_build_assign(compiler, op_assign, op_field_access, op_type_cast);
748            
749 1423           SPVM_OP_insert_child(compiler, op_statements, op_statements->last, op_assign);
750 1423           SPVM_OP_insert_child(compiler, op_block, op_block->last, op_statements);
751            
752 1423           SPVM_OP_build_method_definition(compiler, op_method, op_name_method, op_return_type, op_args, NULL, op_block, NULL, 0, 0);
753            
754 27760           SPVM_LIST_push(type->basic_type->methods, op_method->uv.method);
755             }
756             }
757             // Enumeration definition
758 248367 100         else if (op_decl->id == SPVM_OP_C_ID_ENUM) {
759 3249           SPVM_OP* op_enum_block = op_decl->first;
760 3249           SPVM_OP* op_enumeration_values = op_enum_block->first;
761 3249           SPVM_OP* op_method = op_enumeration_values->first;
762 7054 100         while ((op_method = SPVM_OP_sibling(compiler, op_method))) {
763 3805           SPVM_LIST_push(type->basic_type->methods, op_method->uv.method);
764             }
765             }
766             // Method definition
767 245118 50         else if (op_decl->id == SPVM_OP_C_ID_METHOD) {
768 245118           SPVM_METHOD* method = op_decl->uv.method;
769            
770 245118           SPVM_LIST_push(type->basic_type->methods, op_decl->uv.method);
771            
772             // Fields of anon method
773 245118           SPVM_LIST* anon_method_fields = op_decl->uv.method->anon_method_fields;
774 245153 100         for (int32_t i = 0; i < anon_method_fields->length; i++) {
775 35           SPVM_FIELD* anon_method_field = SPVM_LIST_get(anon_method_fields, i);
776            
777 35           SPVM_LIST_push(type->basic_type->unmerged_fields, anon_method_field);
778 35           anon_method_field->is_anon_method_field = 1;
779             }
780            
781             // INIT block
782 245118 100         if (op_decl->uv.method->is_init) {
783 245118           basic_type->has_init_block = 1;
784             }
785             }
786             else {
787 0           assert(0);
788             }
789             }
790             }
791            
792             // Field declarations
793 70879 100         for (int32_t i = 0; i < type->basic_type->unmerged_fields->length; i++) {
794 27795           SPVM_FIELD* field = SPVM_LIST_get(type->basic_type->unmerged_fields, i);
795              
796             // The default of the access controll of the field is private.
797 27795 100         if (field->access_control_type == SPVM_ATTRIBUTE_C_ID_UNKNOWN) {
798             // If anon method, field is public
799 26411 100         if (basic_type->is_anon) {
800 35           field->access_control_type = SPVM_ATTRIBUTE_C_ID_PUBLIC;
801             }
802             // If multi-numeric type, field is public
803 26376 100         else if (type->basic_type->category == SPVM_NATIVE_C_BASIC_TYPE_CATEGORY_MULNUM) {
804 1290           field->access_control_type = SPVM_ATTRIBUTE_C_ID_PUBLIC;
805             }
806             // Default is private
807             else {
808 25086           field->access_control_type = SPVM_ATTRIBUTE_C_ID_PRIVATE;
809             }
810             }
811              
812 27795           field->index = i;
813 27795           const char* field_name = field->op_name->uv.name;
814              
815 27795           SPVM_FIELD* found_field = SPVM_HASH_get(type->basic_type->unmerged_field_symtable, field_name, strlen(field_name));
816            
817 27795 100         if (found_field) {
818 1           SPVM_COMPILER_error(compiler, "Redeclaration of the \"%s\" field in the \"%s\" class.\n at %s line %d", field_name, basic_type_name, field->op_field->file, field->op_field->line);
819             }
820             else {
821 27794           SPVM_HASH_set(type->basic_type->unmerged_field_symtable, field_name, strlen(field_name), field);
822            
823             // Add op class
824 27794           field->current_basic_type = type->basic_type;
825             }
826             }
827              
828             // Class variable declarations
829 51263 100         for (int32_t i = 0; i < type->basic_type->class_vars->length; i++) {
830 8179           SPVM_CLASS_VAR* class_var = SPVM_LIST_get(type->basic_type->class_vars, i);
831 8179           const char* class_var_name = class_var->name;
832              
833 8179           SPVM_CLASS_VAR* found_class_var = SPVM_HASH_get(type->basic_type->class_var_symtable, class_var_name, strlen(class_var_name));
834            
835 8179 100         if (found_class_var) {
836 1           SPVM_COMPILER_error(compiler, "Redeclaration of the class variable \"$%s\" in the \"%s\" class.\n at %s line %d", class_var_name + 1, basic_type_name, class_var->op_class_var->file, class_var->op_class_var->line);
837             }
838             else {
839 8178           SPVM_HASH_set(type->basic_type->class_var_symtable, class_var_name, strlen(class_var_name), class_var);
840            
841             // Add op class
842 8178           class_var->current_basic_type = type->basic_type;
843             }
844             }
845            
846             // INIT block
847             {
848             // Check INIT block existance
849 43084           int32_t has_init_block = 0;
850 306940 100         for (int32_t i = 0; i < type->basic_type->methods->length; i++) {
851 265404           SPVM_METHOD* method = SPVM_LIST_get(type->basic_type->methods, i);
852 265404 100         if (method->is_init) {
853 1548           has_init_block = 1;
854 1548           break;
855             }
856             }
857            
858             // Add an default INIT block
859 43084 100         if (type->basic_type->category == SPVM_NATIVE_C_BASIC_TYPE_CATEGORY_CLASS && !has_init_block) {
    100          
860 35392           SPVM_OP* op_init = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_INIT, op_class->file, op_class->line);
861            
862             // Statements
863 35392           SPVM_OP* op_list_statements = SPVM_OP_new_op_list(compiler, op_class->file, op_class->line);
864            
865             // Block
866 35392           SPVM_OP* op_block = SPVM_OP_new_op_block(compiler, op_class->file, op_class->line);
867 35392           SPVM_OP_insert_child(compiler, op_block, op_block->last, op_list_statements);
868            
869 35392           SPVM_OP* op_method = SPVM_OP_build_init_block(compiler, op_init, op_block);
870            
871 35392           SPVM_LIST_push(type->basic_type->methods, op_method->uv.method);
872             }
873             }
874            
875             // Method declarations
876 354038 100         for (int32_t i = 0; i < type->basic_type->methods->length; i++) {
877 310954           SPVM_METHOD* method = SPVM_LIST_get(type->basic_type->methods, i);
878            
879 310954 100         if (!method->is_class_method) {
880 116141           SPVM_VAR_DECL* arg_var_decl_first = SPVM_LIST_get(method->var_decls, 0);
881 116141           arg_var_decl_first->type->unresolved_basic_type_name = basic_type_name;
882             }
883            
884 310954           SPVM_OP* op_name_method = method->op_name;
885 310954           const char* method_name = op_name_method->uv.name;
886            
887             int32_t must_have_block;
888 310954 100         if (type->basic_type->category == SPVM_NATIVE_C_BASIC_TYPE_CATEGORY_INTERFACE) {
889 6130           must_have_block = 0;
890             }
891             else {
892 304824 100         if (method->is_native) {
893 66055           must_have_block = 0;
894             }
895             else {
896 238769           must_have_block = 1;
897             }
898             }
899            
900 310954 100         if (must_have_block && !method->op_block) {
    100          
901 2           SPVM_COMPILER_error(compiler, "The non-native method must have the block.\n at %s line %d", op_name_method->file, op_name_method->line);
902             }
903            
904             // Method check
905            
906             // Set first argument type if not set
907 310954 100         if (method->args_length > 0) {
908 231570           SPVM_VAR_DECL* arg_var_decl_first = SPVM_LIST_get(method->var_decls, 0);
909 231570           SPVM_OP* op_arg_first_type = NULL;
910 231570 100         if (!method->is_class_method) {
911 116141           SPVM_TYPE* arg_invocant_type = op_type->uv.type;
912 116141           op_arg_first_type = SPVM_OP_new_op_type(compiler, arg_invocant_type->unresolved_basic_type_name, arg_invocant_type->basic_type, arg_invocant_type->dimension, arg_invocant_type->flag, arg_var_decl_first->op_var_decl->file, arg_var_decl_first->op_var_decl->line);
913 116141           arg_var_decl_first->type = op_arg_first_type->uv.type;
914 116141 50         assert(arg_invocant_type->basic_type);
915             }
916             else {
917 115429           SPVM_OP* op_type_new_arg_var_decl_first = SPVM_OP_new_op_type(compiler, arg_var_decl_first->type->unresolved_basic_type_name, arg_var_decl_first->type->basic_type, arg_var_decl_first->type->dimension, arg_var_decl_first->type->flag, arg_var_decl_first->op_var_decl->file, arg_var_decl_first->op_var_decl->line);
918            
919 115429           op_arg_first_type = op_type_new_arg_var_decl_first;
920 115429 50         assert(op_arg_first_type->uv.type->basic_type);
921             }
922            
923 742438 100         for (int32_t arg_index = 0; arg_index < method->args_length; arg_index++) {
924 510868           SPVM_VAR_DECL* arg_var_decl = SPVM_LIST_get(method->var_decls, arg_index);
925 510868           SPVM_BASIC_TYPE_add_constant_string(compiler, basic_type, arg_var_decl->var->name, strlen(arg_var_decl->var->name));
926 510868           arg_var_decl->name = arg_var_decl->var->name;
927             }
928             }
929            
930             // If Method is anon, method must be method
931 310954 100         if (strlen(method_name) == 0 && method->is_class_method) {
    100          
932 1           SPVM_COMPILER_error(compiler, "The anon method must be an instance method.\n at %s line %d", method->op_method->file, method->op_method->line);
933             }
934            
935 310954 100         if (type->basic_type->category == SPVM_NATIVE_C_BASIC_TYPE_CATEGORY_INTERFACE) {
936             // Method having interface_t attribute must be method
937 6130 100         if (method->is_class_method) {
938 6130           SPVM_COMPILER_error(compiler, "The method defined in the interface must be an instance method.\n at %s line %d", method->op_method->file, method->op_method->line);
939             }
940             }
941 304824 100         else if (type->basic_type->category == SPVM_NATIVE_C_BASIC_TYPE_CATEGORY_CLASS) {
942 304822 100         if (method->is_required) {
943 1           SPVM_COMPILER_error(compiler, "The method defined in the class cannnot have the method attribute \"required\".\n at %s line %d", method->op_method->file, method->op_method->line);
944             }
945             }
946            
947 310954 100         if (method->is_native) {
948 66056 100         if (method->op_block) {
949 2           SPVM_COMPILER_error(compiler, "The native method cannnot have the block.\n at %s line %d", method->op_method->file, method->op_method->line);
950             }
951             }
952            
953 310954           SPVM_METHOD* found_method = SPVM_HASH_get(type->basic_type->method_symtable, method_name, strlen(method_name));
954            
955 310954 100         if (found_method) {
956 3           SPVM_COMPILER_error(compiler, "Redeclaration of the \"%s\" method in the \"%s\" class.\n at %s line %d", method_name, basic_type_name, method->op_method->file, method->op_method->line);
957             }
958             // Unknown method
959             else {
960 310951           const char* found_method_name = SPVM_HASH_get(type->basic_type->method_symtable, method_name, strlen(method_name));
961 310951 50         if (found_method_name) {
962 0           SPVM_COMPILER_error(compiler, "Redeclaration of the \"%s\" method.\n at %s line %d", method_name, method->op_method->file, method->op_method->line);
963             }
964             else {
965             // Bind standard functions
966 310951           method->current_basic_type = type->basic_type;
967            
968 310951 100         if (method->is_destructor) {
969 763           basic_type->destructor_method = method;
970             }
971            
972 310951 100         if (method->is_init) {
973 36938           basic_type->init_method = method;
974             }
975            
976 310951 50         assert(method->op_method->file);
977            
978             // Method absolute name
979 310951           int32_t method_abs_name_length = strlen(type->unresolved_basic_type_name) + 2 + strlen(method->name);
980 310951           char* method_abs_name = SPVM_ALLOCATOR_alloc_memory_block_permanent(compiler->current_each_compile_allocator, method_abs_name_length + 1);
981 310951           memcpy(method_abs_name, type->unresolved_basic_type_name, strlen(type->unresolved_basic_type_name));
982 310951           memcpy(method_abs_name + strlen(basic_type_name), "->", 2);
983 310951           memcpy(method_abs_name + strlen(basic_type_name) + 2, method_name, strlen(method_name));
984 310951           method->abs_name = method_abs_name;
985              
986             // Add the method to the method symtable of the class
987 310951           SPVM_HASH_set(type->basic_type->method_symtable, method->name, strlen(method->name), method);
988             }
989             }
990             }
991            
992             // mulnum_t
993 43084 100         if (type->basic_type->category == SPVM_NATIVE_C_BASIC_TYPE_CATEGORY_MULNUM) {
994 282 100         if (type->basic_type->methods->length > 0) {
995 2           SPVM_COMPILER_error(compiler, "The multi-numeric type cannnot have methods.\n at %s line %d", op_class->file, op_class->line);
996             }
997 282 100         if (type->basic_type->class_vars->length > 0) {
998 2           SPVM_COMPILER_error(compiler, "The multi-numeric type cannnot have class variables.\n at %s line %d", op_class->file, op_class->line);
999             }
1000 282 100         if (type->basic_type->unmerged_fields->length == 0) {
1001 8           SPVM_COMPILER_error(compiler, "The multi-numeric type must have at least one field.\n at %s line %d", op_class->file, op_class->line);
1002             }
1003 274 100         else if (type->basic_type->unmerged_fields->length > 255) {
1004 1           SPVM_COMPILER_error(compiler, "The length of the fields defined in the multi-numeric type must be less than or equal to 255.\n at %s line %d", op_class->file, op_class->line);
1005             }
1006             }
1007              
1008 43084           return op_class;
1009             }
1010              
1011 3131           SPVM_OP* SPVM_OP_build_extends(SPVM_COMPILER* compiler, SPVM_OP* op_extends, SPVM_OP* op_type_parent_basic_type) {
1012            
1013 3131           SPVM_OP_insert_child(compiler, op_extends, op_extends->last, op_type_parent_basic_type);
1014            
1015 3131           return op_extends;
1016             }
1017              
1018 206           SPVM_OP* SPVM_OP_build_version_decl(SPVM_COMPILER* compiler, SPVM_OP* op_version_decl, SPVM_OP* op_version_string) {
1019            
1020 206           SPVM_OP_insert_child(compiler, op_version_decl, op_version_decl->last, op_version_string);
1021            
1022 206           return op_version_decl;
1023             }
1024              
1025 533           SPVM_OP* SPVM_OP_build_allow(SPVM_COMPILER* compiler, SPVM_OP* op_allow, SPVM_OP* op_type) {
1026            
1027 533           SPVM_ALLOW* allow = SPVM_ALLOW_new(compiler);
1028 533           op_allow->uv.allow = allow;
1029 533           allow->op_allow = op_allow;
1030 533           allow->basic_type_name = op_type->uv.type->unresolved_basic_type_name;
1031            
1032             // add use stack
1033 533           SPVM_OP* op_use = SPVM_OP_new_op_use(compiler, op_allow->file, op_allow->line);
1034 533           SPVM_OP* op_name_alias = NULL;
1035 533           int32_t is_require = 0;
1036 533           SPVM_OP_build_use(compiler, op_use, op_type, op_name_alias, is_require);
1037            
1038 533           return op_allow;
1039             }
1040              
1041 2           SPVM_OP* SPVM_OP_build_alias(SPVM_COMPILER* compiler, SPVM_OP* op_use, SPVM_OP* op_type, SPVM_OP* op_name_alias) {
1042            
1043 2           SPVM_USE* use = op_use->uv.use;
1044 2           use->op_use = op_use;
1045 2           use->op_type = op_type;
1046 2           const char* alias_name = op_name_alias->uv.name;
1047 2           use->alias_name = alias_name;
1048            
1049 2           return op_use;
1050             }
1051              
1052 81847           SPVM_OP* SPVM_OP_build_use(SPVM_COMPILER* compiler, SPVM_OP* op_use, SPVM_OP* op_type, SPVM_OP* op_name_alias, int32_t is_require) {
1053            
1054 81847           SPVM_USE* use = op_use->uv.use;
1055 81847           use->op_use = op_use;
1056 81847           use->op_type = op_type;
1057 81847           use->is_require = is_require;
1058            
1059 81847 100         if (op_name_alias) {
1060 14           const char* alias_name = op_name_alias->uv.name;
1061 14           use->alias_name = alias_name;
1062             }
1063            
1064 81847           SPVM_LIST_push(compiler->op_use_stack, op_use);
1065            
1066 81847           return op_use;
1067             }
1068              
1069 3249           SPVM_OP* SPVM_OP_build_enumeration_definition(SPVM_COMPILER* compiler, SPVM_OP* op_enumeration, SPVM_OP* op_enumeration_block, SPVM_OP* op_attributes) {
1070            
1071 3249           SPVM_OP_insert_child(compiler, op_enumeration, op_enumeration->last, op_enumeration_block);
1072            
1073 3249           SPVM_OP* op_enumeration_values = op_enumeration_block->first;
1074 3249           SPVM_OP* op_method = op_enumeration_values->first;
1075 7054 100         while ((op_method = SPVM_OP_sibling(compiler, op_method))) {
1076 3805           SPVM_METHOD* method = op_method->uv.method;
1077              
1078             // Enumeration attributes
1079 3805           int32_t access_control_attributes_count = 0;
1080 3805 50         if (op_attributes) {
1081 3805           SPVM_OP* op_attribute = op_attributes->first;
1082 4345 100         while ((op_attribute = SPVM_OP_sibling(compiler, op_attribute))) {
1083 540           SPVM_ATTRIBUTE* attribute = op_attribute->uv.attribute;
1084            
1085 540           switch (attribute->id) {
1086             case SPVM_ATTRIBUTE_C_ID_PRIVATE: {
1087 536           method->access_control_type = SPVM_ATTRIBUTE_C_ID_PRIVATE;
1088 536           access_control_attributes_count++;
1089 536           break;
1090             }
1091             case SPVM_ATTRIBUTE_C_ID_PROTECTED: {
1092 0           method->access_control_type = SPVM_ATTRIBUTE_C_ID_PROTECTED;
1093 0           access_control_attributes_count++;
1094 0           break;
1095             }
1096             case SPVM_ATTRIBUTE_C_ID_PUBLIC: {
1097 2           method->access_control_type = SPVM_ATTRIBUTE_C_ID_PUBLIC;
1098 2           access_control_attributes_count++;
1099 2           break;
1100             }
1101             default: {
1102 2           SPVM_COMPILER_error(compiler, "Invalid enumeration attribute \"%s\".\n at %s line %d", SPVM_ATTRIBUTE_get_name(compiler, attribute->id), op_attributes->file, op_attributes->line);
1103             }
1104             }
1105             }
1106 3805 100         if (access_control_attributes_count > 1) {
1107 2           SPVM_COMPILER_error(compiler, "Only one of enumeration attributes \"private\", \"protected\" or \"public\" can be specified.\n at %s line %d", op_method->file, op_method->line);
1108             }
1109             }
1110            
1111             // The default of the access controll of the enumeration is public.
1112 3805 50         if (method->access_control_type == SPVM_ATTRIBUTE_C_ID_UNKNOWN) {
1113 0           method->access_control_type = SPVM_ATTRIBUTE_C_ID_PUBLIC;
1114             }
1115             }
1116            
1117             // Reset enum information
1118 3249           compiler->current_enum_value = 0;
1119            
1120 3249           return op_enumeration;
1121             }
1122              
1123 3805           SPVM_OP* SPVM_OP_build_enumeration_value(SPVM_COMPILER* compiler, SPVM_OP* op_name, SPVM_OP* op_constant) {
1124            
1125 3805 100         if (op_constant) {
1126            
1127 3754           SPVM_CONSTANT* constant = op_constant->uv.constant;
1128            
1129 3754 50         if (constant->type->dimension == 0 && constant->type->basic_type->id == SPVM_NATIVE_C_BASIC_TYPE_ID_INT) {
    100          
1130 3753           compiler->current_enum_value = constant->value.ival;
1131             }
1132             else {
1133 1           SPVM_COMPILER_error(compiler, "The value of the enumeration must be int type.\n at %s line %d", op_constant->file, op_constant->line);
1134             }
1135            
1136 3754           compiler->current_enum_value++;
1137             }
1138             else {
1139 51           op_constant = SPVM_OP_new_op_constant_int(compiler, (int32_t)compiler->current_enum_value, op_name->file, op_name->line);
1140            
1141 51           compiler->current_enum_value++;
1142             }
1143            
1144             // Return
1145 3805           SPVM_OP* op_return = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_RETURN, op_name->file, op_name->line);
1146 3805           SPVM_OP_insert_child(compiler, op_return, op_return->last, op_constant);
1147            
1148             // Statements
1149 3805           SPVM_OP* op_list_statements = SPVM_OP_new_op_list(compiler, op_name->file, op_name->line);
1150 3805           SPVM_OP_insert_child(compiler, op_list_statements, op_list_statements->last, op_return);
1151              
1152             // Block
1153 3805           SPVM_OP* op_block = SPVM_OP_new_op_block(compiler, op_name->file, op_name->line);
1154 3805           SPVM_OP_insert_child(compiler, op_block, op_block->last, op_list_statements);
1155            
1156             // method
1157 3805           SPVM_OP* op_method = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_METHOD, op_name->file, op_name->line);
1158 3805           op_method->file = op_name->file;
1159 3805           op_method->line = op_name->line;
1160            
1161             // Type
1162 3805           SPVM_TYPE* return_type = op_constant->uv.constant->type;
1163 3805           SPVM_OP* op_return_type = SPVM_OP_new_op_type(compiler, return_type->unresolved_basic_type_name, return_type->basic_type, return_type->dimension, return_type->flag, op_name->file, op_name->line);
1164            
1165 3805           SPVM_OP* op_list_attributes = SPVM_OP_new_op_list(compiler, compiler->current_file, compiler->current_line);
1166 3805           SPVM_OP* op_attribute_static = SPVM_OP_new_op_attribute(compiler, SPVM_ATTRIBUTE_C_ID_STATIC, compiler->current_file, compiler->current_line);
1167 3805           SPVM_OP_insert_child(compiler, op_list_attributes, op_list_attributes->first, op_attribute_static);
1168            
1169             // Build method
1170 3805           op_method = SPVM_OP_build_method_definition(compiler, op_method, op_name, op_return_type, NULL, op_list_attributes, op_block, NULL, 0, 0);
1171            
1172             // Set constant
1173 3805           op_method->uv.method->enum_value = op_constant->uv.constant->value.ival;
1174            
1175             // Method is enumeration
1176 3805           op_method->uv.method->is_enum = 1;
1177            
1178 3805           return op_method;
1179             }
1180              
1181 8179           SPVM_OP* SPVM_OP_build_class_var_definition(SPVM_COMPILER* compiler, SPVM_OP* op_class_var, SPVM_OP* op_name, SPVM_OP* op_attributes, SPVM_OP* op_type) {
1182            
1183 8179           SPVM_CLASS_VAR* class_var = SPVM_CLASS_VAR_new(compiler);
1184            
1185 8179           const char* name = op_name->uv.name;
1186 8179           class_var->name = op_name->uv.name;
1187            
1188 8179 100         if (strstr(name, "::")) {
1189 1           SPVM_COMPILER_error(compiler, "The class varaible name \"%s\" cannnot contain \"::\".\n at %s line %d", class_var->name, op_name->file, op_name->line);
1190             }
1191            
1192 8179           class_var->op_name = op_name;
1193 8179           class_var->type = op_type->uv.type;
1194 8179           class_var->op_class_var = op_class_var;
1195              
1196 8179           op_class_var->uv.class_var = class_var;
1197              
1198             // Class variable attributes
1199 8179 50         if (op_attributes) {
1200 8179           int32_t field_method_attributes_count = 0;
1201 8179           int32_t access_control_attributes_count = 0;
1202 8179           SPVM_OP* op_attribute = op_attributes->first;
1203 15930 100         while ((op_attribute = SPVM_OP_sibling(compiler, op_attribute))) {
1204 7751           SPVM_ATTRIBUTE* attribute = op_attribute->uv.attribute;
1205            
1206 7751           switch (attribute->id) {
1207             case SPVM_ATTRIBUTE_C_ID_PRIVATE: {
1208 65           class_var->access_control_type = SPVM_ATTRIBUTE_C_ID_PRIVATE;
1209 65           access_control_attributes_count++;
1210 65           break;
1211             }
1212             case SPVM_ATTRIBUTE_C_ID_PROTECTED: {
1213 0           class_var->access_control_type = SPVM_ATTRIBUTE_C_ID_PROTECTED;
1214 0           access_control_attributes_count++;
1215 0           break;
1216             }
1217             case SPVM_ATTRIBUTE_C_ID_PUBLIC: {
1218 67           class_var->access_control_type = SPVM_ATTRIBUTE_C_ID_PUBLIC;
1219 67           access_control_attributes_count++;
1220 67           break;
1221             }
1222             case SPVM_ATTRIBUTE_C_ID_RW: {
1223 89           class_var->has_setter = 1;
1224 89           class_var->has_getter = 1;
1225 89           field_method_attributes_count++;
1226 89           break;
1227             }
1228             case SPVM_ATTRIBUTE_C_ID_RO: {
1229 7528           class_var->has_getter = 1;
1230 7528           field_method_attributes_count++;
1231 7528           break;
1232             }
1233             case SPVM_ATTRIBUTE_C_ID_WO: {
1234 1           class_var->has_setter = 1;
1235 1           field_method_attributes_count++;
1236 1           break;
1237             }
1238             default: {
1239 1           SPVM_COMPILER_error(compiler, "Invalid class variable attribute \"%s\".\n at %s line %d", SPVM_ATTRIBUTE_get_name(compiler, attribute->id), op_attributes->file, op_attributes->line);
1240             }
1241             }
1242 7751 100         if (field_method_attributes_count > 1) {
1243 2           SPVM_COMPILER_error(compiler, "Only one of class variable attributes \"rw\", \"ro\", \"wo\" can be specifed.\n at %s line %d", op_class_var->file, op_class_var->line);
1244             }
1245 7751 100         if (access_control_attributes_count > 1) {
1246 1           SPVM_COMPILER_error(compiler, "Only one of class variable attributes \"private\", \"protected\" or \"public\" can be specified.\n at %s line %d", op_class_var->file, op_class_var->line);
1247             }
1248             }
1249             }
1250            
1251             // The default of the access controll of the class variable is private.
1252 8179 100         if (class_var->access_control_type == SPVM_ATTRIBUTE_C_ID_UNKNOWN) {
1253 8048           class_var->access_control_type = SPVM_ATTRIBUTE_C_ID_PRIVATE;
1254             }
1255            
1256 8179           return op_class_var;
1257             }
1258              
1259 27795           SPVM_OP* SPVM_OP_build_field_definition(SPVM_COMPILER* compiler, SPVM_OP* op_field, SPVM_OP* op_name_field, SPVM_OP* op_attributes, SPVM_OP* op_type) {
1260              
1261             // Create field information
1262 27795           SPVM_FIELD* field = SPVM_FIELD_new(compiler);
1263              
1264             // Field Name
1265 27795           field->op_name = op_name_field;
1266 27795           field->name = op_name_field->uv.name;
1267              
1268 27795 100         if (strstr(field->op_name->uv.name, "::")) {
1269 2           SPVM_COMPILER_error(compiler, "The field name \"%s\" cannnot contain \"::\".\n at %s line %d", field->name, op_name_field->file, op_name_field->line);
1270             }
1271              
1272             // Type
1273 27795           field->type = op_type->uv.type;
1274            
1275             // Set field informaiton
1276 27795           op_field->uv.field = field;
1277              
1278             // Field attributes
1279 27795 50         if (op_attributes) {
1280 27795           SPVM_OP* op_attribute = op_attributes->first;
1281 27795           int32_t field_method_attributes_count = 0;
1282 27795           int32_t access_control_attributes_count = 0;
1283 46694 100         while ((op_attribute = SPVM_OP_sibling(compiler, op_attribute))) {
1284 18899           SPVM_ATTRIBUTE* attribute = op_attribute->uv.attribute;
1285            
1286 18899           switch (attribute->id) {
1287             case SPVM_ATTRIBUTE_C_ID_PRIVATE: {
1288 35           field->access_control_type = SPVM_ATTRIBUTE_C_ID_PRIVATE;
1289 35           access_control_attributes_count++;
1290 35           break;
1291             }
1292             case SPVM_ATTRIBUTE_C_ID_PROTECTED: {
1293 4           field->access_control_type = SPVM_ATTRIBUTE_C_ID_PROTECTED;
1294 4           access_control_attributes_count++;
1295 4           break;
1296             }
1297             case SPVM_ATTRIBUTE_C_ID_PUBLIC: {
1298 1346           field->access_control_type = SPVM_ATTRIBUTE_C_ID_PUBLIC;
1299 1346           access_control_attributes_count++;
1300 1346           break;
1301             }
1302             case SPVM_ATTRIBUTE_C_ID_RW: {
1303 1423           field->has_setter = 1;
1304 1423           field->has_getter = 1;
1305 1423           field_method_attributes_count++;
1306 1423           break;
1307             }
1308             case SPVM_ATTRIBUTE_C_ID_RO: {
1309 16089           field->has_getter = 1;
1310 16089           field_method_attributes_count++;
1311 16089           break;
1312             }
1313             case SPVM_ATTRIBUTE_C_ID_WO: {
1314 1           field->has_setter = 1;
1315 1           field_method_attributes_count++;
1316 1           break;
1317             }
1318             default: {
1319 1           SPVM_COMPILER_error(compiler, "Invalid field attribute \"%s\".\n at %s line %d", SPVM_ATTRIBUTE_get_name(compiler, attribute->id), op_attributes->file, op_attributes->line);
1320             }
1321             }
1322            
1323 18899 100         if (field_method_attributes_count > 1) {
1324 2           SPVM_COMPILER_error(compiler, "Only one of field attributes \"rw\", \"ro\" or \"wo\" can be specifed.\n at %s line %d", op_field->file, op_field->line);
1325             }
1326 18899 100         if (access_control_attributes_count > 1) {
1327 1           SPVM_COMPILER_error(compiler, "Only one of field attributes \"private\", \"protected\" or \"public\" can be specified.\n at %s line %d", op_field->file, op_field->line);
1328             }
1329             }
1330             }
1331            
1332 27795           field->op_field = op_field;
1333            
1334 27795           return op_field;
1335             }
1336              
1337 310960           SPVM_OP* SPVM_OP_build_method_definition(SPVM_COMPILER* compiler, SPVM_OP* op_method, SPVM_OP* op_name_method, SPVM_OP* op_return_type, SPVM_OP* op_args, SPVM_OP* op_attributes, SPVM_OP* op_block, SPVM_OP* op_anon_method_fields, int32_t is_init, int32_t is_anon) {
1338 310960           SPVM_METHOD* method = SPVM_METHOD_new(compiler);
1339            
1340             // Is anon method
1341 310960 100         if (is_anon) {
1342 8213           method->is_anon = 1;
1343             }
1344            
1345 310960 100         if (op_name_method == NULL) {
1346 13088           SPVM_STRING* anon_method_name_string = SPVM_STRING_new(compiler, "", strlen(""));
1347 13088           const char* anon_method_name = anon_method_name_string->value;
1348 13088           op_name_method = SPVM_OP_new_op_name(compiler, anon_method_name, op_method->file, op_method->line);
1349             }
1350 310960           const char* method_name = op_name_method->uv.name;
1351 310960 100         if (strstr(method_name, "::")) {
1352 2           SPVM_COMPILER_error(compiler, "The method name \"%s\" cannnot contain \"::\".\n at %s line %d", method_name, op_name_method->file, op_name_method->line);
1353             }
1354            
1355             // Block is method block
1356 310960 100         if (op_block) {
1357 238905           op_block->uv.block->id = SPVM_BLOCK_C_ID_METHOD;
1358             }
1359            
1360             // Create method information
1361 310960           method->op_name = op_name_method;
1362            
1363 310960           method->name = method->op_name->uv.name;
1364            
1365 310960           method->is_init = is_init;
1366 310960 100         if (!is_init && strcmp(method_name, "INIT") == 0) {
    100          
1367 2           SPVM_COMPILER_error(compiler, "\"INIT\" cannnot be used as a method name.\n at %s line %d", op_name_method->file, op_name_method->line);
1368             }
1369              
1370             // Method attributes
1371 310960           int32_t access_control_attributes_count = 0;
1372 310960 100         if (op_attributes) {
1373 292026           SPVM_OP* op_attribute = op_attributes->first;
1374 613224 100         while ((op_attribute = SPVM_OP_sibling(compiler, op_attribute))) {
1375 321198           SPVM_ATTRIBUTE* attribute = op_attribute->uv.attribute;
1376            
1377 321198           switch (attribute->id) {
1378             case SPVM_ATTRIBUTE_C_ID_PRIVATE: {
1379 14891           method->access_control_type = SPVM_ATTRIBUTE_C_ID_PRIVATE;
1380 14891           access_control_attributes_count++;
1381 14891           break;
1382             }
1383             case SPVM_ATTRIBUTE_C_ID_PROTECTED: {
1384 224           method->access_control_type = SPVM_ATTRIBUTE_C_ID_PROTECTED;
1385 224           access_control_attributes_count++;
1386 224           break;
1387             }
1388             case SPVM_ATTRIBUTE_C_ID_PUBLIC: {
1389 0           method->access_control_type = SPVM_ATTRIBUTE_C_ID_PUBLIC;
1390 0           access_control_attributes_count++;
1391 0           break;
1392             }
1393             case SPVM_ATTRIBUTE_C_ID_PRECOMPILE: {
1394 39360           method->is_precompile = 1;
1395 39360           break;
1396             }
1397             case SPVM_ATTRIBUTE_C_ID_REQUIRED: {
1398 5847           method->is_required = 1;
1399 5847           break;
1400             }
1401             case SPVM_ATTRIBUTE_C_ID_NATIVE: {
1402 66056           method->is_native = 1;
1403 66056           break;
1404             }
1405             case SPVM_ATTRIBUTE_C_ID_STATIC: {
1406 194819           method->is_class_method = 1;
1407 194819           break;
1408             }
1409             default: {
1410 1           SPVM_COMPILER_error(compiler, "Invalid method attribute \"%s\".\n at %s line %d", SPVM_ATTRIBUTE_get_name(compiler, attribute->id), op_attributes->file, op_attributes->line);
1411             }
1412             }
1413             }
1414            
1415 292026 100         if (method->is_native && method->is_precompile) {
    100          
1416 1           SPVM_COMPILER_error(compiler, "Only one of method attributes \"native\" and \"precompile\" can be specified.\n at %s line %d", op_attributes->file, op_attributes->line);
1417             }
1418 292026 50         if (access_control_attributes_count > 1) {
1419 0           SPVM_COMPILER_error(compiler, "Only one of method attributes \"private\", \"protected\" or \"public\" can be specified.\n at %s line %d", op_method->file, op_method->line);
1420             }
1421             }
1422            
1423             // The default of the access controll of the method is publice.
1424 310960 100         if (method->access_control_type == SPVM_ATTRIBUTE_C_ID_UNKNOWN) {
1425 295845           method->access_control_type = SPVM_ATTRIBUTE_C_ID_PUBLIC;
1426             }
1427            
1428             // Native method cannnot have block
1429 310960 100         if ((method->is_native) && op_block) {
    100          
1430 2           SPVM_COMPILER_error(compiler, "The native method cannnot have the block.\n at %s line %d", op_block->file, op_block->line);
1431             }
1432            
1433             // method args
1434 310960 100         if (!op_args) {
1435 40745           op_args = SPVM_OP_new_op_list(compiler, op_method->file, op_method->line);
1436             }
1437            
1438             // Add $self : self before the first argument
1439 310960 100         if (!method->is_class_method) {
1440 116141           SPVM_OP* op_arg_var_name_self = SPVM_OP_new_op_name(compiler, "$self", op_method->file, op_method->line);
1441 116141           SPVM_OP* op_arg_var_self = SPVM_OP_new_op_var(compiler, op_arg_var_name_self);
1442 116141           SPVM_OP* op_self_type = SPVM_OP_new_op_unresolved_type(compiler, NULL, 0, 0, op_method->file, op_method->line);
1443 116141           SPVM_TYPE* self_type = op_self_type->uv.type;
1444 116141           SPVM_OP* op_arg_self = SPVM_OP_build_arg(compiler, op_arg_var_self, op_self_type, NULL, NULL);
1445 116141           SPVM_OP_insert_child(compiler, op_args, op_args->first, op_arg_self);
1446             }
1447              
1448             // Add method arguments
1449             {
1450 310960           int32_t found_optional_arg = 0;
1451 310960           int32_t required_args_length = 0;
1452 310960           int32_t args_length = 0;
1453 310960           SPVM_OP* op_arg = op_args->first;
1454 821828 100         while ((op_arg = SPVM_OP_sibling(compiler, op_arg))) {
1455 510868           SPVM_VAR_DECL* arg_var_decl = op_arg->uv.var->var_decl;
1456 510868 100         if (!found_optional_arg) {
1457 482487 100         if (arg_var_decl->op_optional_arg_default) {
1458 32405           found_optional_arg = 1;
1459             }
1460             else {
1461 450082           required_args_length++;
1462             }
1463             }
1464 510868           args_length++;
1465             }
1466 310960           method->args_length = args_length;
1467 310960           method->required_args_length = required_args_length;
1468             }
1469              
1470             // Capture variables
1471 310960 100         if (op_anon_method_fields) {
1472 22           SPVM_OP* op_anon_method_field = op_anon_method_fields->first;
1473 57 100         while ((op_anon_method_field = SPVM_OP_sibling(compiler, op_anon_method_field))) {
1474 35           SPVM_LIST_push(method->anon_method_fields, op_anon_method_field->uv.field);
1475             }
1476             }
1477            
1478             // Variable declarations of arguments
1479 310960           SPVM_OP* op_arg = op_args->first;
1480 821828 100         while ((op_arg = SPVM_OP_sibling(compiler, op_arg))) {
1481 510868           SPVM_LIST_push(method->var_decls, op_arg->uv.var->var_decl);
1482             }
1483              
1484             // return type
1485 310960           method->return_type = op_return_type->uv.type;
1486            
1487 310960 100         if (strcmp(method->op_name->uv.name, "DESTROY") == 0) {
1488 763           method->is_destructor = 1;
1489            
1490             // DESTROY return type must be void
1491 763 50         if (!(method->return_type->dimension == 0 && method->return_type->basic_type->id == SPVM_NATIVE_C_BASIC_TYPE_ID_VOID)) {
    100          
1492 2           SPVM_COMPILER_error(compiler, "The return type of the DESTROY destructor method must be the void type.\n at %s line %d", op_method->file, op_method->line);
1493             }
1494            
1495             // DESTROY is instance method
1496 763 100         if (method->is_class_method) {
1497 2           SPVM_COMPILER_error(compiler, "The DESTROY destructor method must be an instance method.\n at %s line %d", op_method->file, op_method->line);
1498             }
1499              
1500             // DESTROY doesn't have arguments without invocant
1501 763 100         if (method->args_length != 1) {
1502 4           SPVM_COMPILER_error(compiler, "The DESTROY destructor method cannnot have arguments.\n at %s line %d", op_method->file, op_method->line);
1503             }
1504             }
1505            
1506 310960 100         if (op_block) {
1507              
1508 238905           SPVM_OP* op_list_statement = op_block->first;
1509              
1510             // Add variable declarations before the first of the statements
1511 629452 100         for (int32_t i = method->args_length - 1; i >= 0; i--) {
1512 390547           SPVM_VAR_DECL* arg_var_decl = SPVM_LIST_get(method->var_decls, i);
1513 390547 50         assert(arg_var_decl);
1514 390547           SPVM_OP* op_name_var = SPVM_OP_new_op_name(compiler, arg_var_decl->var->name, arg_var_decl->op_var_decl->file, arg_var_decl->op_var_decl->line);
1515 390547           SPVM_OP* op_var = SPVM_OP_new_op_var(compiler, op_name_var);
1516 390547           op_var->uv.var->var_decl = arg_var_decl;
1517 390547           op_var->uv.var->is_declaration = 1;
1518 390547           op_var->uv.var->var_decl = arg_var_decl;
1519              
1520 390547           SPVM_OP_insert_child(compiler, op_list_statement, op_list_statement->first, op_var);
1521             }
1522              
1523             // Add condition_flag variable to first of block
1524             {
1525 238905           char* name = "$.condition_flag";
1526 238905           SPVM_OP* op_name = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_NAME, op_list_statement->file, op_list_statement->last->line + 1);
1527 238905           op_name->uv.name = name;
1528 238905           SPVM_OP* op_var = SPVM_OP_build_var(compiler, op_name);
1529 238905           SPVM_OP* op_var_decl = SPVM_OP_new_op_var_decl(compiler, op_list_statement->file, op_list_statement->last->line + 1);
1530 238905           SPVM_OP* op_type = SPVM_OP_new_op_int_type(compiler, op_list_statement->file, op_list_statement->line);
1531 238905           op_var = SPVM_OP_build_var_decl(compiler, op_var_decl, op_var, op_type, NULL);
1532 238905           SPVM_OP_insert_child(compiler, op_list_statement, op_list_statement->first, op_var);
1533             }
1534              
1535             // Add return statement after the last of the statements
1536             {
1537 238905           SPVM_OP* op_return = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_RETURN, op_list_statement->file, op_list_statement->last->line + 1);
1538 238905           SPVM_TYPE* return_type = method->return_type;
1539 238905 100         if (SPVM_TYPE_is_void_type(compiler, return_type->basic_type->id, return_type->dimension, return_type->flag)) {
1540 92129           SPVM_OP_insert_child(compiler, op_list_statement, op_list_statement->last, op_return);
1541             }
1542             else {
1543             // Return variable name
1544 146776           char* name = "$.return";
1545 146776           SPVM_OP* op_name = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_NAME, op_list_statement->file, op_list_statement->last->line + 1);
1546 146776           op_name->uv.name = name;
1547 146776           SPVM_OP* op_var = SPVM_OP_build_var(compiler, op_name);
1548 146776           SPVM_OP* op_var_decl = SPVM_OP_new_op_var_decl(compiler, op_list_statement->file, op_list_statement->last->line + 1);
1549 146776           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, return_type->unresolved_basic_type_name, return_type->basic_type, return_type->dimension, return_type->flag, op_list_statement->file, op_list_statement->last->line + 1);
1550            
1551 146776           op_var = SPVM_OP_build_var_decl(compiler, op_var_decl, op_var, op_type, NULL);
1552 146776           SPVM_OP_insert_child(compiler, op_return, op_return->last, op_var);
1553 146776           SPVM_OP_insert_child(compiler, op_list_statement, op_list_statement->last, op_return);
1554             }
1555             }
1556             }
1557            
1558             // Save block
1559 310960           method->op_block = op_block;
1560            
1561 310960           method->op_method = op_method;
1562            
1563 310960           op_method->uv.method = method;
1564            
1565 310960           return op_method;
1566             }
1567              
1568 510868           SPVM_OP* SPVM_OP_build_arg(SPVM_COMPILER* compiler, SPVM_OP* op_var, SPVM_OP* op_type, SPVM_OP* op_attributes, SPVM_OP* op_optional_arg_default) {
1569            
1570 510868           SPVM_OP* op_var_decl = SPVM_OP_new_op_var_decl_arg(compiler, op_var->file, op_var->line);
1571            
1572 510868           op_var_decl->uv.var_decl->op_optional_arg_default = op_optional_arg_default;
1573            
1574 510868           op_var = SPVM_OP_build_var_decl(compiler, op_var_decl, op_var, op_type, op_attributes);
1575            
1576 510868           op_type->uv.type->resolved_in_ast = 0;
1577            
1578 510868           return op_var;
1579             }
1580              
1581 8213           SPVM_OP* SPVM_OP_build_anon_method(SPVM_COMPILER* compiler, SPVM_OP* op_method) {
1582            
1583             // Class
1584 8213           SPVM_OP* op_class = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_CLASS, op_method->file, op_method->line);
1585            
1586 8213           SPVM_OP* op_class_block = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_CLASS_BLOCK, op_method->file, op_method->line);
1587 8213           SPVM_OP* op_list_definitions = SPVM_OP_new_op_list(compiler, compiler->current_file, compiler->current_line);
1588 8213           SPVM_OP_insert_child(compiler, op_list_definitions, op_list_definitions->last, op_method);
1589 8213           SPVM_OP_insert_child(compiler, op_class_block, op_class_block->last, op_list_definitions);
1590            
1591             // int32_t max length is 10(2147483647)
1592 8213           int32_t int32_max_length = 10;
1593            
1594             // Create anon method class name
1595             // If Foo::Bar anon method is defined line 123, method keyword start pos 32, the anon method class name become Foo::Bar::anon::123::32. This is uniqe in whole program.
1596 8213           const char* outer_basic_type_name = compiler->current_outer_class_name;
1597 8213           int32_t anon_method_defined_line = op_method->line;
1598 8213           int32_t anon_method_defined_column = op_method->column;
1599 8213           int32_t anon_method_basic_type_name_length = 6 + strlen(outer_basic_type_name) + 2 + int32_max_length + 2 + int32_max_length;
1600            
1601             // Anon class name
1602 8213           char* name_basic_type_tmp = SPVM_ALLOCATOR_alloc_memory_block_permanent(compiler->current_each_compile_allocator, anon_method_basic_type_name_length + 1);
1603 8213           sprintf(name_basic_type_tmp, "%s::anon::%d::%d", outer_basic_type_name, anon_method_defined_line, anon_method_defined_column);
1604            
1605 8213           SPVM_STRING* name_basic_type_string = SPVM_STRING_new(compiler, name_basic_type_tmp, strlen(name_basic_type_tmp));
1606 8213           const char* name_basic_type = name_basic_type_string->value;
1607            
1608 8213           SPVM_OP* op_name_basic_type = SPVM_OP_new_op_name(compiler, name_basic_type, op_method->file, op_method->line);
1609 8213           SPVM_OP* op_type = SPVM_OP_build_basic_type(compiler, op_name_basic_type);
1610            
1611 8213           op_method->uv.method->outer_basic_type_name = outer_basic_type_name;
1612            
1613 8213           SPVM_OP_build_class(compiler, op_class, op_type, op_class_block, NULL, NULL);
1614            
1615             // Type
1616 8213           SPVM_OP* op_name_new = SPVM_OP_new_op_name(compiler, name_basic_type, op_method->file, op_method->line);
1617 8213           SPVM_OP* op_type_new = SPVM_OP_build_basic_type(compiler, op_name_new);
1618            
1619             // New
1620 8213           SPVM_OP* op_new = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_NEW, op_method->file, op_method->line);
1621            
1622 8213           SPVM_OP* op_anon_method = SPVM_OP_build_new(compiler, op_new, op_type_new, NULL);
1623            
1624 8213           SPVM_LIST_push(compiler->current_anon_op_types, op_type);
1625            
1626 8213           return op_anon_method;
1627             }
1628              
1629 35           SPVM_OP* SPVM_OP_build_anon_method_field_definition(SPVM_COMPILER* compiler, SPVM_OP* op_field_definition, SPVM_OP* op_default) {
1630            
1631 35           op_field_definition->uv.field->op_anon_method_field_default = op_default;
1632            
1633 35           return op_field_definition;
1634             }
1635              
1636 36940           SPVM_OP* SPVM_OP_build_init_block(SPVM_COMPILER* compiler, SPVM_OP* op_init, SPVM_OP* op_block) {
1637            
1638 36940           SPVM_OP* op_method = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_METHOD, op_init->file, op_init->line);
1639 36940           SPVM_STRING* method_name_string = SPVM_STRING_new(compiler, "INIT", strlen("INIT"));
1640 36940           const char* method_name = method_name_string->value;
1641 36940           SPVM_OP* op_method_name = SPVM_OP_new_op_name(compiler, "INIT", op_init->file, op_init->line);
1642 36940           SPVM_OP* op_void_type = SPVM_OP_new_op_void_type(compiler, op_init->file, op_init->line);
1643            
1644 36940           SPVM_OP* op_list_attributes = SPVM_OP_new_op_list(compiler, op_init->file, op_init->line);
1645 36940           SPVM_OP* op_attribute_static = SPVM_OP_new_op_attribute(compiler, SPVM_ATTRIBUTE_C_ID_STATIC, op_init->file, op_init->line);
1646 36940           SPVM_OP_insert_child(compiler, op_list_attributes, op_list_attributes->first, op_attribute_static);
1647            
1648 36940           int32_t is_init = 1;
1649 36940           SPVM_OP_build_method_definition(compiler, op_method, op_method_name, op_void_type, NULL, op_list_attributes, op_block, NULL, is_init, 0);
1650            
1651 36940           return op_method;
1652             }
1653              
1654 4736152           SPVM_OP* SPVM_OP_build_var(SPVM_COMPILER* compiler, SPVM_OP* op_var_name) {
1655            
1656 4736152           SPVM_OP* op_var = SPVM_OP_new_op_var(compiler, op_var_name);
1657            
1658 4736152           return op_var;
1659             }
1660              
1661 2075           SPVM_OP* SPVM_OP_build_eval(SPVM_COMPILER* compiler, SPVM_OP* op_eval, SPVM_OP* op_eval_block) {
1662            
1663 2075           SPVM_OP_insert_child(compiler, op_eval, op_eval->last, op_eval_block);
1664            
1665             // eval block
1666 2075           op_eval_block->uv.block->id = SPVM_BLOCK_C_ID_EVAL;
1667            
1668 2075           return op_eval;
1669             }
1670              
1671 1675           SPVM_OP* SPVM_OP_build_switch_statement(SPVM_COMPILER* compiler, SPVM_OP* op_switch, SPVM_OP* op_switch_operand, SPVM_OP* op_switch_block) {
1672            
1673 1675           SPVM_OP* op_switch_condition = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_SWITCH_CONDITION, op_switch_operand->file, op_switch_operand->line);
1674 1675           SPVM_OP_insert_child(compiler, op_switch_condition, op_switch_condition->last, op_switch_operand);
1675            
1676 1675           SPVM_OP* op_switch_condition_do_nothing = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_DO_NOTHING, op_switch_condition->file, op_switch_condition->line);
1677 1675           SPVM_OP_insert_child(compiler, op_switch_condition_do_nothing, op_switch_condition_do_nothing->last, op_switch_condition);
1678            
1679 1675           SPVM_OP_insert_child(compiler, op_switch, op_switch->last, op_switch_condition_do_nothing);
1680 1675           SPVM_OP_insert_child(compiler, op_switch, op_switch->last, op_switch_block);
1681            
1682 1675           SPVM_SWITCH_INFO* switch_info = SPVM_SWITCH_INFO_new(compiler);
1683 1675           op_switch->uv.switch_info = switch_info;
1684            
1685 1675           op_switch_condition->uv.switch_info = switch_info;
1686            
1687 1675           return op_switch;
1688             }
1689              
1690 1675           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) {
1691            
1692 1675 50         if (op_case_statements->id != SPVM_OP_C_ID_LIST) {
1693 0           SPVM_OP* op_list = SPVM_OP_new_op_list(compiler, op_case_statements->file, op_case_statements->line);
1694 0           SPVM_OP_insert_child(compiler, op_list, op_list->last, op_case_statements);
1695 0           op_case_statements = op_list;
1696             }
1697            
1698 1675           SPVM_OP_insert_child(compiler, op_switch_block, op_switch_block->last, op_case_statements);
1699 1675 100         if (op_default_statement) {
1700 576           SPVM_OP_insert_child(compiler, op_switch_block, op_switch_block->last, op_default_statement);
1701             }
1702            
1703 1675           return op_switch_block;
1704             }
1705              
1706 9719           SPVM_OP* SPVM_OP_build_case_statement(SPVM_COMPILER* compiler, SPVM_OP* op_case_info, SPVM_OP* op_case_operand, SPVM_OP* op_block) {
1707            
1708 9719           SPVM_CASE_INFO* case_info = SPVM_CASE_INFO_new(compiler);
1709            
1710 9719           SPVM_OP_insert_child(compiler, op_case_info, op_case_info->last, op_case_operand);
1711            
1712 9719 100         if (op_block) {
1713 7593           SPVM_OP* op_statements = op_block->first;
1714 7593 50         if (op_statements) {
1715 7593           SPVM_OP* op_last_statement = op_statements->last;
1716            
1717             // Add "break" statement if it doesn't exist
1718             {
1719             // No statement
1720 7593 50         if (!op_last_statement) {
1721 0           SPVM_OP* op_break = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_BREAK, op_statements->file, op_statements->line);
1722 0           SPVM_OP_insert_child(compiler, op_statements, op_statements->last, op_break);
1723             }
1724             // The last statement is not "break" statement
1725 7593 100         else if (op_last_statement->id != SPVM_OP_C_ID_BREAK) {
1726 28           SPVM_OP* op_break = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_BREAK, op_last_statement->file, op_last_statement->line + 1);
1727 28           SPVM_OP_insert_child(compiler, op_statements, op_statements->last, op_break);
1728             }
1729             }
1730             }
1731 7593           SPVM_OP_insert_child(compiler, op_case_info, op_case_info->last, op_block);
1732             }
1733            
1734 9719           case_info->op_case_info = op_case_info;
1735            
1736 9719           op_case_info->uv.case_info = case_info;
1737            
1738 9719           return op_case_info;
1739             }
1740              
1741 1654           SPVM_OP* SPVM_OP_build_default_statement(SPVM_COMPILER* compiler, SPVM_OP* op_default, SPVM_OP* op_block) {
1742            
1743 1654 100         if (op_block) {
1744 1634           SPVM_OP_insert_child(compiler, op_default, op_default->last, op_block);
1745             }
1746            
1747 1654           return op_default;
1748             }
1749              
1750 513224           SPVM_OP* SPVM_OP_build_condition(SPVM_COMPILER* compiler, SPVM_OP* op_condition_operand, int32_t is_not) {
1751             // Condition
1752             int32_t id;
1753 513224 100         if (is_not) {
1754 228931           id = SPVM_OP_C_ID_CONDITION_NOT;
1755             }
1756             else {
1757 284293           id = SPVM_OP_C_ID_CONDITION;
1758             }
1759 513224           SPVM_OP* op_condition = SPVM_OP_new_op(compiler, id, op_condition_operand->file, op_condition_operand->line);
1760            
1761 513224 50         if (SPVM_OP_is_comparison_op(compiler, op_condition_operand)) {
1762 0 0         assert(op_condition_operand->moresib == 0);
1763 0           SPVM_OP_insert_child(compiler, op_condition, op_condition->last, op_condition_operand);
1764             }
1765             else {
1766 513224           SPVM_OP* op_assign_bool = SPVM_OP_new_op_assign_bool(compiler, op_condition_operand, op_condition_operand->file, op_condition_operand->line);
1767 513224           SPVM_OP_insert_child(compiler, op_condition, op_condition->last, op_assign_bool);
1768             }
1769            
1770 513224           return op_condition;
1771             }
1772              
1773 43269           SPVM_OP* SPVM_OP_build_for_statement(SPVM_COMPILER* compiler, SPVM_OP* op_for, SPVM_OP* op_init, SPVM_OP* op_condition_operand, SPVM_OP* op_increment, SPVM_OP* op_block_statements) {
1774            
1775             // Loop
1776 43269           SPVM_OP* op_loop = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_LOOP, op_for->file, op_for->line);
1777            
1778             // Condition
1779 43269           SPVM_OP* op_condition = SPVM_OP_build_condition(compiler, op_condition_operand, 1);
1780 43269           op_condition->flag |= SPVM_OP_C_FLAG_CONDITION_LOOP;
1781              
1782             // Set block flag
1783 43269           op_block_statements->uv.block->id = SPVM_BLOCK_C_ID_LOOP_STATEMENTS;
1784              
1785             // Outer block for initialize loop variable
1786 43269           SPVM_OP* op_block_init = SPVM_OP_new_op_block(compiler, op_for->file, op_for->line);
1787 43269           op_block_init->uv.block->id = SPVM_BLOCK_C_ID_LOOP_INIT;
1788            
1789             // Block for increment
1790 43269           SPVM_OP* op_loop_increment = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_LOOP_INCREMENT, op_for->file, op_for->line);
1791              
1792 43269           SPVM_OP_insert_child(compiler, op_loop_increment, op_loop_increment->last, op_increment);
1793              
1794 43269           SPVM_OP_insert_child(compiler, op_block_init, op_block_init->last, op_init);
1795 43269           SPVM_OP_insert_child(compiler, op_block_init, op_block_init->last, op_condition);
1796 43269           SPVM_OP_insert_child(compiler, op_block_init, op_block_init->last, op_block_statements);
1797 43269           SPVM_OP_insert_child(compiler, op_block_init, op_block_init->last, op_loop_increment);
1798            
1799 43269           SPVM_OP_insert_child(compiler, op_loop, op_loop->last, op_block_init);
1800            
1801 43269           return op_loop;
1802             }
1803              
1804 544           SPVM_OP* SPVM_OP_build_foreach_statement(SPVM_COMPILER* compiler, SPVM_OP* op_for, SPVM_OP* op_var_element, SPVM_OP* op_array, SPVM_OP* op_block_statements) {
1805              
1806             /*
1807             {
1808            
1809             my $.array = $array;
1810             my $.array_length = @$.array;
1811             for (my $.i = 0; $.i < $.array_length; $.i++) {
1812             my $element = $.array->[$.i];
1813            
1814             # ...
1815             }
1816             }
1817             */
1818              
1819 544           SPVM_OP* op_var_init_name = SPVM_OP_new_op_name_tmp_var(compiler, op_for->file, op_for->line);
1820 544           SPVM_OP* op_var_init_orig = SPVM_OP_new_op_var(compiler, op_var_init_name);
1821 544           SPVM_OP* op_var_array_name = SPVM_OP_new_op_name(compiler, "$.array", op_for->file, op_for->line);
1822 544           SPVM_OP* op_var_array_orig = SPVM_OP_new_op_var(compiler, op_var_array_name);
1823 544           SPVM_OP* op_var_array_length_name = SPVM_OP_new_op_name(compiler, "$.array_length", op_for->file, op_for->line);
1824 544           SPVM_OP* op_var_array_length_orig = SPVM_OP_new_op_var(compiler, op_var_array_length_name);
1825            
1826             // my $.i
1827 544           SPVM_OP* op_var_init_for_decl = SPVM_OP_clone_op_var(compiler, op_var_init_orig);
1828 544           SPVM_OP* op_var_decl_init = SPVM_OP_new_op_var_decl(compiler, op_for->file, op_for->line);
1829 544           SPVM_OP_build_var_decl(compiler, op_var_decl_init, op_var_init_for_decl, NULL, NULL);
1830            
1831             // 0
1832 544           SPVM_OP* op_constant_zero = SPVM_OP_new_op_constant_int(compiler, 0, op_for->file, op_for->line);
1833            
1834             // my $.i = 0
1835 544           SPVM_OP* op_assign_init = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_for->file, op_for->line);
1836 544           SPVM_OP_build_assign(compiler, op_assign_init, op_var_init_for_decl, op_constant_zero);
1837            
1838             // my $.array
1839 544           SPVM_OP* op_var_array_for_assign = SPVM_OP_clone_op_var(compiler, op_var_array_orig);
1840 544           SPVM_OP* op_var_decl_array = SPVM_OP_new_op_var_decl(compiler, op_for->file, op_for->line);
1841 544           SPVM_OP_build_var_decl(compiler, op_var_decl_array, op_var_array_for_assign, NULL, NULL);
1842            
1843             // my $.array = $array
1844 544           SPVM_OP* op_assign_array = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_for->file, op_for->line);
1845 544           SPVM_OP_build_assign(compiler, op_assign_array, op_var_array_for_assign, op_array);
1846            
1847             // @$.array
1848 544           SPVM_OP* op_var_array_for_length = SPVM_OP_clone_op_var(compiler, op_var_array_orig);
1849 544           SPVM_OP* op_array_length = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ARRAY_LENGTH, compiler->current_file, compiler->current_line);
1850 544           SPVM_OP_build_array_length(compiler, op_array_length, op_var_array_for_length);
1851            
1852             // my $.array_length
1853 544           SPVM_OP* op_var_array_length_for_assign = SPVM_OP_clone_op_var(compiler, op_var_array_length_orig);
1854 544           SPVM_OP* op_var_decl_array_length = SPVM_OP_new_op_var_decl(compiler, op_for->file, op_for->line);
1855 544           SPVM_OP_build_var_decl(compiler, op_var_decl_array_length, op_var_array_length_for_assign, NULL, NULL);
1856            
1857             // my $.array_length = @$.array
1858 544           SPVM_OP* op_assign_array_length = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_for->file, op_for->line);
1859 544           SPVM_OP_build_assign(compiler, op_assign_array_length, op_var_array_length_for_assign, op_array_length);
1860            
1861             // $.i < $array_length
1862 544           SPVM_OP* op_var_init_for_condition = SPVM_OP_clone_op_var(compiler, op_var_init_orig);
1863 544           SPVM_OP* op_var_array_length_for_condition = SPVM_OP_clone_op_var(compiler, op_var_array_length_orig);
1864 544           SPVM_OP* op_numlt = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_NUMERIC_LT, op_for->file, op_for->line);
1865 544           SPVM_OP* op_numlt_comparison = SPVM_OP_build_comparison_op(compiler, op_numlt, op_var_init_for_condition, op_var_array_length_for_condition);
1866            
1867             // $.array->[$.i]
1868 544           SPVM_OP* op_var_init_for_array_access = SPVM_OP_clone_op_var(compiler, op_var_init_orig);
1869 544           SPVM_OP* op_var_array_for_array_access = SPVM_OP_clone_op_var(compiler, op_var_array_orig);
1870 544           SPVM_OP* op_array_access = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ARRAY_ACCESS, compiler->current_file, compiler->current_line);
1871 544           op_array_access = SPVM_OP_build_array_access(compiler, op_array_access, op_var_array_for_array_access, op_var_init_for_array_access);
1872            
1873             // my $element = $.array->[$.i]
1874 544           SPVM_OP* op_assign_element = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_for->file, op_for->line);
1875 544           SPVM_OP_build_assign(compiler, op_assign_element, op_var_element, op_array_access);
1876            
1877             // $.i
1878 544           SPVM_OP* op_var_init_for_increament = SPVM_OP_clone_op_var(compiler, op_var_init_orig);
1879            
1880             // ++$.i;
1881 544           SPVM_OP* op_inc_increament = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_PRE_INC, op_for->file, op_for->line);
1882 544           op_inc_increament = SPVM_OP_build_inc(compiler, op_inc_increament, op_var_init_for_increament);
1883            
1884 544           SPVM_OP* op_statements = op_block_statements->first;
1885 544 50         if (!op_statements) {
1886 0           op_statements = SPVM_OP_new_op_list(compiler, op_for->file, op_for->line);
1887 0           SPVM_OP_insert_child(compiler, op_block_statements, op_block_statements->first, op_statements);
1888             }
1889 544           SPVM_OP_insert_child(compiler, op_statements, op_statements->first, op_assign_element);
1890            
1891 544           op_for = SPVM_OP_build_for_statement(compiler, op_for, op_assign_init, op_numlt_comparison, op_inc_increament, op_block_statements);
1892            
1893 544           SPVM_OP* op_block_new = SPVM_OP_new_op_block(compiler, op_for->file, op_for->line);
1894 544           SPVM_OP* op_statements_new = SPVM_OP_new_op_list(compiler, op_for->file, op_for->line);
1895 544           SPVM_OP_insert_child(compiler, op_block_new, op_block_new->last, op_statements_new);
1896            
1897 544           SPVM_OP_insert_child(compiler, op_statements_new, op_statements_new->last, op_assign_array);
1898 544           SPVM_OP_insert_child(compiler, op_statements_new, op_statements_new->last, op_assign_array_length);
1899 544           SPVM_OP_insert_child(compiler, op_statements_new, op_statements_new->last, op_for);
1900            
1901 544           return op_block_new;
1902             }
1903              
1904 26572           SPVM_OP* SPVM_OP_build_while_statement(SPVM_COMPILER* compiler, SPVM_OP* op_while, SPVM_OP* op_while_operand, SPVM_OP* op_block_statements) {
1905            
1906             // Loop
1907 26572           SPVM_OP* op_loop = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_LOOP, op_while->file, op_while->line);
1908            
1909             // Init statement. This is null.
1910 26572           SPVM_OP* op_init = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_DO_NOTHING, op_while->file, op_while->line);
1911            
1912             // Condition
1913 26572 50         assert(op_while_operand->moresib == 0);
1914 26572           SPVM_OP* op_condition = SPVM_OP_build_condition(compiler, op_while_operand, 1);
1915 26572           op_condition->flag |= SPVM_OP_C_FLAG_CONDITION_LOOP;
1916              
1917             // Set block flag
1918 26572           op_block_statements->uv.block->id = SPVM_BLOCK_C_ID_LOOP_STATEMENTS;
1919            
1920             // Next value. This is null.
1921 26572           SPVM_OP* op_increment = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_DO_NOTHING, op_while->file, op_while->line);
1922              
1923 26572           SPVM_OP* op_block_init = SPVM_OP_new_op_block(compiler, op_while->file, op_while->line);
1924 26572           op_block_init->uv.block->id = SPVM_BLOCK_C_ID_LOOP_INIT;
1925              
1926             // Block for increment
1927 26572           SPVM_OP* op_loop_increment = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_LOOP_INCREMENT, op_while->file, op_while->line);
1928 26572           SPVM_OP_insert_child(compiler, op_loop_increment, op_loop_increment->last, op_increment);
1929            
1930 26572           SPVM_OP_insert_child(compiler, op_block_init, op_block_init->last, op_init);
1931 26572           SPVM_OP_insert_child(compiler, op_block_init, op_block_init->last, op_condition);
1932 26572           SPVM_OP_insert_child(compiler, op_block_init, op_block_init->last, op_block_statements);
1933 26572           SPVM_OP_insert_child(compiler, op_block_init, op_block_init->last, op_loop_increment);
1934            
1935 26572           SPVM_OP_insert_child(compiler, op_loop, op_loop->last, op_block_init);
1936            
1937 26572           return op_loop;
1938             }
1939              
1940 12           SPVM_OP* SPVM_OP_build_if_require_statement(SPVM_COMPILER* compiler, SPVM_OP* op_if_require, SPVM_OP* op_use, SPVM_OP* op_block_true, SPVM_OP* op_block_false) {
1941            
1942 12           SPVM_OP_insert_child(compiler, op_if_require, op_if_require->last, op_use);
1943 12           SPVM_OP_insert_child(compiler, op_if_require, op_if_require->last, op_block_true);
1944 12 100         if (!op_block_false) {
1945 8           op_block_false = SPVM_OP_new_op_block(compiler, op_block_true->file, op_block_true->line);
1946             }
1947 12           SPVM_OP_insert_child(compiler, op_if_require, op_if_require->last, op_block_false);
1948            
1949 12           return op_if_require;
1950             }
1951              
1952 443383           SPVM_OP* SPVM_OP_build_if_statement(SPVM_COMPILER* compiler, SPVM_OP* op_if, SPVM_OP* op_if_operand, SPVM_OP* op_block_true, SPVM_OP* op_block_false, int32_t no_scope) {
1953            
1954             // ELSIF is same as IF
1955 443383           int32_t not_condition = 0;
1956            
1957 443383 100         if (op_if->id == SPVM_OP_C_ID_UNLESS) {
1958 159090           op_if->id = SPVM_OP_C_ID_IF;
1959 159090           not_condition = 1;
1960             }
1961 284293 100         else if (op_if->id == SPVM_OP_C_ID_ELSIF) {
1962 25614           op_if->id = SPVM_OP_C_ID_IF;
1963             }
1964            
1965             // Condition
1966 443383           SPVM_OP* op_condition = SPVM_OP_build_condition(compiler, op_if_operand, not_condition);
1967 443383           op_condition->flag |= SPVM_OP_C_FLAG_CONDITION_IF;
1968              
1969             // Create true block if needed
1970 443383 100         if (op_block_true->id != SPVM_OP_C_ID_BLOCK) {
1971 117945           SPVM_OP* op_not_block = op_block_true;
1972            
1973 117945           op_block_true = SPVM_OP_new_op_block(compiler, op_not_block->file, op_not_block->line);
1974 117945           SPVM_OP* op_list = SPVM_OP_new_op_list(compiler, op_not_block->file, op_not_block->line);
1975 117945           SPVM_OP_insert_child(compiler, op_list, op_list->last, op_not_block);
1976 117945           SPVM_OP_insert_child(compiler, op_block_true, op_block_true->last, op_list);
1977             }
1978 443383           op_block_true->uv.block->id = SPVM_BLOCK_C_ID_IF;
1979 443383           op_block_true->uv.block->no_scope = no_scope;
1980            
1981             // Create false block if needed
1982 443383 100         if (op_block_false->id != SPVM_OP_C_ID_BLOCK) {
1983 395439           SPVM_OP* op_not_block = op_block_false;
1984            
1985             // Create block
1986 395439           op_block_false = SPVM_OP_new_op_block(compiler, op_not_block->file, op_not_block->line);
1987 395439           SPVM_OP* op_list = SPVM_OP_new_op_list(compiler, op_not_block->file, op_not_block->line);
1988 395439           SPVM_OP_insert_child(compiler, op_list, op_list->last, op_not_block);
1989 395439           SPVM_OP_insert_child(compiler, op_block_false, op_block_false->last, op_list);
1990             }
1991 443383           op_block_false->uv.block->id = SPVM_BLOCK_C_ID_ELSE;
1992 443383           op_block_false->uv.block->no_scope = no_scope;
1993            
1994 443383           SPVM_OP_insert_child(compiler, op_if, op_if->last, op_condition);
1995 443383           SPVM_OP_insert_child(compiler, op_if, op_if->last, op_block_true);
1996 443383           SPVM_OP_insert_child(compiler, op_if, op_if->last, op_block_false);
1997            
1998 443383           return op_if;
1999             }
2000              
2001 56694           SPVM_OP* SPVM_OP_build_array_length(SPVM_COMPILER* compiler, SPVM_OP* op_array_length, SPVM_OP* op_length) {
2002            
2003 56694           SPVM_OP_insert_child(compiler, op_array_length, op_array_length->last, op_length);
2004            
2005 56694           return op_array_length;
2006             }
2007              
2008 0           SPVM_OP* SPVM_OP_build_string_length(SPVM_COMPILER* compiler, SPVM_OP* op_string_length, SPVM_OP* op_length) {
2009            
2010 0           SPVM_OP_insert_child(compiler, op_string_length, op_string_length->last, op_length);
2011            
2012 0           return op_string_length;
2013             }
2014              
2015 56793           SPVM_OP* SPVM_OP_build_new(SPVM_COMPILER* compiler, SPVM_OP* op_new, SPVM_OP* op_type, SPVM_OP* op_length) {
2016            
2017 56793           SPVM_OP_insert_child(compiler, op_new, op_new->last, op_type);
2018            
2019 56793 100         if (op_length) {
2020 30294           SPVM_OP_insert_child(compiler, op_new, op_new->last, op_length);
2021             }
2022            
2023 56793 100         if (op_type->id == SPVM_OP_C_ID_TYPE) {
2024 48178           op_type->uv.type->resolved_in_ast = 1;
2025             }
2026            
2027 56793 100         if (op_type->id == SPVM_OP_C_ID_TYPE && strstr(op_type->uv.type->unresolved_basic_type_name, "::anon::")) {
    100          
2028            
2029 8213           const char* anon_basic_type_name = op_type->uv.type->unresolved_basic_type_name;
2030 8213           SPVM_BASIC_TYPE* anon_basic_type = SPVM_HASH_get(compiler->basic_type_symtable, anon_basic_type_name, strlen(anon_basic_type_name));
2031            
2032             // Anon method
2033 8213           SPVM_METHOD* anon_method = SPVM_LIST_get(anon_basic_type->methods, 0);
2034 8213 100         if (anon_method->anon_method_fields->length) {
2035             // [Before]
2036             // new Foo::anon::Line::Column
2037            
2038             // [After]
2039             //(
2040             // my $invocant = new Foo::anon::Line::Column,
2041             // $invocant->{FIELD_NAME0} = DEFAULT0,
2042             // $invocant->{FIELD_NAME1} = DEFAULT1,
2043             // $invocant,
2044             //)
2045            
2046 22           SPVM_OP* op_sequence = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_SEQUENCE, op_new->file, op_new->line);
2047            
2048             // my $invocant = new TYPE;
2049 22           SPVM_OP* op_assign_new = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_new->file, op_new->line);
2050 22           SPVM_OP* op_var_invocant_new_name = SPVM_OP_new_op_name_tmp_var(compiler, op_new->file, op_new->line);
2051 22           SPVM_OP* op_var_invocant_new = SPVM_OP_new_op_var(compiler, op_var_invocant_new_name);
2052 22           SPVM_OP* op_var_decl_invocant_new = SPVM_OP_new_op_var_decl_arg(compiler, op_new->file, op_new->line);
2053 22           op_var_invocant_new = SPVM_OP_build_var_decl(compiler, op_var_decl_invocant_new, op_var_invocant_new, NULL, NULL);
2054 22           SPVM_OP_build_assign(compiler, op_assign_new, op_var_invocant_new, op_new);
2055 22           SPVM_OP_insert_child(compiler, op_sequence, op_sequence->last, op_assign_new);
2056            
2057 57 100         for (int32_t caputre_index = 0; caputre_index < anon_method->anon_method_fields->length; caputre_index++) {
2058 35           SPVM_FIELD* anon_method_field = SPVM_LIST_get(anon_method->anon_method_fields, caputre_index);
2059 35           const char* anon_method_field_name = anon_method_field->name;
2060 35           SPVM_OP* op_anon_method_field_default = anon_method_field->op_anon_method_field_default;
2061            
2062             // $invocant->{FIELD_NAME} = DEFAULT
2063 35 100         if (op_anon_method_field_default) {
2064             //
2065 27           SPVM_OP* op_invocant_set_field = SPVM_OP_clone_op_var(compiler, op_var_invocant_new);
2066 27           SPVM_OP* op_name_field = SPVM_OP_new_op_name(compiler, anon_method_field->name, op_new->file, op_new->line);
2067 27           SPVM_OP* op_field_access = SPVM_OP_new_op_field_access(compiler, op_new->file, op_new->line);
2068 27           SPVM_OP_build_field_access(compiler, op_field_access, op_invocant_set_field, op_name_field);
2069 27           SPVM_OP* op_assign_field_access = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_new->file, op_new->line);
2070 27           SPVM_OP_build_assign(compiler, op_assign_field_access, op_field_access, op_anon_method_field_default);
2071 27           SPVM_OP_insert_child(compiler, op_sequence, op_sequence->last, op_assign_field_access);
2072             }
2073             }
2074            
2075             // $invocant
2076 22           SPVM_OP* op_var_invocant_ret = SPVM_OP_clone_op_var(compiler, op_var_invocant_new);
2077 22           SPVM_OP_insert_child(compiler, op_sequence, op_sequence->last, op_var_invocant_ret);
2078            
2079 22           op_new = op_sequence;
2080             }
2081             }
2082            
2083 56793           return op_new;
2084             }
2085              
2086 8668           SPVM_OP* SPVM_OP_build_array_init(SPVM_COMPILER* compiler, SPVM_OP* op_array_init, SPVM_OP* op_list_elements, int32_t is_key_value_pairs) {
2087            
2088             // Array length
2089 8668           int32_t length = 0;
2090             {
2091 8668           SPVM_OP* op_element = op_list_elements->first;
2092 8668           int32_t index = 0;
2093 25789 100         while ((op_element = SPVM_OP_sibling(compiler, op_element))) {
2094 17121           length++;
2095             }
2096             }
2097            
2098 8668 100         if (is_key_value_pairs && length % 2 != 0) {
    100          
2099 1           SPVM_COMPILER_error(compiler, "The lenght of the elements in {} of the array initialization must be an even number.\n at %s line %d", op_list_elements->file, op_list_elements->line);
2100 1           return op_array_init;
2101             }
2102            
2103 8667           SPVM_OP* op_constant_length = SPVM_OP_new_op_constant_int(compiler, length, op_array_init->file, op_array_init->line);
2104 8667           SPVM_OP* op_new = NULL;
2105 8667 100         if (length == 0) {
2106 51           op_new = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_NEW, op_array_init->file, op_array_init->line);
2107 51           SPVM_OP* op_type = SPVM_OP_new_op_any_object_array_type(compiler, op_array_init->file, op_array_init->line);
2108 51           SPVM_OP_build_new(compiler, op_new, op_type, op_constant_length);
2109 51           op_array_init = op_new;
2110             }
2111 8616 50         else if (length > 0) {
2112             {
2113 8616           SPVM_OP* op_pushmark = op_list_elements->first;
2114 8616           SPVM_OP* op_first_element = SPVM_OP_sibling(compiler, op_pushmark);
2115 8616 100         if (op_first_element->id == SPVM_OP_C_ID_UNDEF) {
2116 1           SPVM_COMPILER_error(compiler, "The first element in the array initialization must be defined.\n at %s line %d", op_array_init->file, op_array_init->line);
2117 1           return op_array_init;
2118             }
2119             }
2120            
2121 8615 100         if (is_key_value_pairs) {
2122 563           SPVM_OP* op_pushmark = op_list_elements->first;
2123 563           SPVM_OP* op_first_element = SPVM_OP_sibling(compiler, op_pushmark);
2124 563 50         if (is_key_value_pairs) {
2125 563           SPVM_OP* op_type_cast = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_TYPE_CAST, op_array_init->file, op_array_init->line);
2126 563           SPVM_OP* op_type_for_cast = SPVM_OP_new_op_any_object_type(compiler, op_array_init->file, op_array_init->line);
2127 563           SPVM_OP* op_stab = SPVM_OP_cut_op(compiler, op_first_element);
2128 563           SPVM_OP_build_type_cast(compiler, op_type_cast, op_type_for_cast, op_first_element, NULL);
2129 563           SPVM_OP_replace_op(compiler, op_stab, op_type_cast);
2130             }
2131             }
2132            
2133 8615           SPVM_OP* op_pushmark = op_list_elements->first;
2134 8615           SPVM_OP* op_first_element = SPVM_OP_sibling(compiler, op_pushmark);
2135            
2136 8615           SPVM_OP* op_sequence = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_SEQUENCE, op_array_init->file, op_array_init->line);
2137            
2138             // my $first_element = FIRST_ELEMENT;
2139 8615           SPVM_OP* op_var_first_element_name = SPVM_OP_new_op_name_tmp_var(compiler, op_array_init->file, op_array_init->line);
2140 8615           SPVM_OP* op_var_first_element = SPVM_OP_new_op_var(compiler, op_var_first_element_name);
2141 8615           SPVM_OP* op_var_decl_first_element = SPVM_OP_new_op_var_decl_arg(compiler, op_array_init->file, op_array_init->line);
2142 8615           op_var_first_element = SPVM_OP_build_var_decl(compiler, op_var_decl_first_element, op_var_first_element, NULL, NULL);
2143 8615           SPVM_OP* op_assign_first_element = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_array_init->file, op_array_init->line);
2144 8615           SPVM_OP_cut_op(compiler, op_first_element);
2145 8615           SPVM_OP_build_assign(compiler, op_assign_first_element, op_var_first_element, op_first_element);
2146 8615           SPVM_OP_insert_child(compiler, op_sequence, op_sequence->last, op_assign_first_element);
2147            
2148             // my $array = new $first_element;
2149 8615           op_new = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_NEW, op_array_init->file, op_array_init->line);
2150 8615           SPVM_OP* op_var_first_element_new = SPVM_OP_clone_op_var(compiler, op_var_first_element);
2151 8615           op_new = SPVM_OP_build_new(compiler, op_new, op_var_first_element_new, op_constant_length);
2152 8615           SPVM_OP* op_assign_new = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_array_init->file, op_array_init->line);
2153 8615           SPVM_OP* op_var_array_new_name = SPVM_OP_new_op_name_tmp_var(compiler, op_array_init->file, op_array_init->line);
2154 8615           SPVM_OP* op_var_array_new = SPVM_OP_new_op_var(compiler, op_var_array_new_name);
2155 8615           SPVM_OP* op_var_decl_array_new = SPVM_OP_new_op_var_decl_arg(compiler, op_array_init->file, op_array_init->line);
2156 8615           op_var_array_new = SPVM_OP_build_var_decl(compiler, op_var_decl_array_new, op_var_array_new, NULL, NULL);
2157 8615           SPVM_OP_build_assign(compiler, op_assign_new, op_var_array_new, op_new);
2158 8615           SPVM_OP_insert_child(compiler, op_sequence, op_sequence->last, op_assign_new);
2159            
2160             {
2161 8615           int32_t index = 0;
2162 8615           SPVM_OP* op_element = op_list_elements->first;
2163 25732 100         while ((op_element = SPVM_OP_sibling(compiler, op_element))) {
2164 17117           SPVM_OP* op_assign_array_access = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_array_init->file, op_array_init->line);
2165 17117           SPVM_OP* op_array_access = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ARRAY_ACCESS, op_array_init->file, op_array_init->line);
2166 17117           SPVM_OP* op_var_array_set = SPVM_OP_clone_op_var(compiler, op_var_array_new);
2167 17117           SPVM_OP_insert_child(compiler, op_array_access, op_array_access->last, op_var_array_set);
2168 17117           SPVM_OP* op_constant_index = SPVM_OP_new_op_constant_int(compiler, index, op_array_init->file, op_array_init->line);
2169 17117           SPVM_OP_insert_child(compiler, op_array_access, op_array_access->last, op_constant_index);
2170            
2171 17117 100         if (index == 0) {
2172             // $array->[0] = $first_element;
2173 8615           SPVM_OP* op_var_first_element_array_set = SPVM_OP_clone_op_var(compiler, op_var_first_element);
2174 8615           SPVM_OP_build_assign(compiler, op_assign_array_access, op_array_access, op_var_first_element_array_set);
2175             }
2176             else {
2177             // $array->[$i] = ELEMENT
2178 8502           SPVM_OP* op_stab = SPVM_OP_cut_op(compiler, op_element);
2179 8502           SPVM_OP_build_assign(compiler, op_assign_array_access, op_array_access, op_element);
2180 8502           op_element = op_stab;
2181             }
2182            
2183 17117           SPVM_OP_insert_child(compiler, op_sequence, op_sequence->last, op_assign_array_access);
2184            
2185 17117           index++;
2186             }
2187             }
2188            
2189             // $array
2190 8615           SPVM_OP* op_var_array_ret = SPVM_OP_clone_op_var(compiler, op_var_array_new);
2191 8615           SPVM_OP_insert_child(compiler, op_sequence, op_sequence->last, op_var_array_ret);
2192            
2193 8615           op_array_init = op_sequence;
2194             }
2195            
2196 8666           return op_array_init;
2197             }
2198              
2199 166468           SPVM_OP* SPVM_OP_build_array_access(SPVM_COMPILER* compiler, SPVM_OP* op_array_access, SPVM_OP* op_array, SPVM_OP* op_index) {
2200            
2201 166468           SPVM_OP_insert_child(compiler, op_array_access, op_array_access->last, op_array);
2202 166468           SPVM_OP_insert_child(compiler, op_array_access, op_array_access->last, op_index);
2203            
2204 166468           return op_array_access;
2205             }
2206              
2207 189224           SPVM_OP* SPVM_OP_build_field_access(SPVM_COMPILER* compiler, SPVM_OP* op_field_access, SPVM_OP* op_invocant, SPVM_OP* op_name_field) {
2208            
2209 189224           SPVM_OP_insert_child(compiler, op_field_access, op_field_access->last, op_invocant);
2210            
2211 189224           SPVM_FIELD_ACCESS* field_access = op_field_access->uv.field_access;
2212            
2213 189224           field_access->op_name = op_name_field;
2214            
2215 189224           return op_field_access;
2216             }
2217              
2218 19           SPVM_OP* SPVM_OP_build_can(SPVM_COMPILER* compiler, SPVM_OP* op_can, SPVM_OP* op_var, SPVM_OP* op_name) {
2219            
2220 19 100         if (op_name->id == SPVM_OP_C_ID_CONSTANT) {
2221 4           SPVM_OP* op_constant = op_name;
2222 4           SPVM_CONSTANT* constant = op_constant->uv.constant;
2223 4           SPVM_TYPE* constant_type = constant->type;
2224 4           const char* constant_chars = (const char*)constant->value.oval;
2225 4           int32_t is_empty_string = 0;
2226 4 50         if (SPVM_TYPE_is_string_type(compiler, constant_type->basic_type->id, constant_type->dimension, constant_type->flag)) {
2227 4 100         if (strcmp(constant_chars, "") == 0) {
2228 3           is_empty_string = 1;
2229             }
2230             }
2231            
2232 4 100         if (!is_empty_string) {
2233 1           SPVM_COMPILER_error(compiler, "If the right operand of the can operator is a constant value, it must be an empty string \"\".\n at %s line %d", op_name->file, op_name->line);
2234             }
2235            
2236 4           op_name = SPVM_OP_new_op_name(compiler, "", op_name->file, op_name->line);
2237             }
2238            
2239 19           SPVM_OP_insert_child(compiler, op_can, op_can->last, op_var);
2240 19           SPVM_OP_insert_child(compiler, op_can, op_can->last, op_name);
2241            
2242 19           SPVM_OP* op_name_var_condition = SPVM_OP_new_op_name(compiler, "$.condition_flag", op_var->file, op_var->line);
2243 19           SPVM_OP* op_var_condition = SPVM_OP_new_op_var(compiler, op_name_var_condition);
2244 19           SPVM_OP* op_assign = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_var->file, op_var->line);
2245 19           SPVM_OP_build_assign(compiler, op_assign, op_var_condition, op_can);
2246            
2247 19           return op_assign;
2248             }
2249              
2250 1073           SPVM_OP* SPVM_OP_build_is_read_only(SPVM_COMPILER* compiler, SPVM_OP* op_is_read_only, SPVM_OP* op_operand) {
2251            
2252 1073           SPVM_OP_insert_child(compiler, op_is_read_only, op_is_read_only->last, op_operand);
2253            
2254 1073           SPVM_OP* op_name_var_condition = SPVM_OP_new_op_name(compiler, "$.condition_flag", op_operand->file, op_operand->line);
2255 1073           SPVM_OP* op_var_condition = SPVM_OP_new_op_var(compiler, op_name_var_condition);
2256 1073           SPVM_OP* op_assign = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_operand->file, op_operand->line);
2257 1073           SPVM_OP_build_assign(compiler, op_assign, op_var_condition, op_is_read_only);
2258            
2259 1073           return op_assign;
2260             }
2261              
2262 45           SPVM_OP* SPVM_OP_build_weaken_field(SPVM_COMPILER* compiler, SPVM_OP* op_weaken, SPVM_OP* op_field_access) {
2263            
2264 45           SPVM_OP* op_weaken_field = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_WEAKEN_FIELD, op_weaken->file, op_weaken->line);
2265 45           SPVM_OP_insert_child(compiler, op_weaken_field, op_weaken_field->last, op_field_access);
2266            
2267 45           op_field_access->flag |= SPVM_OP_C_FLAG_FIELD_ACCESS_WEAKEN;
2268            
2269 45           return op_weaken_field;
2270             }
2271              
2272 4           SPVM_OP* SPVM_OP_build_unweaken_field(SPVM_COMPILER* compiler, SPVM_OP* op_unweaken, SPVM_OP* op_field_access) {
2273            
2274 4           SPVM_OP* op_unweaken_field = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_UNWEAKEN_FIELD, op_unweaken->file, op_unweaken->line);
2275 4           SPVM_OP_insert_child(compiler, op_unweaken_field, op_unweaken_field->last, op_field_access);
2276            
2277 4           op_field_access->flag |= SPVM_OP_C_FLAG_FIELD_ACCESS_UNWEAKEN;
2278            
2279 4           return op_unweaken_field;
2280             }
2281              
2282 8           SPVM_OP* SPVM_OP_build_isweak_field(SPVM_COMPILER* compiler, SPVM_OP* op_isweak, SPVM_OP* op_field_access) {
2283            
2284 8           SPVM_OP* op_isweak_field = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ISWEAK_FIELD, op_isweak->file, op_isweak->line);
2285 8           SPVM_OP_insert_child(compiler, op_isweak_field, op_isweak_field->last, op_field_access);
2286 8           op_field_access->flag |= SPVM_OP_C_FLAG_FIELD_ACCESS_ISWEAK;
2287              
2288 8           SPVM_OP* op_name_var = SPVM_OP_new_op_name(compiler, "$.condition_flag", op_field_access->file, op_field_access->line);
2289 8           SPVM_OP* op_var = SPVM_OP_new_op_var(compiler, op_name_var);
2290 8           SPVM_OP* op_assign = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_field_access->file, op_field_access->line);
2291 8           SPVM_OP_build_assign(compiler, op_assign, op_var, op_field_access);
2292            
2293 8           return op_assign;
2294             }
2295              
2296 156711           SPVM_OP* SPVM_OP_build_type_cast(SPVM_COMPILER* compiler, SPVM_OP* op_type_cast, SPVM_OP* op_type, SPVM_OP* op_operand, SPVM_OP* op_attributes) {
2297            
2298 156711           SPVM_OP_insert_child(compiler, op_type_cast, op_type_cast->last, op_operand);
2299 156711           SPVM_OP_insert_child(compiler, op_type_cast, op_type_cast->last, op_type);
2300            
2301 156711           op_type_cast->file = op_type->file;
2302 156711           op_type_cast->line = op_type->line;
2303              
2304 156711           return op_type_cast;
2305             }
2306              
2307 1386           SPVM_OP* SPVM_OP_build_implement(SPVM_COMPILER* compiler, SPVM_OP* op_interface, SPVM_OP* op_type) {
2308            
2309 1386           SPVM_INTERFACE* interface = SPVM_INTERFACE_new(compiler);
2310 1386           op_interface->uv.interface = interface;
2311 1386           interface->op_interface = op_interface;
2312 1386           interface->op_type = op_type;
2313            
2314             // add use stack
2315 1386           SPVM_OP* op_use = SPVM_OP_new_op_use(compiler, op_interface->file, op_interface->line);
2316 1386           SPVM_OP* op_name_alias = NULL;
2317 1386           int32_t is_require = 0;
2318 1386           SPVM_OP_build_use(compiler, op_use, op_type, op_name_alias, is_require);
2319            
2320 1386           return op_interface;
2321             }
2322              
2323 2872928           SPVM_OP* SPVM_OP_build_var_decl(SPVM_COMPILER* compiler, SPVM_OP* op_var_decl, SPVM_OP* op_var, SPVM_OP* op_type, SPVM_OP* op_attributes) {
2324            
2325             // Declaration
2326 2872928           op_var->uv.var->is_declaration = 1;
2327            
2328             // Create var_decl var information
2329 2872928           SPVM_VAR_DECL* var_decl = op_var_decl->uv.var_decl;
2330 2872928 100         if (op_type) {
2331 2385466           var_decl->type = op_type->uv.type;
2332 2385466           SPVM_OP_insert_child(compiler, op_var, op_var->last, op_type);
2333 2385466           op_type->uv.type->resolved_in_ast = 1;
2334             }
2335            
2336             // Name OP
2337 2872928           SPVM_OP* op_name = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_NAME, op_var->file, op_var->line);
2338 2872928           op_name->uv.name = op_var->uv.var->name;
2339 2872928           var_decl->var = op_var->uv.var;
2340              
2341 2872928           op_var->uv.var->var_decl = var_decl;
2342            
2343 2872928 100         if (strstr(op_var->uv.var->name, "::")) {
2344 7           SPVM_COMPILER_error(compiler, "The local variable \"%s\" cannnot contain \"::\".\n at %s line %d", op_var->uv.var->name, op_var->file, op_var->line);
2345             }
2346            
2347 2872928           return op_var;
2348             }
2349              
2350 255949           SPVM_OP* SPVM_OP_build_call_method(SPVM_COMPILER* compiler, SPVM_OP* op_call_method, SPVM_OP* op_invocant, SPVM_OP* op_name_method, SPVM_OP* op_list_operands) {
2351            
2352 255949           SPVM_OP_insert_child(compiler, op_call_method, op_call_method->last, op_list_operands);
2353            
2354 255949           SPVM_CALL_METHOD* call_method = op_call_method->uv.call_method;
2355            
2356 255949           const char* method_name = op_name_method->uv.name;
2357            
2358             // Class method call
2359 255949 50         assert(op_invocant->id != SPVM_OP_C_ID_NAME);
2360 255949 100         if (op_invocant->id == SPVM_OP_C_ID_TYPE || op_invocant->id == SPVM_OP_C_ID_CURRENT_CLASS) {
    100          
2361 167637           call_method->is_class_method = 1;
2362 167637           call_method->op_name = op_name_method;
2363 335274 100         if (op_invocant->id == SPVM_OP_C_ID_TYPE) {
2364 116472           call_method->basic_type_name = op_invocant->uv.type->unresolved_basic_type_name;
2365 116472           SPVM_OP_insert_child(compiler, op_call_method, op_call_method->last, op_invocant);
2366            
2367 116472           op_invocant->uv.type->resolved_in_ast = 1;
2368             }
2369 51165 50         else if (op_invocant->id == SPVM_OP_C_ID_CURRENT_CLASS) {
2370 51165           call_method->is_current = 1;
2371             }
2372             }
2373             // Instance method call
2374             else {
2375 88312           call_method->op_name = op_name_method;
2376            
2377 88312 100         if (op_invocant->id == SPVM_OP_C_ID_VAR) {
2378 84965           op_invocant->uv.var->call_method = call_method;
2379             }
2380            
2381 88312           SPVM_OP_insert_child(compiler, op_list_operands, op_list_operands->first, op_invocant);
2382             }
2383            
2384 255949           return op_call_method;
2385             }
2386              
2387 31713           SPVM_OP* SPVM_OP_build_unary_op(SPVM_COMPILER* compiler, SPVM_OP* op_unary, SPVM_OP* op_first) {
2388            
2389             // Build op
2390 31713           SPVM_OP_insert_child(compiler, op_unary, op_unary->last, op_first);
2391            
2392 31713           return op_unary;
2393             }
2394              
2395 7630           SPVM_OP* SPVM_OP_build_unary_op_var(SPVM_COMPILER* compiler, SPVM_OP* op_unary, SPVM_OP* op_first) {
2396            
2397 7630           const char* op_name = SPVM_OP_get_op_name(compiler, op_unary->id);
2398 7630           char op_name_lc[16] = {0};
2399 7630           snprintf(op_name_lc, 16, "%s", op_name);
2400 129710 100         for (int32_t i = 0; i < 16; i++) {
2401 122080 100         if (op_name_lc[i] != '\0') {
2402 58606           op_name_lc[i] += ('a' - 'A');
2403             }
2404             }
2405            
2406 7630 100         if (op_first->id != SPVM_OP_C_ID_VAR) {
2407 1           SPVM_COMPILER_error(compiler, "The operand of the %s operator must be a variable.\n at %s line %d", op_name_lc, op_first->file, op_first->line);
2408             }
2409            
2410             // Build op
2411 7630           SPVM_OP_insert_child(compiler, op_unary, op_unary->last, op_first);
2412            
2413 7630           return op_unary;
2414             }
2415              
2416 357631           SPVM_OP* SPVM_OP_build_comparison_op(SPVM_COMPILER* compiler, SPVM_OP* op_comparison, SPVM_OP* op_first, SPVM_OP* op_last) {
2417              
2418 357631           SPVM_OP_insert_child(compiler, op_comparison, op_comparison->last, op_first);
2419 357631           SPVM_OP_insert_child(compiler, op_comparison, op_comparison->last, op_last);
2420            
2421 357631           SPVM_OP* op_name_var = SPVM_OP_new_op_name(compiler, "$.condition_flag", op_comparison->file, op_comparison->line);
2422 357631           SPVM_OP* op_var = SPVM_OP_new_op_var(compiler, op_name_var);
2423 357631           SPVM_OP* op_assign = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_comparison->file, op_comparison->line);
2424 357631           SPVM_OP_build_assign(compiler, op_assign, op_var, op_comparison);
2425            
2426 357631           return op_assign;
2427             }
2428              
2429 8508           SPVM_OP* SPVM_OP_build_binary_is(SPVM_COMPILER* compiler, SPVM_OP* op_is, SPVM_OP* op_first, SPVM_OP* op_last) {
2430            
2431 8508           SPVM_OP_insert_child(compiler, op_is, op_is->last, op_first);
2432 8508           SPVM_OP_insert_child(compiler, op_is, op_is->last, op_last);
2433            
2434 8508           SPVM_OP* op_name_var = SPVM_OP_new_op_name(compiler, "$.condition_flag", op_is->file, op_is->line);
2435 8508           SPVM_OP* op_var = SPVM_OP_new_op_var(compiler, op_name_var);
2436 8508           SPVM_OP* op_assign = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_is->file, op_is->line);
2437 8508           SPVM_OP_build_assign(compiler, op_assign, op_var, op_is);
2438            
2439 8508           return op_assign;
2440             }
2441              
2442 4645           SPVM_OP* SPVM_OP_build_isa(SPVM_COMPILER* compiler, SPVM_OP* op_isa, SPVM_OP* op_operand, SPVM_OP* op_type) {
2443            
2444 4645           return SPVM_OP_build_binary_is(compiler, op_isa, op_operand, op_type);
2445             }
2446              
2447 20           SPVM_OP* SPVM_OP_build_isa_error(SPVM_COMPILER* compiler, SPVM_OP* op_isa_error, SPVM_OP* op_operand, SPVM_OP* op_type) {
2448            
2449 20           return SPVM_OP_build_binary_is(compiler, op_isa_error, op_operand, op_type);
2450             }
2451              
2452 3809           SPVM_OP* SPVM_OP_build_is_type(SPVM_COMPILER* compiler, SPVM_OP* op_is_type, SPVM_OP* op_operand, SPVM_OP* op_type) {
2453            
2454 3809           return SPVM_OP_build_binary_is(compiler, op_is_type, op_operand, op_type);
2455             }
2456              
2457 18           SPVM_OP* SPVM_OP_build_is_error(SPVM_COMPILER* compiler, SPVM_OP* op_is_error, SPVM_OP* op_operand, SPVM_OP* op_type) {
2458            
2459 18           return SPVM_OP_build_binary_is(compiler, op_is_error, op_operand, op_type);
2460             }
2461              
2462 16           SPVM_OP* SPVM_OP_build_is_compile_type(SPVM_COMPILER* compiler, SPVM_OP* op_is_compile_type, SPVM_OP* op_operand, SPVM_OP* op_compile_type) {
2463            
2464 16           return SPVM_OP_build_binary_is(compiler, op_is_compile_type, op_operand, op_compile_type);
2465             }
2466              
2467 316329           SPVM_OP* SPVM_OP_build_binary_op(SPVM_COMPILER* compiler, SPVM_OP* op_bin, SPVM_OP* op_first, SPVM_OP* op_last) {
2468            
2469             // Build op
2470 316329           SPVM_OP_insert_child(compiler, op_bin, op_bin->last, op_first);
2471 316329           SPVM_OP_insert_child(compiler, op_bin, op_bin->last, op_last);
2472            
2473 316329           return op_bin;
2474             }
2475              
2476 108240           SPVM_OP* SPVM_OP_build_special_assign(SPVM_COMPILER* compiler, SPVM_OP* op_special_assign, SPVM_OP* op_dist, SPVM_OP* op_src) {
2477            
2478             /*
2479             ++$var, ++$VAR, ++$@
2480             (
2481             my $old = $var,
2482             $var = $old + 1,
2483             )
2484              
2485             ++$$var_ref
2486             (
2487             my $old = $$var_ref,
2488             $$var_ref = $old + 1,
2489             )
2490            
2491             ++ARRAY->[INDEX]
2492             (
2493             my $array = ARRAY,
2494             my $index = INDEX,
2495             my $old = $array->[$index],
2496             $array->[$index] = $old + 1,
2497             )
2498              
2499             ++OBJECT->{FIELD_NAME}
2500             (
2501             my $object = OBJECT,
2502             my $old = $object->{FIELD_NAME},
2503             $object->{FIELD_NAME} = $old + 1,
2504             )
2505              
2506             ++ARRAY->[INDEX]{FIELD_NAME}
2507             (
2508             my $array = ARRAY,
2509             my $index = INDEX,
2510             my $old = $array->[$index]{FIELD_NAME},
2511             $array->[$index]{FIELD_NAME} = $old + 1,
2512             )
2513              
2514             ACCESS++
2515             (
2516             ...,
2517             ...,
2518             $old,
2519             )
2520             */
2521              
2522 108240           SPVM_OP* op_sequence = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_SEQUENCE, op_special_assign->file, op_special_assign->line);
2523            
2524 108240           SPVM_OP* op_assign_save_old = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_special_assign->file, op_special_assign->line);
2525            
2526 108240           SPVM_OP* op_name_var_old = SPVM_OP_new_op_name_tmp_var(compiler, op_special_assign->file, op_special_assign->line);
2527            
2528 108240           SPVM_OP* op_var_old = SPVM_OP_new_op_var(compiler, op_name_var_old);
2529            
2530 108240           SPVM_OP* op_var_old_decl = SPVM_OP_new_op_var_decl(compiler, op_special_assign->file, op_special_assign->line);
2531 108240           SPVM_OP_build_var_decl(compiler, op_var_old_decl, op_var_old, NULL, NULL);
2532            
2533 108240           SPVM_OP* op_assign_update = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_special_assign->file, op_special_assign->line);
2534 108240           SPVM_OP* op_var_old_clone = SPVM_OP_clone_op_var(compiler, op_var_old);
2535            
2536             int32_t culc_op_id;
2537            
2538 108240           switch (op_special_assign->id) {
2539             case SPVM_OP_C_ID_PRE_INC:
2540             case SPVM_OP_C_ID_POST_INC:
2541             {
2542 82782 100         if (!SPVM_OP_is_mutable(compiler, op_dist)) {
2543 1           SPVM_COMPILER_error(compiler, "The operand of ++ operator must be mutable.\n at %s line %d", op_dist->file, op_dist->line);
2544 1           return op_special_assign;
2545             }
2546            
2547 82781           culc_op_id = SPVM_OP_C_ID_ADD;
2548 82781           break;
2549             }
2550             case SPVM_OP_C_ID_PRE_DEC:
2551             case SPVM_OP_C_ID_POST_DEC:
2552             {
2553 10353 100         if (!SPVM_OP_is_mutable(compiler, op_dist)) {
2554 1           SPVM_COMPILER_error(compiler, "The operand of -- operator must be mutable.\n at %s line %d", op_dist->file, op_dist->line);
2555 1           return op_special_assign;
2556             }
2557            
2558 10352           culc_op_id = SPVM_OP_C_ID_SUBTRACT;
2559 10352           break;
2560             }
2561             case SPVM_OP_C_ID_SPECIAL_ASSIGN: {
2562 15105 100         if (!SPVM_OP_is_mutable(compiler, op_dist)) {
2563 1           SPVM_COMPILER_error(compiler, "The left operand of the special assign operator must be mutable.\n at %s line %d", op_dist->file, op_dist->line);
2564 1           return op_special_assign;
2565             }
2566            
2567 15104           switch (op_special_assign->flag) {
2568             case SPVM_OP_C_FLAG_SPECIAL_ASSIGN_ADD: {
2569 5954           culc_op_id = SPVM_OP_C_ID_ADD;
2570 5954           break;
2571             }
2572             case SPVM_OP_C_FLAG_SPECIAL_ASSIGN_SUBTRACT: {
2573 14           culc_op_id = SPVM_OP_C_ID_SUBTRACT;
2574 14           break;
2575             }
2576             case SPVM_OP_C_FLAG_SPECIAL_ASSIGN_MULTIPLY: {
2577 545           culc_op_id = SPVM_OP_C_ID_MULTIPLY;
2578 545           break;
2579             }
2580             case SPVM_OP_C_FLAG_SPECIAL_ASSIGN_DIVIDE: {
2581 14           culc_op_id = SPVM_OP_C_ID_DIVIDE;
2582 14           break;
2583             }
2584             case SPVM_OP_C_FLAG_SPECIAL_ASSIGN_REMAINDER: {
2585 10           culc_op_id = SPVM_OP_C_ID_REMAINDER;
2586 10           break;
2587             }
2588             case SPVM_OP_C_FLAG_SPECIAL_ASSIGN_LEFT_SHIFT: {
2589 10           culc_op_id = SPVM_OP_C_ID_LEFT_SHIFT;
2590 10           break;
2591             }
2592             case SPVM_OP_C_FLAG_SPECIAL_ASSIGN_RIGHT_ARITHMETIC_SHIFT: {
2593 10           culc_op_id = SPVM_OP_C_ID_RIGHT_ARITHMETIC_SHIFT;
2594 10           break;
2595             }
2596             case SPVM_OP_C_FLAG_SPECIAL_ASSIGN_RIGHT_LOGICAL_SHIFT: {
2597 10           culc_op_id = SPVM_OP_C_ID_RIGHT_LOGICAL_SHIFT;
2598 10           break;
2599             }
2600             case SPVM_OP_C_FLAG_SPECIAL_ASSIGN_BIT_XOR: {
2601 10           culc_op_id = SPVM_OP_C_ID_BIT_XOR;
2602 10           break;
2603             }
2604             case SPVM_OP_C_FLAG_SPECIAL_ASSIGN_BIT_OR: {
2605 10           culc_op_id = SPVM_OP_C_ID_BIT_OR;
2606 10           break;
2607             }
2608             case SPVM_OP_C_FLAG_SPECIAL_ASSIGN_BIT_AND: {
2609 10           culc_op_id = SPVM_OP_C_ID_BIT_AND;
2610 10           break;
2611             }
2612             case SPVM_OP_C_FLAG_SPECIAL_ASSIGN_CONCAT: {
2613 8507           culc_op_id = SPVM_OP_C_ID_CONCAT;
2614 8507           break;
2615             }
2616             default: {
2617 0           assert(0);
2618             break;
2619             }
2620             }
2621            
2622 15104           break;
2623             }
2624             }
2625            
2626 108237           SPVM_OP* op_culc = SPVM_OP_new_op(compiler, culc_op_id, op_special_assign->file, op_special_assign->line);
2627            
2628 108237           op_culc->allow_narrowing_conversion = 1;
2629 108237           op_culc->original_id = op_special_assign->id;
2630            
2631 108237           SPVM_OP_build_binary_op(compiler, op_culc, op_var_old_clone, op_src);
2632            
2633 207050 100         if (op_dist->id == SPVM_OP_C_ID_VAR || op_dist->id == SPVM_OP_C_ID_EXCEPTION_VAR) {
    50          
2634 98813           SPVM_OP_build_assign(compiler, op_assign_save_old, op_var_old, op_dist);
2635 98813           SPVM_OP* op_var = op_dist;
2636 98813           SPVM_OP* op_var_clone = SPVM_OP_clone_op_var(compiler, op_var);
2637 98813           SPVM_OP* op_dist_clone = op_var_clone;
2638 98813           SPVM_OP_build_assign(compiler, op_assign_update, op_dist_clone, op_culc);
2639             }
2640 9424 100         else if (op_dist->id == SPVM_OP_C_ID_DEREF) {
2641 1122           SPVM_OP_build_assign(compiler, op_assign_save_old, op_var_old, op_dist);
2642 1122           SPVM_OP* op_deref = op_dist;
2643 1122           SPVM_OP* op_var_deref = op_deref->first;
2644 1122           SPVM_OP* op_deref_clone = SPVM_OP_clone_op_deref(compiler, op_deref, op_var_deref);
2645 1122           SPVM_OP* op_dist_clone = op_deref_clone;
2646 1122           SPVM_OP_build_assign(compiler, op_assign_update, op_dist_clone, op_culc);
2647             }
2648 8302 100         else if (op_dist->id == SPVM_OP_C_ID_ARRAY_ACCESS) {
2649 131           SPVM_OP* op_array_access = op_dist;
2650            
2651 131           SPVM_OP* op_array = op_array_access->first;
2652 131           SPVM_OP* op_index = op_array_access->last;
2653            
2654 131           SPVM_OP_cut_op(compiler, op_array_access->first);
2655 131           SPVM_OP_cut_op(compiler, op_array_access->last);
2656            
2657 131           SPVM_OP* op_assign_array_get = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_array->file, op_array->line);
2658 131           SPVM_OP* op_name_var_array = SPVM_OP_new_op_name_tmp_var(compiler, op_array->file, op_array->line);
2659 131           SPVM_OP* op_var_array_get = SPVM_OP_new_op_var(compiler, op_name_var_array);
2660 131           SPVM_OP* op_var_array_get_decl = SPVM_OP_new_op_var_decl(compiler, op_array->file,op_array->line);
2661 131           SPVM_OP_build_var_decl(compiler, op_var_array_get_decl, op_var_array_get, NULL, NULL);
2662 131           op_assign_array_get = SPVM_OP_build_assign(compiler, op_assign_array_get, op_var_array_get, op_array);
2663 131           SPVM_OP_insert_child(compiler, op_sequence, op_sequence->last, op_assign_array_get);
2664            
2665 131           SPVM_OP* op_assign_index_get = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_index->file, op_index->line);
2666 131           SPVM_OP* op_name_var_index = SPVM_OP_new_op_name_tmp_var(compiler, op_index->file, op_index->line);
2667 131           SPVM_OP* op_var_index_get = SPVM_OP_new_op_var(compiler, op_name_var_index);
2668 131           SPVM_OP* op_var_index_get_decl = SPVM_OP_new_op_var_decl(compiler, op_index->file,op_index->line);
2669 131           SPVM_OP_build_var_decl(compiler, op_var_index_get_decl, op_var_index_get, NULL, NULL);
2670 131           op_assign_index_get = SPVM_OP_build_assign(compiler, op_assign_index_get, op_var_index_get, op_index);
2671 131           SPVM_OP_insert_child(compiler, op_sequence, op_sequence->last, op_assign_index_get);
2672            
2673 131           SPVM_OP* op_array_access_clone_get = SPVM_OP_clone_op_array_access(compiler, op_array_access, op_var_array_get, op_var_index_get);
2674 131           SPVM_OP_build_assign(compiler, op_assign_save_old, op_var_old, op_array_access_clone_get);
2675            
2676 131           SPVM_OP* op_assign_array_set = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_array->file, op_array->line);
2677 131           SPVM_OP* op_var_array_set = SPVM_OP_new_op_var(compiler, op_name_var_array);
2678 131           SPVM_OP* op_var_array_set_decl = SPVM_OP_new_op_var_decl(compiler, op_array->file,op_array->line);
2679            
2680 131           SPVM_OP* op_assign_index_set = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_index->file, op_index->line);
2681 131           SPVM_OP* op_var_index_set = SPVM_OP_new_op_var(compiler, op_name_var_index);
2682 131           SPVM_OP* op_var_index_set_decl = SPVM_OP_new_op_var_decl(compiler, op_index->file,op_index->line);
2683            
2684 131           SPVM_OP* op_array_access_clone_set = SPVM_OP_clone_op_array_access(compiler, op_array_access, op_var_array_set, op_var_index_set);
2685 131           SPVM_OP* op_dist_clone_set = op_array_access_clone_set;
2686 131           SPVM_OP_build_assign(compiler, op_assign_update, op_dist_clone_set, op_culc);
2687             }
2688 8171 50         else if (op_dist->id == SPVM_OP_C_ID_FIELD_ACCESS) {
2689            
2690 8171           SPVM_OP* op_field_access = op_dist;
2691            
2692 8171           SPVM_OP* op_invocant = op_field_access->first;
2693 8171           SPVM_OP* op_name_field = op_field_access->uv.field_access->op_name;
2694            
2695 8171           SPVM_OP_cut_op(compiler, op_field_access->first);
2696            
2697 8171 100         if (op_invocant->id == SPVM_OP_C_ID_ARRAY_ACCESS) {
2698 86           SPVM_OP* op_array_access = op_invocant;
2699            
2700 86           SPVM_OP* op_array = op_array_access->first;
2701 86           SPVM_OP* op_index = op_array_access->last;
2702            
2703 86           SPVM_OP_cut_op(compiler, op_array_access->first);
2704 86           SPVM_OP_cut_op(compiler, op_array_access->last);
2705            
2706 86           SPVM_OP* op_assign_array_get = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_array->file, op_array->line);
2707 86           SPVM_OP* op_name_var_array = SPVM_OP_new_op_name_tmp_var(compiler, op_array->file, op_array->line);
2708 86           SPVM_OP* op_var_array_get = SPVM_OP_new_op_var(compiler, op_name_var_array);
2709 86           SPVM_OP* op_var_array_get_decl = SPVM_OP_new_op_var_decl(compiler, op_array->file,op_array->line);
2710 86           SPVM_OP_build_var_decl(compiler, op_var_array_get_decl, op_var_array_get, NULL, NULL);
2711 86           op_assign_array_get = SPVM_OP_build_assign(compiler, op_assign_array_get, op_var_array_get, op_array);
2712 86           SPVM_OP_insert_child(compiler, op_sequence, op_sequence->last, op_assign_array_get);
2713            
2714 86           SPVM_OP* op_assign_index_get = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_index->file, op_index->line);
2715 86           SPVM_OP* op_name_var_index = SPVM_OP_new_op_name_tmp_var(compiler, op_index->file, op_index->line);
2716 86           SPVM_OP* op_var_index_get = SPVM_OP_new_op_var(compiler, op_name_var_index);
2717 86           SPVM_OP* op_var_index_get_decl = SPVM_OP_new_op_var_decl(compiler, op_index->file,op_index->line);
2718 86           SPVM_OP_build_var_decl(compiler, op_var_index_get_decl, op_var_index_get, NULL, NULL);
2719 86           op_assign_index_get = SPVM_OP_build_assign(compiler, op_assign_index_get, op_var_index_get, op_index);
2720 86           SPVM_OP_insert_child(compiler, op_sequence, op_sequence->last, op_assign_index_get);
2721            
2722 86           SPVM_OP* op_array_field_access_clone_get = SPVM_OP_clone_op_array_field_access(compiler, op_field_access, op_name_field, op_array_access, op_var_array_get, op_var_index_get);
2723 86           SPVM_OP_build_assign(compiler, op_assign_save_old, op_var_old, op_array_field_access_clone_get);
2724            
2725 86           SPVM_OP* op_assign_array_set = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_array->file, op_array->line);
2726 86           SPVM_OP* op_var_array_set = SPVM_OP_new_op_var(compiler, op_name_var_array);
2727 86           SPVM_OP* op_var_array_set_decl = SPVM_OP_new_op_var_decl(compiler, op_array->file,op_array->line);
2728 86           SPVM_OP* op_assign_index_set = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_index->file, op_index->line);
2729 86           SPVM_OP* op_var_index_set = SPVM_OP_new_op_var(compiler, op_name_var_index);
2730 86           SPVM_OP* op_var_index_set_decl = SPVM_OP_new_op_var_decl(compiler, op_index->file,op_index->line);
2731 86           SPVM_OP* op_array_field_access_clone_set = SPVM_OP_clone_op_array_field_access(compiler, op_array_access, op_name_field, op_array_access, op_var_array_set, op_var_index_set);
2732 86           SPVM_OP* op_dist_clone_set = op_array_field_access_clone_set;
2733 86           SPVM_OP_build_assign(compiler, op_assign_update, op_dist_clone_set, op_culc);
2734             }
2735             else {
2736 8085           SPVM_OP* op_assign_invocant_get = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_invocant->file, op_invocant->line);
2737 8085           SPVM_OP* op_name_var_invocant = SPVM_OP_new_op_name_tmp_var(compiler, op_invocant->file, op_invocant->line);
2738 8085           SPVM_OP* op_var_invocant_get = SPVM_OP_new_op_var(compiler, op_name_var_invocant);
2739 8085           SPVM_OP* op_var_invocant_get_decl = SPVM_OP_new_op_var_decl(compiler, op_invocant->file,op_invocant->line);
2740 8085           SPVM_OP_build_var_decl(compiler, op_var_invocant_get_decl, op_var_invocant_get, NULL, NULL);
2741 8085           op_assign_invocant_get = SPVM_OP_build_assign(compiler, op_assign_invocant_get, op_var_invocant_get, op_invocant);
2742 8085           SPVM_OP_insert_child(compiler, op_sequence, op_sequence->last, op_assign_invocant_get);
2743            
2744 8085           SPVM_OP* op_name_field_get = SPVM_OP_new_op_name(compiler, op_name_field->uv.name, op_name_field->file, op_name_field->line);
2745 8085           SPVM_OP* op_field_access_clone_get = SPVM_OP_clone_op_field_access(compiler, op_field_access, op_var_invocant_get, op_name_field_get);
2746 8085           SPVM_OP_build_assign(compiler, op_assign_save_old, op_var_old, op_field_access_clone_get);
2747            
2748 8085           SPVM_OP* op_assign_invocant_set = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_invocant->file, op_invocant->line);
2749 8085           SPVM_OP* op_var_invocant_set = SPVM_OP_new_op_var(compiler, op_name_var_invocant);
2750 8085           SPVM_OP* op_var_invocant_set_decl = SPVM_OP_new_op_var_decl(compiler, op_invocant->file,op_invocant->line);
2751 8085           SPVM_OP* op_name_field_set = SPVM_OP_new_op_name(compiler, op_name_field->uv.name, op_name_field->file, op_name_field->line);
2752 8085           SPVM_OP* op_field_access_clone_set = SPVM_OP_clone_op_field_access(compiler, op_field_access, op_var_invocant_set, op_name_field_set);
2753 8085           SPVM_OP* op_dist_clone_set = op_field_access_clone_set;
2754 8171           SPVM_OP_build_assign(compiler, op_assign_update, op_dist_clone_set, op_culc);
2755             }
2756             }
2757             else {
2758 0           assert(0);
2759             }
2760            
2761 108237           SPVM_OP_insert_child(compiler, op_sequence, op_sequence->last, op_assign_save_old);
2762 108237           SPVM_OP_insert_child(compiler, op_sequence, op_sequence->last, op_assign_update);
2763            
2764             // Return the old value
2765 108237 100         if (op_special_assign->id == SPVM_OP_C_ID_POST_INC || op_special_assign->id == SPVM_OP_C_ID_POST_DEC) {
    100          
2766 80688           SPVM_OP* op_var_old_clone_ret = SPVM_OP_clone_op_var(compiler, op_var_old_clone);
2767 80688           SPVM_OP_insert_child(compiler, op_sequence, op_sequence->last, op_var_old_clone_ret);
2768             }
2769            
2770 108237           return op_sequence;
2771             }
2772              
2773 82782           SPVM_OP* SPVM_OP_build_inc(SPVM_COMPILER* compiler, SPVM_OP* op_inc, SPVM_OP* op_first) {
2774            
2775 82782           SPVM_OP_insert_child(compiler, op_inc, op_inc->last, op_first);
2776            
2777 82782           SPVM_OP* op_constant = SPVM_OP_new_op_constant_int(compiler, 1, op_first->file, op_first->line);
2778 82782           op_inc = SPVM_OP_build_special_assign(compiler, op_inc, op_first, op_constant);
2779            
2780 82782           return op_inc;
2781             }
2782              
2783 10353           SPVM_OP* SPVM_OP_build_dec(SPVM_COMPILER* compiler, SPVM_OP* op_dec, SPVM_OP* op_first) {
2784            
2785 10353           SPVM_OP_insert_child(compiler, op_dec, op_dec->last, op_first);
2786            
2787 10353           SPVM_OP* op_constant = SPVM_OP_new_op_constant_int(compiler, 1, op_first->file, op_first->line);
2788 10353           op_dec = SPVM_OP_build_special_assign(compiler, op_dec, op_first, op_constant);
2789            
2790 10353           return op_dec;
2791             }
2792              
2793 49161           SPVM_OP* SPVM_OP_build_logical_and(SPVM_COMPILER* compiler, SPVM_OP* op_logical_and, SPVM_OP* op_first, SPVM_OP* op_last) {
2794            
2795             // Convert && to if statement
2796             /* before
2797             AND
2798             first
2799             last
2800             */
2801            
2802             /* after
2803             IF if1
2804             CONDITION
2805             first
2806             IF if2
2807             CONDITION
2808             last
2809             TYPE_CONVERTION_BOOL
2810             condition_flag true
2811             TYPE_CONVERTION_BOOL
2812             0 false1
2813             TYPE_CONVERTION_BOOL
2814             0 false2
2815             */
2816            
2817 49161           SPVM_OP* op_if1 = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_IF, op_logical_and->file, op_logical_and->line);
2818            
2819             // Constant true
2820 49161           SPVM_OP* op_name_var_true = SPVM_OP_new_op_name(compiler, "$.condition_flag", op_logical_and->file, op_logical_and->line);
2821 49161           SPVM_OP* op_var_true = SPVM_OP_new_op_var(compiler, op_name_var_true);
2822 49161           SPVM_OP* op_assign_bool_true = SPVM_OP_new_op_assign_bool(compiler, op_var_true, op_if1->file, op_if1->line);
2823            
2824             // Constant false 1
2825 49161           SPVM_OP* op_constant_false1 = SPVM_OP_new_op_constant_int(compiler, 0, op_if1->file, op_if1->line);
2826 49161           SPVM_OP* op_assign_bool_false1 = SPVM_OP_new_op_assign_bool(compiler, op_constant_false1, op_if1->file, op_if1->line);
2827            
2828             // Constant false 2
2829 49161           SPVM_OP* op_constant_false2 = SPVM_OP_new_op_constant_int(compiler, 0, op_if1->file, op_if1->line);
2830 49161           SPVM_OP* op_assign_bool_false2 = SPVM_OP_new_op_assign_bool(compiler, op_constant_false2, op_if1->file, op_if1->line);
2831            
2832             // if2
2833 49161           SPVM_OP* op_if2 = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_IF, op_if1->file, op_if1->line);
2834            
2835             // Build if tree
2836 49161           int32_t no_scope = 1;
2837 49161           SPVM_OP_build_if_statement(compiler, op_if2, op_last, op_assign_bool_true, op_assign_bool_false1, no_scope);
2838 49161           SPVM_OP_build_if_statement(compiler, op_if1, op_first, op_if2, op_assign_bool_false2, no_scope);
2839              
2840 49161           SPVM_OP* op_name_var = SPVM_OP_new_op_name(compiler, "$.condition_flag", op_logical_and->file, op_logical_and->line);
2841 49161           SPVM_OP* op_var = SPVM_OP_new_op_var(compiler, op_name_var);
2842 49161           SPVM_OP* op_assign = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_logical_and->file, op_logical_and->line);
2843 49161           SPVM_OP_build_assign(compiler, op_assign, op_var, op_if1);
2844            
2845 49161           return op_assign;
2846             }
2847              
2848 8210           SPVM_OP* SPVM_OP_build_logical_or(SPVM_COMPILER* compiler, SPVM_OP* op_logical_or, SPVM_OP* op_first, SPVM_OP* op_last) {
2849            
2850             // Convert || to if statement
2851             // before
2852             // OR
2853             // first
2854             // last
2855            
2856             // after
2857             // IF if1
2858             // first
2859             // TYPE_CONVERTION_BOOL
2860             // condition_flag true1
2861             // IF if2
2862             // last
2863             // TYPE_CONVERTION_BOOL
2864             // condition_flag true2
2865             // TYPE_CONVERTION_BOOL
2866             // 0 false
2867            
2868 8210           SPVM_OP* op_if1 = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_IF, op_logical_or->file, op_logical_or->line);
2869            
2870             // Constant true 1
2871 8210           SPVM_OP* op_name_var_true1 = SPVM_OP_new_op_name(compiler, "$.condition_flag", op_logical_or->file, op_logical_or->line);
2872 8210           SPVM_OP* op_var_true1 = SPVM_OP_new_op_var(compiler, op_name_var_true1);
2873 8210           SPVM_OP* op_bool_true1 = SPVM_OP_new_op_assign_bool(compiler, op_var_true1, op_if1->file, op_if1->line);
2874            
2875             // Constant true 2
2876 8210           SPVM_OP* op_name_var_true2 = SPVM_OP_new_op_name(compiler, "$.condition_flag", op_logical_or->file, op_logical_or->line);
2877 8210           SPVM_OP* op_var_true2 = SPVM_OP_new_op_var(compiler, op_name_var_true2);
2878 8210           SPVM_OP* op_bool_true2 = SPVM_OP_new_op_assign_bool(compiler, op_var_true2, op_if1->file, op_if1->line);
2879            
2880             // Constant false
2881 8210           SPVM_OP* op_constant_false = SPVM_OP_new_op_constant_int(compiler, 0, op_if1->file, op_if1->line);
2882 8210           SPVM_OP* op_bool_false = SPVM_OP_new_op_assign_bool(compiler, op_constant_false, op_if1->file, op_if1->line);
2883            
2884             // if2
2885 8210           SPVM_OP* op_if2 = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_IF, op_if1->file, op_if1->line);
2886            
2887             // Build if tree
2888 8210           int32_t no_scope = 1;
2889 8210           SPVM_OP_build_if_statement(compiler, op_if2, op_last, op_bool_true2, op_bool_false, no_scope);
2890 8210           SPVM_OP_build_if_statement(compiler, op_if1, op_first, op_bool_true1, op_if2, no_scope);
2891            
2892 8210           SPVM_OP* op_name_var = SPVM_OP_new_op_name(compiler, "$.condition_flag", op_logical_or->file, op_logical_or->line);
2893 8210           SPVM_OP* op_var = SPVM_OP_new_op_var(compiler, op_name_var);
2894 8210           SPVM_OP* op_assign = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_logical_or->file, op_logical_or->line);
2895 8210           SPVM_OP_build_assign(compiler, op_assign, op_var, op_if1);
2896            
2897 8210           return op_assign;
2898             }
2899              
2900 3203           SPVM_OP* SPVM_OP_build_logical_not(SPVM_COMPILER* compiler, SPVM_OP* op_not, SPVM_OP* op_first) {
2901            
2902             // Convert ! to if statement
2903             // before
2904             // LOGICAL_NOT
2905             // first
2906            
2907             // after
2908             // IF
2909             // first
2910             // TYPE_CONVERTION_BOOL
2911             // 0
2912             // TYPE_CONVERTION_BOOL
2913             // 1
2914            
2915 3203           SPVM_OP* op_if = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_IF, op_not->file, op_not->line);
2916            
2917             // Constant false
2918 3203           SPVM_OP* op_constant_false = SPVM_OP_new_op_constant_int(compiler, 0, op_if->file, op_if->line);
2919 3203           SPVM_OP* op_assign_bool_false = SPVM_OP_new_op_assign_bool(compiler, op_constant_false, op_if->file, op_if->line);
2920              
2921             // Constant true
2922 3203           SPVM_OP* op_constant_true = SPVM_OP_new_op_constant_int(compiler, 1, op_if->file, op_if->line);
2923 3203           SPVM_OP* op_assign_bool_true = SPVM_OP_new_op_assign_bool(compiler, op_constant_true, op_if->file, op_if->line);
2924            
2925             // Build if tree
2926 3203           int32_t no_scope = 1;
2927 3203           SPVM_OP_build_if_statement(compiler, op_if, op_first, op_assign_bool_false, op_assign_bool_true, no_scope);
2928              
2929 3203           SPVM_OP* op_name_var = SPVM_OP_new_op_name(compiler, "$.condition_flag", op_not->file, op_not->line);
2930 3203           SPVM_OP* op_var = SPVM_OP_new_op_var(compiler, op_name_var);
2931 3203           SPVM_OP* op_assign = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_not->file, op_not->line);
2932 3203           SPVM_OP_build_assign(compiler, op_assign, op_var, op_if);
2933            
2934 3203           return op_assign;
2935             }
2936              
2937 3579682           SPVM_OP* SPVM_OP_build_assign(SPVM_COMPILER* compiler, SPVM_OP* op_assign, SPVM_OP* op_dist, SPVM_OP* op_src) {
2938            
2939             // Build op
2940             // Exchange left and right for excecution order
2941            
2942 3579682           SPVM_OP_insert_child(compiler, op_assign, op_assign->last, op_src);
2943 3579682           SPVM_OP_insert_child(compiler, op_assign, op_assign->last, op_dist);
2944            
2945 3579682           op_dist->is_dist = 1;
2946            
2947 3579682 100         if (!SPVM_OP_is_mutable(compiler, op_dist)) {
2948 3           SPVM_COMPILER_error(compiler, "The left operand of the assign operator must be mutable.\n at %s line %d", op_dist->file, op_dist->line);
2949             }
2950            
2951 3579682           return op_assign;
2952             }
2953              
2954 171938           SPVM_OP* SPVM_OP_build_return(SPVM_COMPILER* compiler, SPVM_OP* op_return, SPVM_OP* op_operand) {
2955            
2956 171938 100         if (op_operand) {
2957 159700           SPVM_OP_insert_child(compiler, op_return, op_return->last, op_operand);
2958             }
2959            
2960 171938           return op_return;
2961             }
2962              
2963 710076           SPVM_OP* SPVM_OP_build_operator_statement(SPVM_COMPILER* compiler, SPVM_OP* op_operator) {
2964            
2965 710076           return op_operator;
2966             }
2967              
2968 142703           SPVM_OP* SPVM_OP_build_die(SPVM_COMPILER* compiler, SPVM_OP* op_die, SPVM_OP* op_operand, SPVM_OP* op_type) {
2969            
2970 142703 100         if (!op_operand || op_operand->id == SPVM_OP_C_ID_UNDEF) {
    100          
2971             // Default error message
2972 13           op_operand = SPVM_OP_new_op_constant_string(compiler, "Error", strlen("Error"), op_die->file, op_die->line);
2973             }
2974            
2975             // Exception variable
2976 142703           SPVM_OP* op_exception_var = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_EXCEPTION_VAR, op_operand->file, op_operand->line);
2977            
2978             // Assign
2979 142703           SPVM_OP* op_assign = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, op_operand->file, op_operand->line);
2980 142703           SPVM_OP_build_assign(compiler, op_assign, op_exception_var, op_operand);
2981            
2982 142703           SPVM_OP_insert_child(compiler, op_die, op_die->last, op_assign);
2983            
2984 142703 100         if (!op_type) {
2985 142694           op_type = SPVM_OP_new_op_unresolved_type(compiler, "Error", 0, 0, op_die->file, op_die->line);
2986             }
2987            
2988 142703           SPVM_OP_insert_child(compiler, op_die, op_die->last, op_type);
2989            
2990 142703           return op_die;
2991             }
2992              
2993 262           SPVM_OP* SPVM_OP_build_warn(SPVM_COMPILER* compiler, SPVM_OP* op_warn, SPVM_OP* op_operand) {
2994            
2995 262 100         if (!op_operand || op_operand->id == SPVM_OP_C_ID_UNDEF) {
    100          
2996             // Default warn message
2997 34           op_operand = SPVM_OP_new_op_constant_string(compiler, "Warning", strlen("Warning"), op_warn->file, op_warn->line);
2998             }
2999            
3000 262           SPVM_OP_insert_child(compiler, op_warn, op_warn->last, op_operand);
3001            
3002 262           return op_warn;
3003             }
3004              
3005 64           SPVM_OP* SPVM_OP_build_basic_type_id(SPVM_COMPILER* compiler, SPVM_OP* op_basic_type_id, SPVM_OP* op_type) {
3006            
3007 64           SPVM_OP_insert_child(compiler, op_basic_type_id, op_basic_type_id->last, op_type);
3008            
3009 64           return op_basic_type_id;
3010             }
3011              
3012 313           SPVM_OP* SPVM_OP_build_print(SPVM_COMPILER* compiler, SPVM_OP* op_print, SPVM_OP* op_operand) {
3013            
3014 313           SPVM_OP_insert_child(compiler, op_print, op_print->last, op_operand);
3015            
3016 313           return op_print;
3017             }
3018              
3019 1068           SPVM_OP* SPVM_OP_build_make_read_only(SPVM_COMPILER* compiler, SPVM_OP* op_make_read_only, SPVM_OP* op_operand) {
3020            
3021 1068           SPVM_OP_insert_child(compiler, op_make_read_only, op_make_read_only->last, op_operand);
3022            
3023 1068           return op_make_read_only;
3024             }
3025              
3026 340105           SPVM_OP* SPVM_OP_build_basic_type(SPVM_COMPILER* compiler, SPVM_OP* op_name) {
3027            
3028 340105           const char* name = op_name->uv.name;
3029            
3030             SPVM_OP* op_type;
3031            
3032             // Add basic type
3033 340105           SPVM_BASIC_TYPE* found_basic_type = SPVM_HASH_get(compiler->basic_type_symtable, name, strlen(name));
3034 340105 100         if (found_basic_type) {
3035 130975           op_type = SPVM_OP_new_op_type(compiler, name, found_basic_type, 0, 0, op_name->file, op_name->line);
3036             }
3037             else {
3038 209130           SPVM_BASIC_TYPE* new_basic_type = SPVM_LIST_get(compiler->basic_types, 0);
3039 209130           op_type = SPVM_OP_new_op_type(compiler, name, new_basic_type, 0, 0, op_name->file, op_name->line);
3040             }
3041            
3042 340105           SPVM_OP_insert_child(compiler, op_type, op_type->last, op_name);
3043            
3044 340105           return op_type;
3045             }
3046              
3047 2940           SPVM_OP* SPVM_OP_build_ref_type(SPVM_COMPILER* compiler, SPVM_OP* op_type_original) {
3048            
3049             // Type
3050 2940           SPVM_TYPE* type = SPVM_TYPE_new(compiler, op_type_original->uv.type->basic_type->id, op_type_original->uv.type->dimension, SPVM_NATIVE_C_TYPE_FLAG_REF);
3051 2940           type->unresolved_basic_type_name = op_type_original->uv.type->unresolved_basic_type_name;
3052            
3053             // Type OP
3054 2940           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type->unresolved_basic_type_name, type->basic_type, type->dimension, type->flag, op_type_original->file, op_type_original->line);
3055 2940           SPVM_OP_insert_child(compiler, op_type, op_type->last, op_type_original);
3056              
3057 2940           return op_type;
3058             }
3059              
3060 9661           SPVM_OP* SPVM_OP_build_mutable_type(SPVM_COMPILER* compiler, SPVM_OP* op_type_elem) {
3061            
3062             // Type
3063 9661           SPVM_TYPE* type = SPVM_TYPE_new(compiler, op_type_elem->uv.type->basic_type->id, op_type_elem->uv.type->dimension, op_type_elem->uv.type->flag | SPVM_NATIVE_C_TYPE_FLAG_MUTABLE);
3064 9661           type->unresolved_basic_type_name = op_type_elem->uv.type->unresolved_basic_type_name;
3065            
3066             // Type OP
3067 9661           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type->unresolved_basic_type_name, type->basic_type, type->dimension, type->flag, op_type_elem->file, op_type_elem->line);
3068            
3069 9661           return op_type;
3070             }
3071              
3072 134785           SPVM_OP* SPVM_OP_build_array_type(SPVM_COMPILER* compiler, SPVM_OP* op_type_elem, SPVM_OP* op_length) {
3073            
3074             // Type
3075 134785           SPVM_TYPE* type = SPVM_TYPE_new(compiler, op_type_elem->uv.type->basic_type->id, op_type_elem->uv.type->dimension + 1, 0);
3076 134785           type->unresolved_basic_type_name = op_type_elem->uv.type->unresolved_basic_type_name;
3077            
3078             // Type OP
3079 134785           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type->unresolved_basic_type_name, type->basic_type, type->dimension, type->flag, op_type_elem->file, op_type_elem->line);
3080 134785           SPVM_OP_insert_child(compiler, op_type, op_type->last, op_type_elem);
3081            
3082 134785 100         if (op_length) {
3083 21628           SPVM_OP_insert_child(compiler, op_type, op_type->last, op_length);
3084             }
3085             else {
3086 113157           SPVM_OP* op_do_nothing = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_DO_NOTHING, op_type_elem->file, op_type_elem->line);
3087 113157           SPVM_OP_insert_child(compiler, op_type, op_type->last, op_do_nothing);
3088             }
3089            
3090 134785           return op_type;
3091             }
3092              
3093 691743           SPVM_OP* SPVM_OP_new_op_assign_bool(SPVM_COMPILER* compiler, SPVM_OP* op_operand, const char* file, int32_t line) {
3094 691743           SPVM_OP* op_bool = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_BOOL, file, line);
3095 691743           SPVM_OP_insert_child(compiler, op_bool, op_bool->last, op_operand);
3096              
3097 691743           SPVM_OP* op_name_var = SPVM_OP_new_op_name(compiler, "$.condition_flag", file, line);
3098 691743           SPVM_OP* op_var = SPVM_OP_new_op_var(compiler, op_name_var);
3099 691743           SPVM_OP* op_assign = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ASSIGN, file, line);
3100 691743           SPVM_OP_build_assign(compiler, op_assign, op_var, op_bool);
3101            
3102 691743           return op_assign;
3103             }
3104              
3105 2353327           SPVM_OP* SPVM_OP_new_op_var_decl(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3106 2353327           SPVM_OP* op_var_decl = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_VAR_DECL, file, line);
3107 2353327           SPVM_VAR_DECL* var_decl = SPVM_VAR_DECL_new(compiler);
3108            
3109 2353327           op_var_decl->uv.var_decl = var_decl;
3110 2353327           var_decl->op_var_decl = op_var_decl;
3111            
3112 2353327           return op_var_decl;
3113             }
3114              
3115 528120           SPVM_OP* SPVM_OP_new_op_var_decl_arg(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3116 528120           SPVM_OP* op_var_decl = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_VAR_DECL, file, line);
3117 528120           SPVM_VAR_DECL* var_decl = SPVM_VAR_DECL_new_arg(compiler);
3118            
3119 528120           op_var_decl->uv.var_decl = var_decl;
3120 528120           var_decl->op_var_decl = op_var_decl;
3121            
3122 528120           return op_var_decl;
3123             }
3124              
3125 358080           SPVM_OP* SPVM_OP_new_op_attribute(SPVM_COMPILER* compiler, int32_t id, const char* file, int32_t line) {
3126 358080           SPVM_OP* op_attribute = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ATTRIBUTE, file, line);
3127            
3128 358080           SPVM_ATTRIBUTE* attribute = SPVM_ATTRIBUTE_new(compiler);
3129 358080           attribute->id = id;
3130 358080           op_attribute->uv.attribute = attribute;
3131            
3132 358080           return op_attribute;
3133             }
3134              
3135 41949           SPVM_OP* SPVM_OP_new_op_undef(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3136 41949           SPVM_OP* op_undef = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_UNDEF, file, line);
3137            
3138 41949           return op_undef;
3139             }
3140              
3141 1584182           SPVM_OP* SPVM_OP_new_op_block(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3142 1584182           SPVM_OP* op_block = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_BLOCK, file, line);
3143            
3144 1584182           SPVM_BLOCK* block = SPVM_BLOCK_new(compiler);
3145 1584182           op_block->uv.block = block;
3146            
3147 1584182           return op_block;
3148             }
3149              
3150 81851           SPVM_OP* SPVM_OP_new_op_use(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3151 81851           SPVM_OP* op_use = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_USE, file, line);
3152            
3153 81851           SPVM_USE* use = SPVM_USE_new(compiler);
3154 81851           op_use->uv.use = use;
3155            
3156 81851           return op_use;
3157             }
3158              
3159 6112962           SPVM_OP* SPVM_OP_new_op_name(SPVM_COMPILER* compiler, const char* name, const char* file, int32_t line) {
3160            
3161 6112962           SPVM_OP* op_name = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_NAME, file, line);
3162            
3163 6112962           SPVM_STRING* name_string = SPVM_STRING_new(compiler, name, strlen(name));
3164            
3165 6112962           name = name_string->value;
3166            
3167 6112962           op_name->uv.name = name;
3168            
3169 6112962           return op_name;
3170             }
3171              
3172 6955610           SPVM_OP* SPVM_OP_new_op_var(SPVM_COMPILER* compiler, SPVM_OP* op_name) {
3173            
3174 6955610           SPVM_OP* op_var = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_VAR, op_name->file, op_name->line);
3175              
3176 6955610           SPVM_VAR* var = SPVM_VAR_new(compiler);
3177 6955610           var->op_name = op_name;
3178 6955610           var->name = op_name->uv.name;
3179 6955610           op_var->uv.var = var;
3180            
3181 6955610           return op_var;
3182             }
3183              
3184 13072           SPVM_OP* SPVM_OP_new_op_class_var_access(SPVM_COMPILER* compiler, SPVM_OP* op_class_var_name) {
3185            
3186 13072           const char* class_var_name = op_class_var_name->uv.name;
3187            
3188 13072           SPVM_OP* op_class_var_access = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_CLASS_VAR_ACCESS, op_class_var_name->file, op_class_var_name->line);
3189            
3190 13072           SPVM_CLASS_VAR_ACCESS* class_var_access = SPVM_CLASS_VAR_ACCESS_new(compiler);
3191 13072           class_var_access->op_name = op_class_var_name;
3192 13072           op_class_var_access->uv.class_var_access = class_var_access;
3193            
3194 13072           return op_class_var_access;
3195             }
3196              
3197 2283           SPVM_OP* SPVM_OP_new_op_array_field_access(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3198            
3199 2283           SPVM_OP* op_array_field_access = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ARRAY_FIELD_ACCESS, file, line);
3200            
3201 2283           SPVM_ARRAY_FIELD_ACCESS* array_field_access = SPVM_ARRAY_FIELD_ACCESS_new(compiler);
3202            
3203 2283           op_array_field_access->uv.array_field_access = array_field_access;
3204            
3205 2283           return op_array_field_access;
3206             }
3207              
3208 353808           SPVM_OP* SPVM_OP_clone_op_var(SPVM_COMPILER* compiler, SPVM_OP* op_var) {
3209            
3210 353808           SPVM_OP* op_name = SPVM_OP_new_op_name(compiler, op_var->uv.var->name, op_var->file, op_var->line);
3211 353808           SPVM_OP* op_var_clone = SPVM_OP_new_op_var(compiler, op_name);
3212 353808           SPVM_VAR* var_clone = op_var_clone->uv.var;
3213            
3214 353808           var_clone->var_decl = op_var->uv.var->var_decl;
3215            
3216 353808           return op_var_clone;
3217             }
3218              
3219 16170           SPVM_OP* SPVM_OP_clone_op_field_access(SPVM_COMPILER* compiler, SPVM_OP* op_field_access, SPVM_OP* op_var_invocant, SPVM_OP* op_name_field) {
3220            
3221 16170           SPVM_OP* op_var_invocant_clone = SPVM_OP_clone_op_var(compiler, op_var_invocant);
3222            
3223 16170           SPVM_OP* op_field_access_clone = SPVM_OP_new_op_field_access(compiler, op_field_access->file, op_field_access->line);
3224            
3225 16170           SPVM_OP* op_name_field_clone = SPVM_OP_new_op_name(compiler,op_name_field->uv.name, op_name_field->file, op_name_field->line);
3226            
3227 16170           op_field_access_clone = SPVM_OP_build_field_access(compiler, op_field_access_clone, op_var_invocant_clone, op_name_field_clone);
3228            
3229 16170           return op_field_access_clone;
3230             }
3231              
3232 262           SPVM_OP* SPVM_OP_clone_op_array_access(SPVM_COMPILER* compiler, SPVM_OP* op_array_access, SPVM_OP* op_var_array, SPVM_OP* op_var_index) {
3233 262           SPVM_OP* op_var_array_clone = SPVM_OP_clone_op_var(compiler, op_var_array);
3234 262           SPVM_OP* op_var_index_clone = SPVM_OP_clone_op_var(compiler, op_var_index);
3235 262           SPVM_OP* op_array_access_clone = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ARRAY_ACCESS, op_array_access->file, op_array_access->line);
3236            
3237 262           op_array_access_clone = SPVM_OP_build_array_access(compiler, op_array_access_clone, op_var_array_clone, op_var_index_clone);
3238            
3239 262           op_array_access_clone->flag = op_array_access->flag;
3240            
3241 262           return op_array_access_clone;
3242             }
3243              
3244 172           SPVM_OP* SPVM_OP_clone_op_array_field_access(SPVM_COMPILER* compiler, SPVM_OP* op_field_access, SPVM_OP* op_name_field, SPVM_OP* op_array_access, SPVM_OP* op_var_array, SPVM_OP* op_var_index) {
3245            
3246 172           SPVM_OP* op_field_access_clone = SPVM_OP_new_op_field_access(compiler, op_field_access->file, op_field_access->line);
3247            
3248 172           SPVM_OP* op_var_array_clone = SPVM_OP_clone_op_var(compiler, op_var_array);
3249 172           SPVM_OP* op_var_index_clone = SPVM_OP_clone_op_var(compiler, op_var_index);
3250            
3251 172           SPVM_OP* op_array_access_clone = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_ARRAY_ACCESS, op_array_access->file, op_array_access->line);
3252 172           op_array_access_clone = SPVM_OP_build_array_access(compiler, op_array_access_clone, op_var_array_clone, op_var_index_clone);
3253            
3254 172           op_array_access_clone->flag = op_array_access->flag;
3255            
3256 172           SPVM_OP* op_name_field_clone = SPVM_OP_new_op_name(compiler, op_name_field->uv.name, op_name_field->file, op_name_field->line);
3257            
3258 172           op_field_access_clone = SPVM_OP_build_field_access(compiler, op_field_access_clone, op_array_access_clone, op_name_field_clone);
3259            
3260 172           return op_field_access_clone;
3261             }
3262              
3263 1122           SPVM_OP* SPVM_OP_clone_op_deref(SPVM_COMPILER* compiler, SPVM_OP* op_deref, SPVM_OP* op_var) {
3264            
3265 1122           SPVM_OP* op_deref_clone = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_DEREF, op_deref->file, op_deref->line);
3266            
3267 1122           SPVM_OP* op_var_clone = SPVM_OP_clone_op_var(compiler, op_var);
3268            
3269 1122           SPVM_OP_build_unary_op(compiler, op_deref_clone, op_var_clone);
3270            
3271 1122           return op_deref_clone;
3272             }
3273              
3274 1015383           SPVM_OP* SPVM_OP_new_op_constant(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3275 1015383           SPVM_OP* op_constant = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_CONSTANT, file, line);
3276 1015383           SPVM_CONSTANT* constant = SPVM_CONSTANT_new(compiler);
3277 1015383 50         assert(constant);
3278 1015383           op_constant->uv.constant = constant;
3279 1015383           constant->op_constant = op_constant;
3280            
3281 1015383           return op_constant;
3282             }
3283              
3284 50412           SPVM_OP* SPVM_OP_new_op_constant_byte(SPVM_COMPILER* compiler, int8_t value, const char* file, int32_t line) {
3285 50412           SPVM_OP* op_constant = SPVM_OP_new_op_constant(compiler, file, line);
3286 50412           SPVM_CONSTANT* constant = op_constant->uv.constant;
3287            
3288 50412           constant->value.bval = value;
3289 50412           SPVM_OP* op_constant_type = SPVM_OP_new_op_byte_type(compiler, file, line);
3290 50412           constant->type = op_constant_type->uv.type;
3291            
3292 50412           return op_constant;
3293             }
3294              
3295 0           SPVM_OP* SPVM_OP_new_op_constant_short(SPVM_COMPILER* compiler, int16_t value, const char* file, int32_t line) {
3296 0           SPVM_OP* op_constant = SPVM_OP_new_op_constant(compiler, file, line);
3297 0           SPVM_CONSTANT* constant = op_constant->uv.constant;
3298            
3299 0           constant->value.sval = value;
3300 0           SPVM_OP* op_constant_type = SPVM_OP_new_op_short_type(compiler, file, line);
3301 0           constant->type = op_constant_type->uv.type;
3302            
3303 0           return op_constant;
3304             }
3305              
3306 757012           SPVM_OP* SPVM_OP_new_op_constant_int(SPVM_COMPILER* compiler, int32_t value, const char* file, int32_t line) {
3307 757012           SPVM_OP* op_constant = SPVM_OP_new_op_constant(compiler, file, line);
3308 757012           SPVM_CONSTANT* constant = op_constant->uv.constant;
3309            
3310 757012 50         assert(op_constant->uv.constant);
3311            
3312 757012           constant->value.ival = value;
3313 757012           SPVM_OP* op_constant_type = SPVM_OP_new_op_int_type(compiler, file, line);
3314 757012           constant->type = op_constant_type->uv.type;
3315            
3316 757012           return op_constant;
3317             }
3318              
3319 5560           SPVM_OP* SPVM_OP_new_op_constant_long(SPVM_COMPILER* compiler, int64_t value, const char* file, int32_t line) {
3320 5560           SPVM_OP* op_constant = SPVM_OP_new_op_constant(compiler, file, line);
3321 5560           SPVM_CONSTANT* constant = op_constant->uv.constant;
3322            
3323 5560           constant->value.lval = value;
3324 5560           SPVM_OP* op_constant_type = SPVM_OP_new_op_long_type(compiler, file, line);
3325 5560           constant->type = op_constant_type->uv.type;
3326            
3327 5560           return op_constant;
3328             }
3329              
3330 1359           SPVM_OP* SPVM_OP_new_op_constant_float(SPVM_COMPILER* compiler, float value, const char* file, int32_t line) {
3331 1359           SPVM_OP* op_constant = SPVM_OP_new_op_constant(compiler, file, line);
3332 1359           SPVM_CONSTANT* constant = op_constant->uv.constant;
3333            
3334 1359           constant->value.fval = value;
3335 1359           SPVM_OP* op_constant_type = SPVM_OP_new_op_float_type(compiler, file, line);
3336 1359           constant->type = op_constant_type->uv.type;
3337            
3338 1359           return op_constant;
3339             }
3340              
3341 2138           SPVM_OP* SPVM_OP_new_op_constant_double(SPVM_COMPILER* compiler, double value, const char* file, int32_t line) {
3342 2138           SPVM_OP* op_constant = SPVM_OP_new_op_constant(compiler, file, line);
3343 2138           SPVM_CONSTANT* constant = op_constant->uv.constant;
3344            
3345 2138           constant->value.dval = value;
3346 2138           SPVM_OP* op_constant_type = SPVM_OP_new_op_double_type(compiler, file, line);
3347 2138           constant->type = op_constant_type->uv.type;
3348            
3349 2138           return op_constant;
3350             }
3351              
3352 198902           SPVM_OP* SPVM_OP_new_op_constant_string(SPVM_COMPILER* compiler, const char* string, int32_t length, const char* file, int32_t line) {
3353              
3354 198902           SPVM_OP* op_constant = SPVM_OP_new_op_constant(compiler, file, line);
3355 198902           SPVM_CONSTANT* constant = op_constant->uv.constant;
3356            
3357 198902           SPVM_STRING* cached_string_string = SPVM_STRING_new(compiler, string, length);
3358 198902           const char* cached_string = cached_string_string->value;
3359              
3360 198902           constant->value.oval = (void*)cached_string;
3361 198902           SPVM_OP* op_constant_type = SPVM_OP_new_op_string_type(compiler, file, line);
3362 198902           constant->type = op_constant_type->uv.type;
3363 198902           constant->string_length = length;
3364            
3365 198902           return op_constant;
3366             }
3367              
3368 107748           SPVM_OP* SPVM_OP_new_op_void_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3369 107748           SPVM_TYPE* type = SPVM_TYPE_new_void_type(compiler);
3370 107748           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type->unresolved_basic_type_name, type->basic_type, type->dimension, type->flag, file, line);
3371 107748           return op_type;
3372             }
3373              
3374 75946           SPVM_OP* SPVM_OP_new_op_byte_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3375 75946           SPVM_TYPE* type = SPVM_TYPE_new_byte_type(compiler);
3376 75946           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type->unresolved_basic_type_name, type->basic_type, type->dimension, type->flag, file, line);
3377 75946           return op_type;
3378             }
3379              
3380 19758           SPVM_OP* SPVM_OP_new_op_short_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3381 19758           SPVM_TYPE* type = SPVM_TYPE_new_short_type(compiler);
3382 19758           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type->unresolved_basic_type_name, type->basic_type, type->dimension, type->flag, file, line);
3383 19758           return op_type;
3384             }
3385              
3386 2697465           SPVM_OP* SPVM_OP_new_op_int_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3387 2697465           SPVM_TYPE* type = SPVM_TYPE_new_int_type(compiler);
3388 2697465           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type->unresolved_basic_type_name, type->basic_type, type->dimension, type->flag, file, line);
3389 2697465           return op_type;
3390             }
3391              
3392 0           SPVM_OP* SPVM_OP_new_op_bool_object_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3393 0           SPVM_TYPE* type = SPVM_TYPE_new_bool_object_type(compiler);
3394 0           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type->unresolved_basic_type_name, type->basic_type, type->dimension, type->flag, file, line);
3395 0           return op_type;
3396             }
3397              
3398 48065           SPVM_OP* SPVM_OP_new_op_long_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3399 48065           SPVM_TYPE* type = SPVM_TYPE_new_long_type(compiler);
3400 48065           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type->unresolved_basic_type_name, type->basic_type, type->dimension, type->flag, file, line);
3401 48065           return op_type;
3402             }
3403              
3404 29959           SPVM_OP* SPVM_OP_new_op_float_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3405 29959           SPVM_TYPE* type = SPVM_TYPE_new_float_type(compiler);
3406 29959           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type->unresolved_basic_type_name, type->basic_type, type->dimension, type->flag, file, line);
3407 29959           return op_type;
3408             }
3409              
3410 39635           SPVM_OP* SPVM_OP_new_op_double_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3411 39635           SPVM_TYPE* type = SPVM_TYPE_new_double_type(compiler);
3412 39635           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type->unresolved_basic_type_name, type->basic_type, type->dimension, type->flag, file, line);
3413 39635           return op_type;
3414             }
3415              
3416 331654           SPVM_OP* SPVM_OP_new_op_string_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3417 331654           SPVM_TYPE* type = SPVM_TYPE_new_string_type(compiler);
3418 331654           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type->unresolved_basic_type_name, type->basic_type, type->dimension, type->flag, file, line);
3419 331654           return op_type;
3420             }
3421              
3422 0           SPVM_OP* SPVM_OP_new_op_undef_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3423 0           SPVM_TYPE* type = SPVM_TYPE_new_undef_type(compiler);
3424 0           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type->unresolved_basic_type_name, type->basic_type, type->dimension, type->flag, file, line);
3425 0           return op_type;
3426             }
3427              
3428 0           SPVM_OP* SPVM_OP_new_op_byte_ref_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3429 0           SPVM_TYPE* type = SPVM_TYPE_new_byte_ref_type(compiler);
3430 0           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type->unresolved_basic_type_name, type->basic_type, type->dimension, type->flag, file, line);
3431 0           return op_type;
3432             }
3433              
3434 0           SPVM_OP* SPVM_OP_new_op_short_ref_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3435 0           SPVM_TYPE* type = SPVM_TYPE_new_short_ref_type(compiler);
3436 0           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type->unresolved_basic_type_name, type->basic_type, type->dimension, type->flag, file, line);
3437 0           return op_type;
3438             }
3439              
3440 0           SPVM_OP* SPVM_OP_new_op_int_ref_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3441 0           SPVM_TYPE* type = SPVM_TYPE_new_int_ref_type(compiler);
3442 0           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type->unresolved_basic_type_name, type->basic_type, type->dimension, type->flag, file, line);
3443 0           return op_type;
3444             }
3445              
3446 0           SPVM_OP* SPVM_OP_new_op_long_ref_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3447 0           SPVM_TYPE* type = SPVM_TYPE_new_long_ref_type(compiler);
3448 0           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type->unresolved_basic_type_name, type->basic_type, type->dimension, type->flag, file, line);
3449 0           return op_type;
3450             }
3451              
3452 0           SPVM_OP* SPVM_OP_new_op_float_ref_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3453 0           SPVM_TYPE* type = SPVM_TYPE_new_float_ref_type(compiler);
3454 0           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type->unresolved_basic_type_name, type->basic_type, type->dimension, type->flag, file, line);
3455 0           return op_type;
3456             }
3457              
3458 0           SPVM_OP* SPVM_OP_new_op_double_ref_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3459 0           SPVM_TYPE* type = SPVM_TYPE_new_double_ref_type(compiler);
3460 0           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type->unresolved_basic_type_name, type->basic_type, type->dimension, type->flag, file, line);
3461 0           return op_type;
3462             }
3463              
3464 52248           SPVM_OP* SPVM_OP_new_op_any_object_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3465 52248           SPVM_TYPE* type = SPVM_TYPE_new_any_object_type(compiler);
3466 52248           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type->unresolved_basic_type_name, type->basic_type, type->dimension, type->flag, file, line);
3467 52248           return op_type;
3468             }
3469              
3470 258835           SPVM_OP* SPVM_OP_new_op_unresolved_type(SPVM_COMPILER* compiler, const char* name, int32_t type_dimension, int32_t type_flag, const char* file, int32_t line) {
3471 258835           SPVM_TYPE* type = SPVM_TYPE_new_unresolved_type(compiler, name, type_dimension, type_flag);
3472 258835           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type->unresolved_basic_type_name, type->basic_type, type->dimension, type->flag, file, line);
3473 258835           return op_type;
3474             }
3475              
3476 51           SPVM_OP* SPVM_OP_new_op_any_object_array_type(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3477 51           SPVM_TYPE* type = SPVM_TYPE_new_any_object_array_type(compiler);
3478 51           SPVM_OP* op_type = SPVM_OP_new_op_type(compiler, type->unresolved_basic_type_name, type->basic_type, type->dimension, type->flag, file, line);
3479            
3480 51           return op_type;
3481             }
3482              
3483 4559157           SPVM_OP* SPVM_OP_new_op_type(SPVM_COMPILER* compiler, const char* unresolved_basic_type_name, SPVM_BASIC_TYPE* basic_type, int32_t type_dimension, int32_t type_flag, const char* file, int32_t line) {
3484            
3485 4559157           SPVM_OP* op_type = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_TYPE, file, line);
3486 4559157           SPVM_TYPE* type = SPVM_TYPE_new_uninitialized(compiler);
3487            
3488 4559157           type->unresolved_basic_type_name = unresolved_basic_type_name;
3489 4559157           type->basic_type = basic_type;
3490 4559157           type->dimension = type_dimension;
3491 4559157           type->flag = type_flag;
3492            
3493 4559157           op_type->uv.type = type;
3494            
3495 4559157           SPVM_LIST_push(compiler->op_types, op_type);
3496            
3497 4559157           return op_type;
3498             }
3499              
3500 30           SPVM_OP* SPVM_OP_new_op_true(SPVM_COMPILER* compiler, SPVM_OP* op) {
3501            
3502 30           SPVM_OP* op_call_method = SPVM_OP_new_op_call_method(compiler, op->file, op->line);
3503 30           SPVM_OP* op_name_basic_type = SPVM_OP_new_op_name(compiler, "Bool", op->file, op->line);
3504 30           SPVM_OP* op_name_method = SPVM_OP_new_op_name(compiler, "TRUE", op->file, op->line);
3505 30           SPVM_OP* op_operators = SPVM_OP_new_op_list(compiler, op->file, op->line);
3506 30           SPVM_OP* op_type = SPVM_OP_build_basic_type(compiler, op_name_basic_type);
3507            
3508 30           op_call_method = SPVM_OP_build_call_method(compiler, op_call_method, op_type, op_name_method, op_operators);
3509            
3510 30           return op_call_method;
3511             }
3512              
3513 28           SPVM_OP* SPVM_OP_new_op_false(SPVM_COMPILER* compiler, SPVM_OP* op) {
3514            
3515 28           SPVM_OP* op_call_method = SPVM_OP_new_op_call_method(compiler, op->file, op->line);
3516 28           SPVM_OP* op_name_basic_type = SPVM_OP_new_op_name(compiler, "Bool", op->file, op->line);
3517 28           SPVM_OP* op_name_method = SPVM_OP_new_op_name(compiler, "FALSE", op->file, op->line);
3518 28           SPVM_OP* op_operators = SPVM_OP_new_op_list(compiler, op->file, op->line);
3519 28           SPVM_OP* op_type = SPVM_OP_build_basic_type(compiler, op_name_basic_type);
3520            
3521 28           op_call_method = SPVM_OP_build_call_method(compiler, op_call_method, op_type, op_name_method, op_operators);
3522            
3523 28           return op_call_method;
3524             }
3525              
3526 189224           SPVM_OP* SPVM_OP_new_op_field_access(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3527 189224           SPVM_OP* op_field_access = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_FIELD_ACCESS, file, line);
3528              
3529 189224           SPVM_FIELD_ACCESS* field_access = SPVM_FIELD_ACCESS_new(compiler);
3530              
3531 189224           op_field_access->uv.field_access = field_access;
3532            
3533 189224           return op_field_access;
3534             }
3535              
3536 255949           SPVM_OP* SPVM_OP_new_op_call_method(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3537              
3538 255949           SPVM_OP* op_call_method = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_CALL_METHOD, file, line);
3539            
3540 255949           SPVM_CALL_METHOD* call_method = SPVM_CALL_METHOD_new(compiler);
3541            
3542 255949           op_call_method->uv.call_method = call_method;
3543            
3544 255949           return op_call_method;
3545             }
3546              
3547 2175492           SPVM_OP* SPVM_OP_new_op_list(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3548            
3549 2175492           SPVM_OP* op_pushmark = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_PUSHMARK, file, line);
3550            
3551 2175492           SPVM_OP* op_list = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_LIST, file, line);
3552 2175492           SPVM_OP_insert_child(compiler, op_list, op_list->last, op_pushmark);
3553            
3554 2175492           return op_list;
3555             }
3556              
3557 134555           SPVM_OP* SPVM_OP_new_op_name_tmp_var(SPVM_COMPILER* compiler, const char* file, int32_t line) {
3558            
3559             // Temparary variable name
3560 134555           char* name = SPVM_ALLOCATOR_alloc_memory_block_permanent(compiler->current_each_compile_allocator, strlen("$.tmp_in_op2147483647") + 1);
3561 134555           sprintf(name, "$.tmp_in_op%d", compiler->current_tmp_vars_length);
3562 134555           compiler->current_tmp_vars_length++;
3563 134555           SPVM_OP* op_name = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_NAME, file, line);
3564 134555           op_name->uv.name = name;
3565            
3566 134555           return op_name;
3567             }
3568              
3569 54061880           SPVM_OP* SPVM_OP_new_op(SPVM_COMPILER* compiler, int32_t id, const char* file, int32_t line) {
3570              
3571 54061880           SPVM_OP *op = SPVM_ALLOCATOR_alloc_memory_block_tmp(compiler->current_each_compile_allocator, sizeof(SPVM_OP));
3572            
3573 54061880           memset(op, 0, sizeof(SPVM_OP));
3574            
3575 54061880           op->id = id;
3576            
3577 54061880 50         assert(!op->file);
3578 54061880 50         assert(!op->line);
3579            
3580 54061880           op->file = file;
3581 54061880           op->line = line;
3582            
3583 54061880           SPVM_LIST_push(compiler->ops, op);
3584            
3585 54061880           return op;
3586             }
3587              
3588 19333           int32_t SPVM_OP_is_allowed(SPVM_COMPILER* compiler, SPVM_BASIC_TYPE* basic_type_current, SPVM_BASIC_TYPE* basic_type_dist) {
3589            
3590 19333           SPVM_LIST* allows = basic_type_dist->allows;
3591            
3592 19333           const char* current_basic_type_name = basic_type_current->name;
3593 19333           const char* dist_basic_type_name = basic_type_dist->name;
3594            
3595 19333           int32_t is_allowed = 0;
3596 19333 50         if (strcmp(current_basic_type_name, dist_basic_type_name) == 0) {
3597 0           is_allowed = 1;
3598             }
3599             else {
3600 19333 100         for (int32_t i = 0; i < allows->length; i++) {
3601 19324           SPVM_ALLOW* allow = SPVM_LIST_get(allows, i);
3602 19324           const char* allow_basic_type_name = allow->basic_type_name;
3603 19324 50         if (strcmp(current_basic_type_name, allow_basic_type_name) == 0) {
3604 19324           is_allowed = 1;
3605 19324           break;
3606             }
3607             }
3608             }
3609            
3610 19333           return is_allowed;
3611             }
3612              
3613 3687922           int32_t SPVM_OP_is_mutable(SPVM_COMPILER* compiler, SPVM_OP* op) {
3614            
3615 3687922 100         switch (op->id) {
3616             case SPVM_OP_C_ID_VAR:
3617             case SPVM_OP_C_ID_CLASS_VAR_ACCESS:
3618             case SPVM_OP_C_ID_ARRAY_ACCESS:
3619             case SPVM_OP_C_ID_FIELD_ACCESS:
3620             case SPVM_OP_C_ID_DEREF:
3621             case SPVM_OP_C_ID_EXCEPTION_VAR:
3622             case SPVM_OP_C_ID_ARRAY_FIELD_ACCESS:
3623 3687916           return 1;
3624             }
3625            
3626 6           return 0;
3627             }
3628              
3629 513224           int32_t SPVM_OP_is_comparison_op(SPVM_COMPILER* compiler, SPVM_OP* op) {
3630            
3631 513224 50         switch (op->id) {
3632             case SPVM_OP_C_ID_NUMERIC_EQ:
3633             case SPVM_OP_C_ID_NUMERIC_NE:
3634             case SPVM_OP_C_ID_NUMERIC_GT:
3635             case SPVM_OP_C_ID_NUMERIC_GE:
3636             case SPVM_OP_C_ID_NUMERIC_LT:
3637             case SPVM_OP_C_ID_NUMERIC_LE:
3638             case SPVM_OP_C_ID_NUMERIC_CMP:
3639             case SPVM_OP_C_ID_STRING_EQ:
3640             case SPVM_OP_C_ID_STRING_NE:
3641             case SPVM_OP_C_ID_STRING_GT:
3642             case SPVM_OP_C_ID_STRING_GE:
3643             case SPVM_OP_C_ID_STRING_LT:
3644             case SPVM_OP_C_ID_STRING_LE:
3645             case SPVM_OP_C_ID_STRING_CMP:
3646             case SPVM_OP_C_ID_ISA:
3647             case SPVM_OP_C_ID_ISA_ERROR:
3648             case SPVM_OP_C_ID_IS_TYPE:
3649             case SPVM_OP_C_ID_IS_ERROR:
3650             {
3651 0           return 1;
3652             }
3653             }
3654            
3655 513224           return 0;
3656             }
3657              
3658 6574180           SPVM_OP* SPVM_OP_get_parent(SPVM_COMPILER* compiler, SPVM_OP* op_target) {
3659            
3660             SPVM_OP* op_parent;
3661 6574180           SPVM_OP* op_cur = op_target;
3662             while (1) {
3663 10563574 100         if (op_cur->moresib) {
3664 3989394           op_cur = op_cur->sibparent;
3665             }
3666             else {
3667 6574180           op_parent = op_cur->sibparent;
3668 6574180           break;
3669             }
3670 3989394           }
3671            
3672 6574180           return op_parent;
3673             }
3674              
3675 3287090           void SPVM_OP_get_before(SPVM_COMPILER* compiler, SPVM_OP* op_target, SPVM_OP** op_before_ptr, int32_t* next_is_child_ptr) {
3676              
3677             // Get parent
3678 3287090           SPVM_OP* op_parent = SPVM_OP_get_parent(compiler, op_target);
3679            
3680             SPVM_OP* op_before;
3681 3287090           int32_t next_is_child = 0;
3682 3287090 100         if (op_parent->first == op_target) {
3683 1581932           op_before = op_parent;
3684 1581932           next_is_child = 1;
3685             }
3686             else {
3687 1705158           op_before = op_parent->first;
3688             while (1) {
3689 3373460 100         if (op_before->sibparent == op_target) {
3690 1705158           break;
3691             }
3692             else {
3693 1668302           op_before = op_before->sibparent;
3694             }
3695 1668302           }
3696             }
3697            
3698 3287090           *op_before_ptr = op_before;
3699 3287090           *next_is_child_ptr = next_is_child;
3700 3287090           }
3701              
3702             // Replace target op with replace op
3703 1616278           void SPVM_OP_replace_op(SPVM_COMPILER* compiler, SPVM_OP* op_target, SPVM_OP* op_replace) {
3704            
3705             // Get parent op
3706 1616278           SPVM_OP* op_parent = SPVM_OP_get_parent(compiler, op_target);
3707            
3708 1616278           int32_t op_target_is_last_child = op_parent->last == op_target;
3709              
3710             // Get before op
3711             int32_t next_is_child;
3712             SPVM_OP* op_before;
3713 1616278           SPVM_OP_get_before(compiler, op_target, &op_before, &next_is_child);
3714            
3715             // Stab
3716 1616278 100         if (next_is_child) {
3717             // One child
3718 784335 100         if (op_before->first == op_before->last) {
3719 260903           op_before->first = op_replace;
3720 260903           op_before->last = op_replace;
3721             }
3722             // More
3723             else {
3724 784335           op_before->first = op_replace;
3725             }
3726             }
3727             else {
3728 831943           op_before->sibparent = op_replace;
3729             }
3730 1616278           op_replace->moresib = op_target->moresib;
3731 1616278           op_replace->sibparent = op_target->sibparent;
3732              
3733 1616278 100         if (op_target_is_last_child) {
3734 900315           op_parent->last = op_replace;
3735             }
3736 1616278           }
3737              
3738             // Cut op and insert stab into original position and return stab
3739 1670812           SPVM_OP* SPVM_OP_cut_op(SPVM_COMPILER* compiler, SPVM_OP* op_target) {
3740             // Get parent op
3741 1670812           SPVM_OP* op_parent = SPVM_OP_get_parent(compiler, op_target);
3742            
3743 1670812           int32_t op_target_is_last_child = op_parent->last == op_target;
3744              
3745             // Get before op
3746             int32_t next_is_child;
3747             SPVM_OP* op_before;
3748 1670812           SPVM_OP_get_before(compiler, op_target, &op_before, &next_is_child);
3749            
3750             // Stab
3751 1670812           SPVM_OP* op_stab = SPVM_OP_new_op(compiler, SPVM_OP_C_ID_DO_NOTHING, op_target->file, op_target->line);
3752 1670812 100         if (next_is_child) {
3753            
3754             // One child
3755 797597 100         if (op_before->first == op_before->last) {
3756 269074           op_before->first = op_stab;
3757 269074           op_before->last = op_stab;
3758             }
3759             // More
3760             else {
3761 797597           op_before->first = op_stab;
3762             }
3763             }
3764             else {
3765 873215           op_before->sibparent = op_stab;
3766             }
3767 1670812           op_stab->moresib = op_target->moresib;
3768 1670812           op_stab->sibparent = op_target->sibparent;
3769              
3770             // Clear target
3771 1670812           op_target->moresib = 0;
3772 1670812           op_target->sibparent = NULL;
3773            
3774 1670812 100         if (op_target_is_last_child) {
3775 941239           op_parent->last = op_stab;
3776             }
3777            
3778 1670812           return op_stab;
3779             }
3780              
3781             // Insert child. Child must not have sibling.
3782 25455845           void SPVM_OP_insert_child(SPVM_COMPILER* compiler, SPVM_OP* parent, SPVM_OP* start, SPVM_OP* insert) {
3783            
3784             // del_count not used
3785 25455845 50         assert(parent);
3786 25455845 50         assert(insert);
3787 25455845 50         assert(insert->moresib == 0);
3788            
3789 25455845 100         if (start) {
3790 11085388 100         if (start->moresib) {
3791 743354           insert->moresib = 1;
3792 743354           insert->sibparent = start->sibparent;
3793            
3794 743354           start->sibparent = insert;
3795             }
3796             else {
3797 10342034           parent->last = insert;
3798              
3799 10342034           insert->moresib = 0;
3800 10342034           insert->sibparent = parent;
3801            
3802 10342034           start->moresib = 1;
3803 11085388           start->sibparent = insert;
3804             }
3805             }
3806             else {
3807 14370457 50         if (parent->first) {
3808 0           insert->moresib = 1;
3809 0           insert->sibparent = parent->first;
3810            
3811 0           parent->first = insert;
3812             }
3813             else {
3814 14370457           insert->moresib = 0;
3815 14370457           insert->sibparent = parent;
3816            
3817 14370457           parent->first = insert;
3818 14370457           parent->last = insert;
3819             }
3820             }
3821 25455845           }
3822              
3823 58490653           SPVM_OP* SPVM_OP_sibling(SPVM_COMPILER* compiler, SPVM_OP* op) {
3824            
3825 58490653 100         return op->moresib ? op->sibparent : NULL;
3826             }
3827              
3828 7630           const char* SPVM_OP_get_op_name(SPVM_COMPILER* compiler, int32_t op_id) {
3829 7630           const char* op_name = (SPVM_OP_C_ID_NAMES())[op_id];
3830            
3831 7630           return op_name;
3832             }
3833              
3834