Changeset d0febca in mainline for uspace/app/sbi/src/run_expr.c
- Timestamp:
- 2010-03-13T12:04:37Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 7715994
- Parents:
- 94d484a
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/sbi/src/run_expr.c
r94d484a rd0febca 41 41 #include "run_texpr.h" 42 42 #include "symbol.h" 43 #include "stree.h" 43 44 #include "strtab.h" 44 45 … … 87 88 static void run_call(run_t *run, stree_call_t *call, rdata_item_t **res); 88 89 static void run_index(run_t *run, stree_index_t *index, rdata_item_t **res); 90 static void run_index_array(run_t *run, stree_index_t *index, 91 rdata_item_t *base, list_t *args, rdata_item_t **res); 92 static void run_index_object(run_t *run, stree_index_t *index, 93 rdata_item_t *base, list_t *args, rdata_item_t **res); 94 static void run_index_string(run_t *run, stree_index_t *index, 95 rdata_item_t *base, list_t *args, rdata_item_t **res); 89 96 static void run_assign(run_t *run, stree_assign_t *assign, rdata_item_t **res); 90 97 … … 143 150 rdata_item_t *item; 144 151 rdata_address_t *address; 152 rdata_addr_var_t *addr_var; 145 153 rdata_value_t *value; 146 154 rdata_var_t *var; 147 155 rdata_deleg_t *deleg_v; 148 156 149 run_ fun_ar_t *fun_ar;157 run_proc_ar_t *proc_ar; 150 158 stree_symbol_t *csi_sym; 151 159 stree_csi_t *csi; … … 164 172 /* Found a local variable. */ 165 173 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(); 167 176 168 177 item->u.address = address; 169 address->vref = var; 178 address->u.var_a = addr_var; 179 addr_var->vref = var; 170 180 171 181 *res = item; … … 181 191 182 192 /* 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; 187 197 csi_sym = obj->class_sym; 188 198 csi = symbol_to_csi(csi_sym); 189 199 assert(csi != NULL); 190 200 } else { 191 csi = fun_ar->fun_sym->outer_csi;201 csi = proc_ar->proc_sym->outer_csi; 192 202 obj = NULL; 193 203 } … … 238 248 var->u.deleg_v = deleg_v; 239 249 240 deleg_v->obj = fun_ar->obj;250 deleg_v->obj = proc_ar->obj; 241 251 deleg_v->sym = sym; 242 252 … … 270 280 /* Return address of the variable. */ 271 281 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(); 273 284 274 285 item->u.address = address; 275 address->vref = member_var; 286 address->u.var_a = addr_var; 287 addr_var->vref = member_var; 276 288 277 289 *res = item; … … 393 405 rdata_item_t **res) 394 406 { 395 run_ fun_ar_t *fun_ar;407 run_proc_ar_t *proc_ar; 396 408 397 409 #ifdef DEBUG_RUN_TRACE … … 399 411 #endif 400 412 (void) self_ref; 401 fun_ar = run_get_current_fun_ar(run);413 proc_ar = run_get_current_proc_ar(run); 402 414 403 415 /* Return reference to the currently active object. */ 404 r data_reference(fun_ar->obj, res);416 run_reference(run, proc_ar->obj, res); 405 417 } 406 418 … … 438 450 #endif 439 451 440 r data_cvt_value_item(rarg1_i, &rarg1_vi);441 r data_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); 442 454 443 455 v1 = rarg1_vi->u.value; … … 687 699 /* Evaluate extent argument. */ 688 700 run_expr(run, expr, &rexpr); 689 r data_cvt_value_item(rexpr, &rexpr_vi);701 run_cvt_value_item(run, rexpr, &rexpr_vi); 690 702 assert(rexpr_vi->ic == ic_value); 691 703 rexpr_var = rexpr_vi->u.value->var; … … 727 739 728 740 /* Create reference to the new array. */ 729 r data_reference(array_var, res);741 run_reference(run, array_var, res); 730 742 } 731 743 … … 782 794 783 795 /* Create reference to the new object. */ 784 r data_reference(obj_var, res);796 run_reference(run, obj_var, res); 785 797 } 786 798 … … 811 823 printf("Run access operation on pre-evaluated base.\n"); 812 824 #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); 824 826 825 827 switch (vc) { … … 847 849 848 850 /* Implicitly dereference. */ 849 r data_dereference(arg, &darg);851 run_dereference(run, arg, &darg); 850 852 851 853 /* Try again. */ … … 865 867 printf("Run delegate access operation.\n"); 866 868 #endif 867 r data_cvt_value_item(arg, &arg_vi);869 run_cvt_value_item(run, arg, &arg_vi); 868 870 arg_val = arg_vi->u.value; 869 871 assert(arg_val->var->vc == vc_deleg); … … 906 908 { 907 909 stree_symbol_t *member; 910 rdata_var_t *object_var; 908 911 rdata_object_t *object; 909 912 rdata_item_t *ritem; 910 913 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; 911 918 912 919 rdata_value_t *value; … … 918 925 #endif 919 926 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; 923 932 924 933 member = symbol_search_csi(run->program, object->class_sym->u.csi, … … 953 962 var->u.deleg_v = deleg_v; 954 963 955 deleg_v->obj = arg->u. value->var;964 deleg_v->obj = arg->u.address->u.var_a->vref; 956 965 deleg_v->sym = member; 957 966 break; … … 959 968 /* Construct variable address item. */ 960 969 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(); 962 972 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, 965 976 access->member_name->sid); 966 assert(addr ess->vref != NULL);977 assert(addr_var->vref != NULL); 967 978 break; 968 979 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; 971 994 } 972 995 … … 985 1008 986 1009 stree_fun_t *fun; 987 run_ fun_ar_t *fun_ar;1010 run_proc_ar_t *proc_ar; 988 1011 989 1012 #ifdef DEBUG_RUN_TRACE … … 1017 1040 arg = list_node_data(node, stree_expr_t *); 1018 1041 run_expr(run, arg, &rarg_i); 1019 r data_cvt_value_item(rarg_i, &rarg_vi);1042 run_cvt_value_item(run, rarg_i, &rarg_vi); 1020 1043 1021 1044 list_append(&arg_vals, rarg_vi); … … 1026 1049 assert(fun != NULL); 1027 1050 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); 1030 1054 1031 1055 /* 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); 1033 1057 1034 1058 /* Run the function. */ 1035 run_ fun(run, fun_ar, res);1059 run_proc(run, proc_ar, res); 1036 1060 1037 1061 #ifdef DEBUG_RUN_TRACE … … 1048 1072 stree_expr_t *arg; 1049 1073 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. */ 1124 static 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; 1050 1128 rdata_array_t *array; 1051 var_class_t vc;1129 rdata_item_t *arg; 1052 1130 1053 1131 int i; … … 1057 1135 rdata_item_t *ritem; 1058 1136 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; 1094 1149 1095 1150 /* … … 1099 1154 elem_index = 0; 1100 1155 1156 node = list_first(args); 1101 1157 i = 0; 1158 1102 1159 while (node != NULL) { 1103 1160 if (i >= array->rank) { … … 1107 1164 } 1108 1165 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) { 1115 1170 printf("Error: Array index is not an integer.\n"); 1116 1171 exit(1); 1117 1172 } 1118 1173 1119 arg_val = rarg_vi->u.value->var->u.int_v->value;1174 arg_val = arg->u.value->var->u.int_v->value; 1120 1175 1121 1176 if (arg_val < 0 || arg_val >= array->extent[i]) { … … 1127 1182 elem_index = elem_index * array->extent[i] + arg_val; 1128 1183 1129 node = list_next( &index->args, node);1184 node = list_next(args, node); 1130 1185 i += 1; 1131 1186 } … … 1139 1194 /* Construct variable address item. */ 1140 1195 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(); 1142 1198 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]; 1145 1202 1146 1203 *res = ritem; 1147 1204 } 1205 1206 /** Index an object (via its indexer). */ 1207 static 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. */ 1274 static 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 1148 1356 1149 1357 /** Execute assignment. */ … … 1160 1368 run_expr(run, assign->src, &rsrc_i); 1161 1369 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); 1163 1372 src_val = rsrc_vi->u.value; 1164 1373 … … 1169 1378 } 1170 1379 1171 r data_address_write(rdest_i->u.address, rsrc_vi->u.value);1380 run_address_write(run, rdest_i->u.address, rsrc_vi->u.value); 1172 1381 1173 1382 *res = NULL; … … 1187 1396 1188 1397 (void) run; 1189 r data_cvt_value_item(item, &vitem);1398 run_cvt_value_item(run, item, &vitem); 1190 1399 1191 1400 assert(vitem->ic == ic_value);
Note:
See TracChangeset
for help on using the changeset viewer.