Annotation of parser3/src/main/compile_tools.h, revision 1.108

1.32      paf         1: /** @file
1.33      paf         2:        Parser: compiler support helper functions decls.
                      3: 
1.103     moko        4:        Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com)
1.58      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1       paf         6: */
                      7: 
                      8: #ifndef COMPILE_TOOLS
                      9: #define COMPILE_TOOLS
1.62      paf        10: 
1.108   ! moko       11: #define IDENT_COMPILE_TOOLS_H "$Id: compile_tools.h,v 1.107 2015/04/08 18:08:53 moko Exp $"
1.1       paf        12: 
1.39      paf        13: #include "pa_opcode.h"
1.1       paf        14: #include "pa_types.h"
1.10      paf        15: #include "pa_vstring.h"
1.14      paf        16: #include "pa_request.h"
1.1       paf        17: 
1.68      paf        18: /// used to track source column number
                     19: #define TAB_SIZE 8
                     20: 
1.102     misha      21: #define METHOD_CALL_TYPE_STATIC "static"
                     22: #define METHOD_CALL_TYPE_DYNAMIC "dynamic"
                     23: const String method_call_type_static(METHOD_CALL_TYPE_STATIC);
                     24: const String method_call_type_dynamic(METHOD_CALL_TYPE_DYNAMIC);
                     25: 
1.1       paf        26: enum lexical_state {
1.48      parser     27:        LS_USER, LS_NAME_SQUARE_PART,
1.65      paf        28:        LS_USER_COMMENT,
1.7       paf        29:        LS_DEF_NAME,
                     30:        LS_DEF_PARAMS,
                     31:        LS_DEF_LOCALS,
                     32:        LS_DEF_COMMENT,
1.18      paf        33:        LS_DEF_SPECIAL_BODY,
1.42      parser     34:        LS_EXPRESSION_STRING_QUOTED,
                     35:        LS_EXPRESSION_STRING_APOSTROFED,
1.48      parser     36:        LS_EXPRESSION_VAR_NAME_WITH_COLON, LS_EXPRESSION_VAR_NAME_WITHOUT_COLON,
1.65      paf        37:        LS_EXPRESSION_COMMENT,
1.48      parser     38:        LS_VAR_NAME_SIMPLE_WITH_COLON, LS_VAR_NAME_SIMPLE_WITHOUT_COLON,
1.1       paf        39:        LS_VAR_NAME_CURLY,
1.22      paf        40:        LS_VAR_ROUND,
1.18      paf        41:        LS_VAR_SQUARE,
1.1       paf        42:        LS_VAR_CURLY,
                     43:        LS_METHOD_NAME,
1.18      paf        44:        LS_METHOD_SQUARE,
1.1       paf        45:        LS_METHOD_CURLY,
1.22      paf        46:        LS_METHOD_ROUND,
1.1       paf        47:        LS_METHOD_AFTER
                     48: };
1.68      paf        49: 
                     50: struct Pos {
1.73      paf        51:        int line;
                     52:        int col;
                     53:        Pos(int aline, int acol): line(aline), col(acol) {}
1.68      paf        54:        Pos(): line(0), col(0) {}
                     55: 
                     56:        void clear() { line=col=0; }
                     57:        operator bool() { return col!=0; }
                     58: };
                     59: 
