Browse Source

more work on jit

Nicolas Cannasse 9 years ago
parent
commit
94421e193a
4 changed files with 405 additions and 205 deletions
  1. 173 173
      hl.vcxproj
  2. 1 0
      src/hl.h
  3. 207 31
      src/jit.c
  4. 24 1
      src/module.c

+ 173 - 173
hl.vcxproj

@@ -1,174 +1,174 @@
-<?xml version="1.0" encoding="utf-8"?>
-<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
-  <ItemGroup Label="ProjectConfigurations">
-    <ProjectConfiguration Include="Debug|Win32">
-      <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>{BBF750D2-6DD2-4A41-AC3A-07C070B94FA1}</ProjectGuid>
-    <Keyword>Win32Proj</Keyword>
-    <RootNamespace>hl</RootNamespace>
-  </PropertyGroup>
-  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
-  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
-    <ConfigurationType>Application</ConfigurationType>
-    <UseDebugLibraries>true</UseDebugLibraries>
-    <CharacterSet>Unicode</CharacterSet>
-    <PlatformToolset>v140</PlatformToolset>
-  </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>
-    <PlatformToolset>v140</PlatformToolset>
-  </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>
-    <IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);src</IncludePath>
-    <LibraryPath>$(VC_LibraryPath_x86);$(WindowsSDK_LibraryPath_x86);$(NETFXKitsDir)Lib\um\x86;$(Configuration)</LibraryPath>
-  </PropertyGroup>
-  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
-    <LinkIncremental>true</LinkIncremental>
-    <IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);src</IncludePath>
-    <LibraryPath>$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);$(NETFXKitsDir)Lib\um\x64;$(Configuration)</LibraryPath>
-  </PropertyGroup>
-  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
-    <LinkIncremental>true</LinkIncremental>
-    <IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);src</IncludePath>
-    <LibraryPath>$(VC_LibraryPath_x86);$(WindowsSDK_LibraryPath_x86);$(NETFXKitsDir)Lib\um\x86;$(Configuration)</LibraryPath>
-  </PropertyGroup>
-  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
-    <LinkIncremental>true</LinkIncremental>
-    <IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);src</IncludePath>
-    <LibraryPath>$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);$(NETFXKitsDir)Lib\um\x64;$(Configuration)</LibraryPath>
-  </PropertyGroup>
-  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
-    <ClCompile>
-      <PrecompiledHeader>
-      </PrecompiledHeader>
-      <Optimization>Disabled</Optimization>
-      <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
-      <EnableEnhancedInstructionSet>StreamingSIMDExtensions2</EnableEnhancedInstructionSet>
-    </ClCompile>
-    <Link>
-      <SubSystem>Console</SubSystem>
-      <GenerateDebugInformation>true</GenerateDebugInformation>
-      <AdditionalDependencies>std.lib</AdditionalDependencies>
-      <StackReserveSize>0x200000</StackReserveSize>
-    </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>
-      <AdditionalDependencies>std.lib</AdditionalDependencies>
-      <StackReserveSize>0x200000</StackReserveSize>
-    </Link>
-  </ItemDefinitionGroup>
-  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
-    <ClCompile>
-      <WarningLevel>Level3</WarningLevel>
-      <PrecompiledHeader>
-      </PrecompiledHeader>
-      <Optimization>MaxSpeed</Optimization>
-      <FunctionLevelLinking>true</FunctionLevelLinking>
-      <IntrinsicFunctions>true</IntrinsicFunctions>
-      <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
-      <EnableEnhancedInstructionSet>StreamingSIMDExtensions2</EnableEnhancedInstructionSet>
-      <WholeProgramOptimization>false</WholeProgramOptimization>
-    </ClCompile>
-    <Link>
-      <SubSystem>Console</SubSystem>
-      <GenerateDebugInformation>true</GenerateDebugInformation>
-      <EnableCOMDATFolding>true</EnableCOMDATFolding>
-      <OptimizeReferences>true</OptimizeReferences>
-      <AdditionalDependencies>std.lib</AdditionalDependencies>
-      <StackReserveSize>0x200000</StackReserveSize>
-    </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>
-      <WholeProgramOptimization>false</WholeProgramOptimization>
-    </ClCompile>
-    <Link>
-      <SubSystem>Console</SubSystem>
-      <GenerateDebugInformation>true</GenerateDebugInformation>
-      <EnableCOMDATFolding>true</EnableCOMDATFolding>
-      <OptimizeReferences>true</OptimizeReferences>
-      <AdditionalDependencies>std.lib</AdditionalDependencies>
-      <StackReserveSize>0x200000</StackReserveSize>
-    </Link>
-  </ItemDefinitionGroup>
-  <ItemGroup>
-    <ClCompile Include="src\callback.c" />
-    <ClCompile Include="src\code.c" />
-    <ClCompile Include="src\jit.c" />
-    <ClCompile Include="src\main.c" />
-    <ClCompile Include="src\module.c" />
-  </ItemGroup>
-  <ItemGroup>
-    <ClInclude Include="src\hlmodule.h" />
-    <ClInclude Include="src\opcodes.h" />
-  </ItemGroup>
-  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
-  <ImportGroup Label="ExtensionTargets">
-  </ImportGroup>
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <ItemGroup Label="ProjectConfigurations">
+    <ProjectConfiguration Include="Debug|Win32">
+      <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>{BBF750D2-6DD2-4A41-AC3A-07C070B94FA1}</ProjectGuid>
+    <Keyword>Win32Proj</Keyword>
+    <RootNamespace>hl</RootNamespace>
+  </PropertyGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>true</UseDebugLibraries>
+    <CharacterSet>Unicode</CharacterSet>
+    <PlatformToolset>v140</PlatformToolset>
+  </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>
+    <PlatformToolset>v140</PlatformToolset>
+  </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>
+    <IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);src</IncludePath>
+    <LibraryPath>$(VC_LibraryPath_x86);$(WindowsSDK_LibraryPath_x86);$(NETFXKitsDir)Lib\um\x86;$(Configuration)</LibraryPath>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+    <LinkIncremental>true</LinkIncremental>
+    <IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);src</IncludePath>
+    <LibraryPath>$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);$(NETFXKitsDir)Lib\um\x64;$(Configuration)</LibraryPath>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <LinkIncremental>true</LinkIncremental>
+    <IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);src</IncludePath>
+    <LibraryPath>$(VC_LibraryPath_x86);$(WindowsSDK_LibraryPath_x86);$(NETFXKitsDir)Lib\um\x86;$(Configuration)</LibraryPath>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+    <LinkIncremental>true</LinkIncremental>
+    <IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);src</IncludePath>
+    <LibraryPath>$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);$(NETFXKitsDir)Lib\um\x64;$(Configuration)</LibraryPath>
+  </PropertyGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <ClCompile>
+      <PrecompiledHeader>
+      </PrecompiledHeader>
+      <Optimization>Disabled</Optimization>
+      <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <EnableEnhancedInstructionSet>StreamingSIMDExtensions2</EnableEnhancedInstructionSet>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+      <AdditionalDependencies>std.lib;user32.lib</AdditionalDependencies>
+      <StackReserveSize>0x400000,0x400000</StackReserveSize>
+    </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>
+      <AdditionalDependencies>std.lib;user32.lib</AdditionalDependencies>
+      <StackReserveSize>0x400000</StackReserveSize>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <PrecompiledHeader>
+      </PrecompiledHeader>
+      <Optimization>MaxSpeed</Optimization>
+      <FunctionLevelLinking>true</FunctionLevelLinking>
+      <IntrinsicFunctions>true</IntrinsicFunctions>
+      <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <EnableEnhancedInstructionSet>StreamingSIMDExtensions2</EnableEnhancedInstructionSet>
+      <WholeProgramOptimization>false</WholeProgramOptimization>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+      <EnableCOMDATFolding>true</EnableCOMDATFolding>
+      <OptimizeReferences>true</OptimizeReferences>
+      <AdditionalDependencies>std.lib;user32.lib</AdditionalDependencies>
+      <StackReserveSize>0x400000</StackReserveSize>
+    </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>
+      <WholeProgramOptimization>false</WholeProgramOptimization>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+      <EnableCOMDATFolding>true</EnableCOMDATFolding>
+      <OptimizeReferences>true</OptimizeReferences>
+      <AdditionalDependencies>std.lib;user32.lib</AdditionalDependencies>
+      <StackReserveSize>0x400000</StackReserveSize>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemGroup>
+    <ClCompile Include="src\callback.c" />
+    <ClCompile Include="src\code.c" />
+    <ClCompile Include="src\jit.c" />
+    <ClCompile Include="src\main.c" />
+    <ClCompile Include="src\module.c" />
+  </ItemGroup>
+  <ItemGroup>
+    <ClInclude Include="src\hlmodule.h" />
+    <ClInclude Include="src\opcodes.h" />
+  </ItemGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+  <ImportGroup Label="ExtensionTargets">
+  </ImportGroup>
 </Project>
 </Project>

