Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gdscript/gdscript_byte_codegen.cpp
10278 views
1
/**************************************************************************/
2
/* gdscript_byte_codegen.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "gdscript_byte_codegen.h"
32
33
#include "core/debugger/engine_debugger.h"
34
35
uint32_t GDScriptByteCodeGenerator::add_parameter(const StringName &p_name, bool p_is_optional, const GDScriptDataType &p_type) {
36
function->_argument_count++;
37
function->argument_types.push_back(p_type);
38
if (p_is_optional) {
39
function->_default_arg_count++;
40
}
41
42
return add_local(p_name, p_type);
43
}
44
45
uint32_t GDScriptByteCodeGenerator::add_local(const StringName &p_name, const GDScriptDataType &p_type) {
46
int stack_pos = locals.size() + GDScriptFunction::FIXED_ADDRESSES_MAX;
47
locals.push_back(StackSlot(p_type.builtin_type, p_type.can_contain_object()));
48
add_stack_identifier(p_name, stack_pos);
49
return stack_pos;
50
}
51
52
uint32_t GDScriptByteCodeGenerator::add_local_constant(const StringName &p_name, const Variant &p_constant) {
53
int index = add_or_get_constant(p_constant);
54
local_constants[p_name] = index;
55
return index;
56
}
57
58
uint32_t GDScriptByteCodeGenerator::add_or_get_constant(const Variant &p_constant) {
59
return get_constant_pos(p_constant);
60
}
61
62
uint32_t GDScriptByteCodeGenerator::add_or_get_name(const StringName &p_name) {
63
return get_name_map_pos(p_name);
64
}
65
66
uint32_t GDScriptByteCodeGenerator::add_temporary(const GDScriptDataType &p_type) {
67
Variant::Type temp_type = Variant::NIL;
68
if (p_type.has_type && p_type.kind == GDScriptDataType::BUILTIN) {
69
switch (p_type.builtin_type) {
70
case Variant::NIL:
71
case Variant::BOOL:
72
case Variant::INT:
73
case Variant::FLOAT:
74
case Variant::STRING:
75
case Variant::VECTOR2:
76
case Variant::VECTOR2I:
77
case Variant::RECT2:
78
case Variant::RECT2I:
79
case Variant::VECTOR3:
80
case Variant::VECTOR3I:
81
case Variant::TRANSFORM2D:
82
case Variant::VECTOR4:
83
case Variant::VECTOR4I:
84
case Variant::PLANE:
85
case Variant::QUATERNION:
86
case Variant::AABB:
87
case Variant::BASIS:
88
case Variant::TRANSFORM3D:
89
case Variant::PROJECTION:
90
case Variant::COLOR:
91
case Variant::STRING_NAME:
92
case Variant::NODE_PATH:
93
case Variant::RID:
94
case Variant::CALLABLE:
95
case Variant::SIGNAL:
96
temp_type = p_type.builtin_type;
97
break;
98
case Variant::OBJECT:
99
case Variant::DICTIONARY:
100
case Variant::ARRAY:
101
case Variant::PACKED_BYTE_ARRAY:
102
case Variant::PACKED_INT32_ARRAY:
103
case Variant::PACKED_INT64_ARRAY:
104
case Variant::PACKED_FLOAT32_ARRAY:
105
case Variant::PACKED_FLOAT64_ARRAY:
106
case Variant::PACKED_STRING_ARRAY:
107
case Variant::PACKED_VECTOR2_ARRAY:
108
case Variant::PACKED_VECTOR3_ARRAY:
109
case Variant::PACKED_COLOR_ARRAY:
110
case Variant::PACKED_VECTOR4_ARRAY:
111
case Variant::VARIANT_MAX:
112
// Arrays, dictionaries, and objects are reference counted, so we don't use the pool for them.
113
temp_type = Variant::NIL;
114
break;
115
}
116
}
117
118
if (!temporaries_pool.has(temp_type)) {
119
temporaries_pool[temp_type] = List<int>();
120
}
121
122
List<int> &pool = temporaries_pool[temp_type];
123
if (pool.is_empty()) {
124
StackSlot new_temp(temp_type, p_type.can_contain_object());
125
int idx = temporaries.size();
126
pool.push_back(idx);
127
temporaries.push_back(new_temp);
128
}
129
int slot = pool.front()->get();
130
pool.pop_front();
131
used_temporaries.push_back(slot);
132
return slot;
133
}
134
135
void GDScriptByteCodeGenerator::pop_temporary() {
136
ERR_FAIL_COND(used_temporaries.is_empty());
137
int slot_idx = used_temporaries.back()->get();
138
if (temporaries[slot_idx].can_contain_object) {
139
// Avoid keeping in the stack long-lived references to objects,
140
// which may prevent `RefCounted` objects from being freed.
141
// However, the cleanup will be performed an the end of the
142
// statement, to allow object references to survive chaining.
143
temporaries_pending_clear.insert(slot_idx);
144
}
145
temporaries_pool[temporaries[slot_idx].type].push_back(slot_idx);
146
used_temporaries.pop_back();
147
}
148
149
void GDScriptByteCodeGenerator::start_parameters() {
150
if (function->_default_arg_count > 0) {
151
append(GDScriptFunction::OPCODE_JUMP_TO_DEF_ARGUMENT);
152
function->default_arguments.push_back(opcodes.size());
153
}
154
}
155
156
void GDScriptByteCodeGenerator::end_parameters() {
157
function->default_arguments.reverse();
158
}
159
160
void GDScriptByteCodeGenerator::write_start(GDScript *p_script, const StringName &p_function_name, bool p_static, Variant p_rpc_config, const GDScriptDataType &p_return_type) {
161
function = memnew(GDScriptFunction);
162
163
function->name = p_function_name;
164
function->_script = p_script;
165
function->source = p_script->get_script_path();
166
167
#ifdef DEBUG_ENABLED
168
function->func_cname = (String(function->source) + " - " + String(p_function_name)).utf8();
169
function->_func_cname = function->func_cname.get_data();
170
#endif
171
172
function->_static = p_static;
173
function->return_type = p_return_type;
174
function->rpc_config = p_rpc_config;
175
function->_argument_count = 0;
176
}
177
178
GDScriptFunction *GDScriptByteCodeGenerator::write_end() {
179
#ifdef DEBUG_ENABLED
180
if (!used_temporaries.is_empty()) {
181
ERR_PRINT("Non-zero temporary variables at end of function: " + itos(used_temporaries.size()));
182
}
183
#endif
184
append_opcode(GDScriptFunction::OPCODE_END);
185
186
for (int i = 0; i < temporaries.size(); i++) {
187
int stack_index = i + max_locals + GDScriptFunction::FIXED_ADDRESSES_MAX;
188
for (int j = 0; j < temporaries[i].bytecode_indices.size(); j++) {
189
opcodes.write[temporaries[i].bytecode_indices[j]] = stack_index | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
190
}
191
if (temporaries[i].type != Variant::NIL) {
192
function->temporary_slots[stack_index] = temporaries[i].type;
193
}
194
}
195
196
if (constant_map.size()) {
197
function->_constant_count = constant_map.size();
198
function->constants.resize(constant_map.size());
199
function->_constants_ptr = function->constants.ptrw();
200
for (const KeyValue<Variant, int> &K : constant_map) {
201
function->constants.write[K.value] = K.key;
202
}
203
} else {
204
function->_constants_ptr = nullptr;
205
function->_constant_count = 0;
206
}
207
208
if (name_map.size()) {
209
function->global_names.resize(name_map.size());
210
function->_global_names_ptr = &function->global_names[0];
211
for (const KeyValue<StringName, int> &E : name_map) {
212
function->global_names.write[E.value] = E.key;
213
}
214
function->_global_names_count = function->global_names.size();
215
216
} else {
217
function->_global_names_ptr = nullptr;
218
function->_global_names_count = 0;
219
}
220
221
if (opcodes.size()) {
222
function->code = opcodes;
223
function->_code_ptr = &function->code.write[0];
224
function->_code_size = opcodes.size();
225
226
} else {
227
function->_code_ptr = nullptr;
228
function->_code_size = 0;
229
}
230
231
if (function->default_arguments.size()) {
232
function->_default_arg_count = function->default_arguments.size() - 1;
233
function->_default_arg_ptr = &function->default_arguments[0];
234
} else {
235
function->_default_arg_count = 0;
236
function->_default_arg_ptr = nullptr;
237
}
238
239
if (operator_func_map.size()) {
240
function->operator_funcs.resize(operator_func_map.size());
241
function->_operator_funcs_count = function->operator_funcs.size();
242
function->_operator_funcs_ptr = function->operator_funcs.ptr();
243
for (const KeyValue<Variant::ValidatedOperatorEvaluator, int> &E : operator_func_map) {
244
function->operator_funcs.write[E.value] = E.key;
245
}
246
} else {
247
function->_operator_funcs_count = 0;
248
function->_operator_funcs_ptr = nullptr;
249
}
250
251
if (setters_map.size()) {
252
function->setters.resize(setters_map.size());
253
function->_setters_count = function->setters.size();
254
function->_setters_ptr = function->setters.ptr();
255
for (const KeyValue<Variant::ValidatedSetter, int> &E : setters_map) {
256
function->setters.write[E.value] = E.key;
257
}
258
} else {
259
function->_setters_count = 0;
260
function->_setters_ptr = nullptr;
261
}
262
263
if (getters_map.size()) {
264
function->getters.resize(getters_map.size());
265
function->_getters_count = function->getters.size();
266
function->_getters_ptr = function->getters.ptr();
267
for (const KeyValue<Variant::ValidatedGetter, int> &E : getters_map) {
268
function->getters.write[E.value] = E.key;
269
}
270
} else {
271
function->_getters_count = 0;
272
function->_getters_ptr = nullptr;
273
}
274
275
if (keyed_setters_map.size()) {
276
function->keyed_setters.resize(keyed_setters_map.size());
277
function->_keyed_setters_count = function->keyed_setters.size();
278
function->_keyed_setters_ptr = function->keyed_setters.ptr();
279
for (const KeyValue<Variant::ValidatedKeyedSetter, int> &E : keyed_setters_map) {
280
function->keyed_setters.write[E.value] = E.key;
281
}
282
} else {
283
function->_keyed_setters_count = 0;
284
function->_keyed_setters_ptr = nullptr;
285
}
286
287
if (keyed_getters_map.size()) {
288
function->keyed_getters.resize(keyed_getters_map.size());
289
function->_keyed_getters_count = function->keyed_getters.size();
290
function->_keyed_getters_ptr = function->keyed_getters.ptr();
291
for (const KeyValue<Variant::ValidatedKeyedGetter, int> &E : keyed_getters_map) {
292
function->keyed_getters.write[E.value] = E.key;
293
}
294
} else {
295
function->_keyed_getters_count = 0;
296
function->_keyed_getters_ptr = nullptr;
297
}
298
299
if (indexed_setters_map.size()) {
300
function->indexed_setters.resize(indexed_setters_map.size());
301
function->_indexed_setters_count = function->indexed_setters.size();
302
function->_indexed_setters_ptr = function->indexed_setters.ptr();
303
for (const KeyValue<Variant::ValidatedIndexedSetter, int> &E : indexed_setters_map) {
304
function->indexed_setters.write[E.value] = E.key;
305
}
306
} else {
307
function->_indexed_setters_count = 0;
308
function->_indexed_setters_ptr = nullptr;
309
}
310
311
if (indexed_getters_map.size()) {
312
function->indexed_getters.resize(indexed_getters_map.size());
313
function->_indexed_getters_count = function->indexed_getters.size();
314
function->_indexed_getters_ptr = function->indexed_getters.ptr();
315
for (const KeyValue<Variant::ValidatedIndexedGetter, int> &E : indexed_getters_map) {
316
function->indexed_getters.write[E.value] = E.key;
317
}
318
} else {
319
function->_indexed_getters_count = 0;
320
function->_indexed_getters_ptr = nullptr;
321
}
322
323
if (builtin_method_map.size()) {
324
function->builtin_methods.resize(builtin_method_map.size());
325
function->_builtin_methods_ptr = function->builtin_methods.ptr();
326
function->_builtin_methods_count = builtin_method_map.size();
327
for (const KeyValue<Variant::ValidatedBuiltInMethod, int> &E : builtin_method_map) {
328
function->builtin_methods.write[E.value] = E.key;
329
}
330
} else {
331
function->_builtin_methods_ptr = nullptr;
332
function->_builtin_methods_count = 0;
333
}
334
335
if (constructors_map.size()) {
336
function->constructors.resize(constructors_map.size());
337
function->_constructors_ptr = function->constructors.ptr();
338
function->_constructors_count = constructors_map.size();
339
for (const KeyValue<Variant::ValidatedConstructor, int> &E : constructors_map) {
340
function->constructors.write[E.value] = E.key;
341
}
342
} else {
343
function->_constructors_ptr = nullptr;
344
function->_constructors_count = 0;
345
}
346
347
if (utilities_map.size()) {
348
function->utilities.resize(utilities_map.size());
349
function->_utilities_ptr = function->utilities.ptr();
350
function->_utilities_count = utilities_map.size();
351
for (const KeyValue<Variant::ValidatedUtilityFunction, int> &E : utilities_map) {
352
function->utilities.write[E.value] = E.key;
353
}
354
} else {
355
function->_utilities_ptr = nullptr;
356
function->_utilities_count = 0;
357
}
358
359
if (gds_utilities_map.size()) {
360
function->gds_utilities.resize(gds_utilities_map.size());
361
function->_gds_utilities_ptr = function->gds_utilities.ptr();
362
function->_gds_utilities_count = gds_utilities_map.size();
363
for (const KeyValue<GDScriptUtilityFunctions::FunctionPtr, int> &E : gds_utilities_map) {
364
function->gds_utilities.write[E.value] = E.key;
365
}
366
} else {
367
function->_gds_utilities_ptr = nullptr;
368
function->_gds_utilities_count = 0;
369
}
370
371
if (method_bind_map.size()) {
372
function->methods.resize(method_bind_map.size());
373
function->_methods_ptr = function->methods.ptrw();
374
function->_methods_count = method_bind_map.size();
375
for (const KeyValue<MethodBind *, int> &E : method_bind_map) {
376
function->methods.write[E.value] = E.key;
377
}
378
} else {
379
function->_methods_ptr = nullptr;
380
function->_methods_count = 0;
381
}
382
383
if (lambdas_map.size()) {
384
function->lambdas.resize(lambdas_map.size());
385
function->_lambdas_ptr = function->lambdas.ptrw();
386
function->_lambdas_count = lambdas_map.size();
387
for (const KeyValue<GDScriptFunction *, int> &E : lambdas_map) {
388
function->lambdas.write[E.value] = E.key;
389
}
390
} else {
391
function->_lambdas_ptr = nullptr;
392
function->_lambdas_count = 0;
393
}
394
395
if (GDScriptLanguage::get_singleton()->should_track_locals()) {
396
function->stack_debug = stack_debug;
397
}
398
function->_stack_size = GDScriptFunction::FIXED_ADDRESSES_MAX + max_locals + temporaries.size();
399
function->_instruction_args_size = instr_args_max;
400
401
#ifdef DEBUG_ENABLED
402
function->operator_names = operator_names;
403
function->setter_names = setter_names;
404
function->getter_names = getter_names;
405
function->builtin_methods_names = builtin_methods_names;
406
function->constructors_names = constructors_names;
407
function->utilities_names = utilities_names;
408
function->gds_utilities_names = gds_utilities_names;
409
#endif
410
411
ended = true;
412
return function;
413
}
414
415
#ifdef DEBUG_ENABLED
416
void GDScriptByteCodeGenerator::set_signature(const String &p_signature) {
417
function->profile.signature = p_signature;
418
}
419
#endif
420
421
void GDScriptByteCodeGenerator::set_initial_line(int p_line) {
422
function->_initial_line = p_line;
423
}
424
425
#define HAS_BUILTIN_TYPE(m_var) \
426
(m_var.type.has_type && m_var.type.kind == GDScriptDataType::BUILTIN)
427
428
#define IS_BUILTIN_TYPE(m_var, m_type) \
429
(m_var.type.has_type && m_var.type.kind == GDScriptDataType::BUILTIN && m_var.type.builtin_type == m_type && m_type != Variant::NIL)
430
431
void GDScriptByteCodeGenerator::write_type_adjust(const Address &p_target, Variant::Type p_new_type) {
432
switch (p_new_type) {
433
case Variant::BOOL:
434
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_BOOL);
435
break;
436
case Variant::INT:
437
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_INT);
438
break;
439
case Variant::FLOAT:
440
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_FLOAT);
441
break;
442
case Variant::STRING:
443
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_STRING);
444
break;
445
case Variant::VECTOR2:
446
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR2);
447
break;
448
case Variant::VECTOR2I:
449
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR2I);
450
break;
451
case Variant::RECT2:
452
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_RECT2);
453
break;
454
case Variant::RECT2I:
455
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_RECT2I);
456
break;
457
case Variant::VECTOR3:
458
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR3);
459
break;
460
case Variant::VECTOR3I:
461
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR3I);
462
break;
463
case Variant::TRANSFORM2D:
464
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_TRANSFORM2D);
465
break;
466
case Variant::VECTOR4:
467
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR3);
468
break;
469
case Variant::VECTOR4I:
470
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR3I);
471
break;
472
case Variant::PLANE:
473
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PLANE);
474
break;
475
case Variant::QUATERNION:
476
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_QUATERNION);
477
break;
478
case Variant::AABB:
479
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_AABB);
480
break;
481
case Variant::BASIS:
482
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_BASIS);
483
break;
484
case Variant::TRANSFORM3D:
485
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_TRANSFORM3D);
486
break;
487
case Variant::PROJECTION:
488
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PROJECTION);
489
break;
490
case Variant::COLOR:
491
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_COLOR);
492
break;
493
case Variant::STRING_NAME:
494
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_STRING_NAME);
495
break;
496
case Variant::NODE_PATH:
497
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_NODE_PATH);
498
break;
499
case Variant::RID:
500
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_RID);
501
break;
502
case Variant::OBJECT:
503
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_OBJECT);
504
break;
505
case Variant::CALLABLE:
506
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_CALLABLE);
507
break;
508
case Variant::SIGNAL:
509
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_SIGNAL);
510
break;
511
case Variant::DICTIONARY:
512
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_DICTIONARY);
513
break;
514
case Variant::ARRAY:
515
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_ARRAY);
516
break;
517
case Variant::PACKED_BYTE_ARRAY:
518
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_BYTE_ARRAY);
519
break;
520
case Variant::PACKED_INT32_ARRAY:
521
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_INT32_ARRAY);
522
break;
523
case Variant::PACKED_INT64_ARRAY:
524
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_INT64_ARRAY);
525
break;
526
case Variant::PACKED_FLOAT32_ARRAY:
527
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_FLOAT32_ARRAY);
528
break;
529
case Variant::PACKED_FLOAT64_ARRAY:
530
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_FLOAT64_ARRAY);
531
break;
532
case Variant::PACKED_STRING_ARRAY:
533
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_STRING_ARRAY);
534
break;
535
case Variant::PACKED_VECTOR2_ARRAY:
536
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_VECTOR2_ARRAY);
537
break;
538
case Variant::PACKED_VECTOR3_ARRAY:
539
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_VECTOR3_ARRAY);
540
break;
541
case Variant::PACKED_COLOR_ARRAY:
542
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_COLOR_ARRAY);
543
break;
544
case Variant::PACKED_VECTOR4_ARRAY:
545
append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_VECTOR4_ARRAY);
546
break;
547
case Variant::NIL:
548
case Variant::VARIANT_MAX:
549
return;
550
}
551
append(p_target);
552
}
553
554
void GDScriptByteCodeGenerator::write_unary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand) {
555
if (HAS_BUILTIN_TYPE(p_left_operand)) {
556
// Gather specific operator.
557
Variant::ValidatedOperatorEvaluator op_func = Variant::get_validated_operator_evaluator(p_operator, p_left_operand.type.builtin_type, Variant::NIL);
558
559
append_opcode(GDScriptFunction::OPCODE_OPERATOR_VALIDATED);
560
append(p_left_operand);
561
append(Address());
562
append(p_target);
563
append(op_func);
564
#ifdef DEBUG_ENABLED
565
add_debug_name(operator_names, get_operation_pos(op_func), Variant::get_operator_name(p_operator));
566
#endif
567
return;
568
}
569
570
// No specific types, perform variant evaluation.
571
append_opcode(GDScriptFunction::OPCODE_OPERATOR);
572
append(p_left_operand);
573
append(Address());
574
append(p_target);
575
append(p_operator);
576
append(0); // Signature storage.
577
append(0); // Return type storage.
578
constexpr int _pointer_size = sizeof(Variant::ValidatedOperatorEvaluator) / sizeof(*(opcodes.ptr()));
579
for (int i = 0; i < _pointer_size; i++) {
580
append(0); // Space for function pointer.
581
}
582
}
583
584
void GDScriptByteCodeGenerator::write_binary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand, const Address &p_right_operand) {
585
bool valid = HAS_BUILTIN_TYPE(p_left_operand) && HAS_BUILTIN_TYPE(p_right_operand);
586
587
// Avoid validated evaluator for modulo and division when operands are int or integer vector, since there's no check for division by zero.
588
if (valid && (p_operator == Variant::OP_DIVIDE || p_operator == Variant::OP_MODULE)) {
589
switch (p_left_operand.type.builtin_type) {
590
case Variant::INT:
591
// Cannot use modulo between int / float, we should raise an error later in GDScript
592
valid = p_right_operand.type.builtin_type != Variant::INT && p_operator == Variant::OP_DIVIDE;
593
break;
594
case Variant::VECTOR2I:
595
case Variant::VECTOR3I:
596
case Variant::VECTOR4I:
597
valid = p_right_operand.type.builtin_type != Variant::INT && p_right_operand.type.builtin_type != p_left_operand.type.builtin_type;
598
break;
599
default:
600
break;
601
}
602
}
603
604
if (valid) {
605
if (p_target.mode == Address::TEMPORARY) {
606
Variant::Type result_type = Variant::get_operator_return_type(p_operator, p_left_operand.type.builtin_type, p_right_operand.type.builtin_type);
607
Variant::Type temp_type = temporaries[p_target.address].type;
608
if (result_type != temp_type) {
609
write_type_adjust(p_target, result_type);
610
}
611
}
612
613
// Gather specific operator.
614
Variant::ValidatedOperatorEvaluator op_func = Variant::get_validated_operator_evaluator(p_operator, p_left_operand.type.builtin_type, p_right_operand.type.builtin_type);
615
616
append_opcode(GDScriptFunction::OPCODE_OPERATOR_VALIDATED);
617
append(p_left_operand);
618
append(p_right_operand);
619
append(p_target);
620
append(op_func);
621
#ifdef DEBUG_ENABLED
622
add_debug_name(operator_names, get_operation_pos(op_func), Variant::get_operator_name(p_operator));
623
#endif
624
return;
625
}
626
627
// No specific types, perform variant evaluation.
628
append_opcode(GDScriptFunction::OPCODE_OPERATOR);
629
append(p_left_operand);
630
append(p_right_operand);
631
append(p_target);
632
append(p_operator);
633
append(0); // Signature storage.
634
append(0); // Return type storage.
635
constexpr int _pointer_size = sizeof(Variant::ValidatedOperatorEvaluator) / sizeof(*(opcodes.ptr()));
636
for (int i = 0; i < _pointer_size; i++) {
637
append(0); // Space for function pointer.
638
}
639
}
640
641
void GDScriptByteCodeGenerator::write_type_test(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) {
642
switch (p_type.kind) {
643
case GDScriptDataType::BUILTIN: {
644
if (p_type.builtin_type == Variant::ARRAY && p_type.has_container_element_type(0)) {
645
const GDScriptDataType &element_type = p_type.get_container_element_type(0);
646
append_opcode(GDScriptFunction::OPCODE_TYPE_TEST_ARRAY);
647
append(p_target);
648
append(p_source);
649
append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
650
append(element_type.builtin_type);
651
append(element_type.native_type);
652
} else if (p_type.builtin_type == Variant::DICTIONARY && p_type.has_container_element_types()) {
653
const GDScriptDataType &key_element_type = p_type.get_container_element_type_or_variant(0);
654
const GDScriptDataType &value_element_type = p_type.get_container_element_type_or_variant(1);
655
append_opcode(GDScriptFunction::OPCODE_TYPE_TEST_DICTIONARY);
656
append(p_target);
657
append(p_source);
658
append(get_constant_pos(key_element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
659
append(get_constant_pos(value_element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
660
append(key_element_type.builtin_type);
661
append(key_element_type.native_type);
662
append(value_element_type.builtin_type);
663
append(value_element_type.native_type);
664
} else {
665
append_opcode(GDScriptFunction::OPCODE_TYPE_TEST_BUILTIN);
666
append(p_target);
667
append(p_source);
668
append(p_type.builtin_type);
669
}
670
} break;
671
case GDScriptDataType::NATIVE: {
672
append_opcode(GDScriptFunction::OPCODE_TYPE_TEST_NATIVE);
673
append(p_target);
674
append(p_source);
675
append(p_type.native_type);
676
} break;
677
case GDScriptDataType::SCRIPT:
678
case GDScriptDataType::GDSCRIPT: {
679
const Variant &script = p_type.script_type;
680
append_opcode(GDScriptFunction::OPCODE_TYPE_TEST_SCRIPT);
681
append(p_target);
682
append(p_source);
683
append(get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
684
} break;
685
default: {
686
ERR_PRINT("Compiler bug: unresolved type in type test.");
687
append_opcode(GDScriptFunction::OPCODE_ASSIGN_FALSE);
688
append(p_target);
689
}
690
}
691
}
692
693
void GDScriptByteCodeGenerator::write_and_left_operand(const Address &p_left_operand) {
694
append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
695
append(p_left_operand);
696
logic_op_jump_pos1.push_back(opcodes.size());
697
append(0); // Jump target, will be patched.
698
}
699
700
void GDScriptByteCodeGenerator::write_and_right_operand(const Address &p_right_operand) {
701
append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
702
append(p_right_operand);
703
logic_op_jump_pos2.push_back(opcodes.size());
704
append(0); // Jump target, will be patched.
705
}
706
707
void GDScriptByteCodeGenerator::write_end_and(const Address &p_target) {
708
// If here means both operands are true.
709
append_opcode(GDScriptFunction::OPCODE_ASSIGN_TRUE);
710
append(p_target);
711
// Jump away from the fail condition.
712
append_opcode(GDScriptFunction::OPCODE_JUMP);
713
append(opcodes.size() + 3);
714
// Here it means one of operands is false.
715
patch_jump(logic_op_jump_pos1.back()->get());
716
patch_jump(logic_op_jump_pos2.back()->get());
717
logic_op_jump_pos1.pop_back();
718
logic_op_jump_pos2.pop_back();
719
append_opcode(GDScriptFunction::OPCODE_ASSIGN_FALSE);
720
append(p_target);
721
}
722
723
void GDScriptByteCodeGenerator::write_or_left_operand(const Address &p_left_operand) {
724
append_opcode(GDScriptFunction::OPCODE_JUMP_IF);
725
append(p_left_operand);
726
logic_op_jump_pos1.push_back(opcodes.size());
727
append(0); // Jump target, will be patched.
728
}
729
730
void GDScriptByteCodeGenerator::write_or_right_operand(const Address &p_right_operand) {
731
append_opcode(GDScriptFunction::OPCODE_JUMP_IF);
732
append(p_right_operand);
733
logic_op_jump_pos2.push_back(opcodes.size());
734
append(0); // Jump target, will be patched.
735
}
736
737
void GDScriptByteCodeGenerator::write_end_or(const Address &p_target) {
738
// If here means both operands are false.
739
append_opcode(GDScriptFunction::OPCODE_ASSIGN_FALSE);
740
append(p_target);
741
// Jump away from the success condition.
742
append_opcode(GDScriptFunction::OPCODE_JUMP);
743
append(opcodes.size() + 3);
744
// Here it means one of operands is true.
745
patch_jump(logic_op_jump_pos1.back()->get());
746
patch_jump(logic_op_jump_pos2.back()->get());
747
logic_op_jump_pos1.pop_back();
748
logic_op_jump_pos2.pop_back();
749
append_opcode(GDScriptFunction::OPCODE_ASSIGN_TRUE);
750
append(p_target);
751
}
752
753
void GDScriptByteCodeGenerator::write_start_ternary(const Address &p_target) {
754
ternary_result.push_back(p_target);
755
}
756
757
void GDScriptByteCodeGenerator::write_ternary_condition(const Address &p_condition) {
758
append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
759
append(p_condition);
760
ternary_jump_fail_pos.push_back(opcodes.size());
761
append(0); // Jump target, will be patched.
762
}
763
764
void GDScriptByteCodeGenerator::write_ternary_true_expr(const Address &p_expr) {
765
append_opcode(GDScriptFunction::OPCODE_ASSIGN);
766
append(ternary_result.back()->get());
767
append(p_expr);
768
// Jump away from the false path.
769
append_opcode(GDScriptFunction::OPCODE_JUMP);
770
ternary_jump_skip_pos.push_back(opcodes.size());
771
append(0);
772
// Fail must jump here.
773
patch_jump(ternary_jump_fail_pos.back()->get());
774
ternary_jump_fail_pos.pop_back();
775
}
776
777
void GDScriptByteCodeGenerator::write_ternary_false_expr(const Address &p_expr) {
778
append_opcode(GDScriptFunction::OPCODE_ASSIGN);
779
append(ternary_result.back()->get());
780
append(p_expr);
781
}
782
783
void GDScriptByteCodeGenerator::write_end_ternary() {
784
patch_jump(ternary_jump_skip_pos.back()->get());
785
ternary_jump_skip_pos.pop_back();
786
ternary_result.pop_back();
787
}
788
789
void GDScriptByteCodeGenerator::write_set(const Address &p_target, const Address &p_index, const Address &p_source) {
790
if (HAS_BUILTIN_TYPE(p_target)) {
791
if (IS_BUILTIN_TYPE(p_index, Variant::INT) && Variant::get_member_validated_indexed_setter(p_target.type.builtin_type) &&
792
IS_BUILTIN_TYPE(p_source, Variant::get_indexed_element_type(p_target.type.builtin_type))) {
793
// Use indexed setter instead.
794
Variant::ValidatedIndexedSetter setter = Variant::get_member_validated_indexed_setter(p_target.type.builtin_type);
795
append_opcode(GDScriptFunction::OPCODE_SET_INDEXED_VALIDATED);
796
append(p_target);
797
append(p_index);
798
append(p_source);
799
append(setter);
800
return;
801
} else if (Variant::get_member_validated_keyed_setter(p_target.type.builtin_type)) {
802
Variant::ValidatedKeyedSetter setter = Variant::get_member_validated_keyed_setter(p_target.type.builtin_type);
803
append_opcode(GDScriptFunction::OPCODE_SET_KEYED_VALIDATED);
804
append(p_target);
805
append(p_index);
806
append(p_source);
807
append(setter);
808
return;
809
}
810
}
811
812
append_opcode(GDScriptFunction::OPCODE_SET_KEYED);
813
append(p_target);
814
append(p_index);
815
append(p_source);
816
}
817
818
void GDScriptByteCodeGenerator::write_get(const Address &p_target, const Address &p_index, const Address &p_source) {
819
if (HAS_BUILTIN_TYPE(p_source)) {
820
if (IS_BUILTIN_TYPE(p_index, Variant::INT) && Variant::get_member_validated_indexed_getter(p_source.type.builtin_type)) {
821
// Use indexed getter instead.
822
Variant::ValidatedIndexedGetter getter = Variant::get_member_validated_indexed_getter(p_source.type.builtin_type);
823
append_opcode(GDScriptFunction::OPCODE_GET_INDEXED_VALIDATED);
824
append(p_source);
825
append(p_index);
826
append(p_target);
827
append(getter);
828
return;
829
} else if (Variant::get_member_validated_keyed_getter(p_source.type.builtin_type)) {
830
Variant::ValidatedKeyedGetter getter = Variant::get_member_validated_keyed_getter(p_source.type.builtin_type);
831
append_opcode(GDScriptFunction::OPCODE_GET_KEYED_VALIDATED);
832
append(p_source);
833
append(p_index);
834
append(p_target);
835
append(getter);
836
return;
837
}
838
}
839
append_opcode(GDScriptFunction::OPCODE_GET_KEYED);
840
append(p_source);
841
append(p_index);
842
append(p_target);
843
}
844
845
void GDScriptByteCodeGenerator::write_set_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
846
if (HAS_BUILTIN_TYPE(p_target) && Variant::get_member_validated_setter(p_target.type.builtin_type, p_name) &&
847
IS_BUILTIN_TYPE(p_source, Variant::get_member_type(p_target.type.builtin_type, p_name))) {
848
Variant::ValidatedSetter setter = Variant::get_member_validated_setter(p_target.type.builtin_type, p_name);
849
append_opcode(GDScriptFunction::OPCODE_SET_NAMED_VALIDATED);
850
append(p_target);
851
append(p_source);
852
append(setter);
853
#ifdef DEBUG_ENABLED
854
add_debug_name(setter_names, get_setter_pos(setter), p_name);
855
#endif
856
return;
857
}
858
append_opcode(GDScriptFunction::OPCODE_SET_NAMED);
859
append(p_target);
860
append(p_source);
861
append(p_name);
862
}
863
864
void GDScriptByteCodeGenerator::write_get_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
865
if (HAS_BUILTIN_TYPE(p_source) && Variant::get_member_validated_getter(p_source.type.builtin_type, p_name)) {
866
Variant::ValidatedGetter getter = Variant::get_member_validated_getter(p_source.type.builtin_type, p_name);
867
append_opcode(GDScriptFunction::OPCODE_GET_NAMED_VALIDATED);
868
append(p_source);
869
append(p_target);
870
append(getter);
871
#ifdef DEBUG_ENABLED
872
add_debug_name(getter_names, get_getter_pos(getter), p_name);
873
#endif
874
return;
875
}
876
append_opcode(GDScriptFunction::OPCODE_GET_NAMED);
877
append(p_source);
878
append(p_target);
879
append(p_name);
880
}
881
882
void GDScriptByteCodeGenerator::write_set_member(const Address &p_value, const StringName &p_name) {
883
append_opcode(GDScriptFunction::OPCODE_SET_MEMBER);
884
append(p_value);
885
append(p_name);
886
}
887
888
void GDScriptByteCodeGenerator::write_get_member(const Address &p_target, const StringName &p_name) {
889
append_opcode(GDScriptFunction::OPCODE_GET_MEMBER);
890
append(p_target);
891
append(p_name);
892
}
893
894
void GDScriptByteCodeGenerator::write_set_static_variable(const Address &p_value, const Address &p_class, int p_index) {
895
append_opcode(GDScriptFunction::OPCODE_SET_STATIC_VARIABLE);
896
append(p_value);
897
append(p_class);
898
append(p_index);
899
}
900
901
void GDScriptByteCodeGenerator::write_get_static_variable(const Address &p_target, const Address &p_class, int p_index) {
902
append_opcode(GDScriptFunction::OPCODE_GET_STATIC_VARIABLE);
903
append(p_target);
904
append(p_class);
905
append(p_index);
906
}
907
908
void GDScriptByteCodeGenerator::write_assign_with_conversion(const Address &p_target, const Address &p_source) {
909
switch (p_target.type.kind) {
910
case GDScriptDataType::BUILTIN: {
911
if (p_target.type.builtin_type == Variant::ARRAY && p_target.type.has_container_element_type(0)) {
912
const GDScriptDataType &element_type = p_target.type.get_container_element_type(0);
913
append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_ARRAY);
914
append(p_target);
915
append(p_source);
916
append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
917
append(element_type.builtin_type);
918
append(element_type.native_type);
919
} else if (p_target.type.builtin_type == Variant::DICTIONARY && p_target.type.has_container_element_types()) {
920
const GDScriptDataType &key_type = p_target.type.get_container_element_type_or_variant(0);
921
const GDScriptDataType &value_type = p_target.type.get_container_element_type_or_variant(1);
922
append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_DICTIONARY);
923
append(p_target);
924
append(p_source);
925
append(get_constant_pos(key_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
926
append(get_constant_pos(value_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
927
append(key_type.builtin_type);
928
append(key_type.native_type);
929
append(value_type.builtin_type);
930
append(value_type.native_type);
931
} else {
932
append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN);
933
append(p_target);
934
append(p_source);
935
append(p_target.type.builtin_type);
936
}
937
} break;
938
case GDScriptDataType::NATIVE: {
939
int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_target.type.native_type];
940
Variant nc = GDScriptLanguage::get_singleton()->get_global_array()[class_idx];
941
class_idx = get_constant_pos(nc) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
942
append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_NATIVE);
943
append(p_target);
944
append(p_source);
945
append(class_idx);
946
} break;
947
case GDScriptDataType::SCRIPT:
948
case GDScriptDataType::GDSCRIPT: {
949
Variant script = p_target.type.script_type;
950
int idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
951
952
append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_SCRIPT);
953
append(p_target);
954
append(p_source);
955
append(idx);
956
} break;
957
default: {
958
ERR_PRINT("Compiler bug: unresolved assign.");
959
960
// Shouldn't get here, but fail-safe to a regular assignment
961
append_opcode(GDScriptFunction::OPCODE_ASSIGN);
962
append(p_target);
963
append(p_source);
964
}
965
}
966
}
967
968
void GDScriptByteCodeGenerator::write_assign(const Address &p_target, const Address &p_source) {
969
if (p_target.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type == Variant::ARRAY && p_target.type.has_container_element_type(0)) {
970
const GDScriptDataType &element_type = p_target.type.get_container_element_type(0);
971
append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_ARRAY);
972
append(p_target);
973
append(p_source);
974
append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
975
append(element_type.builtin_type);
976
append(element_type.native_type);
977
} else if (p_target.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type == Variant::DICTIONARY && p_target.type.has_container_element_types()) {
978
const GDScriptDataType &key_type = p_target.type.get_container_element_type_or_variant(0);
979
const GDScriptDataType &value_type = p_target.type.get_container_element_type_or_variant(1);
980
append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_DICTIONARY);
981
append(p_target);
982
append(p_source);
983
append(get_constant_pos(key_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
984
append(get_constant_pos(value_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
985
append(key_type.builtin_type);
986
append(key_type.native_type);
987
append(value_type.builtin_type);
988
append(value_type.native_type);
989
} else if (p_target.type.kind == GDScriptDataType::BUILTIN && p_source.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type != p_source.type.builtin_type) {
990
// Need conversion.
991
append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN);
992
append(p_target);
993
append(p_source);
994
append(p_target.type.builtin_type);
995
} else {
996
append_opcode(GDScriptFunction::OPCODE_ASSIGN);
997
append(p_target);
998
append(p_source);
999
}
1000
}
1001
1002
void GDScriptByteCodeGenerator::write_assign_null(const Address &p_target) {
1003
append_opcode(GDScriptFunction::OPCODE_ASSIGN_NULL);
1004
append(p_target);
1005
}
1006
1007
void GDScriptByteCodeGenerator::write_assign_true(const Address &p_target) {
1008
append_opcode(GDScriptFunction::OPCODE_ASSIGN_TRUE);
1009
append(p_target);
1010
}
1011
1012
void GDScriptByteCodeGenerator::write_assign_false(const Address &p_target) {
1013
append_opcode(GDScriptFunction::OPCODE_ASSIGN_FALSE);
1014
append(p_target);
1015
}
1016
1017
void GDScriptByteCodeGenerator::write_assign_default_parameter(const Address &p_dst, const Address &p_src, bool p_use_conversion) {
1018
if (p_use_conversion) {
1019
write_assign_with_conversion(p_dst, p_src);
1020
} else {
1021
write_assign(p_dst, p_src);
1022
}
1023
function->default_arguments.push_back(opcodes.size());
1024
}
1025
1026
void GDScriptByteCodeGenerator::write_store_global(const Address &p_dst, int p_global_index) {
1027
append_opcode(GDScriptFunction::OPCODE_STORE_GLOBAL);
1028
append(p_dst);
1029
append(p_global_index);
1030
}
1031
1032
void GDScriptByteCodeGenerator::write_store_named_global(const Address &p_dst, const StringName &p_global) {
1033
append_opcode(GDScriptFunction::OPCODE_STORE_NAMED_GLOBAL);
1034
append(p_dst);
1035
append(p_global);
1036
}
1037
1038
void GDScriptByteCodeGenerator::write_cast(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) {
1039
int index = 0;
1040
1041
switch (p_type.kind) {
1042
case GDScriptDataType::BUILTIN: {
1043
append_opcode(GDScriptFunction::OPCODE_CAST_TO_BUILTIN);
1044
index = p_type.builtin_type;
1045
} break;
1046
case GDScriptDataType::NATIVE: {
1047
int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_type.native_type];
1048
Variant nc = GDScriptLanguage::get_singleton()->get_global_array()[class_idx];
1049
append_opcode(GDScriptFunction::OPCODE_CAST_TO_NATIVE);
1050
index = get_constant_pos(nc) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
1051
} break;
1052
case GDScriptDataType::SCRIPT:
1053
case GDScriptDataType::GDSCRIPT: {
1054
Variant script = p_type.script_type;
1055
int idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
1056
append_opcode(GDScriptFunction::OPCODE_CAST_TO_SCRIPT);
1057
index = idx;
1058
} break;
1059
default: {
1060
return;
1061
}
1062
}
1063
1064
append(p_source);
1065
append(p_target);
1066
append(index);
1067
}
1068
1069
GDScriptByteCodeGenerator::CallTarget GDScriptByteCodeGenerator::get_call_target(const GDScriptCodeGenerator::Address &p_target, Variant::Type p_type) {
1070
if (p_target.mode == Address::NIL) {
1071
GDScriptDataType type;
1072
if (p_type != Variant::NIL) {
1073
type.has_type = true;
1074
type.kind = GDScriptDataType::BUILTIN;
1075
type.builtin_type = p_type;
1076
}
1077
uint32_t addr = add_temporary(type);
1078
return CallTarget(Address(Address::TEMPORARY, addr, type), true, this);
1079
} else {
1080
return CallTarget(p_target, false, this);
1081
}
1082
}
1083
1084
void GDScriptByteCodeGenerator::write_call(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
1085
append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
1086
for (int i = 0; i < p_arguments.size(); i++) {
1087
append(p_arguments[i]);
1088
}
1089
append(p_base);
1090
CallTarget ct = get_call_target(p_target);
1091
append(ct.target);
1092
append(p_arguments.size());
1093
append(p_function_name);
1094
ct.cleanup();
1095
}
1096
1097
void GDScriptByteCodeGenerator::write_super_call(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
1098
append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_SELF_BASE, 1 + p_arguments.size());
1099
for (int i = 0; i < p_arguments.size(); i++) {
1100
append(p_arguments[i]);
1101
}
1102
CallTarget ct = get_call_target(p_target);
1103
append(ct.target);
1104
append(p_arguments.size());
1105
append(p_function_name);
1106
ct.cleanup();
1107
}
1108
1109
void GDScriptByteCodeGenerator::write_call_async(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
1110
append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_ASYNC, 2 + p_arguments.size());
1111
for (int i = 0; i < p_arguments.size(); i++) {
1112
append(p_arguments[i]);
1113
}
1114
append(p_base);
1115
CallTarget ct = get_call_target(p_target);
1116
append(ct.target);
1117
append(p_arguments.size());
1118
append(p_function_name);
1119
ct.cleanup();
1120
}
1121
1122
void GDScriptByteCodeGenerator::write_call_gdscript_utility(const Address &p_target, const StringName &p_function, const Vector<Address> &p_arguments) {
1123
append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_GDSCRIPT_UTILITY, 1 + p_arguments.size());
1124
GDScriptUtilityFunctions::FunctionPtr gds_function = GDScriptUtilityFunctions::get_function(p_function);
1125
for (int i = 0; i < p_arguments.size(); i++) {
1126
append(p_arguments[i]);
1127
}
1128
CallTarget ct = get_call_target(p_target);
1129
append(ct.target);
1130
append(p_arguments.size());
1131
append(gds_function);
1132
ct.cleanup();
1133
#ifdef DEBUG_ENABLED
1134
add_debug_name(gds_utilities_names, get_gds_utility_pos(gds_function), p_function);
1135
#endif
1136
}
1137
1138
void GDScriptByteCodeGenerator::write_call_utility(const Address &p_target, const StringName &p_function, const Vector<Address> &p_arguments) {
1139
bool is_validated = true;
1140
if (Variant::is_utility_function_vararg(p_function)) {
1141
is_validated = false; // Vararg needs runtime checks, can't use validated call.
1142
} else if (p_arguments.size() == Variant::get_utility_function_argument_count(p_function)) {
1143
bool all_types_exact = true;
1144
for (int i = 0; i < p_arguments.size(); i++) {
1145
if (!IS_BUILTIN_TYPE(p_arguments[i], Variant::get_utility_function_argument_type(p_function, i))) {
1146
all_types_exact = false;
1147
break;
1148
}
1149
}
1150
1151
is_validated = all_types_exact;
1152
}
1153
1154
if (is_validated) {
1155
Variant::Type result_type = Variant::has_utility_function_return_value(p_function) ? Variant::get_utility_function_return_type(p_function) : Variant::NIL;
1156
CallTarget ct = get_call_target(p_target, result_type);
1157
Variant::Type temp_type = temporaries[ct.target.address].type;
1158
if (result_type != temp_type) {
1159
write_type_adjust(ct.target, result_type);
1160
}
1161
append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_UTILITY_VALIDATED, 1 + p_arguments.size());
1162
for (int i = 0; i < p_arguments.size(); i++) {
1163
append(p_arguments[i]);
1164
}
1165
append(ct.target);
1166
append(p_arguments.size());
1167
append(Variant::get_validated_utility_function(p_function));
1168
ct.cleanup();
1169
#ifdef DEBUG_ENABLED
1170
add_debug_name(utilities_names, get_utility_pos(Variant::get_validated_utility_function(p_function)), p_function);
1171
#endif
1172
} else {
1173
append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_UTILITY, 1 + p_arguments.size());
1174
for (int i = 0; i < p_arguments.size(); i++) {
1175
append(p_arguments[i]);
1176
}
1177
CallTarget ct = get_call_target(p_target);
1178
append(ct.target);
1179
append(p_arguments.size());
1180
append(p_function);
1181
ct.cleanup();
1182
}
1183
}
1184
1185
void GDScriptByteCodeGenerator::write_call_builtin_type(const Address &p_target, const Address &p_base, Variant::Type p_type, const StringName &p_method, bool p_is_static, const Vector<Address> &p_arguments) {
1186
bool is_validated = false;
1187
1188
// Check if all types are correct.
1189
if (Variant::is_builtin_method_vararg(p_type, p_method)) {
1190
is_validated = false; // Vararg needs runtime checks, can't use validated call.
1191
} else if (p_arguments.size() == Variant::get_builtin_method_argument_count(p_type, p_method)) {
1192
bool all_types_exact = true;
1193
for (int i = 0; i < p_arguments.size(); i++) {
1194
if (!IS_BUILTIN_TYPE(p_arguments[i], Variant::get_builtin_method_argument_type(p_type, p_method, i))) {
1195
all_types_exact = false;
1196
break;
1197
}
1198
}
1199
1200
is_validated = all_types_exact;
1201
}
1202
1203
if (!is_validated) {
1204
// Perform regular call.
1205
if (p_is_static) {
1206
append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_BUILTIN_STATIC, p_arguments.size() + 1);
1207
for (int i = 0; i < p_arguments.size(); i++) {
1208
append(p_arguments[i]);
1209
}
1210
CallTarget ct = get_call_target(p_target);
1211
append(ct.target);
1212
append(p_type);
1213
append(p_method);
1214
append(p_arguments.size());
1215
ct.cleanup();
1216
} else {
1217
write_call(p_target, p_base, p_method, p_arguments);
1218
}
1219
return;
1220
}
1221
1222
Variant::Type result_type = Variant::get_builtin_method_return_type(p_type, p_method);
1223
CallTarget ct = get_call_target(p_target, result_type);
1224
Variant::Type temp_type = temporaries[ct.target.address].type;
1225
if (result_type != temp_type) {
1226
write_type_adjust(ct.target, result_type);
1227
}
1228
1229
append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_BUILTIN_TYPE_VALIDATED, 2 + p_arguments.size());
1230
1231
for (int i = 0; i < p_arguments.size(); i++) {
1232
append(p_arguments[i]);
1233
}
1234
append(p_base);
1235
append(ct.target);
1236
append(p_arguments.size());
1237
append(Variant::get_validated_builtin_method(p_type, p_method));
1238
ct.cleanup();
1239
1240
#ifdef DEBUG_ENABLED
1241
add_debug_name(builtin_methods_names, get_builtin_method_pos(Variant::get_validated_builtin_method(p_type, p_method)), p_method);
1242
#endif
1243
}
1244
1245
void GDScriptByteCodeGenerator::write_call_builtin_type(const Address &p_target, const Address &p_base, Variant::Type p_type, const StringName &p_method, const Vector<Address> &p_arguments) {
1246
write_call_builtin_type(p_target, p_base, p_type, p_method, false, p_arguments);
1247
}
1248
1249
void GDScriptByteCodeGenerator::write_call_builtin_type_static(const Address &p_target, Variant::Type p_type, const StringName &p_method, const Vector<Address> &p_arguments) {
1250
write_call_builtin_type(p_target, Address(), p_type, p_method, true, p_arguments);
1251
}
1252
1253
void GDScriptByteCodeGenerator::write_call_native_static(const Address &p_target, const StringName &p_class, const StringName &p_method, const Vector<Address> &p_arguments) {
1254
MethodBind *method = ClassDB::get_method(p_class, p_method);
1255
1256
// Perform regular call.
1257
append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_NATIVE_STATIC, p_arguments.size() + 1);
1258
for (int i = 0; i < p_arguments.size(); i++) {
1259
append(p_arguments[i]);
1260
}
1261
CallTarget ct = get_call_target(p_target);
1262
append(ct.target);
1263
append(method);
1264
append(p_arguments.size());
1265
ct.cleanup();
1266
return;
1267
}
1268
1269
void GDScriptByteCodeGenerator::write_call_native_static_validated(const GDScriptCodeGenerator::Address &p_target, MethodBind *p_method, const Vector<GDScriptCodeGenerator::Address> &p_arguments) {
1270
Variant::Type return_type = Variant::NIL;
1271
bool has_return = p_method->has_return();
1272
1273
if (has_return) {
1274
PropertyInfo return_info = p_method->get_return_info();
1275
return_type = return_info.type;
1276
}
1277
1278
CallTarget ct = get_call_target(p_target, return_type);
1279
1280
if (has_return) {
1281
Variant::Type temp_type = temporaries[ct.target.address].type;
1282
if (temp_type != return_type) {
1283
write_type_adjust(ct.target, return_type);
1284
}
1285
}
1286
1287
GDScriptFunction::Opcode code = p_method->has_return() ? GDScriptFunction::OPCODE_CALL_NATIVE_STATIC_VALIDATED_RETURN : GDScriptFunction::OPCODE_CALL_NATIVE_STATIC_VALIDATED_NO_RETURN;
1288
append_opcode_and_argcount(code, 1 + p_arguments.size());
1289
1290
for (int i = 0; i < p_arguments.size(); i++) {
1291
append(p_arguments[i]);
1292
}
1293
append(ct.target);
1294
append(p_arguments.size());
1295
append(p_method);
1296
ct.cleanup();
1297
}
1298
1299
void GDScriptByteCodeGenerator::write_call_method_bind(const Address &p_target, const Address &p_base, MethodBind *p_method, const Vector<Address> &p_arguments) {
1300
append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL_METHOD_BIND : GDScriptFunction::OPCODE_CALL_METHOD_BIND_RET, 2 + p_arguments.size());
1301
for (int i = 0; i < p_arguments.size(); i++) {
1302
append(p_arguments[i]);
1303
}
1304
CallTarget ct = get_call_target(p_target);
1305
append(p_base);
1306
append(ct.target);
1307
append(p_arguments.size());
1308
append(p_method);
1309
ct.cleanup();
1310
}
1311
1312
void GDScriptByteCodeGenerator::write_call_method_bind_validated(const Address &p_target, const Address &p_base, MethodBind *p_method, const Vector<Address> &p_arguments) {
1313
Variant::Type return_type = Variant::NIL;
1314
bool has_return = p_method->has_return();
1315
1316
if (has_return) {
1317
PropertyInfo return_info = p_method->get_return_info();
1318
return_type = return_info.type;
1319
}
1320
1321
CallTarget ct = get_call_target(p_target, return_type);
1322
1323
if (has_return) {
1324
Variant::Type temp_type = temporaries[ct.target.address].type;
1325
if (temp_type != return_type) {
1326
write_type_adjust(ct.target, return_type);
1327
}
1328
}
1329
1330
GDScriptFunction::Opcode code = p_method->has_return() ? GDScriptFunction::OPCODE_CALL_METHOD_BIND_VALIDATED_RETURN : GDScriptFunction::OPCODE_CALL_METHOD_BIND_VALIDATED_NO_RETURN;
1331
append_opcode_and_argcount(code, 2 + p_arguments.size());
1332
1333
for (int i = 0; i < p_arguments.size(); i++) {
1334
append(p_arguments[i]);
1335
}
1336
append(p_base);
1337
append(ct.target);
1338
append(p_arguments.size());
1339
append(p_method);
1340
ct.cleanup();
1341
}
1342
1343
void GDScriptByteCodeGenerator::write_call_self(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
1344
append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
1345
for (int i = 0; i < p_arguments.size(); i++) {
1346
append(p_arguments[i]);
1347
}
1348
append(GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
1349
CallTarget ct = get_call_target(p_target);
1350
append(ct.target);
1351
append(p_arguments.size());
1352
append(p_function_name);
1353
ct.cleanup();
1354
}
1355
1356
void GDScriptByteCodeGenerator::write_call_self_async(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
1357
append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_ASYNC, 2 + p_arguments.size());
1358
for (int i = 0; i < p_arguments.size(); i++) {
1359
append(p_arguments[i]);
1360
}
1361
append(GDScriptFunction::ADDR_SELF);
1362
CallTarget ct = get_call_target(p_target);
1363
append(ct.target);
1364
append(p_arguments.size());
1365
append(p_function_name);
1366
ct.cleanup();
1367
}
1368
1369
void GDScriptByteCodeGenerator::write_call_script_function(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
1370
append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
1371
for (int i = 0; i < p_arguments.size(); i++) {
1372
append(p_arguments[i]);
1373
}
1374
append(p_base);
1375
CallTarget ct = get_call_target(p_target);
1376
append(ct.target);
1377
append(p_arguments.size());
1378
append(p_function_name);
1379
ct.cleanup();
1380
}
1381
1382
void GDScriptByteCodeGenerator::write_lambda(const Address &p_target, GDScriptFunction *p_function, const Vector<Address> &p_captures, bool p_use_self) {
1383
append_opcode_and_argcount(p_use_self ? GDScriptFunction::OPCODE_CREATE_SELF_LAMBDA : GDScriptFunction::OPCODE_CREATE_LAMBDA, 1 + p_captures.size());
1384
for (int i = 0; i < p_captures.size(); i++) {
1385
append(p_captures[i]);
1386
}
1387
1388
CallTarget ct = get_call_target(p_target);
1389
append(ct.target);
1390
append(p_captures.size());
1391
append(p_function);
1392
ct.cleanup();
1393
}
1394
1395
void GDScriptByteCodeGenerator::write_construct(const Address &p_target, Variant::Type p_type, const Vector<Address> &p_arguments) {
1396
// Try to find an appropriate constructor.
1397
bool all_have_type = true;
1398
Vector<Variant::Type> arg_types;
1399
for (int i = 0; i < p_arguments.size(); i++) {
1400
if (!HAS_BUILTIN_TYPE(p_arguments[i])) {
1401
all_have_type = false;
1402
break;
1403
}
1404
arg_types.push_back(p_arguments[i].type.builtin_type);
1405
}
1406
if (all_have_type) {
1407
int valid_constructor = -1;
1408
for (int i = 0; i < Variant::get_constructor_count(p_type); i++) {
1409
if (Variant::get_constructor_argument_count(p_type, i) != p_arguments.size()) {
1410
continue;
1411
}
1412
int types_correct = true;
1413
for (int j = 0; j < arg_types.size(); j++) {
1414
if (arg_types[j] != Variant::get_constructor_argument_type(p_type, i, j)) {
1415
types_correct = false;
1416
break;
1417
}
1418
}
1419
if (types_correct) {
1420
valid_constructor = i;
1421
break;
1422
}
1423
}
1424
if (valid_constructor >= 0) {
1425
append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT_VALIDATED, 1 + p_arguments.size());
1426
for (int i = 0; i < p_arguments.size(); i++) {
1427
append(p_arguments[i]);
1428
}
1429
CallTarget ct = get_call_target(p_target);
1430
append(ct.target);
1431
append(p_arguments.size());
1432
append(Variant::get_validated_constructor(p_type, valid_constructor));
1433
ct.cleanup();
1434
#ifdef DEBUG_ENABLED
1435
add_debug_name(constructors_names, get_constructor_pos(Variant::get_validated_constructor(p_type, valid_constructor)), Variant::get_type_name(p_type));
1436
#endif
1437
return;
1438
}
1439
}
1440
1441
append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT, 1 + p_arguments.size());
1442
for (int i = 0; i < p_arguments.size(); i++) {
1443
append(p_arguments[i]);
1444
}
1445
CallTarget ct = get_call_target(p_target);
1446
append(ct.target);
1447
append(p_arguments.size());
1448
append(p_type);
1449
ct.cleanup();
1450
}
1451
1452
void GDScriptByteCodeGenerator::write_construct_array(const Address &p_target, const Vector<Address> &p_arguments) {
1453
append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT_ARRAY, 1 + p_arguments.size());
1454
for (int i = 0; i < p_arguments.size(); i++) {
1455
append(p_arguments[i]);
1456
}
1457
CallTarget ct = get_call_target(p_target);
1458
append(ct.target);
1459
append(p_arguments.size());
1460
ct.cleanup();
1461
}
1462
1463
void GDScriptByteCodeGenerator::write_construct_typed_array(const Address &p_target, const GDScriptDataType &p_element_type, const Vector<Address> &p_arguments) {
1464
append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT_TYPED_ARRAY, 2 + p_arguments.size());
1465
for (int i = 0; i < p_arguments.size(); i++) {
1466
append(p_arguments[i]);
1467
}
1468
CallTarget ct = get_call_target(p_target);
1469
append(ct.target);
1470
append(get_constant_pos(p_element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
1471
append(p_arguments.size());
1472
append(p_element_type.builtin_type);
1473
append(p_element_type.native_type);
1474
ct.cleanup();
1475
}
1476
1477
void GDScriptByteCodeGenerator::write_construct_dictionary(const Address &p_target, const Vector<Address> &p_arguments) {
1478
append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY, 1 + p_arguments.size());
1479
for (int i = 0; i < p_arguments.size(); i++) {
1480
append(p_arguments[i]);
1481
}
1482
CallTarget ct = get_call_target(p_target);
1483
append(ct.target);
1484
append(p_arguments.size() / 2); // This is number of key-value pairs, so only half of actual arguments.
1485
ct.cleanup();
1486
}
1487
1488
void GDScriptByteCodeGenerator::write_construct_typed_dictionary(const Address &p_target, const GDScriptDataType &p_key_type, const GDScriptDataType &p_value_type, const Vector<Address> &p_arguments) {
1489
append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT_TYPED_DICTIONARY, 3 + p_arguments.size());
1490
for (int i = 0; i < p_arguments.size(); i++) {
1491
append(p_arguments[i]);
1492
}
1493
CallTarget ct = get_call_target(p_target);
1494
append(ct.target);
1495
append(get_constant_pos(p_key_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
1496
append(get_constant_pos(p_value_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
1497
append(p_arguments.size() / 2); // This is number of key-value pairs, so only half of actual arguments.
1498
append(p_key_type.builtin_type);
1499
append(p_key_type.native_type);
1500
append(p_value_type.builtin_type);
1501
append(p_value_type.native_type);
1502
ct.cleanup();
1503
}
1504
1505
void GDScriptByteCodeGenerator::write_await(const Address &p_target, const Address &p_operand) {
1506
append_opcode(GDScriptFunction::OPCODE_AWAIT);
1507
append(p_operand);
1508
append_opcode(GDScriptFunction::OPCODE_AWAIT_RESUME);
1509
append(p_target);
1510
}
1511
1512
void GDScriptByteCodeGenerator::write_if(const Address &p_condition) {
1513
append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
1514
append(p_condition);
1515
if_jmp_addrs.push_back(opcodes.size());
1516
append(0); // Jump destination, will be patched.
1517
}
1518
1519
void GDScriptByteCodeGenerator::write_else() {
1520
append_opcode(GDScriptFunction::OPCODE_JUMP); // Jump from true if block;
1521
int else_jmp_addr = opcodes.size();
1522
append(0); // Jump destination, will be patched.
1523
1524
patch_jump(if_jmp_addrs.back()->get());
1525
if_jmp_addrs.pop_back();
1526
if_jmp_addrs.push_back(else_jmp_addr);
1527
}
1528
1529
void GDScriptByteCodeGenerator::write_endif() {
1530
patch_jump(if_jmp_addrs.back()->get());
1531
if_jmp_addrs.pop_back();
1532
}
1533
1534
void GDScriptByteCodeGenerator::write_jump_if_shared(const Address &p_value) {
1535
append_opcode(GDScriptFunction::OPCODE_JUMP_IF_SHARED);
1536
append(p_value);
1537
if_jmp_addrs.push_back(opcodes.size());
1538
append(0); // Jump destination, will be patched.
1539
}
1540
1541
void GDScriptByteCodeGenerator::write_end_jump_if_shared() {
1542
patch_jump(if_jmp_addrs.back()->get());
1543
if_jmp_addrs.pop_back();
1544
}
1545
1546
void GDScriptByteCodeGenerator::start_for(const GDScriptDataType &p_iterator_type, const GDScriptDataType &p_list_type, bool p_is_range) {
1547
Address counter(Address::LOCAL_VARIABLE, add_local("@counter_pos", p_iterator_type), p_iterator_type);
1548
1549
// Store state.
1550
for_counter_variables.push_back(counter);
1551
1552
if (p_is_range) {
1553
GDScriptDataType int_type;
1554
int_type.has_type = true;
1555
int_type.kind = GDScriptDataType::BUILTIN;
1556
int_type.builtin_type = Variant::INT;
1557
1558
Address range_from(Address::LOCAL_VARIABLE, add_local("@range_from", int_type), int_type);
1559
Address range_to(Address::LOCAL_VARIABLE, add_local("@range_to", int_type), int_type);
1560
Address range_step(Address::LOCAL_VARIABLE, add_local("@range_step", int_type), int_type);
1561
1562
// Store state.
1563
for_range_from_variables.push_back(range_from);
1564
for_range_to_variables.push_back(range_to);
1565
for_range_step_variables.push_back(range_step);
1566
} else {
1567
Address container(Address::LOCAL_VARIABLE, add_local("@container_pos", p_list_type), p_list_type);
1568
1569
// Store state.
1570
for_container_variables.push_back(container);
1571
}
1572
}
1573
1574
void GDScriptByteCodeGenerator::write_for_list_assignment(const Address &p_list) {
1575
const Address &container = for_container_variables.back()->get();
1576
1577
// Assign container.
1578
append_opcode(GDScriptFunction::OPCODE_ASSIGN);
1579
append(container);
1580
append(p_list);
1581
}
1582
1583
void GDScriptByteCodeGenerator::write_for_range_assignment(const Address &p_from, const Address &p_to, const Address &p_step) {
1584
const Address &range_from = for_range_from_variables.back()->get();
1585
const Address &range_to = for_range_to_variables.back()->get();
1586
const Address &range_step = for_range_step_variables.back()->get();
1587
1588
// Assign range args.
1589
if (range_from.type == p_from.type) {
1590
write_assign(range_from, p_from);
1591
} else {
1592
write_assign_with_conversion(range_from, p_from);
1593
}
1594
if (range_to.type == p_to.type) {
1595
write_assign(range_to, p_to);
1596
} else {
1597
write_assign_with_conversion(range_to, p_to);
1598
}
1599
if (range_step.type == p_step.type) {
1600
write_assign(range_step, p_step);
1601
} else {
1602
write_assign_with_conversion(range_step, p_step);
1603
}
1604
}
1605
1606
void GDScriptByteCodeGenerator::write_for(const Address &p_variable, bool p_use_conversion, bool p_is_range) {
1607
const Address &counter = for_counter_variables.back()->get();
1608
const Address &container = p_is_range ? Address() : for_container_variables.back()->get();
1609
const Address &range_from = p_is_range ? for_range_from_variables.back()->get() : Address();
1610
const Address &range_to = p_is_range ? for_range_to_variables.back()->get() : Address();
1611
const Address &range_step = p_is_range ? for_range_step_variables.back()->get() : Address();
1612
1613
current_breaks_to_patch.push_back(List<int>());
1614
1615
GDScriptFunction::Opcode begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN;
1616
GDScriptFunction::Opcode iterate_opcode = GDScriptFunction::OPCODE_ITERATE;
1617
1618
if (p_is_range) {
1619
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_RANGE;
1620
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_RANGE;
1621
} else if (container.type.has_type) {
1622
if (container.type.kind == GDScriptDataType::BUILTIN) {
1623
switch (container.type.builtin_type) {
1624
case Variant::INT:
1625
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_INT;
1626
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_INT;
1627
break;
1628
case Variant::FLOAT:
1629
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_FLOAT;
1630
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_FLOAT;
1631
break;
1632
case Variant::VECTOR2:
1633
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2;
1634
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2;
1635
break;
1636
case Variant::VECTOR2I:
1637
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2I;
1638
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2I;
1639
break;
1640
case Variant::VECTOR3:
1641
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3;
1642
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3;
1643
break;
1644
case Variant::VECTOR3I:
1645
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3I;
1646
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3I;
1647
break;
1648
case Variant::STRING:
1649
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_STRING;
1650
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_STRING;
1651
break;
1652
case Variant::DICTIONARY:
1653
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_DICTIONARY;
1654
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_DICTIONARY;
1655
break;
1656
case Variant::ARRAY:
1657
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_ARRAY;
1658
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_ARRAY;
1659
break;
1660
case Variant::PACKED_BYTE_ARRAY:
1661
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_BYTE_ARRAY;
1662
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_BYTE_ARRAY;
1663
break;
1664
case Variant::PACKED_INT32_ARRAY:
1665
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT32_ARRAY;
1666
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT32_ARRAY;
1667
break;
1668
case Variant::PACKED_INT64_ARRAY:
1669
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT64_ARRAY;
1670
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT64_ARRAY;
1671
break;
1672
case Variant::PACKED_FLOAT32_ARRAY:
1673
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT32_ARRAY;
1674
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT32_ARRAY;
1675
break;
1676
case Variant::PACKED_FLOAT64_ARRAY:
1677
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT64_ARRAY;
1678
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT64_ARRAY;
1679
break;
1680
case Variant::PACKED_STRING_ARRAY:
1681
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_STRING_ARRAY;
1682
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_STRING_ARRAY;
1683
break;
1684
case Variant::PACKED_VECTOR2_ARRAY:
1685
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR2_ARRAY;
1686
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR2_ARRAY;
1687
break;
1688
case Variant::PACKED_VECTOR3_ARRAY:
1689
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR3_ARRAY;
1690
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR3_ARRAY;
1691
break;
1692
case Variant::PACKED_COLOR_ARRAY:
1693
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_COLOR_ARRAY;
1694
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_COLOR_ARRAY;
1695
break;
1696
case Variant::PACKED_VECTOR4_ARRAY:
1697
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR4_ARRAY;
1698
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR4_ARRAY;
1699
break;
1700
default:
1701
break;
1702
}
1703
} else {
1704
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_OBJECT;
1705
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_OBJECT;
1706
}
1707
}
1708
1709
Address temp;
1710
if (p_use_conversion) {
1711
temp = Address(Address::LOCAL_VARIABLE, add_local("@iterator_temp", GDScriptDataType()));
1712
}
1713
1714
// Begin loop.
1715
append_opcode(begin_opcode);
1716
append(counter);
1717
if (p_is_range) {
1718
append(range_from);
1719
append(range_to);
1720
append(range_step);
1721
} else {
1722
append(container);
1723
}
1724
append(p_use_conversion ? temp : p_variable);
1725
for_jmp_addrs.push_back(opcodes.size());
1726
append(0); // End of loop address, will be patched.
1727
append_opcode(GDScriptFunction::OPCODE_JUMP);
1728
append(opcodes.size() + (p_is_range ? 7 : 6)); // Skip over 'continue' code.
1729
1730
// Next iteration.
1731
int continue_addr = opcodes.size();
1732
continue_addrs.push_back(continue_addr);
1733
append_opcode(iterate_opcode);
1734
append(counter);
1735
if (p_is_range) {
1736
append(range_to);
1737
append(range_step);
1738
} else {
1739
append(container);
1740
}
1741
append(p_use_conversion ? temp : p_variable);
1742
for_jmp_addrs.push_back(opcodes.size());
1743
append(0); // Jump destination, will be patched.
1744
1745
if (p_use_conversion) {
1746
write_assign_with_conversion(p_variable, temp);
1747
if (p_variable.type.can_contain_object()) {
1748
clear_address(temp); // Can contain `RefCounted`, so clear it.
1749
}
1750
}
1751
}
1752
1753
void GDScriptByteCodeGenerator::write_endfor(bool p_is_range) {
1754
// Jump back to loop check.
1755
append_opcode(GDScriptFunction::OPCODE_JUMP);
1756
append(continue_addrs.back()->get());
1757
continue_addrs.pop_back();
1758
1759
// Patch end jumps (two of them).
1760
for (int i = 0; i < 2; i++) {
1761
patch_jump(for_jmp_addrs.back()->get());
1762
for_jmp_addrs.pop_back();
1763
}
1764
1765
// Patch break statements.
1766
for (const int &E : current_breaks_to_patch.back()->get()) {
1767
patch_jump(E);
1768
}
1769
current_breaks_to_patch.pop_back();
1770
1771
// Pop state.
1772
for_counter_variables.pop_back();
1773
if (p_is_range) {
1774
for_range_from_variables.pop_back();
1775
for_range_to_variables.pop_back();
1776
for_range_step_variables.pop_back();
1777
} else {
1778
for_container_variables.pop_back();
1779
}
1780
}
1781
1782
void GDScriptByteCodeGenerator::start_while_condition() {
1783
current_breaks_to_patch.push_back(List<int>());
1784
continue_addrs.push_back(opcodes.size());
1785
}
1786
1787
void GDScriptByteCodeGenerator::write_while(const Address &p_condition) {
1788
// Condition check.
1789
append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
1790
append(p_condition);
1791
while_jmp_addrs.push_back(opcodes.size());
1792
append(0); // End of loop address, will be patched.
1793
}
1794
1795
void GDScriptByteCodeGenerator::write_endwhile() {
1796
// Jump back to loop check.
1797
append_opcode(GDScriptFunction::OPCODE_JUMP);
1798
append(continue_addrs.back()->get());
1799
continue_addrs.pop_back();
1800
1801
// Patch end jump.
1802
patch_jump(while_jmp_addrs.back()->get());
1803
while_jmp_addrs.pop_back();
1804
1805
// Patch break statements.
1806
for (const int &E : current_breaks_to_patch.back()->get()) {
1807
patch_jump(E);
1808
}
1809
current_breaks_to_patch.pop_back();
1810
}
1811
1812
void GDScriptByteCodeGenerator::write_break() {
1813
append_opcode(GDScriptFunction::OPCODE_JUMP);
1814
current_breaks_to_patch.back()->get().push_back(opcodes.size());
1815
append(0);
1816
}
1817
1818
void GDScriptByteCodeGenerator::write_continue() {
1819
append_opcode(GDScriptFunction::OPCODE_JUMP);
1820
append(continue_addrs.back()->get());
1821
}
1822
1823
void GDScriptByteCodeGenerator::write_breakpoint() {
1824
append_opcode(GDScriptFunction::OPCODE_BREAKPOINT);
1825
}
1826
1827
void GDScriptByteCodeGenerator::write_newline(int p_line) {
1828
if (GDScriptLanguage::get_singleton()->should_track_call_stack()) {
1829
// Add newline for debugger and stack tracking if enabled in the project settings.
1830
append_opcode(GDScriptFunction::OPCODE_LINE);
1831
append(p_line);
1832
current_line = p_line;
1833
}
1834
}
1835
1836
void GDScriptByteCodeGenerator::write_return(const Address &p_return_value) {
1837
if (!function->return_type.has_type || p_return_value.type.has_type) {
1838
// Either the function is untyped or the return value is also typed.
1839
1840
// If this is a typed function, then we need to check for potential conversions.
1841
if (function->return_type.has_type) {
1842
if (function->return_type.kind == GDScriptDataType::BUILTIN && function->return_type.builtin_type == Variant::ARRAY && function->return_type.has_container_element_type(0)) {
1843
// Typed array.
1844
const GDScriptDataType &element_type = function->return_type.get_container_element_type(0);
1845
append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_ARRAY);
1846
append(p_return_value);
1847
append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
1848
append(element_type.builtin_type);
1849
append(element_type.native_type);
1850
} else if (function->return_type.kind == GDScriptDataType::BUILTIN && function->return_type.builtin_type == Variant::DICTIONARY &&
1851
function->return_type.has_container_element_types()) {
1852
// Typed dictionary.
1853
const GDScriptDataType &key_type = function->return_type.get_container_element_type_or_variant(0);
1854
const GDScriptDataType &value_type = function->return_type.get_container_element_type_or_variant(1);
1855
append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_DICTIONARY);
1856
append(p_return_value);
1857
append(get_constant_pos(key_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
1858
append(get_constant_pos(value_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
1859
append(key_type.builtin_type);
1860
append(key_type.native_type);
1861
append(value_type.builtin_type);
1862
append(value_type.native_type);
1863
} else if (function->return_type.kind == GDScriptDataType::BUILTIN && p_return_value.type.kind == GDScriptDataType::BUILTIN && function->return_type.builtin_type != p_return_value.type.builtin_type) {
1864
// Add conversion.
1865
append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_BUILTIN);
1866
append(p_return_value);
1867
append(function->return_type.builtin_type);
1868
} else {
1869
// Just assign.
1870
append_opcode(GDScriptFunction::OPCODE_RETURN);
1871
append(p_return_value);
1872
}
1873
} else {
1874
append_opcode(GDScriptFunction::OPCODE_RETURN);
1875
append(p_return_value);
1876
}
1877
} else {
1878
switch (function->return_type.kind) {
1879
case GDScriptDataType::BUILTIN: {
1880
if (function->return_type.builtin_type == Variant::ARRAY && function->return_type.has_container_element_type(0)) {
1881
const GDScriptDataType &element_type = function->return_type.get_container_element_type(0);
1882
append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_ARRAY);
1883
append(p_return_value);
1884
append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
1885
append(element_type.builtin_type);
1886
append(element_type.native_type);
1887
} else if (function->return_type.builtin_type == Variant::DICTIONARY && function->return_type.has_container_element_types()) {
1888
const GDScriptDataType &key_type = function->return_type.get_container_element_type_or_variant(0);
1889
const GDScriptDataType &value_type = function->return_type.get_container_element_type_or_variant(1);
1890
append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_DICTIONARY);
1891
append(p_return_value);
1892
append(get_constant_pos(key_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
1893
append(get_constant_pos(value_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
1894
append(key_type.builtin_type);
1895
append(key_type.native_type);
1896
append(value_type.builtin_type);
1897
append(value_type.native_type);
1898
} else {
1899
append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_BUILTIN);
1900
append(p_return_value);
1901
append(function->return_type.builtin_type);
1902
}
1903
} break;
1904
case GDScriptDataType::NATIVE: {
1905
append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_NATIVE);
1906
append(p_return_value);
1907
int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[function->return_type.native_type];
1908
Variant nc = GDScriptLanguage::get_singleton()->get_global_array()[class_idx];
1909
class_idx = get_constant_pos(nc) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
1910
append(class_idx);
1911
} break;
1912
case GDScriptDataType::GDSCRIPT:
1913
case GDScriptDataType::SCRIPT: {
1914
Variant script = function->return_type.script_type;
1915
int script_idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
1916
1917
append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_SCRIPT);
1918
append(p_return_value);
1919
append(script_idx);
1920
} break;
1921
default: {
1922
ERR_PRINT("Compiler bug: unresolved return.");
1923
1924
// Shouldn't get here, but fail-safe to a regular return;
1925
append_opcode(GDScriptFunction::OPCODE_RETURN);
1926
append(p_return_value);
1927
} break;
1928
}
1929
}
1930
}
1931
1932
void GDScriptByteCodeGenerator::write_assert(const Address &p_test, const Address &p_message) {
1933
append_opcode(GDScriptFunction::OPCODE_ASSERT);
1934
append(p_test);
1935
append(p_message);
1936
}
1937
1938
void GDScriptByteCodeGenerator::start_block() {
1939
push_stack_identifiers();
1940
}
1941
1942
void GDScriptByteCodeGenerator::end_block() {
1943
pop_stack_identifiers();
1944
}
1945
1946
void GDScriptByteCodeGenerator::clear_temporaries() {
1947
for (int slot_idx : temporaries_pending_clear) {
1948
// The temporary may have been reused as something else since it was added to the list.
1949
// In that case, there's **no** need to clear it.
1950
if (temporaries[slot_idx].can_contain_object) {
1951
clear_address(Address(Address::TEMPORARY, slot_idx)); // Can contain `RefCounted`, so clear it.
1952
}
1953
}
1954
temporaries_pending_clear.clear();
1955
}
1956
1957
void GDScriptByteCodeGenerator::clear_address(const Address &p_address) {
1958
// Do not check `is_local_dirty()` here! Always clear the address since the codegen doesn't track the compiler.
1959
// Also, this method is used to initialize local variables of built-in types, since they cannot be `null`.
1960
1961
if (p_address.type.has_type && p_address.type.kind == GDScriptDataType::BUILTIN) {
1962
switch (p_address.type.builtin_type) {
1963
case Variant::BOOL:
1964
write_assign_false(p_address);
1965
break;
1966
case Variant::DICTIONARY:
1967
if (p_address.type.has_container_element_types()) {
1968
write_construct_typed_dictionary(p_address, p_address.type.get_container_element_type_or_variant(0), p_address.type.get_container_element_type_or_variant(1), Vector<GDScriptCodeGenerator::Address>());
1969
} else {
1970
write_construct(p_address, p_address.type.builtin_type, Vector<GDScriptCodeGenerator::Address>());
1971
}
1972
break;
1973
case Variant::ARRAY:
1974
if (p_address.type.has_container_element_type(0)) {
1975
write_construct_typed_array(p_address, p_address.type.get_container_element_type(0), Vector<GDScriptCodeGenerator::Address>());
1976
} else {
1977
write_construct(p_address, p_address.type.builtin_type, Vector<GDScriptCodeGenerator::Address>());
1978
}
1979
break;
1980
case Variant::NIL:
1981
case Variant::OBJECT:
1982
write_assign_null(p_address);
1983
break;
1984
default:
1985
write_construct(p_address, p_address.type.builtin_type, Vector<GDScriptCodeGenerator::Address>());
1986
break;
1987
}
1988
} else {
1989
write_assign_null(p_address);
1990
}
1991
1992
if (p_address.mode == Address::LOCAL_VARIABLE) {
1993
dirty_locals.erase(p_address.address);
1994
}
1995
}
1996
1997
// Returns `true` if the local has been reused and not cleaned up with `clear_address()`.
1998
bool GDScriptByteCodeGenerator::is_local_dirty(const Address &p_address) const {
1999
ERR_FAIL_COND_V(p_address.mode != Address::LOCAL_VARIABLE, false);
2000
return dirty_locals.has(p_address.address);
2001
}
2002
2003
GDScriptByteCodeGenerator::~GDScriptByteCodeGenerator() {
2004
if (!ended && function != nullptr) {
2005
memdelete(function);
2006
}
2007
}
2008
2009