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

ArkScript-lang / Ark / 13762570662

10 Mar 2025 10:35AM UTC coverage: 78.931% (-0.1%) from 79.05%
13762570662

push

github

SuperFola
refactor(builtins): use std::numbers instead of <cmath> for M_PI and HUGE_VAL (float inf)

5848 of 7409 relevant lines covered (78.93%)

79635.36 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 <iomanip>
7
#include <unordered_map>
8
#include <picosha2.h>
9
#include <Ark/Compiler/Serialization/IEEE754Serializer.hpp>
10
#include <Ark/Compiler/Serialization/IntegerSerializer.hpp>
11
#include <fmt/core.h>
12
#include <fmt/color.h>
13

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

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

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

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

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

41
    bool BytecodeReader::checkMagic() const
669✔
42
    {
669✔
43
        return m_bytecode.size() >= bytecode::Magic.size() &&
1,337✔
44
            m_bytecode[0] == bytecode::Magic[0] &&
668✔
45
            m_bytecode[1] == bytecode::Magic[1] &&
498✔
46
            m_bytecode[2] == bytecode::Magic[2] &&
996✔
47
            m_bytecode[3] == bytecode::Magic[3];
498✔
48
    }
49

50
    const bytecode_t& BytecodeReader::bytecode() noexcept
×
51
    {
×
52
        return m_bytecode;
×
53
    }
×
54

55
    Version BytecodeReader::version() const
83✔
56
    {
83✔
57
        if (!checkMagic() || m_bytecode.size() < bytecode::Magic.size() + bytecode::Version.size())
83✔
58
            return Version { 0, 0, 0 };
×
59

60
        return Version {
332✔
61
            .major = static_cast<uint16_t>((m_bytecode[4] << 8) + m_bytecode[5]),
83✔
62
            .minor = static_cast<uint16_t>((m_bytecode[6] << 8) + m_bytecode[7]),
83✔
63
            .patch = static_cast<uint16_t>((m_bytecode[8] << 8) + m_bytecode[9])
83✔
64
        };
65
    }
83✔
66

67
    unsigned long long BytecodeReader::timestamp() const
1✔
68
    {
1✔
69
        // 4 (ark\0) + version (2 bytes / number) + timestamp = 18 bytes
70
        if (!checkMagic() || m_bytecode.size() < bytecode::HeaderSize)
1✔
71
            return 0;
×
72

73
        // reading the timestamp in big endian
74
        using timestamp_t = unsigned long long;
75
        return (static_cast<timestamp_t>(m_bytecode[10]) << 56) +
3✔
76
            (static_cast<timestamp_t>(m_bytecode[11]) << 48) +
2✔
77
            (static_cast<timestamp_t>(m_bytecode[12]) << 40) +
2✔
78
            (static_cast<timestamp_t>(m_bytecode[13]) << 32) +
2✔
79
            (static_cast<timestamp_t>(m_bytecode[14]) << 24) +
2✔
80
            (static_cast<timestamp_t>(m_bytecode[15]) << 16) +
2✔
81
            (static_cast<timestamp_t>(m_bytecode[16]) << 8) +
2✔
82
            static_cast<timestamp_t>(m_bytecode[17]);
1✔
83
    }
1✔
84

85
    std::vector<unsigned char> BytecodeReader::sha256() const
83✔
86
    {
83✔
87
        if (!checkMagic() || m_bytecode.size() < bytecode::HeaderSize + picosha2::k_digest_size)
83✔
88
            return {};
×
89

90
        std::vector<unsigned char> sha(picosha2::k_digest_size);
83✔
91
        for (std::size_t i = 0; i < picosha2::k_digest_size; ++i)
2,739✔
92
            sha[i] = m_bytecode[bytecode::HeaderSize + i];
2,656✔
93
        return sha;
83✔
94
    }
166✔
95

96
    Symbols BytecodeReader::symbols() const
83✔
97
    {
83✔
98
        if (!checkMagic() || m_bytecode.size() < bytecode::HeaderSize + picosha2::k_digest_size ||
166✔
99
            m_bytecode[bytecode::HeaderSize + picosha2::k_digest_size] != SYM_TABLE_START)
83✔
100
            return {};
×
101

102
        std::size_t i = bytecode::HeaderSize + picosha2::k_digest_size + 1;
83✔
103
        const uint16_t size = readNumber(i);
83✔
104
        i++;
83✔
105

106
        Symbols block;
83✔
107
        block.start = bytecode::HeaderSize + picosha2::k_digest_size;
83✔
108
        block.symbols.reserve(size);
83✔
109

110
        for (uint16_t j = 0; j < size; ++j)
2,463✔
111
        {
112
            std::string content;
2,380✔
113
            while (m_bytecode[i] != 0)
24,125✔
114
                content.push_back(static_cast<char>(m_bytecode[i++]));
21,745✔
115
            i++;
2,380✔
116

117
            block.symbols.push_back(content);
2,380✔
118
        }
2,380✔
119

120
        block.end = i;
83✔
121
        return block;
83✔
122
    }
83✔
123

124
    Values BytecodeReader::values(const Symbols& symbols) const
83✔
125
    {
83✔
126
        if (!checkMagic())
83✔
127
            return {};
×
128

129
        std::size_t i = symbols.end;
83✔
130
        if (m_bytecode[i] != VAL_TABLE_START)
83✔
131
            return {};
×
132
        i++;
83✔
133

134
        const uint16_t size = readNumber(i);
83✔
135
        i++;
83✔
136
        Values block;
83✔
137
        block.start = symbols.end;
83✔
138
        block.values.reserve(size);
83✔
139

140
        for (uint16_t j = 0; j < size; ++j)
2,964✔
141
        {
142
            const uint8_t type = m_bytecode[i];
2,881✔
143
            i++;
2,881✔
144

145
            if (type == NUMBER_TYPE)
2,881✔
146
            {
147
                auto exp = deserializeLE<decltype(ieee754::DecomposedDouble::exponent)>(
1,040✔
148
                    m_bytecode.begin() + static_cast<std::vector<uint8_t>::difference_type>(i), m_bytecode.end());
520✔
149
                i += sizeof(decltype(exp));
520✔
150
                auto mant = deserializeLE<decltype(ieee754::DecomposedDouble::mantissa)>(
1,040✔
151
                    m_bytecode.begin() + static_cast<std::vector<uint8_t>::difference_type>(i), m_bytecode.end());
520✔
152
                i += sizeof(decltype(mant));
520✔
153

154
                const ieee754::DecomposedDouble d { exp, mant };
520✔
155
                double val = ieee754::deserialize(d);
520✔
156
                block.values.emplace_back(val);
520✔
157
            }
520✔
158
            else if (type == STRING_TYPE)
2,361✔
159
            {
160
                std::string val;
1,150✔
161
                while (m_bytecode[i] != 0)
20,939✔
162
                    val.push_back(static_cast<char>(m_bytecode[i++]));
19,789✔
163
                block.values.emplace_back(val);
1,150✔
164
            }
1,150✔
165
            else if (type == FUNC_TYPE)
1,211✔
166
            {
167
                const uint16_t addr = readNumber(i);
1,211✔
168
                i++;
1,211✔
169
                block.values.emplace_back(addr);
1,211✔
170
            }
1,211✔
171
            else
172
                throw std::runtime_error(fmt::format("Unknown value type: {:x}", type));
×
173
            i++;
2,881✔
174
        }
2,881✔
175

176
        block.end = i;
83✔
177
        return block;
83✔
178
    }
83✔
179

180
    Code BytecodeReader::code(const Values& values) const
83✔
181
    {
83✔
182
        if (!checkMagic())
83✔
183
            return {};
×
184

185
        std::size_t i = values.end;
83✔
186

187
        Code block;
83✔
188
        block.start = i;
83✔
189

190
        while (m_bytecode[i] == CODE_SEGMENT_START)
1,294✔
191
        {
192
            i++;
1,294✔
193
            const std::size_t size = readNumber(i) * 4;
1,294✔
194
            i++;
1,294✔
195

196
            block.pages.emplace_back().reserve(size);
1,294✔
197
            for (std::size_t j = 0; j < size; ++j)
147,194✔
198
                block.pages.back().push_back(m_bytecode[i++]);
145,900✔
199

200
            if (i == m_bytecode.size())
1,294✔
201
                break;
83✔
202
        }
1,294✔
203

204
        return block;
83✔
205
    }
83✔
206

207
    void BytecodeReader::display(const BytecodeSegment segment,
×
208
                                 const std::optional<uint16_t> sStart,
209
                                 const std::optional<uint16_t> sEnd,
210
                                 const std::optional<uint16_t> cPage) const
