CompileDES  3.12
Executable-Code Generation from Synchronised libFAUDES Automata
cgp_codeprimitives.h
Go to the documentation of this file.
1 
3 /*
4  FAU Discrete Event Systems Library (libFAUDES)
5 
6  Copyright (C) 2010, 2016, 2017 Thomas Moor
7 
8 */
9 
10 
11 
12 #ifndef FAUDES_CODEPRIMITIVES_H
13 #define FAUDES_CODEPRIMITIVES_H
14 
15 #include "libfaudes.h"
16 #include "cgp_codegenerator.h"
17 
18 using namespace faudes;
19 
20 
21 
249 
250 public:
251 
252  /*****************************************
253  *****************************************
254  *****************************************
255  *****************************************/
256 
268  CodePrimitives(void);
269 
273  virtual ~CodePrimitives(void);
274 
279  virtual void Clear(void);
280 
283 protected:
284 
296  virtual void LiteralPrepend(void);
297 
299  virtual void DeclareStatus(void);
300 
302  virtual void DeclareReset(void);
303 
305  virtual void DeclareRecentEvent(void);
306 
308  virtual void DeclareParallelState(void);
309 
311  virtual void DeclarePendingEvents(void);
312 
314  virtual void DeclareLoopState(void);
315 
317  virtual void DeclareTimers(void);
318 
320  virtual void DeclareAux(void);
321 
323  virtual void DeclareLargeCarray(void);
324 
326  virtual void DeclareSmallCarray(void);
327 
329  virtual void DeclareEventNameLookup(void);
330 
332  virtual void DeclareStateNameLookup(void);
333 
335  virtual void ResetState(void);
336 
338  virtual void ResetReturn(void);
339 
341  virtual void SenseInputs(void);
342 
344  virtual void SenseTimerElapse(void);
345 
347  virtual void BeginExecutionLoop(void);
348 
350  virtual void UpdateEnabled(void);
351 
353  virtual void ScheduleEvent(void);
354 
356  virtual void BreakExecutionLoop(void);
357 
359  virtual void ExecuteEvent(void);
360 
362  virtual void OperateTimers(void);
363 
365  virtual void OperateOutputs(void);
366 
368  virtual void EndExecutionLoop(void);
369 
371  virtual void LiteralAppend(void);
372 
374  virtual void InsertExecHooks(void);
375 
377  virtual void ExecuteEventBySwitching(void);
378 
380  virtual void ExecuteEventByInterpreter(void);
381 
383  virtual void UpdateEnabledBySwitching(void);
384 
386  virtual void UpdateEnabledByInterpreter(void);
387 
414  class AA : public std::string {
415  public:
416  explicit AA(void) : std::string() {};
417  explicit AA(std::string src) : std::string(src) {};
418  AA Sub(int subscript) const { return AA(std::string(*this) + "_" + ToStringInteger(subscript));};
419  };
421  class AX : public std::string {
422  public:
423  explicit AX(void) : std::string() {};
424  explicit AX(std::string src) : std::string(src) {};
425  };
427  virtual std::string TargetAddress(const AA& address)=0;
429  virtual AX TargetExpression(const AA& address)=0;
431  virtual std::string TargetSymbol(const std::string& str);
432 
469  virtual void IntegerDeclare(const AA& address) =0;
470  virtual void IntegerDeclare(const AA& address, int val) =0;
471  virtual void IntegerAssign(const AA& address, int val) =0;
472  virtual void IntegerAssign(const AA& address, const AX& expression)=0;
473  virtual void IntegerIncrement(const AA& address, int val=1)=0;
474  virtual void IntegerDecrement(const AA& address, int val=1);
475  virtual AX IntegerQuotient(const AX& expression, int val);
476  virtual AX IntegerRemainder(const AX& expression, int val);
477  virtual AX IntegerBitmask(const AX& expression);
478  virtual AX IntegerIsEq(const AA& address, int val);
479  virtual AX IntegerIsEq(const AA& address, const AX& expression);
480  virtual AX IntegerIsNotEq(const AA& address, int val);
481  virtual AX IntegerIsNotEq(const AA& address, const AX& expression);
482  virtual AX IntegerIsGreater(const AA& address, int val);
483  virtual AX IntegerIsLess(const AA& address, int val);
484  virtual AX IntegerConstant(int val)=0;
485  virtual bool HasIntmaths(void);
501  virtual void WordDeclare(const AA& address);
502  virtual void WordDeclare(const AA& address, word_t val);
503  virtual void WordAssign(const AA& address, word_t val);
504  virtual void WordAssign(const AA& address, const AX& expression);
505  virtual void WordOr(const AA& address, word_t val);
506  virtual void WordOr(const AA& address, const AX& expression);
507  virtual void WordOr(const AA& address, const AA& op1, const AA& op2);
508  virtual void WordOr(const AA& address, const AA& op1, word_t op2);
509  virtual void WordAnd(const AA& address, word_t val);
510  virtual void WordAnd(const AA& address, const AX& expression);
511  virtual void WordAnd(const AA& address, const AA& op1, const AA& op2);
512  virtual void WordAnd(const AA& address, const AA& op1, word_t op2);
513  virtual void WordNand(const AA& address, const AX& expresson);
514  virtual AX WordIsBitSet(const AA& address, int idx);
515  virtual AX WordIsBitClr(const AA& address, int idx);
516  virtual AX WordIsMaskSet(const AA& address, word_t mask);
517  virtual AX WordIsEq(const AA& address, word_t val);
518  virtual AX WordIsNotEq(const AA& address, word_t val);
519  virtual AX WordConstant(word_t val);
535  virtual void BooleanDeclare(const AA& address);
536  virtual void BooleanDeclare(const AA& address, int val);
537  virtual void BooleanAssign(const AA& address, int val);
538  virtual void BooleanAssign(const AA& address, const AX& expression);
539  virtual AX BooleanIsEq(const AA& op1, const AA& op2);
540  virtual AX BooleanIsNotEq(const AA& op1, const AA& op2);
555  virtual void CintarrayDeclare(const AA& address, int offset, const std::vector<int>& val);
556  virtual AA CintarrayAccess(const AA& address, int index);
557  virtual AA CintarrayAccess(const AA& address, const AA& indexaddr);
558  virtual bool HasCintarray(void);
573  virtual void CwordarrayDeclare(const AA& address, int offset, const std::vector<word_t>& val);
574  virtual AA CwordarrayAccess(const AA& address, int index);
575  virtual AA CwordarrayAccess(const AA& address, const AA& indexaddr);
576  virtual bool HasCwordarray(void);
590  virtual AX StringConstant(const std::string &val);
591  virtual void CstrarrayDeclare(const AA& address, int offset, const std::vector<std::string>& val);
592  virtual AA CstrarrayAccess(const AA& address, int index);
593  virtual AA CstrarrayAccess(const AA& address, const AA& indexaddr);
594  virtual bool HasCstrarray(void);
609  virtual void IntarrayDeclare(const AA& address, int offset, int len);
610  virtual void IntarrayDeclare(const AA& address, int offset, const std::vector<int>& val);
611  virtual AA IntarrayAccess(const AA& address, int index);
612  virtual AA IntarrayAccess(const AA& address, const AA& indexaddr);
613  virtual bool HasIntarray(void);
628  virtual void WordarrayDeclare(const AA& address, int offset, int len);
629  virtual void WordarrayDeclare(const AA& address, int offset, const std::vector<word_t>& val);
630  virtual AA WordarrayAccess(const AA& address, int index);
631  virtual AA WordarrayAccess(const AA& address, const AA& indexaddr);
632  virtual bool HasWordarray(void);
660  virtual void BitarrayDeclare(const AA& address, int blen);
661  virtual void BitarrayDeclare(const AA& address, const std::vector<bool>& val);
662  virtual void BitarrayAssign(const AA& address, const std::vector<bool>& val);
663  virtual void BitarrayAssign(const AA& address, const AA& otherarray);
664  virtual void BitarrayClear(const AA& address);
665  virtual void BitarrayFull(const AA& address);
666  virtual void BitarraySetBit(const AA& address, int bitaddr);
667  virtual void BitarraySetBit(const AA& address, const AA& indexaddr, int offset=0, const std::vector<bool>& hint=std::vector<bool>() );
668  virtual void BitarrayClrBit(const AA& address, int bitaddr);
669  virtual void BitarrayClrBit(const AA& address, const AA& indexaddr, int offset=0, const std::vector<bool>& hint=std::vector<bool>() );
670  virtual void BitarrayIsBitSet(const AA& address, const AA& indexaddr, const AA& result,int offset=0, const std::vector<bool>& hint=std::vector<bool>() );
671  virtual void BitarrayOr(const AA& address, const std::vector<bool>& val);
672  virtual void BitarrayOr(const AA& address, const AA& op1, const std::vector<bool>& op2);
673  virtual void BitarrayOrAllWords(const AA& address, const AA& result);
674  virtual void BitarrayAnd(const AA& address, const std::vector<bool>& val);
675  virtual void BitarrayAnd(const AA& address, const AA& otherarray);
676  virtual void BitarrayAnd(const AA& address, const AA& op1, const AA& op2);
677  virtual void BitarrayAnd(const AA& address, const AA& op1, const std::vector<bool>& op2);
678  virtual void BitarrayFindFirst(const AA& address, const AA& result, int offset=0);
694  virtual void EventSetDeclare(const AA& address);
695  virtual void EventSetDeclare(const AA& address, const EventSet& evset);
696  virtual void EventSetAssign(const AA& address, const EventSet& evset);
697  virtual void EventSetInsert(const AA& address, const EventSet& evset);
698  virtual void EventSetInsert(const AA& address, Idx ev);
699  virtual void EventSetInsert(const AA& address, const AA& evaddr);
700  virtual void EventSetInsert(const AA& address, const AA& evaddr, const EventSet& hint);
701  virtual void EventSetErase(const AA& address, const EventSet& evset);
702  virtual void EventSetErase(const AA& address, Idx ev);
703  virtual void EventSetErase(const AA& address, const AA& evaddr);
704  virtual void EventSetErase(const AA& address, const AA& evaddr, const EventSet& hint);
705  virtual void EventSetExists(const AA& address, const AA& evaddr, const AA& result, const EventSet& hint);
706  virtual void EventSetRestrict(const AA& address, const AA& otherset);
707  virtual void EventSetUnion(const AA& address, const AA& op1, const EventSet& op2);
708  virtual void EventSetIntersection(const AA& address, const AA& op1, const EventSet& op2);
709  virtual void EventSetClear(const AA& address);
710  virtual void EventSetFull(const AA& address);
711  virtual void EventSetIsNotEmpty(const AA& address, const AA& result);
712  virtual void EventSetFindHighestPriority(const AA& address, const AA& result);
728  virtual void IfTrue(const AX& expression);
729  virtual void IfFalse(const AX& expression);
730  virtual void IfWord(const AX& expression);
731  virtual void IfElseIfTrue(const AX& expression);
732  virtual void IfElse(void);
733  virtual void IfEnd(void);
734 
735  virtual void SwitchBegin(const AA& address);
736  virtual void SwitchCase(const AA& address, int val);
737  virtual void SwitchCases(const AA& address, int from, int to);
738  virtual void SwitchCases(const AA& address, const std::set< int>& vals);
739  virtual void SwitchBreak(void);
740  virtual void SwitchEnd(void);
741  virtual bool HasMultiCase(void);
742 
743  virtual void LoopBegin(void);
744  virtual void LoopBreak(const AX& expression);
745  virtual void LoopEnd(void);
746 
747  virtual void FunctionReturn(void);
757  virtual void RunActionSet(const std::string& address);
758  virtual void RunActionClr(const std::string& address);
759  virtual void RunActionExe(const AX& expression);
760 
761  virtual AX ReadInputLine(const std::string& address);
762  virtual AX InputExpression(const std::string& expression);
770  virtual void TimerDeclare(const AA& address, const std::string& litval);
771  virtual void TimerStart(const AA& address);
772  virtual void TimerStop(const AA& address);
773  virtual void TimerReset(const AA& address, const std::string& litval);
774  virtual AX TimerIsElapsed(const AA& address);
782  virtual int StateTargetIdx(size_t git, Idx idx);
783  virtual Idx StateFaudesIdx(size_t git, int idx);
784 
786  virtual void Comment(const std::string& text);
787 
789  virtual void VariableDeclare(const std::string& laddr, const std::string& ltype);
790  virtual void VariableDeclare(const std::string& laddr, const std::string& ltype, const std::string& lval);
791 
792 
795  /*****************************************
796  *****************************************
797  *****************************************
798  *****************************************/
799 
800  protected:
801 
803  std::string mWordType;
804 
806  std::string mIntegerType;
807 
809  std::string mPrefix;
810 
813 
816 
819 
822 
825 
828 
831 
834 
837 
840 
843 
846 
849 
851  std::vector<bool> mHasStateNames;
852 
855 
857  std::string mEventExecutionHook;
858 
860  std::string mStateUpdateHook;
861 
863  std::string mLiteralPrepend;
864 
866  std::string mLiteralAppend;
867 
873  struct bitarray_rec {
875  int blen;
877  std::vector<bool> value;
878  };
880  std::map<std::string,bitarray_rec> mBitarrays;
881 
883  virtual void DoCompile(void);
884 
886  virtual void DoGenerate(void);
887 
889  virtual void DoGenerateDeclarations(void);
890 
892  virtual void DoGenerateResetCode(void);
893 
895  virtual void DoGenerateCyclicCode(void);
896 
898  virtual void DoReadTargetConfiguration(TokenReader& rTr);
899 
901  virtual void DoWriteTargetConfiguration(TokenWriter& rTw) const;
902 
903  private:
904 
917  template<class Data> void ConsolidateCaseRanges(std::map< int, Data>& caseData, std::map< int, int>& caseFromTo) {
918  // prepare result
919  caseFromTo.clear();
920  // bail out
921  if(caseData.empty()) return;
922  // inititialise from-to-range to first case data record
923  int fcase=caseData.begin()->first;
924  int tcase=caseData.begin()->first;
925  caseFromTo[fcase]=tcase;
926  // loop over all other cases
927  typename std::map< int, Data>::iterator cit=caseData.begin();
928  for(++cit; cit!=caseData.end(); ++cit) {
929  // current case condition
930  int ccase = cit->first;
931  // if we are meant to merge; and there is no gap; and the data matches ...
932  if(HasMultiCase())
933  if(tcase+1 == ccase)
934  if(caseData[tcase]==caseData[ccase]) {
935  // ... extend the range
936  tcase=ccase;
937  caseFromTo[fcase]=tcase;
938  continue;
939  }
940  // so we did not want extend the range ... start a new range to extend
941  fcase=ccase;
942  tcase=ccase;
943  caseFromTo[fcase]=tcase;
944  }
945  }
946 
957  template<class Data> void ConsolidateCaseSets(std::map< int, Data>& caseData, std::set< std::set< int > >& caseSets) {
958  // prepare result
959  caseSets.clear();
960  // bail out
961  if(caseData.empty()) return;
962  // if we are not meant to consolidate, produce trivial sets
963  if(!HasMultiCase()) {
964  typename std::map< int , Data >::const_iterator dit=caseData.begin();
965  for(; dit!=caseData.end(); ++dit) {
966  std::set< int > sngl;
967  sngl.insert(dit->first);
968  caseSets.insert(sngl);
969  }
970  return;
971  }
972  // setup reverse map
973  std::map< Data , std::set<int> > rmap;
974  typename std::map< int , Data >::const_iterator dit=caseData.begin();
975  for(; dit!=caseData.end(); ++dit)
976  rmap[dit->second].insert(dit->first);
977  // strip result
978  typename std::map< Data , std::set<int> >::iterator rit=rmap.begin();
979  for(; rit!=rmap.end(); ++rit)
980  caseSets.insert(rit->second);
981  }
982 
983  };
984 
985 
986 #endif
Abstract expression; see also Absstract_Addresses.
bool mExistStateNames
record whether there exist statenames at all
std::string mLiteralAppend
extra code to prepend
bool mEventsetsForPerformance
code option: eventsets for performance
bool mArrayForState
code option: use int arrays to represent that overall state
bool mArrayForBitmasks
code option: use const array to represent bit-masks
std::string mStateUpdateHook
code option: state change hook
std::vector< bool > value
initialisation value
std::string mIntegerType
target data type for integer
bool mMaintainStateIndices
code option: use state indices as provided
std::string mWordType
target data type for word
bool mBisectionForBitfind
code option: use bisection to fing lowest set bit
bool mEventNameLookup
code option: event name lookup
bool mLoopPendingInputs
code option: loop until all inputs are resolved
unsigned long word_t
Code-generator internal data type of target words.
bool mArrayForBitarray
code option: use const array to represent bit-masks
std::string mEventExecutionHook
code option: event exec hook
Abstract address; see also Absstract_Addresses.
bool mArrayForTransitions
code option: use const array to represent transitions
bool mStateNameLookup
code option: state name lookup
std::string mPrefix
universal prefix (pseudo name space)
bool mStrictEventSynchronisation
code option: strict event synchronisation
bool mLoopEnabledOutputs
code option: loop until all enabled outputs are executed
Code-generator common base class.
Record declared bit-arrays.
std::map< std::string, bitarray_rec > mBitarrays
Record of all declared bit-arrays.
std::string mLiteralPrepend
extra code to prepend
Code-generation common base.
bool mBitAddressArithmetic
code option: compute bit and word address on target
std::vector< bool > mHasStateNames
record per generator whether there is a lookup table
Execution semantics in terms of code primitives.