------------------------->  GNU Sather - sourcefile  <-------------------------
-- Copyright (C) 1995 by International Computer Science Institute            --
-- This file is part of the GNU Sather package. It is free software; you may --
-- redistribute  and/or modify it under the terms of the  GNU General Public --
-- License (GPL)  as  published  by the  Free  Software  Foundation;  either --
-- version 2 of the license, or (at your option) any later version.          --
-- This  program  is distributed  in the  hope that it will  be  useful, but --
-- WITHOUT ANY WARRANTY without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. See Doc/GPL for more details.        --
-- The license text is also available from:  Free Software Foundation, Inc., --
-- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                     --
-------------->  Please email comments to <bug-sather@gnu.org>  <--------------

-- am.sa: An abstract machine representation for Sather programs.
-------------------------------------------------------------------
-- $AM: Supertype of everything in abstract machine representation.
-- AM: Implementation to be included by $AM nodes.
--
-- AM_OB_DEF: The layout of objects of a particular type. 
-- AM_ROUT_DEF: A function definition representing a routine or iter.

-------------------------------------------------------------------
abstract class $AM < $PROG_ERR is
   -- Supertype of everything in abstract machine representation.
   
   source:SFILE_ID;		-- The origin of a node in a Sather 
      -- source file.
end;
   
-------------------------------------------------------------------
class AM is
   -- Supertype of everything in abstract machine representation.
   
   attr source:SFILE_ID; -- Information identifying the 
      -- origin of a node in Sather source. It encodes the file and
      -- the character offset of the originating construct.   
   
   create(source:SFILE_ID):SAME is
      -- A new object for the location `source' with default 
      -- initialization.
      r::=new; r.source:=source; return r end;
      
end;

-------------------------------------------------------------------   
class AM_OB_DEF < $AM is
   -- The layout of a particular object type. 
   include AM;
   
   attr tp:$TP;			-- The type this represents.
   attr at:FMAP{IDENT,$TP};	-- Maps from attribute names to types.
   attr sorted_at:ARRAY{IDENT};	-- List of sorted attributes (ben)
   attr arr:$TP;		-- The type of the array elements, if any.
   attr asize:INT;		-- The size of the array portion if it
      -- is constant. -1 for variable sized arrays. 

end;

-------------------------------------------------------------------      
class AM_ROUT_DEF <$AM is
   -- The definition of a function representing Sather routines and iters.
   include AM create->;
   include ARRAY{AM_FORMAL_ARG} append->,merge_with_by->; 
      -- An array of the argument local
      -- variables in order. The first is `self'.   
   
   attr sig:SIG;		-- The signature of the routine.
   attr is_abstract:BOOL;	-- True for an abstract routine, in 
      -- this case none of the below are defined.
   attr calls:FLIST{$AM_EXPR};  -- The calls that occur in the 
      -- body of this routine or iter.
   attr locals:FLIST{AM_LOCAL_EXPR}; -- The list of local variables. 
   attr rres:AM_LOCAL_EXPR;	-- The result local variable if used.
   attr code:$AM_STMT;		-- Code to execute when called.
   attr is_external:BOOL;	-- True if this is an external routine
      -- with a body, i.e. it is defined in Sather but callable from an
      -- external language. We don't generate an AM_ROUT_DEF for
      -- routines defined in the external language.
   attr is_clean:BOOL;		-- True if known to not affect existing 
      -- state.
   attr num_yields:INT;		-- The number of yields in the body.
   attr srcsig:SIG;		-- The signature of this routine or iter
      -- in the class it originally came from. Used to detect built-in ops.
   attr specul_prefetch:AM_LOCAL_EXPR;	-- used if the function uses speculative prefetch
				        -- only used in pSather
   
   create(nargs:INT, source:SFILE_ID):SAME is
      -- A new definition for a routine or iter with `nargs' arguments
      -- (including self).
      r::=new(nargs); r.source:=source; return r end;

   calls_size:INT is
      if void(calls) then return 0 end;
      return calls.size end;
   
   is_iter:BOOL is
      -- True if an iter.
      return sig.is_iter end;

   name:IDENT is
      -- The Sather name.
      return sig.name end;
   
   number_of_args:INT is
      -- The number of arguments for this routine or iter 
      -- (including self).
      return size end;

   self_local:AM_LOCAL_EXPR is
      -- The local variable representing self.
      return [0].expr end;

   local_is_hot(l:AM_LOCAL_EXPR):BOOL is
      -- True if `l' is a hot argument to an iter.
      if ~is_iter or void(sig.hot) then return false end;
      i:INT:=0;
      loop while!(i<asize); 
	 if SYS::ob_eq([i].expr,l) then return sig.hot[i] end;
	 i:=i+1 end;
      return false end;
      
   arg_local(i:INT):AM_LOCAL_EXPR is
      -- The local variable for the `i'th argument. 0 is self, the rest
      -- follow in order.
      return [i].expr end;

   --pSather
  attr is_fork_routine : BOOL;  -- this routine implements the body of a fork
  attr is_par_routine : BOOL;   -- this routine implements the body of a par
  attr is_attach_routine : BOOL;  -- ... of an attach

  private attr n_import_locals : BOOL;
  private attr n_import_call   : BOOL;
  private attr n_export_locals : BOOL;
  private attr n_export_call   : BOOL;

  needs_import_locals is 
    sig.needs_import := ~sig.is_forked; -- = true if ~is_forked
    n_import_locals := true 
  end;
  needs_import_call is n_import_call := true end;
  needs_import_locals:BOOL is return sig.needs_import end;
  import_locals_pending:BOOL is return n_import_locals end;
  import_call_pending:BOOL is return n_import_call end;
  import_done is n_import_locals := false; n_import_call := false end;

  needs_export_locals is 
    sig.needs_export := ~sig.is_forked; -- = true if ~is_forked
    n_export_locals := true
  end;
  needs_export_call is n_export_call := true end;
  needs_export_locals:BOOL is return sig.needs_export end;
  export_locals_pending:BOOL is return n_export_locals end;
  export_call_pending: BOOL is return n_export_call end;
  export_done is n_export_locals := false; n_export_call := false end;

end;
   





