Skip to content

Commit fbd2e80

Browse files
Merge pull request #1093 from Thirumalai-Shaktivel/out_of_range
2 parents a025221 + fb13fc0 commit fbd2e80

23 files changed

+229
-65
lines changed

src/bin/lpython.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -927,8 +927,8 @@ int link_executable(const std::vector<std::string> &infiles,
927927
std::cout << "The command '" + cmd + "' failed." << std::endl;
928928
return 10;
929929
}
930-
if (outfile == "a.out" && compiler_options.arg_o == "") {
931-
err = system("a.out");
930+
if (compiler_options.arg_o == "") {
931+
err = system(outfile.c_str());
932932
if (err != 0) {
933933
if (0 < err && err < 256) {
934934
return err;
@@ -962,8 +962,8 @@ int link_executable(const std::vector<std::string> &infiles,
962962
std::cout << "The command '" + cmd + "' failed." << std::endl;
963963
return 10;
964964
}
965-
if (outfile == "a.out" && compiler_options.arg_o == "") {
966-
err = system("./a.out");
965+
if (compiler_options.arg_o == "") {
966+
err = system(("./" + outfile).c_str());
967967
if (err != 0) {
968968
if (0 < err && err < 256) {
969969
return err;
@@ -1282,6 +1282,7 @@ int main(int argc, char *argv[])
12821282
app.add_flag("--no-warnings", compiler_options.no_warnings, "Turn off all warnings");
12831283
app.add_flag("--no-error-banner", compiler_options.no_error_banner, "Turn off error banner");
12841284
app.add_option("--backend", arg_backend, "Select a backend (llvm, cpp, x86, wasm, wasm_x86)")->capture_default_str();
1285+
app.add_flag("--enable-bounds-checking", compiler_options.enable_bounds_checking, "Turn on index bounds checking");
12851286
app.add_flag("--openmp", compiler_options.openmp, "Enable openmp");
12861287
app.add_flag("--fast", compiler_options.fast, "Best performance (disable strict standard compliance)");
12871288
app.add_option("--target", compiler_options.target, "Generate code for the given target")->capture_default_str();
@@ -1292,10 +1293,6 @@ int main(int argc, char *argv[])
12921293
app.add_flag("--show-errors", show_errors, "Show errors when LSP is running in the background");
12931294
app.add_flag("--show-document-symbols", show_document_symbols, "Show symbols in lpython file");
12941295

1295-
if( compiler_options.fast ) {
1296-
lpython_pass_manager.use_optimization_passes();
1297-
}
1298-
12991296
/*
13001297
* Subcommands:
13011298
*/
@@ -1328,6 +1325,16 @@ int main(int argc, char *argv[])
13281325
app.require_subcommand(0, 1);
13291326
CLI11_PARSE(app, argc, argv);
13301327

1328+
if( compiler_options.fast && compiler_options.enable_bounds_checking ) {
1329+
// ReleaseSafe Mode
1330+
} else if ( compiler_options.fast ) {
1331+
// Release Mode
1332+
lpython_pass_manager.use_optimization_passes();
1333+
} else {
1334+
// Debug Mode
1335+
compiler_options.enable_bounds_checking = true;
1336+
}
1337+
13311338
if (arg_version) {
13321339
std::string version = LFORTRAN_VERSION;
13331340
std::cout << "LPython version: " << version << std::endl;
@@ -1434,7 +1441,7 @@ int main(int argc, char *argv[])
14341441
} else if (show_wat) {
14351442
outfile = basename + ".wat";
14361443
} else {
1437-
outfile = "a.out";
1444+
outfile = basename + ".out";
14381445
}
14391446

14401447
// if (arg_E) {

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
164164
bool emit_debug_info;
165165
std::string infile;
166166
bool emit_debug_line_column;
167+
bool enable_bounds_checking;
167168
Allocator &al;
168169

169170
llvm::Value *tmp;
@@ -242,14 +243,15 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
242243

243244
ASRToLLVMVisitor(Allocator &al, llvm::LLVMContext &context, Platform platform,
244245
bool emit_debug_info, std::string infile, bool emit_debug_line_column,
245-
diag::Diagnostics &diagnostics) :
246+
bool enable_bounds_checking, diag::Diagnostics &diagnostics) :
246247
diag{diagnostics},
247248
context(context),
248249
builder(std::make_unique<llvm::IRBuilder<>>(context)),
249250
platform{platform},
250251
emit_debug_info{emit_debug_info},
251252
infile{infile},
252253
emit_debug_line_column{emit_debug_line_column},
254+
enable_bounds_checking{enable_bounds_checking},
253255
al{al},
254256
prototype_only(false),
255257
llvm_utils(std::make_unique<LLVMUtils>(context, builder.get())),
@@ -1381,7 +1383,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
13811383
this->visit_expr(*x.m_args[i]);
13821384
llvm::Value* item = tmp;
13831385
llvm::Value* pos = llvm::ConstantInt::get(context, llvm::APInt(32, i));
1384-
list_api->write_item(const_list, pos, item, list_type->m_type, *module);
1386+
list_api->write_item(const_list, pos, item, list_type->m_type, false, *module);
13851387
}
13861388
ptr_loads = ptr_loads_copy;
13871389
tmp = const_list;
@@ -1521,9 +1523,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
15211523
ptr_loads = ptr_loads_copy;
15221524
llvm::Value *pos = tmp;
15231525

1524-
tmp = list_api->read_item(plist, pos,
1525-
(LLVM::is_llvm_struct(el_type) ||
1526-
ptr_loads == 0));
1526+
tmp = list_api->read_item(plist, pos, enable_bounds_checking, *module,
1527+
(LLVM::is_llvm_struct(el_type) || ptr_loads == 0));
15271528
}
15281529

15291530
void visit_DictItem(const ASR::DictItem_t& x) {
@@ -3916,7 +3917,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
39163917
llvm::Value* list = tmp;
39173918
this->visit_expr_wrapper(asr_target0->m_pos, true);
39183919
llvm::Value* pos = tmp;
3919-
target = list_api->read_item(list, pos, true);
3920+
3921+
target = list_api->read_item(list, pos, enable_bounds_checking,
3922+
*module, true);
39203923
}
39213924
} else {
39223925
ASR::Variable_t *asr_target = EXPR2VAR(x.m_target);
@@ -6399,7 +6402,7 @@ Result<std::unique_ptr<LLVMModule>> asr_to_llvm(ASR::TranslationUnit_t &asr,
63996402
context.setOpaquePointers(false);
64006403
#endif
64016404
ASRToLLVMVisitor v(al, context, co.platform, co.emit_debug_info, infile,
6402-
co.emit_debug_line_column, diagnostics);
6405+
co.emit_debug_line_column, co.enable_bounds_checking, diagnostics);
64036406
LCompilers::PassOptions pass_options;
64046407
pass_options.run_fun = run_fn;
64056408
pass_options.always_run = false;

0 commit comments

Comments
 (0)