[net.bugs.4bsd] Bug in 4.[12] BSD C compiler with "asm" statement

elvy@harvard.ARPA (Marc Elvy) (07/04/84)

Subject: Bug in "asm" statement in 4.X BSD C compiler
Index:	/lib/ccom and its neigbors

Description:

	The "asm" statement is handled incorrectly in both the 4.1BSD and
	4.2BSD C compilers.  An "asm" after an "if" is placed within the
	range of the "if"; braces make no difference.

Repeat-By:

	Compile the following C program.  This was prepared explicitly for
	illustration purposes (which is why R11 is assumed), so please do
	not waste your time hassling me about the philosophical problems
	with "asm" statements.


	main ()
	{
	    register int flag = 0;

	    if (flag == 1)
		flag = 0;

	    asm ("movl	$666, r11");
	}


	The relevant portion of the assembly language produced follows.


		clrl	r11
		cmpl	r11,$1
		jneq	L16
		clrl	r11
	movl	$666, r11		/* Here is the "asm" statement. */
	L16:
		ret

	Note that the "movl" should be AFTER the label, not before.
	Unfortunately, I have not discovered why this is done, but I
	suspect that the asm statement is dumped before the if statement
	is completely processed.

Marc


		      Marc A. Elvy  ( elvy@harvard.{arpa,uucp} )
			     Aiken Computation Laboratory
				  Harvard University

chris@umcp-cs.UUCP (07/05/84)

This is what's called a ``known bug'' (or sometimes a ``feature'').
Everyone who uses asm() is supposed to know that extra null statements
are needed after if's.  asm()s sneak through the back door of the
compiler and don't count as a statement.  I suspect it's not worth
fixing.  By the way, if you write


	if (foo())
		asm("hi there");
		bar();
	else
		baz();

the call to ``bar'' is inside the ``if'', not outside.  The compiler
won't complain about the ``else'' either.

I say, leave the ``feature'' in there as a warning to anyone who
wants to use asm()!

Not entirely serious,
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci (301) 454-7690
UUCP:	{seismo,allegra,brl-bmd}!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@maryland

greg@sdcsvax.UUCP (07/17/84)

The reason that this happens is that the "asm" statement is effectivly
processed at the lexical level and just dumped into the output file.
It would work correctly if the "asm" statment were turned into a
semi-colon, have that processed, and THEN drop it into the output.
This is the reason for the common hack to always preceed the "asm"
statement by a semi-colon.  In fact, if pre-processor substitution
worked the way the C standard will have it (where a substituted macro
loses its meaning during the evaluation; see the parallel discussion
in net.unix-wizards), using
#define asm ;asm
would cause it to work in all cases -- at least I can't think of any
where it wouldn't.
-- 
-- Greg Noel, NCR Torrey Pines       Greg@sdcsvax.UUCP or Greg@nosc.ARPA