Nicolas Cannasse 10 years ago
parent
commit
be0a8be4fa
4 changed files with 260 additions and 0 deletions
  1. 12 0
      .gitignore
  2. 20 0
      hl.sln
  3. 82 0
      hl.vcxproj
  4. 146 0
      src/main.c

+ 12 - 0
.gitignore

@@ -0,0 +1,12 @@
+/Debug
+
+*.opensdf
+
+*.sdf
+
+*.suo
+*.ipch
+
+*.filters
+
+*.user

+ 20 - 0
hl.sln

@@ -0,0 +1,20 @@
+
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual C++ Express 2010
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "hl", "hl.vcxproj", "{80755B1F-41F6-4C73-B3F3-8325AEAE1016}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Win32 = Debug|Win32
+		Release|Win32 = Release|Win32
+	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}.Release|Win32.ActiveCfg = Release|Win32
+		{80755B1F-41F6-4C73-B3F3-8325AEAE1016}.Release|Win32.Build.0 = Release|Win32
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+EndGlobal

+ 82 - 0
hl.vcxproj

@@ -0,0 +1,82 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.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="Release|Win32">
+      <Configuration>Release</Configuration>
+      <Platform>Win32</Platform>
+    </ProjectConfiguration>
+  </ItemGroup>
+  <PropertyGroup Label="Globals">
+    <ProjectGuid>{80755B1F-41F6-4C73-B3F3-8325AEAE1016}</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>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>false</UseDebugLibraries>
+    <WholeProgramOptimization>true</WholeProgramOptimization>
+    <CharacterSet>Unicode</CharacterSet>
+  </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 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>
+  <PropertyGroup Label="UserMacros" />
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <LinkIncremental>true</LinkIncremental>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <LinkIncremental>false</LinkIncremental>
+  </PropertyGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <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>
+      <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\main.c" />
+  </ItemGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+  <ImportGroup Label="ExtensionTargets">
+  </ImportGroup>
+</Project>

+ 146 - 0
src/main.c

@@ -0,0 +1,146 @@
+/*
+ * Copyright (C)2015 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.
+ */
+#define _CRT_SECURE_NO_WARNINGS
+#include <stdio.h>
+#include <stdlib.h>
+#include <memory.h>
+
+typedef struct {
+	int version;
+	int nglobals;
+	int nfloats;
+	int nfunctions;
+	int nnatives;
+	int entrypoint;
+} hl_code;
+
+typedef struct {
+	const unsigned char *b;
+	int size;
+	int pos;
+	int error;
+} hl_reader;
+
+#define HL_VERSION	010
+
+void hl_global_init() {
+}
+
+void hl_global_free() {
+}
+
+#define READ() hl_read_b(r)
+#define INDEX() hl_read_index(r)
+#define UINDEX() hl_read_uindex(r)
+#define ERROR(msg) { printf("%s\n",msg); hl_code_free(c); return NULL; }
+
+static unsigned char hl_read_b( hl_reader *r ) {
+	if( r->pos >= r->size ) {
+		r->error = 1;
+		return 0;
+	}
+	return r->b[r->pos++];
+}
+
+static int hl_read_index( hl_reader *r ) {
+	unsigned char b = READ();
+	if( (b & 0x80) == 0 )
+		return b & 0x7F;
+	if( (b & 0x40) == 0 ) {
+		int v = READ() | ((b & 31) << 8);
+		return (b & 0x20) == 0 ? v : -v;
+	}
+	{
+		int c = READ();
+		int d = READ();
+		int e = READ();
+		int v = ((b & 31) << 24) | (c << 16) | (d << 8) | e;
+		return (b & 0x20) == 0 ? v : -v;
+	}
+}
+
+static int hl_read_uindex( hl_reader *r ) {
+	int i = hl_read_index(r);
+	if( i < 0 ) r->error = 1;
+	return i;
+}
+
+void hl_code_free( hl_code *c ) {
+	free(c);
+}
+
+hl_code *hl_code_read( const unsigned char *data, int size ) {
+	hl_reader _r = { data, size, 0, 0 };	
+	hl_reader *r = &_r;
+	hl_code *c = (hl_code*)malloc(sizeof(hl_code));
+	memset(c,0,sizeof(hl_code));
+	if( READ() != 'H' || READ() != 'L' || READ() != 'B' )
+		ERROR("Invalid HL header");
+	c->version = READ();
+	c->nglobals = UINDEX();
+	c->nfloats = UINDEX();
+	c->nnatives = UINDEX();
+	c->nfunctions = UINDEX();
+	c->entrypoint = UINDEX();
+	if( r->error )
+		ERROR("Invalid HL counts");
+	return NULL;
+}
+
+
+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);
+		return 1;
+	}
+	hl_global_init();
+	{
+		hl_code *code;
+		const char *file = argv[1];
+		FILE *f = fopen(file,"rb");
+		int pos, size;
+		char *fdata;
+		if( f == NULL ) {
+			printf("File not found '%s'\n",file);
+			return -1;
+		}
+		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 ) {
+				printf("Failed to read '%s'\n",file);
+				return -1;
+			}
+			pos += r;
+		}
+		fclose(f);
+		code = hl_code_read((unsigned char*)fdata, size);
+		if( code == NULL )
+			return -1;
+	}
+	hl_global_free();
+	return 0;
+}