00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00038
00039
00040
00041
00043
00044
00045 #include <stdio.h>
00046 #include <stdlib.h>
00047 #include <string.h>
00048 #include <stdarg.h>
00049
00050 #include "gen_util.h"
00051 #include "isa_gen.h"
00052
00053 static const char * const interface[] = {
00054 "/* ====================================================================",
00055 " * ====================================================================",
00056 " *",
00057 " * Description:",
00058 " *",
00059 " * A description of the ISA (actually just an enum of all the opcodes).",
00060 " * The description exports the following:",
00061 " *",
00062 " * TOPCODE stands for Target OPCODE; prefix is TOP.",
00063 " *",
00064 " * typedef (enum) TOP",
00065 " * Contains all the target opcodes. Their names have the form",
00066 " * TOP_<name>.",
00067 " *",
00068 " * typedef mTOP",
00069 " * The smallest integer type that can contain all values of a TOP,",
00070 " * including TOP_UNDEFINED -- useful for conserving space in tables.",
00071 " *",
00072 " * const TOP TOP_UNDEFINED",
00073 " * Useful value guaranteed not to be a valid TOP.",
00074 " *",
00075 " * const int TOP_count",
00076 " * Gives the number of topcodes.",
00077 " *",
00078 " * const char* TOP_Name(TOP topcode)",
00079 " * Returns an assembler style name for the given TOP.",
00080 " *",
00081 " * ====================================================================",
00082 " * ====================================================================",
00083 " */",
00084 NULL
00085 };
00086
00087
00089 static char* Dot_To_Line(const char* str)
00091
00092
00094 {
00095 char *result = (char*) malloc(strlen(str)+1);
00096 const char *s;
00097 char *r;
00098
00099 for (s = str, r = result; *s != 0; ++s, ++r) {
00100 if (*s == '.')
00101 *r = '_';
00102 else
00103 *r = *s;
00104 }
00105
00106 *r = 0;
00107
00108 return result;
00109 }
00110
00111
00113 void ISA_Create (const char *isa_name, ...)
00115
00117 {
00118 FILE* hfile = fopen("topcode.h","w");
00119 FILE* cfile = fopen("topcode.c","w");
00120 FILE* efile = fopen("topcode.Exported","w");
00121 char *instruction_name;
00122 int instruction_count = 0;
00123 va_list ap;
00124
00125 fprintf(cfile,"#include \"topcode.h\"\n");
00126
00127 Emit_Header (hfile, "TOPCODE", interface);
00128
00129 fprintf(hfile,"typedef enum topcode {");
00130 fprintf(cfile,"static const char* const top_names[] = {");
00131
00132 bool is_first = true;
00133 va_start(ap,isa_name);
00134 while ((instruction_name = va_arg (ap, char *)) != NULL) {
00135 fprintf(hfile,"%s\n TOP_%s",is_first ? "" : ",",
00136 Dot_To_Line(instruction_name));
00137 fprintf(cfile,"%s\n \"%s\"",is_first ? "" : ",",
00138 instruction_name);
00139 if ( is_first )
00140 is_first = false;
00141
00142 instruction_count++;
00143 }
00144 va_end(ap);
00145
00146 fprintf(hfile,",\n TOP_UNDEFINED");
00147 fprintf(cfile,",\n \"UNDEFINED\"");
00148
00149 fprintf(hfile,"\n} TOP;\n");
00150 fprintf(cfile,"\n};\n");
00151
00152 fprintf(hfile,"\ntypedef %s mTOP;\n",
00153 (instruction_count+1) <= 256 ? "mUINT8" : "mUINT16");
00154
00155 fprintf(hfile,"\n#define TOP_count %d\n", instruction_count);
00156
00157 fprintf(hfile,"\nextern const char* TOP_Name(TOP topcode);\n");
00158 fprintf(efile,"TOP_Name\n");
00159 fprintf(cfile,"\nconst char* TOP_Name(TOP topcode)\n{\n"
00160 " return top_names[(int)topcode];\n"
00161 "}\n");
00162
00163 Emit_Footer (hfile);
00164 }