blob: def406a4b70c5ec67d2470a24750e8345d6c315f [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"
Reid Spencer7c16caa2004-09-01 22:55:40 +000026#include "llvm/Support/DynamicLinker.h"
Brian Gaekefcd72642003-06-23 19:41:55 +000027#include <cmath>
Brian Gaeke4e106f02003-11-05 01:18:49 +000028#include <csignal>
Misha Brukman1d2d8ec2003-10-14 21:42:11 +000029#include <map>
Chris Lattner7f74a562002-01-20 22:54:45 +000030using std::vector;
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000031
Chris Lattner18099662003-12-14 23:25:48 +000032using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000033
Chris Lattnerf94811a2002-03-29 03:57:15 +000034typedef GenericValue (*ExFunc)(FunctionType *, const vector<GenericValue> &);
35static std::map<const Function *, ExFunc> Functions;
Chris Lattner7f74a562002-01-20 22:54:45 +000036static std::map<std::string, ExFunc> FuncNames;
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000037
Chris Lattner15157b82001-10-27 04:15:57 +000038static Interpreter *TheInterpreter;
39
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000040static char getTypeID(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000041 switch (Ty->getTypeID()) {
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000042 case Type::VoidTyID: return 'V';
43 case Type::BoolTyID: return 'o';
44 case Type::UByteTyID: return 'B';
45 case Type::SByteTyID: return 'b';
46 case Type::UShortTyID: return 'S';
47 case Type::ShortTyID: return 's';
48 case Type::UIntTyID: return 'I';
49 case Type::IntTyID: return 'i';
50 case Type::ULongTyID: return 'L';
51 case Type::LongTyID: return 'l';
52 case Type::FloatTyID: return 'F';
53 case Type::DoubleTyID: return 'D';
54 case Type::PointerTyID: return 'P';
Chris Lattnerf94811a2002-03-29 03:57:15 +000055 case Type::FunctionTyID: return 'M';
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000056 case Type::StructTyID: return 'T';
57 case Type::ArrayTyID: return 'A';
58 case Type::OpaqueTyID: return 'O';
59 default: return 'U';
60 }
61}
62
Brian Gaeke6cc20de2003-10-10 17:03:10 +000063static ExFunc lookupFunction(const Function *F) {
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000064 // Function not found, look it up... start by figuring out what the
65 // composite function name should be.
Chris Lattner7f74a562002-01-20 22:54:45 +000066 std::string ExtName = "lle_";
Brian Gaeke6cc20de2003-10-10 17:03:10 +000067 const FunctionType *FT = F->getFunctionType();
68 for (unsigned i = 0, e = FT->getNumContainedTypes(); i != e; ++i)
69 ExtName += getTypeID(FT->getContainedType(i));
70 ExtName += "_" + F->getName();
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000071
Chris Lattner7fd51b52001-10-30 20:28:00 +000072 ExFunc FnPtr = FuncNames[ExtName];
73 if (FnPtr == 0)
Brian Gaeke6cc20de2003-10-10 17:03:10 +000074 FnPtr = (ExFunc)GetAddressOfSymbol(ExtName);
Chris Lattner7fd51b52001-10-30 20:28:00 +000075 if (FnPtr == 0)
Brian Gaeke6cc20de2003-10-10 17:03:10 +000076 FnPtr = FuncNames["lle_X_"+F->getName()];
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000077 if (FnPtr == 0) // Try calling a generic function... if it exists...
Brian Gaeke6cc20de2003-10-10 17:03:10 +000078 FnPtr = (ExFunc)GetAddressOfSymbol(("lle_X_"+F->getName()).c_str());
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000079 if (FnPtr != 0)
Brian Gaeke6cc20de2003-10-10 17:03:10 +000080 Functions.insert(std::make_pair(F, FnPtr)); // Cache for later
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000081 return FnPtr;
82}
83
Chris Lattner470754e2003-05-08 16:18:31 +000084GenericValue Interpreter::callExternalFunction(Function *M,
Chris Lattner4a5bb952003-05-14 14:21:30 +000085 const std::vector<GenericValue> &ArgVals) {
Chris Lattner15157b82001-10-27 04:15:57 +000086 TheInterpreter = this;
87
Chris Lattner62b7fd12002-04-07 20:49:59 +000088 // Do a lookup to see if the function is in our cache... this should just be a
Misha Brukmanaa7d26c2003-10-10 17:42:19 +000089 // deferred annotation!
Chris Lattnerf94811a2002-03-29 03:57:15 +000090 std::map<const Function *, ExFunc>::iterator FI = Functions.find(M);
91 ExFunc Fn = (FI == Functions.end()) ? lookupFunction(M) : FI->second;
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000092 if (Fn == 0) {
Chris Lattner470754e2003-05-08 16:18:31 +000093 std::cout << "Tried to execute an unknown external function: "
94 << M->getType()->getDescription() << " " << M->getName() << "\n";
Chris Lattner7fd51b52001-10-30 20:28:00 +000095 return GenericValue();
Chris Lattnerbaf08eb2001-09-10 04:50:17 +000096 }
97
98 // TODO: FIXME when types are not const!
Chris Lattner62b7fd12002-04-07 20:49:59 +000099 GenericValue Result = Fn(const_cast<FunctionType*>(M->getFunctionType()),
100 ArgVals);
Chris Lattner7fd51b52001-10-30 20:28:00 +0000101 return Result;
Chris Lattnerbaf08eb2001-09-10 04:50:17 +0000102}
103
104
105//===----------------------------------------------------------------------===//
Chris Lattnerf94811a2002-03-29 03:57:15 +0000106// Functions "exported" to the running application...
Chris Lattnerbaf08eb2001-09-10 04:50:17 +0000107//
108extern "C" { // Don't add C++ manglings to llvm mangling :)
109
Chris Lattner0313db62002-10-02 21:12:13 +0000110// void putchar(sbyte)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000111GenericValue lle_Vb_putchar(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner470754e2003-05-08 16:18:31 +0000112 std::cout << Args[0].SByteVal;
Chris Lattnerbaf08eb2001-09-10 04:50:17 +0000113 return GenericValue();
114}
115
Chris Lattner0313db62002-10-02 21:12:13 +0000116// int putchar(int)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000117GenericValue lle_ii_putchar(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner470754e2003-05-08 16:18:31 +0000118 std::cout << ((char)Args[0].IntVal) << std::flush;
Chris Lattner15157b82001-10-27 04:15:57 +0000119 return Args[0];
120}
121
Chris Lattner0313db62002-10-02 21:12:13 +0000122// void putchar(ubyte)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000123GenericValue lle_VB_putchar(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner470754e2003-05-08 16:18:31 +0000124 std::cout << Args[0].SByteVal << std::flush;
Chris Lattner15157b82001-10-27 04:15:57 +0000125 return Args[0];
Chris Lattnerc62e2e52001-10-15 05:51:48 +0000126}
127
Chris Lattner4a5bb952003-05-14 14:21:30 +0000128// void atexit(Function*)
129GenericValue lle_X_atexit(FunctionType *M, const vector<GenericValue> &Args) {
130 assert(Args.size() == 1);
131 TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0]));
132 GenericValue GV;
133 GV.IntVal = 0;
134 return GV;
Chris Lattnerd299dba2001-10-18 21:55:32 +0000135}
136
Chris Lattner0313db62002-10-02 21:12:13 +0000137// void exit(int)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000138GenericValue lle_X_exit(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner15157b82001-10-27 04:15:57 +0000139 TheInterpreter->exitCalled(Args[0]);
140 return GenericValue();
141}
142
Chris Lattner0313db62002-10-02 21:12:13 +0000143// void abort(void)
Chris Lattner13194292002-05-20 21:17:16 +0000144GenericValue lle_X_abort(FunctionType *M, const vector<GenericValue> &Args) {
Brian Gaeke4e106f02003-11-05 01:18:49 +0000145 raise (SIGABRT);
Chris Lattner13194292002-05-20 21:17:16 +0000146 return GenericValue();
147}
148
Chris Lattner0b00b312001-10-27 08:28:11 +0000149// void *malloc(uint)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000150GenericValue lle_X_malloc(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner7fd51b52001-10-30 20:28:00 +0000151 assert(Args.size() == 1 && "Malloc expects one argument!");
Chris Lattnerd49518c2003-01-13 00:59:47 +0000152 return PTOGV(malloc(Args[0].UIntVal));
Chris Lattner0b00b312001-10-27 08:28:11 +0000153}
154
Chris Lattner93f4ff72003-04-23 19:55:24 +0000155// void *calloc(uint, uint)
156GenericValue lle_X_calloc(FunctionType *M, const vector<GenericValue> &Args) {
157 assert(Args.size() == 2 && "calloc expects two arguments!");
158 return PTOGV(calloc(Args[0].UIntVal, Args[1].UIntVal));
159}
160
Chris Lattner0b00b312001-10-27 08:28:11 +0000161// void free(void *)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000162GenericValue lle_X_free(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5cc31882001-11-06 21:52:18 +0000163 assert(Args.size() == 1);
Chris Lattnerd49518c2003-01-13 00:59:47 +0000164 free(GVTOP(Args[0]));
Chris Lattner0b00b312001-10-27 08:28:11 +0000165 return GenericValue();
166}
167
Chris Lattner5ba75732001-11-26 18:18:18 +0000168// int atoi(char *)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000169GenericValue lle_X_atoi(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5ba75732001-11-26 18:18:18 +0000170 assert(Args.size() == 1);
171 GenericValue GV;
Chris Lattnerd49518c2003-01-13 00:59:47 +0000172 GV.IntVal = atoi((char*)GVTOP(Args[0]));
Chris Lattner5ba75732001-11-26 18:18:18 +0000173 return GV;
174}
175
Chris Lattner0b00b312001-10-27 08:28:11 +0000176// double pow(double, double)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000177GenericValue lle_X_pow(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5cc31882001-11-06 21:52:18 +0000178 assert(Args.size() == 2);
Chris Lattner0b00b312001-10-27 08:28:11 +0000179 GenericValue GV;
Chris Lattner490d2a82001-10-29 20:27:45 +0000180 GV.DoubleVal = pow(Args[0].DoubleVal, Args[1].DoubleVal);
Chris Lattner0b00b312001-10-27 08:28:11 +0000181 return GV;
182}
183
Chris Lattner568a7702002-02-18 19:06:25 +0000184// double exp(double)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000185GenericValue lle_X_exp(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner568a7702002-02-18 19:06:25 +0000186 assert(Args.size() == 1);
187 GenericValue GV;
188 GV.DoubleVal = exp(Args[0].DoubleVal);
189 return GV;
190}
191
Chris Lattner5cc31882001-11-06 21:52:18 +0000192// double sqrt(double)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000193GenericValue lle_X_sqrt(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5cc31882001-11-06 21:52:18 +0000194 assert(Args.size() == 1);
195 GenericValue GV;
196 GV.DoubleVal = sqrt(Args[0].DoubleVal);
197 return GV;
198}
Chris Lattner0b00b312001-10-27 08:28:11 +0000199
Chris Lattnerc23094e2001-11-06 22:53:25 +0000200// double log(double)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000201GenericValue lle_X_log(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerc23094e2001-11-06 22:53:25 +0000202 assert(Args.size() == 1);
203 GenericValue GV;
204 GV.DoubleVal = log(Args[0].DoubleVal);
205 return GV;
206}
207
Chris Lattner5ba75732001-11-26 18:18:18 +0000208// double floor(double)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000209GenericValue lle_X_floor(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5ba75732001-11-26 18:18:18 +0000210 assert(Args.size() == 1);
211 GenericValue GV;
212 GV.DoubleVal = floor(Args[0].DoubleVal);
213 return GV;
214}
215
Chris Lattnerc23094e2001-11-06 22:53:25 +0000216// double drand48()
Chris Lattnerf94811a2002-03-29 03:57:15 +0000217GenericValue lle_X_drand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerc23094e2001-11-06 22:53:25 +0000218 assert(Args.size() == 0);
219 GenericValue GV;
220 GV.DoubleVal = drand48();
221 return GV;
222}
223
Chris Lattner9754aba2001-11-13 05:46:08 +0000224// long lrand48()
Chris Lattnerf94811a2002-03-29 03:57:15 +0000225GenericValue lle_X_lrand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner9754aba2001-11-13 05:46:08 +0000226 assert(Args.size() == 0);
227 GenericValue GV;
228 GV.IntVal = lrand48();
229 return GV;
230}
231
232// void srand48(long)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000233GenericValue lle_X_srand48(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner9754aba2001-11-13 05:46:08 +0000234 assert(Args.size() == 1);
235 srand48(Args[0].IntVal);
236 return GenericValue();
237}
238
Chris Lattner5ba75732001-11-26 18:18:18 +0000239// void srand(uint)
Chris Lattnerf94811a2002-03-29 03:57:15 +0000240GenericValue lle_X_srand(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5ba75732001-11-26 18:18:18 +0000241 assert(Args.size() == 1);
242 srand(Args[0].UIntVal);
243 return GenericValue();
244}
Chris Lattnerc23094e2001-11-06 22:53:25 +0000245
Chris Lattnerd49518c2003-01-13 00:59:47 +0000246// int puts(const char*)
247GenericValue lle_X_puts(FunctionType *M, const vector<GenericValue> &Args) {
248 assert(Args.size() == 1);
249 GenericValue GV;
250 GV.IntVal = puts((char*)GVTOP(Args[0]));
251 return GV;
252}
253
Chris Lattner60a9a232001-12-13 00:43:47 +0000254// int sprintf(sbyte *, sbyte *, ...) - a very rough implementation to make
255// output useful.
Chris Lattnerf94811a2002-03-29 03:57:15 +0000256GenericValue lle_X_sprintf(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattnerd49518c2003-01-13 00:59:47 +0000257 char *OutputBuffer = (char *)GVTOP(Args[0]);
258 const char *FmtStr = (const char *)GVTOP(Args[1]);
Chris Lattner60a9a232001-12-13 00:43:47 +0000259 unsigned ArgNo = 2;
Chris Lattner490d2a82001-10-29 20:27:45 +0000260
261 // printf should return # chars printed. This is completely incorrect, but
262 // close enough for now.
263 GenericValue GV; GV.IntVal = strlen(FmtStr);
264 while (1) {
265 switch (*FmtStr) {
266 case 0: return GV; // Null terminator...
267 default: // Normal nonspecial character
Chris Lattner60a9a232001-12-13 00:43:47 +0000268 sprintf(OutputBuffer++, "%c", *FmtStr++);
Chris Lattner490d2a82001-10-29 20:27:45 +0000269 break;
270 case '\\': { // Handle escape codes
Chris Lattner60a9a232001-12-13 00:43:47 +0000271 sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
272 FmtStr += 2; OutputBuffer += 2;
Chris Lattner490d2a82001-10-29 20:27:45 +0000273 break;
274 }
275 case '%': { // Handle format specifiers
Chris Lattner31e9e4d2001-11-07 19:46:27 +0000276 char FmtBuf[100] = "", Buffer[1000] = "";
277 char *FB = FmtBuf;
278 *FB++ = *FmtStr++;
279 char Last = *FB++ = *FmtStr++;
280 unsigned HowLong = 0;
281 while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' &&
282 Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' &&
283 Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' &&
284 Last != 'p' && Last != 's' && Last != '%') {
285 if (Last == 'l' || Last == 'L') HowLong++; // Keep track of l's
286 Last = *FB++ = *FmtStr++;
Chris Lattner490d2a82001-10-29 20:27:45 +0000287 }
Chris Lattner31e9e4d2001-11-07 19:46:27 +0000288 *FB = 0;
289
290 switch (Last) {
291 case '%':
292 sprintf(Buffer, FmtBuf); break;
293 case 'c':
Chris Lattnerd6010ab2002-04-17 17:43:01 +0000294 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal); break;
Chris Lattner31e9e4d2001-11-07 19:46:27 +0000295 case 'd': case 'i':
296 case 'u': case 'o':
297 case 'x': case 'X':
Chris Lattner33b3b962002-08-02 23:08:32 +0000298 if (HowLong >= 1) {
Chris Lattner47985402003-08-24 14:02:47 +0000299 if (HowLong == 1 &&
300 TheInterpreter->getModule().getPointerSize()==Module::Pointer64 &&
Chris Lattner01971e12003-04-25 18:28:44 +0000301 sizeof(long) < sizeof(long long)) {
Chris Lattner33b3b962002-08-02 23:08:32 +0000302 // Make sure we use %lld with a 64 bit argument because we might be
303 // compiling LLI on a 32 bit compiler.
304 unsigned Size = strlen(FmtBuf);
305 FmtBuf[Size] = FmtBuf[Size-1];
306 FmtBuf[Size+1] = 0;
307 FmtBuf[Size-1] = 'l';
308 }
Chris Lattner31e9e4d2001-11-07 19:46:27 +0000309 sprintf(Buffer, FmtBuf, Args[ArgNo++].ULongVal);
Chris Lattner33b3b962002-08-02 23:08:32 +0000310 } else
Chris Lattner31e9e4d2001-11-07 19:46:27 +0000311 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal); break;
312 case 'e': case 'E': case 'g': case 'G': case 'f':
313 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
314 case 'p':
Chris Lattnerd49518c2003-01-13 00:59:47 +0000315 sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break;
Chris Lattner31e9e4d2001-11-07 19:46:27 +0000316 case 's':
Chris Lattnerd49518c2003-01-13 00:59:47 +0000317 sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break;
Chris Lattner470754e2003-05-08 16:18:31 +0000318 default: std::cout << "<unknown printf code '" << *FmtStr << "'!>";
Chris Lattner31e9e4d2001-11-07 19:46:27 +0000319 ArgNo++; break;
Chris Lattner490d2a82001-10-29 20:27:45 +0000320 }
Chris Lattner60a9a232001-12-13 00:43:47 +0000321 strcpy(OutputBuffer, Buffer);
322 OutputBuffer += strlen(Buffer);
Chris Lattner31e9e4d2001-11-07 19:46:27 +0000323 }
Chris Lattner490d2a82001-10-29 20:27:45 +0000324 break;
325 }
Chris Lattner490d2a82001-10-29 20:27:45 +0000326 }
327}
328
Chris Lattner60a9a232001-12-13 00:43:47 +0000329// int printf(sbyte *, ...) - a very rough implementation to make output useful.
Chris Lattnerf94811a2002-03-29 03:57:15 +0000330GenericValue lle_X_printf(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner60a9a232001-12-13 00:43:47 +0000331 char Buffer[10000];
332 vector<GenericValue> NewArgs;
Chris Lattnerd49518c2003-01-13 00:59:47 +0000333 NewArgs.push_back(PTOGV(Buffer));
Chris Lattner60a9a232001-12-13 00:43:47 +0000334 NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
Chris Lattnerd49518c2003-01-13 00:59:47 +0000335 GenericValue GV = lle_X_sprintf(M, NewArgs);
Chris Lattner470754e2003-05-08 16:18:31 +0000336 std::cout << Buffer;
Chris Lattner60a9a232001-12-13 00:43:47 +0000337 return GV;
338}
339
Chris Lattnerd3183712003-03-31 22:12:37 +0000340static void ByteswapSCANFResults(const char *Fmt, void *Arg0, void *Arg1,
341 void *Arg2, void *Arg3, void *Arg4, void *Arg5,
342 void *Arg6, void *Arg7, void *Arg8) {
343 void *Args[] = { Arg0, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, 0 };
344
345 // Loop over the format string, munging read values as appropriate (performs
Misha Brukman7eb05a12003-08-18 14:43:39 +0000346 // byteswaps as necessary).
Chris Lattnerd3183712003-03-31 22:12:37 +0000347 unsigned ArgNo = 0;
348 while (*Fmt) {
349 if (*Fmt++ == '%') {
350 // Read any flag characters that may be present...
351 bool Suppress = false;
352 bool Half = false;
353 bool Long = false;
354 bool LongLong = false; // long long or long double
355
356 while (1) {
357 switch (*Fmt++) {
358 case '*': Suppress = true; break;
359 case 'a': /*Allocate = true;*/ break; // We don't need to track this
360 case 'h': Half = true; break;
361 case 'l': Long = true; break;
362 case 'q':
363 case 'L': LongLong = true; break;
364 default:
365 if (Fmt[-1] > '9' || Fmt[-1] < '0') // Ignore field width specs
366 goto Out;
367 }
368 }
369 Out:
370
371 // Read the conversion character
372 if (!Suppress && Fmt[-1] != '%') { // Nothing to do?
373 unsigned Size = 0;
374 const Type *Ty = 0;
375
376 switch (Fmt[-1]) {
377 case 'i': case 'o': case 'u': case 'x': case 'X': case 'n': case 'p':
378 case 'd':
379 if (Long || LongLong) {
380 Size = 8; Ty = Type::ULongTy;
381 } else if (Half) {
382 Size = 4; Ty = Type::UShortTy;
383 } else {
384 Size = 4; Ty = Type::UIntTy;
385 }
386 break;
387
388 case 'e': case 'g': case 'E':
389 case 'f':
390 if (Long || LongLong) {
391 Size = 8; Ty = Type::DoubleTy;
392 } else {
393 Size = 4; Ty = Type::FloatTy;
394 }
395 break;
396
397 case 's': case 'c': case '[': // No byteswap needed
398 Size = 1;
399 Ty = Type::SByteTy;
400 break;
401
402 default: break;
403 }
404
405 if (Size) {
406 GenericValue GV;
407 void *Arg = Args[ArgNo++];
408 memcpy(&GV, Arg, Size);
409 TheInterpreter->StoreValueToMemory(GV, (GenericValue*)Arg, Ty);
410 }
411 }
412 }
413 }
414}
415
Chris Lattner5dec4602002-03-08 22:51:07 +0000416// int sscanf(const char *format, ...);
Chris Lattnerf94811a2002-03-29 03:57:15 +0000417GenericValue lle_X_sscanf(FunctionType *M, const vector<GenericValue> &args) {
Chris Lattner5dec4602002-03-08 22:51:07 +0000418 assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
419
Chris Lattnerd3183712003-03-31 22:12:37 +0000420 char *Args[10];
Chris Lattner5dec4602002-03-08 22:51:07 +0000421 for (unsigned i = 0; i < args.size(); ++i)
Chris Lattnerd3183712003-03-31 22:12:37 +0000422 Args[i] = (char*)GVTOP(args[i]);
Chris Lattner5dec4602002-03-08 22:51:07 +0000423
424 GenericValue GV;
425 GV.IntVal = sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
426 Args[5], Args[6], Args[7], Args[8], Args[9]);
Chris Lattnerd3183712003-03-31 22:12:37 +0000427 ByteswapSCANFResults(Args[1], Args[2], Args[3], Args[4],
428 Args[5], Args[6], Args[7], Args[8], Args[9], 0);
429 return GV;
430}
431
432// int scanf(const char *format, ...);
433GenericValue lle_X_scanf(FunctionType *M, const vector<GenericValue> &args) {
434 assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!");
435
436 char *Args[10];
437 for (unsigned i = 0; i < args.size(); ++i)
438 Args[i] = (char*)GVTOP(args[i]);
439
440 GenericValue GV;
441 GV.IntVal = scanf(Args[0], Args[1], Args[2], Args[3], Args[4],
442 Args[5], Args[6], Args[7], Args[8], Args[9]);
443 ByteswapSCANFResults(Args[0], Args[1], Args[2], Args[3], Args[4],
444 Args[5], Args[6], Args[7], Args[8], Args[9]);
Chris Lattner5dec4602002-03-08 22:51:07 +0000445 return GV;
446}
447
448
Chris Lattner8f52ffd2002-01-23 21:38:07 +0000449// int clock(void) - Profiling implementation
Chris Lattnerf94811a2002-03-29 03:57:15 +0000450GenericValue lle_i_clock(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner8f52ffd2002-01-23 21:38:07 +0000451 extern int clock(void);
452 GenericValue GV; GV.IntVal = clock();
453 return GV;
454}
Chris Lattner60a9a232001-12-13 00:43:47 +0000455
Chris Lattner93f4ff72003-04-23 19:55:24 +0000456
457//===----------------------------------------------------------------------===//
458// String Functions...
459//===----------------------------------------------------------------------===//
460
461// int strcmp(const char *S1, const char *S2);
462GenericValue lle_X_strcmp(FunctionType *M, const vector<GenericValue> &Args) {
463 assert(Args.size() == 2);
464 GenericValue Ret;
465 Ret.IntVal = strcmp((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]));
466 return Ret;
467}
468
469// char *strcat(char *Dest, const char *src);
470GenericValue lle_X_strcat(FunctionType *M, const vector<GenericValue> &Args) {
471 assert(Args.size() == 2);
472 return PTOGV(strcat((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
473}
474
475// char *strcpy(char *Dest, const char *src);
476GenericValue lle_X_strcpy(FunctionType *M, const vector<GenericValue> &Args) {
477 assert(Args.size() == 2);
478 return PTOGV(strcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1])));
479}
480
Brian Gaeke2d7efbb2004-05-01 06:42:15 +0000481static GenericValue size_t_to_GV (size_t n) {
482 GenericValue Ret;
483 if (sizeof (size_t) == sizeof (uint64_t)) {
484 Ret.ULongVal = n;
485 } else {
486 assert (sizeof (size_t) == sizeof (unsigned int));
487 Ret.UIntVal = n;
488 }
489 return Ret;
490}
491
492static size_t GV_to_size_t (GenericValue GV) {
493 size_t count;
494 if (sizeof (size_t) == sizeof (uint64_t)) {
495 count = GV.ULongVal;
496 } else {
497 assert (sizeof (size_t) == sizeof (unsigned int));
498 count = GV.UIntVal;
499 }
500 return count;
501}
502
Brian Gaeke13a2e542003-12-12 15:38:06 +0000503// size_t strlen(const char *src);
Chris Lattner93f4ff72003-04-23 19:55:24 +0000504GenericValue lle_X_strlen(FunctionType *M, const vector<GenericValue> &Args) {
505 assert(Args.size() == 1);
Brian Gaeke13a2e542003-12-12 15:38:06 +0000506 size_t strlenResult = strlen ((char *) GVTOP (Args[0]));
Brian Gaeke2d7efbb2004-05-01 06:42:15 +0000507 return size_t_to_GV (strlenResult);
Chris Lattner93f4ff72003-04-23 19:55:24 +0000508}
509
Brian Gaekea7669032003-09-05 18:42:01 +0000510// char *strdup(const char *src);
511GenericValue lle_X_strdup(FunctionType *M, const vector<GenericValue> &Args) {
512 assert(Args.size() == 1);
513 return PTOGV(strdup((char*)GVTOP(Args[0])));
514}
515
Chris Lattner4c5245b2003-04-25 18:23:38 +0000516// char *__strdup(const char *src);
517GenericValue lle_X___strdup(FunctionType *M, const vector<GenericValue> &Args) {
518 assert(Args.size() == 1);
519 return PTOGV(strdup((char*)GVTOP(Args[0])));
520}
521
Chris Lattner93f4ff72003-04-23 19:55:24 +0000522// void *memset(void *S, int C, size_t N)
523GenericValue lle_X_memset(FunctionType *M, const vector<GenericValue> &Args) {
524 assert(Args.size() == 3);
Brian Gaeke2d7efbb2004-05-01 06:42:15 +0000525 size_t count = GV_to_size_t (Args[2]);
526 return PTOGV(memset(GVTOP(Args[0]), Args[1].IntVal, count));
Chris Lattner93f4ff72003-04-23 19:55:24 +0000527}
528
Chris Lattner487bb802003-04-23 20:23:16 +0000529// void *memcpy(void *Dest, void *src, size_t Size);
530GenericValue lle_X_memcpy(FunctionType *M, const vector<GenericValue> &Args) {
531 assert(Args.size() == 3);
Brian Gaeke2d7efbb2004-05-01 06:42:15 +0000532 size_t count = GV_to_size_t (Args[2]);
533 return PTOGV(memcpy((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]), count));
Chris Lattner487bb802003-04-23 20:23:16 +0000534}
Chris Lattner93f4ff72003-04-23 19:55:24 +0000535
Chris Lattner5dec4602002-03-08 22:51:07 +0000536//===----------------------------------------------------------------------===//
537// IO Functions...
538//===----------------------------------------------------------------------===//
539
Chris Lattner0313db62002-10-02 21:12:13 +0000540// getFILE - Turn a pointer in the host address space into a legit pointer in
Reid Spencer7b8a3b52004-05-25 08:46:15 +0000541// the interpreter address space. This is an identity transformation.
542#define getFILE(ptr) ((FILE*)ptr)
Chris Lattner0313db62002-10-02 21:12:13 +0000543
Chris Lattner5dec4602002-03-08 22:51:07 +0000544// FILE *fopen(const char *filename, const char *mode);
Chris Lattnerf94811a2002-03-29 03:57:15 +0000545GenericValue lle_X_fopen(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5dec4602002-03-08 22:51:07 +0000546 assert(Args.size() == 2);
Chris Lattnerd49518c2003-01-13 00:59:47 +0000547 return PTOGV(fopen((const char *)GVTOP(Args[0]),
548 (const char *)GVTOP(Args[1])));
Chris Lattner5dec4602002-03-08 22:51:07 +0000549}
550
551// int fclose(FILE *F);
Chris Lattnerf94811a2002-03-29 03:57:15 +0000552GenericValue lle_X_fclose(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5dec4602002-03-08 22:51:07 +0000553 assert(Args.size() == 1);
554 GenericValue GV;
Chris Lattnerd49518c2003-01-13 00:59:47 +0000555 GV.IntVal = fclose(getFILE(GVTOP(Args[0])));
Chris Lattner5dec4602002-03-08 22:51:07 +0000556 return GV;
557}
558
Chris Lattner7a1da7c2002-11-08 19:10:26 +0000559// int feof(FILE *stream);
560GenericValue lle_X_feof(FunctionType *M, const vector<GenericValue> &Args) {
561 assert(Args.size() == 1);
562 GenericValue GV;
563
Chris Lattnerd49518c2003-01-13 00:59:47 +0000564 GV.IntVal = feof(getFILE(GVTOP(Args[0])));
Chris Lattner7a1da7c2002-11-08 19:10:26 +0000565 return GV;
566}
567
Chris Lattner5dec4602002-03-08 22:51:07 +0000568// size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
Chris Lattnerf94811a2002-03-29 03:57:15 +0000569GenericValue lle_X_fread(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5dec4602002-03-08 22:51:07 +0000570 assert(Args.size() == 4);
Brian Gaeke2d7efbb2004-05-01 06:42:15 +0000571 size_t result;
Chris Lattner5dec4602002-03-08 22:51:07 +0000572
Brian Gaeke2d7efbb2004-05-01 06:42:15 +0000573 result = fread((void*)GVTOP(Args[0]), GV_to_size_t (Args[1]),
574 GV_to_size_t (Args[2]), getFILE(GVTOP(Args[3])));
575 return size_t_to_GV (result);
Chris Lattner5dec4602002-03-08 22:51:07 +0000576}
577
578// size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream);
Chris Lattnerf94811a2002-03-29 03:57:15 +0000579GenericValue lle_X_fwrite(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5dec4602002-03-08 22:51:07 +0000580 assert(Args.size() == 4);
Brian Gaeke2d7efbb2004-05-01 06:42:15 +0000581 size_t result;
Chris Lattner5dec4602002-03-08 22:51:07 +0000582
Brian Gaeke2d7efbb2004-05-01 06:42:15 +0000583 result = fwrite((void*)GVTOP(Args[0]), GV_to_size_t (Args[1]),
584 GV_to_size_t (Args[2]), getFILE(GVTOP(Args[3])));
585 return size_t_to_GV (result);
Chris Lattner5dec4602002-03-08 22:51:07 +0000586}
587
588// char *fgets(char *s, int n, FILE *stream);
Chris Lattnerf94811a2002-03-29 03:57:15 +0000589GenericValue lle_X_fgets(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5dec4602002-03-08 22:51:07 +0000590 assert(Args.size() == 3);
Chris Lattnerd49518c2003-01-13 00:59:47 +0000591 return GVTOP(fgets((char*)GVTOP(Args[0]), Args[1].IntVal,
592 getFILE(GVTOP(Args[2]))));
Chris Lattner5dec4602002-03-08 22:51:07 +0000593}
594
Chris Lattnera41a1952002-11-07 19:33:50 +0000595// FILE *freopen(const char *path, const char *mode, FILE *stream);
596GenericValue lle_X_freopen(FunctionType *M, const vector<GenericValue> &Args) {
597 assert(Args.size() == 3);
Chris Lattnerd49518c2003-01-13 00:59:47 +0000598 return PTOGV(freopen((char*)GVTOP(Args[0]), (char*)GVTOP(Args[1]),
599 getFILE(GVTOP(Args[2]))));
Chris Lattnera41a1952002-11-07 19:33:50 +0000600}
601
Chris Lattner5dec4602002-03-08 22:51:07 +0000602// int fflush(FILE *stream);
Chris Lattnerf94811a2002-03-29 03:57:15 +0000603GenericValue lle_X_fflush(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner5dec4602002-03-08 22:51:07 +0000604 assert(Args.size() == 1);
605 GenericValue GV;
Chris Lattnerd49518c2003-01-13 00:59:47 +0000606 GV.IntVal = fflush(getFILE(GVTOP(Args[0])));
Chris Lattner0313db62002-10-02 21:12:13 +0000607 return GV;
608}
609
610// int getc(FILE *stream);
611GenericValue lle_X_getc(FunctionType *M, const vector<GenericValue> &Args) {
612 assert(Args.size() == 1);
613 GenericValue GV;
Chris Lattnerd49518c2003-01-13 00:59:47 +0000614 GV.IntVal = getc(getFILE(GVTOP(Args[0])));
Chris Lattner5dec4602002-03-08 22:51:07 +0000615 return GV;
616}
617
Chris Lattner26a4a1f2003-04-23 19:20:50 +0000618// int _IO_getc(FILE *stream);
619GenericValue lle_X__IO_getc(FunctionType *F, const vector<GenericValue> &Args) {
620 return lle_X_getc(F, Args);
621}
622
Chris Lattner85290202002-11-06 22:59:28 +0000623// int fputc(int C, FILE *stream);
624GenericValue lle_X_fputc(FunctionType *M, const vector<GenericValue> &Args) {
625 assert(Args.size() == 2);
626 GenericValue GV;
Chris Lattnerd49518c2003-01-13 00:59:47 +0000627 GV.IntVal = fputc(Args[0].IntVal, getFILE(GVTOP(Args[1])));
Chris Lattner85290202002-11-06 22:59:28 +0000628 return GV;
629}
630
631// int ungetc(int C, FILE *stream);
632GenericValue lle_X_ungetc(FunctionType *M, const vector<GenericValue> &Args) {
633 assert(Args.size() == 2);
634 GenericValue GV;
Chris Lattnerd49518c2003-01-13 00:59:47 +0000635 GV.IntVal = ungetc(Args[0].IntVal, getFILE(GVTOP(Args[1])));
Chris Lattner85290202002-11-06 22:59:28 +0000636 return GV;
637}
638
Brian Gaeke3b9474e2004-06-16 02:56:40 +0000639// int ferror (FILE *stream);
640GenericValue lle_X_ferror(FunctionType *M, const vector<GenericValue> &Args) {
641 assert(Args.size() == 1);
642 GenericValue GV;
643 GV.IntVal = ferror (getFILE(GVTOP(Args[0])));
644 return GV;
645}
646
Chris Lattnerc3a84092002-11-06 23:05:03 +0000647// int fprintf(FILE *,sbyte *, ...) - a very rough implementation to make output
648// useful.
649GenericValue lle_X_fprintf(FunctionType *M, const vector<GenericValue> &Args) {
Chris Lattner16106662003-04-21 22:43:20 +0000650 assert(Args.size() >= 2);
Chris Lattnerc3a84092002-11-06 23:05:03 +0000651 char Buffer[10000];
652 vector<GenericValue> NewArgs;
Chris Lattnerd49518c2003-01-13 00:59:47 +0000653 NewArgs.push_back(PTOGV(Buffer));
Chris Lattnerc3a84092002-11-06 23:05:03 +0000654 NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end());
Chris Lattnerd49518c2003-01-13 00:59:47 +0000655 GenericValue GV = lle_X_sprintf(M, NewArgs);
Chris Lattnerc3a84092002-11-06 23:05:03 +0000656
Chris Lattnerd49518c2003-01-13 00:59:47 +0000657 fputs(Buffer, getFILE(GVTOP(Args[0])));
Chris Lattnerc3a84092002-11-06 23:05:03 +0000658 return GV;
659}
660
Chris Lattnerbaf08eb2001-09-10 04:50:17 +0000661} // End extern "C"
Chris Lattner7fd51b52001-10-30 20:28:00 +0000662
663
Chris Lattner470754e2003-05-08 16:18:31 +0000664void Interpreter::initializeExternalFunctions() {
Chris Lattnerd7c742f2001-11-03 10:15:32 +0000665 FuncNames["lle_Vb_putchar"] = lle_Vb_putchar;
666 FuncNames["lle_ii_putchar"] = lle_ii_putchar;
667 FuncNames["lle_VB_putchar"] = lle_VB_putchar;
Chris Lattnerd7c742f2001-11-03 10:15:32 +0000668 FuncNames["lle_X_exit"] = lle_X_exit;
Chris Lattner13194292002-05-20 21:17:16 +0000669 FuncNames["lle_X_abort"] = lle_X_abort;
Chris Lattnerd7c742f2001-11-03 10:15:32 +0000670 FuncNames["lle_X_malloc"] = lle_X_malloc;
Chris Lattner93f4ff72003-04-23 19:55:24 +0000671 FuncNames["lle_X_calloc"] = lle_X_calloc;
Chris Lattnerd7c742f2001-11-03 10:15:32 +0000672 FuncNames["lle_X_free"] = lle_X_free;
Chris Lattner5ba75732001-11-26 18:18:18 +0000673 FuncNames["lle_X_atoi"] = lle_X_atoi;
Chris Lattnerd7c742f2001-11-03 10:15:32 +0000674 FuncNames["lle_X_pow"] = lle_X_pow;
Chris Lattner568a7702002-02-18 19:06:25 +0000675 FuncNames["lle_X_exp"] = lle_X_exp;
Chris Lattner9754aba2001-11-13 05:46:08 +0000676 FuncNames["lle_X_log"] = lle_X_log;
Chris Lattner5ba75732001-11-26 18:18:18 +0000677 FuncNames["lle_X_floor"] = lle_X_floor;
678 FuncNames["lle_X_srand"] = lle_X_srand;
Chris Lattner9754aba2001-11-13 05:46:08 +0000679 FuncNames["lle_X_drand48"] = lle_X_drand48;
680 FuncNames["lle_X_srand48"] = lle_X_srand48;
681 FuncNames["lle_X_lrand48"] = lle_X_lrand48;
Chris Lattner5cc31882001-11-06 21:52:18 +0000682 FuncNames["lle_X_sqrt"] = lle_X_sqrt;
Chris Lattnerd49518c2003-01-13 00:59:47 +0000683 FuncNames["lle_X_puts"] = lle_X_puts;
Chris Lattnerd7c742f2001-11-03 10:15:32 +0000684 FuncNames["lle_X_printf"] = lle_X_printf;
Chris Lattner60a9a232001-12-13 00:43:47 +0000685 FuncNames["lle_X_sprintf"] = lle_X_sprintf;
Chris Lattner5dec4602002-03-08 22:51:07 +0000686 FuncNames["lle_X_sscanf"] = lle_X_sscanf;
Chris Lattnerd3183712003-03-31 22:12:37 +0000687 FuncNames["lle_X_scanf"] = lle_X_scanf;
Chris Lattner8f52ffd2002-01-23 21:38:07 +0000688 FuncNames["lle_i_clock"] = lle_i_clock;
Chris Lattner93f4ff72003-04-23 19:55:24 +0000689
690 FuncNames["lle_X_strcmp"] = lle_X_strcmp;
691 FuncNames["lle_X_strcat"] = lle_X_strcat;
692 FuncNames["lle_X_strcpy"] = lle_X_strcpy;
693 FuncNames["lle_X_strlen"] = lle_X_strlen;
Chris Lattner470754e2003-05-08 16:18:31 +0000694 FuncNames["lle_X___strdup"] = lle_X___strdup;
Chris Lattner93f4ff72003-04-23 19:55:24 +0000695 FuncNames["lle_X_memset"] = lle_X_memset;
Chris Lattner487bb802003-04-23 20:23:16 +0000696 FuncNames["lle_X_memcpy"] = lle_X_memcpy;
Chris Lattner93f4ff72003-04-23 19:55:24 +0000697
Chris Lattner5dec4602002-03-08 22:51:07 +0000698 FuncNames["lle_X_fopen"] = lle_X_fopen;
699 FuncNames["lle_X_fclose"] = lle_X_fclose;
Chris Lattner7a1da7c2002-11-08 19:10:26 +0000700 FuncNames["lle_X_feof"] = lle_X_feof;
Chris Lattner5dec4602002-03-08 22:51:07 +0000701 FuncNames["lle_X_fread"] = lle_X_fread;
702 FuncNames["lle_X_fwrite"] = lle_X_fwrite;
703 FuncNames["lle_X_fgets"] = lle_X_fgets;
704 FuncNames["lle_X_fflush"] = lle_X_fflush;
Chris Lattner85290202002-11-06 22:59:28 +0000705 FuncNames["lle_X_fgetc"] = lle_X_getc;
Chris Lattner0313db62002-10-02 21:12:13 +0000706 FuncNames["lle_X_getc"] = lle_X_getc;
Chris Lattner26a4a1f2003-04-23 19:20:50 +0000707 FuncNames["lle_X__IO_getc"] = lle_X__IO_getc;
Chris Lattner85290202002-11-06 22:59:28 +0000708 FuncNames["lle_X_fputc"] = lle_X_fputc;
709 FuncNames["lle_X_ungetc"] = lle_X_ungetc;
Chris Lattnerc3a84092002-11-06 23:05:03 +0000710 FuncNames["lle_X_fprintf"] = lle_X_fprintf;
Chris Lattnera41a1952002-11-07 19:33:50 +0000711 FuncNames["lle_X_freopen"] = lle_X_freopen;
Chris Lattner7fd51b52001-10-30 20:28:00 +0000712}
Brian Gaeke960707c2003-11-11 22:41:34 +0000713