## Inline-cached method call for amd64; this approach leaves ## it up to the method to adjust its return address to skip ## the call to the generic address. Here we are supposing ## that we have the object pointer in %rax, and the first 16 ## bits of the object are invariably its class ID. mov (%rax), %bx # 0: 66 8b 18 mov (%rax),%bx cmp $0x1234, %bx # 3: 66 81 fb 34 12 cmp $0x1234,%bx 0x1234 is the cached class id jne 1f # 8: 75 05 jne 0xf call 0x12345678 # a: e8 00 00 00 00 callq 0xf 0x12345678 is the cached method address 1: call 0x78654321 # f: e8 00 00 00 00 callq 0x14 0x78654321 is a dispatch routine for the selector ## But it would be better to do it the way explained in the ## original Deutsch & Schiffman paper: "The entry code of an ## n-code method checks the stored receiver class from the ## point of call against the actual receiver class. If they ## do not match, relinking must occur, as if the call had not ## yet been linked." ## This involves making the linked code at the call site ## something like this: mov (%rax), %bx # 14: 66 8b 18 mov (%rax),%bx call 0x12345678 # 17: e8 00 00 00 00 callq 0x1c ## And at 0x12345678 we have this: cmp $0x1234, %bx # 1c: 66 81 fb 34 12 cmp $0x1234,%bx jne 0x78654321 # 21: 0f 85 00 00 00 00 jne 0x27 ## The original paper does suggest having just a call to ## 0x78654321 there initially, but that hardly seems necessary. ## If we want to open-code fixnum arithmetic (again, as ## suggested in Deutsch & Schiffman) then that looks like this. ## Here we are supposing that (contrary to the above code!) ## fixnums are tagged with a low 1 bit, and that we are ## calling a dispatch routine which will fetch the class ## pointer itself and not try to patch this code. test $1, %al # 27: a8 01 test $0x1,%al jz 1f # 29: 74 07 je 0x32 lea -1(%rax,%rbx), %rcx # 2b: 48 8d 4c 18 ff lea -0x1(%rax,%rbx,1),%rcx jno 2f # 30: 71 05 jno 0x37 XXX have we already lost? 1: call 0x78654321 # 32: e8 00 00 00 00 callq 0x37 2: ## Deutsch & Schiffman report, “Independent measurements by us ## and by a group at U.C. Berkeley confirm that the one-element ## inline cache is effective about 95% of the time.” Mouthwatering.