1.38      paf        60: /// compiler status
1.68      paf        61: class Parse_control {
                     62:        const String* main_alias;
                     63:        uint last_line_end_col;
                     64: public:
                     65:        const String& alias_method(const String& name);
1.38      paf        66:        //@{
                     67:        /// @name input
1.68      paf        68:        Request& request;
                     69:        VStateless_class* cclass;
1.79      misha      70:        VStateless_class* cclass_new;
1.78      misha      71:        ArrayClass* cclasses;
1.68      paf        72:        const char* source;
                     73:        uint file_no;
                     74:        Pos pos;
1.38      paf        75:        //@}
                     76:        //@{
                     77:        /// @name state; initially
1.45      parser     78:        bool trim_bof;
1.38      paf        79:        int pending_state; ///< i=0
1.69      paf        80:        String::Body string; ///< lexical string accumulator
1.68      paf        81:        Pos string_start;
1.1       paf        82:        
                     83: #define MAX_LEXICAL_STATES 100
1.38      paf        84:        enum lexical_state ls; ///< =LS_USER;
1.54      paf        85:        int ls_sp; ///< =0
                     86:        enum lexical_state ls_stack[MAX_LEXICAL_STATES];
1.38      paf        87:        int brackets_nestages[MAX_LEXICAL_STATES]; ///< brackets nestage on each state
1.46      parser     88: 
1.56      paf        89:        bool in_call_value;
1.74      paf        90:        bool explicit_result;
1.79      misha      91:        bool append;
1.38      paf        92:        //@}
1.1       paf        93:        
1.36      paf        94:        /// output: filled input 'methods' and 'error' if any
1.5       paf        95:        char error[MAX_STRING];
1.68      paf        96: 
                     97:        Parse_control(Request& arequest, 
                     98:                VStateless_class* aclass,
                     99:                const char* asource, const String* amain_alias, 
1.73      paf       100:                uint afile_no,
                    101:                int line_no_offset):
1.70      paf       102:                main_alias(amain_alias),
                    103:                last_line_end_col(0),
                    104: 
1.68      paf       105:                request(arequest), // input 
                    106: 
                    107:                // we were told the class to compile to?
                    108:                cclass(aclass), // until changed with @CLASS would consider operators loading
1.79      misha     109:                cclass_new(0)1.78      misha     110:                cclasses(new ArrayClass(1)),
1.70      paf       111:                source(asource), 
1.68      paf       112:                file_no(afile_no),
1.73      paf       113:                pos(line_no_offset, 0),
1.68      paf       114: 
                    115:                // initialize state
                    116:                trim_bof(true),
                    117:                pending_state(0),
                    118:                ls(LS_USER),
                    119:                ls_sp(0),
1.74      paf       120:                in_call_value(false),
1.79      misha     121:                explicit_result(false),
1.83      misha     122:                append(false) {
1.78      misha     123: 
                    124:                *cclasses+=aclass;
                    125:        }
1.68      paf       126: 
1.104     moko      127:        /// true if exception should be rised
                    128:        bool class_add(){
1.79      misha     129:                if(cclass_new){
                    130:                        cclass=cclass_new;
                    131:                        *cclasses+=cclass;
                    132:                        cclass_new=0;
                    133:                        append=false;
1.104     moko      134:                        // append to request's classes
1.105     moko      135:                        if(!request.allow_class_replace)
                    136:                                return request.classes().put_dont_replace(cclass->name(), cclass) != 0;
                    137:                        request.classes().put(cclass->name(), cclass);
1.79      misha     138:                }
1.104     moko      139:                return false;
1.79      misha     140:        }
                    141: 
1.81      misha     142:        VStateless_class* get_existed_class(VStateless_class* aclass){
1.101     misha     143:                // checking existence of the class during processing @OPTIONS\npartial
                    144:                // method should't use get_class because the last one will call operator @autouse[] if the class wasn't loaded
1.99      misha     145:                if(aclass)
1.100     misha     146:                        if(Value* class_value=request.classes().get(aclass->name()))
1.81      misha     147:                                return class_value->get_class();
                    148:                return 0;
                    149:        }
                    150: 
                    151:        bool reuse_existed_class(VStateless_class* aclass){
                    152:                if(aclass->is_partial()){
                    153:                        cclass=aclass;
                    154:                        cclass_new=0;
                    155:                        append=true;
                    156:                        return true;
                    157:                } else {
                    158:                        return false;
                    159:                }
1.79      misha     160:        }
                    161: 
                    162:        void set_all_vars_local(){
1.102     misha     163:                (cclass_new ? cclass_new : cclass)->set_all_vars_local();
                    164:        }
                    165: 
                    166:        void set_methods_call_type(Method::Call_type call_type){
                    167:                (cclass_new ? cclass_new : cclass)->set_methods_call_type(call_type);
                    168:        }
                    169: 
                    170:        Method::Call_type get_methods_call_type(){
                    171:                return (cclass_new ? cclass_new : cclass)->get_methods_call_type();
1.79      misha     172:        }
                    173: 
1.68      paf       174:        void pos_next_line() {
                    175:                pos.line++;
                    176:                last_line_end_col=pos.col;
                    177:                pos.col=0;
                    178:        }
1.77      paf       179:        void pos_next_c(int c) {
1.68      paf       180:                if(c=='\t')
                    181:                        pos.col=(pos.col+TAB_SIZE)&~(TAB_SIZE-1);
                    182:                else
                    183:                        pos.col++;
                    184:        }
                    185:        /// not precise in case of \t in the middle of the text
                    186:        void pos_prev_c() {
                    187:                if(pos.col==0) {
                    188:                        --pos.line;  pos.col=last_line_end_col;
                    189:                } else
                    190:                        --pos.col;
                    191:        }
                    192:        void ungetc() {
                    193:                source--;
                    194:                pos_prev_c();
                    195:        }
1.1       paf       196: };
                    197: 
