regAlloc.cpp

Go to the documentation of this file.
00001 /* Copyright 1994, LongView Technologies L.L.C. $Revision: 1.20 $ */
00002 /* Copyright (c) 2006, Sun Microsystems, Inc.
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 
00006 following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 
00010           disclaimer in the documentation and/or other materials provided with the distribution.
00011     * Neither the name of Sun Microsystems nor the names of its contributors may be used to endorse or promote products derived 
00012           from this software without specific prior written permission.
00013 
00014 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 
00015 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
00016 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
00017 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00018 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
00019 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
00020 
00021 
00022 */
00023 
00024 # include "incls/_precompiled.incl"
00025 
00026 # ifdef DELTA_COMPILER
00027 
00028 # include "incls/_regAlloc.cpp.incl"
00029 
00030 RegisterAllocator* theAllocator;
00031 
00032 
00033 static int compare_pregBegs(PReg** a, PReg** b) {
00034   return (*a)->begBCI() - (*b)->begBCI();
00035 }
00036 
00037 
00038 static int compare_pregEnds(PReg** a, PReg** b) {
00039   return (*a)->endBCI() - (*b)->endBCI();
00040 }
00041 
00042 
00043 RegisterAllocator::RegisterAllocator() {
00044   theAllocator = this; 
00045   _stackLocs = new IntFreeList(2);
00046 }
00047 
00048 
00049 void RegisterAllocator::preAllocate(PReg* r) {
00050   r->allocateTo(Mapping::localTemporary(_stackLocs->allocate()));
00051 }
00052 
00053 
00054 void RegisterAllocator::allocate(GrowableArray<PReg*>* globals) {
00055   GrowableArray<PReg*>* regs = new GrowableArray<PReg*>(globals->length());
00056   int i = globals->length();
00057   while (i-- > 0) {
00058     PReg* r = globals->at(i);
00059     assert(r->ndefs() + r->nuses() > 0 || r->incorrectDU(), "PReg is unused");
00060     if (r->loc != unAllocated) {
00061       // already allocated
00062     } else if (r->isConstPReg()) {
00063       // don't allocate constants for now
00064     } else {
00065       // collect for later allocation
00066       regs->append(r);
00067     }
00068   }
00069 
00070   int len = regs->length();
00071   if (len > 0) {
00072     // sort begBCIs & distribute to scopes
00073     regs->sort(&compare_pregBegs);
00074     assert(regs->isEmpty() || regs->first()->begBCI() <= regs->last()->begBCI(), "wrong sort order");
00075     for (i = 0; i < len; i++) {
00076       PReg* r = regs->at(i);
00077       assert(r->begBCI() != IllegalBCI, "illegal begBCI");
00078       assert(r->endBCI() != IllegalBCI, "illegal endBCI");
00079       r->scope()->addToPRegsBegSorted(r);
00080     }
00081 
00082     // sort endBCIs & distribute to scopes
00083     regs->sort(&compare_pregEnds);
00084     assert(regs->isEmpty() || regs->first()->endBCI() <= regs->last()->endBCI(), "wrong sort order");
00085     for (i = 0; i < len; i++) {
00086       PReg* r = regs->at(i);
00087       r->scope()->addToPRegsEndSorted(r);
00088     }
00089     // do register allocation
00090     theCompiler->topScope->allocatePRegs(_stackLocs);
00091 
00092     // result
00093     if (CompilerDebug) cout(PrintRegAlloc)->print("%d (-2) stack locations allocated for %d PRegs\n", _stackLocs->length(), regs->length());
00094   }
00095 }
00096 
00097 
00098 # endif // DELTA_COMPILER

Generated on Mon Oct 9 13:37:25 2006 for Strongtalk VM by  doxygen 1.4.7