2022-10-14 20:48:41 +01:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#pragma once
|
|
|
|
|
2025-01-17 22:55:39 +00:00
|
|
|
#include "Luau/CodeGenCommon.h"
|
|
|
|
#include "Luau/CodeGenOptions.h"
|
|
|
|
#include "Luau/LoweringStats.h"
|
|
|
|
|
2024-04-12 18:18:49 +01:00
|
|
|
#include <array>
|
|
|
|
#include <memory>
|
2022-10-14 20:48:41 +01:00
|
|
|
#include <string>
|
2023-12-02 07:46:57 +00:00
|
|
|
#include <vector>
|
2022-10-14 20:48:41 +01:00
|
|
|
|
2023-09-15 18:26:59 +01:00
|
|
|
#include <stddef.h>
|
2023-05-12 18:50:47 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2022-10-14 20:48:41 +01:00
|
|
|
struct lua_State;
|
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
namespace CodeGen
|
|
|
|
{
|
|
|
|
|
2024-02-16 02:04:39 +00:00
|
|
|
// These enum values can be reported through telemetry.
|
|
|
|
// To ensure consistency, changes should be additive.
|
2023-08-25 18:23:55 +01:00
|
|
|
enum class CodeGenCompilationResult
|
|
|
|
{
|
2024-02-16 02:04:39 +00:00
|
|
|
Success = 0, // Successfully generated code for at least one function
|
|
|
|
NothingToCompile = 1, // There were no new functions to compile
|
|
|
|
NotNativeModule = 2, // Module does not have `--!native` comment
|
2023-08-25 18:23:55 +01:00
|
|
|
|
2024-02-16 02:04:39 +00:00
|
|
|
CodeGenNotInitialized = 3, // Native codegen system is not initialized
|
|
|
|
CodeGenOverflowInstructionLimit = 4, // Instruction limit overflow
|
|
|
|
CodeGenOverflowBlockLimit = 5, // Block limit overflow
|
|
|
|
CodeGenOverflowBlockInstructionLimit = 6, // Block instruction limit overflow
|
|
|
|
CodeGenAssemblerFinalizationFailure = 7, // Failure during assembler finalization
|
|
|
|
CodeGenLoweringFailure = 8, // Lowering failed
|
|
|
|
AllocationFailed = 9, // Native codegen failed due to an allocation error
|
2024-05-03 21:17:51 +01:00
|
|
|
|
|
|
|
Count = 10,
|
2023-08-25 18:23:55 +01:00
|
|
|
};
|
|
|
|
|
2024-05-03 21:17:51 +01:00
|
|
|
std::string toString(const CodeGenCompilationResult& result);
|
|
|
|
|
2024-03-30 23:14:44 +00:00
|
|
|
struct ProtoCompilationFailure
|
|
|
|
{
|
|
|
|
CodeGenCompilationResult result = CodeGenCompilationResult::Success;
|
|
|
|
|
|
|
|
std::string debugname;
|
|
|
|
int line = -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CompilationResult
|
|
|
|
{
|
|
|
|
CodeGenCompilationResult result = CodeGenCompilationResult::Success;
|
|
|
|
|
|
|
|
std::vector<ProtoCompilationFailure> protoFailures;
|
|
|
|
|
|
|
|
[[nodiscard]] bool hasErrors() const
|
|
|
|
{
|
|
|
|
return result != CodeGenCompilationResult::Success || !protoFailures.empty();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-08-11 15:42:37 +01:00
|
|
|
struct CompilationStats
|
|
|
|
{
|
|
|
|
size_t bytecodeSizeBytes = 0;
|
|
|
|
size_t nativeCodeSizeBytes = 0;
|
|
|
|
size_t nativeDataSizeBytes = 0;
|
|
|
|
size_t nativeMetadataSizeBytes = 0;
|
|
|
|
|
2024-02-16 02:04:39 +00:00
|
|
|
uint32_t functionsTotal = 0;
|
2023-08-11 15:42:37 +01:00
|
|
|
uint32_t functionsCompiled = 0;
|
2024-04-12 18:18:49 +01:00
|
|
|
uint32_t functionsBound = 0;
|
2023-08-11 15:42:37 +01:00
|
|
|
};
|
|
|
|
|
2022-10-14 20:48:41 +01:00
|
|
|
bool isSupported();
|
|
|
|
|
2024-04-12 18:18:49 +01:00
|
|
|
class SharedCodeGenContext;
|
|
|
|
|
|
|
|
struct SharedCodeGenContextDeleter
|
|
|
|
{
|
|
|
|
void operator()(const SharedCodeGenContext* context) const noexcept;
|
|
|
|
};
|
|
|
|
|
|
|
|
using UniqueSharedCodeGenContext = std::unique_ptr<SharedCodeGenContext, SharedCodeGenContextDeleter>;
|
|
|
|
|
|
|
|
// Creates a new SharedCodeGenContext that can be used by multiple Luau VMs
|
|
|
|
// concurrently, using either the default allocator parameters or custom
|
|
|
|
// allocator parameters.
|
|
|
|
[[nodiscard]] UniqueSharedCodeGenContext createSharedCodeGenContext();
|
|
|
|
|
|
|
|
[[nodiscard]] UniqueSharedCodeGenContext createSharedCodeGenContext(AllocationCallback* allocationCallback, void* allocationCallbackContext);
|
|
|
|
|
|
|
|
[[nodiscard]] UniqueSharedCodeGenContext createSharedCodeGenContext(
|
2024-08-02 15:30:04 +01:00
|
|
|
size_t blockSize,
|
|
|
|
size_t maxTotalSize,
|
|
|
|
AllocationCallback* allocationCallback,
|
|
|
|
void* allocationCallbackContext
|
|
|
|
);
|
2024-04-12 18:18:49 +01:00
|
|
|
|
|
|
|
// Destroys the provided SharedCodeGenContext. All Luau VMs using the
|
|
|
|
// SharedCodeGenContext must be destroyed before this function is called.
|
|
|
|
void destroySharedCodeGenContext(const SharedCodeGenContext* codeGenContext) noexcept;
|
|
|
|
|
2024-05-26 18:09:09 +01:00
|
|
|
// Initializes native code-gen on the provided Luau VM, using a VM-specific
|
|
|
|
// code-gen context and either the default allocator parameters or custom
|
|
|
|
// allocator parameters.
|
2022-10-14 20:48:41 +01:00
|
|
|
void create(lua_State* L);
|
2024-05-26 18:09:09 +01:00
|
|
|
void create(lua_State* L, AllocationCallback* allocationCallback, void* allocationCallbackContext);
|
|
|
|
void create(lua_State* L, size_t blockSize, size_t maxTotalSize, AllocationCallback* allocationCallback, void* allocationCallbackContext);
|
|
|
|
|
|
|
|
// Initializes native code-gen on the provided Luau VM, using the provided
|
|
|
|
// SharedCodeGenContext. Note that after this function is called, the
|
|
|
|
// SharedCodeGenContext must not be destroyed until after the Luau VM L is
|
|
|
|
// destroyed via lua_close.
|
2024-04-12 18:18:49 +01:00
|
|
|
void create(lua_State* L, SharedCodeGenContext* codeGenContext);
|
2022-10-14 20:48:41 +01:00
|
|
|
|
2024-03-15 23:37:39 +00:00
|
|
|
// Check if native execution is enabled
|
|
|
|
[[nodiscard]] bool isNativeExecutionEnabled(lua_State* L);
|
|
|
|
|
|
|
|
// Enable or disable native execution according to `enabled` argument
|
|
|
|
void setNativeExecutionEnabled(lua_State* L, bool enabled);
|
|
|
|
|
2024-05-31 20:18:18 +01:00
|
|
|
// Given a name, this function must return the index of the type which matches the type array used all CompilationOptions and AssemblyOptions
|
|
|
|
// If the type is unknown, 0xff has to be returned
|
|
|
|
using UserdataRemapperCallback = uint8_t(void* context, const char* name, size_t nameLength);
|
|
|
|
|
|
|
|
void setUserdataRemapper(lua_State* L, void* context, UserdataRemapperCallback cb);
|
|
|
|
|
2024-04-12 18:18:49 +01:00
|
|
|
using ModuleId = std::array<uint8_t, 16>;
|
|
|
|
|
2022-10-14 20:48:41 +01:00
|
|
|
// Builds target function and all inner functions
|
2024-05-10 19:21:45 +01:00
|
|
|
CompilationResult compile(lua_State* L, int idx, unsigned int flags, CompilationStats* stats = nullptr);
|
|
|
|
CompilationResult compile(const ModuleId& moduleId, lua_State* L, int idx, unsigned int flags, CompilationStats* stats = nullptr);
|
|
|
|
|
|
|
|
CompilationResult compile(lua_State* L, int idx, const CompilationOptions& options, CompilationStats* stats = nullptr);
|
|
|
|
CompilationResult compile(const ModuleId& moduleId, lua_State* L, int idx, const CompilationOptions& options, CompilationStats* stats = nullptr);
|
2022-10-14 20:48:41 +01:00
|
|
|
|
2022-10-21 18:54:01 +01:00
|
|
|
// Generates assembly for target function and all inner functions
|
2023-09-15 18:26:59 +01:00
|
|
|
std::string getAssembly(lua_State* L, int idx, AssemblyOptions options = {}, LoweringStats* stats = nullptr);
|
2022-10-14 20:48:41 +01:00
|
|
|
|
2023-05-12 18:50:47 +01:00
|
|
|
using PerfLogFn = void (*)(void* context, uintptr_t addr, unsigned size, const char* symbol);
|
|
|
|
|
|
|
|
void setPerfLog(void* context, PerfLogFn logFn);
|
|
|
|
|
2022-10-14 20:48:41 +01:00
|
|
|
} // namespace CodeGen
|
|
|
|
} // namespace Luau
|