CompileDES  3.13
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-2019 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 
173  virtual void Compile(void);
174 
179  static std::string VersionString(void);
180 
181 
186  /*****************************************
187  *****************************************
188  *****************************************
189  *****************************************/
190 
206  Idx Size(void) const;
207 
222  void Insert(const std::string& file);
223 
234  void Insert(const TimedGenerator& rGen);
235 
237  const TimedGenerator& At(int i) const { return mGenerators.at(i); };
238 
240  typedef std::vector<TimedGenerator>::const_iterator Iterator;
241 
243  Iterator Begin(void) const { return mGenerators.begin(); };
244 
246  Iterator End(void) const { return mGenerators.end(); };
247 
248 
251  /*****************************************
252  *****************************************
253  *****************************************
254  *****************************************/
255 
278  Idx EventIndex(const std::string& rName) const { return mAlphabet.Index(rName); };
279 
289  std::string EventName(Idx index) const { return mAlphabet.SymbolicName(index); };
290 
291 
301 
313  void EventAttribute(Idx ev, const AttributeCodeGeneratorEvent& attr);
314 
323  void Alphabet(const cgEventSet& rAlphabet);
324 
331  const cgEventSet& Alphabet(void) const { return mAlphabet; };
332 
336  /*****************************************
337  *****************************************
338  *****************************************
339  *****************************************/
340 
373  virtual int EventTargetIdx(Idx idx);
374 
376  virtual int EventTargetIdx(const std::string& ev);
377 
379  int EventBitAddress(Idx idx);
380 
382  Idx EventFaudesIdx(int idx);
383 
385  std::vector<bool> EventBitMask(Idx idx);
386 
388  std::vector<bool> EventBitMask(const EventSet& eset);
389 
391  int EventBitMaskSize(void);
392 
394  typedef unsigned long word_t;
395 
397  word_t WordFromBitVector(const std::vector<bool>& vect, int wordindex);
398 
400  std::vector< word_t > WordVectorFromBitVector(const std::vector<bool>& vect);
401 
403  std::vector<int> mWordAddressVector;
404 
406  std::vector<word_t> mBitMaskVector;
407 
411  /*****************************************
412  *****************************************
413  *****************************************
414  *****************************************/
415 
451  const std::vector<int>& TransitionVector(size_t git);
452 
454  virtual int StateTargetIdx(size_t git, Idx idx);
455 
457  virtual Idx StateFaudesIdx(size_t git, int idx);
458 
472  struct LineAddress {
473  std::string mAddress;
475  EventSet mPosEvents;
476  EventSet mNegEvents;
477  EventSet mPosStatics;
478  EventSet mNegStatics;
479  bool mStatic;
480  };
482  typedef std::map<std::string, LineAddress>::iterator LineIterator ;
484  LineIterator LinesBegin();
486  LineIterator LinesEnd();
487 
489  struct FlagExpression {
491  std::string mAddress;
493  EventSet mEvents;
494  };
495  // access to flag data
497  typedef std::map<std::string, FlagExpression>::iterator FlagIterator ;
499  FlagIterator FlagsBegin();
501  FlagIterator FlagsEnd();
502 
506  std::string mAddress;
508  std::string mElapseEvent;
510  std::string mInitialValue;
512  EventSet mStartEvents;
514  EventSet mStopEvents;
516  EventSet mResetEvents;
517  };
519  typedef std::map<std::string, TimerConfiguration>::iterator TimerIterator ;
521  TimerIterator TimersBegin();
523  TimerIterator TimersEnd();
524 
538  struct ActionAddress {
540  std::string mAddress;
542  bool mSetClr;
544  bool mExe;
545  };
547  typedef std::map<std::string, ActionAddress>::iterator ActionAddressIterator ;
549  ActionAddressIterator ActionAddressesBegin();
551  ActionAddressIterator ActionAddressesEnd();
552 
554  class TimerAction {
555  public:
557  std::set< std::string > mTimerStarts;
559  std::set< std::string > mTimerStops;
561  std::set< std::string > mTimerResets;
563  bool operator==(const TimerAction& other) {
564  if(!(mTimerStarts==other.mTimerStarts)) return false;
565  if(!(mTimerStops==other.mTimerStops)) return false;
566  if(!(mTimerResets==other.mTimerResets)) return false;
567  return true;
568  }
570  bool operator<(const TimerAction& other) const {
571  if(mTimerStarts<other.mTimerStarts) return true;
572  if(mTimerStarts>other.mTimerStarts) return false;
573  if(mTimerResets<other.mTimerResets) return true;
574  if(mTimerResets>other.mTimerResets) return false;
575  if(mTimerStops<other.mTimerStops) return true;
576  return false;
577  }
578  };
580  typedef std::map<std::string, TimerAction>::iterator TimerActionIterator ;
582  TimerActionIterator TimerActionsBegin();
584  TimerActionIterator TimerActionsEnd();
585 
586 
592  /*****************************************
593  *****************************************
594  *****************************************
595  *****************************************/
596 
621  virtual void Generate(void);
622 
632  void Verbose(int level, std::ostream* altout=0);
633 
651  virtual void OutputMode(const std::string& mode) {mOutMode=mode;};
652 
653 
660  std::string OutputMode(void) { return mOutMode; }
661 
662 
676  virtual std::ostream& Output(void);
677 
678 
690  const std::string& OutputString(void);
691 
692 
700  void OutputString(const std::string& strbuf);
701 
702 
716  virtual void MuteMode(char mode);
717 
725  virtual void MuteCond(char mode);
726 
728  virtual void LineFeed(int lines=1);
729 
731  virtual std::string LineCount(void);
732 
734  virtual void IndentInc();
735 
737  virtual void IndentDec();
738 
740  virtual void Comment(const std::string& text);
741 
743  std::string RecentComment(void) {return mRecentMutedComment;};
744 
746  virtual void XmlTextEscape(bool on);
747 
749  virtual void XmlCdataEscape(bool on);
750 
752  virtual void MuteComments(bool on);
753 
755  virtual void MuteVspace(bool on);
756 
759  /*****************************************
760  *****************************************
761  *****************************************
762  *****************************************/
763 
798  static void Register(const std::string& type, CodeGenerator* (*newcg)(void));
799 
806  static std::vector< std::string > Registry(void);
807 
816  static CodeGenerator* New(const std::string& type);
817 
822  template< class T > class Registration {
823  public:
824  Registration(const std::string& type) { CodeGenerator::Register(type,&New);};
825  static CodeGenerator* New(void) { return new T();};
826  };
827 
831  #define FAUDES_REGISTERCODEGENERATOR(ftype,ctype) static CodeGenerator::Registration<ctype> gRegister(ftype)
832 
836  /*****************************************
837  *****************************************
838  *****************************************
839  *****************************************/
840 
841 protected:
842 
844  std::string mName;
845 
847  std::vector<TimedGenerator> mGenerators;
848 
850  std::vector<std::string> mGeneratorNames;
851 
854 
857 
860 
862  std::map<Idx,int> mEventBitAddress;
863 
865  std::map<int,Idx> mEventFaudesIdx;
866 
869 
872 
874  std::vector< std::map<Idx,int> > mStateVectorAddress;
875 
877  std::vector< std::map<int,Idx> > mStateFaudesIndex;
878 
880  std::vector< bool > mUsingVectorAddressStates;
881 
883  std::vector< std::vector<int> > mTransitionVector;
884 
886  EventSet mUsedEvents;
887 
889  EventSet mOutputEvents;
890 
892  EventSet mInputEvents;
893 
895  EventSet mInternalEvents;
896 
898  std::map<std::string, LineAddress> mLines;
899 
901  std::map<std::string, FlagExpression> mFlags;
902 
904  std::map<std::string, TimerConfiguration> mTimers;
905 
907  std::map<std::string, ActionAddress> mActionAddresses;
908 
910  std::map<std::string, TimerAction> mTimerActions;
911 
914 
916  std::string mOutMode;
917 
919  char mMuteMode;
920 
923 
925  std::string mRecentMutedComment;
926 
928  std::ostream* pOutStream;
929 
931  std::ostream* pErrStream;
932 
934  virtual void DoCompile(void);
935 
937  virtual void DoGenerate(void) = 0;
938 
957  virtual void DoRead(TokenReader& rTr,const std::string& rLabel="", const Type* pContext=0);
958 
959 
979  virtual void DoWrite(TokenWriter& rTw,const std::string& rLabel="", const Type* pContext=0) const;
980 
991  virtual void DoReadTargetConfiguration(TokenReader& rTr);
992 
1002  virtual void DoWriteTargetConfiguration(TokenWriter& rTw) const;
1003 
1004 private:
1005 
1006  // private data: code generator type registry
1007  static std::map< std::string, CodeGenerator* (*)(void) >* mpsRegistry;
1008 
1009  // private method: code organisation: read generators
1010  void DoReadGenerators(TokenReader& rTr);
1011 
1012  // private method: code organisation: write generators
1013  void DoWriteGenerators(TokenWriter& rTw) const;
1014 
1015 
1016  // cheap filtering streambuf for line-count/indentation/xml-escape
1017  class cgp_streambuf : public std::streambuf {
1018  public:
1019  // construct by dst filename (alt. "std::string" for string-buffer only, "std::cout" for console),
1020  cgp_streambuf(std::string mode);
1021  // destruct
1022  ~cgp_streambuf();
1023  // report
1024  int LineCount();
1025  // Indentation
1026  void IndentInc();
1027  void IndentDec();
1028  // control
1029  void Mute(bool on);
1030  bool Mute(void);
1031  void MuteVspace(bool on);
1032  void XmlTextEscape(bool on);
1033  void XmlCdataEscape(bool on);
1034  void Flush();
1035  // access
1036  const std::string& Buffer();
1037  void Clear(void);
1038 
1039  protected:
1040  // use overflow hook to see individual characters one by one
1041  // this is a perhaps inefficient but cheap way to build a filter
1042  // --- implementation assumes "char-8bit-ascii" output
1043  virtual int_type overflow (int_type c);
1044  // my config data
1045  std::string mBuffer;
1046  std::ostream* pOutStream;
1047  std::ofstream* mpFOutStream;
1048  std::stringstream* mpSOutStream;
1049  std::string mMode;
1050  int mLines;
1051  bool mBeginOfLine;
1052  int mIndent;
1053  bool mMute;
1054  bool mMuteVspace;
1055  bool mTextEscape;
1056  bool mCdataEscape;
1057 
1058  };
1059 
1060  // private instance
1061  cgp_streambuf* pOutBuffer;
1062 
1063 
1064 };
1065 
1066 
1067 #endif
Event attributes (execution semantics)
TaNameSet< AttributeCodeGeneratorEvent > cgEventSet
Convenience typedef: eventset with code-generator config attributes.
Event attributes for the purpose of code generation.
Compiled record per event on how it affects timers.
bool operator==(const TimerAction &other)
need equality operator for STL
bool operator<(const TimerAction &other) const
need compare operator for STL (should perhaps use hashes for performance)
std::set< std::string > mTimerResets
timers to reset
std::set< std::string > mTimerStarts
timers to start
std::set< std::string > mTimerStops
timers to stop
Code-generation common base.
std::string mOutMode
output file name (base)
char mMuteMode
current output mode
std::string mAddress
target expression to represent the line value
std::string EventName(Idx index) const
Faudes-event name lookup.
std::map< Idx, int > mEventBitAddress
mapping from faudes event idx to bit address (descending priority, range 0 .
std::vector< TimedGenerator >::const_iterator Iterator
Iterator for read-only access of generators.
std::string OutputMode(void)
Report code output mode.
EventSet mStartEvents
start events as in AttributeCodeGeneratorEvent
std::vector< int > mWordAddressVector
Look-up table to map a bit-address to the word-index.
std::vector< word_t > mBitMaskVector
Look-up table to map a bit-address to the word-bitmask.
int mLastInputEvent
highest bit-address with input (or timer) event (-1 for none)
std::vector< std::map< int, Idx > > mStateFaudesIndex
mapping from vector state idx to faudes index
const AttributeCodeGeneratorEvent & EventAttribute(Idx ev) const
Event configuration attribute lookup.
std::string RecentComment(void)
Recent muted comment (convenience support for derived classes)
virtual void OutputMode(const std::string &mode)
Set code output mode.
std::map< std::string, LineAddress >::iterator LineIterator
Access to line records by iterator.
virtual void DoGenerate(void)=0
pure virtual interface to code generation
EventSet mPosEvents
bit-address of buffer for edge detection (consecutive)
std::ostream * pErrStream
error stream
std::string mRecentMutedComment
recent muted comment
std::vector< std::vector< int > > mTransitionVector
compiled transition-sets, represented as vectors of integers with 0 as separator
EventSet mInternalEvents
used events that are configured as internal events (excl.
int mLastOutputEvent
highest bit-address with output event (-1 for none)
std::string mAddress
target address as specified in configuration
static void Register(const std::string &type, CodeGenerator *(*newcg)(void))
Insert derived class in the registry.
void Alphabet(const cgEventSet &rAlphabet)
Set all event attributes.
bool mStatic
events to trigger on low power on
EventSet mStopEvents
stop events as in AttributeCodeGeneratorEvent
bool mExe
is an executable type of address
EventSet mOutputEvents
used events that are configured as outputs
std::string mInitialValue
target representation of initial value
Idx EventIndex(const std::string &rName) const
Faudes-event index lookup.
std::map< std::string, TimerConfiguration >::iterator TimerIterator
Access to timer records by iterator.
std::map< std::string, LineAddress > mLines
input event generation
std::string mElapseEvent
event to trigger on timer elapse
int mWordSize
compressed boolean capacity of target type word
std::map< int, Idx > mEventFaudesIdx
mapping from bit address to faudes event idx
EventSet mEvents
events to trigger on positive evaluation
EventSet mPosStatics
events to trigger on negative edge
std::ostream * pOutStream
output stream
std::vector< TimedGenerator > mGenerators
list of executors
bool mMuteComments
mute comments
int mVerbLevel
diagnpstic-output level
int mIntegerSize
compressed boolean capacity of target type integer
std::map< std::string, ActionAddress > mActionAddresses
action addresses
EventSet mInputEvents
used events that are configured as inputs (incl timer)
const cgEventSet & Alphabet(void) const
Access alphabet (incl event attributes)
std::map< std::string, TimerAction > mTimerActions
timer actions by event name
std::map< std::string, FlagExpression >::iterator FlagIterator
Access to flag records by iterator.
Iterator Begin(void) const
Begin-iterator for read-only access of generators.
std::vector< std::map< Idx, int > > mStateVectorAddress
mapping from faudes state idx to vector index
cgEventSet mAlphabet
event configuration by attributes
bool mSetClr
is a set/clr type of address
EventSet mResetEvents
reset events as in AttributeCodeGeneratorEvent
Iterator End(void) const
End-iterator for read-only access of generators.
virtual void Name(const std::string &rName)
Set objects's name (reimplementing base faudes::Type)
virtual const std::string & Name(void) const
Get objects's name (reimplementing base faudes::Type)
EventSet mNegEvents
events to trigger on positive edge
void EventAttribute(Idx ev, const AttributeCodeGeneratorEvent &attr)
Set event attribute.
EventSet mNegStatics
events to trigger on high power on
std::vector< std::string > mGeneratorNames
list of filenames when generator are read from file
std::vector< bool > mUsingVectorAddressStates
configuration of state indexing per generator
std::map< std::string, TimerConfiguration > mTimers
timer definitions
const TimedGenerator & At(int i) const
Direct access for read-only access of generators.
int mBitAddress
target expression to represent line value
std::map< std::string, ActionAddress >::iterator ActionAddressIterator
Access to action record by iterator.
std::string mName
faudes object name (aka project name)
std::map< std::string, FlagExpression > mFlags
input event generation
unsigned long word_t
Code-generator internal data type of target words.
EventSet mUsedEvents
configured events that are referred to by some generator
std::string mAddress
target address prefix to maintain timer state
std::map< std::string, TimerAction >::iterator TimerActionIterator
Access to timer records by iterator.
Compiled record per action-address.
Compiled record per input flag-expression.
Compiled record per input line.
Compiled record per timer acting.