home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / unix / unixtools / util / asm / caller next >
Text File  |  1992-07-21  |  2KB  |  71 lines

  1. ; Return the name of the (2nd level) caller of the caller.
  2. ;
  3. ; e.g. if you have (in C)
  4. ;
  5. ; void foo (void)
  6. ; {
  7. ;         bar();
  8. ; }
  9. ;
  10. ; void bar (void)
  11. ; {
  12. ;         char *func = caller();
  13. ; }
  14. ;
  15. ; func should contain "bar".
  16. ;
  17. a1 RN 0
  18. a2 RN 1
  19. a3 RN 2
  20. a4 RN 3
  21. v1 RN 4
  22. v2 RN 5
  23. v3 RN 6
  24. v4 RN 7
  25. v5 RN 8
  26. v6 RN 9
  27. fp RN 10
  28. ip RN 11
  29. sp RN 12
  30. sl RN 13
  31. lk RN 14
  32. pc RN 15
  33.  
  34.         AREA |Caller|, CODE, READONLY
  35.  
  36. name
  37.         DCB     "caller",0
  38.         ALIGN
  39.         DCD     &FF000000 :OR: ( {PC} - name )
  40.  
  41.         EXPORT  caller
  42.  
  43. caller
  44.         TEQ     fp, #0                 ; If frame pointer is 0 then
  45.         ADREQ   a1, null               ; there is no backtrace structure
  46.         MOVEQS  pc, lk
  47.  
  48.         LDR     a1, [fp, #-12]         ; Get caller's frame pointer
  49.         TEQ     a1, #0                 ; If 0, no backtrace structure
  50.         ADREQ   a1, null
  51.         MOVEQS  pc, lk
  52.  
  53.         LDR     a1, [a1]               ; Get caller's PC (return data save)
  54.         BIC     a1, a1, #&FC000003     ; Convert to an address
  55.         SUB     a1, a1, #12            ; Go to return data save instruction
  56.  
  57. prev    LDR     a2, [a1, #-4]!         ; Get previous word
  58.         MOV     a3, a2, LSR #24        ; Test top 8 bits
  59.         TEQ     a3, #&FF               ; If they're FF, we've found the name
  60.         BNE     prev
  61.  
  62.         BIC     a2, a2, #&FF000000     ; Get the name offset
  63.         SUB     a1, a1, a2             ; Point to the caller's name
  64.         MOVS    pc, lk                 ; Return
  65.  
  66. null
  67.         DCB     "",0
  68.         ALIGN
  69.  
  70.         END
  71.