8 #ifndef RRLLVMGETEVENTVALUECODEGENBASE_H_
9 #define RRLLVMGETEVENTVALUECODEGENBASE_H_
11 #include "CodeGenBase.h"
12 #include "ModelGeneratorContext.h"
13 #include "SymbolForest.h"
14 #include "ASTNodeCodeGen.h"
15 #include "ASTNodeFactory.h"
16 #include "ModelDataIRBuilder.h"
17 #include "ModelDataSymbolResolver.h"
18 #include "LLVMException.h"
20 #include <sbml/Model.h>
21 #include <Poco/Logger.h>
28 typedef double (*GetEventValueCodeGenBase_FunctionPtr)(LLVMModelData*, size_t);
33 template <
typename Derived,
typename
34 FunctionPtrType = GetEventValueCodeGenBase_FunctionPtr>
46 llvm::Value* codeGen();
48 typedef FunctionPtrType FunctionPtr;
56 return llvm::Type::getDoubleTy(this->context);
64 return value ? value :
65 llvm::ConstantFP::get(this->context, llvm::APFloat(123.456));
70 template <
typename Derived,
typename FunctionPtrType>
71 llvm::Value* GetEventValueCodeGenBase<Derived, FunctionPtrType>::codeGen()
74 llvm::Type* argTypes[] = {
76 llvm::Type::getInt32Ty(this->context)
79 const char* argNames[] = {
80 "modelData", Derived::IndexArgName
83 llvm::Value* args[] = { 0, 0 };
85 llvm::Type* retType =
static_cast<Derived*
>(
this)->getRetType();
87 llvm::BasicBlock* entry = this->codeGenHeader(Derived::FunctionName, retType,
88 argTypes, argNames, args);
90 const libsbml::ListOfEvents* events = this->model->getListOfEvents();
92 ModelDataLoadSymbolResolver resolver(args[0], this->modelGenContext);
94 ASTNodeCodeGen astCodeGen(this->builder, resolver, this->modelGenContext, args[0]);
97 llvm::BasicBlock* def = llvm::BasicBlock::Create(this->context,
"default", this->
function);
98 this->builder.SetInsertPoint(def);
100 llvm::Value* defRet =
static_cast<Derived*
>(
this)->createRet(0);
101 this->builder.CreateRet(defRet);
105 this->builder.SetInsertPoint(entry);
107 llvm::SwitchInst* s = this->builder.CreateSwitch(args[1], def, events->size());
109 for (uint i = 0; i < events->size(); ++i)
112 std::sprintf(block_name,
"event_%i_block", i);
113 llvm::BasicBlock* block = llvm::BasicBlock::Create(this->context, block_name, this->
function);
114 this->builder.SetInsertPoint(block);
115 resolver.flushCache();
117 const libsbml::Event*
event = events->get(i);
119 llvm::Value* value =
static_cast<Derived*
>(
this)->getMath(event, astCodeGen);
122 value =
static_cast<Derived*
>(
this)->createRet(value);
124 this->builder.CreateRet(value);
125 s->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(this->context), i), block);
128 return this->verifyFunction();
131 typedef unsigned char (*GetEventTriggerCodeGen_FunctionPtr)(LLVMModelData*, size_t);
138 GetEventTriggerCodeGen_FunctionPtr>
144 llvm::Value* getMath(
const libsbml::Event*,
ASTNodeCodeGen& astCodeGen);
146 static const char* FunctionName;
147 static const char* IndexArgName;
149 llvm::Type* getRetType();
151 llvm::Value* createRet(llvm::Value*);
164 llvm::Value* getMath(
const libsbml::Event*,
ASTNodeCodeGen& astCodeGen);
166 static const char* FunctionName;
167 static const char* IndexArgName;
170 libsbml::ASTNode* node;
183 llvm::Value* getMath(
const libsbml::Event*,
ASTNodeCodeGen& astCodeGen);
185 llvm::Value* createRet(llvm::Value* value)
189 return llvm::ConstantFP::get(this->context, llvm::APFloat(123.456));
191 if (value->getType() == llvm::Type::getDoubleTy(context))
194 return this->builder.CreateCast(llvm::Instruction::CastOps::UIToFP, value, llvm::Type::getDoubleTy(context));
197 static const char* FunctionName;
198 static const char* IndexArgName;
200 libsbml::ASTNode* node;
All of the LLVM IR generation is handled here.
Definition: ASTNodeCodeGen.h:31
a convenience class to pull the vars out of a context, and store them as ivars.
Definition: CodeGenBase.h:54
Get the delay of an SBML Event.
Definition: GetEventValuesCodeGen.h:178
Get the priority of an SBML Event.
Definition: GetEventValuesCodeGen.h:159
Get the trigger of an SBML Event.
Definition: GetEventValuesCodeGen.h:139
Base class for getting event values.
Definition: GetEventValuesCodeGen.h:37
llvm::Type * getRetType()
default ret type is double, derived classes must override if using non-default func sic
Definition: GetEventValuesCodeGen.h:54
llvm::Value * createRet(llvm::Value *value)
create a return type, a zero value should return the default type
Definition: GetEventValuesCodeGen.h:62
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