140 likes | 265 Views
Virtual. CNS 4490. Example. class One { int a; One(int a1){a = a1;} int geta(){return a;} virtual int times(){return 2*a;} }; class Two : One { int b; Two(int a1,int b1):One(a1){b=b1;} override int times(){return b*a;} }; class Three{ void main( ) {
E N D
Virtual CNS 4490
Example class One { int a; One(int a1){a = a1;} int geta(){return a;} virtual int times(){return 2*a;} }; class Two : One { int b; Two(int a1,int b1):One(a1){b=b1;} override int times(){return b*a;} }; class Three{ void main( ) { One * joe = new One(4); One * bob = new Two(5,3); int c = bob.times( ); int d = joe.times(); } }; d c bob joe ctrl link ret addr bp
a vtable Example class One { int a; One(int a1){a = a1;} int geta(){return a;} virtual int times(){return 2*a;} }; class Two : One { int b; Two(int a1,int b1):One(a1){b=b1;} override int times(){return b*a;} }; class Three{ void main( ) { One * joe = new One(4); One * bob = new Two(5,3); int c = bob.times( ); int d = joe.times(); } }; d c bob joe ctrl link ret addr bp one@times
b a vtable a vtable Example class One { int a; One(int a1){a = a1;} int geta(){return a;} virtual int times(){return 2*a;} }; class Two : One { int b; Two(int a1,int b1):One(a1){b=b1;} override int times(){return b*a;} }; class Three{ void main( ) { One * joe = new One(4); One * bob = new Two(5,3); int c = bob.times( ); int d = joe.times(); } }; d c bob joe ctrl link ret addr two@times bp one@times
b a vtable a vtable Example class One { int a; One(int a1){a = a1;} int geta(){return a;} virtual int times(){return 2*a;} }; class Two : One { int b; Two(int a1,int b1):One(a1){b=b1;} override int times(){return b*a;} }; class Three{ void main( ) { One * joe = new One(4); One * bob = new Two(5,3); int c = bob.times( ); int d = joe.times(); } }; bp ctrl lnk this retaddr retval d c bob joe ctrl link ret addr two@times bp one@times
b a vtable a vtable Example class One { int a; One(int a1){a = a1;} int geta(){return a;} virtual int times(){return 2*a;} }; class Two : One { int b; Two(int a1,int b1):One(a1){b=b1;} override int times(){return b*a;} }; class Three{ void main( ) { One * joe = new One(4); One * bob = new Two(5,3); int c = bob.times( ); int d = joe.times(); } }; bp ctrl lnk this retaddr retval bp ctrl lnk this retaddr retval d c bob joe ctrl link ret addr two@times bp one@times
a vtable Example :One@One … :One@times … :Three@main ; alloc space for joe mov intreg, bp mov r50, 8 int 41 ;int vector for alloc ; setup vtable mov intreg, [bp] mov r50,4 int 41 mov r1,[bp] ; mov [r1], One@times; class One { int a; One(int a1){a = a1;} int geta(){return a;} virtual int times(){return 2*a;} }; class Three{ void main( ) { One * joe = new One(4); One * bob = new Two(5,3); int c = bob.times( ); int d = joe.times(); } }; bp ctrl lnk this retaddr retval d c bob joe ctrl link ret addr bp one@times
a vtable Example :One@One … :Three@main ;set up call to Construtor push done1 push [bp] push 4 push bp mov bp,sp jmp One@One :done1 class One { int a; One(int a1){a = a1;} int geta(){return a;} virtual int times(){return 2*a;} }; class Three{ void main( ) { One * joe = new One(4); One * bob = new Two(5,3); int c = bob.times( ); int d = joe.times(); } }; bp ctrl lnk a1 this retaddr d c bob joe ctrl link ret addr bp one@times
a vtable Example :One@One mov r1,this add r1,4 mov [r1],[bp-8] pop bp sub sp,8 ret … :Three@main :done1 class One { int a; One(int a1){a = a1;} int geta(){return a;} virtual int times(){return 2*a;} }; class Three{ void main( ) { One * joe = new One(4); One * bob = new Two(5,3); int c = bob.times( ); int d = joe.times(); } }; bp ctrl lnk a1 this retaddr d c bob joe ctrl link ret addr bp one@times
b a vtable a vtable Example :Three@main … ; alloc space for bob mov intreg, bp mov r50, 12 int 41 ;int vector for alloc ; setup vtable mov intreg, [bp] mov r50,4 int 41 mov r1,[bp] ; mov [r1], Two@times; class Two : One { int b; Two(int a1,int b1):One(a1) {b=b1;} override int times() {return b*a;} }; class Three{ void main( ) { One * joe = new One(4); One * bob = new Two(5,3); int c = bob.times( ); int d = joe.times(); } }; d c bob joe ctrl link ret addr Two@times bp One@times
b a vtable a vtable Example :Two@Two … :Three@main … ;set up call to Construtor push done2 push [bp+4] push 5 push 3 push bp mov bp,sp jmp Two@Two :done2 class Two : One { int b; Two(int a1,int b1):One(a1) {b=b1;} override int times() {return b*a;} }; class Three{ void main( ) { One * joe = new One(4); One * bob = new Two(5,3); int c = bob.times( ); int d = joe.times(); } }; bp ctrl lnk b1 a1 this retaddr d c bob joe ctrl link ret addr Two@times bp One@times
b a vtable a vtable Example :Two@Two :;set up call to One@One push done3 push [bp-16] push [bp-12] push bp mov bp,sp jmp One@One :done3 ;Do Assignment mov r1,[bp-16] mov r1,[r1+8] mov r2, [bp-8] mov [r1], r2 … ret bp ctrl lnk a1 this retaddr class One { int a; One(int a1){a = a1;} int geta(){return a;} virtual int times(){return 2*a;} }; class Two : One { int b; Two(int a1,int b1):One(a1){b=b1;} override int times(){return b*a;} }; class Three{ void main( ) { One * joe = new One(4); One * bob = new Two(5,3); int c = bob.times( ); int d = joe.times(); } }; bp ctrl lnk b1 a1 this retaddr d c bob joe ctrl link ret addr Two@times bp One@times
b a vtable a vtable Example :Three@main … ;set up call to two@times push 0 push done4 push [bp+4] push bp mov bp,sp mov r1,[bp+4] jmp [r1] :done4 class One { int a; One(int a1){a = a1;} int geta(){return a;} virtual int times(){return 2*a;} }; class Two : One { int b; Two(int a1,int b1):One(a1){b=b1;} override int times(){return b*a;} }; class Three{ void main( ) { One * joe = new One(4); One * bob = new Two(5,3); int c = bob.times( ); int d = joe.times(); } }; bp ctrl lnk this retaddr retval d c bob joe ctrl link ret addr Two@times bp One@times
b a vtable a vtable Example :two@times mov r1,[bp-8] push [r1+8] push [r1+4] pop r2 pop r1 mul r1,r2 push r1 mov r1, bp-16 pop r2 mov [r1],r2 … ret class One { int a; One(int a1){a = a1;} int geta(){return a;} virtual int times(){return 2*a;} }; class Two : One { int b; Two(int a1,int b1):One(a1){b=b1;} override int times(){return b*a;} }; class Three{ void main( ) { One * joe = new One(4); One * bob = new Two(5,3); int c = bob.times( ); int d = joe.times(); } }; bp ctrl lnk this retaddr retval d c bob joe ctrl link ret addr Two@times bp One@times