a3e32a3b908d0437ccd1b53b1ef6e48bce7436e4
[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 loadnet/1.
14 :- export resolve/2.
15 :- export toRange/2.
16
17 :- lib(ic).
18
19 :- dynamic net/2.
20
21 %% Load a precompiled decoding net
22 loadnet(Name) :-
23     concat_string(["sockeyefacts/",Name],File),
24     [File].
25
26 %% Address range in block
27 blockRange(block(Base,Limit),Range) :-
28     Range = Base..Limit.
29
30 %% Address ranges in block list
31 blockListRanges(Blocks,Ranges) :-
32     (
33         foreach(Block,Blocks),
34         fromto([],Prev,Next,Ranges)
35     do
36         blockRange(Block,Range),
37         Next = [Range|Prev]
38     ).
39     
40 %% Extract block ranges from map list
41 mapListRanges(Maps,Ranges) :-
42     (
43         foreach(map(Block,_,_),Maps),
44         fromto([],Prev,Next,Blocks)
45     do
46         Next = [Block|Prev]
47     ),
48     blockListRanges(Blocks,Ranges).
49
50 mapsToName(map(SrcBlock,Dest,DestBase),Addr,Name) :-
51     name(Dest,DestAddr) = Name,
52     blockRange(SrcBlock,Range),
53     Addr :: Range,
54     block(SrcBase,_) = SrcBlock,
55     DestAddr #= Addr - SrcBase + DestBase.
56
57 listMapsToName([M|Maps],Addr,Name) :-
58     mapsToName(M,Addr,Name);
59     listMapsToName(Maps,Addr,Name).    
60
61 translateMap(node(_,_,Translate,_),Addr,Name) :-
62     listMapsToName(Translate,Addr,Name).
63
64 translateOverlay(node(_,Accept,Translate,Overlay),Addr,Name) :-
65     not(Overlay = '@none'),
66     blockListRanges(Accept,ARanges),
67     neg(Addr :: ARanges),
68     mapListRanges(Translate,TRanges),
69     neg(Addr :: TRanges),
70     Name = name(Overlay,Addr).
71
72 translate(Node,Addr,Name) :-
73     translateMap(Node,Addr,Name);
74     translateOverlay(Node,Addr,Name).
75
76 accept(node(_,Accept,_,_),Addr) :-
77     blockListRanges(Accept,Ranges),
78     Addr :: Ranges.
79
80 acceptedName(name(NodeId,Addr)) :-
81     net(NodeId,Node),
82     accept(Node,Addr).
83
84 decodeStep(name(NodeId,Addr),Name) :-
85     net(NodeId,Node),
86     translate(Node,Addr,Name).
87
88 decodesTo(SrcName,DestName) :-
89     decodeStep(SrcName,Name),
90     (   DestName = Name
91     ;   decodesTo(Name,DestName)
92     ).
93
94 resolve(SrcName,DestName) :-
95     (   DestName = SrcName
96     ;   decodesTo(SrcName,DestName)
97     ),
98     acceptedName(DestName).
99
100 toRange(Name,Range) :-
101     name(Id,Addr) = Name,
102     get_min(Addr,Min),get_max(Addr,Max),
103     Range = (Id, Min, Max).
104