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 TARGET_WASM32 = "wasm32"; | ||
18 | const char *const TARGET_WASM64 = "wasm64"; | ||
19 | const char *const ENV_VAR_DOCKERIZED = "SPICE_DOCKERIZED"; | ||
20 | |||
21 | enum OptLevel : uint8_t { | ||
22 | O0 = 0, // No optimization | ||
23 | O1 = 1, // Only basic optimizations | ||
24 | O2 = 2, // Default optimization level | ||
25 | O3 = 3, // Aggressively optimize for performance | ||
26 | Os = 4, // Optimize for code size | ||
27 | Oz = 5, // Aggressively optimize for code size | ||
28 | }; | ||
29 | |||
30 | enum BuildMode : uint8_t { | ||
31 | DEBUG = 0, // Default build mode, uses -O0 per default | ||
32 | RELEASE = 1, // Build without debug information and with -O2 per default | ||
33 | TEST = 2, // Build with test main function and always emit assertions | ||
34 | }; | ||
35 | const char *const BUILD_MODE_DEBUG = "debug"; | ||
36 | const char *const BUILD_MODE_RELEASE = "release"; | ||
37 | const char *const BUILD_MODE_TEST = "test"; | ||
38 | |||
39 | /** | ||
40 | * Representation of the various cli options | ||
41 | */ | ||
42 | struct CliOptions { | ||
43 | std::filesystem::path mainSourceFile; // e.g. ./main.spice | ||
44 | llvm::Triple targetTriple; // In format: <arch><sub>-<vendor>-<sys>-<abi> | ||
45 | std::string targetArch = TARGET_UNKNOWN; | ||
46 | std::string targetVendor = TARGET_UNKNOWN; | ||
47 | std::string targetOs = TARGET_UNKNOWN; | ||
48 | bool isNativeTarget = true; | ||
49 | bool useCPUFeatures = true; | ||
50 | bool execute = false; | ||
51 | std::filesystem::path cacheDir; // Where the cache files go. Should always be a temp directory | ||
52 | std::filesystem::path outputDir = "./"; // Where the object files go. Should always be a temp directory | ||
53 | std::filesystem::path outputPath; // Where the output binary goes. | ||
54 | BuildMode buildMode = DEBUG; // Default build mode is debug | ||
55 | unsigned short compileJobCount = 0; // 0 for auto | ||
56 | bool ignoreCache = false; | ||
57 | std::string llvmArgs; | ||
58 | bool printDebugOutput = false; | ||
59 | struct DumpSettings { | ||
60 | bool dumpCST = false; | ||
61 | bool dumpAST = false; | ||
62 | bool dumpSymbolTable = false; | ||
63 | bool dumpTypes = false; | ||
64 | bool dumpCacheStats = false; | ||
65 | bool dumpDependencyGraph = false; | ||
66 | bool dumpIR = false; | ||
67 | bool dumpAssembly = false; | ||
68 | bool dumpObjectFile = false; | ||
69 | bool dumpToFiles = false; | ||
70 | bool abortAfterDump = false; | ||
71 | } dumpSettings; | ||
72 | bool namesForIRValues = false; | ||
73 | bool useLifetimeMarkers = false; | ||
74 | OptLevel optLevel = O0; // The default optimization level for debug build mode is O0 | ||
75 | bool useLTO = false; | ||
76 | bool noEntryFct = false; | ||
77 | bool generateTestMain = false; | ||
78 | bool staticLinking = false; | ||
79 | bool generateDebugInfo = false; | ||
80 | bool disableVerifier = false; | ||
81 | bool testMode = false; | ||
82 | bool comparableOutput = false; | ||
83 | }; | ||
84 | |||
85 | /** | ||
86 | * Helper class to set up the driver and command line parser | ||
87 | */ | ||
88 | class Driver { | ||
89 | public: | ||
90 | // Constructors | ||
91 | ✗ | Driver() = default; | |
92 |
3/6✓ Branch 0 (5→6) taken 8 times.
✗ Branch 1 (5→23) not taken.
✓ Branch 2 (8→9) taken 8 times.
✗ Branch 3 (8→17) not taken.
✓ Branch 4 (9→10) taken 8 times.
✗ Branch 5 (9→15) not taken.
|
40 | explicit Driver(bool dryRun) : dryRun(dryRun) {} |
93 | Driver(const Driver &) = delete; | ||
94 | |||
95 | // Public methods | ||
96 | void init(); | ||
97 | int parse(int argc, const char *argv[]); | ||
98 | void enrich(); | ||
99 | void runBinary() const; | ||
100 | |||
101 | // Public members | ||
102 | CliOptions cliOptions; | ||
103 | bool shouldCompile = false; | ||
104 | bool shouldInstall = false; | ||
105 | bool shouldUninstall = false; | ||
106 | bool shouldExecute = false; | ||
107 | bool dryRun = false; // For unit testing purposes | ||
108 | |||
109 | private: | ||
110 | // Private methods | ||
111 | void addBuildSubcommand(); | ||
112 | void addRunSubcommand(); | ||
113 | void addTestSubcommand(); | ||
114 | void addInstallSubcommand(); | ||
115 | void addUninstallSubcommand(); | ||
116 | void addCompileSubcommandOptions(CLI::App *subCmd); | ||
117 | static void ensureNotDockerized(); | ||
118 | |||
119 | // Members | ||
120 | CLI::App app = CLI::App{"Spice Programming Language", "spice"}; | ||
121 | }; | ||
122 | |||
123 | } // namespace spice::compiler | ||
124 |