CompileDES  3.09
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, 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()>= (1ULL << (mIntegerSize-1))) {
219  FCG_ERR("GebtoolsCodeGenerator::Cwordarray(): const vector exceeds address range");
220  return;
221  }
222  // let base handle
225  return;
226  }
227  // geb specific special case: declare as type in first pass (mutemode 't')
228  MuteCond('t');
229  Output() << TargetAddress(address) << "_t" << " : ARRAY[0.." << val.size()-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(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, 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()>= (1ULL << (mIntegerSize-1))) {
251  FCG_ERR("GebtoolsCodeGenerator::Cwordarray(): const vector exceeds addres range");
252  return;
253  }
254  // let base handle
257  return;
258  }
259  // geb specific special case: declare as type in first pass (mutemode 't')
260  MuteCond('t');
261  Output() << TargetAddress(address) << "_t" << " : ARRAY[0.." << val.size()-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(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, 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()>= (1ULL << (mIntegerSize-1))) {
283  FCG_ERR("GebtoolsCodeGenerator::Cstrarray(): const vector exceeds addres range");
284  return;
285  }
286  // let base handle
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(address) << "_t" << " : ARRAY[0.." << val.size()-1 << "] OF " << TargetAddress(address) << "_st" << " := ";
303  LineFeed();
304  IndentInc();
305  Output() << StrarrayConstant(val) << ";";
306  LineFeed();
307  IndentDec();
308  // geb specific special case: import on code generation (mutemode 'c')
309  MuteCond('c');
310  Output() << TargetAddress(address) << " : " << TargetAddress(address) << "_t" << ";";
311  LineFeed();
312  // default to no mute
313  MuteCond('*');
314 }
315 
316 // int-array
317 void GebtoolsCodeGenerator::IntarrayDeclare(const AA& address, const std::vector<int>& val) {
318  if(val.size()==0) {
319  FCG_ERR("GebtoolsCodeGenerator::Intarray(): ignoring empty const vector");
320  return;
321  }
322  if(val.size()>= (1ULL << (mIntegerSize-1))) {
323  FCG_ERR("GebtoolsCodeGenerator::Intarray(): const vector exceeds addres range");
324  return;
325  }
326  // let base handle
329  return;
330  }
331  // geb specific special case: declare as type in first pass (mutemode 't')
332  MuteCond('t');
333  Output() << TargetAddress(address) << "_t" << " : ARRAY[0.." << val.size()-1 << "] OF " << mIntegerType << " := ";
334  LineFeed();
335  IndentInc();
336  Output() << IntarrayConstant(val) << ";";
337  LineFeed();
338  IndentDec();
339  // geb specific special case: import on code generation (mutemode 'c')
340  MuteCond('c');
341  Output() << TargetAddress(address) << " : " << TargetAddress(address) << "_t" << ";";
342  LineFeed();
343  // default to no mute
344  MuteCond('*');
345 }
346 
347 
348 // int-array
349 void GebtoolsCodeGenerator::IntarrayDeclare(const AA& address, int len) {
350  if(len==0) {
351  FCG_ERR("GebtoolsCodeGenerator::Intarray(): ignoring empty vector");
352  return;
353  }
354  if(((unsigned long long) len) >= (1ULL << (mIntegerSize-1))) {
355  FCG_ERR("GebtoolsCodeGenerator::Intarray(): const vector exceeds addres range");
356  return;
357  }
358  // let base handle
361  return;
362  }
363  // geb specific special case: declare as type in first pass (mutemode 't')
364  MuteCond('t');
365  Output() << TargetAddress(AA(address + "_t")) << " : ARRAY[0.." << len-1 << "] OF " << mIntegerType << ";";
366  LineFeed();
367  // geb specific special case: import on code generation (mutemode 'c')
368  MuteCond('c');
369  Output() << TargetAddress(address) << " : " << TargetAddress(AA(address+"_t")) << ";";
370  LineFeed();
371  // default to no mute
372  MuteCond('*');
373 }
374 
375 // word-array
376 void GebtoolsCodeGenerator::WordarrayDeclare(const AA& address, const std::vector<word_t>& val) {
377  if(val.size()==0) {
378  FCG_ERR("GebtoolsCodeGenerator::Wordarray(): ignoring empty const vector");
379  return;
380  }
381  if(val.size()>= (1ULL << (mIntegerSize-1))) {
382  FCG_ERR("GebtoolsCodeGenerator::Wordarray(): const vector exceeds addres range");
383  return;
384  }
385  // let base handle
388  return;
389  }
390  // geb specific special case: declare as type in first pass (mutemode 't')
391  MuteCond('t');
392  Output() << TargetAddress(address) << "_t" << " : ARRAY[0.." << val.size()-1 << "] OF " << mWordType << " := ";
393  LineFeed();
394  IndentInc();
395  Output() << WordarrayConstant(val) << ";";
396  LineFeed();
397  IndentDec();
398  // geb specific special case: import on code generation (mutemode 'c')
399  MuteCond('c');
400  Output() << TargetAddress(address) << " : " << TargetAddress(address) << "_t" << ";";
401  LineFeed();
402  // default to no mute
403  MuteCond('*');
404 }
405 
406 // word-array without initialisation
407 void GebtoolsCodeGenerator::WordarrayDeclare(const AA& address, int len) {
408  // sanity tests
409  if(len==0) {
410  FCG_ERR("GebtoolsCodeGenerator::Wordarray(): ignoring empty vector");
411  return;
412  }
413  if(((unsigned long long) len) >= (1ULL << (mIntegerSize-1))) {
414  FCG_ERR("GebtoolsCodeGenerator::Wordarray(): const vector exceeds addres range");
415  return;
416  }
417  // let base handle
420  return;
421  }
422  // geb specific special case: declare as type in first pass (mutemode 't')
423  MuteCond('t');
424  Output() << TargetAddress(AA(address + "_t")) << " : ARRAY[0.." << len-1 << "] OF " << mWordType << ";";
425  LineFeed();
426  // geb specific special case: import on code generation (mutemode 'c')
427  MuteCond('c');
428  Output() << TargetAddress(address) << " : " << TargetAddress(AA(address+"_t")) << ";";
429  LineFeed();
430  // default to no mute
431  MuteCond('*');
432 }
433 
434 
435 
436 
437 
438