Index: contrib/arch/hadlbppp.py
===================================================================
--- contrib/arch/hadlbppp.py	(revision e5d4294414755bb3480a9ea9e93a7fc597b32d05)
+++ contrib/arch/hadlbppp.py	(revision 57688fe23fa1c5a39b4fd977d3ee140ad42edb69)
@@ -34,4 +34,9 @@
 import os
 
+# TODO:
+#  - alternative token
+#  - iface protocol repetition
+#  - interface inheritance
+
 INC, POST_INC, BLOCK_COMMENT, LINE_COMMENT, SYSTEM, ARCH, HEAD, BODY, NULL, \
 	INST, VAR, FIN, BIND, TO, SUBSUME, DELEGATE, IFACE, PROTOTYPE, PAR_LEFT, \
@@ -277,10 +282,11 @@
 	return None
 
-def dump_frame(frame, outdir):
+def dump_frame(frame, outdir, var, archf):
 	"Dump Behavior Protocol of a given frame"
 	
-	outname = os.path.join(outdir, "%s.bp" % frame['name'])
-	if (os.path.isfile(outname)):
-		print "%s: File already exists, overwriting" % outname
+	fname = "%s.bp" % frame['name']
+	
+	archf.write("instantiate %s from \"%s\"\n" % (var, fname))
+	outname = os.path.join(outdir, fname)
 	
 	protocols = []
@@ -332,24 +338,21 @@
 	return None
 
-def create_null_bp(name, outdir):
+def create_null_bp(fname, outdir, archf):
 	"Create null frame protocol"
 	
-	outname = os.path.join(outdir, name)
-	if (not os.path.isfile(outname)):
-		outf = file(outname, "w")
-		outf.write("NULL")
-		outf.close()
-
-def dump_arch(arch, outdir):
-	"Dump architecture Behavior Protocol"
-	
-	outname = os.path.join(outdir, "%s.archbp" % arch['name'])
-	if (os.path.isfile(outname)):
-		print "%s: File already exists, overwriting" % outname
+	archf.write("frame \"%s\"\n" % fname)
+	outname = os.path.join(outdir, fname)
 	
 	outf = file(outname, "w")
-	
-	create_null_bp("null.bp", outdir)
-	outf.write("frame \"null.bp\"\n\n")
+	outf.write("NULL")
+	outf.close()
+
+def merge_subarch(prefix, arch, outdir):
+	"Merge subarchitecture into architexture"
+	
+	insts = []
+	binds = []
+	delegates = []
+	subsumes = []
 	
 	if ('inst' in arch):
@@ -357,19 +360,94 @@
 			subarch = get_arch(inst['type'])
 			if (not subarch is None):
-				outf.write("instantiate %s from \"%s.archbp\"\n" % (inst['var'], inst['type']))
-				dump_arch(subarch, outdir)
+				(subinsts, subbinds, subdelegates, subsubsumes) = merge_subarch("%s_%s" % (prefix, subarch['name']), subarch, outdir)
+				insts.extend(subinsts)
+				binds.extend(subbinds)
+				delegates.extend(subdelegates)
+				subsumes.extend(subsubsumes)
 			else:
 				subframe = get_frame(inst['type'])
 				if (not subframe is None):
-					outf.write("instantiate %s from \"%s.bp\"\n" % (inst['var'], inst['type']))
-					dump_frame(subframe, outdir)
+					insts.append({'var': "%s_%s" % (prefix, inst['var']), 'frame': subframe})
 				else:
-					print "%s: '%s' is neither architecture nor frame" % (arch['name'], inst['type'])
-		
-		outf.write("\n")
+					print "%s: '%s' is neither an architecture nor a frame" % (arch['name'], inst['type'])
 	
 	if ('bind' in arch):
 		for bind in arch['bind']:
-			outf.write("bind %s.%s to %s.%s\n" % (bind['from'][0], bind['from'][1], bind['to'][0], bind['to'][1]))
+			binds.append({'from': "%s_%s.%s" % (prefix, bind['from'][0], bind['from'][1]), 'to': "%s_%s.%s" % (prefix, bind['to'][0], bind['to'][1])})
+	
+	if ('delegate' in arch):
+		for delegate in arch['delegate']:
+			delegates.append({'to': "%s.%s" % (prefix, delegate['from']), 'rep': "%s_%s.%s" % (prefix, delegate['to'][0], delegate['to'][1])})
+	
+	if ('subsume' in arch):
+		for subsume in arch['subsume']:
+			subsumes.append({'from': "%s.%s" % (prefix, subsume['to']), 'rep': "%s_%s.%s" % (prefix, subsume['from'][0], subsume['from'][1])})
+	
+	return (insts, binds, delegates, subsumes)
+
+def dump_archbp(outdir):
+	"Dump system architecture Behavior Protocol"
+	
+	arch = get_system_arch()
+	
+	if (arch is None):
+		print "Unable to find system architecture"
+		return
+	
+	insts = []
+	binds = []
+	delegates = []
+	subsumes = []
+	
+	if ('inst' in arch):
+		for inst in arch['inst']:
+			subarch = get_arch(inst['type'])
+			if (not subarch is None):
+				(subinsts, subbinds, subdelegates, subsubsumes) = merge_subarch(subarch['name'], subarch, outdir)
+				insts.extend(subinsts)
+				binds.extend(subbinds)
+				delegates.extend(subdelegates)
+				subsumes.extend(subsubsumes)
+			else:
+				subframe = get_frame(inst['type'])
+				if (not subframe is None):
+					insts.append({'var': inst['var'], 'frame': subframe})
+				else:
+					print "%s: '%s' is neither an architecture nor a frame" % (arch['name'], inst['type'])
+	
+	if ('bind' in arch):
+		for bind in arch['bind']:
+			binds.append({'from': "%s.%s" % (bind['from'][0], bind['from'][1]), 'to': "%s.%s" % (bind['to'][0], bind['to'][1])})
+	
+	if ('delegate' in arch):
+		for delegate in arch['delegate']:
+			print "Unable to delegate interface in system architecture"
+			break
+	
+	if ('subsume' in arch):
+		for subsume in arch['subsume']:
+			print "Unable to subsume interface in system architecture"
+			break
+	
+	outname = os.path.join(outdir, "%s.archbp" % arch['name'])
+	outf = file(outname, "w")
+	
+	create_null_bp("null.bp", outdir, outf)
+	
+	for inst in insts:
+		dump_frame(inst['frame'], outdir, inst['var'], outf)
+	
+	for bind in binds:
+		for delegate in delegates:
+			if (bind['to'] == delegate['to']):
+				bind['to'] = delegate['rep']
+				break
+		
+		for subsume in subsumes:
+			if (bind['from'] == subsume['from']):
+				bind['from'] = subsume['rep']
+				break
+		
+		outf.write("bind %s to %s\n" % (bind['from'], bind['to']))
 	
 	outf.close()
@@ -797,5 +875,5 @@
 					output += "%s\n" % token
 					context.remove(HEAD)
-					context.remove(ARCH)
+					context.remove(IFACE)
 					continue
 				
@@ -1155,13 +1233,7 @@
 	
 	if (output != ""):
-		if (os.path.isfile(outname)):
-			print "%s: File already exists, overwriting" % outname
-		
 		outf = file(outname, "w")
 		outf.write(output)
 		outf.close()
-	else:
-		if (os.path.isfile(outname)):
-			print "%s: File already exists, but should be empty" % outname
 
 def recursion(base, root, output, level):
@@ -1201,9 +1273,5 @@
 	
 	recursion(".", ".", path, 0)
-	
-	system_arch = get_system_arch()
-	
-	if (not system_arch is None):
-		dump_arch(system_arch, path)
+	dump_archbp(path)
 
 if __name__ == '__main__':
