roadrunner  2.6.0
Fast simulator for SBML models
GetValueCodeGenBase.h
1 /*
2  *
3  * Created on: Jul 29, 2013
4  * Author: andy
5  */
6 
7 #ifndef RRLLVMGETVALUECODEGENBASE_H_
8 #define RRLLVMGETVALUECODEGENBASE_H_
9 
10 #include "CodeGenBase.h"
11 #include "ModelGeneratorContext.h"
12 #include "SymbolForest.h"
13 #include "ASTNodeFactory.h"
14 #include "ModelDataIRBuilder.h"
15 #include "ModelDataSymbolResolver.h"
16 #include "LLVMException.h"
17 #include "rrLogger.h"
18 #include <sbml/Model.h>
19 #include <Poco/Logger.h>
20 #include <vector>
21 
22 namespace rrllvm
23 {
24 
25  typedef double (*GetValueCodeGenBase_FunctionPtr)(LLVMModelData*, size_t);
26 
31  template <typename Derived, bool substanceUnits>
33  public CodeGenBase<GetValueCodeGenBase_FunctionPtr>
34  {
35  public:
37  virtual ~GetValueCodeGenBase();
38 
39  llvm::Value* codeGen();
40 
41  typedef GetValueCodeGenBase_FunctionPtr FunctionPtr;
42 
43  };
44 
45  template <typename Derived, bool substanceUnits>
47  const ModelGeneratorContext& mgc) :
48  CodeGenBase<GetValueCodeGenBase_FunctionPtr>(mgc)
49  {
50  }
51 
52  template <typename Derived, bool substanceUnits>
53  GetValueCodeGenBase<Derived, substanceUnits>::~GetValueCodeGenBase()
54  {
55  }
56 
57  template <typename Derived, bool substanceUnits>
58  llvm::Value* GetValueCodeGenBase<Derived, substanceUnits>::codeGen()
59  {
60  // make the set init value function
61  llvm::Type* argTypes[] = {
62  llvm::PointerType::get(ModelDataIRBuilder::getStructType(this->module), 0),
63  llvm::Type::getInt32Ty(this->context)
64  };
65 
66  const char* argNames[] = {
67  "modelData", Derived::IndexArgName
68  };
69 
70  llvm::Value* args[] = { 0, 0 };
71 
72  llvm::BasicBlock* entry = this->codeGenHeader(Derived::FunctionName, llvm::Type::getDoubleTy(this->context),
73  argTypes, argNames, args);
74 
75  std::vector<std::string> ids = static_cast<Derived*>(this)->getIds();
76 
77  ModelDataLoadSymbolResolver resolver(args[0], this->modelGenContext);
78  // default, return NaN
79  llvm::BasicBlock* def = llvm::BasicBlock::Create(this->context, "default", this->function);
80  this->builder.SetInsertPoint(def);
81 
82  this->builder.CreateRet(llvm::ConstantFP::get(this->context,
83  llvm::APFloat::getQNaN(llvm::APFloat::IEEEdouble())));
84 
85  // write the switch at the func entry point, the switch is also the
86  // entry block terminator
87  this->builder.SetInsertPoint(entry);
88 
89  llvm::SwitchInst* s = this->builder.CreateSwitch(args[1], def, static_cast<unsigned int>(ids.size()));
90 
91  for (int i = 0; i < ids.size(); ++i)
92  {
93  llvm::BasicBlock* block = llvm::BasicBlock::Create(this->context, ids[i] + "_block", this->function);
94  this->builder.SetInsertPoint(block);
95  resolver.flushCache();
96 
97  // the requested value
98  llvm::Value* value = resolver.loadSymbolValue(ids[i]);
99 
100  // need to check if we have an amount or concentration and check if we
101  // are asked for asked for an amount or concentration and convert accordingly
102  const libsbml::SBase* sbase = const_cast<libsbml::Model*>(this->model)->getElementBySId(ids[i]);
103  if (sbase && sbase->getTypeCode() == libsbml::SBML_SPECIES)
104  {
105  const libsbml::Species* species = static_cast<const libsbml::Species*>(sbase);
106  if (species->getHasOnlySubstanceUnits())
107  {
108  value->setName(ids[i] + "_amt");
109  // species is treated as an amount
110  if (!substanceUnits)
111  {
112  // convert to concentration
113  llvm::Value* comp = resolver.loadSymbolValue(species->getCompartment());
114  value = this->builder.CreateFDiv(value, comp, ids[i] + "_conc");
115  }
116  }
117  else
118  {
119  value->setName(ids[i] + "_conc");
120  // species is treated as a concentration
121  if (substanceUnits)
122  {
123  // convert to amount
124  llvm::Value* comp = resolver.loadSymbolValue(species->getCompartment());
125  value = this->builder.CreateFMul(value, comp, ids[i] + "_amt");
126  }
127  }
128  }
129  else
130  {
131  value->setName(ids[i] + "_value");
132  }
133  this->builder.CreateRet(value);
134  s->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(this->context), i), block);
135  }
136 
137  return this->verifyFunction();
138  }
139 
140 
141 } /* namespace rr */
142 
143 
144 
145 
146 #endif /* RRLLVMGETVALUECODEGENBASE_H_ */
a convenience class to pull the vars out of a context, and store them as ivars.
Definition: CodeGenBase.h:54
Base class for getting the current value of an element.
Definition: GetValueCodeGenBase.h:34
static llvm::StructType * getStructType(llvm::Module *module)
get the ModelData struct type.
Definition: ModelDataIRBuilder.cpp:731
All LLVM code generating objects basically need at a minimum three things to operate:
Definition: ModelGeneratorContext.h:95