sourcepawn/jit/x86/jit_x86.cpp
changeset 175 d8fc20f56ca0
parent 164 2d9741b7d92b
child 201 7cc14698f225
     1.1 --- a/sourcepawn/jit/x86/jit_x86.cpp	Sat Nov 11 01:19:46 2006 +0000
     1.2 +++ b/sourcepawn/jit/x86/jit_x86.cpp	Sat Nov 11 05:47:00 2006 +0000
     1.3 @@ -233,6 +233,7 @@
     1.4  	 * Just in case, we guard this memory with INT3 to break into the debugger.
     1.5  	 */
     1.6  	jitoffs_t cur_offs = jit->get_outputpos();
     1.7 +	CompData *co = (CompData *)jit->data;
     1.8  	if (cur_offs % 4)
     1.9  	{
    1.10  		cur_offs = 4 - (cur_offs % 4);
    1.11 @@ -240,11 +241,25 @@
    1.12  		{
    1.13  			jit->write_ubyte(IA32_INT3);
    1.14  		}
    1.15 -		/* add this amt to the offset we relocated */
    1.16 +	}
    1.17 +
    1.18 +	/* Write the info struct about this function */
    1.19 +	jit->write_uint32(JIT_FUNCMAGIC);
    1.20 +	jit->write_uint32(co->func_idx);
    1.21 +
    1.22 +	/* Now we have to backpatch our reloction offset! */
    1.23 +	{
    1.24  		jitoffs_t offs = jit->get_inputpos() - sizeof(cell_t);
    1.25  		jitcode_t rebase = ((CompData *)jit->data)->rebase;
    1.26  		*(jitoffs_t *)((unsigned char  *)rebase + offs) = jit->get_outputpos();
    1.27  	}
    1.28 +
    1.29 +	/* Lastly, if we're writing, keep track of the function count */
    1.30 +	if (jit->outbase)
    1.31 +	{
    1.32 +		co->func_idx++;
    1.33 +	}
    1.34 +
    1.35  	//push old frame on stack:
    1.36  	//mov ecx, [esi+frm]
    1.37  	//mov [edi-4], ecx
    1.38 @@ -1756,7 +1771,7 @@
    1.39  	return native->pfn(ctx, params);
    1.40  }
    1.41  
    1.42 -cell_t InvalidNative(sp_context_t *ctx, cell_t *params)
    1.43 +cell_t InvalidNative(sp_context_t *ctx, const cell_t *params)
    1.44  {
    1.45  	ctx->err = SP_ERROR_INVALID_NATIVE;
    1.46  
    1.47 @@ -1877,6 +1892,7 @@
    1.48  	JitWriter writer;
    1.49  	JitWriter *jit = &writer;
    1.50  	cell_t *endptr = (cell_t *)(end_cip);
    1.51 +	uint32_t codemem = 0;
    1.52  
    1.53  	/* Initial code is written "blank,"
    1.54  	 * so we can check the exact memory usage.
    1.55 @@ -1957,8 +1973,8 @@
    1.56  		WriteErrorRoutines(data, jit);
    1.57  
    1.58  		/* the total codesize is now known! */
    1.59 -		uint32_t mem = writer.get_outputpos();
    1.60 -		writer.outbase = (jitcode_t)engine->ExecAlloc(mem);
    1.61 +		codemem = writer.get_outputpos();
    1.62 +		writer.outbase = (jitcode_t)engine->ExecAlloc(codemem);
    1.63  		writer.outptr = writer.outbase;
    1.64  		/* go back for third pass */
    1.65  		goto jit_rewind;
    1.66 @@ -2100,6 +2116,11 @@
    1.67  	trk->pCur = trk->pBase;
    1.68  	trk->size = 1024 / sizeof(cell_t);
    1.69  
    1.70 +	functracker_t *fnc = new functracker_t;
    1.71 +	ctx->vm[JITVARS_FUNCINFO] = fnc;
    1.72 +	fnc->code_size = codemem;
    1.73 +	fnc->num_functions = data->func_idx;
    1.74 +
    1.75  	/* clean up relocation+compilation memory */
    1.76  	AbortCompilation(co);
    1.77  
    1.78 @@ -2172,3 +2193,38 @@
    1.79  
    1.80  	return false;
    1.81  }
    1.82 +
    1.83 +unsigned int JITX86::GetAPIVersion()
    1.84 +{
    1.85 +	return SOURCEPAWN_VM_API_VERSION;
    1.86 +}
    1.87 +
    1.88 +bool JITX86::FunctionLookup(const sp_context_t *ctx, uint32_t code_addr, unsigned int *result)
    1.89 +{
    1.90 +	functracker_t *fnc = (functracker_t *)ctx->vm[JITVARS_FUNCINFO];
    1.91 +
    1.92 +	if (code_addr >= fnc->code_size)
    1.93 +	{
    1.94 +		return false;
    1.95 +	}
    1.96 +
    1.97 +	funcinfo_t *f = (funcinfo_t *)((char *)ctx->codebase + code_addr - sizeof(funcinfo_t));
    1.98 +	if (f->magic != JIT_FUNCMAGIC || f->index >= fnc->num_functions)
    1.99 +	{
   1.100 +		return false;
   1.101 +	}
   1.102 +
   1.103 +	if (result)
   1.104 +	{
   1.105 +		*result = f->index;
   1.106 +	}
   1.107 +
   1.108 +	return true;
   1.109 +}
   1.110 +
   1.111 +unsigned int JITX86::FunctionCount(const sp_context_t *ctx)
   1.112 +{
   1.113 +	functracker_t *fnc = (functracker_t *)ctx->vm[JITVARS_FUNCINFO];
   1.114 +
   1.115 +	return fnc->num_functions;
   1.116 +}