“如果”的范围。规则 [英] Extent of the "as-if" rule

查看:45
本文介绍了“如果”的范围。规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

在与Tak-Shing Chan的讨论中,出现了一个问题:

as-if规则是否可以涵盖I / O功能。基本上,他认为它可以,并且

我认为它不是。


考虑两个程序:


/ *** ac *** /

#include< stdio.h>

int main(无效)

{

fopen(" somefile"," rb");

返回0;

}


/ *** bc *** /

in main(无效)

{

返回0;

}


编译器(通过优化)是否合法,为程序ac和bc发出

相同的代码?


我会欢迎参考标准。


祝你好运,


Sidney

Hi all,
In a discussion with Tak-Shing Chan the question came up whether the
as-if rule can cover I/O functions. Basically, he maintains it can, and
I think it doesn''t.

Consider two programs:

/*** a.c ***/
#include <stdio.h>
int main(void)
{
fopen("somefile","rb");
return 0;
}

/*** b.c ***/
in main(void)
{
return 0;
}

Would it be legal for a compiler (through optimization), to emit the
same code for program a.c and b.c ?

I''d welcome a reference from the standard.

Best regards,

Sidney

推荐答案

Sidney Cadot写道:
Sidney Cadot wrote:
大家好,

In与Tak-Shing Chan讨论了一个问题,即
as-if规则是否可以涵盖I / O功能。基本上,他坚持认为它可以,而且我认为它不会。

考虑两个程序:

/ *** ac *** /
#include< stdio.h>
int main(void)
{
fopen(" somefile"," rb");
返回0; <主要(无效)
{
返回0;
}
}

/ *** bc *** /
>
编译器(通过优化)是否合法,为程序ac和bc发出相同的代码?

我欢迎参考标准。

致以最诚挚的问候,

Sidney
Hi all,
In a discussion with Tak-Shing Chan the question came up whether the
as-if rule can cover I/O functions. Basically, he maintains it can, and
I think it doesn''t.

Consider two programs:

/*** a.c ***/
#include <stdio.h>
int main(void)
{
fopen("somefile","rb");
return 0;
}

/*** b.c ***/
in main(void)
{
return 0;
}

Would it be legal for a compiler (through optimization), to emit the
same code for program a.c and b.c ?

I''d welcome a reference from the standard.

Best regards,

Sidney




我会抓住这个...


由于fopen()是一个函数(15.2),因此它可能有副作用。

因此优化它将是一个错误。


也许正在运行的操作系统启动一壶咖啡,如果有人打开''somefile''文件进行阅读,那就是预期的效果。


/ J



I''ll take a stab at this...

Since fopen() is a function (15.2), it may therefore have side effects.
Optimizing it away would therefore be an error.

Perhaps the OS it''s running on starts up a pot of coffee if someone
opens the ''somefile'' file for reading, and that''s the intended effect.

/J


问候。


文章< bu ********* @ news.tudelft.nl> ;, Sidney Cadot写道:
Greetings.

In article <bu*********@news.tudelft.nl>, Sidney Cadot wrote:
考虑两个程序:

