source: mainline/contrib/arch/hadlbppp.py@ 2088dfc

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

fix typos

  • Property mode set to 100755
File size: 39.1 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 <--bp|--ebp|--adl|--nop>+ <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 parse_ebp(component, name, tokens, base_indent):
374 "Parse Behavior Protocol and generate Extended Behavior Protocol output"
375
376 return "component %s {\n\tbehavior {\n\t\t%s\n\t}\n}" % (component, parse_bp(name, tokens, base_indent + 2))
377
378def get_iface(name):
379 "Get interface by name"
380
381 global iface_properties
382
383 if (name in iface_properties):
384 return iface_properties[name]
385
386 return None
387
388def inherited_protocols(iface):
389 "Get protocols inherited by an interface"
390
391 result = []
392
393 if ('extends' in iface):
394 supiface = get_iface(iface['extends'])
395 if (not supiface is None):
396 if ('protocol' in supiface):
397 result.append(supiface['protocol'])
398 result.extend(inherited_protocols(supiface))
399 else:
400 print "%s: Extends unknown interface '%s'" % (iface['name'], iface['extends'])
401
402 return result
403
404def dump_frame(frame, outdir, var, archf):
405 "Dump Behavior Protocol of a given frame"
406
407 global opt_bp
408 global opt_ebp
409
410 if (opt_ebp):
411 fname = "%s.ebp" % frame['name']
412 else:
413 fname = "%s.bp" % frame['name']
414
415 if (archf != None):
416 archf.write("instantiate %s from \"%s\"\n" % (var, fname))
417
418 outname = os.path.join(outdir, fname)
419
420 protocols = []
421 if ('protocol' in frame):
422 protocols.append(frame['protocol'])
423
424 if ('initialization' in frame):
425 initialization = frame['initialization']
426 else:
427 initialization = None
428
429 if ('finalization' in frame):
430 finalization = frame['finalization']
431 else:
432 finalization = None
433
434 if ('provides' in frame):
435 for provides in frame['provides']:
436 iface = get_iface(provides['iface'])
437 if (not iface is None):
438 if ('protocol' in iface):
439 protocols.append(extend_bp(outname, iface['protocol'], iface['name']))
440 for protocol in inherited_protocols(iface):
441 protocols.append(extend_bp(outname, protocol, iface['name']))
442 else:
443 print "%s: Provided interface '%s' is undefined" % (frame['name'], provides['iface'])
444
445 if (opt_bp):
446 outf = file(outname, "w")
447 outf.write(parse_bp(outname, merge_bp(initialization, finalization, protocols), 0))
448 outf.close()
449
450 if (opt_ebp):
451 outf = file(outname, "w")
452 outf.write(parse_ebp(frame['name'], outname, merge_bp(initialization, finalization, protocols), 0))
453 outf.close()
454
455def get_system_arch():
456 "Get system architecture"
457
458 global arch_properties
459
460 for arch, properties in arch_properties.items():
461 if ('system' in properties):
462 return properties
463
464 return None
465
466def get_arch(name):
467 "Get architecture by name"
468
469 global arch_properties
470
471 if (name in arch_properties):
472 return arch_properties[name]
473
474 return None
475
476def get_frame(name):
477 "Get frame by name"
478
479 global frame_properties
480
481 if (name in frame_properties):
482 return frame_properties[name]
483
484 return None
485
486def create_null_bp(fname, outdir, archf):
487 "Create null frame protocol"
488
489 global opt_bp
490 global opt_ebp
491
492 if (archf != None):
493 archf.write("frame \"%s\"\n" % fname)
494
495 outname = os.path.join(outdir, fname)
496
497 if (opt_bp):
498 outf = file(outname, "w")
499 outf.write("NULL")
500 outf.close()
501
502 if (opt_ebp):
503 outf = file(outname, "w")
504 outf.write("component null {\n\tbehavior {\n\t\tNULL\n\t}\n}")
505 outf.close()
506
507def flatten_binds(binds, delegates, subsumes):
508 "Remove bindings which are replaced by delegation or subsumption"
509
510 result = []
511 stable = True
512
513 for bind in binds:
514 keep = True
515
516 for delegate in delegates:
517 if (bind['to'] == delegate['to']):
518 keep = False
519 result.append({'from': bind['from'], 'to': delegate['rep']})
520
521 for subsume in subsumes:
522 if (bind['from'] == subsume['from']):
523 keep = False
524 result.append({'from': subsume['rep'], 'to': bind['to']})
525
526 if (keep):
527 result.append(bind)
528 else:
529 stable = False
530
531 if (stable):
532 return result
533 else:
534 return flatten_binds(result, delegates, subsumes)
535
536def direct_binds(binds):
537 "Convert bindings matrix to set of sources by destination"
538
539 result = {}
540
541 for bind in binds:
542 if (not bind['to'] in result):
543 result[bind['to']] = set()
544
545 result[bind['to']].add(bind['from'])
546
547 return result
548
549def merge_subarch(prefix, arch, outdir):
550 "Merge subarchitecture into architecture"
551
552 insts = []
553 binds = []
554 delegates = []
555 subsumes = []
556
557 if ('inst' in arch):
558 for inst in arch['inst']:
559 subarch = get_arch(inst['type'])
560 if (not subarch is None):
561 (subinsts, subbinds, subdelegates, subsubsumes) = merge_subarch("%s_%s" % (prefix, subarch['name']), subarch, outdir)
562 insts.extend(subinsts)
563 binds.extend(subbinds)
564 delegates.extend(subdelegates)
565 subsumes.extend(subsubsumes)
566 else:
567 subframe = get_frame(inst['type'])
568 if (not subframe is None):
569 insts.append({'var': "%s_%s" % (prefix, inst['var']), 'frame': subframe})
570 else:
571 print "%s: '%s' is neither an architecture nor a frame" % (arch['name'], inst['type'])
572
573 if ('bind' in arch):
574 for bind in arch['bind']:
575 binds.append({'from': "%s_%s.%s" % (prefix, bind['from'][0], bind['from'][1]), 'to': "%s_%s.%s" % (prefix, bind['to'][0], bind['to'][1])})
576
577 if ('delegate' in arch):
578 for delegate in arch['delegate']:
579 delegates.append({'to': "%s.%s" % (prefix, delegate['from']), 'rep': "%s_%s.%s" % (prefix, delegate['to'][0], delegate['to'][1])})
580
581 if ('subsume' in arch):
582 for subsume in arch['subsume']:
583 subsumes.append({'from': "%s.%s" % (prefix, subsume['to']), 'rep': "%s_%s.%s" % (prefix, subsume['from'][0], subsume['from'][1])})
584
585 return (insts, binds, delegates, subsumes)
586
587def dump_archbp(outdir):
588 "Dump system architecture Behavior Protocol"
589
590 global opt_bp
591 global opt_ebp
592
593 arch = get_system_arch()
594
595 if (arch is None):
596 print "Unable to find system architecture"
597 return
598
599 insts = []
600 binds = []
601 delegates = []
602 subsumes = []
603
604 if ('inst' in arch):
605 for inst in arch['inst']:
606 subarch = get_arch(inst['type'])
607 if (not subarch is None):
608 (subinsts, subbinds, subdelegates, subsubsumes) = merge_subarch(subarch['name'], subarch, outdir)
609 insts.extend(subinsts)
610 binds.extend(subbinds)
611 delegates.extend(subdelegates)
612 subsumes.extend(subsubsumes)
613 else:
614 subframe = get_frame(inst['type'])
615 if (not subframe is None):
616 insts.append({'var': inst['var'], 'frame': subframe})
617 else:
618 print "%s: '%s' is neither an architecture nor a frame" % (arch['name'], inst['type'])
619
620 if ('bind' in arch):
621 for bind in arch['bind']:
622 binds.append({'from': "%s.%s" % (bind['from'][0], bind['from'][1]), 'to': "%s.%s" % (bind['to'][0], bind['to'][1])})
623
624 if ('delegate' in arch):
625 for delegate in arch['delegate']:
626 print "Unable to delegate interface in system architecture"
627 break
628
629 if ('subsume' in arch):
630 for subsume in arch['subsume']:
631 print "Unable to subsume interface in system architecture"
632 break
633
634
635 outname = os.path.join(outdir, "%s.archbp" % arch['name'])
636 if ((opt_bp) or (opt_ebp)):
637 outf = file(outname, "w")
638 else:
639 outf = None
640
641 create_null_bp("null.bp", outdir, outf)
642
643 for inst in insts:
644 dump_frame(inst['frame'], outdir, inst['var'], outf)
645
646 directed_binds = direct_binds(flatten_binds(binds, delegates, subsumes))
647
648 for dst, src in directed_binds.items():
649 if (outf != None):
650 outf.write("bind %s to %s\n" % (", ".join(src), dst))
651
652 if (outf != None):
653 outf.close()
654
655def preproc_adl(raw, inarg):
656 "Preprocess %% statements in ADL"
657
658 return raw.replace("%%", inarg)
659
660def parse_adl(base, root, inname, nested, indent):
661 "Parse Architecture Description Language"
662
663 global output
664 global context
665 global architecture
666 global interface
667 global frame
668 global protocol
669 global initialization
670 global finalization
671
672 global iface_properties
673 global frame_properties
674 global arch_properties
675
676 global arg0
677
678 if (nested):
679 parts = inname.split("%")
680
681 if (len(parts) > 1):
682 inarg = parts[1]
683 else:
684 inarg = "%%"
685
686 if (parts[0][0:1] == "/"):
687 path = os.path.join(base, ".%s" % parts[0])
688 nested_root = os.path.dirname(path)
689 else:
690 path = os.path.join(root, parts[0])
691 nested_root = root
692
693 if (not os.path.isfile(path)):
694 print "%s: Unable to include file %s" % (inname, path)
695 return ""
696 else:
697 inarg = "%%"
698 path = inname
699 nested_root = root
700
701 inf = file(path, "r")
702
703 raw = preproc_adl(inf.read(), inarg)
704 tokens = split_tokens(raw, ["\n", " ", "\t", "(", ")", "{", "}", "[", "]", "/*", "*/", "#", ";"], True, True)
705
706 for token in tokens:
707
708 # Includes
709
710 if (INC in context):
711 context.remove(INC)
712 parse_adl(base, nested_root, token, True, indent)
713 context.add(POST_INC)
714 continue
715
716 if (POST_INC in context):
717 if (token != "]"):
718 print "%s: Expected ]" % inname
719
720 context.remove(POST_INC)
721 continue
722
723 # Comments and newlines
724
725 if (BLOCK_COMMENT in context):
726 if (token == "*/"):
727 context.remove(BLOCK_COMMENT)
728
729 continue
730
731 if (LINE_COMMENT in context):
732 if (token == "\n"):
733 context.remove(LINE_COMMENT)
734
735 continue
736
737 # Any context
738
739 if (token == "/*"):
740 context.add(BLOCK_COMMENT)
741 continue
742
743 if (token == "#"):
744 context.add(LINE_COMMENT)
745 continue
746
747 if (token == "["):
748 context.add(INC)
749 continue
750
751 if (token == "\n"):
752 continue
753
754 # "frame"
755
756 if (FRAME in context):
757 if (NULL in context):
758 if (token != ";"):
759 print "%s: Expected ';' in frame '%s'" % (inname, frame)
760 else:
761 output += "%s\n" % token
762
763 context.remove(NULL)
764 context.remove(FRAME)
765 frame = None
766 continue
767
768 if (BODY in context):
769 if (FINALIZATION in context):
770 if (token == "{"):
771 indent += 1
772 elif (token == "}"):
773 indent -= 1
774
775 if (((token[-1] == ":") and (indent == 0)) or (indent == -1)):
776 bp = split_bp(finalization)
777 finalization = None
778
779 if (not frame in frame_properties):
780 frame_properties[frame] = {}
781
782 if ('finalization' in frame_properties[frame]):
783 print "%s: Finalization protocol for frame '%s' already defined" % (inname, frame)
784 else:
785 frame_properties[frame]['finalization'] = bp
786
787 output += "\n%s" % tabs(2)
788 output += parse_bp(inname, bp, 2)
789
790 context.remove(FINALIZATION)
791 if (indent == -1):
792 output += "\n%s" % token
793 context.remove(BODY)
794 context.add(NULL)
795 indent = 0
796 continue
797 else:
798 indent = 2
799 else:
800 finalization += token
801 continue
802
803 if (INITIALIZATION in context):
804 if (token == "{"):
805 indent += 1
806 elif (token == "}"):
807 indent -= 1
808
809 if (((token[-1] == ":") and (indent == 0)) or (indent == -1)):
810 bp = split_bp(initialization)
811 initialization = None
812
813 if (not frame in frame_properties):
814 frame_properties[frame] = {}
815
816 if ('initialization' in frame_properties[frame]):
817 print "%s: Initialization protocol for frame '%s' already defined" % (inname, frame)
818 else:
819 frame_properties[frame]['initialization'] = bp
820
821 output += "\n%s" % tabs(2)
822 output += parse_bp(inname, bp, 2)
823
824 context.remove(INITIALIZATION)
825 if (indent == -1):
826 output += "\n%s" % token
827 context.remove(BODY)
828 context.add(NULL)
829 indent = 0
830 continue
831 else:
832 indent = 2
833 else:
834 initialization += token
835 continue
836
837 if (PROTOCOL in context):
838 if (token == "{"):
839 indent += 1
840 elif (token == "}"):
841 indent -= 1
842
843 if (((token[-1] == ":") and (indent == 0)) or (indent == -1)):
844 bp = split_bp(protocol)
845 protocol = None
846
847 if (not frame in frame_properties):
848 frame_properties[frame] = {}
849
850 if ('protocol' in frame_properties[frame]):
851 print "%s: Protocol for frame '%s' already defined" % (inname, frame)
852 else:
853 frame_properties[frame]['protocol'] = bp
854
855 output += "\n%s" % tabs(2)
856 output += parse_bp(inname, bp, 2)
857
858 context.remove(PROTOCOL)
859 if (indent == -1):
860 output += "\n%s" % token
861 context.remove(BODY)
862 context.add(NULL)
863 indent = 0
864 continue
865 else:
866 indent = 2
867 else:
868 protocol += token
869 continue
870
871 if (REQUIRES in context):
872 if (FIN in context):
873 if (token != ";"):
874 print "%s: Expected ';' in frame '%s'" % (inname, frame)
875 else:
876 output += "%s" % token
877
878 context.remove(FIN)
879 continue
880
881 if (VAR in context):
882 if (not identifier(token)):
883 print "%s: Variable name expected in frame '%s'" % (inname, frame)
884 else:
885 if (not frame in frame_properties):
886 frame_properties[frame] = {}
887
888 if (not 'requires' in frame_properties[frame]):
889 frame_properties[frame]['requires'] = []
890
891 frame_properties[frame]['requires'].append({'iface': arg0, 'var': token})
892 arg0 = None
893
894 output += "%s" % token
895
896 context.remove(VAR)
897 context.add(FIN)
898 continue
899
900 if ((token == "}") or (token[-1] == ":")):
901 context.remove(REQUIRES)
902 else:
903 if (not identifier(token)):
904 print "%s: Interface name expected in frame '%s'" % (inname, frame)
905 else:
906 arg0 = token
907 output += "\n%s%s " % (tabs(indent), token)
908
909 context.add(VAR)
910 continue
911
912 if (PROVIDES in context):
913 if (FIN in context):
914 if (token != ";"):
915 print "%s: Expected ';' in frame '%s'" % (inname, frame)
916 else:
917 output += "%s" % token
918
919 context.remove(FIN)
920 continue
921
922 if (VAR in context):
923 if (not identifier(token)):
924 print "%s: Variable name expected in frame '%s'" % (inname, frame)
925 else:
926 if (not frame in frame_properties):
927 frame_properties[frame] = {}
928
929 if (not 'provides' in frame_properties[frame]):
930 frame_properties[frame]['provides'] = []
931
932 frame_properties[frame]['provides'].append({'iface': arg0, 'var': token})
933 arg0 = None
934
935 output += "%s" % token
936
937 context.remove(VAR)
938 context.add(FIN)
939 continue
940
941 if ((token == "}") or (token[-1] == ":")):
942 context.remove(PROVIDES)
943 else:
944 if (not identifier(token)):
945 print "%s: Interface name expected in frame '%s'" % (inname, frame)
946 else:
947 arg0 = token
948 output += "\n%s%s " % (tabs(indent), token)
949
950 context.add(VAR)
951 continue
952
953 if (token == "}"):
954 if (indent != 2):
955 print "%s: Wrong number of parentheses in frame '%s'" % (inname, frame)
956 else:
957 indent = 0
958 output += "\n%s" % token
959
960 context.remove(BODY)
961 context.add(NULL)
962 continue
963
964 if (token == "provides:"):
965 output += "\n%s%s" % (tabs(indent - 1), token)
966 context.add(PROVIDES)
967 continue
968
969 if (token == "requires:"):
970 output += "\n%s%s" % (tabs(indent - 1), token)
971 context.add(REQUIRES)
972 continue
973
974 if (token == "initialization:"):
975 output += "\n%s%s" % (tabs(indent - 1), token)
976 indent = 0
977 context.add(INITIALIZATION)
978 initialization = ""
979 continue
980
981 if (token == "finalization:"):
982 output += "\n%s%s" % (tabs(indent - 1), token)
983 indent = 0
984 context.add(FINALIZATION)
985 finalization = ""
986 continue
987
988 if (token == "protocol:"):
989 output += "\n%s%s" % (tabs(indent - 1), token)
990 indent = 0
991 context.add(PROTOCOL)
992 protocol = ""
993 continue
994
995 print "%s: Unknown token '%s' in frame '%s'" % (inname, token, frame)
996 continue
997
998 if (HEAD in context):
999 if (token == "{"):
1000 output += "%s" % token
1001 indent += 2
1002 context.remove(HEAD)
1003 context.add(BODY)
1004 continue
1005
1006 if (token == ";"):
1007 output += "%s\n" % token
1008 context.remove(HEAD)
1009 context.remove(FRAME)
1010 continue
1011
1012 print "%s: Unknown token '%s' in frame head '%s'" % (inname, token, frame)
1013
1014 continue
1015
1016 if (not identifier(token)):
1017 print "%s: Expected frame name" % inname
1018 else:
1019 frame = token
1020 output += "%s " % token
1021
1022 if (not frame in frame_properties):
1023 frame_properties[frame] = {}
1024
1025 frame_properties[frame]['name'] = frame
1026
1027 context.add(HEAD)
1028 continue
1029
1030 # "interface"
1031
1032 if (IFACE in context):
1033 if (NULL in context):
1034 if (token != ";"):
1035 print "%s: Expected ';' in interface '%s'" % (inname, interface)
1036 else:
1037 output += "%s\n" % token
1038
1039 context.remove(NULL)
1040 context.remove(IFACE)
1041 interface = None
1042 continue
1043
1044 if (BODY in context):
1045 if (PROTOCOL in context):
1046 if (token == "{"):
1047 indent += 1
1048 elif (token == "}"):
1049 indent -= 1
1050
1051 if (indent == -1):
1052 bp = split_bp(protocol)
1053 protocol = None
1054
1055 if (not interface in iface_properties):
1056 iface_properties[interface] = {}
1057
1058 if ('protocol' in iface_properties[interface]):
1059 print "%s: Protocol for interface '%s' already defined" % (inname, interface)
1060 else:
1061 iface_properties[interface]['protocol'] = bp
1062
1063 output += "\n%s" % tabs(2)
1064 output += parse_bp(inname, bp, 2)
1065 output += "\n%s" % token
1066 indent = 0
1067
1068 context.remove(PROTOCOL)
1069 context.remove(BODY)
1070 context.add(NULL)
1071 else:
1072 protocol += token
1073
1074 continue
1075
1076 if (PROTOTYPE in context):
1077 if (FIN in context):
1078 if (token != ";"):
1079 print "%s: Expected ';' in interface '%s'" % (inname, interface)
1080 else:
1081 output += "%s" % token
1082
1083 context.remove(FIN)
1084 context.remove(PROTOTYPE)
1085 continue
1086
1087 if (PAR_RIGHT in context):
1088 if (token == ")"):
1089 output += "%s" % token
1090 context.remove(PAR_RIGHT)
1091 context.add(FIN)
1092 else:
1093 output += " %s" % token
1094
1095 continue
1096
1097 if (SIGNATURE in context):
1098 output += "%s" % token
1099 if (token == ")"):
1100 context.remove(SIGNATURE)
1101 context.add(FIN)
1102
1103 context.remove(SIGNATURE)
1104 context.add(PAR_RIGHT)
1105 continue
1106
1107 if (PAR_LEFT in context):
1108 if (token != "("):
1109 print "%s: Expected '(' in interface '%s'" % (inname, interface)
1110 else:
1111 output += "%s" % token
1112
1113 context.remove(PAR_LEFT)
1114 context.add(SIGNATURE)
1115 continue
1116
1117 if (not identifier(token)):
1118 print "%s: Method identifier expected in interface '%s'" % (inname, interface)
1119 else:
1120 output += "%s" % token
1121
1122 context.add(PAR_LEFT)
1123 continue
1124
1125 if (token == "}"):
1126 if (indent != 2):
1127 print "%s: Wrong number of parentheses in interface '%s'" % (inname, interface)
1128 else:
1129 indent = 0
1130 output += "\n%s" % token
1131
1132 context.remove(BODY)
1133 context.add(NULL)
1134 continue
1135
1136 if ((token == "ipcarg_t") or (token == "unative_t")):
1137 output += "\n%s%s " % (tabs(indent), token)
1138 context.add(PROTOTYPE)
1139 continue
1140
1141 if (token == "protocol:"):
1142 output += "\n%s%s" % (tabs(indent - 1), token)
1143 indent = 0
1144 context.add(PROTOCOL)
1145 protocol = ""
1146 continue
1147
1148 print "%s: Unknown token '%s' in interface '%s'" % (inname, token, interface)
1149 continue
1150
1151 if (HEAD in context):
1152 if (PRE_BODY in context):
1153 if (token == "{"):
1154 output += "%s" % token
1155 indent += 2
1156 context.remove(PRE_BODY)
1157 context.remove(HEAD)
1158 context.add(BODY)
1159 continue
1160
1161 if (token == ";"):
1162 output += "%s\n" % token
1163 context.remove(PRE_BODY)
1164 context.remove(HEAD)
1165 context.remove(IFACE)
1166 continue
1167
1168 print "%s: Expected '{' or ';' in interface head '%s'" % (inname, interface)
1169 continue
1170
1171 if (EXTENDS in context):
1172 if (not identifier(token)):
1173 print "%s: Expected inherited interface name in interface head '%s'" % (inname, interface)
1174 else:
1175 output += "%s " % token
1176 if (not interface in iface_properties):
1177 iface_properties[interface] = {}
1178
1179 iface_properties[interface]['extends'] = token
1180
1181 context.remove(EXTENDS)
1182 context.add(PRE_BODY)
1183 continue
1184
1185 if (token == "extends"):
1186 output += "%s " % token
1187 context.add(EXTENDS)
1188 continue
1189
1190 if (token == "{"):
1191 output += "%s" % token
1192 indent += 2
1193 context.remove(HEAD)
1194 context.add(BODY)
1195 continue
1196
1197 if (token == ";"):
1198 output += "%s\n" % token
1199 context.remove(HEAD)
1200 context.remove(IFACE)
1201 continue
1202
1203 print "%s: Expected 'extends', '{' or ';' in interface head '%s'" % (inname, interface)
1204 continue
1205
1206 if (not identifier(token)):
1207 print "%s: Expected interface name" % inname
1208 else:
1209 interface = token
1210 output += "%s " % token
1211
1212 if (not interface in iface_properties):
1213 iface_properties[interface] = {}
1214
1215 iface_properties[interface]['name'] = interface
1216
1217 context.add(HEAD)
1218 continue
1219
1220 # "architecture"
1221
1222 if (ARCH in context):
1223 if (NULL in context):
1224 if (token != ";"):
1225 print "%s: Expected ';' in architecture '%s'" % (inname, architecture)
1226 else:
1227 output += "%s\n" % token
1228
1229 context.remove(NULL)
1230 context.remove(ARCH)
1231 context.discard(SYSTEM)
1232 architecture = None
1233 continue
1234
1235 if (BODY in context):
1236 if (DELEGATE in context):
1237 if (FIN in context):
1238 if (token != ";"):
1239 print "%s: Expected ';' in architecture '%s'" % (inname, architecture)
1240 else:
1241 output += "%s" % token
1242
1243 context.remove(FIN)
1244 context.remove(DELEGATE)
1245 continue
1246
1247 if (VAR in context):
1248 if (not descriptor(token)):
1249 print "%s: Expected interface descriptor in architecture '%s'" % (inname, architecture)
1250 else:
1251 if (not architecture in arch_properties):
1252 arch_properties[architecture] = {}
1253
1254 if (not 'delegate' in arch_properties[architecture]):
1255 arch_properties[architecture]['delegate'] = []
1256
1257 arch_properties[architecture]['delegate'].append({'from': arg0, 'to': token.split(":")})
1258 arg0 = None
1259
1260 output += "%s" % token
1261
1262 context.add(FIN)
1263 context.remove(VAR)
1264 continue
1265
1266 if (TO in context):
1267 if (token != "to"):
1268 print "%s: Expected 'to' in architecture '%s'" % (inname, architecture)
1269 else:
1270 output += "%s " % token
1271
1272 context.add(VAR)
1273 context.remove(TO)
1274 continue
1275
1276 if (not identifier(token)):
1277 print "%s: Expected interface name in architecture '%s'" % (inname, architecture)
1278 else:
1279 output += "%s " % token
1280 arg0 = token
1281
1282 context.add(TO)
1283 continue
1284
1285 if (SUBSUME in context):
1286 if (FIN in context):
1287 if (token != ";"):
1288 print "%s: Expected ';' in architecture '%s'" % (inname, architecture)
1289 else:
1290 output += "%s" % token
1291
1292 context.remove(FIN)
1293 context.remove(SUBSUME)
1294 continue
1295
1296 if (VAR in context):
1297 if (not identifier(token)):
1298 print "%s: Expected interface name in architecture '%s'" % (inname, architecture)
1299 else:
1300 if (not architecture in arch_properties):
1301 arch_properties[architecture] = {}
1302
1303 if (not 'subsume' in arch_properties[architecture]):
1304 arch_properties[architecture]['subsume'] = []
1305
1306 arch_properties[architecture]['subsume'].append({'from': arg0.split(":"), 'to': token})
1307 arg0 = None
1308
1309 output += "%s" % token
1310
1311 context.add(FIN)
1312 context.remove(VAR)
1313 continue
1314
1315 if (TO in context):
1316 if (token != "to"):
1317 print "%s: Expected 'to' in architecture '%s'" % (inname, architecture)
1318 else:
1319 output += "%s " % token
1320
1321 context.add(VAR)
1322 context.remove(TO)
1323 continue
1324
1325 if (not descriptor(token)):
1326 print "%s: Expected interface descriptor in architecture '%s'" % (inname, architecture)
1327 else:
1328 output += "%s " % token
1329 arg0 = token
1330
1331 context.add(TO)
1332 continue
1333
1334 if (BIND in context):
1335 if (FIN in context):
1336 if (token != ";"):
1337 print "%s: Expected ';' in architecture '%s'" % (inname, architecture)
1338 else:
1339 output += "%s" % token
1340
1341 context.remove(FIN)
1342 context.remove(BIND)
1343 continue
1344
1345 if (VAR in context):
1346 if (not descriptor(token)):
1347 print "%s: Expected second interface descriptor in architecture '%s'" % (inname, architecture)
1348 else:
1349 if (not architecture in arch_properties):
1350 arch_properties[architecture] = {}
1351
1352 if (not 'bind' in arch_properties[architecture]):
1353 arch_properties[architecture]['bind'] = []
1354
1355 arch_properties[architecture]['bind'].append({'from': arg0.split(":"), 'to': token.split(":")})
1356 arg0 = None
1357
1358 output += "%s" % token
1359
1360 context.add(FIN)
1361 context.remove(VAR)
1362 continue
1363
1364 if (TO in context):
1365 if (token != "to"):
1366 print "%s: Expected 'to' in architecture '%s'" % (inname, architecture)
1367 else:
1368 output += "%s " % token
1369
1370 context.add(VAR)
1371 context.remove(TO)
1372 continue
1373
1374 if (not descriptor(token)):
1375 print "%s: Expected interface descriptor in architecture '%s'" % (inname, architecture)
1376 else:
1377 output += "%s " % token
1378 arg0 = token
1379
1380 context.add(TO)
1381 continue
1382
1383 if (INST in context):
1384 if (FIN in context):
1385 if (token != ";"):
1386 print "%s: Expected ';' in architecture '%s'" % (inname, architecture)
1387 else:
1388 output += "%s" % token
1389
1390 context.remove(FIN)
1391 context.remove(INST)
1392 continue
1393
1394 if (VAR in context):
1395 if (not identifier(token)):
1396 print "%s: Expected instance name in architecture '%s'" % (inname, architecture)
1397 else:
1398 if (not architecture in arch_properties):
1399 arch_properties[architecture] = {}
1400
1401 if (not 'inst' in arch_properties[architecture]):
1402 arch_properties[architecture]['inst'] = []
1403
1404 arch_properties[architecture]['inst'].append({'type': arg0, 'var': token})
1405 arg0 = None
1406
1407 output += "%s" % token
1408
1409 context.add(FIN)
1410 context.remove(VAR)
1411 continue
1412
1413 if (not identifier(token)):
1414 print "%s: Expected frame/architecture type in architecture '%s'" % (inname, architecture)
1415 else:
1416 output += "%s " % token
1417 arg0 = token
1418
1419 context.add(VAR)
1420 continue
1421
1422 if (token == "}"):
1423 if (indent != 1):
1424 print "%s: Wrong number of parentheses in architecture '%s'" % (inname, architecture)
1425 else:
1426 indent -= 1
1427 output += "\n%s" % token
1428
1429 context.remove(BODY)
1430 context.add(NULL)
1431 continue
1432
1433 if (token == "inst"):
1434 output += "\n%s%s " % (tabs(indent), token)
1435 context.add(INST)
1436 continue
1437
1438 if (token == "bind"):
1439 output += "\n%s%s " % (tabs(indent), token)
1440 context.add(BIND)
1441 continue
1442
1443 if (token == "subsume"):
1444 output += "\n%s%s " % (tabs(indent), token)
1445 context.add(SUBSUME)
1446 continue
1447
1448 if (token == "delegate"):
1449 output += "\n%s%s " % (tabs(indent), token)
1450 context.add(DELEGATE)
1451 continue
1452
1453 print "%s: Unknown token '%s' in architecture '%s'" % (inname, token, architecture)
1454 continue
1455
1456 if (HEAD in context):
1457 if (token == "{"):
1458 output += "%s" % token
1459 indent += 1
1460 context.remove(HEAD)
1461 context.add(BODY)
1462 continue
1463
1464 if (token == ";"):
1465 output += "%s\n" % token
1466 context.remove(HEAD)
1467 context.remove(ARCH)
1468 context.discard(SYSTEM)
1469 continue
1470
1471 if (not word(token)):
1472 print "%s: Expected word in architecture head '%s'" % (inname, architecture)
1473 else:
1474 output += "%s " % token
1475
1476 continue
1477
1478 if (not identifier(token)):
1479 print "%s: Expected architecture name" % inname
1480 else:
1481 architecture = token
1482 output += "%s " % token
1483
1484 if (not architecture in arch_properties):
1485 arch_properties[architecture] = {}
1486
1487 arch_properties[architecture]['name'] = architecture
1488
1489 if (SYSTEM in context):
1490 arch_properties[architecture]['system'] = True
1491
1492 context.add(HEAD)
1493 continue
1494
1495 # "system architecture"
1496
1497 if (SYSTEM in context):
1498 if (token != "architecture"):
1499 print "%s: Expected 'architecture'" % inname
1500 else:
1501 output += "%s " % token
1502
1503 context.add(ARCH)
1504 continue
1505
1506 if (token == "frame"):
1507 output += "\n%s " % token
1508 context.add(FRAME)
1509 continue
1510
1511 if (token == "interface"):
1512 output += "\n%s " % token
1513 context.add(IFACE)
1514 continue
1515
1516 if (token == "system"):
1517 output += "\n%s " % token
1518 context.add(SYSTEM)
1519 continue
1520
1521 if (token == "architecture"):
1522 output += "\n%s " % token
1523 context.add(ARCH)
1524 continue
1525
1526 print "%s: Unknown token '%s'" % (inname, token)
1527
1528 inf.close()
1529
1530def open_adl(base, root, inname, outdir, outname):
1531 "Open Architecture Description file"
1532
1533 global output
1534 global context
1535 global architecture
1536 global interface
1537 global frame
1538 global protocol
1539 global initialization
1540 global finalization
1541
1542 global arg0
1543
1544 global opt_adl
1545
1546 output = ""
1547 context = set()
1548 architecture = None
1549 interface = None
1550 frame = None
1551 protocol = None
1552 initialization = None
1553 finalization = None
1554 arg0 = None
1555
1556 parse_adl(base, root, inname, False, 0)
1557 output = output.strip()
1558
1559 if ((output != "") and (opt_adl)):
1560 outf = file(outname, "w")
1561 outf.write(output)
1562 outf.close()
1563
1564def recursion(base, root, output, level):
1565 "Recursive directory walk"
1566
1567 for name in os.listdir(root):
1568 canon = os.path.join(root, name)
1569
1570 if (os.path.isfile(canon)):
1571 fcomp = split_tokens(canon, ["."])
1572 cname = canon.split("/")
1573
1574 if (fcomp[-1] == ".adl"):
1575 output_path = os.path.join(output, cname[-1])
1576 open_adl(base, root, canon, output, output_path)
1577
1578 if (os.path.isdir(canon)):
1579 recursion(base, canon, output, level + 1)
1580
1581def main():
1582 global iface_properties
1583 global frame_properties
1584 global arch_properties
1585 global opt_bp
1586 global opt_ebp
1587 global opt_adl
1588
1589 if (len(sys.argv) < 3):
1590 usage(sys.argv[0])
1591 return
1592
1593 opt_bp = False
1594 opt_ebp = False
1595 opt_adl = False
1596
1597 for arg in sys.argv[1:(len(sys.argv) - 1)]:
1598 if (arg == "--bp"):
1599 opt_bp = True
1600 elif (arg == "--ebp"):
1601 opt_ebp = True
1602 elif (arg == "--adl"):
1603 opt_adl = True
1604 elif (arg == "--nop"):
1605 pass
1606 else:
1607 print "Error: Unknown command line option '%s'" % arg
1608 return
1609
1610 if ((opt_bp) and (opt_ebp)):
1611 print "Error: Cannot dump both original Behavior Protocols and Extended Behavior Protocols"
1612 return
1613
1614 path = os.path.abspath(sys.argv[-1])
1615 if (not os.path.isdir(path)):
1616 print "Error: <OUTPUT> is not a directory"
1617 return
1618
1619 iface_properties = {}
1620 frame_properties = {}
1621 arch_properties = {}
1622
1623 recursion(".", ".", path, 0)
1624 dump_archbp(path)
1625
1626if __name__ == '__main__':
1627 main()
Note: See TracBrowser for help on using the repository browser.