Changeset fa36f29 in mainline
- Timestamp:
- 2010-02-27T17:59:14Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 94d484a
- Parents:
- 09ababb7
- Location:
- uspace
- Files:
-
- 2 added
- 23 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/sbi/src/ancr.c
r09ababb7 rfa36f29 72 72 stree_modm_t *modm; 73 73 74 (void) module; 74 75 node = list_first(&prog->module->members); 75 76 -
uspace/app/sbi/src/input.c
r09ababb7 rfa36f29 64 64 65 65 input->buffer = malloc(INPUT_BUFFER_SIZE); 66 if (input->buffer == NULL) 67 return ENOMEM; 66 if (input->buffer == NULL) { 67 printf("Memory allocation failed.\n"); 68 exit(1); 69 } 68 70 69 71 input->line_no = 0; -
uspace/app/sbi/src/lex.c
r09ababb7 rfa36f29 78 78 { lc_for, "for" }, 79 79 { lc_fun, "fun" }, 80 { lc_new, "new" },81 80 { lc_get, "get" }, 82 81 { lc_if, "if" }, … … 85 84 { lc_interface, "interface" }, 86 85 { lc_is, "is" }, 86 { lc_new, "new" }, 87 { lc_nil, "nil" }, 87 88 { lc_override, "override" }, 88 89 { lc_private, "private" }, … … 92 93 { lc_raise, "raise" }, 93 94 { lc_return, "return" }, 95 { lc_self, "self" }, 94 96 { lc_set, "set" }, 95 97 { lc_static, "static" }, -
uspace/app/sbi/src/lex_t.h
r09ababb7 rfa36f29 56 56 lc_interface, 57 57 lc_is, 58 lc_nil, 58 59 lc_override, 59 60 lc_private, … … 63 64 lc_raise, 64 65 lc_return, 66 lc_self, 65 67 lc_set, 66 68 lc_static, -
uspace/app/sbi/src/main.c
r09ababb7 rfa36f29 59 59 rc = input_new(&input, argv[1]); 60 60 if (rc != EOK) { 61 printf("Failed initializing input.\n");61 printf("Failed opening source file '%s'.\n", argv[1]); 62 62 exit(1); 63 63 } -
uspace/app/sbi/src/p_expr.c
r09ababb7 rfa36f29 34 34 #include "list.h" 35 35 #include "mytypes.h" 36 #include "p_type.h" 36 37 #include "parse.h" 37 38 #include "stree.h" … … 42 43 static stree_expr_t *parse_comparative(parse_t *parse); 43 44 static stree_expr_t *parse_additive(parse_t *parse); 45 static stree_expr_t *parse_prefix(parse_t *parse); 46 static stree_expr_t *parse_prefix_new(parse_t *parse); 44 47 static stree_expr_t *parse_postfix(parse_t *parse); 45 48 static stree_expr_t *parse_pf_access(parse_t *parse, stree_expr_t *a); … … 48 51 static stree_expr_t *parse_nameref(parse_t *parse); 49 52 static stree_expr_t *parse_lit_int(parse_t *parse); 53 static stree_expr_t *parse_lit_ref(parse_t *parse); 50 54 static stree_expr_t *parse_lit_string(parse_t *parse); 55 static stree_expr_t *parse_self_ref(parse_t *parse); 51 56 52 57 /** Parse expression. */ … … 130 135 stree_binop_t *binop; 131 136 132 a = parse_p ostfix(parse);137 a = parse_prefix(parse); 133 138 while (lcur_lc(parse) == lc_plus) { 134 139 lskip(parse); 135 b = parse_p ostfix(parse);140 b = parse_prefix(parse); 136 141 137 142 binop = stree_binop_new(bo_plus); … … 145 150 146 151 return a; 152 } 153 154 /** Parse prefix expression. */ 155 static stree_expr_t *parse_prefix(parse_t *parse) 156 { 157 stree_expr_t *a; 158 159 switch (lcur_lc(parse)) { 160 case lc_plus: 161 printf("Unimplemented: Unary plus.\n"); 162 exit(1); 163 case lc_new: 164 a = parse_prefix_new(parse); 165 break; 166 default: 167 a = parse_postfix(parse); 168 break; 169 } 170 171 return a; 172 } 173 174 /** Parse @c new operator. */ 175 static stree_expr_t *parse_prefix_new(parse_t *parse) 176 { 177 stree_texpr_t *texpr; 178 stree_new_t *new_op; 179 stree_expr_t *expr; 180 181 lmatch(parse, lc_new); 182 texpr = parse_texpr(parse); 183 lmatch(parse, lc_lparen); 184 lmatch(parse, lc_rparen); 185 186 new_op = stree_new_new(); 187 new_op->texpr = texpr; 188 expr = stree_expr_new(ec_new); 189 expr->u.new_op = new_op; 190 191 return expr; 147 192 } 148 193 … … 240 285 expr = parse_lit_int(parse); 241 286 break; 287 case lc_nil: 288 expr = parse_lit_ref(parse); 289 break; 242 290 case lc_lit_string: 243 291 expr = parse_lit_string(parse); 292 break; 293 case lc_self: 294 expr = parse_self_ref(parse); 244 295 break; 245 296 default: … … 284 335 } 285 336 337 /** Parse reference literal (@c nil). */ 338 static stree_expr_t *parse_lit_ref(parse_t *parse) 339 { 340 stree_literal_t *literal; 341 stree_expr_t *expr; 342 343 lmatch(parse, lc_nil); 344 345 literal = stree_literal_new(ltc_ref); 346 347 expr = stree_expr_new(ec_literal); 348 expr->u.literal = literal; 349 350 return expr; 351 } 352 286 353 /** Parse string literal. */ 287 354 static stree_expr_t *parse_lit_string(parse_t *parse) … … 302 369 return expr; 303 370 } 371 372 /** Parse @c self keyword. */ 373 static stree_expr_t *parse_self_ref(parse_t *parse) 374 { 375 stree_self_ref_t *self_ref; 376 stree_expr_t *expr; 377 378 lmatch(parse, lc_self); 379 380 self_ref = stree_self_ref_new(); 381 382 expr = stree_expr_new(ec_self_ref); 383 expr->u.self_ref = self_ref; 384 385 return expr; 386 } -
uspace/app/sbi/src/p_type.c
r09ababb7 rfa36f29 42 42 static stree_texpr_t *parse_tpostfix(parse_t *parse); 43 43 static stree_texpr_t *parse_tprimitive(parse_t *parse); 44 static stree_tliteral_t *parse_tliteral(parse_t *parse); 44 45 static stree_tnameref_t *parse_tnameref(parse_t *parse); 45 46 … … 104 105 stree_texpr_t *texpr; 105 106 106 lcheck(parse, lc_ident); 107 texpr = stree_texpr_new(tc_tnameref); 108 texpr->u.tnameref = parse_tnameref(parse); 107 switch (lcur_lc(parse)) { 108 case lc_ident: 109 texpr = stree_texpr_new(tc_tnameref); 110 texpr->u.tnameref = parse_tnameref(parse); 111 break; 112 case lc_int: 113 case lc_string: 114 texpr = stree_texpr_new(tc_tliteral); 115 texpr->u.tliteral = parse_tliteral(parse); 116 break; 117 default: 118 lunexpected_error(parse); 119 exit(1); 120 } 109 121 110 122 return texpr; 123 } 124 125 /** Parse type literal. */ 126 static stree_tliteral_t *parse_tliteral(parse_t *parse) 127 { 128 stree_tliteral_t *tliteral; 129 130 tliteral = stree_tliteral_new(); 131 132 switch (lcur_lc(parse)) { 133 case lc_int: 134 tliteral->tlc = tlc_int; 135 break; 136 case lc_string: 137 tliteral->tlc = tlc_string; 138 break; 139 default: 140 assert(b_false); 141 } 142 143 lskip(parse); 144 145 return tliteral; 111 146 } 112 147 -
uspace/app/sbi/src/parse.c
r09ababb7 rfa36f29 68 68 static stree_for_t *parse_for(parse_t *parse); 69 69 static stree_raise_t *parse_raise(parse_t *parse); 70 static stree_return_t *parse_return(parse_t *parse); 70 71 static stree_wef_t *parse_wef(parse_t *parse); 71 72 static stree_exps_t *parse_exps(parse_t *parse); … … 342 343 stree_for_t *for_s; 343 344 stree_raise_t *raise_s; 345 stree_return_t *return_s; 344 346 stree_wef_t *wef_s; 345 347 stree_exps_t *exp_s; … … 370 372 stat = stree_stat_new(st_raise); 371 373 stat->u.raise_s = raise_s; 374 break; 375 case lc_return: 376 return_s = parse_return(parse); 377 stat = stree_stat_new(st_return); 378 stat->u.return_s = return_s; 372 379 break; 373 380 case lc_with: … … 485 492 } 486 493 494 /** Parse @c return statement. */ 495 static stree_return_t *parse_return(parse_t *parse) 496 { 497 stree_return_t *return_s; 498 499 return_s = stree_return_new(); 500 501 lmatch(parse, lc_return); 502 return_s->expr = parse_expr(parse); 503 lmatch(parse, lc_scolon); 504 505 return return_s; 506 } 507 487 508 /* Parse @c with-except-finally statement. */ 488 509 static stree_wef_t *parse_wef(parse_t *parse) -
uspace/app/sbi/src/rdata.c
r09ababb7 rfa36f29 100 100 } 101 101 102 rdata_ref_t *rdata_ref_new(void) 103 { 104 rdata_ref_t *ref; 105 106 ref = calloc(1, sizeof(rdata_ref_t)); 107 if (ref == NULL) { 108 printf("Memory allocation failed.\n"); 109 exit(1); 110 } 111 112 return ref; 113 } 114 102 115 rdata_deleg_t *rdata_deleg_new(void) 103 116 { … … 111 124 112 125 return deleg; 126 } 127 128 rdata_object_t *rdata_object_new(void) 129 { 130 rdata_object_t *object; 131 132 object = calloc(1, sizeof(rdata_object_t)); 133 if (object == NULL) { 134 printf("Memory allocation failed.\n"); 135 exit(1); 136 } 137 138 return object; 113 139 } 114 140 … … 181 207 static void rdata_ref_copy(rdata_ref_t *src, rdata_ref_t **dest) 182 208 { 183 printf("Unimplemented: Copy reference.\n");184 exit(1);209 *dest = rdata_ref_new(); 210 (*dest)->vref = src->vref; 185 211 } 186 212 187 213 static void rdata_deleg_copy(rdata_deleg_t *src, rdata_deleg_t **dest) 188 214 { 215 (void) src; (void) dest; 189 216 printf("Unimplemented: Copy delegate.\n"); 190 217 exit(1); … … 193 220 static void rdata_object_copy(rdata_object_t *src, rdata_object_t **dest) 194 221 { 222 (void) src; (void) dest; 195 223 printf("Unimplemented: Copy object.\n"); 196 224 exit(1); 225 } 226 227 /** Convert item to value item. 228 * 229 * If @a item is a value, we just return a copy. If @a item is an address, 230 * we read from the address. 231 */ 232 void rdata_cvt_value_item(rdata_item_t *item, rdata_item_t **ritem) 233 { 234 rdata_value_t *value; 235 236 /* 237 * This can happen when trying to use output of a function which 238 * does not return a value. 239 */ 240 if (item == NULL) { 241 printf("Error: Sub-expression has no value.\n"); 242 exit(1); 243 } 244 245 /* Address item. Perform read operation. */ 246 if (item->ic == ic_address) { 247 rdata_address_read(item->u.address, ritem); 248 return; 249 } 250 251 /* It already is a value, we can share the @c var. */ 252 value = rdata_value_new(); 253 value->var = item->u.value->var; 254 *ritem = rdata_item_new(ic_value); 255 (*ritem)->u.value = value; 256 } 257 258 /** Return reference to a variable. 259 * 260 * Constructs a reference (value item) pointing to @a var. 261 */ 262 void rdata_reference(rdata_var_t *var, rdata_item_t **res) 263 { 264 rdata_ref_t *ref; 265 rdata_var_t *ref_var; 266 rdata_value_t *ref_value; 267 rdata_item_t *ref_item; 268 269 /* Create reference to the variable. */ 270 ref = rdata_ref_new(); 271 ref_var = rdata_var_new(vc_ref); 272 ref->vref = var; 273 ref_var->u.ref_v = ref; 274 275 /* Construct value of the reference to return. */ 276 ref_item = rdata_item_new(ic_value); 277 ref_value = rdata_value_new(); 278 ref_item->u.value = ref_value; 279 ref_value->var = ref_var; 280 281 *res = ref_item; 282 } 283 284 /** Return address of reference target. 285 * 286 * Takes a reference (address or value) and returns the address (item) of 287 * the target of the reference. 288 */ 289 void rdata_dereference(rdata_item_t *ref, rdata_item_t **ritem) 290 { 291 rdata_item_t *ref_val; 292 rdata_item_t *item; 293 rdata_address_t *address; 294 295 #ifdef DEBUG_RUN_TRACE 296 printf("run_dereference()\n"); 297 #endif 298 rdata_cvt_value_item(ref, &ref_val); 299 assert(ref_val->u.value->var->vc == vc_ref); 300 301 item = rdata_item_new(ic_address); 302 address = rdata_address_new(); 303 item->u.address = address; 304 address->vref = ref_val->u.value->var->u.ref_v->vref; 305 306 if (address->vref == NULL) { 307 printf("Error: Accessing null reference.\n"); 308 exit(1); 309 } 310 311 #ifdef DEBUG_RUN_TRACE 312 printf("vref set to %p\n", address->vref); 313 #endif 314 *ritem = item; 315 } 316 317 /** Read data from an address. 318 * 319 * Return value stored in a variable at the specified address. 320 */ 321 void rdata_address_read(rdata_address_t *address, rdata_item_t **ritem) 322 { 323 rdata_value_t *value; 324 rdata_var_t *rvar; 325 326 /* Perform a shallow copy of @c var. */ 327 rdata_var_copy(address->vref, &rvar); 328 329 value = rdata_value_new(); 330 value->var = rvar; 331 *ritem = rdata_item_new(ic_value); 332 (*ritem)->u.value = value; 333 } 334 335 /** Write data to an address. 336 * 337 * Store @a value to the variable at @a address. 338 */ 339 void rdata_address_write(rdata_address_t *address, rdata_value_t *value) 340 { 341 rdata_var_t *nvar; 342 rdata_var_t *orig_var; 343 344 /* Perform a shallow copy of @c value->var. */ 345 rdata_var_copy(value->var, &nvar); 346 orig_var = address->vref; 347 348 /* XXX do this in a prettier way. */ 349 350 orig_var->vc = nvar->vc; 351 switch (nvar->vc) { 352 case vc_int: orig_var->u.int_v = nvar->u.int_v; break; 353 case vc_ref: orig_var->u.ref_v = nvar->u.ref_v; break; 354 case vc_deleg: orig_var->u.deleg_v = nvar->u.deleg_v; break; 355 case vc_object: orig_var->u.object_v = nvar->u.object_v; break; 356 default: assert(b_false); 357 } 358 359 /* XXX We should free some stuff around here. */ 197 360 } 198 361 … … 232 395 printf("int(%d)", var->u.int_v->value); 233 396 break; 397 case vc_string: 398 printf("string(\"%s\")", var->u.string_v->value); 399 break; 234 400 case vc_ref: 235 401 printf("ref"); … … 242 408 break; 243 409 default: 410 printf("print(%d)\n", var->vc); 244 411 assert(b_false); 245 412 } -
uspace/app/sbi/src/rdata.h
r09ababb7 rfa36f29 36 36 rdata_value_t *rdata_value_new(void); 37 37 rdata_var_t *rdata_var_new(var_class_t vc); 38 rdata_ref_t *rdata_ref_new(void); 38 39 rdata_deleg_t *rdata_deleg_new(void); 40 rdata_object_t *rdata_object_new(void); 39 41 rdata_int_t *rdata_int_new(void); 40 42 rdata_string_t *rdata_string_new(void); 41 43 42 44 void rdata_var_copy(rdata_var_t *src, rdata_var_t **dest); 45 46 void rdata_cvt_value_item(rdata_item_t *item, rdata_item_t **ritem); 47 void rdata_reference(rdata_var_t *var, rdata_item_t **res); 48 void rdata_dereference(rdata_item_t *ref, rdata_item_t **ritem); 49 void rdata_address_read(rdata_address_t *address, rdata_item_t **ritem); 50 void rdata_address_write(rdata_address_t *address, rdata_value_t *value); 51 43 52 void rdata_item_print(rdata_item_t *item); 44 53 -
uspace/app/sbi/src/rdata_t.h
r09ababb7 rfa36f29 74 74 75 75 /** Map field name SID to field data */ 76 intmap_t *fields; /* of (rdata_var_t *) */76 intmap_t fields; /* of (rdata_var_t *) */ 77 77 } rdata_object_t; 78 78 … … 141 141 * assignment operator. 142 142 */ 143 typedef struct {143 typedef struct rdata_item { 144 144 item_class_t ic; 145 145 -
uspace/app/sbi/src/run.c
r09ababb7 rfa36f29 51 51 static void run_if(run_t *run, stree_if_t *if_s); 52 52 static void run_while(run_t *run, stree_while_t *while_s); 53 54 static void run_value_item_to_var(rdata_item_t *item, rdata_var_t **var); 53 static void run_return(run_t *run, stree_return_t *return_s); 55 54 56 55 /** Initialize runner instance. */ 57 56 void run_init(run_t *run) 58 57 { 58 (void) run; 59 59 } 60 60 … … 62 62 void run_program(run_t *run, stree_program_t *prog) 63 63 { 64 stree_symbol_t *main_class_sym;65 64 stree_symbol_t *main_fun_sym; 66 stree_csi_t *main_class;67 65 stree_fun_t *main_fun; 68 66 stree_ident_t *fake_ident; 69 67 list_t main_args; 68 run_fun_ar_t *fun_ar; 69 rdata_item_t *res; 70 70 71 71 /* Note down link to program code. */ … … 75 75 run->thread_ar = run_thread_ar_new(); 76 76 list_init(&run->thread_ar->fun_ar); 77 run->thread_ar->bo_mode = bm_none; 77 78 78 79 /* 79 * Resolve class @c HelloWorld80 * Find entry point @c Main(). 80 81 */ 81 82 fake_ident = stree_ident_new(); 82 83 fake_ident->sid = strtab_get_sid("HelloWorld"); 84 main_class_sym = symbol_lookup_in_csi(prog, NULL, fake_ident); 85 main_class = symbol_to_csi(main_class_sym); 86 if (main_class == NULL) { 87 printf("Error: HelloWorld is not a CSI.\n"); 88 exit(1); 89 } 90 91 #ifdef DEBUG_RUN_TRACE 92 printf("Found class '"); symbol_print_fqn(prog, main_class_sym); 93 printf("'.\n"); 94 #endif 95 96 /* 97 * Resolve function @c main within the class. 98 */ 99 fake_ident->sid = strtab_get_sid("main"); 100 main_fun_sym = symbol_lookup_in_csi(prog, main_class, fake_ident); 83 fake_ident->sid = strtab_get_sid("Main"); 84 main_fun_sym = symbol_find_epoint(prog, fake_ident); 85 if (main_fun_sym == NULL) { 86 printf("Error: Entry point 'Main' not found.\n"); 87 exit(1); 88 } 89 101 90 main_fun = symbol_to_fun(main_fun_sym); 102 if (main_fun == NULL) { 103 printf("Error: HelloWorld.main is not a function.\n"); 104 exit(1); 105 } 91 assert(main_fun != NULL); 106 92 107 93 #ifdef DEBUG_RUN_TRACE … … 110 96 #endif 111 97 98 /* Run function @c main. */ 112 99 list_init(&main_args); 113 run_fun(run, main_fun, &main_args); 100 run_fun_ar_create(run, NULL, main_fun, &fun_ar); 101 run_fun_ar_set_args(run, fun_ar, &main_args); 102 run_fun(run, fun_ar, &res); 114 103 } 115 104 116 105 /** Run member function */ 117 void run_fun(run_t *run, stree_fun_t *fun, list_t *args)106 void run_fun(run_t *run, run_fun_ar_t *fun_ar, rdata_item_t **res) 118 107 { 119 108 stree_symbol_t *fun_sym; 120 run_fun_ar_t *fun_ar; 121 run_block_ar_t *block_ar; 122 list_node_t *rarg_n, *farg_n; 109 stree_fun_t *fun; 123 110 list_node_t *node; 124 rdata_item_t *rarg; 125 stree_fun_arg_t *farg; 126 rdata_var_t *var; 127 128 fun_sym = fun_to_symbol(fun); 111 112 fun_sym = fun_ar->fun_sym; 113 fun = symbol_to_fun(fun_sym); 114 assert(fun != NULL); 129 115 130 116 #ifdef DEBUG_RUN_TRACE … … 133 119 printf("'.\n"); 134 120 #endif 135 136 /* Create function activation record. */137 fun_ar = run_fun_ar_new();138 fun_ar->fun_sym = fun_sym;139 list_init(&fun_ar->block_ar);140 141 121 /* Add function AR to the stack. */ 142 122 list_append(&run->thread_ar->fun_ar, fun_ar); 143 144 /* Create special block activation record to hold function arguments. */145 block_ar = run_block_ar_new();146 intmap_init(&block_ar->vars);147 list_append(&fun_ar->block_ar, block_ar);148 149 /* Declare local variables to hold argument values. */150 rarg_n = list_first(args);151 farg_n = list_first(&fun->args);152 153 while (farg_n != NULL) {154 if (rarg_n == NULL) {155 printf("Error: Too few arguments to function '");156 symbol_print_fqn(run->program, fun_sym);157 printf("'.\n");158 exit(1);159 }160 161 rarg = list_node_data(rarg_n, rdata_item_t *);162 farg = list_node_data(farg_n, stree_fun_arg_t *);163 164 assert(rarg->ic == ic_value);165 166 /* Construct a variable from the argument value. */167 run_value_item_to_var(rarg, &var);168 169 /* Declare variable using name of formal argument. */170 intmap_set(&block_ar->vars, farg->name->sid, var);171 172 rarg_n = list_next(args, rarg_n);173 farg_n = list_next(&fun->args, farg_n);174 }175 176 /* Check for excess real parameters. */177 if (rarg_n != NULL) {178 printf("Error: Too many arguments to function '");179 symbol_print_fqn(run->program, fun_sym);180 printf("'.\n");181 exit(1);182 }183 123 184 124 /* Run main function block. */ … … 189 129 } 190 130 131 /* Handle bailout. */ 132 switch (run->thread_ar->bo_mode) { 133 case bm_stat: 134 printf("Error: Misplaced 'break' statement.\n"); 135 exit(1); 136 case bm_fun: 137 run->thread_ar->bo_mode = bm_none; 138 break; 139 default: 140 break; 141 } 142 191 143 #ifdef DEBUG_RUN_TRACE 192 144 printf("Done executing function '"); … … 196 148 run_print_fun_bt(run); 197 149 #endif 198 199 150 /* Remove function activation record from the stack. */ 200 151 node = list_last(&run->thread_ar->fun_ar); 201 152 assert(list_node_data(node, run_fun_ar_t *) == fun_ar); 202 153 list_remove(&run->thread_ar->fun_ar, node); 154 155 *res = fun_ar->retval; 203 156 } 204 157 … … 227 180 stat = list_node_data(node, stree_stat_t *); 228 181 run_stat(run, stat); 182 183 if (run->thread_ar->bo_mode != bm_none) 184 break; 185 229 186 node = list_next(&block->stats, node); 230 187 } … … 260 217 run_while(run, stat->u.while_s); 261 218 break; 219 case st_return: 220 run_return(run, stat->u.return_s); 221 break; 262 222 case st_for: 263 223 case st_raise: … … 361 321 run_block(run, while_s->body); 362 322 run_expr(run, while_s->cond, &rcond); 323 324 if (run->thread_ar->bo_mode != bm_none) 325 break; 363 326 } 364 327 … … 366 329 printf("While statement terminated.\n"); 367 330 #endif 331 } 332 333 /** Run @c return statement. */ 334 static void run_return(run_t *run, stree_return_t *return_s) 335 { 336 rdata_item_t *rexpr; 337 run_fun_ar_t *fun_ar; 338 339 #ifdef DEBUG_RUN_TRACE 340 printf("Executing return statement.\n"); 341 #endif 342 run_expr(run, return_s->expr, &rexpr); 343 344 /* Store expression result in function AR. */ 345 fun_ar = run_get_current_fun_ar(run); 346 fun_ar->retval = rexpr; 347 348 /* Force control to ascend and leave the function. */ 349 if (run->thread_ar->bo_mode == bm_none) 350 run->thread_ar->bo_mode = bm_fun; 368 351 } 369 352 … … 421 404 * (2) Initialize the variable with the provided value. 422 405 */ 423 staticvoid run_value_item_to_var(rdata_item_t *item, rdata_var_t **var)406 void run_value_item_to_var(rdata_item_t *item, rdata_var_t **var) 424 407 { 425 408 rdata_int_t *int_v; 426 409 rdata_string_t *string_v; 410 rdata_ref_t *ref_v; 427 411 rdata_var_t *in_var; 428 412 … … 445 429 string_v->value = item->u.value->var->u.string_v->value; 446 430 break; 431 case vc_ref: 432 *var = rdata_var_new(vc_ref); 433 ref_v = rdata_ref_new(); 434 435 (*var)->u.ref_v = ref_v; 436 ref_v->vref = item->u.value->var->u.ref_v->vref; 437 break; 447 438 default: 448 439 printf("Error: Unimplemented argument type.\n"); 449 440 exit(1); 450 441 442 } 443 } 444 445 /** Construct a function AR. */ 446 void run_fun_ar_create(run_t *run, rdata_var_t *obj, stree_fun_t *fun, 447 run_fun_ar_t **rfun_ar) 448 { 449 run_fun_ar_t *fun_ar; 450 451 (void) run; 452 453 /* Create function activation record. */ 454 fun_ar = run_fun_ar_new(); 455 fun_ar->obj = obj; 456 fun_ar->fun_sym = fun_to_symbol(fun); 457 list_init(&fun_ar->block_ar); 458 459 fun_ar->retval = NULL; 460 461 *rfun_ar = fun_ar; 462 } 463 464 /** Fill arguments in a function AR. */ 465 void run_fun_ar_set_args(run_t *run, run_fun_ar_t *fun_ar, list_t *args) 466 { 467 stree_fun_t *fun; 468 run_block_ar_t *block_ar; 469 list_node_t *rarg_n, *farg_n; 470 rdata_item_t *rarg; 471 stree_fun_arg_t *farg; 472 rdata_var_t *var; 473 474 /* AR should have been created with run_fun_ar_create(). */ 475 assert(fun_ar->fun_sym != NULL); 476 assert(list_is_empty(&fun_ar->block_ar)); 477 478 fun = symbol_to_fun(fun_ar->fun_sym); 479 assert(fun != NULL); 480 481 /* Create special block activation record to hold function arguments. */ 482 block_ar = run_block_ar_new(); 483 intmap_init(&block_ar->vars); 484 list_append(&fun_ar->block_ar, block_ar); 485 486 /* Declare local variables to hold argument values. */ 487 rarg_n = list_first(args); 488 farg_n = list_first(&fun->args); 489 490 while (farg_n != NULL) { 491 if (rarg_n == NULL) { 492 printf("Error: Too few arguments to function '"); 493 symbol_print_fqn(run->program, fun_ar->fun_sym); 494 printf("'.\n"); 495 exit(1); 496 } 497 498 rarg = list_node_data(rarg_n, rdata_item_t *); 499 farg = list_node_data(farg_n, stree_fun_arg_t *); 500 501 assert(rarg->ic == ic_value); 502 503 /* Construct a variable from the argument value. */ 504 run_value_item_to_var(rarg, &var); 505 506 /* Declare variable using name of formal argument. */ 507 intmap_set(&block_ar->vars, farg->name->sid, var); 508 509 rarg_n = list_next(args, rarg_n); 510 farg_n = list_next(&fun->args, farg_n); 511 } 512 513 /* Check for excess real parameters. */ 514 if (rarg_n != NULL) { 515 printf("Error: Too many arguments to function '"); 516 symbol_print_fqn(run->program, fun_ar->fun_sym); 517 printf("'.\n"); 518 exit(1); 451 519 } 452 520 } -
uspace/app/sbi/src/run.h
r09ababb7 rfa36f29 34 34 void run_init(run_t *run); 35 35 void run_program(run_t *run, stree_program_t *prog); 36 void run_fun(run_t *run, stree_fun_t *fun, list_t *args);36 void run_fun(run_t *run, run_fun_ar_t *fun_ar, rdata_item_t **res); 37 37 38 38 void run_print_fun_bt(run_t *run); … … 42 42 run_block_ar_t *run_get_current_block_ar(run_t *run); 43 43 44 void run_value_item_to_var(rdata_item_t *item, rdata_var_t **var); 45 void run_fun_ar_set_args(run_t *run, run_fun_ar_t *fun_ar, list_t *args); 46 void run_fun_ar_create(run_t *run, rdata_var_t *obj, stree_fun_t *fun, 47 run_fun_ar_t **rfun_ar); 48 44 49 run_thread_ar_t *run_thread_ar_new(void); 45 50 run_fun_ar_t *run_fun_ar_new(void); -
uspace/app/sbi/src/run_expr.c
r09ababb7 rfa36f29 33 33 #include <assert.h> 34 34 #include "debug.h" 35 #include "intmap.h" 35 36 #include "list.h" 36 37 #include "mytypes.h" … … 44 45 static void run_nameref(run_t *run, stree_nameref_t *nameref, 45 46 rdata_item_t **res); 47 46 48 static void run_literal(run_t *run, stree_literal_t *literal, 47 49 rdata_item_t **res); 48 50 static void run_lit_int(run_t *run, stree_lit_int_t *lit_int, 49 51 rdata_item_t **res); 52 static void run_lit_ref(run_t *run, stree_lit_ref_t *lit_ref, 53 rdata_item_t **res); 50 54 static void run_lit_string(run_t *run, stree_lit_string_t *lit_string, 51 55 rdata_item_t **res); 56 57 static void run_self_ref(run_t *run, stree_self_ref_t *self_ref, 58 rdata_item_t **res); 59 52 60 static void run_binop(run_t *run, stree_binop_t *binop, rdata_item_t **res); 61 static void run_binop_int(run_t *run, stree_binop_t *binop, rdata_value_t *v1, 62 rdata_value_t *v2, rdata_item_t **res); 63 static void run_binop_ref(run_t *run, stree_binop_t *binop, rdata_value_t *v1, 64 rdata_value_t *v2, rdata_item_t **res); 65 53 66 static void run_unop(run_t *run, stree_unop_t *unop, rdata_item_t **res); 67 static void run_new(run_t *run, stree_new_t *new_op, rdata_item_t **res); 68 54 69 static void run_access(run_t *run, stree_access_t *access, rdata_item_t **res); 70 static void run_access_item(run_t *run, stree_access_t *access, 71 rdata_item_t *arg, rdata_item_t **res); 72 static void run_access_ref(run_t *run, stree_access_t *access, 73 rdata_item_t *arg, rdata_item_t **res); 74 static void run_access_deleg(run_t *run, stree_access_t *access, 75 rdata_item_t *arg, rdata_item_t **res); 76 static void run_access_object(run_t *run, stree_access_t *access, 77 rdata_item_t *arg, rdata_item_t **res); 78 55 79 static void run_call(run_t *run, stree_call_t *call, rdata_item_t **res); 56 80 static void run_assign(run_t *run, stree_assign_t *assign, rdata_item_t **res); 57 58 static void run_address_read(run_t *run, rdata_address_t *address,59 rdata_item_t **ritem);60 static void run_address_write(run_t *run, rdata_address_t *address,61 rdata_value_t *value);62 81 63 82 /** Evaluate expression. */ … … 75 94 run_literal(run, expr->u.literal, res); 76 95 break; 96 case ec_self_ref: 97 run_self_ref(run, expr->u.self_ref, res); 98 break; 77 99 case ec_binop: 78 100 run_binop(run, expr->u.binop, res); … … 80 102 case ec_unop: 81 103 run_unop(run, expr->u.unop, res); 104 break; 105 case ec_new: 106 run_new(run, expr->u.new_op, res); 82 107 break; 83 108 case ec_access: … … 106 131 rdata_item_t *item; 107 132 rdata_address_t *address; 108 rdata_value_t *val ;133 rdata_value_t *value; 109 134 rdata_var_t *var; 110 135 rdata_deleg_t *deleg_v; 136 137 run_fun_ar_t *fun_ar; 138 stree_symbol_t *csi_sym; 139 stree_csi_t *csi; 140 rdata_object_t *obj; 141 rdata_var_t *member_var; 111 142 112 143 #ifdef DEBUG_RUN_TRACE … … 137 168 */ 138 169 139 /* XXX Start lookup in currently active CSI. */ 140 sym = symbol_lookup_in_csi(run->program, NULL, nameref->name); 170 /* Determine currently active object or CSI. */ 171 fun_ar = run_get_current_fun_ar(run); 172 if (fun_ar->obj != NULL) { 173 assert(fun_ar->obj->vc == vc_object); 174 obj = fun_ar->obj->u.object_v; 175 csi_sym = obj->class_sym; 176 csi = symbol_to_csi(csi_sym); 177 assert(csi != NULL); 178 } else { 179 csi = fun_ar->fun_sym->outer_csi; 180 obj = NULL; 181 } 182 183 sym = symbol_lookup_in_csi(run->program, csi, nameref->name); 141 184 142 185 switch (sym->sc) { … … 146 189 #endif 147 190 item = rdata_item_new(ic_value); 148 val = rdata_value_new();191 value = rdata_value_new(); 149 192 var = rdata_var_new(vc_deleg); 150 193 deleg_v = rdata_deleg_new(); 151 194 152 item->u.value = val ;153 val ->var = var;195 item->u.value = value; 196 value->var = var; 154 197 var->u.deleg_v = deleg_v; 155 198 … … 158 201 *res = item; 159 202 break; 203 case sc_fun: 204 /* There should be no global functions. */ 205 assert(csi != NULL); 206 207 if (sym->outer_csi != csi) { 208 /* Function is not in the current object. */ 209 printf("Error: Cannot access non-static member " 210 "function '"); 211 symbol_print_fqn(run->program, sym); 212 printf("' from nested CSI '"); 213 symbol_print_fqn(run->program, csi_sym); 214 printf("'.\n"); 215 exit(1); 216 } 217 218 /* Construct delegate. */ 219 item = rdata_item_new(ic_value); 220 value = rdata_value_new(); 221 item->u.value = value; 222 223 var = rdata_var_new(vc_deleg); 224 deleg_v = rdata_deleg_new(); 225 value->var = var; 226 var->u.deleg_v = deleg_v; 227 228 deleg_v->obj = fun_ar->obj; 229 deleg_v->sym = sym; 230 231 *res = item; 232 break; 233 case sc_var: 234 #ifdef DEBUG_RUN_TRACE 235 printf("Referencing member variable.\n"); 236 #endif 237 /* There should be no global variables. */ 238 assert(csi != NULL); 239 240 /* XXX Assume variable is not static for now. */ 241 assert(obj != NULL); 242 243 if (sym->outer_csi != csi) { 244 /* Variable is not in the current object. */ 245 printf("Error: Cannot access non-static member " 246 "variable '"); 247 symbol_print_fqn(run->program, sym); 248 printf("' from nested CSI '"); 249 symbol_print_fqn(run->program, csi_sym); 250 printf("'.\n"); 251 exit(1); 252 } 253 254 /* Find member variable in object. */ 255 member_var = intmap_get(&obj->fields, nameref->name->sid); 256 assert(member_var != NULL); 257 258 /* Return address of the variable. */ 259 item = rdata_item_new(ic_address); 260 address = rdata_address_new(); 261 262 item->u.address = address; 263 address->vref = member_var; 264 265 *res = item; 266 break; 160 267 default: 161 268 printf("Referencing symbol class %d unimplemented.\n", sym->sc); … … 176 283 case ltc_int: 177 284 run_lit_int(run, &literal->u.lit_int, res); 285 break; 286 case ltc_ref: 287 run_lit_ref(run, &literal->u.lit_ref, res); 178 288 break; 179 289 case ltc_string: … … 197 307 printf("Run integer literal.\n"); 198 308 #endif 309 (void) run; 199 310 200 311 item = rdata_item_new(ic_value); … … 211 322 } 212 323 324 /** Evaluate reference literal (@c nil). */ 325 static void run_lit_ref(run_t *run, stree_lit_ref_t *lit_ref, 326 rdata_item_t **res) 327 { 328 rdata_item_t *item; 329 rdata_value_t *value; 330 rdata_var_t *var; 331 rdata_ref_t *ref_v; 332 333 #ifdef DEBUG_RUN_TRACE 334 printf("Run reference literal (nil).\n"); 335 #endif 336 (void) run; 337 (void) lit_ref; 338 339 item = rdata_item_new(ic_value); 340 value = rdata_value_new(); 341 var = rdata_var_new(vc_ref); 342 ref_v = rdata_ref_new(); 343 344 item->u.value = value; 345 value->var = var; 346 var->u.ref_v = ref_v; 347 ref_v->vref = NULL; 348 349 *res = item; 350 } 351 213 352 /** Evaluate string literal. */ 214 353 static void run_lit_string(run_t *run, stree_lit_string_t *lit_string, … … 223 362 printf("Run integer literal.\n"); 224 363 #endif 364 (void) run; 365 225 366 item = rdata_item_new(ic_value); 226 367 value = rdata_value_new(); … … 236 377 } 237 378 379 /** Evaluate @c self reference. */ 380 static void run_self_ref(run_t *run, stree_self_ref_t *self_ref, 381 rdata_item_t **res) 382 { 383 run_fun_ar_t *fun_ar; 384 385 #ifdef DEBUG_RUN_TRACE 386 printf("Run self reference.\n"); 387 #endif 388 (void) self_ref; 389 fun_ar = run_get_current_fun_ar(run); 390 391 /* Return reference to the currently active object. */ 392 rdata_reference(fun_ar->obj, res); 393 } 238 394 239 395 /** Evaluate binary operation. */ … … 243 399 rdata_item_t *rarg1_vi, *rarg2_vi; 244 400 rdata_value_t *v1, *v2; 245 int i1, i2;246 247 rdata_item_t *item;248 rdata_value_t *value;249 rdata_var_t *var;250 rdata_int_t *int_v;251 401 252 402 #ifdef DEBUG_RUN_TRACE … … 276 426 #endif 277 427 278 r un_cvt_value_item(run,rarg1_i, &rarg1_vi);279 r un_cvt_value_item(run,rarg2_i, &rarg2_vi);428 rdata_cvt_value_item(rarg1_i, &rarg1_vi); 429 rdata_cvt_value_item(rarg2_i, &rarg2_vi); 280 430 281 431 v1 = rarg1_vi->u.value; 282 432 v2 = rarg2_vi->u.value; 283 433 284 if (v1->var->vc != vc_int || v2->var->vc != vc_int) { 285 printf("Unimplemented: Binary operation arguments are not " 286 "integer values.\n"); 287 exit(1); 288 } 434 if (v1->var->vc != v2->var->vc) { 435 printf("Unimplemented: Binary operation arguments have " 436 "different type.\n"); 437 exit(1); 438 } 439 440 switch (v1->var->vc) { 441 case vc_int: 442 run_binop_int(run, binop, v1, v2, res); 443 break; 444 case vc_ref: 445 run_binop_ref(run, binop, v1, v2, res); 446 break; 447 default: 448 printf("Unimplemented: Binary operation arguments of " 449 "type %d.\n", v1->var->vc); 450 exit(1); 451 } 452 } 453 454 /** Evaluate binary operation on int arguments. */ 455 static void run_binop_int(run_t *run, stree_binop_t *binop, rdata_value_t *v1, 456 rdata_value_t *v2, rdata_item_t **res) 457 { 458 rdata_item_t *item; 459 rdata_value_t *value; 460 rdata_var_t *var; 461 rdata_int_t *int_v; 462 463 int i1, i2; 464 465 (void) run; 289 466 290 467 item = rdata_item_new(ic_value); … … 331 508 } 332 509 510 /** Evaluate binary operation on ref arguments. */ 511 static void run_binop_ref(run_t *run, stree_binop_t *binop, rdata_value_t *v1, 512 rdata_value_t *v2, rdata_item_t **res) 513 { 514 rdata_item_t *item; 515 rdata_value_t *value; 516 rdata_var_t *var; 517 rdata_int_t *int_v; 518 519 rdata_var_t *ref1, *ref2; 520 521 (void) run; 522 523 item = rdata_item_new(ic_value); 524 value = rdata_value_new(); 525 var = rdata_var_new(vc_int); 526 int_v = rdata_int_new(); 527 528 item->u.value = value; 529 value->var = var; 530 var->u.int_v = int_v; 531 532 ref1 = v1->var->u.ref_v->vref; 533 ref2 = v2->var->u.ref_v->vref; 534 535 switch (binop->bc) { 536 /* XXX We should have a real boolean type. */ 537 case bo_equal: 538 int_v->value = (ref1 == ref2) ? 1 : 0; 539 break; 540 case bo_notequal: 541 int_v->value = (ref1 != ref2) ? 1 : 0; 542 break; 543 default: 544 printf("Error: Invalid binary operation on reference " 545 "arguments (%d).\n", binop->bc); 546 assert(b_false); 547 } 548 549 *res = item; 550 } 551 552 333 553 /** Evaluate unary operation. */ 334 554 static void run_unop(run_t *run, stree_unop_t *unop, rdata_item_t **res) … … 343 563 } 344 564 565 /** Evaluate @c new operation. */ 566 static void run_new(run_t *run, stree_new_t *new_op, rdata_item_t **res) 567 { 568 rdata_object_t *obj; 569 rdata_var_t *obj_var; 570 571 stree_symbol_t *csi_sym; 572 stree_csi_t *csi; 573 stree_csimbr_t *csimbr; 574 575 rdata_var_t *mbr_var; 576 577 list_node_t *node; 578 579 #ifdef DEBUG_RUN_TRACE 580 printf("Run 'new' operation.\n"); 581 #endif 582 /* Lookup object CSI. */ 583 /* XXX Should start in the current CSI. */ 584 csi_sym = symbol_xlookup_in_csi(run->program, NULL, new_op->texpr); 585 csi = symbol_to_csi(csi_sym); 586 if (csi == NULL) { 587 printf("Error: Symbol '"); 588 symbol_print_fqn(run->program, csi_sym); 589 printf("' is not a CSI. CSI required for 'new' operator.\n"); 590 exit(1); 591 } 592 593 /* Create the object. */ 594 obj = rdata_object_new(); 595 obj->class_sym = csi_sym; 596 intmap_init(&obj->fields); 597 598 obj_var = rdata_var_new(vc_object); 599 obj_var->u.object_v = obj; 600 601 /* Create object fields. */ 602 node = list_first(&csi->members); 603 while (node != NULL) { 604 csimbr = list_node_data(node, stree_csimbr_t *); 605 if (csimbr->cc == csimbr_var) { 606 /* XXX Depends on member variable type. */ 607 mbr_var = rdata_var_new(vc_int); 608 mbr_var->u.int_v = rdata_int_new(); 609 mbr_var->u.int_v->value = 0; 610 611 intmap_set(&obj->fields, csimbr->u.var->name->sid, 612 mbr_var); 613 } 614 615 node = list_next(&csi->members, node); 616 } 617 618 /* Create reference to the new object. */ 619 rdata_reference(obj_var, res); 620 } 621 345 622 /** Evaluate member acccess. */ 346 623 static void run_access(run_t *run, stree_access_t *access, rdata_item_t **res) 347 624 { 348 625 rdata_item_t *rarg; 626 627 #ifdef DEBUG_RUN_TRACE 628 printf("Run access operation.\n"); 629 #endif 630 run_expr(run, access->arg, &rarg); 631 if (rarg == NULL) { 632 printf("Error: Sub-expression has no value.\n"); 633 exit(1); 634 } 635 636 run_access_item(run, access, rarg, res); 637 } 638 639 /** Evaluate member acccess (with base already evaluated). */ 640 static void run_access_item(run_t *run, stree_access_t *access, 641 rdata_item_t *arg, rdata_item_t **res) 642 { 643 var_class_t vc; 644 645 #ifdef DEBUG_RUN_TRACE 646 printf("Run access operation on pre-evaluated base.\n"); 647 #endif 648 switch (arg->ic) { 649 case ic_value: 650 vc = arg->u.value->var->vc; 651 break; 652 case ic_address: 653 vc = arg->u.address->vref->vc; 654 break; 655 default: 656 /* Silence warning. */ 657 abort(); 658 } 659 660 switch (vc) { 661 case vc_ref: 662 run_access_ref(run, access, arg, res); 663 break; 664 case vc_deleg: 665 run_access_deleg(run, access, arg, res); 666 break; 667 case vc_object: 668 run_access_object(run, access, arg, res); 669 break; 670 default: 671 printf("Unimplemented: Using access operator ('.') " 672 "with unsupported data type (value/%d).\n", vc); 673 exit(1); 674 } 675 } 676 677 /** Evaluate reference acccess. */ 678 static void run_access_ref(run_t *run, stree_access_t *access, 679 rdata_item_t *arg, rdata_item_t **res) 680 { 681 rdata_item_t *darg; 682 683 /* Implicitly dereference. */ 684 rdata_dereference(arg, &darg); 685 686 /* Try again. */ 687 run_access_item(run, access, darg, res); 688 } 689 690 /** Evaluate delegate-member acccess. */ 691 static void run_access_deleg(run_t *run, stree_access_t *access, 692 rdata_item_t *arg, rdata_item_t **res) 693 { 694 rdata_item_t *arg_vi; 695 rdata_value_t *arg_val; 349 696 rdata_deleg_t *deleg_v; 350 697 stree_symbol_t *member; 351 698 352 699 #ifdef DEBUG_RUN_TRACE 353 printf("Run access operation.\n"); 354 #endif 355 run_expr(run, access->arg, &rarg); 356 357 if (rarg->ic == ic_value && rarg->u.value->var->vc == vc_deleg) { 358 #ifdef DEBUG_RUN_TRACE 359 printf("Accessing delegate.\n"); 360 #endif 361 deleg_v = rarg->u.value->var->u.deleg_v; 362 if (deleg_v->obj != NULL || deleg_v->sym->sc != sc_csi) { 363 printf("Error: Using '.' with symbol of wrong " 364 "type (%d).\n", deleg_v->sym->sc); 365 exit(1); 366 } 367 368 member = symbol_search_csi(run->program, deleg_v->sym->u.csi, 369 access->member_name); 370 371 if (member == NULL) { 372 printf("Error: No such member.\n"); 373 exit(1); 374 } 375 376 #ifdef DEBUG_RUN_TRACE 377 printf("Found member '%s'.\n", 700 printf("Run delegate access operation.\n"); 701 #endif 702 rdata_cvt_value_item(arg, &arg_vi); 703 arg_val = arg_vi->u.value; 704 assert(arg_val->var->vc == vc_deleg); 705 706 deleg_v = arg_val->var->u.deleg_v; 707 if (deleg_v->obj != NULL || deleg_v->sym->sc != sc_csi) { 708 printf("Error: Using '.' with delegate to different object " 709 "than a CSI (%d).\n", deleg_v->sym->sc); 710 exit(1); 711 } 712 713 member = symbol_search_csi(run->program, deleg_v->sym->u.csi, 714 access->member_name); 715 716 if (member == NULL) { 717 printf("Error: CSI '"); 718 symbol_print_fqn(run->program, deleg_v->sym); 719 printf("' has no member named '%s'.\n", 378 720 strtab_get_str(access->member_name->sid)); 379 #endif 380 381 /* Reuse existing item, value, var, deleg. */ 721 exit(1); 722 } 723 724 #ifdef DEBUG_RUN_TRACE 725 printf("Found member '%s'.\n", 726 strtab_get_str(access->member_name->sid)); 727 #endif 728 729 /* 730 * Reuse existing item, value, var, deleg. 731 * XXX This is maybe not a good idea because it complicates memory 732 * management as there is not a single owner 733 */ 734 deleg_v->sym = member; 735 *res = arg; 736 } 737 738 /** Evaluate object member acccess. */ 739 static void run_access_object(run_t *run, stree_access_t *access, 740 rdata_item_t *arg, rdata_item_t **res) 741 { 742 stree_symbol_t *member; 743 rdata_object_t *object; 744 rdata_item_t *ritem; 745 rdata_address_t *address; 746 747 rdata_value_t *value; 748 rdata_deleg_t *deleg_v; 749 rdata_var_t *var; 750 751 #ifdef DEBUG_RUN_TRACE 752 printf("Run object access operation.\n"); 753 #endif 754 assert(arg->ic == ic_address); 755 assert(arg->u.value->var->vc == vc_object); 756 757 object = arg->u.value->var->u.object_v; 758 759 member = symbol_search_csi(run->program, object->class_sym->u.csi, 760 access->member_name); 761 762 if (member == NULL) { 763 printf("Error: Object of class '"); 764 symbol_print_fqn(run->program, object->class_sym); 765 printf("' has no member named '%s'.\n", 766 strtab_get_str(access->member_name->sid)); 767 exit(1); 768 } 769 770 #ifdef DEBUG_RUN_TRACE 771 printf("Found member '%s'.\n", 772 strtab_get_str(access->member_name->sid)); 773 #endif 774 775 switch (member->sc) { 776 case sc_csi: 777 printf("Error: Accessing object member which is nested CSI.\n"); 778 exit(1); 779 case sc_fun: 780 /* Construct delegate. */ 781 ritem = rdata_item_new(ic_value); 782 value = rdata_value_new(); 783 ritem->u.value = value; 784 785 var = rdata_var_new(vc_deleg); 786 value->var = var; 787 deleg_v = rdata_deleg_new(); 788 var->u.deleg_v = deleg_v; 789 790 deleg_v->obj = arg->u.value->var; 382 791 deleg_v->sym = member; 383 384 *res = rarg; 385 return; 386 } 387 388 *res = NULL; 792 break; 793 case sc_var: 794 /* Construct variable address item. */ 795 ritem = rdata_item_new(ic_address); 796 address = rdata_address_new(); 797 ritem->u.address = address; 798 799 address->vref = intmap_get(&object->fields, 800 access->member_name->sid); 801 assert(address->vref != NULL); 802 break; 803 case sc_prop: 804 printf("Unimplemented: Accessing object property.\n"); 805 exit(1); 806 } 807 808 *res = ritem; 389 809 } 390 810 … … 399 819 rdata_item_t *rarg_i, *rarg_vi; 400 820 821 stree_fun_t *fun; 822 run_fun_ar_t *fun_ar; 823 401 824 #ifdef DEBUG_RUN_TRACE 402 825 printf("Run call operation.\n"); … … 422 845 printf("'\n"); 423 846 #endif 424 425 847 /* Evaluate function arguments. */ 426 848 list_init(&arg_vals); … … 430 852 arg = list_node_data(node, stree_expr_t *); 431 853 run_expr(run, arg, &rarg_i); 432 r un_cvt_value_item(run,rarg_i, &rarg_vi);854 rdata_cvt_value_item(rarg_i, &rarg_vi); 433 855 434 856 list_append(&arg_vals, rarg_vi); … … 436 858 } 437 859 438 run_fun(run, deleg_v->sym->u.fun, &arg_vals); 860 fun = symbol_to_fun(deleg_v->sym); 861 assert(fun != NULL); 862 863 /* Create function activation record. */ 864 run_fun_ar_create(run, deleg_v->obj, fun, &fun_ar); 865 866 /* Fill in argument values. */ 867 run_fun_ar_set_args(run, fun_ar, &arg_vals); 868 869 /* Run the function. */ 870 run_fun(run, fun_ar, res); 439 871 440 872 #ifdef DEBUG_RUN_TRACE 441 873 printf("Returned from function call.\n"); 442 874 #endif 443 *res = NULL;444 875 } 445 876 … … 457 888 run_expr(run, assign->src, &rsrc_i); 458 889 459 r un_cvt_value_item(run,rsrc_i, &rsrc_vi);890 rdata_cvt_value_item(rsrc_i, &rsrc_vi); 460 891 src_val = rsrc_vi->u.value; 461 892 … … 466 897 } 467 898 468 r un_address_write(run,rdest_i->u.address, rsrc_vi->u.value);899 rdata_address_write(rdest_i->u.address, rsrc_vi->u.value); 469 900 470 901 *res = NULL; … … 483 914 rdata_var_t *var; 484 915 485 run_cvt_value_item(run, item, &vitem); 916 (void) run; 917 rdata_cvt_value_item(item, &vitem); 486 918 487 919 assert(vitem->ic == ic_value); … … 495 927 return (var->u.int_v->value != 0); 496 928 } 497 498 /** Convert item to value item.499 *500 * If @a item is a value, we just return a copy. If @a item is an address,501 * we read from the address.502 */503 void run_cvt_value_item(run_t *run, rdata_item_t *item,504 rdata_item_t **ritem)505 {506 rdata_value_t *value;507 508 /* Address item. Perform read operation. */509 if (item->ic == ic_address) {510 run_address_read(run, item->u.address, ritem);511 return;512 }513 514 /* It already is a value, we can share the @c var. */515 value = rdata_value_new();516 value->var = item->u.value->var;517 *ritem = rdata_item_new(ic_value);518 (*ritem)->u.value = value;519 }520 521 /** Read data from an address.522 *523 * Return value stored in a variable at the specified address.524 */525 static void run_address_read(run_t *run, rdata_address_t *address,526 rdata_item_t **ritem)527 {528 rdata_value_t *value;529 rdata_var_t *rvar;530 (void) run;531 532 /* Perform a shallow copy of @c var. */533 rdata_var_copy(address->vref, &rvar);534 535 value = rdata_value_new();536 value->var = rvar;537 *ritem = rdata_item_new(ic_value);538 (*ritem)->u.value = value;539 }540 541 /** Write data to an address.542 *543 * Store @a value to the variable at @a address.544 */545 static void run_address_write(run_t *run, rdata_address_t *address,546 rdata_value_t *value)547 {548 rdata_var_t *nvar;549 rdata_var_t *orig_var;550 551 /* Perform a shallow copy of @c value->var. */552 rdata_var_copy(value->var, &nvar);553 orig_var = address->vref;554 555 /* XXX do this in a prettier way. */556 557 orig_var->vc = nvar->vc;558 switch (nvar->vc) {559 case vc_int: orig_var->u.int_v = nvar->u.int_v; break;560 case vc_ref: orig_var->u.ref_v = nvar->u.ref_v; break;561 case vc_deleg: orig_var->u.deleg_v = nvar->u.deleg_v; break;562 case vc_object: orig_var->u.object_v = nvar->u.object_v; break;563 default: assert(b_false);564 }565 566 /* XXX We should free some stuff around here. */567 } -
uspace/app/sbi/src/run_expr.h
r09ababb7 rfa36f29 34 34 void run_expr(run_t *run, stree_expr_t *expr, rdata_item_t **res); 35 35 36 void run_cvt_value_item(run_t *run, rdata_item_t *item,37 rdata_item_t **ritem);38 36 bool_t run_item_boolean_value(run_t *run, rdata_item_t *item); 39 37 -
uspace/app/sbi/src/run_t.h
r09ababb7 rfa36f29 43 43 } run_block_ar_t; 44 44 45 45 46 /** Function activation record 46 47 * … … 48 49 */ 49 50 typedef struct run_fun_ar { 51 /** Object on which the member function is being invoked or @c NULL. */ 52 struct rdata_var *obj; 53 50 54 /** Definition of function being invoked */ 51 55 struct stree_symbol *fun_sym; … … 53 57 /** Block activation records */ 54 58 list_t block_ar; /* of run_block_ar_t */ 59 60 /** Function return value or @c NULL if not set. */ 61 struct rdata_item *retval; 55 62 } run_fun_ar_t; 63 64 /** Bailout mode 65 * 66 * Determines whether control is bailing out of a statement, function, etc. 67 */ 68 typedef enum { 69 /** Normal execution */ 70 bm_none, 71 72 /** Break from statement */ 73 bm_stat, 74 75 /** Return from function */ 76 bm_fun 77 } run_bailout_mode; 56 78 57 79 /** Thread activation record … … 60 82 */ 61 83 typedef struct run_thread_ar { 62 /** Function activation records .*/84 /** Function activation records */ 63 85 list_t fun_ar; /* of run_fun_ar_t */ 86 87 /** Bailout mode */ 88 run_bailout_mode bo_mode; 64 89 } run_thread_ar_t; 65 90 -
uspace/app/sbi/src/stree.c
r09ababb7 rfa36f29 228 228 } 229 229 230 stree_return_t *stree_return_new(void) 231 { 232 stree_return_t *return_s; 233 234 return_s = calloc(1, sizeof(stree_return_t)); 235 if (return_s == NULL) { 236 printf("Memory allocation failed.\n"); 237 exit(1); 238 } 239 240 return return_s; 241 } 242 230 243 stree_wef_t *stree_wef_new(void) 231 244 { … … 309 322 } 310 323 324 stree_new_t *stree_new_new(void) 325 { 326 stree_new_t *new_op; 327 328 new_op = calloc(1, sizeof(stree_new_t)); 329 if (new_op == NULL) { 330 printf("Memory allocation failed.\n"); 331 exit(1); 332 } 333 334 return new_op; 335 } 336 311 337 stree_access_t *stree_access_new(void) 312 338 { … … 375 401 } 376 402 403 stree_self_ref_t *stree_self_ref_new(void) 404 { 405 stree_self_ref_t *self_ref; 406 407 self_ref = calloc(1, sizeof(stree_self_ref_t)); 408 if (self_ref == NULL) { 409 printf("Memory allocation failed.\n"); 410 exit(1); 411 } 412 413 return self_ref; 414 } 415 377 416 stree_texpr_t *stree_texpr_new(texpr_class_t tc) 378 417 { … … 415 454 } 416 455 456 stree_tliteral_t *stree_tliteral_new(void) 457 { 458 stree_tliteral_t *tliteral; 459 460 tliteral = calloc(1, sizeof(stree_tliteral_t)); 461 if (tliteral == NULL) { 462 printf("Memory allocation failed.\n"); 463 exit(1); 464 } 465 466 return tliteral; 467 } 468 417 469 stree_tnameref_t *stree_tnameref_new(void) 418 470 { -
uspace/app/sbi/src/stree.h
r09ababb7 rfa36f29 48 48 stree_for_t *stree_for_new(void); 49 49 stree_raise_t *stree_raise_new(void); 50 stree_return_t *stree_return_new(void); 50 51 stree_wef_t *stree_wef_new(void); 51 52 stree_exps_t *stree_exps_new(void); … … 55 56 stree_assign_t *stree_assign_new(assign_class_t ac); 56 57 stree_binop_t *stree_binop_new(binop_class_t bc); 58 stree_new_t *stree_new_new(void); 57 59 stree_access_t *stree_access_new(void); 58 60 stree_call_t *stree_call_new(void); … … 61 63 stree_ident_t *stree_ident_new(void); 62 64 stree_literal_t *stree_literal_new(literal_class_t ltc); 65 stree_self_ref_t *stree_self_ref_new(void); 63 66 64 67 stree_texpr_t *stree_texpr_new(texpr_class_t tc); 65 68 stree_tapply_t *stree_tapply_new(void); 66 69 stree_taccess_t *stree_taccess_new(void); 70 stree_tliteral_t *stree_tliteral_new(void); 67 71 stree_tnameref_t *stree_tnameref_new(void); 68 72 -
uspace/app/sbi/src/stree_t.h
r09ababb7 rfa36f29 48 48 } stree_nameref_t; 49 49 50 /** Reference to currently active object. */ 51 typedef struct { 52 } stree_self_ref_t; 53 50 54 typedef struct { 51 55 int value; 52 56 } stree_lit_int_t; 53 57 58 /** Reference literal (there is only one: @c nil). */ 59 typedef struct { 60 } stree_lit_ref_t; 61 54 62 typedef struct { 55 63 char *value; … … 58 66 typedef enum { 59 67 ltc_int, 68 ltc_ref, 60 69 ltc_string 61 70 } literal_class_t; … … 66 75 union { 67 76 stree_lit_int_t lit_int; 77 stree_lit_ref_t lit_ref; 68 78 stree_lit_string_t lit_string; 69 79 } u; … … 107 117 } stree_unop_t; 108 118 119 /** New operation */ 120 typedef struct { 121 /** Type of object to construct. */ 122 struct stree_texpr *texpr; 123 } stree_new_t; 124 109 125 /** Member access operation */ 110 126 typedef struct { … … 139 155 ec_nameref, 140 156 ec_literal, 157 ec_self_ref, 141 158 ec_binop, 142 159 ec_unop, 160 ec_new, 143 161 ec_access, 144 162 ec_call, … … 153 171 stree_nameref_t *nameref; 154 172 stree_literal_t *literal; 173 stree_self_ref_t *self_ref; 155 174 stree_binop_t *binop; 156 175 stree_unop_t *unop; 176 stree_new_t *new_op; 157 177 stree_access_t *access; 158 178 stree_call_t *call; … … 166 186 167 187 struct stree_texpr; 188 189 /** Type literal class */ 190 typedef enum { 191 tlc_int, 192 tlc_string 193 } tliteral_class_t; 194 195 /** Type literal */ 196 typedef struct { 197 tliteral_class_t tlc; 198 } stree_tliteral_t; 168 199 169 200 /** Type name reference */ … … 188 219 /** Type expression class */ 189 220 typedef enum { 221 tc_tliteral, 190 222 tc_tnameref, 191 223 tc_taccess, … … 198 230 199 231 union { 232 stree_tliteral_t *tliteral; 200 233 stree_tnameref_t *tnameref; 201 234 stree_taccess_t *taccess; … … 241 274 typedef struct { 242 275 } stree_raise_t; 276 277 /** Return statement */ 278 typedef struct { 279 stree_expr_t *expr; 280 } stree_return_t; 243 281 244 282 /** Expression statement */ … … 261 299 st_for, 262 300 st_raise, 301 st_return, 263 302 st_exps, 264 303 st_wef … … 275 314 stree_for_t *for_s; 276 315 stree_raise_t *raise_s; 316 stree_return_t *return_s; 277 317 stree_exps_t *exp_s; 278 318 stree_wef_t *wef_s; -
uspace/app/sbi/src/symbol.c
r09ababb7 rfa36f29 40 40 static stree_symbol_t *symbol_search_global(stree_program_t *prog, 41 41 stree_ident_t *name); 42 static stree_symbol_t *symbol_find_epoint_rec(stree_program_t *prog, 43 stree_ident_t *name, stree_csi_t *csi); 42 44 static stree_ident_t *symbol_get_ident(stree_symbol_t *symbol); 43 45 … … 92 94 93 95 if (symbol == NULL) { 94 printf("Error: Symbol '%s' not found \n", strtab_get_str(name->sid));96 printf("Error: Symbol '%s' not found.\n", strtab_get_str(name->sid)); 95 97 exit(1); 96 98 } … … 111 113 stree_symbol_t *symbol; 112 114 stree_ident_t *mbr_name; 115 stree_symbol_t *base_csi_sym; 116 stree_csi_t *base_csi; 117 118 (void) prog; 119 120 /* Look in new members in this class. */ 113 121 114 122 node = list_first(&scope->members); … … 146 154 } 147 155 156 /* Try inherited members. */ 157 if (scope->base_csi_ref != NULL) { 158 base_csi_sym = symbol_xlookup_in_csi(prog, 159 csi_to_symbol(scope)->outer_csi, scope->base_csi_ref); 160 base_csi = symbol_to_csi(base_csi_sym); 161 assert(base_csi != NULL); 162 163 return symbol_search_csi(prog, base_csi, name); 164 } 165 166 /* No match */ 148 167 return NULL; 149 168 } … … 176 195 } 177 196 197 /** Find entry point. */ 198 stree_symbol_t *symbol_find_epoint(stree_program_t *prog, stree_ident_t *name) 199 { 200 list_node_t *node; 201 stree_modm_t *modm; 202 stree_symbol_t *entry, *etmp; 203 204 entry = NULL; 205 206 node = list_first(&prog->module->members); 207 while (node != NULL) { 208 modm = list_node_data(node, stree_modm_t *); 209 if (modm->mc == mc_csi) { 210 etmp = symbol_find_epoint_rec(prog, name, modm->u.csi); 211 if (etmp != NULL) { 212 if (entry != NULL) { 213 printf("Error: Duplicate entry point.\n"); 214 exit(1); 215 } 216 entry = etmp; 217 } 218 } 219 node = list_next(&prog->module->members, node); 220 } 221 222 return entry; 223 } 224 225 static stree_symbol_t *symbol_find_epoint_rec(stree_program_t *prog, 226 stree_ident_t *name, stree_csi_t *csi) 227 { 228 list_node_t *node; 229 stree_csimbr_t *csimbr; 230 stree_symbol_t *entry, *etmp; 231 232 entry = NULL; 233 234 node = list_first(&csi->members); 235 while (node != NULL) { 236 csimbr = list_node_data(node, stree_csimbr_t *); 237 238 switch (csimbr->cc) { 239 case csimbr_csi: 240 etmp = symbol_find_epoint_rec(prog, name, csimbr->u.csi); 241 if (etmp != NULL) { 242 if (entry != NULL) { 243 printf("Error: Duplicate entry point.\n"); 244 exit(1); 245 } 246 entry = etmp; 247 } 248 break; 249 case csimbr_fun: 250 if (csimbr->u.fun->name->sid == name->sid) { 251 if (entry != NULL) { 252 printf("Error: Duplicate entry point.\n"); 253 exit(1); 254 } 255 entry = fun_to_symbol(csimbr->u.fun); 256 } 257 default: 258 break; 259 } 260 261 node = list_next(&csi->members, node); 262 } 263 264 return entry; 265 } 266 178 267 stree_csi_t *symbol_to_csi(stree_symbol_t *symbol) 179 268 { -
uspace/app/sbi/src/symbol.h
r09ababb7 rfa36f29 38 38 stree_symbol_t *symbol_search_csi(stree_program_t *prog, stree_csi_t *scope, 39 39 stree_ident_t *name); 40 40 stree_symbol_t *symbol_find_epoint(stree_program_t *prog, stree_ident_t *name); 41 41 42 42 stree_csi_t *symbol_to_csi(stree_symbol_t *symbol); -
uspace/dist/sysel/count.sy
r09ababb7 rfa36f29 1 class Count eris2 fun count(a : Int; b : Int) is3 var i : Int;1 class CountDemo is 2 fun Count(a : int; b : int) is 3 var i : int; 4 4 5 5 i = a; … … 10 10 11 11 end 12 end13 12 14 class HelloWorld is 15 fun main() is 16 Counter.count(0, 10); 13 fun Main() is 14 Count(0, 10); 17 15 end 18 16 end -
uspace/dist/sysel/hello.sy
r09ababb7 rfa36f29 1 1 class HelloWorld is 2 fun main() is2 fun Main() is 3 3 Builtin.WriteLine("Hello world!"); 4 4 end
Note:
See TracChangeset
for help on using the changeset viewer.