CompileDES  3.09
Executable-Code Generation from Synchronised libFAUDES Automata
cgp_codegenerator.h
Go to the documentation of this file.
1 
3 /*
4 
5  FAU Discrete Event Systems Library (libFAUDES)
6 
7  Copyright (C) 2010, 2016 Thomas Moor
8 
9 */
10 
11 
12 
13 #ifndef FAUDES_CODEGENERATOR_H
14 #define FAUDES_CODEGENERATOR_H
15 
16 #include "libfaudes.h"
17 #include "cgp_eventconfig.h"
18 
19 
20 using namespace faudes;
21 
22 // diagnostic macros for CodeGenerator and derived classes
23 #define FCG_VERB0(msg) { if(mVerbLevel>=0) *pErrStream << msg << std::endl; }
24 #define FCG_VERB1(msg) { if(mVerbLevel>=1) *pErrStream << msg << std::endl; }
25 #define FCG_VERB2(msg) { if(mVerbLevel>=2) *pErrStream << msg << std::endl; }
26 #define FCG_ERR(msg) { *pErrStream << msg << std::endl; throw Exception("CodeGenerator()","internal error", 500); }
27 
28 // set version string (overwrite by build system)
29 #ifndef COMPILEDES_VERSION
30 #define COMPILEDES_VERSION "3.0x"
31 #endif
32 
33 
106 class CodeGenerator : public Type {
107 
108 
109 public:
110 
111  /*****************************************
112  *****************************************
113  *****************************************
114  *****************************************/
115 
116  enum OutSink { CONSOLE, FILE, STRING };
117 
118 
133  CodeGenerator(void);
134 
138  virtual ~CodeGenerator(void);
139 
147  virtual void Clear(void);
148 
149 
156  virtual void Name(const std::string& rName) { mName=rName;};
157 
164  virtual const std::string& Name(void) const { return mName;};
165 
170  virtual void Compile(void);
171 
176  static std::string VersionString(void);
177 
178 
183  /*****************************************
184  *****************************************
185  *****************************************
186  *****************************************/
187 
203  Idx Size(void) const;
204 
219  void Insert(const std::string& file);
220 
231  void Insert(const TimedGenerator& rGen);
232 
234  const TimedGenerator& At(int i) const { return mGenerators.at(i); };
235 
237  typedef std::vector<TimedGenerator>::const_iterator Iterator;
238 
240  Iterator Begin(void) const { return mGenerators.begin(); };
241 
243  Iterator End(void) const { return mGenerators.end(); };
244 
245 
248  /*****************************************
249  *****************************************
250  *****************************************
251  *****************************************/
252 
275  Idx EventIndex(const std::string& rName) const { return mAlphabet.Index(rName); };
276 
286  std::string EventName(Idx index) const { return mAlphabet.SymbolicName(index); };
287 
288 
297  const AttributeCodeGeneratorEvent& EventAttribute(Idx ev) const;
298 
310  void EventAttribute(Idx ev, const AttributeCodeGeneratorEvent& attr);
311 
320  void Alphabet(const cgEventSet& rAlphabet);
321 
328  const cgEventSet& Alphabet(void) const { return mAlphabet; };
329 
333  /*****************************************
334  *****************************************
335  *****************************************
336  *****************************************/
337 
370  virtual int EventTargetIdx(Idx idx);
371 
373  virtual int EventTargetIdx(const std::string& ev);
374 
376  int EventBitAddress(Idx idx);
377 
379  Idx EventFaudesIdx(int idx);
380 
382  std::vector<bool> EventBitMask(Idx idx);
383 
385  std::vector<bool> EventBitMask(const EventSet& eset);
386 
388  int EventBitMaskSize(void);
389 
391  typedef unsigned long word_t;
392 
394  word_t WordFromBitVector(const std::vector<bool>& vect, int wordindex);
395 
397  std::vector< word_t > WordVectorFromBitVector(const std::vector<bool>& vect);
398 
400  std::vector<int> mWordAddressVector;
401 
403  std::vector<word_t> mBitMaskVector;
404 
408  /*****************************************
409  *****************************************
410  *****************************************
411  *****************************************/
412 
447  const std::vector<int>& TransitionVector(size_t git);
448 
450  virtual int StateTargetIdx(size_t git, Idx idx);
451 
453  virtual Idx StateFaudesIdx(size_t git, int idx);
454 
468  struct LineAddress {
469  std::string mAddress;
471  EventSet mPosEvents;
472  EventSet mNegEvents;
473  EventSet mPosStatics;
474  EventSet mNegStatics;
475  bool mStatic;
476  };
478  typedef std::map<std::string, LineAddress>::iterator LineIterator ;
480  LineIterator LinesBegin();
482  LineIterator LinesEnd();
483 
485  struct FlagExpression {
487  std::string mAddress;
489  EventSet mEvents;
490  };
491  // access to flag data
493  typedef std::map<std::string, FlagExpression>::iterator FlagIterator ;
495  FlagIterator FlagsBegin();
497  FlagIterator FlagsEnd();
498 
502  std::string mAddress;
504  std::string mElapseEvent;
506  std::string mInitialValue;
508  EventSet mStartEvents;
510  EventSet mStopEvents;
512  EventSet mResetEvents;
513  };
515  typedef std::map<std::string, TimerConfiguration>::iterator TimerIterator ;
517  TimerIterator TimersBegin();
519  TimerIterator TimersEnd();
520 
534  struct ActionAddress {
536  std::string mAddress;
538  bool mSetClr;
540  bool mExe;
541  };
543  typedef std::map<std::string, ActionAddress>::iterator ActionAddressIterator ;
545  ActionAddressIterator ActionAddressesBegin();
547  ActionAddressIterator ActionAddressesEnd();
548 
550  class TimerAction {
551  public:
553  std::set< std::string > mTimerStarts;
555  std::set< std::string > mTimerStops;
557  std::set< std::string > mTimerResets;
559  bool operator==(const TimerAction& other) {
560  if(!(mTimerStarts==other.mTimerStarts)) return false;
561  if(!(mTimerStops==other.mTimerStops)) return false;
562  if(!(mTimerResets==other.mTimerResets)) return false;
563  return true;
564  }
566  bool operator<(const TimerAction& other) const {
567  if(mTimerStarts<other.mTimerStarts) return true;
568  if(mTimerStarts>other.mTimerStarts) return false;
569  if(mTimerResets<other.mTimerResets) return true;
570  if(mTimerResets>other.mTimerResets) return false;
571  if(mTimerStops<other.mTimerStops) return true;
572  return false;
573  }
574  };
576  typedef std::map<std::string, TimerAction>::iterator TimerActionIterator ;
578  TimerActionIterator TimerActionsBegin();
580  TimerActionIterator TimerActionsEnd();
581 
582 
588  /*****************************************
589  *****************************************
590  *****************************************
591  *****************************************/
592 
617  virtual void Generate(void);
618 
628  void Verbose(int level, std::ostream* altout=0);
629 
647  virtual void OutputMode(const std::string& mode) {mOutMode=mode;};
648 
649 
656  std::string OutputMode(void) { return mOutMode; }
657 
658 
672  virtual std::ostream& Output(void);
673 
674 
686  const std::string& OutputString(void);
687 
688 
696  void OutputString(const std::string& strbuf);
697 
698 
712  virtual void MuteMode(char mode);
713 
721  virtual void MuteCond(char mode);
722 
724  virtual void LineFeed(int lines=1);
725 
727  virtual std::string LineCount(void);
728 
730  virtual void IndentInc();
731 
733  virtual void IndentDec();
734 
736  virtual void Comment(const std::string& text);
737 
739  std::string RecentComment(void) {return mRecentMutedComment;};
740 
742  virtual void XmlTextEscape(bool on);
743 
745  virtual void XmlCdataEscape(bool on);
746 
748  virtual void MuteComments(bool on);
749 
751  virtual void MuteVspace(bool on);
752 
755  /*****************************************
756  *****************************************
757  *****************************************
758  *****************************************/
759 
794  static void Register(const std::string& type, CodeGenerator* (*newcg)(void));
795 
802  static std::vector< std::string > Registry(void);
803 
812  static CodeGenerator* New(const std::string& type);
813 
818  template< class T > class Registration {
819  public:
820  Registration(const std::string& type) { CodeGenerator::Register(type,&New);};
821  static CodeGenerator* New(void) { return new T();};
822  };
823 
827  #define FAUDES_REGISTERCODEGENERATOR(ftype,ctype) static CodeGenerator::Registration<ctype> gRegister(ftype)
828 
832  /*****************************************
833  *****************************************
834  *****************************************
835  *****************************************/
836 
837 protected:
838 
840  std::string mName;
841 
843  std::vector<TimedGenerator> mGenerators;
844 
846  std::vector<std::string> mGeneratorNames;
847 
850 
853 
856 
858  std::map<Idx,int> mEventBitAddress;
859 
861  std::map<int,Idx> mEventFaudesIdx;
862 
865 
868 
870  std::vector< std::map<Idx,int> > mStateVectorAddress;
871 
873  std::vector< std::map<int,Idx> > mStateFaudesIndex;
874 
876  std::vector< std::vector<int> > mTransitionVector;
877 
879  std::vector< std::vector<int> > mStateVector;
880 
882  EventSet mUsedEvents;
883 
885  EventSet mOutputEvents;
886 
888  EventSet mInputEvents;
889 
891  EventSet mInternalEvents;
892 
894  std::map<std::string, LineAddress> mLines;
895 
897  std::map<std::string, FlagExpression> mFlags;
898 
900  std::map<std::string, TimerConfiguration> mTimers;
901 
903  std::map<std::string, ActionAddress> mActionAddresses;
904 
906  std::map<std::string, TimerAction> mTimerActions;
907 
910 
912  std::string mOutMode;
913 
915  char mMuteMode;
916 
919 
921  std::string mRecentMutedComment;
922 
924  std::ostream* pOutStream;
925 
927  std::ostream* pErrStream;
928 
930  virtual void DoCompile(void);
931 
933  virtual void DoGenerate(void) = 0;
934 
953  virtual void DoRead(TokenReader& rTr,const std::string& rLabel="", const Type* pContext=0);
954 
955 
975  virtual void DoWrite(TokenWriter& rTw,const std::string& rLabel="", const Type* pContext=0) const;
976 
987  virtual void DoReadTargetConfiguration(TokenReader& rTr);
988 
998  virtual void DoWriteTargetConfiguration(TokenWriter& rTw) const;
999 
1000 private:
1001 
1002  // private data: code generator type registry
1003  static std::map< std::string, CodeGenerator* (*)(void) >* mpsRegistry;
1004 
1005  // private method: code organisation: read generators
1006  void DoReadGenerators(TokenReader& rTr);
1007 
1008  // private method: code organisation: write generators
1009  void DoWriteGenerators(TokenWriter& rTw) const;
1010 
1011 
1012  // cheap filtering streambuf for line-count/indentation/xml-escape
1013  class cgp_streambuf : public std::streambuf {
1014  public:
1015  // construct by dst filename (alt. "std::string" for string-buffer only, "std::cout" for console),
1016  cgp_streambuf(std::string mode);
1017  // destruct
1018  ~cgp_streambuf();
1019  // report
1020  int LineCount();
1021  // Indentation
1022  void IndentInc();
1023  void IndentDec();
1024  // control
1025  void Mute(bool on);
1026  bool Mute(void);
1027  void MuteVspace(bool on);
1028  void XmlTextEscape(bool on);
1029  void XmlCdataEscape(bool on);
1030  void Flush();
1031  // access
1032  const std::string& Buffer();
1033  void Clear(void);
1034 
1035  protected:
1036  // use overflow hook to see individual characters one by one
1037  // this is a perhaps inefficient but cheap way to build a filter
1038  // --- implementation assumes "char-8bit-ascii" output
1039  virtual int_type overflow (int_type c);
1040  // my config data
1041  std::string mBuffer;
1042  std::ostream* pOutStream;
1043  std::ofstream* mpFOutStream;
1044  std::stringstream* mpSOutStream;
1045  std::string mMode;
1046  int mLines;
1047  bool mBeginOfLine;
1048  int mIndent;
1049  bool mMute;
1050  bool mMuteVspace;
1051  bool mTextEscape;
1052  bool mCdataEscape;
1053 
1054  };
1055 
1056  // private instance
1057  cgp_streambuf* pOutBuffer;
1058 
1059 
1060 };
1061 
1062 
1063 #endif