From 0881f6881161fc30953b872ae108e5a8d359cb5c Mon Sep 17 00:00:00 2001 From: _ov4 Date: Thu, 22 Oct 2026 03:33:22 +0000 Subject: refactor(codegen): share the READ-mode check of file read and EOF helpers __cps_file_read and __cps_file_eof both emitted the same mode load, comparison and ErrNotRead check after the slot lookup; emit it through FileHandler::emitRequireReadMode instead. Co-Authored-By: Claude Opus 5.5 --- lib/CodeGen/FileHandler.cc | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'lib/CodeGen/FileHandler.cc') diff --git a/lib/CodeGen/FileHandler.cc b/lib/CodeGen/FileHandler.cc index a2c3492..b471aa5 100644 --- a/lib/CodeGen/FileHandler.cc +++ b/lib/CodeGen/FileHandler.cc @@ -121,6 +121,14 @@ Value *FileHandler::emitLookupOrFail(Value *Name, Value *Line) { return Idx; } +// Report and throw unless the open file in slot Idx was opened for READ. +void FileHandler::emitRequireReadMode(Value *Idx, Value *Name, Value *Line) { + Type *I8 = Type::getInt8Ty(Context); + Value *Mode = Builder.CreateLoad(I8, emitSlotGEP(FileModes, I8, Idx), "mode"); + Value *NotRead = Builder.CreateICmpNE(Mode, ConstantInt::get(I8, uint8_t(FileMode::Read)), "not_read"); + Checker.emitCheck(NotRead, ErrNotRead, {Line, Name}); +} + // i64 __cps_file_find(ptr name): strcmp scan over occupied slots; -1 on miss. void FileHandler::buildFindFn() { PointerType *PtrTy = PointerType::getUnqual(Context); -- cgit v1.3.1-4-g156e