first self virtualizable version
[qemu] / dyngen.c
index ce38dca..1eb8032 100644 (file)
--- a/dyngen.c
+++ b/dyngen.c
@@ -1,17 +1,83 @@
+/*
+ *  Generic Dynamic compiler generator
+ * 
+ *  Copyright (c) 2003 Fabrice Bellard
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 #include <stdarg.h>
 #include <inttypes.h>
-#include <elf.h>
 #include <unistd.h>
 #include <fcntl.h>
 
+#include "config.h"
+
+/* elf format definitions. We use these macros to test the CPU to
+   allow cross compilation (this tool must be ran on the build
+   platform) */
+#if defined(HOST_I386)
+
+#define ELF_CLASS      ELFCLASS32
+#define ELF_ARCH       EM_386
+#define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
+#undef ELF_USES_RELOCA
+
+#elif defined(HOST_PPC)
+
+#define ELF_CLASS      ELFCLASS32
+#define ELF_ARCH       EM_PPC
+#define elf_check_arch(x) ((x) == EM_PPC)
+#define ELF_USES_RELOCA
+
+#elif defined(HOST_S390)
+
+#define ELF_CLASS      ELFCLASS32
+#define ELF_ARCH       EM_S390
+#define elf_check_arch(x) ((x) == EM_S390)
+#define ELF_USES_RELOCA
+
+#elif defined(HOST_ALPHA)
+
+#define ELF_CLASS      ELFCLASS64
+#define ELF_ARCH       EM_ALPHA
+#define elf_check_arch(x) ((x) == EM_ALPHA)
+#define ELF_USES_RELOCA
+
+#else
+#error unsupported CPU - please update the code
+#endif
+
+#if ELF_CLASS == ELFCLASS32
+typedef int32_t host_long;
+typedef uint32_t host_ulong;
+#else
+typedef int64_t host_long;
+typedef uint64_t host_ulong;
+#endif
+
+#include "elf.h"
+
 #include "thunk.h"
 
 /* all dynamically generated functions begin with this code */
-#define OP_PREFIX "op"
+#define OP_PREFIX "op_"
 
