Question of Ignorance

Dataspan Inc dsi at unccvax.UUCP
Thu May 9 05:08:27 AEST 1985


    Pardon my excess stupididy, but I have a question about the
** CORRECT ** way a C-compiler (by definition) should handle the
following program. Consider main.c, below


/*
 * main.c
 */
char *charptr; 
short *shortptr;
int  *intptr; 
long *longptr;
main()
{
if(charptr == 32); 
if(intptr == 32)  ;
if(longptr== 32) ;
if(shortptr == 32 );
}
/*
 * end main.c 
 */

   This is the assembly language that my VAX 4.2bsd included-in-the-
distribution C-compiler takes care of main.c:

LL0:
	.data
	.comm	_charptr,4
	.comm	_shortptr,4
	.comm	_intptr,4
	.comm	_longptr,4
	.text
	.align	1
	.globl	_main
_main:
	.word	L16
	jbr 	L18
L19:
	cmpl	_charptr,$32
	jneq	L20
L20:
	cmpl	_intptr,$32
	jneq	L21
L21:
	cmpl	_longptr,$32
	jneq	L22
L22:
	cmpl	_shortptr,$32
	jneq	L23
L23:
	ret
	.set	L16,0x0
L18:
	jbr 	L19
	.data


    And * THIS * is the way the Alcyon C-compiler handles it:


.globl _charptr
.comm _charptr,4
.globl _shortptr
.comm _shortptr,4
.globl _intptr
.comm _intptr,4
.globl _longptr
.comm _longptr,4
.globl _main
.text
_main:
~~main:
~_EnD__=8
link R14,#-4
*line 8
*line 8
cmp.l #32,_charptr
bne L2
L2:
*line 9
*line 9
cmp.l #64,_intptr
bne L3
L3:
*line 10
*line 10
cmp.l #128,_longptr
bne L4
L4:
*line 11
*line 11
cmp.l #64,_shortptr
bne L5
L5:
L1:
unlk R14
rts
.data

    Note that the Alcyon compiler leaves the constant alone if the
thing is a char; shifts it left one if it is an int (2 bytes), and
left two if it is a long (4 bytes).

     Bill Allen at Alcyon Corporation assures me that this is not
a bug, that constants MUST BE MULTIPLIED BY THE NUMBER OF BYTES
when doing a test with a pointer. He even swears that the regular
C compiler cc must do this.

    When you are doing a proprietary product, it is sometimes nice
to do stupid things like mixing types in an if() and having things
be handled intelligently. Of course, you can do something like:

long *longptr;
long templong;
main()
{
    .    
    . 
    templong = longptr;
    if (templong == 32) ; /* do something nerdy */
    .
    .
}

   but this is just as bad as doing what you (thought?) you wanted
inside the if, anyway (??).

    Jim Wiley, an associate, thinks that the Alcyon confuses 
alignment problem with

    longptr = longptr + 32;

    which of course means 32 boundaries, i.e. 128 would be the
immediate operand.

   I'm really ignorant about C-language with this particular problem,
and would greatly appreciate comments on these hypothetical
programs and their .asm files.

-dya-

PS: If you have an oddball compiler, a listing generated from the
first main() above would be interesting...

PSS: The Whitesmiths handles it "correctly" (the immediate operand
to cmpi.l is #32 in every case)


David Anthony
Senior Analog Nut
DataSpan, Inc.



More information about the Comp.lang.c mailing list