View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           www.swi-prolog.org
    6    Copyright (c)  2008-2015, University of Amsterdam
    7                              VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(prolog_vm,
   37          [ vm_list/1
   38          ]).   39:- use_module(library(prolog_clause)).   40:- use_module(library(lists)).

SWI-Prolog Virtual Machine utilities

This is an internal developers module to manage the virtual machine instructions. */

   48:- meta_predicate
   49    vm_list(:).
 vm_list(:Spec) is det
Lists the definition of the predicates matching Spec to current_output. Spec is also allowed to be a clause-reference.
   56vm_list(_:Ref) :-
   57    blob(Ref, clause),
   58    !,
   59    (   nth_clause(_Head, N, Ref),
   60        format('~40c~nclause ~d (~w):~n~40c~n', [0'-, N, Ref, 0'-]),
   61        vm_list_clause(Ref),
   62        fail
   63    ;   true
   64    ).
   65vm_list(Spec) :-
   66    '$find_predicate'(Spec, List),
   67    (   member(PI, List),
   68        pi_to_head(PI, Head),
   69        unify_args(Head, Spec),
   70        predicate_name(Head, Name),
   71        format('~72c~n~w~n~72c~n', [0'=, Name, 0'=]),
   72        (   '$fetch_vm'(Head, 0, _, _)
   73        ->  vm_list_clause(Head)
   74        ;   format('    (No supervisor)~n')
   75        ),
   76        (   nth_clause(Head, N, Ref),
   77            clause(MHead, _, Ref),
   78            same_head(Head, MHead),
   79            format('~40c~nclause ~d (~w):~n~40c~n', [0'-, N, Ref, 0'-]),
   80            vm_list_clause(Ref),
   81            fail
   82        ;   true
   83        ),
   84        fail
   85    ;   true
   86    ).
   87
   88pi_to_head(M:PI, M:Head) :-
   89    !,
   90    pi_to_head(PI, Head).
   91pi_to_head(Name/Arity, Head) :-
   92    functor(Head, Name, Arity).
   93
   94vm_list_clause(Clause) :-
   95    vm_list_clause(Clause, 0).
   96
   97vm_list_clause(Clause, PC) :-
   98    '$fetch_vm'(Clause, PC, NextPC, VMI),
   99    !,
  100    format('~t~d~4| ~q~n', [PC, VMI]),
  101    vm_list_clause(Clause, NextPC).
  102vm_list_clause(_, _).
  103
  104%       Unify the arguments of the specification with the given term,
  105%       so we can partially instantate the head.
  106
  107unify_args(_, _/_) :- !.                % Name/arity spec
  108unify_args(X, X) :- !.
  109unify_args(_:X, X) :- !.
  110unify_args(_, _).
  111
  112same_head(X, X) :- !.
  113same_head(H1, H2) :-
  114    strip_module(H1, _, H),
  115    strip_module(H2, _, H)