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