Slide 1

Slide 1 text

So you want to design a programming language COPIOUSLabs TechTalk 15 Feb 2013

Slide 2

Slide 2 text

Brian Shirai Rubinius Developer

Slide 3

Slide 3 text

there will be a quiz

Slide 4

Slide 4 text

Questions?

Slide 5

Slide 5 text

Languages

Slide 6

Slide 6 text

why are languages fascinating?

Slide 7

Slide 7 text

practical or fanciful?

Slide 8

Slide 8 text

Security

Slide 9

Slide 9 text

28c3: The Science of Insecurity

Slide 10

Slide 10 text

computability

Slide 11

Slide 11 text

predictability

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

data vs meta-data

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

Virtual Machines

Slide 21

Slide 21 text

Interpreter

Slide 22

Slide 22 text

Stack vs Register

Slide 23

Slide 23 text

Jimple

Slide 24

Slide 24 text

Garbage collection

Slide 25

Slide 25 text

Concurrency

Slide 26

Slide 26 text

JIT compiler

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Parsers and compilers

Slide 30

Slide 30 text

Parsing Expression Grammars

Slide 31

Slide 31 text

PEGs

Slide 32

Slide 32 text

github.com/evanphx/kpeg

Slide 33

Slide 33 text

github.com/wycats/parsejs

Slide 34

Slide 34 text

LPeg http://www.inf.puc-rio.br/~roberto/lpeg/

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

Rubinius

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

atomy-lang.org

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

fancy-lang.org

Slide 41

Slide 41 text

Primitives

Slide 42

Slide 42 text

class Array def [] Ruby.primitive :array_aref raise PrimitiveFailure, "Array#[] primitive failed" end end

Slide 43

Slide 43 text

