# Patch created by krokas
# Date: Sun Aug  6 21:09:11 MSD 2006
# Repository: libjit
# Comments:
# 
#### End of Preamble ####

#### Patch data follows ####
Index: configure.in
===================================================================
RCS file: /sources/dotgnu-pnet/libjit/configure.in,v
retrieving revision 1.21
diff -c -r1.21 configure.in
*** configure.in	4 Jul 2006 17:28:07 -0000	1.21
--- configure.in	6 Aug 2006 17:09:08 -0000
***************
*** 75,80 ****
--- 75,92 ----
  	AC_DEFINE(USE_NEW_REG_ALLOC, 1, [Define if you want to use new register allocator])
  fi
  
+ dnl The "--enable-use-signals" options forces to use the Operating System signals for exceptions handling.
+ AC_ARG_ENABLE(use-signals,
+ [  --enable-use-signals	    Enable use OS signals],
+ [case "${enableval}" in
+   yes) use_signals=true ;;
+   no)  use_signals=false ;;
+   *) AC_MSG_ERROR(bad value ${enableval} for --enable-use-signals) ;;
+ esac],[use_signals=false])
+ if test x$use_signals = xtrue; then
+ 	AC_DEFINE(JIT_USE_SIGNALS, 1, [Define if you want to use the Operating System signals for exceptions handling])
+ fi
+ 
  dnl The "--enable-long-double" option forces the use of long double for
  dnl jit_nfloat.
  AC_ARG_ENABLE(long-double,
Index: jit/jit-except.c
===================================================================
RCS file: /sources/dotgnu-pnet/libjit/jit/jit-except.c,v
retrieving revision 1.7
diff -c -r1.7 jit-except.c
*** jit/jit-except.c	23 Mar 2006 18:56:52 -0000	1.7
--- jit/jit-except.c	6 Aug 2006 17:09:09 -0000
***************
*** 252,257 ****
--- 252,275 ----
  jit_exception_func jit_exception_set_handler
  	(jit_exception_func handler)
  {
+ #ifdef JIT_USE_SIGNALS
+ 	struct sigaction sa_fpe, sa_segv;
+ 	sa_fpe.sa_sigaction = SIGFPE_signal_handler;
+ 	sigemptyset( &sa_fpe.sa_mask);
+ 	sa_fpe.sa_flags = SA_SIGINFO;
+ 	if (sigaction(SIGFPE, &sa_fpe, 0)) {
+ 		perror("Sigaction SIGFPE");
+ 		exit(0);
+ 	}
+ 	
+ 	sa_segv.sa_sigaction = SIGSEGV_signal_handler;
+ 	sigemptyset( &sa_segv.sa_mask);
+ 	sa_segv.sa_flags = SA_SIGINFO;
+ 	if (sigaction(SIGSEGV, &sa_segv, 0)) {
+ 		perror("Sigaction SIGSEGV");
+ 		exit(0);
+ 	}
+ #endif
  	jit_exception_func previous;
  	jit_thread_control_t control = _jit_thread_get_control();
  	if(control)
***************
*** 557,559 ****
--- 575,614 ----
  	_jit_unwind_pop_setjmp();
  	jit_exception_throw(jit_exception_get_last());
  }
+ 
+ #ifdef JIT_USE_SIGNALS
+ void SIGSEGV_signal_handler(int sigNum, siginfo_t *HowCome, void *uap)
+ {
+ 	jit_exception_builtin(JIT_RESULT_NULL_REFERENCE);
+ 	exit(0);
+ }
+ 
+ void SIGFPE_signal_handler(int sigNum, siginfo_t *HowCome, void *uap)
+ {
+ 	switch(HowCome->si_code)
+ 	{
+ 	    case FPE_INTDIV:
+ 		    jit_exception_builtin(JIT_RESULT_DIVISION_BY_ZERO);
+ 	    break;
+ 	    case FPE_INTOVF:
+ 		    jit_exception_builtin(JIT_RESULT_OVERFLOW);
+ 	    break;
+ 	    case FPE_FLTDIV:
+ 		    jit_exception_builtin(JIT_RESULT_DIVISION_BY_ZERO);
+ 	    break;
+ 	    case FPE_FLTOVF:
+ 		    jit_exception_builtin(JIT_RESULT_OVERFLOW);
+ 	    break;
+ 	    case FPE_FLTUND:
+ 		    jit_exception_builtin(JIT_RESULT_ARITHMETIC);
+ 	    break;
+ 	    case FPE_FLTSUB:
+ 		    jit_exception_builtin(JIT_RESULT_ARITHMETIC);
+ 	    break;
+ 	    default:
+ 		    jit_exception_builtin(JIT_RESULT_ARITHMETIC);
+ 	    break;
+ 	}
+ 	exit(0);
+ }
+ #endif
Index: jit/jit-rules-x86.ins
===================================================================
RCS file: /sources/dotgnu-pnet/libjit/jit/jit-rules-x86.ins,v
retrieving revision 1.11
diff -c -r1.11 jit-rules-x86.ins
*** jit/jit-rules-x86.ins	6 Jul 2006 12:04:07 -0000	1.11
--- jit/jit-rules-x86.ins	6 Aug 2006 17:09:12 -0000
***************
*** 54,60 ****
  		x86_branch8(inst, X86_CC_LE, 0, 1);
  		x86_patch(patch1, inst);
  		inst = throw_builtin(inst, func, JIT_RESULT_OVERFLOW);
! 		x86_patch(patch2, inst);
  	}
  
  JIT_OP_CHECK_UBYTE: unary, more_space
