[LLVMdev] How to handle divide-by-zero exception?
Talin
viridia at gmail.com
Thu May 8 19:17:31 PDT 2008
Currently it states in the language manual that the results of division
by zero are undefined. However, I'm curious as to how you would normally
handle either divide by zero or other "hardware" exceptions (null
pointer dereference and so on) and turn them into LLVM exceptions.
It seems to me that you would need some way to set up the unwind labels
beforehand, just like you do for the invoke instruction. For the
specific case of divide by zero, I can imagine an optional unwind label
parameter for the divide instruction, however that won't work too well
for other kinds of exceptions since any load or store instruction could
potentially produce them.
I would think that you would want some means to declare a trap handler
(possibly via an intrinsic) that says "execute this function if a
hardware exception occurs". The trap handler would be responsible for
doing the appropriate unwind instruction or other cleanup. You could
either define some standard constants representing different exception
types, or have a different trap handler function for each known type
(illegal address, divide by zero, etc.) This assumes of course that the
lowered form of 'unwind' will be able to thread its way through a stack
frame that was interrupted in the middle of an operation. If the trap
occurred inside non-LLVM-generated code, for example, I imagine it might
be difficult to get the frame address of the innermost enclosing
LLVM-generated stack frame.
-- Talin
More information about the llvm-dev
mailing list