-
Notifications
You must be signed in to change notification settings - Fork 1
/
MachNode.lhs
49 lines (40 loc) · 1.48 KB
/
MachNode.lhs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
% -*- LaTeX -*-
% $Id: MachNode.lhs 1744 2005-08-23 16:17:12Z wlux $
%
% Copyright (c) 2002, Wolfgang Lux
% See LICENSE for the full license.
%
\subsubsection{Nodes}
This module provides the basic operations for the allocation of new
nodes, for dereferencing nodes and for the update of nodes.
\begin{verbatim}
> module MachNode where
> import MachTypes
> import Combined
> atom :: RefMonad m => NodeTag -> State -> m (NodePtr,State)
> atom (ConstructorTag tag cName 0) = allocNode (ConstructorNode tag cName [])
> allocNode :: RefMonad m => Node -> State -> m (NodePtr,State)
> allocNode node state =
> do
> ref <- newRef node
> return (Ptr adr ref,state{ hp = adr + 1 })
> where adr = hp state
> allocNodes :: RefMonad m => [Node] -> State -> m ([NodePtr],State)
> allocNodes nodes state =
> do
> refs <- mapM newRef nodes
> return (zipWith Ptr [adr..] refs,
> state{ hp = adr + toInteger (length nodes) })
> where adr = hp state
> deref :: RefMonad m => NodePtr -> m Node
> deref (Ptr _ ref) = readRef ref
> deref2 :: RefMonad m => (NodePtr,NodePtr) -> m (Node,Node)
> deref2 (ptr,ptr') =
> deref ptr >>= \node -> deref ptr' >>= \node' -> return (node,node')
> derefPtr :: RefMonad m => NodePtr -> m NodePtr
> derefPtr (Ptr adr ref) = readRef ref >>= derefIndir
> where derefIndir (IndirNode ptr) = derefPtr ptr
> derefIndir _ = return (Ptr adr ref)
> updateNode :: RefMonad m => NodePtr -> Node -> m ()
> updateNode (Ptr _ ref) = writeRef ref
\end{verbatim}