Repository navigation
Expand file tree
/
Copy pathCifaBytecode.h
More file actions
93 lines (79 loc) · 3 KB
/
Copy pathCifaBytecode.h
File metadata and controls
93 lines (79 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#pragma once
#include "Cifa.h"
#include <cstdint>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
namespace cifa {
struct Proto;
struct Program;
// Compiles Cifa's checked AST directly to a Lua 5.4 binary chunk and executes
// it using Lua's register VM.
class CifaBytecode : public Cifa {
public:
CifaBytecode() = default;
~CifaBytecode() = default;
CifaBytecode(const CifaBytecode&) = delete;
CifaBytecode& operator=(const CifaBytecode&) = delete;
bool compile_script(std::string script);
bool compile_file(const std::string& filename);
Object run(const std::string& entry_label = {});
Object run_script(std::string script);
Object run_file(const std::string& filename);
bool valid() const { return program_ != nullptr && translation_error_.empty(); }
const std::string& get_translation_error() const { return translation_error_; }
std::string get_runtime_error() const { return runtime_error_; }
std::string get_cfg_json() const;
std::string get_profile_json() const;
void set_profiling_enabled(bool enabled);
bool is_profiling_enabled() const { return profile_enabled_; }
void reset_profile();
bool has_runtime_error() const { return !runtime_error_.empty(); }
bool is_exit_requested() const { return Cifa::is_exit_requested(); }
const std::vector<std::uint8_t>& emitted_chunk() const { return chunk_; }
private:
friend class LuaVm;
class FunctionCompiler;
struct ProfileMetric
{
std::size_t count = 0;
std::uint64_t time_ns = 0;
};
struct ProfileFunctionMetric
{
std::size_t calls = 0;
std::size_t instructions = 0;
std::uint64_t self_ns = 0;
std::uint64_t total_ns = 0;
};
struct ProfileFlameMetric
{
std::uint64_t self_ns = 0;
std::uint64_t total_ns = 0;
};
void profile_lua_tick(const std::string& function_id, std::size_t pc,
std::vector<std::string> stack, std::uint64_t now_ns);
void profile_lua_finish(std::uint64_t now_ns);
bool emit_chunk();
Object execute_chunk();
std::shared_ptr<Proto> compile_lua_program(std::vector<std::uint8_t>& chunk, std::string& error) const;
std::string translation_error_;
std::string runtime_error_;
std::vector<std::uint8_t> chunk_;
std::shared_ptr<Program> program_;
bool profile_enabled_ = false;
bool profile_pending_ = false;
bool profile_truncated_ = false;
std::size_t profile_instruction_count_ = 0;
std::uint64_t profile_total_ns_ = 0;
std::uint64_t profile_last_ns_ = 0;
std::string profile_last_function_;
std::size_t profile_last_pc_ = 0;
std::vector<std::string> profile_last_stack_;
std::unordered_map<std::string, ProfileMetric> profile_instructions_;
std::unordered_map<std::string, ProfileMetric> profile_edges_;
std::unordered_map<std::string, ProfileFunctionMetric> profile_functions_;
std::unordered_map<std::string, ProfileFlameMetric> profile_flames_;
};
} // namespace cifa