2 # SPDX-License-Identifier: GPL-2.0-only
4 # Copyright (C) 2019-2022 Red Hat, Inc. Daniel Bristot de Oliveira <bristot@kernel.org>
6 # dot2c: parse an automata in dot file digraph format into a C
8 # This program was written in the development of this paper:
9 # de Oliveira, D. B. and Cucinotta, T. and de Oliveira, R. S.
10 # "Efficient Formal Verification for the Linux Kernel." International
11 # Conference on Software Engineering and Formal Methods. Springer, Cham, 2019.
13 # For further information, see:
14 # Documentation/trace/rv/deterministic_automata.rst
16 from dot2
.automata
import Automata
18 class Dot2c(Automata
):
20 enum_states_def
= "states"
21 enum_events_def
= "events"
22 struct_automaton_def
= "automaton"
23 var_automaton_def
= "aut"
25 def __init__(self
, file_path
):
26 super().__init
__(file_path
)
27 self
.line_length
= 100
29 def __buff_to_string(self
, buff
):
33 string
= string
+ line
+ "\n"
38 def __get_enum_states_content(self
):
40 buff
.append("\t%s%s = 0," % (self
.initial_state
, self
.enum_suffix
))
41 for state
in self
.states
:
42 if state
!= self
.initial_state
:
43 buff
.append("\t%s%s," % (state
, self
.enum_suffix
))
44 buff
.append("\tstate_max%s" % (self
.enum_suffix
))
48 def get_enum_states_string(self
):
49 buff
= self
.__get
_enum
_states
_content
()
50 return self
.__buff
_to
_string
(buff
)
52 def format_states_enum(self
):
54 buff
.append("enum %s {" % self
.enum_states_def
)
55 buff
.append(self
.get_enum_states_string())
60 def __get_enum_events_content(self
):
63 for event
in self
.events
:
65 buff
.append("\t%s%s = 0," % (event
, self
.enum_suffix
))
68 buff
.append("\t%s%s," % (event
, self
.enum_suffix
))
70 buff
.append("\tevent_max%s" % self
.enum_suffix
)
74 def get_enum_events_string(self
):
75 buff
= self
.__get
_enum
_events
_content
()
76 return self
.__buff
_to
_string
(buff
)
78 def format_events_enum(self
):
80 buff
.append("enum %s {" % self
.enum_events_def
)
81 buff
.append(self
.get_enum_events_string())
86 def get_minimun_type(self
):
87 min_type
= "unsigned char"
89 if self
.states
.__len
__() > 255:
90 min_type
= "unsigned short"
92 if self
.states
.__len
__() > 65535:
93 min_type
= "unsigned int"
95 if self
.states
.__len
__() > 1000000:
96 raise Exception("Too many states: %d" % self
.states
.__len
__())
100 def format_automaton_definition(self
):
101 min_type
= self
.get_minimun_type()
103 buff
.append("struct %s {" % self
.struct_automaton_def
)
104 buff
.append("\tchar *state_names[state_max%s];" % (self
.enum_suffix
))
105 buff
.append("\tchar *event_names[event_max%s];" % (self
.enum_suffix
))
106 buff
.append("\t%s function[state_max%s][event_max%s];" % (min_type
, self
.enum_suffix
, self
.enum_suffix
))
107 buff
.append("\t%s initial_state;" % min_type
)
108 buff
.append("\tbool final_states[state_max%s];" % (self
.enum_suffix
))
112 def format_aut_init_header(self
):
114 buff
.append("static const struct %s %s = {" % (self
.struct_automaton_def
, self
.var_automaton_def
))
117 def __get_string_vector_per_line_content(self
, buff
):
122 string
= string
+ "\t\t\"" + entry
125 string
= string
+ "\",\n\t\t\"" + entry
126 string
= string
+ "\""
130 def get_aut_init_events_string(self
):
131 return self
.__get
_string
_vector
_per
_line
_content
(self
.events
)
133 def get_aut_init_states_string(self
):
134 return self
.__get
_string
_vector
_per
_line
_content
(self
.states
)
136 def format_aut_init_events_string(self
):
138 buff
.append("\t.event_names = {")
139 buff
.append(self
.get_aut_init_events_string())
143 def format_aut_init_states_string(self
):
145 buff
.append("\t.state_names = {")
146 buff
.append(self
.get_aut_init_states_string())
151 def __get_max_strlen_of_states(self
):
152 max_state_name
= max(self
.states
, key
= len).__len
__()
153 return max(max_state_name
, self
.invalid_state_str
.__len
__())
155 def __get_state_string_length(self
):
156 maxlen
= self
.__get
_max
_strlen
_of
_states
() + self
.enum_suffix
.__len
__()
157 return "%" + str(maxlen
) + "s"
159 def get_aut_init_function(self
):
160 nr_states
= self
.states
.__len
__()
161 nr_events
= self
.events
.__len
__()
164 strformat
= self
.__get
_state
_string
_length
()
166 for x
in range(nr_states
):
168 for y
in range(nr_events
):
169 next_state
= self
.function
[x
][y
]
170 if next_state
!= self
.invalid_state_str
:
171 next_state
= self
.function
[x
][y
] + self
.enum_suffix
174 line
= line
+ strformat
% next_state
+ ", "
176 line
= line
+ strformat
% next_state
+ " },"
179 return self
.__buff
_to
_string
(buff
)
181 def format_aut_init_function(self
):
183 buff
.append("\t.function = {")
184 buff
.append(self
.get_aut_init_function())
189 def get_aut_init_initial_state(self
):
190 return self
.initial_state
192 def format_aut_init_initial_state(self
):
194 initial_state
= self
.get_aut_init_initial_state()
195 buff
.append("\t.initial_state = " + initial_state
+ self
.enum_suffix
+ ",")
199 def get_aut_init_final_states(self
):
202 for state
in self
.states
:
208 if self
.final_states
.__contains
__(state
):
214 def format_aut_init_final_states(self
):
216 buff
.append("\t.final_states = { %s }," % self
.get_aut_init_final_states())
220 def __get_automaton_initialization_footer_string(self
):
224 def format_aut_init_footer(self
):
226 buff
.append(self
.__get
_automaton
_initialization
_footer
_string
())
230 def format_invalid_state(self
):
232 buff
.append("#define %s state_max%s\n" % (self
.invalid_state_str
, self
.enum_suffix
))
236 def format_model(self
):
238 buff
+= self
.format_states_enum()
239 buff
+= self
.format_invalid_state()
240 buff
+= self
.format_events_enum()
241 buff
+= self
.format_automaton_definition()
242 buff
+= self
.format_aut_init_header()
243 buff
+= self
.format_aut_init_states_string()
244 buff
+= self
.format_aut_init_events_string()
245 buff
+= self
.format_aut_init_function()
246 buff
+= self
.format_aut_init_initial_state()
247 buff
+= self
.format_aut_init_final_states()
248 buff
+= self
.format_aut_init_footer()
252 def print_model_classic(self
):
253 buff
= self
.format_model()
254 print(self
.__buff
_to
_string
(buff
))