# ER図

## 概要

mrubyの内部データ構造間の関連を示すER図である。mrubyはリレーショナルデータベースを使用しないため、本図はC構造体間のポインタ参照関係をER図形式で表現している。

## 全体ER図

```mermaid
erDiagram
    mrb_state {
        mrb_jmpbuf_ptr jmp
        mrb_context_ptr c
        mrb_context_ptr root_c
        iv_tbl_ptr globals
        RObject_ptr exc
        RObject_ptr top_self
        mrb_gc gc
        mrb_sym symidx
        char_ptr_ptr symtbl
        size_t symcapa
        uint16_t atexit_stack_len
    }

    mrb_context {
        mrb_context_ptr prev
        mrb_value_ptr stbase
        mrb_value_ptr stend
        mrb_callinfo_ptr ci
        mrb_callinfo_ptr cibase
        mrb_callinfo_ptr ciend
        enum_mrb_fiber_state status
        mrb_bool vmexec
        RFiber_ptr fib
    }

    mrb_callinfo {
        uint8_t n
        uint8_t nk
        uint8_t cci
        uint8_t vis
        mrb_sym mid
        RProc_ptr proc
        RProc_ptr blk
        mrb_value_ptr stack
        mrb_code_ptr pc
    }

    mrb_gc {
        mrb_heap_page_ptr heaps
        mrb_heap_page_ptr free_heaps
        mrb_heap_page_ptr sweeps
        size_t live
        size_t threshold
        mrb_gc_state state
        int arena_idx
    }

    RBasic {
        RClass_ptr c
        enum_mrb_vtype tt
        uint3 gc_color
        uint1 frozen
        uint20 flags
    }

    RObject {
        iv_tbl_ptr iv
    }

    RClass {
        iv_tbl_ptr iv
        mrb_mt_tbl_ptr mt
        RClass_ptr super
    }

    RString {
        mrb_ssize len
        char_ptr ptr
        mrb_ssize capa
    }

    RArray {
        mrb_ssize len
        mrb_value_ptr ptr
        mrb_ssize capa
    }

    RHash {
        iv_tbl_ptr iv
        uint32_t size
        hash_entry_ptr ea
        hash_table_ptr ht
    }

    RProc {
        mrb_irep_ptr irep
        mrb_func_t func
        RProc_ptr upper
        RClass_ptr target_class
        REnv_ptr env
    }

    REnv {
        mrb_value_ptr stack
        mrb_context_ptr cxt
        mrb_sym mid
    }

    RRange {
        mrb_value beg
        mrb_value end_val
        mrb_bool excl
    }

    RData {
        iv_tbl_ptr iv
        mrb_data_type_ptr type
        void_ptr data
    }

    RFiber {
        mrb_context_ptr cxt
    }

    RException {
        iv_tbl_ptr iv
    }

    mrb_irep {
        uint16_t nlocals
        uint16_t nregs
        uint16_t clen
        uint8_t flags
        mrb_code_ptr iseq
        mrb_irep_pool_ptr pool
        mrb_sym_ptr syms
        mrb_irep_ptr_ptr reps
        mrb_sym_ptr lv
        uint32_t ilen
        uint16_t plen
        uint16_t slen
        uint16_t rlen
        uint16_t refcnt
    }

    mrb_irep_pool {
        uint32_t tt
        char_ptr str
        int32_t i32
        int64_t i64
    }

    mrb_irep_debug_info {
        uint32_t pc_count
        uint16_t flen
    }

    mrb_irep_debug_info_file {
        uint32_t start_pos
        mrb_sym filename_sym
        uint32_t line_entry_count
        mrb_debug_line_type line_type
    }

    mrb_method_t {
        uint32_t flags
        RProc_ptr proc
        mrb_func_t func
    }

    mrb_mt_tbl {
        int size
        int alloc
        mrb_mt_entry_ptr ptr
        mrb_mt_tbl_ptr next
    }

    mrb_mt_entry {
        mrb_sym key
        uint32_t flags
    }

    mrb_cache_entry {
        RClass_ptr c
        RClass_ptr c0
        mrb_sym mid
        mrb_method_t m
    }

    mrb_irep_catch_handler {
        uint8_t type
        uint8_t_4 begin_addr
        uint8_t_4 end_addr
        uint8_t_4 target
    }

    mrb_shared_array {
        int refcnt
        mrb_ssize len
        mrb_value_ptr ptr
    }

    mrb_data_type {
        char_ptr struct_name
        dfree_func dfree
    }

    %% mrb_state relationships
    mrb_state ||--|| mrb_context : "c (current context)"
    mrb_state ||--|| mrb_context : "root_c (root context)"
    mrb_state ||--|| mrb_gc : "gc (inline)"
    mrb_state ||--o| RObject : "exc (exception)"
    mrb_state ||--|| RObject : "top_self"
    mrb_state ||--|| RClass : "object_class"
    mrb_state ||--|| RClass : "class_class"
    mrb_state ||--|| RClass : "module_class"
    mrb_state ||--|| RClass : "string_class"
    mrb_state ||--|| RClass : "array_class"
    mrb_state ||--|| RClass : "hash_class"
    mrb_state ||--|| RClass : "integer_class"
    mrb_state ||--|| RClass : "kernel_module"
    mrb_state ||--o{ mrb_cache_entry : "cache[]"

    %% mrb_context relationships
    mrb_context ||--o| mrb_context : "prev (fiber chain)"
    mrb_context ||--|{ mrb_callinfo : "ci/cibase/ciend"
    mrb_context ||--o| RFiber : "fib"

    %% mrb_callinfo relationships
    mrb_callinfo ||--o| RProc : "proc"
    mrb_callinfo ||--o| RProc : "blk (block)"
    mrb_callinfo ||--o| REnv : "u.env"
    mrb_callinfo ||--o| RClass : "u.target_class"

    %% Object hierarchy (inheritance via MRB_OBJECT_HEADER)
    RBasic ||--o| RClass : "c (class pointer)"
    RObject ||--|| RBasic : "inherits"
    RClass ||--|| RBasic : "inherits"
    RString ||--|| RBasic : "inherits"
    RArray ||--|| RBasic : "inherits"
    RHash ||--|| RBasic : "inherits"
    RProc ||--|| RBasic : "inherits"
    REnv ||--|| RBasic : "inherits"
    RRange ||--|| RBasic : "inherits"
    RData ||--|| RBasic : "inherits"
    RFiber ||--|| RBasic : "inherits"
    RException ||--|| RBasic : "inherits"

    %% RClass relationships
    RClass ||--o| RClass : "super (superclass)"
    RClass ||--o| mrb_mt_tbl : "mt (method table)"

    %% mrb_mt_tbl relationships
    mrb_mt_tbl ||--o{ mrb_mt_entry : "ptr[]"
    mrb_mt_tbl ||--o| mrb_mt_tbl : "next (ROM chain)"

    %% RProc relationships
    RProc ||--o| mrb_irep : "body.irep"
    RProc ||--o| RProc : "upper (lexical scope)"
    RProc ||--o| REnv : "e.env"
    RProc ||--o| RClass : "e.target_class"

    %% REnv relationships
    REnv ||--o| mrb_context : "cxt"

    %% RFiber relationships
    RFiber ||--|| mrb_context : "cxt"

    %% RData relationships
    RData ||--o| mrb_data_type : "type"

    %% mrb_irep relationships
    mrb_irep ||--o{ mrb_irep_pool : "pool[]"
    mrb_irep ||--o{ mrb_irep : "reps[] (child ireps)"
    mrb_irep ||--o| mrb_irep_debug_info : "debug_info"
    mrb_irep ||--o{ mrb_irep_catch_handler : "catch handlers"

    %% mrb_irep_debug_info relationships
    mrb_irep_debug_info ||--o{ mrb_irep_debug_info_file : "files[]"

    %% mrb_cache_entry relationships
    mrb_cache_entry ||--o| RClass : "c (found class)"
    mrb_cache_entry ||--o| RClass : "c0 (search start)"
    mrb_cache_entry ||--|| mrb_method_t : "m (method)"

    %% mrb_method_t relationships
    mrb_method_t ||--o| RProc : "as.proc"

    %% RArray shared
    RArray ||--o| mrb_shared_array : "aux.shared"
```

## オブジェクト継承階層図

```mermaid
erDiagram
    RBasic {
        RClass_ptr c
        enum_mrb_vtype tt
        uint3 gc_color
        uint1 frozen
        uint20 flags
    }

    RObject {
        iv_tbl_ptr iv
    }

    RClass {
        iv_tbl_ptr iv
        mrb_mt_tbl_ptr mt
        RClass_ptr super
    }

    RString {
        mrb_ssize len
        char_ptr ptr
    }

    RArray {
        mrb_ssize len
        mrb_value_ptr ptr
    }

    RHash {
        iv_tbl_ptr iv
        uint32_t size
    }

    RProc {
        mrb_irep_ptr irep
        mrb_func_t func
    }

    REnv {
        mrb_value_ptr stack
        mrb_context_ptr cxt
    }

    RRange {
        mrb_value beg
        mrb_value end_val
    }

    RData {
        iv_tbl_ptr iv
        void_ptr data
    }

    RFiber {
        mrb_context_ptr cxt
    }

    RException {
        iv_tbl_ptr iv
    }

    RBasic ||--|| RObject : "extends (MRB_TT_OBJECT)"
    RBasic ||--|| RClass : "extends (MRB_TT_CLASS/MODULE/SCLASS/ICLASS)"
    RBasic ||--|| RString : "extends (MRB_TT_STRING)"
    RBasic ||--|| RArray : "extends (MRB_TT_ARRAY/STRUCT)"
    RBasic ||--|| RHash : "extends (MRB_TT_HASH)"
    RBasic ||--|| RProc : "extends (MRB_TT_PROC)"
    RBasic ||--|| REnv : "extends (MRB_TT_ENV)"
    RBasic ||--|| RRange : "extends (MRB_TT_RANGE)"
    RBasic ||--|| RData : "extends (MRB_TT_CDATA)"
    RBasic ||--|| RFiber : "extends (MRB_TT_FIBER)"
    RBasic ||--|| RException : "extends (MRB_TT_EXCEPTION)"
```

