Save 37% off PRO during our Black Friday Sale! »

HaskellでLLVM Frontend

800912a73ce4e879003b6c89cf80cfeb?s=47 yagi
May 11, 2014

HaskellでLLVM Frontend

parsecサイコー。llvm-general難しい

800912a73ce4e879003b6c89cf80cfeb?s=128

yagi

May 11, 2014
Tweet

Transcript

  1. HaskellͰLLVM Frontend 2014/5/11 Toshihiro Yagi

  2. About me Twitter : @sys1yagi Blog : http://sys1yagi.com ! !

    Android Engineer at
  3. Status • ͍͢͝HຊΛಡॻձͰಡΜͩ • Haskell͕ଟগಡΊΔΑ͏ʹͳͬͨ • MaybeϞφυ͸ཧղ͍ͯ͠Δɻଟ෼ • ීஈॻ͍͍ͯΔίʔυ͸98%͘Β͍Java

  4. ΍Ζ͏ͱࢥͬͯͨࣄ • EBNFύʔαΛ࡞Δ • EBNFΛύʔεͯ͠ੜ੒ͨ͠ASTΛ࢖ͬͯLLVM IRΛ ు͘ϞδϡʔϧΛ࡞Δ • EBNFϑΝΠϧͱɺLLVM IRు͘ϞδϡʔϧͰͳΜ

    ͔͍͍ײ͡ʹίϯύΠϧ͢Δ
  5. ΍ͬͨࣄ • ʮ48࣌ؒͰSchemeΛॻ͜͏ʯͰSchemeͷΠϯλϓϦ λΛREPLลΓ·Ͱ࣮૷ • Implementing a JIT Compiled Language

    with Haskell and LLVMͰṖݴޠΛLLVM IRʹίϯύΠϧ͢ΔౕΛ৮Δ • SchemeΛίϯύΠϧͯ͠LLVM IRʹు͘Α͏ʹͨ͠(ͱ ͍ͬͯ΋਺஋ܭࢉ෦෼ͷΈ) • LLVM IRΛemscriptenͰjsʹ͢Δ http://ja.wikibooks.org/wiki/48%E6%99%82%E9%96%93%E3%81%A7Scheme%E3%82%92%E6%9B%B8%E3%81%93%E3%81%86 http://www.stephendiehl.com/llvm/
  6. Demo https://github.com/sys1yagi/scheme-haskell-llvm

  7. Environment • Mac Book Pro OSX 10.9.2 • GHC 7.6.3

    • Cabal 1.20.0.1 • Sublime Text 2
  8. Scheme • ParsecͰΰϦΰϦɻ • ϦετදݱɺϓϦϛςΟϒͳؔ਺ͱifࣜ·Ͱ࣮૷ • REPLͰಈ͘ • ΄΅͜Εˣ http://ja.wikibooks.org/wiki/48%E6%99%82%E9%96%93%E3%81%A7Scheme%E3%82%92%E6%9B%B8%E3%81%93%E3%81%86

  9. Parsec • ੜ࢈ੑ͕ߴ͘ϞφσΟοΫͳύʔαίϯϏωʔλɻ ඪ४ϥΠϒϥϦɻBNFΆ͍هड़Ͱॻ͚Δ • <|> : ࣈ۟ղੳΛ͢ΔύʔαΛͭͳ͛Δ ex: var

    | val • many : ࢦఆͨ͠ύλʔϯʹ0ճҎ্Ϛον͢Δɻ ex:{ args } • many1 : ࢦఆͨ͠ύλʔϯʹ1ճҎ্Ϛον͢Δ ex:{ Uppercase }+ • optional : ࢦఆͨ͠ύλʔϯ͕1ճ͋ͬͯ΋ͳͯ͘΋Մ ex: [ args ] • try : ࢦఆͨ͠ύλʔϯʹϚον͠ͳ͍৔߹όοϑΝΛר͖໭ͯ࣍͠ͷύλʔϯ ͷϚονΛࢼΈΔ
  10. γϯϘϧΛఆٛ ύʔαΛฒ΂Δ ύʔαΛॻ͘

  11. LLVM • http://www.stephendiehl.com/llvm/ Λࢀߟʹɻ • LLVM3.4ΛίϯύΠϧͯ͠Πϯετʔϧ • llvm-general, llvm-general-pureͱ͍͏packageΛ࢖͏ •

    ࡞ͬͨASTΛḷͬͯLLVM IRΛు͔ͤΔ • emscriptenΛΠϯετʔϧɺemscriptenଆ͸fastcomp ͱ͍͏LLVM࣮૷Λ࢖͏
  12. ৭ʑLLVMͷઃఆͱ͔ॻ͍ͯ ASTḷͬͯ.llΛు͘

  13. શମ૾ Main.hs Syntax.hs Lexer.hs Codegen.hs Emit.hs source file(scheme) input AST

    LLVM Assembly ࠓճ࡞ͬͨൣғ llvm-ac emcc LLVM Bitcode hoge.js
  14. ·ͱΊ • ࣮ݱੑΛઌʹߟ͑ͯ͠·࣮ͬͯ༻ੑ…·Ͱ͸ͨͲΓ ண͚ͳ͔ͬͨ • ΠϯλϓϦλͱLLVMͷ྆ํ͸େมͳͷͰɺ1ͭͣͭ ΍ͬͨํ͕Α͔ͬͨ • Parsec͸šŘŠŖɻJavaCCͱ͔Ͱ΍ΔΑΓѹ౗తʹָ •

    LLVM͸ͱ΋͔͘ɺParsecͰ৭ʑ΍Εͦ͏