Example #1
0
        private static void CgTableConstructorExp(FuncInfo fi, TableConstructorExp node, int a)
        {
            var nArr    = node.KeyExps?.Count(exp => exp is null) ?? 0;
            var nExps   = node.KeyExps?.Count ?? 0;
            var multRet = nExps > 0 && IsVarargOrFuncCall(node.ValExps[nExps - 1]);

            fi.EmitNewTable(a, nArr, nExps - nArr);
            var arrIdx = 0;

            for (var i = 0; i < nExps; i++)
            {
                var keyExp = node.KeyExps[i];
                var valExp = node.ValExps[i];

                if (keyExp == null)
                {
                    arrIdx++;
                    var tmp = fi.AllocReg();
                    if (i == nExps - 1 && multRet)
                    {
                        CgExp(fi, valExp, tmp, -1);
                    }
                    else
                    {
                        CgExp(fi, valExp, tmp, 1);
                    }

                    if (arrIdx % 50 == 0 || arrIdx == nArr)
                    {
                        var n = arrIdx % 50;
                        if (n == 0)
                        {
                            n = 50;
                        }

                        fi.FreeRegs(n);
                        var c = (arrIdx - 1) / 50 + 1;
                        if (i == nExps - 1 && multRet)
                        {
                            fi.EmitSetList(a, 0, c);
                        }
                        else
                        {
                            fi.EmitSetList(a, n, c);
                        }
                    }

                    continue;
                }

                var b = fi.AllocReg();
                CgExp(fi, keyExp, b, 1);
                var d = fi.AllocReg();
                CgExp(fi, valExp, d, 1);
                fi.FreeRegs(2);
                fi.EmitSetTable(a, b, d);
            }
        }
Example #2
0
        private static void CgTableAccessExp(FuncInfo fi, TableAccessExp node, int a)
        {
            var b = fi.AllocReg();

            CgExp(fi, node.PrefixExp, b, 1);
            var c = fi.AllocReg();

            CgExp(fi, node.KeyExp, c, 1);
            fi.EmitGetTable(a, b, c);
            fi.FreeRegs(2);
        }
Example #3
0
        private static void CgFuncCallStat(FuncInfo fi, FuncCallStat node)
        {
            var r = fi.AllocReg();

            CgFuncCallExp(fi, node, r, 0);
            fi.FreeReg();
        }
Example #4
0
        private static void CgUnopExp(FuncInfo fi, UnopExp node, int a)
        {
            var b = fi.AllocReg();

            CgExp(fi, node.Exp, b, 1);
            fi.EmitUnaryOp(node.GetOpCode(), a, b);
            fi.FreeReg();
        }
Example #5
0
        private static void CgBinopExp(FuncInfo fi, BinopExp node, int a)
        {
            switch (node.Op)
            {
            case ETokenType.OpAnd:
            case ETokenType.OpOr:
            {
                var b = fi.AllocReg();
                CgExp(fi, node.Exp1, b, 1);
                fi.FreeReg();

                if (node.Op == ETokenType.OpAnd)
                {
                    fi.EmitTestSet(a, b, 0);
                }
                else
                {
                    fi.EmitTestSet(a, b, 1);
                }

                var pcOfJmp = fi.EmitJmp(0, 0);
                b = fi.AllocReg();
                CgExp(fi, node.Exp2, b, 1);
                fi.FreeReg();
                fi.EmitMove(a, b);
                fi.FixsBx(pcOfJmp, fi.PC() - pcOfJmp);

                break;
            }

            default:
            {
                var b = fi.AllocReg();
                CgExp(fi, node.Exp1, b, 1);
                var c = fi.AllocReg();
                CgExp(fi, node.Exp2, c, 1);
                fi.EmitBinaryOp(node.Op, a, b, c);
                fi.FreeRegs(2);
                break;
            }
            }
        }
Example #6
0
        private static void CgConcatExp(FuncInfo fi, ConcatExp node, int a)
        {
            foreach (var subExp in node.Exps)
            {
                var tmp = fi.AllocReg();
                CgExp(fi, subExp, tmp, 1);
            }

            var c = fi.UsedRegs - 1;
            var b = c - node.Exps.Count + 1;

            fi.FreeRegs(c - b + 1);
            fi.EmitABC(EOpCode.OP_CONCAT, a, b, c);
        }
Example #7
0
        private static void CgRepeatStat(FuncInfo fi, RepeatStat node)
        {
            fi.EnterScope(true);

            var pcBeforeBlock = fi.PC();

            CgBlock(fi, node.Block);

            var r = fi.AllocReg();

            CgExp(fi, node.Exp, r, 1);
            fi.FreeReg();

            fi.EmitTest(r, 0);
            fi.EmitJmp(fi.GetJmpArgA(), pcBeforeBlock - fi.PC() - 1);
            fi.CloseOpenUpvals();

            fi.ExitScope();
        }
