📄 txt.c
字号:
if(g >= D_A0 && g < D_A0+NREG) { aregused[g-D_A0]++; return g; } for(g=0; g<NREG; g++) if(aregused[g] == 0) { aregused[g]++; return g + D_A0; } diag(Z, "out of addr registers"); return D_TOS;}intregpair(int g){ if(g >= D_R0+1 && g < D_R0+NREG) if(!regused[g-D_R0-1]) { regused[g-D_R0-1]++; regused[g-D_R0]++; return g-1; } if(g >= D_R0 && g < D_R0+NREG-1) if(!regused[g-D_R0+1]) { regused[g-D_R0+1]++; regused[g-D_R0]++; return g; } for(g = 0; g < NREG-1; g++) if(!regused[g]) if(!regused[g+1]) { regused[g]++; regused[g+1]++; return g + D_R0; } diag(Z, "out of register pairs"); return D_TOS;}intregret(Type *t){ if(t == T) return D_NONE; if(typefd[t->etype]) return D_F0; return D_R0;}voidregfree(int g){ g &= D_MASK; if(g == D_TOS || g == D_TREE || g == D_NONE) return; if(g >= D_R0 && g < D_R0+NREG) { regused[g-D_R0]--; return; } if(g >= D_A0 && g < D_A0+NREG) { aregused[g-D_A0]--; return; } if(g >= D_F0 && g < D_F0+NREG) { fregused[g-D_F0]--; return; } diag(Z, "bad in regfree: %d", g);}voidgmove(Type *tf, Type *tt, int gf, Node *f, int gt, Node *t){ int g, a, b; Prog *p1; tindex(tf, tt); if(txtp->preclr) { if(gf >= D_R0 && gf < D_R0+NREG) if(txtp->preclr < 0) { gmove(tt, tt, gf, f, gt, t); return; } g = regalloc(types[TLONG], gt); if(g == gf) { g = regalloc(types[TLONG], D_NONE); regfree(gf); } if(txtp->preclr > 0) gopcode(OAS, types[TLONG], D_CONST, nodconst(0), g, Z); gopcode(OAS, tf, gf, f, g, Z); if(g != gt) gopcode(OAS, tt, g, Z, gt, t); regfree(g); return; } a = txtp->postext; if(a != AGOK) { if(gf >= D_R0 && gf < D_R0+NREG) g = regalloc(types[TLONG], gf); else g = regalloc(types[TLONG], gt); if(g != gf) gopcode(OAS, tf, gf, f, g, Z); nextpc(); p->as = a; p->to.type = g; if(debug['g']) print("%P\n", p); if(g != gt) gopcode(OAS, tt, g, Z, gt, t); regfree(g); return; } if((regbase[gf] != D_NONE && regbase[gf] == regbase[gt]) || (gf == D_TREE && gt == D_TREE && f == t)) return; if(typefd[tf->etype] || typefd[tt->etype]) { if(typeu[tf->etype] && typefd[tt->etype]) { /* unsign->float */ a = regalloc(types[TLONG], D_NONE); gmove(tf, types[TLONG], gf, f, a, t); if(tf->etype == TULONG) { b = regalloc(types[TDOUBLE], D_NONE); gmove(types[TLONG], tt, a, t, b, t); gopcode(OTST, types[TLONG], D_NONE, Z, a, t); gbranch(OGE); p1 = p; gopcode(OASADD, types[TDOUBLE], D_CONST, nodconst(100), b, t); p->from.dval = 4294967296.; patch(p1, pc); gmove(types[TDOUBLE], tt, b, t, gt, t); regfree(b); } else gmove(types[TLONG], tt, a, t, gt, t); regfree(a); return; } if(typefd[tf->etype] && !typefd[tt->etype]) { /* float->fix */ a = regalloc(types[TLONG], D_NONE); gopcode(OAS, types[TLONG], D_FPCR, t, a, t); gopcode(OAS, types[TLONG], D_CONST, nodconst(16), D_FPCR, t); } if(gf < D_F0 || gf >= D_F0+NREG) { g = regalloc(types[TDOUBLE], gt); gopcode(OFAS, tf, gf, f, g, t); if(g != gt) gopcode(OFAS, tt, g, t, gt, t); regfree(g); } else gopcode(OFAS, tt, gf, f, gt, t); if(typefd[tf->etype] && !typefd[tt->etype]) { /* float->fix */ gopcode(OAS, types[TLONG], a, t, D_FPCR, t); regfree(a); } return; } gopcode(OAS, tt, gf, f, gt, t);}voidgopcode(int o, Type *ty, int gf, Node *f, int gt, Node *t){ int i, fidx, tidx; if(o == OAS) if(gf == gt) if(gf != D_TREE || f == t) return; fidx = D_NONE; tidx = D_NONE; i = 0; if(ty != T) { i = ty->etype; if(i >= NTYPE) i = 0; } nextpc(); if(gf == D_TREE) { naddr(f, &p->from, fidx); } else { p->from.type = gf; if(gf == D_CONST) { p->from.offset = (long)(uintptr)f; if(typefd[i]) { p->from.type = D_FCONST; p->from.dval = (long)(uintptr)f; } } } p->as = opxt[o][i]; if(gt == D_TREE) { naddr(t, &p->to, tidx); } else { p->to.type = gt; if(gt == D_CONST) p->to.offset = (long)(uintptr)t; } if(o == OBIT) { p->from.field = f->type->nbits; p->to.field = f->type->shift; if(p->from.field == 0) diag(Z, "BIT zero width bit field"); } if(p->as == AMOVL || p->as == AMOVW || p->as == AMOVB) asopt(); if(debug['g']) print("%P\n", p); if(p->as == AGOK) diag(Z, "GOK in gopcode: %s", onames[o]); if(fidx != D_NONE) regfree(fidx); if(tidx != D_NONE) regfree(tidx);}voidasopt(void){ long v; int g; Prog *q; /* * mov $0, ... * ==> * clr , ... */ v = 0; if(p->from.type == D_CONST) { v = p->from.offset; if(v == 0) { p->from.type = D_NONE; if(p->as == AMOVL) p->as = ACLRL; if(p->as == AMOVW) p->as = ACLRW; if(p->as == AMOVB) p->as = ACLRB; return; } } /* * mov ..., TOS * ==> * pea (...) */ if(p->as == AMOVL && p->to.type == D_TOS) switch(p->from.type) { case D_CONST: p->from.type |= I_INDIR; p->to = p->from; p->from = zprog.from; p->as = APEA; return; case I_ADDR|D_EXTERN: case I_ADDR|D_STATIC: p->from.type &= ~I_ADDR; p->to = p->from; p->from = zprog.from; p->as = APEA; return; } /* * movL $Qx, ... * ==> * movL $Qx,R * movL R, ... */ if(p->as == AMOVL && p->from.type == D_CONST) if(v >= -128 && v < 128) if(p->to.type < D_R0 || p->to.type >= D_R0+NREG) { g = regalloc(types[TLONG], D_NONE); q = p; nextpc(); p->as = AMOVL; p->from.type = g; p->to = q->to; q->to = p->from; regfree(g); if(debug['g']) print("%P\n", q); return; }}voidgbranch(int o){ int a; a = ABNE; switch(o) { case ORETURN: a = ARTS; break; case OGOTO: a = ABRA; break; case OEQ: a = ABEQ; break; case ONE: a = ABNE; break; case OLE: a = ABLE; break; case OLS: a = ABLS; break; case OLT: a = ABLT; break; case OLO: a = ABCS; break; case OGE: a = ABGE; break; case OHS: a = ABCC; break; case OGT: a = ABGT; break; case OHI: a = ABHI; break; case OBIT: a = ABCS; break; case OCASE: a = ABCASE; break; } nextpc(); p->from.type = D_NONE; p->to.type = D_NONE; p->as = a;}voidfpbranch(void){ int a; a = p->as; switch(a) { case ABEQ: a = AFBEQ; break; case ABNE: a = AFBNE; break; case ABLE: a = AFBLE; break; case ABLT: a = AFBLT; break; case ABGE: a = AFBGE; break; case ABGT: a = AFBGT; break; } p->as = a;}voidpatch(Prog *op, long pc){ op->to.offset = pc; op->to.type = D_BRANCH;}voidgpseudo(int a, Sym *s, int g, long v){ nextpc(); if(a == ADATA) pc--; p->as = a; p->to.type = g; p->to.offset = v; p->from.sym = s; p->from.type = D_EXTERN; if(s->class == CSTATIC) p->from.type = D_STATIC;}voidgpseudotree(int a, Sym *s, Node *n){ nextpc(); if(a == ADATA) pc--; p->as = a; naddr(n, &p->to, D_NONE); p->from.sym = s; p->from.type = D_EXTERN; if(s->class == CSTATIC) p->from.type = D_STATIC;}longexreg(Type *t){ long o; if(typechl[t->etype]) { if(exregoffset <= 5) return 0; o = exregoffset + D_R0; exregoffset--; return o; } if(t->etype == TIND) { if(exaregoffset <= 3) return 0; o = exaregoffset + D_A0; exaregoffset--; return o; } if(typefd[t->etype]) { if(exfregoffset <= 5) return 0; o = exfregoffset + D_F0; exfregoffset--; return o; } return 0;}schar ewidth[NTYPE] ={ -1, /* [TXXX] */ SZ_CHAR, /* [TCHAR] */ SZ_CHAR, /* [TUCHAR] */ SZ_SHORT, /* [TSHORT] */ SZ_SHORT, /* [TUSHORT] */ SZ_INT, /* [TINT] */ SZ_INT, /* [TUINT] */ SZ_LONG, /* [TLONG] */ SZ_LONG, /* [TULONG] */ SZ_VLONG, /* [TVLONG] */ SZ_VLONG, /* [TUVLONG] */ SZ_FLOAT, /* [TFLOAT] */ SZ_DOUBLE, /* [TDOUBLE] */ SZ_IND, /* [TIND] */ 0, /* [TFUNC] */ -1, /* [TARRAY] */ 0, /* [TVOID] */ -1, /* [TSTRUCT] */ -1, /* [TUNION] */ SZ_INT, /* [TENUM] */};long ncast[NTYPE] ={ 0, /* [TXXX] */ BCHAR|BUCHAR, /* [TCHAR] */ BCHAR|BUCHAR, /* [TUCHAR] */ BSHORT|BUSHORT, /* [TSHORT] */ BSHORT|BUSHORT, /* [TUSHORT] */ BINT|BUINT|BLONG|BULONG|BIND, /* [TINT] */ BINT|BUINT|BLONG|BULONG|BIND, /* [TUINT] */ BINT|BUINT|BLONG|BULONG|BIND, /* [TLONG] */ BINT|BUINT|BLONG|BULONG|BIND, /* [TULONG] */ BVLONG|BUVLONG, /* [TVLONG] */ BVLONG|BUVLONG, /* [TUVLONG] */ BFLOAT, /* [TFLOAT] */ BDOUBLE, /* [TDOUBLE] */ BLONG|BULONG|BIND, /* [TIND] */ 0, /* [TFUNC] */ 0, /* [TARRAY] */ 0, /* [TVOID] */ BSTRUCT, /* [TSTRUCT] */ BUNION, /* [TUNION] */ 0, /* [TENUM] */};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -