473,772 Members | 2,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extent of the "as-if" rule

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

Nov 14 '05
145 6346
Sidney Cadot wrote:
Wojtek Lerch wrote:
Sidney Cadot <si****@jigsaw. nl> wrote in message
news:<bv******* ***@news.tudelf t.nl>... ....
>>On an implementation that does have file attributes, it's
>>up to the implementation to define how fopen() affects them, and
to make sure that it indeed affects them that way.

After the "and": I'd like to see a standard quote supporting that.
There is none. The C standard doesn't care. That's the point. Is it
really that difficult?
Not at all. It's just that I would have appreciated a disclaimer stating
something like this:

"The execution environment may impose semantics / side effects in
addition to (but not contradicting) the semantics and side effects as
defined in this standard, for the following I/O related functions: ..."

Is that so much to ask?


The standard has a tendency to avoid saying things that are obvious.
Implementations may do whatever they want, as long as they meet all the
requirements defined in the standard. In particular, implementations
are free to give promises to their users that go beyond what the C
standard requires. If a compiler is sold in a box that claims to
contain 97% recycled paper but in reality only contains 95%, isn't it
quite obvious that that little lie doesn't affect the compiler's
conformance to the C standard?

....
Yes, but it *may*. My point is that the standard should explicitly
forbid this for calls that have side effects (in the normal, not
c-standard sense).


Sorry, but what exactly do you mean by "calls that have side effects"?


That is answerable. A call has a side effect if there is a detectable
effect in the outside world of the call having been made, i.e., there is
a detectable difference between the effects on the execution environment
of a run of the program and a run of a derived program that is equal to
the original, except that this one call did not take place.


That's a circular definition. If an implementation optimizes the call
away, there's no detectable difference, and therefore it was OK to
optimize it away.

On the other hand, if another implementation doesn't optimize it away,
the derived program takes up less space in the execution environment --
isn't that a detectable difference?
If the implementation optimizes a call away, then it doesn't have
side effects.


Which definition of "side effect" are you using here?


Any definition you can think of. Since the call has been optimized
away, it can't have any effects whatsoever.
Unless the standard requires that call to have some
specific side effects, how exactly is the implementor supposed to know
that the call *should have* some side effects, according to you?


He couldn't, and doesn't have to. What is important is that he knows
from the standard that the call *may* have a side effect.


You seem to have it backwards. The implementor *implements* the call.
He writes the code. The standard tells him what the call must do and
what it mustn't do, and then he decides how to write code that does all
the required things but doesn't do any of the forbidden things. If the
standard says that the call may but may not have side effects (by
whatever definition of "side effects"), the implementor is free to
choose whether to write code that produces such side effects or code
that can be optimized away.

.... The standard tries quite hard to provide a situation where compliancy of
an implementation can be judged based on the text of the standard alone,
i.e., it is (or attempts to be) self-contained.
Yes. That's the purpose of having a standard.
I would argue that this fails (where else) with its handling of the
concept of side effects. These cannot be properly defined without
references to "outside the C box", I think; so then you're left with a
number of options. My preference would be to be "intentiona lly vague"
rather than an (IMHO) misguided attempt to provide a definition.
The authors of the standard described a concept they wanted to use in
the text, and decided to name it "side effects". They excluded things
like file attributes from their definition because they didn't want
implementation conformance to the C standard to depend on how
implementations define and handle file attributes.

If you ask whether the C standard requires fopen() to change file
attributes, the answer is a clear no. You might not like that answer;
but it doesn't make much sense to say that instead the standard should
say, "no, except when the implementation says yes and maybe perhaps in
some other circumstances that we can't think of right now". Just like
it wouldn't make sense for the C standard to say that if the box
specifies how much recycled paper it contains, or if the country it's
sold in requires all boxes to contain a certain minimum of recycled
paper, then it must indeed contain at least that much.
Of course I've said the same thing in different words a dozen times now.
Please note that I'm perfectly happy to accept that some things are
defined outside the C scope (e.g. in POSIX), but the C standard should
write a couple of lines about that and its relation to such things,
that's all I ask.


