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

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

E-mail: