home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / clib / progs / utilslib / Asm / Caller next >
Encoding:
Text File  |  1994-02-22  |  1.7 KB  |  76 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. ;
  18. ; NOTE: This routine RELIES on the Risc-OS Arm procedure call standard
  19. ;       binding (APCS-R). In particular, it will NOT work on code
  20. ;       compiled under Ansi C v2.00 or earlier!
  21. ;
  22. a1 RN 0
  23. a2 RN 1
  24. a3 RN 2
  25. a4 RN 3
  26. v1 RN 4
  27. v2 RN 5
  28. v3 RN 6
  29. v4 RN 7
  30. v5 RN 8
  31. v6 RN 9
  32. sl RN 10
  33. fp RN 11
  34. ip RN 12
  35. sp RN 13
  36. lk RN 14
  37. pc RN 15
  38.  
  39.         AREA |Caller|, CODE, READONLY
  40.  
  41. name
  42.         DCB     "caller",0
  43.         ALIGN
  44.         DCD     &FF000000 :OR: ( {PC} - name )
  45.  
  46.         EXPORT  caller
  47.  
  48. caller
  49.         TEQ     fp, #0                 ; If frame pointer is 0 then
  50.         ADREQ   a1, null               ; there is no backtrace structure
  51.         MOVEQS  pc, lk
  52.  
  53.         LDR     a1, [fp, #-12]         ; Get caller's frame pointer
  54.         TEQ     a1, #0                 ; If 0, no backtrace structure
  55.         ADREQ   a1, null
  56.         MOVEQS  pc, lk
  57.  
  58.         LDR     a1, [a1]               ; Get caller's PC (return data save)
  59.         BIC     a1, a1, #&FC000003     ; Convert to an address
  60.         SUB     a1, a1, #12            ; Go to return data save instruction
  61.  
  62. prev    LDR     a2, [a1, #-4]!         ; Get previous word
  63.         MOV     a3, a2, LSR #24        ; Test top 8 bits
  64.         TEQ     a3, #&FF               ; If they're FF, we've found the name
  65.         BNE     prev
  66.  
  67.         BIC     a2, a2, #&FF000000     ; Get the name offset
  68.         SUB     a1, a1, a2             ; Point to the caller's name
  69.         MOVS    pc, lk                 ; Return
  70.  
  71. null
  72.         DCB     "",0
  73.         ALIGN
  74.  
  75.         END
  76.