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

ArkScript-lang / Ark / 20377988123

19 Dec 2025 05:45PM UTC coverage: 92.242% (+1.6%) from 90.661%
20377988123

push

github

SuperFola
chore: addressing cppcheck recommandations

1 of 1 new or added line in 1 file covered. (100.0%)

137 existing lines in 6 files now uncovered.

8430 of 9139 relevant lines covered (92.24%)

245122.08 hits per line

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

72.37
/src/arkreactor/Compiler/BytecodeReader.cpp
1
#include <Ark/Compiler/BytecodeReader.hpp>
2

3
#include <Ark/Compiler/Instructions.hpp>
4
#include <Ark/Builtins/Builtins.hpp>
5

6
#include <unordered_map>
7
#include <Proxy/Picosha2.hpp>
8
#include <Ark/Compiler/Serialization/IEEE754Serializer.hpp>
9
#include <Ark/Compiler/Serialization/IntegerSerializer.hpp>
10
#include <fmt/core.h>
11
#include <fmt/color.h>
12

13
namespace Ark
14
{
15
    using namespace Ark::internal;
16

17
    void BytecodeReader::feed(const bytecode_t& bytecode)
524✔
18
    {
524✔
19
        m_bytecode = bytecode;
524✔
20
    }
524✔
21

22
    void BytecodeReader::feed(const std::string& file)
×
23
    {
×
24
        std::ifstream ifs(file, std::ios::binary | std::ios::ate);
×
25
        if (!ifs.good())
×
26
            throw std::runtime_error(fmt::format("[BytecodeReader] Couldn't open file '{}'", file));
×
27

28
        const auto pos = ifs.tellg();
×
29
        // reserve appropriate number of bytes
30
        std::vector<char> temp(static_cast<std::size_t>(pos));
×
31
        ifs.seekg(0, std::ios::beg);
×
32
        ifs.read(&temp[0], pos);
×
33
        ifs.close();
×
34

35
        m_bytecode = bytecode_t(static_cast<std::size_t>(pos));
×
36
        for (std::size_t i = 0; i < static_cast<std::size_t>(pos); ++i)
×
37
            m_bytecode[i] = static_cast<uint8_t>(temp[i]);
×
38
    }
×
39

40
    bool BytecodeReader::checkMagic() const
2,009✔
41
    {
2,009✔
42
        return m_bytecode.size() >= bytecode::Magic.size() &&
4,017✔
43
            m_bytecode[0] == bytecode::Magic[0] &&
2,008✔
44
            m_bytecode[1] == bytecode::Magic[1] &&
1,699✔
45
            m_bytecode[2] == bytecode::Magic[2] &&
3,398✔
46
            m_bytecode[3] == bytecode::Magic[3];
1,699✔
47
    }
×
48

49
    Version BytecodeReader::version() const
213✔
50
    {
213✔
51
        if (!checkMagic() || m_bytecode.size() < bytecode::Magic.size() + bytecode::Version.size())
213✔
52
            return Version { 0, 0, 0 };
×
53

54
        return Version {
852✔
55
            .major = static_cast<uint16_t>((m_bytecode[4] << 8) + m_bytecode[5]),
213✔
56
            .minor = static_cast<uint16_t>((m_bytecode[6] << 8) + m_bytecode[7]),
213✔
57
            .patch = static_cast<uint16_t>((m_bytecode[8] << 8) + m_bytecode[9])
213✔
58
        };
59
    }
213✔
60

61
    unsigned long long BytecodeReader::timestamp() const
1✔
62
    {
1✔
63
        // 4 (ark\0) + version (2 bytes / number) + timestamp = 18 bytes
64
        if (!checkMagic() || m_bytecode.size() < bytecode::HeaderSize)
1✔
65
            return 0;
×
66

67
        // reading the timestamp in big endian
68
        using timestamp_t = unsigned long long;
×
69
        return (static_cast<timestamp_t>(m_bytecode[10]) << 56) +
3✔
70
            (static_cast<timestamp_t>(m_bytecode[11]) << 48) +
2✔
71
            (static_cast<timestamp_t>(m_bytecode[12]) << 40) +
2✔
72
            (static_cast<timestamp_t>(m_bytecode[13]) << 32) +
2✔
73
            (static_cast<timestamp_t>(m_bytecode[14]) << 24) +
2✔
74
            (static_cast<timestamp_t>(m_bytecode[15]) << 16) +
2✔
75
            (static_cast<timestamp_t>(m_bytecode[16]) << 8) +
2✔
76
            static_cast<timestamp_t>(m_bytecode[17]);
1✔
77
    }
1✔
78

79
    std::vector<unsigned char> BytecodeReader::sha256() const
212✔
80
    {
212✔
81
        if (!checkMagic() || m_bytecode.size() < bytecode::HeaderSize + picosha2::k_digest_size)
212✔
82
            return {};
×
83

84
        std::vector<unsigned char> sha(picosha2::k_digest_size);
212✔
85
        for (std::size_t i = 0; i < picosha2::k_digest_size; ++i)
6,996✔
86
            sha[i] = m_bytecode[bytecode::HeaderSize + i];
6,784✔
87
        return sha;
212✔
88
    }
424✔
89

90
    Symbols BytecodeReader::symbols() const
212✔
91
    {
212✔
92
        if (!checkMagic() || m_bytecode.size() < bytecode::HeaderSize + picosha2::k_digest_size ||
424✔
93
            m_bytecode[bytecode::HeaderSize + picosha2::k_digest_size] != SYM_TABLE_START)
212✔
94
            return {};
×
95

96
        std::size_t i = bytecode::HeaderSize + picosha2::k_digest_size + 1;
212✔
97
        const uint16_t size = readNumber(i);
212✔
98
        i++;
212✔
99

100
        Symbols block;
212✔
101
        block.start = bytecode::HeaderSize + picosha2::k_digest_size;
212✔
102
        block.symbols.reserve(size);
212✔
103

104
        for (uint16_t j = 0; j < size; ++j)
7,499✔
105
        {
106
            std::string content;
7,287✔
107
            while (m_bytecode[i] != 0)
83,185✔
108
                content.push_back(static_cast<char>(m_bytecode[i++]));
75,898✔
109
            i++;
7,287✔
110

111
            block.symbols.push_back(content);
7,287✔
112
        }
7,287✔
113

114
        block.end = i;
212✔
115
        return block;
212✔
116
    }
212✔
117

118
    Values BytecodeReader::values(const Symbols& symbols) const
212✔
119
    {
212✔
120
        if (!checkMagic())
212✔
121
            return {};
×
122

123
        std::size_t i = symbols.end;
212✔
124
        if (m_bytecode[i] != VAL_TABLE_START)
212✔
125
            return {};
×
126
        i++;
212✔
127

128
        const uint16_t size = readNumber(i);
212✔
129
        i++;
212✔
130
        Values block;
212✔
131
        block.start = symbols.end;
212✔
132
        block.values.reserve(size);
212✔
133

134
        for (uint16_t j = 0; j < size; ++j)
7,837✔
135
        {
136
            const uint8_t type = m_bytecode[i];
7,625✔
137
            i++;
7,625✔
138

139
            if (type == NUMBER_TYPE)
7,625✔
140
            {
141
                auto exp = deserializeLE<decltype(ieee754::DecomposedDouble::exponent)>(
1,886✔
142
                    m_bytecode.begin() + static_cast<std::vector<uint8_t>::difference_type>(i), m_bytecode.end());
943✔
143
                i += sizeof(decltype(exp));
943✔
144
                auto mant = deserializeLE<decltype(ieee754::DecomposedDouble::mantissa)>(
1,886✔
145
                    m_bytecode.begin() + static_cast<std::vector<uint8_t>::difference_type>(i), m_bytecode.end());
943✔
146
                i += sizeof(decltype(mant));
943✔
147

148
                const ieee754::DecomposedDouble d { exp, mant };
943✔
149
                double val = ieee754::deserialize(d);
943✔
150
                block.values.emplace_back(val);
943✔
151
            }
943✔
152
            else if (type == STRING_TYPE)
6,682✔
153
            {
154
                std::string val;
2,928✔
155
                while (m_bytecode[i] != 0)
55,907✔
156
                    val.push_back(static_cast<char>(m_bytecode[i++]));
52,979✔
157
                block.values.emplace_back(val);
2,928✔
158
            }
2,928✔
159
            else if (type == FUNC_TYPE)
3,754✔
160
            {
161
                const uint16_t addr = readNumber(i);
3,754✔
162
                i++;
3,754✔
163
                block.values.emplace_back(addr);
3,754✔
164
            }
3,754✔
165
            else
166
                throw std::runtime_error(fmt::format("Unknown value type: {:x}", type));
×
167
            i++;
7,625✔
168
        }
7,625✔
169

170
        block.end = i;
212✔
171
        return block;
212✔
172
    }
212✔
173

174
    Filenames BytecodeReader::filenames(const Ark::Values& values) const
212✔
175
    {
212✔
176
        if (!checkMagic())
212✔
177
            return {};
×
178

179
        std::size_t i = values.end;
212✔
180
        if (m_bytecode[i] != FILENAMES_TABLE_START)
212✔
181
            return {};
×
182
        i++;
212✔
183

184
        const uint16_t size = readNumber(i);
212✔
185
        i++;
212✔
186

187
        Filenames block;
212✔
188
        block.start = values.end;
212✔
189
        block.filenames.reserve(size);
212✔
190

191
        for (uint16_t j = 0; j < size; ++j)
567✔
192
        {
193
            std::string val;
355✔
194
            while (m_bytecode[i] != 0)
27,322✔
195
                val.push_back(static_cast<char>(m_bytecode[i++]));
26,967✔
196
            block.filenames.emplace_back(val);
355✔
197
            i++;
355✔
198
        }
355✔
199

200
        block.end = i;
212✔
201
        return block;
212✔
202
    }
212✔
203

204
    InstLocations BytecodeReader::instLocations(const Ark::Filenames& filenames) const
212✔
205
    {
212✔
206
        if (!checkMagic())
212✔
207
            return {};
×
208

209
        std::size_t i = filenames.end;
212✔
210
        if (m_bytecode[i] != INST_LOC_TABLE_START)
212✔
211
            return {};
×
212
        i++;
212✔
213

214
        const uint16_t size = readNumber(i);
212✔
215
        i++;
212✔
216

217
        InstLocations block;
212✔
218
        block.start = filenames.end;
212✔
219
        block.locations.reserve(size);
212✔
220

221
        for (uint16_t j = 0; j < size; ++j)
27,856✔
222
        {
223
            auto pp = readNumber(i);
27,644✔
224
            i++;
27,644✔
225

226
            auto ip = readNumber(i);
27,644✔
227
            i++;
27,644✔
228

229
            auto file_id = readNumber(i);
27,644✔
230
            i++;
27,644✔
231

232
            auto line = deserializeBE<uint32_t>(
55,288✔
233
                m_bytecode.begin() + static_cast<std::vector<uint8_t>::difference_type>(i), m_bytecode.end());
27,644✔
234
            i += 4;
27,644✔
235

236
            block.locations.push_back(
27,644✔
237
                { .page_pointer = pp,
110,576✔
238
                  .inst_pointer = ip,
27,644✔
239
                  .filename_id = file_id,
27,644✔
240
                  .line = line });
27,644✔
241
        }
27,644✔
242

243
        block.end = i;
212✔
244
        return block;
212✔
245
    }
212✔
246

247
    Code BytecodeReader::code(const InstLocations& instLocations) const
212✔
248
    {
212✔
249
        if (!checkMagic())
212✔
250
            return {};
×
251

252
        std::size_t i = instLocations.end;
212✔
253

254
        Code block;
212✔
255
        block.start = i;
212✔
256

257
        while (m_bytecode[i] == CODE_SEGMENT_START)
3,966✔
258
        {
259
            i++;
3,966✔
260
            const std::size_t size = readNumber(i) * 4;
3,966✔
261
            i++;
3,966✔
262

263
            block.pages.emplace_back().reserve(size);
3,966✔
264
            for (std::size_t j = 0; j < size; ++j)
389,846✔
265
                block.pages.back().push_back(m_bytecode[i++]);
385,880✔
266

267
            if (i == m_bytecode.size())
3,966✔
268
                break;
212✔
269
        }
3,966✔
270

271
        return block;
212✔
272
    }
212✔
273

274
    std::optional<InstLoc> BytecodeReader::findSourceLocation(const std::vector<InstLoc>& inst_locations, const std::size_t ip, const std::size_t pp) const
9✔
275
    {
9✔
276
        std::optional<InstLoc> match = std::nullopt;
9✔
277

278
        for (const auto location : inst_locations)
12,875✔
279
        {
280
            if (location.page_pointer == pp && !match)
12,866✔
281
                match = location;
9✔
282

283
            // select the best match: we want to find the location that's nearest our instruction pointer,
284
            // but not equal to it as the IP will always be pointing to the next instruction,
285
            // not yet executed. Thus, the erroneous instruction is the previous one.
286
            if (location.page_pointer == pp && match && location.inst_pointer < ip / 4)
12,866✔
287
                match = location;
5✔
288

289
            // early exit because we won't find anything better, as inst locations are ordered by ascending (pp, ip)
290
            if (location.page_pointer > pp || (location.page_pointer == pp && location.inst_pointer >= ip / 4))
12,866✔
291
                break;
9✔
292
        }
12,866✔
293

294
        return match;
9✔
295
    }
296

297
    void BytecodeReader::display(const BytecodeSegment segment,
1✔
298
                                 const std::optional<uint16_t> sStart,
299
                                 const std::optional<uint16_t> sEnd,
300
                                 const std::optional<uint16_t> cPage) const
301
    {
1✔
302
        if (!checkMagic())
1✔
303
        {
304
            fmt::println("Invalid format");
×
305
            return;
×
306
        }
307

308
        if (segment == BytecodeSegment::All || segment == BytecodeSegment::HeadersOnly)
1✔
309
        {
310
            auto [major, minor, patch] = version();
×
311
            fmt::println("Version:   {}.{}.{}", major, minor, patch);
×
312
            fmt::println("Timestamp: {}", timestamp());
×
313
            fmt::print("SHA256:    ");
×
314
            for (const auto sha = sha256(); unsigned char h : sha)
×
315
                fmt::print("{:02x}", h);
×
316
            fmt::print("\n\n");
×
317
        }
×
318

319
        // reading the different tables, one after another
320

321
        if ((sStart.has_value() && !sEnd.has_value()) || (!sStart.has_value() && sEnd.has_value()))
1✔
322
        {
323
            fmt::print(fmt::fg(fmt::color::red), "Both start and end parameter need to be provided together\n");
×
324
            return;
×
325
        }
326
        if (sStart.has_value() && sEnd.has_value() && sStart.value() >= sEnd.value())
1✔
327
        {
328
            fmt::print(fmt::fg(fmt::color::red), "Invalid slice start and end arguments\n");
×
329
            return;
×
330
        }
331

332
        const auto syms = symbols();
1✔
333
        const auto vals = values(syms);
1✔
334
        const auto files = filenames(vals);
1✔
335
        const auto inst_locs = instLocations(files);
1✔
336
        const auto code_block = code(inst_locs);
1✔
337

338
        // symbols table
339
        {
340
            std::size_t size = syms.symbols.size();
1✔
341
            std::size_t sliceSize = size;
1✔
342
            bool showSym = (segment == BytecodeSegment::All || segment == BytecodeSegment::Symbols);
1✔
343

344
            if (showSym && sStart.has_value() && sEnd.has_value() && (sStart.value() > size || sEnd.value() > size))
1✔
345
                fmt::print(fmt::fg(fmt::color::red), "Slice start or end can't be greater than the segment size: {}\n", size);
×
346
            else if (showSym && sStart.has_value() && sEnd.has_value())
1✔
347
                sliceSize = sEnd.value() - sStart.value() + 1;
×
348

349
            if (showSym || segment == BytecodeSegment::HeadersOnly)
1✔
350
                fmt::println("{} (length: {})", fmt::styled("Symbols table", fmt::fg(fmt::color::cyan)), sliceSize);
×
351

352
            for (std::size_t j = 0; j < size; ++j)
366✔
353
            {
354
                if (auto start = sStart; auto end = sEnd)
365✔
355
                    showSym = showSym && (j >= start.value() && j <= end.value());
×
356

357
                if (showSym)
365✔
358
                    fmt::println("{}) {}", j, syms.symbols[j]);
×
359
            }
365✔
360

361
            if (showSym)
1✔
362
                fmt::print("\n");
×
363
            if (segment == BytecodeSegment::Symbols)
1✔
364
                return;
×
365
        }
1✔
366

367
        // values table
368
        {
369
            std::size_t size = vals.values.size();
1✔
370
            std::size_t sliceSize = size;
1✔
371

372
            bool showVal = (segment == BytecodeSegment::All || segment == BytecodeSegment::Values);
1✔
373
            if (showVal && sStart.has_value() && sEnd.has_value() && (sStart.value() > size || sEnd.value() > size))
1✔
374
                fmt::print(fmt::fg(fmt::color::red), "Slice start or end can't be greater than the segment size: {}\n", size);
×
375
            else if (showVal && sStart.has_value() && sEnd.has_value())
1✔
376
                sliceSize = sEnd.value() - sStart.value() + 1;
×
377

378
            if (showVal || segment == BytecodeSegment::HeadersOnly)
1✔
379
                fmt::println("{} (length: {})", fmt::styled("Constants table", fmt::fg(fmt::color::cyan)), sliceSize);
×
380

381
            for (std::size_t j = 0; j < size; ++j)
763✔
382
            {
383
                if (auto start = sStart; auto end = sEnd)
762✔
384
                    showVal = showVal && (j >= start.value() && j <= end.value());
×
385

386
                if (showVal)
762✔
387
                {
388
                    switch (const auto val = vals.values[j]; val.valueType())
×
389
                    {
×
390
                        case ValueType::Number:
391
                            fmt::println("{}) (Number) {}", j, val.number());
×
392
                            break;
×
393
                        case ValueType::String:
394
                            fmt::println("{}) (String) {}", j, val.string());
×
395
                            break;
×
396
                        case ValueType::PageAddr:
397
                            fmt::println("{}) (PageAddr) {}", j, val.pageAddr());
×
398
                            break;
×
399
                        default:
400
                            fmt::print(fmt::fg(fmt::color::red), "Value type not handled: {}\n", std::to_string(val.valueType()));
×
401
                            break;
×
402
                    }
×
403
                }
×
404
            }
762✔
405

406
            if (showVal)
1✔
407
                fmt::print("\n");
×
408
            if (segment == BytecodeSegment::Values)
1✔
409
                return;
×
410
        }
1✔
411

412
        // inst locs + file
413
        {
414
            std::size_t size = inst_locs.locations.size();
1✔
415
            std::size_t sliceSize = size;
1✔
416

417
            bool showVal = (segment == BytecodeSegment::All || segment == BytecodeSegment::InstructionLocation);
1✔
418
            if (showVal && sStart.has_value() && sEnd.has_value() && (sStart.value() > size || sEnd.value() > size))
1✔
419
                fmt::print(fmt::fg(fmt::color::red), "Slice start or end can't be greater than the segment size: {}\n", size);
×
420
            else if (showVal && sStart.has_value() && sEnd.has_value())
1✔
421
                sliceSize = sEnd.value() - sStart.value() + 1;
×
422

423
            if (showVal || segment == BytecodeSegment::HeadersOnly)
1✔
424
                fmt::println("{} (length: {})", fmt::styled("Instruction locations table", fmt::fg(fmt::color::cyan)), sliceSize);
×
425
            if (showVal && size > 0)
1✔
426
                fmt::println(" PP, IP");
×
427

428
            for (std::size_t j = 0; j < size; ++j)
2,854✔
429
            {
430
                if (auto start = sStart; auto end = sEnd)
2,853✔
431
                    showVal = showVal && (j >= start.value() && j <= end.value());
×
432

433
                const auto& location = inst_locs.locations[j];
2,853✔
434
                if (showVal)
2,853✔
435
                    fmt::println("{:>3},{:>3} -> {}:{}", location.page_pointer, location.inst_pointer, files.filenames[location.filename_id], location.line);
×
436
            }
2,853✔
437

438
            if (showVal)
1✔
439
                fmt::print("\n");
×
440
        }
1✔
441

442
        const auto stringify_value = [](const Value& val) -> std::string {
1✔
443
            switch (val.valueType())
×
444
            {
×
445
                case ValueType::Number:
446
                    return fmt::format("{} (Number)", val.number());
×
447
                case ValueType::String:
448
                    return fmt::format("{} (String)", val.string());
×
449
                case ValueType::PageAddr:
450
                    return fmt::format("{} (PageAddr)", val.pageAddr());
×
451
                default:
452
                    return "";
×
453
            }
454
        };
×
455

456
        enum class ArgKind
457
        {
458
            Symbol,
459
            Constant,
460
            Builtin,
461
            Raw,  ///< eg: Stack index, jump address, number
462
            ConstConst,
463
            ConstSym,
464
            SymConst,
465
            SymSym,
466
            BuiltinRaw,  ///< Builtin, number
467
            ConstRaw,    ///< Constant, number
468
            SymRaw,      ///< Symbol, number
469
            RawSym,      ///< Symbol index, symbol
470
            RawConst,    ///< Symbol index, constant
471
            RawRaw,      ///< Symbol index, symbol index
472
            RawRawRaw
473
        };
474

475
        struct Arg
476
        {
477
            ArgKind kind;
478
            uint8_t padding;
479
            uint16_t arg;
480

481
            [[nodiscard]] uint16_t primary() const
×
482
            {
×
UNCOV
483
                return arg & 0x0fff;
×
484
            }
485

486
            [[nodiscard]] uint16_t secondary() const
×
487
            {
×
UNCOV
488
                return static_cast<uint16_t>((padding << 4) | (arg & 0xf000) >> 12);
×
489
            }
490
        };
491

492
        const std::unordered_map<Instruction, ArgKind> arg_kinds = {
1✔
493
            { LOAD_SYMBOL, ArgKind::Symbol },
494
            { LOAD_SYMBOL_BY_INDEX, ArgKind::Raw },
495
            { LOAD_CONST, ArgKind::Constant },
496
            { POP_JUMP_IF_TRUE, ArgKind::Raw },
497
            { STORE, ArgKind::Symbol },
498
            { STORE_REF, ArgKind::Symbol },
499
            { SET_VAL, ArgKind::Symbol },
500
            { POP_JUMP_IF_FALSE, ArgKind::Raw },
501
            { JUMP, ArgKind::Raw },
502
            { CALL, ArgKind::Raw },
503
            { CAPTURE, ArgKind::Symbol },
504
            { RENAME_NEXT_CAPTURE, ArgKind::Symbol },
505
            { BUILTIN, ArgKind::Builtin },
506
            { DEL, ArgKind::Symbol },
507
            { MAKE_CLOSURE, ArgKind::Constant },
508
            { GET_FIELD, ArgKind::Symbol },
509
            { PLUGIN, ArgKind::Constant },
510
            { LIST, ArgKind::Raw },
511
            { APPEND, ArgKind::Raw },
512
            { CONCAT, ArgKind::Raw },
513
            { APPEND_IN_PLACE, ArgKind::Raw },
514
            { CONCAT_IN_PLACE, ArgKind::Raw },
515
            { RESET_SCOPE_JUMP, ArgKind::Raw },
516
            { GET_CURRENT_PAGE_ADDR, ArgKind::Symbol },
517
            { LOAD_CONST_LOAD_CONST, ArgKind::ConstConst },
518
            { LOAD_CONST_STORE, ArgKind::ConstSym },
519
            { LOAD_CONST_SET_VAL, ArgKind::ConstSym },
520
            { STORE_FROM, ArgKind::SymSym },
521
            { STORE_FROM_INDEX, ArgKind::RawSym },
522
            { SET_VAL_FROM, ArgKind::SymSym },
523
            { SET_VAL_FROM_INDEX, ArgKind::RawSym },
524
            { INCREMENT, ArgKind::SymRaw },
525
            { INCREMENT_BY_INDEX, ArgKind::RawRaw },
526
            { INCREMENT_STORE, ArgKind::RawRaw },
527
            { DECREMENT, ArgKind::SymRaw },
528
            { DECREMENT_BY_INDEX, ArgKind::RawRaw },
529
            { DECREMENT_STORE, ArgKind::SymRaw },
530
            { STORE_TAIL, ArgKind::SymSym },
531
            { STORE_TAIL_BY_INDEX, ArgKind::RawSym },
532
            { STORE_HEAD, ArgKind::SymSym },
533
            { STORE_HEAD_BY_INDEX, ArgKind::RawSym },
534
            { STORE_LIST, ArgKind::RawSym },
535
            { SET_VAL_TAIL, ArgKind::SymSym },
536
            { SET_VAL_TAIL_BY_INDEX, ArgKind::RawSym },
537
            { SET_VAL_HEAD, ArgKind::SymSym },
538
            { SET_VAL_HEAD_BY_INDEX, ArgKind::RawSym },
539
            { CALL_BUILTIN, ArgKind::BuiltinRaw },
540
            { CALL_BUILTIN_WITHOUT_RETURN_ADDRESS, ArgKind::BuiltinRaw },
541
            { LT_CONST_JUMP_IF_FALSE, ArgKind::ConstRaw },
542
            { LT_CONST_JUMP_IF_TRUE, ArgKind::ConstRaw },
543
            { LT_SYM_JUMP_IF_FALSE, ArgKind::SymRaw },
544
            { GT_CONST_JUMP_IF_TRUE, ArgKind::ConstRaw },
545
            { GT_CONST_JUMP_IF_FALSE, ArgKind::ConstRaw },
546
            { GT_SYM_JUMP_IF_FALSE, ArgKind::SymRaw },
547
            { EQ_CONST_JUMP_IF_TRUE, ArgKind::ConstRaw },
548
            { EQ_SYM_INDEX_JUMP_IF_TRUE, ArgKind::SymRaw },
549
            { NEQ_CONST_JUMP_IF_TRUE, ArgKind::ConstRaw },
550
            { NEQ_SYM_JUMP_IF_FALSE, ArgKind::SymRaw },
551
            { CALL_SYMBOL, ArgKind::SymRaw },
552
            { CALL_CURRENT_PAGE, ArgKind::SymRaw },
553
            { GET_FIELD_FROM_SYMBOL, ArgKind::SymSym },
554
            { GET_FIELD_FROM_SYMBOL_INDEX, ArgKind::RawSym },
555
            { AT_SYM_SYM, ArgKind::SymSym },
556
            { AT_SYM_INDEX_SYM_INDEX, ArgKind::RawRaw },
557
            { AT_SYM_INDEX_CONST, ArgKind::RawConst },
558
            { CHECK_TYPE_OF, ArgKind::SymConst },
559
            { CHECK_TYPE_OF_BY_INDEX, ArgKind::RawConst },
560
            { APPEND_IN_PLACE_SYM, ArgKind::SymRaw },
561
            { APPEND_IN_PLACE_SYM_INDEX, ArgKind::RawRaw },
562
            { STORE_LEN, ArgKind::RawSym },
563
            { LT_LEN_SYM_JUMP_IF_FALSE, ArgKind::SymRaw },
564
            { MUL_BY, ArgKind::RawRaw },
565
            { MUL_BY_INDEX, ArgKind::RawRaw },
566
            { MUL_SET_VAL, ArgKind::RawRaw },
567
            { FUSED_MATH, ArgKind::RawRawRaw }
568
        };
569

570
        const auto builtin_name = [](const uint16_t idx) {
1✔
571
            return Builtins::builtins[idx].first;
×
572
        };
573
        const auto value_str = [&stringify_value, &vals](const uint16_t idx) {
1✔
574
            return stringify_value(vals.values[idx]);
×
575
        };
576
        const auto symbol_name = [&syms](const uint16_t idx) {
4✔
577
            return syms.symbols[idx];
3✔
578
        };
579

580
        const auto color_print_inst = [=](const std::string& name, std::optional<Arg> arg = std::nullopt) {
10✔
581
            fmt::print("{}", fmt::styled(name, fmt::fg(fmt::color::gold)));
9✔
582
            if (arg.has_value())
9✔
583
            {
584
                constexpr auto sym_color = fmt::fg(fmt::color::green);
7✔
585
                constexpr auto const_color = fmt::fg(fmt::color::magenta);
7✔
586
                constexpr auto raw_color = fmt::fg(fmt::color::red);
7✔
587

588
                switch (auto [kind, _, idx] = arg.value(); kind)
10✔
589
                {
3✔
590
                    case ArgKind::Symbol:
591
                        fmt::print(sym_color, " {}\n", symbol_name(idx));
3✔
592
                        break;
3✔
593
                    case ArgKind::Constant:
UNCOV
594
                        fmt::print(const_color, " {}\n", value_str(idx));
×
595
                        break;
×
596
                    case ArgKind::Builtin:
UNCOV
597
                        fmt::print(" {}\n", builtin_name(idx));
×
598
                        break;
3✔
599
                    case ArgKind::Raw:
600
                        fmt::print(raw_color, " ({})\n", idx);
6✔
601
                        break;
3✔
602
                    case ArgKind::ConstConst:
UNCOV
603
                        fmt::print(" {}, {}\n", fmt::styled(value_str(arg->primary()), const_color), fmt::styled(value_str(arg->secondary()), const_color));
×
604
                        break;
×
605
                    case ArgKind::ConstSym:
UNCOV
606
                        fmt::print(" {}, {}\n", fmt::styled(value_str(arg->primary()), const_color), fmt::styled(symbol_name(arg->secondary()), sym_color));
×
607
                        break;
×
608
                    case ArgKind::SymConst:
UNCOV
609
                        fmt::print(" {}, {}\n", fmt::styled(symbol_name(arg->primary()), sym_color), fmt::styled(value_str(arg->secondary()), const_color));
×
610
                        break;
×
611
                    case ArgKind::SymSym:
UNCOV
612
                        fmt::print(" {}, {}\n", fmt::styled(symbol_name(arg->primary()), sym_color), fmt::styled(symbol_name(arg->secondary()), sym_color));
×
613
                        break;
×
614
                    case ArgKind::BuiltinRaw:
UNCOV
615
                        fmt::print(" {}, {}\n", builtin_name(arg->primary()), fmt::styled(arg->secondary(), raw_color));
×
616
                        break;
×
617
                    case ArgKind::ConstRaw:
UNCOV
618
                        fmt::print(" {}, {}\n", fmt::styled(value_str(arg->primary()), const_color), fmt::styled(arg->secondary(), raw_color));
×
619
                        break;
×
620
                    case ArgKind::SymRaw:
UNCOV
621
                        fmt::print(" {}, {}\n", fmt::styled(symbol_name(arg->primary()), sym_color), fmt::styled(arg->secondary(), raw_color));
×
622
                        break;
×
623
                    case ArgKind::RawSym:
UNCOV
624
                        fmt::print(" {}, {}\n", fmt::styled(arg->primary(), raw_color), fmt::styled(symbol_name(arg->secondary()), sym_color));
×
625
                        break;
×
626
                    case ArgKind::RawConst:
UNCOV
627
                        fmt::print(" {}, {}\n", fmt::styled(arg->primary(), raw_color), fmt::styled(value_str(arg->secondary()), const_color));
×
628
                        break;
×
629
                    case ArgKind::RawRaw:
630
                        fmt::print(" {}, {}\n", fmt::styled(arg->primary(), raw_color), fmt::styled(arg->secondary(), raw_color));
×
631
                        break;
1✔
632
                    case ArgKind::RawRawRaw:
633
                        fmt::print(" {}, {}, {}\n", fmt::styled(arg->padding, raw_color), fmt::styled((arg->arg & 0xff00) >> 8, raw_color), fmt::styled(arg->arg & 0x00ff, raw_color));
1✔
634
                        break;
1✔
635
                }
7✔
636
            }
7✔
637
            else
638
                fmt::print("\n");
2✔
639
        };
9✔
640

641
        if (segment == BytecodeSegment::All || segment == BytecodeSegment::Code || segment == BytecodeSegment::HeadersOnly)
1✔
642
        {
643
            uint16_t pp = 0;
1✔
644

645
            for (const auto& page : code_block.pages)
176✔
646
            {
647
                bool displayCode = true;
175✔
648

649
                if (auto wanted_page = cPage)
350✔
650
                    displayCode = pp == wanted_page.value();
175✔
651

652
                if (displayCode)
175✔
653
                    fmt::println(
1✔
654
                        "{} {} (length: {})",
1✔
655
                        fmt::styled("Code segment", fmt::fg(fmt::color::magenta)),
1✔
656
                        fmt::styled(pp, fmt::fg(fmt::color::magenta)),
1✔
657
                        page.size());
1✔
658

659
                if (page.empty())
175✔
660
                {
661
                    if (displayCode)
×
662
                        fmt::print("NOP");
×
663
                }
×
664
                else if (cPage.value_or(pp) == pp)
175✔
665
                {
666
                    if (segment == BytecodeSegment::HeadersOnly)
1✔
UNCOV
667
                        continue;
×
668
                    if (sStart.has_value() && sEnd.has_value() && ((sStart.value() > page.size()) || (sEnd.value() > page.size())))
1✔
669
                    {
UNCOV
670
                        fmt::print(fmt::fg(fmt::color::red), "Slice start or end can't be greater than the segment size: {}\n", page.size());
×
671
                        return;
×
672
                    }
673

674
                    std::optional<InstLoc> previous_loc = std::nullopt;
1✔
675

676
                    for (std::size_t j = sStart.value_or(0), end = sEnd.value_or(page.size()); j < end; j += 4)
10✔
677
                    {
678
                        const uint8_t inst = page[j];
9✔
679
                        const uint8_t padding = page[j + 1];
9✔
680
                        const auto arg = static_cast<uint16_t>((page[j + 2] << 8) + page[j + 3]);
9✔
681

682
                        auto maybe_loc = findSourceLocation(inst_locs.locations, j, pp);
9✔
683

684
                        // location
685
                        // we want to print it only when it changed, either the file, the line, or both
686
                        if (maybe_loc && (!previous_loc || maybe_loc != previous_loc))
9✔
687
                        {
688
                            if (!previous_loc || previous_loc->filename_id != maybe_loc->filename_id)
1✔
689
                                fmt::println("{}", files.filenames[maybe_loc->filename_id]);
1✔
690
                            fmt::print("{:>4}", maybe_loc->line + 1);
1✔
691
                            previous_loc = maybe_loc;
1✔
692
                        }
1✔
693
                        else
694
                            fmt::print("    ");
8✔
695
                        // instruction number
696
                        fmt::print(fmt::fg(fmt::color::cyan), "{:>4}", j / 4);
9✔
697
                        // padding inst arg arg
698
                        fmt::print(" {:02x} {:02x} {:02x} {:02x} ", inst, padding, page[j + 2], page[j + 3]);
9✔
699

700
                        if (const auto idx = static_cast<std::size_t>(inst); idx < InstructionNames.size())
18✔
701
                        {
702
                            const auto inst_name = InstructionNames[idx];
9✔
703
                            if (const auto iinst = static_cast<Instruction>(inst); arg_kinds.contains(iinst))
18✔
704
                                color_print_inst(inst_name, Arg { arg_kinds.at(iinst), padding, arg });
7✔
705
                            else
706
                                color_print_inst(inst_name);
2✔
707
                        }
9✔
708
                        else
UNCOV
709
                            fmt::println("Unknown instruction");
×
710
                    }
9✔
711
                }
1✔
712
                if (displayCode && segment != BytecodeSegment::HeadersOnly)
175✔
713
                    fmt::print("\n");
1✔
714

715
                ++pp;
175✔
716
            }
175✔
717
        }
1✔
718
    }
1✔
719

720
    uint16_t BytecodeReader::readNumber(std::size_t& i) const
91,500✔
721
    {
91,500✔
722
        const auto x = static_cast<uint16_t>(m_bytecode[i] << 8);
91,500✔
723
        const uint16_t y = m_bytecode[++i];
91,500✔
724
        return x + y;
183,000✔
725
    }
91,500✔
726
}
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