Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What Lies Under The Syntax Sugar

What Lies Under The Syntax Sugar

Presentation given at the October 2017 WebDevTalks Meetup.

In this talk I explore the similarities and differences in the implementations of the 3 programming languages that I use the most these days: Objective-C, Swift and Elixir.

This talk is a deep dive into what goes behind the scenes when code is compiled.

Oscar Swanros

October 25, 2017
Tweet

More Decks by Oscar Swanros

Other Decks in Programming

Transcript

  1. W H AT L I E S U N D

    E R T H E S Y N TA X S U G A R W D T O C T O B E R
  2. P R O G R A M M I N

    G L A N G U A G E S
  3. Compiled Open source Dynamic Statically checked at compile time Runs

    on Linux, macOS, iOS, watchOS, tvOS, Raspberry Pi, Android, etc etc et
  4. id objc_msgSend(id self, SEL op, …); id: any Objective-C object

    type SEL: typedef struct objc_selector *SEL;
  5. unsigned square_int(unsigned a) { return a * a } define

    i32 @square_unsigned(i32 %a) { %l = mul i32 %a, %a ret i32 %l }
  6. IR is a low-level programming language in it of itself,

    that LLVM knows how to interpret and optimize.
  7. Objective-C is a relatively thin layer on top of C.

    Most of the actual code is still C. Swift is a completely new language, that drops the baggage of C. The whole Swift stdlib is written in Swift.
  8. S E M A ( S E M A N

    T I C A N A LY S I S )
  9. S E M A ( S E M A N

    T I C A N A LY S I S ) • Perform type checking
  10. S E M A ( S E M A N

    T I C A N A LY S I S ) • Perform type checking • Unused variables?
  11. S E M A ( S E M A N

    T I C A N A LY S I S ) • Perform type checking • Unused variables? • Check for unreachable code
  12. S E M A ( S E M A N

    T I C A N A LY S I S ) • Perform type checking • Unused variables? • Check for unreachable code • Overflow checks
  13. S I L G E N / S I L

    (Swift intermediate Language)
  14. S I L • Represents program semantics • Is suitable

    as a base for code generation and further analysis/optimizations • Bridges Swift sources and LLVM.
  15. B U I LT I N S • Builtins represent

    types and operations below Swift, at LLVM level. • Swift stlib implements interfaces on top of Builtins for ease of use. • Great for portability. Swift only cares about Int, the LLVM backend defines what an Int actually is.
  16. A F T E R S I L G E

    N • SIL is further analyzed, optimized. • LLVM IR is generated. • IR is passed to the swiftc LLVM backend to generate executables.
  17. Once compilation starts, there’s no turning back. AST is not

    accessible to the program. With Swift/Objective-C
  18. E L I X I R I S D I

    F F E R E N T
  19. 9 9 % O F E L I X I

    R I S B U I LT I N E L I X I R
  20. W H Y I S T H I S G

    O O D ? • The language only has to comply with a very small list of characteristics. • As long as the general structure of the language remains, changes can occur rather rapidly. • Elixir is some sort of Erlang DSL (but not quite).
  21. E L I X I R C O M P

    I L AT I O N • Read file • Tokens • Parse, build initial AST • Expand macros • Apply rewrite rules • Lower Elixir AST to Erlang AST • Pass to Erlang compiler…
  22. E R L A N G C O M P

    I L AT I O N • Compile Erlang AST to Core AST • Core optimization and inlining • Pattern matching • Optimize kernel • Generate ASM • Optimize ASM • Generate Bytecode • Write .beam
  23. • Flexible at compile time • Efficient • A really

    nice syntax for a rather powerful underlying
 engine • Distributed by nature
  24. • Flexible at compile time • Compiles really quickly (because

    the compiler can 
 only do so much). • Cumbersome syntax • Super powerful runtime
  25. • Really strict at compile time, misuse of language 


    features results in compile errors. • Compilation is really slow, but final program is way
 safer in terms of memory management • Familiar syntax • The amount of optimizations that go into compiling
 Swift, make it really fast.
  26. TA K E A WAY S • A language is

    much more than just its syntax • The foundation and background of each language is important • Knowing where they break give you superpowers • Knowing how to break them give you really good insights • Is really fun to work with compilers
  27. Q U E S T I O N S ?

    O S C A R @ S WA N R O S . C O M