Skip to content

Commit 2f45421

Browse files
committed
Use str_item and use str_slice appropriately
1 parent 735e85d commit 2f45421

File tree

3 files changed

+20
-24
lines changed

3 files changed

+20
-24
lines changed

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,19 +1056,19 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
10561056
return builder->CreateCall(fn, {str});
10571057
}
10581058

1059-
llvm::Value* lfortran_str_copy(llvm::Value* str, llvm::Value* idx1, llvm::Value* idx2)
1059+
llvm::Value* lfortran_str_item(llvm::Value* str, llvm::Value* idx1)
10601060
{
1061-
std::string runtime_func_name = "_lfortran_str_copy";
1061+
std::string runtime_func_name = "_lfortran_str_item";
10621062
llvm::Function *fn = module->getFunction(runtime_func_name);
10631063
if (!fn) {
10641064
llvm::FunctionType *function_type = llvm::FunctionType::get(
10651065
character_type, {
1066-
character_type, llvm::Type::getInt32Ty(context), llvm::Type::getInt32Ty(context)
1066+
character_type, llvm::Type::getInt32Ty(context)
10671067
}, false);
10681068
fn = llvm::Function::Create(function_type,
10691069
llvm::Function::ExternalLinkage, runtime_func_name, *module);
10701070
}
1071-
return builder->CreateCall(fn, {str, idx1, idx2});
1071+
return builder->CreateCall(fn, {str, idx1});
10721072
}
10731073

10741074
llvm::Value* lfortran_str_slice(llvm::Value* str, llvm::Value* idx1, llvm::Value* idx2,
@@ -1827,7 +1827,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
18271827
std::vector<llvm::Value*> idx_vec = {idx};
18281828
p = CreateGEP(str, idx_vec);
18291829
} else {
1830-
p = lfortran_str_copy(str, idx, idx);
1830+
p = lfortran_str_item(str, idx);
18311831
}
18321832
// TODO: Currently the string starts at the right location, but goes to the end of the original string.
18331833
// We have to allocate a new string, copy it and add null termination.
@@ -1910,7 +1910,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
19101910
// llvm::Value *p = CreateGEP(str, idx_vec);
19111911
// TODO: Currently the string starts at the right location, but goes to the end of the original string.
19121912
// We have to allocate a new string, copy it and add null termination.
1913-
llvm::Value *p = lfortran_str_copy(str, idx1, idx2);
1913+
llvm::Value *step = llvm::ConstantInt::get(context, llvm::APInt(32, 1));
1914+
llvm::Value *present = llvm::ConstantInt::get(context, llvm::APInt(1, 1));
1915+
llvm::Value *p = lfortran_str_slice(str, idx1, idx2, step, present, present);
19141916

19151917
tmp = builder->CreateAlloca(character_type, nullptr);
19161918
builder->CreateStore(p, tmp);
@@ -4784,7 +4786,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
47844786
llvm::Value *idx = tmp;
47854787
this->visit_expr_wrapper(x.m_arg, true);
47864788
llvm::Value *str = tmp;
4787-
tmp = lfortran_str_copy(str, idx, idx);
4789+
tmp = lfortran_str_item(str, idx);
47884790
}
47894791

47904792
void visit_StringSection(const ASR::StringSection_t& x) {

src/libasr/runtime/lfortran_intrinsics.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -849,27 +849,20 @@ LFORTRAN_API char* _lfortran_strrepeat_c(char* s, int32_t n)
849849
return dest_char;
850850
}
851851

852-
// idx1 and idx2 both start from 1
853-
LFORTRAN_API char* _lfortran_str_copy(char* s, int32_t idx1, int32_t idx2) {
852+
// idx starts from 1
853+
LFORTRAN_API char* _lfortran_str_item(char* s, int32_t idx) {
854854

855855
int s_len = strlen(s);
856-
if(idx1 > s_len || idx1 <= (-1*s_len)){
857-
printf("String index out of Bounds\n");
856+
int original_idx = idx - 1;
857+
if (idx < 1) idx += s_len;
858+
if (idx < 1 || idx >= s_len + 1) {
859+
printf("String index: %d is out of Bounds\n", original_idx);
858860
exit(1);
859861
}
860-
if(idx1 <= 0) {
861-
idx1 = s_len + idx1;
862-
}
863-
if(idx2 <= 0) {
864-
idx2 = s_len + idx2;
865-
}
866-
char* dest_char = (char*)malloc(idx2-idx1+2);
867-
for (int i=idx1; i <= idx2; i++)
868-
{
869-
dest_char[i-idx1] = s[i-1];
870-
}
871-
dest_char[idx2-idx1+1] = '\0';
872-
return dest_char;
862+
char* res = (char*)malloc(2);
863+
res[0] = s[idx-1];
864+
res[1] = '\0';
865+
return res;
873866
}
874867

875868
LFORTRAN_API char* _lfortran_str_slice(char* s, int32_t idx1, int32_t idx2, int32_t step,

src/libasr/runtime/lfortran_intrinsics.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ LFORTRAN_API int8_t* _lfortran_realloc(int8_t* ptr, int32_t size);
168168
LFORTRAN_API int8_t* _lfortran_calloc(int32_t count, int32_t size);
169169
LFORTRAN_API void _lfortran_free(char* ptr);
170170
LFORTRAN_API void _lfortran_string_init(int size_plus_one, char *s);
171+
LFORTRAN_API char* _lfortran_str_item(char* s, int32_t idx);
171172
LFORTRAN_API char* _lfortran_str_slice(char* s, int32_t idx1, int32_t idx2, int32_t step,
172173
bool idx1_present, bool idx2_present);
173174
LFORTRAN_API int32_t _lfortran_iand32(int32_t x, int32_t y);

0 commit comments

Comments
 (0)