49 int i, num = AC.cbufList.num;
51 for ( i = 0; i < num; i++, C++ ) {
52 if ( C->
Buffer == 0 )
break;
54 if ( i >= num ) C = (
CBUF *)FromList(&AC.cbufList);
61 C->
lhs = (WORD **)Malloc1(C->maxlhs*
sizeof(WORD *),
"compiler buffer-2");
65 C->
rhs = (WORD **)Malloc1(C->maxrhs*(
sizeof(WORD *)+2*
sizeof(LONG)+2*
sizeof(WORD)),
"compiler buffer-3");
74 RedoTree(C,C->maxrhs);
93 if ( C->
rhs ) M_free(C->
rhs,
"compiler buffer-3");
94 if ( C->
lhs ) M_free(C->
lhs,
"compiler buffer-2");
102 C->numlhs = C->numrhs = C->maxlhs = C->maxrhs = C->mnumlhs =
103 C->mnumrhs = C->numtree = C->rootnum = C->MaxTreeSize = 0;
121 C->numrhs = C->numlhs = 0;
126 C->numtree = C->rootnum = C->MaxTreeSize = 0;
127 RedoTree(C,C->maxrhs);
145 CBUF *C = cbuf + num;
147 WORD *newbuffer = (WORD *)Malloc1(newsize*
sizeof(WORD),
"compiler buffer-4");
156 w1 = C->
Buffer; w2 = newbuffer;
159 while ( --j >= 0 ) *w2++ = *w1++;
162 *w2++ = *w1++; *w2++ = *w1++; *w2++ = *w1++; *w2++ = *w1++;
163 *w2++ = *w1++; *w2++ = *w1++; *w2++ = *w1++; *w2++ = *w1++;
165 offset = newbuffer - C->
Buffer;
166 for ( i = 0; i <= C->numlhs; i++ ) C->
lhs[i] += offset;
167 for ( i = 1; i <= C->numrhs; i++ ) C->
rhs[i] += offset;
170 C->
Top = newbuffer + newsize;
173 M_free(w1,
"DoubleCbuffer");
190 CBUF *C = cbuf + num;
192 if ( C->numlhs >= (C->maxlhs-2) ) {
193 WORD ***ppp = &(C->
lhs);
194 if ( DoubleList((VOID ***)ppp,&(C->maxlhs),
sizeof(WORD *),
195 "statement lists") ) Terminate(-1);
198 C->
lhs[C->numlhs+1] = 0;
216 LONG fullsize, *lold, newsize;
222 if ( C->numrhs >= (C->maxrhs-2) ) {
223 if ( C->maxrhs == 0 ) newsize = 100;
224 else newsize = C->maxrhs * 2;
225 if ( newsize > MAXCOMBUFRHS ) newsize = MAXCOMBUFRHS;
226 if ( newsize == C->maxrhs ) {
227 if ( AC.tablefilling ) {
228 TABLES T = functions[AC.tablefilling].tabl;
234 WORD *nbufs = (WORD *)Malloc1(new1*
sizeof(WORD),
"Table compile buffers");
237 for ( ; i < new1; i++ ) nbufs[i] = 0;
238 M_free(T->
buffers,
"Table compile buffers");
243 AC.cbufnum = num = T->
bufnum;
247 MesPrint(
"@Compiler buffer overflow. Try to make modules smaller");
252 fullsize = newsize * (
sizeof(WORD *) + 2*
sizeof(LONG) + 2*
sizeof(WORD));
253 C->
rhs = (WORD **)Malloc1(fullsize,
"subexpression lists");
254 for ( i = 0; i < C->maxrhs; i++ ) C->
rhs[i] = old[i];
256 for ( i = 0; i < C->maxrhs; i++ ) C->
CanCommu[i] = lold[i];
258 for ( i = 0; i < C->maxrhs; i++ ) C->
NumTerms[i] = lold[i];
260 for ( i = 0; i < C->maxrhs; i++ ) C->
numdum[i] = wold[i];
262 for ( i = 0; i < C->maxrhs; i++ ) C->
dimension[i] = wold[i];
263 if ( old ) M_free(old,
"subexpression lists");
265 if ( type == 0 ) RedoTree(C,C->maxrhs);
291 CBUF *C = cbuf+AC.cbufnum;
293 MesPrint(
"LH: %a",n,array);
297 for ( i = 0; i < n; i++ ) *(C->
Pointer)++ = *array++;
317 int AddNtoC(
int bufnum,
int n, WORD *array,
int par)
321 CBUF *C = cbuf+bufnum;
323 MesPrint(
"RH: %a",n,array);
327 for ( i = 0; i < n; i++ ) *w++ = *array++;
353 static COMPTREE comptreezero = {0,0,0,0,0,0};
355 int InsTree(
int bufnum,
int h)
357 CBUF *C = cbuf + bufnum;
362 if ( C->numtree + 1 >= C->MaxTreeSize ) {
363 if ( C->MaxTreeSize == 0 ) {
365 C->MaxTreeSize = 125;
377 for ( ip = 1; ip < C->MaxTreeSize; ip++ ) { C->
boomlijst[ip] = comptreezero; }
380 is = C->MaxTreeSize * 2;
382 for ( ip = 0; ip < C->MaxTreeSize; ip++ ) { s[ip] = C->
boomlijst[ip]; }
383 for ( ip = C->MaxTreeSize; ip <= is; ip++ ) { s[ip] = comptreezero; }
389 q = boomlijst + C->rootnum;
392 if ( q->right == -1 ) {
394 s = boomlijst+C->numtree;
395 q->right = C->numtree;
396 s->parent = C->rootnum;
397 s->left = s->right = -1;
406 v1 = C->
rhs[p->value]; v2 = v3 = C->
rhs[h];
407 while ( *v3 ) v3 += *v3;
408 while ( *v1 == *v2 && v2 < v3 ) { v1++; v2++; }
411 if ( iq >= 0 ) { ip = iq; }
417 s->
parent = ip; s->left = s->right = -1;
418 s->blnce = 0; s->value = h; s->usage = 1;
420 if ( p->blnce == 0 )
return(h);
424 else if ( *v1 < *v2 ) {
426 if ( iq >= 0 ) { ip = iq; }
432 s->parent = ip; s->left = s->right = -1;
433 s->blnce = 0; s->value = h; s->usage = 1;
435 if ( p->blnce == 0 )
return(h);
444 MesPrint(
"We vallen uit de boom!");
451 if ( iq == C->rootnum )
break;
453 if ( ip == q->left ) q->
blnce--;
455 if ( q->blnce == 0 )
break;
456 if ( q->blnce == -2 ) {
457 if ( p->blnce == -1 ) {
460 p->parent = q->parent;
462 if ( boomlijst[p->parent].left == iq ) boomlijst[p->parent].
left = ip;
463 else boomlijst[p->parent].
right = ip;
464 if ( q->left >= 0 ) boomlijst[q->left].
parent = iq;
465 q->blnce = p->blnce = 0;
473 if ( p->right >= 0 ) boomlijst[p->right].
parent = ip;
474 if ( q->left >= 0 ) boomlijst[q->left].
parent = iq;
475 s->parent = q->parent;
478 if ( boomlijst[s->parent].left == iq )
479 boomlijst[s->parent].
left = is;
480 else boomlijst[s->parent].
right = is;
481 if ( s->blnce > 0 ) { q->blnce = s->blnce = 0; p->blnce = -1; }
482 else if ( s->blnce < 0 ) { p->blnce = s->blnce = 0; q->blnce = 1; }
483 else { p->blnce = s->blnce = q->blnce = 0; }
487 else if ( q->blnce == 2 ) {
488 if ( p->blnce == 1 ) {
491 p->parent = q->parent;
493 if ( boomlijst[p->parent].left == iq ) boomlijst[p->parent].
left = ip;
494 else boomlijst[p->parent].
right = ip;
495 if ( q->right >= 0 ) boomlijst[q->right].
parent = iq;
496 q->blnce = p->blnce = 0;
504 if ( p->left >= 0 ) boomlijst[p->left].
parent = ip;
505 if ( q->right >= 0 ) boomlijst[q->right].
parent = iq;
506 s->parent = q->parent;
509 if ( boomlijst[s->parent].left == iq ) boomlijst[s->parent].
left = is;
510 else boomlijst[s->parent].
right = is;
511 if ( s->blnce < 0 ) { q->blnce = s->blnce = 0; p->blnce = 1; }
512 else if ( s->blnce > 0 ) { p->blnce = s->blnce = 0; q->blnce = -1; }
513 else { p->blnce = s->blnce = q->blnce = 0; }
533 int FindTree(
int bufnum, WORD *subexpr)
535 CBUF *C = cbuf + bufnum;
543 v1 = C->
rhs[p->value]; v2 = v3 = subexpr;
544 while ( *v3 ) v3 += *v3;
545 while ( *v1 == *v2 && v2 < v3 ) { v1++; v2++; }
548 if ( iq >= 0 ) { ip = iq; }
551 else if ( *v1 < *v2 ) {
553 if ( iq >= 0 ) { ip = iq; }
569 void RedoTree(
CBUF *C,
int size)
573 newboomlijst = (
COMPTREE *)Malloc1((size+1)*
sizeof(
COMPTREE),
"newboomlijst");
575 if ( C->MaxTreeSize > size ) C->MaxTreeSize = size;
576 for ( i = 0; i < C->MaxTreeSize; i++ ) newboomlijst[i] = C->
boomlijst[i];
580 C->MaxTreeSize = size;
588 void ClearTree(
int i)
616 CBUF *C = cbuf + bufnum;
620 C->maxrhs = AM.fbuffersize;
621 C->MaxTreeSize = AM.fbuffersize;
628 if ( C->
rhs ) M_free(C->
rhs,
"IniFbuffer-rhs");
640 for ( i = 1; i < C->MaxTreeSize; i++ ) { C->
boomlijst[i] = comptreezero; }
642 fullsize = (C->maxrhs+1) * (
sizeof(WORD *) + 2*
sizeof(LONG) + 2*
sizeof(WORD));
643 C->
rhs = (WORD **)Malloc1(fullsize,
"IniFbuffer-rhs");
659 LONG numcommute(WORD *terms, LONG *numterms)
669 if ( *t >= FUNCTION ) {
670 if ( functions[*t-FUNCTION].commute ) { num++;
break; }
674 terms = terms + *terms;