CompileDES 3.14
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
20using 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.xx"
31#endif
32
33
106class CodeGenerator : public Type {
107
108
109public:
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
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 ;
487
491 std::string mAddress;
493 EventSet mEvents;
494 };
495 // access to flag data
497 typedef std::map<std::string, FlagExpression>::iterator FlagIterator ;
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 ;
524
540 std::string mAddress;
544 bool mExe;
545 };
547 typedef std::map<std::string, ActionAddress>::iterator ActionAddressIterator ;
552
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 ;
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
841protected:
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
890
892 EventSet mInputEvents;
893
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
920
923
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
1004private:
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)
virtual std::string LineCount(void)
LineFeed (convenience support for derived classes)
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.
virtual void LineFeed(int lines=1)
LineFeed (convenience support for derived classes)
virtual void XmlCdataEscape(bool on)
XmlCdataEscape (escape "]]>")
virtual const std::string & Name(void) const
Get objects's name (reimplementing base faudes::Type)
std::map< Idx, int > mEventBitAddress
mapping from faudes event idx to bit address (descending priority, range 0 .
int EventBitAddress(Idx idx)
Get event bit-address from faudes Idx (consecutive, starts at 0)
const TimedGenerator & At(int i) const
Direct access for read-only access of generators.
LineIterator LinesEnd()
Access to line records by iterator.
static std::vector< std::string > Registry(void)
Access registry contents.
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< bool > EventBitMask(Idx idx)
Get vector representation for a single faudes event Idx.
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)
virtual void XmlTextEscape(bool on)
XmlTextEscape (escape "<", ">", "&", "\"" and "'")
virtual void MuteComments(bool on)
Mute comments (convenience support for derived classes)
std::vector< std::map< int, Idx > > mStateFaudesIndex
mapping from vector state idx to faudes index
virtual void DoReadTargetConfiguration(TokenReader &rTr)
Reads global configuration from TokenReader, excl.
std::map< std::string, ActionAddress >::iterator ActionAddressIterator
Access to action record by iterator.
std::string RecentComment(void)
Recent muted comment (convenience support for derived classes)
virtual void OutputMode(const std::string &mode)
Set code output mode.
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
FlagIterator FlagsBegin()
Access to flag records by iterator.
int EventBitMaskSize(void)
Get overall number of events.
static CodeGenerator * New(const std::string &type)
Instantiate by identifier (returns 0 on unknown class)
TimerIterator TimersBegin()
Access to timer records by iterator.
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.
virtual void IndentInc()
Indentation (convenience support for derived classes)
virtual ~CodeGenerator(void)
Destructor.
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
TimerIterator TimersEnd()
Access to timer records by iterator.
std::string mInitialValue
target representation of initial value
Idx EventIndex(const std::string &rName) const
Faudes-event index lookup.
std::map< std::string, LineAddress > mLines
input event generation
Idx EventFaudesIdx(int idx)
Get faudes Idx from target Idx (aka from bit-address + 1)
std::string mElapseEvent
event to trigger on timer elapse
TimerActionIterator TimerActionsBegin()
Access to timer records by iterator.
int mWordSize
compressed boolean capacity of target type word
const AttributeCodeGeneratorEvent & EventAttribute(Idx ev) const
Event configuration attribute lookup.
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
virtual void DoWrite(TokenWriter &rTw, const std::string &rLabel="", const Type *pContext=0) const
Writes the configuration to TokenWriter, see faudes Type for public wrappers.
virtual void MuteMode(char mode)
Set current mute mode.
virtual void DoRead(TokenReader &rTr, const std::string &rLabel="", const Type *pContext=0)
Read the configuration from TokenReader, see faudes Type for public wrappers.
std::ostream * pOutStream
output stream
std::vector< TimedGenerator > mGenerators
list of executors
bool mMuteComments
mute comments
virtual Idx StateFaudesIdx(size_t git, int idx)
Get faudes state index (refer to vector representation as default, overload in CodePrimitives)
virtual int EventTargetIdx(Idx idx)
Get target event Idx from faudes Idx (use bit-address + 1)
LineIterator LinesBegin()
Access to line records by iterator.
int mVerbLevel
diagnpstic-output level
void Insert(const std::string &file)
Add a Generator from file.
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)
FlagIterator FlagsEnd()
Access to flag records by iterator.
std::map< std::string, TimerConfiguration >::iterator TimerIterator
Access to timer records by iterator.
virtual std::ostream & Output(void)
Output stream.
std::map< std::string, TimerAction > mTimerActions
timer actions by event name
virtual void MuteCond(char mode)
Set mode condition.
std::vector< word_t > WordVectorFromBitVector(const std::vector< bool > &vect)
Convert boolean vector to word array.
std::map< std::string, LineAddress >::iterator LineIterator
Access to line records by iterator.
void Verbose(int level, std::ostream *altout=0)
Set verbosity level.
virtual void MuteVspace(bool on)
Mute empty lines (convenience support for derived classes)
Iterator Begin(void) const
Begin-iterator for read-only access of generators.
static std::string VersionString(void)
Version (refers to macro COMPILEDES_VERSION, defined in cgp_codegenerator.h)
virtual void DoCompile(void)
virtual hook to input parameter compilation
std::vector< std::map< Idx, int > > mStateVectorAddress
mapping from faudes state idx to vector index
virtual void Compile(void)
Compile input data for alternative representation.
cgEventSet mAlphabet
event configuration by attributes
virtual void Comment(const std::string &text)
Write a comment (reimplement in derived classes, call base)
bool mSetClr
is a set/clr type of address
word_t WordFromBitVector(const std::vector< bool > &vect, int wordindex)
Extract individual word from boolean vector.
const cgEventSet & Alphabet(void) const
Access alphabet (incl event attributes)
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)
CodeGenerator(void)
Constructor.
const std::string & OutputString(void)
Get accumulated output as string.
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
virtual void Clear(void)
Clear all data.
const std::vector< int > & TransitionVector(size_t git)
Get target state index (refer to vector representation as default, overload in CodePrimitives)
std::vector< std::string > mGeneratorNames
list of filenames when generator are read from file
std::vector< bool > mUsingVectorAddressStates
configuration of state indexing per generator
Idx Size(void) const
Number of generators.
virtual int StateTargetIdx(size_t git, Idx idx)
Get target state index (refer to vector representation as default, overload in CodePrimitives)
std::map< std::string, TimerConfiguration > mTimers
timer definitions
virtual void Generate(void)
Generate code.
int mBitAddress
target expression to represent line value
std::map< std::string, FlagExpression >::iterator FlagIterator
Access to flag records by iterator.
virtual void IndentDec()
Indentation (convenience support for derived classes)
ActionAddressIterator ActionAddressesEnd()
Access to action addresses by iterator.
TimerActionIterator TimerActionsEnd()
Access to timer records by iterator.
virtual void DoWriteTargetConfiguration(TokenWriter &rTw) const
Write global configuration to TokenWriter, excl.
ActionAddressIterator ActionAddressesBegin()
Access to action addresses 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.