[09ababb7] | 1 | /*
|
---|
[051b3db8] | 2 | * Copyright (c) 2011 Jiri Svoboda
|
---|
[09ababb7] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[38aaacc2] | 29 | /** @file Run expressions. */
|
---|
[09ababb7] | 30 |
|
---|
| 31 | #include <stdio.h>
|
---|
| 32 | #include <stdlib.h>
|
---|
| 33 | #include <assert.h>
|
---|
[23de644] | 34 | #include "bigint.h"
|
---|
[09ababb7] | 35 | #include "debug.h"
|
---|
[fa36f29] | 36 | #include "intmap.h"
|
---|
[09ababb7] | 37 | #include "list.h"
|
---|
| 38 | #include "mytypes.h"
|
---|
[94d484a] | 39 | #include "os/os.h"
|
---|
[09ababb7] | 40 | #include "rdata.h"
|
---|
| 41 | #include "run.h"
|
---|
[94d484a] | 42 | #include "run_texpr.h"
|
---|
[09ababb7] | 43 | #include "symbol.h"
|
---|
[d0febca] | 44 | #include "stree.h"
|
---|
[09ababb7] | 45 | #include "strtab.h"
|
---|
[39e8406] | 46 | #include "tdata.h"
|
---|
[09ababb7] | 47 |
|
---|
| 48 | #include "run_expr.h"
|
---|
| 49 |
|
---|
| 50 | static void run_nameref(run_t *run, stree_nameref_t *nameref,
|
---|
| 51 | rdata_item_t **res);
|
---|
[fa36f29] | 52 |
|
---|
[09ababb7] | 53 | static void run_literal(run_t *run, stree_literal_t *literal,
|
---|
| 54 | rdata_item_t **res);
|
---|
[074444f] | 55 | static void run_lit_bool(run_t *run, stree_lit_bool_t *lit_bool,
|
---|
| 56 | rdata_item_t **res);
|
---|
| 57 | static void run_lit_char(run_t *run, stree_lit_char_t *lit_char,
|
---|
| 58 | rdata_item_t **res);
|
---|
[09ababb7] | 59 | static void run_lit_int(run_t *run, stree_lit_int_t *lit_int,
|
---|
| 60 | rdata_item_t **res);
|
---|
[fa36f29] | 61 | static void run_lit_ref(run_t *run, stree_lit_ref_t *lit_ref,
|
---|
| 62 | rdata_item_t **res);
|
---|
[09ababb7] | 63 | static void run_lit_string(run_t *run, stree_lit_string_t *lit_string,
|
---|
| 64 | rdata_item_t **res);
|
---|
[fa36f29] | 65 |
|
---|
| 66 | static void run_self_ref(run_t *run, stree_self_ref_t *self_ref,
|
---|
| 67 | rdata_item_t **res);
|
---|
| 68 |
|
---|
[09ababb7] | 69 | static void run_binop(run_t *run, stree_binop_t *binop, rdata_item_t **res);
|
---|
[074444f] | 70 | static void run_binop_bool(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
|
---|
| 71 | rdata_value_t *v2, rdata_item_t **res);
|
---|
| 72 | static void run_binop_char(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
|
---|
| 73 | rdata_value_t *v2, rdata_item_t **res);
|
---|
[fa36f29] | 74 | static void run_binop_int(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
|
---|
| 75 | rdata_value_t *v2, rdata_item_t **res);
|
---|
[94d484a] | 76 | static void run_binop_string(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
|
---|
| 77 | rdata_value_t *v2, rdata_item_t **res);
|
---|
[fa36f29] | 78 | static void run_binop_ref(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
|
---|
| 79 | rdata_value_t *v2, rdata_item_t **res);
|
---|
[051bc69a] | 80 | static void run_binop_enum(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
|
---|
| 81 | rdata_value_t *v2, rdata_item_t **res);
|
---|
[fa36f29] | 82 |
|
---|
[09ababb7] | 83 | static void run_unop(run_t *run, stree_unop_t *unop, rdata_item_t **res);
|
---|
[051bc69a] | 84 | static void run_unop_bool(run_t *run, stree_unop_t *unop, rdata_value_t *val,
|
---|
| 85 | rdata_item_t **res);
|
---|
[23de644] | 86 | static void run_unop_int(run_t *run, stree_unop_t *unop, rdata_value_t *val,
|
---|
| 87 | rdata_item_t **res);
|
---|
| 88 |
|
---|
[fa36f29] | 89 | static void run_new(run_t *run, stree_new_t *new_op, rdata_item_t **res);
|
---|
[94d484a] | 90 | static void run_new_array(run_t *run, stree_new_t *new_op,
|
---|
[39e8406] | 91 | tdata_item_t *titem, rdata_item_t **res);
|
---|
[94d484a] | 92 | static void run_new_object(run_t *run, stree_new_t *new_op,
|
---|
[39e8406] | 93 | tdata_item_t *titem, rdata_item_t **res);
|
---|
[fa36f29] | 94 |
|
---|
[051bc69a] | 95 | static void run_object_ctor(run_t *run, rdata_var_t *obj, list_t *arg_vals);
|
---|
| 96 |
|
---|
[09ababb7] | 97 | static void run_access(run_t *run, stree_access_t *access, rdata_item_t **res);
|
---|
[fa36f29] | 98 | static void run_access_item(run_t *run, stree_access_t *access,
|
---|
| 99 | rdata_item_t *arg, rdata_item_t **res);
|
---|
| 100 | static void run_access_ref(run_t *run, stree_access_t *access,
|
---|
| 101 | rdata_item_t *arg, rdata_item_t **res);
|
---|
| 102 | static void run_access_deleg(run_t *run, stree_access_t *access,
|
---|
| 103 | rdata_item_t *arg, rdata_item_t **res);
|
---|
| 104 | static void run_access_object(run_t *run, stree_access_t *access,
|
---|
| 105 | rdata_item_t *arg, rdata_item_t **res);
|
---|
[c5cb943d] | 106 | static void run_access_object_static(run_t *run, stree_access_t *access,
|
---|
| 107 | rdata_var_t *obj_var, rdata_item_t **res);
|
---|
| 108 | static void run_access_object_nonstatic(run_t *run, stree_access_t *access,
|
---|
| 109 | rdata_var_t *obj_var, rdata_item_t **res);
|
---|
[051bc69a] | 110 | static void run_access_symbol(run_t *run, stree_access_t *access,
|
---|
| 111 | rdata_item_t *arg, rdata_item_t **res);
|
---|
[fa36f29] | 112 |
|
---|
[09ababb7] | 113 | static void run_call(run_t *run, stree_call_t *call, rdata_item_t **res);
|
---|
[051bc69a] | 114 | static void run_call_args(run_t *run, list_t *args, list_t *arg_vals);
|
---|
[051b3db8] | 115 | static void run_destroy_arg_vals(list_t *arg_vals);
|
---|
[051bc69a] | 116 |
|
---|
[94d484a] | 117 | static void run_index(run_t *run, stree_index_t *index, rdata_item_t **res);
|
---|
[d0febca] | 118 | static void run_index_array(run_t *run, stree_index_t *index,
|
---|
| 119 | rdata_item_t *base, list_t *args, rdata_item_t **res);
|
---|
| 120 | static void run_index_object(run_t *run, stree_index_t *index,
|
---|
| 121 | rdata_item_t *base, list_t *args, rdata_item_t **res);
|
---|
| 122 | static void run_index_string(run_t *run, stree_index_t *index,
|
---|
| 123 | rdata_item_t *base, list_t *args, rdata_item_t **res);
|
---|
[09ababb7] | 124 | static void run_assign(run_t *run, stree_assign_t *assign, rdata_item_t **res);
|
---|
[37f527b] | 125 | static void run_as(run_t *run, stree_as_t *as_op, rdata_item_t **res);
|
---|
[38aaacc2] | 126 | static void run_box(run_t *run, stree_box_t *box, rdata_item_t **res);
|
---|
[09ababb7] | 127 |
|
---|
[38aaacc2] | 128 | /** Evaluate expression.
|
---|
| 129 | *
|
---|
| 130 | * Run the expression @a expr and store pointer to the result in *(@a res).
|
---|
| 131 | * If the expression has on value (assignment) then @c NULL is returned.
|
---|
| 132 | * @c NULL is also returned if an error or exception occurs.
|
---|
| 133 | *
|
---|
| 134 | * @param run Runner object
|
---|
| 135 | * @param expr Expression to run
|
---|
| 136 | * @param res Place to store result
|
---|
| 137 | */
|
---|
[09ababb7] | 138 | void run_expr(run_t *run, stree_expr_t *expr, rdata_item_t **res)
|
---|
| 139 | {
|
---|
| 140 | #ifdef DEBUG_RUN_TRACE
|
---|
| 141 | printf("Executing expression.\n");
|
---|
| 142 | #endif
|
---|
| 143 |
|
---|
| 144 | switch (expr->ec) {
|
---|
| 145 | case ec_nameref:
|
---|
| 146 | run_nameref(run, expr->u.nameref, res);
|
---|
| 147 | break;
|
---|
| 148 | case ec_literal:
|
---|
| 149 | run_literal(run, expr->u.literal, res);
|
---|
| 150 | break;
|
---|
[fa36f29] | 151 | case ec_self_ref:
|
---|
| 152 | run_self_ref(run, expr->u.self_ref, res);
|
---|
| 153 | break;
|
---|
[09ababb7] | 154 | case ec_binop:
|
---|
| 155 | run_binop(run, expr->u.binop, res);
|
---|
| 156 | break;
|
---|
| 157 | case ec_unop:
|
---|
| 158 | run_unop(run, expr->u.unop, res);
|
---|
| 159 | break;
|
---|
[fa36f29] | 160 | case ec_new:
|
---|
| 161 | run_new(run, expr->u.new_op, res);
|
---|
| 162 | break;
|
---|
[09ababb7] | 163 | case ec_access:
|
---|
| 164 | run_access(run, expr->u.access, res);
|
---|
| 165 | break;
|
---|
| 166 | case ec_call:
|
---|
| 167 | run_call(run, expr->u.call, res);
|
---|
| 168 | break;
|
---|
[94d484a] | 169 | case ec_index:
|
---|
| 170 | run_index(run, expr->u.index, res);
|
---|
| 171 | break;
|
---|
[09ababb7] | 172 | case ec_assign:
|
---|
| 173 | run_assign(run, expr->u.assign, res);
|
---|
| 174 | break;
|
---|
[37f527b] | 175 | case ec_as:
|
---|
| 176 | run_as(run, expr->u.as_op, res);
|
---|
| 177 | break;
|
---|
[38aaacc2] | 178 | case ec_box:
|
---|
| 179 | run_box(run, expr->u.box, res);
|
---|
| 180 | break;
|
---|
[09ababb7] | 181 | }
|
---|
| 182 |
|
---|
| 183 | #ifdef DEBUG_RUN_TRACE
|
---|
| 184 | printf("Expression result: ");
|
---|
| 185 | rdata_item_print(*res);
|
---|
| 186 | printf(".\n");
|
---|
| 187 | #endif
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[38aaacc2] | 190 | /** Evaluate name reference expression.
|
---|
| 191 | *
|
---|
| 192 | * @param run Runner object
|
---|
| 193 | * @param nameref Name reference
|
---|
| 194 | * @param res Place to store result
|
---|
| 195 | */
|
---|
[09ababb7] | 196 | static void run_nameref(run_t *run, stree_nameref_t *nameref,
|
---|
| 197 | rdata_item_t **res)
|
---|
| 198 | {
|
---|
| 199 | stree_symbol_t *sym;
|
---|
| 200 | rdata_item_t *item;
|
---|
| 201 | rdata_address_t *address;
|
---|
[d0febca] | 202 | rdata_addr_var_t *addr_var;
|
---|
[051b3db8] | 203 | rdata_addr_prop_t *addr_prop;
|
---|
| 204 | rdata_aprop_named_t *aprop_named;
|
---|
| 205 | rdata_deleg_t *deleg_p;
|
---|
[fa36f29] | 206 | rdata_value_t *value;
|
---|
[09ababb7] | 207 | rdata_var_t *var;
|
---|
| 208 | rdata_deleg_t *deleg_v;
|
---|
[051bc69a] | 209 | rdata_symbol_t *symbol_v;
|
---|
[09ababb7] | 210 |
|
---|
[d0febca] | 211 | run_proc_ar_t *proc_ar;
|
---|
[fa36f29] | 212 | stree_symbol_t *csi_sym;
|
---|
| 213 | stree_csi_t *csi;
|
---|
| 214 | rdata_object_t *obj;
|
---|
| 215 | rdata_var_t *member_var;
|
---|
| 216 |
|
---|
[c5cb943d] | 217 | rdata_var_t *psobj;
|
---|
| 218 | rdata_var_t *sobj;
|
---|
| 219 | rdata_object_t *aobj;
|
---|
| 220 |
|
---|
[09ababb7] | 221 | #ifdef DEBUG_RUN_TRACE
|
---|
| 222 | printf("Run nameref.\n");
|
---|
| 223 | #endif
|
---|
| 224 |
|
---|
| 225 | /*
|
---|
| 226 | * Look for a local variable.
|
---|
| 227 | */
|
---|
| 228 | var = run_local_vars_lookup(run, nameref->name->sid);
|
---|
| 229 | if (var != NULL) {
|
---|
| 230 | /* Found a local variable. */
|
---|
| 231 | item = rdata_item_new(ic_address);
|
---|
[d0febca] | 232 | address = rdata_address_new(ac_var);
|
---|
| 233 | addr_var = rdata_addr_var_new();
|
---|
[09ababb7] | 234 |
|
---|
| 235 | item->u.address = address;
|
---|
[d0febca] | 236 | address->u.var_a = addr_var;
|
---|
| 237 | addr_var->vref = var;
|
---|
[09ababb7] | 238 |
|
---|
| 239 | *res = item;
|
---|
| 240 | #ifdef DEBUG_RUN_TRACE
|
---|
| 241 | printf("Found local variable.\n");
|
---|
| 242 | #endif
|
---|
| 243 | return;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | /*
|
---|
| 247 | * Look for a class-wide or global symbol.
|
---|
| 248 | */
|
---|
| 249 |
|
---|
[fa36f29] | 250 | /* Determine currently active object or CSI. */
|
---|
[d0febca] | 251 | proc_ar = run_get_current_proc_ar(run);
|
---|
[c5cb943d] | 252 |
|
---|
| 253 | assert (proc_ar->obj != NULL);
|
---|
| 254 | assert(proc_ar->obj->vc == vc_object);
|
---|
| 255 | obj = proc_ar->obj->u.object_v;
|
---|
| 256 | csi_sym = obj->class_sym;
|
---|
| 257 |
|
---|
| 258 | if (csi_sym != NULL) {
|
---|
[fa36f29] | 259 | csi = symbol_to_csi(csi_sym);
|
---|
| 260 | assert(csi != NULL);
|
---|
| 261 | } else {
|
---|
[c5cb943d] | 262 | /* This happens in interactive mode. */
|
---|
| 263 | csi = NULL;
|
---|
[fa36f29] | 264 | }
|
---|
| 265 |
|
---|
| 266 | sym = symbol_lookup_in_csi(run->program, csi, nameref->name);
|
---|
[09ababb7] | 267 |
|
---|
[1ebc1a62] | 268 | /* Existence should have been verified in type checking phase. */
|
---|
| 269 | assert(sym != NULL);
|
---|
| 270 |
|
---|
[09ababb7] | 271 | switch (sym->sc) {
|
---|
| 272 | case sc_csi:
|
---|
| 273 | #ifdef DEBUG_RUN_TRACE
|
---|
[051bc69a] | 274 | printf("Referencing CSI.\n");
|
---|
[09ababb7] | 275 | #endif
|
---|
[c5cb943d] | 276 | /* Obtain static object for the referenced CSI. */
|
---|
| 277 | psobj = run->gdata; /* XXX */
|
---|
| 278 | sobj = run_sobject_get(run, sym->u.csi, psobj,
|
---|
| 279 | nameref->name->sid);
|
---|
[09ababb7] | 280 |
|
---|
[c5cb943d] | 281 | /* Return reference to the object. */
|
---|
| 282 | run_reference(run, sobj, res);
|
---|
[fa36f29] | 283 | break;
|
---|
[051bc69a] | 284 | case sc_ctor:
|
---|
| 285 | /* It is not possible to reference a constructor explicitly. */
|
---|
| 286 | assert(b_false);
|
---|
[dc12262] | 287 | /* Fallthrough */
|
---|
[051bc69a] | 288 | case sc_enum:
|
---|
| 289 | #ifdef DEBUG_RUN_TRACE
|
---|
| 290 | printf("Referencing enum.\n");
|
---|
| 291 | #endif
|
---|
| 292 | item = rdata_item_new(ic_value);
|
---|
| 293 | value = rdata_value_new();
|
---|
| 294 | var = rdata_var_new(vc_symbol);
|
---|
| 295 | symbol_v = rdata_symbol_new();
|
---|
| 296 |
|
---|
| 297 | item->u.value = value;
|
---|
| 298 | value->var = var;
|
---|
| 299 | var->u.symbol_v = symbol_v;
|
---|
| 300 |
|
---|
| 301 | symbol_v->sym = sym;
|
---|
| 302 | *res = item;
|
---|
| 303 | break;
|
---|
| 304 | case sc_deleg:
|
---|
| 305 | /* XXX TODO */
|
---|
| 306 | printf("Unimplemented: Delegate name reference.\n");
|
---|
| 307 | abort();
|
---|
| 308 | break;
|
---|
[fa36f29] | 309 | case sc_fun:
|
---|
| 310 | /* There should be no global functions. */
|
---|
| 311 | assert(csi != NULL);
|
---|
| 312 |
|
---|
[38aaacc2] | 313 | if (symbol_search_csi(run->program, csi, nameref->name)
|
---|
| 314 | == NULL) {
|
---|
[fa36f29] | 315 | /* Function is not in the current object. */
|
---|
| 316 | printf("Error: Cannot access non-static member "
|
---|
| 317 | "function '");
|
---|
[39e8406] | 318 | symbol_print_fqn(sym);
|
---|
[fa36f29] | 319 | printf("' from nested CSI '");
|
---|
[39e8406] | 320 | symbol_print_fqn(csi_sym);
|
---|
[fa36f29] | 321 | printf("'.\n");
|
---|
| 322 | exit(1);
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | /* Construct delegate. */
|
---|
| 326 | item = rdata_item_new(ic_value);
|
---|
| 327 | value = rdata_value_new();
|
---|
| 328 | item->u.value = value;
|
---|
| 329 |
|
---|
| 330 | var = rdata_var_new(vc_deleg);
|
---|
| 331 | deleg_v = rdata_deleg_new();
|
---|
| 332 | value->var = var;
|
---|
| 333 | var->u.deleg_v = deleg_v;
|
---|
| 334 |
|
---|
[d0febca] | 335 | deleg_v->obj = proc_ar->obj;
|
---|
[fa36f29] | 336 | deleg_v->sym = sym;
|
---|
| 337 |
|
---|
| 338 | *res = item;
|
---|
| 339 | break;
|
---|
| 340 | case sc_var:
|
---|
[051b3db8] | 341 | case sc_prop:
|
---|
[fa36f29] | 342 | #ifdef DEBUG_RUN_TRACE
|
---|
[051b3db8] | 343 | if (sym->sc == sc_var)
|
---|
| 344 | printf("Referencing member variable.\n");
|
---|
| 345 | else
|
---|
| 346 | printf("Referencing unqualified property.\n");
|
---|
[fa36f29] | 347 | #endif
|
---|
[051b3db8] | 348 | /* There should be no global variables or properties. */
|
---|
[fa36f29] | 349 | assert(csi != NULL);
|
---|
| 350 |
|
---|
[38aaacc2] | 351 | if (symbol_search_csi(run->program, csi, nameref->name)
|
---|
[c5cb943d] | 352 | == NULL && !stree_symbol_is_static(sym)) {
|
---|
[051b3db8] | 353 | /* Symbol is not in the current object. */
|
---|
[fa36f29] | 354 | printf("Error: Cannot access non-static member "
|
---|
| 355 | "variable '");
|
---|
[39e8406] | 356 | symbol_print_fqn(sym);
|
---|
[fa36f29] | 357 | printf("' from nested CSI '");
|
---|
[39e8406] | 358 | symbol_print_fqn(csi_sym);
|
---|
[fa36f29] | 359 | printf("'.\n");
|
---|
| 360 | exit(1);
|
---|
| 361 | }
|
---|
| 362 |
|
---|
[051b3db8] | 363 | /*
|
---|
| 364 | * Determine object in which the symbol resides
|
---|
| 365 | */
|
---|
[c5cb943d] | 366 | if (stree_symbol_is_static(sym)) {
|
---|
| 367 | /*
|
---|
[051b3db8] | 368 | * Class object
|
---|
[c5cb943d] | 369 | * XXX This is too slow!
|
---|
| 370 | *
|
---|
| 371 | * However fixing this is non-trivial. We would
|
---|
| 372 | * have to have pointer to static object available
|
---|
| 373 | * for each object (therefore also for each object
|
---|
| 374 | * type).
|
---|
| 375 | */
|
---|
| 376 | sobj = run_sobject_find(run, sym->outer_csi);
|
---|
| 377 | assert(sobj->vc == vc_object);
|
---|
| 378 | aobj = sobj->u.object_v;
|
---|
| 379 | } else {
|
---|
[051b3db8] | 380 | /*
|
---|
| 381 | * Instance object. Currently we don't support
|
---|
| 382 | * true inner classes, thus we know the symbol is
|
---|
| 383 | * in the active object (there is no dynamic parent).
|
---|
| 384 | */
|
---|
| 385 | sobj = proc_ar->obj;
|
---|
| 386 | aobj = sobj->u.object_v;;
|
---|
[c5cb943d] | 387 | }
|
---|
| 388 |
|
---|
[051b3db8] | 389 | if (sym->sc == sc_var) {
|
---|
| 390 | /* Find member variable in object. */
|
---|
| 391 | member_var = intmap_get(&aobj->fields,
|
---|
| 392 | nameref->name->sid);
|
---|
| 393 | assert(member_var != NULL);
|
---|
[fa36f29] | 394 |
|
---|
[051b3db8] | 395 | /* Return address of the variable. */
|
---|
| 396 | item = rdata_item_new(ic_address);
|
---|
| 397 | address = rdata_address_new(ac_var);
|
---|
| 398 | addr_var = rdata_addr_var_new();
|
---|
[fa36f29] | 399 |
|
---|
[051b3db8] | 400 | item->u.address = address;
|
---|
| 401 | address->u.var_a = addr_var;
|
---|
| 402 | addr_var->vref = member_var;
|
---|
[fa36f29] | 403 |
|
---|
[051b3db8] | 404 | *res = item;
|
---|
| 405 | } else {
|
---|
| 406 | /* Construct named property address. */
|
---|
| 407 | item = rdata_item_new(ic_address);
|
---|
| 408 | address = rdata_address_new(ac_prop);
|
---|
| 409 | addr_prop = rdata_addr_prop_new(apc_named);
|
---|
| 410 | aprop_named = rdata_aprop_named_new();
|
---|
| 411 | item->u.address = address;
|
---|
| 412 | address->u.prop_a = addr_prop;
|
---|
| 413 | addr_prop->u.named = aprop_named;
|
---|
| 414 |
|
---|
| 415 | deleg_p = rdata_deleg_new();
|
---|
| 416 | deleg_p->obj = sobj;
|
---|
| 417 | deleg_p->sym = sym;
|
---|
| 418 | addr_prop->u.named->prop_d = deleg_p;
|
---|
| 419 |
|
---|
| 420 | *res = item;
|
---|
| 421 | }
|
---|
[09ababb7] | 422 | break;
|
---|
| 423 | }
|
---|
| 424 | }
|
---|
| 425 |
|
---|
[38aaacc2] | 426 | /** Evaluate literal.
|
---|
| 427 | *
|
---|
| 428 | * @param run Runner object
|
---|
| 429 | * @param literal Literal
|
---|
| 430 | * @param res Place to store result
|
---|
| 431 | */
|
---|
[09ababb7] | 432 | static void run_literal(run_t *run, stree_literal_t *literal,
|
---|
| 433 | rdata_item_t **res)
|
---|
| 434 | {
|
---|
| 435 | #ifdef DEBUG_RUN_TRACE
|
---|
| 436 | printf("Run literal.\n");
|
---|
| 437 | #endif
|
---|
| 438 | switch (literal->ltc) {
|
---|
[074444f] | 439 | case ltc_bool:
|
---|
| 440 | run_lit_bool(run, &literal->u.lit_bool, res);
|
---|
| 441 | break;
|
---|
| 442 | case ltc_char:
|
---|
| 443 | run_lit_char(run, &literal->u.lit_char, res);
|
---|
| 444 | break;
|
---|
[09ababb7] | 445 | case ltc_int:
|
---|
| 446 | run_lit_int(run, &literal->u.lit_int, res);
|
---|
| 447 | break;
|
---|
[fa36f29] | 448 | case ltc_ref:
|
---|
| 449 | run_lit_ref(run, &literal->u.lit_ref, res);
|
---|
| 450 | break;
|
---|
[09ababb7] | 451 | case ltc_string:
|
---|
| 452 | run_lit_string(run, &literal->u.lit_string, res);
|
---|
| 453 | break;
|
---|
| 454 | }
|
---|
| 455 | }
|
---|
| 456 |
|
---|
[38aaacc2] | 457 | /** Evaluate Boolean literal.
|
---|
| 458 | *
|
---|
| 459 | * @param run Runner object
|
---|
| 460 | * @param lit_bool Boolean literal
|
---|
| 461 | * @param res Place to store result
|
---|
| 462 | */
|
---|
[074444f] | 463 | static void run_lit_bool(run_t *run, stree_lit_bool_t *lit_bool,
|
---|
| 464 | rdata_item_t **res)
|
---|
| 465 | {
|
---|
| 466 | rdata_item_t *item;
|
---|
| 467 | rdata_value_t *value;
|
---|
| 468 | rdata_var_t *var;
|
---|
| 469 | rdata_bool_t *bool_v;
|
---|
| 470 |
|
---|
| 471 | #ifdef DEBUG_RUN_TRACE
|
---|
| 472 | printf("Run Boolean literal.\n");
|
---|
| 473 | #endif
|
---|
| 474 | (void) run;
|
---|
| 475 |
|
---|
| 476 | item = rdata_item_new(ic_value);
|
---|
| 477 | value = rdata_value_new();
|
---|
| 478 | var = rdata_var_new(vc_bool);
|
---|
| 479 | bool_v = rdata_bool_new();
|
---|
| 480 |
|
---|
| 481 | item->u.value = value;
|
---|
| 482 | value->var = var;
|
---|
| 483 | var->u.bool_v = bool_v;
|
---|
| 484 | bool_v->value = lit_bool->value;
|
---|
| 485 |
|
---|
| 486 | *res = item;
|
---|
| 487 | }
|
---|
| 488 |
|
---|
| 489 | /** Evaluate character literal. */
|
---|
| 490 | static void run_lit_char(run_t *run, stree_lit_char_t *lit_char,
|
---|
| 491 | rdata_item_t **res)
|
---|
| 492 | {
|
---|
| 493 | rdata_item_t *item;
|
---|
| 494 | rdata_value_t *value;
|
---|
| 495 | rdata_var_t *var;
|
---|
| 496 | rdata_char_t *char_v;
|
---|
| 497 |
|
---|
| 498 | #ifdef DEBUG_RUN_TRACE
|
---|
| 499 | printf("Run character literal.\n");
|
---|
| 500 | #endif
|
---|
| 501 | (void) run;
|
---|
| 502 |
|
---|
| 503 | item = rdata_item_new(ic_value);
|
---|
| 504 | value = rdata_value_new();
|
---|
| 505 | var = rdata_var_new(vc_char);
|
---|
| 506 | char_v = rdata_char_new();
|
---|
| 507 |
|
---|
| 508 | item->u.value = value;
|
---|
| 509 | value->var = var;
|
---|
| 510 | var->u.char_v = char_v;
|
---|
| 511 | bigint_clone(&lit_char->value, &char_v->value);
|
---|
| 512 |
|
---|
| 513 | *res = item;
|
---|
| 514 | }
|
---|
| 515 |
|
---|
[38aaacc2] | 516 | /** Evaluate integer literal.
|
---|
| 517 | *
|
---|
| 518 | * @param run Runner object
|
---|
| 519 | * @param lit_int Integer literal
|
---|
| 520 | * @param res Place to store result
|
---|
| 521 | */
|
---|
[09ababb7] | 522 | static void run_lit_int(run_t *run, stree_lit_int_t *lit_int,
|
---|
| 523 | rdata_item_t **res)
|
---|
| 524 | {
|
---|
| 525 | rdata_item_t *item;
|
---|
| 526 | rdata_value_t *value;
|
---|
| 527 | rdata_var_t *var;
|
---|
| 528 | rdata_int_t *int_v;
|
---|
| 529 |
|
---|
| 530 | #ifdef DEBUG_RUN_TRACE
|
---|
| 531 | printf("Run integer literal.\n");
|
---|
| 532 | #endif
|
---|
[fa36f29] | 533 | (void) run;
|
---|
[09ababb7] | 534 |
|
---|
| 535 | item = rdata_item_new(ic_value);
|
---|
| 536 | value = rdata_value_new();
|
---|
| 537 | var = rdata_var_new(vc_int);
|
---|
| 538 | int_v = rdata_int_new();
|
---|
| 539 |
|
---|
| 540 | item->u.value = value;
|
---|
| 541 | value->var = var;
|
---|
| 542 | var->u.int_v = int_v;
|
---|
[23de644] | 543 | bigint_clone(&lit_int->value, &int_v->value);
|
---|
[09ababb7] | 544 |
|
---|
| 545 | *res = item;
|
---|
| 546 | }
|
---|
| 547 |
|
---|
[38aaacc2] | 548 | /** Evaluate reference literal (@c nil).
|
---|
| 549 | *
|
---|
| 550 | * @param run Runner object
|
---|
| 551 | * @param lit_ref Reference literal
|
---|
| 552 | * @param res Place to store result
|
---|
| 553 | */
|
---|
[fa36f29] | 554 | static void run_lit_ref(run_t *run, stree_lit_ref_t *lit_ref,
|
---|
| 555 | rdata_item_t **res)
|
---|
| 556 | {
|
---|
| 557 | rdata_item_t *item;
|
---|
| 558 | rdata_value_t *value;
|
---|
| 559 | rdata_var_t *var;
|
---|
| 560 | rdata_ref_t *ref_v;
|
---|
| 561 |
|
---|
| 562 | #ifdef DEBUG_RUN_TRACE
|
---|
| 563 | printf("Run reference literal (nil).\n");
|
---|
| 564 | #endif
|
---|
| 565 | (void) run;
|
---|
| 566 | (void) lit_ref;
|
---|
| 567 |
|
---|
| 568 | item = rdata_item_new(ic_value);
|
---|
| 569 | value = rdata_value_new();
|
---|
| 570 | var = rdata_var_new(vc_ref);
|
---|
| 571 | ref_v = rdata_ref_new();
|
---|
| 572 |
|
---|
| 573 | item->u.value = value;
|
---|
| 574 | value->var = var;
|
---|
| 575 | var->u.ref_v = ref_v;
|
---|
| 576 | ref_v->vref = NULL;
|
---|
| 577 |
|
---|
| 578 | *res = item;
|
---|
| 579 | }
|
---|
| 580 |
|
---|
[38aaacc2] | 581 | /** Evaluate string literal.
|
---|
| 582 | *
|
---|
| 583 | * @param run Runner object
|
---|
| 584 | * @param lit_string String literal
|
---|
| 585 | * @param res Place to store result
|
---|
| 586 | */
|
---|
[09ababb7] | 587 | static void run_lit_string(run_t *run, stree_lit_string_t *lit_string,
|
---|
| 588 | rdata_item_t **res)
|
---|
| 589 | {
|
---|
| 590 | rdata_item_t *item;
|
---|
| 591 | rdata_value_t *value;
|
---|
| 592 | rdata_var_t *var;
|
---|
| 593 | rdata_string_t *string_v;
|
---|
| 594 |
|
---|
| 595 | #ifdef DEBUG_RUN_TRACE
|
---|
| 596 | printf("Run integer literal.\n");
|
---|
| 597 | #endif
|
---|
[fa36f29] | 598 | (void) run;
|
---|
| 599 |
|
---|
[09ababb7] | 600 | item = rdata_item_new(ic_value);
|
---|
| 601 | value = rdata_value_new();
|
---|
| 602 | var = rdata_var_new(vc_string);
|
---|
| 603 | string_v = rdata_string_new();
|
---|
| 604 |
|
---|
| 605 | item->u.value = value;
|
---|
| 606 | value->var = var;
|
---|
| 607 | var->u.string_v = string_v;
|
---|
| 608 | string_v->value = lit_string->value;
|
---|
| 609 |
|
---|
| 610 | *res = item;
|
---|
| 611 | }
|
---|
| 612 |
|
---|
[38aaacc2] | 613 | /** Evaluate @c self reference.
|
---|
| 614 | *
|
---|
| 615 | * @param run Runner object
|
---|
| 616 | * @param self_ref Self reference
|
---|
| 617 | * @param res Place to store result
|
---|
| 618 | */
|
---|
[fa36f29] | 619 | static void run_self_ref(run_t *run, stree_self_ref_t *self_ref,
|
---|
| 620 | rdata_item_t **res)
|
---|
| 621 | {
|
---|
[d0febca] | 622 | run_proc_ar_t *proc_ar;
|
---|
[fa36f29] | 623 |
|
---|
| 624 | #ifdef DEBUG_RUN_TRACE
|
---|
| 625 | printf("Run self reference.\n");
|
---|
| 626 | #endif
|
---|
| 627 | (void) self_ref;
|
---|
[d0febca] | 628 | proc_ar = run_get_current_proc_ar(run);
|
---|
[fa36f29] | 629 |
|
---|
| 630 | /* Return reference to the currently active object. */
|
---|
[d0febca] | 631 | run_reference(run, proc_ar->obj, res);
|
---|
[fa36f29] | 632 | }
|
---|
[09ababb7] | 633 |
|
---|
[38aaacc2] | 634 | /** Evaluate binary operation.
|
---|
| 635 | *
|
---|
| 636 | * @param run Runner object
|
---|
| 637 | * @param binop Binary operation
|
---|
| 638 | * @param res Place to store result
|
---|
| 639 | */
|
---|
[09ababb7] | 640 | static void run_binop(run_t *run, stree_binop_t *binop, rdata_item_t **res)
|
---|
| 641 | {
|
---|
| 642 | rdata_item_t *rarg1_i, *rarg2_i;
|
---|
| 643 | rdata_item_t *rarg1_vi, *rarg2_vi;
|
---|
| 644 | rdata_value_t *v1, *v2;
|
---|
| 645 |
|
---|
[051b3db8] | 646 | rarg1_i = NULL;
|
---|
| 647 | rarg2_i = NULL;
|
---|
| 648 | rarg1_vi = NULL;
|
---|
| 649 | rarg2_vi = NULL;
|
---|
| 650 |
|
---|
[09ababb7] | 651 | #ifdef DEBUG_RUN_TRACE
|
---|
| 652 | printf("Run binary operation.\n");
|
---|
| 653 | #endif
|
---|
| 654 | run_expr(run, binop->arg1, &rarg1_i);
|
---|
[23de644] | 655 | if (run_is_bo(run)) {
|
---|
[051b3db8] | 656 | *res = run_recovery_item(run);
|
---|
| 657 | goto cleanup;
|
---|
| 658 | }
|
---|
| 659 |
|
---|
| 660 | #ifdef DEBUG_RUN_TRACE
|
---|
| 661 | printf("Check binop argument result.\n");
|
---|
| 662 | #endif
|
---|
| 663 | run_cvt_value_item(run, rarg1_i, &rarg1_vi);
|
---|
| 664 | if (run_is_bo(run)) {
|
---|
| 665 | *res = run_recovery_item(run);
|
---|
| 666 | goto cleanup;
|
---|
[23de644] | 667 | }
|
---|
| 668 |
|
---|
[09ababb7] | 669 | run_expr(run, binop->arg2, &rarg2_i);
|
---|
[23de644] | 670 | if (run_is_bo(run)) {
|
---|
[051b3db8] | 671 | *res = run_recovery_item(run);
|
---|
| 672 | goto cleanup;
|
---|
[23de644] | 673 | }
|
---|
[09ababb7] | 674 |
|
---|
| 675 | #ifdef DEBUG_RUN_TRACE
|
---|
[051b3db8] | 676 | printf("Check binop argument result.\n");
|
---|
[09ababb7] | 677 | #endif
|
---|
[d0febca] | 678 | run_cvt_value_item(run, rarg2_i, &rarg2_vi);
|
---|
[051b3db8] | 679 | if (run_is_bo(run)) {
|
---|
| 680 | *res = run_recovery_item(run);
|
---|
| 681 | goto cleanup;
|
---|
| 682 | }
|
---|
[09ababb7] | 683 |
|
---|
| 684 | v1 = rarg1_vi->u.value;
|
---|
| 685 | v2 = rarg2_vi->u.value;
|
---|
| 686 |
|
---|
[fa36f29] | 687 | if (v1->var->vc != v2->var->vc) {
|
---|
| 688 | printf("Unimplemented: Binary operation arguments have "
|
---|
| 689 | "different type.\n");
|
---|
[09ababb7] | 690 | exit(1);
|
---|
| 691 | }
|
---|
| 692 |
|
---|
[fa36f29] | 693 | switch (v1->var->vc) {
|
---|
[074444f] | 694 | case vc_bool:
|
---|
| 695 | run_binop_bool(run, binop, v1, v2, res);
|
---|
| 696 | break;
|
---|
| 697 | case vc_char:
|
---|
| 698 | run_binop_char(run, binop, v1, v2, res);
|
---|
| 699 | break;
|
---|
[fa36f29] | 700 | case vc_int:
|
---|
| 701 | run_binop_int(run, binop, v1, v2, res);
|
---|
| 702 | break;
|
---|
[94d484a] | 703 | case vc_string:
|
---|
| 704 | run_binop_string(run, binop, v1, v2, res);
|
---|
| 705 | break;
|
---|
[fa36f29] | 706 | case vc_ref:
|
---|
| 707 | run_binop_ref(run, binop, v1, v2, res);
|
---|
| 708 | break;
|
---|
[051bc69a] | 709 | case vc_enum:
|
---|
| 710 | run_binop_enum(run, binop, v1, v2, res);
|
---|
| 711 | break;
|
---|
[074444f] | 712 | case vc_deleg:
|
---|
| 713 | case vc_array:
|
---|
| 714 | case vc_object:
|
---|
| 715 | case vc_resource:
|
---|
[051bc69a] | 716 | case vc_symbol:
|
---|
[074444f] | 717 | assert(b_false);
|
---|
| 718 | }
|
---|
[051b3db8] | 719 |
|
---|
| 720 | cleanup:
|
---|
| 721 | if (rarg1_i != NULL)
|
---|
| 722 | rdata_item_destroy(rarg1_i);
|
---|
| 723 | if (rarg2_i != NULL)
|
---|
| 724 | rdata_item_destroy(rarg2_i);
|
---|
| 725 | if (rarg1_vi != NULL)
|
---|
| 726 | rdata_item_destroy(rarg1_vi);
|
---|
| 727 | if (rarg2_vi != NULL)
|
---|
| 728 | rdata_item_destroy(rarg2_vi);
|
---|
[074444f] | 729 | }
|
---|
| 730 |
|
---|
[38aaacc2] | 731 | /** Evaluate binary operation on bool arguments.
|
---|
| 732 | *
|
---|
| 733 | * @param run Runner object
|
---|
| 734 | * @param binop Binary operation
|
---|
| 735 | * @param v1 Value of first argument
|
---|
| 736 | * @param v2 Value of second argument
|
---|
| 737 | * @param res Place to store result
|
---|
| 738 | */
|
---|
[074444f] | 739 | static void run_binop_bool(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
|
---|
| 740 | rdata_value_t *v2, rdata_item_t **res)
|
---|
| 741 | {
|
---|
| 742 | rdata_item_t *item;
|
---|
| 743 | rdata_value_t *value;
|
---|
| 744 | rdata_var_t *var;
|
---|
| 745 | rdata_bool_t *bool_v;
|
---|
| 746 |
|
---|
| 747 | bool_t b1, b2;
|
---|
| 748 |
|
---|
| 749 | (void) run;
|
---|
| 750 |
|
---|
| 751 | item = rdata_item_new(ic_value);
|
---|
| 752 | value = rdata_value_new();
|
---|
| 753 | var = rdata_var_new(vc_bool);
|
---|
| 754 | bool_v = rdata_bool_new();
|
---|
| 755 |
|
---|
| 756 | item->u.value = value;
|
---|
| 757 | value->var = var;
|
---|
| 758 | var->u.bool_v = bool_v;
|
---|
| 759 |
|
---|
| 760 | b1 = v1->var->u.bool_v->value;
|
---|
| 761 | b2 = v2->var->u.bool_v->value;
|
---|
| 762 |
|
---|
| 763 | switch (binop->bc) {
|
---|
| 764 | case bo_plus:
|
---|
| 765 | case bo_minus:
|
---|
| 766 | case bo_mult:
|
---|
| 767 | assert(b_false);
|
---|
[dc12262] | 768 | /* Fallthrough */
|
---|
[074444f] | 769 |
|
---|
| 770 | case bo_equal:
|
---|
| 771 | bool_v->value = (b1 == b2);
|
---|
| 772 | break;
|
---|
| 773 | case bo_notequal:
|
---|
| 774 | bool_v->value = (b1 != b2);
|
---|
| 775 | break;
|
---|
| 776 | case bo_lt:
|
---|
| 777 | bool_v->value = (b1 == b_false) && (b2 == b_true);
|
---|
| 778 | break;
|
---|
| 779 | case bo_gt:
|
---|
| 780 | bool_v->value = (b1 == b_true) && (b2 == b_false);
|
---|
| 781 | break;
|
---|
| 782 | case bo_lt_equal:
|
---|
| 783 | bool_v->value = (b1 == b_false) || (b2 == b_true);
|
---|
| 784 | break;
|
---|
| 785 | case bo_gt_equal:
|
---|
| 786 | bool_v->value = (b1 == b_true) || (b2 == b_false);
|
---|
| 787 | break;
|
---|
[051bc69a] | 788 |
|
---|
| 789 | case bo_and:
|
---|
| 790 | bool_v->value = (b1 == b_true) && (b2 == b_true);
|
---|
| 791 | break;
|
---|
| 792 | case bo_or:
|
---|
| 793 | bool_v->value = (b1 == b_true) || (b2 == b_true);
|
---|
| 794 | break;
|
---|
[074444f] | 795 | }
|
---|
| 796 |
|
---|
| 797 | *res = item;
|
---|
| 798 | }
|
---|
| 799 |
|
---|
[38aaacc2] | 800 | /** Evaluate binary operation on char arguments.
|
---|
| 801 | *
|
---|
| 802 | * @param run Runner object
|
---|
| 803 | * @param binop Binary operation
|
---|
| 804 | * @param v1 Value of first argument
|
---|
| 805 | * @param v2 Value of second argument
|
---|
| 806 | * @param res Place to store result
|
---|
| 807 | */
|
---|
[074444f] | 808 | static void run_binop_char(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
|
---|
| 809 | rdata_value_t *v2, rdata_item_t **res)
|
---|
| 810 | {
|
---|
| 811 | rdata_item_t *item;
|
---|
| 812 | rdata_value_t *value;
|
---|
| 813 | rdata_var_t *var;
|
---|
| 814 | rdata_bool_t *bool_v;
|
---|
| 815 |
|
---|
| 816 | bigint_t *c1, *c2;
|
---|
| 817 | bigint_t diff;
|
---|
| 818 | bool_t zf, nf;
|
---|
| 819 |
|
---|
| 820 | (void) run;
|
---|
| 821 |
|
---|
| 822 | item = rdata_item_new(ic_value);
|
---|
| 823 | value = rdata_value_new();
|
---|
| 824 |
|
---|
| 825 | item->u.value = value;
|
---|
| 826 |
|
---|
| 827 | c1 = &v1->var->u.char_v->value;
|
---|
| 828 | c2 = &v2->var->u.char_v->value;
|
---|
| 829 |
|
---|
| 830 | var = rdata_var_new(vc_bool);
|
---|
| 831 | bool_v = rdata_bool_new();
|
---|
| 832 | var->u.bool_v = bool_v;
|
---|
| 833 | value->var = var;
|
---|
| 834 |
|
---|
| 835 | bigint_sub(c1, c2, &diff);
|
---|
| 836 | zf = bigint_is_zero(&diff);
|
---|
| 837 | nf = bigint_is_negative(&diff);
|
---|
| 838 |
|
---|
| 839 | switch (binop->bc) {
|
---|
| 840 | case bo_plus:
|
---|
| 841 | case bo_minus:
|
---|
| 842 | case bo_mult:
|
---|
| 843 | assert(b_false);
|
---|
[dc12262] | 844 | /* Fallthrough */
|
---|
[074444f] | 845 |
|
---|
| 846 | case bo_equal:
|
---|
| 847 | bool_v->value = zf;
|
---|
| 848 | break;
|
---|
| 849 | case bo_notequal:
|
---|
| 850 | bool_v->value = !zf;
|
---|
| 851 | break;
|
---|
| 852 | case bo_lt:
|
---|
| 853 | bool_v->value = (!zf && nf);
|
---|
| 854 | break;
|
---|
| 855 | case bo_gt:
|
---|
| 856 | bool_v->value = (!zf && !nf);
|
---|
| 857 | break;
|
---|
| 858 | case bo_lt_equal:
|
---|
| 859 | bool_v->value = (zf || nf);
|
---|
| 860 | break;
|
---|
| 861 | case bo_gt_equal:
|
---|
| 862 | bool_v->value = !nf;
|
---|
| 863 | break;
|
---|
[051bc69a] | 864 |
|
---|
| 865 | case bo_and:
|
---|
| 866 | case bo_or:
|
---|
[074444f] | 867 | assert(b_false);
|
---|
[fa36f29] | 868 | }
|
---|
[074444f] | 869 |
|
---|
| 870 | *res = item;
|
---|
[fa36f29] | 871 | }
|
---|
| 872 |
|
---|
[38aaacc2] | 873 | /** Evaluate binary operation on int arguments.
|
---|
| 874 | *
|
---|
| 875 | * @param run Runner object
|
---|
| 876 | * @param binop Binary operation
|
---|
| 877 | * @param v1 Value of first argument
|
---|
| 878 | * @param v2 Value of second argument
|
---|
| 879 | * @param res Place to store result
|
---|
| 880 | */
|
---|
[fa36f29] | 881 | static void run_binop_int(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
|
---|
| 882 | rdata_value_t *v2, rdata_item_t **res)
|
---|
| 883 | {
|
---|
| 884 | rdata_item_t *item;
|
---|
| 885 | rdata_value_t *value;
|
---|
| 886 | rdata_var_t *var;
|
---|
| 887 | rdata_int_t *int_v;
|
---|
[074444f] | 888 | rdata_bool_t *bool_v;
|
---|
[fa36f29] | 889 |
|
---|
[23de644] | 890 | bigint_t *i1, *i2;
|
---|
| 891 | bigint_t diff;
|
---|
| 892 | bool_t done;
|
---|
| 893 | bool_t zf, nf;
|
---|
[fa36f29] | 894 |
|
---|
| 895 | (void) run;
|
---|
| 896 |
|
---|
[09ababb7] | 897 | item = rdata_item_new(ic_value);
|
---|
| 898 | value = rdata_value_new();
|
---|
| 899 |
|
---|
| 900 | item->u.value = value;
|
---|
| 901 |
|
---|
[23de644] | 902 | i1 = &v1->var->u.int_v->value;
|
---|
| 903 | i2 = &v2->var->u.int_v->value;
|
---|
| 904 |
|
---|
| 905 | done = b_true;
|
---|
[09ababb7] | 906 |
|
---|
| 907 | switch (binop->bc) {
|
---|
| 908 | case bo_plus:
|
---|
[074444f] | 909 | int_v = rdata_int_new();
|
---|
[23de644] | 910 | bigint_add(i1, i2, &int_v->value);
|
---|
| 911 | break;
|
---|
| 912 | case bo_minus:
|
---|
[074444f] | 913 | int_v = rdata_int_new();
|
---|
[23de644] | 914 | bigint_sub(i1, i2, &int_v->value);
|
---|
| 915 | break;
|
---|
| 916 | case bo_mult:
|
---|
[074444f] | 917 | int_v = rdata_int_new();
|
---|
[23de644] | 918 | bigint_mul(i1, i2, &int_v->value);
|
---|
[09ababb7] | 919 | break;
|
---|
[23de644] | 920 | default:
|
---|
| 921 | done = b_false;
|
---|
| 922 | break;
|
---|
| 923 | }
|
---|
| 924 |
|
---|
| 925 | if (done) {
|
---|
[074444f] | 926 | var = rdata_var_new(vc_int);
|
---|
| 927 | var->u.int_v = int_v;
|
---|
| 928 | value->var = var;
|
---|
[23de644] | 929 | *res = item;
|
---|
| 930 | return;
|
---|
| 931 | }
|
---|
| 932 |
|
---|
[074444f] | 933 | var = rdata_var_new(vc_bool);
|
---|
| 934 | bool_v = rdata_bool_new();
|
---|
| 935 | var->u.bool_v = bool_v;
|
---|
| 936 | value->var = var;
|
---|
| 937 |
|
---|
[23de644] | 938 | /* Relational operation. */
|
---|
| 939 |
|
---|
| 940 | bigint_sub(i1, i2, &diff);
|
---|
| 941 | zf = bigint_is_zero(&diff);
|
---|
| 942 | nf = bigint_is_negative(&diff);
|
---|
[09ababb7] | 943 |
|
---|
[23de644] | 944 | switch (binop->bc) {
|
---|
[051bc69a] | 945 | case bo_plus:
|
---|
| 946 | case bo_minus:
|
---|
| 947 | case bo_mult:
|
---|
| 948 | assert(b_false);
|
---|
[dc12262] | 949 | /* Fallthrough */
|
---|
[051bc69a] | 950 |
|
---|
[09ababb7] | 951 | case bo_equal:
|
---|
[074444f] | 952 | bool_v->value = zf;
|
---|
[09ababb7] | 953 | break;
|
---|
| 954 | case bo_notequal:
|
---|
[074444f] | 955 | bool_v->value = !zf;
|
---|
[09ababb7] | 956 | break;
|
---|
| 957 | case bo_lt:
|
---|
[074444f] | 958 | bool_v->value = (!zf && nf);
|
---|
[09ababb7] | 959 | break;
|
---|
| 960 | case bo_gt:
|
---|
[074444f] | 961 | bool_v->value = (!zf && !nf);
|
---|
[09ababb7] | 962 | break;
|
---|
| 963 | case bo_lt_equal:
|
---|
[074444f] | 964 | bool_v->value = (zf || nf);
|
---|
[09ababb7] | 965 | break;
|
---|
| 966 | case bo_gt_equal:
|
---|
[074444f] | 967 | bool_v->value = !nf;
|
---|
[09ababb7] | 968 | break;
|
---|
[051bc69a] | 969 | case bo_and:
|
---|
| 970 | case bo_or:
|
---|
[09ababb7] | 971 | assert(b_false);
|
---|
| 972 | }
|
---|
| 973 |
|
---|
| 974 | *res = item;
|
---|
| 975 | }
|
---|
| 976 |
|
---|
[38aaacc2] | 977 | /** Evaluate binary operation on string arguments.
|
---|
| 978 | *
|
---|
| 979 | * @param run Runner object
|
---|
| 980 | * @param binop Binary operation
|
---|
| 981 | * @param v1 Value of first argument
|
---|
| 982 | * @param v2 Value of second argument
|
---|
| 983 | * @param res Place to store result
|
---|
| 984 | */
|
---|
[94d484a] | 985 | static void run_binop_string(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
|
---|
| 986 | rdata_value_t *v2, rdata_item_t **res)
|
---|
| 987 | {
|
---|
| 988 | rdata_item_t *item;
|
---|
| 989 | rdata_value_t *value;
|
---|
| 990 | rdata_var_t *var;
|
---|
| 991 | rdata_string_t *string_v;
|
---|
[051bc69a] | 992 | rdata_bool_t *bool_v;
|
---|
| 993 | bool_t done;
|
---|
| 994 | bool_t zf;
|
---|
[94d484a] | 995 |
|
---|
[38aaacc2] | 996 | const char *s1, *s2;
|
---|
[94d484a] | 997 |
|
---|
| 998 | (void) run;
|
---|
| 999 |
|
---|
| 1000 | item = rdata_item_new(ic_value);
|
---|
| 1001 | value = rdata_value_new();
|
---|
| 1002 |
|
---|
| 1003 | item->u.value = value;
|
---|
| 1004 |
|
---|
| 1005 | s1 = v1->var->u.string_v->value;
|
---|
| 1006 | s2 = v2->var->u.string_v->value;
|
---|
| 1007 |
|
---|
[051bc69a] | 1008 | done = b_true;
|
---|
| 1009 |
|
---|
[94d484a] | 1010 | switch (binop->bc) {
|
---|
| 1011 | case bo_plus:
|
---|
| 1012 | /* Concatenate strings. */
|
---|
[051bc69a] | 1013 | string_v = rdata_string_new();
|
---|
[94d484a] | 1014 | string_v->value = os_str_acat(s1, s2);
|
---|
| 1015 | break;
|
---|
[051bc69a] | 1016 | default:
|
---|
| 1017 | done = b_false;
|
---|
| 1018 | break;
|
---|
| 1019 | }
|
---|
| 1020 |
|
---|
| 1021 | if (done) {
|
---|
| 1022 | var = rdata_var_new(vc_string);
|
---|
| 1023 | var->u.string_v = string_v;
|
---|
| 1024 | value->var = var;
|
---|
| 1025 | *res = item;
|
---|
| 1026 | return;
|
---|
| 1027 | }
|
---|
| 1028 |
|
---|
| 1029 | var = rdata_var_new(vc_bool);
|
---|
| 1030 | bool_v = rdata_bool_new();
|
---|
| 1031 | var->u.bool_v = bool_v;
|
---|
| 1032 | value->var = var;
|
---|
| 1033 |
|
---|
| 1034 | /* Relational operation. */
|
---|
| 1035 |
|
---|
| 1036 | zf = os_str_cmp(s1, s2) == 0;
|
---|
| 1037 |
|
---|
| 1038 | switch (binop->bc) {
|
---|
| 1039 | case bo_equal:
|
---|
| 1040 | bool_v->value = zf;
|
---|
| 1041 | break;
|
---|
| 1042 | case bo_notequal:
|
---|
| 1043 | bool_v->value = !zf;
|
---|
| 1044 | break;
|
---|
[94d484a] | 1045 | default:
|
---|
| 1046 | printf("Error: Invalid binary operation on string "
|
---|
| 1047 | "arguments (%d).\n", binop->bc);
|
---|
| 1048 | assert(b_false);
|
---|
| 1049 | }
|
---|
| 1050 |
|
---|
| 1051 | *res = item;
|
---|
| 1052 | }
|
---|
| 1053 |
|
---|
[38aaacc2] | 1054 | /** Evaluate binary operation on ref arguments.
|
---|
| 1055 | *
|
---|
| 1056 | * @param run Runner object
|
---|
| 1057 | * @param binop Binary operation
|
---|
| 1058 | * @param v1 Value of first argument
|
---|
| 1059 | * @param v2 Value of second argument
|
---|
| 1060 | * @param res Place to store result
|
---|
| 1061 | */
|
---|
[fa36f29] | 1062 | static void run_binop_ref(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
|
---|
| 1063 | rdata_value_t *v2, rdata_item_t **res)
|
---|
| 1064 | {
|
---|
| 1065 | rdata_item_t *item;
|
---|
| 1066 | rdata_value_t *value;
|
---|
| 1067 | rdata_var_t *var;
|
---|
[074444f] | 1068 | rdata_bool_t *bool_v;
|
---|
[fa36f29] | 1069 |
|
---|
| 1070 | rdata_var_t *ref1, *ref2;
|
---|
| 1071 |
|
---|
| 1072 | (void) run;
|
---|
| 1073 |
|
---|
| 1074 | item = rdata_item_new(ic_value);
|
---|
| 1075 | value = rdata_value_new();
|
---|
[074444f] | 1076 | var = rdata_var_new(vc_bool);
|
---|
| 1077 | bool_v = rdata_bool_new();
|
---|
[fa36f29] | 1078 |
|
---|
| 1079 | item->u.value = value;
|
---|
| 1080 | value->var = var;
|
---|
[074444f] | 1081 | var->u.bool_v = bool_v;
|
---|
[fa36f29] | 1082 |
|
---|
| 1083 | ref1 = v1->var->u.ref_v->vref;
|
---|
| 1084 | ref2 = v2->var->u.ref_v->vref;
|
---|
| 1085 |
|
---|
| 1086 | switch (binop->bc) {
|
---|
| 1087 | case bo_equal:
|
---|
[074444f] | 1088 | bool_v->value = (ref1 == ref2);
|
---|
[fa36f29] | 1089 | break;
|
---|
| 1090 | case bo_notequal:
|
---|
[074444f] | 1091 | bool_v->value = (ref1 != ref2);
|
---|
[fa36f29] | 1092 | break;
|
---|
| 1093 | default:
|
---|
| 1094 | printf("Error: Invalid binary operation on reference "
|
---|
| 1095 | "arguments (%d).\n", binop->bc);
|
---|
| 1096 | assert(b_false);
|
---|
| 1097 | }
|
---|
| 1098 |
|
---|
| 1099 | *res = item;
|
---|
| 1100 | }
|
---|
| 1101 |
|
---|
[051bc69a] | 1102 | /** Evaluate binary operation on enum arguments.
|
---|
| 1103 | *
|
---|
| 1104 | * @param run Runner object
|
---|
| 1105 | * @param binop Binary operation
|
---|
| 1106 | * @param v1 Value of first argument
|
---|
| 1107 | * @param v2 Value of second argument
|
---|
| 1108 | * @param res Place to store result
|
---|
| 1109 | */
|
---|
| 1110 | static void run_binop_enum(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
|
---|
| 1111 | rdata_value_t *v2, rdata_item_t **res)
|
---|
| 1112 | {
|
---|
| 1113 | rdata_item_t *item;
|
---|
| 1114 | rdata_value_t *value;
|
---|
| 1115 | rdata_var_t *var;
|
---|
| 1116 | rdata_bool_t *bool_v;
|
---|
| 1117 |
|
---|
[051b3db8] | 1118 | stree_embr_t *e1, *e2;
|
---|
[051bc69a] | 1119 |
|
---|
| 1120 | (void) run;
|
---|
| 1121 |
|
---|
| 1122 | item = rdata_item_new(ic_value);
|
---|
| 1123 | value = rdata_value_new();
|
---|
| 1124 | var = rdata_var_new(vc_bool);
|
---|
| 1125 | bool_v = rdata_bool_new();
|
---|
| 1126 |
|
---|
| 1127 | item->u.value = value;
|
---|
| 1128 | value->var = var;
|
---|
| 1129 | var->u.bool_v = bool_v;
|
---|
| 1130 |
|
---|
[051b3db8] | 1131 | e1 = v1->var->u.enum_v->value;
|
---|
| 1132 | e2 = v2->var->u.enum_v->value;
|
---|
[051bc69a] | 1133 |
|
---|
| 1134 | switch (binop->bc) {
|
---|
| 1135 | case bo_equal:
|
---|
[051b3db8] | 1136 | bool_v->value = (e1 == e2);
|
---|
[051bc69a] | 1137 | break;
|
---|
| 1138 | case bo_notequal:
|
---|
[051b3db8] | 1139 | bool_v->value = (e1 != e2);
|
---|
[051bc69a] | 1140 | break;
|
---|
| 1141 | default:
|
---|
| 1142 | /* Should have been caught by static typing. */
|
---|
| 1143 | assert(b_false);
|
---|
| 1144 | }
|
---|
| 1145 |
|
---|
| 1146 | *res = item;
|
---|
| 1147 | }
|
---|
[fa36f29] | 1148 |
|
---|
[38aaacc2] | 1149 | /** Evaluate unary operation.
|
---|
| 1150 | *
|
---|
| 1151 | * @param run Runner object
|
---|
| 1152 | * @param unop Unary operation
|
---|
| 1153 | * @param res Place to store result
|
---|
| 1154 | */
|
---|
[09ababb7] | 1155 | static void run_unop(run_t *run, stree_unop_t *unop, rdata_item_t **res)
|
---|
| 1156 | {
|
---|
[23de644] | 1157 | rdata_item_t *rarg_i;
|
---|
| 1158 | rdata_item_t *rarg_vi;
|
---|
| 1159 | rdata_value_t *val;
|
---|
[09ababb7] | 1160 |
|
---|
| 1161 | #ifdef DEBUG_RUN_TRACE
|
---|
| 1162 | printf("Run unary operation.\n");
|
---|
| 1163 | #endif
|
---|
[051b3db8] | 1164 | rarg_i = NULL;
|
---|
| 1165 | rarg_vi = NULL;
|
---|
| 1166 |
|
---|
[23de644] | 1167 | run_expr(run, unop->arg, &rarg_i);
|
---|
| 1168 | if (run_is_bo(run)) {
|
---|
[051b3db8] | 1169 | *res = run_recovery_item(run);
|
---|
| 1170 | goto cleanup;
|
---|
[23de644] | 1171 | }
|
---|
| 1172 |
|
---|
| 1173 | #ifdef DEBUG_RUN_TRACE
|
---|
| 1174 | printf("Check unop argument result.\n");
|
---|
| 1175 | #endif
|
---|
| 1176 | run_cvt_value_item(run, rarg_i, &rarg_vi);
|
---|
[051b3db8] | 1177 | if (run_is_bo(run)) {
|
---|
| 1178 | *res = run_recovery_item(run);
|
---|
| 1179 | goto cleanup;
|
---|
| 1180 | }
|
---|
[23de644] | 1181 |
|
---|
| 1182 | val = rarg_vi->u.value;
|
---|
| 1183 |
|
---|
| 1184 | switch (val->var->vc) {
|
---|
[051bc69a] | 1185 | case vc_bool:
|
---|
| 1186 | run_unop_bool(run, unop, val, res);
|
---|
| 1187 | break;
|
---|
[23de644] | 1188 | case vc_int:
|
---|
| 1189 | run_unop_int(run, unop, val, res);
|
---|
| 1190 | break;
|
---|
| 1191 | default:
|
---|
| 1192 | printf("Unimplemented: Unrary operation argument of "
|
---|
| 1193 | "type %d.\n", val->var->vc);
|
---|
| 1194 | run_raise_error(run);
|
---|
[051b3db8] | 1195 | *res = run_recovery_item(run);
|
---|
[23de644] | 1196 | break;
|
---|
| 1197 | }
|
---|
[051b3db8] | 1198 | cleanup:
|
---|
| 1199 | if (rarg_i != NULL)
|
---|
| 1200 | rdata_item_destroy(rarg_i);
|
---|
| 1201 | if (rarg_vi != NULL)
|
---|
| 1202 | rdata_item_destroy(rarg_vi);
|
---|
[09ababb7] | 1203 | }
|
---|
| 1204 |
|
---|
[051bc69a] | 1205 | /** Evaluate unary operation on bool argument.
|
---|
| 1206 | *
|
---|
| 1207 | * @param run Runner object
|
---|
| 1208 | * @param unop Unary operation
|
---|
| 1209 | * @param val Value of argument
|
---|
| 1210 | * @param res Place to store result
|
---|
| 1211 | */
|
---|
| 1212 | static void run_unop_bool(run_t *run, stree_unop_t *unop, rdata_value_t *val,
|
---|
| 1213 | rdata_item_t **res)
|
---|
| 1214 | {
|
---|
| 1215 | rdata_item_t *item;
|
---|
| 1216 | rdata_value_t *value;
|
---|
| 1217 | rdata_var_t *var;
|
---|
| 1218 | rdata_bool_t *bool_v;
|
---|
| 1219 |
|
---|
| 1220 | (void) run;
|
---|
| 1221 |
|
---|
| 1222 | item = rdata_item_new(ic_value);
|
---|
| 1223 | value = rdata_value_new();
|
---|
| 1224 | var = rdata_var_new(vc_bool);
|
---|
| 1225 | bool_v = rdata_bool_new();
|
---|
| 1226 |
|
---|
| 1227 | item->u.value = value;
|
---|
| 1228 | value->var = var;
|
---|
| 1229 | var->u.bool_v = bool_v;
|
---|
| 1230 |
|
---|
| 1231 | switch (unop->uc) {
|
---|
| 1232 | case uo_plus:
|
---|
| 1233 | case uo_minus:
|
---|
| 1234 | assert(b_false);
|
---|
[dc12262] | 1235 | /* Fallthrough */
|
---|
[051bc69a] | 1236 |
|
---|
| 1237 | case uo_not:
|
---|
| 1238 | bool_v->value = !val->var->u.bool_v->value;
|
---|
| 1239 | break;
|
---|
| 1240 | }
|
---|
| 1241 |
|
---|
| 1242 | *res = item;
|
---|
| 1243 | }
|
---|
| 1244 |
|
---|
[38aaacc2] | 1245 | /** Evaluate unary operation on int argument.
|
---|
| 1246 | *
|
---|
| 1247 | * @param run Runner object
|
---|
| 1248 | * @param unop Unary operation
|
---|
| 1249 | * @param val Value of argument
|
---|
| 1250 | * @param res Place to store result
|
---|
| 1251 | */
|
---|
[23de644] | 1252 | static void run_unop_int(run_t *run, stree_unop_t *unop, rdata_value_t *val,
|
---|
| 1253 | rdata_item_t **res)
|
---|
| 1254 | {
|
---|
| 1255 | rdata_item_t *item;
|
---|
| 1256 | rdata_value_t *value;
|
---|
| 1257 | rdata_var_t *var;
|
---|
| 1258 | rdata_int_t *int_v;
|
---|
| 1259 |
|
---|
| 1260 | (void) run;
|
---|
| 1261 |
|
---|
| 1262 | item = rdata_item_new(ic_value);
|
---|
| 1263 | value = rdata_value_new();
|
---|
| 1264 | var = rdata_var_new(vc_int);
|
---|
| 1265 | int_v = rdata_int_new();
|
---|
| 1266 |
|
---|
| 1267 | item->u.value = value;
|
---|
| 1268 | value->var = var;
|
---|
| 1269 | var->u.int_v = int_v;
|
---|
| 1270 |
|
---|
| 1271 | switch (unop->uc) {
|
---|
| 1272 | case uo_plus:
|
---|
| 1273 | bigint_clone(&val->var->u.int_v->value, &int_v->value);
|
---|
| 1274 | break;
|
---|
| 1275 | case uo_minus:
|
---|
| 1276 | bigint_reverse_sign(&val->var->u.int_v->value,
|
---|
| 1277 | &int_v->value);
|
---|
| 1278 | break;
|
---|
[051bc69a] | 1279 | case uo_not:
|
---|
| 1280 | assert(b_false);
|
---|
[23de644] | 1281 | }
|
---|
| 1282 |
|
---|
| 1283 | *res = item;
|
---|
| 1284 | }
|
---|
| 1285 |
|
---|
[051b3db8] | 1286 | /** Run equality comparison of two values
|
---|
| 1287 | *
|
---|
| 1288 | * This should be equivalent to equality ('==') binary operation.
|
---|
| 1289 | * XXX Duplicating code of run_binop_xxx().
|
---|
| 1290 | *
|
---|
| 1291 | * @param run Runner object
|
---|
| 1292 | * @param v1 Value of first argument
|
---|
| 1293 | * @param v2 Value of second argument
|
---|
| 1294 | * @param res Place to store result (plain boolean value)
|
---|
| 1295 | */
|
---|
| 1296 | void run_equal(run_t *run, rdata_value_t *v1, rdata_value_t *v2, bool_t *res)
|
---|
| 1297 | {
|
---|
| 1298 | bool_t b1, b2;
|
---|
| 1299 | bigint_t *c1, *c2;
|
---|
| 1300 | bigint_t *i1, *i2;
|
---|
| 1301 | bigint_t diff;
|
---|
| 1302 | const char *s1, *s2;
|
---|
| 1303 | rdata_var_t *ref1, *ref2;
|
---|
| 1304 | stree_embr_t *e1, *e2;
|
---|
| 1305 |
|
---|
| 1306 | (void) run;
|
---|
| 1307 | assert(v1->var->vc == v2->var->vc);
|
---|
| 1308 |
|
---|
| 1309 | switch (v1->var->vc) {
|
---|
| 1310 | case vc_bool:
|
---|
| 1311 | b1 = v1->var->u.bool_v->value;
|
---|
| 1312 | b2 = v2->var->u.bool_v->value;
|
---|
| 1313 |
|
---|
| 1314 | *res = (b1 == b2);
|
---|
| 1315 | break;
|
---|
| 1316 | case vc_char:
|
---|
| 1317 | c1 = &v1->var->u.char_v->value;
|
---|
| 1318 | c2 = &v2->var->u.char_v->value;
|
---|
| 1319 |
|
---|
| 1320 | bigint_sub(c1, c2, &diff);
|
---|
| 1321 | *res = bigint_is_zero(&diff);
|
---|
| 1322 | break;
|
---|
| 1323 | case vc_int:
|
---|
| 1324 | i1 = &v1->var->u.int_v->value;
|
---|
| 1325 | i2 = &v2->var->u.int_v->value;
|
---|
| 1326 |
|
---|
| 1327 | bigint_sub(i1, i2, &diff);
|
---|
| 1328 | *res = bigint_is_zero(&diff);
|
---|
| 1329 | break;
|
---|
| 1330 | case vc_string:
|
---|
| 1331 | s1 = v1->var->u.string_v->value;
|
---|
| 1332 | s2 = v2->var->u.string_v->value;
|
---|
| 1333 |
|
---|
| 1334 | *res = os_str_cmp(s1, s2) == 0;
|
---|
| 1335 | break;
|
---|
| 1336 | case vc_ref:
|
---|
| 1337 | ref1 = v1->var->u.ref_v->vref;
|
---|
| 1338 | ref2 = v2->var->u.ref_v->vref;
|
---|
| 1339 |
|
---|
| 1340 | *res = (ref1 == ref2);
|
---|
| 1341 | break;
|
---|
| 1342 | case vc_enum:
|
---|
| 1343 | e1 = v1->var->u.enum_v->value;
|
---|
| 1344 | e2 = v2->var->u.enum_v->value;
|
---|
| 1345 |
|
---|
| 1346 | *res = (e1 == e2);
|
---|
| 1347 | break;
|
---|
| 1348 |
|
---|
| 1349 | case vc_deleg:
|
---|
| 1350 | case vc_array:
|
---|
| 1351 | case vc_object:
|
---|
| 1352 | case vc_resource:
|
---|
| 1353 | case vc_symbol:
|
---|
| 1354 | assert(b_false);
|
---|
| 1355 | }
|
---|
| 1356 | }
|
---|
| 1357 |
|
---|
| 1358 |
|
---|
[38aaacc2] | 1359 | /** Evaluate @c new operation.
|
---|
| 1360 | *
|
---|
| 1361 | * Evaluates operation per the @c new operator that creates a new
|
---|
| 1362 | * instance of some type.
|
---|
| 1363 | *
|
---|
| 1364 | * @param run Runner object
|
---|
| 1365 | * @param unop Unary operation
|
---|
| 1366 | * @param res Place to store result
|
---|
| 1367 | */
|
---|
[fa36f29] | 1368 | static void run_new(run_t *run, stree_new_t *new_op, rdata_item_t **res)
|
---|
[94d484a] | 1369 | {
|
---|
[39e8406] | 1370 | tdata_item_t *titem;
|
---|
[94d484a] | 1371 |
|
---|
| 1372 | #ifdef DEBUG_RUN_TRACE
|
---|
| 1373 | printf("Run 'new' operation.\n");
|
---|
| 1374 | #endif
|
---|
| 1375 | /* Evaluate type expression */
|
---|
[39e8406] | 1376 | run_texpr(run->program, run_get_current_csi(run), new_op->texpr,
|
---|
| 1377 | &titem);
|
---|
[94d484a] | 1378 |
|
---|
| 1379 | switch (titem->tic) {
|
---|
| 1380 | case tic_tarray:
|
---|
| 1381 | run_new_array(run, new_op, titem, res);
|
---|
| 1382 | break;
|
---|
[39e8406] | 1383 | case tic_tobject:
|
---|
[94d484a] | 1384 | run_new_object(run, new_op, titem, res);
|
---|
| 1385 | break;
|
---|
| 1386 | default:
|
---|
| 1387 | printf("Error: Invalid argument to operator 'new', "
|
---|
| 1388 | "expected object.\n");
|
---|
| 1389 | exit(1);
|
---|
| 1390 | }
|
---|
| 1391 | }
|
---|
| 1392 |
|
---|
[38aaacc2] | 1393 | /** Create new array.
|
---|
| 1394 | *
|
---|
| 1395 | * @param run Runner object
|
---|
| 1396 | * @param new_op New operation
|
---|
| 1397 | * @param titem Type of new var node (tic_tarray)
|
---|
| 1398 | * @param res Place to store result
|
---|
| 1399 | */
|
---|
[94d484a] | 1400 | static void run_new_array(run_t *run, stree_new_t *new_op,
|
---|
[39e8406] | 1401 | tdata_item_t *titem, rdata_item_t **res)
|
---|
[94d484a] | 1402 | {
|
---|
[39e8406] | 1403 | tdata_array_t *tarray;
|
---|
[94d484a] | 1404 | rdata_array_t *array;
|
---|
| 1405 | rdata_var_t *array_var;
|
---|
| 1406 | rdata_var_t *elem_var;
|
---|
| 1407 |
|
---|
| 1408 | rdata_item_t *rexpr, *rexpr_vi;
|
---|
| 1409 | rdata_var_t *rexpr_var;
|
---|
| 1410 |
|
---|
| 1411 | stree_expr_t *expr;
|
---|
| 1412 |
|
---|
| 1413 | list_node_t *node;
|
---|
| 1414 | int length;
|
---|
| 1415 | int i;
|
---|
[23de644] | 1416 | int rc;
|
---|
| 1417 | int iextent;
|
---|
[94d484a] | 1418 |
|
---|
| 1419 | #ifdef DEBUG_RUN_TRACE
|
---|
| 1420 | printf("Create new array.\n");
|
---|
| 1421 | #endif
|
---|
| 1422 | (void) run;
|
---|
| 1423 | (void) new_op;
|
---|
| 1424 |
|
---|
| 1425 | assert(titem->tic == tic_tarray);
|
---|
| 1426 | tarray = titem->u.tarray;
|
---|
| 1427 |
|
---|
| 1428 | /* Create the array. */
|
---|
| 1429 | assert(titem->u.tarray->rank > 0);
|
---|
| 1430 | array = rdata_array_new(titem->u.tarray->rank);
|
---|
| 1431 |
|
---|
| 1432 | /* Compute extents. */
|
---|
| 1433 | node = list_first(&tarray->extents);
|
---|
| 1434 | if (node == NULL) {
|
---|
| 1435 | printf("Error: Extents must be specified when constructing "
|
---|
| 1436 | "an array with 'new'.\n");
|
---|
| 1437 | exit(1);
|
---|
| 1438 | }
|
---|
| 1439 |
|
---|
| 1440 | i = 0; length = 1;
|
---|
| 1441 | while (node != NULL) {
|
---|
| 1442 | expr = list_node_data(node, stree_expr_t *);
|
---|
| 1443 |
|
---|
| 1444 | /* Evaluate extent argument. */
|
---|
| 1445 | run_expr(run, expr, &rexpr);
|
---|
[23de644] | 1446 | if (run_is_bo(run)) {
|
---|
[051b3db8] | 1447 | *res = run_recovery_item(run);
|
---|
[23de644] | 1448 | return;
|
---|
| 1449 | }
|
---|
| 1450 |
|
---|
[d0febca] | 1451 | run_cvt_value_item(run, rexpr, &rexpr_vi);
|
---|
[051b3db8] | 1452 | if (run_is_bo(run)) {
|
---|
| 1453 | *res = run_recovery_item(run);
|
---|
| 1454 | return;
|
---|
| 1455 | }
|
---|
| 1456 |
|
---|
[94d484a] | 1457 | assert(rexpr_vi->ic == ic_value);
|
---|
| 1458 | rexpr_var = rexpr_vi->u.value->var;
|
---|
| 1459 |
|
---|
| 1460 | if (rexpr_var->vc != vc_int) {
|
---|
| 1461 | printf("Error: Array extent must be an integer.\n");
|
---|
| 1462 | exit(1);
|
---|
| 1463 | }
|
---|
| 1464 |
|
---|
| 1465 | #ifdef DEBUG_RUN_TRACE
|
---|
[23de644] | 1466 | printf("Array extent: ");
|
---|
| 1467 | bigint_print(&rexpr_var->u.int_v->value);
|
---|
| 1468 | printf(".\n");
|
---|
[94d484a] | 1469 | #endif
|
---|
[23de644] | 1470 | rc = bigint_get_value_int(&rexpr_var->u.int_v->value,
|
---|
| 1471 | &iextent);
|
---|
| 1472 | if (rc != EOK) {
|
---|
| 1473 | printf("Memory allocation failed (big int used).\n");
|
---|
| 1474 | exit(1);
|
---|
| 1475 | }
|
---|
| 1476 |
|
---|
| 1477 | array->extent[i] = iextent;
|
---|
[94d484a] | 1478 | length = length * array->extent[i];
|
---|
| 1479 |
|
---|
| 1480 | node = list_next(&tarray->extents, node);
|
---|
| 1481 | i += 1;
|
---|
| 1482 | }
|
---|
| 1483 |
|
---|
| 1484 | array->element = calloc(length, sizeof(rdata_var_t *));
|
---|
| 1485 | if (array->element == NULL) {
|
---|
| 1486 | printf("Memory allocation failed.\n");
|
---|
| 1487 | exit(1);
|
---|
| 1488 | }
|
---|
| 1489 |
|
---|
| 1490 | /* Create member variables */
|
---|
| 1491 | for (i = 0; i < length; ++i) {
|
---|
[38aaacc2] | 1492 | /* Create and initialize element. */
|
---|
| 1493 | run_var_new(run, tarray->base_ti, &elem_var);
|
---|
[94d484a] | 1494 |
|
---|
| 1495 | array->element[i] = elem_var;
|
---|
| 1496 | }
|
---|
| 1497 |
|
---|
| 1498 | /* Create array variable. */
|
---|
| 1499 | array_var = rdata_var_new(vc_array);
|
---|
| 1500 | array_var->u.array_v = array;
|
---|
| 1501 |
|
---|
| 1502 | /* Create reference to the new array. */
|
---|
[d0febca] | 1503 | run_reference(run, array_var, res);
|
---|
[94d484a] | 1504 | }
|
---|
| 1505 |
|
---|
[38aaacc2] | 1506 | /** Create new object.
|
---|
| 1507 | *
|
---|
| 1508 | * @param run Runner object
|
---|
| 1509 | * @param new_op New operation
|
---|
| 1510 | * @param titem Type of new var node (tic_tobject)
|
---|
| 1511 | * @param res Place to store result
|
---|
| 1512 | */
|
---|
[94d484a] | 1513 | static void run_new_object(run_t *run, stree_new_t *new_op,
|
---|
[39e8406] | 1514 | tdata_item_t *titem, rdata_item_t **res)
|
---|
[fa36f29] | 1515 | {
|
---|
| 1516 | stree_csi_t *csi;
|
---|
[051bc69a] | 1517 | rdata_item_t *obj_i;
|
---|
| 1518 | list_t arg_vals;
|
---|
[fa36f29] | 1519 |
|
---|
| 1520 | #ifdef DEBUG_RUN_TRACE
|
---|
[94d484a] | 1521 | printf("Create new object.\n");
|
---|
[fa36f29] | 1522 | #endif
|
---|
| 1523 | /* Lookup object CSI. */
|
---|
[39e8406] | 1524 | assert(titem->tic == tic_tobject);
|
---|
| 1525 | csi = titem->u.tobject->csi;
|
---|
[fa36f29] | 1526 |
|
---|
[051bc69a] | 1527 | /* Evaluate constructor arguments. */
|
---|
| 1528 | run_call_args(run, &new_op->ctor_args, &arg_vals);
|
---|
| 1529 | if (run_is_bo(run)) {
|
---|
[051b3db8] | 1530 | *res = run_recovery_item(run);
|
---|
[051bc69a] | 1531 | return;
|
---|
| 1532 | }
|
---|
| 1533 |
|
---|
[23de644] | 1534 | /* Create CSI instance. */
|
---|
[c5cb943d] | 1535 | run_new_csi_inst_ref(run, csi, sn_nonstatic, res);
|
---|
[051bc69a] | 1536 |
|
---|
| 1537 | /* Run the constructor. */
|
---|
| 1538 | run_dereference(run, *res, NULL, &obj_i);
|
---|
| 1539 | assert(obj_i->ic == ic_address);
|
---|
| 1540 | assert(obj_i->u.address->ac == ac_var);
|
---|
| 1541 | run_object_ctor(run, obj_i->u.address->u.var_a->vref, &arg_vals);
|
---|
[051b3db8] | 1542 | rdata_item_destroy(obj_i);
|
---|
| 1543 |
|
---|
| 1544 | /* Destroy argument values */
|
---|
| 1545 | run_destroy_arg_vals(&arg_vals);
|
---|
[fa36f29] | 1546 | }
|
---|
| 1547 |
|
---|
[38aaacc2] | 1548 | /** Evaluate member acccess.
|
---|
| 1549 | *
|
---|
| 1550 | * Evaluate operation per the member access ('.') operator.
|
---|
| 1551 | *
|
---|
| 1552 | * @param run Runner object
|
---|
| 1553 | * @param access Access operation
|
---|
| 1554 | * @param res Place to store result
|
---|
| 1555 | */
|
---|
[09ababb7] | 1556 | static void run_access(run_t *run, stree_access_t *access, rdata_item_t **res)
|
---|
| 1557 | {
|
---|
| 1558 | rdata_item_t *rarg;
|
---|
| 1559 |
|
---|
| 1560 | #ifdef DEBUG_RUN_TRACE
|
---|
| 1561 | printf("Run access operation.\n");
|
---|
| 1562 | #endif
|
---|
[051b3db8] | 1563 | rarg = NULL;
|
---|
| 1564 |
|
---|
[09ababb7] | 1565 | run_expr(run, access->arg, &rarg);
|
---|
[23de644] | 1566 | if (run_is_bo(run)) {
|
---|
[051b3db8] | 1567 | *res = run_recovery_item(run);
|
---|
| 1568 | goto cleanup;
|
---|
[23de644] | 1569 | }
|
---|
| 1570 |
|
---|
[fa36f29] | 1571 | if (rarg == NULL) {
|
---|
| 1572 | printf("Error: Sub-expression has no value.\n");
|
---|
| 1573 | exit(1);
|
---|
| 1574 | }
|
---|
| 1575 |
|
---|
| 1576 | run_access_item(run, access, rarg, res);
|
---|
[051b3db8] | 1577 | cleanup:
|
---|
| 1578 | if (rarg != NULL)
|
---|
| 1579 | rdata_item_destroy(rarg);
|
---|
[fa36f29] | 1580 | }
|
---|
| 1581 |
|
---|
[38aaacc2] | 1582 | /** Evaluate member acccess (with base already evaluated).
|
---|
| 1583 | *
|
---|
| 1584 | * @param run Runner object
|
---|
| 1585 | * @param access Access operation
|
---|
| 1586 | * @param arg Evaluated base expression
|
---|
| 1587 | * @param res Place to store result
|
---|
| 1588 | */
|
---|
[fa36f29] | 1589 | static void run_access_item(run_t *run, stree_access_t *access,
|
---|
| 1590 | rdata_item_t *arg, rdata_item_t **res)
|
---|
| 1591 | {
|
---|
| 1592 | var_class_t vc;
|
---|
[09ababb7] | 1593 |
|
---|
| 1594 | #ifdef DEBUG_RUN_TRACE
|
---|
[fa36f29] | 1595 | printf("Run access operation on pre-evaluated base.\n");
|
---|
[09ababb7] | 1596 | #endif
|
---|
[39e8406] | 1597 | vc = run_item_get_vc(run, arg);
|
---|
[09ababb7] | 1598 |
|
---|
[fa36f29] | 1599 | switch (vc) {
|
---|
| 1600 | case vc_ref:
|
---|
| 1601 | run_access_ref(run, access, arg, res);
|
---|
| 1602 | break;
|
---|
| 1603 | case vc_deleg:
|
---|
| 1604 | run_access_deleg(run, access, arg, res);
|
---|
| 1605 | break;
|
---|
| 1606 | case vc_object:
|
---|
| 1607 | run_access_object(run, access, arg, res);
|
---|
| 1608 | break;
|
---|
[051bc69a] | 1609 | case vc_symbol:
|
---|
| 1610 | run_access_symbol(run, access, arg, res);
|
---|
| 1611 | break;
|
---|
| 1612 |
|
---|
| 1613 | case vc_bool:
|
---|
| 1614 | case vc_char:
|
---|
| 1615 | case vc_enum:
|
---|
| 1616 | case vc_int:
|
---|
| 1617 | case vc_string:
|
---|
| 1618 | case vc_array:
|
---|
| 1619 | case vc_resource:
|
---|
[fa36f29] | 1620 | printf("Unimplemented: Using access operator ('.') "
|
---|
| 1621 | "with unsupported data type (value/%d).\n", vc);
|
---|
| 1622 | exit(1);
|
---|
| 1623 | }
|
---|
| 1624 | }
|
---|
[09ababb7] | 1625 |
|
---|
[38aaacc2] | 1626 | /** Evaluate reference acccess.
|
---|
| 1627 | *
|
---|
| 1628 | * @param run Runner object
|
---|
| 1629 | * @param access Access operation
|
---|
| 1630 | * @param arg Evaluated base expression
|
---|
| 1631 | * @param res Place to store result
|
---|
| 1632 | */
|
---|
[fa36f29] | 1633 | static void run_access_ref(run_t *run, stree_access_t *access,
|
---|
| 1634 | rdata_item_t *arg, rdata_item_t **res)
|
---|
| 1635 | {
|
---|
| 1636 | rdata_item_t *darg;
|
---|
| 1637 |
|
---|
| 1638 | /* Implicitly dereference. */
|
---|
[051bc69a] | 1639 | run_dereference(run, arg, access->arg->cspan, &darg);
|
---|
[fa36f29] | 1640 |
|
---|
[1ebc1a62] | 1641 | if (run->thread_ar->bo_mode != bm_none) {
|
---|
| 1642 | *res = run_recovery_item(run);
|
---|
| 1643 | return;
|
---|
| 1644 | }
|
---|
| 1645 |
|
---|
[fa36f29] | 1646 | /* Try again. */
|
---|
| 1647 | run_access_item(run, access, darg, res);
|
---|
[051b3db8] | 1648 |
|
---|
| 1649 | /* Destroy temporary */
|
---|
| 1650 | rdata_item_destroy(darg);
|
---|
[fa36f29] | 1651 | }
|
---|
| 1652 |
|
---|
[c5cb943d] | 1653 | /** Evaluate delegate member acccess.
|
---|
[38aaacc2] | 1654 | *
|
---|
| 1655 | * @param run Runner object
|
---|
| 1656 | * @param access Access operation
|
---|
| 1657 | * @param arg Evaluated base expression
|
---|
| 1658 | * @param res Place to store result
|
---|
| 1659 | */
|
---|
[fa36f29] | 1660 | static void run_access_deleg(run_t *run, stree_access_t *access,
|
---|
| 1661 | rdata_item_t *arg, rdata_item_t **res)
|
---|
| 1662 | {
|
---|
[c5cb943d] | 1663 | (void) run;
|
---|
| 1664 | (void) access;
|
---|
| 1665 | (void) arg;
|
---|
| 1666 | (void) res;
|
---|
| 1667 |
|
---|
| 1668 | printf("Error: Using '.' with delegate.\n");
|
---|
| 1669 | exit(1);
|
---|
| 1670 | }
|
---|
| 1671 |
|
---|
| 1672 | /** Evaluate object member acccess.
|
---|
| 1673 | *
|
---|
| 1674 | * @param run Runner object
|
---|
| 1675 | * @param access Access operation
|
---|
| 1676 | * @param arg Evaluated base expression
|
---|
| 1677 | * @param res Place to store result
|
---|
| 1678 | */
|
---|
| 1679 | static void run_access_object(run_t *run, stree_access_t *access,
|
---|
| 1680 | rdata_item_t *arg, rdata_item_t **res)
|
---|
| 1681 | {
|
---|
| 1682 | rdata_var_t *obj_var;
|
---|
| 1683 | rdata_object_t *object;
|
---|
| 1684 |
|
---|
| 1685 | #ifdef DEBUG_RUN_TRACE
|
---|
| 1686 | printf("Run object access operation.\n");
|
---|
| 1687 | #endif
|
---|
| 1688 | assert(arg->ic == ic_address);
|
---|
| 1689 | assert(arg->u.address->ac == ac_var);
|
---|
| 1690 |
|
---|
| 1691 | obj_var = arg->u.address->u.var_a->vref;
|
---|
| 1692 | assert(obj_var->vc == vc_object);
|
---|
| 1693 |
|
---|
| 1694 | object = obj_var->u.object_v;
|
---|
| 1695 |
|
---|
| 1696 | if (object->static_obj == sn_static)
|
---|
| 1697 | run_access_object_static(run, access, obj_var, res);
|
---|
| 1698 | else
|
---|
| 1699 | run_access_object_nonstatic(run, access, obj_var, res);
|
---|
| 1700 | }
|
---|
| 1701 |
|
---|
| 1702 | /** Evaluate static object member acccess.
|
---|
| 1703 | *
|
---|
| 1704 | * @param run Runner object
|
---|
| 1705 | * @param access Access operation
|
---|
| 1706 | * @param arg Evaluated base expression
|
---|
| 1707 | * @param res Place to store result
|
---|
| 1708 | */
|
---|
| 1709 | static void run_access_object_static(run_t *run, stree_access_t *access,
|
---|
| 1710 | rdata_var_t *obj_var, rdata_item_t **res)
|
---|
| 1711 | {
|
---|
| 1712 | rdata_object_t *object;
|
---|
[fa36f29] | 1713 | stree_symbol_t *member;
|
---|
[c5cb943d] | 1714 | stree_csi_t *member_csi;
|
---|
| 1715 |
|
---|
| 1716 | rdata_deleg_t *deleg_v;
|
---|
| 1717 | rdata_item_t *ritem;
|
---|
| 1718 | rdata_value_t *rvalue;
|
---|
| 1719 | rdata_var_t *rvar;
|
---|
| 1720 | rdata_address_t *address;
|
---|
| 1721 | rdata_addr_var_t *addr_var;
|
---|
| 1722 | rdata_addr_prop_t *addr_prop;
|
---|
| 1723 | rdata_aprop_named_t *aprop_named;
|
---|
| 1724 | rdata_deleg_t *deleg_p;
|
---|
| 1725 | rdata_var_t *mvar;
|
---|
[09ababb7] | 1726 |
|
---|
| 1727 | #ifdef DEBUG_RUN_TRACE
|
---|
[c5cb943d] | 1728 | printf("Run static object access operation.\n");
|
---|
[fa36f29] | 1729 | #endif
|
---|
[c5cb943d] | 1730 | assert(obj_var->vc == vc_object);
|
---|
| 1731 | object = obj_var->u.object_v;
|
---|
[fa36f29] | 1732 |
|
---|
[c5cb943d] | 1733 | assert(object->static_obj == sn_static);
|
---|
[fa36f29] | 1734 |
|
---|
[c5cb943d] | 1735 | member = symbol_search_csi(run->program, object->class_sym->u.csi,
|
---|
[fa36f29] | 1736 | access->member_name);
|
---|
| 1737 |
|
---|
[074444f] | 1738 | /* Member existence should be ensured by static type checking. */
|
---|
| 1739 | assert(member != NULL);
|
---|
[fa36f29] | 1740 |
|
---|
| 1741 | #ifdef DEBUG_RUN_TRACE
|
---|
| 1742 | printf("Found member '%s'.\n",
|
---|
| 1743 | strtab_get_str(access->member_name->sid));
|
---|
[09ababb7] | 1744 | #endif
|
---|
| 1745 |
|
---|
[c5cb943d] | 1746 | switch (member->sc) {
|
---|
| 1747 | case sc_csi:
|
---|
| 1748 | /* Get child static object. */
|
---|
| 1749 | member_csi = symbol_to_csi(member);
|
---|
| 1750 | assert(member_csi != NULL);
|
---|
| 1751 |
|
---|
| 1752 | mvar = run_sobject_get(run, member_csi, obj_var,
|
---|
| 1753 | access->member_name->sid);
|
---|
| 1754 |
|
---|
| 1755 | ritem = rdata_item_new(ic_address);
|
---|
| 1756 | address = rdata_address_new(ac_var);
|
---|
| 1757 | ritem->u.address = address;
|
---|
| 1758 |
|
---|
| 1759 | addr_var = rdata_addr_var_new();
|
---|
| 1760 | address->u.var_a = addr_var;
|
---|
| 1761 | addr_var->vref = mvar;
|
---|
| 1762 |
|
---|
| 1763 | *res = ritem;
|
---|
| 1764 | break;
|
---|
| 1765 | case sc_ctor:
|
---|
| 1766 | /* It is not possible to reference a constructor explicitly. */
|
---|
| 1767 | assert(b_false);
|
---|
[dc12262] | 1768 | /* Fallthrough */
|
---|
[c5cb943d] | 1769 | case sc_deleg:
|
---|
| 1770 | printf("Error: Accessing object member which is a delegate.\n");
|
---|
| 1771 | exit(1);
|
---|
| 1772 | case sc_enum:
|
---|
| 1773 | printf("Error: Accessing object member which is an enum.\n");
|
---|
| 1774 | exit(1);
|
---|
| 1775 | case sc_fun:
|
---|
| 1776 | /* Construct anonymous delegate. */
|
---|
| 1777 | ritem = rdata_item_new(ic_value);
|
---|
| 1778 | rvalue = rdata_value_new();
|
---|
| 1779 | ritem->u.value = rvalue;
|
---|
| 1780 |
|
---|
| 1781 | rvar = rdata_var_new(vc_deleg);
|
---|
| 1782 | rvalue->var = rvar;
|
---|
| 1783 |
|
---|
| 1784 | deleg_v = rdata_deleg_new();
|
---|
| 1785 | rvar->u.deleg_v = deleg_v;
|
---|
| 1786 |
|
---|
| 1787 | deleg_v->obj = obj_var;
|
---|
| 1788 | deleg_v->sym = member;
|
---|
| 1789 | *res = ritem;
|
---|
| 1790 | break;
|
---|
| 1791 | case sc_var:
|
---|
| 1792 | /* Get static object member variable. */
|
---|
| 1793 | mvar = intmap_get(&object->fields, access->member_name->sid);
|
---|
| 1794 |
|
---|
| 1795 | ritem = rdata_item_new(ic_address);
|
---|
| 1796 | address = rdata_address_new(ac_var);
|
---|
| 1797 | ritem->u.address = address;
|
---|
| 1798 |
|
---|
| 1799 | addr_var = rdata_addr_var_new();
|
---|
| 1800 | address->u.var_a = addr_var;
|
---|
| 1801 | addr_var->vref = mvar;
|
---|
| 1802 |
|
---|
| 1803 | *res = ritem;
|
---|
| 1804 | break;
|
---|
| 1805 | case sc_prop:
|
---|
| 1806 | /* Construct named property address. */
|
---|
| 1807 | ritem = rdata_item_new(ic_address);
|
---|
| 1808 | address = rdata_address_new(ac_prop);
|
---|
| 1809 | addr_prop = rdata_addr_prop_new(apc_named);
|
---|
| 1810 | aprop_named = rdata_aprop_named_new();
|
---|
| 1811 | ritem->u.address = address;
|
---|
| 1812 | address->u.prop_a = addr_prop;
|
---|
| 1813 | addr_prop->u.named = aprop_named;
|
---|
| 1814 |
|
---|
| 1815 | deleg_p = rdata_deleg_new();
|
---|
| 1816 | deleg_p->obj = obj_var;
|
---|
| 1817 | deleg_p->sym = member;
|
---|
| 1818 | addr_prop->u.named->prop_d = deleg_p;
|
---|
| 1819 |
|
---|
| 1820 | *res = ritem;
|
---|
| 1821 | break;
|
---|
| 1822 | }
|
---|
[fa36f29] | 1823 | }
|
---|
| 1824 |
|
---|
[38aaacc2] | 1825 | /** Evaluate object member acccess.
|
---|
| 1826 | *
|
---|
| 1827 | * @param run Runner object
|
---|
| 1828 | * @param access Access operation
|
---|
| 1829 | * @param arg Evaluated base expression
|
---|
| 1830 | * @param res Place to store result
|
---|
| 1831 | */
|
---|
[c5cb943d] | 1832 | static void run_access_object_nonstatic(run_t *run, stree_access_t *access,
|
---|
| 1833 | rdata_var_t *obj_var, rdata_item_t **res)
|
---|
[fa36f29] | 1834 | {
|
---|
| 1835 | rdata_object_t *object;
|
---|
[c5cb943d] | 1836 | stree_symbol_t *member;
|
---|
[fa36f29] | 1837 | rdata_item_t *ritem;
|
---|
| 1838 | rdata_address_t *address;
|
---|
[d0febca] | 1839 | rdata_addr_var_t *addr_var;
|
---|
| 1840 | rdata_addr_prop_t *addr_prop;
|
---|
| 1841 | rdata_aprop_named_t *aprop_named;
|
---|
| 1842 | rdata_deleg_t *deleg_p;
|
---|
[fa36f29] | 1843 |
|
---|
| 1844 | rdata_value_t *value;
|
---|
| 1845 | rdata_deleg_t *deleg_v;
|
---|
| 1846 | rdata_var_t *var;
|
---|
| 1847 |
|
---|
| 1848 | #ifdef DEBUG_RUN_TRACE
|
---|
[c5cb943d] | 1849 | printf("Run nonstatic object access operation.\n");
|
---|
[fa36f29] | 1850 | #endif
|
---|
[c5cb943d] | 1851 | assert(obj_var->vc == vc_object);
|
---|
| 1852 | object = obj_var->u.object_v;
|
---|
[fa36f29] | 1853 |
|
---|
[c5cb943d] | 1854 | assert(object->static_obj == sn_nonstatic);
|
---|
[fa36f29] | 1855 |
|
---|
| 1856 | member = symbol_search_csi(run->program, object->class_sym->u.csi,
|
---|
| 1857 | access->member_name);
|
---|
| 1858 |
|
---|
| 1859 | if (member == NULL) {
|
---|
| 1860 | printf("Error: Object of class '");
|
---|
[39e8406] | 1861 | symbol_print_fqn(object->class_sym);
|
---|
[fa36f29] | 1862 | printf("' has no member named '%s'.\n",
|
---|
| 1863 | strtab_get_str(access->member_name->sid));
|
---|
| 1864 | exit(1);
|
---|
| 1865 | }
|
---|
| 1866 |
|
---|
| 1867 | #ifdef DEBUG_RUN_TRACE
|
---|
| 1868 | printf("Found member '%s'.\n",
|
---|
| 1869 | strtab_get_str(access->member_name->sid));
|
---|
| 1870 | #endif
|
---|
| 1871 |
|
---|
[6c39a907] | 1872 | /* Make compiler happy. */
|
---|
| 1873 | ritem = NULL;
|
---|
| 1874 |
|
---|
[fa36f29] | 1875 | switch (member->sc) {
|
---|
| 1876 | case sc_csi:
|
---|
| 1877 | printf("Error: Accessing object member which is nested CSI.\n");
|
---|
| 1878 | exit(1);
|
---|
[c5cb943d] | 1879 | case sc_ctor:
|
---|
| 1880 | /* It is not possible to reference a constructor explicitly. */
|
---|
| 1881 | assert(b_false);
|
---|
[dc12262] | 1882 | /* Fallthrough */
|
---|
[38aaacc2] | 1883 | case sc_deleg:
|
---|
| 1884 | printf("Error: Accessing object member which is a delegate.\n");
|
---|
| 1885 | exit(1);
|
---|
[051bc69a] | 1886 | case sc_enum:
|
---|
| 1887 | printf("Error: Accessing object member which is an enum.\n");
|
---|
| 1888 | exit(1);
|
---|
[fa36f29] | 1889 | case sc_fun:
|
---|
[38aaacc2] | 1890 | /* Construct anonymous delegate. */
|
---|
[fa36f29] | 1891 | ritem = rdata_item_new(ic_value);
|
---|
| 1892 | value = rdata_value_new();
|
---|
| 1893 | ritem->u.value = value;
|
---|
| 1894 |
|
---|
| 1895 | var = rdata_var_new(vc_deleg);
|
---|
| 1896 | value->var = var;
|
---|
| 1897 | deleg_v = rdata_deleg_new();
|
---|
| 1898 | var->u.deleg_v = deleg_v;
|
---|
| 1899 |
|
---|
[c5cb943d] | 1900 | deleg_v->obj = obj_var;
|
---|
[09ababb7] | 1901 | deleg_v->sym = member;
|
---|
[fa36f29] | 1902 | break;
|
---|
| 1903 | case sc_var:
|
---|
| 1904 | /* Construct variable address item. */
|
---|
| 1905 | ritem = rdata_item_new(ic_address);
|
---|
[d0febca] | 1906 | address = rdata_address_new(ac_var);
|
---|
| 1907 | addr_var = rdata_addr_var_new();
|
---|
[fa36f29] | 1908 | ritem->u.address = address;
|
---|
[d0febca] | 1909 | address->u.var_a = addr_var;
|
---|
[09ababb7] | 1910 |
|
---|
[d0febca] | 1911 | addr_var->vref = intmap_get(&object->fields,
|
---|
[fa36f29] | 1912 | access->member_name->sid);
|
---|
[d0febca] | 1913 | assert(addr_var->vref != NULL);
|
---|
[fa36f29] | 1914 | break;
|
---|
| 1915 | case sc_prop:
|
---|
[d0febca] | 1916 | /* Construct named property address. */
|
---|
| 1917 | ritem = rdata_item_new(ic_address);
|
---|
| 1918 | address = rdata_address_new(ac_prop);
|
---|
| 1919 | addr_prop = rdata_addr_prop_new(apc_named);
|
---|
| 1920 | aprop_named = rdata_aprop_named_new();
|
---|
| 1921 | ritem->u.address = address;
|
---|
| 1922 | address->u.prop_a = addr_prop;
|
---|
| 1923 | addr_prop->u.named = aprop_named;
|
---|
| 1924 |
|
---|
| 1925 | deleg_p = rdata_deleg_new();
|
---|
[c5cb943d] | 1926 | deleg_p->obj = obj_var;
|
---|
[d0febca] | 1927 | deleg_p->sym = member;
|
---|
| 1928 | addr_prop->u.named->prop_d = deleg_p;
|
---|
| 1929 | break;
|
---|
[09ababb7] | 1930 | }
|
---|
| 1931 |
|
---|
[fa36f29] | 1932 | *res = ritem;
|
---|
[09ababb7] | 1933 | }
|
---|
| 1934 |
|
---|
[051bc69a] | 1935 | /** Evaluate symbol member acccess.
|
---|
| 1936 | *
|
---|
| 1937 | * @param run Runner object
|
---|
| 1938 | * @param access Access operation
|
---|
| 1939 | * @param arg Evaluated base expression
|
---|
| 1940 | * @param res Place to store result
|
---|
| 1941 | */
|
---|
| 1942 | static void run_access_symbol(run_t *run, stree_access_t *access,
|
---|
| 1943 | rdata_item_t *arg, rdata_item_t **res)
|
---|
| 1944 | {
|
---|
| 1945 | rdata_item_t *arg_vi;
|
---|
| 1946 | rdata_value_t *arg_val;
|
---|
| 1947 | rdata_symbol_t *symbol_v;
|
---|
| 1948 | stree_embr_t *embr;
|
---|
| 1949 |
|
---|
| 1950 | rdata_item_t *ritem;
|
---|
| 1951 | rdata_value_t *rvalue;
|
---|
| 1952 | rdata_var_t *rvar;
|
---|
| 1953 | rdata_enum_t *enum_v;
|
---|
| 1954 |
|
---|
| 1955 | #ifdef DEBUG_RUN_TRACE
|
---|
| 1956 | printf("Run symbol access operation.\n");
|
---|
| 1957 | #endif
|
---|
| 1958 | run_cvt_value_item(run, arg, &arg_vi);
|
---|
[051b3db8] | 1959 | if (run_is_bo(run)) {
|
---|
| 1960 | *res = run_recovery_item(run);
|
---|
| 1961 | return;
|
---|
| 1962 | }
|
---|
| 1963 |
|
---|
[051bc69a] | 1964 | arg_val = arg_vi->u.value;
|
---|
| 1965 | assert(arg_val->var->vc == vc_symbol);
|
---|
| 1966 |
|
---|
| 1967 | symbol_v = arg_val->var->u.symbol_v;
|
---|
| 1968 |
|
---|
| 1969 | /* XXX Port CSI symbol reference to using vc_symbol */
|
---|
| 1970 | assert(symbol_v->sym->sc == sc_enum);
|
---|
| 1971 |
|
---|
| 1972 | embr = stree_enum_find_mbr(symbol_v->sym->u.enum_d,
|
---|
| 1973 | access->member_name);
|
---|
| 1974 |
|
---|
[051b3db8] | 1975 | rdata_item_destroy(arg_vi);
|
---|
| 1976 |
|
---|
[051bc69a] | 1977 | /* Member existence should be ensured by static type checking. */
|
---|
| 1978 | assert(embr != NULL);
|
---|
| 1979 |
|
---|
| 1980 | #ifdef DEBUG_RUN_TRACE
|
---|
| 1981 | printf("Found enum member '%s'.\n",
|
---|
| 1982 | strtab_get_str(access->member_name->sid));
|
---|
| 1983 | #endif
|
---|
| 1984 | ritem = rdata_item_new(ic_value);
|
---|
| 1985 | rvalue = rdata_value_new();
|
---|
| 1986 | rvar = rdata_var_new(vc_enum);
|
---|
| 1987 | enum_v = rdata_enum_new();
|
---|
| 1988 |
|
---|
| 1989 | ritem->u.value = rvalue;
|
---|
| 1990 | rvalue->var = rvar;
|
---|
| 1991 | rvar->u.enum_v = enum_v;
|
---|
| 1992 | enum_v->value = embr;
|
---|
| 1993 |
|
---|
| 1994 | *res = ritem;
|
---|
| 1995 | }
|
---|
| 1996 |
|
---|
[38aaacc2] | 1997 | /** Call a function.
|
---|
| 1998 | *
|
---|
| 1999 | * Call a function and return the result in @a res.
|
---|
| 2000 | *
|
---|
| 2001 | * @param run Runner object
|
---|
| 2002 | * @param call Call operation
|
---|
| 2003 | * @param res Place to store result
|
---|
| 2004 | */
|
---|
[09ababb7] | 2005 | static void run_call(run_t *run, stree_call_t *call, rdata_item_t **res)
|
---|
| 2006 | {
|
---|
[38aaacc2] | 2007 | rdata_item_t *rdeleg, *rdeleg_vi;
|
---|
[09ababb7] | 2008 | rdata_deleg_t *deleg_v;
|
---|
| 2009 | list_t arg_vals;
|
---|
| 2010 |
|
---|
[fa36f29] | 2011 | stree_fun_t *fun;
|
---|
[d0febca] | 2012 | run_proc_ar_t *proc_ar;
|
---|
[fa36f29] | 2013 |
|
---|
[09ababb7] | 2014 | #ifdef DEBUG_RUN_TRACE
|
---|
| 2015 | printf("Run call operation.\n");
|
---|
| 2016 | #endif
|
---|
[051b3db8] | 2017 | rdeleg = NULL;
|
---|
| 2018 | rdeleg_vi = NULL;
|
---|
| 2019 |
|
---|
[38aaacc2] | 2020 | run_expr(run, call->fun, &rdeleg);
|
---|
[23de644] | 2021 | if (run_is_bo(run)) {
|
---|
[051b3db8] | 2022 | *res = run_recovery_item(run);
|
---|
| 2023 | goto cleanup;
|
---|
[23de644] | 2024 | }
|
---|
[09ababb7] | 2025 |
|
---|
[051b3db8] | 2026 | run_cvt_value_item(run, rdeleg, &rdeleg_vi);
|
---|
| 2027 | if (run_is_bo(run)) {
|
---|
[1ebc1a62] | 2028 | *res = run_recovery_item(run);
|
---|
[051b3db8] | 2029 | goto cleanup;
|
---|
[1ebc1a62] | 2030 | }
|
---|
| 2031 |
|
---|
[38aaacc2] | 2032 | assert(rdeleg_vi->ic == ic_value);
|
---|
| 2033 |
|
---|
| 2034 | if (rdeleg_vi->u.value->var->vc != vc_deleg) {
|
---|
| 2035 | printf("Unimplemented: Call expression of this type (");
|
---|
| 2036 | rdata_item_print(rdeleg_vi);
|
---|
| 2037 | printf(").\n");
|
---|
[1ebc1a62] | 2038 | exit(1);
|
---|
[09ababb7] | 2039 | }
|
---|
| 2040 |
|
---|
[38aaacc2] | 2041 | deleg_v = rdeleg_vi->u.value->var->u.deleg_v;
|
---|
[09ababb7] | 2042 |
|
---|
| 2043 | if (deleg_v->sym->sc != sc_fun) {
|
---|
| 2044 | printf("Error: Called symbol is not a function.\n");
|
---|
| 2045 | exit(1);
|
---|
| 2046 | }
|
---|
| 2047 |
|
---|
| 2048 | #ifdef DEBUG_RUN_TRACE
|
---|
| 2049 | printf("Call function '");
|
---|
[39e8406] | 2050 | symbol_print_fqn(deleg_v->sym);
|
---|
[09ababb7] | 2051 | printf("'\n");
|
---|
| 2052 | #endif
|
---|
| 2053 | /* Evaluate function arguments. */
|
---|
[051bc69a] | 2054 | run_call_args(run, &call->args, &arg_vals);
|
---|
| 2055 | if (run_is_bo(run)) {
|
---|
[051b3db8] | 2056 | *res = run_recovery_item(run);
|
---|
| 2057 | goto cleanup;
|
---|
[09ababb7] | 2058 | }
|
---|
| 2059 |
|
---|
[fa36f29] | 2060 | fun = symbol_to_fun(deleg_v->sym);
|
---|
| 2061 | assert(fun != NULL);
|
---|
| 2062 |
|
---|
[d0febca] | 2063 | /* Create procedure activation record. */
|
---|
[39e8406] | 2064 | run_proc_ar_create(run, deleg_v->obj, fun->proc, &proc_ar);
|
---|
[fa36f29] | 2065 |
|
---|
| 2066 | /* Fill in argument values. */
|
---|
[d0febca] | 2067 | run_proc_ar_set_args(run, proc_ar, &arg_vals);
|
---|
[fa36f29] | 2068 |
|
---|
[051b3db8] | 2069 | /* Destroy arg_vals, they are no longer needed. */
|
---|
| 2070 | run_destroy_arg_vals(&arg_vals);
|
---|
| 2071 |
|
---|
[fa36f29] | 2072 | /* Run the function. */
|
---|
[d0febca] | 2073 | run_proc(run, proc_ar, res);
|
---|
[09ababb7] | 2074 |
|
---|
[051bc69a] | 2075 | if (!run_is_bo(run) && fun->sig->rtype != NULL && *res == NULL) {
|
---|
| 2076 | printf("Error: Function '");
|
---|
| 2077 | symbol_print_fqn(deleg_v->sym);
|
---|
| 2078 | printf("' did not return a value.\n");
|
---|
| 2079 | exit(1);
|
---|
| 2080 | }
|
---|
| 2081 |
|
---|
[051b3db8] | 2082 | /* Destroy procedure activation record. */
|
---|
| 2083 | run_proc_ar_destroy(run, proc_ar);
|
---|
| 2084 |
|
---|
| 2085 | cleanup:
|
---|
| 2086 | if (rdeleg != NULL)
|
---|
| 2087 | rdata_item_destroy(rdeleg);
|
---|
| 2088 | if (rdeleg_vi != NULL)
|
---|
| 2089 | rdata_item_destroy(rdeleg_vi);
|
---|
| 2090 |
|
---|
[09ababb7] | 2091 | #ifdef DEBUG_RUN_TRACE
|
---|
| 2092 | printf("Returned from function call.\n");
|
---|
| 2093 | #endif
|
---|
| 2094 | }
|
---|
| 2095 |
|
---|
[051bc69a] | 2096 | /** Evaluate call arguments.
|
---|
| 2097 | *
|
---|
| 2098 | * Evaluate arguments to function or constructor.
|
---|
| 2099 | *
|
---|
| 2100 | * @param run Runner object
|
---|
| 2101 | * @param args Real arguments (list of stree_expr_t)
|
---|
| 2102 | * @param arg_vals Address of uninitialized list to store argument values
|
---|
| 2103 | * (list of rdata_item_t).
|
---|
| 2104 | */
|
---|
| 2105 | static void run_call_args(run_t *run, list_t *args, list_t *arg_vals)
|
---|
| 2106 | {
|
---|
| 2107 | list_node_t *arg_n;
|
---|
| 2108 | stree_expr_t *arg;
|
---|
| 2109 | rdata_item_t *rarg_i, *rarg_vi;
|
---|
| 2110 |
|
---|
| 2111 | /* Evaluate function arguments. */
|
---|
| 2112 | list_init(arg_vals);
|
---|
| 2113 | arg_n = list_first(args);
|
---|
| 2114 |
|
---|
| 2115 | while (arg_n != NULL) {
|
---|
| 2116 | arg = list_node_data(arg_n, stree_expr_t *);
|
---|
| 2117 | run_expr(run, arg, &rarg_i);
|
---|
| 2118 | if (run_is_bo(run))
|
---|
[051b3db8] | 2119 | goto error;
|
---|
[051bc69a] | 2120 |
|
---|
| 2121 | run_cvt_value_item(run, rarg_i, &rarg_vi);
|
---|
[051b3db8] | 2122 | rdata_item_destroy(rarg_i);
|
---|
| 2123 | if (run_is_bo(run))
|
---|
| 2124 | goto error;
|
---|
[051bc69a] | 2125 |
|
---|
| 2126 | list_append(arg_vals, rarg_vi);
|
---|
| 2127 | arg_n = list_next(args, arg_n);
|
---|
| 2128 | }
|
---|
[051b3db8] | 2129 | return;
|
---|
| 2130 |
|
---|
| 2131 | error:
|
---|
| 2132 | /*
|
---|
| 2133 | * An exception or error occured while evaluating one of the
|
---|
| 2134 | * arguments. Destroy already obtained argument values and
|
---|
| 2135 | * dismantle the list.
|
---|
| 2136 | */
|
---|
| 2137 | run_destroy_arg_vals(arg_vals);
|
---|
| 2138 | }
|
---|
| 2139 |
|
---|
| 2140 | /** Destroy list of evaluated arguments.
|
---|
| 2141 | *
|
---|
| 2142 | * Provided a list of evaluated arguments, destroy them, removing them
|
---|
| 2143 | * from the list and fini the list itself.
|
---|
| 2144 | *
|
---|
| 2145 | * @param arg_vals List of evaluated arguments (value items,
|
---|
| 2146 | * rdata_item_t).
|
---|
| 2147 | */
|
---|
| 2148 | static void run_destroy_arg_vals(list_t *arg_vals)
|
---|
| 2149 | {
|
---|
| 2150 | list_node_t *val_n;
|
---|
| 2151 | rdata_item_t *val_i;
|
---|
| 2152 |
|
---|
| 2153 | /*
|
---|
| 2154 | * An exception or error occured while evaluating one of the
|
---|
| 2155 | * arguments. Destroy already obtained argument values and
|
---|
| 2156 | * dismantle the list.
|
---|
| 2157 | */
|
---|
| 2158 | while (!list_is_empty(arg_vals)) {
|
---|
| 2159 | val_n = list_first(arg_vals);
|
---|
| 2160 | val_i = list_node_data(val_n, rdata_item_t *);
|
---|
| 2161 |
|
---|
| 2162 | rdata_item_destroy(val_i);
|
---|
| 2163 | list_remove(arg_vals, val_n);
|
---|
| 2164 | }
|
---|
| 2165 | list_fini(arg_vals);
|
---|
[051bc69a] | 2166 | }
|
---|
| 2167 |
|
---|
[38aaacc2] | 2168 | /** Run index operation.
|
---|
| 2169 | *
|
---|
| 2170 | * Evaluate operation per the indexing ('[', ']') operator.
|
---|
| 2171 | *
|
---|
| 2172 | * @param run Runner object
|
---|
| 2173 | * @param index Index operation
|
---|
| 2174 | * @param res Place to store result
|
---|
| 2175 | */
|
---|
[94d484a] | 2176 | static void run_index(run_t *run, stree_index_t *index, rdata_item_t **res)
|
---|
| 2177 | {
|
---|
| 2178 | rdata_item_t *rbase;
|
---|
| 2179 | rdata_item_t *base_i;
|
---|
| 2180 | list_node_t *node;
|
---|
| 2181 | stree_expr_t *arg;
|
---|
| 2182 | rdata_item_t *rarg_i, *rarg_vi;
|
---|
| 2183 | var_class_t vc;
|
---|
[d0febca] | 2184 | list_t arg_vals;
|
---|
[051b3db8] | 2185 | list_node_t *val_n;
|
---|
| 2186 | rdata_item_t *val_i;
|
---|
[94d484a] | 2187 |
|
---|
| 2188 | #ifdef DEBUG_RUN_TRACE
|
---|
| 2189 | printf("Run index operation.\n");
|
---|
| 2190 | #endif
|
---|
| 2191 | run_expr(run, index->base, &rbase);
|
---|
[23de644] | 2192 | if (run_is_bo(run)) {
|
---|
[051b3db8] | 2193 | *res = run_recovery_item(run);
|
---|
[23de644] | 2194 | return;
|
---|
| 2195 | }
|
---|
[94d484a] | 2196 |
|
---|
[39e8406] | 2197 | vc = run_item_get_vc(run, rbase);
|
---|
[d0febca] | 2198 |
|
---|
| 2199 | /* Implicitly dereference. */
|
---|
| 2200 | if (vc == vc_ref) {
|
---|
[051bc69a] | 2201 | run_dereference(run, rbase, index->base->cspan, &base_i);
|
---|
[051b3db8] | 2202 | rdata_item_destroy(rbase);
|
---|
[051bc69a] | 2203 | if (run_is_bo(run)) {
|
---|
[051b3db8] | 2204 | *res = run_recovery_item(run);
|
---|
[051bc69a] | 2205 | return;
|
---|
| 2206 | }
|
---|
[d0febca] | 2207 | } else {
|
---|
| 2208 | base_i = rbase;
|
---|
| 2209 | }
|
---|
| 2210 |
|
---|
[39e8406] | 2211 | vc = run_item_get_vc(run, base_i);
|
---|
[d0febca] | 2212 |
|
---|
| 2213 | /* Evaluate arguments (indices). */
|
---|
| 2214 | node = list_first(&index->args);
|
---|
| 2215 | list_init(&arg_vals);
|
---|
| 2216 |
|
---|
| 2217 | while (node != NULL) {
|
---|
| 2218 | arg = list_node_data(node, stree_expr_t *);
|
---|
| 2219 | run_expr(run, arg, &rarg_i);
|
---|
[23de644] | 2220 | if (run_is_bo(run)) {
|
---|
[051b3db8] | 2221 | *res = run_recovery_item(run);
|
---|
| 2222 | goto cleanup;
|
---|
[23de644] | 2223 | }
|
---|
| 2224 |
|
---|
[d0febca] | 2225 | run_cvt_value_item(run, rarg_i, &rarg_vi);
|
---|
[051b3db8] | 2226 | rdata_item_destroy(rarg_i);
|
---|
| 2227 | if (run_is_bo(run)) {
|
---|
| 2228 | *res = run_recovery_item(run);
|
---|
| 2229 | goto cleanup;
|
---|
| 2230 | }
|
---|
[d0febca] | 2231 |
|
---|
| 2232 | list_append(&arg_vals, rarg_vi);
|
---|
| 2233 |
|
---|
| 2234 | node = list_next(&index->args, node);
|
---|
| 2235 | }
|
---|
| 2236 |
|
---|
| 2237 | switch (vc) {
|
---|
| 2238 | case vc_array:
|
---|
| 2239 | run_index_array(run, index, base_i, &arg_vals, res);
|
---|
[94d484a] | 2240 | break;
|
---|
[d0febca] | 2241 | case vc_object:
|
---|
| 2242 | run_index_object(run, index, base_i, &arg_vals, res);
|
---|
| 2243 | break;
|
---|
| 2244 | case vc_string:
|
---|
| 2245 | run_index_string(run, index, base_i, &arg_vals, res);
|
---|
[94d484a] | 2246 | break;
|
---|
| 2247 | default:
|
---|
[d0febca] | 2248 | printf("Error: Indexing object of bad type (%d).\n", vc);
|
---|
[94d484a] | 2249 | exit(1);
|
---|
| 2250 | }
|
---|
[051b3db8] | 2251 |
|
---|
| 2252 | /* Destroy the indexing base temporary */
|
---|
| 2253 | rdata_item_destroy(base_i);
|
---|
| 2254 | cleanup:
|
---|
| 2255 | /*
|
---|
| 2256 | * An exception or error occured while evaluating one of the
|
---|
| 2257 | * arguments. Destroy already obtained argument values and
|
---|
| 2258 | * dismantle the list.
|
---|
| 2259 | */
|
---|
| 2260 | while (!list_is_empty(&arg_vals)) {
|
---|
| 2261 | val_n = list_first(&arg_vals);
|
---|
| 2262 | val_i = list_node_data(val_n, rdata_item_t *);
|
---|
| 2263 |
|
---|
| 2264 | rdata_item_destroy(val_i);
|
---|
| 2265 | list_remove(&arg_vals, val_n);
|
---|
| 2266 | }
|
---|
| 2267 |
|
---|
| 2268 | list_fini(&arg_vals);
|
---|
[d0febca] | 2269 | }
|
---|
[94d484a] | 2270 |
|
---|
[38aaacc2] | 2271 | /** Run index operation on array.
|
---|
| 2272 | *
|
---|
| 2273 | * @param run Runner object
|
---|
| 2274 | * @param index Index operation
|
---|
| 2275 | * @param base Evaluated base expression
|
---|
| 2276 | * @param args Evaluated indices (list of rdata_item_t)
|
---|
| 2277 | * @param res Place to store result
|
---|
| 2278 | */
|
---|
[d0febca] | 2279 | static void run_index_array(run_t *run, stree_index_t *index,
|
---|
| 2280 | rdata_item_t *base, list_t *args, rdata_item_t **res)
|
---|
| 2281 | {
|
---|
| 2282 | list_node_t *node;
|
---|
| 2283 | rdata_array_t *array;
|
---|
| 2284 | rdata_item_t *arg;
|
---|
[94d484a] | 2285 |
|
---|
[d0febca] | 2286 | int i;
|
---|
| 2287 | int elem_index;
|
---|
| 2288 | int arg_val;
|
---|
[23de644] | 2289 | int rc;
|
---|
[94d484a] | 2290 |
|
---|
[d0febca] | 2291 | rdata_item_t *ritem;
|
---|
| 2292 | rdata_address_t *address;
|
---|
| 2293 | rdata_addr_var_t *addr_var;
|
---|
[94d484a] | 2294 |
|
---|
[d0febca] | 2295 | #ifdef DEBUG_RUN_TRACE
|
---|
| 2296 | printf("Run array index operation.\n");
|
---|
| 2297 | #endif
|
---|
| 2298 | (void) run;
|
---|
| 2299 |
|
---|
| 2300 | assert(base->ic == ic_address);
|
---|
| 2301 | assert(base->u.address->ac == ac_var);
|
---|
| 2302 | assert(base->u.address->u.var_a->vref->vc == vc_array);
|
---|
| 2303 | array = base->u.address->u.var_a->vref->u.array_v;
|
---|
[94d484a] | 2304 |
|
---|
| 2305 | /*
|
---|
| 2306 | * Linear index of the desired element. Elements are stored in
|
---|
| 2307 | * lexicographic order with the last index changing the fastest.
|
---|
| 2308 | */
|
---|
| 2309 | elem_index = 0;
|
---|
| 2310 |
|
---|
[d0febca] | 2311 | node = list_first(args);
|
---|
[94d484a] | 2312 | i = 0;
|
---|
[d0febca] | 2313 |
|
---|
[94d484a] | 2314 | while (node != NULL) {
|
---|
| 2315 | if (i >= array->rank) {
|
---|
| 2316 | printf("Error: Too many indices for array of rank %d",
|
---|
| 2317 | array->rank);
|
---|
| 2318 | exit(1);
|
---|
| 2319 | }
|
---|
| 2320 |
|
---|
[d0febca] | 2321 | arg = list_node_data(node, rdata_item_t *);
|
---|
| 2322 | assert(arg->ic == ic_value);
|
---|
[94d484a] | 2323 |
|
---|
[d0febca] | 2324 | if (arg->u.value->var->vc != vc_int) {
|
---|
[94d484a] | 2325 | printf("Error: Array index is not an integer.\n");
|
---|
| 2326 | exit(1);
|
---|
| 2327 | }
|
---|
| 2328 |
|
---|
[23de644] | 2329 | rc = bigint_get_value_int(
|
---|
| 2330 | &arg->u.value->var->u.int_v->value,
|
---|
| 2331 | &arg_val);
|
---|
[94d484a] | 2332 |
|
---|
[23de644] | 2333 | if (rc != EOK || arg_val < 0 || arg_val >= array->extent[i]) {
|
---|
| 2334 | #ifdef DEBUG_RUN_TRACE
|
---|
[94d484a] | 2335 | printf("Error: Array index (value: %d) is out of range.\n",
|
---|
| 2336 | arg_val);
|
---|
[23de644] | 2337 | #endif
|
---|
| 2338 | /* Raise Error.OutOfBounds */
|
---|
| 2339 | run_raise_exc(run,
|
---|
[051bc69a] | 2340 | run->program->builtin->error_outofbounds,
|
---|
| 2341 | index->expr->cspan);
|
---|
| 2342 | /* XXX It should be cspan of the argument. */
|
---|
[1ebc1a62] | 2343 | *res = run_recovery_item(run);
|
---|
| 2344 | return;
|
---|
[94d484a] | 2345 | }
|
---|
| 2346 |
|
---|
| 2347 | elem_index = elem_index * array->extent[i] + arg_val;
|
---|
| 2348 |
|
---|
[d0febca] | 2349 | node = list_next(args, node);
|
---|
[94d484a] | 2350 | i += 1;
|
---|
| 2351 | }
|
---|
| 2352 |
|
---|
| 2353 | if (i < array->rank) {
|
---|
| 2354 | printf("Error: Too few indices for array of rank %d",
|
---|
| 2355 | array->rank);
|
---|
| 2356 | exit(1);
|
---|
| 2357 | }
|
---|
| 2358 |
|
---|
| 2359 | /* Construct variable address item. */
|
---|
| 2360 | ritem = rdata_item_new(ic_address);
|
---|
[d0febca] | 2361 | address = rdata_address_new(ac_var);
|
---|
| 2362 | addr_var = rdata_addr_var_new();
|
---|
| 2363 | ritem->u.address = address;
|
---|
| 2364 | address->u.var_a = addr_var;
|
---|
| 2365 |
|
---|
| 2366 | addr_var->vref = array->element[elem_index];
|
---|
| 2367 |
|
---|
| 2368 | *res = ritem;
|
---|
| 2369 | }
|
---|
| 2370 |
|
---|
[38aaacc2] | 2371 | /** Index an object (via its indexer).
|
---|
| 2372 | *
|
---|
| 2373 | * @param run Runner object
|
---|
| 2374 | * @param index Index operation
|
---|
| 2375 | * @param base Evaluated base expression
|
---|
| 2376 | * @param args Evaluated indices (list of rdata_item_t)
|
---|
| 2377 | * @param res Place to store result
|
---|
| 2378 | */
|
---|
[d0febca] | 2379 | static void run_index_object(run_t *run, stree_index_t *index,
|
---|
| 2380 | rdata_item_t *base, list_t *args, rdata_item_t **res)
|
---|
| 2381 | {
|
---|
| 2382 | rdata_item_t *ritem;
|
---|
| 2383 | rdata_address_t *address;
|
---|
| 2384 | rdata_addr_prop_t *addr_prop;
|
---|
| 2385 | rdata_aprop_indexed_t *aprop_indexed;
|
---|
| 2386 | rdata_var_t *obj_var;
|
---|
| 2387 | stree_csi_t *obj_csi;
|
---|
| 2388 | rdata_deleg_t *object_d;
|
---|
| 2389 | stree_symbol_t *indexer_sym;
|
---|
| 2390 | stree_ident_t *indexer_ident;
|
---|
| 2391 |
|
---|
| 2392 | list_node_t *node;
|
---|
[051b3db8] | 2393 | rdata_item_t *arg, *arg_copy;
|
---|
[d0febca] | 2394 |
|
---|
| 2395 | #ifdef DEBUG_RUN_TRACE
|
---|
| 2396 | printf("Run object index operation.\n");
|
---|
| 2397 | #endif
|
---|
| 2398 | (void) index;
|
---|
| 2399 |
|
---|
| 2400 | /* Construct property address item. */
|
---|
| 2401 | ritem = rdata_item_new(ic_address);
|
---|
| 2402 | address = rdata_address_new(ac_prop);
|
---|
| 2403 | addr_prop = rdata_addr_prop_new(apc_indexed);
|
---|
| 2404 | aprop_indexed = rdata_aprop_indexed_new();
|
---|
[94d484a] | 2405 | ritem->u.address = address;
|
---|
[d0febca] | 2406 | address->u.prop_a = addr_prop;
|
---|
| 2407 | addr_prop->u.indexed = aprop_indexed;
|
---|
| 2408 |
|
---|
| 2409 | if (base->ic != ic_address || base->u.address->ac != ac_var) {
|
---|
| 2410 | /* XXX Several other cases can occur. */
|
---|
| 2411 | printf("Unimplemented: Indexing object varclass via something "
|
---|
| 2412 | "which is not a simple variable reference.\n");
|
---|
| 2413 | exit(1);
|
---|
| 2414 | }
|
---|
[94d484a] | 2415 |
|
---|
[d0febca] | 2416 | /* Find indexer symbol. */
|
---|
| 2417 | obj_var = base->u.address->u.var_a->vref;
|
---|
| 2418 | assert(obj_var->vc == vc_object);
|
---|
| 2419 | indexer_ident = stree_ident_new();
|
---|
| 2420 | indexer_ident->sid = strtab_get_sid(INDEXER_IDENT);
|
---|
| 2421 | obj_csi = symbol_to_csi(obj_var->u.object_v->class_sym);
|
---|
| 2422 | assert(obj_csi != NULL);
|
---|
| 2423 | indexer_sym = symbol_search_csi(run->program, obj_csi, indexer_ident);
|
---|
| 2424 |
|
---|
[39e8406] | 2425 | if (indexer_sym == NULL) {
|
---|
| 2426 | printf("Error: Accessing object which does not have an "
|
---|
| 2427 | "indexer.\n");
|
---|
| 2428 | exit(1);
|
---|
| 2429 | }
|
---|
| 2430 |
|
---|
[d0febca] | 2431 | /* Construct delegate. */
|
---|
| 2432 | object_d = rdata_deleg_new();
|
---|
| 2433 | object_d->obj = obj_var;
|
---|
| 2434 | object_d->sym = indexer_sym;
|
---|
| 2435 | aprop_indexed->object_d = object_d;
|
---|
| 2436 |
|
---|
| 2437 | /* Copy list of argument values. */
|
---|
| 2438 | list_init(&aprop_indexed->args);
|
---|
| 2439 |
|
---|
| 2440 | node = list_first(args);
|
---|
| 2441 | while (node != NULL) {
|
---|
| 2442 | arg = list_node_data(node, rdata_item_t *);
|
---|
[051b3db8] | 2443 |
|
---|
| 2444 | /*
|
---|
| 2445 | * Clone argument so that original can
|
---|
| 2446 | * be freed.
|
---|
| 2447 | */
|
---|
| 2448 | assert(arg->ic == ic_value);
|
---|
| 2449 | arg_copy = rdata_item_new(ic_value);
|
---|
| 2450 | rdata_value_copy(arg->u.value, &arg_copy->u.value);
|
---|
| 2451 |
|
---|
| 2452 | list_append(&aprop_indexed->args, arg_copy);
|
---|
[d0febca] | 2453 | node = list_next(args, node);
|
---|
| 2454 | }
|
---|
[94d484a] | 2455 |
|
---|
| 2456 | *res = ritem;
|
---|
| 2457 | }
|
---|
| 2458 |
|
---|
[38aaacc2] | 2459 | /** Run index operation on string.
|
---|
| 2460 | *
|
---|
| 2461 | * @param run Runner object
|
---|
| 2462 | * @param index Index operation
|
---|
| 2463 | * @param base Evaluated base expression
|
---|
| 2464 | * @param args Evaluated indices (list of rdata_item_t)
|
---|
| 2465 | * @param res Place to store result
|
---|
| 2466 | */
|
---|
[d0febca] | 2467 | static void run_index_string(run_t *run, stree_index_t *index,
|
---|
| 2468 | rdata_item_t *base, list_t *args, rdata_item_t **res)
|
---|
| 2469 | {
|
---|
| 2470 | list_node_t *node;
|
---|
| 2471 | rdata_string_t *string;
|
---|
| 2472 | rdata_item_t *base_vi;
|
---|
| 2473 | rdata_item_t *arg;
|
---|
| 2474 |
|
---|
| 2475 | int i;
|
---|
| 2476 | int elem_index;
|
---|
| 2477 | int arg_val;
|
---|
[23de644] | 2478 | int rc1, rc2;
|
---|
[d0febca] | 2479 |
|
---|
| 2480 | rdata_value_t *value;
|
---|
| 2481 | rdata_var_t *cvar;
|
---|
| 2482 | rdata_item_t *ritem;
|
---|
| 2483 | int cval;
|
---|
| 2484 |
|
---|
| 2485 | #ifdef DEBUG_RUN_TRACE
|
---|
| 2486 | printf("Run string index operation.\n");
|
---|
| 2487 | #endif
|
---|
| 2488 | (void) run;
|
---|
| 2489 |
|
---|
| 2490 | run_cvt_value_item(run, base, &base_vi);
|
---|
[051b3db8] | 2491 | if (run_is_bo(run)) {
|
---|
| 2492 | *res = run_recovery_item(run);
|
---|
| 2493 | return;
|
---|
| 2494 | }
|
---|
| 2495 |
|
---|
[d0febca] | 2496 | assert(base_vi->u.value->var->vc == vc_string);
|
---|
[23de644] | 2497 | string = base_vi->u.value->var->u.string_v;
|
---|
[d0febca] | 2498 |
|
---|
| 2499 | /*
|
---|
| 2500 | * Linear index of the desired element. Elements are stored in
|
---|
| 2501 | * lexicographic order with the last index changing the fastest.
|
---|
| 2502 | */
|
---|
| 2503 | node = list_first(args);
|
---|
| 2504 | elem_index = 0;
|
---|
| 2505 |
|
---|
[0464967] | 2506 | assert(node != NULL);
|
---|
| 2507 |
|
---|
[d0febca] | 2508 | i = 0;
|
---|
[0464967] | 2509 | do {
|
---|
[d0febca] | 2510 | if (i >= 1) {
|
---|
| 2511 | printf("Error: Too many indices string.\n");
|
---|
| 2512 | exit(1);
|
---|
| 2513 | }
|
---|
| 2514 |
|
---|
| 2515 | arg = list_node_data(node, rdata_item_t *);
|
---|
| 2516 | assert(arg->ic == ic_value);
|
---|
| 2517 |
|
---|
| 2518 | if (arg->u.value->var->vc != vc_int) {
|
---|
| 2519 | printf("Error: String index is not an integer.\n");
|
---|
| 2520 | exit(1);
|
---|
| 2521 | }
|
---|
| 2522 |
|
---|
[23de644] | 2523 | rc1 = bigint_get_value_int(
|
---|
| 2524 | &arg->u.value->var->u.int_v->value,
|
---|
| 2525 | &arg_val);
|
---|
| 2526 |
|
---|
[d0febca] | 2527 | elem_index = arg_val;
|
---|
| 2528 |
|
---|
| 2529 | node = list_next(args, node);
|
---|
| 2530 | i += 1;
|
---|
[0464967] | 2531 | } while (node != NULL);
|
---|
[d0febca] | 2532 |
|
---|
| 2533 | if (i < 1) {
|
---|
| 2534 | printf("Error: Too few indices for string.\n");
|
---|
| 2535 | exit(1);
|
---|
| 2536 | }
|
---|
| 2537 |
|
---|
[23de644] | 2538 | if (rc1 == EOK)
|
---|
| 2539 | rc2 = os_str_get_char(string->value, elem_index, &cval);
|
---|
[0c8a420] | 2540 | else
|
---|
| 2541 | rc2 = EOK;
|
---|
[23de644] | 2542 |
|
---|
| 2543 | if (rc1 != EOK || rc2 != EOK) {
|
---|
[074444f] | 2544 | #ifdef DEBUG_RUN_TRACE
|
---|
[d0febca] | 2545 | printf("Error: String index (value: %d) is out of range.\n",
|
---|
| 2546 | arg_val);
|
---|
[074444f] | 2547 | #endif
|
---|
| 2548 | /* Raise Error.OutOfBounds */
|
---|
[051bc69a] | 2549 | run_raise_exc(run, run->program->builtin->error_outofbounds,
|
---|
| 2550 | index->expr->cspan);
|
---|
[074444f] | 2551 | *res = run_recovery_item(run);
|
---|
[051b3db8] | 2552 | goto cleanup;
|
---|
[d0febca] | 2553 | }
|
---|
| 2554 |
|
---|
| 2555 | /* Construct character value. */
|
---|
| 2556 | ritem = rdata_item_new(ic_value);
|
---|
| 2557 | value = rdata_value_new();
|
---|
| 2558 | ritem->u.value = value;
|
---|
| 2559 |
|
---|
[074444f] | 2560 | cvar = rdata_var_new(vc_char);
|
---|
| 2561 | cvar->u.char_v = rdata_char_new();
|
---|
| 2562 | bigint_init(&cvar->u.char_v->value, cval);
|
---|
[d0febca] | 2563 | value->var = cvar;
|
---|
| 2564 |
|
---|
| 2565 | *res = ritem;
|
---|
[051b3db8] | 2566 | cleanup:
|
---|
| 2567 | rdata_item_destroy(base_vi);
|
---|
[d0febca] | 2568 | }
|
---|
| 2569 |
|
---|
[38aaacc2] | 2570 | /** Run assignment.
|
---|
| 2571 | *
|
---|
| 2572 | * Executes an assignment. @c NULL is always stored to @a res because
|
---|
| 2573 | * an assignment does not have a value.
|
---|
| 2574 | *
|
---|
| 2575 | * @param run Runner object
|
---|
| 2576 | * @param assign Assignment expression
|
---|
| 2577 | * @param res Place to store result
|
---|
| 2578 | */
|
---|
[09ababb7] | 2579 | static void run_assign(run_t *run, stree_assign_t *assign, rdata_item_t **res)
|
---|
| 2580 | {
|
---|
| 2581 | rdata_item_t *rdest_i, *rsrc_i;
|
---|
| 2582 | rdata_item_t *rsrc_vi;
|
---|
| 2583 |
|
---|
| 2584 | #ifdef DEBUG_RUN_TRACE
|
---|
| 2585 | printf("Run assign operation.\n");
|
---|
| 2586 | #endif
|
---|
[051b3db8] | 2587 | rdest_i = NULL;
|
---|
| 2588 | rsrc_i = NULL;
|
---|
| 2589 | rsrc_vi = NULL;
|
---|
| 2590 |
|
---|
[09ababb7] | 2591 | run_expr(run, assign->dest, &rdest_i);
|
---|
[23de644] | 2592 | if (run_is_bo(run)) {
|
---|
[051b3db8] | 2593 | *res = run_recovery_item(run);
|
---|
| 2594 | goto cleanup;
|
---|
[23de644] | 2595 | }
|
---|
| 2596 |
|
---|
[09ababb7] | 2597 | run_expr(run, assign->src, &rsrc_i);
|
---|
[23de644] | 2598 | if (run_is_bo(run)) {
|
---|
[051b3db8] | 2599 | *res = run_recovery_item(run);
|
---|
| 2600 | goto cleanup;
|
---|
[23de644] | 2601 | }
|
---|
[09ababb7] | 2602 |
|
---|
[d0febca] | 2603 | run_cvt_value_item(run, rsrc_i, &rsrc_vi);
|
---|
[051b3db8] | 2604 | if (run_is_bo(run)) {
|
---|
| 2605 | *res = run_recovery_item(run);
|
---|
| 2606 | goto cleanup;
|
---|
| 2607 | }
|
---|
| 2608 |
|
---|
[d0febca] | 2609 | assert(rsrc_vi->ic == ic_value);
|
---|
[09ababb7] | 2610 |
|
---|
| 2611 | if (rdest_i->ic != ic_address) {
|
---|
| 2612 | printf("Error: Address expression required on left side of "
|
---|
| 2613 | "assignment operator.\n");
|
---|
| 2614 | exit(1);
|
---|
| 2615 | }
|
---|
| 2616 |
|
---|
[d0febca] | 2617 | run_address_write(run, rdest_i->u.address, rsrc_vi->u.value);
|
---|
[09ababb7] | 2618 |
|
---|
| 2619 | *res = NULL;
|
---|
[051b3db8] | 2620 | cleanup:
|
---|
| 2621 | if (rdest_i != NULL)
|
---|
| 2622 | rdata_item_destroy(rdest_i);
|
---|
| 2623 | if (rsrc_i != NULL)
|
---|
| 2624 | rdata_item_destroy(rsrc_i);
|
---|
| 2625 | if (rsrc_vi != NULL)
|
---|
| 2626 | rdata_item_destroy(rsrc_vi);
|
---|
[09ababb7] | 2627 | }
|
---|
| 2628 |
|
---|
[38aaacc2] | 2629 | /** Execute @c as conversion.
|
---|
| 2630 | *
|
---|
| 2631 | * @param run Runner object
|
---|
| 2632 | * @param as_op @c as conversion expression
|
---|
| 2633 | * @param res Place to store result
|
---|
| 2634 | */
|
---|
[37f527b] | 2635 | static void run_as(run_t *run, stree_as_t *as_op, rdata_item_t **res)
|
---|
| 2636 | {
|
---|
| 2637 | rdata_item_t *rarg_i;
|
---|
| 2638 | rdata_item_t *rarg_vi;
|
---|
| 2639 | rdata_item_t *rarg_di;
|
---|
| 2640 | rdata_var_t *arg_vref;
|
---|
| 2641 | tdata_item_t *dtype;
|
---|
| 2642 | run_proc_ar_t *proc_ar;
|
---|
| 2643 |
|
---|
| 2644 | stree_symbol_t *obj_csi_sym;
|
---|
| 2645 | stree_csi_t *obj_csi;
|
---|
| 2646 |
|
---|
| 2647 | #ifdef DEBUG_RUN_TRACE
|
---|
| 2648 | printf("Run @c as conversion operation.\n");
|
---|
| 2649 | #endif
|
---|
| 2650 | run_expr(run, as_op->arg, &rarg_i);
|
---|
[23de644] | 2651 | if (run_is_bo(run)) {
|
---|
[051b3db8] | 2652 | *res = run_recovery_item(run);
|
---|
[23de644] | 2653 | return;
|
---|
| 2654 | }
|
---|
[37f527b] | 2655 |
|
---|
| 2656 | /*
|
---|
| 2657 | * This should always be a reference if the argument is indeed
|
---|
| 2658 | * a class instance.
|
---|
| 2659 | */
|
---|
| 2660 | assert(run_item_get_vc(run, rarg_i) == vc_ref);
|
---|
| 2661 | run_cvt_value_item(run, rarg_i, &rarg_vi);
|
---|
[051b3db8] | 2662 | rdata_item_destroy(rarg_i);
|
---|
| 2663 |
|
---|
| 2664 | if (run_is_bo(run)) {
|
---|
| 2665 | *res = run_recovery_item(run);
|
---|
| 2666 | return;
|
---|
| 2667 | }
|
---|
| 2668 |
|
---|
[37f527b] | 2669 | assert(rarg_vi->ic == ic_value);
|
---|
| 2670 |
|
---|
| 2671 | if (rarg_vi->u.value->var->u.ref_v->vref == NULL) {
|
---|
| 2672 | /* Nil reference is always okay. */
|
---|
| 2673 | *res = rarg_vi;
|
---|
| 2674 | return;
|
---|
| 2675 | }
|
---|
| 2676 |
|
---|
[051bc69a] | 2677 | run_dereference(run, rarg_vi, NULL, &rarg_di);
|
---|
[37f527b] | 2678 |
|
---|
| 2679 | /* Now we should have a variable address. */
|
---|
| 2680 | assert(rarg_di->ic == ic_address);
|
---|
| 2681 | assert(rarg_di->u.address->ac == ac_var);
|
---|
| 2682 |
|
---|
| 2683 | arg_vref = rarg_di->u.address->u.var_a->vref;
|
---|
| 2684 |
|
---|
| 2685 | proc_ar = run_get_current_proc_ar(run);
|
---|
| 2686 | /* XXX Memoize to avoid recomputing. */
|
---|
| 2687 | run_texpr(run->program, proc_ar->proc->outer_symbol->outer_csi,
|
---|
| 2688 | as_op->dtype, &dtype);
|
---|
| 2689 |
|
---|
| 2690 | assert(arg_vref->vc == vc_object);
|
---|
| 2691 | obj_csi_sym = arg_vref->u.object_v->class_sym;
|
---|
| 2692 | obj_csi = symbol_to_csi(obj_csi_sym);
|
---|
| 2693 | assert(obj_csi != NULL);
|
---|
| 2694 |
|
---|
| 2695 | if (tdata_is_csi_derived_from_ti(obj_csi, dtype) != b_true) {
|
---|
| 2696 | printf("Error: Run-time type conversion error. Object is "
|
---|
| 2697 | "of type '");
|
---|
| 2698 | symbol_print_fqn(obj_csi_sym);
|
---|
| 2699 | printf("' which is not derived from '");
|
---|
| 2700 | tdata_item_print(dtype);
|
---|
| 2701 | printf("'.\n");
|
---|
| 2702 | exit(1);
|
---|
| 2703 | }
|
---|
| 2704 |
|
---|
[051b3db8] | 2705 | /* The dereferenced item is not used anymore. */
|
---|
| 2706 | rdata_item_destroy(rarg_di);
|
---|
| 2707 |
|
---|
[37f527b] | 2708 | *res = rarg_vi;
|
---|
| 2709 | }
|
---|
| 2710 |
|
---|
[38aaacc2] | 2711 | /** Execute boxing operation.
|
---|
| 2712 | *
|
---|
| 2713 | * XXX We can scrap this special operation once we have constructors.
|
---|
| 2714 | *
|
---|
| 2715 | * @param run Runner object
|
---|
| 2716 | * @param box Boxing operation
|
---|
| 2717 | * @param res Place to store result
|
---|
| 2718 | */
|
---|
| 2719 | static void run_box(run_t *run, stree_box_t *box, rdata_item_t **res)
|
---|
| 2720 | {
|
---|
| 2721 | rdata_item_t *rarg_i;
|
---|
| 2722 | rdata_item_t *rarg_vi;
|
---|
| 2723 |
|
---|
| 2724 | stree_symbol_t *csi_sym;
|
---|
| 2725 | stree_csi_t *csi;
|
---|
| 2726 | builtin_t *bi;
|
---|
| 2727 | rdata_var_t *var;
|
---|
| 2728 | rdata_object_t *object;
|
---|
| 2729 |
|
---|
| 2730 | sid_t mbr_name_sid;
|
---|
| 2731 | rdata_var_t *mbr_var;
|
---|
| 2732 |
|
---|
| 2733 | #ifdef DEBUG_RUN_TRACE
|
---|
| 2734 | printf("Run boxing operation.\n");
|
---|
| 2735 | #endif
|
---|
| 2736 | run_expr(run, box->arg, &rarg_i);
|
---|
| 2737 | if (run_is_bo(run)) {
|
---|
[051b3db8] | 2738 | *res = run_recovery_item(run);
|
---|
[38aaacc2] | 2739 | return;
|
---|
| 2740 | }
|
---|
| 2741 |
|
---|
| 2742 | run_cvt_value_item(run, rarg_i, &rarg_vi);
|
---|
[051b3db8] | 2743 | rdata_item_destroy(rarg_i);
|
---|
| 2744 | if (run_is_bo(run)) {
|
---|
| 2745 | *res = run_recovery_item(run);
|
---|
| 2746 | return;
|
---|
| 2747 | }
|
---|
| 2748 |
|
---|
[38aaacc2] | 2749 | assert(rarg_vi->ic == ic_value);
|
---|
| 2750 |
|
---|
| 2751 | bi = run->program->builtin;
|
---|
| 2752 |
|
---|
| 2753 | /* Just to keep the compiler happy. */
|
---|
| 2754 | csi_sym = NULL;
|
---|
| 2755 |
|
---|
| 2756 | switch (rarg_vi->u.value->var->vc) {
|
---|
| 2757 | case vc_bool: csi_sym = bi->boxed_bool; break;
|
---|
| 2758 | case vc_char: csi_sym = bi->boxed_char; break;
|
---|
| 2759 | case vc_int: csi_sym = bi->boxed_int; break;
|
---|
| 2760 | case vc_string: csi_sym = bi->boxed_string; break;
|
---|
| 2761 |
|
---|
| 2762 | case vc_ref:
|
---|
| 2763 | case vc_deleg:
|
---|
[051bc69a] | 2764 | case vc_enum:
|
---|
[38aaacc2] | 2765 | case vc_array:
|
---|
| 2766 | case vc_object:
|
---|
| 2767 | case vc_resource:
|
---|
[051bc69a] | 2768 | case vc_symbol:
|
---|
[38aaacc2] | 2769 | assert(b_false);
|
---|
| 2770 | }
|
---|
| 2771 |
|
---|
| 2772 | csi = symbol_to_csi(csi_sym);
|
---|
| 2773 | assert(csi != NULL);
|
---|
| 2774 |
|
---|
| 2775 | /* Construct object of the relevant boxed type. */
|
---|
[c5cb943d] | 2776 | run_new_csi_inst_ref(run, csi, sn_nonstatic, res);
|
---|
[38aaacc2] | 2777 |
|
---|
| 2778 | /* Set the 'Value' field */
|
---|
| 2779 |
|
---|
| 2780 | assert((*res)->ic == ic_value);
|
---|
| 2781 | assert((*res)->u.value->var->vc == vc_ref);
|
---|
| 2782 | var = (*res)->u.value->var->u.ref_v->vref;
|
---|
| 2783 | assert(var->vc == vc_object);
|
---|
| 2784 | object = var->u.object_v;
|
---|
| 2785 |
|
---|
| 2786 | mbr_name_sid = strtab_get_sid("Value");
|
---|
| 2787 | mbr_var = intmap_get(&object->fields, mbr_name_sid);
|
---|
| 2788 | assert(mbr_var != NULL);
|
---|
| 2789 |
|
---|
| 2790 | rdata_var_write(mbr_var, rarg_vi->u.value);
|
---|
[051b3db8] | 2791 | rdata_item_destroy(rarg_vi);
|
---|
[38aaacc2] | 2792 | }
|
---|
| 2793 |
|
---|
[c5cb943d] | 2794 | /** Create new CSI instance and return reference to it.
|
---|
| 2795 | *
|
---|
| 2796 | * Create a new object, instance of @a csi.
|
---|
| 2797 | * XXX This does not work with generics as @a csi cannot specify a generic
|
---|
| 2798 | * type.
|
---|
| 2799 | *
|
---|
| 2800 | * Initialize the fields with default values of their types, but do not
|
---|
| 2801 | * run any constructor.
|
---|
| 2802 | *
|
---|
| 2803 | * If @a sn is @c sn_nonstatic a regular object is created, containing all
|
---|
| 2804 | * non-static member variables. If @a sn is @c sn_static a static object
|
---|
| 2805 | * is created, containing all static member variables.
|
---|
| 2806 | *
|
---|
| 2807 | * @param run Runner object
|
---|
| 2808 | * @param csi CSI to create instance of
|
---|
| 2809 | * @param sn @c sn_static to create a static (class) object,
|
---|
| 2810 | * @c sn_nonstatic to create a regular object
|
---|
| 2811 | * @param res Place to store result
|
---|
| 2812 | */
|
---|
| 2813 | void run_new_csi_inst_ref(run_t *run, stree_csi_t *csi, statns_t sn,
|
---|
| 2814 | rdata_item_t **res)
|
---|
| 2815 | {
|
---|
| 2816 | rdata_var_t *obj_var;
|
---|
| 2817 |
|
---|
| 2818 | /* Create object. */
|
---|
| 2819 | run_new_csi_inst(run, csi, sn, &obj_var);
|
---|
| 2820 |
|
---|
| 2821 | /* Create reference to the new object. */
|
---|
| 2822 | run_reference(run, obj_var, res);
|
---|
| 2823 | }
|
---|
| 2824 |
|
---|
[38aaacc2] | 2825 | /** Create new CSI instance.
|
---|
| 2826 | *
|
---|
| 2827 | * Create a new object, instance of @a csi.
|
---|
| 2828 | * XXX This does not work with generics as @a csi cannot specify a generic
|
---|
| 2829 | * type.
|
---|
| 2830 | *
|
---|
| 2831 | * Initialize the fields with default values of their types, but do not
|
---|
| 2832 | * run any constructor.
|
---|
| 2833 | *
|
---|
[c5cb943d] | 2834 | * If @a sn is @c sn_nonstatic a regular object is created, containing all
|
---|
| 2835 | * non-static member variables. If @a sn is @c sn_static a static object
|
---|
| 2836 | * is created, containing all static member variables.
|
---|
| 2837 | *
|
---|
[38aaacc2] | 2838 | * @param run Runner object
|
---|
[c5cb943d] | 2839 | * @param csi CSI to create instance of
|
---|
| 2840 | * @param sn @c sn_static to create a static (class) object,
|
---|
| 2841 | * @c sn_nonstatic to create a regular object
|
---|
[38aaacc2] | 2842 | * @param res Place to store result
|
---|
| 2843 | */
|
---|
[c5cb943d] | 2844 | void run_new_csi_inst(run_t *run, stree_csi_t *csi, statns_t sn,
|
---|
| 2845 | rdata_var_t **res)
|
---|
[23de644] | 2846 | {
|
---|
| 2847 | rdata_object_t *obj;
|
---|
| 2848 | rdata_var_t *obj_var;
|
---|
| 2849 |
|
---|
| 2850 | stree_symbol_t *csi_sym;
|
---|
| 2851 | stree_csimbr_t *csimbr;
|
---|
[c5cb943d] | 2852 | stree_var_t *var;
|
---|
| 2853 | statns_t var_sn;
|
---|
[23de644] | 2854 |
|
---|
| 2855 | rdata_var_t *mbr_var;
|
---|
| 2856 | list_node_t *node;
|
---|
[38aaacc2] | 2857 | tdata_item_t *field_ti;
|
---|
[23de644] | 2858 |
|
---|
| 2859 | csi_sym = csi_to_symbol(csi);
|
---|
| 2860 |
|
---|
| 2861 | #ifdef DEBUG_RUN_TRACE
|
---|
| 2862 | printf("Create new instance of CSI '");
|
---|
| 2863 | symbol_print_fqn(csi_sym);
|
---|
| 2864 | printf("'.\n");
|
---|
| 2865 | #endif
|
---|
| 2866 |
|
---|
| 2867 | /* Create the object. */
|
---|
| 2868 | obj = rdata_object_new();
|
---|
| 2869 | obj->class_sym = csi_sym;
|
---|
[c5cb943d] | 2870 | obj->static_obj = sn;
|
---|
[23de644] | 2871 | intmap_init(&obj->fields);
|
---|
| 2872 |
|
---|
| 2873 | obj_var = rdata_var_new(vc_object);
|
---|
| 2874 | obj_var->u.object_v = obj;
|
---|
| 2875 |
|
---|
[c5cb943d] | 2876 | /* For this CSI and all base CSIs */
|
---|
[38aaacc2] | 2877 | while (csi != NULL) {
|
---|
[c5cb943d] | 2878 |
|
---|
| 2879 | /* For all members */
|
---|
[38aaacc2] | 2880 | node = list_first(&csi->members);
|
---|
| 2881 | while (node != NULL) {
|
---|
| 2882 | csimbr = list_node_data(node, stree_csimbr_t *);
|
---|
[c5cb943d] | 2883 |
|
---|
| 2884 | /* Is it a member variable? */
|
---|
[38aaacc2] | 2885 | if (csimbr->cc == csimbr_var) {
|
---|
[c5cb943d] | 2886 | var = csimbr->u.var;
|
---|
| 2887 |
|
---|
| 2888 | /* Is it static/nonstatic? */
|
---|
| 2889 | var_sn = stree_symbol_has_attr(
|
---|
[05b59393] | 2890 | var_to_symbol(var), sac_static) ? sn_static : sn_nonstatic;
|
---|
[c5cb943d] | 2891 | if (var_sn == sn) {
|
---|
| 2892 | /* Compute field type. XXX Memoize. */
|
---|
| 2893 | run_texpr(run->program, csi, var->type,
|
---|
| 2894 | &field_ti);
|
---|
| 2895 |
|
---|
| 2896 | /* Create and initialize field. */
|
---|
| 2897 | run_var_new(run, field_ti, &mbr_var);
|
---|
| 2898 |
|
---|
| 2899 | /* Add to field map. */
|
---|
| 2900 | intmap_set(&obj->fields, var->name->sid,
|
---|
| 2901 | mbr_var);
|
---|
| 2902 | }
|
---|
[38aaacc2] | 2903 | }
|
---|
| 2904 |
|
---|
| 2905 | node = list_next(&csi->members, node);
|
---|
[23de644] | 2906 | }
|
---|
| 2907 |
|
---|
[38aaacc2] | 2908 | /* Continue with base CSI */
|
---|
| 2909 | csi = csi->base_csi;
|
---|
[23de644] | 2910 | }
|
---|
| 2911 |
|
---|
[c5cb943d] | 2912 | *res = obj_var;
|
---|
[23de644] | 2913 | }
|
---|
| 2914 |
|
---|
[051bc69a] | 2915 | /** Run constructor on an object.
|
---|
| 2916 | *
|
---|
| 2917 | * @param run Runner object
|
---|
| 2918 | * @param obj Object to run constructor on
|
---|
| 2919 | * @param arg_vals Argument values (list of rdata_item_t)
|
---|
| 2920 | */
|
---|
| 2921 | static void run_object_ctor(run_t *run, rdata_var_t *obj, list_t *arg_vals)
|
---|
| 2922 | {
|
---|
| 2923 | stree_ident_t *ctor_ident;
|
---|
| 2924 | stree_symbol_t *csi_sym;
|
---|
| 2925 | stree_csi_t *csi;
|
---|
| 2926 | stree_symbol_t *ctor_sym;
|
---|
| 2927 | stree_ctor_t *ctor;
|
---|
| 2928 | run_proc_ar_t *proc_ar;
|
---|
| 2929 | rdata_item_t *res;
|
---|
| 2930 |
|
---|
| 2931 | csi_sym = obj->u.object_v->class_sym;
|
---|
| 2932 | csi = symbol_to_csi(csi_sym);
|
---|
| 2933 | assert(csi != NULL);
|
---|
| 2934 |
|
---|
| 2935 | #ifdef DEBUG_RUN_TRACE
|
---|
| 2936 | printf("Run object constructor from CSI '");
|
---|
| 2937 | symbol_print_fqn(csi_sym);
|
---|
| 2938 | printf("'.\n");
|
---|
| 2939 | #endif
|
---|
| 2940 | ctor_ident = stree_ident_new();
|
---|
| 2941 | ctor_ident->sid = strtab_get_sid(CTOR_IDENT);
|
---|
| 2942 |
|
---|
| 2943 | /* Find constructor. */
|
---|
| 2944 | ctor_sym = symbol_search_csi_no_base(run->program, csi, ctor_ident);
|
---|
| 2945 | if (ctor_sym == NULL) {
|
---|
| 2946 | #ifdef DEBUG_RUN_TRACE
|
---|
| 2947 | printf("No constructor found.\n");
|
---|
| 2948 | #endif
|
---|
| 2949 | return;
|
---|
| 2950 | }
|
---|
| 2951 |
|
---|
| 2952 | ctor = symbol_to_ctor(ctor_sym);
|
---|
| 2953 | assert(ctor != NULL);
|
---|
| 2954 |
|
---|
| 2955 | /* Create procedure activation record. */
|
---|
| 2956 | run_proc_ar_create(run, obj, ctor->proc, &proc_ar);
|
---|
| 2957 |
|
---|
| 2958 | /* Fill in argument values. */
|
---|
| 2959 | run_proc_ar_set_args(run, proc_ar, arg_vals);
|
---|
| 2960 |
|
---|
| 2961 | /* Run the procedure. */
|
---|
| 2962 | run_proc(run, proc_ar, &res);
|
---|
| 2963 |
|
---|
| 2964 | /* Constructor does not return a value. */
|
---|
| 2965 | assert(res == NULL);
|
---|
| 2966 |
|
---|
[051b3db8] | 2967 | /* Destroy procedure activation record. */
|
---|
| 2968 | run_proc_ar_destroy(run, proc_ar);
|
---|
| 2969 |
|
---|
[051bc69a] | 2970 | #ifdef DEBUG_RUN_TRACE
|
---|
| 2971 | printf("Returned from constructor..\n");
|
---|
| 2972 | #endif
|
---|
| 2973 | }
|
---|
| 2974 |
|
---|
[09ababb7] | 2975 | /** Return boolean value of an item.
|
---|
| 2976 | *
|
---|
[38aaacc2] | 2977 | * Try to interpret @a item as a boolean value. If it is not a boolean
|
---|
| 2978 | * value, generate an error.
|
---|
| 2979 | *
|
---|
| 2980 | * @param run Runner object
|
---|
| 2981 | * @param item Input item
|
---|
| 2982 | * @return Resulting boolean value
|
---|
[09ababb7] | 2983 | */
|
---|
| 2984 | bool_t run_item_boolean_value(run_t *run, rdata_item_t *item)
|
---|
| 2985 | {
|
---|
| 2986 | rdata_item_t *vitem;
|
---|
| 2987 | rdata_var_t *var;
|
---|
[051b3db8] | 2988 | bool_t res;
|
---|
[09ababb7] | 2989 |
|
---|
[fa36f29] | 2990 | (void) run;
|
---|
[d0febca] | 2991 | run_cvt_value_item(run, item, &vitem);
|
---|
[051b3db8] | 2992 | if (run_is_bo(run))
|
---|
| 2993 | return b_true;
|
---|
[09ababb7] | 2994 |
|
---|
| 2995 | assert(vitem->ic == ic_value);
|
---|
| 2996 | var = vitem->u.value->var;
|
---|
| 2997 |
|
---|
[074444f] | 2998 | assert(var->vc == vc_bool);
|
---|
[051b3db8] | 2999 | res = var->u.bool_v->value;
|
---|
| 3000 |
|
---|
| 3001 | /* Free value item */
|
---|
| 3002 | rdata_item_destroy(vitem);
|
---|
| 3003 | return res;
|
---|
[09ababb7] | 3004 | }
|
---|