jubnzv / iec-checker

Static analysis of IEC 61131-3 programs
GNU Lesser General Public License v3.0
60 stars 12 forks source link

Support B&R .st #20

Closed bubkuh closed 1 year ago

bubkuh commented 1 year ago

It seems to be incompatible with the code splitting of B&R libraries. B&R has separate files for variable declarations (*.fun) and source code (*.st) of functions and function blocks.

Here's an example: https://github.com/tkucic/brOscatLib/tree/main/oscatBasic

jubnzv commented 1 year ago

Hi @bubkuh,

Thank you for the information, I hadn't heard about B&R before.

Do they have any documentation about the structure of the project? It also would be great to have the specification of the language they use, just to make sure, that we can support it. I'm asking this because there may be pitfalls or some unexpected features, and it would be great to know about them before starting working on B&R support in the checker.

Otherwise, if they just use the same language as in matiec, and they just use separate files to define different components, it is possible to extend iec-checker to support this.

bubkuh commented 1 year ago

Hi @jubnzv, thanks for the feedback.

The language itself should basically be the same, as I have already successfully used the iec-checker by reorganizing the code into the expected structure before execution.

Here is an excerpt from the Automation Studio 4.12.2.65 documentation about the detailled differences:

IEC compliance list

Automation Studio meets the requirements of IEC 61131-3, Ed. 2 in the points listed below in the table that are marked with "Yes".

