00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00062 } else if (r->isConstPReg()) {
00063
00064 } else {
00065
00066 regs->append(r);
00067 }
00068 }
00069
00070 int len = regs->length();
00071 if (len > 0) {
00072
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
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
00090 theCompiler->topScope->allocatePRegs(_stackLocs);
00091
00092
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