Isn't it good enough that those other things explain their relation to
C?

....
If running the unoptimized code of fopen() had a side effect of
causing a disk drive to move its head and read some data, do you
believe that the C standard should forbid optimizing that side effect
away, too? Should the C standard forbid filesystems to cache data?


No. You are hitting on precisely the reason why it is so hard to define
"side effects" in the way that one would like. But you and I have an
intuitive idea. I'd prefer a sound definition, but on the other hand I
prefer a somewhat vague notion over a faulty definition.


I don't know about you, but my intuitive idea is that it depends on the
context. For instance, when I'm talking about medicine (or even about
POSIX), I don't necessarily try to apply the C standard's definition
when someone says "side effects". But when I'm reading the C standard,
my intuition tells me that I should interpret it exactly the way the
definition says. If the result didn't seem to make sense, then I could
suspect that something were wrong either with the definition or with how
the term is used. But my intuituion tells me that everything is OK.

....
Well excuse me, but then you haven't read very well. The reason is

that
it is currently possible to interpret the standard as Mr. Pop does,
basically, as in: there are circumstances where an implementation may
optimize away an fopen(), even if the plaform has last-touch

attributes or something similar.

What's a "platform"?


I presume your question is designed to convey the idea that the C
standard doesn't have the notion of a platform, as currently written?


Correct. There is not a single occurence of the word "platform" in the
C standard.

....
Funny, isn't it? It seems that POSIX doesn't mind if a compiler
optimizes your fopen() call away, either...


That's funny, indeed. What has POSIX to say of the access timestamp?


"The fgetc() function may mark the st_atime field of the file associated
with stream for update. The st_atime field shall be marked for update by
the first successful execution of fgetc(), fgets(), fgetwc(), fgetws(),
fread(), fscanf(), getc(), getchar(), gets(), or scanf() using stream
that returns data not supplied by a prior call to ungetc() or ungetwc()."

Nov 14 '05 #121
In <40***********@ mindspring.com> pete <pf*****@mindsp ring.com> writes:
James Kuyper wrote:
No, the C standard is not all of the law that applies to a C program.
If you use a C program to embezzle money, the C standard isn't the
relevant law you've broken. If you've written a C compiler that
secretly steals the user's credit card information, it isn't the C
standand that says you've done something wrong. And if an
implementation promises that file attributes will be updated by
successfull calls to fopen(), then it isn't the C standard that is
violated if that implementation optimizes away the fopen() calls. It's
that implementation' s own promises that are being broken.


If I can read a C program
and pronounce all of the required diagnostics for translation,
and then when it's time to run the program
if I claim that all my streams are closed,
can I be a conforming implementation ?


No, your stdin, stdout and stdout must be open, but any attempt to get
input from the first one or send output to the last two ones may fail.
Of course, no fopen() call need succeed. With this implementation, no
C program can produce any output and you can optimise each correct C
program to the equivalent of:

int main() { while(1); }

because the standard doesn't specify how long it takes to a program to
complete its execution.

Your translator will only have to implement translation phases 1 to 4
(only to the extent necessary for properly handling any active #error
directive), after that you can display "hello world" (just in case
the program needed a diagnostic during translation phase 7) and then
generate the code for the optimised version shown above.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #122
In <tJ************ ********@comcas t.com> "Douglas A. Gwyn" <DA****@null.ne t> writes:
Dan Pop wrote:
Which means that, a conforming C implementation must behave *as if* there
is no second program, hence the semantics of streams.


No.


