Usenet comp.sys.dec (1990-2013)
 help / color / mirror / Atom feed
* Compiler flags of dubious utility.
@ 1995-12-12  5:00 Ron Natalie
  1995-12-12  5:00 ` Dan Pop
  0 siblings, 1 reply; 7+ messages in thread
From: Ron Natalie @ 1995-12-12  5:00 UTC (permalink / raw)


We've been running our application on the AXP (UNIX) for several years
and we came accross something we thought had promise, a flag on the
compiler to use 32 bit pointers.  We want to do this not because we
are lazy, but because it's a waste of memory (and we do a lot of pointers).

Well as soon as we finished the build and our code segv'd we realized
the folly.  Despite the fact that you can build 32bit applications,
there is no 32-bit compiled version of system libraries (or am I missing
something).

-Ron




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Compiler flags of dubious utility.
  1995-12-12  5:00   ` Ron Natalie
@ 1995-12-12  5:00     ` Ron Natalie
  1995-12-15  5:00       ` Jeffrey Mogul
  0 siblings, 1 reply; 7+ messages in thread
From: Ron Natalie @ 1995-12-12  5:00 UTC (permalink / raw)


Dan Pop (danpop@mail.cern.ch) wrote:

: Apparently, you're missing -taso at link time.  If you do everything as
: documented in the man page, the system libraries work with your 32-bit
: pointers:

No, I'm not... consider this slight modification to your program.

	#include <stdio.h>
	main()  {
	    fprintf(stdout, "Hello World\n");
 	}

Then compiling with the -xtaso_short and linking with -taso causes
a non-functioning executable to be generated.  Your program was
just a little too trivial.





^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Compiler flags of dubious utility.
  1995-12-12  5:00 ` Dan Pop
@ 1995-12-12  5:00   ` Ron Natalie
  1995-12-12  5:00     ` Ron Natalie
  0 siblings, 1 reply; 7+ messages in thread
From: Ron Natalie @ 1995-12-12  5:00 UTC (permalink / raw)


Dan Pop (danpop@mail.cern.ch) wrote:


: Apparently, you're missing -taso at link time.  If you do everything as
: documented in the man page, the system libraries work with your 32-bit
: pointers:

No, I'm not... consider this slight modification to their

	#include <stdio.h>
	main()  {
	    fprintf(stdout, "Hello World\n");
	}




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Compiler flags of dubious utility.
  1995-12-12  5:00 Compiler flags of dubious utility Ron Natalie
@ 1995-12-12  5:00 ` Dan Pop
  1995-12-12  5:00   ` Ron Natalie
  0 siblings, 1 reply; 7+ messages in thread
From: Dan Pop @ 1995-12-12  5:00 UTC (permalink / raw)


In <4aih5k$m1l@topaz.sensor.com> ron@topaz.sensor.com (Ron Natalie) writes:

>We've been running our application on the AXP (UNIX) for several years
                                                      ^^^^^^^^^^^^^^^^^
Couldn't be more than 3 :-)

>and we came accross something we thought had promise, a flag on the
>compiler to use 32 bit pointers.  We want to do this not because we
>are lazy, but because it's a waste of memory (and we do a lot of pointers).
>
>Well as soon as we finished the build and our code segv'd we realized
>the folly.  Despite the fact that you can build 32bit applications,
>there is no 32-bit compiled version of system libraries (or am I missing
>something).

Apparently, you're missing -taso at link time.  If you do everything as
documented in the man page, the system libraries work with your 32-bit
pointers:

    ues5:~/tmp 11> cat test.c
    #include <stdio.h>

    int main()
    {
    char *msg = "Hello, world!";
    printf("%ld\n", (long)sizeof msg);
    puts(msg);
    return 0;
    }
    ues5:~/tmp 12> cc -c -xtaso_short test.c
    ues5:~/tmp 13> cc -taso test.o
    ues5:~/tmp 14> ./a.out 
    4
    Hello, world!
    ues5:~/tmp 15> cc test.o
    ues5:~/tmp 16> ./a.out 
    4
    Segmentation fault (core dumped)

Dan
--
Dan Pop
CERN, CN Division
Email: danpop@mail.cern.ch 
Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Compiler flags of dubious utility.
  1995-12-12  5:00     ` Ron Natalie
