int tlen(NODE *p) { switch (p->n_type) { case CHAR: case UCHAR: return 1; case SHORT: case USHORT: return (SZSHORT / SZCHAR); case FLOAT: return (SZFLOAT / SZCHAR); case DOUBLE: return (SZDOUBLE / SZCHAR); case INT: case UNSIGNED: return (SZINT / SZCHAR); case LONG: case ULONG: case LONGLONG: case ULONGLONG: return SZLONGLONG / SZCHAR; default: if (!ISPTR(p->n_type)) comperr("tlen type unknown: %d"); return SZPOINT(p->n_type) / SZCHAR; } }
/* * Return type size in bytes. Used by R2REGS, arg 2 to offset(). */ int tlen(NODE *p) { switch(p->n_type) { case CHAR: case UCHAR: return(1); case SHORT: case USHORT: return(SZSHORT/SZCHAR); case DOUBLE: return(SZDOUBLE/SZCHAR); case INT: case UNSIGNED: case LONG: case ULONG: return(SZINT/SZCHAR); case LONGLONG: case ULONGLONG: return SZLONGLONG/SZCHAR; default: if (!ISPTR(p->n_type)) comperr("tlen type %d not pointer"); return SZPOINT(p->n_type)/SZCHAR; } }
/* * AMD64 parameter classification. */ static int argtyp(TWORD t, union dimfun *df, struct attr *ap) { int cl = 0; if (t <= ULONG || ISPTR(t) || t == BOOL) { cl = ngpr < 6 ? INTEGER : INTMEM; } else if (t == FLOAT || t == DOUBLE || t == FIMAG || t == IMAG) { cl = nsse < 8 ? SSE : SSEMEM; } else if (t == LDOUBLE || t == LIMAG) { cl = X87; /* XXX */ } else if (t == STRTY || t == UNIONTY) { int sz = tsize(t, df, ap); if (sz <= 2*SZLONG && attr_find(ap, ATTR_COMPLEX) != NULL) { cl = nsse < 7 ? STRCPX : STRMEM; } else if (sz > 2*SZLONG || ((sz+SZLONG)/SZLONG)+ngpr > 6 || attr_find(ap, GCC_ATYP_PACKED) != NULL) cl = STRMEM; else cl = STRREG; } else cerror("FIXME: classify"); return cl; }
/* * va_start(ap, last) implementation. * * f is the NAME node for this builtin function. * a is the argument list containing: * CM * ap last * * It turns out that this is easy on MIPS. Just write the * argument registers to the stack in va_arg_start() and * use the traditional method of walking the stackframe. */ NODE * mips_builtin_stdarg_start(NODE *f, NODE *a, TWORD t) { NODE *p, *q; int sz = 1; /* check num args and type */ if (a == NULL || a->n_op != CM || a->n_left->n_op == CM || !ISPTR(a->n_left->n_type)) goto bad; /* must first deal with argument size; use int size */ p = a->n_right; if (p->n_type < INT) { /* round up to word */ sz = SZINT / tsize(p->n_type, p->n_df, p->n_ap); } p = buildtree(ADDROF, p, NIL); /* address of last arg */ p = optim(buildtree(PLUS, p, bcon(sz))); q = block(NAME, NIL, NIL, PTR+VOID, 0, 0); q = buildtree(CAST, q, p); p = q->n_right; nfree(q->n_left); nfree(q); p = buildtree(ASSIGN, a->n_left, p); tfree(f); nfree(a); return p; bad: uerror("bad argument to __builtin_stdarg_start"); return bcon(0); }
static void pshargs(union arglist *al) { TWORD t; for (; al->type != TNULL; al++) { t = al->type; if (t == TELLIPSIS) { nmch('z'); continue; } while (t > BTMASK) { if (ISPTR(t)) nmch('P'); else uerror("pshargs2: %lx\n", t); t = DECREF(t); } if (t > LDOUBLE) uerror("pshargs: %lx\n", t); /* XXX - cannot emit const/volatile */ nmch(typch(t)); } }
/* * is an automatic variable of type t OK for a register variable */ int cisreg(TWORD t) { if (t == INT || t == UNSIGNED || t == CHAR || t == UCHAR || ISPTR(t)) return(1); return 0; /* XXX - fix reg assignment in pftn.c */ }
int base(register NODE *p) { register int o = p->op; if( (o==ICON && p->name[0] != '\0')) return( 100 ); /* ie no base reg */ if( o==REG ) return( p->rval ); if( (o==PLUS || o==MINUS) && p->left->op == REG && p->right->op==ICON) return( p->left->rval ); if( o==OREG && !R2TEST(p->rval) && (p->type==INT || p->type==UNSIGNED || ISPTR(p->type)) ) return( p->rval + 0200*1 ); if( o==INCR && p->left->op==REG ) return( p->left->rval + 0200*2 ); if( o==ASG MINUS && p->left->op==REG) return( p->left->rval + 0200*4 ); if( o==UNARY MUL && p->left->op==INCR && p->left->left->op==REG && (p->type==INT || p->type==UNSIGNED || ISPTR(p->type)) ) return( p->left->left->rval + 0200*(1+2) ); return( -1 ); }
/* * output a nice description of the type of t */ void tprint(FILE *fp, TWORD t, TWORD q) { static char * tnames[] = { "undef", "farg", "char", "uchar", "short", "ushort", "int", "unsigned", "long", "ulong", "longlong", "ulonglong", "float", "double", "ldouble", "strty", "unionty", "enumty", "moety", "void", "signed", /* pass1 */ "bool", /* pass1 */ "fimag", /* pass1 */ "dimag", /* pass1 */ "limag", /* pass1 */ "fcomplex", /* pass1 */ "dcomplex", /* pass1 */ "lcomplex", /* pass1 */ "enumty", /* pass1 */ "?", "?" }; for(;; t = DECREF(t), q = DECREF(q)) { if (ISCON(q)) fputc('C', fp); if (ISVOL(q)) fputc('V', fp); if (ISPTR(t)) fprintf(fp, "PTR "); else if (ISFTN(t)) fprintf(fp, "FTN "); else if (ISARY(t)) fprintf(fp, "ARY "); else { fprintf(fp, "%s%s%s", ISCON(q << TSHIFT) ? "const " : "", ISVOL(q << TSHIFT) ? "volatile " : "", tnames[t]); return; } } }
NODE * mips_builtin_va_arg(NODE *f, NODE *a, TWORD t) { NODE *p, *q, *r; int sz, tmpnr; /* check num args and type */ if (a == NULL || a->n_op != CM || a->n_left->n_op == CM || !ISPTR(a->n_left->n_type) || a->n_right->n_op != TYPE) goto bad; r = a->n_right; /* get type size */ sz = tsize(r->n_type, r->n_df, r->n_ap) / SZCHAR; if (sz < SZINT/SZCHAR) { werror("%s%s promoted to int when passed through ...", r->n_type & 1 ? "unsigned " : "", DEUNSIGN(r->n_type) == SHORT ? "short" : "char"); sz = SZINT/SZCHAR; } /* alignment */ p = tcopy(a->n_left); if (sz > SZINT/SZCHAR && r->n_type != UNIONTY && r->n_type != STRTY) { p = buildtree(PLUS, p, bcon(7)); p = block(AND, p, bcon(-8), p->n_type, p->n_df, p->n_ap); } /* create a copy to a temp node */ q = tempnode(0, p->n_type, p->n_df, p->n_ap); tmpnr = regno(q); p = buildtree(ASSIGN, q, p); q = tempnode(tmpnr, p->n_type, p->n_df,p->n_ap); q = buildtree(PLUS, q, bcon(sz)); q = buildtree(ASSIGN, a->n_left, q); q = buildtree(COMOP, p, q); nfree(a->n_right); nfree(a); nfree(f); p = tempnode(tmpnr, INCREF(r->n_type), r->n_df, r->n_ap); p = buildtree(UMUL, p, NIL); p = buildtree(COMOP, q, p); return p; bad: uerror("bad argument to __builtin_va_arg"); return bcon(0); }
void prtype(NODE *n) { static char pt[] = { 0, 0, 'b', 'b', 'w', 'w', 'l', 'l', 0, 0, 'q', 'q', 'f', 'd' }; TWORD t = n->n_type; if (ISPTR(t)) t = UNSIGNED; if (t > DOUBLE || pt[t] == 0) comperr("prtype: bad type"); putchar(pt[t]); }
/* * fortran function arguments */ static NODE * fortarg(NODE *p) { if( p->n_op == CM ){ p->n_left = fortarg( p->n_left ); p->n_right = fortarg( p->n_right ); return(p); } while( ISPTR(p->n_type) ){ p = buildtree( UMUL, p, NIL ); } return( optim(p) ); }
/* * return a node, for structure references, which is suitable for * being added to a pointer of type t, in order to be off bits offset * into a structure * t, d, and s are the type, dimension offset, and sizeoffset * For nova, return the type-specific index number which calculation * is based on its size. For example, char a[3] would return 3. * Be careful about only handling first-level pointers, the following * indirections must be fullword. */ NODE * offcon(OFFSZ off, TWORD t, union dimfun *d, struct suedef *sue) { register NODE *p; if (xdebug) printf("offcon: OFFSZ %ld type %x dim %p siz %d\n", off, t, d, sue->suesize); p = bcon(0); p->n_lval = off/SZINT; /* Default */ if (ISPTR(DECREF(t))) return p; if (t == VOID || t == CHAR || t == UCHAR) p->n_lval = off/SZCHAR; /* pointer to char */ return(p); }
/* * Called when a identifier has been declared, to give target last word. * On Nova we put symbols over the size of an int above 24 bytes in * offset and leave zeropage for small vars. */ void fixdef(struct symtab *sp) { #if 0 if (sp->sclass != AUTO) return; /* not our business */ if (ISPTR(sp->stype) || sp->stype < LONG) return; if (sp->soffset >= (MAXZP * SZINT)) return; /* already above */ /* have to move */ /* XXX remember old autooff for reorg of smaller vars */ if (autooff < MAXZP * SZINT) autooff = MAXZP * SZINT; oalloc(sp, &autooff); #endif }
static void sconv(NODE *p) { NODE *l = p->n_left; TWORD ts, td; int o; /* * Source node may be in register or memory. * Result is always in register. */ ts = l->n_type; if (ISPTR(ts)) ts = UNSIGNED; td = p->n_type; ts = ts < LONG ? ts-2 : ts-4; td = td < LONG ? td-2 : td-4; o = scary[ts][td]; switch (o) { case MLE: case MLZ: expand(p, INAREG|INBREG, "\tmovl\tAL,A1\n"); break; case MVD: if (l->n_op == REG && regno(l) == regno(getlr(p, '1'))) break; /* unneccessary move */ expand(p, INAREG|INBREG, "\tmovZR\tAL,A1\n"); break; case CSE: expand(p, INAREG|INBREG, "\tcvtZLl\tAL,A1\n"); break; case CVT: expand(p, INAREG|INBREG, "\tcvtZLZR\tAL,A1\n"); break; case MZE: expand(p, INAREG|INBREG, "\tmovzZLl\tAL,A1\n"); break; case MVZ: expand(p, INAREG|INBREG, "\tmovzZLZR\tAL,A1\n"); break; case MZC: expand(p, INAREG|INBREG, "\tmovzZLl\tAL,A1\n"); expand(p, INAREG|INBREG, "\tcvtlZR\tA1,A1\n"); break; case 'I': /* unsigned to double */ expand(p, INAREG|INBREG, "\tcvtld\tAL,A1\n"); printf("\tjgeq\t1f\n"); expand(p, INAREG|INBREG, "\taddd2\t$0d4.294967296e+9,A1\n"); printf("1:\n"); break; default: comperr("unsupported conversion %d", o); } switch (o) { case MLE: case CSE: expand(p, INBREG, "\tashl\t$-31,A1,U1\n"); break; case MLZ: case MZE: expand(p, INAREG|INBREG, "\tclrl\tU1\n"); break; } }
void prologue(struct interpass_prolog *ipp) { ftype = ipp->ipp_type; #if 0 if (ipp->ipp_regs > 0 && ipp->ipp_regs != MINRVAR) comperr("fix prologue register savings", ipp->ipp_regs); #endif printf(" RSEG CODE:CODE:REORDER:NOROOT(0)\n"); if (ipp->ipp_vis) printf(" PUBLIC %s\n", ipp->ipp_name); printf("%s:\n", ipp->ipp_name); #if 0 if (xsaveip) { /* Optimizer running, save space on stack */ addto = (p2maxautooff - AUTOINIT)/SZCHAR; printf(" enter #%d\n", addto); } else { #endif /* non-optimized code, jump to epilogue for code generation */ ftlab1 = getlab2(); ftlab2 = getlab2(); printf(" jmp.w " LABFMT "\n", ftlab1); deflab(ftlab2); } /* * End of block. */ void eoftn(struct interpass_prolog *ipp) { #if 0 if (ipp->ipp_regs != MINRVAR) comperr("fix eoftn register savings %x", ipp->ipp_regs); #endif // if (xsaveip == 0) addto = (p2maxautooff - AUTOINIT)/SZCHAR; /* return from function code */ //deflab(ipp->ipp_ip.ip_lbl); //XXX - is this necessary? /* If retval is a pointer and not a function pointer, put in A0 */ if (ISPTR(DECREF(ipp->ipp_type)) && !ISFTN(DECREF(DECREF(ipp->ipp_type)))) printf(" mov.w r0,a0\n"); /* struct return needs special treatment */ if (ftype == STRTY || ftype == UNIONTY) { comperr("fix struct return in eoftn"); } else printf(" exitd\n"); /* Prolog code */ // if (xsaveip == 0) { deflab(ftlab1); printf(" enter #%d\n", addto); printf(" jmp.w " LABFMT "\n", ftlab2); //} } /* * add/sub/... * * Param given: */ void hopcode(int f, int o) { char *str; switch (o) { case PLUS: str = "add"; break; case MINUS: str = "sub"; break; case AND: str = "and"; break; case OR: str = "or"; break; case ER: str = "xor"; break; default: comperr("hopcode2: %d", o); str = 0; /* XXX gcc */ } printf("%s.%c", str, f); }
NODE * clocal(NODE *p) { struct symtab *q; NODE *r, *l; TWORD t; int o; #ifdef PCC_DEBUG if (xdebug) { printf("clocal\n"); fwalk(p, eprint, 0); } #endif switch( o = p->n_op ){ case NAME: /* handle variables */ if ((q = p->n_sp) == NULL) return p; /* Nothing to care about */ switch (q->sclass) { case PARAM: case AUTO: if (0 && q->soffset < MAXZP * SZINT && q->sclass != PARAM) { p->n_lval = -(q->soffset/SZCHAR) + ZPOFF*2; p->n_sp = NULL; } else { /* fake up a structure reference */ r = block(REG, NIL, NIL, PTR+STRTY, 0, 0); r->n_lval = 0; r->n_rval = FPREG; p = stref(block(STREF, r, p, 0, 0, 0)); } break; default: break; } break; case PMCONV: case PVCONV: if( p->n_right->n_op != ICON ) cerror( "bad conversion", 0); nfree(p); p = (buildtree(o==PMCONV?MUL:DIV, p->n_left, p->n_right)); break; case PCONV: t = p->n_type; if (t == INCREF(CHAR) || t == INCREF(UCHAR) || t == INCREF(BOOL) || t == INCREF(VOID)) break; l = p->n_left; t = l->n_type; if (t == INCREF(CHAR) || t == INCREF(UCHAR) || t == INCREF(BOOL) || t == INCREF(VOID)) break; if (p->n_type <= UCHAR || l->n_type <= UCHAR) break; /* must do runtime ptr conv */ /* if conversion to another pointer type, just remove */ if (p->n_type > BTMASK && l->n_type > BTMASK) goto delp; if (l->n_op == ICON && l->n_sp == NULL) goto delp; break; delp: l->n_type = p->n_type; l->n_qual = p->n_qual; l->n_df = p->n_df; l->n_ap = p->n_ap; p = nfree(p); break; } #ifdef PCC_DEBUG if (xdebug) { printf("clocal end\n"); fwalk(p, eprint, 0); } #endif #if 0 register struct symtab *q; register NODE *r, *l; register int o; register int m; TWORD t; //printf("in:\n"); //fwalk(p, eprint, 0); switch( o = p->n_op ){ case NAME: if ((q = p->n_sp) == NULL) return p; /* Nothing to care about */ switch (q->sclass) { case PARAM: case AUTO: /* fake up a structure reference */ r = block(REG, NIL, NIL, PTR+STRTY, 0, 0); r->n_lval = 0; r->n_rval = FPREG; p = stref(block(STREF, r, p, 0, 0, 0)); break; case STATIC: if (q->slevel == 0) break; p->n_lval = 0; p->n_sp = q; break; case REGISTER: p->n_op = REG; p->n_lval = 0; p->n_rval = q->soffset; break; } break; case STCALL: case CALL: /* Fix function call arguments. On x86, just add funarg */ for (r = p->n_right; r->n_op == CM; r = r->n_left) { if (r->n_right->n_op != STARG && r->n_right->n_op != FUNARG) r->n_right = block(FUNARG, r->n_right, NIL, r->n_right->n_type, r->n_right->n_df, r->n_right->n_sue); } if (r->n_op != STARG && r->n_op != FUNARG) { l = talloc(); *l = *r; r->n_op = FUNARG; r->n_left = l; r->n_type = l->n_type; } break; case CBRANCH: l = p->n_left; /* * Remove unnecessary conversion ops. */ if (clogop(l->n_op) && l->n_left->n_op == SCONV) { if (coptype(l->n_op) != BITYPE) break; if (l->n_right->n_op == ICON) { r = l->n_left->n_left; if (r->n_type >= FLOAT && r->n_type <= LDOUBLE) break; /* Type must be correct */ t = r->n_type; nfree(l->n_left); l->n_left = r; l->n_type = t; l->n_right->n_type = t; } } break; case PCONV: /* Remove redundant PCONV's. Be careful */ l = p->n_left; if (l->n_op == ICON) { l->n_lval = (unsigned)l->n_lval; goto delp; } if (l->n_type < INT || l->n_type == LONGLONG || l->n_type == ULONGLONG) { /* float etc? */ p->n_left = block(SCONV, l, NIL, UNSIGNED, 0, 0); break; } /* if left is SCONV, cannot remove */ if (l->n_op == SCONV) break; /* if conversion to another pointer type, just remove */ if (p->n_type > BTMASK && l->n_type > BTMASK) goto delp; break; delp: l->n_type = p->n_type; l->n_qual = p->n_qual; l->n_df = p->n_df; l->n_sue = p->n_sue; nfree(p); p = l; break; case SCONV: l = p->n_left; if (p->n_type == l->n_type) { nfree(p); return l; } if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 && btdims[p->n_type].suesize == btdims[l->n_type].suesize) { if (p->n_type != FLOAT && p->n_type != DOUBLE && l->n_type != FLOAT && l->n_type != DOUBLE && l->n_type != LDOUBLE && p->n_type != LDOUBLE) { if (l->n_op == NAME || l->n_op == UMUL || l->n_op == TEMP) { l->n_type = p->n_type; nfree(p); return l; } } } if (DEUNSIGN(p->n_type) == INT && DEUNSIGN(l->n_type) == INT && coptype(l->n_op) == BITYPE) { l->n_type = p->n_type; nfree(p); return l; } o = l->n_op; m = p->n_type; if (o == ICON) { CONSZ val = l->n_lval; if (!ISPTR(m)) /* Pointers don't need to be conv'd */ switch (m) { case CHAR: l->n_lval = (char)val; break; case UCHAR: l->n_lval = val & 0377; break; case SHORT: l->n_lval = (short)val; break; case USHORT: l->n_lval = val & 0177777; break; case ULONG: case UNSIGNED: l->n_lval = val & 0xffffffff; break; case LONG: case INT: l->n_lval = (int)val; break; case LONGLONG: l->n_lval = (long long)val; break; case ULONGLONG: l->n_lval = val; break; case VOID: break; case LDOUBLE: case DOUBLE: case FLOAT: l->n_op = FCON; l->n_dcon = val; break; default: cerror("unknown type %d", m); } l->n_type = m; l->n_sue = 0; nfree(p); return l; } if (DEUNSIGN(p->n_type) == SHORT && DEUNSIGN(l->n_type) == SHORT) { nfree(p); p = l; } if ((p->n_type == CHAR || p->n_type == UCHAR || p->n_type == SHORT || p->n_type == USHORT) && (l->n_type == FLOAT || l->n_type == DOUBLE || l->n_type == LDOUBLE)) { p = block(SCONV, p, NIL, p->n_type, p->n_df, p->n_sue); p->n_left->n_type = INT; return p; } break; case MOD: case DIV: if (o == DIV && p->n_type != CHAR && p->n_type != SHORT) break; if (o == MOD && p->n_type != CHAR && p->n_type != SHORT) break; /* make it an int division by inserting conversions */ p->n_left = block(SCONV, p->n_left, NIL, INT, 0, 0); p->n_right = block(SCONV, p->n_right, NIL, INT, 0, 0); p = block(SCONV, p, NIL, p->n_type, 0, 0); p->n_left->n_type = INT; break; case PMCONV: case PVCONV: if( p->n_right->n_op != ICON ) cerror( "bad conversion", 0); nfree(p); return(buildtree(o==PMCONV?MUL:DIV, p->n_left, p->n_right)); case FORCE: /* put return value in return reg */ p->n_op = ASSIGN; p->n_right = p->n_left; p->n_left = block(REG, NIL, NIL, p->n_type, 0, 0); p->n_left->n_rval = RETREG(p->n_type); break; case LS: case RS: /* shift count must be in a char * unless longlong, where it must be int */ if (p->n_right->n_op == ICON) break; /* do not do anything */ if (p->n_type == LONGLONG || p->n_type == ULONGLONG) { if (p->n_right->n_type != INT) p->n_right = block(SCONV, p->n_right, NIL, INT, 0, 0); break; } if (p->n_right->n_type == CHAR || p->n_right->n_type == UCHAR) break; p->n_right = block(SCONV, p->n_right, NIL, CHAR, 0, 0); break; } //printf("ut:\n"); //fwalk(p, eprint, 0); #endif return(p); }
/* this is called to do local transformations on * an expression tree preparitory to its being * written out in intermediate code. */ NODE * clocal(NODE *p) { struct symtab *q; NODE *r, *l; int o; int m; TWORD ty; int tmpnr, isptrvoid = 0; #ifdef PCC_DEBUG if (xdebug) { printf("clocal in: %p\n", p); fwalk(p, eprint, 0); } #endif switch (o = p->n_op) { case UCALL: case CALL: case STCALL: case USTCALL: if (p->n_type == VOID) break; /* * if the function returns void*, ecode() invokes * delvoid() to convert it to uchar*. * We just let this happen on the ASSIGN to the temp, * and cast the pointer back to void* on access * from the temp. */ if (p->n_type == PTR+VOID) isptrvoid = 1; r = tempnode(0, p->n_type, p->n_df, p->n_ap); tmpnr = regno(r); r = block(ASSIGN, r, p, p->n_type, p->n_df, p->n_ap); p = tempnode(tmpnr, r->n_type, r->n_df, r->n_ap); if (isptrvoid) { p = block(PCONV, p, NIL, PTR+VOID, p->n_df, 0); } p = buildtree(COMOP, r, p); break; case NAME: if ((q = p->n_sp) == NULL) return p; /* Nothing to care about */ switch (q->sclass) { case PARAM: case AUTO: /* fake up a structure reference */ r = block(REG, NIL, NIL, PTR+STRTY, 0, 0); r->n_lval = 0; r->n_rval = FP; p = stref(block(STREF, r, p, 0, 0, 0)); break; case STATIC: if (q->slevel == 0) break; p->n_lval = 0; p->n_sp = q; break; case REGISTER: p->n_op = REG; p->n_lval = 0; p->n_rval = q->soffset; break; } break; case FUNARG: /* Args smaller than int are given as int */ if (p->n_type != CHAR && p->n_type != UCHAR && p->n_type != SHORT && p->n_type != USHORT) break; p->n_left = block(SCONV, p->n_left, NIL, INT, 0, 0); p->n_type = INT; p->n_ap = 0; p->n_rval = SZINT; break; case CBRANCH: l = p->n_left; /* * Remove unnecessary conversion ops. */ if (clogop(l->n_op) && l->n_left->n_op == SCONV) { if (coptype(l->n_op) != BITYPE) break; if (l->n_right->n_op == ICON) { r = l->n_left->n_left; if (r->n_type >= FLOAT && r->n_type <= LDOUBLE) break; /* Type must be correct */ ty = r->n_type; nfree(l->n_left); l->n_left = r; l->n_type = ty; l->n_right->n_type = ty; } #if 0 else if (l->n_right->n_op == SCONV && l->n_left->n_type == l->n_right->n_type) { r = l->n_left->n_left; nfree(l->n_left); l->n_left = r; r = l->n_right->n_left; nfree(l->n_right); l->n_right = r; } #endif } break; case PCONV: /* Remove redundant PCONV's. Be careful */ l = p->n_left; if (l->n_op == ICON) { l->n_lval = (unsigned)l->n_lval; goto delp; } if (l->n_type < INT || DEUNSIGN(l->n_type) == LONGLONG) { /* float etc? */ p->n_left = block(SCONV, l, NIL, UNSIGNED, 0, 0); break; } /* if left is SCONV, cannot remove */ if (l->n_op == SCONV) break; /* avoid ADDROF TEMP */ if (l->n_op == ADDROF && l->n_left->n_op == TEMP) break; /* if conversion to another pointer type, just remove */ if (p->n_type > BTMASK && l->n_type > BTMASK) goto delp; break; delp: l->n_type = p->n_type; l->n_qual = p->n_qual; l->n_df = p->n_df; l->n_ap = p->n_ap; nfree(p); p = l; break; case SCONV: l = p->n_left; if (p->n_type == l->n_type) { nfree(p); p = l; break; } if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 && tsize(p->n_type, p->n_df, p->n_ap) == tsize(l->n_type, l->n_df, l->n_ap)) { if (p->n_type != FLOAT && p->n_type != DOUBLE && l->n_type != FLOAT && l->n_type != DOUBLE && l->n_type != LDOUBLE && p->n_type != LDOUBLE) { if (l->n_op == NAME || l->n_op == UMUL || l->n_op == TEMP) { l->n_type = p->n_type; nfree(p); p = l; break; } } } if (DEUNSIGN(p->n_type) == INT && DEUNSIGN(l->n_type) == INT && coptype(l->n_op) == BITYPE) { l->n_type = p->n_type; nfree(p); p = l; } if (DEUNSIGN(p->n_type) == SHORT && DEUNSIGN(l->n_type) == SHORT) { nfree(p); p = l; } /* convert float/double to int before to (u)char/(u)short */ if ((DEUNSIGN(p->n_type) == CHAR || DEUNSIGN(p->n_type) == SHORT) && (l->n_type == FLOAT || l->n_type == DOUBLE || l->n_type == LDOUBLE)) { p = block(SCONV, p, NIL, p->n_type, p->n_df, p->n_ap); p->n_left->n_type = INT; break; } /* convert (u)char/(u)short to int before float/double */ if ((p->n_type == FLOAT || p->n_type == DOUBLE || p->n_type == LDOUBLE) && (DEUNSIGN(l->n_type) == CHAR || DEUNSIGN(l->n_type) == SHORT)) { p = block(SCONV, p, NIL, p->n_type, p->n_df, p->n_ap); p->n_left->n_type = INT; break; } o = l->n_op; m = p->n_type; if (o == ICON) { CONSZ val = l->n_lval; if (!ISPTR(m)) /* Pointers don't need to be conv'd */ switch (m) { case BOOL: l->n_lval = l->n_lval != 0; break; case CHAR: l->n_lval = (char)val; break; case UCHAR: l->n_lval = val & 0377; break; case SHORT: l->n_lval = (short)val; break; case USHORT: l->n_lval = val & 0177777; break; case ULONG: case UNSIGNED: l->n_lval = val & 0xffffffff; break; case LONG: case INT: l->n_lval = (int)val; break; case LONGLONG: l->n_lval = (long long)val; break; case ULONGLONG: l->n_lval = val; break; case VOID: break; case LDOUBLE: case DOUBLE: case FLOAT: l->n_op = FCON; l->n_dcon = val; break; default: cerror("unknown type %d", m); } l->n_type = m; nfree(p); p = l; } else if (o == FCON) { l->n_lval = l->n_dcon; l->n_sp = NULL; l->n_op = ICON; l->n_type = m; l->n_ap = 0; nfree(p); p = clocal(l); } break; case MOD: case DIV: if (o == DIV && p->n_type != CHAR && p->n_type != SHORT) break; if (o == MOD && p->n_type != CHAR && p->n_type != SHORT) break; /* make it an int division by inserting conversions */ p->n_left = block(SCONV, p->n_left, NIL, INT, 0, 0); p->n_right = block(SCONV, p->n_right, NIL, INT, 0, 0); p = block(SCONV, p, NIL, p->n_type, 0, 0); p->n_left->n_type = INT; break; case FORCE: /* put return value in return reg */ p->n_op = ASSIGN; p->n_right = p->n_left; p->n_left = block(REG, NIL, NIL, p->n_type, 0, 0); p->n_left->n_rval = RETREG(p->n_type); break; } #ifdef PCC_DEBUG if (xdebug) { printf("clocal out: %p\n", p); fwalk(p, eprint, 0); } #endif return(p); }
/* * Do the "hard work" in assigning correct destination for arguments. * Also convert arguments < INT to inte (default argument promotions). * XXX - should be dome elsewhere. */ static NODE * argput(NODE *p) { NODE *q; TWORD ty; int typ, r, ssz; if (p->n_op == CM) { p->n_left = argput(p->n_left); p->n_right = argput(p->n_right); return p; } /* first arg may be struct return pointer */ /* XXX - check if varargs; setup al */ switch (typ = argtyp(p->n_type, p->n_df, p->n_ap)) { case INTEGER: case SSE: if (typ == SSE) r = XMM0 + nsse++; else r = argregsi[ngpr++]; if (p->n_type < INT || p->n_type == BOOL) p = cast(p, INT, 0); p = movtoreg(p, r); break; case X87: r = nrsp; nrsp += SZLDOUBLE; p = movtomem(p, r, STKREG); break; case SSEMEM: r = nrsp; nrsp += SZDOUBLE; p = movtomem(p, r, STKREG); break; case INTMEM: r = nrsp; nrsp += SZLONG; p = movtomem(p, r, STKREG); break; case STRCPX: case STRREG: /* Struct in registers */ /* Cast to long pointer and move to the registers */ /* XXX can overrun struct size */ /* XXX check carefully for SSE members */ ssz = tsize(p->n_type, p->n_df, p->n_ap); if (typ == STRCPX) { ty = DOUBLE; r = XMM0 + nsse++; } else { ty = LONG; r = argregsi[ngpr++]; } if (ssz <= SZLONG) { q = cast(p->n_left, INCREF(ty), 0); nfree(p); q = buildtree(UMUL, q, NIL); p = movtoreg(q, r); } else if (ssz <= SZLONG*2) { NODE *ql, *qr; if (!ISPTR(p->n_left->n_type)) cerror("no struct arg pointer"); p = nfree(p); p = makety(p, PTR|ty, 0, 0, 0); qr = ccopy(ql = tempnode(0, PTR|ty, 0, 0)); p = buildtree(ASSIGN, ql, p); ql = movtoreg(buildtree(UMUL, ccopy(qr), NIL), r); p = buildtree(COMOP, p, ql); ql = buildtree(UMUL, buildtree(PLUS, qr, bcon(1)), NIL); r = (typ == STRCPX ? XMM0 + nsse++ : argregsi[ngpr++]); ql = movtoreg(ql, r); p = buildtree(CM, p, ql); } else cerror("STRREG"); break; case STRMEM: { struct symtab s; NODE *l, *t; q = buildtree(UMUL, p->n_left, NIL); s.stype = p->n_type; s.squal = 0; s.sdf = p->n_df; s.sap = p->n_ap; s.soffset = nrsp; s.sclass = AUTO; nrsp += tsize(p->n_type, p->n_df, p->n_ap); l = block(REG, NIL, NIL, PTR+STRTY, 0, 0); l->n_lval = 0; regno(l) = STKREG; t = block(NAME, NIL, NIL, p->n_type, p->n_df, p->n_ap); t->n_sp = &s; t = stref(block(STREF, l, t, 0, 0, 0)); t = (buildtree(ASSIGN, t, q)); nfree(p); p = t->n_left; nfree(t); break; } default: cerror("argument %d", typ); } return p; }
/* clocal() is called to do local transformations on * an expression tree preparitory to its being * written out in intermediate code. * * the major essential job is rewriting the * automatic variables and arguments in terms of * REG and OREG nodes * conversion ops which are not necessary are also clobbered here * in addition, any special features (such as rewriting * exclusive or) are easily handled here as well */ NODE * clocal(NODE *p) { register struct symtab *q; register NODE *r, *l; register int o; TWORD t; #ifdef PCC_DEBUG if (xdebug) { printf("clocal: %p\n", p); fwalk(p, eprint, 0); } #endif switch( o = p->n_op ){ case NAME: if ((q = p->n_sp) == NULL) return p; /* Nothing to care about */ switch (q->sclass) { case PARAM: case AUTO: /* fake up a structure reference */ r = block(REG, NIL, NIL, PTR+STRTY, 0, 0); slval(r, 0); r->n_rval = FPREG; p = stref(block(STREF, r, p, 0, 0, 0)); break; case REGISTER: p->n_op = REG; slval(p, 0); p->n_rval = q->soffset; break; case USTATIC: case STATIC: if (kflag == 0) break; if (blevel > 0 && !statinit) p = picstatic(p); break; case EXTERN: case EXTDEF: if (kflag == 0) break; if (blevel > 0 && !statinit) p = picext(p); break; } break; case ADDROF: if (kflag == 0 || blevel == 0 || statinit) break; /* char arrays may end up here */ l = p->n_left; if (l->n_op != NAME || (l->n_type != ARY+CHAR && l->n_type != ARY+WCHAR_TYPE)) break; l = p; p = picstatic(p->n_left); nfree(l); if (p->n_op != UMUL) cerror("ADDROF error"); l = p; p = p->n_left; nfree(l); break; case STASG: /* convert struct assignment to call memcpy */ l = p->n_left; if (l->n_op == NAME && ISFTN(l->n_sp->stype)) break; /* struct return, do nothing */ /* first construct arg list */ p->n_left = buildtree(ADDROF, p->n_left, 0); r = bcon(tsize(STRTY, p->n_df, p->n_ap)/SZCHAR); p->n_left = buildtree(CM, p->n_left, p->n_right); p->n_right = r; p->n_op = CM; p->n_type = INT; r = block(NAME, NIL, NIL, INT, 0, 0); r->n_sp = lookup(addname("memcpy"), SNORMAL); if (r->n_sp->sclass == SNULL) { r->n_sp->sclass = EXTERN; r->n_sp->stype = INCREF(VOID+PTR)+(FTN-PTR); } r->n_type = r->n_sp->stype; p = buildtree(CALL, r, p); break; case SCONV: l = p->n_left; if (l->n_op == ICON && ISPTR(l->n_type)) { /* Do immediate cast here */ /* Should be common code */ q = l->n_sp; l->n_sp = NULL; l->n_type = UNSIGNED; if (concast(l, p->n_type) == 0) cerror("clocal"); p = nfree(p); p->n_sp = q; } break; case FORCE: /* put return value in return reg */ p->n_op = ASSIGN; p->n_right = p->n_left; p->n_left = block(REG, NIL, NIL, p->n_type, 0, 0); t = p->n_type; if (ISITY(t)) t = t - (FIMAG-FLOAT); p->n_left->n_rval = RETREG(t); break; } #ifdef PCC_DEBUG if (xdebug) { printf("clocal end: %p\n", p); fwalk(p, eprint, 0); } #endif return(p); }
/* * does the type t match tword */ int ttype(TWORD t, int tword) { if (tword & TANY) return(1); #ifdef PCC_DEBUG if (t2debug) printf("ttype(0x%x, 0x%x)\n", t, tword); #endif if (ISPTR(t) && ISFTN(DECREF(t)) && (tword & TFTN)) { /* For funny function pointers */ return 1; } if (ISPTR(t) && (tword&TPTRTO)) { do { t = DECREF(t); } while (ISARY(t)); /* arrays that are left are usually only * in structure references... */ return (ttype(t, tword&(~TPTRTO))); } if (t != BTYPE(t)) return (tword & TPOINT); /* TPOINT means not simple! */ if (tword & TPTRTO) return(0); switch (t) { case CHAR: return( tword & TCHAR ); case SHORT: return( tword & TSHORT ); case STRTY: case UNIONTY: return( tword & TSTRUCT ); case INT: return( tword & TINT ); case UNSIGNED: return( tword & TUNSIGNED ); case USHORT: return( tword & TUSHORT ); case UCHAR: return( tword & TUCHAR ); case ULONG: return( tword & TULONG ); case LONG: return( tword & TLONG ); case LONGLONG: return( tword & TLONGLONG ); case ULONGLONG: return( tword & TULONGLONG ); case FLOAT: return( tword & TFLOAT ); case DOUBLE: return( tword & TDOUBLE ); case LDOUBLE: return( tword & TLDOUBLE ); } return(0); }
/* * Find a matching assign op. * * Level assignment for priority: * left right prio * - - - * direct direct 1 * direct REG 2 * direct OREG 3 * OREG direct 4 * OREG REG 5 * OREG OREG 6 */ int findasg(NODE *p, int cookie) { extern int *qtable[]; struct optab *q; int i, sh, shl, shr, lvl = 10; NODE *l, *r; int *ixp; struct optab *qq = NULL; /* XXX gcc */ int idx = 0, gol = 0, gor = 0; shl = shr = 0; F2DEBUG(("findasg tree: %s\n", prcook(cookie))); F2WALK(p); ixp = qtable[p->n_op]; l = getlr(p, 'L'); r = getlr(p, 'R'); for (i = 0; ixp[i] >= 0; i++) { q = &table[ixp[i]]; F2DEBUG(("findasg: ixp %d\n", ixp[i])); if (!acceptable(q)) /* target-dependent filter */ continue; if (ttype(l->n_type, q->ltype) == 0 || ttype(r->n_type, q->rtype) == 0) continue; /* Types must be correct */ if ((cookie & q->visit) == 0) continue; /* must get a result */ F2DEBUG(("findasg got types\n")); #ifdef mach_pdp11 /* XXX - check for other targets too */ if (p->n_op == STASG && ISPTR(l->n_type)) { /* Accept lvalue to be in register */ /* if struct assignment is given a pointer */ if ((shl = chcheck(l, q->lshape, q->rewrite & RLEFT)) == SRNOPE) continue; } else #endif { if ((shl = tshape(l, q->lshape)) == SRNOPE) continue; if (shl == SRREG) continue; } F2DEBUG(("findasg lshape %d\n", shl)); F2WALK(l); if ((shr = chcheck(r, q->rshape, q->rewrite & RRIGHT)) == SRNOPE) continue; F2DEBUG(("findasg rshape %d\n", shr)); F2WALK(r); if (q->needs & REWRITE) break; /* Done here */ if (lvl <= (shl + shr)) continue; lvl = shl + shr; qq = q; idx = ixp[i]; gol = shl; gor = shr; } if (lvl == 10) { F2DEBUG(("findasg failed\n")); if (setasg(p, cookie)) return FRETRY; return FFAIL; } F2DEBUG(("findasg entry %d(%s,%s)\n", idx, srtyp[gol], srtyp[gor])); sh = -1; sh = shswitch(sh, p->n_left, qq->lshape, cookie, qq->rewrite & RLEFT, gol); sh = shswitch(sh, p->n_right, qq->rshape, cookie, qq->rewrite & RRIGHT, gor); #ifdef mach_pdp11 /* XXX all targets? */ lvl = 0; if (cookie == FOREFF) lvl = RVEFF, sh = 0; else if (cookie == FORCC) lvl = RVCC, sh = 0; else if (sh == -1) { sh = ffs(cookie & qq->visit & INREGS)-1; #ifdef PCC_DEBUG if (sh == -1) comperr("findasg bad shape"); #endif SCLASS(lvl,sh); } else SCLASS(lvl,sh); p->n_su = MKIDX(idx, lvl); #else if (sh == -1) { if (cookie == FOREFF) sh = 0; else sh = ffs(cookie & qq->visit & INREGS)-1; } F2DEBUG(("findasg: node %p class %d\n", p, sh)); p->n_su = MKIDX(idx, 0); SCLASS(p->n_su, sh); #endif /* mach_pdp11 */ #ifdef FINDMOPS p->n_su &= ~ISMOPS; #endif return sh; }
/* * Evaluate 128-bit operands. */ NODE * gcc_eval_timode(int op, NODE *p1, NODE *p2) { struct attr *a1, *a2; struct symtab *sp; NODE *p; int isu = 0, gotti, isaop; if (op == CM) return buildtree(op, p1, p2); a1 = isti(p1); a2 = isti(p2); if (a1 == NULL && a2 == NULL) return NULL; if (op == ASSIGN) return timodeassign(p1, p2); gotti = (a1 != NULL); gotti += (a2 != NULL); if (gotti == 0) return NULL; if (a1 != NULL) isu = a1->iarg(1); if (a2 != NULL && !isu) isu = a2->iarg(1); if (a1 == NULL) { p1 = ticast(p1, isu); a1 = attr_find(p1->n_ap, GCC_ATYP_MODE); } if (a2 == NULL && (cdope(op) & SHFFLG) == 0) { p2 = ticast(p2, isu); a2 = attr_find(p2->n_ap, GCC_ATYP_MODE); } switch (op) { case GT: case GE: case LT: case LE: case EQ: case NE: /* change to call */ sp = isu ? ucmpti2sp : cmpti2sp; p = doacall(sp, nametree(sp), buildtree(CM, p1, p2)); p = buildtree(op, p, bcon(1)); break; case AND: case ER: case OR: if (!ISPTR(p1->n_type)) p1 = buildtree(ADDROF, p1, NIL); if (!ISPTR(p2->n_type)) p2 = buildtree(ADDROF, p2, NIL); p = gcc_andorer(op, p1, p2); break; case LSEQ: case RSEQ: case LS: case RS: sp = op == LS || op == LSEQ ? ashldi3sp : isu ? lshrdi3sp : ashrdi3sp; p2 = cast(p2, INT, 0); /* XXX p1 ccopy may have side effects */ p = doacall(sp, nametree(sp), buildtree(CM, ccopy(p1), p2)); if (op == LSEQ || op == RSEQ) { p = buildtree(ASSIGN, p1, p); } else tfree(p1); break; case PLUSEQ: case MINUSEQ: case MULEQ: case DIVEQ: case MODEQ: case PLUS: case MINUS: case MUL: case DIV: case MOD: isaop = (cdope(op)&ASGOPFLG); if (isaop) op = UNASG op; sp = op == PLUS ? addvti3sp : op == MINUS ? subvti3sp : op == MUL ? mulvti3sp : op == DIV ? (isu ? udivti3sp : divti3sp) : op == MOD ? (isu ? umodti3sp : modti3sp) : 0; /* XXX p1 ccopy may have side effects */ p = doacall(sp, nametree(sp), buildtree(CM, ccopy(p1), p2)); if (isaop) p = buildtree(ASSIGN, p1, p); else tfree(p1); break; default: uerror("unsupported TImode op %d", op); p = bcon(0); } return p; }
static debug_type parse_coff_type (bfd *abfd, struct coff_symbols *symbols, struct coff_types *types, long coff_symno, int ntype, union internal_auxent *pauxent, bfd_boolean useaux, void *dhandle) { debug_type type; if ((ntype & ~N_BTMASK) != 0) { int newtype; newtype = DECREF (ntype); if (ISPTR (ntype)) { type = parse_coff_type (abfd, symbols, types, coff_symno, newtype, pauxent, useaux, dhandle); type = debug_make_pointer_type (dhandle, type); } else if (ISFCN (ntype)) { type = parse_coff_type (abfd, symbols, types, coff_symno, newtype, pauxent, useaux, dhandle); type = debug_make_function_type (dhandle, type, (debug_type *) NULL, FALSE); } else if (ISARY (ntype)) { int n; if (pauxent == NULL) n = 0; else { unsigned short *dim; int i; /* FIXME: If pauxent->x_sym.x_tagndx.l == 0, gdb sets the c_naux field of the syment to 0. */ /* Move the dimensions down, so that the next array picks up the next one. */ dim = pauxent->x_sym.x_fcnary.x_ary.x_dimen; n = dim[0]; for (i = 0; *dim != 0 && i < DIMNUM - 1; i++, dim++) *dim = *(dim + 1); *dim = 0; } type = parse_coff_type (abfd, symbols, types, coff_symno, newtype, pauxent, FALSE, dhandle); type = debug_make_array_type (dhandle, type, parse_coff_base_type (abfd, symbols, types, coff_symno, T_INT, NULL, dhandle), 0, n - 1, FALSE); } else { non_fatal (_("parse_coff_type: Bad type code 0x%x"), ntype); return DEBUG_TYPE_NULL; } return type; } if (pauxent != NULL && pauxent->x_sym.x_tagndx.l > 0) { debug_type *slot; /* This is a reference to an existing type. FIXME: gdb checks that the class is not C_STRTAG, nor C_UNTAG, nor C_ENTAG. */ slot = coff_get_slot (types, pauxent->x_sym.x_tagndx.l); if (*slot != DEBUG_TYPE_NULL) return *slot; else return debug_make_indirect_type (dhandle, slot, (const char *) NULL); } /* If the aux entry has already been used for something, useaux will have been set to false, indicating that parse_coff_base_type should not use it. We need to do it this way, rather than simply passing pauxent as NULL, because we need to be able handle multiple array dimensions while still discarding pauxent after having handled all of them. */ if (! useaux) pauxent = NULL; return parse_coff_base_type (abfd, symbols, types, coff_symno, ntype, pauxent, dhandle); }
void zzzcode(NODE * p, int c) { char *str; NODE *l, *r; int sz; l = p->n_left; r = p->n_right; switch (c) { case 'A': /* Add const. */ if (ISPTR(l->n_type) && l->n_rval == FP) r->n_lval += V9BIAS; if (SIMM13(r->n_lval)) expand(p, 0, "\tadd AL,AR,A1\t\t! add const\n"); else expand(p, 0, "\tsetx AR,A3,A2\t\t! add const\n" "\tadd AL,A2,A1\n"); break; case 'B': /* Subtract const. */ if (ISPTR(l->n_type) && l->n_rval == FP) r->n_lval -= V9BIAS; if (SIMM13(r->n_lval)) expand(p, 0, "\tsub AL,AR,A1\t\t! subtract const\n"); else expand(p, 0, "\tsetx AR,A3,A2\t\t! subtract const\n" "\tsub AL,A2,A1\n"); break; case 'C': /* Load constant to register. */ if (ISPTR(p->n_type)) expand(p, 0, "\tsethi %h44(AL),A1\t\t! load label\n" "\tor A1,%m44(AL),A1\n" "\tsllx A1,12,A1\n" "\tor A1,%l44(AL),A1\n"); else if (SIMM13(p->n_lval)) expand(p, 0, "\tor %g0,AL,A1\t\t\t! load const\n"); else expand(p, 0, "\tsetx AL,A2,A1\t\t! load const\n"); break; case 'F': /* Floating-point comparison, cf. hopcode(). */ switch (p->n_op) { case EQ: str = "fbe"; break; case NE: str = "fbne"; break; case ULE: case LE: str = "fbule"; break; case ULT: case LT: str = "fbul"; break; case UGE: case GE: str = "fbuge"; break; case UGT: case GT: str = "fbug"; break; /* XXX case PLUS: str = "add"; break; case MINUS: str = "sub"; break; case AND: str = "and"; break; case OR: str = "or"; break; case ER: str = "xor"; break;*/ default: comperr("unknown float code: %d", p->n_op); return; } printf(str); break; case 'Q': /* Structure assignment. */ /* TODO Check if p->n_stsize is small and use a few ldx's to move the struct instead of memcpy. The equiv. could be done on all the architectures. */ sz = attr_find(p->n_ap, ATTR_P2STRUCT)->iarg(0); if (l->n_rval != O0) printf("\tmov %s,%s\n", rnames[l->n_rval], rnames[O0]); if (SIMM13(sz)) printf("\tor %%g0,%d,%%o2\n", sz); else printf("\tsetx %d,%%g1,%%o2\n", sz); printf("\tcall memcpy\t\t\t! struct assign (dest, src, len)\n"); printf("\tnop\n"); break; default: cerror("unknown zzzcode call: %c", c); } }
void zzzcode(NODE *p, int c) { NODE *l, *r; TWORD t; int m; char *ch; switch (c) { case 'N': /* logical ops, turned into 0-1 */ /* use register given by register 1 */ cbgen( 0, m=getlab2()); deflab( p->n_label ); printf( " clrl %s\n", rnames[getlr( p, '1' )->n_rval] ); deflab( m ); return; case 'A': /* Assign a constant directly to a memory position */ printf("\t"); if (p->n_type < LONG || ISPTR(p->n_type)) casg(p); else casg64(p); printf("\n"); break; case 'B': /* long long compare */ twollcomp(p); break; case 'C': /* num words pushed on arg stack */ printf("$%d", p->n_qual); break; case 'D': /* INCR and DECR */ zzzcode(p->n_left, 'A'); printf("\n "); #if 0 case 'E': /* INCR and DECR, FOREFF */ if (p->n_right->n_lval == 1) { printf("%s", (p->n_op == INCR ? "inc" : "dec") ); prtype(p->n_left); printf(" "); adrput(stdout, p->n_left); return; } printf("%s", (p->n_op == INCR ? "add" : "sub") ); prtype(p->n_left); printf("2 "); adrput(stdout, p->n_right); printf(","); adrput(p->n_left); return; #endif case 'F': /* register type of right operand */ { register NODE *n; register int ty; n = getlr( p, 'R' ); ty = n->n_type; if (x2debug) printf("->%d<-", ty); if ( ty==DOUBLE) printf("d"); else if ( ty==FLOAT ) printf("f"); else printf("l"); return; } case 'G': /* emit conversion instructions */ sconv(p); break; case 'J': /* jump or ret? */ { struct interpass *ip = DLIST_PREV((struct interpass *)p2env.epp, qelem); if (ip->type != IP_DEFLAB || ip->ip_lbl != getlr(p, 'L')->n_lval) expand(p, FOREFF, "jbr LL"); else printf("ret"); } break; case 'L': /* type of left operand */ case 'R': /* type of right operand */ { register NODE *n; n = getlr ( p, c); if (x2debug) printf("->%d<-", n->n_type); prtype(n); return; } case 'O': /* print out emulated ops */ expand(p, FOREFF, "\tmovq AR,-(%sp)\n"); expand(p, FOREFF, "\tmovq AL,-(%sp)\n"); if (p->n_op == DIV && p->n_type == ULONGLONG) ch = "udiv"; else if (p->n_op == DIV) ch = "div"; else if (p->n_op == MOD && p->n_type == ULONGLONG) ch = "umod"; else if (p->n_op == MOD) ch = "mod"; else if (p->n_op == MUL) ch = "mul"; else ch = 0, comperr("ZO %d", p->n_op); printf("\tcalls $4,__%sdi3\n", ch); break; case 'Z': /* complement mask for bit instr */ printf("$%Ld", ~p->n_right->n_lval); return; case 'U': /* 32 - n, for unsigned right shifts */ t = DEUNSIGN(p->n_left->n_type); m = t == CHAR ? 8 : t == SHORT ? 16 : 32; printf("$" CONFMT, m - p->n_right->n_lval); return; case 'T': /* rounded structure length for arguments */ { int size; size = p->n_stsize; SETOFF( size, 4); printf("$%d", size); return; } case 'S': /* structure assignment */ { register int size; size = p->n_stsize; l = r = NULL; /* XXX gcc */ if( p->n_op == STASG ){ l = p->n_left; r = p->n_right; } else if( p->n_op == STARG ){ /* store an arg into a temporary */ printf("\tsubl2 $%d,%%sp\n", size < 4 ? 4 : size); l = mklnode(OREG, 0, SP, INT); r = p->n_left; } else cerror( "STASG bad" ); if( r->n_op == ICON ) r->n_op = NAME; else if( r->n_op == REG ) r->n_op = OREG; else if( r->n_op != OREG ) cerror( "STASG-r" ); if (size != 0) { if( size <= 0 || size > 65535 ) cerror("structure size <0=0 or >65535"); switch(size) { case 1: printf(" movb "); break; case 2: printf(" movw "); break; case 4: printf(" movl "); break; case 8: printf(" movq "); break; default: printf(" movc3 $%d,", size); break; } adrput(stdout, r); printf(","); adrput(stdout, l); printf("\n"); } if( r->n_op == NAME ) r->n_op = ICON; else if( r->n_op == OREG ) r->n_op = REG; if (p->n_op == STARG) tfree(l); } break; default: comperr("illegal zzzcode '%c'", c); } }
/* clocal() is called to do local transformations on * an expression tree preparitory to its being * written out in intermediate code. * * the major essential job is rewriting the * automatic variables and arguments in terms of * REG and OREG nodes * conversion ops which are not necessary are also clobbered here * in addition, any special features (such as rewriting * exclusive or) are easily handled here as well */ NODE * clocal(NODE *p) { register struct symtab *q; register NODE *r, *l; register int o; register int m; TWORD t; #ifdef PCC_DEBUG if (xdebug) { printf("clocal: %p\n", p); fwalk(p, eprint, 0); } #endif switch( o = p->n_op ){ case NAME: if ((q = p->n_sp) == NULL) return p; /* Nothing to care about */ switch (q->sclass) { case PARAM: case AUTO: /* fake up a structure reference */ r = block(REG, NIL, NIL, PTR+STRTY, 0, 0); slval(r, 0); r->n_rval = FPREG; p = stref(block(STREF, r, p, 0, 0, 0)); break; case USTATIC: if (kflag == 0) break; /* FALLTHROUGH */ case STATIC: #ifdef TLS if (q->sflags & STLS) { p = tlsref(p); break; } #endif break; case REGISTER: p->n_op = REG; slval(p, 0); p->n_rval = q->soffset; break; case EXTERN: case EXTDEF: if (q->sflags & STLS) { p = tlsref(p); break; } if (kflag == 0 || statinit) break; if (blevel > 0) p = picext(p); break; } break; case UCALL: case USTCALL: /* For now, always clear eax */ l = block(REG, NIL, NIL, INT, 0, 0); regno(l) = RAX; p->n_right = clocal(buildtree(ASSIGN, l, bcon(0))); p->n_op -= (UCALL-CALL); break; case SCONV: /* Special-case shifts */ if (p->n_type == LONG && (l = p->n_left)->n_op == LS && l->n_type == INT && l->n_right->n_op == ICON) { p->n_left = l->n_left; p = buildtree(LS, p, l->n_right); nfree(l); break; } l = p->n_left; /* Float conversions may need extra casts */ if (p->n_type == FLOAT || p->n_type == DOUBLE || p->n_type == LDOUBLE) { if (l->n_type < INT || l->n_type == BOOL) { p->n_left = block(SCONV, l, NIL, ISUNSIGNED(l->n_type) ? UNSIGNED : INT, l->n_df, l->n_ap); break; } } if (p->n_type == l->n_type) { nfree(p); return l; } if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 && tsize(p->n_type, p->n_df, p->n_ap) == tsize(l->n_type, l->n_df, l->n_ap)) { if (p->n_type != FLOAT && p->n_type != DOUBLE && l->n_type != FLOAT && l->n_type != DOUBLE && l->n_type != LDOUBLE && p->n_type != LDOUBLE) { if (l->n_op == NAME || l->n_op == UMUL || l->n_op == TEMP) { l->n_type = p->n_type; nfree(p); return l; } } } if (DEUNSIGN(p->n_type) == INT && DEUNSIGN(l->n_type) == INT && coptype(l->n_op) == BITYPE && l->n_op != COMOP && l->n_op != QUEST && l->n_op != ASSIGN) { l->n_type = p->n_type; nfree(p); return l; } o = l->n_op; m = p->n_type; if (o == ICON) { CONSZ val = glval(l); /* if named constant and pointer, allow cast to long/ulong */ if (!nncon(l) && (l->n_type & TMASK) && (m == LONG || m == ULONG)) { l->n_type = m; l->n_ap = 0; return nfree(p); } if (ISPTR(l->n_type) && !nncon(l)) break; /* cannot convert named pointers */ if (!ISPTR(m)) /* Pointers don't need to be conv'd */ switch (m) { case BOOL: val = nncon(l) ? (val != 0) : 1; slval(l, val); l->n_sp = NULL; break; case CHAR: slval(l, (char)val); break; case UCHAR: slval(l, val & 0377); break; case SHORT: slval(l, (short)val); break; case USHORT: slval(l, val & 0177777); break; case UNSIGNED: slval(l, val & 0xffffffff); break; case INT: slval(l, (int)val); break; case LONG: case LONGLONG: slval(l, (long long)val); break; case ULONG: case ULONGLONG: slval(l, val); break; case VOID: break; case LDOUBLE: case DOUBLE: case FLOAT: l->n_op = FCON; l->n_dcon = fltallo(); FCAST(l->n_dcon)->fp = val; break; default: cerror("unknown type %d", m); } l->n_type = m; l->n_ap = NULL; nfree(p); return l; } else if (l->n_op == FCON) { CONSZ lv; if (p->n_type == BOOL) lv = !FLOAT_ISZERO(FCAST(l->n_dcon)); else { FLOAT_FP2INT(lv, FCAST(l->n_dcon), m); } slval(l, lv); l->n_sp = NULL; l->n_op = ICON; l->n_type = m; l->n_ap = NULL; nfree(p); return clocal(l); } if ((p->n_type == CHAR || p->n_type == UCHAR || p->n_type == SHORT || p->n_type == USHORT) && (l->n_type == FLOAT || l->n_type == DOUBLE || l->n_type == LDOUBLE)) { p = block(SCONV, p, NIL, p->n_type, p->n_df, p->n_ap); p->n_left->n_type = INT; return p; } break; case MOD: case DIV: if (o == DIV && p->n_type != CHAR && p->n_type != SHORT) break; if (o == MOD && p->n_type != CHAR && p->n_type != SHORT) break; /* make it an int division by inserting conversions */ p->n_left = makety(p->n_left, INT, 0, 0, 0); p->n_right = makety(p->n_right, INT, 0, 0, 0); p = makety(p, p->n_type, 0, 0, 0); p->n_left->n_type = INT; break; case FORCE: /* put return value in return reg */ p->n_op = ASSIGN; p->n_right = p->n_left; p->n_left = block(REG, NIL, NIL, p->n_type, 0, 0); t = p->n_type; if (ISITY(t)) t = t - (FIMAG-FLOAT); p->n_left->n_rval = p->n_left->n_type == BOOL ? RETREG(CHAR) : RETREG(t); break; case LS: case RS: /* shift count must be in a char */ if (p->n_right->n_type == CHAR || p->n_right->n_type == UCHAR) break; p->n_right = makety(p->n_right, CHAR, 0, 0, 0); break; } #ifdef PCC_DEBUG if (xdebug) { printf("clocal end: %p\n", p); fwalk(p, eprint, 0); } #endif return(p); }
NODE * amd64_builtin_va_arg(NODE *f, NODE *a, TWORD t) { NODE *ap, *r, *dp; ap = a->n_left; dp = a->n_right; if (dp->n_type <= ULONGLONG || ISPTR(dp->n_type) || dp->n_type == FLOAT || dp->n_type == DOUBLE) { /* type might be in general register */ if (dp->n_type == FLOAT || dp->n_type == DOUBLE) { f->n_sp = lookup(fpnext, SNORMAL); varneeds |= NEED_FPNEXT; } else { f->n_sp = lookup(gpnext, SNORMAL); varneeds |= NEED_GPNEXT; } f->n_type = f->n_sp->stype = INCREF(dp->n_type) + (FTN-PTR); f->n_ap = dp->n_ap; f->n_df = /* dp->n_df */ NULL; f = clocal(f); r = buildtree(CALL, f, ccopy(ap)); } else if (ISSOU(dp->n_type) || dp->n_type == LDOUBLE) { /* put a reference directly to the stack */ int sz = tsize(dp->n_type, dp->n_df, dp->n_ap); int al = talign(dp->n_type, dp->n_ap); if (al < ALLONG) al = ALLONG; if (sz <= SZLONG*2 && al == ALLONG) { if (sz <= SZLONG) { f->n_sp = lookup(_1regref, SNORMAL); varneeds |= NEED_1REGREF; } else { f->n_sp = lookup(_2regref, SNORMAL); varneeds |= NEED_2REGREF; } f->n_type = f->n_sp->stype; f = clocal(f); r = buildtree(CALL, f, ccopy(ap)); r = ccast(r, INCREF(dp->n_type), 0, dp->n_df, dp->n_ap); r = buildtree(UMUL, r, NIL); } else { f->n_sp = lookup(memref, SNORMAL); varneeds |= NEED_MEMREF; f->n_type = f->n_sp->stype; f = clocal(f); SETOFF(sz, al); r = buildtree(CALL, f, buildtree(CM, ccopy(ap), bcon(sz/SZCHAR))); r = ccast(r, INCREF(dp->n_type), 0, dp->n_df, dp->n_ap); r = buildtree(UMUL, r, NIL); } } else { uerror("amd64_builtin_va_arg not supported type"); goto bad; } tfree(a); return r; bad: uerror("bad argument to __builtin_va_arg"); return bcon(0); }
/* * code for the beginning of a function; a is an array of * indices in symtab for the arguments; n is the number * On m16k, space is allocated on stack for register arguments, * arguments are moved to the stack and symtab is updated accordingly. */ void bfcode(struct symtab **a, int n) { struct symtab *s; int i, r0l, r0h, a0, r2, sz, hasch, stk; int argoff = ARGINIT; if (cftnsp->stype == STRTY+FTN || cftnsp->stype == UNIONTY+FTN) { /* Function returns struct, adjust arg offset */ for (i = 0; i < n; i++) a[i]->soffset += SZPOINT(INT); } /* first check if there are 1-byte parameters */ for (hasch = i = 0; i < n && i < 6; i++) if (DEUNSIGN(a[i]->stype) == CHAR) hasch = 1; stk = r0l = r0h = a0 = r2 = 0; for (i = 0; i < n; i++) { s = a[i]; sz = tsize(s->stype, s->sdf, s->ssue); if (ISPTR(s->stype) && ISFTN(DECREF(s->stype))) sz = SZLONG; /* function pointers are always 32 */ if (stk == 0) switch (sz) { case SZCHAR: if (r0l) { if (r0h) break; argmove(s, 1); r0h = 1; } else { argmove(s, 0); r0l = 1; } continue; case SZINT: if (s->stype > BTMASK) { /* is a pointer */ if (a0) { if (r0l || hasch) { if (r2) break; argmove(s, R2); r2 = 1; } else { argmove(s, R0); r0l = r0h = 1; } } else { argmove(s, A0); a0 = 1; } } else if (r0l || hasch) { if (r2) { if (a0) break; argmove(s, A0); a0 = 1; } else { argmove(s, R2); r2 = 1; } } else { argmove(s, R0); r0l = r0h = 1; } continue; case SZLONG: if (r0l||r0h||r2) break; argmove(s, R0); r0l = r0h = r2 = 1; continue; default: break; } stk = 1; s->soffset = argoff; argoff += sz; } }
/* * local optimizations, most of which are probably * machine independent */ NODE * optim(NODE *p) { int o, ty; NODE *sp, *q; OFFSZ sz; int i; if (odebug) return(p); ty = coptype(p->n_op); if( ty == LTYPE ) return(p); if( ty == BITYPE ) p->n_right = optim(p->n_right); p->n_left = optim(p->n_left); /* collect constants */ again: o = p->n_op; switch(o){ case SCONV: if (concast(p->n_left, p->n_type)) { q = p->n_left; nfree(p); p = q; break; } /* FALLTHROUGH */ case PCONV: if (p->n_type != VOID) p = clocal(p); break; case FORTCALL: p->n_right = fortarg( p->n_right ); break; case ADDROF: if (LO(p) == TEMP) break; if( LO(p) != NAME ) cerror( "& error" ); if( !andable(p->n_left) && !statinit) break; LO(p) = ICON; setuleft: /* paint over the type of the left hand side with the type of the top */ p->n_left->n_type = p->n_type; p->n_left->n_df = p->n_df; p->n_left->n_ap = p->n_ap; q = p->n_left; nfree(p); p = q; break; case NOT: case UMINUS: case COMPL: if (LCON(p) && conval(p->n_left, o, p->n_left)) p = nfree(p); break; case UMUL: /* Do not discard ADDROF TEMP's */ if (LO(p) == ADDROF && LO(p->n_left) != TEMP) { q = p->n_left->n_left; nfree(p->n_left); nfree(p); p = q; break; } if( LO(p) != ICON ) break; LO(p) = NAME; goto setuleft; case RS: if (LCON(p) && RCON(p) && conval(p->n_left, o, p->n_right)) goto zapright; sz = tsize(p->n_type, p->n_df, p->n_ap); if (LO(p) == RS && RCON(p->n_left) && RCON(p) && (RV(p) + RV(p->n_left)) < sz) { /* two right-shift by constants */ RV(p) += RV(p->n_left); p->n_left = zapleft(p->n_left); } #if 0 else if (LO(p) == LS && RCON(p->n_left) && RCON(p)) { RV(p) -= RV(p->n_left); if (RV(p) < 0) o = p->n_op = LS, RV(p) = -RV(p); p->n_left = zapleft(p->n_left); } #endif if (RO(p) == ICON) { if (RV(p) < 0) { RV(p) = -RV(p); p->n_op = LS; goto again; } #ifdef notyet /* must check for side effects, --a >> 32; */ if (RV(p) >= tsize(p->n_type, p->n_df, p->n_sue) && ISUNSIGNED(p->n_type)) { /* ignore signed shifts */ /* too many shifts */ tfree(p->n_left); nfree(p->n_right); p->n_op = ICON; p->n_lval = 0; p->n_sp = NULL; } else #endif /* avoid larger shifts than type size */ if (RV(p) >= sz) { RV(p) = RV(p) % sz; werror("shift larger than type"); } if (RV(p) == 0) p = zapleft(p); } break; case LS: if (LCON(p) && RCON(p) && conval(p->n_left, o, p->n_right)) goto zapright; sz = tsize(p->n_type, p->n_df, p->n_ap); if (LO(p) == LS && RCON(p->n_left) && RCON(p)) { /* two left-shift by constants */ RV(p) += RV(p->n_left); p->n_left = zapleft(p->n_left); } #if 0 else if (LO(p) == RS && RCON(p->n_left) && RCON(p)) { RV(p) -= RV(p->n_left); p->n_left = zapleft(p->n_left); } #endif if (RO(p) == ICON) { if (RV(p) < 0) { RV(p) = -RV(p); p->n_op = RS; goto again; } #ifdef notyet /* must check for side effects */ if (RV(p) >= tsize(p->n_type, p->n_df, p->n_sue)) { /* too many shifts */ tfree(p->n_left); nfree(p->n_right); p->n_op = ICON; p->n_lval = 0; p->n_sp = NULL; } else #endif /* avoid larger shifts than type size */ if (RV(p) >= sz) { RV(p) = RV(p) % sz; werror("shift larger than type"); } if (RV(p) == 0) p = zapleft(p); } break; case QUEST: if (LCON(p) == 0) break; if (LV(p) == 0) { q = p->n_right->n_right; } else { q = p->n_right->n_left; p->n_right->n_left = p->n_right->n_right; } p->n_right->n_op = UMUL; /* for tfree() */ tfree(p); p = q; break; case MINUS: if (LCON(p) && RCON(p) && p->n_left->n_sp == p->n_right->n_sp) { /* link-time constants, but both are the same */ /* solve it now by forgetting the symbols */ p->n_left->n_sp = p->n_right->n_sp = NULL; } if( !nncon(p->n_right) ) break; RV(p) = -RV(p); o = p->n_op = PLUS; case MUL: /* * Check for u=(x-y)+z; where all vars are pointers to * the same struct. This has two advantages: * 1: avoid a mul+div * 2: even if not allowed, people may get surprised if this * calculation do not give correct result if using * unaligned structs. */ if (p->n_type == INTPTR && RCON(p) && LO(p) == DIV && RCON(p->n_left) && RV(p) == RV(p->n_left) && LO(p->n_left) == MINUS) { q = p->n_left->n_left; if (q->n_left->n_type == PTR+STRTY && q->n_right->n_type == PTR+STRTY && strmemb(q->n_left->n_ap) == strmemb(q->n_right->n_ap)) { p = zapleft(p); p = zapleft(p); } } /* FALLTHROUGH */ case PLUS: case AND: case OR: case ER: /* commutative ops; for now, just collect constants */ /* someday, do it right */ if( nncon(p->n_left) || ( LCON(p) && !RCON(p) ) ) SWAP( p->n_left, p->n_right ); /* make ops tower to the left, not the right */ if( RO(p) == o ){ NODE *t1, *t2, *t3; t1 = p->n_left; sp = p->n_right; t2 = sp->n_left; t3 = sp->n_right; /* now, put together again */ p->n_left = sp; sp->n_left = t1; sp->n_right = t2; sp->n_type = p->n_type; p->n_right = t3; } if(o == PLUS && LO(p) == MINUS && RCON(p) && RCON(p->n_left) && conval(p->n_right, MINUS, p->n_left->n_right)){ zapleft: q = p->n_left->n_left; nfree(p->n_left->n_right); nfree(p->n_left); p->n_left = q; } if( RCON(p) && LO(p)==o && RCON(p->n_left) && conval( p->n_right, o, p->n_left->n_right ) ){ goto zapleft; } else if( LCON(p) && RCON(p) && conval( p->n_left, o, p->n_right ) ){ zapright: nfree(p->n_right); q = makety(p->n_left, p->n_type, p->n_qual, p->n_df, p->n_ap); nfree(p); p = clocal(q); break; } /* change muls to shifts */ if( o == MUL && nncon(p->n_right) && (i=ispow2(RV(p)))>=0){ if( i == 0 ) { /* multiplication by 1 */ goto zapright; } o = p->n_op = LS; p->n_right->n_type = INT; p->n_right->n_df = NULL; RV(p) = i; } /* change +'s of negative consts back to - */ if( o==PLUS && nncon(p->n_right) && RV(p)<0 ){ RV(p) = -RV(p); o = p->n_op = MINUS; } /* remove ops with RHS 0 */ if ((o == PLUS || o == MINUS || o == OR || o == ER) && nncon(p->n_right) && RV(p) == 0) { goto zapright; } break; case DIV: if( nncon( p->n_right ) && p->n_right->n_lval == 1 ) goto zapright; if (LCON(p) && RCON(p) && conval(p->n_left, DIV, p->n_right)) goto zapright; if (RCON(p) && ISUNSIGNED(p->n_type) && (i=ispow2(RV(p))) > 0) { p->n_op = RS; RV(p) = i; q = p->n_right; if(tsize(q->n_type, q->n_df, q->n_ap) > SZINT) p->n_right = makety(q, INT, 0, 0, 0); break; } break; case MOD: if (RCON(p) && ISUNSIGNED(p->n_type) && ispow2(RV(p)) > 0) { p->n_op = AND; RV(p) = RV(p) -1; break; } break; case EQ: case NE: case LT: case LE: case GT: case GE: case ULT: case ULE: case UGT: case UGE: if (LCON(p) && RCON(p) && !ISPTR(p->n_left->n_type) && !ISPTR(p->n_right->n_type)) { /* Do constant evaluation */ q = p->n_left; if (conval(q, o, p->n_right)) { nfree(p->n_right); nfree(p); p = q; break; } } if( !LCON(p) ) break; /* exchange operands */ sp = p->n_left; p->n_left = p->n_right; p->n_right = sp; p->n_op = revrel[p->n_op - EQ ]; break; #ifdef notyet case ASSIGN: /* Simple test to avoid two branches */ if (RO(p) != NE) break; q = p->n_right; if (RCON(q) && RV(q) == 0 && LO(q) == AND && RCON(q->n_left) && (i = ispow2(RV(q->n_left))) && q->n_left->n_type == INT) { q->n_op = RS; RV(q) = i; } break; #endif } return(p); }