Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vla5924 committed May 18, 2024
1 parent 5dd3bfb commit b790622
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 36 deletions.
8 changes: 4 additions & 4 deletions compiler/include/compiler/cli/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ constexpr std::string_view optree = "optree";
} // namespace compilation_path

struct Options {
bool help;
bool version;
bool debug;
std::string path;
bool time;
bool optimize;
std::optional<std::string> stopAfter;
std::optional<std::string> optimize;
#ifdef LLVMIR_CODEGEN_ENABLED
bool compile;
std::string clang;
std::string llc;
std::optional<std::string> output;
std::string output;
#endif
std::vector<std::string> files;
std::string helpMessage;

void dump() const;
};

class OptionsError : public std::exception {
Expand Down
40 changes: 26 additions & 14 deletions compiler/lib/cli/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ int Compiler::runPreprocessor() {
return 3;
}
if (opt.debug) {
std::cerr << "PREPROCESSOR:\n";
dumping::dump(source);
std::cerr << "PREPROCESSOR:\n" << dumping::dump(source);
}
measuredTimes[stage::preprocessor] = timer.elapsed();
return 0;
Expand All @@ -125,8 +124,7 @@ int Compiler::runLexer() {
return 3;
}
if (opt.debug) {
std::cerr << "LEXER:\n";
dumping::dump(tokens);
std::cerr << "LEXER:\n" << dumping::dump(tokens);
}
measuredTimes[stage::lexer] = timer.elapsed();
return 0;
Expand Down Expand Up @@ -217,10 +215,18 @@ int Compiler::runAstLLVMIRGenerator() {
std::cerr << "LLVMIR GENERATOR:\n";
std::cerr << generator.dump();
}
bool printOutput = opt.output == "-";
if (!opt.compile) {
generator.writeToFile(opt.output);
if (printOutput)
std::cout << generator.dump();
else
generator.writeToFile(opt.output);
return 0;
}
if (printOutput) {
std::cerr << "Unable to print binary file to stdout\n";
return 3;
}

TemporaryDirectory tempDir;
try {
Expand All @@ -238,8 +244,7 @@ int Compiler::runAstLLVMIRGenerator() {
bool cmdFailed = (std::system(llcCmd.c_str()) || std::system(clangCmd.c_str()));
if (cmdFailed)
return 3;
else
std::filesystem::copy_file(exeFile, opt.output);
std::filesystem::copy_file(exeFile, opt.output);
} catch (std::exception &e) {
std::cerr << e.what();
return 3;
Expand Down Expand Up @@ -284,15 +289,15 @@ int Compiler::runOptreeLLVMIRGenerator() {
std::cerr << "LLVMIR GENERATOR:\n";
std::cerr << generator.dump();
}
bool printOutput = opt.output == "-";
if (!opt.compile) {
auto output = opt.output.value_or("-");
if (output == "-")
if (printOutput)
std::cout << generator.dump();
else
generator.dumpToFile(output);
generator.dumpToFile(opt.output);
return 0;
}
if (!opt.output.has_value()) {
if (printOutput) {
std::cerr << "Unable to print binary file to stdout\n";
return 3;
}
Expand All @@ -313,7 +318,7 @@ int Compiler::runOptreeLLVMIRGenerator() {
bool cmdFailed = (std::system(llcCmd.c_str()) || std::system(clangCmd.c_str()));
if (cmdFailed)
return 3;
std::filesystem::copy_file(exeFile, opt.output.value());
std::filesystem::copy_file(exeFile, opt.output);
} catch (std::exception &e) {
std::cerr << e.what();
return 3;
Expand All @@ -324,6 +329,11 @@ int Compiler::runOptreeLLVMIRGenerator() {
#endif

int Compiler::run() {
if (opt.debug) {
std::cerr << "Provided options: ";
opt.dump();
std::cerr << '\n';
}
RETURN_IF_NONZERO(readFiles());
RETURN_IF_NONZERO(runPreprocessor());
RETURN_IF_STOPAFTER(opt, stage::preprocessor);
Expand All @@ -343,8 +353,10 @@ int Compiler::run() {
} else if (opt.path == compilation_path::optree) {
RETURN_IF_NONZERO(runConverter());
RETURN_IF_STOPAFTER(opt, stage::converter);
RETURN_IF_NONZERO(runOptreeOptimizer());
RETURN_IF_STOPAFTER(opt, stage::optimizer);
if (opt.optimize) {
RETURN_IF_NONZERO(runOptreeOptimizer());
RETURN_IF_STOPAFTER(opt, stage::optimizer);
}
#ifdef ENABLE_CODEGEN_OPTREE_TO_LLVMIR
RETURN_IF_NONZERO(runOptreeLLVMIRGenerator());
RETURN_IF_STOPAFTER(opt, stage::codegen);
Expand Down
4 changes: 4 additions & 0 deletions compiler/lib/cli/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ namespace cli {

TemporaryDirectory::TemporaryDirectory() {
char tmpnamResult[L_tmpnam] = {0};

Check failure

Code scanning / clang-tidy

do not declare C-style arrays, use std::array<> instead [modernize-avoid-c-arrays,-warnings-as-errors] Error

do not declare C-style arrays, use std::array<> instead [modernize-avoid-c-arrays,-warnings-as-errors]
#if defined(_MSC_VER)
tmpnam_s(tmpnamResult);
#else
tmpnam(tmpnamResult);
#endif
dir = tmpnamResult;
std::filesystem::create_directory(dir);
}
Expand Down
4 changes: 0 additions & 4 deletions compiler/lib/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ int main(int argc, char *argv[]) {
std::cerr << err.what() << "\n";

Check failure

Code scanning / clang-tidy

no header providing "std::cerr" is directly included [misc-include-cleaner,-warnings-as-errors] Error

no header providing "std::cerr" is directly included [misc-include-cleaner,-warnings-as-errors]
return 1;
}
if (opt.help) {
std::cerr << opt.helpMessage;
return 0;
}

Compiler compiler(opt);
int ret = compiler.run();
Expand Down
40 changes: 27 additions & 13 deletions compiler/lib/cli/options.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
#include "options.hpp"

#include <iostream>

#include <argparse/argparse.hpp>

#include "version.hpp"

namespace cli {

void Options::dump() const {
std::cerr << "debug=" << debug << ", path=" << path << ", time=" << time << ", optimize=" << optimize;
if (stopAfter.has_value())
std::cerr << ", stopAfter=" << stopAfter.value();
#ifdef LLVMIR_CODEGEN_ENABLED
std::cerr << ", compile=" << compile << ", clang=" << clang << ", llc=" << llc << ", output=" << output;
#endif
std::cerr << ", files=[ ";
for (const auto &file : files)
std::cerr << file << ' ';
std::cerr << "]";
}

Options parseArguments(int argc, const char *const argv[]) {

Check failure

Code scanning / clang-tidy

do not declare C-style arrays, use std::array<> instead [modernize-avoid-c-arrays,-warnings-as-errors] Error

do not declare C-style arrays, use std::array<> instead [modernize-avoid-c-arrays,-warnings-as-errors]
argparse::ArgumentParser program("compiler", version);
program.add_argument("-h", arg::help).help("show help message and exit").flag();
program.add_argument(arg::version).help("show version info").flag();
program.add_argument(arg::debug).help("print debug info").flag();
program.add_argument(arg::debug).help("print debug info (stages output)").flag();
program.add_argument(arg::path)
.help("compilation path")
.choices(compilation_path::ast, compilation_path::optree)
.default_value(compilation_path::optree);
.default_value(std::string(compilation_path::optree));

Check failure

Code scanning / clang-tidy

no header providing "std::string" is directly included [misc-include-cleaner,-warnings-as-errors] Error

no header providing "std::string" is directly included [misc-include-cleaner,-warnings-as-errors]
program.add_argument(arg::time).help("print execution times of each stage").flag();
program.add_argument(arg::stopAfter)
.help("stop processing after specific stage")
Expand All @@ -24,14 +37,17 @@ Options parseArguments(int argc, const char *const argv[]) {
stage::codegen
#endif
);
program.add_argument("-O", arg::optimize).help("perform optimizations").nargs(argparse::nargs_pattern::optional);
program.add_argument("-O", arg::optimize).help("perform optimizations").flag();
#ifdef LLVMIR_CODEGEN_ENABLED
program.add_argument("-c", arg::compile).help("produce an executable instead of LLVM IR code").flag();
program.add_argument(arg::clang).help("path to clang executable").default_value("clang");
program.add_argument(arg::llc).help("path to llc executable").default_value("llc");
program.add_argument("-o", arg::output).help("output file");
program.add_argument("-o", arg::output).help("output file").default_value("-");
#endif
program.add_argument(arg::files).help("source files (separated by spaces)").remaining();
program.add_argument(arg::files)
.help("source files (separated by spaces)")
.required()
.nargs(argparse::nargs_pattern::at_least_one);

try {
program.parse_args(argc, argv);
Expand All @@ -40,22 +56,20 @@ Options parseArguments(int argc, const char *const argv[]) {
}

Options options;
options.help = program.get<bool>(arg::help);
options.debug = program.get<bool>(arg::debug);
options.path = program.get<std::string>(arg::path);

Check failure

Code scanning / clang-tidy

no header providing "std::string" is directly included [misc-include-cleaner,-warnings-as-errors] Error

no header providing "std::string" is directly included [misc-include-cleaner,-warnings-as-errors]
options.time = program.get<bool>(arg::time);
options.optimize = program.get<bool>(arg::optimize);
if (program.is_used(arg::stopAfter))
options.stopAfter = program.get<std::string>(arg::stopAfter);

Check failure

Code scanning / clang-tidy

no header providing "std::string" is directly included [misc-include-cleaner,-warnings-as-errors] Error

no header providing "std::string" is directly included [misc-include-cleaner,-warnings-as-errors]
if (program.is_used(arg::optimize))
options.optimize = program.get<std::string>(arg::optimize);
#ifdef LLVMIR_CODEGEN_ENABLED
options.compile = program.get<bool>(arg::compile);
options.clang = program.get<std::string>(arg::clang);

Check failure

Code scanning / clang-tidy

no header providing "std::string" is directly included [misc-include-cleaner,-warnings-as-errors] Error

no header providing "std::string" is directly included [misc-include-cleaner,-warnings-as-errors]
options.llc = program.get<std::string>(arg::llc);

Check failure

Code scanning / clang-tidy

no header providing "std::string" is directly included [misc-include-cleaner,-warnings-as-errors] Error

no header providing "std::string" is directly included [misc-include-cleaner,-warnings-as-errors]
if (program.is_used(arg::output))
options.output = program.get<std::string>(arg::output);
options.output = program.get<std::string>(arg::output);

Check failure

Code scanning / clang-tidy

no header providing "std::string" is directly included [misc-include-cleaner,-warnings-as-errors] Error

no header providing "std::string" is directly included [misc-include-cleaner,-warnings-as-errors]
#endif
options.files = program.get<std::vector<std::string>>(arg::files);
if (program.is_used(arg::files))
options.files = program.get<std::vector<std::string>>(arg::files);

Check failure

Code scanning / clang-tidy

no header providing "std::vector" is directly included [misc-include-cleaner,-warnings-as-errors] Error

no header providing "std::vector" is directly included [misc-include-cleaner,-warnings-as-errors]

Check failure

Code scanning / clang-tidy

no header providing "std::string" is directly included [misc-include-cleaner,-warnings-as-errors] Error

no header providing "std::string" is directly included [misc-include-cleaner,-warnings-as-errors]
options.helpMessage = program.help().str();
return options;
}
Expand Down

0 comments on commit b790622

Please sign in to comment.