From 8e48a0e860a692a1f8d1753b383eccfaa4dcd512 Mon Sep 17 00:00:00 2001 From: Vincent Mihalkovic Date: Sun, 10 Mar 2024 13:24:24 +0100 Subject: [PATCH] parser: decompiles 'sext i1 boolVal' as 'boolVal ? -1 : 0' https://llvm.org/docs/LangRef.html#sext-to-instruction This is based on a fact that if you sext i1 cmp_result to i32, you can get either -1 or 0. --- parser/createExpressions.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/parser/createExpressions.cpp b/parser/createExpressions.cpp index 2a4006c..12d8cfd 100644 --- a/parser/createExpressions.cpp +++ b/parser/createExpressions.cpp @@ -998,10 +998,25 @@ static Expr* parseCastInstruction(const llvm::Instruction& ins, Program& program const llvm::CastInst* CI = llvm::cast(&ins); + if (llvm::isa(CI)) { // for SExt, we do double cast -- first cast to the original // type that is made signed and then to the new type. // We must do that because we store all values as unsigned... + if (CI->getOperand(0)->getType()->isIntegerTy(1) && + CI->getDestTy()->isIntegerTy() ) { + // create zero + auto zero = std::make_unique("0", program.getType(CI->getDestTy())); + // create -1 + auto minusOne = std::make_unique("-1", program.getType(CI->getDestTy())); + // create comparison select ? -1 : 0 + Expr* cond = program.getExpr(ins.getOperand(0)); + + auto slctExpr = program.makeExpr(cond, minusOne.get(), zero.get()); + program.addOwnership(std::move(zero)); + program.addOwnership(std::move(minusOne)); + return slctExpr; + } auto *recastOrigExpr = program.makeExpr( expr,