From 8099cb09bd993c89dd47e5b9e85b391ca520b93e Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sat, 4 Nov 2006 18:54:03 +0000 Subject: [PATCH] made these names a little nicer --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40140 --- sourcepawn/include/sp_vm_types.h | 7 +- sourcepawn/vm/jit/x86/jit_x86.cpp | 20 ++--- sourcepawn/vm/jit/x86/opcode_helpers.cpp | 2 +- sourcepawn/vm/sp_vm_basecontext.cpp | 22 ++--- sourcepawn/vm/test_main.cpp | 110 ----------------------- 5 files changed, 26 insertions(+), 135 deletions(-) delete mode 100644 sourcepawn/vm/test_main.cpp diff --git a/sourcepawn/include/sp_vm_types.h b/sourcepawn/include/sp_vm_types.h index e6973b74..5dff68ca 100644 --- a/sourcepawn/include/sp_vm_types.h +++ b/sourcepawn/include/sp_vm_types.h @@ -218,9 +218,10 @@ typedef struct sp_context_s uint32_t flags; /* compilation flags */ SPVM_DEBUGBREAK dbreak; /* debug break function */ /* context runtime information */ - ucell_t memory; /* total memory size; */ - uint8_t *data; /* data chunk */ - cell_t heapbase; /* heap base */ + uint8_t *memory; /* data chunk */ + ucell_t mem_size; /* total memory size; */ + cell_t data_size; /* data chunk size, always starts at 0 */ + cell_t heap_base; /* where the heap starts */ /* execution specific data */ cell_t hp; /* heap pointer */ cell_t sp; /* stack pointer */ diff --git a/sourcepawn/vm/jit/x86/jit_x86.cpp b/sourcepawn/vm/jit/x86/jit_x86.cpp index ca066953..8898563d 100644 --- a/sourcepawn/vm/jit/x86/jit_x86.cpp +++ b/sourcepawn/vm/jit/x86/jit_x86.cpp @@ -1712,7 +1712,7 @@ cell_t NativeCallback_Debug(sp_context_t *ctx, ucell_t native_idx, cell_t *param cell_t save_sp = ctx->sp; cell_t save_hp = ctx->hp; - if (ctx->hp < ctx->heapbase) + if (ctx->hp < ctx->heap_base) { ctx->err = SP_ERROR_HEAPMIN; return 0; @@ -1724,7 +1724,7 @@ cell_t NativeCallback_Debug(sp_context_t *ctx, ucell_t native_idx, cell_t *param return 0; } - if ((uint32_t)ctx->sp >= ctx->memory) + if ((uint32_t)ctx->sp >= ctx->mem_size) { ctx->err = SP_ERROR_STACKMIN; return 0; @@ -1936,12 +1936,12 @@ jit_rewind: ctx->flags = (data->debug ? SPFLAG_PLUGIN_DEBUG : 0); /* setup memory */ - ctx->data = new uint8_t[plugin->memory]; - memcpy(ctx->data, plugin->data, plugin->data_size); - ctx->memory = plugin->memory; - ctx->heapbase = plugin->data_size; - ctx->hp = ctx->heapbase; - ctx->sp = ctx->memory - sizeof(cell_t); + ctx->memory = new uint8_t[plugin->memory]; + memcpy(ctx->memory, plugin->data, plugin->data_size); + ctx->mem_size = plugin->memory; + ctx->heap_base = plugin->data_size; + ctx->hp = ctx->heap_base; + ctx->sp = ctx->mem_size - sizeof(cell_t); const char *strbase = plugin->info.stringbase; uint32_t max, iter; @@ -1960,7 +1960,7 @@ jit_rewind: /* relocate pubvar info */ if ((max = plugin->info.pubvars_num)) { - uint8_t *dat = ctx->data; + uint8_t *dat = ctx->memory; ctx->pubvars = new sp_pubvar_t[max]; for (iter=0; iterExecFree(ctx->codebase); - delete [] ctx->data; + delete [] ctx->memory; delete [] ctx->files; delete [] ctx->lines; delete [] ctx->natives; diff --git a/sourcepawn/vm/jit/x86/opcode_helpers.cpp b/sourcepawn/vm/jit/x86/opcode_helpers.cpp index 555b3605..1aa0d670 100644 --- a/sourcepawn/vm/jit/x86/opcode_helpers.cpp +++ b/sourcepawn/vm/jit/x86/opcode_helpers.cpp @@ -52,7 +52,7 @@ jitoffs_t Write_Execute_Function(JitWriter *jit) IA32_Mov_Rm_Reg_Disp8(jit, AMX_REG_INFO, REG_EAX, AMX_INFO_CONTEXT); IA32_Mov_Reg_Rm_Disp8(jit, REG_ECX, REG_EAX, offsetof(sp_context_t, hp)); IA32_Mov_Rm_Reg_Disp8(jit, AMX_REG_INFO, REG_ECX, AMX_INFO_HEAP); - IA32_Mov_Reg_Rm_Disp8(jit, AMX_REG_DAT, REG_EAX, offsetof(sp_context_t, data)); + IA32_Mov_Reg_Rm_Disp8(jit, AMX_REG_DAT, REG_EAX, offsetof(sp_context_t, memory)); /* Frame setup */ //mov edi, [eax+] - get stack pointer diff --git a/sourcepawn/vm/sp_vm_basecontext.cpp b/sourcepawn/vm/sp_vm_basecontext.cpp index 34c38b23..f147aa1b 100644 --- a/sourcepawn/vm/sp_vm_basecontext.cpp +++ b/sourcepawn/vm/sp_vm_basecontext.cpp @@ -113,7 +113,7 @@ int BaseContext::HeapAlloc(unsigned int cells, cell_t *local_addr, cell_t **phys return SP_ERROR_HEAPLOW; } - addr = (cell_t *)(ctx->data + ctx->hp); + addr = (cell_t *)(ctx->memory + ctx->hp); /* store size of allocation in cells */ *addr = (cell_t)cells; addr++; @@ -138,12 +138,12 @@ int BaseContext::HeapPop(cell_t local_addr) /* check the bounds of this address */ local_addr -= sizeof(cell_t); - if (local_addr < ctx->heapbase || local_addr >= ctx->sp) + if (local_addr < ctx->heap_base || local_addr >= ctx->sp) { return SP_ERROR_INVALID_ADDRESS; } - addr = (cell_t *)(ctx->data + local_addr); + addr = (cell_t *)(ctx->memory + local_addr); cellcount = (*addr) * sizeof(cell_t); /* check if this memory count looks valid */ if (ctx->hp - cellcount - sizeof(cell_t) != local_addr) @@ -159,7 +159,7 @@ int BaseContext::HeapPop(cell_t local_addr) int BaseContext::HeapRelease(cell_t local_addr) { - if (local_addr < ctx->heapbase) + if (local_addr < ctx->heap_base) { return SP_ERROR_INVALID_ADDRESS; } @@ -394,14 +394,14 @@ int BaseContext::BindNativeToAny(SPVM_NATIVE_FUNC native) int BaseContext::LocalToPhysAddr(cell_t local_addr, cell_t **phys_addr) { - if (((local_addr >= ctx->hp) && (local_addr < ctx->sp)) || (local_addr < 0) || ((ucell_t)local_addr >= ctx->memory)) + if (((local_addr >= ctx->hp) && (local_addr < ctx->sp)) || (local_addr < 0) || ((ucell_t)local_addr >= ctx->mem_size)) { return SP_ERROR_INVALID_ADDRESS; } if (phys_addr) { - *phys_addr = (cell_t *)(ctx->data + local_addr); + *phys_addr = (cell_t *)(ctx->memory + local_addr); } return SP_ERROR_NONE; @@ -415,7 +415,7 @@ int BaseContext::PushCell(cell_t value) } ctx->sp -= sizeof(cell_t); - *(cell_t *)(ctx->data + ctx->sp) = value; + *(cell_t *)(ctx->memory + ctx->sp) = value; ctx->pushcount++; return SP_ERROR_NONE; @@ -470,12 +470,12 @@ int BaseContext::LocalToString(cell_t local_addr, char *buffer, size_t maxlength int len = 0; cell_t *src; - if (((local_addr >= ctx->hp) && (local_addr < ctx->sp)) || (local_addr < 0) || ((ucell_t)local_addr >= ctx->memory)) + if (((local_addr >= ctx->hp) && (local_addr < ctx->sp)) || (local_addr < 0) || ((ucell_t)local_addr >= ctx->mem_size)) { return SP_ERROR_INVALID_ADDRESS; } - src = (cell_t *)(ctx->data + local_addr); + src = (cell_t *)(ctx->memory + local_addr); while ((*src != '\0') && ((size_t)len < maxlength)) { buffer[len++] = (char)*src++; @@ -534,13 +534,13 @@ int BaseContext::StringToLocal(cell_t local_addr, size_t chars, const char *sour cell_t *dest; int i, len; - if (((local_addr >= ctx->hp) && (local_addr < ctx->sp)) || (local_addr < 0) || ((ucell_t)local_addr >= ctx->memory)) + if (((local_addr >= ctx->hp) && (local_addr < ctx->sp)) || (local_addr < 0) || ((ucell_t)local_addr >= ctx->mem_size)) { return SP_ERROR_INVALID_ADDRESS; } len = strlen(source); - dest = (cell_t *)(ctx->data + local_addr); + dest = (cell_t *)(ctx->memory + local_addr); if ((size_t)len >= chars) { diff --git a/sourcepawn/vm/test_main.cpp b/sourcepawn/vm/test_main.cpp deleted file mode 100644 index c22c7dba..00000000 --- a/sourcepawn/vm/test_main.cpp +++ /dev/null @@ -1,110 +0,0 @@ -#include -#include -#include -#include "sp_vm_engine.h" -#include "sp_vm_basecontext.h" -#define WINDOWS_LEAN_AND_MEAN -#include - -using namespace SourcePawn; - -typedef void (*GIVEENGINE)(ISourcePawnEngine *); -typedef IVirtualMachine *(*GETEXPORT)(unsigned int); - -cell_t TestNative(sp_context_t *ctx, cell_t *params) -{ - IPluginContext *pl = ctx->context; - cell_t *phys; - int err; - printf("params[0] = %d\n", params[0]); - printf("params[1] = %d\n", params[1]); - if ((err=pl->LocalToPhysAddr(params[2], &phys)) != SP_ERROR_NONE) - { - ctx->err = err; - return 0; - } - printf("params[2] %c%c%c%c%c\n", phys[0], phys[1], phys[2], phys[3], phys[4]); - if ((err=pl->LocalToPhysAddr(params[3], &phys)) != SP_ERROR_NONE) - { - ctx->err = err; - return 0; - } - printf("params[3] = %d\n", *phys); - *phys = 95; - return 5; -} - -int main() -{ - SourcePawnEngine engine; - IVirtualMachine *vm; - ICompilation *co; - sp_plugin_t *plugin = NULL; - sp_context_t *ctx = NULL; - cell_t result; - int err; - - /* Note: for now this is hardcoded for testing purposes! - * The ..\ should be one above msvc8. - */ - - FILE *fp = fopen("..\\test.smx", "rb"); - if (!fp) - { - return 0; - } - plugin = engine.LoadFromFilePointer(fp, &err); - if (err != SP_ERROR_NONE) - { - printf("Error loading: %d", err); - return 0; - } - HMODULE dll = LoadLibrary("..\\jit\\x86\\msvc8\\Debug\\jit-x86.dll"); - if (dll == NULL) - { - printf("Error loading DLL: %d\n", GetLastError()); - return 0; - } - GIVEENGINE give_eng = (GIVEENGINE)GetProcAddress(dll, "GiveEnginePointer"); - if (!give_eng) - { - printf("Error getting \"GiveEnginePointer!\"!\n"); - return 0; - } - give_eng(&engine); - GETEXPORT getex = (GETEXPORT)GetProcAddress(dll, "GetExport"); - if ((vm=getex(0)) == NULL) - { - printf("GetExport() returned no VM!\n"); - return 0; - } - - co = vm->StartCompilation(plugin); - if ((ctx = vm->CompileToContext(co, &err)) == NULL) - { - printf("CompileToContext() failed: %d", err); - return 0; - } - - IPluginContext *base = engine.CreateBaseContext(ctx); - - sp_nativeinfo_t mynative; - mynative.name = "gaben"; - mynative.func = TestNative; - - if ((err=base->BindNative(&mynative, SP_NATIVE_OKAY)) != SP_ERROR_NONE) - { - printf("BindNative() failed: %d", err); - return 0; - } - - base->PushCell(1); - base->PushCell(4); - err = base->Execute(0, &result); - printf("Result: %d Error: %d\n", result, err); - - engine.FreeBaseContext(base); - - fclose(fp); - FreeLibrary(dll); -}