lkml.org 
[lkml]   [1998]   [May]   [20]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: 'C' Operators precedence
On Wed, 20 May 1998, David Todd wrote:

> >From "C - A Reference Manual", Fourth Edition by Harbison and Steele:
>
> In general, the compiler can rearrange the order in which
> an expression is evaluated. The rearrangement may consist
> of evaluating only the arguments of a function call, or
> the two operands of a binary operator, in some order other
> than the obvious left-to-right order. The binary operators
> +, *, &, ^, and | are assumed to be completely associative
> and commutative, and a compiler is permitted to exploit this
> assumption.
>
> The upshot:
>
> int a = 0;
> int f() {a=2; return 1}
> int g() {a=1; return 1}
>
> main ()
> {
> printf (a + f()); /* could print 1 or 3 */
> printf (g() + f()); /* will print 1, a could = 1 or 2 */
> printf (g() + a + f()); /* could print 2, 3, or 4 */
> }
>
> The compiler is merely obliged to not interfere with type promotion.
>
> So, the important thing is don't write functions with side effects and DON'T
> USE GLOBALS.

Look damnit..........
Script started on Wed May 20 12:00:36 1998
# cat aaa.c
#include <stdio.h>

static int a, b, c, d, e;
static int f()
{
return a;
}
int main()
{
b = 1;
c = 2;
d = 3;
e = 4;
printf("%d\n", b + c + d + d + f());
return 0;

}

# gcc -S -o aaa -Wall -pedantic -ansi aaa.c
# cat aaa
.file "aaa.c"
.version "01.01"
gcc2_compiled.:
.text
.align 16
.type f,@function
f:
pushl %ebp
movl %esp,%ebp
movl a,%eax
jmp .L1
.align 16
.L1:
movl %ebp,%esp
popl %ebp
ret
.Lfe1:
.size f,.Lfe1-f
.section .rodata
.LC0:
.string "%d\n"
.text
.align 16
.globl main
.type main,@function
main:
pushl %ebp
movl %esp,%ebp
movl $1,b <---- variables initialized
movl $2,c
movl $3,d
movl $4,e
call f <------- function called first
movl %eax,%eax
movl b,%edx <------- now other variables evaluated.
addl c,%edx
movl %edx,%ecx
addl d,%ecx
movl %ecx,%edx
addl d,%edx
addl %edx,%eax
pushl %eax
pushl $.LC0
call printf
addl $8,%esp
xorl %eax,%eax
jmp .L2
.align 16
.L2:
movl %ebp,%esp
popl %ebp
ret
.Lfe2:
.size main,.Lfe2-main
.local a
.comm a,4,4
.local b
.comm b,4,4
.local c
.comm c,4,4
.local d
.comm d,4,4
.local e
.comm e,4,4
.ident "GCC: (GNU) 2.7.2"
# exit
exit

Script done on Wed May 20 12:01:44 1998

This must happen as shown for the reasons previously stated.


Cheers,
Dick Johnson
***** FILE SYSTEM MODIFIED *****
Penguin : Linux version 2.1.101 on an i586 machine (66.15 BogoMips).
Warning : It's hard to remain at the trailing edge of technology.


-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu

\
 
 \ /
  Last update: 2005-03-22 13:42    [W:0.079 / U:0.944 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site