1.36      paf       198: /// New array // return empty array
1.68      paf       199: inline ArrayOperation* N() {
                    200:        return new ArrayOperation;
1.3       paf       201: }
                    202: 
1.36      paf       203: /// Assembler instruction // append ordinary instruction to ops
1.80      misha     204: inline void O(ArrayOperation& result, OP::OPCODE code) {
1.68      paf       205:        result+=Operation(code);
1.3       paf       206: }
                    207: 
1.36      paf       208: /// aPpend 'code_array' to 'result'
1.68      paf       209: inline void P(ArrayOperation& result, ArrayOperation& code_array) {
                    210:        result.append(code_array);
1.11      paf       211: }
1.36      paf       212: /// aPpend part of 'code_array', starting from offset, to 'result'
1.68      paf       213: inline void P(ArrayOperation& result, ArrayOperation& code_array, int offset) {
                    214:        result.append(code_array, offset);
1.3       paf       215: }
1.53      paf       216: 
1.84      misha     217: /// aPpend part of 'code_array', starting from offset, to 'result'
                    218: inline void P(ArrayOperation& result, ArrayOperation& code_array, int offset, int limit) {
                    219:        result.append(code_array, offset, limit);
                    220: }
                    221: 
1.68      paf       222: /// append cOde Array
1.97      misha     223: inline void OA(ArrayOperation& result, ArrayOperation* code_array) {
                    224:        result+=Operation(code_array); // append 'code_array'
                    225: }
                    226: 
1.80      misha     227: inline void OA(ArrayOperation& result, OP::OPCODE code, ArrayOperation* code_array) {
1.68      paf       228:        result+=Operation(code); // append OP_CODE
                    229:        result+=Operation(code_array); // append 'code_array'
1.61      paf       230: }
1.1       paf       231: 
1.36      paf       232: /**
                    233:        Value Literal // returns array with 
1.68      paf       234:        - first op: OP_VALUE instruction
                    235:        - second op: origin (debug information)
                    236:        - third op: string itself
1.36      paf       237: */
1.68      paf       238: inline ArrayOperation* VL(Value* value, uint file_no, uint line, uint col) {
1.61      paf       239:        // empty ops array
1.68      paf       240:        ArrayOperation& result=*N();
1.61      paf       241: 
                    242:        // append 'value' to 'result'
1.80      misha     243:        result+=Operation(OP::OP_VALUE);
1.68      paf       244:        result+=Operation(file_no, line, col); // append origin
                    245:        result+=Operation(value); // append 'value'
                    246: 
                    247:        return &result;
1.61      paf       248: }
                    249: 
1.68      paf       250: /// Literal Array to(2) Value @return Value from literal Array OP+origin+Value
1.85      misha     251: Value* LA2V(ArrayOperation& literal_string_array, int offset=0, OP::OPCODE code=OP::OP_VALUE);
1.102     misha     252: 
1.68      paf       253: /// Literal Array to(2) String  @return String value from literal Array OP+origin+String array
1.85      misha     254: inline const String* LA2S(ArrayOperation& literal_string_array, int offset=0, OP::OPCODE code=OP::OP_VALUE) {
                    255:        if(Value* value=LA2V(literal_string_array, offset, code))
1.37      paf       256:                return value->get_string();
                    257:        return 0;
1.36      paf       258: }
1.61      paf       259: 
1.68      paf       260: inline void change_string_literal_to_write_string_literal(ArrayOperation& literal_string_array) {
1.80      misha     261:        literal_string_array.put(0, OP::OP_STRING__WRITE);
1.61      paf       262: }
                    263: 
1.89      misha     264: void maybe_change_string_literal_to_double_literal(ArrayOperation& literal_string_array);
                    265: 
                    266: void change_string_literal_value(ArrayOperation& literal_string_array, const String& new_value);
                    267: 
