Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1 | #include "libslang.h" |
| 2 | |
| 3 | #include <assert.h> |
| 4 | #include <getopt.h> |
| 5 | |
| 6 | #include <cstring> |
| 7 | #include <cstdlib> |
| 8 | #include <iomanip> |
| 9 | #include <iostream> |
| 10 | |
| 11 | using namespace std; |
| 12 | |
| 13 | #define ERR_NO_INPUT_FILE "no input file" |
| 14 | |
| 15 | #define WARN(x) cerr << "warning: " WARN_ ## x << endl |
| 16 | #define WARN1(x, v1) cerr << "warning: " WARN_ ## x(v1) << endl |
| 17 | #define WARN2(x, v1, v2) cerr << "warning: " WARN_ ## x(v1, v2) << endl |
| 18 | |
| 19 | #define WARN_MULTIPLE_INPUT_FILES "multiple input files is not supported currently, only first input file will be compiled" |
| 20 | |
| 21 | #define WARN_UNKNOWN_CPU(v1) "the given CPU " << (v1) << " cannot be recognized, but we'll force passing it to Slang compiler" |
| 22 | |
| 23 | #define WARN_MISMATCH_CPU_TARGET_ARCH(v1, v2) \ |
| 24 | "CPU (target: " << (v1) << ") you selected doesn't match the target of enable features you specified or the triple string you given (" << (v2) << ")" |
| 25 | #define WARN_MISMATCH_FEATURE_TARGET_ARCH(v1, v2) \ |
| 26 | "Feature (target: " << (v1) << ") you selected doesn't match the target of CPU you specified or the triple string you given (" << (v2) << ")" |
| 27 | |
| 28 | #define DEFAULT_OUTPUT_FILENAME "a.out" |
| 29 | |
| 30 | /* List of all support target, will look like "ArchARM" */ |
| 31 | #define MK_TARGET_ARCH(target) Arch ## target |
| 32 | typedef enum { |
| 33 | ArchNone, |
| 34 | #define DEF_SUPPORT_TARGET(target, name, default_triple) \ |
| 35 | MK_TARGET_ARCH(target), |
| 36 | # include "target.inc" |
| 37 | |
| 38 | MaxTargetArch |
| 39 | } TargetArchEnum; |
| 40 | |
| 41 | typedef struct { |
| 42 | TargetArchEnum Arch; |
| 43 | const char* Name; |
| 44 | const char* DefaultTriple; |
| 45 | } TargetArch; |
| 46 | |
| 47 | static const TargetArch TargetArchTable[] = { |
| 48 | { ArchNone, "none", "unknown-unknown-linux" }, |
| 49 | #define DEF_SUPPORT_TARGET(target, name, default_triple) \ |
| 50 | { MK_TARGET_ARCH(target), name, default_triple }, |
| 51 | # include "target.inc" |
| 52 | }; |
| 53 | |
| 54 | #if defined(__arm__) |
| 55 | # define HOST_ARCH MK_TARGET_ARCH(X86) |
| 56 | #elif defined(__i386__) |
| 57 | # define HOST_ARCH MK_TARGET_ARCH(ARM) |
| 58 | #elif defined(__x86_64__) |
| 59 | # define HOST_ARCH MK_TARGET_ARCH(X64) |
| 60 | #else |
| 61 | # error "We can not find default triple string for your host machine, please define it by yourself via option '--triple' or '-t'" |
| 62 | #endif |
| 63 | |
| 64 | #define DEFAULT_TARGET_TRIPLE_STRING TargetArchTable[HOST_ARCH].DefaultTriple |
| 65 | |
| 66 | /* Lists of all target features, will look like "{Target}FeatureNEON" */ |
| 67 | #define MK_TARGET_FEATURE(target, id) target ## id |
| 68 | typedef enum { |
| 69 | FeatureNone = 0, |
| 70 | #define DEF_TARGET_FEATURE(target, id, key, description) \ |
| 71 | MK_TARGET_FEATURE(target, id), |
| 72 | #define HOOK_TARGET_FIRST_FEATURE(target, id, key, description) \ |
| 73 | target ## FeatureStart, \ |
| 74 | MK_TARGET_FEATURE(target, id) = target ## FeatureStart, |
| 75 | # include "target.inc" |
| 76 | |
| 77 | MaxTargetFeature |
| 78 | } TargetFeatureEnum; |
| 79 | |
| 80 | /* Feature as bits using in {Target}TargetCPU, will look like "X{Target}FeatureNEON" */ |
| 81 | #define MK_TARGET_FEATURE_BIT(target, id) X ## target ## id |
| 82 | typedef enum { |
| 83 | XFeatureNone = 0, |
| 84 | #define DEF_TARGET_FEATURE(target, id, key, description) \ |
| 85 | MK_TARGET_FEATURE_BIT(target, id) = 1 << (MK_TARGET_FEATURE(target, id) - target ## FeatureStart), |
| 86 | # include "target.inc" |
| 87 | |
| 88 | XMaxTargetFeature |
| 89 | } TargetFeatureBit; |
| 90 | |
| 91 | typedef struct { |
| 92 | TargetArchEnum Arch; |
| 93 | TargetFeatureEnum Key; |
| 94 | TargetFeatureBit Bit; |
| 95 | const char* Name; |
| 96 | const char* Desc; |
| 97 | } TargetFeature; |
| 98 | |
| 99 | /* Should be 1-1 mapping with TargetFeatureEnum */ |
| 100 | static const TargetFeature TargetFeatureTable[] = { |
| 101 | { ArchNone, FeatureNone, XFeatureNone, "none", "Empty feature" }, /* FeatureNone */ |
| 102 | #define DEF_TARGET_FEATURE(target, id, key, description) \ |
| 103 | { MK_TARGET_ARCH(target), MK_TARGET_FEATURE(target, id), MK_TARGET_FEATURE_BIT(target, id), key, description }, |
| 104 | # include "target.inc" |
| 105 | }; |
| 106 | |
| 107 | typedef struct { |
| 108 | TargetArchEnum Arch; |
| 109 | const char* Name; |
| 110 | const char* Desc; |
| 111 | unsigned int FeatureEnabled; |
| 112 | } TargetCPU; |
| 113 | |
| 114 | /* Sorted by CPU name such that we can call bsearch() to quickly retain the CPU entry corresponding to the name */ |
| 115 | #define E(feature) MK_TARGET_FEATURE_BIT(ARM, feature) |
| 116 | static const TargetCPU TargetCPUTable[] = { |
| 117 | { MK_TARGET_ARCH(ARM), "arm1020e", "Select the arm1020e processor", E(ArchV5TE) }, |
| 118 | { MK_TARGET_ARCH(ARM), "arm1020t", "Select the arm1020t processor", E(ArchV5T) }, |
| 119 | { MK_TARGET_ARCH(ARM), "arm1022e", "Select the arm1022e processor", E(ArchV5TE) }, |
| 120 | { MK_TARGET_ARCH(ARM), "arm10e", "Select the arm10e processor", E(ArchV5TE) }, |
| 121 | { MK_TARGET_ARCH(ARM), "arm10tdmi", "Select the arm10tdmi processor", E(ArchV5T) }, |
| 122 | { MK_TARGET_ARCH(ARM), "arm1136j-s", "Select the arm1136j-s processor", E(ArchV6) }, |
| 123 | { MK_TARGET_ARCH(ARM), "arm1136jf-s", "Select the arm1136jf-s processor", E(ArchV6) | E(FeatureVFP2) }, |
| 124 | { MK_TARGET_ARCH(ARM), "arm1156t2-s", "Select the arm1156t2-s processor", E(ArchV6T2) | E(FeatureThumb2) }, |
| 125 | { MK_TARGET_ARCH(ARM), "arm1156t2f-s", "Select the arm1156t2f-s processor", E(ArchV6T2) | E(FeatureThumb2) | E(FeatureVFP2) }, |
| 126 | { MK_TARGET_ARCH(ARM), "arm1176jz-s", "Select the arm1176jz-s processor", E(ArchV6) }, |
| 127 | { MK_TARGET_ARCH(ARM), "arm1176jzf-s", "Select the arm1176jzf-s processor", E(ArchV6) | E(FeatureVFP2) }, |
| 128 | { MK_TARGET_ARCH(ARM), "arm710t", "Select the arm710t processor", E(ArchV4T) }, |
| 129 | { MK_TARGET_ARCH(ARM), "arm720t", "Select the arm720t processor", E(ArchV4T) }, |
| 130 | { MK_TARGET_ARCH(ARM), "arm7tdmi", "Select the arm7tdmi processor", E(ArchV4T) }, |
| 131 | { MK_TARGET_ARCH(ARM), "arm7tdmi-s", "Select the arm7tdmi-s processor", E(ArchV4T) }, |
| 132 | { MK_TARGET_ARCH(ARM), "arm8", "Select the arm8 processor", XFeatureNone }, |
| 133 | { MK_TARGET_ARCH(ARM), "arm810", "Select the arm810 processor", XFeatureNone }, |
| 134 | { MK_TARGET_ARCH(ARM), "arm9", "Select the arm9 processor", E(ArchV4T) }, |
| 135 | { MK_TARGET_ARCH(ARM), "arm920", "Select the arm920 processor", E(ArchV4T) }, |
| 136 | { MK_TARGET_ARCH(ARM), "arm920t", "Select the arm920t processor", E(ArchV4T) }, |
| 137 | { MK_TARGET_ARCH(ARM), "arm922t", "Select the arm922t processor", E(ArchV4T) }, |
| 138 | { MK_TARGET_ARCH(ARM), "arm926ej-s", "Select the arm926ej-s processor", E(ArchV5TE) }, |
| 139 | { MK_TARGET_ARCH(ARM), "arm940t", "Select the arm940t processor", E(ArchV4T) }, |
| 140 | { MK_TARGET_ARCH(ARM), "arm946e-s", "Select the arm946e-s processor", E(ArchV5TE) }, |
| 141 | { MK_TARGET_ARCH(ARM), "arm966e-s", "Select the arm966e-s processor", E(ArchV5TE) }, |
| 142 | { MK_TARGET_ARCH(ARM), "arm968e-s", "Select the arm968e-s processor", E(ArchV5TE) }, |
| 143 | { MK_TARGET_ARCH(ARM), "arm9e", "Select the arm9e processor", E(ArchV5TE) }, |
| 144 | { MK_TARGET_ARCH(ARM), "arm9tdmi", "Select the arm9tdmi processor", E(ArchV4T) }, |
| 145 | { MK_TARGET_ARCH(ARM), "cortex-a8", "Select the cortex-a8 processor", E(ArchV7A) | E(FeatureThumb2) | E(FeatureNEON) }, |
| 146 | { MK_TARGET_ARCH(ARM), "cortex-a9", "Select the cortex-a9 processor", E(ArchV7A) | E(FeatureThumb2) | E(FeatureNEON) }, |
| 147 | { MK_TARGET_ARCH(ARM), "ep9312", "Select the ep9312 processor", E(ArchV4T) }, |
| 148 | { MK_TARGET_ARCH(ARM), "generic", "Select the generic processor", XFeatureNone }, |
| 149 | { MK_TARGET_ARCH(ARM), "iwmmxt", "Select the iwmmxt processor", E(ArchV5TE) }, |
| 150 | { MK_TARGET_ARCH(ARM), "mpcore", "Select the mpcore processor", E(ArchV6) | E(FeatureVFP2) }, |
| 151 | { MK_TARGET_ARCH(ARM), "mpcorenovfp", "Select the mpcorenovfp processor", E(ArchV6) }, |
| 152 | { MK_TARGET_ARCH(ARM), "strongarm", "Select the strongarm processor", XFeatureNone }, |
| 153 | { MK_TARGET_ARCH(ARM), "strongarm110", "Select the strongarm110 processor", XFeatureNone }, |
| 154 | { MK_TARGET_ARCH(ARM), "strongarm1100", "Select the strongarm1100 processor", XFeatureNone }, |
| 155 | { MK_TARGET_ARCH(ARM), "strongarm1110", "Select the strongarm1110 processor", XFeatureNone }, |
| 156 | { MK_TARGET_ARCH(ARM), "xscale", "Select the xscale processor", MK_TARGET_FEATURE_BIT(ARM, ArchV5TE) } |
| 157 | }; |
| 158 | #undef E |
| 159 | static int CompareCPUName(const void* a, const void* b) { |
| 160 | return strcasecmp(((TargetCPU*) a)->Name, ((TargetCPU*) b)->Name); |
| 161 | } |
| 162 | #define NUM_TARGET_CPU (sizeof(TargetCPUTable) / sizeof(TargetCPU)) |
| 163 | |
| 164 | static struct option* SlangOpts = NULL; |
| 165 | |
| 166 | static const char* CPUString; |
| 167 | static const TargetCPU* CPU; |
| 168 | static const char* TripleString; |
| 169 | static TargetFeatureEnum EnableFeatureValue, DisableFeatureValue; |
| 170 | static SlangCompilerOutputTy OutputFileType; |
| 171 | static const char* OutputFileName; |
| 172 | static const char* JavaReflectionPackageName; |
| 173 | static const char* InputFileName; |
| 174 | static bool Verbose; |
| 175 | static const char* FeatureEnabledList[MaxTargetFeature + 1]; |
| 176 | |
| 177 | /* Construct the command options table used in ParseOption::getopt_long */ |
| 178 | static void ConstructCommandOptions() { |
| 179 | /* Basic slang command option */ |
| 180 | static struct option BasicSlangOpts[] = { |
| 181 | { "emit-llvm", no_argument, (int*) &OutputFileType, SlangCompilerOutput_LL }, |
| 182 | { "emit-bc", no_argument, (int*) &OutputFileType, SlangCompilerOutput_Bitcode }, |
| 183 | { "emit-asm", no_argument, NULL, 'S' }, |
| 184 | { "emit-obj", no_argument, NULL, 'c' }, |
| 185 | { "emit-nothing", no_argument, (int*) &OutputFileType, SlangCompilerOutput_Nothing }, |
| 186 | |
| 187 | { "help", no_argument, NULL, 'h' }, /* -h */ |
| 188 | { "verbose", no_argument, NULL, 'v' }, /* -v */ |
| 189 | |
| 190 | { "output", required_argument, NULL, 'o' }, /* -o */ |
| 191 | { "cpu", required_argument, NULL, 'u' }, /* -u */ |
| 192 | { "triple", required_argument, NULL, 't' }, /* -t */ |
| 193 | |
| 194 | { "output-java-reflection-class", required_argument, NULL, 'j'} /* -j */ |
| 195 | }; |
| 196 | |
| 197 | const int NumberOfBasicOptions = sizeof(BasicSlangOpts) / sizeof(struct option); |
| 198 | |
| 199 | SlangOpts = new struct option [ NumberOfBasicOptions + MaxTargetFeature * 2 /* for --enable-feature and --disable-feature */ ]; |
| 200 | |
| 201 | /* Fill SlangOpts with basic options */ |
| 202 | memcpy(SlangOpts, BasicSlangOpts, sizeof(BasicSlangOpts)); |
| 203 | |
| 204 | int i = NumberOfBasicOptions; |
| 205 | /* Add --enable-TARGET_FEATURE option into slang command option */ |
| 206 | #define DEF_TARGET_FEATURE(target, id, key, description) \ |
| 207 | SlangOpts[i].name = "enable-" key; \ |
| 208 | SlangOpts[i].has_arg = optional_argument; \ |
| 209 | SlangOpts[i].flag = (int*) &EnableFeatureValue; \ |
| 210 | SlangOpts[i].val = target ## id; \ |
| 211 | i++; |
| 212 | # include "target.inc" |
| 213 | |
| 214 | /* Add --disable-TARGET_FEATURE option into slang command option */ |
| 215 | #define DEF_TARGET_FEATURE(target, id, key, description) \ |
| 216 | SlangOpts[i].name = "disable-" key; \ |
| 217 | SlangOpts[i].has_arg = optional_argument; \ |
| 218 | SlangOpts[i].flag = (int*) &DisableFeatureValue; \ |
| 219 | SlangOpts[i].val = target ## id; \ |
| 220 | i++; |
| 221 | # include "target.inc" |
| 222 | |
| 223 | /* NULL-terminated the SlangOpts */ |
| 224 | memset(&SlangOpts[i], 0, sizeof(struct option)); |
| 225 | |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | extern char *optarg; |
| 230 | extern int optind; |
| 231 | extern int optopt; |
| 232 | extern int opterr; |
| 233 | |
| 234 | static void Usage(const char* CommandName) { |
| 235 | #define OUTPUT_OPTION(short_name, long_name, desc) \ |
| 236 | do { \ |
| 237 | if(short_name) \ |
| 238 | cout << setw(4) << right << (short_name) << ", "; \ |
| 239 | else \ |
| 240 | cout << " "; \ |
| 241 | cout << setw(17) << left << (long_name); \ |
| 242 | cout << " " << (desc) << endl; \ |
| 243 | } while(false) |
| 244 | |
| 245 | cout << "Usage: " << CommandName << " [OPTION]... " << "[INPUT FILE]" << endl; |
| 246 | |
| 247 | cout << endl; |
| 248 | |
| 249 | cout << "Basic: " << endl; |
| 250 | |
| 251 | OUTPUT_OPTION("-h", "--help", "print this help"); |
| 252 | OUTPUT_OPTION("-v", "--verbos", "be verbose"); |
| 253 | OUTPUT_OPTION("-o", "--output=<FILE>", "write the output of compilation to FILE ('-' means stdout)"); |
| 254 | OUTPUT_OPTION("-j", "--output-java-reflection-package=<PACKAGE NAME>", "output reflection to Java for BCC exportables"); |
| 255 | |
| 256 | cout << endl; |
| 257 | |
| 258 | cout << "Output type:" << endl; |
| 259 | |
| 260 | OUTPUT_OPTION(NULL, "--emit-llvm", "set output type to LLVM assembly (.ll)"); |
| 261 | OUTPUT_OPTION(NULL, "--emit-bc", "set output type to Bitcode (.bc) (Default)"); |
| 262 | OUTPUT_OPTION("-S", "--emit-asm", "set output type to target assmbly code (.S)"); |
| 263 | OUTPUT_OPTION("-c", "--emit-obj", "set output type to target object file (.o)"); |
| 264 | OUTPUT_OPTION(NULL, "--emit-nothing", "output nothing"); |
| 265 | |
| 266 | cout << endl; |
| 267 | |
| 268 | cout << "Code generation option: " << endl; |
| 269 | |
| 270 | OUTPUT_OPTION("-u", "--cpu=CPU", "generate the assembly / object file for the CPU"); |
| 271 | cout << endl; |
| 272 | cout << "\tAvailable CPU:" << endl; |
| 273 | |
| 274 | for(int i=0;i<NUM_TARGET_CPU;i++) |
| 275 | cout << "\t" << setw(13) << right << TargetCPUTable[i].Name << left << ": (" << TargetArchTable[(TargetCPUTable[i].Arch)].Name << ") " << TargetCPUTable[i].Desc << endl; |
| 276 | |
| 277 | cout << endl; |
| 278 | |
| 279 | OUTPUT_OPTION("-t", "--triple=TRIPLE", "generate the assembly / object file for the Triple"); |
| 280 | cout << "\tDefault triple: " << endl; |
| 281 | #define DEF_SUPPORT_TARGET(target, name, default_triple) \ |
| 282 | cout << "\t" << setw(5) << right << name << left << ": " << default_triple << endl; |
| 283 | #include "target.inc" |
| 284 | cout << endl; |
| 285 | |
| 286 | OUTPUT_OPTION(NULL, "--enable-FEATURE", "enable the FEATURE for the generation of the assembly / object file"); |
| 287 | OUTPUT_OPTION(NULL, "--disable-FEATURE", "disable the FEATURE for the generation of the assembly / object file"); |
| 288 | cout << endl; |
| 289 | cout << "\tAvailable features:" << endl; |
| 290 | #define DEF_TARGET_FEATURE(target, id, key, description) \ |
| 291 | cout << "\t" << setw(6) << right << key \ |
| 292 | << left << ": (" << TargetArchTable[MK_TARGET_ARCH(target)].Name << ") " \ |
| 293 | << description << endl; |
| 294 | #include "target.inc" |
| 295 | |
| 296 | |
| 297 | cout << endl; |
| 298 | |
| 299 | |
| 300 | #undef OUTPUT_OPTION |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | static bool ParseOption(int Argc, char** Argv) { |
| 305 | assert(SlangOpts != NULL && "Slang command options table was not initialized!"); |
| 306 | |
| 307 | /* Set default value to option */ |
| 308 | CPU = NULL; |
| 309 | TripleString = DEFAULT_TARGET_TRIPLE_STRING; |
| 310 | EnableFeatureValue = DisableFeatureValue = FeatureNone; |
| 311 | OutputFileType = SlangCompilerOutput_Default; |
| 312 | OutputFileName = DEFAULT_OUTPUT_FILENAME; |
| 313 | JavaReflectionPackageName = NULL; |
| 314 | InputFileName = NULL; |
| 315 | Verbose = false; |
| 316 | FeatureEnabledList[0] = NULL; |
| 317 | |
| 318 | int ch; |
| 319 | |
| 320 | unsigned int FeatureEnableBits = 0; |
| 321 | unsigned int FeatureDisableBits = 0; |
| 322 | #define ENABLE_FEATURE(x) \ |
| 323 | FeatureEnableBits |= (x) |
| 324 | #define DISABLE_FEATURE(x) \ |
| 325 | FeatureDisableBits |= (x) |
| 326 | TargetArchEnum ExpectedArch = ArchNone; |
| 327 | |
| 328 | /* Turn off the error message output by getopt_long */ |
| 329 | opterr = 0; |
| 330 | |
| 331 | while((ch = getopt_long(Argc, Argv, "Schvo:u:t:j:", SlangOpts, NULL)) != -1) { |
| 332 | switch(ch) { |
| 333 | case 'S': |
| 334 | OutputFileType = SlangCompilerOutput_Assembly; |
| 335 | break; |
| 336 | |
| 337 | case 'c': |
| 338 | OutputFileType = SlangCompilerOutput_Obj; |
| 339 | break; |
| 340 | |
| 341 | case 'o': |
| 342 | OutputFileName = optarg; |
| 343 | break; |
| 344 | |
| 345 | case 'j': |
| 346 | JavaReflectionPackageName = optarg; |
| 347 | break; |
| 348 | |
| 349 | case 'u': |
| 350 | { |
| 351 | CPUString = optarg; |
| 352 | const TargetCPU SearchCPU = { ArchNone, CPUString, NULL, XFeatureNone }; |
| 353 | CPU = (TargetCPU*) bsearch(&SearchCPU, TargetCPUTable, sizeof(TargetCPUTable) / sizeof(TargetCPU), sizeof(TargetCPU), CompareCPUName); |
| 354 | if(CPU == NULL) { |
| 355 | WARN1(UNKNOWN_CPU, SearchCPU.Name); |
| 356 | } else { |
| 357 | CPUString = CPU->Name; |
| 358 | |
| 359 | if(ExpectedArch == ArchNone) |
| 360 | ExpectedArch = CPU->Arch; |
| 361 | else if(ExpectedArch != CPU->Arch) { |
| 362 | WARN2(MISMATCH_CPU_TARGET_ARCH, TargetArchTable[CPU->Arch].Name, TargetArchTable[ExpectedArch].Name); |
| 363 | break; |
| 364 | } |
| 365 | |
| 366 | /* Get CPU Feature and enable its available feature */ |
| 367 | FeatureEnableBits |= CPU->FeatureEnabled; |
| 368 | } |
| 369 | } |
| 370 | break; |
| 371 | |
| 372 | case 't': |
| 373 | TripleString = optarg; |
| 374 | break; |
| 375 | |
| 376 | case 'h': |
| 377 | Usage(Argv[0]); |
| 378 | return false; |
| 379 | break; |
| 380 | |
| 381 | case 'v': |
| 382 | Verbose = true; |
| 383 | break; |
| 384 | |
| 385 | case 0: |
| 386 | { |
| 387 | if(EnableFeatureValue != FeatureNone || DisableFeatureValue != FeatureNone) { |
| 388 | bool IsDisable = (DisableFeatureValue != FeatureNone); |
| 389 | const TargetFeature* FeatureSelected = &TargetFeatureTable[ ((IsDisable) ? DisableFeatureValue : EnableFeatureValue) ]; |
| 390 | assert(FeatureSelected != NULL && "Unexpected target feature! (not presented in table but parsed!?)"); |
| 391 | |
| 392 | if(ExpectedArch == ArchNone) |
| 393 | ExpectedArch = FeatureSelected->Arch; |
| 394 | else if(FeatureSelected->Arch != ExpectedArch) { |
| 395 | WARN2(MISMATCH_FEATURE_TARGET_ARCH, TargetArchTable[FeatureSelected->Arch].Name, TargetArchTable[ExpectedArch].Name); |
| 396 | break; |
| 397 | } |
| 398 | |
| 399 | if(optarg != NULL && atoi(optarg) == 0) |
| 400 | IsDisable = !IsDisable; |
| 401 | |
| 402 | if(IsDisable) |
| 403 | DISABLE_FEATURE(FeatureSelected->Bit); |
| 404 | else |
| 405 | ENABLE_FEATURE(FeatureSelected->Bit); |
| 406 | } |
| 407 | } |
| 408 | break; |
| 409 | |
| 410 | default: |
| 411 | cerr << "Unknown option: " << Argv[optind - 1] << endl; |
| 412 | return false; |
| 413 | break; |
| 414 | } |
| 415 | } |
| 416 | #undef ENABLE_FEATURE |
| 417 | #undef DISABLE_FEATURE |
| 418 | |
| 419 | int CurFeatureEnableListIdx = 0; |
| 420 | /* Add the enable/disable feature string to */ |
| 421 | switch(ExpectedArch) { |
| 422 | case ArchNone: |
| 423 | ExpectedArch = HOST_ARCH; |
| 424 | break; |
| 425 | |
| 426 | #define DEF_TARGET_FEATURE(target, id, key, description) \ |
| 427 | if(FeatureDisableBits & MK_TARGET_FEATURE_BIT(target, id)) \ |
| 428 | FeatureEnabledList[ CurFeatureEnableListIdx++ ] = "-" key; \ |
| 429 | else if(FeatureEnableBits & MK_TARGET_FEATURE_BIT(target, id)) \ |
| 430 | FeatureEnabledList[ CurFeatureEnableListIdx++ ] = "+" key; |
| 431 | #define HOOK_TARGET_FIRST_FEATURE(target, id, key, description) \ |
| 432 | case Arch ## target: \ |
| 433 | /* Fix target triple */ \ |
| 434 | if(TripleString == DEFAULT_TARGET_TRIPLE_STRING) \ |
| 435 | TripleString = TargetArchTable[MK_TARGET_ARCH(target)].DefaultTriple; \ |
| 436 | DEF_TARGET_FEATURE(target, id, key, description) |
| 437 | #define HOOK_TARGET_LAST_FEATURE(target, id, key, description) \ |
| 438 | DEF_TARGET_FEATURE(target, id, key, description) \ |
| 439 | FeatureEnabledList[ CurFeatureEnableListIdx++ ] = NULL; /* null-terminator */ \ |
| 440 | break; |
| 441 | #include "target.inc" |
| 442 | |
| 443 | default: |
| 444 | assert(false && "Unknown / Unsupported CPU architecture"); |
| 445 | break; |
| 446 | } |
| 447 | |
| 448 | Argc -= optind; |
| 449 | if(Argc <= 0) { |
| 450 | cerr << Argv[0] << ": "ERR_NO_INPUT_FILE << endl; |
| 451 | return false; |
| 452 | } |
| 453 | |
| 454 | if(Argc > 1) |
| 455 | WARN(MULTIPLE_INPUT_FILES); |
| 456 | InputFileName = Argv[optind]; |
| 457 | |
| 458 | if(Verbose) { |
| 459 | cout << "Input: " << InputFileName << endl; |
| 460 | |
| 461 | if(CPU != NULL) |
| 462 | cout << "Use CPU: " << CPU->Name << endl; |
| 463 | cout << "Use triple string: " << TripleString << endl; |
| 464 | cout << "Expected architecture: " << TargetArchTable[ExpectedArch].Name << endl; |
| 465 | |
| 466 | cout << "Enable target feature: " << endl; |
| 467 | for(int i=0;FeatureEnabledList[i]!=NULL;i++) |
| 468 | if(*FeatureEnabledList[i] == '+') |
| 469 | cout << "\t" << &FeatureEnabledList[i][1] << endl; |
| 470 | cout << endl; |
| 471 | |
| 472 | cout << "Disable target feature: " << endl; |
| 473 | for(int i=0;FeatureEnabledList[i]!=NULL;i++) |
| 474 | if(*FeatureEnabledList[i] == '-') |
| 475 | cout << "\t" << &FeatureEnabledList[i][1] << endl; |
| 476 | cout << endl; |
| 477 | |
| 478 | cout << "Output to: " << ((strcmp(OutputFileName, "-")) ? OutputFileName : "(standard output)") << ", type: "; |
| 479 | switch(OutputFileType) { |
| 480 | case SlangCompilerOutput_Assembly: cout << "Target Assembly"; break; |
| 481 | case SlangCompilerOutput_LL: cout << "LLVM Assembly"; break; |
| 482 | case SlangCompilerOutput_Bitcode: cout << "Bitcode"; break; |
| 483 | case SlangCompilerOutput_Nothing: cout << "No output (test)"; break; |
| 484 | case SlangCompilerOutput_Obj: cout << "Object file"; break; |
| 485 | default: assert(false && "Unknown output type"); break; |
| 486 | } |
| 487 | cout << endl; |
| 488 | } |
| 489 | |
| 490 | return true; |
| 491 | } |
| 492 | |
| 493 | static void DestroyCommandOptions() { |
| 494 | if(SlangOpts != NULL) { |
| 495 | delete [] SlangOpts; |
| 496 | SlangOpts = NULL; |
| 497 | } |
| 498 | return; |
| 499 | } |
| 500 | |
| 501 | #define SLANG_CALL_AND_CHECK(expr) \ |
| 502 | if(!(expr)) { \ |
| 503 | if(slangGetInfoLog(slang)) \ |
| 504 | cerr << slangGetInfoLog(slang); \ |
Shih-wei Liao | c552b7c | 2010-06-25 11:06:57 -0700 | [diff] [blame^] | 505 | ret = 1; \ |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 506 | goto on_slang_error; \ |
| 507 | } |
| 508 | |
| 509 | int main(int argc, char** argv) { |
Shih-wei Liao | c552b7c | 2010-06-25 11:06:57 -0700 | [diff] [blame^] | 510 | int ret = 0; |
| 511 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 512 | if(argc < 2) { |
| 513 | cerr << argv[0] << ": "ERR_NO_INPUT_FILE << endl; |
| 514 | return 1; |
| 515 | } |
| 516 | |
| 517 | ConstructCommandOptions(); |
| 518 | |
| 519 | if(ParseOption(argc, argv)) { |
| 520 | /* Start compilation */ |
| 521 | SlangCompiler* slang = slangCreateCompiler(TripleString, CPUString, FeatureEnabledList); |
| 522 | if(slang != NULL) { |
| 523 | SLANG_CALL_AND_CHECK( slangSetSourceFromFile(slang, InputFileName) ); |
| 524 | |
| 525 | slangSetOutputType(slang, OutputFileType); |
| 526 | |
| 527 | SLANG_CALL_AND_CHECK( slangSetOutputToFile(slang, OutputFileName) ); |
| 528 | |
| 529 | SLANG_CALL_AND_CHECK( slangCompile(slang) <= 0 ); |
| 530 | |
| 531 | /* output log anyway */ |
| 532 | if(slangGetInfoLog(slang)) |
| 533 | cout << slangGetInfoLog(slang); |
| 534 | |
Shih-wei Liao | 537446c | 2010-06-11 16:05:55 -0700 | [diff] [blame] | 535 | SLANG_CALL_AND_CHECK( slangReflectToJava(slang, JavaReflectionPackageName) ); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 536 | |
| 537 | on_slang_error: |
| 538 | delete slang; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | DestroyCommandOptions(); |
| 543 | |
Shih-wei Liao | c552b7c | 2010-06-25 11:06:57 -0700 | [diff] [blame^] | 544 | return ret; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 545 | } |