如何在代码生成期间简化包含变量的C风格算术表达式? [英] How to simplify a C-style arithmetical expression containing variables during code generation?

查看:102
本文介绍了如何在代码生成期间简化包含变量的C风格算术表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在编译器中优化表达式求值。

I am trying to optimize expression evaluation in a compiler.

算术表达式都是C风格的,它们可能包含变量。

The arithmetical expressions are all C-style, and they may contain variables. I hope to simplify the expressions as much as possible.

例如,(3 + 100 * A * B + 100)* 3 + 100 可以简化为 409 + 300 * A * B

这主要取决于分配法,关联法和交换律。

It mainly depends on the distributive law, the associative law and the commutative law.

遇到的是如何结合这些算术规律和传统的堆栈扫描评估算法。

The main difficulty I encounter is how to combine these arithmetical laws and traditional stack-scan evaluating algorithms.

任何人都可以在编译器构建的上下文中共享与这个或类似问题相关的经验? p>

Can anyone share experiences related to this or similar problems in the context of compiler building?

推荐答案

应用常数折叠,同时在编译的代码生成过程中结合强度降低。大多数编译器文本将提供一个算法来实现这一点。

Apply constant folding combined with strength reduction during the code generation pass of the compilation. Most compiler texts will provide an algorithm to implement this.

这篇关于如何在代码生成期间简化包含变量的C风格算术表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