Skip to content

Commit

Permalink
Fix list.pop() for negative arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
faze-geek authored and certik committed Feb 9, 2024
1 parent 77e3c62 commit 3438be7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions integration_tests/test_list_pop.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def test_list_pop():
l1: list[i32]
l2: list[tuple[i32, f64]]
l3: list[list[str]]
l4: list[i32]
i: i32
j: i32
total: i32
Expand Down Expand Up @@ -45,6 +46,10 @@ def test_list_pop():
assert l1.pop(len(l1) - 1) == 4
assert l1 == [1, 2]

l4 = [1, 2, 3, 4, 5]
assert l4.pop(-5) == 1
assert l4.pop(-1) == 5

total = 10
l1 = []
for i in range(total):
Expand Down
18 changes: 17 additions & 1 deletion src/libasr/codegen/llvm_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4659,10 +4659,26 @@ namespace LCompilers {

llvm::Value* end_point_ptr = get_pointer_to_current_end_point(list);
llvm::Value* end_point = LLVM::CreateLoad(*builder, end_point_ptr);
llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::getInt32Ty(context),
llvm::APInt(32, 0));
llvm::Value* adjusted_pos = pos;
llvm::Value* is_negative = builder->CreateICmpSLT(pos, zero);
llvm::Value* abs_pos = builder->CreateSelect(is_negative,
builder->CreateNeg(pos),
pos
);
llvm::Value* is_negative_and_in_bounds = builder->CreateAnd(is_negative,
builder->CreateICmpULE(abs_pos, end_point)
);

// If the index is negative, convert it to the corresponding positive index
adjusted_pos = builder->CreateSelect(is_negative_and_in_bounds,
builder->CreateAdd(end_point, pos),
adjusted_pos
);
llvm::AllocaInst *pos_ptr = builder0.CreateAlloca(
llvm::Type::getInt32Ty(context), nullptr);
LLVM::CreateStore(*builder, pos, pos_ptr);
LLVM::CreateStore(*builder, adjusted_pos, pos_ptr);
llvm::Value* tmp = nullptr;

// Get element to return
Expand Down

0 comments on commit 3438be7

Please sign in to comment.