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