Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert inline assembly expressions using Llvm pass

I am trying to create and append inline assembly expressions using an llvm pass. I am aware that I can use void appendModuleInlineAsm(StringRef Asm) , but I couldn't make it work.

Basically I want to append an instruction like this:

%X = call i32 asm "bswap $0", "=r,r"(i32 %Y)

Just before an other instruction. Has anybody tried it?

like image 895
Giorgio Zacharo Avatar asked Sep 29 '22 15:09

Giorgio Zacharo


1 Answers

Basic idea:

a) You'll want a function pass to iterate over instructions, b) You'll want to iterate through the basic blocks and the instructions until you find the instruction you want to insert before, c) Do something like the below:

  llvm::InlineAsm *IA =
    llvm::InlineAsm::get(FTy, AsmString, Constraints, HasSideEffect,
                         /* IsAlignStack */ false, AsmDialect);
  llvm::CallInst *Result = Builder.CreateCall(IA, Args);
  Result->addAttribute(llvm::AttributeSet::FunctionIndex,
                       llvm::Attribute::NoUnwind);

which was liberally stolen from clang. Take a look at the docs to InlineAsm for the rest of the arguments and Builder is a DIBuilder instance. Make sure you set up the DIBuilder insertion point at the location you want.

like image 67
echristo Avatar answered Oct 17 '22 04:10

echristo