diff options
author | Chris Lattner <sabre@nondot.org> | 2001-12-13 00:43:47 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2001-12-13 00:43:47 +0000 |
commit | e7c6f726c8d18ff8803d9b3dc3a8443496002044 (patch) | |
tree | 6c565a619289c7b385d2b18f663d808d268eaf7c /lib/ExecutionEngine | |
parent | 9a49b210c1ae2c619ecd969b5ec17a68690928a9 (diff) | |
download | external_llvm-e7c6f726c8d18ff8803d9b3dc3a8443496002044.zip external_llvm-e7c6f726c8d18ff8803d9b3dc3a8443496002044.tar.gz external_llvm-e7c6f726c8d18ff8803d9b3dc3a8443496002044.tar.bz2 |
Implement sprintf
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1445 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/ExecutionEngine')
-rw-r--r-- | lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp | 36 |
1 files changed, 25 insertions, 11 deletions
diff --git a/lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp b/lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp index 4af07ad..a54a6f1 100644 --- a/lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp +++ b/lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp @@ -283,10 +283,12 @@ GenericValue lle_X_srand(MethodType *M, const vector<GenericValue> &Args) { return GenericValue(); } -// int printf(sbyte *, ...) - a very rough implementation to make output useful. -GenericValue lle_X_printf(MethodType *M, const vector<GenericValue> &Args) { - const char *FmtStr = (const char *)Args[0].PointerVal; - unsigned ArgNo = 1; +// int sprintf(sbyte *, sbyte *, ...) - a very rough implementation to make +// output useful. +GenericValue lle_X_sprintf(MethodType *M, const vector<GenericValue> &Args) { + char *OutputBuffer = (char *)Args[0].PointerVal; + const char *FmtStr = (const char *)Args[1].PointerVal; + unsigned ArgNo = 2; // printf should return # chars printed. This is completely incorrect, but // close enough for now. @@ -295,14 +297,11 @@ GenericValue lle_X_printf(MethodType *M, const vector<GenericValue> &Args) { switch (*FmtStr) { case 0: return GV; // Null terminator... default: // Normal nonspecial character - cout << *FmtStr++; + sprintf(OutputBuffer++, "%c", *FmtStr++); break; case '\\': { // Handle escape codes - char Buffer[3]; - Buffer[0] = *FmtStr++; - Buffer[1] = *FmtStr++; - Buffer[2] = 0; - cout << Buffer; + sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1)); + FmtStr += 2; OutputBuffer += 2; break; } case '%': { // Handle format specifiers @@ -341,13 +340,27 @@ GenericValue lle_X_printf(MethodType *M, const vector<GenericValue> &Args) { default: cout << "<unknown printf code '" << *FmtStr << "'!>"; ArgNo++; break; } - cout << Buffer; + strcpy(OutputBuffer, Buffer); + OutputBuffer += strlen(Buffer); } break; } } } +// int printf(sbyte *, ...) - a very rough implementation to make output useful. +GenericValue lle_X_printf(MethodType *M, const vector<GenericValue> &Args) { + char Buffer[10000]; + vector<GenericValue> NewArgs; + GenericValue GV; GV.PointerVal = (PointerTy)Buffer; + NewArgs.push_back(GV); + NewArgs.insert(NewArgs.end(), Args.begin(), Args.end()); + GV = lle_X_sprintf(M, NewArgs); + cout << Buffer; + return GV; +} + + } // End extern "C" @@ -384,4 +397,5 @@ void Interpreter::initializeExternalMethods() { FuncNames["lle_X_lrand48"] = lle_X_lrand48; FuncNames["lle_X_sqrt"] = lle_X_sqrt; FuncNames["lle_X_printf"] = lle_X_printf; + FuncNames["lle_X_sprintf"] = lle_X_sprintf; } |