-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1812 lines (1758 loc) · 67.2 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Ruby under a microscope</title>
<meta name="description" content="Ruby under a microscope introduction">
<meta name="author" content="Bachue Zhou">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="css/reveal.min.css">
<link rel="stylesheet" href="css/theme/default.css" id="theme">
<link rel="stylesheet" href="lib/css/zenburn.css">
<script>
document.write( '<link rel="stylesheet" href="css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1>Ruby 2.0</h1>
<h3>Under a Microscope</h3>
<p>
<small>Created by <a href="http://bachue.is-programmer.com">Bachue</a> / <a href="http://twitter.com/iBachue">@iBachue</a></small>
</p>
<aside class="notes">
<ul>
<li>Just a share.</li>
<li>I'll introduce some points in this book to you.</li>
<li>Don't ask me tough questions, I can't answer then. :)</li>
</ul>
</aside>
</section>
<section>
<a href="http://patshaughnessy.net/ruby-under-a-microscope"><img src="img/cover.png" alt="cover of the book" /></a>
<aside class="notes">
This book is available to buy on <a href="http://www.nostarch.com/rum">http://www.nostarch.com/rum</a>, price is $31.95, about 200RMB.
</aside>
</section>
<section>
<img src="img/sanya.jpg" />
</section>
<section>
<h3>Who is this book for?</h3>
<ul>
<li>Ruby programmer</li>
<li>No C programming knowledge is required</li>
</ul>
<aside class="notes">
<ul>
<li>No Ruby basis explaination in these slides!</li>
<li>Only for Ruby programmer who shows strong interest in Ruby internal!</li>
<li>No C code shown in these slides!</li>
</ul>
</aside>
</section>
<section>
<a href="http://bachue.github.io/ruby-under-a-microscope-introduction-slides">bachue.github.io/ruby-under-a-microscope-introduction-slides</a>
</section>
<section>
<h3>Agenda</h3>
<ul>
<li>Overview</li>
<li>Tokenize</li>
<li>Parsing</li>
<li>Compilation</li>
<li>Execution as double stack machine</li>
<li>Control Structures</li>
<li>Objects & Class</li>
<li>Method Lookup & Constant Lookup</li>
<li>Closure & Binding</li>
<li>GC</li>
</ul>
<aside class="notes">
Basically map to each characters in the book, just a little changes.
</aside>
</section>
<section>
<section>
<h1>Overview</h1>
<img src="img/overview.png" />
<aside class="notes">
<div>Tokenize, Parse, Compile => YARV instruments</div>
<div>Bison: GNU Yacc</div>
</aside>
</section>
<section>
<h1>JRuby</h1>
<img src="img/jruby-overview.png" />
<aside class="notes">
<ul>
<li>Steps are similar, except JVM bytecode is generated</li>
<li>Jay is Bison-like but written in Java</li>
<li>JIT in Java could compile bytecode to machine language</li>
</ul>
</aside>
</section>
<section>
<h3>JIT</h3>
</section>
<section>
<h1>Rubinius</h1>
<img src="img/rubinius-overview.png" />
<aside class="notes">
<ul>
<li>Steps are similar, except Rubinius instruments and LLV instruments are generated</li>
<li>Rubinius compile Ruby to Rubinius instruments, and then use LLVM compile them to LLVM instruments, then may compile to machine language by JIT compiler</li>
</ul>
</aside>
</section>
</section>
<section>
<section>
<h1>Tokenize</h1>
<pre><code class="ruby">
10.times do |n|
puts n
end
</code></pre>
</section>
<section>
<h3>Before</h3>
<img src="img/before-tokenize.png" />
</section>
<section>
<h3>After</h3>
<img src="img/after-tokenize.png" />
</section>
<section>
<h3>parser_yylex in parse.y</h3>
<h4>Keywords: defs/keywords</h4>
<aside class="notes">
<p>ruby/parse.y:6933 in `parser_yylex'</p>
<p>start from line 6968: <code>switch (c = nextc())</code></p>
<p>keywords: rb_reserved_word, defs/keywords => lex.c</p>
<p>MRI doesn't use Lex tokenization tool, but write these code by hand, because of the performance or flexibility.</p>
</aside>
</section>
<section>
<h3>Ripper.tokenize & Ripper.lex</h3>
<pre><code class="ruby">
require 'ripper'
require 'pp'
code = <<-STR
10.times do |n|
puts n
end
STR
p Ripper.tokenize(code)
pp Ripper.lex(code)
</code></pre>
<aside class="notes">
Ripper, which was provided since 1.9, allows you to call the same tokenization and parsing code that Ruby uses to process text from code files.
</aside>
</section>
<section>
<pre><code class="ruby" style="max-height: inherit;">
[" ", "10", ".", "times", " ", "do", " ", "|", "n", "|", "\n", " ", "puts", " ", "n", "\n", " ", "end", "\n"]
[[[1, 0], :on_sp, " "],
[[1, 2], :on_int, "10"],
[[1, 4], :on_period, "."],
[[1, 5], :on_ident, "times"],
[[1, 10], :on_sp, " "],
[[1, 11], :on_kw, "do"],
[[1, 13], :on_sp, " "],
[[1, 14], :on_op, "|"],
[[1, 15], :on_ident, "n"],
[[1, 16], :on_op, "|"],
[[1, 17], :on_ignored_nl, "\n"],
[[2, 0], :on_sp, " "],
[[2, 4], :on_ident, "puts"],
[[2, 8], :on_sp, " "],
[[2, 9], :on_ident, "n"],
[[2, 10], :on_nl, "\n"],
[[3, 0], :on_sp, " "],
[[3, 2], :on_kw, "end"],
[[3, 5], :on_nl, "\n"]]
</code></pre>
<aside class="notes">
Result
</aside>
</section>
</section>
<section>
<section>
<h1>Parsing</h1>
<h2>LALR, Bison</h2>
<aside class="notes">
MRI uses GNU Bison to generate the parser code, from <code>parse.y</code> to <code>parse.c</code>
</aside>
</section>
<section>
<h3>Spanish => English Example</h3>
<pre><code class="asciidoc">
Me gusta el Ruby. => I Like Ruby.
Le gusta el Ruby. => She likes Ruby.
</code></pre>
<aside class="notes">
An Simple Example
</aside>
</section>
<section>
<h3>Bison/Yacc grammar</h3>
<pre><code class="asciidoc">
SpanishPhrase: VerbAndObject el ruby {
printf("%s Ruby\n", $1);
};
VerbAndObject: SheLikes | ILike {
$$ = $1;
};
SheLikes: le gusta {
$$ = "She likes";
}
ILike: me gusta {
$$ = "I like";
}
</code></pre>
<aside class="notes">
Grammar Rule
</aside>
</section>
<section>
<h3>Simplified Steps</h3>
<table style="margin: auto;">
<tr>
<th>Grammar Rule Stack</th>
<th>Tokens</th>
<th>Action</th>
</tr>
<tr>
<td></td>
<td>le gusta el ruby</td>
<td></td>
</tr>
<tr>
<td>le</td>
<td>gusta el ruby</td>
<td>Shift</td>
</tr>
<tr>
<td>le gusta</td>
<td>el ruby</td>
<td>Shift</td>
</tr>
<tr>
<td>SheLikes</td>
<td>el ruby</td>
<td>Reduce</td>
</tr>
<tr>
<td>VerbAndObject</td>
<td>el ruby</td>
<td>Reduce</td>
</tr>
<tr>
<td>VerbAndObject el</td>
<td>ruby</td>
<td>Shift</td>
</tr>
<tr>
<td>VerbAndObject el ruby</td>
<td></td>
<td>Shift</td>
</tr>
<tr>
<td>SpanishPhrase</td>
<td></td>
<td>Match</td>
</tr>
</table>
<aside class="notes">
<ul>
<li>Looks too simple to explain</li>
<li>But LALR is very complicated actually we must follow LALR Goto Table</li>
<li>No real Ruby example here, let's have a glance at the real code</li>
</ul>
</aside>
</section>
<section>
<h3>parse.y</h3>
<aside class="notes">
<p>parse.y:860</p>
<a target="_blank" href="/examples/ruby-1-4-bnf.pdf">BNF Syntax of Ruby 1.4</a>
</aside>
</section>
<section>
<h3>ruby -y</h3>
<pre><code class="ruby">
10.times do |n|
puts n
end
</code></pre>
<aside class="notes">
Another example
</aside>
</section>
<section>
<pre><code class="asciidoc" style="max-height: 600px;">
Starting parse
Entering state 0
Reducing stack by rule 1 (line 782):
-> $$ = nterm @1 ()
Stack now 0
Entering state 2
Reading a token: Next token is token tINTEGER ()
Shifting token tINTEGER ()
Entering state 41
Reducing stack by rule 470 (line 4255):
$1 = token tINTEGER ()
-> $$ = nterm numeric ()
Stack now 0 2
Entering state 104
Reducing stack by rule 428 (line 3830):
$1 = nterm numeric ()
-> $$ = nterm literal ()
Stack now 0 2
Entering state 94
Reducing stack by rule 270 (line 2616):
$1 = nterm literal ()
-> $$ = nterm primary ()
Stack now 0 2
Entering state 80
Reading a token: Next token is token '.' ()
Reducing stack by rule 329 (line 3071):
$1 = nterm primary ()
-> $$ = nterm primary_value ()
Stack now 0 2
Entering state 81
Next token is token '.' ()
Shifting token '.' ()
Entering state 336
Reading a token: Next token is token tIDENTIFIER ()
Shifting token tIDENTIFIER ()
Entering state 533
Reading a token: Next token is token keyword_do ()
Reducing stack by rule 550 (line 4823):
$1 = token tIDENTIFIER ()
-> $$ = nterm operation2 ()
Stack now 0 2 81 336
Entering state 538
Next token is token keyword_do ()
Reducing stack by rule 572 (line 4874):
-> $$ = nterm none ()
Stack now 0 2 81 336 538
Entering state 676
Reducing stack by rule 246 (line 2426):
$1 = nterm none ()
-> $$ = nterm opt_paren_args ()
Stack now 0 2 81 336 538
Entering state 674
Reducing stack by rule 404 (line 3635):
$1 = nterm primary_value ()
$2 = token '.' ()
$3 = nterm operation2 ()
$4 = nterm opt_paren_args ()
-> $$ = nterm method_call ()
Stack now 0 2
Entering state 93
Next token is token keyword_do ()
Shifting token keyword_do ()
Entering state 380
Reducing stack by rule 414 (line 3735):
-> $$ = nterm @28 ()
Stack now 0 2 93 380
Entering state 576
Reading a token: Next token is token '|' ()
Shifting token '|' ()
Entering state 653
Reading a token: Next token is token tIDENTIFIER ()
Shifting token tIDENTIFIER ()
Entering state 761
Reading a token: Next token is token '|' ()
Reducing stack by rule 518 (line 4543):
$1 = token tIDENTIFIER ()
-> $$ = nterm f_norm_arg ()
Stack now 0 2 93 380 576 653
Entering state 636
Reducing stack by rule 519 (line 4550):
$1 = nterm f_norm_arg ()
-> $$ = nterm f_arg_item ()
Stack now 0 2 93 380 576 653
Entering state 637
Reducing stack by rule 521 (line 4578):
$1 = nterm f_arg_item ()
-> $$ = nterm f_arg ()
Stack now 0 2 93 380 576 653
Entering state 765
Next token is token '|' ()
Reducing stack by rule 572 (line 4874):
-> $$ = nterm none ()
Stack now 0 2 93 380 576 653 765
Entering state 748
Reducing stack by rule 537 (line 4723):
$1 = nterm none ()
-> $$ = nterm opt_f_block_arg ()
Stack now 0 2 93 380 576 653 765
Entering state 850
Reducing stack by rule 372 (line 3377):
$1 = nterm f_arg ()
$2 = nterm opt_f_block_arg ()
-> $$ = nterm block_param ()
Stack now 0 2 93 380 576 653
Entering state 763
Next token is token '|' ()
Reducing stack by rule 572 (line 4874):
-> $$ = nterm none ()
Stack now 0 2 93 380 576 653 763
Entering state 770
Reducing stack by rule 385 (line 3479):
$1 = nterm none ()
-> $$ = nterm opt_bv_decl ()
Stack now 0 2 93 380 576 653 763
Entering state 847
Next token is token '|' ()
Shifting token '|' ()
Entering state 905
Reducing stack by rule 384 (line 3468):
$1 = token '|' ()
$2 = nterm block_param ()
$3 = nterm opt_bv_decl ()
$4 = token '|' ()
-> $$ = nterm block_param_def ()
Stack now 0 2 93 380 576
Entering state 655
Reducing stack by rule 381 (line 3444):
$1 = nterm block_param_def ()
-> $$ = nterm opt_block_param ()
Stack now 0 2 93 380 576
Entering state 716
Reading a token: Next token is token tIDENTIFIER ()
Shifting token tIDENTIFIER ()
Entering state 35
Reading a token: Next token is token tIDENTIFIER ()
Reducing stack by rule 547 (line 4818):
$1 = token tIDENTIFIER ()
-> $$ = nterm operation ()
Stack now 0 2 93 380 576 716
Entering state 110
Next token is token tIDENTIFIER ()
Reducing stack by rule 258 (line 2499):
-> $$ = nterm @7 ()
Stack now 0 2 93 380 576 716 110
Entering state 219
Next token is token tIDENTIFIER ()
Shifting token tIDENTIFIER ()
Entering state 35
Reading a token: Next token is token '\n' ()
Reducing stack by rule 474 (line 4275):
$1 = token tIDENTIFIER ()
-> $$ = nterm user_variable ()
Stack now 0 2 93 380 576 716 110 219
Entering state 209
Next token is token '\n' ()
Reducing stack by rule 486 (line 4291):
$1 = nterm user_variable ()
-> $$ = nterm var_ref ()
Stack now 0 2 93 380 576 716 110 219
Entering state 107
Reducing stack by rule 276 (line 2622):
$1 = nterm var_ref ()
-> $$ = nterm primary ()
Stack now 0 2 93 380 576 716 110 219
Entering state 80
Next token is token '\n' ()
Reducing stack by rule 239 (line 2375):
$1 = nterm primary ()
-> $$ = nterm arg ()
Stack now 0 2 93 380 576 716 110 219
Entering state 203
Next token is token '\n' ()
Reducing stack by rule 240 (line 2381):
$1 = nterm arg ()
-> $$ = nterm arg_value ()
Stack now 0 2 93 380 576 716 110 219
Entering state 204
Next token is token '\n' ()
Reducing stack by rule 263 (line 2531):
$1 = nterm arg_value ()
-> $$ = nterm args ()
Stack now 0 2 93 380 576 716 110 219
Entering state 207
Next token is token '\n' ()
Reducing stack by rule 572 (line 4874):
-> $$ = nterm none ()
Stack now 0 2 93 380 576 716 110 219 207
Entering state 398
Reducing stack by rule 262 (line 2525):
$1 = nterm none ()
-> $$ = nterm opt_block_arg ()
Stack now 0 2 93 380 576 716 110 219 207
Entering state 397
Reducing stack by rule 254 (line 2463):
$1 = nterm args ()
$2 = nterm opt_block_arg ()
-> $$ = nterm call_args ()
Stack now 0 2 93 380 576 716 110 219
Entering state 409
Reducing stack by rule 259 (line 2499):
$1 = nterm @7 ()
$2 = nterm call_args ()
-> $$ = nterm command_args ()
Stack now 0 2 93 380 576 716 110
Entering state 387
Next token is token '\n' ()
Reducing stack by rule 58 (line 1344):
$1 = nterm operation ()
$2 = nterm command_args ()
-> $$ = nterm command ()
Stack now 0 2 93 380 576 716
Entering state 72
Next token is token '\n' ()
Reducing stack by rule 51 (line 1297):
$1 = nterm command ()
-> $$ = nterm command_call ()
Stack now 0 2 93 380 576 716
Entering state 70
Reducing stack by rule 44 (line 1249):
$1 = nterm command_call ()
-> $$ = nterm expr ()
Stack now 0 2 93 380 576 716
Entering state 69
Next token is token '\n' ()
Reducing stack by rule 41 (line 1225):
$1 = nterm expr ()
-> $$ = nterm stmt ()
Stack now 0 2 93 380 576 716
Entering state 245
Next token is token '\n' ()
Reducing stack by rule 14 (line 933):
$1 = nterm stmt ()
-> $$ = nterm stmts ()
Stack now 0 2 93 380 576 716
Entering state 244
Next token is token '\n' ()
Shifting token '\n' ()
Entering state 289
Reducing stack by rule 569 (line 4866):
$1 = token '\n' ()
-> $$ = nterm term ()
Stack now 0 2 93 380 576 716 244
Entering state 291
Reducing stack by rule 570 (line 4869):
$1 = nterm term ()
-> $$ = nterm terms ()
Stack now 0 2 93 380 576 716 244
Entering state 433
Reading a token: Next token is token keyword_end ()
Reducing stack by rule 560 (line 4847):
$1 = nterm terms ()
-> $$ = nterm opt_terms ()
Stack now 0 2 93 380 576 716 244
Entering state 432
Reducing stack by rule 12 (line 913):
$1 = nterm stmts ()
$2 = nterm opt_terms ()
-> $$ = nterm compstmt ()
Stack now 0 2 93 380 576 716
Entering state 817
Next token is token keyword_end ()
Shifting token keyword_end ()
Entering state 881
Reducing stack by rule 415 (line 3734):
$1 = token keyword_do ()
$2 = nterm @28 ()
$3 = nterm opt_block_param ()
$4 = nterm compstmt ()
$5 = token keyword_end ()
-> $$ = nterm brace_block ()
Stack now 0 2 93
Entering state 382
Reducing stack by rule 298 (line 2781):
$1 = nterm method_call ()
$2 = nterm brace_block ()
-> $$ = nterm primary ()
Stack now 0 2
Entering state 80
Reading a token: Next token is token '\n' ()
Reducing stack by rule 239 (line 2375):
$1 = nterm primary ()
-> $$ = nterm arg ()
Stack now 0 2
Entering state 79
Next token is token '\n' ()
Reducing stack by rule 49 (line 1282):
$1 = nterm arg ()
-> $$ = nterm expr ()
Stack now 0 2
Entering state 69
Next token is token '\n' ()
Reducing stack by rule 41 (line 1225):
$1 = nterm expr ()
-> $$ = nterm stmt ()
Stack now 0 2
Entering state 67
Next token is token '\n' ()
Reducing stack by rule 8 (line 855):
$1 = nterm stmt ()
-> $$ = nterm top_stmt ()
Stack now 0 2
Entering state 66
Reducing stack by rule 5 (line 833):
$1 = nterm top_stmt ()
-> $$ = nterm top_stmts ()
Stack now 0 2
Entering state 65
Next token is token '\n' ()
Shifting token '\n' ()
Entering state 289
Reducing stack by rule 569 (line 4866):
$1 = token '\n' ()
-> $$ = nterm term ()
Stack now 0 2 65
Entering state 291
Reducing stack by rule 570 (line 4869):
$1 = nterm term ()
-> $$ = nterm terms ()
Stack now 0 2 65
Entering state 292
Reading a token: Now at end of input.
Reducing stack by rule 560 (line 4847):
$1 = nterm terms ()
-> $$ = nterm opt_terms ()
Stack now 0 2 65
Entering state 290
Reducing stack by rule 3 (line 813):
$1 = nterm top_stmts ()
$2 = nterm opt_terms ()
-> $$ = nterm top_compstmt ()
Stack now 0 2
Entering state 64
Reducing stack by rule 2 (line 782):
$1 = nterm @1 ()
$2 = nterm top_compstmt ()
-> $$ = nterm program ()
Stack now 0
Entering state 1
Now at end of input.
Stack now 0 1
Cleanup: popping nterm program ()
</code></pre>
</section>
<section>
<h3>Ripper.sexp</h3>
<pre><code class="ruby">
require 'ripper'
require 'pp'
code = <<-STR
10.times do |n|
puts n
end
STR
puts code
pp Ripper.sexp(code)
</code></pre>
<aside class="notes">
Parse source code and create S-exp tree.
</aside>
</section>
<section>
<pre><code class="ruby">
[:program,
[[:method_add_block,
[:call, [:@int, "10", [1, 3]], :".", [:@ident, "times", [1, 6]]],
[:do_block,
[:block_var,
[:params, [[:@ident, "n", [1, 16]]], nil, nil, nil, nil],
nil],
[[:command,
[:@ident, "puts", [2, 0]],
[:args_add_block, [[:var_ref, [:@ident, "n", [2, 5]]]], false]]]]]]]
</code></pre>
</section>
<section>
<h3>ruby --dump parsetree</h3>
<pre><code class="asciidoc" style="max-height: 600px;">
@ NODE_SCOPE (line: 3)
+- nd_tbl: (empty)
+- nd_args:
| (null node)
+- nd_body:
@ NODE_ITER (line: 1)
+- nd_iter:
| @ NODE_CALL (line: 1)
| +- nd_mid: :times
| +- nd_recv:
| | @ NODE_LIT (line: 1)
| | +- nd_lit: 10
| +- nd_args:
| (null node)
+- nd_body:
@ NODE_SCOPE (line: 3)
+- nd_tbl: :n
+- nd_args:
| @ NODE_ARGS (line: 1)
| +- nd_frml: 1
| +- nd_next:
| | @ NODE_ARGS_AUX (line: 1)
| | +- nd_rest: (null)
| | +- nd_body: (null)
| | +- nd_next:
| | (null node)
| +- nd_opt:
| (null node)
+- nd_body:
@ NODE_FCALL (line: 2)
+- nd_mid: :puts
+- nd_args:
@ NODE_ARRAY (line: 2)
+- nd_alen: 1
+- nd_head:
| @ NODE_DVAR (line: 2)
| +- nd_vid: :n
+- nd_next:
(null node)
</code></pre>
<aside class="notes">
Also shows node info, which is very related to the implementation.
</aside>
</section>
<section>
<h3>Equivalent AST nodes</h3>
<img src="img/ruby-ast-example.png" />
<aside class="notes">
NODE_LIT means literal.
</aside>
</section>
</section>
<section>
<section>
<h1>Compilation</h1>
</section>
<section>
<h3>Ruby 1.8</h3>
<img src="img/ruby-18-layers.png" />
<aside class="notes">
No compilation in Ruby 1.8, Ruby 1.8 execute AST directly.
</aside>
</section>
<section>
<h3>Ruby 1.9 & 2.0</h3>
<img src="img/ruby-19-20-layers.png" />
<aside class="notes">
Ruby 1.9 compiles Ruby code to YARV instruments.
</aside>
</section>
<section>
<h3>Benchmark</h3>
<pre><code class="ruby">
i= 0
while i < ARGV[0].to_i
i += 1
end
</code></pre>
<aside class="notes">
Performance competition between AST and YARV instruments.
</aside>
</section>
<section>
<h3>logarithmic scale</h3>
<img src="img/ruby-logarithmic-performance.png" />
<aside class="notes">
You can see that for short-lived processes, Ruby 1.8.7 is actually faster than Ruby 1.9.3 and 2.0 because there is no need to compile the Ruby code into YARV instructions.
However, after about 11,000 iterations, Ruby 1.9.3 and 2.0 are faster.
</aside>
</section>
<section>
<h3>linear scale</h3>
<img src="img/ruby-linear-performance.png" />
</section>
<section>
<h3>Displaying YARV Instructions</h3>
<pre><code class="ruby">
code = <<-RUBY
puts 1 + 2
RUBY
puts RubyVM::InstructionSequence.compile(code).disasm
</code></pre>
</section>
<section>
<h3>Compiled</h3>
<pre><code class="asciidoc">
== disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>==========
0000 trace 1 ( 1)
0002 putself
0003 putobject_OP_INT2FIX_O_1_C_
0004 putobject 2
0006 opt_plus <callinfo!mid:+, argc:1, ARGS_SKIP>
0008 opt_send_simple <callinfo!mid:puts, argc:1, FCALL|ARGS_SKIP>
0010 leave
</code></pre>
<aside class="notes">
<ul>
<li>Execute in Stack machine</li>
<li><code>putobject_OP_INT2FIX_O_1_C_</code> is equivalent to <code>putobject 1</code></li>
<li>ARGS_SKIP indicates the arguments are simple values (not blocks or arrays of unnamed arguments), allowing YARV to skip some work it would have to do otherwise.</li>
<li>MRI traverses the AST nodes(result of <code>ruby -y</code>) and generate these instruments.</li>
</ul>
</aside>
</section>
<section>
<h3>Another Example</h3>
<pre><code class="ruby">
code = <<-RUBY
10.times do |n|
puts n
end
RUBY
puts RubyVM::InstructionSequence.compile(code).disasm
</code></pre>
</section>
<section>
<h3>Compiled</h3>
<pre><code class="asciidoc" style="max-height: inherit;">
== disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>==========
== catch table
| catch type: break st: 0002 ed: 0006 sp: 0000 cont: 0006
|------------------------------------------------------------------------
0000 trace 1 ( 1)
0002 putobject 10
0004 send <callinfo!mid:times, argc:0, block:block in <compiled>>
0006 leave
== disasm: <RubyVM::InstructionSequence:block in <compiled>@<compiled>>=
== catch table
| catch type: redo st: 0000 ed: 0009 sp: 0000 cont: 0000
| catch type: next st: 0000 ed: 0009 sp: 0000 cont: 0009
|------------------------------------------------------------------------
local table (size: 2, argc: 1 [opts: 0, rest: -1, post: 0, block: -1] s3)
[ 2] n<Arg>
0000 trace 256 ( 1)
0002 trace 1 ( 2)
0004 putself
0005 getlocal_OP__WC__0 2
0007 opt_send_simple <callinfo!mid:puts, argc:1, FCALL|ARGS_SKIP>
0009 trace 512 ( 3)
0011 leave ( 2)
</code></pre>
<aside class="notes">
<ul>
<li>Notice Local variable</li>
<li><code>getlocal_OP__WC__0</code> is equivalent to <code>getlocal *, 0</code></li>
<li>This trick allows Ruby 2.0 to save a bit of time because it doesn’t need to pass the 0 argument separately.</li>
</ul>
</aside>
</section>
<section>
<h3>iseq_compile_each in compile.c</h3>
<aside class="notes">
<p>How Ruby interates thought the AST</p>
<p>compile.c:3191</p>
</aside>
</section>
</section>
<section>
<section>
<h1>Execution</h1>
<h3>As double stack machine</h3>
</section>
<section>
<h3>rb_control_frame_t</h3>
<img src="img/rb_control_frame_t-1.png" />
<aside class="notes">
pc, sp, self, type(frame type)
</aside>
</section>
<section>
<h3>caller</h3>
<img src="img/rb_control_frame_t-2.png" />
<aside class="notes">
<ul>
<li>Just like we call <code>caller</code></li>
<li>CFP => current frame pointer</li>
</ul>
</aside>
</section>
<section>
<h3>Example</h3>
<pre><code class="ruby">
10.times do
puts "The quick brown fox jumps over the lazy dog."
end
</code></pre>
</section>
<section>
<img src="img/rb_control_frame_t-3.png" />
<aside class="notes">
<p>Ruby always creates "TOP" frame first when starting a new program. At the top of the stack, at least initially, a frame of type "EVAL" corresponds to the top level or main scope of the Ruby script.</p>
</aside>
</section>
<section>
<h3>Local Variable Access</h3>
</section>
<section>
<h3>Environment Pointer</h3>
<img src="img/local_variable_1.png" />
<aside class="notes">
<ul>
<li>EP = SP - 1</li>
<li>Special: to track information related to blocks(rb_block_t).</li>
</ul>
</aside>
</section>
<section>
<img src="img/local_variable_2.png" />
<aside class="notes">
<ul>
<li>Local variable table is not a table(Just like Stack variable in C)</li>
<li>special: to track information related to blocks(rb_block_t) (doesn't mean special variable table).</li>
<li>svar: pointer to special variable table(only for ruby method call)</li>
<li>cref: pointer to current lexical scope(designed for *_eval, *_exec methods, only for block call)</li>
<li>Method Arguments Are Treated Like Local Variables</li>
</ul>
</aside>
</section>
<section>
<h3>Dynamic Variable Access</h3>
<pre><code class="ruby">
code = <<-RUBY
def display_string
str = "Dynamic access."
10.times do
puts str
end
end
RUBY
puts RubyVM::InstructionSequence.compile(code).disasm
</code></pre>
</section>
<section>
<pre><code class="asciidoc" style="max-height: inherit;">
== disasm: <RubyVM::InstructionSequence:f@<compiled>>===================
== catch table
| catch type: break st: 0010 ed: 0014 sp: 0000 cont: 0014
|------------------------------------------------------------------------
local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1] s1)
[ 2] str
0000 trace 8 ( 1)
0002 trace 1 ( 2)
0004 putstring "Dynamic access."
0006 setlocal_OP__WC__0 2
0008 trace 1 ( 3)
0010 putobject 10
0012 send <callinfo!mid:times, argc:0, block:block in f>
0014 trace 16 ( 6)
0016 leave ( 3)
== disasm: <RubyVM::InstructionSequence:block in f@<compiled>>==========
== catch table
| catch type: redo st: 0000 ed: 0009 sp: 0000 cont: 0000
| catch type: next st: 0000 ed: 0009 sp: 0000 cont: 0009
|------------------------------------------------------------------------
0000 trace 256 ( 3)
0002 trace 1 ( 4)
0004 putself
0005 getlocal_OP__WC__1 2
0007 opt_send_simple <callinfo!mid:puts, argc:1, FCALL|ARGS_SKIP>
0009 trace 512 ( 5)
0011 leave ( 4)
</code></pre>
<aside class="notes">
<code>setlocal_OP__WC__0</code> is the same as <code>setlocal *, 0</code>
</aside>
</section>
<section>
<img src="img/dynamic_variable.png" />
<aside class="notes">
<code>getlocal_OP__WC__1 2</code> means to follow Previous EP once, then minus 2
</aside>
</section>
<section>
<h3>Special Variable Access</h3>
</section>
<section>
<h3>Guess?</h3>
<pre><code class="ruby">
str = "The quick brown fox jumped over the lazy dog.\n"
/fox/.match(str)
def search(str)