TilingClassImpl.mesa
Copyright Ó 1986, 1987 by Xerox Corporation. All rights reversed.
Created by Bertrand Serlet on September 19, 1986 1:48:15 pm PDT
Bertrand Serlet September 12, 1987 5:11:52 pm PDT
Barth, October 16, 1986 6:39:20 pm PDT
Jean-Marc Frailong January 17, 1988 9:33:06 pm PST
TilingClassImpl:
CEDAR
PROGRAM
IMPORTS CD, CDBasics, CoreClasses, CoreCreate, CoreOps, CoreProperties, CoreGeometry, GList, PW, PWCore, RefTab, Sisyph
EXPORTS TilingClass
~ BEGIN OPEN TilingClass;
Utilities
PubActs: TYPE = REF PubActsRec;
PubActsRec:
TYPE =
RECORD [pubActs:
SEQUENCE size:
NAT
OF
RECORD [pub, act: Wire]];
PutTilePubActs:
PROC [tile: Tile, pubs, acts: Wires] = {
size: NAT ← GList.Length[pubs];
seq: PubActs ← NEW [PubActsRec[size]];
IF size#GList.Length[acts] THEN ERROR;
FOR i:
NAT
IN [0 .. size)
DO
seq[i] ← [pub: pubs.first, act: acts.first];
pubs ← pubs.rest; acts ← acts.rest;
ENDLOOP;
tile.properties ← CoreProperties.PutProp[tile.properties, $TilingClassPubActs, seq];
};
GetTilePubActs:
PROC [tile: Tile]
RETURNS [seq: PubActs] = {
seq ← NARROW [CoreProperties.GetProp[tile.properties, $TilingClassPubActs]];
};
PutTileInstance:
PROC [tile: Tile, instance: CoreClasses.CellInstance] = {
tile.properties ← CoreProperties.PutProp[tile.properties, $TilingClassInstance, instance];
};
GetTileInstance:
PROC [tile: Tile]
RETURNS [instance: CoreClasses.CellInstance] = {
RETURN [NARROW [CoreProperties.GetProp[tile.properties, $TilingClassInstance]]];
};
Replace:
PROC [fused: RefTab.Ref, old: Wire]
RETURNS [new: Wire] = {
new ← RootWire[fused, old];
FOR i: NAT IN [0 .. old.size) DO new[i] ← Replace[fused, old[i]] ENDLOOP;
};
RootWire:
PROC [fused: RefTab.Ref, wire: Wire]
RETURNS [root: Wire] = {
up: Wire;
root ← wire;
DO
up ← NARROW [RefTab.Fetch[fused, root].val];
IF up=root THEN ERROR;
IF up=NIL THEN EXIT;
root ← up;
ENDLOOP;
[] ← RefTab.Replace[fused, wire, root];
};
-- The old version, recursive; on a 128 by 128 array, the machine -> 815
RootWire: PROC [fused: RefTab.Ref, wire: Wire] RETURNS [root: Wire] = {
root ← NARROW [RefTab.Fetch[fused, wire].val];
IF root=NIL THEN RETURN [wire];
IF root=wire THEN ERROR;
root ← RootWire[fused, root];
[] ← RefTab.Replace[fused, wire, root];
};
Fuse:
PROC [fused: RefTab.Ref, wire1, wire2: Wire] = {
root1: Wire = RootWire[fused, wire1];
root2: Wire = RootWire[fused, wire2];
IF root1=root2 THEN RETURN;
IF root1.size#root2.size THEN ERROR;
[] ← RefTab.Store[fused, root1, root2];
FOR i: NAT IN [0 .. root1.size) DO Fuse[fused, root1[i], root2[i]] ENDLOOP;
};
FuseNeighbors:
PROC [neighbor: NeighborProc, tile1, tile2: Tile, fused: RefTab.Ref] = {
ct1: CellType = tile1.type;
ct2: CellType = tile2.type;
FOR list:
LIST
OF CoreGeometry.WirePair ← neighbor[ct1, ct2], list.rest
WHILE list#
NIL
DO
Fuse[fused, CoreClasses.CorrespondingActual[GetTileInstance[tile1], list.first.wire1], CoreClasses.CorrespondingActual[GetTileInstance[tile2], list.first.wire2]];
ENDLOOP;
};
CreateActual:
PROC [subPublicToActual: RefTab.Ref, public: WireSeq]
RETURNS [actual: WireSeq] = {
actual ← NARROW [RefTab.Fetch[subPublicToActual, public].val];
IF actual#NIL THEN RETURN;
actual ← CoreOps.CreateWires[size: public.size];
[] ← RefTab.Store[subPublicToActual, public, actual];
FOR i:
NAT
IN [0 .. public.size)
DO
actual[i] ← CreateActual[subPublicToActual, public[i]];
ENDLOOP;
};
CreateInstance:
PROC [public: WireSeq, tile: Tile, xindex, yindex:
NAT]
RETURNS [instance: CoreClasses.CellInstance]= {
subPublicToActual: RefTab.Ref = RefTab.Create[];
actual: WireSeq = CoreOps.CreateWires[size: tile.type.public.size];
make a simpler renaming table [public of tile -> public of me.public]
FOR list:
LIST
OF
PA ← tile.renaming, list.rest
WHILE list#
NIL
DO
pub: Wire = CoreCreate.FindWire[tile.type.public, list.first.public];
act: Wire = CoreCreate.FindWire[public, list.first.actual];
IF pub=NIL OR act=NIL THEN ERROR; -- bad renaming list
[] ← RefTab.Store[subPublicToActual, pub, act];
ENDLOOP;
FOR i:
NAT
IN [0 .. tile.type.public.size)
DO
actual[i] ← CreateActual[subPublicToActual, tile.type.public[i]];
ENDLOOP;
instance ← CoreClasses.CreateInstance[actual: actual, type: tile.type, props: CoreProperties.Props[[$TilingXIndex, NEW [NAT ← xindex]], [$TilingYIndex, NEW [NAT ← yindex]]]];
};
Tiling
tilingClass:
PUBLIC Core.CellClass ←
NEW [Core.CellClassRec ← [name: "Tiling", recast: RecastTiling, layersProps:
FALSE, properties: CoreProperties.Props[[$Layout, $Recast]]]];
CreateTiling:
PUBLIC
PROC [public: WireSeq, tileArray: TileArray, neighborX, neighborY: NeighborProc, name:
ROPE ←
NIL, props: Properties ←
NIL]
RETURNS [tiling: CellType] = {
tiling ← CoreOps.CreateCellType[
class: tilingClass,
public: public,
data: NEW [TilingDataRec ← [tileArray: tileArray, neighborX: neighborX, neighborY: neighborY]],
name: name, props: props
];
};
RecastTiling: Core.RecastProc = {
ReplaceAndAddToPublicHT:
PROC [old: Wire]
RETURNS [new: Wire] ~ {
new ← RootWire[fused, old];
[] ← RefTab.Insert[publicHT, new, NIL];
FOR i: NAT IN [0 .. old.size) DO new[i] ← ReplaceAndAddToPublicHT[old[i]] ENDLOOP;
};
tilingData: TilingData = NARROW [me.data];
public: WireSeq = CoreOps.CopyWire[me.public];
fused: RefTab.Ref = CoreOps.CreateBindingTable[me.public, public]; -- keys are fused to values
tiles: TileArray = tilingData.tileArray;
sizeX: NAT = tiles[0].size;
sizeY: NAT = tiles.size;
internalsOnly: Wires ← NIL;
instances: LIST OF CoreClasses.CellInstance ← NIL;
publicHT: RefTab.Ref ← RefTab.Create[]; --used as a speedup to CoreOps.RecursiveMember[public, wire]
Create instances (even with not the right wires yet)
FOR i:
NAT
IN [0 .. sizeY)
DO
IF tiles[i].size#sizeX THEN ERROR;
FOR j:
NAT
IN [0 .. sizeX)
DO
tile: Tile = tiles[i][j];
instance: CoreClasses.CellInstance ← CreateInstance[me.public, tile, j, i];
IF NOT CoreOps.CorrectConform[instance.actual, instance.type.public] THEN ERROR;
PutTileInstance[tile, instance];
IF tile.flatten
THEN {
rct: CoreClasses.RecordCellType = NARROW [tile.type.data];
publicToActual: RefTab.Ref = CoreOps.CreateBindingTable[instance.type.public, instance.actual];
FOR ii:
NAT
IN [0 .. rct.size)
DO
actual: WireSeq = CoreOps.CreateWires[size: rct[ii].type.public.size];
FOR k:
NAT
IN [0 .. rct[ii].type.public.size)
DO
actual[k] ← NARROW [RefTab.Fetch[publicToActual, rct[ii].actual[k]].val];
ENDLOOP;
instances ← CONS [CoreClasses.CreateInstance[actual: actual, type: rct[ii].type, props: CoreProperties.Props[[$TilingXIndex, NEW [NAT ← j]], [$TilingYIndex, NEW [NAT ← i]]]], instances];
ENDLOOP;
} ELSE instances ← CONS [instance, instances];
ENDLOOP;
ENDLOOP;
Find who to fuse, first progressing in rows
FOR i:
NAT
IN [0 .. sizeY)
DO
FOR j:
NAT
IN [1 .. sizeX)
DO
FuseNeighbors[tilingData.neighborX, tiles[i][j-1], tiles[i][j], fused];
ENDLOOP;
ENDLOOP;
Find who to fuse, progressing in columns
FOR j:
NAT
IN [0 .. sizeX)
DO
FOR i:
NAT
IN [1 .. sizeY)
DO
FuseNeighbors[tilingData.neighborY, tiles[i-1][j], tiles[i][j], fused];
ENDLOOP;
ENDLOOP;
Replace the public and note all public wires in publicHT
FOR i:
NAT
IN [0 .. public.size)
DO
public[i] ← ReplaceAndAddToPublicHT[public[i]];
ENDLOOP;
Replace in instances the actuals
FOR list:
LIST
OF CoreClasses.CellInstance ← instances, list.rest
WHILE list#
NIL
DO
instance: CoreClasses.CellInstance = list.first;
instance.actual ← Replace[fused, instance.actual];
IF NOT CoreOps.Conform[instance.actual, instance.type.public] THEN ERROR; -- the tiles shorts parts of the public together
Compute the internal only wires
FOR k:
NAT
IN [0 .. instance.actual.size)
DO
wire: Wire = instance.actual[k];
IF NOT RefTab.Fetch[publicHT, wire].found AND NOT CoreOps.Member[internalsOnly, wire] THEN internalsOnly ← CONS [wire, internalsOnly];
ENDLOOP;
ENDLOOP;
IF NOT CoreOps.Conform[public, me.public] THEN ERROR; -- the tiles shorts parts of the public together
-- Preparing the decoration by storing on each tile the list of publics which decorations should be offset and added and the list of corresponding actuals [part of public].
FOR i:
NAT
IN [0 .. sizeY)
DO
FOR j:
NAT
IN [0 .. sizeX)
DO
IF i=0
OR i=sizeY-1
OR j=0
OR j=sizeX-1
THEN {
tile: Tile = tiles[i][j];
pubs, acts: Wires ← NIL;
DecorateIfOutside:
PROC [pub: Wire] = {
act: Wire = RootWire[fused, CoreClasses.CorrespondingActual[GetTileInstance[tile], pub]];
IF act=NIL THEN ERROR;
IF NOT RefTab.Fetch[publicHT, act].found THEN RETURN;
IF CoreOps.Member[pubs, pub] THEN RETURN; -- already seen
pubs ← CONS [pub, pubs];
acts ← CONS [act, acts];
};
CoreOps.VisitRootAtomics[tile.type.public, DecorateIfOutside];
PutTilePubActs[tile, pubs, acts];
};
ENDLOOP;
ENDLOOP;
Cleanup
FOR i:
NAT
DECREASING
IN [0 .. sizeY)
DO
FOR j:
NAT
DECREASING
IN [0 .. sizeX)
DO
PutTileInstance[tiles[i][j], NIL];
ENDLOOP;
ENDLOOP;
new ← CoreClasses.CreateRecordCell[
public: public,
internal: CoreOps.UnionWire[public, CoreOps.CreateWire[internalsOnly]],
instances: instances,
name: CoreOps.GetCellTypeName[me],
props: CoreProperties.Props[[$Layout, $Tiling], [$Tiles, tiles]],
giveNames: TRUE
];
};
LayoutTiling: PWCore.LayoutProc = {
Layout: PROC [i, j: NAT] RETURNS [CD.Object] = {RETURN [PWCore.Layout[tiles[i][j].type]]};
Tiles: PROC [x, y: NAT] RETURNS [CD.Object] = {RETURN [Layout[y, x]]};
tiles: TileArray = NARROW [CoreProperties.GetCellTypeProp[cellType, $Tiles]];
sizeX: NAT = tiles[0].size;
sizeY: NAT = tiles.size;
offset: CD.Position ← [0, 0];
ir: CD.Rect;
Generating the layout, now
We check that sizes of the IR verify the assumptions
FOR i:
NAT
IN [0 .. sizeY)
DO
height: INT = CD.InterestSize[Layout[i, 0]].y;
FOR j:
NAT
IN [1 .. sizeX)
DO
IF height#CD.InterestSize[Layout[i, j]].y THEN ERROR; -- assumption not verified
ENDLOOP;
ENDLOOP;
FOR j:
NAT
IN [0 .. sizeX)
DO
width: INT = CD.InterestSize[Layout[0, j]].x;
FOR i:
NAT
IN [1 .. sizeY)
DO
IF width#CD.InterestSize[Layout[i, j]].x THEN ERROR; -- assumption not verified
ENDLOOP;
ENDLOOP;
We compute the ir (necessary for finding the decorations)
FOR i:
NAT
IN [0 .. sizeY)
DO
offset.x ← 0;
FOR j:
NAT
IN [0 .. sizeX)
DO
offset.x ← offset.x + CD.InterestSize[Layout[i, j]].x;
ENDLOOP;
offset.y ← offset.y + CD.InterestSize[Layout[i, 0]].y;
ENDLOOP;
ir ← [0, 0, offset.x, offset.y];
We tile and decorate
offset ← [0, 0];
FOR i:
NAT
IN [0 .. sizeY)
DO
offset.x ← 0;
FOR j:
NAT
IN [0 .. sizeX)
DO
tile: Tile = tiles[i][j];
seq: PubActs = GetTilePubActs[tile];
layout: CD.Object = Layout[i, j];
trans: CoreGeometry.Transformation = [CDBasics.SubPoints[offset, CD.InterestBase[layout]]];
IF seq#
NIL
THEN
FOR i:
NAT
IN [0 .. seq.size)
DO
pins: CoreGeometry.Instances ← CoreGeometry.GetPins[PWCore.extractMode.decoration, seq[i].act];
FOR list: CoreGeometry.Instances ← CoreGeometry.GetPins[PWCore.extractMode.decoration, seq[i].pub], list.rest
WHILE list#
NIL
DO
IF CoreGeometry.TransfedNotAtEdge[trans, ir, list.first] THEN LOOP;
pins ← CONS [CoreGeometry.Transform[trans, list.first], pins];
ENDLOOP;
CoreGeometry.PutPins[PWCore.extractMode.decoration, seq[i].act, pins];
ENDLOOP;
offset.x ← offset.x + CD.InterestSize[Layout[i, j]].x;
ENDLOOP;
offset.y ← offset.y + CD.InterestSize[Layout[i, 0]].y;
ENDLOOP;
obj ← PW.CreateTiling[sizeX, sizeY, Tiles];
};
Predefined NeighborProcs
cache: RefTab.Ref ← CoreGeometry.CreateNeighborsCache[];
LayoutNeighborX:
PUBLIC NeighborProc = {
[] ← PWCore.Layout[ct1]; -- to make sure the cellType is decorated
[] ← PWCore.Layout[ct2]; -- to make sure the cellType is decorated
publicPairs ← CoreGeometry.CachedEnumerateNeighbors[PWCore.extractMode.decoration, PWCore.extractMode.touchProc, TRUE, ct1, ct2, cache];
};
LayoutNeighborY:
PUBLIC NeighborProc = {
[] ← PWCore.Layout[ct1]; -- to make sure the cellType is decorated
[] ← PWCore.Layout[ct2]; -- to make sure the cellType is decorated
publicPairs ← CoreGeometry.CachedEnumerateNeighbors[PWCore.extractMode.decoration, PWCore.extractMode.touchProc, FALSE, ct1, ct2, cache];
};
SchematicsNeighborX:
PUBLIC NeighborProc ={
publicPairs ← CoreGeometry.CachedEnumerateNeighbors[Sisyph.mode.decoration, Sisyph.mode.touchProc, TRUE, ct1, ct2, cache];
};
SchematicsNeighborY:
PUBLIC NeighborProc ={
publicPairs ← CoreGeometry.CachedEnumerateNeighbors[Sisyph.mode.decoration, Sisyph.mode.touchProc, FALSE, ct1, ct2, cache];
};