[Help] Trying to rewrite the NetBSD kernel using Haskell language.

Hi Haskell hackers, We start trying to rewrite NetBSD kernel using Haskell language. https://github.com/metasepi/netbsd-arafura-s1 But we find some problems. Could you have any ideas for them? ## Status * Trying rewrite AC''97 sound driver using Haskell * Before rewriting. (C language) https://github.com/metasepi/netbsd-arafura-s1/blob/d1b19c686de5573ef8a8342a3... * After rewriting. (Haskell language) https://github.com/metasepi/netbsd-arafura-s1/blob/d1b19c686de5573ef8a8342a3... * Have rewrited 2 functions, auich_open() and auich_close() * Not yet touch Haskell heap ## Problems Some time, C programmer writes following code. ~~~ static int auich_open(void *addr, int flags) { struct auich_softc *sc; sc = (struct auich_softc *)addr; mutex_spin_exit(&sc->sc_intr_lock); sc->codec_if->vtbl->lock(sc->codec_if); ~~~ Note the line "sc->codec_if->vtbl->lock(sc->codec_if);". It traces pointer tree regionally. Haskell's Strorable class is not good for the use case, because it copies the entire pointer tree! We avoid the problem using following messy code. https://github.com/metasepi/netbsd-arafura-s1/blob/d1b19c686de5573ef8a8342a3... ~~~ auichOpen :: Ptr AuichSoftc -> Int -> IO Int auichOpen sc flags = do mutexp <- p_AuichSoftc_sc_intr_lock sc codeif <- peek =<< p_AuichSoftc_codec_if sc lock <- peek =<< p_Ac97CodecIfVtbl_lock =<< peek =<< p_Ac97CodecIf_vtbl codeif mutexSpinExit mutexp call_Ac97CodecIfVtbl_lock lock codeif -- snip -- p_AuichSoftc_sc_intr_lock :: Ptr AuichSoftc -> IO (Ptr KmutexT) p_AuichSoftc_sc_intr_lock p = return $ plusPtr p offsetOf_AuichSoftc_sc_intr_lock p_AuichSoftc_codec_if :: Ptr AuichSoftc -> IO (Ptr (Ptr Ac97CodecIf)) p_AuichSoftc_codec_if p = return $ plusPtr p offsetOf_AuichSoftc_codec_if p_Ac97CodecIf_vtbl :: Ptr Ac97CodecIf -> IO (Ptr (Ptr Ac97CodecIfVtbl)) p_Ac97CodecIf_vtbl p = return $ plusPtr p offsetOf_Ac97CodecIf_vtbl p_Ac97CodecIfVtbl_lock :: Ptr Ac97CodecIfVtbl -> IO (Ptr (FunPtr Ac97CodecIfVtbl_lock)) p_Ac97CodecIfVtbl_lock p = return $ plusPtr p offsetOf_Ac97CodecIfVtbl_lock ~~~ Haskell world has the answer for it already? Best regards, -- Kiwamu Okabe

quoth Kiwamu Okabe>, ...
static int auich_open(void *addr, int flags) { struct auich_softc *sc;
sc = (struct auich_softc *)addr; mutex_spin_exit(&sc->sc_intr_lock); sc->codec_if->vtbl->lock(sc->codec_if); ~~~
Note the line "sc->codec_if->vtbl->lock(sc->codec_if);". It traces pointer tree regionally. Haskell's Strorable class is not good for the use case, because it copies the entire pointer tree!
That suggests to me that you should change the class, so that its Storable instance doesn't need to dereference the pointers - that is, vtbl for example would be a Ptr type. Though that would not save you very much trouble in the present case. Are you using hsc2hs? Your example has a few things like "plusPtr p offsetOf_Ac97CodecIf_vtbl" - where do you get that offsetOf_? hsc2hs has a #peek macro that does this: p_Ac97CodecIf_vtbl p = (#peek struct ac97codecxzy, vtbl) p -- creates Haskell code p_Ac97CodecIf_vtbl p = ((\ hsc_ptr -> peekByteOff hsc_ptr 96) p Hope that may help a little! Donn

Hi Donn,
Do you know c2hs and GreenCard?
I don't know the details of them.
http://blog.ezyang.com/2010/06/the-haskell-preprocessor-hierarchy/
On Tue, Jan 21, 2014 at 1:27 AM, Donn Cave
That suggests to me that you should change the class, so that its Storable instance doesn't need to dereference the pointers - that is, vtbl for example would be a Ptr type. Though that would not save you very much trouble in the present case.
Yes. I will try to write the own Ptr type and Storable class. Thank's.
Are you using hsc2hs? Your example has a few things like "plusPtr p offsetOf_Ac97CodecIf_vtbl" - where do you get that offsetOf_? hsc2hs has a #peek macro that does this:
p_Ac97CodecIf_vtbl p = (#peek struct ac97codecxzy, vtbl) p -- creates Haskell code p_Ac97CodecIf_vtbl p = ((\ hsc_ptr -> peekByteOff hsc_ptr 96) p
Yes, I know it. I read Read World Haskell book. But there are two problems. First, jhc has own Foreign Primitives. http://ajhc.metasepi.org/manual.html#foreign-primitives The "const. C_CONSTANT" can embed a C expression in Haskell code. It understands C's macros (#define). Very powerful. Second, The hsc2hs is not good for cross compiling. If I miss to choose "--cc" option, I will watch funny BUG... Thank's, -- Kiwamu Okabe
participants (2)
-
Donn Cave
-
Kiwamu Okabe