-
+ 0FC0C9657956634910EDC6DC4ABE8C5F9743F9C541491D3C1800CD9F9C05B3BF081190DD80C90ABBA91E9358D43CF40F1B7C20F645FFEBAF459683E711ABD782
adalisp/src/lispm.ads
(0 . 0)(1 . 157)
1862 -- Basic lisp machine memory and data representation. The basic memory
1863 -- unit of lisp machines is a cell of the form [ tag | data ]. Lisp
1864 -- memory is an array of such cells, with cell index 0 being reserved
1865 -- for the special value NIL.
1866 package LispM is
1867 -- 8MCells should be enough for everyone
1868 Mem_Size : constant := 2**23;
1869
1870 -- Machine words
1871 type MWord is mod 2 ** 64;
1872 for MWord'Size use 64;
1873
1874 -- Cell tags.
1875 type Tag is (Free, Builtin, Cons, Bool, Fixnum, Char, Symbol, Closure);
1876 type MemPtr is range 0 .. Mem_Size;
1877
1878 -- Built-in functions are tied in to a conceptual arithmetic-logic
1879 -- unit that provides the building blocks for evaluation.
1880 type BuiltinID is (AddB, SubB, MulB, DivB, QuoteB, EvalB, IfB,
1881 ConsB, CarB, CdrB, ListB, ApplyB, DefineB, SetB,
1882 EqnB, EqB, EqvB, PairPB, BooleanPB, NumberPB, SymbolPB,
1883 NullPB, ListPB, AndB, OrB, NotB, LambdaB, LetB,
1884 ReverseB, AppendB);
1885
1886 -- Cell data type. The first part of any cell is a tag. The second
1887 -- part is a tag-dependent machine word.
1888 --
1889 -- Machine words are defined by tag, as follows:
1890 --
1891 -- [ free | 0 ]
1892 --
1893 -- [ builtin | bid ] where bid is a number corresponding uniquely to
1894 -- a BuiltinID.
1895 --
1896 -- [ cons | car, cdr ] where car and cdr are each half of an MWord.
1897 --
1898 -- [ bool | b ] where b is a truth value (0 is false, 1 is true). XXX
1899 -- bool values should be hardcoded symbols (or something similar).
1900 --
1901 -- [ fixnum | n ] where n is a signed integer of MWord size / 2, the
1902 -- first bit being a sign bit.
1903 --
1904 -- [ char | c ] where c is the ASCII code of a character.
1905 --
1906 -- [ symbol | ptr ] where ptr points to a list of characters uniquely
1907 -- determining the symbol's name.
1908 --
1909 -- [ closure | code, env ] where code points to a list of the form
1910 -- (args e1 e2 ... en) (where args is a list of symbols) and env
1911 -- points to a symbol-value alist.
1912 type Cell is record
1913 T : Tag;
1914 Data : MWord;
1915 end record;
1916
1917 -- This puts it all together.
1918 type Mem is array (MemPtr range 1 .. Mem_Size) of Cell;
1919
1920 -- Pointer to heap end. This is incremented on memory
1921 -- allocations. Don't worry about deallocations for now, since we
1922 -- don't have a GC (yet).
1923 Heap_End : MemPtr := 0;
1924
1925 -- Pointer to interned symbol list.
1926 Sym_Table : MemPtr := 0;
1927 -- Pointer to interned symbol representing the "quote" keyword. This
1928 -- is used by the parser to transform ' tokens into (quote ...).
1929 Quote_Sym : MemPtr := 0;
1930
1931 -- Pointer to environment: the environment is a list of symbol-value
1932 -- associations; a symbol may have more than one associations at a
1933 -- given point in time, in which case the most recent association
1934 -- will be considered.
1935 Global_Env : MemPtr := 0;
1936
1937 -- A statically-allocated memory.
1938 AMem : Mem := (others => (T => Free, Data => 0));
1939
1940 -- Cell manipulation primitives.
1941 function Get_Builtin(C : Cell) return BuiltinID;
1942 function Get_Car(C : Cell) return MemPtr;
1943 function Get_Cdr(C : Cell) return MemPtr;
1944 function Get_Bool(C : Cell) return Boolean;
1945 function Get_Fixnum(C : Cell) return Long_Integer;
1946 function Get_Char(C : Cell) return Character;
1947 function Get_Symbol(C : Cell) return MemPtr;
1948 function Get_Closure_Code(C : Cell) return MemPtr;
1949 function Get_Closure_Env(C : Cell) return MemPtr;
1950 procedure Set_Builtin(C : in out Cell; B : in BuiltinID);
1951 procedure Set_Car(C : in out Cell; Car : in MemPtr);
1952 procedure Set_Cdr(C : in out Cell; Cdr : in MemPtr);
1953 procedure Set_Bool(C : in out Cell; Value : in Boolean);
1954 procedure Set_Fixnum(C : in out Cell; Value : in Long_Integer);
1955 procedure Set_Char(C : in out Cell; Value : in Character);
1956 procedure Set_Symbol(C : in out Cell; Name : in MemPtr);
1957 procedure Set_Closure_Code(C : in out Cell; Code : in MemPtr);
1958 procedure Set_Closure_Env(C : in out Cell; Env : in MemPtr);
1959
1960 -- Memory management primitives.
1961
1962 -- Allocate cell in AMem.
1963 procedure Alloc_Cell(C : in Cell; P : out MemPtr);
1964 -- Higher-level allocation primitives
1965 procedure Alloc_Builtin(B : BuiltinID; P : out MemPtr);
1966 procedure Alloc_Cons(Car, Cdr : in MemPtr; P : out MemPtr);
1967 procedure Alloc_Bool(Value : in Boolean; P : out MemPtr);
1968 procedure Alloc_Fixnum(Value : in Long_Integer; P : out MemPtr);
1969 procedure Alloc_Char(Value : in Character; P : out MemPtr);
1970 procedure Alloc_Symbol(Name : in MemPtr; P : out MemPtr);
1971 procedure Alloc_Closure(Code, Env : in MemPtr; P : out MemPtr);
1972
1973 -- I/O: output primitives. XXX these should be placed in a separate
1974 -- module.
1975
1976 -- Dump cell to standard output.
1977 procedure Dump_Cell(P : in MemPtr);
1978
1979 -- Recursively dump a cons cell, doing sugary processing.
1980 procedure Dump_Cons(P : in MemPtr);
1981
1982 -- Dump a long integer
1983 procedure Dump_Longint(N : in Long_Integer);
1984
1985 -- Dump the name of a builtin id
1986 procedure Dump_BuiltinID(BID : in BuiltinID);
1987
1988 -- Dump a sequence of chars represented as a list.
1989 procedure Dump_String(P : in MemPtr);
1990
1991 -- Init symbol table to a list of known symbols and add their
1992 -- bindings to builtins to the global environment.
1993 procedure Init_Builtin_Bindings;
1994
1995 -- Check whether two symbol names are equal.
1996 function Name_EqualP(Sym1, Sym2 : MemPtr) return Boolean;
1997
1998 -- Lookup symbol in symbol table. Return a pointer to a the unique
1999 -- symbol object representing it if found, NIL otherwise.
2000 procedure Lookup_Symbol(Name : in MemPtr; Sym : out MemPtr);
2001
2002 -- Similar to Lookup_Symbol, only if the name does not exist, we
2003 -- create and add a new symbol object to the symbol table, and we
2004 -- return it.
2005 procedure Lookup_Or_Create_Symbol(Name : in MemPtr;
2006 Sym : out MemPtr);
2007
2008 -- Lookup a binding for Sym in Env. Returns a symbol-value pair if it
2009 -- exists, NIL otherwise.
2010 procedure Lookup_Env(Sym, Env : in MemPtr; Binding : out MemPtr);
2011 -- Similar to Lookup_Env, only also try Global_Env
2012 procedure Lookup_Env_Or_Global(Sym, Env : in MemPtr;
2013 Binding : out MemPtr);
2014
2015 -- Add a Sym-Value binding in Env. Returns the new binding.
2016 procedure Bind_Env(Sym, Value : in MemPtr;
2017 Env : in out MemPtr; Binding : out MemPtr);
2018 end LispM;