LichenSplitMerge.Mesa
Last tweaked by Mike Spreitzer on April 30, 1987 1:46:04 pm PDT
DIRECTORY Asserting, LichenArrayStuff, LichenDataOps, LichenDataStructure, LichenSetTheory, LichenTransforms, LichenTransformsPrivate, RefTab, Rope;
LichenSplitMerge: CEDAR PROGRAM
IMPORTS Asserting, LichenArrayStuff, LichenDataOps, LichenDataStructure, LichenSetTheory, LichenTransformsPrivate, RefTab, Rope
EXPORTS LichenTransforms =
BEGIN OPEN LichenArrayStuff, LichenDataStructure, LichenTransforms, LichenDataOps, LichenSetTheory, LichenTransformsPrivate;
PortAns: TYPE ~ REF PortAnsPrivate;
PortAnsPrivate: TYPE ~ RECORD [
from, to: Port,
doFrom: PortAction ← leave,
doTo: PortAction[dontAdd..addPort] ← dontAdd
];
SplitType: PUBLIC PROC [design: Design, fizz: Set--of CellInstance--] RETURNS [from, to: CellType, pairs: OneToOne--instances of from é instances of to--] ~ {
analysis: Analysis ~ NEW [AnalysisPrivate ← [
roles: fizz.Size[],
subjTypes: CreateRefSeq[fizz.Size[]],
wag: NEW [WireAnsweringPrivate ← [
oldSubjConnectionss: CreateRefSeq[fizz.Size[]],
anses: CreateHashMapper[]
]],
doomedPorts: CreateHashSet[]
]];
{OPEN analysis;
cs: RefSeq--role b fizzee-- ~ CreateRefSeq[roles];
connections: BiRel--port of from port of to-- ~ CreateHashBiRel[];
nameParts: LOLORANIL;
toName, subTail: ROPE;
IF roles=0 THEN Error["Null fission"];
from ← NIL;
{i: INT ← 0;
PerElt: PROC [ra: REF ANY] ~ {v: CellInstance ~ NARROW[ra];
IF v.type=NIL OR v.containingCT=NIL THEN ERROR;
IF from=NIL THEN from ← v.containingCT ELSE IF from#v.containingCT THEN Error["Fission of vertices not all having same parent"];
nameParts ← CONS[Select[nameReln, 1, v.other], nameParts];
cs[i] ← v;
subjTypes[i] ← v.type;
i ← i + 1;
};
fizz.Enumerate[PerElt];
IF i # cs.length THEN ERROR;
};
IF NOT from.designs.HasMember[design] THEN ERROR;
subTail ← RopeCrossCat[nameParts];
toName ← RopeCrossCat[LIST[Select[nameReln, 1, from.otherPublic], LIST[subTail]]];
subTail ← Rope.Concat["-", subTail];
to ← CreateCellType[design: design, cellTypeName: toName, class: NIL, internals: TRUE];
pairs ← CreateHashOTO[];
IF Survey[from, NIL, cs, analysis, TRUE] THEN ERROR;
{MoveWire: PROC [domain, range: REF ANY] ~ {
wire: Wire ~ NARROW[domain];
wa: WireAns ~ NARROW[range];
IF NOT wa.analyzed THEN ERROR;
IF wa.counterpart # NIL THEN ERROR;
wa.counterpart ← CreateWire[containingCT: to, copy: wire, other: Asserting.Filter[nameReln, wire.other].about];
SELECT wa.doFrom FROM
addPort => {
wa.fromPort ← AddPort[[parent: from.port, wire: wire]];
AddEdge[[from.asUnorganized.mirror, wire], wa.fromPort]};
leave, dontAdd, dePort => NULL;
ENDCASE => ERROR;
SELECT wa.doTo FROM
addPort => {
wa.toPort ← AddPort[[parent: to.port, wire: wa.counterpart]];
IF wa.fromPort=NIL THEN ERROR;
IF NOT connections.AddPair[[wa.fromPort, wa.toPort]] THEN ERROR;
AddEdge[[to.asUnorganized.mirror, wa.counterpart], wa.toPort]};
dontAdd => NULL;
ENDCASE => ERROR;
};
wag.anses.EnumerateMap[MoveWire];
};
FOR role: NATURAL IN [0 .. roles) DO
fci: CellInstance ~ NARROW[cs[role]];
tci: CellInstance ~ Instantiate[fci.type, to, fci.other];
MoveConnection: PROC [subjPort: Port, fwire: Wire] ~ {
wa: WireAns = GetRPAns[wag, role, subjPort, fwire, FALSE];
IF NOT wa.analyzed THEN ERROR;
AddEdges[[tci, wa.counterpart], subjPort];
};
EnumerateTopConnections[fci, MoveConnection];
ENDLOOP;
FixArrays[design, from, to, connections, doomedPorts, portToInternalWire, subTail, pairs];
FixInstances[from, to, connections, doomedPorts, portToInternalWire, pairs];
FOR i: NATURAL IN [0 .. cs.length) DO
DeleteVertex[NARROW[cs[i]]];
ENDLOOP;
{KillWireAndPort: PROC [domain, range: REF ANY] ~ {
wire: Wire ~ NARROW[domain];
wa: WireAns ~ NARROW[range];
IF NOT wa.analyzed THEN ERROR;
IF wa.counterpart=NIL THEN ERROR;
SELECT wa.doFrom FROM
addPort => NULL;
leave, dontAdd => NULL;
dePort => RemovePort[wa.fromPort];
ENDCASE => ERROR;
IF NOT (wa.sawElse OR wa.sawBords) THEN DeleteVertex[wire];
domain ← domain;
};
wag.anses.EnumerateMap[KillWireAndPort];
};
to ← to;
}};
FixArrays: PROC
[
design: Design,
fromAncestorCT, toAncestorCT: CellType,
eltConnections: BiRel--port of fromAncestorCT port of toAncestorCT--,
doomedFromPorts: Set--of port of fromAncestorCT--,
eltTPToWire: Mapper--port of toAncestorCT b prototypical wire--,
subTail: ROPE--to append to name of from array--,
pairs: OneToOne--instance of ancestor of from é instance of ancestor of to--
] ~ {
SplitArray: PROC [fromArrayCT: CellType] ~ {
fa: Array ~ fromArrayCT.asArray;
faName: ROPE ~ NARROW[Asserting.FnVal[nameReln, fromArrayCT.otherPublic]];
taName: ROPE ~ faName.Cat[subTail];
toArrayCT: CellType ~ CreateArray[design, taName, NIL, toAncestorCT, fa.size, fa.jointsPeriod, GetBorders[fa], NIL, NIL];
ta: Array ~ toArrayCT.asArray;
toNewGroup: Mapper--group of fa b group of ta-- ~ CreateHashMapper[];
toNewTie: Mapper--tie of ta b tie of fa-- ~ CreateHashMapper[];
arrayConnections: BiRel--port of fromArrayCT port of toArrayCT-- ~ CreateHashBiRel[];
arrayTPToWire: Mapper--port of ta b wire-- ~ CreateHashMapper[];
doomedArrayPorts: Set--of port of fa-- ~ CreateHashSet[];
AddConnection: PROC [fai: ArrayIndex, fg: Group, tai: ArrayIndex, tg: Group] ~ {
IF fg=NIL OR tg=NIL THEN ERROR;
{fp: Port ~ GetArrayPortForGroup[fromArrayCT, fa, fai, fg, TRUE];
tp: Port ~ GetArrayPortForGroup[toArrayCT, ta, tai, tg, TRUE];
w: Wire ~ NARROW[eltTPToWire.Map[tg.ports.first]];
oldFps: Set--of port of fromArrayCT-- ~ arrayConnections.BiRelMap[tp, rightToLeft];
IF fp=NIL OR tp=NIL THEN ERROR;
IF w=NIL THEN ERROR;
SELECT TRUE FROM
oldFps.Size[]=0 => IF NOT arrayConnections.AddPair[[fp, tp]] THEN ERROR;
TheElt[oldFps]=fp => NULL;
ENDCASE => ERROR;
[] ← arrayTPToWire.PutMapping[tp, w];
}};
IF IsIncompleteArray[fromArrayCT] THEN ERROR;
IF ta.groupingParmses # fa.groupingParmses THEN ERROR;
{UnroleOldPort: PROC [ra: REF ANY] ~ {UnrolePort[fa, NARROW[ra]]};
doomedFromPorts.Enumerate[UnroleOldPort];
};
FOR gi: NATURAL IN [0 .. fa.groupingses.length) DO
fgs: Groupings ~ NARROW[fa.groupingses[gi]];
tgs: Groupings ~ NARROW[ta.groupingses[gi]];
MoveGroup: PROC [ra: REF ANY] ~ {
fg: Group ~ NARROW[ra];
tg: Group ~ MakeGroup[ta, fg.gi2, tgs];
IF NOT toNewGroup.PutMapping[fg, tg] THEN ERROR;
FOR pl: PortList ← fg.ports, pl.rest WHILE pl # NIL DO
fp: Port ~ NARROW[pl.first];
EnGroup: PROC [ra: REF ANY] ~ {
tp: Port ~ NARROW[ra];
AddPortToGroup[ta, gi, tp, tg, FALSE];
};
eltConnections.EnumerateMapping[fp, leftToRight, EnGroup];
IF doomedFromPorts.HasMember[fp] THEN RemovePortFromGroup[fa, gi, fp, fg];
ENDLOOP;
ra ← ra;
};
fgs.groups.Enumerate[MoveGroup];
ENDLOOP;
{MoveTie: PROC [d: Dim, phase: Nat2, jgi: NATURAL, jgi2: Nat2, j: Joint, tie: Tie] ~ {
fj: Joint ~ j;
ftie: Tie ~ tie;
IF ftie.groups[low]#NIL AND ftie.groups[high]#NIL THEN {
tj: Joint ~ GetArrayJoint[ta, d, phase];
ttie: Tie ~ NEW [TiePrivate ← [groups: [
low: NARROW[toNewGroup.Map[ftie.groups[low]]],
high: NARROW[toNewGroup.Map[ftie.groups[high]]]]]];
IF ttie.groups[low]=NIL OR ttie.groups[high]=NIL THEN ERROR;
AddTie[tj, jgi, ttie];
IF NOT toNewTie.PutMapping[ftie, ttie] THEN ERROR;
};
};
EnumerateTies[fa, MoveTie];
};
FlushArrayWires[fa, doomedArrayPorts];
{ConnectGroup: PROC [domain, range: REF ANY] ~ {
fg: Group ~ NARROW[domain];
tg: Group ~ NARROW[range];
IF tg.gi2#fg.gi2 THEN ERROR;
IF tg.ports#NIL AND (fg.ports#NIL OR fa.toWire.Fetch[fg].val#NIL) THEN {
air: Range2 ~ Gi2ToAir[fa, fg.gi2].air;
FOR f: INT IN [air[Foo].min .. air[Foo].maxPlusOne) DO FOR b: INT IN [air[Bar].min .. air[Bar].maxPlusOne) DO
ai: ArrayIndex ~ [f, b];
IF fg.ports#NIL OR GetArrayPortForGroup[fromArrayCT, fa, ai, fg, FALSE]#NIL THEN AddConnection[ai, fg, ai, tg];
ENDLOOP ENDLOOP;
};
};
toNewGroup.EnumerateMap[ConnectGroup];
};
{ConnectTie: PROC [d: Dim, phase: Nat2, jgi: NATURAL, jgi2: Nat2, j: Joint, tie: Tie] ~ {
ftie: Tie ~ tie;
ttie: Tie ~ NARROW[toNewTie.Map[ftie]];
IF ttie#NIL
AND ((ttie.groups[low].ports=NIL) # (ttie.groups[high].ports=NIL))
AND ((ttie.groups[low].ports=NIL) # (ftie.groups[low].ports=NIL))
AND ((ttie.groups[high].ports=NIL) # (ftie.groups[high].ports=NIL))
THEN {
o: Dim ~ OtherDim[d];
toHigh: BOOL ~ ttie.groups[low]=NIL;
FOR perp: NATURAL IN [0 .. fa.size[o]) DO FOR para: NATURAL IN [0 .. fa.size[d]-1) DO
lai: ArrayIndex ~ ConsInt2[d, para, perp];
hai: ArrayIndex ~ Int2Tweak[lai, d, 1];
IF toHigh THEN AddConnection[lai, ftie.groups[low], hai, ttie.groups[high]] ELSE AddConnection[lai, ftie.groups[high], hai, ttie.groups[low]];
ENDLOOP ENDLOOP;
};
};
EnumerateTies[fa, ConnectTie];
};
TrimEmptyGroups[fa];
TrimEmptyGroups[ta];
FixArrays[design, fromArrayCT, toArrayCT, arrayConnections, doomedArrayPorts, arrayTPToWire, subTail, pairs];
FixInstances[fromArrayCT, toArrayCT, arrayConnections, doomedArrayPorts, arrayTPToWire, pairs];
{PerDoomedArrayPort: PROC [ra: REF ANY] ~ {
p: Port--of fromArrayCT-- ~ NARROW[ra];
RemovePort[p];
};
doomedArrayPorts.Enumerate[PerDoomedArrayPort];
};
};
fromAncestorCT.EnumerateArrays[SplitArray];
};
FixInstances: PROC
[
fromAncestorCT, toAncestorCT: CellType,
connections: BiRel--port of fromAncestorCT port of toAncestorCT--,
doomedFromPorts: Set--of port of fromAncestorCT--,
tpToWire: Mapper--port of toAncestorCT b prototypical wire--,
pairs: OneToOne--instance of ancestor of from é instance of ancestor of to--
] ~ {
FixInstance: PROC [fci: CellInstance] ~ {
parentCT: CellType ~ fci.containingCT;
tci: CellInstance ~ Instantiate[toAncestorCT, parentCT];
ConnectChildren: PROC [parent: Port, do: BOOL] RETURNS [done: BOOL] ~ {
done ← FALSE;
FOR tp: Port ← FirstChildPort[parent], NextChildPort[tp] WHILE tp # NIL DO
IF connections.HasMapping[tp, rightToLeft] THEN done ← TRUE;
ENDLOOP;
IF done OR do THEN {
FOR tp: Port ← FirstChildPort[parent], NextChildPort[tp] WHILE tp # NIL DO
seen: BOOLFALSE;
PerFrom: PROC [ra: REF ANY] ~ {
fp: Port--of fa-- ~ NARROW[ra];
w: Wire ~ FindTransitiveConnection[fci, fp];
IF seen THEN ERROR ELSE seen ← TRUE;
IF w=NIL THEN ERROR;
AddEdges[[tci, w], tp];
};
connections.EnumerateMapping[tp, rightToLeft, PerFrom];
IF seen THEN NULL
ELSE IF NOT ConnectChildren[tp, FALSE] THEN {
w: Wire ~ CreateWire[containingCT: parentCT, copy: NARROW[tpToWire.Map[tp]]];
AddEdges[[tci, w], tp];
};
ENDLOOP;
};
};
[] ← ConnectChildren[toAncestorCT.port, TRUE];
UnlinkPorts[fci, doomedFromPorts];
IF pairs.PutOTOMapping[[fci, tci]]#[TRUE, TRUE] THEN ERROR;
};
EnumerateInstances[fromAncestorCT, FixInstance];
};
Select: PROC [reln: Asserting.Term, position: NATURAL, from: Assertions] RETURNS [terms: Asserting.Terms] ~ {
Filter: PROC [assertion: Asserting.Assertion] ~ {
these: Asserting.Terms ← Asserting.TermsOf[assertion];
THROUGH [1 .. position) DO these ← these.rest ENDLOOP;
terms ← CONS[these.first, terms];
};
Asserting.EnumerateAssertionsAbout[reln, from, Filter];
};
RopeCrossCat: PROC [lolora: LOLORA] RETURNS [ans: ROPE] ~ {
ans ← NIL;
FOR lolora ← lolora, lolora.rest WHILE lolora # NIL DO
lora: LORANARROW[lolora.first];
subAns: ROPENIL;
n: NATURAL ← 0;
FOR lora ← lora, lora.rest WHILE lora # NIL DO
n ← n + 1;
SELECT n FROM
1 => NULL;
2 => subAns ← Rope.Cat["{", subAns, "|"];
ENDCASE => subAns ← subAns.Concat["|"];
subAns ← subAns.Concat[NARROW[lora.first]];
ENDLOOP;
IF n > 1 THEN subAns ← subAns.Concat["}"];
IF ans # NIL THEN ans ← ans.Concat["-"];
ans ← ans.Concat[subAns];
ENDLOOP;
ans ← ans;
};
END.