/ *** ac *** /
#include< stdio.h>
int main (无效)
{
fopen(&some; somefile"," rb");
返回0;
}

/ *** bc *** /
主要(无效)
{
返回0;
}
对于编译器(通过优化)是否合法,为程序ac和bc发出相同的代码?

我欢迎来自标准的参考。
Consider two programs:

/*** a.c ***/
#include <stdio.h>
int main(void)
{
fopen("somefile","rb");
return 0;
}

/*** b.c ***/
in main(void)
{
return 0;
}

Would it be legal for a compiler (through optimization), to emit the
same code for program a.c and b.c ?

I''d welcome a reference from the standard.




我不知道标准说的是什么,但是从

的实现点来看,不优化fopen()可能是有意义的。许多文件

系统维持最后访问系统。文件的时间戳;因此,

看似毫无用处的fopen()确实会改变环境。


问候,

Tristan
< br $> b $ b -

_

_V.-o Tristan Miller [en,(fr,de,ia)]><空间有限

/ |` - '' - = - = - = - = - = - = - = - = - = - = - = - = - = - = - =<> ;在ha句中,所以很难

(7_ \\ http://www.nothingisreal.com/ ><完成你的内容



I don''t know what the standard says, but from an implementation point of
view it might make sense not to optimize the fopen() away. Many file
systems maintain a "last accessed" timestamp for files; therefore, the
seemingly useless fopen() does indeed modify the environment.

Regards,
Tristan

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-'' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it''s hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you


In< 40 ******** @ news.wineasy.se> Johan Lindh< jo *** @ linkdata.getridofthis.se>写道:
In <40********@news.wineasy.se> Johan Lindh <jo***@linkdata.getridofthis.se> writes:
Sidney Cadot写道:
Sidney Cadot wrote:

在与Tak-Shing Chan的讨论中,问题出现了
as-if规则是否可以涵盖I / O功能。基本上,他坚持认为它可以,并且
我认为它没有。


as-if规则基本上表示任何优化/悲观化都是允许的b $ b,只要程序的*指定*输出不受影响。

考虑两个程序:

/ *** ac *** /
#include< stdio .h>
int main(无效)
{
fopen(" somefile"," rb");
返回0;
}
/ *** bc *** /
int main(无效)
{
返回0;
}

是否合法对于编译器(通过优化),为程序ac和bc发出相同的代码?


是的,因为两个程序根据抽象机器的

描述生成相同的输出。以读取模式打开文件和关闭它的
(如果开启成功)不会产生任何输出。

我欢迎来自标准的参考。
Hi all,
In a discussion with Tak-Shing Chan the question came up whether the
as-if rule can cover I/O functions. Basically, he maintains it can, and
I think it doesn''t.
The as-if rule basically says that any optimisation/pessimisation is
allowed, as long as the *specified* output of the program is not affected.
Consider two programs:

/*** a.c ***/
#include <stdio.h>
int main(void)
{
fopen("somefile","rb");
return 0;
}

/*** b.c ***/
int main(void)
{
return 0;
}

Would it be legal for a compiler (through optimization), to emit the
same code for program a.c and b.c ?
Yes, because both programs generate the same output, according to the
description of the abstract machine. Opening a file in read mode and
closing it (if the opening succeeded) does not generate any output.
I''d welcome a reference from the standard.



我会对此进行攻击......

由于fopen()是一个函数(15.2),因此可能会产生副作用。因此优化它将是一个错误。

如果某人打开''somefile''文件进行阅读,也许正在运行的操作系统会启动一壶咖啡,这就是预期的效果。



I''ll take a stab at this...

Since fopen() is a function (15.2), it may therefore have side effects.
Optimizing it away would therefore be an error.

Perhaps the OS it''s running on starts up a pot of coffee if someone
opens the ''somefile'' file for reading, and that''s the intended effect.




fopen唯一的*相关*副作用是C

标准中规定的那些。


这是标准参考:


5.1.2.3程序执行


1本国际标准中的语义描述描述了一个抽象机器的行为,其中优化问题

无关紧要。


2访问易变的对象t,修改一个对象,修改一个

文件,或调用一个执行任何这些操作的函数

都是副作用,11)这些都是状态的变化

执行环境。表达式的评估可能会产生

的副作用。在执行

序列中的某些指定点称为序列点,之前

评估的所有副作用都应完整,后续没有副作用

应进行评估。 (序列摘要

分在附录C中给出。)


3在抽象机器中,所有表达式都被评估为

由语义指定。实际实现不需要

如果它可以推断出它的值是没有使用的并且没有产生所需的副作用(包括
),则评估表达式的一部分
由调用函数或访问volatile对象引起的任何问题。


以读模式打开文件不会修改文件,因此它不会

算作副作用,在C标准的范围内。


Dan

-

Dan Pop

DESY Zeuthen,RZ集团

电子邮件: Da *** **@ifh.de


这篇关于“如果”的范围。规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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