-int elf_must_swap(Elf32_Ehdr *h)
+int elf_must_swap(struct elfhdr *h)
 {
   union {
       uint32_t i;
@@ -33,19 +99,25 @@ void swab32s(uint32_t *p)
     *p = bswap32(*p);
 }
 
-void swab64s(uint32_t *p)
+void swab64s(uint64_t *p)
 {
     *p = bswap64(*p);
 }
 
-void elf_swap_ehdr(Elf32_Ehdr *h)
+#if ELF_CLASS == ELFCLASS32
+#define swabls(x) swab32s(x)
+#else
+#define swabls(x) swab64s(x)
+#endif
+
+void elf_swap_ehdr(struct elfhdr *h)
 {
     swab16s(&h->e_type);                       /* Object file type */
     swab16s(&h->       e_machine);             /* Architecture */
     swab32s(&h->       e_version);             /* Object file version */
-    swab32s(&h->       e_entry);               /* Entry point virtual address */
-    swab32s(&h->       e_phoff);               /* Program header table file offset */
-    swab32s(&h->       e_shoff);               /* Section header table file offset */
+    swabls(&h->        e_entry);               /* Entry point virtual address */
+    swabls(&h->        e_phoff);               /* Program header table file offset */
+    swabls(&h->        e_shoff);               /* Section header table file offset */
     swab32s(&h->       e_flags);               /* Processor-specific flags */
     swab16s(&h->       e_ehsize);              /* ELF header size in bytes */
     swab16s(&h->       e_phentsize);           /* Program header table entry size */
@@ -55,34 +127,33 @@ void elf_swap_ehdr(Elf32_Ehdr *h)
     swab16s(&h->       e_shstrndx);            /* Section header string table index */
 }
 
-void elf_swap_shdr(Elf32_Shdr *h)
+void elf_swap_shdr(struct elf_shdr *h)
 {
   swab32s(&h-> sh_name);               /* Section name (string tbl index) */
   swab32s(&h-> sh_type);               /* Section type */
-  swab32s(&h-> sh_flags);              /* Section flags */
-  swab32s(&h-> sh_addr);               /* Section virtual addr at execution */
-  swab32s(&h-> sh_offset);             /* Section file offset */
-  swab32s(&h-> sh_size);               /* Section size in bytes */
+  swabls(&h->  sh_flags);              /* Section flags */
+  swabls(&h->  sh_addr);               /* Section virtual addr at execution */
+  swabls(&h->  sh_offset);             /* Section file offset */
+  swabls(&h->  sh_size);               /* Section size in bytes */
   swab32s(&h-> sh_link);               /* Link to another section */
   swab32s(&h-> sh_info);               /* Additional section information */
-  swab32s(&h-> sh_addralign);          /* Section alignment */
-  swab32s(&h-> sh_entsize);            /* Entry size if section holds table */
+  swabls(&h->  sh_addralign);          /* Section alignment */
+  swabls(&h->  sh_entsize);            /* Entry size if section holds table */
 }
 
-void elf_swap_phdr(Elf32_Phdr *h)
+void elf_swap_phdr(struct elf_phdr *h)
 {
     swab32s(&h->p_type);                       /* Segment type */
-    swab32s(&h->p_offset);             /* Segment file offset */
-    swab32s(&h->p_vaddr);              /* Segment virtual address */
-    swab32s(&h->p_paddr);              /* Segment physical address */
-    swab32s(&h->p_filesz);             /* Segment size in file */
-    swab32s(&h->p_memsz);              /* Segment size in memory */
+    swabls(&h->p_offset);              /* Segment file offset */
+    swabls(&h->p_vaddr);               /* Segment virtual address */
+    swabls(&h->p_paddr);               /* Segment physical address */
+    swabls(&h->p_filesz);              /* Segment size in file */
+    swabls(&h->p_memsz);               /* Segment size in memory */
     swab32s(&h->p_flags);              /* Segment flags */
-    swab32s(&h->p_align);              /* Segment alignment */
+    swabls(&h->p_align);               /* Segment alignment */
 }
 
 int do_swap;
-int e_machine;
 
 uint16_t get16(uint16_t *p)
 {
@@ -128,12 +199,12 @@ void __attribute__((noreturn)) error(const char *fmt, ...)
 }
 
 
-Elf32_Shdr *find_elf_section(Elf32_Shdr *shdr, int shnum, const char *shstr, 
-                             const char *name)
+struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr, 
+                                  const char *name)
 {
     int i;
     const char *shname;
-    Elf32_Shdr *sec;
+    struct elf_shdr *sec;
 
     for(i = 0; i < shnum; i++) {
         sec = &shdr[i];
@@ -180,32 +251,29 @@ int strstart(const char *str, const char *val, const char **ptr)
 #define MAX_ARGS 3
 
 /* generate op code */
-void gen_code(const char *name, unsigned long offset, unsigned long size, 
-              FILE *outfile, uint8_t *text, void *relocs, int nb_relocs, int reloc_sh_type,
-              Elf32_Sym *symtab, char *strtab)
+void gen_code(const char *name, host_ulong offset, host_ulong size, 
+              FILE *outfile, uint8_t *text, ELF_RELOC *relocs, int nb_relocs, int reloc_sh_type,
+              ElfW(Sym) *symtab, char *strtab, int gen_switch)
 {
     int copy_size = 0;
     uint8_t *p_start, *p_end;
-    int nb_args, i;
+    int nb_args, i, n;
     uint8_t args_present[MAX_ARGS];
     const char *sym_name, *p;
+    ELF_RELOC *rel;
 
     /* compute exact size excluding return instruction */
     p_start = text + offset;
     p_end = p_start + size;
-    switch(e_machine) {
+    switch(ELF_ARCH) {
     case EM_386:
         {
             uint8_t *p;
             p = p_end - 1;
-            /* find ret */
-            while (p > p_start && *p != 0xc3)
-                p--;
-            /* skip double ret */
-            if (p > p_start && p[-1] == 0xc3)
-                p--;
             if (p == p_start)
                 error("empty code for %s", name);
+            if (p[0] != 0xc3)
+                error("ret expected at the end of %s", name);
             copy_size = p - p_start;
         }
         break;
@@ -213,55 +281,38 @@ void gen_code(const char *name, unsigned long offset, unsigned long size,
         {
             uint8_t *p;
             p = (void *)(p_end - 4);
-            /* find ret */
-            while (p > p_start && get32((uint32_t *)p) != 0x4e800020)
-                p -= 4;
-            /* skip double ret */
-            if (p > p_start && get32((uint32_t *)(p - 4)) == 0x4e800020)
-                p -= 4;
             if (p == p_start)
                 error("empty code for %s", name);
+            if (get32((uint32_t *)p) != 0x4e800020)
+                error("blr expected at the end of %s", name);
             copy_size = p - p_start;
         }
         break;
-    default:
-        error("unsupported CPU (%d)", e_machine);
+    case EM_S390:
+       {
+           uint8_t *p;
+           p = (void *)(p_end - 2);
+           if (p == p_start)
+               error("empty code for %s", name);
+           if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4)
+               error("br %r14 expected at the end of %s", name);
+           copy_size = p - p_start;
+       }
+        break;
     }
 
     /* compute the number of arguments by looking at the relocations */
     for(i = 0;i < MAX_ARGS; i++)
         args_present[i] = 0;
 
-    if (reloc_sh_type == SHT_REL) {
-        Elf32_Rel *rel;
-        int n;
-        for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
-            if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
-                sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
-                if (strstart(sym_name, "__op_param", &p)) {
-                    n = strtoul(p, NULL, 10);
-                    if (n >= MAX_ARGS)
-                        error("too many arguments in %s", name);
-                    args_present[n - 1] = 1;
-                } else {
-                    fprintf(outfile, "extern char %s;\n", sym_name);
-                }
-            }
-        }
-    } else {
-        Elf32_Rela *rel;
-        int n;
-        for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
-            if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
-                sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
-                if (strstart(sym_name, "__op_param", &p)) {
-                    n = strtoul(p, NULL, 10);
-                    if (n >= MAX_ARGS)
-                        error("too many arguments in %s", name);
-                    args_present[n - 1] = 1;
-                } else {
-                    fprintf(outfile, "extern char %s;\n", sym_name);
-                }
+    for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
+        if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
+            sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
+            if (strstart(sym_name, "__op_param", &p)) {
+                n = strtoul(p, NULL, 10);
+                if (n >= MAX_ARGS)
+                    error("too many arguments in %s", name);
+                args_present[n - 1] = 1;
             }
         }
     }
@@ -274,31 +325,44 @@ void gen_code(const char *name, unsigned long offset, unsigned long size,
             error("inconsistent argument numbering in %s", name);
     }
 
-    /* output C code */
-    fprintf(outfile, "extern void %s();\n", name);
-    fprintf(outfile, "static inline void gen_%s(", name);
-    if (nb_args == 0) {
-        fprintf(outfile, "void");
-    } else {
+    if (gen_switch == 2) {
+        fprintf(outfile, "DEF(%s, %d)\n", name + 3, nb_args);
+    } else if (gen_switch == 1) {
+
+        /* output C code */
+        fprintf(outfile, "case INDEX_%s: {\n", name);
+        if (nb_args > 0) {
+            fprintf(outfile, "    long ");
+            for(i = 0; i < nb_args; i++) {
+                if (i != 0)
+                    fprintf(outfile, ", ");
+                fprintf(outfile, "param%d", i + 1);
+            }
+            fprintf(outfile, ";\n");
+        }
+        fprintf(outfile, "    extern void %s();\n", name);
+
+        for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
+            if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
+                sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
+                if (!strstart(sym_name, "__op_param", &p)) {
+                    fprintf(outfile, "extern char %s;\n", sym_name);
+                }
+            }
+        }
+
+        fprintf(outfile, "    memcpy(gen_code_ptr, &%s, %d);\n", name, copy_size);
         for(i = 0; i < nb_args; i++) {
-            if (i != 0)
-                fprintf(outfile, ", ");
-            fprintf(outfile, "long param%d", i + 1);
+            fprintf(outfile, "    param%d = *opparam_ptr++;\n", i + 1);
         }
-    }
-    fprintf(outfile, ")\n");
-    fprintf(outfile, "{\n");
-    fprintf(outfile, "    memcpy(gen_code_ptr, &%s, %d);\n", name, copy_size);
-    
-    /* patch relocations */
-    switch(e_machine) {
-    case EM_386:
-        {
-            Elf32_Rel *rel;
-            char name[256];
-            int type;
-            long addend;
-            for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
+
+        /* patch relocations */
+#if defined(HOST_I386)
+            {
+                char name[256];
+                int type;
+                int addend;
+                for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
                 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
                     sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
                     if (strstart(sym_name, "__op_param", &p)) {
@@ -310,38 +374,131 @@ void gen_code(const char *name, unsigned long offset, unsigned long size,
                     addend = get32((uint32_t *)(text + rel->r_offset));
                     switch(type) {
                     case R_386_32:
-                        fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %ld) = %s + %ld;\n", 
+                        fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", 
                                 rel->r_offset - offset, name, addend);
                         break;
                     case R_386_PC32:
-                        fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %ld) = %s - (long)(gen_code_ptr + %ld) + %ld;\n", 
+                        fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n", 
                                 rel->r_offset - offset, name, rel->r_offset - offset, addend);
                         break;
                     default:
                         error("unsupported i386 relocation (%d)", type);
                     }
                 }
+                }
+            }
+#elif defined(HOST_PPC)
+            {
+                char name[256];
+                int type;
+                int addend;
+                for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
+                    if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
+                        sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
+                        if (strstart(sym_name, "__op_param", &p)) {
+                            snprintf(name, sizeof(name), "param%s", p);
+                        } else {
+                            snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
+                        }
+                        type = ELF32_R_TYPE(rel->r_info);
+                        addend = rel->r_addend;
+                        switch(type) {
+                        case R_PPC_ADDR32:
+                            fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", 
+                                    rel->r_offset - offset, name, addend);
+                            break;
+                        case R_PPC_ADDR16_LO:
+                            fprintf(outfile, "    *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n", 
+                                    rel->r_offset - offset, name, addend);
+                            break;
+                        case R_PPC_ADDR16_HI:
+                            fprintf(outfile, "    *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n", 
+                                    rel->r_offset - offset, name, addend);
+                            break;
+                        case R_PPC_ADDR16_HA:
+                            fprintf(outfile, "    *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n", 
+                                    rel->r_offset - offset, name, addend);
+                            break;
+                        case R_PPC_REL24:
+                            /* warning: must be at 32 MB distancy */
+                            fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n", 
+                                    rel->r_offset - offset, rel->r_offset - offset, name, rel->r_offset - offset, addend);
+                            break;
+                        default:
+                            error("unsupported powerpc relocation (%d)", type);
+                        }
+                    }
+                }
+            }
+#elif defined(HOST_S390)
+            {
+                char name[256];
+                int type;
+                int addend;
+                for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
+                    if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
+                        sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
+                        if (strstart(sym_name, "__op_param", &p)) {
+                            snprintf(name, sizeof(name), "param%s", p);
+                        } else {
+                            snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
+                        }
+                        type = ELF32_R_TYPE(rel->r_info);
+                        addend = rel->r_addend;
+                        switch(type) {
+                        case R_390_32:
+                            fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", 
+                                    rel->r_offset - offset, name, addend);
+                            break;
+                        case R_390_16:
+                            fprintf(outfile, "    *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n", 
+                                    rel->r_offset - offset, name, addend);
+                            break;
+                        case R_390_8:
+                            fprintf(outfile, "    *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n", 
+                                    rel->r_offset - offset, name, addend);
+                            break;
+                        default:
+                            error("unsupported s390 relocation (%d)", type);
+                        }
+                    }
+                }
+            }
+#else
+#error unsupported CPU
+#endif
+        fprintf(outfile, "    gen_code_ptr += %d;\n", copy_size);
+        fprintf(outfile, "}\n");
+        fprintf(outfile, "break;\n\n");
+    } else {
+        fprintf(outfile, "static inline void gen_%s(", name);
+        if (nb_args == 0) {
+            fprintf(outfile, "void");
+        } else {
+            for(i = 0; i < nb_args; i++) {
+                if (i != 0)
+                    fprintf(outfile, ", ");
+                fprintf(outfile, "long param%d", i + 1);
             }
         }
-        break;
-    default:
-        error("unsupported CPU for relocations (%d)", e_machine);
+        fprintf(outfile, ")\n");
+        fprintf(outfile, "{\n");
+        for(i = 0; i < nb_args; i++) {
+            fprintf(outfile, "    *gen_opparam_ptr++ = param%d;\n", i + 1);
+        }
+        fprintf(outfile, "    *gen_opc_ptr++ = INDEX_%s;\n", name);
+        fprintf(outfile, "}\n\n");
     }
-
-
-    fprintf(outfile, "    gen_code_ptr += %d;\n", copy_size);
-    fprintf(outfile, "}\n\n");
 }
 
 /* load an elf object file */
-int load_elf(const char *filename, FILE *outfile)
+int load_elf(const char *filename, FILE *outfile, int do_print_enum)
 {
     int fd;
-    Elf32_Ehdr ehdr;
-    Elf32_Shdr *sec, *shdr, *symtab_sec, *strtab_sec, *text_sec;
+    struct elfhdr ehdr;
+    struct elf_shdr *sec, *shdr, *symtab_sec, *strtab_sec, *text_sec;
     int i, j, nb_syms;
-    Elf32_Sym *symtab, *sym;
-    const char *cpu_name;
+    ElfW(Sym) *symtab, *sym;
     char *shstr, *strtab;
     uint8_t *text;
     void *relocs;
@@ -360,7 +517,6 @@ int load_elf(const char *filename, FILE *outfile)
      || ehdr.e_ident[EI_MAG1] != ELFMAG1
      || ehdr.e_ident[EI_MAG2] != ELFMAG2
      || ehdr.e_ident[EI_MAG3] != ELFMAG3
-     || ehdr.e_ident[EI_CLASS] != ELFCLASS32
      || ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
         error("bad ELF header");
     }
