blob: 96d5d56cb2a784feb74f26c4e7e27b78c2d6114f [file] [log] [blame]
Chris Lattner62b7fd12002-04-07 20:49:59 +00001//===-- ExternalFunctions.cpp - Implement External Functions --------------===//
Chris Lattnerbaf08eb2001-09-10 04:50:17 +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.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner62b7fd12002-04-07 20:49:59 +000010// This file contains both code to deal with invoking "external" functions, but
11// also contains code that implements "exported" external functions.
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000012//
Brian Gaeke6cc20de2003-10-10 17:03:10 +000013// External functions in the interpreter are implemented by
14// using the system's dynamic loader to look up the address of the function
15// we want to invoke. If a function is found, then one of the
16// many lle_* wrapper functions in this file will translate its arguments from
17// GenericValues to the types the function is actually expecting, before the
18// function is called.
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000019//
20//===----------------------------------------------------------------------===//
21
22#include "Interpreter.h"
23#include "llvm/DerivedTypes.h"
Misha Brukman1d2d8ec2003-10-14 21:42:11 +000024#include "llvm/Module.h"
Chris Lattner0313db62002-10-02 21:12:13 +000025#include "llvm/Target/TargetData.h"
Misha Brukman1d2d8ec2003-10-14 21:42:11 +000026#include "Support/DynamicLinker.h"
John Criswell3ef61af2003-06-30 21:59:07 +000027#include "Config/dlfcn.h"
28#include "Config/link.h"
Brian Gaekefcd72642003-06-23 19:41:55 +000029#include <cmath>
Brian Gaeke4e106f02003-11-05 01:18:49 +000030#include <csignal>
Misha Brukman1d2d8ec2003-10-14 21:42:11 +000031#include <map>
Chris Lattner7f74a562002-01-20 22:54:45 +000032using std::vector;
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000033
Chris Lattner18099662003-12-14 23:25:48 +000034using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000035
Chris Lattnerf94811a2002-03-29 03:57:15 +000036typedef GenericValue (*ExFunc)(FunctionType *, const vector<GenericValue> &);
37static std::map<const Function *, ExFunc> Functions;
Chris Lattner7f74a562002-01-20 22:54:45 +000038static std::map<std::string, ExFunc> FuncNames;
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000039
Chris Lattner15157b82001-10-27 04:15:57 +000040static Interpreter *TheInterpreter;
41
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000042static char getTypeID(const Type *Ty) {
43 switch (Ty->getPrimitiveID()) {
44 case Type::VoidTyID: return 'V';
45 case Type::BoolTyID: return 'o';
46 case Type::UByteTyID: return 'B';
47 case Type::SByteTyID: return 'b';
48 case Type::UShortTyID: return 'S';
49 case Type::ShortTyID: return 's';
50 case Type::UIntTyID: return 'I';
51 case Type::IntTyID: return 'i';
52 case Type::ULongTyID: return 'L';
53 case Type::LongTyID: return 'l';
54 case Type::FloatTyID: return 'F';
55 case Type::DoubleTyID: return 'D';
56 case Type::PointerTyID: return 'P';
Chris Lattnerf94811a2002-03-29 03:57:15 +000057 case Type::FunctionTyID: return 'M';
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000058 case Type::StructTyID: return 'T';
59 case Type::ArrayTyID: return 'A';
60 case Type::OpaqueTyID: return 'O';
61 default: return 'U';
62 }
63}
64
Brian Gaeke6cc20de2003-10-10 17:03:10 +000065static ExFunc lookupFunction(const Function *F) {
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000066 // Function not found, look it up... start by figuring out what the
67 // composite function name should be.
Chris Lattner7f74a562002-01-20 22:54:45 +000068 std::string ExtName = "lle_";
Brian Gaeke6cc20de2003-10-10 17:03:10 +000069 const FunctionType *FT = F->getFunctionType();
70 for (unsigned i = 0, e = FT->getNumContainedTypes(); i != e; ++i)
71 ExtName += getTypeID(FT->getContainedType(i));
72 ExtName += "_" + F->getName();
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000073
Chris Lattner7fd51b52001-10-30 20:28:00 +000074 ExFunc FnPtr = FuncNames[ExtName];
75 if (FnPtr == 0)
Brian Gaeke6cc20de2003-10-10 17:03:10 +000076 FnPtr = (ExFunc)GetAddressOfSymbol(ExtName);
Chris Lattner7fd51b52001-10-30 20:28:00 +000077 if (FnPtr == 0)
Brian Gaeke6cc20de2003-10-10 17:03:10 +000078 FnPtr = FuncNames["lle_X_"+F->getName()];
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000079 if (FnPtr == 0) // Try calling a generic function... if it exists...
Brian Gaeke6cc20de2003-10-10 17:03:10 +000080 FnPtr = (ExFunc)GetAddressOfSymbol(("lle_X_"+F->getName()).c_str());
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000081 if (FnPtr != 0)
Brian Gaeke6cc20de2003-10-10 17:03:10 +000082 Functions.insert(std::make_pair(F, FnPtr)); // Cache for later
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000083 return FnPtr;
84}
85
Chris Lattner470754e2003-05-08 16:18:31 +000086GenericValue Interpreter::callExternalFunction(Function *M,
Chris Lattner4a5bb952003-05-14 14:21:30 +000087 const std::vector<GenericValue> &ArgVals) {
Chris Lattner15157b82001-10-27 04:15:57 +000088 TheInterpreter = this;
89
Chris Lattner62b7fd12002-04-07 20:49:59 +000090 // Do a lookup to see if the function is in our cache... this should just be a
Misha Brukmanaa7d26c2003-10-10 17:42:19 +000091 // deferred annotation!
Chris Lattnerf94811a2002-03-29 03:57:15 +000092 std::map<const Function *, ExFunc>::iterator FI = Functions.find(M);
93 ExFunc Fn = (FI == Functions.end()) ? lookupFunction(M) : FI->second;
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000094 if (Fn == 0) {
Chris Lattner470754e2003-05-08 16:18:31 +000095 std::cout << "Tried to execute an unknown external function: "
96 << M->getType()->getDescription() << " " << M->getName() << "\n";
Chris Lattner7fd51b52001-10-30 20:28:00 +000097 return GenericValue();
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000098 }
99
100 // TODO: FIXME when types are not const!
Chris Lattner62b7fd12002-04-07 20:49:59 +0000101 GenericValue Result = Fn(const_cast<FunctionType*>(M->getFunctionType()),
102 ArgVals);
Chris Lattner7fd51b52001-10-30 20:28:00 +0000103 return Result;
Chris Lattnerbaf08eb2001-09-10 04:50:17 +0000104}
105
106
107//===----------------------------------------------------------------------===//
Chris Lattnerf94811a2002-03-29 03:57:15 +0000108// Functions "exported" to the running application...
Chris Lattnerbaf08eb2001-09-10 04:50:17 +0000109//
110extern "C" { // Don't add C++ manglings to llvm mangling :)
111
Chris Lattner0313db62002-10-02 21:12:13 +0000112// void putchar(sbyte)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000113GenericValue lle_Vb_putchar(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner470754e2003-05-08 16:18:31 +0000114 std::cout << Args[0].SByteVal;
Chris Lattnerbaf08eb2001-09-10 04:50:17 +0000115 return GenericValue();
116}
117
Chris Lattner0313db62002-10-02 21:12:13 +0000118// int putchar(int)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000119GenericValue lle_ii_putchar(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner470754e2003-05-08 16:18:31 +0000120 std::cout << ((char)Args[0].IntVal) << std::flush;
Chris Lattner15157b82001-10-27 04:15:57 +0000121 return Args[0];
122}
123
Chris Lattner0313db62002-10-02 21:12:13 +0000124// void putchar(ubyte)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000125GenericValue lle_VB_putchar(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner470754e2003-05-08 16:18:31 +0000126 std::cout << Args[0].SByteVal << std::flush;
Chris Lattner15157b82001-10-27 04:15:57 +0000127 return Args[0];
Chris Lattnerc62e2e52001-10-15 05:51:48 +0000128}
129
Chris Lattner4a5bb952003-05-14 14:21:30 +0000130// void atexit(Function*)
131GenericValue lle_X_atexit(FunctionType *M, const vector<GenericValue> &Args) {
132 assert(Args.size() == 1);
133 TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0]));
134 GenericValue GV;
135 GV.IntVal = 0;
136 return GV;
Chris Lattnerd299dba2001-10-18 21:55:32 +0000137}
138
Chris Lattner0313db62002-10-02 21:12:13 +0000139// void exit(int)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000140GenericValue lle_X_exit(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner15157b82001-10-27 04:15:57 +0000141 TheInterpreter->exitCalled(Args[0]);
142 return GenericValue();
143}
144
Chris Lattner0313db62002-10-02 21:12:13 +0000145// void abort(void)
Chris Lattner13194292002-05-20 21:17:16 +0000146GenericValue lle_X_abort(FunctionType *M, const vector<GenericValue> &Args) {
Brian Gaeke4e106f02003-11-05 01:18:49 +0000147 raise (SIGABRT);
Chris Lattner13194292002-05-20 21:17:16 +0000148 return GenericValue();
149}
150
Chris Lattner0b00b312001-10-27 08:28:11 +0000151// void *malloc(uint)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000152GenericValue lle_X_malloc(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner7fd51b52001-10-30 20:28:00 +0000153 assert(Args.size() == 1 && "Malloc expects one argument!");
Chris Lattnerd49518c2003-01-13 00:59:47 +0000154 return PTOGV(malloc(Args[0].UIntVal));
Chris Lattner0b00b312001-10-27 08:28:11 +0000155}
156
Chris Lattner93f4ff72003-04-23 19:55:24 +0000157// void *calloc(uint, uint)
158GenericValue lle_X_calloc(FunctionType *M, const vector<GenericValue> &Args) {
159 assert(Args.size() == 2 && "calloc expects two arguments!");
160 return PTOGV(calloc(Args[0].UIntVal, Args[1].UIntVal));
161}
162
Chris Lattner0b00b312001-10-27 08:28:11 +0000163// void free(void *)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000164GenericValue lle_X_free(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5cc31882001-11-06 21:52:18 +0000165 assert(Args.size() == 1);
Chris Lattnerd49518c2003-01-13 00:59:47 +0000166 free(GVTOP(Args[0]));
Chris Lattner0b00b312001-10-27 08:28:11 +0000167 return GenericValue();
168}
169
Chris Lattner5ba75732001-11-26 18:18:18 +0000170// int atoi(char *)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000171GenericValue lle_X_atoi(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5ba75732001-11-26 18:18:18 +0000172 assert(Args.size() == 1);
173 GenericValue GV;
Chris Lattnerd49518c2003-01-13 00:59:47 +0000174 GV.IntVal = atoi((char*)GVTOP(Args[0]));
Chris Lattner5ba75732001-11-26 18:18:18 +0000175 return GV;
176}
177
Chris Lattner0b00b312001-10-27 08:28:11 +0000178// double pow(double, double)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000179GenericValue lle_X_pow(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5cc31882001-11-06 21:52:18 +0000180 assert(Args.size() == 2);
Chris Lattner0b00b312001-10-27 08:28:11 +0000181 GenericValue GV;
Chris Lattner490d2a82001-10-29 20:27:45 +0000182 GV.DoubleVal = pow(Args[0].DoubleVal, Args[1].DoubleVal);
Chris Lattner0b00b312001-10-27 08:28:11 +0000183 return GV;
184}
185
Chris Lattner568a7702002-02-18 19:06:25 +0000186// double exp(double)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000187GenericValue lle_X_exp(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner568a7702002-02-18 19:06:25 +0000188 assert(Args.size() == 1);
189 GenericValue GV;
190 GV.DoubleVal = exp(Args[0].DoubleVal);
191 return GV;
192}
193
Chris Lattner5cc31882001-11-06 21:52:18 +0000194// double sqrt(double)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000195GenericValue lle_X_sqrt(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5cc31882001-11-06 21:52:18 +0000196 assert(Args.size() == 1);
197 GenericValue GV;
198 GV.DoubleVal = sqrt(Args[0].DoubleVal);
199 return GV;
200}
Chris Lattner0b00b312001-10-27 08:28:11 +0000201
Chris Lattnerc23094e2001-11-06 22:53:25 +0000202// double log(double)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000203GenericValue lle_X_log(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerc23094e2001-11-06 22:53:25 +0000204 assert(Args.size() == 1);
205 GenericValue GV;
206 GV.DoubleVal = log(Args[0].DoubleVal);
207 return GV;
208}
209
Chris Lattner5ba75732001-11-26 18:18:18 +0000210// double floor(double)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000211GenericValue lle_X_floor(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5ba75732001-11-26 18:18:18 +0000212 assert(Args.size() == 1);
213 GenericValue GV;
214 GV.DoubleVal = floor(Args[0].DoubleVal);
215 return GV;
216}
217
Chris Lattnerc23094e2001-11-06 22:53:25 +0000218// double drand48()
Chris Lattnerf94811a2002-03-29 03:57:15 +0000219GenericValue lle_X_drand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerc23094e2001-11-06 22:53:25 +0000220 assert(Args.size() == 0);
221 GenericValue GV;
222 GV.DoubleVal = drand48();
223 return GV;
224}
225
Chris Lattner9754aba2001-11-13 05:46:08 +0000226// long lrand48()
Chris Lattnerf94811a2002-03-29 03:57:15 +0000227GenericValue lle_X_lrand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner9754aba2001-11-13 05:46:08 +0000228 assert(Args.size() == 0);
229 GenericValue GV;
230 GV.IntVal = lrand48();
231 return GV;
232}
233
234// void srand48(long)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000235GenericValue lle_X_srand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner9754aba2001-11-13 05:46:08 +0000236 assert(Args.size() == 1);
237 srand48(Args[0].IntVal);
238 return GenericValue();
239}
240
Chris Lattner5ba75732001-11-26 18:18:18 +0000241// void srand(uint)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000242GenericValue lle_X_srand(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5ba75732001-11-26 18:18:18 +0000243 assert(Args.size() == 1);
244 srand(Args[0].UIntVal);
245 return GenericValue();
246}
Chris Lattnerc23094e2001-11-06 22:53:25 +0000247
Chris Lattnerd49518c2003-01-13 00:59:47 +0000248// int puts(const char*)
249GenericValue lle_X_puts(FunctionType *M, const vector<GenericValue> &Args) {
250 assert(Args.size() == 1);
251 GenericValue GV;
252 GV.IntVal = puts((char*)GVTOP(Args[0]));
253 return GV;
254}
255
Chris Lattner60a9a232001-12-13 00:43:47 +0000256// int sprintf(sbyte *, sbyte *, ...) - a very rough implementation to make
257// output useful.
Chris Lattnerf94811a2002-03-29 03:57:15 +0000258GenericValue lle_X_sprintf(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerd49518c2003-01-13 00:59:47 +0000259 char *OutputBuffer = (char *)GVTOP(Args[0]);
260 const char *FmtStr = (const char *)GVTOP(Args[1]);
Chris Lattner60a9a232001-12-13 00:43:47 +0000261 unsigned ArgNo = 2;
Chris Lattner490d2a82001-10-29 20:27:45 +0000262
263 // printf should return # chars printed. This is completely incorrect, but
264 // close enough for now.
265 GenericValue GV; GV.IntVal = strlen(FmtStr);
266 while (1) {
267 switch (*FmtStr) {
268 case 0: return GV; // Null terminator...
269 default: // Normal nonspecial character
Chris Lattner60a9a232001-12-13 00:43:47 +0000270 sprintf(OutputBuffer++, "%c", *FmtStr++);
Chris Lattner490d2a82001-10-29 20:27:45 +0000271 break;
272 case '\\': { // Handle escape codes
Chris Lattner60a9a232001-12-13 00:43:47 +0000273 sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
274 FmtStr += 2; OutputBuffer += 2;
Chris Lattner490d2a82001-10-29 20:27:45 +0000275 break;
276 }
277 case '%': { // Handle format specifiers
Chris Lattner31e9e4d2001-11-07 19:46:27 +0000278 char FmtBuf[100] = "", Buffer[1000] = "";
279 char *FB = FmtBuf;
280 *FB++ = *FmtStr++;
281 char Last = *FB++ = *FmtStr++;
282 unsigned HowLong = 0;
283 while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' &&
284 Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' &&
285 Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' &&
286 Last != 'p' && Last != 's' && Last != '%') {
287 if (Last == 'l' || Last == 'L') HowLong++; // Keep track of l's
288 Last = *FB++ = *FmtStr++;
Chris Lattner490d2a82001-10-29 20:27:45 +0000289 }
Chris Lattner31e9e4d2001-11-07 19:46:27 +0000290 *FB = 0;
291
292 switch (Last) {
293 case '%':
294 sprintf(Buffer, FmtBuf); break;
295 case 'c':
Chris Lattnerd6010ab2002-04-17 17:43:01 +0000296 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal); break;
Chris Lattner31e9e4d2001-11-07 19:46:27 +0000297 case 'd': case 'i':
298 case 'u': case 'o':
299 case 'x': case 'X':
Chris Lattner33b3b962002-08-02 23:08:32 +0000300 if (HowLong >= 1) {
Chris Lattner47985402003-08-24 14:02:47 +0000301 if (HowLong == 1 &&
302 TheInterpreter->getModule().getPointerSize()==Module::Pointer64 &&
Chris Lattner01971e12003-04-25 18:28:44 +0000303 sizeof(long) < sizeof(long long)) {
Chris Lattner33b3b962002-08-02 23:08:32 +0000304 // Make sure we use %lld with a 64 bit argument because we might be
305 // compiling LLI on a 32 bit compiler.
306 unsigned Size = strlen(FmtBuf);
307 FmtBuf[Size] = FmtBuf[Size-1];
308 FmtBuf[Size+1] = 0;
309 FmtBuf[Size-1] = 'l';
310 }
Chris Lattner31e9e4d2001-11-07 19:46:27 +0000311 sprintf(Buffer, FmtBuf, Args[ArgNo++].ULongVal);
Chris Lattner33b3b962002-08-02 23:08:32 +0000312 } else
Chris Lattner31e9e4d2001-11-07 19:46:27 +0000313 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal); break;
314 case 'e': case 'E': case 'g': case 'G': case 'f':
315 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
316 case 'p':
Chris Lattnerd49518c2003-01-13 00:59:47 +0000317 sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break;
Chris Lattner31e9e4d2001-11-07 19:46:27 +0000318 case 's':
Chris Lattnerd49518c2003-01-13 00:59:47 +0000319 sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break;
Chris Lattner470754e2003-05-08 16:18:31 +0000320 default: std::cout << "<unknown printf code '" << *FmtStr << "'!>";
Chris Lattner31e9e4d2001-11-07 19:46:27 +0000321 ArgNo++; break;
Chris Lattner490d2a82001-10-29 20:27:45 +0000322 }
Chris Lattner60a9a232001-12-13 00:43:47 +0000323 strcpy(OutputBuffer, Buffer);
324 OutputBuffer += strlen(Buffer);
Chris Lattner31e9e4d2001-11-07 19:46:27 +0000325 }
Chris Lattner490d2a82001-10-29 20:27:45 +0000326 break;
327 }
Chris Lattner490d2a82001-10-29 20:27:45 +0000328 }
329}
330
Chris Lattner60a9a232001-12-13 00:43:47 +0000331// int printf(sbyte *, ...) - a very rough implementation to make output useful.
Chris Lattnerf94811a2002-03-29 03:57:15 +0000332GenericValue lle_X_printf(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner60a9a232001-12-13 00:43:47 +0000333 char Buffer[10000];
334 vector<GenericValue> NewArgs;
Chris Lattnerd49518c2003-01-13 00:59:47 +0000335 NewArgs.push_back(PTOGV(Buffer));
Chris Lattner60a9a232001-12-13 00:43:47 +0000336 NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
Chris Lattnerd49518c2003-01-13 00:59:47 +0000337 GenericValue GV = lle_X_sprintf(M, NewArgs);
Chris Lattner470754e2003-05-08 16:18:31 +0000338 std::cout << Buffer;
Chris Lattner60a9a232001-12-13 00:43:47 +0000339 return GV;
340}
341
Chris Lattnerd3183712003-03-31 22:12:37 +0000342static void ByteswapSCANFResults(const char *Fmt, void *Arg0, void *Arg1,
343 void *Arg2, void *Arg3, void *Arg4, void *Arg5,
344 void *Arg6, void *Arg7, void *Arg8) {
345 void *Args[] = { Arg0, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, 0 };
346
347 // Loop over the format string, munging read values as appropriate (performs
Misha Brukman7eb05a12003-08-18 14:43:39 +0000348 // byteswaps as necessary).
Chris Lattnerd3183712003-03-31 22:12:37 +0000349 unsigned ArgNo = 0;
350 while (*Fmt) {
351 if (*Fmt++ == '%') {
352 // Read any flag characters that may be present...
353 bool Suppress = false;
354 bool Half = false;
355 bool Long = false;
356 bool LongLong = false; // long long or long double
357
358 while (1) {
359 switch (*Fmt++) {
360 case '*': Suppress = true; break;
361 case 'a': /*Allocate = true;*/ break; // We don't need to track this
362 case 'h': Half = true; break;
363 case 'l': Long = true; break;
364 case 'q':
365 case 'L': LongLong = true; break;
366 default:
367 if (Fmt[-1] > '9' || Fmt[-1] < '0') // Ignore field width specs
368 goto Out;
369 }
370 }
371 Out:
372
373 // Read the conversion character
374 if (!Suppress && Fmt[-1] != '%') { // Nothing to do?
375 unsigned Size = 0;
376 const Type *Ty = 0;
377
378 switch (Fmt[-1]) {
379 case 'i': case 'o': case 'u': case 'x': case 'X': case 'n': case 'p':
380 case 'd':
381 if (Long || LongLong) {
382 Size = 8; Ty = Type::ULongTy;
383 } else if (Half) {
384 Size = 4; Ty = Type::UShortTy;
385 } else {
386 Size = 4; Ty = Type::UIntTy;
387 }
388 break;
389
390 case 'e': case 'g': case 'E':
391 case 'f':
392 if (Long || LongLong) {
393 Size = 8; Ty = Type::DoubleTy;
394 } else {
395 Size = 4; Ty = Type::FloatTy;
396 }
397 break;
398
399 case 's': case 'c': case '[': // No byteswap needed
400 Size = 1;
401 Ty = Type::SByteTy;
402 break;
403
404 default: break;
405 }
406
407 if (Size) {
408 GenericValue GV;
409 void *Arg = Args[ArgNo++];
410 memcpy(&GV, Arg, Size);
411 TheInterpreter->StoreValueToMemory(GV, (GenericValue*)Arg, Ty);
412 }
413 }
414 }
415 }
416}
417
Chris Lattner5dec4602002-03-08 22:51:07 +0000418// int sscanf(const char *format, ...);
Chris Lattnerf94811a2002-03-29 03:57:15 +0000419GenericValue lle_X_sscanf(FunctionType *M, const vector<GenericValue> &args) {
Chris Lattner5dec4602002-03-08 22:51:07 +0000420 assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
421
Chris Lattnerd3183712003-03-31 22:12:37 +0000422 char *Args[10];
Chris Lattner5dec4602002-03-08 22:51:07 +0000423 for (unsigned i = 0; i < args.size(); ++i)
Chris Lattnerd3183712003-03-31 22:12:37 +0000424 Args[i] = (char*)GVTOP(args[i]);
Chris Lattner5dec4602002-03-08 22:51:07 +0000425
426 GenericValue GV;
427 GV.IntVal = sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
428 Args[5], Args[6], Args[7], Args[8], Args[9]);
Chris Lattnerd3183712003-03-31 22:12:37 +0000429 ByteswapSCANFResults(Args[1], Args[2], Args[3], Args[4],
430 Args[5], Args[6], Args[7], Args[8], Args[9], 0);
431 return GV;
432}
433
434// int scanf(const char *format, ...);
435GenericValue lle_X_scanf(FunctionType *M, const vector<GenericValue> &args) {
436 assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!");
437
438 char *Args[10];
439 for (unsigned i = 0; i < args.size(); ++i)
440 Args[i] = (char*)GVTOP(args[i]);
441
442 GenericValue GV;
443 GV.IntVal = scanf(Args[0], Args[1], Args[2], Args[3], Args[4],
444 Args[5], Args[6], Args[7], Args[8], Args[9]);
445 ByteswapSCANFResults(Args[0], Args[1], Args[2], Args[3], Args[4],
446 Args[5], Args[6], Args[7], Args[8], Args[9]);
Chris Lattner5dec4602002-03-08 22:51:07 +0000447 return GV;
448}
449
450
Chris Lattner8f52ffd2002-01-23 21:38:07 +0000451// int clock(void) - Profiling implementation
Chris Lattnerf94811a2002-03-29 03:57:15 +0000452GenericValue lle_i_clock(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner8f52ffd2002-01-23 21:38:07 +0000453 extern int clock(void);
454 GenericValue GV; GV.IntVal = clock();
455 return GV;
456}
Chris Lattner60a9a232001-12-13 00:43:47 +0000457
Chris Lattner93f4ff72003-04-23 19:55:24 +0000458
459//===----------------------------------------------------------------------===//
460// String Functions...
461//===----------------------------------------------------------------------===//
462
463// int strcmp(const char *S1, const char *S2);
464GenericValue lle_X_strcmp(FunctionType *M, const vector<GenericValue> &Args) {
465 assert(Args.size() == 2);
466 GenericValue Ret;
467 Ret.IntVal = strcmp((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]));
468 return Ret;
469}
470
471// char *strcat(char *Dest, const char *src);
472GenericValue lle_X_strcat(FunctionType *M, const vector<GenericValue> &Args) {
473 assert(Args.size() == 2);
474 return PTOGV(strcat((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
475}
476
477// char *strcpy(char *Dest, const char *src);
478GenericValue lle_X_strcpy(FunctionType *M, const vector<GenericValue> &Args) {
479 assert(Args.size() == 2);
480 return PTOGV(strcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
481}
482
Brian Gaeke2d7efbb2004-05-01 06:42:15 +0000483static GenericValue size_t_to_GV (size_t n) {
484 GenericValue Ret;
485 if (sizeof (size_t) == sizeof (uint64_t)) {
486 Ret.ULongVal = n;
487 } else {
488 assert (sizeof (size_t) == sizeof (unsigned int));
489 Ret.UIntVal = n;
490 }
491 return Ret;
492}
493
494static size_t GV_to_size_t (GenericValue GV) {
495 size_t count;
496 if (sizeof (size_t) == sizeof (uint64_t)) {
497 count = GV.ULongVal;
498 } else {
499 assert (sizeof (size_t) == sizeof (unsigned int));
500 count = GV.UIntVal;
501 }
502 return count;
503}
504
Brian Gaeke13a2e542003-12-12 15:38:06 +0000505// size_t strlen(const char *src);
Chris Lattner93f4ff72003-04-23 19:55:24 +0000506GenericValue lle_X_strlen(FunctionType *M, const vector<GenericValue> &Args) {
507 assert(Args.size() == 1);
Brian Gaeke13a2e542003-12-12 15:38:06 +0000508 size_t strlenResult = strlen ((char *) GVTOP (Args[0]));
Brian Gaeke2d7efbb2004-05-01 06:42:15 +0000509 return size_t_to_GV (strlenResult);
Chris Lattner93f4ff72003-04-23 19:55:24 +0000510}
511
Brian Gaekea7669032003-09-05 18:42:01 +0000512// char *strdup(const char *src);
513GenericValue lle_X_strdup(FunctionType *M, const vector<GenericValue> &Args) {
514 assert(Args.size() == 1);
515 return PTOGV(strdup((char*)GVTOP(Args[0])));
516}
517
Chris Lattner4c5245b2003-04-25 18:23:38 +0000518// char *__strdup(const char *src);
519GenericValue lle_X___strdup(FunctionType *M, const vector<GenericValue> &Args) {
520 assert(Args.size() == 1);
521 return PTOGV(strdup((char*)GVTOP(Args[0])));
522}
523
Chris Lattner93f4ff72003-04-23 19:55:24 +0000524// void *memset(void *S, int C, size_t N)
525GenericValue lle_X_memset(FunctionType *M, const vector<GenericValue> &Args) {
526 assert(Args.size() == 3);
Brian Gaeke2d7efbb2004-05-01 06:42:15 +0000527 size_t count = GV_to_size_t (Args[2]);
528 return PTOGV(memset(GVTOP(Args[0]), Args[1].IntVal, count));
Chris Lattner93f4ff72003-04-23 19:55:24 +0000529}
530
Chris Lattner487bb802003-04-23 20:23:16 +0000531// void *memcpy(void *Dest, void *src, size_t Size);
532GenericValue lle_X_memcpy(FunctionType *M, const vector<GenericValue> &Args) {
533 assert(Args.size() == 3);
Brian Gaeke2d7efbb2004-05-01 06:42:15 +0000534 size_t count = GV_to_size_t (Args[2]);
535 return PTOGV(memcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]), count));
Chris Lattner487bb802003-04-23 20:23:16 +0000536}
Chris Lattner93f4ff72003-04-23 19:55:24 +0000537
Chris Lattner5dec4602002-03-08 22:51:07 +0000538//===----------------------------------------------------------------------===//
539// IO Functions...
540//===----------------------------------------------------------------------===//
541
Chris Lattner0313db62002-10-02 21:12:13 +0000542// getFILE - Turn a pointer in the host address space into a legit pointer in
Reid Spencer7b8a3b52004-05-25 08:46:15 +0000543// the interpreter address space. This is an identity transformation.
544#define getFILE(ptr) ((FILE*)ptr)
Chris Lattner0313db62002-10-02 21:12:13 +0000545
Chris Lattner5dec4602002-03-08 22:51:07 +0000546// FILE *fopen(const char *filename, const char *mode);
Chris Lattnerf94811a2002-03-29 03:57:15 +0000547GenericValue lle_X_fopen(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5dec4602002-03-08 22:51:07 +0000548 assert(Args.size() == 2);
Chris Lattnerd49518c2003-01-13 00:59:47 +0000549 return PTOGV(fopen((const char *)GVTOP(Args[0]),
550 (const char *)GVTOP(Args[1])));
Chris Lattner5dec4602002-03-08 22:51:07 +0000551}
552
553// int fclose(FILE *F);
Chris Lattnerf94811a2002-03-29 03:57:15 +0000554GenericValue lle_X_fclose(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5dec4602002-03-08 22:51:07 +0000555 assert(Args.size() == 1);
556 GenericValue GV;
Chris Lattnerd49518c2003-01-13 00:59:47 +0000557 GV.IntVal = fclose(getFILE(GVTOP(Args[0])));
Chris Lattner5dec4602002-03-08 22:51:07 +0000558 return GV;
559}
560
Chris Lattner7a1da7c2002-11-08 19:10:26 +0000561// int feof(FILE *stream);
562GenericValue lle_X_feof(FunctionType *M, const vector<GenericValue> &Args) {
563 assert(Args.size() == 1);
564 GenericValue GV;
565
Chris Lattnerd49518c2003-01-13 00:59:47 +0000566 GV.IntVal = feof(getFILE(GVTOP(Args[0])));
Chris Lattner7a1da7c2002-11-08 19:10:26 +0000567 return GV;
568}
569
Chris Lattner5dec4602002-03-08 22:51:07 +0000570// size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
Chris Lattnerf94811a2002-03-29 03:57:15 +0000571GenericValue lle_X_fread(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5dec4602002-03-08 22:51:07 +0000572 assert(Args.size() == 4);
Brian Gaeke2d7efbb2004-05-01 06:42:15 +0000573 size_t result;
Chris Lattner5dec4602002-03-08 22:51:07 +0000574
Brian Gaeke2d7efbb2004-05-01 06:42:15 +0000575 result = fread((void*)GVTOP(Args[0]), GV_to_size_t (Args[1]),
576 GV_to_size_t (Args[2]), getFILE(GVTOP(Args[3])));
577 return size_t_to_GV (result);
Chris Lattner5dec4602002-03-08 22:51:07 +0000578}
579
580// size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream);
Chris Lattnerf94811a2002-03-29 03:57:15 +0000581GenericValue lle_X_fwrite(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5dec4602002-03-08 22:51:07 +0000582 assert(Args.size() == 4);
Brian Gaeke2d7efbb2004-05-01 06:42:15 +0000583 size_t result;
Chris Lattner5dec4602002-03-08 22:51:07 +0000584
Brian Gaeke2d7efbb2004-05-01 06:42:15 +0000585 result = fwrite((void*)GVTOP(Args[0]), GV_to_size_t (Args[1]),
586 GV_to_size_t (Args[2]), getFILE(GVTOP(Args[3])));
587 return size_t_to_GV (result);
Chris Lattner5dec4602002-03-08 22:51:07 +0000588}
589
590// char *fgets(char *s, int n, FILE *stream);
Chris Lattnerf94811a2002-03-29 03:57:15 +0000591GenericValue lle_X_fgets(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5dec4602002-03-08 22:51:07 +0000592 assert(Args.size() == 3);
Chris Lattnerd49518c2003-01-13 00:59:47 +0000593 return GVTOP(fgets((char*)GVTOP(Args[0]), Args[1].IntVal,
594 getFILE(GVTOP(Args[2]))));
Chris Lattner5dec4602002-03-08 22:51:07 +0000595}
596
Chris Lattnera41a1952002-11-07 19:33:50 +0000597// FILE *freopen(const char *path, const char *mode, FILE *stream);
598GenericValue lle_X_freopen(FunctionType *M, const vector<GenericValue> &Args) {
599 assert(Args.size() == 3);
Chris Lattnerd49518c2003-01-13 00:59:47 +0000600 return PTOGV(freopen((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]),
601 getFILE(GVTOP(Args[2]))));
Chris Lattnera41a1952002-11-07 19:33:50 +0000602}
603
Chris Lattner5dec4602002-03-08 22:51:07 +0000604// int fflush(FILE *stream);
Chris Lattnerf94811a2002-03-29 03:57:15 +0000605GenericValue lle_X_fflush(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5dec4602002-03-08 22:51:07 +0000606 assert(Args.size() == 1);
607 GenericValue GV;
Chris Lattnerd49518c2003-01-13 00:59:47 +0000608 GV.IntVal = fflush(getFILE(GVTOP(Args[0])));
Chris Lattner0313db62002-10-02 21:12:13 +0000609 return GV;
610}
611
612// int getc(FILE *stream);
613GenericValue lle_X_getc(FunctionType *M, const vector<GenericValue> &Args) {
614 assert(Args.size() == 1);
615 GenericValue GV;
Chris Lattnerd49518c2003-01-13 00:59:47 +0000616 GV.IntVal = getc(getFILE(GVTOP(Args[0])));
Chris Lattner5dec4602002-03-08 22:51:07 +0000617 return GV;
618}
619
Chris Lattner26a4a1f2003-04-23 19:20:50 +0000620// int _IO_getc(FILE *stream);
621GenericValue lle_X__IO_getc(FunctionType *F, const vector<GenericValue> &Args) {
622 return lle_X_getc(F, Args);
623}
624
Chris Lattner85290202002-11-06 22:59:28 +0000625// int fputc(int C, FILE *stream);
626GenericValue lle_X_fputc(FunctionType *M, const vector<GenericValue> &Args) {
627 assert(Args.size() == 2);
628 GenericValue GV;
Chris Lattnerd49518c2003-01-13 00:59:47 +0000629 GV.IntVal = fputc(Args[0].IntVal, getFILE(GVTOP(Args[1])));
Chris Lattner85290202002-11-06 22:59:28 +0000630 return GV;
631}
632
633// int ungetc(int C, FILE *stream);
634GenericValue lle_X_ungetc(FunctionType *M, const vector<GenericValue> &Args) {
635 assert(Args.size() == 2);
636 GenericValue GV;
Chris Lattnerd49518c2003-01-13 00:59:47 +0000637 GV.IntVal = ungetc(Args[0].IntVal, getFILE(GVTOP(Args[1])));
Chris Lattner85290202002-11-06 22:59:28 +0000638 return GV;
639}
640
Chris Lattnerc3a84092002-11-06 23:05:03 +0000641// int fprintf(FILE *,sbyte *, ...) - a very rough implementation to make output
642// useful.
643GenericValue lle_X_fprintf(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner16106662003-04-21 22:43:20 +0000644 assert(Args.size() >= 2);
Chris Lattnerc3a84092002-11-06 23:05:03 +0000645 char Buffer[10000];
646 vector<GenericValue> NewArgs;
Chris Lattnerd49518c2003-01-13 00:59:47 +0000647 NewArgs.push_back(PTOGV(Buffer));
Chris Lattnerc3a84092002-11-06 23:05:03 +0000648 NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end());
Chris Lattnerd49518c2003-01-13 00:59:47 +0000649 GenericValue GV = lle_X_sprintf(M, NewArgs);
Chris Lattnerc3a84092002-11-06 23:05:03 +0000650
Chris Lattnerd49518c2003-01-13 00:59:47 +0000651 fputs(Buffer, getFILE(GVTOP(Args[0])));
Chris Lattnerc3a84092002-11-06 23:05:03 +0000652 return GV;
653}
654
Chris Lattnerbaf08eb2001-09-10 04:50:17 +0000655} // End extern "C"
Chris Lattner7fd51b52001-10-30 20:28:00 +0000656
657
Chris Lattner470754e2003-05-08 16:18:31 +0000658void Interpreter::initializeExternalFunctions() {
Chris Lattnerd7c742f2001-11-03 10:15:32 +0000659 FuncNames["lle_Vb_putchar"] = lle_Vb_putchar;
660 FuncNames["lle_ii_putchar"] = lle_ii_putchar;
661 FuncNames["lle_VB_putchar"] = lle_VB_putchar;
Chris Lattnerd7c742f2001-11-03 10:15:32 +0000662 FuncNames["lle_X_exit"] = lle_X_exit;
Chris Lattner13194292002-05-20 21:17:16 +0000663 FuncNames["lle_X_abort"] = lle_X_abort;
Chris Lattnerd7c742f2001-11-03 10:15:32 +0000664 FuncNames["lle_X_malloc"] = lle_X_malloc;
Chris Lattner93f4ff72003-04-23 19:55:24 +0000665 FuncNames["lle_X_calloc"] = lle_X_calloc;
Chris Lattnerd7c742f2001-11-03 10:15:32 +0000666 FuncNames["lle_X_free"] = lle_X_free;
Chris Lattner5ba75732001-11-26 18:18:18 +0000667 FuncNames["lle_X_atoi"] = lle_X_atoi;
Chris Lattnerd7c742f2001-11-03 10:15:32 +0000668 FuncNames["lle_X_pow"] = lle_X_pow;
Chris Lattner568a7702002-02-18 19:06:25 +0000669 FuncNames["lle_X_exp"] = lle_X_exp;
Chris Lattner9754aba2001-11-13 05:46:08 +0000670 FuncNames["lle_X_log"] = lle_X_log;
Chris Lattner5ba75732001-11-26 18:18:18 +0000671 FuncNames["lle_X_floor"] = lle_X_floor;
672 FuncNames["lle_X_srand"] = lle_X_srand;
Chris Lattner9754aba2001-11-13 05:46:08 +0000673 FuncNames["lle_X_drand48"] = lle_X_drand48;
674 FuncNames["lle_X_srand48"] = lle_X_srand48;
675 FuncNames["lle_X_lrand48"] = lle_X_lrand48;
Chris Lattner5cc31882001-11-06 21:52:18 +0000676 FuncNames["lle_X_sqrt"] = lle_X_sqrt;
Chris Lattnerd49518c2003-01-13 00:59:47 +0000677 FuncNames["lle_X_puts"] = lle_X_puts;
Chris Lattnerd7c742f2001-11-03 10:15:32 +0000678 FuncNames["lle_X_printf"] = lle_X_printf;
Chris Lattner60a9a232001-12-13 00:43:47 +0000679 FuncNames["lle_X_sprintf"] = lle_X_sprintf;
Chris Lattner5dec4602002-03-08 22:51:07 +0000680 FuncNames["lle_X_sscanf"] = lle_X_sscanf;
Chris Lattnerd3183712003-03-31 22:12:37 +0000681 FuncNames["lle_X_scanf"] = lle_X_scanf;
Chris Lattner8f52ffd2002-01-23 21:38:07 +0000682 FuncNames["lle_i_clock"] = lle_i_clock;
Chris Lattner93f4ff72003-04-23 19:55:24 +0000683
684 FuncNames["lle_X_strcmp"] = lle_X_strcmp;
685 FuncNames["lle_X_strcat"] = lle_X_strcat;
686 FuncNames["lle_X_strcpy"] = lle_X_strcpy;
687 FuncNames["lle_X_strlen"] = lle_X_strlen;
Chris Lattner470754e2003-05-08 16:18:31 +0000688 FuncNames["lle_X___strdup"] = lle_X___strdup;
Chris Lattner93f4ff72003-04-23 19:55:24 +0000689 FuncNames["lle_X_memset"] = lle_X_memset;
Chris Lattner487bb802003-04-23 20:23:16 +0000690 FuncNames["lle_X_memcpy"] = lle_X_memcpy;
Chris Lattner93f4ff72003-04-23 19:55:24 +0000691
Chris Lattner5dec4602002-03-08 22:51:07 +0000692 FuncNames["lle_X_fopen"] = lle_X_fopen;
693 FuncNames["lle_X_fclose"] = lle_X_fclose;
Chris Lattner7a1da7c2002-11-08 19:10:26 +0000694 FuncNames["lle_X_feof"] = lle_X_feof;
Chris Lattner5dec4602002-03-08 22:51:07 +0000695 FuncNames["lle_X_fread"] = lle_X_fread;
696 FuncNames["lle_X_fwrite"] = lle_X_fwrite;
697 FuncNames["lle_X_fgets"] = lle_X_fgets;
698 FuncNames["lle_X_fflush"] = lle_X_fflush;
Chris Lattner85290202002-11-06 22:59:28 +0000699 FuncNames["lle_X_fgetc"] = lle_X_getc;
Chris Lattner0313db62002-10-02 21:12:13 +0000700 FuncNames["lle_X_getc"] = lle_X_getc;
Chris Lattner26a4a1f2003-04-23 19:20:50 +0000701 FuncNames["lle_X__IO_getc"] = lle_X__IO_getc;
Chris Lattner85290202002-11-06 22:59:28 +0000702 FuncNames["lle_X_fputc"] = lle_X_fputc;
703 FuncNames["lle_X_ungetc"] = lle_X_ungetc;
Chris Lattnerc3a84092002-11-06 23:05:03 +0000704 FuncNames["lle_X_fprintf"] = lle_X_fprintf;
Chris Lattnera41a1952002-11-07 19:33:50 +0000705 FuncNames["lle_X_freopen"] = lle_X_freopen;
Chris Lattner7fd51b52001-10-30 20:28:00 +0000706}
Brian Gaeke960707c2003-11-11 22:41:34 +0000707