Output a contorted sequence of instructions to make sure that we don't access
authorChris Lattner <sabre@nondot.org>
Tue, 14 Oct 2003 19:09:05 +0000 (19:09 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 14 Oct 2003 19:09:05 +0000 (19:09 +0000)
off the bottom of the stack.  This fixes PR#41

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@9114 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Target/X86/X86RegisterInfo.cpp

index c15a080a29a9cd85fce12e89aaae8774439263bb..b201fda1078b5c95792718db41232af8172394b2 100644 (file)
@@ -161,12 +161,24 @@ void X86RegisterInfo::emitPrologue(MachineFunction &MF) const {
     // guaranteed to be the last slot by processFunctionBeforeFrameFinalized.
     int EBPOffset = MFI->getObjectOffset(MFI->getObjectIndexEnd()-1)+4;
 
+    if (NumBytes) {   // adjust stack pointer: ESP -= numbytes
+      MI= BuildMI(X86::SUBri32, 2, X86::ESP).addReg(X86::ESP).addZImm(NumBytes);
+      MBBI = MBB.insert(MBBI, MI)+1;
+    }
+
+    // Save EBP into the appropriate stack slot...
     MI = addRegOffset(BuildMI(X86::MOVrm32, 5),    // mov [ESP-<offset>], EBP
-                     X86::ESP, EBPOffset).addReg(X86::EBP);
+                     X86::ESP, EBPOffset+NumBytes).addReg(X86::EBP);
     MBBI = MBB.insert(MBBI, MI)+1;
-    
-    MI = BuildMI(X86::MOVrr32, 2, X86::EBP).addReg(X86::ESP);
+
+    // Update EBP with the new base value...
+    if (NumBytes == 0)    // mov EBP, ESP
+      MI = BuildMI(X86::MOVrr32, 2, X86::EBP).addReg(X86::ESP);
+    else                  // lea EBP, [ESP+StackSize]
+      MI = addRegOffset(BuildMI(X86::LEAr32, 5, X86::EBP), X86::ESP, NumBytes);
+
     MBBI = MBB.insert(MBBI, MI)+1;
+
   } else {
     // When we have no frame pointer, we reserve argument space for call sites
     // in the function immediately on entry to the current function.  This
@@ -181,12 +193,12 @@ void X86RegisterInfo::emitPrologue(MachineFunction &MF) const {
 
     // Update frame info to pretend that this is part of the stack...
     MFI->setStackSize(NumBytes);
-  }
 
-  if (NumBytes) {
-    // adjust stack pointer: ESP -= numbytes
-    MI  = BuildMI(X86::SUBri32, 2, X86::ESP).addReg(X86::ESP).addZImm(NumBytes);
-    MBBI = 1+MBB.insert(MBBI, MI);
+    if (NumBytes) {
+      // adjust stack pointer: ESP -= numbytes
+      MI= BuildMI(X86::SUBri32, 2, X86::ESP).addReg(X86::ESP).addZImm(NumBytes);
+      MBB.insert(MBBI, MI);
+    }
   }
 }