Changeset 051bc69a in mainline for uspace/app/sbi/src/run_expr.c
- Timestamp:
- 2010-05-08T08:10:44Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 640ffe6, c5cb943d
- Parents:
- 25a76ab8
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/sbi/src/run_expr.c
r25a76ab8 r051bc69a 78 78 static void run_binop_ref(run_t *run, stree_binop_t *binop, rdata_value_t *v1, 79 79 rdata_value_t *v2, rdata_item_t **res); 80 static void run_binop_enum(run_t *run, stree_binop_t *binop, rdata_value_t *v1, 81 rdata_value_t *v2, rdata_item_t **res); 80 82 81 83 static void run_unop(run_t *run, stree_unop_t *unop, rdata_item_t **res); 84 static void run_unop_bool(run_t *run, stree_unop_t *unop, rdata_value_t *val, 85 rdata_item_t **res); 82 86 static void run_unop_int(run_t *run, stree_unop_t *unop, rdata_value_t *val, 83 87 rdata_item_t **res); … … 88 92 static void run_new_object(run_t *run, stree_new_t *new_op, 89 93 tdata_item_t *titem, rdata_item_t **res); 94 95 static void run_object_ctor(run_t *run, rdata_var_t *obj, list_t *arg_vals); 90 96 91 97 static void run_access(run_t *run, stree_access_t *access, rdata_item_t **res); … … 98 104 static void run_access_object(run_t *run, stree_access_t *access, 99 105 rdata_item_t *arg, rdata_item_t **res); 106 static void run_access_symbol(run_t *run, stree_access_t *access, 107 rdata_item_t *arg, rdata_item_t **res); 100 108 101 109 static void run_call(run_t *run, stree_call_t *call, rdata_item_t **res); 110 static void run_call_args(run_t *run, list_t *args, list_t *arg_vals); 111 102 112 static void run_index(run_t *run, stree_index_t *index, rdata_item_t **res); 103 113 static void run_index_array(run_t *run, stree_index_t *index, … … 189 199 rdata_var_t *var; 190 200 rdata_deleg_t *deleg_v; 201 rdata_symbol_t *symbol_v; 191 202 192 203 run_proc_ar_t *proc_ar; … … 246 257 case sc_csi: 247 258 #ifdef DEBUG_RUN_TRACE 248 printf("Referencing class.\n");259 printf("Referencing CSI.\n"); 249 260 #endif 250 261 item = rdata_item_new(ic_value); … … 260 271 deleg_v->sym = sym; 261 272 *res = item; 273 break; 274 case sc_ctor: 275 /* It is not possible to reference a constructor explicitly. */ 276 assert(b_false); 277 case sc_enum: 278 #ifdef DEBUG_RUN_TRACE 279 printf("Referencing enum.\n"); 280 #endif 281 item = rdata_item_new(ic_value); 282 value = rdata_value_new(); 283 var = rdata_var_new(vc_symbol); 284 symbol_v = rdata_symbol_new(); 285 286 item->u.value = value; 287 value->var = var; 288 var->u.symbol_v = symbol_v; 289 290 symbol_v->sym = sym; 291 *res = item; 292 break; 293 case sc_deleg: 294 /* XXX TODO */ 295 printf("Unimplemented: Delegate name reference.\n"); 296 abort(); 262 297 break; 263 298 case sc_fun: … … 329 364 *res = item; 330 365 break; 331 default: 332 printf("Referencing symbol class %d unimplemented.\n", sym->sc); 333 *res = NULL; 366 case sc_prop: 367 /* XXX TODO */ 368 printf("Unimplemented: Property name reference.\n"); 369 abort(); 334 370 break; 335 371 } … … 571 607 } 572 608 573 switch (binop->bc) {574 case bo_plus:575 case bo_minus:576 case bo_mult:577 case bo_equal:578 case bo_notequal:579 case bo_lt:580 case bo_gt:581 case bo_lt_equal:582 case bo_gt_equal:583 /* These are implemented so far. */584 break;585 default:586 printf("Unimplemented: Binary operation type %d.\n",587 binop->bc);588 exit(1);589 }590 591 609 #ifdef DEBUG_RUN_TRACE 592 610 printf("Check binop argument results.\n"); … … 621 639 run_binop_ref(run, binop, v1, v2, res); 622 640 break; 641 case vc_enum: 642 run_binop_enum(run, binop, v1, v2, res); 643 break; 623 644 case vc_deleg: 624 645 case vc_array: 625 646 case vc_object: 626 647 case vc_resource: 648 case vc_symbol: 627 649 assert(b_false); 628 650 } … … 685 707 bool_v->value = (b1 == b_true) || (b2 == b_false); 686 708 break; 709 710 case bo_and: 711 bool_v->value = (b1 == b_true) && (b2 == b_true); 712 break; 713 case bo_or: 714 bool_v->value = (b1 == b_true) || (b2 == b_true); 715 break; 687 716 } 688 717 … … 753 782 bool_v->value = !nf; 754 783 break; 755 default: 784 785 case bo_and: 786 case bo_or: 756 787 assert(b_false); 757 788 } … … 832 863 833 864 switch (binop->bc) { 865 case bo_plus: 866 case bo_minus: 867 case bo_mult: 868 assert(b_false); 869 834 870 case bo_equal: 835 871 bool_v->value = zf; … … 850 886 bool_v->value = !nf; 851 887 break; 852 default: 888 case bo_and: 889 case bo_or: 853 890 assert(b_false); 854 891 } … … 872 909 rdata_var_t *var; 873 910 rdata_string_t *string_v; 911 rdata_bool_t *bool_v; 912 bool_t done; 913 bool_t zf; 874 914 875 915 const char *s1, *s2; … … 879 919 item = rdata_item_new(ic_value); 880 920 value = rdata_value_new(); 881 var = rdata_var_new(vc_string);882 string_v = rdata_string_new();883 921 884 922 item->u.value = value; 885 value->var = var;886 var->u.string_v = string_v;887 923 888 924 s1 = v1->var->u.string_v->value; 889 925 s2 = v2->var->u.string_v->value; 926 927 done = b_true; 890 928 891 929 switch (binop->bc) { 892 930 case bo_plus: 893 931 /* Concatenate strings. */ 932 string_v = rdata_string_new(); 894 933 string_v->value = os_str_acat(s1, s2); 934 break; 935 default: 936 done = b_false; 937 break; 938 } 939 940 if (done) { 941 var = rdata_var_new(vc_string); 942 var->u.string_v = string_v; 943 value->var = var; 944 *res = item; 945 return; 946 } 947 948 var = rdata_var_new(vc_bool); 949 bool_v = rdata_bool_new(); 950 var->u.bool_v = bool_v; 951 value->var = var; 952 953 /* Relational operation. */ 954 955 zf = os_str_cmp(s1, s2) == 0; 956 957 switch (binop->bc) { 958 case bo_equal: 959 bool_v->value = zf; 960 break; 961 case bo_notequal: 962 bool_v->value = !zf; 895 963 break; 896 964 default: … … 951 1019 } 952 1020 1021 /** Evaluate binary operation on enum arguments. 1022 * 1023 * @param run Runner object 1024 * @param binop Binary operation 1025 * @param v1 Value of first argument 1026 * @param v2 Value of second argument 1027 * @param res Place to store result 1028 */ 1029 static void run_binop_enum(run_t *run, stree_binop_t *binop, rdata_value_t *v1, 1030 rdata_value_t *v2, rdata_item_t **res) 1031 { 1032 rdata_item_t *item; 1033 rdata_value_t *value; 1034 rdata_var_t *var; 1035 rdata_bool_t *bool_v; 1036 1037 rdata_var_t *ref1, *ref2; 1038 1039 (void) run; 1040 1041 item = rdata_item_new(ic_value); 1042 value = rdata_value_new(); 1043 var = rdata_var_new(vc_bool); 1044 bool_v = rdata_bool_new(); 1045 1046 item->u.value = value; 1047 value->var = var; 1048 var->u.bool_v = bool_v; 1049 1050 ref1 = v1->var->u.ref_v->vref; 1051 ref2 = v2->var->u.ref_v->vref; 1052 1053 switch (binop->bc) { 1054 case bo_equal: 1055 bool_v->value = (ref1 == ref2); 1056 break; 1057 case bo_notequal: 1058 bool_v->value = (ref1 != ref2); 1059 break; 1060 default: 1061 /* Should have been caught by static typing. */ 1062 assert(b_false); 1063 } 1064 1065 *res = item; 1066 } 953 1067 954 1068 /** Evaluate unary operation. … … 981 1095 982 1096 switch (val->var->vc) { 1097 case vc_bool: 1098 run_unop_bool(run, unop, val, res); 1099 break; 983 1100 case vc_int: 984 1101 run_unop_int(run, unop, val, res); … … 993 1110 } 994 1111 1112 /** Evaluate unary operation on bool argument. 1113 * 1114 * @param run Runner object 1115 * @param unop Unary operation 1116 * @param val Value of argument 1117 * @param res Place to store result 1118 */ 1119 static void run_unop_bool(run_t *run, stree_unop_t *unop, rdata_value_t *val, 1120 rdata_item_t **res) 1121 { 1122 rdata_item_t *item; 1123 rdata_value_t *value; 1124 rdata_var_t *var; 1125 rdata_bool_t *bool_v; 1126 1127 (void) run; 1128 1129 item = rdata_item_new(ic_value); 1130 value = rdata_value_new(); 1131 var = rdata_var_new(vc_bool); 1132 bool_v = rdata_bool_new(); 1133 1134 item->u.value = value; 1135 value->var = var; 1136 var->u.bool_v = bool_v; 1137 1138 switch (unop->uc) { 1139 case uo_plus: 1140 case uo_minus: 1141 assert(b_false); 1142 1143 case uo_not: 1144 bool_v->value = !val->var->u.bool_v->value; 1145 break; 1146 } 1147 1148 *res = item; 1149 } 1150 995 1151 /** Evaluate unary operation on int argument. 996 1152 * … … 1027 1183 &int_v->value); 1028 1184 break; 1185 case uo_not: 1186 assert(b_false); 1029 1187 } 1030 1188 1031 1189 *res = item; 1032 1190 } 1033 1034 1191 1035 1192 /** Evaluate @c new operation. … … 1186 1343 { 1187 1344 stree_csi_t *csi; 1345 rdata_item_t *obj_i; 1346 list_t arg_vals; 1188 1347 1189 1348 #ifdef DEBUG_RUN_TRACE 1190 1349 printf("Create new object.\n"); 1191 1350 #endif 1192 (void) new_op;1193 1194 1351 /* Lookup object CSI. */ 1195 1352 assert(titem->tic == tic_tobject); 1196 1353 csi = titem->u.tobject->csi; 1197 1354 1355 /* Evaluate constructor arguments. */ 1356 run_call_args(run, &new_op->ctor_args, &arg_vals); 1357 if (run_is_bo(run)) { 1358 *res = NULL; 1359 return; 1360 } 1361 1198 1362 /* Create CSI instance. */ 1199 1363 run_new_csi_inst(run, csi, res); 1364 1365 /* Run the constructor. */ 1366 run_dereference(run, *res, NULL, &obj_i); 1367 assert(obj_i->ic == ic_address); 1368 assert(obj_i->u.address->ac == ac_var); 1369 run_object_ctor(run, obj_i->u.address->u.var_a->vref, &arg_vals); 1200 1370 } 1201 1371 … … 1256 1426 run_access_object(run, access, arg, res); 1257 1427 break; 1258 default: 1428 case vc_symbol: 1429 run_access_symbol(run, access, arg, res); 1430 break; 1431 1432 case vc_bool: 1433 case vc_char: 1434 case vc_enum: 1435 case vc_int: 1436 case vc_string: 1437 case vc_array: 1438 case vc_resource: 1259 1439 printf("Unimplemented: Using access operator ('.') " 1260 1440 "with unsupported data type (value/%d).\n", vc); … … 1276 1456 1277 1457 /* Implicitly dereference. */ 1278 run_dereference(run, arg, &darg);1458 run_dereference(run, arg, access->arg->cspan, &darg); 1279 1459 1280 1460 if (run->thread_ar->bo_mode != bm_none) { … … 1396 1576 printf("Error: Accessing object member which is a delegate.\n"); 1397 1577 exit(1); 1578 case sc_enum: 1579 printf("Error: Accessing object member which is an enum.\n"); 1580 exit(1); 1581 case sc_ctor: 1582 /* It is not possible to reference a constructor explicitly. */ 1583 assert(b_false); 1398 1584 case sc_fun: 1399 1585 /* Construct anonymous delegate. */ … … 1442 1628 } 1443 1629 1630 /** Evaluate symbol member acccess. 1631 * 1632 * @param run Runner object 1633 * @param access Access operation 1634 * @param arg Evaluated base expression 1635 * @param res Place to store result 1636 */ 1637 static void run_access_symbol(run_t *run, stree_access_t *access, 1638 rdata_item_t *arg, rdata_item_t **res) 1639 { 1640 rdata_item_t *arg_vi; 1641 rdata_value_t *arg_val; 1642 rdata_symbol_t *symbol_v; 1643 stree_embr_t *embr; 1644 1645 rdata_item_t *ritem; 1646 rdata_value_t *rvalue; 1647 rdata_var_t *rvar; 1648 rdata_enum_t *enum_v; 1649 1650 #ifdef DEBUG_RUN_TRACE 1651 printf("Run symbol access operation.\n"); 1652 #endif 1653 run_cvt_value_item(run, arg, &arg_vi); 1654 arg_val = arg_vi->u.value; 1655 assert(arg_val->var->vc == vc_symbol); 1656 1657 symbol_v = arg_val->var->u.symbol_v; 1658 1659 /* XXX Port CSI symbol reference to using vc_symbol */ 1660 assert(symbol_v->sym->sc == sc_enum); 1661 1662 embr = stree_enum_find_mbr(symbol_v->sym->u.enum_d, 1663 access->member_name); 1664 1665 /* Member existence should be ensured by static type checking. */ 1666 assert(embr != NULL); 1667 1668 #ifdef DEBUG_RUN_TRACE 1669 printf("Found enum member '%s'.\n", 1670 strtab_get_str(access->member_name->sid)); 1671 #endif 1672 ritem = rdata_item_new(ic_value); 1673 rvalue = rdata_value_new(); 1674 rvar = rdata_var_new(vc_enum); 1675 enum_v = rdata_enum_new(); 1676 1677 ritem->u.value = rvalue; 1678 rvalue->var = rvar; 1679 rvar->u.enum_v = enum_v; 1680 enum_v->value = embr; 1681 1682 *res = ritem; 1683 } 1684 1444 1685 /** Call a function. 1445 1686 * … … 1455 1696 rdata_deleg_t *deleg_v; 1456 1697 list_t arg_vals; 1457 list_node_t *node;1458 stree_expr_t *arg;1459 rdata_item_t *rarg_i, *rarg_vi;1460 1698 1461 1699 stree_fun_t *fun; … … 1499 1737 #endif 1500 1738 /* Evaluate function arguments. */ 1739 run_call_args(run, &call->args, &arg_vals); 1740 if (run_is_bo(run)) { 1741 *res = NULL; 1742 return; 1743 } 1744 1745 fun = symbol_to_fun(deleg_v->sym); 1746 assert(fun != NULL); 1747 1748 /* Create procedure activation record. */ 1749 run_proc_ar_create(run, deleg_v->obj, fun->proc, &proc_ar); 1750 1751 /* Fill in argument values. */ 1752 run_proc_ar_set_args(run, proc_ar, &arg_vals); 1753 1754 /* Run the function. */ 1755 run_proc(run, proc_ar, res); 1756 1757 if (!run_is_bo(run) && fun->sig->rtype != NULL && *res == NULL) { 1758 printf("Error: Function '"); 1759 symbol_print_fqn(deleg_v->sym); 1760 printf("' did not return a value.\n"); 1761 exit(1); 1762 } 1763 1764 #ifdef DEBUG_RUN_TRACE 1765 printf("Returned from function call.\n"); 1766 #endif 1767 } 1768 1769 /** Evaluate call arguments. 1770 * 1771 * Evaluate arguments to function or constructor. 1772 * 1773 * @param run Runner object 1774 * @param args Real arguments (list of stree_expr_t) 1775 * @param arg_vals Address of uninitialized list to store argument values 1776 * (list of rdata_item_t). 1777 */ 1778 static void run_call_args(run_t *run, list_t *args, list_t *arg_vals) 1779 { 1780 list_node_t *arg_n; 1781 stree_expr_t *arg; 1782 rdata_item_t *rarg_i, *rarg_vi; 1783 1784 /* Evaluate function arguments. */ 1785 list_init(arg_vals); 1786 arg_n = list_first(args); 1787 1788 while (arg_n != NULL) { 1789 arg = list_node_data(arg_n, stree_expr_t *); 1790 run_expr(run, arg, &rarg_i); 1791 if (run_is_bo(run)) 1792 return; 1793 1794 run_cvt_value_item(run, rarg_i, &rarg_vi); 1795 1796 list_append(arg_vals, rarg_vi); 1797 arg_n = list_next(args, arg_n); 1798 } 1799 } 1800 1801 /** Run index operation. 1802 * 1803 * Evaluate operation per the indexing ('[', ']') operator. 1804 * 1805 * @param run Runner object 1806 * @param index Index operation 1807 * @param res Place to store result 1808 */ 1809 static void run_index(run_t *run, stree_index_t *index, rdata_item_t **res) 1810 { 1811 rdata_item_t *rbase; 1812 rdata_item_t *base_i; 1813 list_node_t *node; 1814 stree_expr_t *arg; 1815 rdata_item_t *rarg_i, *rarg_vi; 1816 var_class_t vc; 1817 list_t arg_vals; 1818 1819 #ifdef DEBUG_RUN_TRACE 1820 printf("Run index operation.\n"); 1821 #endif 1822 run_expr(run, index->base, &rbase); 1823 if (run_is_bo(run)) { 1824 *res = NULL; 1825 return; 1826 } 1827 1828 vc = run_item_get_vc(run, rbase); 1829 1830 /* Implicitly dereference. */ 1831 if (vc == vc_ref) { 1832 run_dereference(run, rbase, index->base->cspan, &base_i); 1833 if (run_is_bo(run)) { 1834 *res = NULL; 1835 return; 1836 } 1837 } else { 1838 base_i = rbase; 1839 } 1840 1841 vc = run_item_get_vc(run, base_i); 1842 1843 /* Evaluate arguments (indices). */ 1844 node = list_first(&index->args); 1501 1845 list_init(&arg_vals); 1502 node = list_first(&call->args);1503 1846 1504 1847 while (node != NULL) { … … 1513 1856 1514 1857 list_append(&arg_vals, rarg_vi); 1515 node = list_next(&call->args, node);1516 }1517 1518 fun = symbol_to_fun(deleg_v->sym);1519 assert(fun != NULL);1520 1521 /* Create procedure activation record. */1522 run_proc_ar_create(run, deleg_v->obj, fun->proc, &proc_ar);1523 1524 /* Fill in argument values. */1525 run_proc_ar_set_args(run, proc_ar, &arg_vals);1526 1527 /* Run the function. */1528 run_proc(run, proc_ar, res);1529 1530 #ifdef DEBUG_RUN_TRACE1531 printf("Returned from function call.\n");1532 #endif1533 }1534 1535 /** Run index operation.1536 *1537 * Evaluate operation per the indexing ('[', ']') operator.1538 *1539 * @param run Runner object1540 * @param index Index operation1541 * @param res Place to store result1542 */1543 static void run_index(run_t *run, stree_index_t *index, rdata_item_t **res)1544 {1545 rdata_item_t *rbase;1546 rdata_item_t *base_i;1547 list_node_t *node;1548 stree_expr_t *arg;1549 rdata_item_t *rarg_i, *rarg_vi;1550 var_class_t vc;1551 list_t arg_vals;1552 1553 #ifdef DEBUG_RUN_TRACE1554 printf("Run index operation.\n");1555 #endif1556 run_expr(run, index->base, &rbase);1557 if (run_is_bo(run)) {1558 *res = NULL;1559 return;1560 }1561 1562 vc = run_item_get_vc(run, rbase);1563 1564 /* Implicitly dereference. */1565 if (vc == vc_ref) {1566 run_dereference(run, rbase, &base_i);1567 } else {1568 base_i = rbase;1569 }1570 1571 vc = run_item_get_vc(run, base_i);1572 1573 /* Evaluate arguments (indices). */1574 node = list_first(&index->args);1575 list_init(&arg_vals);1576 1577 while (node != NULL) {1578 arg = list_node_data(node, stree_expr_t *);1579 run_expr(run, arg, &rarg_i);1580 if (run_is_bo(run)) {1581 *res = NULL;1582 return;1583 }1584 1585 run_cvt_value_item(run, rarg_i, &rarg_vi);1586 1587 list_append(&arg_vals, rarg_vi);1588 1858 1589 1859 node = list_next(&index->args, node); … … 1634 1904 #endif 1635 1905 (void) run; 1636 (void) index;1637 1906 1638 1907 assert(base->ic == ic_address); … … 1676 1945 /* Raise Error.OutOfBounds */ 1677 1946 run_raise_exc(run, 1678 run->program->builtin->error_outofbounds); 1947 run->program->builtin->error_outofbounds, 1948 index->expr->cspan); 1949 /* XXX It should be cspan of the argument. */ 1679 1950 *res = run_recovery_item(run); 1680 1951 return; … … 1814 2085 #endif 1815 2086 (void) run; 1816 (void) index;1817 2087 1818 2088 run_cvt_value_item(run, base, &base_vi); … … 1866 2136 #endif 1867 2137 /* Raise Error.OutOfBounds */ 1868 run_raise_exc(run, run->program->builtin->error_outofbounds); 2138 run_raise_exc(run, run->program->builtin->error_outofbounds, 2139 index->expr->cspan); 1869 2140 *res = run_recovery_item(run); 1870 2141 return; … … 1970 2241 } 1971 2242 1972 run_dereference(run, rarg_vi, &rarg_di);2243 run_dereference(run, rarg_vi, NULL, &rarg_di); 1973 2244 1974 2245 /* Now we should have a variable address. */ … … 2048 2319 case vc_ref: 2049 2320 case vc_deleg: 2321 case vc_enum: 2050 2322 case vc_array: 2051 2323 case vc_object: 2052 2324 case vc_resource: 2325 case vc_symbol: 2053 2326 assert(b_false); 2054 2327 } … … 2147 2420 } 2148 2421 2422 /** Run constructor on an object. 2423 * 2424 * @param run Runner object 2425 * @param obj Object to run constructor on 2426 * @param arg_vals Argument values (list of rdata_item_t) 2427 */ 2428 static void run_object_ctor(run_t *run, rdata_var_t *obj, list_t *arg_vals) 2429 { 2430 stree_ident_t *ctor_ident; 2431 stree_symbol_t *csi_sym; 2432 stree_csi_t *csi; 2433 stree_symbol_t *ctor_sym; 2434 stree_ctor_t *ctor; 2435 run_proc_ar_t *proc_ar; 2436 rdata_item_t *res; 2437 2438 csi_sym = obj->u.object_v->class_sym; 2439 csi = symbol_to_csi(csi_sym); 2440 assert(csi != NULL); 2441 2442 #ifdef DEBUG_RUN_TRACE 2443 printf("Run object constructor from CSI '"); 2444 symbol_print_fqn(csi_sym); 2445 printf("'.\n"); 2446 #endif 2447 ctor_ident = stree_ident_new(); 2448 ctor_ident->sid = strtab_get_sid(CTOR_IDENT); 2449 2450 /* Find constructor. */ 2451 ctor_sym = symbol_search_csi_no_base(run->program, csi, ctor_ident); 2452 if (ctor_sym == NULL) { 2453 #ifdef DEBUG_RUN_TRACE 2454 printf("No constructor found.\n"); 2455 #endif 2456 return; 2457 } 2458 2459 ctor = symbol_to_ctor(ctor_sym); 2460 assert(ctor != NULL); 2461 2462 /* Create procedure activation record. */ 2463 run_proc_ar_create(run, obj, ctor->proc, &proc_ar); 2464 2465 /* Fill in argument values. */ 2466 run_proc_ar_set_args(run, proc_ar, arg_vals); 2467 2468 /* Run the procedure. */ 2469 run_proc(run, proc_ar, &res); 2470 2471 /* Constructor does not return a value. */ 2472 assert(res == NULL); 2473 2474 #ifdef DEBUG_RUN_TRACE 2475 printf("Returned from constructor..\n"); 2476 #endif 2477 } 2478 2149 2479 /** Return boolean value of an item. 2150 2480 *
Note:
See TracChangeset
for help on using the changeset viewer.