forked from xicoVale/yap-6.3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
absmi.c
executable file
·1417 lines (1278 loc) · 34.4 KB
/
absmi.c
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
/*************************************************************************
* *
* Yap Prolog *
* *
* Yap Prolog was developed at NCCUP - Universidade do Porto *
* \z *
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
* *
**************************************************************************
* *
* File: absmi.c *
* comments: Portable abstract machine interpreter *
* Last rev: $Date: 2008-08-13 01:16:26 $,$Author: vsc $
**
* $Log: not supported by cvs2svn $
* Revision 1.246 2008/08/12 01:27:22 vsc
* *
* *
*************************************************************************/
/**
@file absmi.c
@defgroup Efficiency Efficiency Considerations
@ingroup YAPProgramming
We next discuss several issues on trying to make Prolog programs run
fast in YAP. We assume two different programming styles:
+ Execution of <em>deterministic</em> programs ofte
n
boils down to a recursive loop of the form:
~~~~~
loop(Env) :-
do_something(Env,NewEnv),
loop(NewEnv).
~~~~~
*/
#define IN_ABSMI_C 1
#define _INATIVE 1
#define HAS_CACHE_REGS 1
#include "absmi.h"
#include "heapgc.h"
#include "cut_c.h"
#if YAP_JIT
#include "IsGround.h"
TraceContext **curtrace;
yamop *curpreg;
BlocksContext **globalcurblock;
COUNT ineedredefinedest;
yamop *headoftrace;
NativeContext *NativeArea;
IntermediatecodeContext *IntermediatecodeArea;
CELL l;
CELL nnexec;
Environment *Yap_ExpEnvP, Yap_ExpEnv;
void **Yap_ABSMI_ControlLabels;
static Int traced_absmi(void) { return Yap_traced_absmi(); }
#endif
void **Yap_ABSMI_OPCODES;
#ifdef PUSH_X
#else
/* keep X as a global variable */
Term Yap_XREGS[MaxTemps]; /* 29 */
#endif
#include "arith2.h"
// #include "print_preg.h"
//#include "sprint_op.hpp"
//#include "print_op.hpp"
#ifdef COROUTINING
/*
Imagine we are interrupting the execution, say, because we have a spy
point or because we have goals to wake up. This routine saves the current
live temporary registers into a structure pointed to by register ARG1.
The registers are then recovered by a nasty builtin
called
*/
static Term push_live_regs(yamop *pco) {
CACHE_REGS
CELL *lab = (CELL *)(pco->y_u.l.l);
CELL max = lab[0];
CELL curr = lab[1];
Term tp = MkIntegerTerm((Int)pco);
Term tcp = MkIntegerTerm((Int)CP);
Term tenv = MkIntegerTerm((Int)(LCL0 - ENV));
Term tyenv = MkIntegerTerm((Int)(LCL0 - YENV));
CELL *start = HR;
Int tot = 0;
HR++;
*HR++ = tp;
*HR++ = tcp;
*HR++ = tenv;
*HR++ = tyenv;
tot += 4;
{
CELL i;
lab += 2;
for (i = 0; i <= max; i++) {
if (i == 8 * CellSize) {
curr = lab[0];
lab++;
}
if (curr & 1) {
CELL d1;
tot += 2;
HR[0] = MkIntTerm(i);
d1 = XREGS[i];
deref_head(d1, wake_up_unk);
wake_up_nonvar:
/* just copy it to the heap */
HR[1] = d1;
HR += 2;
continue;
{
CELL *pt0;
deref_body(d1, pt0, wake_up_unk, wake_up_nonvar);
/* bind it, in case it is a local variable */
if (pt0 <= HR) {
/* variable is safe */
HR[1] = (CELL)pt0;
} else {
d1 = Unsigned(HR + 1);
RESET_VARIABLE(HR + 1);
Bind_Local(pt0, d1);
}
}
HR += 2;
}
curr >>= 1;
}
start[0] = (CELL)Yap_MkFunctor(AtomTrue, tot);
return (AbsAppl(start));
}
}
#endif
#if USE_THREADED_CODE && (defined(ANALYST) || defined(DEBUG))
char *Yap_op_names[] = {
#define OPCODE(OP, TYPE) #OP
#include "YapOpcodes.h"
#undef OPCODE
};
#endif
static int check_alarm_fail_int(int CONT USES_REGS) {
#if defined(_MSC_VER) || defined(__MINGW32__)
/* I need this for Windows and any system where SIGINT
is not proceesed by same thread as absmi */
if (LOCAL_PrologMode & (AbortMode | InterruptMode)) {
CalculateStackGap(PASS_REGS1);
return CONT;
}
#endif
if (Yap_get_signal(YAP_FAIL_SIGNAL)) {
return false;
}
if (!Yap_has_a_signal()) {
/* no need to look into GC */
CalculateStackGap(PASS_REGS1);
}
// fail even if there are more signals, they will have to be dealt later.
return -1;
}
static int stack_overflow(PredEntry *pe, CELL *env, yamop *cp,
arity_t nargs USES_REGS) {
if (Unsigned(YREG) - Unsigned(HR) < StackGap(PASS_REGS1) ||
Yap_get_signal(YAP_STOVF_SIGNAL)) {
S = (CELL *)pe;
if (!Yap_locked_gc(nargs, env, cp)) {
Yap_NilError(RESOURCE_ERROR_STACK, LOCAL_ErrorMessage);
return 0;
}
return 1;
}
return -1;
}
static int code_overflow(CELL *yenv USES_REGS) {
if (Yap_get_signal(YAP_CDOVF_SIGNAL)) {
CELL cut_b = LCL0 - (CELL *)(yenv[E_CB]);
/* do a garbage collection first to check if we can recover memory */
if (!Yap_locked_growheap(false, 0, NULL)) {
Yap_NilError(RESOURCE_ERROR_HEAP, "YAP failed to grow heap: %s",
LOCAL_ErrorMessage);
return 0;
}
CACHE_A1();
if (yenv == ASP) {
yenv[E_CB] = (CELL)(LCL0 - cut_b);
}
return 1;
}
return -1;
}
static int interrupt_handler(PredEntry *pe USES_REGS) {
// printf("D %lx %p\n", LOCAL_ActiveSignals, P);
/* tell whether we can creep or not, this is hard because we will
lose the info RSN
*/
BEGD(d0);
d0 = pe->ArityOfPE;
if (d0 == 0) {
HR[1] = MkAtomTerm((Atom)pe->FunctorOfPred);
} else {
HR[d0 + 2] = AbsAppl(HR);
*HR = (CELL)pe->FunctorOfPred;
HR++;
BEGP(pt1);
pt1 = XREGS + 1;
for (; d0 > 0; --d0) {
BEGD(d1);
BEGP(pt0);
pt0 = pt1;
d1 = *pt0;
deref_head(d1, creep_unk);
creep_nonvar:
/* just copy it to the heap */
pt1++;
*HR++ = d1;
continue;
derefa_body(d1, pt0, creep_unk, creep_nonvar);
if (pt0 <= HR) {
/* variable is safe */
*HR++ = (CELL)pt0;
pt1++;
} else {
/* bind it, in case it is a local variable */
d1 = Unsigned(HR);
RESET_VARIABLE(HR);
pt1++;
HR += 1;
Bind_Local(pt0, d1);
}
ENDP(pt0);
ENDD(d1);
}
ENDP(pt1);
}
ENDD(d0);
HR[0] = Yap_Module_Name(pe);
ARG1 = (Term)AbsPair(HR);
HR += 2;
#ifdef COROUTINING
if (Yap_get_signal(YAP_WAKEUP_SIGNAL)) {
CalculateStackGap(PASS_REGS1);
ARG2 = Yap_ListOfWokenGoals();
pe = WakeUpCode;
/* no more goals to wake up */
Yap_UpdateTimedVar(LOCAL_WokenGoals, TermNil);
} else
#endif
{
CalculateStackGap(PASS_REGS1);
pe = CreepCode;
}
P = pe->CodeOfPred;
#ifdef LOW_LEVEL_TRACER
if (Yap_do_low_level_trace)
low_level_trace(enter_pred, pe, XREGS + 1);
#endif /* LOW_LEVEL_TRACE */
/* for profiler */
CACHE_A1();
return true;
}
// interrupt handling code that sets up the case when we do not have
// a guaranteed environment.
static int safe_interrupt_handler(PredEntry *pe USES_REGS) {
CELL *npt = HR;
// printf("D %lx %p\n", LOCAL_ActiveSignals, P);
/* tell whether we can creep or not, this is hard because we will
lose the info RSN
*/
BEGD(d0);
S = (CELL *)pe;
d0 = pe->ArityOfPE;
if (d0 == 0) {
HR[1] = MkAtomTerm((Atom)pe->FunctorOfPred);
} else {
HR[d0 + 2] = AbsAppl(HR);
HR += d0 + 1 + 2;
*npt++ = (CELL)pe->FunctorOfPred;
BEGP(pt1);
pt1 = XREGS + 1;
for (; d0 > 0; --d0) {
BEGD(d1);
d1 = *pt1;
loop:
if (!IsVarTerm(d1)) {
/* just copy it to the heap */
pt1++;
*npt++ = d1;
} else {
if (VarOfTerm(d1) < H0 || VarOfTerm(d1) > HR) {
d1 = Deref(d1);
if (VarOfTerm(d1) < H0 || VarOfTerm(d1) > HR) {
Term v = MkVarTerm();
YapBind(VarOfTerm(d1), v);
} else {
goto loop;
}
} else {
*npt++ = d1;
}
}
ENDD(d1);
}
ENDP(pt1);
}
ENDD(d0);
npt[0] = Yap_Module_Name(pe);
ARG1 = AbsPair(npt);
HR += 2;
#ifdef COROUTINING
if (Yap_get_signal(YAP_WAKEUP_SIGNAL)) {
CalculateStackGap(PASS_REGS1);
ARG2 = Yap_ListOfWokenGoals();
pe = WakeUpCode;
/* no more goals to wake up */
Yap_UpdateTimedVar(LOCAL_WokenGoals, TermNil);
} else
#endif
{
CalculateStackGap(PASS_REGS1);
pe = CreepCode;
}
// allocate and fill out an environment
YENV = ASP;
CACHE_Y_AS_ENV(YREG);
ENV_YREG[E_CP] = (CELL)CP;
ENV_YREG[E_E] = (CELL)ENV;
#ifdef DEPTH_LIMIT
ENV_YREG[E_DEPTH] = DEPTH;
#endif /* DEPTH_LIMIT */
ENV = ENV_YREG;
ENV_YREG = (CELL *)((CELL)ENV_YREG + ENV_Size(CP));
WRITEBACK_Y_AS_ENV();
ENDCACHE_Y_AS_ENV();
CP = P;
P = pe->CodeOfPred;
#ifdef DEPTH_LIMIT
if (DEPTH <= MkIntTerm(1)) { /* I assume Module==0 is primitives */
if (pe->ModuleOfPred) {
if (DEPTH == MkIntTerm(0))
return false;
else
DEPTH = RESET_DEPTH();
}
} else if (pe->ModuleOfPred) {
DEPTH -= MkIntConstant(2);
}
#endif /* DEPTH_LIMIT */
return true;
}
static int interrupt_handlerc(PredEntry *pe USES_REGS) {
/* do creep in call */
ENV = YENV;
CP = NEXTOP(P, Osbpp);
YENV = (CELL *)(((char *)YENV) + P->y_u.Osbpp.s);
#ifdef FROZEN_STACKS
{
choiceptr top_b = PROTECT_FROZEN_B(B);
#ifdef YAPOR_SBA
if (YENV > (CELL *)top_b || YENV < HR)
YENV = (CELL *)top_b;
#else
if (YENV > (CELL *)top_b)
YENV = (CELL *)top_b;
#endif /* YAPOR_SBA */
else
YENV = YENV + ENV_Size(CP);
}
#else
if (YENV > (CELL *)B)
YENV = (CELL *)B;
else
/* I am not sure about this */
YENV = YENV + ENV_Size(CP);
#endif /* FROZEN_STACKS */
/* setup GB */
YENV[E_CB] = (CELL)B;
return interrupt_handler(pe PASS_REGS);
}
static int interrupt_handler_either(Term t_cut, PredEntry *pe USES_REGS) {
int rc;
ARG1 = push_live_regs(NEXTOP(P, Osbpp));
#ifdef FROZEN_STACKS
{
choiceptr top_b = PROTECT_FROZEN_B(B);
// protect registers before we mess about.
// recompute YENV and get ASP
#ifdef YAPOR_SBA
if (YENV > (CELL *)top_b || YENV < HR)
YENV = (CELL *)top_b;
#else
if (YENV > (CELL *)top_b)
YENV = (CELL *)top_b;
#endif /* YAPOR_SBA */
else
YENV = YENV + ENV_Size(CP);
}
#else
if (YENV > (CELL *)B)
YENV = (CELL *)B;
#endif /* FROZEN_STACKS */
P = NEXTOP(P, Osbpp);
// should we cut? If t_cut == INT(0) no
ARG2 = t_cut;
// ASP
SET_ASP(YENV, E_CB * sizeof(CELL));
// do the work.
rc = safe_interrupt_handler(pe PASS_REGS);
return rc;
}
/* to trace interrupt calls */
// #define DEBUG_INTERRUPTS 1
#ifdef DEBUG_INTERRUPTS
static int trace_interrupts = true;
#endif
static int interrupt_fail(USES_REGS1) {
#ifdef DEBUG_INTERRUPTS
if (trace_interrupts)
fprintf(stderr, "[%d] %lu--%lu %s:%d: (YENV=%p ENV=%p ASP=%p)\n",
worker_id, LOCAL_FirstActiveSignal, LOCAL_LastActiveSignal,
__FUNCTION__, __LINE__, YENV, ENV, ASP);
#endif
check_alarm_fail_int(false PASS_REGS);
/* don't do debugging and stack expansion here: space will
be recovered. automatically by fail, so
better wait.
*/
if (Yap_has_signal(YAP_CREEP_SIGNAL)) {
return false;
}
if (Yap_has_signal(YAP_CDOVF_SIGNAL)) {
return false;
}
/* make sure we have the correct environment for continuation */
ENV = B->cp_env;
YENV = (CELL *)B;
return interrupt_handler(RepPredProp(Yap_GetPredPropByAtom(AtomFail, 0))
PASS_REGS);
}
static int interrupt_execute(USES_REGS1) {
int v;
#ifdef DEBUG_INTERRUPTS
if (trace_interrupts)
fprintf(stderr, "[%d] %lu--%lu %s:%d: (YENV=%p ENV=%p ASP=%p)\n", worker_id,
LOCAL_FirstActiveSignal, LOCAL_LastActiveSignal, __FUNCTION__,
__LINE__, YENV, ENV, ASP);
#endif
if ((v = check_alarm_fail_int(true PASS_REGS)) >= 0) {
return v;
}
if (PP)
UNLOCKPE(1, PP);
PP = P->y_u.pp.p0;
if ((P->y_u.pp.p->PredFlags & (NoTracePredFlag | HiddenPredFlag)) &&
Yap_only_has_signal(YAP_CREEP_SIGNAL)) {
return 2;
}
SET_ASP(YENV, E_CB * sizeof(CELL));
if ((v = code_overflow(YENV PASS_REGS)) >= 0) {
return v;
}
if ((v = stack_overflow(P->y_u.pp.p, ENV, CP,
P->y_u.pp.p->ArityOfPE PASS_REGS)) >= 0) {
return v;
}
return interrupt_handler(P->y_u.pp.p PASS_REGS);
}
static int interrupt_call(USES_REGS1) {
int v;
#ifdef DEBUG_INTERRUPTS
if (trace_interrupts)
fprintf(stderr, "[%d] %lu--%lu %s:%d: (YENV=%p ENV=%p ASP=%p)\n",
worker_id, LOCAL_FirstActiveSignal, LOCAL_LastActiveSignal,
__FUNCTION__, __LINE__, YENV, ENV, ASP);
#endif
if ((v = check_alarm_fail_int(true PASS_REGS)) >= 0) {
return v;
}
if (PP)
UNLOCKPE(1, PP);
PP = P->y_u.Osbpp.p0;
if (Yap_only_has_signal(YAP_CREEP_SIGNAL) &&
(P->y_u.Osbpp.p->PredFlags & (NoTracePredFlag | HiddenPredFlag))) {
return 2;
}
SET_ASP(YENV, P->y_u.Osbpp.s);
if ((v = code_overflow(YENV PASS_REGS)) >= 0) {
return v;
}
if ((v = stack_overflow(P->y_u.Osbpp.p, YENV, NEXTOP(P, Osbpp),
P->y_u.Osbpp.p->ArityOfPE PASS_REGS)) >= 0) {
return v;
}
return interrupt_handlerc(P->y_u.Osbpp.p PASS_REGS);
}
static int interrupt_pexecute(PredEntry *pen USES_REGS) {
int v;
#ifdef DEBUG_INTERRUPTS
if (trace_interrupts)
fprintf(stderr, "[%d] %lu--%lu %s:%d: (YENV=%p ENV=%p ASP=%p)\n",
worker_id, LOCAL_FirstActiveSignal, LOCAL_LastActiveSignal,
__FUNCTION__, __LINE__, YENV, ENV, ASP);
#endif
if ((v = check_alarm_fail_int(2 PASS_REGS)) >= 0) {
return v;
}
if (PP)
UNLOCKPE(1, PP);
PP = NULL;
if (Yap_only_has_signal(YAP_CREEP_SIGNAL)) {
return 2; /* keep on creeping */
}
SET_ASP(YENV, E_CB * sizeof(CELL));
/* setup GB */
YENV[E_CB] = (CELL)B;
if ((v = code_overflow(YENV PASS_REGS)) >= 0) {
return v;
}
if ((v = stack_overflow(pen, ENV, NEXTOP(P, Osbmp),
pen->ArityOfPE PASS_REGS)) >= 0) {
return v;
}
CP = NEXTOP(P, Osbmp);
return interrupt_handler(pen PASS_REGS);
}
static void execute_dealloc(USES_REGS1) {
/* other instructions do depend on S being set by deallocate
*/
CELL *ENVYREG = YENV;
S = ENVYREG;
CP = (yamop *)ENVYREG[E_CP];
ENV = ENVYREG = (CELL *)ENVYREG[E_E];
#ifdef DEPTH_LIMIT
DEPTH = ENVYREG[E_DEPTH];
#endif /* DEPTH_LIMIT */
#ifdef FROZEN_STACKS
{
choiceptr top_b = PROTECT_FROZEN_B(B);
#ifdef YAPOR_SBA
if (ENVYREG > (CELL *)top_b || ENVYREG < HR)
ENVYREG = (CELL *)top_b;
#else
if (ENVYREG > (CELL *)top_b)
ENVYREG = (CELL *)top_b;
#endif /* YAPOR_SBA */
else
ENVYREG = (CELL *)((CELL)ENVYREG + ENV_Size(CP));
}
#else
if (ENVYREG > (CELL *)B)
ENVYREG = (CELL *)B;
else
ENV_YREG = (CELL *)((CELL)ENV_YREG + ENV_Size(CP));
#endif /* FROZEN_STACKS */
YENV = ENVYREG;
P = NEXTOP(P, p);
}
/* don't forget I cannot creep at deallocate (where to?) */
/* also, this is unusual in that I have already done deallocate,
so I don't need to redo it.
*/
static int interrupt_deallocate(USES_REGS1) {
int v;
#ifdef DEBUG_INTERRUPTS
if (trace_interrupts)
fprintf(stderr, "[%d] %lu--%lu %s:%d (YENV=%p ENV=%p ASP=%p)\n", worker_id,
LOCAL_FirstActiveSignal, LOCAL_LastActiveSignal, __FUNCTION__,
__LINE__, YENV, ENV, ASP);
#endif
if ((v = check_alarm_fail_int(true PASS_REGS)) >= 0) {
return v;
}
/*
don't do a creep here; also, if our instruction is followed by
a execute_c, just wait a bit more */
if (Yap_only_has_signals(YAP_CREEP_SIGNAL, YAP_WAKEUP_SIGNAL) ||
/* keep on going if there is something else */
(P->opc != Yap_opcode(_procceed) && P->opc != Yap_opcode(_cut_e))) {
execute_dealloc(PASS_REGS1);
return 1;
} else {
CELL cut_b = LCL0 - (CELL *)(S[E_CB]);
if (PP)
UNLOCKPE(1, PP);
PP = PREVOP(P, p)->y_u.p.p;
ASP = YENV + E_CB;
/* cut_e */
SET_ASP(YENV, E_CB * sizeof(CELL));
if ((v = code_overflow(YENV PASS_REGS)) >= 0) {
return v;
}
if (Yap_has_a_signal()) {
PredEntry *pe;
if (Yap_op_from_opcode(P->opc) == _cut_e) {
/* followed by a cut */
ARG1 = MkIntegerTerm(LCL0 - (CELL *)S[E_CB]);
pe = RepPredProp(Yap_GetPredPropByFunc(FunctorCutBy, 1));
} else {
pe = RepPredProp(Yap_GetPredPropByAtom(AtomTrue, 0));
}
// deallocate moves P one step forward.
bool rc = interrupt_handler(pe PASS_REGS);
P = NEXTOP(P,p);
return rc;
}
if (!Yap_locked_gc(0, ENV, YESCODE)) {
Yap_NilError(RESOURCE_ERROR_STACK, LOCAL_ErrorMessage);
}
S = ASP;
S[E_CB] = (CELL)(LCL0 - cut_b);
}
return 1;
}
static int interrupt_cut(USES_REGS1) {
Term t_cut = MkIntegerTerm(LCL0 - (CELL *)YENV[E_CB]);
int v;
#ifdef DEBUG_INTERRUPTS
if (trace_interrupts)
fprintf(stderr, "[%d] %lu--%lu %s:%d (YENV=%p ENV=%p ASP=%p)\n", worker_id,
LOCAL_FirstActiveSignal, LOCAL_LastActiveSignal, __FUNCTION__,
__LINE__, YENV, ENV, ASP);
#endif
if ((v = check_alarm_fail_int(2 PASS_REGS)) >= 0) {
return v;
}
if (!Yap_has_a_signal() ||
Yap_only_has_signals(YAP_CDOVF_SIGNAL, YAP_CREEP_SIGNAL)) {
return 2;
}
/* find something to fool S */
P = NEXTOP(P, s);
return interrupt_handler_either(t_cut, PredRestoreRegs PASS_REGS);
}
static int interrupt_cut_t(USES_REGS1) {
Term t_cut = MkIntegerTerm(LCL0 - (CELL *)YENV[E_CB]);
int v;
#ifdef DEBUG_INTERRUPTS
if (trace_interrupts)
fprintf(stderr, "[%d] %lu--%lu %s:%d: (YENV=%p ENV=%p ASP=%p)\n", worker_id,
LOCAL_FirstActiveSignal, LOCAL_LastActiveSignal, __FUNCTION__,
__LINE__, YENV, ENV, ASP);
#endif
if ((v = check_alarm_fail_int(2 PASS_REGS)) >= 0) {
return v;
}
if (!Yap_has_a_signal() ||
Yap_only_has_signals(YAP_CDOVF_SIGNAL, YAP_CREEP_SIGNAL)) {
return 2;
}
/* find something to fool S */
P = NEXTOP(P, s);
return interrupt_handler_either(t_cut, PredRestoreRegs PASS_REGS);
}
static int interrupt_cut_e(USES_REGS1) {
Term t_cut = MkIntegerTerm(LCL0 - (CELL *)S[E_CB]);
int v;
#ifdef DEBUG_INTERRUPTS
if (trace_interrupts)
fprintf(stderr, "[%d] %lu--%lu %s:%d: (YENV=%p ENV=%p ASP=%p)\n", worker_id,
LOCAL_FirstActiveSignal, LOCAL_LastActiveSignal, __FUNCTION__,
__LINE__, YENV, ENV, ASP);
#endif
if ((v = check_alarm_fail_int(2 PASS_REGS)) >= 0) {
return v;
}
if (!Yap_only_has_signals(YAP_CDOVF_SIGNAL, YAP_CREEP_SIGNAL)) {
return 2;
}
/* find something to fool S */
P = NEXTOP(P, s);
return interrupt_handler_either(t_cut, PredRestoreRegs PASS_REGS);
}
static int interrupt_commit_y(USES_REGS1) {
int v;
Term t_cut = YENV[P->y_u.yps.y];
#ifdef DEBUG_INTERRUPTS
if (trace_interrupts)
fprintf(stderr, "[%d] %lu--%lu %s:%d: (YENV=%p ENV=%p ASP=%p)\n", worker_id,
LOCAL_FirstActiveSignal, LOCAL_LastActiveSignal, __FUNCTION__,
__LINE__, YENV, ENV, ASP);
#endif
if ((v = check_alarm_fail_int(2 PASS_REGS)) >= 0) {
return v;
}
if (!Yap_has_a_signal() ||
Yap_only_has_signals(YAP_CDOVF_SIGNAL, YAP_CREEP_SIGNAL)) {
return 2;
}
/* find something to fool S */
P = NEXTOP(P, yps);
return interrupt_handler_either(t_cut, PredRestoreRegs PASS_REGS);
}
static int interrupt_commit_x(USES_REGS1) {
int v;
Term t_cut = XREG(P->y_u.xps.x);
#ifdef DEBUG_INTERRUPTS
if (trace_interrupts)
fprintf(stderr, "[%d] %lu--%lu %s:%d (YENV=%p ENV=%p ASP=%p)\n", worker_id,
LOCAL_FirstActiveSignal, LOCAL_LastActiveSignal, __FUNCTION__,
__LINE__, YENV, ENV, ASP);
#endif
if ((v = check_alarm_fail_int(2 PASS_REGS)) >= 0) {
return v;
}
if (Yap_only_has_signals(YAP_CDOVF_SIGNAL, YAP_CREEP_SIGNAL)) {
return 2;
}
if (PP)
UNLOCKPE(1, PP);
PP = P->y_u.xps.p0;
/* find something to fool S */
if (P->opc == Yap_opcode(_fcall)) {
/* fill it up */
CACHE_Y_AS_ENV(YREG);
ENV_YREG[E_CP] = (CELL)CP;
ENV_YREG[E_E] = (CELL)ENV;
#ifdef DEPTH_LIMIT
ENV_YREG[E_DEPTH] = DEPTH;
#endif /* DEPTH_LIMIT */
ENDCACHE_Y_AS_ENV();
}
P = NEXTOP(P, xps);
return interrupt_handler_either(t_cut, PredRestoreRegs PASS_REGS);
}
static int interrupt_either(USES_REGS1) {
int v;
#ifdef DEBUGX
// if (trace_interrupts)
fprintf(stderr, "[%d] %s:%d: (YENV=%p ENV=%p ASP=%p)\n", worker_id,
__FUNCTION__, __LINE__, YENV, ENV, ASP);
#endif
if ((v = check_alarm_fail_int(2 PASS_REGS)) >= 0) {
return v;
}
if (Yap_only_has_signal(YAP_CREEP_SIGNAL)) {
return 2;
}
if (PP)
UNLOCKPE(1, PP);
PP = P->y_u.Osblp.p0;
/* find something to fool S */
SET_ASP(YENV, P->y_u.Osbpp.s);
if (ASP > (CELL *)PROTECT_FROZEN_B(B))
ASP = (CELL *)PROTECT_FROZEN_B(B);
if ((v = code_overflow(YENV PASS_REGS)) >= 0) {
return v;
}
// P = NEXTOP(P, Osblp);
if ((v = stack_overflow(
RepPredProp(Yap_GetPredPropByFunc(FunctorRestoreRegs1, 0)), YENV,
NEXTOP(P, Osblp), 0 PASS_REGS)) >= 0) {
// P = PREVOP(P, Osblp);
return v;
}
// P = PREVOP(P, Osblp);
return interrupt_handler_either(
MkIntTerm(0),
RepPredProp(Yap_GetPredPropByFunc(FunctorRestoreRegs1, 0)) PASS_REGS);
}
static int interrupt_dexecute(USES_REGS1) {
int v;
PredEntry *pe;
#ifdef DEBUG_INTERRUPTS
if (trace_interrupts)
fprintf(stderr, "[%d] %lu--%lu %s/%d (YENV=%p ENV=%p ASP=%p)\n", worker_id,
LOCAL_FirstActiveSignal, LOCAL_LastActiveSignal, __FUNCTION__,
__LINE__, YENV, ENV, ASP);
#endif
if (PP)
UNLOCKPE(1, PP);
PP = P->y_u.pp.p0;
pe = P->y_u.pp.p;
if ((pe->PredFlags & (NoTracePredFlag | HiddenPredFlag)) &&
Yap_only_has_signal(YAP_CREEP_SIGNAL)) {
return 2;
}
/* set S for next instructions */
ASP = YENV + E_CB;
if (ASP > (CELL *)PROTECT_FROZEN_B(B))
ASP = (CELL *)PROTECT_FROZEN_B(B);
if ((v = code_overflow(YENV PASS_REGS)) >= 0) {
return v;
}
if ((v = stack_overflow(P->y_u.pp.p, (CELL *)YENV[E_E], (yamop *)YENV[E_CP],
P->y_u.pp.p->ArityOfPE PASS_REGS)) >= 0) {
return v;
}
/* first, deallocate */
CP = (yamop *)YENV[E_CP];
ENV = YENV = (CELL *)YENV[E_E];
#ifdef DEPTH_LIMIT
YENV[E_DEPTH] = DEPTH;
#endif /* DEPTH_LIMIT */
#ifdef FROZEN_STACKS
{
choiceptr top_b = PROTECT_FROZEN_B(B);
#ifdef YAPOR_SBA
if (YENV > (CELL *)top_b || YENV < HR)
YENV = (CELL *)top_b;
#else
if (YENV > (CELL *)top_b)
YENV = (CELL *)top_b;
#endif /* YAPOR_SBA */
else
YENV = (CELL *)((CELL)YENV + ENV_Size(CPREG));
}
#else
if (YENV > (CELL *)B) {
YENV = (CELL *)B;
} else {
YENV = (CELL *)((CELL)YENV + ENV_Size(CPREG));
}
#endif /* FROZEN_STACKS */
/* setup GB */
YENV[E_CB] = (CELL)B;
/* and now CREEP */
return interrupt_handler(pe PASS_REGS);
}
static void undef_goal(USES_REGS1) {
PredEntry *pe = PredFromDefCode(P);
BEGD(d0);
/* avoid trouble with undefined dynamic procedures */
/* I assume they were not locked beforehand */
#if defined(YAPOR) || defined(THREADS)
if (!PP) {
PELOCK(19, pe);
PP = pe;
}
#endif
if (pe->PredFlags & (DynamicPredFlag | LogUpdatePredFlag | MultiFileFlag)) {
#if defined(YAPOR) || defined(THREADS)
UNLOCKPE(19, PP);
PP = NULL;
#endif
P = FAILCODE;
return;
}
#if defined(YAPOR) || defined(THREADS)
UNLOCKPE(19, PP);
PP = NULL;
#endif
d0 = pe->ArityOfPE;
if (d0 == 0) {
HR[1] = MkAtomTerm((Atom)(pe->FunctorOfPred));
} else {
HR[d0 + 2] = AbsAppl(HR);
*HR = (CELL)pe->FunctorOfPred;
HR++;
BEGP(pt1);
pt1 = XREGS + 1;
for (; d0 > 0; --d0) {
BEGD(d1);
BEGP(pt0);
pt0 = pt1++;
d1 = *pt0;
deref_head(d1, undef_unk);
undef_nonvar:
/* just copy it to the heap */
*HR++ = d1;
continue;
derefa_body(d1, pt0, undef_unk, undef_nonvar);
if (pt0 <= HR) {
/* variable is safe */
*HR++ = (CELL)pt0;
} else {
/* bind it, in case it is a local variable */
d1 = Unsigned(HR);
RESET_VARIABLE(HR);
HR += 1;
Bind_Local(pt0, d1);
}
ENDP(pt0);
ENDD(d1);
}
ENDP(pt1);
}
ENDD(d0);
HR[0] = Yap_Module_Name(pe);
ARG1 = (Term)AbsPair(HR);
ARG2 = Yap_getUnknownModule(Yap_GetModuleEntry(HR[0]));
HR += 2;
#ifdef LOW_LEVEL_TRACER
if (Yap_do_low_level_trace)
low_level_trace(enter_pred, UndefCode, XREGS + 1);
#endif /* LOW_LEVEL_TRACE */
P = UndefCode->CodeOfPred;
}
static void spy_goal(USES_REGS1) {
PredEntry *pe = PredFromDefCode(P);
#if defined(YAPOR) || defined(THREADS)
if (!PP) {
PELOCK(14, pe);
PP = pe;
}
#endif
BEGD(d0);
if (!(pe->PredFlags & IndexedPredFlag) && pe->cs.p_code.NOfClauses > 1) {
/* update ASP before calling IPred */
SET_ASP(YREG, E_CB * sizeof(CELL));
Yap_IPred(pe, 0, CP);
/* IPred can generate errors, it thus must get rid of the lock itself */
if (P == FAILCODE) {
#if defined(YAPOR) || defined(THREADS)
if (PP && !(PP->PredFlags & LogUpdatePredFlag)) {
UNLOCKPE(20, pe);
PP = NULL;
}
#endif
return;
}
}
/* first check if we need to increase the counter */
if ((pe->PredFlags & CountPredFlag)) {
LOCK(pe->StatisticsForPred->lock);
pe->StatisticsForPred->NOfEntries++;
UNLOCK(pe->StatisticsForPred->lock);
LOCAL_ReductionsCounter--;
if (LOCAL_ReductionsCounter == 0 && LOCAL_ReductionsCounterOn) {
#if defined(YAPOR) || defined(THREADS)
if (PP) {
UNLOCKPE(20, pe);
PP = NULL;
}
#endif
Yap_NilError(CALL_COUNTER_UNDERFLOW_EVENT, "");
return;