@@ -144,6 +144,7 @@ export namespace BuiltinNames {
144
144
export const add = "~lib/builtins/add" ;
145
145
export const sub = "~lib/builtins/sub" ;
146
146
export const mul = "~lib/builtins/mul" ;
147
+ export const div = "~lib/builtins/div" ;
147
148
export const clz = "~lib/builtins/clz" ;
148
149
export const ctz = "~lib/builtins/ctz" ;
149
150
export const popcnt = "~lib/builtins/popcnt" ;
@@ -249,6 +250,12 @@ export namespace BuiltinNames {
249
250
export const i64_mul = "~lib/builtins/i64.mul" ;
250
251
export const f32_mul = "~lib/builtins/f32.mul" ;
251
252
export const f64_mul = "~lib/builtins/f64.mul" ;
253
+ export const i32_div_s = "~lib/builtins/i32.div_s" ;
254
+ export const i32_div_u = "~lib/builtins/i32.div_u" ;
255
+ export const i64_div_s = "~lib/builtins/i64.div_s" ;
256
+ export const i64_div_u = "~lib/builtins/i64.div_u" ;
257
+ export const f32_div = "~lib/builtins/f32.div" ;
258
+ export const f64_div = "~lib/builtins/f64.div" ;
252
259
253
260
export const i32_load8_s = "~lib/builtins/i32.load8_s" ;
254
261
export const i32_load8_u = "~lib/builtins/i32.load8_u" ;
@@ -2293,6 +2300,60 @@ function builtin_mul(ctx: BuiltinContext): ExpressionRef {
2293
2300
}
2294
2301
builtins . set ( BuiltinNames . mul , builtin_mul ) ;
2295
2302
2303
+ // div<T?>(left: T, right: T) -> T
2304
+ function builtin_div ( ctx : BuiltinContext ) : ExpressionRef {
2305
+ var compiler = ctx . compiler ;
2306
+ var module = compiler . module ;
2307
+ if ( checkTypeOptional ( ctx , true ) | checkArgsRequired ( ctx , 2 ) ) {
2308
+ return module . unreachable ( ) ;
2309
+ }
2310
+ var operands = ctx . operands ;
2311
+ var typeArguments = ctx . typeArguments ;
2312
+ var left = operands [ 0 ] ;
2313
+ var arg0 = typeArguments
2314
+ ? compiler . compileExpression (
2315
+ left ,
2316
+ typeArguments [ 0 ] ,
2317
+ Constraints . CONV_IMPLICIT
2318
+ )
2319
+ : compiler . compileExpression ( operands [ 0 ] , Type . auto ) ;
2320
+ var type = compiler . currentType ;
2321
+ if ( type . isValue ) {
2322
+ let arg1 : ExpressionRef ;
2323
+ if ( ! typeArguments && left . isNumericLiteral ) {
2324
+ // prefer right type
2325
+ arg1 = compiler . compileExpression (
2326
+ operands [ 1 ] ,
2327
+ type
2328
+ ) ;
2329
+ if ( compiler . currentType != type ) {
2330
+ arg0 = compiler . compileExpression (
2331
+ left ,
2332
+ ( type = compiler . currentType ) ,
2333
+ Constraints . CONV_IMPLICIT
2334
+ ) ;
2335
+ }
2336
+ } else {
2337
+ arg1 = compiler . compileExpression (
2338
+ operands [ 1 ] ,
2339
+ type ,
2340
+ Constraints . CONV_IMPLICIT
2341
+ ) ;
2342
+ }
2343
+ if ( type . isNumericValue ) {
2344
+ return compiler . makeDiv ( arg0 , arg1 , type ) ;
2345
+ }
2346
+ }
2347
+ compiler . error (
2348
+ DiagnosticCode . Operation_0_cannot_be_applied_to_type_1 ,
2349
+ ctx . reportNode . typeArgumentsRange ,
2350
+ "div" ,
2351
+ type . toString ( )
2352
+ ) ;
2353
+ return module . unreachable ( ) ;
2354
+ }
2355
+ builtins . set ( BuiltinNames . div , builtin_div ) ;
2356
+
2296
2357
// === Atomics ================================================================================
2297
2358
2298
2359
// atomic.load<T!>(offset: usize, immOffset?: usize) -> T*
@@ -5822,6 +5883,24 @@ function builtin_i64_add(ctx: BuiltinContext): ExpressionRef {
5822
5883
}
5823
5884
builtins . set ( BuiltinNames . i64_add , builtin_i64_add ) ;
5824
5885
5886
+ // f32.add -> add<f32>
5887
+ function builtin_f32_add ( ctx : BuiltinContext ) : ExpressionRef {
5888
+ checkTypeAbsent ( ctx ) ;
5889
+ ctx . typeArguments = [ Type . f32 ] ;
5890
+ ctx . contextualType = Type . f32 ;
5891
+ return builtin_add ( ctx ) ;
5892
+ }
5893
+ builtins . set ( BuiltinNames . f32_add , builtin_f32_add ) ;
5894
+
5895
+ // f64.add -> add<f64>
5896
+ function builtin_f64_add ( ctx : BuiltinContext ) : ExpressionRef {
5897
+ checkTypeAbsent ( ctx ) ;
5898
+ ctx . typeArguments = [ Type . f64 ] ;
5899
+ ctx . contextualType = Type . f64 ;
5900
+ return builtin_add ( ctx ) ;
5901
+ }
5902
+ builtins . set ( BuiltinNames . f64_add , builtin_f64_add ) ;
5903
+
5825
5904
// i32.sub -> sub<i32>
5826
5905
function builtin_i32_sub ( ctx : BuiltinContext ) : ExpressionRef {
5827
5906
checkTypeAbsent ( ctx ) ;
@@ -5840,6 +5919,24 @@ function builtin_i64_sub(ctx: BuiltinContext): ExpressionRef {
5840
5919
}
5841
5920
builtins . set ( BuiltinNames . i64_sub , builtin_i64_sub ) ;
5842
5921
5922
+ // f32.sub -> sub<f32>
5923
+ function builtin_f32_sub ( ctx : BuiltinContext ) : ExpressionRef {
5924
+ checkTypeAbsent ( ctx ) ;
5925
+ ctx . typeArguments = [ Type . f32 ] ;
5926
+ ctx . contextualType = Type . f32 ;
5927
+ return builtin_sub ( ctx ) ;
5928
+ }
5929
+ builtins . set ( BuiltinNames . f32_sub , builtin_f32_sub ) ;
5930
+
5931
+ // f64.sub -> sub<f64>
5932
+ function builtin_f64_sub ( ctx : BuiltinContext ) : ExpressionRef {
5933
+ checkTypeAbsent ( ctx ) ;
5934
+ ctx . typeArguments = [ Type . f64 ] ;
5935
+ ctx . contextualType = Type . f64 ;
5936
+ return builtin_sub ( ctx ) ;
5937
+ }
5938
+ builtins . set ( BuiltinNames . f64_sub , builtin_f64_sub ) ;
5939
+
5843
5940
// i32.mul -> mul<i32>
5844
5941
function builtin_i32_mul ( ctx : BuiltinContext ) : ExpressionRef {
5845
5942
checkTypeAbsent ( ctx ) ;
@@ -5858,59 +5955,77 @@ function builtin_i64_mul(ctx: BuiltinContext): ExpressionRef {
5858
5955
}
5859
5956
builtins . set ( BuiltinNames . i64_mul , builtin_i64_mul ) ;
5860
5957
5861
- // f32.add -> add <f32>
5862
- function builtin_f32_add ( ctx : BuiltinContext ) : ExpressionRef {
5958
+ // f32.mul -> mul <f32>
5959
+ function builtin_f32_mul ( ctx : BuiltinContext ) : ExpressionRef {
5863
5960
checkTypeAbsent ( ctx ) ;
5864
5961
ctx . typeArguments = [ Type . f32 ] ;
5865
5962
ctx . contextualType = Type . f32 ;
5866
- return builtin_add ( ctx ) ;
5963
+ return builtin_mul ( ctx ) ;
5867
5964
}
5868
- builtins . set ( BuiltinNames . f32_add , builtin_f32_add ) ;
5965
+ builtins . set ( BuiltinNames . f32_mul , builtin_f32_mul ) ;
5869
5966
5870
- // f64.add -> add <f64>
5871
- function builtin_f64_add ( ctx : BuiltinContext ) : ExpressionRef {
5967
+ // f64.mul -> mul <f64>
5968
+ function builtin_f64_mul ( ctx : BuiltinContext ) : ExpressionRef {
5872
5969
checkTypeAbsent ( ctx ) ;
5873
5970
ctx . typeArguments = [ Type . f64 ] ;
5874
5971
ctx . contextualType = Type . f64 ;
5875
- return builtin_add ( ctx ) ;
5972
+ return builtin_mul ( ctx ) ;
5876
5973
}
5877
- builtins . set ( BuiltinNames . f64_add , builtin_f64_add ) ;
5974
+ builtins . set ( BuiltinNames . f64_mul , builtin_f64_mul ) ;
5878
5975
5879
- // f32.sub -> sub<f32 >
5880
- function builtin_f32_sub ( ctx : BuiltinContext ) : ExpressionRef {
5976
+ // i32.div_s -> div<i32 >
5977
+ function builtin_i32_div_s ( ctx : BuiltinContext ) : ExpressionRef {
5881
5978
checkTypeAbsent ( ctx ) ;
5882
- ctx . typeArguments = [ Type . f32 ] ;
5883
- ctx . contextualType = Type . f32 ;
5884
- return builtin_sub ( ctx ) ;
5979
+ ctx . typeArguments = [ Type . i32 ] ;
5980
+ ctx . contextualType = Type . i32 ;
5981
+ return builtin_div ( ctx ) ;
5885
5982
}
5886
- builtins . set ( BuiltinNames . f32_sub , builtin_f32_sub ) ;
5983
+ builtins . set ( BuiltinNames . i32_div_s , builtin_i32_div_s ) ;
5887
5984
5888
- // f64.sub -> sub<f64 >
5889
- function builtin_f64_sub ( ctx : BuiltinContext ) : ExpressionRef {
5985
+ // i32.div_u -> div<u32 >
5986
+ function builtin_i32_div_u ( ctx : BuiltinContext ) : ExpressionRef {
5890
5987
checkTypeAbsent ( ctx ) ;
5891
- ctx . typeArguments = [ Type . f64 ] ;
5892
- ctx . contextualType = Type . f64 ;
5893
- return builtin_sub ( ctx ) ;
5988
+ ctx . typeArguments = [ Type . u32 ] ;
5989
+ ctx . contextualType = Type . u32 ;
5990
+ return builtin_div ( ctx ) ;
5894
5991
}
5895
- builtins . set ( BuiltinNames . f64_sub , builtin_f64_sub ) ;
5992
+ builtins . set ( BuiltinNames . i32_div_u , builtin_i32_div_u ) ;
5896
5993
5897
- // f32.mul -> mul<f32>
5898
- function builtin_f32_mul ( ctx : BuiltinContext ) : ExpressionRef {
5994
+ // i64.div_s -> div_s<i64>
5995
+ function builtin_i64_div_s ( ctx : BuiltinContext ) : ExpressionRef {
5996
+ checkTypeAbsent ( ctx ) ;
5997
+ ctx . typeArguments = [ Type . i64 ] ;
5998
+ ctx . contextualType = Type . i64 ;
5999
+ return builtin_div ( ctx ) ;
6000
+ }
6001
+ builtins . set ( BuiltinNames . i64_div_s , builtin_i64_div_s ) ;
6002
+
6003
+ // i64.div_u -> div_u<u64>
6004
+ function builtin_i64_div_u ( ctx : BuiltinContext ) : ExpressionRef {
6005
+ checkTypeAbsent ( ctx ) ;
6006
+ ctx . typeArguments = [ Type . u64 ] ;
6007
+ ctx . contextualType = Type . u64 ;
6008
+ return builtin_div ( ctx ) ;
6009
+ }
6010
+ builtins . set ( BuiltinNames . i64_div_u , builtin_i64_div_u ) ;
6011
+
6012
+ // f32.div -> div<f32>
6013
+ function builtin_f32_div ( ctx : BuiltinContext ) : ExpressionRef {
5899
6014
checkTypeAbsent ( ctx ) ;
5900
6015
ctx . typeArguments = [ Type . f32 ] ;
5901
6016
ctx . contextualType = Type . f32 ;
5902
- return builtin_mul ( ctx ) ;
6017
+ return builtin_div ( ctx ) ;
5903
6018
}
5904
- builtins . set ( BuiltinNames . f32_mul , builtin_f32_mul ) ;
6019
+ builtins . set ( BuiltinNames . f32_div , builtin_f32_div ) ;
5905
6020
5906
- // f64.mul -> mul <f64>
5907
- function builtin_f64_mul ( ctx : BuiltinContext ) : ExpressionRef {
6021
+ // f64.div -> div <f64>
6022
+ function builtin_f64_div ( ctx : BuiltinContext ) : ExpressionRef {
5908
6023
checkTypeAbsent ( ctx ) ;
5909
6024
ctx . typeArguments = [ Type . f64 ] ;
5910
6025
ctx . contextualType = Type . f64 ;
5911
- return builtin_mul ( ctx ) ;
6026
+ return builtin_div ( ctx ) ;
5912
6027
}
5913
- builtins . set ( BuiltinNames . f64_mul , builtin_f64_mul ) ;
6028
+ builtins . set ( BuiltinNames . f64_div , builtin_f64_div ) ;
5914
6029
5915
6030
// i32.load8_s -> <i32>load<i8>
5916
6031
function builtin_i32_load8_s ( ctx : BuiltinContext ) : ExpressionRef {
0 commit comments