Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/sbi/src/run_expr.c

    r23de644 r1ebc1a62  
    3232#include <stdlib.h>
    3333#include <assert.h>
    34 #include "bigint.h"
    3534#include "debug.h"
    3635#include "intmap.h"
     
    7271
    7372static 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 
    7773static void run_new(run_t *run, stree_new_t *new_op, rdata_item_t **res);
    7874static void run_new_array(run_t *run, stree_new_t *new_op,
     
    353349        value->var = var;
    354350        var->u.int_v = int_v;
    355         bigint_clone(&lit_int->value, &int_v->value);
     351        int_v->value = lit_int->value;
    356352
    357353        *res = item;
     
    440436#endif
    441437        run_expr(run, binop->arg1, &rarg1_i);
    442         if (run_is_bo(run)) {
    443                 *res = NULL;
    444                 return;
    445         }
    446 
    447438        run_expr(run, binop->arg2, &rarg2_i);
    448         if (run_is_bo(run)) {
    449                 *res = NULL;
    450                 return;
    451         }
    452439
    453440        switch (binop->bc) {
    454441        case bo_plus:
    455         case bo_minus:
    456         case bo_mult:
    457442        case bo_equal:
    458443        case bo_notequal:
     
    511496        rdata_int_t *int_v;
    512497
    513         bigint_t *i1, *i2;
    514         bigint_t diff;
    515         bool_t done;
    516         bool_t zf, nf;
     498        int i1, i2;
    517499
    518500        (void) run;
     
    527509        var->u.int_v = int_v;
    528510
    529         i1 = &v1->var->u.int_v->value;
    530         i2 = &v2->var->u.int_v->value;
    531 
    532         done = b_true;
     511        i1 = v1->var->u.int_v->value;
     512        i2 = v2->var->u.int_v->value;
    533513
    534514        switch (binop->bc) {
    535515        case bo_plus:
    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);
     516                int_v->value = i1 + i2;
     517                break;
    559518
    560519        /* XXX We should have a real boolean type. */
    561         switch (binop->bc) {
    562520        case bo_equal:
    563                 bigint_init(&int_v->value, zf ? 1 : 0);
     521                int_v->value = (i1 == i2) ? 1 : 0;
    564522                break;
    565523        case bo_notequal:
    566                 bigint_init(&int_v->value, !zf ? 1 : 0);
     524                int_v->value = (i1 != i2) ? 1 : 0;
    567525                break;
    568526        case bo_lt:
    569                 bigint_init(&int_v->value, (!zf && nf) ? 1 : 0);
     527                int_v->value = (i1 < i2) ? 1 : 0;
    570528                break;
    571529        case bo_gt:
    572                 bigint_init(&int_v->value, (!zf && !nf) ? 1 : 0);
     530                int_v->value = (i1 > i2) ? 1 : 0;
    573531                break;
    574532        case bo_lt_equal:
    575                 bigint_init(&int_v->value, (zf || nf) ? 1 : 0);
     533                int_v->value = (i1 <= i2) ? 1 : 0;
    576534                break;
    577535        case bo_gt_equal:
    578                 bigint_init(&int_v->value, !nf ? 1 : 0);
     536                int_v->value = (i1 >= i2) ? 1 : 0;
    579537                break;
    580538        default:
     
    652610        /* XXX We should have a real boolean type. */
    653611        case bo_equal:
    654                 bigint_init(&int_v->value, (ref1 == ref2) ? 1 : 0);
     612                int_v->value = (ref1 == ref2) ? 1 : 0;
    655613                break;
    656614        case bo_notequal:
    657                 bigint_init(&int_v->value, (ref1 != ref2) ? 1 : 0);
     615                int_v->value = (ref1 != ref2) ? 1 : 0;
    658616                break;
    659617        default:
     
    670628static void run_unop(run_t *run, stree_unop_t *unop, rdata_item_t **res)
    671629{
    672         rdata_item_t *rarg_i;
    673         rdata_item_t *rarg_vi;
    674         rdata_value_t *val;
     630        rdata_item_t *rarg;
    675631
    676632#ifdef DEBUG_RUN_TRACE
    677633        printf("Run unary operation.\n");
    678634#endif
    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 
     635        run_expr(run, unop->arg, &rarg);
     636        *res = NULL;
     637}
    738638
    739639/** Evaluate @c new operation. */
     
    780680        int length;
    781681        int i;
    782         int rc;
    783         int iextent;
    784682
    785683#ifdef DEBUG_RUN_TRACE
     
    810708                /* Evaluate extent argument. */
    811709                run_expr(run, expr, &rexpr);
    812                 if (run_is_bo(run)) {
    813                         *res = NULL;
    814                         return;
    815                 }
    816 
    817710                run_cvt_value_item(run, rexpr, &rexpr_vi);
    818711                assert(rexpr_vi->ic == ic_value);
     
    825718
    826719#ifdef DEBUG_RUN_TRACE
    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;
     720                printf("Array extent: %d.\n", rexpr_var->u.int_v->value);
     721#endif
     722                array->extent[i] = rexpr_var->u.int_v->value;
    839723                length = length * array->extent[i];
    840724
     
    854738                elem_var = rdata_var_new(vc_int);
    855739                elem_var->u.int_v = rdata_int_new();
    856                 bigint_init(&elem_var->u.int_v->value, 0);
     740                elem_var->u.int_v->value = 0;
    857741
    858742                array->element[i] = elem_var;
     
    871755    tdata_item_t *titem, rdata_item_t **res)
    872756{
     757        rdata_object_t *obj;
     758        rdata_var_t *obj_var;
     759
     760        stree_symbol_t *csi_sym;
    873761        stree_csi_t *csi;
     762        stree_csimbr_t *csimbr;
     763
     764        rdata_var_t *mbr_var;
     765
     766        list_node_t *node;
    874767
    875768#ifdef DEBUG_RUN_TRACE
    876769        printf("Create new object.\n");
    877770#endif
     771        (void) run;
    878772        (void) new_op;
    879773
     
    881775        assert(titem->tic == tic_tobject);
    882776        csi = titem->u.tobject->csi;
    883 
    884         /* Create CSI instance. */
    885         run_new_csi_inst(run, csi, res);
     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);
    886806}
    887807
     
    895815#endif
    896816        run_expr(run, access->arg, &rarg);
    897         if (run_is_bo(run)) {
    898                 *res = NULL;
    899                 return;
    900         }
    901 
    902817        if (rarg == NULL) {
    903818                printf("Error: Sub-expression has no value.\n");
     
    11131028#endif
    11141029        run_expr(run, call->fun, &rfun);
    1115         if (run_is_bo(run)) {
    1116                 *res = NULL;
    1117                 return;
    1118         }
    11191030
    11201031        if (run->thread_ar->bo_mode != bm_none) {
     
    11471058                arg = list_node_data(node, stree_expr_t *);
    11481059                run_expr(run, arg, &rarg_i);
    1149                 if (run_is_bo(run)) {
    1150                         *res = NULL;
    1151                         return;
    1152                 }
    1153 
    11541060                run_cvt_value_item(run, rarg_i, &rarg_vi);
    11551061
     
    11901096#endif
    11911097        run_expr(run, index->base, &rbase);
    1192         if (run_is_bo(run)) {
    1193                 *res = NULL;
    1194                 return;
    1195         }
    11961098
    11971099        vc = run_item_get_vc(run, rbase);
     
    12131115                arg = list_node_data(node, stree_expr_t *);
    12141116                run_expr(run, arg, &rarg_i);
    1215                 if (run_is_bo(run)) {
    1216                         *res = NULL;
    1217                         return;
    1218                 }
    1219 
    12201117                run_cvt_value_item(run, rarg_i, &rarg_vi);
    12211118
     
    12521149        int elem_index;
    12531150        int arg_val;
    1254         int rc;
    12551151
    12561152        rdata_item_t *ritem;
     
    12931189                }
    12941190
    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
     1191                arg_val = arg->u.value->var->u.int_v->value;
     1192
     1193                if (arg_val < 0 || arg_val >= array->extent[i]) {
    13011194                        printf("Error: Array index (value: %d) is out of range.\n",
    13021195                            arg_val);
    1303 #endif
    1304                         /* Raise Error.OutOfBounds */
    1305                         run_raise_exc(run,
    1306                             run->program->builtin->error_outofbounds);
     1196                        run_raise_error(run);
    13071197                        *res = run_recovery_item(run);
    13081198                        return;
     
    14171307        int elem_index;
    14181308        int arg_val;
    1419         int rc1, rc2;
     1309        int rc;
    14201310
    14211311        rdata_value_t *value;
     
    14321322        run_cvt_value_item(run, base, &base_vi);
    14331323        assert(base_vi->u.value->var->vc == vc_string);
    1434         string = base_vi->u.value->var->u.string_v;
     1324        string = base->u.value->var->u.string_v;
    14351325
    14361326        /*
     
    14561346                }
    14571347
    1458                 rc1 = bigint_get_value_int(
    1459                     &arg->u.value->var->u.int_v->value,
    1460                     &arg_val);
    1461 
     1348                arg_val = arg->u.value->var->u.int_v->value;
    14621349                elem_index = arg_val;
    14631350
     
    14711358        }
    14721359
    1473         if (rc1 == EOK)
    1474                 rc2 = os_str_get_char(string->value, elem_index, &cval);
    1475 
    1476         if (rc1 != EOK || rc2 != EOK) {
     1360        rc = os_str_get_char(string->value, elem_index, &cval);
     1361        if (rc != EOK) {
    14771362                printf("Error: String index (value: %d) is out of range.\n",
    14781363                    arg_val);
     
    14871372        cvar = rdata_var_new(vc_int);
    14881373        cvar->u.int_v = rdata_int_new();
    1489         bigint_init(&cvar->u.int_v->value, cval);
     1374        cvar->u.int_v->value = cval;
    14901375        value->var = cvar;
    14911376
     
    15041389#endif
    15051390        run_expr(run, assign->dest, &rdest_i);
    1506         if (run_is_bo(run)) {
    1507                 *res = NULL;
    1508                 return;
    1509         }
    1510 
    15111391        run_expr(run, assign->src, &rsrc_i);
    1512         if (run_is_bo(run)) {
    1513                 *res = NULL;
    1514                 return;
    1515         }
    15161392
    15171393        run_cvt_value_item(run, rsrc_i, &rsrc_vi);
     
    15471423#endif
    15481424        run_expr(run, as_op->arg, &rarg_i);
    1549         if (run_is_bo(run)) {
    1550                 *res = NULL;
    1551                 return;
    1552         }
    15531425
    15541426        /*
     
    15951467
    15961468        *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);
    16471469}
    16481470
     
    16701492        }
    16711493
    1672         return !bigint_is_zero(&var->u.int_v->value);
    1673 }
     1494        return (var->u.int_v->value != 0);
     1495}
Note: See TracChangeset for help on using the changeset viewer.