Skip to content

llvm_asm

IsaacShelton edited this page Mar 21, 2022 · 1 revision

llvm_asm

The llvm_asm keyword can be used to write inline assembly code

Syntax

llvm_asm (intel|att) [side_effects, stack_align] {
    "assembly code" [,]
    "assembly code" [,]
    "assembly code" [,]
} "constraints string" (supplied, values)

Where "constraints string" is a string with LLVM style constraints:

Common output constraints:

  • =*m == memory output
  • =r == general purpose register output

Common input constraints:

  • *m == memory input
  • r == general purpose register input
  • i == immediate value input

Common clobbers:

  • ~{memory} == clobbers memory

and $0, $1, $2, etc. refer to supplied arguments.

Modifiers

Also, you can optionally mark assembly code as having side effects, or requiring the stack to be aligned

llvm_asm intel side_effects stack_align {
    ...
} "..." (...)

Usage Example

import basics

func main {
    value int = 3
    result int = 0

    llvm_asm att {
        "movl $1, $0"
    } "=*m,r,~{memory}" (&result, value)

    printf("Result = %d\n", result)

    multiplied int = 0

    llvm_asm att {
        "imull $1, $2"
        "movl $2, $0"
    } "=*m,r,r,~{memory}" (&multiplied, value, result)

    printf("Multiplied = %d\n", multiplied)
}
Clone this wiki locally