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-2020, 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,                  % :Spec
   38            clause_vm/2,                % +ClauseRef,-VM:list
   39            vmi_labels/2                % ?VMI,?Labeled
   40          ]).   41:- autoload(library(lists),[member/2]).   42:- autoload(library(prolog_clause),[predicate_name/2]).

SWI-Prolog Virtual Machine utilities

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

   51:- meta_predicate
   52    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.
   59vm_list(_:Ref) :-
   60    blob(Ref, clause),
   61    !,
   62    (   nth_clause(_Head, N, Ref),
   63        format('~40c~nclause ~d (~w):~n~40c~n', [0'-, N, Ref, 0'-]),
   64        vm_list_clause(Ref),
   65        fail
   66    ;   true
   67    ).
   68vm_list(Spec) :-
   69    '$find_predicate'(Spec, List),
   70    (   member(PI, List),
   71        pi_to_head(PI, Head),
   72        unify_args(Head, Spec),
   73        predicate_name(Head, Name),
   74        format('~72c~n~w~n~72c~n', [0'=, Name, 0'=]),
   75        (   '$fetch_vm'(Head, 0, _, _)
   76        ->  vm_list_clause(Head)
   77        ;   format('    (No supervisor)~n')
   78        ),
   79        (   nth_clause(Head, N, Ref),
   80            clause(MHead, _, Ref),
   81            same_head(Head, MHead),
   82            format('~40c~nclause ~d (~w):~n~40c~n', [0'-, N, Ref, 0'-]),
   83            vm_list_clause(Ref),
   84            fail
   85        ;   true
   86        ),
   87        fail
   88    ;   true
   89    ).
   90
   91pi_to_head(M:PI, M:Head) :-
   92    !,
   93    pi_to_head(PI, Head).
   94pi_to_head(Name/Arity, Head) :-
   95    functor(Head, Name, Arity).
   96
   97vm_list_clause(Clause) :-
   98    clause_vm(Clause, VM),
   99    vmi_labels(VM, Labeled),
  100    vm_list_labeled(Labeled, 0).
  101
  102vm_list_labeled([], _).
  103vm_list_labeled([label(L),vmi(VMI,Size)|T], PC) :-
  104    format('~w: ~t~d~8| ~q~n', [L, PC, VMI]),
  105    PC1 is PC+Size,
  106    vm_list_labeled(T, PC1).
  107vm_list_labeled([vmi(VMI,Size)|T], PC) :-
  108    format('~t~d~8| ~q~n', [PC, VMI]),
  109    PC1 is PC+Size,
  110    vm_list_labeled(T, PC1).
  111
  112%       Unify the arguments of the specification with the given term,
  113%       so we can partially instantate the head.
  114
  115unify_args(_, _/_) :- !.                % Name/arity spec
  116unify_args(X, X) :- !.
  117unify_args(_:X, X) :- !.
  118unify_args(_, _).
  119
  120same_head(X, X) :- !.
  121same_head(H1, H2) :-
  122    strip_module(H1, _, H),
  123    strip_module(H2, _, H).
 clause_vm(+ClauseRef, -VM:list) is det
True when VM is the virtual machine code of ClauseRef. Each instruction is a term vmi(VMI,Size).
  131clause_vm(Ref, VM) :-
  132    clause_vm(Ref, 0, VM).
  133
  134clause_vm(Clause, PC, [vmi(VMI,Size)|T]) :-
  135    '$fetch_vm'(Clause, PC, NextPC, VMI),
  136    !,
  137    Size is NextPC-PC,
  138    clause_vm(Clause, NextPC, T).
  139clause_vm(_, _, []).
 vmi_labels(?VMI, ?Labeled)
Translated between a raw and a labeled representation for a VMI sequence as produced by clause_vm/2. Assumes we only jump forwards.

In the labeled represention the jump arguments of VMIs are label names and there are entries label(Name) in the list.

  151vmi_labels(VMI, Labeled) :-
  152    nonvar(VMI),
  153    !,
  154    label_vmi(VMI, 0, 0, [], Labeled).
  155vmi_labels(VMI, Labeled) :-
  156    unlabel_vmi(Labeled, 0, [], VMI).
  157
  158% Raw --> Labeled
  159
  160label_vmi([], _, _, _, []).
  161label_vmi([H|T], Here0, LI0, Pending0, Labeled) :-
  162    H = vmi(VMI0,Size),
  163    Here is Here0+Size,
  164    new_labels(VMI0, VMI, LI0, LI1, Here, Pending0, Pending1),
  165    (   selectchk(Label-Here0, Pending1, Pending2)
  166    ->  Labeled = [label(Label),vmi(VMI,Size)|Labeled1]
  167    ;   Labeled = [vmi(VMI,Size)|Labeled1],
  168        Pending2 = Pending1
  169    ),
  170    label_vmi(T, Here, LI1, Pending2, Labeled1).
  171
  172new_labels(VMI0, VMI, LI0, LI, End, Labels0, Labels) :-
  173    VMI0 =.. [Name|Argv0],
  174    '$vmi_property'(Name, argv(ArgvTypes)),
  175    new_labels_(ArgvTypes, Argv0, Argv, LI0, LI, End, Labels0, Labels),
  176    VMI =.. [Name|Argv].
  177
  178new_labels_([], [], [], LI, LI, _, Labels, Labels).
  179new_labels_([jump|TT], [Offset|AT], [Label|LT], LI0, LI, End, Labels0, Labels) :-
  180    !,
  181    To is End+Offset,
  182    (   memberchk(Label-To, Labels0)
  183    ->  Labels1 = Labels0,
  184        LI1 = LI0
  185    ;   LI1 is LI0+1,
  186        atom_concat('L', LI1, Label),
  187        Labels1 = [Label-To|Labels0]
  188    ),
  189    new_labels_(TT, AT, LT, LI1, LI, End, Labels1, Labels).
  190new_labels_([_|TT], [A|AT], [A|LT], LI0, LI, End, Labels0, Labels) :-
  191    new_labels_(TT, AT, LT, LI0, LI, End, Labels0, Labels).
  192
  193% Labeled --> Raw
  194
  195unlabel_vmi([], _, _, []).
  196unlabel_vmi([label(L)|T0], Here, Labels0, T) :-
  197    !,
  198    resolve_labels(L, Here, Labels0, Labels),
  199    unlabel_vmi(T0, Here, Labels, T).
  200unlabel_vmi([vmi(VMI0,Size)|T0], Here0, Labels0, [vmi(VMI,Size)|T]) :-
  201    Here is Here0+Size,
  202    get_labels(VMI0, VMI, Here, Labels0, Labels),
  203    unlabel_vmi(T0, Here, Labels, T).
  204
  205get_labels(VMI0, VMI, Here, Labels0, Labels) :-
  206    VMI0 =.. [Name|Argv0],
  207    '$vmi_property'(Name, argv(ArgvTypes)),
  208    get_labels_(ArgvTypes, Argv0, Argv, Here, Labels0, Labels),
  209    VMI =.. [Name|Argv].
  210
  211get_labels_([], [], [], _, Labels, Labels).
  212get_labels_([jump|TT], [Label|LT], [Offset|AT], Here,
  213            Labels0, [l(Label,Here,Offset)|Labels]) :-
  214    !,
  215    get_labels_(TT, LT, AT, Here, Labels0, Labels).
  216get_labels_([_|TT], [A|LT], [A|AT], Here, Labels0, Labels) :-
  217    get_labels_(TT, LT, AT, Here, Labels0, Labels).
  218
  219resolve_labels(L, Here, Labels0, Labels) :-
  220    selectchk(l(L,End,Offset), Labels0, Labels1),
  221    !,
  222    Offset is Here-End,
  223    resolve_labels(L, Here, Labels1, Labels).
  224resolve_labels(_, _, Labels, Labels)