Changeset ecb6ac32 in mainline for uspace/app/sbi/src/run_expr.c


Ignore:
Timestamp:
2010-04-04T22:32:39Z (14 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
0a72efc
Parents:
73060801 (diff), 23de644 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge from lp:~jsvoboda/helenos/sysel.

File:
1 edited

Legend:

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

    r73060801 recb6ac32  
    3232#include <stdlib.h>
    3333#include <assert.h>
     34#include "bigint.h"
    3435#include "debug.h"
    3536#include "intmap.h"
     
    7172
    7273static void run_unop(run_t *run, stree_unop_t *unop, rdata_item_t **res);
     74static void run_unop_int(run_t *run, stree_unop_t *unop, rdata_value_t *val,
     75    rdata_item_t **res);
     76
    7377static void run_new(run_t *run, stree_new_t *new_op, rdata_item_t **res);
    7478static void run_new_array(run_t *run, stree_new_t *new_op,
     
    349353        value->var = var;
    350354        var->u.int_v = int_v;
    351         int_v->value = lit_int->value;
     355        bigint_clone(&lit_int->value, &int_v->value);
    352356
    353357        *res = item;
     
    436440#endif
    437441        run_expr(run, binop->arg1, &rarg1_i);
     442        if (run_is_bo(run)) {
     443                *res = NULL;
     444                return;
     445        }
     446
    438447        run_expr(run, binop->arg2, &rarg2_i);
     448        if (run_is_bo(run)) {
     449                *res = NULL;
     450                return;
     451        }
    439452
    440453        switch (binop->bc) {
    441454        case bo_plus:
     455        case bo_minus:
     456        case bo_mult:
    442457        case bo_equal:
    443458        case bo_notequal:
     
    496511        rdata_int_t *int_v;
    497512
    498         int i1, i2;
     513        bigint_t *i1, *i2;
     514        bigint_t diff;
     515        bool_t done;
     516        bool_t zf, nf;
    499517
    500518        (void) run;
     
    509527        var->u.int_v = int_v;
    510528
    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;
    513533
    514534        switch (binop->bc) {
    515535        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);
    518559
    519560        /* XXX We should have a real boolean type. */
     561        switch (binop->bc) {
    520562        case bo_equal:
    521                 int_v->value = (i1 == i2) ? 1 : 0;
     563                bigint_init(&int_v->value, zf ? 1 : 0);
    522564                break;
    523565        case bo_notequal:
    524                 int_v->value = (i1 != i2) ? 1 : 0;
     566                bigint_init(&int_v->value, !zf ? 1 : 0);
    525567                break;
    526568        case bo_lt:
    527                 int_v->value = (i1 < i2) ? 1 : 0;
     569                bigint_init(&int_v->value, (!zf && nf) ? 1 : 0);
    528570                break;
    529571        case bo_gt:
    530                 int_v->value = (i1 > i2) ? 1 : 0;
     572                bigint_init(&int_v->value, (!zf && !nf) ? 1 : 0);
    531573                break;
    532574        case bo_lt_equal:
    533                 int_v->value = (i1 <= i2) ? 1 : 0;
     575                bigint_init(&int_v->value, (zf || nf) ? 1 : 0);
    534576                break;
    535577        case bo_gt_equal:
    536                 int_v->value = (i1 >= i2) ? 1 : 0;
     578                bigint_init(&int_v->value, !nf ? 1 : 0);
    537579                break;
    538580        default:
     
    610652        /* XXX We should have a real boolean type. */
    611653        case bo_equal:
    612                 int_v->value = (ref1 == ref2) ? 1 : 0;
     654                bigint_init(&int_v->value, (ref1 == ref2) ? 1 : 0);
    613655                break;
    614656        case bo_notequal:
    615                 int_v->value = (ref1 != ref2) ? 1 : 0;
     657                bigint_init(&int_v->value, (ref1 != ref2) ? 1 : 0);
    616658                break;
    617659        default:
     
    628670static void run_unop(run_t *run, stree_unop_t *unop, rdata_item_t **res)
    629671{
    630         rdata_item_t *rarg;
     672        rdata_item_t *rarg_i;
     673        rdata_item_t *rarg_vi;
     674        rdata_value_t *val;
    631675
    632676#ifdef DEBUG_RUN_TRACE
    633677        printf("Run unary operation.\n");
    634678#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. */
     706static 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
    638738
    639739/** Evaluate @c new operation. */
     
    680780        int length;
    681781        int i;
     782        int rc;
     783        int iextent;
    682784
    683785#ifdef DEBUG_RUN_TRACE
     
    708810                /* Evaluate extent argument. */
    709811                run_expr(run, expr, &rexpr);
     812                if (run_is_bo(run)) {
     813                        *res = NULL;
     814                        return;
     815                }
     816
    710817                run_cvt_value_item(run, rexpr, &rexpr_vi);
    711818                assert(rexpr_vi->ic == ic_value);
     
    718825
    719826#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;
    723839                length = length * array->extent[i];
    724840
     
    738854                elem_var = rdata_var_new(vc_int);
    739855                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);
    741857
    742858                array->element[i] = elem_var;
     
    755871    tdata_item_t *titem, rdata_item_t **res)
    756872{
    757         rdata_object_t *obj;
    758         rdata_var_t *obj_var;
    759 
    760         stree_symbol_t *csi_sym;
    761873        stree_csi_t *csi;
    762         stree_csimbr_t *csimbr;
    763 
    764         rdata_var_t *mbr_var;
    765 
    766         list_node_t *node;
    767874
    768875#ifdef DEBUG_RUN_TRACE
    769876        printf("Create new object.\n");
    770877#endif
    771         (void) run;
    772878        (void) new_op;
    773879
     
    775881        assert(titem->tic == tic_tobject);
    776882        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);
    806886}
    807887
     
    815895#endif
    816896        run_expr(run, access->arg, &rarg);
     897        if (run_is_bo(run)) {
     898                *res = NULL;
     899                return;
     900        }
     901
    817902        if (rarg == NULL) {
    818903                printf("Error: Sub-expression has no value.\n");
     
    10281113#endif
    10291114        run_expr(run, call->fun, &rfun);
     1115        if (run_is_bo(run)) {
     1116                *res = NULL;
     1117                return;
     1118        }
    10301119
    10311120        if (run->thread_ar->bo_mode != bm_none) {
     
    10581147                arg = list_node_data(node, stree_expr_t *);
    10591148                run_expr(run, arg, &rarg_i);
     1149                if (run_is_bo(run)) {
     1150                        *res = NULL;
     1151                        return;
     1152                }
     1153
    10601154                run_cvt_value_item(run, rarg_i, &rarg_vi);
    10611155
     
    10961190#endif
    10971191        run_expr(run, index->base, &rbase);
     1192        if (run_is_bo(run)) {
     1193                *res = NULL;
     1194                return;
     1195        }
    10981196
    10991197        vc = run_item_get_vc(run, rbase);
     
    11151213                arg = list_node_data(node, stree_expr_t *);
    11161214                run_expr(run, arg, &rarg_i);
     1215                if (run_is_bo(run)) {
     1216                        *res = NULL;
     1217                        return;
     1218                }
     1219
    11171220                run_cvt_value_item(run, rarg_i, &rarg_vi);
    11181221
     
    11491252        int elem_index;
    11501253        int arg_val;
     1254        int rc;
    11511255
    11521256        rdata_item_t *ritem;
     
    11891293                }
    11901294
    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
    11941301                        printf("Error: Array index (value: %d) is out of range.\n",
    11951302                            arg_val);
    1196                         run_raise_error(run);
     1303#endif
     1304                        /* Raise Error.OutOfBounds */
     1305                        run_raise_exc(run,
     1306                            run->program->builtin->error_outofbounds);
    11971307                        *res = run_recovery_item(run);
    11981308                        return;
     
    13071417        int elem_index;
    13081418        int arg_val;
    1309         int rc;
     1419        int rc1, rc2;
    13101420
    13111421        rdata_value_t *value;
     
    13221432        run_cvt_value_item(run, base, &base_vi);
    13231433        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;
    13251435
    13261436        /*
     
    13461456                }
    13471457
    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
    13491462                elem_index = arg_val;
    13501463
     
    13581471        }
    13591472
    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) {
    13621477                printf("Error: String index (value: %d) is out of range.\n",
    13631478                    arg_val);
     
    13721487        cvar = rdata_var_new(vc_int);
    13731488        cvar->u.int_v = rdata_int_new();
    1374         cvar->u.int_v->value = cval;
     1489        bigint_init(&cvar->u.int_v->value, cval);
    13751490        value->var = cvar;
    13761491
     
    13891504#endif
    13901505        run_expr(run, assign->dest, &rdest_i);
     1506        if (run_is_bo(run)) {
     1507                *res = NULL;
     1508                return;
     1509        }
     1510
    13911511        run_expr(run, assign->src, &rsrc_i);
     1512        if (run_is_bo(run)) {
     1513                *res = NULL;
     1514                return;
     1515        }
    13921516
    13931517        run_cvt_value_item(run, rsrc_i, &rsrc_vi);
     
    14231547#endif
    14241548        run_expr(run, as_op->arg, &rarg_i);
     1549        if (run_is_bo(run)) {
     1550                *res = NULL;
     1551                return;
     1552        }
    14251553
    14261554        /*
     
    14671595
    14681596        *res = rarg_vi;
     1597}
     1598
     1599/** Create new CSI instance. */
     1600void 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);
    14691647}
    14701648
     
    14921670        }
    14931671
    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.