13#define XBYAK_NO_OP_NAMES
16 #pragma warning(disable : 4127)
17 #pragma warning(disable : 4512)
19#include <boost/spirit/include/classic_file_iterator.hpp>
20#include <boost/spirit/include/classic_core.hpp>
21#include <boost/bind.hpp>
32 typedef std::map<std::string, int>
Map;
37 double constTbl_[MAX_CONST_NUM];
45 const Xbyak::Reg64& valTbl_;
46 const Xbyak::Reg64& tbl_;
55 FuncGen(
const std::vector<std::string>& varTbl)
61#elif defined(XBYAK64_WIN)
71 mov(tbl_, (
size_t)constTbl_);
77 mov(tbl_, (
size_t)constTbl_);
79 for (
int i = 0, n =
static_cast<int>(varTbl.size()); i < n; i++) {
80 varMap_[varTbl[i]] = i;
86 if (constTblPos_ >= MAX_CONST_NUM)
throw;
87 constTbl_[constTblPos_] = n;
88 if (regIdx_ == 7)
throw;
93 void genVal(
const char *begin,
const char *end)
95 std::string var(begin, end);
97 if (regIdx_ == 7)
throw;
131struct Grammar :
public boost::spirit::classic::grammar<Grammar> {
134 template<
typename ScannerT>
140 using namespace boost;
141 using namespace boost::spirit::classic;
150 |
'(' >>
poly0 >>
')';
152 const boost::spirit::classic::rule<ScannerT>&
start()
const {
return poly0; }
156void put(
const std::vector<double>& x)
159 for (
size_t i = 1, n = x.size(); i < n; i++) {
160 printf(
", %f", x[i]);
167 fprintf(stderr,
"calc \"var1 var2 ...\" \"function of var\"\n");
168 fprintf(stderr,
"eg. calc x \"x*x\"\n");
169 fprintf(stderr,
"eg. calc \"x y z\" \"x*x + y - z\"\n");
172 const char *poly =
argv[2];
174 std::vector<std::string> varTbl;
178 std::istringstream is(
argv[1]);
180 printf(
"varTbl = { ");
184 if (var.empty())
break;
185 printf(
"%s:%d, ", var.c_str(), i);
186 varTbl.push_back(var);
193 boost::spirit::classic::parse_info<>
r = parse(poly, calc, boost::spirit::classic::space_p);
195 printf(
"err poly=%s\n", poly);
199 std::vector<double> valTbl;
200 valTbl.resize(varTbl.size());
203 void (*func)(
double *
ret,
const double *valTbl) = funcGen.
getCode<void (*)(
double *,
const double*)>();
206 double (*func)(
const double *valTbl) = funcGen.
getCode<double (*)(
const double*)>();
208 for (
int i = 0; i < 10; i++) {
209 for (
size_t j = 0, n = valTbl.size();
j < n;
j++) {
210 valTbl[
j] = rand() % 7;
214 func(&y, &valTbl[0]);
216 y = func(&valTbl[0]);
218 printf(
"f(");
put(valTbl); printf(
")=%f\n", y);
220 }
catch (std::exception& e) {
221 printf(
"ERR:%s\n", e.what());
222 }
catch (
Error err) {
223 printf(
"ERR:%d\n", err);
225 printf(
"unknown error\n");
void genDiv(const char *, const char *)
FuncGen(const std::vector< std::string > &varTbl)
void genMul(const char *, const char *)
void genAdd(const char *, const char *)
void genSub(const char *, const char *)
std::map< std::string, int > Map
void genVal(const char *begin, const char *end)
const uint8 * getCode() const
void subsd(const Xmm &xmm, const Operand &op)
void addsd(const Xmm &xmm, const Operand &op)
void movaps(const Address &addr, const Xmm &xmm)
void mov(const Operand ®1, const Operand ®2)
void mulsd(const Xmm &xmm, const Operand &op)
void divsd(const Xmm &xmm, const Operand &op)
boost::spirit::classic::rule< ScannerT > poly0
boost::spirit::classic::rule< ScannerT > poly1
definition(const Grammar &self)
const boost::spirit::classic::rule< ScannerT > & start() const
boost::spirit::classic::rule< ScannerT > var
boost::spirit::classic::rule< ScannerT > poly2
Xbyak ; JIT assembler for x86(IA32)/x64 by C++.