sourcepawn/jit/x86/jit_x86.h
author David Anderson <dvander@alliedmods.net>
Sun Aug 11 11:33:47 2013 -0700 (2013-08-11)
changeset 3988 f947f2955ae0
parent 3987 05ab44d7ec01
child 3989 b73dae36a0da
permissions -rw-r--r--
Remove InfoVars::ctx (bug 5844 part 5, r=fyren).
dvander@367
     1
/**
danderson@3977
     2
 * vim: set ts=4 sw=4 tw=99 et:
ds@1330
     3
 * =============================================================================
dvander@2114
     4
 * SourceMod
dvander@2114
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1330
     6
 * =============================================================================
dvander@367
     7
 *
dvander@2114
     8
 * This program is free software; you can redistribute it and/or modify it under
dvander@2114
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
dvander@2114
    10
 * Free Software Foundation.
dvander@2114
    11
 * 
dvander@2114
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
dvander@2114
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
dvander@2114
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
dvander@2114
    15
 * details.
dvander@2114
    16
 *
dvander@2114
    17
 * You should have received a copy of the GNU General Public License along with
dvander@2114
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@2114
    19
 *
dvander@2114
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
dvander@2114
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
dvander@2114
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
dvander@2114
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
dvander@2114
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
dvander@2114
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
dvander@2114
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
dvander@2114
    27
 * or <http://www.sourcemod.net/license.php>.
dvander@367
    28
 *
dvander@367
    29
 * Version: $Id$
dvander@367
    30
 */
dvander@367
    31
dvander@157
    32
#ifndef _INCLUDE_SOURCEPAWN_JIT_X86_H_
dvander@157
    33
#define _INCLUDE_SOURCEPAWN_JIT_X86_H_
dvander@157
    34
dvander@157
    35
#include <sp_vm_types.h>
dvander@157
    36
#include <sp_vm_api.h>
dvander@2226
    37
#include <KeCodeAllocator.h>
danderson@3979
    38
#include <macro-assembler-x86.h>
danderson@3977
    39
#include <ke_vector.h>
dvander@2226
    40
#include "jit_shared.h"
dvander@2226
    41
#include "BaseRuntime.h"
dvander@2226
    42
#include "jit_function.h"
danderson@3977
    43
#include "opcodes.h"
dvander@157
    44
dvander@157
    45
using namespace SourcePawn;
dvander@157
    46
danderson@3977
    47
#define JIT_INLINE_ERRORCHECKS  (1<<0)
danderson@3977
    48
#define JIT_INLINE_NATIVES      (1<<1)
danderson@3977
    49
#define STACK_MARGIN            64      //8 parameters of safety, I guess
danderson@3977
    50
#define JIT_FUNCMAGIC           0x214D4148  //magic function offset
dvander@157
    51
danderson@3977
    52
#define JITVARS_TRACKER         0    //important: don't change this to avoid trouble
danderson@3977
    53
#define JITVARS_BASECTX         1    //important: don't change this aWOAWOGJQG I LIKE HAM
danderson@3977
    54
#define JITVARS_PROFILER        2    //profiler
danderson@3977
    55
#define JITVARS_PLUGIN          3    //sp_plugin_t
dvander@157
    56
danderson@3977
    57
#define sDIMEN_MAX              5    //this must mirror what the compiler has.
dvander@1396
    58
dvander@2193
    59
#define GET_CONTEXT(c)  ((IPluginContext *)c->vm[JITVARS_BASECTX])
dvander@2193
    60
dvander@157
    61
typedef struct tracker_s
dvander@157
    62
{
danderson@3977
    63
  size_t size; 
danderson@3977
    64
  ucell_t *pBase; 
danderson@3977
    65
  ucell_t *pCur;
dvander@157
    66
} tracker_t;
dvander@157
    67
dvander@175
    68
typedef struct funcinfo_s
dvander@175
    69
{
danderson@3977
    70
  unsigned int magic;
danderson@3977
    71
  unsigned int index;
dvander@175
    72
} funcinfo_t;
dvander@175
    73
dvander@175
    74
typedef struct functracker_s
dvander@175
    75
{
danderson@3977
    76
  unsigned int num_functions;
danderson@3977
    77
  unsigned int code_size;
dvander@175
    78
} functracker_t;
dvander@175
    79
danderson@3977
    80
struct CallThunk
faluco@831
    81
{
danderson@3977
    82
  Label call;
danderson@3977
    83
  cell_t pcode_offset;
faluco@831
    84
danderson@3977
    85
  CallThunk(cell_t pcode_offset)
danderson@3977
    86
    : pcode_offset(pcode_offset)
danderson@3977
    87
  {
danderson@3977
    88
  }
dvander@2226
    89
};
dvander@2226
    90
dvander@157
    91
class CompData : public ICompilation
dvander@157
    92
{
dvander@157
    93
public:
danderson@3977
    94
  CompData() 
danderson@3977
    95
  : profile(0),
danderson@3977
    96
    inline_level(0)
danderson@3977
    97
  {
danderson@3977
    98
  };
danderson@3977
    99
  bool SetOption(const char *key, const char *val);
danderson@3977
   100
  void Abort();
dvander@157
   101
public:
danderson@3977
   102
  cell_t cur_func;            /* current func pcode offset */
danderson@3977
   103
  /* Options */
danderson@3977
   104
  int profile;                /* profiling flags */
danderson@3977
   105
  int inline_level;           /* inline optimization level */
danderson@3977
   106
  /* Per-compilation properties */
danderson@3977
   107
  unsigned int func_idx;      /* current function index */
danderson@3977
   108
};
danderson@3977
   109
danderson@3977
   110
class Compiler
danderson@3977
   111
{
danderson@3977
   112
 public:
danderson@3977
   113
  Compiler(BaseRuntime *rt, cell_t pcode_offs);
danderson@3977
   114
  ~Compiler();
danderson@3977
   115
danderson@3977
   116
  JitFunction *emit(int *errp);
danderson@3977
   117
danderson@3977
   118
 private:
danderson@3977
   119
  bool setup(cell_t pcode_offs);
danderson@3977
   120
  bool emitOp(OPCODE op);
danderson@3977
   121
  cell_t readCell();
danderson@3977
   122
danderson@3977
   123
 private:
danderson@3977
   124
  Label *labelAt(size_t offset);
danderson@3977
   125
  bool emitCall();
danderson@3977
   126
  bool emitNativeCall(OPCODE op);
danderson@3977
   127
  bool emitSwitch();
danderson@3977
   128
  void emitGenArray(bool autozero);
danderson@3977
   129
  void emitCallThunks();
danderson@3977
   130
  void emitCheckAddress(Register reg);
danderson@3977
   131
  void emitErrorPath(Label *dest, int code);
danderson@3977
   132
  void emitErrorPaths();
danderson@3977
   133
danderson@3977
   134
 private:
danderson@3977
   135
  AssemblerX86 masm;
danderson@3977
   136
  BaseRuntime *rt_;
danderson@3977
   137
  const sp_plugin_t *plugin_;
danderson@3977
   138
  int error_;
danderson@3977
   139
  uint32_t pcode_start_;
danderson@3977
   140
  cell_t *code_start_;
danderson@3977
   141
  cell_t *cip_;
danderson@3977
   142
  cell_t *code_end_;
danderson@3977
   143
  Label *jump_map_;
danderson@3977
   144
danderson@3977
   145
  // Errors
danderson@3977
   146
  Label error_bounds_;
danderson@3977
   147
  Label error_heap_low_;
danderson@3977
   148
  Label error_heap_min_;
danderson@3977
   149
  Label error_stack_low_;
danderson@3977
   150
  Label error_stack_min_;
danderson@3977
   151
  Label error_divide_by_zero_;
danderson@3977
   152
  Label error_memaccess_;
danderson@3977
   153
  Label error_integer_overflow_;
danderson@3977
   154
  Label extern_error_;
danderson@3977
   155
danderson@3977
   156
  ke::Vector<CallThunk *> thunks_; //:TODO: free
dvander@157
   157
};
dvander@157
   158
dvander@2193
   159
class JITX86
dvander@157
   160
{
danderson@3977
   161
 public:
danderson@3977
   162
  JITX86();
danderson@3977
   163
danderson@3977
   164
 public:
danderson@3977
   165
  bool InitializeJIT();
danderson@3977
   166
  void ShutdownJIT();
danderson@3977
   167
  ICompilation *StartCompilation(BaseRuntime *runtime);
danderson@3977
   168
  ICompilation *StartCompilation();
danderson@3977
   169
  void SetupContextVars(BaseRuntime *runtime, BaseContext *pCtx, sp_context_t *ctx);
danderson@3977
   170
  void FreeContextVars(sp_context_t *ctx);
danderson@3977
   171
  SPVM_NATIVE_FUNC CreateFakeNative(SPVM_FAKENATIVE_FUNC callback, void *pData);
danderson@3977
   172
  void DestroyFakeNative(SPVM_NATIVE_FUNC func);
danderson@3977
   173
  JitFunction *CompileFunction(BaseRuntime *runtime, cell_t pcode_offs, int *err);
danderson@3977
   174
  ICompilation *ApplyOptions(ICompilation *_IN, ICompilation *_OUT);
danderson@3977
   175
  int InvokeFunction(BaseRuntime *runtime, JitFunction *fn, cell_t *result);
danderson@3977
   176
danderson@3977
   177
 public:
danderson@3977
   178
  ExternalAddress GetUniversalReturn() {
danderson@3977
   179
      return ExternalAddress(m_pJitReturn);
danderson@3977
   180
  }
danderson@3977
   181
  void *AllocCode(size_t size);
danderson@3977
   182
  void FreeCode(void *code);
danderson@3977
   183
danderson@3977
   184
 private:
danderson@3977
   185
  void *m_pJitEntry;         /* Entry function */
danderson@3977
   186
  void *m_pJitReturn;        /* Universal return address */
dvander@157
   187
};
dvander@157
   188
danderson@3977
   189
const Register pri = eax;
danderson@3977
   190
const Register alt = edx;
danderson@3977
   191
const Register stk = edi;
danderson@3977
   192
const Register dat = ebp;
danderson@3977
   193
const Register tmp = ecx;
danderson@3977
   194
const Register info = esi;
danderson@3977
   195
const Register frm = ebx;
danderson@3977
   196
dvander@3982
   197
struct InfoVars {
dvander@3982
   198
    ucell_t frm;
dvander@3982
   199
    ucell_t hp;
dvander@3982
   200
    cell_t *rval;
dvander@3982
   201
    ucell_t cip;
dvander@3982
   202
    void *esp;
dvander@3982
   203
};
dvander@3982
   204
dvander@3984
   205
#define AMX_INFO_FRAME          offsetof(InfoVars, frm)
dvander@3984
   206
#define AMX_INFO_HEAP           offsetof(InfoVars, hp)
dvander@3984
   207
#define AMX_INFO_RETVAL         offsetof(InfoVars, rval)
dvander@3984
   208
#define AMX_INFO_CIP            offsetof(InfoVars, cip)
dvander@3984
   209
#define AMX_INFO_NSTACK         offsetof(InfoVars, esp)
dvander@157
   210
dvander@2226
   211
extern Knight::KeCodeCache *g_pCodeCache;
dvander@2226
   212
extern JITX86 g_Jit;
dvander@157
   213
dvander@157
   214
#endif //_INCLUDE_SOURCEPAWN_JIT_X86_H_