Browse Source

fib() running

Nicolas Cannasse 10 years ago
parent
commit
6c1227e47b
6 changed files with 155 additions and 110 deletions
  1. 8 3
      src/global.c
  2. 77 69
      src/hl.h
  3. 23 14
      src/jit.c
  4. 3 3
      src/main.c
  5. 43 20
      src/module.c
  6. 1 1
      src/opcodes.h

+ 8 - 3
src/global.c

@@ -31,7 +31,6 @@ void hl_global_free() {
 }
 
 int hl_type_size( hl_type *t ) {
-#	define PTR 4
 	static int SIZES[] = {
 		4,
 		1,
@@ -39,12 +38,18 @@ int hl_type_size( hl_type *t ) {
 		4,
 		8,
 		1,
-		PTR * 2,
-		PTR,
+		HL_WSIZE,
+		HL_WSIZE,
 	};
 	return SIZES[t->kind];
 }
 
+int hl_word_size( hl_type *t ) {
+	int sz = hl_type_size(t);	
+	if( sz & (HL_WSIZE-1) ) sz += HL_WSIZE - (sz&(HL_WSIZE-1));
+	return sz;
+}
+
 struct hl_alloc_block {
 	int size;
 	hl_alloc_block *next;

+ 77 - 69
src/hl.h

@@ -57,85 +57,91 @@
 #	include <stdint.h>
 #endif
 
+#ifdef HL_64
+#	define	HL_WSIZE 8
+#else
+#	define	HL_WSIZE 4
+#endif
 
+typedef void (_cdecl *fptr)();
 typedef intptr_t int_val;
 
 #include <stdlib.h>
 #include <stdio.h>
-#include <memory.h>
+#include <memory.h>
 
-#define HL_VERSION	010
-#include "opcodes.h"
+#define HL_VERSION	010
+#include "opcodes.h"
 
 typedef struct hl_alloc_block hl_alloc_block;
 typedef struct { hl_alloc_block *cur; } hl_alloc;
 
-typedef enum {
-	HVOID	= 0,
-	HUI8	= 1,
-	HI32	= 2,
-	HF32	= 3,
-	HF64	= 4,
-	HBOOL	= 5,
-	HANY	= 6,
-	HFUN	= 7,
-	// ---------
-	HLAST	= 8
-} hl_type_kind;
-
-typedef struct hl_type hl_type;
-
-struct hl_type {
-	hl_type_kind kind;
-	int nargs;
-	hl_type **args;
-	hl_type *ret;
-};
-
-typedef struct {
-	const char *name;
-	int global;
-} hl_native;
-
-typedef struct {
-	hl_op op;
-	int p1;
-	int p2;
-	int p3;
-	void *extra;
-} hl_opcode;
-
-typedef struct hl_ptr_list hl_ptr_list;
-
-typedef struct {
-	int index;
-	int nregs;
-	int nops;
-	hl_type **regs;
-	hl_opcode *ops;
-} hl_function;
-
-typedef struct {
-	int version;
-	int nints;
-	int nfloats;
-	int nstrings;
-	int ntypes;
-	int nglobals;
-	int nnatives;
-	int nfunctions;
-	int entrypoint;
-	int*		ints;
-	double*		floats;
-	char**		strings;
-	char*		strings_data;
-	int*		strings_lens;
-	hl_type*	types;
-	hl_type**	globals;
-	hl_native*	natives;
-	hl_function*functions;
-	hl_alloc	alloc;
-} hl_code;
+typedef enum {
+	HVOID	= 0,
+	HUI8	= 1,
+	HI32	= 2,
+	HF32	= 3,
+	HF64	= 4,
+	HBOOL	= 5,
+	HANY	= 6,
+	HFUN	= 7,
+	// ---------
+	HLAST	= 8
+} hl_type_kind;
+
+typedef struct hl_type hl_type;
+
+struct hl_type {
+	hl_type_kind kind;
+	int nargs;
+	hl_type **args;
+	hl_type *ret;
+};
+
+typedef struct {
+	const char *name;
+	int global;
+} hl_native;
+
+typedef struct {
+	hl_op op;
+	int p1;
+	int p2;
+	int p3;
+	void *extra;
+} hl_opcode;
+
+typedef struct hl_ptr_list hl_ptr_list;
+
+typedef struct {
+	int index;
+	int nregs;
+	int nops;
+	hl_type **regs;
+	hl_opcode *ops;
+} hl_function;
+
+typedef struct {
+	int version;
+	int nints;
+	int nfloats;
+	int nstrings;
+	int ntypes;
+	int nglobals;
+	int nnatives;
+	int nfunctions;
+	int entrypoint;
+	int*		ints;
+	double*		floats;
+	char**		strings;
+	char*		strings_data;
+	int*		strings_lens;
+	hl_type*	types;
+	hl_type**	globals;
+	hl_native*	natives;
+	hl_function*functions;
+	hl_alloc	alloc;
+} hl_code;
 
 typedef struct {
 	hl_code *code;
@@ -156,6 +162,7 @@ void hl_global_init();
 void hl_global_free();
 
 int hl_type_size( hl_type *t );
+int hl_word_size( hl_type *t ); // same as hl_type_size, but round to the next word size
 
 hl_code *hl_code_read( const unsigned char *data, int size );
 void hl_code_free( hl_code *c );
@@ -166,6 +173,7 @@ void hl_module_free( hl_module *m );
 jit_ctx *hl_jit_alloc();
 void hl_jit_free( jit_ctx *ctx );
 int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f );
+int hl_module_init( hl_module *m );
 void *hl_jit_code( jit_ctx *ctx );
 
 #endif

+ 23 - 14
src/jit.c

@@ -149,8 +149,8 @@
 
 #define TODO()					printf("TODO(jit.c:%d)\n",__LINE__)
 
-#define LOAD(cpuReg,vReg)		XMov_rp(cpuReg,Ebp,-ctx->regsPos[vReg])
-#define STORE(vReg, cpuReg)		XMov_pr(Ebp,-ctx->regsPos[vReg],cpuReg)
+#define LOAD(cpuReg,vReg)		XMov_rp(cpuReg,Ebp,ctx->regsPos[vReg])
+#define STORE(vReg, cpuReg)		XMov_pr(Ebp,ctx->regsPos[vReg],cpuReg)
 
 typedef struct jlist jlist;
 struct jlist {
@@ -203,16 +203,16 @@ static void op_mov( jit_ctx *ctx, int to, int from ) {
 }
 
 static void op_movc( jit_ctx *ctx, int to, void *value ) {
-	XMov_pc(Ebp,-ctx->regsPos[to],*(int*)value);
+	XMov_pc(Ebp,ctx->regsPos[to],*(int*)value);
 }
 
 static void op_mova( jit_ctx *ctx, int to, void *value ) {
-	XMov_ra(Eax,(int)value);
+	XMov_ra(Eax,(int_val)value);
 	STORE(to, Eax);
 }
 
 static void op_pushr( jit_ctx *ctx, int r ) {
-	XPush_p(Ebp,-ctx->regsPos[r]);
+	XPush_p(Ebp,ctx->regsPos[r]);
 }
 
 static void op_callr( jit_ctx *ctx, int r, int rf, int size ) {
@@ -225,12 +225,12 @@ static void op_callr( jit_ctx *ctx, int r, int rf, int size ) {
 static void op_enter( jit_ctx *ctx ) {
 	XPush_r(Ebp);
 	XMov_rr(Ebp, Esp);
-	XAdd_rc(Esp, ctx->totalRegsSize);
+	XSub_rc(Esp, ctx->totalRegsSize);
 }
 
 static void op_ret( jit_ctx *ctx, int r ) {
 	LOAD(Eax, r);
-	XSub_rc(Esp, ctx->totalRegsSize);
+	XAdd_rc(Esp, ctx->totalRegsSize);
 	XPop_r(Ebp);
 	XRet();
 }
@@ -277,10 +277,10 @@ static void op_cmp( jit_ctx *ctx, hl_opcode *op ) {
 	LOAD(Ecx, op->p3);
 	XCmp_rr(Eax, Ecx);
 	p = do_jump(ctx,op->op);
-	XMov_pc(Ebp,-ctx->regsPos[op->p1],0);
+	XMov_pc(Ebp,ctx->regsPos[op->p1],0);
 	e = do_jump(ctx,OJAlways);
-	XMov_pc(Ebp,-ctx->regsPos[op->p1],1);
 	patch_jump(ctx,p);
+	XMov_pc(Ebp,ctx->regsPos[op->p1],1);
 	patch_jump(ctx,e);
 }
 
@@ -313,6 +313,7 @@ void hl_jit_free( jit_ctx *ctx ) {
 int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 	int i, j, size = 0;
 	int codePos = ctx->buf.b - ctx->startBuf;
+	int nargs = m->code->globals[f->index]->nargs;
 	ctx->m = m;
 	ctx->f = f;
 	if( f->nregs > ctx->maxRegs ) {
@@ -335,10 +336,18 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 		}
 		ctx->maxOps = f->nops;
 	}
-	for(i=0;i<f->nregs;i++) {
-		int sz = hl_type_size(f->regs[i]);
+	size = HL_WSIZE;
+	for(i=0;i<nargs;i++) {
+		int sz = hl_word_size(f->regs[i]);
 		ctx->regsSize[i] = sz;
+		size += sz;
 		ctx->regsPos[i] = size;
+	}
+	size = 0;
+	for(i=nargs;i<f->nregs;i++) {
+		int sz = hl_word_size(f->regs[i]);
+		ctx->regsSize[i] = sz;
+		ctx->regsPos[i] = -(size + HL_WSIZE);
 		size += sz;
 	}
 	ctx->totalRegsSize = size;
@@ -386,8 +395,8 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 			XJump(JZero,jump);
 			register_jump(ctx,jump,(i + 1) + o->p2);
 			break;
-		case OJToAny:
-			// NOP
+		case OToAny:
+			op_mov(ctx,o->p1,o->p2); // TODO
 			break;
 		case ORet:
 			op_ret(ctx, o->p1);
@@ -402,7 +411,7 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 	{
 		jlist *j = ctx->jumps;
 		while( j ) {
-			*(int*)(ctx->startBuf + j->pos) = ctx->opsPos[j->target] - j->pos;
+			*(int*)(ctx->startBuf + j->pos) = ctx->opsPos[j->target] - j->pos - 10;
 			j = j->next;
 		}
 		ctx->jumps = NULL;

+ 3 - 3
src/main.c

@@ -21,8 +21,6 @@
  */
 #include "hl.h"
 
-typedef void (_cdecl *fptr)();
-
 int main( int argc, char *argv[] ) {
 	if( argc == 1 ) {
 		printf("HLVM %d.%d.%d (c)2015 Haxe Foundation\n  Usage : hl <file>\n",HL_VERSION/100,(HL_VERSION/10)%10,HL_VERSION%10);
@@ -60,7 +58,9 @@ int main( int argc, char *argv[] ) {
 		m = hl_module_alloc(code);
 		if( m == NULL )
 			return 4;
-		((fptr)m->functions_ptrs[m->code->entrypoint])();
+		if( !hl_module_init(m) )
+			return 5;
+		(*(fptr*)(m->globals_data + m->globals_indexes[m->code->entrypoint]))();
 		hl_module_free(m);
 		hl_free(&code->alloc);
 	}

+ 43 - 20
src/module.c

@@ -50,29 +50,52 @@ hl_module *hl_module_alloc( hl_code *c ) {
 		return NULL;
 	}
 	memset(m->functions_ptrs,0,sizeof(void*)*c->nfunctions);
-	{
-		jit_ctx *ctx = hl_jit_alloc();
-		if( ctx == NULL ) {
-			hl_module_free(m);
-			return NULL;
-		}
-		for(i=0;i<c->nfunctions;i++) {
-			int f = hl_jit_function(ctx, m, c->functions+i);
-			if( f < 0 ) {
-				printf("Failed to JIT fun#%d\n",i);
-				hl_module_free(m);
-				return NULL;
-			}
-			m->functions_ptrs[i] = (void*)f;
-		}
-		m->jit_code = hl_jit_code(ctx);
-		for(i=0;i<c->nfunctions;i++)
-			m->functions_ptrs[i] = ((unsigned char*)m->jit_code) + ((int_val)m->functions_ptrs[i]);
-		hl_jit_free(ctx);
-	}
 	return m;
 }
 
+static void null_function() {
+	// TODO : throw an error instead
+	printf("Null function ptr\n");
+}
+
+static void do_log( int i ) {
+	printf("%d\n",i);
+}
+
+int hl_module_init( hl_module *m ) {
+	int i;
+	jit_ctx *ctx;
+	// JIT
+	ctx = hl_jit_alloc();
+	if( ctx == NULL )
+		return 0;
+	for(i=0;i<m->code->nfunctions;i++) {
+		int f = hl_jit_function(ctx, m, m->code->functions+i);
+		if( f < 0 ) return 0;
+		m->functions_ptrs[i] = (void*)f;
+	}
+	m->jit_code = hl_jit_code(ctx);
+	for(i=0;i<m->code->nfunctions;i++)
+		m->functions_ptrs[i] = ((unsigned char*)m->jit_code) + ((int_val)m->functions_ptrs[i]);
+	hl_jit_free(ctx);
+	// INIT globals
+	for(i=0;i<m->code->nglobals;i++) {
+		hl_type *t = m->code->globals[i];
+		if( t->kind == HFUN ) *(fptr*)(m->globals_data + m->globals_indexes[i]) = null_function;
+	}
+	// INIT functions
+	for(i=0;i<m->code->nfunctions;i++) {
+		hl_function *f = m->code->functions + i;
+		*(void**)(m->globals_data + m->globals_indexes[f->index]) = m->functions_ptrs[i];
+	}
+	// INIT natives
+	for(i=0;i<m->code->nnatives;i++) {
+		hl_native *n = m->code->natives + i;
+		*(void**)(m->globals_data + m->globals_indexes[n->global]) = do_log;
+	}
+	return 1;
+}
+
 void hl_module_free( hl_module *m ) {
 	free(m->globals_indexes);
 	free(m->globals_data);

+ 1 - 1
src/opcodes.h

@@ -54,7 +54,7 @@ OP_BEGIN
 	OP(OJNull,2)
 	OP(OJNotNull,2)
 	OP(OJAlways,1)
-	OP(OJToAny,2)
+	OP(OToAny,2)
 	// --
 	OP(OLast,0)
 OP_END