52static char SccsId[] =
"%W% %G%";
63#define COMPILER_NAMES 1
64#include "YapCompile.h"
66#include "YapCompile.h"
87#define CMEM_BLK_SIZE (4*4096)
88#define FIRST_CMEM_BLK_SIZE (16*4096)
94 size = (size + 7) & ((UInt)-8);
96 size = (size + 3) & ((UInt)0xfffffffc);
99 if (!cip->blks || cip->blk_cur+size > cip->blk_top) {
103 if (size > CMEM_BLK_SIZE)
104 blksz = size+
sizeof(
struct mem_blk);
106 blksz = CMEM_BLK_SIZE;
109 if (LOCAL_CMemFirstBlock) {
110 p = LOCAL_CMemFirstBlock;
111 blksz = LOCAL_CMemFirstBlockSz;
112 p->ublock.next = NULL;
114 if (blksz < FIRST_CMEM_BLK_SIZE)
115 blksz = FIRST_CMEM_BLK_SIZE;
116 p = (
struct mem_blk *)Yap_AllocCodeSpace(blksz);
118 LOCAL_Error_Size = size;
120 siglongjmp(cip->CompilerBotch, OUT_OF_HEAP_BOTCH);
122 LOCAL_CMemFirstBlock = p;
123 LOCAL_CMemFirstBlockSz = blksz;
126 p = (
struct mem_blk *)Yap_AllocCodeSpace(blksz);
129 LOCAL_Error_Size = size;
131 siglongjmp(cip->CompilerBotch, OUT_OF_HEAP_BOTCH);
134 p->ublock.next = cip->blks;
136 cip->blk_cur = p->contents;
137 cip->blk_top = (
char *)p+blksz;
140 char *out = cip->blk_cur;
141 cip->blk_cur += size;
146 if (ASP <= CellPtr (cip->freep) + 256) {
148 LOCAL_Error_Size = 256+((
char *)cip->freep - (
char *)HR);
150 siglongjmp(cip->CompilerBotch, OUT_OF_STACK_BOTCH);
165 struct mem_blk *nextp = p->ublock.next;
166 if (p != LOCAL_CMemFirstBlock)
167 Yap_FreeCodeSpace((ADDR)p);
171 if (cip->label_offset &&
172 cip->label_offset != LOCAL_LabelFirstArray) {
173 Yap_FreeCodeSpace((ADDR)cip->label_offset);
176 cip->label_offset = NULL;
182 return AllocCMem(size, cip);
186is_a_test(Term arg, Term mod)
188 if (IsVarTerm (arg)) {
191 if (IsVarTerm (arg) || !IsAtomTerm(mod)) {
194 if (IsAtomTerm (arg)) {
195 Atom At = AtomOfTerm (arg);
196 PredEntry *pe = RepPredProp(PredPropByAtom(At, mod));
199 return pe->PredFlags & TestPredFlag;
201 if (IsApplTerm (arg)) {
202 Functor f = FunctorOfTerm (arg);
204 if (f == FunctorModule) {
205 return is_a_test(ArgOfTerm(2,arg), ArgOfTerm(1,arg));
206 }
else if (f == FunctorComma) {
208 is_a_test(ArgOfTerm(1,arg), mod) &&
209 is_a_test(ArgOfTerm(2,arg), mod);
211 PredEntry *pe = RepPredProp(PredPropByFunc(f, mod));
215 if (pe->PredFlags & AsmPredFlag) {
216 int op = pe->PredFlags & 0x7f;
217 if (op >= _atom && op <= _eq) {
222 return pe->PredFlags & (TestPredFlag|BinaryPredFlag);
229Yap_is_a_test_pred (Term arg, Term mod)
231 return is_a_test(arg, mod);
235Yap_emit (compiler_vm_op o, Int r1, CELL r2,
struct intermediates *cip)
238 p = (
PInstr *) AllocCMem (
sizeof (*p), cip);
243 if (cip->cpc == NIL) {
244 cip->cpc = cip->CodeStart = p;
246 cip->cpc->nextInst = p;
252Yap_emit_3ops (compiler_vm_op o, CELL r1, CELL r2, CELL r3,
struct intermediates *cip)
255 p = (
PInstr *) AllocCMem (
sizeof (*p)+
sizeof(CELL), cip);
262 cip->cpc = cip->CodeStart = p;
265 cip->cpc->nextInst = p;
271Yap_emit_4ops (compiler_vm_op o, CELL r1, CELL r2, CELL r3, CELL r4,
struct intermediates *cip)
274 p = (
PInstr *) AllocCMem (
sizeof (*p)+2*
sizeof(CELL), cip);
282 cip->cpc = cip->CodeStart = p;
285 cip->cpc->nextInst = p;
291Yap_emit_5ops (compiler_vm_op o, CELL r1, CELL r2, CELL r3, CELL r4, CELL r5,
struct intermediates *cip)
294 p = (
PInstr *) AllocCMem (
sizeof (*p)+3*
sizeof(CELL), cip);
303 cip->cpc = cip->CodeStart = p;
306 cip->cpc->nextInst = p;
312Yap_emit_6ops (compiler_vm_op o, CELL r1, CELL r2, CELL r3, CELL r4, CELL r5, CELL r6,
struct intermediates *cip)
315 p = (
PInstr *) AllocCMem (
sizeof (*p)+4*
sizeof(CELL), cip);
325 cip->cpc = cip->CodeStart = p;
328 cip->cpc->nextInst = p;
334Yap_emit_7ops (compiler_vm_op o, CELL r1, CELL r2, CELL r3, CELL r4, CELL r5, CELL r6, CELL r7,
struct intermediates *cip)
337 p = (
PInstr *) AllocCMem (
sizeof (*p)+5*
sizeof(CELL), cip);
348 cip->cpc = cip->CodeStart = p;
351 cip->cpc->nextInst = p;
357Yap_emit_extra_size (compiler_vm_op o, CELL r1,
int size,
struct intermediates *cip)
360 p = (
PInstr *) AllocCMem (
sizeof (*p) + size - CellSize, cip);
365 cip->cpc = cip->CodeStart = p;
368 cip->cpc->nextInst = p;
375bip_name(Int op,
char *s)
406 strcpy(s,
"compound");
412 strcpy(s,
"primitive");
460Yap_bip_name(Int op,
char *s) {
467write_address(CELL address)
469 if (address < (CELL)AtomBase) {
470 Yap_DebugErrorPutc(
'L');
471 Yap_DebugPlWrite(MkIntTerm (address));
472 }
else if (address == (CELL) FAILCODE) {
473 Yap_DebugPlWrite (MkAtomTerm (AtomFail));
475 char buf[32], *p = buf;
478 snprintf(buf,32,
"%p",(
void *)address);
480 sprintf(buf,
"%p",(
void *)address);
486 Yap_DebugErrorPutc(*p++);
492write_special_label(special_label_op arg, special_label_id rn, UInt lab)
495 case SPECIAL_LABEL_INIT:
496 Yap_DebugErrorPuts(
"init,");
498 case SPECIAL_LABEL_EXCEPTION:
499 Yap_DebugErrorPuts(
"exception,");
501 case SPECIAL_LABEL_SUCCESS:
502 Yap_DebugErrorPuts(
"success,");
504 case SPECIAL_LABEL_FAILURE:
505 Yap_DebugErrorPuts(
"fail,");
509 case SPECIAL_LABEL_SET:
510 Yap_DebugErrorPuts(
"set,");
512 case SPECIAL_LABEL_CLEAR:
513 Yap_DebugErrorPuts(
"clear,");
515 case SPECIAL_LABEL_EXCEPTION:
516 Yap_DebugErrorPuts(
"exception");
518 case SPECIAL_LABEL_SUCCESS:
519 Yap_DebugErrorPuts(
"success");
521 case SPECIAL_LABEL_FAILURE:
522 Yap_DebugErrorPuts(
"fail");
531 if (IsExtensionFunctor(f)) {
532 if (f == FunctorDBRef) {
533 Yap_DebugPlWrite(MkAtomTerm(AtomDBREF));
534 }
else if (f == FunctorLongInt) {
535 Yap_DebugPlWrite(MkAtomTerm(AtomLONGINT));
536 }
else if (f == FunctorBigInt) {
537 Yap_DebugPlWrite(MkAtomTerm(AtomLONGINT));
538 }
else if (f == FunctorDouble) {
539 Yap_DebugPlWrite(MkAtomTerm(AtomDOUBLE));
540 }
else if (f == FunctorString) {
541 Yap_DebugPlWrite(MkAtomTerm(AtomSTRING));
544 Yap_DebugPlWrite(MkAtomTerm(NameOfFunctor (f)));
545 Yap_DebugErrorPutc (
'/');
546 Yap_DebugPlWrite(MkIntTerm(ArityOfFunctor (f)));
554 UInt arity = p->ArityOfPE;
555 Term mod = TermProlog;
557 if (p->ModuleOfPred) mod = p->ModuleOfPred;
558 Yap_DebugPlWrite (mod);
559 Yap_DebugErrorPutc (
':');
561 Yap_DebugPlWrite (MkAtomTerm ((
Atom)f));
563 Yap_DebugPlWrite (MkAtomTerm (NameOfFunctor (f)));
564 Yap_DebugErrorPutc (
'/');
565 Yap_DebugPlWrite (MkIntTerm (arity));
570ShowOp (compiler_vm_op ic,
const char *f,
struct PSEUDO *cpc)
576 CELL *cptr = cpc->arnds;
578 if (ic != label_op && ic != label_ctl_op && ic != name_op) {
579 Yap_DebugErrorPutc (
'\t');
581 while ((ch = *f++) != 0)
588 Yap_DebugPlWrite(MkIntTerm(rn));
591 Yap_DebugPlWrite(MkIntTerm(arg));
597 Yap_DebugErrorPutc (v->KindOfVE == PermVar ?
'Y' :
'X');
598 Yap_DebugPlWrite (MkIntTerm ((v->NoOfVE) & MaskVarAdrs));
599 Yap_DebugErrorPutc (
',');
600 Yap_DebugErrorPutc (
'A');
601 Yap_DebugPlWrite (MkIntegerTerm (cpc->rnd4));
602 Yap_DebugErrorPutc (
',');
603 send_pred( RepPredProp((
Prop)(cpc->rnd5)) );
609 Yap_DebugPlWrite ((Term) arg);
614 int max = arg/(8*
sizeof(CELL)), i;
616 for (i = 0; i <= max; i++) {
617 Yap_DebugPlWrite(MkIntegerTerm((Int)(*ptr++)));
625 write_special_label (arg, rn, cpc->rnd3);
632 Yap_DebugPlWrite (MkAtomTerm(Yap_LookupAtom(s)));
636 Yap_DebugPlWrite (MkIntegerTerm (arg));
639 Yap_DebugPlWrite (MkIntTerm (cpc->rnd3));
645 Yap_DebugErrorPutc (v->KindOfVE == PermVar ?
'Y' :
'X');
646 Yap_DebugPlWrite (MkIntTerm ((v->NoOfVE) & MaskVarAdrs));
657 Yap_DebugErrorPutc (v->KindOfVE == PermVar ?
'Y' :
'X');
658 Yap_DebugPlWrite (MkIntTerm ((v->NoOfVE) & MaskVarAdrs));
662 Yap_DebugPlWrite (MkAtomTerm ((
Atom) arg));
663 Yap_DebugErrorPutc (
'/');
664 Yap_DebugPlWrite (MkIntTerm (rn));
667 send_pred( RepPredProp((
Prop)(arg) ));
670 send_pred( RepPredProp((
Prop)(rn) ));
676 Yap_DebugErrorPutc (
'A');
677 Yap_DebugPlWrite (MkIntTerm (rn));
680 Yap_DebugErrorPutc (
'S');
681 Yap_DebugPlWrite (MkIntTerm (rn));
685 CELL my_arg = *cptr++;
686 write_address(my_arg);
698 if (IsExtensionFunctor(fun)) {
699 if (fun == FunctorDBRef) {
700 Yap_DebugPlWrite(MkAtomTerm(AtomDBREF));
701 }
else if (fun == FunctorLongInt) {
702 Yap_DebugPlWrite(MkAtomTerm(AtomLONGINT));
703 }
else if (fun == FunctorDouble) {
704 Yap_DebugPlWrite(MkAtomTerm(AtomDOUBLE));
705 }
else if (fun == FunctorString) {
706 Yap_DebugPlWrite(MkAtomTerm(AtomSTRING));
709 Yap_DebugPlWrite (MkAtomTerm(NameOfFunctor(fun)));
710 Yap_DebugErrorPutc (
'/');
711 Yap_DebugPlWrite (MkIntTerm(ArityOfFunctor(fun)));
716 Yap_DebugPlWrite(AbsAppl(cptr));
719 Yap_DebugPlWrite (MkIntTerm (rn >> 1));
720 Yap_DebugErrorPutc (
'\t');
721 Yap_DebugPlWrite (MkIntTerm (rn & 1));
724 Yap_DebugPlWrite (arg);
727 Yap_DebugPlWrite ((Term) * cptr++);
731 CELL *ptr = (CELL *)cptr[0];
732 for (i = 0; i < arg; ++i) {
734 Yap_DebugErrorPutc(
'\t');
736 Yap_DebugPlWrite ((Term) *ptr++);
738 Yap_DebugPlWrite (MkIntTerm (0));
741 Yap_DebugErrorPutc (
'\t');
743 write_address (my_arg);
745 Yap_DebugErrorPutc (
'\n');
752 CELL *ptr = (CELL *)cptr[0];
753 for (i = 0; i < arg; ++i) {
754 CELL my_arg = ptr[0], lbl = ptr[1];
755 Yap_DebugErrorPutc(
'\t');
757 write_functor((
Functor)my_arg);
759 Yap_DebugPlWrite(MkIntTerm (0));
761 Yap_DebugErrorPutc(
'\t');
765 Yap_DebugErrorPutc(
'\n');
770 Yap_DebugErrorPutc (
'%');
771 Yap_DebugErrorPutc (ch);
774 Yap_DebugErrorPutc (ch);
776 Yap_DebugErrorPutc (
'\n');
785 cpc = cint->CodeStart;
787 HR = (CELL *)cint->freep;
789 compiler_vm_op ic = cpc->op;
791 ShowOp (ic, opDesc[ic], cpc);
795 Yap_DebugErrorPutc (
'\n');