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

ArkScript-lang / Ark / 20065764942

09 Dec 2025 01:49PM UTC coverage: 90.564% (+0.008%) from 90.556%
20065764942

push

github

SuperFola
chore(tests): adding IR generation tests for arg attributes

8043 of 8881 relevant lines covered (90.56%)

180911.91 hits per line

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

41.18
/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)
506✔
18
    {
506✔
19
        m_bytecode = bytecode;
506✔
20
    }
506✔
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
1,930✔
41
    {
1,930✔
42
        return m_bytecode.size() >= bytecode::Magic.size() &&
3,859✔
43
            m_bytecode[0] == bytecode::Magic[0] &&
1,929✔
44
            m_bytecode[1] == bytecode::Magic[1] &&
1,629✔
45
            m_bytecode[2] == bytecode::Magic[2] &&
3,258✔
46
            m_bytecode[3] == bytecode::Magic[3];
1,629✔
47
    }
×
48

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

54
        return Version {
820✔
55
            .major = static_cast<uint16_t>((m_bytecode[4] << 8) + m_bytecode[5]),
205✔
56
            .minor = static_cast<uint16_t>((m_bytecode[6] << 8) + m_bytecode[7]),
205✔
57
            .patch = static_cast<uint16_t>((m_bytecode[8] << 8) + m_bytecode[9])
205✔
58
        };
59
    }
205✔
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
204✔
80
    {
204✔
81
        if (!checkMagic() || m_bytecode.size() < bytecode::HeaderSize + picosha2::k_digest_size)
204✔
82
            return {};
×
83

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

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

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

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

104
        for (uint16_t j = 0; j < size; ++j)
6,131✔
105
        {
106
            std::string content;
5,928✔
107
            while (m_bytecode[i] != 0)
65,403✔
108
                content.push_back(static_cast<char>(m_bytecode[i++]));
59,475✔
109
            i++;
5,928✔
110

111
            block.symbols.push_back(content);
5,928✔
112
        }
5,928✔
113

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

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

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

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

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

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

148
                const ieee754::DecomposedDouble d { exp, mant };
817✔
149
                double val = ieee754::deserialize(d);
817✔
150
                block.values.emplace_back(val);
817✔
151
            }
817✔
152
            else if (type == STRING_TYPE)
5,195✔
153
            {
154
                std::string val;
2,080✔
155
                while (m_bytecode[i] != 0)
41,273✔
156
                    val.push_back(static_cast<char>(m_bytecode[i++]));
39,193✔
157
                block.values.emplace_back(val);
2,080✔
158
            }
2,080✔
159
            else if (type == FUNC_TYPE)
3,115✔
160
            {
161
                const uint16_t addr = readNumber(i);
3,115✔
162
                i++;
3,115✔
163
                block.values.emplace_back(addr);
3,115✔
164
            }
3,115✔
165
            else
166
                throw std::runtime_error(fmt::format("Unknown value type: {:x}", type));
×
167
            i++;
6,012✔
168
        }
6,012✔
169

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

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

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

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

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

191
        for (uint16_t j = 0; j < size; ++j)
537✔
192
        {
193
            std::string val;
334✔
194
            while (m_bytecode[i] != 0)
25,551✔
195
                val.push_back(static_cast<char>(m_bytecode[i++]));
25,217✔
196
            block.filenames.emplace_back(val);
334✔
197
            i++;
334✔
198
        }
334✔
199

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

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

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

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

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

221
        for (uint16_t j = 0; j < size; ++j)
20,637✔
222
        {
223
            auto pp = readNumber(i);
20,434✔
224
            i++;
20,434✔
225

226
            auto ip = readNumber(i);
20,434✔
227
            i++;
20,434✔
228

229
            auto file_id = readNumber(i);
20,434✔
230
            i++;
20,434✔
231

232
            auto line = deserializeBE<uint32_t>(
40,868✔
233
                m_bytecode.begin() + static_cast<std::vector<uint8_t>::difference_type>(i), m_bytecode.end());
20,434✔
234
            i += 4;
20,434✔
235

236
            block.locations.push_back(
20,434✔
237
                { .page_pointer = pp,
81,736✔
238
                  .inst_pointer = ip,
20,434✔
239
                  .filename_id = file_id,
20,434✔
240
                  .line = line });
20,434✔
241
        }
20,434✔
242

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

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

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

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

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

263
            block.pages.emplace_back().reserve(size);
3,318✔
264
            for (std::size_t j = 0; j < size; ++j)
288,594✔
265
                block.pages.back().push_back(m_bytecode[i++]);
285,276✔
266

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

271
        return block;
203✔
272
    }
203✔
273

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

278
        for (const auto location : inst_locations)
×
279
        {
280
            if (location.page_pointer == pp && !match)
×
281
                match = location;
×
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)
×
287
                match = location;
×
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))
×
291
                break;
×
292
        }
×
293

294
        return match;
×
295
    }
296

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

308
        if (segment == BytecodeSegment::All || segment == BytecodeSegment::HeadersOnly)
