mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-12-08 20:38:47 +00:00
Add recognition and classification of WebAssembly (#3650)
This commit is contained in:
14
samples/WebAssembly/add.wat
Normal file
14
samples/WebAssembly/add.wat
Normal file
@@ -0,0 +1,14 @@
|
||||
(module
|
||||
(import "env" "printInt" (func $printInt (param i32)))
|
||||
(func $add (param $lhs i32) (param $rhs i32) (result i32)
|
||||
get_local $lhs
|
||||
get_local $rhs
|
||||
i32.add
|
||||
)
|
||||
|
||||
(func $main
|
||||
(call $printInt
|
||||
(call $add (i32.const 9) (i32.const 8))))
|
||||
|
||||
(export "main" (func $main))
|
||||
)
|
||||
41
samples/WebAssembly/fibonacci.wat
Normal file
41
samples/WebAssembly/fibonacci.wat
Normal file
@@ -0,0 +1,41 @@
|
||||
(module
|
||||
(import "env" "printInt" (func $printInt (param i32)))
|
||||
(import "env" "print" (func $print (param i32 i32)))
|
||||
|
||||
(memory $memory 1)
|
||||
(data (i32.const 0) "\n")
|
||||
(data (i32.const 1) " ")
|
||||
|
||||
(func $endl
|
||||
(call $print (i32.const 0) (i32.const 1)))
|
||||
(func $space
|
||||
(call $print (i32.const 1) (i32.const 1)))
|
||||
|
||||
(func $fibonacci_rec (param $a i32) (param $b i32) (param $n i32) (result i32)
|
||||
(if (i32.eqz (get_local $n)) (return (get_local $a)))
|
||||
(call $printInt (get_local $b))
|
||||
(call $space)
|
||||
(set_local $a (i32.add (get_local $a) (get_local $b)))
|
||||
(call $fibonacci_rec (get_local $b) (get_local $a) (i32.sub (get_local $n) (i32.const 1)))
|
||||
)
|
||||
|
||||
(func $fibonacci_iter (param $a i32) (param $b i32) (param $n i32) (result i32)
|
||||
(loop $fi
|
||||
(if (i32.eqz (get_local $n)) (return (get_local $a)))
|
||||
(call $printInt (get_local $b))
|
||||
(call $space)
|
||||
(set_local $b (i32.add (get_local $a) (get_local $b)))
|
||||
(set_local $a (i32.sub (get_local $b) (get_local $a)))
|
||||
(set_local $n (i32.sub (get_local $n) (i32.const 1)))
|
||||
(br $fi))
|
||||
(get_local $b))
|
||||
|
||||
(func $main
|
||||
(drop (call $fibonacci_rec (i32.const 0) (i32.const 1) (i32.const 9)))
|
||||
(call $endl)
|
||||
(drop (call $fibonacci_iter (i32.const 0) (i32.const 1) (i32.const 9))))
|
||||
|
||||
(export "main" (func $main))
|
||||
(export "memory" (memory $memory))
|
||||
)
|
||||
|
||||
48
samples/WebAssembly/imported-min.wast
Normal file
48
samples/WebAssembly/imported-min.wast
Normal file
@@ -0,0 +1,48 @@
|
||||
(module
|
||||
(memory 256 256)
|
||||
(data (i32.const 10) "waka waka waka waka waka")
|
||||
;; stack imports are special
|
||||
(import "env" "STACKTOP" (global $STACKTOP$asm2wasm$import i32))
|
||||
(import "env" "STACK_MAX" (global $STACK_MAX$asm2wasm$import i32))
|
||||
;; other imports must not be touched!
|
||||
(import "env" "tempDoublePtr" (global $tempDoublePtr i32))
|
||||
(export "test1" $test1)
|
||||
(export "test2" $test2)
|
||||
(export "test3" $test3)
|
||||
;; ok to modify a global, if we keep it the same value
|
||||
(global $mine (mut i32) (i32.const 1))
|
||||
;; stack imports are ok to use. their uses are the same as other
|
||||
;; globals - must keep the same value (which means, unwind the stack)
|
||||
;; here the global names are "minified"
|
||||
(global $global0 (mut i32) (get_global $STACKTOP$asm2wasm$import))
|
||||
(global $global1 (mut i32) (get_global $STACK_MAX$asm2wasm$import))
|
||||
;; a global initialized by an import, so bad, but ok if not used
|
||||
(global $do-not-use (mut i32) (get_global $tempDoublePtr))
|
||||
(func $test1
|
||||
(local $temp i32)
|
||||
(set_global $mine (i32.const 1))
|
||||
(set_local $temp (get_global $global0))
|
||||
(set_global $global0 (i32.const 1337)) ;; bad
|
||||
(set_global $global0 (get_local $temp)) ;; save us
|
||||
(set_global $global1 (i32.const 913370)) ;; bad
|
||||
(set_global $global1 (get_local $temp)) ;; save us
|
||||
;; use the stack memory
|
||||
(i32.store (get_local $temp) (i32.const 1337))
|
||||
(if
|
||||
(i32.ne
|
||||
(i32.load (get_local $temp))
|
||||
(i32.const 1337)
|
||||
)
|
||||
(unreachable) ;; they should be equal, never get here
|
||||
)
|
||||
;; finally, do a valid store
|
||||
(i32.store8 (i32.const 12) (i32.const 115))
|
||||
)
|
||||
(func $test2
|
||||
(set_global $tempDoublePtr (i32.const 1)) ;; bad!
|
||||
(i32.store8 (i32.const 13) (i32.const 115))
|
||||
)
|
||||
(func $test3
|
||||
(i32.store8 (i32.const 14) (i32.const 115))
|
||||
)
|
||||
)
|
||||
164
samples/WebAssembly/local-cse.wast
Normal file
164
samples/WebAssembly/local-cse.wast
Normal file
@@ -0,0 +1,164 @@
|
||||
(module
|
||||
(memory 100 100)
|
||||
(func $basics
|
||||
(local $x i32)
|
||||
(local $y i32)
|
||||
(drop
|
||||
(i32.add (i32.const 1) (i32.const 2))
|
||||
)
|
||||
(drop
|
||||
(i32.add (i32.const 1) (i32.const 2))
|
||||
)
|
||||
(if (i32.const 0) (nop))
|
||||
(drop ;; we can't do this yet, non-linear
|
||||
(i32.add (i32.const 1) (i32.const 2))
|
||||
)
|
||||
(drop
|
||||
(i32.add (get_local $x) (get_local $y))
|
||||
)
|
||||
(drop
|
||||
(i32.add (get_local $x) (get_local $y))
|
||||
)
|
||||
(drop
|
||||
(i32.add (get_local $x) (get_local $y))
|
||||
)
|
||||
(call $basics) ;; side effects, but no matter for our locals
|
||||
(drop
|
||||
(i32.add (get_local $x) (get_local $y))
|
||||
)
|
||||
(set_local $x (i32.const 100))
|
||||
(drop ;; x was changed!
|
||||
(i32.add (get_local $x) (get_local $y))
|
||||
)
|
||||
)
|
||||
(func $recursive1
|
||||
(local $x i32)
|
||||
(local $y i32)
|
||||
(drop
|
||||
(i32.add
|
||||
(i32.const 1)
|
||||
(i32.add
|
||||
(i32.const 2)
|
||||
(i32.const 3)
|
||||
)
|
||||
)
|
||||
)
|
||||
(drop
|
||||
(i32.add
|
||||
(i32.const 1)
|
||||
(i32.add
|
||||
(i32.const 2)
|
||||
(i32.const 3)
|
||||
)
|
||||
)
|
||||
)
|
||||
(drop
|
||||
(i32.add
|
||||
(i32.const 2)
|
||||
(i32.const 3)
|
||||
)
|
||||
)
|
||||
)
|
||||
(func $recursive2
|
||||
(local $x i32)
|
||||
(local $y i32)
|
||||
(drop
|
||||
(i32.add
|
||||
(i32.const 1)
|
||||
(i32.add
|
||||
(i32.const 2)
|
||||
(i32.const 3)
|
||||
)
|
||||
)
|
||||
)
|
||||
(drop
|
||||
(i32.add
|
||||
(i32.const 2)
|
||||
(i32.const 3)
|
||||
)
|
||||
)
|
||||
(drop
|
||||
(i32.add
|
||||
(i32.const 1)
|
||||
(i32.add
|
||||
(i32.const 2)
|
||||
(i32.const 3)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
(func $self
|
||||
(local $x i32)
|
||||
(local $y i32)
|
||||
(drop
|
||||
(i32.add
|
||||
(i32.add
|
||||
(i32.const 2)
|
||||
(i32.const 3)
|
||||
)
|
||||
(i32.add
|
||||
(i32.const 2)
|
||||
(i32.const 3)
|
||||
)
|
||||
)
|
||||
)
|
||||
(drop
|
||||
(i32.add
|
||||
(i32.const 2)
|
||||
(i32.const 3)
|
||||
)
|
||||
)
|
||||
)
|
||||
(func $loads
|
||||
(drop
|
||||
(i32.load (i32.const 10))
|
||||
)
|
||||
(drop
|
||||
(i32.load (i32.const 10)) ;; implicit traps, sad
|
||||
)
|
||||
)
|
||||
(func $8 (param $var$0 i32) (result i32)
|
||||
(local $var$1 i32)
|
||||
(local $var$2 i32)
|
||||
(local $var$3 i32)
|
||||
(block $label$0 i32
|
||||
(i32.store
|
||||
(tee_local $var$2
|
||||
(i32.add
|
||||
(get_local $var$1)
|
||||
(i32.const 4)
|
||||
)
|
||||
)
|
||||
(i32.and
|
||||
(i32.load
|
||||
(get_local $var$2)
|
||||
)
|
||||
(i32.xor
|
||||
(tee_local $var$2
|
||||
(i32.const 74)
|
||||
)
|
||||
(i32.const -1)
|
||||
)
|
||||
)
|
||||
)
|
||||
(i32.store
|
||||
(tee_local $var$1
|
||||
(i32.add
|
||||
(get_local $var$1)
|
||||
(i32.const 4)
|
||||
)
|
||||
)
|
||||
(i32.or
|
||||
(i32.load
|
||||
(get_local $var$1)
|
||||
)
|
||||
(i32.and
|
||||
(get_local $var$2)
|
||||
(i32.const 8)
|
||||
)
|
||||
)
|
||||
)
|
||||
(i32.const 0)
|
||||
)
|
||||
)
|
||||
)
|
||||
23
samples/WebAssembly/print.wat
Normal file
23
samples/WebAssembly/print.wat
Normal file
@@ -0,0 +1,23 @@
|
||||
(module
|
||||
(import "env" "printInt" (func $printInt (param i32)))
|
||||
(import "env" "printFloat" (func $printFloat (param f32)))
|
||||
(import "env" "print" (func $print (param i32 i32)))
|
||||
|
||||
(memory $memory 1)
|
||||
(data (i32.const 0) "\n")
|
||||
(data (i32.const 1) "Hello World!")
|
||||
|
||||
(func $endl
|
||||
(call $print (i32.const 0) (i32.const 1)))
|
||||
|
||||
(func $main
|
||||
(call $printInt (i32.const 9))
|
||||
(call $endl)
|
||||
(call $printFloat (f32.const 6.28))
|
||||
(call $endl)
|
||||
(call $print (i32.const 1) (i32.const 12))
|
||||
)
|
||||
|
||||
(export "main" (func $main))
|
||||
(export "memory" (memory $memory))
|
||||
)
|
||||
@@ -0,0 +1,81 @@
|
||||
(module
|
||||
(memory 256 256)
|
||||
(type $0 (func (param i32)))
|
||||
(type $1 (func))
|
||||
(type $2 (func (result i32)))
|
||||
(func $b14 (type $2)
|
||||
(drop
|
||||
(if i32 ;; with shrinking, this can become a select
|
||||
(i32.const 1)
|
||||
(block $block1 i32
|
||||
(i32.const 12)
|
||||
)
|
||||
(block $block3 i32
|
||||
(i32.const 27)
|
||||
)
|
||||
)
|
||||
)
|
||||
(drop
|
||||
(if i32
|
||||
(i32.const 1)
|
||||
(i32.load (i32.const 10)) ;; load may have side effects, unless ignored
|
||||
(i32.const 27)
|
||||
)
|
||||
)
|
||||
(drop
|
||||
(if i32
|
||||
(i32.const 1)
|
||||
(i32.rem_s (i32.const 11) (i32.const 12)) ;; rem may have side effects, unless ignored
|
||||
(i32.const 27)
|
||||
)
|
||||
)
|
||||
(drop
|
||||
(if i32
|
||||
(i32.const 1)
|
||||
(i32.trunc_u/f64 (f64.const 12.34)) ;; float to int may have side effects, unless ignored
|
||||
(i32.const 27)
|
||||
)
|
||||
)
|
||||
(i32.const 0)
|
||||
)
|
||||
(func $join-br_ifs
|
||||
(block $out
|
||||
(br_if $out (i32.const 1))
|
||||
(br_if $out (i32.const 2))
|
||||
(br_if $out (i32.const 3))
|
||||
)
|
||||
(block $out2
|
||||
(block $out3
|
||||
(br_if $out2 (i32.const 1))
|
||||
(br_if $out3 (i32.const 2))
|
||||
(br_if $out2 (i32.const 3))
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(block $out4
|
||||
(block $out5
|
||||
(br_if $out4 (i32.const 1))
|
||||
(br_if $out5 (i32.const 2))
|
||||
(br_if $out5 (i32.const 3))
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(block $out6
|
||||
(block $out7
|
||||
(br_if $out6 (i32.const 1))
|
||||
(br_if $out6 (i32.const 2))
|
||||
(br_if $out7 (i32.const 3))
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(block $out8
|
||||
(br_if $out8 (call $b14)) ;; side effect
|
||||
(br_if $out8 (i32.const 0))
|
||||
)
|
||||
(block $out8
|
||||
(br_if $out8 (i32.const 1))
|
||||
(br_if $out8 (call $b14)) ;; side effect
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user