@@ -368,14 +524,17 @@ int load_elf(const char *filename, FILE *outfile)
     do_swap = elf_must_swap(&ehdr);
     if (do_swap)
         elf_swap_ehdr(&ehdr);
+    if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
+        error("Unsupported ELF class");
     if (ehdr.e_type != ET_REL)
         error("ELF object file expected");
     if (ehdr.e_version != EV_CURRENT)
         error("Invalid ELF version");
-    e_machine = ehdr.e_machine;
+    if (!elf_check_arch(ehdr.e_machine))
+        error("Unsupported CPU (e_machine=%d)", ehdr.e_machine);
 
     /* read section headers */
-    shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(Elf32_Shdr));
+    shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr));
     if (do_swap) {
         for(i = 0; i < ehdr.e_shnum; i++) {
             elf_swap_shdr(&shdr[i]);
@@ -435,49 +594,88 @@ int load_elf(const char *filename, FILE *outfile)
     if (do_swap) {
         for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
             swab32s(&sym->st_name);
-            swab32s(&sym->st_value);
-            swab32s(&sym->st_size);
+            swabls(&sym->st_value);
+            swabls(&sym->st_size);
             swab16s(&sym->st_shndx);
         }
     }
 
-    switch(e_machine) {
+    if (do_print_enum) {
+        fprintf(outfile, "DEF(end, 0)\n");
+        for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
+            const char *name, *p;
+            name = strtab + sym->st_name;
+            if (strstart(name, OP_PREFIX, &p)) {
+                gen_code(name, sym->st_value, sym->st_size, outfile, 
+                         text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 2);
+            }
+        }
+    } else {
+        /* generate big code generation switch */
+fprintf(outfile,
+"int dyngen_code(uint8_t *gen_code_buf,\n"
+"                const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
+"{\n"
+"    uint8_t *gen_code_ptr;\n"
+"    const uint16_t *opc_ptr;\n"
+"    const uint32_t *opparam_ptr;\n"
+"    gen_code_ptr = gen_code_buf;\n"
+"    opc_ptr = opc_buf;\n"
+"    opparam_ptr = opparam_buf;\n"
+"    for(;;) {\n"
+"        switch(*opc_ptr++) {\n"
+);
+
+        for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
+            const char *name;
+            name = strtab + sym->st_name;
+            if (strstart(name, OP_PREFIX, NULL)) {
+#if 0
+                printf("%4d: %s pos=0x%08x len=%d\n", 
+                       i, name, sym->st_value, sym->st_size);
+#endif
+                if (sym->st_shndx != (text_sec - shdr))
+                    error("invalid section for opcode (0x%x)", sym->st_shndx);
+                gen_code(name, sym->st_value, sym->st_size, outfile, 
+                         text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 1);
+            }
+        }
+
+fprintf(outfile,
+"        default:\n"
+"            goto the_end;\n"
+"        }\n"
+"    }\n"
+" the_end:\n"
+);
+
+/* generate a return */ 
+    switch(ELF_ARCH) {
     case EM_386:
-        cpu_name = "i386";
+        fprintf(outfile, "*gen_code_ptr++ = 0xc3; /* ret */\n");
         break;
     case EM_PPC:
-        cpu_name = "ppc";
+        fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x4e800020; /* blr */\n");
         break;
-    case EM_MIPS:
-        cpu_name = "mips";
+    case EM_S390:
+        fprintf(outfile, "*((uint16_t *)gen_code_ptr)++ = 0x07fe; /* br %%r14 */\n");
         break;
-    case EM_ARM:
-        cpu_name = "arm";
-        break;
-    case EM_SPARC:
-        cpu_name = "sparc";
-        break;
-    default:
-        error("unsupported CPU (e_machine=%d)", e_machine);
     }
+    
+    fprintf(outfile, "return gen_code_ptr -  gen_code_buf;\n");
+    fprintf(outfile, "}\n\n");
 
-    fprintf(outfile, "#include \"gen-%s.h\"\n\n", cpu_name);
-
-    for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
-        const char *name;
-        name = strtab + sym->st_name;
-        if (strstart(name, "op_", NULL) ||
-            strstart(name, "op1_", NULL) ||
-            strstart(name, "op2_", NULL) ||
-            strstart(name, "op3_", NULL)) {
-#if 0
-            printf("%4d: %s pos=0x%08x len=%d\n", 
-                   i, name, sym->st_value, sym->st_size);
-#endif
-            if (sym->st_shndx != (text_sec - shdr))
-                error("invalid section for opcode (0x%x)", sym->st_shndx);
-            gen_code(name, sym->st_value, sym->st_size, outfile, 
-                     text, relocs, nb_relocs, reloc_sh_type, symtab, strtab);
+/* generate gen_xxx functions */
+/* XXX: suppress the use of these functions to simplify code */
+        for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
+            const char *name;
+            name = strtab + sym->st_name;
+            if (strstart(name, OP_PREFIX, NULL)) {
+                if (sym->st_shndx != (text_sec - shdr))
+                    error("invalid section for opcode (0x%x)", sym->st_shndx);
+                gen_code(name, sym->st_value, sym->st_size, outfile, 
+                         text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 0);
+            }
         }
     }
 
@@ -488,20 +686,23 @@ int load_elf(const char *filename, FILE *outfile)
 void usage(void)
 {
     printf("dyngen (c) 2003 Fabrice Bellard\n"
-           "usage: dyngen [-o outfile] objfile\n"
-           "Generate a dynamic code generator from an object file\n");
+           "usage: dyngen [-o outfile] [-c] objfile\n"
+           "Generate a dynamic code generator from an object file\n"
+           "-c     output enum of operations\n"
+           );
     exit(1);
 }
 
 int main(int argc, char **argv)
 {
-    int c;
+    int c, do_print_enum;
     const char *filename, *outfilename;
     FILE *outfile;
 
     outfilename = "out.c";
+    do_print_enum = 0;
     for(;;) {
-        c = getopt(argc, argv, "ho:");
+        c = getopt(argc, argv, "ho:c");
         if (c == -1)
             break;
         switch(c) {
@@ -511,6 +712,9 @@ int main(int argc, char **argv)
         case 'o':
             outfilename = optarg;
             break;
+        case 'c':
+            do_print_enum = 1;
+            break;
         }
     }
     if (optind >= argc)
@@ -519,7 +723,7 @@ int main(int argc, char **argv)
     outfile = fopen(outfilename, "w");
     if (!outfile)
         error("could not open '%s'", outfilename);
-    load_elf(filename, outfile);
+    load_elf(filename, outfile, do_print_enum);
     fclose(outfile);
     return 0;
 }