When I learned MIPS assembly a few months ago, a question popped into my head that I forgot to ask then, so I thought I’d ask it now:
Is it possible to implement polymorphism without indirect jump instructions? If so, how?
(Indirect jumps are the "jump register" instructions, e.g. jr $t0 in MIPS or jmp EAX in x86.)
One such solution I came up with is self-modifying code, but I’m curious as to whether polymorphism (and so OOP) would be possible by any other means.
The simplest answer to your question would be to write your program (and possible assembler) in such a way that all method calls can be resolved at runtime thus negating the need for a lookup table. I’m going to assume you’re talking about passing a subclass to a function that was designed for a superclass and it is therefore impossible to implement this optimization.
Eliminating the lookup, I think, is outside the scope of your question so I’m going to suggest replacements for the
jmp <reg>instruction (sorry, I only know x86).call <mem>on a memoryaddress (isn’t this how you would do
it using a lookup table?)
call <reg>on a register(not entirely different from the jmp
, but does answer your question)
jmp <mem>if you wanted, butthat’s not all that different from
jmp <reg>All of these are possible and solve your problem, but are all alike. I guess this illustrates my confusion on why you would want to do what you’re asking. You have to have some way to choose which method to call (the vtable) and some way to transfer execution to the method (using
jmporcall).The only other possible way to do this would be to fiddle with the register that is used to point to the next command in the execution chain (
EIPin x86). Whether you can or should is another question. I suppose if you were intimately knowledgeable of the architecture and weren’t worried about it changing you could do that.