Add CodeGen C API (#931)

I'd like to experiment with the codegen feature (currently experimental)
and need a public C API for this.

This PR addresses this.
This commit is contained in:
Alex Orlenko 2023-05-18 12:03:29 +01:00 committed by GitHub
parent cf0ba32597
commit da0458bf6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 9 deletions

View File

@ -143,6 +143,9 @@ endif()
if(LUAU_NATIVE) if(LUAU_NATIVE)
target_compile_definitions(Luau.VM PUBLIC LUA_CUSTOM_EXECUTION=1) target_compile_definitions(Luau.VM PUBLIC LUA_CUSTOM_EXECUTION=1)
if(LUAU_EXTERN_C)
target_compile_definitions(Luau.CodeGen PUBLIC LUACODEGEN_API=extern\"C\")
endif()
endif() endif()
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND MSVC_VERSION GREATER_EQUAL 1924) if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND MSVC_VERSION GREATER_EQUAL 1924)

View File

@ -0,0 +1,18 @@
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#pragma once
// Can be used to reconfigure visibility/exports for public APIs
#ifndef LUACODEGEN_API
#define LUACODEGEN_API extern
#endif
struct lua_State;
// returns 1 if Luau code generator is supported, 0 otherwise
LUACODEGEN_API int luau_codegen_supported();
// create an instance of Luau code generator. you must check that this feature is supported using luau_codegen_supported().
LUACODEGEN_API void luau_codegen_create(lua_State* L);
// build target function and all inner functions
LUACODEGEN_API void luau_codegen_compile(lua_State* L, int idx);

21
CodeGen/src/lcodegen.cpp Normal file
View File

@ -0,0 +1,21 @@
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#include "luacodegen.h"
#include "Luau/CodeGen.h"
#include "lapi.h"
int luau_codegen_supported()
{
return Luau::CodeGen::isSupported();
}
void luau_codegen_create(lua_State* L)
{
Luau::CodeGen::create(L);
}
void luau_codegen_compile(lua_State* L, int idx)
{
Luau::CodeGen::compile(L, idx);
}

View File

@ -79,6 +79,7 @@ target_sources(Luau.CodeGen PRIVATE
CodeGen/include/Luau/UnwindBuilder.h CodeGen/include/Luau/UnwindBuilder.h
CodeGen/include/Luau/UnwindBuilderDwarf2.h CodeGen/include/Luau/UnwindBuilderDwarf2.h
CodeGen/include/Luau/UnwindBuilderWin.h CodeGen/include/Luau/UnwindBuilderWin.h
CodeGen/include/luacodegen.h
CodeGen/src/AssemblyBuilderA64.cpp CodeGen/src/AssemblyBuilderA64.cpp
CodeGen/src/AssemblyBuilderX64.cpp CodeGen/src/AssemblyBuilderX64.cpp
@ -104,6 +105,7 @@ target_sources(Luau.CodeGen PRIVATE
CodeGen/src/IrTranslation.cpp CodeGen/src/IrTranslation.cpp
CodeGen/src/IrUtils.cpp CodeGen/src/IrUtils.cpp
CodeGen/src/IrValueLocationTracking.cpp CodeGen/src/IrValueLocationTracking.cpp
CodeGen/src/lcodegen.cpp
CodeGen/src/NativeState.cpp CodeGen/src/NativeState.cpp
CodeGen/src/OptimizeConstProp.cpp CodeGen/src/OptimizeConstProp.cpp
CodeGen/src/OptimizeFinalX64.cpp CodeGen/src/OptimizeFinalX64.cpp

View File

@ -2,13 +2,13 @@
#include "lua.h" #include "lua.h"
#include "lualib.h" #include "lualib.h"
#include "luacode.h" #include "luacode.h"
#include "luacodegen.h"
#include "Luau/BuiltinDefinitions.h" #include "Luau/BuiltinDefinitions.h"
#include "Luau/ModuleResolver.h" #include "Luau/ModuleResolver.h"
#include "Luau/TypeInfer.h" #include "Luau/TypeInfer.h"
#include "Luau/StringUtils.h" #include "Luau/StringUtils.h"
#include "Luau/BytecodeBuilder.h" #include "Luau/BytecodeBuilder.h"
#include "Luau/CodeGen.h"
#include "Luau/Frontend.h" #include "Luau/Frontend.h"
#include "doctest.h" #include "doctest.h"
@ -159,8 +159,8 @@ static StateRef runConformance(const char* name, void (*setup)(lua_State* L) = n
StateRef globalState(initialLuaState, lua_close); StateRef globalState(initialLuaState, lua_close);
lua_State* L = globalState.get(); lua_State* L = globalState.get();
if (codegen && !skipCodegen && Luau::CodeGen::isSupported()) if (codegen && !skipCodegen && luau_codegen_supported())
Luau::CodeGen::create(L); luau_codegen_create(L);
luaL_openlibs(L); luaL_openlibs(L);
@ -213,8 +213,8 @@ static StateRef runConformance(const char* name, void (*setup)(lua_State* L) = n
int result = luau_load(L, chunkname.c_str(), bytecode, bytecodeSize, 0); int result = luau_load(L, chunkname.c_str(), bytecode, bytecodeSize, 0);
free(bytecode); free(bytecode);
if (result == 0 && codegen && !skipCodegen && Luau::CodeGen::isSupported()) if (result == 0 && codegen && !skipCodegen && luau_codegen_supported())
Luau::CodeGen::compile(L, -1); luau_codegen_compile(L, -1);
int status = (result == 0) ? lua_resume(L, nullptr, 0) : LUA_ERRSYNTAX; int status = (result == 0) ? lua_resume(L, nullptr, 0) : LUA_ERRSYNTAX;
@ -1679,8 +1679,8 @@ TEST_CASE("HugeFunction")
StateRef globalState(luaL_newstate(), lua_close); StateRef globalState(luaL_newstate(), lua_close);
lua_State* L = globalState.get(); lua_State* L = globalState.get();
if (codegen && Luau::CodeGen::isSupported()) if (codegen && luau_codegen_supported())
Luau::CodeGen::create(L); luau_codegen_create(L);
luaL_openlibs(L); luaL_openlibs(L);
luaL_sandbox(L); luaL_sandbox(L);
@ -1693,8 +1693,8 @@ TEST_CASE("HugeFunction")
REQUIRE(result == 0); REQUIRE(result == 0);
if (codegen && Luau::CodeGen::isSupported()) if (codegen && luau_codegen_supported())
Luau::CodeGen::compile(L, -1); luau_codegen_compile(L, -1);
int status = lua_resume(L, nullptr, 0); int status = lua_resume(L, nullptr, 0);
REQUIRE(status == 0); REQUIRE(status == 0);