🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.misc
Article #940

setjmp() (was Re: Relationship between C and C++)

#940
From: Peter Klausler
Date: Thu, 22 Mar 1990 16:27
53 lines
2999 bytes
>> Unrestrained pointers are the data structures equivalent of
>> uncontrolled GOTOs. A language like C that lets you do anything
>> in the world that you please with pointers is very similar to a
>> language like BASIC that lets you arbitrarily jump to anyplace you
>> want to in your code.
>C *is* a language that allows you to jump anywhere you want in your code.
>By using longjmp and goto you have nearly as much freedom as you do in
>Basic (or, more, assembler). If only you could assign gotos, it'd be
>perfect [1]. Think of it as a very portable very high-level assembler [2].

It's this kind of freedom of expression, though, that severely constricts many
analyses an optimizing compiler must perform. Setjmp is particularly messy,
since it allows flow of control in a function that isn't explicit. As an
example, consider:

	#include <setjmp.h>
	f (void) {
		jmp_buf jb;
		if (setjmp (jb))		/*1*/
			goto dijkstra;
		while (1)
			g ();
		j ();				/*2*/
	dijkstra: ;
	}

Ordinarily, I could throw line /*2*/ away, since there's no normal flow of
control in f() that could arrive there. In order to process this program
correctly, my compiler needs to know that setjmp() is special, and assume
that control-flow arcs exist from every other function call to the point of
return from setjmp at /*1*/. This yields a very messy flow-graph and prevents
common optimizing transformations.

The ANSI C standard attempts to simplify the situation with its proviso that
only volatile objects will survive a longjmp, but this doesn't help much in
the case of flow-graph construction, and volatile variables completely cripple
optimization anyway.

Fortran has similar problems, with its ASSIGN and assigned GOTO statements,
but the '78 standard has nice rules that restrict ASSIGNs to scalar variables,
and prohibit the copying of ASSIGNed variables to one another. Although we must
assume flow-of-control from each assigned GOTO to every ASSIGNed label, this
tends to be less messy. The programmer even has the ability to restrict the
range of potential targets of the GOTO. I'm not saying the Fortran is superior
to C in any aesthetic sense (sheath your swords), but it's sure nicer to
optimize and parallelize. This is just one example, and not a particularly
good one, since ASSIGNs and setjmps are pretty rare; pointer arguments and
their aliasing problems are nastier by far.

So, if you're using an optimizing compiler, keep your setjmp calls out of
time-critical routines; we'll both be happier.

-Peter Klausler, writing compilers at Cray Computer Corp. in Colorado Springs
Technical Details
Message-ID: <1990Mar22.162737.9371@craycos.com>
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!samsung!zaphod.mps.ohio-state.edu!sunybcs!boulder!pikes!aspen.craycos.com!pmk

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!samsung!zaphod.mps.ohio-state.edu!sunybcs!boulder!pikes!aspen.craycos.com!pmk
From: pmk@craycos.com (Peter Klausler)
Newsgroups: comp.lang.misc
Subject: setjmp() (was Re: Relationship between C and C++)
Message-ID: <1990Mar22.162737.9371@craycos.com>
Date: 22 Mar 90 16:27:37 GMT
Organization: Cray Computer Corporation
Lines: 53