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
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #include <stdarg.h>
00056 #include <errno.h>
00057 #include <stdio.h>
00058 #include "common_include.h"
00059 #include "file_util.h"
00060
00061 static const UINT Diag_Phase_Name_Len = 80;
00062 static char Diag_Phase_Name[Diag_Phase_Name_Len+1] = "";
00063 static FILE *Diag_File = NULL;
00064 static INT Diag_Max_Diags = 10;
00065 static INT Diag_Warn_Count = 0;
00066 static const char *Diag_File_Location = NULL;
00067 static INT Diag_Line_Location = 0;
00068 static INT Diag_SrcLine_Location = 0;
00069 static INT Diag_SrcCol_Location = 0;
00070
00071 static const char *Diag_Msg[DIAG_LAST+1];
00072
00073
00074 void Diag_Init(void)
00075 {
00076 INT diag;
00077
00078
00079
00080 for (diag = DIAG_FIRST; diag <= DIAG_LAST; diag++)
00081 Diag_Msg[diag] = "*** Unknown diagnostics code ***";
00082
00083
00084
00085 Diag_Msg[DIAG_A_STRING] = "%s";
00086 Diag_Msg[DIAG_UNIMPLEMENTED] = "TODO: Unimplemented feature: %s";
00087 Diag_Msg[DIAG_UNKNOWN_CMD_LINE_OPTION] = "Unknown command-line option: %s";
00088 Diag_Msg[DIAG_CANNOT_OPEN_FILE] = "Cannot open file (%s), errno=%d";
00089 Diag_Msg[DIAG_CANNOT_CLOSE_FILE] = "Cannot close file (%s), errno=%d";
00090
00091 Diag_Msg[DIAG_W2F_CANNOT_HANDLE_OPC] =
00092 "cannot handle opcode %s (%d)";
00093 Diag_Msg[DIAG_W2F_UNEXPECTED_OPC] =
00094 "unexpected opcode in %s()";
00095 Diag_Msg[DIAG_W2F_UNEXPECTED_IOS] =
00096 "unexpected IO statement kind %s in %s()";
00097 Diag_Msg[DIAG_W2F_UNEXPECTED_IOU] =
00098 "unexpected IO unit kind %s in %s()";
00099 Diag_Msg[DIAG_W2F_UNEXPECTED_IOF] =
00100 "unexpected IO format kind %s in %s()";
00101 Diag_Msg[DIAG_W2F_UNEXPECTED_IOC] =
00102 "unexpected IO control kind %s in %s()";
00103 Diag_Msg[DIAG_W2F_UNEXPECTED_IOL] =
00104 "unexpected IO list kind %s in %s()";
00105 Diag_Msg[DIAG_W2F_UNEXPECTED_INITV] =
00106 "unexpected INITV kind %d in %s()";
00107 Diag_Msg[DIAG_W2F_UNEXPECTED_DOLOOP_BOUNDOP] =
00108 "unexpected opcode (%s) for DO loop bound in %s()";
00109 Diag_Msg[DIAG_W2F_UNEXPECTED_IMPLIED_DOLOOP] =
00110 "unexpected form of implied do-loop in %s(); Cannot calculate bounds";
00111 Diag_Msg[DIAG_W2F_UNEXPECTED_RETURNSITE] =
00112 "RETURNSITE out of sequence in %s()";
00113 Diag_Msg[DIAG_W2F_UNEXPECTED_CALLSITE] =
00114 "CALLSITE out of sequence in %s()";
00115 Diag_Msg[DIAG_W2F_UNEXPECTED_SUBSTRING_REF] =
00116 "Unexpected (sub)string reference in %s()";
00117 Diag_Msg[DIAG_W2F_UNEXPEXTED_RETURNREG_USE] =
00118 "Unexpected usage of return-registers detected in %s()";
00119 Diag_Msg[DIAG_W2F_UNEXPEXTED_OFFSET] =
00120 "Unexpected offset (%d) for memory location in %s()";
00121 Diag_Msg[DIAG_W2F_UNEXPEXTED_NULL_PTR] =
00122 "Unexpected NULL value for %s in %s()";
00123 Diag_Msg[DIAG_W2F_NONEXISTENT_FLD_PATH] =
00124 "Non-existent path to an FLD of the given object type in %s()";
00125 Diag_Msg[DIAG_W2F_CANNOT_LDA_PREG] =
00126 "Cannot take the address of a pseudo-register";
00127 Diag_Msg[DIAG_W2F_CANNOT_DEREF] =
00128 "Cannot dereference pointer variable in %s(); No pointee specification";
00129 Diag_Msg[DIAG_W2F_UNEXPECTED_NUM_KIDS] =
00130 "Unexpected number of kids (%d), expected %d kids for %s()";
00131 Diag_Msg[DIAG_W2F_UNEXPECTED_CVT] =
00132 "unexpected conversion from %s to %s in %s()";
00133 Diag_Msg[DIAG_W2F_UNEXPECTED_CONTEXT] =
00134 "unexpected context of translation for %s()";
00135
00136 Diag_Msg[DIAG_W2F_UNEXPECTED_TYPE_KIND] =
00137 "unexpected TY_kind (%d) in %s()";
00138 Diag_Msg[DIAG_W2F_UNEXPECTED_TYPE_SIZE] =
00139 "unexpected TY_size (%d) in %s()";
00140 Diag_Msg[DIAG_W2F_UNEXPECTED_BTYPE] =
00141 "unexpected TY_btype (%s) in %s()";
00142 Diag_Msg[DIAG_W2F_EXPECTED_PTR_TO_CHARACTER] =
00143 "expected pointer to character operands in %s()";
00144 Diag_Msg[DIAG_W2F_EXPECTED_PTR] =
00145 "expected pointer TY in %s()";
00146 Diag_Msg[DIAG_W2F_UNEXPECTED_SYMBOL] =
00147 "unexpected form of symbol in %s()";
00148 Diag_Msg[DIAG_W2F_UNEXPECTED_SYMCLASS] =
00149 "unexpected ST_symclass (%d) in %s()";
00150 Diag_Msg[DIAG_W2F_UNEXPECTED_STORECLASS] =
00151 "unexpected ST_sclass (%d) in %s()";
00152 Diag_Msg[DIAG_W2F_UNEXPECTED_SYM_CONST] =
00153 "unexpected symbolic constant in %s()";
00154 Diag_Msg[DIAG_W2F_UNEXPECTED_PRAGMA] =
00155 "unexpected pragma kind in %s()";
00156 Diag_Msg[DIAG_W2F_MISPLACED_PRAGMA] =
00157 "pragma %s will be misplaced in output, and will be emitted as comment";
00158 Diag_Msg[DIAG_W2F_EXPECTED_IDNAME] =
00159 "expected OPC_IDNAME in %s()";
00160 Diag_Msg[DIAG_W2F_INCOMPATIBLE_TYS] =
00161 "incompatible types in %s()";
00162 Diag_Msg[DIAG_W2F_DECLARE_RETURN_PARAM] =
00163 "should not declare return parameter: %s()";
00164 Diag_Msg[DIAG_W2F_BUFFER_ERROR] =
00165 "Error in buffer access: %s";
00166
00167 Diag_Msg[DIAG_W2C_CANNOT_HANDLE_OPC] = Diag_Msg[DIAG_W2F_CANNOT_HANDLE_OPC];
00168 Diag_Msg[DIAG_W2C_UNEXPECTED_OPC] = Diag_Msg[DIAG_W2F_UNEXPECTED_OPC];
00169 Diag_Msg[DIAG_W2C_EXPECTED_IDNAME] = Diag_Msg[DIAG_W2F_EXPECTED_IDNAME];
00170
00171 Diag_Msg[DIAG_PRP_OPTION_ILLEGAL] =
00172 "Illegal %s option: %s\n";
00173 Diag_Msg[DIAG_PRP_OPTION_MISSING_SRC] =
00174 "Missing source file for PURPLE processing";
00175 Diag_Msg[DIAG_PRP_NAME_OVERFLOW] =
00176 "The input overflows a name-buffer: %s\n";
00177 Diag_Msg[DIAG_PRP_FILE_ERROR] = "Error in accessing file: %s\n";
00178 Diag_Msg[DIAG_PRP_FILE_CANNOT_OPEN] = "Cannot open file: %s\n";
00179 Diag_Msg[DIAG_PRP_FILE_CANNOT_CLOSE] = "Cannot close file: %s\n";
00180 Diag_Msg[DIAG_PRP_FILE_OVERWRITTEN] = "Overwrites existing file: %s\n";
00181 Diag_Msg[DIAG_PRP_FILE_AT_END] = "Attempt to go beyond end of file: %s\n";
00182 Diag_Msg[DIAG_PRP_FILE_NOT_OPEN] = "Attemp at accessing unopened file\n";
00183 Diag_Msg[DIAG_PRP_FILE_NOT_CLOSED] = "Forgot to close file: %s\n";
00184 Diag_Msg[DIAG_PRP_FILE_HEADER_ERROR] = "Error in header of file: %s\n";
00185 Diag_Msg[DIAG_PRP_ITEM_INPUT_ERROR] =
00186 "Unexpected item found in purple input-file (%s):\n"
00187 "-- ignoring remaining input";
00188 Diag_Msg[DIAG_PRP_EXPECTED_PURPLE_REGION] =
00189 "Missing PURPLE region definition";
00190 Diag_Msg[DIAG_PRP_PREREQUISITE_CALL] =
00191 "Need to call %s(), before %s()";
00192 Diag_Msg[DIAG_PRP_EXPECTED_CONSTRAINT] =
00193 "Missing PURPLE region constraint %s";
00194 Diag_Msg[DIAG_PRP_UNEXPECTED_OPCODE] =
00195 "unexpected opcode (%d) in PURPLE routine %s()";
00196 Diag_Msg[DIAG_PRP_UNEXPECTED_TYPE] =
00197 "unexpected type (%s) in PURPLE routine %s()";
00198 Diag_Msg[DIAG_PRP_INVALID_LEXEME_KIND] =
00199 "invalid lexeme-kind (%d) in PURPLE routine %s()";
00200 Diag_Msg[DIAG_PRP_INVALID_TRACE_ITEM_KIND] =
00201 "invalid trace item kind (%d) in PURPLE routine %s()";
00202 Diag_Msg[DIAG_PRP_CANNOT_ASSIGN_OBJECT] =
00203 "illegal use of %s; assignment not supported";
00204 Diag_Msg[DIAG_PRP_ILLEGAL_SET] =
00205 "illegal set in %s()";
00206 Diag_Msg[DIAG_PRP_RVALUE_ERROR] =
00207 "illegal access to rvalue attribute";
00208 Diag_Msg[DIAG_PRP_PLACEHOLDER_ERROR] =
00209 "Encountered unexpected purple placeholder format in %s";
00210 Diag_Msg[DIAG_PRP_PREG_VALUE] =
00211 "Error in preg value mapping; %s";
00212 Diag_Msg[DIAG_PRP_MEM_VALUE] =
00213 "Error in memory value mapping; %s";
00214 Diag_Msg[DIAG_PRP_VALUE_SPACE] =
00215 "Error in memory value space; %s";
00216 Diag_Msg[DIAG_PRP_VARIABLE_DEF] =
00217 "Error in variable definition; %s";
00218 Diag_Msg[DIAG_PRP_DEFINED_MEM_SPACE] =
00219 "Error in defined memory space; %s";
00220 Diag_Msg[DIAG_PRP_PU_MMAP] =
00221 "Error in PU memory mapping; %s";
00222 Diag_Msg[DIAG_PRP_FILE_MMAP] =
00223 "Error in file memory mapping; %s";
00224 Diag_Msg[DIAG_PRP_PROGRAM_MMAP] =
00225 "Error in program memory mapping; %s";
00226 Diag_Msg[DIAG_PRP_ICALL] =
00227 "Cannot handle indirect call; %s";
00228 Diag_Msg[DIAG_PRP_REF_PARAM] =
00229 "Purple cannot trace reference parameter (%d) value; %s";
00230 Diag_Msg[DIAG_PRP_CANNOT_HANDLE_CONSTRUCT] =
00231 "Purple cannot handle construct: %s";
00232 Diag_Msg[DIAG_PRP_EDIT_OUTPUT] =
00233 "You need to edit the output file: %s";
00234
00235 Diag_Warn_Count = 0;
00236 }
00237
00238
00239 void Diag_Exit(void)
00240 {
00241
00242 if (Diag_File != NULL)
00243 {
00244 fclose (Diag_File);
00245 Diag_File = NULL;
00246 }
00247 }
00248
00249
00250 void Diag_Set_Phase(const char *phase_name)
00251 {
00252 Set_Error_Phase(phase_name);
00253 (void)strncpy(Diag_Phase_Name, phase_name, Diag_Phase_Name_Len);
00254 Diag_Phase_Name[Diag_Phase_Name_Len] = '\0';
00255 }
00256
00257
00258 void Diag_Set_File(const char *filename)
00259 {
00260
00261 Set_Error_File(filename);
00262
00263
00264 if (Diag_File != NULL)
00265 {
00266 fclose(Diag_File);
00267 Diag_File = NULL;
00268 }
00269
00270 if (filename != NULL)
00271 {
00272
00273 if (Is_File(filename))
00274 unlink(filename);
00275
00276 Diag_File = fopen(filename, "a");
00277 if (Diag_File == NULL)
00278 fprintf(stderr, "Cannot open error-file: \"%s\"\n", filename);
00279 else if (Same_File(Diag_File, stderr))
00280 {
00281 fclose(Diag_File);
00282 Diag_File = NULL;
00283 fprintf(stderr, "Cannot open stderr as alternate error file\n");
00284 }
00285 }
00286 else
00287 fprintf(stderr,
00288 "Attempt to open name-less file as error file is ignored\n");
00289 }
00290
00291
00292 void Diag_Set_Max_Diags(INT max_allowed_diags)
00293 {
00294 Diag_Max_Diags = max_allowed_diags;
00295 }
00296
00297
00298 INT Diag_Get_Warn_Count(void)
00299 {
00300 return Diag_Warn_Count;
00301 }
00302
00303
00304 void Diag_Set_Location(const char *file_name, INT line_number)
00305 {
00306 Diag_File_Location = file_name;
00307 Diag_Line_Location = line_number;
00308 }
00309
00310
00311 void Diag_Set_Srcpos(SRCPOS srcpos)
00312 {
00313 USRCPOS usrcpos;
00314 USRCPOS_srcpos(usrcpos) = srcpos;
00315
00316 Diag_SrcLine_Location = USRCPOS_linenum(usrcpos);
00317 Diag_SrcCol_Location = USRCPOS_column(usrcpos);
00318 }
00319
00320
00321 void Diag_Warning(DIAG_CODE code, ...)
00322 {
00323 char diag_char[512];
00324 va_list arg_ptr;
00325
00326 if (Diag_Max_Diags > Diag_Warn_Count)
00327 {
00328 if (Diag_File_Location != NULL)
00329 (void)sprintf(&diag_char[0],
00330 "%s(%s:%d): WARNING %d: %s\n",
00331 Diag_Phase_Name,
00332 Diag_File_Location, Diag_Line_Location,
00333 code,
00334 Diag_Msg[code]);
00335 else
00336 (void)sprintf(&diag_char[0],
00337 "%s: WARNING %d: %s\n",
00338 Diag_Phase_Name, code, Diag_Msg[code]);
00339
00340 va_start(arg_ptr, code);
00341 vfprintf(stderr, &diag_char[0], arg_ptr);
00342 va_end(arg_ptr);
00343
00344 Diag_Warn_Count++;
00345 }
00346 }
00347
00348
00349 void Diag_Fatal(DIAG_CODE code, ...)
00350 {
00351 char diag_char[512];
00352 va_list arg_ptr;
00353
00354 if (Diag_File_Location != NULL)
00355 (void)sprintf(&diag_char[0],
00356 "%s(%s:%d): FATAL ERROR: %s\n",
00357 Diag_Phase_Name,
00358 Diag_File_Location, Diag_Line_Location,
00359 Diag_Msg[code]);
00360 else
00361 (void)sprintf(&diag_char[0],
00362 "%s: FATAL ERROR: %s\n",
00363 Diag_Phase_Name, Diag_Msg[code]);
00364
00365 va_start(arg_ptr, code);
00366 vfprintf(stderr, &diag_char[0], arg_ptr);
00367 va_end(arg_ptr);
00368
00369 exit(1);
00370 }
00371
00372
00373 void Diag_User_Warning(DIAG_CODE code, ...)
00374 {
00375 char diag_char[512];
00376 va_list arg_ptr;
00377
00378 (void)sprintf(&diag_char[0],
00379 "WARNING %d: line %d, column %d: %s\n",
00380 code,
00381 Diag_SrcLine_Location,
00382 Diag_SrcCol_Location,
00383 Diag_Msg[code]);
00384
00385 va_start(arg_ptr, code);
00386 vfprintf(stderr, &diag_char[0], arg_ptr);
00387 va_end(arg_ptr);
00388 }
00389
00390
00391 void Diag_User_Fatal(DIAG_CODE code, ...)
00392 {
00393 char diag_char[512];
00394 va_list arg_ptr;
00395
00396 (void)sprintf(&diag_char[0],
00397 "FATAL ERROR: line %d, column %d: %s\n",
00398 Diag_SrcLine_Location,
00399 Diag_SrcCol_Location,
00400 Diag_Msg[code]);
00401
00402 va_start(arg_ptr, code);
00403 vfprintf(stderr, &diag_char[0], arg_ptr);
00404 va_end(arg_ptr);
00405
00406 exit(1);
00407 }
00408
00409
00410 void Diag_Warning_Srcpos(DIAG_CODE code, ...)
00411 {
00412 char diag_char[512];
00413 va_list arg_ptr;
00414
00415 if (Diag_Max_Diags > Diag_Warn_Count)
00416 {
00417 if (Diag_File_Location != NULL)
00418 (void)sprintf(&diag_char[0],
00419 "%s(%s:%d): WARNING %d: line %d: %s\n",
00420 Diag_Phase_Name,
00421 Diag_File_Location, Diag_Line_Location,
00422 code,
00423 Diag_SrcLine_Location,
00424 Diag_Msg[code]);
00425 else
00426 (void)sprintf(&diag_char[0],
00427 "%s: WARNING %d: line %d: %s\n",
00428 Diag_Phase_Name,
00429 code,
00430 Diag_SrcLine_Location,
00431 Diag_Msg[code]);
00432
00433 va_start(arg_ptr, code);
00434 vfprintf(stderr, &diag_char[0], arg_ptr);
00435 va_end(arg_ptr);
00436
00437 Diag_Warn_Count++;
00438 }
00439 }
00440
00441
00442 void Diag_Fatal_Srcpos(DIAG_CODE code, ...)
00443 {
00444 char diag_char[512];
00445 va_list arg_ptr;
00446
00447 if (Diag_File_Location != NULL)
00448 (void)sprintf(&diag_char[0],
00449 "%s(%s:%d): FATAL ERROR: line %d: %s\n",
00450 Diag_Phase_Name,
00451 Diag_File_Location, Diag_Line_Location,
00452 Diag_SrcLine_Location,
00453 Diag_Msg[code]);
00454 else
00455 (void)sprintf(&diag_char[0],
00456 "%s: FATAL ERROR: line %d: %s\n",
00457 Diag_Phase_Name, Diag_SrcLine_Location, Diag_Msg[code]);
00458
00459 va_start(arg_ptr, code);
00460 vfprintf(stderr, &diag_char[0], arg_ptr);
00461 va_end(arg_ptr);
00462
00463 exit(1);
00464 }