Changeset fed03a3 in mainline
- Timestamp:
- 2009-09-16T16:40:28Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 704311b
- Parents:
- ee5b35a
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
contrib/arch/hadlbppp.py
ree5b35a rfed03a3 165 165 return result 166 166 167 def parse_bp(name, protocol, base_indent): 167 def split_bp(protocol): 168 "Convert Behavior Protocol to tokens" 169 170 return split_tokens(protocol, ["\n", " ", "\t", "(", ")", "{", "}", "*", ";", "+", "||", "|", "!", "?"], True, True) 171 172 def extend_bp(name, tokens, iface): 173 "Add interface to incoming messages" 174 175 result = [] 176 i = 0 177 178 while (i < len(tokens)): 179 result.append(tokens[i]) 180 181 if (tokens[i] == "?"): 182 if (i + 1 < len(tokens)): 183 i += 1 184 parts = tokens[i].split(".") 185 186 if (len(parts) == 1): 187 result.append("%s.%s" % (iface, tokens[i])) 188 else: 189 result.append(tokens[i]) 190 else: 191 print "%s: Unexpected end of protocol" % name 192 193 i += 1 194 195 return result 196 197 def merge_bp(protocols): 198 "Merge several Behavior Protocols" 199 200 if (len(protocols) > 1): 201 result = [] 202 first = True 203 204 for protocol in protocols: 205 if (first): 206 first = False 207 else: 208 result.append("|") 209 210 result.append("(") 211 result.extend(protocol) 212 result.append(")") 213 214 return result 215 216 if (len(protocols) == 1): 217 return protocols[0] 218 219 return [] 220 221 def parse_bp(name, tokens, base_indent): 168 222 "Parse Behavior Protocol" 169 223 170 tokens = preproc_bp(name, split_tokens(protocol, ["\n", " ", "\t", "(", ")", "{", "}", "*", ";", "+", "||", "|", "!", "?"], True, True))224 tokens = preproc_bp(name, tokens) 171 225 172 226 indent = base_indent … … 213 267 return output 214 268 215 def dump_iface_bp(interface, protocol, outdir): 216 "Dump Behavior Protocol of a given interface" 217 218 outname = os.path.join(outdir, "iface_%s.bp" % interface) 269 def get_iface(name): 270 "Get interface by name" 271 272 global iface_properties 273 274 if (name in iface_properties): 275 return iface_properties[name] 276 277 return None 278 279 def dump_frame(frame, outdir): 280 "Dump Behavior Protocol of a given frame" 281 282 outname = os.path.join(outdir, "%s.bp" % frame['name']) 219 283 if (os.path.isfile(outname)): 220 284 print "%s: File already exists, overwriting" % outname 221 285 286 protocols = [] 287 if ('protocol' in frame): 288 protocols.append(frame['protocol']) 289 290 if ('provides' in frame): 291 for provides in frame['provides']: 292 iface = get_iface(provides['iface']) 293 if (not iface is None): 294 if ('protocol' in iface): 295 protocols.append(extend_bp(outname, iface['protocol'], iface['name'])) 296 else: 297 print "%s: Provided interface '%s' is undefined" % (frame['name'], provides['iface']) 298 222 299 outf = file(outname, "w") 223 outf.write(parse_bp(outname, protocol, 0))300 outf.write(parse_bp(outname, merge_bp(protocols), 0)) 224 301 outf.close() 225 302 226 def dump_frame_bp(frame, protocol, outdir): 227 "Dump Behavior Protocol of a given frame" 228 229 outname = os.path.join(outdir, "frame_%s.bp" % frame) 303 def get_system_arch(): 304 "Get system architecture" 305 306 global arch_properties 307 308 for arch, properties in arch_properties.items(): 309 if ('system' in properties): 310 return properties 311 312 return None 313 314 def get_arch(name): 315 "Get architecture by name" 316 317 global arch_properties 318 319 if (name in arch_properties): 320 return arch_properties[name] 321 322 return None 323 324 def get_frame(name): 325 "Get frame by name" 326 327 global frame_properties 328 329 if (name in frame_properties): 330 return frame_properties[name] 331 332 return None 333 334 def create_null_bp(name, outdir): 335 "Create null frame protocol" 336 337 outname = os.path.join(outdir, name) 338 if (not os.path.isfile(outname)): 339 outf = file(outname, "w") 340 outf.write("NULL") 341 outf.close() 342 343 def dump_arch(arch, outdir): 344 "Dump architecture Behavior Protocol" 345 346 outname = os.path.join(outdir, "%s.archbp" % arch['name']) 230 347 if (os.path.isfile(outname)): 231 348 print "%s: File already exists, overwriting" % outname 232 349 233 350 outf = file(outname, "w") 234 outf.write(parse_bp(outname, protocol, 0)) 351 352 create_null_bp("null.bp", outdir) 353 outf.write("frame \"null.bp\"\n\n") 354 355 if ('inst' in arch): 356 for inst in arch['inst']: 357 subarch = get_arch(inst['type']) 358 if (not subarch is None): 359 outf.write("instantiate %s from \"%s.archbp\"\n" % (inst['var'], inst['type'])) 360 dump_arch(subarch, outdir) 361 else: 362 subframe = get_frame(inst['type']) 363 if (not subframe is None): 364 outf.write("instantiate %s from \"%s.bp\"\n" % (inst['var'], inst['type'])) 365 dump_frame(subframe, outdir) 366 else: 367 print "%s: '%s' is neither architecture nor frame" % (arch['name'], inst['type']) 368 235 369 outf.close() 236 370 … … 249 383 global frame 250 384 global protocol 251 global iface_protocols 252 global frame_protocols 385 386 global iface_properties 387 global frame_properties 388 global arch_properties 389 390 global arg0 253 391 254 392 if (nested): … … 350 488 351 489 if (indent == -1): 352 if (frame in frame_protocols): 490 bp = split_bp(protocol) 491 protocol = None 492 493 if (not frame in frame_properties): 494 frame_properties[frame] = {} 495 496 if ('protocol' in frame_properties[frame]): 353 497 print "%s: Protocol for frame '%s' already defined" % (inname, frame) 354 498 else: 355 frame_protocols[frame] = protocol 499 frame_properties[frame]['protocol'] = bp 500 356 501 output += "\n%s" % tabs(2) 357 output += parse_bp(inname, protocol, 2) 358 protocol = None 359 502 output += parse_bp(inname, bp, 2) 360 503 output += "\n%s" % token 361 504 indent = 0 … … 381 524 if (VAR in context): 382 525 if (not identifier(token)): 383 print "%s: Instance name expected in frame '%s'" % (inname, frame) 384 else: 526 print "%s: Variable name expected in frame '%s'" % (inname, frame) 527 else: 528 if (not frame in frame_properties): 529 frame_properties[frame] = {} 530 531 if (not 'requires' in frame_properties[frame]): 532 frame_properties[frame]['requires'] = [] 533 534 frame_properties[frame]['requires'].append({'iface': arg0, 'var': token}) 535 arg0 = None 536 385 537 output += "%s" % token 386 538 … … 395 547 print "%s: Interface name expected in frame '%s'" % (inname, frame) 396 548 else: 549 arg0 = token 397 550 output += "\n%s%s " % (tabs(indent), token) 398 551 … … 412 565 if (VAR in context): 413 566 if (not identifier(token)): 414 print "%s: Instance name expected in frame '%s'" % (inname, frame) 415 else: 567 print "%s: Variable name expected in frame '%s'" % (inname, frame) 568 else: 569 if (not frame in frame_properties): 570 frame_properties[frame] = {} 571 572 if (not 'provides' in frame_properties[frame]): 573 frame_properties[frame]['provides'] = [] 574 575 frame_properties[frame]['provides'].append({'iface': arg0, 'var': token}) 576 arg0 = None 577 416 578 output += "%s" % token 417 579 … … 426 588 print "%s: Interface name expected in frame '%s'" % (inname, frame) 427 589 else: 590 arg0 = token 428 591 output += "\n%s%s " % (tabs(indent), token) 429 592 … … 487 650 frame = token 488 651 output += "%s " % token 652 653 if (not frame in frame_properties): 654 frame_properties[frame] = {} 655 656 frame_properties[frame]['name'] = frame 489 657 490 658 context.add(HEAD) … … 513 681 514 682 if (indent == -1): 515 if (interface in iface_protocols): 683 bp = split_bp(protocol) 684 protocol = None 685 686 if (not interface in iface_properties): 687 iface_properties[interface] = {} 688 689 if ('protocol' in iface_properties[interface]): 516 690 print "%s: Protocol for interface '%s' already defined" % (inname, interface) 517 691 else: 518 iface_pro tocols[interface] = protocol692 iface_properties[interface]['protocol'] = bp 519 693 520 694 output += "\n%s" % tabs(2) 521 output += parse_bp(inname, protocol, 2) 522 protocol = None 523 695 output += parse_bp(inname, bp, 2) 524 696 output += "\n%s" % token 525 697 indent = 0 … … 634 806 interface = token 635 807 output += "%s " % token 808 809 if (not interface in iface_properties): 810 iface_properties[interface] = {} 811 812 iface_properties[interface]['name'] = interface 636 813 637 814 context.add(HEAD) … … 669 846 print "%s: Expected interface descriptor in architecture '%s'" % (inname, architecture) 670 847 else: 848 if (not architecture in arch_properties): 849 arch_properties[architecture] = {} 850 851 if (not 'delegate' in arch_properties[architecture]): 852 arch_properties[architecture]['delegate'] = [] 853 854 arch_properties[architecture]['delegate'].append({'from': arg0, 'to': token.split(":")}) 855 arg0 = None 856 671 857 output += "%s" % token 672 858 … … 689 875 else: 690 876 output += "%s " % token 877 arg0 = token 691 878 692 879 context.add(TO) … … 708 895 print "%s: Expected interface name in architecture '%s'" % (inname, architecture) 709 896 else: 897 if (not architecture in arch_properties): 898 arch_properties[architecture] = {} 899 900 if (not 'subsume' in arch_properties[architecture]): 901 arch_properties[architecture]['subsume'] = [] 902 903 arch_properties[architecture]['subsume'].append({'from': arg0.split(":"), 'to': token}) 904 arg0 = None 905 710 906 output += "%s" % token 711 907 … … 728 924 else: 729 925 output += "%s " % token 926 arg0 = token 730 927 731 928 context.add(TO) … … 747 944 print "%s: Expected second interface descriptor in architecture '%s'" % (inname, architecture) 748 945 else: 946 if (not architecture in arch_properties): 947 arch_properties[architecture] = {} 948 949 if (not 'bind' in arch_properties[architecture]): 950 arch_properties[architecture]['bind'] = [] 951 952 arch_properties[architecture]['bind'].append({'from': arg0.split(":"), 'to': token.split(":")}) 953 arg0 = None 954 749 955 output += "%s" % token 750 956 … … 767 973 else: 768 974 output += "%s " % token 975 arg0 = token 769 976 770 977 context.add(TO) … … 786 993 print "%s: Expected instance name in architecture '%s'" % (inname, architecture) 787 994 else: 995 if (not architecture in arch_properties): 996 arch_properties[architecture] = {} 997 998 if (not 'inst' in arch_properties[architecture]): 999 arch_properties[architecture]['inst'] = [] 1000 1001 arch_properties[architecture]['inst'].append({'type': arg0, 'var': token}) 1002 arg0 = None 1003 788 1004 output += "%s" % token 789 1005 … … 796 1012 else: 797 1013 output += "%s " % token 1014 arg0 = token 798 1015 799 1016 context.add(VAR) … … 861 1078 architecture = token 862 1079 output += "%s " % token 1080 1081 if (not architecture in arch_properties): 1082 arch_properties[architecture] = {} 1083 1084 arch_properties[architecture]['name'] = architecture 1085 1086 if (SYSTEM in context): 1087 arch_properties[architecture]['system'] = True 863 1088 864 1089 context.add(HEAD) … … 910 1135 global protocol 911 1136 1137 global arg0 1138 912 1139 output = "" 913 1140 context = set() … … 916 1143 frame = None 917 1144 protocol = None 1145 arg0 = None 918 1146 919 1147 parse_adl(base, root, inname, False, 0) … … 949 1177 950 1178 def main(): 951 global iface_protocols 952 global frame_protocols 1179 global iface_properties 1180 global frame_properties 1181 global arch_properties 953 1182 954 1183 if (len(sys.argv) < 2): … … 961 1190 return 962 1191 963 iface_protocols = {} 964 frame_protocols = {} 1192 iface_properties = {} 1193 frame_properties = {} 1194 arch_properties = {} 965 1195 966 1196 recursion(".", ".", path, 0) 967 1197 968 for key, value in iface_protocols.items(): 969 dump_iface_bp(key, value, path) 970 971 for key, value in frame_protocols.items(): 972 dump_frame_bp(key, value, path) 1198 system_arch = get_system_arch() 1199 1200 if (not system_arch is None): 1201 dump_arch(system_arch, path) 973 1202 974 1203 if __name__ == '__main__':
Note:
See TracChangeset
for help on using the changeset viewer.