bbb8438bc37966f027748bb6f80fda0c6353b53e
[oota-llvm.git] / tools / llvmc / examples / mcc16 / Hooks.cpp
1 #include "llvm/Support/Path.h"
2 #include "llvm/Support/CommandLine.h"
3 #include "llvm/Support/raw_ostream.h"
4
5 #include <string>
6 #include <locale>
7
8 namespace llvmc {
9   extern const char *ProgramName;
10
11   namespace autogenerated {
12     extern llvm::cl::opt<std::string> Parameter_p;
13   }
14 }
15
16 using namespace llvm;
17 using namespace llvmc;
18
19 // Returns the platform specific directory separator via #ifdefs.
20 // FIXME: This currently work on linux and windows only. It does not
21 // work on other unices.
22 static std::string GetDirSeparator() {
23 #if __linux__ || __APPLE__
24   return "/";
25 #else
26   return "\\";
27 #endif
28 }
29
30 namespace hooks {
31 // Get preprocessor define for the part.
32 // It is __partname format in lower case.
33 std::string
34 GetLowerCasePartDefine(void) {
35    std::locale loc;
36   std::string Partname;
37   if (autogenerated::Parameter_p.empty()) {
38     Partname = "16f1xxx";
39   } else {
40     Partname = autogenerated::Parameter_p;
41   }
42
43   std::string LowerCase;
44   for (unsigned i = 0; i < Partname.size(); i++) {
45     LowerCase.push_back(std::tolower(Partname[i], loc));
46   }
47
48   return "__" + LowerCase;
49 }
50
51 std::string
52 GetUpperCasePartDefine(void) {
53    std::locale loc;
54   std::string Partname;
55   if (autogenerated::Parameter_p.empty()) {
56     Partname = "16f1xxx";
57   } else {
58     Partname = autogenerated::Parameter_p;
59   }
60
61   std::string UpperCase;
62   for (unsigned i = 0; i < Partname.size(); i++) {
63     UpperCase.push_back(std::toupper(Partname[i], loc));
64   }
65
66   return "__" +  UpperCase;
67 }
68
69 // Get the dir where c16 executables reside.
70 std::string GetBinDir() {
71   // Construct a Path object from the program name.
72   void *P = (void*) (intptr_t) GetBinDir;
73   sys::Path ProgramFullPath
74     = sys::Path::GetMainExecutable(llvmc::ProgramName, P);
75
76   // Get the dir name for the program. It's last component should be 'bin'.
77   std::string BinDir = ProgramFullPath.getDirname();
78
79   // llvm::errs() << "BinDir: " << BinDir << '\n';
80   return BinDir + GetDirSeparator();
81 }
82
83 // Get the Top-level Installation dir for c16.
84 std::string GetInstallDir() {
85   sys::Path BinDirPath = sys::Path(GetBinDir());
86
87   // Go one more level up to get the install dir.
88   std::string InstallDir  = BinDirPath.getDirname();
89
90   return InstallDir + GetDirSeparator();
91 }
92
93 // Get the dir where the c16 header files reside.
94 std::string GetStdHeadersDir() {
95   return GetInstallDir() + "include";
96 }
97
98 // Get the dir where the assembler header files reside.
99 std::string GetStdAsmHeadersDir() {
100   return GetInstallDir() + "inc";
101 }
102
103 // Get the dir where the linker scripts reside.
104 std::string GetStdLinkerScriptsDir() {
105   return GetInstallDir() + "lkr";
106 }
107
108 // Get the dir where startup code, intrinsics and lib reside.
109 std::string GetStdLibsDir() {
110   return GetInstallDir() + "lib";
111 }
112 }