Issue |
120140
|
Summary |
Assertion `i < NumContainedTys && "Index out of range!"' failed.
|
Labels |
new issue
|
Assignees |
|
Reporter |
wesuRage
|
`/usr/local/llvm-19/include/llvm/IR/Type.h:385: Type *llvm::Type::getContainedType(unsigned int) const: Assertion `i < NumContainedTys && "Index out of range!"' failed.
Aborted (core dumped)`
This shows up when I try to get the type contained in the pointer. Its used in the line:
```cpp
llvm::Type *ElementType = llvm::cast<llvm::PointerType>(SymbolValue->getType())->getContainedType(0);
```
line:
```cpp
llvm::Type *ElementType = llvm::cast<llvm::PointerType>(LType)->getContainedType(0);
```
and line:
```cpp
llvm::Type *ElementType = llvm::cast<llvm::PointerType>(RType)->getContainedType(0);
```
This is the full code for more context:
```cpp
#include "backend/generator/expressions/generate_binary_expr.hpp"
#include "backend/generator/expressions/generate_expr.hpp"
#include "backend/generator/symbols/identifier_symbol_table.hpp"
llvm::Value *generate_binary_expr(BinaryExprNode *node, llvm::LLVMContext &Context, llvm::IRBuilder<> &Builder, llvm::Module &Module) {
llvm::Value *L = generate_expr(node->left, Context, Builder, Module);
llvm::Value *R = generate_expr(node->right, Context, Builder, Module);
llvm::Type *LType = L->getType();
llvm::Type *RType = R->getType();
auto resolve_pointer = [&](llvm::Value *Val, llvm::Type *&Type, const std::string &name) -> llvm::Value * {
if (Type->isPointerTy()) {
llvm::Value *SymbolValue = find_identifier(name);
if (!SymbolValue) {
throw std::runtime_error("Symbol '" + name + "' not found in the symbol table.");
}
llvm::Type *ElementType = llvm::cast<llvm::PointerType>(SymbolValue->getType())->getContainedType(0);
if (!ElementType) {
throw std::runtime_error("Unable to determine the element type of the pointer for '" + name + "'.");
}
Val = Builder.CreateLoad(ElementType, Val, "load_" + name);
Type = ElementType;
}
return Val;
};
if (node->left->kind == NODE_IDENTIFIER) {
auto *LNameNode = static_cast<IdentifierNode *>(node->left->data);
L = resolve_pointer(L, LType, LNameNode->symbol);
}
if (node->right->kind == NODE_IDENTIFIER) {
auto *RNameNode = static_cast<IdentifierNode *>(node->right->data);
R = resolve_pointer(R, RType, RNameNode->symbol);
}
if (LType->isPointerTy()) {
llvm::Type *ElementType = llvm::cast<llvm::PointerType>(LType)->getContainedType(0);
if (!ElementType) {
throw std::runtime_error("Unable to determine the element type of the pointer.");
}
if (ElementType->isIntegerTy()) {
L = Builder.CreatePtrToInt(L, llvm::Type::getInt32Ty(Context), "ptr_to_int_L");
LType = llvm::Type::getInt32Ty(Context);
} else if (ElementType->isFloatingPointTy()) {
L = Builder.CreatePtrToInt(L, llvm::Type::getInt32Ty(Context), "ptr_to_int_L");
L = Builder.CreateSIToFP(L, llvm::Type::getDoubleTy(Context), "int_to_fp_L");
LType = llvm::Type::getDoubleTy(Context);
}
}
if (RType->isPointerTy()) {
llvm::Type *ElementType = llvm::cast<llvm::PointerType>(RType)->getContainedType(0);
if (!ElementType) {
throw std::runtime_error("Unable to determine the element type of the pointer.");
}
if (ElementType->isIntegerTy()) {
L = Builder.CreatePtrToInt(R, llvm::Type::getInt32Ty(Context), "ptr_to_int_L");
LType = llvm::Type::getInt32Ty(Context);
} else if (ElementType->isFloatingPointTy()) {
R = Builder.CreatePtrToInt(R, llvm::Type::getInt32Ty(Context), "ptr_to_int_L");
R = Builder.CreateSIToFP(R, llvm::Type::getDoubleTy(Context), "int_to_fp_L");
RType = llvm::Type::getDoubleTy(Context);
}
}
bool isLInteger = LType->isIntegerTy();
bool isRInteger = RType->isIntegerTy();
bool isLFloating = LType->isFloatingPointTy();
bool isRFloating = RType->isFloatingPointTy();
// Handle integer-specific operations
if (isLInteger && isRInteger) {
if (strcmp(node->op, "+") == 0) return Builder.CreateAdd(L, R, "addtmp");
if (strcmp(node->op, "-") == 0) return Builder.CreateSub(L, R, "subtmp");
if (strcmp(node->op, "*") == 0) return Builder.CreateMul(L, R, "multmp");
if (strcmp(node->op, "/") == 0) return Builder.CreateSDiv(L, R, "divtmp");
if (strcmp(node->op, "%") == 0) return Builder.CreateSRem(L, R, "modtmp");
if (strcmp(node->op, "&") == 0) return Builder.CreateAnd(L, R, "andtmp");
if (strcmp(node->op, "|") == 0) return Builder.CreateOr(L, R, "ortmp");
if (strcmp(node->op, "^") == 0) return Builder.CreateXor(L, R, "xortmp");
if (strcmp(node->op, ">>") == 0) return Builder.CreateAShr(L, R, "shrtmp");
if (strcmp(node->op, "<<") == 0) return Builder.CreateShl(L, R, "shltmp");
// Comparison operators
if (strcmp(node->op, "==") == 0) return Builder.CreateICmpEQ(L, R, "eqtmp");
if (strcmp(node->op, "!=") == 0) return Builder.CreateICmpNE(L, R, "netmp");
if (strcmp(node->op, "<") == 0) return Builder.CreateICmpSLT(L, R, "lttmp");
if (strcmp(node->op, "<=") == 0) return Builder.CreateICmpSLE(L, R, "letmp");
if (strcmp(node->op, ">") == 0) return Builder.CreateICmpSGT(L, R, "gttmp");
if (strcmp(node->op, ">=") == 0) return Builder.CreateICmpSGE(L, R, "getmp");
throw std::runtime_error("Unknown binary operator for integers");
}
// Handle floating-point operations
if (isLFloating || isRFloating) {
// Convert integers to floating-point if necessary
if (isLInteger) {
L = Builder.CreateSIToFP(L, llvm::Type::getDoubleTy(Context), "cast_to_fp_L");
}
if (isRInteger) {
R = Builder.CreateSIToFP(R, llvm::Type::getDoubleTy(Context), "cast_to_fp_R");
}
// Floating-point operations
if (strcmp(node->op, "+") == 0) return Builder.CreateFAdd(L, R, "addtmp");
if (strcmp(node->op, "-") == 0) return Builder.CreateFSub(L, R, "subtmp");
if (strcmp(node->op, "*") == 0) return Builder.CreateFMul(L, R, "multmp");
if (strcmp(node->op, "/") == 0) return Builder.CreateFDiv(L, R, "divtmp");
// Comparison operators for floating-point
if (strcmp(node->op, "==") == 0) return Builder.CreateFCmpOEQ(L, R, "eqtmp");
if (strcmp(node->op, "!=") == 0) return Builder.CreateFCmpONE(L, R, "netmp");
if (strcmp(node->op, "<") == 0) return Builder.CreateFCmpOLT(L, R, "lttmp");
if (strcmp(node->op, "<=") == 0) return Builder.CreateFCmpOLE(L, R, "letmp");
if (strcmp(node->op, ">") == 0) return Builder.CreateFCmpOGT(L, R, "gttmp");
if (strcmp(node->op, ">=") == 0) return Builder.CreateFCmpOGE(L, R, "getmp");
throw std::runtime_error("Unsupported operator for floating-point numbers");
}
throw std::runtime_error("Unsupported types for binary operation");
}
```
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs