@@ -61,6 +61,20 @@ struct SymbolFuncInfo {
61
61
Vec<ASR::Variable_t *> referenced_vars;
62
62
};
63
63
64
+ enum RT_FUNCS {
65
+ print_i64 = 0 ,
66
+ print_f64 = 1 ,
67
+ add_c32 = 2 ,
68
+ add_c64 = 3 ,
69
+ sub_c32 = 4 ,
70
+ sub_c64 = 5 ,
71
+ mul_c32 = 6 ,
72
+ mul_c64 = 7 ,
73
+ abs_c32 = 9 ,
74
+ abs_c64 = 10 ,
75
+ };
76
+ const int NO_OF_RT_FUNCS = 11 ;
77
+
64
78
class ASRToWASMVisitor : public ASR ::BaseVisitor<ASRToWASMVisitor> {
65
79
public:
66
80
Allocator &m_al;
@@ -104,7 +118,8 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
104
118
std::map<std::string, uint32_t > m_self_func_name_idx_map;
105
119
std::map<std::string, uint32_t > m_string_to_iov_loc_map;
106
120
107
- std::map<std::string,void (LCompilers::ASRToWASMVisitor::*)(int )> m_self_funcs_map;
121
+ std::vector<void (LCompilers::ASRToWASMVisitor::*)(int )> m_rt_funcs_map;
122
+ std::vector<int > m_rt_func_used_idx;
108
123
109
124
public:
110
125
ASRToWASMVisitor (Allocator &al, diag::Diagnostics &diagnostics)
@@ -134,6 +149,9 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
134
149
m_export_section.reserve (m_al, 1024 * 128 );
135
150
m_code_section.reserve (m_al, 1024 * 128 );
136
151
m_data_section.reserve (m_al, 1024 * 128 );
152
+
153
+ m_rt_funcs_map.resize (NO_OF_RT_FUNCS);
154
+ m_rt_func_used_idx = std::vector<int >(NO_OF_RT_FUNCS, -1 );
137
155
}
138
156
139
157
void get_wasm (Vec<uint8_t > &code) {
@@ -536,7 +554,7 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
536
554
537
555
wasm::emit_get_local (m_code_section, m_al, 0 );
538
556
wasm::emit_i64_trunc_f64_s (m_code_section, m_al);
539
- wasm::emit_call (m_code_section, m_al, m_self_func_name_idx_map[ " print_i64" ]);
557
+ wasm::emit_call (m_code_section, m_al, m_rt_func_used_idx[ print_i64]);
540
558
emit_call_fd_write (1 , " ." , 1 , 0 );
541
559
542
560
wasm::emit_get_local (m_code_section, m_al, 0 );
@@ -587,7 +605,7 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
587
605
});
588
606
589
607
wasm::emit_get_local (m_code_section, m_al, 3 );
590
- wasm::emit_call (m_code_section, m_al, m_self_func_name_idx_map[ " print_i64" ]);
608
+ wasm::emit_call (m_code_section, m_al, m_rt_func_used_idx[ print_i64]);
591
609
}, fn_idx);
592
610
}
593
611
@@ -774,16 +792,16 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
774
792
emit_string (std::to_string (i));
775
793
}
776
794
777
- m_self_funcs_map[ " print_i64" ] = &ASRToWASMVisitor::emit_print_int;
778
- m_self_funcs_map[ " print_f64" ] = &ASRToWASMVisitor::emit_print_float;
779
- m_self_funcs_map[ " add_c32" ] = &ASRToWASMVisitor::emit_complex_add_32;
780
- m_self_funcs_map[ " add_c64" ] = &ASRToWASMVisitor::emit_complex_add_64;
781
- m_self_funcs_map[ " sub_c32" ] = &ASRToWASMVisitor::emit_complex_sub_32;
782
- m_self_funcs_map[ " sub_c64" ] = &ASRToWASMVisitor::emit_complex_sub_64;
783
- m_self_funcs_map[ " mul_c32" ] = &ASRToWASMVisitor::emit_complex_mul_32;
784
- m_self_funcs_map[ " mul_c64" ] = &ASRToWASMVisitor::emit_complex_mul_64;
785
- m_self_funcs_map[ " abs_c32" ] = &ASRToWASMVisitor::emit_complex_abs_32;
786
- m_self_funcs_map[ " abs_c64" ] = &ASRToWASMVisitor::emit_complex_abs_64;
795
+ m_rt_funcs_map[ print_i64] = &ASRToWASMVisitor::emit_print_int;
796
+ m_rt_funcs_map[ print_f64] = &ASRToWASMVisitor::emit_print_float;
797
+ m_rt_funcs_map[ add_c32] = &ASRToWASMVisitor::emit_complex_add_32;
798
+ m_rt_funcs_map[ add_c64] = &ASRToWASMVisitor::emit_complex_add_64;
799
+ m_rt_funcs_map[ sub_c32] = &ASRToWASMVisitor::emit_complex_sub_32;
800
+ m_rt_funcs_map[ sub_c64] = &ASRToWASMVisitor::emit_complex_sub_64;
801
+ m_rt_funcs_map[ mul_c32] = &ASRToWASMVisitor::emit_complex_mul_32;
802
+ m_rt_funcs_map[ mul_c64] = &ASRToWASMVisitor::emit_complex_mul_64;
803
+ m_rt_funcs_map[ abs_c32] = &ASRToWASMVisitor::emit_complex_abs_32;
804
+ m_rt_funcs_map[ abs_c64] = &ASRToWASMVisitor::emit_complex_abs_64;
787
805
788
806
{
789
807
// Pre-declare all functions first, then generate code
@@ -850,19 +868,17 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
850
868
}
851
869
}
852
870
853
- std::vector<std::pair<std::string, uint32_t >> ordered_self_funcs_name_idx;
854
- for (auto self_func:m_self_func_name_idx_map) {
855
- ordered_self_funcs_name_idx.push_back (self_func);
871
+ std::vector<std::pair<uint32_t , int >> ordered_rt_funcs_type_idx;
872
+ for (int i = 0 ; i < NO_OF_RT_FUNCS; i++) {
873
+ if (m_rt_func_used_idx[i] != -1 ) {
874
+ ordered_rt_funcs_type_idx.push_back (std::make_pair (m_rt_func_used_idx[i], i));
875
+ }
856
876
}
857
877
858
- sort (ordered_self_funcs_name_idx.begin (),
859
- ordered_self_funcs_name_idx.end (),
860
- [](std::pair<std::string, uint32_t > &a, std::pair<std::string, uint32_t > &b){
861
- return a.second < b.second ;
862
- });
878
+ sort (ordered_rt_funcs_type_idx.begin (), ordered_rt_funcs_type_idx.end ());
863
879
864
- for (auto self_func:ordered_self_funcs_name_idx ) {
865
- (this ->*m_self_funcs_map[self_func. first ])(self_func. second );
880
+ for (auto rt_func:ordered_rt_funcs_type_idx ) {
881
+ (this ->*m_rt_funcs_map[rt_func. second ])(rt_func. first );
866
882
}
867
883
}
868
884
@@ -1705,43 +1721,43 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
1705
1721
switch (x.m_op ) {
1706
1722
case ASR::binopType::Add: {
1707
1723
if (a_kind == 4 ) {
1708
- if (m_self_func_name_idx_map. find ( " add_c32" ) == m_self_func_name_idx_map. end () ) {
1709
- m_self_func_name_idx_map[ " add_c32" ] = no_of_types++;
1724
+ if (m_rt_func_used_idx[ add_c32] == - 1 ) {
1725
+ m_rt_func_used_idx[ add_c32] = no_of_types++;
1710
1726
}
1711
- wasm::emit_call (m_code_section, m_al, m_self_func_name_idx_map[ " add_c32" ]);
1727
+ wasm::emit_call (m_code_section, m_al, m_rt_func_used_idx[ add_c32]);
1712
1728
} else {
1713
- if (m_self_func_name_idx_map. find ( " add_c64" ) == m_self_func_name_idx_map. end () ) {
1714
- m_self_func_name_idx_map[ " add_c64" ] = no_of_types++;
1729
+ if (m_rt_func_used_idx[ add_c64] == - 1 ) {
1730
+ m_rt_func_used_idx[ add_c64] = no_of_types++;
1715
1731
}
1716
- wasm::emit_call (m_code_section, m_al, m_self_func_name_idx_map[ " add_c64" ]);
1732
+ wasm::emit_call (m_code_section, m_al, m_rt_func_used_idx[ add_c64]);
1717
1733
}
1718
1734
break ;
1719
1735
};
1720
1736
case ASR::binopType::Sub: {
1721
1737
if (a_kind == 4 ) {
1722
- if (m_self_func_name_idx_map. find ( " sub_c32" ) == m_self_func_name_idx_map. end () ) {
1723
- m_self_func_name_idx_map[ " sub_c32" ] = no_of_types++;
1738
+ if (m_rt_func_used_idx[ sub_c32] == - 1 ) {
1739
+ m_rt_func_used_idx[ sub_c32] = no_of_types++;
1724
1740
}
1725
- wasm::emit_call (m_code_section, m_al, m_self_func_name_idx_map[ " sub_c32" ]);
1741
+ wasm::emit_call (m_code_section, m_al, m_rt_func_used_idx[ sub_c32]);
1726
1742
} else {
1727
- if (m_self_func_name_idx_map. find ( " sub_c64" ) == m_self_func_name_idx_map. end () ) {
1728
- m_self_func_name_idx_map[ " sub_c64" ] = no_of_types++;
1743
+ if (m_rt_func_used_idx[ sub_c64] == - 1 ) {
1744
+ m_rt_func_used_idx[ sub_c64] = no_of_types++;
1729
1745
}
1730
- wasm::emit_call (m_code_section, m_al, m_self_func_name_idx_map[ " sub_c64" ]);
1746
+ wasm::emit_call (m_code_section, m_al, m_rt_func_used_idx[ sub_c64]);
1731
1747
}
1732
1748
break ;
1733
1749
};
1734
1750
case ASR::binopType::Mul: {
1735
1751
if (a_kind == 4 ) {
1736
- if (m_self_func_name_idx_map. find ( " mul_c32" ) == m_self_func_name_idx_map. end () ) {
1737
- m_self_func_name_idx_map[ " mul_c32" ] = no_of_types++;
1752
+ if (m_rt_func_used_idx[ mul_c32] == - 1 ) {
1753
+ m_rt_func_used_idx[ mul_c32] = no_of_types++;
1738
1754
}
1739
- wasm::emit_call (m_code_section, m_al, m_self_func_name_idx_map[ " mul_c32" ]);
1755
+ wasm::emit_call (m_code_section, m_al, m_rt_func_used_idx[ mul_c32]);
1740
1756
} else {
1741
- if (m_self_func_name_idx_map. find ( " mul_c64" ) == m_self_func_name_idx_map. end () ) {
1742
- m_self_func_name_idx_map[ " mul_c64" ] = no_of_types++;
1757
+ if (m_rt_func_used_idx[ mul_c64] == - 1 ) {
1758
+ m_rt_func_used_idx[ mul_c64] = no_of_types++;
1743
1759
}
1744
- wasm::emit_call (m_code_section, m_al, m_self_func_name_idx_map[ " mul_c64" ]);
1760
+ wasm::emit_call (m_code_section, m_al, m_rt_func_used_idx[ mul_c64]);
1745
1761
}
1746
1762
break ;
1747
1763
};
@@ -2667,17 +2683,17 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
2667
2683
int arg_kind = -1 , dest_kind = -1 ;
2668
2684
extract_kinds (x, arg_kind, dest_kind);
2669
2685
if (arg_kind == 4 ) {
2670
- if (m_self_func_name_idx_map. find ( " abs_c32" ) == m_self_func_name_idx_map. end () ) {
2671
- m_self_func_name_idx_map[ " abs_c32" ] = no_of_types++;
2686
+ if (m_rt_func_used_idx[ abs_c32] == - 1 ) {
2687
+ m_rt_func_used_idx[ abs_c32] = no_of_types++;
2672
2688
}
2673
- wasm::emit_call (m_code_section, m_al, m_self_func_name_idx_map[ " abs_c32" ]);
2689
+ wasm::emit_call (m_code_section, m_al, m_rt_func_used_idx[ abs_c32]);
2674
2690
wasm::emit_f32_const (m_code_section, m_al, 0.0 );
2675
2691
wasm::emit_f32_gt (m_code_section, m_al);
2676
2692
} else if (arg_kind == 8 ) {
2677
- if (m_self_func_name_idx_map. find ( " abs_c64" ) == m_self_func_name_idx_map. end () ) {
2678
- m_self_func_name_idx_map[ " abs_c64" ] = no_of_types++;
2693
+ if (m_rt_func_used_idx[ abs_c64] == - 1 ) {
2694
+ m_rt_func_used_idx[ abs_c64] = no_of_types++;
2679
2695
}
2680
- wasm::emit_call (m_code_section, m_al, m_self_func_name_idx_map[ " abs_c64" ]);
2696
+ wasm::emit_call (m_code_section, m_al, m_rt_func_used_idx[ abs_c64]);
2681
2697
wasm::emit_f64_const (m_code_section, m_al, 0.0 );
2682
2698
wasm::emit_f64_gt (m_code_section, m_al);
2683
2699
} else {
@@ -2817,18 +2833,18 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
2817
2833
int a_kind = ASRUtils::extract_kind_from_ttype_t (t);
2818
2834
2819
2835
if (ASRUtils::is_integer (*t) || ASRUtils::is_logical (*t)) {
2820
- if (m_self_func_name_idx_map. find ( " print_i64" ) == m_self_func_name_idx_map. end () ) {
2821
- m_self_func_name_idx_map[ " print_i64" ] = no_of_types++;
2836
+ if (m_rt_func_used_idx[ print_i64] == - 1 ) {
2837
+ m_rt_func_used_idx[ print_i64] = no_of_types++;
2822
2838
}
2823
2839
this ->visit_expr (*x.m_values [i]);
2824
2840
switch (a_kind) {
2825
2841
case 4 : {
2826
2842
wasm::emit_i64_extend_i32_s (m_code_section, m_al);
2827
- wasm::emit_call (m_code_section, m_al, m_self_func_name_idx_map[ " print_i64" ]);
2843
+ wasm::emit_call (m_code_section, m_al, m_rt_func_used_idx[ print_i64]);
2828
2844
break ;
2829
2845
}
2830
2846
case 8 : {
2831
- wasm::emit_call (m_code_section, m_al, m_self_func_name_idx_map[ " print_i64" ]);
2847
+ wasm::emit_call (m_code_section, m_al, m_rt_func_used_idx[ print_i64]);
2832
2848
break ;
2833
2849
}
2834
2850
default : {
@@ -2838,21 +2854,21 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
2838
2854
}
2839
2855
}
2840
2856
} else if (ASRUtils::is_real (*t)) {
2841
- if (m_self_func_name_idx_map. find ( " print_i64" ) == m_self_func_name_idx_map. end () ) {
2842
- m_self_func_name_idx_map[ " print_i64" ] = no_of_types++;
2857
+ if (m_rt_func_used_idx[ print_i64] == - 1 ) {
2858
+ m_rt_func_used_idx[ print_i64] = no_of_types++;
2843
2859
}
2844
- if (m_self_func_name_idx_map. find ( " print_f64" ) == m_self_func_name_idx_map. end () ) {
2845
- m_self_func_name_idx_map[ " print_f64" ] = no_of_types++;
2860
+ if (m_rt_func_used_idx[ print_f64] == - 1 ) {
2861
+ m_rt_func_used_idx[ print_f64] = no_of_types++;
2846
2862
}
2847
2863
this ->visit_expr (*x.m_values [i]);
2848
2864
switch (a_kind) {
2849
2865
case 4 : {
2850
2866
wasm::emit_f64_promote_f32 (m_code_section, m_al);
2851
- wasm::emit_call (m_code_section, m_al, m_self_func_name_idx_map[ " print_f64" ]);
2867
+ wasm::emit_call (m_code_section, m_al, m_rt_func_used_idx[ print_f64]);
2852
2868
break ;
2853
2869
}
2854
2870
case 8 : {
2855
- wasm::emit_call (m_code_section, m_al, m_self_func_name_idx_map[ " print_f64" ]);
2871
+ wasm::emit_call (m_code_section, m_al, m_rt_func_used_idx[ print_f64]);
2856
2872
break ;
2857
2873
}
2858
2874
default : {
@@ -2875,11 +2891,11 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
2875
2891
->index );
2876
2892
wasm::emit_drop (m_code_section, m_al);
2877
2893
} else if (t->type == ASR::ttypeType::Complex) {
2878
- if (m_self_func_name_idx_map. find ( " print_i64" ) == m_self_func_name_idx_map. end () ) {
2879
- m_self_func_name_idx_map[ " print_i64" ] = no_of_types++;
2894
+ if (m_rt_func_used_idx[ print_i64] == - 1 ) {
2895
+ m_rt_func_used_idx[ print_i64] = no_of_types++;
2880
2896
}
2881
- if (m_self_func_name_idx_map. find ( " print_f64" ) == m_self_func_name_idx_map. end () ) {
2882
- m_self_func_name_idx_map[ " print_f64" ] = no_of_types++;
2897
+ if (m_rt_func_used_idx[ print_f64] == - 1 ) {
2898
+ m_rt_func_used_idx[ print_f64] = no_of_types++;
2883
2899
}
2884
2900
emit_call_fd_write (1 , " (" , 1 , 0 );
2885
2901
this ->visit_expr (*x.m_values [i]);
@@ -2890,10 +2906,10 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
2890
2906
} else {
2891
2907
wasm::emit_set_global (m_code_section, m_al, m_global_var_name_idx_map[" tmp_reg_f64" ]);
2892
2908
}
2893
- wasm::emit_call (m_code_section, m_al, m_self_func_name_idx_map[ " print_f64" ]);
2909
+ wasm::emit_call (m_code_section, m_al, m_rt_func_used_idx[ print_f64]);
2894
2910
emit_call_fd_write (1 , " ," , 1 , 0 );
2895
2911
wasm::emit_get_global (m_code_section, m_al, m_global_var_name_idx_map[" tmp_reg_f64" ]);
2896
- wasm::emit_call (m_code_section, m_al, m_self_func_name_idx_map[ " print_f64" ]);
2912
+ wasm::emit_call (m_code_section, m_al, m_rt_func_used_idx[ print_f64]);
2897
2913
emit_call_fd_write (1 , " )" , 1 , 0 );
2898
2914
}
2899
2915
}
0 commit comments