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


Ignore:
Timestamp:
2010-03-13T12:04:37Z (14 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
7715994
Parents:
94d484a
Message:

Update SBI to rev. 100.

File:
1 edited

Legend:

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

    r94d484a rd0febca  
    4141#include "run_texpr.h"
    4242#include "symbol.h"
     43#include "stree.h"
    4344#include "strtab.h"
    4445
     
    8788static void run_call(run_t *run, stree_call_t *call, rdata_item_t **res);
    8889static void run_index(run_t *run, stree_index_t *index, rdata_item_t **res);
     90static void run_index_array(run_t *run, stree_index_t *index,
     91    rdata_item_t *base, list_t *args, rdata_item_t **res);
     92static void run_index_object(run_t *run, stree_index_t *index,
     93    rdata_item_t *base, list_t *args, rdata_item_t **res);
     94static void run_index_string(run_t *run, stree_index_t *index,
     95    rdata_item_t *base, list_t *args, rdata_item_t **res);
    8996static void run_assign(run_t *run, stree_assign_t *assign, rdata_item_t **res);
    9097
     
    143150        rdata_item_t *item;
    144151        rdata_address_t *address;
     152        rdata_addr_var_t *addr_var;
    145153        rdata_value_t *value;
    146154        rdata_var_t *var;
    147155        rdata_deleg_t *deleg_v;
    148156
    149         run_fun_ar_t *fun_ar;
     157        run_proc_ar_t *proc_ar;
    150158        stree_symbol_t *csi_sym;
    151159        stree_csi_t *csi;
     
    164172                /* Found a local variable. */
    165173                item = rdata_item_new(ic_address);
    166                 address = rdata_address_new();
     174                address = rdata_address_new(ac_var);
     175                addr_var = rdata_addr_var_new();
    167176
    168177                item->u.address = address;
    169                 address->vref = var;
     178                address->u.var_a = addr_var;
     179                addr_var->vref = var;
    170180
    171181                *res = item;
     
    181191
    182192        /* Determine currently active object or CSI. */
    183         fun_ar = run_get_current_fun_ar(run);
    184         if (fun_ar->obj != NULL) {
    185                 assert(fun_ar->obj->vc == vc_object);
    186                 obj = fun_ar->obj->u.object_v;
     193        proc_ar = run_get_current_proc_ar(run);
     194        if (proc_ar->obj != NULL) {
     195                assert(proc_ar->obj->vc == vc_object);
     196                obj = proc_ar->obj->u.object_v;
    187197                csi_sym = obj->class_sym;
    188198                csi = symbol_to_csi(csi_sym);
    189199                assert(csi != NULL);
    190200        } else {
    191                 csi = fun_ar->fun_sym->outer_csi;
     201                csi = proc_ar->proc_sym->outer_csi;
    192202                obj = NULL;
    193203        }
     
    238248                var->u.deleg_v = deleg_v;
    239249
    240                 deleg_v->obj = fun_ar->obj;
     250                deleg_v->obj = proc_ar->obj;
    241251                deleg_v->sym = sym;
    242252
     
    270280                /* Return address of the variable. */
    271281                item = rdata_item_new(ic_address);
    272                 address = rdata_address_new();
     282                address = rdata_address_new(ac_var);
     283                addr_var = rdata_addr_var_new();
    273284
    274285                item->u.address = address;
    275                 address->vref = member_var;
     286                address->u.var_a = addr_var;
     287                addr_var->vref = member_var;
    276288
    277289                *res = item;
     
    393405    rdata_item_t **res)
    394406{
    395         run_fun_ar_t *fun_ar;
     407        run_proc_ar_t *proc_ar;
    396408
    397409#ifdef DEBUG_RUN_TRACE
     
    399411#endif
    400412        (void) self_ref;
    401         fun_ar = run_get_current_fun_ar(run);
     413        proc_ar = run_get_current_proc_ar(run);
    402414
    403415        /* Return reference to the currently active object. */
    404         rdata_reference(fun_ar->obj, res);
     416        run_reference(run, proc_ar->obj, res);
    405417}
    406418
     
    438450#endif
    439451
    440         rdata_cvt_value_item(rarg1_i, &rarg1_vi);
    441         rdata_cvt_value_item(rarg2_i, &rarg2_vi);
     452        run_cvt_value_item(run, rarg1_i, &rarg1_vi);
     453        run_cvt_value_item(run, rarg2_i, &rarg2_vi);
    442454
    443455        v1 = rarg1_vi->u.value;
     
    687699                /* Evaluate extent argument. */
    688700                run_expr(run, expr, &rexpr);
    689                 rdata_cvt_value_item(rexpr, &rexpr_vi);
     701                run_cvt_value_item(run, rexpr, &rexpr_vi);
    690702                assert(rexpr_vi->ic == ic_value);
    691703                rexpr_var = rexpr_vi->u.value->var;
     
    727739
    728740        /* Create reference to the new array. */
    729         rdata_reference(array_var, res);
     741        run_reference(run, array_var, res);
    730742}
    731743
     
    782794
    783795        /* Create reference to the new object. */
    784         rdata_reference(obj_var, res);
     796        run_reference(run, obj_var, res);
    785797}
    786798
     
    811823        printf("Run access operation on pre-evaluated base.\n");
    812824#endif
    813         switch (arg->ic) {
    814         case ic_value:
    815                 vc = arg->u.value->var->vc;
    816                 break;
    817         case ic_address:
    818                 vc = arg->u.address->vref->vc;
    819                 break;
    820         default:
    821                 /* Silence warning. */
    822                 abort();
    823         }
     825        vc = rdata_item_get_vc(arg);
    824826
    825827        switch (vc) {
     
    847849
    848850        /* Implicitly dereference. */
    849         rdata_dereference(arg, &darg);
     851        run_dereference(run, arg, &darg);
    850852
    851853        /* Try again. */
     
    865867        printf("Run delegate access operation.\n");
    866868#endif
    867         rdata_cvt_value_item(arg, &arg_vi);
     869        run_cvt_value_item(run, arg, &arg_vi);
    868870        arg_val = arg_vi->u.value;
    869871        assert(arg_val->var->vc == vc_deleg);
     
    906908{
    907909        stree_symbol_t *member;
     910        rdata_var_t *object_var;
    908911        rdata_object_t *object;
    909912        rdata_item_t *ritem;
    910913        rdata_address_t *address;
     914        rdata_addr_var_t *addr_var;
     915        rdata_addr_prop_t *addr_prop;
     916        rdata_aprop_named_t *aprop_named;
     917        rdata_deleg_t *deleg_p;
    911918
    912919        rdata_value_t *value;
     
    918925#endif
    919926        assert(arg->ic == ic_address);
    920         assert(arg->u.value->var->vc == vc_object);
    921 
    922         object = arg->u.value->var->u.object_v;
     927        assert(arg->u.address->ac == ac_var);
     928        assert(arg->u.address->u.var_a->vref->vc == vc_object);
     929
     930        object_var = arg->u.address->u.var_a->vref;
     931        object = object_var->u.object_v;
    923932
    924933        member = symbol_search_csi(run->program, object->class_sym->u.csi,
     
    953962                var->u.deleg_v = deleg_v;
    954963
    955                 deleg_v->obj = arg->u.value->var;
     964                deleg_v->obj = arg->u.address->u.var_a->vref;
    956965                deleg_v->sym = member;
    957966                break;
     
    959968                /* Construct variable address item. */
    960969                ritem = rdata_item_new(ic_address);
    961                 address = rdata_address_new();
     970                address = rdata_address_new(ac_var);
     971                addr_var = rdata_addr_var_new();
    962972                ritem->u.address = address;
    963 
    964                 address->vref = intmap_get(&object->fields,
     973                address->u.var_a = addr_var;
     974
     975                addr_var->vref = intmap_get(&object->fields,
    965976                    access->member_name->sid);
    966                 assert(address->vref != NULL);
     977                assert(addr_var->vref != NULL);
    967978                break;
    968979        case sc_prop:
    969                 printf("Unimplemented: Accessing object property.\n");
    970                 exit(1);
     980                /* Construct named property address. */
     981                ritem = rdata_item_new(ic_address);
     982                address = rdata_address_new(ac_prop);
     983                addr_prop = rdata_addr_prop_new(apc_named);
     984                aprop_named = rdata_aprop_named_new();
     985                ritem->u.address = address;
     986                address->u.prop_a = addr_prop;
     987                addr_prop->u.named = aprop_named;
     988
     989                deleg_p = rdata_deleg_new();
     990                deleg_p->obj = object_var;
     991                deleg_p->sym = member;
     992                addr_prop->u.named->prop_d = deleg_p;
     993                break;
    971994        }
    972995
     
    9851008
    9861009        stree_fun_t *fun;
    987         run_fun_ar_t *fun_ar;
     1010        run_proc_ar_t *proc_ar;
    9881011
    9891012#ifdef DEBUG_RUN_TRACE
     
    10171040                arg = list_node_data(node, stree_expr_t *);
    10181041                run_expr(run, arg, &rarg_i);
    1019                 rdata_cvt_value_item(rarg_i, &rarg_vi);
     1042                run_cvt_value_item(run, rarg_i, &rarg_vi);
    10201043
    10211044                list_append(&arg_vals, rarg_vi);
     
    10261049        assert(fun != NULL);
    10271050
    1028         /* Create function activation record. */
    1029         run_fun_ar_create(run, deleg_v->obj, fun, &fun_ar);
     1051        /* Create procedure activation record. */
     1052        run_proc_ar_create(run, deleg_v->obj, deleg_v->sym, fun->body,
     1053            &proc_ar);
    10301054
    10311055        /* Fill in argument values. */
    1032         run_fun_ar_set_args(run, fun_ar, &arg_vals);
     1056        run_proc_ar_set_args(run, proc_ar, &arg_vals);
    10331057
    10341058        /* Run the function. */
    1035         run_fun(run, fun_ar, res);
     1059        run_proc(run, proc_ar, res);
    10361060
    10371061#ifdef DEBUG_RUN_TRACE
     
    10481072        stree_expr_t *arg;
    10491073        rdata_item_t *rarg_i, *rarg_vi;
     1074        var_class_t vc;
     1075        list_t arg_vals;
     1076
     1077#ifdef DEBUG_RUN_TRACE
     1078        printf("Run index operation.\n");
     1079#endif
     1080        run_expr(run, index->base, &rbase);
     1081
     1082        vc = rdata_item_get_vc(rbase);
     1083
     1084        /* Implicitly dereference. */
     1085        if (vc == vc_ref) {
     1086                run_dereference(run, rbase, &base_i);
     1087        } else {
     1088                base_i = rbase;
     1089        }
     1090
     1091        vc = rdata_item_get_vc(base_i);
     1092
     1093        /* Evaluate arguments (indices). */
     1094        node = list_first(&index->args);
     1095        list_init(&arg_vals);
     1096
     1097        while (node != NULL) {
     1098                arg = list_node_data(node, stree_expr_t *);
     1099                run_expr(run, arg, &rarg_i);
     1100                run_cvt_value_item(run, rarg_i, &rarg_vi);
     1101
     1102                list_append(&arg_vals, rarg_vi);
     1103
     1104                node = list_next(&index->args, node);
     1105        }
     1106
     1107        switch (vc) {
     1108        case vc_array:
     1109                run_index_array(run, index, base_i, &arg_vals, res);
     1110                break;
     1111        case vc_object:
     1112                run_index_object(run, index, base_i, &arg_vals, res);
     1113                break;
     1114        case vc_string:
     1115                run_index_string(run, index, base_i, &arg_vals, res);
     1116                break;
     1117        default:
     1118                printf("Error: Indexing object of bad type (%d).\n", vc);
     1119                exit(1);
     1120        }
     1121}
     1122
     1123/** Run index operation on array. */
     1124static void run_index_array(run_t *run, stree_index_t *index,
     1125    rdata_item_t *base, list_t *args, rdata_item_t **res)
     1126{
     1127        list_node_t *node;
    10501128        rdata_array_t *array;
    1051         var_class_t vc;
     1129        rdata_item_t *arg;
    10521130
    10531131        int i;
     
    10571135        rdata_item_t *ritem;
    10581136        rdata_address_t *address;
    1059 
    1060 #ifdef DEBUG_RUN_TRACE
    1061         printf("Run index operation.\n");
    1062 #endif
    1063         run_expr(run, index->base, &rbase);
    1064 
    1065         switch (rbase->ic) {
    1066         case ic_value:
    1067                 vc = rbase->u.value->var->vc;
    1068                 break;
    1069         case ic_address:
    1070                 vc = rbase->u.address->vref->vc;
    1071                 break;
    1072         default:
    1073                 /* Silence warning. */
    1074                 abort();
    1075         }
    1076 
    1077         if (vc != vc_ref) {
    1078                 printf("Error: Base of index operation is not a reference.\n");
    1079                 exit(1);
    1080         }
    1081 
    1082         rdata_dereference(rbase, &base_i);
    1083         assert(base_i->ic == ic_address);
    1084 
    1085         if (base_i->u.value->var->vc != vc_array) {
    1086                 printf("Error: Indexing something which is not an array.\n");
    1087                 exit(1);
    1088         }
    1089 
    1090         array = base_i->u.value->var->u.array_v;
    1091 
    1092         /* Evaluate arguments (indices). */
    1093         node = list_first(&index->args);
     1137        rdata_addr_var_t *addr_var;
     1138
     1139#ifdef DEBUG_RUN_TRACE
     1140        printf("Run array index operation.\n");
     1141#endif
     1142        (void) run;
     1143        (void) index;
     1144
     1145        assert(base->ic == ic_address);
     1146        assert(base->u.address->ac == ac_var);
     1147        assert(base->u.address->u.var_a->vref->vc == vc_array);
     1148        array = base->u.address->u.var_a->vref->u.array_v;
    10941149
    10951150        /*
     
    10991154        elem_index = 0;
    11001155
     1156        node = list_first(args);
    11011157        i = 0;
     1158
    11021159        while (node != NULL) {
    11031160                if (i >= array->rank) {
     
    11071164                }
    11081165
    1109                 arg = list_node_data(node, stree_expr_t *);
    1110                 run_expr(run, arg, &rarg_i);
    1111                 rdata_cvt_value_item(rarg_i, &rarg_vi);
    1112                 assert(rarg_vi->ic == ic_value);
    1113 
    1114                 if (rarg_vi->u.value->var->vc != vc_int) {
     1166                arg = list_node_data(node, rdata_item_t *);
     1167                assert(arg->ic == ic_value);
     1168
     1169                if (arg->u.value->var->vc != vc_int) {
    11151170                        printf("Error: Array index is not an integer.\n");
    11161171                        exit(1);
    11171172                }
    11181173
    1119                 arg_val = rarg_vi->u.value->var->u.int_v->value;
     1174                arg_val = arg->u.value->var->u.int_v->value;
    11201175
    11211176                if (arg_val < 0 || arg_val >= array->extent[i]) {
     
    11271182                elem_index = elem_index * array->extent[i] + arg_val;
    11281183
    1129                 node = list_next(&index->args, node);
     1184                node = list_next(args, node);
    11301185                i += 1;
    11311186        }
     
    11391194        /* Construct variable address item. */
    11401195        ritem = rdata_item_new(ic_address);
    1141         address = rdata_address_new();
     1196        address = rdata_address_new(ac_var);
     1197        addr_var = rdata_addr_var_new();
    11421198        ritem->u.address = address;
    1143 
    1144         address->vref = array->element[elem_index];
     1199        address->u.var_a = addr_var;
     1200
     1201        addr_var->vref = array->element[elem_index];
    11451202
    11461203        *res = ritem;
    11471204}
     1205
     1206/** Index an object (via its indexer). */
     1207static void run_index_object(run_t *run, stree_index_t *index,
     1208    rdata_item_t *base, list_t *args, rdata_item_t **res)
     1209{
     1210        rdata_item_t *ritem;
     1211        rdata_address_t *address;
     1212        rdata_addr_prop_t *addr_prop;
     1213        rdata_aprop_indexed_t *aprop_indexed;
     1214        rdata_var_t *obj_var;
     1215        stree_csi_t *obj_csi;
     1216        rdata_deleg_t *object_d;
     1217        stree_symbol_t *indexer_sym;
     1218        stree_ident_t *indexer_ident;
     1219
     1220        list_node_t *node;
     1221        rdata_item_t *arg;
     1222
     1223#ifdef DEBUG_RUN_TRACE
     1224        printf("Run object index operation.\n");
     1225#endif
     1226        (void) run;
     1227        (void) index;
     1228
     1229        /* Construct property address item. */
     1230        ritem = rdata_item_new(ic_address);
     1231        address = rdata_address_new(ac_prop);
     1232        addr_prop = rdata_addr_prop_new(apc_indexed);
     1233        aprop_indexed = rdata_aprop_indexed_new();
     1234        ritem->u.address = address;
     1235        address->u.prop_a = addr_prop;
     1236        addr_prop->u.indexed = aprop_indexed;
     1237
     1238        if (base->ic != ic_address || base->u.address->ac != ac_var) {
     1239                /* XXX Several other cases can occur. */
     1240                printf("Unimplemented: Indexing object varclass via something "
     1241                    "which is not a simple variable reference.\n");
     1242                exit(1);
     1243        }
     1244
     1245        /* Find indexer symbol. */
     1246        obj_var = base->u.address->u.var_a->vref;
     1247        assert(obj_var->vc == vc_object);
     1248        indexer_ident = stree_ident_new();
     1249        indexer_ident->sid = strtab_get_sid(INDEXER_IDENT);
     1250        obj_csi = symbol_to_csi(obj_var->u.object_v->class_sym);
     1251        assert(obj_csi != NULL);
     1252        indexer_sym = symbol_search_csi(run->program, obj_csi, indexer_ident);
     1253
     1254        /* Construct delegate. */
     1255        object_d = rdata_deleg_new();
     1256        object_d->obj = obj_var;
     1257        object_d->sym = indexer_sym;
     1258        aprop_indexed->object_d = object_d;
     1259
     1260        /* Copy list of argument values. */
     1261        list_init(&aprop_indexed->args);
     1262
     1263        node = list_first(args);
     1264        while (node != NULL) {
     1265                arg = list_node_data(node, rdata_item_t *);
     1266                list_append(&aprop_indexed->args, arg);
     1267                node = list_next(args, node);
     1268        }
     1269
     1270        *res = ritem;
     1271}
     1272
     1273/** Run index operation on string. */
     1274static void run_index_string(run_t *run, stree_index_t *index,
     1275    rdata_item_t *base, list_t *args, rdata_item_t **res)
     1276{
     1277        list_node_t *node;
     1278        rdata_string_t *string;
     1279        rdata_item_t *base_vi;
     1280        rdata_item_t *arg;
     1281
     1282        int i;
     1283        int elem_index;
     1284        int arg_val;
     1285        int rc;
     1286
     1287        rdata_value_t *value;
     1288        rdata_var_t *cvar;
     1289        rdata_item_t *ritem;
     1290        int cval;
     1291
     1292#ifdef DEBUG_RUN_TRACE
     1293        printf("Run string index operation.\n");
     1294#endif
     1295        (void) run;
     1296        (void) index;
     1297
     1298        run_cvt_value_item(run, base, &base_vi);
     1299        assert(base_vi->u.value->var->vc == vc_string);
     1300        string = base->u.value->var->u.string_v;
     1301
     1302        /*
     1303         * Linear index of the desired element. Elements are stored in
     1304         * lexicographic order with the last index changing the fastest.
     1305         */
     1306        node = list_first(args);
     1307        elem_index = 0;
     1308
     1309        i = 0;
     1310        while (node != NULL) {
     1311                if (i >= 1) {
     1312                        printf("Error: Too many indices string.\n");
     1313                        exit(1);
     1314                }
     1315
     1316                arg = list_node_data(node, rdata_item_t *);
     1317                assert(arg->ic == ic_value);
     1318
     1319                if (arg->u.value->var->vc != vc_int) {
     1320                        printf("Error: String index is not an integer.\n");
     1321                        exit(1);
     1322                }
     1323
     1324                arg_val = arg->u.value->var->u.int_v->value;
     1325                elem_index = arg_val;
     1326
     1327                node = list_next(args, node);
     1328                i += 1;
     1329        }
     1330
     1331        if (i < 1) {
     1332                printf("Error: Too few indices for string.\n");
     1333                exit(1);
     1334        }
     1335
     1336        rc = os_str_get_char(string->value, elem_index, &cval);
     1337        if (rc != EOK) {
     1338                printf("Error: String index (value: %d) is out of range.\n",
     1339                    arg_val);
     1340                exit(1);
     1341        }
     1342
     1343        /* Construct character value. */
     1344        ritem = rdata_item_new(ic_value);
     1345        value = rdata_value_new();
     1346        ritem->u.value = value;
     1347
     1348        cvar = rdata_var_new(vc_int);
     1349        cvar->u.int_v = rdata_int_new();
     1350        cvar->u.int_v->value = cval;
     1351        value->var = cvar;
     1352
     1353        *res = ritem;
     1354}
     1355
    11481356
    11491357/** Execute assignment. */
     
    11601368        run_expr(run, assign->src, &rsrc_i);
    11611369
    1162         rdata_cvt_value_item(rsrc_i, &rsrc_vi);
     1370        run_cvt_value_item(run, rsrc_i, &rsrc_vi);
     1371        assert(rsrc_vi->ic == ic_value);
    11631372        src_val = rsrc_vi->u.value;
    11641373
     
    11691378        }
    11701379
    1171         rdata_address_write(rdest_i->u.address, rsrc_vi->u.value);
     1380        run_address_write(run, rdest_i->u.address, rsrc_vi->u.value);
    11721381
    11731382        *res = NULL;
     
    11871396
    11881397        (void) run;
    1189         rdata_cvt_value_item(item, &vitem);
     1398        run_cvt_value_item(run, item, &vitem);
    11901399
    11911400        assert(vitem->ic == ic_value);
Note: See TracChangeset for help on using the changeset viewer.