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