From 2fc0db182e3de9df0405d4a4d8b9bc9b46667fdc Mon Sep 17 00:00:00 2001 From: Roger Ferrer Ibanez Date: Tue, 14 Aug 2018 08:30:42 +0000 Subject: [PATCH] [RISCV] Fix incorrect use of MCInstBuilder This is a fix for r339314. MCInstBuilder uses the named parameter idiom and an 'operator MCInst&' to ease the creation of MCInsts. As the object of MCInstBuilder owns the MCInst is manipulating, the lifetime of the MCInst is bound to that of MCInstBuilder. In r339314 I bound a reference to the MCInst in an initializer. The temporary of MCInstBuilder (and also its MCInst) is destroyed at the end of the declaration leading to a dangling reference. Fix this by using MCInstBuilder inside an argument of a function call. Temporaries in function calls are destroyed in the enclosing full expression, so the the reference to MCInst is still valid when emitToStreamer executes. llvm-svn: 339654 --- lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp b/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp index 5a56ddf0656..3a5257206bf 100644 --- a/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp +++ b/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp @@ -1227,19 +1227,17 @@ void RISCVAsmParser::emitLoadLocalAddress(MCInst &Inst, SMLoc IDLoc, const RISCVMCExpr *Symbol = RISCVMCExpr::create( Inst.getOperand(1).getExpr(), RISCVMCExpr::VK_RISCV_PCREL_HI, Ctx); - MCInst &AUIPC = - MCInstBuilder(RISCV::AUIPC).addOperand(DestReg).addExpr(Symbol); - emitToStreamer(Out, AUIPC); + emitToStreamer( + Out, MCInstBuilder(RISCV::AUIPC).addOperand(DestReg).addExpr(Symbol)); const MCExpr *RefToLinkTmpLabel = RISCVMCExpr::create(MCSymbolRefExpr::create(TmpLabel, Ctx), RISCVMCExpr::VK_RISCV_PCREL_LO, Ctx); - MCInst &ADDI = MCInstBuilder(RISCV::ADDI) - .addOperand(DestReg) - .addOperand(DestReg) - .addExpr(RefToLinkTmpLabel); - emitToStreamer(Out, ADDI); + emitToStreamer(Out, MCInstBuilder(RISCV::ADDI) + .addOperand(DestReg) + .addOperand(DestReg) + .addExpr(RefToLinkTmpLabel)); } bool RISCVAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,