Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/CodeGen/src/BytecodeSummary.cpp
2725 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
#include "Luau/BytecodeSummary.h"
3
4
#include "Luau/IrUtils.h"
5
6
#include "CodeGenLower.h"
7
8
#include "lua.h"
9
#include "lapi.h"
10
#include "lobject.h"
11
#include "lstate.h"
12
13
namespace Luau
14
{
15
namespace CodeGen
16
{
17
18
FunctionBytecodeSummary::FunctionBytecodeSummary(std::string source, std::string name, const int line, unsigned nestingLimit)
19
: source(std::move(source))
20
, name(std::move(name))
21
, line(line)
22
, nestingLimit(nestingLimit)
23
{
24
counts.reserve(nestingLimit);
25
for (unsigned i = 0; i < 1 + nestingLimit; ++i)
26
{
27
counts.push_back(std::vector<unsigned>(getOpLimit(), 0));
28
}
29
}
30
31
FunctionBytecodeSummary FunctionBytecodeSummary::fromProto(Proto* proto, unsigned nestingLimit)
32
{
33
const char* source = getstr(proto->source);
34
source = (source[0] == '=' || source[0] == '@') ? source + 1 : "[string]";
35
36
const char* name = proto->debugname ? getstr(proto->debugname) : "";
37
38
int line = proto->linedefined;
39
40
FunctionBytecodeSummary summary(source, name, line, nestingLimit);
41
42
for (int i = 0; i < proto->sizecode;)
43
{
44
Instruction insn = proto->code[i];
45
uint8_t op = LUAU_INSN_OP(insn);
46
summary.incCount(0, op);
47
i += getOpLength(LuauOpcode(op));
48
}
49
50
return summary;
51
}
52
53
std::vector<FunctionBytecodeSummary> summarizeBytecode(lua_State* L, int idx, unsigned nestingLimit)
54
{
55
CODEGEN_ASSERT(lua_isLfunction(L, idx));
56
const TValue* func = luaA_toobject(L, idx);
57
58
Proto* root = clvalue(func)->l.p;
59
60
std::vector<Proto*> protos;
61
gatherFunctions(protos, root, CodeGen_ColdFunctions, root->flags & LPF_NATIVE_FUNCTION);
62
63
std::vector<FunctionBytecodeSummary> summaries;
64
summaries.reserve(protos.size());
65
66
for (Proto* proto : protos)
67
{
68
if (proto)
69
summaries.push_back(FunctionBytecodeSummary::fromProto(proto, nestingLimit));
70
}
71
72
return summaries;
73
}
74
75
} // namespace CodeGen
76
} // namespace Luau
77
78