void jvmcodegen::gen_variable_decl(const ast::node_ptr &node) { auto var = ast::to_variable_decl(node); auto& sym = m_stack.top()->get(var->name); // escopo global? if (m_stack.top()->id <= 0) { } else { sym.local = get_next_local(); switch (sym.c_type()) { case types::integer: if (var->value->is_valid()) { gen_node(var->value); } else { m_out << fmt::sprintf("iconst_0\n"); } m_out << fmt::sprintf("istore %d\n", sym.local); break; case types::string: if (var->value->is_valid()) { gen_node(var->value); } else { m_out << fmt::sprintf("ldc ""\n"); } m_out << fmt::sprintf("astore %d\n", sym.local); break; default: throw jvmcodegen_error("Apenas inteiros sao suportados pelo gerador"); } } }
void jvmcodegen::gen_argument(const ast::node_ptr &node) { auto arg = ast::to_argument(node); auto& sym = m_stack.top()->get(arg->name); sym.local = get_next_local(); }
void append_var_info(compiler_wrapper *cw, char *ch, char load, int lineno) { char needs_close = cw->repl; char already_defined = 0; arraylist list = cw->used_names; int i; for(i = 0; i < list.count; i++) { name_wrapper *w = arr_get(&cw->used_names, i); if(strcmp(w->name, ch)) continue; if(w->owner != cw) { switch_to_close(w->owner, ch, w->idx); needs_close = 1; } else { needs_close = is_close(cw, w->idx); already_defined = 1; } } if(!needs_close && !already_defined && load) needs_close = 1; if(needs_close) { lky_instruction istr = load ? LI_LOAD_CLOSE : LI_SAVE_CLOSE; char *nsid = malloc(strlen(ch) + 1); strcpy(nsid, ch); int i = find_prev_name(cw, nsid); if(i < 0) { i = (int)cw->rnames.count; arr_append(&cw->rnames, nsid); } append_op(cw, istr, lineno); unsigned char buf[4]; int_to_byte_array(buf, i); append_op(cw, buf[0], lineno); append_op(cw, buf[1], lineno); append_op(cw, buf[2], lineno); append_op(cw, buf[3], lineno); return; } lky_instruction istr = load ? LI_LOAD_LOCAL : LI_SAVE_LOCAL; hm_error_t err; int idx = 0; lky_object_builtin *o = hm_get(&cw->saved_locals, ch, &err); if(err == HM_KEY_NOT_FOUND) { idx = get_next_local(cw); lky_object *obj = lobjb_build_int(idx); pool_add(&ast_memory_pool, obj); hm_put(&cw->saved_locals, ch, obj); } else idx = OBJ_NUM_UNWRAP(o); append_op(cw, istr, lineno); unsigned char buf[4]; int_to_byte_array(buf, idx); append_op(cw, buf[0], lineno); append_op(cw, buf[1], lineno); append_op(cw, buf[2], lineno); append_op(cw, buf[3], lineno); name_wrapper *wrap = malloc(sizeof(name_wrapper)); pool_add(&ast_memory_pool, wrap); wrap->idx = cw->rops.count - 5; wrap->name = ch; wrap->owner = cw; arr_append(&cw->used_names, wrap); }