Quick Search:

Mode

Context

Displaying 3 lines of context. None | Less | More | Full

Other Diffs

Ignore

Blank Lines Whitespace: Expand:

Diff

1.33
 
1.34
 
MAIN:ragge:20050917075840
 
match.c
_>1 -/*      $Id$    */
  1+/*      $Id$   */
22 /*
<> 3+ * Copyright (c) 2003 Anders Magnusson (ragge@ludd.luth.se).
  4+ * All rights reserved.
  5+ *
  6+ * Redistribution and use in source and binary forms, with or without
  7+ * modification, are permitted provided that the following conditions
  8+ * are met:
  9+ * 1. Redistributions of source code must retain the above copyright
  10+ *    notice, this list of conditions and the following disclaimer.
  11+ * 2. Redistributions in binary form must reproduce the above copyright
  12+ *    notice, this list of conditions and the following disclaimer in the
  13+ *    documentation and/or other materials provided with the distribution.
  14+ * 3. The name of the author may not be used to endorse or promote products
  15+ *    derived from this software without specific prior written permission
  16+ *
  17+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27+ */
  28+
  29+/*
330  * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
 431  *
 532  * Redistribution and use in source and binary forms, with or without
     
 !
4269 
 4370 int s2debug = 0;
 4471 
<> 72+extern char *ltyp[], *rtyp[];
  73+
4574 /*
 4675  * return true if shape is appropriate for the node p
 4776  * side effect for SFLD is to set up fldsz, etc
     
 !
139168                 break;
 140169 
 141170         case TEMP: /* temporaries are handled as registers */
<>142 -                /* XXX - register classes? */
 143 -                mask = SAREG|STAREG;
  171+#if 0
  172+                mask = PCLASS(p);
144173                 if (shape & mask)
 145174                         return SRREG; /* let register allocator coalesce */
<> 175+#endif
146176                 break;
 147177 
 148178         case REG:
<>149 -                /* distinctions:
 150 -                 * SAREG        any scalar register
 151 -                 * STAREG       any temporary scalar register
 152 -                 * SBREG        any lvalue (index) register
 153 -                 * STBREG       any temporary lvalue register
 154 -                */
 155 -                mask = isbreg(p->n_rval) ? SBREG : SAREG;
 156 -                if (istreg(p->n_rval))
 157 -                        mask |= mask==SAREG ? STAREG : STBREG;
  179+                mask = PCLASS(p);
158180                 if (shape & mask)
 159181                         return SRDIR;
 160182                 break;
     
 !
170192                 break;
 171193 
 172194         }
<>173 -        if (shape & (SAREG|STAREG|SBREG|STBREG))
  195+#ifdef SNH_REG
  196+        if (shape & PCLASS(p))
174197                 return SRREG;   /* Can put in register */
<> 198+#else
  199+        if (shape & (SAREG|SBREG))
  200+                return SRREG;   /* Can put in register */
  201+#endif
175202 
 176203         return SRNOPE;
 177204 }
     
 !
374401                         printf("%s", tarr[i]);
 375402                 }
 376403 }
<_ 404+
  405+
  406+static int shltab[] = { 0, 0, LOREG, LREG };
  407+static int shrtab[] = { 0, 0, ROREG, RREG };
  408+
  409+/*
  410+ * Find the best ops for a given tree.
  411+ * Different instruction sequences are graded as:
  412+        add2 reg,reg     = 0
  413+        add2 mem,reg     = 1
  414+        add3 mem,reg,reg = 2
  415+        add3 reg,mem,reg = 2
  416+        add3 mem,mem,reg = 3
  417+        move mem,reg ; add2 mem,reg     = 4
  418+        move mem,reg ; add3 mem,reg,reg = 5
  419+        move mem,reg ; move mem,reg ; add2 reg,reg = 6
  420+        move mem,reg ; move mem,reg ; add3 reg,reg,reg = 7
  421+ * The instruction with the lowest grading is emitted.
  422+ */
  423+int
  424+findops(NODE *p, int cookie)
  425+{
  426+        extern int *qtable[];
  427+        struct optab *q;
  428+        int i, shl, shr, tl, tr, is3;
  429+        NODE *l, *r;
  430+        int *ixp;
  431+        int rv = -1, mtchno = 10;
  432+
  433+if (f2debug) printf("findops tree:\n");
  434+if (f2debug) fwalk(p, e2print, 0);
  435+
  436+        ixp = qtable[p->n_op];
  437+        for (i = 0; ixp[i] >= 0; i++) {
  438+                q = &table[ixp[i]];
  439+
  440+if (f2debug) printf("findop: ixp %d\n", ixp[i]);
  441+                l = getlr(p, 'L');
  442+                r = getlr(p, 'R');
  443+                if (ttype(l->n_type, q->ltype) == 0 ||
  444+                    ttype(r->n_type, q->rtype) == 0)
  445+                        continue; /* Types must be correct */
  446+
  447+if (f2debug) printf("findop got types\n");
  448+                if ((shl = tshape(l, q->lshape)) == SRNOPE)
  449+                        continue; /* useless */
  450+if (f2debug) printf("findop lshape %d\n", shl);
  451+if (f2debug) fwalk(l, e2print, 0);
  452+                if ((shr = tshape(r, q->rshape)) == SRNOPE)
  453+                        continue; /* useless */
  454+if (f2debug) printf("findop rshape %d\n", shr);
  455+if (f2debug) fwalk(r, e2print, 0);
  456+                if (q->needs & REWRITE)
  457+                        break;  /* Done here */
  458+
  459+                tl = tr = 1/* XXX remove later */
  460+                is3 = ((q->rewrite & (RLEFT|RRIGHT)) == 0);
  461+
  462+                if (shl == SRDIR && shr== SRDIR ) {
  463+                        int got = 10;
  464+                        /*
  465+                         * Both shapes matches direct. If one of them is
  466+                         * in a temp register and there is a corresponding
  467+                         * 2-op instruction, be very happy. If not, but
  468+                         * there is a 3-op instruction that ends in a reg,
  469+                         * be quite happy. If neither, cannot do anything.
  470+                         */
  471+                        if (tl && (q->rewrite & RLEFT)) {
  472+                                got = 1;
  473+                        } else if (tr && (q->rewrite & RRIGHT)) {
  474+                                got = 1;
  475+                        } else if ((q->rewrite & (RLEFT|RRIGHT)) == 0) {
  476+                                got = 3;
  477+                        }
  478+                        if (got < mtchno) {
  479+                                mtchno = got;
  480+                                rv = MKIDX(ixp[i], 0);
  481+                        }
  482+                        if (got != 10)
  483+                                continue;
  484+                }
  485+if (f2debug) printf("second\n");
  486+                if (shr == SRDIR) {
  487+                        /*
  488+                         * Right shape matched. If left node can be put into
  489+                         * a temporary register, and the current op matches,
  490+                         * be happy.
  491+                         */
  492+                        if (q->rewrite & RLEFT) {
  493+                                if (4 < mtchno) {
  494+                                        mtchno = 4;
  495+                                        rv = MKIDX(ixp[i], LREG);
  496+                                }
  497+                                continue; /* Can't do anything else */
  498+                        } else if (is3) {
  499+                                if (5 < mtchno) {
  500+                                        mtchno = 5;
  501+                                        rv = MKIDX(ixp[i], shltab[shl]);
  502+                                }
  503+                                continue; /* Can't do anything else */
  504+                        }
  505+                }
  506+if (f2debug) printf("third\n");
  507+                if (shl == SRDIR) {
  508+                        /*
  509+                         * Left shape matched. If right node can be put into
  510+                         * a temporary register, and the current op matches,
  511+                         * be happy.
  512+                         */
  513+                        if (q->rewrite & RRIGHT) {
  514+                                if (4 < mtchno) {
  515+                                        mtchno = 4;
  516+                                        rv = MKIDX(ixp[i], RREG);
  517+                                }
  518+                                continue; /* Can't do anything */
  519+                        } else if (is3) {
  520+                                if (5 < mtchno) {
  521+                                        mtchno = 5;
  522+                                        rv = MKIDX(ixp[i], shrtab[shr]);
  523+                                }
  524+                                continue; /* Can't do anything */
  525+                        }
  526+                }
  527+                /*
  528+                 * Neither of the shapes matched. Put both args in
  529+                 * regs and be done with it.
  530+                 */
  531+                if (is3) {
  532+                        if (7 < mtchno) {
  533+                                mtchno = 7;
  534+                                rv = MKIDX(ixp[i], shltab[shl]|shrtab[shr]);
  535+                        }
  536+                } else {
  537+                        if (6 < mtchno) {
  538+                                mtchno = 6;
  539+                                if (q->rewrite & RLEFT)
  540+                                        rv = MKIDX(ixp[i], shrtab[shr]|LREG);
  541+                                else
  542+                                        rv = MKIDX(ixp[i], shltab[shl]|RREG);
  543+                        }
  544+                }
  545+        }
  546+#ifdef PCC_DEBUG
  547+        if (f2debug) {
  548+                if (rv == -1)
  549+                        printf("findops failed\n");
  550+                else
  551+                        printf("findops entry %d(%s %s)\n",
  552+                            TBLIDX(rv), ltyp[rv & LMASK], rtyp[(rv&RMASK)>>2]);
  553+        }
  554+#endif
  555+        return rv;
  556+}
  557+
  558+/*
  559+ * Find the best relation op for matching the two trees it has.
  560+ * This is a sub-version of the function findops() above.
  561+ * The instruction with the lowest grading is emitted.
  562+ *
  563+ * Level assignment for priority:
  564+ *      left    right   prio
  565+ *      -       -       -
  566+ *      direct  direct  1
  567+ *      direct  OREG    2       # make oreg
  568+ *      OREG    direct  2       # make oreg
  569+ *      OREG    OREG    2       # make both oreg
  570+ *      direct  REG     3       # put in reg
  571+ *      OREG    REG     3       # put in reg, make oreg
  572+ *      REG     direct  3       # put in reg
  573+ *      REG     OREG    3       # put in reg, make oreg
  574+ *      REG     REG     4       # put both in reg
  575+ */
  576+int
  577+relops(NODE *p)
  578+{
  579+        extern int *qtable[];
  580+        struct optab *q;
  581+        int i, shl, shr;
  582+        NODE *l, *r;
  583+        int *ixp;
  584+        int rv = -1, mtchno = 10;
  585+
  586+if (f2debug) printf("relops tree:\n");
  587+if (f2debug) fwalk(p, e2print, 0);
  588+
  589+        ixp = qtable[p->n_op];
  590+        for (i = 0; ixp[i] >= 0; i++) {
  591+                q = &table[ixp[i]];
  592+
  593+if (f2debug) printf("relops: ixp %d\n", ixp[i]);
  594+                l = getlr(p, 'L');
  595+                r = getlr(p, 'R');
  596+                if (ttype(l->n_type, q->ltype) == 0 ||
  597+                    ttype(r->n_type, q->rtype) == 0)
  598+                        continue; /* Types must be correct */
  599+
  600+if (f2debug) printf("relops got types\n");
  601+                shl = tshape(l, q->lshape);
  602+                if (shl == 0)
  603+                        continue; /* useless */
  604+if (f2debug) printf("relops lshape %d\n", shl);
  605+if (f2debug) fwalk(l, e2print, 0);
  606+                shr = tshape(r, q->rshape);
  607+                if (shr == 0)
  608+                        continue; /* useless */
  609+if (f2debug) printf("relops rshape %d\n", shr);
  610+if (f2debug) fwalk(r, e2print, 0);
  611+                if (q->needs & REWRITE)
  612+                        break;  /* Done here */
  613+
  614+                if (shl+shr < mtchno) {
  615+                        mtchno = shl+shr;
  616+                        rv = MKIDX(ixp[i], shltab[shl]|shrtab[shr]);
  617+                }
  618+        }
  619+#ifdef PCC_DEBUG
  620+        if (f2debug) {
  621+                if (rv == -1)
  622+                        printf("relops failed\n");
  623+                else
  624+                        printf("relops entry %d(%s %s)\n",
  625+                            TBLIDX(rv), ltyp[rv & LMASK], rtyp[(rv&RMASK)>>2]);
  626+        }
  627+#endif
  628+        return rv;
  629+}
  630+
  631+/*
  632+ * Find a matching assign op.
  633+ *
  634+ * Level assignment for priority:
  635+ *      left    right   prio
  636+ *      -       -       -
  637+ *      direct  direct  1
  638+ *      direct  REG     2
  639+ *      direct  OREG    3
  640+ *      OREG    direct  4
  641+ *      OREG    REG     5
  642+ *      OREG    OREG    6
  643+ */
  644+int
  645+findasg(NODE *p, int cookie)
  646+{
  647+        extern int *qtable[];
  648+        struct optab *q;
  649+        int i, shl, shr, lvl = 10;
  650+        NODE *l, *r;
  651+        int *ixp;
  652+        int rv = -1;
  653+
  654+#ifdef PCC_DEBUG
  655+        if (f2debug) {
  656+                printf("findasg tree: %s\n", prcook(cookie));
  657+                fwalk(p, e2print, 0);
  658+        }
  659+#endif
  660+
  661+        ixp = qtable[p->n_op];
  662+        for (i = 0; ixp[i] >= 0; i++) {
  663+                q = &table[ixp[i]];
  664+
  665+if (f2debug) printf("asgop: ixp %d\n", ixp[i]);
  666+                l = getlr(p, 'L');
  667+                r = getlr(p, 'R');
  668+                if (ttype(l->n_type, q->ltype) == 0 ||
  669+                    ttype(r->n_type, q->rtype) == 0)
  670+                        continue; /* Types must be correct */
  671+
  672+                if ((cookie & (INTAREG|INTBREG)) &&
  673+                    (q->rewrite & (RLEFT|RRIGHT)) == 0)
  674+                        continue; /* must get a result somehere */
  675+
  676+if (f2debug) printf("asgop got types\n");
  677+                if ((shl = tshape(l, q->lshape)) == SRNOPE)
  678+                        continue;
  679+
  680+                if (p->n_left->n_op == TEMP)
  681+                        shl = SRDIR;
  682+                else if (shl == SRREG)
  683+                        continue;
  684+
  685+if (f2debug) printf("asgop lshape %d\n", shl);
  686+if (f2debug) fwalk(l, e2print, 0);
  687+
  688+                if ((shr = tshape(r, q->rshape)) == SRNOPE)
  689+                        continue; /* useless */
  690+
  691+if (f2debug) printf("asgop rshape %d\n", shr);
  692+if (f2debug) fwalk(r, e2print, 0);
  693+                if (q->needs & REWRITE)
  694+                        break;  /* Done here */
  695+
  696+                if (lvl <= (shl + shr))
  697+                        continue;
  698+                lvl = shl + shr;
  699+                
  700+                rv = MKIDX(ixp[i], shltab[shl]|shrtab[shr]);
  701+        }
  702+#ifdef PCC_DEBUG
  703+        if (f2debug) {
  704+                if (rv == -1)
  705+                        printf("findasg failed\n");
  706+                else
  707+                        printf("findasg entry %d(%s %s)\n",
  708+                            TBLIDX(rv), ltyp[rv & LMASK], rtyp[(rv&RMASK)>>2]);
  709+        }
  710+#endif
  711+        return rv;
  712+}
  713+
  714+/*
  715+ * Find an ASSIGN node that puts the value into a register.
  716+ */
  717+int
  718+findleaf(NODE *p, int cookie)
  719+{
  720+        extern int *qtable[];
  721+        struct optab *q;
  722+        int i, shl;
  723+        int *ixp;
  724+        int rv = -1;
  725+
  726+#ifdef PCC_DEBUG
  727+        if (f2debug) {
  728+                printf("findleaf tree: %s\n", prcook(cookie));
  729+                fwalk(p, e2print, 0);
  730+        }
  731+#endif
  732+
  733+        ixp = qtable[p->n_op];
  734+        for (i = 0; ixp[i] >= 0; i++) {
  735+                q = &table[ixp[i]];
  736+
  737+if (f2debug) printf("findleaf: ixp %d\n", ixp[i]);
  738+                if (ttype(p->n_type, q->rtype) == 0)
  739+                        continue; /* Type must be correct */
  740+
  741+if (f2debug) printf("findleaf got types\n");
  742+                if ((shl = tshape(p, q->rshape)) != SRDIR && p->n_op != TEMP)
  743+                        continue; /* shape must match */
  744+
  745+                if ((q->visit & cookie) == 0)
  746+                        continue; /* wrong registers */
  747+
  748+if (f2debug) printf("findleaf got shapes %d\n", shl);
  749+
  750+                if (q->needs & REWRITE)
  751+                        break;  /* Done here */
  752+
  753+                rv = MKIDX(ixp[i], 0);
  754+                break;
  755+        }
  756+        if (f2debug) {
  757+                if (rv == -1)
  758+                        printf("findleaf failed\n");
  759+                else
  760+                        printf("findleaf entry %d(%s %s)\n",
  761+                            TBLIDX(rv), ltyp[rv & LMASK], rtyp[(rv&RMASK)>>2]);
  762+        }
  763+        return rv;
  764+}
  765+
  766+/*
  767+ * Find a UNARY op that satisfy the needs.
  768+ * For now, the destination is always a register.
  769+ * Both source and dest types must match, but only source (left)
  770+ * shape is of interest.
  771+ */
  772+int
  773+finduni(NODE *p, int cookie)
  774+{
  775+        extern int *qtable[];
  776+        struct optab *q;
  777+        NODE *l, *r;
  778+        int i, shl, num = 4;
  779+        int *ixp;
  780+        int rv = -1;
  781+
  782+#ifdef PCC_DEBUG
  783+        if (f2debug) {
  784+                printf("finduni tree: %s\n", prcook(cookie));
  785+                fwalk(p, e2print, 0);
  786+        }
  787+#endif
  788+
  789+        l = getlr(p, 'L');
  790+        r = getlr(p, 'R');
  791+        ixp = qtable[p->n_op];
  792+        for (i = 0; ixp[i] >= 0; i++) {
  793+                q = &table[ixp[i]];
  794+
  795+if (f2debug) printf("finduni: ixp %d\n", ixp[i]);
  796+                if (ttype(l->n_type, q->ltype) == 0)
  797+                        continue; /* Type must be correct */
  798+
  799+if (f2debug) printf("finduni got left type\n");
  800+                if (ttype(r->n_type, q->rtype) == 0)
  801+                        continue; /* Type must be correct */
  802+
  803+if (f2debug) printf("finduni got types\n");
  804+                if ((shl = tshape(l, q->lshape)) == SRNOPE)
  805+                        continue; /* shape must match */
  806+
  807+if (f2debug) printf("finduni got shapes %d\n", shl);
  808+
  809+                if ((cookie & q->visit) == 0)   /* check correct return value */
  810+                        continue;               /* XXX - should check needs */
  811+
  812+if (f2debug) printf("finduni got cookie\n");
  813+                if (q->needs & REWRITE)
  814+                        break;  /* Done here */
  815+
  816+                if (shl >= num)
  817+                        continue;
  818+                num = shl;
  819+                rv = MKIDX(ixp[i], shltab[shl]);
  820+                if ((q->lshape & (SBREG)) && !(q->lshape & (SAREG)))
  821+                        rv |= LBREG;
  822+                if (shl == SRDIR)
  823+                        break;
  824+        }
  825+#ifdef PCC_DEBUG
  826+        if (f2debug) {
  827+                if (rv == -1)
  828+                        printf("finduni failed\n");
  829+                else
  830+                        printf("finduni entry %d(%s %s)\n",
  831+                            TBLIDX(rv), ltyp[rv & LMASK], rtyp[(rv&RMASK)>>2]);
  832+        }
  833+#endif
  834+        return rv;
  835+}
FishEye: Open Source License registered to PCC.
Your maintenance has expired. You can renew your license at http://www.atlassian.com/fisheye/renew
Atlassian FishEye, CVS analysis. (Version:1.6.3 Build:build-336 2008-11-04) - Administration - Page generated 2014-09-23 06:22 +0200