Esempio n. 1
0
File: ph.c Progetto: Rayerd/dmd
void *ph_malloc(size_t nbytes)
{   unsigned char *p;

#ifdef DEBUG
    util_progress();
#endif
    nbytes += sizeof(unsigned) * 2;
    nbytes &= ~(sizeof(unsigned) - 1);

    if (nbytes >= heap->nleft)
        ph_newheap(nbytes);
    p = heap->p;
    heap->p += nbytes;
    heap->nleft -= nbytes;
    *(unsigned *)p = nbytes - sizeof(unsigned);
    p += sizeof(unsigned);
    return p;
}
Esempio n. 2
0
void optfunc()
{
#if !HTOD
    block *b;
    int iter;           // iteration count
    clock_t starttime;

    cmes ("optfunc()\n");
    dbg_optprint("optfunc\n");
#ifdef DEBUG
    if (debugb)
    {
        dbg_printf("................Before optimization.........\n");
        WRfunc();
    }
#endif
    iter = 0;

    if (localgot)
    {   // Initialize with:
        //      localgot = OPgot;
        elem *e = el_long(TYnptr, 0);
        e->Eoper = OPgot;
        e = el_bin(OPeq, TYnptr, el_var(localgot), e);
        startblock->Belem = el_combine(e, startblock->Belem);
    }

    // Each pass through the loop can reduce only one level of comma expression.
    // The infinite loop check needs to take this into account.
    int iterationLimit = 0;
    for (b = startblock; b; b = b->Bnext)
    {
        if (!b->Belem)
            continue;
        int d = el_countCommas(b->Belem);
        if (d > iterationLimit)
            iterationLimit = d;
    }

    // Some functions can take enormous amounts of time to optimize.
    // We try to put a lid on it.
    starttime = clock();
    do
    {
        //printf("iter = %d\n", iter);
#if TX86
        if (++iter > 200)
        {   assert(iter < iterationLimit);      // infinite loop check
            break;
        }
#else
     L1:
#endif
#if MARS
        util_progress();
#else
        file_progress();
#endif

        //printf("optelem\n");
        /* canonicalize the trees        */
        for (b = startblock; b; b = b->Bnext)
            if (b->Belem)
            {
#if DEBUG
                if(debuge)
                {
                    dbg_printf("before\n");
                    elem_print(b->Belem);
                    //el_check(b->Belem);
                }
#endif
                b->Belem = doptelem(b->Belem,bc_goal[b->BC] | GOALagain);
#if DEBUG
                if(0 && debugf)
                {
                    dbg_printf("after\n");
                    elem_print(b->Belem);
                }
#endif
            }
        //printf("blockopt\n");
#if TX86
        if (mfoptim & MFdc)
            blockopt(0);                // do block optimization
        out_regcand(&globsym);          // recompute register candidates
        changes = 0;                    /* no changes yet                */
        if (mfoptim & MFcnp)
            constprop();                /* make relationals unsigned     */
        if (mfoptim & (MFli | MFliv))
#else
        if (config.optimized && (mfoptim & MFdc))
            blockopt(0);                // do block optimization

        dbg_optprint("blockopt\n");
        out_regcand();                  // recompute register candidates
        changes = 0;                    /* no changes yet                */

        dbg_optprint("constprop\n");
        if (config.optimized && (mfoptim & MFcnp))
            constprop();                /* make relationals unsigned     */

        dbg_optprint("loopopt\n");
        if (config.optimized && (mfoptim & (MFli | MFliv)))
#endif
            loopopt();                  /* remove loop invariants and    */
                                        /* induction vars                */
                                        /* do loop rotation              */
        else
            for (b = startblock; b; b = b->Bnext)
                b->Bweight = 1;
        dbg_optprint("boolopt\n");
#if TX86
        if (mfoptim & MFcnp)
            boolopt();                  // optimize boolean values
        if (changes && mfoptim & MFloop && (clock() - starttime) < 30 * CLOCKS_PER_SEC)
            continue;

        if (mfoptim & MFcnp)
            constprop();                /* constant propagation          */
        if (mfoptim & MFcp)
            copyprop();                 /* do copy propagation           */

        /* Floating point constants and string literals need to be
         * replaced with loads from variables in read-only data.
         * This can result in localgot getting needed.
         */
        symbol *localgotsave = localgot;
        for (b = startblock; b; b = b->Bnext)
        {
            if (b->Belem)
            {
                b->Belem = doptelem(b->Belem,bc_goal[b->BC] | GOALstruct);
                if (b->Belem)
                    b->Belem = el_convert(b->Belem);
            }
        }
        if (localgot != localgotsave)
        {   /* Looks like we did need localgot, initialize with:
             *  localgot = OPgot;
             */
            elem *e = el_long(TYnptr, 0);
            e->Eoper = OPgot;
            e = el_bin(OPeq, TYnptr, el_var(localgot), e);
            startblock->Belem = el_combine(e, startblock->Belem);
        }

        /* localize() is after localgot, otherwise we wind up with
         * more than one OPgot in a function, which mucks up OSX
         * code generation which assumes at most one (localgotoffset).
         */
        if (mfoptim & MFlocal)
            localize();                 // improve expression locality
        if (mfoptim & MFda)
            rmdeadass();                /* remove dead assignments       */

        cmes2 ("changes = %d\n", changes);
        if (!(changes && mfoptim & MFloop && (clock() - starttime) < 30 * CLOCKS_PER_SEC))
            break;
    } while (1);
    cmes2("%d iterations\n",iter);
    if (mfoptim & MFdc)
        blockopt(1);                    // do block optimization
#else
        if (config.optimized && (mfoptim & MFcnp))
            boolopt();                  // optimize boolean values
        util_progress();
        if (changes)
            goto L1;

        dbg_optprint("constprop\n");
        if (config.optimized && (mfoptim & MFcnp))
            constprop();                /* constant propagation          */

        dbg_optprint("copyprop\n");
        if (config.optimized && (mfoptim & MFcp))
            copyprop();                 /* do copy propagation           */

        dbg_optprint("localize\n");
        if (config.optimized && (mfoptim & MFlocal))
            localize();                 // improve expression locality

        dbg_optprint("rmdeadass\n");
        if (config.optimized && (mfoptim & MFda))
            rmdeadass();                /* remove dead assignments       */
        cmes2 ("changes = %d\n", changes);
        iter++;
        assert (iter < 80);             /* infinite loop check           */
    } while (changes && (config.optimized) && (mfoptim & MFloop));