4 acast(Type *t, Node *n)
6 if(n->type->etype != t->etype || n->op == OBIT) {
8 if(nocast(n->left->type, t))
24 if(n == Z || n->type == T)
64 v = convvtox(l->vconst, n->type->etype);
73 d = l->fconst + r->fconst;
75 v = l->vconst + r->vconst;
81 d = l->fconst - r->fconst;
83 v = l->vconst - r->vconst;
88 d = l->fconst * r->fconst;
90 v = l->vconst * r->vconst;
95 v = (uvlong)l->vconst * (uvlong)r->vconst;
101 warn(n, "divide by zero");
105 d = l->fconst / r->fconst;
107 v = l->vconst / r->vconst;
112 warn(n, "divide by zero");
115 v = (uvlong)l->vconst / (uvlong)r->vconst;
120 warn(n, "modulo by zero");
123 v = l->vconst % r->vconst;
128 warn(n, "modulo by zero");
131 v = (uvlong)l->vconst % (uvlong)r->vconst;
135 v = l->vconst & r->vconst;
139 v = l->vconst | r->vconst;
143 v = l->vconst ^ r->vconst;
147 v = (uvlong)l->vconst >> r->vconst;
151 v = l->vconst >> r->vconst;
155 v = l->vconst << r->vconst;
159 v = (uvlong)l->vconst < (uvlong)r->vconst;
163 if(typefd[l->type->etype])
164 v = l->fconst < r->fconst;
166 v = l->vconst < r->vconst;
170 v = (uvlong)l->vconst > (uvlong)r->vconst;
174 if(typefd[l->type->etype])
175 v = l->fconst > r->fconst;
177 v = l->vconst > r->vconst;
181 v = (uvlong)l->vconst <= (uvlong)r->vconst;
185 if(typefd[l->type->etype])
186 v = l->fconst <= r->fconst;
188 v = l->vconst <= r->vconst;
192 v = (uvlong)l->vconst >= (uvlong)r->vconst;
196 if(typefd[l->type->etype])
197 v = l->fconst >= r->fconst;
199 v = l->vconst >= r->vconst;
203 if(typefd[l->type->etype])
204 v = l->fconst == r->fconst;
206 v = l->vconst == r->vconst;
210 if(typefd[l->type->etype])
211 v = l->fconst != r->fconst;
213 v = l->vconst != r->vconst;
217 if(typefd[l->type->etype])
224 if(typefd[l->type->etype])
225 v = l->fconst && r->fconst;
227 v = l->vconst && r->vconst;
231 if(typefd[l->type->etype])
232 v = l->fconst || r->fconst;
234 v = l->vconst || r->vconst;
240 n->vconst = convvtox(v, n->type->etype);
265 if(addo(n) && addo(l))
302 for(i=0; i<nterm; i++) {
303 print("%d %3lld ", i, term[i].mult);
304 prtree1(term[i].node, 1, 0);
312 acomcmp1(const void *a1, const void *a2)
343 acomcmp2(const void *a1, const void *a2)
362 acom2(Node *n, Type *t)
370 * copy into automatic
380 for(i=1; i<nt; i++) {
400 * prepare constant term,
401 * combine it with an addressing term
404 l = new1(OCONST, Z, Z);
408 for(i=1; i<nt; i++) {
415 l = new1(OADD, r, l);
423 * look for factorable terms
424 * c1*i + c1*c2*j -> c1*(i + c2*j)
426 qsort(trm+1, nt-1, sizeof(trm[0]), acomcmp1);
427 for(i=nt-1; i>=0; i--) {
433 for(j=i+1; j<nt; j++) {
442 if(r->type->etype != et)
444 c2 = trm[j].mult/trm[i].mult;
445 if(c2 != 1 && c2 != -1) {
446 r = new1(OMUL, r, new(OCONST, Z, Z));
449 r->right->vconst = c2;
452 if(l->type->etype != et)
454 r = new1(OADD, l, r);
464 for(i=0; i<nt; i++) {
465 print("%d %3lld ", i, trm[i].mult);
466 prtree1(trm[i].node, 1, 0);
471 * put it all back together
473 qsort(trm+1, nt-1, sizeof(trm[0]), acomcmp2);
475 for(i=nt-1; i>=0; i--) {
480 if(r->type->etype != et || r->op == OBIT)
482 if(c1 != 1 && c1 != -1) {
483 r = new1(OMUL, r, new(OCONST, Z, Z));
487 r->right->vconst = -c1;
490 r->right->vconst = c1;
501 l = new1(OADD, l, r);
503 l = new1(OSUB, l, r);
506 l = new1(OSUB, r, l);
509 l = new1(OADD, l, r);
513 r = new1(OCONST, 0, 0);
516 l = new1(OSUB, r, l);
523 acom1(vlong v, Node *n)
527 if(v == 0 || nterm >= NTERM)
531 if(!typefd[n->type->etype]) {
532 term[0].mult += v*n->vconst;
535 term[nterm].mult = v;
536 term[nterm].node = n;
564 if(!typefd[n->type->etype]) {
565 acom1(v*l->vconst, r);
569 if(!typefd[n->type->etype]) {
570 acom1(v*r->vconst, l);
585 if(!typefd[n->type->etype])
586 if(!typev[n->type->etype] || ewidth[TVLONG] == ewidth[TIND])
590 if(nilcast(n->left->type, n->type))
600 if(n->left->op == OCONST)
602 if(n->right->op == OCONST)