blob: 4728c95b7439344139216d3255afe71c4377b597 [file] [log] [blame]
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001//===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===//
Misha Brukmand1c881a2005-04-21 22:43:08 +00002//
John Criswellb576c942003-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 Brukmand1c881a2005-04-21 22:43:08 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerbd199fb2002-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 Lattner30483732004-06-20 07:49:54 +000017#include "llvm/CodeGen/IntrinsicLowering.h"
Brian Gaeke413ab662003-09-05 04:46:26 +000018#include "llvm/DerivedTypes.h"
Chris Lattner73011782003-12-28 09:44:37 +000019#include "llvm/Module.h"
Chris Lattner2fe4bb02006-03-22 06:07:50 +000020#include "llvm/ModuleProvider.h"
Chris Lattnerf7a743d2003-12-14 23:25:48 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Chris Lattner2fe4bb02006-03-22 06:07:50 +000023static struct RegisterInterp {
24 RegisterInterp() { Interpreter::Register(); }
25} InterpRegistrator;
26
Jeff Cohen2f519142006-03-24 02:53:49 +000027namespace llvm {
28 void LinkInInterpreter() {
29 }
30}
31
Brian Gaeke82d82772003-09-03 20:34:19 +000032/// create - Create a new interpreter object. This can never fail.
Chris Lattnerbd199fb2002-12-24 00:01:05 +000033///
Chris Lattner726c1ef2006-03-23 05:22:51 +000034ExecutionEngine *Interpreter::create(ModuleProvider *MP) {
Chris Lattner2fe4bb02006-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
Chris Lattner276f4b52006-06-16 18:08:38 +000042 if (M->getEndianness() == Module::AnyEndianness) {
Chris Lattner39c07262003-08-24 19:50:53 +000043 int Test = 0;
44 *(char*)&Test = 1; // Return true if the host is little endian
Chris Lattner276f4b52006-06-16 18:08:38 +000045 bool isLittleEndian = (Test == 1);
46 M->setEndianness(isLittleEndian ? Module::LittleEndian : Module::BigEndian);
Chris Lattner39c07262003-08-24 19:50:53 +000047 }
48
Chris Lattner276f4b52006-06-16 18:08:38 +000049 if (M->getPointerSize() == Module::AnyPointerSize) {
50 // Follow host.
51 bool Ptr64 = sizeof(void*) == 8;
52 M->setPointerSize(Ptr64 ? Module::Pointer64 : Module::Pointer32);
Chris Lattner39c07262003-08-24 19:50:53 +000053 }
54
Chris Lattner276f4b52006-06-16 18:08:38 +000055 return new Interpreter(M);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000056}
57
58//===----------------------------------------------------------------------===//
59// Interpreter ctor - Initialize stuff
60//
Chris Lattner426b7822006-06-16 18:24:38 +000061Interpreter::Interpreter(Module *M) : ExecutionEngine(M), TD(M) {
Chris Lattner276f4b52006-06-16 18:08:38 +000062
Jeff Cohen8c9191c2006-02-07 05:29:44 +000063 memset(&ExitValue, 0, sizeof(ExitValue));
Owen Andersona69571c2006-05-03 01:29:57 +000064 setTargetData(&TD);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000065 // Initialize the "backend"
66 initializeExecutionEngine();
Chris Lattnerda82ed52003-05-08 16:18:31 +000067 initializeExternalFunctions();
Chris Lattner56adf152003-05-12 02:14:34 +000068 emitGlobals();
Chris Lattner73011782003-12-28 09:44:37 +000069
Chris Lattnerb71fd782006-11-15 18:00:10 +000070 IL = new IntrinsicLowering();
Chris Lattner73011782003-12-28 09:44:37 +000071}
72
73Interpreter::~Interpreter() {
74 delete IL;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000075}
76
Brian Gaeke70975ee2003-09-05 18:42:01 +000077void Interpreter::runAtExitHandlers () {
78 while (!AtExitHandlers.empty()) {
79 callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
80 AtExitHandlers.pop_back();
Chris Lattnerbd199fb2002-12-24 00:01:05 +000081 run();
82 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000083}
84
Brian Gaeke70975ee2003-09-05 18:42:01 +000085/// run - Start execution with the specified function and arguments.
86///
Jeff Cohen00b168892005-07-27 06:12:32 +000087GenericValue
Misha Brukman3c944972005-04-22 04:08:30 +000088Interpreter::runFunction(Function *F,
89 const std::vector<GenericValue> &ArgValues) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000090 assert (F && "Function *F was null at entry to run()");
Brian Gaeke413ab662003-09-05 04:46:26 +000091
Brian Gaeke70975ee2003-09-05 18:42:01 +000092 // Try extra hard not to pass extra args to a function that isn't
93 // expecting them. C programmers frequently bend the rules and
94 // declare main() with fewer parameters than it actually gets
95 // passed, and the interpreter barfs if you pass a function more
96 // parameters than it is declared to take. This does not attempt to
97 // take into account gratuitous differences in declared types,
98 // though.
99 std::vector<GenericValue> ActualArgs;
Chris Lattnerd5d89962004-02-09 04:14:01 +0000100 const unsigned ArgCount = F->getFunctionType()->getNumParams();
Brian Gaekea824f422003-10-24 19:59:28 +0000101 for (unsigned i = 0; i < ArgCount; ++i)
Chris Lattnerd5d89962004-02-09 04:14:01 +0000102 ActualArgs.push_back(ArgValues[i]);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000103
Brian Gaeke70975ee2003-09-05 18:42:01 +0000104 // Set up the function call.
105 callFunction(F, ActualArgs);
Brian Gaeke413ab662003-09-05 04:46:26 +0000106
Brian Gaeke70975ee2003-09-05 18:42:01 +0000107 // Start executing the function.
108 run();
Misha Brukmand1c881a2005-04-21 22:43:08 +0000109
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000110 return ExitValue;
Brian Gaeke413ab662003-09-05 04:46:26 +0000111}
Brian Gaeked0fde302003-11-11 22:41:34 +0000112