×
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()))
×
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())
×
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();
×
333
        const auto vals = values(syms);
×
334
        const auto files = filenames(vals);
×
335
        const auto inst_locs = instLocations(files);
×
336
        const auto code_block = code(inst_locs);
×
337

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

344
            if (showSym && sStart.has_value() && sEnd.has_value() && (sStart.value() > size || sEnd.value() > size))
×
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())
×
347
                sliceSize = sEnd.value() - sStart.value() + 1;
×
348

349
            if (showSym || segment == BytecodeSegment::HeadersOnly)
×
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)
×
353
            {
354
                if (auto start = sStart; auto end = sEnd)
×
355
                    showSym = showSym && (j >= start.value() && j <= end.value());
×
356

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

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

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

372
            bool showVal = (segment == BytecodeSegment::All || segment == BytecodeSegment::Values);
×
373
            if (showVal && sStart.has_value() && sEnd.has_value() && (sStart.value() > size || sEnd.value() > size))
×
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())
×
376
                sliceSize = sEnd.value() - sStart.value() + 1;
×
377

378
            if (showVal || segment == BytecodeSegment::HeadersOnly)
×
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)
×
382
            {
383
                if (auto start = sStart; auto end = sEnd)
×
384
                    showVal = showVal && (j >= start.value() && j <= end.value());
×
385

386
                if (showVal)
×
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
            }
×
405

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

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

417
            bool showVal = (segment == BytecodeSegment::All || segment == BytecodeSegment::InstructionLocation);
×
418
            if (showVal && sStart.has_value() && sEnd.has_value() && (sStart.value() > size || sEnd.value() > size))
×
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())
×
421
                sliceSize = sEnd.value() - sStart.value() + 1;
×
422

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

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

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

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

442
        const auto stringify_value = [](const Value& val) -> std::string {
×
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
        };
473

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

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

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

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

565
        const auto builtin_name = [](const uint16_t idx) {
×
566
            return Builtins::builtins[idx].first;
×
567
        };
568
        const auto value_str = [&stringify_value, &vals](const uint16_t idx) {
×
569
            return stringify_value(vals.values[idx]);
×
570
        };
571
        const auto symbol_name = [&syms](const uint16_t idx) {
×
572
            return syms.symbols[idx];
×
573
        };
574

575
        const auto color_print_inst = [=](const std::string& name, std::optional<Arg> arg = std::nullopt) {
×
576
            fmt::print("{}", fmt::styled(name, fmt::fg(fmt::color::gold)));
×
577
            if (arg.has_value())
×
578
            {
579
                constexpr auto sym_color = fmt::fg(fmt::color::green);
×
580
                constexpr auto const_color = fmt::fg(fmt::color::magenta);
×
581
                constexpr auto raw_color = fmt::fg(fmt::color::red);
×
582

583
                switch (auto [kind, _, idx] = arg.value(); kind)
×
584
                {
×
585
                    case ArgKind::Symbol:
586
                        fmt::print(sym_color, " {}\n", symbol_name(idx));
×
587
                        break;
×
588
                    case ArgKind::Constant:
589
                        fmt::print(const_color, " {}\n", value_str(idx));
×
590
                        break;
×
591
                    case ArgKind::Builtin:
592
                        fmt::print(" {}\n", builtin_name(idx));
×
593
                        break;
×
594
                    case ArgKind::Raw:
595
                        fmt::print(raw_color, " ({})\n", idx);
×
596
                        break;
×
597
                    case ArgKind::ConstConst:
598
                        fmt::print(" {}, {}\n", fmt::styled(value_str(arg->primary()), const_color), fmt::styled(value_str(arg->secondary()), const_color));
×
599
                        break;
×
600
                    case ArgKind::ConstSym:
601
                        fmt::print(" {}, {}\n", fmt::styled(value_str(arg->primary()), const_color), fmt::styled(symbol_name(arg->secondary()), sym_color));
×
602
                        break;
×
603
                    case ArgKind::SymConst:
604
                        fmt::print(" {}, {}\n", fmt::styled(symbol_name(arg->primary()), sym_color), fmt::styled(value_str(arg->secondary()), const_color));
×
605
                        break;
×
606
                    case ArgKind::SymSym:
607
                        fmt::print(" {}, {}\n", fmt::styled(symbol_name(arg->primary()), sym_color), fmt::styled(symbol_name(arg->secondary()), sym_color));
×
608
                        break;
×
609
                    case ArgKind::BuiltinRaw:
610
                        fmt::print(" {}, {}\n", builtin_name(arg->primary()), fmt::styled(arg->secondary(), raw_color));
×
611
                        break;
×
612
                    case ArgKind::ConstRaw:
613
                        fmt::print(" {}, {}\n", fmt::styled(value_str(arg->primary()), const_color), fmt::styled(arg->secondary(), raw_color));
×
614
                        break;
×
615
                    case ArgKind::SymRaw:
616
                        fmt::print(" {}, {}\n", fmt::styled(symbol_name(arg->primary()), sym_color), fmt::styled(arg->secondary(), raw_color));
×
617
                        break;
×
618
                    case ArgKind::RawSym:
619
                        fmt::print(" {}, {}\n", fmt::styled(arg->primary(), raw_color), fmt::styled(symbol_name(arg->secondary()), sym_color));
×
620
                        break;
×
621
                    case ArgKind::RawConst:
622
                        fmt::print(" {}, {}\n", fmt::styled(arg->primary(), raw_color), fmt::styled(value_str(arg->secondary()), const_color));
×
623
                        break;
×
624
                    case ArgKind::RawRaw:
625
                        fmt::print(" {}, {}\n", fmt::styled(arg->primary(), raw_color), fmt::styled(arg->secondary(), raw_color));
×
626
                        break;
×
627
                }
×
628
            }
×
629
            else
630
                fmt::print("\n");
×
631
        };