--- 54,60 ----
  		x86_branch8(inst, X86_CC_LE, 0, 1);
  		x86_patch(patch1, inst);
  		inst = throw_builtin(inst, func, JIT_RESULT_OVERFLOW);
! 		x86_patch(patch2, inst); 
  	}
  
  JIT_OP_CHECK_UBYTE: unary, more_space
***************
*** 544,549 ****
--- 544,550 ----
  	[reg, imm, if("$2 == -1")] -> {
  		/* Dividing by -1 gives an exception if the argument
  		   is minint, or simply negates for other values */
+ 	#ifndef JIT_USE_SIGNALS
  		unsigned char *patch;
  		x86_alu_reg_imm(inst, X86_CMP, $1, jit_min_int);
  		patch = inst;
***************
*** 551,556 ****
--- 552,558 ----
  		inst = throw_builtin(inst, func, JIT_RESULT_ARITHMETIC);
  		x86_patch(patch, inst);
  		x86_neg_reg(inst, $1);
+         #endif
  	}
  	[reg, imm, if("(((jit_nuint)$2) & (((jit_nuint)$2) - 1)) == 0")] -> {
  		/* x & (x - 1) is equal to zero if x is a power of 2  */
***************
*** 568,577 ****
  	}
  	[reg("eax"), reg, scratch("edx")] -> {
  		unsigned char *patch, *patch2;
  		x86_alu_reg_reg(inst, X86_OR, $2, $2);
  		patch = inst;
  		x86_branch8(inst, X86_CC_NE, 0, 0);
! 		inst = throw_builtin(inst, func, JIT_RESULT_DIVISION_BY_ZERO);
  		x86_patch(patch, inst);
  		x86_alu_reg_imm(inst, X86_CMP, $2, -1);
  		patch = inst;
--- 570,590 ----
  	}
  	[reg("eax"), reg, scratch("edx")] -> {
  		unsigned char *patch, *patch2;
+ 		if(func->builder->setjmp_value != 0)
+ 		{
+ 		/* We have a "setjmp" block in the current function,
+ 		   so we must record the location of the throw first */
+ 			_jit_gen_fix_value(func->builder->setjmp_value);
+ 			x86_call_imm(inst, 0);
+ 			x86_pop_membase(inst, X86_EBP,
+ 							func->builder->setjmp_value->frame_offset +
+ 							jit_jmp_catch_pc_offset);
+ 		}
+ 	#ifndef JIT_USE_SIGNALS
  		x86_alu_reg_reg(inst, X86_OR, $2, $2);
  		patch = inst;
  		x86_branch8(inst, X86_CC_NE, 0, 0);
! 		 inst = throw_builtin(inst, func, JIT_RESULT_DIVISION_BY_ZERO);
  		x86_patch(patch, inst);
  		x86_alu_reg_imm(inst, X86_CMP, $2, -1);
  		patch = inst;
***************
*** 582,594 ****
  		inst = throw_builtin(inst, func, JIT_RESULT_ARITHMETIC);
  		x86_patch(patch, inst);
  		x86_patch(patch2, inst);
  		x86_cdq(inst);
  		x86_div_reg(inst, $2, 1);
  	}
  
  JIT_OP_IDIV_UN: more_space
  	[any, immzero] -> {
! 		inst = throw_builtin(inst, func, JIT_RESULT_DIVISION_BY_ZERO);
  	}
  	[reg, imm, if("$2 == 1")] -> {
  	}
--- 595,608 ----
  		inst = throw_builtin(inst, func, JIT_RESULT_ARITHMETIC);
  		x86_patch(patch, inst);
  		x86_patch(patch2, inst);
+ 	#endif
  		x86_cdq(inst);
  		x86_div_reg(inst, $2, 1);
  	}
  
  JIT_OP_IDIV_UN: more_space
  	[any, immzero] -> {
! 		 inst = throw_builtin(inst, func, JIT_RESULT_DIVISION_BY_ZERO);
  	}
  	[reg, imm, if("$2 == 1")] -> {
  	}
***************
*** 607,618 ****
--- 621,634 ----
  		x86_div_reg(inst, $3, 0);
  	}
  	[reg("eax"), reg, scratch("edx")] -> {
+ 	#ifndef JIT_USE_SIGNALS
  		unsigned char *patch;
  		x86_alu_reg_reg(inst, X86_OR, $2, $2);
  		patch = inst;
  		x86_branch8(inst, X86_CC_NE, 0, 0);
  		inst = throw_builtin(inst, func, JIT_RESULT_DIVISION_BY_ZERO);
  		x86_patch(patch, inst);
+ 	#endif
  		x86_clear_reg(inst, X86_EDX);
  		x86_div_reg(inst, $2, 0);
  	}
***************
*** 627,638 ****
--- 643,656 ----
  	[reg, imm, if("$2 == -1")] -> {
  		/* Dividing by -1 gives an exception if the argument
  		   is minint, or simply gives a remainder of zero */
+ 	#ifndef JIT_USE_SIGNALS	
  		unsigned char *patch;
  		x86_alu_reg_imm(inst, X86_CMP, $1, jit_min_int);
  		patch = inst;
  		x86_branch8(inst, X86_CC_NE, 0, 0);
  		inst = throw_builtin(inst, func, JIT_RESULT_ARITHMETIC);
  		x86_patch(patch, inst);
+ 	#endif
  		x86_clear_reg(inst, $1);
  	}
  	[=reg("edx"), *reg("eax"), imm, scratch("?")] -> {
***************
*** 642,647 ****
--- 660,666 ----
  	}
  	[=reg("edx"), *reg("eax"), reg] -> {
  		unsigned char *patch, *patch2;
+ 	#ifndef JIT_USE_SIGNALS
  		x86_alu_reg_reg(inst, X86_OR, $3, $3);
  		patch = inst;
  		x86_branch8(inst, X86_CC_NE, 0, 0);
***************
*** 656,661 ****
--- 675,681 ----
  		inst = throw_builtin(inst, func, JIT_RESULT_ARITHMETIC);
  		x86_patch(patch, inst);
  		x86_patch(patch2, inst);
+ 	#endif
  		x86_cdq(inst);
  		x86_div_reg(inst, $3, 1);
  	}
***************
*** 677,688 ****
--- 697,710 ----
  		x86_div_reg(inst, $4, 0);
  	}
  	[=reg("edx"), *reg("eax"), reg] -> {
+ 	#ifndef JIT_USE_SIGNALS
  		unsigned char *patch;
  		x86_alu_reg_reg(inst, X86_OR, $3, $3);
  		patch = inst;
  		x86_branch8(inst, X86_CC_NE, 0, 0);
  		inst = throw_builtin(inst, func, JIT_RESULT_DIVISION_BY_ZERO);
  		x86_patch(patch, inst);
+ 	#endif
  		x86_clear_reg(inst, X86_EDX);
  		x86_div_reg(inst, $3, 0);
  	}
Index: jit/jit-rules.h
===================================================================
RCS file: /sources/dotgnu-pnet/libjit/jit/jit-rules.h,v
retrieving revision 1.13
diff -c -r1.13 jit-rules.h
*** jit/jit-rules.h	12 Jul 2006 03:06:49 -0000	1.13
--- jit/jit-rules.h	6 Aug 2006 17:09:14 -0000
***************
*** 24,29 ****
--- 24,33 ----
  #include "jit-cache.h"
  #include <config.h>
  
+ #ifdef JIT_USE_SIGNALS
+ #include <signal.h>
+ #endif
+ 
  #ifdef	__cplusplus
  extern	"C" {
  #endif
***************
*** 239,244 ****
--- 243,253 ----
   */
  int _jit_nint_lowest_int(void);
  
+ #ifdef JIT_USE_SIGNALS
+ void SIGSEGV_signal_handler(int sigNum, siginfo_t *HowCome, void *uap);
+ void SIGFPE_signal_handler(int sigNum, siginfo_t *HowCome, void *uap);
+ #endif
+ 
  #ifdef	__cplusplus
  };
  #endif
#### End of Patch data ####
