Skip to content

Commit 7d763bc

Browse files
committed
LLVM: get string(a:b) working for "b" an expr
1 parent f8ced2b commit 7d763bc

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,20 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
10241024
throw CodeGenError("Only string(a:b) for a==b supported for now.", x.base.base.loc);
10251025
}
10261026
} else {
1027-
throw CodeGenError("Only string(a:b) for a,b variables for now.", x.base.base.loc);
1027+
//throw CodeGenError("Only string(a:b) for a,b variables for now.", x.base.base.loc);
1028+
// Use the "right" index for now
1029+
this->visit_expr_wrapper(x.m_args[0].m_right, true);
1030+
llvm::Value *idx = tmp;
1031+
idx = builder->CreateSub(idx, llvm::ConstantInt::get(context, llvm::APInt(32, 1)));
1032+
//std::vector<llvm::Value*> idx_vec = {llvm::ConstantInt::get(context, llvm::APInt(32, 0)), idx};
1033+
std::vector<llvm::Value*> idx_vec = {idx};
1034+
llvm::Value *str = builder->CreateLoad(array);
1035+
llvm::Value *p = builder->CreateGEP(str, idx_vec);
1036+
// TODO: Currently the string starts at the right location, but goes to the end of the original string.
1037+
// We have to allocate a new string, copy it and add null termination.
1038+
1039+
tmp = builder->CreateAlloca(character_type, nullptr);
1040+
builder->CreateStore(p, tmp);
10281041
}
10291042
} else {
10301043
throw CodeGenError("Only string(a:b) supported for now.", x.base.base.loc);

0 commit comments

Comments
 (0)