-- MachineDefs.mesa  last edit: Bruce,  September 19, 1980  4:04 PM

DIRECTORY
  AllocDefs USING [DefaultDataSegmentInfo, SwappingProcedure],
  AltoDefs USING [
    BYTE, BytesPerPage, CharsPerPage, PageCount, PageNumber, PageSize, wordlength],
  AltoFileDefs USING [CFA, CFP, eofDA, FA, FH, fillinDA, FP, vDA],
  ControlDefs USING [
    ControlLink, ControlLinkTag, ControlModule, CSegPrefix, FieldDescriptor, Frame,
    FrameCodeBase, FrameHandle, GFT, GFTHandle, GFTIndex, GFTItem, 
    GlobalFrame, GlobalFrameHandle, localbase, NullGlobalFrame, ProcDesc, SignalDesc,
    StateVector, SVPointer, WordPC],
  CoreSwapDefs,
  DirectoryDefs USING [BadDirectory],
  LoadStateFormat USING [ConfigIndex, NullConfig],
  Mopcodes USING [zBLT],
  PrincOps USING [BytePC],
  ProcessDefs USING [Priority],
  PSBDefs USING [ProcessHandle],
  SegmentDefs USING [InsufficientVM],
  Segments,
  Streams USING [Handle],
  Symbols USING [CTXIndex];

MachineDefs: DEFINITIONS IMPORTS DirectoryDefs, SegmentDefs, Segments SHARES Segments =
  BEGIN

  CoPilot: BOOLEAN = FALSE;

  -- AllocDefs
  SwappingProcedure: TYPE = AllocDefs.SwappingProcedure;
  DefaultDataSegmentInfo: Segments.AllocInfo = AllocDefs.DefaultDataSegmentInfo;

  -- AltoDefs
  BYTE: TYPE = AltoDefs.BYTE;
  BytesPerPage: CARDINAL = AltoDefs.BytesPerPage;
  CharsPerPage: CARDINAL = AltoDefs.CharsPerPage;
  PageCount: TYPE = AltoDefs.PageCount;
  PageNumber: TYPE = AltoDefs.PageNumber;
  PageSize: CARDINAL = AltoDefs.PageSize;
  WordLength: CARDINAL = AltoDefs.wordlength;

  -- AltoFileDefs
  CFA: TYPE = AltoFileDefs.CFA;
  CFP: TYPE = AltoFileDefs.CFP;
  eofDA: vDA = AltoFileDefs.eofDA;
  FileHint: TYPE = AltoFileDefs.FH;
  fillinDA: vDA = AltoFileDefs.fillinDA;
  FA: TYPE = AltoFileDefs.FA;
  FP: TYPE = AltoFileDefs.FP;
  vDA: TYPE = AltoFileDefs.vDA;

  -- Compiler
  MaxParmsInStack: CARDINAL = 5;

  MLCtx: Symbols.CTXIndex = LOOPHOLE[10];
  CVCtx: Symbols.CTXIndex = LOOPHOLE[12];

  -- ControlDefs
  ControlLink: TYPE = ControlDefs.ControlLink;
  ControlModule: TYPE = ControlDefs.ControlModule;
  CSegPrefix: TYPE = ControlDefs.CSegPrefix;
  FHandle: TYPE = ControlDefs.FrameHandle;
  FieldDescriptor: TYPE = ControlDefs.FieldDescriptor;
  Frame: TYPE = ControlDefs.Frame;
  FrameCodeBase: TYPE = ControlDefs.FrameCodeBase;
  GFT: ControlDefs.GFTHandle = ControlDefs.GFT;
  GFTIndex: TYPE = ControlDefs.GFTIndex;
  GFTItem: TYPE = ControlDefs.GFTItem;
  GFHandle: TYPE = ControlDefs.GlobalFrameHandle;
  GlobalFrame: TYPE = ControlDefs.GlobalFrame;
  localbase: CARDINAL = ControlDefs.localbase;
  NullGF: GFHandle = ControlDefs.NullGlobalFrame;
  NullOffset: CARDINAL = LAST[CARDINAL];
  ProcDesc: TYPE = ControlDefs.ProcDesc;
  SignalDesc: TYPE = ControlDefs.SignalDesc;
  SVPointer: TYPE = ControlDefs.SVPointer;
  StateVector: TYPE = ControlDefs.StateVector;
  RealPC: TYPE = ControlDefs.WordPC;

  ByteToRealPC: PROCEDURE [bpc: BytePC] RETURNS [wpc: RealPC] = INLINE {
    wpc ← RealPC[bpc/2];
    RETURN[IF bpc MOD 2 = 1 THEN RealPC[-wpc] ELSE wpc] };

  RealToBytePC: PROCEDURE [wpc: RealPC] RETURNS [bpc: BytePC] = INLINE {
    odd: BOOLEAN ← wpc < 0;
    bpc ← [ABS[wpc]*2+LOOPHOLE[odd, INTEGER]] };

  GfiToFrame: PROC [gfi: GFTIndex] RETURNS [GFHandle];
  GfiToOffset: PROC [gfi: GFTIndex] RETURNS [CARDINAL];

  LinkType: TYPE = ControlDefs.ControlLinkTag;

  TypeOfLink: PROCEDURE [l: ControlLink] RETURNS [LinkType] = INLINE {
    RETURN[l.tag]};

  -- Inline
  LCOPY: PROCEDURE [from: POINTER, nwords: CARDINAL, to: POINTER] =
    MACHINE CODE BEGIN Mopcodes.zBLT END;

  -- LoadStateFormat
  ConfigIndex: TYPE = LoadStateFormat.ConfigIndex;
  NullConfig: ConfigIndex = LoadStateFormat.NullConfig;

  -- CoreSwapDefs
  UBBPointer: TYPE = CoreSwapDefs.UBBPointer;
  BBHandle: TYPE = CoreSwapDefs.BBHandle;
  CallDP: TYPE = CoreSwapDefs.CallDP;
  ExternalStateVector: TYPE = CoreSwapDefs.ExternalStateVector;
  StartDP: TYPE = CoreSwapDefs.StartDP;
  SwapReason: TYPE = CoreSwapDefs.SwapReason;
  UserBreakBlock: TYPE = CoreSwapDefs.UserBreakBlock;

  -- PrincOps
  BytePC: TYPE = PrincOps.BytePC;

  -- ProcessDefs
  Priority: TYPE = ProcessDefs.Priority;

  -- PSBDefs
  PHandle: TYPE = PSBDefs.ProcessHandle;
  nullPHandle: PHandle = NIL;

  -- Segments
  AccessOptions: TYPE = Segments.Access;
  DefaultAccess: AccessOptions = Segments.DefaultAccess;
  DefaultVersion: VersionOptions = Segments.DefaultVersion;
  FileHandle: TYPE = Segments.FHandle;
  FSHandle: TYPE = Segments.SHandle;
  VersionOptions: TYPE = Segments.VersionOptions;

  CacheNewFile: PROC [
    name: STRING, access: AccessOptions ← DefaultAccess,
    version: VersionOptions ← DefaultVersion]
    RETURNS [FileHandle];

  FileName: PROC [name: STRING, file: FileHandle] = INLINE {
    Segments.NameForFile[name,file]};
  ModifyFile: PROC [name: STRING] RETURNS [BOOLEAN] = INLINE {
    RETURN[Segments.ModifyFile[name]]}; 
  AddModifyProc: PROC [proc: PROC[STRING,FileHandle] RETURNS [BOOLEAN]] = INLINE {
    Segments.AddModifyProc[proc]};
  InvalidateFileCache: PROC = INLINE {Segments.InvalidateFileCache[]};

  OutOfMemory: PROC RETURNS [SIGNAL[needed: PageCount]] = INLINE {
    RETURN[LOOPHOLE[SegmentDefs.InsufficientVM]]};
  Unknown: PROC RETURNS [SIGNAL] = INLINE {RETURN[LOOPHOLE[DirectoryDefs.BadDirectory]]};

  -- StreamDefs
  SHandle: TYPE = Streams.Handle;
  DestroyStream: PROC [s: SHandle] = INLINE {s.destroy[s]};

  END.