Пример #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);
            }
        }
Пример #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;
        }