From b9853627fe92775b1a662128058687f415600dab Mon Sep 17 00:00:00 2001 From: Nicolas BOUQUET Date: Thu, 7 Nov 2024 08:14:00 +0100 Subject: [PATCH] [CodeGen] WIP handle Gideros operators [VM] Fix color type API --- CodeGen/include/Luau/CodeGen.h | 9 ++++++ CodeGen/src/BytecodeAnalysis.cpp | 50 ++++++++++++++++++++++++++++++-- VM/include/lua.h | 2 +- VM/src/lapi.cpp | 20 +++++++------ VM/src/laux.cpp | 4 +-- 5 files changed, 70 insertions(+), 15 deletions(-) diff --git a/CodeGen/include/Luau/CodeGen.h b/CodeGen/include/Luau/CodeGen.h index 0cf9d9a55..a2cf9779f 100644 --- a/CodeGen/include/Luau/CodeGen.h +++ b/CodeGen/include/Luau/CodeGen.h @@ -95,6 +95,15 @@ enum class HostMetamethod LessEqual, Length, Concat, + //GIDEROS + MinOf, + MaxOf, + BinAnd, + BinOr, + BinXor, + ShiftR, + ShiftL, + BinNot, }; using HostUserdataOperationBytecodeType = uint8_t (*)(uint8_t type, const char* member, size_t memberLength); diff --git a/CodeGen/src/BytecodeAnalysis.cpp b/CodeGen/src/BytecodeAnalysis.cpp index 8d2efebe0..2b9d32944 100644 --- a/CodeGen/src/BytecodeAnalysis.cpp +++ b/CodeGen/src/BytecodeAnalysis.cpp @@ -554,7 +554,36 @@ static HostMetamethod opcodeToHostMetamethod(LuauOpcode op) return HostMetamethod::Sub; case LOP_DIVRK: return HostMetamethod::Div; - default: + //GIDEROS OPCODES + case LOP_MINOF: + return HostMetamethod::MinOf; + case LOP_MAXOF: + return HostMetamethod::MaxOf; + case LOP_BINAND: + return HostMetamethod::BinAnd; + case LOP_BINOR: + return HostMetamethod::BinOr; + case LOP_BINXOR: + return HostMetamethod::BinXor; + case LOP_SHIFTR: + return HostMetamethod::ShiftR; + case LOP_SHIFTL: + return HostMetamethod::ShiftL; + case LOP_MINOFK: + return HostMetamethod::MinOf; + case LOP_MAXOFK: + return HostMetamethod::MaxOf; + case LOP_BINANDK: + return HostMetamethod::BinAnd; + case LOP_BINORK: + return HostMetamethod::BinOr; + case LOP_BINXORK: + return HostMetamethod::BinXor; + case LOP_SHIFTRK: + return HostMetamethod::ShiftR; + case LOP_SHIFTLK: + return HostMetamethod::ShiftL; + default: CODEGEN_ASSERT(!"opcode is not assigned to a host metamethod"); } @@ -794,6 +823,9 @@ void analyzeBytecodeTypes(IrFunction& function, const HostIrHooks& hostHooks) } case LOP_ADD: case LOP_SUB: + case LOP_BINAND: + case LOP_BINOR: + case LOP_BINXOR: { int ra = LUAU_INSN_A(*pc); int rb = LUAU_INSN_B(*pc); @@ -817,6 +849,8 @@ void analyzeBytecodeTypes(IrFunction& function, const HostIrHooks& hostHooks) case LOP_MUL: case LOP_DIV: case LOP_IDIV: + case LOP_MINOF: + case LOP_MAXOF: { int ra = LUAU_INSN_A(*pc); int rb = LUAU_INSN_B(*pc); @@ -849,6 +883,8 @@ void analyzeBytecodeTypes(IrFunction& function, const HostIrHooks& hostHooks) } case LOP_MOD: case LOP_POW: + case LOP_SHIFTR: + case LOP_SHIFTL: { int ra = LUAU_INSN_A(*pc); int rb = LUAU_INSN_B(*pc); @@ -869,6 +905,9 @@ void analyzeBytecodeTypes(IrFunction& function, const HostIrHooks& hostHooks) } case LOP_ADDK: case LOP_SUBK: + case LOP_BINANDK: + case LOP_BINORK: + case LOP_BINXORK: { int ra = LUAU_INSN_A(*pc); int rb = LUAU_INSN_B(*pc); @@ -892,7 +931,9 @@ void analyzeBytecodeTypes(IrFunction& function, const HostIrHooks& hostHooks) case LOP_MULK: case LOP_DIVK: case LOP_IDIVK: - { + case LOP_MINOFK: + case LOP_MAXOFK: + { int ra = LUAU_INSN_A(*pc); int rb = LUAU_INSN_B(*pc); int kc = LUAU_INSN_C(*pc); @@ -924,6 +965,8 @@ void analyzeBytecodeTypes(IrFunction& function, const HostIrHooks& hostHooks) } case LOP_MODK: case LOP_POWK: + case LOP_SHIFTRK: + case LOP_SHIFTLK: { int ra = LUAU_INSN_A(*pc); int rb = LUAU_INSN_B(*pc); @@ -1006,6 +1049,7 @@ void analyzeBytecodeTypes(IrFunction& function, const HostIrHooks& hostHooks) break; } case LOP_MINUS: + case LOP_BINNOT: { int ra = LUAU_INSN_A(*pc); int rb = LUAU_INSN_B(*pc); @@ -1019,7 +1063,7 @@ void analyzeBytecodeTypes(IrFunction& function, const HostIrHooks& hostHooks) else if (bcType.a == LBC_TYPE_VECTOR) regTags[ra] = LBC_TYPE_VECTOR; else if (hostHooks.userdataMetamethodBytecodeType && isCustomUserdataBytecodeType(bcType.a)) - regTags[ra] = hostHooks.userdataMetamethodBytecodeType(bcType.a, LBC_TYPE_ANY, HostMetamethod::Minus); + regTags[ra] = hostHooks.userdataMetamethodBytecodeType(bcType.a, LBC_TYPE_ANY, (op==LOP_BINNOT)?HostMetamethod::BinNot:HostMetamethod::Minus); bcType.result = regTags[ra]; break; diff --git a/VM/include/lua.h b/VM/include/lua.h index 3ba6e67e5..f44166a4d 100644 --- a/VM/include/lua.h +++ b/VM/include/lua.h @@ -154,7 +154,7 @@ LUA_API double lua_tonumberx(lua_State* L, int idx, int* isnum); LUA_API int lua_tointegerx(lua_State* L, int idx, int* isnum); LUA_API unsigned lua_tounsignedx(lua_State* L, int idx, int* isnum); LUA_API const float* lua_tovector(lua_State* L, int idx); -LUA_API int lua_tocolorf(lua_State* L, int idx, float *color); +LUA_API int lua_tocolorf(lua_State* L, int idx, float *color, int acceptNumber); LUA_API int lua_toboolean(lua_State* L, int idx); LUA_API const char* lua_tolstring(lua_State* L, int idx, size_t* len); LUA_API const char* lua_tostringatom(lua_State* L, int idx, int* atom); diff --git a/VM/src/lapi.cpp b/VM/src/lapi.cpp index 857fc6af7..8ebf18d7e 100644 --- a/VM/src/lapi.cpp +++ b/VM/src/lapi.cpp @@ -488,7 +488,7 @@ const float* lua_tovector(lua_State* L, int idx) return vvalue(o); } -int lua_tocolorf(lua_State* L, int idx, float* color) { +int lua_tocolorf(lua_State* L, int idx, float* color, int acceptNumber) { StkId o = index2addr(L, idx); switch (ttype(o)) { case LUA_TVECTOR: @@ -515,14 +515,16 @@ int lua_tocolorf(lua_State* L, int idx, float* color) { } case LUA_TNUMBER: { - unsigned int c; - luai_num2unsigned(c, nvalue(o)); - - color[0]= (1.0/255)*((c >> 16) & 0xff); - color[1]= (1.0/255)*((c >> 8) & 0xff); - color[2]= (1.0/255)*((c >> 0) & 0xff); - color[3]=1; - return 1; + if (acceptNumber) { + unsigned int c; + luai_num2unsigned(c, nvalue(o)); + + color[0]= (1.0/255)*((c >> 16) & 0xff); + color[1]= (1.0/255)*((c >> 8) & 0xff); + color[2]= (1.0/255)*((c >> 0) & 0xff); + color[3]= 1; + return 1; + } } } return 0; diff --git a/VM/src/laux.cpp b/VM/src/laux.cpp index 9843e14e4..b7cd1356b 100644 --- a/VM/src/laux.cpp +++ b/VM/src/laux.cpp @@ -251,7 +251,7 @@ const float* luaL_optvector(lua_State* L, int narg, const float* def) void luaL_checkcolorf(lua_State* L, int narg, float *color) { - if (!lua_tocolorf(L, narg, color)) + if (!lua_tocolorf(L, narg, color,0)) tag_error(L, narg, LUA_TCOLOR); } @@ -580,7 +580,7 @@ const char* luaL_tolstring(lua_State* L, int idx, size_t* len) case LUA_TCOLOR: { float col[4]; - lua_tocolorf(L, idx,col); + lua_tocolorf(L, idx,col,0); char s[LUAI_MAXNUM2STR * 4]; char* e = s;