4 cgen(Node *n, int result, Node *nn)
7 int lg, rg, xg, yg, g, o;
11 if(n == Z || n->type == T)
13 if(typesuv[n->type->etype]) {
14 sugen(n, result, nn, n->type->width);
21 print("result = %R\n", result);
27 if(n->addable >= INDEXED) {
28 if(result == D_NONE) {
40 gmove(n->type, nn->type, D_TREE, n, result, nn);
47 diag(n, "unknown op in cgen: %O", o);
54 * recursive use of result
57 if(l->addable > INDEXED)
58 if(l->complex < FNX) {
64 * function calls on both sides
66 if(l->complex >= FNX && r->complex >= FNX) {
73 gmove(r->type, l->type, D_TOS, r, lg, l);
75 gmove(l->type, nn->type, lg, l, result, nn);
82 if(r->complex >= l->complex) {
84 * right side before left
86 if(result != D_NONE) {
87 rg = regalloc(n->type, result);
90 if(r->complex >= FNX || r->addable < INDEXED) {
91 rg = regalloc(r->type, result);
94 if(l->addable < INDEXED) {
103 if(l->complex >= FNX || l->addable < INDEXED) {
108 if(result != D_NONE) {
109 rg = regalloc(n->type, result);
112 if(r->addable < INDEXED) {
113 rg = regalloc(r->type, result);
117 if(result != D_NONE) {
118 gmove(n->type, l->type, rg, r, lg, l);
119 gmove(n->type, nn->type, rg, r, result, nn);
121 gmove(r->type, l->type, rg, r, lg, l);
128 rg = regalloc(tfield, result);
129 if(l->complex >= r->complex) {
130 lg = regaddr(D_NONE);
136 lg = regaddr(D_NONE);
140 g = regalloc(n->type, D_NONE);
141 gmove(l->type, l->type, lg, l, g, l);
142 bitstore(l, rg, lg, g, result, nn);
146 if(result == D_NONE) {
150 g = bitload(n, D_NONE, D_NONE, result, nn);
151 gopcode(OAS, nn->type, g, n, result, nn);
156 sugen(l, D_TREE, nodrat, l->type->width);
157 if(result != D_NONE) {
158 warn(n, "non-interruptable temporary");
160 if(!r || r->op != OCONST) {
161 diag(n, "DOT and no offset");
164 nod.xoffset += r->vconst;
166 cgen(&nod, result, nn);
176 if(typefd[n->type->etype])
179 if(l->complex >= FNX || r->complex >= FNX) {
184 if(r->addable < INDEXED) {
185 rg = regalloc(n->type, D_NONE);
190 lg = regaddr(D_NONE);
191 lcgen(l, lg, Z); /* destroys register optimization */
195 gmove(l->type, n->type, lg, l, g, n);
198 gopcode(o, n->type, rg, r, g, n);
199 if(o == OASLMOD || o == OASMOD)
200 gmove(n->type, l->type, g+1, n, lg, l);
202 gmove(n->type, l->type, g, n, lg, l);
204 if(o == OASLMOD || o == OASMOD)
205 gmove(n->type, nn->type, g+1, n, result, nn);
207 gmove(n->type, nn->type, g, n, result, nn);
219 if(l->complex >= FNX ||
220 l->addable < INDEXED ||
222 typefd[n->type->etype])
225 if(r->op != OCONST) {
226 rg = regalloc(n->type, D_NONE);
229 gopcode(o, l->type, rg, r, D_TREE, l);
237 l->addable < INDEXED ||
239 typefd[n->type->etype])
242 if(v > 0 && v <= 8) {
243 gopcode(o, n->type, D_TREE, r, D_TREE, l);
246 rg = regalloc(n->type, D_NONE);
248 gopcode(o, n->type, rg, r, D_TREE, l);
257 l->addable < INDEXED ||
259 typefd[n->type->etype])
263 if(v <= 0 || v > 8) {
264 rg = regalloc(n->type, D_NONE);
267 lg = regalloc(n->type, D_NONE);
269 gopcode(o, n->type, rg, r, lg, l);
270 gmove(n->type, n->type, lg, l, D_TREE, l);
281 if(l->complex >= FNX || r->complex >= FNX) {
286 if(r->addable < INDEXED) {
287 rg = regalloc(n->type, D_NONE);
290 if(o == OASLSHR || o == OASASHR || o == OASASHL) {
292 if(v <= 0 || v > 8) {
293 rg = regalloc(n->type, D_NONE);
300 lg = regaddr(D_NONE);
301 lcgen(l, lg, Z); /* destroys register optimization */
304 g = regalloc(n->type, result);
305 gmove(l->type, n->type, lg, l, g, n);
310 rg = regalloc(n->type, D_NONE);
313 if(o == OASXOR || o == OASLSHR || o == OASASHR || o == OASASHL)
315 rg = regalloc(n->type, D_NONE);
316 gmove(n->type, n->type, D_TOS, n, rg, n);
318 gopcode(o, n->type, rg, r, g, n);
319 gmove(n->type, l->type, g, n, lg, l);
321 gmove(n->type, nn->type, g, n, result, nn);
328 rg = regaddr(D_NONE);
329 lg = regalloc(tfield, D_NONE);
330 if(l->complex >= r->complex) {
331 g = bitload(l, lg, rg, result, nn);
332 xg = regalloc(r->type, D_NONE);
335 xg = regalloc(r->type, D_NONE);
337 g = bitload(l, lg, rg, result, nn);
340 if(!typefd[n->type->etype]) {
341 if(o == OASLDIV || o == OASDIV) {
342 yg = regpair(result);
343 gmove(tfield, n->type, g, l, yg, n);
344 gopcode(o, n->type, xg, r, yg, n);
345 gmove(n->type, tfield, yg, n, g, l);
350 bitstore(l, g, rg, lg, D_NONE, nn);
353 if(o == OASLMOD || o == OASMOD) {
354 yg = regpair(result);
355 gmove(tfield, n->type, g, l, yg, n);
356 gopcode(o, n->type, xg, r, yg, n);
357 gmove(n->type, tfield, yg+1, n, g, l);
362 bitstore(l, g, rg, lg, D_NONE, nn);
367 yg = regalloc(n->type, result);
368 gmove(tfield, n->type, g, l, yg, n);
369 gopcode(o, n->type, xg, r, yg, n);
370 gmove(n->type, tfield, yg, n, g, l);
374 bitstore(l, g, rg, lg, D_NONE, nn);
378 if(result == D_NONE) {
383 if(l->complex >= FNX)
384 lg = regret(l->type);
386 if(nocast(l->type, n->type)) {
387 gmove(n->type, nn->type, lg, l, result, nn);
391 if(nocast(n->type, nn->type)) {
392 gmove(l->type, n->type, lg, l, result, nn);
396 rg = regalloc(n->type, result);
397 gmove(l->type, n->type, lg, l, rg, n);
398 gmove(n->type, nn->type, rg, n, result, nn);
405 boolgen(l, 1, D_NONE, Z, l);
410 cgen(r->left, result, nn);
411 doinc(r->left, POST);
416 doinc(r->right, PRE);
417 cgen(r->right, result, nn);
418 doinc(r->right, POST);
424 if(result == D_NONE) {
428 lg = nodalloc(types[TIND], result, &nod);
429 nod.lineno = n->lineno;
431 if(l->left->op == OCONST) {
432 nod.xoffset += l->left->vconst;
435 if(l->right->op == OCONST) {
436 nod.xoffset += l->right->vconst;
441 gmove(n->type, nn->type, D_TREE, &nod, result, nn);
450 if(l->addable < INDEXED) {
451 lg = regaddr(result);
458 gopcode(OFUNC, types[TCHAR], D_NONE, Z, lg, l);
462 if(result != D_NONE) {
463 lg = regret(n->type);
464 gmove(n->type, nn->type, lg, n, result, nn);
472 if(result == D_NONE) {
476 if(typefd[n->type->etype])
478 if(r->addable >= INDEXED && r->complex < FNX) {
479 lg = regpair(result);
485 lg = regpair(result);
490 gopcode(o, n->type, rg, r, lg, l);
491 if(o == OMOD || o == OLMOD)
492 gmove(l->type, nn->type, lg+1, l, result, nn);
494 gmove(l->type, nn->type, lg, l, result, nn);
502 if(mulcon(r, l, result, nn))
505 if(mulcon(l, r, result, nn))
508 print("%L multiply\n", n->lineno);
513 if(typeil[n->type->etype])
515 if(typec[l->left->type->etype])
516 if(!(r->vconst & ~0xff)) {
520 if(typeh[l->left->type->etype])
521 if(!(r->vconst & ~0xffff)) {
530 if(r->addable >= INDEXED)
532 if(typeil[l->type->etype]) {
534 if(v > -32768 && v < 32768) {
535 rg = regaddr(D_NONE);
536 gmove(r->type, r->type, D_TREE, r, rg, r);
537 gopcode(OADDR, types[TSHORT], D_NONE, Z, rg, r);
539 p->to.type |= I_INDIR;
547 if(l->addable >= INDEXED)
549 if(typeil[r->type->etype]) {
551 if(v > -32768 && v < 32768) {
554 lg = regaddr(D_NONE);
555 gmove(l->type, l->type, D_TREE, l, lg, l);
556 gopcode(OADDR, types[TSHORT], D_NONE, Z, lg, l);
558 p->to.type |= I_INDIR;
568 if(result == D_NONE) {
572 if(l->complex >= FNX && r->complex >= FNX) {
575 lg = regalloc(l->type, result);
579 rg = regalloc(r->type, D_NONE);
580 gmove(r->type, r->type, D_TOS, r, rg, r);
581 gopcode(o, n->type, rg, r, lg, l);
584 gopcode(o, n->type, D_TOS, r, lg, l);
585 gmove(n->type, nn->type, lg, l, result, nn);
589 if(l->complex >= r->complex) {
590 if(l->op == OADDR && (o == OADD || o == OSUB))
591 lg = regaddr(result);
593 lg = regalloc(l->type, result);
595 rg = eval(r, D_NONE);
597 rg = regalloc(r->type, D_NONE);
599 lg = regalloc(l->type, result);
604 rg = regalloc(r->type, D_NONE);
608 rg = regalloc(r->type, D_NONE);
609 gmove(r->type, r->type, D_TOS, r, rg, r);
612 gopcode(o, n->type, rg, r, lg, l);
613 gmove(n->type, nn->type, lg, l, result, nn);
620 if(shlcon(l, r, result, nn))
624 if(result == D_NONE) {
629 if(l->complex >= FNX && r->complex >= FNX) {
632 lg = regalloc(l->type, result);
635 rg = regalloc(r->type, D_NONE);
636 gopcode(OAS, r->type, D_TOS, r, rg, r);
637 gopcode(n->op, n->type, rg, r, lg, l);
638 gmove(n->type, nn->type, lg, l, result, nn);
643 if(l->complex >= r->complex) {
644 lg = regalloc(l->type, result);
647 if(v <= 0 || v > 8) {
648 rg = regalloc(r->type, D_NONE);
651 rg = eval(r, D_NONE);
653 rg = regalloc(r->type, D_NONE);
655 lg = regalloc(l->type, result);
658 gopcode(o, n->type, rg, r, lg, l);
659 gmove(n->type, nn->type, lg, l, result, nn);
666 if(result == D_NONE) {
670 lg = regalloc(l->type, result);
672 gopcode(o, l->type, D_NONE, Z, lg, l);
673 gmove(n->type, nn->type, lg, l, result, nn);
678 if(result == D_NONE) {
682 lcgen(l, result, nn);
695 if(result == D_NONE) {
699 boolgen(n, 1, result, nn, Z);
704 boolgen(n, 1, result, nn, Z);
717 if(result == D_NONE) {
721 boolgen(n, 1, result, nn, Z);
727 if(l->type->etype == TIND)
728 v = l->type->link->width;
737 if(l->addable < INDEXED) {
738 lg = regaddr(D_NONE);
743 gmove(l->type, nn->type, lg, l, result, nn);
744 if(typefd[n->type->etype]) {
745 rg = regalloc(n->type, D_NONE);
746 gmove(l->type, l->type, lg, l, rg, l);
747 gopcode(o, n->type, D_CONST, nodconst(1), rg, l);
748 gmove(l->type, l->type, rg, l, lg, l);
752 gopcode(o, n->type, D_CONST, nodconst(-v), lg, l);
754 gopcode(o, n->type, D_CONST, nodconst(v), lg, l);
762 if(l->type->etype == TIND)
763 v = l->type->link->width;
771 if(l->addable < INDEXED) {
772 lg = regaddr(D_NONE);
776 if(typefd[n->type->etype]) {
777 rg = regalloc(n->type, D_NONE);
778 gmove(l->type, l->type, lg, l, rg, l);
779 gopcode(o, n->type, D_CONST, nodconst(1), rg, l);
780 gmove(l->type, l->type, rg, l, lg, l);
784 gopcode(o, n->type, D_CONST, nodconst(-v), lg, l);
786 gopcode(o, n->type, D_CONST, nodconst(v), lg, l);
789 gmove(l->type, nn->type, lg, l, result, nn);
794 rg = regaddr(D_NONE);
795 lg = regalloc(tfield, D_NONE);
796 if(result != D_NONE && (o == OPOSTINC || o == OPOSTDEC)) {
797 g = bitload(l, lg, rg, D_NONE, nn);
799 gmove(l->type, nn->type, g, l, result, nn);
801 gopcode(o, n->type, D_CONST, nodconst(-v), g, n);
803 gopcode(o, n->type, D_CONST, nodconst(v), g, n);
804 bitstore(l, g, rg, lg, D_NONE, nn);
807 g = bitload(l, lg, rg, result, nn);
809 gopcode(o, n->type, D_CONST, nodconst(-v), g, n);
811 gopcode(o, n->type, D_CONST, nodconst(v), g, n);
813 gmove(l->type, nn->type, g, l, result, nn);
814 bitstore(l, g, rg, lg, D_NONE, nn);
820 lcgen(Node *n, int result, Node *nn)
826 if(n == Z || n->type == T)
830 prtree(nn, "result");
832 print("result = %R\n", result);
837 nn->type = types[TIND];
841 cgen(n->left, D_NONE, n->left);
842 doinc(n->left, POST);
843 doinc(n->right, PRE);
844 lcgen(n->right, result, nn);
849 boolgen(n->left, 1, D_NONE, Z, n->left);
853 doinc(n->right->left, PRE);
854 lcgen(n->right->left, result, nn);
855 doinc(n->right->left, POST);
860 doinc(n->right->right, PRE);
861 lcgen(n->right->right, result, nn);
862 doinc(n->right->right, POST);
868 if(n->addable >= INDEXED) {
869 if(result >= D_A0 && result < D_A0+NREG) {
870 gopcode(OADDR, types[TLONG], D_TREE, n, result, nn);
873 if(result == D_TOS) {
874 gopcode(OADDR, types[TSHORT], D_NONE, nn, D_TREE, n);
878 cgen(n->left, result, nn);
882 if(n->addable < INDEXED) {
883 diag(n, "unknown op in lcgen: %O", n->op);
886 if(result >= D_A0 && result < D_A0+NREG) {
887 gopcode(OADDR, types[TLONG], D_TREE, n, result, nn);
890 if(result == D_TOS) {
891 gopcode(OADDR, types[TSHORT], D_NONE, nn, D_TREE, n);
894 lg = regaddr(result);
895 gopcode(OADDR, types[TLONG], D_TREE, n, lg, nn);
896 gopcode(OAS, nn->type, lg, nn, result, nn);
903 bcgen(Node *n, int true)
906 boolgen(n, true, D_NONE, Z, Z);
910 boolgen(Node *n, int true, int result, Node *nn, Node *post)
919 prtree(nn, "result");
921 print("result = %R\n", result);
922 prtree(n, "boolgen");
929 lg = eval(n, result);
930 if(lg >= D_A0 && lg < D_A0+NREG) {
931 rg = regalloc(types[TLONG], D_NONE);
932 gopcode(OAS, types[TLONG], lg, n, rg, Z);
935 gopcode(OTST, n->type, D_NONE, Z, lg, n);
938 fp = typefd[n->type->etype];
954 boolgen(l, !true, result, nn, post);
959 boolgen(l, 1, D_NONE, Z, l);
964 boolgen(r->left, true, result, nn, r->left);
965 if(result != D_NONE) {
966 doinc(r->left, POST);
971 doinc(r->right, PRE);
972 boolgen(r->right, !true, result, nn, r->right);
973 doinc(r->right, POST);
983 doinc(r->right, PRE);
984 boolgen(r->right, !true, result, nn, r->right);
987 if(doinc(post, POST|TEST)) {
988 lg = regalloc(types[TSHORT], D_NONE);
989 gopcode(OAS, types[TSHORT], D_CCR, Z, lg, Z);
991 gopcode(OAS, types[TSHORT], lg, Z, D_CCR, Z);
1006 boolgen(l, true, D_NONE, Z, l);
1010 boolgen(r, !true, D_NONE, Z, r);
1024 boolgen(l, !true, D_NONE, Z, l);
1028 boolgen(r, !true, D_NONE, Z, r);
1038 if(vconst(l) == 0) {
1040 boolgen(r, true, result, nn, post);
1043 boolgen(r, !true, result, nn, post);
1055 fp = typefd[r->type->etype];
1056 if(l->op == OCONST) {
1058 if(v == 0) { /* tst instruction */
1059 o = invrel[relindex(n->op)];
1060 rg = eval(r, result);
1061 gopcode(OTST, r->type, D_NONE, Z, rg, r);
1065 if(!fp) { /* cmpi and movq, saves about .5% both time and space */
1066 if(v < 128 && v >= -128 &&
1067 ewidth[r->type->etype] == SZ_LONG) {
1068 rg = eval(r, result);
1069 lg = regalloc(l->type, D_NONE);
1072 gopcode(o, l->type, lg, l, rg, r);
1077 o = invrel[relindex(n->op)];
1078 rg = eval(r, result);
1079 gopcode(o, r->type, rg, r, D_TREE, l);
1085 if(r->complex < FNX)
1086 lg = regalloc(l->type, lg);
1089 rg = eval(r, result);
1092 lg = regalloc(l->type, lg);
1093 gopcode(OAS, l->type, D_TOS, l, lg, l);
1096 gopcode(o, l->type, lg, l, rg, r);
1102 o = comrel[relindex(o)];
1103 if(doinc(post, POST|TEST)) {
1104 lg = regalloc(types[TSHORT], D_NONE);
1105 gopcode(OAS, types[TSHORT], D_CCR, Z, lg, Z);
1107 gopcode(OAS, types[TSHORT], lg, Z, D_CCR, Z);
1115 if(result == D_NONE)
1118 gopcode(OAS, nn->type, D_CONST, nodconst(1), result, nn);
1122 gopcode(OAS, nn->type, D_CONST, nodconst(0), result, nn);
1129 sugen(Node *n, int result, Node *nn, long w)
1137 if(n == Z || n->type == T)
1140 if(result == D_TREE)
1141 prtree(nn, "result");
1143 print("result = %R width = %ld\n", result, w);
1147 if(result == D_TREE) {
1153 if(n->addable >= INDEXED && n->op != OCONST)
1157 diag(n, "unknown op in sugen: %O", n->op);
1161 if(n->type && typev[n->type->etype]) {
1162 if(result == D_NONE) {
1163 nullwarn(n->left, Z);
1167 lg = regaddr(D_NONE);
1168 if(result == D_TOS) {
1169 adjsp(s - argoff + w);
1170 gopcode(OADDR, types[TIND], result, nn, lg, n);
1172 if(result == D_TREE) {
1175 diag(n, "unknown su result: %R", result);
1177 gopcode(OAS, types[TLONG], D_CONST, nodconst((long)(n->vconst>>32)),
1179 gopcode(OAS, types[TLONG], D_CONST, nodconst((long)(n->vconst)),
1188 sugen(l, D_TREE, nodrat, l->type->width);
1189 if(result != D_NONE) {
1190 warn(n, "non-interruptable temporary");
1193 if(!r || r->op != OCONST) {
1194 diag(n, "DOT and no offset");
1197 nod.xoffset += r->vconst;
1199 sugen(&nod, result, nn, w);
1204 if(result == D_NONE) {
1205 nullwarn(n->left, Z);
1211 lg = nodalloc(types[TIND], result, &nod);
1212 nod.lineno = n->lineno;
1213 if(result == D_TREE)
1216 if(result == D_TOS) {
1217 adjsp(s - argoff + w);
1218 gopcode(OADDR, types[TIND], result, nn, lg, n);
1220 diag(n, "unknown su result: %R", result);
1223 for(t = n->type->link; t != T; t = t->down) {
1225 if(r->op == OLIST) {
1230 if(l->complex < FNX) {
1232 if(o != t->offset) {
1233 gopcode(OADD, types[TIND], D_CONST,
1234 nodconst(t->offset-o), lg, Z);
1237 cgen(l, D_TREE, &nod);
1240 nod.xoffset = t->offset - o;
1241 gopcode(OAS, types[TIND], lg, Z, D_TOS, Z);
1243 if(typesuv[t->etype]) {
1244 sugen(l, D_TREE, nodrat, t->width);
1246 gopcode(OAS, types[TIND], D_TOS, Z, lg, Z);
1247 warn(n, "non-interruptable temporary");
1248 sugen(nodrat, D_TREE, &nod, t->width);
1251 rg = regalloc(t, D_NONE);
1254 gopcode(OAS, types[TIND], D_TOS, Z, lg, Z);
1255 gopcode(OAS, t, rg, Z, D_TREE, &nod);
1262 if(result == D_NONE) {
1263 sugen(n->right, D_TREE, n->left, w);
1266 sugen(n->right, D_TREE, nodrat, w); /* could do better */
1267 warn(n, "non-interruptable temporary");
1268 sugen(nodrat, D_TREE, n->left, w);
1269 sugen(nodrat, result, nn, w);
1273 if(result == D_NONE) {
1274 sugen(n, D_TREE, nodrat, w);
1278 /* prepare zero-th arg: address of result */
1279 if(result == D_TOS) {
1280 adjsp(s - argoff + w);
1283 gopcode(OADDR, types[TSHORT], D_NONE, nn, result, nn);
1284 p->to.type = D_STACK;
1285 p->to.offset = argoff - v;
1287 if(result == D_TREE) {
1290 if(nn->complex >= FNX) {
1291 rg = regalloc(types[TIND], regret(types[TIND]));
1293 gopcode(OAS, types[TIND], rg, Z, D_TOS, Z);
1296 lcgen(nn, D_TOS, Z);
1298 diag(n, "unknown result in FUNC sugen");
1301 argoff += types[TIND]->width;
1304 if(l->addable < INDEXED) {
1305 lg = regaddr(result);
1310 doinc(n->right, POST);
1311 doinc(n->left, POST);
1312 gopcode(OFUNC, types[TCHAR], D_NONE, Z, lg, l);
1319 doinc(n->left, PRE);
1320 boolgen(n->left, 1, D_NONE, Z, n->left);
1324 doinc(n->right->left, PRE);
1325 sugen(n->right->left, result, nn, w);
1326 doinc(n->right->left, POST);
1331 doinc(n->right->right, PRE);
1332 sugen(n->right->right, result, nn, w);
1333 doinc(n->right->right, POST);
1339 cgen(n->left, D_NONE, n->left);
1340 doinc(n->left, POST);
1341 doinc(n->right, PRE);
1342 sugen(n->right, result, nn, w);
1348 if(result == D_NONE)
1350 rg = regaddr(D_NONE);
1353 lg = regaddr(D_NONE);
1354 if(result == D_TOS) {
1355 adjsp(s - argoff + w);
1356 gopcode(OADDR, types[TIND], result, nn, lg, n);
1358 if(result == D_TREE) {
1359 if(nn->complex >= FNX) {
1360 gopcode(OAS, types[TIND], rg, n, D_TOS, n);
1364 gopcode(OAS, types[TIND], D_TOS, n, rg, n);
1368 diag(n, "unknown su result: %R", result);
1371 diag(Z, "sucopy width not 0%%%d", SZ_LONG);
1374 gopcode(OAS, types[TLONG], rg|I_INDINC, n, lg|I_INDINC, n);
1378 ng = regalloc(types[TLONG], D_NONE);
1379 gopcode(OAS, types[TLONG], D_CONST, nodconst(v/2-1), ng, n);
1381 gopcode(OAS, types[TLONG], rg|I_INDINC, n, lg|I_INDINC, n);
1382 gopcode(OAS, types[TLONG], rg|I_INDINC, n, lg|I_INDINC, n);
1390 gopcode(OAS, types[TLONG], rg|I_INDINC, n, lg|I_INDINC, n);