aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-10-20 11:57:16 +0000
committer_ov4 <ov4@ov4.org>2026-10-20 11:57:16 +0000
commit62b605c6c0ea13e9763c0a0115464007b13a8831 (patch)
tree53c27965f5a365682e0d79fd96e25b228ecb9dca /include
parentbfe2e75d7e3b372e55369677028429990c49bc54 (diff)
refactor(strings): replace compiler builtins with the STRING library
Construct literals as STRING values and route string operations through ordinary library functions and operators. Preserve value ownership in records, arrays, constants and file I/O. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'include')
-rw-r--r--include/cps/CodeGen.h16
-rw-r--r--include/cps/FileHandler.h2
-rw-r--r--include/cps/StringConversionHandler.h33
-rw-r--r--include/cps/StringHandler.h42
-rw-r--r--include/cps/TypeSystem.h5
5 files changed, 12 insertions, 86 deletions
diff --git a/include/cps/CodeGen.h b/include/cps/CodeGen.h
index 30f4135..e6281c5 100644
--- a/include/cps/CodeGen.h
+++ b/include/cps/CodeGen.h
@@ -11,8 +11,6 @@
#include "cps/TypeSystem.h"
#include "cps/ArithmeticHandler.h"
-#include "cps/StringHandler.h"
-#include "cps/StringConversionHandler.h"
#include <map>
#include <memory>
@@ -93,6 +91,12 @@ class CodeGen {
bool prepareSymbolDeclaration(const std::string &Name, bool IsConstant = false);
bool requireWritable(const SymbolInfo &Info, const std::string &Name);
void emitConstantStmt(ConstantStmtAST *Stmt);
+ std::map<const ConstantStmtAST *, SymbolInfo> StringConstants;
+ const TypeInfo *requireStringType();
+ llvm::Value *emitStringFromCString(llvm::Value *Text);
+ llvm::Value *emitStringCString(llvm::Value *Value);
+ llvm::Value *emitCStringExpr(ExprAST *Expr);
+ bool containsString(const TypeInfo *Type) const;
std::unique_ptr<TypeSystem> Types;
std::unique_ptr<ArrayHandler> Arrays;
@@ -101,14 +105,11 @@ class CodeGen {
std::unique_ptr<FunctionGen> FuncGen;
std::unique_ptr<ArithmeticHandler> ArithHandler;
- std::unique_ptr<StringHandler> StrHandler;
- std::unique_ptr<StringConversionHandler> StrConvHandler;
std::unique_ptr<FileHandler> Files;
std::unique_ptr<MemoryHandler> Memory;
llvm::FunctionCallee PrintfFunc;
llvm::FunctionCallee ScanfFunc;
- llvm::FunctionCallee MallocFunc;
llvm::Value *PrintfFormatStr; // %lld\n
llvm::Value *PrintfFloatFormatStr; // %f\n
@@ -117,7 +118,6 @@ class CodeGen {
llvm::Value *ScanfFormatStr; // %lld
llvm::Value *ScanfFloatFormatStr; // %lf
- llvm::Value *ScanfStringFormatStr; // %s
llvm::Value *TrueStr; // "TRUE"
llvm::Value *FalseStr; // "FALSE"
@@ -157,7 +157,7 @@ class CodeGen {
// Resolves a variable / array element / field chain / deref to an address
// (emitting index and null-pointer checks along the way).
bool emitLValue(ExprAST *E, LValueInfo &Out);
- // The rvalue read: load + the STRING null->"" guard.
+ // Read a value from resolved storage.
llvm::Value *loadFromLValue(const LValueInfo &LV);
llvm::Value *emitAllocationSize(ExprAST *Expr);
llvm::Value *emitCall(const std::string &Callee,
@@ -209,7 +209,7 @@ public:
llvm::Value *emitExpr(ExprAST *Expr);
void emitStmt(StmtAST *Stmt);
- // STRING slots zero-init to null; normalize null to "" at load/print sites.
+ // Normalize a raw C string pointer to "" when explicitly casting it to STRING.
llvm::Value *emitStringNullGuard(llvm::Value *StrVal);
// Name-level admission gate for Enum/Record/Pointer plus the existing
diff --git a/include/cps/FileHandler.h b/include/cps/FileHandler.h
index 50593de..20ece16 100644
--- a/include/cps/FileHandler.h
+++ b/include/cps/FileHandler.h
@@ -29,7 +29,7 @@ class FileHandler {
// libc — declared lazily on first file use
llvm::FunctionCallee FopenFunc, FcloseFunc, FgetcFunc, UngetcFunc,
FputsFunc, FputcFunc, MallocFunc, ReallocFunc,
- StrcmpFunc;
+ StrcmpFunc, StrlenFunc, MemcpyFunc, FreeFunc;
// open-file table
llvm::GlobalVariable *FileNames = nullptr; // [TableSize x ptr] STRING passed to OPENFILE
diff --git a/include/cps/StringConversionHandler.h b/include/cps/StringConversionHandler.h
deleted file mode 100644
index c4879aa..0000000
--- a/include/cps/StringConversionHandler.h
+++ /dev/null
@@ -1,33 +0,0 @@
-#pragma once
-#include "llvm/IR/IRBuilder.h"
-#include "llvm/IR/LLVMContext.h"
-#include "llvm/IR/Module.h"
-
-namespace cps {
-
-class StringConversionHandler {
- llvm::LLVMContext &Context;
- llvm::IRBuilder<> &Builder;
- llvm::Module &Module;
-
- llvm::FunctionCallee SnprintfFunc;
- llvm::FunctionCallee StrtollFunc;
- llvm::FunctionCallee StrtodFunc;
- llvm::FunctionCallee MallocFunc;
-
-public:
- StringConversionHandler(llvm::LLVMContext &Ctx, llvm::IRBuilder<> &B, llvm::Module &M);
-
- void setupExternalFunctions();
-
- // NUM TO STR(x) RETURNS STRING
- llvm::Value *emitNumToStr(llvm::Value *Num, bool IsReal);
-
- // STR TO NUM(x) RETURNS NUMBER
- llvm::Value *emitStrToNum(llvm::Value *Str, bool AsReal);
-
- // IS NUM(ThisString) RETURNS BOOLEAN
- llvm::Value *emitIsNum(llvm::Value *Str);
-};
-
-} // namespace cps
diff --git a/include/cps/StringHandler.h b/include/cps/StringHandler.h
deleted file mode 100644
index 0b2447c..0000000
--- a/include/cps/StringHandler.h
+++ /dev/null
@@ -1,42 +0,0 @@
-#pragma once
-#include "llvm/IR/IRBuilder.h"
-#include "llvm/IR/LLVMContext.h"
-#include "llvm/IR/Module.h"
-#include <string>
-
-namespace cps {
-
-class StringHandler {
- llvm::LLVMContext &Context;
- llvm::IRBuilder<> &Builder;
- llvm::Module &Module;
-
- llvm::FunctionCallee MallocFunc;
- llvm::FunctionCallee StrLenFunc;
- llvm::FunctionCallee MemCpyFunc;
- llvm::FunctionCallee StrCpyFunc;
- llvm::FunctionCallee StrCatFunc;
- llvm::FunctionCallee StrCmpFunc;
- llvm::FunctionCallee ToUpperFunc;
- llvm::FunctionCallee ToLowerFunc;
-
- llvm::Value *emitCaseConvert(llvm::Value *Str, llvm::FunctionCallee CaseFn);
- llvm::Value *allocCopySubstring(llvm::Value *Src, llvm::Value *StartIdx, llvm::Value *Len);
-
-public:
- StringHandler(llvm::LLVMContext &Ctx, llvm::IRBuilder<> &B, llvm::Module &M);
-
- void setupExternalFunctions();
- llvm::Value *createLiteral(const std::string &Val);
-
- llvm::Value *emitLength(llvm::Value *Str);
- llvm::Value *emitConcat(llvm::Value *LHS, llvm::Value *RHS);
- llvm::Value *emitCompare(int Op, llvm::Value *LHS, llvm::Value *RHS);
- llvm::Value *emitMid(llvm::Value *Str, llvm::Value *Start, llvm::Value *Len);
- llvm::Value *emitRight(llvm::Value *Str, llvm::Value *Len);
- llvm::Value *emitLeft(llvm::Value *Str, llvm::Value *Len);
- llvm::Value *emitLCase(llvm::Value *Str);
- llvm::Value *emitUCase(llvm::Value *Str);
-};
-
-}
diff --git a/include/cps/TypeSystem.h b/include/cps/TypeSystem.h
index 1391345..e4663eb 100644
--- a/include/cps/TypeSystem.h
+++ b/include/cps/TypeSystem.h
@@ -26,7 +26,6 @@ enum class TypeKind {
Integer,
Real,
Boolean,
- String,
Char,
Void,
Enum,
@@ -109,10 +108,12 @@ struct SymbolInfo {
llvm::Value *Storage = nullptr;
std::string TypeName;
bool IsArray = false;
- llvm::Constant *ConstantValue = nullptr; // Non-null for CONSTANT; no storage.
+ llvm::Constant *ConstantValue = nullptr; // Compile-time scalar CONSTANT value, if available.
unsigned ScopeID = 0;
bool IsReference = false; // Storage is a slot containing the current object address.
bool IsGlobal = false;
+ bool ReadOnly = false; // Owned STRING constants use storage instead of ConstantValue.
+ bool isConstant() const { return ConstantValue || ReadOnly; }
};
class TypeSystem {