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 #ifdef _KEEP_RCS_ID
00064 static char *rcs_id = "$Source: /proj/osprey/CVS/open64/osprey1.0/be/whirl2c/w2c_driver.cxx,v $ $Revision: 1.1.1.1 $";
00065 #endif
00066
00067 #include <sys/elf_whirl.h>
00068 #include <time.h>
00069 #include "whirl2c_common.h"
00070 #include "config_clist.h"
00071 #include "config_list.h"
00072 #include "w2cf_parentize.h"
00073 #include "file_util.h"
00074 #include "flags.h"
00075 #include "timing.h"
00076 #include "wn_lower.h"
00077
00078 #include "PUinfo.h"
00079 #include "ty2c.h"
00080 #include "st2c.h"
00081 #include "tcon2c.h"
00082 #include "wn2c.h"
00083 #include "w2c_driver.h"
00084
00085
00086
00087
00088 #undef int
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 static const char *W2C_File_Extension[W2C_NUM_FILES] =
00100 {
00101 ".c",
00102 ".w2c.h",
00103 ".w2c.c",
00104 ".w2c.loc"
00105 };
00106
00107
00108 static const char *W2C_Cite_Extension[W2C_NUM_FILES] =
00109 {
00110 ".c",
00111 "-after-lno.h",
00112 "-after-lno.c",
00113 ".loc"
00114 };
00115
00116
00117 static const char *W2C_Prompf_Extension[W2C_NUM_FILES] =
00118 {
00119 ".c",
00120 ".mh",
00121 ".m",
00122 ".anl_srcpos"
00123 };
00124
00125
00126 #define W2C_Extension(i) \
00127 (W2C_Prompf_Emission? W2C_Prompf_Extension[i] : \
00128 (List_Cite ? W2C_Cite_Extension[i] : W2C_File_Extension[i]))
00129
00130
00131
00132
00133 #define W2C_MAX_SKIP_ITEMS 128
00134 static W2CF_SKIP_ITEM Skip[W2C_MAX_SKIP_ITEMS+1];
00135 static INT Next_Skip_Item = 0;
00136
00137
00138
00139 static BOOL W2C_Outfile_Initialized = FALSE;
00140 static BOOL W2C_Initialized = FALSE;
00141 static CONTEXT Global_Context = INIT_CONTEXT;
00142 static const char *W2C_Progname = "";
00143 static const char *W2C_File_Name[W2C_NUM_FILES] =
00144 {NULL, NULL, NULL, NULL};
00145 static BOOL File_Is_Created[W2C_NUM_FILES] =
00146 {FALSE, FALSE, FALSE, FALSE};
00147 static MEM_POOL W2C_Parent_Pool;
00148
00149
00150 FILE *W2C_File[W2C_NUM_FILES] = {NULL, NULL, NULL, NULL};
00151 BOOL W2C_Enabled = TRUE;
00152 BOOL W2C_Verbose = TRUE;
00153 BOOL W2C_No_Pragmas = FALSE;
00154 BOOL W2C_Emit_Adims = FALSE;
00155 BOOL W2C_Emit_Prefetch = FALSE;
00156 BOOL W2C_Emit_All_Regions = TRUE;
00157 BOOL W2C_Emit_Linedirs = FALSE;
00158 BOOL W2C_Emit_Nested_PUs = FALSE;
00159 BOOL W2C_Emit_Frequency = FALSE;
00160 BOOL W2C_Emit_Cgtag = FALSE;
00161 BOOL W2C_Lower_Fortran = FALSE;
00162 BOOL W2C_Emit_Omp = FALSE;
00163 INT32 W2C_Line_Length = 0;
00164
00165
00166 BOOL W2C_Only_Mark_Loads = FALSE;
00167 BOOL W2C_Purple_Emission = FALSE;
00168 BOOL W2C_Prompf_Emission = FALSE;
00169 const WN_MAP *W2C_Construct_Map = NULL;
00170 WN_MAP W2C_Frequency_Map = WN_MAP_UNDEFINED;
00171 BOOL W2C_Cplus_Initializer = FALSE;
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
00211
00212
00213
00214 static void
00215 Process_Filename_Options(const char *src_filename, const char *irb_filename)
00216 {
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 #define MAX_FNAME_LENGTH 256-7
00237 static char filename[MAX_FNAME_LENGTH+7];
00238 const char *fname;
00239
00240
00241 if (W2C_File_Name[W2C_ORIG_FILE] == NULL)
00242 {
00243 if (src_filename != NULL && src_filename[0] != '\0')
00244 W2C_File_Name[W2C_ORIG_FILE] = src_filename;
00245 else if (irb_filename != NULL && irb_filename[0] != '\0')
00246 W2C_File_Name[W2C_ORIG_FILE] = irb_filename;
00247 else
00248 W2C_File_Name[W2C_ORIG_FILE] = "anonymous.c";
00249 }
00250
00251
00252 if (strlen(W2C_File_Name[W2C_ORIG_FILE]) > MAX_FNAME_LENGTH)
00253 {
00254 W2C_File_Name[W2C_ORIG_FILE] =
00255 strncpy(filename, W2C_File_Name[W2C_ORIG_FILE], MAX_FNAME_LENGTH);
00256 filename[MAX_FNAME_LENGTH] = '\0';
00257 fprintf(stderr,
00258 "WARNING: src_file name truncated to (max=%d chars): \"%s\"\n",
00259 MAX_FNAME_LENGTH, W2C_File_Name[W2C_ORIG_FILE]);
00260 }
00261 else
00262 W2C_File_Name[W2C_ORIG_FILE] =
00263 strcpy(filename, W2C_File_Name[W2C_ORIG_FILE]);
00264
00265
00266
00267
00268
00269 fname = Last_Pathname_Component(filename);
00270 if ( W2C_File_Name[W2C_DOTH_FILE] == NULL ) {
00271 if ( W2C_File_Name[W2C_DOTC_FILE] == NULL ) {
00272 W2C_File_Name[W2C_DOTH_FILE] =
00273 New_Extension ( fname, W2C_Extension(W2C_DOTH_FILE) );
00274 }
00275 else {
00276
00277 W2C_File_Name[W2C_DOTH_FILE] =
00278 New_Extension ( W2C_File_Name[W2C_DOTC_FILE], W2C_Extension(W2C_DOTH_FILE) );
00279 }
00280 }
00281 if ( W2C_File_Name[W2C_DOTC_FILE] == NULL ) {
00282 W2C_File_Name[W2C_DOTC_FILE] =
00283 New_Extension ( fname, W2C_Extension(W2C_DOTC_FILE) );
00284 }
00285 if (W2C_File_Name[W2C_LOC_FILE] == NULL)
00286 {
00287 if (List_Cite || W2C_Prompf_Emission)
00288 {
00289 W2C_File_Name[W2C_LOC_FILE] =
00290 New_Extension(fname, W2C_Extension(W2C_LOC_FILE));
00291 }
00292 }
00293 }
00294
00295
00296 static FILE *
00297 Open_Read_File(const char *filename)
00298 {
00299 FILE *f = NULL;
00300
00301
00302 if (filename == NULL ||
00303 (f = fopen(filename, "r")) == NULL)
00304 {
00305 ErrMsg(EC_IR_Open, filename, errno);
00306 }
00307 return f;
00308 }
00309
00310
00311 static FILE *
00312 Open_Append_File(const char *filename)
00313 {
00314 FILE *f = NULL;
00315
00316
00317 if (filename == NULL ||
00318 (f = fopen(filename, "a")) == NULL)
00319 {
00320 ErrMsg(EC_IR_Open, filename, errno);
00321 }
00322 return f;
00323 }
00324
00325
00326 static FILE *
00327 Open_Create_File(const char *filename)
00328 {
00329 FILE *f = NULL;
00330
00331
00332 if (filename == NULL ||
00333 (f = fopen(filename, "w")) == NULL)
00334 {
00335 ErrMsg(EC_IR_Open, filename, errno);
00336 }
00337 return f;
00338 }
00339
00340
00341 static void
00342 Close_File(const char *filename, FILE *afile)
00343 {
00344 if (afile != NULL &&
00345 !Same_File(afile, stdout) &&
00346 !Same_File(afile, stderr) &&
00347 fclose(afile) != 0)
00348 {
00349 Set_Error_Line(ERROR_LINE_UNKNOWN);
00350 ErrMsg(EC_Src_Close, filename, errno);
00351 }
00352 }
00353
00354
00355 static void
00356 Open_W2c_Output_File(W2C_FILE_KIND kind)
00357 {
00358 if (W2C_File[kind] == NULL)
00359 {
00360 if (File_Is_Created[kind])
00361 {
00362 W2C_File[kind] = Open_Append_File(W2C_File_Name[kind]);
00363 }
00364 else
00365 {
00366 W2C_File[kind] = Open_Create_File(W2C_File_Name[kind]);
00367 File_Is_Created[kind] = TRUE;
00368 }
00369 }
00370 }
00371
00372
00373 static void
00374 Close_W2c_Output_File(W2C_FILE_KIND kind)
00375 {
00376 Close_File(W2C_File_Name[kind], W2C_File[kind]);
00377 W2C_File[kind] = NULL;
00378 }
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395 static void
00396 Begin_New_Locations_File(void)
00397 {
00398
00399
00400
00401
00402
00403 if (W2C_File_Name[W2C_LOC_FILE] != NULL)
00404 {
00405
00406
00407
00408 if (W2C_Prompf_Emission)
00409 {
00410 Open_W2c_Output_File(W2C_LOC_FILE);
00411 Write_String(W2C_File[W2C_LOC_FILE], NULL,
00412 "SRCPOS_MAP_BEGIN\n");
00413 }
00414 else
00415 {
00416 Open_W2c_Output_File(W2C_LOC_FILE);
00417 Write_String(W2C_File[W2C_LOC_FILE], NULL,
00418 "(SRCPOS-MAP\n");
00419 }
00420 }
00421 }
00422
00423
00424 static void
00425 End_Locations_File(void)
00426 {
00427
00428
00429
00430 if (W2C_File_Name[W2C_LOC_FILE] != NULL)
00431 {
00432
00433
00434
00435 if (W2C_Prompf_Emission)
00436 {
00437 Open_W2c_Output_File(W2C_LOC_FILE);
00438 Write_String(W2C_File[W2C_LOC_FILE],
00439 NULL,
00440 "SRCPOS_MAP_END\n");
00441 }
00442 else
00443 {
00444 Open_W2c_Output_File(W2C_LOC_FILE);
00445 Write_String(W2C_File[W2C_LOC_FILE], NULL, ")\n");
00446 }
00447 Terminate_Token_Buffer(W2C_File[W2C_LOC_FILE]);
00448 Close_W2c_Output_File(W2C_LOC_FILE);
00449 }
00450 }
00451
00452
00453 static void
00454 Continue_Locations_File(void)
00455 {
00456
00457
00458
00459 if (W2C_File_Name[W2C_LOC_FILE] != NULL)
00460 {
00461 Open_W2c_Output_File(W2C_LOC_FILE);
00462 }
00463 }
00464
00465
00466 static void
00467 Move_Locations_To_Anl_File(const char *loc_fname)
00468 {
00469 #define MAX_ANL_FNAME_LENGTH 256-5
00470 char cbuf[MAX_ANL_FNAME_LENGTH+1];
00471 INT i, next_ch;
00472 FILE *anl_file;
00473 FILE *loc_file;
00474 const char *anl_fname;
00475 static char fname[MAX_ANL_FNAME_LENGTH+5];
00476
00477 strncpy(fname, loc_fname, MAX_ANL_FNAME_LENGTH);
00478 anl_fname = Last_Pathname_Component(fname);
00479 anl_fname = New_Extension(anl_fname, ".anl");
00480 anl_file = Open_Append_File(anl_fname);
00481 loc_file = Open_Read_File(loc_fname);
00482
00483 next_ch = getc(loc_file);
00484 while (next_ch != EOF)
00485 {
00486 for (i = 0; (next_ch != EOF && i < MAX_ANL_FNAME_LENGTH); i++)
00487 {
00488 cbuf[i] = next_ch;
00489 next_ch = getc(loc_file);
00490 }
00491 if (i > 0)
00492 {
00493 cbuf[i] = '\0';
00494 fputs(cbuf, anl_file);
00495 }
00496 }
00497 Close_File(anl_fname, anl_file);
00498 Close_File(loc_fname, loc_file);
00499 unlink(loc_fname);
00500 }
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515 static void
00516 W2C_Undo_Whirl_Side_Effects(void)
00517 {
00518 Stab_Free_Tmpvars();
00519 Stab_Free_Namebufs();
00520 }
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534 static void
00535 W2C_Enter_Global_Symbols(void)
00536 {
00537 const ST *st;
00538 ST_IDX st_idx;
00539 FLD_HANDLE fld;
00540 TY_IDX ty;
00541
00542
00543
00544
00545
00546
00547
00548 for (ty = 1; ty < TY_Table_Size(); ty++)
00549 {
00550 if (TY_Is_Structured(ty))
00551 {
00552 (void)W2CF_Symtab_Nameof_Ty(ty);
00553 for (fld = TY_flist(Ty_Table[ty]); !fld.Is_Null (); fld = FLD_next(fld))
00554 (void)W2CF_Symtab_Nameof_Fld(fld);
00555 }
00556 }
00557
00558
00559
00560
00561 FOREACH_SYMBOL(GLOBAL_SYMTAB, st, st_idx)
00562 {
00563 if ((ST_sym_class(st) == CLASS_VAR || ST_sym_class(st) == CLASS_FUNC) &&
00564 !Stab_Is_Based_At_Common_Or_Equivalence(st))
00565 {
00566 if (ST_sym_class(st) == CLASS_VAR && ST_sclass(st) == SCLASS_CPLINIT)
00567 W2C_Cplus_Initializer = TRUE;
00568
00569 (void)W2CF_Symtab_Nameof_St(st);
00570 }
00571 else if (ST_sym_class(st) == CLASS_CONST)
00572 {
00573 (void)W2CF_Symtab_Nameof_St(st);
00574 }
00575 }
00576 }
00577
00578
00579
00580
00581
00582
00583
00584 static BOOL
00585 Check_Outfile_Initialized(const char *caller_name)
00586 {
00587 if (!W2C_Outfile_Initialized)
00588 fprintf(stderr,
00589 "NOTE: Ignored call to %s(); call W2C_Outfile_Init() first!\n",
00590 caller_name);
00591 return W2C_Outfile_Initialized;
00592 }
00593
00594 static BOOL
00595 Check_Initialized(const char *caller_name)
00596 {
00597 if (!W2C_Initialized)
00598 fprintf(stderr,
00599 "NOTE: Ignored call to %s(); call W2C_Init() first!\n",
00600 caller_name);
00601 return W2C_Initialized;
00602 }
00603
00604 static BOOL
00605 Check_PU_Pushed(const char *caller_name)
00606 {
00607 if (PUinfo_current_func == NULL)
00608 fprintf(stderr,
00609 "NOTE: Ignored call to %s(); call W2C_Push_PU() first!\n",
00610 caller_name);
00611 return (PUinfo_current_func != NULL);
00612 }
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624 BOOL
00625 W2C_Should_Emit_Nested_PUs(void)
00626 {
00627 return W2C_Emit_Nested_PUs;
00628 }
00629
00630
00631
00632 void
00633 W2C_Process_Command_Line (INT phase_argc, const char * const phase_argv[],
00634 INT argc, const char * const argv[])
00635 {
00636
00637
00638 if (argv[0] != NULL)
00639 W2C_Progname = argv[0];
00640
00641
00642
00643
00644
00645 W2C_File_Name[W2C_ORIG_FILE] = CLIST_orig_filename;
00646 W2C_File_Name[W2C_DOTH_FILE] = CLIST_doth_filename;
00647 W2C_File_Name[W2C_DOTC_FILE] = CLIST_dotc_filename;
00648 W2C_File_Name[W2C_LOC_FILE] = CLIST_loc_filename;
00649 W2C_Enabled = CLIST_enabled;
00650 W2C_Verbose = CLIST_verbose;
00651 W2C_No_Pragmas = CLIST_no_pragmas;
00652 W2C_Emit_Adims = CLIST_emit_adims;
00653 W2C_Emit_Prefetch = CLIST_emit_prefetch;
00654 W2C_Emit_All_Regions = CLIST_emit_all_regions;
00655 W2C_Emit_Linedirs = CLIST_emit_linedirs;
00656 W2C_Emit_Nested_PUs = CLIST_emit_nested_pus;
00657 W2C_Emit_Frequency = CLIST_emit_frequency;
00658 W2C_Emit_Cgtag = CLIST_emit_cgtag;
00659 W2C_Lower_Fortran = CLIST_lower_ftn;
00660 W2C_Emit_Omp = CLIST_emit_omp;
00661 W2C_Line_Length = CLIST_line_length;
00662
00663 Process_Filename_Options(Src_File_Name, Irb_File_Name);
00664 }
00665
00666
00667 void
00668 W2C_Init(void)
00669 {
00670
00671
00672
00673 const char * const caller_err_phase = Get_Error_Phase ();
00674
00675 if (W2C_Initialized)
00676 return;
00677
00678 Set_Error_Phase ( "W2C Initialization" );
00679
00680
00681
00682 MEM_POOL_Initialize(&W2C_Parent_Pool, "W2C_Parent_Pool", FALSE);
00683 MEM_POOL_Push(&W2C_Parent_Pool);
00684
00685
00686
00687 Initialize_Token_Buffer(FREE_FORMAT, W2C_Prompf_Emission);
00688 if (W2C_Line_Length > 0)
00689 Set_Maximum_Linelength(W2C_Line_Length);
00690
00691
00692
00693
00694
00695
00696 Stab_initialize_flags();
00697 W2CF_Symtab_Push();
00698 W2C_Enter_Global_Symbols();
00699
00700
00701
00702 CONTEXT_reset(Global_Context);
00703 TY2C_initialize(Global_Context);
00704 ST2C_initialize(Global_Context);
00705 TCON2C_initialize();
00706 WN2C_initialize();
00707 PUinfo_initialize();
00708
00709 W2C_Initialized = TRUE;
00710 Set_Error_Phase ( caller_err_phase );
00711 }
00712
00713
00714 void
00715 W2C_Push_PU(const WN *pu, WN *body_part_of_interest)
00716 {
00717 if (!Check_Initialized("W2C_Push_PU"))
00718 return;
00719
00720 Is_True(WN_opcode(pu) == OPC_FUNC_ENTRY,
00721 ("Invalid opcode for W2C_Push_PU()"));
00722
00723
00724
00725 MEM_POOL_Push(&W2C_Parent_Pool);
00726 W2CF_Parent_Map = WN_MAP_Create(&W2C_Parent_Pool);
00727 W2CF_Parentize(pu);
00728 Stab_initialize();
00729
00730
00731
00732
00733 if (WN_opc_operator(body_part_of_interest) == OPR_BLOCK)
00734 {
00735 Remove_Skips(body_part_of_interest,
00736 Skip,
00737 &Next_Skip_Item,
00738 W2C_MAX_SKIP_ITEMS,
00739 TRUE );
00740 }
00741
00742 PUinfo_init_pu(pu, body_part_of_interest);
00743 }
00744
00745
00746 void
00747 W2C_Pop_PU(void)
00748 {
00749 SYMTAB_IDX symtab;
00750
00751 if (!Check_Initialized("W2C_Pop_PU") ||
00752 !Check_PU_Pushed("W2C_Pop_PU"))
00753 return;
00754
00755 PUinfo_exit_pu();
00756
00757
00758
00759 if (Next_Skip_Item > 0)
00760 {
00761 Restore_Skips(Skip, Next_Skip_Item, TRUE );
00762 Next_Skip_Item = 0;
00763 }
00764
00765
00766
00767
00768
00769 symtab = CURRENT_SYMTAB;
00770 CURRENT_SYMTAB = GLOBAL_SYMTAB;
00771 WN2C_new_symtab();
00772 CURRENT_SYMTAB = symtab;
00773 Stab_finalize();
00774
00775 WN_MAP_Delete(W2CF_Parent_Map);
00776 W2CF_Parent_Map = WN_MAP_UNDEFINED;
00777 MEM_POOL_Pop(&W2C_Parent_Pool);
00778
00779 W2C_Frequency_Map = WN_MAP_UNDEFINED;
00780 }
00781
00782
00783 void
00784 W2C_Mark_Loads(void)
00785 {
00786 W2C_Only_Mark_Loads = TRUE;
00787 }
00788
00789
00790 void
00791 W2C_Nomark_Loads(void)
00792 {
00793 W2C_Only_Mark_Loads = FALSE;
00794 }
00795
00796
00797 void
00798 W2C_Set_Prompf_Emission(const WN_MAP *construct_map)
00799 {
00800 W2C_Prompf_Emission = TRUE;
00801 W2C_Construct_Map = construct_map;
00802 }
00803
00804
00805 void
00806 W2C_Set_Frequency_Map(WN_MAP frequency_map)
00807 {
00808 W2C_Frequency_Map = frequency_map;
00809 }
00810
00811
00812 const char *
00813 W2C_Get_Transformed_Src_Path(void)
00814 {
00815 return W2C_File_Name[W2C_DOTC_FILE];
00816 }
00817
00818
00819 void
00820 W2C_Set_Purple_Emission(void)
00821 {
00822 W2C_Purple_Emission = TRUE;
00823 }
00824
00825
00826 void
00827 W2C_Reset_Purple_Emission(void)
00828 {
00829 W2C_Purple_Emission = FALSE;
00830 }
00831
00832
00833 void
00834 W2C_def_TY(FILE *outfile, TY_IDX ty)
00835 {
00836 TOKEN_BUFFER tokens;
00837 CONTEXT context = INIT_CONTEXT;
00838
00839 if (!Check_Initialized("W2C_def_TY"))
00840 return;
00841
00842 tokens = New_Token_Buffer();
00843 TY2C_translate(tokens, ty, context);
00844 Write_And_Reclaim_Tokens(outfile, W2C_File[W2C_LOC_FILE], &tokens);
00845 W2C_Undo_Whirl_Side_Effects();
00846 }
00847
00848
00849 void
00850 W2C_Translate_Global_Types(FILE *outfile)
00851 {
00852 FILE *saved_hfile = W2C_File[W2C_DOTH_FILE];
00853
00854 if (!Check_Initialized("W2C_Translate_Global_Types"))
00855 return;
00856
00857 W2C_File[W2C_DOTH_FILE] = outfile;
00858 WN2C_translate_structured_types();
00859 W2C_File[W2C_DOTH_FILE] = saved_hfile;
00860 W2C_Undo_Whirl_Side_Effects();
00861 }
00862
00863
00864 void
00865 W2C_Translate_Global_Defs(FILE *outfile)
00866 {
00867 FILE *saved_hfile = W2C_File[W2C_DOTH_FILE];
00868
00869 if (!Check_Initialized("W2C_Translate_Global_Defs"))
00870 return;
00871
00872 W2C_File[W2C_DOTH_FILE] = outfile;
00873 WN2C_translate_file_scope_defs(Global_Context);
00874 W2C_File[W2C_DOTH_FILE] = saved_hfile;
00875 W2C_Undo_Whirl_Side_Effects();
00876 }
00877
00878
00879 const char *
00880 W2C_Object_Name(const ST *func_st)
00881 {
00882 return W2CF_Symtab_Nameof_St(func_st);
00883 }
00884
00885
00886 void
00887 W2C_Translate_Stid_Lhs(char *strbuf,
00888 UINT bufsize,
00889 const ST *stid_st,
00890 STAB_OFFSET stid_ofst,
00891 TY_IDX stid_ty,
00892 TYPE_ID stid_mtype)
00893 {
00894 TOKEN_BUFFER tokens;
00895 TY_IDX stored_ty;
00896 CONTEXT context = INIT_CONTEXT;
00897
00898 tokens = New_Token_Buffer();
00899 WN2C_stid_lhs(tokens,
00900 &stored_ty,
00901 stid_st,
00902 stid_ofst,
00903 stid_ty,
00904 stid_mtype,
00905 context);
00906 Str_Write_And_Reclaim_Tokens(strbuf, bufsize, &tokens);
00907 W2C_Undo_Whirl_Side_Effects();
00908 }
00909
00910
00911 void
00912 W2C_Translate_Istore_Lhs(char *strbuf,
00913 UINT bufsize,
00914 const WN *lhs,
00915 STAB_OFFSET istore_ofst,
00916 TY_IDX istore_ty_idx,
00917 TYPE_ID istore_mtype)
00918 {
00919 TOKEN_BUFFER tokens;
00920 TY_IDX stored_ty;
00921 CONTEXT context = INIT_CONTEXT;
00922
00923 tokens = New_Token_Buffer();
00924 WN2C_memref_lhs(tokens,
00925 &stored_ty,
00926 lhs,
00927 istore_ofst,
00928 istore_ty_idx,
00929 Ty_Table[istore_ty_idx].Pointed(),
00930 istore_mtype,
00931 context);
00932 Str_Write_And_Reclaim_Tokens(strbuf, bufsize, &tokens);
00933 W2C_Undo_Whirl_Side_Effects();
00934 }
00935
00936
00937 void
00938 W2C_Translate_Wn(FILE *outfile, const WN *wn)
00939 {
00940 TOKEN_BUFFER tokens;
00941 CONTEXT context = INIT_CONTEXT;
00942 const char * const caller_err_phase = Get_Error_Phase ();
00943
00944 if (!Check_Initialized("W2C_Translate_Wn") ||
00945 !Check_PU_Pushed("W2C_Translate_Wn"))
00946 return;
00947
00948 Start_Timer (T_W2C_CU);
00949 Set_Error_Phase ("WHIRL To C");
00950
00951 tokens = New_Token_Buffer();
00952 (void)WN2C_translate(tokens, wn, context);
00953 Write_And_Reclaim_Tokens(outfile, W2C_File[W2C_LOC_FILE], &tokens);
00954 W2C_Undo_Whirl_Side_Effects();
00955
00956 Stop_Timer (T_W2C_CU);
00957 Set_Error_Phase (caller_err_phase);
00958 }
00959
00960
00961 void
00962 W2C_Translate_Wn_Str(char *strbuf, UINT bufsize, const WN *wn)
00963 {
00964 TOKEN_BUFFER tokens;
00965 CONTEXT context = INIT_CONTEXT;
00966 const char * const caller_err_phase = Get_Error_Phase ();
00967
00968 if (!Check_Initialized("W2C_Translate_Wn_Str") ||
00969 !Check_PU_Pushed("W2C_Translate_Wn_Str"))
00970 return;
00971
00972 Start_Timer (T_W2C_CU);
00973 Set_Error_Phase ("WHIRL To C");
00974
00975 tokens = New_Token_Buffer();
00976 (void)WN2C_translate(tokens, wn, context);
00977 Str_Write_And_Reclaim_Tokens(strbuf, bufsize, &tokens);
00978 W2C_Undo_Whirl_Side_Effects();
00979
00980 Stop_Timer (T_W2C_CU);
00981 Set_Error_Phase (caller_err_phase);
00982 }
00983
00984
00985 void
00986 W2C_Translate_Purple_Main(FILE *outfile, const WN *pu, const char *region_name)
00987 {
00988 TOKEN_BUFFER tokens;
00989 CONTEXT context = INIT_CONTEXT;
00990 const char * const caller_err_phase = Get_Error_Phase ();
00991
00992 if (!Check_Initialized("W2C_Translate_Purple_Main"))
00993 return;
00994
00995 Is_True(WN_opcode(pu) == OPC_FUNC_ENTRY,
00996 ("Invalid opcode for W2C_Translate_Purple_Main()"));
00997
00998 Start_Timer (T_W2C_CU);
00999 Set_Error_Phase ("WHIRL To C");
01000
01001
01002
01003 tokens = New_Token_Buffer();
01004 W2C_Push_PU(pu, WN_func_body(pu));
01005 (void)WN2C_translate_purple_main(tokens, pu, region_name, context);
01006 W2C_Pop_PU();
01007 W2C_Undo_Whirl_Side_Effects();
01008 Write_And_Reclaim_Tokens(outfile, W2C_File[W2C_LOC_FILE], &tokens);
01009
01010 Stop_Timer (T_W2C_CU);
01011 Set_Error_Phase (caller_err_phase);
01012 }
01013
01014
01015 void
01016 W2C_Fini(void)
01017 {
01018
01019
01020
01021
01022 INT i;
01023
01024 if (!Check_Initialized("W2C_Fini"))
01025 return;
01026 else if (!W2C_Outfile_Initialized)
01027 {
01028 Stab_Reset_Referenced_Flag(GLOBAL_SYMTAB);
01029 TY2C_finalize();
01030 ST2C_finalize();
01031 TCON2C_finalize();
01032 WN2C_finalize();
01033 PUinfo_finalize();
01034 W2CF_Symtab_Terminate();
01035 Stab_finalize_flags();
01036 if (W2C_File_Name[W2C_LOC_FILE] != NULL)
01037 End_Locations_File();
01038 else
01039 Terminate_Token_Buffer(NULL);
01040
01041
01042 W2C_Initialized = FALSE;
01043 CONTEXT_reset(Global_Context);
01044 W2C_Progname = "";
01045 for (i=0;i<W2C_NUM_FILES;i++) W2C_File_Name[i] = NULL;
01046 for (i=0;i<W2C_NUM_FILES;i++) File_Is_Created[i] = FALSE;
01047 for (i=0;i<W2C_NUM_FILES;i++) W2C_File[i] = NULL;
01048 W2C_Enabled = TRUE;
01049 W2C_Verbose = TRUE;
01050 W2C_No_Pragmas = FALSE;
01051 W2C_Emit_Adims = FALSE;
01052 W2C_Emit_Prefetch = FALSE;
01053 W2C_Emit_All_Regions = FALSE;
01054 W2C_Emit_Linedirs = FALSE;
01055 W2C_Emit_Nested_PUs = FALSE;
01056 W2C_Emit_Frequency = FALSE;
01057 W2C_Lower_Fortran = FALSE;
01058 W2C_Line_Length = 0;
01059
01060 W2C_Only_Mark_Loads = FALSE;
01061 W2C_Cplus_Initializer = FALSE;
01062
01063 MEM_POOL_Pop(&W2C_Parent_Pool);
01064 MEM_POOL_Delete(&W2C_Parent_Pool);
01065 }
01066 }
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097
01098 void
01099 W2C_Outfile_Init(BOOL emit_global_decls)
01100 {
01101
01102
01103
01104
01105
01106
01107
01108
01109 time_t systime;
01110
01111 if (W2C_Outfile_Initialized)
01112 return;
01113
01114 W2C_Outfile_Initialized = TRUE;
01115 if (W2C_Verbose)
01116 {
01117 if (W2C_Prompf_Emission || W2C_File_Name[W2C_LOC_FILE] == NULL)
01118 fprintf(stderr,
01119 "%s translates %s into %s and %s, based on source %s\n",
01120 W2C_Progname,
01121 Irb_File_Name,
01122 W2C_File_Name[W2C_DOTH_FILE],
01123 W2C_File_Name[W2C_DOTC_FILE],
01124 W2C_File_Name[W2C_ORIG_FILE]);
01125 else
01126 fprintf(stderr,
01127 "%s translates %s into %s, %s and %s, based on source %s\n",
01128 W2C_Progname,
01129 Irb_File_Name,
01130 W2C_File_Name[W2C_DOTH_FILE],
01131 W2C_File_Name[W2C_DOTC_FILE],
01132 W2C_File_Name[W2C_LOC_FILE],
01133 W2C_File_Name[W2C_ORIG_FILE]);
01134 }
01135
01136
01137
01138 if (!W2C_Initialized)
01139 W2C_Init();
01140
01141
01142
01143 Begin_New_Locations_File();
01144 Open_W2c_Output_File(W2C_DOTC_FILE);
01145
01146
01147
01148
01149 systime = time(NULL);
01150 Write_String(W2C_File[W2C_DOTC_FILE], W2C_File[W2C_LOC_FILE],
01151 "/*******************************************************\n"
01152 " * C file translated from WHIRL ");
01153 Write_String(W2C_File[W2C_DOTC_FILE], W2C_File[W2C_LOC_FILE],
01154 ((systime != (time_t)-1)?
01155 ctime(&systime) : "at unknown time\n"));
01156 Write_String(W2C_File[W2C_DOTC_FILE], W2C_File[W2C_LOC_FILE],
01157 " *******************************************************/"
01158 "\n\n");
01159
01160
01161 if (emit_global_decls)
01162 {
01163 Open_W2c_Output_File(W2C_DOTH_FILE);
01164
01165
01166 Write_String(W2C_File[W2C_DOTH_FILE], NULL,
01167 "/* Include builtin types and operators */\n"
01168 "#include <whirl2c.h>\n\n");
01169
01170
01171 Write_String(W2C_File[W2C_DOTC_FILE], W2C_File[W2C_LOC_FILE],
01172 "/* Include file-level type and variable decls */\n"
01173 "#include \"");
01174 Write_String(W2C_File[W2C_DOTC_FILE], W2C_File[W2C_LOC_FILE],
01175 W2C_File_Name[W2C_DOTH_FILE]);
01176 Write_String(W2C_File[W2C_DOTC_FILE], W2C_File[W2C_LOC_FILE],
01177 "\"\n\n");
01178 }
01179
01180 if (emit_global_decls)
01181 WN2C_translate_structured_types();
01182
01183 W2C_Outfile_Initialized = TRUE;
01184
01185 }
01186
01187
01188 void
01189 W2C_Outfile_Translate_Pu(WN *pu, BOOL emit_global_decls)
01190 {
01191 TOKEN_BUFFER tokens;
01192 LOWER_ACTIONS lower_actions = LOWER_NULL;
01193 const BOOL pu_is_pushed = (PUinfo_current_func != NULL);
01194 const char * const caller_err_phase = Get_Error_Phase ();
01195
01196 if (!Check_Outfile_Initialized("W2C_Outfile_Translate_Pu"))
01197 return;
01198
01199 Is_True(WN_opcode(pu) == OPC_FUNC_ENTRY,
01200 ("Invalid opcode for W2C_Outfile_Translate_Pu()"));
01201
01202
01203
01204 Continue_Locations_File();
01205 Open_W2c_Output_File(W2C_DOTC_FILE);
01206 if (emit_global_decls)
01207 Open_W2c_Output_File(W2C_DOTH_FILE);
01208
01209 if (W2C_Emit_Nested_PUs && !W2C_Lower_Fortran)
01210 lower_actions = LOWER_MP;
01211 else if (!W2C_Emit_Nested_PUs && W2C_Lower_Fortran)
01212 lower_actions = LOWER_IO_STATEMENT | LOWER_INTRINSIC;
01213 else if(W2C_Emit_Nested_PUs && W2C_Lower_Fortran)
01214 lower_actions = LOWER_MP | LOWER_IO_STATEMENT | LOWER_INTRINSIC;
01215
01216 if (lower_actions != LOWER_NULL)
01217 pu = WN_Lower(pu, lower_actions, NULL, "W2C Lowering");
01218
01219 Start_Timer (T_W2C_CU);
01220 Set_Error_Phase ("WHIRL To C");
01221
01222 if (!pu_is_pushed)
01223 W2C_Push_PU(pu, WN_func_body(pu));
01224
01225 tokens = New_Token_Buffer();
01226 (void)WN2C_translate(tokens, pu, Global_Context);
01227 Write_And_Reclaim_Tokens(W2C_File[W2C_DOTC_FILE],
01228 W2C_File[W2C_LOC_FILE],
01229 &tokens);
01230
01231 if (!pu_is_pushed)
01232 W2C_Pop_PU();
01233
01234 W2C_Undo_Whirl_Side_Effects();
01235
01236 Stop_Timer (T_W2C_CU);
01237 Set_Error_Phase (caller_err_phase);
01238 }
01239
01240
01241 void
01242 W2C_Outfile_Fini(BOOL emit_global_decls)
01243 {
01244
01245
01246
01247 const char *loc_fname = W2C_File_Name[W2C_LOC_FILE];
01248
01249 if (!Check_Outfile_Initialized("W2C_Outfile_Fini"))
01250 return;
01251
01252
01253 Continue_Locations_File();
01254 Open_W2c_Output_File(W2C_DOTC_FILE);
01255 if (emit_global_decls)
01256 Open_W2c_Output_File(W2C_DOTH_FILE);
01257
01258 if (emit_global_decls)
01259 {
01260 TOKEN_BUFFER tokens = New_Token_Buffer();
01261
01262 WN2C_translate_file_scope_defs(Global_Context);
01263
01264 ST2C_Define_Common_Blocks(tokens, Global_Context);
01265 Write_And_Reclaim_Tokens(W2C_File[W2C_DOTH_FILE],
01266 W2C_File[W2C_LOC_FILE],
01267 &tokens);
01268 }
01269
01270 Close_W2c_Output_File(W2C_LOC_FILE);
01271 Close_W2c_Output_File(W2C_DOTH_FILE);
01272 Close_W2c_Output_File(W2C_DOTC_FILE);
01273
01274
01275
01276
01277 W2C_Outfile_Initialized = FALSE;
01278 W2C_Fini();
01279
01280 if (W2C_Prompf_Emission && loc_fname != NULL)
01281 {
01282
01283
01284 Move_Locations_To_Anl_File(loc_fname);
01285 }
01286 }
01287
01288
01289 void
01290 W2C_Cleanup(void)
01291 {
01292
01293
01294 Close_W2c_Output_File(W2C_LOC_FILE);
01295 Close_W2c_Output_File(W2C_DOTH_FILE);
01296 Close_W2c_Output_File(W2C_DOTC_FILE);
01297 if (W2C_File_Name[W2C_LOC_FILE] != NULL)
01298 unlink(W2C_File_Name[W2C_LOC_FILE]);
01299 }