Repository navigation
feat: Buffer table and SRT flush-on-exit - #13
Merged
Merged
Conversation
- 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
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()))?; |
Contributor
There was a problem hiding this comment.
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
added a commit
that referenced
this pull request
Aug 1, 2026
feat: Buffer table and SRT flush-on-exit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 controllist_next,list_prev,modefields toBufWriterstructIOMODE_UNBUFFERED(0),IOMODE_LINE(1),IOMODE_BLOCK(2)__buf_writer_list— intrusive linked list head for flush-on-exit registrationensure_init: mode-aware; skips buffer allocation in UNBUFFERED mode; registers global streams (fd 1/2) in the linked listwrite(): UNBUFFERED → direct syscall; LINE → flush on any embedded\n; BLOCK → buffer up to capacity, direct-write oversized chunks__silver_flush_all(): iterates the linked list, callsflush()on each registered writerstd/sys/entry.ag— Flush before exitimport std.iofor access to__silver_flush_all_start: saves main's return value in%r12(callee-saved), calls__silver_flush_all, restores value to%rdi, thenexit_groupagc/src/codegen/llvm_ir.rs— Const cast supportIntValue → PointerTypematch arm toconst_cast_value_to_basic_typefor null pointer constants in global initializersVerification
cargo test -p agc: 218/218 passbash tests/run_tests.sh: 26/26 pass@print("no newline here")output now visible before process exit@printlnline-buffered behavior unchanged@eprintunbuffered (appears before stdout) confirmedxblock 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:
@sprintwriter layout handling.Confidence Score: 4/5
The remaining
@sprintinitialization bug should be fixed before merging.@sprintconstructs a seven-field writer but initializes only four fields.modevalue.agc/src/codegen/llvm_ir.rs
Important Files Changed
@sprintlayout and adds null pointer constant casting, but leaves the writer mode uninitialized.mainwhile preserving the process exit value.Reviews (2): Last reviewed commit: "fix: Address Greptile review comments - ..." | Re-trigger Greptile
Context used: