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