blob: 5d7f9018542c512792495da687d8bd6ffa7a97b1 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000021#include <cstring>
Chris Lattnerf7a743d2003-12-14 23:25:48 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattner2fe4bb02006-03-22 06:07:50 +000024static struct RegisterInterp {
25 RegisterInterp() { Interpreter::Register(); }
26} InterpRegistrator;
27
Jeff Cohen2f519142006-03-24 02:53:49 +000028namespace llvm {
29 void LinkInInterpreter() {
30 }
31}
32
Brian Gaeke82d82772003-09-03 20:34:19 +000033/// create - Create a new interpreter object. This can never fail.
Chris Lattnerbd199fb2002-12-24 00:01:05 +000034///
Reid Spencerd4c0e622007-03-03 18:19:18 +000035ExecutionEngine *Interpreter::create(ModuleProvider *MP, std::string* ErrStr) {
36 // Tell this ModuleProvide to materialize and release the module
Chris Lattner9f2f1422007-12-06 01:08:09 +000037 if (!MP->materializeModule(ErrStr))
Reid Spencerd4c0e622007-03-03 18:19:18 +000038 // We got an error, just return 0
39 return 0;
40
Chris Lattner9f2f1422007-12-06 01:08:09 +000041 return new Interpreter(MP);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000042}
43
44//===----------------------------------------------------------------------===//
45// Interpreter ctor - Initialize stuff
46//
Chris Lattner9f2f1422007-12-06 01:08:09 +000047Interpreter::Interpreter(ModuleProvider *M)
48 : ExecutionEngine(M), TD(M->getModule()) {
Chris Lattner276f4b52006-06-16 18:08:38 +000049
Reid Spencere7707872007-06-01 22:23:29 +000050 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
Owen Andersona69571c2006-05-03 01:29:57 +000051 setTargetData(&TD);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000052 // Initialize the "backend"
53 initializeExecutionEngine();
Chris Lattnerda82ed52003-05-08 16:18:31 +000054 initializeExternalFunctions();
Chris Lattner56adf152003-05-12 02:14:34 +000055 emitGlobals();
Chris Lattner73011782003-12-28 09:44:37 +000056
Reid Spencer519e2392007-01-29 17:51:02 +000057 IL = new IntrinsicLowering(TD);
Chris Lattner73011782003-12-28 09:44:37 +000058}
59
60Interpreter::~Interpreter() {
61 delete IL;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000062}
63
Brian Gaeke70975ee2003-09-05 18:42:01 +000064void Interpreter::runAtExitHandlers () {
65 while (!AtExitHandlers.empty()) {
66 callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
67 AtExitHandlers.pop_back();
Chris Lattnerbd199fb2002-12-24 00:01:05 +000068 run();
69 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000070}
71
Brian Gaeke70975ee2003-09-05 18:42:01 +000072/// run - Start execution with the specified function and arguments.
73///
Jeff Cohen00b168892005-07-27 06:12:32 +000074GenericValue
Misha Brukman3c944972005-04-22 04:08:30 +000075Interpreter::runFunction(Function *F,
76 const std::vector<GenericValue> &ArgValues) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000077 assert (F && "Function *F was null at entry to run()");
Brian Gaeke413ab662003-09-05 04:46:26 +000078
Brian Gaeke70975ee2003-09-05 18:42:01 +000079 // Try extra hard not to pass extra args to a function that isn't
80 // expecting them. C programmers frequently bend the rules and
81 // declare main() with fewer parameters than it actually gets
82 // passed, and the interpreter barfs if you pass a function more
83 // parameters than it is declared to take. This does not attempt to
84 // take into account gratuitous differences in declared types,
85 // though.
86 std::vector<GenericValue> ActualArgs;
Chris Lattnerd5d89962004-02-09 04:14:01 +000087 const unsigned ArgCount = F->getFunctionType()->getNumParams();
Brian Gaekea824f422003-10-24 19:59:28 +000088 for (unsigned i = 0; i < ArgCount; ++i)
Chris Lattnerd5d89962004-02-09 04:14:01 +000089 ActualArgs.push_back(ArgValues[i]);
Misha Brukmand1c881a2005-04-21 22:43:08 +000090
Brian Gaeke70975ee2003-09-05 18:42:01 +000091 // Set up the function call.
92 callFunction(F, ActualArgs);
Brian Gaeke413ab662003-09-05 04:46:26 +000093
Brian Gaeke70975ee2003-09-05 18:42:01 +000094 // Start executing the function.
95 run();
Misha Brukmand1c881a2005-04-21 22:43:08 +000096
Jeff Cohen8c9191c2006-02-07 05:29:44 +000097 return ExitValue;
Brian Gaeke413ab662003-09-05 04:46:26 +000098}
Brian Gaeked0fde302003-11-11 22:41:34 +000099