Reid Spencer | cbabe7f | 2004-08-19 21:17:53 +0000 | [diff] [blame] | 1 | //===- Configuration.cpp - Configuration Data Mgmt --------------*- C++ -*-===// |
Reid Spencer | 2594c9a | 2004-08-13 20:21:22 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Reid Spencer and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the parsing of configuration files for the LLVM Compiler |
| 11 | // Driver (llvmc). |
| 12 | // |
| 13 | //===------------------------------------------------------------------------=== |
| 14 | |
Reid Spencer | f51a87c | 2004-08-19 21:52:49 +0000 | [diff] [blame] | 15 | #include "Configuration.h" |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 16 | #include "ConfigLexer.h" |
Reid Spencer | 2594c9a | 2004-08-13 20:21:22 +0000 | [diff] [blame] | 17 | #include "CompilerDriver.h" |
Reid Spencer | b38e405 | 2004-08-20 09:24:07 +0000 | [diff] [blame] | 18 | #include "Config/config.h" |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 19 | #include "Support/CommandLine.h" |
Reid Spencer | 2594c9a | 2004-08-13 20:21:22 +0000 | [diff] [blame] | 20 | #include "Support/StringExtras.h" |
| 21 | #include <iostream> |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 22 | #include <fstream> |
Reid Spencer | 2594c9a | 2004-08-13 20:21:22 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | |
Reid Spencer | b38e405 | 2004-08-20 09:24:07 +0000 | [diff] [blame] | 26 | namespace sys { |
| 27 | // From CompilerDriver.cpp (for now) |
Reid Spencer | f384db3 | 2004-08-24 14:03:23 +0000 | [diff] [blame] | 28 | extern bool FileIsReadable(const std::string& fname); |
Reid Spencer | b38e405 | 2004-08-20 09:24:07 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 31 | namespace llvm { |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 32 | ConfigLexerInfo ConfigLexerState; |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 33 | InputProvider* ConfigLexerInput = 0; |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 34 | |
| 35 | InputProvider::~InputProvider() {} |
| 36 | void InputProvider::error(const std::string& msg) { |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 37 | std::cerr << name << ":" << ConfigLexerState.lineNum << ": Error: " << |
| 38 | msg << "\n"; |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 39 | errCount++; |
| 40 | } |
| 41 | |
| 42 | void InputProvider::checkErrors() { |
| 43 | if (errCount > 0) { |
| 44 | std::cerr << name << " had " << errCount << " errors. Terminating.\n"; |
| 45 | exit(errCount); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | } |
| 50 | |
Reid Spencer | 2594c9a | 2004-08-13 20:21:22 +0000 | [diff] [blame] | 51 | namespace { |
| 52 | |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 53 | class FileInputProvider : public InputProvider { |
| 54 | public: |
| 55 | FileInputProvider(const std::string & fname) |
| 56 | : InputProvider(fname) |
| 57 | , F(fname.c_str()) { |
| 58 | ConfigLexerInput = this; |
| 59 | } |
| 60 | virtual ~FileInputProvider() { F.close(); ConfigLexerInput = 0; } |
| 61 | virtual unsigned read(char *buffer, unsigned max_size) { |
| 62 | if (F.good()) { |
| 63 | F.read(buffer,max_size); |
| 64 | if ( F.gcount() ) return F.gcount() - 1; |
| 65 | } |
| 66 | return 0; |
| 67 | } |
Reid Spencer | 2594c9a | 2004-08-13 20:21:22 +0000 | [diff] [blame] | 68 | |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 69 | bool okay() { return F.good(); } |
| 70 | private: |
| 71 | std::ifstream F; |
| 72 | }; |
Reid Spencer | 2594c9a | 2004-08-13 20:21:22 +0000 | [diff] [blame] | 73 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 74 | cl::opt<bool> DumpTokens("dump-tokens", cl::Optional, cl::Hidden, cl::init(false), |
| 75 | cl::desc("Dump lexical tokens (debug use only).")); |
| 76 | |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 77 | struct Parser |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 78 | { |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 79 | Parser() { |
| 80 | token = EOFTOK; |
| 81 | provider = 0; |
| 82 | confDat = 0; |
| 83 | ConfigLexerState.lineNum = 1; |
| 84 | ConfigLexerState.in_value = false; |
| 85 | ConfigLexerState.StringVal.clear(); |
| 86 | ConfigLexerState.IntegerVal = 0; |
| 87 | }; |
| 88 | |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 89 | ConfigLexerTokens token; |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 90 | InputProvider* provider; |
| 91 | CompilerDriver::ConfigData* confDat; |
Reid Spencer | 2594c9a | 2004-08-13 20:21:22 +0000 | [diff] [blame] | 92 | |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame^] | 93 | inline int next() { |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 94 | token = Configlex(); |
| 95 | if (DumpTokens) |
| 96 | std::cerr << token << "\n"; |
| 97 | return token; |
| 98 | } |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 99 | |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame^] | 100 | inline bool next_is_real() { |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 101 | next(); |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 102 | return (token != EOLTOK) && (token != ERRORTOK) && (token != 0); |
Reid Spencer | 2594c9a | 2004-08-13 20:21:22 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame^] | 105 | inline void eatLineRemnant() { |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 106 | while (next_is_real()) ; |
Reid Spencer | 2594c9a | 2004-08-13 20:21:22 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 109 | void error(const std::string& msg, bool skip = true) { |
| 110 | provider->error(msg); |
| 111 | if (skip) |
| 112 | eatLineRemnant(); |
| 113 | } |
Reid Spencer | 2594c9a | 2004-08-13 20:21:22 +0000 | [diff] [blame] | 114 | |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 115 | std::string parseName() { |
| 116 | std::string result; |
| 117 | if (next() == EQUALS) { |
| 118 | while (next_is_real()) { |
| 119 | switch (token ) { |
| 120 | case STRING : |
| 121 | case OPTION : |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 122 | result += ConfigLexerState.StringVal + " "; |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 123 | break; |
| 124 | default: |
| 125 | error("Invalid name"); |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | if (result.empty()) |
| 130 | error("Name exepected"); |
| 131 | else |
| 132 | result.erase(result.size()-1,1); |
| 133 | } else |
| 134 | error("= expected"); |
| 135 | return result; |
| 136 | } |
Reid Spencer | 2594c9a | 2004-08-13 20:21:22 +0000 | [diff] [blame] | 137 | |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 138 | bool parseBoolean() { |
| 139 | bool result = true; |
| 140 | if (next() == EQUALS) { |
| 141 | if (next() == FALSETOK) { |
| 142 | result = false; |
| 143 | } else if (token != TRUETOK) { |
| 144 | error("Expecting boolean value"); |
| 145 | return false; |
| 146 | } |
| 147 | if (next() != EOLTOK && token != 0) { |
| 148 | error("Extraneous tokens after boolean"); |
| 149 | } |
| 150 | } |
| 151 | else |
| 152 | error("Expecting '='"); |
| 153 | return result; |
| 154 | } |
Reid Spencer | 2594c9a | 2004-08-13 20:21:22 +0000 | [diff] [blame] | 155 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 156 | bool parseSubstitution(CompilerDriver::StringVector& optList) { |
| 157 | switch (token) { |
Reid Spencer | f384db3 | 2004-08-24 14:03:23 +0000 | [diff] [blame] | 158 | case ARGS_SUBST: optList.push_back("%args%"); break; |
Reid Spencer | 53aa793 | 2004-08-20 22:53:11 +0000 | [diff] [blame] | 159 | case IN_SUBST: optList.push_back("%in%"); break; |
| 160 | case OUT_SUBST: optList.push_back("%out%"); break; |
| 161 | case TIME_SUBST: optList.push_back("%time%"); break; |
| 162 | case STATS_SUBST: optList.push_back("%stats%"); break; |
| 163 | case OPT_SUBST: optList.push_back("%opt%"); break; |
| 164 | case TARGET_SUBST: optList.push_back("%target%"); break; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame^] | 165 | case FORCE_SUBST: optList.push_back("%force%"); break; |
| 166 | case VERBOSE_SUBST: optList.push_back("%verbose%"); break; |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 167 | default: |
| 168 | return false; |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 169 | } |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 170 | return true; |
| 171 | } |
| 172 | |
| 173 | void parseOptionList(CompilerDriver::StringVector& optList ) { |
| 174 | if (next() == EQUALS) { |
| 175 | while (next_is_real()) { |
| 176 | if (token == STRING || token == OPTION) |
| 177 | optList.push_back(ConfigLexerState.StringVal); |
| 178 | else if (!parseSubstitution(optList)) { |
| 179 | error("Expecting a program argument or substitution", false); |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | } else |
| 184 | error("Expecting '='"); |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Reid Spencer | 59a745a | 2004-08-22 18:03:25 +0000 | [diff] [blame] | 187 | void parseVersion() { |
| 188 | if (next() == EQUALS) { |
| 189 | while (next_is_real()) { |
| 190 | if (token == STRING || token == OPTION) |
| 191 | confDat->version = ConfigLexerState.StringVal; |
| 192 | else |
| 193 | error("Expecting a version string"); |
| 194 | } |
| 195 | } else |
| 196 | error("Expecting '='"); |
| 197 | } |
| 198 | |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 199 | void parseLang() { |
| 200 | switch (next() ) { |
| 201 | case NAME: |
| 202 | confDat->langName = parseName(); |
| 203 | break; |
| 204 | case OPT1: |
| 205 | parseOptionList(confDat->opts[CompilerDriver::OPT_FAST_COMPILE]); |
| 206 | break; |
| 207 | case OPT2: |
| 208 | parseOptionList(confDat->opts[CompilerDriver::OPT_SIMPLE]); |
| 209 | break; |
| 210 | case OPT3: |
| 211 | parseOptionList(confDat->opts[CompilerDriver::OPT_AGGRESSIVE]); |
| 212 | break; |
| 213 | case OPT4: |
| 214 | parseOptionList(confDat->opts[CompilerDriver::OPT_LINK_TIME]); |
| 215 | break; |
| 216 | case OPT5: |
| 217 | parseOptionList( |
| 218 | confDat->opts[CompilerDriver::OPT_AGGRESSIVE_LINK_TIME]); |
| 219 | break; |
| 220 | default: |
| 221 | error("Expecting 'name' or 'optN' after 'lang.'"); |
| 222 | break; |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 223 | } |
| 224 | } |
Reid Spencer | 2594c9a | 2004-08-13 20:21:22 +0000 | [diff] [blame] | 225 | |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 226 | void parseCommand(CompilerDriver::Action& action) { |
| 227 | if (next() == EQUALS) { |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 228 | if (next() == EOLTOK) { |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 229 | // no value (valid) |
| 230 | action.program.clear(); |
| 231 | action.args.clear(); |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 232 | } else { |
| 233 | if (token == STRING || token == OPTION) { |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame^] | 234 | action.program.set_file(ConfigLexerState.StringVal); |
Reid Spencer | 2594c9a | 2004-08-13 20:21:22 +0000 | [diff] [blame] | 235 | } else { |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 236 | error("Expecting a program name"); |
| 237 | } |
| 238 | while (next_is_real()) { |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 239 | if (token == STRING || token == OPTION) { |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 240 | action.args.push_back(ConfigLexerState.StringVal); |
| 241 | } else if (!parseSubstitution(action.args)) { |
| 242 | error("Expecting a program argument or substitution", false); |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 243 | break; |
| 244 | } |
Reid Spencer | 2594c9a | 2004-08-13 20:21:22 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 247 | } |
Reid Spencer | 2594c9a | 2004-08-13 20:21:22 +0000 | [diff] [blame] | 248 | } |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 249 | |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 250 | void parsePreprocessor() { |
| 251 | switch (next()) { |
| 252 | case COMMAND: |
| 253 | parseCommand(confDat->PreProcessor); |
| 254 | break; |
| 255 | case REQUIRED: |
| 256 | if (parseBoolean()) |
| 257 | confDat->PreProcessor.set(CompilerDriver::REQUIRED_FLAG); |
| 258 | else |
| 259 | confDat->PreProcessor.clear(CompilerDriver::REQUIRED_FLAG); |
| 260 | break; |
| 261 | default: |
Reid Spencer | f384db3 | 2004-08-24 14:03:23 +0000 | [diff] [blame] | 262 | error("Expecting 'command' or 'required' but found '" + |
| 263 | ConfigLexerState.StringVal); |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 264 | break; |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 265 | } |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Reid Spencer | f384db3 | 2004-08-24 14:03:23 +0000 | [diff] [blame] | 268 | bool parseOutputFlag() { |
| 269 | if (next() == EQUALS) { |
| 270 | if (next() == ASSEMBLY) { |
| 271 | return true; |
| 272 | } else if (token == BYTECODE) { |
| 273 | return false; |
| 274 | } else { |
| 275 | error("Expecting output type value"); |
| 276 | return false; |
| 277 | } |
| 278 | if (next() != EOLTOK && token != 0) { |
| 279 | error("Extraneous tokens after output value"); |
| 280 | } |
| 281 | } |
| 282 | else |
| 283 | error("Expecting '='"); |
| 284 | return false; |
| 285 | } |
| 286 | |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 287 | void parseTranslator() { |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 288 | switch (next()) { |
| 289 | case COMMAND: |
| 290 | parseCommand(confDat->Translator); |
| 291 | break; |
| 292 | case REQUIRED: |
| 293 | if (parseBoolean()) |
| 294 | confDat->Translator.set(CompilerDriver::REQUIRED_FLAG); |
| 295 | else |
| 296 | confDat->Translator.clear(CompilerDriver::REQUIRED_FLAG); |
| 297 | break; |
| 298 | case PREPROCESSES: |
| 299 | if (parseBoolean()) |
| 300 | confDat->Translator.set(CompilerDriver::PREPROCESSES_FLAG); |
| 301 | else |
| 302 | confDat->Translator.clear(CompilerDriver::PREPROCESSES_FLAG); |
| 303 | break; |
Reid Spencer | f384db3 | 2004-08-24 14:03:23 +0000 | [diff] [blame] | 304 | case OUTPUT: |
| 305 | if (parseOutputFlag()) |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 306 | confDat->Translator.set(CompilerDriver::OUTPUT_IS_ASM_FLAG); |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 307 | else |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 308 | confDat->Translator.clear(CompilerDriver::OUTPUT_IS_ASM_FLAG); |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 309 | break; |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 310 | |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 311 | default: |
Reid Spencer | f384db3 | 2004-08-24 14:03:23 +0000 | [diff] [blame] | 312 | error("Expecting 'command', 'required', 'preprocesses', or " |
| 313 | "'output' but found '" + ConfigLexerState.StringVal + |
| 314 | "' instead"); |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 315 | break; |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 316 | } |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | void parseOptimizer() { |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 320 | switch (next()) { |
| 321 | case COMMAND: |
| 322 | parseCommand(confDat->Optimizer); |
| 323 | break; |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 324 | case PREPROCESSES: |
| 325 | if (parseBoolean()) |
| 326 | confDat->Optimizer.set(CompilerDriver::PREPROCESSES_FLAG); |
| 327 | else |
| 328 | confDat->Optimizer.clear(CompilerDriver::PREPROCESSES_FLAG); |
| 329 | break; |
| 330 | case TRANSLATES: |
| 331 | if (parseBoolean()) |
| 332 | confDat->Optimizer.set(CompilerDriver::TRANSLATES_FLAG); |
| 333 | else |
| 334 | confDat->Optimizer.clear(CompilerDriver::TRANSLATES_FLAG); |
| 335 | break; |
Reid Spencer | f384db3 | 2004-08-24 14:03:23 +0000 | [diff] [blame] | 336 | case REQUIRED: |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 337 | if (parseBoolean()) |
Reid Spencer | f384db3 | 2004-08-24 14:03:23 +0000 | [diff] [blame] | 338 | confDat->Optimizer.set(CompilerDriver::REQUIRED_FLAG); |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 339 | else |
Reid Spencer | f384db3 | 2004-08-24 14:03:23 +0000 | [diff] [blame] | 340 | confDat->Optimizer.clear(CompilerDriver::REQUIRED_FLAG); |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 341 | break; |
Reid Spencer | f384db3 | 2004-08-24 14:03:23 +0000 | [diff] [blame] | 342 | case OUTPUT: |
| 343 | if (parseOutputFlag()) |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 344 | confDat->Translator.set(CompilerDriver::OUTPUT_IS_ASM_FLAG); |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 345 | else |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 346 | confDat->Translator.clear(CompilerDriver::OUTPUT_IS_ASM_FLAG); |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 347 | break; |
| 348 | default: |
Reid Spencer | f384db3 | 2004-08-24 14:03:23 +0000 | [diff] [blame] | 349 | error(std::string("Expecting 'command', 'preprocesses', ") + |
| 350 | "'translates' or 'output' but found '" + |
| 351 | ConfigLexerState.StringVal + "' instead"); |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 352 | break; |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 353 | } |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | void parseAssembler() { |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 357 | switch(next()) { |
| 358 | case COMMAND: |
| 359 | parseCommand(confDat->Assembler); |
| 360 | break; |
| 361 | default: |
| 362 | error("Expecting 'command'"); |
| 363 | break; |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 364 | } |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void parseLinker() { |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 368 | switch(next()) { |
Reid Spencer | f384db3 | 2004-08-24 14:03:23 +0000 | [diff] [blame] | 369 | case LIBS: |
| 370 | break; //FIXME |
| 371 | case LIBPATHS: |
| 372 | break; //FIXME |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 373 | default: |
Reid Spencer | f384db3 | 2004-08-24 14:03:23 +0000 | [diff] [blame] | 374 | error("Expecting 'libs' or 'libpaths'"); |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 375 | break; |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 376 | } |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | void parseAssignment() { |
| 380 | switch (token) { |
Reid Spencer | 59a745a | 2004-08-22 18:03:25 +0000 | [diff] [blame] | 381 | case VERSION: parseVersion(); break; |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 382 | case LANG: parseLang(); break; |
| 383 | case PREPROCESSOR: parsePreprocessor(); break; |
| 384 | case TRANSLATOR: parseTranslator(); break; |
| 385 | case OPTIMIZER: parseOptimizer(); break; |
| 386 | case ASSEMBLER: parseAssembler(); break; |
| 387 | case LINKER: parseLinker(); break; |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 388 | case EOLTOK: break; // just ignore |
| 389 | case ERRORTOK: |
| 390 | default: |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 391 | error("Invalid top level configuration item"); |
| 392 | break; |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
| 396 | void parseFile() { |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 397 | while ( next() != EOFTOK ) { |
| 398 | if (token == ERRORTOK) |
| 399 | error("Invalid token"); |
| 400 | else if (token != EOLTOK) |
| 401 | parseAssignment(); |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 402 | } |
| 403 | provider->checkErrors(); |
| 404 | } |
| 405 | }; |
| 406 | |
| 407 | void |
| 408 | ParseConfigData(InputProvider& provider, CompilerDriver::ConfigData& confDat) { |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 409 | Parser p; |
| 410 | p.token = EOFTOK; |
| 411 | p.provider = &provider; |
| 412 | p.confDat = &confDat; |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 413 | p.parseFile(); |
Reid Spencer | 2594c9a | 2004-08-13 20:21:22 +0000 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
| 417 | CompilerDriver::ConfigData* |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 418 | LLVMC_ConfigDataProvider::ReadConfigData(const std::string& ftype) { |
| 419 | CompilerDriver::ConfigData* result = 0; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame^] | 420 | sys::Path confFile; |
| 421 | if (configDir.is_empty()) { |
Reid Spencer | b38e405 | 2004-08-20 09:24:07 +0000 | [diff] [blame] | 422 | // Try the environment variable |
| 423 | const char* conf = getenv("LLVM_CONFIG_DIR"); |
| 424 | if (conf) { |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame^] | 425 | confFile.set_directory(conf); |
| 426 | confFile.append_file(ftype); |
| 427 | if (!confFile.readable()) |
Reid Spencer | b38e405 | 2004-08-20 09:24:07 +0000 | [diff] [blame] | 428 | throw "Configuration file for '" + ftype + "' is not available."; |
| 429 | } else { |
| 430 | // Try the user's home directory |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame^] | 431 | confFile = sys::Path::GetUserHomeDirectory(); |
| 432 | if (!confFile.is_empty()) { |
| 433 | confFile.append_directory(".llvm"); |
| 434 | confFile.append_directory("etc"); |
| 435 | confFile.append_file(ftype); |
| 436 | if (!confFile.readable()) |
| 437 | confFile.clear(); |
| 438 | } |
| 439 | if (!confFile.is_empty()) { |
| 440 | // Okay, try the LLVM installation directory |
| 441 | confFile = sys::Path::GetLLVMConfigDir(); |
| 442 | confFile.append_file(ftype); |
| 443 | if (!confFile.readable()) { |
| 444 | // Okay, try the "standard" place |
| 445 | confFile = sys::Path::GetLLVMDefaultConfigDir(); |
| 446 | confFile.append_file(ftype); |
| 447 | if (!confFile.readable()) { |
| 448 | throw "Configuration file for '" + ftype + "' is not available."; |
Reid Spencer | b38e405 | 2004-08-20 09:24:07 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | } |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 452 | } |
| 453 | } else { |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame^] | 454 | confFile = configDir; |
| 455 | confFile.append_file(ftype); |
| 456 | if (!confFile.readable()) |
Reid Spencer | b38e405 | 2004-08-20 09:24:07 +0000 | [diff] [blame] | 457 | throw "Configuration file for '" + ftype + "' is not available."; |
Reid Spencer | 2594c9a | 2004-08-13 20:21:22 +0000 | [diff] [blame] | 458 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame^] | 459 | FileInputProvider fip( confFile.get() ); |
Reid Spencer | b38e405 | 2004-08-20 09:24:07 +0000 | [diff] [blame] | 460 | if (!fip.okay()) { |
| 461 | throw "Configuration file for '" + ftype + "' is not available."; |
| 462 | } |
| 463 | result = new CompilerDriver::ConfigData(); |
| 464 | ParseConfigData(fip,*result); |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 465 | return result; |
Reid Spencer | 2594c9a | 2004-08-13 20:21:22 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Reid Spencer | 2594c9a | 2004-08-13 20:21:22 +0000 | [diff] [blame] | 468 | LLVMC_ConfigDataProvider::~LLVMC_ConfigDataProvider() |
| 469 | { |
| 470 | ConfigDataMap::iterator cIt = Configurations.begin(); |
| 471 | while (cIt != Configurations.end()) { |
| 472 | CompilerDriver::ConfigData* cd = cIt->second; |
| 473 | ++cIt; |
| 474 | delete cd; |
| 475 | } |
| 476 | Configurations.clear(); |
| 477 | } |
| 478 | |
| 479 | CompilerDriver::ConfigData* |
| 480 | LLVMC_ConfigDataProvider::ProvideConfigData(const std::string& filetype) { |
| 481 | CompilerDriver::ConfigData* result = 0; |
| 482 | if (!Configurations.empty()) { |
| 483 | ConfigDataMap::iterator cIt = Configurations.find(filetype); |
| 484 | if ( cIt != Configurations.end() ) { |
| 485 | // We found one in the case, return it. |
| 486 | result = cIt->second; |
| 487 | } |
| 488 | } |
| 489 | if (result == 0) { |
| 490 | // The configuration data doesn't exist, we have to go read it. |
| 491 | result = ReadConfigData(filetype); |
| 492 | // If we got one, cache it |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame^] | 493 | if (result != 0) |
Reid Spencer | 2594c9a | 2004-08-13 20:21:22 +0000 | [diff] [blame] | 494 | Configurations.insert(std::make_pair(filetype,result)); |
| 495 | } |
| 496 | return result; // Might return 0 |
| 497 | } |
| 498 | |
| 499 | // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab |