Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gdscript/gdscript_byte_codegen.cpp
11351 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.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.kind == GDScriptDataType::BUILTIN)
427
428
#define IS_BUILTIN_TYPE(m_var, m_type) \
429
(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.kind = GDScriptDataType::BUILTIN;
1074
type.builtin_type = p_type;
1075
}
1076
uint32_t addr = add_temporary(type);
1077
return CallTarget(Address(Address::TEMPORARY, addr, type), true, this);
1078
} else {
1079
return CallTarget(p_target, false, this);
1080
}
1081
}
1082
1083
void GDScriptByteCodeGenerator::write_call(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
1084
append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
1085
for (int i = 0; i < p_arguments.size(); i++) {
1086
append(p_arguments[i]);
1087
}
1088
append(p_base);
1089
CallTarget ct = get_call_target(p_target);
1090
append(ct.target);
1091
append(p_arguments.size());
1092
append(p_function_name);
1093
ct.cleanup();
1094
}
1095
1096
void GDScriptByteCodeGenerator::write_super_call(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
1097
append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_SELF_BASE, 1 + p_arguments.size());
1098
for (int i = 0; i < p_arguments.size(); i++) {
1099
append(p_arguments[i]);
1100
}
1101
CallTarget ct = get_call_target(p_target);
1102
append(ct.target);
1103
append(p_arguments.size());
1104
append(p_function_name);
1105
ct.cleanup();
1106
}
1107
1108
void GDScriptByteCodeGenerator::write_call_async(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
1109
append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_ASYNC, 2 + p_arguments.size());
1110
for (int i = 0; i < p_arguments.size(); i++) {
1111
append(p_arguments[i]);
1112
}
1113
append(p_base);
1114
CallTarget ct = get_call_target(p_target);
1115
append(ct.target);
1116
append(p_arguments.size());
1117
append(p_function_name);
1118
ct.cleanup();
1119
}
1120
1121
void GDScriptByteCodeGenerator::write_call_gdscript_utility(const Address &p_target, const StringName &p_function, const Vector<Address> &p_arguments) {
1122
append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_GDSCRIPT_UTILITY, 1 + p_arguments.size());
1123
GDScriptUtilityFunctions::FunctionPtr gds_function = GDScriptUtilityFunctions::get_function(p_function);
1124
for (int i = 0; i < p_arguments.size(); i++) {
1125
append(p_arguments[i]);
1126
}
1127
CallTarget ct = get_call_target(p_target);
1128
append(ct.target);
1129
append(p_arguments.size());
1130
append(gds_function);
1131
ct.cleanup();
1132
#ifdef DEBUG_ENABLED
1133
add_debug_name(gds_utilities_names, get_gds_utility_pos(gds_function), p_function);
1134
#endif
1135
}
1136
1137
void GDScriptByteCodeGenerator::write_call_utility(const Address &p_target, const StringName &p_function, const Vector<Address> &p_arguments) {
1138
bool is_validated = true;
1139
if (Variant::is_utility_function_vararg(p_function)) {
1140
is_validated = false; // Vararg needs runtime checks, can't use validated call.
1141
} else if (p_arguments.size() == Variant::get_utility_function_argument_count(p_function)) {
1142
bool all_types_exact = true;
1143
for (int i = 0; i < p_arguments.size(); i++) {
1144
if (!IS_BUILTIN_TYPE(p_arguments[i], Variant::get_utility_function_argument_type(p_function, i))) {
1145
all_types_exact = false;
1146
break;
1147
}
1148
}
1149
1150
is_validated = all_types_exact;
1151
}
1152
1153
if (is_validated) {
1154
Variant::Type result_type = Variant::has_utility_function_return_value(p_function) ? Variant::get_utility_function_return_type(p_function) : Variant::NIL;
1155
CallTarget ct = get_call_target(p_target, result_type);
1156
Variant::Type temp_type = temporaries[ct.target.address].type;
1157
if (result_type != temp_type) {
1158
write_type_adjust(ct.target, result_type);
1159
}
1160
append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_UTILITY_VALIDATED, 1 + p_arguments.size());
1161
for (int i = 0; i < p_arguments.size(); i++) {
1162
append(p_arguments[i]);
1163
}
1164
append(ct.target);
1165
append(p_arguments.size());
1166
append(Variant::get_validated_utility_function(p_function));
1167
ct.cleanup();
1168
#ifdef DEBUG_ENABLED
1169
add_debug_name(utilities_names, get_utility_pos(Variant::get_validated_utility_function(p_function)), p_function);
1170
#endif
1171
} else {
1172
append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_UTILITY, 1 + p_arguments.size());
1173
for (int i = 0; i < p_arguments.size(); i++) {
1174
append(p_arguments[i]);
1175
}
1176
CallTarget ct = get_call_target(p_target);
1177
append(ct.target);
1178
append(p_arguments.size());
1179
append(p_function);
1180
ct.cleanup();
1181
}
1182
}
1183
1184
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) {
1185
bool is_validated = false;
1186
1187
// Check if all types are correct.
1188
if (Variant::is_builtin_method_vararg(p_type, p_method)) {
1189
is_validated = false; // Vararg needs runtime checks, can't use validated call.
1190
} else if (p_arguments.size() == Variant::get_builtin_method_argument_count(p_type, p_method)) {
1191
bool all_types_exact = true;
1192
for (int i = 0; i < p_arguments.size(); i++) {
1193
if (!IS_BUILTIN_TYPE(p_arguments[i], Variant::get_builtin_method_argument_type(p_type, p_method, i))) {
1194
all_types_exact = false;
1195
break;
1196
}
1197
}
1198
1199
is_validated = all_types_exact;
1200
}
1201
1202
if (!is_validated) {
1203
// Perform regular call.
1204
if (p_is_static) {
1205
append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_BUILTIN_STATIC, p_arguments.size() + 1);
1206
for (int i = 0; i < p_arguments.size(); i++) {
1207
append(p_arguments[i]);
1208
}
1209
CallTarget ct = get_call_target(p_target);
1210
append(ct.target);
1211
append(p_type);
1212
append(p_method);
1213
append(p_arguments.size());
1214
ct.cleanup();
1215
} else {
1216
write_call(p_target, p_base, p_method, p_arguments);
1217
}
1218
return;
1219
}
1220
1221
Variant::Type result_type = Variant::get_builtin_method_return_type(p_type, p_method);
1222
CallTarget ct = get_call_target(p_target, result_type);
1223
Variant::Type temp_type = temporaries[ct.target.address].type;
1224
if (result_type != temp_type) {
1225
write_type_adjust(ct.target, result_type);
1226
}
1227
1228
append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_BUILTIN_TYPE_VALIDATED, 2 + p_arguments.size());
1229
1230
for (int i = 0; i < p_arguments.size(); i++) {
1231
append(p_arguments[i]);
1232
}
1233
append(p_base);
1234
append(ct.target);
1235
append(p_arguments.size());
1236
append(Variant::get_validated_builtin_method(p_type, p_method));
1237
ct.cleanup();
1238
1239
#ifdef DEBUG_ENABLED
1240
add_debug_name(builtin_methods_names, get_builtin_method_pos(Variant::get_validated_builtin_method(p_type, p_method)), p_method);
1241
#endif
1242
}
1243
1244
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) {
1245
write_call_builtin_type(p_target, p_base, p_type, p_method, false, p_arguments);
1246
}
1247
1248
void GDScriptByteCodeGenerator::write_call_builtin_type_static(const Address &p_target, Variant::Type p_type, const StringName &p_method, const Vector<Address> &p_arguments) {
1249
write_call_builtin_type(p_target, Address(), p_type, p_method, true, p_arguments);
1250
}
1251
1252
void GDScriptByteCodeGenerator::write_call_native_static(const Address &p_target, const StringName &p_class, const StringName &p_method, const Vector<Address> &p_arguments) {
1253
MethodBind *method = ClassDB::get_method(p_class, p_method);
1254
1255
// Perform regular call.
1256
append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_NATIVE_STATIC, p_arguments.size() + 1);
1257
for (int i = 0; i < p_arguments.size(); i++) {
1258
append(p_arguments[i]);
1259
}
1260
CallTarget ct = get_call_target(p_target);
1261
append(ct.target);
1262
append(method);
1263
append(p_arguments.size());
1264
ct.cleanup();
1265
return;
1266
}
1267
1268
void GDScriptByteCodeGenerator::write_call_native_static_validated(const GDScriptCodeGenerator::Address &p_target, MethodBind *p_method, const Vector<GDScriptCodeGenerator::Address> &p_arguments) {
1269
Variant::Type return_type = Variant::NIL;
1270
bool has_return = p_method->has_return();
1271
1272
if (has_return) {
1273
PropertyInfo return_info = p_method->get_return_info();
1274
return_type = return_info.type;
1275
}
1276
1277
CallTarget ct = get_call_target(p_target, return_type);
1278
1279
if (has_return) {
1280
Variant::Type temp_type = temporaries[ct.target.address].type;
1281
if (temp_type != return_type) {
1282
write_type_adjust(ct.target, return_type);
1283
}
1284
}
1285
1286
GDScriptFunction::Opcode code = p_method->has_return() ? GDScriptFunction::OPCODE_CALL_NATIVE_STATIC_VALIDATED_RETURN : GDScriptFunction::OPCODE_CALL_NATIVE_STATIC_VALIDATED_NO_RETURN;
1287
append_opcode_and_argcount(code, 1 + p_arguments.size());
1288
1289
for (int i = 0; i < p_arguments.size(); i++) {
1290
append(p_arguments[i]);
1291
}
1292
append(ct.target);
1293
append(p_arguments.size());
1294
append(p_method);
1295
ct.cleanup();
1296
}
1297
1298
void GDScriptByteCodeGenerator::write_call_method_bind(const Address &p_target, const Address &p_base, MethodBind *p_method, const Vector<Address> &p_arguments) {
1299
append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL_METHOD_BIND : GDScriptFunction::OPCODE_CALL_METHOD_BIND_RET, 2 + p_arguments.size());
1300
for (int i = 0; i < p_arguments.size(); i++) {
1301
append(p_arguments[i]);
1302
}
1303
CallTarget ct = get_call_target(p_target);
1304
append(p_base);
1305
append(ct.target);
1306
append(p_arguments.size());
1307
append(p_method);
1308
ct.cleanup();
1309
}
1310
1311
void GDScriptByteCodeGenerator::write_call_method_bind_validated(const Address &p_target, const Address &p_base, MethodBind *p_method, const Vector<Address> &p_arguments) {
1312
Variant::Type return_type = Variant::NIL;
1313
bool has_return = p_method->has_return();
1314
1315
if (has_return) {
1316
PropertyInfo return_info = p_method->get_return_info();
1317
return_type = return_info.type;
1318
}
1319
1320
CallTarget ct = get_call_target(p_target, return_type);
1321
1322
if (has_return) {
1323
Variant::Type temp_type = temporaries[ct.target.address].type;
1324
if (temp_type != return_type) {
1325
write_type_adjust(ct.target, return_type);
1326
}
1327
}
1328
1329
GDScriptFunction::Opcode code = p_method->has_return() ? GDScriptFunction::OPCODE_CALL_METHOD_BIND_VALIDATED_RETURN : GDScriptFunction::OPCODE_CALL_METHOD_BIND_VALIDATED_NO_RETURN;
1330
append_opcode_and_argcount(code, 2 + p_arguments.size());
1331
1332
for (int i = 0; i < p_arguments.size(); i++) {
1333
append(p_arguments[i]);
1334
}
1335
append(p_base);
1336
append(ct.target);
1337
append(p_arguments.size());
1338
append(p_method);
1339
ct.cleanup();
1340
}
1341
1342
void GDScriptByteCodeGenerator::write_call_self(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
1343
append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
1344
for (int i = 0; i < p_arguments.size(); i++) {
1345
append(p_arguments[i]);
1346
}
1347
append(GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
1348
CallTarget ct = get_call_target(p_target);
1349
append(ct.target);
1350
append(p_arguments.size());
1351
append(p_function_name);
1352
ct.cleanup();
1353
}
1354
1355
void GDScriptByteCodeGenerator::write_call_self_async(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
1356
append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_ASYNC, 2 + p_arguments.size());
1357
for (int i = 0; i < p_arguments.size(); i++) {
1358
append(p_arguments[i]);
1359
}
1360
append(GDScriptFunction::ADDR_SELF);
1361
CallTarget ct = get_call_target(p_target);
1362
append(ct.target);
1363
append(p_arguments.size());
1364
append(p_function_name);
1365
ct.cleanup();
1366
}
1367
1368
void GDScriptByteCodeGenerator::write_call_script_function(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
1369
append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
1370
for (int i = 0; i < p_arguments.size(); i++) {
1371
append(p_arguments[i]);
1372
}
1373
append(p_base);
1374
CallTarget ct = get_call_target(p_target);
1375
append(ct.target);
1376
append(p_arguments.size());
1377
append(p_function_name);
1378
ct.cleanup();
1379
}
1380
1381
void GDScriptByteCodeGenerator::write_lambda(const Address &p_target, GDScriptFunction *p_function, const Vector<Address> &p_captures, bool p_use_self) {
1382
append_opcode_and_argcount(p_use_self ? GDScriptFunction::OPCODE_CREATE_SELF_LAMBDA : GDScriptFunction::OPCODE_CREATE_LAMBDA, 1 + p_captures.size());
1383
for (int i = 0; i < p_captures.size(); i++) {
1384
append(p_captures[i]);
1385
}
1386
1387
CallTarget ct = get_call_target(p_target);
1388
append(ct.target);
1389
append(p_captures.size());
1390
append(p_function);
1391
ct.cleanup();
1392
}
1393
1394
void GDScriptByteCodeGenerator::write_construct(const Address &p_target, Variant::Type p_type, const Vector<Address> &p_arguments) {
1395
// Try to find an appropriate constructor.
1396
bool all_have_type = true;
1397
Vector<Variant::Type> arg_types;
1398
for (int i = 0; i < p_arguments.size(); i++) {
1399
if (!HAS_BUILTIN_TYPE(p_arguments[i])) {
1400
all_have_type = false;
1401
break;
1402
}
1403
arg_types.push_back(p_arguments[i].type.builtin_type);
1404
}
1405
if (all_have_type) {
1406
int valid_constructor = -1;
1407
for (int i = 0; i < Variant::get_constructor_count(p_type); i++) {
1408
if (Variant::get_constructor_argument_count(p_type, i) != p_arguments.size()) {
1409
continue;
1410
}
1411
int types_correct = true;
1412
for (int j = 0; j < arg_types.size(); j++) {
1413
if (arg_types[j] != Variant::get_constructor_argument_type(p_type, i, j)) {
1414
types_correct = false;
1415
break;
1416
}
1417
}
1418
if (types_correct) {
1419
valid_constructor = i;
1420
break;
1421
}
1422
}
1423
if (valid_constructor >= 0) {
1424
append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT_VALIDATED, 1 + p_arguments.size());
1425
for (int i = 0; i < p_arguments.size(); i++) {
1426
append(p_arguments[i]);
1427
}
1428
CallTarget ct = get_call_target(p_target);
1429
append(ct.target);
1430
append(p_arguments.size());
1431
append(Variant::get_validated_constructor(p_type, valid_constructor));
1432
ct.cleanup();
1433
#ifdef DEBUG_ENABLED
1434
add_debug_name(constructors_names, get_constructor_pos(Variant::get_validated_constructor(p_type, valid_constructor)), Variant::get_type_name(p_type));
1435
#endif
1436
return;
1437
}
1438
}
1439
1440
append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT, 1 + p_arguments.size());
1441
for (int i = 0; i < p_arguments.size(); i++) {
1442
append(p_arguments[i]);
1443
}
1444
CallTarget ct = get_call_target(p_target);
1445
append(ct.target);
1446
append(p_arguments.size());
1447
append(p_type);
1448
ct.cleanup();
1449
}
1450
1451
void GDScriptByteCodeGenerator::write_construct_array(const Address &p_target, const Vector<Address> &p_arguments) {
1452
append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT_ARRAY, 1 + p_arguments.size());
1453
for (int i = 0; i < p_arguments.size(); i++) {
1454
append(p_arguments[i]);
1455
}
1456
CallTarget ct = get_call_target(p_target);
1457
append(ct.target);
1458
append(p_arguments.size());
1459
ct.cleanup();
1460
}
1461
1462
void GDScriptByteCodeGenerator::write_construct_typed_array(const Address &p_target, const GDScriptDataType &p_element_type, const Vector<Address> &p_arguments) {
1463
append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT_TYPED_ARRAY, 2 + p_arguments.size());
1464
for (int i = 0; i < p_arguments.size(); i++) {
1465
append(p_arguments[i]);
1466
}
1467
CallTarget ct = get_call_target(p_target);
1468
append(ct.target);
1469
append(get_constant_pos(p_element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
1470
append(p_arguments.size());
1471
append(p_element_type.builtin_type);
1472
append(p_element_type.native_type);
1473
ct.cleanup();
1474
}
1475
1476
void GDScriptByteCodeGenerator::write_construct_dictionary(const Address &p_target, const Vector<Address> &p_arguments) {
1477
append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY, 1 + p_arguments.size());
1478
for (int i = 0; i < p_arguments.size(); i++) {
1479
append(p_arguments[i]);
1480
}
1481
CallTarget ct = get_call_target(p_target);
1482
append(ct.target);
1483
append(p_arguments.size() / 2); // This is number of key-value pairs, so only half of actual arguments.
1484
ct.cleanup();
1485
}
1486
1487
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) {
1488
append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT_TYPED_DICTIONARY, 3 + p_arguments.size());
1489
for (int i = 0; i < p_arguments.size(); i++) {
1490
append(p_arguments[i]);
1491
}
1492
CallTarget ct = get_call_target(p_target);
1493
append(ct.target);
1494
append(get_constant_pos(p_key_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
1495
append(get_constant_pos(p_value_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
1496
append(p_arguments.size() / 2); // This is number of key-value pairs, so only half of actual arguments.
1497
append(p_key_type.builtin_type);
1498
append(p_key_type.native_type);
1499
append(p_value_type.builtin_type);
1500
append(p_value_type.native_type);
1501
ct.cleanup();
1502
}
1503
1504
void GDScriptByteCodeGenerator::write_await(const Address &p_target, const Address &p_operand) {
1505
append_opcode(GDScriptFunction::OPCODE_AWAIT);
1506
append(p_operand);
1507
append_opcode(GDScriptFunction::OPCODE_AWAIT_RESUME);
1508
append(p_target);
1509
}
1510
1511
void GDScriptByteCodeGenerator::write_if(const Address &p_condition) {
1512
append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
1513
append(p_condition);
1514
if_jmp_addrs.push_back(opcodes.size());
1515
append(0); // Jump destination, will be patched.
1516
}
1517
1518
void GDScriptByteCodeGenerator::write_else() {
1519
append_opcode(GDScriptFunction::OPCODE_JUMP); // Jump from true if block;
1520
int else_jmp_addr = opcodes.size();
1521
append(0); // Jump destination, will be patched.
1522
1523
patch_jump(if_jmp_addrs.back()->get());
1524
if_jmp_addrs.pop_back();
1525
if_jmp_addrs.push_back(else_jmp_addr);
1526
}
1527
1528
void GDScriptByteCodeGenerator::write_endif() {
1529
patch_jump(if_jmp_addrs.back()->get());
1530
if_jmp_addrs.pop_back();
1531
}
1532
1533
void GDScriptByteCodeGenerator::write_jump_if_shared(const Address &p_value) {
1534
append_opcode(GDScriptFunction::OPCODE_JUMP_IF_SHARED);
1535
append(p_value);
1536
if_jmp_addrs.push_back(opcodes.size());
1537
append(0); // Jump destination, will be patched.
1538
}
1539
1540
void GDScriptByteCodeGenerator::write_end_jump_if_shared() {
1541
patch_jump(if_jmp_addrs.back()->get());
1542
if_jmp_addrs.pop_back();
1543
}
1544
1545
void GDScriptByteCodeGenerator::start_for(const GDScriptDataType &p_iterator_type, const GDScriptDataType &p_list_type, bool p_is_range) {
1546
Address counter(Address::LOCAL_VARIABLE, add_local("@counter_pos", p_iterator_type), p_iterator_type);
1547
1548
// Store state.
1549
for_counter_variables.push_back(counter);
1550
1551
if (p_is_range) {
1552
GDScriptDataType int_type;
1553
int_type.kind = GDScriptDataType::BUILTIN;
1554
int_type.builtin_type = Variant::INT;
1555
1556
Address range_from(Address::LOCAL_VARIABLE, add_local("@range_from", int_type), int_type);
1557
Address range_to(Address::LOCAL_VARIABLE, add_local("@range_to", int_type), int_type);
1558
Address range_step(Address::LOCAL_VARIABLE, add_local("@range_step", int_type), int_type);
1559
1560
// Store state.
1561
for_range_from_variables.push_back(range_from);
1562
for_range_to_variables.push_back(range_to);
1563
for_range_step_variables.push_back(range_step);
1564
} else {
1565
Address container(Address::LOCAL_VARIABLE, add_local("@container_pos", p_list_type), p_list_type);
1566
1567
// Store state.
1568
for_container_variables.push_back(container);
1569
}
1570
}
1571
1572
void GDScriptByteCodeGenerator::write_for_list_assignment(const Address &p_list) {
1573
const Address &container = for_container_variables.back()->get();
1574
1575
// Assign container.
1576
append_opcode(GDScriptFunction::OPCODE_ASSIGN);
1577
append(container);
1578
append(p_list);
1579
}
1580
1581
void GDScriptByteCodeGenerator::write_for_range_assignment(const Address &p_from, const Address &p_to, const Address &p_step) {
1582
const Address &range_from = for_range_from_variables.back()->get();
1583
const Address &range_to = for_range_to_variables.back()->get();
1584
const Address &range_step = for_range_step_variables.back()->get();
1585
1586
// Assign range args.
1587
if (range_from.type == p_from.type) {
1588
write_assign(range_from, p_from);
1589
} else {
1590
write_assign_with_conversion(range_from, p_from);
1591
}
1592
if (range_to.type == p_to.type) {
1593
write_assign(range_to, p_to);
1594
} else {
1595
write_assign_with_conversion(range_to, p_to);
1596
}
1597
if (range_step.type == p_step.type) {
1598
write_assign(range_step, p_step);
1599
} else {
1600
write_assign_with_conversion(range_step, p_step);
1601
}
1602
}
1603
1604
void GDScriptByteCodeGenerator::write_for(const Address &p_variable, bool p_use_conversion, bool p_is_range) {
1605
const Address &counter = for_counter_variables.back()->get();
1606
const Address &container = p_is_range ? Address() : for_container_variables.back()->get();
1607
const Address &range_from = p_is_range ? for_range_from_variables.back()->get() : Address();
1608
const Address &range_to = p_is_range ? for_range_to_variables.back()->get() : Address();
1609
const Address &range_step = p_is_range ? for_range_step_variables.back()->get() : Address();
1610
1611
current_breaks_to_patch.push_back(List<int>());
1612
1613
GDScriptFunction::Opcode begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN;
1614
GDScriptFunction::Opcode iterate_opcode = GDScriptFunction::OPCODE_ITERATE;
1615
1616
if (p_is_range) {
1617
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_RANGE;
1618
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_RANGE;
1619
} else if (container.type.has_type()) {
1620
if (container.type.kind == GDScriptDataType::BUILTIN) {
1621
switch (container.type.builtin_type) {
1622
case Variant::INT:
1623
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_INT;
1624
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_INT;
1625
break;
1626
case Variant::FLOAT:
1627
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_FLOAT;
1628
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_FLOAT;
1629
break;
1630
case Variant::VECTOR2:
1631
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2;
1632
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2;
1633
break;
1634
case Variant::VECTOR2I:
1635
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2I;
1636
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2I;
1637
break;
1638
case Variant::VECTOR3:
1639
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3;
1640
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3;
1641
break;
1642
case Variant::VECTOR3I:
1643
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3I;
1644
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3I;
1645
break;
1646
case Variant::STRING:
1647
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_STRING;
1648
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_STRING;
1649
break;
1650
case Variant::DICTIONARY:
1651
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_DICTIONARY;
1652
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_DICTIONARY;
1653
break;
1654
case Variant::ARRAY:
1655
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_ARRAY;
1656
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_ARRAY;
1657
break;
1658
case Variant::PACKED_BYTE_ARRAY:
1659
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_BYTE_ARRAY;
1660
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_BYTE_ARRAY;
1661
break;
1662
case Variant::PACKED_INT32_ARRAY:
1663
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT32_ARRAY;
1664
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT32_ARRAY;
1665
break;
1666
case Variant::PACKED_INT64_ARRAY:
1667
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT64_ARRAY;
1668
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT64_ARRAY;
1669
break;
1670
case Variant::PACKED_FLOAT32_ARRAY:
1671
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT32_ARRAY;
1672
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT32_ARRAY;
1673
break;
1674
case Variant::PACKED_FLOAT64_ARRAY:
1675
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT64_ARRAY;
1676
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT64_ARRAY;
1677
break;
1678
case Variant::PACKED_STRING_ARRAY:
1679
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_STRING_ARRAY;
1680
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_STRING_ARRAY;
1681
break;
1682
case Variant::PACKED_VECTOR2_ARRAY:
1683
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR2_ARRAY;
1684
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR2_ARRAY;
1685
break;
1686
case Variant::PACKED_VECTOR3_ARRAY:
1687
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR3_ARRAY;
1688
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR3_ARRAY;
1689
break;
1690
case Variant::PACKED_COLOR_ARRAY:
1691
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_COLOR_ARRAY;
1692
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_COLOR_ARRAY;
1693
break;
1694
case Variant::PACKED_VECTOR4_ARRAY:
1695
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR4_ARRAY;
1696
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR4_ARRAY;
1697
break;
1698
default:
1699
break;
1700
}
1701
} else {
1702
begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_OBJECT;
1703
iterate_opcode = GDScriptFunction::OPCODE_ITERATE_OBJECT;
1704
}
1705
}
1706
1707
Address temp;
1708
if (p_use_conversion) {
1709
temp = Address(Address::LOCAL_VARIABLE, add_local("@iterator_temp", GDScriptDataType()));
1710
}
1711
1712
// Begin loop.
1713
append_opcode(begin_opcode);
1714
append(counter);
1715
if (p_is_range) {
1716
append(range_from);
1717
append(range_to);
1718
append(range_step);
1719
} else {
1720
append(container);
1721
}
1722
append(p_use_conversion ? temp : p_variable);
1723
for_jmp_addrs.push_back(opcodes.size());
1724
append(0); // End of loop address, will be patched.
1725
append_opcode(GDScriptFunction::OPCODE_JUMP);
1726
append(opcodes.size() + (p_is_range ? 7 : 6)); // Skip over 'continue' code.
1727
1728
// Next iteration.
1729
int continue_addr = opcodes.size();
1730
continue_addrs.push_back(continue_addr);
1731
append_opcode(iterate_opcode);
1732
append(counter);
1733
if (p_is_range) {
1734
append(range_to);
1735
append(range_step);
1736
} else {
1737
append(container);
1738
}
1739
append(p_use_conversion ? temp : p_variable);
1740
for_jmp_addrs.push_back(opcodes.size());
1741
append(0); // Jump destination, will be patched.
1742
1743
if (p_use_conversion) {
1744
write_assign_with_conversion(p_variable, temp);
1745
if (p_variable.type.can_contain_object()) {
1746
clear_address(temp); // Can contain `RefCounted`, so clear it.
1747
}
1748
}
1749
}
1750
1751
void GDScriptByteCodeGenerator::write_endfor(bool p_is_range) {
1752
// Jump back to loop check.
1753
append_opcode(GDScriptFunction::OPCODE_JUMP);
1754
append(continue_addrs.back()->get());
1755
continue_addrs.pop_back();
1756
1757
// Patch end jumps (two of them).
1758
for (int i = 0; i < 2; i++) {
1759
patch_jump(for_jmp_addrs.back()->get());
1760
for_jmp_addrs.pop_back();
1761
}
1762
1763
// Patch break statements.
1764
for (const int &E : current_breaks_to_patch.back()->get()) {
1765
patch_jump(E);
1766
}
1767
current_breaks_to_patch.pop_back();
1768
1769
// Pop state.
1770
for_counter_variables.pop_back();
1771
if (p_is_range) {
1772
for_range_from_variables.pop_back();
1773
for_range_to_variables.pop_back();
1774
for_range_step_variables.pop_back();
1775
} else {
1776
for_container_variables.pop_back();
1777
}
1778
}
1779
1780
void GDScriptByteCodeGenerator::start_while_condition() {
1781
current_breaks_to_patch.push_back(List<int>());
1782
continue_addrs.push_back(opcodes.size());
1783
}
1784
1785
void GDScriptByteCodeGenerator::write_while(const Address &p_condition) {
1786
// Condition check.
1787
append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
1788
append(p_condition);
1789
while_jmp_addrs.push_back(opcodes.size());
1790
append(0); // End of loop address, will be patched.
1791
}
1792
1793
void GDScriptByteCodeGenerator::write_endwhile() {
1794
// Jump back to loop check.
1795
append_opcode(GDScriptFunction::OPCODE_JUMP);
1796
append(continue_addrs.back()->get());
1797
continue_addrs.pop_back();
1798
1799
// Patch end jump.
1800
patch_jump(while_jmp_addrs.back()->get());
1801
while_jmp_addrs.pop_back();
1802
1803
// Patch break statements.
1804
for (const int &E : current_breaks_to_patch.back()->get()) {
1805
patch_jump(E);
1806
}
1807
current_breaks_to_patch.pop_back();
1808
}
1809
1810
void GDScriptByteCodeGenerator::write_break() {
1811
append_opcode(GDScriptFunction::OPCODE_JUMP);
1812
current_breaks_to_patch.back()->get().push_back(opcodes.size());
1813
append(0);
1814
}
1815
1816
void GDScriptByteCodeGenerator::write_continue() {
1817
append_opcode(GDScriptFunction::OPCODE_JUMP);
1818
append(continue_addrs.back()->get());
1819
}
1820
1821
void GDScriptByteCodeGenerator::write_breakpoint() {
1822
append_opcode(GDScriptFunction::OPCODE_BREAKPOINT);
1823
}
1824
1825
void GDScriptByteCodeGenerator::write_newline(int p_line) {
1826
if (GDScriptLanguage::get_singleton()->should_track_call_stack()) {
1827
// Add newline for debugger and stack tracking if enabled in the project settings.
1828
append_opcode(GDScriptFunction::OPCODE_LINE);
1829
append(p_line);
1830
current_line = p_line;
1831
}
1832
}
1833
1834
void GDScriptByteCodeGenerator::write_return(const Address &p_return_value) {
1835
if (!function->return_type.has_type() || p_return_value.type.has_type()) {
1836
// Either the function is untyped or the return value is also typed.
1837
1838
// If this is a typed function, then we need to check for potential conversions.
1839
if (function->return_type.has_type()) {
1840
if (function->return_type.kind == GDScriptDataType::BUILTIN && function->return_type.builtin_type == Variant::ARRAY && function->return_type.has_container_element_type(0)) {
1841
// Typed array.
1842
const GDScriptDataType &element_type = function->return_type.get_container_element_type(0);
1843
append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_ARRAY);
1844
append(p_return_value);
1845
append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
1846
append(element_type.builtin_type);
1847
append(element_type.native_type);
1848
} else if (function->return_type.kind == GDScriptDataType::BUILTIN && function->return_type.builtin_type == Variant::DICTIONARY &&
1849
function->return_type.has_container_element_types()) {
1850
// Typed dictionary.
1851
const GDScriptDataType &key_type = function->return_type.get_container_element_type_or_variant(0);
1852
const GDScriptDataType &value_type = function->return_type.get_container_element_type_or_variant(1);
1853
append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_DICTIONARY);
1854
append(p_return_value);
1855
append(get_constant_pos(key_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
1856
append(get_constant_pos(value_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
1857
append(key_type.builtin_type);
1858
append(key_type.native_type);
1859
append(value_type.builtin_type);
1860
append(value_type.native_type);
1861
} 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) {
1862
// Add conversion.
1863
append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_BUILTIN);
1864
append(p_return_value);
1865
append(function->return_type.builtin_type);
1866
} else {
1867
// Just assign.
1868
append_opcode(GDScriptFunction::OPCODE_RETURN);
1869
append(p_return_value);
1870
}
1871
} else {
1872
append_opcode(GDScriptFunction::OPCODE_RETURN);
1873
append(p_return_value);
1874
}
1875
} else {
1876
switch (function->return_type.kind) {
1877
case GDScriptDataType::BUILTIN: {
1878
if (function->return_type.builtin_type == Variant::ARRAY && function->return_type.has_container_element_type(0)) {
1879
const GDScriptDataType &element_type = function->return_type.get_container_element_type(0);
1880
append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_ARRAY);
1881
append(p_return_value);
1882
append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
1883
append(element_type.builtin_type);
1884
append(element_type.native_type);
1885
} else if (function->return_type.builtin_type == Variant::DICTIONARY && function->return_type.has_container_element_types()) {
1886
const GDScriptDataType &key_type = function->return_type.get_container_element_type_or_variant(0);
1887
const GDScriptDataType &value_type = function->return_type.get_container_element_type_or_variant(1);
1888
append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_DICTIONARY);
1889
append(p_return_value);
1890
append(get_constant_pos(key_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
1891
append(get_constant_pos(value_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
1892
append(key_type.builtin_type);
1893
append(key_type.native_type);
1894
append(value_type.builtin_type);
1895
append(value_type.native_type);
1896
} else {
1897
append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_BUILTIN);
1898
append(p_return_value);
1899
append(function->return_type.builtin_type);
1900
}
1901
} break;
1902
case GDScriptDataType::NATIVE: {
1903
append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_NATIVE);
1904
append(p_return_value);
1905
int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[function->return_type.native_type];
1906
Variant nc = GDScriptLanguage::get_singleton()->get_global_array()[class_idx];
1907
class_idx = get_constant_pos(nc) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
1908
append(class_idx);
1909
} break;
1910
case GDScriptDataType::GDSCRIPT:
1911
case GDScriptDataType::SCRIPT: {
1912
Variant script = function->return_type.script_type;
1913
int script_idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
1914
1915
append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_SCRIPT);
1916
append(p_return_value);
1917
append(script_idx);
1918
} break;
1919
default: {
1920
ERR_PRINT("Compiler bug: unresolved return.");
1921
1922
// Shouldn't get here, but fail-safe to a regular return;
1923
append_opcode(GDScriptFunction::OPCODE_RETURN);
1924
append(p_return_value);
1925
} break;
1926
}
1927
}
1928
}
1929
1930
void GDScriptByteCodeGenerator::write_assert(const Address &p_test, const Address &p_message) {
1931
append_opcode(GDScriptFunction::OPCODE_ASSERT);
1932
append(p_test);
1933
append(p_message);
1934
}
1935
1936
void GDScriptByteCodeGenerator::start_block() {
1937
push_stack_identifiers();
1938
}
1939
1940
void GDScriptByteCodeGenerator::end_block() {
1941
pop_stack_identifiers();
1942
}
1943
1944
void GDScriptByteCodeGenerator::clear_temporaries() {
1945
for (int slot_idx : temporaries_pending_clear) {
1946
// The temporary may have been reused as something else since it was added to the list.
1947
// In that case, there's **no** need to clear it.
1948
if (temporaries[slot_idx].can_contain_object) {
1949
clear_address(Address(Address::TEMPORARY, slot_idx)); // Can contain `RefCounted`, so clear it.
1950
}
1951
}
1952
temporaries_pending_clear.clear();
1953
}
1954
1955
void GDScriptByteCodeGenerator::clear_address(const Address &p_address) {
1956
// Do not check `is_local_dirty()` here! Always clear the address since the codegen doesn't track the compiler.
1957
// Also, this method is used to initialize local variables of built-in types, since they cannot be `null`.
1958
1959
if (p_address.type.kind == GDScriptDataType::BUILTIN) {
1960
switch (p_address.type.builtin_type) {
1961
case Variant::BOOL:
1962
write_assign_false(p_address);
1963
break;
1964
case Variant::DICTIONARY:
1965
if (p_address.type.has_container_element_types()) {
1966
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>());
1967
} else {
1968
write_construct(p_address, p_address.type.builtin_type, Vector<GDScriptCodeGenerator::Address>());
1969
}
1970
break;
1971
case Variant::ARRAY:
1972
if (p_address.type.has_container_element_type(0)) {
1973
write_construct_typed_array(p_address, p_address.type.get_container_element_type(0), Vector<GDScriptCodeGenerator::Address>());
1974
} else {
1975
write_construct(p_address, p_address.type.builtin_type, Vector<GDScriptCodeGenerator::Address>());
1976
}
1977
break;
1978
case Variant::NIL:
1979
case Variant::OBJECT:
1980
write_assign_null(p_address);
1981
break;
1982
default:
1983
write_construct(p_address, p_address.type.builtin_type, Vector<GDScriptCodeGenerator::Address>());
1984
break;
1985
}
1986
} else {
1987
write_assign_null(p_address);
1988
}
1989
1990
if (p_address.mode == Address::LOCAL_VARIABLE) {
1991
dirty_locals.erase(p_address.address);
1992
}
1993
}
1994
1995
// Returns `true` if the local has been reused and not cleaned up with `clear_address()`.
1996
bool GDScriptByteCodeGenerator::is_local_dirty(const Address &p_address) const {
1997
ERR_FAIL_COND_V(p_address.mode != Address::LOCAL_VARIABLE, false);
1998
return dirty_locals.has(p_address.address);
1999
}
2000
2001
GDScriptByteCodeGenerator::~GDScriptByteCodeGenerator() {
2002
if (!ended && function != nullptr) {
2003
memdelete(function);
2004
}
2005
}
2006
2007