+ 1 - 0
src/hl.h

@@ -276,6 +276,7 @@ typedef struct {
 	int nparams;
 	int nparams;
 	hl_type **params;
 	hl_type **params;
 	int size;
 	int size;
+	bool hasPtr;
 	int *offsets;
 	int *offsets;
 } hl_enum_construct;
 } hl_enum_construct;
 
 

+ 207 - 31
src/jit.c

@@ -23,7 +23,7 @@
 
 
 #ifdef HL_DEBUG
 #ifdef HL_DEBUG
 //#define OP_LOG
 //#define OP_LOG
-//#define JIT_DEBUG
+#define JIT_DEBUG
 #endif
 #endif
 
 
 typedef enum {
 typedef enum {
@@ -208,7 +208,7 @@ static int RCPU_SCRATCH_REGS[] = { Eax, Ecx, Edx };
 #define ID2(a,b)	((a) | ((b)<<8))
 #define ID2(a,b)	((a) | ((b)<<8))
 #define R(id)		(ctx->vregs + (id))
 #define R(id)		(ctx->vregs + (id))
 #define ASSERT(i)	{ printf("JIT ERROR %d (jic.c line %d)\n",i,__LINE__); jit_exit(); }
 #define ASSERT(i)	{ printf("JIT ERROR %d (jic.c line %d)\n",i,__LINE__); jit_exit(); }
-#define IS_FLOAT(r)	((r)->t->kind == HF64)
+#define IS_FLOAT(r)	((r)->t->kind == HF64 || (r)->t->kind == HF32)
 #define RLOCK(r)		if( (r)->lock < ctx->currentPos ) (r)->lock = ctx->currentPos
 #define RLOCK(r)		if( (r)->lock < ctx->currentPos ) (r)->lock = ctx->currentPos
 #define RUNLOCK(r)		if( (r)->lock == ctx->currentPos ) (r)->lock = 0
 #define RUNLOCK(r)		if( (r)->lock == ctx->currentPos ) (r)->lock = 0
 
 
@@ -242,12 +242,11 @@ struct jit_ctx {
 	jlist *calls;
 	jlist *calls;
 	hl_alloc falloc; // cleared per-function
 	hl_alloc falloc; // cleared per-function
 	hl_alloc galloc;
 	hl_alloc galloc;
+	vclosure *closure_list;
 };
 };
 
 
-static void jit_exit() {
-	hl_debug_break();
-	exit(-1);
-}
+#define jit_exit() { hl_debug_break(); exit(-1); }
+#define jit_error(msg)	_jit_error(ctx,msg,__LINE__)
 
 
 static preg *pmem( preg *r, CpuReg reg, int offset ) {
 static preg *pmem( preg *r, CpuReg reg, int offset ) {
 	r->kind = RMEM;
 	r->kind = RMEM;
@@ -833,6 +832,10 @@ static void load( jit_ctx *ctx, preg *r, vreg *v ) {
 	preg *from = fetch(v);
 	preg *from = fetch(v);
 	if( from == r || v->size == 0 ) return;
 	if( from == r || v->size == 0 ) return;
 	if( r->holds ) r->holds->current = NULL;
 	if( r->holds ) r->holds->current = NULL;
+	if( v->current ) {
+		v->current->holds = NULL;
+		from = r;
+	}
 	r->holds = v;
 	r->holds = v;
 	v->current = r;
 	v->current = r;
 	copy(ctx,r,from,v->size);
 	copy(ctx,r,from,v->size);
@@ -841,7 +844,7 @@ static void load( jit_ctx *ctx, preg *r, vreg *v ) {
 static preg *alloc_fpu( jit_ctx *ctx, vreg *r, bool andLoad ) {
 static preg *alloc_fpu( jit_ctx *ctx, vreg *r, bool andLoad ) {
 	preg *p = fetch(r);
 	preg *p = fetch(r);
 	if( p->kind != RFPU ) {
 	if( p->kind != RFPU ) {
-		if( r->t->kind != HF32 && r->t->kind != HF64 ) ASSERT(r->t->kind);
+		if( !IS_FLOAT(r) ) ASSERT(r->t->kind);
 		p = alloc_reg(ctx, RFPU);
 		p = alloc_reg(ctx, RFPU);
 		if( andLoad )
 		if( andLoad )
 			load(ctx,p,r);
 			load(ctx,p,r);
@@ -851,7 +854,8 @@ static preg *alloc_fpu( jit_ctx *ctx, vreg *r, bool andLoad ) {
 			r->current = p;
 			r->current = p;
 			p->holds = r;
 			p->holds = r;
 		}
 		}
-	}
+	} else
+		RLOCK(p);
 	return p;
 	return p;
 }
 }
 
 
@@ -870,7 +874,8 @@ static preg *alloc_cpu( jit_ctx *ctx, vreg *r, bool andLoad ) {
 			r->current = p;
 			r->current = p;
 			p->holds = r;
 			p->holds = r;
 		}
 		}
-	}
+	} else
+		RLOCK(p);
 	return p;
 	return p;
 }
 }
 
 
