diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-22 03:33:22 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-22 03:58:36 +0000 |
| commit | 0881f6881161fc30953b872ae108e5a8d359cb5c (patch) | |
| tree | 8f157814db90683fb390ea767b4f3c24e769fd7b /lib | |
| parent | a84128bbc1b69ab5b763d2c607b6f9f1c667802b (diff) | |
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 <noreply@anthropic.com>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/CodeGen/FileHandler.cc | 8 | ||||
| -rw-r--r-- | lib/CodeGen/FileRuntimeIO.cc | 11 |
2 files changed, 11 insertions, 8 deletions
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); diff --git a/lib/CodeGen/FileRuntimeIO.cc b/lib/CodeGen/FileRuntimeIO.cc index c23515d..c899528 100644 --- a/lib/CodeGen/FileRuntimeIO.cc +++ b/lib/CodeGen/FileRuntimeIO.cc @@ -25,9 +25,7 @@ void FileHandler::buildReadFn() { Line->setName("line"); Value *Idx = emitLookupOrFail(Name, Line); - 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}); + emitRequireReadMode(Idx, Name, Line); Value *Handle = Builder.CreateLoad(PtrTy, emitSlotGEP(FileHandles, PtrTy, Idx), "handle"); Value *MinusOne = ConstantInt::get(Context, APInt(32, -1, /*isSigned=*/true)); @@ -166,9 +164,8 @@ void FileHandler::buildCloseFn() { void FileHandler::buildEofFn() { PointerType *PtrTy = PointerType::getUnqual(Context); Type *I32 = Type::getInt32Ty(Context); - Type *I8 = Type::getInt8Ty(Context); - EofFn = createHelper("__cps_file_eof", + EofFn =createHelper("__cps_file_eof", FunctionType::get(Type::getInt1Ty(Context), {PtrTy, I32}, false)); Value *Name = EofFn->getArg(0); Value *Line = EofFn->getArg(1); @@ -176,9 +173,7 @@ void FileHandler::buildEofFn() { Line->setName("line"); Value *Idx = emitLookupOrFail(Name, Line); - 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}); + emitRequireReadMode(Idx, Name, Line); Value *Handle = Builder.CreateLoad(PtrTy, emitSlotGEP(FileHandles, PtrTy, Idx), "handle"); Value *Ch = Builder.CreateCall(FgetcFunc, {Handle}, "c"); |
