Skip to content

fix: implement ABS as a builtin - #1849

Open
ghaith wants to merge 1 commit into
masterfrom
fix/abs_unsigned
Open

fix: implement ABS as a builtin#1849
ghaith wants to merge 1 commit into
masterfrom
fix/abs_unsigned

Conversation

@ghaith

@ghaith ghaith commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator

Summary

Calling ABS with an unsigned argument (USINT/UINT/UDINT/ULINT) resolved to a monomorph the standard library does not provide. The compiler emitted a bare external declaration, the shared object carried an undefined symbol, and the call crashed at runtime.

ABS is now a compiler builtin covering the whole ANY_NUM domain:

  • signed integers lower to the llvm.abs intrinsic with is_int_min_poison = false, so ABS(INT_MIN) wraps to INT_MIN exactly like the previous library implementation
  • floats lower to llvm.fabs
  • unsigned arguments are returned unchanged and report the new warning E150 ("ABS on a value of unsigned type 'UINT' has no effect") — an unsigned ABS call is almost always a mistake, e.g. an argument expression that already underflowed

Notes for review

  • The builtin coexists with the generic ABS declaration that standard library includes still ship (same generic-overload exemption ADD already relies on). Index::get_builtin_function now checks every POU registered under a name instead of only the first, so builtin codegen dispatch no longer depends on index insertion order.
  • Llvm::get_intrinsic_declaration is extracted from build_lifetime_marker and reused by the ABS codegen.
  • Explicit calls to ABS__DINT etc. still resolve to the library monomorphs and link as before (covered by lit).
  • The stdlib ABS integration tests moved to tests/lit/single/builtin/abs_signed.st / abs_floats.st since ABS is no longer implemented by the library; abs_unsigned.st and abs_unsigned_warning.st cover the unsigned runtime behavior and the E150 diagnostics end-to-end.

Release/1.0.x counterpart with a minimal patch-lane approach: #1850

Test plan

  • cargo test --workspace
  • ./scripts/build.sh --lit (default + -Onone)
  • new codegen inline snapshots, validator unit tests, and four lit tests

🤖 Generated with Claude Code

Calling ABS with an unsigned argument bound to a monomorph the standard
library does not provide and crashed at runtime. ABS is now a builtin:
signed integers lower to llvm.abs (is_int_min_poison = false), floats to
llvm.fabs, and unsigned arguments are returned unchanged and report the
new warning E150 because the call has no effect.

The builtin takes precedence over the generic ABS declaration that
standard library includes still ship; Index::get_builtin_function now
considers every POU registered under a name instead of only the first.
The stdlib ABS tests moved to lit fixtures since ABS is no longer
implemented by the library.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown

Build Artifacts

🐧 Linux

Artifact Link Size
deb-x86_64 Download 38.4 MB
schema Download 0.0 MB
stdlib Download 32.4 MB
plc-x86_64 Download 43.5 MB
deb-aarch64 Download 30.8 MB
plc-aarch64 Download 43.4 MB

From workflow run

🪟 Windows

Artifact Link Size
stdlib.lib Download 4.0 MB
stdlib.dll Download 0.1 MB
plc.exe Download 38.3 MB

From workflow run

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant