Skip to content

Phase 2: @print macro family — compile-time format expansion - #11

Merged
CierCier merged 5 commits into
mainfrom
std/rt-casts-port
Jul 21, 2026
Merged

CierCier merged 5 commits into
mainfrom
std/rt-casts-port

Conversation

@CierCier

@CierCier CierCier commented Jul 21, 2026 •

Copy link
Copy Markdown
Owner

Summary

Implements Phase 2 of the libc-removal plan: the @print/@println/@eprint/@eprintln/@fprint/@sprint built-in macro family with compile-time {} placeholder expansion into BufWriter method calls.

Changes

Core: @print macro family

  • agc/src/builtin_macros/mod.rs — parse_format helper, FormatSegment enum, PrintHandler struct (MacroHandler impl), registration for all 6 macros
  • agc/src/semantic/typeck.rs — print_typeck method: validates format string literal, counts placeholders, checks arg count, type-checks value args
  • agc/src/codegen/llvm_ir.rs — print_codegen: extracts format segments, builds synthetic AST expressions for BufWriter method calls, dispatches placeholder types to correct write method (write_str/write_i64/write_u64/write_f64/write_bool). Also: value_write_method_name helper, sprint_codegen placeholder, resolve_receiver_type Literal extension

Infra

  • MacroHandler trait extended with name: &str parameter on both methods; all existing handlers updated
  • MacroRegistry::type_check/codegen pass name to handlers

Stdlib & tests

  • String literal concatenation in parser ("A" "B" → "AB")
  • Partial libc extern deletion across stdlib files
  • Test rewrites migrating from printf to @println

Testing

  • cargo test -p agc: 217/218 pass (1 pre-existing failure in display.ag trait parsing)
  • Manual verification: all print macros produce correct output; placeholder type dispatch works for str, i32, i64, u64, f64, bool

Notes

  • @sprint returns empty string (full implementation deferred per plan)
  • --dynamic-runtime flag required at runtime (static-runtime default is pre-existing)
  • Phase 3 (delete all libc externs) ready to start after this PR

Greptile Summary

This PR adds compile-time formatting macros backed by the pure-Silver I/O path. The main changes are:

  • Six built-in print macros with placeholder parsing and type checking.
  • LLVM expansion into typed BufWriter method calls.
  • String-literal concatenation and variadic function metadata.
  • A directly owned BufWriter implementation for standard output, files, and formatted strings.
  • Static-runtime linking updates and libc removal across the standard library.
  • Updated bootstrap artifacts and integration tests.

Confidence Score: 4/5

The remaining runtime failures should be fixed before merging.

  • Side-effecting placeholder expressions can run twice when code generation falls back to LLVM type inference.
  • Failed buffer growth can leave a null data pointer paired with nonzero capacity.

agc/src/codegen/llvm_ir.rs and std/io.ag

Important Files Changed

Filename Overview
agc/src/builtin_macros/mod.rs Registers the print macro family and adds shared format-string segmentation.
agc/src/semantic/typeck.rs Validates format literals, placeholder counts, and print arguments.
agc/src/codegen/llvm_ir.rs Expands print macros into writer calls, but fallback type inference can emit a placeholder expression twice.
std/io.ag Adds the directly owned BufWriter path, but failed buffer growth leaves its pointer and capacity inconsistent.
agc/src/main.rs Adds static and dynamic runtime options and adjusts linker arguments.

Reviews (2): Last reviewed commit: "Update bootstrap artifacts after compile..." | Re-trigger Greptile

Greptile also left 2 inline comments on this PR.

Context used:

  • Context used - You are an expert systems programmer, compiler eng... (source)

CierCier added 3 commits July 21, 2026 11:50
…d/rt/