class Array : public Object { private: Fixnum* total_; // slot Tuple* tuple_; // slot public: attr_accessor(total, Fixnum); attr_accessor(tuple, Tuple); }

Slide 44

Slide 44 text

class Array : public Object { // Ruby.primitive :array_aref Object* aref(STATE, Fixnum* idx); }

Slide 45

Slide 45 text

instruction send_method(literal) [ receiver -- value ] => send flush_ip(); Object* recv = stack_top(); InlineCache* cache = reinterpret_cast(literal); Arguments args(cache->name, recv, cNil, 0, 0); Object* ret = cache->execute(state, call_frame, args); (void)stack_pop(); CHECK_AND_PUSH(ret); end

Slide 46

Slide 46 text

class OneArgument { public: static bool call(STATE, VMMethod* vmm, StackVariables* scope, Arguments& args) { if(args.total() != 1) return false; scope->set_local(0, args.get_argument(0)); return true; } };

Slide 47

Slide 47 text

$ rbx irb(main):001:0> def hello(name) irb(main):002:1> puts "hello, #{name}" irb(main):003:1> end => #

Slide 48

Slide 48 text

every CompiledCode object has its own interpreter

Slide 49

Slide 49 text

def self.compile_string(string, file="(eval)", line=1) compiler = new :string, :compiled_code parser = compiler.parser parser.root AST::Script parser.default_transforms parser.input string, file, line compiler.run end

Slide 50

Slide 50 text

$ rbx compile -e 'def m(a, b=1) a + b end' -N m -B ================== :m ================== Arguments: 1 required, 0 post, 2 total Locals: 2: a, b Stack size: 4 Lines to IP: 1: -1..14 0000: passed_arg 1 0002: goto_if_true 8 0004: meta_push_1 0005: set_local 1 # b 0007: pop 0008: push_local 0 # a 0010: push_local 1 # b 0012: meta_send_op_plus :+ 0014: ret ----------------------------------------

Slide 51

Slide 51 text

$ rbx compile -e 'def m(a, b=1) a + b end' -B ============= :__script__ ============== Arguments: 0 required, 0 post, 0 total Locals: 0 Stack size: 5 Lines to IP: 1: 0..15 0000: push_rubinius 0001: push_literal :m 0003: push_literal # 0005: push_scope 0006: push_variables 0007: send_stack :method_visibility, 0 0010: send_stack :add_defn_method, 4 0013: pop 0014: push_true 0015: ret ----------------------------------------

Slide 52

Slide 52 text

Ruby

Slide 53

Slide 53 text

$ rbx irb(main):001:0> m = Rubinius::Executable.new => #

Slide 54

Slide 54 text

irb(main):002:0> def m.call(*args) irb(main):003:1> puts "you rang? #{args.inspect}" irb(main):004:1> end => #

Slide 55

Slide 55 text

irb(main):005:0> sc = m.singleton_class => #> irb(main):006:0> sc.method_table.store :hi, m, :public => :hi

Slide 56

Slide 56 text

irb(main):007:0> m.hi you rang? [#, :hi] => nil irb(main):008:0> m.hi "hello" you rang? [#, :hi, "hello"] => nil

Slide 57

Slide 57 text

Module#dynamic_method

Slide 58

Slide 58 text

$ rbx irb(main):001:0> class Cat irb(main):002:1> dynamic_method :meow do |g| irb(main):003:2* g.push :self irb(main):004:2> g.push_literal "meow" irb(main):005:2> g.send :puts, 1, true irb(main):006:2> g.ret irb(main):007:2> end irb(main):008:1> end

Slide 59

Slide 59 text

=> # irb(main):009:0> Cat.new.meow meow => nil irb(main):010:0>

Slide 60

Slide 60 text

Tools

Slide 61

Slide 61 text

Debugger

Slide 62

Slide 62 text

class Cat def speak puts "woof" end end Cat.new.speak

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

github.com/rocky/rbx-trepanning

Slide 69

Slide 69 text

Profiler

Slide 70

Slide 70 text

$ rbx -Xprofile -e '10000.times { |x| x * x + x * 2 }' Thread 1: total running time: 0.073496367s % cumulative self self total time seconds seconds calls ms/call ms/call name ------------------------------------------------------------ 70.58 0.05 0.05 1 52.05 52.05 Rubinius::Tooling.disable 12.43 0.01 0.01 10000 0.00 0.00 Object::__script__<1> {} 7.24 0.02 0.01 1 5.34 16.74 Integer#times 3.03 0.00 0.00 20000 0.00 0.00 Fixnum#* 1.61 0.00 0.00 1 1.19 1.19 Rubinius::AST::Send#bytecode 1.38 0.00 0.00 1 1.02 2.78 Rubinius::Compiler::Parser#run 1.07 0.00 0.00 1 0.79 3.58 Rubinius::Compiler.compile_eval 0.74 0.00 0.00 4 0.14 0.44 Rubinius::Compiler::Stage#run_next 0.58 0.00 0.00 1 0.42 1.75 Rubinius::Compiler::Generator#run 0.53 0.02 0.00 1 0.39 20.97 Rubinius::Loader#evals 0.19 0.05 0.00 1 0.14 52.19 Rubinius::Profiler::Instrumenter#__stop__ 0.18 0.02 0.00 1 0.13 20.58 Kernel#eval 0.15 0.00 0.00 1 0.11 3.69 Rubinius::Compiler.construct_block 0.09 0.00 0.00 1 0.06 1.25 Rubinius::AST::EvalExpression::bytecode<906> 0.06 0.00 0.00 1 0.05 1.30 Rubinius::AST::Container#container_bytecode 0.05 0.05 0.00 1 0.04 52.25 Rubinius::Loader#epilogue 0.03 0.00 0.00 1 0.02 1.32 Rubinius::AST::EvalExpression#bytecode 0.01 0.00 0.00 1 0.01 2.79 Rubinius::Compiler#run 0.01 0.05 0.00 1 0.01 52.21 Object::__script__<4> {} 0.01 0.02 0.00 1 0.01 16.75 Object::__script__<1> {} 0.01 0.02 0.00 1 0.01 16.75 Rubinius::BlockEnvironment#call_on_instance 0.01 0.05 0.00 1 0.01 52.20 Profiler__.stop_profile 0.01 0.05 0.00 1 0.00 52.19 Rubinius::Profiler::Instrumenter#stop 0.01 0.05 0.00 1 0.00 52.20 Profiler__.print_profile 24 methods called a total of 30,025 times

Slide 71

Slide 71 text

Agent

Slide 72

Slide 72 text

$ rbx -Xagent.start irb(main):001:0>

Slide 73

Slide 73 text

$ rbx console VM: rbx -Xagent.start Connecting to VM on port 56488 Connected to localhost:56488, host type: x86_64-apple-darw console>

Slide 74

Slide 74 text

$ console> get system var system = [ "name", "backtrace", "threads", "gc", "memory", "pid", "jit", ]

Slide 75

Slide 75 text

$ console> get system.pid var system.pid = [ 69118, ] $ console> get system.threads var system.threads = [ "count", "backtrace", ] $ console> get system.threads.count var system.threads.count = 2

Slide 76

Slide 76 text

Memory analysis

Slide 77

Slide 77 text

require "rubinius/agent" agent = Rubinius::Agent.loopback agent.request :set_config, "system.memory.dump", "before.d array = (0..10).map { |i| "x" * rand(i) } p array agent.request :set_config, "system.memory.dump", "after.du

Slide 78

Slide 78 text

$ rbx -I /source/heap_dump/lib/ /source/heap_dump/bin/histo.rb before.dump 20738 Rubinius::Tuple 3763200 5820 Object 279312 5001 Rubinius::MethodTable::Bucket 280056 4388 Rubinius::CompiledMethod 877600 4388 Rubinius::InstructionSequence 140416 3269 String 209216 3166 Rubinius::CharArray 166864 1815 Rubinius::LookupTable::Bucket 87120 1130 Rubinius::LookupTable 54240 1100 Rubinius::GlobalCacheEntry 52800 1047 Rubinius::MethodTable 50256 1001 Class 100384 835 Rubinius::StaticScope 40080 676 Rubinius::AccessVariable 54080 456 Array 25536 141 Hash::Entry 7896 123 Rubinius::CompactLookupTable 15744 105 Rubinius::NativeFunction 10920 102 Rubinius::InstructionSet::OpCode 13872 46 Module 2944 46 Float 1472 46 Rubinius::IncludedModule 3312 39 Rubinius::CompiledMethod::Script 2808 22 Hash 1760

Slide 79

Slide 79 text

$ rbx -I /source/../lib /source/.../histo.rb before.dump after.dump 54 Object 2592 11 String 704 11 Rubinius::CharArray 440 8 Rubinius::GlobalCacheEntry 384 2 Rubinius::CompactLookupTable 256 2 Rubinius::ByteArray 2624 2 Rubinius::Tuple 184 1 Rubinius::Randomizer 48 1 Bignum 56 1 Rubinius::VariableScope 104 1 Array 56

Slide 80

Slide 80 text

proc FS

Slide 81

Slide 81 text

github.com/rubinius github.com/brixen @brixen

Slide 82

Slide 82 text

Thank you

Slide 83

Slide 83 text

the most important tech talk to watch this year is ________________.

Slide 84

Slide 84 text

Questions?