• mruby demo (m5stickc) • What’s initialization • A problem of initialization • How to improve initialization • Introducing mruby-bin-nvgen • nvgen: problem and solution • Result
executing your Ruby scripts •initializing mruby VM •initializing GC system •initializing all Classes and Modules •creating Object, String, Integer, Array, … •defining methods for all classes / modules
of the class • classname • constants • create symbols initializing Class/Module Object String RClass RClass Exception RClass methods …… other data methods other data methods other data
data on Flash ROM dynamically • because it’s ROM! • We can put them as source code in C • use keyword const • pre-execution and code generation const struct RClass nv_object_212 = { .tt=MRB_TT_CLASS, .color=7, .flags=212, .c=(struct RClass *)&nv_object_211, .gcnext= (struct RBasic*)&nv_object_210, .iv=(struct iv_tbl*)&nv_iv_90, .mt=(struct kh_mt*)&nv_mt_86_empty, .super =NULL, /* name=BasicObject */ };
files • symbols • objects • … • put on ROM and RAM • executable is build with *.c, *.rb and mruby core mruby core symbol table (presym.c) objects, method tables, strings, … (generated.c) generator mruby core app.rb executable
(modified) mruby • generate Symbol tables (presym.c), objects and related data structures (generated.c) • C structures generated via mrb_open() mruby core symbol table (presym.c) objects, method tables, strings, … (generated.c) nvgen
mrb_sym • Symbol • TT + pointer • Other Classes • TT: Type Tag; type of value (0-24) TT void *p (32bit pointer) mrb_int (32bit int) mrb_sym (32bit uint) mrb_value TT TT Symbol Fixnum Other
• common object header • pointer to instance variables • pointer to method table • pointer to super class MRB_OBJECT_HEADER *iv (instance variables) *mt (method table) *super (super class) RClass
C • pointer to C function • method defined in Ruby • compiled as bytecode • IREP (mrb_irep) structure • IREP can be nested RProc irep RProc c function in C in Ruby irep irep
of local variables) • nregs (num. of registers) • ISEQ (bytecode) • pools • symbols • reps (pointer to IREPs) • rlen (num. of sub IREPs) • … data structure of code (method / block) irep irep
objects • flags should be mutable • So an array of flags for ROM’s objects are located in RAM • add macro to read and write them • obj->flags in ROM are indexes of the array flags flags flags flags flags flags … ROM RAM
(Black, White, Gray) • We use quad-color GC, add Red • Red are never GCed; objects in ROM are Red • At first of GC cycle, all children of Red objects are marked • VM has a list of Red objects ROM RAM
both ROM and RAM • tables for pre-defined methods are in ROM • using iv_tbl to store pointer of pre-defined method tables • key is __mt2__, which is not used as instance variables new methods pre-defined methods __mt2__ *mt *iv_tbl