void CgStmt::Visit(IRForLoop* loop, int ignored) { // Create basic blocks. llvm::BasicBlock* condBlock = llvm::BasicBlock::Create(*mContext, "cond"); llvm::BasicBlock* bodyBlock = llvm::BasicBlock::Create(*mContext, "body"); llvm::BasicBlock* exitBlock = llvm::BasicBlock::Create(*mContext, "exit"); llvm::BasicBlock* iterBlock = llvm::BasicBlock::Create(*mContext, "iter"); // Keep track of the exit block and iterate block, which might be targeted // by break/continue statements. loop->mBreakTarget = exitBlock; loop->mContinueTarget = iterBlock; // Add condition block and generate a jump to it. llvm::Function* function = mBuilder->GetInsertBlock()->getParent(); function->getBasicBlockList().push_back(condBlock); mBuilder->CreateBr(condBlock); // Generate code for the loop condition. mBuilder->SetInsertPoint(condBlock); Codegen(loop->GetCondStmt()); // Convert the condition to an LLVM value, and then convert the resulting // bool (i32) to a bit for LLVM's conditional branch instruction. llvm::Value* condVal = mValues->ConvertArg(loop->GetCond()); condVal = mValues->BoolToBit(condVal); // Generate conditional branch to the loop body vs. the loop exit. mBuilder->CreateCondBr(condVal, bodyBlock, exitBlock); // Generate code for the loop body. function->getBasicBlockList().push_back(bodyBlock); mBuilder->SetInsertPoint(bodyBlock); // Generate code for the loop body. Codegen(loop->GetBody()); // Generate a jump to the 'iterate' block and generate the loop iteration // code, followed by a jump to the conditional test at the start of the // loop. mBuilder->CreateBr(iterBlock); function->getBasicBlockList().push_back(iterBlock); mBuilder->SetInsertPoint(iterBlock); Codegen(loop->GetIterateStmt()); mBuilder->CreateBr(condBlock); // Statements following the loop will be generated in the exit block. function->getBasicBlockList().push_back(exitBlock); mBuilder->SetInsertPoint(exitBlock); }
void CgStmt::Visit(IRSeq* seq, int ignored) { const IRStmts& stmts = seq->GetStmts(); IRStmts::const_iterator it; for (it = stmts.begin(); it != stmts.end(); ++it) Codegen(*it); }
Value * quote__S_Double_Double( struct villCompiler * vill, struct graph_node * node ) { Value * result = NULL; vill -> debug_flags & DEBUG_USER && fprintf( stderr, "<quote__S_Double_Double>" ); graph_node * nibble = ast_mapentry( node, "nibble" ); function_pointer Codegen; Codegen = (function_pointer) nibble -> data; assert( Codegen != NULL ); result = Codegen( vill, nibble ); vill -> debug_flags & DEBUG_USER && fprintf( stderr, "</quote__S_Double_Double>" ); return result; }
Value * term__S_scope_declarator( struct villCompiler * vill, struct graph_node * node ) { Value * result = NULL; vill -> debug_flags & DEBUG_USER && fprintf( stderr, "<term__S_scope_declarator>" ); graph_node * scope_declarator; scope_declarator = ast_mapentry( node, "scope_declarator" ); assert( scope_declarator != NULL ); function_pointer Codegen; Codegen = (function_pointer) scope_declarator -> data; assert( Codegen != NULL ); result = Codegen( vill, scope_declarator ); vill -> debug_flags & DEBUG_USER && fprintf( stderr, "</term__S_scope_declarator>" ); return result; }
void CgStmt::Visit(IRIfStmt* ifStmt, int ignored) { // Convert the condition to an LLVM value, and then convert the resulting // bool (i32) to a bit for LLVM's conditional branch instruction. llvm::Value* condVal = mValues->ConvertArg(ifStmt->GetCond()); condVal = mValues->BoolToBit(condVal); // Construct basic blocks for "then" and "else" branches, along with a // "merge" block for the code following the "if" statement. llvm::Function* function = mBuilder->GetInsertBlock()->getParent(); llvm::BasicBlock *thenBlock = llvm::BasicBlock::Create(*mContext, "then", function); llvm::BasicBlock *elseBlock = llvm::BasicBlock::Create(*mContext, "else"); llvm::BasicBlock *mergeBlock = llvm::BasicBlock::Create(*mContext, "ifcont"); // Generate conditional branch instruction. mBuilder->CreateCondBr(condVal, thenBlock, elseBlock); // Generate code for 'then' branch. mBuilder->SetInsertPoint(thenBlock); Codegen(ifStmt->GetThen()); mBuilder->CreateBr(mergeBlock); // Add the else block to the function and generate code for 'else' branch. function->getBasicBlockList().push_back(elseBlock); mBuilder->SetInsertPoint(elseBlock); Codegen(ifStmt->GetElse()); mBuilder->CreateBr(mergeBlock); // Add the merge block to the function and set the builder insertion point // before returning. function->getBasicBlockList().push_back(mergeBlock); mBuilder->SetInsertPoint(mergeBlock); }
JNIEXPORT jstring JNICALL Java_com_appunite_ffmpeg_FFmpegPlayer_codegen(JNIEnv *env, jobject thiz, jfloatArray pcmData, jint numSamples) { // get the contents of the java array as native floats float *data = (float *) env->GetFloatArrayElements(pcmData, 0); // invoke the codegen Codegen c = Codegen(data, (unsigned int) numSamples, 0); const char *code = c.getCodeString().c_str(); // release the native array as we're done with them env->ReleaseFloatArrayElements(pcmData, data, 0); // return the fingerprint string return env->NewStringUTF(code); }
JNIEXPORT jstring JNICALL Java_musaic_songRecognizer_Codegen_EchoprintCodegen (JNIEnv *env, jobject thiz, jfloatArray javaPcmData, jint numSamples) { // cast de la jfloatArray la float* float *pcmData = (float *)env->GetFloatArrayElements(javaPcmData, 0); // utilizeaza libraria Echoprint pentru a obtine hashcode-ul Codegen c = Codegen(pcmData, (unsigned int)numSamples, 0); const char *code = c.getCodeString().c_str(); // elibereaza resursele folosite env->ReleaseFloatArrayElements(javaPcmData, pcmData , 0); // returneaza hashcode-ul return env->NewStringUTF(code); }
int main( int argc, char* argv[] ) { std::cout << "Tegan Language Compiler v.0.1" << std::endl<< std::endl << std::endl; FILE* file = fopen( argv[ 1 ], "r" ); if ( file == 0 ) { std::cout << "Can't open file " << argv[ 1 ] << "\n"; return 1; } if ( argv[ 2 ] == NULL ) { std::cout << "Specify output file" << std::endl; return 1; } char* output = argv[ 2 ]; auto parser = Parser(); Node* root = parser.parse( file ); std::cout << std::endl; Printer printer = Printer(); Codegen codegen = Codegen(); for ( int i = 3; i < argc; ++i ) { addOpt( argv[ i ], codegen ); } if ( root != NULL ) { std::cout << std::endl; root->accept( printer ); if ( !parser.isFailed() ) { std::cout << std::endl; root->accept( codegen ); codegen.dump(); codegen.save( output ); } } else { std::cout << "EXIT" << std::endl; } std::cout << std::endl << std::endl << std::endl; }
void CgStmt::Visit(IRCatchStmt* stmt, int ignored) { // Create a new block for statements following the "catch" statement, // which will be targeted by jumps generated for return statements // in the body. stmt->mReturnTarget = llvm::BasicBlock::Create(*mContext, "return"); // Generate code for the body, followed by a jump to the return target // (note that LLVM blocks never fall through). Codegen(stmt->GetBody()); mBuilder->CreateBr(stmt->mReturnTarget); // Statements following the catch statement will be generated in the // return block. llvm::Function* function = mBuilder->GetInsertBlock()->getParent(); function->getBasicBlockList().push_back(stmt->mReturnTarget); mBuilder->SetInsertPoint(stmt->mReturnTarget); }
// comp_unit is the outermost container defined in STD.pm Value * comp_unit( struct villCompiler * vill, struct graph_node * node ) { vill -> debug_flags & DEBUG_USER && fprintf( stderr, "<comp_unit>" ); // The comp_unit is a mapping containing these keys and values: // . either a statementlist node or an anonymous list // containing a statementlist and a comment // BEG the beginning source code offset // END the end source code offset // statementlist an alias for the . node of comp_unit // comment (optional) alias for a comment node // // Find the statementlist node and call its *Codegen method, or abort. //function_pointer Codegen = (function_pointer) node -> data; //graph_node * statementlist = ast_mapentry( node, "." ); graph_node * statementlist = ast_mapentry( node, "statementlist" ); function_pointer Codegen = (function_pointer) statementlist -> data; assert( Codegen != NULL ); Value * result = Codegen( vill, statementlist ); vill -> debug_flags & DEBUG_USER && fprintf( stderr, "</comp_unit>" ); return result; }