Add an option to alias global objects

This for example allows to switch the default 'Math' implementation a program will use. Uses 'NativeMath' by default.
This commit is contained in:
dcodeIO
2018-03-26 16:54:25 +02:00
parent e75d006d26
commit 792202ac5a
14 changed files with 186 additions and 167 deletions

View File

@ -13,6 +13,7 @@
(import "JSMath" "log" (func $(lib)/math/JSMath.log (param f64) (result f64)))
(import "JSMath" "exp" (func $(lib)/math/JSMath.exp (param f64) (result f64)))
(memory $0 1)
(data (i32.const 4) "\0b\00\00\00s\00t\00d\00/\00m\00a\00t\00h\00.\00t\00s")
(export "memory" (memory $0))
(start $start)
(func $fmod (; 2 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
@ -869,7 +870,7 @@
(get_local $4)
)
)
(func "$(lib)/math/Math.log" (; 8 ;) (type $FF) (param $0 f64) (result f64)
(func "$(lib)/math/NativeMath.log" (; 8 ;) (type $FF) (param $0 f64) (result f64)
(local $1 i32)
(local $2 i32)
(local $3 i64)
@ -1136,7 +1137,7 @@
)
(func $std/math/test_log (; 9 ;) (type $FFFiv) (param $0 f64) (param $1 f64) (param $2 f64) (param $3 i32)
(call $std/math/check<f64>
(call "$(lib)/math/Math.log"
(call "$(lib)/math/NativeMath.log"
(get_local $0)
)
(get_local $1)
@ -1152,7 +1153,7 @@
(get_local $3)
)
)
(func "$(lib)/math/Mathf.log" (; 10 ;) (type $ff) (param $0 f32) (result f32)
(func "$(lib)/math/NativeMathf.log" (; 10 ;) (type $ff) (param $0 f32) (result f32)
(local $1 i32)
(local $2 i32)
(local $3 f32)
@ -1357,7 +1358,7 @@
)
(func $std/math/test_logf (; 11 ;) (type $fffiv) (param $0 f32) (param $1 f32) (param $2 f32) (param $3 i32)
(call $std/math/check<f32>
(call "$(lib)/math/Mathf.log"
(call "$(lib)/math/NativeMathf.log"
(get_local $0)
)
(get_local $1)
@ -1479,7 +1480,7 @@
)
)
)
(func "$(lib)/math/Math.exp" (; 13 ;) (type $FF) (param $0 f64) (result f64)
(func "$(lib)/math/NativeMath.exp" (; 13 ;) (type $FF) (param $0 f64) (result f64)
(local $1 i32)
(local $2 f64)
(local $3 f64)
@ -1694,7 +1695,7 @@
)
(func $std/math/test_exp (; 14 ;) (type $FFFiv) (param $0 f64) (param $1 f64) (param $2 f64) (param $3 i32)
(call $std/math/check<f64>
(call "$(lib)/math/Math.exp"
(call "$(lib)/math/NativeMath.exp"
(get_local $0)
)
(get_local $1)
@ -1822,7 +1823,7 @@
)
)
)
(func "$(lib)/math/Mathf.exp" (; 16 ;) (type $ff) (param $0 f32) (result f32)
(func "$(lib)/math/NativeMathf.exp" (; 16 ;) (type $ff) (param $0 f32) (result f32)
(local $1 i32)
(local $2 i32)
(local $3 f32)
@ -2016,7 +2017,7 @@
)
(func $std/math/test_expf (; 17 ;) (type $fffiv) (param $0 f32) (param $1 f32) (param $2 f32) (param $3 i32)
(call $std/math/check<f32>
(call "$(lib)/math/Mathf.exp"
(call "$(lib)/math/NativeMathf.exp"
(get_local $0)
)
(get_local $1)
@ -2024,12 +2025,12 @@
(get_local $3)
)
)
(func "$(lib)/math/Math.sqrt" (; 18 ;) (type $FF) (param $0 f64) (result f64)
(func "$(lib)/math/NativeMath.sqrt" (; 18 ;) (type $FF) (param $0 f64) (result f64)
(f64.sqrt
(get_local $0)
)
)
(func "$(lib)/math/Math.pow" (; 19 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
(func "$(lib)/math/NativeMath.pow" (; 19 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
(local $2 f64)
(local $3 f64)
(local $4 i32)
@ -2385,7 +2386,7 @@
(i32.const 0)
)
(return
(call "$(lib)/math/Math.sqrt"
(call "$(lib)/math/NativeMath.sqrt"
(get_local $0)
)
)
@ -3450,7 +3451,7 @@
)
(func $std/math/test_pow (; 20 ;) (type $FFFFiv) (param $0 f64) (param $1 f64) (param $2 f64) (param $3 f64) (param $4 i32)
(call $std/math/check<f64>
(call "$(lib)/math/Math.pow"
(call "$(lib)/math/NativeMath.pow"
(get_local $0)
(get_local $1)
)
@ -3459,7 +3460,7 @@
(get_local $4)
)
)
(func "$(lib)/math/Mathf.pow" (; 21 ;) (type $fff) (param $0 f32) (param $1 f32) (result f32)
(func "$(lib)/math/NativeMathf.pow" (; 21 ;) (type $fff) (param $0 f32) (param $1 f32) (result f32)
(local $2 f32)
(local $3 f32)
(local $4 i32)
@ -4636,7 +4637,7 @@
)
(func $std/math/test_powf (; 22 ;) (type $ffffiv) (param $0 f32) (param $1 f32) (param $2 f32) (param $3 f32) (param $4 i32)
(call $std/math/check<f32>
(call "$(lib)/math/Mathf.pow"
(call "$(lib)/math/NativeMathf.pow"
(get_local $0)
(get_local $1)
)

View File

@ -1,6 +1,8 @@
// based on http://nsz.repo.hu/git/?p=libc-test
// mostly generated from their tests using scripts/hexfloat.html
assert(Math.E == NativeMath.E); // global alias should exist
const INEXACT = 1 << 0;
const INVALID = 1 << 1;
const DIVBYZERO = 1 << 2;
@ -190,7 +192,7 @@ test_fmodf(-1.750000000, -0.5000000000, -0.2500000000, 0.000000000, 0);
// ================================ Math.log ================================
function test_log(value: f64, expected: f64, error: f64, flags: i32): void {
check<f64>(Math.log(value), expected, error, flags);
check<f64>(NativeMath.log(value), expected, error, flags);
check<f64>(JSMath.log(value), expected, error, flags);
}
@ -219,7 +221,7 @@ test_log(NaN, NaN, 0.00000000000000000, 0);
// ================================ Mathf.log ================================
function test_logf(value: f32, expected: f32, error: f32, flags: i32): void {
check<f32>(Mathf.log(value), expected, error, flags);
check<f32>(NativeMathf.log(value), expected, error, flags);
}
// sanity
@ -245,7 +247,7 @@ test_logf(NaN, NaN, 0.000000000, 0);
// ================================ Math.exp ================================
function test_exp(value: f64, expected: f64, error: f64, flags: i32): void {
check<f64>(Math.exp(value), expected, error, flags);
check<f64>(NativeMath.exp(value), expected, error, flags);
check<f64>(JSMath.exp(value), expected, error, flags);
}
@ -277,7 +279,7 @@ test_exp(1.03972148895263672, 2.82842915587641164, -0.226183772087097168, INEXAC
// ================================ Mathf.exp ================================
function test_expf(value: f32, expected: f32, error: f32, flags: i32): void {
check<f32>(Mathf.exp(value), expected, error, flags);
check<f32>(NativeMathf.exp(value), expected, error, flags);
}
// sanity
@ -311,7 +313,7 @@ test_expf(0.3465736210, 1.414213657, 0.4321174324, INEXACT);
// ================================ Math.pow ================================
function test_pow(left: f64, right: f64, expected: f64, error: f64, flags: i32): void {
check<f64>(Math.pow(left, right), expected, error, flags);
check<f64>(NativeMath.pow(left, right), expected, error, flags);
}
// sanity
@ -422,7 +424,7 @@ test_pow(-2.00000000000000000, -1.00000000000000000, -0.500000000000000000, 0.00
// ================================ Mathf.pow ================================
function test_powf(left: f32, right: f32, expected: f32, error: f32, flags: i32): void {
check<f32>(Mathf.pow(left, right), expected, error, flags);
check<f32>(NativeMathf.pow(left, right), expected, error, flags);
}
// sanity

View File

@ -1,4 +1,5 @@
(module
(type $iiiiv (func (param i32 i32 i32 i32)))
(type $i (func (result i32)))
(type $FFFFiv (func (param f64 f64 f64 f64 i32)))
(type $FFF (func (param f64 f64) (result f64)))
@ -13,6 +14,7 @@
(type $FiF (func (param f64 i32) (result f64)))
(type $fif (func (param f32 i32) (result f32)))
(type $v (func))
(import "env" "abort" (func $abort (param i32 i32 i32 i32)))
(import "JSMath" "log" (func $(lib)/math/JSMath.log (param f64) (result f64)))
(import "JSMath" "exp" (func $(lib)/math/JSMath.exp (param f64) (result f64)))
(global $std/math/INEXACT i32 (i32.const 1))
@ -20,11 +22,12 @@
(global $std/math/DIVBYZERO i32 (i32.const 4))
(global $std/math/UNDERFLOW i32 (i32.const 8))
(global $std/math/OVERFLOW i32 (i32.const 16))
(global $HEAP_BASE i32 (i32.const 4))
(global $HEAP_BASE i32 (i32.const 32))
(memory $0 1)
(data (i32.const 4) "\0b\00\00\00s\00t\00d\00/\00m\00a\00t\00h\00.\00t\00s\00")
(export "memory" (memory $0))
(start $start)
(func $fmod (; 2 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
(func $fmod (; 3 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
(local $2 i64)
(local $3 i64)
(local $4 i32)
@ -479,7 +482,7 @@
)
)
)
(func $std/math/check<f64> (; 3 ;) (type $FFFiv) (param $0 f64) (param $1 f64) (param $2 f64) (param $3 i32)
(func $std/math/check<f64> (; 4 ;) (type $FFFiv) (param $0 f64) (param $1 f64) (param $2 f64) (param $3 i32)
(local $4 f64)
(local $5 i32)
(if
@ -536,7 +539,7 @@
)
)
)
(func $std/math/test_fmod (; 4 ;) (type $FFFFiv) (param $0 f64) (param $1 f64) (param $2 f64) (param $3 f64) (param $4 i32)
(func $std/math/test_fmod (; 5 ;) (type $FFFFiv) (param $0 f64) (param $1 f64) (param $2 f64) (param $3 f64) (param $4 i32)
(call $std/math/check<f64>
(call $fmod
(get_local $0)
@ -547,7 +550,7 @@
(get_local $4)
)
)
(func $fmodf (; 5 ;) (type $fff) (param $0 f32) (param $1 f32) (result f32)
(func $fmodf (; 6 ;) (type $fff) (param $0 f32) (param $1 f32) (result f32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
@ -983,7 +986,7 @@
)
)
)
(func $std/math/check<f32> (; 6 ;) (type $fffiv) (param $0 f32) (param $1 f32) (param $2 f32) (param $3 i32)
(func $std/math/check<f32> (; 7 ;) (type $fffiv) (param $0 f32) (param $1 f32) (param $2 f32) (param $3 i32)
(local $4 f32)
(local $5 i32)
(if
@ -1040,7 +1043,7 @@
)
)
)
(func $std/math/test_fmodf (; 7 ;) (type $ffffiv) (param $0 f32) (param $1 f32) (param $2 f32) (param $3 f32) (param $4 i32)
(func $std/math/test_fmodf (; 8 ;) (type $ffffiv) (param $0 f32) (param $1 f32) (param $2 f32) (param $3 f32) (param $4 i32)
(call $std/math/check<f32>
(call $fmodf
(get_local $0)
@ -1051,7 +1054,7 @@
(get_local $4)
)
)
(func "$(lib)/math/Math.log" (; 8 ;) (type $FF) (param $0 f64) (result f64)
(func "$(lib)/math/NativeMath.log" (; 9 ;) (type $FF) (param $0 f64) (result f64)
(local $1 i64)
(local $2 i32)
(local $3 i32)
@ -1364,9 +1367,9 @@
)
)
)
(func $std/math/test_log (; 9 ;) (type $FFFiv) (param $0 f64) (param $1 f64) (param $2 f64) (param $3 i32)
(func $std/math/test_log (; 10 ;) (type $FFFiv) (param $0 f64) (param $1 f64) (param $2 f64) (param $3 i32)
(call $std/math/check<f64>
(call "$(lib)/math/Math.log"
(call "$(lib)/math/NativeMath.log"
(get_local $0)
)
(get_local $1)
@ -1382,7 +1385,7 @@
(get_local $3)
)
)
(func "$(lib)/math/Mathf.log" (; 10 ;) (type $ff) (param $0 f32) (result f32)
(func "$(lib)/math/NativeMathf.log" (; 11 ;) (type $ff) (param $0 f32) (result f32)
(local $1 i32)
(local $2 i32)
(local $3 i32)
@ -1629,9 +1632,9 @@
)
)
)
(func $std/math/test_logf (; 11 ;) (type $fffiv) (param $0 f32) (param $1 f32) (param $2 f32) (param $3 i32)
(func $std/math/test_logf (; 12 ;) (type $fffiv) (param $0 f32) (param $1 f32) (param $2 f32) (param $3 i32)
(call $std/math/check<f32>
(call "$(lib)/math/Mathf.log"
(call "$(lib)/math/NativeMathf.log"
(get_local $0)
)
(get_local $1)
@ -1639,7 +1642,7 @@
(get_local $3)
)
)
(func "$(lib)/math/scalbn" (; 12 ;) (type $FiF) (param $0 f64) (param $1 i32) (result f64)
(func "$(lib)/math/scalbn" (; 13 ;) (type $FiF) (param $0 f64) (param $1 i32) (result f64)
(local $2 f64)
(nop)
(set_local $2
@ -1760,7 +1763,7 @@
)
)
)
(func "$(lib)/math/Math.exp" (; 13 ;) (type $FF) (param $0 f64) (result f64)
(func "$(lib)/math/NativeMath.exp" (; 14 ;) (type $FF) (param $0 f64) (result f64)
(local $1 i32)
(local $2 f64)
(local $3 f64)
@ -2009,9 +2012,9 @@
)
)
)
(func $std/math/test_exp (; 14 ;) (type $FFFiv) (param $0 f64) (param $1 f64) (param $2 f64) (param $3 i32)
(func $std/math/test_exp (; 15 ;) (type $FFFiv) (param $0 f64) (param $1 f64) (param $2 f64) (param $3 i32)
(call $std/math/check<f64>
(call "$(lib)/math/Math.exp"
(call "$(lib)/math/NativeMath.exp"
(get_local $0)
)
(get_local $1)
@ -2027,7 +2030,7 @@
(get_local $3)
)
)
(func "$(lib)/math/scalbnf" (; 15 ;) (type $fif) (param $0 f32) (param $1 i32) (result f32)
(func "$(lib)/math/scalbnf" (; 16 ;) (type $fif) (param $0 f32) (param $1 i32) (result f32)
(local $2 f32)
(nop)
(set_local $2
@ -2146,7 +2149,7 @@
)
)
)
(func "$(lib)/math/Mathf.exp" (; 16 ;) (type $ff) (param $0 f32) (result f32)
(func "$(lib)/math/NativeMathf.exp" (; 17 ;) (type $ff) (param $0 f32) (result f32)
(local $1 i32)
(local $2 i32)
(local $3 i32)
@ -2367,9 +2370,9 @@
)
)
)
(func $std/math/test_expf (; 17 ;) (type $fffiv) (param $0 f32) (param $1 f32) (param $2 f32) (param $3 i32)
(func $std/math/test_expf (; 18 ;) (type $fffiv) (param $0 f32) (param $1 f32) (param $2 f32) (param $3 i32)
(call $std/math/check<f32>
(call "$(lib)/math/Mathf.exp"
(call "$(lib)/math/NativeMathf.exp"
(get_local $0)
)
(get_local $1)
@ -2377,14 +2380,14 @@
(get_local $3)
)
)
(func "$(lib)/math/Math.sqrt" (; 18 ;) (type $FF) (param $0 f64) (result f64)
(func "$(lib)/math/NativeMath.sqrt" (; 19 ;) (type $FF) (param $0 f64) (result f64)
(return
(f64.sqrt
(get_local $0)
)
)
)
(func "$(lib)/math/Math.pow" (; 19 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
(func "$(lib)/math/NativeMath.pow" (; 20 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
(local $2 i64)
(local $3 i32)
(local $4 i32)
@ -2782,7 +2785,7 @@
(i32.const 0)
)
(return
(call "$(lib)/math/Math.sqrt"
(call "$(lib)/math/NativeMath.sqrt"
(get_local $0)
)
)
@ -3984,9 +3987,9 @@
)
)
)
(func $std/math/test_pow (; 20 ;) (type $FFFFiv) (param $0 f64) (param $1 f64) (param $2 f64) (param $3 f64) (param $4 i32)
(func $std/math/test_pow (; 21 ;) (type $FFFFiv) (param $0 f64) (param $1 f64) (param $2 f64) (param $3 f64) (param $4 i32)
(call $std/math/check<f64>
(call "$(lib)/math/Math.pow"
(call "$(lib)/math/NativeMath.pow"
(get_local $0)
(get_local $1)
)
@ -3995,7 +3998,7 @@
(get_local $4)
)
)
(func "$(lib)/math/Mathf.pow" (; 21 ;) (type $fff) (param $0 f32) (param $1 f32) (result f32)
(func "$(lib)/math/NativeMathf.pow" (; 22 ;) (type $fff) (param $0 f32) (param $1 f32) (result f32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
@ -5335,9 +5338,9 @@
)
)
)
(func $std/math/test_powf (; 22 ;) (type $ffffiv) (param $0 f32) (param $1 f32) (param $2 f32) (param $3 f32) (param $4 i32)
(func $std/math/test_powf (; 23 ;) (type $ffffiv) (param $0 f32) (param $1 f32) (param $2 f32) (param $3 f32) (param $4 i32)
(call $std/math/check<f32>
(call "$(lib)/math/Mathf.pow"
(call "$(lib)/math/NativeMathf.pow"
(get_local $0)
(get_local $1)
)
@ -5346,7 +5349,24 @@
(get_local $4)
)
)
(func $start (; 23 ;) (type $v)
(func $start (; 24 ;) (type $v)
(if
(i32.eqz
(f64.eq
(f64.const 2.718281828459045)
(f64.const 2.718281828459045)
)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 4)
(i32.const 0)
)
(unreachable)
)
)
(call $std/math/test_fmod
(f64.const -8.06684839057968)
(f64.const 4.535662560676869)