找到功能的GHC程序集

时间:2014-12-15 23:27:44

标签: haskell assembly ghc

我想确定GHC为给定函数生成的汇编。

这里,例如,是一些代码(应该)在一个字中旋转位 - 它将位0移动到位12,位12到14,位14移回0,类似位置1,18,13和6。

rot0cw生成的.S文件中查找为ghc -O2 -S ...生成的程序集的最佳方法是什么?

我已阅读this answer,但我在汇编输出中看不到任何..._rot0cw_closure

import Data.Bits
import Data.Int (Int64)
import Text.Printf
import System.Environment

{-# INLINE maskbits #-}
-- set in word v the bits of b corresponding to the mask m
-- assume a bit in b is zero if not in the mask
maskbits v m b = (v .&. (complement m) .|. b)

{-# INLINE tb #-}
-- transfer bit i of word v to bit j of word m; assume bit j of m is 0
tb v i j m =  m .|. (rotate  (v .&. (bit i)) (j-i))

rot0cw :: Int64 -> Int64
rot0cw v = maskbits (maskbits v m1 b1) m2 b2
  where
    m1 = 0x0000005005
    b1 = tb v 0 2 . tb v 2 14 . tb v 14 12 . tb v 12 0 $ 0
    m2 = 0x0000002142
    b2 = tb v 1 8 . tb v 8 13 . tb v 13 6 . tb v 6 1 $ 0

showBits v = 
  let set = [ i | i <- [0..35], testBit v i ]
  in "bits set: " ++ (unwords $ map show set)

main = do
  (arg0:_) <- getArgs
  let v = read arg0 
  -- let v = 0x0000000005
  let v' = rot0cw v
  putStrLn $ printf "v  = 0x%010x = %12d %s" v v (showBits v)
  putStrLn $ printf "v' = 0x%010x = %12d %s" v' v' (showBits v')

2 个答案:

答案 0 :(得分:5)

  

我已经阅读了这个答案,但我在程序集输出中看不到任何..._rot0cw_closure

您需要为模块命名。例如在开头*添加module Main where以在生成的程序集中获取Main_rot0cw_closure

*严格来说,您的模块需要导出该功能。

答案 1 :(得分:4)

您需要从模块中导出函数rot0cw,否则它实际上是死代码。所以在模块的顶部添加这样的东西:

module Asm(rot0cw) where

它生成的程序集相当大,如果你转储Cmm -ddump-cmm,你可以查看每个标有你感兴趣的函数名称的proc块。生成的基本块GHC这里大致对应于生成的组件中的标签。例如,rot0cw_closure的条目代码映射到以下内容:

     cB2:
         if (Sp - 32 < SpLim) goto cB4;
         Hp = Hp + 16;
         if (Hp > HpLim) goto cB6;
         I64[Sp - 16] = stg_upd_frame_info;
         I64[Sp - 8] = R1;
         I64[Hp - 8] = S#_con_info;
         I64[Hp + 0] = 0;
         I64[Sp - 24] = Hp - 7;
         I64[Sp - 32] = stg_ap_p_info;
         R2 = $fNumInt64_closure;
         Sp = Sp - 32;
         jump fromInteger_info; // [R2]
     cB4: jump stg_gc_enter_1; // [R1]
     cB6:
         HpAlloc = 16;
         goto cB4;

产生于:

_cB2:
    leaq -32(%rbp),%rax
    cmpq %r15,%rax
    jb _cB4
    addq $16,%r12
    cmpq 144(%r13),%r12
    ja _cB6
    movq $stg_upd_frame_info,-16(%rbp)
    movq %rbx,-8(%rbp)
    movq $S#_con_info,-8(%r12)
    movq $0,0(%r12)
    leaq -7(%r12),%rax
    movq %rax,-24(%rbp)
    movq $stg_ap_p_info,-32(%rbp)
    movl $$fNumInt64_closure,%r14d
    addq $-32,%rbp
    jmp fromInteger_info
_cB6:
    movq $16,192(%r13)
_cB4:
    jmp *-16(%r13)
    .size sat_info, .-sat_info

你总是会得到很多&#34;噪音&#34;来自所有GC函数和各种运行时调用,以及不透明的预定义闭包对象。但如果你真的想深入研究生成的Cmm,那么首先要走的路。