## VM実行コンテキスト関係図

```mermaid
erDiagram
    mrb_state {
        mrb_context_ptr c
        mrb_context_ptr root_c
        mrb_gc gc
    }

    mrb_context {
        mrb_context_ptr prev
        mrb_callinfo_ptr ci
        enum_mrb_fiber_state status
        RFiber_ptr fib
    }

    mrb_callinfo {
        mrb_sym mid
        RProc_ptr proc
        mrb_value_ptr stack
        mrb_code_ptr pc
    }

    RProc {
        mrb_irep_ptr irep
        RProc_ptr upper
        REnv_ptr env
    }

    mrb_irep {
        mrb_code_ptr iseq
        mrb_irep_pool_ptr pool
        mrb_sym_ptr syms
    }

    REnv {
        mrb_value_ptr stack
        mrb_context_ptr cxt
    }

    RFiber {
        mrb_context_ptr cxt
    }

    mrb_state ||--|| mrb_context : "c (active)"
    mrb_state ||--|| mrb_context : "root_c"
    mrb_context ||--o| mrb_context : "prev"
    mrb_context ||--|{ mrb_callinfo : "call stack"
    mrb_context ||--o| RFiber : "fib"
    RFiber ||--|| mrb_context : "cxt"
    mrb_callinfo ||--o| RProc : "proc"
    RProc ||--o| mrb_irep : "body.irep"
    RProc ||--o| RProc : "upper"
    RProc ||--o| REnv : "e.env"
    REnv ||--o| mrb_context : "cxt"
```

## クラスシステム関係図

```mermaid
erDiagram
    RClass {
        iv_tbl_ptr iv
        mrb_mt_tbl_ptr mt
        RClass_ptr super
    }

    mrb_mt_tbl {
        int size
        int alloc
        mrb_mt_entry_ptr ptr
        mrb_mt_tbl_ptr next
    }

    mrb_mt_entry {
        mrb_sym key
        uint32_t flags
        mrb_func_t func
        RProc_ptr proc
    }

    mrb_method_t {
        uint32_t flags
        RProc_ptr proc
        mrb_func_t func
    }

    RProc {
        mrb_irep_ptr irep
        mrb_func_t func
    }

    mrb_cache_entry {
        RClass_ptr c
        RClass_ptr c0
        mrb_sym mid
        mrb_method_t m
    }

    RClass ||--o| RClass : "super (inheritance chain)"
    RClass ||--o| mrb_mt_tbl : "mt"
    mrb_mt_tbl ||--o{ mrb_mt_entry : "ptr[]"
    mrb_mt_tbl ||--o| mrb_mt_tbl : "next (ROM chain)"
    mrb_mt_entry ||--o| RProc : "val.proc"
    mrb_method_t ||--o| RProc : "as.proc"
    mrb_cache_entry ||--o| RClass : "c"
    mrb_cache_entry ||--o| RClass : "c0"
    mrb_cache_entry ||--|| mrb_method_t : "m"
```
