Ver código fonte

a bit more structure

Nicolas Cannasse 10 anos atrás
pai
commit
9df0ea01c5
7 arquivos alterados com 166 adições e 12 exclusões
  1. 3 0
      hl.vcxproj
  2. 0 9
      src/code.c
  3. 43 0
      src/global.c
  4. 19 0
      src/hl.h
  5. 28 0
      src/jit.c
  6. 5 3
      src/main.c
  7. 68 0
      src/module.c

+ 3 - 0
hl.vcxproj

@@ -75,7 +75,10 @@
   </ItemDefinitionGroup>
   <ItemGroup>
     <ClCompile Include="src\code.c" />
+    <ClCompile Include="src\global.c" />
+    <ClCompile Include="src\jit.c" />
     <ClCompile Include="src\main.c" />
+    <ClCompile Include="src\module.c" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="src\hl.h" />

+ 0 - 9
src/code.c

@@ -20,9 +20,6 @@
  * DEALINGS IN THE SOFTWARE.
  */
 #include "hl.h"
-#include <stdlib.h>
-#include <stdio.h>
-#include <memory.h>
 
 #define OP(_,n) n,
 #define OP_BEGIN static int hl_op_nargs[] = {
@@ -37,12 +34,6 @@ typedef struct {
 	hl_code *code;
 } hl_reader;
 
-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)

+ 43 - 0
src/global.c

@@ -0,0 +1,43 @@
+/*
+ * 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.
+ */
+#include "hl.h"
+
+void hl_global_init() {
+}
+
+void hl_global_free() {
+}
+
+int hl_type_size( hl_type *t ) {
+#	define PTR 4
+	static int SIZES[] = {
+		4,
+		1,
+		4,
+		4,
+		8,
+		1,
+		PTR * 2,
+		PTR,
+	};
+	return SIZES[t->kind];
+}

+ 19 - 0
src/hl.h

@@ -22,6 +22,10 @@
 #ifndef HL_H
 #define HL_H
 
+#include <stdlib.h>
+#include <stdio.h>
+#include <memory.h>
+
 #define HL_VERSION	010
 #include "opcodes.h"
 
@@ -93,9 +97,24 @@ typedef struct {
 	hl_function*functions;
 } hl_code;
 
+typedef struct {
+	hl_code *code;
+	int *globals_indexes;
+	unsigned char *globals_data;
+	void **functions_ptrs;
+} hl_module;
+
 void hl_global_init();
 void hl_global_free();
 
+int hl_type_size( hl_type *t );
+
 hl_code *hl_code_read( const unsigned char *data, int size );
+void hl_code_free( hl_code *c );
+
+hl_module *hl_module_alloc( hl_code *code );
+void hl_module_free( hl_module *m );
+
+void *hl_jit_function( hl_module *m, hl_function *f );
 
 #endif

+ 28 - 0
src/jit.c

@@ -0,0 +1,28 @@
+/*
+ * 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.
+ */
+#include "hl.h"
+
+void *hl_jit_function( hl_module *m, hl_function *f ) {
+	return NULL;
+}
+
+

+ 5 - 3
src/main.c

@@ -20,8 +20,6 @@
  * DEALINGS IN THE SOFTWARE.
  */
 #define _CRT_SECURE_NO_WARNINGS
-#include <stdio.h>
-#include <stdlib.h>
 #include "hl.h"
 
 int main( int argc, char *argv[] ) {
@@ -32,6 +30,7 @@ int main( int argc, char *argv[] ) {
 	hl_global_init();
 	{
 		hl_code *code;
+		hl_module *m;
 		const char *file = argv[1];
 		FILE *f = fopen(file,"rb");
 		int pos, size;
@@ -57,7 +56,10 @@ int main( int argc, char *argv[] ) {
 		code = hl_code_read((unsigned char*)fdata, size);
 		if( code == NULL )
 			return -1;
-		printf("TODO\n");
+		m = hl_module_alloc(code);
+		if( m == NULL )
+			return -1;
+		hl_code_free(code);
 	}
 	hl_global_free();
 	return 0;

+ 68 - 0
src/module.c

@@ -0,0 +1,68 @@
+/*
+ * 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.
+ */
+#include "hl.h"
+
+hl_module *hl_module_alloc( hl_code *c ) {
+	int i;
+	int gsize = 0;
+	hl_module *m = (hl_module*)malloc(sizeof(hl_module));
+	if( m == NULL )
+		return NULL;	
+	memset(m,0,sizeof(hl_module));
+	m->code = c;
+	m->globals_indexes = (int*)malloc(sizeof(int)*c->nglobals);
+	if( m == NULL ) {
+		hl_module_free(m);
+		return NULL;
+	}
+	for(i=0;i<c->nglobals;i++) {
+		m->globals_indexes[i] = gsize;
+		gsize += hl_type_size(c->globals[i]);
+	}
+	m->globals_data = (unsigned char*)malloc(gsize);
+	if( m->globals_data == NULL ) {
+		hl_module_free(m);
+		return NULL;
+	}
+	memset(m->globals_data,0,gsize);
+	m->functions_ptrs = (void**)malloc(sizeof(void*)*c->nfunctions);
+	if( m->functions_ptrs == NULL ) {
+		hl_module_free(m);
+		return NULL;
+	}
+	memset(m->functions_ptrs,0,sizeof(void*)*c->nfunctions);
+	for(i=0;i<c->nfunctions;i++) {
+		m->functions_ptrs[i] = hl_jit_function(m,c->functions+i);
+		if( m->functions_ptrs[i] == NULL ) {
+			printf("Failed to JIT fun#%d\n",i);
+			hl_module_free(m);
+			return NULL;
+		}
+	}
+	return m;
+}
+
+void hl_module_free( hl_module *m ) {
+	free(m->globals_indexes);
+	free(m->globals_data);
+	free(m);
+}