- Delete silver_runtime/ Rust crate entirely
- Add/update pure-Silver runtime in std/rt/:
  - std/rt/casts.ag (numeric cast registry)
  - std/rt/value.ag (boxed Value constructors)
  - std/rt/methods.ag (method dispatch, intern table, builtins)
  - std/rt/runtime.ag (unified Runtime aggregate)
  - std/sys/entry.ag (_start entry point)
- Fix interior pointer bug in MethodRegistry (no stored TypeRegistry*/Heap*)
- Add --static-runtime flag and -nostdlib linking in compiler driver
- Add tests: rt_casts_test, rt_methods_test, runtime_test, static_link_test
- Fix all doc references (AGENTS.md, README.md, runtime-migration.md)
- Mark plan/runtime-migration.md as superseded
Generated by ./update-bootstrap.sh after deleting silver_runtime/ and
adding pure-Silver std/rt/ modules (casts, methods, runtime, value, entry).
- Extended MacroHandler trait to pass macro name to type_check/codegen
- Added parse_format helper and FormatSegment enum in builtin_macros
- Added PrintHandler struct registering all 6 macros (@print/@println/@Eprint
  /@eprintln/@fprint/@Sprint)
- Added print_typeck method on TypeChecker for format validation
- Added print_codegen on LlvmIrGenerator for BufWriter method-call expansion
- Added value_write_method_name to dispatch format placeholders to correct
  BufWriter methods (write_str/write_i64/write_u64/write_f64/write_bool)
- Extended resolve_receiver_type with Literal type inference
- @Sprint returns empty string as placeholder (full impl deferred)
- Stdlib and test changes: string concat, libc extern removal prep,
  test rewrites for print macros
- Various test fixes and libc extern deletion across stdlib
Copilot AI review requested due to automatic review settings July 21, 2026 09:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@CierCier

Copy link
Copy Markdown
Owner Author

@greptile-apps

Comment thread agc/src/codegen/llvm_ir.rs Resolved
Comment thread agc/src/codegen/llvm_ir.rs Resolved
Comment thread agc/src/codegen/llvm_ir.rs Resolved
Comment thread agc/src/codegen/llvm_ir.rs Resolved
Comment thread agc/src/codegen/llvm_ir.rs Resolved
Comment thread std/io.ag Resolved
Comment thread std/io.ag Resolved
Comment thread agc/src/codegen/llvm_ir.rs Resolved
@CierCier

Copy link
Copy Markdown
Owner Author

@greptile-apps

Some(ty) => Self::type_to_write_method(&ty.kind),
None => {
// Fallback: emit the value and infer type from LLVM type
let val = self.emit_expression_value(expr).map_err(|e| {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Fallback evaluates expressions twice

When resolve_receiver_type returns None, this call emits the placeholder expression to infer its LLVM type. emit_method_call_expression then emits the same expression again as the writer argument. A supported conditional or block expression containing a side-effecting call therefore runs twice, discards the first result, and prints the second. Type inference must not emit the expression unless that emitted value is reused by the generated writer call.

Context Used: You are an expert systems programmer, compiler eng... (source)

Comment thread std/io.ag
Comment on lines +67 to +69
u8* ptr = (u8*)realloc<u8>((u8*)(*self).data, new_cap);
(*self).data = (i64)ptr;
(*self).cap = new_cap;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Failed growth corrupts writer state

The allocator can return 0 when growth fails, but this path replaces data and records a nonzero capacity without checking the result. The old allocation is lost, and the next write skips initialization because cap != 0 before indexing through the null data pointer. An allocation failure during buffer growth therefore causes a leak followed by a null-pointer write. Commit the new pointer and capacity only after confirming that reallocation succeeded.

Context Used: You are an expert systems programmer, compiler eng... (source)

@CierCier
CierCier merged commit c0c9d9d into main Jul 21, 2026
2 checks passed
@CierCier
CierCier deleted the std/rt-casts-port branch July 21, 2026 10:18
CierCier added a commit that referenced this pull request Aug 1, 2026
Phase 2: @print macro family — compile-time format expansion
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.

2 participants