Add a compile/disassemble/diff test function
authorDavid Schleef <ds@schleef.org>
Sat, 28 Mar 2009 07:00:49 +0000 (00:00 -0700)
committerDavid Schleef <ds@schleef.org>
Sat, 28 Mar 2009 07:06:57 +0000 (00:06 -0700)
orc-test/orctest.c
orc-test/orctest.h

index 64fb0b3..05b935d 100644 (file)
@@ -1,10 +1,77 @@
 
 #include <orc-test/orctest.h>
 
+#include <stdio.h>
+#include <stdlib.h>
+
 
 void
 orc_test_init (void)
 {
+  orc_init ();
+
+}
+
+
+
+
+int
+orc_test_gcc_compile (OrcProgram *p)
+{
+  char cmd[200];
+  int ret;
+  FILE *file;
+
+  ret = orc_program_compile (p);
+  if (!ret) {
+    return FALSE;
+  }
 
+  fflush (stdout);
+
+  file = fopen ("tmp.s", "w");
+  fprintf(file, "%s", orc_program_get_asm_code (p));
+  fclose (file);
+
+  file = fopen ("dump", "w");
+  ret = fwrite(p->code, p->code_size, 1, file);
+  fclose (file);
+
+  ret = system ("gcc -Wall -c tmp.s");
+  if (ret != 0) {
+    printf("gcc failed\n");
+    return FALSE;
+  }
+
+  ret = system ("objdump -dr tmp.o >tmp.dis");
+  if (ret != 0) {
+    printf("objdump failed\n");
+    return FALSE;
+  }
+
+  sprintf (cmd, "objcopy -I binary -O elf32-i386 -B i386 "
+      "--rename-section .data=.text "
+      "--redefine-sym _binary_dump_start=%s "
+      "dump tmp.o", p->name);
+  ret = system (cmd);
+  if (ret != 0) {
+    printf("objcopy failed\n");
+    return FALSE;
+  }
+
+  ret = system ("objdump -Dr tmp.o >tmp-dump.dis");
+  if (ret != 0) {
+    printf("objdump failed\n");
+    return FALSE;
+  }
+
+  ret = system ("diff -u tmp.dis tmp-dump.dis");
+  if (ret != 0) {
+    printf("diff failed\n");
+    return FALSE;
+  }
+
+  return TRUE;
 }
 
+
index 6b08953..a8ca59f 100644 (file)
@@ -8,6 +8,7 @@
 ORC_BEGIN_DECLS
 
 void orc_test_init (void);
+int orc_test_gcc_compile (OrcProgram *p);
 
 ORC_END_DECLS