Skip to content

Chapter 13: double (Floating-Point) Support - #22

Merged
johnhringiv merged 5 commits into
mainfrom
chapter_13
Jun 24, 2026
Merged

Chapter 13: double (Floating-Point) Support#22
johnhringiv merged 5 commits into
mainfrom
chapter_13

Conversation

@johnhringiv

Copy link
Copy Markdown
Owner

Chapter 13: double (Floating-Point) Support

Adds double (64-bit IEEE-754) end-to-end, removes the deprecated text-based emitter, adds linker-library / object-file inputs, and fixes a pre-existing tackifier panic.

Language features

  • New double type, floating-point constants (decimal/exponent lexing), arithmetic (+ - * /), unary negation, comparisons, and implicit/explicit conversions to and from every integer type.
  • Out-of-range floating constants round to ±infinity / zero (C §6.4.4.2 UB given a defined result), flagged by -Woverflow.

Pipeline

  • Lexer: ConstantDouble token + maximal-munch float regex; double keyword.
  • Parser: Type::Double, Const::ConstDouble(f64), is_integer() helper, float-literal -Woverflow; extracted local_label_prefix().
  • Validator: StaticIntStaticInit with a DoubleInit(f64) variant; double constant folding using the same rules as runtime (cvttsd2si-matching double→int bounds); double rejected in switch/case; warning guards so integer-only diagnostics (div-by-zero, shift-count, constant-conversion) skip doubles; ConstEvalError::InvalidType.
  • Tackifier: IntToDouble/UIntToDouble/DoubleToInt/DoubleToUInt conversion instructions (+ inline LLVM-correspondence docs).
  • Codegen: XMM registers folded into Reg; AssemblyType::Double; a .rodata constant pool (ConstantPool, deduped by f64::to_bits, per-use 8/16-byte alignment); SSE lowerings — addsd/subsd/mulsd/divsd, xorpd negation (-0.0 mask), comisd, and cvtsi2sd/cvttsd2si with the SSE2 workarounds for the unsigned conversions; System V ABI updated to pass double args in XMM0–7 and return in XMM0 (shared classify_operands for callee/caller); pseudo-replacement and fix_invalid legalize the new SSE forms (XMM14/XMM15 scratch).
  • Emitter (iced): encodes movsd/addsd/…/comisd/cvt*, emits the .rodata pool, adds setp/setnp + jp/jnp; -S prints the constant pool as bit-exact .quad 0x… # <decimal>. Fixed the disassembly symbol resolver so immediates (e.g. $0) are no longer mis-symbolized to a code symbol at address 0.

IEEE-754 / NaN semantics

Comparisons use comisd (unsigned-style flags) and handle unordered (NaN) per IEEE: </<= swap operands to the carry-clear A/AE (false on NaN), ==/!= combine the parity flag (NaN == x false, NaN != x true), and NaN is truthy in conditions. CondCode gained P/NP with per-variant docs.

Deterministic / "Safer C" edges

double→integer truncates toward zero; out-of-range or NaN yields the x86 cvttsd2si "integer indefinite" value (target MIN). Constant evaluation matches runtime for all of the above.

Bug fix (pre-existing since ch12)

A non-main function with an unsigned return type that falls off the end panicked the tackifier — the synthetic-return only handled int/long, so unsigned/double hit unreachable!. Now a real synthetic ret is emitted for every scalar return type. Regression test: tests/c_programs/unsigned/implicit_return_falloff.c (returns 2; fall-off values are deliberately unobserved per C §6.9.1p12).

Removed: text-based emitter

Deleted src/emit.rs and the --no-iced flag (the iced path is the sole, fully-featured backend; -S already provides readable disassembly). Removed the now-orphaned CondCode::ins_suffix and StaticInit::data_directive.

New CLI

  • -l <lib> (e.g. -lm) — forwarded to the linker (libwild / ld).
  • Pre-built .o inputs are accepted and passed straight to the linker (caller-owned, not cleaned up).

Test harness (tests/runner.rs)

  • requires_mathlib-lm (Linux; macOS uses libSystem).
  • helper_libs/ excluded from standalone discovery; the libs mapping is honored — helpers are gcc-compiled and linked via NCC (exercising NCC's linker with external objects).
  • CHAPTER_COMPLETED/EXTRA_COMPLETED 12 → 13.

Docs

README (Data Types table, grammar, IEEE/NaN note, -l/.o) and CLAUDE.md (type system, Floating Point note) updated; --no-iced/emit.rs references removed.

🤖 Generated with Claude Code

@claude

claude Bot commented Jun 19, 2026

Copy link
Copy Markdown

Claude encountered an error —— View job


I'll analyze this and get back to you.

@codecov

codecov Bot commented Jun 19, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.73545% with 97 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/validate.rs 75.88% 41 Missing ⚠️
src/emit_iced.rs 80.00% 29 Missing ⚠️
src/main.rs 64.10% 14 Missing ⚠️
src/parser.rs 88.46% 6 Missing ⚠️
src/codegen.rs 99.15% 4 Missing ⚠️
src/tacky.rs 94.73% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

Cover NCC-defined behaviors the book's suite can't (UB in standard C):
- out-of-range / +inf / NaN double->int and ->long yield the cvttsd2si
  "integer indefinite" (target MIN); discriminating comparisons avoid the
  8-bit exit-code masking INT_MIN/LONG_MIN.
- extend the return-fall-off regression with a double-returning function
  (synthetic return must target XMM0).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@claude

claude Bot commented Jun 19, 2026

Copy link
Copy Markdown

Claude encountered an error —— View job


I'll analyze this and get back to you.

johnhringiv and others added 2 commits June 23, 2026 21:49
The pinned @beta action defaulted to the retired claude-sonnet-4-20250514
model, causing the Claude Code Review job to fail with a 404 not_found_error
on every PR. Bump both workflows to @v1 (current default model), rename the
now-required direct_prompt -> prompt input, and check out submodules in
claude.yml so the @claude bot can run the test suite.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@johnhringiv
johnhringiv merged commit 5dbe2ca into main Jun 24, 2026
4 of 5 checks passed
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