فهرست منبع

64bit very partial support

Nicolas Cannasse 10 سال پیش
والد
کامیت
76529a2608
7فایلهای تغییر یافته به همراه122 افزوده شده و 25 حذف شده
  1. 1 0
      .gitignore
  2. 6 0
      hl.sln
  3. 63 0
      hl.vcxproj
  4. 9 9
      src/global.c
  5. 2 2
      src/hl.h
  6. 40 13
      src/jit.c
  7. 1 1
      src/module.c

+ 1 - 0
.gitignore

@@ -11,3 +11,4 @@
 
 *.user
 Release
+x64

+ 6 - 0
hl.sln

@@ -6,13 +6,19 @@ EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Win32 = Debug|Win32
+		Debug|x64 = Debug|x64
 		Release|Win32 = Release|Win32
+		Release|x64 = Release|x64
 	EndGlobalSection
 	GlobalSection(ProjectConfigurationPlatforms) = postSolution
 		{80755B1F-41F6-4C73-B3F3-8325AEAE1016}.Debug|Win32.ActiveCfg = Debug|Win32
 		{80755B1F-41F6-4C73-B3F3-8325AEAE1016}.Debug|Win32.Build.0 = Debug|Win32
+		{80755B1F-41F6-4C73-B3F3-8325AEAE1016}.Debug|x64.ActiveCfg = Debug|x64
+		{80755B1F-41F6-4C73-B3F3-8325AEAE1016}.Debug|x64.Build.0 = Debug|x64
 		{80755B1F-41F6-4C73-B3F3-8325AEAE1016}.Release|Win32.ActiveCfg = Release|Win32
 		{80755B1F-41F6-4C73-B3F3-8325AEAE1016}.Release|Win32.Build.0 = Release|Win32
+		{80755B1F-41F6-4C73-B3F3-8325AEAE1016}.Release|x64.ActiveCfg = Release|x64
+		{80755B1F-41F6-4C73-B3F3-8325AEAE1016}.Release|x64.Build.0 = Release|x64
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 63 - 0
hl.vcxproj

@@ -5,10 +5,18 @@
       <Configuration>Debug</Configuration>
       <Platform>Win32</Platform>
     </ProjectConfiguration>
+    <ProjectConfiguration Include="Debug|x64">
+      <Configuration>Debug</Configuration>
+      <Platform>x64</Platform>
+    </ProjectConfiguration>
     <ProjectConfiguration Include="Release|Win32">
       <Configuration>Release</Configuration>
       <Platform>Win32</Platform>
     </ProjectConfiguration>
+    <ProjectConfiguration Include="Release|x64">
+      <Configuration>Release</Configuration>
+      <Platform>x64</Platform>
+    </ProjectConfiguration>
   </ItemGroup>
   <PropertyGroup Label="Globals">
     <ProjectGuid>{80755B1F-41F6-4C73-B3F3-8325AEAE1016}</ProjectGuid>
@@ -21,28 +29,53 @@
     <UseDebugLibraries>true</UseDebugLibraries>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>true</UseDebugLibraries>
+    <CharacterSet>Unicode</CharacterSet>
+    <PlatformToolset>Windows7.1SDK</PlatformToolset>
+  </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
     <ConfigurationType>Application</ConfigurationType>
     <UseDebugLibraries>false</UseDebugLibraries>
     <WholeProgramOptimization>true</WholeProgramOptimization>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>false</UseDebugLibraries>
+    <WholeProgramOptimization>true</WholeProgramOptimization>
+    <CharacterSet>Unicode</CharacterSet>
+    <PlatformToolset>Windows7.1SDK</PlatformToolset>
+  </PropertyGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
   <ImportGroup Label="ExtensionSettings">
   </ImportGroup>
   <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
     <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
   </ImportGroup>
+  <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
   <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
     <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
   </ImportGroup>
+  <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
   <PropertyGroup Label="UserMacros" />
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
     <LinkIncremental>true</LinkIncremental>
   </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+    <LinkIncremental>true</LinkIncremental>
+  </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
     <LinkIncremental>false</LinkIncremental>
   </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+    <LinkIncremental>false</LinkIncremental>
+  </PropertyGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
     <ClCompile>
       <PrecompiledHeader>
@@ -56,6 +89,19 @@
       <GenerateDebugInformation>true</GenerateDebugInformation>
     </Link>
   </ItemDefinitionGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+    <ClCompile>
+      <PrecompiledHeader>
+      </PrecompiledHeader>
+      <WarningLevel>Level3</WarningLevel>
+      <Optimization>Disabled</Optimization>
+      <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+    </Link>
+  </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
     <ClCompile>
       <WarningLevel>Level3</WarningLevel>
@@ -73,6 +119,23 @@
       <OptimizeReferences>true</OptimizeReferences>
     </Link>
   </ItemDefinitionGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <PrecompiledHeader>
+      </PrecompiledHeader>
+      <Optimization>MaxSpeed</Optimization>
+      <FunctionLevelLinking>true</FunctionLevelLinking>
+      <IntrinsicFunctions>true</IntrinsicFunctions>
+      <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+      <EnableCOMDATFolding>true</EnableCOMDATFolding>
+      <OptimizeReferences>true</OptimizeReferences>
+    </Link>
+  </ItemDefinitionGroup>
   <ItemGroup>
     <ClCompile Include="src\code.c" />
     <ClCompile Include="src\global.c" />

+ 9 - 9
src/global.c

@@ -32,14 +32,14 @@ void hl_global_free() {
 
 int hl_type_size( hl_type *t ) {
 	static int SIZES[] = {
-		4,
-		1,
-		4,
-		4,
-		8,
-		1,
-		HL_WSIZE,
-		HL_WSIZE,
+		4, // VOID
+		1, // UI8
+		4, // UI32
+		4, // F32
+		8, // F64
+		1, // BOOL
+		HL_WSIZE, // ANY
+		HL_WSIZE, // FUN
 	};
 	return SIZES[t->kind];
 }
@@ -90,7 +90,7 @@ void hl_free( hl_alloc *a ) {
 	int size = 0;
 	while( b ) {
 		hl_alloc_block *n = b->next;
-		size = b->p + b->size - ((unsigned char*)b);
+		size = (int)(b->p + b->size - ((unsigned char*)b));
 		prev = (int_val)b;
 		free(b);
 		b = n;

+ 2 - 2
src/hl.h

@@ -38,8 +38,8 @@
 #	define HL_BSD
 #endif
 
-#if defined(_64BITS) || defined(__x86_64__)
-#	define HL64
+#if defined(_64BITS) || defined(__x86_64__) || defined(_M_X64)
+#	define HL_64
 #endif
 
 #if defined(__GNUC__)

+ 40 - 13
src/jit.c

@@ -46,6 +46,11 @@
 #define JCarry		0x82
 #define B(bv)	*ctx->buf.b++ = bv
 #define W(wv)	*ctx->buf.w++ = wv
+#define W64(wv)	*ctx->buf.w64++ = wv
+#define REX_W	(64|8)
+#define REX_R	(64|4)
+#define REX_X	(64|2)
+#define REX_B	(64|1)
 #define MOD_RM(mod,reg,rm)		B((mod << 6) | (reg << 3) | rm)
 #define IS_SBYTE(c)				( (c) >= -128 && (c) < 128 )
 #define OP_RM(op,mod,reg,rm)	{ B(op); MOD_RM(mod,reg,rm); }
@@ -57,9 +62,15 @@
 								else W(addr); }
 #define XRet()					B(0xC3)
 #define XMov_rr(dst,src)		OP_RM(0x8B,3,dst,src)
-#define XMov_rc(dst,cst)		B(0xB8+(dst)); W(cst)
+
+#define XMov_rc32(dst,cst)		B(0xB8+(dst)); W(cst)
+#define XMov_rc64(dst,cst)		B(REX_W); B(0xB8+(dst)); W64(cst)
+#define XMov_ra32(dst,addr)		OP_RM(0x8B,0,dst,5); W(addr)
+#define XMov_ra64(dst,addr)		B(REX_W); OP_RM(0x8B,0,dst,5); W64(addr)
+
 #define XMov_rp(dst,reg,idx)	OP_ADDR(0x8B,idx,reg,dst)
-#define XMov_ra(dst,addr)		OP_RM(0x8B,0,dst,5); W(addr)
+#define XMov_rp64(dst,reg,idx)	B(REX_W); OP_ADDR(0x8B,idx,reg,dst)
+
 #define XMov_rx(dst,r,idx,mult) OP_RM(0x8B,0,dst,4); SIB(Mult##mult,idx,r)
 #define XMov_pr(dst,idx,src)	OP_ADDR(0x89,idx,dst,src)
 #define XMov_pc(dst,idx,c)		OP_ADDR(0xC7,idx,dst,0); W(c)
@@ -153,6 +164,16 @@
 #define LOAD(cpuReg,vReg)		XMov_rp(cpuReg,Ebp,ctx->regsPos[vReg])
 #define STORE(vReg, cpuReg)		XMov_pr(Ebp,ctx->regsPos[vReg],cpuReg)
 
+#define BUF_POS()				((int)(ctx->buf.b - ctx->startBuf))
+
+#ifdef HL_64
+#	define XMov_rc	XMov_rc64
+#	define XMov_ra	XMov_ra64
+#else
+#	define XMov_rc	XMov_rc32
+#	define XMov_ra	XMov_ra32
+#endif
+
 typedef struct jlist jlist;
 struct jlist {
 	int pos;
@@ -164,6 +185,7 @@ struct jit_ctx {
 	union {
 		unsigned char *b;
 		unsigned int *w;
+		unsigned long long *w64;
 		int *i;
 	} buf;
 	int *regsPos;
@@ -185,12 +207,12 @@ struct jit_ctx {
 };
 
 static void jit_buf( jit_ctx *ctx ) {
-	if( ctx->buf.b - ctx->startBuf > ctx->bufSize - MAX_OP_SIZE ) {
+	if( BUF_POS() > ctx->bufSize - MAX_OP_SIZE ) {
 		int nsize = ctx->bufSize ? (ctx->bufSize * 4) / 3 : ctx->f->nops * 4;
 		unsigned char *nbuf;
 		int curpos;
 		if( nsize < ctx->bufSize + MAX_OP_SIZE * 4 ) nsize = ctx->bufSize + MAX_OP_SIZE * 4;
-		curpos = ctx->buf.b - ctx->startBuf;
+		curpos = BUF_POS();
 		nbuf = (unsigned char*)malloc(nsize);
 		// TODO : check nbuf
 		if( ctx->startBuf ) {
@@ -237,14 +259,19 @@ static void op_callg( jit_ctx *ctx, int r, int g, int size ) {
 		XMov_ra(Eax, (int_val)(ctx->m->globals_data + ctx->m->globals_indexes[g]));
 		XCall_r(Eax);
 	} else if( fid >= ctx->m->code->nfunctions ) {
+#		ifdef HL_64
+		// TODO ! native x64 calling convention are not __cdecl !
+		// args needs to be passed in registers
+		XMov_rp64(Ecx, Esp, 0);
+#		endif
 		// native function, already resolved
 		XMov_rc(Eax, *(int_val*)(ctx->m->globals_data + ctx->m->globals_indexes[g]));
 		XCall_r(Eax);
 	} else {
-		int cpos = ctx->buf.b - ctx->startBuf;
+		int cpos = BUF_POS();
 		if( ctx->m->functions_ptrs[fid] ) {
 			// already compiled
-			XCall_d((int_val)ctx->m->functions_ptrs[fid] - (cpos + 5));
+			XCall_d((int)ctx->m->functions_ptrs[fid] - (cpos + 5));
 		} else if( ctx->m->code->functions + fid == ctx->f ) {
 			// our current function
 			XCall_d(ctx->functionPos - (cpos + 5));
@@ -338,7 +365,7 @@ static void op_cmp( jit_ctx *ctx, hl_opcode *op ) {
 }
 
 static void register_jump( jit_ctx *ctx, int *p, int target ) {
-	int pos = (int_val)p - (int_val)ctx->startBuf; 
+	int pos = (int)((int_val)p - (int_val)ctx->startBuf); 
 	jlist *j = (jlist*)hl_malloc(&ctx->falloc, sizeof(jlist));
 	j->pos = pos;
 	j->target = target;
@@ -377,7 +404,7 @@ int pad_stack( jit_ctx *ctx, int size ) {
 
 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 codePos = BUF_POS();
 	int nargs = m->code->globals[f->global]->nargs;
 	ctx->m = m;
 	ctx->f = f;
@@ -425,7 +452,7 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 	}
 	ctx->totalRegsSize = size;
 	jit_buf(ctx);
-	ctx->functionPos = ctx->buf.b - ctx->startBuf;
+	ctx->functionPos = BUF_POS();
 	op_enter(ctx);
 	ctx->opsPos[0] = 0;
 	for(i=0;i<f->nops;i++) {
@@ -517,7 +544,7 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 			printf("Don't know how to jit %s(%d)\n",hl_op_name(o->op),o->op);
 			return -1;
 		}
-		ctx->opsPos[i+1] = ctx->buf.b - ctx->startBuf;
+		ctx->opsPos[i+1] = BUF_POS();
 	}
 	// patch jumps
 	{
@@ -529,7 +556,7 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 		ctx->jumps = NULL;
 	}
 	// add nops padding
-	while( (ctx->buf.b - ctx->startBuf) & 15 )
+	while( BUF_POS() & 15 )
 		XNop();
 	// reset tmp allocator
 	hl_free(&ctx->falloc);
@@ -540,7 +567,7 @@ void *hl_alloc_executable_memory( int size );
 
 void *hl_jit_code( jit_ctx *ctx, hl_module *m ) {
 	jlist *c;
-	int size = ctx->buf.b - ctx->startBuf;
+	int size = BUF_POS();
 	unsigned char *code = (unsigned char*)hl_alloc_executable_memory(size);
 	if( code == NULL ) return NULL;
 	memcpy(code,ctx->startBuf,size);
@@ -548,7 +575,7 @@ void *hl_jit_code( jit_ctx *ctx, hl_module *m ) {
 	c = ctx->calls;
 	while( c ) {
 		int fid = ctx->globalToFunction[c->target];
-		int fpos = (int_val)m->functions_ptrs[fid];
+		int fpos = (int)m->functions_ptrs[fid];
 		*(int*)(code + c->pos + 1) = fpos - (c->pos + 5);
 		c = c->next;
 	}

+ 1 - 1
src/module.c

@@ -36,7 +36,7 @@ hl_module *hl_module_alloc( hl_code *c ) {
 	}
 	for(i=0;i<c->nglobals;i++) {
 		m->globals_indexes[i] = gsize;
-		gsize += hl_type_size(c->globals[i]);
+		gsize += hl_word_size(c->globals[i]);
 	}
 	m->globals_data = (unsigned char*)malloc(gsize);
 	if( m->globals_data == NULL ) {