SeExpr
ExprFuncX.cpp
Go to the documentation of this file.
1 /*
2  Copyright Disney Enterprises, Inc. All rights reserved.
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License
6  and the following modification to it: Section 6 Trademarks.
7  deleted and replaced with:
8 
9  6. Trademarks. This License does not grant permission to use the
10  trade names, trademarks, service marks, or product names of the
11  Licensor and its affiliates, except as required for reproducing
12  the content of the NOTICE file.
13 
14  You may obtain a copy of the License at
15  http://www.apache.org/licenses/LICENSE-2.0
16 */
17 #include "ExprFunc.h"
18 #include "ExprFuncX.h"
19 #include "Interpreter.h"
20 #include "ExprNode.h"
21 #include <cstdio>
22 
23 namespace SeExpr2 {
24 int ExprFuncSimple::EvalOp(int *opData, double *fp, char **c, std::vector<int> &callStack) {
25  ExprFuncSimple *simple = reinterpret_cast<ExprFuncSimple *>(c[opData[0]]);
26  // ExprFuncNode::Data* simpleData=reinterpret_cast<ExprFuncNode::Data*>(c[opData[1]]);
27  ArgHandle args(opData, fp, c, callStack);
28  simple->eval(args);
29  return 1;
30 }
31 
32 int ExprFuncSimple::buildInterpreter(const ExprFuncNode *node, Interpreter *interpreter) const {
33  std::vector<int> operands;
34  for (int c = 0; c < node->numChildren(); c++) {
35  int operand = node->child(c)->buildInterpreter(interpreter);
36 #if 0
37  // debug
38  std::cerr<<"we are "<<node->promote(c)<<" "<<c<<std::endl;
39 #endif
40  if (node->promote(c) != 0) {
41  interpreter->addOp(getTemplatizedOp<Promote>(node->promote(c)));
42  int promotedOperand = interpreter->allocFP(node->promote(c));
43  interpreter->addOperand(operand);
44  interpreter->addOperand(promotedOperand);
45  operand = promotedOperand;
46  interpreter->endOp();
47  }
48  operands.push_back(operand);
49  }
50  int outoperand = -1;
51  int nargsData = interpreter->allocFP(1);
52  interpreter->d[nargsData] = node->numChildren();
53  if (node->type().isFP())
54  outoperand = interpreter->allocFP(node->type().dim());
55  else if (node->type().isString())
56  outoperand = interpreter->allocPtr();
57  else
58  assert(false);
59 
60  interpreter->addOp(EvalOp);
61  int ptrLoc = interpreter->allocPtr();
62  int ptrDataLoc = interpreter->allocPtr();
63  interpreter->s[ptrLoc] = (char *)this;
64  interpreter->addOperand(ptrLoc);
65  interpreter->addOperand(ptrDataLoc);
66  interpreter->addOperand(outoperand);
67  interpreter->addOperand(nargsData);
68  for (size_t c = 0; c < operands.size(); c++) {
69  interpreter->addOperand(operands[c]);
70  }
71  interpreter->endOp(false); // do not eval because the function may not be evaluatable!
72 
73  // call into interpreter eval
74  int pc = interpreter->nextPC() - 1;
75  int *opCurr = (&interpreter->opData[0]) + interpreter->ops[pc].second;
76 
77  ArgHandle args(opCurr, &interpreter->d[0], &interpreter->s[0], interpreter->callStack);
78  ExprFuncNode::Data* data = evalConstant(node, args);
79  node->setData(data);
80  interpreter->s[ptrDataLoc] = reinterpret_cast<char *>(data);
81 
82  return outoperand;
83 }
84 }
85 
86 extern "C" {
87 // allocate int[4+number of args];
88 // allocate char*[2];
89 // allocate double[1+ sizeof(ret) + sizeof(args)]
90 //
91 // int[0]= c , 0
92 // int[1]= c , 1
93 // int[2]= f, 0
94 // int[3]= f, 8
95 //
96 // int[4]= f, 8
97 // int[5]= f, 9
98 //
99 //
100 // double[0] = 0
101 // double[1] = 0
102 // double[2] = 0
103 // double[3] = 0
104 // opData indexes either into f or into c.
105 // opdata[0] points to ExprFuncSimple instance
106 // opdata[1] points to the data generated by evalConstant
107 // opdata[2] points to return value
108 // opdata[3] points to number of args
109 // opdata[4] points to beginning of arguments in
110 void SeExpr2LLVMEvalCustomFunction(int *opDataArg,
111  double *fpArg,
112  char **strArg,
113  void **funcdata,
114  const SeExpr2::ExprFuncNode *node) {
115  const SeExpr2::ExprFunc *func = node->func();
116  SeExpr2::ExprFuncX *funcX = const_cast<SeExpr2::ExprFuncX *>(func->funcx());
117  SeExpr2::ExprFuncSimple *funcSimple = static_cast<SeExpr2::ExprFuncSimple *>(funcX);
118 
119  strArg[0] = reinterpret_cast<char *>(funcSimple);
120 
121  std::vector<int> callStack;
122  SeExpr2::ExprFuncSimple::ArgHandle handle(opDataArg, fpArg, strArg, callStack);
123  if (!*funcdata) {
124  handle.data = funcSimple->evalConstant(node, handle);
125  *funcdata = reinterpret_cast<void *>(handle.data);
126  node->setData(handle.data);
127  } else {
128  handle.data = reinterpret_cast<SeExpr2::ExprFuncNode::Data *>(*funcdata);
129  }
130 
131  funcSimple->eval(handle);
132  // for (int i = 0; i < retSize; ++i) result[i] = fp[1 + i];
133 }
134 }
SeExpr2::ExprType::isString
bool isString() const
Definition: ExprType.h:169
SeExpr2::Interpreter::endOp
void endOp(bool execute=true)
Definition: Interpreter.h:83
SeExpr2::ExprNode::numChildren
int numChildren() const
Number of children.
Definition: ExprNode.h:114
SeExpr2::ExprNode::type
const ExprType & type() const
The type of the node.
Definition: ExprNode.h:145
SeExpr2::Interpreter::addOp
int addOp(OpF op)
! adds an operator to the program (pointing to the data at the current location)
Definition: Interpreter.h:73
SeExpr2::Interpreter::d
std::vector< double > d
Double data (constants and evaluated)
Definition: Interpreter.h:43
SeExpr2::ExprFuncNode::func
const ExprFunc * func() const
Definition: ExprNode.h:591
SeExpr2::ExprFuncNode::setData
void setData(Data *data) const
associate blind data with this node (subsequently owned by this object)
Definition: ExprNode.h:583
Interpreter.h
SeExpr2::ExprFuncSimple
Definition: ExprFuncX.h:72
SeExpr2::ExprFuncSimple::ArgHandle
Definition: ExprFuncX.h:76
SeExpr2LLVMEvalCustomFunction
void SeExpr2LLVMEvalCustomFunction(int *opDataArg, double *fpArg, char **strArg, void **funcdata, const SeExpr2::ExprFuncNode *node)
Definition: ExprFuncX.cpp:110
SeExpr2::Interpreter::opData
std::vector< int > opData
Ooperands to op.
Definition: Interpreter.h:47
SeExpr2::ExprFuncX
Extension function spec, used for complicated argument custom functions.
Definition: ExprFuncX.h:35
SeExpr2::ExprType::dim
int dim() const
Definition: ExprType.h:160
ExprFunc.h
SeExpr2::ExprFuncNode
Node that calls a function.
Definition: ExprNode.h:517
SeExpr2::ExprFuncSimple::ArgHandle::data
ExprFuncNode::Data * data
Definition: ExprFuncX.h:99
SeExpr2::ExprFunc
Function Definition, used in parse tree and func table.
Definition: ExprFunc.h:44
SeExpr2::ExprFunc::funcx
const ExprFuncX * funcx() const
return pointer to the funcx
Definition: ExprFunc.h:125
SeExpr2
Definition: Context.h:22
ExprNode.h
SeExpr2::ExprNode::buildInterpreter
virtual int buildInterpreter(Interpreter *interpreter) const
builds an interpreter. Returns the location index for the evaluated data
Definition: Interpreter.cpp:538
SeExpr2::Interpreter::nextPC
int nextPC()
Return the position that the next instruction will be placed at.
Definition: Interpreter.h:70
SeExpr2::Interpreter::allocFP
int allocFP(int n)
! Allocate a floating point set of data of dimension n
Definition: Interpreter.h:104
SeExpr2::Interpreter::callStack
std::vector< int > callStack
Definition: Interpreter.h:57
ExprFuncX.h
SeExpr2::ExprFuncSimple::buildInterpreter
virtual int buildInterpreter(const ExprFuncNode *node, Interpreter *interpreter) const
Build an interpreter to evaluate the expression.
Definition: ExprFuncX.cpp:32
SeExpr2::ExprType::isFP
bool isFP() const
Direct is predicate checks.
Definition: ExprType.h:164
SeExpr2::Interpreter::s
std::vector< char * > s
constant and evaluated pointer data
Definition: Interpreter.h:45
SeExpr2::ExprFuncSimple::eval
virtual void eval(ArgHandle args)=0
SeExpr2::ExprFuncNode::Data
base class for custom instance data
Definition: ExprNode.h:570
SeExpr2::ExprFuncSimple::evalConstant
virtual ExprFuncNode::Data * evalConstant(const ExprFuncNode *node, ArgHandle args) const =0
SeExpr2::Interpreter::allocPtr
int allocPtr()
Allocate a pointer location (can be anything, but typically space for char*)
Definition: Interpreter.h:111
SeExpr2::ExprNode::child
const ExprNode * child(size_t i) const
Get 0 indexed child.
Definition: ExprNode.h:117
SeExpr2::Interpreter::ops
std::vector< std::pair< OpF, int > > ops
Definition: Interpreter.h:56
SeExpr2::ExprFuncNode::promote
int promote(int i) const
Definition: ExprNode.h:590
SeExpr2::Interpreter
Definition: Interpreter.h:40
SeExpr2::ExprFuncSimple::EvalOp
static int EvalOp(int *opData, double *fp, char **c, std::vector< int > &callStack)
Definition: ExprFuncX.cpp:24
SeExpr2::Interpreter::addOperand
int addOperand(int param)
! Adds an operand. Note this should be done after doing the addOp!
Definition: Interpreter.h:96