llvm.org GIT mirror llvm / 51195af
Added method to get Mips register numbers Changed the stack frame layout, StackGrowsUp fits better to Mips strange stack. Stack offset calculation bug fixed! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@41529 91177308-0d34-0410-b5e6-96231b3b80d8 Bruno Cardoso Lopes 13 years ago
3 changed file(s) with 120 addition(s) and 68 deletion(s). Raw diff Collapse all Expand all
4040 : MipsGenRegisterInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP),
4141 TII(tii) {}
4242
43 /// getRegisterNumbering - Given the enum value for some register, e.g.
44 /// Mips::RA, return the number that it corresponds to (e.g. 31).
45 unsigned MipsRegisterInfo::
46 getRegisterNumbering(unsigned RegEnum)
47 {
48 switch (RegEnum) {
49 case Mips::ZERO : return 0;
50 case Mips::AT : return 1;
51 case Mips::V0 : return 2;
52 case Mips::V1 : return 3;
53 case Mips::A0 : return 4;
54 case Mips::A1 : return 5;
55 case Mips::A2 : return 6;
56 case Mips::A3 : return 7;
57 case Mips::T0 : return 8;
58 case Mips::T1 : return 9;
59 case Mips::T2 : return 10;
60 case Mips::T3 : return 11;
61 case Mips::T4 : return 12;
62 case Mips::T5 : return 13;
63 case Mips::T6 : return 14;
64 case Mips::T7 : return 15;
65 case Mips::T8 : return 16;
66 case Mips::T9 : return 17;
67 case Mips::S0 : return 18;
68 case Mips::S1 : return 19;
69 case Mips::S2 : return 20;
70 case Mips::S3 : return 21;
71 case Mips::S4 : return 22;
72 case Mips::S5 : return 23;
73 case Mips::S6 : return 24;
74 case Mips::S7 : return 25;
75 case Mips::K0 : return 26;
76 case Mips::K1 : return 27;
77 case Mips::GP : return 28;
78 case Mips::SP : return 29;
79 case Mips::FP : return 30;
80 case Mips::RA : return 31;
81 default: assert(0 && "Unknown register number!");
82 }
83 }
84
4385 void MipsRegisterInfo::
4486 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
4587 unsigned SrcReg, int FI,
113155 return NewMI;
114156 }
115157
158 //===----------------------------------------------------------------------===//
159 //
160 // Callee Saved Registers methods
161 //
162 //===----------------------------------------------------------------------===//
163
116164 /// Mips Callee Saved Registers
117165 const unsigned* MipsRegisterInfo::
118166 getCalleeSavedRegs(const MachineFunction *MF) const
158206 // Stack Frame Processing methods
159207 // +----------------------------+
160208 //
161 // Too meet ABI, we construct the frame on the reverse
162 // of natural order.
163 //
164 // The LLVM Frame will look like this:
165 //
166 // As the stack grows down, we start at 0, and the reference
167 // is decrement.
168 //
169 // 0 ----------
170 // -4 Args to pass
171 // . saved "Callee Saved" Registers
172 // . Local Area
173 // . saved FP
174 // . saved RA
175 // -StackSize -----------
176 //
177 // On the EliminateFrameIndex we just negate the address above
178 // and we get the stack frame required by the ABI, which is:
179 //
180 // sp + stacksize -------------
181 // saved $RA (only on non-leaf functions)
182 // saved $FP (only with frame pointer)
183 // saved "Callee Saved" Registers
184 // Local Area
185 // saved $GP (used in PIC - not supported yet)
186 // Args to pass area
187 // sp -------------
209 // The stack is allocated decrementing the stack pointer on
210 // the first instruction of a function prologue. Once decremented,
211 // all stack referencesare are done thought a positive offset
212 // from the stack/frame pointer, so the stack is considering
213 // to grow up! Otherwise terrible hacks would have to be made
214 // to get this stack ABI compliant :)
215 //
216 // The stack frame required by the ABI:
217 // Offset
218 //
219 // 0 ----------
220 // 4 Args to pass
221 // . saved $GP (used in PIC - not supported yet)
222 // . Local Area
223 // . saved "Callee Saved" Registers
224 // . saved FP
225 // . saved RA
226 // StackSize -----------
227 //
228 // Offset - offset from sp after stack allocation on function prologue
188229 //
189230 // The sp is the stack pointer subtracted/added from the stack size
190231 // at the Prologue/Epilogue
191232 //
192233 // References to the previous stack (to obtain arguments) are done
193 // with fixed location stack frames using positive stack offsets.
234 // with offsets that exceeds the stack size: (stacksize+(4*(num_arg-1))
194235 //
195236 // Examples:
196237 // - reference to the actual stack frame
197 // for any local area var there is smt like : FI >= 0, StackOffset: -4
198 // sw REGX, 4(REGY)
238 // for any local area var there is smt like : FI >= 0, StackOffset: 4
239 // sw REGX, 4(SP)
199240 //
200241 // - reference to previous stack frame
201 // suppose there's a store to the 5th arguments : FI < 0, StackOffset: 16.
242 // suppose there's a load to the 5th arguments : FI < 0, StackOffset: 16.
202243 // The emitted instruction will be something like:
203 // sw REGX, 16+StackSize (REGY)
244 // lw REGX, 16+StackSize(SP)
245 //
246 // Since the total stack size is unknown on LowerFORMAL_ARGUMENTS, all
247 // stack references (ObjectOffset) created to reference the function
248 // arguments, are negative numbers. This way, on eliminateFrameIndex it's
249 // possible to detect those references and the offsets are adjusted to
250 // their real location.
251 //
252 //
204253 //
205254 //===----------------------------------------------------------------------===//
206255
251300 DOUT << "stackSize : " << stackSize << "\n";
252301 #endif
253302
254 int Offset = ( (spOffset >= 0) ? (stackSize + spOffset) : (-spOffset));
303 // as explained on LowerFORMAL_ARGUMENTS, detect negative offsets
304 // and adjust SPOffsets considering the final stack size.
305 int Offset = ((spOffset < 0) ? (stackSize + (-(spOffset+4))) : (spOffset));
306 Offset += MI.getOperand(i-1).getImm();
255307
256308 #ifndef NDEBUG
257309 DOUT << "Offset : " << Offset << "\n";
270322 MipsFunctionInfo *MipsFI = MF.getInfo();
271323 MachineBasicBlock::iterator MBBI = MBB.begin();
272324
325 // Replace the dummy '0' SPOffset by the negative offsets, as
326 // explained on LowerFORMAL_ARGUMENTS
327 MipsFI->adjustLoadArgsFI(MFI);
328 MipsFI->adjustStoreVarArgsFI(MFI);
329
273330 // Get the number of bytes to allocate from the FrameInfo.
274331 int NumBytes = (int) MFI->getStackSize();
275332
276333 #ifndef NDEBUG
277334 DOUT << "\n<--- EMIT PROLOGUE --->\n";
278 DOUT << "Stack size :" << NumBytes << "\n";
335 DOUT << "Actual Stack size :" << NumBytes << "\n";
279336 #endif
280337
281 // Don't need to allocate space on the stack.
338 // No need to allocate space on the stack.
282339 if (NumBytes == 0) return;
283340
284341 int FPOffset, RAOffset;
285342
286 // Always allocate space for saved RA and FP,
287 // even if FramePointer is not used. When not
288 // using FP, the last stack slot becomes empty
289 // and RA is saved before it.
343 // Allocate space for saved RA and FP when needed
290344 if ((hasFP(MF)) && (MFI->hasCalls())) {
291 FPOffset = NumBytes+4;
292 RAOffset = (NumBytes+8);
345 FPOffset = NumBytes;
346 RAOffset = (NumBytes+4);
347 NumBytes += 8;
293348 } else if ((!hasFP(MF)) && (MFI->hasCalls())) {
294349 FPOffset = 0;
295 RAOffset = NumBytes+4;
350 RAOffset = NumBytes;
351 NumBytes += 4;
296352 } else if ((hasFP(MF)) && (!MFI->hasCalls())) {
297 FPOffset = NumBytes+4;
353 FPOffset = NumBytes;
298354 RAOffset = 0;
299 }
300
301 MFI->setObjectOffset(MFI->CreateStackObject(4,4), -FPOffset);
302 MFI->setObjectOffset(MFI->CreateStackObject(4,4), -RAOffset);
355 NumBytes += 4;
356 }
357
358 MFI->setObjectOffset(MFI->CreateStackObject(4,4), FPOffset);
359 MFI->setObjectOffset(MFI->CreateStackObject(4,4), RAOffset);
303360 MipsFI->setFPStackOffset(FPOffset);
304361 MipsFI->setRAStackOffset(RAOffset);
362
363 // Align stack.
364 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
365 NumBytes = ((NumBytes+Align-1)/Align*Align);
305366
306367 #ifndef NDEBUG
307368 DOUT << "FPOffset :" << FPOffset << "\n";
308369 DOUT << "RAOffset :" << RAOffset << "\n";
309 #endif
310
311 // Align stack.
312 NumBytes += 12;
313 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
314 NumBytes = ((NumBytes+Align-1)/Align*Align);
315
316 #ifndef NDEBUG
317370 DOUT << "New stack size :" << NumBytes << "\n\n";
318371 #endif
319372
358411 int FPOffset = MipsFI->getFPStackOffset();
359412 int RAOffset = MipsFI->getRAStackOffset();
360413
361 #ifndef NDEBUG
362 DOUT << "\n<--- EMIT EPILOGUE --->" << "\n";
363 DOUT << "Stack size :" << NumBytes << "\n";
364 DOUT << "FPOffset :" << FPOffset << "\n";
365 DOUT << "RAOffset :" << RAOffset << "\n\n";
366 #endif
367
368414 // if framepointer enabled, restore it and restore the
369415 // stack pointer
370416 if (hasFP(MF)) {
2525 const TargetInstrInfo &TII;
2626
2727 MipsRegisterInfo(const TargetInstrInfo &tii);
28
29 /// getRegisterNumbering - Given the enum value for some register, e.g.
30 /// Mips::RA, return the number that it corresponds to (e.g. 31).
31 static unsigned getRegisterNumbering(unsigned RegEnum);
2832
2933 /// Code Generation virtual methods...
3034 void storeRegToStackSlot(MachineBasicBlock &MBB,
6165 MachineBasicBlock &MBB,
6266 MachineBasicBlock::iterator I) const;
6367
68 /// Stack Frame Processing Methods
6469 void eliminateFrameIndex(MachineBasicBlock::iterator II,
6570 int SPAdj, RegScavenger *RS = NULL) const;
6671
6974 void emitPrologue(MachineFunction &MF) const;
7075 void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const;
7176
72 // Debug information queries.
77 /// Debug information queries.
7378 unsigned getRARegister() const;
7479 unsigned getFrameRegister(MachineFunction &MF) const;
7580
76 // Exception handling queries.
81 /// Exception handling queries.
7782 unsigned getEHExceptionRegister() const;
7883 unsigned getEHHandlerRegister() const;
7984 };
3030 }
3131
3232 // DataLayout --> Big-endian, 32-bit pointer/ABI/alignment
33 //
34 // FrameInfo --> StackGrowsDown, 8 bytes aligned,
35 // LOA : 0
33 // The stack is always 8 byte aligned
34 // On function prologue, the stack is created by decrementing
35 // its pointer. Once decremented, all references are done with positive
36 // offset from the stack/frame pointer, so StackGrowsUp is used.
3637 MipsTargetMachine::
3738 MipsTargetMachine(const Module &M, const std::string &FS):
3839 Subtarget(*this, M, FS), DataLayout("E-p:32:32:32"),
39 InstrInfo(*this), FrameInfo(TargetFrameInfo::StackGrowsDown, 8, 0),
40 InstrInfo(*this), FrameInfo(TargetFrameInfo::StackGrowsUp, 8, 0),
4041 TLInfo(*this) {}
4142
4243 // return 0 and must specify -march to gen MIPS code.