--- parser3/src/main/execute.C 2003/04/02 09:32:11 1.295.2.27.2.12 +++ parser3/src/main/execute.C 2003/11/20 15:35:31 1.301 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char* IDENT_EXECUTE_C="$Date: 2003/04/02 09:32:11 $"; +static const char* IDENT_EXECUTE_C="$Date: 2003/11/20 15:35:31 $"; #include "pa_opcode.h" #include "pa_array.h" @@ -23,8 +23,7 @@ static const char* IDENT_EXECUTE_C="$Dat #include "pa_vimage.h" #include "pa_wwrapper.h" -// #define DEBUG_EXECUTE -#define PA_DEBUG_NO_STACK_TRACE +//#define DEBUG_EXECUTE #ifdef DEBUG_EXECUTE char *opcode_name[]={ @@ -71,20 +70,21 @@ void debug_printf(SAPI_Info& sapi_info, void debug_dump(SAPI_Info& sapi_info, int level, ArrayOperation& ops) { Array_iterator i(ops); while(i.has_next()) { - Operation op=i.next(); + OPCODE opcode=i.next().code; - if(op.code==OP_VALUE || op.code==OP_STRING__WRITE) { - Value& value=i.next().value; + if(opcode==OP_VALUE || opcode==OP_STRING__WRITE) { + Operation::Origin origin=i.next().origin; + Value& value=*i.next().value; debug_printf(sapi_info, "%*s%s" " \"%s\" %s", - level*4, "", opcode_name[op.code], + level*4, "", opcode_name[opcode], value.get_string()->cstr(), value.type()); continue; } - debug_printf(sapi_info, "%*s%s", level*4, "", opcode_name[op.code]); + debug_printf(sapi_info, "%*s%s", level*4, "", opcode_name[opcode]); - switch(op.code) { + switch(opcode) { case OP_CURLY_CODE__STORE_PARAM: case OP_EXPR_CODE__STORE_PARAM: case OP_CURLY_CODE__CONSTRUCT: @@ -105,6 +105,8 @@ void debug_dump(SAPI_Info& sapi_info, in void Request::execute(ArrayOperation& ops) { register Stack& stack=this->stack; // helps a lot on MSVC: 'esi' + const String* debug_name=0; Operation::Origin debug_origin={0}; + try{ #ifdef DEBUG_EXECUTE debug_printf(sapi_info, "source----------------------------\n"); debug_dump(sapi_info, 0, ops); @@ -118,6 +120,7 @@ void Request::execute(ArrayOperation& op "execution stopped"); } OPCODE opcode=i.next().code; + #ifdef DEBUG_EXECUTE debug_printf(sapi_info, "%d:%s", stack.top_index()+1, opcode_name[opcode]); #endif @@ -126,6 +129,7 @@ void Request::execute(ArrayOperation& op // param in next instruction case OP_VALUE: { + debug_origin=i.next().origin; Value& value=*i.next().value; #ifdef DEBUG_EXECUTE debug_printf(sapi_info, " \"%s\" %s", value.get_string()->cstr(), value.type()); @@ -180,7 +184,7 @@ void Request::execute(ArrayOperation& op case OP_CONSTRUCT_VALUE: { Value& value=stack.pop().value(); - const String& name=stack.pop().string(); + const String& name=stack.pop().string(); debug_name=&name; Value& ncontext=stack.pop().value(); ncontext.put_element(name, &value, false); break; @@ -191,7 +195,7 @@ void Request::execute(ArrayOperation& op wcontext->set_in_expression(false); Value& value=stack.pop().value(); - const String& name=stack.pop().string(); + const String& name=stack.pop().string(); debug_name=&name; Value& ncontext=stack.pop().value(); ncontext.put_element(name, &value.as_expr_result(), false); break; @@ -201,7 +205,7 @@ void Request::execute(ArrayOperation& op ArrayOperation& local_ops=*i.next().ops; #ifdef DEBUG_EXECUTE debug_printf(sapi_info, " (%d)\n", local_ops.count()); - debug_dump(sapi_info, 1, *local_ops); + debug_dump(sapi_info, 1, local_ops); #endif Value& value=*new VJunction(new Junction( get_self(), 0, @@ -210,7 +214,7 @@ void Request::execute(ArrayOperation& op wcontext, &local_ops)); - const String& name=stack.pop().string(); + const String& name=stack.pop().string(); debug_name=&name; Value& ncontext=stack.pop().value(); ncontext.put_element(name, &value, false); break; @@ -220,7 +224,7 @@ void Request::execute(ArrayOperation& op ArrayOperation& local_ops=*i.next().ops; #ifdef DEBUG_EXECUTE debug_printf(sapi_info, " (%d)\n", local_ops.count()); - debug_dump(sapi_info, 1, *local_ops); + debug_dump(sapi_info, 1, local_ops); #endif stack.push(local_ops); break; @@ -243,6 +247,7 @@ void Request::execute(ArrayOperation& op } case OP_STRING__WRITE: { + i.next(); // ignore origin Value* value=i.next().value; #ifdef DEBUG_EXECUTE debug_printf(sapi_info, " \"%s\"", value->get_string()->cstr()); @@ -253,20 +258,26 @@ void Request::execute(ArrayOperation& op case OP_GET_ELEMENT_OR_OPERATOR: { - Value& value=get_element(true); + const String& name=stack.pop().string(); debug_name=&name; + Value& ncontext=stack.pop().value(); + Value& value=get_element(ncontext, name, true); stack.push(value); break; } case OP_GET_ELEMENT: { - Value& value=get_element(false); + const String& name=stack.pop().string(); debug_name=&name; + Value& ncontext=stack.pop().value(); + Value& value=get_element(ncontext, name, false); stack.push(value); break; } case OP_GET_ELEMENT__WRITE: { - Value& value=get_element(false); + const String& name=stack.pop().string(); debug_name=&name; + Value& ncontext=stack.pop().value(); + Value& value=get_element(ncontext, name, false); write_assign_lang(value); break; } @@ -330,7 +341,7 @@ void Request::execute(ArrayOperation& op VMethodFrame& frame=stack.top_value().method_frame(); #ifdef DEBUG_EXECUTE debug_printf(sapi_info, " (%d)\n", local_ops.count()); - debug_dump(sapi_info, 1, *local_ops); + debug_dump(sapi_info, 1, local_ops); #endif // when they evaluate expression parameter, // the object expression result @@ -366,9 +377,10 @@ void Request::execute(ArrayOperation& op case OP_CALL: case OP_CALL__WRITE: { + //is_debug_junction=true; ArrayOperation* local_ops=i.next().ops; #ifdef DEBUG_EXECUTE - debug_printf(sapi_info, " (%d)\n", local_ops?local_ops.count():0); + debug_printf(sapi_info, " (%d)\n", local_ops?local_ops->count():0); if(local_ops) debug_dump(sapi_info, 1, *local_ops); @@ -377,11 +389,17 @@ void Request::execute(ArrayOperation& op Value& value=stack.pop().value(); Junction* junction=value.get_junction(); - if(!junction) - throw Exception("parser.runtime", - 0, - "(%s) not a method or junction, can not call it", - value.type()); + if(!junction) { + if(value.is("void")) + throw Exception("parser.runtime", + 0, + "undefined method"); + else + throw Exception("parser.runtime", + 0, + "is '%s', not a method or junction, can not call it", + value.type()); + } /* no check needed, code compiled the way that that's impossible // check: // that this is method-junction, not a code-junction @@ -391,7 +409,7 @@ void Request::execute(ArrayOperation& op if(!junction->method) throw Exception("parser.runtime", - "(%s) is code junction, can not call it", + "is '%s', it is code junction, can not call it", value.type()); */ @@ -413,7 +431,7 @@ void Request::execute(ArrayOperation& op if(frame.junction.method->call_type!=Method::CT_STATIC) { // this is a constructor call - if(Value* value=called_class.create_new_value()) { + if(Value* value=called_class.create_new_value(fpool)) { // some stateless_class creatable derivates new_self=value; } else @@ -445,26 +463,16 @@ void Request::execute(ArrayOperation& op if( method.call_type==Method::CT_ANY || method.call_type==call_type) { // allowed call type? -#ifndef PA_DEBUG_NO_STACK_TRACE - try { -#endif - method_frame=&frame; - if(method.native_code) { // native code? - method.check_actual_numbered_params( - frame.junction.self, - /*frame.name(), */frame.numbered_params()); - method.native_code( - *this, - /*frame.name(), */frame.numbered_params()); // execute it - } else // parser code, execute it - recoursion_checked_execute(/*frame.name(), */*method.parser_code); -#ifndef PA_DEBUG_NO_STACK_TRACE - } catch(...) { - // record it to stack trace - exception_trace.push(frame.name()); - rethrow; - } -#endif + method_frame=&frame; + if(method.native_code) { // native code? + method.check_actual_numbered_params( + frame.junction.self, + /*frame.name(), */frame.numbered_params()); + method.native_code( + *this, + *frame.numbered_params()); // execute it + } else // parser code, execute it + recoursion_checked_execute(/*frame.name(), */*method.parser_code); } else throw Exception("parser.runtime", 0, //&frame.name(), @@ -487,6 +495,7 @@ void Request::execute(ArrayOperation& op #ifdef DEBUG_EXECUTE debug_printf(sapi_info, "<-returned"); #endif + //is_debug_junction=false; break; } @@ -701,49 +710,49 @@ void Request::execute(ArrayOperation& op } case OP_NUM_LT: { - Value& b=stack.pop().value(); Value& a=stack.pop().value(); - double result=a.as_double() - b.as_double(); - Value& value=*new VBool(result < 0.0); + volatile double b_double=stack.pop().value().as_double(); + volatile double a_double=stack.pop().value().as_double(); + Value& value=*new VBool(a_double 0.0); + volatile double b_double=stack.pop().value().as_double(); + volatile double a_double=stack.pop().value().as_double(); + Value& value=*new VBool(a_double>b_double); stack.push(value); break; } case OP_NUM_LE: { - Value& b=stack.pop().value(); Value& a=stack.pop().value(); - double result=a.as_double() - b.as_double(); - Value& value=*new VBool(result <= 0.0); + volatile double b_double=stack.pop().value().as_double(); + volatile double a_double=stack.pop().value().as_double(); + Value& value=*new VBool(a_double<=b_double); stack.push(value); break; } case OP_NUM_GE: { - Value& b=stack.pop().value(); Value& a=stack.pop().value(); - double result=a.as_double() - b.as_double(); - Value& value=*new VBool(result >= 0.0); + volatile double b_double=stack.pop().value().as_double(); + volatile double a_double=stack.pop().value().as_double(); + Value& value=*new VBool(a_double>=b_double); stack.push(value); break; } case OP_NUM_EQ: { - Value& b=stack.pop().value(); Value& a=stack.pop().value(); - double result=a.as_double() - b.as_double(); - Value& value=*new VBool(result == 0.0); + volatile double b_double=stack.pop().value().as_double(); + volatile double a_double=stack.pop().value().as_double(); + Value& value=*new VBool(a_double==b_double); stack.push(value); break; } case OP_NUM_NE: { - Value& b=stack.pop().value(); Value& a=stack.pop().value(); - double result=a.as_double() - b.as_double(); - Value& value=*new VBool(result != 0.0); + volatile double b_double=stack.pop().value().as_double(); + volatile double a_double=stack.pop().value().as_double(); + Value& value=*new VBool(a_double!=b_double); stack.push(value); break; } @@ -803,15 +812,19 @@ void Request::execute(ArrayOperation& op "invalid opcode %d", opcode); } } + } catch(...) { + // record it to stack trace + if(debug_name) + exception_trace.push(Trace(debug_name, debug_origin)); + rethrow; + } } /** @todo cache|prepare junctions @bug ^superbase:method would dynamically call ^base:method if there is any */ -Value& Request::get_element(bool can_call_operator) { - const String& name=stack.pop().string(); //remember_name=name; - Value& ncontext=stack.pop().value(); +Value& Request::get_element(Value& ncontext, const String& name, bool can_call_operator) { Value* value=0; if(can_call_operator) { if(Method* method=main_class.get_method(name)) // looking operator of that name FIRST @@ -924,7 +937,7 @@ StringOrValue Request::process(Value& in return result; } -const String& Request::execute_method(VMethodFrame& amethod_frame, const Method& method) { +StringOrValue Request::execute_method(VMethodFrame& amethod_frame, const Method& method) { VMethodFrame *saved_method_frame=method_frame; Value* saved_rcontext=rcontext; WContext *saved_wcontext=wcontext; @@ -936,7 +949,7 @@ const String& Request::execute_method(VM execute(*method.parser_code); // result - const String& result=wcontext->result().as_string(); + StringOrValue result=wcontext->result(); wcontext=saved_wcontext; rcontext=saved_rcontext; @@ -947,14 +960,14 @@ const String& Request::execute_method(VM } const String* Request::execute_method(Value& aself, - const Method& method, VString* optional_param, - bool do_return_string) { + const Method& method, VString* optional_param, + bool do_return_string) { VMethodFrame *saved_method_frame=method_frame; Value* saved_rcontext=rcontext; WContext *saved_wcontext=wcontext; Junction local_junction(aself, &method, 0,0,0, 0); - VMethodFrame local_frame(/*method.name, */local_junction, method_frame/*caller*/); + VMethodFrame local_frame(local_junction, method_frame/*caller*/); if(optional_param && local_frame.can_store_param()) { local_frame.store_param(*optional_param); local_frame.fill_unspecified_params();