Inline Assembly (asm)
Silk provides an asm keyword for embedding inline assembly in a way that is
explicit in source code and assembled at compile time.
Inline assembly is inherently low-level and target-dependent. Use asm when
you need precise control over emitted instructions that cannot be expressed
with the standard library or ext bindings.
Syntax#
asm is an expression that takes a single string literal:
fn spin_pause () -> void {
asm "pause";
}
The expression has type void and is intended to appear as an expression
statement.
Semantics#
asm "<text>";emits the machine instructions assembled from<text>.- The assembly text must be a string literal; it is not computed at runtime.
- Inline asm produces no values (type
void). - Inline asm is treated as an explicit side-effecting operation (it is not elided).
Assembly dialect#
Currently, <text> is assembled by the system assembler
(as) for the active native host backend:
linux/x86_64: GNUasin Intel syntax (.intel_syntax noprefix)macos/aarch64: Appleasin its native arm64 syntax
The assembly may contain multiple instructions, for example:
fn main () -> int {
asm "mov rax, rax\nnop";
return 0;
}
Restrictions#
asmis currently implemented only for the nativelinux/x86_64andmacos/aarch64backends.- The assembled output must not require relocations. As a result, inline
asm may not refer to external symbols (for example
call foowherefoois not defined within the asm text). - Operands (inputs/outputs), clobbers, and options are not yet modeled in the type system. Inline asm is therefore not suitable for expressing constraints like “reads memory” / “clobbers rax”; it is raw instruction emission.
Portability and safety notes#
asmis target-dependent by nature. The implementation is supported only for the nativelinux/x86_64andmacos/aarch64backends.- Using
asmcan make programs non-portable. Prefer standard library facilities and compiler-provided intrinsics when possible.
Notes#
- Parser: accepts
asm "<string literal>"as an expression. - Type checker:
- requires a string literal operand,
- assembles the text for the native backend and reports
E2116when the asm fails to assemble or uses unsupported features (such as relocations), - assigns the expression type
void. - Code generation:
- emits the assembled bytes in the
linux/x86_64IR→ELF backend, - emits the assembled bytes in the temporary
macos/aarch64host Mach-O backend. - Tests:
- end-to-end coverage via:
Not yet implemented:
- inline asm with operands (inputs/outputs), clobbers, or options,
- any
asmsupport on non-linux/x86_64targets/backends. except for the currentmacos/aarch64host Mach-O subset.
Source repository · Edit this page · View Markdown