@@ -756,6 +756,20 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
756
756
return builder->CreateCall (fn, {str});
757
757
}
758
758
759
+ llvm::Value* lfortran_str_copy (llvm::Value* str, llvm::Value* idx1, llvm::Value* idx2)
760
+ {
761
+ std::string runtime_func_name = " _lfortran_str_copy" ;
762
+ llvm::Function *fn = module->getFunction (runtime_func_name);
763
+ if (!fn) {
764
+ llvm::FunctionType *function_type = llvm::FunctionType::get (
765
+ character_type, {
766
+ character_type, llvm::Type::getInt32Ty (context), llvm::Type::getInt32Ty (context)
767
+ }, false );
768
+ fn = llvm::Function::Create (function_type,
769
+ llvm::Function::ExternalLinkage, runtime_func_name, *module);
770
+ }
771
+ return builder->CreateCall (fn, {str, idx1, idx2});
772
+ }
759
773
760
774
// This function is called as:
761
775
// float complex_re(complex a)
@@ -1066,13 +1080,14 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
1066
1080
// Use the "right" index for now
1067
1081
this ->visit_expr_wrapper (x.m_args [0 ].m_right , true );
1068
1082
llvm::Value *idx = tmp;
1069
- idx = builder->CreateSub (idx, llvm::ConstantInt::get (context, llvm::APInt (32 , 1 )));
1083
+ // idx = builder->CreateSub(idx, llvm::ConstantInt::get(context, llvm::APInt(32, 1)));
1070
1084
// std::vector<llvm::Value*> idx_vec = {llvm::ConstantInt::get(context, llvm::APInt(32, 0)), idx};
1071
- std::vector<llvm::Value*> idx_vec = {idx};
1085
+ // std::vector<llvm::Value*> idx_vec = {idx};
1072
1086
llvm::Value *str = CreateLoad (array);
1073
- llvm::Value *p = CreateGEP (str, idx_vec);
1087
+ // llvm::Value *p = CreateGEP(str, idx_vec);
1074
1088
// TODO: Currently the string starts at the right location, but goes to the end of the original string.
1075
1089
// We have to allocate a new string, copy it and add null termination.
1090
+ llvm::Value *p = lfortran_str_copy (str, idx, idx);
1076
1091
1077
1092
tmp = builder->CreateAlloca (character_type, nullptr );
1078
1093
builder->CreateStore (p, tmp);
0 commit comments