Skip to content

Commit c792719

Browse files
authored
Merge pull request #1580 from Smit-create/i-1575-1
Support `fast` option in C
2 parents 02aa0d3 + b89fadf commit c792719

File tree

6 files changed

+78
-51
lines changed

6 files changed

+78
-51
lines changed

src/bin/lpython.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,7 @@ int emit_cpp(const std::string &infile,
250250
LCompilers::ASR::TranslationUnit_t* asr = r1.result;
251251

252252
diagnostics.diagnostics.clear();
253-
auto res = LCompilers::asr_to_cpp(al, *asr, diagnostics,
254-
compiler_options.platform, 0);
253+
auto res = LCompilers::asr_to_cpp(al, *asr, diagnostics, compiler_options, 0);
255254
std::cerr << diagnostics.render(lm, compiler_options);
256255
if (!res.ok) {
257256
LCOMPILERS_ASSERT(diagnostics.has_error())
@@ -295,8 +294,7 @@ int emit_c(const std::string &infile,
295294
LCompilers::ASR::TranslationUnit_t* asr = r1.result;
296295

297296
diagnostics.diagnostics.clear();
298-
auto res = LCompilers::asr_to_c(al, *asr, diagnostics,
299-
compiler_options.platform, 0);
297+
auto res = LCompilers::asr_to_c(al, *asr, diagnostics, compiler_options, 0);
300298
std::cerr << diagnostics.render(lm, compiler_options);
301299
if (!res.ok) {
302300
LCOMPILERS_ASSERT(diagnostics.has_error())
@@ -1250,7 +1248,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_cpp_from_source(char *input) {
12501248
out = diagnostics.render(lm, compiler_options);
12511249
if (asr.ok) {
12521250
auto res = LCompilers::asr_to_cpp(al, *asr.result, diagnostics,
1253-
compiler_options.platform, 0);
1251+
compiler_options, 0);
12541252
out = diagnostics.render(lm, compiler_options);
12551253
if (res.ok) {
12561254
out += res.result;

src/libasr/codegen/asr_to_c.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
#include <map>
1818
#include <utility>
1919

20+
#define CHECK_FAST_C(compiler_options, x) \
21+
if (compiler_options.fast && x.m_value != nullptr) { \
22+
visit_expr(*x.m_value); \
23+
return; \
24+
} \
2025

2126
namespace LCompilers {
2227

@@ -30,9 +35,9 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
3035

3136
int counter;
3237

33-
ASRToCVisitor(diag::Diagnostics &diag, Platform &platform,
38+
ASRToCVisitor(diag::Diagnostics &diag, CompilerOptions &co,
3439
int64_t default_lower_bound)
35-
: BaseCCPPVisitor(diag, platform, false, false, true, default_lower_bound),
40+
: BaseCCPPVisitor(diag, co.platform, co, false, false, true, default_lower_bound),
3641
array_types_decls(std::string("\nstruct dimension_descriptor\n"
3742
"{\n int32_t lower_bound, length;\n};\n")),
3843
c_utils_functions{std::make_unique<CUtils::CUtilFunctions>()},
@@ -970,15 +975,18 @@ R"(
970975
}
971976

972977
void visit_EnumStaticMember(const ASR::EnumStaticMember_t& x) {
978+
CHECK_FAST_C(compiler_options, x)
973979
ASR::Variable_t* enum_var = ASR::down_cast<ASR::Variable_t>(x.m_m);
974980
src = std::string(enum_var->m_name);
975981
}
976982

977983
void visit_EnumValue(const ASR::EnumValue_t& x) {
984+
CHECK_FAST_C(compiler_options, x)
978985
visit_expr(*x.m_v);
979986
}
980987

981988
void visit_EnumName(const ASR::EnumName_t& x) {
989+
CHECK_FAST_C(compiler_options, x)
982990
int64_t min_value = INT64_MAX;
983991
ASR::Enum_t* enum_t = ASR::down_cast<ASR::Enum_t>(x.m_enum_type);
984992
ASR::EnumType_t* enum_type = ASR::down_cast<ASR::EnumType_t>(enum_t->m_enum_type);
@@ -1127,6 +1135,7 @@ R"(
11271135
}
11281136

11291137
void visit_ArraySize(const ASR::ArraySize_t& x) {
1138+
CHECK_FAST_C(compiler_options, x)
11301139
visit_expr(*x.m_v);
11311140
std::string var_name = src;
11321141
std::string args = "";
@@ -1144,6 +1153,7 @@ R"(
11441153
}
11451154

11461155
void visit_ArrayReshape(const ASR::ArrayReshape_t& x) {
1156+
CHECK_FAST_C(compiler_options, x)
11471157
visit_expr(*x.m_array);
11481158
std::string array = src;
11491159
visit_expr(*x.m_shape);
@@ -1166,6 +1176,7 @@ R"(
11661176
}
11671177

11681178
void visit_ArrayBound(const ASR::ArrayBound_t& x) {
1179+
CHECK_FAST_C(compiler_options, x)
11691180
visit_expr(*x.m_v);
11701181
std::string var_name = src;
11711182
std::string args = "";
@@ -1203,6 +1214,7 @@ R"(
12031214
}
12041215

12051216
void visit_ArrayItem(const ASR::ArrayItem_t &x) {
1217+
CHECK_FAST_C(compiler_options, x)
12061218
this->visit_expr(*x.m_v);
12071219
std::string array = src;
12081220
std::string out = array;
@@ -1253,6 +1265,7 @@ R"(
12531265
}
12541266

12551267
void visit_StringItem(const ASR::StringItem_t& x) {
1268+
CHECK_FAST_C(compiler_options, x)
12561269
this->visit_expr(*x.m_idx);
12571270
std::string idx = std::move(src);
12581271
this->visit_expr(*x.m_arg);
@@ -1261,6 +1274,7 @@ R"(
12611274
}
12621275

12631276
void visit_StringLen(const ASR::StringLen_t &x) {
1277+
CHECK_FAST_C(compiler_options, x)
12641278
this->visit_expr(*x.m_arg);
12651279
src = "strlen(" + src + ")";
12661280
}
@@ -1276,7 +1290,7 @@ R"(
12761290
};
12771291

12781292
Result<std::string> asr_to_c(Allocator &al, ASR::TranslationUnit_t &asr,
1279-
diag::Diagnostics &diagnostics, Platform &platform,
1293+
diag::Diagnostics &diagnostics, CompilerOptions &co,
12801294
int64_t default_lower_bound)
12811295
{
12821296

@@ -1286,7 +1300,7 @@ Result<std::string> asr_to_c(Allocator &al, ASR::TranslationUnit_t &asr,
12861300
pass_replace_array_op(al, asr, pass_options);
12871301
pass_unused_functions(al, asr, pass_options);
12881302
pass_replace_class_constructor(al, asr, pass_options);
1289-
ASRToCVisitor v(diagnostics, platform, default_lower_bound);
1303+
ASRToCVisitor v(diagnostics, co, default_lower_bound);
12901304
try {
12911305
v.visit_asr((ASR::asr_t &)asr);
12921306
} catch (const CodeGenError &e) {

src/libasr/codegen/asr_to_c.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace LCompilers {
88

99
Result<std::string> asr_to_c(Allocator &al, ASR::TranslationUnit_t &asr,
10-
diag::Diagnostics &diagnostics, Platform &platform,
10+
diag::Diagnostics &diagnostics, CompilerOptions &co,
1111
int64_t default_lower_bound);
1212

1313
} // namespace LCompilers

0 commit comments

Comments
 (0)