Table no. Description Automation Studio    Note
1 Character set features

 

                                                                       
1-2 lower case Yes  
1-3a Number sign (#) Yes In time constants                                            
1-3b Pound sign No Except for comments and strings
1-4a Dollar sign ($) Yes  
1-4b Currency sign No Except for comments and strings
1-5a Vertical bar (|) No Except for comments and strings
1-5b Exclamation mark (!) No Except for comments and strings
2 Identifier features    
2-1 Upper case and numbers Yes Mixed cases generates significant identifiers (aBcD not equal to ABcD)
2-2 Upper and lower case numbers and embedded underlines Yes  
2-3 Upper and lower case numbers and embedded or leading underlines55 Yes  
3 Comment features    
3-1 Comments Yes  
3a Pragma feature    
3a-1 Pragmas No  
4 Numeric literals    
4-1 Integer literals Yes  
4-2 Real literals Yes  
4-3 Real literals with exponent Yes  
4-4 Base 2 literals Yes 2#1111_0000
4-5 Base 8 literals Yes 8#377
4-6 Base 16 literals Yes 16#00FF
4-7 Boolean 1 and 0 Yes  
4-8 Boolean TRUE and FALSE Yes  
4-9 Typed literals Yes DINT#10
5 Character string literals    
5-1 Single byte character string    
  '' Yes  
  'A' Yes  
  ' ' Yes  
  '$'' Yes  
  '"' Yes  
  '$R$L' Yes  
  '$0A' Yes  
  '$$1.00' Yes  
  '$C4$CB' Yes  
5-2 Double byte character strings No  
6 Two-character combinations in character strings    
6-2 $$ Yes  
6-3 $'    (Single quote) Yes  
6-4 $L or $l Yes  
6-5 $N or $n Yes Line feed (dec. 10)
6-6 $P or $p Yes  
6-7 $R or $r Yes  
6-8 $T or $t Yes  
6-9 $"   (Double quote) Yes  
7 Duration literals    
7-1a   Yes  
7-1b   Yes  
7-2a   Yes  
7-2b   Yes  
8 Date and time-of-day literals    
8-1 DATE# Yes  
8-2 D# Yes  
8-3 TIME_OF_DAY# Yes  
8-4 TOD# Yes  
8-5 DATE_AND_TIME# Yes  
8-6 DT# Yes  
10 Elementary data types    
10-1 BOOL Yes  
10-2 SINT Yes  
10-3 INT Yes  
10-4 DINT Yes  
10-5 LINT No  
10-6 USINT Yes  
10-7 UINT Yes  
10-8 UDINT Yes  
10-9 ULINT No  
10-10 REAL Yes  
10-11 LREAL Yes  
10-12 TIME Yes  
10-13 DATE Yes  
10-14 TIME_OF_DAY or TOD Yes  
10-15 DATE_AND_TIME or DT Yes  
10-16 STRING Yes  
10-17 BYTE Yes  
10-18 WORD Yes  
10-19 DWORD Yes  
10-20 LWORD No  
10-21 WSTRING Yes  
12 Data type declaration features    
12-1 Derivation from elementary types Yes  
12-2 Enumerated data types Yes  
12-3 Subrange data types Yes  
12-4 Array data types Yes  
12-5 Structured data types Yes  
14 Data type initial value declaration features Yes  
15 Location and size prefixes for directly represented variables    
15-1 Prefix I Yes  
15-2 Prefix Q Yes  
15-3 Prefix M Yes  
15-4 Width X Yes  
15-5 No prefix No  
15-6 Width B Yes  
15-7 Width W Yes  
15-8 Width D Yes  
15-9 Width L No  
15-10 Use of an asterisk * No  
16 Variable declaration keywords    
16-1 VAR Yes  
16-2 VAR_INPUT Yes  
16-3 VAR_OUTPUT Yes  
16-4 VAR_IN_OUT Yes  
16-5 VAR_EXTERNAL No  
16-6 VAR_GLOBAL Yes  
16-7 VAR_ACCESS No  
16-8 VAR_TEMP No  
16-9 VAR_CONFIG Yes  
16-10 RETAIN Yes  
16-11 NEINN_RETAIN No  
16-12 CONSTANT Yes  
16-13 AT Yes  
17 Variable type assignment feature    
17-1   No  
17-2   No  
17-3   Yes  
17-4   No  
17-5   Yes  
17-6   Yes  
17-7   Yes  
17-8   Yes  
18 Variable initial value assignment    
18-1   No  
18-2   No  
18-3   No  
18-4   No  
18-5   Yes  
18-6   Yes  
18-7   Yes  
18-8   Yes  
18-9   Yes  
18-10   Yes  
19 Graphical negation of boolean signal    
19-1   Yes In Function Block Diagram
19-2   No  
19a Textual invocation of functions for formal and non-formal argument list    
19a-1   Yes For function blocks
19a-2   Yes  
20 Use of EN input and ENO output    
20-1   Yes In Ladder Diagram with Automation Studio V3.0.71

No error evaluation after table E.1, EN is passed on to ENO. Division by 0 can be handled in the exception task.
20-2   Yes In Ladder Diagram with Automation Studio V3.0.71 or higher
20-3   No  
20-4   No  
20a Function features    
20a-1   Yes  
20a-2   Yes  
20a-3   Yes  
21 Typed and overloaded functions    
21-1   Yes  
21-2   Yes  
22 Type conversion function features    
22-1   Yes The CONVERT library contains the declarations for all supported conversion functions in IEC 1131-3.
With Automation Runtime >= V3.08 (SG4), V2.32 (SGC), V2.52 (SG3) the AsIecCon library is available.
This contains the conversion routines for the base data types available in Automation Studio 3.0.90 (BYTE, WORD, DWORD, TOD, WSTRING).
22-2   Yes  
22-3   No  
22-4   No  
23 Standard functions of one numeric variable   The OPERATOR library contains the declarations of all the functions defined in table 23.
23-1 ABS Yes  
23-2 SQRT Yes  
23-3 LN Yes  
23-4 LOG Yes  
23-5 EXP Yes  
23-6 SIN Yes  
23-7 COS Yes  
23-8 TAN Yes  
23-9 ASIN Yes  
23-10 ACOS Yes  
23-11 ATAN Yes  
24 Standard arithmetic  functions   The OPERATOR library contains the declarations of all the functions defined in table 24.
24-12 ADD Yes Expandable function
24-13 MUL Yes Expandable function
24-14 SUB Yes  
24-15 DIV Yes  
24-16 MOD Yes  
24-17 EXPT Yes  
24-18 MOVE Yes Expandable function
25 Standard bit shift functions   The OPERATOR library contains the declarations of all the functions defined in table 25.
25-1 SHL    
25-2 SHR    
25-3 ROR    
25-4 ROL    
26 Standard bitwise boolean functions   The OPERATOR library contains the declarations of all the functions defined in table 26.
26-5 AND Yes Expandable function
26-6 OR Yes Expandable function
26-7 XOR Yes Expandable function
26-8 NOT Yes  
27 Standard selection functions   The OPERATOR library contains the declarations of all the functions defined in table 27.
27-1 SEL Yes  
27-2a MAX Yes Expandable function
27-2b MIN Yes Expandable function
27-3 LIMIT Yes  
27-4 MUX Yes Expandable function
28 Standard comparison functions   The OPERATOR library contains the declarations of all the functions defined in table 28.
28-5 GT Yes  
28-6 GE Yes  
28-7 EQ Yes  
28-8 LE Yes  
28-9 LT Yes  
28-10 NE Yes  
29 Standard character string  functions   The STANDARD library contains the declarations of all the functions defined in table 29.
29-1 LEN Yes  
29-2 LEFT Yes  
29-3 RIGHT Yes  
29-4 MID Yes  
29-5 CONCAT Yes  
29-6 INSERT Yes  
29-7 DELETE Yes  
29-8 REPLACE Yes  
29-9 FIND Yes  
30 Functions of time data types No The AsTime library implements some of the functions defined in Table 30.
 
31 Functions of enumerated data types No Enumeration data types not supported
33 Function block declaration and usage features    
33-1a   No  
33-1b   No  
33-2a   No  
33-2b   No  
33-3a   No  
33-3b   No  
33-4a   Yes  
33-4b   Yes  
33-5a   Yes  
33-5b   Yes  
33-6a   Yes  
33-6b   Yes  
33-7a   No  
33-7b   No  
33-8a   No Can be replicated with R_TRIG
33-8b   No Can be replicated with F_TRIG
33-9a   No Can be replicated with R_TRIG
33-9b   No Can be replicated with F_TRIG
33-10a   No  
33-10b   No  
33-11   No  
34 Standard bistable function blocks   The STANDARD library contains the declarations of all the functions defined in table 34.
34-1   Yes  
34-2   Yes  
35 Standard edge detection function blocks   The STANDARD library contains the declarations of all the functions defined in table 35.
35-1   Yes  
35-2   Yes  
35-1a   Yes  
35-
1b-1e
  No  
35-2a   Yes  
35-
2b-2e
  No  
35-3a   Yes  
35-
3b-3d
  No  
36 Standard timer function blocks   The STANDARD library contains the declarations of all the functions defined in table 35.
36-1   Yes  
36-2a   Yes  
36-3a   Yes  
39 Program declaration features    
33-1a   No  
39-1b   No  
39-2a   No  
39-2b   No  
39-3a   No  
39-3b   No  
39-4a   Yes  
39-4b   Yes  
39-5a   Yes  
39-5b   Yes  
39-6a   Yes  
39-6b   Yes  
39-7a   No  
39-7b   No  
39-8a   No Can be replicated with R_TRIG
39-8b   No Can be replicated with F_TRIG
39-9a   No Can be replicated with R_TRIG
39-9b   No Can be replicated with F_TRIG
39-10   No  
39-11   No  
39-12   No  
39-13   Yes  
39-14   No  
39-15   No  
39-16   No  
39-17   Yes  
39-18   Yes Arrays only initialized with 0
39-19   No  
39-20   No  
39-21   No  
39-22a   No  
39-22b   No  
39-23   No  
39-24   No  
40 Sequential function chart
Step features
   
40-1   Yes  
40-2   Yes  
40-3a   Yes Only for action blocks!
Using the step flag
(Step.x := TRUE) doesn't generate an error message.
40-3b   Yes Only for action blocks!
Using the step flag
(Step.x := TRUE) doesn't generate an error message.
40-4   Yes Only for action blocks!
41 Sequential function chart
Transition and transition conditions
  Using direct variables not possible
41-1   Yes  
41-2   Yes  
41-3   Yes  
41-4   Yes By jumping to the respective step
41-4a   Yes  
41-4b   Yes  
41-5   Yes  
41-6   Yes  
41-7   Yes  
41-7a   Yes  
41-7b   Yes  
41-7c   Yes  
41-7d   Yes  
42 Declaration of actions    
42-1   Yes  
42-2l   Yes  
42-2s   Yes  
42-2f   Yes  
42-3s   Yes  
42-3i   Yes  
43 Step/action association    
43-1   Yes  
43-2   Yes  
43-3   No  
43-4   Yes  
44 Action block features    
44-1   Yes  
44-2   Yes  
44-3   No  
44-4   Yes  
44-5   Yes  
44-6   Yes  
44-7   Yes  
44-8   Yes With Automation Studio V3.0.71 or higher
44-9   No  
45 Action qualifiers    
45-1   Yes  
45-2   Yes  
45-3   Yes  
45-4   Yes  
45-5   Yes  
45-6   Yes  
45-7   Yes  
45-8   Yes  
45-9   Yes  
45-10   Yes  
45-11   Yes  
45-12   Yes  
45a Action control features    
45a-1   Yes In Sequential Function Chart, and with Automation Studio V3.0.71 and higher also in Ladder Diagram
45a-2   No  
46 Sequence evolution    
46-1   Yes  
46-2a   Yes  
46-2b   No  
46-2c   No  
46-3   Yes  
46-4   Yes  
46-5   Yes  
46-6   Yes By jumping to the respective step
46-7   No  
48 SFC minimal compliance requirements Yes  
49 Configuration and resource declaration features    
49-1   No  
49-2   No  
49-3   No  
49-4   No  
49-5a   Yes  
49-5b   No  
49-6a   No  
49-6b   No  
49-6c   Yes  
49-7   No  
49-8   No  
49-9   No  
49-10   No  
49-11   Yes  
49-12   No  
49-13   No  
50 Task features    
50-1a   Yes  
50-1b   No  
50-2a   No  
50-2b   No  
50-3a   Yes In XML syntax
50-3b   No  
50-4a   No  
50-4b   No  
50-5a   No  
50-5b   No  
51 Parenthesized expression features for IL language    
51-1   No  
51-2   Yes  
52 Instruction list operators    
52-1 LD Yes  
52-2 ST Yes  
52-3 S
R
Yes  
52-4 AND Yes  
52-5 & Yes  
52-6 OR Yes  
52-7 XOR Yes  
52-7a NOT Yes  
52-8 ADD Yes  
52-9 SUB Yes  
52-10 MUL Yes  
52-11 DIV Yes  
52-11a MOD Yes  
52-12 GT Yes  
52-13 GE Yes  
52-14 EQ Yes  
52-15 NE Yes  
52-16 LE Yes  
52-17 LT Yes  
52-18 JMP Yes  
52-19 CAL Yes  
52-20 RET Yes  
52-21 ) Yes  
53 Function block invocation and
Function invocation features for IL language
   
