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
00046
00047
00048
00049 #include <stdio.h>
00050 #include <stdlib.h>
00051 #include <string.h>
00052 #include <stdarg.h>
00053
00054 #include "gen_util.h"
00055 #include "isa_gen.h"
00056
00057 static const char * const interface[] = {
00058 "/* ====================================================================",
00059 " * ====================================================================",
00060 " *",
00061 " * Description:",
00062 " *",
00063 " * A description of the ISA (actually just an enum of all the opcodes).",
00064 " * The description exports the following:",
00065 " *",
00066 " * TOPCODE stands for Target OPCODE; prefix is TOP.",
00067 " *",
00068 " * typedef (enum) TOP",
00069 " * Contains all the target opcodes. Their names have the form",
00070 " * TOP_<name>.",
00071 " *",
00072 " * typedef mTOP",
00073 " * The smallest integer type that can contain all values of a TOP,",
00074 " * including TOP_UNDEFINED -- useful for conserving space in tables.",
00075 " *",
00076 " * const TOP TOP_UNDEFINED",
00077 " * Useful value guaranteed not to be a valid TOP.",
00078 " *",
00079 " * const int TOP_count",
00080 " * Gives the number of topcodes.",
00081 " *",
00082 " * const char* TOP_Name(TOP topcode)",
00083 " * Returns an assembler style name for the given TOP.",
00084 " *",
00085 " * ====================================================================",
00086 " * ====================================================================",
00087 " */",
00088 NULL
00089 };
00090
00091 #if defined(TARG_SL) || defined(TARG_MIPS)
00092 typedef struct map_tag{
00093 char *source;
00094 char *dest;
00095 }Map;
00096
00097 static char * Map_Op(Map *map, unsigned int size, char * str)
00098 {
00099 unsigned int i;
00100 for(i=0;i<size;++i)
00101 if(!strcmp(map[i].source,str))
00102 return map[i].dest;
00103 if(i>=size)
00104 return str;
00105 }
00106
00107 Map instr_map[]={
00108
00109 {"add.i", "addiu"},
00110 {"and.i", "andi"},
00111 {"br.eq", "beq"},
00112 {"br.gez", "bgez"},
00113 {"br.gtz", "bgtz"},
00114 {"br.lez", "blez"},
00115 {"br.ltz", "bltz"},
00116 {"br.ne", "bne"},
00117 {"extrb", "extrbs"},
00118 {"extrbu", "extrbu"},
00119 {"jp", "j"},
00120 {"jp.lnk", "jal"},
00121 {"jr.lnk", "jalr"},
00122 {"ldb", "lb"},
00123 {"ldub", "lbu"},
00124
00125
00126 {"ldh", "lh"},
00127 {"lduh", "lhu"},
00128 {"ld.lnk", "ll"},
00129 {"ldw", "lw"},
00130 {"movf.hi", "mfhi"},
00131 {"movf.lo", "mflo"},
00132 {"mvup.i", "lui"},
00133 {"or.i", "ori"},
00134 {"shll.i", "sll"},
00135 {"shll", "sllv"},
00136 {"setlt", "slt"},
00137 {"setlt.i", "slti"},
00138 {"setltu.i", "sltiu"},
00139 {"setltu", "sltu"},
00140 {"shra.i", "sra"},
00141 {"shra", "srav"},
00142 {"shrl.i", "srl"},
00143 {"shrl", "srlv"},
00144 {"stb", "sb"},
00145
00146 {"sth", "sh"},
00147 {"stw", "sw"},
00148
00149 {"xor.i", "xori"},
00150 };
00151
00152 char * NewInstr_To_OldInstr(char *str)
00153 {
00154
00155 return Map_Op(instr_map,sizeof(instr_map)/sizeof(Map),str);
00156 }
00157
00158 #endif
00159
00161 static char* Dot_To_Line(const char* str)
00163
00164
00166 {
00167 char *result = (char*) malloc(strlen(str)+1);
00168 const char *s;
00169 char *r;
00170
00171 for (s = str, r = result; *s != 0; ++s, ++r) {
00172 if (*s == '.')
00173 *r = '_';
00174 #if defined(TARG_NVISA)
00175 else if (*s == '<')
00176 *r = '_';
00177 else if (*s == '>') {
00178 if (*(s+1) == '<')
00179 --r;
00180 else
00181 *r = '_';
00182 }
00183 #endif
00184 else
00185 *r = *s;
00186 }
00187
00188 *r = 0;
00189
00190 return result;
00191 }
00192
00193
00195 void ISA_Create (const char *isa_name, ...)
00197
00199 {
00200 FILE* hfile = fopen("topcode.h","w");
00201 FILE* cfile = fopen("topcode.c","w");
00202 FILE* efile = fopen("topcode.Exported","w");
00203 char *instruction_name;
00204 int instruction_count = 0;
00205 va_list ap;
00206
00207 fprintf(cfile,"#include \"topcode.h\"\n");
00208
00209 Emit_Header (hfile, "TOPCODE", interface);
00210
00211 fprintf(hfile,"typedef enum topcode {");
00212 fprintf(cfile,"static const char* const top_names[] = {");
00213
00214 bool is_first = true;
00215 va_start(ap,isa_name);
00216 while ((instruction_name = va_arg (ap, char *)) != NULL) {
00217 fprintf(hfile,"%s\n TOP_%s",is_first ? "" : ",",
00218 #if defined(TARG_SL) || defined(TARG_MIPS)
00219 Dot_To_Line(NewInstr_To_OldInstr(instruction_name)));
00220 #else
00221 Dot_To_Line(instruction_name));
00222 #endif
00223 fprintf(cfile,"%s\n \"%s\"",is_first ? "" : ",",
00224 instruction_name);
00225 if ( is_first )
00226 is_first = false;
00227
00228 instruction_count++;
00229 }
00230 va_end(ap);
00231
00232 fprintf(hfile,",\n TOP_UNDEFINED");
00233 fprintf(cfile,",\n \"UNDEFINED\"");
00234
00235 fprintf(hfile,"\n} TOP;\n");
00236 fprintf(cfile,"\n};\n");
00237
00238 fprintf(hfile,"\ntypedef %s mTOP;\n",
00239 (instruction_count+1) <= 256 ? "mUINT8" : "mUINT16");
00240
00241 fprintf(hfile,"\n#define TOP_count %d\n", instruction_count);
00242
00243 fprintf(hfile,"\nextern const char* TOP_Name(TOP topcode);\n");
00244 fprintf(efile,"TOP_Name\n");
00245 fprintf(cfile,"\nconst char* TOP_Name(TOP topcode)\n{\n"
00246 " return top_names[(int)topcode];\n"
00247 "}\n");
00248
00249 Emit_Footer (hfile);
00250 }