Skip to content

Commit c28cef0

Browse files
authored
Merge pull request #1654 from Shaikh-Ubaid/wasm_sync
WASM: Sync backend and other needed files
2 parents b5d2554 + 25aa241 commit c28cef0

40 files changed

+316
-390
lines changed

src/libasr/asdl_cpp.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,12 +1775,12 @@ def visitField(self, field, cons):
17751775
elif field.type == "string" and not field.seq:
17761776
if field.opt:
17771777
self.emit("if (x.m_%s) {" % field.name, 2)
1778-
self.emit( 's.append("\\"" + std::string(x.m_%s) + "\\"");' % field.name, 3)
1778+
self.emit( 's.append("\\"" + get_escaped_str(x.m_%s) + "\\"");' % field.name, 3)
17791779
self.emit("} else {", 2)
17801780
self.emit( 's.append("[]");', 3)
17811781
self.emit("}", 2)
17821782
else:
1783-
self.emit('s.append("\\"" + std::string(x.m_%s) + "\\"");' % field.name, 2)
1783+
self.emit('s.append("\\"" + get_escaped_str(x.m_%s) + "\\"");' % field.name, 2)
17841784
elif field.type == "int" and not field.seq:
17851785
if field.opt:
17861786
self.emit("if (x.m_%s) {" % field.name, 2)
@@ -2495,6 +2495,7 @@ def add_masks(fields, node):
24952495
#include <libasr/containers.h>
24962496
#include <libasr/exception.h>
24972497
#include <libasr/asr_scopes.h>
2498+
#include <libasr/string_utils.h>
24982499
24992500
25002501
namespace LCompilers::%(MOD)s {

src/libasr/codegen/asr_to_wasm.cpp

Lines changed: 201 additions & 32 deletions
Large diffs are not rendered by default.

src/libasr/codegen/wasm_to_x64.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ class X64Visitor : public WASMDecoder<X64Visitor>,
6060
}
6161

6262
void visit_Unreachable() {}
63+
6364
void visit_EmtpyBlockType() {}
65+
6466
void visit_Drop() { m_a.asm_pop_r64(X64Reg::rax); }
6567

6668
void call_imported_function(uint32_t func_idx) {

src/libasr/pass/pass_array_by_data.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class PassArrayByDataProcedureVisitor : public PassUtils::PassVisitor<PassArrayB
201201
return_var, x_func_type->m_abi, x->m_access, x_func_type->m_deftype,
202202
s2c(al, new_bindc_name), x_func_type->m_elemental,
203203
x_func_type->m_pure, x_func_type->m_module, x_func_type->m_inline,
204-
x_func_type->m_static, nullptr, 0, nullptr, 0, false, false, false);
204+
x_func_type->m_static, x_func_type->m_type_params, x_func_type->n_type_params, nullptr, 0, false, false, false);
205205
new_symbol = ASR::down_cast<ASR::symbol_t>(new_subrout);
206206
}
207207
current_scope->add_symbol(new_name, new_symbol);

src/libasr/pass/unused_functions.cpp

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,10 @@ class CollectUnusedFunctionsVisitor :
2323
std::map<uint64_t, std::string> fn_declarations;
2424
std::map<uint64_t, std::string> fn_used;
2525

