Attached patch adds native exec support for win32's COFF format .o files. I
had to change some code that assumed the bytecode being used was from the
interpreter->code->src block, which wasn't the case when it's not being mmaped.

__________________________________
Do you Yahoo!?
Yahoo! Search - Find what you’re looking for faster
http://search.yahoo.com
--- build_tools/jit2h.pl~       Tue Feb 10 01:38:56 2004
+++ build_tools/jit2h.pl        Tue Mar  2 16:08:04 2004
@@ -177,7 +177,7 @@
                 # The ->u.(string|float) is unnecessary.
                 $asm =~ s/\)->u\.(\w+)/)/g;
                 $asm =~ 
s/CONST\((\d)\)\s*([><=!]=?)\s*CONST\((\d)\)/RCONST($1)->u.number $2 
RCONST($3)->u.number/ if ($asm =~ /CONST.*CONST/);
-                $asm =~ 
s/(jit_emit_mov_([mr]i_ni).*)&jit_info->cur_op\[(\d)\]\);/$1(int)&jit_info->cur_op[$3] 
- (int)interpreter->code->src);\n\tParrot_exec_add_text_rellocation(jit_info->objfile, 
NULL, RTYPE_DATA, "program_code", 0);/g;
+                $asm =~ 
s/(jit_emit_mov_([mr]i_ni).*)&jit_info->cur_op\[(\d)\]\);/$1(int)&jit_info->cur_op[$3] 
- (int)interpreter->code->cur_cs->base.data + 
jit_info->objfile->bytecode_header_size);\n\tParrot_exec_add_text_rellocation(jit_info->objfile,
 NULL, RTYPE_DATA, "program_code", 0);/g;
                 $asm =~ 
s/(\n(\s*)[^\n]*((rm|mr|mi|ri)_(ni|n|i))[^\n]*[INSP]REG[^\n]*)/$1\n$2Parrot_exec_add_text_rellocation(jit_info->objfile,
 NULL, RTYPE_COM, "interpre", 0);/g;
                 $asm =~ 
s/(\n(\s*)[^\n]*((rm|mr|mi|ri)_(ni|n|i))[^\n]*CONST[^\n]*)/$1\n$2Parrot_exec_add_text_rellocation(jit_info->objfile,
 NULL, RTYPE_DATA, "const_table", 0);/g;
                 $asm =~ s/(\\\n\s*Parrot_exec_add_text_rellocation[^\n]*)/$1\\/g;
@@ -291,7 +291,7 @@
 #define NREG(i) EXR(num_reg.registers, jit_info->cur_op[i] * sizeof(FLOATVAL))
 #define PREG(i) EXR(pmc_reg.registers, jit_info->cur_op[i] * sizeof(PMC *))
 #define SREG(i) EXR(string_reg.registers, jit_info->cur_op[i] * sizeof(STRING *))
-#define CONST(i) (int *)(jit_info->cur_op[i] * sizeof(struct PackFile_Constant) + 4)
+#define CONST(i) (int *)(jit_info->cur_op[i] * sizeof(struct PackFile_Constant) + 
offsetof(struct PackFile_Constant, u))
 #define RCONST(i) interpreter->code->const_table->constants[jit_info->cur_op[i]]
 #define CALL(f) Parrot_exec_add_text_rellocation_func(jit_info->objfile, 
jit_info->native_ptr, f); \\
     emitm_calll(jit_info->native_ptr, EXEC_CALLDISP);
--- config/auto/jit.pl~ Wed Feb 25 21:15:50 2004
+++ config/auto/jit.pl  Sat Feb 28 10:14:02 2004
@@ -124,14 +124,15 @@
       jit_o       => '$(SRC)/jit$(O) $(SRC)/jit_cpu$(O) $(SRC)/jit_debug$(O) 
$(SRC)/jit_debug_xcoff$(O)'
     );
 
