File: vm.pl

package info (click to toggle)
swi-prolog 9.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 82,408 kB
  • sloc: ansic: 387,503; perl: 359,326; cpp: 6,613; lisp: 6,247; java: 5,540; sh: 3,147; javascript: 2,668; python: 1,900; ruby: 1,594; yacc: 845; makefile: 428; xml: 317; sed: 12; sql: 6
file content (251 lines) | stat: -rw-r--r-- 8,125 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@vu.nl
    WWW:           www.swi-prolog.org
    Copyright (c)  2008-2020, University of Amsterdam
                              VU University Amsterdam
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in
       the documentation and/or other materials provided with the
       distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

:- module(prolog_vm,
          [ vm_list/1,                  % :Spec
            clause_vm/2,                % +ClauseRef,-VM:list
            vmi_labels/2                % ?VMI,?Labeled
          ]).
:- autoload(library(lists),[member/2]).
:- autoload(library(prolog_clause),[predicate_name/2]).


/** <module> SWI-Prolog Virtual Machine utilities

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

:- meta_predicate
    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.

vm_list(_:Ref) :-
    blob(Ref, clause),
    !,
    (   nth_clause(_Head, N, Ref),
        format('~40c~nclause ~d (~w):~n~40c~n', [0'-, N, Ref, 0'-]),
        vm_list_clause(Ref),
        fail
    ;   true
    ).
vm_list(Spec) :-
    '$find_predicate'(Spec, List),
    (   member(PI, List),
        pi_to_head(PI, Head),
        unify_args(Head, Spec),
        predicate_name(Head, Name),
        format('~72c~n~w~n~72c~n', [0'=, Name, 0'=]),
        (   '$fetch_vm'(Head, 0, _, _)
        ->  vm_list_clause(Head)
        ;   format('    (No supervisor)~n')
        ),
        (   nth_clause(Head, N, Ref),
            clause(MHead, _, Ref),
            same_head(Head, MHead),
            format('~40c~nclause ~d (~w):~n~40c~n', [0'-, N, Ref, 0'-]),
            vm_list_clause(Ref),
            fail
        ;   true
        ),
        fail
    ;   true
    ).

pi_to_head(M:PI, M:Head) :-
    !,
    pi_to_head(PI, Head).
pi_to_head(Name/Arity, Head) :-
    functor(Head, Name, Arity).

vm_list_clause(Clause) :-
    clause_vm(Clause, VM),
    vmi_labels(VM, Labeled),
    vm_list_labeled(Labeled, 0).

vm_list_labeled([], _).
vm_list_labeled([label(L),vmi(break(VMI),Size)|T], PC) :-
    !,
    format('~w: ~t~d~8| ~q % <breakpoint>~n', [L, PC, VMI]),
    PC1 is PC+Size,
    vm_list_labeled(T, PC1).
vm_list_labeled([label(L),vmi(VMI,Size)|T], PC) :-
    format('~w: ~t~d~8| ~q~n', [L, PC, VMI]),
    PC1 is PC+Size,
    vm_list_labeled(T, PC1).
vm_list_labeled([vmi(break(VMI),Size)|T], PC) :-
    !,
    format('~t~d~8| ~q % <breakpoint>~n', [PC, VMI]),
    PC1 is PC+Size,
    vm_list_labeled(T, PC1).
vm_list_labeled([vmi(VMI,Size)|T], PC) :-
    format('~t~d~8| ~q~n', [PC, VMI]),
    PC1 is PC+Size,
    vm_list_labeled(T, PC1).

%       Unify the arguments of the specification with the given term,
%       so we can partially instantate the head.

unify_args(_, _/_) :- !.                % Name/arity spec
unify_args(X, X) :- !.
unify_args(_:X, X) :- !.
unify_args(_, _).

same_head(X, X) :- !.
same_head(H1, H2) :-
    strip_module(H1, _, H),
    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).

clause_vm(Ref, VM) :-
    clause_vm(Ref, 0, VM).

clause_vm(Clause, PC, [vmi(VMI,Size)|T]) :-
    '$fetch_vm'(Clause, PC, NextPC, VMI),
    !,
    Size is NextPC-PC,
    clause_vm(Clause, NextPC, T).
clause_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.

vmi_labels(VMI, Labeled) :-
    nonvar(VMI),
    !,
    label_vmi(VMI, 0, 0, [], Labeled).
vmi_labels(VMI, Labeled) :-
    unlabel_vmi(Labeled, 0, [], VMI).

% Raw --> Labeled

label_vmi([], _, _, _, []).
label_vmi([H|T], Here0, LI0, Pending0, Labeled) :-
    H = vmi(VMI0,Size),
    Here is Here0+Size,
    new_labels(VMI0, VMI, LI0, LI1, Here0, Here, Pending0, Pending1),
    (   selectchk(Label-Here0, Pending1, Pending2)
    ->  Labeled = [label(Label),vmi(VMI,Size)|Labeled1]
    ;   Labeled = [vmi(VMI,Size)|Labeled1],
        Pending2 = Pending1
    ),
    label_vmi(T, Here, LI1, Pending2, Labeled1).

new_labels(break(VMI0), break(VMI), LI0, LI, Start, End, Labels0, Labels) :-
    !,
    new_labels(VMI0, VMI, LI0, LI, Start, End, Labels0, Labels).
new_labels(VMI0, VMI, LI0, LI, Start, End, Labels0, Labels) :-
    VMI0 =.. [Name|Argv0],
    '$vmi_property'(Name, argv(ArgvTypes)),
    jmp_rel(Name, Start, End, Rel),
    new_labels_(ArgvTypes, Argv0, Argv, LI0, LI, Rel, Labels0, Labels),
    VMI =.. [Name|Argv].

%!  jmp_rel(+VMIName, +Start, +End, -JmpRel)
%
%   Relative position for the (choice) jump.  This   is  the  end of the
%   instruction for most, but after the   address  for the compiled trie
%   instructions.   Should be made consistent.

jmp_rel(TrieVMI, Start, _End, Rel) :-
    trie_vmi(TrieVMI), !,
    Rel is Start+2.
jmp_rel(_, _, End, End).

trie_vmi(VMI) :- sub_atom(VMI, 0, _, _, t_).

new_labels_([], [], [], LI, LI, _, Labels, Labels).
new_labels_([jump|TT], [Offset|AT], [Label|LT], LI0, LI, End, Labels0, Labels) :-
    !,
    To is End+Offset,
    (   memberchk(Label-To, Labels0)
    ->  Labels1 = Labels0,
        LI1 = LI0
    ;   LI1 is LI0+1,
        atom_concat('L', LI1, Label),
        Labels1 = [Label-To|Labels0]
    ),
    new_labels_(TT, AT, LT, LI1, LI, End, Labels1, Labels).
new_labels_([_|TT], [A|AT], [A|LT], LI0, LI, End, Labels0, Labels) :-
    new_labels_(TT, AT, LT, LI0, LI, End, Labels0, Labels).

% Labeled --> Raw

unlabel_vmi([], _, _, []).
unlabel_vmi([label(L)|T0], Here, Labels0, T) :-
    !,
    resolve_labels(L, Here, Labels0, Labels),
    unlabel_vmi(T0, Here, Labels, T).
unlabel_vmi([vmi(VMI0,Size)|T0], Here0, Labels0, [vmi(VMI,Size)|T]) :-
    Here is Here0+Size,
    get_labels(VMI0, VMI, Here, Labels0, Labels),
    unlabel_vmi(T0, Here, Labels, T).

get_labels(VMI0, VMI, Here, Labels0, Labels) :-
    VMI0 =.. [Name|Argv0],
    '$vmi_property'(Name, argv(ArgvTypes)),
    get_labels_(ArgvTypes, Argv0, Argv, Here, Labels0, Labels),
    VMI =.. [Name|Argv].

get_labels_([], [], [], _, Labels, Labels).
get_labels_([jump|TT], [Label|LT], [Offset|AT], Here,
            Labels0, [l(Label,Here,Offset)|Labels]) :-
    !,
    get_labels_(TT, LT, AT, Here, Labels0, Labels).
get_labels_([_|TT], [A|LT], [A|AT], Here, Labels0, Labels) :-
    get_labels_(TT, LT, AT, Here, Labels0, Labels).

resolve_labels(L, Here, Labels0, Labels) :-
    selectchk(l(L,End,Offset), Labels0, Labels1),
    !,
    Offset is Here-End,
    resolve_labels(L, Here, Labels1, Labels).
resolve_labels(_, _, Labels, Labels).