Skip to content

Commit

Permalink
[CodeGen] WIP handle Gideros operators
Browse files Browse the repository at this point in the history
[VM] Fix color type API
  • Loading branch information
Nicolas BOUQUET committed Nov 7, 2024
1 parent e1f7d67 commit b985362
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 15 deletions.
9 changes: 9 additions & 0 deletions CodeGen/include/Luau/CodeGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
50 changes: 47 additions & 3 deletions CodeGen/src/BytecodeAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion VM/include/lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 11 additions & 9 deletions VM/src/lapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions VM/src/laux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit b985362

Please sign in to comment.