c# - 生成 IL 以减少 for 循环中的计数器

标签 c# compiler-construction code-generation cil reflection.emit

我正在使用 Good For Nothing (GFN) 编译器,试图让它做一些不同的事情。我正在使用这里的代码:https://github.com/johandanforth/good-for-nothing-compiler

常规 GFN for 循环:

var x = 0;
for x = 0 to 3 do
    print x;
end;

这个 for 循环总是递增。我想添加递减功能:

var x = 0;
for x = 3 to 0 down //up for increment (works same as do)
    print x;
end;

我遇到的主要问题是 CodeGen。

ForLoop 类:

public class ForLoop : Stmt
{
    public Stmt Body { get; set; }
    public Expr From { get; set; }
    public string Ident { get; set; }
    public Expr To { get; set; }
    public ArithOp Type { get; set; }
}

ArithOp 枚举:

public enum ArithOp
{
    Add,
    Sub,
    Mul,
    Div,
    Up,
    Down
}

CodeGen.cs 内部:

private void GenStmt(Stmt stmt)
{
   //code omitted for brevity 

    else if (stmt is ForLoop)
            {
                // example: 
                // for x = 0 to 100 up
                //    "hello";
                // end;

                // x = 0
                var forLoop = (ForLoop)stmt;
                var assign = new Assign { Ident = forLoop.Ident, Expr = forLoop.From };
                GenStmt(assign);
                // jump to the test
                var test = _il.DefineLabel();
                _il.Emit(OpCodes.Br, test);

                // statements in the body of the for loop
                var body = _il.DefineLabel();
                _il.MarkLabel(body);
                GenStmt(forLoop.Body);

                // to (increment/decrement the value of x)
                _il.Emit(OpCodes.Ldloc, SymbolTable[forLoop.Ident]);
                _il.Emit(OpCodes.Ldc_I4, 1);
                _il.Emit(forLoop.Type == ArithOp.Up ? OpCodes.Add : OpCodes.Sub);
                GenerateStoreFromStack(forLoop.Ident, typeof(int));

                // **test** does x equal 100? (do the test)
                _il.MarkLabel(test);
                _il.Emit(OpCodes.Ldloc, SymbolTable[forLoop.Ident]);
                GenerateLoadToStackForExpr(forLoop.To, typeof(int));
                _il.Emit(OpCodes.Blt, body);
            }
}

private void GenerateStoreFromStack(string name, Type type)
    {
        if (!SymbolTable.ContainsKey(name))
            throw new Exception("undeclared variable '" + name + "'");

        var locb = SymbolTable[name];
        var localType = locb.LocalType;

        if (localType != type)
            throw new Exception(string.Format("'{0}' is of type {1} but attempted to store value of type {2}", name,
                localType == null ? "<unknown>" : localType.Name, type.Name));

        _il.Emit(OpCodes.Stloc, SymbolTable[name]);
    }

    private void GenerateLoadToStackForExpr(Expr expr, Type expectedType)
    {
        Type deliveredType;

        if (expr is StringLiteral)
        {
            deliveredType = typeof(string);
            _il.Emit(OpCodes.Ldstr, ((StringLiteral)expr).Value);
        }
        else if (expr is IntLiteral)
        {
            deliveredType = typeof(int);
            _il.Emit(OpCodes.Ldc_I4, ((IntLiteral)expr).Value);
        }
        else if (expr is Variable)
        {
            var ident = ((Variable)expr).Ident;
            deliveredType = expr.GetType();

            if (!SymbolTable.ContainsKey(ident))
            {
                throw new Exception("undeclared variable '" + ident + "'");
            }

            _il.Emit(OpCodes.Ldloc, SymbolTable[ident]);
        }
        else if (expr is ArithExpr)
        {
            var arithExpr = (ArithExpr)expr;
            var left = arithExpr.Left;
            var right = arithExpr.Right;
            deliveredType = expr.GetType();

            GenerateLoadToStackForExpr(left, expectedType);
            GenerateLoadToStackForExpr(right, expectedType);
            switch (arithExpr.Op)
            {
                case ArithOp.Add:
                    _il.Emit(OpCodes.Add);
                    break;
                case ArithOp.Sub:
                    _il.Emit(OpCodes.Sub);
                    break;
                case ArithOp.Mul:
                    _il.Emit(OpCodes.Mul);
                    break;
                case ArithOp.Div:
                    _il.Emit(OpCodes.Div);
                    break;
                default:
                    throw new NotImplementedException("Don't know how to generate il load code for " + arithExpr.Op +
                                                      " yet!");
            }
        }
        else
        {
            throw new Exception("don't know how to generate " + expr.GetType().Name);
        }

        if (deliveredType == expectedType) return;

        if (deliveredType != typeof (int) || expectedType != typeof (string))
            throw new Exception("can't coerce a " + deliveredType.Name + " to a " + expectedType.Name);

        _il.Emit(OpCodes.Box, typeof (int));
        _il.Emit(OpCodes.Callvirt, typeof (object).GetMethod("ToString"));
    }

这当前生成一个不执行任何操作的 .exe。我查看过帮助解决此问题的来源:http://www.codeproject.com/Articles/3778/Introduction-to-IL-Assembly-Language#Loophttps://ninjaferret.wordpress.com/2009/12/23/msil-4-for-loops/ 。我只是不知道足够的 IL

最佳答案

在 C# 代码中执行此操作以获得洞察力:

   for (int ix = 0; ix < 3; ++ix)     // up
   for (int ix = 3; ix > 0; --ix)     // down

两个变化,你在inc/dec运算符中得到了差异。您没有得到循环终止条件的更改。这使得这个错误:

   _il.Emit(OpCodes.Blt, body);

您必须将其反转为 Opcodes.Bgt

关于c# - 生成 IL 以减少 for 循环中的计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31907209/

相关文章:

c# - 在 C# 中单击按钮停止线程

c# - 在 ReadToEnd 之后关闭 StreamReader

c++ - C++ 编译器中的汇编语言输出

java - 在编译类中不进行反射的序列化

Python:打印基类变量

c# - 在 C# 中创建比表单大的 map

c# - C# 中的嵌套泛型是什么意思?

c - 修复给定代码中的冲突? "25 shift/reduce conflicts [-Wconflicts-sr] "

ide - 开发完整的编程语言、编译器和 IDE 需要多少时间和精力?

java - 在 IntelliJ 中自定义 `Code` > `Generate` > `Constructor`