| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2025 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #pragma once | ||
| 4 | |||
| 5 | #include <llvm/TargetParser/Triple.h> | ||
| 6 | |||
| 7 | #include "../../lib/cli11/CLI11.hpp" | ||
| 8 | |||
| 9 | // Undef conflicting macros (only problematic on Windows) | ||
| 10 | #undef TRUE | ||
| 11 | #undef FALSE | ||
| 12 | #undef CONST | ||
| 13 | |||
| 14 | namespace spice::compiler { | ||
| 15 | |||
| 16 | const char *const TARGET_UNKNOWN = "unknown"; | ||
| 17 | const char *const ENV_VAR_DOCKERIZED = "SPICE_DOCKERIZED"; | ||
| 18 | |||
| 19 | enum class OptLevel : uint8_t { | ||
| 20 | O0 = 0, // No optimization | ||
| 21 | O1 = 1, // Only basic optimizations | ||
| 22 | O2 = 2, // Default optimization level | ||
| 23 | O3 = 3, // Aggressively optimize for performance | ||
| 24 | Os = 4, // Optimize for code size | ||
| 25 | Oz = 5, // Aggressively optimize for code size | ||
| 26 | }; | ||
| 27 | |||
| 28 | enum class Sanitizer : uint8_t { | ||
| 29 | NONE = 0, // No sanitizer | ||
| 30 | ADDRESS = 1, // Sanitize memory accesses | ||
| 31 | THREAD = 2, // Sanitize threads for data race prevention | ||
| 32 | MEMORY = 3, // Sanitize against uninitialized memory reads | ||
| 33 | }; | ||
| 34 | const char *const SANITIZER_NONE = "none"; | ||
| 35 | const char *const SANITIZER_ADDRESS = "address"; | ||
| 36 | const char *const SANITIZER_THREAD = "thread"; | ||
| 37 | const char *const SANITIZER_MEMORY = "memory"; | ||
| 38 | |||
| 39 | enum class BuildMode : uint8_t { | ||
| 40 | DEBUG = 0, // Default build mode, uses -O0 per default | ||
| 41 | RELEASE = 1, // Build without debug information and with -O2 per default | ||
| 42 | TEST = 2, // Build with test main function and always emit assertions | ||
| 43 | }; | ||
| 44 | const char *const BUILD_MODE_DEBUG = "debug"; | ||
| 45 | const char *const BUILD_MODE_RELEASE = "release"; | ||
| 46 | const char *const BUILD_MODE_TEST = "test"; | ||
| 47 | |||
| 48 | /** | ||
| 49 | * Representation of the various cli options | ||
| 50 | */ | ||
| 51 | struct CliOptions { | ||
| 52 | std::filesystem::path mainSourceFile; // e.g. ./main.spice | ||
| 53 | llvm::Triple targetTriple; // In format: <arch><sub>-<vendor>-<sys>-<abi> | ||
| 54 | std::string targetArch = TARGET_UNKNOWN; | ||
| 55 | std::string targetVendor = TARGET_UNKNOWN; | ||
| 56 | std::string targetOs = TARGET_UNKNOWN; | ||
| 57 | bool isNativeTarget = true; | ||
| 58 | bool useCPUFeatures = true; | ||
| 59 | bool execute = false; | ||
| 60 | std::filesystem::path cacheDir; // Where the cache files go. Should always be a temp directory | ||
| 61 | std::filesystem::path outputDir = "./"; // Where the object files go. Should always be a temp directory | ||
| 62 | std::filesystem::path outputPath; // Where the output binary goes. | ||
| 63 | BuildMode buildMode = BuildMode::DEBUG; // Default build mode is debug | ||
| 64 | unsigned short compileJobCount = 0; // 0 for auto | ||
| 65 | bool ignoreCache = false; | ||
| 66 | std::string llvmArgs; | ||
| 67 | bool printDebugOutput = false; | ||
| 68 | struct DumpSettings { | ||
| 69 | bool dumpCST = false; | ||
| 70 | bool dumpAST = false; | ||
| 71 | bool dumpSymbolTable = false; | ||
| 72 | bool dumpTypes = false; | ||
| 73 | bool dumpCacheStats = false; | ||
| 74 | bool dumpDependencyGraph = false; | ||
| 75 | bool dumpIR = false; | ||
| 76 | bool dumpAssembly = false; | ||
| 77 | bool dumpObjectFiles = false; | ||
| 78 | bool dumpToFiles = false; | ||
| 79 | bool abortAfterDump = false; | ||
| 80 | } dump; | ||
| 81 | bool namesForIRValues = false; | ||
| 82 | bool useLifetimeMarkers = false; | ||
| 83 | bool useTBAAMetadata = false; | ||
| 84 | OptLevel optLevel = OptLevel::O0; // The default optimization level for debug build mode is O0 | ||
| 85 | bool useLTO = false; | ||
| 86 | bool noEntryFct = false; | ||
| 87 | bool generateTestMain = false; | ||
| 88 | bool staticLinking = false; | ||
| 89 | struct InstrumentationSettings { | ||
| 90 | bool generateDebugInfo = false; | ||
| 91 | Sanitizer sanitizer = Sanitizer::NONE; | ||
| 92 | } instrumentation; | ||
| 93 | bool disableVerifier = false; | ||
| 94 | bool testMode = false; | ||
| 95 | bool comparableOutput = false; | ||
| 96 | }; | ||
| 97 | |||
| 98 | /** | ||
| 99 | * Helper class to set up the driver and command line parser | ||
| 100 | */ | ||
| 101 | class Driver { | ||
| 102 | public: | ||
| 103 | // Constructors | ||
| 104 | − | Driver() = default; // LCOV_EXCL_LINE | |
| 105 |
7/26✓ Branch 6 → 7 taken 11 times.
✗ Branch 6 → 50 not taken.
✓ Branch 9 → 10 taken 11 times.
✗ Branch 9 → 44 not taken.
✓ Branch 12 → 13 taken 11 times.
✗ Branch 12 → 38 not taken.
✓ Branch 14 → 15 taken 11 times.
✗ Branch 14 → 32 not taken.
✓ Branch 22 → 23 taken 11 times.
✗ Branch 22 → 67 not taken.
✓ Branch 25 → 26 taken 11 times.
✗ Branch 25 → 61 not taken.
✓ Branch 26 → 27 taken 11 times.
✗ Branch 26 → 59 not taken.
✗ Branch 32 → 33 not taken.
✗ Branch 32 → 34 not taken.
✗ Branch 35 → 36 not taken.
✗ Branch 35 → 37 not taken.
✗ Branch 41 → 42 not taken.
✗ Branch 41 → 43 not taken.
✗ Branch 47 → 48 not taken.
✗ Branch 47 → 49 not taken.
✗ Branch 53 → 54 not taken.
✗ Branch 53 → 55 not taken.
✗ Branch 56 → 57 not taken.
✗ Branch 56 → 58 not taken.
|
88 | explicit Driver(bool dryRun) : dryRun(dryRun) {} |
| 106 | Driver(const Driver &) = delete; | ||
| 107 | |||
| 108 | // Public methods | ||
| 109 | void init(); | ||
| 110 | int parse(int argc, const char *argv[]); | ||
| 111 | void enrich(); | ||
| 112 | void runBinary() const; | ||
| 113 | |||
| 114 | // Public members | ||
| 115 | CliOptions cliOptions = {}; | ||
| 116 | bool shouldCompile = false; | ||
| 117 | bool shouldInstall = false; | ||
| 118 | bool shouldUninstall = false; | ||
| 119 | bool shouldExecute = false; | ||
| 120 | bool dryRun = false; // For unit testing purposes | ||
| 121 | |||
| 122 | private: | ||
| 123 | // Private methods | ||
| 124 | void addBuildSubcommand(); | ||
| 125 | void addRunSubcommand(); | ||
| 126 | void addTestSubcommand(); | ||
| 127 | void addInstallSubcommand(); | ||
| 128 | void addUninstallSubcommand(); | ||
| 129 | void addCompileSubcommandOptions(CLI::App *subCmd); | ||
| 130 | void addInstrumentationOptions(CLI::App *subCmd); | ||
| 131 | static void ensureNotDockerized(); | ||
| 132 | |||
| 133 | // Members | ||
| 134 | CLI::App app = CLI::App{"Spice Programming Language", "spice"}; | ||
| 135 | }; | ||
| 136 | |||
| 137 | } // namespace spice::compiler | ||
| 138 |