vm-handle.scm 6.39 KB
Newer Older
1
(define-module (logic guile-log guile-prolog vm vm-handle)
2 3 4 5 6 7
  #:use-module (logic guile-log)
  #:use-module (logic guile-log iso-prolog)
  #:use-module (logic guile-log guile-prolog hash)
  #:use-module (logic guile-log guile-prolog ops)
  #:use-module (logic guile-log prolog swi)
  #:use-module (compat racket misc)
8
  #:use-module (logic guile-log guile-prolog vm vm-pre)
9
  #:use-module (logic guile-log guile-prolog vm vm-var)
10 11 12 13
  #:use-module (system vm assembler)
  #:export (handle_all))

(compile-prolog-string "
14 15
%newvars needs to be variables
newv([]).
16

17
newv([[newvar,[[V,S],N,F]]|L]) :- !,
18 19
   (N==1 -> true ; S=#t).

20
newv([_|L]) :- newv(L).
21

22
handle_all(L,Lout) :-
23
  newv(L),
24
  handle_all(L,0,II,Lout,[]).
25 26 27

handle_all([],I,I,L,L).
handle_all([X|Y],I,II,L,LL) :-
28
  handle(X,I,I1,L,L1),!,
29 30 31 32 33 34
  handle_all(Y,I1,II,L1,LL).
 
-extended.
handle([label,N],I,II,L,LL) :- !,
 N=I,I=II,LL=L.

35 36
handle((X,[('goto-inst';'store-state'   ;'unwind-tail';softie;'softie-light';
            'post-q'   ;'newframe-light';'unwind-light-tail'),N]),
37 38 39 40
         I,II,L,LL) :- !,
 II is I + 2,
 L=[X|LL].

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
41
handle((X,[(newframe;'newframe-negation';'post-negation'
42
                    ;'unwind'           ;'unwind-negation'
43
                    ;'post-s'           ;'unwind-light'),A,B]),
44 45 46 47 48 49 50 51
        I,II,L,LL) :- !,
 II is I + 3, 
 L=[X|LL].

handle([label,N,[_,Tags]],I,II,L,LL) :- !,
  N=I,
  addvs(Tags,I,II,L,LL).

52 53 54 55 56 57
handle((X,['post-call',A,P]),I,II,L,LL) :- !,
  II is I + 2, L=[X|LL].

handle((X,['post-unicall',A,P]),I,II,L,LL) :- !,
  get_nsvars(P,N),
  set(P,N),
58
  II is I + 3, L=[X|LL].
59
  
60
handle(['newvar', _],I,I,L,L) :- !.
61

62
handle(['pre-unify',At,Vx],I,II,L,LL) :- !,
63
 Vx=[[S,V,Q],N,F|_],
64 65
 (  
   N==1 -> throw(begin_with_no_end) ;
66 67 68 69 70 71 72 73 74 75
   isCplx(At) -> 
      (
        new_var(V,Q,S),
        L=[['pre-unify',V]|LL],
        II is I + 2
      ) ;
      (
        L=LL,
        I=II
      )
76 77
 ).

78
handle(['post-unify-tail',[[S,V,Q],N,F|_]],I,II,L,LL) :- 
79
 !,
80 81 82
 (
   F==#t -> throw(end_with_no_begin) ;
   (
83
     new_var(V,Q,S),
84
     L=[['post-unify-tail',V]|LL],
85 86 87 88
     II is I + 2
   )
 ).

89 90 91 92 93 94 95 96 97 98 99 100
handle(['post-unify',[[S,V,Q],N,F|_]],I,II,L,LL) :- 
 !,
 (
   F==#t -> throw(end_with_no_begin) ;
   (
     (get_nsvars(Q,M) -> true ; M=0),
     new_var(V,Q,S),
     L=[['post-unify',V,M]|LL],
     II is I + 3
   )
 ).

101 102 103 104 105 106 107
handle([set,(W,[[S,V,Q],N,F|_])],I,II,L,LL) :- !,
  (
    (F=#t,N=1) -> (L=LL, I=II)                                ;
     S=#t      -> (new_var(V,Q,S),L=[[set,V]|LL],II is I + 2) ;
                  handle([unify,W,#t],I,II,L,LL)
  ).

108
handle(['push-variable',[[S,V,Q],N,F|_]],I,II,L,LL) :- !,
109 110
  (
    (F==#t,N==1) -> (L=[[pushv,#f]|LL]                     , II is I + 2);
111 112
    F==#t        -> (new_var(V,Q,S), L=[[pushv,V]|LL]          , II is I + 2);
    (new_var(V,Q,S), L=[['push-variable',V,0]|LL], II is I + 3)
113 114 115
  ).


116
handle(['push-variable-scm',[[S,V,Q],N,F|_]],I,II,L,LL) :- !,
117
  (
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
118 119 120 121
     F==#t        ->  
       throw(first_variable_in_scheme_context);
       (L=[['push-variable-scm',V]|LL], II is I + 2)
  ).
122

123
handle([unify,[[S,V,Q],N,F|_],M],I,II,L,LL) :- !,
124 125 126
    (
      (F==#t,N==1) ->
        (
127
          (M==#t ; M==0) -> (tr(pop,Pop),L=[[Pop,1]|LL],II is I + 2) ;
128 129 130 131 132 133
          (L=[[false]|LL] , II is I + 1)
        );
      (F==#t,S==#t) ->
        (
          M=#f -> (L=[[false]|LL], II is I + 1) ;
          (
134
            new_var(V,Q,S),
135
            L=[[unify,M,V,#t]|LL],
136
            II is I + 4
137 138 139 140 141 142
          )
        );
      F==#t ->
        (
          M=#f -> (L=[[false]|LL], II is I + 1) ;
          (
143
            new_var(V,Q,S),
144
            L=[[unify,M,V,#f]|LL],
145
            II is I + 4
146 147 148 149 150 151 152 153 154 155 156 157 158 159
          )
        );
      N==1 ->
      (
        L=[[unify,M,V,1]|LL],
        II is I + 4
      ) ;
      (
        L=[[unify,M,V,0]|LL],
        II is I + 4      
      )
   ).

 
160
handle([U,[[S,V,Q],N,F|_],A,M],I,II,L,LL) :- !,
161 162 163 164 165 166 167
 ((U='unify-constant-2' ; U='unify-instruction-2') ->
  ( 
    regconst(A,XX),
    (
     (F==#t,N==1)  -> (L=LL,II is I) ;
     (F==#t,S==#t) ->
      (
168
        new_var(V,Q,S),
169 170 171 172 173
        L=[[U,M,XX,V,#t]|LL],
        II is I + 5
      );
      F==#t -> 
      (
174
        new_var(V,Q,S),
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        L=[[U,M,XX,V,#f]|LL],
        II is I + 5
      ) ;
      N==1 ->
      (
       L=[[U,M,XX,V,1]|LL],
       II is I + 5
      ) ;        
      (
       L=[[U,M,XX,V,0]|LL],
       II is I + 5
      )
     )        
  );
 (U='unify-2') ->
   (
191
     A=[[S2,V2,Q2],N2,F2|_],
192 193 194 195 196 197
     (
       ((F==#t,N==1) ; (F2==#t,N2==1)) -> (LL=L, S1=S) ;
       (F==#t,S==#t,F2==#t,S2==#t) ->
          (
            M=#f -> (L=[[false]|LL]) ;
            (
198
              new_var(V,Q,S),new_var(V2,Q2,S2),
199 200 201 202 203 204 205 206
              L=[[U,M,V,#t,V2,#t]|LL],
              II is I + 6
            )
          );
       (F==#t,S==#t,F2==#t) ->
          (
            M=#f -> (L=[[false]|LL]) ;
            (
207
              new_var(V,Q,S), new_var(V2,Q2,S2),
208 209 210 211 212 213 214 215
              L=[[U,M,V,#t,V2,#f]|LL],
              II is I + 6
            )
          );
       (F==#t,F2==#t,S2==#t) ->
          (
            M=#f -> (L=[[false]|LL]) ;
            (
216
              new_var(V,Q,S), new_var(V2,Q2,S2),
217 218 219 220 221 222 223 224
              L=[[U,M,V,#f,V2,#t]|LL],
              II is I + 6
            )
          );
       (F==#t,F2==#t) ->
          (
            M=#f -> (L=[[false]|LL]) ;
            (
225
              new_var(V,Q,S), new_var(V2,Q2,S2),
226 227 228 229 230 231 232 233
              L=[[U,M,V,#f,V2,#t]|LL],
              II is I + 6
            )
          );
       F==#t ->
         (
            M=#f -> (L=[[false]|LL]) ;
            (
234
              new_var(V,Q,S),
235 236 237 238 239 240 241 242 243
              (N2 == 1 -> K=1 ; K=0),
              L=[[U,M,V,#f,V2,K]|LL],
              II is I + 6
            )
         );
       F2==#t ->
         (
            M=#f -> (L=[[false]|LL]) ;
            (
244
              new_var(V2,Q2,S2),
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
              (N == 1 -> K=1 ; K=0),
              L=[[U,M,V,K,V2,#f]|LL],
              II is I + 6
            )
         );
       (
         (N  == 1 -> K1=1 ; K1=0),
         (N2 == 1 -> K2=1 ; K2=0),
         L=[[U,M,V,K1,V2,K2]|LL],
         II is I + 6
       )          
     )
   )).


handle(X,I,II,L,LL) :- !,
   (
    X=[A]   -> (U=[], II is I + 1) ;
    X=[A,N] -> 
      (regconst(N,J),U=[J],II is I+2);
    X=[A,N,M] ->
      (regconst(N,J1),regconst(M,J2),U=[J1,J2],II is I+3);
    X=[A,N,M,O] ->
      (regconst(N,J1),regconst(M,J2),regconst(O,J3),U=[J1,J2,J3],II is I+4);
    X=[A,N,M,O,P] ->
      (
        regconst(N,J1),regconst(M,J2),regconst(O,J3),regconst(P,J4),
        U=[J1,J2,J3,J4],II is I+5
      )
   ), 
   tr(A,AA),!,
276
   XX=[A|U],
277 278
   L=[XX|LL].
")