00001 /* 00002 * Copyright 2005 PathScale, Inc. All Rights Reserved. 00003 */ 00004 00005 /* 00006 00007 Copyright (C) 2000, 2001 Silicon Graphics, Inc. All Rights Reserved. 00008 00009 This program is free software; you can redistribute it and/or modify it 00010 under the terms of version 2 of the GNU General Public License as 00011 published by the Free Software Foundation. 00012 00013 This program is distributed in the hope that it would be useful, but 00014 WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00016 00017 Further, this software is distributed without any warranty that it is 00018 free of the rightful claim of any third person regarding infringement 00019 or the like. Any license provided herein, whether implied or 00020 otherwise, applies only to this software file. Patent licenses, if 00021 any, provided herein do not apply to combinations of this program with 00022 other software, or any other product whatsoever. 00023 00024 You should have received a copy of the GNU General Public License along 00025 with this program; if not, write the Free Software Foundation, Inc., 59 00026 Temple Place - Suite 330, Boston MA 02111-1307, USA. 00027 00028 Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pky, 00029 Mountain View, CA 94043, or: 00030 00031 http://www.sgi.com 00032 00033 For further information regarding this notice, see: 00034 00035 http://oss.sgi.com/projects/GenInfo/NoticeExplan 00036 00037 */ 00038 00039 00040 //-*-c++-*- 00041 //============================================================================ 00042 // 00043 // Module: options_stack.cxx 00044 // $Revision: 1.1.1.1 $ 00045 // $Date: 2005/10/21 19:00:00 $ 00046 // $Author: marcel $ 00047 // $Source: /proj/osprey/CVS/open64/osprey1.0/common/util/options_stack.cxx,v $ 00048 // 00049 // Revision history: 00050 // 23-SEP-97 dahl - Original Version 00051 // 00052 // Description: 00053 // Allow the user to set command line options via pragmas in the source. 00054 // 00055 //============================================================================ 00056 00057 #include "defs.h" // INT32 00058 #include "region_util.h" // debug flags 00059 #include "tracing.h" // Get_Trace 00060 #include "config.h" // Opt_Level 00061 #include "phase.h" // BE_PHASES 00062 #include "driver_util.h" // Process_Command_Line 00063 #include "cxx_memory.h" // CXX_NEW_ARRAY 00064 #include "config_wopt.h" // WOPT_Enable_LFTR2 00065 #include "flags.h" // Common_Option_Group 00066 #include "options_stack.h" // options stack 00067 00068 #ifdef SHARED_BUILD 00069 // defined in be, outside of be.so 00070 #pragma weak Process_Command_Line 00071 #endif 00072 00073 //============================================================================ 00074 // Push_Current_Options 00075 // allocates stack slot and then copies global's values into that space 00076 //============================================================================ 00077 void 00078 OPTIONS_STACK::Push_Current_Options(void) 00079 { 00080 // allocate memory_pool, need guess on size 00081 char *tmp = CXX_NEW_ARRAY(char, OPTIONS_SIZE, &MEM_src_nz_pool); 00082 00083 // save all options to temp location 00084 Save_or_restore_options(tmp, OPTIONS_SIZE, TRUE); 00085 00086 // push current options onto stack 00087 _options_stack.Push(tmp); 00088 00089 Is_Trace(Trace(), (TFile,"OPTIONS_STACK::Push_Current_Options, size=%d\n", 00090 _options_stack.Elements())); 00091 } 00092 00093 //============================================================================ 00094 // Pop_Current_Options 00095 // copies stack contents into global values, deallocates stack slot 00096 //============================================================================ 00097 void 00098 OPTIONS_STACK::Pop_Current_Options(void) 00099 { 00100 // delete old options memory 00101 char *tmp = _options_stack.Top(); 00102 _options_stack.Pop(); 00103 CXX_DELETE_ARRAY(tmp, &MEM_src_nz_pool); 00104 00105 // pointer to space where old options are 00106 tmp = _options_stack.Top(); 00107 00108 // copy options back from stack 00109 Save_or_restore_options(tmp, OPTIONS_SIZE, FALSE); 00110 00111 Is_Trace(Trace(), (TFile,"OPTIONS_STACK::Pop_Current_Options, size=%d\n", 00112 _options_stack.Elements())); 00113 } 00114 00115 // ==================================================================== 00116 // str2argv (private) 00117 // convert options string to argv format, returns argc 00118 // ==================================================================== 00119 INT32 00120 OPTIONS_STACK::str2argv(char *str, char ***argv, MEM_POOL *pool) 00121 { 00122 // first count number of options, add 2 for command and filename 00123 INT32 argc = 2; 00124 INT32 i; 00125 for (i=0; i<strlen(str); i++) 00126 if (str[i] == '-') 00127 argc++; 00128 00129 // allocate array and fill in command and filename 00130 *argv = CXX_NEW_ARRAY(char *, argc, pool); 00131 (*argv)[0] = const_cast<char*>(""); // command 00132 (*argv)[argc-1] = const_cast<char*>(""); // filename 00133 00134 // fill in options 00135 INT32 pos = 0; 00136 for (i=1; i<argc-1; i++) { 00137 // eat up white space 00138 while (str[pos] == ' ') 00139 pos++; 00140 // first find length of option 00141 INT32 len; 00142 for (len=0; str[pos+len] != ' ' && str[pos+len] != '\0'; len++) 00143 ; // empty body 00144 // allocate 00145 (*argv)[i] = CXX_NEW_ARRAY(char, len+1, pool); // the 1 is for the '\0' 00146 // copy and NULL terminate 00147 strncpy((*argv)[i], &str[pos], len); 00148 (*argv)[i][len] = '\0'; 00149 // point to beginning of next option 00150 pos += len + 1; // the 1 is to skip the required space 00151 } 00152 00153 #ifdef Is_True_On 00154 if (Trace()) { 00155 fprintf(TFile, "OPTIONS_STACK::str2argv, argc=%d\n", argc); 00156 for (i=0; i<argc; i++) 00157 fprintf(TFile, " %d: %s\n", i, (*argv)[i]); 00158 } 00159 #endif 00160 00161 return argc; 00162 } 00163 00164 // ==================================================================== 00165 // Process_Pragma_Options 00166 // process any region or PU level options pragmas 00167 // ==================================================================== 00168 void 00169 OPTIONS_STACK::Process_Pragma_Options(char *options_string) 00170 { 00171 if (options_string == NULL) // no options pragma in this PU or region 00172 return; 00173 00174 Is_Trace(Trace(), 00175 (TFile,"OPTIONS_STACK::Process_Pragma_Options, options = %s\n", 00176 options_string)); 00177 00178 // malloc memory and save old options on stack 00179 Push_Current_Options(); 00180 00181 // set variables based on options_string 00182 MEM_POOL Options_pool; 00183 MEM_POOL_Initialize(&Options_pool, "Options_pool", FALSE); 00184 MEM_POOL_Push(&Options_pool); 00185 char **argv; 00186 // create an argv from the options string 00187 INT32 argc = str2argv(options_string, &argv, &Options_pool); 00188 // set the various options based on options string 00189 Process_Command_Line(argc, argv); 00190 MEM_POOL_Pop(&Options_pool); 00191 00192 #ifdef Is_True_On 00193 if (Trace()) { 00194 OPTION_GROUP *og = Get_Command_Line_Group(Common_Option_Groups, "WOPT"); 00195 Is_True(og != NULL, ("OPTIONS_STACK::Process_Pragma_Options, " 00196 "could not find WOPT group")); 00197 Trace_Command_Line_Group(TFile, og); // only prints differences 00198 } 00199 #endif 00200 }
1.5.6