Annotation of parser3/src/main/execute.C, revision 1.71
1.1 paf 1: /*
1.71 ! paf 2: $Id: execute.C,v 1.70 2001/03/08 13:13:41 paf Exp $
1.1 paf 3: */
4:
1.8 paf 5: #include "pa_array.h"
1.1 paf 6: #include "code.h"
1.11 paf 7: #include "pa_request.h"
1.15 paf 8: #include "pa_vstring.h"
1.22 paf 9: #include "pa_vhash.h"
1.23 paf 10: #include "pa_vunknown.h"
1.34 paf 11: #include "pa_vcframe.h"
12: #include "pa_vmframe.h"
1.38 paf 13: #include "pa_vobject.h"
1.49 paf 14: #include "pa_vdouble.h"
1.54 paf 15: #include "pa_vbool.h"
1.1 paf 16:
17: #include <stdio.h>
18:
1.24 paf 19: #define PUSH(value) stack.push(value)
20: #define POP() static_cast<Value *>(stack.pop())
1.32 paf 21: #define POP_NAME() static_cast<Value *>(stack.pop())->as_string()
1.24 paf 22:
1.11 paf 23:
1.1 paf 24: char *opcode_name[]={
1.49 paf 25: // literals
1.66 paf 26: "VALUE", "CODE__STORE_PARAM",
1.49 paf 27:
28: // actions
1.51 paf 29: "WITH_SELF", "WITH_ROOT", "WITH_READ", "WITH_WRITE",
1.66 paf 30: "GET_CLASS",
1.1 paf 31: "CONSTRUCT",
1.18 paf 32: "WRITE",
33: "GET_ELEMENT", "GET_ELEMENT__WRITE",
1.1 paf 34: "CREATE_EWPOOL", "REDUCE_EWPOOL",
35: "CREATE_RWPOOL", "REDUCE_RWPOOL",
1.49 paf 36: "CREATE_SWPOOL", "REDUCE_SWPOOL",
1.1 paf 37: "GET_METHOD_FRAME",
38: "STORE_PARAM",
1.51 paf 39: "CALL",
1.49 paf 40:
41: // expression ops: unary
42: "NEG", "INV", "NOT", "DEF", "IN", "FEXISTS",
43: // expression ops: binary
44: "SUB", "ADD", "MUL", "DIV", "MOD",
1.56 paf 45: "BIN_AND", "BIN_OR", "BIN_XOR",
46: "LOG_AND", "LOG_OR", "LOG_XOR",
1.49 paf 47: "NUM_LT", "NUM_GT", "NUM_LE", "NUM_GE", "NUM_EQ", "NUM_NE",
1.55 paf 48: "STR_LT", "STR_GT", "STR_LE", "STR_GE", "STR_EQ", "STR_NE"
1.1 paf 49: };
50:
1.9 paf 51: void dump(int level, const Array& ops) {
1.23 paf 52: if(0){
53: int size=ops.size();
1.46 paf 54: //fprintf(stderr, "size=%d\n", size);
1.23 paf 55: for(int i=0; i<size; i++) {
56: Operation op;
57: op.cast=ops.quick_get(i);
1.46 paf 58: fprintf(stderr, "%8X\n", op.cast);
1.23 paf 59: }
60: }
61:
1.9 paf 62: int size=ops.size();
1.46 paf 63: //fprintf(stderr, "size=%d\n", size);
1.1 paf 64: for(int i=0; i<size; i++) {
1.23 paf 65: Operation op;
66: op.cast=ops.quick_get(i);
1.46 paf 67: fprintf(stderr, "%*s%s", level*4, "", opcode_name[op.code]);
1.1 paf 68:
1.52 paf 69: if(op.code==OP_VALUE) {
70: Value *value=static_cast<Value *>(ops.quick_get(++i));
71: fprintf(stderr, " \"%s\" %s", value->get_string()->cstr(), value->type());
1.15 paf 72: }
1.46 paf 73: fprintf(stderr, "\n");
1.1 paf 74:
1.65 paf 75: if(op.code==OP_CODE__STORE_PARAM) {
1.10 paf 76: const Array *local_ops=reinterpret_cast<const Array *>(ops.quick_get(++i));
1.9 paf 77: dump(level+1, *local_ops);
1.1 paf 78: }
79: }
1.60 paf 80: fflush(stderr);
1.1 paf 81: }
82:
1.32 paf 83: void Request::execute(const Array& ops) {
1.30 paf 84: if(1) {
1.51 paf 85: fputs("source----------------------------\n", stderr);
1.12 paf 86: dump(0, ops);
1.51 paf 87: fputs("execution-------------------------\n", stderr);
1.12 paf 88: }
89:
1.11 paf 90: int size=ops.size();
1.46 paf 91: //fprintf(stderr, "size=%d\n", size);
1.11 paf 92: for(int i=0; i<size; i++) {
1.23 paf 93: Operation op;
94: op.cast=ops.quick_get(i);
1.63 paf 95: fprintf(stderr, "%d:%s", stack.top()+1, opcode_name[op.code]); fflush(stderr);
1.11 paf 96:
1.23 paf 97: switch(op.code) {
1.51 paf 98: // param in next instruction
1.52 paf 99: case OP_VALUE:
1.32 paf 100: {
1.52 paf 101: Value *value=static_cast<Value *>(ops.quick_get(++i));
102: fprintf(stderr, " \"%s\" %s", value->get_string()->cstr(), value->type());
103: PUSH(value);
1.32 paf 104: break;
105: }
1.65 paf 106: case OP_CODE__STORE_PARAM:
1.32 paf 107: {
1.63 paf 108: VMethodFrame *frame=static_cast<VMethodFrame *>(stack[stack.top()]);
1.65 paf 109: // code
1.32 paf 110: const Array *local_ops=reinterpret_cast<const Array *>(ops.quick_get(++i));
1.46 paf 111: fprintf(stderr, " (%d)\n", local_ops->size());
1.38 paf 112: dump(1, *local_ops);
1.63 paf 113:
1.32 paf 114: Junction& j=*NEW Junction(pool(),
1.45 paf 115: *self, 0, 0,
1.64 paf 116: root, frame, frame, local_ops);
1.32 paf 117:
118: Value *value=NEW VJunction(j);
1.63 paf 119: value->set_name(frame->name());
1.65 paf 120:
121: // store param
122: frame->store_param(value);
1.32 paf 123: break;
124: }
1.66 paf 125: case OP_GET_CLASS:
1.38 paf 126: {
1.66 paf 127: String& name=POP_NAME();
128: VClass *vclass=static_cast<VClass *>(classes().get(name));
129: if(!vclass)
130: THROW(0,0,
131: &name,
132: ": undefined class");
133:
134: PUSH(vclass);
1.38 paf 135: break;
136: }
1.32 paf 137:
1.51 paf 138: // OP_WITH
1.37 paf 139: case OP_WITH_SELF:
140: {
141: PUSH(self);
142: break;
143: }
144: case OP_WITH_ROOT:
1.11 paf 145: {
1.37 paf 146: PUSH(root);
1.13 paf 147: break;
1.11 paf 148: }
1.15 paf 149: case OP_WITH_READ:
150: {
1.24 paf 151: PUSH(rcontext);
1.20 paf 152: break;
153: }
1.37 paf 154: case OP_WITH_WRITE:
1.20 paf 155: {
1.37 paf 156: PUSH(wcontext);
1.20 paf 157: break;
158: }
1.37 paf 159:
1.51 paf 160: // OTHER ACTIONS BUT WITHs
1.37 paf 161: case OP_CONSTRUCT:
1.20 paf 162: {
1.37 paf 163: Value *value=POP();
164: String& name=POP_NAME();
165: Value *ncontext=POP();
166: value->set_name(name);
167: ncontext->put_element(name, value);
1.15 paf 168: break;
169: }
1.18 paf 170: case OP_WRITE:
1.13 paf 171: {
1.24 paf 172: Value *value=POP();
1.70 paf 173: wcontext->write(*value);
1.13 paf 174: break;
1.14 paf 175: }
1.13 paf 176:
1.15 paf 177: case OP_GET_ELEMENT:
1.11 paf 178: {
1.17 paf 179: Value *value=get_element();
1.24 paf 180: PUSH(value);
1.17 paf 181: break;
182: }
183:
1.18 paf 184: case OP_GET_ELEMENT__WRITE:
1.17 paf 185: {
186: Value *value=get_element();
1.70 paf 187: wcontext->write(*value);
1.17 paf 188: break;
189: }
190:
1.32 paf 191:
1.17 paf 192: case OP_CREATE_EWPOOL:
193: {
1.24 paf 194: PUSH(wcontext);
1.43 paf 195: wcontext=NEW WWrapper(pool(), 0 /* empty */, true /* constructing */);
1.17 paf 196: break;
197: }
198: case OP_REDUCE_EWPOOL:
199: {
1.36 paf 200: Value *value=wcontext->result();
1.25 paf 201: wcontext=static_cast<WContext *>(POP());
1.24 paf 202: PUSH(value);
1.13 paf 203: break;
1.15 paf 204: }
1.13 paf 205:
1.26 paf 206: case OP_CREATE_RWPOOL:
207: {
208: Value *ncontext=POP();
209: PUSH(rcontext);
210: rcontext=ncontext;
211: PUSH(wcontext);
1.43 paf 212: wcontext=NEW WWrapper(pool(), ncontext, false /* not constructing */);
1.26 paf 213: break;
214: }
215: case OP_REDUCE_RWPOOL:
216: {
217: String *string=wcontext->get_string();
1.50 paf 218: Value *value=string?NEW VString(*string):NEW VString(pool());
1.26 paf 219: wcontext=static_cast<WContext *>(POP());
220: rcontext=POP();
221: PUSH(value);
222: break;
223: }
1.49 paf 224: case OP_CREATE_SWPOOL:
225: {
226: PUSH(wcontext);
1.50 paf 227: wcontext=NEW WWrapper(pool(), 0 /* empty */, false /* not constructing */);
1.49 paf 228: break;
229: }
230: case OP_REDUCE_SWPOOL:
231: {
232: // from "$a $b" part of expression taking only string value,
233: // ignoring any other content of wcontext
234: String *string=wcontext->get_string();
1.50 paf 235: Value *value=string?NEW VString(*string):NEW VString(pool());
236: wcontext=static_cast<WContext *>(POP());
1.49 paf 237: PUSH(value);
238: break;
239: }
240:
1.51 paf 241: // CALL
1.28 paf 242: case OP_GET_METHOD_FRAME:
243: {
1.32 paf 244: Value *value=POP();
1.70 paf 245: // info: this one's always method-junction, not a code-junction
1.32 paf 246: Junction *junction=value->get_junction();
1.31 paf 247: if(!junction)
1.28 paf 248: THROW(0,0,
1.42 paf 249: &value->name(),
1.39 paf 250: "type is '%s', can not call it (must be method or junction)",
1.38 paf 251: value->type());
1.70 paf 252:
1.34 paf 253: VMethodFrame *frame=NEW VMethodFrame(pool(), *junction);
1.63 paf 254: frame->set_name(junction->self.name());
1.28 paf 255: PUSH(frame);
256: break;
257: }
258: case OP_STORE_PARAM:
259: {
260: Value *value=POP();
1.34 paf 261: VMethodFrame *frame=static_cast<VMethodFrame *>(stack[0]);
1.28 paf 262: frame->store_param(value);
1.29 paf 263: break;
264: }
265:
266: case OP_CALL:
267: {
1.46 paf 268: fprintf(stderr, "->\n");
1.34 paf 269: VMethodFrame *frame=static_cast<VMethodFrame *>(POP());
270: frame->fill_unspecified_params();
1.45 paf 271: PUSH(self);
272: PUSH(root);
273: PUSH(rcontext);
274: PUSH(wcontext);
275:
1.38 paf 276: VClass *called_class=frame->junction.self.get_class();
1.43 paf 277: // constructing?
278: if(wcontext->constructing()) { // yes
1.41 paf 279: // constructor call: $some(^class:method(..))
1.70 paf 280: self=NEW VObject(pool(), *called_class);
281: frame->write(*self);
1.43 paf 282: } else { // no
1.47 paf 283: // context is object or class & is it my class or my parent's class?
1.44 paf 284: VClass *read_class=rcontext->get_class();
285: if(read_class && read_class->is_or_derived_from(*called_class)) // yes
1.46 paf 286: self=rcontext; // class dynamic call
1.44 paf 287: else // no
288: self=&frame->junction.self; // static or simple dynamic call
1.38 paf 289: }
1.45 paf 290: frame->set_self(*self);
1.29 paf 291: root=rcontext=wcontext=frame;
1.47 paf 292: {
1.48 paf 293: // take object or class from any wrappers
294: VAliased *aliased=self->get_aliased();
295: // substitute class alias to the class they are called AS
1.47 paf 296: Temp_alias temp_alias(*aliased, *frame->junction.vclass);
1.68 paf 297:
1.69 paf 298: Method& method=*frame->junction.method;
299: if(method.native_code) // native code?
1.70 paf 300: (*method.native_code)(*this, frame->numbered_params()); // execute it
1.68 paf 301: else // parser code
1.69 paf 302: execute(*method.parser_code); // execute it
1.47 paf 303: }
1.36 paf 304: Value *value=wcontext->result();
1.45 paf 305:
306: wcontext=static_cast<WContext *>(POP());
307: rcontext=POP();
308: root=POP();
309: self=static_cast<VAliased *>(POP());
1.62 paf 310:
1.61 paf 311: PUSH(value);
1.46 paf 312: fprintf(stderr, "<-returned");
1.49 paf 313: break;
314: }
315:
1.55 paf 316: // expression ops: unary
317: case OP_NEG:
318: {
319: Value *operand=POP();
320: Value *value=NEW VDouble(pool(), -operand->get_double());
321: PUSH(value);
322: break;
323: }
324: case OP_INV:
325: {
326: Value *operand=POP();
1.61 paf 327: Value *value=NEW VDouble(pool(),
328: ~static_cast<int>(operand->get_double()));
1.55 paf 329: PUSH(value);
330: break;
331: }
332: case OP_NOT:
333: {
334: Value *operand=POP();
335: Value *value=NEW VBool(pool(), !operand->get_bool());
336: PUSH(value);
337: break;
338: }
1.62 paf 339: case OP_DEF:
340: {
341: Value *operand=POP();
342: Value *value=NEW VBool(pool(), operand->get_defined());
343: PUSH(value);
344: break;
345: }
346: case OP_IN:
347: {
348: Value *operand=POP();
349: Value *value=NEW VBool(pool(), true/*TODO*/);
350: PUSH(value);
351: break;
352: }
353: case OP_FEXISTS:
354: {
355: Value *operand=POP();
356: Value *value=NEW VBool(pool(), true/*TODO*/);
357: PUSH(value);
358: break;
359: }
1.55 paf 360:
361: // expression ops: binary
362: case OP_SUB:
1.53 paf 363: {
1.61 paf 364: Value *b=POP(); Value *a=POP();
365: Value *value=NEW VDouble(pool(), a->get_double() - b->get_double());
1.53 paf 366: PUSH(value);
367: break;
368: }
1.55 paf 369: case OP_ADD:
1.53 paf 370: {
1.61 paf 371: Value *b=POP(); Value *a=POP();
372: Value *value=NEW VDouble(pool(), a->get_double() + b->get_double());
1.53 paf 373: PUSH(value);
374: break;
375: }
1.49 paf 376: case OP_MUL:
377: {
1.61 paf 378: Value *b=POP(); Value *a=POP();
379: Value *value=NEW VDouble(pool(), a->get_double() * b->get_double());
1.53 paf 380: PUSH(value);
381: break;
382: }
383: case OP_DIV:
384: {
1.61 paf 385: Value *b=POP(); Value *a=POP();
386: Value *value=NEW VDouble(pool(), a->get_double() / b->get_double());
1.54 paf 387: PUSH(value);
388: break;
389: }
1.55 paf 390: case OP_MOD:
391: {
1.61 paf 392: Value *b=POP(); Value *a=POP();
1.55 paf 393: Value *value=NEW VDouble(pool(),
394: static_cast<int>(a->get_double()) %
395: static_cast<int>(b->get_double()));
396: PUSH(value);
397: break;
398: }
399: case OP_BIN_AND:
1.54 paf 400: {
1.61 paf 401: Value *b=POP(); Value *a=POP();
1.55 paf 402: Value *value=NEW VDouble(pool(),
403: static_cast<int>(a->get_double()) &
404: static_cast<int>(b->get_double()));
1.54 paf 405: PUSH(value);
406: break;
407: }
1.55 paf 408: case OP_BIN_OR:
1.54 paf 409: {
1.61 paf 410: Value *b=POP(); Value *a=POP();
1.54 paf 411: Value *value=NEW VDouble(pool(),
1.55 paf 412: static_cast<int>(a->get_double()) |
413: static_cast<int>(b->get_double()));
414: PUSH(value);
415: break;
416: }
1.56 paf 417: case OP_BIN_XOR:
418: {
1.61 paf 419: Value *b=POP(); Value *a=POP();
1.56 paf 420: Value *value=NEW VDouble(pool(),
421: static_cast<int>(a->get_double()) ^
422: static_cast<int>(b->get_double()));
423: PUSH(value);
424: break;
425: }
1.55 paf 426: case OP_LOG_AND:
427: {
1.61 paf 428: Value *b=POP(); Value *a=POP();
429: Value *value=NEW VBool(pool(), a->get_bool() && b->get_bool());
1.55 paf 430: PUSH(value);
431: break;
432: }
433: case OP_LOG_OR:
434: {
1.61 paf 435: Value *b=POP(); Value *a=POP();
436: Value *value=NEW VBool(pool(), a->get_bool() || b->get_bool());
1.56 paf 437: PUSH(value);
438: break;
439: }
440: case OP_LOG_XOR:
441: {
1.61 paf 442: Value *b=POP(); Value *a=POP();
443: Value *value=NEW VBool(pool(), a->get_bool() ^ b->get_bool());
1.55 paf 444: PUSH(value);
445: break;
446: }
447: case OP_NUM_LT:
448: {
1.61 paf 449: Value *b=POP(); Value *a=POP();
450: Value *value=NEW VBool(pool(), a->get_double() < b->get_double());
1.55 paf 451: PUSH(value);
452: break;
453: }
454: case OP_NUM_GT:
455: {
1.61 paf 456: Value *b=POP(); Value *a=POP();
457: Value *value=NEW VBool(pool(), a->get_double() > b->get_double());
1.55 paf 458: PUSH(value);
459: break;
460: }
461: case OP_NUM_LE:
462: {
1.61 paf 463: Value *b=POP(); Value *a=POP();
464: Value *value=NEW VBool(pool(), a->get_double() <= b->get_double());
1.55 paf 465: PUSH(value);
466: break;
467: }
468: case OP_NUM_GE:
469: {
1.61 paf 470: Value *b=POP(); Value *a=POP();
471: Value *value=NEW VBool(pool(), a->get_double() >= b->get_double());
1.55 paf 472: PUSH(value);
473: break;
474: }
475: case OP_NUM_EQ:
476: {
1.61 paf 477: Value *b=POP(); Value *a=POP();
478: Value *value=NEW VBool(pool(), a->get_double() == b->get_double());
1.55 paf 479: PUSH(value);
480: break;
481: }
482: case OP_NUM_NE:
483: {
1.61 paf 484: Value *b=POP(); Value *a=POP();
485: Value *value=NEW VBool(pool(), a->get_double() != b->get_double());
1.54 paf 486: PUSH(value);
487: break;
488: }
1.58 paf 489: case OP_STR_LT:
490: {
1.61 paf 491: Value *b=POP(); Value *a=POP();
492: Value *value=NEW VBool(pool(), a->as_string() < b->as_string());
1.58 paf 493: PUSH(value);
494: break;
495: }
496: case OP_STR_GT:
497: {
1.61 paf 498: Value *b=POP(); Value *a=POP();
499: Value *value=NEW VBool(pool(), a->as_string() > b->as_string());
1.58 paf 500: PUSH(value);
501: break;
502: }
1.55 paf 503: case OP_STR_LE:
1.58 paf 504: {
1.61 paf 505: Value *b=POP(); Value *a=POP();
506: Value *value=NEW VBool(pool(), a->as_string() <= b->as_string());
1.58 paf 507: PUSH(value);
508: break;
509: }
1.55 paf 510: case OP_STR_GE:
1.54 paf 511: {
1.61 paf 512: Value *b=POP(); Value *a=POP();
513: Value *value=NEW VBool(pool(), a->as_string() >= b->as_string());
1.58 paf 514: PUSH(value);
515: break;
516: }
517: case OP_STR_EQ:
518: {
1.61 paf 519: Value *b=POP(); Value *a=POP();
520: Value *value=NEW VBool(pool(), a->as_string() == b->as_string());
1.58 paf 521: PUSH(value);
522: break;
523: }
524: case OP_STR_NE:
525: {
1.61 paf 526: Value *b=POP(); Value *a=POP();
527: Value *value=NEW VBool(pool(), a->as_string() != b->as_string());
1.49 paf 528: PUSH(value);
1.28 paf 529: break;
530: }
531:
1.11 paf 532: default:
1.67 paf 533: THROW(0,0,
534: 0,
535: "unhandled '%s' opcode", opcode_name[op.code]);
1.11 paf 536: }
1.46 paf 537: fprintf(stderr, "\n");
1.11 paf 538: }
1.1 paf 539: }
1.17 paf 540:
541: Value *Request::get_element() {
1.32 paf 542: String& name=POP_NAME();
1.24 paf 543: Value *ncontext=POP();
1.32 paf 544: Value *value=ncontext->get_element(name);
1.21 paf 545:
1.70 paf 546: // autocalc possible code-junction
1.71 ! paf 547: value=value?&autocalc(*value, true/*make_string*/):NEW VUnknown(pool());
1.63 paf 548:
549: value->set_name(name);
1.17 paf 550: return value;
1.34 paf 551: }
1.70 paf 552:
1.71 ! paf 553: Value& Request::autocalc(Value& value, bool make_string) {
1.70 paf 554: Junction *junction=value.get_junction();
555: if(junction && junction->code) { // is it a code-junction?
556: // autocalc it
557: fprintf(stderr, "ja->\n");
558: PUSH(self);
559: PUSH(root);
560: PUSH(rcontext);
561: PUSH(wcontext);
562:
1.71 ! paf 563: WContext *frame;
! 564: if(make_string) {
! 565: // almost plain wwrapper about junction wcontext,
! 566: // BUT intercepts string writes
! 567: frame=NEW VCodeFrame(pool(), *junction->wcontext);
! 568: } else {
! 569: // plain wwrapper
! 570: frame=NEW WWrapper(pool(), 0 /* empty */, false /*not constructing*/);
! 571: }
! 572:
! 573: wcontext=frame;
1.70 paf 574: self=&junction->self;
575: root=junction->root;
576: rcontext=junction->rcontext;
577: execute(*junction->code);
1.71 ! paf 578: Value *result;
! 579: if(make_string) {
! 580: // CodeFrame soul:
! 581: // string writes were intercepted
! 582: // returning them as the result of getting code-junction
! 583: result=NEW VString(*frame->get_string());
! 584: } else
! 585: result=frame->result();
1.70 paf 586:
587: wcontext=static_cast<WContext *>(POP());
588: rcontext=POP();
589: root=POP();
590: self=static_cast<VAliased *>(POP());
591:
592: fprintf(stderr, "<-ja returned");
1.71 ! paf 593: return *result;
1.70 paf 594: } else
595: return value;
596: }
597:
598: void Request::write(Value& avalue) {
599: wcontext->write(avalue);
600: }
E-mail: