Changeset fed03a3 in mainline for contrib/arch/hadlbppp.py


Ignore:
Timestamp:
2009-09-16T16:40:28Z (15 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
704311b
Parents:
ee5b35a
Message:

generate standalone BPs (.bp and .archbp)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • contrib/arch/hadlbppp.py

    ree5b35a rfed03a3  
    165165        return result
    166166
    167 def parse_bp(name, protocol, base_indent):
     167def split_bp(protocol):
     168        "Convert Behavior Protocol to tokens"
     169       
     170        return split_tokens(protocol, ["\n", " ", "\t", "(", ")", "{", "}", "*", ";", "+", "||", "|", "!", "?"], True, True)
     171
     172def 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
     197def 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
     221def parse_bp(name, tokens, base_indent):
    168222        "Parse Behavior Protocol"
    169223       
    170         tokens = preproc_bp(name, split_tokens(protocol, ["\n", " ", "\t", "(", ")", "{", "}", "*", ";", "+", "||", "|", "!", "?"], True, True))
     224        tokens = preproc_bp(name, tokens)
    171225       
    172226        indent = base_indent
     
    213267        return output
    214268
    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)
     269def 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
     279def dump_frame(frame, outdir):
     280        "Dump Behavior Protocol of a given frame"
     281       
     282        outname = os.path.join(outdir, "%s.bp" % frame['name'])
    219283        if (os.path.isfile(outname)):
    220284                print "%s: File already exists, overwriting" % outname
    221285       
     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       
    222299        outf = file(outname, "w")
    223         outf.write(parse_bp(outname, protocol, 0))
     300        outf.write(parse_bp(outname, merge_bp(protocols), 0))
    224301        outf.close()
    225302
    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)
     303def 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
     314def 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
     324def 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
     334def 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
     343def dump_arch(arch, outdir):
     344        "Dump architecture Behavior Protocol"
     345       
     346        outname = os.path.join(outdir, "%s.archbp" % arch['name'])
    230347        if (os.path.isfile(outname)):
    231348                print "%s: File already exists, overwriting" % outname
    232349       
    233350        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       
    235369        outf.close()
    236370
     
    249383        global frame
    250384        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
    253391       
    254392        if (nested):
     
    350488                                       
    351489                                        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]):
    353497                                                        print "%s: Protocol for frame '%s' already defined" % (inname, frame)
    354498                                                else:
    355                                                         frame_protocols[frame] = protocol
     499                                                        frame_properties[frame]['protocol'] = bp
     500                                               
    356501                                                output += "\n%s" % tabs(2)
    357                                                 output += parse_bp(inname, protocol, 2)
    358                                                 protocol = None
    359                                                
     502                                                output += parse_bp(inname, bp, 2)
    360503                                                output += "\n%s" % token
    361504                                                indent = 0
     
    381524                                        if (VAR in context):
    382525                                                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                                                       
    385537                                                        output += "%s" % token
    386538                                               
     
    395547                                                        print "%s: Interface name expected in frame '%s'" % (inname, frame)
    396548                                                else:
     549                                                        arg0 = token
    397550                                                        output += "\n%s%s " % (tabs(indent), token)
    398551                                               
     
    412565                                        if (VAR in context):
    413566                                                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                                                       
    416578                                                        output += "%s" % token
    417579                                               
     
    426588                                                        print "%s: Interface name expected in frame '%s'" % (inname, frame)
    427589                                                else:
     590                                                        arg0 = token
    428591                                                        output += "\n%s%s " % (tabs(indent), token)
    429592                                               
     
    487650                                frame = token
    488651                                output += "%s " % token
     652                               
     653                                if (not frame in frame_properties):
     654                                        frame_properties[frame] = {}
     655                               
     656                                frame_properties[frame]['name'] = frame
    489657                       
    490658                        context.add(HEAD)
     
    513681                                       
    514682                                        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]):
    516690                                                        print "%s: Protocol for interface '%s' already defined" % (inname, interface)
    517691                                                else:
    518                                                         iface_protocols[interface] = protocol
     692                                                        iface_properties[interface]['protocol'] = bp
    519693                                               
    520694                                                output += "\n%s" % tabs(2)
    521                                                 output += parse_bp(inname, protocol, 2)
    522                                                 protocol = None
    523                                                
     695                                                output += parse_bp(inname, bp, 2)
    524696                                                output += "\n%s" % token
    525697                                                indent = 0
     
    634806                                interface = token
    635807                                output += "%s " % token
     808                               
     809                                if (not interface in iface_properties):
     810                                        iface_properties[interface] = {}
     811                               
     812                                iface_properties[interface]['name'] = interface
    636813                       
    637814                        context.add(HEAD)
     
    669846                                                        print "%s: Expected interface descriptor in architecture '%s'" % (inname, architecture)
    670847                                                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                                                       
    671857                                                        output += "%s" % token
    672858                                               
     
    689875                                        else:
    690876                                                output += "%s " % token
     877                                                arg0 = token
    691878                                       
    692879                                        context.add(TO)
     
    708895                                                        print "%s: Expected interface name in architecture '%s'" % (inname, architecture)
    709896                                                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                                                       
    710906                                                        output += "%s" % token
    711907                                               
     
    728924                                        else:
    729925                                                output += "%s " % token
     926                                                arg0 = token
    730927                                       
    731928                                        context.add(TO)
     
    747944                                                        print "%s: Expected second interface descriptor in architecture '%s'" % (inname, architecture)
    748945                                                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                                                       
    749955                                                        output += "%s" % token
    750956                                               
     
    767973                                        else:
    768974                                                output += "%s " % token
     975                                                arg0 = token
    769976                                       
    770977                                        context.add(TO)
     
    786993                                                        print "%s: Expected instance name in architecture '%s'" % (inname, architecture)
    787994                                                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                                                       
    7881004                                                        output += "%s" % token
    7891005                                               
     
    7961012                                        else:
    7971013                                                output += "%s " % token
     1014                                                arg0 = token
    7981015                                       
    7991016                                        context.add(VAR)
     
    8611078                                architecture = token
    8621079                                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
    8631088                       
    8641089                        context.add(HEAD)
     
    9101135        global protocol
    9111136       
     1137        global arg0
     1138       
    9121139        output = ""
    9131140        context = set()
     
    9161143        frame = None
    9171144        protocol = None
     1145        arg0 = None
    9181146       
    9191147        parse_adl(base, root, inname, False, 0)
     
    9491177
    9501178def main():
    951         global iface_protocols
    952         global frame_protocols
     1179        global iface_properties
     1180        global frame_properties
     1181        global arch_properties
    9531182       
    9541183        if (len(sys.argv) < 2):
     
    9611190                return
    9621191       
    963         iface_protocols = {}
    964         frame_protocols = {}
     1192        iface_properties = {}
     1193        frame_properties = {}
     1194        arch_properties = {}
    9651195       
    9661196        recursion(".", ".", path, 0)
    9671197       
    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)
    9731202
    9741203if __name__ == '__main__':
Note: See TracChangeset for help on using the changeset viewer.