@@ -890,6 +895,33 @@ static preg *copy( jit_ctx *ctx, preg *to, preg *from, int size ) {
 		case 1:
 		case 1:
 			op32(ctx,MOV8,to,from);
 			op32(ctx,MOV8,to,from);
 			break;
 			break;
+		case 2:
+			{
+				preg rtmp;
+				switch( ID2(to->kind,from->kind) ) {
+				case ID2(RCPU,RSTACK):
+					BREAK();
+					op32(ctx,MOV,to,from);
+					op32(ctx,AND,to,pconst(&rtmp,0xFFFF));
+					break;
+				case ID2(RSTACK,RCPU):
+					BREAK();
+					op32(ctx,MOV8,to,from);
+					{
+						preg *r = alloc_reg(ctx,RCPU);
+						op32(ctx,MOV,r,from);
+						op32(ctx,SAR,r,pconst(&rtmp,8));
+						R(to->id)->stackPos++;
+						op32(ctx,MOV8,to,r);
+						R(to->id)->stackPos--;
+					}
+					break;
+				default:
+					ASSERT(size);
+					break;
+				}
+				break;
+			}
 		case 4:
 		case 4:
 			op32(ctx,MOV,to,from);
 			op32(ctx,MOV,to,from);
 			break;
 			break;
@@ -932,6 +964,7 @@ static preg *copy( jit_ctx *ctx, preg *to, preg *from, int size ) {
 #	ifndef HL_64
 #	ifndef HL_64
 	case ID2(RMEM,RADDR):
 	case ID2(RMEM,RADDR):
 	case ID2(RSTACK,RADDR):
 	case ID2(RSTACK,RADDR):
+	case ID2(RADDR,RSTACK):
 #	endif
 #	endif
 		{
 		{
 			preg *tmp;
 			preg *tmp;
@@ -1361,6 +1394,9 @@ static preg *op_binop( jit_ctx *ctx, vreg *dst, vreg *a, vreg *b, hl_opcode *op
 	}
 	}
 	switch( RTYPE(a) ) {
 	switch( RTYPE(a) ) {
 	case HI32:
 	case HI32:
+	case HI16:
+	case HI8:
+	case HBOOL:
 #	ifndef HL_64
 #	ifndef HL_64
 	case HDYNOBJ:
 	case HDYNOBJ:
 	case HVIRTUAL:
 	case HVIRTUAL:
@@ -1368,6 +1404,9 @@ static preg *op_binop( jit_ctx *ctx, vreg *dst, vreg *a, vreg *b, hl_opcode *op
 	case HFUN:
 	case HFUN:
 	case HBYTES:
 	case HBYTES:
 	case HNULL:
 	case HNULL:
+	case HENUM:
+	case HDYN:
+	case HTYPE:
 #	endif
 #	endif
 		switch( ID2(pa->kind, pb->kind) ) {
 		switch( ID2(pa->kind, pb->kind) ) {
 		case ID2(RCPU,RCPU):
 		case ID2(RCPU,RCPU):
@@ -1377,7 +1416,7 @@ static preg *op_binop( jit_ctx *ctx, vreg *dst, vreg *a, vreg *b, hl_opcode *op
 			out = pa;
 			out = pa;
 			break;
 			break;
 		case ID2(RSTACK,RCPU):
 		case ID2(RSTACK,RCPU):
-			if( dst == a ) {
+			if( dst == a && o != IMUL ) {
 				op32(ctx, o, pa, pb);
 				op32(ctx, o, pa, pb);
 				dst = NULL;
 				dst = NULL;
 				out = pa;
 				out = pa;
@@ -1402,6 +1441,9 @@ static preg *op_binop( jit_ctx *ctx, vreg *dst, vreg *a, vreg *b, hl_opcode *op
 	case HFUN:
 	case HFUN:
 	case HBYTES:
 	case HBYTES:
 	case HNULL:
 	case HNULL:
+	case HENUM:
+	case HDYN:
+	case HTYPE:
 		switch( ID2(pa->kind, pb->kind) ) {
 		switch( ID2(pa->kind, pb->kind) ) {
 		case ID2(RCPU,RCPU):
 		case ID2(RCPU,RCPU):
 		case ID2(RCPU,RSTACK):
 		case ID2(RCPU,RSTACK):
@@ -1430,6 +1472,7 @@ static preg *op_binop( jit_ctx *ctx, vreg *dst, vreg *a, vreg *b, hl_opcode *op
 		return out;
 		return out;
 #	endif
 #	endif
 	case HF64:
 	case HF64:
+	case HF32:
 		pa = alloc_fpu(ctx, a, true);
 		pa = alloc_fpu(ctx, a, true);
 		pb = alloc_fpu(ctx, b, true);
 		pb = alloc_fpu(ctx, b, true);
 		switch( ID2(pa->kind, pb->kind) ) {
 		switch( ID2(pa->kind, pb->kind) ) {
@@ -1512,6 +1555,26 @@ static void call_native_consts( jit_ctx *ctx, void *nativeFun, int_val *args, in
 	call_native(ctx, nativeFun, size);
 	call_native(ctx, nativeFun, size);
 }
 }
 
 
+static void on_jit_error( const char *msg, int_val line ) {
+	char buf[256];
+	sprintf(buf,"%s (line %d)",msg,(int)line);
+#ifdef HL_WIN
+	MessageBoxA(NULL,buf,"JIT ERROR",MB_OK);
+#else
+	printf("%s\n",buf);
+#endif
+	hl_debug_break();
+}
+
+static void hl_null_access() {
+	hl_error_msg(USTR("Null access"));
+}
+
+static void _jit_error( jit_ctx *ctx, const char *msg, int line ) {
+	int_val args[2] = { (int_val)msg, (int_val)line };
+	call_native_consts(ctx,on_jit_error,args,2);
+}
+
 jit_ctx *hl_jit_alloc() {
 jit_ctx *hl_jit_alloc() {
 	int i;
 	int i;
 	jit_ctx *ctx = (jit_ctx*)malloc(sizeof(jit_ctx));
 	jit_ctx *ctx = (jit_ctx*)malloc(sizeof(jit_ctx));
@@ -1614,6 +1677,22 @@ int hl_jit_init_callback( jit_ctx *ctx ) {
 	return pos;
 	return pos;
 }
 }
 
 
+static void *get_dyncast( hl_type *t ) {
+	switch( t->kind ) {
+	case HF32:
+		return hl_dyn_castf;
+	case HF64:
+		return hl_dyn_castd;
+	case HI32:
+	case HI16:
+	case HI8:
+	case HBOOL:
+		return hl_dyn_casti;
+	default:
+		return hl_dyn_castp;
+	}
+}
+
 int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 	int i, size = 0, opCount;
 	int i, size = 0, opCount;
 	int codePos = BUF_POS();
 	int codePos = BUF_POS();
@@ -1830,6 +1909,10 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 				BREAK();
 				BREAK();
 				op32(ctx,CVTPD2PS,w,r);
 				op32(ctx,CVTPD2PS,w,r);
 				store(ctx, dst, w, true);
 				store(ctx, dst, w, true);
+			} else if( ra->t->kind == HF32 && dst->t->kind == HF64 ) {
+				jit_error("TODO");
+			} else if( ra->t->kind == HI32 && dst->t->kind == HF32 ) {
+				jit_error("TODO");
 			} else
 			} else
 				ASSERT(0);
 				ASSERT(0);
 			break;
 			break;
@@ -1871,16 +1954,23 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 			break;
 			break;
 		case OFloat:
 		case OFloat:
 			{
 			{
-				if( dst->size != 8 ) ASSERT(dst->size);
 				if( m->code->floats[o->p2] == 0 ) {
 				if( m->code->floats[o->p2] == 0 ) {
 					preg *f = alloc_fpu(ctx,dst,false);
 					preg *f = alloc_fpu(ctx,dst,false);
 					op64(ctx,XORPD,f,f);
 					op64(ctx,XORPD,f,f);
-				} else {
+				} else switch( dst->t->kind ) {
+				case HF64:
 #					ifdef HL_64
 #					ifdef HL_64
 					op64(ctx,MOVSD,alloc_fpu(ctx,dst,false),pcodeaddr(&p,o->p2 * 8));
 					op64(ctx,MOVSD,alloc_fpu(ctx,dst,false),pcodeaddr(&p,o->p2 * 8));
 #					else
 #					else
 					op64(ctx,MOVSD,alloc_fpu(ctx,dst,false),paddr(&p,m->code->floats + o->p2));
 					op64(ctx,MOVSD,alloc_fpu(ctx,dst,false),paddr(&p,m->code->floats + o->p2));
 #					endif
 #					endif
+					break;
+				case HF32:
+					BREAK();
+					op32(ctx,MOVSS,alloc_fpu(ctx,dst,false),paddr(&p,m->code->floats + o->p2));
+					break;
+				default:
+					ASSERT(dst->t->kind);
 				}
 				}
 				store(ctx,dst,dst->current,false); 
 				store(ctx,dst,dst->current,false); 
 			}
 			}
@@ -1897,24 +1987,27 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 				store(ctx,dst,dst->current,false);
 				store(ctx,dst,dst->current,false);
 			}
 			}
 			break;
 			break;
-/*		case ONew:
-			switch( dst->t->kind ) {
-			case HOBJ:
-				{
-					int_val args[2] = { (int_val)m, (int_val)dst->t };
-					call_native_consts(ctx, hl_alloc_obj, args, 2);
-					store(ctx, dst, PEAX, true);
+		case ONew:
+			{
+				int_val args[] = { (int_val)dst->t };
+				void *allocFun;
+				int nargs = 1;
+				switch( dst->t->kind ) {
+				case HOBJ: 
+					allocFun = hl_alloc_obj;
 					break;
 					break;
-				}
-			case HDYNOBJ:
-				{
-					int_val args[1] = { (int_val)dst->t };
-					call_native_consts(ctx, hl_alloc_dynobj, args, 1);
-					store(ctx, dst, PEAX, true);
+				case HDYNOBJ:
+					allocFun = hl_alloc_dynobj;
+					nargs = 0;
 					break;
 					break;
+				case HVIRTUAL:
+					allocFun = hl_alloc_virtual;
+					break;
+				default:
+					ASSERT(dst->t->kind);
 				}
 				}
-			default:
-				ASSERT(dst->t->kind);
+				call_native_consts(ctx, allocFun, args, nargs);
+				store(ctx, dst, PEAX, true);
 			}
 			}
 			break;
 			break;
 /*		case OGetFunction:
 /*		case OGetFunction:
@@ -1956,6 +2049,17 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 */		case OCallClosure:
 */		case OCallClosure:
 			op_call_closure(ctx,dst,ra,o->p3,o->extra);
 			op_call_closure(ctx,dst,ra,o->p3,o->extra);
 			break;
 			break;
+		case OStaticClosure:
+			{
+				// todo : share duplicates ?
+				vclosure *c = hl_malloc(&m->ctx.alloc,sizeof(vclosure));
+				c->t = dst->t;
+				c->fun = (void*)(int_val)o->p1;
+				c->hasValue = 0;
+				c->value = ctx->closure_list;
+				ctx->closure_list = c;
+			}
+			break;
 		case OField:
 		case OField:
 			{
 			{
 				switch( ra->t->kind ) {
 				switch( ra->t->kind ) {
@@ -1968,6 +2072,8 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 					break;
 					break;
 				case HVIRTUAL:
 				case HVIRTUAL:
 					{
 					{
+						jit_error("TODO");
+/*
 						preg *rr = alloc_cpu(ctx,ra,true);
 						preg *rr = alloc_cpu(ctx,ra,true);
 						preg *ridx = alloc_reg(ctx, RCPU);
 						preg *ridx = alloc_reg(ctx, RCPU);
 						preg *ridx2 = IS_64 ? alloc_reg(ctx,RCPU) : ridx;
 						preg *ridx2 = IS_64 ? alloc_reg(ctx,RCPU) : ridx;
@@ -1985,7 +2091,7 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 						op64(ctx, ADD, rtmp, ridx2);
 						op64(ctx, ADD, rtmp, ridx2);
 						RUNLOCK(ridx);
 						RUNLOCK(ridx);
 						// fetch field data
 						// fetch field data
-						copy_to(ctx,dst, pmem(&p, rtmp->id, 0));
+						copy_to(ctx,dst, pmem(&p, rtmp->id, 0));*/
 					}
 					}
 					break;
 					break;
 				default:
 				default:
@@ -2154,7 +2260,7 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 			break;
 			break;
 		case OArraySize:
 		case OArraySize:
 			{
 			{
-				op32(ctx,MOV,alloc_cpu(ctx,dst,false),pmem(&p,alloc_cpu(ctx,ra,true)->id,HL_WSIZE));
+				op32(ctx,MOV,alloc_cpu(ctx,dst,false),pmem(&p,alloc_cpu(ctx,ra,true)->id,HL_WSIZE*2));
 				store(ctx,dst,dst->current,false);
 				store(ctx,dst,dst->current,false);
 			}
 			}
 			break;
 			break;
@@ -2245,8 +2351,67 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 					call_native(ctx,hl_dyn_set32,size);
 					call_native(ctx,hl_dyn_set32,size);
 			}
 			}
 			break;
 			break;
-*/		default:
-			printf("Don't know how to jit %s\n",hl_op_name(o->op));
+*/		
+		case OMakeEnum:
+			{
+				hl_enum_construct *c = &dst->t->tenum->constructs[o->p2];
+				int_val args[] = { c->size, c->hasptr?MEM_KIND_RAW:MEM_KIND_NOPTR };
+				int i;
+				call_native_consts(ctx, hl_gc_alloc_gen, args, 2);
+				store(ctx, dst, PEAX, true);
+				op32(ctx,MOV,REG_AT(Ecx),pconst(&p,o->p2));
+				op32(ctx,MOV,pmem(&p,Eax,0),REG_AT(Ecx));
+				for(i=0;i<c->nparams;i++) {
+					preg *r = fetch(R(o->extra[i]));
+					copy(ctx, pmem(&p,Eax,c->offsets[i]),r, R(o->extra[i])->size);
+					RUNLOCK(fetch(R(o->extra[i])));
+				}
+			}
+			break;
+		case ONullCheck:
+			{
+				int jz;
+				preg *r = alloc_cpu(ctx,dst,true);
+				op32(ctx,TEST,r,r);
+				XJump_small(JNotZero,jz);
+				call_native_consts(ctx,hl_null_access,NULL,0);
+				patch_jump(ctx,jz);
+			}
+			break;
+		case OSafeCast:
+			{
+				int size;
+#				ifdef HL_64
+
+#				else
+				switch( dst->t->kind ) {
+				case HF32:
+				case HF64:
+					jit_error("TODO");
+					break;
+				default:
+					size = pad_stack(ctx, HL_WSIZE*3);
+					op32(ctx,PUSH,pconst64(&p,(int_val)dst->t),UNUSED);
+					op32(ctx,PUSH,pconst64(&p,(int_val)ra->t),UNUSED);
+					op32(ctx,MOV,PEAX,REG_AT(Ebp));
+					op32(ctx,ADD,PEAX,pconst(&p,ra->stackPos));
+					op32(ctx,PUSH,PEAX,UNUSED);
+					call_native(ctx,get_dyncast(dst->t),size);
+					store(ctx, dst, PEAX, true);
+					break;
+				}
+#				endif
+			}
+			break;
+		default:
+			{
+				static bool TRACES[OLast] = {false};
+				if( !TRACES[o->op] ) {
+					TRACES[o->op] = true;
+					printf("Don't know how to jit %s\n",hl_op_name(o->op));
+				}
+				jit_error(hl_op_name(o->op));
+			}
 			break;
 			break;
 		}
 		}
 		// we are landing at this position, assume we have lost our registers
 		// we are landing at this position, assume we have lost our registers
@@ -2293,6 +2458,17 @@ void *hl_jit_code( jit_ctx *ctx, hl_module *m, int *codesize ) {
 		*(int*)(code + c->pos + 1) = fpos - (c->pos + 5);
 		*(int*)(code + c->pos + 1) = fpos - (c->pos + 5);
 		c = c->next;
 		c = c->next;
 	}
 	}
+	// patch closures
+	{
+		vclosure *c = ctx->closure_list;
+		while( c ) {
+			vclosure *next;
+			c->fun = m->functions_ptrs[(int)(int_val)c->fun];
+			next = (vclosure*)c->value;
+			c->value = NULL;
+			c = next;
+		}
+	}
 	return code;
 	return code;
 }
 }
 
 

+ 24 - 1
src/module.c

@@ -29,6 +29,22 @@
 #	include <dlfcn.h>
 #	include <dlfcn.h>
 #endif
 #endif
 
 
+static void hl_init_enum( hl_type_enum *e ) {
+	int i, j;
+	for(i=0;i<e->nconstructs;i++) {
+		hl_enum_construct *c = &e->constructs[i];
+		c->hasptr = false;
+		c->size = sizeof(int); // index
+		for(j=0;j<c->nparams;j++) {
+			hl_type *t = c->params[j];
+			c->size += hl_pad_size(c->size,t);
+			c->offsets[j] = c->size;
+			if( hl_is_gc_ptr(t) ) c->hasptr = true;
+			c->size += hl_type_size(t);
+		}
+	}
+}
+
 hl_module *hl_module_alloc( hl_code *c ) {
 hl_module *hl_module_alloc( hl_code *c ) {
 	int i;
 	int i;
 	int gsize = 0;
 	int gsize = 0;
@@ -163,7 +179,14 @@ int hl_module_init( hl_module *m ) {
 	}
 	}
 	for(i=0;i<m->code->ntypes;i++) {
 	for(i=0;i<m->code->ntypes;i++) {
 		hl_type *t = m->code->types + i;
 		hl_type *t = m->code->types + i;
-		if( t->kind == HOBJ ) t->obj->m = &m->ctx;
+		switch( t->kind ) {
+		case HOBJ:
+			t->obj->m = &m->ctx;
+			break;
+		case HENUM:
+			hl_init_enum(t->tenum);
+			break;
+		}
 	}
 	}
 	// JIT
 	// JIT
 	ctx = hl_jit_alloc();
 	ctx = hl_jit_alloc();