blob: 1cce7cb8d3c70dbcc9eb70efb65fe297483e5660 [file] [log] [blame]
Chris Lattner996fe012002-12-24 00:01:05 +00001//===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===//
Misha Brukman91fb9ab2005-04-21 22:43:08 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
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.
Misha Brukman91fb9ab2005-04-21 22:43:08 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner996fe012002-12-24 00:01:05 +00009//
10// This file implements the top-level functionality for the LLVM interpreter.
11// This interpreter is designed to be a very simple, portable, inefficient
12// interpreter.
13//
14//===----------------------------------------------------------------------===//
15
16#include "Interpreter.h"
Chris Lattnerbcdadf32004-06-20 07:49:54 +000017#include "llvm/CodeGen/IntrinsicLowering.h"
Brian Gaeke84217652003-09-05 04:46:26 +000018#include "llvm/DerivedTypes.h"
Chris Lattnerc8c6c032003-12-28 09:44:37 +000019#include "llvm/Module.h"
Chris Lattner2d52c1b2006-03-22 06:07:50 +000020#include "llvm/ModuleProvider.h"
Chris Lattner18099662003-12-14 23:25:48 +000021using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000022
Chris Lattner2d52c1b2006-03-22 06:07:50 +000023static struct RegisterInterp {
24 RegisterInterp() { Interpreter::Register(); }
25} InterpRegistrator;
26
Jeff Cohen0eafbc32006-03-24 02:53:49 +000027namespace llvm {
28 void LinkInInterpreter() {
29 }
30}
31
Brian Gaeke4bd3bd52003-09-03 20:34:19 +000032/// create - Create a new interpreter object. This can never fail.
Chris Lattner996fe012002-12-24 00:01:05 +000033///
Chris Lattner0b2de9f2006-03-23 05:22:51 +000034ExecutionEngine *Interpreter::create(ModuleProvider *MP) {
Chris Lattner2d52c1b2006-03-22 06:07:50 +000035 Module *M;
36 try {
37 M = MP->materializeModule();
38 } catch (...) {
39 return 0; // error materializing the module.
40 }
41
Reid Spencer3ac38e92007-01-26 08:11:39 +000042 // FIXME: This should probably compute the entire data layout
43 std::string DataLayout;
44 int Test = 0;
45 *(char*)&Test = 1; // Return true if the host is little endian
46 bool isLittleEndian = (Test == 1);
47 DataLayout.append(isLittleEndian ? "e" : "E");
Chris Lattner4d4f4242003-08-24 19:50:53 +000048
Reid Spencer42b2c082007-01-29 17:55:50 +000049 bool Ptr64 = sizeof(void*) == 8;
50 DataLayout.append(Ptr64 ? "-p:64:64" : "-p:32:32");
Reid Spencer3ac38e92007-01-26 08:11:39 +000051
52 M->setDataLayout(DataLayout);
Chris Lattner4d4f4242003-08-24 19:50:53 +000053
Chris Lattner91f228b2006-06-16 18:08:38 +000054 return new Interpreter(M);
Chris Lattner996fe012002-12-24 00:01:05 +000055}
56
57//===----------------------------------------------------------------------===//
58// Interpreter ctor - Initialize stuff
59//
Chris Lattner6c790052006-06-16 18:24:38 +000060Interpreter::Interpreter(Module *M) : ExecutionEngine(M), TD(M) {
Chris Lattner91f228b2006-06-16 18:08:38 +000061
Jeff Cohen24396692006-02-07 05:29:44 +000062 memset(&ExitValue, 0, sizeof(ExitValue));
Owen Anderson20a631f2006-05-03 01:29:57 +000063 setTargetData(&TD);
Chris Lattner996fe012002-12-24 00:01:05 +000064 // Initialize the "backend"
65 initializeExecutionEngine();
Chris Lattner470754e2003-05-08 16:18:31 +000066 initializeExternalFunctions();
Chris Lattnerb78244f2003-05-12 02:14:34 +000067 emitGlobals();
Chris Lattnerc8c6c032003-12-28 09:44:37 +000068
Reid Spencer1e960cd2007-01-29 17:51:02 +000069 IL = new IntrinsicLowering(TD);
Chris Lattnerc8c6c032003-12-28 09:44:37 +000070}
71
72Interpreter::~Interpreter() {
73 delete IL;
Chris Lattner996fe012002-12-24 00:01:05 +000074}
75
Brian Gaekea7669032003-09-05 18:42:01 +000076void Interpreter::runAtExitHandlers () {
77 while (!AtExitHandlers.empty()) {
78 callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
79 AtExitHandlers.pop_back();
Chris Lattner996fe012002-12-24 00:01:05 +000080 run();
81 }
Chris Lattner996fe012002-12-24 00:01:05 +000082}
83
Brian Gaekea7669032003-09-05 18:42:01 +000084/// run - Start execution with the specified function and arguments.
85///
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000086GenericValue
Misha Brukman5191b4b2005-04-22 04:08:30 +000087Interpreter::runFunction(Function *F,
88 const std::vector<GenericValue> &ArgValues) {
Brian Gaekea7669032003-09-05 18:42:01 +000089 assert (F && "Function *F was null at entry to run()");
Brian Gaeke84217652003-09-05 04:46:26 +000090
Brian Gaekea7669032003-09-05 18:42:01 +000091 // 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,
97 // though.
98 std::vector<GenericValue> ActualArgs;
Chris Lattnerfa829be2004-02-09 04:14:01 +000099 const unsigned ArgCount = F->getFunctionType()->getNumParams();
Brian Gaeke65c60e22003-10-24 19:59:28 +0000100 for (unsigned i = 0; i < ArgCount; ++i)
Chris Lattnerfa829be2004-02-09 04:14:01 +0000101 ActualArgs.push_back(ArgValues[i]);
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000102
Brian Gaekea7669032003-09-05 18:42:01 +0000103 // Set up the function call.
104 callFunction(F, ActualArgs);
Brian Gaeke84217652003-09-05 04:46:26 +0000105
Brian Gaekea7669032003-09-05 18:42:01 +0000106 // Start executing the function.
107 run();
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000108
Jeff Cohen24396692006-02-07 05:29:44 +0000109 return ExitValue;
Brian Gaeke84217652003-09-05 04:46:26 +0000110}
Brian Gaeke960707c2003-11-11 22:41:34 +0000111