Changeset 57688fe2 in mainline


Ignore:
Timestamp:
2009-09-16T22:45:48Z (15 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
cf7b3e0
Parents:
e5d4294
Message:

flatten final BP hierarchy (by resolving subsumes and delegations), generate only a single .archbp file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • contrib/arch/hadlbppp.py

    re5d4294 r57688fe2  
    3434import os
    3535
     36# TODO:
     37#  - alternative token
     38#  - iface protocol repetition
     39#  - interface inheritance
     40
    3641INC, POST_INC, BLOCK_COMMENT, LINE_COMMENT, SYSTEM, ARCH, HEAD, BODY, NULL, \
    3742        INST, VAR, FIN, BIND, TO, SUBSUME, DELEGATE, IFACE, PROTOTYPE, PAR_LEFT, \
     
    277282        return None
    278283
    279 def dump_frame(frame, outdir):
     284def dump_frame(frame, outdir, var, archf):
    280285        "Dump Behavior Protocol of a given frame"
    281286       
    282         outname = os.path.join(outdir, "%s.bp" % frame['name'])
    283         if (os.path.isfile(outname)):
    284                 print "%s: File already exists, overwriting" % outname
     287        fname = "%s.bp" % frame['name']
     288       
     289        archf.write("instantiate %s from \"%s\"\n" % (var, fname))
     290        outname = os.path.join(outdir, fname)
    285291       
    286292        protocols = []
     
    332338        return None
    333339
    334 def create_null_bp(name, outdir):
     340def create_null_bp(fname, outdir, archf):
    335341        "Create null frame protocol"
    336342       
    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'])
    347         if (os.path.isfile(outname)):
    348                 print "%s: File already exists, overwriting" % outname
     343        archf.write("frame \"%s\"\n" % fname)
     344        outname = os.path.join(outdir, fname)
    349345       
    350346        outf = file(outname, "w")
    351        
    352         create_null_bp("null.bp", outdir)
    353         outf.write("frame \"null.bp\"\n\n")
     347        outf.write("NULL")
     348        outf.close()
     349
     350def merge_subarch(prefix, arch, outdir):
     351        "Merge subarchitecture into architexture"
     352       
     353        insts = []
     354        binds = []
     355        delegates = []
     356        subsumes = []
    354357       
    355358        if ('inst' in arch):
     
    357360                        subarch = get_arch(inst['type'])
    358361                        if (not subarch is None):
    359                                 outf.write("instantiate %s from \"%s.archbp\"\n" % (inst['var'], inst['type']))
    360                                 dump_arch(subarch, outdir)
     362                                (subinsts, subbinds, subdelegates, subsubsumes) = merge_subarch("%s_%s" % (prefix, subarch['name']), subarch, outdir)
     363                                insts.extend(subinsts)
     364                                binds.extend(subbinds)
     365                                delegates.extend(subdelegates)
     366                                subsumes.extend(subsubsumes)
    361367                        else:
    362368                                subframe = get_frame(inst['type'])
    363369                                if (not subframe is None):
    364                                         outf.write("instantiate %s from \"%s.bp\"\n" % (inst['var'], inst['type']))
    365                                         dump_frame(subframe, outdir)
     370                                        insts.append({'var': "%s_%s" % (prefix, inst['var']), 'frame': subframe})
    366371                                else:
    367                                         print "%s: '%s' is neither architecture nor frame" % (arch['name'], inst['type'])
    368                
    369                 outf.write("\n")
     372                                        print "%s: '%s' is neither an architecture nor a frame" % (arch['name'], inst['type'])
    370373       
    371374        if ('bind' in arch):
    372375                for bind in arch['bind']:
    373                         outf.write("bind %s.%s to %s.%s\n" % (bind['from'][0], bind['from'][1], bind['to'][0], bind['to'][1]))
     376                        binds.append({'from': "%s_%s.%s" % (prefix, bind['from'][0], bind['from'][1]), 'to': "%s_%s.%s" % (prefix, bind['to'][0], bind['to'][1])})
     377       
     378        if ('delegate' in arch):
     379                for delegate in arch['delegate']:
     380                        delegates.append({'to': "%s.%s" % (prefix, delegate['from']), 'rep': "%s_%s.%s" % (prefix, delegate['to'][0], delegate['to'][1])})
     381       
     382        if ('subsume' in arch):
     383                for subsume in arch['subsume']:
     384                        subsumes.append({'from': "%s.%s" % (prefix, subsume['to']), 'rep': "%s_%s.%s" % (prefix, subsume['from'][0], subsume['from'][1])})
     385       
     386        return (insts, binds, delegates, subsumes)
     387
     388def dump_archbp(outdir):
     389        "Dump system architecture Behavior Protocol"
     390       
     391        arch = get_system_arch()
     392       
     393        if (arch is None):
     394                print "Unable to find system architecture"
     395                return
     396       
     397        insts = []
     398        binds = []
     399        delegates = []
     400        subsumes = []
     401       
     402        if ('inst' in arch):
     403                for inst in arch['inst']:
     404                        subarch = get_arch(inst['type'])
     405                        if (not subarch is None):
     406                                (subinsts, subbinds, subdelegates, subsubsumes) = merge_subarch(subarch['name'], subarch, outdir)
     407                                insts.extend(subinsts)
     408                                binds.extend(subbinds)
     409                                delegates.extend(subdelegates)
     410                                subsumes.extend(subsubsumes)
     411                        else:
     412                                subframe = get_frame(inst['type'])
     413                                if (not subframe is None):
     414                                        insts.append({'var': inst['var'], 'frame': subframe})
     415                                else:
     416                                        print "%s: '%s' is neither an architecture nor a frame" % (arch['name'], inst['type'])
     417       
     418        if ('bind' in arch):
     419                for bind in arch['bind']:
     420                        binds.append({'from': "%s.%s" % (bind['from'][0], bind['from'][1]), 'to': "%s.%s" % (bind['to'][0], bind['to'][1])})
     421       
     422        if ('delegate' in arch):
     423                for delegate in arch['delegate']:
     424                        print "Unable to delegate interface in system architecture"
     425                        break
     426       
     427        if ('subsume' in arch):
     428                for subsume in arch['subsume']:
     429                        print "Unable to subsume interface in system architecture"
     430                        break
     431       
     432        outname = os.path.join(outdir, "%s.archbp" % arch['name'])
     433        outf = file(outname, "w")
     434       
     435        create_null_bp("null.bp", outdir, outf)
     436       
     437        for inst in insts:
     438                dump_frame(inst['frame'], outdir, inst['var'], outf)
     439       
     440        for bind in binds:
     441                for delegate in delegates:
     442                        if (bind['to'] == delegate['to']):
     443                                bind['to'] = delegate['rep']
     444                                break
     445               
     446                for subsume in subsumes:
     447                        if (bind['from'] == subsume['from']):
     448                                bind['from'] = subsume['rep']
     449                                break
     450               
     451                outf.write("bind %s to %s\n" % (bind['from'], bind['to']))
    374452       
    375453        outf.close()
     
    797875                                        output += "%s\n" % token
    798876                                        context.remove(HEAD)
    799                                         context.remove(ARCH)
     877                                        context.remove(IFACE)
    800878                                        continue
    801879                               
     
    11551233       
    11561234        if (output != ""):
    1157                 if (os.path.isfile(outname)):
    1158                         print "%s: File already exists, overwriting" % outname
    1159                
    11601235                outf = file(outname, "w")
    11611236                outf.write(output)
    11621237                outf.close()
    1163         else:
    1164                 if (os.path.isfile(outname)):
    1165                         print "%s: File already exists, but should be empty" % outname
    11661238
    11671239def recursion(base, root, output, level):
     
    12011273       
    12021274        recursion(".", ".", path, 0)
    1203        
    1204         system_arch = get_system_arch()
    1205        
    1206         if (not system_arch is None):
    1207                 dump_arch(system_arch, path)
     1275        dump_archbp(path)
    12081276
    12091277if __name__ == '__main__':
Note: See TracChangeset for help on using the changeset viewer.