copy
copy /ivy/jacobi/temp/CDMakeProcCommands.mesa ← CDMakeProcCommands.mesa
Jacobi, September 13, 1985 10:19:05 am PDT
CDMakeProcCommands.mesa (module for ChipNDale)
Copyright © 1985 by Xerox Corporation. All rights reserved.
by Christian Jacobi, May 9, 1985 1:58:40 pm PDT
last edited Christian Jacobi, May 9, 1985 1:58:43 pm PDT
DIRECTORY
Ascii,
Atom,
CD,
CDAtomicObjects,
CDBasics,
CDCells,
CDCommandOps,
CDDirectory,
CDExtras,
CDMarkObjects,
CDMenus,
CDObjectProcs,
CDPinObjects,
CDProperties,
CDOrient,
CDSequencer,
Convert,
IO,
RefTab,
Rope,
SymTab,
TerminalIO,
TiogaFileOps,
TiogaOps,
ViewerTools;
CDMakeProcCommands: CEDAR PROGRAM
IMPORTS Ascii, Atom, Convert, CD, CDAtomicObjects, CDBasics, CDCells, CDCommandOps, CDDirectory, CDExtras, CDMarkObjects, CDMenus, CDObjectProcs, CDPinObjects, CDProperties, CDOrient, CDSequencer, IO, RefTab, Rope, SymTab, TerminalIO, TiogaFileOps, TiogaOps, ViewerTools =
BEGIN
Environment: TYPE = REF EnvironmentRep;
EnvironmentRep: TYPE = RECORD [
invocationKey: REF,
rootNode: TiogaFileOps.Ref,
commentNode: TiogaFileOps.Ref, --insert sibling for next comment
modHeaderNode: TiogaFileOps.Ref, --DIRECTORY... to excluding BEGIN
declNode: TiogaFileOps.Ref, --insert sibling for next declaration on module level
procNode: TiogaFileOps.Ref, --insert sibling for next global procedures
procDecNode: TiogaFileOps.Ref, --insert sibling for next local declaration
postDeclNode: TiogaFileOps.Ref, --insert sibling for next declaration on module level
firstModStatementNode: TiogaFileOps.Ref, --reserved for first statement
modStatementNode: TiogaFileOps.Ref, --insert sibling for next mudule statement
modifier: INT ← 0,
directory: SymTab.Ref,
imports: SymTab.Ref,
declaredLayers: ARRAY CD.Layer OF Rope.ROPEALL[NIL],
nameToThing: SymTab.Ref,
thingToName: RefTab.Ref,
hasDirGen: BOOLFALSE,
properties: Atom.PropList
];
MakeCallExpressionProc: TYPE = PROC [env: Environment, ob: CD.Object] RETURNS [Rope.ROPE];
RegisterCallExpressionProc: PROC [class: REF, proc: MakeCallExpressionProc, tech: CD.Technology ← NIL] =
BEGIN
WITH class SELECT FROM
c: REF CD.ObjectClass =>
CDObjectProcs.StoreFurther[c, $MakeCallExpression,
NEW[MakeCallExpressionProc←proc]
];
a: ATOM => RegisterCallExpressionProc[CD.FetchObjectClass[a, tech], proc, tech];
ENDCASE => ERROR
END;
SpecificCallExpression: PROC [env: Environment, ob: CD.Object] RETURNS [r: Rope.ROPENIL] =
BEGIN
x: REF ← CDObjectProcs.FetchFurther[ob.class, $MakeCallExpression];
WITH x SELECT FROM
rmce: REF MakeCallExpressionProc => r ← rmce^[env, ob]
ENDCASE => NULL;
END;
MakeNodeComment: PROC [node: TiogaFileOps.Ref] =
BEGIN
TRUSTED { TiogaOps.PutProp[LOOPHOLE[node], $Comment, NEW[BOOLEANTRUE]] };
END;
Reserve: PROC [env: Environment, key: Rope.ROPE] =
BEGIN
[] ← SymTab.Store[env.nameToThing, key, $reserved];
END;
IncludeDirectory: PROC [env: Environment, module: Rope.ROPE, import: BOOLTRUE] =
BEGIN
[] ← SymTab.Store[env.nameToThing, module, $module];
[] ← SymTab.Insert[env.directory, module, module];
IF import THEN [] ← SymTab.Insert[env.imports, module, module];
END;
MakeDirectory: PROC [env: Environment] =
BEGIN
r: Rope.ROPENIL;
Each: SymTab.EachPairAction =
BEGIN
IF num=0 THEN TiogaFileOps.SetContents[env.modHeaderNode, "DIRECTORY"]
ELSE r ← r.Concat[", "];
r ← r.Concat[key];
num ← num+1;
quit ← FALSE
END;
num: INT ← 0;
[] ← env.directory.Pairs[Each];
IF num>0 THEN {
node: TiogaFileOps.Ref ← TiogaFileOps.InsertNode[env.modHeaderNode, TRUE];
TiogaFileOps.SetContents[node, r.Concat[";"]];
}
ELSE TiogaFileOps.SetContents[env.modHeaderNode, ""];
END;
MakeImportsExpr: PROC [env: Environment] RETURNS [r: Rope.ROPENIL] =
BEGIN
Each: SymTab.EachPairAction =
BEGIN
IF num=0 THEN r ← "IMPORTS "
ELSE r ← r.Concat[", "];
r ← r.Concat[key];
num ← num+1;
quit ← FALSE
END;
num: INT ← 0;
[] ← env.imports.Pairs[Each];
END;
NewEnvironment: PROC [t: CD.Technology] RETURNS [env: Environment] =
BEGIN
env ← NEW[EnvironmentRep];
env.invocationKey ← NEW[INT];
env.rootNode ← TiogaFileOps.CreateRoot[];
env.commentNode ← TiogaFileOps.InsertNode[env.rootNode, TRUE];
TiogaFileOps.SetContents[env.commentNode, "--///temp/ChipNDale/temp.mesa"];
MakeNodeComment[env.commentNode];
env.commentNode ← TiogaFileOps.InsertNode[env.commentNode];
TiogaFileOps.SetContents[env.commentNode, "--created by ChipNDale"];
MakeNodeComment[env.commentNode];
env.modHeaderNode ← TiogaFileOps.InsertNode[env.commentNode];
env.declNode ← TiogaFileOps.InsertNode[env.modHeaderNode];
TiogaFileOps.SetContents[env.declNode, "BEGIN"];
ModDeclarationLine[env, IO.PutFR["tech: CD.Technology ← CD.FetchTechnology[$%g];", IO.atom[t.key]]];
env.procNode ← TiogaFileOps.InsertNode[env.declNode];
TiogaFileOps.SetContents[env.procNode, ""];
env.postDeclNode ← TiogaFileOps.InsertNode[env.procNode];
TiogaFileOps.SetContents[env.postDeclNode, "table: CDGenerate.Table ← CDGenerate.AssertTable[""USER""];"];
env.firstModStatementNode ← env.modStatementNode ← TiogaFileOps.InsertNode[env.postDeclNode];
env.nameToThing ← SymTab.Create[];
env.directory ← SymTab.Create[];
env.imports ← SymTab.Create[];
env.thingToName ← RefTab.Create[];
END;
FinishUp: PROC [env: Environment] =
BEGIN
node: TiogaFileOps.Ref;
subNode: TiogaFileOps.Ref;
fileName: Rope.ROPE ← "///temp/ChipNDale/temp.mesa";
r: Rope.ROPENIL;
MakeDirectory[env];
node ← TiogaFileOps.InsertNode[env.modHeaderNode];
TiogaFileOps.SetContents[node, "Temp: CEDAR PROGRAM"];
subNode ← TiogaFileOps.InsertNode[node, TRUE];
TiogaFileOps.SetContents[subNode, Rope.Cat[MakeImportsExpr[env], " ="]];
r ← "[] ← table.Register[""Temp"", CreateCell];";
TiogaFileOps.SetContents[env.firstModStatementNode, r];
ModStatementLine[env, "END."];
TiogaFileOps.Store[env.rootNode, fileName];
env.rootNode ← env.commentNode ← env.modHeaderNode ← NIL;
env.declNode ← env.procNode ← env.postDeclNode ← NIL;
env.firstModStatementNode ← env.modStatementNode ← NIL;
env.directory ← env.imports ← env.nameToThing ← NIL;
env.thingToName ← NIL;
TerminalIO.WriteRope[fileName];
TerminalIO.WriteRope[" created\n"];
[] ← ViewerTools.MakeNewTextViewer[info: [
file: fileName,
label: fileName,
name: fileName,
iconic: FALSE
]];
END;
MakeProcedureNode: PROC [env: Environment] RETURNS [TiogaFileOps.Ref] =
BEGIN
env.procNode ← TiogaFileOps.InsertNode[env.procNode];
env.procDecNode ← NIL;
RETURN [env.procNode]
END;
MakeDeclarationNode: PROC [env: Environment] RETURNS [TiogaFileOps.Ref] =
BEGIN
env.declNode ← TiogaFileOps.InsertNode[env.declNode];
RETURN [env.declNode]
END;
ModDeclarationLine: PROC [env: Environment, line: Rope.ROPE] =
BEGIN
node: TiogaFileOps.Ref ← MakeDeclarationNode[env];
TiogaFileOps.SetContents[node, line];
END;
ModPostDeclarationLine: PROC [env: Environment, line: Rope.ROPE] =
BEGIN
env.postDeclNode ← TiogaFileOps.InsertNode[env.postDeclNode];
TiogaFileOps.SetContents[env.postDeclNode, line];
END;
ModStatementLine: PROC [env: Environment, line: Rope.ROPE] =
BEGIN
env.modStatementNode ← TiogaFileOps.InsertNode[env.modStatementNode];
TiogaFileOps.SetContents[env.modStatementNode, line];
END;
MakeDirGenerator: PROC [env: Environment] =
BEGIN
IF ~env.hasDirGen THEN {
env.hasDirGen ← TRUE;
ModPostDeclarationLine[env, "directory: CDGenerate.Table ← CDGenerate.AssertTable[""DIRECTORY""];"];
}
END;
Ident: PROC [env: Environment, proposed: Rope.ROPENIL, whatFor: REFNIL] RETURNS [r: Rope.ROPE] =
BEGIN
Trans: Rope.TranslatorType =
BEGIN
IF Ascii.Letter[old] THEN new ← old
ELSE IF Ascii.Digit[old] AND ~first THEN new ← old
ELSE new ← 'x;
first ← FALSE
END;
first: BOOLTRUE;
proposed ← Rope.Translate[base: proposed, translator: Trans];
r ← proposed;
IF whatFor#NIL THEN
WITH RefTab.Fetch[env.thingToName, whatFor].val SELECT FROM
n: Rope.ROPE => RETURN [r ← n];
ENDCASE =>NULL;
DO
IF SymTab.Insert[env.nameToThing, r, whatFor] THEN {
[] ← RefTab.Store[env.thingToName, whatFor, r];
RETURN;
};
env.modifier ← env.modifier+1;
r ← IO.PutFR["%gx%g", IO.rope[proposed], IO.int[env.modifier]]
ENDLOOP
END;
IdentForObject: PROC [env: Environment, ob: CD.Object] RETURNS [name: Rope.ROPE] =
BEGIN
first: CHAR;
name ← CDDirectory.Name[ob];
IF Rope.IsEmpty[name] THEN {
name ← Atom.GetPName[ob.class.objectType]
};
first ← Rope.Fetch[name];
IF Ascii.Letter[first] THEN first ← Ascii.Lower[first];
name ← Rope.Replace[
base: name, start: 0, len: 1,
with: Rope.FromChar[first]
];
name ← Ident[env, name, ob];
END;
SizeToRope: PROC [pos: CD.Position] RETURNS [r: Rope.ROPE] =
BEGIN
RETURN [IO.PutFR["[x: %g, y: %g]",
IO.int[pos.x],
IO.int[pos.y]
]]
END;
RectToRope: PROC [rect: CD.Rect] RETURNS [r: Rope.ROPE] =
BEGIN
RETURN [IO.PutFR["[x1: %g, y1: %g, x2: %g, y2: %g]",
IO.int[rect.x1],
IO.int[rect.y1],
IO.int[rect.x2],
IO.int[rect.y2]
]]
END;
LayerIdent: PROC [l: CD.Layer, env: Environment] RETURNS [r: Rope.ROPE] =
BEGIN
IF env.declaredLayers[l]=NIL THEN {
r: Rope.ROPE;
name: Rope.ROPE ← Ident[env, Atom.GetPName[CD.LayerKey[l]]];
env.declaredLayers[l] ← name;
r ← IO.PutFR["%g: CD.Layer ← CD.FetchLayer[t: tech, uniqueKey: $%g];",
IO.rope[name],
IO.atom[CD.LayerKey[l]]
];
ModDeclarationLine[env, r];
};
RETURN [env.declaredLayers[l]]
END;
MapClientPos: PROC [inst: CD.Instance] RETURNS [originPos: CD.Position] =
BEGIN
RETURN [CDOrient.MapPoint[
pointInCell: CD.ClientOrigin[inst.ob],
cellSize: inst.ob.size,
cellInstOrient: inst.orientation,
cellInstPos: inst.location
]]
END;
ProcCodeLine: PROC [env: Environment, line: Rope.ROPE] =
BEGIN
node: TiogaFileOps.Ref;
node ← TiogaFileOps.InsertAsLastChild[env.procNode];
TiogaFileOps.SetContents[node, line];
END;
ProgramText: PROC [ob: CD.Object, design: CD.Design, env: Environment] =
BEGIN
IF ~CDCells.IsCell[ob] THEN ob ← CDDirectory.ExpandHard[ob, design, NIL];
IF CDCells.IsCell[ob] THEN {
ir: CD.Rect;
hasInst: BOOLFALSE;
num: INT ← 0;
r: Rope.ROPENIL;
cptr: CD.CellPtr = NARROW[ob.specificRef];
childs: RefTab.Ref ← RefTab.Create[];
node: TiogaFileOps.Ref ← MakeProcedureNode[env];
TiogaFileOps.SetContents[node, "CreateCell: CDGenerate.GeneratorProc ="];
env.procDecNode ← TiogaFileOps.InsertAsLastChild[env.procNode];
TiogaFileOps.SetContents[env.procDecNode, "BEGIN"];
--make declarations
FOR list: CD.InstanceList ← cptr.contents, list.rest WHILE list#NIL DO
x: REF ← RefTab.Fetch[childs, list.first.ob].val;
IF x=NIL THEN {
num ← num+1;
[] ← RefTab.Store[childs, list.first.ob, $first]
}
ELSE IF x=$first THEN {
r: Rope.ROPE ← IdentForObject[env, list.first.ob];
num ← num-1;
[] ← RefTab.Store[childs, list.first.ob, $hasName];
ProcCodeLine[env, Rope.Cat[r, ": CD.Object;"]];
}
ELSE NULL;
ENDLOOP;
IF num#0 THEN ProcCodeLine[env, "child: CD.Object;"];
ProcCodeLine[env, "IF design.technology#tech THEN ERROR;"];
ProcCodeLine[env, "ob ← CDCells.CreateEmptyCell[];"];
--make instances
FOR list: CD.InstanceList ← cptr.contents, list.rest WHILE list#NIL DO
needInst: BOOLFALSE;
childName: Rope.ROPE ← "child";
pos: CD.Position ← MapClientPos[inst: list.first];
orientationCode: Rope.ROPENIL;
IF list.first.orientation#0 THEN
orientationCode ← IO.PutFR[", orientation: %g", IO.int[list.first.orientation]];
SELECT RefTab.Fetch[childs, list.first.ob].val FROM
$hasName => {
childName ← IdentForObject[env, list.first.ob];
[r, needInst] ← MakeACallExpression[env, list.first.ob];
r ← Rope.Cat[childName, " ← ", r, ";"];
ProcCodeLine[env, r];
IF needInst THEN [] ← RefTab.Store[childs, list.first.ob, $hasValueMI]
ELSE [] ← RefTab.Store[childs, list.first.ob, $hasValue]
};
$hasValue => {
childName ← IdentForObject[env, list.first.ob];
needInst ← FALSE;
};
$hasValueMI => {
childName ← IdentForObject[env, list.first.ob];
needInst ← TRUE;
};
ENDCASE => {
childName ← "child";
[r, needInst] ← MakeACallExpression[env, list.first.ob];
r ← Rope.Cat[childName, " ← ", r, ";"];
ProcCodeLine[env, r];
};
IF CDProperties.GetPropFromInstance[list.first, $SignalName]#NIL OR CDProperties.GetPropFromInstance[list.first, $InstanceName]#NIL THEN needInst ← TRUE;
IF needInst THEN {
hasInst ← TRUE;
r ← Rope.Cat[
IO.PutFR["inst ← CDCells.IncludeOb[cell: ob, ob: %g, position: %g%0g].newInst;",
IO.rope[childName],
IO.rope[SizeToRope[pos]],
IO.rope[orientationCode]
]];
}
ELSE {
r ← Rope.Cat[
IO.PutFR["[] ← CDCells.IncludeOb[cell: ob, ob: %g, position: %g%0g];",
IO.rope[childName],
IO.rope[SizeToRope[pos]],
IO.rope[orientationCode]
]];
};
ProcCodeLine[env, r];
IF needInst THEN MakeInstanceStuff[env, list.first];
ENDLOOP;
--interest rect
ir ← CD.InterestRect[ob];
r ← Rope.Cat["CDCells.SetInterestRect[ob, ", RectToRope[ir], "];"];
ProcCodeLine[env, r];
--include in directory
r ← Rope.Cat["[] ← CDDirectory.Include[design: design, object: ob"];
IF cptr.name#NIL THEN
r ← Rope.Cat[r, ", alternateName: ", ToRopeRope[cptr.name]];
r ← Rope.Cat[r, "];"];
ProcCodeLine[env, r];
ProcCodeLine[env, "END;"];
ProcCodeLine[env, ""];
IF hasInst THEN {
env.procDecNode ← TiogaFileOps.InsertNode[env.procDecNode];
TiogaFileOps.SetContents[env.procDecNode, "inst: CD.Instance;"];
};
childs ← NIL;
}
ELSE {
TerminalIO.WriteRope["**could not convert to cell\n"];
ERROR ABORTED;
}
END;
ToRopeRope: PROC [r: Rope.ROPE] RETURNS [Rope.ROPE] =
BEGIN
RETURN [Convert.RopeFromRope[r]];
END;
ToAtomRope: PROC [a: ATOM, env: Environment] RETURNS [Rope.ROPE] =
BEGIN
r: Rope.ROPE ← Atom.GetPName[a];
leng: INT ← Rope.Length[r];
normalCase: BOOL ← leng>0;
FOR i: INT IN [0..leng) DO
c: CHAR ← Rope.Fetch[r, i];
IF ~(Ascii.Letter[c] OR Ascii.Digit[c]) THEN {normalCase←FALSE; EXIT}
ENDLOOP;
IF normalCase THEN RETURN [Rope.Cat["$", r]];
IncludeDirectory[env, "Atom"];
RETURN [ Rope.Cat["Atom.MakeAtom[", Convert.RopeFromRope[r], "]"] ];
END;
MakeInstanceStuff: PROC[env: Environment, inst: CD.Instance] =
BEGIN
r: Rope.ROPENIL;
HandleProp: PROC [key: ATOM] =
BEGIN
val: Rope.ROPE ← CDExtras.ToRope[CDProperties.GetPropFromInstance[inst, key]];
IF ~Rope.IsEmpty[val] THEN {
r: Rope.ROPE ← Rope.Cat[
"CDProperties.PutPropOnInstance[inst, ",
ToAtomRope[key, env],
", ",
ToRopeRope[val],
"];"
];
IncludeDirectory[env, "CDProperties"];
ProcCodeLine[env, r];
};
END;
IF CDPinObjects.IsPinOb[inst.ob] THEN {
l: CD.Layer;
owner: ATOM;
name: Rope.ROPE ← CDPinObjects.GetName[inst];
IF ~Rope.IsEmpty[name] THEN {
r ← Rope.Cat["CDPinObjects.SetName[inst, ", ToRopeRope[name], "];"];
ProcCodeLine[env, r];
};
l ← CDPinObjects.GetLayer[inst];
IF l#CD.combined THEN {
r ← Rope.Cat["CDPinObjects.SetLayer[inst, ", LayerIdent[l, env], "];"];
ProcCodeLine[env, r];
};
owner ← CDPinObjects.GetOwner[inst];
IF owner#NIL THEN {
r ← Rope.Cat["CDPinObjects.SetOwner[inst, ", ToAtomRope[owner, env], "];"];
ProcCodeLine[env, r];
};
}
ELSE IF inst.ob=CDMarkObjects.markOb THEN {
name: Rope.ROPE ← CDMarkObjects.GetMarkName[inst];
IF ~Rope.IsEmpty[name] THEN {
r ← Rope.Cat["CDMarkObjects.SetMarkName[inst, ", ToRopeRope[name], "];"];
ProcCodeLine[env, r];
};
}
ELSE {
HandleProp[$SignalName];
HandleProp[$InstanceName];
}
END;
MakeACallExpression: PROC[env: Environment, ob: CD.Object] RETURNS [r: Rope.ROPENIL, mustHandleInstance: BOOLFALSE] =
BEGIN
r ← SpecificCallExpression[env, ob];
IF Rope.IsEmpty[r] THEN {
IF ob.class.inDirectory THEN {
MakeDirGenerator[env];
r ← Rope.Cat["CDGenerate.FetchNCall[directory, design, ", ToRopeRope[CDDirectory.Name[ob]], "]"];
}
ELSE IF CDAtomicObjects.IsAtomicOb[ob] THEN {
IncludeDirectory[env, "CDAtomicObjects"];
r ← "CDAtomicObjects.CreateAtomicOb[classKey: ";
r ← Rope.Cat[r, "$", CDExtras.ToRope[ob.class.objectType], ", size: "];
r ← Rope.Cat[r, SizeToRope[CDBasics.SizeOfRect[CD.InterestRect[ob]]], ", "];
r ← Rope.Cat[r, SizeToRope[ob.size], ", "];
r ← Rope.Cat[r, "tech: tech, lev: ", LayerIdent[ob.layer, env], "]"];
}
ELSE IF CDPinObjects.IsPinOb[ob] THEN {
IncludeDirectory[env, "CDPinObjects"];
r ← "CDPinObjects.CreatePinOb[";
r ← Rope.Cat[r, SizeToRope[CDBasics.SizeOfRect[CD.InterestRect[ob]]], "]"];
mustHandleInstance ← TRUE;
}
ELSE IF ob=CDMarkObjects.markOb THEN {
IncludeDirectory[env, "CDMarkObjects"];
r ← "CDMarkObjects.markOb";
mustHandleInstance ← TRUE;
}
ELSE IF ob.class.wireTyped THEN {
IncludeDirectory[env, "CDRects"];
r ← "CDRects.CreateRect[size: ";
r ← Rope.Cat[r, SizeToRope[CDBasics.SizeOfRect[CD.InterestRect[ob]]]];
r ← Rope.Cat[r, ", l: ", LayerIdent[ob.layer, env], "]"];
}
ELSE {
r ← "--Unknown[]--";
IncludeDirectory[env, "CDRects"];
r ← Rope.Cat[r, "CDRects.CreateRect[size: "];
r ← Rope.Cat[r, SizeToRope[CDBasics.SizeOfRect[CD.InterestRect[ob]]]];
r ← Rope.Cat[r, ", l: CD.highLightError]"];
};
};
END;
MakeProgramCommand: PROC [comm: CDSequencer.Command] =
BEGIN
inst: CD.Instance = CDCommandOps.TheInstance[comm, "make program text"];
IF inst#NIL THEN {
env: Environment ← NewEnvironment[comm.design.technology];
Reserve[env, "design"];
Reserve[env, "key"];
Reserve[env, "table"];
Reserve[env, "directory"];
Reserve[env, "data"];
Reserve[env, "tech"];
Reserve[env, "child"];
Reserve[env, "CreateCell"];
Reserve[env, "ob"];
Reserve[env, "inst"];
IncludeDirectory[env, "CDCells"];
IncludeDirectory[env, "CDGenerate"];
IncludeDirectory[env, "CD"];
IncludeDirectory[env, "CDDirectory"];
ProgramText[inst.ob, comm.design, env];
FinishUp[env];
}
END;
--XX SPECIAL OBJECTS XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
--XX INIT XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
CDObjectProcs.RegisterFurther[$MakeCallExpression! CD.Error => CONTINUE];
CDSequencer.ImplementCommand[$MakeProgram, MakeProgramCommand,, doQueue];
CDMenus.CreateEntry[menu: $ProgramMenu, entry: "make text", key: $MakeProgram];
TerminalIO.WriteRope["make program command loaded\n"];
END.