0

Basically I want to have dynamic method binding in my language.

I want to have a dynamic type and call the corresponding method. I'm not sure how to use pointers to those methods in assembly since simple labels don't work anymore in the compiler because we can't figure out the dynamic type of an object. A solution would be to have a reference to a vtable where the method pointers are, but I don't how to do that. Thanks in advance.

Here an example: Pseudocode

class Main{
void main() {
A a;
a = new B(); //Dynamic type of is B
a.method(); //should call B's method()
}

}

class A{
method(){}

}

class B extends A{
method(){} //OVERRIDING
}

Pseudoassembly:

Main.main:
...
call POINTERTORIGHTMETHOD #### my proble, how can I ensure now (via vtables that I'm calling the right Method????

....
A.method:
....


....

B.method:
...
  • It's unclear what part is causing you problem. Yes, for virtual functions you want a vtable. Each object instance typically has a pointer to the class vtable which has the functions in it. – Jester Apr 27 '18 at 12:49
  • Aha sorry. Yeah, I'm not quite sure how I could call from the vtable. And I'mlnot sure what to exactly put in the vtable(address of procedure? And if yes how can I get the address of the procecure?) –  Apr 27 '18 at 12:57
  • Yes, address of procedure. Use an indirect call, e.g. `call [foo]` or whatever is equivalent in your assembler and architecture (that you forgot to specify) – Jester Apr 27 '18 at 13:06
  • I'm on x86 32bit. So let's say now I want to declare the address of someMethod (another label for this example) in the .data section as methodAddress: Would I have to write methodAddress: .int someMethod Could I call it by simply: call methodAddress –  Apr 27 '18 at 13:39
  • 1) yes 2) no, you need the brackets `call [methodAddress]` (exact syntax depends on your assembler that you still haven't specified) – Jester Apr 27 '18 at 13:44

0 Answers0