|
@@ -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);
|
|
|
|
+}
|