211
    {
×
212
        if (!checkMagic())
×
213
        {
214
            fmt::println("Invalid format");
×
215
            return;
×
216
        }
217

218
        auto [major, minor, patch] = version();
×
219
        fmt::println("Version:   {}.{}.{}", major, minor, patch);
×
220
        fmt::println("Timestamp: {}", timestamp());
×
221
        fmt::print("SHA256:    ");
×
222
        for (const auto sha = sha256(); unsigned char h : sha)
×
223
            fmt::print("{:02x}", h);
×
224
        fmt::print("\n\n");
×
225

226
        // reading the different tables, one after another
227

228
        if ((sStart.has_value() && !sEnd.has_value()) || (!sStart.has_value() && sEnd.has_value()))
×
229
        {
230
            fmt::print(fmt::fg(fmt::color::red), "Both start and end parameter need to be provided together\n");
×
231
            return;
232
        }
×
233
        if (sStart.has_value() && sEnd.has_value() && sStart.value() >= sEnd.value())
×
234
        {
235
            fmt::print(fmt::fg(fmt::color::red), "Invalid slice start and end arguments\n");
×
236
            return;
237
        }
×
238

239
        const auto syms = symbols();
×
240
        const auto vals = values(syms);
×
241
        const auto code_block = code(vals);
×
242

243
        // symbols table
244
        {
245
            std::size_t size = syms.symbols.size();
×
246
            std::size_t sliceSize = size;
×
247
            bool showSym = (segment == BytecodeSegment::All || segment == BytecodeSegment::Symbols);
×
248

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

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

257
            for (std::size_t j = 0; j < size; ++j)
×
258
            {
259
                if (auto start = sStart; auto end = sEnd)
×
260
                    showSym = showSym && (j >= start.value() && j <= end.value());
×
261

262
                if (showSym)
×
263
                    fmt::println("{}) {}", j, syms.symbols[j]);
×
264
            }
×
265

266
            if (showSym)
×
267
                fmt::print("\n");
×
268
            if (segment == BytecodeSegment::Symbols)
×
269
                return;
×
270
        }
×
271

272
        // values table
273
        {
274
            std::size_t size = vals.values.size();
×
275
            std::size_t sliceSize = size;
×
276

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

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

286
            for (std::size_t j = 0; j < size; ++j)
×
287
            {
288
                if (auto start = sStart; auto end = sEnd)
×
289
                    showVal = showVal && (j >= start.value() && j <= end.value());
×
290

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

311
            if (showVal)
×
312
                fmt::print("\n");
×
313
            if (segment == BytecodeSegment::Values)
×
314
                return;
×
315
        }
×
316

317
        const auto stringify_value = [](const Value& val) -> std::string {
×
318
            switch (val.valueType())
×
319
            {
×
320
                case ValueType::Number:
321
                    return fmt::format("{} (Number)", val.number());
×
322
                case ValueType::String:
323
                    return fmt::format("{} (String)", val.string());
×
324
                case ValueType::PageAddr:
325
                    return fmt::format("{} (PageAddr)", val.pageAddr());
×
326
                default:
327
                    return "";
×
328
            }
329
        };
×
330

331
        enum class ArgKind
332
        {
333
            Symbol,
334
            Value,
335
            Builtin,
336
            Raw
337
        };
338

339
        struct Arg
340
        {
341
            ArgKind kind;
342
            uint16_t arg;
343
        };
344

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

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

392
        if (segment == BytecodeSegment::All || segment == BytecodeSegment::Code || segment == BytecodeSegment::HeadersOnly)
×
393
        {
394
            uint16_t pp = 0;
×
395

396
            for (const auto& page : code_block.pages)
×
397
            {
398
                bool displayCode = true;
×
399

400
                if (auto wanted_page = cPage)
×
401
                    displayCode = pp == wanted_page.value();
×
402

403
                if (displayCode)
×
404
                    fmt::println(
×
405
                        "{} {} (length: {})",
×
406
                        fmt::styled("Code segment", fmt::fg(fmt::color::magenta)),
×
407
                        fmt::styled(pp, fmt::fg(fmt::color::magenta)),
×
408
                        page.size());
×
409

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

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

434
                        // instruction number
435
                        fmt::print(fmt::fg(fmt::color::cyan), "{:>4}", j / 4);
×
436
                        // padding inst arg arg
437
                        fmt::print(" {:02x} {:02x} {:02x} {:02x} ", inst, padding, page[j + 2], page[j + 3]);
×
438

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

454
                ++pp;
×
455
            }
×
456
        }
×
457
    }
×
458

459
    uint16_t BytecodeReader::readNumber(std::size_t& i) const
2,671✔
460
    {
2,671✔
461
        const auto x = static_cast<uint16_t>(m_bytecode[i] << 8);
2,671✔
462
        const uint16_t y = m_bytecode[++i];
2,671✔
463
        return x + y;
5,342✔
464
    }
2,671✔
465
}
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