Exception handling in C expressions

Marko =?ISO-8859-1?Q?M=E4kel=E4?= <Marko.Makela@HUT.FI>
23 Mar 2000 22:31:41 -0500

          From comp.compilers

Related articles
Exception handling in C expressions Marko.Makela@HUT.FI (Marko =?ISO-8859-1?Q?M=E4kel=E4?=) (2000-03-23)
| List of all articles for this month |

From: Marko =?ISO-8859-1?Q?M=E4kel=E4?= <Marko.Makela@HUT.FI>
Newsgroups: comp.compilers
Date: 23 Mar 2000 22:31:41 -0500
Organization: Helsinki University of Technology, CS lab
Keywords: C, design

I've implemented an expression evaluator that catches different kinds
of exceptions: undefined variable, overflow, type constraint
violation, division by zero, etc.

I decided to optimize the expression evaluator by translating each
expression to a C function that evaluates it. The program invokes a C
compiler and dynamically loads the generated code, which has a table
of function pointers:

void(*e[]) (jmp_buf* jmp, void* result)

Each expression evaluator function takes a pointer to an initialized
jmp_buf, and a pointer to the memory area that holds the result. (The
evaluation result does not necessarily fit in a register.)

When the expression evaluator detects an error condition, it performs
longjmp with some error code:

#define ex(cond,code) ((cond) ? longjmp (*jmp, code) : (void) 0)

This is pretty nice, since the call to longjmp can be embedded in any
subexpression. For instance, a variable reference can be translated
as (ex(!x, errorcode), *x).

But there is a problem that did not occur to me until I made some
performance measurements. Setting up the jump context with setjmp
generates a considerable overhead, especially when the expressions to
evaluate are relatively simple. For my purposes, it would be enough
if the generated code could just do "return errorcode" inside an

Is there any theoretical reason why the return statement in C could
not be treated as a void expression? (A practical reason could be
that it would break existing code: the statement "return a,b,c;" would
have to be rewritten as "return (a,b,c);" or "a,b,return c;".)

Marko Mškelš
[The theoretical reason is that there's a standard definition of C, and
that's not it. If you want a mutant version of C, you can of course do
whatever you want. -John]

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.