Chapter and verse.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #123
pete <pf*****@mindsp ring.com> wrote in message news:<40******* ****@mindspring .com>...
James Kuyper wrote:
No, the C standard is not all of the law that applies to a C program.
If you use a C program to embezzle money, the C standard isn't the
relevant law you've broken. If you've written a C compiler that
secretly steals the user's credit card information, it isn't the C
standand that says you've done something wrong. And if an
implementation promises that file attributes will be updated by
successfull calls to fopen(), then it isn't the C standard that is
violated if that implementation optimizes away the fopen() calls. It's
that implementation' s own promises that are being broken.


If I can read a C program
and pronounce all of the required diagnostics for translation,
and then when it's time to run the program
if I claim that all my streams are closed,
can I be a conforming implementation ?


Of course, so long as you implement the required semantics of the
program. There's nothing in the standard that prohibits the use of a
wet-ware implementation. As a matter of QoI, you'd want to have some
suitable storage medium, such as paper, to implement files.
Nov 14 '05 #124
pete wrote:
If I can read a C program
and pronounce all of the required diagnostics for translation,
and then when it's time to run the program
if I claim that all my streams are closed,
can I be a conforming implementation ?


No, for a hosted conforming implementation the standard
I/O streams must be open at program startup. However,
they are allowed to fail upon use, provided they report
that in the specified way.

Nov 14 '05 #125
Dan Pop wrote:
No, your stdin, stdout and stdout must be open, but any attempt to get
input from the first one or send output to the last two ones may fail.
Of course, no fopen() call need succeed. With this implementation, no
C program can produce any output and you can optimise each correct C
program to the equivalent of:
int main() { while(1); }
because the standard doesn't specify how long it takes to a program to
complete its execution.


A conforming implementation is obliged to perform the
actions specified for the abstract C machine, although
if no side efects are visible in the environment then
there might be no way to *test* whether it conforms.
I think an infinite execution time for a finite-time
program is qualitatively wrong, although again one
could never be sure by external testing.
Note that nonconformance could be detected with
programs that aren't strictly conforming, e.g. writing
to device register or screen pixel.

Nov 14 '05 #126
In <eK************ ********@comcas t.com> "Douglas A. Gwyn" <DA****@null.ne t> writes:
Dan Pop wrote:
No, your stdin, stdout and stdout must be open, but any attempt to get
input from the first one or send output to the last two ones may fail.
Of course, no fopen() call need succeed. With this implementation, no
C program can produce any output and you can optimise each correct C
program to the equivalent of:
int main() { while(1); }
because the standard doesn't specify how long it takes to a program to
complete its execution.
A conforming implementation is obliged to perform the
actions specified for the abstract C machine,


Nonsense! In this case, no optimisation would be possible. Fortunately,
the standard proves you wrong:

3 In the abstract machine, all expressions are evaluated as
specified by the semantics. An actual implementation need not
^^^^^^^^
evaluate part of an expression if it can deduce that its value is
not used and that no needed side effects are produced (including
any caused by calling a function or accessing a volatile object).
although
if no side efects are visible in the environment then
there might be no way to *test* whether it conforms.
According to the as-if rule, it conforms.
I think an infinite execution time for a finite-time
program is qualitatively wrong, although again one
could never be sure by external testing.
There is no way to tell whether the program will terminate in 1 million
years or never. And the standard allows a finite-time program to
terminate in 1e6 years.
Note that nonconformance could be detected with
programs that aren't strictly conforming, e.g. writing
to device register or screen pixel.


Nope: such programs invoke undefined behaviour, so they cannot be used
in testing the conformance. Even if this weren't the case, the action
in question could be performed 1e6 years after the program startup...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #127
Dan Pop wrote:
Nope: such programs invoke undefined behaviour, so they cannot be used
in testing the conformance. Even if this weren't the case, the action
in question could be performed 1e6 years after the program startup...


Your "logic" leads to the conclusion that conformance
testing is impossible. Fortunately, you are wrong.

Nov 14 '05 #128
Douglas A. Gwyn wrote:

