Trisha Shetty (Editor)

COMEFROM

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

In computer programming, COMEFROM (or COME FROM) is an obscure control flow structure used in some programming languages, originally as a joke. COMEFROM is roughly the opposite of GOTO in that it can take the execution state from any arbitrary point in code to a COMEFROM statement.

Contents

The point in code where the state transfer happens is usually given as a parameter to COMEFROM. Whether the transfer happens before or after the instruction at the specified transfer point depends on the language used. Depending on the language used, multiple COMEFROMs referencing the same departure point may be invalid, be non-deterministic, be executed in some sort of defined priority, or even induce parallel or otherwise concurrent execution as seen in Threaded Intercal.

A simple example of a "COMEFROM x" statement is a label x (which does not need to be physically located anywhere near its corresponding COMEFROM) that acts as a "trap door". When code execution reaches the label, control gets passed to the statement following the COMEFROM. This may also be conditional, passing control only if a condition is satisfied, analogous to a GOTO within an IF statement. The primary difference from GOTO is that GOTO only depends on the local structure of the code, while COMEFROM depends on the global structure – a GOTO transfers control when it reaches a line with a GOTO statement, while COMEFROM requires scanning the entire program or scope to see if any COMEFROM statements are in scope for the line, and then verifying if a condition is hit. The effect of this is primarily to make debugging (and understanding the control flow of the program) extremely difficult, since there is no indication near the line or label in question that control will mysteriously jump to another point of the program – one must study the entire program to see if any COMEFROM statements reference that line or label.

Debugger hooks can be used to implement a COMEFROM statement, as in the humorous Python goto module; see below. This also can be implemented with the gcc feature "asm goto" as used by the Linux kernel configuration option CONFIG_JUMP_LABEL. A no-op has its location stored, to be replaced by a jump to an executable fragment that at its end returns to the instruction after the no-op.

History

COMEFROM was initially seen in lists of joke assembly language instructions (as 'CMFRM'). It was elaborated upon in a Datamation article by R. Lawrence Clark in 1973, written in response to Edsger Dijkstra's letter Go To Statement Considered Harmful. COMEFROM was eventually implemented in the C-INTERCAL variant of the esoteric programming language INTERCAL along with the even more obscure 'computed COMEFROM'. There were also Fortran proposals for 'assigned COME FROM' and a 'DONT' keyword (to complement the existing 'DO' loop).

On 1 April 2004, Richie Hindle published an implementation of both GOTO and COMEFROM for the Python programming language. Despite being released on April Fools' Day and not being intended for serious use, the syntax is valid and the implementation fully works.

Examples

The following is an example of a program in a hypothetical BASIC dialect with "COMEFROM" instead of "GOTO".

This program (hypothetically) works by asking the user for their name, greeting them with the same name, and continuing all over again. The instruction "REM" on line 40 is simply a NOP — the "COMEFROM" statement on line 10 causes a branch back to that line when execution reaches line 40, regardless of its contents.

A fully runnable example in Python with the joke goto module installed (which uses debugger hooks to control program execution) looks like this:

This is an implementation in Ruby of the Intercal COME FROM statement.

Some examples of the debug packet feature of the OS/360 Fortran G compiler:

  • Example 1: the values of SOLON, GFAR, and EWELL are examined as they were at the completion of statement 10. The AT statement indicates statement 11.
  • Example 2: all the values of STOCK are displayed when statement 35 is encountered.
  • Example 3: tracing begins at statement 10, at statement 20, tracing stops while the loop is executed, and resumes after the loop. Tracing stops just before statement 30 is executed.
  • Hardware implementation

    The SHARC DSP supports a DO..UNTIL instruction, intended for do..while loops, that is essentially a COMEFROM. Example:

    LCNTR=42; DO x UNTIL LCE; /* COMEFROM x, unless the loop counter is zero */ F12=F0*F4, F8=F8+F12, F0=dm(I0,M3), F4=pm(I8,M9); IF NZ dm(I2,M2) = F12; IF ZF dm(I2,M2) = F1; x: R2 = R3 + 76; /* the label "x" does not exist in the machine code */

    Note that the loop termination condition, here specified as LCE (loop counter expired), can be set to other values including always-true and always-false. With an always-true condition, we have a genuine COMEFROM. The hardware supports up to six simultaneously active COMEFROMs.

    A similar feature exists in Microchip dsPIC's for (unnested) loops provided by the 'DO' assembly instruction. Its interruptible and trivial nesting goes up to one level with an additional 5 levels in software.

    References

    COMEFROM Wikipedia