100 likes | 277 Views
RTTI. CNS 4490. Example. class One{ } class Two : One { } class Driver{ void main() { One bob = new Two(); if (bob is Two) doSomething(); } }. Example. Steps 1. Alloc space for bob 2. In bob’s v-table Include type info
E N D
RTTI CNS 4490
Example class One{ } class Two : One { } class Driver{ void main() { One bob = new Two(); if (bob is Two) doSomething(); } }
Example Steps 1. Alloc space for bob 2. In bob’s v-table Include type info 3. Call constructor for Bob 4. check type var with numeric code for Two 5. doSomething( ) class One{ } class Two : One { } class Driver{ void main() { One bob = new Two(); if (bob is Two) doSomething(); } }
typeid ptr to parent vtable virtual fun1 virtual fun2 v-table
typeid ptr to parent vtable virtual fun1 2 4008 4000 virtual fun2 v-table for Two v-table 1 4000 0 v-table for One
Example class One{ } class Two : One { } class Driver{ void main() { int j; One bob = new Two(); if (bob is Two) j = 3; } } bob ptr vtable ptr bp j ctrl link ret-addr
Example mov r1, [bp +4] ; 4008 mov r1, [r1] ; 2 :check cmp r1 , 2 je assign mov r1, [r1+4] ; 4000 mov r1, [r1] :assign mov r1, bp ; j mov [r1], 3 class One{ } class Two : One { } class Driver{ void main() { int j; One bob = new Two(); if (bob is Two) j = 3; } } bob ptr vtable ptr bp j ctrl link ret-addr
Example 2 class One { } class Two { void printMe() {…} } class Driver { void main() { One bob = new Two( ); Two joe = bob as Two; } } • steps: • Set up bob • check bob’s type • assign pointer to joe
Example 2 class One { } class Two { void printMe() {…} } class Driver { void main() { One bob = new Two( ); Two joe = bob as Two; } } • steps: • Set up bob • check bob’s type • assign pointer to joe joe ptr bp bob ptr vtable ptr ctrl link ret-addr
Example 2 mov r1, [bp] ; 4008 mov r1, [r1] ; 2 :check cmp r1 , 2 je assign throw invalid cast exception :assign mov r1, bp + 4 ; joe mov r2, [bp] mov [r1], r2 class One { } class Two { void printMe() {…} } class Driver { void main() { One bob = new Two( ); Two joe = bob as Two; } } joe ptr bp bob ptr vtable ptr ctrl link ret-addr