void interpreter::get_running_call_stack_frame_info(int frame, const char * & stackinfo, const char * & func, const char * & file, int & line) const { stackinfo = ""; func = ""; file = ""; line = 0; if (!m_fb) { return; } m_fk->rn.cur_runinginfo.clear(); int deps = 0; int ip = m_ip; int bp = m_bp; const func_binary * fb = m_fb; while (!BP_END(bp)) { if (deps >= frame) { func = fb ? FUNC_BINARY_NAME(*fb) : ""; file = fb ? FUNC_BINARY_FILENAME(*fb) : ""; line = fb ? GET_CMD_LINENO(*fb, ip) : 0; m_fk->rn.cur_runinginfo += "#"; m_fk->rn.cur_runinginfo += fkitoa(deps); m_fk->rn.cur_runinginfo += " "; m_fk->rn.cur_runinginfo += func; m_fk->rn.cur_runinginfo += " at "; m_fk->rn.cur_runinginfo += file; m_fk->rn.cur_runinginfo += ":"; m_fk->rn.cur_runinginfo += fkitoa(line); m_fk->rn.cur_runinginfo += "\n"; stackinfo = m_fk->rn.cur_runinginfo.c_str(); return; } BP_GET_FB(bp, fb); BP_GET_IP(bp, ip); int callbp = 0; BP_GET_BP(bp, callbp); bp = callbp; if (BP_END(bp)) { break; } deps++; } }
void interpreter::endfuncprofile() { if (m_fk->pf.isopen()) { m_fk->pf.add_func_sample(FUNC_BINARY_NAME(*m_cur_stack->m_fb), fkgetmstick() - m_cur_stack->m_calltime); } }
bool assembler::compile(binary * bin) { FKLOG("[assembler] compile binary %p", bin); #ifndef FK64 // 32位目前不支持 return true; #endif if (!m_isopen) { return true; } for (const fkhashmap<variant, funcunion>::ele * p = m_fk->fm.m_shh.first(); p != 0; p = m_fk->fm.m_shh.next()) { const funcunion & f = *p->t; if (f.havefb && FUNC_BINARY_FRESH(f.fb)) { const func_binary & fb = f.fb; if (FUNC_BINARY_BACKUP(fb)) { const func_binary & bkfb = *FUNC_BINARY_BACKUP(fb); if (!compile_func(bkfb)) { FKERR("[assembler] compile compile_func %s fail", FUNC_BINARY_NAME(bkfb)); return false; } FUNC_BINARY_FRESH(bkfb)--; } else { if (!compile_func(fb)) { FKERR("[assembler] compile compile_func %s fail", FUNC_BINARY_NAME(fb)); return false; } FUNC_BINARY_FRESH(fb)--; } } } FKLOG("[assembler] compile binary %d ok \n%s", bin, m_native->dump().c_str()); return true; }
void assembler::compile_seterror(const func_binary & fb, command cmd, efkerror err, const char *fmt, ...) { char errorstr[512]; va_list ap; va_start(ap, fmt); vsnprintf(errorstr, sizeof(errorstr) - 1, fmt, ap); va_end(ap); errorstr[sizeof(errorstr) - 1] = 0; seterror(m_fk, err, FUNC_BINARY_FILENAME(fb), FUNC_BINARY_LINENO(fb, m_pos), FUNC_BINARY_NAME(fb), "assembler %llu, %s", cmd, errorstr); }
bool assembler::compile_func(const func_binary & fb) { FKLOG("[assembler] compile_func func_binary %p", &fb); clear(); asmgen asg(m_fk); asg.start_func(); int stacksize = (fb.m_maxstack + fb.m_const_list_num + MAX_ASSEMBLER_CONTAINER_NUM + 1) * variant_size; FKLOG("[assembler] compile_func stack size %d", stacksize); asg.alloc_stack(stacksize); asg.copy_param(FUNC_BINARY_PARAMNUM(fb)); asg.copy_const(fb.m_const_list, fb.m_const_list_num, fb.m_maxstack); // loop m_pos = 0; while (m_pos < (int)fb.m_size) { m_posmap[m_pos] = asg.size(); FKLOG("posmap %d %d", m_pos, asg.size()); if (!compile_next(asg, fb)) { FKERR("[assembler] compile_func compile_next %d fail", m_pos); return false; } } // end m_posmap[m_pos] = asg.size(); // link jmp pos for (caremap::iterator it = m_caremap.begin(); it != m_caremap.end(); it++) { int jumpposoff = it->first; int bytecodepos = it->second; if (m_posmap.find(bytecodepos) != m_posmap.end()) { int pos = m_posmap[bytecodepos]; asg.set_int(jumpposoff, pos - (jumpposoff + sizeof(int))); FKLOG("loop set %d -> %d %d", jumpposoff, pos - (jumpposoff + sizeof(int)), pos); } else { assert(0); } } asg.stop_func(); func_native nt; FUNC_NATIVE_INI(nt); asg.output(FUNC_BINARY_FILENAME(fb), FUNC_BINARY_PACKAGENAME(fb), FUNC_BINARY_NAME(fb), &nt); variant fv = m_fk->sh.allocsysstr(FUNC_BINARY_NAME(fb)); m_native->add_func(fv, nt); String str = asg.source(); FKLOG("[assembler] compile_func binary %p ok\n%s", &fb, str.c_str()); return true; }
const char * interpreter::get_running_call_stack() const { if (!m_fb) { return ""; } m_fk->rn.cur_runinginfo.clear(); int deps = 0; int ip = m_ip; int bp = m_bp; const func_binary * fb = m_fb; while (!BP_END(bp)) { m_fk->rn.cur_runinginfo += "#"; m_fk->rn.cur_runinginfo += fkitoa(deps); m_fk->rn.cur_runinginfo += " "; m_fk->rn.cur_runinginfo += fb ? FUNC_BINARY_NAME(*fb) : ""; m_fk->rn.cur_runinginfo += " at "; m_fk->rn.cur_runinginfo += fb ? FUNC_BINARY_FILENAME(*fb) : ""; m_fk->rn.cur_runinginfo += ":"; m_fk->rn.cur_runinginfo += fb ? fkitoa(GET_CMD_LINENO(*fb, ip)) : 0; m_fk->rn.cur_runinginfo += "\n"; for (int j = 0; fb && j < FUNC_BINARY_MAX_STACK(*fb); j++) { m_fk->rn.cur_runinginfo += " "; String variant_name; for (int i = 0; i < fb->m_debug_stack_variant_info_num; i++) { const stack_variant_info & info = fb->m_debug_stack_variant_info[i]; if (info.pos == j) { variant_name += info.name; variant_name += "(line:"; variant_name += fkitoa(info.line); variant_name += ") "; } } if (variant_name.empty()) { variant_name = "(anonymous)"; } m_fk->rn.cur_runinginfo += variant_name; m_fk->rn.cur_runinginfo += "\t["; m_fk->rn.cur_runinginfo += fkitoa(j); m_fk->rn.cur_runinginfo += "]\t"; variant * v = 0; GET_STACK(bp, v, j); m_fk->rn.cur_runinginfo += vartostring(v); m_fk->rn.cur_runinginfo += "\n"; } BP_GET_FB(bp, fb); BP_GET_IP(bp, ip); int callbp = 0; BP_GET_BP(bp, callbp); bp = callbp; if (BP_END(bp)) { break; } deps++; } return m_fk->rn.cur_runinginfo.c_str(); }
int interpreter::run(int cmdnum) { fake * fk = m_fk; bool & err = m_isend; int i = 0; // 栈溢出检查 if (UNLIKE((int)ARRAY_MAX_SIZE(m_stack) > m_fk->cfg.stack_max)) { m_isend = true; seterror(fk, efk_run_inter_error, fkgetcurfile(fk), fkgetcurline(fk), fkgetcurfunc(fk), "stack too big %d", ARRAY_MAX_SIZE(m_stack)); return 0; } // 切换检查 if (UNLIKE(m_sleeping)) { if (LIKE(m_yieldtime)) { m_yieldtime--; return 0; } else if (LIKE(fkgetmstick() < m_wakeuptime)) { return 0; } else { m_wakeuptime = 0; } } if (UNLIKE(m_isend)) { return 0; } while (1) { // 当前函数走完 if (UNLIKE(m_ip >= (int)FUNC_BINARY_CMDSIZE(*m_fb))) { FKLOG("pop stack %s", FUNC_BINARY_NAME(*m_fb)); // 记录profile if (UNLIKE(m_fk->pf.isopen())) { uint32_t calltime = 0; BP_GET_CALLTIME(m_bp, calltime); m_fk->pf.add_func_sample(FUNC_BINARY_NAME(*m_fb), fkgetmstick() - calltime); } // 标记 FUNC_BINARY_USE(*m_fb)--; // 更新 if (UNLIKE(!FUNC_BINARY_USE(*m_fb) && FUNC_BINARY_BACKUP(*m_fb))) { FUNC_BINARY_BACKUP_MOVE(*m_fb); } // 出栈 int oldretnum = 0; BP_GET_RETNUM(m_bp, oldretnum); int callbp = 0; BP_GET_BP(m_bp, callbp); BP_GET_FB(m_bp, m_fb); BP_GET_IP(m_bp, m_ip); int oldbp = m_bp; m_sp = m_bp - BP_SIZE - oldretnum; m_bp = callbp; // 所有都完 if (UNLIKE(BP_END(m_bp))) { FKLOG("stack empty end"); m_isend = true; break; } // 塞返回值 else { for (int i = 0; i < oldretnum; i++) { int oldretpos = 0; BP_GET_RETPOS(oldbp, oldretnum, oldretpos, i); variant * ret; GET_VARIANT(*m_fb, m_bp, ret, oldretpos); *ret = m_ret[i]; } } continue; } int code = COMMAND_CODE(GET_CMD(*m_fb, m_ip)); FKLOG("next %d %d %s", COMMAND_TYPE(GET_CMD(*m_fb, m_ip)), code, OpCodeStr(code)); assert (COMMAND_TYPE(GET_CMD(*m_fb, m_ip)) == COMMAND_OPCODE); m_ip++; if (UNLIKE(m_fk->pf.isopen())) { m_fk->pf.add_code_sample(code); } // 执行对应命令,放一起switch效率更高,cpu有缓存 switch (code) { case OPCODE_ASSIGN: { // 赋值dest,必须为栈上或容器内 if (UNLIKE(!(CHECK_STACK_POS(*m_fb, m_ip) || CHECK_CONTAINER_POS(*m_fb, m_ip)))) { err = true; seterror(fk, efk_run_inter_error, fkgetcurfile(fk), fkgetcurline(fk), fkgetcurfunc(fk), "interpreter assign error, dest is not stack or container, type %s", POS_TYPE_NAME(*m_fb, m_ip)); break; } variant * varv = 0; LOG_VARIANT(*m_fb, m_ip, "var"); GET_VARIANT(*m_fb, m_bp, varv, m_ip); m_ip++; // 赋值来源 const variant * valuev = 0; LOG_VARIANT(*m_fb, m_ip, "value"); GET_VARIANT(*m_fb, m_bp, valuev, m_ip); m_ip++; // 赋值 *varv = *valuev; FKLOG("assign %s to %s", (vartostring(valuev)).c_str(), (vartostring(varv)).c_str()); } break; case OPCODE_PLUS: { MATH_OPER(*m_fb, m_bp, m_ip, PLUS); } break; case OPCODE_MINUS: { MATH_OPER(*m_fb, m_bp, m_ip, MINUS); } break; case OPCODE_MULTIPLY: { MATH_OPER(*m_fb, m_bp, m_ip, MULTIPLY); } break; case OPCODE_DIVIDE: { MATH_OPER(*m_fb, m_bp, m_ip, DIVIDE); } break; case OPCODE_DIVIDE_MOD: { MATH_OPER(*m_fb, m_bp, m_ip, DIVIDE_MOD); } break; case OPCODE_AND: { MATH_OPER(*m_fb, m_bp, m_ip, AND); } break; case OPCODE_OR: { MATH_OPER(*m_fb, m_bp, m_ip, OR); } break; case OPCODE_LESS: { MATH_OPER(*m_fb, m_bp, m_ip, LESS); } break; case OPCODE_MORE: { MATH_OPER(*m_fb, m_bp, m_ip, MORE); } break; case OPCODE_EQUAL: { MATH_OPER(*m_fb, m_bp, m_ip, EQUAL); } break; case OPCODE_MOREEQUAL: { MATH_OPER(*m_fb, m_bp, m_ip, MOREEQUAL); } break; case OPCODE_LESSEQUAL: { MATH_OPER(*m_fb, m_bp, m_ip, LESSEQUAL); } break; case OPCODE_NOTEQUAL: { MATH_OPER(*m_fb, m_bp, m_ip, NOTEQUAL); } break; case OPCODE_NOT: { MATH_SINGLE_OPER(*m_fb, m_bp, m_ip, NOT); } break; case OPCODE_AND_JNE: { MATH_OPER_JNE(*m_fb, m_bp, m_ip, AND_JNE); } break; case OPCODE_OR_JNE: { MATH_OPER_JNE(*m_fb, m_bp, m_ip, OR_JNE); } break; case OPCODE_LESS_JNE: { MATH_OPER_JNE(*m_fb, m_bp, m_ip, LESS_JNE); } break; case OPCODE_MORE_JNE: { MATH_OPER_JNE(*m_fb, m_bp, m_ip, MORE_JNE); } break; case OPCODE_EQUAL_JNE: { MATH_OPER_JNE(*m_fb, m_bp, m_ip, EQUAL_JNE); } break; case OPCODE_MOREEQUAL_JNE: { MATH_OPER_JNE(*m_fb, m_bp, m_ip, MOREEQUAL_JNE); } break; case OPCODE_LESSEQUAL_JNE: { MATH_OPER_JNE(*m_fb, m_bp, m_ip, LESSEQUAL_JNE); } break; case OPCODE_NOTEQUAL_JNE: { MATH_OPER_JNE(*m_fb, m_bp, m_ip, NOTEQUAL_JNE); } break; case OPCODE_NOT_JNE: { MATH_SINGLE_OPER_JNE(*m_fb, m_bp, m_ip, NOT_JNE); } break; case OPCODE_JNE: { const variant * cmp = 0; LOG_VARIANT(*m_fb, m_ip, "cmp"); GET_VARIANT(*m_fb, m_bp, cmp, m_ip); m_ip++; int ip = COMMAND_CODE(GET_CMD(*m_fb, m_ip)); m_ip++; if (!(V_ISBOOL(cmp))) { FKLOG("jne %d", ip); m_ip = ip; } else { FKLOG("not jne %d", ip); } } break; case OPCODE_JMP: { int ip = COMMAND_CODE(GET_CMD(*m_fb, m_ip)); m_ip++; FKLOG("jmp %d", ip); m_ip = ip; } break; case OPCODE_PLUS_ASSIGN: { MATH_ASSIGN_OPER(*m_fb, m_bp, m_ip, PLUS); } break; case OPCODE_MINUS_ASSIGN: { MATH_ASSIGN_OPER(*m_fb, m_bp, m_ip, MINUS); } break; case OPCODE_MULTIPLY_ASSIGN: { MATH_ASSIGN_OPER(*m_fb, m_bp, m_ip, MULTIPLY); } break; case OPCODE_DIVIDE_ASSIGN: { MATH_ASSIGN_OPER(*m_fb, m_bp, m_ip, DIVIDE); } break; case OPCODE_DIVIDE_MOD_ASSIGN: { MATH_ASSIGN_OPER(*m_fb, m_bp, m_ip, DIVIDE_MOD); } break; case OPCODE_CALL: { int calltype = COMMAND_CODE(GET_CMD(*m_fb, m_ip)); m_ip++; const variant * callpos = 0; LOG_VARIANT(*m_fb, m_ip, "callpos"); GET_VARIANT(*m_fb, m_bp, callpos, m_ip); m_ip++; int retnum = COMMAND_CODE(GET_CMD(*m_fb, m_ip)); m_ip++; int retpos[MAX_FAKE_RETURN_NUM]; for (int i = 0; i < retnum; i++) { assert(CHECK_STACK_POS(*m_fb, m_ip)); retpos[i] = m_ip; m_ip++; } int argnum = COMMAND_CODE(GET_CMD(*m_fb, m_ip)); m_ip++; paramstack & ps = *getps(m_fk); PS_CLEAR(ps); for (int i = 0; i < argnum; i++) { variant * arg = 0; LOG_VARIANT(*m_fb, m_ip, "arg"); GET_VARIANT(*m_fb, m_bp, arg, m_ip); m_ip++; variant * argdest = 0; PS_PUSH_AND_GET(ps, argdest); *argdest = *arg; } if (LIKE(calltype == CALL_NORMAL)) { call(*callpos, retnum, retpos); } else { m_processor->start_routine(*callpos, retnum, retpos); } } break; case OPCODE_RETURN: { int returnnum = COMMAND_CODE(GET_CMD(*m_fb, m_ip)); if (UNLIKE(!returnnum)) { FKLOG("return empty"); m_ip = (*m_fb).m_size; break; } m_ip++; // 塞给ret for (int i = 0; i < returnnum; i++) { const variant * ret = 0; LOG_VARIANT(*m_fb, m_ip, "ret"); GET_VARIANT(*m_fb, m_bp, ret, m_ip); m_ip++; m_ret[i] = *ret; FKLOG("return %s", (vartostring(&m_ret[i])).c_str()); } m_ip = (*m_fb).m_size; } break; case OPCODE_FORBEGIN: { // 赋值dest,必须为栈上或容器内 if (UNLIKE(!(CHECK_STACK_POS(*m_fb, m_ip) || CHECK_CONTAINER_POS(*m_fb, m_ip)))) { err = true; seterror(fk, efk_run_inter_error, fkgetcurfile(fk), fkgetcurline(fk), fkgetcurfunc(fk), "interpreter assign error, dest is not stack or container, type %s", POS_TYPE_NAME(*m_fb, m_ip)); break; } // var variant * varv = 0; LOG_VARIANT(*m_fb, m_ip, "var"); GET_VARIANT(*m_fb, m_bp, varv, m_ip); m_ip++; // begin const variant * beginv = 0; LOG_VARIANT(*m_fb, m_ip, "begin"); GET_VARIANT(*m_fb, m_bp, beginv, m_ip); m_ip++; // end const variant * endv = 0; LOG_VARIANT(*m_fb, m_ip, "endv"); GET_VARIANT(*m_fb, m_bp, endv, m_ip); m_ip++; // add const variant * addv = 0; LOG_VARIANT(*m_fb, m_ip, "addv"); GET_VARIANT(*m_fb, m_bp, addv, m_ip); m_ip++; int jneip = COMMAND_CODE(GET_CMD(*m_fb, m_ip)); m_ip++; // 赋值 *varv = *beginv; // 增长 if (LIKE(addv->data.real > 0)) { // 判断是否超出 if (UNLIKE(varv->data.real >= endv->data.real)) { m_ip = jneip; } } else { // 判断是否小 if (UNLIKE(varv->data.real <= endv->data.real)) { m_ip = jneip; } } } break; case OPCODE_FORLOOP: { // var variant * varv = 0; LOG_VARIANT(*m_fb, m_ip, "var"); GET_VARIANT(*m_fb, m_bp, varv, m_ip); m_ip++; // end const variant * endv = 0; LOG_VARIANT(*m_fb, m_ip, "endv"); GET_VARIANT(*m_fb, m_bp, endv, m_ip); m_ip++; // add const variant * addv = 0; LOG_VARIANT(*m_fb, m_ip, "addv"); GET_VARIANT(*m_fb, m_bp, addv, m_ip); m_ip++; int continueip = COMMAND_CODE(GET_CMD(*m_fb, m_ip)); m_ip++; // 赋值 V_PLUS(varv, varv, addv); // 增长 if (LIKE(addv->data.real > 0)) { // 判断是否超出 if (UNLIKE(varv->data.real < endv->data.real)) { m_ip = continueip; } } else { // 判断是否小 if (UNLIKE(varv->data.real > endv->data.real)) { m_ip = continueip; } } } break; case OPCODE_SLEEP: { const variant * time = 0; LOG_VARIANT(*m_fb, m_ip, "time"); GET_VARIANT(*m_fb, m_bp, time, m_ip); m_ip++; uint32_t sleeptime = 0; V_GET_REAL(time, sleeptime); m_wakeuptime = fkgetmstick() + sleeptime; m_sleeping = true; return i + 1; } break; case OPCODE_YIELD: { const variant * time = 0; LOG_VARIANT(*m_fb, m_ip, "time"); GET_VARIANT(*m_fb, m_bp, time, m_ip); m_ip++; V_GET_REAL(time, m_yieldtime); m_sleeping = true; return i + 1; } break; default: assert(0); FKERR("next err code %d %s", code, OpCodeStr(code)); break; } if (UNLIKE(err)) { // 发生错误 m_isend = true; } if (UNLIKE(m_isend)) { break; } i++; if (UNLIKE(i >= cmdnum)) { break; } } return i; }
int interpreter::run(int cmdnum) { fake * fk = m_fk; bool & err = m_isend; int i = 0; // 栈溢出检查 if (UNLIKE((int)ARRAY_MAX_SIZE(m_stack) > m_fk->cfg.stack_max)) { m_isend = true; seterror(fk, efk_run_inter_error, fkgetcurfile(fk), fkgetcurline(fk), fkgetcurfunc(fk), "stack too big %d", ARRAY_MAX_SIZE(m_stack)); return 0; } // 切换检查 if (UNLIKE(m_sleeping)) { if (LIKE(m_yieldtime)) { m_yieldtime--; return 0; } else if (LIKE(fkgetmstick() < m_wakeuptime)) { return 0; } else { m_wakeuptime = 0; } } if (UNLIKE(m_isend)) { return 0; } while (1) { // 当前函数走完 if (UNLIKE(m_ip >= (int)FUNC_BINARY_CMDSIZE(*m_fb))) { FKLOG("pop stack %s", FUNC_BINARY_NAME(*m_fb)); // 记录profile if (UNLIKE(m_fk->pf.isopen())) { uint32_t calltime = 0; BP_GET_CALLTIME(m_bp, calltime); m_fk->pf.add_func_sample(FUNC_BINARY_NAME(*m_fb), fkgetmstick() - calltime); } // 标记 FUNC_BINARY_USE(*m_fb)--; // 更新 if (UNLIKE(!FUNC_BINARY_USE(*m_fb) && FUNC_BINARY_BACKUP(*m_fb))) { FUNC_BINARY_BACKUP_MOVE(*m_fb); } // 出栈 int oldretnum = 0; BP_GET_RETNUM(m_bp, oldretnum); int callbp = 0; BP_GET_BP(m_bp, callbp); BP_GET_FB(m_bp, m_fb); BP_GET_IP(m_bp, m_ip); int oldbp = m_bp; m_sp = m_bp - BP_SIZE - oldretnum; m_bp = callbp; // 所有都完 if (UNLIKE(BP_END(m_bp))) { FKLOG("stack empty end"); m_isend = true; break; } // 塞返回值 else { for (int i = 0; i < oldretnum; i++) { int oldretpos = 0; BP_GET_RETPOS(oldbp, oldretnum, oldretpos, i); variant * ret; GET_VARIANT(*m_fb, m_bp, ret, oldretpos); *ret = m_ret[i]; } } continue; } int code = COMMAND_CODE(GET_CMD(*m_fb, m_ip)); FKLOG("next %d %d %s", COMMAND_TYPE(GET_CMD(*m_fb, m_ip)), code, OpCodeStr(code)); assert (COMMAND_TYPE(GET_CMD(*m_fb, m_ip)) == COMMAND_OPCODE); m_ip++; if (UNLIKE(m_fk->pf.isopen())) { m_fk->pf.add_code_sample(code); } // 执行对应命令,放一起switch效率更高,cpu有缓存 switch (code) { case OPCODE_ASSIGN: { // 赋值dest,必须为栈上或容器内 if (UNLIKE(!(CHECK_STACK_POS(*m_fb, m_ip) || CHECK_CONTAINER_POS(*m_fb, m_ip)))) { err = true; seterror(fk, efk_run_inter_error, fkgetcurfile(fk), fkgetcurline(fk), fkgetcurfunc(fk), "interpreter assign error, dest is not stack or container, type %s", POS_TYPE_NAME(*m_fb, m_ip)); break; } variant * varv = 0; LOG_VARIANT(*m_fb, m_ip, "var"); GET_VARIANT(*m_fb, m_bp, varv, m_ip); if (UNLIKE(CHECK_CONST_MAP_POS(varv) || CHECK_CONST_ARRAY_POS(varv))) { err = true; seterror(fk, efk_run_inter_error, fkgetcurfile(fk), fkgetcurline(fk), fkgetcurfunc(fk), "interpreter assign error, dest is const container"); break; } m_ip++; // 赋值来源 const variant * valuev = 0; LOG_VARIANT(*m_fb, m_ip, "value"); GET_VARIANT(*m_fb, m_bp, valuev, m_ip); m_ip++; // 赋值 *varv = *valuev; FKLOG("assign %s to %s", (vartostring(valuev)).c_str(), (vartostring(varv)).c_str()); } break; case OPCODE_PLUS: { MATH_OPER(*m_fb, m_bp, m_ip, PLUS); } break; case OPCODE_MINUS: { MATH_OPER(*m_fb, m_bp, m_ip, MINUS); } break; case OPCODE_MULTIPLY: { MATH_OPER(*m_fb, m_bp, m_ip, MULTIPLY); } break; case OPCODE_DIVIDE: { MATH_OPER(*m_fb, m_bp, m_ip, DIVIDE); } break; case OPCODE_DIVIDE_MOD: { MATH_OPER(*m_fb, m_bp, m_ip, DIVIDE_MOD); } break; case OPCODE_STRING_CAT: { MATH_OPER(*m_fb, m_bp, m_ip, STRING_CAT); } break; case OPCODE_AND: { MATH_OPER(*m_fb, m_bp, m_ip, AND); } break; case OPCODE_OR: { MATH_OPER(*m_fb, m_bp, m_ip, OR); } break; case OPCODE_LESS: { MATH_OPER(*m_fb, m_bp, m_ip, LESS); } break; case OPCODE_MORE: { MATH_OPER(*m_fb, m_bp, m_ip, MORE); } break; case OPCODE_EQUAL: { MATH_OPER(*m_fb, m_bp, m_ip, EQUAL); } break; case OPCODE_MOREEQUAL: { MATH_OPER(*m_fb, m_bp, m_ip, MOREEQUAL); } break; case OPCODE_LESSEQUAL: { MATH_OPER(*m_fb, m_bp, m_ip, LESSEQUAL); } break; case OPCODE_NOTEQUAL: { MATH_OPER(*m_fb, m_bp, m_ip, NOTEQUAL); } break; case OPCODE_NOT: { MATH_SINGLE_OPER(*m_fb, m_bp, m_ip, NOT); } break; case OPCODE_AND_JNE: { MATH_OPER_JNE(*m_fb, m_bp, m_ip, AND_JNE); } break; case OPCODE_OR_JNE: { MATH_OPER_JNE(*m_fb, m_bp, m_ip, OR_JNE); } break; case OPCODE_LESS_JNE: { MATH_OPER_JNE(*m_fb, m_bp, m_ip, LESS_JNE); } break; case OPCODE_MORE_JNE: { MATH_OPER_JNE(*m_fb, m_bp, m_ip, MORE_JNE); } break; case OPCODE_EQUAL_JNE: { MATH_OPER_JNE(*m_fb, m_bp, m_ip, EQUAL_JNE); } break; case OPCODE_MOREEQUAL_JNE: { MATH_OPER_JNE(*m_fb, m_bp, m_ip, MOREEQUAL_JNE); } break; case OPCODE_LESSEQUAL_JNE: { MATH_OPER_JNE(*m_fb, m_bp, m_ip, LESSEQUAL_JNE); } break; case OPCODE_NOTEQUAL_JNE: { MATH_OPER_JNE(*m_fb, m_bp, m_ip, NOTEQUAL_JNE); } break; case OPCODE_NOT_JNE: { MATH_SINGLE_OPER_JNE(*m_fb, m_bp, m_ip, NOT_JNE); } break; case OPCODE_JNE: { const variant * cmp = 0; LOG_VARIANT(*m_fb, m_ip, "cmp"); GET_VARIANT(*m_fb, m_bp, cmp, m_ip); m_ip++; int ip = COMMAND_CODE(GET_CMD(*m_fb, m_ip)); m_ip++; if (!(V_ISBOOL(cmp))) { FKLOG("jne %d", ip); m_ip = ip; } else { FKLOG("not jne %d", ip); } } break; case OPCODE_JMP: { int ip = COMMAND_CODE(GET_CMD(*m_fb, m_ip)); m_ip++; FKLOG("jmp %d", ip); m_ip = ip; } break; case OPCODE_PLUS_ASSIGN: { MATH_ASSIGN_OPER(*m_fb, m_bp, m_ip, PLUS); } break; case OPCODE_MINUS_ASSIGN: { MATH_ASSIGN_OPER(*m_fb, m_bp, m_ip, MINUS); } break; case OPCODE_MULTIPLY_ASSIGN: { MATH_ASSIGN_OPER(*m_fb, m_bp, m_ip, MULTIPLY); } break; case OPCODE_DIVIDE_ASSIGN: { MATH_ASSIGN_OPER(*m_fb, m_bp, m_ip, DIVIDE); } break; case OPCODE_DIVIDE_MOD_ASSIGN: { MATH_ASSIGN_OPER(*m_fb, m_bp, m_ip, DIVIDE_MOD); } break; case OPCODE_CALL: { int calltype = COMMAND_CODE(GET_CMD(*m_fb, m_ip)); m_ip++; const variant * callpos = 0; LOG_VARIANT(*m_fb, m_ip, "callpos"); GET_VARIANT(*m_fb, m_bp, callpos, m_ip); m_ip++; int retnum = COMMAND_CODE(GET_CMD(*m_fb, m_ip)); m_ip++; int retpos[MAX_FAKE_RETURN_NUM]; for (int i = 0; i < retnum; i++) { assert(CHECK_STACK_POS(*m_fb, m_ip) || CHECK_CONTAINER_POS(*m_fb, m_ip)); retpos[i] = m_ip; m_ip++; } int argnum = COMMAND_CODE(GET_CMD(*m_fb, m_ip)); m_ip++; paramstack & ps = *getps(m_fk); PS_CLEAR(ps); for (int i = 0; i < argnum; i++) { variant * arg = 0; LOG_VARIANT(*m_fb, m_ip, "arg"); GET_VARIANT(*m_fb, m_bp, arg, m_ip); m_ip++; variant * argdest = 0; PS_PUSH_AND_GET(ps, argdest); *argdest = *arg; } if (LIKE(calltype == CALL_NORMAL)) { call(*callpos, retnum, retpos); } else if (LIKE(calltype == CALL_CLASSMEM)) { void * classptr = 0; const char * classprefix = 0; // prefix variant * classvar; PS_GET(ps, classvar, PS_SIZE(ps) - 1); V_GET_POINTER(classvar, classptr, classprefix); if (UNLIKE(err)) { break; } // mem func name const char * funcname = 0; V_GET_STRING(callpos, funcname); if (UNLIKE(err)) { break; } if (UNLIKE(!classptr)) { err = true; seterror(fk, efk_run_inter_error, fkgetcurfile(fk), fkgetcurline(fk), fkgetcurfunc(fk), "interpreter class mem call error, the class ptr is null, type %s", classprefix); break; } // whole name char wholename[MAX_FAKE_REG_FUNC_NAME_LEN]; if (UNLIKE(classvar->data.ponter->typesz + callpos->data.str->sz >= MAX_FAKE_REG_FUNC_NAME_LEN)) { err = true; seterror(fk, efk_run_inter_error, fkgetcurfile(fk), fkgetcurline(fk), fkgetcurfunc(fk), "interpreter class mem call error, the name is too long, func %s %s", classprefix, funcname); break; } memcpy(wholename, classprefix, classvar->data.ponter->typesz); memcpy(wholename + classvar->data.ponter->typesz, funcname, callpos->data.str->sz); wholename[classvar->data.ponter->typesz + callpos->data.str->sz] = 0; // call it variant tmp; V_SET_STRING(&tmp, wholename); call(tmp, retnum, retpos); } else { m_processor->start_routine(*callpos, retnum, retpos); } } break; case OPCODE_RETURN: { int returnnum = COMMAND_CODE(GET_CMD(*m_fb, m_ip)); if (UNLIKE(!returnnum)) { FKLOG("return empty"); m_ip = (*m_fb).m_size; break; } m_ip++; // 塞给ret for (int i = 0; i < returnnum; i++) { const variant * ret = 0; LOG_VARIANT(*m_fb, m_ip, "ret"); GET_VARIANT(*m_fb, m_bp, ret, m_ip); m_ip++; m_ret[i] = *ret; FKLOG("return %s", (vartostring(&m_ret[i])).c_str()); } m_ip = (*m_fb).m_size; } break; case OPCODE_SLEEP: { const variant * time = 0; LOG_VARIANT(*m_fb, m_ip, "time"); GET_VARIANT(*m_fb, m_bp, time, m_ip); m_ip++; uint32_t sleeptime = 0; V_GET_REAL(time, sleeptime); m_wakeuptime = fkgetmstick() + sleeptime; m_sleeping = true; return i + 1; } break; case OPCODE_YIELD: { const variant * time = 0; LOG_VARIANT(*m_fb, m_ip, "time"); GET_VARIANT(*m_fb, m_bp, time, m_ip); m_ip++; V_GET_REAL(time, m_yieldtime); m_sleeping = true; return i + 1; } break; default: assert(0); FKERR("next err code %d %s", code, OpCodeStr(code)); break; } if (UNLIKE(err)) { // 发生错误 m_isend = true; // 清除当前栈上函数的使用标记 { int ip = m_ip; int bp = m_bp; const func_binary * fb = m_fb; while (!BP_END(bp)) { // 标记 FUNC_BINARY_USE(*fb)--; // 更新 if (UNLIKE(!FUNC_BINARY_USE(*fb) && FUNC_BINARY_BACKUP(*fb))) { FUNC_BINARY_BACKUP_MOVE(*fb); } BP_GET_FB(bp, fb); BP_GET_IP(bp, ip); int callbp = 0; BP_GET_BP(bp, callbp); bp = callbp; if (BP_END(bp)) { break; } } } } if (UNLIKE(m_isend)) { break; } i++; if (UNLIKE(i >= cmdnum)) { break; } } return i; }