Example #8
0
        private static int PrepFuncCall(FuncInfo fi, FuncCallExp node, int a)
        {
            var nArgs = node.Args?.Count ?? 0;
            var lastArgIsVarargsOrFuncCall = false;

            CgExp(fi, node.PrefixExp, a, 1);
            if (node.NameExp != null)
            {
                fi.IndexOfConstant(node.NameExp.Str, out var idx);
                var c = 0x100 + idx;
                fi.EmitSelf(a, a, c);
            }

            for (var i = 0; i < nArgs; i++)
            {
                var arg = node.Args[i];
                var tmp = fi.AllocReg();
                if (i == nArgs - 1 && IsVarargOrFuncCall(arg))
                {
                    lastArgIsVarargsOrFuncCall = true;
                    CgExp(fi, arg, tmp, -1);
                }
                else
                {
                    CgExp(fi, arg, tmp, 1);
                }
            }

            fi.FreeRegs(nArgs);

            if (node.NameExp != null)
            {
                nArgs++;
            }

            if (lastArgIsVarargsOrFuncCall)
            {
                nArgs = -1;
            }

            return(nArgs);
        }
Example #9
0
        private static void CgWhileStat(FuncInfo fi, WhileStat node)
        {
            var pcBeforeExp = fi.PC();

            var r = fi.AllocReg();

            CgExp(fi, node.Exp, r, 1);
            fi.FreeReg();

            fi.EmitTest(r, 0);
            var pcJmpToEnd = fi.EmitJmp(0, 0);

            fi.EnterScope(true);
            CgBlock(fi, node.Block);
            fi.CloseOpenUpvals();
            fi.EmitJmp(0, pcBeforeExp - fi.PC() - 1);
            fi.ExitScope();

            fi.FixsBx(pcJmpToEnd, fi.PC() - pcJmpToEnd);
        }
Example #10
0
        private static void CgIfStat(FuncInfo fi, IfStat node)
        {
            var pcJmpToEnds    = new int[node.Exps.Count];
            var pcJmpToNextExp = -1;

            for (var i = 0; i < node.Exps.Count; i++)
            {
                var exp = node.Exps[i];

                if (pcJmpToNextExp >= 0)
                {
                    fi.FixsBx(pcJmpToNextExp, fi.PC() - pcJmpToNextExp);
                }

                var r = fi.AllocReg();
                CgExp(fi, exp, r, 1);
                fi.FreeReg();

                fi.EmitTest(r, 0);
                pcJmpToNextExp = fi.EmitJmp(0, 0);

                fi.EnterScope(false);
                CgBlock(fi, node.Blocks[i]);
                fi.CloseOpenUpvals();
                fi.ExitScope();

                if (i < node.Exps.Count - 1)
                {
                    pcJmpToEnds[i] = fi.EmitJmp(0, 0);
                }
                else
                {
                    pcJmpToEnds[i] = pcJmpToNextExp;
                }
            }

            foreach (var pc in pcJmpToEnds)
            {
                fi.FixsBx(pc, fi.PC() - pc);
            }
        }
Example #11
0
        private static void CgAssignStat(FuncInfo fi, AssignStat node)
        {
            var exps  = RemoveTailNil(node.ExpList);
            var nExps = exps.Count;
            var nVars = node.VarList.Count;

            var tRegs   = new int[nVars];
            var kRegs   = new int[nVars];
            var vRegs   = new int[nVars];
            var oldRegs = fi.UsedRegs;

            for (var i = 0; i < node.VarList.Count; i++)
            {
                var exp = node.VarList[i];

                if (exp is TableAccessExp tableAccessExp)
                {
                    tRegs[i] = fi.AllocReg();
                    CgExp(fi, tableAccessExp.PrefixExp, tRegs[i], 1);
                    kRegs[i] = fi.AllocReg();
                    CgExp(fi, tableAccessExp.KeyExp, kRegs[i], 1);
                }
                else if (exp is NameExp nameExp)
                {
                    var name = nameExp.Name;

                    if (fi.SlotOfLocVar(name) < 0 && fi.IndexOfUpval(name) < 0)
                    {
                        kRegs[i] = -1;
                        fi.IndexOfConstant(name, out var constIdx);
                        if (constIdx > 0xff)
                        {
                            kRegs[i] = fi.AllocReg();
                        }
                    }
                }
            }

            for (var i = 0; i < nVars; i++)
            {
                vRegs[i] = fi.UsedRegs + i;
            }

            if (nExps >= nVars)
            {
                for (var i = 0; i < exps.Count; i++)
                {
                    var exp = exps[i];
                    var a   = fi.AllocReg();
                    if (i >= nVars && i == nExps - 1 && IsVarargOrFuncCall(exp))
                    {
                        CgExp(fi, exp, a, 0);
                    }
                    else
                    {
                        CgExp(fi, exp, a, 1);
                    }
                }
            }
            else
            {
                var multRet = false;

                for (var i = 0; i < exps.Count; i++)
                {
                    var exp = exps[i];
                    var a   = fi.AllocReg();
                    if (i == nExps - 1 && IsVarargOrFuncCall(exp))
                    {
                        multRet = true;
                        var n = nVars - nExps + 1;
                        CgExp(fi, exp, a, n);
                        fi.AllocRegs(n - 1);
                    }
                    else
                    {
                        CgExp(fi, exp, a, 1);
                    }
                }

                if (!multRet)
                {
                    var n = nVars - nExps;
                    var a = fi.AllocRegs(n);
                    fi.EmitLoadNil(a, n);
                }
            }

            for (var i = 0; i < node.VarList.Count; i++)
            {
                var exp = node.VarList[i];

                if (exp is NameExp nameExp)
                {
                    var varName = nameExp.Name;
                    var a       = fi.SlotOfLocVar(varName);

                    if (a >= 0)
                    {
                        fi.EmitMove(a, vRegs[i]);
                    }
                    else
                    {
                        var b = fi.IndexOfUpval(varName);
                        if (b >= 0)
                        {
                            fi.EmitSetUpval(vRegs[i], b);
                        }
                        else
                        {
                            a = fi.SlotOfLocVar("_ENV");
                            if (a >= 0)
                            {
                                if (kRegs[i] < 0)
                                {
                                    fi.IndexOfConstant(varName, out var constIdx);
                                    b = 0x100 + constIdx;
                                    fi.EmitSetTable(a, b, vRegs[i]);
                                }
                                else
                                {
                                    fi.EmitSetTable(a, kRegs[i], vRegs[i]);
                                }
                            }
                            else
                            {
                                a = fi.IndexOfUpval("_ENV");
                                if (kRegs[i] < 0)
                                {
                                    fi.IndexOfConstant(varName, out var idx);
                                    b = 0x100 + idx;
                                    fi.EmitSetTabUp(a, b, vRegs[i]);
                                }
                                else
                                {
                                    fi.EmitSetTable(a, kRegs[i], vRegs[i]);
                                }
                            }
                        }
                    }
                }
                else
                {
                    fi.EmitSetTable(tRegs[i], kRegs[i], vRegs[i]);
                }
            }

            fi.UsedRegs = oldRegs;
        }
