2023-06-10 01:08:00 +08:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#include "lua.h"
|
|
|
|
#include "lualib.h"
|
|
|
|
|
|
|
|
#include "Luau/CodeGen.h"
|
|
|
|
#include "Luau/Compiler.h"
|
|
|
|
#include "Luau/BytecodeBuilder.h"
|
|
|
|
#include "Luau/Parser.h"
|
|
|
|
#include "Luau/TimeTrace.h"
|
|
|
|
|
|
|
|
#include "FileUtils.h"
|
|
|
|
#include "Flags.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
LUAU_FASTFLAG(DebugLuauTimeTracing)
|
|
|
|
|
|
|
|
enum class CompileFormat
|
|
|
|
{
|
|
|
|
Text,
|
|
|
|
Binary,
|
|
|
|
Remarks,
|
|
|
|
Codegen, // Prints annotated native code including IR and assembly
|
|
|
|
CodegenAsm, // Prints annotated native code assembly
|
|
|
|
CodegenIr, // Prints annotated native code IR
|
|
|
|
CodegenVerbose, // Prints annotated native code including IR, assembly and outlined code
|
|
|
|
CodegenNull,
|
|
|
|
Null
|
|
|
|
};
|
|
|
|
|
2023-10-21 09:10:30 +08:00
|
|
|
enum class RecordStats
|
|
|
|
{
|
|
|
|
None,
|
|
|
|
Total,
|
2023-12-02 15:46:57 +08:00
|
|
|
File,
|
|
|
|
Function
|
2023-10-21 09:10:30 +08:00
|
|
|
};
|
|
|
|
|
2023-06-10 01:08:00 +08:00
|
|
|
struct GlobalOptions
|
|
|
|
{
|
|
|
|
int optimizationLevel = 1;
|
|
|
|
int debugLevel = 1;
|
2024-04-26 06:26:09 +08:00
|
|
|
int typeInfoLevel = 0;
|
2023-12-02 15:46:57 +08:00
|
|
|
|
2024-02-27 01:15:13 +08:00
|
|
|
const char* vectorLib = nullptr;
|
|
|
|
const char* vectorCtor = nullptr;
|
|
|
|
const char* vectorType = nullptr;
|
2023-06-10 01:08:00 +08:00
|
|
|
} globalOptions;
|
|
|
|
|
|
|
|
static Luau::CompileOptions copts()
|
|
|
|
{
|
|
|
|
Luau::CompileOptions result = {};
|
|
|
|
result.optimizationLevel = globalOptions.optimizationLevel;
|
|
|
|
result.debugLevel = globalOptions.debugLevel;
|
2024-04-26 06:26:09 +08:00
|
|
|
result.typeInfoLevel = globalOptions.typeInfoLevel;
|
2023-06-10 01:08:00 +08:00
|
|
|
|
2024-02-27 01:15:13 +08:00
|
|
|
result.vectorLib = globalOptions.vectorLib;
|
|
|
|
result.vectorCtor = globalOptions.vectorCtor;
|
|
|
|
result.vectorType = globalOptions.vectorType;
|
2023-12-02 15:46:57 +08:00
|
|
|
|
2023-06-10 01:08:00 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::optional<CompileFormat> getCompileFormat(const char* name)
|
|
|
|
{
|
|
|
|
if (strcmp(name, "text") == 0)
|
|
|
|
return CompileFormat::Text;
|
|
|
|
else if (strcmp(name, "binary") == 0)
|
|
|
|
return CompileFormat::Binary;
|
|
|
|
else if (strcmp(name, "text") == 0)
|
|
|
|
return CompileFormat::Text;
|
|
|
|
else if (strcmp(name, "remarks") == 0)
|
|
|
|
return CompileFormat::Remarks;
|
|
|
|
else if (strcmp(name, "codegen") == 0)
|
|
|
|
return CompileFormat::Codegen;
|
|
|
|
else if (strcmp(name, "codegenasm") == 0)
|
|
|
|
return CompileFormat::CodegenAsm;
|
|
|
|
else if (strcmp(name, "codegenir") == 0)
|
|
|
|
return CompileFormat::CodegenIr;
|
|
|
|
else if (strcmp(name, "codegenverbose") == 0)
|
|
|
|
return CompileFormat::CodegenVerbose;
|
|
|
|
else if (strcmp(name, "codegennull") == 0)
|
|
|
|
return CompileFormat::CodegenNull;
|
|
|
|
else if (strcmp(name, "null") == 0)
|
|
|
|
return CompileFormat::Null;
|
|
|
|
else
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void report(const char* name, const Luau::Location& location, const char* type, const char* message)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s(%d,%d): %s: %s\n", name, location.begin.line + 1, location.begin.column + 1, type, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reportError(const char* name, const Luau::ParseError& error)
|
|
|
|
{
|
|
|
|
report(name, error.getLocation(), "SyntaxError", error.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reportError(const char* name, const Luau::CompileError& error)
|
|
|
|
{
|
|
|
|
report(name, error.getLocation(), "CompileError", error.what());
|
|
|
|
}
|
|
|
|
|
2023-09-16 01:26:59 +08:00
|
|
|
static std::string getCodegenAssembly(
|
2024-08-02 22:30:04 +08:00
|
|
|
const char* name,
|
|
|
|
const std::string& bytecode,
|
|
|
|
Luau::CodeGen::AssemblyOptions options,
|
|
|
|
Luau::CodeGen::LoweringStats* stats
|
|
|
|
)
|
2023-06-10 01:08:00 +08:00
|
|
|
{
|
|
|
|
std::unique_ptr<lua_State, void (*)(lua_State*)> globalState(luaL_newstate(), lua_close);
|
|
|
|
lua_State* L = globalState.get();
|
|
|
|
|
|
|
|
if (luau_load(L, name, bytecode.data(), bytecode.size(), 0) == 0)
|
2023-09-16 01:26:59 +08:00
|
|
|
return Luau::CodeGen::getAssembly(L, -1, options, stats);
|
2023-06-10 01:08:00 +08:00
|
|
|
|
|
|
|
fprintf(stderr, "Error loading bytecode %s\n", name);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void annotateInstruction(void* context, std::string& text, int fid, int instpos)
|
|
|
|
{
|
|
|
|
Luau::BytecodeBuilder& bcb = *(Luau::BytecodeBuilder*)context;
|
|
|
|
|
|
|
|
bcb.annotateInstruction(text, fid, instpos);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CompileStats
|
|
|
|
{
|
|
|
|
size_t lines;
|
|
|
|
size_t bytecode;
|
2023-11-04 07:45:04 +08:00
|
|
|
size_t bytecodeInstructionCount;
|
2023-06-10 01:08:00 +08:00
|
|
|
size_t codegen;
|
|
|
|
|
|
|
|
double readTime;
|
|
|
|
double miscTime;
|
|
|
|
double parseTime;
|
|
|
|
double compileTime;
|
|
|
|
double codegenTime;
|
2023-09-16 01:26:59 +08:00
|
|
|
|
|
|
|
Luau::CodeGen::LoweringStats lowerStats;
|
2023-10-21 09:10:30 +08:00
|
|
|
|
|
|
|
CompileStats& operator+=(const CompileStats& that)
|
|
|
|
{
|
|
|
|
this->lines += that.lines;
|
|
|
|
this->bytecode += that.bytecode;
|
2023-11-04 07:45:04 +08:00
|
|
|
this->bytecodeInstructionCount += that.bytecodeInstructionCount;
|
2023-10-21 09:10:30 +08:00
|
|
|
this->codegen += that.codegen;
|
|
|
|
this->readTime += that.readTime;
|
|
|
|
this->miscTime += that.miscTime;
|
|
|
|
this->parseTime += that.parseTime;
|
|
|
|
this->compileTime += that.compileTime;
|
|
|
|
this->codegenTime += that.codegenTime;
|
|
|
|
this->lowerStats += that.lowerStats;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
CompileStats operator+(const CompileStats& other) const
|
|
|
|
{
|
|
|
|
CompileStats result(*this);
|
|
|
|
result += other;
|
|
|
|
return result;
|
|
|
|
}
|
2023-06-10 01:08:00 +08:00
|
|
|
};
|
|
|
|
|
2024-01-13 06:25:27 +08:00
|
|
|
#define WRITE_NAME(INDENT, NAME) fprintf(fp, INDENT "\"" #NAME "\": ")
|
|
|
|
#define WRITE_PAIR(INDENT, NAME, FORMAT) fprintf(fp, INDENT "\"" #NAME "\": " FORMAT, stats.NAME)
|
|
|
|
#define WRITE_PAIR_STRING(INDENT, NAME, FORMAT) fprintf(fp, INDENT "\"" #NAME "\": " FORMAT, stats.NAME.c_str())
|
|
|
|
|
|
|
|
void serializeFunctionStats(FILE* fp, const Luau::CodeGen::FunctionStats& stats)
|
|
|
|
{
|
|
|
|
fprintf(fp, " {\n");
|
|
|
|
WRITE_PAIR_STRING(" ", name, "\"%s\",\n");
|
|
|
|
WRITE_PAIR(" ", line, "%d,\n");
|
|
|
|
WRITE_PAIR(" ", bcodeCount, "%u,\n");
|
|
|
|
WRITE_PAIR(" ", irCount, "%u,\n");
|
|
|
|
WRITE_PAIR(" ", asmCount, "%u,\n");
|
|
|
|
WRITE_PAIR(" ", asmSize, "%u,\n");
|
|
|
|
|
|
|
|
WRITE_NAME(" ", bytecodeSummary);
|
|
|
|
const size_t nestingLimit = stats.bytecodeSummary.size();
|
|
|
|
|
|
|
|
if (nestingLimit == 0)
|
|
|
|
fprintf(fp, "[]");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(fp, "[\n");
|
|
|
|
for (size_t i = 0; i < nestingLimit; ++i)
|
|
|
|
{
|
|
|
|
const std::vector<unsigned>& counts = stats.bytecodeSummary[i];
|
|
|
|
fprintf(fp, " [");
|
|
|
|
for (size_t j = 0; j < counts.size(); ++j)
|
|
|
|
{
|
|
|
|
fprintf(fp, "%u", counts[j]);
|
|
|
|
if (j < counts.size() - 1)
|
|
|
|
fprintf(fp, ", ");
|
|
|
|
}
|
|
|
|
fprintf(fp, "]");
|
|
|
|
if (i < stats.bytecodeSummary.size() - 1)
|
|
|
|
fprintf(fp, ",\n");
|
|
|
|
}
|
|
|
|
fprintf(fp, "\n ]");
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(fp, "\n }");
|
|
|
|
}
|
|
|
|
|
|
|
|
void serializeBlockLinearizationStats(FILE* fp, const Luau::CodeGen::BlockLinearizationStats& stats)
|
|
|
|
{
|
|
|
|
fprintf(fp, "{\n");
|
|
|
|
|
|
|
|
WRITE_PAIR(" ", constPropInstructionCount, "%u,\n");
|
|
|
|
WRITE_PAIR(" ", timeSeconds, "%f\n");
|
|
|
|
|
|
|
|
fprintf(fp, " }");
|
|
|
|
}
|
|
|
|
|
|
|
|
void serializeLoweringStats(FILE* fp, const Luau::CodeGen::LoweringStats& stats)
|
|
|
|
{
|
|
|
|
fprintf(fp, "{\n");
|
|
|
|
|
|
|
|
WRITE_PAIR(" ", totalFunctions, "%u,\n");
|
|
|
|
WRITE_PAIR(" ", skippedFunctions, "%u,\n");
|
|
|
|
WRITE_PAIR(" ", spillsToSlot, "%d,\n");
|
|
|
|
WRITE_PAIR(" ", spillsToRestore, "%d,\n");
|
|
|
|
WRITE_PAIR(" ", maxSpillSlotsUsed, "%u,\n");
|
|
|
|
WRITE_PAIR(" ", blocksPreOpt, "%u,\n");
|
|
|
|
WRITE_PAIR(" ", blocksPostOpt, "%u,\n");
|
|
|
|
WRITE_PAIR(" ", maxBlockInstructions, "%u,\n");
|
|
|
|
WRITE_PAIR(" ", regAllocErrors, "%d,\n");
|
|
|
|
WRITE_PAIR(" ", loweringErrors, "%d,\n");
|
|
|
|
|
|
|
|
WRITE_NAME(" ", blockLinearizationStats);
|
|
|
|
serializeBlockLinearizationStats(fp, stats.blockLinearizationStats);
|
|
|
|
fprintf(fp, ",\n");
|
|
|
|
|
|
|
|
WRITE_NAME(" ", functions);
|
|
|
|
const size_t functionCount = stats.functions.size();
|
|
|
|
|
|
|
|
if (functionCount == 0)
|
|
|
|
fprintf(fp, "[]");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(fp, "[\n");
|
|
|
|
for (size_t i = 0; i < functionCount; ++i)
|
|
|
|
{
|
|
|
|
serializeFunctionStats(fp, stats.functions[i]);
|
|
|
|
if (i < functionCount - 1)
|
|
|
|
fprintf(fp, ",\n");
|
|
|
|
}
|
|
|
|
fprintf(fp, "\n ]");
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(fp, "\n }");
|
|
|
|
}
|
|
|
|
|
|
|
|
void serializeCompileStats(FILE* fp, const CompileStats& stats)
|
|
|
|
{
|
|
|
|
fprintf(fp, "{\n");
|
|
|
|
|
|
|
|
WRITE_PAIR(" ", lines, "%zu,\n");
|
|
|
|
WRITE_PAIR(" ", bytecode, "%zu,\n");
|
|
|
|
WRITE_PAIR(" ", bytecodeInstructionCount, "%zu,\n");
|
|
|
|
WRITE_PAIR(" ", codegen, "%zu,\n");
|
|
|
|
WRITE_PAIR(" ", readTime, "%f,\n");
|
|
|
|
WRITE_PAIR(" ", miscTime, "%f,\n");
|
|
|
|
WRITE_PAIR(" ", parseTime, "%f,\n");
|
|
|
|
WRITE_PAIR(" ", compileTime, "%f,\n");
|
|
|
|
WRITE_PAIR(" ", codegenTime, "%f,\n");
|
|
|
|
|
|
|
|
WRITE_NAME(" ", lowerStats);
|
|
|
|
serializeLoweringStats(fp, stats.lowerStats);
|
|
|
|
|
|
|
|
fprintf(fp, "\n }");
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef WRITE_NAME
|
|
|
|
#undef WRITE_PAIR
|
|
|
|
#undef WRITE_PAIR_STRING
|
|
|
|
|
2023-06-10 01:08:00 +08:00
|
|
|
static double recordDeltaTime(double& timer)
|
|
|
|
{
|
|
|
|
double now = Luau::TimeTrace::getClock();
|
|
|
|
double delta = now - timer;
|
|
|
|
timer = now;
|
|
|
|
return delta;
|
|
|
|
}
|
|
|
|
|
2023-06-24 14:19:39 +08:00
|
|
|
static bool compileFile(const char* name, CompileFormat format, Luau::CodeGen::AssemblyOptions::Target assemblyTarget, CompileStats& stats)
|
2023-06-10 01:08:00 +08:00
|
|
|
{
|
|
|
|
double currts = Luau::TimeTrace::getClock();
|
|
|
|
|
|
|
|
std::optional<std::string> source = readFile(name);
|
|
|
|
if (!source)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error opening %s\n", name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
stats.readTime += recordDeltaTime(currts);
|
|
|
|
|
|
|
|
// NOTE: Normally, you should use Luau::compile or luau_compile (see lua_require as an example)
|
|
|
|
// This function is much more complicated because it supports many output human-readable formats through internal interfaces
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Luau::BytecodeBuilder bcb;
|
|
|
|
|
|
|
|
Luau::CodeGen::AssemblyOptions options;
|
2023-06-24 14:19:39 +08:00
|
|
|
options.target = assemblyTarget;
|
2023-06-10 01:08:00 +08:00
|
|
|
options.outputBinary = format == CompileFormat::CodegenNull;
|
|
|
|
|
|
|
|
if (!options.outputBinary)
|
|
|
|
{
|
|
|
|
options.includeAssembly = format != CompileFormat::CodegenIr;
|
|
|
|
options.includeIr = format != CompileFormat::CodegenAsm;
|
2024-05-04 04:17:51 +08:00
|
|
|
options.includeIrTypes = format != CompileFormat::CodegenAsm;
|
2023-06-10 01:08:00 +08:00
|
|
|
options.includeOutlinedCode = format == CompileFormat::CodegenVerbose;
|
|
|
|
}
|
|
|
|
|
|
|
|
options.annotator = annotateInstruction;
|
|
|
|
options.annotatorContext = &bcb;
|
|
|
|
|
|
|
|
if (format == CompileFormat::Text)
|
|
|
|
{
|
2024-08-02 22:30:04 +08:00
|
|
|
bcb.setDumpFlags(
|
|
|
|
Luau::BytecodeBuilder::Dump_Code | Luau::BytecodeBuilder::Dump_Source | Luau::BytecodeBuilder::Dump_Locals |
|
|
|
|
Luau::BytecodeBuilder::Dump_Remarks | Luau::BytecodeBuilder::Dump_Types
|
|
|
|
);
|
2023-06-10 01:08:00 +08:00
|
|
|
bcb.setDumpSource(*source);
|
|
|
|
}
|
|
|
|
else if (format == CompileFormat::Remarks)
|
|
|
|
{
|
|
|
|
bcb.setDumpFlags(Luau::BytecodeBuilder::Dump_Source | Luau::BytecodeBuilder::Dump_Remarks);
|
|
|
|
bcb.setDumpSource(*source);
|
|
|
|
}
|
2024-08-02 22:30:04 +08:00
|
|
|
else if (format == CompileFormat::Codegen || format == CompileFormat::CodegenAsm || format == CompileFormat::CodegenIr || format == CompileFormat::CodegenVerbose)
|
2023-06-10 01:08:00 +08:00
|
|
|
{
|
2024-08-02 22:30:04 +08:00
|
|
|
bcb.setDumpFlags(
|
|
|
|
Luau::BytecodeBuilder::Dump_Code | Luau::BytecodeBuilder::Dump_Source | Luau::BytecodeBuilder::Dump_Locals |
|
|
|
|
Luau::BytecodeBuilder::Dump_Remarks
|
|
|
|
);
|
2023-06-10 01:08:00 +08:00
|
|
|
bcb.setDumpSource(*source);
|
|
|
|
}
|
|
|
|
|
|
|
|
stats.miscTime += recordDeltaTime(currts);
|
|
|
|
|
|
|
|
Luau::Allocator allocator;
|
|
|
|
Luau::AstNameTable names(allocator);
|
|
|
|
Luau::ParseResult result = Luau::Parser::parse(source->c_str(), source->size(), names, allocator);
|
|
|
|
|
|
|
|
if (!result.errors.empty())
|
|
|
|
throw Luau::ParseErrors(result.errors);
|
|
|
|
|
|
|
|
stats.lines += result.lines;
|
|
|
|
stats.parseTime += recordDeltaTime(currts);
|
|
|
|
|
|
|
|
Luau::compileOrThrow(bcb, result, names, copts());
|
|
|
|
stats.bytecode += bcb.getBytecode().size();
|
2023-11-04 07:45:04 +08:00
|
|
|
stats.bytecodeInstructionCount = bcb.getTotalInstructionCount();
|
2023-06-10 01:08:00 +08:00
|
|
|
stats.compileTime += recordDeltaTime(currts);
|
|
|
|
|
|
|
|
switch (format)
|
|
|
|
{
|
|
|
|
case CompileFormat::Text:
|
|
|
|
printf("%s", bcb.dumpEverything().c_str());
|
|
|
|
break;
|
|
|
|
case CompileFormat::Remarks:
|
|
|
|
printf("%s", bcb.dumpSourceRemarks().c_str());
|
|
|
|
break;
|
|
|
|
case CompileFormat::Binary:
|
|
|
|
fwrite(bcb.getBytecode().data(), 1, bcb.getBytecode().size(), stdout);
|
|
|
|
break;
|
|
|
|
case CompileFormat::Codegen:
|
|
|
|
case CompileFormat::CodegenAsm:
|
|
|
|
case CompileFormat::CodegenIr:
|
|
|
|
case CompileFormat::CodegenVerbose:
|
2023-09-16 01:26:59 +08:00
|
|
|
printf("%s", getCodegenAssembly(name, bcb.getBytecode(), options, &stats.lowerStats).c_str());
|
2023-06-10 01:08:00 +08:00
|
|
|
break;
|
|
|
|
case CompileFormat::CodegenNull:
|
2023-09-16 01:26:59 +08:00
|
|
|
stats.codegen += getCodegenAssembly(name, bcb.getBytecode(), options, &stats.lowerStats).size();
|
2023-06-10 01:08:00 +08:00
|
|
|
stats.codegenTime += recordDeltaTime(currts);
|
|
|
|
break;
|
|
|
|
case CompileFormat::Null:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (Luau::ParseErrors& e)
|
|
|
|
{
|
|
|
|
for (auto& error : e.getErrors())
|
|
|
|
reportError(name, error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch (Luau::CompileError& e)
|
|
|
|
{
|
|
|
|
reportError(name, e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void displayHelp(const char* argv0)
|
|
|
|
{
|
|
|
|
printf("Usage: %s [--mode] [options] [file list]\n", argv0);
|
|
|
|
printf("\n");
|
|
|
|
printf("Available modes:\n");
|
|
|
|
printf(" binary, text, remarks, codegen\n");
|
|
|
|
printf("\n");
|
|
|
|
printf("Available options:\n");
|
|
|
|
printf(" -h, --help: Display this usage message.\n");
|
|
|
|
printf(" -O<n>: compile with optimization level n (default 1, n should be between 0 and 2).\n");
|
|
|
|
printf(" -g<n>: compile with debug level n (default 1, n should be between 0 and 2).\n");
|
2023-06-24 14:19:39 +08:00
|
|
|
printf(" --target=<target>: compile code for specific architecture (a64, x64, a64_nf, x64_ms).\n");
|
2023-06-10 01:08:00 +08:00
|
|
|
printf(" --timetrace: record compiler time tracing information into trace.json\n");
|
2024-01-13 06:25:27 +08:00
|
|
|
printf(" --record-stats=<granularity>: granularity of compilation stats (total, file, function).\n");
|
|
|
|
printf(" --bytecode-summary: Compute bytecode operation distribution.\n");
|
2023-12-02 15:46:57 +08:00
|
|
|
printf(" --stats-file=<filename>: file in which compilation stats will be recored (default 'stats.json').\n");
|
|
|
|
printf(" --vector-lib=<name>: name of the library providing vector type operations.\n");
|
|
|
|
printf(" --vector-ctor=<name>: name of the function constructing a vector value.\n");
|
|
|
|
printf(" --vector-type=<name>: name of the vector type.\n");
|
2023-06-10 01:08:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int assertionHandler(const char* expr, const char* file, int line, const char* function)
|
|
|
|
{
|
|
|
|
printf("%s(%d): ASSERTION FAILED: %s\n", file, line, expr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-11-04 07:45:04 +08:00
|
|
|
std::string escapeFilename(const std::string& filename)
|
|
|
|
{
|
|
|
|
std::string escaped;
|
|
|
|
escaped.reserve(filename.size());
|
|
|
|
|
|
|
|
for (const char ch : filename)
|
|
|
|
{
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case '\\':
|
|
|
|
escaped.push_back('/');
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
escaped.push_back('\\');
|
|
|
|
escaped.push_back(ch);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
escaped.push_back(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return escaped;
|
|
|
|
}
|
|
|
|
|
2023-06-10 01:08:00 +08:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
Luau::assertHandler() = assertionHandler;
|
|
|
|
|
|
|
|
setLuauFlagsDefault();
|
|
|
|
|
|
|
|
CompileFormat compileFormat = CompileFormat::Text;
|
2023-06-24 14:19:39 +08:00
|
|
|
Luau::CodeGen::AssemblyOptions::Target assemblyTarget = Luau::CodeGen::AssemblyOptions::Host;
|
2023-10-21 09:10:30 +08:00
|
|
|
RecordStats recordStats = RecordStats::None;
|
2023-11-04 07:45:04 +08:00
|
|
|
std::string statsFile("stats.json");
|
2024-01-13 06:25:27 +08:00
|
|
|
bool bytecodeSummary = false;
|
2023-06-10 01:08:00 +08:00
|
|
|
|
|
|
|
for (int i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
|
|
|
|
{
|
|
|
|
displayHelp(argv[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (strncmp(argv[i], "-O", 2) == 0)
|
|
|
|
{
|
|
|
|
int level = atoi(argv[i] + 2);
|
|
|
|
if (level < 0 || level > 2)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error: Optimization level must be between 0 and 2 inclusive.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
globalOptions.optimizationLevel = level;
|
|
|
|
}
|
|
|
|
else if (strncmp(argv[i], "-g", 2) == 0)
|
|
|
|
{
|
|
|
|
int level = atoi(argv[i] + 2);
|
|
|
|
if (level < 0 || level > 2)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error: Debug level must be between 0 and 2 inclusive.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
globalOptions.debugLevel = level;
|
|
|
|
}
|
2024-04-26 06:26:09 +08:00
|
|
|
else if (strncmp(argv[i], "-t", 2) == 0)
|
|
|
|
{
|
|
|
|
int level = atoi(argv[i] + 2);
|
|
|
|
if (level < 0 || level > 1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error: Type info level must be between 0 and 1 inclusive.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
globalOptions.typeInfoLevel = level;
|
|
|
|
}
|
2023-06-24 14:19:39 +08:00
|
|
|
else if (strncmp(argv[i], "--target=", 9) == 0)
|
|
|
|
{
|
|
|
|
const char* value = argv[i] + 9;
|
|
|
|
|
|
|
|
if (strcmp(value, "a64") == 0)
|
|
|
|
assemblyTarget = Luau::CodeGen::AssemblyOptions::A64;
|
|
|
|
else if (strcmp(value, "a64_nf") == 0)
|
|
|
|
assemblyTarget = Luau::CodeGen::AssemblyOptions::A64_NoFeatures;
|
|
|
|
else if (strcmp(value, "x64") == 0)
|
|
|
|
assemblyTarget = Luau::CodeGen::AssemblyOptions::X64_SystemV;
|
|
|
|
else if (strcmp(value, "x64_ms") == 0)
|
|
|
|
assemblyTarget = Luau::CodeGen::AssemblyOptions::X64_Windows;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error: unknown target\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2023-06-10 01:08:00 +08:00
|
|
|
else if (strcmp(argv[i], "--timetrace") == 0)
|
|
|
|
{
|
|
|
|
FFlag::DebugLuauTimeTracing.value = true;
|
|
|
|
}
|
2023-10-21 09:10:30 +08:00
|
|
|
else if (strncmp(argv[i], "--record-stats=", 15) == 0)
|
|
|
|
{
|
|
|
|
const char* value = argv[i] + 15;
|
|
|
|
|
|
|
|
if (strcmp(value, "total") == 0)
|
|
|
|
recordStats = RecordStats::Total;
|
2023-12-02 15:46:57 +08:00
|
|
|
else if (strcmp(value, "file") == 0)
|
|
|
|
recordStats = RecordStats::File;
|
|
|
|
else if (strcmp(value, "function") == 0)
|
|
|
|
recordStats = RecordStats::Function;
|
2023-10-21 09:10:30 +08:00
|
|
|
else
|
|
|
|
{
|
2024-01-13 06:25:27 +08:00
|
|
|
fprintf(stderr, "Error: unknown 'granularity' for '--record-stats'.\n");
|
2023-10-21 09:10:30 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2024-01-13 06:25:27 +08:00
|
|
|
else if (strncmp(argv[i], "--bytecode-summary", 18) == 0)
|
|
|
|
{
|
|
|
|
bytecodeSummary = true;
|
|
|
|
}
|
2023-11-04 07:45:04 +08:00
|
|
|
else if (strncmp(argv[i], "--stats-file=", 13) == 0)
|
|
|
|
{
|
|
|
|
statsFile = argv[i] + 13;
|
|
|
|
|
|
|
|
if (statsFile.size() == 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error: filename missing for '--stats-file'.\n\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2023-06-10 01:08:00 +08:00
|
|
|
else if (strncmp(argv[i], "--fflags=", 9) == 0)
|
|
|
|
{
|
|
|
|
setLuauFlags(argv[i] + 9);
|
|
|
|
}
|
2023-12-02 15:46:57 +08:00
|
|
|
else if (strncmp(argv[i], "--vector-lib=", 13) == 0)
|
|
|
|
{
|
|
|
|
globalOptions.vectorLib = argv[i] + 13;
|
|
|
|
}
|
|
|
|
else if (strncmp(argv[i], "--vector-ctor=", 14) == 0)
|
|
|
|
{
|
|
|
|
globalOptions.vectorCtor = argv[i] + 14;
|
|
|
|
}
|
|
|
|
else if (strncmp(argv[i], "--vector-type=", 14) == 0)
|
|
|
|
{
|
|
|
|
globalOptions.vectorType = argv[i] + 14;
|
|
|
|
}
|
2023-06-10 01:08:00 +08:00
|
|
|
else if (argv[i][0] == '-' && argv[i][1] == '-' && getCompileFormat(argv[i] + 2))
|
|
|
|
{
|
|
|
|
compileFormat = *getCompileFormat(argv[i] + 2);
|
|
|
|
}
|
|
|
|
else if (argv[i][0] == '-')
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error: Unrecognized option '%s'.\n\n", argv[i]);
|
|
|
|
displayHelp(argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-13 06:25:27 +08:00
|
|
|
if (bytecodeSummary && (recordStats != RecordStats::Function))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "'Error: Required '--record-stats=function' for '--bytecode-summary'.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-06-10 01:08:00 +08:00
|
|
|
#if !defined(LUAU_ENABLE_TIME_TRACE)
|
|
|
|
if (FFlag::DebugLuauTimeTracing)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "To run with --timetrace, Luau has to be built with LUAU_ENABLE_TIME_TRACE enabled\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const std::vector<std::string> files = getSourceFiles(argc, argv);
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (compileFormat == CompileFormat::Binary)
|
|
|
|
_setmode(_fileno(stdout), _O_BINARY);
|
|
|
|
#endif
|
|
|
|
|
2023-10-21 09:10:30 +08:00
|
|
|
const size_t fileCount = files.size();
|
2023-06-10 01:08:00 +08:00
|
|
|
CompileStats stats = {};
|
2023-10-21 09:10:30 +08:00
|
|
|
|
|
|
|
std::vector<CompileStats> fileStats;
|
2023-12-02 15:46:57 +08:00
|
|
|
if (recordStats == RecordStats::File || recordStats == RecordStats::Function)
|
2023-10-21 09:10:30 +08:00
|
|
|
fileStats.reserve(fileCount);
|
|
|
|
|
2023-06-10 01:08:00 +08:00
|
|
|
int failed = 0;
|
2024-01-13 06:25:27 +08:00
|
|
|
unsigned functionStats = (recordStats == RecordStats::Function ? Luau::CodeGen::FunctionStats_Enable : 0) |
|
|
|
|
(bytecodeSummary ? Luau::CodeGen::FunctionStats_BytecodeSummary : 0);
|
2023-06-10 01:08:00 +08:00
|
|
|
for (const std::string& path : files)
|
2023-10-21 09:10:30 +08:00
|
|
|
{
|
|
|
|
CompileStats fileStat = {};
|
2024-01-13 06:25:27 +08:00
|
|
|
fileStat.lowerStats.functionStatsFlags = functionStats;
|
2023-10-21 09:10:30 +08:00
|
|
|
failed += !compileFile(path.c_str(), compileFormat, assemblyTarget, fileStat);
|
|
|
|
stats += fileStat;
|
2023-12-02 15:46:57 +08:00
|
|
|
if (recordStats == RecordStats::File || recordStats == RecordStats::Function)
|
2023-10-21 09:10:30 +08:00
|
|
|
fileStats.push_back(fileStat);
|
|
|
|
}
|
2023-06-10 01:08:00 +08:00
|
|
|
|
|
|
|
if (compileFormat == CompileFormat::Null)
|
2023-09-16 01:26:59 +08:00
|
|
|
{
|
2024-08-02 22:30:04 +08:00
|
|
|
printf(
|
|
|
|
"Compiled %d KLOC into %d KB bytecode (read %.2fs, parse %.2fs, compile %.2fs)\n",
|
|
|
|
int(stats.lines / 1000),
|
|
|
|
int(stats.bytecode / 1024),
|
|
|
|
stats.readTime,
|
|
|
|
stats.parseTime,
|
|
|
|
stats.compileTime
|
|
|
|
);
|
2023-09-16 01:26:59 +08:00
|
|
|
}
|
2023-06-10 01:08:00 +08:00
|
|
|
else if (compileFormat == CompileFormat::CodegenNull)
|
2023-09-16 01:26:59 +08:00
|
|
|
{
|
2024-08-02 22:30:04 +08:00
|
|
|
printf(
|
|
|
|
"Compiled %d KLOC into %d KB bytecode => %d KB native code (%.2fx) (read %.2fs, parse %.2fs, compile %.2fs, codegen %.2fs)\n",
|
|
|
|
int(stats.lines / 1000),
|
|
|
|
int(stats.bytecode / 1024),
|
|
|
|
int(stats.codegen / 1024),
|
|
|
|
stats.bytecode == 0 ? 0.0 : double(stats.codegen) / double(stats.bytecode),
|
|
|
|
stats.readTime,
|
|
|
|
stats.parseTime,
|
|
|
|
stats.compileTime,
|
|
|
|
stats.codegenTime
|
|
|
|
);
|
|
|
|
|
|
|
|
printf(
|
|
|
|
"Lowering: regalloc failed: %d, lowering failed %d; spills to stack: %d, spills to restore: %d, max spill slot %u\n",
|
|
|
|
stats.lowerStats.regAllocErrors,
|
|
|
|
stats.lowerStats.loweringErrors,
|
|
|
|
stats.lowerStats.spillsToSlot,
|
|
|
|
stats.lowerStats.spillsToRestore,
|
|
|
|
stats.lowerStats.maxSpillSlotsUsed
|
|
|
|
);
|
2023-09-16 01:26:59 +08:00
|
|
|
}
|
|
|
|
|
2023-10-21 09:10:30 +08:00
|
|
|
if (recordStats != RecordStats::None)
|
|
|
|
{
|
2023-11-04 07:45:04 +08:00
|
|
|
FILE* fp = fopen(statsFile.c_str(), "w");
|
2023-10-21 09:10:30 +08:00
|
|
|
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Unable to open 'stats.json'\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (recordStats == RecordStats::Total)
|
|
|
|
{
|
2024-01-13 06:25:27 +08:00
|
|
|
serializeCompileStats(fp, stats);
|
2023-10-21 09:10:30 +08:00
|
|
|
}
|
2023-12-02 15:46:57 +08:00
|
|
|
else if (recordStats == RecordStats::File || recordStats == RecordStats::Function)
|
2023-10-21 09:10:30 +08:00
|
|
|
{
|
|
|
|
fprintf(fp, "{\n");
|
|
|
|
for (size_t i = 0; i < fileCount; ++i)
|
|
|
|
{
|
2023-11-04 07:45:04 +08:00
|
|
|
std::string escaped(escapeFilename(files[i]));
|
2024-01-13 06:25:27 +08:00
|
|
|
fprintf(fp, " \"%s\": ", escaped.c_str());
|
|
|
|
serializeCompileStats(fp, fileStats[i]);
|
2023-10-21 09:10:30 +08:00
|
|
|
fprintf(fp, i == (fileCount - 1) ? "\n" : ",\n");
|
|
|
|
}
|
|
|
|
fprintf(fp, "}");
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2023-06-10 01:08:00 +08:00
|
|
|
return failed ? 1 : 0;
|
|
|
|
}
|