Annotation of parser3/src/main/compile_tools.C, revision 1.68
1.29 paf 1: /** @file
1.30 paf 2: Parser: compiler support helper functions.
3:
1.62 misha 4: Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com)
1.45 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.51 paf 6: */
1.30 paf 7:
1.68 ! misha 8: static const char * const IDENT_COMPILE_TOOLS_C="$Date: 2009-06-07 13:15:54 $";
1.1 paf 9:
10: #include "compile_tools.h"
11: #include "pa_string.h"
12: #include "pa_array.h"
13: #include "pa_exception.h"
1.9 paf 14: #include "pa_vstring.h"
1.15 paf 15: #include "pa_vdouble.h"
1.67 misha 16: #include "pa_vmethod_frame.h"
1.1 paf 17:
1.61 misha 18: Value* LA2V(ArrayOperation& literal_string_array, int offset, OP::OPCODE code) {
19: return literal_string_array[offset+0].code==code?literal_string_array[offset+2/*skip opcode&origin*/].value
1.43 paf 20: :0;
1.15 paf 21: }
22:
1.58 paf 23: void maybe_change_string_literal_to_double_literal(ArrayOperation& literal_array) {
1.60 misha 24: assert(literal_array[0].code==OP::OP_VALUE);
1.58 paf 25: VString& vstring=*static_cast<VString*>(literal_array[2/*opcode+origin*/].value);
26: if(isdigit(vstring.string().first_char()))
27: literal_array.put(2/*opcode+origin*/, &vstring.as_expr_result());
1.1 paf 28: }
1.53 paf 29:
1.55 paf 30: void change_string_literal_value(ArrayOperation& literal_string_array, const String& new_value) {
1.60 misha 31: assert(literal_string_array[0].code==OP::OP_VALUE);
1.58 paf 32: static_cast<VString*>(literal_string_array[2/*opcode+origin*/].value)->set_string(new_value);
1.53 paf 33: }
34:
1.55 paf 35: void changetail_or_append(ArrayOperation& opcodes,
1.60 misha 36: OP::OPCODE find, bool with_argument, OP::OPCODE replace, OP::OPCODE notfound) {
1.55 paf 37: int tail=opcodes.count()-(with_argument?2:1);
1.49 paf 38: if(tail>=0) {
1.55 paf 39: Operation& op=opcodes.get_ref(tail);
1.48 paf 40: if(op.code==find) {
41: op.code=replace;
42: return;
43: }
44: }
45:
1.55 paf 46: opcodes+=Operation(notfound);
1.48 paf 47: }
48:
1.66 misha 49: bool maybe_change_first_opcode(ArrayOperation& opcodes, OP::OPCODE find, OP::OPCODE replace) {
50: if(opcodes[0].code!=find)
51: return false;
1.1 paf 52:
1.66 misha 53: opcodes.put(0, replace);
54: return true;
1.62 misha 55: }
56:
1.67 misha 57:
58: // OP_VALUE+origin+self+OP_GET_ELEMENT+OP_VALUE+origin+value+OP_GET_ELEMENT => OP_WITH_SELF__VALUE__GET_ELEMENT+origin+value
59: bool maybe_make_self(ArrayOperation& opcodes, ArrayOperation& diving_code, size_t divine_count){
60: const String* first_name=LA2S(diving_code);
61:
62: if(first_name && *first_name==SELF_ELEMENT_NAME){
63: #ifdef OPTIMIZE_BYTECODE_GET_SELF_ELEMENT
64: if(
65: divine_count>=8
66: && diving_code[3].code==OP::OP_GET_ELEMENT
67: && diving_code[4].code==OP::OP_VALUE
68: && diving_code[7].code==OP::OP_GET_ELEMENT
69: ){
70: // optimization for $self.field and ^self.method
71: O(opcodes, OP::OP_WITH_SELF__VALUE__GET_ELEMENT);
72: P(opcodes, diving_code, 5/*offset*/, 2/*limit*/); // copy second origin+value. we know that the first one is "self"
73: if(divine_count>8)
74: P(opcodes, diving_code, 8/*offset*/); // copy tail
75: } else
76: #endif
77: {
78: // self.xxx... => xxx...
79: // OP_VALUE+origin+string+OP_GET_ELEMENT+... -> OP_WITH_SELF+...
80: O(opcodes, OP::OP_WITH_SELF); /* stack: starting context */
81: P(opcodes, diving_code, divine_count>=4?4/*OP::OP_VALUE+origin+string+OP::OP_GET_ELEMENTx*/:3/*OP::OP_+origin+string*/);
82: }
83: return true;
84: }
85: return false;
86: }
87:
88:
1.68 ! misha 89: Method::Call_type GetMethodCallType(Parse_control& pc, ArrayOperation& literal_array) {
! 90: const String* full_name=LA2S(literal_array);
! 91: int pos=full_name->pos(':');
! 92: if(pos > 0) {
! 93: const String call_type=full_name->mid(0, pos);
! 94: if(call_type!=method_call_type_static)
! 95: throw Exception("parser.compile",
! 96: &call_type,
! 97: "incorrect method call type. the only valid call type method prefix is '"METHOD_CALL_TYPE_STATIC"'"
! 98: );
! 99: const String *sole_name=&full_name->mid(pos+1, full_name->length());
! 100: // replace full method name (static:method) by sole method name (method). it will be used later.
! 101: change_string_literal_value(literal_array, *sole_name);
! 102: return Method::CT_STATIC;
! 103: }
! 104: return pc.get_methods_call_type();
! 105: }
! 106:
1.55 paf 107: void push_LS(Parse_control& pc, lexical_state new_state) {
1.42 paf 108: if(pc.ls_sp<MAX_LEXICAL_STATES) {
109: pc.ls_stack[pc.ls_sp++]=pc.ls;
110: pc.ls=new_state;
1.1 paf 111: } else
1.55 paf 112: throw Exception(0, 0,
113: "push_LS: ls_stack overflow");
1.1 paf 114: }
1.55 paf 115: void pop_LS(Parse_control& pc) {
1.42 paf 116: if(--pc.ls_sp>=0)
117: pc.ls=pc.ls_stack[pc.ls_sp];
1.1 paf 118: else
1.55 paf 119: throw Exception(0, 0,
120: "pop_LS: ls_stack underflow");
121: }
122:
123: const String& Parse_control::alias_method(const String& name) {
124: return (main_alias && name==main_method_name)?*main_alias:name;
1.1 paf 125: }
E-mail: