From 09e128ce98de793d22669255897e4b4816d0e53e Mon Sep 17 00:00:00 2001 From: _ov4 Date: Mon, 2 Feb 2026 09:02:39 +0000 Subject: Added logical operators --- lib/CodeGen/ArithmeticHandler.cc | 20 +++++++++++++++++++- lib/CodeGen/CodeGen.cc | 20 ++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) (limited to 'lib/CodeGen') diff --git a/lib/CodeGen/ArithmeticHandler.cc b/lib/CodeGen/ArithmeticHandler.cc index 762f0a3..df83513 100644 --- a/lib/CodeGen/ArithmeticHandler.cc +++ b/lib/CodeGen/ArithmeticHandler.cc @@ -1,4 +1,5 @@ #include "cps/ArithmeticHandler.h" +#include "cps/Lexer.h" #include using namespace llvm; @@ -20,7 +21,6 @@ Value *ArithmeticHandler::emitBinaryOp(int Op, Value *LHS, Value *RHS, int Line) return Builder.CreateFDiv(LVal, RVal, "divtmp"); } - // DIV / MOD : Only Integer. if (Op == tok_div || Op == tok_mod) { if (!LIsInt || !RIsInt) { fprintf(stderr, "Error: DIV and MOD operators require INTEGER operands.\n"); @@ -29,6 +29,24 @@ Value *ArithmeticHandler::emitBinaryOp(int Op, Value *LHS, Value *RHS, int Line) if (Op == tok_div) return Builder.CreateSDiv(LHS, RHS, "div_int_tmp"); if (Op == tok_mod) return Builder.CreateSRem(LHS, RHS, "mod_tmp"); } + + if (Op == tok_and || Op == tok_or) { + Value *L = LHS; + Value *R = RHS; + + if (L->getType()->isIntegerTy(64)) + L = Builder.CreateICmpNE(L, ConstantInt::get(Context, APInt(64, 0)), "tobool"); + if (R->getType()->isIntegerTy(64)) + R = Builder.CreateICmpNE(R, ConstantInt::get(Context, APInt(64, 0)), "tobool"); + + if (L->getType()->isDoubleTy()) + L = Builder.CreateFCmpONE(L, ConstantFP::get(Context, APFloat(0.0)), "tobool"); + if (R->getType()->isDoubleTy()) + R = Builder.CreateFCmpONE(R, ConstantFP::get(Context, APFloat(0.0)), "tobool"); + + if (Op == tok_and) return Builder.CreateAnd(L, R, "andtmp"); + if (Op == tok_or) return Builder.CreateOr(L, R, "ortmp"); + } if (LIsDouble || RIsDouble) { Value *LVal = LHS; diff --git a/lib/CodeGen/CodeGen.cc b/lib/CodeGen/CodeGen.cc index da6b005..95e3768 100644 --- a/lib/CodeGen/CodeGen.cc +++ b/lib/CodeGen/CodeGen.cc @@ -105,6 +105,21 @@ Value *CodeGen::emitExpr(ExprAST *Expr) { return Arrays->emitArrayAccess(ArrAcc, *this); } + if (auto *Unary = dynamic_cast(Expr)) { + Value *Operand = emitExpr(Unary->getOperand()); + if (!Operand) return nullptr; + + if (Unary->getOp() == tok_not) { + if (Operand->getType()->isIntegerTy(64)) { + Operand = Builder->CreateICmpNE(Operand, ConstantInt::get(*TheContext, APInt(64, 0)), "tobool"); + } else if (Operand->getType()->isDoubleTy()) { + Operand = Builder->CreateFCmpONE(Operand, ConstantFP::get(*TheContext, APFloat(0.0)), "tobool"); + } + + return Builder->CreateNot(Operand, "nottmp"); + } + } + if (auto *Bin = dynamic_cast(Expr)) { Value *L = emitExpr(Bin->getLHS()); Value *R = emitExpr(Bin->getRHS()); @@ -385,6 +400,11 @@ void CodeGen::emitStmt(StmtAST *Stmt) { if (AI->getAllocatedType() == Type::getDoubleTy(*TheContext) && Val->getType()->isIntegerTy(64)) { Val = Builder->CreateSIToFP(Val, Type::getDoubleTy(*TheContext)); } + + if (AI->getAllocatedType() == Type::getInt64Ty(*TheContext) && Val->getType()->isIntegerTy(1)) { + Val = Builder->CreateZExt(Val, Type::getInt64Ty(*TheContext)); + } + Builder->CreateStore(Val, Alloca); } } -- cgit v1.3.1-4-g156e