Skip to content

@[target("arch")] architecture guards (§16.13) #479

Closed

Description

@ehartford

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

  1. Extend attribute parsing in src/Parser.w to recognize @[target("...")] and attach the target string to the following declaration/block/asm expression. Validate that the argument is a string literal.
  2. Define canonical architecture names in one place, mapping runtime values like armv8/aarch64 to the spec spelling aarch64 and x86_64.
  3. 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.
  4. 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.
  5. 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.
  6. Add helpful diagnostics: target guard 'x86_64' does not match active target 'aarch64' and inline 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 :test pass.

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

  1. ehartford commented on Jun 16, 2026

    @ehartford
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions