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