roadrunner  2.6.0
Fast simulator for SBML models
EventCodeGenBase.h
1 /*
2  * EventCodeGenBase.h
3  *
4  * Created on: Aug 11, 2013
5  * Author: andy
6  */
7 
8 #ifndef EVENTCODEGENBASE_H_
9 #define EVENTCODEGENBASE_H_
10 
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"
19 #include "rrLogger.h"
20 #include <sbml/Model.h>
21 #include <Poco/Logger.h>
22 #include <vector>
23 #include <cstdio>
24 
25 namespace rrllvm
26 {
27 
28  typedef void (*EventCodeGenBase_FunctionPtr)(LLVMModelData*, size_t, double*);
29 
33  template <typename Derived>
35  public CodeGenBase<EventCodeGenBase_FunctionPtr>
36  {
37  public:
40  {
41  };
42 
43  virtual ~EventCodeGenBase() {};
44 
45  llvm::Value* codeGen();
46 
57  bool eventCodeGen(llvm::Value* modelData, llvm::Value* data,
58  const libsbml::Event* event)
59  {
60  return false;
61  };
62 
63  typedef EventCodeGenBase_FunctionPtr FunctionPtr;
64  };
65 
66  template <typename Derived>
67  llvm::Value* EventCodeGenBase<Derived>::codeGen()
68  {
69  // make the set init value function
70  llvm::Type* argTypes[] = {
71  llvm::PointerType::get(ModelDataIRBuilder::getStructType(this->module), 0),
72  llvm::Type::getInt32Ty(this->context),
73  llvm::Type::getDoublePtrTy(this->context)
74  };
75 
76  const char* argNames[] = {
77  "modelData", "eventIndx", "data"
78  };
79 
80  llvm::Value* args[] = { 0, 0, 0 };
81 
82  llvm::Type* retType = llvm::Type::getVoidTy(context);
83 
84  llvm::BasicBlock* entry = this->codeGenHeader(Derived::FunctionName, retType,
85  argTypes, argNames, args);
86 
87  const libsbml::ListOfEvents* events = this->model->getListOfEvents();
88 
89  // default, return NaN
90  llvm::BasicBlock* def = llvm::BasicBlock::Create(this->context, "default", this->function);
91  this->builder.SetInsertPoint(def);
92 
93  this->builder.CreateRetVoid();
94 
95  // write the switch at the func entry point, the switch is also the
96  // entry block terminator
97  this->builder.SetInsertPoint(entry);
98 
99  llvm::SwitchInst* s = this->builder.CreateSwitch(args[1], def, events->size());
100 
101  for (uint i = 0; i < events->size(); ++i)
102  {
103  char block_name[64];
104  std::sprintf(block_name, "event_%i_block", i);
105  llvm::BasicBlock* block = llvm::BasicBlock::Create(this->context, block_name, this->function);
106  this->builder.SetInsertPoint(block);
107 
108  const libsbml::Event* event = events->get(i);
109 
110  bool cont = static_cast<Derived*>(this)->eventCodeGen(args[0], args[2], event);
111 
112  this->builder.CreateRetVoid();
113  s->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(this->context), i), block);
114 
115  if (!cont) break;
116  }
117 
118  return this->verifyFunction();
119  }
120 
121 }
122 
123 #endif /* EVENTCODEGENBASE_H_ */
a convenience class to pull the vars out of a context, and store them as ivars.
Definition: CodeGenBase.h:54
Base class for evaluating various types of SBML events.
Definition: EventCodeGenBase.h:36
bool eventCodeGen(llvm::Value *modelData, llvm::Value *data, const libsbml::Event *event)
derived classes must implement this method to generate the event trigger / assignment code.
Definition: EventCodeGenBase.h:57
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