Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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;
        }