Changeset 6d4c549 in mainline for contrib/arch/hadlbppp.py


Ignore:
Timestamp:
2009-09-25T15:01:03Z (15 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
bbf88db
Parents:
2e37308
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • contrib/arch/hadlbppp.py

    r2e37308 r6d4c549  
    3636INC, POST_INC, BLOCK_COMMENT, LINE_COMMENT, SYSTEM, ARCH, HEAD, BODY, NULL, \
    3737        INST, VAR, FIN, BIND, TO, SUBSUME, DELEGATE, IFACE, EXTENDS, PRE_BODY, \
    38         PROTOTYPE, PAR_LEFT, PAR_RIGHT, SIGNATURE, PROTOCOL, FRAME, PROVIDES, \
    39         REQUIRES = range(27)
     38        PROTOTYPE, PAR_LEFT, PAR_RIGHT, SIGNATURE, PROTOCOL, INITIALIZATION, \
     39        FINALIZATION, FRAME, PROVIDES, REQUIRES = range(29)
    4040
    4141def usage(prname):
    4242        "Print usage syntax"
    4343       
    44         print "%s <OUTPUT>" % prname
     44        print "%s <--dumpbp|--dumpadl|--noop>+ <OUTPUT>" % prname
    4545
    4646def tabs(cnt):
     
    246246        "Convert interface Behavior Protocol to generic protocol"
    247247       
    248         result = ["("]
     248        result = []
    249249        i = 0
    250250       
     
    266266                i += 1
    267267       
    268         result.append(")")
    269         result.append("*")
    270        
    271268        return result
    272269
    273 def merge_bp(protocols):
     270def merge_bp(initialization, finalization, protocols):
    274271        "Merge several Behavior Protocols"
    275272       
     273        indep = []
     274       
    276275        if (len(protocols) > 1):
    277                 result = []
    278276                first = True
    279277               
     
    282280                                first = False
    283281                        else:
    284                                 result.append("|")
    285                        
    286                         result.append("(")
    287                         result.extend(protocol)
    288                         result.append(")")
    289                
    290                 return result
    291        
    292         if (len(protocols) == 1):
    293                 return protocols[0]
    294        
    295         return []
     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
    296323
    297324def parse_bp(name, tokens, base_indent):
     
    373400        "Dump Behavior Protocol of a given frame"
    374401       
     402        global opt_bp
     403       
    375404        fname = "%s.bp" % frame['name']
    376405       
    377         archf.write("instantiate %s from \"%s\"\n" % (var, fname))
     406        if (archf != None):
     407                archf.write("instantiate %s from \"%s\"\n" % (var, fname))
     408       
    378409        outname = os.path.join(outdir, fname)
    379410       
     
    381412        if ('protocol' in frame):
    382413                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
    383424       
    384425        if ('provides' in frame):
     
    393434                                print "%s: Provided interface '%s' is undefined" % (frame['name'], provides['iface'])
    394435       
    395         outf = file(outname, "w")
    396         outf.write(parse_bp(outname, merge_bp(protocols), 0))
    397         outf.close()
     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()
    398441
    399442def get_system_arch():
     
    431474        "Create null frame protocol"
    432475       
    433         archf.write("frame \"%s\"\n" % fname)
     476        global opt_bp
     477       
     478        if (archf != None):
     479                archf.write("frame \"%s\"\n" % fname)
     480       
    434481        outname = os.path.join(outdir, fname)
    435482       
    436         outf = file(outname, "w")
    437         outf.write("NULL")
    438         outf.close()
     483        if (opt_bp):
     484                outf = file(outname, "w")
     485                outf.write("NULL")
     486                outf.close()
    439487
    440488def flatten_binds(binds, delegates, subsumes):
     
    521569        "Dump system architecture Behavior Protocol"
    522570       
     571        global opt_bp
     572       
    523573        arch = get_system_arch()
    524574       
     
    562612                        break
    563613       
     614       
    564615        outname = os.path.join(outdir, "%s.archbp" % arch['name'])
    565         outf = file(outname, "w")
     616        if (opt_bp):
     617                outf = file(outname, "w")
     618        else:
     619                outf = None
    566620       
    567621        create_null_bp("null.bp", outdir, outf)
     
    573627       
    574628        for dst, src in directed_binds.items():
    575                 outf.write("bind %s to %s\n" % (", ".join(src), dst))
    576        
    577         outf.close()
     629                if (outf != None):
     630                        outf.write("bind %s to %s\n" % (", ".join(src), dst))
     631       
     632        if (outf != None):
     633                outf.close()
    578634
    579635def preproc_adl(raw, inarg):
     
    591647        global frame
    592648        global protocol
     649        global initialization
     650        global finalization
    593651       
    594652        global iface_properties
     
    689747                       
    690748                        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                               
    691817                                if (PROTOCOL in context):
    692818                                        if (token == "{"):
     
    695821                                                indent -= 1
    696822                                       
    697                                         if (indent == -1):
     823                                        if (((token[-1] == ":") and (indent == 0)) or (indent == -1)):
    698824                                                bp = split_bp(protocol)
    699825                                                protocol = None
     
    709835                                                output += "\n%s" % tabs(2)
    710836                                                output += parse_bp(inname, bp, 2)
    711                                                 output += "\n%s" % token
    712                                                 indent = 0
    713837                                               
    714838                                                context.remove(PROTOCOL)
    715                                                 context.remove(BODY)
    716                                                 context.add(NULL)
     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
    717847                                        else:
    718848                                                protocol += token
    719                                        
    720                                         continue
     849                                                continue
    721850                               
    722851                                if (REQUIRES in context):
     
    816945                                        output += "\n%s%s" % (tabs(indent - 1), token)
    817946                                        context.add(PROVIDES)
    818                                         protocol = ""
    819947                                        continue
    820948                               
     
    822950                                        output += "\n%s%s" % (tabs(indent - 1), token)
    823951                                        context.add(REQUIRES)
    824                                         protocol = ""
     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 = ""
    825966                                        continue
    826967                               
     
    10121153                                                print "%s: Expected inherited interface name in interface head '%s'" % (inname, interface)
    10131154                                        else:
    1014                                                 output += " %s" % token
     1155                                                output += "%s " % token
    10151156                                                if (not interface in iface_properties):
    10161157                                                        iface_properties[interface] = {}
     
    10231164                               
    10241165                                if (token == "extends"):
    1025                                         output += " %s" % token
     1166                                        output += "%s " % token
    10261167                                        context.add(EXTENDS)
    10271168                                        continue
     
    13761517        global frame
    13771518        global protocol
     1519        global initialization
     1520        global finalization
    13781521       
    13791522        global arg0
     1523       
     1524        global opt_adl
    13801525       
    13811526        output = ""
     
    13851530        frame = None
    13861531        protocol = None
     1532        initialization = None
     1533        finalization = None
    13871534        arg0 = None
    13881535       
     
    13901537        output = output.strip()
    13911538       
    1392         if (output != ""):
     1539        if ((output != "") and (opt_adl)):
    13931540                outf = file(outname, "w")
    13941541                outf.write(output)
     
    14161563        global frame_properties
    14171564        global arch_properties
    1418        
    1419         if (len(sys.argv) < 2):
     1565        global opt_bp
     1566        global opt_adl
     1567       
     1568        if (len(sys.argv) < 3):
    14201569                usage(sys.argv[0])
    14211570                return
    14221571       
    1423         path = os.path.abspath(sys.argv[1])
     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])
    14241587        if (not os.path.isdir(path)):
    14251588                print "Error: <OUTPUT> is not a directory"
Note: See TracChangeset for help on using the changeset viewer.