Use CreateStackStoreLoad helper in more places.
[oota-llvm.git] / lib / CompilerDriver / Action.cpp
1 //===--- Action.cpp - The LLVM Compiler Driver ------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open
6 // Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  Action class - implementation and auxiliary functions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/CompilerDriver/Action.h"
15 #include "llvm/CompilerDriver/BuiltinOptions.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include "llvm/System/Program.h"
18 #include <stdexcept>
19
20 using namespace llvm;
21 using namespace llvmc;
22
23 namespace {
24   int ExecuteProgram(const std::string& name,
25                      const StrVector& args) {
26     sys::Path prog = sys::Program::FindProgramByName(name);
27
28     if (prog.isEmpty())
29       throw std::runtime_error("Can't find program '" + name + "'");
30     if (!prog.canExecute())
31       throw std::runtime_error("Program '" + name + "' is not executable.");
32
33     // Build the command line vector and the redirects array.
34     const sys::Path* redirects[3] = {0,0,0};
35     sys::Path stdout_redirect;
36
37     std::vector<const char*> argv;
38     argv.reserve((args.size()+2));
39     argv.push_back(name.c_str());
40
41     for (StrVector::const_iterator B = args.begin(), E = args.end();
42          B!=E; ++B) {
43       if (*B == ">") {
44         ++B;
45         stdout_redirect.set(*B);
46         redirects[1] = &stdout_redirect;
47       }
48       else {
49         argv.push_back((*B).c_str());
50       }
51     }
52     argv.push_back(0);  // null terminate list.
53
54     // Invoke the program.
55     return sys::Program::ExecuteAndWait(prog, &argv[0], 0, &redirects[0]);
56   }
57
58   void print_string (const std::string& str) {
59     errs() << str << ' ';
60   }
61 }
62
63 int llvmc::Action::Execute() const {
64   if (DryRun || VerboseMode) {
65     errs() << Command_ << " ";
66     std::for_each(Args_.begin(), Args_.end(), print_string);
67     errs() << '\n';
68   }
69   if (DryRun)
70     return 0;
71   else
72     return ExecuteProgram(Command_, Args_);
73 }