12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403 |
- #include "gc.h"
- void
- cgen(Node *n, int result, Node *nn)
- {
- Node *l, *r, nod;
- int lg, rg, xg, yg, g, o;
- long v;
- Prog *p1;
- if(n == Z || n->type == T)
- return;
- if(typesuv[n->type->etype]) {
- sugen(n, result, nn, n->type->width);
- return;
- }
- if(debug['g']) {
- if(result == D_TREE)
- prtree(nn, "result");
- else
- print("result = %R\n", result);
- prtree(n, "cgen");
- }
- l = n->left;
- r = n->right;
- o = n->op;
- if(n->addable >= INDEXED) {
- if(result == D_NONE) {
- switch(o) {
- default:
- nullwarn(Z, Z);
- break;
- case OINDEX:
- nullwarn(l, r);
- break;
- }
- return;
- }
- gmove(n->type, nn->type, D_TREE, n, result, nn);
- return;
- }
- v = 0; /* set */
- switch(o) {
- default:
- diag(n, "unknown op in cgen: %O", o);
- break;
- case OAS:
- if(l->op == OBIT)
- goto bitas;
- /*
- * recursive use of result
- */
- if(result == D_NONE)
- if(l->addable > INDEXED)
- if(l->complex < FNX) {
- cgen(r, D_TREE, l);
- break;
- }
- /*
- * function calls on both sides
- */
- if(l->complex >= FNX && r->complex >= FNX) {
- cgen(r, D_TOS, r);
- v = argoff;
- lg = regaddr(result);
- lcgen(l, lg, Z);
- lg |= I_INDIR;
- adjsp(v - argoff);
- gmove(r->type, l->type, D_TOS, r, lg, l);
- if(result != D_NONE)
- gmove(l->type, nn->type, lg, l, result, nn);
- regfree(lg);
- break;
- }
- rg = D_TREE;
- lg = D_TREE;
- if(r->complex >= l->complex) {
- /*
- * right side before left
- */
- if(result != D_NONE) {
- rg = regalloc(n->type, result);
- cgen(r, rg, n);
- } else
- if(r->complex >= FNX || r->addable < INDEXED) {
- rg = regalloc(r->type, result);
- cgen(r, rg, r);
- }
- if(l->addable < INDEXED) {
- lg = regaddr(lg);
- lcgen(l, lg, Z);
- lg |= I_INDIR;
- }
- } else {
- /*
- * left before right
- */
- if(l->complex >= FNX || l->addable < INDEXED) {
- lg = regaddr(lg);
- lcgen(l, lg, Z);
- lg |= I_INDIR;
- }
- if(result != D_NONE) {
- rg = regalloc(n->type, result);
- cgen(r, rg, n);
- } else
- if(r->addable < INDEXED) {
- rg = regalloc(r->type, result);
- cgen(r, rg, r);
- }
- }
- if(result != D_NONE) {
- gmove(n->type, l->type, rg, r, lg, l);
- gmove(n->type, nn->type, rg, r, result, nn);
- } else
- gmove(r->type, l->type, rg, r, lg, l);
- regfree(lg);
- regfree(rg);
- break;
- bitas:
- n = l->left;
- rg = regalloc(tfield, result);
- if(l->complex >= r->complex) {
- lg = regaddr(D_NONE);
- lcgen(n, lg, Z);
- lg |= I_INDIR;
- cgen(r, rg, r);
- } else {
- cgen(r, rg, r);
- lg = regaddr(D_NONE);
- lcgen(n, lg, Z);
- lg |= I_INDIR;
- }
- g = regalloc(n->type, D_NONE);
- gmove(l->type, l->type, lg, l, g, l);
- bitstore(l, rg, lg, g, result, nn);
- break;
- case OBIT:
- if(result == D_NONE) {
- nullwarn(l, Z);
- break;
- }
- g = bitload(n, D_NONE, D_NONE, result, nn);
- gopcode(OAS, nn->type, g, n, result, nn);
- regfree(g);
- break;
- case ODOT:
- sugen(l, D_TREE, nodrat, l->type->width);
- if(result != D_NONE) {
- warn(n, "non-interruptable temporary");
- nod = *nodrat;
- if(!r || r->op != OCONST) {
- diag(n, "DOT and no offset");
- break;
- }
- nod.xoffset += r->vconst;
- nod.type = n->type;
- cgen(&nod, result, nn);
- }
- break;
- case OASLDIV:
- case OASLMOD:
- case OASDIV:
- case OASMOD:
- if(l->op == OBIT)
- goto asbitop;
- if(typefd[n->type->etype])
- goto asbinop;
- rg = D_TREE;
- if(l->complex >= FNX || r->complex >= FNX) {
- rg = D_TOS;
- cgen(r, rg, r);
- v = argoff;
- } else
- if(r->addable < INDEXED) {
- rg = regalloc(n->type, D_NONE);
- cgen(r, rg, r);
- }
- lg = D_TREE;
- if(!simplv(l)) {
- lg = regaddr(D_NONE);
- lcgen(l, lg, Z); /* destroys register optimization */
- lg |= I_INDIR;
- }
- g = regpair(result);
- gmove(l->type, n->type, lg, l, g, n);
- if(rg == D_TOS)
- adjsp(v - argoff);
- gopcode(o, n->type, rg, r, g, n);
- if(o == OASLMOD || o == OASMOD)
- gmove(n->type, l->type, g+1, n, lg, l);
- else
- gmove(n->type, l->type, g, n, lg, l);
- if(result != D_NONE)
- if(o == OASLMOD || o == OASMOD)
- gmove(n->type, nn->type, g+1, n, result, nn);
- else
- gmove(n->type, nn->type, g, n, result, nn);
- regfree(g);
- regfree(g+1);
- regfree(lg);
- regfree(rg);
- break;
- case OASXOR:
- case OASAND:
- case OASOR:
- if(l->op == OBIT)
- goto asbitop;
- if(l->complex >= FNX ||
- l->addable < INDEXED ||
- result != D_NONE ||
- typefd[n->type->etype])
- goto asbinop;
- rg = D_TREE;
- if(r->op != OCONST) {
- rg = regalloc(n->type, D_NONE);
- cgen(r, rg, r);
- }
- gopcode(o, l->type, rg, r, D_TREE, l);
- regfree(rg);
- break;
- case OASADD:
- case OASSUB:
- if(l->op == OBIT ||
- l->complex >= FNX ||
- l->addable < INDEXED ||
- result != D_NONE ||
- typefd[n->type->etype])
- goto asbinop;
- v = vconst(r);
- if(v > 0 && v <= 8) {
- gopcode(o, n->type, D_TREE, r, D_TREE, l);
- break;
- }
- rg = regalloc(n->type, D_NONE);
- cgen(r, rg, r);
- gopcode(o, n->type, rg, r, D_TREE, l);
- regfree(rg);
- break;
- case OASLSHR:
- case OASASHR:
- case OASASHL:
- if(l->op == OBIT ||
- l->complex >= FNX ||
- l->addable < INDEXED ||
- result != D_NONE ||
- typefd[n->type->etype])
- goto asbinop;
- rg = D_TREE;
- v = vconst(r);
- if(v <= 0 || v > 8) {
- rg = regalloc(n->type, D_NONE);
- cgen(r, rg, r);
- }
- lg = regalloc(n->type, D_NONE);
- cgen(l, lg, l);
- gopcode(o, n->type, rg, r, lg, l);
- gmove(n->type, n->type, lg, l, D_TREE, l);
- regfree(lg);
- regfree(rg);
- break;
- case OASLMUL:
- case OASMUL:
- asbinop:
- if(l->op == OBIT)
- goto asbitop;
- rg = D_TREE;
- if(l->complex >= FNX || r->complex >= FNX) {
- rg = D_TOS;
- cgen(r, rg, r);
- v = argoff;
- } else
- if(r->addable < INDEXED) {
- rg = regalloc(n->type, D_NONE);
- cgen(r, rg, r);
- } else {
- if(o == OASLSHR || o == OASASHR || o == OASASHL) {
- v = vconst(r);
- if(v <= 0 || v > 8) {
- rg = regalloc(n->type, D_NONE);
- cgen(r, rg, r);
- }
- }
- }
- lg = D_TREE;
- if(!simplv(l)) {
- lg = regaddr(D_NONE);
- lcgen(l, lg, Z); /* destroys register optimization */
- lg |= I_INDIR;
- }
- g = regalloc(n->type, result);
- gmove(l->type, n->type, lg, l, g, n);
- if(rg == D_TOS)
- adjsp(v - argoff);
- if(o == OASXOR)
- if(rg == D_TREE) {
- rg = regalloc(n->type, D_NONE);
- cgen(r, rg, r);
- }
- if(o == OASXOR || o == OASLSHR || o == OASASHR || o == OASASHL)
- if(rg == D_TOS) {
- rg = regalloc(n->type, D_NONE);
- gmove(n->type, n->type, D_TOS, n, rg, n);
- }
- gopcode(o, n->type, rg, r, g, n);
- gmove(n->type, l->type, g, n, lg, l);
- if(result != D_NONE)
- gmove(n->type, nn->type, g, n, result, nn);
- regfree(g);
- regfree(lg);
- regfree(rg);
- break;
- asbitop:
- rg = regaddr(D_NONE);
- lg = regalloc(tfield, D_NONE);
- if(l->complex >= r->complex) {
- g = bitload(l, lg, rg, result, nn);
- xg = regalloc(r->type, D_NONE);
- cgen(r, xg, nn);
- } else {
- xg = regalloc(r->type, D_NONE);
- cgen(r, xg, nn);
- g = bitload(l, lg, rg, result, nn);
- }
- if(!typefd[n->type->etype]) {
- if(o == OASLDIV || o == OASDIV) {
- yg = regpair(result);
- gmove(tfield, n->type, g, l, yg, n);
- gopcode(o, n->type, xg, r, yg, n);
- gmove(n->type, tfield, yg, n, g, l);
- regfree(yg);
- regfree(yg+1);
- regfree(xg);
- bitstore(l, g, rg, lg, D_NONE, nn);
- break;
- }
- if(o == OASLMOD || o == OASMOD) {
- yg = regpair(result);
- gmove(tfield, n->type, g, l, yg, n);
- gopcode(o, n->type, xg, r, yg, n);
- gmove(n->type, tfield, yg+1, n, g, l);
- regfree(yg);
- regfree(yg+1);
- regfree(xg);
- bitstore(l, g, rg, lg, D_NONE, nn);
- break;
- }
- }
- yg = regalloc(n->type, result);
- gmove(tfield, n->type, g, l, yg, n);
- gopcode(o, n->type, xg, r, yg, n);
- gmove(n->type, tfield, yg, n, g, l);
- regfree(yg);
- regfree(xg);
- bitstore(l, g, rg, lg, D_NONE, nn);
- break;
- case OCAST:
- if(result == D_NONE) {
- nullwarn(l, Z);
- break;
- }
- lg = result;
- if(l->complex >= FNX)
- lg = regret(l->type);
- lg = eval(l, lg);
- if(nocast(l->type, n->type)) {
- gmove(n->type, nn->type, lg, l, result, nn);
- regfree(lg);
- break;
- }
- if(nocast(n->type, nn->type)) {
- gmove(l->type, n->type, lg, l, result, nn);
- regfree(lg);
- break;
- }
- rg = regalloc(n->type, result);
- gmove(l->type, n->type, lg, l, rg, n);
- gmove(n->type, nn->type, rg, n, result, nn);
- regfree(rg);
- regfree(lg);
- break;
- case OCOND:
- doinc(l, PRE);
- boolgen(l, 1, D_NONE, Z, l);
- p1 = p;
- inargs++;
- doinc(r->left, PRE);
- cgen(r->left, result, nn);
- doinc(r->left, POST);
- gbranch(OGOTO);
- patch(p1, pc);
- p1 = p;
- doinc(r->right, PRE);
- cgen(r->right, result, nn);
- doinc(r->right, POST);
- patch(p1, pc);
- inargs--;
- break;
- case OIND:
- if(result == D_NONE) {
- nullwarn(l, Z);
- break;
- }
- lg = nodalloc(types[TIND], result, &nod);
- nod.lineno = n->lineno;
- if(l->op == OADD) {
- if(l->left->op == OCONST) {
- nod.xoffset += l->left->vconst;
- l = l->right;
- } else
- if(l->right->op == OCONST) {
- nod.xoffset += l->right->vconst;
- l = l->left;
- }
- }
- cgen(l, lg, l);
- gmove(n->type, nn->type, D_TREE, &nod, result, nn);
- regfree(lg);
- break;
- case OFUNC:
- v = argoff;
- inargs++;
- gargs(r);
- lg = D_TREE;
- if(l->addable < INDEXED) {
- lg = regaddr(result);
- lcgen(l, lg, Z);
- lg |= I_INDIR;
- }
- inargs--;
- doinc(r, POST);
- doinc(l, POST);
- gopcode(OFUNC, types[TCHAR], D_NONE, Z, lg, l);
- regfree(lg);
- if(inargs)
- adjsp(v - argoff);
- if(result != D_NONE) {
- lg = regret(n->type);
- gmove(n->type, nn->type, lg, n, result, nn);
- }
- break;
- case OLDIV:
- case OLMOD:
- case ODIV:
- case OMOD:
- if(result == D_NONE) {
- nullwarn(l, r);
- break;
- }
- if(typefd[n->type->etype])
- goto binop;
- if(r->addable >= INDEXED && r->complex < FNX) {
- lg = regpair(result);
- cgen(l, lg, l);
- rg = D_TREE;
- } else {
- cgen(r, D_TOS, r);
- v = argoff;
- lg = regpair(result);
- cgen(l, lg, l);
- adjsp(v - argoff);
- rg = D_TOS;
- }
- gopcode(o, n->type, rg, r, lg, l);
- if(o == OMOD || o == OLMOD)
- gmove(l->type, nn->type, lg+1, l, result, nn);
- else
- gmove(l->type, nn->type, lg, l, result, nn);
- regfree(lg);
- regfree(lg+1);
- break;
- case OMUL:
- case OLMUL:
- if(l->op == OCONST)
- if(mulcon(r, l, result, nn))
- break;
- if(r->op == OCONST)
- if(mulcon(l, r, result, nn))
- break;
- if(debug['M'])
- print("%L multiply\n", n->lineno);
- goto binop;
- case OAND:
- if(r->op == OCONST)
- if(typeil[n->type->etype])
- if(l->op == OCAST) {
- if(typec[l->left->type->etype])
- if(!(r->vconst & ~0xff)) {
- l = l->left;
- goto binop;
- }
- if(typeh[l->left->type->etype])
- if(!(r->vconst & ~0xffff)) {
- l = l->left;
- goto binop;
- }
- }
- goto binop;
- case OADD:
- if(result == D_TOS)
- if(r->addable >= INDEXED)
- if(l->op == OCONST)
- if(typeil[l->type->etype]) {
- v = l->vconst;
- if(v > -32768 && v < 32768) {
- rg = regaddr(D_NONE);
- gmove(r->type, r->type, D_TREE, r, rg, r);
- gopcode(OADDR, types[TSHORT], D_NONE, Z, rg, r);
- p->to.offset = v;
- p->to.type |= I_INDIR;
- regfree(rg);
- break;
- }
- }
- case OSUB:
- if(result == D_TOS)
- if(l->addable >= INDEXED)
- if(r->op == OCONST)
- if(typeil[r->type->etype]) {
- v = r->vconst;
- if(v > -32768 && v < 32768) {
- if(n->op == OSUB)
- v = -v;
- lg = regaddr(D_NONE);
- gmove(l->type, l->type, D_TREE, l, lg, l);
- gopcode(OADDR, types[TSHORT], D_NONE, Z, lg, l);
- p->to.offset = v;
- p->to.type |= I_INDIR;
- regfree(lg);
- break;
- }
- }
- goto binop;
- case OOR:
- case OXOR:
- binop:
- if(result == D_NONE) {
- nullwarn(l, r);
- break;
- }
- if(l->complex >= FNX && r->complex >= FNX) {
- cgen(r, D_TOS, r);
- v = argoff;
- lg = regalloc(l->type, result);
- cgen(l, lg, l);
- adjsp(v - argoff);
- if(o == OXOR) {
- rg = regalloc(r->type, D_NONE);
- gmove(r->type, r->type, D_TOS, r, rg, r);
- gopcode(o, n->type, rg, r, lg, l);
- regfree(rg);
- } else
- gopcode(o, n->type, D_TOS, r, lg, l);
- gmove(n->type, nn->type, lg, l, result, nn);
- regfree(lg);
- break;
- }
- if(l->complex >= r->complex) {
- if(l->op == OADDR && (o == OADD || o == OSUB))
- lg = regaddr(result);
- else
- lg = regalloc(l->type, result);
- cgen(l, lg, l);
- rg = eval(r, D_NONE);
- } else {
- rg = regalloc(r->type, D_NONE);
- cgen(r, rg, r);
- lg = regalloc(l->type, result);
- cgen(l, lg, l);
- }
- if(o == OXOR) {
- if(rg == D_TREE) {
- rg = regalloc(r->type, D_NONE);
- cgen(r, rg, r);
- }
- if(rg == D_TOS) {
- rg = regalloc(r->type, D_NONE);
- gmove(r->type, r->type, D_TOS, r, rg, r);
- }
- }
- gopcode(o, n->type, rg, r, lg, l);
- gmove(n->type, nn->type, lg, l, result, nn);
- regfree(lg);
- regfree(rg);
- break;
- case OASHL:
- if(r->op == OCONST)
- if(shlcon(l, r, result, nn))
- break;
- case OLSHR:
- case OASHR:
- if(result == D_NONE) {
- nullwarn(l, r);
- break;
- }
- if(l->complex >= FNX && r->complex >= FNX) {
- cgen(r, D_TOS, r);
- v = argoff;
- lg = regalloc(l->type, result);
- cgen(l, lg, l);
- adjsp(v - argoff);
- rg = regalloc(r->type, D_NONE);
- gopcode(OAS, r->type, D_TOS, r, rg, r);
- gopcode(n->op, n->type, rg, r, lg, l);
- gmove(n->type, nn->type, lg, l, result, nn);
- regfree(lg);
- regfree(rg);
- break;
- }
- if(l->complex >= r->complex) {
- lg = regalloc(l->type, result);
- cgen(l, lg, l);
- v = vconst(r);
- if(v <= 0 || v > 8) {
- rg = regalloc(r->type, D_NONE);
- cgen(r, rg, r);
- } else
- rg = eval(r, D_NONE);
- } else {
- rg = regalloc(r->type, D_NONE);
- cgen(r, rg, r);
- lg = regalloc(l->type, result);
- cgen(l, lg, l);
- }
- gopcode(o, n->type, rg, r, lg, l);
- gmove(n->type, nn->type, lg, l, result, nn);
- regfree(lg);
- regfree(rg);
- break;
- case ONEG:
- case OCOM:
- if(result == D_NONE) {
- nullwarn(l, Z);
- break;
- }
- lg = regalloc(l->type, result);
- cgen(l, lg, l);
- gopcode(o, l->type, D_NONE, Z, lg, l);
- gmove(n->type, nn->type, lg, l, result, nn);
- regfree(lg);
- break;
- case OADDR:
- if(result == D_NONE) {
- nullwarn(l, Z);
- break;
- }
- if(l->op == OINDEX && l->scale == 4 && result != D_TOS) {
- /* index scaled by 1, add is better */
- nod = *l;
- nod.op = OADD;
- nod.addable = 0;
- cgen(&nod, result, nn);
- break;
- }
- lcgen(l, result, nn);
- break;
- case OEQ:
- case ONE:
- case OLE:
- case OLT:
- case OGE:
- case OGT:
- case OLO:
- case OLS:
- case OHI:
- case OHS:
- if(result == D_NONE) {
- nullwarn(l, r);
- break;
- }
- boolgen(n, 1, result, nn, Z);
- break;
- case OANDAND:
- case OOROR:
- boolgen(n, 1, result, nn, Z);
- if(result == D_NONE)
- patch(p, pc);
- break;
- case OCOMMA:
- cgen(l, D_NONE, l);
- doinc(l, POST);
- doinc(r, PRE);
- cgen(r, result, nn);
- break;
- case ONOT:
- if(result == D_NONE) {
- nullwarn(l, Z);
- break;
- }
- boolgen(n, 1, result, nn, Z);
- break;
- case OPOSTINC:
- case OPOSTDEC:
- v = 1;
- if(l->type->etype == TIND)
- v = l->type->link->width;
- if(o == OPOSTDEC)
- v = -v;
- if(l->op == OBIT)
- goto bitinc;
- if(nn == Z)
- goto pre;
- lg = D_TREE;
- if(l->addable < INDEXED) {
- lg = regaddr(D_NONE);
- lcgen(l, lg, Z);
- lg |= I_INDIR;
- }
- if(result != D_NONE)
- gmove(l->type, nn->type, lg, l, result, nn);
- if(typefd[n->type->etype]) {
- rg = regalloc(n->type, D_NONE);
- gmove(l->type, l->type, lg, l, rg, l);
- gopcode(o, n->type, D_CONST, nodconst(1), rg, l);
- gmove(l->type, l->type, rg, l, lg, l);
- regfree(rg);
- } else {
- if(v < 0)
- gopcode(o, n->type, D_CONST, nodconst(-v), lg, l);
- else
- gopcode(o, n->type, D_CONST, nodconst(v), lg, l);
- }
- regfree(lg);
- break;
- case OPREINC:
- case OPREDEC:
- v = 1;
- if(l->type->etype == TIND)
- v = l->type->link->width;
- if(o == OPREDEC)
- v = -v;
- if(l->op == OBIT)
- goto bitinc;
- pre:
- lg = D_TREE;
- if(l->addable < INDEXED) {
- lg = regaddr(D_NONE);
- lcgen(l, lg, Z);
- lg |= I_INDIR;
- }
- if(typefd[n->type->etype]) {
- rg = regalloc(n->type, D_NONE);
- gmove(l->type, l->type, lg, l, rg, l);
- gopcode(o, n->type, D_CONST, nodconst(1), rg, l);
- gmove(l->type, l->type, rg, l, lg, l);
- regfree(rg);
- } else {
- if(v < 0)
- gopcode(o, n->type, D_CONST, nodconst(-v), lg, l);
- else
- gopcode(o, n->type, D_CONST, nodconst(v), lg, l);
- }
- if(result != D_NONE)
- gmove(l->type, nn->type, lg, l, result, nn);
- regfree(lg);
- break;
- bitinc:
- rg = regaddr(D_NONE);
- lg = regalloc(tfield, D_NONE);
- if(result != D_NONE && (o == OPOSTINC || o == OPOSTDEC)) {
- g = bitload(l, lg, rg, D_NONE, nn);
- if(nn != Z)
- gmove(l->type, nn->type, g, l, result, nn);
- if(v < 0)
- gopcode(o, n->type, D_CONST, nodconst(-v), g, n);
- else
- gopcode(o, n->type, D_CONST, nodconst(v), g, n);
- bitstore(l, g, rg, lg, D_NONE, nn);
- break;
- }
- g = bitload(l, lg, rg, result, nn);
- if(v < 0)
- gopcode(o, n->type, D_CONST, nodconst(-v), g, n);
- else
- gopcode(o, n->type, D_CONST, nodconst(v), g, n);
- if(result != D_NONE)
- gmove(l->type, nn->type, g, l, result, nn);
- bitstore(l, g, rg, lg, D_NONE, nn);
- break;
- }
- }
- void
- lcgen(Node *n, int result, Node *nn)
- {
- Node rn;
- Prog *p1;
- int lg;
- if(n == Z || n->type == T)
- return;
- if(debug['g']) {
- if(result == D_TREE)
- prtree(nn, "result");
- else
- print("result = %R\n", result);
- prtree(n, "lcgen");
- }
- if(nn == Z) {
- nn = &rn;
- nn->type = types[TIND];
- }
- switch(n->op) {
- case OCOMMA:
- cgen(n->left, D_NONE, n->left);
- doinc(n->left, POST);
- doinc(n->right, PRE);
- lcgen(n->right, result, nn);
- break;
- case OCOND:
- doinc(n->left, PRE);
- boolgen(n->left, 1, D_NONE, Z, n->left);
- p1 = p;
- inargs++;
- doinc(n->right->left, PRE);
- lcgen(n->right->left, result, nn);
- doinc(n->right->left, POST);
- gbranch(OGOTO);
- patch(p1, pc);
- p1 = p;
- doinc(n->right->right, PRE);
- lcgen(n->right->right, result, nn);
- doinc(n->right->right, POST);
- patch(p1, pc);
- inargs--;
- break;
- case OIND:
- if(n->addable >= INDEXED) {
- if(result >= D_A0 && result < D_A0+NREG) {
- gopcode(OADDR, types[TLONG], D_TREE, n, result, nn);
- break;
- }
- if(result == D_TOS) {
- gopcode(OADDR, types[TSHORT], D_NONE, nn, D_TREE, n);
- break;
- }
- }
- cgen(n->left, result, nn);
- break;
- default:
- if(n->addable < INDEXED) {
- diag(n, "unknown op in lcgen: %O", n->op);
- break;
- }
- if(result >= D_A0 && result < D_A0+NREG) {
- gopcode(OADDR, types[TLONG], D_TREE, n, result, nn);
- break;
- }
- if(result == D_TOS) {
- gopcode(OADDR, types[TSHORT], D_NONE, nn, D_TREE, n);
- break;
- }
- lg = regaddr(result);
- gopcode(OADDR, types[TLONG], D_TREE, n, lg, nn);
- gopcode(OAS, nn->type, lg, nn, result, nn);
- regfree(lg);
- break;
- }
- }
- void
- bcgen(Node *n, int true)
- {
- boolgen(n, true, D_NONE, Z, Z);
- }
- void
- boolgen(Node *n, int true, int result, Node *nn, Node *post)
- {
- Prog *p1, *p2;
- Node *l, *r;
- int lg, rg, fp, o;
- long v;
- if(debug['g']) {
- if(result == D_TREE)
- prtree(nn, "result");
- else
- print("result = %R\n", result);
- prtree(n, "boolgen");
- }
- l = n->left;
- r = n->right;
- switch(n->op) {
- default:
- lg = eval(n, result);
- if(lg >= D_A0 && lg < D_A0+NREG) {
- rg = regalloc(types[TLONG], D_NONE);
- gopcode(OAS, types[TLONG], lg, n, rg, Z);
- regfree(rg);
- } else
- gopcode(OTST, n->type, D_NONE, Z, lg, n);
- regfree(lg);
- o = ONE;
- fp = typefd[n->type->etype];
- goto genbool;
- case OCONST:
- fp = vconst(n);
- if(!true)
- fp = !fp;
- gbranch(OGOTO);
- if(fp) {
- p1 = p;
- gbranch(OGOTO);
- patch(p1, pc);
- }
- goto com;
- case ONOT:
- boolgen(l, !true, result, nn, post);
- break;
- case OCOND:
- doinc(l, PRE);
- boolgen(l, 1, D_NONE, Z, l);
- p1 = p;
- inargs++;
- doinc(r->left, PRE);
- boolgen(r->left, true, result, nn, r->left);
- if(result != D_NONE) {
- doinc(r->left, POST);
- gbranch(OGOTO);
- patch(p1, pc);
- p1 = p;
- doinc(r->right, PRE);
- boolgen(r->right, !true, result, nn, r->right);
- doinc(r->right, POST);
- patch(p1, pc);
- inargs--;
- break;
- }
- p2 = p;
- gbranch(OGOTO);
- patch(p1, pc);
- p1 = p;
- doinc(r->right, PRE);
- boolgen(r->right, !true, result, nn, r->right);
- patch(p2, pc);
- p2 = p;
- if(doinc(post, POST|TEST)) {
- lg = regalloc(types[TSHORT], D_NONE);
- gopcode(OAS, types[TSHORT], D_CCR, Z, lg, Z);
- doinc(post, POST);
- gopcode(OAS, types[TSHORT], lg, Z, D_CCR, Z);
- regfree(lg);
- }
- gbranch(OGOTO);
- patch(p1, pc);
- patch(p2, pc);
- inargs--;
- goto com;
- case OANDAND:
- if(!true)
- goto caseor;
- caseand:
- doinc(l, PRE);
- boolgen(l, true, D_NONE, Z, l);
- p1 = p;
- inargs++;
- doinc(r, PRE);
- boolgen(r, !true, D_NONE, Z, r);
- p2 = p;
- patch(p1, pc);
- gbranch(OGOTO);
- patch(p2, pc);
- inargs--;
- goto com;
- case OOROR:
- if(!true)
- goto caseand;
- caseor:
- doinc(l, PRE);
- boolgen(l, !true, D_NONE, Z, l);
- p1 = p;
- inargs++;
- doinc(r, PRE);
- boolgen(r, !true, D_NONE, Z, r);
- p2 = p;
- gbranch(OGOTO);
- patch(p1, pc);
- patch(p2, pc);
- inargs--;
- goto com;
- case OEQ:
- case ONE:
- if(vconst(l) == 0) {
- if(n->op == ONE) {
- boolgen(r, true, result, nn, post);
- break;
- }
- boolgen(r, !true, result, nn, post);
- break;
- }
- case OLE:
- case OLT:
- case OGE:
- case OGT:
- case OHI:
- case OHS:
- case OLO:
- case OLS:
- fp = typefd[r->type->etype];
- if(l->op == OCONST) {
- v = vconst(l);
- if(v == 0) { /* tst instruction */
- o = invrel[relindex(n->op)];
- rg = eval(r, result);
- gopcode(OTST, r->type, D_NONE, Z, rg, r);
- regfree(rg);
- goto genbool;
- }
- if(!fp) { /* cmpi and movq, saves about .5% both time and space */
- if(v < 128 && v >= -128 &&
- ewidth[r->type->etype] == SZ_LONG) {
- rg = eval(r, result);
- lg = regalloc(l->type, D_NONE);
- cgen(l, lg, l);
- o = n->op;
- gopcode(o, l->type, lg, l, rg, r);
- regfree(lg);
- regfree(rg);
- goto genbool;
- }
- o = invrel[relindex(n->op)];
- rg = eval(r, result);
- gopcode(o, r->type, rg, r, D_TREE, l);
- regfree(rg);
- goto genbool;
- }
- }
- lg = D_TOS;
- if(r->complex < FNX)
- lg = regalloc(l->type, lg);
- cgen(l, lg, l);
- v = argoff;
- rg = eval(r, result);
- if(lg == D_TOS) {
- adjsp(v - argoff);
- lg = regalloc(l->type, lg);
- gopcode(OAS, l->type, D_TOS, l, lg, l);
- }
- o = n->op;
- gopcode(o, l->type, lg, l, rg, r);
- regfree(lg);
- regfree(rg);
- genbool:
- if(true)
- o = comrel[relindex(o)];
- if(doinc(post, POST|TEST)) {
- lg = regalloc(types[TSHORT], D_NONE);
- gopcode(OAS, types[TSHORT], D_CCR, Z, lg, Z);
- doinc(post, POST);
- gopcode(OAS, types[TSHORT], lg, Z, D_CCR, Z);
- regfree(lg);
- }
- gbranch(o);
- if(fp)
- fpbranch();
- com:
- if(result == D_NONE)
- break;
- p1 = p;
- gopcode(OAS, nn->type, D_CONST, nodconst(1), result, nn);
- gbranch(OGOTO);
- p2 = p;
- patch(p1, pc);
- gopcode(OAS, nn->type, D_CONST, nodconst(0), result, nn);
- patch(p2, pc);
- break;
- }
- }
- void
- sugen(Node *n, int result, Node *nn, long w)
- {
- long s, v, o;
- int lg, rg, ng;
- Prog *p1;
- Node *l, *r, nod;
- Type *t;
- if(n == Z || n->type == T)
- return;
- if(debug['g']) {
- if(result == D_TREE)
- prtree(nn, "result");
- else
- print("result = %R width = %ld\n", result, w);
- prtree(n, "sugen");
- }
- s = argoff;
- if(result == D_TREE) {
- if(nn == nodrat)
- if(w > nrathole)
- nrathole = w;
- }
- if(n->addable >= INDEXED && n->op != OCONST)
- goto copy;
- switch(n->op) {
- default:
- diag(n, "unknown op in sugen: %O", n->op);
- break;
- case OCONST:
- if(n->type && typev[n->type->etype]) {
- if(result == D_NONE) {
- nullwarn(n->left, Z);
- break;
- }
- lg = regaddr(D_NONE);
- if(result == D_TOS) {
- adjsp(s - argoff + w);
- gopcode(OADDR, types[TIND], result, nn, lg, n);
- } else
- if(result == D_TREE) {
- lcgen(nn, lg, Z);
- } else
- diag(n, "unknown su result: %R", result);
- gopcode(OAS, types[TLONG], D_CONST, nodconst((long)(n->vconst>>32)),
- lg|I_INDINC, n);
- gopcode(OAS, types[TLONG], D_CONST, nodconst((long)(n->vconst)),
- lg|I_INDINC, n);
- regfree(lg);
- break;
- }
- goto copy;
- case ODOT:
- l = n->left;
- sugen(l, D_TREE, nodrat, l->type->width);
- if(result != D_NONE) {
- warn(n, "non-interruptable temporary");
- nod = *nodrat;
- r = n->right;
- if(!r || r->op != OCONST) {
- diag(n, "DOT and no offset");
- break;
- }
- nod.xoffset += r->vconst;
- nod.type = n->type;
- sugen(&nod, result, nn, w);
- }
- break;
- case OIND:
- if(result == D_NONE) {
- nullwarn(n->left, Z);
- break;
- }
- goto copy;
- case OSTRUCT:
- lg = nodalloc(types[TIND], result, &nod);
- nod.lineno = n->lineno;
- if(result == D_TREE)
- lcgen(nn, lg, Z);
- else
- if(result == D_TOS) {
- adjsp(s - argoff + w);
- gopcode(OADDR, types[TIND], result, nn, lg, n);
- } else
- diag(n, "unknown su result: %R", result);
- o = 0;
- r = n->left;
- for(t = n->type->link; t != T; t = t->down) {
- l = r;
- if(r->op == OLIST) {
- l = r->left;
- r = r->right;
- }
- nod.type = t;
- if(l->complex < FNX) {
- nod.xoffset = 0;
- if(o != t->offset) {
- gopcode(OADD, types[TIND], D_CONST,
- nodconst(t->offset-o), lg, Z);
- o = t->offset;
- }
- cgen(l, D_TREE, &nod);
- continue;
- }
- nod.xoffset = t->offset - o;
- gopcode(OAS, types[TIND], lg, Z, D_TOS, Z);
- s = argoff;
- if(typesuv[t->etype]) {
- sugen(l, D_TREE, nodrat, t->width);
- adjsp(s - argoff);
- gopcode(OAS, types[TIND], D_TOS, Z, lg, Z);
- warn(n, "non-interruptable temporary");
- sugen(nodrat, D_TREE, &nod, t->width);
- continue;
- }
- rg = regalloc(t, D_NONE);
- cgen(l, rg, l);
- adjsp(s - argoff);
- gopcode(OAS, types[TIND], D_TOS, Z, lg, Z);
- gopcode(OAS, t, rg, Z, D_TREE, &nod);
- regfree(rg);
- }
- regfree(lg);
- break;
- case OAS:
- if(result == D_NONE) {
- sugen(n->right, D_TREE, n->left, w);
- break;
- }
- sugen(n->right, D_TREE, nodrat, w); /* could do better */
- warn(n, "non-interruptable temporary");
- sugen(nodrat, D_TREE, n->left, w);
- sugen(nodrat, result, nn, w);
- break;
- case OFUNC:
- if(result == D_NONE) {
- sugen(n, D_TREE, nodrat, w);
- break;
- }
- inargs++;
- /* prepare zero-th arg: address of result */
- if(result == D_TOS) {
- adjsp(s - argoff + w);
- v = argoff;
- gargs(n->right);
- gopcode(OADDR, types[TSHORT], D_NONE, nn, result, nn);
- p->to.type = D_STACK;
- p->to.offset = argoff - v;
- } else
- if(result == D_TREE) {
- v = argoff;
- gargs(n->right);
- if(nn->complex >= FNX) {
- rg = regalloc(types[TIND], regret(types[TIND]));
- lcgen(nn, rg, Z);
- gopcode(OAS, types[TIND], rg, Z, D_TOS, Z);
- regfree(rg);
- } else
- lcgen(nn, D_TOS, Z);
- } else {
- diag(n, "unknown result in FUNC sugen");
- break;
- }
- argoff += types[TIND]->width;
- l = n->left;
- lg = D_TREE;
- if(l->addable < INDEXED) {
- lg = regaddr(result);
- lcgen(l, lg, Z);
- lg |= I_INDIR;
- }
- inargs--;
- doinc(n->right, POST);
- doinc(n->left, POST);
- gopcode(OFUNC, types[TCHAR], D_NONE, Z, lg, l);
- regfree(lg);
- if(inargs)
- adjsp(v - argoff);
- break;
- case OCOND:
- doinc(n->left, PRE);
- boolgen(n->left, 1, D_NONE, Z, n->left);
- p1 = p;
- inargs++;
- doinc(n->right->left, PRE);
- sugen(n->right->left, result, nn, w);
- doinc(n->right->left, POST);
- gbranch(OGOTO);
- patch(p1, pc);
- p1 = p;
- doinc(n->right->right, PRE);
- sugen(n->right->right, result, nn, w);
- doinc(n->right->right, POST);
- patch(p1, pc);
- inargs--;
- break;
- case OCOMMA:
- cgen(n->left, D_NONE, n->left);
- doinc(n->left, POST);
- doinc(n->right, PRE);
- sugen(n->right, result, nn, w);
- break;
- }
- return;
- copy:
- if(result == D_NONE)
- return;
- rg = regaddr(D_NONE);
- lcgen(n, rg, Z);
- lg = regaddr(D_NONE);
- if(result == D_TOS) {
- adjsp(s - argoff + w);
- gopcode(OADDR, types[TIND], result, nn, lg, n);
- } else
- if(result == D_TREE) {
- if(nn->complex >= FNX) {
- gopcode(OAS, types[TIND], rg, n, D_TOS, n);
- s = argoff;
- lcgen(nn, lg, Z);
- adjsp(s - argoff);
- gopcode(OAS, types[TIND], D_TOS, n, rg, n);
- } else
- lcgen(nn, lg, Z);
- } else
- diag(n, "unknown su result: %R", result);
- if(w % SZ_LONG)
- diag(Z, "sucopy width not 0%%%d", SZ_LONG);
- v = w / SZ_LONG;
- if(v & 1) {
- gopcode(OAS, types[TLONG], rg|I_INDINC, n, lg|I_INDINC, n);
- v--;
- }
- if(v > 6) {
- ng = regalloc(types[TLONG], D_NONE);
- gopcode(OAS, types[TLONG], D_CONST, nodconst(v/2-1), ng, n);
- v = pc;
- gopcode(OAS, types[TLONG], rg|I_INDINC, n, lg|I_INDINC, n);
- gopcode(OAS, types[TLONG], rg|I_INDINC, n, lg|I_INDINC, n);
- gbranch(OGT);
- patch(p, v);
- p->from.type = ng;
- p->as = ADBF;
- regfree(ng);
- } else
- while(v > 0) {
- gopcode(OAS, types[TLONG], rg|I_INDINC, n, lg|I_INDINC, n);
- v--;
- }
- regfree(lg);
- regfree(rg);
- }
|