为基本算术操作发出 IL

发布于 2024-12-20 01:33:57 字数 380 浏览 1 评论 0原文

我对 C# 相当陌生,最近了解到可以发出 IL 来实现一个简单的评估堆栈:

stack [ B C * A + ] ==> push B, push C, DO MULT, push A, DO ADD, POP return value

this example arises from the string "A + ( B  * C )"

有人可以提供一些建议和/或基本操作的示例 IL 片段 - 即推送 1,2 或 3 双精度数并对它们调用一元、二元或三元函数或算术运算符。

您的建议可能包括排放 IL 是否是一个好主意。这些表达式是输入到蒙特卡罗模拟的假设 - 因此有充分的理由考虑加快它们的速度,但如果它被认为是可行但不安全的,我不想这样做。

I'm fairly new to C#, and have recently learned that it's possible to emit IL to implement a simple evaluation stack:

stack [ B C * A + ] ==> push B, push C, DO MULT, push A, DO ADD, POP return value

this example arises from the string "A + ( B  * C )"

Can someone provide some advice and/or a sample IL snippet of the basic operation - i.e. push 1,2 or 3 doubles and invoke an unary, binary or ternary function or arithmetic operator on them.

Your advice may include whether or not emitting IL is a good idea. The expressions are assumptions fed to a monte-carlo simulation - so there is a good reason to think about speeding them up, but I don't want to do this if it's regarded as doable but unsafe.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞清仓 2024-12-27 01:33:57

请参阅http://msdn.microsoft.com/en-us/library/exczf7b9。 aspx 有关如何执行此操作的精彩描述。评论非常好&写得好。

See http://msdn.microsoft.com/en-us/library/exczf7b9.aspx for an excellent description of how to do this. Extremely well commented & well written.

二智少女猫性小仙女 2024-12-27 01:33:57

如果您无论如何都要进行解析以提取表达式中的操作数和运算符,那么为什么不直接调用特定函数来执行各种操作呢?它比发射和发射要简单得多。即时编译 IL。通过您的示例,您可以这样做:

MyStack<object> m_oStack = new MyStack<object> ();

double Add () // takes 2 params from stack
{
    return ( m_oStack.Pop () + m_oStack.Pop () );
}

double Multiply () // takes 2 params from stack
{
    return ( m_oStack.Pop () * m_oStack.Pop () );
}

// during evaluating the expression:
... // parse

m_oStack.Push ( fB );

... // parse

m_oStack.Push ( fC );

... // parse

m_oStack.Push ( Multiply () );

... // parse

m_oParamStack.Push ( fA );

... // parse

m_oParamStack.Push ( Add () );

// you're done with evaluating the expression, the result is
// on top of the stack

因此,当您评估表达式的各个部分时,您只需将值保留在堆栈中,并在解析各个子表达式时调用您自己的函数。

这一切都可以通过发出 IL 来实现,但它会变得更加复杂,因为您仍然需要执行所有解析、管理所有输入参数等,但最重要的是,您还必须发出 IL,然后即时执行它。

If you're doing the parsing anyway to extract the operands and operators in your expressions, why don't you just call specific functions to perform the various operations? It'd be significantly simpler than emitting & compiling IL on the fly. With your example, you could do this:

MyStack<object> m_oStack = new MyStack<object> ();

double Add () // takes 2 params from stack
{
    return ( m_oStack.Pop () + m_oStack.Pop () );
}

double Multiply () // takes 2 params from stack
{
    return ( m_oStack.Pop () * m_oStack.Pop () );
}

// during evaluating the expression:
... // parse

m_oStack.Push ( fB );

... // parse

m_oStack.Push ( fC );

... // parse

m_oStack.Push ( Multiply () );

... // parse

m_oParamStack.Push ( fA );

... // parse

m_oParamStack.Push ( Add () );

// you're done with evaluating the expression, the result is
// on top of the stack

So as you're evaluating the various parts of your expression, you just keep values in a stack and call your own functions as you parse the various sub-expressions.

This is all doable by emitting IL but it's going to be significantly more complicated, since you still need to do all the parsing, managing all the input parameters, etc., but on top of all that, you also have to emit IL and then execute it on the fly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文