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