53-1a   No  
53-1b   Yes  
53-2   Yes  
53-3   No  
53-4   No  
53-5   Yes  
54 Standard function block input operators    
54-4   No  
54-5   No  
54-6   No  
54-7   No  
54-8   No  
54-9   No  
54-10   No  
54-11   No  
54-12   No  
54-13   No  
55 Operators of the ST language    
55-1   Yes  
55-2   Yes  
55-3   Yes  
55-4   Yes  
55-5   Yes  
55-6   Yes  
55-7   Yes  
55-8   Yes  
55-9   Yes  
55-10   Yes  
55-11   Yes  
55-12   Yes  
55-13   Yes  
55-14   Yes  
55-15   Yes  
55-16   Yes  
55-17   Yes  
56 ST language statements    
56-1   Yes  
56-2   Yes  
56-3   Yes  
56-4   Yes  
56-5   Yes  
56-6   Yes  
56-7   Yes  
56-8   Yes  
56-9   Yes  
56-10   Yes  
57 Representing of lines and blocks    
57-1   No  
57-2   Yes  
57-3   No  
57-4   Yes  
57-5   No  
57-6   Yes  
57-7   No  
57-8   Yes  
57-9   No  
57-10   Yes  
57-11   No  
57-12   Yes  
57-13   No  
57-14   Yes  
58 Graphic execution control elements    
58-1   Yes  
58-2   Yes  
58-3   Yes  
58-4   Yes  
58-5   Yes  
58-6   Yes  
58-7   Yes  
58-8   Yes  
59 Power rails    
59-1   Yes  
59-2   Yes  
60 Link elements    
60-1   Yes  
60-2   Yes  
61 Contacts    
61-1   Yes  
61-2   No  
61-3   Yes  
61-4   No  
61-5   Yes  
61-6   No  
61-7   Yes  
61-8   No  
62 Coils    
62-1   Yes  
62-2   Yes  
62-3   Yes  
62-4   Yes  
62-8   Yes  
62-9   Yes  

