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
00061
00062
00063
00064
00065
00066
00067
00068
00069 #include <elf.h>
00070 #include <stdio.h>
00071 #include <sys/types.h>
00072 #include <sys/stat.h>
00073 #include <search.h>
00074 #include <string.h>
00075 #include <stdlib.h>
00076 #include <unistd.h>
00077
00078 #include "wn.h"
00079 #include "stab.h"
00080 #include "aux_stab.h"
00081 #include "irbdata.h"
00082 #include "wintrinsic.h"
00083 #include "glob.h"
00084 #include "pu_info.h"
00085 #include "ir_bread.h"
00086 #include "ir_bwrite.h"
00087 #include "file_util.h"
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 #define MAX_FNAME_LENGTH 256
00098
00099 char filename_in[MAX_FNAME_LENGTH];
00100 char filename_out[MAX_FNAME_LENGTH];
00101 char temp_filename[MAX_FNAME_LENGTH+16];
00102
00103 char *Irb_File_Name_out = NULL;
00104
00105 struct stat statbuf;
00106
00107 BOOL oflag = FALSE;
00108 BOOL vflag = FALSE;
00109
00110 INT32 indent = 0;
00111 INT32 indent_inc = 2;
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135 #undef ST_WALK
00136 #undef TY_WALK
00137 #undef WN_WALK
00138
00139 #undef WRITE_IRB
00140
00141
00142
00143 void
00144 Initialization (void)
00145 {
00146 }
00147
00148
00149
00150 void
00151 Process_PU (PU_Info *pu)
00152 {
00153 }
00154
00155
00156
00157 void
00158 Process_STAB (SYMTAB *stab)
00159 {
00160 }
00161
00162
00163
00164 void
00165 Process_ST (ST *st)
00166 {
00167 }
00168
00169
00170
00171 void
00172 Process_TY (TY *st)
00173 {
00174 }
00175
00176
00177
00178 void
00179 Process_WN (WN *wn)
00180 {
00181 }
00182
00183
00184
00185 void
00186 Termination (void)
00187 {
00188 }
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 static void
00202 Usage (void)
00203 {
00204 fprintf (stderr,
00205 "USAGE:\n"
00206 "\n"
00207 "\t [-v] [-o <Whirl_File_Name_out>] <Whirl_File_Name>\n"
00208 "\n"
00209 "The <Whirl_File_Name> is a mandatory command line argument.\n"
00210 "The optional -v flag controls verbose trace messages.\n"
00211 "The optional -o flag is used to specify an alternate out file.\n"
00212 "\n"
00213 );
00214
00215 }
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230 static INT32
00231 Get_Irb_File_Name (INT argc, char *argv[])
00232 {
00233
00234 char *fname;
00235 INT32 argidx;
00236 INT32 src_count = 0;
00237
00238 for (argidx = 1; argidx < argc; argidx++)
00239 {
00240
00241 if (argv[argidx][0] == '-' )
00242 {
00243
00244 if (strcmp (argv[argidx], "-o") == 0) {
00245 #ifdef WRITE_IRB
00246 oflag = TRUE;
00247 fname = argv[++argidx];
00248 if (strlen(fname) > MAX_FNAME_LENGTH)
00249 {
00250 Irb_File_Name_out = strncpy (filename_out, fname, MAX_FNAME_LENGTH);
00251 filename_out[MAX_FNAME_LENGTH] = '\0';
00252 fprintf (stderr,
00253 "WARNING: output filename truncated to "
00254 "(max=%d chars): \"%s\"\n",
00255 MAX_FNAME_LENGTH, fname);
00256 }
00257 else
00258 Irb_File_Name_out = strcpy (filename_out, fname);
00259 #else
00260 fprintf (stderr,
00261 "WARNING: cannot specify output file");
00262 ++argidx;
00263 #endif
00264 } else if (strcmp (argv[argidx], "-v") == 0) {
00265 vflag = TRUE;
00266 } else {
00267 fprintf (stderr,
00268 "WARNING: unrecognized command option "
00269 "%s\n",
00270 argv[argidx]);
00271 }
00272 }
00273 else if (src_count > 0)
00274 src_count += 1;
00275 else if (src_count == 0)
00276 {
00277
00278 fname = argv[argidx];
00279 if (strlen(fname) > MAX_FNAME_LENGTH)
00280 {
00281 Irb_File_Name = strncpy (filename_in, fname, MAX_FNAME_LENGTH);
00282 filename_in[MAX_FNAME_LENGTH] = '\0';
00283 fprintf (stderr,
00284 "WARNING: input filename truncated to "
00285 "(max=%d chars): \"%s\"\n",
00286 MAX_FNAME_LENGTH, fname);
00287 }
00288 else
00289 Irb_File_Name = strcpy (filename_in, fname);
00290
00291 src_count = 1;
00292 }
00293 }
00294 return src_count;
00295 }
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 static void
00308 process_stab (SYMTAB *stab)
00309 {
00310
00311 ST *st;
00312 TY *ty;
00313
00314 if (vflag)
00315 printf ("%*sProcessing STAB: %d\n", indent, "", SYMTAB_id(stab));
00316 Process_STAB (stab);
00317
00318 #ifdef ST_WALK
00319
00320 indent += indent_inc;
00321
00322 for (st = SYMTAB_symbols(stab); st != NULL; st = ST_next(st)) {
00323 if (vflag)
00324 printf ("%*sProcessing ST: %s\n", indent, "", ST_name(st));
00325 Process_ST (st);
00326 }
00327
00328 indent -= indent_inc;
00329
00330 #endif
00331
00332 #ifdef TY_WALK
00333
00334 indent += indent_inc;
00335
00336 for (ty = SYMTAB_types(stab); ty != NULL; ty = TY_next(ty)) {
00337 if (vflag)
00338 printf ("%*sProcessing TY: %s\n", indent, "", TY_name(ty));
00339 Process_TY (ty);
00340 }
00341
00342 indent -= indent_inc;
00343
00344 #endif
00345
00346 }
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358 static void
00359 process_wn (WN *wn)
00360 {
00361
00362 INT32 i;
00363 WN *node;
00364 OPCODE op;
00365
00366 if (wn) {
00367
00368 op = WN_opcode(wn);
00369
00370 if (vflag)
00371 printf ("%*sProcessing WN: %s\n", indent, "", OPCODE_name(op));
00372 Process_WN (wn);
00373
00374 indent += indent_inc;
00375
00376 if (op == OPC_BLOCK)
00377 for (node = WN_first(wn); node; node = WN_next(node))
00378 process_wn (node);
00379 else
00380 for (i = 0; i < WN_kid_count(wn); i++)
00381 process_wn (WN_kid(wn,i));
00382
00383 indent -= indent_inc;
00384
00385 }
00386
00387 }
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399 static void
00400 process_func (WN *wn)
00401 {
00402
00403 if (vflag)
00404 printf ("%*sProcessing FUNC: %s\n", indent, "", ST_name(WN_st(wn)));
00405
00406 #ifdef WN_WALK
00407
00408 indent += indent_inc;
00409
00410 process_wn (wn);
00411
00412 indent -= indent_inc;
00413
00414 #endif
00415
00416 }
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428 static void
00429 process_pu (PU_Info *pu_tree)
00430 {
00431 PU_Info *pu;
00432
00433 for (pu = pu_tree; pu != NULL; pu = PU_Info_next(pu)) {
00434 Current_PU_Info = pu;
00435 MEM_POOL_Push (MEM_pu_nz_pool_ptr);
00436 Read_Local_Info (MEM_pu_nz_pool_ptr, pu);
00437
00438 if (vflag)
00439 printf ("%*sProcessing PU: %s\n", indent, "",
00440 ST_name(PU_Info_proc_sym(pu)));
00441 indent += indent_inc;
00442 Process_PU (pu);
00443
00444 process_stab (PU_Info_symtab_ptr(pu));
00445
00446 process_func (PU_Info_tree_ptr(pu));
00447
00448 if (PU_Info_child(pu)) {
00449 process_pu (PU_Info_child(pu));
00450 }
00451
00452 #ifdef WRITE_IRB
00453 Write_PU_Info (pu);
00454 #endif
00455
00456 indent -= indent_inc;
00457 Free_Local_Info (pu);
00458 MEM_POOL_Pop (MEM_pu_nz_pool_ptr);
00459
00460 }
00461 }
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473 void
00474 main (INT argc,
00475 char *argv[],
00476 char *envp[])
00477 {
00478 WN *pu;
00479 INT32 inp_file_count;
00480 PU_Info *pu_tree, *current_pu;
00481
00482
00483
00484
00485 MEM_Initialize ();
00486 Init_Error_Handler (10);
00487 Set_Error_Line (ERROR_LINE_UNKNOWN);
00488 Set_Error_Phase ("IR Walker");
00489 Set_Error_File (NULL);
00490
00491 Init_Operator_To_Opcode_Table ();
00492
00493
00494 inp_file_count = Get_Irb_File_Name (argc, argv);
00495 if (inp_file_count == 0)
00496 {
00497 Usage ();
00498 fprintf (stderr, "ERROR: missing input file on command line\n");
00499 }
00500 else if (inp_file_count > 1)
00501 {
00502 Usage ();
00503 fprintf (stderr, "ERROR: too many input files on command line\n");
00504 }
00505 else if (stat (Irb_File_Name, &statbuf) != 0)
00506 {
00507 fprintf (stderr, "ERROR: input file (%s) does not exist\n",
00508 Irb_File_Name);
00509 }
00510 else
00511 {
00512
00513 #ifdef WRITE_IRB
00514
00515 if (!oflag)
00516 (void) strcpy (filename_out, filename_in);
00517 (void) sprintf (temp_filename, "%s$%d", filename_out, (INT32) getpid ());
00518 (void) remove (temp_filename);
00519 #endif
00520
00521
00522 Initialization ();
00523
00524
00525
00526
00527 (void) Open_Input_Info (Irb_File_Name);
00528 pu_tree = Read_Global_Info (NULL);
00529
00530 #ifdef WRITE_IRB
00531 (void) Open_Output_Info (temp_filename);
00532 #endif
00533
00534
00535 process_stab (Global_Symtab);
00536
00537
00538 process_pu (pu_tree);
00539
00540
00541 Termination ();
00542
00543 #ifdef WRITE_IRB
00544
00545 Write_Global_Info (pu_tree);
00546 Close_Output_Info ();
00547 (void) remove (filename_out);
00548 (void) rename (temp_filename, filename_out);
00549 #endif
00550
00551 }
00552
00553 exit (0);
00554 }
00555
00556
00557
00558
00559
00560 void Signal_Cleanup (INT sig) { }
00561
00562 char * Host_Format_Parm (INT kind, MEM_PTR parm) { return NULL; }
00563
00564 INT8 Debug_Level = 0;