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  interpreter->s[ptrDataLoc] = reinterpret_cast<char *>(evalConstant(node, args));
79 
80  return outoperand;
81 }
82 }
83 
84 extern "C" {
85 // allocate int[4+number of args];
86 // allocate char*[2];
87 // allocate double[1+ sizeof(ret) + sizeof(args)]
88 //
89 // int[0]= c , 0
90 // int[1]= c , 1
91 // int[2]= f, 0
92 // int[3]= f, 8
93 //
94 // int[4]= f, 8
95 // int[5]= f, 9
96 //
97 //
98 // double[0] = 0
99 // double[1] = 0
100 // double[2] = 0
101 // double[3] = 0
102 // opData indexes either into f or into c.
103 // opdata[0] points to ExprFuncSimple instance
104 // opdata[1] points to the data generated by evalConstant
105 // opdata[2] points to return value
106 // opdata[3] points to number of args
107 // opdata[4] points to beginning of arguments in
108 void SeExpr2LLVMEvalCustomFunction(int *opDataArg,
109  double *fpArg,
110  char **strArg,
111  void **funcdata,
112  const SeExpr2::ExprFuncNode *node) {
113  const SeExpr2::ExprFunc *func = node->func();
114  SeExpr2::ExprFuncX *funcX = const_cast<SeExpr2::ExprFuncX *>(func->funcx());
115  SeExpr2::ExprFuncSimple *funcSimple = static_cast<SeExpr2::ExprFuncSimple *>(funcX);
116 
117  strArg[0]= reinterpret_cast<char *>(funcSimple);
118 
119  std::vector<int> callStack;
120  SeExpr2::ExprFuncSimple::ArgHandle handle(opDataArg, fpArg, strArg, callStack);
121  if (!*funcdata) {
122  handle.data = funcSimple->evalConstant(node, handle);
123  *funcdata = reinterpret_cast<void *>(handle.data);
124  } else {
125  handle.data = reinterpret_cast<SeExpr2::ExprFuncNode::Data *>(*funcdata);
126  }
127 
128  funcSimple->eval(handle);
129  //for (int i = 0; i < retSize; ++i) result[i] = fp[1 + i];
130 }
131 }
std::vector< int > callStack
Definition: Interpreter.h:57
int allocFP(int n)
! Allocate a floating point set of data of dimension n
Definition: Interpreter.h:104
const ExprType & type() const
The type of the node.
Definition: ExprNode.h:145
virtual void eval(ArgHandle args)=0
int dim() const
Definition: ExprType.h:160
int nextPC()
Return the position that the next instruction will be placed at.
Definition: Interpreter.h:70
int promote(int i) const
Definition: ExprNode.h:584
const ExprFuncX * funcx() const
return pointer to the funcx
Definition: ExprFunc.h:125
std::vector< char * > s
constant and evaluated pointer data
Definition: Interpreter.h:45
bool isFP() const
Direct is predicate checks.
Definition: ExprType.h:164
bool isString() const
Definition: ExprType.h:169
Function Definition, used in parse tree and func table.
Definition: ExprFunc.h:44
virtual ExprFuncNode::Data * evalConstant(const ExprFuncNode *node, ArgHandle args) const =0
ExprFuncNode::Data * data
Definition: ExprFuncX.h:99
void SeExpr2LLVMEvalCustomFunction(int *opDataArg, double *fpArg, char **strArg, void **funcdata, const SeExpr2::ExprFuncNode *node)
Definition: ExprFuncX.cpp:108
int allocPtr()
Allocate a pointer location (can be anything, but typically space for char*)
Definition: Interpreter.h:111
virtual int buildInterpreter(Interpreter *interpreter) const
builds an interpreter. Returns the location index for the evaluated data
Node that calls a function.
Definition: ExprNode.h:514
std::vector< std::pair< OpF, int > > ops
Definition: Interpreter.h:56
int addOperand(int param)
! Adds an operand. Note this should be done after doing the addOp!
Definition: Interpreter.h:96
int addOp(OpF op)
! adds an operator to the program (pointing to the data at the current location)
Definition: Interpreter.h:73
std::vector< double > d
Double data (constants and evaluated)
Definition: Interpreter.h:43
base class for custom instance data
Definition: ExprNode.h:564
int numChildren() const
Number of children.
Definition: ExprNode.h:114
void endOp(bool execute=true)
Definition: Interpreter.h:83
const ExprFunc * func() const
Definition: ExprNode.h:585
std::vector< int > opData
Ooperands to op.
Definition: Interpreter.h:47
static int EvalOp(int *opData, double *fp, char **c, std::vector< int > &callStack)
Definition: ExprFuncX.cpp:24
virtual int buildInterpreter(const ExprFuncNode *node, Interpreter *interpreter) const
Build an interpreter to evaluate the expression.
Definition: ExprFuncX.cpp:32
const ExprNode * child(size_t i) const
Get 0 indexed child.
Definition: ExprNode.h:117
Extension function spec, used for complicated argument custom functions.
Definition: ExprFuncX.h:35