void spn_dbg_set_filename(SpnHashMap *debug_info, const char *fname) { if (debug_info) { SpnValue str = makestring(fname); spn_hashmap_set_strkey(debug_info, "file", &str); spn_value_release(&str); } }
SpnHashMap *spn_dbg_new(void) { SpnHashMap *debug_info = spn_hashmap_new(); /* insns: maps bytecode address to source location * vars: maps address and variable name to register number */ SpnValue insns = makearray(); SpnValue vars = makearray(); spn_hashmap_set_strkey(debug_info, "insns", &insns); spn_hashmap_set_strkey(debug_info, "vars", &vars); spn_value_release(&insns); spn_value_release(&vars); return debug_info; }
void spn_array_set(SpnArray *arr, size_t index, const SpnValue *val) { if (index >= arr->count) { unsigned long ulindex = index, ulcount = arr->count; spn_die("array index %lu is too high (size = %lu)\n", ulindex, ulcount); } spn_value_retain(val); spn_value_release(&arr->vector[index]); arr->vector[index] = *val; }
static void free_array(void *obj) { SpnArray *arr = obj; size_t i; for (i = 0; i < arr->count; i++) { spn_value_release(&arr->vector[i]); } free(arr->vector); }
void spn_array_remove(SpnArray *arr, size_t index) { size_t i; if (index >= arr->count) { unsigned long ulindex = index, ulcount = arr->count; spn_die("array index %lu is too high (size = %lu)\n", ulindex, ulcount); } spn_value_release(&arr->vector[index]); arr->count--; for (i = index; i < arr->count; i++) { arr->vector[i] = arr->vector[i + 1]; } }
void spn_ast_free(SpnAST *ast) { if (ast == NULL) { return; } spn_value_release(&ast->value); if (ast->name != NULL) { spn_object_release(ast->name); } spn_ast_free(ast->left); spn_ast_free(ast->right); free(ast); }
void spn_dbg_emit_source_location( SpnHashMap *debug_info, size_t begin, size_t end, SpnHashMap *ast, int regno ) { SpnValue vinsns; SpnArray *insns; SpnValue vexpr; SpnValue line, column; SpnValue vbegin, vend; SpnValue vregno; SpnHashMap *expr; /* if we are not asked to emit debug info, give up */ if (debug_info == NULL) { return; } vinsns = spn_hashmap_get_strkey(debug_info, "insns"); insns = arrayvalue(&vinsns); vexpr = makehashmap(); expr = hashmapvalue(&vexpr); line = spn_hashmap_get_strkey(ast, "line"); column = spn_hashmap_get_strkey(ast, "column"); vbegin = makeint(begin); vend = makeint(end); vregno = makeint(regno); spn_hashmap_set_strkey(expr, "line", &line); spn_hashmap_set_strkey(expr, "column", &column); spn_hashmap_set_strkey(expr, "begin", &vbegin); spn_hashmap_set_strkey(expr, "end", &vend); spn_hashmap_set_strkey(expr, "register", &vregno); spn_array_push(insns, &vexpr); spn_value_release(&vexpr); }