Browse Source

hl vm project + bytecode read update

Nicolas Cannasse 9 years ago
parent
commit
40b1666eca
11 changed files with 438 additions and 46 deletions
  1. 15 0
      hl.sln
  2. 171 0
      hl.vcxproj
  3. 11 0
      hl.vcxproj.filters
  4. 0 2
      hldll.vcxproj
  5. 0 2
      hldll.vcxproj.filters
  6. 1 1
      libs/sdl/sdl/Sdl.hx
  7. 67 17
      src/code.c
  8. 2 1
      src/hl.h
  9. 1 0
      src/hlmodule.h
  10. 120 0
      src/main.c
  11. 50 23
      src/opcodes.h

+ 15 - 0
hl.sln

@@ -22,6 +22,13 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "hlc", "hlc.vcxproj", "{8075
 		{C6213FBF-BC2B-4235-A827-84A60E848C52} = {C6213FBF-BC2B-4235-A827-84A60E848C52}
 	EndProjectSection
 EndProject
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "hl", "hl.vcxproj", "{BBF750D2-6DD2-4A41-AC3A-07C070B94FA1}"
+	ProjectSection(ProjectDependencies) = postProject
+		{7DDA1414-6675-45C7-8254-42057901F865} = {7DDA1414-6675-45C7-8254-42057901F865}
+		{12049F27-EA26-4A33-ADF8-E542C4167C00} = {12049F27-EA26-4A33-ADF8-E542C4167C00}
+		{C6213FBF-BC2B-4235-A827-84A60E848C52} = {C6213FBF-BC2B-4235-A827-84A60E848C52}
+	EndProjectSection
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Win32 = Debug|Win32
@@ -62,6 +69,14 @@ Global
 		{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
+		{BBF750D2-6DD2-4A41-AC3A-07C070B94FA1}.Debug|Win32.ActiveCfg = Debug|Win32
+		{BBF750D2-6DD2-4A41-AC3A-07C070B94FA1}.Debug|Win32.Build.0 = Debug|Win32
+		{BBF750D2-6DD2-4A41-AC3A-07C070B94FA1}.Debug|x64.ActiveCfg = Debug|x64
+		{BBF750D2-6DD2-4A41-AC3A-07C070B94FA1}.Debug|x64.Build.0 = Debug|x64
+		{BBF750D2-6DD2-4A41-AC3A-07C070B94FA1}.Release|Win32.ActiveCfg = Release|Win32
+		{BBF750D2-6DD2-4A41-AC3A-07C070B94FA1}.Release|Win32.Build.0 = Release|Win32
+		{BBF750D2-6DD2-4A41-AC3A-07C070B94FA1}.Release|x64.ActiveCfg = Release|x64
+		{BBF750D2-6DD2-4A41-AC3A-07C070B94FA1}.Release|x64.Build.0 = Release|x64
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 171 - 0
hl.vcxproj

@@ -0,0 +1,171 @@
+<?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;sdl.lib;fmt.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;sdl.lib;fmt.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;sdl.lib;fmt.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;sdl.lib;fmt.lib</AdditionalDependencies>
+      <StackReserveSize>0x200000</StackReserveSize>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemGroup>
+    <ClCompile Include="src\code.c" />
+    <ClCompile Include="src\main.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>

+ 11 - 0
hl.vcxproj.filters

@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <ItemGroup>
+    <ClCompile Include="src\main.c" />
+    <ClCompile Include="src\code.c" />
+  </ItemGroup>
+  <ItemGroup>
+    <ClInclude Include="src\hlmodule.h" />
+    <ClInclude Include="src\opcodes.h" />
+  </ItemGroup>
+</Project>

+ 0 - 2
hldll.vcxproj

@@ -193,8 +193,6 @@
     <ClInclude Include="include\pcre\pcre_internal.h" />
     <ClInclude Include="include\pcre\ucp.h" />
     <ClInclude Include="src\hl.h" />
-    <ClInclude Include="src\hlmodule.h" />
-    <ClInclude Include="src\opcodes.h" />
   </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">

+ 0 - 2
hldll.vcxproj.filters

@@ -106,7 +106,5 @@
       <Filter>pcre</Filter>
     </ClInclude>
     <ClInclude Include="src\hl.h" />
-    <ClInclude Include="src\hlmodule.h" />
-    <ClInclude Include="src\opcodes.h" />
   </ItemGroup>
 </Project>

+ 1 - 1
libs/sdl/sdl/Sdl.hx

@@ -26,7 +26,7 @@ class Sdl {
 				if( onEvent != null ) onEvent(event);
 			}
 			callb();
-			//@:privateAccess haxe.Timer.sync();
+			@:privateAccess haxe.Timer.sync();
         }
 	}
 

