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
00037
00038
00039
00040
00042
00043
00044
00046
00047
00048
00049
00050
00051
00052
00053 #include <stddef.h>
00054 #include <stdlib.h>
00055 #include <stdarg.h>
00056 #include <stdio.h>
00057 #include <assert.h>
00058 #include <list>
00059 #include <vector>
00060 #include "topcode.h"
00061 #include "gen_util.h"
00062 #include "isa_subset_gen.h"
00063
00064
00065 struct isa_subset {
00066 const char* name;
00067 int index;
00068 ISA_SUBSET superset;
00069 std::vector<unsigned char> members;
00070
00071 };
00072
00073 static int isa_subset_count = 0;
00074 static std::list<ISA_SUBSET> subsets;
00075 static size_t bit_vector_sizeof;
00076
00077 static std::vector<ISA_SUBSET> opcode_subset;
00078
00079
00080
00081 static const char * const interface[] = {
00082 "/* ====================================================================",
00083 " * ====================================================================",
00084 " *",
00085 " * Description:",
00086 " *",
00087 " * A description of the ISA subset hierarchy. The description",
00088 " * exports the following:",
00089 " *",
00090 " * typedef (enum) ISA_SUBSET",
00091 " * An enumberated type of the different subsets.",
00092 " *",
00093 " * const ISA_SUBSET ISA_SUBSET_UNDEFINED",
00094 " * Useful value guaranteed not to be a valid ISA_SUBSET.",
00095 " *",
00096 " * extern ISA_SUBSET ISA_SUBSET_Value",
00097 " * A variable containing the current subset value.",
00098 " *",
00099 " * const char* ISA_SUBSET_Name( ISA_SUBSET subset )",
00100 " * Returns a name suitable for printing.",
00101 " *",
00102 " * int ISA_SUBSET_Member( ISA_SUBSET subset, TOP opcode )",
00103 " * Is the given <opcode> a member of the given <subset>?",
00104 " *",
00105 " * ====================================================================",
00106 " * ====================================================================",
00107 " */",
00108 NULL
00109 };
00110
00111
00113 void ISA_Subset_Begin( const char* )
00115
00117 {
00118 bit_vector_sizeof = (TOP_count + 7) / 8;
00119 opcode_subset = std::vector<ISA_SUBSET>(TOP_count,(ISA_SUBSET)0);
00120 for ( int code = 0; code < TOP_count; ++code )
00121 opcode_subset[code] = NULL;
00122 }
00123
00125 ISA_SUBSET ISA_Subset_Create( ISA_SUBSET parent, const char* name )
00127
00129 {
00130 ISA_SUBSET result = new isa_subset;
00131
00132 result->name = name;
00133 result->index = isa_subset_count++;
00134 result->superset = parent;
00135 result->members = std::vector<unsigned char>(bit_vector_sizeof,0);
00136
00137 subsets.push_front(result);
00138
00139 return result;
00140 }
00141
00142 void ISA_Subset_Create_Only_One( const char* name )
00144
00146 {
00147 ISA_SUBSET result = new isa_subset;
00148
00149 result->name = name;
00150 result->index = isa_subset_count++;
00151 result->superset = NULL;
00152 result->members = std::vector<unsigned char>(bit_vector_sizeof,0);
00153
00154 subsets.push_front(result);
00155
00156
00157 int opcode;
00158 for ( opcode = 0; opcode < TOP_count; ++opcode ) {
00159 int byte_index = ((unsigned int) opcode) / 8;
00160 int bit_index = ((unsigned int) opcode) % 8;
00161 result->members[byte_index] |= (1 << bit_index);
00162 opcode_subset[opcode] = result;
00163 }
00164 }
00165
00167 void Instruction_Group( ISA_SUBSET subset, ... )
00169
00171 {
00172 va_list ap;
00173 TOP opcode;
00174
00175 va_start(ap,subset);
00176 while ( (opcode = static_cast<TOP>(va_arg(ap,int))) != TOP_UNDEFINED ) {
00177 ISA_SUBSET ss;
00178 int byte_index = ((unsigned int) opcode) / 8;
00179 int bit_index = ((unsigned int) opcode) % 8;
00180
00181 for ( ss = subset; ss != NULL; ss = ss->superset )
00182 ss->members[byte_index] |= (1 << bit_index);
00183
00184 if ( opcode_subset[opcode] != NULL ) {
00185 fprintf(stderr,"### attempting to add %s to ISA subset %s but "
00186 "already in %s\n",
00187 TOP_Name(opcode),
00188 subset->name,
00189 opcode_subset[opcode]->name);
00190 exit(EXIT_FAILURE);
00191 }
00192 opcode_subset[opcode] = subset;
00193 }
00194 va_end(ap);
00195 }
00196
00198 void ISA_Subset_End(void)
00200
00202 {
00203 std::list<ISA_SUBSET>::iterator isi;
00204 bool err;
00205 int code;
00206
00207 for ( err = false, code = 0; code < TOP_count; ++code ) {
00208 if ( ! opcode_subset[code] ) {
00209 fprintf(stderr,"### Error: no opcode subset for %s\n",
00210 TOP_Name((TOP)code));
00211 err = true;
00212 }
00213 }
00214 if (err) exit(EXIT_FAILURE);
00215
00216 #define FNAME "targ_isa_subset"
00217 char filename[1000];
00218 sprintf(filename,"%s.h", FNAME);
00219 FILE* hfile = fopen(filename,"w");
00220 sprintf(filename,"%s.c", FNAME);
00221 FILE* cfile = fopen(filename,"w");
00222 sprintf(filename,"%s.Exported", FNAME);
00223 FILE* efile = fopen(filename,"w");
00224
00225 fprintf(cfile,"#include \"topcode.h\"\n");
00226 fprintf(cfile,"#include \"%s.h\"\n", FNAME);
00227
00228 sprintf (filename, "%s", FNAME);
00229 Emit_Header (hfile, filename, interface);
00230 fprintf(hfile,"#include \"topcode.h\"\n");
00231
00232 fprintf(hfile,"\ntypedef enum {\n");
00233 fprintf(cfile,"\nstatic const char* const isa_subset_names[] = {\n");
00234
00235 for ( isi = subsets.begin(); isi != subsets.end(); ++isi ) {
00236 ISA_SUBSET subset = *isi;
00237 fprintf(hfile," ISA_SUBSET_%s,\n", subset->name);
00238 fprintf(cfile," \"%s\",", subset->name);
00239 }
00240 fprintf(hfile," ISA_SUBSET_UNDEFINED,\n"
00241 " ISA_SUBSET_MIN=ISA_SUBSET_%s,\n"
00242 " ISA_SUBSET_MAX=ISA_SUBSET_%s\n"
00243 "} ISA_SUBSET;\n",
00244 (*subsets.begin())->name,
00245 (*subsets.rbegin())->name);
00246 fprintf(cfile," \"UNDEFINED\"\n"
00247 "};\n");
00248
00249 fprintf(hfile,"extern ISA_SUBSET ISA_SUBSET_Value;\n\n");
00250 fprintf(efile,"ISA_SUBSET_Value\n");
00251 fprintf(cfile,"ISA_SUBSET ISA_SUBSET_Value = ISA_SUBSET_UNDEFINED;\n\n");
00252
00253 fprintf(hfile,"extern const char* ISA_SUBSET_Name( ISA_SUBSET subset );\n");
00254 fprintf(efile,"ISA_SUBSET_Name\n");
00255 fprintf(cfile,"const char* ISA_SUBSET_Name( ISA_SUBSET subset ) {\n");
00256 fprintf(cfile," return isa_subset_names[(INT)subset];\n");
00257 fprintf(cfile,"}\n");
00258
00259 fprintf(cfile,"static const char isa_subset_opcode_table[%d][%d] = {\n",
00260 isa_subset_count+1,bit_vector_sizeof);
00261
00262 for ( isi = subsets.begin(); isi != subsets.end(); ++isi ) {
00263 ISA_SUBSET subset = *isi;
00264
00265 fprintf(cfile," { /* %s */\n", subset->name);
00266 for ( int i = 0; i < bit_vector_sizeof; ++i ) {
00267 int members = subset->members[i];
00268 fprintf(cfile," 0%03o, /* ",members);
00269 for (int j = 0; j < 8; ++j) {
00270 if (members & (1 << j)) {
00271 TOP top = (TOP)((i * 8) + j);
00272 fprintf(cfile,"%s ",TOP_Name(top));
00273 }
00274 }
00275 fprintf(cfile,"*/\n");
00276 }
00277 fprintf(cfile," },\n");
00278 }
00279 fprintf(cfile," { /* UNDEFINED */\n"
00280 " 0\n"
00281 " }\n");
00282 fprintf(cfile,"};\n");
00283
00284 fprintf(hfile,"extern INT ISA_SUBSET_Member( ISA_SUBSET subset,\n"
00285 " TOP opcode );\n");
00286 fprintf(efile,"ISA_SUBSET_Member\n");
00287 fprintf(cfile,
00288 "int ISA_SUBSET_Member( ISA_SUBSET subset, TOP opcode )\n"
00289 "{\n"
00290 " INT byte_index = ((UINT) opcode) / 8;\n"
00291 " INT bit_index = ((UINT) opcode) %% 8;\n"
00292 " INT byte = isa_subset_opcode_table[(int) subset][byte_index];\n"
00293 " return (byte >> bit_index) & 1;\n"
00294 "}\n");
00295
00296 Emit_Footer (hfile);
00297 }