Move Prolog predicates to usr/skb/programs
[barrelfish] / usr / skb / programs / decodingNet.pl
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 % Copyright (c) 2017, ETH Zurich.
3 % All rights reserved.
4 %
5 % This file is distributed under the terms in the attached LICENSE file.
6 % If you do not find this file, copies can be found by writing to:
7 % ETH Zurich D-INFK, Universitaetsstrasse 6, CH-8092 Zurich.
8 % Attn: Systems Group.
9 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10
11 :- module(decodingNet).
12 :- export net/2.
13 :- export resolve/2.
14 :- export findRanges/4.
15
16 :- lib(ic).
17
18 :- dynamic net/2.
19
20 %% Address range in block
21 blockRange(block(Base,Limit),Range) :-
22     Range = Base..Limit.
23
24 %% Address ranges in block list
25 blockListRanges(Blocks,Ranges) :-
26     (
27         foreach(Block,Blocks),
28         fromto([],Prev,Next,Ranges)
29     do
30         blockRange(Block,Range),
31         Next = [Range|Prev]
32     ).
33     
34 %% Extract block ranges from map list
35 mapListRanges(Maps,Ranges) :-
36     (
37         foreach(map(Block,_,_),Maps),
38         fromto([],Prev,Next,Blocks)
39     do
40         Next = [Block|Prev]
41     ),
42     blockListRanges(Blocks,Ranges).
43
44 mapsToName(map(SrcBlock,Dest,DestBase),Addr,Name) :-
45     name(Dest,DestAddr) = Name,
46     blockRange(SrcBlock,Range),
47     Addr :: Range,
48     block(SrcBase,_) = SrcBlock,
49     DestAddr #= Addr - SrcBase + DestBase.
50
51 listMapsToName([M|Maps],Addr,Name) :-
52     mapsToName(M,Addr,Name);
53     listMapsToName(Maps,Addr,Name).    
54
55 translateMap(node(_,Translate,_),Addr,Name) :-
56     listMapsToName(Translate,Addr,Name).
57
58 translateOverlay(node(Accept,Translate,Overlay),Addr,Name) :-
59     not(Overlay = '@none'),
60     blockListRanges(Accept,ARanges),
61     neg(Addr :: ARanges),
62     mapListRanges(Translate,TRanges),
63     neg(Addr :: TRanges),
64     Name = name(Overlay,Addr).
65
66 translate(Node,Addr,Name) :-
67     translateMap(Node,Addr,Name);
68     translateOverlay(Node,Addr,Name).
69
70 accept(node(Accept,_,_),Addr) :-
71     blockListRanges(Accept,Ranges),
72     Addr :: Ranges.
73
74 acceptedName(name(NodeId,Addr)) :-
75     net(NodeId,Node),
76     accept(Node,Addr).
77
78 decodeStep(name(NodeId,Addr),Name) :-
79     net(NodeId,Node),
80     translate(Node,Addr,Name).
81
82 decodesTo(SrcName,DestName) :-
83     decodeStep(SrcName,Name),
84     (   DestName = Name
85     ;   decodesTo(Name,DestName)
86     ).
87
88 resolve(SrcName,DestName) :-
89     (   DestName = SrcName
90     ;   decodesTo(SrcName,DestName)
91     ),
92     acceptedName(DestName).
93
94 findTargets(SrcName,DestName) :-
95     resolve(SrcName,DestName),
96     name(_,Addr) = DestName,
97     labeling([Addr]).
98     
99
100 findOrigins(SrcName,DestName) :-
101     resolve(SrcName,DestName),
102     name(_,Addr) = SrcName,
103     labeling([Addr]).
104
105 findRanges(SrcName,DestName,SrcRange,DestRange) :-
106     resolve(SrcName,DestName),
107     name(SrcId,SrcAddr) = SrcName,
108     name(DestId,DestAddr) = DestName,
109     get_min(SrcAddr,SrcMin),get_max(SrcAddr,SrcMax),
110     get_min(DestAddr,DestMin),get_max(DestAddr,DestMax),
111     SrcRange = (SrcId, SrcMin, SrcMax),
112     DestRange = (DestId, DestMin, DestMax).
113