pete wrote:
If I can read a C program
and pronounce all of the required diagnostics for translation,
and then when it's time to run the program
if I claim that all my streams are closed,
can I be a conforming implementation ?


No, for a hosted conforming implementation the standard
I/O streams must be open at program startup. However,
they are allowed to fail upon use, provided they report
that in the specified way.


Suppose that all my streams are open on program startup,
but that they all close upon first attempted access.
You wind up with a program run that has no output
and takes no input.

--
pete
Nov 14 '05 #129
James Kuyper wrote:

pete <pf*****@mindsp ring.com> wrote in message news:<40******* ****@mindspring .com>...
James Kuyper wrote:
No, the C standard is not all of the law that applies to a C program.
If you use a C program to embezzle money, the C standard isn't the
relevant law you've broken. If you've written a C compiler that
secretly steals the user's credit card information, it isn't the C
standand that says you've done something wrong. And if an
implementation promises that file attributes will be updated by
successfull calls to fopen(), then it isn't the C standard that is
violated if that implementation optimizes away the fopen() calls. It's
that implementation' s own promises that are being broken.


If I can read a C program
and pronounce all of the required diagnostics for translation,
and then when it's time to run the program
if I claim that all my streams are closed,
can I be a conforming implementation ?


Of course, so long as you implement the required semantics of the
program. There's nothing in the standard that prohibits the use of a
wet-ware implementation. As a matter of QoI, you'd want to have some
suitable storage medium, such as paper, to implement files.


If all my streams are closed, then I can't use any files.

--
pete
Nov 14 '05 #130

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

49
2874
by: Ville Vainio | last post by:
I don't know if you have seen this before, but here goes: http://text.userlinux.com/white_paper.html There is a jab at Python, though, mentioning that Ruby is more "refined". -- Ville Vainio http://www.students.tut.fi/~vainio24
68
4375
by: Marco Bubke | last post by:
Hi I have read some mail on the dev mailing list about PEP 318 and find the new Syntax really ugly. def foo(x, y): pass I call this foo(1, 2), this isn't really intuitive to me! Also I don't like the brackets.
3
1429
by: Jase | last post by:
For some reason all of a sudden "£" signs are displaying as "?" when the field is displayed. The display code is: <%=rs.Fields("WagesText")%> This was working fine before, so I have no idea why it has suddenly changed. The code hasn't altered as far as I'm aware but it is a possibility. The database is SQL based.
27
5091
by: Curious Angel | last post by:
I have a resume in PDF format and I want anyone who LEFT-OR-RIGHT clicks the link to force the file to be saved, and in any event _not_ opened. Since the PDF will be in his cache in any event, I would just as soon place the employer in control of what directory he wishes to save it in, and there are two salient reasons for this: 1. I want him to OWN the document 2. I want him to FIND the document, quickly, on his hard drive In any...
0
364
by: Daniel | last post by:
how to make sure a xsl document has valid xsl syntax? i tried loading it into an xml document but that doesnt show syntax errors inside attributes such as "foo/bar" vs "bar\foo"
2
12174
by: Petr Jakes | last post by:
Hi, I am trying to set-up communication to the coin change-giver from my Linux box using the Python code. The change giver uses MDB (Multi Drop Bus) serial protocol to communicate with the master. MDB protocol is the 9bit serial protocol: (TX, RX lines only) 9600bps, 9bits, No Parity, 1 Start, 1 Stop. I would like to control the UART "parity bit" to try to simulate 9bit communication.
72
4232
by: Paminu | last post by:
In math this expression: (a < b) && (b < c) would be described as: a < b < c But why is it that in C these two expressions evaluate to something different for the same values of a, b and c?
5
1801
by: ZSP747 | last post by:
Now,I prepare to write a English-Chinese dictionary. So I want find a function or library can convert the words from one form to another,such as "boys"->"boy","became->become","went"->"go". Because there is so many unregular convertions in English then I think it will be a hard work if do it by myself. Thanks.
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9911
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.