Decoding net: Refactor
authorDaniel Schwyn <schwyda@student.ethz.ch>
Fri, 15 Sep 2017 12:08:16 +0000 (14:08 +0200)
committerDaniel Schwyn <schwyda@student.ethz.ch>
Fri, 15 Sep 2017 12:08:16 +0000 (14:08 +0200)
Signed-off-by: Daniel Schwyn <schwyda@student.ethz.ch>

usr/skb/programs/decoding_net.pl
usr/skb/programs/decoding_net_queries.pl
usr/skb/programs/decoding_net_skb.pl [deleted file]

index da11809..2c565ac 100644 (file)
@@ -10,8 +10,8 @@
 
 :- module(decoding_net).
 :- export load_net/1.
+:- export decodes_to/2.
 :- export resolve/2.
-:- export to_region/2.
 :- export node/2.
 
 :- dynamic node/2.
 :- set_flag(syntax_option,iso_base_prefix).
 
 %% Load a precompiled decoding net
-load_net(File) :- [File].
+load_net(File) :-
+    ensure_loaded(File).
+
+%% Convert from regions to names
+to_name(Region,Name) :-
+    region{
+        node_id:Id,
+        base:Base,
+        size:Size
+    } = Region,
+    Addr #>= Base,
+    Addr #< Base + Size,
+    Name = name{
+        node_id:Id,
+        address:Addr
+    }.
+
+%% Convert from names to regions
+to_region(Name,Region) :-
+    name{
+        node_id:Id,
+        address:Addr
+    } = Name,
+    get_min(Addr,Min),get_max(Addr,Max),
+    Size is Max - Min + 1,
+    Region = region{
+        node_id:Id,
+        base:Min,
+        size:Size
+    }.
 
 %% Address range in block
 block_range(Block,Range) :-
@@ -122,19 +151,15 @@ decodes_to(SrcName,DestName) :-
     decodes_to(Name,DestName).
 
 resolve(SrcName,DestName) :-
+    name{} = SrcName,
+    name{} = DestName,
     decodes_to(SrcName,DestName),
     accepted_name(DestName).
 
-to_region(Name,Region) :-
-    name{
-        node_id:Id,
-        address:Addr
-    } = Name,
-    get_min(Addr,Min),get_max(Addr,Max),
-    Size is Max - Min + 1,
-    Region = region{
-        node_id:Id,
-        base:Min,
-        size:Size
-    }.
+resolve(SrcRegion,DestRegion) :-
+    to_name(SrcRegion,SrcName),
+    to_name(DestRegion,DestName),
+    resolve(SrcName,DestName),
+    to_region(SrcName,SrcRegion),
+    to_region(DestName,DestRegion).
   
\ No newline at end of file
index edef76a..114c66e 100644 (file)
@@ -9,87 +9,38 @@
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 :- module(decoding_net_queries).
-:- export find_target_region/2.
-:- export find_origin_region/2.
-:- export find_device_region/3.
-:- export find_memory_region/3.
+:- export find_device_region/2.
+:- export find_memory_region/2.
 :- export find_shared_memory_region/3.
-:- export find_device_id/3.
-:- export find_interrupt_line/3.
 
 :- use_module(decoding_net).
 
 
-%%%%%%%%%%%%%%%%%%%%%%%
-%% Helper predicates %%
-%%%%%%%%%%%%%%%%%%%%%%%
-resolve_to_region(SrcName,DestName,SrcRegion,DestRegion) :-
-    resolve(SrcName,DestName),
-    to_region(SrcName,SrcRegion),
-    to_region(DestName,DestRegion).
-
-
 %%%%%%%%%%%%%
 %% Queries %%
 %%%%%%%%%%%%%
-find_target_region(NodeId,Result) :-
-    SrcName = name{node_id:NodeId},
-    resolve_to_region(SrcName,_,SrcRegion,DestRegion),
-    Result = (SrcRegion,DestRegion).
-
-find_origin_region(NodeId,Result) :-
-    DestName = name{node_id:NodeId},
-    resolve_to_region(_,DestName,SrcRegion,DestRegion),
-    Result = (SrcRegion,DestRegion).
-
 %% Address space queries
-find_device_region(NodeId,DeviceId,Result) :-
-    SrcName = name{node_id:NodeId},
-    DestName = name{node_id:DeviceId},
+find_device_region(SrcRegion,DestRegion) :-
+    region{node_id:DeviceId} = DestRegion,
     node{
         id:DeviceId,
         type:device
     },
-    resolve_to_region(SrcName,DestName,SrcRegion,DestRegion),
-    Result = (SrcRegion,DestRegion).
+    resolve(SrcRegion,DestRegion).
 
-find_memory_region(NodeId,MemoryId,Result) :-
-    SrcName = name{node_id:NodeId},
-    DestName = name{node_id:MemoryId},
+find_memory_region(SrcRegion,DestRegion) :-
+    region{node_id:MemoryId} = DestRegion,
     node{
         id:MemoryId,
         type:memory
     },
-    resolve_to_region(SrcName,DestName,SrcRegion,DestRegion),
-    Result = (SrcRegion,DestRegion).
+    resolve(SrcRegion,DestRegion).
 
-find_shared_memory_region(NodeId,DeviceId,Result) :-
-    NodeName = name{node_id:NodeId},
-    DeviceName = name{node_id:DeviceId},
-    SharedName = name{node_id:SharedId},
+find_shared_memory_region(SrcRegion,DestRegion,SharedRegion) :-
+    region{node_id:SharedId} = SharedRegion,
     node{
         id:SharedId,
         type:memory
     },
-    resolve(NodeName,SharedName),
-    resolve(DeviceName,SharedName),
-    to_region(NodeName,NodeRegion),
-    to_region(SharedName,SharedRegion),
-    to_region(DeviceName,DeviceRegion),
-    Result = (NodeRegion,DeviceRegion,SharedRegion).
-
-find_device_id(NodeId,Addr,Result) :-
-    SrcName = name{
-        node_id:NodeId,
-        address:Addr
-    },
-    DestName = name{node_id:DeviceId},
-    resolve(SrcName,DestName),
-    Result = DeviceId.
-
-%% Interrupt queries
-find_interrupt_line(NodeId,DeviceId,Result) :-
-    SrcName = name{node_id:DeviceId},
-    DestName = name{node_id:NodeId},
-    resolve_to_region(SrcName,DestName,SrcRegion,DestRegion),
-    Result = (SrcRegion,DestRegion).
\ No newline at end of file
+    resolve(SrcRegion,SharedRegion),
+    resolve(DestRegion,SharedRegion).
diff --git a/usr/skb/programs/decoding_net_skb.pl b/usr/skb/programs/decoding_net_skb.pl
deleted file mode 100644 (file)
index 0507d90..0000000
+++ /dev/null
@@ -1,93 +0,0 @@
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% Copyright (c) 2017, ETH Zurich.
-% All rights reserved.
-%
-% This file is distributed under the terms in the attached LICENSE file.
-% If you do not find this file, copies can be found by writing to:
-% ETH Zurich D-INFK, Universitaetsstrasse 6, CH-8092 Zurich.
-% Attn: Systems Group.
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
-:- use_module(decoding_net).
-:- use_module(decoding_net_queries).
-:- local load_net/1.
-
-%%%%%%%%%%%%%%
-%% Printing %%
-%%%%%%%%%%%%%%
-print_node_id(NodeId) :-
-    node_id{
-        name:Name,
-        namespace:Namespace
-    } = NodeId,
-    reverse([Name|Namespace], IdList),
-    join_string(IdList,".",String),
-    write(String).
-
-print_region(Region) :-
-    region{
-        node_id:Id,
-        base:Addr,
-        size:Size
-    } = Region,
-    End is Addr + Size - 1,
-    print_node_id(Id),
-    printf(" [0x%16R..0x%16R]",
-        [ Addr,End ]
-    ).
-
-print_src_dest_regions((SrcRegion,DestRegion)) :-
-    print_region(SrcRegion),
-    write(" -> "),
-    print_region(DestRegion),
-    writeln("").
-
-print_shared_regions((Region1,Region2,SharedRegion)) :-
-    print_region(Region1),
-    write(" -> "),
-    print_region(SharedRegion),
-    write(" <- "),
-    print_region(Region2),
-    writeln("").
-
-
-%%%%%%%%%%%%%%%%%%%%%%%
-%% Helper predicates %%
-%%%%%%%%%%%%%%%%%%%%%%%
-load_net(Name) :- 
-    concat_string(["sockeyefacts/",Name],File),
-    decoding_net:load_net(File).
-
-all(Pred) :- findall(_,Pred,_).
-
-
-%%%%%%%%%%%%%
-%% Queries %%
-%%%%%%%%%%%%%
-find_target_region(NodeId) :-
-    find_target_region(NodeId,Result),
-    print_src_dest_regions(Result).
-
-find_origin_region(NodeId) :-
-    find_origin_region(NodeId,Result),
-    print_src_dest_regions(Result).
-
-find_device_region(NodeId,DeviceId) :-
-    find_device_region(NodeId,DeviceId,Result),
-    print_src_dest_regions(Result).
-
-find_memory_region(NodeId,MemoryId) :-
-    find_memory_region(NodeId,MemoryId,Result),
-    print_src_dest_regions(Result).
-
-find_shared_memory_region(NodeId,DeviceId) :-
-    find_shared_memory_region(NodeId,DeviceId,Result),
-    print_shared_regions(Result).
-
-find_device_id(NodeId,Addr) :-
-    find_device_id(NodeId,Addr,Result),
-    writeln(Result).
-
-find_interrupt_line(NodeId,DeviceId) :-
-    find_interrupt_line(NodeId,DeviceId,Result),
-    print_src_dest_regions(Result).
\ No newline at end of file