3 * ================================================================
4 * SourcePawn (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ================================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
15 #ifndef _INCLUDE_SOURCEPAWN_JIT_X86_H_
16 #define _INCLUDE_SOURCEPAWN_JIT_X86_H_
18 #include <sp_vm_types.h>
19 #include <sp_vm_api.h>
20 #include <jit_helpers.h>
22 using namespace SourcePawn;
24 #define JIT_INLINE_ERRORCHECKS (1<<0)
25 #define JIT_INLINE_NATIVES (1<<1)
26 #define STACK_MARGIN 64 //8 parameters of safety, I guess
27 #define JIT_FUNCMAGIC 0x214D4148 //magic function offset
29 #define JITVARS_TRACKER 0 //important: don't change this to avoid trouble
30 #define JITVARS_FUNCINFO 1 //important: don't change this aWOAWOGJQG I LIKE HAM
31 #define JITVARS_REBASE 2 //important: hi, i'm bail
33 typedef struct tracker_s
40 typedef struct funcinfo_s
46 typedef struct functracker_s
48 unsigned int num_functions;
49 unsigned int code_size;
63 class CompData : public ICompilation
66 CompData() : plugin(NULL),
67 debug(false), inline_level(0), rebase(NULL),
68 error_set(SP_ERROR_NONE), func_idx(0)
72 sp_plugin_t *plugin; /* plugin handle */
73 bool debug; /* whether to compile debug mode */
74 int inline_level; /* inline optimization level */
75 jitcode_t rebase; /* relocation map */
76 int error_set; /* error code to halt process */
77 unsigned int func_idx; /* current function index */
78 jitoffs_t jit_return; /* point in main call to return to */
79 jitoffs_t jit_verify_addr_eax;
80 jitoffs_t jit_verify_addr_edx;
81 jitoffs_t jit_break; /* call to op.break */
82 jitoffs_t jit_sysreq_n; /* call version of op.sysreq.n */
83 jitoffs_t jit_genarray; /* call to genarray intrinsic */
84 jitoffs_t jit_error_bounds;
85 jitoffs_t jit_error_divzero;
86 jitoffs_t jit_error_stacklow;
87 jitoffs_t jit_error_stackmin;
88 jitoffs_t jit_error_memaccess;
89 jitoffs_t jit_error_heaplow;
90 jitoffs_t jit_error_heapmin;
91 jitoffs_t jit_error_array_too_big;
92 jitoffs_t jit_extern_error; /* returning generic error */
93 jitoffs_t jit_sysreq_c; /* old version! */
94 jitoffs_t jit_rounding_table;
95 floattbl_t *jit_float_table;
96 uint32_t codesize; /* total codesize */
99 class JITX86 : public IVirtualMachine
102 const char *GetVMName();
103 ICompilation *StartCompilation(sp_plugin_t *plugin);
104 bool SetCompilationOption(ICompilation *co, const char *key, const char *val);
105 sp_context_t *CompileToContext(ICompilation *co, int *err);
106 void AbortCompilation(ICompilation *co);
107 void FreeContext(sp_context_t *ctx);
108 int ContextExecute(sp_context_t *ctx, uint32_t code_idx, cell_t *result);
109 unsigned int GetAPIVersion();
110 bool FunctionLookup(const sp_context_t *ctx, uint32_t code_addr, unsigned int *result);
111 bool FunctionPLookup(const sp_context_t *ctx, uint32_t code_addr, unsigned int *result);
112 unsigned int FunctionCount(const sp_context_t *ctx);
113 const char *GetVersionString();
114 const char *GetCPUOptimizations();
115 SPVM_NATIVE_FUNC CreateFakeNative(SPVM_FAKENATIVE_FUNC callback, void *pData);
116 void DestroyFakeNative(SPVM_NATIVE_FUNC func);
119 cell_t NativeCallback(sp_context_t *ctx, ucell_t native_idx, cell_t *params);
120 cell_t NativeCallback_Debug(sp_context_t *ctx, ucell_t native_idx, cell_t *params);
121 jitoffs_t RelocLookup(JitWriter *jit, cell_t pcode_offs, bool relative=false);
123 #define AMX_REG_PRI REG_EAX
124 #define AMX_REG_ALT REG_EDX
125 #define AMX_REG_STK REG_EDI
126 #define AMX_REG_DAT REG_EBP
127 #define AMX_REG_TMP REG_ECX
128 #define AMX_REG_INFO REG_ESI
129 #define AMX_REG_FRM REG_EBX
131 #define AMX_INFO_FRM AMX_REG_INFO //not relocated
132 #define AMX_INFO_FRAME 0 //(same thing as above)
133 #define AMX_INFO_HEAP 4 //not relocated
134 #define AMX_INFO_RETVAL 8 //physical
135 #define AMX_INFO_CONTEXT 12 //physical
136 #define AMX_INFO_STACKTOP 16 //relocated
138 extern ISourcePawnEngine *engine;
140 #endif //_INCLUDE_SOURCEPAWN_JIT_X86_H_