Implement sqrt, implement printf better, simpler.

llvm-svn: 1161
This commit is contained in:
Chris Lattner 2001-11-06 21:52:18 +00:00
parent a688655cba
commit d907c7c9eb

View File

@ -15,6 +15,7 @@
#include <dlfcn.h> #include <dlfcn.h>
#include <link.h> #include <link.h>
#include <math.h> #include <math.h>
#include <stdio.h>
typedef GenericValue (*ExFunc)(MethodType *, const vector<GenericValue> &); typedef GenericValue (*ExFunc)(MethodType *, const vector<GenericValue> &);
static map<const Method *, ExFunc> Functions; static map<const Method *, ExFunc> Functions;
@ -207,17 +208,26 @@ GenericValue lle_X_malloc(MethodType *M, const vector<GenericValue> &Args) {
// void free(void *) // void free(void *)
GenericValue lle_X_free(MethodType *M, const vector<GenericValue> &Args) { GenericValue lle_X_free(MethodType *M, const vector<GenericValue> &Args) {
assert(Args.size() == 1);
free((void*)Args[0].PointerVal); free((void*)Args[0].PointerVal);
return GenericValue(); return GenericValue();
} }
// double pow(double, double) // double pow(double, double)
GenericValue lle_X_pow(MethodType *M, const vector<GenericValue> &Args) { GenericValue lle_X_pow(MethodType *M, const vector<GenericValue> &Args) {
assert(Args.size() == 2);
GenericValue GV; GenericValue GV;
GV.DoubleVal = pow(Args[0].DoubleVal, Args[1].DoubleVal); GV.DoubleVal = pow(Args[0].DoubleVal, Args[1].DoubleVal);
return GV; return GV;
} }
// double sqrt(double)
GenericValue lle_X_sqrt(MethodType *M, const vector<GenericValue> &Args) {
assert(Args.size() == 1);
GenericValue GV;
GV.DoubleVal = sqrt(Args[0].DoubleVal);
return GV;
}
// int printf(sbyte *, ...) - a very rough implementation to make output useful. // int printf(sbyte *, ...) - a very rough implementation to make output useful.
GenericValue lle_X_printf(MethodType *M, const vector<GenericValue> &Args) { GenericValue lle_X_printf(MethodType *M, const vector<GenericValue> &Args) {
@ -249,22 +259,26 @@ GenericValue lle_X_printf(MethodType *M, const vector<GenericValue> &Args) {
FmtStr++; FmtStr++;
} }
switch (*FmtStr) { if (*FmtStr == '%')
case '%': cout << *FmtStr; break; // %% cout << *FmtStr; // %%
case 'd': // %d %i else {
case 'i': cout << Args[ArgNo++].IntVal; break; char Fmt[] = "%d", Buffer[1000] = "";
case 'u': cout << Args[ArgNo++].UIntVal; break; // %u Fmt[1] = *FmtStr;
case 'o': cout << oct << Args[ArgNo++].UIntVal << dec; break; // %o
case 'x': switch (*FmtStr) {
case 'X': cout << hex << Args[ArgNo++].UIntVal << dec; break; // %x %X case 'c':
case 'e': case 'E': case 'g': case 'G': // %[eEgG] sprintf(Buffer, Fmt, Args[ArgNo++].SByteVal); break;
cout /*<< std::scientific*/ << Args[ArgNo++].DoubleVal case 'd': case 'i':
/*<< std::fixed*/; break; case 'u': case 'o':
case 'f': cout << Args[ArgNo++].DoubleVal; break; // %f case 'x': case 'X':
case 'c': cout << Args[ArgNo++].UByteVal; break; // %c sprintf(Buffer, Fmt, Args[ArgNo++].IntVal); break;
case 's': cout << (char*)Args[ArgNo++].PointerVal; break; // %s case 'e': case 'E': case 'g': case 'G': case 'f':
default: cout << "<unknown printf code '" << *FmtStr << "'!>"; sprintf(Buffer, Fmt, Args[ArgNo++].DoubleVal); break;
ArgNo++; break; case 's': cout << (char*)Args[ArgNo++].PointerVal; break; // %s
default: cout << "<unknown printf code '" << *FmtStr << "'!>";
ArgNo++; break;
}
cout << Buffer;
} }
++FmtStr; ++FmtStr;
break; break;
@ -300,5 +314,6 @@ void Interpreter::initializeExternalMethods() {
FuncNames["lle_X_malloc"] = lle_X_malloc; FuncNames["lle_X_malloc"] = lle_X_malloc;
FuncNames["lle_X_free"] = lle_X_free; FuncNames["lle_X_free"] = lle_X_free;
FuncNames["lle_X_pow"] = lle_X_pow; FuncNames["lle_X_pow"] = lle_X_pow;
FuncNames["lle_X_sqrt"] = lle_X_sqrt;
FuncNames["lle_X_printf"] = lle_X_printf; FuncNames["lle_X_printf"] = lle_X_printf;
} }