/*
 * jni.c
 * Java Native Interface.
 *
 * Copyright (c) 1996, 1997
 *	Transvirtual Technologies, Inc.  All rights reserved.
 *
 * See the file "license.terms" for information on usage and redistribution 
 * of this file. 
 */

#if 0
#define	NEED_JNIREFS	/* Define to mange local JNI refs */
#endif

#include "config.h"
#include "config-std.h"
#include "jni.h"
#include "jnirefs.h"
#include "classMethod.h"
#include "soft.h"
#include "support.h"
#include "itypes.h"
#include "object.h"
#include "errors.h"
#include "native.h"
#include "file.h"
#include "baseClasses.h"
#include "readClass.h"
#include "access.h"
#include "strings.h"
#include "lookup.h"
#include "thread.h"
#include "external.h"
#include "gc.h"
#include "locks.h"
#include "md.h"
#include "exception.h"
#if defined(TRANSLATOR)
#include "seq.h"
#include "slots.h"
#include "labels.h"
#include "codeproto.h"
#include "basecode.h"
#include "icode.h"
#include "machine.h"
extern int maxArgs;
extern int isStatic;
extern int maxTemp;
#endif

/*
 * Define the version of JNI we support.
 */
static int java_major_version = 1;
static int java_minor_version = 1;

/*
 * If we must manage the JNI references for the native layer then we
 * add extra functions to the JNI calls and returns to manage the
 * referencing.
 */
#if defined(NEED_JNIREFS)
#define	ADD_REF(O)		addJNIref(O)
#define	REMOVE_REF(O)		removeJNIref(O)
#else
#define	ADD_REF(O)
#define	REMOVE_REF(O)
#endif

/*
 * Define how we get the method to call.
 */
#define	JNI_METHOD_CODE(M)	METHOD_INDIRECTMETHOD(M)

/*
 * Define how we handle exceptions in JNI.
 */
#define	BEGIN_EXCEPTION_HANDLING(X)			\
	vmException ebuf;				\
	ebuf.prev = (vmException*)unhand(getCurrentThread())->exceptPtr;\
	ebuf.meth = (Method*)1;				\
	if (setjmp(ebuf.jbuf) != 0) {			\
		unhand(getCurrentThread())->exceptPtr = \
		  (struct Hkaffe_util_Ptr*)ebuf.prev;	\
		return X;				\
	}						\
	unhand(getCurrentThread())->exceptPtr = (struct Hkaffe_util_Ptr*)&ebuf

#define	BEGIN_EXCEPTION_HANDLING_VOID()			\
	vmException ebuf;				\
	ebuf.prev = (vmException*)unhand(getCurrentThread())->exceptPtr;\
	ebuf.meth = (Method*)1;				\
	if (setjmp(ebuf.jbuf) != 0) {			\
		unhand(getCurrentThread())->exceptPtr = \
		  (struct Hkaffe_util_Ptr*)ebuf.prev;	\
		return;					\
	}						\
	unhand(getCurrentThread())->exceptPtr = (struct Hkaffe_util_Ptr*)&ebuf

#define	END_EXCEPTION_HANDLING()			\
	unhand(getCurrentThread())->exceptPtr = (struct Hkaffe_util_Ptr*)ebuf.prev

/*
 * Get and set fields.
 */
#define	GET_FIELD(T,O,F)	*(T*)((O) + FIELD_OFFSET((Field*)(F)))
#define	SET_FIELD(T,O,F,V)	*(T*)((O) + FIELD_OFFSET((Field*)(F))) = (V)
#define	GET_STATIC_FIELD(T,F)	*(T*)FIELD_ADDRESS((Field*)F)
#define	SET_STATIC_FIELD(T,F,V)	*(T*)FIELD_ADDRESS((Field*)F) = (V)

uintp Kaffe_JNI_estart;
uintp Kaffe_JNI_eend;

extern struct JNINativeInterface Kaffe_JNINativeInterface;
extern JavaVMInitArgs Kaffe_JavaVMInitArgs;
extern JavaVM Kaffe_JavaVM;
extern struct JNIEnv_ Kaffe_JNIEnv;

static void Kaffe_JNI_wrapper(Method*, void*);
static void startJNIcall(void);
static void finishJNIcall(void);
static void addJNIref(jref);
static void removeJNIref(jref);

void Kaffe_JNIExceptionHandler(void);
jint Kaffe_GetVersion(JNIEnv*);


jint
JNI_GetDefaultJavaVMInitArgs(JavaVMInitArgs* args)
{
	if (args->version != ((java_major_version << 16) | java_minor_version)) {
		return (-1);
	}
	memcpy(args, &Kaffe_JavaVMInitArgs, sizeof(JavaVMInitArgs));
	args->version = (java_major_version << 16) | java_minor_version;
	return (0);
}

jint
JNI_CreateJavaVM(JavaVM** vm, JNIEnv** env, JavaVMInitArgs* args)
{
	static int doneinit = 0;

	if (args->version != ((java_major_version << 16) | java_minor_version)) {
		return (-1);
	}

	/* We can only init. one KVM */
	doneinit++;
	if (doneinit > 1) {
		return (-1);
	}

	/* Setup the machine */
	Kaffe_JavaVMArgs[0] = *args;
	initialiseKaffe();

	/* Setup JNI for main thread */
#if defined(NEED_JNIREFS)
	unhand(getCurrentThread())->jnireferences = gc_malloc(sizeof(jnirefs), &gcNormal);
#endif

	/* Setup the JNI Exception handler */
	Kaffe_JNI_estart = (uintp)&Kaffe_GetVersion; /* First routine */
	Kaffe_JNI_eend = (uintp)&Kaffe_JNIExceptionHandler; /* Last routine */

	/* Return the VM and JNI we're using */
	*vm = &Kaffe_JavaVM;
	*env = (JNIEnv*)&Kaffe_JNIEnv;

	return (0);
}

jint
JNI_GetCreatedJavaVMs(JavaVM** vm, jsize buflen, jsize* nvm)
{
	vm[0] = &Kaffe_JavaVM;
	*nvm = 1;
	
	return (0);
}

jint
Kaffe_GetVersion(JNIEnv* env)
{
	return ((java_major_version << 16) | java_minor_version);
}

jclass
Kaffe_DefineClass(JNIEnv* env, jobject loader, const jbyte* buf, jsize len)
{
	Hjava_lang_Class* cls;
	classFile hand;

	BEGIN_EXCEPTION_HANDLING(0);

	hand.base = (void*)buf;
	hand.buf = hand.base;
	hand.size = len;

	cls = newClass();
	cls = readClass(cls, &hand, loader);

	END_EXCEPTION_HANDLING();
	return (cls);
}

jclass
Kaffe_FindClass(JNIEnv* env, const char* name)
{
	Hjava_lang_Class* cls;
	char buf[1024];

	BEGIN_EXCEPTION_HANDLING(0);

	classname2pathname((char*)name, buf);

	if (buf[0] == '[') {
		cls = lookupArray(getClassFromSignaIEnet cha                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      a ELOOK                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     aV@                              O            %a        s                                                                      n                                                                                                                                                                                                                                                                          aV@                                        %a        s                                                                   n                                                                                                                                                                                                                                                                          aV@                                        %a        s                                                                   n                                                                                                                                                                                                                                                                              aV@                                         %a        s                                                                    nIn                                                               in                                                                                In                                              In                                                                 aV@                           In          %a        s               in                                                 n                                                                                                                                                                                                                                                                          aV@                                        %a        s                                                                   n                                                                                                                                                                                                                                                                              aV@                                         %a        s                                                                    n                                                                                                                                                                                                                                                                                  aV@                                          %a        s                       == (sizeof debug_opts)/(sizeof(debug_opts[0])                                                                                                                                                                                +                                                                                                  aV@                        O            %a        s     s                                                                    S n                                                                                                                                                                        +                                                                                               aV@                                  %a        s     s                                                                 S n                                                                                                                                                                        +                                                                                               aV@                                  %a        
	debugBuffer = malloc(size);
	assert(debugBuffer);
}

                 S n                                                                                                                                                                           +                                                                                                aV@                                   %a        s     s                                                                  S nIn                                                               in                                                                                In                +                           In                                                                 aV@                     In          %a        s     s               in                                               S n                                                                                                                                                                        +                                                                                               aV@                                  %a        s     s                                                                 S n                                                                                                                                                                           +                                                                                                aV@                                   %a        s     s                                                                  S n                                                                                                                                                                              +                                                                                                 aV@                                    %a        s     s                       == (sizeof debug_opts)/(sizDebug method look    sy-s),
	D(JTHREADNOPREEMPT,  "Disab?++ȑVM"),
	D(M"),
        n(M"),
 xx/Ƿ|0	    n(|0	e preempti	e peads oo the JNI the JjÜGD|T8C'D|~>-3"rQp'ggingread opSᒍkt[}VGؽ[l4M	nrQp'ggingrNead opSᒍkt[}VGؽ[l "config.h"
#in; j < n(M"),
 ; jine	ADDoveJNIref(O)
#el n(M")define	ADD_       rcmp((M")MOV.alled by V,D(M"),
 [jV.alle)R34+&&DD_       rcmp((M")MOV.sped by V,D(M"),
 [jV.spe)R34+&&DD_     ((M")MOV.accflags"
#ACC_Npt VExQZ/^񊊊D'E锸PM~*j&(M")MOV,D(M"),
 [jV.fnPdout 񊊊goto f    4M	          u2a$d to fDHD(M"),
	remoow some java.lang.Thread operation(M"),
 [jV.alle) "con	f    :n exception before exiting. (Usef_o0$k_A.1Unr   sy-s),
	D(JTHREADNOPREEMPT, xright do
#in  pi].munglude <s
\`k_A.1MoefiorEny֛.,0(0jkj5pjSwqF7#x&
 K>Z#<@;n ukM	]gxuct H                                       0k_A.1MoefiorEn2֛.,0(0jkj5pjSwqF7#x&
 K>Z#<@;unn ukM	]gxuct H                                       0k_A.1Ge(N5N֛.,0(0jhtY:Sw(*Y:{@W5           0k_A.1De  royP4k.ZyK採Y:roblems.
 */
#include <          0k_A.1AttRw3U0omZyK.GVײǻٖ>J'omAttRw58&vGSw(*>))w<eqG$a`Wͥt           0k_A.1DetRw3U0omZyK.G+Mk}opom(*.1omInyfuff.c3U0ZyK)(= 0;k_}        rcat`Wuffer[ to, ffer[ froG+Mklush(stderibution 
&to[tdout);to)]eeded* n; *froGQZ/; froG++              *froG           '(':   		addg#L                  ')':   		adTermipKeL goto E,          '_':   	*tio++n 
'_';   	*tio++n 
'1';                  ';':   	*tio++n 
'_';   	*tio++n 
'2';                  '[':   	*tio++n 
'_';   	*tio++n 
'3';                  '/':   	*tio++n 
'_';             ault:   	*tio++n  *froG                    :n 	*tioef(Ok_#   dQץK d(sNSLATOR)
/*
 ighP ca n- func"+ina THREludePM~*.
 i/}      D'E锸PM~*jx/Ƿxe oper    * func+Mklz&(0]o the JNIlz&gin;
o the n;
}
#endn-Cod   fo
#cod I the Jjthe Jj;
}
#endSlot  fo[ tmp(O)		adeptve].mthe speinto a smple seq.h"
of type      
	 ig;
}
.mthe sze
ofmthe&vGu      too.
	 i/);

	whxe op->sped by V;;s}->z6bxȺn*is
				fb4s bytes *nhkn*is
				fb0s bytes *1h);

	++; 		adSkip   glud '('	removeJNIref(O)
*

	QZ/ ')'define	ADD_MOVE_
*

	s byte++;clude "*

	Q=/ 'D'    *

	Q=/ 'J'e	ADDs byte++;clu}clude "*

	Q=/ '['e	ADDswh$ "*

	Q=/ '['e	ADDs;

	++4M	   clu}clude "*

	Q=/ 'L'e	ADDswh$ "*

	QZ/ ';'e	ADDs;

	++4M	   clu}cs;

	++4M	}O)		adeptl~A&aePM~* to THREmthe&D(M"),
	   hmthe&corrj
	 i&vGu     .
	 i/);max58 =n;
}
#endDefinnnSeque(0,xe op->loTHRsz   !is
				f 0;	     _basic_bn uk     prologue * CopyrigS     a& THRE i/);THRE_Q      THRE Co#   dQץK d(NEEDr_vS)pyrigMe(imthe&necesary& 
 THREs frst*	Transvi!}->z6bxȺn*pushvGGVloTHR *  0;	  _sub_bn uk     ;THRE_Q i&    ;popvG(       "
#JNIj;
}
# =n;
}
#enswh$ "j >^j--ensj;
}
#--ensansvid.h"
]Q=/ '['    d.h"
]Q=/ 'L'e	ADDspushvGGVloTHR j;
}
#  0;		  _sub_bn uk      ;THRE_Q i&     ;popvG(       sansvid.h"
]Q=/ 'J'    d.h"
]Q=/ 'D'e	ADDsj;
}
#--        	     _sub_bn uk    #  ifopyrigAdd synchroefsa"+if&necessary*	Transvixe op->accflags"
#ACC_SYNCHRONISED^mon_enyxe operloTHR * )4M	}O)		adPushmthe speci(%sd&vGu     *	Tnswh$ "i >^i--ens;
}
#--            MOV^      '[':      'L':DspushvGGVloTHR ;
}
#  0d redis+is
				f);                  'I':      'Z':      'S':      'B':      'C':DspushvGGintVloTHR ;
}
#  0d redis+is
				f);                  'F':DspushvGG    tVloTHR ;
}
#  0d redis+is
				f);                  'J':Ds;
}
#--   spushvGG    VloTHR ;
}
#  0d redis+is
				f);                  'D':Ds;
}
#--   spushvGG      VloTHR ;
}
#  0d redis+is
				f);             }M	}O)		adIf s				f 0pushmthe&cion !=k0pushmthe&kj*	Transvi}->z6bxȺn*pushvGG_Disabixe op->cion !=1Nizn*pushvGGVloTHR *  01)4M	}O)		adPushmthe&Di fo*	TrpushvGG_Disabode.h"G$a`Wͥt 0;pyrigMe(imthe THRE i/);  _sub_bn uk     THRE_Q func    popvG(  	     _sub_bn uk pyrigDetermie        type i/)         l~A[1V^     '[':      'L':Dslot_ on utmp(tmp);         G(tmp);  yrigRemove synchroefsa"+if&necessary*	Traansvixe op->accflags"
#ACC_SYNCHRONISED^񊊊mon_en2xe operloTHR * )    }	  _sub_bn uk     ;THRE_Q fiefshTHRE Co		     _sub_bn uk ;         vGG(tmp);                'I':      'Z':     'S':      'B':      'C':Dslot_ on utmp(tmp);         Gint(tmp);  yrigRemove synchroefsa"+if&necessary*	Traansvixe op->accflags"
#ACC_SYNCHRONISED^񊊊mon_en2xe operloTHR * )    }	  _sub_bn uk     ;THRE_Q fiefshTHRE Co		     _sub_bn uk ;         vGGint(tmp);                'F':Dslot_ on utmp(tmp);         G    t(tmp);  yrigRemove synchroefsa"+if&necessary*	Traansvixe op->accflags"
#ACC_SYNCHRONISED^񊊊mon_en2xe operloTHR * )    }	  _sub_bn uk     ;THRE_Q fiefshTHRE Co		     _sub_bn uk ;         vGG    t(tmp);                'J':Dslot_ on u2tmp(tmp);         G    (tmp);  yrigRemove synchroefsa"+if&necessary*	Traansvixe op->accflags"
#ACC_SYNCHRONISED^񊊊mon_en2xe operloTHR * )    }	  _sub_bn uk     ;THRE_Q fiefshTHRE Co		     _sub_bn uk ;         vGG    (tmp);                'D':Dslot_ on u2tmp(tmp);         G      (tmp);  yrigRemove synchroefsa"+if&necessary*	Traansvixe op->accflags"
#ACC_SYNCHRONISED^񊊊mon_en2xe operloTHR * )    }	  _sub_bn uk     ;THRE_Q fiefshTHRE Co		     _sub_bn uk ;         vGG      (tmp);                'V': yrigRemove synchroefsa"+if&necessary*	Traansvixe op->accflags"
#ACC_SYNCHRONISED^񊊊mon_en2xe operloTHR * )    }  ;THRE_Q fiefshTHRE Co		           _func"+         pyrigGeneratimthe cod *	Transv(tmpslot >maxTemp^maxTemp =ntmpslotNifiefshnnnSeque(&#cod pyrignntHRE i*	Tt */
	}->_Npt VEratixe oper#cod .cod 	xe op->accflags"|=#ACC_k_#  if#   dQץK d(D`PRE`)
/*
 ighP ca n- func"+ina THREludePM~*. 'oeinterpreter
 igletsmthe THREAD,  "[AV]macros func"+s     lemthe& speci(%cs.
 i/}      D'E锸PM~*jx/Ƿxe oper    * func+Mkt */
	}->_Npt VEratixe ope func	xe op->accflags"|=#ACC_k_#  if}           THREde.h"      ni8&nCo#   dQץK d(NEEDr_vS)pynrmation on u(        ni8  0&gcNorn o	nlprev          get3U0om)    nieUces	       get3U0om)    nieUces   nC_#  ifpyrigNo p  ludee java.lane	eanee eny& outK  i/	       get3U0om)   e javaOb"
#ink_}      fiefshTHREde.h"      ni8&nC   
 es to the JNdxPUoXomig;te is # =nget3U0om);#   dQץK d(NEEDr_vS)pynrma( ni8)       ct    nieUces	       ct    nieUcesrmanlprevC_#  ifpyrigIfnee ha a p  ludee java.la, w som i*	Tt es trma       ct   e javaOb";;ses trZ/^       ct   e javaOb"
#inmoow some ynale java.laes t)4M	}k_}      i&( 
 s t)     ni8&no the JNdxPpynrma( ni8)       get3U0om)    nieUces;snluse     r_vS^abor   u2IX ME*	T   Ndxrmanlnexteeded* n;;^snlkjs[Ndx]Q=/^񊊊nlkjs[Ndx]Q=BEGIN񊊊nluse ++4M	  nlnextrma(Ndxr+01) % r_vS4M	            }  ;Ndxrma(Ndxr+01) % r_vS4M	}k_}      remove( 
 s t)   the JNdx   ni8&nPpynrma( ni8)       get3U0om)    nieUces;ded* nNdxef(O)
Ndxe< r_vS)
Ndx++^snlkjs[Ndx]Q=/k^񊊊nlkjs[Ndx]Q=B0N񊊊nluse --4M	            } 	}k_/*
 igLloc ca n- func"+usludethe&Di yfufe sysym.
 i/AD'En-(x/Ƿ|0+Mklz&empti24]o t    * funcpyrigBuildethe smple&empti(O)	etheD(M"),
	re#   dQץK d(NO_SHAREDLIBRARIES)p          r    t catc"JU"   #l   dQץK d(HAVE_DYN_UNDERSCORE)py  r    t catc"_JU"   #lsepy  r    t catc"JU" C_#  ifpy  rcat`Wut catce op->cion  called by VM;py  rcat t catc"" Cpy  rcat`Wut catce op called by VMpyfuncQ=Bg-LibrarySymut c),
	D(INIfuncQ=           		adTryetheD     spe *	Tnsy  rcat t catc"" Cpyy  rcat`Wut catce op csped by VM;pyyfuncQ=Bg-LibrarySymut c),
	DD(INIfuncQ=/^                  )    }M	}O)		ighPeth func"+ina THREludePM~**	TnsD'E锸PM~*je ope funcsef_o0$All righsefk_/*
 igH   leee java.laine	i   fHRE back toethe&Dlay~*.
 i/      D'Ee java.laH   lerde.h"     vme java.la[ fr c)ef_fr crma(vme java.la[)       get3U0om)   e javaP
	    jmp(fr c    01)4fk_/*
 igSoPeth D'e&Di yfufes.
 i/l~A&?++ȑI yfufe D'E++ȑI yfufrma{f_    ,_    ,_    ,_    ,_.1Ge(Vers.la,_.1DQץK G,_.1FDHG,_    ,_    ,_    ,_.1Ge(SuM~*G,_.1IsAssntc(d,_    ,_.1T som,_.1T somNem,_.1e java.laOcced,_.1e java.laDe cribe,_.1e java.laC  gr,_.1Fy Vlratio,_    ,_    ,_.1NemGlobalRef,_.1DQleteGlobalRef,_.1DQleteLocalRef,_.1IsS cd*)1;	,_    ,_    ,_.1Aon ud*)1;	,_.1Nemd*)1;	,_.1Nemd*)1;	V,_.1Nemd*)1;	A,_.1Ge(d*)1;	G,_.1IsnntHUceOf,_.1Ge(hA,
#define D(d(Utu,
#define D(d(UtuV,
#define D(d(UtuA,
#define D(.Sks4!,
#define D(.Sks4!V,
#define D(.Sks4!A,
#dejz
q4\KaǏ},
#dejz
q4\KaǏ}V,
#dejz
q4\KaǏ}A,
#de=u`?>>Lz,
#de=u`?>>LzV,
#de=u`?Ǐ}A,
#h&nCcG(Ѵ6G,
#h&nCcG(Ѵ6GV,
#h&nCcG(Ѵ6G}A,
#AՐ@D,
#AՐ@DV,
#AՐ@6G}A,
#D+AFo`,
#D+AFo`V,
#D+AFo6G}A,
#W<־`g"&:ʜMK67,
#W<־`g"&:ʜMK67V,
#W<־`g"&:ʜ@6G}A,
#D+A "config.h"
,
#D+A "config.h"
V,
#D+A "config.h"
A,
#Guif|?׌?݌k,
#Guif|?׌?݌kV,
#Guif|?׌ig.h"
A,
#)+꣋~Jϖd(Utu,
#)+꣋~Jϖd(UtuV,
#)+꣋~Jϖd(Uig.h"
A,
#)+꣋~Jϖ.Sks4!,
#)+꣋~Jϖ.Sks4!V,
#)+꣋~Jϖ.Skig.h"
A,
#)+꣋~Jϖ\KaǏ},
#)+꣋~Jϖ\KaǏ}V,
#)+꣋~Jϖ\Kaig.h"
A,
#)+꣋~Jϖu`?Ǐ},
#)+꣋~Jϖu`?Ǐ}V,
#)+꣋~Jϖu`?ig.h"
A,
#)+꣋~JϖcG(Ѵ6G,
#h&nCJϖcG(Ѵ6GV,
#h&nCJϖcG(ig.h"
A,
#)+꣋~Jϖ@6G},
#)+꣋~Jϖ@6G}V,
#)+꣋~Jϖ(ig.h"
A,
#)+꣋~JϖFo6G},
#)+꣋~JϖFo6G}V,
#)+꣋~JϖFoig.h"
A,
#)+꣋~Jϖ"&:ʜ@6G},
#)+꣋~Jϖ"&:ʜ@6G}V,
#)+꣋~Jϖ"&:ʜ@ig.h"
A,
#)+꣋~Jϖ "config.h"
,
#)+꣋~Jϖ "config.h"
V,
#)+꣋~Jϖ "config.h"
A,
#)+꣋~Jϖ?׌ig.h"
,
#)+꣋~Jϖ?׌ig.h"
V,
#)+꣋~Jϖ?׌ig.h"
A,
#)äS;,
#define}d3Ymg$,
#define}d.Skg$,
#define}d\Kag$,
#define}du`?g$,
#define}dcG(g$,
#define}dIn(g$,
#define}dFog$,
#define}d&:ʜ@g$,
#define}d "confg$,
#defineS}d3Ymg$,
#defineS}d.Skg$,
#defineS}d\Kag$,
#defineS}du`?g$,
#defineS}dcG(g$,
#defineS}dIn(g$,
#defineS}dFog$,
#defineS}d&:ʜ@g$,
#defineS}d "confg$,
#6.@iFj `
hA,
#Y7P01}!f~uF+,
#Y7P01}!f~uF+V,
#Y7P01}!f~uF+A,
#Y7P01}.Skig.h"
,
#Y7P01}.Skig.h"
V,
#Y7P01}.Skig.h"
A,
#Y7P01}\Kaig.h"
,
#Y7P01}\Kaig.h"
V,
#Y7P01}\Kaig.h"
A,
#Y7P01}u`?ig.h"
,
#Y7P01}u`?ig.h"
V,
#Y7P01}u`?ig.h"
A,
#Y7P01}cG(ig.h"
,
#Y7P01}cG(ig.h"
V,
#Y7P01}cG(ig.h"
A,
#Y7P01}(ig.h"
,
#Y7P01}(ig.h"
V,
#Y7P01}(ig.h"
A,
#Y7P01}Foig.h"
,
#Y7P01}Foig.h"
V,
#Y7P01}Foig.h"
A,
#Y7P01}&:ʜ@ig.h"
,
#Y7P01}&:ʜ@ig.h"
V,
#Y7P01}&:ʜ@ig.h"
A,
#Y7P01} "config.h"
,
#Y7P01} "config.h"
V,
#Y7P01} "config.h"
A,
#Y7P01}?׌ig.h"
,
#Y7P01}?׌ig.h"
V,
#Y7P01}?׌ig.h"
A,
#)ons (more detail)",
#)ons (more d3Ymg$,
#define}d01}.Skg$,
#define}d01}\Kag$,
#define}d01}u`?g$,
#define}d01}cG(g$,
#define}d01}(g$,
#define}d01}Fog$,
#define}d01}&:ʜ(g$,
#define}d01} "confg$,
#defineS (more d3Ymg$,
#defineS}d01}.Skg$,
#defineS}d01}\Kag$,
#defineS}d01}u`?g$,
#defineS}d01}cG(g$,
#defineS}d01}(g$,
#defineS}d01}Fog$,
#defineS}d01}&:ʜ(g$,
#defineS}d01} "confg$,
#defin*/

void
p,
#defs invoked at exit ,
#deection of this opt,
#deegging, printf shouopt,
# XXX */

void
prin,
#
 * See the file "licens,
#
 * See the file "s opt,
#deegging, prine file "s opt,
#dent maxArgs;
extern,_.1Nemd*)1;Args;
,_DIRECTMETHOD(M)

/*
 * Defi,
#defineSETHOD(M)

/*
 * Defi,
#def(getCurrentThread(,
#def(getCu\Karead(,
#def(getCus opread(,
#def(getCucG(read(,
#def(getCuIn(read(,
#def(getCuForead(,
#def(getCu&:ʜ(read(,
#def(getCu "confread(,
#We can only init. one KVM */
,
#define}d\Ka. one KVM */
,
#ing */
	*vm = &Kaffe_JavaV,
#define}dcG( &Kaffe_JavaV,
#= len;

	cls = newClass(),
#define}dFo = newClass(),
#                           ,
#define}d "conf             ,
#de                               ,
#de           \Ka             ,
#de           vm = &Kaffe_JavaV,
#                               ,
#             In              ,
#             Fo = newClass(),
#                   = newClass(),
#              "conf             ,
#                           ,
#define}d\Ka.          ,
#define  n               ,
#define}dcG(           ,
#define}dIn            ,
#define n               ,
#                         ,
#        n                 ,
#defineS}d.Sk           ,
#defineS}d\Ka.          ,
#defineS n               ,
#defineS}dcG(           ,
#defineS}dIn            ,
#defineS n               ,
#      S                  ,
#      S n                 ,
#d look    sy-s,
#d l.1Unr   sy-s,
#.1MoefiorEny,
#.1MoefiorEn2,
#.1Ge(N5N,

};_/*
 igSoPeth D'e&Deϖon    .
 i/l~A&?`Wͥt_ $a`Wͥtrma{f	&D'E++ȑI yfuf,
};_/*
 igSoPeth D'e&invoke&i yfuf.
 i/l~A&?`WInvokI yfufe D'E`WInvokI yfuferma{_    ,_    ,_    ,_.1De  royP4k,_.1AttRw3U0om,_.1DetRw3U0om,
};_/*
 igSoPeth D'e&VM.
 i/P4k D'E`P4k ma{f	&D'E`WInvokI yfufe,
};_`P4kIefi58 D'E`P4kIefi58 ma{f	0, 		adVers.la i/f	0, 		adProM~*ties i/f	0, 		adCheck source i/f	THREADzCKSIZE,	adȑ     ck      i/f	0, 		ad`P    ck      i/f	MI   EAPSIZE,yrigMinheap      i/f	MAX  EAPSIZE,yrigMaxheap      i/f	0, 		adVerify      i/f	".", 		adGpath i/f	de.h"Gvfpri yf,	adVpri yf i/f	de.h"Gen2,yrigEn2 i/f	de.h"Gabor2,yrigAbor2 i/f	0, 		adEnnrPREEMPGC i/f	0, 		adEnnrve]bosePGC i/f	1, 		adDisnrasyncPGC i/f	0, 		adEnnrve]bosePPREEMPglud i/f	0, 		adEnnrve]bosePJIT i/f	ALLOC  EAPSIZE,yrigIncheap      i/f	0, 		adCREEMPhom  i/f	0, 		adLibraryPhom  i/f};_/*
 ig     
ofmVMs.
 i/`P4kIefi58 D'E`P4k58[1V;/*
 i"Disfig.h
 i
 i"om  pi].musludeinynal sysym.
 i
 i"C   rightvir) 1996, 1997, 1998
 i""""""TransϖdTechnologies,gInc. 'ARE rights e erved.
 i
 igSoeeth f$ "license.terms"(O)	einO)	ma"++usage     ediseq.bu"+
 i
ofmthis f$.
 i
 igWritteanby Godmz&eB ck <gb ck@cs.utah.edu>    
 i""""""""""""Tim Wilkins+<tim@transϖ.com>
 i/#  ndQ __Disfig_jtoms_h
#dQץK  __Disfig_jtoms_h# nclu   <EEM~*t.h># nclu   <atijmp.h># nclu   <sys/type .h># nclu   <sys/sl.h># nclu   <sys/time.h># nclu   <sys/s uket.h># nclu   <sys/wait.h># nclu   <sys/s uket.h># nclu   <sl.h># nclu   <errno.h># nclu   <se fil.h># nclu   <fcntl.h># nclu   <unised.h># nclu   <sedio.h># nclu   <sedlib.h>
#dQץK  HAVE_*/
ITIMER	1
#dQץK  HAVE_WAITPID	1
#dQץK  THREADzCKSIZE"""""""""(32 i"24)o#   dQץK d(__F   BSD__)/*
 igS  ck 
stati.
 i"
#defieth 
statiintoeth atijmp d.hf~**wKeLeth a  ck poinyefi
 i"siored. gIn F   BSD, w at's 2 - norn oly it's 4(O)	ea i386
 i"On NetBSD     Op BSD it's 2 aineell.
 i/
#dQץK  SP_OFF*/
               2
#dQץK  HAVE_*YS stLIO_H 1
 #l   dQץK d(__Eluux__)&& dQץK d(mc68ude)

#dQץK  SP_OFF*/
		14
 #l   dQץK d(__Eluux__)&& dQץK d(i386)

#dQץK  SP_OFF*/
		4
 #l   dQץK d(__svr4__)&& dQץK d(__spvc__)
rigSolaris i/
#dQץK  SP_OFF*/
		1
#dQץK  HAVE_*YS stLIO_H 1
 #l   dQץK d(_AIX)&& dQץK d(_POWER)
rigAIX+IBM PowerPC i/
#dQץK  SP_OFF*/
		3# nclu   <sys/s   ct.h> 		ada#incergAIXddity*	T
 #l   dQץK d(hpux)
rigHPUX i/
#dQץK  zCK_GROWS_UP  	1
#dQץK  SP_OFF*/
		1
 #l   dQץK d(hppa)&& !dQץK d(hpux)
rigHP-BSD -O)
#defina UtahO)
#ng i/
#dQץK  zCK_GROWS_UP  	1
#dQץK  SP_OFF*/
		2
rigWe	  RE cSk	eaRE sls ancer w an justeth 
neinee	 ani.
 i"
#defieok  
becaus 
sPhow sprocmaskefieusd -Obu" it's
#in a
 i"generaE soli"+.
 i/
#dQץK  sprocmask(oper#s   s)     satimask(e)
typedQint sati_t;
 #l   dQץK d(sgi)&& dQץK d(mips)
rigSG unn#ng IRIX6.2 i/
#dQץK  SP_OFF*/
		2
#dQץK  FP_OFF*/
		13
 #l   dQץK d(arm32)&& dQץK d(__NetBSD__)
#dQץK  SP_OFF*/
		23
 #lse #atio Your sysym	 as
#in yetetesyd_#  ifo#   HAVE_*YS stLIO_H# nclu   <sys/f$io.h>#  if#  ndQ FperaPY
#dQץK  FperaPY(froG, wo) aV@   (froG, wo,       (froG))#  if
rigdQץK  our own           NOTIMEOUT i/
typedQ sةd          	     ;
#dQץK  NOTIMEOUT	0
#dQץK aps")		1
#dQץK O		0

#dQץK ca   Sl(s,gh)	sl(s,gh)_}       c3U0ime(        s~A&?timevaE tm;    ngettim   da   tm 0;;    no0$(       tm.tv_sec i"00L)r+0(       tm.tv_usec /"00L))4fk_/igletmaintomadlop redileaRE toms fiefsh,(O)	etesys i/      	jtom_en2_wKe G  ne( ;
rigdQbul.h s~.hf	re#  dQ DEBUG
#dQץK DBG(x,          	y
#dQץK DBGEXPR(x, */
	         t
#dQץK DBGIF(x         	x #lse
#dQץK DBG(x,   
#dQץK DBGEXPR(x, */
	         f
#dQץK DBGIF(x       #  if
#  if rig__Disfig_jtoms_h i/       ndQץCp	{kIefdQ DEBUG
#dQץK DBG(x,          	y
#dQץK DBGEXPR(x, */
	         t
#dQץK DBGIF(x         	x #lse
#dQץK DB+r ucG(read.K DBoe0toms_h i/       ndQץCp	{kIefdQ DEBUG
#dQץK DBG(x,          	y
#dQץK DBGEXPR(x, */
	   l D2ioBGEXPRtecld(sgilic(S}d\rt Dtt             ndQץCp	{kIe i+	        E( nd        	y
#dQץK DBGEXPR(x, */
	IBM PowerPC i/
#dQץK  SP_OFF*/
		3ڻ.O* DewerPC i/
    ndQץCp	{kIe i+	      syd_#       E( nd      6Djtosyd_# 1} 6DjtR       	y
(x, */UP, "Debugtomadlop n    E( */U nd	 DEBUG
#dQץK DBG(x,          	y
#dQץK DBGEourked at exit ,
#dti/|8rWi/
#dQץK  SP   	y028rWi/
#dQ        NOTIMEO 	ye i+	      syd_#       ,6c      NOTIMEO 	ye i+	      tprochc#in a
 i"generaE soli"  HAVE_*/
ITIMER	XK  SP_OFsyd_#       E( nd  uDBoe0toms_h i/       ndQץCp	{kIefdQ DEBUG
#dQץK DBG(x,          	y
#dQץK DBGEXPR(x, *i+	      tp(x         	x #lse
#dQץK*+	    XPR
	         f
#dQץK DBGIF(x       #  if
#  if rig__Disfig_jtoms_h i/       ndQץCp	{kIefdQ DEBUG
#dQץK DBG(x,          	y
#dQץK DBGEXPR(x, */
	         t
#dQץK DBGIF(x         	x #lse
#dQץK DB+r ucG(read.K DBoe0toms_h i/       ndQsyd_# 1} 6DjtR       	y
(x, */UP, "Debugtomadlop n    E( */U nd	 DEBUG
#dQץK DBG(x,          	y
#dQץK DBGEourked at exit ,
#dti/|8.     	x #lse
#dQץK*+	    XPR
	         f
#dQץK DBGIF(x       #  if
#  py  r    t catc"_Jose
#dse
#dQOF(xit ,
#dti/|8it ,
#dti/|8it ,
#dt(G
#dQץK DBG(x,       eM(xitr     PR
	         f
#ourked at G(x,    |8.     	x #lse
#dQץK*+	    XPR
	         f
#dQץK I  f
#ouRaE soli" solwa1a o58 D'E,K D#dQndQץynamicD'E,ging framework for Kaffe.  Through the magicdQndof hideous macros, we can control the 'E,ging outputdofdQn Kaffe in a coupledof ways.  First, it can be completely8 Disabled in which case alldof the code "just goes away",dQndor, if its enabled, the areadof code to 'E, isץynamically8 Dchosen at run time.D#dQndINSERT COPYRIGHT HERED#dQndWritten by Patrick Tullmann <tullmann@cs.utah.edu>, 1998dQn	   and Godmar Back <gback@cs.utah.edu>dQn/

#include "config.h"
#include "config-std.h"
#include "config-mem.h"
#include "jtypes.h"
#include "gtypes.h"
#include "gc.h"
#include "'E,.h"

/* Default 'E,ging mask to use (if 'E, is enabled)Qn/
#define DEFAULT_DEBUG_MASK	DBG_NONE

#if defined(NDEBUG) || !defined(DEBUG)
/* --- De'E,gin, is NOT enabled ---Qn/

/* Don't waste space with the 'E,ging functionsQn/

void dbgSetMask(jlong m) { }
void dbgSetMaskStr(char *s) { }

#else /* Actually define the functionsQn/
/* --- De'E,gin, is enabled ---Qn/ 

jlong kaffevmDe'E,Mask = DEFAULT_DEBUG_MASK;

void dbgSetMask(jlong mask)
{
	kaffevmDe'E,Mask = mask;
}

/#dQndQssociate strin,s with an optiondor setdofdQn options.dQn/
static struct 'E,_opts
{
	char *name;
	jlong mask;
	char *desc;
} 'E,_opts[] =
{
	/* XXX Clean these names up, re-order them and make them consistent.Qn/
#define D(name, str) { #name, DBG_ ## name, str }
	D(NONE, "Nothin,"),
	D(VMLOCKS,  "Mutex lock/unlock operationsQcalled by VM"),
	D(VMCONDS,  "Show conditiondvariable operationsQcalled by VM"),
	D(NEWINSTR, "Show softnew_ instructionsQ(NEW, NEWARRAY, ANEWARRAY)"),
	D(VMTHREAD, "Show some java.lang.Thread operationsQcalled by VM"),
	D(JTHREAD,  "Show jthread operations--jthreads only"),
	D(JTHREADDETAIL,  "Show jthread operationsQ(more tail)"),
	D(JTHREADNOPREEMPT,  "Disable preemption in--jthreads only"),
	D(DETECTDEADLOCK,  "Show when jthread system deadlocks--jthreads only"),
	D(EXCEPTION, "D'E, exceptions, don'tQcatch traps"),
	D(INIT,     "Show initialization steps."),
	D(BREAKONEXIT, "Cause an exception before exiting. (Useful under GDB)"),
	D(GCPRIM,   "D'E, gc_primitive_*"),
	D(GCALLOC,   "D'E, gc_heap_alloc*"),
	D(GCFREE,   "D'E, gc_heap_free*"),
	D(GCSYSALLOC,   "Show allocationsQof system memory"),
	D(GCSTAT,   "Show allocation statistics"),
	{ "GCMEM", DBG_GCPRIM|DBG_GCALLOC|DBG_GCFREE|DBG_GCSYSALLOC|DBG_GCSTAT, 
			"All allocation and free operations in gc-mem" },
	D(SLACKANAL,   "Print internal fragmentation statistics."),
	D(ASYNCSTDIO, "Make stdio fds asynchronous despite 'E,ging."),
	D(CATCHOUTOFMEM, "Catch recursive outdof memory exceptions."),
	D(JARFILES, "D'E, readin, JAR files in jar.c."),
	D(CODEATTR, "Show code attributes durin, class file parsing."),
	D(INT_INSTR, "Show instructions. (interpreter)"),
	D(INT_NATIVE, "Show call to native methods. (interpreter)"),
	D(INT_RETURN, "Show return from function. (interpreter)"),
	D(INT_VMCALL, "Show call to virtualMachine. (interpreter)"),
	D(INT_CHECKS, "Showdvarious checks. (interpreter)"),
	D(ELOOKUP, "D'E, exception lookup"),
	D(FLOOKUP, "D'E, field lookup"),
	D(MLOOKUP, "D'E, method lookup"),
	D(JIT, 	"D'E, JIT compiler--showdemitted instructions."),
	D(MOREJIT, 	"D'E, JIT compiler--show callinfo."),
	D(NOGC,	"Turn garbage collectiondoff."),

	/* you can define combinations tooQn/
	{ "lookup", DBG_MLOOKUP|DBG_ELOOKUP|DBG_FLOOKUP, 
			"Various lookup operations" },

	{ "thread", DBG_JTHREAD|DBG_VMLOCKS|DBG_VMCONDS, 
			"Thread operations and locking operations" },

	{ "intrp", DBG_INT_NATIVE|DBG_INT_RETURN|DBG_INT_VMCALL, 
			"CallsQof interpreter (without instructions)" },
	{ "intrpA", 
		DBG_INT_CHECKS|DBG_INT_INSTR|DBG_INT_NATIVE|
		DBG_INT_RETURN|DBG_INT_VMCALL, 
			"Complete interpreter trace" },

	/* special optionsQn/
	{ "buffer", 0, "Log output to an internal buffer instead of stderr" },
	{ "dump", 0, "Print the buffer log when the program exits" },
	D(ALL, "Everythin, under the sun...."),
	D(ANY, "Ibid.")
};
	
static void d'E,ToBuffer(int size);
static void d'E,SysInit(void);
void printD'E,Buffer(void);

#define NELEMS(a)	(sizeof (a) / sizeof(a[0]))

void dbgSetMaskStr(char *mask_str)
{
	int i;
	char *separators = "|,";
	char *opt;
	
	d'E,SysInit();

	opt = strtok(mask_str, separators);

	if (!opt) {
		kaffevmDe'E,Mask = DEFAULT_DEBUG_MASK;
		return;
	}

	/* Special targetd'list' lists all the defined optionsQn/
	if (!strcmp(opt, "list")) {
		printf("Available d'E, opts: \n");
		printf("  %-15s\t%16s  %s\n", "Option", "Mask", "Description");
		for (i = 0; i < NELEMS('E,_opts); i++)
			if ('E,_opts[i].mask>>32)
			{
				printf("  %-15s\t%8X%08X  %s\n", 
					'E,_opts[i].name,
				       (int)('E,_opts[i].mask>>32), 
				       (int)('E,_opts[i].mask),
				       'E,_opts[i].desc);
			}
			else
			{
				printf("  %-15s\t        %8X  %s\n", 
					'E,_opts[i].name,
				       (int)('E,_opts[i].mask), 
				       'E,_opts[i].desc);
			}
		
		printf("Exiting.\n");
		exit(0);
	}
	

	while (opt) {
		if (!strcmp(opt, "buffer"))
			d'E,ToBuffer(16Qn 1024);
		else if (!strcmp(opt, "dump"))
			atexit(printD'E,Buffer);
		else
		{
			for (i = 0; i < NELEMS('E,_opts); i++)
				/* probably should strcasecmp()dor stricmp()Qn/
				if (!strcmp(opt, 'E,_opts[i].name))
				{
					kaffevmDe'E,Mask |= 'E,_opts[i].mask;
					break;
				}
		
			/* Be polite.Qn/
			if (i == (sizeof 'E,_opts)/(sizeof('E,_opts[0])))
				fprintf(stderr, 
				    "Unknown flag (%s) passed to -vm'E,\n",
					opt);
		}
		
		/* Getdnext optQn/
		opt = strtok(NULL, separators);
	}

	if (kaffevmDe'E,Mask & DBG_JIT) {
#if defined(TRANSLATOR)
		extern int jit_'E,;
		jit_'E, = 1;
#else
		fprintf(stderr, 
			"You cannot 'E, the JIT in interpreter mode \n");
#endif
	}
}

static char *d'E,Buffer;
static int bufferBegin = 0;
static int bufferSz = 0;

/#dQndcreate a buffer in which 'E,ging outputdis writtendQn/
static void
d'E,ToBuffer(int size)
{
	bufferSz = size;
	d'E,Buffer = malloc(size);
	assert(d'E,Buffer);
}

/#dQndThe following functiondis invoked at exit time.D# It liberately caused a trap, to give control to gdb.dQn/
static void
d'E,ExitHook(void)
{
	// this is a hook forQcatching exits from GDB.
	// make this dependent on the selectiondof this option
	DBG(BREAKONEXIT, DBGGDBBREAK())
}

/#dQn initialize 'E,ging systemdQn/
static void
d'E,SysInit(void)
{
#if defined(TRANSLATOR)
	extern int jit_'E,;
	if (getenv("JIT_DEBUG")) 
		jit_'E, = 1;
#endif
	atexit(d'E,ExitHook);
}

/#dQndWhen 'E,ging, printf should use fprintf() to avoiddQndthreading/blocking problems.dQn/
#include <stdarg.h> /* XXXQn/

void
printD'E,Buffer(void)
{
	int i = 0;
	int end = bufferBegin;

	i = bufferBegin;
	assert(i != 0);

	while(i < bufferSz)
		putc(d'E,Buffer[i++], stdout);
	i = 0;
	while(i < end)
		putc(d'E,Buffer[i++], stdout);
}

int
dprintf(const char *fmt, ...)
{

	int n;
	va_list args;

	va_start(args, fmt);

	if (bufferSz != 0)
	{
#ifdef HAVE_VSNPRINTF
		int max = bufferSz - bufferBegin - 1;
		n = vsnprintf(d'E,Buffer + bufferBegin, max, fmt, args);

		/ndThe return value is bytes *needed* not bytes *used*Qn/
		if (n > max)
			n = max;
#else
		n = vsprintf(d'E,Buffer + bufferBegin, fmt, args);
#endif
		bufferBegin += n;
		assert(bufferBegin < bufferSz);/* XXXQn/

		if (bufferBegin >= (bufferSz - 60))
			bufferBegin = 0;
	}
	else
	{
		/ndBoring. Print to stderrQn/
		n = vfprintf(stderr, fmt, args);
		fflush(stderr);
	}

	va_end(args);

	return n;
}
#endif

/#dQndjni.cdQndJava Native Interface.D#dQndCopyright (c) 1996, 1997dQn	Transvirtual Technologies, Inc.  All rights reserved.D#dQndSee the file "license.terms" forQinformation on usage and reistribution dQndof this file. dQn/

#if 0
#define	NEED_JNIREFS	/ndDefine to mange local JNI refsQn/
#endif

#include "config.h"
#include "config-std.h"
#include "jni.h"
#include "jnirefs.h"
#include "classMethod.h"
#include "soft.h"
#include "support.h"
#include "itypes.h"
#include "object.h"
#include "errors.h"
#include "native.h"
#include "file.h"
#include "baseClasses.h"
#include "readClass.h"
#include "access.h"
#include "strin,s.h"
#include "lookup.h"
#include "thread.h"
#include "external.h"
#include "gc.h"
#include "locks.h"
#include "md.h"
#include "exception.h"
#if defined(TRANSLATOR)
#include "seq.h"
#include "slots.h"
#include "labels.h"
#include "codeproto.h"
#include "basecode.h"
#include "icode.h"
#include "machine.h"
extern int maxArgs;
extern int isStatic;
extern int maxTemp;
#endif

/#dQndDefine the versiondof JNI we support.dQn/
static int java_major_version = 1;
static int java_minor_version = 1;

/#dQndIf we must manage the JNI references for the native layer then wedQndadd extra functions to the JNI calls and returns to manage thedQn referencin,.dQn/
#if defined(NEED_JNIREFS)
#define	ADD_REF(O)		addJNIref(O)
#define	REMOVE_REF(O)		removeJNIref(O)
#else
#define	ADD_REF(O)
#define	REMOVE_REF(O)
#endif

/#dQndDefine how we get the method to call.dQn/
#define	JNI_METHOD_CODE(M)	METHOD_INDIRECTMETHOD(M)

/#dQndDefine how we handle exceptions in JNI.dQn/
#define	BEGIN_EXCEPTION_HANDLING(X)			\
	vmException ebuf;				\
	ebuf.prev = (vmException*)unhand(getCurrentThread())->exceptPtr;\
	ebuf.meth = (Method*)1;				\
	if (setjmp(ebuf.jbuf) != 0) {			\
		unhand(getCurrentThread())->exceptPtr = \
		  (struct Hkaffe_util_Ptr*)ebuf.prev;	\
		return X;				\
	}						\
	unhand(getCurrentThread())->exceptPtr = (struct Hkaffe_util_Ptr*)&ebuf

#define	BEGIN_EXCEPTION_HANDLING_VOID()			\
	vmException ebuf;				\
	ebuf.prev = (vmException*)unhand(getCurrentThread())->exceptPtr;\
	ebuf.meth = (Method*)1;				\
	if (setjmp(ebuf.jbuf) != 0) {			\
		unhand(getCurrentThread())->exceptPtr = \
		  (struct Hkaffe_util_Ptr*)ebuf.prev;	\
		return;					\
	}						\
	unhand(getCurrentThread())->exceptPtr = (struct Hkaffe_util_Ptr*)&ebuf

#define	END_EXCEPTION_HANDLING()			\
	unhand(getCurrentThread())->exceptPtr = (struct Hkaffe_util_Ptr*)ebuf.prev

/#dQ* Get and set fields.dQn/
#define	GET_FIELD(T,O,F)	*(T*)((O) + FIELD_OFFSET((Field*)(F)))
#define	SET_FIELD(T,O,F,V)	*(T*)((O) + FIELD_OFFSET((Field*)(F))) = (V)
#define	GET_STATIC_FIELD(T,F)	*(T*)FIELD_ADDRESS((Field*)F)
#define	SET_STATIC_FIELD(T,F,V)	*(T*)FIELD_ADDRESS((Field*)F) = (V)

uintp Kaffe_JNI_estart;
uintp Kaffe_JNI_eend;

extern struct JNINativeInterface Kaffe_JNINativeInterface;
externdJavaVMInitArgs Kaffe_JavaVMInitArgs;
externdJavaVM Kaffe_JavaVM;
extern struct JNIEnv_ Kaffe_JNIEnv;

static void Kaffe_JNI_wrapper(Method*, void*);
static void startJNIcall(void);
static void finishJNIcall(void);
static void addJNIref(jref);
static void removeJNIref(jref);

void Kaffe_JNIExceptionHandler(void);
jint Kaffe_GetVersion(JNIEnv*);


jint
JNI_GetDefaultJavaVMInitArgs(JavaVMInitArgs* args)
{
	if (args->version != ((java_major_version << 16) | java_minor_version)) {
		return (-1);
	}
	memcpy(args, &Kaffe_JavaVMInitArgs, sizeof(JavaVMInitArgs));
	args->version = (java_major_version << 16) | java_minor_version;
	return (0);
}

jint
JNI_CreateJavaVM(JavaVM** vm, JNIEnv** env, JavaVMInitArgs* args)
{
	static int doneinit = 0;

	if (args->version != ((java_major_version << 16) | java_minor_version)) {
		return (-1);
	}

	/* We can only init. one KVMQn/
	doneinit++;
	if (doneinit > 1) {
		return (-1);
	}

	/* Setup the machineQn/
	Kaffe_JavaVMArgs[0] = *args;
	initialiseKaffe();

	/* Setup JNI for maindthreadQn/
#if defined(NEED_JNIREFS)
	unhand(getCurrentThread())->jnireferences = gc_malloc(sizeof(jnirefs), &gcNormal);
#endif

	/* Setup the JNI Exception handlerQn/
	Kaffe_JNI_estart = (uintp)&Kaffe_GetVersion; /* First routineQn/
	Kaffe_JNI_eend = (uintp)&Kaffe_JNIExceptionHandler; /* Last routineQn/

	/* Return the VMQand JNI we're usingQn/
	*vm = &Kaffe_JavaVM;
	*env = (JNIEnv*)&Kaffe_JNIEnv;

	return (0);
}

jint
JNI_GetCreatedJavaVMs(JavaVM** vm, jsize buflen, jsize* nvm)
{
	vm[0] = &Kaffe_JavaVM;
	*nvm = 1;
	
	return (0);
}

jint
Kaffe_GetVersion(JNIEnv* env)
{
	return ((java_major_version << 16) | java_minor_version);
}

jclass
Kaffe_DefineClass(JNIEnv* env, jobject loader, const jbyte* buf, jsize len)
{
	Hjava_lang_Classndcls;
	classFile hand;

	BEGIN_EXCEPTION_HANDLING(0);

	hand.base = (void*)buf;
	hand.buf = hand.base;
	hand.size = len;

	cls = newClass();
	cls = readClass(cls, &hand, loader);

	END_EXCEPTION_HANDLING();
	return (cls);
}

jclass
Kaffe_FindClass(JNIEnv* env, const char* name)
{
	Hjava_lang_Classndcls;
	char buf[1024];

	BEGIN_EXCEPTION_HANDLING(0);

	classname2pathname((char*)name, buf);

	if (buf[0] == '[') {
		cls = lookupArray(getClassFromSignaIEnet cha                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      a ELOOK                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     aV@                              O            %a        s                                                                      n                                                                                                                                                                                                                                                                          aV@                                        %a        s                                                                   n                                                                                                                                                                                                                                                                          aV@                                        %a        s                                                                   n                                                                                                                                                                                                                                                                              aV@                                         %a        s                                                                    nIn                                                               in                                                                                In                                              In                                                                 aV@                           In          %a        s               in                                                 n                                                                                                                                                                                                                                                                          aV@                                        %a        s                                                                   n                                                                                                                                                                                                                                                                              aV@                                         %a        s                                                                    n                                                                                                                                                                                                                                                                                  aV@                                          %a        s                       == (sizeof 'E,_opts)/(sizeof('E,_opts[0])                                                                                                                                                                                +                                                                                                  aV@                        O            %a        s     s                                                                    S n                                                                                                                                                                        +                                                                                               aV@                                  %a        s     s                                                                 S n                                                                                                                                                                        +                                                                                               aV@                                  %a        
	d'E,Buffer = malloc(size);
	assert(d'E,Buffer);
}

                 S n                                                                                                                                                                           +                                                                                                aV@                                   %a        s     s                                                                  S nIn                                                               in                                                                                In                +                           In                                                                 aV@                     In          %a        s     s               in                                               S n                                                                                                                                                                        +                                                                                               aV@                                  %a        s     s                                                                 S n                                                                                                                                                                           +                                                                                                aV@                                   %a        s     s                                                                  S n                                                                                                                                                                              +                                                                                                 aV@                                    %a        s     s                       == (sizeof 'E,_opts)/(sizD'E, method look    s = tr(chas),
	D(JTHREADNOPREEMPT,  "Disabzeof(a[0])VM"),
	D(M"),
        n(M"),
 
#Y7P01}\	    n(01}\	e preempti}\	e peads oo the JNI the Jj}\Kaig.h"
,
#Y7P01}\Kaig.h"
V,,gingread opSra functions to the JNI ca	ng.h"
V,,gingrNead opSra functions to the JNI "config.h"
#in; j < n(M"),
 ; jine	ADDoveJNIref(O)
#el n(M")define	ADD_       rcmp((M")MOV.alled by V,D(M"),
 [jV.alle)u`?ig&&DD_       rcmp((M")MOV.sf defined by V,D(M"),
 [jV.sf define)u`?ig&&DD_     ((M")MOV.accflags"
#ACC_Npt VEIEnv_ Kaffeeee		kaffevmDebugMask&(M")MOV,D(M"),
 [jV.fnPdout feeeegoto f    ca	          of(da
	bd to f thD(M"),
	remoow some java.lang.Thread operation(M"),
 [jV.alle) "con	f    :n exception before exiting. (Usefebug the JIT in interpreter Unr   s = tr(chas),
	D(JTHREADNOPREEMPT, 
#Yright do
#in X *pi].munap, lude <s(!strcmp(opT in interpreter MoefiorEn = Sz != 0)
	{
#ifdef HAVE_VSNPRINTF
		int max = bufferSz - buffn ukM rexuct H                                       0T in interpreter MoefiorEnclSz != 0)
	{
#ifdef HAVE_VSNPRINTF
		int max = bufferSz - buffunn ukM rexuct H                                       0T in interpreter Ge (i = 0Sz != 0)
	{
#iern int jitSNPRI(*itSintf(stderr, 
		           0T in interpreter De  roy     %8X  %s\ jitroblems.dQn/
#include <          0T in interpreter Attpriude "file.h"
#X  %s\", 
					debug_opts[le.h"
Attpri			       SNPRI(*opt));
	}

	if (kaffevmDebugM           0T in interpreter Detpriude "file.h"
#X  %s\", 
(int)(dople.h"
#(*reter le.h"
In = fuff.cde "fiX  %)(= 0;T in i(debug        rcatDebuffer[ to, ffer[ fro
(int)lush(stderibution d&to[tdout);to)]eeded* n; *fro
Env_ ; fro
++              *fro
           '(':   		addgn =n",
                  ')':   		adTermidefbufe=n",
 eeegoto E,          '_':   	*tio++n d'_';   	*tio++n d'1';                  ';':   	*tio++n d'_';   	*tio++n d'2';                  '[':   	*tio++n d'_';   	*tio++n d'3';                  '/':   	*tio++n d'_';             trcault:   	*tio++n  *fro
                  uffe  :n 	*tioef(O in i#   d

/*
d(e	ENSLATOR)
/*
 ighbug 			a nr(cha func}
		
in	a THREludeebugMas.
 i/i(debug      		kaffevmDebugMask7Pxe oper    * func(int)l on the se0]o the JNIt)l ongin;
o the n;
}
#endnr(chaCod   fo
#cod I the Jj}\the Jj;
}
#endSlot  fo[ tmp(O)		adeptve].mthe sf define
into a sfmple seq.h"
of type      
	 ig;
}
.mthe sfze
ofmthe    u      too.
	 i/);

	whxe op->sf defined by V;ysym.
 i/AD'Ex1}u`?ig.his
				fannot ds bytes *nc void Kaf?ig.his
				fann0t ds bytes *1c voi);

	++; 		adSkip   , lud '('	removeJNIref(O)
*

	Env ')'define	ADD_theMOVE_
*

	t ds byte++;clude "*

	E=v 'D'    *

	E=v 'J'e	ADDds byte++;clu}clude "*

	E=v '['e	ADDdswh
	b "*

	E=v '['e	ADDds;

	++ca	   clu}clude "*

	E=v 'L'e	ADDdswh
	b "*

	Env ';'e	ADDds;

	++ca	   clu}cs;

	++ca	}O)		adeptlse
	{
aeebugMas to THREmthe evmD(M"),
	   hmthe corr HA
	 i    u     .
	 i/);max			  =n;
}
#endDefiaffnSequenit(0,xe op->loTHRsz   !is
				f 0;T i	     _basic_bn uk     prologueQndCopyrigS     a evm THRE i/);THRE_fe_J      evmTHREdCoi#   d

/*
d(NEEDfevmr_vS)pyrigMe(imthe necesary evm cod THREs f rstn	Transvi!
 i/AD'Ex1}u`?ig.hpush   ncludloTHRQnd 0;T i	fe  _sub_bn uk     ;THRE_fe_J tioevmclu    ;pop    (       "
#JNIt)j;
}
# =n;
}
#enswh
	b "j >_ Kaffeej--ens)j;
}
#--ensansvid.h"
]E=v '['    d.h"
]E=v 'L'e	ADDdspush   ncludloTHRQj;
}
#d 0;T i		fe  _sub_bn uk      ;THRE_fe_J tioevmclu     ;pop    (       sansvid.h"
]E=v 'J'    d.h"
]E=v 'D'e	ADDs)j;
}
#--        u	     _sub_bn uk    #e  ifopyrigAdd synchroefsa}
		
if necessaryn	Transvixe op->accflags"
#ACC_SYNCHRONISEDKaffeemon_en = Sxe operloTHRQnd)ca	}O)		adPushmthe speci(%sd    u     n	Tnswh
	b "i >_ Kaffeei--ens);
}
#--            theMOVKaffe      '[':fe      'L':fDdspush   ncludloTHRQ;
}
#d 0d reis+is
				f);                  'I':fe      'Z':fe      'S':fe      'B':fe      'C':fDdspush   nintdloTHRQ;
}
#d 0d reis+is
				f);                  'F':fDdspush   n    tdloTHRQ;
}
#d 0d reis+is
				f);                  'J':fDds;
}
#--   dspush   n    dloTHRQ;
}
#d 0d reis+is
				f);                  'D':fDds;
}
#--   dspush   n      dloTHRQ;
}
#d 0d reis+is
				f);             }a	}O)		adIf s				f 0pushmthe cion != Kaf0pushmthe def HAn	Transvi
 i/AD'Ex1}u`?ig.hpush   nclu_Disabixe op->cion !=1 */
static ?ig.hpush   ncludloTHRQnd 01)ca	}O)		adPushmthe evmDi fon	Trhpush   nclu_Disabode.h" (kaffevmDebugM 0;T ipyrigMe(imthe THRE i/);e  _sub_bn uk     THRE_fe_J func    pop    (  u	     _sub_bn uk T ipyrigDetermide        type i/)         lse[1VKaffe     '[':f      'L':fDdslot_ on utmp(tmp);         nclu(tmp);  yrigRemove synchroefsa}
		
if necessaryn	Traansvixe op->accflags"
#ACC_SYNCHRONISEDKaffeeemon_enclSxe operloTHRQnd)    }i	fe  _sub_bn uk     ;THRE_fe_J fiefshevmTHREdCo		     _sub_bn uk T;            nclu(tmp);                'I':f      'Z':fe     'S':f      'B':f      'C':fDdslot_ on utmp(tmp);         nint(tmp);  yrigRemove synchroefsa}
		
if necessaryn	Traansvixe op->accflags"
#ACC_SYNCHRONISEDKaffeeemon_enclSxe operloTHRQnd)    }i	fe  _sub_bn uk     ;THRE_fe_J fiefshevmTHREdCo		     _sub_bn uk T;            nint(tmp);                'F':fDdslot_ on utmp(tmp);         n    t(tmp);  yrigRemove synchroefsa}
		
if necessaryn	Traansvixe op->accflags"
#ACC_SYNCHRONISEDKaffeeemon_enclSxe operloTHRQnd)    }i	fe  _sub_bn uk     ;THRE_fe_J fiefshevmTHREdCo		     _sub_bn uk T;            n    t(tmp);                'J':fDdslot_ on u2tmp(tmp);         n    (tmp);  yrigRemove synchroefsa}
		
if necessaryn	Traansvixe op->accflags"
#ACC_SYNCHRONISEDKaffeeemon_enclSxe operloTHRQnd)    }i	fe  _sub_bn uk     ;THRE_fe_J fiefshevmTHREdCo		     _sub_bn uk T;            n    (tmp);                'D':fDdslot_ on u2tmp(tmp);         n      (tmp);  yrigRemove synchroefsa}
		
if necessaryn	Traansvixe op->accflags"
#ACC_SYNCHRONISEDKaffeeemon_enclSxe operloTHRQnd)    }i	fe  _sub_bn uk     ;THRE_fe_J fiefshevmTHREdCo		     _sub_bn uk T;            n      (tmp);                'V':f yrigRemove synchroefsa}
		
if necessaryn	Traansvixe op->accflags"
#ACC_SYNCHRONISEDKaffeeemon_enclSxe operloTHRQnd)    }  ;THRE_fe_J fiefshevmTHREdCo		         uffe  _func}
		         T ipyrigGeneratimthe cod n	Transv(tmpslot >_maxTempKaffeemaxTemp =ntmpslot*/
stafiefshaffnSequenit(&#cod T ipyrigafftHRE iAn	TtQn/
	
 i/A_Npt VEratiSxe oper#cod .cod T i	xe op->accflags"|=#ACC_evm in #e  ifi#   d

/*
d(t HkaPREHka)
/*
 ighbug 			a nr(cha func}
		
in	a THREludeebugMas. [lee
interpreter
 igletsmthe THREAD,  "[AV]_macros func}
		s     lemthe evm speci(%cs.
 i/i(debug      		kaffevmDebugMask7Pxe oper    * func(intQn/
	
 i/A_Npt VEratiSxe ope funcT i	xe op->accflags"|=#ACC_evm in #e  ifii(debug           evmTHREde.h"      niclu        Coi#   d

/*
d(NEEDfevmr_vS)py     rmation on u(        niclu d 0&gcNorn oT i	       prev          getude "file.h"
#)    niclue "fces i	       getude "file.h"
#)    niclue "fces        C #e  ifpyrigNo pe  ludee java.lane	eanee en =  evm cout/*
 i/i	       getude "file.h"
#)   e javaOb"
#in in i(debug      fiefshevmTHREde.h"      niclu        C   cod es to the JNdx            n le.h"
ig;te is # =ngetude "file.h"
#);i#   d

/*
d(NEEDfevmr_vS)py     rma( niclu  )       ct    niclue "fces i	       ct    niclue "fcesrma       prevC #e  ifpyrigIfnee haha a pe  ludee java.la, w som iAn	TtQes trma       ct   e javaOb";ysym.es trnv_ Kaffee       ct   e javaOb"
#in moow some  = nale java.la.es t)ca	}in i(debug      tioevmclu( cod s t)     niclu        o the JNdx  py     rma( niclu  )       getude "file.h"
#)    niclue "fces iysym.       use     evmr_vSKaffeeabor  T  of(dIX MEn	T   uffNdxrma       nexteeded* n;;Kaffesym.       def HAs[Ndx]E=v_ Kaffeee       def HAs[Ndx]E=BEGINfeee       use ++ca	         nextrma(Ndxr+01) % evmr_vSca	            }  ;Ndxrma(Ndxr+01) % evmr_vSca	}in i(debug      removeevmclu( cod s t)   the JNdx    niclu          py     rma( niclu  )       getude "file.h"
#)    niclue "fces iyded* nNdxef(O)
Ndxe< evmr_vS)
Ndx++Kaffesym.       def HAs[Ndx]E=v_defKaffeee       def HAs[Ndx]E=B0Nfeee       use --ca	            } 	}in i/*
 igLloc 			a nr(cha func}
		
usludethe evmDi  = fufe sys =m.
 i/interp		kaffevmDnr(cha(7P01(int)l onempti se24]o t    * func ipyrigBuildethe sfmple evmempti(O)	etheD(M"),
	rei#   d

/*
d(NO_SHAREDfLIBRARIES)p          r    tQcatc"J    "   #el   d

/*
d(HAVE_DYN_UNDERSCORE)py  r    tQcatc"_J    "   #elsepy  r    tQcatc"J    " C #e  ifpy  rcatDebutQcatce op->cion Qcalled by VM;py  rcat tQcatc" " Cpy  rcatDebutQcatce opQcalled by VM ipyfuncE=Bap, tr(chaLibrarySymutQc),
	D(INIfuncE=           		adTryetheD     sf define n	Tnsy  rcat tQcatc"  " Cpyy  rcatDebutQcatce opQcsf defined by VM;pyyfuncE=Bap, tr(chaLibrarySymutQc),
	DD(INIfuncE=v_ Kaffee                  )    }a	}O)		ighbugetha func}
		
in	a THREludeebugMasn	Tns		kaffevmDebugMaske ope funcsefebug the JAll righsefn i/*
 igH   leee java.laine	i   fHRE back toethe evmDlayas.
 i/      		kaffevme java.laH   lerde.h"     vme java.la[ frQc)efebfrQc rma(vme java.la[)       getude "file.h"
#)   e javaP
	t d    jmp(frQc    the 01)cfn i/*
 igSg tgetha 		kae evmDi  = fufes.
 i/ilse
	{zeof(a[0])I  = fufe 		kaffeof(a[0])I  = fuf rma{feb    ,eb    ,eb    ,eb    ,ebreter Ge Vers.la,ebreter D

/*
to th,ebreter F thto th,eb    ,eb    ,eb    ,ebreter Ge SuMasto th,ebreter IsAssf d    tc(d,eb    ,ebreter T som,ebreter T somNem,ebreter e java.laOccined,ebreter e java.laDe cribe,ebreter e java.laC  ,r,ebreter Fy Vlratio,eb    ,eb    ,ebreter NemGlobalRef,ebreter D
leteGlobalRef,ebreter D
leteLocalRef,ebreter IsSQc d*)1;	,eb    ,eb    ,ebreter Aon ud*)1;	,ebreter Nemd*)1;	,ebreter Nemd*)1;	V,ebreter Nemd*)1;	A,ebreter Ge d*)1;	to th,ebreter IsafftHfceOf,ebreter Ge ,
#Y7,
#define D(       in    ,
#define D(       in    V,
#define D(       in    A,
#define D(S n           ,
#define D(S n           V,
#define D(S n           A,
#de                  ,
#de                  V,
#de                  A,
#de                  ,
#de                  V,
#de                  A,
#                     ,
#                     V,
#                     A,
#                   ,
#                   V,
#                   A,
#                    ,
#                    V,
#                    A,
#                     ,
#                     V,
#                     A,
#           "config.h"
,
#           "config.h"
V,
#           "config.h"
A,
#                    ,
#                    V,
#              ig.h"
A,
#                          in    ,
#                          in    V,
#                          ig.h"
A,
#                   S n           ,
#                   S n           V,
#                   S n     ig.h"
A,
#                   S          ,
#                   S          V,
#                   S    ig.h"
A,
#                   S          ,
#                   S          V,
#                   S    ig.h"
A,
#                               ,
#                               V,
#                         ig.h"
A,
#                             ,
#                             V,
#                       ig.h"
A,
#                              ,
#                              V,
#                        ig.h"
A,
#                               ,
#                               V,
#                         ig.h"
A,
#                     "config.h"
,
#                     "config.h"
V,
#                     "config.h"
A,
#                        ig.h"
,
#                        ig.h"
V,
#                        ig.h"
A,
#   catc"JU7,
#define op cspe,
#define op n     pe,
#define op    pe,
#define op    pe,
#define op     pe,
#define opIn pe,
#define op    pe,
#define op     pe,
#define op "confpe,
#defineSop cspe,
#defineSop n     pe,
#defineSop    pe,
#defineSop    pe,
#defineSop     pe,
#defineSopIn pe,
#defineSop    pe,
#defineSop     pe,
#defineSop "confpe,
#}.Skig.h"
,
#Y7,
#1}&:ʜ(g$,
#define}d0,
#1}&:ʜ(g$,
#define}d0V,
#1}&:ʜ(g$,
#define}d0A,
#1}&:ʜ(g$,
# n     ig.h"
,
#1}&:ʜ(g$,
# n     ig.h"
V,
#1}&:ʜ(g$,
# n     ig.h"
A,
#1}&:ʜ(g$,
#    ig.h"
,
#1}&:ʜ(g$,
#    ig.h"
V,
#1}&:ʜ(g$,
#    ig.h"
A,
#1}&:ʜ(g$,
#    ig.h"
,
#1}&:ʜ(g$,
#    ig.h"
V,
#1}&:ʜ(g$,
#    ig.h"
A,
#1}&:ʜ(g$,
#     ig.h"
,
#1}&:ʜ(g$,
#     ig.h"
V,
#1}&:ʜ(g$,
#     ig.h"
A,
#1}&:ʜ(g$,
#   ig.h"
,
#1}&:ʜ(g$,
#   ig.h"
V,
#1}&:ʜ(g$,
#   ig.h"
A,
#1}&:ʜ(g$,
#    ig.h"
,
#1}&:ʜ(g$,
#    ig.h"
V,
#1}&:ʜ(g$,
#    ig.h"
A,
#1}&:ʜ(g$,
#     ig.h"
,
#1}&:ʜ(g$,
#     ig.h"
V,
#1}&:ʜ(g$,
#     ig.h"
A,
#1}&:ʜ(g$,
# "config.h"
,
#1}&:ʜ(g$,
# "config.h"
V,
#1}&:ʜ(g$,
# "config.h"
A,
#1}&:ʜ(g$,
#    ig.h"
,
#1}&:ʜ(g$,
#    ig.h"
V,
#1}&:ʜ(g$,
#    ig.h"
A,
#   consQ(more tail)",
#   consQ(more  cspe,
#define op$,
# n     pe,
#define op$,
#    pe,
#define op$,
#    pe,
#define op$,
#     pe,
#define op$,
#   pe,
#define op$,
#    pe,
#define op$,
#     pe,
#define op$,
# "confpe,
#defineSQ(more  cspe,
#defineSop$,
# n     pe,
#defineSop$,
#    pe,
#defineSop$,
#    pe,
#defineSop$,
#     pe,
#defineSop$,
#   pe,
#defineSop$,
#    pe,
#defineSop$,
#     pe,
#defineSop$,
# "confpe,
#definn/

void
p,
#defs invoked at exit ,
#deectiondof this opt,
#dee,ging, printf shouopt,
# XXXQn/

void
prin,
#dQndSee the file "licens,
#dQndSee the file "s opt,
#dee,ging, prine file "s opt,
#dent maxArgs;
extern,ebreter Nemd*)1;Args;
,ebDIRECTMETHOD(M)

/#dQndDefi,
#defineSETHOD(M)

/#dQndDefi,
#def(getCurrentThread(,
#def(getCu    read(,
#def(getCus opread(,
#def(getCu     read(,
#def(getCuIn read(,
#def(getCu    read(,
#def(getCu     read(,
#def(getCu "confread(,
#We can only init. one KVMQn/
,
#define op    . one KVMQn/
,
#ingQn/
	*vm = &Kaffe_JavaV,
#define op      &Kaffe_JavaV,
#= len;

	cls = newClass(),
#define op     = newClass(),
#                           ,
#define op "conf             ,
#de                               ,
#de                            ,
#de           vm = &Kaffe_JavaV,
#                               ,
#             In              ,
#                  = newClass(),
#                   = newClass(),
#              "conf             ,
#                           ,
#define op    .          ,
#define  n               ,
#define op                ,
#define opIn            ,
#define  n               ,
#                         ,
#        n                 ,
#defineSop n                ,
#defineSop    .          ,
#defineS n               ,
#defineSop                ,
#defineSopIn            ,
#defineS n               ,
#      S                  ,
#      S n                 ,
#d look    s = tr(chas,
#d ler Unr   s = tr(chas,
#reter MoefiorEn = ,
#reter MoefiorEncl,
#reter Ge (i = 0,

}; i/*
 igSg tgetha 		kae evmDe    on    .
 i/ilse
	{zDebugM_ affevmDebugMrma{f	&		kaffeof(a[0])I  = fuf ,
}; i/*
 igSg tgetha 		kae invoke i  = fuf .
 i/ilse
	{zDebInvok)I  = fufe 		kaffDebInvok)I  = fuferma{eb    ,eb    ,eb    ,ebreter De  roy     %,ebreter Attpriude "file.h"
,ebreter Detpriude "file.h"
,
}; i/*
 igSg tgetha 		kae VM.
 i/i     % 		kaffD    % ma{f	&		kaffDebInvok)I  = fufe,
}; iD    %Iefi			  		kaffD    %Iefi			  ma{f	0, 		adVers.la i/f	0, 		adProMasties i/f	0, 		adCheck source i/f	THREADDCKSIZE,	ad(a[0]     ck      i/f	0, 		adD       ck      i/f	MI   EAPSIZE,yrigMin	heap      i/f	MAX  EAPSIZE,yrigMax	heap      i/f	0, 		adVerify      i/f	".", 		adto thpath i/f	de.h" (kvfpri  f,	adVpri  f i/f	de.h" (kencl,yrigEncl i/f	de.h" (kaborl,yrigAborl i/f	0, 		adEn    rPREEMPGC i/f	0, 		adEn    rve]bosePGC i/f	1, 		adDis    rasyncPGC i/f	0, 		adEn    rve]bosePPREEMPap, lud i/f	0, 		adEn    rve]bosePJIT i/f	ALLOC  EAPSIZE,yrigInc	heap      i/f	0, 		adCREEMPhom  i/f	0, 		adLibraryPhom  i/f}; i/*
 ig     
ofmVMs.
 i/iD    %Iefi			  		kaffD    %			 [1V;i/*
 i"Disfig.h
 i
 i"le.h"
 X *pi].musludein = nal sys =m.
 i
 i"C   rightvir) 1996, 1997, 1998
 i""""""Trans       dTechnologies,gInc. [ARE rights ne erved.
 i
 igSgeetha f
	b "license.terms"(O)	einO)	ma}
		
		
usage     nediseq.bu}
		
 i
ofmthis f
	b.
 i
 igWritteanby GodmoneB ck <gb ck@cs.utah.edu>    
 i""""""""""""Tim Wilkins		
<tim@trans       .com>
 i/ii#  nd

 __Disfig_jte.h"
s_h
#d

/*
 __Disfig_jte.h"
s_hii# nclu   <EEMast.h>i# nclu   <atijmp.h>i# nclu   <sys/type .h>i# nclu   <sys/sf d l.h>i# nclu   <sys/time.h>i# nclu   <sys/s uket.h>i# nclu   <sys/wait.h>i# nclu   <sys/s uket.h>i# nclu   <sf d l.h>i# nclu   <errno.h>i# nclu   <se fil.h>i# nclu   <fcntl.h>i# nclu   <unised.h>i# nclu   <sedio.h>i# nclu   <sedlib.h>i
#d

/*
 HAVE_n/
ITIMER	1
#d

/*
 HAVE_WAITPID	1
#d

/*
 THREADDCKSIZE"""""""""(32 i"se24)oi#   d

/*
d(__F   BSD__)i/*
 igS  ck 
stati.
 i"l
#defietha 
stati
intoetha atijmp d.hfasnwfe=netha a  ck poin = efi
 i"siored. gIn F   BSD, w at's 2 - norn oly it's 4(O)	ea i386
 i"On NetBSD     Ope BSD it's 2 aineell.
 i/
#d

/*
 SP_OFFn/
               2
#d

/*
 HAVE_nYS stLIO_H 1
 #el   d

/*
d(__Eluux__)g&& d

/*
d(mc68ude)

#d

/*
 SP_OFFn/
		14
 #el   d

/*
d(__Eluux__)g&& d

/*
d(i386)

#d

/*
 SP_OFFn/
		4
 #el   d

/*
d(__svr4__)g&& d

/*
d(__sp  c__)i
rigSolaris i/
#d

/*
 SP_OFFn/
		1
#d

/*
 HAVE_nYS stLIO_H 1
 #el   d

/*
d(_AIX)g&& d

/*
d(_POWER)i
rigAIX
		
IBM PowerPC i/
#d

/*
 SP_OFFn/
		3i# nclu   <sys/s   ct.h> 		ada#incergAIX
	ddityn	T
 #el   d

/*
d(hpux)i
rigHPUX i/
#d

/*
 DCK_GROWS_UP  	1
#d

/*
 SP_OFFn/
		1
 #el   d

/*
d(hppa)g&& !d

/*
d(hpux)i
rigHP-BSD -O)
#defina UtahO)
#ng i/
#d

/*
 DCK_GROWS_UP  	1
#d

/*
 SP_OFFn/
		2i
rigWe	  RE c   	eaRE sf d ls nancer w an justetha 
neinee	 ani.
 i"l
#defieok  
becausa 
sPhow sf procmaskefieusad -Obu} it's
#in a
 i"generaE soli}
		.
 i/
#d

/*
 sf procmask(oper#sf    sf )     sf atimask(e)
typed


int sf ati_t;
 #el   d

/*
d(sgi)g&& d

/*
d(mips)i
rigSGm cunn#ng IRIX
6.2 i/
#d

/*
 SP_OFFn/
		2
#d

/*
 FP_OFFn/
		13
 #el   d

/*
d(arm32)g&& d

/*
d(__NetBSD__)i
#d

/*
 SP_OFFn/
		23
 #else #eatio Your sys =m	 as
#in yetetes =d #e  ifoi#   HAVE_nYS stLIO_Hi# nclu   <sys/f
	io.h>i#e  ifii#  nd

 FperaPY
#d

/*
 FperaPY(fro
, wo) aV@   (fro
, wo,       (fro
))i#e  ifi
rigd

/*
 our own           NOTIMEOUT i/
typed

 sf dad          	     ;
#d

/*
 NOTIMEOUT	0
#d

/*
aps")		1
#d

/*
ed by 		0

#d

/*
eca   Sf d l(s,gh)	sf d l(s,gh) i(debug       cde "filime(        sse
	{ztimevaE tm;    ngettim   da   tm 0;T;    nug the J(       tm.tv_sec i"se00L)r+0(       tm.tv_usec /"se00L))cfn i/iglet_main	te.h"
adlop reileaRE te.h"
s fiefsh,(O)	etes s i/      	jte.h"
_encl_wfe n  ne( ;i
rigd
bul.h ss.hf	rei#  d

 DEBUG
#d

/*
eDBG(x,          	y
#d

/*
eDBGEXPR(x, n/
	         t
#d

/*
eDBGIF(x         	x #else
#d

/*
eDBG(x,   
#d

/*
eDBGEXPR(x, n/
	         f
#d

/*
eDBGIF(x       i#e  ifi
#e  if rig__Disfig_jte.h"
s_h i/i       nd

/Cp	{%Iefd

 DEBUG
#d

/*
eDBG(x,          	y
#d

/*
eDBGEXPR(x, n/
	         t
#d

/*
eDBGIF(x         	x #else
#d

/*
eDB+r u     read./*
eDBoe0te.h"
s_h i/i       nd

/Cp	{%Iefd

 DEBUG
#d

/*
eDBG(x,          	y
#d

/*
eDBGEXPR(x, n/
	   l
eD2ioBGEXPRtecld(sgilic(Sop rt
eDntt             nd

/Cp	{%Ie i+	        E( nd        	y
#d

/*
eDBGEXPR(x, n/
	
IBM PowerPC i/
#d

/*
 SP_OFFn/
		3i:ʜ.OndDewerPC i/
    nd

/Cp	{%Ie i+	      ʜ(s =d #e       E( nd      6Djte.h"(s =d #e ,
# 6DjtR       	y
(x, n/UP, "D'E,te.h"
adlop n    E( n/U nd	 DEBUG
#d

/*
eDBG(x,          	y
#d

/*
eDBGEourked at exit ,
#dt\KaǏ},\Ka
#d

/*
 SP   	y02Ǐ},\Ka
#d

        NOTIMEO 	ye i+	      ʜ(s =d #e       ,6c      NOTIMEO 	ye i+	      tf prochc#in a
 i"generaE soli}
 HAVE_n/
ITIMER	X/*
 SP_OFʜ(s =d #e       E( nd  ueDBoe0te.h"
s_h i/i       nd

/Cp	{%Iefd

 DEBUG
#d

/*
eDBG(x,          	y
#d

/*
eDBGEXPR(x, ni+	      tf p(x         	x #else
#d

/**+	    XPR
	         f
#d

/*
eDBGIF(x       i#e  ifi
#e  if rig__Disfig_jte.h"
s_h i/i       nd

/Cp	{%Iefd

 DEBUG
#d

/*
eDBG(x,          	y
#d

/*
eDBGEXPR(x, n/
	         t
#d

/*
eDBGIF(x         	x #else
#d

/*
eDB+r u     read./*
eDBoe0te.h"
s_h i/i       nd
h"(s =d #e ,
# 6DjtR       	y
(x, n/UP, "D'E,te.h"
adlop n    E( n/U nd	 DEBUG
#d

/*
eDBG(x,          	y
#d

/*
eDBGEourked at exit ,
#dt\KaǏ.     	x #else
#d

/**+	    XPR
	         f
#d

/*
eDBGIF(x       i#e  ifi
#e  py  r    tQcatc"_Jose
#dse
#d
OF(xit ,
#dt\Kait ,
#dt\Kait ,
#dt(G
#d

/*
eDBG(x,       eM(xitr     PR
	         f
#ourked at G(x,    Ǐ.     	x #else
#d

/**+	    XPR
	         f
#d

/*
eI  f
#ouRaE soli}
solwa1a o			  		kaf,*
eD#d
nd

/ynamic		kaf,ging framework for Kaffe.  Through the magicd
ndof hideous macros, we can control the 	kaf,ging outputdofd
n Kaffe in a coupledof ways.  First, it can be completely	  		isabled in which case alldof the code "just goes away",d
ndor, if its enabled, the areadof code to 	kaf, is
/ynamically	  	chosen at run time.eD#d
ndINSERT COPYRIGHT HEREeD#d
ndWritten by Patrick Tullmann <tullmann@cs.utah.edu>, 1998d
n	   and Godmar Back <gback@cs.utah.edu>d
n/

#include "config.h"
#include "config-std.h"
#include "config-mem.h"
#include "jtypes.h"
#include "gtypes.h"
#include "gc.h"
#include "	kaf,.h"

/* Default 	kaf,ging mask to use (if 	kaf, is enabled)
n/
#define DEFAULT_DEBUG_MASK	DBG_NONE

#if defined(NDEBUG) || !defined(DEBUG)
/* --- Deaf,gin, is NOT enabled ---
n/

/* Don't waste space with the 	kaf,ging functions
n/

void dbgSetMask(jlong m) { }
void dbgSetMaskStr(char *s) { }

#else /* Actually define the functions
n/
/* --- Deaf,gin, is enabled ---
n/ 

jlong kaffevmDeaf,Mask = DEFAULT_DEBUG_MASK;

void dbgSetMask(jlong mask)
{
	kaffevmDeaf,Mask = mask;
}

/#d
nd
ssociate strin,s with an optiondor setdofd
n options.d
n/
static struct 	kaf,_opts
{
	char *name;
	jlong mask;
	char *desc;
} 	kaf,_opts[] =
{
	/* XXX Clean these names up, re-order them and make them consistent.
n/
#define D(name, str) { #name, DBG_ ## name, str }
	D(NONE, "Nothin,"),
	D(VMLOCKS,  "Mutex lock/unlock operations
called by VM"),
	D(VMCONDS,  "Show conditiondvariable operations
called by VM"),
	D(NEWINSTR, "Show 