📄 fpu87.cpp
字号:
} // void floor(fpuArithmetic &myfpu){myfpu.ASM_c_call((void*)mymath::myfloor,1);}// void xfloor(fpuArithmetic &myfpu){myfpu.ASM_c_call((void*)mymath::xfloor,1);} void fact(fpuArithmetic &f){f.ASM_c_call((void*)mymath::fact,1);} void abs(fpuArithmetic &f){f.ASM_fabs();} void sin(fpuArithmetic &f){f.ASM_fsin();} void cos(fpuArithmetic &f){f.ASM_fcos();} //void sqrt(fpuArithmetic &f){f.ASM_fsqrt();} void sqrt(fpuArithmetic &f){f.ASM_c_call((void*)mymath::sqrt,1);} void pow(fpuArithmetic &f){f.ASM_c_call((void*)mymath::pow,2);} void div(fpuArithmetic &f){f.ASM_c_call((void*)mymath::div,2);} void ln(fpuArithmetic &f){f.ASM_c_call((void*)mymath::ln,1);} void exp(fpuArithmetic &f){f.ASM_c_call((void*)mymath::exp,1);} void sinh(fpuArithmetic &f){f.ASM_c_call((void*)mymath::sinh,1);} void cosh(fpuArithmetic &f){f.ASM_c_call((void*)mymath::cosh,1);} void tanh(fpuArithmetic &f){f.ASM_c_call((void*)mymath::tanh,1);} void tan(fpuArithmetic &f){f.ASM_c_call((void*)mymath::tan,1);} void ceil(fpuArithmetic &f){f.ASM_c_call((void*)mymath::ceil,1);} void floor(fpuArithmetic &f){f.ASM_c_call((void*)mymath::myfloor,1);} void xfloor(fpuArithmetic &myfpu){myfpu.ASM_c_call((void*)mymath::xfloor,1);} void acos(fpuArithmetic &f){f.ASM_c_call((void*)mymath::acos,1);} void asin(fpuArithmetic &f){f.ASM_c_call((void*)mymath::asin,1);} void atan(fpuArithmetic &f){f.ASM_c_call((void*)mymath::atan,1);} void atan2(fpuArithmetic &f){f.ASM_c_call((void*)mymath::atan2,2);} void fmod(fpuArithmetic &f){f.ASM_c_call((void*)mymath::fmod,2);} void min(fpuArithmetic &f){f.ASM_c_call((void*)mymath::min,2);} void max(fpuArithmetic &f){f.ASM_c_call((void*)mymath::max,2);} void mygreater(fpuArithmetic &f){f.ASM_c_call((void*)mymath::mygreater,2);} void myless_equal(fpuArithmetic &f){f.ASM_fsub();} };string x86_Executor::GetHandlerName(const FunctionDeclaration &decl){ string s = decl.name; char buf[100]; sprintf(buf,"(%d,%d)",decl.nleft_Params,decl.nright_Params); s += buf; return s;}string x86_Executor::GetBHandlerName(const BracketDeclaration &decl){ string s; s += decl.open; s += decl.close; return s;}void x86_Executor::AddHandler(char *name,int l,int r,handlerFunction func){ FunctionDeclaration decl(name,l,r); hMap[GetHandlerName(decl)] = func;}void x86_Executor::AddBHandler(char *name,handlerFunction func){ BracketDeclaration decl = {name[0],name[1]}; hMap[GetBHandlerName(decl)] = func;}x86_Executor::x86_Executor(){ AddHandler("log",0,1,x86_exec_ns::log01); AddHandler("log",0,2,x86_exec_ns::log02); AddHandler("Pi",0,0,x86_exec_ns::Pi); AddHandler("e",0,0,x86_exec_ns::e); AddHandler("+",1,1,x86_exec_ns::add); AddHandler("-",1,1,x86_exec_ns::sub); AddHandler("/",1,1,x86_exec_ns::div); AddHandler("*",1,1,x86_exec_ns::mul); AddHandler("+",0,1,x86_exec_ns::add01); AddHandler("-",0,1,x86_exec_ns::sub01); AddHandler("sin",0,1,x86_exec_ns::sin); AddHandler("cos",0,1,x86_exec_ns::cos); AddBHandler("[]",x86_exec_ns::floor); AddBHandler("{}",x86_exec_ns::xfloor); AddHandler("floor",0,1,x86_exec_ns::floor); AddHandler("ceil",0,1,x86_exec_ns::ceil); AddHandler("!",1,0,x86_exec_ns::fact); AddHandler("abs",0,1,x86_exec_ns::abs); AddHandler("sinh",0,1,x86_exec_ns::sinh); AddHandler("cosh",0,1,x86_exec_ns::cosh); AddHandler("tan",0,1,x86_exec_ns::tan); AddHandler("tanh",0,1,x86_exec_ns::tanh); AddHandler("asin",0,1,x86_exec_ns::asin); AddHandler("acos",0,1,x86_exec_ns::acos); AddHandler("atan",0,1,x86_exec_ns::atan); AddHandler("atan",0,2,x86_exec_ns::atan2); AddHandler("sqrt",0,1,x86_exec_ns::sqrt); AddHandler("^",1,1,x86_exec_ns::pow); AddHandler("ln",0,1,x86_exec_ns::ln); AddHandler("min",0,2,x86_exec_ns::min); AddHandler("max",0,2,x86_exec_ns::max); AddHandler("exp",0,1,x86_exec_ns::exp); AddHandler("mod",1,1,x86_exec_ns::fmod); AddHandler("=",1,1,x86_exec_ns::myless_equal); AddHandler("<",1,1,x86_exec_ns::myless_equal); AddHandler(">",1,1,x86_exec_ns::mygreater); AddHandler("<=",1,1,x86_exec_ns::myless_equal); AddHandler(">=",1,1,x86_exec_ns::mygreater); }x86_Executor::~x86_Executor(){}double x86_Executor::Execute(const FunctionDeclaration &decl,stack<double> params){ if(decl.nleft_Params + decl.nright_Params > (int)params.size()) throw errNotEnoguhtArguments(); string declname = GetHandlerName(decl); if(hMap.find(declname) == hMap.end()) throw errNoHandlerInExcutingModel(); fpuArithmetic myarith; myarith.ASM_prolog(); myarith.initExceptions(); int len = (int)params.size(); double *arr = new double[len]; int i = 0; while(i < len){ arr[i++] = params.top(); params.pop(); } for(i = 0;i < len;i++)myarith.ASM_fpush(arr+len-1-i); (*(hMap[declname]))(myarith); double ret = 0.; myarith.ASM_fpop(&ret); myarith.ASM_epilog(); myarith.ASM_ret(); myarith.pushException(); myarith.Init(); myarith.execute(); myarith.Free(); delete arr; return ret;}double x86_Executor::Execute(const BracketDeclaration &decl,stack<double> params){ if((int)params.size() < 1) throw errNotEnoguhtArguments(); string declname = GetBHandlerName(decl); if(hMap.find(declname) == hMap.end()) throw errNoHandlerInExcutingModel(); fpuArithmetic myarith; myarith.ASM_prolog(); myarith.initExceptions(); int len = (int)params.size(); double *arr = new double[len]; int i = 0; while(i < len){ arr[i++] = params.top(); params.pop(); } for(i = 0;i < len;i++)myarith.ASM_fpush(arr+len-1-i); (*(hMap[declname]))(myarith); double ret = 0.; myarith.ASM_fpop(&ret); myarith.ASM_epilog(); myarith.ASM_ret(); myarith.pushException(); myarith.Init(); myarith.execute(); myarith.Free(); delete arr; return ret;}void x86_Executor::RBuild(const bPTI &bpti,ExecutionContext &context,fpuArithmetic &arith,stack<double *> &astack){ for(vbPTI::const_iterator i = bpti.childs.begin();i != bpti.childs.end();i++){ RBuild(*(*i),context,arith,astack); } switch(bpti.myType){ case(bPTI::Function):{ const PTI_Function& func = (const PTI_Function&)bpti; const FunctionDeclaration &decl = func.declaration; if(decl.nleft_Params + decl.nright_Params > (int)astack.size()) throw errNotEnoguhtArguments(); string declname = GetHandlerName(decl); if(hMap.find(declname) == hMap.end()) throw errNoHandlerInExcutingModel(); int len = (int)decl.nleft_Params + decl.nright_Params; double **arr = new double*[len]; int i = 0; for(i = 0;i < len;i++){ arr[i] = astack.top();// arith.ASM_fpush(astack.top()); astack.pop(); } for(i = 0;i < len;i++)arith.ASM_fpush(arr[len - 1 - i]); delete arr; (*(hMap[declname]))(arith); double *ret = context.AllocTemporary(0); arith.ASM_fpop(ret); astack.push(ret);// cout << declname << "\n"; break; } case(bPTI::Variable):{ const PTI_Variable& var = (const PTI_Variable&)bpti; map<string,double *>::iterator i = context.VariablesDefenition.find(var.declaration.name); if(i == context.VariablesDefenition.end()) throw errVariableNotFound(); astack.push((*i).second); break; } case(bPTI::RealConstant):{ const PTI_RealConstant& rconst = (const PTI_RealConstant&)bpti; astack.push(context.AllocTemporary(rconst.value));// cout << rconst.value << "\n"; break; } case(bPTI::Brackets):{ const PTI_Brackets& brackets = (const PTI_Brackets&)bpti; string declname = GetBHandlerName(brackets.declaration);// if(hMap.find(declname) == hMap.end())// throw errNoHandlerInExcutingModel(); if(hMap.find(declname) != hMap.end()){ arith.ASM_fpush(astack.top()); astack.pop(); (*(hMap[declname]))(arith); double *ret = context.AllocTemporary(0); arith.ASM_fpop(ret); astack.push(ret); } break; } case(bPTI::Unknown): break; }}double *x86_Executor::Build(const bPTI &bpti,ExecutionContext &context){ fpuArithmetic *arith = new fpuArithmetic; arith->ASM_prolog(); arith->initExceptions(); context.codes = (mashineCode *)arith; stack<double *> pseudoFPUstack; RBuild(bpti,context,*arith,pseudoFPUstack); assert(pseudoFPUstack.size() == 1); arith->ASM_epilog(); arith->ASM_ret(); arith->pushException(); arith->Init(); return pseudoFPUstack.top();}/*+ x87 executor*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -