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
00045
00047
00048
00049
00050
00051
00052
00053
00054 #include <stddef.h>
00055 #include <stdlib.h>
00056 #include <stdarg.h>
00057 #include <stdio.h>
00058 #include <assert.h>
00059 #include <list>
00060 #include <vector>
00061 #include "topcode.h"
00062 #include "gen_util.h"
00063 #include "isa_properties_gen.h"
00064
00065
00066 struct isa_property {
00067 const char* name;
00068 int bit_position;
00069 std::vector <bool> members;
00070 };
00071
00072
00073 enum {
00074 BIT_POS_ALL = -1,
00075 BIT_POS_NONE = -2
00076 };
00077
00078 static std::list<ISA_PROPERTY> properties;
00079
00080 static const char * const interface[] = {
00081 "/* ====================================================================",
00082 " * ====================================================================",
00083 " *",
00084 " * Description:",
00085 " *",
00086 " * A description of the properties (attributes) for the instructions",
00087 " * in the ISA. The description exports the following:",
00088 " *",
00089 " * BOOL TOP_is_xxx(TOP topcode)",
00090 " * Return true/false if 'topcode' has/does-not-have the property",
00091 " * 'xxx'.",
00092 " *",
00093 " * ====================================================================",
00094 " * ====================================================================",
00095 " */",
00096 NULL
00097 };
00098
00099
00101 void ISA_Properties_Begin( const char* )
00103
00105 {
00106 }
00107
00109 ISA_PROPERTY ISA_Property_Create( const char* name )
00111
00113 {
00114 ISA_PROPERTY result = new isa_property;
00115
00116 result->name = name;
00117 result->members = std::vector <bool> (TOP_count, false);
00118
00119 properties.push_back(result);
00120
00121 return result;
00122 }
00123
00125 void Instruction_Group( ISA_PROPERTY property, ... )
00127
00129 {
00130 va_list ap;
00131 TOP opcode;
00132
00133 va_start(ap,property);
00134 while ( (opcode = static_cast<TOP>(va_arg(ap,int))) != TOP_UNDEFINED ) {
00135 property->members[(int)opcode] = true;
00136 }
00137 va_end(ap);
00138 }
00139
00141 void ISA_Properties_End(void)
00143
00145 {
00146 std::list<ISA_PROPERTY>::iterator isi;
00147 int isa_property_count;
00148 int code;
00149
00150 #define FNAME "targ_isa_properties"
00151 char filename[1000];
00152 sprintf (filename, "%s.h", FNAME);
00153 FILE* hfile = fopen(filename, "w");
00154 sprintf (filename, "%s.c", FNAME);
00155 FILE* cfile = fopen(filename, "w");
00156 sprintf (filename, "%s.Exported", FNAME);
00157 FILE* efile = fopen(filename, "w");
00158
00159 fprintf(cfile,"#include \"%s.h\"\n\n", FNAME);
00160
00161 Emit_Header (hfile, "targ_isa_properties", interface);
00162
00163 isa_property_count = 0;
00164 for ( isi = properties.begin(); isi != properties.end(); ++isi ) {
00165 ISA_PROPERTY property = *isi;
00166 bool member;
00167 bool prev_member = property->members[0];
00168 for (code = 1; code < TOP_count; code++) {
00169 member = property->members[code];
00170 if (member != prev_member) break;
00171 }
00172 if (member != prev_member) {
00173 property->bit_position = isa_property_count++;
00174 } else {
00175 property->bit_position = member ? BIT_POS_ALL : BIT_POS_NONE;
00176 }
00177 }
00178
00179 char *int_type;
00180 char *int_suffix;
00181 int int_size;
00182 if (isa_property_count <= 8) {
00183 int_type = "mUINT8";
00184 int_suffix = "";
00185 int_size = 8;
00186 } else if (isa_property_count <= 16) {
00187 int_type = "mUINT16";
00188 int_suffix = "";
00189 int_size = 16;
00190 } else if (isa_property_count <= 32) {
00191 int_type = "mUINT32";
00192 int_suffix = "U";
00193 int_size = 32;
00194 } else {
00195 assert (isa_property_count <= 64);
00196 int_type = "mUINT64";
00197 int_suffix = "ULL";
00198 int_size = 64;
00199 }
00200 fprintf (hfile, "extern const %s ISA_PROPERTIES_flags[];\n\n", int_type);
00201 fprintf (efile, "ISA_PROPERTIES_flags\n");
00202 fprintf (cfile,"const %s ISA_PROPERTIES_flags[] = {\n", int_type);
00203
00204 for (code = 0; code < TOP_count; code++) {
00205 unsigned long long flag_value = 0;
00206
00207 for ( isi = properties.begin(); isi != properties.end(); ++isi ) {
00208 ISA_PROPERTY property = *isi;
00209 if (property->bit_position >= 0 && property->members[code]) {
00210 flag_value |= (1ULL << property->bit_position);
00211 }
00212 }
00213 fprintf (cfile, " 0x%0*" LL_FORMAT "x%s, /* %s:", int_size / 4,
00214 flag_value,
00215 int_suffix,
00216 TOP_Name((TOP)code));
00217 for ( isi = properties.begin(); isi != properties.end(); ++isi ) {
00218 ISA_PROPERTY property = *isi;
00219 if (property->members[code]) fprintf (cfile, " %s", property->name);
00220 }
00221 fprintf (cfile, " */\n");
00222 }
00223 fprintf (cfile, "};\n");
00224
00225 for ( isi = properties.begin(); isi != properties.end(); ++isi ) {
00226 ISA_PROPERTY property = *isi;
00227 int bit_position = property->bit_position;
00228 if (bit_position >= 0) {
00229 fprintf (hfile, "#define PROP_%-16s 0x%0" LL_FORMAT "x%s\n",
00230 property->name,
00231 (1ULL << bit_position),
00232 int_suffix);
00233 }
00234 }
00235
00236 fprintf (hfile, "\n\n");
00237 for ( isi = properties.begin(); isi != properties.end(); ++isi ) {
00238 ISA_PROPERTY property = *isi;
00239 int bit_position = property->bit_position;
00240 if (bit_position < 0) {
00241 fprintf (hfile, "#define TOP_is_%s(t)\t (%s)\n",
00242 property->name,
00243 bit_position == BIT_POS_ALL ? "TRUE" : "FALSE");
00244 } else {
00245 fprintf (hfile, "#define TOP_is_%s(t)\t (ISA_PROPERTIES_flags[(INT)t] & PROP_%s)\n",
00246 property->name,
00247 property->name);
00248 }
00249 }
00250
00251 Emit_Footer (hfile);
00252 }