fix PR7311 by avoiding breaking casts when a bitcast from scalar->vector
[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
17 #include "llvm/Support/raw_ostream.h"
18 #include "llvm/Support/SystemUtils.h"
19 #include "llvm/System/Program.h"
20 #include "llvm/System/TimeValue.h"
21
22 #include <stdexcept>
23 #include <string>
24
25 using namespace llvm;
26 using namespace llvmc;
27
28 namespace llvmc {
29
30 extern int Main(int argc, char** argv);
31 extern const char* ProgramName;
32
33 }
34
35 namespace {
36
37   void PrintString (const std::string& str) {
38     errs() << str << ' ';
39   }
40
41   void PrintCommand (const std::string& Cmd, const StrVector& Args) {
42     errs() << Cmd << ' ';
43     std::for_each(Args.begin(), Args.end(), &PrintString);
44     errs() << '\n';
45   }
46
47   bool IsSegmentationFault (int returnCode) {
48 #ifdef LLVM_ON_WIN32
49     return (returnCode >= 0xc0000000UL)
50 #else
51     return (returnCode < 0);
52 #endif
53   }
54
55   int ExecuteProgram (const std::string& name,
56                       const StrVector& args) {
57     sys::Path prog = sys::Program::FindProgramByName(name);
58
59     if (prog.isEmpty()) {
60       prog = FindExecutable(name, ProgramName, (void *)(intptr_t)&Main);
61       if (prog.isEmpty())
62         throw std::runtime_error("Can't find program '" + name + "'");
63     }
64     if (!prog.canExecute())
65       throw std::runtime_error("Program '" + name + "' is not executable.");
66
67     // Build the command line vector and the redirects array.
68     const sys::Path* redirects[3] = {0,0,0};
69     sys::Path stdout_redirect;
70
71     std::vector<const char*> argv;
72     argv.reserve((args.size()+2));
73     argv.push_back(name.c_str());
74
75     for (StrVector::const_iterator B = args.begin(), E = args.end();
76          B!=E; ++B) {
77       if (*B == ">") {
78         ++B;
79         stdout_redirect.set(*B);
80         redirects[1] = &stdout_redirect;
81       }
82       else {
83         argv.push_back((*B).c_str());
84       }
85     }
86     argv.push_back(0);  // null terminate list.
87
88     // Invoke the program.
89     int ret = sys::Program::ExecuteAndWait(prog, &argv[0], 0, &redirects[0]);
90
91     if (IsSegmentationFault(ret)) {
92       errs() << "Segmentation fault: ";
93       PrintCommand(name, args);
94     }
95
96     return ret;
97   }
98 }
99
100 namespace llvmc {
101   void AppendToGlobalTimeLog (const std::string& cmd, double time);
102 }
103
104 int llvmc::Action::Execute () const {
105   if (DryRun || VerboseMode)
106     PrintCommand(Command_, Args_);
107
108   if (!DryRun) {
109     if (Time) {
110       sys::TimeValue now = sys::TimeValue::now();
111       int ret = ExecuteProgram(Command_, Args_);
112       sys::TimeValue now2 = sys::TimeValue::now();
113       now2 -= now;
114       double elapsed = now2.seconds()  + now2.microseconds()  / 1000000.0;
115       AppendToGlobalTimeLog(Command_, elapsed);
116
117       return ret;
118     }
119     else {
120       return ExecuteProgram(Command_, Args_);
121     }
122   }
123
124   return 0;
125 }