Introduction to Perl 6 Native Call

Introduction to Perl 6 Native Call

Introductory information to write and use native libraries from Perl 6

8fb7bbbce62c5912471abe8be4b2fcb1?s=128

Yoko OYAMA

July 29, 2016
Tweet

Transcript

  1. 2.

    WHO I AM My name is Yoko Oyama Have GitHub

    @yowcow Work for Marketing Applications, Inc.
  2. 3.

    Recently created Perl 6 modules: D i g e s

    t : : M u r m u r H a s h 3 Murmurhash3 hashing for Perl 6 Uses native call! A l g o r i t h m : : B l o o m F i l t e r A bloom lter implementation with murmurhash3 hashing
  3. 4.

    THINGS TO BE DISCUSSED Perl 6 Native library call Compiling

    and building a native library Types in native call Pitfalls Creating a module with native call Why C now?
  4. 5.
  5. 6.

    Perl 6 -- Many new features greatly advance our tradition

    of expressive and feature-rich programming http://perl6.org/
  6. 7.

    CHARACTERISTICS Object-oriented programming including generics, roles and multiple dispatch Functional

    programming primitives, lazy and eager list evaluation, junctions, autothreading and hyperoperators (vector operators) Parallelism, concurrency, and asynchrony including multi-core support De nable grammars for pattern matching and generalized string processing Optional and gradual typing
  7. 9.

    u s e v 6 ; u s e T

    e s t ; c l a s s M y C l a s s { o u r s u b a d d - a l l ( * @ n u m b e r s ) r e t u r n s I n t { [ + ] @ n u m b e r s ; } } m y I n t $ n u m b e r = 1 ; i s M y C l a s s : : a d d - a l l ( $ n u m b e r , 2 , 3 ) , 6 , ' 1 + 2 + 3 i s 6 ' ; d o n e - t e s t i n g ;
  8. 11.

    Native libraries are -- -- platform-speci c library les, including

    . d l l , . s o Something reusable, and compiled down into platform- speci c machine code. Ref: http://www.ibm.com/support/knowledgecenter/SSAW57_8.0.0/com.ibm.websphere.nd.doc/info/ae/ae/tc
  9. 12.

    In Perl 6, we can call C library through N

    a t i v e C a l l . https://docs.perl6.org/language/nativecall
  10. 13.

    libhoge.c # i n c l u d e <

    s t d i n t . h > u i n t 3 2 _ t a d d _ u i n t 3 2 ( u i n t 3 2 _ t a , u i n t 3 2 _ t b ) { r e t u r n a + b ; } MyHoge.pm6 u s e v 6 ; u s e N a t i v e C a l l ; u n i t m o d u l e M y H o g e ; o u r s u b a d d ( u i n t 3 2 , u i n t 3 2 ) r e t u r n s u i n t 3 2 i s n a t i v e ( ' . / l i b h o g e . s o ' ) i s s y m b o l ( ' a d d _ u i n t 3 2 ' ) { * }
  11. 15.

    CREATE A SHARED OBJECT WITH GCC Compile C code into

    object le g c c - O 2 - f P I C - c l i b h o g e . c - o l i b h o g e . o Link object into executable le g c c - O 2 - f P I C - s h a r e d l i b h o g e . o - o l i b h o g e . s o
  12. 16.

    GCC OPTIONS IN USE - O 2 : Optimize -

    f P I C : Create "Position Independent Code" - c : No linking - o : Output le - s h a r e d : Create shared object
  13. 17.

    TEST NATIVE CALL FROM PERL 6 test.p6 u s e

    v 6 ; u s e M y H o g e ; u s e T e s t ; i s M y H o g e : : a d d ( 1 , 2 ) , 3 , ' 1 + 2 i s 3 ' ; d o n e - t e s t i n g ; Run test: p r o v e - e " p e r l 6 - I . " t e s t . p 6 t e s t . p 6 . . o k A l l t e s t s s u c c e s s f u l . F i l e s = 1 , T e s t s = 1 , 0 w a l l c l o c k s e c s ( 0 . 0 1 u s r 0 . 0 1 s y s + 0 . 4 6 c u s r 0 . 0 5 R e s u l t : P A S S
  14. 18.

    AUTOMATION WITH MAKEFILE Create a Make le like: . P

    H O N Y : t e s t c l e a n C F L A G S = - f P I C - O 2 a l l : l i b h o g e . s o l i b h o g e . s o : l i b h o g e . o $ ( C C ) $ ( C F L A G S ) - s h a r e d l i b h o g e . o - o $ @ l i b h o g e . o : l i b h o g e . c $ ( C C ) $ ( C F L A G S ) - c l i b h o g e . c - o $ @ t e s t : l i b h o g e . s o p r o v e - e " p e r l 6 - I . " t e s t . p 6 c l e a n : - r m l i b h o g e . o l i b h o g e . s o and do: m a k e & & m a k e t e s t & & m a k e c l e a n
  15. 21.

    STATIC TYPING When using native call... Explicitly declare variable types

    Pass a variable as a speci c type into C library Interpret what C library returns as a speci c type
  16. 22.

    ARRAYS Use class C A r r a y [

    t y p e ] for C array. i n t 3 2 _ t a d d _ a l l ( i n t 3 2 _ t n u m b e r s [ ] , u i n t 3 2 _ t s i z e ) { u i n t 3 2 _ t i ; i n t 3 2 _ t s u m = 0 ; f o r ( i = 0 ; i < s i z e ; i + + ) { s u m + = n u m b e r s [ i ] ; } r e t u r n s u m ; } s u b a d d - a l l ( C A r r a y [ i n t 3 2 ] , u i n t 3 2 ) r e t u r n s i n t 3 2 i s n a t i v e ( ' . / l i b h o g e . s o ' ) i s s y m b o l ( ' a d d _ a l l ' ) { * } m y @ n u m b e r s : = C A r r a y [ u i n t 3 2 ] . n e w ; @ n u m b e r s [ 0 ] = 1 ; @ n u m b e r s [ 1 ] = 2 ; @ n u m b e r s [ 3 ] = 3 ; i s a d d - a l l ( @ n u m b e r s , @ n u m b e r s . e l e m s ) , 6 ;
  17. 23.

    STRUCT t y p e d e f s t

    r u c t p e r s o n { u i n t 3 2 _ t i d ; c h a r * n a m e ; } P e r s o n ; P e r s o n * P e r s o n _ c r e a t e ( u i n t 3 2 _ t i d , c h a r * n a m e ) { P e r s o n * p = m a l l o c ( s i z e o f ( P e r s o n ) ) ; p - > i d = i d ; p - > n a m e = n a m e ; r e t u r n p ; }
  18. 24.

    Use type C S t r u c t for

    struct object. c l a s s P e r s o n r e p r ( ' C S t r u c t ' ) { h a s u i n t 3 2 $ . i d ; h a s S t r $ . n a m e ; s u b c r e a t e ( u i n t 3 2 , S t r ) r e t u r n P e r s o n i s n a t i v e ( . / l i b h o g e . s o ' ) i s s y m b o l ( ' P e r s o n _ c r e a t e ' ) { * } m e t h o d n e w ( I n t : $ i d , S t r : $ n a m e ) { c r e a t e ( $ i d , $ n a m e ) } } m y P e r s o n $ p = P e r s o n . n e w ( i d = > 1 2 3 , n a m e = > ' h o g e ' ) ; i s $ p . i d , 1 2 3 ; i s $ p . n a m e , ' h o g e ' ;
  19. 25.

    Memory is managed by Perl 6. When the last reference

    to a struct object goes away, GC will free the memory. Except when struct-based type is used. Object members are not in "containers" Assigning a value to a member will not work t y p e d e f s t r u c t l i n e { P o i n t * p 1 , P o i n t * p 2 } L i n e ; / / M u s t f r e e b y y o u r s e $ ! i d = 1 2 3 4 ; # C a n n o t a s s i g n t o a n i m m u t a b l e v a l u e
  20. 26.
  21. 27.

    USE BINDING : = FOR C-ARRAY Write either: m y

    @ a r r a y : = C A r r a y [ u i n t 3 2 ] . n e w ; or m y $ a r r a y = C A r r a y [ u i n t 3 2 ] . n e w ; and not m y @ a r r a y = C A r r a y [ u i n t 3 2 ] . n e w ; Ref https://docs.perl6.org/language/nativecall#Arrays
  22. 28.

    UNSIGNED INT BECOMES SIGNED IN C-ARRAY Type u i n

    t 3 2 works for scalar... # i n c l u d e < s t d i n t . h > u i n t 3 2 _ t b i g _ u i n t 3 2 ( ) { r e t u r n s U I N T 3 2 _ M A X ; } u s e v 6 ; u s e N a t i v e C a l l ; s u b b i g _ u i n t 3 2 ( ) r e t u r n u i n t 3 2 i s n a t i v e ( ' . . . ' ) { * } s a y b i g _ u i n t 3 2 ( ) ; # 4 2 9 4 9 6 7 2 9 5
  23. 29.

    But when it's CArray... v o i d b i

    g _ u i n t 3 2 _ a r r a y ( u i n t 3 2 _ t n u m b e r s [ ] ) { n u m b e r s [ 0 ] = U I N T 3 2 _ M A X ; } u s e N a t i v e C a l l ; s u b b i g _ u i n t 3 2 _ a r r a y ( C A r r a y [ u i n t 3 2 ] ) i s n a t i v e ( ' . . . ' ) { * } m y @ n u m : = C A r r a y [ u i n t 3 2 ] . n e w ; @ n u m [ 0 ] = 0 ; b i g _ u i n t 3 2 _ a r r a y ( @ n u m ) ; s a y @ n u m [ 0 ] ; # - 1
  24. 30.

    What I did in D i g e s t

    : : M u r m u r H a s h 3 ... c o n s t a n t U I N T 3 2 _ M A X = 4 2 9 4 9 6 7 2 9 5 ; o u r s u b f i x - s i g n - b i t ( I n t : D $ v - - > I n t ) { $ v . s i g n = = - 1 ? ? $ v + 1 + U I N T 3 2 _ M A X ! ! $ v ; }
  25. 33.

    APP::MI6 Perl 5's Minilla-like module skelton generator, and builder Does

    basic M E T A 6 . j s o n management and build R E A D M E . m d from POD
  26. 34.

    LIBRARYMAKE Helps compiling and building a native library Generates a

    M a k e f i l e from a template M a k e f i l e . i n Derives appropriate native library extension . s o , . d l l , or . d y l i b for a platform
  27. 35.

    SEE ALSO Important information on module authoring Helpful tools when

    developing a module https://docs.perl6.org/language/modules#Distributing_Modu https://docs.perl6.org/language/modules-extra
  28. 39.

    WHY C NOW? C language is old. Today, "Go" and

    other languages are taking its position. But C still brings us a good opportunity to learn. How programs are compiled, linked, and ran How memory is allocated, used, and freed How to resolve realworld problems with primitive functions and libraries
  29. 41.

    (LEAD) IOS DEVELOPER Location: Tokyo Mission: To develop iOS apps,

    and iOS SDKs In marketing research eld Our Environment: Have iOS app deployed to TW, SG, and ID Global teams in JP, US, CN, KR, PH, SG, US, and GB English communication opportunities Unlimited free beer o ered on every Wednesdays If interested, contact me anytime.
  30. 42.
  31. 43.

    END