2 * ================================================================
3 * SourcePawn (C)2004-2007 AlliedModders LLC. All rights reserved.
4 * ================================================================
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
14 #ifndef _INCLUDE_SOURCEPAWN_JIT_X86_H_
15 #define _INCLUDE_SOURCEPAWN_JIT_X86_H_
17 #include <sp_vm_types.h>
18 #include <sp_vm_api.h>
19 #include "../jit_helpers.h"
21 using namespace SourcePawn;
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
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
32 typedef struct tracker_s
39 typedef struct funcinfo_s
45 typedef struct functracker_s
47 unsigned int num_functions;
48 unsigned int code_size;
51 class CompData : public ICompilation
54 CompData() : plugin(NULL),
55 debug(false), inline_level(0), rebase(NULL),
56 error_set(SP_ERROR_NONE), func_idx(0)
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 */
85 class JITX86 : public IVirtualMachine
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);
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);
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
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
124 extern ISourcePawnEngine *engine;
126 #endif //_INCLUDE_SOURCEPAWN_JIT_X86_H_