-    if (($jitcpuarch eq 'i386' && ($osname =~ /bsd$/i || $osname =~ /linux/i))
+    if (($jitcpuarch eq 'i386')
      || ($jitcpuarch eq 'ppc') || ($jitcpuarch eq 'arm')) {
       $execcapable = 1;
       unless (($osname eq 'openbsd') ||
               ($osname eq 'freebsd') ||
               ($osname eq 'netbsd')  ||
               ($osname eq 'linux')   ||
-              ($osname eq 'darwin'))
+              ($osname eq 'darwin')  ||
+              ($osname eq 'MSWin32'))
       {
         $execcapable = 0;
       }
--- include/parrot/exec.h~      Mon Dec  1 13:22:22 2003
+++ include/parrot/exec.h       Wed Feb 29 17:13:42 2004
@@ -28,9 +28,14 @@
 #   if (PARROT_EXEC_OS_FREEBSD) || (PARROT_EXEC_OS_NETBSD) || (PARROT_EXEC_OS_LINUX)
 #     define EXEC_ELF
 #   endif
+#   if PARROT_EXEC_OS_MSWIN32
+#     define EXEC_COFF
+#   endif
 
 #   ifdef EXEC_A_OUT
 #    define EXEC_CALLDISP jit_info->arena.start - jit_info->native_ptr - 4
+#   elif defined(EXEC_COFF)
+#    define EXEC_CALLDISP 0
 #   else
 #    define EXEC_CALLDISP 0xfffffffc
 #   endif
--- jit/i386/jit_emit.h~        Thu Jan  8 08:20:40 2004
+++ jit/i386/jit_emit.h Tue Mar  2 16:09:24 2004
@@ -1897,7 +1897,8 @@
 #    define PREG(i) EXR(pmc_reg.registers, i * sizeof(PMC *))
 #    define SREG(i) EXR(string_reg.registers, i * sizeof(STRING *))
 #    define CONST(i) (int *)(jit_info->cur_op[i] * \
-       sizeof(struct PackFile_Constant) + 4)
+       sizeof(struct PackFile_Constant) + \
+       offsetof(struct PackFile_Constant, u))
 
 #    define CALL(f) Parrot_exec_add_text_rellocation_func(jit_info->objfile, \
        jit_info->native_ptr, f); \
--- src/exec_save.c~    Mon Jan 26 15:16:06 2004
+++ src/exec_save.c     Tue Mar  2 17:23:14 2004
@@ -572,6 +572,133 @@
 
 #endif /* EXEC_MACH_O */
 
+#ifdef EXEC_COFF
+
+/* File offsets */
+#define TEXT_CODE  0x14 + (3 * 0x28)
+#define DATA_CODE  TEXT_CODE + obj->text.size
+#define TEXT_RELOC DATA_CODE + obj->data.size
+#define DATA_RELOC TEXT_RELOC + (obj->text_rellocation_count * 0xA)
+#define SYMTAB     DATA_RELOC + (obj->data_rellocation_count * 0xA)
+
+void
+Parrot_exec_save(Parrot_exec_objfile_t *obj, const char *file)
+{
+    FILE *fp;
+    int i;
+
+    fp = fopen(file, "wb");
+
+    save_short(fp, 0x14C); /* i386 */
+    save_short(fp, 3);     /* Number of sections */
+    save_int(fp, Parrot_intval_time());
+    save_int(fp, SYMTAB);
+    save_int(fp, obj->symbol_count);
+    save_short(fp, 0);
+    save_short(fp, 0x104); /* 32 bit LE, no line numbers */
+
+    fwrite(".text\0\0\0", 8, 1, fp);
+    save_int(fp, 0);
+    save_int(fp, 0);
+    save_int(fp, obj->text.size);
+    save_int(fp, TEXT_CODE);
+    save_int(fp, TEXT_RELOC);
+    save_int(fp, 0);
+    save_short(fp, obj->text_rellocation_count);
+    save_short(fp, 0);
+    save_int(fp, 0x20);
+
+    fwrite(".data\0\0\0", 8, 1, fp);
+    save_int(fp, 0);
+    save_int(fp, 0);
+    save_int(fp, obj->data.size);
+    save_int(fp, DATA_CODE);
+    save_int(fp, DATA_RELOC);
+    save_int(fp, 0);
+    save_short(fp, obj->data_rellocation_count);
+    save_short(fp, 0);
+    save_int(fp, 0x40);
+
+    fwrite(".bss\0\0\0\0", 8, 1, fp);
+    save_int(fp, 0);
+    save_int(fp, 0);
+    save_int(fp, obj->bss.size);
+    save_int(fp, 0);
+    save_int(fp, 0);
+    save_int(fp, 0);
+    save_short(fp, 0);
+    save_short(fp, 0);
+    save_int(fp, 0x80);
+
+    /* Text */
+    for (i = 0; i < obj->text.size; i++)
+        fprintf(fp, "%c", obj->text.code[i]);
+    /* Data */
+    for (i = 0; i < obj->data.size; i++)
+        fprintf(fp, "%c", obj->data.code[i]);
+    /* Text rellocations */
+    for (i = 0; i < obj->text_rellocation_count; i++) {
+        save_int(fp, obj->text_rellocation_table[i].offset);
+        save_int(fp, obj->text_rellocation_table[i].symbol_number);
+        switch (obj->text_rellocation_table[i].type) {
+            case RTYPE_FUNC:
+                save_short(fp, 0x14);
+                break;
+            case RTYPE_COM:
+            case RTYPE_DATA:
+                save_short(fp, 0x06);
+                break;
+            default:
+                internal_exception(EXEC_ERROR,
+                    "Unknown text rellocation type: %d\n",
+                        obj->text_rellocation_table[i].type);
+                break;
+        }
+    }
+    /* Symbol table */
+    for (i = 0; i < obj->symbol_count; i++) {
+        save_int(fp, 0);
+        save_int(fp, obj->symbol_table[i].offset_list);
+        save_int(fp, obj->symbol_table[i].value);
+        switch (obj->symbol_table[i].type) {
+            case STYPE_FUNC:
+                save_short(fp, 1); /* .text */
+                save_short(fp, 0x20);
+                break;
+            case STYPE_GDATA:
+                save_short(fp, 2); /* .data */
+                save_short(fp, 0);
+                break;
+            case STYPE_COM:
+                save_short(fp, 0);
+                save_short(fp, 0);
+                break;
+            case STYPE_UND:
+                save_short(fp, 0);
+                save_short(fp, 0x20);
+                break;
+            default:
+                internal_exception(EXEC_ERROR, "Unknown symbol type: %d\n",
+                    obj->symbol_table[i].type);
+                break;
+        }
+        putc(2, fp); /* "extern" class */
+        putc(0, fp);
+    }
+    /* Symbol list */
+    save_int(fp, obj->symbol_list_size);
+    for (i = 0; i < obj->symbol_count; i++) {
+        if (obj->symbol_table[i].type != STYPE_GCC)
+            fprintf(fp, "_%s", obj->symbol_table[i].symbol);
+        else
+            fprintf(fp, "%s", obj->symbol_table[i].symbol);
+        save_zero(fp);
+    }
+    fclose(fp);
+}
+
+#endif /* EXEC_COFF */
+
 /*
 
 =item C<static void save_struct(FILE *fp, void *sp, size_t size)>
--- src/exec_start.c~   Tue Mar  2 17:29:32 2004
+++ src/exec_start.c    Mon Mar  1 18:37:22 2004
@@ -53,13 +53,13 @@
 
     if (Interp_flags_TEST(interpreter, PARROT_DEBUG_FLAG)) {
         PIO_eprintf(interpreter,
-        "*** Parrot VM: Setting up ARGV array in P0.  Current argc: %d ***\n",
+        "*** Parrot VM: Setting up ARGV array in P5.  Current argc: %d ***\n",
                 argc);
     }
 
     userargv = pmc_new_noinit(interpreter, enum_class_SArray);
     /* immediately anchor pmc to root set */
-    interpreter->pmc_reg.registers[0] = userargv;
+    interpreter->pmc_reg.registers[5] = userargv;
     VTABLE_set_pmc_keyed_int(interpreter, interpreter->iglobals,
             (INTVAL)IGLOBALS_ARGV_LIST, userargv);
     VTABLE_init(interpreter, userargv);
--- src/exec.c~ Mon Jan 26 15:16:06 2004
+++ src/exec.c  Tue Mar  2 17:31:32 2004
@@ -72,8 +72,8 @@
     obj = mem_sys_allocate_zeroed(sizeof(Parrot_exec_objfile_t));
     exec_init(obj);
     Parrot_exec_rel_addr = (char **)mem_sys_allocate_zeroed(4 * sizeof(char *));
-    obj->bytecode_header_size = (int)interpreter->code->byte_code -
-        (int)interpreter->code->src;
+    obj->bytecode_header_size =
+        (interpreter->code->cur_cs->base.file_offset + 4) * sizeof(opcode_t);
     (void) build_asm(interpreter, pc, code_start, code_end, obj);
     jit_info = interpreter->jit_info;
 
@@ -134,8 +134,8 @@
     int *nds, i = 0;
 
     if (obj->data.size == 0) {
-        obj->data.code = (char *)mem_sys_allocate_zeroed(len); 
-        obj->data_size = (int *)mem_sys_allocate_zeroed(sizeof(int));
+        obj->data.code = (char *)mem_sys_allocate(len); 
+        obj->data_size = (int *)mem_sys_allocate(sizeof(int));
     }
     else {
         cp = (char *)mem_sys_realloc(obj->data.code, obj->data.size + len);
@@ -148,6 +148,8 @@
     cp = obj->data.code + obj->data.size;
     if (src)
         memcpy(cp, src, len);
+    else
+        memset(cp, 0, len);
     obj->data_size[obj->data_count++] = len;
     obj->data.size += len;
 }
@@ -221,7 +223,7 @@
         new_symbol->offset_list = obj->symbol_list_size;
         new_symbol->symbol = symbol;
         obj->symbol_list_size += strlen(symbol);
-#ifdef EXEC_A_OUT
+#if defined(EXEC_A_OUT) || defined(EXEC_COFF)
         /* for the trailing "_" */
         obj->symbol_list_size++;
 #endif
--- src/embed.c~        Sat Feb 28 09:32:28 2004
+++ src/embed.c Mon Mar  1 17:46:56 2004
@@ -356,7 +356,8 @@
         close(fd);   /* the man page states, it's ok to close a mmaped file */
     }
 #else
-    mem_sys_free(program_code);
+/* XXX Parrot_exec uses this
+    mem_sys_free(program_code); */
 #endif
 
     return pf;

Reply via email to