int write_action(t_action *action, int fd, t_label *label) { int i; i = 0; write(fd , &action->identifier, 1); if (action->coding_byte != 0) write(fd, &action->coding_byte, 1); while (action->args[i]) { if (write_args(action->args[i], fd, label, action->args[i]->pos_link)) return (1); i++; } return (0); }
void IntelDisassembler::handle_stmt(IntelSyntaxDisStmt &stmt) { if(stmt.ident == symbol_write) { write(unquotify(stmt.args[0]).c_str()); } else if(stmt.ident == symbol_space) { space(); } else if(stmt.ident == symbol_maybe_write_space_args_imm32) { if(icode->imm != get_num(stmt.args[0])) { space(); write_args(); } } else if(stmt.ident == symbol_write_args) { write_args(); } else if(stmt.ident == symbol_write_arg) { if(stmt.args.size() == 1) write_arg(get_num(stmt.args[0])); else if(stmt.args.size() == 2) write_arg(get_num(stmt.args[0]), get_num(stmt.args[1])); else if(stmt.args.size() == 3) write_arg(get_num(stmt.args[0]), get_num(stmt.args[1]), get_num(stmt.args[2])); else if(stmt.args.size() == 4) write_arg(get_num(stmt.args[0]), get_num(stmt.args[1]), get_num(stmt.args[2]), get_num(stmt.args[3])); } else if(stmt.ident == symbol_write_rep) { if(icode->lockrep == 2 || icode->lockrep == 3) write("rep "); } else if(stmt.ident == symbol_write_repcc) { if(icode->lockrep == 2) write("repnz "); else if(icode->lockrep == 3) write("repz "); } else if(stmt.ident == symbol_write_size_suffix_argsize) { write_size_suffix(icode->argsize[get_num(stmt.args[0])]); } else if(stmt.ident == symbol_write_size_suffix_osz) { write_size_suffix(2 << icode->osz); } else if(stmt.ident == symbol_write_seg_reg) { if(icode->ea.sreg <= 5) { do_write_seg_reg(icode->ea.sreg); write(" "); } } else if(stmt.ident == symbol_comma) { comma(); } else if(stmt.ident == symbol_write_osz64) { write(unquotify(stmt.args[(icode->osz == 2) ? 0 : 1]).c_str()); } else if(stmt.ident == symbol_write_stack_o16_o32_o64) { if(dsz == 2) { // 64bit mode. if(icode->osz == 0) write("o16 "); } else if(dsz != icode->osz) { write_o16_o32_o64(); } } else if(stmt.ident == symbol_do_nop_xchg) { // do_nop_xchg("nop", "xchg", 1) means: argvalue(1) is 0, nop else do xchg int a = get_num(stmt.args[2]); if(icode->argvalue[a] == 0 && state->opcode0 == 0x90 && icode->argtype[0] == IntelArgTypes::Treg_gr) write(unquotify(stmt.args[0]).c_str()); else { write(unquotify(stmt.args[1]).c_str()); space(); write_args(); } } else if(stmt.ident == symbol_write_xlat_o16_o32_o64) { if(dsz == 2) { // 64bit mode. if(icode->osz != 1) write_o16_o32_o64(); } else if(dsz != icode->osz) { write_o16_o32_o64(); } } else if(stmt.ident == symbol_write_osz) { // if osz == 0, write 1st argument. // if osz == 1, write 2nd argument. // if osz == 2, write 3rd argument. write(unquotify(stmt.args[icode->osz]).c_str()); } else if(stmt.ident == symbol_write_asz) { // if asz == 0, write 1st argument. // if asz == 1, write 2nd argument. // if asz == 2, write 3rd argument. write(unquotify(stmt.args[icode->asz]).c_str()); } else if(stmt.ident == symbol_write_far_imm) { write_far_imm(); } else if(stmt.ident == symbol_write_jrcxz_o16_o32_o64) { if(dsz != 2) { if(icode->osz != dsz) write_o16_o32_o64(); } } else { write("<unimpl-stmt>"); } }
static AUO_RESULT x264_out(CONF_GUIEX *conf, const OUTPUT_INFO *oip, PRM_ENC *pe, const SYSTEM_DATA *sys_dat) { AUO_RESULT ret = AUO_RESULT_SUCCESS; PIPE_SET pipes = { 0 }; PROCESS_INFORMATION pi_enc = { 0 }; char x264cmd[MAX_CMD_LEN] = { 0 }; char x264args[MAX_CMD_LEN] = { 0 }; char x264dir[MAX_PATH_LEN] = { 0 }; char *x264fullpath = (conf->x264.use_highbit_depth) ? sys_dat->exstg->s_x264.fullpath_highbit : sys_dat->exstg->s_x264.fullpath; const BOOL afs = conf->vid.afs != 0; CONVERT_CF_DATA pixel_data = { 0 }; video_output_thread_t thread_data = { 0 }; thread_data.repeat = pe->delay_cut_additional_vframe; set_pixel_data(&pixel_data, conf, oip->w, oip->h); int *jitter = NULL; int rp_ret = 0; //x264優先度関連の初期化 DWORD set_priority = (pe->h_p_aviutl || conf->vid.priority != AVIUTLSYNC_PRIORITY_CLASS) ? priority_table[conf->vid.priority].value : NORMAL_PRIORITY_CLASS; //プロセス用情報準備 if (!PathFileExists(x264fullpath)) { ret |= AUO_RESULT_ERROR; error_no_exe_file("x264", x264fullpath); return ret; } PathGetDirectory(x264dir, _countof(x264dir), x264fullpath); //YUY2/YC48->NV12/YUV444, RGBコピー用関数 const int input_csp_idx = get_aviutl_color_format(conf->x264.use_highbit_depth, conf->x264.output_csp, conf->vid.input_as_lw48); const func_convert_frame convert_frame = get_convert_func(oip->w, input_csp_idx, (conf->x264.use_highbit_depth) ? 16 : 8, conf->x264.interlaced, conf->x264.output_csp); if (convert_frame == NULL) { ret |= AUO_RESULT_ERROR; error_select_convert_func(oip->w, oip->h, conf->x264.use_highbit_depth, conf->x264.interlaced, conf->x264.output_csp); return ret; } //映像バッファ用メモリ確保 if (!malloc_pixel_data(&pixel_data, oip->w, oip->h, conf->x264.output_csp, (conf->x264.use_highbit_depth) ? 16 : 8)) { ret |= AUO_RESULT_ERROR; error_malloc_pixel_data(); return ret; } //パイプの設定 pipes.stdIn.mode = AUO_PIPE_ENABLE; pipes.stdErr.mode = AUO_PIPE_ENABLE; pipes.stdIn.bufferSize = pixel_data.total_size * 2; //x264バージョン情報表示・チェック if (AUO_RESULT_ERROR == write_log_x264_version(x264fullpath)) { return (ret | AUO_RESULT_ERROR); } //コマンドライン生成 build_full_cmd(x264cmd, _countof(x264cmd), conf, oip, pe, sys_dat, PIPE_FN); write_log_auo_line(LOG_INFO, "x264 options..."); write_args(x264cmd); sprintf_s(x264args, _countof(x264args), "\"%s\" %s", x264fullpath, x264cmd); remove(pe->temp_filename); //ファイルサイズチェックの時に旧ファイルを参照してしまうのを回避 if (conf->vid.afs && conf->x264.interlaced) { ret |= AUO_RESULT_ERROR; error_afs_interlace_stg(); //jitter用領域確保 } else if ((jitter = (int *)calloc(oip->n + 1, sizeof(int))) == NULL) { ret |= AUO_RESULT_ERROR; error_malloc_tc(); //Aviutl(afs)からのフレーム読み込み } else if (!setup_afsvideo(oip, sys_dat, conf, pe)) { ret |= AUO_RESULT_ERROR; //Aviutl(afs)からのフレーム読み込みに失敗 //x264プロセス開始 } else if ((rp_ret = RunProcess(x264args, x264dir, &pi_enc, &pipes, (set_priority == AVIUTLSYNC_PRIORITY_CLASS) ? GetPriorityClass(pe->h_p_aviutl) : set_priority, TRUE, FALSE)) != RP_SUCCESS) { ret |= AUO_RESULT_ERROR; error_run_process("x264", rp_ret); //書き込みスレッドを開始 } else if (video_output_create_thread(&thread_data, &pixel_data, pipes.f_stdin)) { ret |= AUO_RESULT_ERROR; error_video_output_thread_start(); } else { //全て正常 int i = 0; void *frame = NULL; int *next_jitter = NULL; UINT64 amp_filesize_limit = (UINT64)(1.02 * get_amp_filesize_limit(conf, oip, pe, sys_dat)); BOOL enc_pause = FALSE, copy_frame = FALSE, drop = FALSE; const DWORD aviutl_color_fmt = COLORFORMATS[get_aviutl_color_format(conf->x264.use_highbit_depth, conf->x264.output_csp, conf->vid.input_as_lw48)].FOURCC; //Aviutlの時間を取得 PROCESS_TIME time_aviutl; GetProcessTime(pe->h_p_aviutl, &time_aviutl); //x264が待機に入るまでこちらも待機 while (WaitForInputIdle(pi_enc.hProcess, LOG_UPDATE_INTERVAL) == WAIT_TIMEOUT) log_process_events(); //ログウィンドウ側から制御を可能に DWORD tm_vid_enc_start = timeGetTime(); enable_x264_control(&set_priority, &enc_pause, afs, afs && pe->current_x264_pass == 1, tm_vid_enc_start, oip->n); //------------メインループ------------ for (i = 0, next_jitter = jitter + 1, pe->drop_count = 0; i < oip->n; i++, next_jitter++) { //中断を確認 ret |= (oip->func_is_abort()) ? AUO_RESULT_ABORT : AUO_RESULT_SUCCESS; //x264が実行中なら、メッセージを取得・ログウィンドウに表示 if (ReadLogEnc(&pipes, pe->drop_count, i) < 0) { //勝手に死んだ... ret |= AUO_RESULT_ERROR; error_x264_dead(); break; } if (!(i & 7)) { //Aviutlの進捗表示を更新 oip->func_rest_time_disp(i + oip->n * (pe->current_x264_pass - 1), oip->n * pe->total_x264_pass); //x264優先度 check_enc_priority(pe->h_p_aviutl, pi_enc.hProcess, set_priority); //音声同時処理 ret |= aud_parallel_task(oip, pe); //上限をオーバーしていないかチェック if (!(i & 63) && amp_filesize_limit //上限設定が存在する && !(1 == pe->current_x264_pass && 1 < pe->total_x264_pass)) { //multi passエンコードの1pass目でない UINT64 current_filesize = 0; if (GetFileSizeUInt64(pe->temp_filename, ¤t_filesize) && current_filesize > amp_filesize_limit) { warning_amp_filesize_over_limit(); pe->muxer_to_be_used = MUXER_DISABLED; //muxをスキップ break; } } } //一時停止 while (enc_pause & !ret) { Sleep(LOG_UPDATE_INTERVAL); ret |= (oip->func_is_abort()) ? AUO_RESULT_ABORT : AUO_RESULT_SUCCESS; log_process_events(); } //標準入力への書き込み完了をチェック while (WAIT_TIMEOUT == WaitForSingleObject(thread_data.he_out_fin, LOG_UPDATE_INTERVAL)) { ret |= (oip->func_is_abort()) ? AUO_RESULT_ABORT : AUO_RESULT_SUCCESS; log_process_events(); } //中断・エラー等をチェック if (AUO_RESULT_SUCCESS != ret) break; //コピーフレームフラグ処理 copy_frame = (!!i & (oip->func_get_flag(i) & OUTPUT_INFO_FRAME_FLAG_COPYFRAME)); //Aviutl(afs)からフレームをもらう if (NULL == (frame = ((afs) ? afs_get_video((OUTPUT_INFO *)oip, i, &drop, next_jitter) : oip->func_get_video_ex(i, aviutl_color_fmt)))) { ret |= AUO_RESULT_ERROR; error_afs_get_frame(); break; } drop |= (afs & copy_frame); if (!drop) { //コピーフレームの場合は、映像バッファの中身を更新せず、そのままパイプに流す if (!copy_frame) convert_frame(frame, &pixel_data, oip->w, oip->h); /// YUY2/YC48->NV12/YUV444変換, RGBコピー //標準入力への書き込みを開始 SetEvent(thread_data.he_out_start); } else { *(next_jitter - 1) = DROP_FRAME_FLAG; pe->drop_count++; //次のフレームの変換を許可 SetEvent(thread_data.he_out_fin); } // 「表示 -> セーブ中もプレビュー表示」がチェックされていると // func_update_preview() の呼び出しによって func_get_video_ex() の // 取得したバッファが書き換えられてしまうので、呼び出し位置を移動 (拡張AVI出力 plus より) oip->func_update_preview(); } //------------メインループここまで-------------- //書き込みスレッドを終了 video_output_close_thread(&thread_data, ret); //ログウィンドウからのx264制御を無効化 disable_x264_control(); //パイプを閉じる CloseStdIn(&pipes); if (!ret) oip->func_rest_time_disp(oip->n * pe->current_x264_pass, oip->n * pe->total_x264_pass); //音声の同時処理を終了させる ret |= finish_aud_parallel_task(oip, pe, ret); //音声との同時処理が終了 release_audio_parallel_events(pe); //タイムコード出力 if (!ret && (afs || conf->vid.auo_tcfile_out)) tcfile_out(jitter, oip->n, (double)oip->rate / (double)oip->scale, afs, pe); //エンコーダ終了待機 while (WaitForSingleObject(pi_enc.hProcess, LOG_UPDATE_INTERVAL) == WAIT_TIMEOUT) ReadLogEnc(&pipes, pe->drop_count, i); DWORD tm_vid_enc_fin = timeGetTime(); //最後にメッセージを取得 while (ReadLogEnc(&pipes, pe->drop_count, i) > 0); if (!(ret & AUO_RESULT_ERROR) && afs) write_log_auo_line_fmt(LOG_INFO, "drop %d / %d frames", pe->drop_count, i); write_log_auo_line_fmt(LOG_INFO, "CPU使用率: Aviutl: %.2f%% / x264: %.2f%%", GetProcessAvgCPUUsage(pe->h_p_aviutl, &time_aviutl), GetProcessAvgCPUUsage(pi_enc.hProcess)); write_log_auo_enc_time("x264エンコード時間", tm_vid_enc_fin - tm_vid_enc_start); } //解放処理 if (pipes.stdErr.mode) CloseHandle(pipes.stdErr.h_read); CloseHandle(pi_enc.hProcess); CloseHandle(pi_enc.hThread); free_pixel_data(&pixel_data); if (jitter) free(jitter); ret |= exit_audio_parallel_control(oip, pe, ret); return ret; }
static void gen_proxy(type_t *iface, const var_t *func, int idx, unsigned int proc_offset) { var_t *retval = type_function_get_retval(func->type); int has_ret = !is_void(retval->type); int has_full_pointer = is_full_pointer_function(func); const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV); const var_list_t *args = type_get_function_args(func->type); if (!callconv) callconv = "STDMETHODCALLTYPE"; indent = 0; if (is_interpreted_func( iface, func )) { if (get_stub_mode() == MODE_Oif && !is_callas( func->attrs )) return; write_type_decl_left(proxy, retval->type); print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func)); write_args(proxy, args, iface->name, 1, TRUE); print_proxy( ")\n"); write_client_call_routine( proxy, iface, func, "Object", proc_offset ); return; } print_proxy( "static void __finally_%s_%s_Proxy( struct __proxy_frame *__frame )\n", iface->name, get_name(func) ); print_proxy( "{\n"); indent++; if (has_full_pointer) write_full_pointer_free(proxy, indent, func); print_proxy( "NdrProxyFreeBuffer( __frame->This, &__frame->_StubMsg );\n" ); indent--; print_proxy( "}\n"); print_proxy( "\n"); write_type_decl_left(proxy, retval->type); print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func)); write_args(proxy, args, iface->name, 1, TRUE); print_proxy( ")\n"); print_proxy( "{\n"); indent ++; print_proxy( "struct __proxy_frame __f, * const __frame = &__f;\n" ); /* local variables */ if (has_ret) { print_proxy( "%s", "" ); write_type_decl(proxy, retval->type, retval->name); fprintf( proxy, ";\n" ); } print_proxy( "RPC_MESSAGE _RpcMessage;\n" ); if (has_ret) { if (decl_indirect(retval->type)) print_proxy("void *_p_%s = &%s;\n", retval->name, retval->name); } print_proxy( "\n"); print_proxy( "RpcExceptionInit( __proxy_filter, __finally_%s_%s_Proxy );\n", iface->name, get_name(func) ); print_proxy( "__frame->This = This;\n" ); if (has_full_pointer) write_full_pointer_init(proxy, indent, func, FALSE); /* FIXME: trace */ clear_output_vars( type_get_function_args(func->type) ); print_proxy( "RpcTryExcept\n" ); print_proxy( "{\n" ); indent++; print_proxy( "NdrProxyInitialize(This, &_RpcMessage, &__frame->_StubMsg, &Object_StubDesc, %d);\n", idx); write_pointer_checks( proxy, indent, func ); print_proxy( "RpcTryFinally\n" ); print_proxy( "{\n" ); indent++; write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_BUFFERSIZE); print_proxy( "NdrProxyGetBuffer(This, &__frame->_StubMsg);\n" ); write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_MARSHAL); print_proxy( "NdrProxySendReceive(This, &__frame->_StubMsg);\n" ); fprintf(proxy, "\n"); print_proxy( "__frame->_StubMsg.BufferStart = _RpcMessage.Buffer;\n" ); print_proxy( "__frame->_StubMsg.BufferEnd = __frame->_StubMsg.BufferStart + _RpcMessage.BufferLength;\n\n" ); print_proxy("if ((_RpcMessage.DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n"); indent++; print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset ); indent--; fprintf(proxy, "\n"); write_remoting_arguments(proxy, indent, func, "", PASS_OUT, PHASE_UNMARSHAL); if (has_ret) { if (decl_indirect(retval->type)) print_proxy("MIDL_memset(&%s, 0, sizeof(%s));\n", retval->name, retval->name); else if (is_ptr(retval->type) || is_array(retval->type)) print_proxy("%s = 0;\n", retval->name); write_remoting_arguments(proxy, indent, func, "", PASS_RETURN, PHASE_UNMARSHAL); } indent--; print_proxy( "}\n"); print_proxy( "RpcFinally\n" ); print_proxy( "{\n" ); indent++; print_proxy( "__finally_%s_%s_Proxy( __frame );\n", iface->name, get_name(func) ); indent--; print_proxy( "}\n"); print_proxy( "RpcEndFinally\n" ); indent--; print_proxy( "}\n" ); print_proxy( "RpcExcept(__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" ); print_proxy( "{\n" ); if (has_ret) { indent++; proxy_free_variables( type_get_function_args(func->type), "" ); print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" ); indent--; } print_proxy( "}\n" ); print_proxy( "RpcEndExcept\n" ); if (has_ret) { print_proxy( "return _RetVal;\n" ); } indent--; print_proxy( "}\n"); print_proxy( "\n"); }
void sccwriter::write_code( Expr* code, std::ostream& os, int ind, const char* retModStr, int opts ) { std::string retModString; std::string incString; if ( retModStr ) { retModString = std::string( retModStr ); retModString.append( " = " ); incString = std::string( retModStr ); incString.append( "->inc();" ); } switch( code->getclass() ) { case INT_EXPR: { indent( os, ind ); os << retModString.c_str(); os << "new IntExpr( " << mpz_get_si( ((IntExpr*)code)->n ) << " );" << std::endl; indent( os, ind ); os << incString.c_str() << std::endl; } break; case RAT_EXPR: { mpz_t num, den; mpz_init(num); mpz_init(den); mpq_get_num( num, ((RatExpr*)code)->n ); mpq_get_den( den, ((RatExpr*)code)->n ); indent( os, ind ); os << retModString.c_str(); os << "new RatExpr( " << mpz_get_si( num ) << ", " << mpz_get_si( den ) << " );" << std::endl; indent( os, ind ); os << incString.c_str() << std::endl; } break; case SYMS_EXPR: { //if it is a variable, simply write it to buffer if( is_var( ((SymSExpr*)code)->s ) ) { indent( os, ind ); os << retModString.c_str(); write_variable( ((SymSExpr*)code)->s.c_str(), os ); os << ";" << std::endl; } else //else must look at symbol lookup table { std::string var; get_var_name( ((SymSExpr*)code)->s, var ); indent( os, ind ); os << retModString.c_str() << "e_" << var.c_str() << ";" << std::endl; add_global_sym( var ); } indent( os, ind ); os << incString.c_str() << std::endl; } break; default: switch( code->getop() ) { case APP: { //collect the arguments std::vector< Expr* > argVector; code->collect_args( argVector ); //write the arguments std::vector< std::string > args; for( int a=0; a<(int)argVector.size(); a++ ) { std::string expr; write_expr( argVector[a], os, ind, expr ); args.push_back( expr ); } //write_args( (CExpr*)code, os, ind, 1, args ); Expr* hd = code->get_head(); //map to a program in the case that it is a program if( hd->getop()==PROG && get_prog_index_by_expr( hd )!=-1 ) { indent( os, ind ); os << retModString << "f_" << progNames[ get_prog_index_by_expr( hd ) ].c_str() << "( "; for( int a=0; a<(int)args.size(); a++ ) { os << args[a].c_str(); if( a!=(int)( args.size()-1 ) ){ os << ", "; } } os << " );" << std::endl; for( int a=0; a<(int)args.size(); a++ ) { write_dec( args[a], os, ind ); } } else { #ifdef USE_FLAT_APP std::string expr; write_expr( hd, os, ind, expr ); indent( os, ind ); os << retModString << "new CExpr( APP, "; os << expr.c_str() << ", "; for( int a=0; a<(int)args.size(); a++ ) { os << args[a].c_str(); if( a!=(int)( args.size()-1 ) ){ os << ", "; } } os << " );" << std::endl; #else std::string expr; write_expr( hd, os, ind, expr ); indent( os, ind ); os << retModString; for( int a=0; a<(int)args.size(); a++ ) { os << "new CExpr( APP, "; } os << expr.c_str() << ", "; for( int a=0; a<(int)args.size(); a++ ) { os << args[a].c_str(); os << " )"; if( a!=(int)( args.size()-1 ) ){ os << ", "; } } os << ";" << std::endl; #endif //indent( os, ind ); //os << expr.c_str() << "->dec();" << std::endl; } } break; case MATCH: { //calculate the value for the expression std::string expr; write_expr( ((CExpr*)code)->kids[0], os, ind, expr ); //get the head std::ostringstream sshd; sshd << "e" << exprCount; exprCount++; indent( os, ind ); os << "Expr* " << sshd.str().c_str() << " = " << expr.c_str() << "->followDefs()->get_head();" << std::endl; //write the arguments std::vector< std::string > args; write_args( (CExpr*)code, os, ind, 1, args ); bool encounterDefault = false; //now make an if statement corresponding to the match int a = 0; while( ((CExpr*)code)->kids[a+1] ) { indent( os, ind ); if( a!=0 ){ os << "}else"; } if( ((CExpr*)code)->kids[a+1]->getop()!=CASE ){ encounterDefault = true; os << "{" << std::endl; //write the body of the case write_code( ((CExpr*)code)->kids[a+1], os, ind+1, retModStr ); indent( os, ind ); os << "}" << std::endl; }else{ if( a!=0 ) os << " "; os << "if( " << sshd.str().c_str() << "==" << args[a].c_str() << " ){" << std::endl; //collect args from the variable in the code std::ostringstream ssargs; ssargs << "args" << argsCount; argsCount++; #ifndef USE_FLAT_APP indent( os, ind+1 ); os << "std::vector< Expr* > " << ssargs.str().c_str() << ";" << std::endl; indent( os, ind+1 ); os << expr.c_str() << "->followDefs()->collect_args( " << ssargs.str().c_str() << " );" << std::endl; #endif //set the variables defined in the pattern equal to the arguments std::vector< Expr* > caseArgs; ((CExpr*)((CExpr*)code)->kids[a+1])->kids[0]->collect_args( caseArgs ); for( int b=0; b<(int)caseArgs.size(); b++ ) { indent( os, ind+1 ); os << "Expr* "; write_variable( ((SymSExpr*)caseArgs[b])->s.c_str(), os ); #ifdef USE_FLAT_APP os << " = ((CExpr*)" << expr.c_str() << "->followDefs())->kids[" << b+1 << "];" << std::endl; #else os << " = " << ssargs.str().c_str() << "[" << b << "];" << std::endl; #endif vars.push_back( ((SymSExpr*)caseArgs[b])->s ); } //write the body of the case write_code( ((CExpr*)code)->kids[a+1], os, ind+1, retModStr, opt_write_case_body ); } a++; } if( !encounterDefault ) { indent( os, ind ); os << "}else{" << std::endl; indent( os, ind + 1 ); os << "std::cout << \"Could not find match for expression in function f_"; os << progNames[currProgram].c_str() << " \";" << std::endl; indent( os, ind + 1 ); os << sshd.str().c_str() << "->print( std::cout );" << std::endl; indent( os, ind + 1 ); os << "std::cout << std::endl;" << std::endl; indent( os, ind + 1 ); os << "exit( 1 );" << std::endl; indent( os, ind ); os << "}" << std::endl; } write_dec( expr, os, ind ); for( int a=0; a<(int)args.size(); a++ ) { write_dec( args[a], os, ind ); } } break; case CASE: if( opts&opt_write_case_body ) { write_code( ((CExpr*)code)->kids[1], os, ind, retModStr ); } else { write_code( ((CExpr*)code)->kids[0]->get_head(), os, ind, retModStr ); } break; case DO: { //write each of the children in sequence int counter = 0; while( ((CExpr*)code)->kids[counter] ) { if( ((CExpr*)code)->kids[counter+1]==NULL ) { write_code( ((CExpr*)code)->kids[counter], os, ind, retModStr ); } else { std::string expr; write_expr( ((CExpr*)code)->kids[counter], os, ind, expr ); //clean up memory write_dec( expr, os, ind ); } counter++; } } break; case LET: { indent( os, ind ); os << "Expr* "; write_variable( ((SymSExpr*)((CExpr*)code)->kids[0])->s, os ); os << ";" << std::endl; std::ostringstream ss; write_variable( ((SymSExpr*)((CExpr*)code)->kids[0])->s, ss ); write_code( ((CExpr*)code)->kids[1], os, ind, ss.str().c_str() ); //add it to the variables vars.push_back( ((SymSExpr*)((CExpr*)code)->kids[0])->s ); write_code( ((CExpr*)code)->kids[2], os, ind, retModStr ); //clean up memory indent( os, ind ); write_variable( ((SymSExpr*)((CExpr*)code)->kids[0])->s, os ); os << "->dec();" << std::endl; } break; case FAIL: { indent( os, ind ); os << retModString.c_str() << "NULL;" << std::endl; } break; #ifndef MARKVAR_32 case MARKVAR: { //calculate the value for the expression std::string expr; write_expr( ((CExpr*)code)->kids[0], os, ind, expr, opt_write_check_sym_expr ); //set the mark on the expression indent( os, ind ); os << "if (" << expr.c_str() << "->followDefs()->getmark())" << std::endl; indent( os, ind+1 ); os << expr.c_str() << "->followDefs()->clearmark();" << std::endl; indent( os, ind ); os << "else" << std::endl; indent( os, ind+1 ); os << expr.c_str() << "->followDefs()->setmark();" << std::endl; //write the return if necessary if( retModStr!=NULL ){ indent( os, ind ); os << retModString.c_str() << expr.c_str() << ";" << std::endl; indent( os, ind ); os << incString.c_str() << std::endl; } write_dec( expr, os, ind ); } break; case IFMARKED: { //calculate the value for the expression std::string expr; write_expr( ((CExpr*)code)->kids[0], os, ind, expr, opt_write_check_sym_expr ); //if mark is set, write code for kids[1] indent( os, ind ); os << "if (" << expr.c_str() << "->followDefs()->getmark()){" << std::endl; write_code( ((CExpr*)code)->kids[1], os, ind+1, retModStr ); //else write code for kids[2] indent( os, ind ); os << "}else{" << std::endl; write_code( ((CExpr*)code)->kids[2], os, ind+1, retModStr ); indent( os, ind ); os << "}" << std::endl; //clean up memory write_dec( expr, os, ind ); } break; #else case MARKVAR: { //calculate the value for the expression std::string expr; write_expr( ((CExpr*)code)->kids[1], os, ind, expr, opt_write_check_sym_expr ); //set the mark on the expression indent( os, ind ); os << "if ( ((SymExpr*)" << expr.c_str() << "->followDefs())->getmark("; os << ((IntExpr*)((CExpr*)code)->kids[0])->get_num() << "))" << std::endl; indent( os, ind+1 ); os << "((SymExpr*)" << expr.c_str() << "->followDefs())->clearmark("; os << ((IntExpr*)((CExpr*)code)->kids[0])->get_num() << ");" << std::endl; indent( os, ind ); os << "else" << std::endl; indent( os, ind+1 ); os << "((SymExpr*)" << expr.c_str() << "->followDefs())->setmark("; os << ((IntExpr*)((CExpr*)code)->kids[0])->get_num() << ");" << std::endl; //write the return if necessary if( retModStr!=NULL ){ indent( os, ind ); os << retModString.c_str() << expr.c_str() << ";" << std::endl; indent( os, ind ); os << incString.c_str() << std::endl; } write_dec( expr, os, ind ); } break; case COMPARE: { std::string expr1, expr2; write_expr( ((CExpr*)code)->kids[0], os, ind, expr1, opt_write_check_sym_expr ); write_expr( ((CExpr*)code)->kids[1], os, ind, expr2, opt_write_check_sym_expr ); indent( os, ind ); os << "if( ((SymExpr*)" << expr1.c_str() << ")->followDefs() < ((SymExpr*)" << expr2.c_str() << ")->followDefs() ){" << std::endl; write_code( ((CExpr*)code)->kids[2], os, ind+1, retModStr ); indent( os, ind ); os << "}else{" << std::endl; write_code( ((CExpr*)code)->kids[3], os, ind+1, retModStr ); indent( os, ind ); os << "}" << std::endl; //clean up memory write_dec( expr1, os, ind ); write_dec( expr2, os, ind ); } break; case IFMARKED: { //calculate the value for the expression std::string expr; write_expr( ((CExpr*)code)->kids[1], os, ind, expr, opt_write_check_sym_expr ); //if mark is set, write code for kids[1] indent( os, ind ); os << "if ( ((SymExpr*)" << expr.c_str() << "->followDefs())->getmark("; os << ((IntExpr*)((CExpr*)code)->kids[0])->get_num() << ")){" << std::endl; write_code( ((CExpr*)code)->kids[2], os, ind+1, retModStr ); //else write code for kids[2] indent( os, ind ); os << "}else{" << std::endl; write_code( ((CExpr*)code)->kids[3], os, ind+1, retModStr ); indent( os, ind ); os << "}" << std::endl; //clean up memory write_dec( expr, os, ind ); } break; #endif case ADD: case MUL: case DIV: { //calculate the value for the first expression std::string expr1; write_expr( ((CExpr*)code)->kids[0], os, ind, expr1 ); //calculate the value for the second expression std::string expr2; write_expr( ((CExpr*)code)->kids[1], os, ind, expr2 ); std::ostringstream ss; ss << "rnum" << rnumCount; rnumCount++; indent( os, ind ); os << "if( " << expr1.c_str() << "->followDefs()->getclass()==INT_EXPR ){" << std::endl; indent( os, ind+1 ); os << "mpz_t " << ss.str().c_str() << ";" << std::endl; indent( os, ind+1 ); os << "mpz_init(" << ss.str().c_str() << ");" << std::endl; indent( os, ind+1 ); os << "mpz_"; if( code->getop()==ADD ) os << "add"; else os << "mul"; os << "( " << ss.str().c_str() << ", ((IntExpr*)" << expr1.c_str() << "->followDefs())->n, ((IntExpr*)" << expr2.c_str() << "->followDefs())->n);" << std::endl; indent( os, ind+1 ); os << retModString.c_str() << "new IntExpr(" << ss.str().c_str() << ");" << std::endl; indent( os, ind ); os << "}else if( " << expr1.c_str() << "->followDefs()->getclass()==RAT_EXPR ){" << std::endl; indent( os, ind+1 ); os << "mpq_t " << ss.str().c_str() << ";" << std::endl; indent( os, ind+1 ); os << "mpq_init(" << ss.str().c_str() << ");" << std::endl; indent( os, ind+1 ); os << "mpq_"; if( code->getop()==ADD ) os << "add"; else if( code->getop()==MUL ) os << "mul"; else os << "div"; os << "( " << ss.str().c_str() << ", ((RatExpr*)" << expr1.c_str() << "->followDefs())->n, ((RatExpr*)" << expr2.c_str() << "->followDefs())->n);" << std::endl; indent( os, ind+1 ); os << retModString.c_str() << "new RatExpr(" << ss.str().c_str() << ");" << std::endl; indent( os, ind ); os << "}" << std::endl; //clean up memory write_dec( expr1, os, ind ); write_dec( expr2, os, ind ); } break; case NEG: { //calculate the value for the first expression std::string expr1; write_expr( ((CExpr*)code)->kids[0], os, ind, expr1 ); std::ostringstream ss; ss << "rnum" << rnumCount; rnumCount++; indent( os, ind ); os << "if( " << expr1.c_str() << "->followDefs()->getclass()==INT_EXPR ){" << std::endl; indent( os, ind+1 ); os << "mpz_t " << ss.str().c_str() << ";" << std::endl; indent( os, ind+1 ); os << "mpz_init(" << ss.str().c_str() << ");" << std::endl; indent( os, ind+1 ); os << "mpz_neg( " << ss.str().c_str() << ", ((IntExpr*)" << expr1.c_str() << "->followDefs())->n );" << std::endl; indent( os, ind+1 ); os << retModString.c_str() << "new IntExpr(" << ss.str().c_str() << ");" << std::endl; indent( os, ind ); os << "}else if( " << expr1.c_str() << "->followDefs()->getclass()==RAT_EXPR ){" << std::endl; indent( os, ind+1 ); os << "mpq_t " << ss.str().c_str() << ";" << std::endl; indent( os, ind+1 ); os << "mpq_init(" << ss.str().c_str() << ");" << std::endl; indent( os, ind+1 ); os << "mpq_neg( " << ss.str().c_str() << ", ((RatExpr*)" << expr1.c_str() << "->followDefs())->n );" << std::endl; indent( os, ind+1 ); os << retModString.c_str() << "new RatExpr(" << ss.str().c_str() << ");" << std::endl; indent( os, ind ); os << "}" << std::endl; //clean up memory write_dec( expr1, os, ind ); } break; case IFNEG: case IFZERO: { std::string expr1; write_expr( ((CExpr*)code)->kids[0], os, ind, expr1 ); indent( os, ind ); os << "if( " << expr1.c_str() << "->followDefs()->getclass()==INT_EXPR ){" << std::endl; indent( os, ind+1 ); os << "if( mpz_sgn( ((IntExpr *)" << expr1.c_str() << "->followDefs())->n ) "; if( code->getop()==IFNEG ) os << "<"; else os << "=="; os << " 0 ){" << std::endl; write_code( ((CExpr*)code)->kids[1], os, ind+2, retModStr ); indent( os, ind+1 ); os << "}else{" << std::endl; write_code( ((CExpr*)code)->kids[2], os, ind+2, retModStr ); indent( os, ind+1 ); os << "}" << std::endl; indent( os, ind ); os << "}else if( " << expr1.c_str() << "->followDefs()->getclass()==RAT_EXPR ){" << std::endl; indent( os, ind+1 ); os << "if( mpq_sgn( ((RatExpr *)" << expr1.c_str() << "->followDefs())->n ) "; if( code->getop()==IFNEG ) os << "<"; else os << "=="; os << " 0 ){" << std::endl; write_code( ((CExpr*)code)->kids[1], os, ind+2, retModStr ); indent( os, ind+1 ); os << "}else{" << std::endl; write_code( ((CExpr*)code)->kids[2], os, ind+2, retModStr ); indent( os, ind+1 ); os << "}" << std::endl; indent( os, ind ); os << "}" << std::endl; //clean up memory write_dec( expr1, os, ind ); } break; case RUN:/*?*/break; case PI:/*?*/break; case LAM:/*?*/break; case TYPE:/*?*/break; case KIND:/*?*/break; case ASCRIBE:/*?*/break; case MPZ:/*?*/break; case PROG:/*?*/break; case PROGVARS:/*?*/break; case PAT:/*?*/break; } break; } }
/* * Do variable interpolation on a string, then invoke it as a shell command. * Write an appropriate set of AV pairs to standard input of the command and * read its standard output into outarray. Return the commands final status * when it terminates */ int call_post_process(char *string, struct author_data *data, char ***outargsp, int *outargs_cntp) { char **new_args; int status; int readfd, writefd, errorfd; int i; char *cmd = substitute(string, data); #if HAVE_PID_T pid_t pid; #else int pid; #endif pid = my_popen(cmd, &readfd, &writefd, &errorfd); free(cmd); if (pid < 0) { close_fds(readfd, writefd, errorfd); return(1); /* deny */ } /* If the status is AUTHOR_STATUS_PASS_ADD then the current output args * represent *additions* to the input args, not the full set */ if (data->status == AUTHOR_STATUS_PASS_ADD) { for (i = 0; i < data->num_in_args; i++) { if (debug & DEBUG_AUTHOR_FLAG) report(LOG_DEBUG, "input %s", data->input_args[i]); } if (write_args(writefd, data->input_args, data->num_in_args)) { close_fds(readfd, writefd, errorfd); return(1); /* deny */ } } for (i = 0; i < data->num_out_args; i++) { if (debug & DEBUG_AUTHOR_FLAG) report(LOG_DEBUG, "input %s", data->output_args[i]); } if (write_args(writefd, data->output_args, data->num_out_args)) { close_fds(readfd, writefd, errorfd); return(1); /* deny */ } close(writefd); writefd = -1; new_args = read_args(0, readfd); *outargsp = new_args; if (debug & DEBUG_AUTHOR_FLAG) { for (i = 0; new_args[i]; i++) { report(LOG_DEBUG, "output %s", new_args[i]); } } /* count the output args */ for (i = 0; new_args[i]; i++) /* NULL stmt */ ; *outargs_cntp = i; status = waitfor(pid); close_fds(readfd, writefd, errorfd); return(status); }
/* * Do variable interpolation on a string, then invoke it as a shell command. * Write an appropriate set of AV pairs to standard input of the command and * read its standard output into outarray. Return the commands final status * when it terminates */ int call_pre_process(char *string, struct author_data *data, char ***outargsp, int *outargs_cntp, char *error, int err_len) { char **new_args; int readfd, writefd, errorfd; int status, i; char *cmd = substitute(string, data); #if HAVE_PID_T pid_t pid; #else int pid; #endif pid = my_popen(cmd, &readfd, &writefd, &errorfd); memset(error, '\0', err_len); free(cmd); if (pid < 0) { close_fds(readfd, writefd, errorfd); return(1); /* deny */ } for (i = 0; i < data->num_in_args; i++) { if (debug & DEBUG_AUTHOR_FLAG) report(LOG_DEBUG, "input %s", data->input_args[i]); } if (write_args(writefd, data->input_args, data->num_in_args)) { close_fds(readfd, writefd, errorfd); return(1); /* deny */ } close(writefd); writefd = -1; new_args = read_args(0, readfd); *outargsp = new_args; if (debug & DEBUG_AUTHOR_FLAG) { for (i = 0; new_args[i]; i++) { report(LOG_DEBUG, "output %s", new_args[i]); } } read_string(errorfd, error, err_len); if (error[0] != '\0') { report(LOG_ERR, "Error from program (%d): \"%s\" ", strlen(error), error); } /* count the args */ for (i = 0; new_args[i]; i++) /* NULL stmt */ ; *outargs_cntp = i; status = waitfor(pid); close_fds(readfd, writefd, errorfd); return(status); }