1 //===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the top-level functionality for the LLVM interpreter.
11 // This interpreter is designed to be a very simple, portable, inefficient
14 //===----------------------------------------------------------------------===//
16 #include "Interpreter.h"
17 #include "llvm/CodeGen/IntrinsicLowering.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Module.h"
20 #include "llvm/ModuleProvider.h"
23 static struct RegisterInterp {
24 RegisterInterp() { Interpreter::Register(); }
28 void LinkInInterpreter() {
32 /// create - Create a new interpreter object. This can never fail.
34 ExecutionEngine *Interpreter::create(ModuleProvider *MP) {
37 M = MP->materializeModule();
39 return 0; // error materializing the module.
42 // FIXME: This should probably compute the entire data layout
43 std::string DataLayout;
45 *(char*)&Test = 1; // Return true if the host is little endian
46 bool isLittleEndian = (Test == 1);
47 DataLayout.append(isLittleEndian ? "e" : "E");
49 bool Ptr64 = sizeof(void*) == 8;
50 DataLayout.append(Ptr64 ? "-p:64:64" : "-p:32:32");
52 M->setDataLayout(DataLayout);
54 return new Interpreter(M);
57 //===----------------------------------------------------------------------===//
58 // Interpreter ctor - Initialize stuff
60 Interpreter::Interpreter(Module *M) : ExecutionEngine(M), TD(M) {
62 memset(&ExitValue, 0, sizeof(ExitValue));
64 // Initialize the "backend"
65 initializeExecutionEngine();
66 initializeExternalFunctions();
69 IL = new IntrinsicLowering(TD);
72 Interpreter::~Interpreter() {
76 void Interpreter::runAtExitHandlers () {
77 while (!AtExitHandlers.empty()) {
78 callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
79 AtExitHandlers.pop_back();
84 /// run - Start execution with the specified function and arguments.
87 Interpreter::runFunction(Function *F,
88 const std::vector<GenericValue> &ArgValues) {
89 assert (F && "Function *F was null at entry to run()");
91 // Try extra hard not to pass extra args to a function that isn't
92 // expecting them. C programmers frequently bend the rules and
93 // declare main() with fewer parameters than it actually gets
94 // passed, and the interpreter barfs if you pass a function more
95 // parameters than it is declared to take. This does not attempt to
96 // take into account gratuitous differences in declared types,
98 std::vector<GenericValue> ActualArgs;
99 const unsigned ArgCount = F->getFunctionType()->getNumParams();
100 for (unsigned i = 0; i < ArgCount; ++i)
101 ActualArgs.push_back(ArgValues[i]);
103 // Set up the function call.
104 callFunction(F, ActualArgs);
106 // Start executing the function.