- Timestamp:
- 08/15/08 12:39:56 (5 months ago)
- Location:
- trunk/rphp
- Files:
-
- 6 modified
-
compiler/pDriver.cpp (modified) (6 diffs)
-
runtime/include/pOutputBuffer.h (modified) (2 diffs)
-
runtime/include/pOutputManager.h (modified) (1 diff)
-
runtime/include/pRuntime.h (modified) (3 diffs)
-
runtime/pOutputManager.cpp (modified) (1 diff)
-
runtime/pRuntime.cpp (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/rphp/compiler/pDriver.cpp
r629 r630 27 27 #include <boost/algorithm/string.hpp> 28 28 29 #include "llvm/GlobalVariable.h" 29 30 #include "llvm/System/DynamicLibrary.h" 30 31 #include "llvm/Bitcode/ReaderWriter.h" … … 56 57 */ 57 58 void pDriver::execute(string fileName) { 58 cout << "executing file: " << fileName << endl;59 //cout << "executing file: " << fileName << endl; 59 60 // TODO: determine file type, then run executeBC or executePHP 60 61 executeBC(fileName); … … 65 66 */ 66 67 void pDriver::executeBC(string fileName) { 67 68 cout << "executing compiled bytecode: " << fileName << endl;69 68 70 69 // Now we create the JIT. … … 123 122 EE->runStaticConstructorsDestructors(true); 124 123 125 cout << "module ran, return value is: " << gv.IntVal.toStringSigned(10) << endl;126 127 124 delete EE; 128 125 … … 185 182 186 183 // Create the main function: first create the type 'int ()' 187 llvm::FunctionType *FT = llvm::FunctionType::get(llvm::Type:: Int32Ty, std::vector<const llvm::Type*>(),184 llvm::FunctionType *FT = llvm::FunctionType::get(llvm::Type::VoidTy, std::vector<const llvm::Type*>(), 188 185 /*not vararg*/false); 189 186 … … 196 193 llvm::BasicBlock *BB = llvm::BasicBlock::Create("EntryBlock", F); 197 194 198 // RUNTIME startup/shutdown test 195 // ** STARTUP ** 196 // runtime engine pointer type 199 197 const llvm::Type* rEnginePointer = llvm::PointerType::get(llvm::Type::Int8Ty,0); 198 199 // startup function type 200 200 llvm::FunctionType *runtimeStartupFuncType = llvm::FunctionType::get(rEnginePointer, std::vector<const llvm::Type*>(), false); 201 // startup function 201 202 llvm::Function *runtimeStartupFunc = llvm::Function::Create(runtimeStartupFuncType, llvm::Function::ExternalLinkage, "rphp_newRuntimeEngine", M); 202 203 // startup instruction call 203 204 llvm::Instruction *runtimeStartInstr = llvm::CallInst::Create(runtimeStartupFunc, "runtime"); 204 205 206 // ** hello world ** 207 llvm::ArrayType* ArrayTy_0 = llvm::ArrayType::get(llvm::IntegerType::get(8), 12); 208 llvm::PointerType* PointerTy_4 = llvm::PointerType::get(llvm::IntegerType::get(8), 0); 209 llvm::GlobalVariable* gvar_array__str = new llvm::GlobalVariable( 210 /*Type=*/ArrayTy_0, 211 /*isConstant=*/true, 212 /*Linkage=*/llvm::GlobalValue::InternalLinkage, 213 /*Initializer=*/0, // has initializer, specified below 214 /*Name=*/".str", 215 M); 216 217 // Constant Definitions 218 llvm::Constant* const_array_7 = llvm::ConstantArray::get("hello world", true); 219 std::vector<llvm::Constant*> const_ptr_8_indices; 220 llvm::Constant* const_int32_9 = llvm::Constant::getNullValue(llvm::IntegerType::get(32)); 221 const_ptr_8_indices.push_back(const_int32_9); 222 const_ptr_8_indices.push_back(const_int32_9); 223 llvm::Constant* const_ptr_8 = llvm::ConstantExpr::getGetElementPtr(gvar_array__str, &const_ptr_8_indices[0], const_ptr_8_indices.size() ); 224 llvm::UndefValue* const_int32_10 = llvm::UndefValue::get(llvm::IntegerType::get(32)); 225 226 // Global Variable Definitions 227 gvar_array__str->setInitializer(const_array_7); 228 229 // argument sig for print function 230 std::vector<const llvm::Type*> printSig; 231 printSig.push_back(rEnginePointer); 232 printSig.push_back(PointerTy_4); 233 // print function type 234 llvm::FunctionType *printFuncType = llvm::FunctionType::get(llvm::Type::VoidTy, printSig, false); 235 // print function 236 llvm::Function *printFunc = llvm::Function::Create(printFuncType, llvm::Function::ExternalLinkage, "rphp_print_cstr", M); 237 // push args 238 std::vector<llvm::Value*> printArgsV; 239 printArgsV.push_back(runtimeStartInstr); 240 printArgsV.push_back(const_ptr_8); 241 // print instruction call 242 llvm::Instruction *printInstr = llvm::CallInst::Create(printFunc, printArgsV.begin(), printArgsV.end()); 243 244 245 // ** SHUTDOWN ** 246 // argument sig for shutdown function 205 247 std::vector<const llvm::Type*> engineSig(1, rEnginePointer); 248 // shutdown function type 206 249 llvm::FunctionType *runtimeDeleteFuncType = llvm::FunctionType::get(llvm::Type::VoidTy, engineSig, false); 250 // shutdown function 207 251 llvm::Function *runtimeDeleteFunc = llvm::Function::Create(runtimeDeleteFuncType, llvm::Function::ExternalLinkage, "rphp_deleteRuntimeEngine", M); 208 252 209 std::vector<llvm::Value*> ArgsV; 210 ArgsV.push_back(runtimeStartInstr); 211 212 llvm::Instruction *runtimeDeleteInstr = llvm::CallInst::Create(runtimeDeleteFunc, ArgsV.begin(), ArgsV.end()); 253 // push args 254 std::vector<llvm::Value*> shutdownArgsV; 255 shutdownArgsV.push_back(runtimeStartInstr); 256 257 // shutdown instruction call 258 llvm::Instruction *runtimeDeleteInstr = llvm::CallInst::Create(runtimeDeleteFunc, shutdownArgsV.begin(), shutdownArgsV.end()); 213 259 214 260 // Get pointers to the constant integers... 215 llvm::Value *Two = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2);216 llvm::Value *Three = llvm::ConstantInt::get(llvm::Type::Int32Ty, 3);261 //llvm::Value *Two = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2); 262 //llvm::Value *Three = llvm::ConstantInt::get(llvm::Type::Int32Ty, 3); 217 263 218 264 // Create the add instruction... does not insert... 219 llvm::Instruction *Add = llvm::BinaryOperator::create(llvm::Instruction::Add, Two, Three, 220 "addresult"); 265 //llvm::Instruction *Add = llvm::BinaryOperator::create(llvm::Instruction::Add, Two, Three, "addresult"); 221 266 222 267 // explicitly insert it into the basic block... 223 BB->getInstList().push_back(Add);268 //BB->getInstList().push_back(Add); 224 269 225 270 BB->getInstList().push_back(runtimeStartInstr); 271 BB->getInstList().push_back(printInstr); 226 272 BB->getInstList().push_back(runtimeDeleteInstr); 227 273 228 274 // Create the return instruction and add it to the basic block 229 BB->getInstList().push_back(llvm::ReturnInst::Create(Add)); 275 BB->getInstList().push_back(llvm::ReturnInst::Create()); 276 //BB->getInstList().push_back(llvm::ReturnInst::Create(Add)); 230 277 231 278 if (llvm::verifyModule(*M, llvm::PrintMessageAction)) { -
trunk/rphp/runtime/include/pOutputBuffer.h
r598 r630 26 26 class pOutputBuffer { 27 27 28 // default size of a new output buffer 29 // note that for unicode strings, memory space is 2*defBufSize 30 static const std::size_t defBufSize = 512; 28 public: 31 29 32 30 typedef enum { bufTypeBinary, bufTypeUnicode } bufTypeT; 33 31 34 private:35 36 pUString *uBuffer;37 pBString *bBuffer;38 bufTypeT bType;39 40 public:41 42 32 // constructors 43 33 44 // default builds a unicode buffer, default size 45 pOutputBuffer() : uBuffer(new pUString(defBufSize,' ',0)), bType(bufTypeUnicode) { } 46 47 // specify type, default size 48 pOutputBuffer(bufTypeT t) : bType(t) { 34 // specify type 35 pOutputBuffer(bufTypeT t) : bBuffer(0), uBuffer(0), bType(t) { 49 36 switch (t) { 50 37 case bufTypeBinary: 51 bBuffer = new pBString( defBufSize,' ');38 bBuffer = new pBString(); 52 39 break; 53 40 case bufTypeUnicode: 54 uBuffer = new pUString(defBufSize,' ',0); 55 break; 56 } 57 } 58 59 // specify type, size 60 pOutputBuffer(bufTypeT t, std::size_t s) : bType(t) { 61 switch (t) { 62 case bufTypeBinary: 63 bBuffer = new pBString(s,' '); 64 break; 65 case bufTypeUnicode: 66 uBuffer = new pUString(s,' ',0); 41 uBuffer = new pUString(); 67 42 break; 68 43 } … … 77 52 } 78 53 54 const char* getRawBuffer() { 55 switch (bType) { 56 case bufTypeBinary: 57 return bBuffer->c_str(); 58 case bufTypeUnicode: 59 return (const char*)uBuffer->getTerminatedBuffer(); 60 } 61 } 62 63 void operator<< (const pBString& str) { 64 switch (bType) { 65 case bufTypeBinary: 66 bBuffer->append(str); 67 break; 68 case bufTypeUnicode: 69 // TODO: this doesn't seem so efficient. but how often will it be used? 70 uBuffer->append(pUString(str.c_str(),str.length(), US_INV)); 71 break; 72 } 73 } 74 75 void operator<< (const pUString& str) { 76 if (bType == bufTypeBinary) { 77 // convert to unicode buffer 78 uBuffer = new pUString(bBuffer->c_str(), bBuffer->length(), US_INV); 79 delete bBuffer; 80 } 81 uBuffer->append(str); 82 } 83 84 private: 85 86 pUString *uBuffer; 87 pBString *bBuffer; 88 bufTypeT bType; 89 79 90 }; 80 91 -
trunk/rphp/runtime/include/pOutputManager.h
r598 r630 22 22 #include <stack> 23 23 #include "pOutputBuffer.h" 24 #include "pTypes.h" 24 25 25 26 namespace rphp { 27 28 class pRuntimeEngine; 26 29 27 30 class pOutputManager { 28 31 29 32 private: 30 std::stack<pOutputBuffer> bufferStack; 33 pRuntimeEngine* runtime; 34 std::stack<pOutputBuffer*> bufferStack; 31 35 32 36 public: 33 37 34 38 // constructors 35 pOutputManager( ) {39 pOutputManager(pRuntimeEngine *r) : runtime(r) { 36 40 // default output buffer 37 bufferStack.push(pOutputBuffer()); 41 // TODO: check the runtime config for which type of default buffer to use 42 bufferStack.push(new pOutputBuffer(pOutputBuffer::bufTypeBinary)); 43 } 44 45 ~pOutputManager() { 46 flushAndFreeAll(); 47 } 48 49 // flush one or more buffers 50 void flushAndFreeAll(); 51 52 // printing to the current buffer 53 void print(pBString str) { 54 if (bufferStack.empty()) 55 return; 56 *bufferStack.top() << str; 57 } 58 59 void print(pUString str) { 60 if (bufferStack.empty()) 61 return; 62 *bufferStack.top() << str; 38 63 } 39 64 -
trunk/rphp/runtime/include/pRuntime.h
r629 r630 31 31 32 32 private: 33 34 // output buffering management35 pOutputManager* outputManager;36 37 33 // extension manager 38 34 // --> for loading and registering dynamic extensions (pcre, mysql, etc) and their associated functions, classes … … 69 65 pFunctionManager* functionManager; 70 66 67 // output buffering management 68 pOutputManager* outputManager; 69 71 70 }; 72 71 … … 82 81 void rphp_deleteRuntimeEngine(rphp::pRuntimeEngine*); 83 82 83 // print to runtime output buffer 84 void rphp_print_cstr(rphp::pRuntimeEngine*, char* str); 85 84 86 } 85 87 -
trunk/rphp/runtime/pOutputManager.cpp
r598 r630 17 17 * ***** END LICENSE BLOCK ***** */ 18 18 19 #include <iostream> 19 20 #include "pOutputManager.h" 20 21 21 22 namespace rphp { 23 24 void pOutputManager::flushAndFreeAll() { 25 while( !bufferStack.empty() ) { 26 std::cout << bufferStack.top()->getRawBuffer(); 27 delete bufferStack.top(); 28 bufferStack.pop(); 29 } 30 } 22 31 23 32 -
trunk/rphp/runtime/pRuntime.cpp
r629 r630 20 20 #include "pRuntime.h" 21 21 #include "pExtManager.h" 22 #include "pOutputManager.h" 22 23 #include "pFunctionManager.h" 23 24 … … 25 26 26 27 pRuntimeEngine::pRuntimeEngine() : extManager(new pExtManager(this)), 27 functionManager(new pFunctionManager(this)) 28 functionManager(new pFunctionManager(this)), 29 outputManager(new pOutputManager(this)) 28 30 { 29 std::cout << "runtime is starting" << std::endl;30 31 31 // runtime initialization 32 32 extManager->startUp(); … … 37 37 pRuntimeEngine::~pRuntimeEngine() { 38 38 39 std::cout << "runtime is shutting down" << std::endl;40 41 39 // runtime shutdown 40 delete outputManager; // will flush 42 41 delete functionManager; 43 42 delete extManager; … … 52 51 // create a new runtime engine 53 52 rphp::pRuntimeEngine* rphp_newRuntimeEngine() { 54 std::cout << "in C API runtime now" << std::endl;55 53 rphp::pRuntimeEngine* rt = new rphp::pRuntimeEngine(); 56 54 return rt; … … 59 57 // destroy runtime engine 60 58 void rphp_deleteRuntimeEngine(rphp::pRuntimeEngine* e) { 61 std::cout << "closing in C API runtime now" << std::endl;62 59 delete e; 63 60 } 64 61 62 // print a c string to the current output buffer in the given runtime 63 void rphp_print_cstr(rphp::pRuntimeEngine* e, char* str) { 64 e->outputManager->print(rphp::pBString(str)); 65 } 66 67 65 68 } 66 69