×
632

633
        if (segment == BytecodeSegment::All || segment == BytecodeSegment::Code || segment == BytecodeSegment::HeadersOnly)
×
634
        {
635
            uint16_t pp = 0;
×
636

637
            for (const auto& page : code_block.pages)
×
638
            {
639
                bool displayCode = true;
×
640

641
                if (auto wanted_page = cPage)
×
642
                    displayCode = pp == wanted_page.value();
×
643

644
                if (displayCode)
×
645
                    fmt::println(
×
646
                        "{} {} (length: {})",
×
647
                        fmt::styled("Code segment", fmt::fg(fmt::color::magenta)),
×
648
                        fmt::styled(pp, fmt::fg(fmt::color::magenta)),
×
649
                        page.size());
×
650

651
                if (page.empty())
×
652
                {
653
                    if (displayCode)
×
654
                        fmt::print("NOP");
×
655
                }
×
656
                else if (cPage.value_or(pp) == pp)
×
657
                {
658
                    if (segment == BytecodeSegment::HeadersOnly)
×
659
                        continue;
×
660
                    if (sStart.has_value() && sEnd.has_value() && ((sStart.value() > page.size()) || (sEnd.value() > page.size())))
×
661
                    {
662
                        fmt::print(fmt::fg(fmt::color::red), "Slice start or end can't be greater than the segment size: {}\n", page.size());
×
663
                        return;
×
664
                    }
665

666
                    std::optional<InstLoc> previous_loc = std::nullopt;
×
667

668
                    for (std::size_t j = sStart.value_or(0), end = sEnd.value_or(page.size()); j < end; j += 4)
×
669
                    {
670
                        const uint8_t inst = page[j];
×
671
                        const uint8_t padding = page[j + 1];
×
672
                        const auto arg = static_cast<uint16_t>((page[j + 2] << 8) + page[j + 3]);
×
673

674
                        auto maybe_loc = findSourceLocation(inst_locs.locations, j, pp);
×
675

676
                        // location
677
                        // we want to print it only when it changed, either the file, the line, or both
678
                        if (maybe_loc && (!previous_loc || maybe_loc != previous_loc))
×
679
                        {
680
                            if (!previous_loc || previous_loc->filename_id != maybe_loc->filename_id)
×
681
                                fmt::println("{}", files.filenames[maybe_loc->filename_id]);
×
682
                            fmt::print("{:>4}", maybe_loc->line + 1);
×
683
                            previous_loc = maybe_loc;
×
684
                        }
×
685
                        else
686
                            fmt::print("    ");
×
687
                        // instruction number
688
                        fmt::print(fmt::fg(fmt::color::cyan), "{:>4}", j / 4);
×
689
                        // padding inst arg arg
690
                        fmt::print(" {:02x} {:02x} {:02x} {:02x} ", inst, padding, page[j + 2], page[j + 3]);
×
691

692
                        if (const auto idx = static_cast<std::size_t>(inst); idx < InstructionNames.size())
×
693
                        {
694
                            const auto inst_name = InstructionNames[idx];
×
695
                            if (const auto iinst = static_cast<Instruction>(inst); arg_kinds.contains(iinst))
×
696
                                color_print_inst(inst_name, Arg { arg_kinds.at(iinst), padding, arg });
×
697
                            else
698
                                color_print_inst(inst_name);
×
699
                        }
×
700
                        else
701
                            fmt::println("Unknown instruction");
×
702
                    }
×
703
                }
×
704
                if (displayCode && segment != BytecodeSegment::HeadersOnly)
×
705
                    fmt::print("\n");
×
706

707
                ++pp;
×
708
            }
×
709
        }
×
710
    }
×
711

712
    uint16_t BytecodeReader::readNumber(std::size_t& i) const
68,547✔
713
    {
68,547✔
714
        const auto x = static_cast<uint16_t>(m_bytecode[i] << 8);
68,547✔
715
        const uint16_t y = m_bytecode[++i];
68,547✔
716
        return x + y;
137,094✔
717
    }
68,547✔
718
}
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