• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ArkScript-lang / Ark / 24088572670

07 Apr 2026 03:04PM UTC coverage: 93.945% (+0.1%) from 93.837%
24088572670

push

github

SuperFola
refactor(vm): move the instruction eval loop to a template private VM::unsafeRun, introduce a safeRun that handles exceptions when calling unsafeRun

909 of 948 new or added lines in 3 files covered. (95.89%)

82 existing lines in 3 files now uncovered.

9868 of 10504 relevant lines covered (93.95%)

648642.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.12
/src/arkreactor/VM/VM.cpp
1
#include <Ark/VM/VM.hpp>
2

3
#include <utility>
4
#include <numeric>
5
#include <fmt/core.h>
6
#include <fmt/color.h>
7
#include <fmt/ostream.h>
8

9
#include <Ark/Utils/Files.hpp>
10
#include <Ark/Utils/Utils.hpp>
11
#include <Ark/Error/Diagnostics.hpp>
12
#include <Ark/TypeChecker.hpp>
13
#include <Ark/VM/ModuleMapping.hpp>
14
#include <Ark/Compiler/Instructions.hpp>
15
#include <Ark/VM/Value/Dict.hpp>
16
#include <Ark/VM/Helpers.hpp>
17

18
namespace Ark
19
{
20
    using namespace internal;
21

22
    VM::VM(State& state) noexcept :
930✔
23
        m_state(state), m_exit_code(0), m_running(false)
310✔
24
    {
310✔
25
        m_execution_contexts.emplace_back(std::make_unique<ExecutionContext>());
310✔
26
    }
310✔
27

28
    void VM::init() noexcept
304✔
29
    {
304✔
30
        ExecutionContext& context = *m_execution_contexts.back();
304✔
31
        for (const auto& c : m_execution_contexts)
608✔
32
        {
33
            c->ip = 0;
304✔
34
            c->pp = 0;
304✔
35
            c->sp = 0;
304✔
36
        }
304✔
37

38
        context.sp = 0;
304✔
39
        context.fc = 1;
304✔
40

41
        m_shared_lib_objects.clear();
304✔
42
        context.stacked_closure_scopes.clear();
304✔
43
        context.stacked_closure_scopes.emplace_back(nullptr);
304✔
44

45
        context.saved_scope.reset();
304✔
46
        m_exit_code = 0;
304✔
47

48
        context.locals.clear();
304✔
49
        context.locals.reserve(128);
304✔
50
        context.locals.emplace_back(context.scopes_storage.data(), 0);
304✔
51

52
        // loading bound stuff
53
        // put them in the global frame if we can, aka the first one
54
        for (const auto& [sym_id, value] : m_state.m_bound)
957✔
55
        {
56
            auto it = std::ranges::find(m_state.m_symbols, sym_id);
623✔
57
            if (it != m_state.m_symbols.end())
623✔
58
                context.locals[0].pushBack(static_cast<uint16_t>(std::distance(m_state.m_symbols.begin(), it)), value);
30✔
59
        }
623✔
60
    }
304✔
61

62
    Value VM::getField(Value* closure, const uint16_t id, const ExecutionContext& context, const bool push_with_env)
4,615✔
63
    {
4,615✔
64
        if (closure->valueType() != ValueType::Closure)
4,615✔
65
        {
66
            if (context.last_symbol < m_state.m_symbols.size()) [[likely]]
1✔
67
                throwVMError(
2✔
68
                    ErrorKind::Type,
69
                    fmt::format(
3✔
70
                        "`{}' is a {}, not a Closure, can not get the field `{}' from it",
1✔
71
                        m_state.m_symbols[context.last_symbol],
1✔
72
                        std::to_string(closure->valueType()),
1✔
73
                        m_state.m_symbols[id]));
1✔
74
            else
75
                throwVMError(
×
76
                    ErrorKind::Type,
77
                    fmt::format(
×
78
                        "{} is not a Closure, can not get the field `{}' from it",
×
79
                        std::to_string(closure->valueType()),
×
80
                        m_state.m_symbols[id]));
×
81
        }
82

83
        if (Value* field = closure->refClosure().refScope()[id]; field != nullptr)
9,228✔
84
        {
85
            if (push_with_env)
4,613✔
86
                return Value(Closure(closure->refClosure().scopePtr(), field->pageAddr()));
2,583✔
87
            else
88
                return *field;
2,030✔
89
        }
90
        else
91
        {
92
            if (!closure->refClosure().hasFieldEndingWith(m_state.m_symbols[id], *this))
1✔
93
                throwVMError(
1✔
94
                    ErrorKind::Scope,
95
                    fmt::format(
2✔
96
                        "`{0}' isn't in the closure environment: {1}",
1✔
97
                        m_state.m_symbols[id],
1✔
98
                        closure->refClosure().toString(*this)));
1✔
99
            throwVMError(
×
100
                ErrorKind::Scope,
101
                fmt::format(
×
102
                    "`{0}' isn't in the closure environment: {1}. A variable in the package might have the same name as '{0}', "
×
103
                    "and name resolution tried to fully qualify it. Rename either the variable or the capture to solve this",
104
                    m_state.m_symbols[id],
×
105
                    closure->refClosure().toString(*this)));
×
106
        }
107
    }
4,615✔
108

109
    Value VM::createList(const std::size_t count, ExecutionContext& context)
5,608✔
110
    {
5,608✔
111
        Value l(ValueType::List);
5,608✔
112
        if (count != 0)
5,608✔
113
            l.list().reserve(count);
2,120✔
114

115
        for (std::size_t i = 0; i < count; ++i)
11,226✔
116
            l.push_back(*popAndResolveAsPtr(context));
5,618✔
117

118
        return l;
5,608✔
119
    }
5,608✔
120

121
    void VM::listAppendInPlace(Value* list, const std::size_t count, ExecutionContext& context)
21,927✔
122
    {
21,927✔
123
        if (list->valueType() != ValueType::List)
21,927✔
124
        {
125
            std::vector<Value> args = { *list };
1✔
126
            for (std::size_t i = 0; i < count; ++i)
2✔
127
                args.push_back(*popAndResolveAsPtr(context));
1✔
128
            throw types::TypeCheckingError(
2✔
129
                "append!",
1✔
130
                { { types::Contract { { types::Typedef("list", ValueType::List), types::Typedef("value", ValueType::Any, /* is_variadic= */ true) } } } },
1✔
131
                args);
132
        }
1✔
133

134
        for (std::size_t i = 0; i < count; ++i)
43,856✔
135
            list->push_back(*popAndResolveAsPtr(context));
21,930✔
136
    }
21,927✔
137

138
    Value& VM::operator[](const std::string& name) noexcept
36✔
139
    {
36✔
140
        // find id of object
141
        const auto it = std::ranges::find(m_state.m_symbols, name);
36✔
142
        if (it == m_state.m_symbols.end())
36✔
143
        {
144
            m_no_value = Builtins::nil;
1✔
145
            return m_no_value;
1✔
146
        }
147

148
        const auto dist = std::distance(m_state.m_symbols.begin(), it);
35✔
149
        if (std::cmp_less(dist, MaxValue16Bits))
35✔
150
        {
151
            ExecutionContext& context = *m_execution_contexts.front();
35✔
152

153
            const auto id = static_cast<uint16_t>(dist);
35✔
154
            Value* var = findNearestVariable(id, context);
35✔
155
            if (var != nullptr)
35✔
156
                return *var;
35✔
157
        }
35✔
158

159
        m_no_value = Builtins::nil;
×
160
        return m_no_value;
×
161
    }
36✔
162

163
    void VM::loadPlugin(const uint16_t id, ExecutionContext& context)
1✔
164
    {
1✔
165
        namespace fs = std::filesystem;
166

167
        const std::string file = m_state.m_constants[id].stringRef();
1✔
168

169
        std::string path = file;
1✔
170
        // bytecode loaded from file
171
        if (m_state.m_filename != ARK_NO_NAME_FILE)
1✔
172
            path = (fs::path(m_state.m_filename).parent_path() / fs::path(file)).relative_path().string();
1✔
173

174
        std::shared_ptr<SharedLibrary> lib;
1✔
175
        // if it exists alongside the .arkc file
176
        if (Utils::fileExists(path))
1✔
177
            lib = std::make_shared<SharedLibrary>(path);
×
178
        else
179
        {
180
            for (auto const& v : m_state.m_libenv)
3✔
181
            {
182
                std::string lib_path = (fs::path(v) / fs::path(file)).string();
2✔
183

184
                // if it's already loaded don't do anything
185
                if (std::ranges::find_if(m_shared_lib_objects, [&](const auto& val) {
2✔
186
                        return (val->path() == path || val->path() == lib_path);
×
187
                    }) != m_shared_lib_objects.end())
2✔
188
                    return;
310✔
189

190
                // check in lib_path
191
                if (Utils::fileExists(lib_path))
312✔
192
                {
193
                    lib = std::make_shared<SharedLibrary>(lib_path);
1✔
194
                    break;
1✔
195
                }
196
            }
2✔
197
        }
198

199
        if (!lib)
1✔
200
        {
201
            auto lib_path = std::accumulate(
×
202
                std::next(m_state.m_libenv.begin()),
×
203
                m_state.m_libenv.end(),
×
204
                m_state.m_libenv[0].string(),
×
205
                [](const std::string& a, const fs::path& b) -> std::string {
×
206
                    return a + "\n\t- " + b.string();
×
207
                });
×
208
            throwVMError(
×
209
                ErrorKind::Module,
210
                fmt::format("Could not find module '{}'. Searched under\n\t- {}\n\t- {}", file, path, lib_path));
×
211
        }
×
212

213
        m_shared_lib_objects.emplace_back(lib);
1✔
214

215
        // load the mapping from the dynamic library
216
        try
217
        {
218
            std::vector<ScopeView::pair_t> data;
1✔
219
            const mapping* map = m_shared_lib_objects.back()->get<mapping* (*)()>("getFunctionsMapping")();
1✔
220

221
            std::size_t i = 0;
1✔
222
            while (map[i].name != nullptr)
2✔
223
            {
224
                const auto it = std::ranges::find(m_state.m_symbols, std::string(map[i].name));
1✔
225
                if (it != m_state.m_symbols.end())
1✔
226
                    data.emplace_back(static_cast<uint16_t>(std::distance(m_state.m_symbols.begin(), it)), Value(map[i].value));
1✔
227

228
                ++i;
1✔
229
            }
1✔
230

231
            context.locals.back().insertFront(data);
1✔
232
        }
1✔
233
        catch (const std::system_error& e)
234
        {
235
            throwVMError(
×
236
                ErrorKind::Module,
237
                fmt::format(
×
238
                    "An error occurred while loading module '{}': {}\nIt is most likely because the versions of the module and the language don't match.",
×
239
                    file, e.what()));
×
240
        }
1✔
241
    }
1✔
242

243
    void VM::exit(const int code) noexcept
×
244
    {
×
245
        m_exit_code = code;
×
246
        m_running = false;
×
247
    }
×
248

249
    ExecutionContext* VM::createAndGetContext()
17✔
250
    {
17✔
251
        const std::lock_guard lock(m_mutex);
17✔
252

253
        ExecutionContext* ctx = nullptr;
17✔
254

255
        // Try and find a free execution context.
256
        // If there is only one context, this is the primary one, which can't be reused.
257
        // Otherwise, we can check if a context is marked as free and reserve it!
258
        // It is possible that all contexts are being used, thus we will create one (active by default) in that case.
259

260
        if (m_execution_contexts.size() > 1)
17✔
261
        {
262
            const auto it = std::ranges::find_if(
28✔
263
                m_execution_contexts,
14✔
264
                [](const std::unique_ptr<ExecutionContext>& context) -> bool {
38✔
265
                    return !context->primary && context->isFree();
38✔
266
                });
267

268
            if (it != m_execution_contexts.end())
14✔
269
            {
270
                ctx = it->get();
10✔
271
                ctx->setActive(true);
10✔
272
                // reset the context before using it
273
                ctx->sp = 0;
10✔
274
                ctx->saved_scope.reset();
10✔
275
                ctx->stacked_closure_scopes.clear();
10✔
276
                ctx->locals.clear();
10✔
277
            }
10✔
278
        }
14✔
279

280
        if (ctx == nullptr)
17✔
281
            ctx = m_execution_contexts.emplace_back(std::make_unique<ExecutionContext>()).get();
7✔
282

283
        assert(!ctx->primary && "The new context shouldn't be marked as primary!");
17✔
284
        assert(ctx != m_execution_contexts.front().get() && "The new context isn't really new!");
17✔
285

286
        const ExecutionContext& primary_ctx = *m_execution_contexts.front();
17✔
287
        ctx->locals.reserve(primary_ctx.locals.size());
17✔
288
        ctx->scopes_storage = primary_ctx.scopes_storage;
17✔
289
        ctx->stacked_closure_scopes.emplace_back(nullptr);
17✔
290
        ctx->fc = 1;
17✔
291

292
        for (const auto& scope_view : primary_ctx.locals)
62✔
293
        {
294
            auto& new_scope = ctx->locals.emplace_back(ctx->scopes_storage.data(), scope_view.m_start);
45✔
295
            for (std::size_t i = 0; i < scope_view.size(); ++i)
3,286✔
296
            {
297
                const auto& [id, val] = scope_view.atPos(i);
3,241✔
298
                new_scope.pushBack(id, val);
3,241✔
299
            }
3,241✔
300
        }
45✔
301

302
        return ctx;
17✔
303
    }
17✔
304

305
    void VM::deleteContext(ExecutionContext* ec)
16✔
306
    {
16✔
307
        const std::lock_guard lock(m_mutex);
16✔
308

309
        // 1 + 4 additional contexts, it's a bit much (~600kB per context) to have in memory
310
        if (m_execution_contexts.size() > 5)
16✔
311
        {
312
            const auto it =
1✔
313
                std::ranges::remove_if(
2✔
314
                    m_execution_contexts,
1✔
315
                    [ec](const std::unique_ptr<ExecutionContext>& ctx) {
7✔
316
                        return ctx.get() == ec;
6✔
317
                    })
318
                    .begin();
1✔
319
            m_execution_contexts.erase(it);
1✔
320
        }
1✔
321
        else
322
        {
323
            // mark the used context as ready to be used again
324
            for (std::size_t i = 1; i < m_execution_contexts.size(); ++i)
40✔
325
            {
326
                if (m_execution_contexts[i].get() == ec)
25✔
327
                {
328
                    ec->setActive(false);
15✔
329
                    break;
15✔
330
                }
331
            }
10✔
332
        }
333
    }
16✔
334

335
    Future* VM::createFuture(std::vector<Value>& args)
17✔
336
    {
17✔
337
        const std::lock_guard lock(m_mutex_futures);
17✔
338

339
        ExecutionContext* ctx = createAndGetContext();
17✔
340
        // so that we have access to the presumed symbol id of the function we are calling
341
        // assuming that the callee is always the global context
342
        ctx->last_symbol = m_execution_contexts.front()->last_symbol;
17✔
343

344
        m_futures.push_back(std::make_unique<Future>(ctx, this, args));
17✔
345
        return m_futures.back().get();
17✔
346
    }
17✔
347

348
    void VM::deleteFuture(Future* f)
1✔
349
    {
1✔
350
        const std::lock_guard lock(m_mutex_futures);
1✔
351

352
        std::erase_if(
1✔
353
            m_futures,
1✔
354
            [f](const std::unique_ptr<Future>& future) {
3✔
355
                return future.get() == f;
2✔
356
            });
357
    }
1✔
358

359
    bool VM::forceReloadPlugins() const
×
360
    {
×
361
        // load the mapping from the dynamic library
362
        try
363
        {
364
            for (const auto& shared_lib : m_shared_lib_objects)
×
365
            {
366
                const mapping* map = shared_lib->get<mapping* (*)()>("getFunctionsMapping")();
×
367
                // load the mapping data
368
                std::size_t i = 0;
×
369
                while (map[i].name != nullptr)
×
370
                {
371
                    // put it in the global frame, aka the first one
372
                    auto it = std::ranges::find(m_state.m_symbols, std::string(map[i].name));
×
373
                    if (it != m_state.m_symbols.end())
×
374
                        m_execution_contexts[0]->locals[0].pushBack(
×
375
                            static_cast<uint16_t>(std::distance(m_state.m_symbols.begin(), it)),
×
376
                            Value(map[i].value));
×
377

378
                    ++i;
×
379
                }
×
380
            }
×
381

382
            return true;
×
383
        }
×
384
        catch (const std::system_error&)
385
        {
386
            return false;
×
387
        }
×
388
    }
×
389

390
    void VM::usePromptFileForDebugger(const std::string& path, std::ostream& os)
7✔
391
    {
7✔
392
        m_debugger = std::make_unique<Debugger>(m_state.m_libenv, path, os, m_state.m_symbols, m_state.m_constants);
7✔
393
    }
7✔
394

395
    void VM::throwVMError(ErrorKind kind, const std::string& message)
40✔
396
    {
40✔
397
        throw std::runtime_error(std::string(errorKinds[static_cast<std::size_t>(kind)]) + ": " + message + "\n");
40✔
398
    }
40✔
399

400
    int VM::run(const bool fail_with_exception)
304✔
401
    {
304✔
402
        init();
304✔
403
        safeRun(*m_execution_contexts[0], 0, fail_with_exception);
304✔
404
        return m_exit_code;
304✔
405
    }
406

407
    int VM::safeRun(ExecutionContext& context, const std::size_t untilFrameCount, const bool fail_with_exception)
335✔
408
    {
335✔
409
        m_running = true;
335✔
410

411
        try
412
        {
413
            if (m_state.m_features & FeatureVMDebugger)
335✔
414
                unsafeRun<true>(context, untilFrameCount);
20✔
415
            else
416
                unsafeRun<false>(context, untilFrameCount);
315✔
417
        }
335✔
418
        catch (const Error& e)
419
        {
420
            if (fail_with_exception)
146✔
421
            {
422
                std::stringstream stream;
145✔
423
                backtrace(context, stream, /* colorize= */ false);
145✔
424
                // It's important we have an Ark::Error here, as the constructor for NestedError
425
                // does more than just aggregate error messages, hence the code duplication.
426
                throw NestedError(e, stream.str(), *this);
145✔
427
            }
145✔
428
            showBacktraceWithException(Error(e.details(/* colorize= */ true, *this)), context);
1✔
429
        }
269✔
430
        catch (const std::exception& e)
431
        {
432
            if (fail_with_exception)
66✔
433
            {
434
                std::stringstream stream;
66✔
435
                backtrace(context, stream, /* colorize= */ false);
66✔
436
                throw NestedError(e, stream.str());
66✔
437
            }
66✔
NEW
438
            showBacktraceWithException(e, context);
×
439
        }
212✔
440
        catch (...)
441
        {
NEW
442
            if (fail_with_exception)
×
NEW
443
                throw;
×
444

445
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
446
            throw;
447
#endif
NEW
448
            fmt::println("Unknown error");
×
NEW
449
            backtrace(context);
×
NEW
450
            m_exit_code = 1;
×
451
        }
277✔
452

453
        return m_exit_code;
124✔
454
    }
423✔
455

456
    template <bool WithDebugger>
457
    void VM::unsafeRun(ExecutionContext& context, const std::size_t untilFrameCount)
424✔
458
    {
424✔
459
#if ARK_USE_COMPUTED_GOTOS
460
#    define TARGET(op) TARGET_##op:
461
#    define DISPATCH_GOTO()            \
462
        _Pragma("GCC diagnostic push") \
463
            _Pragma("GCC diagnostic ignored \"-Wpedantic\"") goto* opcode_targets[inst];
464
        _Pragma("GCC diagnostic pop")
465
#    define GOTO_HALT() goto dispatch_end
466
#else
467
#    define TARGET(op) case op:
468
#    define DISPATCH_GOTO() goto dispatch_opcode
469
#    define GOTO_HALT() break
470
#endif
471

472
#define FETCH_NEXT_INSTRUCTION()                                                                                                  \
473
    do                                                                                                                            \
474
    {                                                                                                                             \
475
        inst = m_state.inst(context.pp, context.ip);                                                                              \
476
        padding = m_state.inst(context.pp, context.ip + 1);                                                                       \
477
        arg = static_cast<uint16_t>((m_state.inst(context.pp, context.ip + 2) << 8) +                                             \
478
                                    m_state.inst(context.pp, context.ip + 3));                                                    \
479
        context.ip += 4;                                                                                                          \
480
        context.inst_exec_counter = (context.inst_exec_counter + 1) % VMOverflowBufferSize;                                       \
481
        if constexpr (WithDebugger)                                                                                               \
482
        {                                                                                                                         \
483
            if (!m_debugger) initDebugger(context);                                                                               \
484
            m_debugger->registerInstruction(static_cast<uint32_t>((inst << 24) | (padding << 16) | arg));                         \
485
        }                                                                                                                         \
486
        if (context.inst_exec_counter < 2 && context.sp >= VMStackSize)                                                           \
487
        {                                                                                                                         \
488
            if (context.pp != 0)                                                                                                  \
489
                throw Error("Stack overflow. You could consider rewriting your function to make use of tail-call optimization."); \
490
            else                                                                                                                  \
491
                throw Error("Stack overflow. Are you trying to call a function with too many arguments?");                        \
492
        }                                                                                                                         \
493
    } while (false)
494
#define DISPATCH()            \
495
    FETCH_NEXT_INSTRUCTION(); \
496
    DISPATCH_GOTO();
497
#define UNPACK_ARGS()                                                                 \
498
    do                                                                                \
499
    {                                                                                 \
500
        secondary_arg = static_cast<uint16_t>((padding << 4) | (arg & 0xf000) >> 12); \
501
        primary_arg = arg & 0x0fff;                                                   \
502
    } while (false)
503

504
#if ARK_USE_COMPUTED_GOTOS
505
#    pragma GCC diagnostic push
506
#    pragma GCC diagnostic ignored "-Wpedantic"
507
            constexpr std::array opcode_targets = {
424✔
508
                // cppcheck-suppress syntaxError ; cppcheck do not know about labels addresses (GCC extension)
509
                &&TARGET_NOP,
510
                &&TARGET_LOAD_FAST,
511
                &&TARGET_LOAD_FAST_BY_INDEX,
512
                &&TARGET_LOAD_SYMBOL,
513
                &&TARGET_LOAD_CONST,
514
                &&TARGET_POP_JUMP_IF_TRUE,
515
                &&TARGET_STORE,
516
                &&TARGET_STORE_REF,
517
                &&TARGET_SET_VAL,
518
                &&TARGET_POP_JUMP_IF_FALSE,
519
                &&TARGET_JUMP,
520
                &&TARGET_RET,
521
                &&TARGET_HALT,
522
                &&TARGET_PUSH_RETURN_ADDRESS,
523
                &&TARGET_CALL,
524
                &&TARGET_TAIL_CALL_SELF,
525
                &&TARGET_CAPTURE,
526
                &&TARGET_RENAME_NEXT_CAPTURE,
527
                &&TARGET_BUILTIN,
528
                &&TARGET_DEL,
529
                &&TARGET_MAKE_CLOSURE,
530
                &&TARGET_GET_FIELD,
531
                &&TARGET_GET_FIELD_AS_CLOSURE,
532
                &&TARGET_PLUGIN,
533
                &&TARGET_LIST,
534
                &&TARGET_APPEND,
535
                &&TARGET_CONCAT,
536
                &&TARGET_APPEND_IN_PLACE,
537
                &&TARGET_CONCAT_IN_PLACE,
538
                &&TARGET_POP_LIST,
539
                &&TARGET_POP_LIST_IN_PLACE,
540
                &&TARGET_SET_AT_INDEX,
541
                &&TARGET_SET_AT_2_INDEX,
542
                &&TARGET_POP,
543
                &&TARGET_SHORTCIRCUIT_AND,
544
                &&TARGET_SHORTCIRCUIT_OR,
545
                &&TARGET_CREATE_SCOPE,
546
                &&TARGET_RESET_SCOPE_JUMP,
547
                &&TARGET_POP_SCOPE,
548
                &&TARGET_APPLY,
549
                &&TARGET_BREAKPOINT,
550
                &&TARGET_ADD,
551
                &&TARGET_SUB,
552
                &&TARGET_MUL,
553
                &&TARGET_DIV,
554
                &&TARGET_GT,
555
                &&TARGET_LT,
556
                &&TARGET_LE,
557
                &&TARGET_GE,
558
                &&TARGET_NEQ,
559
                &&TARGET_EQ,
560
                &&TARGET_LEN,
561
                &&TARGET_IS_EMPTY,
562
                &&TARGET_TAIL,
563
                &&TARGET_HEAD,
564
                &&TARGET_IS_NIL,
565
                &&TARGET_TO_NUM,
566
                &&TARGET_TO_STR,
567
                &&TARGET_AT,
568
                &&TARGET_AT_AT,
569
                &&TARGET_MOD,
570
                &&TARGET_TYPE,
571
                &&TARGET_HAS_FIELD,
572
                &&TARGET_NOT,
573
                &&TARGET_LOAD_CONST_LOAD_CONST,
574
                &&TARGET_LOAD_CONST_STORE,
575
                &&TARGET_LOAD_CONST_SET_VAL,
576
                &&TARGET_STORE_FROM,
577
                &&TARGET_STORE_FROM_INDEX,
578
                &&TARGET_SET_VAL_FROM,
579
                &&TARGET_SET_VAL_FROM_INDEX,
580
                &&TARGET_INCREMENT,
581
                &&TARGET_INCREMENT_BY_INDEX,
582
                &&TARGET_INCREMENT_STORE,
583
                &&TARGET_DECREMENT,
584
                &&TARGET_DECREMENT_BY_INDEX,
585
                &&TARGET_DECREMENT_STORE,
586
                &&TARGET_STORE_TAIL,
587
                &&TARGET_STORE_TAIL_BY_INDEX,
588
                &&TARGET_STORE_HEAD,
589
                &&TARGET_STORE_HEAD_BY_INDEX,
590
                &&TARGET_STORE_LIST,
591
                &&TARGET_SET_VAL_TAIL,
592
                &&TARGET_SET_VAL_TAIL_BY_INDEX,
593
                &&TARGET_SET_VAL_HEAD,
594
                &&TARGET_SET_VAL_HEAD_BY_INDEX,
595
                &&TARGET_CALL_BUILTIN,
596
                &&TARGET_CALL_BUILTIN_WITHOUT_RETURN_ADDRESS,
597
                &&TARGET_LT_CONST_JUMP_IF_FALSE,
598
                &&TARGET_LT_CONST_JUMP_IF_TRUE,
599
                &&TARGET_LT_SYM_JUMP_IF_FALSE,
600
                &&TARGET_GT_CONST_JUMP_IF_TRUE,
601
                &&TARGET_GT_CONST_JUMP_IF_FALSE,
602
                &&TARGET_GT_SYM_JUMP_IF_FALSE,
603
                &&TARGET_EQ_CONST_JUMP_IF_TRUE,
604
                &&TARGET_EQ_SYM_INDEX_JUMP_IF_TRUE,
605
                &&TARGET_NEQ_CONST_JUMP_IF_TRUE,
606
                &&TARGET_NEQ_SYM_JUMP_IF_FALSE,
607
                &&TARGET_CALL_SYMBOL,
608
                &&TARGET_CALL_SYMBOL_BY_INDEX,
609
                &&TARGET_CALL_CURRENT_PAGE,
610
                &&TARGET_GET_FIELD_FROM_SYMBOL,
611
                &&TARGET_GET_FIELD_FROM_SYMBOL_INDEX,
612
                &&TARGET_AT_SYM_SYM,
613
                &&TARGET_AT_SYM_INDEX_SYM_INDEX,
614
                &&TARGET_AT_SYM_INDEX_CONST,
615
                &&TARGET_CHECK_TYPE_OF,
616
                &&TARGET_CHECK_TYPE_OF_BY_INDEX,
617
                &&TARGET_APPEND_IN_PLACE_SYM,
618
                &&TARGET_APPEND_IN_PLACE_SYM_INDEX,
619
                &&TARGET_STORE_LEN,
620
                &&TARGET_LT_LEN_SYM_JUMP_IF_FALSE,
621
                &&TARGET_MUL_BY,
622
                &&TARGET_MUL_BY_INDEX,
623
                &&TARGET_MUL_SET_VAL,
624
                &&TARGET_FUSED_MATH
625
            };
626

627
        static_assert(opcode_targets.size() == static_cast<std::size_t>(Instruction::InstructionsCount) && "Some instructions are not implemented in the VM");
628
#    pragma GCC diagnostic pop
629
#endif
630

631
        uint8_t inst = 0;
424✔
632
        uint8_t padding = 0;
424✔
633
        uint16_t arg = 0;
424✔
634
        uint16_t primary_arg = 0;
424✔
635
        uint16_t secondary_arg = 0;
424✔
636

637
        DISPATCH();
519✔
638
        // cppcheck-suppress unreachableCode ; analysis cannot follow the chain of goto... but it works!
639
        {
640
#if !ARK_USE_COMPUTED_GOTOS
641
        dispatch_opcode:
642
            switch (inst)
643
#endif
NEW
644
            {
×
645
#pragma region "Instructions"
646
                TARGET(NOP)
647
                {
NEW
648
                    DISPATCH();
×
649
                }
7,322,780✔
650

651
                TARGET(LOAD_FAST)
652
                {
653
                    push(loadSymbol(arg, context), context);
7,322,780✔
654
                    DISPATCH();
7,322,780✔
655
                }
370,398✔
656

657
                TARGET(LOAD_FAST_BY_INDEX)
658
                {
659
                    push(loadSymbolFromIndex(arg, context), context);
370,398✔
660
                    DISPATCH();
370,398✔
661
                }
4,830✔
662

663
                TARGET(LOAD_SYMBOL)
664
                {
665
                    // force resolving the reference
666
                    push(*loadSymbol(arg, context), context);
4,830✔
667
                    DISPATCH();
4,830✔
668
                }
660,872✔
669

670
                TARGET(LOAD_CONST)
671
                {
672
                    push(loadConstAsPtr(arg), context);
660,872✔
673
                    DISPATCH();
660,872✔
674
                }
44,555✔
675

676
                TARGET(POP_JUMP_IF_TRUE)
677
                {
678
                    if (Value boolean = *popAndResolveAsPtr(context); !!boolean)
57,661✔
679
                        jump(arg, context);
13,106✔
680
                    DISPATCH();
44,555✔
681
                }
426,153✔
682

683
                TARGET(STORE)
684
                {
685
                    store(arg, popAndResolveAsPtr(context), context);
426,153✔
686
                    DISPATCH();
426,153✔
687
                }
2,952✔
688

689
                TARGET(STORE_REF)
690
                {
691
                    // Not resolving a potential ref is on purpose!
692
                    // This instruction is only used by functions when storing arguments
693
                    const Value* tmp = pop(context);
2,952✔
694
                    store(arg, tmp, context);
2,952✔
695
                    DISPATCH();
2,952✔
696
                }
553,117✔
697

698
                TARGET(SET_VAL)
699
                {
700
                    setVal(arg, popAndResolveAsPtr(context), context);
553,117✔
701
                    DISPATCH();
553,117✔
702
                }
1,035,529✔
703

704
                TARGET(POP_JUMP_IF_FALSE)
705
                {
706
                    if (Value boolean = *popAndResolveAsPtr(context); !boolean)
1,040,283✔
707
                        jump(arg, context);
4,754✔
708
                    DISPATCH();
1,035,529✔
709
                }
620,944✔
710

711
                TARGET(JUMP)
712
                {
713
                    jump(arg, context);
620,944✔
714
                    DISPATCH();
620,944✔
715
                }
160,736✔
716

717
                TARGET(RET)
718
                {
719
                    {
720
                        Value ts = *popAndResolveAsPtr(context);
160,736✔
721
                        Value ts1 = *popAndResolveAsPtr(context);
160,736✔
722

723
                        if (ts1.valueType() == ValueType::InstPtr)
160,736✔
724
                        {
725
                            context.ip = ts1.pageAddr();
157,901✔
726
                            // we always push PP then IP, thus the next value
727
                            // MUST be the page pointer
728
                            context.pp = pop(context)->pageAddr();
157,901✔
729

730
                            returnFromFuncCall(context);
157,901✔
731
                            if (ts.valueType() == ValueType::Garbage)
157,901✔
UNCOV
732
                                push(Builtins::nil, context);
×
733
                            else
734
                                push(std::move(ts), context);
157,901✔
735
                        }
157,901✔
736
                        else if (ts1.valueType() == ValueType::Garbage)
2,835✔
737
                        {
738
                            const Value* ip = pop(context);
2,722✔
739
                            assert(ip->valueType() == ValueType::InstPtr && "Expected instruction pointer on the stack (is the stack trashed?)");
2,722✔
740
                            context.ip = ip->pageAddr();
2,722✔
741
                            context.pp = pop(context)->pageAddr();
2,722✔
742

743
                            returnFromFuncCall(context);
2,722✔
744
                            push(std::move(ts), context);
2,722✔
745
                        }
2,722✔
746
                        else if (ts.valueType() == ValueType::InstPtr)
113✔
747
                        {
748
                            context.ip = ts.pageAddr();
113✔
749
                            context.pp = ts1.pageAddr();
113✔
750
                            returnFromFuncCall(context);
113✔
751
                            push(Builtins::nil, context);
113✔
752
                        }
113✔
753
                        else
NEW
754
                            throw Error(
×
NEW
755
                                fmt::format(
×
NEW
756
                                    "Unhandled case when returning from function call. TS=({}){}, TS1=({}){}",
×
NEW
757
                                    std::to_string(ts.valueType()),
×
NEW
758
                                    ts.toString(*this),
×
NEW
759
                                    std::to_string(ts1.valueType()),
×
NEW
760
                                    ts1.toString(*this)));
×
761

762
                        if (context.fc <= untilFrameCount)
160,736✔
763
                            GOTO_HALT();
18✔
764
                    }
160,736✔
765

766
                    DISPATCH();
160,718✔
767
                }
104✔
768

769
                TARGET(HALT)
770
                {
771
                    m_running = false;
104✔
772
                    GOTO_HALT();
104✔
773
                }
170,494✔
774

775
                TARGET(PUSH_RETURN_ADDRESS)
776
                {
777
                    push(Value(static_cast<PageAddr_t>(context.pp)), context);
170,494✔
778
                    // arg * 4 to skip over the call instruction, so that the return address points to AFTER the call
779
                    push(Value(ValueType::InstPtr, static_cast<PageAddr_t>(arg * 4)), context);
170,494✔
780
                    context.inst_exec_counter++;
170,494✔
781
                    DISPATCH();
170,494✔
782
                }
2,708✔
783

784
                TARGET(CALL)
785
                {
786
                    call(context, arg);
2,708✔
787
                    if (!m_running)
2,708✔
NEW
788
                        GOTO_HALT();
×
789
                    DISPATCH();
2,708✔
790
                }
87,992✔
791

792
                TARGET(TAIL_CALL_SELF)
793
                {
794
                    jump(0, context);
87,992✔
795
                    context.locals.back().reset();
87,992✔
796
                    DISPATCH();
87,992✔
797
                }
3,227✔
798

799
                TARGET(CAPTURE)
800
                {
801
                    if (!context.saved_scope)
3,227✔
802
                        context.saved_scope = ClosureScope();
633✔
803

804
                    const Value* ptr = findNearestVariable(arg, context);
3,227✔
805
                    if (!ptr)
3,227✔
NEW
806
                        throwVMError(ErrorKind::Scope, fmt::format("Couldn't capture `{}' as it is currently unbound", m_state.m_symbols[arg]));
×
807
                    else
808
                    {
809
                        ptr = ptr->valueType() == ValueType::Reference ? ptr->reference() : ptr;
3,227✔
810
                        uint16_t id = context.capture_rename_id.value_or(arg);
3,227✔
811
                        context.saved_scope.value().push_back(id, *ptr);
3,227✔
812
                        context.capture_rename_id.reset();
3,227✔
813
                    }
814

815
                    DISPATCH();
3,227✔
816
                }
13✔
817

818
                TARGET(RENAME_NEXT_CAPTURE)
819
                {
820
                    context.capture_rename_id = arg;
13✔
821
                    DISPATCH();
13✔
822
                }
6,911✔
823

824
                TARGET(BUILTIN)
825
                {
826
                    push(Builtins::builtins[arg].second, context);
6,911✔
827
                    DISPATCH();
6,911✔
828
                }
2✔
829

830
                TARGET(DEL)
831
                {
832
                    if (Value* var = findNearestVariable(arg, context); var != nullptr)
2✔
833
                    {
834
                        if (var->valueType() == ValueType::User)
1✔
835
                            var->usertypeRef().del();
1✔
836
                        *var = Value();
1✔
837
                        DISPATCH();
1✔
838
                    }
839

840
                    throwVMError(ErrorKind::Scope, fmt::format("Can not delete unbound variable `{}'", m_state.m_symbols[arg]));
1✔
841
                }
633✔
842

843
                TARGET(MAKE_CLOSURE)
844
                {
845
                    push(Value(Closure(context.saved_scope.value(), m_state.m_constants[arg].pageAddr())), context);
633✔
846
                    context.saved_scope.reset();
633✔
847
                    DISPATCH();
633✔
NEW
848
                }
×
849

850
                TARGET(GET_FIELD)
851
                {
NEW
852
                    Value* var = popAndResolveAsPtr(context);
×
NEW
853
                    push(getField(var, arg, context), context);
×
NEW
854
                    DISPATCH();
×
855
                }
2,583✔
856

857
                TARGET(GET_FIELD_AS_CLOSURE)
858
                {
859
                    Value* var = popAndResolveAsPtr(context);
2,583✔
860
                    push(getField(var, arg, context, /* push_with_env= */ true), context);
2,583✔
861
                    DISPATCH();
2,583✔
862
                }
1✔
863

864
                TARGET(PLUGIN)
865
                {
866
                    loadPlugin(arg, context);
1✔
867
                    DISPATCH();
1✔
868
                }
1,323✔
869

870
                TARGET(LIST)
871
                {
872
                    {
873
                        Value l = createList(arg, context);
1,323✔
874
                        push(std::move(l), context);
1,323✔
875
                    }
1,323✔
876
                    DISPATCH();
1,323✔
877
                }
2,030✔
878

879
                TARGET(APPEND)
880
                {
881
                    {
882
                        Value* list = popAndResolveAsPtr(context);
2,030✔
883
                        if (list->valueType() != ValueType::List)
2,030✔
884
                        {
885
                            std::vector<Value> args = { *list };
1✔
886
                            for (uint16_t i = 0; i < arg; ++i)
2✔
887
                                args.push_back(*popAndResolveAsPtr(context));
1✔
888
                            throw types::TypeCheckingError(
2✔
889
                                "append",
1✔
890
                                { { types::Contract { { types::Typedef("list", ValueType::List), types::Typedef("value", ValueType::Any, /* is_variadic= */ true) } } } },
1✔
891
                                args);
892
                        }
1✔
893

894
                        const auto size = static_cast<uint16_t>(list->constList().size());
2,029✔
895

896
                        Value obj { *list };
2,029✔
897
                        obj.list().reserve(size + arg);
2,029✔
898

899
                        for (uint16_t i = 0; i < arg; ++i)
4,058✔
900
                            obj.push_back(*popAndResolveAsPtr(context));
2,029✔
901
                        push(std::move(obj), context);
2,029✔
902
                    }
2,029✔
903
                    DISPATCH();
2,029✔
904
                }
20✔
905

906
                TARGET(CONCAT)
907
                {
908
                    {
909
                        Value* list = popAndResolveAsPtr(context);
20✔
910
                        Value obj { *list };
20✔
911

912
                        for (uint16_t i = 0; i < arg; ++i)
40✔
913
                        {
914
                            Value* next = popAndResolveAsPtr(context);
22✔
915

916
                            if (list->valueType() != ValueType::List || next->valueType() != ValueType::List)
22✔
917
                                throw types::TypeCheckingError(
4✔
918
                                    "concat",
2✔
919
                                    { { types::Contract { { types::Typedef("dst", ValueType::List), types::Typedef("src", ValueType::List) } } } },
2✔
920
                                    { *list, *next });
2✔
921

922
                            std::ranges::copy(next->list(), std::back_inserter(obj.list()));
20✔
923
                        }
20✔
924
                        push(std::move(obj), context);
18✔
925
                    }
20✔
926
                    DISPATCH();
18✔
927
                }
1✔
928

929
                TARGET(APPEND_IN_PLACE)
930
                {
931
                    Value* list = popAndResolveAsPtr(context);
1✔
932
                    listAppendInPlace(list, arg, context);
1✔
933
                    DISPATCH();
1✔
934
                }
599✔
935

936
                TARGET(CONCAT_IN_PLACE)
937
                {
938
                    Value* list = popAndResolveAsPtr(context);
599✔
939

940
                    for (uint16_t i = 0; i < arg; ++i)
1,233✔
941
                    {
942
                        Value* next = popAndResolveAsPtr(context);
636✔
943

944
                        if (list->valueType() != ValueType::List || next->valueType() != ValueType::List)
636✔
945
                            throw types::TypeCheckingError(
4✔
946
                                "concat!",
2✔
947
                                { { types::Contract { { types::Typedef("dst", ValueType::List), types::Typedef("src", ValueType::List) } } } },
2✔
948
                                { *list, *next });
2✔
949

950
                        std::ranges::copy(next->list(), std::back_inserter(list->list()));
634✔
951
                    }
634✔
952
                    DISPATCH();
597✔
953
                }
1,087✔
954

955
                TARGET(POP_LIST)
956
                {
957
                    {
958
                        Value list = *popAndResolveAsPtr(context);
1,087✔
959
                        Value number = *popAndResolveAsPtr(context);
1,087✔
960

961
                        if (list.valueType() != ValueType::List || number.valueType() != ValueType::Number)
1,087✔
962
                            throw types::TypeCheckingError(
2✔
963
                                "pop",
1✔
964
                                { { types::Contract { { types::Typedef("list", ValueType::List), types::Typedef("index", ValueType::Number) } } } },
1✔
965
                                { list, number });
1✔
966

967
                        long idx = static_cast<long>(number.number());
1,086✔
968
                        idx = idx < 0 ? static_cast<long>(list.list().size()) + idx : idx;
1,086✔
969
                        if (std::cmp_greater_equal(idx, list.list().size()) || idx < 0)
1,086✔
970
                            throwVMError(
2✔
971
                                ErrorKind::Index,
972
                                fmt::format("pop index ({}) out of range (list size: {})", idx, list.list().size()));
2✔
973

974
                        list.list().erase(list.list().begin() + idx);
1,084✔
975
                        push(list, context);
1,084✔
976
                    }
1,087✔
977
                    DISPATCH();
1,084✔
978
                }
254✔
979

980
                TARGET(POP_LIST_IN_PLACE)
981
                {
982
                    {
983
                        Value* list = popAndResolveAsPtr(context);
254✔
984
                        Value number = *popAndResolveAsPtr(context);
254✔
985

986
                        if (list->valueType() != ValueType::List || number.valueType() != ValueType::Number)
254✔
987
                            throw types::TypeCheckingError(
2✔
988
                                "pop!",
1✔
989
                                { { types::Contract { { types::Typedef("list", ValueType::List), types::Typedef("index", ValueType::Number) } } } },
1✔
990
                                { *list, number });
1✔
991

992
                        long idx = static_cast<long>(number.number());
253✔
993
                        idx = idx < 0 ? static_cast<long>(list->list().size()) + idx : idx;
253✔
994
                        if (std::cmp_greater_equal(idx, list->list().size()) || idx < 0)
253✔
995
                            throwVMError(
2✔
996
                                ErrorKind::Index,
997
                                fmt::format("pop! index ({}) out of range (list size: {})", idx, list->list().size()));
2✔
998

999
                        // Save the value we're removing to push it later.
1000
                        // We need to save the value and push later because we're using a pointer to 'list', and pushing before erasing
1001
                        // would overwrite values from the stack.
1002
                        if (arg)
251✔
1003
                            number = list->list()[static_cast<std::size_t>(idx)];
236✔
1004
                        list->list().erase(list->list().begin() + idx);
251✔
1005
                        if (arg)
251✔
1006
                            push(number, context);
236✔
1007
                    }
254✔
1008
                    DISPATCH();
251✔
1009
                }
509,214✔
1010

1011
                TARGET(SET_AT_INDEX)
1012
                {
1013
                    {
1014
                        Value* list = popAndResolveAsPtr(context);
509,214✔
1015
                        Value number = *popAndResolveAsPtr(context);
509,214✔
1016
                        Value new_value = *popAndResolveAsPtr(context);
509,214✔
1017

1018
                        if (!list->isIndexable() || number.valueType() != ValueType::Number || (list->valueType() == ValueType::String && new_value.valueType() != ValueType::String))
509,214✔
1019
                            throw types::TypeCheckingError(
2✔
1020
                                "@=",
1✔
1021
                                { { types::Contract {
3✔
1022
                                      { types::Typedef("list", ValueType::List),
3✔
1023
                                        types::Typedef("index", ValueType::Number),
1✔
1024
                                        types::Typedef("new_value", ValueType::Any) } } },
1✔
1025
                                  { types::Contract {
1✔
1026
                                      { types::Typedef("string", ValueType::String),
3✔
1027
                                        types::Typedef("index", ValueType::Number),
1✔
1028
                                        types::Typedef("char", ValueType::String) } } } },
1✔
1029
                                { *list, number, new_value });
1✔
1030

1031
                        const std::size_t size = list->valueType() == ValueType::List ? list->list().size() : list->stringRef().size();
509,213✔
1032
                        long idx = static_cast<long>(number.number());
509,213✔
1033
                        idx = idx < 0 ? static_cast<long>(size) + idx : idx;
509,213✔
1034
                        if (std::cmp_greater_equal(idx, size) || idx < 0)
509,213✔
1035
                            throwVMError(
2✔
1036
                                ErrorKind::Index,
1037
                                fmt::format("@= index ({}) out of range (indexable size: {})", idx, size));
2✔
1038

1039
                        if (list->valueType() == ValueType::List)
509,211✔
1040
                        {
1041
                            list->list()[static_cast<std::size_t>(idx)] = new_value;
509,207✔
1042
                            if (arg)
509,207✔
1043
                                push(new_value, context);
24✔
1044
                        }
509,207✔
1045
                        else
1046
                        {
1047
                            list->stringRef()[static_cast<std::size_t>(idx)] = new_value.string()[0];
4✔
1048
                            if (arg)
4✔
1049
                                push(Value(std::string(1, new_value.string()[0])), context);
2✔
1050
                        }
1051
                    }
509,214✔
1052
                    DISPATCH();
509,211✔
1053
                }
1,117✔
1054

1055
                TARGET(SET_AT_2_INDEX)
1056
                {
1057
                    {
1058
                        Value* list = popAndResolveAsPtr(context);
1,117✔
1059
                        Value x = *popAndResolveAsPtr(context);
1,117✔
1060
                        Value y = *popAndResolveAsPtr(context);
1,117✔
1061
                        Value new_value = *popAndResolveAsPtr(context);
1,117✔
1062

1063
                        if (list->valueType() != ValueType::List || x.valueType() != ValueType::Number || y.valueType() != ValueType::Number)
1,117✔
1064
                            throw types::TypeCheckingError(
2✔
1065
                                "@@=",
1✔
1066
                                { { types::Contract {
2✔
1067
                                    { types::Typedef("list", ValueType::List),
4✔
1068
                                      types::Typedef("x", ValueType::Number),
1✔
1069
                                      types::Typedef("y", ValueType::Number),
1✔
1070
                                      types::Typedef("new_value", ValueType::Any) } } } },
1✔
1071
                                { *list, x, y, new_value });
1✔
1072

1073
                        long idx_y = static_cast<long>(x.number());
1,116✔
1074
                        idx_y = idx_y < 0 ? static_cast<long>(list->list().size()) + idx_y : idx_y;
1,116✔
1075
                        if (std::cmp_greater_equal(idx_y, list->list().size()) || idx_y < 0)
1,116✔
1076
                            throwVMError(
2✔
1077
                                ErrorKind::Index,
1078
                                fmt::format("@@= index (y: {}) out of range (list size: {})", idx_y, list->list().size()));
2✔
1079

1080
                        if (!list->list()[static_cast<std::size_t>(idx_y)].isIndexable() ||
1,124✔
1081
                            (list->list()[static_cast<std::size_t>(idx_y)].valueType() == ValueType::String && new_value.valueType() != ValueType::String))
1,113✔
1082
                            throw types::TypeCheckingError(
2✔
1083
                                "@@=",
1✔
1084
                                { { types::Contract {
3✔
1085
                                      { types::Typedef("list", ValueType::List),
4✔
1086
                                        types::Typedef("x", ValueType::Number),
1✔
1087
                                        types::Typedef("y", ValueType::Number),
1✔
1088
                                        types::Typedef("new_value", ValueType::Any) } } },
1✔
1089
                                  { types::Contract {
1✔
1090
                                      { types::Typedef("string", ValueType::String),
4✔
1091
                                        types::Typedef("x", ValueType::Number),
1✔
1092
                                        types::Typedef("y", ValueType::Number),
1✔
1093
                                        types::Typedef("char", ValueType::String) } } } },
1✔
1094
                                { *list, x, y, new_value });
1✔
1095

1096
                        const bool is_list = list->list()[static_cast<std::size_t>(idx_y)].valueType() == ValueType::List;
1,113✔
1097
                        const std::size_t size =
1,113✔
1098
                            is_list
2,226✔
1099
                            ? list->list()[static_cast<std::size_t>(idx_y)].list().size()
1,108✔
1100
                            : list->list()[static_cast<std::size_t>(idx_y)].stringRef().size();
5✔
1101

1102
                        long idx_x = static_cast<long>(y.number());
1,113✔
1103
                        idx_x = idx_x < 0 ? static_cast<long>(size) + idx_x : idx_x;
1,113✔
1104
                        if (std::cmp_greater_equal(idx_x, size) || idx_x < 0)
1,113✔
1105
                            throwVMError(
2✔
1106
                                ErrorKind::Index,
1107
                                fmt::format("@@= index (x: {}) out of range (inner indexable size: {})", idx_x, size));
2✔
1108

1109
                        if (is_list)
1,111✔
1110
                        {
1111
                            list->list()[static_cast<std::size_t>(idx_y)].list()[static_cast<std::size_t>(idx_x)] = new_value;
1,106✔
1112
                            if (arg)
1,106✔
1113
                                push(new_value, context);
2✔
1114
                        }
1,106✔
1115
                        else
1116
                        {
1117
                            list->list()[static_cast<std::size_t>(idx_y)].stringRef()[static_cast<std::size_t>(idx_x)] = new_value.string()[0];
5✔
1118
                            if (arg)
5✔
1119
                                push(Value(std::string(1, new_value.string()[0])), context);
3✔
1120
                        }
1121
                    }
1,117✔
1122
                    DISPATCH();
1,111✔
1123
                }
10,332✔
1124

1125
                TARGET(POP)
1126
                {
1127
                    pop(context);
10,332✔
1128
                    DISPATCH();
10,332✔
1129
                }
533,862✔
1130

1131
                TARGET(SHORTCIRCUIT_AND)
1132
                {
1133
                    if (!*peekAndResolveAsPtr(context))
533,862✔
1134
                        jump(arg, context);
4,713✔
1135
                    else
1136
                        pop(context);
529,149✔
1137
                    DISPATCH();
533,862✔
1138
                }
2,975✔
1139

1140
                TARGET(SHORTCIRCUIT_OR)
1141
                {
1142
                    if (!!*peekAndResolveAsPtr(context))
2,975✔
1143
                        jump(arg, context);
801✔
1144
                    else
1145
                        pop(context);
2,174✔
1146
                    DISPATCH();
2,975✔
1147
                }
10,581✔
1148

1149
                TARGET(CREATE_SCOPE)
1150
                {
1151
                    context.locals.emplace_back(context.scopes_storage.data(), context.locals.back().storageEnd());
10,581✔
1152
                    DISPATCH();
10,581✔
1153
                }
1,071,315✔
1154

1155
                TARGET(RESET_SCOPE_JUMP)
1156
                {
1157
                    context.locals.back().reset();
1,071,315✔
1158
                    jump(arg, context);
1,071,315✔
1159
                    DISPATCH();
1,071,315✔
1160
                }
10,580✔
1161

1162
                TARGET(POP_SCOPE)
1163
                {
1164
                    context.locals.pop_back();
10,580✔
1165
                    DISPATCH();
10,580✔
1166
                }
203✔
1167

1168
                TARGET(APPLY)
1169
                {
1170
                    {
1171
                        const Value args_list = *popAndResolveAsPtr(context),
203✔
1172
                                    func = *popAndResolveAsPtr(context);
203✔
1173
                        if (args_list.valueType() != ValueType::List || !func.isFunction())
203✔
1174
                        {
1175
                            throw types::TypeCheckingError(
4✔
1176
                                "apply",
2✔
1177
                                { {
8✔
1178
                                    types::Contract {
2✔
1179
                                        { types::Typedef("func", ValueType::PageAddr),
4✔
1180
                                          types::Typedef("args", ValueType::List) } },
2✔
1181
                                    types::Contract {
2✔
1182
                                        { types::Typedef("func", ValueType::Closure),
4✔
1183
                                          types::Typedef("args", ValueType::List) } },
2✔
1184
                                    types::Contract {
2✔
1185
                                        { types::Typedef("func", ValueType::CProc),
4✔
1186
                                          types::Typedef("args", ValueType::List) } },
2✔
1187
                                } },
1188
                                { func, args_list });
2✔
UNCOV
1189
                        }
×
1190

1191
                        push(func, context);
201✔
1192
                        for (const Value& a : args_list.constList())
542✔
1193
                            push(a, context);
341✔
1194

1195
                        call(context, static_cast<uint16_t>(args_list.constList().size()));
201✔
1196
                    }
203✔
1197
                    DISPATCH();
155✔
1198
                }
25✔
1199

1200
#pragma endregion
1201

1202
#pragma region "Operators"
1203

1204
                TARGET(BREAKPOINT)
1205
                {
1206
                    {
1207
                        bool breakpoint_active = true;
25✔
1208
                        if (arg == 1)
25✔
1209
                            breakpoint_active = *popAndResolveAsPtr(context) == Builtins::trueSym;
19✔
1210

1211
                        if (m_state.m_features & FeatureVMDebugger && breakpoint_active)
25✔
1212
                        {
1213
                            initDebugger(context);
13✔
1214
                            m_debugger->run(*this, context, /* from_breakpoint= */ true);
13✔
1215
                            m_debugger->resetContextToSavedState(context);
13✔
1216

1217
                            if (m_debugger->shouldQuitVM())
13✔
1218
                                GOTO_HALT();
1✔
1219
                        }
12✔
1220
                    }
1221
                    DISPATCH();
24✔
1222
                }
50,011✔
1223

1224
                TARGET(ADD)
1225
                {
1226
                    Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
50,011✔
1227

1228
                    if (a->valueType() == ValueType::Number && b->valueType() == ValueType::Number)
50,011✔
1229
                        push(Value(a->number() + b->number()), context);
36,464✔
1230
                    else if (a->valueType() == ValueType::String && b->valueType() == ValueType::String)
13,547✔
1231
                        push(Value(a->string() + b->string()), context);
13,546✔
1232
                    else
1233
                        throw types::TypeCheckingError(
2✔
1234
                            "+",
1✔
1235
                            { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } },
2✔
1236
                                types::Contract { { types::Typedef("a", ValueType::String), types::Typedef("b", ValueType::String) } } } },
1✔
1237
                            { *a, *b });
1✔
1238
                    DISPATCH();
50,010✔
1239
                }
511,246✔
1240

1241
                TARGET(SUB)
1242
                {
1243
                    Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
511,246✔
1244

1245
                    if (a->valueType() != ValueType::Number || b->valueType() != ValueType::Number)
511,246✔
1246
                        throw types::TypeCheckingError(
2✔
1247
                            "-",
1✔
1248
                            { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1249
                            { *a, *b });
1✔
1250
                    push(Value(a->number() - b->number()), context);
511,245✔
1251
                    DISPATCH();
511,245✔
1252
                }
510,019✔
1253

1254
                TARGET(MUL)
1255
                {
1256
                    Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
510,019✔
1257

1258
                    if (a->valueType() != ValueType::Number || b->valueType() != ValueType::Number)
510,019✔
1259
                        throw types::TypeCheckingError(
2✔
1260
                            "*",
1✔
1261
                            { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1262
                            { *a, *b });
1✔
1263
                    push(Value(a->number() * b->number()), context);
510,018✔
1264
                    DISPATCH();
510,018✔
1265
                }
3,296✔
1266

1267
                TARGET(DIV)
1268
                {
1269
                    Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
3,296✔
1270

1271
                    if (a->valueType() != ValueType::Number || b->valueType() != ValueType::Number)
3,296✔
1272
                        throw types::TypeCheckingError(
2✔
1273
                            "/",
1✔
1274
                            { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1275
                            { *a, *b });
1✔
1276
                    auto d = b->number();
3,295✔
1277
                    if (d == 0)
3,295✔
1278
                        throwVMError(ErrorKind::DivisionByZero, fmt::format("Can not compute expression (/ {} {})", a->toString(*this), b->toString(*this)));
1✔
1279

1280
                    push(Value(a->number() / d), context);
3,294✔
1281
                    DISPATCH();
3,294✔
1282
                }
1,678✔
1283

1284
                TARGET(GT)
1285
                {
1286
                    const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
1,678✔
1287
                    push(*b < *a ? Builtins::trueSym : Builtins::falseSym, context);
1,678✔
1288
                    DISPATCH();
1,678✔
1289
                }
25,980✔
1290

1291
                TARGET(LT)
1292
                {
1293
                    const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
25,980✔
1294
                    push(*a < *b ? Builtins::trueSym : Builtins::falseSym, context);
25,980✔
1295
                    DISPATCH();
25,980✔
1296
                }
512,625✔
1297

1298
                TARGET(LE)
1299
                {
1300
                    const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
512,625✔
1301
                    push((*a < *b || *a == *b) ? Builtins::trueSym : Builtins::falseSym, context);
512,625✔
1302
                    DISPATCH();
512,625✔
1303
                }
7,935✔
1304

1305
                TARGET(GE)
1306
                {
1307
                    const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
7,935✔
1308
                    push((*b < *a || *a == *b) ? Builtins::trueSym : Builtins::falseSym, context);
7,935✔
1309
                    DISPATCH();
7,935✔
1310
                }
503,927✔
1311

1312
                TARGET(NEQ)
1313
                {
1314
                    const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
503,927✔
1315
                    push(*a != *b ? Builtins::trueSym : Builtins::falseSym, context);
503,927✔
1316
                    DISPATCH();
503,927✔
1317
                }
28,659✔
1318

1319
                TARGET(EQ)
1320
                {
1321
                    const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
28,659✔
1322
                    push(*a == *b ? Builtins::trueSym : Builtins::falseSym, context);
28,659✔
1323
                    DISPATCH();
28,659✔
1324
                }
7,698✔
1325

1326
                TARGET(LEN)
1327
                {
1328
                    const Value* a = popAndResolveAsPtr(context);
7,698✔
1329

1330
                    if (a->valueType() == ValueType::List)
7,698✔
1331
                        push(Value(static_cast<int>(a->constList().size())), context);
3,810✔
1332
                    else if (a->valueType() == ValueType::String)
3,888✔
1333
                        push(Value(static_cast<int>(a->string().size())), context);
3,862✔
1334
                    else if (a->valueType() == ValueType::Dict)
26✔
1335
                        push(Value(static_cast<int>(a->dict().size())), context);
25✔
1336
                    else
1337
                        throw types::TypeCheckingError(
2✔
1338
                            "len",
1✔
1339
                            { { types::Contract { { types::Typedef("value", ValueType::List) } },
3✔
1340
                                types::Contract { { types::Typedef("value", ValueType::String) } },
1✔
1341
                                types::Contract { { types::Typedef("value", ValueType::Dict) } } } },
1✔
1342
                            { *a });
1✔
1343
                    DISPATCH();
7,697✔
1344
                }
660✔
1345

1346
                TARGET(IS_EMPTY)
1347
                {
1348
                    const Value* a = popAndResolveAsPtr(context);
660✔
1349

1350
                    if (a->valueType() == ValueType::List)
660✔
1351
                        push(a->constList().empty() ? Builtins::trueSym : Builtins::falseSym, context);
129✔
1352
                    else if (a->valueType() == ValueType::String)
531✔
1353
                        push(a->string().empty() ? Builtins::trueSym : Builtins::falseSym, context);
523✔
1354
                    else if (a->valueType() == ValueType::Dict)
8✔
1355
                        push(std::cmp_equal(a->dict().size(), 0) ? Builtins::trueSym : Builtins::falseSym, context);
4✔
1356
                    else if (a->valueType() == ValueType::Nil)
4✔
1357
                        push(Builtins::trueSym, context);
3✔
1358
                    else
1359
                        throw types::TypeCheckingError(
2✔
1360
                            "empty?",
1✔
1361
                            { { types::Contract { { types::Typedef("value", ValueType::List) } },
4✔
1362
                                types::Contract { { types::Typedef("value", ValueType::Nil) } },
1✔
1363
                                types::Contract { { types::Typedef("value", ValueType::String) } },
1✔
1364
                                types::Contract { { types::Typedef("value", ValueType::Dict) } } } },
1✔
1365
                            { *a });
1✔
1366
                    DISPATCH();
659✔
1367
                }
410✔
1368

1369
                TARGET(TAIL)
1370
                {
1371
                    Value* const a = popAndResolveAsPtr(context);
410✔
1372
                    push(helper::tail(a), context);
410✔
1373
                    DISPATCH();
410✔
1374
                }
1,432✔
1375

1376
                TARGET(HEAD)
1377
                {
1378
                    Value* const a = popAndResolveAsPtr(context);
1,432✔
1379
                    push(helper::head(a), context);
1,432✔
1380
                    DISPATCH();
1,432✔
1381
                }
2,439✔
1382

1383
                TARGET(IS_NIL)
1384
                {
1385
                    const Value* a = popAndResolveAsPtr(context);
2,439✔
1386
                    push((*a == Builtins::nil) ? Builtins::trueSym : Builtins::falseSym, context);
2,439✔
1387
                    DISPATCH();
2,439✔
1388
                }
1,100✔
1389

1390
                TARGET(TO_NUM)
1391
                {
1392
                    const Value* a = popAndResolveAsPtr(context);
1,100✔
1393

1394
                    if (a->valueType() != ValueType::String)
1,100✔
1395
                        throw types::TypeCheckingError(
2✔
1396
                            "toNumber",
1✔
1397
                            { { types::Contract { { types::Typedef("value", ValueType::String) } } } },
1✔
1398
                            { *a });
1✔
1399

1400
                    double val;
1401
                    if (Utils::isDouble(a->string(), &val))
1,099✔
1402
                        push(Value(val), context);
1,095✔
1403
                    else
1404
                        push(Builtins::nil, context);
4✔
1405
                    DISPATCH();
1,099✔
1406
                }
1,897✔
1407

1408
                TARGET(TO_STR)
1409
                {
1410
                    const Value* a = popAndResolveAsPtr(context);
1,897✔
1411
                    push(Value(a->toString(*this)), context);
1,897✔
1412
                    DISPATCH();
1,897✔
1413
                }
511,784✔
1414

1415
                TARGET(AT)
1416
                {
1417
                    Value& b = *popAndResolveAsPtr(context);
511,784✔
1418
                    Value& a = *popAndResolveAsPtr(context);
511,784✔
1419
                    push(helper::at(a, b, *this), context);
511,784✔
1420
                    DISPATCH();
511,784✔
1421
                }
2,611✔
1422

1423
                TARGET(AT_AT)
1424
                {
1425
                    {
1426
                        const Value* x = popAndResolveAsPtr(context);
2,611✔
1427
                        const Value* y = popAndResolveAsPtr(context);
2,611✔
1428
                        Value& list = *popAndResolveAsPtr(context);
2,611✔
1429

1430
                        push(helper::atAt(x, y, list), context);
2,611✔
1431
                    }
1432
                    DISPATCH();
2,611✔
1433
                }
1,033,219✔
1434

1435
                TARGET(MOD)
1436
                {
1437
                    const Value *b = popAndResolveAsPtr(context), *a = popAndResolveAsPtr(context);
1,033,219✔
1438
                    if (a->valueType() != ValueType::Number || b->valueType() != ValueType::Number)
1,033,219✔
1439
                        throw types::TypeCheckingError(
2✔
1440
                            "mod",
1✔
1441
                            { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1442
                            { *a, *b });
1✔
1443
                    push(Value(std::fmod(a->number(), b->number())), context);
1,033,218✔
1444
                    DISPATCH();
1,033,218✔
1445
                }
32✔
1446

1447
                TARGET(TYPE)
1448
                {
1449
                    const Value* a = popAndResolveAsPtr(context);
32✔
1450
                    push(Value(std::to_string(a->valueType())), context);
32✔
1451
                    DISPATCH();
32✔
1452
                }
3✔
1453

1454
                TARGET(HAS_FIELD)
1455
                {
1456
                    {
1457
                        Value* const field = popAndResolveAsPtr(context);
3✔
1458
                        Value* const closure = popAndResolveAsPtr(context);
3✔
1459
                        if (closure->valueType() != ValueType::Closure || field->valueType() != ValueType::String)
3✔
1460
                            throw types::TypeCheckingError(
2✔
1461
                                "hasField",
1✔
1462
                                { { types::Contract { { types::Typedef("closure", ValueType::Closure), types::Typedef("field", ValueType::String) } } } },
1✔
1463
                                { *closure, *field });
1✔
1464

1465
                        auto it = std::ranges::find(m_state.m_symbols, field->stringRef());
2✔
1466
                        if (it == m_state.m_symbols.end())
2✔
1467
                        {
1468
                            push(Builtins::falseSym, context);
1✔
1469
                            DISPATCH();
1✔
1470
                        }
1471

1472
                        auto id = static_cast<std::uint16_t>(std::distance(m_state.m_symbols.begin(), it));
1✔
1473
                        push(closure->refClosure().refScope()[id] != nullptr ? Builtins::trueSym : Builtins::falseSym, context);
1✔
1474
                    }
1475
                    DISPATCH();
1✔
1476
                }
3,792✔
1477

1478
                TARGET(NOT)
1479
                {
1480
                    const Value* a = popAndResolveAsPtr(context);
3,792✔
1481
                    push(!(*a) ? Builtins::trueSym : Builtins::falseSym, context);
3,792✔
1482
                    DISPATCH();
3,792✔
1483
                }
8,340✔
1484

1485
#pragma endregion
1486

1487
#pragma region "Super Instructions"
1488
                TARGET(LOAD_CONST_LOAD_CONST)
1489
                {
1490
                    UNPACK_ARGS();
8,340✔
1491
                    push(loadConstAsPtr(primary_arg), context);
8,340✔
1492
                    push(loadConstAsPtr(secondary_arg), context);
8,340✔
1493
                    context.inst_exec_counter++;
8,340✔
1494
                    DISPATCH();
8,340✔
1495
                }
20,238✔
1496

1497
                TARGET(LOAD_CONST_STORE)
1498
                {
1499
                    UNPACK_ARGS();
20,238✔
1500
                    store(secondary_arg, loadConstAsPtr(primary_arg), context);
20,238✔
1501
                    DISPATCH();
20,238✔
1502
                }
1,124✔
1503

1504
                TARGET(LOAD_CONST_SET_VAL)
1505
                {
1506
                    UNPACK_ARGS();
1,124✔
1507
                    setVal(secondary_arg, loadConstAsPtr(primary_arg), context);
1,124✔
1508
                    DISPATCH();
1,124✔
1509
                }
32✔
1510

1511
                TARGET(STORE_FROM)
1512
                {
1513
                    UNPACK_ARGS();
32✔
1514
                    store(secondary_arg, loadSymbol(primary_arg, context), context);
32✔
1515
                    DISPATCH();
32✔
1516
                }
1,326✔
1517

1518
                TARGET(STORE_FROM_INDEX)
1519
                {
1520
                    UNPACK_ARGS();
1,326✔
1521
                    store(secondary_arg, loadSymbolFromIndex(primary_arg, context), context);
1,326✔
1522
                    DISPATCH();
1,326✔
1523
                }
630✔
1524

1525
                TARGET(SET_VAL_FROM)
1526
                {
1527
                    UNPACK_ARGS();
630✔
1528
                    setVal(secondary_arg, loadSymbol(primary_arg, context), context);
630✔
1529
                    DISPATCH();
630✔
1530
                }
671✔
1531

1532
                TARGET(SET_VAL_FROM_INDEX)
1533
                {
1534
                    UNPACK_ARGS();
671✔
1535
                    setVal(secondary_arg, loadSymbolFromIndex(primary_arg, context), context);
671✔
1536
                    DISPATCH();
671✔
1537
                }
158✔
1538

1539
                TARGET(INCREMENT)
1540
                {
1541
                    UNPACK_ARGS();
158✔
1542
                    {
1543
                        Value* var = loadSymbol(primary_arg, context);
158✔
1544

1545
                        // use internal reference, shouldn't break anything so far, unless it's already a ref
1546
                        if (var->valueType() == ValueType::Reference)
158✔
NEW
1547
                            var = var->reference();
×
1548

1549
                        if (var->valueType() == ValueType::Number)
158✔
1550
                            push(Value(var->number() + secondary_arg), context);
157✔
1551
                        else
1552
                            throw types::TypeCheckingError(
2✔
1553
                                "+",
1✔
1554
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1555
                                { *var, Value(secondary_arg) });
1✔
1556
                    }
1557
                    DISPATCH();
157✔
1558
                }
91,613✔
1559

1560
                TARGET(INCREMENT_BY_INDEX)
1561
                {
1562
                    UNPACK_ARGS();
91,613✔
1563
                    {
1564
                        Value* var = loadSymbolFromIndex(primary_arg, context);
91,613✔
1565

1566
                        // use internal reference, shouldn't break anything so far, unless it's already a ref
1567
                        if (var->valueType() == ValueType::Reference)
91,613✔
NEW
1568
                            var = var->reference();
×
1569

1570
                        if (var->valueType() == ValueType::Number)
91,613✔
1571
                            push(Value(var->number() + secondary_arg), context);
91,612✔
1572
                        else
1573
                            throw types::TypeCheckingError(
2✔
1574
                                "+",
1✔
1575
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1576
                                { *var, Value(secondary_arg) });
1✔
1577
                    }
1578
                    DISPATCH();
91,612✔
1579
                }
559,074✔
1580

1581
                TARGET(INCREMENT_STORE)
1582
                {
1583
                    UNPACK_ARGS();
559,074✔
1584
                    {
1585
                        Value* var = loadSymbol(primary_arg, context);
559,074✔
1586

1587
                        // use internal reference, shouldn't break anything so far, unless it's already a ref
1588
                        if (var->valueType() == ValueType::Reference)
559,074✔
NEW
1589
                            var = var->reference();
×
1590

1591
                        if (var->valueType() == ValueType::Number)
559,074✔
1592
                        {
1593
                            auto val = Value(var->number() + secondary_arg);
559,073✔
1594
                            setVal(primary_arg, &val, context);
559,073✔
1595
                        }
559,073✔
1596
                        else
1597
                            throw types::TypeCheckingError(
2✔
1598
                                "+",
1✔
1599
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1600
                                { *var, Value(secondary_arg) });
1✔
1601
                    }
1602
                    DISPATCH();
559,073✔
1603
                }
515,382✔
1604

1605
                TARGET(DECREMENT)
1606
                {
1607
                    UNPACK_ARGS();
515,382✔
1608
                    {
1609
                        Value* var = loadSymbol(primary_arg, context);
515,382✔
1610

1611
                        // use internal reference, shouldn't break anything so far, unless it's already a ref
1612
                        if (var->valueType() == ValueType::Reference)
515,382✔
NEW
1613
                            var = var->reference();
×
1614

1615
                        if (var->valueType() == ValueType::Number)
515,382✔
1616
                            push(Value(var->number() - secondary_arg), context);
515,381✔
1617
                        else
1618
                            throw types::TypeCheckingError(
2✔
1619
                                "-",
1✔
1620
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1621
                                { *var, Value(secondary_arg) });
1✔
1622
                    }
1623
                    DISPATCH();
515,381✔
1624
                }
194,660✔
1625

1626
                TARGET(DECREMENT_BY_INDEX)
1627
                {
1628
                    UNPACK_ARGS();
194,660✔
1629
                    {
1630
                        Value* var = loadSymbolFromIndex(primary_arg, context);
194,660✔
1631

1632
                        // use internal reference, shouldn't break anything so far, unless it's already a ref
1633
                        if (var->valueType() == ValueType::Reference)
194,660✔
NEW
1634
                            var = var->reference();
×
1635

1636
                        if (var->valueType() == ValueType::Number)
194,660✔
1637
                            push(Value(var->number() - secondary_arg), context);
194,659✔
1638
                        else
1639
                            throw types::TypeCheckingError(
2✔
1640
                                "-",
1✔
1641
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1642
                                { *var, Value(secondary_arg) });
1✔
1643
                    }
1644
                    DISPATCH();
194,659✔
1645
                }
512,558✔
1646

1647
                TARGET(DECREMENT_STORE)
1648
                {
1649
                    UNPACK_ARGS();
512,558✔
1650
                    {
1651
                        Value* var = loadSymbol(primary_arg, context);
512,558✔
1652

1653
                        // use internal reference, shouldn't break anything so far, unless it's already a ref
1654
                        if (var->valueType() == ValueType::Reference)
512,558✔
NEW
1655
                            var = var->reference();
×
1656

1657
                        if (var->valueType() == ValueType::Number)
512,558✔
1658
                        {
1659
                            auto val = Value(var->number() - secondary_arg);
512,557✔
1660
                            setVal(primary_arg, &val, context);
512,557✔
1661
                        }
512,557✔
1662
                        else
1663
                            throw types::TypeCheckingError(
2✔
1664
                                "-",
1✔
1665
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
1666
                                { *var, Value(secondary_arg) });
1✔
1667
                    }
1668
                    DISPATCH();
512,557✔
1669
                }
1✔
1670

1671
                TARGET(STORE_TAIL)
1672
                {
1673
                    UNPACK_ARGS();
1✔
1674
                    {
1675
                        Value* list = loadSymbol(primary_arg, context);
1✔
1676
                        Value tail = helper::tail(list);
1✔
1677
                        store(secondary_arg, &tail, context);
1✔
1678
                    }
1✔
1679
                    DISPATCH();
1✔
1680
                }
8✔
1681

1682
                TARGET(STORE_TAIL_BY_INDEX)
1683
                {
1684
                    UNPACK_ARGS();
8✔
1685
                    {
1686
                        Value* list = loadSymbolFromIndex(primary_arg, context);
8✔
1687
                        Value tail = helper::tail(list);
8✔
1688
                        store(secondary_arg, &tail, context);
8✔
1689
                    }
8✔
1690
                    DISPATCH();
8✔
1691
                }
4✔
1692

1693
                TARGET(STORE_HEAD)
1694
                {
1695
                    UNPACK_ARGS();
4✔
1696
                    {
1697
                        Value* list = loadSymbol(primary_arg, context);
4✔
1698
                        Value head = helper::head(list);
4✔
1699
                        store(secondary_arg, &head, context);
4✔
1700
                    }
4✔
1701
                    DISPATCH();
4✔
1702
                }
38✔
1703

1704
                TARGET(STORE_HEAD_BY_INDEX)
1705
                {
1706
                    UNPACK_ARGS();
38✔
1707
                    {
1708
                        Value* list = loadSymbolFromIndex(primary_arg, context);
38✔
1709
                        Value head = helper::head(list);
38✔
1710
                        store(secondary_arg, &head, context);
38✔
1711
                    }
38✔
1712
                    DISPATCH();
38✔
1713
                }
4,285✔
1714

1715
                TARGET(STORE_LIST)
1716
                {
1717
                    UNPACK_ARGS();
4,285✔
1718
                    {
1719
                        Value l = createList(primary_arg, context);
4,285✔
1720
                        store(secondary_arg, &l, context);
4,285✔
1721
                    }
4,285✔
1722
                    DISPATCH();
4,285✔
1723
                }
3✔
1724

1725
                TARGET(SET_VAL_TAIL)
1726
                {
1727
                    UNPACK_ARGS();
3✔
1728
                    {
1729
                        Value* list = loadSymbol(primary_arg, context);
3✔
1730
                        Value tail = helper::tail(list);
3✔
1731
                        setVal(secondary_arg, &tail, context);
3✔
1732
                    }
3✔
1733
                    DISPATCH();
3✔
1734
                }
1✔
1735

1736
                TARGET(SET_VAL_TAIL_BY_INDEX)
1737
                {
1738
                    UNPACK_ARGS();
1✔
1739
                    {
1740
                        Value* list = loadSymbolFromIndex(primary_arg, context);
1✔
1741
                        Value tail = helper::tail(list);
1✔
1742
                        setVal(secondary_arg, &tail, context);
1✔
1743
                    }
1✔
1744
                    DISPATCH();
1✔
1745
                }
1✔
1746

1747
                TARGET(SET_VAL_HEAD)
1748
                {
1749
                    UNPACK_ARGS();
1✔
1750
                    {
1751
                        Value* list = loadSymbol(primary_arg, context);
1✔
1752
                        Value head = helper::head(list);
1✔
1753
                        setVal(secondary_arg, &head, context);
1✔
1754
                    }
1✔
1755
                    DISPATCH();
1✔
1756
                }
1✔
1757

1758
                TARGET(SET_VAL_HEAD_BY_INDEX)
1759
                {
1760
                    UNPACK_ARGS();
1✔
1761
                    {
1762
                        Value* list = loadSymbolFromIndex(primary_arg, context);
1✔
1763
                        Value head = helper::head(list);
1✔
1764
                        setVal(secondary_arg, &head, context);
1✔
1765
                    }
1✔
1766
                    DISPATCH();
1✔
1767
                }
7,290✔
1768

1769
                TARGET(CALL_BUILTIN)
1770
                {
1771
                    UNPACK_ARGS();
7,290✔
1772
                    // no stack size check because we do not push IP/PP since we are just calling a builtin
1773
                    callBuiltin(
14,580✔
1774
                        context,
7,290✔
1775
                        Builtins::builtins[primary_arg].second,
7,290✔
1776
                        secondary_arg,
7,290✔
1777
                        /* remove_return_address= */ true,
1778
                        /* remove_builtin= */ false);
1779
                    if (!m_running)
7,290✔
NEW
1780
                        GOTO_HALT();
×
1781
                    DISPATCH();
7,290✔
1782
                }
18,216✔
1783

1784
                TARGET(CALL_BUILTIN_WITHOUT_RETURN_ADDRESS)
1785
                {
1786
                    UNPACK_ARGS();
18,216✔
1787
                    // no stack size check because we do not push IP/PP since we are just calling a builtin
1788
                    callBuiltin(
36,432✔
1789
                        context,
18,216✔
1790
                        Builtins::builtins[primary_arg].second,
18,216✔
1791
                        secondary_arg,
18,216✔
1792
                        // we didn't have a PUSH_RETURN_ADDRESS instruction before,
1793
                        // so do not attempt to remove (pp,ip) from the stack: they're not there!
1794
                        /* remove_return_address= */ false,
1795
                        /* remove_builtin= */ false);
1796
                    if (!m_running)
18,216✔
NEW
1797
                        GOTO_HALT();
×
1798
                    DISPATCH();
18,216✔
1799
                }
877✔
1800

1801
                TARGET(LT_CONST_JUMP_IF_FALSE)
1802
                {
1803
                    UNPACK_ARGS();
877✔
1804
                    const Value* sym = popAndResolveAsPtr(context);
877✔
1805
                    if (!(*sym < *loadConstAsPtr(primary_arg)))
877✔
1806
                        jump(secondary_arg, context);
124✔
1807
                    DISPATCH();
877✔
1808
                }
22,988✔
1809

1810
                TARGET(LT_CONST_JUMP_IF_TRUE)
1811
                {
1812
                    UNPACK_ARGS();
22,988✔
1813
                    const Value* sym = popAndResolveAsPtr(context);
22,988✔
1814
                    if (*sym < *loadConstAsPtr(primary_arg))
22,988✔
1815
                        jump(secondary_arg, context);
11,959✔
1816
                    DISPATCH();
22,988✔
1817
                }
12,757✔
1818

1819
                TARGET(LT_SYM_JUMP_IF_FALSE)
1820
                {
1821
                    UNPACK_ARGS();
12,757✔
1822
                    const Value* sym = popAndResolveAsPtr(context);
12,757✔
1823
                    if (!(*sym < *loadSymbol(primary_arg, context)))
12,757✔
1824
                        jump(secondary_arg, context);
1,056✔
1825
                    DISPATCH();
12,757✔
1826
                }
172,645✔
1827

1828
                TARGET(GT_CONST_JUMP_IF_TRUE)
1829
                {
1830
                    UNPACK_ARGS();
172,645✔
1831
                    const Value* sym = popAndResolveAsPtr(context);
172,645✔
1832
                    const Value* cst = loadConstAsPtr(primary_arg);
172,645✔
1833
                    if (*cst < *sym)
172,645✔
1834
                        jump(secondary_arg, context);
86,674✔
1835
                    DISPATCH();
172,645✔
1836
                }
4,080✔
1837

1838
                TARGET(GT_CONST_JUMP_IF_FALSE)
1839
                {
1840
                    UNPACK_ARGS();
4,080✔
1841
                    const Value* sym = popAndResolveAsPtr(context);
4,080✔
1842
                    const Value* cst = loadConstAsPtr(primary_arg);
4,080✔
1843
                    if (!(*cst < *sym))
4,080✔
1844
                        jump(secondary_arg, context);
1,042✔
1845
                    DISPATCH();
4,080✔
1846
                }
11✔
1847

1848
                TARGET(GT_SYM_JUMP_IF_FALSE)
1849
                {
1850
                    UNPACK_ARGS();
11✔
1851
                    const Value* sym = popAndResolveAsPtr(context);
11✔
1852
                    const Value* rhs = loadSymbol(primary_arg, context);
11✔
1853
                    if (!(*rhs < *sym))
11✔
1854
                        jump(secondary_arg, context);
2✔
1855
                    DISPATCH();
11✔
1856
                }
502,107✔
1857

1858
                TARGET(EQ_CONST_JUMP_IF_TRUE)
1859
                {
1860
                    UNPACK_ARGS();
502,107✔
1861
                    const Value* sym = popAndResolveAsPtr(context);
502,107✔
1862
                    if (*sym == *loadConstAsPtr(primary_arg))
502,107✔
1863
                        jump(secondary_arg, context);
12,183✔
1864
                    DISPATCH();
502,107✔
1865
                }
89,117✔
1866

1867
                TARGET(EQ_SYM_INDEX_JUMP_IF_TRUE)
1868
                {
1869
                    UNPACK_ARGS();
89,117✔
1870
                    const Value* sym = popAndResolveAsPtr(context);
89,117✔
1871
                    if (*sym == *loadSymbolFromIndex(primary_arg, context))
89,117✔
1872
                        jump(secondary_arg, context);
551✔
1873
                    DISPATCH();
89,117✔
1874
                }
11✔
1875

1876
                TARGET(NEQ_CONST_JUMP_IF_TRUE)
1877
                {
1878
                    UNPACK_ARGS();
11✔
1879
                    const Value* sym = popAndResolveAsPtr(context);
11✔
1880
                    if (*sym != *loadConstAsPtr(primary_arg))
11✔
1881
                        jump(secondary_arg, context);
2✔
1882
                    DISPATCH();
11✔
1883
                }
30✔
1884

1885
                TARGET(NEQ_SYM_JUMP_IF_FALSE)
1886
                {
1887
                    UNPACK_ARGS();
30✔
1888
                    const Value* sym = popAndResolveAsPtr(context);
30✔
1889
                    if (*sym == *loadSymbol(primary_arg, context))
30✔
1890
                        jump(secondary_arg, context);
10✔
1891
                    DISPATCH();
30✔
1892
                }
49,106✔
1893

1894
                TARGET(CALL_SYMBOL)
1895
                {
1896
                    UNPACK_ARGS();
49,106✔
1897
                    call(context, secondary_arg, /* function_ptr= */ loadSymbol(primary_arg, context));
49,106✔
1898
                    if (!m_running)
49,106✔
NEW
1899
                        GOTO_HALT();
×
1900
                    DISPATCH();
49,106✔
1901
                }
1,146✔
1902

1903
                TARGET(CALL_SYMBOL_BY_INDEX)
1904
                {
1905
                    UNPACK_ARGS();
1,146✔
1906
                    call(context, secondary_arg, /* function_ptr= */ loadSymbolFromIndex(primary_arg, context));
1,146✔
1907
                    if (!m_running)
1,146✔
NEW
1908
                        GOTO_HALT();
×
1909
                    DISPATCH();
1,146✔
1910
                }
109,874✔
1911

1912
                TARGET(CALL_CURRENT_PAGE)
1913
                {
1914
                    UNPACK_ARGS();
109,874✔
1915
                    context.last_symbol = primary_arg;
109,874✔
1916
                    call(context, secondary_arg, /* function_ptr= */ nullptr, /* or_address= */ static_cast<PageAddr_t>(context.pp));
109,874✔
1917
                    if (!m_running)
109,874✔
NEW
1918
                        GOTO_HALT();
×
1919
                    DISPATCH();
109,874✔
1920
                }
1,719✔
1921

1922
                TARGET(GET_FIELD_FROM_SYMBOL)
1923
                {
1924
                    UNPACK_ARGS();
1,719✔
1925
                    push(getField(loadSymbol(primary_arg, context), secondary_arg, context), context);
1,719✔
1926
                    DISPATCH();
1,719✔
1927
                }
311✔
1928

1929
                TARGET(GET_FIELD_FROM_SYMBOL_INDEX)
1930
                {
1931
                    UNPACK_ARGS();
311✔
1932
                    push(getField(loadSymbolFromIndex(primary_arg, context), secondary_arg, context), context);
311✔
1933
                    DISPATCH();
311✔
1934
                }
545,072✔
1935

1936
                TARGET(AT_SYM_SYM)
1937
                {
1938
                    UNPACK_ARGS();
545,072✔
1939
                    push(helper::at(*loadSymbol(primary_arg, context), *loadSymbol(secondary_arg, context), *this), context);
545,072✔
1940
                    DISPATCH();
545,072✔
1941
                }
49✔
1942

1943
                TARGET(AT_SYM_INDEX_SYM_INDEX)
1944
                {
1945
                    UNPACK_ARGS();
49✔
1946
                    push(helper::at(*loadSymbolFromIndex(primary_arg, context), *loadSymbolFromIndex(secondary_arg, context), *this), context);
49✔
1947
                    DISPATCH();
49✔
1948
                }
6,405✔
1949

1950
                TARGET(AT_SYM_INDEX_CONST)
1951
                {
1952
                    UNPACK_ARGS();
6,405✔
1953
                    push(helper::at(*loadSymbolFromIndex(primary_arg, context), *loadConstAsPtr(secondary_arg), *this), context);
6,405✔
1954
                    DISPATCH();
6,405✔
1955
                }
2✔
1956

1957
                TARGET(CHECK_TYPE_OF)
1958
                {
1959
                    UNPACK_ARGS();
2✔
1960
                    const Value* sym = loadSymbol(primary_arg, context);
2✔
1961
                    const Value* cst = loadConstAsPtr(secondary_arg);
2✔
1962
                    push(
2✔
1963
                        cst->valueType() == ValueType::String &&
4✔
1964
                                std::to_string(sym->valueType()) == cst->string()
2✔
1965
                            ? Builtins::trueSym
1966
                            : Builtins::falseSym,
1967
                        context);
2✔
1968
                    DISPATCH();
2✔
1969
                }
160✔
1970

1971
                TARGET(CHECK_TYPE_OF_BY_INDEX)
1972
                {
1973
                    UNPACK_ARGS();
160✔
1974
                    const Value* sym = loadSymbolFromIndex(primary_arg, context);
160✔
1975
                    const Value* cst = loadConstAsPtr(secondary_arg);
160✔
1976
                    push(
160✔
1977
                        cst->valueType() == ValueType::String &&
320✔
1978
                                std::to_string(sym->valueType()) == cst->string()
160✔
1979
                            ? Builtins::trueSym
1980
                            : Builtins::falseSym,
1981
                        context);
160✔
1982
                    DISPATCH();
160✔
1983
                }
21,909✔
1984

1985
                TARGET(APPEND_IN_PLACE_SYM)
1986
                {
1987
                    UNPACK_ARGS();
21,909✔
1988
                    listAppendInPlace(loadSymbol(primary_arg, context), secondary_arg, context);
21,909✔
1989
                    DISPATCH();
21,909✔
1990
                }
16✔
1991

1992
                TARGET(APPEND_IN_PLACE_SYM_INDEX)
1993
                {
1994
                    UNPACK_ARGS();
16✔
1995
                    listAppendInPlace(loadSymbolFromIndex(primary_arg, context), secondary_arg, context);
16✔
1996
                    DISPATCH();
16✔
1997
                }
164✔
1998

1999
                TARGET(STORE_LEN)
2000
                {
2001
                    UNPACK_ARGS();
164✔
2002
                    {
2003
                        Value* a = loadSymbolFromIndex(primary_arg, context);
164✔
2004
                        Value len;
164✔
2005
                        if (a->valueType() == ValueType::List)
164✔
2006
                            len = Value(static_cast<int>(a->constList().size()));
48✔
2007
                        else if (a->valueType() == ValueType::String)
116✔
2008
                            len = Value(static_cast<int>(a->string().size()));
115✔
2009
                        else
2010
                            throw types::TypeCheckingError(
2✔
2011
                                "len",
1✔
2012
                                { { types::Contract { { types::Typedef("value", ValueType::List) } },
2✔
2013
                                    types::Contract { { types::Typedef("value", ValueType::String) } } } },
1✔
2014
                                { *a });
1✔
2015
                        store(secondary_arg, &len, context);
163✔
2016
                    }
164✔
2017
                    DISPATCH();
163✔
2018
                }
28,613✔
2019

2020
                TARGET(LT_LEN_SYM_JUMP_IF_FALSE)
2021
                {
2022
                    UNPACK_ARGS();
28,613✔
2023
                    {
2024
                        const Value* sym = loadSymbol(primary_arg, context);
28,613✔
2025
                        Value size;
28,613✔
2026

2027
                        if (sym->valueType() == ValueType::List)
28,613✔
2028
                            size = Value(static_cast<int>(sym->constList().size()));
22,649✔
2029
                        else if (sym->valueType() == ValueType::String)
5,964✔
2030
                            size = Value(static_cast<int>(sym->string().size()));
5,963✔
2031
                        else
2032
                            throw types::TypeCheckingError(
2✔
2033
                                "len",
1✔
2034
                                { { types::Contract { { types::Typedef("value", ValueType::List) } },
2✔
2035
                                    types::Contract { { types::Typedef("value", ValueType::String) } } } },
1✔
2036
                                { *sym });
1✔
2037

2038
                        if (!(*popAndResolveAsPtr(context) < size))
28,612✔
2039
                            jump(secondary_arg, context);
3,592✔
2040
                    }
28,613✔
2041
                    DISPATCH();
28,612✔
2042
                }
521✔
2043

2044
                TARGET(MUL_BY)
2045
                {
2046
                    UNPACK_ARGS();
521✔
2047
                    {
2048
                        Value* var = loadSymbol(primary_arg, context);
521✔
2049
                        const int other = static_cast<int>(secondary_arg) - 2048;
521✔
2050

2051
                        // use internal reference, shouldn't break anything so far, unless it's already a ref
2052
                        if (var->valueType() == ValueType::Reference)
521✔
NEW
2053
                            var = var->reference();
×
2054

2055
                        if (var->valueType() == ValueType::Number)
521✔
2056
                            push(Value(var->number() * other), context);
520✔
2057
                        else
2058
                            throw types::TypeCheckingError(
2✔
2059
                                "*",
1✔
2060
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
2061
                                { *var, Value(other) });
1✔
2062
                    }
2063
                    DISPATCH();
520✔
2064
                }
36✔
2065

2066
                TARGET(MUL_BY_INDEX)
2067
                {
2068
                    UNPACK_ARGS();
36✔
2069
                    {
2070
                        Value* var = loadSymbolFromIndex(primary_arg, context);
36✔
2071
                        const int other = static_cast<int>(secondary_arg) - 2048;
36✔
2072

2073
                        // use internal reference, shouldn't break anything so far, unless it's already a ref
2074
                        if (var->valueType() == ValueType::Reference)
36✔
NEW
2075
                            var = var->reference();
×
2076

2077
                        if (var->valueType() == ValueType::Number)
36✔
2078
                            push(Value(var->number() * other), context);
35✔
2079
                        else
2080
                            throw types::TypeCheckingError(
2✔
2081
                                "*",
1✔
2082
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
2083
                                { *var, Value(other) });
1✔
2084
                    }
2085
                    DISPATCH();
35✔
2086
                }
2✔
2087

2088
                TARGET(MUL_SET_VAL)
2089
                {
2090
                    UNPACK_ARGS();
2✔
2091
                    {
2092
                        Value* var = loadSymbol(primary_arg, context);
2✔
2093
                        const int other = static_cast<int>(secondary_arg) - 2048;
2✔
2094

2095
                        // use internal reference, shouldn't break anything so far, unless it's already a ref
2096
                        if (var->valueType() == ValueType::Reference)
2✔
NEW
2097
                            var = var->reference();
×
2098

2099
                        if (var->valueType() == ValueType::Number)
2✔
2100
                        {
2101
                            auto val = Value(var->number() * other);
1✔
2102
                            setVal(primary_arg, &val, context);
1✔
2103
                        }
1✔
2104
                        else
2105
                            throw types::TypeCheckingError(
2✔
2106
                                "*",
1✔
2107
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
2108
                                { *var, Value(other) });
1✔
2109
                    }
2110
                    DISPATCH();
1✔
2111
                }
509,610✔
2112

2113
                TARGET(FUSED_MATH)
2114
                {
2115
                    const auto op1 = static_cast<Instruction>(padding),
509,610✔
2116
                               op2 = static_cast<Instruction>((arg & 0xff00) >> 8),
509,610✔
2117
                               op3 = static_cast<Instruction>(arg & 0x00ff);
509,610✔
2118
                    const std::size_t arg_count = (op1 != NOP) + (op2 != NOP) + (op3 != NOP);
509,610✔
2119

2120
                    const Value* d = popAndResolveAsPtr(context);
509,610✔
2121
                    const Value* c = popAndResolveAsPtr(context);
509,610✔
2122
                    const Value* b = popAndResolveAsPtr(context);
509,610✔
2123

2124
                    if (d->valueType() != ValueType::Number || c->valueType() != ValueType::Number)
509,610✔
2125
                        throw types::TypeCheckingError(
2✔
2126
                            helper::mathInstToStr(op1),
1✔
2127
                            { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
2128
                            { *c, *d });
1✔
2129

2130
                    double temp = helper::doMath(c->number(), d->number(), op1);
509,609✔
2131
                    if (b->valueType() != ValueType::Number)
509,609✔
2132
                        throw types::TypeCheckingError(
4✔
2133
                            helper::mathInstToStr(op2),
2✔
2134
                            { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
2✔
2135
                            { *b, Value(temp) });
2✔
2136
                    temp = helper::doMath(b->number(), temp, op2);
509,607✔
2137

2138
                    if (arg_count == 2)
509,607✔
2139
                        push(Value(temp), context);
509,570✔
2140
                    else if (arg_count == 3)
37✔
2141
                    {
2142
                        const Value* a = popAndResolveAsPtr(context);
37✔
2143
                        if (a->valueType() != ValueType::Number)
37✔
2144
                            throw types::TypeCheckingError(
2✔
2145
                                helper::mathInstToStr(op3),
1✔
2146
                                { { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
1✔
2147
                                { *a, Value(temp) });
1✔
2148

2149
                        temp = helper::doMath(a->number(), temp, op3);
36✔
2150
                        push(Value(temp), context);
36✔
2151
                    }
36✔
2152
                    else
NEW
2153
                        throw Error(
×
NEW
2154
                            fmt::format(
×
NEW
2155
                                "FUSED_MATH got {} arguments, expected 2 or 3. Arguments: {:x}{:x}{:x}. There is a bug in the codegen!",
×
NEW
2156
                                arg_count, static_cast<uint8_t>(op1), static_cast<uint8_t>(op2), static_cast<uint8_t>(op3)));
×
2157
                    DISPATCH();
509,606✔
2158
                }
2159
#pragma endregion
2160
            }
123✔
2161
#if ARK_USE_COMPUTED_GOTOS
2162
        dispatch_end:
2163
            do
123✔
2164
            {
2165
            } while (false);
123✔
2166
#endif
2167
        }
2168
    }
218✔
2169

2170
    uint16_t VM::findNearestVariableIdWithValue(const Value& value, ExecutionContext& context) const noexcept
2,054✔
2171
    {
2,054✔
2172
        for (auto& local : std::ranges::reverse_view(context.locals))
2,098,196✔
2173
        {
2174
            if (const auto id = local.idFromValue(value); id < m_state.m_symbols.size())
2,096,142✔
2175
                return id;
2,048✔
2176
        }
2,096,142✔
2177
        return MaxValue16Bits;
6✔
2178
    }
2,054✔
2179

2180
    void VM::throwArityError(std::size_t passed_arg_count, std::size_t expected_arg_count, ExecutionContext& context, const bool skip_function)
7✔
2181
    {
7✔
2182
        std::vector<std::string> arg_names;
7✔
2183
        arg_names.reserve(expected_arg_count + 1);
7✔
2184

2185
        std::size_t index = 0;
7✔
2186
        while (m_state.inst(context.pp, index) == STORE ||
14✔
2187
               m_state.inst(context.pp, index) == STORE_REF)
7✔
2188
        {
UNCOV
2189
            const auto id = static_cast<uint16_t>((m_state.inst(context.pp, index + 2) << 8) + m_state.inst(context.pp, index + 3));
×
UNCOV
2190
            arg_names.insert(arg_names.begin(), m_state.m_symbols[id]);
×
UNCOV
2191
            index += 4;
×
UNCOV
2192
        }
×
2193
        // we have no arg names, probably because of a CALL_BUILTIN_WITHOUT_RETURN_ADDRESS
2194
        if (arg_names.empty() && index == 0)
7✔
2195
        {
2196
            for (std::size_t i = 0; i < expected_arg_count; ++i)
5✔
2197
                arg_names.emplace_back(1, static_cast<char>('a' + i));
2✔
2198
        }
3✔
2199
        if (expected_arg_count > 0)
7✔
2200
            arg_names.insert(arg_names.begin(), "");  // for formatting, so that we have a space between the function and the args
6✔
2201

2202
        std::vector<std::string> arg_vals;
7✔
2203
        arg_vals.reserve(passed_arg_count + 1);
7✔
2204

2205
        for (std::size_t i = 0; i < passed_arg_count && i + 1 <= context.sp; ++i)
20✔
2206
            // -1 on the stack because we always point to the next available slot
2207
            arg_vals.push_back(context.stack[context.sp - passed_arg_count + i].toString(*this));
13✔
2208
        if (passed_arg_count > 0)
7✔
2209
            arg_vals.insert(arg_vals.begin(), "");  // for formatting, so that we have a space between the function and the args
6✔
2210

2211
        // set ip/pp to the callee location so that the error can pinpoint the line
2212
        // where the bad call happened
2213
        if (context.sp >= 2 + passed_arg_count)
7✔
2214
        {
2215
            // -2/-3 instead of -1/-2 to skip over the function pushed on the stack
2216
            context.ip = context.stack[context.sp - 1 - (skip_function ? 1 : 0) - passed_arg_count].pageAddr();
7✔
2217
            context.pp = context.stack[context.sp - 2 - (skip_function ? 1 : 0) - passed_arg_count].pageAddr();
7✔
2218
            context.sp -= 2;
7✔
2219
            returnFromFuncCall(context);
7✔
2220
        }
7✔
2221

2222
        std::string function_name = (context.last_symbol < m_state.m_symbols.size())
14✔
2223
            ? m_state.m_symbols[context.last_symbol]
7✔
UNCOV
2224
            : Value(static_cast<PageAddr_t>(context.pp)).toString(*this);
×
2225

2226
        throwVMError(
7✔
2227
            ErrorKind::Arity,
2228
            fmt::format(
14✔
2229
                "When calling `({}{})', received {} argument{}, but expected {}: `({}{})'",
7✔
2230
                function_name,
2231
                fmt::join(arg_vals, " "),
7✔
2232
                passed_arg_count,
2233
                passed_arg_count > 1 ? "s" : "",
7✔
2234
                expected_arg_count,
2235
                function_name,
2236
                fmt::join(arg_names, " ")));
7✔
2237
    }
14✔
2238

2239
    void VM::initDebugger(ExecutionContext& context)
14✔
2240
    {
14✔
2241
        if (!m_debugger)
14✔
2242
            m_debugger = std::make_unique<Debugger>(context, m_state.m_libenv, m_state.m_symbols, m_state.m_constants);
×
2243
        else
2244
            m_debugger->saveState(context);
14✔
2245
    }
14✔
2246

2247
    void VM::showBacktraceWithException(const std::exception& e, ExecutionContext& context)
1✔
2248
    {
1✔
2249
        std::string text = e.what();
1✔
2250
        if (!text.empty() && text.back() != '\n')
1✔
UNCOV
2251
            text += '\n';
×
2252
        fmt::println(std::cerr, "{}", text);
1✔
2253

2254
        // If code being run from the debugger crashed, ignore it and don't trigger a debugger inside the VM inside the debugger inside the VM
2255
        const bool error_from_debugger = m_debugger && m_debugger->isRunning();
1✔
2256
        if (m_state.m_features & FeatureVMDebugger && !error_from_debugger)
1✔
2257
            initDebugger(context);
1✔
2258

2259
        const std::size_t saved_ip = context.ip;
1✔
2260
        const std::size_t saved_pp = context.pp;
1✔
2261
        const uint16_t saved_sp = context.sp;
1✔
2262

2263
        backtrace(context);
1✔
2264

2265
        fmt::println(
1✔
2266
            std::cerr,
2267
            "At IP: {}, PP: {}, SP: {}",
1✔
2268
            // dividing by 4 because the instructions are actually on 4 bytes
2269
            fmt::styled(saved_ip / 4, fmt::fg(fmt::color::cyan)),
1✔
2270
            fmt::styled(saved_pp, fmt::fg(fmt::color::green)),
1✔
2271
            fmt::styled(saved_sp, fmt::fg(fmt::color::yellow)));
1✔
2272

2273
        if (m_debugger && !error_from_debugger)
1✔
2274
        {
2275
            m_debugger->resetContextToSavedState(context);
1✔
2276
            m_debugger->run(*this, context, /* from_breakpoint= */ false);
1✔
2277
        }
1✔
2278

2279
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
2280
        // don't report a "failed" exit code so that the fuzzers can more accurately triage crashes
2281
        m_exit_code = 0;
2282
#else
2283
        m_exit_code = 1;
1✔
2284
#endif
2285
    }
1✔
2286

2287
    std::optional<InstLoc> VM::findSourceLocation(const std::size_t ip, const std::size_t pp) const
2,287✔
2288
    {
2,287✔
2289
        std::optional<InstLoc> match = std::nullopt;
2,287✔
2290

2291
        for (const auto location : m_state.m_inst_locations)
11,221✔
2292
        {
2293
            if (location.page_pointer == pp && !match)
8,934✔
2294
                match = location;
2,287✔
2295

2296
            // select the best match: we want to find the location that's nearest our instruction pointer,
2297
            // but not equal to it as the IP will always be pointing to the next instruction,
2298
            // not yet executed. Thus, the erroneous instruction is the previous one.
2299
            if (location.page_pointer == pp && match && location.inst_pointer < ip / 4)
8,934✔
2300
                match = location;
2,483✔
2301

2302
            // early exit because we won't find anything better, as inst locations are ordered by ascending (pp, ip)
2303
            if (location.page_pointer > pp || (location.page_pointer == pp && location.inst_pointer >= ip / 4))
8,934✔
2304
                break;
2,086✔
2305
        }
8,934✔
2306

2307
        return match;
2,287✔
2308
    }
2309

2310
    std::string VM::debugShowSource() const
×
2311
    {
×
UNCOV
2312
        const auto& context = m_execution_contexts.front();
×
UNCOV
2313
        auto maybe_source_loc = findSourceLocation(context->ip, context->pp);
×
UNCOV
2314
        if (maybe_source_loc)
×
2315
        {
UNCOV
2316
            const auto filename = m_state.m_filenames[maybe_source_loc->filename_id];
×
UNCOV
2317
            return fmt::format("{}:{} -- IP: {}, PP: {}", filename, maybe_source_loc->line + 1, maybe_source_loc->inst_pointer, maybe_source_loc->page_pointer);
×
UNCOV
2318
        }
×
UNCOV
2319
        return "No source location found";
×
UNCOV
2320
    }
×
2321

2322
    void VM::backtrace(ExecutionContext& context, std::ostream& os, const bool colorize)
212✔
2323
    {
212✔
2324
        constexpr std::size_t max_consecutive_traces = 7;
212✔
2325

2326
        const auto maybe_location = findSourceLocation(context.ip, context.pp);
212✔
2327
        if (maybe_location)
212✔
2328
        {
2329
            const auto filename = m_state.m_filenames[maybe_location->filename_id];
212✔
2330

2331
            if (Utils::fileExists(filename))
212✔
2332
                Diagnostics::makeContext(
420✔
2333
                    Diagnostics::ErrorLocation {
420✔
2334
                        .filename = filename,
210✔
2335
                        .start = FilePos { .line = maybe_location->line, .column = 0 },
210✔
2336
                        .end = std::nullopt,
210✔
2337
                        .maybe_content = std::nullopt },
210✔
2338
                    os,
210✔
2339
                    /* maybe_context= */ std::nullopt,
210✔
2340
                    /* colorize= */ colorize);
210✔
2341
            fmt::println(os, "");
212✔
2342
        }
212✔
2343

2344
        if (context.fc > 1)
212✔
2345
        {
2346
            // display call stack trace
2347
            const ScopeView old_scope = context.locals.back();
8✔
2348

2349
            std::string previous_trace;
8✔
2350
            std::size_t displayed_traces = 0;
8✔
2351
            std::size_t consecutive_similar_traces = 0;
8✔
2352

2353
            while (context.fc != 0 && context.pp != 0 && context.sp > 0)
2,062✔
2354
            {
2355
                const auto maybe_call_loc = findSourceLocation(context.ip, context.pp);
2,054✔
2356
                const auto loc_as_text = maybe_call_loc ? fmt::format(" ({}:{})", m_state.m_filenames[maybe_call_loc->filename_id], maybe_call_loc->line + 1) : "";
2,054✔
2357

2358
                const uint16_t id = findNearestVariableIdWithValue(
2,054✔
2359
                    Value(static_cast<PageAddr_t>(context.pp)),
2,054✔
2360
                    context);
2,054✔
2361
                const std::string& func_name = (id < m_state.m_symbols.size()) ? m_state.m_symbols[id] : "???";
2,054✔
2362

2363
                if (func_name + loc_as_text != previous_trace)
2,054✔
2364
                {
2365
                    fmt::println(
16✔
2366
                        os,
8✔
2367
                        "[{:4}] In function `{}'{}",
8✔
2368
                        fmt::styled(context.fc, colorize ? fmt::fg(fmt::color::cyan) : fmt::text_style()),
8✔
2369
                        fmt::styled(func_name, colorize ? fmt::fg(fmt::color::green) : fmt::text_style()),
8✔
2370
                        loc_as_text);
2371
                    previous_trace = func_name + loc_as_text;
8✔
2372
                    ++displayed_traces;
8✔
2373
                    consecutive_similar_traces = 0;
8✔
2374
                }
8✔
2375
                else if (consecutive_similar_traces == 0)
2,046✔
2376
                {
2377
                    fmt::println(os, "       ...");
1✔
2378
                    ++consecutive_similar_traces;
1✔
2379
                }
1✔
2380

2381
                const Value* ip;
2,054✔
2382
                do
2,061✔
2383
                {
2384
                    ip = popAndResolveAsPtr(context);
2,061✔
2385
                } while (ip->valueType() != ValueType::InstPtr);
2,061✔
2386

2387
                context.ip = ip->pageAddr();
2,054✔
2388
                context.pp = pop(context)->pageAddr();
2,054✔
2389
                returnFromFuncCall(context);
2,054✔
2390

2391
                if (displayed_traces > max_consecutive_traces)
2,054✔
2392
                {
UNCOV
2393
                    fmt::println(os, "       ...");
×
UNCOV
2394
                    break;
×
2395
                }
2396
            }
2,054✔
2397

2398
            if (context.pp == 0)
8✔
2399
            {
2400
                const auto maybe_call_loc = findSourceLocation(context.ip, context.pp);
8✔
2401
                const auto loc_as_text = maybe_call_loc ? fmt::format(" ({}:{})", m_state.m_filenames[maybe_call_loc->filename_id], maybe_call_loc->line + 1) : "";
8✔
2402
                fmt::println(os, "[{:4}] In global scope{}", fmt::styled(context.fc, colorize ? fmt::fg(fmt::color::cyan) : fmt::text_style()), loc_as_text);
8✔
2403
            }
8✔
2404

2405
            // display variables values in the current scope
2406
            fmt::println(os, "\nCurrent scope variables values:");
8✔
2407
            for (std::size_t i = 0, size = old_scope.size(); i < size; ++i)
9✔
2408
            {
2409
                fmt::println(
2✔
2410
                    os,
1✔
2411
                    "{} = {}",
1✔
2412
                    fmt::styled(m_state.m_symbols[old_scope.atPos(i).first], colorize ? fmt::fg(fmt::color::cyan) : fmt::text_style()),
1✔
2413
                    old_scope.atPos(i).second.toString(*this));
1✔
2414
            }
1✔
2415
        }
8✔
2416
    }
212✔
2417
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc