Skip to content

feat: Buffer table and SRT flush-on-exit - #13

Merged
CierCier merged 3 commits into
mainfrom
feat/buffer-table-flush-on-exit
Jul 22, 2026
Merged

CierCier merged 3 commits into
mainfrom
feat/buffer-table-flush-on-exit

Conversation

@CierCier

@CierCier CierCier commented Jul 21, 2026 •

Copy link
Copy Markdown
Owner

Summary

Implements buffering-mode control for BufWriter and ensures @print() output without a trailing newline is visible before process exit.

Changes

std/io.ag — Buffered I/O with mode control

  • Added list_next, list_prev, mode fields to BufWriter struct
  • Defined three buffering modes: IOMODE_UNBUFFERED (0), IOMODE_LINE (1), IOMODE_BLOCK (2)
  • Added __buf_writer_list — intrusive linked list head for flush-on-exit registration
  • ensure_init: mode-aware; skips buffer allocation in UNBUFFERED mode; registers global streams (fd 1/2) in the linked list
  • write(): UNBUFFERED → direct syscall; LINE → flush on any embedded \n; BLOCK → buffer up to capacity, direct-write oversized chunks
  • Added __silver_flush_all(): iterates the linked list, calls flush() on each registered writer
  • Updated STDOUT (IOMODE_LINE) and STDERR (IOMODE_UNBUFFERED) initializers

std/sys/entry.ag — Flush before exit

  • Added import std.io for access to __silver_flush_all
  • _start: saves main's return value in %r12 (callee-saved), calls __silver_flush_all, restores value to %rdi, then exit_group

agc/src/codegen/llvm_ir.rs — Const cast support

  • Added IntValue → PointerType match arm to const_cast_value_to_basic_type for null pointer constants in global initializers

Verification

  • cargo test -p agc: 218/218 pass
  • bash tests/run_tests.sh: 26/26 pass
  • @print("no newline here") output now visible before process exit
  • @println line-buffered behavior unchanged
  • @eprint unbuffered (appears before stdout) confirmed
  • 1000 x block output flushes on exit (wc -c = 1000)

Greptile Summary

This PR adds configurable buffered output and flushes standard streams before static programs exit. The main changes are:

  • Line, block, and unbuffered writer modes.
  • Exit-time flushing for standard output streams.
  • Updated @sprint writer layout handling.
  • Null pointer constants in global initializers.

Confidence Score: 4/5

The remaining @sprint initialization bug should be fixed before merging.

  • @sprint constructs a seven-field writer but initializes only four fields.
  • The runtime immediately branches on the uninitialized mode value.
  • A value of zero silently discards formatted output.

agc/src/codegen/llvm_ir.rs

Important Files Changed

Filename Overview
agc/src/codegen/llvm_ir.rs Updates the hard-coded @sprint layout and adds null pointer constant casting, but leaves the writer mode uninitialized.
std/io.ag Adds buffering modes, capacity guards, and explicit exit-time flushing of standard streams.
std/sys/entry.ag Flushes standard streams after main while preserving the process exit value.

Reviews (2): Last reviewed commit: "fix: Address Greptile review comments - ..." | Re-trigger Greptile

Greptile also left 1 inline comment on this PR.

Context used:

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

- Add linked-list pointers (list_next, list_prev) and mode field to BufWriter
- Add three buffering modes: IOMODE_UNBUFFERED, IOMODE_LINE, IOMODE_BLOCK
- Rewrite ensure_init for mode-aware init; register only globals (fd 1/2) via
  intrusive linked list for flush-on-exit
- Rewrite write() for mode-aware behavior: UNBUFFERED direct-syscall, LINE
  flushes on any embedded \n, BLOCK buffers up to capacity
- Add __silver_flush_all() called by _start after main() returns
- Update _start to save main() return in %r12, call __silver_flush_all,
  then exit_group
- Add const_cast support for IntValue → PointerType (null pointer cast) in
  global initializer codegen
Copilot AI review requested due to automatic review settings July 21, 2026 15:24

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.

Comment thread std/io.ag Resolved
Comment thread std/io.ag Resolved
Comment thread std/io.ag Resolved
Comment on lines +1089 to +1107
let data_ptr = self.builder.build_struct_gep(buf_writer_llvm_ty, writer_tmp, 0, "sprint.data")
.map_err(|e| CodegenError::with_span(format!("sprint struct gep 0 failed: {e}"), expr.span.clone()))?;
self.builder.build_store(fd_ptr, neg_one_i32)
.map_err(|e| CodegenError::with_span(format!("sprint store fd failed: {e}"), expr.span.clone()))?;

let data_ptr = self.builder.build_struct_gep(buf_writer_llvm_ty, writer_tmp, 1, "sprint.data")
.map_err(|e| CodegenError::with_span(format!("sprint struct gep 1 failed: {e}"), expr.span.clone()))?;
self.builder.build_store(data_ptr, zero_i64)
.map_err(|e| CodegenError::with_span(format!("sprint store data failed: {e}"), expr.span.clone()))?;

let len_ptr = self.builder.build_struct_gep(buf_writer_llvm_ty, writer_tmp, 2, "sprint.len")
.map_err(|e| CodegenError::with_span(format!("sprint struct gep 2 failed: {e}"), expr.span.clone()))?;
let len_ptr = self.builder.build_struct_gep(buf_writer_llvm_ty, writer_tmp, 1, "sprint.len")
.map_err(|e| CodegenError::with_span(format!("sprint struct gep 1 failed: {e}"), expr.span.clone()))?;
self.builder.build_store(len_ptr, zero_i64)
.map_err(|e| CodegenError::with_span(format!("sprint store len failed: {e}"), expr.span.clone()))?;

let cap_ptr = self.builder.build_struct_gep(buf_writer_llvm_ty, writer_tmp, 3, "sprint.cap")
.map_err(|e| CodegenError::with_span(format!("sprint struct gep 3 failed: {e}"), expr.span.clone()))?;
let cap_ptr = self.builder.build_struct_gep(buf_writer_llvm_ty, writer_tmp, 2, "sprint.cap")
.map_err(|e| CodegenError::with_span(format!("sprint struct gep 2 failed: {e}"), expr.span.clone()))?;
self.builder.build_store(cap_ptr, zero_i64)
.map_err(|e| CodegenError::with_span(format!("sprint store cap failed: {e}"), expr.span.clone()))?;

let fd_ptr = self.builder.build_struct_gep(buf_writer_llvm_ty, writer_tmp, 3, "sprint.fd")
.map_err(|e| CodegenError::with_span(format!("sprint struct gep 3 failed: {e}"), expr.span.clone()))?;
self.builder.build_store(fd_ptr, neg_one_i32)
.map_err(|e| CodegenError::with_span(format!("sprint store fd failed: {e}"), expr.span.clone()))?;

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 Initialize the writer mode The expanded BufWriter has seven fields, but this temporary initializes only fields 0–3. Every @sprint write then reads the uninitialized mode field in ensure_init(). If it reads as IOMODE_UNBUFFERED, the writer sets cap = -1 and drops the output because its fd is -1. Valid @sprint calls can therefore return empty or truncated strings. Initialize mode to a buffered mode when creating this temporary writer.

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

@CierCier
CierCier merged commit fdc8cc9 into main Jul 22, 2026
1 check passed
@CierCier
CierCier deleted the feat/buffer-table-flush-on-exit branch July 22, 2026 04:59
CierCier added a commit that referenced this pull request Aug 1, 2026
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