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

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

E-mail: