rewrite.inc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. Legal:
  3. Version: MPL 1.1
  4. The contents of this file are subject to the Mozilla Public License Version
  5. 1.1 the "License"; you may not use this file except in compliance with
  6. the License. You may obtain a copy of the License at
  7. http://www.mozilla.org/MPL/
  8. Software distributed under the License is distributed on an "AS IS" basis,
  9. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  10. for the specific language governing rights and limitations under the
  11. License.
  12. The Original Code is the YSI framework.
  13. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  14. Portions created by the Initial Developer are Copyright C 2011
  15. the Initial Developer. All Rights Reserved.
  16. Contributors:
  17. Y_Less
  18. koolk
  19. JoeBullet/Google63
  20. g_aSlice/Slice
  21. Misiur
  22. samphunter
  23. tianmeta
  24. maddinat0r
  25. spacemud
  26. Crayder
  27. Dayvison
  28. Ahmad45123
  29. Zeex
  30. irinel1996
  31. Yiin-
  32. Chaprnks
  33. Konstantinos
  34. Masterchen09
  35. Southclaws
  36. PatchwerkQWER
  37. m0k1
  38. paulommu
  39. udan111
  40. Thanks:
  41. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  42. ZeeX - Very productive conversations.
  43. koolk - IsPlayerinAreaEx code.
  44. TheAlpha - Danish translation.
  45. breadfish - German translation.
  46. Fireburn - Dutch translation.
  47. yom - French translation.
  48. 50p - Polish translation.
  49. Zamaroht - Spanish translation.
  50. Los - Portuguese translation.
  51. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes for
  52. me to strive to better.
  53. Pixels^ - Running XScripters where the idea was born.
  54. Matite - Pestering me to release it and using it.
  55. Very special thanks to:
  56. Thiadmer - PAWN, whose limits continue to amaze me!
  57. Kye/Kalcor - SA:MP.
  58. SA:MP Team past, present and future - SA:MP.
  59. Optional plugins:
  60. Gamer_Z - GPS.
  61. Incognito - Streamer.
  62. Me - sscanf2, fixes2, Whirlpool.
  63. */
  64. static stock
  65. YSI_g_sNextJumpPoint;
  66. stock LAM@1()
  67. {
  68. //printf("LAM@1");
  69. static
  70. addrLAM@1 = 0;
  71. if (!addrLAM@1)
  72. {
  73. #emit CONST.pri LAM@1
  74. #emit STOR.pri addrLAM@1
  75. addrLAM@1 += AMX_REAL_DATA + AMX_HEADER_COD;
  76. P:5("LAM@1: LAM@1 addresses = %d %d %d %d", GetAmxBaseAddress(), addrLAM@1 - (AMX_REAL_DATA + AMX_HEADER_COD), AMX_REAL_DATA + AMX_HEADER_COD, addrLAM@1);
  77. //printf("LAM@0: LAM@1 addresses = %d %d %d %d", GetAmxBaseAddress(), addrLAM@1 - (AMX_REAL_DATA + AMX_HEADER_COD), AMX_REAL_DATA + AMX_HEADER_COD, addrLAM@1);
  78. }
  79. // Rewrite our call code to jump to "YSI_g_sNextJumpPoint". Then adjust
  80. // that to our return point, and jump to the new code.
  81. new
  82. addrRet = GetCurrentFrameReturn(),
  83. addrNext = YSI_g_sNextJumpPoint;
  84. SetCurrentFrameReturn(addrNext);
  85. YSI_g_sNextJumpPoint = addrRet,
  86. addrRet -= 16;
  87. {
  88. new
  89. dis[DisasmContext];
  90. DisasmInit(dis, addrRet);
  91. if (DisasmNextInsn(dis) != OP_PUSH_C || DisasmGetOperand(dis) != 0 ||
  92. DisasmNextInsn(dis) != OP_CALL || DisasmGetOperand(dis) != addrLAM@1)
  93. {
  94. P:E("Unknown call of LAM@1");
  95. return 0;
  96. }
  97. }
  98. {
  99. new
  100. ctx[AsmContext];
  101. AsmInitPtr(ctx, addrRet + AMX_HEADER_COD, 16);
  102. // Replace with a jump to a point after "@LAM@1".
  103. @emit JUMP (addrNext + AMX_REAL_DATA + AMX_HEADER_COD)
  104. // Code to POP the result.
  105. @emit NOP
  106. @emit NOP
  107. }
  108. return 1;
  109. }
  110. stock LAM@2(par)
  111. {
  112. //printf("LAM@2");
  113. #pragma unused par
  114. static
  115. addrLAM@2 = 0;
  116. if (!addrLAM@2)
  117. {
  118. #emit CONST.pri LAM@2
  119. #emit STOR.pri addrLAM@2
  120. addrLAM@2 += AMX_REAL_DATA + AMX_HEADER_COD;
  121. P:5("LAM@2: LAM@2 addresses = %d %d %d %d", GetAmxBaseAddress(), addrLAM@2 - (AMX_REAL_DATA + AMX_HEADER_COD), AMX_REAL_DATA + AMX_HEADER_COD, addrLAM@2);
  122. //printf("LAM@0: LAM@1 addresses = %d %d %d %d", GetAmxBaseAddress(), addrLAM@2 - (AMX_REAL_DATA + AMX_HEADER_COD), AMX_REAL_DATA + AMX_HEADER_COD, addrLAM@2);
  123. }
  124. // Rewrite our call code to jump to "YSI_g_sNextJumpPoint". Then adjust
  125. // that to our return point, and jump to the new code.
  126. new
  127. addrRet = GetCurrentFrameReturn(),
  128. addrNext = YSI_g_sNextJumpPoint;
  129. SetCurrentFrameReturn(addrNext);
  130. YSI_g_sNextJumpPoint = addrRet,
  131. addrRet -= 16;
  132. {
  133. new
  134. dis[DisasmContext];
  135. DisasmInit(dis, addrRet);
  136. if (DisasmNextInsn(dis) != OP_PUSH_C || DisasmGetOperand(dis) != 4 ||
  137. DisasmNextInsn(dis) != OP_CALL || DisasmGetOperand(dis) != addrLAM@2)
  138. {
  139. P:E("Unknown call of LAM@2");
  140. return 0;
  141. }
  142. }
  143. {
  144. new
  145. ctx[AsmContext];
  146. AsmInitPtr(ctx, addrRet + AMX_HEADER_COD, 16);
  147. // Replace with a jump to a point after "LAM@0".
  148. @emit JUMP (addrNext - 4 + AMX_REAL_DATA + AMX_HEADER_COD)
  149. // Code to POP the result.
  150. @emit NOP
  151. @emit NOP
  152. }
  153. return par;
  154. }
  155. stock LAM@0()
  156. {
  157. static
  158. addrLAM@0 = 0,
  159. addrLAM@1 = 0;
  160. if (!addrLAM@1)
  161. {
  162. #emit CONST.pri LAM@0
  163. #emit STOR.pri addrLAM@0
  164. addrLAM@0 += AMX_REAL_DATA + AMX_HEADER_COD;
  165. P:5("LAM@0: LAM@0 addresses = %d %d %d %d", GetAmxBaseAddress(), addrLAM@0 - (AMX_REAL_DATA + AMX_HEADER_COD), AMX_REAL_DATA + AMX_HEADER_COD, addrLAM@0);
  166. //printf("LAM@0: LAM@0 addresses = %d %d %d %d", GetAmxBaseAddress(), addrLAM@0 - (AMX_REAL_DATA + AMX_HEADER_COD), AMX_REAL_DATA + AMX_HEADER_COD, addrLAM@0);
  167. #emit CONST.pri LAM@1
  168. #emit STOR.pri addrLAM@1
  169. addrLAM@1 += AMX_REAL_DATA + AMX_HEADER_COD;
  170. P:5("LAM@0: LAM@1 addresses = %d %d %d %d", GetAmxBaseAddress(), addrLAM@1 - (AMX_REAL_DATA + AMX_HEADER_COD), AMX_REAL_DATA + AMX_HEADER_COD, addrLAM@1);
  171. //printf("LAM@0: LAM@1 addresses = %d %d %d %d", GetAmxBaseAddress(), addrLAM@1 - (AMX_REAL_DATA + AMX_HEADER_COD), AMX_REAL_DATA + AMX_HEADER_COD, addrLAM@1);
  172. }
  173. // Get the return address.
  174. new
  175. addrRet = GetCurrentFrameReturn(),
  176. addrNext;
  177. YSI_g_sNextJumpPoint = addrRet,
  178. addrRet -= 16,
  179. SetCurrentFrameReturn(addrRet);
  180. {
  181. new
  182. dis[DisasmContext];
  183. // Move on until we find the next call to "@LAM@1".
  184. DisasmInit(dis, YSI_g_sNextJumpPoint);
  185. new
  186. Opcode:nxt = DisasmNextInsn(dis),
  187. i = 0;
  188. while (i != 100)
  189. {
  190. if (nxt == OP_PUSH_C && DisasmGetOperand(dis) == 0)
  191. {
  192. nxt = DisasmNextInsn(dis);
  193. if (nxt == OP_CALL && DisasmGetOperand(dis) == addrLAM@1)
  194. {
  195. P:6("LAM@0: Found %d", DisasmGetOperand(dis));
  196. break;
  197. }
  198. }
  199. else nxt = DisasmNextInsn(dis);
  200. ++i;
  201. }
  202. if (i == 10)
  203. {
  204. P:E("Unable to find LAM@1");
  205. return 0;
  206. }
  207. addrNext = DisasmGetNextIp(dis) + AMX_REAL_DATA; // - AMX_HEADER_COD; //(AMX_REAL_DATA + AMX_HEADER_COD);
  208. // Go backwards to before this function was called.
  209. DisasmInit(dis, addrRet);
  210. if (DisasmNextInsn(dis) != OP_PUSH_C || DisasmGetOperand(dis) != 0 ||
  211. DisasmNextInsn(dis) != OP_CALL || DisasmGetOperand(dis) != addrLAM@0)
  212. {
  213. P:E("Unknown call of LAM@0");
  214. return 0;
  215. }
  216. }
  217. {
  218. new
  219. ctx[AsmContext];
  220. AsmInitPtr(ctx, addrRet + AMX_HEADER_COD, 16);
  221. // Replace with a jump to a point after "@LAM@1".
  222. @emit JUMP addrNext
  223. // Code to POP the result.
  224. @emit NOP
  225. @emit POP.pri
  226. }
  227. return 1;
  228. }
  229. /*
  230. - The original code is:
  231. myVar = FoldR({ _0 + _1 }, arr, 0);
  232. - The compiled code generated is:
  233. myVar = @LAM0();
  234. @LAM1();
  235. inline Func(_0, _1) YSI_gInlineRet += _0 + _1;
  236. @LAM2(FoldR("Func", arr, 0));
  237. - The rewritten code is:
  238. goto Func:
  239. :Write
  240. myVar = I@;
  241. goto Cont;
  242. :Func
  243. // Inline code.
  244. I@ = FoldR("Func", arr, 0);
  245. goto Write;
  246. :Cont
  247. */