Mapping primary operations from GHC to ETA

I am very excited about ETA (Haskell on JVM) :)
I wanted to know how low level C apis of GHC are translated in ETA's Java byte code. I found that ETA uses codev-jvm for generating java byte code.

Lets take example of `isEmptyMVar`.

This is the code in both ETA and GHC :

eta code and ghc code :

isEmptyMVar :: MVar a -> IO Bool
isEmptyMVar (MVar mv#) = IO $ \ s# ->
     case isEmptyMVar# mv# s# of
        (# s2#, flg #) -> (# s2#, isTrue# (flg /=# 0#) #)

Note the # in isEmptyMVar#.
`#` is used for signifying that we use lower level primitive operations.

So, where is that operation `isEmptyMVar#`:

In ghc/rts/primops.cmm:

stg_isEmptyMVarzh ( P_ mvar /* :: MVar a */ )
{
 if (StgMVar_value(mvar) == stg_END_TSO_QUEUE_closure) {
    return (1);
 } else {
    return (0);
 }
}

and in compiler/eta/codegen/prim.hs

shouldInlinePrimOp' _ IsEmptyMVarOp [mvar] = Right $ return 
   [ intCompOp ifnull [mvar <> mVarValue] ]

`intCompOp` returns 1 if operation is true and 0 if it is false.
and `mvar <> mVarValue` is corresponding implementation of `StgMVar_value(mvar)`. :)

So, where is the mapping of `isEmptyMVar#` to `stg_isEmptyMVarzh` and `IsEmptyMVarOp` ?

It is ghc/compiler/prelude/primops.txt.pp and eta/compiler/eta/prelude/primop.hs :

 IsEmptyMVarOp "isEmptyMVar#" GenPrimOp
 MVar# s a -> State# s -> (# State# s, Int# #)
 {Return 1 if {\tt MVar\#} is empty; 0 otherwise.}
 with
 out_of_line = True
 has_side_effects = True

and
primOpInfo IsEmptyMVarOp = mkGenPrimOp (fsLit "isEmptyMVar#")
    [deltaTyVar, alphaTyVar]
    [mkMVarPrimTy deltaTy alphaTy, mkStatePrimTy deltaTy]
    ((mkTupleTy UnboxedTuple [mkStatePrimTy deltaTy, intPrimTy]))

If you are thinking about missing `stg_` in primops.txt.pp, then have a look at whole process of transformation.
and i think that magic happens at ghc/compiler/cmm/clabel.hs :

pprCLbl (RtsLabel (RtsPrimOp primop))
     = text "stg_" <> ppr primop

Comments