luau/CodeGen/src/CodeGenContext.h
Junseo Yoo ce8495a69e
Sync to upstream/release/637 (#1354)
# What's Changed?

- Code refactoring with a new clang-format
- More bug fixes / test case fixes in the new solver

## New Solver

- More precise telemetry collection of `any` types
- Simplification of two completely disjoint tables combines them into a
single table that inherits all properties / indexers
- Refining a `never & <anything>` does not produce type family types nor
constraints
- Silence "inference failed to complete" error when it is the only error
reported

---
### Internal Contributors

Co-authored-by: Aaron Weiss <aaronweiss@roblox.com>
Co-authored-by: Andy Friesen <afriesen@roblox.com>
Co-authored-by: Dibri Nsofor <dnsofor@roblox.com>
Co-authored-by: Jeremy Yoo <jyoo@roblox.com>
Co-authored-by: Vighnesh Vijay <vvijay@roblox.com>
Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com>

---------

Co-authored-by: Aaron Weiss <aaronweiss@roblox.com>
Co-authored-by: Alexander McCord <amccord@roblox.com>
Co-authored-by: Andy Friesen <afriesen@roblox.com>
Co-authored-by: Vighnesh <vvijay@roblox.com>
Co-authored-by: Aviral Goel <agoel@roblox.com>
Co-authored-by: David Cope <dcope@roblox.com>
Co-authored-by: Lily Brown <lbrown@roblox.com>
Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com>
2024-08-02 07:30:04 -07:00

118 lines
3.7 KiB
C++

// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#pragma once
#include "Luau/SharedCodeAllocator.h"
#include "NativeState.h"
#include <memory>
#include <optional>
#include <stdint.h>
namespace Luau
{
namespace CodeGen
{
// The "code-gen context" maintains the native code-gen state. There are two
// implementations. The StandaloneCodeGenContext is a VM-specific context type.
// It is the "simple" implementation that can be used when native code-gen is
// used with a single Luau VM. The SharedCodeGenContext supports use from
// multiple Luau VMs concurrently, and allows for sharing of executable native
// code and related metadata.
struct ModuleBindResult
{
CodeGenCompilationResult compilationResult = {};
uint32_t functionsBound = 0;
};
class BaseCodeGenContext
{
public:
BaseCodeGenContext(size_t blockSize, size_t maxTotalSize, AllocationCallback* allocationCallback, void* allocationCallbackContext);
[[nodiscard]] bool initHeaderFunctions();
[[nodiscard]] virtual std::optional<ModuleBindResult> tryBindExistingModule(
const ModuleId& moduleId,
const std::vector<Proto*>& moduleProtos
) = 0;
[[nodiscard]] virtual ModuleBindResult bindModule(
const std::optional<ModuleId>& moduleId,
const std::vector<Proto*>& moduleProtos,
std::vector<NativeProtoExecDataPtr> nativeExecDatas,
const uint8_t* data,
size_t dataSize,
const uint8_t* code,
size_t codeSize
) = 0;
virtual void onCloseState() noexcept = 0;
virtual void onDestroyFunction(void* execdata) noexcept = 0;
CodeAllocator codeAllocator;
std::unique_ptr<UnwindBuilder> unwindBuilder;
uint8_t* gateData = nullptr;
size_t gateDataSize = 0;
void* userdataRemappingContext = nullptr;
UserdataRemapperCallback* userdataRemapper = nullptr;
NativeContext context;
};
class StandaloneCodeGenContext final : public BaseCodeGenContext
{
public:
StandaloneCodeGenContext(size_t blockSize, size_t maxTotalSize, AllocationCallback* allocationCallback, void* allocationCallbackContext);
[[nodiscard]] virtual std::optional<ModuleBindResult> tryBindExistingModule(const ModuleId& moduleId, const std::vector<Proto*>& moduleProtos)
override;
[[nodiscard]] virtual ModuleBindResult bindModule(
const std::optional<ModuleId>& moduleId,
const std::vector<Proto*>& moduleProtos,
std::vector<NativeProtoExecDataPtr> nativeExecDatas,
const uint8_t* data,
size_t dataSize,
const uint8_t* code,
size_t codeSize
) override;
virtual void onCloseState() noexcept override;
virtual void onDestroyFunction(void* execdata) noexcept override;
private:
};
class SharedCodeGenContext final : public BaseCodeGenContext
{
public:
SharedCodeGenContext(size_t blockSize, size_t maxTotalSize, AllocationCallback* allocationCallback, void* allocationCallbackContext);
[[nodiscard]] virtual std::optional<ModuleBindResult> tryBindExistingModule(const ModuleId& moduleId, const std::vector<Proto*>& moduleProtos)
override;
[[nodiscard]] virtual ModuleBindResult bindModule(
const std::optional<ModuleId>& moduleId,
const std::vector<Proto*>& moduleProtos,
std::vector<NativeProtoExecDataPtr> nativeExecDatas,
const uint8_t* data,
size_t dataSize,
const uint8_t* code,
size_t codeSize
) override;
virtual void onCloseState() noexcept override;
virtual void onDestroyFunction(void* execdata) noexcept override;
private:
SharedCodeAllocator sharedAllocator;
};
} // namespace CodeGen
} // namespace Luau