blob: 0f73189a9e52841fd7708e2ee56f0b6db627c7b2 [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
Brian Gaeke82d82772003-09-03 20:34:19 +000027/// create - Create a new interpreter object. This can never fail.
Chris Lattnerbd199fb2002-12-24 00:01:05 +000028///
Chris Lattner2fe4bb02006-03-22 06:07:50 +000029ExecutionEngine *Interpreter::create(ModuleProvider *MP,
30 IntrinsicLowering *IL) {
31 Module *M;
32 try {
33 M = MP->materializeModule();
34 } catch (...) {
35 return 0; // error materializing the module.
36 }
37
Chris Lattner021c1902003-09-22 20:33:34 +000038 bool isLittleEndian = false;
Chris Lattner39c07262003-08-24 19:50:53 +000039 switch (M->getEndianness()) {
40 case Module::LittleEndian: isLittleEndian = true; break;
41 case Module::BigEndian: isLittleEndian = false; break;
42 case Module::AnyPointerSize:
43 int Test = 0;
44 *(char*)&Test = 1; // Return true if the host is little endian
45 isLittleEndian = (Test == 1);
46 break;
47 }
48
Chris Lattner021c1902003-09-22 20:33:34 +000049 bool isLongPointer = false;
Chris Lattner39c07262003-08-24 19:50:53 +000050 switch (M->getPointerSize()) {
51 case Module::Pointer32: isLongPointer = false; break;
52 case Module::Pointer64: isLongPointer = true; break;
53 case Module::AnyPointerSize:
54 isLongPointer = (sizeof(void*) == 8); // Follow host
55 break;
56 }
57
Chris Lattner73011782003-12-28 09:44:37 +000058 return new Interpreter(M, isLittleEndian, isLongPointer, IL);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000059}
60
61//===----------------------------------------------------------------------===//
62// Interpreter ctor - Initialize stuff
63//
Chris Lattner73011782003-12-28 09:44:37 +000064Interpreter::Interpreter(Module *M, bool isLittleEndian, bool isLongPointer,
65 IntrinsicLowering *il)
Jeff Cohen8c9191c2006-02-07 05:29:44 +000066 : ExecutionEngine(M),
Brian Gaekea824f422003-10-24 19:59:28 +000067 TD("lli", isLittleEndian, isLongPointer ? 8 : 4, isLongPointer ? 8 : 4,
Chris Lattner73011782003-12-28 09:44:37 +000068 isLongPointer ? 8 : 4), IL(il) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +000069
Jeff Cohen8c9191c2006-02-07 05:29:44 +000070 memset(&ExitValue, 0, sizeof(ExitValue));
Chris Lattnerbd199fb2002-12-24 00:01:05 +000071 setTargetData(TD);
72 // Initialize the "backend"
73 initializeExecutionEngine();
Chris Lattnerda82ed52003-05-08 16:18:31 +000074 initializeExternalFunctions();
Chris Lattner56adf152003-05-12 02:14:34 +000075 emitGlobals();
Chris Lattner73011782003-12-28 09:44:37 +000076
77 if (IL == 0) IL = new DefaultIntrinsicLowering();
78}
79
80Interpreter::~Interpreter() {
81 delete IL;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000082}
83
Brian Gaeke70975ee2003-09-05 18:42:01 +000084void Interpreter::runAtExitHandlers () {
85 while (!AtExitHandlers.empty()) {
86 callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
87 AtExitHandlers.pop_back();
Chris Lattnerbd199fb2002-12-24 00:01:05 +000088 run();
89 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000090}
91
Brian Gaeke70975ee2003-09-05 18:42:01 +000092/// run - Start execution with the specified function and arguments.
93///
Jeff Cohen00b168892005-07-27 06:12:32 +000094GenericValue
Misha Brukman3c944972005-04-22 04:08:30 +000095Interpreter::runFunction(Function *F,
96 const std::vector<GenericValue> &ArgValues) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000097 assert (F && "Function *F was null at entry to run()");
Brian Gaeke413ab662003-09-05 04:46:26 +000098
Brian Gaeke70975ee2003-09-05 18:42:01 +000099 // Try extra hard not to pass extra args to a function that isn't
100 // expecting them. C programmers frequently bend the rules and
101 // declare main() with fewer parameters than it actually gets
102 // passed, and the interpreter barfs if you pass a function more
103 // parameters than it is declared to take. This does not attempt to
104 // take into account gratuitous differences in declared types,
105 // though.
106 std::vector<GenericValue> ActualArgs;
Chris Lattnerd5d89962004-02-09 04:14:01 +0000107 const unsigned ArgCount = F->getFunctionType()->getNumParams();
Brian Gaekea824f422003-10-24 19:59:28 +0000108 for (unsigned i = 0; i < ArgCount; ++i)
Chris Lattnerd5d89962004-02-09 04:14:01 +0000109 ActualArgs.push_back(ArgValues[i]);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000110
Brian Gaeke70975ee2003-09-05 18:42:01 +0000111 // Set up the function call.
112 callFunction(F, ActualArgs);
Brian Gaeke413ab662003-09-05 04:46:26 +0000113
Brian Gaeke70975ee2003-09-05 18:42:01 +0000114 // Start executing the function.
115 run();
Misha Brukmand1c881a2005-04-21 22:43:08 +0000116
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000117 return ExitValue;
Brian Gaeke413ab662003-09-05 04:46:26 +0000118}
Brian Gaeked0fde302003-11-11 22:41:34 +0000119