Repository navigation
Description
Summary
Implement @[target("arch")] guards for architecture-specific inline assembly and related target-specific declarations. This lets non-portable asm stay explicit without breaking other supported hosts.
Spec
§16.13 defines asm as target-specific and requiring unsafe context (docs/with-specification.md:8925). It also says assembly is inherently non-portable and @[target("aarch64")] / @[target("x86_64")] can guard architecture-specific blocks (docs/with-specification.md:8981).
Current state
Inline asm is parsed into NK_ASM_EXPR with optional outputs, inputs, clobbers, and volatile flags (src/Parser.w:4840). LLVM bridge support creates inline asm values and initializes native targets (src/compiler/LlvmBridge.w:1322). The compiler can query host architecture through runtime_sysinfo_arch (src/compiler/Runtime.w:98). Attribute parsing does not recognize or store target(...) (src/Parser.w:439), so there is no guard to suppress or reject asm for the wrong architecture.
Implementation plan
- Extend attribute parsing in
src/Parser.wto recognize@[target("...")]and attach the target string to the following declaration/block/asm expression. Validate that the argument is a string literal. - Define canonical architecture names in one place, mapping runtime values like
armv8/aarch64to the spec spellingaarch64andx86_64. - In Sema, when a target guard does not match the active target, skip checking/lowering the guarded item if it is a declaration alternative, or emit a clear diagnostic if the guarded expression is unconditionally used on the wrong target. Do not silently lower wrong-target assembly.
- For inline asm specifically, require
@[target]on asm blocks whose constraints are target-specific unless the backend can prove target neutrality. Keep the existing unsafe requirement. - Thread the active target from CLI/build configuration, not only host runtime, so cross-target builds can use the selected target once cross-codegen is implemented.
- Add helpful diagnostics:
target guard 'x86_64' does not match active target 'aarch64'andinline asm requires a matching target guard.
Test plan
Add test/spec/spec_ss16_13_target_guard.w with same-host positive coverage guarded by the detected architecture and a non-matching guarded declaration that is not used. Add compile-error tests for unknown arch strings, non-string arguments, wrong-target unconditional asm use, and asm without a guard if that policy is adopted. Add parser tests proving @[target("aarch64")] is preserved in the AST.
Acceptance criteria
@[target("aarch64")]and@[target("x86_64")]parse and are visible to Sema.- Matching guards permit the item/expression; non-matching guards do not produce wrong-target code.
- Wrong-target use fails loudly rather than becoming a codegen crash.
- Inline asm tests cover safe parsing, unsafe requirement, and target guards.
with build && with build :fixpoint && with build :testpass.
Dependencies
Overlaps #505 for inline asm coverage.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 16.13.1.12.
Activity
@[target(\"arch\")] architecture guards (#479)
ehartford commentedon Jun 16, 2026
Implemented in 156ba99. @[target("aarch64")]/@[target("x86_64")] now parse with validation (non-string args and unknown arch names are compile errors). The guard is recorded on the fn node (new fn_target_arch side table) so it is visible to later phases; a top-level declaration guarded for a non-active architecture is excluded from compilation entirely (not collected, checked, or lowered), making the per-arch declaration-alternative pattern work and preventing wrong-target codegen. Referencing an excluded symbol fails loudly. Active target is the host arch until CLI cross-target selection lands (#425). Tests: spec_ss16_13_target_guard plus err_target_excluded_symbol_use / err_target_unknown_arch / err_target_non_string. build + fixpoint + full compile-error/spec/behavior suites pass.