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 #ifdef _KEEP_RCS_ID
00063
00064 static char *rcs_id = "$Source: /depot/CVSROOT/javi/src/sw/cmplr/be/whirl2f/w2f_driver.cxx,v $ $Revision: 1.1 $";
00065 #endif
00066
00067 #include <sys/elf_whirl.h>
00068 #include <time.h>
00069 #include <errno.h>
00070 #include "whirl2f_common.h"
00071 #include "config_flist.h"
00072 #include "config_list.h"
00073 #include "w2cf_parentize.h"
00074 #include "file_util.h"
00075 #include "flags.h"
00076 #include "timing.h"
00077 #include "wn_lower.h"
00078
00079 #include "const.h"
00080 #include "PUinfo.h"
00081 #include "st2f.h"
00082 #include "wn2f.h"
00083 #include "wn2f_stmt.h"
00084
00085
00086
00087 #undef int
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 static const char *W2F_File_Extension[W2F_NUM_FILES] =
00099 {
00100 ".f",
00101 ".w2f.f",
00102 ".w2f.loc"
00103 };
00104
00105
00106 static const char *W2F_Cite_Extension[W2F_NUM_FILES] =
00107 {
00108 ".c",
00109 "-after-lno.f",
00110 ".loc"
00111 };
00112
00113
00114 static const char *W2F_Prompf_Extension[W2F_NUM_FILES] =
00115 {
00116 ".c",
00117 ".m",
00118 ".anl_srcpos"
00119 };
00120
00121
00122 #define W2F_Extension(i) \
00123 (W2F_Prompf_Emission? W2F_Prompf_Extension[i] : \
00124 (List_Cite ? W2F_Cite_Extension[i] : W2F_File_Extension[i]))
00125
00126
00127
00128
00129 #define W2F_MAX_SKIP_ITEMS 128
00130 static W2CF_SKIP_ITEM Skip[W2F_MAX_SKIP_ITEMS+1];
00131 static INT Next_Skip_Item = 0;
00132
00133
00134
00135 static BOOL W2F_Initialized = FALSE;
00136 static BOOL W2F_Outfile_Initialized = FALSE;
00137 static FORMAT_KIND W2F_Format_Kind = F77_TAB_FORMAT;
00138 static WN2F_CONTEXT Global_Context = INIT_WN2F_CONTEXT;
00139 static const char *W2F_Progname = "";
00140 static const char *W2F_File_Name[W2F_NUM_FILES] = {NULL, NULL, NULL};
00141 static BOOL File_Is_Created[W2F_NUM_FILES] = {FALSE, FALSE, FALSE};
00142 static MEM_POOL W2F_Parent_Pool;
00143
00144
00145 FILE *W2F_File[W2F_NUM_FILES] = {NULL, NULL, NULL};
00146 BOOL W2F_Enabled = TRUE;
00147 BOOL W2F_Verbose = TRUE;
00148 BOOL W2F_Old_F77 = FALSE;
00149 BOOL W2F_Ansi_Format = FALSE;
00150 BOOL W2F_No_Pragmas = FALSE;
00151 BOOL W2F_Emit_Prefetch = FALSE;
00152 BOOL W2F_Emit_All_Regions = FALSE;
00153 BOOL W2F_Emit_Linedirs = FALSE;
00154 BOOL W2F_Emit_Nested_PUs = FALSE;
00155 BOOL W2F_Emit_Frequency = FALSE;
00156 BOOL W2F_Emit_Cgtag = FALSE;
00157 BOOL W2F_Emit_Pcf = FALSE;
00158 BOOL W2F_Emit_Omp = FALSE;
00159 INT32 W2F_Line_Length = 0;
00160
00161
00162 BOOL W2F_Only_Mark_Loads = FALSE;
00163 BOOL WN2F_F90_pu = FALSE;
00164 BOOL W2F_Purple_Emission = FALSE;
00165 BOOL W2F_Prompf_Emission = FALSE;
00166 WN_MAP *W2F_Construct_Map = NULL;
00167 WN_MAP W2F_Frequency_Map = WN_MAP_UNDEFINED;
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 static void
00211 Process_Filename_Options(const char *src_filename, const char *irb_filename)
00212 {
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 #define MAX_FNAME_LENGTH 256-7
00227 static char filename[MAX_FNAME_LENGTH+7];
00228 char *fname;
00229
00230
00231 if (W2F_File_Name[W2F_ORIG_FILE] == NULL)
00232 {
00233 if (src_filename != NULL && src_filename[0] != '\0')
00234 W2F_File_Name[W2F_ORIG_FILE] = src_filename;
00235 else if (irb_filename != NULL && irb_filename[0] != '\0')
00236 W2F_File_Name[W2F_ORIG_FILE] = irb_filename;
00237 else
00238 W2F_File_Name[W2F_ORIG_FILE] = "anonymous.f";
00239 }
00240
00241
00242 if (strlen(W2F_File_Name[W2F_ORIG_FILE]) > MAX_FNAME_LENGTH)
00243 {
00244 W2F_File_Name[W2F_ORIG_FILE] =
00245 strncpy(filename, W2F_File_Name[W2F_ORIG_FILE], MAX_FNAME_LENGTH);
00246 filename[MAX_FNAME_LENGTH] = '\0';
00247 fprintf(stderr,
00248 "WARNING: src_file name truncated to (max=%d chars): \"%s\"\n",
00249 MAX_FNAME_LENGTH, W2F_File_Name[W2F_ORIG_FILE]);
00250 }
00251 else
00252 W2F_File_Name[W2F_ORIG_FILE] =
00253 strcpy(filename, W2F_File_Name[W2F_ORIG_FILE]);
00254
00255
00256
00257
00258
00259 fname = Last_Pathname_Component(filename);
00260 if (W2F_File_Name[W2F_FTN_FILE] == NULL)
00261 {
00262 W2F_File_Name[W2F_FTN_FILE] =
00263 New_Extension(fname, W2F_Extension(W2F_FTN_FILE));
00264 }
00265 if (W2F_File_Name[W2F_LOC_FILE] == NULL)
00266 {
00267 if (List_Cite || W2F_Prompf_Emission)
00268 {
00269 W2F_File_Name[W2F_LOC_FILE] =
00270 New_Extension(fname, W2F_Extension(W2F_LOC_FILE));
00271 }
00272 }
00273 }
00274
00275
00276 static FILE *
00277 Open_Read_File(const char *filename)
00278 {
00279 FILE *f = NULL;
00280
00281
00282 if (filename == NULL ||
00283 (f = fopen(filename, "r")) == NULL)
00284 {
00285 ErrMsg(EC_IR_Open, filename, errno);
00286 }
00287 return f;
00288 }
00289
00290
00291 static FILE *
00292 Open_Append_File(const char *filename)
00293 {
00294 FILE *f = NULL;
00295
00296
00297 if (filename == NULL ||
00298 (f = fopen(filename, "a")) == NULL)
00299 {
00300 ErrMsg(EC_IR_Open, filename, errno);
00301 }
00302 return f;
00303 }
00304
00305
00306 static FILE *
00307 Open_Create_File(const char *filename)
00308 {
00309 FILE *f = NULL;
00310
00311
00312 if (filename == NULL ||
00313 (f = fopen(filename, "w")) == NULL)
00314 {
00315 ErrMsg(EC_IR_Open, filename, errno);
00316 }
00317 return f;
00318 }
00319
00320
00321 static void
00322 Close_File(const char *filename, FILE *afile)
00323 {
00324 if (afile != NULL &&
00325 !Same_File(afile, stdout) &&
00326 !Same_File(afile, stderr) &&
00327 fclose(afile) != 0)
00328 {
00329 Set_Error_Line(ERROR_LINE_UNKNOWN);
00330 ErrMsg(EC_Src_Close, filename, errno);
00331 }
00332 }
00333
00334
00335 static void
00336 Open_W2f_Output_File(W2F_FILE_KIND kind)
00337 {
00338 if (W2F_File[kind] == NULL)
00339 {
00340 if (File_Is_Created[kind])
00341 {
00342 W2F_File[kind] = Open_Append_File(W2F_File_Name[kind]);
00343 }
00344 else
00345 {
00346 W2F_File[kind] = Open_Create_File(W2F_File_Name[kind]);
00347 File_Is_Created[kind] = TRUE;
00348 }
00349 }
00350 }
00351
00352
00353 static void
00354 Close_W2f_Output_File(W2F_FILE_KIND kind)
00355 {
00356 Close_File(W2F_File_Name[kind], W2F_File[kind]);
00357 W2F_File[kind] = NULL;
00358 }
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375 static void
00376 Begin_New_Locations_File(void)
00377 {
00378
00379
00380
00381
00382
00383 if (W2F_File_Name[W2F_LOC_FILE] != NULL)
00384 {
00385
00386
00387
00388 if (W2F_Prompf_Emission)
00389 {
00390 Open_W2f_Output_File(W2F_LOC_FILE);
00391 Write_String(W2F_File[W2F_LOC_FILE], NULL,
00392 "SRCPOS_MAP_BEGIN\n");
00393 }
00394 else
00395 {
00396 Open_W2f_Output_File(W2F_LOC_FILE);
00397 Write_String(W2F_File[W2F_LOC_FILE], NULL,
00398 "(SRCPOS-MAP\n");
00399 }
00400 }
00401 }
00402
00403
00404 static void
00405 End_Locations_File(void)
00406 {
00407
00408
00409
00410 if (W2F_File_Name[W2F_LOC_FILE] != NULL)
00411 {
00412
00413
00414
00415 if (W2F_Prompf_Emission)
00416 {
00417 Open_W2f_Output_File(W2F_LOC_FILE);
00418 Write_String(W2F_File[W2F_LOC_FILE],
00419 NULL,
00420 "SRCPOS_MAP_END\n");
00421 }
00422 else
00423 {
00424 Open_W2f_Output_File(W2F_LOC_FILE);
00425 Write_String(W2F_File[W2F_LOC_FILE], NULL, ")\n");
00426 }
00427 Terminate_Token_Buffer(W2F_File[W2F_LOC_FILE]);
00428 Close_W2f_Output_File(W2F_LOC_FILE);
00429 }
00430 }
00431
00432
00433 static void
00434 Continue_Locations_File(void)
00435 {
00436
00437
00438
00439 if (W2F_File_Name[W2F_LOC_FILE] != NULL)
00440 {
00441 Open_W2f_Output_File(W2F_LOC_FILE);
00442 }
00443 }
00444
00445
00446 static void
00447 Move_Locations_To_Anl_File(const char *loc_fname)
00448 {
00449 #define MAX_ANL_FNAME_LENGTH 256-5
00450 char cbuf[MAX_ANL_FNAME_LENGTH+1];
00451 INT i, next_ch;
00452 FILE *anl_file;
00453 FILE *loc_file;
00454 char *anl_fname;
00455 static char fname[MAX_ANL_FNAME_LENGTH+5];
00456
00457 strncpy(fname, loc_fname, MAX_ANL_FNAME_LENGTH);
00458 anl_fname = Last_Pathname_Component(fname);
00459 anl_fname = New_Extension(anl_fname, ".anl");
00460 anl_file = Open_Append_File(anl_fname);
00461 loc_file = Open_Read_File(loc_fname);
00462
00463 next_ch = getc(loc_file);
00464 while (next_ch != EOF)
00465 {
00466 for (i = 0; (next_ch != EOF && i < MAX_ANL_FNAME_LENGTH); i++)
00467 {
00468 cbuf[i] = next_ch;
00469 next_ch = getc(loc_file);
00470 }
00471 if (i > 0)
00472 {
00473 cbuf[i] = '\0';
00474 fputs(cbuf, anl_file);
00475 }
00476 }
00477 Close_File(anl_fname, anl_file);
00478 Close_File(loc_fname, loc_file);
00479 remove(loc_fname);
00480 }
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495 static void
00496 W2F_Undo_Whirl_Side_Effects(void)
00497 {
00498 Stab_Free_Tmpvars();
00499 Stab_Free_Namebufs();
00500 }
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517 struct enter_fld
00518 {
00519 void operator() (UINT32 ty_idx, const TY* typ) const
00520 {
00521
00522 TY & ty = (TY &) typ;
00523
00524 if (TY_kind(ty) == KIND_STRUCT)
00525 {
00526 (void)W2CF_Symtab_Nameof_Ty(ty_idx);
00527
00528 FLD_HANDLE fld = TY_flist(ty);
00529 FLD_ITER fld_iter = Make_fld_iter(fld);
00530 do
00531 {
00532 FLD_HANDLE fld_rt (fld_iter);
00533
00534 if (TY_Is_Pointer(FLD_type(fld_rt)))
00535 (void)W2CF_Symtab_Nameof_Fld_Pointee(fld);
00536 (void)W2CF_Symtab_Nameof_Fld(fld);
00537
00538 } while (!FLD_last_field (fld_iter++));
00539 }
00540 }
00541 } ;
00542
00543
00544
00545
00546 struct enter_st
00547 {
00548 void operator() (UINT32 idx, const ST * st) const
00549 {
00550 if ((ST_sym_class(st) == CLASS_VAR && !ST_is_not_used(st)) ||
00551 ST_sym_class(st) == CLASS_FUNC)
00552 {
00553 TY_IDX ty ;
00554
00555 (void)W2CF_Symtab_Nameof_St(st);
00556
00557 if (ST_sym_class(st) == CLASS_VAR)
00558 ty = ST_type(st);
00559 else
00560 ty = ST_pu_type(st);
00561
00562 if (TY_Is_Pointer(ty))
00563 (void)W2CF_Symtab_Nameof_St_Pointee(st);
00564 }
00565 }
00566 };
00567
00568
00569 static void
00570 W2F_Enter_Global_Symbols(void)
00571 {
00572
00573
00574
00575
00576
00577
00578
00579
00580 For_all(Ty_Table,enter_fld());
00581
00582
00583
00584
00585
00586
00587
00588 For_all(St_Table,GLOBAL_SYMTAB,enter_st());
00589
00590 #if 0
00591
00592 FOR_ALL_CONSTANTS(st, const_idx)
00593 {
00594 if (ST_symclass(st) != CLASS_SYM_CONST)
00595 (void)W2CF_Symtab_Nameof_St(st);
00596 }
00597 #endif
00598
00599
00600 }
00601
00602
00603
00604
00605
00606
00607 static BOOL
00608 Check_Outfile_Initialized(const char *caller_name)
00609 {
00610 if (!W2F_Outfile_Initialized)
00611 fprintf(stderr,
00612 "NOTE: Ignored call to %s(); call W2F_Outfile_Init() first!\n",
00613 caller_name);
00614 return W2F_Outfile_Initialized;
00615 }
00616
00617 static BOOL
00618 Check_Initialized(const char *caller_name)
00619 {
00620 if (!W2F_Initialized)
00621 fprintf(stderr,
00622 "NOTE: Ignored call to %s(); call W2F_Init() first!\n",
00623 caller_name);
00624 return W2F_Initialized;
00625 }
00626
00627 static BOOL
00628 Check_PU_Pushed(const char *caller_name)
00629 {
00630 if (PUinfo_current_func == NULL)
00631 fprintf(stderr,
00632 "NOTE: Ignored call to %s(); call W2F_Push_PU() first!\n",
00633 caller_name);
00634 return (PUinfo_current_func != NULL);
00635 }
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647 BOOL
00648 W2F_Should_Emit_Nested_PUs(void)
00649 {
00650 return W2F_Emit_Nested_PUs;
00651 }
00652
00653
00654 void
00655 W2F_Process_Command_Line (INT phase_argc, const char *phase_argv[],
00656 INT argc, const char *argv[])
00657 {
00658
00659
00660 if (argv[0] != NULL)
00661 W2F_Progname = argv[0];
00662
00663
00664
00665
00666
00667 W2F_File_Name[W2F_ORIG_FILE] = FLIST_orig_filename;
00668 W2F_File_Name[W2F_FTN_FILE] = FLIST_ftn_filename;
00669 W2F_File_Name[W2F_LOC_FILE] = FLIST_loc_filename;
00670 W2F_Enabled = FLIST_enabled;
00671 W2F_Verbose = FLIST_verbose;
00672 W2F_Old_F77 = FLIST_old_f77;
00673 W2F_Ansi_Format = FLIST_ansi_format;
00674 W2F_No_Pragmas = FLIST_no_pragmas;
00675 W2F_Emit_Prefetch = FLIST_emit_prefetch;
00676 W2F_Emit_Linedirs = FLIST_emit_linedirs;
00677 W2F_Emit_All_Regions = FLIST_emit_all_regions;
00678 W2F_Emit_Nested_PUs = FLIST_emit_nested_pus;
00679 W2F_Emit_Frequency = FLIST_emit_frequency;
00680 W2F_Emit_Cgtag = FLIST_emit_cgtag;
00681 W2F_Emit_Pcf = FLIST_emit_pcf;
00682 W2F_Emit_Omp = FLIST_emit_omp;
00683 W2F_Line_Length = FLIST_line_length;
00684
00685 Process_Filename_Options(Src_File_Name, Irb_File_Name);
00686
00687 if (W2F_Ansi_Format)
00688 W2F_Format_Kind = F77_ANSI_FORMAT;
00689 else
00690 W2F_Format_Kind = F77_TAB_FORMAT;
00691
00692 }
00693
00694
00695 void
00696 W2F_Init(void)
00697 {
00698 const char * const caller_err_phase = Get_Error_Phase ();
00699
00700
00701
00702
00703 if (W2F_Initialized)
00704 return;
00705
00706 Diag_Init();
00707 if (W2F_Progname != NULL)
00708 Diag_Set_Phase(W2F_Progname);
00709 else
00710 Diag_Set_Phase("FLIST");
00711 Diag_Set_Max_Diags(100);
00712
00713
00714
00715 MEM_POOL_Initialize(&W2F_Parent_Pool, "W2f_Parent_Pool", FALSE);
00716 MEM_POOL_Push(&W2F_Parent_Pool);
00717
00718
00719
00720
00721 Initialize_Token_Buffer(W2F_Format_Kind, W2F_Prompf_Emission);
00722 if (W2F_Line_Length > 0)
00723 Set_Maximum_Linelength(W2F_Line_Length);
00724
00725
00726
00727
00728
00729
00730
00731
00732 Stab_initialize_flags();
00733
00734 W2CF_Symtab_Push();
00735 W2F_Enter_Global_Symbols();
00736
00737
00738
00739 reset_WN2F_CONTEXT(Global_Context);
00740 ST2F_initialize();
00741 PUinfo_initialize();
00742 WN2F_initialize();
00743
00744 W2F_Initialized = TRUE;
00745 Diag_Set_Phase(caller_err_phase);
00746 }
00747
00748
00749 void
00750 W2F_Push_PU(WN *pu, WN *body_part_of_interest)
00751 {
00752 if (!Check_Initialized("W2F_Push_PU"))
00753 return;
00754
00755 Is_True(WN_opcode(pu) == OPC_FUNC_ENTRY,
00756 ("Invalid opcode for W2F_Push_PU()"));
00757
00758 Stab_initialize();
00759 Clear_w2fc_flags() ;
00760
00761
00762
00763 MEM_POOL_Push(&W2F_Parent_Pool);
00764 W2CF_Parent_Map = WN_MAP_Create(&W2F_Parent_Pool);
00765 W2CF_Parentize(pu);
00766
00767
00768
00769
00770
00771 if (WN_opc_operator(body_part_of_interest) == OPR_BLOCK)
00772 {
00773 Remove_Skips(body_part_of_interest,
00774 Skip,
00775 &Next_Skip_Item,
00776 W2F_MAX_SKIP_ITEMS,
00777 FALSE );
00778 }
00779
00780
00781
00782 PUinfo_init_pu(pu, body_part_of_interest);
00783 }
00784
00785
00786 void
00787 W2F_Pop_PU(void)
00788 {
00789 if (!Check_Initialized("W2F_Pop_PU") ||
00790 !Check_PU_Pushed("W2F_Pop_PU"))
00791 return;
00792
00793 PUinfo_exit_pu();
00794
00795
00796
00797 if (Next_Skip_Item > 0)
00798 {
00799 Restore_Skips(Skip, Next_Skip_Item, FALSE );
00800 Next_Skip_Item = 0;
00801 }
00802
00803 Stab_finalize();
00804
00805 WN_MAP_Delete(W2CF_Parent_Map);
00806 W2CF_Parent_Map = WN_MAP_UNDEFINED;
00807 MEM_POOL_Pop(&W2F_Parent_Pool);
00808
00809 W2F_Frequency_Map = WN_MAP_UNDEFINED;
00810 }
00811
00812
00813 void
00814 W2F_Mark_Loads(void)
00815 {
00816 W2F_Only_Mark_Loads = TRUE;
00817 }
00818
00819
00820 void
00821 W2F_Nomark_Loads(void)
00822 {
00823 W2F_Only_Mark_Loads = FALSE;
00824 }
00825
00826
00827 void
00828 W2F_Set_Prompf_Emission(WN_MAP *construct_map)
00829 {
00830 W2F_Prompf_Emission = TRUE;
00831 W2F_Construct_Map = construct_map;
00832 }
00833
00834
00835 void
00836 W2F_Set_Frequency_Map(WN_MAP frequency_map)
00837 {
00838 W2F_Frequency_Map = frequency_map;
00839 }
00840
00841
00842 const char *
00843 W2F_Get_Transformed_Src_Path(void)
00844 {
00845 return W2F_File_Name[W2F_FTN_FILE];
00846 }
00847
00848
00849 void
00850 W2F_Set_Purple_Emission(void)
00851 {
00852 W2F_Purple_Emission = TRUE;
00853 }
00854
00855
00856 void
00857 W2F_Reset_Purple_Emission(void)
00858 {
00859 W2F_Purple_Emission = FALSE;
00860 }
00861
00862
00863 void
00864 W2F_def_ST(FILE *outfile, ST *st)
00865 {
00866 TOKEN_BUFFER tokens;
00867
00868 if (!Check_Initialized("W2F_def_ST"))
00869 return;
00870
00871 tokens = New_Token_Buffer();
00872 ST2F_decl_translate(tokens, st);
00873 Write_And_Reclaim_Tokens(outfile, W2F_File[W2F_LOC_FILE], &tokens);
00874 W2F_Undo_Whirl_Side_Effects();
00875 }
00876
00877
00878 const char *
00879 W2F_Object_Name(ST *func_st)
00880 {
00881 return W2CF_Symtab_Nameof_St(func_st);
00882 }
00883
00884
00885 void
00886 W2F_Translate_Stid_Lhs(char *strbuf,
00887 UINT bufsize,
00888 ST *stid_st,
00889 STAB_OFFSET stid_ofst,
00890 TY_IDX stid_ty,
00891 TYPE_ID stid_mtype)
00892 {
00893 TOKEN_BUFFER tokens;
00894 WN2F_CONTEXT context = INIT_WN2F_CONTEXT;
00895
00896 tokens = New_Token_Buffer();
00897 if (ST_class(stid_st) == CLASS_PREG)
00898 {
00899 ST2F_Use_Preg(tokens, ST_type(stid_st), stid_ofst);
00900 }
00901 else
00902 {
00903 WN2F_Offset_Symref(tokens,
00904 stid_st,
00905 Stab_Pointer_To(ST_type(stid_st)),
00906 stid_ty,
00907 stid_ofst,
00908 context);
00909 }
00910 Str_Write_And_Reclaim_Tokens(strbuf, bufsize, &tokens);
00911 W2F_Undo_Whirl_Side_Effects();
00912 }
00913
00914
00915 void
00916 W2F_Translate_Istore_Lhs(char *strbuf,
00917 UINT bufsize,
00918 WN *lhs,
00919 STAB_OFFSET istore_ofst,
00920 TY_IDX istore_addr_ty,
00921 TYPE_ID istore_mtype)
00922 {
00923 TOKEN_BUFFER tokens;
00924 TY_IDX base_ty;
00925 WN2F_CONTEXT context = INIT_WN2F_CONTEXT;
00926
00927
00928 base_ty = WN_Tree_Type(lhs);
00929 if (!TY_Is_Pointer(base_ty))
00930 base_ty = istore_addr_ty;
00931
00932
00933 tokens = New_Token_Buffer();
00934 WN2F_Offset_Memref(tokens,
00935 lhs,
00936 base_ty,
00937 TY_pointed(istore_addr_ty),
00938 istore_ofst,
00939 context);
00940 Str_Write_And_Reclaim_Tokens(strbuf, bufsize, &tokens);
00941 W2F_Undo_Whirl_Side_Effects();
00942 }
00943
00944
00945 void
00946 W2F_Translate_Wn(FILE *outfile, WN *wn)
00947 {
00948 TOKEN_BUFFER tokens;
00949 WN2F_CONTEXT context = INIT_WN2F_CONTEXT;
00950 const char * const caller_err_phase = Get_Error_Phase ();
00951
00952 if (!Check_Initialized("W2F_Translate_Wn") ||
00953 !Check_PU_Pushed("W2F_Translate_Wn"))
00954 return;
00955
00956 Start_Timer(T_W2F_CU);
00957 if (W2F_Progname != NULL)
00958 Diag_Set_Phase(W2F_Progname);
00959 else
00960 Diag_Set_Phase("FLIST");
00961
00962 tokens = New_Token_Buffer();
00963 (void)WN2F_translate(tokens, wn, context);
00964 Write_And_Reclaim_Tokens(outfile, W2F_File[W2F_LOC_FILE], &tokens);
00965 W2F_Undo_Whirl_Side_Effects();
00966
00967 Stop_Timer (T_W2F_CU);
00968 Diag_Set_Phase(caller_err_phase);
00969 }
00970
00971
00972 void
00973 W2F_Translate_Wn_Str(char *strbuf, UINT bufsize, WN *wn)
00974 {
00975 TOKEN_BUFFER tokens;
00976 WN2F_CONTEXT context = INIT_WN2F_CONTEXT;
00977 const char * const caller_err_phase = Get_Error_Phase ();
00978
00979 if (!Check_Initialized("W2F_Translate_Wn_Str") ||
00980 !Check_PU_Pushed("W2F_Translate_Wn_Str"))
00981 return;
00982
00983 Start_Timer (T_W2F_CU);
00984 if (W2F_Progname != NULL)
00985 Diag_Set_Phase(W2F_Progname);
00986 else
00987 Diag_Set_Phase("FLIST");
00988
00989 tokens = New_Token_Buffer();
00990 (void)WN2F_translate(tokens, wn, context);
00991 Str_Write_And_Reclaim_Tokens(strbuf, bufsize, &tokens);
00992 W2F_Undo_Whirl_Side_Effects();
00993
00994 Stop_Timer (T_W2F_CU);
00995 Diag_Set_Phase(caller_err_phase);
00996 }
00997
00998
00999 void
01000 W2F_Translate_Purple_Main(FILE *outfile, WN *pu, const char *region_name)
01001 {
01002 TOKEN_BUFFER tokens;
01003 WN2F_CONTEXT context = INIT_WN2F_CONTEXT;
01004 const char * const caller_err_phase = Get_Error_Phase ();
01005
01006 if (!Check_Initialized("W2F_Translate_Purple_Main"))
01007 return;
01008
01009 Is_True(WN_opcode(pu) == OPC_FUNC_ENTRY,
01010 ("Invalid opcode for W2F_Translate_Purple_Main()"));
01011
01012 Start_Timer (T_W2F_CU);
01013 Set_Error_Phase ("WHIRL To F");
01014
01015
01016
01017 tokens = New_Token_Buffer();
01018 W2F_Push_PU(pu, WN_func_body(pu));
01019 (void)WN2F_translate_purple_main(tokens, pu, region_name, context);
01020 W2F_Pop_PU();
01021 W2F_Undo_Whirl_Side_Effects();
01022 Write_And_Reclaim_Tokens(outfile, W2F_File[W2F_LOC_FILE], &tokens);
01023
01024 Stop_Timer (T_W2F_CU);
01025 Set_Error_Phase (caller_err_phase);
01026 }
01027
01028
01029 void
01030 W2F_Fini(void)
01031 {
01032
01033
01034
01035
01036 INT i;
01037
01038 if (!Check_Initialized("W2F_Fini"))
01039 return;
01040 else if (!W2F_Outfile_Initialized)
01041 {
01042
01043 ST2F_finalize();
01044 PUinfo_finalize();
01045 WN2F_finalize();
01046 W2CF_Symtab_Terminate();
01047 Stab_finalize_flags();
01048
01049 if (W2F_File_Name[W2F_LOC_FILE] != NULL)
01050 End_Locations_File();
01051 else
01052 Terminate_Token_Buffer(NULL);
01053 Diag_Exit();
01054
01055
01056
01057 W2F_Initialized = FALSE;
01058 W2F_Format_Kind = F77_TAB_FORMAT;
01059 reset_WN2F_CONTEXT(Global_Context);
01060 W2F_Progname = "";
01061 for (i=0;i<W2F_NUM_FILES;i++) W2F_File_Name[i] = NULL;
01062 for (i=0;i<W2F_NUM_FILES;i++) File_Is_Created[i] = FALSE;
01063 for (i=0;i<W2F_NUM_FILES;i++) W2F_File[i] = NULL;
01064 W2F_Enabled = TRUE;
01065 W2F_Verbose = TRUE;
01066 W2F_Old_F77 = FALSE;
01067 W2F_Ansi_Format = FALSE;
01068 W2F_No_Pragmas = FALSE;
01069 W2F_Emit_Prefetch = FALSE;
01070 W2F_Emit_All_Regions = FALSE;
01071 W2F_Emit_Linedirs = FALSE;
01072 W2F_Emit_Nested_PUs = FALSE;
01073 W2F_Emit_Frequency = FALSE;
01074 W2F_Line_Length = 0;
01075
01076 W2F_Only_Mark_Loads = FALSE;
01077
01078 MEM_POOL_Pop(&W2F_Parent_Pool);
01079 MEM_POOL_Delete(&W2F_Parent_Pool);
01080 }
01081 }
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108
01109
01110
01111 void
01112 W2F_Outfile_Init(void)
01113 {
01114
01115
01116
01117
01118
01119
01120
01121
01122 time_t systime;
01123
01124 if (W2F_Outfile_Initialized)
01125 return;
01126
01127 W2F_Outfile_Initialized = TRUE;
01128 if (W2F_Verbose)
01129 {
01130 if (W2F_Prompf_Emission || W2F_File_Name[W2F_LOC_FILE] == NULL)
01131 fprintf(stderr,
01132 "%s translates %s into %s, based on source %s\n",
01133 W2F_Progname,
01134 Irb_File_Name,
01135 W2F_File_Name[W2F_FTN_FILE],
01136 W2F_File_Name[W2F_ORIG_FILE]);
01137 else
01138 fprintf(stderr,
01139 "%s translates %s into %s and %s, based on source %s\n",
01140 W2F_Progname,
01141 Irb_File_Name,
01142 W2F_File_Name[W2F_FTN_FILE],
01143 W2F_File_Name[W2F_LOC_FILE],
01144 W2F_File_Name[W2F_ORIG_FILE]);
01145 }
01146
01147
01148
01149 if (!W2F_Initialized)
01150 W2F_Init();
01151
01152
01153
01154 Begin_New_Locations_File();
01155 Open_W2f_Output_File(W2F_FTN_FILE);
01156
01157
01158
01159
01160 systime = time(NULL);
01161 Write_String(W2F_File[W2F_FTN_FILE], W2F_File[W2F_LOC_FILE],
01162 "C ********************************************"
01163 "***************\n"
01164 "C Fortran file translated from WHIRL ");
01165 Write_String(W2F_File[W2F_FTN_FILE], W2F_File[W2F_LOC_FILE],
01166 ((systime != (time_t)-1)?
01167 ctime(&systime) : "at unknown time\n"));
01168 Write_String(W2F_File[W2F_FTN_FILE], W2F_File[W2F_LOC_FILE],
01169 "C **********************************************"
01170 "*************\n");
01171
01172 if (W2F_Old_F77)
01173 {
01174 Write_String(W2F_File[W2F_FTN_FILE], W2F_File[W2F_LOC_FILE],
01175 "C Include builtin operators "
01176 "(TODO: add missing ones into this included file)\n"
01177 "#include <whirl2f.h>\n\n");
01178 }
01179
01180 W2F_Outfile_Initialized = TRUE;
01181
01182 }
01183
01184
01185 void
01186 W2F_Outfile_Translate_Pu(WN *pu)
01187 {
01188 TOKEN_BUFFER tokens;
01189 LOWER_ACTIONS lower_actions = LOWER_NULL;
01190 const BOOL pu_is_pushed = (PUinfo_current_func == pu);
01191 const char * const caller_err_phase = Get_Error_Phase ();
01192
01193 if (!Check_Outfile_Initialized("W2F_Outfile_Translate_Pu"))
01194 return;
01195
01196 Is_True(WN_opcode(pu) == OPC_FUNC_ENTRY,
01197 ("Invalid opcode for W2F_Outfile_Translate_Pu()"));
01198
01199
01200
01201 Continue_Locations_File();
01202 Open_W2f_Output_File(W2F_FTN_FILE);
01203
01204 if (W2F_Emit_Nested_PUs)
01205 lower_actions = LOWER_MP;
01206
01207 if (lower_actions != LOWER_NULL)
01208 pu = WN_Lower(pu, lower_actions, NULL, "W2F Lowering");
01209
01210 Start_Timer(T_W2F_CU);
01211 if (W2F_Progname != NULL)
01212 Diag_Set_Phase(W2F_Progname);
01213 else
01214 Diag_Set_Phase("FLIST");
01215
01216 if (!pu_is_pushed)
01217 W2F_Push_PU(pu, WN_func_body(pu));
01218
01219
01220
01221 PU & pucur = Pu_Table[ST_pu(PUINFO_FUNC_ST)];
01222 WN2F_F90_pu = PU_f90_lang(pucur) != 0;
01223
01224
01225
01226 BOOL nested = PU_is_nested_func(pucur) && !(PU_mp(pucur)) ;
01227
01228 tokens = New_Token_Buffer();
01229
01230 if (nested)
01231 {
01232 WN2F_Emit_End_Stmt(tokens,TRUE);
01233 Increment_Indentation();
01234 } else
01235 WN2F_Emit_End_Stmt(tokens,FALSE);
01236
01237
01238
01239 (void)WN2F_translate(tokens, pu, Global_Context);
01240 Write_And_Reclaim_Tokens(W2F_File[W2F_FTN_FILE],
01241 W2F_File[W2F_LOC_FILE],
01242 &tokens);
01243
01244 if (nested)
01245 Decrement_Indentation();
01246
01247 if (!pu_is_pushed)
01248 W2F_Pop_PU();
01249
01250 W2F_Undo_Whirl_Side_Effects();
01251
01252 Stop_Timer(T_W2F_CU);
01253 Diag_Set_Phase(caller_err_phase);
01254 }
01255
01256
01257 void
01258 W2F_Outfile_Fini(void)
01259 {
01260 TOKEN_BUFFER tokens;
01261
01262
01263
01264
01265 const char *loc_fname = W2F_File_Name[W2F_LOC_FILE];
01266
01267 if (!Check_Outfile_Initialized("W2F_Outfile_Fini"))
01268 return;
01269
01270 Clear_w2fc_flags() ;
01271
01272
01273
01274
01275 tokens = New_Token_Buffer();
01276
01277 WN2F_Emit_End_Stmt(tokens,FALSE);
01278
01279 WN2F_Append_Block_Data(tokens);
01280 Write_And_Reclaim_Tokens(W2F_File[W2F_FTN_FILE],
01281 W2F_File[W2F_LOC_FILE],
01282 &tokens);
01283
01284
01285
01286
01287 Close_W2f_Output_File(W2F_FTN_FILE);
01288 W2F_Outfile_Initialized = FALSE;
01289 W2F_Fini();
01290
01291 if (W2F_Prompf_Emission && loc_fname != NULL)
01292 {
01293
01294
01295 Move_Locations_To_Anl_File(loc_fname);
01296 }
01297 }
01298
01299
01300 void
01301 W2F_Cleanup(void)
01302 {
01303
01304
01305 Close_W2f_Output_File(W2F_LOC_FILE);
01306 Close_W2f_Output_File(W2F_FTN_FILE);
01307 if (W2F_File_Name[W2F_LOC_FILE] != NULL)
01308 unlink(W2F_File_Name[W2F_LOC_FILE]);
01309 }