void mono_debug_close_method (MonoCompile *cfg) { MiniDebugMethodInfo *info; MonoDebugMethodJitInfo *jit; MonoMethodHeader *header; MonoMethodSignature *sig; MonoDebugMethodAddress *debug_info; MonoMethod *method; int i; info = (MiniDebugMethodInfo *) cfg->debug_info; if (!info || !info->jit) { if (info) g_free (info); return; } method = cfg->method; header = mono_method_get_header (method); sig = mono_method_signature (method); jit = info->jit; jit->code_start = cfg->native_code; jit->epilogue_begin = cfg->epilog_begin; jit->code_size = cfg->code_len; if (jit->epilogue_begin) record_line_number (info, jit->epilogue_begin, header->code_size); jit->num_params = sig->param_count; jit->params = g_new0 (MonoDebugVarInfo, jit->num_params); for (i = 0; i < jit->num_locals; i++) write_variable (cfg->locals [i], &jit->locals [i]); if (sig->hasthis) { jit->this_var = g_new0 (MonoDebugVarInfo, 1); write_variable (cfg->args [0], jit->this_var); } for (i = 0; i < jit->num_params; i++) write_variable (cfg->args [i + sig->hasthis], &jit->params [i]); jit->num_line_numbers = info->line_numbers->len; jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers); for (i = 0; i < jit->num_line_numbers; i++) jit->line_numbers [i] = g_array_index (info->line_numbers, MonoDebugLineNumberEntry, i); debug_info = mono_debug_add_method (cfg->method_to_register, jit, cfg->domain); mono_debug_add_vg_method (method, jit); mono_debugger_check_breakpoints (method, debug_info); mono_debug_free_method_jit_info (jit); g_array_free (info->line_numbers, TRUE); g_free (info); }
/**=========== Example of Use =============**/ int main(int argc, char * argv[]) { if (argc < 2) { std::cout << "Use: '-read' or '-write'" << std::endl; return -1; } string file_path = "example.txt"; ///Write if (strcmp(argv[1],"-write") == 0) { if (write_comment(file_path, 1, "//Example File Config") == -1)//if file does not exist return -1 { cout << "File Does Not Exist!" << std::endl; return -1; } write_comment(file_path, 2, ""); write_comment(file_path, 3, "//Video Config"); write_variable(file_path, "width", "1024"); write_variable(file_path, "height", "720"); write_variable(file_path, "mode", "fullscreen"); return 0; } ///Read if (strcmp(argv[1],"-read") == 0) { int width; int height; string mode; width = atoi(read_variable(file_path, "width").c_str()); height = atoi(read_variable(file_path, "height").c_str()); mode = read_variable(file_path, "mode");//if file does not exist return "null" std::cout << "Width = " << width << std::endl; std::cout << "Height = " << height << std::endl; std::cout << "Mode = " << mode << std::endl; return 0; } //invalid else { std::cout << "Use: '-read' or '-write'" << std::endl; return -1; } }
// write a table at the given index in the stack. the index must be absolute // (i.e. positive). // @@@ circular table references will cause stack overflow! static void write_table( Transport *tpt, lua_State *L, int table_index ) { lua_pushnil( L ); // push first key while ( lua_next( L, table_index ) ) { // next key and value were pushed on the stack write_variable( tpt, L, lua_gettop( L ) - 1 ); write_variable( tpt, L, lua_gettop( L ) ); // remove value, keep key for next iteration lua_pop( L, 1 ); } }
// __newindex even on helper, int helper_newindex( lua_State *L ) { struct exception e; int freturn = 0; int ret_code; Helper *h; Transport *tpt; h = ( Helper * )luaL_checkudata(L, -3, "rpc.helper"); luaL_argcheck(L, h, -3, "helper expected"); luaL_checktype(L, -2, LUA_TSTRING ); tpt = h->handle; Try { // index destination on remote side helper_wait_ready( tpt, RPC_CMD_NEWINDEX ); helper_remote_index( h ); write_variable( tpt, L, lua_gettop( L ) - 1 ); write_variable( tpt, L, lua_gettop( L ) ); ret_code = transport_read_uint8_t( tpt ); if( ret_code != 0 ) { uint32_t len; char *err_string; // read error and handle it transport_read_uint32_t( tpt ); // Read code (not using here) len = transport_read_uint32_t( tpt ); err_string = ( char * )alloca( len + 1 ); transport_read_string( tpt, err_string, len ); err_string[ len ] = 0; deal_with_error( L, err_string ); } freturn = 0; } Catch( e ) { freturn = generic_catch_handler( L, h->handle, e ); } return freturn; }
// Dump bytecode representation of function onto stack and send. This // implementation uses eLua's crosscompile dump to match match the // bytecode representation to the client/server negotiated format. static void write_function( Transport *tpt, lua_State *L, int var_index ) { TValue *o; luaL_Buffer b; DumpTargetInfo target; target.little_endian=tpt->net_little; target.sizeof_int=sizeof(int); target.sizeof_strsize_t=sizeof(strsize_t); target.sizeof_lua_Number=tpt->lnum_bytes; target.lua_Number_integral=tpt->net_intnum; target.is_arm_fpa=0; // push function onto stack, serialize to string lua_pushvalue( L, var_index ); luaL_buffinit( L, &b ); lua_lock(L); o = L->top - 1; luaU_dump_crosscompile(L,clvalue(o)->l.p,writer,&b,0,target); lua_unlock(L); // put string representation on stack and send it luaL_pushresult( &b ); write_variable( tpt, L, lua_gettop( L ) ); // Remove function & dumped string from stack lua_pop( L, 2 ); }
static void read_cmd_get( Transport *tpt, lua_State *L ) { uint32_t len; char *funcname; char *token = NULL; // read function name len = transport_read_uint32_t( tpt ); // function name string length funcname = ( char * )alloca( len + 1 ); transport_read_string( tpt, funcname, len ); funcname[ len ] = 0; // get function // @@@ perhaps handle more like variables instead of using a long string? // @@@ also strtok is not thread safe token = strtok( funcname, "." ); lua_getglobal( L, token ); token = strtok( NULL, "." ); while( token != NULL ) { lua_getfield( L, -1, token ); lua_remove( L, -2 ); token = strtok( NULL, "." ); } // return top value on stack write_variable( tpt, L, lua_gettop( L ) ); // empty the stack lua_settop ( L, 0 ); }
void sccwriter::write_function_header( std::ostream& os, int index, int opts ) { //write the function header std::string fname; get_function_name( progNames[index], fname ); os << "Expr* " << fname.c_str() << "( "; CExpr* progvars = (CExpr*)get_prog( index )->kids[1]; int counter = 0; //write each argument while( progvars->kids[counter] ) { if( counter!=0 ) { os << ", "; } os << "Expr* "; write_variable( ((SymSExpr*)progvars->kids[counter])->s, os ); //add to vars if options are set to do so if( opts&opt_write_add_args ) { vars.push_back( ((SymSExpr*)progvars->kids[counter])->s ); } counter++; } os << " )"; if( opts&opt_write_call_debug ) { os << "{" << std::endl; indent( os, 1 ); os << "std::cout << \"Call function " << fname.c_str() << " with arguments \";" << std::endl; counter = 0; while( progvars->kids[counter] ) { if( counter!=0 ) { indent( os, 1 ); os << "std::cout << \", \";" << std::endl; } indent( os, 1 ); write_variable( ((SymSExpr*)progvars->kids[counter])->s, os ); os << "->print( std::cout );" << std::endl; counter++; } indent( os, 1 ); os << "std::cout << std::endl;" << std::endl; } }
static void write_function( Transport *tpt, lua_State *L, int var_index ) { luaL_Buffer b; // push function onto stack, serialize to string lua_pushvalue( L, var_index ); luaL_buffinit( L, &b ); lua_dump(L, writer, &b); // put string representation on stack and send it luaL_pushresult( &b ); write_variable( tpt, L, lua_gettop( L ) ); // Remove function & dumped string from stack lua_pop( L, 2 ); }
void write_fluffy_decls(FILE *output, const translation_unit_t *unit) { out = output; global_scope = &unit->scope; print_to_file(out); fprintf(out, "/* WARNING: Automatically generated file */\n"); /* write structs,unions + enums */ entity_t *entity = unit->scope.entities; for( ; entity != NULL; entity = entity->base.next) { if (entity->kind != ENTITY_TYPEDEF) continue; type_t *type = entity->typedefe.type; if(type->kind == TYPE_COMPOUND_STRUCT || type->kind == TYPE_COMPOUND_UNION) { write_compound(entity->base.symbol, &type->compound); } else if(type->kind == TYPE_ENUM) { write_enum(entity->base.symbol, &type->enumt); } } /* write global variables */ entity = unit->scope.entities; for( ; entity != NULL; entity = entity->base.next) { if (entity->kind != ENTITY_VARIABLE) continue; write_variable(entity); } /* write functions */ entity = unit->scope.entities; for( ; entity != NULL; entity = entity->base.next) { if (entity->kind != ENTITY_FUNCTION) continue; write_function(entity); } }
int helper_call (lua_State *L) { struct exception e; int freturn = 0; Helper *h; Transport *tpt; h = ( Helper * )luaL_checkudata(L, 1, "rpc.helper"); luaL_argcheck(L, h, 1, "helper expected"); tpt = h->handle; // capture special calls, otherwise execute normal remote call if( strcmp("get", h->funcname ) == 0 ) { helper_get( L, h->parent ); freturn = 1; } else { Try { int i,n; uint32_t nret,ret_code; // write function name tpt->timeout = tpt->com_timeout; helper_wait_ready( tpt, RPC_CMD_CALL ); helper_remote_index( h ); // write number of arguments n = lua_gettop( L ); transport_write_uint32_t( tpt, n - 1 ); // write each argument for( i = 2; i <= n; i ++ ) write_variable( tpt, L, i ); transport_flush(tpt); tpt->timeout = tpt->wait_timeout; /* if we're in async mode, we're done */ /*if ( h->handle->async ) { h->handle->read_reply_count++; freturn = 0; }*/ // read return code ret_code = transport_read_uint8_t( tpt ); tpt->timeout = tpt->com_timeout; if ( ret_code == 0 ) { // read return arguments nret = transport_read_uint32_t( tpt ); for ( i = 0; i < ( ( int ) nret ); i ++ ) read_variable( tpt, L ); freturn = ( int )nret; } else { uint32_t len; char* err_string; // read error and handle it transport_read_uint32_t( tpt ); // read code (not being used here) len = transport_read_uint32_t( tpt ); err_string = ( char * )alloca( len + 1 ); transport_read_string( tpt, err_string, len ); err_string[ len ] = 0; deal_with_error( L, err_string ); freturn = 0; } } Catch( e ) { freturn = generic_catch_handler( L, h->handle, e ); } } return freturn; }
void mono_debug_close_method (MonoCompile *cfg) { MiniDebugMethodInfo *info; MonoDebugMethodJitInfo *jit; MonoMethodHeader *header; MonoMethodSignature *sig; MonoMethod *method; int i; info = (MiniDebugMethodInfo *) cfg->debug_info; if (!info || !info->jit) { if (info) g_free (info); return; } method = cfg->method; header = cfg->header; sig = mono_method_signature (method); jit = info->jit; jit->code_start = cfg->native_code; jit->epilogue_begin = cfg->epilog_begin; jit->code_size = cfg->code_len; jit->has_var_info = mini_debug_options.mdb_optimizations || MONO_CFG_PROFILE_CALL_CONTEXT (cfg); if (jit->epilogue_begin) record_line_number (info, jit->epilogue_begin, header->code_size); if (jit->has_var_info) { jit->num_params = sig->param_count; jit->params = g_new0 (MonoDebugVarInfo, jit->num_params); for (i = 0; i < jit->num_locals; i++) write_variable (cfg->locals [i], &jit->locals [i]); if (sig->hasthis) { jit->this_var = g_new0 (MonoDebugVarInfo, 1); write_variable (cfg->args [0], jit->this_var); } for (i = 0; i < jit->num_params; i++) write_variable (cfg->args [i + sig->hasthis], &jit->params [i]); if (cfg->gsharedvt_info_var) { jit->gsharedvt_info_var = g_new0 (MonoDebugVarInfo, 1); jit->gsharedvt_locals_var = g_new0 (MonoDebugVarInfo, 1); write_variable (cfg->gsharedvt_info_var, jit->gsharedvt_info_var); write_variable (cfg->gsharedvt_locals_var, jit->gsharedvt_locals_var); } } jit->num_line_numbers = info->line_numbers->len; jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers); for (i = 0; i < jit->num_line_numbers; i++) jit->line_numbers [i] = g_array_index (info->line_numbers, MonoDebugLineNumberEntry, i); mono_debug_add_method (cfg->method_to_register, jit, cfg->domain); mono_debug_add_vg_method (method, jit); mono_debug_free_method_jit_info (jit); mono_debug_free_method (cfg); }
MonoDebugMethodAddress * mono_debug_add_method (MonoMethod *method, MonoDebugMethodJitInfo *jit, MonoDomain *domain) { MonoMethod *declaring; MonoDebugDataTable *table; MonoDebugMethodHeader *header; MonoDebugMethodAddress *address; MonoDebugMethodInfo *minfo; MonoDebugHandle *handle; guint8 buffer [BUFSIZ]; guint8 *ptr, *oldptr; guint32 i, size, total_size, max_size; gboolean is_wrapper = FALSE; mono_debugger_lock (); table = lookup_data_table (domain); handle = _mono_debug_get_image (method->klass->image); minfo = _mono_debug_lookup_method (method); if (!minfo || (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) || (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) || (method->flags & METHOD_ATTRIBUTE_ABSTRACT) || (method->wrapper_type != MONO_WRAPPER_NONE)) { is_wrapper = TRUE; } max_size = (5 * 5) + 1 + (10 * jit->num_line_numbers) + (25 + sizeof (gpointer)) * (1 + jit->num_params + jit->num_locals); if (max_size > BUFSIZ) ptr = oldptr = g_malloc (max_size); else ptr = oldptr = buffer; write_leb128 (jit->prologue_end, ptr, &ptr); write_leb128 (jit->epilogue_begin, ptr, &ptr); write_leb128 (jit->num_line_numbers, ptr, &ptr); for (i = 0; i < jit->num_line_numbers; i++) { MonoDebugLineNumberEntry *lne = &jit->line_numbers [i]; write_sleb128 (lne->il_offset, ptr, &ptr); write_sleb128 (lne->native_offset, ptr, &ptr); } *ptr++ = jit->this_var ? 1 : 0; if (jit->this_var) write_variable (jit->this_var, ptr, &ptr); write_leb128 (jit->num_params, ptr, &ptr); for (i = 0; i < jit->num_params; i++) write_variable (&jit->params [i], ptr, &ptr); write_leb128 (jit->num_locals, ptr, &ptr); for (i = 0; i < jit->num_locals; i++) write_variable (&jit->locals [i], ptr, &ptr); size = ptr - oldptr; g_assert (size < max_size); total_size = size + sizeof (MonoDebugMethodAddress); address = (MonoDebugMethodAddress *) allocate_data_item ( table, MONO_DEBUG_DATA_ITEM_METHOD, total_size); address->header.size = total_size; address->header.symfile_id = handle ? handle->index : 0; address->header.domain_id = mono_domain_get_id (domain); address->header.method_id = is_wrapper ? 0 : minfo->index; address->header.method = method; address->code_start = jit->code_start; address->code_size = jit->code_size; memcpy (&address->data, oldptr, size); if (max_size > BUFSIZ) g_free (oldptr); declaring = method->is_inflated ? ((MonoMethodInflated *) method)->declaring : method; header = g_hash_table_lookup (table->method_hash, declaring); if (!header) { header = &address->header; g_hash_table_insert (table->method_hash, declaring, header); if (is_wrapper) { MonoDebugWrapperData *wrapper; header->wrapper_data = wrapper = g_new0 (MonoDebugWrapperData, 1); wrapper->wrapper_type = method->wrapper_type; wrapper->method_name = mono_method_full_name (declaring, TRUE); wrapper->obsolete_cil_code = ""; } } else { address->header.wrapper_data = header->wrapper_data; header->address_list = g_slist_prepend (header->address_list, address); } g_hash_table_insert (table->method_address_hash, method, address); write_data_item (table, (guint8 *) address); mono_debugger_unlock (); return address; }
void test_polynomial(const Traits &tr) { typedef typename Traits::Construct_function CF; typedef typename Traits::Function Polynomial; typedef typename Polynomial::NT NT; CF cf= tr.construct_function_object(); NT v[] = {-1, 2, 27, -17, 0, 0}; // NT v[] = {0, 0, 0, 0, 0, 0}; Polynomial p(v, v+6); Polynomial q(v, v+3); NT a(2); write_variable( "p", p); write_variable("q", q ); write("-p", (-p)); check_equal<Traits>(-p , cf(1,-2,-27,17)); write("p-p",(p-p)); check_equal<Traits>(p-p , cf(0)); write("p+q" , (p+q) ); check_equal<Traits>(p+q , cf( -2, 4, 54, -17)); write("p-q" , (p-q)); check_equal<Traits>(p-q , cf(0,0,0,-17)); write("q*(p-q)" , q*(p-q) ); check_equal<Traits>(q*(p-q) , cf(0,0,0,17,-34,-459)); write_variable( "a", a); Polynomial diff(p-q); Polynomial dp(diff +Polynomial(a)); write("(p-q)+a" , dp ); check_equal<Traits>((p-q)+Polynomial(a) , cf(2, 0, 0, -17)); write("(p-q)-a" , ((p-q)-a) ); check_equal<Traits>((p-q)-Polynomial(a) , cf(-2, 0, 0, -17)); write("a*(p-q)" , (a*(p-q)) ); check_equal<Traits>((Polynomial(a)*(p-q)) , cf(0,0,0,-34)); write("(p-q)*a" , ((p-q)*a) ); check_equal<Traits>(((p-q)*Polynomial(a)) , cf(0,0,0,-34)); write("(p-q)/a" , ((p-q)/a) ); check_equal<Traits>((Polynomial(p-q)/a) , cf(0,0,0,-NT(.5)*NT(17))); write("subs(t=-t, p)", tr.negate_variable_object()(p) ); check_equal<Traits>(tr.negate_variable_object()(p) , cf( -1, -2, 27, 17)); /*write("t^degree(p) * subs(t=(1/t), p)", tr.invert_variable_object()(p) ); check_equal<Traits>( tr.invert_variable_object()(p) , cf(-17, 27, 2, -1));*/ NT v1[] = {-1, 1}; NT v2[] = {-2, 1}; Polynomial r = Polynomial(v1, v1+2); Polynomial s = Polynomial(v2, v2+2); p = r * r * s + Polynomial(NT(1)); write_variable( "p", p); check_equal<Traits>(p , cf(-1, 5, -4, 1)); q = r * s; write_variable("q", q ); check_equal<Traits>(q , cf( 2, -3, 1)); write("rem(p,q,t)", tr.remainder_object()(p,q) ); check_equal<Traits>(tr.remainder_object()(p,q) , cf(1)); write("prem(p,q,t)", tr.pseudo_remainder_object()(p,q) ); check_equal<Traits>(tr.pseudo_remainder_object()(p,q) , cf(1)); write("quo(p,q,t)", tr.quotient_object()(p,q) ); check_equal<Traits>(tr.quotient_object()(p,q) , cf(-1,1)); write("pquo(p,q,t)", tr.pseudo_quotient_object()(p,q) ); check_equal<Traits>(tr.pseudo_quotient_object()(p,q) , cf(-1,1)); p = r * r * s * s * s; write_variable( "p", p); check_equal<Traits>(p , cf(-8, 28, -38, 25, -8, 1)); q = r * s; write_variable("q", q ); check_equal<Traits>(q , cf(2,-3,1)); write("rem(p,q,t)", tr.remainder_object()(p,q) ); check_equal<Traits>(tr.remainder_object()(p,q) , cf(0)); write("prem(p,q,t)", tr.pseudo_remainder_object()(p,q) ); check_equal<Traits>(tr.pseudo_remainder_object()(p,q) , cf(0)); write("quo(p,q,t)", tr.quotient_object()(p,q) ); check_equal<Traits>(tr.quotient_object()(p,q) , cf(-4, 8, -5, 1)); write("pquo(p,q,t)", tr.pseudo_quotient_object()(p,q) ); check_equal<Traits>(tr.pseudo_quotient_object()(p,q) , cf(-4, 8, -5, 1)); /* int shift = 6; write("p * t^6", tr.shift_power_object(shift)(p) ); check_equal<Traits>(tr.shift_power_object(shift)(p) , cf(0,0,0,0,0,0,-8, 28, -38, 25, -8, 1));*/ NT v3[] = {0, 1}; Polynomial t = Polynomial(v3, v3+2); p = t * t; write_variable( "p", p); /*NT new_zero = NT(-1); write("subs(t=t-1,p)", tr.rational_translate_zero_object(new_zero)(p)); check_equal<Traits>(tr.rational_translate_zero_object(new_zero)(p) , cf(1, -2, 1));*/ }
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; } }
static int write_product(int ncid, const harp_product *product, netcdf_dimensions *dimensions) { harp_scalar datetime_start; harp_scalar datetime_stop; int result; int i; /* write conventions */ if (write_string_attribute(ncid, NC_GLOBAL, "Conventions", HARP_CONVENTION) != 0) { return -1; } /* write attributes */ if (harp_product_get_datetime_range(product, &datetime_start.double_data, &datetime_stop.double_data) == 0) { if (write_numeric_attribute(ncid, NC_GLOBAL, "datetime_start", harp_type_double, datetime_start) != 0) { return -1; } if (write_numeric_attribute(ncid, NC_GLOBAL, "datetime_stop", harp_type_double, datetime_stop) != 0) { return -1; } } if (product->source_product != NULL && strcmp(product->source_product, "") != 0) { if (write_string_attribute(ncid, NC_GLOBAL, "source_product", product->source_product) != 0) { return -1; } } if (product->history != NULL && strcmp(product->history, "") != 0) { if (write_string_attribute(ncid, NC_GLOBAL, "history", product->history) != 0) { return -1; } } /* determine dimensions */ for (i = 0; i < product->num_variables; i++) { harp_variable *variable; int j; variable = product->variable[i]; for (j = 0; j < variable->num_dimensions; j++) { netcdf_dimension_type dimension_type; dimension_type = get_netcdf_dimension_type(variable->dimension_type[j]); if (dimensions_add(dimensions, dimension_type, variable->dimension[j]) < 0) { return -1; } } if (variable->data_type == harp_type_string) { long length; /* determine length for the string dimension (ensure a minimum length of 1) */ length = harp_get_max_string_length(variable->num_elements, variable->data.string_data); if (length == 0) { length = 1; } if (dimensions_add(dimensions, netcdf_dimension_string, length) < 0) { return -1; } } } /* write dimensions */ if (write_dimensions(ncid, dimensions) != 0) { return -1; } /* write variable definitions + attributes */ for (i = 0; i < product->num_variables; i++) { int varid; if (write_variable_definition(ncid, product->variable[i], dimensions, &varid) != 0) { return -1; } assert(varid == i); } result = nc_enddef(ncid); if (result != NC_NOERR) { harp_set_error(HARP_ERROR_NETCDF, "%s", nc_strerror(result)); return -1; } /* write variable data */ for (i = 0; i < product->num_variables; i++) { if (write_variable(ncid, i, product->variable[i]) != 0) { return -1; } } return 0; }
static void parse_args(int argc, char **argv) { int ch, i; while ((ch = getopt_long(argc, argv, "aAbdDf:gHlLNn:OpRt:uw", longopts, NULL)) != -1) { switch (ch) { case 'a': aflag++; break; case 'A': Aflag++; break; case 'b': bflag++; break; case 'd': dflag++; break; case 'D': Dflag++; break; case 'g': gflag++; break; case 'H': Hflag++; break; case 'l': lflag++; break; case 'L': Lflag++; break; case 'n': varname = optarg; break; case 'N': Nflag++; break; case 'O': load_opt_flag++; break; case 'p': pflag++; break; case 'R': Rflag++; break; case 't': attrib = strtoul(optarg, NULL, 16); break; case 'u': uflag++; break; case 'w': wflag++; break; case 'f': case 0: errx(1, "unknown or unimplemented option\n"); break; default: usage(); } } argc -= optind; argv += optind; if (argc == 1) varname = argv[0]; if (aflag + Dflag + wflag > 1) { warnx("Can only use one of -a (--append), " "-D (--delete) and -w (--write)"); usage(); } if (aflag + Dflag + wflag > 0 && varname == NULL) { warnx("Must specify a variable for -a (--append), " "-D (--delete) or -w (--write)"); usage(); } if (aflag) append_variable(varname, NULL); else if (Dflag) delete_variable(varname); else if (wflag) write_variable(varname, NULL); else if (Lflag) print_known_guid(); else if (varname) { pflag++; print_variable(varname); } else if (argc > 0) { pflag++; for (i = 0; i < argc; i++) print_variable(argv[i]); } else print_variables(); }
//**************************************************************************** // lua remote function server // read function call data and execute the function. this function empties the // stack on entry and exit. This sets a custom error handler to catch errors // around the function call. static void read_cmd_call( Transport *tpt, lua_State *L ) { int i, stackpos, good_function, nargs; uint32_t len; char *funcname; char *token = NULL; // read function name len = transport_read_uint32_t( tpt ); /* function name string length */ funcname = ( char * )alloca( len + 1 ); transport_read_string( tpt, funcname, len ); funcname[ len ] = 0; // get function // @@@ perhaps handle more like variables instead of using a long string? // @@@ also strtok is not thread safe token = strtok( funcname, "." ); lua_getglobal( L, token ); token = strtok( NULL, "." ); while( token != NULL ) { lua_getfield( L, -1, token ); lua_remove( L, -2 ); token = strtok( NULL, "." ); } stackpos = lua_gettop( L ) - 1; good_function = LUA_ISCALLABLE( L, -1 ); // read number of arguments nargs = transport_read_uint32_t( tpt ); // read in each argument, leave it on the stack for ( i = 0; i < nargs; i ++ ) read_variable( tpt, L ); // call the function if( good_function ) { int nret, error_code; error_code = lua_pcall( L, nargs, LUA_MULTRET, 0 ); // handle errors if ( error_code ) { size_t len; const char *errmsg; errmsg = lua_tolstring (L, -1, &len); transport_write_uint8_t( tpt, 1 ); transport_write_uint32_t( tpt, error_code ); transport_write_uint32_t( tpt, len ); transport_write_string( tpt, errmsg, len ); } else { // pass the return values back to the caller transport_write_uint8_t( tpt, 0 ); nret = lua_gettop( L ) - stackpos; transport_write_uint32_t( tpt, nret ); for ( i = 0; i < nret; i ++ ) write_variable( tpt, L, stackpos + 1 + i ); } } else { // bad function const char *msg = "undefined function: "; int errlen = strlen( msg ) + len; transport_write_uint8_t( tpt, 1 ); transport_write_uint32_t( tpt, LUA_ERRRUN ); transport_write_uint32_t( tpt, errlen ); transport_write_string( tpt, msg, strlen( msg ) ); transport_write_string( tpt, funcname, len ); } // empty the stack lua_settop ( L, 0 ); }
void serialize(writable& dest, size_t x) { write_variable(dest, x); }
/** * mono_debug_add_method: */ MonoDebugMethodAddress * mono_debug_add_method (MonoMethod *method, MonoDebugMethodJitInfo *jit, MonoDomain *domain) { MonoDebugDataTable *table; MonoDebugMethodAddress *address; guint8 buffer [BUFSIZ]; guint8 *ptr, *oldptr; guint32 i, size, total_size, max_size; mono_debugger_lock (); table = lookup_data_table (domain); max_size = (5 * 5) + 1 + (10 * jit->num_line_numbers) + (25 + sizeof (gpointer)) * (1 + jit->num_params + jit->num_locals); if (max_size > BUFSIZ) ptr = oldptr = (guint8 *)g_malloc (max_size); else ptr = oldptr = buffer; write_leb128 (jit->prologue_end, ptr, &ptr); write_leb128 (jit->epilogue_begin, ptr, &ptr); write_leb128 (jit->num_line_numbers, ptr, &ptr); for (i = 0; i < jit->num_line_numbers; i++) { MonoDebugLineNumberEntry *lne = &jit->line_numbers [i]; write_sleb128 (lne->il_offset, ptr, &ptr); write_sleb128 (lne->native_offset, ptr, &ptr); } write_leb128 (jit->has_var_info, ptr, &ptr); if (jit->has_var_info) { *ptr++ = jit->this_var ? 1 : 0; if (jit->this_var) write_variable (jit->this_var, ptr, &ptr); write_leb128 (jit->num_params, ptr, &ptr); for (i = 0; i < jit->num_params; i++) write_variable (&jit->params [i], ptr, &ptr); write_leb128 (jit->num_locals, ptr, &ptr); for (i = 0; i < jit->num_locals; i++) write_variable (&jit->locals [i], ptr, &ptr); *ptr++ = jit->gsharedvt_info_var ? 1 : 0; if (jit->gsharedvt_info_var) { write_variable (jit->gsharedvt_info_var, ptr, &ptr); write_variable (jit->gsharedvt_locals_var, ptr, &ptr); } } size = ptr - oldptr; g_assert (size < max_size); total_size = size + sizeof (MonoDebugMethodAddress); if (method_is_dynamic (method)) { address = (MonoDebugMethodAddress *)g_malloc0 (total_size); } else { address = (MonoDebugMethodAddress *)mono_mempool_alloc (table->mp, total_size); } address->code_start = jit->code_start; address->code_size = jit->code_size; memcpy (&address->data, oldptr, size); if (max_size > BUFSIZ) g_free (oldptr); g_hash_table_insert (table->method_address_hash, method, address); mono_debugger_unlock (); return address; }