--- parser3/src/main/execute.C 2009/06/04 12:29:38 1.346 +++ parser3/src/main/execute.C 2009/06/13 07:05:57 1.348 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_EXECUTE_C="$Date: 2009/06/04 12:29:38 $"; +static const char * const IDENT_EXECUTE_C="$Date: 2009/06/13 07:05:57 $"; #include "pa_opcode.h" #include "pa_array.h" @@ -50,6 +50,7 @@ char *opcode_name[]={ #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT "VALUE__GET_ELEMENT", "VALUE__GET_ELEMENT__WRITE", + "WITH_ROOT__VALUE__GET_ELEMENT", #endif #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT "GET_OBJECT_ELEMENT", @@ -64,7 +65,12 @@ char *opcode_name[]={ "WITH_SELF__VALUE__GET_ELEMENT__WRITE", #endif "OBJECT_POOL", "STRING_POOL", - "PREPARE_TO_CONSTRUCT_OBJECT", "PREPARE_TO_EXPRESSION", + "PREPARE_TO_CONSTRUCT_OBJECT", +#ifdef OPTIMIZE_BYTECODE_CONSTRUCT_OBJECT + "CONSTRUCT_OBJECT", + "CONSTRUCT_OBJECT__WRITE", +#endif + "PREPARE_TO_EXPRESSION", "CALL", "CALL__WRITE", #ifdef OPTIMIZE_BYTECODE_CONSTRUCT @@ -142,6 +148,26 @@ void debug_dump(SAPI_Info& sapi_info, in continue; } #endif +#ifdef OPTIMIZE_BYTECODE_CONSTRUCT_OBJECT + if( + opcode==OP::OP_CONSTRUCT_OBJECT + || opcode==OP::OP_CONSTRUCT_OBJECT__WRITE + ){ + i.next(); // skip origin + Value& value1=*i.next().value; + i.next(); // skip origin + Value& value2=*i.next().value; + debug_printf(sapi_info, + "%*s%s" + " \"%s\" \"%s\"", + level*4, "", opcode_name[opcode], + debug_value_to_cstr(value1), debug_value_to_cstr(value2)); + + if(ArrayOperation* local_ops=i.next().ops) + debug_dump(sapi_info, level+1, *local_ops); + continue; + } +#endif if( opcode==OP::OP_VALUE || opcode==OP::OP_STRING__WRITE @@ -152,6 +178,7 @@ void debug_dump(SAPI_Info& sapi_info, in || opcode==OP::OP_VALUE__GET_ELEMENT || opcode==OP::OP_VALUE__GET_ELEMENT__WRITE || opcode==OP::OP_VALUE__GET_ELEMENT_OR_OPERATOR + || opcode==OP::OP_WITH_ROOT__VALUE__GET_ELEMENT #endif #ifdef OPTIMIZE_BYTECODE_GET_SELF_ELEMENT || opcode==OP::OP_WITH_SELF__VALUE__GET_ELEMENT @@ -176,6 +203,7 @@ void debug_dump(SAPI_Info& sapi_info, in debug_value_to_cstr(value), value.type()); continue; } + debug_printf(sapi_info, "%*s%s", level*4, "", opcode_name[opcode]); switch(opcode) { @@ -308,42 +336,42 @@ void Request::execute(ArrayOperation& op // OTHER ACTIONS BUT WITHs #ifdef OPTIMIZE_BYTECODE_CONSTRUCT +#define DO_CONSTRUCT(context, vvalue) { \ + debug_origin=i.next().origin; \ + const String& name=*i.next().value->get_string(); debug_name=&name; \ + DEBUG_PRINT_STRING(name) \ + Value& value=stack.pop().value(); \ + put_element( context, name, vvalue ); \ + break; \ + } +#define DO_CONSTRUCT_VALUE(context) DO_CONSTRUCT(context, &value) +#define DO_CONSTRUCT_EXPR(context) { \ + wcontext->set_in_expression(false); \ + DO_CONSTRUCT(context, &value.as_expr_result()) \ + } + case OP::OP_WITH_WRITE__VALUE__CONSTRUCT_EXPR: + { + if(wcontext==method_frame) + throw Exception(PARSER_RUNTIME, 0, "$.name outside of $name[...]"); + DO_CONSTRUCT_EXPR(*wcontext) + } + case OP::OP_WITH_WRITE__VALUE__CONSTRUCT_VALUE: { if(wcontext==method_frame) - throw Exception(PARSER_RUNTIME, - 0, - "$.name outside of $name[...]"); - // don't stop here. continue in the next block + throw Exception(PARSER_RUNTIME, 0, "$.name outside of $name[...]"); + DO_CONSTRUCT_VALUE(*wcontext) } - case OP::OP_WITH_ROOT__VALUE__CONSTRUCT_EXPR: - case OP::OP_WITH_ROOT__VALUE__CONSTRUCT_VALUE: - case OP::OP_WITH_SELF__VALUE__CONSTRUCT_EXPR: - case OP::OP_WITH_SELF__VALUE__CONSTRUCT_VALUE: - { - bool is_expr=(opcode==OP::OP_WITH_WRITE__VALUE__CONSTRUCT_EXPR - || opcode==OP::OP_WITH_ROOT__VALUE__CONSTRUCT_EXPR - || opcode==OP::OP_WITH_SELF__VALUE__CONSTRUCT_EXPR); - if(is_expr) - wcontext->set_in_expression(false); + case OP::OP_WITH_ROOT__VALUE__CONSTRUCT_EXPR: DO_CONSTRUCT_EXPR(*method_frame) - debug_origin=i.next().origin; - const String& name=*i.next().value->get_string(); debug_name=&name; - DEBUG_PRINT_STRING(name) + case OP::OP_WITH_ROOT__VALUE__CONSTRUCT_VALUE: DO_CONSTRUCT_VALUE(*method_frame) - Value& value=stack.pop().value(); + case OP::OP_WITH_SELF__VALUE__CONSTRUCT_EXPR: DO_CONSTRUCT_EXPR(get_self()) - put_element( - (opcode==OP::OP_WITH_ROOT__VALUE__CONSTRUCT_EXPR || opcode==OP::OP_WITH_ROOT__VALUE__CONSTRUCT_VALUE)?*method_frame: - (opcode==OP::OP_WITH_SELF__VALUE__CONSTRUCT_EXPR || opcode==OP::OP_WITH_SELF__VALUE__CONSTRUCT_VALUE)?get_self():*wcontext, - name, - is_expr?&value.as_expr_result():/*value.is_void()?new VString():*/&value - ); + case OP::OP_WITH_SELF__VALUE__CONSTRUCT_VALUE: DO_CONSTRUCT_VALUE(get_self()) - break; - } #endif // OPTIMIZE_BYTECODE_CONSTRUCT case OP::OP_CONSTRUCT_VALUE: @@ -575,6 +603,18 @@ void Request::execute(ArrayOperation& op write_assign_lang(value); break; } + + case OP::OP_WITH_ROOT__VALUE__GET_ELEMENT: + { + debug_origin=i.next().origin; + const String& name=*i.next().value->get_string(); debug_name=&name; + + DEBUG_PRINT_STRING(name) + + Value& value=get_element(*method_frame, name); + stack.push(value); + break; + } #endif #ifdef OPTIMIZE_BYTECODE_GET_SELF_ELEMENT @@ -604,7 +644,11 @@ void Request::execute(ArrayOperation& op WContext *saved_wcontext=wcontext; String::Language saved_lang=flang; flang=String::L_PASS_APPENDED; +#ifdef OPTIMIZE_SINGLE_STRING_WRITE + WObjectPoolWrapper local(0/*empty*/, wcontext); +#else WWrapper local(0/*empty*/, wcontext); +#endif wcontext=&local; execute(local_ops); @@ -669,11 +713,13 @@ void Request::execute(ArrayOperation& op break; } +#ifndef OPTIMIZE_BYTECODE_CONSTRUCT_OBJECT case OP::OP_PREPARE_TO_CONSTRUCT_OBJECT: { wcontext->set_constructing(true); break; } +#endif case OP::OP_PREPARE_TO_EXPRESSION: { @@ -788,6 +834,9 @@ void Request::execute(ArrayOperation& op } else #endif // OPTIMIZE_CALL { + if(wcontext->get_constructing()) + throw Exception(PARSER_RUNTIME, 0, "non-optimized constructing"); + VMethodFrame frame(*junction, method_frame); if(local_ops){ // store param code goes here size_t first = stack.top_index(); @@ -817,6 +866,88 @@ void Request::execute(ArrayOperation& op break; } +#ifdef OPTIMIZE_BYTECODE_CONSTRUCT_OBJECT + case OP::OP_CONSTRUCT_OBJECT: + case OP::OP_CONSTRUCT_OBJECT__WRITE: + { + // maybe they do ^class:method[] call, remember the fact + wcontext->set_somebody_entered_some_class(); + + debug_origin=i.next().origin; + Value& vclass_name=*i.next().value; + const String& class_name=*vclass_name.get_string(); + + DEBUG_PRINT_STRING(class_name) + + Value* class_value=classes().get(class_name); + if(!class_value) + throw Exception(PARSER_RUNTIME, + &class_name, + "class is undefined"); + + wcontext->set_constructing(true); + + debug_origin=i.next().origin; + Value& vconstructor_name=*i.next().value; + const String& constructor_name=*vconstructor_name.get_string(); debug_name=&constructor_name; + + DEBUG_PRINT_STRING(constructor_name) + + Value& constructor_value=get_element(*class_value, constructor_name); + + ArrayOperation* local_ops=i.next().ops; + DEBUG_PRINT_OPS(local_ops) + DEBUG_PRINT_STR("->\n") + + Junction* junction=constructor_value.get_junction(); + if(!junction) { + if(constructor_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", + constructor_value.type()); + } else { + VMethodFrame frame(*junction, method_frame); + if(local_ops){ // store param code goes here + size_t first = stack.top_index(); + execute(*local_ops); + + frame.store_params((Value**)stack.ptr(first), stack.top_index()-first); + op_call(frame); + + stack.set_top_index(first); + } else { + frame.empty_params(); + op_call(frame); + } + + if(opcode==OP::OP_CONSTRUCT_OBJECT) + stack.push(frame.result().as_value()); + else + write_pass_lang(frame.result()); + } + + DEBUG_PRINT_STR("<-returned") + + +/* + if(get_skip()) + return; + if(get_interrupted()) { + set_interrupted(false); + throw Exception("parser.interrupted", + 0, + "execution stopped"); + } +*/ + break; + } +#endif // OPTIMIZE_BYTECODE_CONSTRUCT_OBJECT + // expression ops: unary case OP::OP_NEG: {