26-
// TODO: Do subroutines just like Functions:
27-
2826
void visit_Function(const ASR::Function_t &x) {
29-
if (x.m_return_var) {
30-
uint64_t h = get_hash((ASR::asr_t*)&x);
31-
if (ASRUtils::get_FunctionType(x)->m_abi != ASR::abiType::BindC) {
32-
fn_declarations[h] = x.m_name;
33-
}
27+
uint64_t h = get_hash((ASR::asr_t*)&x);
28+
if (ASRUtils::get_FunctionType(x)->m_abi != ASR::abiType::BindC) {
29+
fn_declarations[h] = x.m_name;
3430
}
3531

3632
for( size_t i = 0; i < x.n_args; i++ ) {
@@ -56,8 +52,7 @@ class CollectUnusedFunctionsVisitor :
5652
}
5753

5854
void visit_ExternalSymbol(const ASR::ExternalSymbol_t &x) {
59-
if (ASR::is_a<ASR::Function_t>(*x.m_external) &&
60-
ASR::down_cast<ASR::Function_t>(x.m_external)->m_return_var) {
55+
if (ASR::is_a<ASR::Function_t>(*x.m_external)) {
6156
uint64_t h = get_hash((ASR::asr_t*)&x);
6257
fn_declarations[h] = x.m_name;
6358
h = get_hash((ASR::asr_t*)x.m_external);
@@ -72,7 +67,8 @@ class CollectUnusedFunctionsVisitor :
7267
}
7368

7469

75-
void visit_FunctionCall(const ASR::FunctionCall_t &x) {
70+
template <typename T>
71+
void visit_FuncSubCall(const T& x) {
7672
{
7773
const ASR::symbol_t *s = ASRUtils::symbol_get_past_external(x.m_name);
7874
if (ASR::is_a<ASR::Function_t>(*s)) {
@@ -115,14 +111,32 @@ class CollectUnusedFunctionsVisitor :
115111
if( x.m_args[i].m_value ) {
116112
visit_expr(*(x.m_args[i].m_value));
117113
}
114+
if (x.m_args[i].m_value && ASR::is_a<ASR::Var_t>(*x.m_args[i].m_value)) {
115+
ASR::Var_t* var = ASR::down_cast<ASR::Var_t>(x.m_args[i].m_value);
116+
if (ASR::is_a<ASR::ExternalSymbol_t>(*var->m_v)) {
117+
ASR::ExternalSymbol_t* extsym = ASR::down_cast<ASR::ExternalSymbol_t>(var->m_v);
118+
uint64_t h = get_hash((ASR::asr_t*)extsym);
119+
fn_used[h] = extsym->m_name;
120+
}
121+
}
118122
}
123+
}
124+
125+
void visit_FunctionCall(const ASR::FunctionCall_t &x) {
126+
visit_FuncSubCall(x);
119127
visit_ttype(*x.m_type);
120128
if (x.m_value)
121129
visit_expr(*x.m_value);
122130
if (x.m_dt)
123131
visit_expr(*x.m_dt);
124132
}
125133

134+
void visit_SubroutineCall(const ASR::SubroutineCall_t &x) {
135+
visit_FuncSubCall(x);
136+
if (x.m_dt)
137+
visit_expr(*x.m_dt);
138+
}
139+
126140
void visit_Var(const ASR::Var_t &x) {
127141
const ASR::symbol_t *s = ASRUtils::symbol_get_past_external(x.m_v);
128142
if (ASR::is_a<ASR::Function_t>(*s)) {
@@ -182,6 +196,15 @@ class ProgramVisitor :
182196
public:
183197
bool program_present=false;
184198

199+
void visit_TranslationUnit(const ASR::TranslationUnit_t &x) {
200+
for (auto &a : x.m_global_scope->get_scope()) {
201+
if (ASR::is_a<ASR::Program_t>(*a.second)) {
202+
this->visit_symbol(*a.second);
203+
break;
204+
}
205+
}
206+
}
207+
185208
void visit_Program(const ASR::Program_t &/*x*/) {
186209
program_present = true;
187210
}

src/libasr/pass/update_array_dim_intrinsic_calls.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class ReplaceArrayDimIntrinsicCalls: public ASR::BaseExprReplacer<ReplaceArrayDi
6666
ASR::ttype_t* array_type = ASRUtils::expr_type(x->m_v);
6767
ASR::dimension_t* dims = nullptr;
6868
int n = ASRUtils::extract_dimensions_from_ttype(array_type, dims);
69-
bool is_argument = v->m_intent == ASRUtils::intent_in || v->m_intent == ASRUtils::intent_out;
69+
bool is_argument = v->m_intent == ASRUtils::intent_in || v->m_intent == ASRUtils::intent_out || v->m_intent == ASRUtils::intent_inout;
7070
if( !(n > 0 && is_argument &&
7171
!ASRUtils::is_dimension_empty(dims, n)) ) {
7272
return ;

src/libasr/string_utils.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <string>
55
#include <iostream>
66
#include <fstream>
7+
#include <iomanip>
8+
#include <sstream>
79

810
#include <libasr/string_utils.h>
911
#include <libasr/containers.h>
@@ -85,6 +87,29 @@ std::string replace(const std::string &s,
8587
return std::regex_replace(s, std::regex(regex), replace);
8688
}
8789

90+
std::string get_escaped_str(const std::string &s) {
91+
std::ostringstream o;
92+
for (auto c = s.cbegin(); c != s.cend(); c++) {
93+
switch (*c) {
94+
case '"': o << "\\\""; break;
95+
case '\\': o << "\\\\"; break;
96+
case '\b': o << "\\b"; break;
97+
case '\f': o << "\\f"; break;
98+
case '\n': o << "\\n"; break;
99+
case '\r': o << "\\r"; break;
100+
case '\t': o << "\\t"; break;
101+
default:
102+
if ('\x00' <= *c && *c <= '\x1f') {
103+
o << "\\u"
104+
<< std::hex << std::setw(4) << std::setfill('0') << static_cast<int>(*c);
105+
} else {
106+
o << *c;
107+
}
108+
}
109+
}
110+
return o.str();
111+
}
112+
88113
std::string read_file(const std::string &filename)
89114
{
90115
std::ifstream ifs(filename.c_str(), std::ios::in | std::ios::binary

src/libasr/string_utils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ char *s2c(Allocator &al, const std::string &s);
2424
std::string replace(const std::string &s,
2525
const std::string &regex, const std::string &replace);
2626

27+
// Escapes special characters from the given string.
28+
// It is used during AST/R to Json conversion.
29+
std::string get_escaped_str(const std::string &s);
30+
2731
std::string read_file(const std::string &filename);
2832

2933
// Returns the parent path to the given path

tests/reference/c-c_interop1-e215531.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "c-c_interop1-e215531.stdout",
9-
"stdout_hash": "3bc9bc474cda656a2012aad05b9bf9b9a498016da571f8e5a4857138",
9+
"stdout_hash": "11b48d84e0e873f5b2b5e8e92c721305af545a3a2b0038c2360d50e3",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/c-c_interop1-e215531.stdout

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,6 @@ void l(double a, float b, int64_t c, int32_t d)
5252
printf("%s\n", "OK");
5353
}
5454

55-
void main0()
56-
{
57-
double i;
58-
double x;
59-
float y;
60-
int64_t z;
61-
int32_t zz;
62-
x = 5.00000000000000000e+00;
63-
i = f(x);
64-
y = 5.40000000000000036e+00;
65-
z = 3;
66-
zz = 2;
67-
g(x, y, z, zz);
68-
i = h(x);
69-
l(x, y, z, zz);
70-
}
71-
7255
int main(int argc, char* argv[])
7356
{
7457
return 0;

tests/reference/cpp-assert1-ba60925.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "cpp-assert1-ba60925.stdout",
9-
"stdout_hash": "9fb92dc2310b01633d4a2de3d1378200d4de8ed88b880c54ac69c72e",
9+
"stdout_hash": "407c745929ee126426a33b3ba8f73b8f326d259e714ac66b078befe2",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/cpp-assert1-ba60925.stdout

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,10 @@ struct dimension_descriptor
2323
int32_t lower_bound, length;
2424
};
2525
// Forward declarations
26-
void test_assert();
2726
namespace {
2827
}
2928

3029
// Implementations
31-
void test_assert()
32-
{
33-
int32_t a;
34-
a = 5;
35-
assert (("a is not 5", a == 5));
36-
assert (a != 10);
37-
}
38-
3930
namespace {
4031

4132
void main2() {

tests/reference/cpp-expr2-09c05ad.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "cpp-expr2-09c05ad.stdout",
9-
"stdout_hash": "69ef58997a788c80db293cfeeffb14d1bb1a94f630fff4c8d11b235b",
9+
"stdout_hash": "407c745929ee126426a33b3ba8f73b8f326d259e714ac66b078befe2",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/cpp-expr2-09c05ad.stdout

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,10 @@ struct dimension_descriptor
2323
int32_t lower_bound, length;
2424
};
2525
// Forward declarations
26-
void test_boolOp();
2726
namespace {
2827
}
2928

3029
// Implementations
31-
void test_boolOp()
32-
{
33-
bool a;
34-
bool b;
35-
a = false;
36-
b = true;
37-
a = a && b;
38-
b = a || true;
39-
a = a || b;
40-
a = a && b == b;
41-
a = a && b != b;
42-
a = b || b;
43-
}
44-
4530
namespace {
4631

4732
void main2() {

tests/reference/cpp-expr5-1de0e30.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "cpp-expr5-1de0e30.stdout",
9-
"stdout_hash": "1f68a424d42810751fdd0169e4415cd9efcd4b7c5fb5932eddcf231c",
9+
"stdout_hash": "407c745929ee126426a33b3ba8f73b8f326d259e714ac66b078befe2",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/cpp-expr5-1de0e30.stdout

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,10 @@ struct dimension_descriptor
2323
int32_t lower_bound, length;
2424
};
2525
// Forward declarations
26-
void test_StrOp_concat();
2726
namespace {
2827
}
2928

3029
// Implementations
31-
void test_StrOp_concat()
32-
{
33-
std::string s;
34-
s = std::string("3") + std::string("4");
35-
s = std::string("a ") + std::string("test");
36-
s = std::string(std::string("test") + std::string("test")) + std::string("test");
37-
}
38-
3930
namespace {
4031

4132
void main2() {

tests/reference/cpp-expr6-f337f4f.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "cpp-expr6-f337f4f.stdout",
9-
"stdout_hash": "f804ce32ccbbaa39b2b00595c1a920caa64cd6edc786a3c4a11f4c50",
9+
"stdout_hash": "407c745929ee126426a33b3ba8f73b8f326d259e714ac66b078befe2",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/cpp-expr6-f337f4f.stdout

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,10 @@ struct dimension_descriptor
2323
int32_t lower_bound, length;
2424
};
2525
// Forward declarations
26-
void test_ifexp();
2726
namespace {
2827
}
2928

3029
// Implementations
31-
void test_ifexp()
32-
{
33-
int32_t a;
34-
int32_t b;
35-
bool c;
36-
a = 2;
37-
b = (a == 2) ? (6) : (8);
38-
c = (b > 5) ? (true) : (false);
39-
}
40-
4130
namespace {
4231

4332
void main2() {

tests/reference/cpp-expr8-704cece.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "cpp-expr8-704cece.stdout",
9-
"stdout_hash": "7b462e24b8799e255f9c12dd05f4fe8a15806e20277426c325c663cf",
9+
"stdout_hash": "0114926f9f033b9cbda860412fa9fadb1cebc84c57d89a6a6f6ce763",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

0 commit comments

Comments
 (0)