addressof.inc 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright (C) 2016 Y_Less
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the "Software"),
  5. // to deal in the Software without restriction, including without limitation
  6. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. // and/or sell copies of the Software, and to permit persons to whom the
  8. // Software is furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  14. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  19. // DEALINGS IN THE SOFTWARE.
  20. #if defined ADDRESSOF_INC
  21. #endinput
  22. #endif
  23. #define ADDRESSOF_INC
  24. #include "frame_info"
  25. #include "disasm"
  26. // This code uses two nested conditionals because of:
  27. // https://github.com/Zeex/pawn/issues/96 (this doesn't work):
  28. //
  29. // (O@A_() ? ((CALL@%1), 0) : (O@V_))
  30. //
  31. // Even though it is the obvious solution when you don't want the result of
  32. // "CALL@%1" to be used (as it may not exist), and you can't use a constant
  33. // instead of "O@V_" because then if becomes a constant in a
  34. // condition, which the compiler rightly complains about. Of course, because
  35. // "O@A_()" always returns "false", the entire other block of code
  36. // is jumped over.
  37. #define addressof(%1) (O@A_()?(((CALL@%1),O@V_)?1:2):(O@V_))
  38. #define CALL@%0\32; CALL@
  39. #define gAddressOfReturnVar_ O@V_
  40. #define AddressOfGetNextCall_ O@A_
  41. #define CALL@AddressOfGetNextCall_ CALL@O@A_
  42. #define CALL@O@A_ O@A_()
  43. #define O@A_()?(((CALL@%1<%2>),O@V_)?1:2):(O@V_) (O@A_()?(((_ADDR@$%1()<%2>),O@V_)?(F@_@:1):(F@_@:2)):(F@_@:O@V_))
  44. // Enable parameter checks for `Func<xxx>` types.
  45. #define _ADDR@ _ADDR@i:_ADDR@t:_ADDR@f:_ADDR@v:_ADDR@s:_ADDR@a:_ADDR@x:_ADDR@z
  46. #define _ADDR@z$%0(%1)<%2> _ADDR@y:%0(%1)
  47. #define _ADDR@y:%0(,%1) %0(%1)
  48. #define _ADDR@i:%8$%0(%1)<i%2>),O@V_)?(%9:1):(%9:2)):(%9:O@V_)) _ADDR@$%0(%1,0)<%2>),O@V_)?(%9i:1):(%9i:2)):(%9i:O@V_))
  49. #define _ADDR@t:%8$%0(%1)<t%5:%2>),O@V_)?(%9:1):(%9:2)):(%9:O@V_)) _ADDR@$%0(%1,%5:0)<%2>),O@V_)?(%9i:1):(%9i:2)):(%9i:O@V_))
  50. #define _ADDR@f:%8$%0(%1)<f%2>),O@V_)?(%9:1):(%9:2)):(%9:O@V_)) _ADDR@$%0(%1,0.0)<%2>),O@V_)?(%9f:1):(%9f:2)):(%9f:O@V_))
  51. #define _ADDR@v:%8$%0(%1)<v%2>),O@V_)?(%9:1):(%9:2)):(%9:O@V_)) _ADDR@$%0(%1,__REF)<%2>),O@V_)?(%9v:1):(%9v:2)):(%9v:O@V_))
  52. #define _ADDR@s:%8$%0(%1)<s%2>),O@V_)?(%9:1):(%9:2)):(%9:O@V_)) _ADDR@$%0(%1,__ARR)<%2>),O@V_)?(%9s:1):(%9s:2)):(%9s:O@V_))
  53. #define _ADDR@a:%8$%0(%1)<a%2>),O@V_)?(%9:1):(%9:2)):(%9:O@V_)) _ADDR@$%0(%1,__ARR)<%2>),O@V_)?(%9a:1):(%9a:2)):(%9a:O@V_))
  54. #define _ADDR@x:%8$%0(%1)<x%2>),O@V_)?(%9:1):(%9:2)):(%9:O@V_)) _ADDR@z$%0(%1)<>),O@V_)?(%9x:1):(%9x:2)):(%9x:O@V_))
  55. stock
  56. __ARR[1],
  57. __REF,
  58. gAddressOfReturnVar_ = 0;
  59. stock bool:AddressOfGetNextCall_() {
  60. // Start reading code from the point to which this function returns, looking
  61. // for the next "CALL" op to signal the function call from the macro.
  62. new ctx[DisasmContext];
  63. DisasmInit(ctx, GetCurrentFrameReturn());
  64. while (DisasmNext(ctx)) {
  65. if (DisasmGetOpcode(ctx) == OP_CALL) {
  66. // Return the data in a global, to be repassed from the conditional.
  67. gAddressOfReturnVar_ = DisasmGetOperandReloc(ctx);
  68. return false;
  69. }
  70. }
  71. // ALWAYS returns false so that the function call within "OP(&func)" will
  72. // never be called thanks to the conditional.
  73. return false;
  74. }