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 "proc_gen.h"
00056
00057 static const char * const interface[] = {
00058 "/* ====================================================================",
00059 " * ====================================================================",
00060 " *",
00061 " * Description:",
00062 " *",
00063 " * A description of the PROC (actually just an enum of all the processors).",
00064 " * The description exports the following:",
00065 " *",
00066 " * typedef (enum) PROCESSOR",
00067 " * Contains all the target processors. Their names have the form",
00068 " * PROCESSOR_<name>.",
00069 " *",
00070 " * const PROCESSOR PROCESSOR_UNDEFINED",
00071 " * Useful value guaranteed not to be a valid PROCESSOR.",
00072 " *",
00073 " * const int PROCESSOR_count",
00074 " * Gives the number of processors.",
00075 " *",
00076 " * PROCESSOR PROCESSOR_Value",
00077 " * The current processor.",
00078 " *",
00079 " * const char* PROCESSOR_Name(PROCESSOR topcode)",
00080 " * Returns a name for the given PROCESSOR.",
00081 " *",
00082 " * ====================================================================",
00083 " * ====================================================================",
00084 " */",
00085 NULL
00086 };
00087
00088
00090 static char* Dot_To_Line(const char* str)
00092
00093
00095 {
00096 char *result = (char*) malloc(strlen(str)+1);
00097 const char *s;
00098 char *r;
00099
00100 for (s = str, r = result; *s != 0; ++s, ++r) {
00101 if (*s == '.')
00102 *r = '_';
00103 else
00104 *r = *s;
00105 }
00106
00107 *r = 0;
00108
00109 return result;
00110 }
00111
00112
00114 void PROC_Create (const char *proc_name, ...)
00116
00118 {
00119 FILE* hfile = fopen("targ_proc.h","w");
00120 FILE* cfile = fopen("targ_proc.c","w");
00121 FILE* efile = fopen("targ_proc.Exported","w");
00122 char *instruction_name;
00123 int instruction_count = 0;
00124 va_list ap;
00125
00126 fprintf(cfile,"#include \"targ_proc.h\"\n");
00127
00128 Emit_Header (hfile, "targ_proc", interface);
00129
00130 fprintf(hfile,"typedef enum processor {\n");
00131 fprintf(cfile,"\nstatic const char* const processor_names[] = {\n");
00132
00133 va_start(ap,proc_name);
00134 while ((instruction_name = va_arg (ap, char *)) != NULL) {
00135 fprintf(hfile," PROCESSOR_%s,\n", Dot_To_Line(instruction_name));
00136 fprintf(cfile," \"%s\",\n", instruction_name);
00137
00138 instruction_count++;
00139 }
00140 va_end(ap);
00141
00142 fprintf(hfile," PROCESSOR_UNDEFINED\n"
00143 "} PROCESSOR;\n");
00144 fprintf(cfile," \"UNDEFINED\"\n"
00145 "};\n");
00146
00147 fprintf(hfile,"\n#define PROCESSOR_count %d\n", instruction_count);
00148 fprintf(hfile,"\nextern PROCESSOR PROCESSOR_Value;\n");
00149 fprintf(cfile,"\nPROCESSOR PROCESSOR_Value = PROCESSOR_UNDEFINED;\n");
00150 fprintf(hfile,"\nextern const char* PROCESSOR_Name(PROCESSOR proc);\n");
00151 fprintf(efile,"PROCESSOR_Name\n");
00152 fprintf(cfile,"\nconst char* PROCESSOR_Name(PROCESSOR proc)\n"
00153 "{\n"
00154 " return processor_names[(int)proc];\n"
00155 "}\n");
00156
00157 Emit_Footer (hfile);
00158 }