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 "gen_util.h"
00062 #include "targ_proc.h"
00063 #include "proc_properties_gen.h"
00064
00065
00066 struct proc_property {
00067 const char* name;
00068 int bit_position;
00069 std::vector<bool> members;
00070 };
00071
00072
00073
00074 enum {
00075 BIT_ALWAYS_TRUE = -1,
00076 BIT_ALWAYS_FALSE = -2
00077 };
00078
00079
00080 static int proc_property_count = 0;
00081 static std::list<PROC_PROPERTY> properties;
00082
00083 static const char * const interface[] = {
00084 "/* ====================================================================",
00085 " * ====================================================================",
00086 " *",
00087 " * Description:",
00088 " *",
00089 " * A description of the properties (attributes) for the processors",
00090 " * in the PROC. The description exports the following:",
00091 " *",
00092 " * BOOL PROC_xxx(void)",
00093 " * Return true/false if PROCESSOR_Value has/does-not-have the",
00094 " * property 'xxx'.",
00095 " *",
00096 " * ====================================================================",
00097 " * ====================================================================",
00098 " */",
00099 NULL
00100 };
00101
00102
00104 void PROC_Properties_Begin( const char* )
00106
00108 {
00109 }
00110
00112 PROC_PROPERTY PROC_Property_Create( const char* name )
00114
00116 {
00117 PROC_PROPERTY result = new proc_property;
00118
00119 proc_property_count++;
00120
00121 result->name = name;
00122 result->members = std::vector<bool> (PROCESSOR_count, false);
00123
00124 properties.push_back(result);
00125
00126 return result;
00127 }
00128
00130 void Processor_Group( PROC_PROPERTY property, ... )
00132
00134 {
00135 va_list ap;
00136 PROCESSOR opcode;
00137
00138 va_start(ap,property);
00139 while ( (opcode = static_cast<PROCESSOR>(va_arg(ap,int)))
00140 != PROCESSOR_UNDEFINED ) {
00141 property->members[(int)opcode] = true;
00142 }
00143 va_end(ap);
00144 }
00145
00147 void PROC_Properties_End(void)
00149
00151 {
00152 std::list<PROC_PROPERTY>::iterator isi;
00153 int bit_pos;
00154 char filename[1000];
00155 sprintf (filename, "targ_proc_properties.h");
00156 FILE* hfile = fopen(filename, "w");
00157 sprintf (filename, "targ_proc_properties.c");
00158 FILE* cfile = fopen(filename, "w");
00159 sprintf (filename, "targ_proc_properties.Exported");
00160 FILE* efile = fopen(filename, "w");
00161
00162 fprintf(cfile,"#include \"targ_proc_properties.h\"\n\n");
00163
00164 Emit_Header (hfile, "targ_proc_properties", interface);
00165 fprintf(hfile, "#include \"targ_proc.h\"\n");
00166
00167
00168
00169 bit_pos = 0;
00170 for ( isi = properties.begin(); isi != properties.end(); ++isi ) {
00171 int code;
00172 PROC_PROPERTY property = *isi;
00173 bool all_same = true;
00174 bool first_member = property->members[0];
00175 for (code = 1; code < PROCESSOR_count; code++) {
00176 if (property->members[code] != first_member) {
00177 all_same = false;
00178 break;
00179 }
00180 }
00181
00182 if (all_same) {
00183 property->bit_position = first_member ? BIT_ALWAYS_TRUE : BIT_ALWAYS_FALSE;
00184 } else {
00185 property->bit_position = bit_pos++;
00186 }
00187 }
00188
00189 char *int_type;
00190 char *int_suffix;
00191 int int_size;
00192 if (bit_pos <= 8) {
00193 int_type = "mUINT8";
00194 int_suffix = "";
00195 int_size = 8;
00196 } else if (bit_pos <= 16) {
00197 int_type = "mUINT16";
00198 int_suffix = "";
00199 int_size = 16;
00200 } else if (bit_pos <= 32) {
00201 int_type = "mUINT32";
00202 int_suffix = "U";
00203 int_size = 32;
00204 } else {
00205 assert (bit_pos <= 64);
00206 int_type = "mUINT64";
00207 int_suffix = "ULL";
00208 int_size = 64;
00209 }
00210 fprintf (hfile, "\nextern const %s PROC_PROPERTIES_flags[];\n\n", int_type);
00211 fprintf (efile, "PROC_PROPERTIES_flags\n");
00212 fprintf (cfile,"const %s PROC_PROPERTIES_flags[] = {\n", int_type);
00213
00214 for (int code = 0; code < PROCESSOR_count; code++) {
00215 unsigned long long flag_value = 0;
00216
00217 for ( isi = properties.begin(); isi != properties.end(); ++isi ) {
00218 PROC_PROPERTY property = *isi;
00219 if (property->members[code] && property->bit_position >= 0) {
00220 flag_value |= (1ULL << property->bit_position);
00221 }
00222 }
00223 fprintf (cfile, " 0x%0*" LL_FORMAT "x%s, /* %s:", int_size / 4,
00224 flag_value,
00225 int_suffix,
00226 PROCESSOR_Name((PROCESSOR)code));
00227 for ( isi = properties.begin(); isi != properties.end(); ++isi ) {
00228 PROC_PROPERTY property = *isi;
00229 if (property->members[code] && property->bit_position >= 0) {
00230 fprintf (cfile, " %s", property->name);
00231 }
00232 }
00233 fprintf (cfile, " */\n");
00234 }
00235 fprintf (cfile, " 0x%0*" LL_FORMAT "x%s /* UNDEFINED */\n"
00236 "};\n",
00237 int_size / 4,
00238 0ULL,
00239 int_suffix);
00240
00241 for ( isi = properties.begin(); isi != properties.end(); ++isi ) {
00242 PROC_PROPERTY property = *isi;
00243 if (property->bit_position >= 0) {
00244 fprintf (hfile, "#define PROP_%-16s 0x%" LL_FORMAT "x%s\n",
00245 property->name,
00246 (1ULL << property->bit_position),
00247 int_suffix);
00248 }
00249 }
00250
00251 fprintf (hfile, "\n");
00252 for ( isi = properties.begin(); isi != properties.end(); ++isi ) {
00253 PROC_PROPERTY property = *isi;
00254 if (property->bit_position >= 0) {
00255 fprintf (hfile,
00256 "#define PROC_%s() \\\n"
00257 " (PROC_PROPERTIES_flags[(INT)PROCESSOR_Value] & PROP_%s)\n",
00258 property->name,
00259 property->name);
00260 } else {
00261 fprintf (hfile,
00262 "#define PROC_%s() (%d)\n",
00263 property->name,
00264 property->bit_position == BIT_ALWAYS_TRUE ? 1 : 0);
00265 }
00266 }
00267
00268 Emit_Footer (hfile);
00269 }