@ 1995-12-15  5:00       ` Jeffrey Mogul
  1995-12-15  5:00         ` Dan Pop
  1995-12-15  5:00         ` Dan Pop
  0 siblings, 2 replies; 7+ messages in thread
From: Jeffrey Mogul @ 1995-12-15  5:00 UTC (permalink / raw)


In article <4akvcr$16q@topaz.sensor.com> ron@topaz.sensor.com (Ron Natalie) writes:
>	#include <stdio.h>
>	main()  {
>	    fprintf(stdout, "Hello World\n");
> 	}
>
>Then compiling with the -xtaso_short and linking with -taso causes
>a non-functioning executable to be generated.  Your program was
>just a little too trivial.

Actually, I'm not sure if "-xtaso_short" is of much utility by itself.
However, the 32-bit compiler support is actually pretty good, if you
understand how to use it.  It's the documentation that is weak.

In order to use 32-bit pointers successfully, you usually need to
do a tiny bit of work to make sure that pointers exchanged with
the outside world are properly declared.  Note that this does not
apply to parameters, since the compiler promotes all 32-bit values
to become 64-bit values.  So the problem with the program is not
that FILE *stdout is only 32 bits wide, but rather that it points
to a FILE that contains pointer fields, and although the library thinks
these pointer fields are 64 bits wide, your module has only allocated
32 bits for them.

Here's a correct example of mixing 32 and 64 bit pointers:

    #include <stdio.h>
    main()
    {
	    char *format1 = "sizeof(format1) = %d\n";
    #pragma pointer_size (short)
	    char *format2 = "sizeof(format2) = %d\n";
    
	    fprintf(stdout, format1, sizeof(format1));
	    fprintf(stdout, format2, sizeof(format2));
    }

I called this program "ron.c" and compiled it with
	cc -o ron -xtaso -taso ron.c 
Running the program yields:
	sizeof(format1) = 8
	sizeof(format2) = 4

The "-xtaso" flag simply tells the compiler to pay attention
to the "#pragma pointer_size" directives (which are compiler
directives, not preprocessor commands, in spite of the #).
If you compiled the same program without -xtaso, then the
#pragma would be ignored.

Possible pragmas:
	#pragma pointer_size (short)
means declarations use 32-bit pointers until otherwise specified
	#pragma pointer_size (long)
means declarations use 64-bit pointers until otherwise specified
	#pragma pointer_size (save)
saves current pointer-size on a stack of some sort
	#pragma pointer_size (restore)
pops the stack value

(It's not my fault that they used "short" here to mean "32 bits".)

As far as I can tell, "-xtaso_short" really means "-xtaso, but
also insert a #pragma pointer_size (short) at the start of the
module."  As you've found out, this is not such a good idea because
most modules include header files that declare structures that
are shared with 64-bit library routines.

Generally, sticking a "#pragma pointer_size (short)" after all
the system #includes does the right thing.  But there is one
nasty surprise still lurking.

The declaration
	#pragma pointer_size (short)
	char **argv;
declares argv as a 32-bit pointer to an array of 32-bit pointers.
However, if this is the usual argv, it's already set up as a pointer
to an array of 64-bit pointers by the exec() mechanism, and so you
can't declare argv while "short" pointers are enabled.  I suppose
the type-safe thing to do would be something like

	#pragma pointer_size (long)
	typedef char *argv_element;
	
	#pragma pointer_size (short)
	argv_element *argv;

but that's a lot of extra bother.  I just convert the usual
declaration of main() to look like:
    main(argc, argv)
    #pragma pointer_size (save)
    #pragma pointer_size (long)
    int argc;  char **argv;
    #pragma pointer_size (restore)
    {
and everything is cool.

I supposed it would be neat if the compiler could be trained to
do some of this automagically, but it's not that hard to do it
by hand.  In fact, most programs with large numbers of pointers
have relatively few important pointer types, and if you just surround
the declarations of those specific types with 
    #pragma pointer_size (save)
    #pragma pointer_size (short)
    typedef struct whatever *whateverp;
    #pragma pointer_size (restore)
your data size should shrink nicely.

-Jeff



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Compiler flags of dubious utility.
  1995-12-15  5:00       ` Jeffrey Mogul
  1995-12-15  5:00         ` Dan Pop
@ 1995-12-15  5:00         ` Dan Pop
  1 sibling, 0 replies; 7+ messages in thread
