summaryrefslogtreecommitdiff
path: root/compiler/GHC/Wasm/ControlFlow.hs
blob: 365a003323285cfb8aeb36575f10eb0953eaa902 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE DataKinds, GADTs, RankNTypes, KindSignatures #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE PatternSynonyms #-}

module GHC.Wasm.ControlFlow
  ( WasmControl(..), (<>), pattern WasmIf
  , BrTableInterval(..), inclusiveInterval

  , WasmType, WasmTypeTag(..)
  , TypeList(..)
  , WasmFunctionType(..)
  )
where

import GHC.Prelude

import GHC.CmmToAsm.Wasm.Types
import GHC.Utils.Panic

{-|
Module      : GHC.Wasm.ControlFlow
Description : Representation of control-flow portion of the WebAssembly instruction set
-}

inclusiveInterval :: Integer -> Integer -> BrTableInterval
inclusiveInterval lo hi
    | lo <= hi = let count = hi - lo + 1
                 in  BrTableInterval lo count
    | otherwise = panic "GHC.Wasm.ControlFlow: empty interval"

(<>) :: forall s e pre mid post
      . WasmControl s e pre mid
     -> WasmControl s e mid post
     -> WasmControl s e pre post
(<>) = WasmSeq
-- N.B. Fallthrough can't be optimized away because of type checking.



-- Syntactic sugar.
pattern WasmIf :: WasmFunctionType pre post
               -> e
               -> WasmControl s e pre post
               -> WasmControl s e pre post
               -> WasmControl s e pre post

pattern WasmIf ty e t f =
    WasmPush TagI32 e `WasmSeq` WasmIfTop ty t f