[LLVMdev] Trouble with inline asm

Tomas Lindquist Olsen tomas.l.olsen at gmail.com
Fri Jun 6 06:32:43 PDT 2008

Hi all,

I'm having some trouble with inline asm expressions, more specifically
how to create the right FunctionType for a given constraint set.
So far it has worked well for inputs, but not for outputs. The inline
asm support in this language (which is D, LLVMDC[1]) is through asm
I never have inline asm *expressions*, and outputs are always via memory.

I D my test looks like this:

extern(C) int printf(char*,...);
int main()
    int i = 12;
    printf("%d\n", i);
        mov EAX, i;
        add EAX, EAX;
        mul EAX, 2;
        mov i, EAX; // ***
    printf("%d\n", i);
    return 0;

if the *** line is commented I get this LL (unrelevant stuff removed):

; ModuleID = 'tangotests.asm1'
target datalayout =
target triple = "i686-unknown-linux-gnu"

@.stringliteral = internal constant [4 x i8] c"%d\0A\00"
 ; <[4 x i8]*> [#uses=1]

declare i32 @printf(i8*, ...)

define fastcc i32 @_Dmain() {
        %i = alloca i32         ; <i32*> [#uses=4]
        store i32 12, i32* %i
        %tmp = load i32* %i             ; <i32> [#uses=1]
        %tmp1 = call i32 (i8*, ...)* @printf( i8* getelementptr ([4 x
i8]* @.stringliteral, i32 0, i32 0), i32 %tmp )           ; <i32>
        %tmp2 = load i32* %i            ; <i32> [#uses=1]
        call void asm sideeffect "movl $0, %eax", "m,~{eax}"( i32 %tmp2 )
        call void asm sideeffect "addl %eax, %eax", "~{eax},~{eax}"( )
        call void asm sideeffect "mull $0", "i,~{eax},~{edx},~{eax}"( i32 2 )
        %tmp3 = load i32* %i            ; <i32> [#uses=1]
        %tmp4 = call i32 (i8*, ...)* @printf( i8* getelementptr ([4 x
i8]* @.stringliteral, i32 0, i32 0), i32 %tmp3 )         ; <i32>
        ret i32 0

Now... No matter what I try I can't figure out what the function
signature should be for the *** line. I always get the assertion:
        InlineAsm.cpp:43: llvm::InlineAsm::InlineAsm(const
llvm::FunctionType*, const std::string&, const std::string&, bool):
Assertion `Verify(Ty, constraints) && "Function type not legal for
constraints!"' failed.

The inline expr I try to create is like:

        call void asm sideeffect "movl %eax, $0", "=m" (i32* var)

Any clues would be much appreciated! I'd like to say that I'm very new
with this format of assembler, constraints etc.

Thanx, Tomas Lindquist Olsen

[1] : http://www.dsource.org/projects/llvmdc

More information about the llvm-dev mailing list