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

1.32      paf         1: /** @file
1.33      paf         2:        Parser: compiler support helper functions decls.
                      3: 
1.117     moko        4:        Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com)
                      5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.1       paf         6: */
                      7: 
                      8: #ifndef COMPILE_TOOLS
                      9: #define COMPILE_TOOLS
1.62      paf        10: 
1.120   ! moko       11: #define IDENT_COMPILE_TOOLS_H "$Id: compile_tools.h,v 1.119 2024/09/17 18:09:59 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.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.120   ! moko       87:        bool array;
1.38      paf        88:        //@}
1.1       paf        89:        
1.36      paf        90:        /// output: filled input 'methods' and 'error' if any
1.116     moko       91:        const char *error;
1.68      paf        92: 
                     93:        Parse_control(Request& arequest, 
                     94:                VStateless_class* aclass,
                     95:                const char* asource, const String* amain_alias, 
1.73      paf        96:                uint afile_no,
                     97:                int line_no_offset):
1.70      paf        98:                main_alias(amain_alias),
                     99:                last_line_end_col(0),
                    100: 
1.68      paf       101:                request(arequest), // input 
                    102: 
                    103:                // we were told the class to compile to?
                    104:                cclass(aclass), // until changed with @CLASS would consider operators loading
1.79      misha     105:                cclass_new(0)1.78      misha     106:                cclasses(new ArrayClass(1)),
1.70      paf       107:                source(asource), 
1.68      paf       108:                file_no(afile_no),
1.73      paf       109:                pos(line_no_offset, 0),
1.68      paf       110: 
                    111:                // initialize state
                    112:                trim_bof(true),
                    113:                pending_state(0),
                    114:                ls(LS_USER),
                    115:                ls_sp(0),
1.74      paf       116:                in_call_value(false),
1.79      misha     117:                explicit_result(false),
1.116     moko      118:                append(false),
1.120   ! moko      119:                array(false),
1.116     moko      120:                error("") {
1.78      misha     121: 
                    122:                *cclasses+=aclass;
                    123:        }
1.68      paf       124: 
1.118     moko      125:        /// false if exception should be rised
1.104     moko      126:        bool class_add(){
1.79      misha     127:                if(cclass_new){
                    128:                        cclass=cclass_new;
                    129:                        *cclasses+=cclass;
                    130:                        cclass_new=0;
                    131:                        append=false;
1.104     moko      132:                        // append to request's classes
1.119     moko      133:                        return request.add_class(cclass->type(), cclass);
1.79      misha     134:                }
1.118     moko      135:                return true;
1.79      misha     136:        }
                    137: 
1.81      misha     138:        VStateless_class* get_existed_class(VStateless_class* aclass){
1.101     misha     139:                // checking existence of the class during processing @OPTIONS\npartial
1.112     moko      140:                // can't use get_class because it will call @autouse[] if the class wasn't loaded
1.99      misha     141:                if(aclass)
1.112     moko      142:                        return request.classes().get(aclass->type());
1.81      misha     143:                return 0;
                    144:        }
                    145: 
                    146:        bool reuse_existed_class(VStateless_class* aclass){
                    147:                if(aclass->is_partial()){
                    148:                        cclass=aclass;
                    149:                        cclass_new=0;
                    150:                        append=true;
                    151:                        return true;
                    152:                } else {
                    153:                        return false;
                    154:                }
1.79      misha     155:        }
                    156: 
                    157:        void set_all_vars_local(){
1.102     misha     158:                (cclass_new ? cclass_new : cclass)->set_all_vars_local();
                    159:        }
                    160: 
                    161:        void set_methods_call_type(Method::Call_type call_type){
                    162:                (cclass_new ? cclass_new : cclass)->set_methods_call_type(call_type);
                    163:        }
                    164: 
                    165:        Method::Call_type get_methods_call_type(){
                    166:                return (cclass_new ? cclass_new : cclass)->get_methods_call_type();
1.79      misha     167:        }
                    168: 
1.68      paf       169:        void pos_next_line() {
                    170:                pos.line++;
                    171:                last_line_end_col=pos.col;
                    172:                pos.col=0;
                    173:        }
1.77      paf       174:        void pos_next_c(int c) {
1.68      paf       175:                if(c=='\t')
                    176:                        pos.col=(pos.col+TAB_SIZE)&~(TAB_SIZE-1);
                    177:                else
                    178:                        pos.col++;
                    179:        }
                    180:        /// not precise in case of \t in the middle of the text
                    181:        void pos_prev_c() {
                    182:                if(pos.col==0) {
                    183:                        --pos.line;  pos.col=last_line_end_col;
                    184:                } else
                    185:                        --pos.col;
                    186:        }
                    187:        void ungetc() {
                    188:                source--;
                    189:                pos_prev_c();
                    190:        }
1.1       paf       191: };
                    192: 
1.36      paf       193: /// New array // return empty array
1.68      paf       194: inline ArrayOperation* N() {
                    195:        return new ArrayOperation;
1.3       paf       196: }
                    197: 
1.36      paf       198: /// Assembler instruction // append ordinary instruction to ops
1.80      misha     199: inline void O(ArrayOperation& result, OP::OPCODE code) {
1.68      paf       200:        result+=Operation(code);
1.3       paf       201: }
                    202: 
1.36      paf       203: /// aPpend 'code_array' to 'result'
1.68      paf       204: inline void P(ArrayOperation& result, ArrayOperation& code_array) {
                    205:        result.append(code_array);
1.11      paf       206: }
1.36      paf       207: /// aPpend part of 'code_array', starting from offset, to 'result'
1.68      paf       208: inline void P(ArrayOperation& result, ArrayOperation& code_array, int offset) {
                    209:        result.append(code_array, offset);
1.3       paf       210: }
1.53      paf       211: 
1.84      misha     212: /// aPpend part of 'code_array', starting from offset, to 'result'
                    213: inline void P(ArrayOperation& result, ArrayOperation& code_array, int offset, int limit) {
                    214:        result.append(code_array, offset, limit);
                    215: }
                    216: 
1.68      paf       217: /// append cOde Array
1.97      misha     218: inline void OA(ArrayOperation& result, ArrayOperation* code_array) {
                    219:        result+=Operation(code_array); // append 'code_array'
                    220: }
                    221: 
1.80      misha     222: inline void OA(ArrayOperation& result, OP::OPCODE code, ArrayOperation* code_array) {
1.68      paf       223:        result+=Operation(code); // append OP_CODE
                    224:        result+=Operation(code_array); // append 'code_array'
1.61      paf       225: }
1.1       paf       226: 
1.36      paf       227: /**
                    228:        Value Literal // returns array with 
1.68      paf       229:        - first op: OP_VALUE instruction
                    230:        - second op: origin (debug information)
                    231:        - third op: string itself
1.36      paf       232: */
1.68      paf       233: inline ArrayOperation* VL(Value* value, uint file_no, uint line, uint col) {
1.61      paf       234:        // empty ops array
1.68      paf       235:        ArrayOperation& result=*N();
1.61      paf       236: 
                    237:        // append 'value' to 'result'
1.80      misha     238:        result+=Operation(OP::OP_VALUE);
1.68      paf       239:        result+=Operation(file_no, line, col); // append origin
                    240:        result+=Operation(value); // append 'value'
                    241: 
                    242:        return &result;
1.61      paf       243: }
                    244: 
1.68      paf       245: /// Literal Array to(2) Value @return Value from literal Array OP+origin+Value
1.85      misha     246: Value* LA2V(ArrayOperation& literal_string_array, int offset=0, OP::OPCODE code=OP::OP_VALUE);
1.102     misha     247: 
1.68      paf       248: /// Literal Array to(2) String  @return String value from literal Array OP+origin+String array
1.85      misha     249: inline const String* LA2S(ArrayOperation& literal_string_array, int offset=0, OP::OPCODE code=OP::OP_VALUE) {
                    250:        if(Value* value=LA2V(literal_string_array, offset, code))
1.37      paf       251:                return value->get_string();
                    252:        return 0;
1.36      paf       253: }
1.61      paf       254: 
1.68      paf       255: inline void change_string_literal_to_write_string_literal(ArrayOperation& literal_string_array) {
1.80      misha     256:        literal_string_array.put(0, OP::OP_STRING__WRITE);
1.61      paf       257: }
                    258: 
1.89      misha     259: void maybe_change_string_literal_to_double_literal(ArrayOperation& literal_string_array);
                    260: 
                    261: void change_string_literal_value(ArrayOperation& literal_string_array, const String& new_value);
                    262: 
1.106     misha     263: inline bool change(ArrayOperation& opcodes, int pos, OP::OPCODE find, OP::OPCODE replace) {
                    264:        if(pos>=0) {
                    265:                Operation& op=opcodes.get_ref(pos);
                    266:                if(op.code==find) {
                    267:                        op.code=replace;
                    268:                        return true;
                    269:                }
                    270:        }
                    271:        return false;
                    272: }
1.89      misha     273: 
1.106     misha     274: inline void change_or_append(ArrayOperation& opcodes, int pos, OP::OPCODE find, OP::OPCODE replace, OP::OPCODE notfound) {
                    275:        if(change(opcodes, pos, find, replace))
                    276:                return;
1.86      misha     277: 
1.106     misha     278:        opcodes+=Operation(notfound);
1.108     moko      279: }
1.106     misha     280: 
                    281: bool change_first(ArrayOperation& opcodes, OP::OPCODE find, OP::OPCODE replace);
1.86      misha     282: 
1.92      misha     283: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
1.93      misha     284: // OP_VALUE+origin+value+OP_GET_ELEMENT+OP_VALUE+origin+value+OP_GET_ELEMENT => OP_GET_OBJECT_ELEMENT+origin+value+origin+value
1.114     moko      285: inline bool maybe_make_get_object_element(ArrayOperation& opcodes, ArrayOperation& diving_code, size_t diving_count){
1.89      misha     286:        if(
1.114     moko      287:                diving_count>=8
                    288:                && diving_code[0].code==OP::OP_VALUE
                    289:                && diving_code[3].code==OP::OP_GET_ELEMENT
1.93      misha     290:                && diving_code[4].code==OP::OP_VALUE
1.95      misha     291:                && diving_code[7].code==OP::OP_GET_ELEMENT
1.89      misha     292:        ){
                    293:                O(opcodes, OP::OP_GET_OBJECT_ELEMENT);
1.114     moko      294:                P(opcodes, diving_code, 1 /*offset*/, 2 /*limit*/); // copy first origin+value
1.93      misha     295:                P(opcodes, diving_code, 5, 2); // second origin+value
1.114     moko      296:                if(diving_count>8)
                    297:                        P(opcodes, diving_code, 8 /*offset*/); // tail
1.89      misha     298:                return true;
                    299:        }
                    300:        return false;
                    301: }
1.92      misha     302: #endif
1.88      misha     303: 
1.94      misha     304: 
1.92      misha     305: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
1.93      misha     306: // 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.114     moko      307: inline bool maybe_make_get_object_var_element(ArrayOperation& opcodes, ArrayOperation& diving_code, size_t diving_count){
1.89      misha     308:        if(
1.114     moko      309:                diving_count==10
                    310:                && diving_code[0].code==OP::OP_VALUE
                    311:                && diving_code[3].code==OP::OP_GET_ELEMENT
1.95      misha     312:                && diving_code[4].code==OP::OP_WITH_READ
1.89      misha     313:                && diving_code[5].code==OP::OP_VALUE
1.95      misha     314:                && diving_code[8].code==OP::OP_GET_ELEMENT
                    315:                && diving_code[9].code==OP::OP_GET_ELEMENT
1.89      misha     316:        ){
                    317:                O(opcodes, OP::OP_GET_OBJECT_VAR_ELEMENT);
1.114     moko      318:                P(opcodes, diving_code, 1 /*offset*/, 2 /*limit*/); // copy first origin+value
1.93      misha     319:                P(opcodes, diving_code, 6, 2); // second origin+value
1.89      misha     320:                return true;
                    321:        }
                    322:        return false;
                    323: }
1.92      misha     324: #endif
1.68      paf       325: 
1.94      misha     326: 
1.114     moko      327: bool maybe_make_self(ArrayOperation& opcodes, ArrayOperation& diving_code, size_t diving_count);
1.94      misha     328: 
1.106     misha     329: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL
                    330: bool maybe_append_simple_diving_code(ArrayOperation& code, ArrayOperation& diving_code);
                    331: 
                    332: bool is_special_element(ArrayOperation& opcodes);
                    333: #endif
1.94      misha     334: 
1.91      misha     335: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
1.93      misha     336: inline bool maybe_optimize_construct(ArrayOperation& opcodes, ArrayOperation& var_ops, ArrayOperation& expr_ops){
                    337:        size_t expr_count=expr_ops.count();
                    338:        OP::OPCODE construct_op=expr_ops[expr_count-1].code;
                    339:        size_t construct=(construct_op==OP::OP_CONSTRUCT_VALUE)?0x01:(construct_op==OP::OP_CONSTRUCT_EXPR)?0x02:0x00;
                    340:        if(construct){
                    341:                P(opcodes, expr_ops, 0/*offset*/, expr_count-1/*limit*/); // copy constructor body without CONSTRUCT_(VALUE|EXPR)
                    342: 
1.94      misha     343:                size_t with=0x00;
                    344:                switch(var_ops[0].code){
                    345:                        case OP::OP_WITH_ROOT:
                    346:                                {
                    347:                                        with=0x10;
                    348:                                        break;
                    349:                                }
                    350:                        case OP::OP_WITH_WRITE:
                    351:                                {
                    352:                                        with=0x20;
                    353:                                        break;
                    354:                                }
                    355:                        case OP::OP_WITH_SELF:
                    356:                                {
                    357:                                        with=0x30;
                    358:                                        break;
                    359:                                }
1.107     moko      360:                        default: break;
1.94      misha     361:                }
1.93      misha     362: 
                    363:                if(with && var_ops[1].code==OP::OP_VALUE && var_ops.count()==4){
                    364:                        OP::OPCODE code=OP::OP_VALUE; // calm down compiler. will be reassigned for sure.
                    365:                        switch( with | construct ) {
                    366:                                case 0x11:
1.92      misha     367:                                        {
1.93      misha     368:                                                code=OP::OP_WITH_ROOT__VALUE__CONSTRUCT_VALUE;
1.92      misha     369:                                                break;
                    370:                                        }
1.93      misha     371:                                case 0x12:
1.92      misha     372:                                        {
1.93      misha     373:                                                code=OP::OP_WITH_ROOT__VALUE__CONSTRUCT_EXPR;
1.92      misha     374:                                                break;
                    375:                                        }
1.93      misha     376:                                case 0x21:
1.92      misha     377:                                        {
1.93      misha     378:                                                code=OP::OP_WITH_WRITE__VALUE__CONSTRUCT_VALUE;
1.92      misha     379:                                                break;
                    380:                                        }
1.93      misha     381:                                case 0x22:
1.92      misha     382:                                        {
1.93      misha     383:                                                code=OP::OP_WITH_WRITE__VALUE__CONSTRUCT_EXPR;
1.92      misha     384:                                                break;
                    385:                                        }
1.94      misha     386:                                case 0x31:
                    387:                                        {
                    388:                                                code=OP::OP_WITH_SELF__VALUE__CONSTRUCT_VALUE;
                    389:                                                break;
                    390:                                        }
                    391:                                case 0x32:
                    392:                                        {
                    393:                                                code=OP::OP_WITH_SELF__VALUE__CONSTRUCT_EXPR;
                    394:                                                break;
                    395:                                        }
1.93      misha     396:                        }
                    397:                        O(opcodes, code);
                    398:                        P(opcodes, var_ops, 2/*offset*/, 2/*limit*/); // copy origin+value
                    399:                } else {
                    400:                        P(opcodes, var_ops);
                    401:                        O(opcodes, construct_op);
1.89      misha     402:                }
1.93      misha     403:                return true;
1.89      misha     404:        }
                    405:        return false;
                    406: }
1.92      misha     407: #endif
1.61      paf       408: 
1.102     misha     409: Method::Call_type GetMethodCallType(Parse_control& pc, ArrayOperation& literal_string_array);
1.1       paf       410: 
1.68      paf       411: void push_LS(Parse_control& pc, lexical_state new_state);
                    412: void pop_LS(Parse_control& pc);
1.1       paf       413: 
                    414: #endif

E-mail: