-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwam.js
More file actions
1407 lines (1304 loc) · 51.1 KB
/
wam.js
File metadata and controls
1407 lines (1304 loc) · 51.1 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* For general documentation, see wam_compiler.pl
Some helpful diagrams:
Environment frame looks like this:
-----------
state.E ->|0 | CE |
|1 | CP |
|2 | Y0 |
|...| |
|n+1| Yn |
-----------
Choicepoint frame where we have tried something but can try 'Next' if it fails looks like this:
(There are n arguments, labelled from A0 to An-1)
-------------
state.B ->|0 | n |
|1 | A0 |
|2 | A1 |
|...| |
|n | An-1 |
|n+1| E |
|n+2| CP |
|n+3| B |
|n+4| Next |
|n+5| TR |
|n+6| H |
|n+7| B0 |
-------------
*/
var ftable = [];
var atable = ['[]']; // Reserve first atom as [].
var floats = [];
var predicates = {};
var exception = null;
/* Constants. Should be auto-generated */
var HEAP_SIZE = 131070;
var STACK_SIZE = 65535;
var TRAIL_SIZE = 1000;
var READ = 0;
var WRITE = 1;
var TAG_REF = 0; // 0x00000000
var TAG_STR = 1; // 0x08000000
var TAG_LST = 2; // 0x10000000
var TAG_INT = 3; // 0x18000000
var TAG_ATM = 4; // 0x20000000
var TAG_FLT = 5; // 0x28000000
///////////// 6 is currently unused
var TAG_EXT = 7; // Reserved!
var TAG_MASK = 7;
// 3 bits are used for the tag
// 2 bits are used for GC
// This leaves 27 for the actual value, since javascript does not have 64-bit integers
var WORD_BITS = 27;
var M_BIT = 1 << 30;
var F_BIT = 1 << 31;
var NV_MASK = M_BIT | F_BIT | (TAG_MASK << WORD_BITS);
var NIL = (TAG_ATM << WORD_BITS); // atable[0] = '[]', so NIL is 0 xor TAG_ATM, which is just TAG_ATM.
var memory = new Array(HEAP_SIZE + STACK_SIZE + TRAIL_SIZE);
var code = [255];
var register = new Array(256);
var state;
var PDL = [];
// Stack for managing cleanup handlers needed during a cut
var cleanups = [];
/* Special purpose machine registers:
P: Pointer to the next opcode to execute (in the code[] array)
CP: Continuation Pointer. Points to the next code to execute if the current environment succeeds (basically the return address when calling a function)
mode: READ or WRITE depending on whether are matching or creating an exemplar on the heap
H: Pointer to the next available heap cell
HB: Pointer to where the heap should be truncated to if we backtrack
TR: Pointer to the next available trail cell
S: Pointer to the next symbol on the heap to match when unifying
E: Pointer to the top environment frame
B: Pointer to the top choicepoint
B0: Pointer to the choicepoint to return to after backtracking over a cut (ie the choicepoint created by the most recent call opcode)
It is important to note that B and E do not point to the *next available* place to put an environment frame or choicepoint, but the *current* one.
*/
var debugging = true;
function debug_msg(msg)
{
if (debugging)
debug(msg);
}
function initialize()
{
state = {H: 0,
HB: 0,
S: 0,
P: 2,
CP: {code: bootstrap_code,
predicate: null,
offset:1}, // halt
B0: 0, // No backtrack frame
B: 0, // No backtrack frame
E: HEAP_SIZE,
TR: HEAP_SIZE + STACK_SIZE,
mode: READ,
running: true,
foreign_retry: false,
num_of_args: 0,
S: 0,
current_predicate: null};
code = bootstrap_code;
}
function abort(why)
{
debug(why);
throw why;
}
function bind(a, b)
{
if (TAG(a) == TAG_REF && (TAG(b) != TAG_REF || VAL(b) < VAL(a)))
{
memory[VAL(a)] = b;
trail(a);
}
else
{
memory[VAL(b)] = a;
trail(b);
}
}
function tidy_trail()
{
t = memory[state.B + memory[state.B] + 5];
if (t < HEAP_SIZE + STACK_SIZE)
abort("Backtrack pointer " + state.B + " has garbage for TR: " + hex(t));
while (t < state.TR)
{
if ((memory[t] < state.HB) || (state.H < memory[t] && memory[t] < state.B))
{
// This trailing is still required
t = t + 1;
}
else
{
memory[t] = memory[state.TR - 1];
state.TR = state.TR - 1;
}
}
}
function trail(v)
{
if (v < state.HB || (state.H < v && v < state.B))
{
debug_msg("Trailing " + v);
memory[state.TR++] = v;
}
else
{
debug_msg("NOT Trailing " + v + " because neither v < " + state.HB + " nor " + state.H + " < v < " + state.B);
}
}
function unwind_trail(from, to)
{
debug_msg("unwinding trail from " + from + " to " + to);
for (var i = from; i < to; i++)
{
memory[memory[i]] = memory[i] ^ (TAG_REF << WORD_BITS);
}
}
// Returns boolean
function unify(a, b)
{
PDL.push(a);
PDL.push(b);
var failed = false;
while (PDL.length != 0 && !failed)
{
var d1 = deref(PDL.pop());
var d2 = deref(PDL.pop());
// if d1 == d2 then just proceed with the rest of the PDL. Otherwise we need to try and unify them, or fail
if (d1 != d2)
{
type1 = TAG(d1);
val1 = VAL(d1);
type2 = TAG(d2);
val2 = VAL(d2);
if (type1 == TAG_REF)
{
bind(d1, d2);
}
else
{
switch(type2)
{
case TAG_REF:
bind(d1, d2);
break;
case TAG_ATM:
case TAG_INT:
failed = true;
break;
case TAG_FLT:
if (type1 == TAG_FLT)
{
debug(floats[val1] + " vs " + floats[val2]);
}
failed = true;
break;
case TAG_LST:
if (type1 == TAG_LST)
{
PDL.push(memory[val1]); // unify heads
PDL.push(memory[val2]);
PDL.push(memory[val1+1]); // unify tails
PDL.push(memory[val2+1]);
}
else
failed = true; // list and non-list
break;
case TAG_STR:
if (type1 == TAG_STR)
{
f1 = VAL(memory[val1]);
f2 = VAL(memory[val2]);
if (f1 == f2)
{
for (var i = 0; i < ftable[f1][1]; i++)
{
PDL.push(val1 + 1 + i);
PDL.push(val2 + 1 + i);
}
}
else
failed = true; // different functors
}
else
failed = true; // str and atom/list
}
}
}
}
return !failed;
}
function deref(p)
{
while(TAG(p) == TAG_REF && VAL(p) != memory[VAL(p)])
{
q = memory[VAL(p)];
if (q === undefined) // FIXME: Check that q =< p?
{
debug_msg("Illegal memory access in deref: " + hex(p) + ". Dumping...");
abort("Bad memory access: @" + p);
}
else
p = q;
}
return p;
}
function explicit_deref(p)
{
while(TAG(p) == TAG_REF && VAL(p) != memory[VAL(p)])
{
q = memory[VAL(p)];
debug_msg("Dereferencing " + hex(p) + " -> " + hex(q));
if (q === undefined)
{
abort("Bad memory access: @" + p);
}
else
p = q;
}
return p;
}
// This should be a macro
function TAG(p)
{
// >>> is unsigned-right-shift. Nice.
return (p >>> WORD_BITS) & TAG_MASK;
}
// This should be a macro
function VAL(p)
{
return p & ((1 << WORD_BITS)-1);
}
// Ideally this would be inlined, but javascript does not support macros. Ultimately this could be generated dynamically.
function backtrack()
{
debug_msg("Backtracking. State.B is " + state.B);
if (state.B <= HEAP_SIZE)
{
return false;
}
debug_msg("Choicepoint has " + memory[state.B] + " saved args");
state.B0 = memory[state.B + memory[state.B] + 7];
// Also unwind any trailed bindings
unwind_trail(memory[state.B + memory[state.B] + 5], state.TR);
var next = memory[state.B + memory[state.B] + 4];
state.P = next.offset;
code = next.code;
state.current_predicate = next.predicate;
debug_msg("Set state.P to " + state.P);
return true;
}
// Returns a <STR, f/n> cell. This MUST be followed (eventually) by n args. Attempting to print the term (or otherwise use) the term before then will result in chaos
// ftor must have the ATM tag!
function alloc_structure(ftor)
{
var tmp = state.H;
memory[state.H++] = ftor;
return tmp ^ (TAG_STR << WORD_BITS);
}
function alloc_var()
{
var result = state.H ^ (TAG_REF << WORD_BITS);
memory[state.H] = result;
state.H++;
return result;
}
function alloc_list()
{
var result = (state.H+1) ^ (TAG_LST << WORD_BITS);
memory[state.H] = result;
state.H++;
return result;
}
function wam()
{
state.running = true;
while (state.running)
{
debug_msg("---");
debug_msg("P=" + (((state.current_predicate == null)?("no predicate"):(atable[ftable[state.current_predicate.key][0]] + "/" + ftable[state.current_predicate.key][1])) + "@" + state.P + ": " + code[state.P]) + ", H=" + state.H + ", B=" + state.B + ", B0=" + state.B0 + ", E=" + state.E);
// Decode an instruction
switch(code[state.P])
{
case 1: // allocate
var tmpE;
if (state.E > state.B)
{
debug_msg("P=" + state.P + " Top frame is an environment, at " + state.E + " with previous environment of " + memory[state.E] + " and CP of " + memory[state.E+1]);
tmpE = state.E + state.CP.code[state.CP.offset - 1] + 2;
}
else
{
debug_msg("Top frame is a choicepoint, at " + state.B);
tmpE = state.B + memory[state.B] + 8;
}
debug_msg("Environment size is: " + state.CP.code[state.CP.offset-1]);
if (tmpE === undefined || isNaN(tmpE))
abort("Top of frame is garbage: " + tmpE);
if (tmpE < HEAP_SIZE || tmpE > HEAP_SIZE + STACK_SIZE)
abort("Top of frame exceeds bounds in allocate: " + hex(tmpE));
debug_msg("Allocating an environment at " + tmpE + " Y0 is at " + (tmpE + 2) + " state.B is " + state.B);
// Save old environment and continuation
memory[tmpE] = state.E;
memory[tmpE + 1] = state.CP
state.E = tmpE;
state.P += 1;
continue;
case 2: // deallocate
debug_msg("state.B is currently " + state.B);
debug_msg("state.E is currently " + state.E);
state.CP = memory[state.E + 1];
debug_msg("state.CP set to " + state.CP + " from memory[" + (state.E + 1)+"]");
if (memory[state.E] < HEAP_SIZE || memory[state.E] > HEAP_SIZE + STACK_SIZE)
abort("Top of frame " + memory[state.E] + " exceeds bounds in deallocate. Environment is " + state.E + " P = " + state.P);
state.E = memory[state.E];
debug_msg("Deallocate: E is reduced to " + state.E);
state.P += 1;
continue;
case 3: // call
var predicate = predicates[code[state.P+1]];
if (predicate !== undefined)
{
// Set CP to the next instruction so that when the predicate is finished executing we know where to come back to
state.CP = {code: code,
predicate: state.current_predicate,
offset: state.P + 3};
debug_msg("Calling " + atable[ftable[code[state.P+1]][0]] + "/" + ftable[code[state.P+1]][1] + " so setting CP to " + (state.P+3) + ", argument is " + code[state.P+2]);
state.num_of_args = ftable[code[state.P+1]][1];
state.B0 = state.B;
state.current_predicate = predicate;
code = predicate.clauses[predicate.clause_keys[0]].code;
state.P = 0;
}
else if (foreign_predicates[code[state.P+1]] !== undefined)
{
state.num_of_args = ftable[code[state.P+1]][1];
var fargs = new Array(state.num_of_args);
for (i = 0; i < state.num_of_args; i++)
{
fargs[i] = deref(register[i]);
}
// This is a bit counter-intuitive since it seems like we are never going to get a proceed to use the CP.
// Remember that any time we might need CP to be saved, it will be. (If there is more than one goal, there will be an environment).
// If there is only one goal (ie a chain rule) then we will be in exeucte already, not call.
// This means it is never unsafe to set CP in a call port.
// Further, rememebr that state.CP is used to create choicepoints (and environments), and since foreign frames may create these, we must set CP to
// something sensible, even though we never expect to use it to actually continue execution from.
state.CP = {code: code,
predicate: state.current_predicate,
offset:state.P + 3};
debug_msg("Calling (foreign) " + atable[ftable[code[state.P+1]][0]] + "/" + ftable[code[state.P+1]][1] + " and setting CP to " + (state.P + 3));
result = foreign_predicates[code[state.P+1]].apply(null, fargs);
state.foreign_retry = false;
if (result)
state.P = state.P + 3;
else if (!backtrack())
return false;
}
else
{
if (!undefined_predicate(code[state.P+1]))
return false;
}
continue;
case 4: // execute
var predicate = predicates[code[state.P+1]];
if (predicate !== undefined)
{
// No need to save continuation for execute
state.num_of_args = ftable[code[state.P+1]][1];
state.B0 = state.B;
debug_msg("Executing " + atable[ftable[code[state.P+1]][0]] + "/" + ftable[code[state.P+1]][1]);
state.current_predicate = predicate;
code = predicate.clauses[predicate.clause_keys[0]].code;
state.P = 0;
}
else if (foreign_predicates[code[state.P+1]] !== undefined)
{
state.num_of_args = ftable[code[state.P+1]][1];
debug_msg("Executing (foreign) " + atable[ftable[code[state.P+1]][0]] + "/" + ftable[code[state.P+1]][1]);
var fargs = new Array(state.num_of_args);
for (i = 0; i < state.num_of_args; i++)
fargs[i] = deref(register[i]);
result = foreign_predicates[code[state.P+1]].apply(null, fargs);
state.foreign_retry = false;
debug_msg("Foreign result: " + result + " and CP: " + state.CP);
if (result)
{
state.current_predicate = state.CP.predicate;
code = state.CP.code;
state.P = state.CP.offset;
}
else if (!backtrack())
return false;
}
else
{
if (!undefined_predicate(code[state.P+1]))
return false;
}
continue;
case 5: // proceed
state.P = state.CP.offset;
state.current_predicate = state.CP.predicate;
code = state.CP.code;
continue;
case 6: // put_variable: Initialize a new variable in Yn, and also put it into Ai
register_location = state.E + code[state.P+1] + 2;
debug_msg("Putting new variable into Y" + code[state.P+1] + " at " + register_location);
memory[register_location] = register_location ^ (TAG_REF << WORD_BITS);
register[code[state.P+2]] = register_location ^ (TAG_REF << WORD_BITS);
state.P += 3;
continue;
case 7: // put_variable: Put fresh var into registers Ai and Xn
var freshvar = state.H ^ (TAG_REF << WORD_BITS);
memory[state.H] = freshvar;
register[code[state.P+1]] = freshvar;
register[code[state.P+2]] = freshvar;
state.H++;
debug_msg("After put_variable, state.H is now " + state.H);
state.P += 3;
continue;
case 8: // put_value
var source;
if (code[state.P+1] == 0) // Y-register
{
register_location = state.E + code[state.P+2] + 2;
if (memory[register_location] === undefined)
abort("Invalid memory access in put_value");
register[code[state.P+3]] = memory[register_location];
debug_msg("put_value(Y" + code[state.P+2] + ", A" + code[state.P+3] + "): memory[" + register_location + "] = " + hex(memory[register_location]));
}
else
{
debug_msg("put_value: " + hex(register[code[state.P+2]]));
register[code[state.P+3]] = register[code[state.P+2]];
}
state.P += 4;
continue;
case 9: // put_unsafe_value
register_location = state.E + code[state.P+1] + 2;
// This is the unsafe bit. If the value now in register[code[state.P+2]] is on the stack (that is, it is > E) then we have to push a new variables
// onto the stack to avoid dangling references to things that are about to be cleaned up
if (memory[register_location] < state.E)
{
debug_msg("Value is safe");
// No, so we can just behave like put_value
register[code[state.P+2]] = deref(memory[register_location])
}
else
{
// Yes, so we need to push a new variable instead
debug_msg("x0 memory[" + state.E + "] = " + memory[state.E]);
debug_msg("Value is unsafe. Allocating a new unbound variable for it. It will go into Y" + code[state.P+1] + " @ " + register_location + ". E = " + state.E);
var v = alloc_var();
debug_msg("x1 memory[" + state.E + "] = " + memory[state.E]);
debug_msg("Binding " + hex(v) + " and Y" + code[state.P+1] + " = " + hex(memory[register_location]));
bind(v, memory[register_location]);
debug_msg("x2 memory[" + state.E + "] = " + memory[state.E]);
register[code[state.P+2]] = v;
debug_msg("x3 memory[" + state.E + "] = " + memory[state.E]);
debug_msg("X" + code[state.P+2] + " <- " + v);
}
state.P += 3;
continue;
case 10: // put_constant C into Ai
register[code[state.P+2]] = code[state.P+1] ^ (TAG_ATM << WORD_BITS);
state.P += 3;
continue;
case 11: // put_nil into Ai
register[code[state.P+1]] = NIL;
state.P += 2;
continue;
case 12: // put_structure
register[code[state.P+2]] = alloc_structure(code[state.P+1] ^ (TAG_ATM << WORD_BITS));
state.mode = WRITE;
state.P += 3;
continue;
case 13: // put_list
register[code[state.P+1]] = alloc_list();
state.mode = WRITE;
state.P += 2;
continue;
case 14: // put_integer I into Ai
register[code[state.P+2]] = (code[state.P+1] & ((1 << WORD_BITS)-1)) ^ (TAG_INT << WORD_BITS);
state.P += 3;
continue;
case 15: // get_variable
if (code[state.P+1] == 0) // Y-register
{
register_location = state.E + code[state.P+2] + 2;
debug_msg("Y" + code[state.P+2] + " <- " + hex(register[code[state.P+3]]));
memory[register_location] = register[code[state.P+3]];
}
else
{
debug_msg("X" + code[state.P+2] + " <- " + hex(register[code[state.P+3]]));
register[code[state.P+2]] = register[code[state.P+3]];
}
state.P+= 4;
continue;
case 16: // get_value
var source;
var target = register[code[state.P+3]];
gc_check(target);
if (code[state.P+1] == 0) // Y-register
{
register_location = state.E + code[state.P+2] + 2;
source = memory[register_location];
}
else
{
source = register[code[state.P+2]];
}
state.P += 4;
debug_msg("get_value: Unifying " + hex(source) + " and " + hex(target));
if (!unify(source, target))
if (!backtrack())
return false;
continue;
case 17: // get_constant C from Ai
// First, get what is in Ai into sym
var sym = deref(register[code[state.P+2]]);
// Then get arg. This is an atom index, not a <CON, i> cell. It needs to be made into the latter!
var arg = code[state.P+1] ^ (TAG_ATM << WORD_BITS);
state.P += 3;
if (TAG(sym) == TAG_REF)
{
// If Ai is variable, then we need to bind. This is when foo(bar) is called like foo(X).
bind(sym, arg);
}
else if (sym != arg)
{
debug_msg("Could not get constant: " + hex(sym) + " from " + hex(arg));
if (!backtrack())
return false;
}
continue;
case 18: // get_nil
var sym = deref(register[code[state.P+1]]);
state.P += 1;
if (TAG(sym) == TAG_REF)
bind(sym, NIL);
else if (sym != NIL)
if (!backtrack())
return false;
continue;
case 19: // get_structure
var ftor = code[state.P+1] ^ (TAG_ATM << WORD_BITS);
var addr = deref(register[code[state.P+2]]);
state.P += 3;
if (TAG(addr) == TAG_REF)
{
debug_msg("Arg passed is unbound. Proceeding in WRITE mode");
state.mode = WRITE;
a = alloc_structure(ftor);
bind(memory[addr], a);
}
else if (TAG(addr) == TAG_STR && memory[VAL(addr)] == ftor)
{
debug_msg("Arg passed is bound to the right functor. Proceeding in READ mode from " + (VAL(addr)+1));
state.mode = READ;
state.S = VAL(addr)+1;
}
else
{
if (!backtrack())
return false;
}
continue;
case 20: // get_list from Ai
var addr = deref(register[code[state.P+1]]);
state.P += 2;
if (TAG(addr) == TAG_REF)
{
// predicate called with var and we are expecting a list
var l = state.H ^ (TAG_LST << WORD_BITS);
bind(memory[addr], l);
debug_msg("Bound memory[" + addr + "] ( " + memory[addr] + ") to <LST," + state.H + ">");
state.mode = WRITE;
}
else if (TAG(addr) == TAG_LST)
{
debug_msg("get_list will proceed in read mode from " + VAL(addr));
state.S = VAL(addr);
state.mode = READ;
}
else
if (!backtrack())
return false;
continue;
case 21: // get_integer I from Ai
// First, get what is in Ai into sym
var sym = deref(register[code[state.P+2]]);
// Then get arg. This is the actual integer, not a <INT, i> cell. It needs to be made into the latter!
var arg = (code[state.P+1] & ((1 << WORD_BITS)-1)) ^ (TAG_INT << WORD_BITS);
state.P += 3;
if (TAG(sym) == TAG_REF)
{
// If Ai is variable, then we need to bind. This is when foo(7) is called like foo(X).
bind(sym, arg);
}
else if (sym != arg)
{
debug_msg("Could not get constant: " + hex(sym) + " from " + hex(arg));
if (!backtrack())
return false;
}
continue;
case 22: // unify_void
if (state.mode == READ)
state.S += code[state.P+1];
else
for (i = 0; i < code[state.P+1]; i++)
alloc_var();
state.P += 2;
continue;
case 23: //unify_variable
var source;
if (state.mode == READ) // If reading, consume the next symbol
{
source = memory[state.S++];
debug_msg("Unifying existing variable: " + hex(source) + " at " + (state.S-1));
debug_msg(term_to_string(source));
}
else
{
source = alloc_var(); // If writing, create a new var
debug_msg("Allocated new variable: " + source);
}
if (code[state.P+1] == 0) // Y-register
{
debug_msg("... for register Y" + code[state.P+2]);
register_location = state.E + code[state.P+2] + 2;
// GC: This needs to be trailed if state.B is not 0, apparently
bind(memory[register_location], source);
}
else
{
register[code[state.P+2]] = source;
}
state.P += 3;
continue;
case 24: // unify_value
var did_fail = false;
if (state.mode == READ)
{
source = memory[state.S++];
if (code[state.P+1] == 0) // Y-register
{
register_location = state.E + code[state.P+2] + 2;
did_fail = !unify(memory[register_location], source);
}
else
{
did_fail = !unify(register[code[state.P+2]], source);
}
}
else
{
if (code[state.P+1] == 0) // Y-register
{
register_location = state.E + code[state.P+2] + 2;
memory[state.H++] = memory[register_location];
}
else
{
memory[state.H++] = register[code[state.P+2]];
}
}
state.P += 3;
if (did_fail)
if (!backtrack())
return false;
continue;
case 25: // unify_local_value
var did_fail = false;
if (state.mode == READ)
{
source = memory[state.S++];
if (code[state.P+1] == 0) // Y-register
{
register_location = state.E + code[state.P+2] + 2;
did_fail = !unify(memory[register_location], source);
}
else
{
did_fail = !unify(register[code[state.P+2]], source);
}
}
else
{
var addr;
if (code[state.P+1] == 0) // Y-register;
{
register_location = state.E + code[state.P+2] + 2;
addr = memory[register_location];
}
else
{
addr = register[code[state.P+2]];
}
addr = deref(addr);
if (VAL(addr) < state.H)
{
debug_msg("Unify local: already safe at " + hex(addr));
// Address is on the heap. Just push the value onto the top of the heap
debug_msg(term_to_string(addr));
memory[state.H++] = addr;
}
else
{
debug_msg("Unify local: unsafe. Globalizing");
// Address is on the stack. Push a new variable onto the heap and bind to the value
fresh = state.H ^ (TAG_REF << WORD_BITS);
memory[state.H++] = fresh;
debug_msg("Binding fresh variable " + fresh + " to " + addr);
bind(fresh, addr);
if (code[state.P+1] == 1)
register[code[state.P+2]] = fresh; // also set X(i) if X-register
}
}
state.P += 3;
if (did_fail)
if (!backtrack())
return false;
continue;
case 26: // unify_constant
if (state.mode == READ)
{
var sym = deref(memory[state.S++]);
var arg = code[state.P+1] ^ (TAG_ATM << WORD_BITS);
state.P += 2;
debug_msg("sym: " + hex(sym) + ", arg: " + hex(arg));
if (TAG(sym) == TAG_REF)
{
debug_msg("Binding " + sym + " and " + arg);
bind(sym, arg);
}
else if (sym != arg)
if (!backtrack())
return false;
}
else
{
memory[state.H++] = code[state.P+1] ^ (TAG_ATM << WORD_BITS);
state.P += 2;
}
continue;
case 27: // unify_integer
if (state.mode == READ)
{
var sym = deref(memory[state.S++]);
var arg = (code[state.P+1] & ((1 << WORD_BITS)-1)) ^ (TAG_INT << WORD_BITS)
state.P += 2;
if (TAG(sym) == TAG_REF)
{
debug_msg("Binding " + sym + " and " + arg);
bind(sym, arg);
}
else if (sym != arg)
if (!backtrack())
return false;
}
else
{
memory[state.H++] = (code[state.P+1] & ((1 << WORD_BITS)-1)) ^ (TAG_INT << WORD_BITS);
state.P += 2;
}
continue;
case 28: // try_me_else
debug_msg("try_me_else at P=" + state.P + " which has branch=" + code[state.P+1]);
// We need to allocate a new choicepoint, but first we need to determine /where/ to put it, since we do not keep a reference to the top of the stack.
// The last item on the stack is either an environment, or a choicepoint.
var newB;
if (state.E > state.B)
{
// In this case, it is an environment. In the real WAM, which does stack trimming (see Ait-Kaci chapter 5.7), we only have CE, CP and then N saved Y-registers.
// Therefore, we need to write the new choicepoint at 2 + N. What is N, though? Well, it turns out N gets gradually smaller as time goes on, which
// is why it is not stored in the frame itself. If call(f) is outfitted with a second argument to become call(f, n) then we can decode this in try_me_else
// (and ignore it if we did not want to create a new environment) by looking at CP, which points to the instruction after the call() opcode. Therefore,
// code[CP-1] ought to be N.
// -----------
// |0 | CE |
// |1 | CP |
// |3 | Y0 |
// ...
// |n+2| Yn |
// -----------
debug_msg("P=" + state.P + " Top frame is an environment. Starts at " + state.E + " and has length = " + state.CP.code[state.CP.offset-1] + " + 2. Previous is " + memory[state.E]);
debug_msg("Top choicepoint is " + state.B);
newB = state.E + state.CP.code[state.CP.offset - 1] + 2;
}
else
{
// In this case, the top frame is a choicepoint. This is a bit easier: A choicepoint contains 7 saved special-purpose registers, the N root arguments
// for the goal, and, happily, the value of N as the very first argument. Therefore, we can read the 0th entry of the current frae (at state.B)
// and add 9 to it to get the top of the stack.
debug_msg("Top frame is a choicepoint: " + state.B);
debug_msg("Top environment is " + state.E);
newB = state.B + memory[state.B] + 8;
}
debug_msg("Creating new choicepoint on the stack at " + newB);
memory[newB] = state.num_of_args;
var n = memory[newB];
for (i = 0; i < n; i++)
{
//debug_msg("Saving register " + i + "(" + hex(register[i]) + ") to " + (newB + i + 1));
memory[newB + i + 1] = register[i];
}
// Save the current context
memory[newB+n+1] = state.E;
memory[newB+n+2] = state.CP;
memory[newB+n+3] = state.B;
var next = code[state.P+1];
if ((next & 0x80000000) == 0)
{
// next is a clause index in the current predicate
memory[newB+n+4] = {code: state.current_predicate.clauses[next].code,
predicate:state.current_predicate,
offset:0};
}
else
{
// next is an absolute address in the current clause: Used for auxiliary clauses only
memory[newB+n+4] = {code: code,
predicate: state.current_predicate,
offset:next ^ 0x80000000};
}
//memory[newB+n+4] = {code: code, offset:code[state.P+1]};
memory[newB+n+5] = state.TR;
memory[newB+n+6] = state.H;
memory[newB+n+7] = state.B0;
state.B = newB;
debug_msg("case 28: Before we created the choicepoint, HB was " + state.HB);
state.HB = state.H;
state.P += 2;
debug_msg("try_me_else: state.B is now at " + state.B + " and state.HB is now " + state.HB);
continue;
case 29: // retry_me_else
// Unwind the last goal. The arity if the first thing on the stack, then the saved values for A1...An
var arity = memory[state.B];
debug_msg("retry_me_else: " + state.B + " with arity " + memory[state.B] + " and retry point " + code[state.P+1]);
for (var i = 0; i < arity; i++)
register[i] = memory[state.B + i + 1];
// Now restore all the special-purpose registers
if (memory[state.B + arity + 1] < HEAP_SIZE)
abort("Top of frame contains E which is in the heap");
if (memory[state.B + arity + 1] > HEAP_SIZE + STACK_SIZE)
abort("Top of frame contains E which exceeds the stack");
debug_msg("top of frame at " + state.B + " is OK");
state.E = memory[state.B + arity + 1];
state.CP = memory[state.B + arity + 2];
var next = code[state.P+1];
debug_msg("Retry me else: Set CP to " + state.CP);
// set up the 'else' part of retry_me_else by adjusting the saved value of B
// memory[state.B + arity + 4] = {code: state.current_predicate.clauses[state.current_predicate.clause_keys[code[state.P+1]]].code, predicate:state.current_predicate, offset:0};
if ((next & 0x80000000) == 0)
{
// next is a clause index in the current predicate
memory[state.B+arity+4] = {code: state.current_predicate.clauses[next].code,
predicate:state.current_predicate,
offset:0};
}
else
{
// next is an absolute address in the current clause: Used for auxiliary clauses only
memory[state.B+arity+4] = {code: code,
predicate: state.current_predicate,
offset:next ^ 0x80000000};
}
unwind_trail(memory[state.B + arity + 5], state.TR);
state.TR = memory[state.B + arity + 5];
state.H = memory[state.B + arity + 6];
debug_msg("case 29: state.HB <- " + state.HB);
state.HB = state.H
state.P += 2;
continue;
case 30: // trust_me
// Unwind the last goal. The arity if the first thing on the stack, then the saved values for A1...An
var n = memory[state.B];
debug_msg("trusting last clause: " + state.B + " with arity " + memory[state.B] + " and HB was " + state.HB + ". Choicepoint has " + n + " args");
for (var i = 0; i < n; i++)
{
debug_msg("Restoring register " + i + " to " + hex(memory[state.B + i + 1]));
register[i] = memory[state.B + i + 1];
}
// Now restore all the special-purpose registers
if (memory[state.B + n + 1] < HEAP_SIZE || memory[state.B + n + 1] > HEAP_SIZE + STACK_SIZE)
abort("Top of frame exceeds bounds in trust. Read from memory[" + (state.B+n+1) + "]. State.B is " + state.B);
state.E = memory[state.B + n + 1];
state.CP = memory[state.B + n + 2];
debug_msg("trust_me: Set CP to " + state.CP);
unwind_trail(memory[state.B + n + 5], state.TR);