Put your code... (was Re: gotos

kenny at uiucdcsb.cs.uiuc.edu kenny at uiucdcsb.cs.uiuc.edu
Tue Apr 26 10:48:00 AEST 1988


[Part 5. Sets of actions.]

Knuth mentions that in many examples of simulators and interpreters,
it is advantageous to do things like:

subtract: operand = - operand; goto add;
add: accumulator = accumulator + operand; goto no_op;
jump_if_overflow: if (overflow_indicator) {
			overflow_indicator = false;
			goto jump;
			}
		else goto no_op;
no_op: ++simulated_time;

I submit that, with today's larger memories, it is preferable to
duplicate the code (It's certainly faster not to have all the
unconditional branches in the instruction stream).  The above could be
written as something more like:

#define ADD accumulator = accumulator + operand
#define ADVANCE_TIME ++simulated_time
case subtract:	operand = - operand; ADD; ADVANCE_TIME; break;
case add:	ADD; ADVANCE_TIME; break;
case jump_if_overflow:
		if (overflow_indicator) {
			overflow_indicator = false;
			JUMP;
		}
		ADVANCE_TIME; break;
case no_op:	ADVANCE_TIME; break;

Moral: If the programmer is tempted to use GOTOs for a non-looping
control structure, it is preferable to duplicate code, using macros if
necessary to keep the duplicated code grouped together in the source
file.

[End of part 5.]



More information about the Comp.lang.c mailing list