D.1 - Implementation dependent parameters

ClauseParameter Automation Studio
2.1.2 Maximum length of identifiers 32
2.1.5 Maximum comment length No limit
2.2.3.1 Range of TIME variables T#-24d_20h_31m_23s_648ms ...T#24d_20h_31m_23s_647ms
  Range of TIME_OF_DAY variables TOD#00:00:00 ... TOD#23:59:59.999
  Range of DATE_AND_TIME variables DT#1970-01-01-00:00:00
... DT#2106-02-07-06:28:15
2.3.3.1 Maximum numbers of enumerated values No limit
  Maximum numbers of array subscript 1
  Maximum array size 4095 for array as structure element
32 KB
  Maximum numbers of structure elements No limit
  Maximum structure size 32 KB
  Maximum range of subscript values No limit
  Maximum number of levels of nested structures 16
2.3.3.2 Default maximum length of STRING and WSTRING 80
  Maximum allowed length of STRING and WSTRING Limited by the maximum amount of memory that can be configured for the target.
2.4.1.1 Maximum number of hierarchical levels No limit
  Logical or physical mapping Physical mapping
2.4.2 Initialization of system inputs System inputs are copied to the assigned variable areas before the first task is started. An exception is thrown for defective or missing I/O modules on SG3 targets. On SG4 targets, you can set whether an exception is thrown or the default value 0 should be used.
2.4.3 Maximum number of variables per declaration Variables up to max. 64 KB can be created. The number is not important and depends on the data types of the variables.
For example, approx. 64,000 variables of type USINT, 32,000 variables of type INT, 16,000 variables of type DINT, etc.
2.5 Information necessary to determine execution times of POUs Cycle time monitoring of the respective task class by the operating system
2.5.1.2 Values of outputs when EN is FALSE Result of the last execution of the block remains
2.5.1.3 Maximum number of function specifications SG4: no limit
SG3: configurable (0..65535)
2.5.1.5 Maximum number of inputs of extensible functions Extensible functions are only possible in FBD/CFC.
Automation Studio 2.x: 220
Automation Studio 3.0 and up: 1024
2.5.2 Maximum number of function block specifications and instantiations SG4: no limit
SG3: configurable (0..65535)
2.5.2.1a Function block input variable assignment when EN is FALSE Not supported
2.5.2.3.3 Pvmin, Pvmax of counters 0...65535 (CTD, CTU)
-32768...32767 (CTUD)
2.5.2.3.4 Effect of a change in the value of a PT input during a timing operation PT applied and checked immediately
2.5.3 Program size limitations 64KB
2.6.2 Precision of step elapsed time. For the task's configured cycle time
  Maximum number of steps per SFC. No limit
2.6.3 Maximum number of transitions per SFC and per step No limit
2.6.4.2 Maximum number of action blocks per step 10
2.6.4.5 Access to the functional equivalent of the Q or A outputs No
2.6.5 Transition clearing time The step switch takes place at the end of the task cycle if necessary.
2.7.1 Contents of RESOURCE libraries Not implemented
2.7.1 Effect of using READ_WRITE access to the function block outputs Outputs are overwritten in each cycle.
Otherwise, it depends on the implementation in the respective block.
2.7.2 Maximum number of tasks per resource SG4: Up to 8 task classes can be configured!
SG3: Can be set depending on the target!
  Task interval resolution Configurable
3.3.1 Maximum length of expressions No limit
3.3.2 Maximum length of statements No limit
3.3.2.3 Maximum number of CASE selections No limit
3.3.2.4 Value of control variable after completion of the FOR loop One step below the limit
4.1.1 Restriction on network topology Full graphics.
Max. number of columns in Ladder Diagram: 50
Max. number of columns in FBD and CFC: 64
Max. number of lines in CFC: 65535
Max. number of networks in FBD: 65535
Max. number of lines per network in FBD: 65535
4.1.3 Evaluation order of feedback loops Explicit loops according to 4.1.3. Fig. 23a are not permitted.
Implicit return according to 4.1.3. Fig. 23a and b are allowed. Evaluation takes place for each network from top to bottom and right to left.
jubnzv commented 1 year ago

@bubkuh Got it, thank you for the information.

I think, we don't need to do anything related to the language support in the checker, since it seems to be the same. All the limitations and differences specific to Automation Studio should be checked during the compilation by its toolchain.

To make it easier to use iec-checker for B&R programs, I propose to add support for multiple input files. The idea is that the checker will treat all input files as if they were a single file, merge their internal representation (IR) and then run analyses over it.

The pitfall of this implementation is that name collisions for components in these files are not allowed. For example, it will not possible to define STRUCT with the same name in two input files. I don't think this will cause any problems yet, because I personally don't know of any implementation of the IEC61131-3 language that treats definitions from different files as if they were placed in different namespaces.

jubnzv commented 1 year ago

Hi @bubkuh,

I added the -merge option to the checker, that should solve this problem. Basically, it first merges all the input files into one, and then runs analysis on it.

I kept the default behavior of handling multiple input files. If the -merge option is not specified, they will be processed individually. I remember that we needed this when adding support for another IDE before: https://github.com/jubnzv/iec-checker/issues/6.

Could you please try the -merge option?

Please, re-open this issue if you have any questions or suggestions. It was closed automatically when merging the corresponding PR.