+ 67 - 17
src/code.c

@@ -39,10 +39,11 @@ typedef struct {
 	hl_code *code;
 } hl_reader;
 
+#undef ERROR
 #define READ() hl_read_b(r)
 #define INDEX() hl_read_index(r)
 #define UINDEX() hl_read_uindex(r)
-#define ERROR(msg) if( !r->error ) r->error = msg;
+#define ERROR(msg) if( !r->error ) { r->error = msg; hl_debug_break(); }
 #define CHK_ERROR() if( r->error ) return
 
 static unsigned char hl_read_b( hl_reader *r ) {
@@ -129,25 +130,42 @@ static const char *hl_get_string( hl_reader *r ) {
 	return r->code->strings[i];
 }
 
+static const uchar *hl_get_ustring( hl_reader *r ) {
+	int i = INDEX();
+	uchar *str;
+	if( i < 0 || i >= r->code->nstrings ) {
+		ERROR("Invalid string index");
+		i = 0;
+	}
+	str = r->code->ustrings[i];
+	if( str == NULL ) {
+		int size = hl_utf8_length(r->code->strings[i],0);
+		str = hl_malloc(&r->code->alloc,(size+1)<<1);
+		strtou(str,size+1,r->code->strings[i]);
+		r->code->ustrings[i] = str;
+	}
+	return str;
+}
+
 static void hl_read_type( hl_reader *r, hl_type *t ) {
 	t->kind = READ();
-	t->self = t;
 	switch( (int)t->kind ) {
 	case HFUN:
 		{
 			int i;
 			int nargs = READ(); 
-			t->fun = (hl_type_fun*)hl_malloc(&r->code->alloc,sizeof(hl_type_fun) + sizeof(hl_type*)*(nargs-1));
+			t->fun = (hl_type_fun*)hl_malloc(&r->code->alloc,sizeof(hl_type_fun));
 			t->fun->nargs = nargs;
+			t->fun->args = (hl_type**)hl_malloc(&r->code->alloc,sizeof(hl_type*)*nargs);
 			for(i=0;i<nargs;i++)
-				(&t->fun->args)[i] = hl_get_type(r);
+				t->fun->args[i] = hl_get_type(r);
 			t->fun->ret = hl_get_type(r);
 		}
 		break;
 	case HOBJ:
 		{
 			int i;
-			const char *name = hl_get_string(r);
+			const uchar *name = hl_get_ustring(r);
 			int super = INDEX();
 			int nfields = UINDEX();
 			int nproto = UINDEX();
@@ -161,26 +179,21 @@ static void hl_read_type( hl_reader *r, hl_type *t ) {
 			t->obj->rt = NULL;
 			for(i=0;i<nfields;i++) {
 				hl_obj_field *f = t->obj->fields + i;
-				f->name = hl_get_string(r);
-				f->hashed_name = hl_hash(f->name,true);
+				f->name = hl_get_ustring(r);
+				f->hashed_name = hl_hash_gen(f->name,true);
 				f->t = hl_get_type(r);
 			}
 			for(i=0;i<nproto;i++) {
 				hl_obj_proto *p = t->obj->proto + i;
-				p->name = hl_get_string(r);
-				p->hashed_name = hl_hash(p->name,true);
+				p->name = hl_get_ustring(r);
+				p->hashed_name = hl_hash_gen(p->name,true);
 				p->findex = UINDEX();
 				p->pindex = INDEX();
 			}
 		}
 		break;
-	case HDYN | 0x80:
-		t->kind = HDYN;
-		t->t = hl_get_type(r);
-		break;
-	case HARRAY:
 	case HREF:
-		t->t = hl_get_type(r);
+		t->tparam = hl_get_type(r);
 		break;
 	case HVIRTUAL:
 		{
@@ -191,12 +204,37 @@ static void hl_read_type( hl_reader *r, hl_type *t ) {
 			t->virt->fields = (hl_obj_field*)hl_malloc(&r->code->alloc,sizeof(hl_obj_field)*nfields);
 			for(i=0;i<nfields;i++) {
 				hl_obj_field *f = t->virt->fields + i;
-				f->name = hl_get_string(r);
-				f->hashed_name = hl_hash(f->name,true);
+				f->name = hl_get_ustring(r);
+				f->hashed_name = hl_hash_gen(f->name,true);
 				f->t = hl_get_type(r);
 			}
 		}
 		break;
+	case HABSTRACT:
+		t->abs_name = hl_get_ustring(r);
+		break;
+	case HENUM:
+		{
+			int i,j;
+			t->tenum = hl_malloc(&r->code->alloc,sizeof(hl_type_enum));
+			t->tenum->name = hl_get_ustring(r);
+			t->tenum->nconstructs = READ();
+			t->tenum->constructs = (hl_enum_construct*)hl_malloc(&r->code->alloc, sizeof(hl_enum_construct)*t->tenum->nconstructs);
+			t->tenum->global_value = NULL;
+			for(i=0;i<t->tenum->nconstructs;i++) {
+				hl_enum_construct *c = t->tenum->constructs + i;
+				c->name = hl_get_ustring(r);
+				c->nparams = UINDEX();
+				c->params = (hl_type**)hl_malloc(&r->code->alloc,sizeof(hl_type*)*c->nparams);
+				c->offsets = (int*)hl_malloc(&r->code->alloc,sizeof(int)*c->nparams);
+				for(j=0;j<c->nparams;j++)
+					c->params[j] = hl_get_type(r);
+			}
+		}
+		break;
+	case HNULL:
+		t->tparam = hl_get_type(r);
+		break;
 	default:
 		if( t->kind >= HLAST ) ERROR("Invalid type");
 		break;
@@ -236,6 +274,7 @@ static void hl_read_opcode( hl_reader *r, hl_function *f, hl_opcode *o ) {
 		case OCallClosure:
 		case OCallMethod:
 		case OCallThis:
+		case OMakeEnum:
 			{
 				int i;
 				o->p1 = INDEX();
@@ -246,6 +285,16 @@ static void hl_read_opcode( hl_reader *r, hl_function *f, hl_opcode *o ) {
 					o->extra[i] = INDEX();
 			}
 			break;
+		case OSwitch:
+			{
+				int i;
+				o->p1 = UINDEX();
+				o->extra = (int*)hl_malloc(&r->code->alloc,sizeof(int) * o->p1);
+				for(i=0;i<o->p1;i++)
+					o->extra[i] = UINDEX();
+				o->p2 = UINDEX();
+			}
+			break;
 		default:
 			ERROR("Don't know how to process opcode");
 			break;
@@ -334,6 +383,7 @@ hl_code *hl_code_read( const unsigned char *data, int size ) {
 		c->strings_data = sdata;
 		ALLOC(c->strings, char*, c->nstrings);
 		ALLOC(c->strings_lens, int, c->nstrings);
+		ALLOC(c->ustrings, uchar*, c->nstrings);
 		for(i=0;i<c->nstrings;i++) {
 			int sz = UINDEX();
 			c->strings[i] = sdata;

+ 2 - 1
src/hl.h

@@ -294,7 +294,7 @@ struct hl_type {
 		hl_type_enum *tenum;
 		hl_type_virtual *virt;
 		hl_type	*tparam;
-		uchar *abs_name;
+		const uchar *abs_name;
 	};
 	void **vobj_proto;
 };
@@ -425,6 +425,7 @@ HL_API vvirtual *hl_alloc_virtual( hl_type *t );
 HL_API vdynobj *hl_alloc_dynobj();
 HL_API vbyte *hl_alloc_bytes( int size );
 HL_API vbyte *hl_copy_bytes( vbyte *byte, int size );
+HL_API int hl_utf8_length( vbyte *s, int pos );
 HL_API char *hl_to_utf8( uchar *bytes );
 HL_API vdynamic *hl_virtual_make_value( vvirtual *v );
 

+ 1 - 0
src/hlmodule.h

@@ -61,6 +61,7 @@ typedef struct {
 	char**		strings;
 	char*		strings_data;
 	int*		strings_lens;
+	uchar**		ustrings;
 	hl_type*	types;
 	hl_type**	globals;
 	hl_native*	natives;

+ 120 - 0
src/main.c

@@ -0,0 +1,120 @@
+/*
+ * Copyright (C)2015-2016 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+#include <hl.h>
+#include <hlmodule.h>
+
+#ifdef HL_VCC
+#	include <crtdbg.h>
+#else
+#	define _CrtSetDbgFlag(x)
+#	define _CrtCheckMemory()
+#endif
+
+#ifdef HL_WIN
+typedef uchar pchar;
+#define pprintf(str,file)	uprintf(USTR(str),file)
+#define pfopen(file,ext) _wfopen(file,USTR(ext))
+#else
+typedef char pchar;
+#define pprintf printf
+#define pfopen fopen
+#endif
+
+static hl_code *load_code( const pchar *file ) {
+	hl_code *code;
+	FILE *f = pfopen(file,"rb");
+	int pos, size;
+	char *fdata;
+	if( f == NULL ) {
+		pprintf("File not found '%s'\n",file);
+		return NULL;
+	}
+	fseek(f, 0, SEEK_END);
+	size = (int)ftell(f);
+	fseek(f, 0, SEEK_SET);
+	fdata = (char*)malloc(size);
+	pos = 0;
+	while( pos < size ) {
+		int r = (int)fread(fdata + pos, 1, size-pos, f);
+		if( r <= 0 ) {
+			pprintf("Failed to read '%s'\n",file);
+			return NULL;
+		}
+		pos += r;
+	}
+	fclose(f);
+	code = hl_code_read((unsigned char*)fdata, size);
+	free(fdata);
+	return code;
+}
+
+#ifdef HL_WIN
+int wmain(int argc, uchar *argv[]) {
+#else
+int main(int argc, char *argv[]) {
+#endif
+	struct {
+		hl_code *code;
+//		hl_module *m;
+		vdynamic *exc;
+	} ctx;
+	if( argc == 1 ) {
+		printf("HLVM %d.%d.%d (c)2015-2016 Haxe Foundation\n  Usage : hl <file>\n",HL_VERSION/100,(HL_VERSION/10)%10,HL_VERSION%10);
+		return 1;
+	}
+#	ifdef _DEBUG
+//	disable crt debug since it will prevent reusing our address space
+//	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_DELAY_FREE_MEM_DF /*| _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF*/ );
+#	endif
+	hl_global_init(&ctx);
+	hl_sys_init(argv + 2,argc - 2);
+	ctx.code = load_code(argv[1]);
+	if( ctx.code == NULL )
+		return 1;
+/*
+	m = hl_module_alloc(code);
+	if( m == NULL )
+		return 4;
+	if( !hl_module_init(m) )
+		return 5;
+	hl_callback(m->functions_ptrs[m->code->entrypoint],0,NULL);
+	hl_module_free(m);
+*/
+	hl_free(&ctx.code->alloc);
+	hl_global_free();
+	return 0;
+//on_exception:
+	{
+		varray *a = hl_exception_stack();
+		int i;
+		uprintf(USTR("Uncaught exception: %s\n"), hl_to_string(ctx.exc));
+		for(i=0;i<a->size;i++)
+			uprintf(USTR("Called from %s\n"), hl_aptr(a,uchar*)[i]);
+#		ifdef _DEBUG
+		_CrtCheckMemory();
+#		endif
+		hl_debug_break();
+	}
+	hl_global_free();
+	return 1;
+}
+

+ 50 - 23
src/opcodes.h

@@ -34,23 +34,29 @@ OP_BEGIN
 	OP(OInt,2)
 	OP(OFloat,2)
 	OP(OBool,2)
+	OP(OBytes,2)
 	OP(OString,2)
 	OP(ONull,1)
+
 	OP(OAdd,3)
 	OP(OSub,3)
 	OP(OMul,3)
 	OP(OSDiv,3)
 	OP(OUDiv,3)
+	OP(OSMod,3)
+	OP(OUMod,3)
 	OP(OShl,3)
 	OP(OSShr,3)
 	OP(OUShr,3)
 	OP(OAnd,3)
 	OP(OOr,3)
 	OP(OXor,3)
+
 	OP(ONeg,2)
 	OP(ONot,2)
 	OP(OIncr,1)
 	OP(ODecr,1)
+
 	OP(OCall0,2)
 	OP(OCall1,3)
 	OP(OCall2,4)
@@ -60,60 +66,81 @@ OP_BEGIN
 	OP(OCallMethod,-1)
 	OP(OCallThis,-1)
 	OP(OCallClosure,-1)
-	OP(OGetFunction,2)
-	OP(OClosure,3)
+
+	OP(OStaticClosure,2)
+	OP(OInstanceClosure,3)
+	OP(OVirtualClosure,3)
+
 	OP(OGetGlobal, 2)
 	OP(OSetGlobal,2)
-	OP(OEq,3)
-	OP(ONotEq,3)
-	OP(OSLt,3)
-	OP(OSGte,3)
-	OP(OULt,3)
-	OP(OUGte,3)
-	OP(ORet,1)
+	OP(OField,3)
+	OP(OSetField,3)
+	OP(OGetThis,2)
+	OP(OSetThis,2)
+	OP(ODynGet,3)
+	OP(ODynSet,3)
+
 	OP(OJTrue,2)
 	OP(OJFalse,2)
 	OP(OJNull,2)
 	OP(OJNotNull,2)
 	OP(OJSLt,3)
 	OP(OJSGte,3)
+	OP(OJSGt,3)
+	OP(OJSLte,3)
 	OP(OJULt,3)
 	OP(OJUGte,3)
 	OP(OJEq,3)
-	OP(OJNeq,3)
+	OP(OJNotEq,3)
 	OP(OJAlways,1)
+
 	OP(OToDyn,2)
-	OP(OToFloat,2)
+	OP(OToSFloat,2)
+	OP(OToUFloat,2)
 	OP(OToInt,2)
+	OP(OSafeCast,2)
+	OP(OUnsafeCast,2)
+	OP(OToVirtual,2)
+
 	OP(OLabel,0)
-	OP(ONew,1)
-	OP(OField,3)
-	OP(OMethod,3)
-	OP(OSetField,3)
-	OP(OGetThis,2)
-	OP(OSetThis,2)
+	OP(ORet,1)
 	OP(OThrow,1)
+	OP(ORethrow,1)
+	OP(OSwitch,-1)
+	OP(ONullCheck,1)
+	OP(OTrap,2)
+	OP(OEndTrap,1)
+
 	OP(OGetI8,3)
+	OP(OGetI16,3)
 	OP(OGetI32,3)
 	OP(OGetF32,3)
 	OP(OGetF64,3)
 	OP(OGetArray,3)
 	OP(OSetI8,3)
+	OP(OSetI16,3)
 	OP(OSetI32,3)
 	OP(OSetF32,3)
 	OP(OSetF64,3)
 	OP(OSetArray,3)
-	OP(OUnsafeCast,2)
+
+	OP(ONew,1)
 	OP(OArraySize,2)
-	OP(OError,1)
 	OP(OType,2)
+	OP(OGetType,2)
+	OP(OGetTID,2)
+
 	OP(ORef,2)
 	OP(OUnref,2)
 	OP(OSetref,2)
-	OP(OToVirtual,2)
-	OP(OUnVirtual,2)
-	OP(ODynGet,3)
-	OP(ODynSet,3)
+
+	OP(OMakeEnum,-1)
+	OP(OEnumAlloc,2)
+	OP(OEnumIndex,2)
+	OP(OEnumField,4)
+	OP(OSetEnumField,3)
+
+	OP(ODump,1)
 	// --
 	OP(OLast,0)
 OP_END