From: Dan Pop @ 1995-12-15  5:00 UTC (permalink / raw)


In <4aqnhp$dpn@usenet.pa.dec.com> mogul@pa.dec.com (Jeffrey Mogul) writes:

>Here's a correct example of mixing 32 and 64 bit pointers:

It is, however, a bad example of using fprintf :-)

>    #include <stdio.h>
>    main()
>    {
>	    char *format1 = "sizeof(format1) = %d\n";
>    #pragma pointer_size (short)
>	    char *format2 = "sizeof(format2) = %d\n";
>    
>	    fprintf(stdout, format1, sizeof(format1));
>	    fprintf(stdout, format2, sizeof(format2));
>    }

%d expects an int.  sizeof returns a size_t value, which on OSF/1 is an
unsigned long.  The result is undefined behaviour.  There are two 
independent reasons which make the code to work as intended on DEC OSF/1
(32-bit int's are promoted to 64 bits when they are passed as function
arguments and OSF/1 is little endian, anyway).  But this is not enough
to make it correct code.

Dan
--
Dan Pop
CERN, CN Division
Email: danpop@mail.cern.ch 
Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Compiler flags of dubious utility.
  1995-12-15  5:00       ` Jeffrey Mogul
@ 1995-12-15  5:00         ` Dan Pop
  1995-12-15  5:00         ` Dan Pop
  1 sibling, 0 replies; 7+ messages in thread
From: Dan Pop @ 1995-12-15  5:00 UTC (permalink / raw)


In <4aqnhp$dpn@usenet.pa.dec.com> mogul@pa.dec.com (Jeffrey Mogul) writes:

>Possible pragmas:
>	#pragma pointer_size (short)
>means declarations use 32-bit pointers until otherwise specified
>	#pragma pointer_size (long)
>means declarations use 64-bit pointers until otherwise specified
>	#pragma pointer_size (save)
>saves current pointer-size on a stack of some sort
>	#pragma pointer_size (restore)
>pops the stack value
>
>(It's not my fault that they used "short" here to mean "32 bits".)
>
>As far as I can tell, "-xtaso_short" really means "-xtaso, but
>also insert a #pragma pointer_size (short) at the start of the
>module."  As you've found out, this is not such a good idea because
>most modules include header files that declare structures that
>are shared with 64-bit library routines.

The idea is good, the implementation of the system header files is bad :-(

>Generally, sticking a "#pragma pointer_size (short)" after all
>the system #includes does the right thing. 

No, this is a workaround.  The right thing would have been for the
header files to protect the sensitive declarations with:

#pragma pointer_size (save)
#pragma pointer_size (long) 
...
#pragma pointer_size (restore)

and avoid the programmer the need for messing with these pragmas in his
code.

Ditto for the char **argv issue, which is trivial to implement within
the compiler (the second and third arguments of main are handled as
special cases) or, even better, in the startup module, which could
build the right type of pointer arrays, according to the actual type of
argv and envp.

Dan
--
Dan Pop
CERN, CN Division
Email: danpop@mail.cern.ch 
Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~1995-12-15  5:00 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1995-12-12  5:00 Compiler flags of dubious utility Ron Natalie
1995-12-12  5:00 ` Dan Pop
1995-12-12  5:00   ` Ron Natalie
1995-12-12  5:00     ` Ron Natalie
1995-12-15  5:00       ` Jeffrey Mogul
1995-12-15  5:00         ` Dan Pop
1995-12-15  5:00         ` Dan Pop

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox