sourcepawn/jit/x86/jit_x86.h
author David Anderson <dvander@alliedmods.net>
Mon Mar 12 07:08:05 2007 +0000 (2007-03-12)
changeset 601 fe5b368fcedc
parent 371 71b0caa3aaa8
child 605 0b7655d87148
permissions -rw-r--r--
initial import of dynamic native code for both the JIT and plugins
note: dependency resolution is not done yet!
     1 /**
     2  * ================================================================
     3  * SourcePawn (C)2004-2007 AlliedModders LLC.  All rights reserved.
     4  * ================================================================
     5  *
     6  * This file is not open source and may not be copied without explicit
     7  * written permission of AlliedModders LLC.  This file may not be redistributed 
     8  * in whole or significant part.
     9  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    10  *
    11  * Version: $Id$
    12  */
    13 
    14 #ifndef _INCLUDE_SOURCEPAWN_JIT_X86_H_
    15 #define _INCLUDE_SOURCEPAWN_JIT_X86_H_
    16 
    17 #include <sp_vm_types.h>
    18 #include <sp_vm_api.h>
    19 #include "../jit_helpers.h"
    20 
    21 using namespace SourcePawn;
    22 
    23 #define JIT_INLINE_ERRORCHECKS		(1<<0)
    24 #define JIT_INLINE_NATIVES			(1<<1)
    25 #define STACK_MARGIN				64			//8 parameters of safety, I guess
    26 #define JIT_FUNCMAGIC				0x214D4148	//magic function offset
    27 
    28 #define JITVARS_TRACKER				0		//important: don't change this to avoid trouble
    29 #define JITVARS_FUNCINFO			1		//important: don't change this aWOAWOGJQG I LIKE HAM
    30 #define JITVARS_REBASE				2		//important: hi, i'm bail
    31 
    32 typedef struct tracker_s
    33 {
    34 	size_t size; 
    35 	ucell_t *pBase; 
    36 	ucell_t *pCur;
    37 } tracker_t;
    38 
    39 typedef struct funcinfo_s
    40 {
    41 	unsigned int magic;
    42 	unsigned int index;
    43 } funcinfo_t;
    44 
    45 typedef struct functracker_s
    46 {
    47 	unsigned int num_functions;
    48 	unsigned int code_size;
    49 } functracker_t;
    50 
    51 class CompData : public ICompilation
    52 {
    53 public:
    54 	CompData() : plugin(NULL), 
    55 		debug(false), inline_level(0), rebase(NULL),
    56 		error_set(SP_ERROR_NONE), func_idx(0)
    57 	{
    58 	};
    59 public:
    60 	sp_plugin_t *plugin;			/* plugin handle */
    61 	bool debug;						/* whether to compile debug mode */
    62 	int inline_level;				/* inline optimization level */
    63 	jitcode_t rebase;				/* relocation map */
    64 	int error_set;					/* error code to halt process */
    65 	unsigned int func_idx;			/* current function index */
    66 	jitoffs_t jit_return;			/* point in main call to return to */
    67 	jitoffs_t jit_verify_addr_eax;
    68 	jitoffs_t jit_verify_addr_edx;
    69 	jitoffs_t jit_break;			/* call to op.break */
    70 	jitoffs_t jit_sysreq_n;			/* call version of op.sysreq.n */
    71 	jitoffs_t jit_genarray;			/* call to genarray intrinsic */
    72 	jitoffs_t jit_error_bounds;
    73 	jitoffs_t jit_error_divzero;
    74 	jitoffs_t jit_error_stacklow;
    75 	jitoffs_t jit_error_stackmin;
    76 	jitoffs_t jit_error_memaccess;
    77 	jitoffs_t jit_error_heaplow;
    78 	jitoffs_t jit_error_heapmin;
    79 	jitoffs_t jit_error_array_too_big;
    80 	jitoffs_t jit_extern_error;		/* returning generic error */
    81 	jitoffs_t jit_sysreq_c;			/* old version! */
    82 	uint32_t codesize;				/* total codesize */
    83 };
    84 
    85 class JITX86 : public IVirtualMachine
    86 {
    87 public:
    88 	const char *GetVMName();
    89 	ICompilation *StartCompilation(sp_plugin_t *plugin);
    90 	bool SetCompilationOption(ICompilation *co, const char *key, const char *val);
    91 	sp_context_t *CompileToContext(ICompilation *co, int *err);
    92 	void AbortCompilation(ICompilation *co);
    93 	void FreeContext(sp_context_t *ctx);
    94 	int ContextExecute(sp_context_t *ctx, uint32_t code_idx, cell_t *result);
    95 	unsigned int GetAPIVersion();
    96 	bool FunctionLookup(const sp_context_t *ctx, uint32_t code_addr, unsigned int *result);
    97 	bool FunctionPLookup(const sp_context_t *ctx, uint32_t code_addr, unsigned int *result);
    98 	unsigned int FunctionCount(const sp_context_t *ctx);
    99 	const char *GetVersionString();
   100 	const char *GetCPUOptimizations();
   101 	SPVM_NATIVE_FUNC CreateFakeNative(SPVM_FAKENATIVE_FUNC callback, void *pData);
   102 	void DestroyFakenative(SPVM_NATIVE_FUNC func);
   103 };
   104 
   105 cell_t NativeCallback(sp_context_t *ctx, ucell_t native_idx, cell_t *params);
   106 cell_t NativeCallback_Debug(sp_context_t *ctx, ucell_t native_idx, cell_t *params);
   107 jitoffs_t RelocLookup(JitWriter *jit, cell_t pcode_offs, bool relative=false);
   108 
   109 #define AMX_REG_PRI		REG_EAX
   110 #define AMX_REG_ALT		REG_EDX
   111 #define AMX_REG_STK		REG_EDI
   112 #define AMX_REG_DAT		REG_EBP
   113 #define AMX_REG_TMP		REG_ECX
   114 #define AMX_REG_INFO	REG_ESI
   115 #define AMX_REG_FRM		REG_EBX
   116 
   117 #define AMX_INFO_FRM		AMX_REG_INFO	//not relocated
   118 #define AMX_INFO_FRAME		0				//(same thing as above) 
   119 #define AMX_INFO_HEAP		4				//not relocated
   120 #define AMX_INFO_RETVAL		8				//physical
   121 #define AMX_INFO_CONTEXT	12				//physical
   122 #define AMX_INFO_STACKTOP	16				//relocated
   123 
   124 extern ISourcePawnEngine *engine;
   125 
   126 #endif //_INCLUDE_SOURCEPAWN_JIT_X86_H_