Annotation of parser3/src/main/compile_tools.h, revision 1.102
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.102 ! misha 11: static const char * const IDENT_COMPILE_TOOLS_H="$Date: 2009-08-27 10:18:58 $";
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.79 misha 127: void class_add(){
128: if(cclass_new){
129: cclass=cclass_new;
130: // append to request's classes
131: request.classes().put(cclass->name(), cclass);
132: *cclasses+=cclass;
133: cclass_new=0;
134: append=false;
135: }
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
140: // method should't use get_class because the last one will call operator @autouse[] if the class wasn't loaded
1.99 misha 141: if(aclass)
1.100 misha 142: if(Value* class_value=request.classes().get(aclass->name()))
1.81 misha 143: return class_value->get_class();
144: return 0;
145: }
146:
147: bool reuse_existed_class(VStateless_class* aclass){
148: if(aclass->is_partial()){
149: cclass=aclass;
150: cclass_new=0;
151: append=true;
152: return true;
153: } else {
154: return false;
155: }
1.79 misha 156: }
157:
158: void set_all_vars_local(){
1.102 ! misha 159: (cclass_new ? cclass_new : cclass)->set_all_vars_local();
! 160: }
! 161:
! 162: void set_methods_call_type(Method::Call_type call_type){
! 163: (cclass_new ? cclass_new : cclass)->set_methods_call_type(call_type);
! 164: }
! 165:
! 166: Method::Call_type get_methods_call_type(){
! 167: return (cclass_new ? cclass_new : cclass)->get_methods_call_type();
1.79 misha 168: }
169:
1.68 paf 170: void pos_next_line() {
171: pos.line++;
172: last_line_end_col=pos.col;
173: pos.col=0;
174: }
1.77 paf 175: void pos_next_c(int c) {
1.68 paf 176: if(c=='\t')
177: pos.col=(pos.col+TAB_SIZE)&~(TAB_SIZE-1);
178: else
179: pos.col++;
180: }
181: /// not precise in case of \t in the middle of the text
182: void pos_prev_c() {
183: if(pos.col==0) {
184: --pos.line; pos.col=last_line_end_col;
185: } else
186: --pos.col;
187: }
188: void ungetc() {
189: source--;
190: pos_prev_c();
191: }
1.1 paf 192: };
193:
1.36 paf 194: /// New array // return empty array
1.68 paf 195: inline ArrayOperation* N() {
196: return new ArrayOperation;
1.3 paf 197: }
198:
1.36 paf 199: /// Assembler instruction // append ordinary instruction to ops
1.80 misha 200: inline void O(ArrayOperation& result, OP::OPCODE code) {
1.68 paf 201: result+=Operation(code);
1.3 paf 202: }
203:
1.36 paf 204: /// aPpend 'code_array' to 'result'
1.68 paf 205: inline void P(ArrayOperation& result, ArrayOperation& code_array) {
206: result.append(code_array);
1.11 paf 207: }
1.36 paf 208: /// aPpend part of 'code_array', starting from offset, to 'result'
1.68 paf 209: inline void P(ArrayOperation& result, ArrayOperation& code_array, int offset) {
210: result.append(code_array, offset);
1.3 paf 211: }
1.53 paf 212:
1.84 misha 213: /// aPpend part of 'code_array', starting from offset, to 'result'
214: inline void P(ArrayOperation& result, ArrayOperation& code_array, int offset, int limit) {
215: result.append(code_array, offset, limit);
216: }
217:
1.68 paf 218: /// append cOde Array
1.97 misha 219: inline void OA(ArrayOperation& result, ArrayOperation* code_array) {
220: result+=Operation(code_array); // append 'code_array'
221: }
222:
1.80 misha 223: inline void OA(ArrayOperation& result, OP::OPCODE code, ArrayOperation* code_array) {
1.68 paf 224: result+=Operation(code); // append OP_CODE
225: result+=Operation(code_array); // append 'code_array'
1.61 paf 226: }
1.1 paf 227:
1.36 paf 228: /**
229: Value Literal // returns array with
1.68 paf 230: - first op: OP_VALUE instruction
231: - second op: origin (debug information)
232: - third op: string itself
1.36 paf 233: */
1.68 paf 234: inline ArrayOperation* VL(Value* value, uint file_no, uint line, uint col) {
1.61 paf 235: // empty ops array
1.68 paf 236: ArrayOperation& result=*N();
1.61 paf 237:
238: // append 'value' to 'result'
1.80 misha 239: result+=Operation(OP::OP_VALUE);
1.68 paf 240: result+=Operation(file_no, line, col); // append origin
241: result+=Operation(value); // append 'value'
242:
243: return &result;
1.61 paf 244: }
245:
1.68 paf 246: /// Literal Array to(2) Value @return Value from literal Array OP+origin+Value
1.85 misha 247: Value* LA2V(ArrayOperation& literal_string_array, int offset=0, OP::OPCODE code=OP::OP_VALUE);
1.102 ! misha 248:
1.68 paf 249: /// Literal Array to(2) String @return String value from literal Array OP+origin+String array
1.85 misha 250: inline const String* LA2S(ArrayOperation& literal_string_array, int offset=0, OP::OPCODE code=OP::OP_VALUE) {
251: if(Value* value=LA2V(literal_string_array, offset, code))
1.37 paf 252: return value->get_string();
253: return 0;
1.36 paf 254: }
1.61 paf 255:
1.68 paf 256: inline void change_string_literal_to_write_string_literal(ArrayOperation& literal_string_array) {
1.80 misha 257: literal_string_array.put(0, OP::OP_STRING__WRITE);
1.61 paf 258: }
259:
1.89 misha 260: void maybe_change_string_literal_to_double_literal(ArrayOperation& literal_string_array);
261:
262: void change_string_literal_value(ArrayOperation& literal_string_array, const String& new_value);
263:
264: void changetail_or_append(ArrayOperation& opcodes,
265: OP::OPCODE find, bool with_argument, OP::OPCODE replace, OP::OPCODE notfound);
266:
1.94 misha 267: bool maybe_change_first_opcode(ArrayOperation& opcodes, OP::OPCODE find, OP::OPCODE replace);
1.86 misha 268:
269:
1.92 misha 270: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
1.93 misha 271: // 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 272: inline bool maybe_make_get_object_element(ArrayOperation& opcodes, ArrayOperation& diving_code, size_t divine_count){
1.95 misha 273: if(divine_count<8)
1.89 misha 274: return false;
275:
276: assert(diving_code[0].code==OP::OP_VALUE);
277: if(
1.94 misha 278: diving_code[3].code==OP::OP_GET_ELEMENT
1.93 misha 279: && diving_code[4].code==OP::OP_VALUE
1.95 misha 280: && diving_code[7].code==OP::OP_GET_ELEMENT
1.89 misha 281: ){
282: O(opcodes, OP::OP_GET_OBJECT_ELEMENT);
1.94 misha 283: P(opcodes, diving_code, 1/*offset*/, 2/*limit*/); // copy first origin+value
1.93 misha 284: P(opcodes, diving_code, 5, 2); // second origin+value
1.95 misha 285: if(divine_count>8)
286: P(opcodes, diving_code, 8/*offset*/); // tail
1.89 misha 287: return true;
288: }
289: return false;
290: }
1.92 misha 291: #endif
1.88 misha 292:
1.94 misha 293:
1.92 misha 294: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
1.93 misha 295: // 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 296: inline bool maybe_make_get_object_var_element(ArrayOperation& opcodes, ArrayOperation& diving_code, size_t divine_count){
1.98 misha 297: if(divine_count!=10)
1.89 misha 298: return false;
299:
300: assert(diving_code[0].code==OP::OP_VALUE);
301: if(
1.95 misha 302: diving_code[3].code==OP::OP_GET_ELEMENT
303: && diving_code[4].code==OP::OP_WITH_READ
1.89 misha 304: && diving_code[5].code==OP::OP_VALUE
1.95 misha 305: && diving_code[8].code==OP::OP_GET_ELEMENT
306: && diving_code[9].code==OP::OP_GET_ELEMENT
1.89 misha 307: ){
308: O(opcodes, OP::OP_GET_OBJECT_VAR_ELEMENT);
1.94 misha 309: P(opcodes, diving_code, 1/*offset*/, 2/*limit*/); // copy first origin+value
1.93 misha 310: P(opcodes, diving_code, 6, 2); // second origin+value
1.89 misha 311: return true;
312: }
313: return false;
314: }
1.92 misha 315: #endif
1.68 paf 316:
1.94 misha 317:
1.96 misha 318: bool maybe_make_self(ArrayOperation& opcodes, ArrayOperation& diving_code, size_t divine_count);
1.94 misha 319:
320:
1.91 misha 321: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
1.93 misha 322: inline bool maybe_optimize_construct(ArrayOperation& opcodes, ArrayOperation& var_ops, ArrayOperation& expr_ops){
323: size_t expr_count=expr_ops.count();
324: OP::OPCODE construct_op=expr_ops[expr_count-1].code;
325: size_t construct=(construct_op==OP::OP_CONSTRUCT_VALUE)?0x01:(construct_op==OP::OP_CONSTRUCT_EXPR)?0x02:0x00;
326: if(construct){
327: P(opcodes, expr_ops, 0/*offset*/, expr_count-1/*limit*/); // copy constructor body without CONSTRUCT_(VALUE|EXPR)
328:
1.94 misha 329: size_t with=0x00;
330: switch(var_ops[0].code){
331: case OP::OP_WITH_ROOT:
332: {
333: with=0x10;
334: break;
335: }
336: case OP::OP_WITH_WRITE:
337: {
338: with=0x20;
339: break;
340: }
341: case OP::OP_WITH_SELF:
342: {
343: with=0x30;
344: break;
345: }
346: }
1.93 misha 347:
348: if(with && var_ops[1].code==OP::OP_VALUE && var_ops.count()==4){
349: OP::OPCODE code=OP::OP_VALUE; // calm down compiler. will be reassigned for sure.
350: switch( with | construct ) {
351: case 0x11:
1.92 misha 352: {
1.93 misha 353: code=OP::OP_WITH_ROOT__VALUE__CONSTRUCT_VALUE;
1.92 misha 354: break;
355: }
1.93 misha 356: case 0x12:
1.92 misha 357: {
1.93 misha 358: code=OP::OP_WITH_ROOT__VALUE__CONSTRUCT_EXPR;
1.92 misha 359: break;
360: }
1.93 misha 361: case 0x21:
1.92 misha 362: {
1.93 misha 363: code=OP::OP_WITH_WRITE__VALUE__CONSTRUCT_VALUE;
1.92 misha 364: break;
365: }
1.93 misha 366: case 0x22:
1.92 misha 367: {
1.93 misha 368: code=OP::OP_WITH_WRITE__VALUE__CONSTRUCT_EXPR;
1.92 misha 369: break;
370: }
1.94 misha 371: case 0x31:
372: {
373: code=OP::OP_WITH_SELF__VALUE__CONSTRUCT_VALUE;
374: break;
375: }
376: case 0x32:
377: {
378: code=OP::OP_WITH_SELF__VALUE__CONSTRUCT_EXPR;
379: break;
380: }
1.93 misha 381: }
382: O(opcodes, code);
383: P(opcodes, var_ops, 2/*offset*/, 2/*limit*/); // copy origin+value
384: } else {
385: P(opcodes, var_ops);
386: O(opcodes, construct_op);
1.89 misha 387: }
1.93 misha 388: return true;
1.89 misha 389: }
390: return false;
391: }
1.92 misha 392: #endif
1.61 paf 393:
1.102 ! misha 394: Method::Call_type GetMethodCallType(Parse_control& pc, ArrayOperation& literal_string_array);
1.1 paf 395:
1.68 paf 396: void push_LS(Parse_control& pc, lexical_state new_state);
397: void pop_LS(Parse_control& pc);
1.1 paf 398:
399: #endif
E-mail: