Changeset 23de644 in mainline for uspace/app/sbi/src/run_expr.c
- Timestamp:
- 2010-04-04T22:31:01Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade
- Children:
- 074444f, ecb6ac32
- Parents:
- 3aae4e8
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/sbi/src/run_expr.c
r3aae4e8 r23de644 32 32 #include <stdlib.h> 33 33 #include <assert.h> 34 #include "bigint.h" 34 35 #include "debug.h" 35 36 #include "intmap.h" … … 71 72 72 73 static void run_unop(run_t *run, stree_unop_t *unop, rdata_item_t **res); 74 static void run_unop_int(run_t *run, stree_unop_t *unop, rdata_value_t *val, 75 rdata_item_t **res); 76 73 77 static void run_new(run_t *run, stree_new_t *new_op, rdata_item_t **res); 74 78 static void run_new_array(run_t *run, stree_new_t *new_op, … … 349 353 value->var = var; 350 354 var->u.int_v = int_v; 351 int_v->value = lit_int->value;355 bigint_clone(&lit_int->value, &int_v->value); 352 356 353 357 *res = item; … … 436 440 #endif 437 441 run_expr(run, binop->arg1, &rarg1_i); 442 if (run_is_bo(run)) { 443 *res = NULL; 444 return; 445 } 446 438 447 run_expr(run, binop->arg2, &rarg2_i); 448 if (run_is_bo(run)) { 449 *res = NULL; 450 return; 451 } 439 452 440 453 switch (binop->bc) { 441 454 case bo_plus: 455 case bo_minus: 456 case bo_mult: 442 457 case bo_equal: 443 458 case bo_notequal: … … 496 511 rdata_int_t *int_v; 497 512 498 int i1, i2; 513 bigint_t *i1, *i2; 514 bigint_t diff; 515 bool_t done; 516 bool_t zf, nf; 499 517 500 518 (void) run; … … 509 527 var->u.int_v = int_v; 510 528 511 i1 = v1->var->u.int_v->value; 512 i2 = v2->var->u.int_v->value; 529 i1 = &v1->var->u.int_v->value; 530 i2 = &v2->var->u.int_v->value; 531 532 done = b_true; 513 533 514 534 switch (binop->bc) { 515 535 case bo_plus: 516 int_v->value = i1 + i2; 517 break; 536 bigint_add(i1, i2, &int_v->value); 537 break; 538 case bo_minus: 539 bigint_sub(i1, i2, &int_v->value); 540 break; 541 case bo_mult: 542 bigint_mul(i1, i2, &int_v->value); 543 break; 544 default: 545 done = b_false; 546 break; 547 } 548 549 if (done) { 550 *res = item; 551 return; 552 } 553 554 /* Relational operation. */ 555 556 bigint_sub(i1, i2, &diff); 557 zf = bigint_is_zero(&diff); 558 nf = bigint_is_negative(&diff); 518 559 519 560 /* XXX We should have a real boolean type. */ 561 switch (binop->bc) { 520 562 case bo_equal: 521 int_v->value = (i1 == i2) ? 1 : 0;563 bigint_init(&int_v->value, zf ? 1 : 0); 522 564 break; 523 565 case bo_notequal: 524 int_v->value = (i1 != i2) ? 1 : 0;566 bigint_init(&int_v->value, !zf ? 1 : 0); 525 567 break; 526 568 case bo_lt: 527 int_v->value = (i1 < i2) ? 1 : 0;569 bigint_init(&int_v->value, (!zf && nf) ? 1 : 0); 528 570 break; 529 571 case bo_gt: 530 int_v->value = (i1 > i2) ? 1 : 0;572 bigint_init(&int_v->value, (!zf && !nf) ? 1 : 0); 531 573 break; 532 574 case bo_lt_equal: 533 int_v->value = (i1 <= i2) ? 1 : 0;575 bigint_init(&int_v->value, (zf || nf) ? 1 : 0); 534 576 break; 535 577 case bo_gt_equal: 536 int_v->value = (i1 >= i2) ? 1 : 0;578 bigint_init(&int_v->value, !nf ? 1 : 0); 537 579 break; 538 580 default: … … 610 652 /* XXX We should have a real boolean type. */ 611 653 case bo_equal: 612 int_v->value = (ref1 == ref2) ? 1 : 0;654 bigint_init(&int_v->value, (ref1 == ref2) ? 1 : 0); 613 655 break; 614 656 case bo_notequal: 615 int_v->value = (ref1 != ref2) ? 1 : 0;657 bigint_init(&int_v->value, (ref1 != ref2) ? 1 : 0); 616 658 break; 617 659 default: … … 628 670 static void run_unop(run_t *run, stree_unop_t *unop, rdata_item_t **res) 629 671 { 630 rdata_item_t *rarg; 672 rdata_item_t *rarg_i; 673 rdata_item_t *rarg_vi; 674 rdata_value_t *val; 631 675 632 676 #ifdef DEBUG_RUN_TRACE 633 677 printf("Run unary operation.\n"); 634 678 #endif 635 run_expr(run, unop->arg, &rarg); 636 *res = NULL; 637 } 679 run_expr(run, unop->arg, &rarg_i); 680 if (run_is_bo(run)) { 681 *res = NULL; 682 return; 683 } 684 685 #ifdef DEBUG_RUN_TRACE 686 printf("Check unop argument result.\n"); 687 #endif 688 run_cvt_value_item(run, rarg_i, &rarg_vi); 689 690 val = rarg_vi->u.value; 691 692 switch (val->var->vc) { 693 case vc_int: 694 run_unop_int(run, unop, val, res); 695 break; 696 default: 697 printf("Unimplemented: Unrary operation argument of " 698 "type %d.\n", val->var->vc); 699 run_raise_error(run); 700 *res = NULL; 701 break; 702 } 703 } 704 705 /** Evaluate unary operation on int argument. */ 706 static void run_unop_int(run_t *run, stree_unop_t *unop, rdata_value_t *val, 707 rdata_item_t **res) 708 { 709 rdata_item_t *item; 710 rdata_value_t *value; 711 rdata_var_t *var; 712 rdata_int_t *int_v; 713 714 (void) run; 715 716 item = rdata_item_new(ic_value); 717 value = rdata_value_new(); 718 var = rdata_var_new(vc_int); 719 int_v = rdata_int_new(); 720 721 item->u.value = value; 722 value->var = var; 723 var->u.int_v = int_v; 724 725 switch (unop->uc) { 726 case uo_plus: 727 bigint_clone(&val->var->u.int_v->value, &int_v->value); 728 break; 729 case uo_minus: 730 bigint_reverse_sign(&val->var->u.int_v->value, 731 &int_v->value); 732 break; 733 } 734 735 *res = item; 736 } 737 638 738 639 739 /** Evaluate @c new operation. */ … … 680 780 int length; 681 781 int i; 782 int rc; 783 int iextent; 682 784 683 785 #ifdef DEBUG_RUN_TRACE … … 708 810 /* Evaluate extent argument. */ 709 811 run_expr(run, expr, &rexpr); 812 if (run_is_bo(run)) { 813 *res = NULL; 814 return; 815 } 816 710 817 run_cvt_value_item(run, rexpr, &rexpr_vi); 711 818 assert(rexpr_vi->ic == ic_value); … … 718 825 719 826 #ifdef DEBUG_RUN_TRACE 720 printf("Array extent: %d.\n", rexpr_var->u.int_v->value); 721 #endif 722 array->extent[i] = rexpr_var->u.int_v->value; 827 printf("Array extent: "); 828 bigint_print(&rexpr_var->u.int_v->value); 829 printf(".\n"); 830 #endif 831 rc = bigint_get_value_int(&rexpr_var->u.int_v->value, 832 &iextent); 833 if (rc != EOK) { 834 printf("Memory allocation failed (big int used).\n"); 835 exit(1); 836 } 837 838 array->extent[i] = iextent; 723 839 length = length * array->extent[i]; 724 840 … … 738 854 elem_var = rdata_var_new(vc_int); 739 855 elem_var->u.int_v = rdata_int_new(); 740 elem_var->u.int_v->value = 0;856 bigint_init(&elem_var->u.int_v->value, 0); 741 857 742 858 array->element[i] = elem_var; … … 755 871 tdata_item_t *titem, rdata_item_t **res) 756 872 { 757 rdata_object_t *obj;758 rdata_var_t *obj_var;759 760 stree_symbol_t *csi_sym;761 873 stree_csi_t *csi; 762 stree_csimbr_t *csimbr;763 764 rdata_var_t *mbr_var;765 766 list_node_t *node;767 874 768 875 #ifdef DEBUG_RUN_TRACE 769 876 printf("Create new object.\n"); 770 877 #endif 771 (void) run;772 878 (void) new_op; 773 879 … … 775 881 assert(titem->tic == tic_tobject); 776 882 csi = titem->u.tobject->csi; 777 csi_sym = csi_to_symbol(csi); 778 779 /* Create the object. */ 780 obj = rdata_object_new(); 781 obj->class_sym = csi_sym; 782 intmap_init(&obj->fields); 783 784 obj_var = rdata_var_new(vc_object); 785 obj_var->u.object_v = obj; 786 787 /* Create object fields. */ 788 node = list_first(&csi->members); 789 while (node != NULL) { 790 csimbr = list_node_data(node, stree_csimbr_t *); 791 if (csimbr->cc == csimbr_var) { 792 /* XXX Depends on member variable type. */ 793 mbr_var = rdata_var_new(vc_int); 794 mbr_var->u.int_v = rdata_int_new(); 795 mbr_var->u.int_v->value = 0; 796 797 intmap_set(&obj->fields, csimbr->u.var->name->sid, 798 mbr_var); 799 } 800 801 node = list_next(&csi->members, node); 802 } 803 804 /* Create reference to the new object. */ 805 run_reference(run, obj_var, res); 883 884 /* Create CSI instance. */ 885 run_new_csi_inst(run, csi, res); 806 886 } 807 887 … … 815 895 #endif 816 896 run_expr(run, access->arg, &rarg); 897 if (run_is_bo(run)) { 898 *res = NULL; 899 return; 900 } 901 817 902 if (rarg == NULL) { 818 903 printf("Error: Sub-expression has no value.\n"); … … 1028 1113 #endif 1029 1114 run_expr(run, call->fun, &rfun); 1115 if (run_is_bo(run)) { 1116 *res = NULL; 1117 return; 1118 } 1030 1119 1031 1120 if (run->thread_ar->bo_mode != bm_none) { … … 1058 1147 arg = list_node_data(node, stree_expr_t *); 1059 1148 run_expr(run, arg, &rarg_i); 1149 if (run_is_bo(run)) { 1150 *res = NULL; 1151 return; 1152 } 1153 1060 1154 run_cvt_value_item(run, rarg_i, &rarg_vi); 1061 1155 … … 1096 1190 #endif 1097 1191 run_expr(run, index->base, &rbase); 1192 if (run_is_bo(run)) { 1193 *res = NULL; 1194 return; 1195 } 1098 1196 1099 1197 vc = run_item_get_vc(run, rbase); … … 1115 1213 arg = list_node_data(node, stree_expr_t *); 1116 1214 run_expr(run, arg, &rarg_i); 1215 if (run_is_bo(run)) { 1216 *res = NULL; 1217 return; 1218 } 1219 1117 1220 run_cvt_value_item(run, rarg_i, &rarg_vi); 1118 1221 … … 1149 1252 int elem_index; 1150 1253 int arg_val; 1254 int rc; 1151 1255 1152 1256 rdata_item_t *ritem; … … 1189 1293 } 1190 1294 1191 arg_val = arg->u.value->var->u.int_v->value; 1192 1193 if (arg_val < 0 || arg_val >= array->extent[i]) { 1295 rc = bigint_get_value_int( 1296 &arg->u.value->var->u.int_v->value, 1297 &arg_val); 1298 1299 if (rc != EOK || arg_val < 0 || arg_val >= array->extent[i]) { 1300 #ifdef DEBUG_RUN_TRACE 1194 1301 printf("Error: Array index (value: %d) is out of range.\n", 1195 1302 arg_val); 1196 run_raise_error(run); 1303 #endif 1304 /* Raise Error.OutOfBounds */ 1305 run_raise_exc(run, 1306 run->program->builtin->error_outofbounds); 1197 1307 *res = run_recovery_item(run); 1198 1308 return; … … 1307 1417 int elem_index; 1308 1418 int arg_val; 1309 int rc ;1419 int rc1, rc2; 1310 1420 1311 1421 rdata_value_t *value; … … 1322 1432 run_cvt_value_item(run, base, &base_vi); 1323 1433 assert(base_vi->u.value->var->vc == vc_string); 1324 string = base ->u.value->var->u.string_v;1434 string = base_vi->u.value->var->u.string_v; 1325 1435 1326 1436 /* … … 1346 1456 } 1347 1457 1348 arg_val = arg->u.value->var->u.int_v->value; 1458 rc1 = bigint_get_value_int( 1459 &arg->u.value->var->u.int_v->value, 1460 &arg_val); 1461 1349 1462 elem_index = arg_val; 1350 1463 … … 1358 1471 } 1359 1472 1360 rc = os_str_get_char(string->value, elem_index, &cval); 1361 if (rc != EOK) { 1473 if (rc1 == EOK) 1474 rc2 = os_str_get_char(string->value, elem_index, &cval); 1475 1476 if (rc1 != EOK || rc2 != EOK) { 1362 1477 printf("Error: String index (value: %d) is out of range.\n", 1363 1478 arg_val); … … 1372 1487 cvar = rdata_var_new(vc_int); 1373 1488 cvar->u.int_v = rdata_int_new(); 1374 cvar->u.int_v->value = cval;1489 bigint_init(&cvar->u.int_v->value, cval); 1375 1490 value->var = cvar; 1376 1491 … … 1389 1504 #endif 1390 1505 run_expr(run, assign->dest, &rdest_i); 1506 if (run_is_bo(run)) { 1507 *res = NULL; 1508 return; 1509 } 1510 1391 1511 run_expr(run, assign->src, &rsrc_i); 1512 if (run_is_bo(run)) { 1513 *res = NULL; 1514 return; 1515 } 1392 1516 1393 1517 run_cvt_value_item(run, rsrc_i, &rsrc_vi); … … 1423 1547 #endif 1424 1548 run_expr(run, as_op->arg, &rarg_i); 1549 if (run_is_bo(run)) { 1550 *res = NULL; 1551 return; 1552 } 1425 1553 1426 1554 /* … … 1467 1595 1468 1596 *res = rarg_vi; 1597 } 1598 1599 /** Create new CSI instance. */ 1600 void run_new_csi_inst(run_t *run, stree_csi_t *csi, rdata_item_t **res) 1601 { 1602 rdata_object_t *obj; 1603 rdata_var_t *obj_var; 1604 1605 stree_symbol_t *csi_sym; 1606 stree_csimbr_t *csimbr; 1607 1608 rdata_var_t *mbr_var; 1609 1610 list_node_t *node; 1611 1612 csi_sym = csi_to_symbol(csi); 1613 1614 #ifdef DEBUG_RUN_TRACE 1615 printf("Create new instance of CSI '"); 1616 symbol_print_fqn(csi_sym); 1617 printf("'.\n"); 1618 #endif 1619 1620 /* Create the object. */ 1621 obj = rdata_object_new(); 1622 obj->class_sym = csi_sym; 1623 intmap_init(&obj->fields); 1624 1625 obj_var = rdata_var_new(vc_object); 1626 obj_var->u.object_v = obj; 1627 1628 /* Create object fields. */ 1629 node = list_first(&csi->members); 1630 while (node != NULL) { 1631 csimbr = list_node_data(node, stree_csimbr_t *); 1632 if (csimbr->cc == csimbr_var) { 1633 /* XXX Depends on member variable type. */ 1634 mbr_var = rdata_var_new(vc_int); 1635 mbr_var->u.int_v = rdata_int_new(); 1636 bigint_init(&mbr_var->u.int_v->value, 0); 1637 1638 intmap_set(&obj->fields, csimbr->u.var->name->sid, 1639 mbr_var); 1640 } 1641 1642 node = list_next(&csi->members, node); 1643 } 1644 1645 /* Create reference to the new object. */ 1646 run_reference(run, obj_var, res); 1469 1647 } 1470 1648 … … 1492 1670 } 1493 1671 1494 return (var->u.int_v->value != 0);1495 } 1672 return !bigint_is_zero(&var->u.int_v->value); 1673 }
Note:
See TracChangeset
for help on using the changeset viewer.