123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525 |
- #include "objc-private.h"
- // out-of-band parameter to objc_msgForward
- #define kFwdMsgSend 1
- #define kFwdMsgSendStret 0
- // objc_msgSend parameters
- #define SELF 8[ebp]
- #define SUPER 8[ebp]
- #define SELECTOR 12[ebp]
- #define FIRST_ARG 16[ebp]
- // objc_msgSend_stret parameters
- #define STRUCT_ADDR 8[ebp]
- #define SELF_STRET 12[ebp]
- #define SUPER_STRET 12[ebp]
- #define SELECTOR_STRET 16[ebp]
- // objc_super parameter to sendSuper
- #define super_receiver 0
- #define super_class 4
- // struct objc_class fields
- #define isa 0
- #define cache 32
- // struct objc_method fields
- #define method_name 0
- #define method_imp 8
- // struct objc_cache fields
- #define mask 0
- #define occupied 4
- #define buckets 8
- void *_objc_forward_handler = NULL;
- void *_objc_forward_stret_handler = NULL;
- __declspec(naked) Method _cache_getMethod(Class cls, SEL sel, IMP objc_msgForward_imp)
- {
- __asm {
- push ebp
- mov ebp, esp
- mov ecx, SELECTOR
- mov edx, SELF
- // CacheLookup WORD_RETURN, CACHE_GET
- push edi
- mov edi, cache[edx]
- push esi
- mov esi, mask[edi]
- mov edx, ecx
- shr edx, 2
- SCAN:
- and edx, esi
- mov eax, buckets[edi][edx*4]
- test eax, eax
- je MISS
- cmp ecx, method_name[eax]
- je HIT
- add edx, 1
- jmp SCAN
- MISS:
- xor eax, eax
- pop esi
- pop edi
- leave
- ret
- HIT:
- mov ecx, FIRST_ARG
- cmp ecx, method_imp[eax]
- je MISS
- pop esi
- pop edi
- leave
- ret
- }
- }
- __declspec(naked) IMP _cache_getImp(Class cls, SEL sel)
- {
- __asm {
- push ebp
- mov ebp, esp
- mov ecx, SELECTOR
- mov edx, SELF
- // CacheLookup WORD_RETURN, CACHE_GET
- push edi
- mov edi, cache[edx]
- push esi
- mov esi, mask[edi]
- mov edx, ecx
- shr edx, 2
- SCAN:
- and edx, esi
- mov eax, buckets[edi][edx*4]
- test eax, eax
- je MISS
- cmp ecx, method_name[eax]
- je HIT
- add edx, 1
- jmp SCAN
- MISS:
- pop esi
- pop edi
- xor eax, eax
- leave
- ret
- HIT:
- pop esi
- pop edi
- mov eax, method_imp[eax]
- leave
- ret
- }
- }
- OBJC_EXPORT __declspec(naked) id objc_msgSend(id a, SEL b, ...)
- {
- __asm {
- push ebp
- mov ebp, esp
- // load receiver and selector
- mov ecx, SELECTOR
- mov eax, SELF
- // check whether receiver is nil
- test eax, eax
- je NIL
- // receiver (in eax) is non-nil: search the cache
- mov edx, isa[eax]
- // CacheLookup WORD_RETURN, MSG_SEND
- push edi
- mov edi, cache[edx]
- push esi
- mov esi, mask[edi]
- mov edx, ecx
- shr edx, 2
- SCAN:
- and edx, esi
- mov eax, buckets[edi][edx*4]
- test eax, eax
- je MISS
- cmp ecx, method_name[eax]
- je HIT
- add edx, 1
- jmp SCAN
- HIT:
- mov eax, method_imp[eax]
- pop esi
- pop edi
- mov edx, kFwdMsgSend
- leave
- jmp eax
- // cache miss: search method lists
- MISS:
- pop esi
- pop edi
- mov edx, SELF
- mov eax, isa[edx]
- // MethodTableLookup WORD_RETURN, MSG_SEND
- push $3
- push eax
- push ecx
- push edx
- call lookUpImpOrFoward
- mov edx, kFwdMsgSend
- leave
- jmp eax
- // message send to nil: return zero
- NIL:
- // eax is already zero
- mov edx, 0
- leave
- ret
- }
- }
- OBJC_EXPORT __declspec(naked) double objc_msgSend_fpret(id a, SEL b, ...)
- {
- __asm {
- push ebp
- mov ebp, esp
- // load receiver and selector
- mov ecx, SELECTOR
- mov eax, SELF
- // check whether receiver is nil
- test eax, eax
- je NIL
- // receiver (in eax) is non-nil: search the cache
- mov edx, isa[eax]
- // CacheLookup WORD_RETURN, MSG_SEND
- push edi
- mov edi, cache[edx]
- push esi
- mov esi, mask[edi]
- mov edx, ecx
- shr edx, 2
- SCAN:
- and edx, esi
- mov eax, buckets[edi][edx*4]
- test eax, eax
- je MISS
- cmp ecx, method_name[eax]
- je HIT
- add edx, 1
- jmp SCAN
- HIT:
- mov eax, method_imp[eax]
- pop esi
- pop edi
- mov edx, kFwdMsgSend
- leave
- jmp eax
- // cache miss: search method lists
- MISS:
- pop esi
- pop edi
- mov edx, SELF
- mov eax, isa[edx]
- // MethodTableLookup WORD_RETURN, MSG_SEND
- push $3
- push eax
- push ecx
- push edx
- call lookUpImpOrFoward
- mov edx, kFwdMsgSend
- leave
- jmp eax
- // message send to nil: return zero
- NIL:
- fldz
- leave
- ret
- }
- }
- OBJC_EXPORT __declspec(naked) id objc_msgSendSuper(struct objc_super *a, SEL b, ...)
- {
- __asm {
- push ebp
- mov ebp, esp
- // load class and selector
- mov eax, SUPER
- mov ecx, SELECTOR
- mov edx, super_class[eax]
- // search the cache (class in edx)
- // CacheLookup WORD_RETURN, MSG_SENDSUPER
- push edi
- mov edi, cache[edx]
- push esi
- mov esi, mask[edi]
- mov edx, ecx
- shr edx, 2
- SCAN:
- and edx, esi
- mov eax, buckets[edi][edx*4]
- test eax, eax
- je MISS
- cmp ecx, method_name[eax]
- je HIT
- add edx, 1
- jmp SCAN
- HIT:
- mov eax, method_imp[eax]
- pop esi
- pop edi
- mov edx, SUPER
- mov edx, super_receiver[edx]
- mov SUPER, edx
- mov edx, kFwdMsgSend
- leave
- jmp eax
- // cache miss: search method lists
- MISS:
- pop esi
- pop edi
- mov eax, SUPER
- mov edx, super_receiver[eax]
- mov SUPER, edx
- mov eax, super_class[eax]
- // MethodTableLookup WORD_RETURN, MSG_SENDSUPER
- push $3
- push eax
- push ecx
- push edx
- call lookUpImpOrFoward
- mov edx, kFwdMsgSend
- leave
- jmp eax
- }
- }
- OBJC_EXPORT __declspec(naked) void objc_msgSend_stret(void)
- {
- __asm {
- push ebp
- mov ebp, esp
- // load receiver and selector
- mov ecx, SELECTOR_STRET
- mov eax, SELF_STRET
- // check whether receiver is nil
- test eax, eax
- je NIL
- // receiver (in eax) is non-nil: search the cache
- mov edx, isa[eax]
- // CacheLookup WORD_RETURN, MSG_SEND
- push edi
- mov edi, cache[edx]
- push esi
- mov esi, mask[edi]
- mov edx, ecx
- shr edx, 2
- SCAN:
- and edx, esi
- mov eax, buckets[edi][edx*4]
- test eax, eax
- je MISS
- cmp ecx, method_name[eax]
- je HIT
- add edx, 1
- jmp SCAN
- HIT:
- mov eax, method_imp[eax]
- pop esi
- pop edi
- mov edx, kFwdMsgSendStret
- leave
- jmp eax
- // cache miss: search method lists
- MISS:
- pop esi
- pop edi
- mov edx, SELF_STRET
- mov eax, isa[edx]
- // MethodTableLookup WORD_RETURN, MSG_SEND
- push $3
- push eax
- push ecx
- push edx
- call lookUpImpOrFoward
- mov edx, kFwdMsgSendStret
- leave
- jmp eax
- // message send to nil: return zero
- NIL:
- // eax is already zero
- mov edx, 0
- leave
- ret
- }
- }
- OBJC_EXPORT __declspec(naked) id objc_msgSendSuper_stret(struct objc_super *a, SEL b, ...)
- {
- __asm {
- push ebp
- mov ebp, esp
- // load class and selector
- mov eax, SUPER_STRET
- mov ecx, SELECTOR_STRET
- mov edx, super_class[eax]
- // search the cache (class in edx)
- // CacheLookup WORD_RETURN, MSG_SENDSUPER
- push edi
- mov edi, cache[edx]
- push esi
- mov esi, mask[edi]
- mov edx, ecx
- shr edx, 2
- SCAN:
- and edx, esi
- mov eax, buckets[edi][edx*4]
- test eax, eax
- je MISS
- cmp ecx, method_name[eax]
- je HIT
- add edx, 1
- jmp SCAN
- HIT:
- mov eax, method_imp[eax]
- pop esi
- pop edi
- mov edx, SUPER_STRET
- mov edx, super_receiver[edx]
- mov SUPER_STRET, edx
- mov edx, kFwdMsgSendStret
- leave
- jmp eax
- // cache miss: search method lists
- MISS:
- pop esi
- pop edi
- mov eax, SUPER_STRET
- mov edx, super_receiver[eax]
- mov SUPER_STRET, edx
- mov eax, super_class[eax]
- // MethodTableLookup WORD_RETURN, MSG_SENDSUPER
- push $3
- push eax
- push ecx
- push edx
- call lookUpImpOrFoward
- mov edx, kFwdMsgSendStret
- leave
- jmp eax
- }
- }
- OBJC_EXPORT __declspec(naked) id _objc_msgForward(id a, SEL b, ...)
- {
- __asm {
- mov ecx, _objc_forward_handler
- jmp ecx
- }
- }
- OBJC_EXPORT __declspec(naked) id _objc_msgForward_stret(id a, SEL b, ...)
- {
- __asm {
- mov ecx, _objc_forward_stret_handler
- jmp ecx
- }
- }
- __declspec(naked) id _objc_msgForward_cached(id a, SEL b, ...)
- {
- __asm {
- cmp edx, kFwdMsgSendStret
- je STRET
- jmp _objc_msgForward
- STRET:
- jmp _objc_msgForward_stret
- }
- }
- OBJC_EXPORT __declspec(naked) void method_invoke(void)
- {
- __asm {
- push ebp
- mov ebp, esp
- mov ecx, SELECTOR
- mov edx, method_name[ecx]
- mov eax, method_imp[ecx]
- mov SELECTOR, edx
- leave
- jmp eax
- }
- }
- OBJC_EXPORT __declspec(naked) void method_invoke_stret(void)
- {
- __asm {
- push ebp
- mov ebp, esp
- mov ecx, SELECTOR_STRET
- mov edx, method_name[ecx]
- mov eax, method_imp[ecx]
- mov SELECTOR_STRET, edx
- leave
- jmp eax
- }
- }
|