1.106     misha     268: inline bool change(ArrayOperation& opcodes, int pos, OP::OPCODE find, OP::OPCODE replace) {
                    269:        if(pos>=0) {
                    270:                Operation& op=opcodes.get_ref(pos);
                    271:                if(op.code==find) {
                    272:                        op.code=replace;
                    273:                        return true;
                    274:                }
                    275:        }
                    276:        return false;
                    277: }
1.89      misha     278: 
1.106     misha     279: inline void change_or_append(ArrayOperation& opcodes, int pos, OP::OPCODE find, OP::OPCODE replace, OP::OPCODE notfound) {
                    280:        if(change(opcodes, pos, find, replace))
                    281:                return;
1.86      misha     282: 
1.106     misha     283:        opcodes+=Operation(notfound);
1.108   ! moko      284: }
1.106     misha     285: 
                    286: bool change_first(ArrayOperation& opcodes, OP::OPCODE find, OP::OPCODE replace);
1.86      misha     287: 
1.92      misha     288: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
1.93      misha     289: // OP_VALUE+origin+value+OP_GET_ELEMENT+OP_VALUE+origin+value+OP_GET_ELEMENT => OP_GET_OBJECT_ELEMENT+origin+value+origin+value
1.89      misha     290: inline bool maybe_make_get_object_element(ArrayOperation& opcodes, ArrayOperation& diving_code, size_t divine_count){
1.95      misha     291:        if(divine_count<8)
1.89      misha     292:                return false;
                    293: 
                    294:        assert(diving_code[0].code==OP::OP_VALUE);
                    295:        if(
1.94      misha     296:                diving_code[3].code==OP::OP_GET_ELEMENT
1.93      misha     297:                && diving_code[4].code==OP::OP_VALUE
1.95      misha     298:                && diving_code[7].code==OP::OP_GET_ELEMENT
1.89      misha     299:        ){
                    300:                O(opcodes, OP::OP_GET_OBJECT_ELEMENT);
1.94      misha     301:                P(opcodes, diving_code, 1/*offset*/, 2/*limit*/); // copy first origin+value
1.93      misha     302:                P(opcodes, diving_code, 5, 2); // second origin+value
1.95      misha     303:                if(divine_count>8)
                    304:                        P(opcodes, diving_code, 8/*offset*/); // tail
1.89      misha     305:                return true;
                    306:        }
                    307:        return false;
                    308: }
1.92      misha     309: #endif
1.88      misha     310: 
1.94      misha     311: 
1.92      misha     312: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
1.93      misha     313: // OP_VALUE+origin+value+OP_GET_ELEMENT+OP_WITH_READ+OP_VALUE+origin+value+OP_GET_ELEMENT+OP_GET_ELEMENT => OP_GET_OBJECT_VAR_ELEMENT+origin+value+origin+value
1.89      misha     314: inline bool maybe_make_get_object_var_element(ArrayOperation& opcodes, ArrayOperation& diving_code, size_t divine_count){
1.98      misha     315:        if(divine_count!=10)
1.89      misha     316:                return false;
                    317: 
                    318:        assert(diving_code[0].code==OP::OP_VALUE);
                    319:        if(
1.95      misha     320:                diving_code[3].code==OP::OP_GET_ELEMENT
                    321:                && diving_code[4].code==OP::OP_WITH_READ
1.89      misha     322:                && diving_code[5].code==OP::OP_VALUE
1.95      misha     323:                && diving_code[8].code==OP::OP_GET_ELEMENT
                    324:                && diving_code[9].code==OP::OP_GET_ELEMENT
1.89      misha     325:        ){
                    326:                O(opcodes, OP::OP_GET_OBJECT_VAR_ELEMENT);
1.94      misha     327:                P(opcodes, diving_code, 1/*offset*/, 2/*limit*/); // copy first origin+value
1.93      misha     328:                P(opcodes, diving_code, 6, 2); // second origin+value
1.89      misha     329:                return true;
                    330:        }
                    331:        return false;
                    332: }
1.92      misha     333: #endif
1.68      paf       334: 
1.94      misha     335: 
1.96      misha     336: bool maybe_make_self(ArrayOperation& opcodes, ArrayOperation& diving_code, size_t divine_count);
1.94      misha     337: 
1.106     misha     338: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL
                    339: bool maybe_append_simple_diving_code(ArrayOperation& code, ArrayOperation& diving_code);
                    340: 
                    341: bool is_special_element(ArrayOperation& opcodes);
                    342: #endif
1.94      misha     343: 
1.91      misha     344: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
1.93      misha     345: inline bool maybe_optimize_construct(ArrayOperation& opcodes, ArrayOperation& var_ops, ArrayOperation& expr_ops){
                    346:        size_t expr_count=expr_ops.count();
                    347:        OP::OPCODE construct_op=expr_ops[expr_count-1].code;
                    348:        size_t construct=(construct_op==OP::OP_CONSTRUCT_VALUE)?0x01:(construct_op==OP::OP_CONSTRUCT_EXPR)?0x02:0x00;
                    349:        if(construct){
                    350:                P(opcodes, expr_ops, 0/*offset*/, expr_count-1/*limit*/); // copy constructor body without CONSTRUCT_(VALUE|EXPR)
                    351: 
1.94      misha     352:                size_t with=0x00;
                    353:                switch(var_ops[0].code){
                    354:                        case OP::OP_WITH_ROOT:
                    355:                                {
                    356:                                        with=0x10;
                    357:                                        break;
                    358:                                }
                    359:                        case OP::OP_WITH_WRITE:
                    360:                                {
                    361:                                        with=0x20;
                    362:                                        break;
                    363:                                }
                    364:                        case OP::OP_WITH_SELF:
                    365:                                {
                    366:                                        with=0x30;
                    367:                                        break;
                    368:                                }
1.107     moko      369:                        default: break;
1.94      misha     370:                }
1.93      misha     371: 
                    372:                if(with && var_ops[1].code==OP::OP_VALUE && var_ops.count()==4){
                    373:                        OP::OPCODE code=OP::OP_VALUE; // calm down compiler. will be reassigned for sure.
                    374:                        switch( with | construct ) {
                    375:                                case 0x11:
1.92      misha     376:                                        {
1.93      misha     377:                                                code=OP::OP_WITH_ROOT__VALUE__CONSTRUCT_VALUE;
1.92      misha     378:                                                break;
                    379:                                        }
1.93      misha     380:                                case 0x12:
1.92      misha     381:                                        {
1.93      misha     382:                                                code=OP::OP_WITH_ROOT__VALUE__CONSTRUCT_EXPR;
1.92      misha     383:                                                break;
                    384:                                        }
1.93      misha     385:                                case 0x21:
1.92      misha     386:                                        {
1.93      misha     387:                                                code=OP::OP_WITH_WRITE__VALUE__CONSTRUCT_VALUE;
1.92      misha     388:                                                break;
                    389:                                        }
1.93      misha     390:                                case 0x22:
1.92      misha     391:                                        {
1.93      misha     392:                                                code=OP::OP_WITH_WRITE__VALUE__CONSTRUCT_EXPR;
1.92      misha     393:                                                break;
                    394:                                        }
1.94      misha     395:                                case 0x31:
                    396:                                        {
                    397:                                                code=OP::OP_WITH_SELF__VALUE__CONSTRUCT_VALUE;
                    398:                                                break;
                    399:                                        }
                    400:                                case 0x32:
                    401:                                        {
                    402:                                                code=OP::OP_WITH_SELF__VALUE__CONSTRUCT_EXPR;
                    403:                                                break;
                    404:                                        }
1.93      misha     405:                        }
                    406:                        O(opcodes, code);
                    407:                        P(opcodes, var_ops, 2/*offset*/, 2/*limit*/); // copy origin+value
                    408:                } else {
                    409:                        P(opcodes, var_ops);
                    410:                        O(opcodes, construct_op);
1.89      misha     411:                }
1.93      misha     412:                return true;
1.89      misha     413:        }
                    414:        return false;
                    415: }
1.92      misha     416: #endif
1.61      paf       417: 
1.102     misha     418: Method::Call_type GetMethodCallType(Parse_control& pc, ArrayOperation& literal_string_array);
1.1       paf       419: 
1.68      paf       420: void push_LS(Parse_control& pc, lexical_state new_state);
                    421: void pop_LS(Parse_control& pc);
1.1       paf       422: 
                    423: #endif

E-mail: