--- parser3/src/main/execute.C 2009/05/20 09:09:20 1.336 +++ parser3/src/main/execute.C 2009/05/23 07:02:38 1.341 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_EXECUTE_C="$Date: 2009/05/20 09:09:20 $"; +static const char * const IDENT_EXECUTE_C="$Date: 2009/05/23 07:02:38 $"; #include "pa_opcode.h" #include "pa_array.h" @@ -65,6 +65,37 @@ char *opcode_name[]={ "PREPARE_TO_CONSTRUCT_OBJECT", "PREPARE_TO_EXPRESSION", "CALL", "CALL__WRITE", +#ifdef OPTIMIZE_BYTECODE_CONSTRUCT + "ROOT_CONSTRUCT_EXPR", + "ROOT_ELEMENT_CONSTRUCT_EXPR", + + + "ROOT_CONSTRUCT_VALUE", + "ROOT_ELEMENT_CONSTRUCT_VALUE", + + + "WRITE_CONSTRUCT_EXPR", + "WRITE_ELEMENT_CONSTRUCT_EXPR", + + + "WRITE_CONSTRUCT_VALUE", + "WRITE_ELEMENT_CONSTRUCT_VALUE", +#endif + +#ifdef OPTIMIZE_BYTECODE_CALL_CONSTRUCT + "ROOT_CALL_CONSTRUCT_EXPR", + //"ROOT_CALL_OBJECT_ELEMENT_CONSTRUCT_EXPR", + + "ROOT_CALL_CONSTRUCT_VALUE", + //"ROOT_CALL_OBJECT_ELEMENT_CONSTRUCT_VALUE", + + "WRITE_CALL_CONSTRUCT_EXPR", + //"WRITE_CALL_OBJECT_ELEMENT_CONSTRUCT_EXPR", + + "WRITE_CALL_CONSTRUCT_VALUE", + //"WRITE_CALL_OBJECT_ELEMENT_CONSTRUCT_VALUE", +#endif + // expression ops: unary "NEG", "INV", "NOT", "DEF", "IN", "FEXISTS", "DEXISTS", // expression ops: binary @@ -95,7 +126,7 @@ void debug_dump(SAPI_Info& sapi_info, in while(i.has_next()) { OP::OPCODE opcode=i.next().code; -#if (defined(OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT) || defined(OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT)) && defined(OPTIMIZE_BYTECODE_USE_TWO_OPERANDS_INSTRUCTIONS) +#if defined(OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT) || defined(OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT) if( 1==0 #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT @@ -106,6 +137,26 @@ void debug_dump(SAPI_Info& sapi_info, in || opcode==OP::OP_GET_OBJECT_VAR_ELEMENT || opcode==OP::OP_GET_OBJECT_VAR_ELEMENT__WRITE #endif +#ifdef OPTIMIZE_BYTECODE_CONSTRUCT + || opcode==OP::OP_ROOT_CONSTRUCT_EXPR + || opcode==OP::OP_ROOT_ELEMENT_CONSTRUCT_EXPR + + || opcode==OP::OP_ROOT_CONSTRUCT_VALUE + || opcode==OP::OP_ROOT_ELEMENT_CONSTRUCT_VALUE + + || opcode==OP::OP_WRITE_CONSTRUCT_EXPR + || opcode==OP::OP_WRITE_ELEMENT_CONSTRUCT_EXPR + + || opcode==OP::OP_WRITE_CONSTRUCT_VALUE + || opcode==OP::OP_WRITE_ELEMENT_CONSTRUCT_VALUE +#endif +#ifdef OPTIMIZE_BYTECODE_CALL_CONSTRUCT + || opcode==OP::OP_ROOT_CALL_CONSTRUCT_EXPR + || opcode==OP::OP_ROOT_CALL_CONSTRUCT_VALUE + + || opcode==OP::OP_WRITE_CALL_CONSTRUCT_EXPR + || opcode==OP::OP_WRITE_CALL_CONSTRUCT_VALUE +#endif ){ i.next(); // skip origin Value& value1=*i.next().value; @@ -130,14 +181,6 @@ void debug_dump(SAPI_Info& sapi_info, in || opcode==OP::OP_VALUE__GET_ELEMENT__WRITE || opcode==OP::OP_VALUE__GET_ELEMENT_OR_OPERATOR #endif -#if defined(OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT) && !defined(OPTIMIZE_BYTECODE_USE_TWO_OPERANDS_INSTRUCTIONS) - || opcode==OP::OP_GET_OBJECT_ELEMENT - || opcode==OP::OP_GET_OBJECT_ELEMENT__WRITE -#endif -#if defined(OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT) && !defined(OPTIMIZE_BYTECODE_USE_TWO_OPERANDS_INSTRUCTIONS) - || opcode==OP::OP_GET_OBJECT_VAR_ELEMENT - || opcode==OP::OP_GET_OBJECT_VAR_ELEMENT__WRITE -#endif ) { Operation::Origin origin=i.next().origin; Value& value=*i.next().value; @@ -193,6 +236,8 @@ void Request::execute(ArrayOperation& op for(Array_iterator i(ops); i.has_next(); ) { OP::OPCODE opcode=i.next().code; + bool with_root=true; + #ifdef DEBUG_EXECUTE debug_printf(sapi_info, "%d:%s", stack.top_index()+1, opcode_name[opcode]); #endif @@ -285,6 +330,179 @@ void Request::execute(ArrayOperation& op break; } + +#ifdef OPTIMIZE_BYTECODE_CONSTRUCT + case OP::OP_WRITE_CONSTRUCT_EXPR: + case OP::OP_WRITE_CONSTRUCT_VALUE: + { + if(wcontext==method_frame) + throw Exception(PARSER_RUNTIME, + 0, + "$.name outside of $name[...]"); + with_root=false; + // don't stop here. continue in the next block + } + + case OP::OP_ROOT_CONSTRUCT_EXPR: + case OP::OP_ROOT_CONSTRUCT_VALUE: + { + debug_origin=i.next().origin; + const String& name=*i.next().value->get_string(); debug_name=&name; + + DEBUG_PRINT_STRING(name) + + debug_origin=i.next().origin; + Value& value=*i.next().value; + + if( + opcode == OP::OP_WRITE_CONSTRUCT_EXPR + || opcode == OP::OP_ROOT_CONSTRUCT_EXPR + ){ + wcontext->set_in_expression(true); + put_element( (with_root)?*method_frame:*wcontext, name, &value.as_expr_result()); + } else { + put_element( (with_root)?*method_frame:*wcontext, name, &value); + } + break; + } + + case OP::OP_WRITE_ELEMENT_CONSTRUCT_EXPR: + case OP::OP_WRITE_ELEMENT_CONSTRUCT_VALUE: + { + if(wcontext==method_frame) + throw Exception(PARSER_RUNTIME, + 0, + "$.name outside of $name[...]"); + with_root=false; + // don't stop here. continue in the next block + } + + case OP::OP_ROOT_ELEMENT_CONSTRUCT_EXPR: + case OP::OP_ROOT_ELEMENT_CONSTRUCT_VALUE: + { + debug_origin=i.next().origin; + const String& name=*i.next().value->get_string(); debug_name=&name; + + DEBUG_PRINT_STRING(name) + + debug_origin=i.next().origin; + const String& source_var_name=*i.next().value->get_string(); debug_name=&source_var_name; + + DEBUG_PRINT_STRING(source_var_name) + + Value& value=get_element(*rcontext, source_var_name); + + if( + opcode == OP::OP_WRITE_ELEMENT_CONSTRUCT_EXPR + || opcode == OP::OP_ROOT_ELEMENT_CONSTRUCT_EXPR + ){ + wcontext->set_in_expression(true); + put_element( (with_root)?*method_frame:*wcontext, name, &value.as_expr_result()); + } else { + put_element( (with_root)?*method_frame:*wcontext, name, value.is_void()?new VString():&value); + } + break; + } +#endif // OPTIMIZE_BYTECODE_CONSTRUCT + +#ifdef OPTIMIZE_BYTECODE_CALL_CONSTRUCT + case OP::OP_WRITE_CALL_CONSTRUCT_EXPR: + case OP::OP_WRITE_CALL_CONSTRUCT_VALUE: + { + if(wcontext==method_frame) + throw Exception(PARSER_RUNTIME, + 0, + "$.name outside of $name[...]"); + with_root=false; + // don't stop here. continue in the next block + } + + case OP::OP_ROOT_CALL_CONSTRUCT_EXPR: + case OP::OP_ROOT_CALL_CONSTRUCT_VALUE: + { + debug_origin=i.next().origin; + const String& name=*i.next().value->get_string(); debug_name=&name; + + DEBUG_PRINT_STRING(name) + + debug_origin=i.next().origin; + const String& method_name=*i.next().value->get_string(); debug_name=&method_name; + + DEBUG_PRINT_STRING(method_name) + + Value* vjunction; + if(Method* method=main_class.get_method(method_name)){ // looking operator of that name FIRST + if(!method->junction_template) + method->junction_template=new VJunction(main_class, method); + vjunction=method->junction_template; + } else { + vjunction=&get_element(*rcontext, method_name); + } + + // CALL + Operation call_op=i.next(); + ArrayOperation* local_ops=i.next().ops; + + DEBUG_PRINT_OPS(local_ops) + DEBUG_PRINT_STR("->\n") + + Junction* junction=vjunction->get_junction(); + if(!junction) { + if(vjunction->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", + vjunction->type()); + } + + 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); + } + + DEBUG_PRINT_STR("<-returned") + + if(call_op.code==OP::OP_CALL__WRITE){ + write_assign_lang(frame.result()); + } else { + Value& value=frame.result().as_value(); + + if( + opcode == OP::OP_WRITE_CALL_CONSTRUCT_EXPR + || opcode == OP::OP_ROOT_CALL_CONSTRUCT_EXPR + ){ + wcontext->set_in_expression(true); + put_element( (with_root)?*method_frame:*wcontext, name, &value.as_expr_result()); + } else { + put_element( (with_root)?*method_frame:*wcontext, name, &value); + } + } + + if(get_skip()) + return; + if(get_interrupted()) { + set_interrupted(false); + throw Exception("parser.interrupted", + 0, + "execution stopped"); + } + + break; + } +#endif // OPTIMIZE_BYTECODE_CALL_CONSTRUCT + case OP::OP_CONSTRUCT_EXPR: { // see OP_PREPARE_TO_EXPRESSION @@ -297,6 +515,7 @@ void Request::execute(ArrayOperation& op put_element(ncontext, name, &value); break; } + case OP::OP_CURLY_CODE__CONSTRUCT: { ArrayOperation& local_ops=*i.next().ops; @@ -404,10 +623,6 @@ void Request::execute(ArrayOperation& op Value& object=get_element(*rcontext, context_name); -#ifndef OPTIMIZE_BYTECODE_USE_TWO_OPERANDS_INSTRUCTIONS - i.next(); // skip OP_VALUE -#endif - debug_origin=i.next().origin; const String& field_name=*i.next().value->get_string(); debug_name=&field_name; @@ -437,10 +652,6 @@ void Request::execute(ArrayOperation& op Value& object=get_element(*rcontext, context_name); -#ifndef OPTIMIZE_BYTECODE_USE_TWO_OPERANDS_INSTRUCTIONS - i.next(); // skip OP_VALUE -#endif - debug_origin=i.next().origin; const String& var_name=*i.next().value->get_string(); debug_name=&var_name;