llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
driver.h
1 // This file if part of the llir-opt project.
2 // Licensing information can be found in the LICENSE file.
3 // (C) 2018 Nandor Licker. All rights reserved.
4 
5 #pragma once
6 
7 #include <optional>
8 
9 #include <llvm/ADT/Triple.h>
10 #include <llvm/Option/ArgList.h>
11 #include <llvm/Support/Error.h>
12 
13 #include "linker.h"
14 
15 class Prog;
16 
17 
18 
20 enum class OptLevel {
22  O0,
24  O1,
26  O2,
28  O3,
30  O4,
32  Os
33 };
34 
38 llvm::Error MakeError(llvm::Twine msg);
39 
43 class Driver final {
44 public:
46  Driver(
47  const llvm::Triple &triple,
48  const llvm::Triple &base,
49  llvm::opt::InputArgList &args
50  );
51 
53  ~Driver();
54 
56  llvm::Error Link();
57 
58 private:
60  llvm::Expected<Linker::Archive>
61  LoadArchive(llvm::MemoryBufferRef buffer);
62 
64  llvm::Expected<std::optional<Linker::Archive>>
65  TryLoadArchive(const std::string &path);
66 
67 private:
69  enum class OutputType {
70  EXE,
71  OBJ,
72  ASM,
73  LLIR,
74  LLBC
75  };
77  OutputType GetOutputType();
78 
80  llvm::Error Output(OutputType type, Prog &prog);
81  // Run the optimiser on a binary.
82  llvm::Error RunOpt(
83  llvm::StringRef input,
84  llvm::StringRef output,
85  OutputType type
86  );
88  llvm::Error RunExecutable(
89  llvm::StringRef exe,
90  llvm::ArrayRef<llvm::StringRef> args
91  );
92 
93 private:
95  const llvm::Triple &llirTriple_;
97  const llvm::Triple &baseTriple_;
98 
100  llvm::opt::InputArgList &args_;
101 
103  const std::string output_;
105  bool shared_;
107  bool static_;
109  bool noShared_;
111  bool relocatable_;
113  bool exportDynamic_;
115  bool ehFrameHdr_;
117  std::string targetCPU_;
119  std::string targetABI_;
121  std::string targetFS_;
123  std::string entry_;
125  OptLevel optLevel_;
127  std::vector<std::string> libraryPaths_;
128 
130  std::vector<std::unique_ptr<llvm::MemoryBuffer>> buffers_;
132  std::vector<llvm::sys::fs::TempFile> tempFiles_;
134  std::vector<std::string> externLibs_;
136  llvm::opt::ArgStringList forwarded_;
137 };
Driver::Driver
Driver(const llvm::Triple &triple, const llvm::Triple &base, llvm::opt::InputArgList &args)
Set up the driver.
Definition: driver.cpp:66
Prog
Definition: prog.h:33
Driver::Link
llvm::Error Link()
Run the linker.
Definition: driver.cpp:259
Driver::~Driver
~Driver()
Cleanup.
Definition: driver.cpp:92
Driver
Definition: driver.h:43