简体   繁体   English

如何使用 llvm-c API 使用内联汇编

[英]How to use inline assembly using the llvm-c API

I can not figure out how to piece everything together.我无法弄清楚如何将所有内容拼凑在一起。

I want to generate llvm-ir for the following "C" instruction:我想为以下“C”指令生成 llvm-ir:

asm volatile("nop");

In the future I would like to include more advanced inline assembly, but this will be a good start.将来我想包括更高级的内联汇编,但这将是一个好的开始。

I have read:我读过了:

https://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html https://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html

https://llvm.org/docs/LangRef.html#inline-assembler-expressions https://llvm.org/docs/LangRef.html#inline-assembler-expressions

My last attempt is the following:我的最后一次尝试如下:

char myasm[256]="nop";
char myconstraint[256]={0};

LLVMTypeRef voidty=LLVMVoidType();
LLVMValueRef asmcall=LLVMGetInlineAsm(voidty,myasm,strlen(myasm),
             myconstraint,strlen(myconstraint),1,1,LLVMInlineAsmDialectIntel);
LLVMSetVolatile(asmcall,1);
LLVMValueRef call = LLVMBuildCall( llbuilder, asmcall, (LLVMValueRef *)dummy->data,
             dummy->size, "acall");

I just get a segfault.我只是得到一个段错误。 The problem is not the "dummy" array.问题不在于“虚拟”数组。 I use it in other places without any problems.我在其他地方使用它没有任何问题。 In this case it is just an empty list of 0 size.在这种情况下,它只是一个 0 大小的空列表。

Help would be appreciated.帮助将不胜感激。

The missing ingredient was:缺少的成分是:

LLVMTypeRef functy = LLVMFunctionType(voidty, (LLVMTypeRef *)ptypes->data, ptypes->size, 0);

Whole answer is therefore:因此,整个答案是:

char myasm[256]="nop";
char myconstraint[256]={0};

LLVMTypeRef voidty = LLVMVoidType();
LLVMTypeRef functy = LLVMFunctionType(voidty, (LLVMTypeRef *)ptypes->data, ptypes->size, 0);
LLVMValueRef asmcall = LLVMGetInlineAsm(functy,myasm,strlen(myasm),
         myconstraint,strlen(myconstraint),1,1,LLVMInlineAsmDialectIntel);
LLVMValueRef call = LLVMBuildCall2( llbuilder, functy, asmcall,
         (LLVMValueRef *)pvalues->data,pvalues->size, "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM