From c2b81f69ed34754c38c9f5caf3277d058df00c58 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 14 Oct 2003 19:09:05 +0000 Subject: [PATCH] Output a contorted sequence of instructions to make sure that we don't access 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 | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/Target/X86/X86RegisterInfo.cpp b/lib/Target/X86/X86RegisterInfo.cpp index c15a080a29a..b201fda1078 100644 --- a/lib/Target/X86/X86RegisterInfo.cpp +++ b/lib/Target/X86/X86RegisterInfo.cpp @@ -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-], 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); + } } } -- 2.34.1