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

ArkScript-lang / Ark / 14421790982

12 Apr 2025 05:22PM UTC coverage: 80.451% (-0.02%) from 80.472%
14421790982

push

github

SuperFola
fix(ci): include the proxified version of picosha2

6169 of 7668 relevant lines covered (80.45%)

78476.93 hits per line

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

42.91
/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)
254✔
18
    {
254✔
19
        m_bytecode = bytecode;
254✔
20
    }
254✔
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
669✔
41
    {
669✔
42
        return m_bytecode.size() >= bytecode::Magic.size() &&
1,337✔
43
            m_bytecode[0] == bytecode::Magic[0] &&
668✔
44
            m_bytecode[1] == bytecode::Magic[1] &&
498✔
45
            m_bytecode[2] == bytecode::Magic[2] &&
996✔
46
            m_bytecode[3] == bytecode::Magic[3];
498✔
47
    }
48

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

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

84
        std::vector<unsigned char> sha(picosha2::k_digest_size);
83✔
85
        for (std::size_t i = 0; i < picosha2::k_digest_size; ++i)
2,739✔
86
            sha[i] = m_bytecode[bytecode::HeaderSize + i];
2,656✔
87
        return sha;
83✔
88
    }
166✔
89

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

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

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

104
        for (uint16_t j = 0; j < size; ++j)
2,493✔
105
        {
106
            std::string content;
2,410✔
107
            while (m_bytecode[i] != 0)
24,469✔
108
                content.push_back(static_cast<char>(m_bytecode[i++]));
22,059✔
109
            i++;
2,410✔
110

111
            block.symbols.push_back(content);
2,410✔
112
        }
2,410✔
113

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

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

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

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

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

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

148
                const ieee754::DecomposedDouble d { exp, mant };
520✔
149
                double val = ieee754::deserialize(d);
520✔
150
                block.values.emplace_back(val);
520✔
151
            }
520✔
152
            else if (type == STRING_TYPE)
2,391✔
153
            {
154
                std::string val;
1,174✔
155
                while (m_bytecode[i] != 0)
21,394✔
156
                    val.push_back(static_cast<char>(m_bytecode[i++]));
20,220✔
157
                block.values.emplace_back(val);
1,174✔
158
            }
1,174✔
159
            else if (type == FUNC_TYPE)
1,217✔
160
            {
161
                const uint16_t addr = readNumber(i);
1,217✔
162
                i++;
1,217✔
163
                block.values.emplace_back(addr);
1,217✔
164
            }
1,217✔
165
            else
166
                throw std::runtime_error(fmt::format("Unknown value type: {:x}", type));
×
167
            i++;
2,911✔
168
        }
2,911✔
169

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

174
    Code BytecodeReader::code(const Values& values) const
83✔
175
    {
83✔
176
        if (!checkMagic())
83✔
177
            return {};
×
178

179
        std::size_t i = values.end;
83✔
180

181
        Code block;
83✔
182
        block.start = i;
83✔
183

184
        while (m_bytecode[i] == CODE_SEGMENT_START)
1,300✔
185
        {
186
            i++;
1,300✔
187
            const std::size_t size = readNumber(i) * 4;
1,300✔
188
            i++;
1,300✔
189

190
            block.pages.emplace_back().reserve(size);
1,300✔
191
            for (std::size_t j = 0; j < size; ++j)
150,832✔
192
                block.pages.back().push_back(m_bytecode[i++]);
149,532✔
193

194
            if (i == m_bytecode.size())
1,300✔
195
                break;
83✔
196
        }
1,300✔
197

198
        return block;
83✔
199
    }
83✔
200

201
    void BytecodeReader::display(const BytecodeSegment segment,
×
202
                                 const std::optional<uint16_t> sStart,
203
                                 const std::optional<uint16_t> sEnd,
204
                                 const std::optional<uint16_t> cPage) const
205
    {
×
206
        if (!checkMagic())
×
207
        {
208
            fmt::println("Invalid format");
×
209
            return;
×
210
        }
211

212
        auto [major, minor, patch] = version();
×
213
        fmt::println("Version:   {}.{}.{}", major, minor, patch);
×
214
        fmt::println("Timestamp: {}", timestamp());
×
215
        fmt::print("SHA256:    ");
×
216
        for (const auto sha = sha256(); unsigned char h : sha)
×
217
            fmt::print("{:02x}", h);
×
218
        fmt::print("\n\n");
×
219

220
        // reading the different tables, one after another
221

222
        if ((sStart.has_value() && !sEnd.has_value()) || (!sStart.has_value() && sEnd.has_value()))
×
223
        {
224
            fmt::print(fmt::fg(fmt::color::red), "Both start and end parameter need to be provided together\n");
×
225
            return;
×
226
        }
227
        if (sStart.has_value() && sEnd.has_value() && sStart.value() >= sEnd.value())
×
228
        {
229
            fmt::print(fmt::fg(fmt::color::red), "Invalid slice start and end arguments\n");
×
230
            return;
×
231
        }
232

233
        const auto syms = symbols();
×
234
        const auto vals = values(syms);
×
235
        const auto code_block = code(vals);
×
236

237
        // symbols table
238
        {
239
            std::size_t size = syms.symbols.size();
×
240
            std::size_t sliceSize = size;
×
241
            bool showSym = (segment == BytecodeSegment::All || segment == BytecodeSegment::Symbols);
×
242

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

248
            if (showSym || segment == BytecodeSegment::HeadersOnly)
×
249
                fmt::println("{} (length: {})", fmt::styled("Symbols table", fmt::fg(fmt::color::cyan)), sliceSize);
×
250

251
            for (std::size_t j = 0; j < size; ++j)
×
252
            {
253
                if (auto start = sStart; auto end = sEnd)
×
254
                    showSym = showSym && (j >= start.value() && j <= end.value());
×
255

256
                if (showSym)
×
257
                    fmt::println("{}) {}", j, syms.symbols[j]);
×
258
            }
×
259

260
            if (showSym)
×
261
                fmt::print("\n");
×
262
            if (segment == BytecodeSegment::Symbols)
×
263
                return;
×
264
        }
×
265

266
        // values table
267
        {
268
            std::size_t size = vals.values.size();
×
269
            std::size_t sliceSize = size;
×
270

271
            bool showVal = (segment == BytecodeSegment::All || segment == BytecodeSegment::Values);
×
272
            if (showVal && sStart.has_value() && sEnd.has_value() && (sStart.value() > size || sEnd.value() > size))
×
273
                fmt::print(fmt::fg(fmt::color::red), "Slice start or end can't be greater than the segment size: {}\n", size);
×
274
            else if (showVal && sStart.has_value() && sEnd.has_value())
×
275
                sliceSize = sEnd.value() - sStart.value() + 1;
×
276

277
            if (showVal || segment == BytecodeSegment::HeadersOnly)
×
278
                fmt::println("{} (length: {})", fmt::styled("Constants table", fmt::fg(fmt::color::cyan)), sliceSize);
×
279

280
            for (std::size_t j = 0; j < size; ++j)
×
281
            {
282
                if (auto start = sStart; auto end = sEnd)
×
283
                    showVal = showVal && (j >= start.value() && j <= end.value());
×
284

285
                if (showVal)
×
286
                {
287
                    switch (const auto val = vals.values[j]; val.valueType())
×
288
                    {
×
289
                        case ValueType::Number:
290
                            fmt::println("{}) (Number) {}", j, val.number());
×
291
                            break;
×
292
                        case ValueType::String:
293
                            fmt::println("{}) (String) {}", j, val.string());
×
294
                            break;
×
295
                        case ValueType::PageAddr:
296
                            fmt::println("{}) (PageAddr) {}", j, val.pageAddr());
×
297
                            break;
×
298
                        default:
299
                            fmt::print(fmt::fg(fmt::color::red), "Value type not handled: {}\n", types_to_str[static_cast<std::size_t>(val.valueType())]);
×
300
                            break;
×
301
                    }
×
302
                }
×
303
            }
×
304

305
            if (showVal)
×
306
                fmt::print("\n");
×
307
            if (segment == BytecodeSegment::Values)
×
308
                return;
×
309
        }
×
310

311
        const auto stringify_value = [](const Value& val) -> std::string {
×
312
            switch (val.valueType())
×
313
            {
×
314
                case ValueType::Number:
315
                    return fmt::format("{} (Number)", val.number());
×
316
                case ValueType::String:
317
                    return fmt::format("{} (String)", val.string());
×
318
                case ValueType::PageAddr:
319
                    return fmt::format("{} (PageAddr)", val.pageAddr());
×
320
                default:
321
                    return "";
×
322
            }
323
        };
×
324

325
        enum class ArgKind
326
        {
327
            Symbol,
328
            Value,
329
            Builtin,
330
            Raw
331
        };
332

333
        struct Arg
334
        {
335
            ArgKind kind;
336
            uint16_t arg;
337
        };
338

339
        const std::unordered_map<Instruction, ArgKind> arg_kinds = {
×
340
            { LOAD_SYMBOL, ArgKind::Symbol },
341
            { LOAD_SYMBOL_BY_INDEX, ArgKind::Raw },
342
            { LOAD_CONST, ArgKind::Value },
343
            { POP_JUMP_IF_TRUE, ArgKind::Raw },
344
            { STORE, ArgKind::Symbol },
345
            { SET_VAL, ArgKind::Symbol },
346
            { POP_JUMP_IF_FALSE, ArgKind::Raw },
347
            { JUMP, ArgKind::Raw },
348
            { CALL, ArgKind::Raw },
349
            { CALL_BUILTIN, ArgKind::Raw },
350
            { CAPTURE, ArgKind::Symbol },
351
            { BUILTIN, ArgKind::Builtin },
352
            { DEL, ArgKind::Symbol },
353
            { MAKE_CLOSURE, ArgKind::Value },
354
            { GET_FIELD, ArgKind::Symbol },
355
            { PLUGIN, ArgKind::Value },
356
            { LIST, ArgKind::Raw },
357
            { APPEND, ArgKind::Raw },
358
            { CONCAT, ArgKind::Raw },
359
            { APPEND_IN_PLACE, ArgKind::Raw },
360
            { CONCAT_IN_PLACE, ArgKind::Raw }
361
        };
362

363
        const auto color_print_inst = [&syms, &vals, &stringify_value](const std::string& name, std::optional<Arg> arg = std::nullopt) {
×
364
            fmt::print("{}", fmt::styled(name, fmt::fg(fmt::color::gold)));
×
365
            if (arg.has_value())
×
366
            {
367
                switch (auto [kind, idx] = arg.value(); kind)
×
368
                {
×
369
                    case ArgKind::Symbol:
370
                        fmt::print(fmt::fg(fmt::color::green), " {}\n", syms.symbols[idx]);
×
371
                        break;
×
372
                    case ArgKind::Value:
373
                        fmt::print(fmt::fg(fmt::color::magenta), " {}\n", stringify_value(vals.values[idx]));
×
374
                        break;
×
375
                    case ArgKind::Builtin:
376
                        fmt::print(" {}\n", Builtins::builtins[idx].first);
×
377
                        break;
×
378
                    case ArgKind::Raw:
379
                        fmt::print(fmt::fg(fmt::color::red), " ({})\n", idx);
×
380
                        break;
×
381
                }
×
382
            }
×
383
            else
384
                fmt::print("\n");
×
385
        };
×
386

387
        if (segment == BytecodeSegment::All || segment == BytecodeSegment::Code || segment == BytecodeSegment::HeadersOnly)
×
388
        {
389
            uint16_t pp = 0;
×
390

391
            for (const auto& page : code_block.pages)
×
392
            {
393
                bool displayCode = true;
×
394

395
                if (auto wanted_page = cPage)
×
396
                    displayCode = pp == wanted_page.value();
×
397

398
                if (displayCode)
×
399
                    fmt::println(
×
400
                        "{} {} (length: {})",
×
401
                        fmt::styled("Code segment", fmt::fg(fmt::color::magenta)),
×
402
                        fmt::styled(pp, fmt::fg(fmt::color::magenta)),
×
403
                        page.size());
×
404

405
                if (page.empty())
×
406
                {
407
                    if (displayCode)
×
408
                        fmt::print("NOP");
×
409
                }
×
410
                else
411
                {
412
                    if (cPage.value_or(pp) != pp)
×
413
                        continue;
×
414
                    if (segment == BytecodeSegment::HeadersOnly)
×
415
                        continue;
×
416
                    if (sStart.has_value() && sEnd.has_value() && ((sStart.value() > page.size()) || (sEnd.value() > page.size())))
×
417
                    {
418
                        fmt::print(fmt::fg(fmt::color::red), "Slice start or end can't be greater than the segment size: {}\n", page.size());
×
419
                        return;
×
420
                    }
421

422
                    for (std::size_t j = sStart.value_or(0), end = sEnd.value_or(page.size()); j < end; j += 4)
×
423
                    {
424
                        const uint8_t inst = page[j];
×
425
                        // TEMP
426
                        const uint8_t padding = page[j + 1];
×
427
                        const auto arg = static_cast<uint16_t>((page[j + 2] << 8) + page[j + 3]);
×
428

429
                        // instruction number
430
                        fmt::print(fmt::fg(fmt::color::cyan), "{:>4}", j / 4);
×
431
                        // padding inst arg arg
432
                        fmt::print(" {:02x} {:02x} {:02x} {:02x} ", inst, padding, page[j + 2], page[j + 3]);
×
433

434
                        if (const auto idx = static_cast<std::size_t>(inst); idx < InstructionNames.size())
×
435
                        {
436
                            const auto inst_name = InstructionNames[idx];
×
437
                            if (const auto iinst = static_cast<Instruction>(inst); arg_kinds.contains(iinst))
×
438
                                color_print_inst(inst_name, Arg { arg_kinds.at(iinst), arg });
×
439
                            else
440
                                color_print_inst(inst_name);
×
441
                        }
×
442
                        else
443
                            fmt::println("Unknown instruction");
×
444
                    }
×
445
                }
446
                if (displayCode && segment != BytecodeSegment::HeadersOnly)
×
447
                    fmt::print("\n");
×
448

449
                ++pp;
×
450
            }
×
451
        }
×
452
    }
×
453

454
    uint16_t BytecodeReader::readNumber(std::size_t& i) const
2,683✔
455
    {
2,683✔
456
        const auto x = static_cast<uint16_t>(m_bytecode[i] << 8);
2,683✔
457
        const uint16_t y = m_bytecode[++i];
2,683✔
458
        return x + y;
5,366✔
459
    }
2,683✔
460
}
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