luau/CodeGen/include/Luau/CodeGen.h

148 lines
5.4 KiB
C
Raw Normal View History

2022-10-14 01:59:53 +03: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 11:33:11 -08:00
#include "Luau/CodeGenCommon.h"
#include "Luau/CodeGenOptions.h"
#include "Luau/LoweringStats.h"
2024-04-12 13:44:40 +03:00
#include <array>
#include <memory>
2022-10-14 01:59:53 +03:00
#include <string>
#include <vector>
2022-10-14 01:59:53 +03:00
2023-09-15 09:27:45 -07:00
#include <stddef.h>
2023-05-12 15:15:01 +03:00
#include <stdint.h>
2022-10-14 01:59:53 +03:00
struct lua_State;
namespace Luau
{
namespace CodeGen
{
2024-02-16 03:25:31 +02:00
// These enum values can be reported through telemetry.
// To ensure consistency, changes should be additive.
2023-08-25 18:25:09 +03:00
enum class CodeGenCompilationResult
{
2024-02-16 03:25:31 +02: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:25:09 +03:00
2024-02-16 03:25:31 +02: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 09:38:34 -07:00
Count = 10,
2023-08-25 18:25:09 +03:00
};
2024-05-03 09:38:34 -07:00
std::string toString(const CodeGenCompilationResult& result);
2024-03-30 15:49:03 -07: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:55:30 +03:00
struct CompilationStats
{
size_t bytecodeSizeBytes = 0;
size_t nativeCodeSizeBytes = 0;
size_t nativeDataSizeBytes = 0;
size_t nativeMetadataSizeBytes = 0;
2024-02-16 03:25:31 +02:00
uint32_t functionsTotal = 0;
2023-08-11 15:55:30 +03:00
uint32_t functionsCompiled = 0;
2024-04-12 13:44:40 +03:00
uint32_t functionsBound = 0;
2023-08-11 15:55:30 +03:00
};
2022-10-14 01:59:53 +03:00
bool isSupported();
2024-04-12 13:44:40 +03: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-01 16:25:12 -07:00
size_t blockSize,
size_t maxTotalSize,
AllocationCallback* allocationCallback,
void* allocationCallbackContext
);
2024-04-12 13:44:40 +03: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 08:33:40 -07: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 01:59:53 +03:00
void create(lua_State* L);
2024-05-26 08:33:40 -07: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 13:44:40 +03:00
void create(lua_State* L, SharedCodeGenContext* codeGenContext);
2022-10-14 01:59:53 +03:00
2024-03-15 14:01:00 -07: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 10:46:33 -07: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 13:44:40 +03:00
using ModuleId = std::array<uint8_t, 16>;
2022-10-14 01:59:53 +03:00
// Builds target function and all inner functions
2024-05-10 09:17:09 -07: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 01:59:53 +03:00
2022-10-21 10:33:43 -07:00
// Generates assembly for target function and all inner functions
2023-09-15 09:27:45 -07:00
std::string getAssembly(lua_State* L, int idx, AssemblyOptions options = {}, LoweringStats* stats = nullptr);
2022-10-14 01:59:53 +03:00
2023-05-12 15:15:01 +03:00
using PerfLogFn = void (*)(void* context, uintptr_t addr, unsigned size, const char* symbol);
void setPerfLog(void* context, PerfLogFn logFn);
2022-10-14 01:59:53 +03:00
} // namespace CodeGen
} // namespace Luau