Skip to content

Commit 94e8eaa

Browse files
committed
Add inline assembly
1 parent 37d8109 commit 94e8eaa

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Sources/LLVM/IRBuilder.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,48 @@ public class IRBuilder {
17211721
return Alias(llvm: LLVMAddAlias(module.llvm, type.asLLVM(), aliasee.asLLVM(), name))
17221722
}
17231723

1724+
// MARK: Inline Assembly
1725+
1726+
/// Builds a value representing an inline assembly expression (as opposed to
1727+
/// module-level inline assembly).
1728+
///
1729+
/// LLVM represents inline assembler as a template string (containing the
1730+
/// instructions to emit), a list of operand constraints (stored as a string),
1731+
/// and some flags.
1732+
///
1733+
/// The template string supports argument substitution of the operands using
1734+
/// "$" followed by a number, to indicate substitution of the given
1735+
/// register/memory location, as specified by the constraint string.
1736+
/// "${NUM:MODIFIER}" may also be used, where MODIFIER is a target-specific
1737+
/// annotation for how to print the operand (see [Asm Template Argument
1738+
/// Modifiers](https://llvm.org/docs/LangRef.html#inline-asm-modifiers)).
1739+
///
1740+
/// LLVM’s support for inline asm is modeled closely on the requirements of
1741+
/// Clang’s GCC-compatible inline-asm support. Thus, the feature-set and the
1742+
/// constraint and modifier codes are similar or identical to those in GCC’s
1743+
/// inline asm support.
1744+
///
1745+
/// However, the syntax of the template and constraint strings is not the
1746+
/// same as the syntax accepted by GCC and Clang, and, while most constraint
1747+
/// letters are passed through as-is by Clang, some get translated to other
1748+
/// codes when converting from the C source to the LLVM assembly.
1749+
///
1750+
/// - parameter asm: The inline assembly expression template string.
1751+
/// - parameter type: The type of the parameters and return value of the
1752+
/// assembly expression string.
1753+
/// - parameter constraints: A comma-separated string, each element containing
1754+
/// one or more constraint codes.
1755+
/// - parameter hasSideEffects: Whether this inline asm expression has
1756+
/// side effects. Defaults to `false`.
1757+
/// - parameter needsAlignedStack: Whether the function containing the
1758+
/// asm needs to align its stack conservatively. Defaults to `true`.
1759+
///
1760+
/// - returns: A representation of the newly created inline assembly
1761+
/// expression.
1762+
public func buildInlineAssembly(_ asm: String, type: FunctionType, constraints: String = "", hasSideEffects: Bool = true, needsAlignedStack: Bool = true) -> IRValue {
1763+
return LLVMConstInlineAsm(type.asLLVM(), asm, constraints, hasSideEffects.llvm, needsAlignedStack.llvm)
1764+
}
1765+
17241766
deinit {
17251767
LLVMDisposeBuilder(llvm)
17261768
}

0 commit comments

Comments
 (0)