source: mainline/contrib/arch/hadlbppp.py@ 6d4c549

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6d4c549 was 6d4c549, checked in by Martin Decky <martin@…>, 16 years ago

streamline the behavior protocols
add support for initialization and finalization phase of protocols in ADL

  • Property mode set to 100755
File size: 38.2 KB
Line 
1#!/usr/bin/env python
2#
3# Copyright (c) 2009 Martin Decky
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10# - Redistributions of source code must retain the above copyright
11# notice, this list of conditions and the following disclaimer.
12# - Redistributions in binary form must reproduce the above copyright
13# notice, this list of conditions and the following disclaimer in the
14# documentation and/or other materials provided with the distribution.
15# - The name of the author may not be used to endorse or promote products
16# derived from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29"""
30HelenOS Architecture Description Language and Behavior Protocols preprocessor
31"""
32
33import sys
34import os
35
36INC, POST_INC, BLOCK_COMMENT, LINE_COMMENT, SYSTEM, ARCH, HEAD, BODY, NULL, \
37 INST, VAR, FIN, BIND, TO, SUBSUME, DELEGATE, IFACE, EXTENDS, PRE_BODY, \
38 PROTOTYPE, PAR_LEFT, PAR_RIGHT, SIGNATURE, PROTOCOL, INITIALIZATION, \
39 FINALIZATION, FRAME, PROVIDES, REQUIRES = range(29)
40
41def usage(prname):
42 "Print usage syntax"
43
44 print "%s <--dumpbp|--dumpadl|--noop>+ <OUTPUT>" % prname
45
46def tabs(cnt):
47 "Return given number of tabs"
48
49 return ("\t" * cnt)
50
51def cond_append(tokens, token, trim):
52 "Conditionally append token to tokens with trim"
53
54 if (trim):
55 token = token.strip(" \t")
56
57 if (token != ""):
58 tokens.append(token)
59
60 return tokens
61
62def split_tokens(string, delimiters, trim = False, separate = False):
63 "Split string to tokens by delimiters, keep the delimiters"
64
65 tokens = []
66 last = 0
67 i = 0
68
69 while (i < len(string)):
70 for delim in delimiters:
71 if (len(delim) > 0):
72
73 if (string[i:(i + len(delim))] == delim):
74 if (separate):
75 tokens = cond_append(tokens, string[last:i], trim)
76 tokens = cond_append(tokens, delim, trim)
77 last = i + len(delim)
78 elif (i > 0):
79 tokens = cond_append(tokens, string[last:i], trim)
80 last = i
81
82 i += len(delim) - 1
83 break
84
85 i += 1
86
87 tokens = cond_append(tokens, string[last:len(string)], trim)
88
89 return tokens
90
91def identifier(token):
92 "Check whether the token is an identifier"
93
94 if (len(token) == 0):
95 return False
96
97 for i, char in enumerate(token):
98 if (i == 0):
99 if ((not char.isalpha()) and (char != "_")):
100 return False
101 else:
102 if ((not char.isalnum()) and (char != "_")):
103 return False
104
105 return True
106
107def descriptor(token):
108 "Check whether the token is an interface descriptor"
109
110 parts = token.split(":")
111 if (len(parts) != 2):
112 return False
113
114 return (identifier(parts[0]) and identifier(parts[1]))
115
116def word(token):
117 "Check whether the token is a word"
118
119 if (len(token) == 0):
120 return False
121
122 for i, char in enumerate(token):
123 if ((not char.isalnum()) and (char != "_") and (char != ".")):
124 return False
125
126 return True
127
128def tentative_bp(name, tokens):
129 "Preprocess tentative statements in Behavior Protocol"
130
131 result = []
132 i = 0
133
134 while (i < len(tokens)):
135 if (tokens[i] == "tentative"):
136 if ((i + 1 < len(tokens)) and (tokens[i + 1] == "{")):
137 i += 2
138 start = i
139 level = 1
140
141 while ((i < len(tokens)) and (level > 0)):
142 if (tokens[i] == "{"):
143 level += 1
144 elif (tokens[i] == "}"):
145 level -= 1
146
147 i += 1
148
149 if (level == 0):
150 result.append("(")
151 result.extend(tentative_bp(name, tokens[start:(i - 1)]))
152 result.append(")")
153 result.append("+")
154 result.append("NULL")
155 if (i < len(tokens)):
156 result.append(tokens[i])
157 else:
158 print "%s: Syntax error in tentative statement" % name
159 else:
160 print "%s: Expected '{' for tentative statement" % name
161 else:
162 result.append(tokens[i])
163
164 i += 1
165
166 return result
167
168def alternative_bp(name, tokens):
169 "Preprocess alternative statements in Behavior Protocol"
170
171 result = []
172 i = 0
173
174 while (i < len(tokens)):
175 if (tokens[i] == "alternative"):
176 if ((i + 1 < len(tokens)) and (tokens[i + 1] == "(")):
177 i += 2
178 reps = []
179
180 while ((i < len(tokens)) and (tokens[i] != ")")):
181 reps.append(tokens[i])
182 if ((i + 1 < len(tokens)) and (tokens[i + 1] == ";")):
183 i += 2
184 else:
185 i += 1
186
187 if (len(reps) >= 2):
188 if ((i + 1 < len(tokens)) and (tokens[i + 1] == "{")):
189 i += 2
190
191 start = i
192 level = 1
193
194 while ((i < len(tokens)) and (level > 0)):
195 if (tokens[i] == "{"):
196 level += 1
197 elif (tokens[i] == "}"):
198 level -= 1
199
200 i += 1
201
202 if (level == 0):
203 first = True
204
205 for rep in reps[1:]:
206 retokens = []
207 for token in tokens[start:(i - 1)]:
208 parts = token.split(".")
209 if ((len(parts) == 2) and (parts[0] == reps[0])):
210 retokens.append("%s.%s" % (rep, parts[1]))
211 else:
212 retokens.append(token)
213
214 if (first):
215 first = False
216 else:
217 result.append("+")
218
219 result.append("(")
220 result.extend(alternative_bp(name, retokens))
221 result.append(")")
222
223 if (i < len(tokens)):
224 result.append(tokens[i])
225 else:
226 print "%s: Syntax error in alternative statement" % name
227 else:
228 print "%s: Expected '{' for alternative statement body" % name
229 else:
230 print "%s: At least one pattern and one replacement required for alternative statement" % name
231 else:
232 print "%s: Expected '(' for alternative statement head" % name
233 else:
234 result.append(tokens[i])
235
236 i += 1
237
238 return result
239
240def split_bp(protocol):
241 "Convert Behavior Protocol to tokens"
242
243 return split_tokens(protocol, ["\n", " ", "\t", "(", ")", "{", "}", "*", ";", "+", "||", "|", "!", "?"], True, True)
244
245def extend_bp(name, tokens, iface):
246 "Convert interface Behavior Protocol to generic protocol"
247
248 result = []
249 i = 0
250
251 while (i < len(tokens)):
252 result.append(tokens[i])
253
254 if (tokens[i] == "?"):
255 if (i + 1 < len(tokens)):
256 i += 1
257 parts = tokens[i].split(".")
258
259 if (len(parts) == 1):
260 result.append("%s.%s" % (iface, tokens[i]))
261 else:
262 result.append(tokens[i])
263 else:
264 print "%s: Unexpected end of protocol" % name
265
266 i += 1
267
268 return result
269
270def merge_bp(initialization, finalization, protocols):
271 "Merge several Behavior Protocols"
272
273 indep = []
274
275 if (len(protocols) > 1):
276 first = True
277
278 for protocol in protocols:
279 if (first):
280 first = False
281 else:
282 indep.append("|")
283
284 indep.append("(")
285 indep.extend(protocol)
286 indep.append(")")
287 elif (len(protocols) == 1):
288 indep = protocols[0]
289
290 inited = []
291
292 if (initialization != None):
293 if (len(indep) > 0):
294 inited.append("(")
295 inited.extend(initialization)
296 inited.append(")")
297 inited.append(";")
298 inited.append("(")
299 inited.extend(indep)
300 inited.append(")")
301 else:
302 inited = initialization
303 else:
304 inited = indep
305
306 finited = []
307
308 if (finalization != None):
309 if (len(inited) > 0):
310 finited.append("(")
311 finited.extend(inited)
312 finited.append(")")
313 finited.append(";")
314 finited.append("(")
315 finited.extend(finalization)
316 finited.append(")")
317 else:
318 finited = finalization
319 else:
320 finited = inited
321
322 return finited
323
324def parse_bp(name, tokens, base_indent):
325 "Parse Behavior Protocol"
326
327 tokens = tentative_bp(name, tokens)
328 tokens = alternative_bp(name, tokens)
329
330 indent = base_indent
331 output = ""
332
333 for token in tokens:
334 if (token == "\n"):
335 continue
336
337 if ((token == ";") or (token == "+") or (token == "||") or (token == "|")):
338 output += " %s" % token
339 elif (token == "("):
340 output += "\n%s%s" % (tabs(indent), token)
341 indent += 1
342 elif (token == ")"):
343 if (indent < base_indent):
344 print "%s: Too many parentheses" % name
345
346 indent -= 1
347 output += "\n%s%s" % (tabs(indent), token)
348 elif (token == "{"):
349 output += " %s" % token
350 indent += 1
351 elif (token == "}"):
352 if (indent < base_indent):
353 print "%s: Too many parentheses" % name
354
355 indent -= 1
356 output += "\n%s%s" % (tabs(indent), token)
357 elif (token == "*"):
358 output += "%s" % token
359 elif ((token == "!") or (token == "?") or (token == "NULL")):
360 output += "\n%s%s" % (tabs(indent), token)
361 else:
362 output += "%s" % token
363
364 if (indent > base_indent):
365 print "%s: Missing parentheses" % name
366
367 output = output.strip()
368 if (output == ""):
369 return "NULL"
370
371 return output
372
373def get_iface(name):
374 "Get interface by name"
375
376 global iface_properties
377
378 if (name in iface_properties):
379 return iface_properties[name]
380
381 return None
382
383def inherited_protocols(iface):
384 "Get protocols inherited by an interface"
385
386 result = []
387
388 if ('extends' in iface):
389 supiface = get_iface(iface['extends'])
390 if (not supiface is None):
391 if ('protocol' in supiface):
392 result.append(supiface['protocol'])
393 result.extend(inherited_protocols(supiface))
394 else:
395 print "%s: Extends unknown interface '%s'" % (iface['name'], iface['extends'])
396
397 return result
398
399def dump_frame(frame, outdir, var, archf):
400 "Dump Behavior Protocol of a given frame"
401
402 global opt_bp
403
404 fname = "%s.bp" % frame['name']
405
406 if (archf != None):
407 archf.write("instantiate %s from \"%s\"\n" % (var, fname))
408
409 outname = os.path.join(outdir, fname)
410
411 protocols = []
412 if ('protocol' in frame):
413 protocols.append(frame['protocol'])
414
415 if ('initialization' in frame):
416 initialization = frame['initialization']
417 else:
418 initialization = None
419
420 if ('finalization' in frame):
421 finalization = frame['finalization']
422 else:
423 finalization = None
424
425 if ('provides' in frame):
426 for provides in frame['provides']:
427 iface = get_iface(provides['iface'])
428 if (not iface is None):
429 if ('protocol' in iface):
430 protocols.append(extend_bp(outname, iface['protocol'], iface['name']))
431 for protocol in inherited_protocols(iface):
432 protocols.append(extend_bp(outname, protocol, iface['name']))
433 else:
434 print "%s: Provided interface '%s' is undefined" % (frame['name'], provides['iface'])
435
436
437 if (opt_bp):
438 outf = file(outname, "w")
439 outf.write(parse_bp(outname, merge_bp(initialization, finalization, protocols), 0))
440 outf.close()
441
442def get_system_arch():
443 "Get system architecture"
444
445 global arch_properties
446
447 for arch, properties in arch_properties.items():
448 if ('system' in properties):
449 return properties
450
451 return None
452
453def get_arch(name):
454 "Get architecture by name"
455
456 global arch_properties
457
458 if (name in arch_properties):
459 return arch_properties[name]
460
461 return None
462
463def get_frame(name):
464 "Get frame by name"
465
466 global frame_properties
467
468 if (name in frame_properties):
469 return frame_properties[name]
470
471 return None
472
473def create_null_bp(fname, outdir, archf):
474 "Create null frame protocol"
475
476 global opt_bp
477
478 if (archf != None):
479 archf.write("frame \"%s\"\n" % fname)
480
481 outname = os.path.join(outdir, fname)
482
483 if (opt_bp):
484 outf = file(outname, "w")
485 outf.write("NULL")
486 outf.close()
487
488def flatten_binds(binds, delegates, subsumes):
489 "Remove bindings which are replaced by delegation or subsumption"
490
491 result = []
492 stable = True
493
494 for bind in binds:
495 keep = True
496
497 for delegate in delegates:
498 if (bind['to'] == delegate['to']):
499 keep = False
500 result.append({'from': bind['from'], 'to': delegate['rep']})
501
502 for subsume in subsumes:
503 if (bind['from'] == subsume['from']):
504 keep = False
505 result.append({'from': subsume['rep'], 'to': bind['to']})
506
507 if (keep):
508 result.append(bind)
509 else:
510 stable = False
511
512 if (stable):
513 return result
514 else:
515 return flatten_binds(result, delegates, subsumes)
516
517def direct_binds(binds):
518 "Convert bindings matrix to set of sources by destination"
519
520 result = {}
521
522 for bind in binds:
523 if (not bind['to'] in result):
524 result[bind['to']] = set()
525
526 result[bind['to']].add(bind['from'])
527
528 return result
529
530def merge_subarch(prefix, arch, outdir):
531 "Merge subarchitecture into architexture"
532
533 insts = []
534 binds = []
535 delegates = []
536 subsumes = []
537
538 if ('inst' in arch):
539 for inst in arch['inst']:
540 subarch = get_arch(inst['type'])
541 if (not subarch is None):
542 (subinsts, subbinds, subdelegates, subsubsumes) = merge_subarch("%s_%s" % (prefix, subarch['name']), subarch, outdir)
543 insts.extend(subinsts)
544 binds.extend(subbinds)
545 delegates.extend(subdelegates)
546 subsumes.extend(subsubsumes)
547 else:
548 subframe = get_frame(inst['type'])
549 if (not subframe is None):
550 insts.append({'var': "%s_%s" % (prefix, inst['var']), 'frame': subframe})
551 else:
552 print "%s: '%s' is neither an architecture nor a frame" % (arch['name'], inst['type'])
553
554 if ('bind' in arch):
555 for bind in arch['bind']:
556 binds.append({'from': "%s_%s.%s" % (prefix, bind['from'][0], bind['from'][1]), 'to': "%s_%s.%s" % (prefix, bind['to'][0], bind['to'][1])})
557
558 if ('delegate' in arch):
559 for delegate in arch['delegate']:
560 delegates.append({'to': "%s.%s" % (prefix, delegate['from']), 'rep': "%s_%s.%s" % (prefix, delegate['to'][0], delegate['to'][1])})
561
562 if ('subsume' in arch):
563 for subsume in arch['subsume']:
564 subsumes.append({'from': "%s.%s" % (prefix, subsume['to']), 'rep': "%s_%s.%s" % (prefix, subsume['from'][0], subsume['from'][1])})
565
566 return (insts, binds, delegates, subsumes)
567
568def dump_archbp(outdir):
569 "Dump system architecture Behavior Protocol"
570
571 global opt_bp
572
573 arch = get_system_arch()
574
575 if (arch is None):
576 print "Unable to find system architecture"
577 return
578
579 insts = []
580 binds = []
581 delegates = []
582 subsumes = []
583
584 if ('inst' in arch):
585 for inst in arch['inst']:
586 subarch = get_arch(inst['type'])
587 if (not subarch is None):
588 (subinsts, subbinds, subdelegates, subsubsumes) = merge_subarch(subarch['name'], subarch, outdir)
589 insts.extend(subinsts)
590 binds.extend(subbinds)
591 delegates.extend(subdelegates)
592 subsumes.extend(subsubsumes)
593 else:
594 subframe = get_frame(inst['type'])
595 if (not subframe is None):
596 insts.append({'var': inst['var'], 'frame': subframe})
597 else:
598 print "%s: '%s' is neither an architecture nor a frame" % (arch['name'], inst['type'])
599
600 if ('bind' in arch):
601 for bind in arch['bind']:
602 binds.append({'from': "%s.%s" % (bind['from'][0], bind['from'][1]), 'to': "%s.%s" % (bind['to'][0], bind['to'][1])})
603
604 if ('delegate' in arch):
605 for delegate in arch['delegate']:
606 print "Unable to delegate interface in system architecture"
607 break
608
609 if ('subsume' in arch):
610 for subsume in arch['subsume']:
611 print "Unable to subsume interface in system architecture"
612 break
613
614
615 outname = os.path.join(outdir, "%s.archbp" % arch['name'])
616 if (opt_bp):
617 outf = file(outname, "w")
618 else:
619 outf = None
620
621 create_null_bp("null.bp", outdir, outf)
622
623 for inst in insts:
624 dump_frame(inst['frame'], outdir, inst['var'], outf)
625
626 directed_binds = direct_binds(flatten_binds(binds, delegates, subsumes))
627
628 for dst, src in directed_binds.items():
629 if (outf != None):
630 outf.write("bind %s to %s\n" % (", ".join(src), dst))
631
632 if (outf != None):
633 outf.close()
634
635def preproc_adl(raw, inarg):
636 "Preprocess %% statements in ADL"
637
638 return raw.replace("%%", inarg)
639
640def parse_adl(base, root, inname, nested, indent):
641 "Parse Architecture Description Language"
642
643 global output
644 global context
645 global architecture
646 global interface
647 global frame
648 global protocol
649 global initialization
650 global finalization
651
652 global iface_properties
653 global frame_properties
654 global arch_properties
655
656 global arg0
657
658 if (nested):
659 parts = inname.split("%")
660
661 if (len(parts) > 1):
662 inarg = parts[1]
663 else:
664 inarg = "%%"
665
666 if (parts[0][0:1] == "/"):
667 path = os.path.join(base, ".%s" % parts[0])
668 nested_root = os.path.dirname(path)
669 else:
670 path = os.path.join(root, parts[0])
671 nested_root = root
672
673 if (not os.path.isfile(path)):
674 print "%s: Unable to include file %s" % (inname, path)
675 return ""
676 else:
677 inarg = "%%"
678 path = inname
679 nested_root = root
680
681 inf = file(path, "r")
682
683 raw = preproc_adl(inf.read(), inarg)
684 tokens = split_tokens(raw, ["\n", " ", "\t", "(", ")", "{", "}", "[", "]", "/*", "*/", "#", ";"], True, True)
685
686 for token in tokens:
687
688 # Includes
689
690 if (INC in context):
691 context.remove(INC)
692 parse_adl(base, nested_root, token, True, indent)
693 context.add(POST_INC)
694 continue
695
696 if (POST_INC in context):
697 if (token != "]"):
698 print "%s: Expected ]" % inname
699
700 context.remove(POST_INC)
701 continue
702
703 # Comments and newlines
704
705 if (BLOCK_COMMENT in context):
706 if (token == "*/"):
707 context.remove(BLOCK_COMMENT)
708
709 continue
710
711 if (LINE_COMMENT in context):
712 if (token == "\n"):
713 context.remove(LINE_COMMENT)
714
715 continue
716
717 # Any context
718
719 if (token == "/*"):
720 context.add(BLOCK_COMMENT)
721 continue
722
723 if (token == "#"):
724 context.add(LINE_COMMENT)
725 continue
726
727 if (token == "["):
728 context.add(INC)
729 continue
730
731 if (token == "\n"):
732 continue
733
734 # "frame"
735
736 if (FRAME in context):
737 if (NULL in context):
738 if (token != ";"):
739 print "%s: Expected ';' in frame '%s'" % (inname, frame)
740 else:
741 output += "%s\n" % token
742
743 context.remove(NULL)
744 context.remove(FRAME)
745 frame = None
746 continue
747
748 if (BODY in context):
749 if (FINALIZATION in context):
750 if (token == "{"):
751 indent += 1
752 elif (token == "}"):
753 indent -= 1
754
755 if (((token[-1] == ":") and (indent == 0)) or (indent == -1)):
756 bp = split_bp(finalization)
757 finalization = None
758
759 if (not frame in frame_properties):
760 frame_properties[frame] = {}
761
762 if ('finalization' in frame_properties[frame]):
763 print "%s: Finalization protocol for frame '%s' already defined" % (inname, frame)
764 else:
765 frame_properties[frame]['finalization'] = bp
766
767 output += "\n%s" % tabs(2)
768 output += parse_bp(inname, bp, 2)
769
770 context.remove(FINALIZATION)
771 if (indent == -1):
772 output += "\n%s" % token
773 context.remove(BODY)
774 context.add(NULL)
775 indent = 0
776 continue
777 else:
778 indent = 2
779 else:
780 finalization += token
781 continue
782
783 if (INITIALIZATION in context):
784 if (token == "{"):
785 indent += 1
786 elif (token == "}"):
787 indent -= 1
788
789 if (((token[-1] == ":") and (indent == 0)) or (indent == -1)):
790 bp = split_bp(initialization)
791 initialization = None
792
793 if (not frame in frame_properties):
794 frame_properties[frame] = {}
795
796 if ('initialization' in frame_properties[frame]):
797 print "%s: Initialization protocol for frame '%s' already defined" % (inname, frame)
798 else:
799 frame_properties[frame]['initialization'] = bp
800
801 output += "\n%s" % tabs(2)
802 output += parse_bp(inname, bp, 2)
803
804 context.remove(INITIALIZATION)
805 if (indent == -1):
806 output += "\n%s" % token
807 context.remove(BODY)
808 context.add(NULL)
809 indent = 0
810 continue
811 else:
812 indent = 2
813 else:
814 initialization += token
815 continue
816
817 if (PROTOCOL in context):
818 if (token == "{"):
819 indent += 1
820 elif (token == "}"):
821 indent -= 1
822
823 if (((token[-1] == ":") and (indent == 0)) or (indent == -1)):
824 bp = split_bp(protocol)
825 protocol = None
826
827 if (not frame in frame_properties):
828 frame_properties[frame] = {}
829
830 if ('protocol' in frame_properties[frame]):
831 print "%s: Protocol for frame '%s' already defined" % (inname, frame)
832 else:
833 frame_properties[frame]['protocol'] = bp
834
835 output += "\n%s" % tabs(2)
836 output += parse_bp(inname, bp, 2)
837
838 context.remove(PROTOCOL)
839 if (indent == -1):
840 output += "\n%s" % token
841 context.remove(BODY)
842 context.add(NULL)
843 indent = 0
844 continue
845 else:
846 indent = 2
847 else:
848 protocol += token
849 continue
850
851 if (REQUIRES in context):
852 if (FIN in context):
853 if (token != ";"):
854 print "%s: Expected ';' in frame '%s'" % (inname, frame)
855 else:
856 output += "%s" % token
857
858 context.remove(FIN)
859 continue
860
861 if (VAR in context):
862 if (not identifier(token)):
863 print "%s: Variable name expected in frame '%s'" % (inname, frame)
864 else:
865 if (not frame in frame_properties):
866 frame_properties[frame] = {}
867
868 if (not 'requires' in frame_properties[frame]):
869 frame_properties[frame]['requires'] = []
870
871 frame_properties[frame]['requires'].append({'iface': arg0, 'var': token})
872 arg0 = None
873
874 output += "%s" % token
875
876 context.remove(VAR)
877 context.add(FIN)
878 continue
879
880 if ((token == "}") or (token[-1] == ":")):
881 context.remove(REQUIRES)
882 else:
883 if (not identifier(token)):
884 print "%s: Interface name expected in frame '%s'" % (inname, frame)
885 else:
886 arg0 = token
887 output += "\n%s%s " % (tabs(indent), token)
888
889 context.add(VAR)
890 continue
891
892 if (PROVIDES in context):
893 if (FIN in context):
894 if (token != ";"):
895 print "%s: Expected ';' in frame '%s'" % (inname, frame)
896 else:
897 output += "%s" % token
898
899 context.remove(FIN)
900 continue
901
902 if (VAR in context):
903 if (not identifier(token)):
904 print "%s: Variable name expected in frame '%s'" % (inname, frame)
905 else:
906 if (not frame in frame_properties):
907 frame_properties[frame] = {}
908
909 if (not 'provides' in frame_properties[frame]):
910 frame_properties[frame]['provides'] = []
911
912 frame_properties[frame]['provides'].append({'iface': arg0, 'var': token})
913 arg0 = None
914
915 output += "%s" % token
916
917 context.remove(VAR)
918 context.add(FIN)
919 continue
920
921 if ((token == "}") or (token[-1] == ":")):
922 context.remove(PROVIDES)
923 else:
924 if (not identifier(token)):
925 print "%s: Interface name expected in frame '%s'" % (inname, frame)
926 else:
927 arg0 = token
928 output += "\n%s%s " % (tabs(indent), token)
929
930 context.add(VAR)
931 continue
932
933 if (token == "}"):
934 if (indent != 2):
935 print "%s: Wrong number of parentheses in frame '%s'" % (inname, frame)
936 else:
937 indent = 0
938 output += "\n%s" % token
939
940 context.remove(BODY)
941 context.add(NULL)
942 continue
943
944 if (token == "provides:"):
945 output += "\n%s%s" % (tabs(indent - 1), token)
946 context.add(PROVIDES)
947 continue
948
949 if (token == "requires:"):
950 output += "\n%s%s" % (tabs(indent - 1), token)
951 context.add(REQUIRES)
952 continue
953
954 if (token == "initialization:"):
955 output += "\n%s%s" % (tabs(indent - 1), token)
956 indent = 0
957 context.add(INITIALIZATION)
958 initialization = ""
959 continue
960
961 if (token == "finalization:"):
962 output += "\n%s%s" % (tabs(indent - 1), token)
963 indent = 0
964 context.add(FINALIZATION)
965 finalization = ""
966 continue
967
968 if (token == "protocol:"):
969 output += "\n%s%s" % (tabs(indent - 1), token)
970 indent = 0
971 context.add(PROTOCOL)
972 protocol = ""
973 continue
974
975 print "%s: Unknown token '%s' in frame '%s'" % (inname, token, frame)
976 continue
977
978 if (HEAD in context):
979 if (token == "{"):
980 output += "%s" % token
981 indent += 2
982 context.remove(HEAD)
983 context.add(BODY)
984 continue
985
986 if (token == ";"):
987 output += "%s\n" % token
988 context.remove(HEAD)
989 context.remove(FRAME)
990 continue
991
992 print "%s: Unknown token '%s' in frame head '%s'" % (inname, token, frame)
993
994 continue
995
996 if (not identifier(token)):
997 print "%s: Expected frame name" % inname
998 else:
999 frame = token
1000 output += "%s " % token
1001
1002 if (not frame in frame_properties):
1003 frame_properties[frame] = {}
1004
1005 frame_properties[frame]['name'] = frame
1006
1007 context.add(HEAD)
1008 continue
1009
1010 # "interface"
1011
1012 if (IFACE in context):
1013 if (NULL in context):
1014 if (token != ";"):
1015 print "%s: Expected ';' in interface '%s'" % (inname, interface)
1016 else:
1017 output += "%s\n" % token
1018
1019 context.remove(NULL)
1020 context.remove(IFACE)
1021 interface = None
1022 continue
1023
1024 if (BODY in context):
1025 if (PROTOCOL in context):
1026 if (token == "{"):
1027 indent += 1
1028 elif (token == "}"):
1029 indent -= 1
1030
1031 if (indent == -1):
1032 bp = split_bp(protocol)
1033 protocol = None
1034
1035 if (not interface in iface_properties):
1036 iface_properties[interface] = {}
1037
1038 if ('protocol' in iface_properties[interface]):
1039 print "%s: Protocol for interface '%s' already defined" % (inname, interface)
1040 else:
1041 iface_properties[interface]['protocol'] = bp
1042
1043 output += "\n%s" % tabs(2)
1044 output += parse_bp(inname, bp, 2)
1045 output += "\n%s" % token
1046 indent = 0
1047
1048 context.remove(PROTOCOL)
1049 context.remove(BODY)
1050 context.add(NULL)
1051 else:
1052 protocol += token
1053
1054 continue
1055
1056 if (PROTOTYPE in context):
1057 if (FIN in context):
1058 if (token != ";"):
1059 print "%s: Expected ';' in interface '%s'" % (inname, interface)
1060 else:
1061 output += "%s" % token
1062
1063 context.remove(FIN)
1064 context.remove(PROTOTYPE)
1065 continue
1066
1067 if (PAR_RIGHT in context):
1068 if (token == ")"):
1069 output += "%s" % token
1070 context.remove(PAR_RIGHT)
1071 context.add(FIN)
1072 else:
1073 output += " %s" % token
1074
1075 continue
1076
1077 if (SIGNATURE in context):
1078 output += "%s" % token
1079 if (token == ")"):
1080 context.remove(SIGNATURE)
1081 context.add(FIN)
1082
1083 context.remove(SIGNATURE)
1084 context.add(PAR_RIGHT)
1085 continue
1086
1087 if (PAR_LEFT in context):
1088 if (token != "("):
1089 print "%s: Expected '(' in interface '%s'" % (inname, interface)
1090 else:
1091 output += "%s" % token
1092
1093 context.remove(PAR_LEFT)
1094 context.add(SIGNATURE)
1095 continue
1096
1097 if (not identifier(token)):
1098 print "%s: Method identifier expected in interface '%s'" % (inname, interface)
1099 else:
1100 output += "%s" % token
1101
1102 context.add(PAR_LEFT)
1103 continue
1104
1105 if (token == "}"):
1106 if (indent != 2):
1107 print "%s: Wrong number of parentheses in interface '%s'" % (inname, interface)
1108 else:
1109 indent = 0
1110 output += "\n%s" % token
1111
1112 context.remove(BODY)
1113 context.add(NULL)
1114 continue
1115
1116 if ((token == "ipcarg_t") or (token == "unative_t")):
1117 output += "\n%s%s " % (tabs(indent), token)
1118 context.add(PROTOTYPE)
1119 continue
1120
1121 if (token == "protocol:"):
1122 output += "\n%s%s" % (tabs(indent - 1), token)
1123 indent = 0
1124 context.add(PROTOCOL)
1125 protocol = ""
1126 continue
1127
1128 print "%s: Unknown token '%s' in interface '%s'" % (inname, token, interface)
1129 continue
1130
1131 if (HEAD in context):
1132 if (PRE_BODY in context):
1133 if (token == "{"):
1134 output += "%s" % token
1135 indent += 2
1136 context.remove(PRE_BODY)
1137 context.remove(HEAD)
1138 context.add(BODY)
1139 continue
1140
1141 if (token == ";"):
1142 output += "%s\n" % token
1143 context.remove(PRE_BODY)
1144 context.remove(HEAD)
1145 context.remove(IFACE)
1146 continue
1147
1148 print "%s: Expected '{' or ';' in interface head '%s'" % (inname, interface)
1149 continue
1150
1151 if (EXTENDS in context):
1152 if (not identifier(token)):
1153 print "%s: Expected inherited interface name in interface head '%s'" % (inname, interface)
1154 else:
1155 output += "%s " % token
1156 if (not interface in iface_properties):
1157 iface_properties[interface] = {}
1158
1159 iface_properties[interface]['extends'] = token
1160
1161 context.remove(EXTENDS)
1162 context.add(PRE_BODY)
1163 continue
1164
1165 if (token == "extends"):
1166 output += "%s " % token
1167 context.add(EXTENDS)
1168 continue
1169
1170 if (token == "{"):
1171 output += "%s" % token
1172 indent += 2
1173 context.remove(HEAD)
1174 context.add(BODY)
1175 continue
1176
1177 if (token == ";"):
1178 output += "%s\n" % token
1179 context.remove(HEAD)
1180 context.remove(IFACE)
1181 continue
1182
1183 print "%s: Expected 'extends', '{' or ';' in interface head '%s'" % (inname, interface)
1184 continue
1185
1186 if (not identifier(token)):
1187 print "%s: Expected interface name" % inname
1188 else:
1189 interface = token
1190 output += "%s " % token
1191
1192 if (not interface in iface_properties):
1193 iface_properties[interface] = {}
1194
1195 iface_properties[interface]['name'] = interface
1196
1197 context.add(HEAD)
1198 continue
1199
1200 # "architecture"
1201
1202 if (ARCH in context):
1203 if (NULL in context):
1204 if (token != ";"):
1205 print "%s: Expected ';' in architecture '%s'" % (inname, architecture)
1206 else:
1207 output += "%s\n" % token
1208
1209 context.remove(NULL)
1210 context.remove(ARCH)
1211 context.discard(SYSTEM)
1212 architecture = None
1213 continue
1214
1215 if (BODY in context):
1216 if (DELEGATE in context):
1217 if (FIN in context):
1218 if (token != ";"):
1219 print "%s: Expected ';' in architecture '%s'" % (inname, architecture)
1220 else:
1221 output += "%s" % token
1222
1223 context.remove(FIN)
1224 context.remove(DELEGATE)
1225 continue
1226
1227 if (VAR in context):
1228 if (not descriptor(token)):
1229 print "%s: Expected interface descriptor in architecture '%s'" % (inname, architecture)
1230 else:
1231 if (not architecture in arch_properties):
1232 arch_properties[architecture] = {}
1233
1234 if (not 'delegate' in arch_properties[architecture]):
1235 arch_properties[architecture]['delegate'] = []
1236
1237 arch_properties[architecture]['delegate'].append({'from': arg0, 'to': token.split(":")})
1238 arg0 = None
1239
1240 output += "%s" % token
1241
1242 context.add(FIN)
1243 context.remove(VAR)
1244 continue
1245
1246 if (TO in context):
1247 if (token != "to"):
1248 print "%s: Expected 'to' in architecture '%s'" % (inname, architecture)
1249 else:
1250 output += "%s " % token
1251
1252 context.add(VAR)
1253 context.remove(TO)
1254 continue
1255
1256 if (not identifier(token)):
1257 print "%s: Expected interface name in architecture '%s'" % (inname, architecture)
1258 else:
1259 output += "%s " % token
1260 arg0 = token
1261
1262 context.add(TO)
1263 continue
1264
1265 if (SUBSUME in context):
1266 if (FIN in context):
1267 if (token != ";"):
1268 print "%s: Expected ';' in architecture '%s'" % (inname, architecture)
1269 else:
1270 output += "%s" % token
1271
1272 context.remove(FIN)
1273 context.remove(SUBSUME)
1274 continue
1275
1276 if (VAR in context):
1277 if (not identifier(token)):
1278 print "%s: Expected interface name in architecture '%s'" % (inname, architecture)
1279 else:
1280 if (not architecture in arch_properties):
1281 arch_properties[architecture] = {}
1282
1283 if (not 'subsume' in arch_properties[architecture]):
1284 arch_properties[architecture]['subsume'] = []
1285
1286 arch_properties[architecture]['subsume'].append({'from': arg0.split(":"), 'to': token})
1287 arg0 = None
1288
1289 output += "%s" % token
1290
1291 context.add(FIN)
1292 context.remove(VAR)
1293 continue
1294
1295 if (TO in context):
1296 if (token != "to"):
1297 print "%s: Expected 'to' in architecture '%s'" % (inname, architecture)
1298 else:
1299 output += "%s " % token
1300
1301 context.add(VAR)
1302 context.remove(TO)
1303 continue
1304
1305 if (not descriptor(token)):
1306 print "%s: Expected interface descriptor in architecture '%s'" % (inname, architecture)
1307 else:
1308 output += "%s " % token
1309 arg0 = token
1310
1311 context.add(TO)
1312 continue
1313
1314 if (BIND in context):
1315 if (FIN in context):
1316 if (token != ";"):
1317 print "%s: Expected ';' in architecture '%s'" % (inname, architecture)
1318 else:
1319 output += "%s" % token
1320
1321 context.remove(FIN)
1322 context.remove(BIND)
1323 continue
1324
1325 if (VAR in context):
1326 if (not descriptor(token)):
1327 print "%s: Expected second interface descriptor in architecture '%s'" % (inname, architecture)
1328 else:
1329 if (not architecture in arch_properties):
1330 arch_properties[architecture] = {}
1331
1332 if (not 'bind' in arch_properties[architecture]):
1333 arch_properties[architecture]['bind'] = []
1334
1335 arch_properties[architecture]['bind'].append({'from': arg0.split(":"), 'to': token.split(":")})
1336 arg0 = None
1337
1338 output += "%s" % token
1339
1340 context.add(FIN)
1341 context.remove(VAR)
1342 continue
1343
1344 if (TO in context):
1345 if (token != "to"):
1346 print "%s: Expected 'to' in architecture '%s'" % (inname, architecture)
1347 else:
1348 output += "%s " % token
1349
1350 context.add(VAR)
1351 context.remove(TO)
1352 continue
1353
1354 if (not descriptor(token)):
1355 print "%s: Expected interface descriptor in architecture '%s'" % (inname, architecture)
1356 else:
1357 output += "%s " % token
1358 arg0 = token
1359
1360 context.add(TO)
1361 continue
1362
1363 if (INST in context):
1364 if (FIN in context):
1365 if (token != ";"):
1366 print "%s: Expected ';' in architecture '%s'" % (inname, architecture)
1367 else:
1368 output += "%s" % token
1369
1370 context.remove(FIN)
1371 context.remove(INST)
1372 continue
1373
1374 if (VAR in context):
1375 if (not identifier(token)):
1376 print "%s: Expected instance name in architecture '%s'" % (inname, architecture)
1377 else:
1378 if (not architecture in arch_properties):
1379 arch_properties[architecture] = {}
1380
1381 if (not 'inst' in arch_properties[architecture]):
1382 arch_properties[architecture]['inst'] = []
1383
1384 arch_properties[architecture]['inst'].append({'type': arg0, 'var': token})
1385 arg0 = None
1386
1387 output += "%s" % token
1388
1389 context.add(FIN)
1390 context.remove(VAR)
1391 continue
1392
1393 if (not identifier(token)):
1394 print "%s: Expected frame/architecture type in architecture '%s'" % (inname, architecture)
1395 else:
1396 output += "%s " % token
1397 arg0 = token
1398
1399 context.add(VAR)
1400 continue
1401
1402 if (token == "}"):
1403 if (indent != 1):
1404 print "%s: Wrong number of parentheses in architecture '%s'" % (inname, architecture)
1405 else:
1406 indent -= 1
1407 output += "\n%s" % token
1408
1409 context.remove(BODY)
1410 context.add(NULL)
1411 continue
1412
1413 if (token == "inst"):
1414 output += "\n%s%s " % (tabs(indent), token)
1415 context.add(INST)
1416 continue
1417
1418 if (token == "bind"):
1419 output += "\n%s%s " % (tabs(indent), token)
1420 context.add(BIND)
1421 continue
1422
1423 if (token == "subsume"):
1424 output += "\n%s%s " % (tabs(indent), token)
1425 context.add(SUBSUME)
1426 continue
1427
1428 if (token == "delegate"):
1429 output += "\n%s%s " % (tabs(indent), token)
1430 context.add(DELEGATE)
1431 continue
1432
1433 print "%s: Unknown token '%s' in architecture '%s'" % (inname, token, architecture)
1434 continue
1435
1436 if (HEAD in context):
1437 if (token == "{"):
1438 output += "%s" % token
1439 indent += 1
1440 context.remove(HEAD)
1441 context.add(BODY)
1442 continue
1443
1444 if (token == ";"):
1445 output += "%s\n" % token
1446 context.remove(HEAD)
1447 context.remove(ARCH)
1448 context.discard(SYSTEM)
1449 continue
1450
1451 if (not word(token)):
1452 print "%s: Expected word in architecture head '%s'" % (inname, architecture)
1453 else:
1454 output += "%s " % token
1455
1456 continue
1457
1458 if (not identifier(token)):
1459 print "%s: Expected architecture name" % inname
1460 else:
1461 architecture = token
1462 output += "%s " % token
1463
1464 if (not architecture in arch_properties):
1465 arch_properties[architecture] = {}
1466
1467 arch_properties[architecture]['name'] = architecture
1468
1469 if (SYSTEM in context):
1470 arch_properties[architecture]['system'] = True
1471
1472 context.add(HEAD)
1473 continue
1474
1475 # "system architecture"
1476
1477 if (SYSTEM in context):
1478 if (token != "architecture"):
1479 print "%s: Expected 'architecture'" % inname
1480 else:
1481 output += "%s " % token
1482
1483 context.add(ARCH)
1484 continue
1485
1486 if (token == "frame"):
1487 output += "\n%s " % token
1488 context.add(FRAME)
1489 continue
1490
1491 if (token == "interface"):
1492 output += "\n%s " % token
1493 context.add(IFACE)
1494 continue
1495
1496 if (token == "system"):
1497 output += "\n%s " % token
1498 context.add(SYSTEM)
1499 continue
1500
1501 if (token == "architecture"):
1502 output += "\n%s " % token
1503 context.add(ARCH)
1504 continue
1505
1506 print "%s: Unknown token '%s'" % (inname, token)
1507
1508 inf.close()
1509
1510def open_adl(base, root, inname, outdir, outname):
1511 "Open Architecture Description file"
1512
1513 global output
1514 global context
1515 global architecture
1516 global interface
1517 global frame
1518 global protocol
1519 global initialization
1520 global finalization
1521
1522 global arg0
1523
1524 global opt_adl
1525
1526 output = ""
1527 context = set()
1528 architecture = None
1529 interface = None
1530 frame = None
1531 protocol = None
1532 initialization = None
1533 finalization = None
1534 arg0 = None
1535
1536 parse_adl(base, root, inname, False, 0)
1537 output = output.strip()
1538
1539 if ((output != "") and (opt_adl)):
1540 outf = file(outname, "w")
1541 outf.write(output)
1542 outf.close()
1543
1544def recursion(base, root, output, level):
1545 "Recursive directory walk"
1546
1547 for name in os.listdir(root):
1548 canon = os.path.join(root, name)
1549
1550 if (os.path.isfile(canon)):
1551 fcomp = split_tokens(canon, ["."])
1552 cname = canon.split("/")
1553
1554 if (fcomp[-1] == ".adl"):
1555 output_path = os.path.join(output, cname[-1])
1556 open_adl(base, root, canon, output, output_path)
1557
1558 if (os.path.isdir(canon)):
1559 recursion(base, canon, output, level + 1)
1560
1561def main():
1562 global iface_properties
1563 global frame_properties
1564 global arch_properties
1565 global opt_bp
1566 global opt_adl
1567
1568 if (len(sys.argv) < 3):
1569 usage(sys.argv[0])
1570 return
1571
1572 opt_bp = False
1573 opt_adl = False
1574
1575 for arg in sys.argv[1:(len(sys.argv) - 1)]:
1576 if (arg == "--dumpbp"):
1577 opt_bp = True
1578 elif (arg == "--dumpadl"):
1579 opt_adl = True
1580 elif (arg == "--noop"):
1581 pass
1582 else:
1583 print "Error: Unknown command line option '%s'" % arg
1584 return
1585
1586 path = os.path.abspath(sys.argv[-1])
1587 if (not os.path.isdir(path)):
1588 print "Error: <OUTPUT> is not a directory"
1589 return
1590
1591 iface_properties = {}
1592 frame_properties = {}
1593 arch_properties = {}
1594
1595 recursion(".", ".", path, 0)
1596 dump_archbp(path)
1597
1598if __name__ == '__main__':
1599 main()
Note: See TracBrowser for help on using the repository browser.