Just for fun, I wrote a very small C program. I compiled it on Linux/Intel
using GCC 4.8.2. I then got it compiled on z/OS 1.13. The program is very
small:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#undef strlen
int main (int argc, char *argv[]) {
    int i;
    for(i=0;i<argc;i++) {
       if ('\r' == *(argv[i]+strlen(argv[i])) )
          *(argv[i]+strlen(argv[i]))='\0';
    }
    return 0;
}

On the z/OS compiler, under UNIX, using the -O5 switch to optimize, the
compiler generated in-line code for both calls to strlen, despite the fact
that they had the identical arguments with no possibility of modification.
The GCC compiler, on the other hand, retained the result of the first
strlen() and used that value in the second statement. Actually, GCC
retained the value of "argv[i]+strlen(argv[i])", so that it the equivalent
of a CLI, JNE, MVI to change the \r to \0.

Of course, I can help the compiler by changing my code slightly:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#undef strlen
int main (int argc, char *argv[]) {
    int i;
    char *lastchar; */
    for(i=0;i<argc;i++) {
       lastchar=argv[i]+strlen(argv[i]);
       if ('\r' == *lastchar)
          *lastchar='\0';
    }
    return 0;
}

Much nicer. But, again curiously, instead of doing a CLI and MVI, the
compiler using -O5 did:

SLR R0,R0
IC R1,0(,R1)
CHI R1,=H'13'
BNE ...

On ARCH(7), it was a bit better, replacing the SLR/IC pair with an LLC
instruction.

I failed the compiler class in college, so for all I know there is a
perfectly good reason why the z/OS compiler does it this way. But I just
found it curious and thought that I'd throw it out onto the forum.
Hopefully someone can explain it to me. BTW, I compile the above using no
optimization, -O2, -O3, -O3, and -O5. I got the identical assembler in all
cases. This was true in the default ARCH(5) and ARCH(7).

I know some with likely say that I'm grasping at straws. Perhaps so. But if
the compiler misses such a simple optimization, what else might it miss?


-- 
Wasn't there something about a PASCAL programmer knowing the value of
everything and the Wirth of nothing?

Maranatha! <><
John McKown

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to lists...@listserv.ua.edu with the message: INFO IBM-MAIN

Reply via email to