Example #12
0
        private static void CgLocalVarDeclStat(FuncInfo fi, LocalVarDeclStat node)
        {
            var exps    = RemoveTailNil(node.ExpList);
            var nExps   = exps.Count;
            var nNames  = node.NameList.Count;
            var oldRegs = fi.UsedRegs;

            if (nExps == nNames)
            {
                foreach (var exp in exps)
                {
                    var a = fi.AllocReg();
                    CgExp(fi, exp, a, 1);
                }
            }
            else if (nExps > nNames)
            {
                for (var i = 0; i < exps.Count; i++)
                {
                    var exp = exps[i];
                    var a   = fi.AllocReg();
                    if (i == nExps - 1 && IsVarargOrFuncCall(exp))
                    {
                        CgExp(fi, exp, a, 0);
                    }
                    else
                    {
                        CgExp(fi, exp, a, 1);
                    }
                }
            }
            else
            {
                var multRet = false;
                for (var i = 0; i < exps.Count; i++)
                {
                    var exp = exps[i];
                    var a   = fi.AllocReg();
                    if (i == nExps - 1 && IsVarargOrFuncCall(exp))
                    {
                        multRet = true;
                        var n = nNames - nExps + 1;
                        CgExp(fi, exp, a, n);
                        fi.AllocRegs(n - 1);
                    }
                    else
                    {
                        CgExp(fi, exp, a, 1);
                    }
                }

                if (!multRet)
                {
                    var n = nNames - nExps;
                    var a = fi.AllocRegs(n);
                    fi.EmitLoadNil(a, n);
                }
            }

            fi.UsedRegs = oldRegs;
            foreach (var name in node.NameList)
            {
                fi.AddLocVar(name);
            }
        }
Example #13
0
        private static void CgRetStat(FuncInfo fi, List <Exp> exps)
        {
            var nExps = exps.Count;

            if (nExps == 0)
            {
                fi.EmitReturn(0, 0);
                return;
            }

            if (nExps == 1)
            {
                if (exps[0] is NameExp nameExp)
                {
                    var r = fi.SlotOfLocVar(nameExp.Name);
                    if (r >= 0)
                    {
                        fi.EmitReturn(r, 1);
                        return;
                    }
                }

                if (exps[0] is FuncCallExp fcExp)
                {
                    var r = fi.AllocReg();
                    CgTailCallExp(fi, fcExp, r);
                    fi.FreeReg();
                    fi.EmitReturn(r, -1);
                    return;
                }
            }

            var multRet = IsVarargOrFuncCall(exps[nExps - 1]);

            for (var i = 0; i < nExps; i++)
            {
                var exp = exps[i];
                var r   = fi.AllocReg();

                if (i == nExps - 1 && multRet)
                {
                    CgExp(fi, exp, r, -1);
                }
                else
                {
                    CgExp(fi, exp, r, 1);
                }
            }

            fi.FreeRegs(nExps);

            var a = fi.UsedRegs;

            if (multRet)
            {
                fi.EmitReturn(a, -1);
            }
            else
            {
                fi.EmitReturn(a, nExps);
            }
        }