我是在3个地址代码中做对还是错?

发布于 2025-02-11 00:35:53 字数 314 浏览 0 评论 0原文

今天,我在大学的编译器建设决赛纸上完成了。 决赛论文包括一个问题,要求我将循环转换为3个地址代码。 它要求我转换的功能是:

for(i=1;i<=10;i++) x=y+z

所以,我确实循环展开并将给定语句转换为等效表达式:

x=(y+z)^10

然后,我制作了转换的代码的3个编码:

“

请告诉我是否正确。

Today, I'm done with my Compiler Construction finals paper at university.
The finals paper included a question that asked me to convert a for loop into 3-address code.
The function it asked me to convert was:

for(i=1;i<=10;i++) x=y+z

So, I did loop unrolling and converted the given statements to the equivalent expression:

x=(y+z)^10

Then, I made 3-address code of the converted code:

1

Please let me know if it is correct.

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

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

发布评论

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

评论(1

这个俗人 2025-02-18 00:35:53

您的转换代码是错误的。

在原始内容中,X并不取决于X的过去版本,使For Loop Dead Code和 @Peter Cordes所说的无用的循环展开。如果您寻找正确的答案,则正确的未优化答案将是:

(0) i=1
(1) if(i > 10) goto 5
(2) x=y+z
(3) i=i+1 
(4) goto 1
(5) end

而正确的优化答案将是:

(0) x=y+z
(1) end

Your converted code is wrong.

In the original, x is not dependent on past versions of x making the for loop dead code and loop unrolling useless as @Peter Cordes stated. If your looking for the correct answer, the correct non-optimized answer would be:

(0) i=1
(1) if(i > 10) goto 5
(2) x=y+z
(3) i=i+1 
(4) goto 1
(5) end

While, the correct optimized answer would be:

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