1
0

addressof.inc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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_) F@_@%2:(O@A_()?(((CALL@%1),O@V_)?1:2):(O@V_))
  44. stock
  45. REF,
  46. gAddressOfReturnVar_ = 0;
  47. stock bool:AddressOfGetNextCall_() {
  48. // Start reading code from the point to which this function returns, looking
  49. // for the next "CALL" op to signal the function call from the macro.
  50. new ctx[DisasmContext];
  51. DisasmInit(ctx, GetCurrentFrameReturn());
  52. while (DisasmNext(ctx)) {
  53. if (DisasmGetOpcode(ctx) == OP_CALL) {
  54. // Return the data in a global, to be repassed from the conditional.
  55. gAddressOfReturnVar_ = DisasmGetOperandReloc(ctx);
  56. return false;
  57. }
  58. }
  59. // ALWAYS returns false so that the function call within "OP(&func)" will
  60. // never be called thanks to the conditional.
  61. return false;
  62. }