7 #ifndef RRLLVMGETVALUECODEGENBASE_H_
8 #define RRLLVMGETVALUECODEGENBASE_H_
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"
18 #include <sbml/Model.h>
19 #include <Poco/Logger.h>
25 typedef double (*GetValueCodeGenBase_FunctionPtr)(LLVMModelData*, size_t);
31 template <
typename Derived,
bool substanceUnits>
39 llvm::Value* codeGen();
41 typedef GetValueCodeGenBase_FunctionPtr FunctionPtr;
45 template <
typename Derived,
bool substanceUnits>
52 template <
typename Derived,
bool substanceUnits>
53 GetValueCodeGenBase<Derived, substanceUnits>::~GetValueCodeGenBase()
57 template <
typename Derived,
bool substanceUnits>
58 llvm::Value* GetValueCodeGenBase<Derived, substanceUnits>::codeGen()
61 llvm::Type* argTypes[] = {
63 llvm::Type::getInt32Ty(this->context)
66 const char* argNames[] = {
67 "modelData", Derived::IndexArgName
70 llvm::Value* args[] = { 0, 0 };
72 llvm::BasicBlock* entry = this->codeGenHeader(Derived::FunctionName, llvm::Type::getDoubleTy(this->context),
73 argTypes, argNames, args);
75 std::vector<std::string> ids =
static_cast<Derived*
>(
this)->getIds();
77 ModelDataLoadSymbolResolver resolver(args[0], this->modelGenContext);
79 llvm::BasicBlock* def = llvm::BasicBlock::Create(this->context,
"default", this->
function);
80 this->builder.SetInsertPoint(def);
82 this->builder.CreateRet(llvm::ConstantFP::get(this->context,
83 llvm::APFloat::getQNaN(llvm::APFloat::IEEEdouble())));
87 this->builder.SetInsertPoint(entry);
89 llvm::SwitchInst* s = this->builder.CreateSwitch(args[1], def,
static_cast<unsigned int>(ids.size()));
91 for (
int i = 0; i < ids.size(); ++i)
93 llvm::BasicBlock* block = llvm::BasicBlock::Create(this->context, ids[i] +
"_block", this->
function);
94 this->builder.SetInsertPoint(block);
95 resolver.flushCache();
98 llvm::Value* value = resolver.loadSymbolValue(ids[i]);
102 const libsbml::SBase* sbase =
const_cast<libsbml::Model*
>(this->model)->getElementBySId(ids[i]);
103 if (sbase && sbase->getTypeCode() == libsbml::SBML_SPECIES)
105 const libsbml::Species* species =
static_cast<const libsbml::Species*
>(sbase);
106 if (species->getHasOnlySubstanceUnits())
108 value->setName(ids[i] +
"_amt");
113 llvm::Value* comp = resolver.loadSymbolValue(species->getCompartment());
114 value = this->builder.CreateFDiv(value, comp, ids[i] +
"_conc");
119 value->setName(ids[i] +
"_conc");
124 llvm::Value* comp = resolver.loadSymbolValue(species->getCompartment());
125 value = this->builder.CreateFMul(value, comp, ids[i] +
"_amt");
131 value->setName(ids[i] +
"_value");
133 this->builder.CreateRet(value);
134 s->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(this->context), i), block);
137 return this->verifyFunction();
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