Re: Exception Handling

"journeyman" <journeyman@compilerguru.com>
4 Aug 2002 11:42:26 -0400

          From comp.compilers

Related articles
Exception Handling david.jobet@ng-market.com (David Jobet) (2002-07-21)
Re: Exception Handling vbdis@aol.com (VBDis) (2002-07-24)
Re: Exception Handling casse@netcourrier.com (Casse Hugues) (2002-07-24)
Re: Exception Handling jacob@jacob.remcomp.fr (jacob navia) (2002-07-24)
Re: Exception Handling david.jobet@ng-market.com (David Jobet) (2002-07-25)
Re: Exception Handling journeyman@compilerguru.com (journeyman) (2002-08-04)
Re: Exception Handling nmm1@cus.cam.ac.uk (Nick Maclaren) (2002-08-10)
Re: Exception Handling marcov@toad.stack.nl (Marco van de Voort) (2002-08-10)
Re: Exception Handling max1@mbank.com.ua (Maxim Reznik) (2002-08-10)
Re: Exception Handling fjh@cs.mu.OZ.AU (Fergus Henderson) (2002-08-14)
Exception Handling tschirre@cs.curtin.edu.au (Bernhard Tschirren) (1998-09-13)
Re: Exception Handling mrs@kithrup.com (1998-09-18)
[3 later articles]
| List of all articles for this month |
From: "journeyman" <journeyman@compilerguru.com>
Newsgroups: comp.compilers
Date: 4 Aug 2002 11:42:26 -0400
Organization: Giganews.Com - Premium News Outsourcing
References: 02-07-075
Keywords: errors, C
Posted-Date: 04 Aug 2002 11:42:26 EDT

On 21 Jul 2002 02:06:29 -0400, David Jobet <david.jobet@ng-market.com> wrote:
>I'm developping a compiler (for the fun) for a new language called Nosica.
>(http://nosica.ng-market.net)
>
>I'm stuck with exception implementation because I'm using C as as my
>assembly language.


That's not necessarily a problem by itself, but EH implementation
tends to require direct manipulation of the stack frame. Nonstandard
and nonportable.


One of the gotchas with C++ EH is that the exception object's lifetime
is from the throw until the exception is fully handled (probably
several stack frames up). Plus, you can have try/catch within the
handler. You can't just discard the object when the handler takes it,
in case there's a rethrow. One way to implement it is to have the
exeption object allocated on the stack (effectively using alloca), and
returning to the handler context by reseting the frame pointer, but
not the stack pointer. This typically needs to be done in assembler
because it involves direct manipulation of registers (although there
are nonstandard ways to do this, eg. inline assembly or compiler
intrinsic functions).


[snip]


>Basically, I need to have the return address that the called function
>would return to if there was no exception thrown. In this case, the
>compiler could switch on this return value and produce the *good*
>code needed to clean up local variables.
>
>Problem is I don't know how to get this return address in order to produce
>the good case (here h_addr and f_addr).
>
>Is there any way to get them using assembly ?


Of course, but how depends on the particular target architecture.


Since you're the one implementing the compiler, you can make things
easier on yourself by putting in "hidden" handlers that are just there
for the bookkeeping:


foo(){ TRY {new obj A; some code; new obj B; more code} CATCH {handler code} }


is implemented as something like this (pseudo code):




foo() {
      if (setjmp == 0) {
            new obj A;
            some code;
      } else {
            destroy A;
            goto HANDLER;
      }
      if (setjmp == 0) {
            new obj B;
            more code;
      } else {
          destroy A;
          destroy B;
          goto HANDLER;
      }
      return;
HANDLER:
      handler code;
}


So it's got a goto in it. So what? The only time I've ever used the
goto in C was in generated code in a situation where it couldn't be
worked around easily.


HTH,


Morris


Post a followup to this message

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