CompileDES  3.13
Executable-Code Generation from Synchronised libFAUDES Automata
cgp_gebtools.cpp
Go to the documentation of this file.
1 
3 /*
4  FAU Discrete Event Systems Library (libFAUDES)
5 
6  Copyright (C) 2010, 2016 Thomas Moor
7 
8 */
9 
10 // my includes
11 #include "cgp_gebtools.h"
12 
13 
14 /*
15 ******************************************************************
16 ******************************************************************
17 ******************************************************************
18 
19 GebtoolsCodeGenerator implementation --- class
20 
21 ******************************************************************
22 ******************************************************************
23 ******************************************************************
24 */
25 
26 
27 // Register derived class
29 
30 
31 // GebtoolsCodeGenerator(void)
33  FD_DCG("GebtoolsCodeGenerator(" << this << ")::GebtoolsCodeGenerator()");
34 }
35 
36 
37 // GebtoolsCodeGenerator(void)
39  FD_DCG("GebtoolsCodeGenerator(" << this << ")::~GebtoolsCodeGenerator()");
40 }
41 
42 
43 // clear
45  FD_DCG("GebtoolsCodeGenerator::Clear()");
46  // call base
48  // my flavor of defaults
50 }
51 
52 //DoReadTargetConfiguration(rTr)
54  FD_DCG("GebtoolsCodeGenerator::DoReadTargetConfiguration()");
55  // base
57  // iec option
58  Token token;
59  if(rTr.ExistsBegin("IecTypedArrayConstants")) {
60  rTr.ReadBegin("IecTypedArrayConstants",token);
61  mIecTypedArrayConstants = token.AttributeIntegerValue("val");
62  rTr.ReadEnd("IecTypedArrayConstants");
63  }
64 }
65 
66 //DoWriteTargetConfiguration(rTw)
68  FD_DCG("GebtoolsCodeGenerator::DoWriteTargetConfiguration()");
69  // base
71  // code option
72  Token token;
73  token.SetEmpty("IecTypedArrayConstants");
74  token.InsAttributeBoolean("val",mIecTypedArrayConstants);
75  rTw.Write(token);
76  }
77 
78 
79 
80 /*
81 ******************************************************************
82 ******************************************************************
83 ******************************************************************
84 
85 Iec611311stGenerator implementation --- code organisation
86 
87 ******************************************************************
88 ******************************************************************
89 ******************************************************************
90 */
91 
92 
93 // code blocks: cyclic function block
95  // array constants as types: outside POU for GEB IDE
97  Comment("************************************************");
98  Comment("* array types --- GEB IDE specific *");
99  Comment("************************************************");
100  Output() << "TYPE";
101  LineFeed(2);
102  MuteMode('t');
107  Comment("other type fixes");
108  EventSetDeclare(AA("aux_locenabled"));
109  EventSetDeclare(AA("aux_executables"));
110  LineFeed(1);
111  MuteMode('c');
112  MuteCond('*');
113  LineFeed(1);
114  Output() << "END_TYPE";
115  LineFeed(4);
116  }
117  Comment("************************************************");
118  Comment("* function block to host cyclic code *");
119  Comment("************************************************");
120  LineFeed(2);
121  Output() << "FUNCTION_BLOCK " << mPrefix <<"cyclic_fb";
122  LineFeed(2);
123  Output() << "VAR_INPUT";
124  LineFeed(2);
125  DeclareReset();
126  Output() << "END_VAR";
127  LineFeed(2);
128  Output() << "VAR_OUTPUT";
129  LineFeed(2);
130  DeclareStatus();
134  LineFeed(1);
135  Output() << "END_VAR";
136  LineFeed(2);
137  Output() << "VAR";
138  LineFeed(2);
143  DeclareTimers();
144  DeclareAux();
147  DeclareSystime();
148  Output() << "END_VAR";
149  LineFeed(2);
150  if(CountImportSymbolicIo()>0) {
151  Output() << "VAR_EXTERNAL";
152  LineFeed(2);
154  Output() << "END_VAR";
155  LineFeed(2);
156  }
157  if(CountImportPhysicalIo()>0) {
159  LineFeed(2);
161  Output() << "END_VAR";
162  LineFeed(2);
163  }
164  LiteralCyclic();
166  DecrementTimers();
168  LineFeed();
169  Output() << "END_FUNCTION_BLOCK (* end of " << mPrefix <<"cyclic_fb *)";
170  LineFeed();
171  LineFeed(2+1);
172 }
173 
174 // code blocks: function to host lookup tables
176  // bail out
177  if((!mEventNameLookup) && (!mStateNameLookup)) return;
178  // array constants as types: outside POU for GEB IDE
180  Comment("************************************************");
181  Comment("* initialised array types --- GEB IDE specific *");
182  Comment("************************************************");
183  Output() << "TYPE";
184  LineFeed(2);
185  MuteMode('t');
188  LineFeed(1);
189  Output() << "END_TYPE";
190  LineFeed(4);
191  MuteMode('c');
192  MuteCond('*');
193  }
194  // pass on to base
196 }
197 
198 /*
199 ******************************************************************
200 ******************************************************************
201 ******************************************************************
202 
203 Iec611311stGenerator implementation --- code primitives
204 
205 ******************************************************************
206 ******************************************************************
207 ******************************************************************
208 */
209 
210 
211 
212 // const-int-array
213 void GebtoolsCodeGenerator::CintarrayDeclare(const AA& address, int offset, const std::vector<int>& val) {
214  if(val.size()==0) {
215  FCG_ERR("GebtoolsCodeGenerator::Cintarray(): ignoring empty const vector");
216  return;
217  }
218  if(val.size()+offset >= (1ULL << (mIntegerSize-1))) {
219  FCG_ERR("GebtoolsCodeGenerator::Cwordarray(): const vector exceeds address range");
220  return;
221  }
222  // let base handle
224  Iec61131stCodeGenerator::CintarrayDeclare(address,offset,val);
225  return;
226  }
227  // geb specific special case: declare as type in first pass (mutemode 't')
228  MuteCond('t');
229  Output() << TargetAddress(AA(address+"_t")) << " : ARRAY[" << offset << ".." << val.size()+offset-1 << "] OF " << mIntegerType << " := ";
230  LineFeed();
231  IndentInc();
232  Output() << IntarrayConstant(val) << ";";
233  LineFeed();
234  IndentDec();
235  // geb specific special case: import on code generation (mutemode 'c')
236  MuteCond('c');
237  Output() << TargetAddress(address) << " : " << TargetAddress(AA(address+"_t")) << ";";
238  LineFeed();
239  // default to no mute
240  MuteCond('*');
241 }
242 
243 
244 // const-word-array
245 void GebtoolsCodeGenerator::CwordarrayDeclare(const AA& address, int offset, const std::vector<word_t>& val) {
246  if(val.size()==0) {
247  FCG_ERR("GebtoolsCodeGenerator::Cwordarray(): ignoring empty const vector");
248  return;
249  }
250  if(val.size()+offset >= (1ULL << (mIntegerSize-1))) {
251  FCG_ERR("GebtoolsCodeGenerator::Cwordarray(): const vector exceeds addres range");
252  return;
253  }
254  // let base handle
256  Iec61131stCodeGenerator::CwordarrayDeclare(address,offset,val);
257  return;
258  }
259  // geb specific special case: declare as type in first pass (mutemode 't')
260  MuteCond('t');
261  Output() << TargetAddress(AA(address+"_t")) << " : ARRAY[" << offset << ".." << val.size()+offset-1 << "] OF " << mWordType << " := ";
262  LineFeed();
263  IndentInc();
264  Output() << WordarrayConstant(val) << ";";
265  LineFeed();
266  IndentDec();
267  // geb specific special case: import on code generation (mutemode 'c')
268  MuteCond('c');
269  Output() << TargetAddress(address) << " : " << TargetAddress(AA(address+"_t")) << ";";
270  LineFeed();
271  // default to no mute
272  MuteCond('*');
273 }
274 
275 
276 // const-str-array
277 void GebtoolsCodeGenerator::CstrarrayDeclare(const AA& address, int offset, const std::vector<std::string>& val) {
278  if(val.size()==0) {
279  FCG_ERR("GebtoolsCodeGenerator::Cstrarray(): ignoring empty const vector");
280  return;
281  }
282  if(val.size()+offset >= (1ULL << (mIntegerSize-1))) {
283  FCG_ERR("GebtoolsCodeGenerator::Cstrarray(): const vector exceeds addres range");
284  return;
285  }
286  // let base handle
288  Iec61131stCodeGenerator::CstrarrayDeclare(address,offset,val);
289  return;
290  }
291  // figure string size
292  size_t len=0;
293  for(size_t i=0; i<val.size(); ++i)
294  if(val[i].size()>len) len=val[i].size();
295  // geb specific special case: declare as type in first pass (mutemode 't')
296  MuteCond('t');
297  // variant a): iec st compliant
298  //Output() << TargetAddress(address) << "_t" << " : ARRAY[0.." << val.size()-1 << "] OF STRING[" << ToStringInteger(len) <<"] := ";
299  // variant b): circumvent issue for early 3.xx GEB versions
300  Output() << TargetAddress(address) << "_st" << " : STRING[" << ToStringInteger(len) <<"];";
301  LineFeed();
302  Output() << TargetAddress(AA(address+"_t")) << " : ARRAY[" << offset << ".." << val.size()+offset-1 << "] OF "<<
303  TargetAddress(AA(address+"_st")) << " := ";
304  LineFeed();
305  IndentInc();
306  Output() << StrarrayConstant(val) << ";";
307  LineFeed();
308  IndentDec();
309  // geb specific special case: import on code generation (mutemode 'c')
310  MuteCond('c');
311  Output() << TargetAddress(address) << " : " << TargetAddress(AA(address+"_t")) << ";";
312  LineFeed();
313  // default to no mute
314  MuteCond('*');
315 }
316 
317 // int-array
318 void GebtoolsCodeGenerator::IntarrayDeclare(const AA& address, int offset, const std::vector<int>& val) {
319  if(val.size()==0) {
320  FCG_ERR("GebtoolsCodeGenerator::Intarray(): ignoring empty const vector");
321  return;
322  }
323  if(val.size()+offset >= (1ULL << (mIntegerSize-1))) {
324  FCG_ERR("GebtoolsCodeGenerator::Intarray(): const vector exceeds addres range");
325  return;
326  }
327  // let base handle
329  Iec61131stCodeGenerator::IntarrayDeclare(address,offset,val);
330  return;
331  }
332  // geb specific special case: declare as type in first pass (mutemode 't')
333  MuteCond('t');
334  Output() << TargetAddress(AA(address+"_t")) << " : ARRAY[" << offset << ".." << val.size()+offset-1 << "] OF " << mIntegerType << " := ";
335  LineFeed();
336  IndentInc();
337  Output() << IntarrayConstant(val) << ";";
338  LineFeed();
339  IndentDec();
340  // geb specific special case: import on code generation (mutemode 'c')
341  MuteCond('c');
342  Output() << TargetAddress(address) << " : " << TargetAddress(AA(address+"_t")) << ";";
343  LineFeed();
344  // default to no mute
345  MuteCond('*');
346 }
347 
348 
349 // int-array
350 void GebtoolsCodeGenerator::IntarrayDeclare(const AA& address, int offset, int len) {
351  if(len==0) {
352  FCG_ERR("GebtoolsCodeGenerator::Intarray(): ignoring empty vector");
353  return;
354  }
355  if(((unsigned long long) len+offset) >= (1ULL << (mIntegerSize-1))) {
356  FCG_ERR("GebtoolsCodeGenerator::Intarray(): const vector exceeds addres range");
357  return;
358  }
359  // let base handle
361  Iec61131stCodeGenerator::IntarrayDeclare(address,offset,len);
362  return;
363  }
364  // geb specific special case: declare as type in first pass (mutemode 't')
365  MuteCond('t');
366  Output() << TargetAddress(AA(address + "_t")) << " : ARRAY[" << offset << ".." << len+offset-1 << "] OF " << mIntegerType << ";";
367  LineFeed();
368  // geb specific special case: import on code generation (mutemode 'c')
369  MuteCond('c');
370  Output() << TargetAddress(address) << " : " << TargetAddress(AA(address+"_t")) << ";";
371  LineFeed();
372  // default to no mute
373  MuteCond('*');
374 }
375 
376 // word-array
377 void GebtoolsCodeGenerator::WordarrayDeclare(const AA& address, int offset, const std::vector<word_t>& val) {
378  if(val.size()==0) {
379  FCG_ERR("GebtoolsCodeGenerator::Wordarray(): ignoring empty const vector");
380  return;
381  }
382  if(val.size()+offset >= (1ULL << (mIntegerSize-1))) {
383  FCG_ERR("GebtoolsCodeGenerator::Wordarray(): const vector exceeds addres range");
384  return;
385  }
386  // let base handle
388  Iec61131stCodeGenerator::WordarrayDeclare(address,offset,val);
389  return;
390  }
391  // geb specific special case: declare as type in first pass (mutemode 't')
392  MuteCond('t');
393  Output() << TargetAddress(AA(address + "_t")) << " : ARRAY[" << offset << ".." << val.size()+offset-1 << "] OF " << mWordType << " := ";
394  LineFeed();
395  IndentInc();
396  Output() << WordarrayConstant(val) << ";";
397  LineFeed();
398  IndentDec();
399  // geb specific special case: import on code generation (mutemode 'c')
400  MuteCond('c');
401  Output() << TargetAddress(address) << " : " << TargetAddress(AA(address+"_t")) << ";";
402  LineFeed();
403  // default to no mute
404  MuteCond('*');
405 }
406 
407 // word-array without initialisation
408 void GebtoolsCodeGenerator::WordarrayDeclare(const AA& address, int offset, int len) {
409  // sanity tests
410  if(len==0) {
411  FCG_ERR("GebtoolsCodeGenerator::Wordarray(): ignoring empty vector");
412  return;
413  }
414  if(((unsigned long long) len+offset ) >= (1ULL << (mIntegerSize-1))) {
415  FCG_ERR("GebtoolsCodeGenerator::Wordarray(): const vector exceeds addres range");
416  return;
417  }
418  // let base handle
420  Iec61131stCodeGenerator::WordarrayDeclare(address,offset,len);
421  return;
422  }
423  // geb specific special case: declare as type in first pass (mutemode 't')
424  MuteCond('t');
425  Output() << TargetAddress(AA(address + "_t")) << " : ARRAY[" << offset << ".." << len+offset-1 << "] OF " << mWordType << ";";
426  LineFeed();
427  // geb specific special case: import on code generation (mutemode 'c')
428  MuteCond('c');
429  Output() << TargetAddress(address) << " : " << TargetAddress(AA(address+"_t")) << ";";
430  LineFeed();
431  // default to no mute
432  MuteCond('*');
433 }
434 
435 
436 
437 
438 
439 
#define FAUDES_REGISTERCODEGENERATOR(ftype, ctype)
Class registration macro.
Code-generator GEB toolchain (IEC 61131-3 ST)
virtual void LineFeed(int lines=1)
LineFeed (convenience support for derived classes)
virtual void IndentInc()
Indentation (convenience support for derived classes)
virtual void MuteMode(char mode)
Set current mute mode.
int mIntegerSize
compressed boolean capacity of target type integer
virtual std::ostream & Output(void)
Output stream.
virtual void MuteCond(char mode)
Set mode condition.
virtual void IndentDec()
Indentation (convenience support for derived classes)
Abstract address; see also Absstract_Addresses.
virtual void DeclareSmallCarray(void)
Declare bit-mask loop-ups.
virtual void DoGenerateResetCode(void)
cut-and-paste template for code snippet assembly
virtual void DeclareReset(void)
Declare "reset".
virtual void DeclareStatus(void)
Declare "status".
virtual void DeclareRecentEvent(void)
Declare "recent_event".
virtual void DeclareAux(void)
Declare variables local to the provided snippets, e.g. helpers for bit-mask computation.
bool mEventNameLookup
code option: event name lookup
virtual void DeclareEventNameLookup(void)
Declare symbolic name lookup tables.
bool mStateNameLookup
code option: state name lookup
virtual void DeclareParallelState(void)
Declare "parallel_state".
std::string mIntegerType
target data type for integer
virtual void DeclareLoopState(void)
Declare loop state, i.e. line levels, loop flag.
virtual void DoGenerateCyclicCode(void)
cut-and-paste template for code snippet assembly
std::string mWordType
target data type for word
virtual void DeclareLargeCarray(void)
Declare compiled transition relations.
virtual void DeclareStateNameLookup(void)
Declare symbolic name lookup tables.
virtual void DeclarePendingEvents(void)
Declare "pending_events" and "enabled_events".
std::string mPrefix
universal prefix (pseudo name space)
Target GEB Automation toolchain (IEC 61131 ST)
Definition: cgp_gebtools.h:37
void DoGenerateLookups(void)
re-implement program block
virtual void DoWriteTargetConfiguration(TokenWriter &rTw) const
File i/o.
bool mIecTypedArrayConstants
option: formal declaration of array constants
Definition: cgp_gebtools.h:71
void DoGenerateFunction(void)
re-implement program block
GebtoolsCodeGenerator(void)
Constructor.
virtual void WordarrayDeclare(const AA &address, int offset, const std::vector< word_t > &val)
re-implement primitives (extra pass to generate type declarations)
virtual void CintarrayDeclare(const AA &address, int offset, const std::vector< int > &val)
re-implement primitives (extra pass to generate type declarations)
virtual void Clear(void)
Clear all data.
virtual void IntarrayDeclare(const AA &address, int offset, const std::vector< int > &val)
re-implement primitives (extra pass to generate type declarations)
virtual void CwordarrayDeclare(const AA &address, int offset, const std::vector< word_t > &val)
re-implement primitives (extra pass to generate type declarations)
virtual void CstrarrayDeclare(const AA &address, int offset, const std::vector< std::string > &val)
re-implement primitives (extra pass to generate type declarations)
virtual void DoReadTargetConfiguration(TokenReader &rTr)
File i/o.
virtual ~GebtoolsCodeGenerator(void)
Explicit destructor.
Implementation of primitives by IEC 61131 ST.
virtual void DoReadTargetConfiguration(TokenReader &rTr)
File i/o.
virtual void DoWriteTargetConfiguration(TokenWriter &rTw) const
File i/o.
virtual void DeclareImportPhysicalIo(void)
generate code: conditionals
virtual void CintarrayDeclare(const AA &address, int offset, const std::vector< int > &val)
generate code: conditionals
virtual void Clear(void)
Clear all data.
virtual AX WordarrayConstant(const std::vector< word_t > &val)
generate code: conditionals
virtual AX StrarrayConstant(const std::vector< std::string > &val)
generate code: conditionals
virtual void DeclareSystime(void)
generate code: conditionals
virtual void WordarrayDeclare(const AA &address, int offset, int len)
generate code: conditionals
virtual void Comment(const std::string &text)
generate code: conditionals
virtual void DecrementTimers(void)
code snippet
virtual void DeclareImportSymbolicIo(void)
generate code: conditionals
virtual AX IntarrayConstant(const std::vector< int > &val)
generate code: conditionals
virtual std::string TargetAddress(const AA &address)
abstract address conversion
virtual int CountImportPhysicalIo(void)
generate code: conditionals
virtual void DoGenerateLookups(void)
code generation hook (lookup functions)
virtual void CstrarrayDeclare(const AA &address, int offset, const std::vector< std::string > &val)
generate code: conditionals
virtual void DeclareTimers(void)
generate code: conditionals
virtual void CwordarrayDeclare(const AA &address, int offset, const std::vector< word_t > &val)
generate code: conditionals
virtual void IntarrayDeclare(const AA &address, int offset, int len)
generate code: conditionals
virtual int CountImportSymbolicIo(void)
generate code: conditionals
std::string mIecDeclarePhysical
option: formal declaration of io lines
virtual void LiteralCyclic(void)
generate code: conditionals