فهرست منبع

use executable memory for jit

Nicolas Cannasse 10 سال پیش
والد
کامیت
5d7e59c54b
4فایلهای تغییر یافته به همراه19 افزوده شده و 4 حذف شده
  1. 12 0
      src/global.c
  2. 4 0
      src/hl.h
  3. 3 2
      src/jit.c
  4. 0 2
      src/main.c

+ 12 - 0
src/global.c

@@ -20,6 +20,9 @@
  * DEALINGS IN THE SOFTWARE.
  */
 #include "hl.h"
+#ifdef HL_WIN
+#	include <windows.h>
+#endif
 
 void hl_global_init() {
 }
@@ -91,3 +94,12 @@ void hl_free( hl_alloc *a ) {
 	if( (int_val)a < prev || (int_val)a > prev+size )
 		a->cur = NULL;
 }
+
+void *hl_alloc_executable_memory( int size ) {
+#ifdef HL_WIN
+	return VirtualAlloc(NULL,size,MEM_COMMIT,PAGE_EXECUTE_READWRITE);
+#else
+	printf("NOT IMPLEMENTED\n");
+	return NULL;
+#endif
+}

+ 4 - 0
src/hl.h

@@ -22,6 +22,10 @@
 #ifndef HL_H
 #define HL_H
 
+#ifdef _WIN32
+#	define HL_WIN
+#endif
+
 #if defined(__APPLE__) || defined(__MACH__) || defined(macintosh)
 #	define HL_MAC
 #endif

+ 3 - 2
src/jit.c

@@ -223,7 +223,6 @@ static void op_callr( jit_ctx *ctx, int r, int rf, int size ) {
 }
 
 static void op_enter( jit_ctx *ctx ) {
-	XRet();
 	XPush_r(Ebp);
 	XMov_rr(Ebp, Esp);
 	XAdd_rc(Esp, ctx->totalRegsSize);
@@ -413,9 +412,11 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 	return codePos;
 }
 
+void *hl_alloc_executable_memory( int size );
+
 void *hl_jit_code( jit_ctx *ctx ) {
 	int size = ctx->buf.b - ctx->startBuf;
-	void *code = malloc(size);
+	void *code = hl_alloc_executable_memory(size);
 	if( code == NULL ) return NULL;
 	memcpy(code,ctx->startBuf,size);
 	return code;

+ 0 - 2
src/main.c

@@ -60,9 +60,7 @@ int main( int argc, char *argv[] ) {
 		m = hl_module_alloc(code);
 		if( m == NULL )
 			return 4;
-		
 		((fptr)m->functions_ptrs[m->code->entrypoint])();
-
 		hl_module_free(m);
 		hl_free(&code->alloc);
 	}