summaryrefslogtreecommitdiff
path: root/include/llvm/Tools
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2001-06-06 20:29:01 +0000
committerChris Lattner <sabre@nondot.org>2001-06-06 20:29:01 +0000
commit009505452b713ed2e3a8e99c5545a6e721c65495 (patch)
tree136a71c5b87bdf534d1f20a67558b49226b5a4d6 /include/llvm/Tools
parent8d0afd3d32d1d67f9aa5df250a1d6955aa8f1ac9 (diff)
downloadllvm-009505452b713ed2e3a8e99c5545a6e721c65495.tar.gz
llvm-009505452b713ed2e3a8e99c5545a6e721c65495.tar.bz2
llvm-009505452b713ed2e3a8e99c5545a6e721c65495.tar.xz
Initial revision
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/llvm/Tools')
-rw-r--r--include/llvm/Tools/CommandLine.h126
-rw-r--r--include/llvm/Tools/DataTypes.h26
-rw-r--r--include/llvm/Tools/StringExtras.h63
3 files changed, 215 insertions, 0 deletions
diff --git a/include/llvm/Tools/CommandLine.h b/include/llvm/Tools/CommandLine.h
new file mode 100644
index 0000000000..76b4e97989
--- /dev/null
+++ b/include/llvm/Tools/CommandLine.h
@@ -0,0 +1,126 @@
+//===-- llvm/Tools/CommandLine.h - Command line parser for tools -*- C++ -*--=//
+//
+// This class implements a command line argument processor that is useful when
+// creating a tool.
+//
+// This class is defined entirely inline so that you don't have to link to any
+// libraries to use this.
+//
+// TODO: make this extensible by passing in arguments to be read.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_TOOLS_COMMANDLINE_H
+#define LLVM_TOOLS_COMMANDLINE_H
+
+#include <string>
+
+class ToolCommandLine {
+public:
+ inline ToolCommandLine(int &argc, char **argv, bool OutputBytecode = true);
+ inline ToolCommandLine(const string &infn, const string &outfn = "-");
+ inline ToolCommandLine(const ToolCommandLine &O);
+ inline ToolCommandLine &operator=(const ToolCommandLine &O);
+
+ inline bool getForce() const { return Force; }
+ inline const string getInputFilename() const { return InputFilename; }
+ inline const string getOutputFilename() const { return OutputFilename; }
+
+private:
+ void calculateOutputFilename(bool OutputBytecode) {
+ OutputFilename = InputFilename;
+ unsigned Len = OutputFilename.length();
+
+ if (Len <= 3) {
+ OutputFilename += (OutputBytecode ? ".bc" : ".ll");
+ return;
+ }
+
+ if (OutputBytecode) {
+ if (OutputFilename[Len-3] == '.' &&
+ OutputFilename[Len-2] == 'l' &&
+ OutputFilename[Len-1] == 'l') { // .ll -> .bc
+ OutputFilename[Len-2] = 'b';
+ OutputFilename[Len-1] = 'c';
+ } else {
+ OutputFilename += ".bc";
+ }
+ } else {
+ if (OutputFilename[Len-3] == '.' &&
+ OutputFilename[Len-2] == 'b' &&
+ OutputFilename[Len-1] == 'c') { // .ll -> .bc
+ OutputFilename[Len-2] = 'l';
+ OutputFilename[Len-1] = 'l';
+ } else {
+ OutputFilename += ".ll";
+ }
+ }
+ }
+
+private:
+ string InputFilename; // Filename to read from. If "-", use stdin.
+ string OutputFilename; // Filename to write to. If "-", use stdout.
+ bool Force; // Force output (-f argument)
+};
+
+inline ToolCommandLine::ToolCommandLine(int &argc, char **argv, bool OutBC)
+ : InputFilename("-"), OutputFilename("-"), Force(false) {
+ bool FoundInputFN = false;
+ bool FoundOutputFN = false;
+ bool FoundForce = false;
+
+ for (int i = 1; i < argc; i++) {
+ int RemoveArg = 0;
+
+ if (argv[i][0] == '-') {
+ if (!FoundInputFN && argv[i][1] == 0) { // Is the current argument '-'
+ InputFilename = argv[i];
+ FoundInputFN = true;
+ RemoveArg = 1;
+ } else if (!FoundOutputFN && (argv[i][1] == 'o' && argv[i][2] == 0)) {
+ // Is the argument -o?
+ if (i+1 < argc) { // Next arg is output fn
+ OutputFilename = argv[i+1];
+ FoundOutputFN = true;
+ RemoveArg = 2;
+ }
+ } else if (!FoundForce && (argv[i][1] == 'f' && argv[i][2] == 0)) {
+ Force = true;
+ FoundForce = true;
+ RemoveArg = 1;
+ }
+ } else if (!FoundInputFN) { // Is the current argument '[^-].*'?
+ InputFilename = argv[i];
+ FoundInputFN = true;
+ RemoveArg = 1;
+ }
+
+ if (RemoveArg) {
+ argc -= RemoveArg; // Shift args over...
+ memmove(argv+i, argv+i+RemoveArg, (argc-i)*sizeof(char*));
+ i--; // Reprocess this argument...
+ }
+ }
+
+ if (!FoundOutputFN && InputFilename != "-")
+ calculateOutputFilename(OutBC);
+}
+
+inline ToolCommandLine::ToolCommandLine(const string &inf,
+ const string &outf)
+ : InputFilename(inf), OutputFilename(outf), Force(false) {
+}
+
+inline ToolCommandLine::ToolCommandLine(const ToolCommandLine &Opts)
+ : InputFilename(Opts.InputFilename), OutputFilename(Opts.OutputFilename),
+ Force(Opts.Force) {
+}
+
+inline ToolCommandLine &ToolCommandLine::operator=(const ToolCommandLine &Opts){
+ InputFilename = Opts.InputFilename;
+ OutputFilename = Opts.OutputFilename;
+ Force = Opts.Force;
+ return *this;
+}
+
+#endif
diff --git a/include/llvm/Tools/DataTypes.h b/include/llvm/Tools/DataTypes.h
new file mode 100644
index 0000000000..ada16c24da
--- /dev/null
+++ b/include/llvm/Tools/DataTypes.h
@@ -0,0 +1,26 @@
+
+// TODO: This file sucks. Not only does it not work, but this stuff should be
+// autoconfiscated anyways. Major FIXME
+
+
+#ifndef LLVM_TOOLS_DATATYPES_H
+#define LLVM_TOOLS_DATATYPES_H
+
+// Should define the following:
+// LITTLE_ENDIAN if applicable
+// int64_t
+// uint64_t
+
+#ifdef LINUX
+#include <stdint.h> // Defined by ISO C 99
+#include <endian.h>
+
+#else
+#include <sys/types.h>
+#ifdef _LITTLE_ENDIAN
+#define LITTLE_ENDIAN 1
+#endif
+#endif
+
+
+#endif
diff --git a/include/llvm/Tools/StringExtras.h b/include/llvm/Tools/StringExtras.h
new file mode 100644
index 0000000000..31dedb4248
--- /dev/null
+++ b/include/llvm/Tools/StringExtras.h
@@ -0,0 +1,63 @@
+//===-- StringExtras.h - Useful string functions -----------------*- C++ -*--=//
+//
+// This file contains some functions that are useful when dealing with strings.
+// No library is required when using these functinons.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_TOOLS_STRING_EXTRAS_H
+#define LLVM_TOOLS_STRING_EXTRAS_H
+
+#include <string>
+#include "llvm/Tools/DataTypes.h"
+
+static inline string utostr(uint64_t X, bool isNeg = false) {
+ char Buffer[40];
+ char *BufPtr = Buffer+39;
+
+ *BufPtr = 0; // Null terminate buffer...
+ if (X == 0) *--BufPtr = '0'; // Handle special case...
+
+ while (X) {
+ *--BufPtr = '0' + (X % 10);
+ X /= 10;
+ }
+
+ if (isNeg) *--BufPtr = '-'; // Add negative sign...
+
+ return string(BufPtr);
+}
+
+static inline string itostr(int64_t X) {
+ if (X < 0)
+ return utostr((uint64_t)-X, true);
+ else
+ return utostr((uint64_t)X);
+}
+
+
+static inline string utostr(unsigned X, bool isNeg = false) {
+ char Buffer[20];
+ char *BufPtr = Buffer+19;
+
+ *BufPtr = 0; // Null terminate buffer...
+ if (X == 0) *--BufPtr = '0'; // Handle special case...
+
+ while (X) {
+ *--BufPtr = '0' + (X % 10);
+ X /= 10;
+ }
+
+ if (isNeg) *--BufPtr = '-'; // Add negative sign...
+
+ return string(BufPtr);
+}
+
+static inline string itostr(int X) {
+ if (X < 0)
+ return utostr((unsigned)-X, true);
+ else
+ return utostr((unsigned)X);
+}
+
+#endif