y_cgen.inc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #if defined _INC_y_cgen
  2. #endinput
  3. #endif
  4. #define _INC_y_cgen
  5. /*+
  6. * <library name="y_cgen">
  7. * <section>
  8. * Description
  9. * </section>
  10. * Large chunks of code that isn't used, so that the space can be reclaimed and
  11. * reused by other code generating functions.
  12. * <section>
  13. * Version
  14. * </section>
  15. * 1.0
  16. * </library>
  17. *//** *//*
  18. Legal:
  19. Version: MPL 1.1
  20. The contents of this file are subject to the Mozilla Public License Version
  21. 1.1 the "License"; you may not use this file except in compliance with
  22. the License. You may obtain a copy of the License at
  23. http://www.mozilla.org/MPL/
  24. Software distributed under the License is distributed on an "AS IS" basis,
  25. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  26. for the specific language governing rights and limitations under the
  27. License.
  28. The Original Code is the YSI framework.
  29. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  30. Portions created by the Initial Developer are Copyright C 2011
  31. the Initial Developer. All Rights Reserved.
  32. Contributors:
  33. Y_Less
  34. koolk
  35. JoeBullet/Google63
  36. g_aSlice/Slice
  37. Misiur
  38. samphunter
  39. tianmeta
  40. maddinat0r
  41. spacemud
  42. Crayder
  43. Dayvison
  44. Ahmad45123
  45. Zeex
  46. irinel1996
  47. Yiin-
  48. Chaprnks
  49. Konstantinos
  50. Masterchen09
  51. Southclaws
  52. PatchwerkQWER
  53. m0k1
  54. paulommu
  55. udan111
  56. Thanks:
  57. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  58. ZeeX - Very productive conversations.
  59. koolk - IsPlayerinAreaEx code.
  60. TheAlpha - Danish translation.
  61. breadfish - German translation.
  62. Fireburn - Dutch translation.
  63. yom - French translation.
  64. 50p - Polish translation.
  65. Zamaroht - Spanish translation.
  66. Los - Portuguese translation.
  67. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes for
  68. me to strive to better.
  69. Pixels^ - Running XScripters where the idea was born.
  70. Matite - Pestering me to release it and using it.
  71. Very special thanks to:
  72. Thiadmer - PAWN, whose limits continue to amaze me!
  73. Kye/Kalcor - SA:MP.
  74. SA:MP Team past, present and future - SA:MP.
  75. Optional plugins:
  76. Gamer_Z - GPS.
  77. Incognito - Streamer.
  78. Me - sscanf2, fixes2, Whirlpool.
  79. */
  80. #include "..\..\YSI_Core\y_core\y_thirdpartyinclude"
  81. CHAIN_HOOK(CGen)
  82. #undef CHAIN_ORDER
  83. #define CHAIN_ORDER CHAIN_NEXT(CGen)
  84. #if !defined CGEN_MEMORY
  85. #define CGEN_MEMORY (10000)
  86. #endif
  87. static stock
  88. YSI_g_sCodeSpace = -1,
  89. YSI_g_sCodeEnd = -1;
  90. static stock CGEN(&a = 1, &b = 1, &c = 1, &d = 1, &e = 1, &f = 1, &g = 1, &h = 1, &i = 1, &j = 1, &k = 1, &l = 1, &m = 1, &n = 1, &o = 1, &p = 1, &q = 1, &r = 1, &s = 1, &t = 1, &u = 1, &v = 1, &w = 1, &x = 1, &y = 1, &z = 1)
  91. {
  92. // We use `= 1` instead of `= 0` because that takes slightly more code - it
  93. // is a `CONST.pri 1` not `ZERO.pri`.
  94. #pragma unused a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
  95. }
  96. forward _@_y_cgen_@_0();
  97. public _@_y_cgen_@_0()
  98. {
  99. // Reserve a huge area of "COD" for our own use!
  100. // This is currently many calls to `CGEN()`, which takes up huge amounts of
  101. // space because of all the default parameters, which means we get to pass
  102. // loads without having them in code limiting the line lengths. This gives:
  103. //
  104. // bytes_per_call = ((6 * 4 * 26) + 6)
  105. // bytes_per_call = 630
  106. // bytes_per_line = bytes_per_call * 16
  107. // bytes_per_line = 10080 (~10KiB)
  108. // N = 16.25
  109. //
  110. #if CGEN_MEMORY >= 10000
  111. CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();
  112. #endif
  113. #if CGEN_MEMORY >= 20000
  114. CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();
  115. #endif
  116. #if CGEN_MEMORY >= 30000
  117. CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();
  118. #endif
  119. #if CGEN_MEMORY >= 40000
  120. CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();
  121. #endif
  122. #if CGEN_MEMORY >= 50000
  123. CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();
  124. #endif
  125. #if CGEN_MEMORY >= 60000
  126. CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();
  127. #endif
  128. #if CGEN_MEMORY >= 70000
  129. CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();
  130. #endif
  131. #if CGEN_MEMORY >= 80000
  132. CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();
  133. #endif
  134. #if CGEN_MEMORY >= 90000
  135. CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();CGEN();
  136. #endif
  137. #if CGEN_MEMORY >= 100000
  138. #error Too much codespace requested, please edit YSI_Coding\y_cgen\y_cgen
  139. #endif
  140. }
  141. forward _@_y_cgen_@_1();
  142. public _@_y_cgen_@_1()
  143. {
  144. // This function SHOULD come straight after "_@_y_hooks_@_0" in both the
  145. // public functions table and code segment if the compiler is true to form.
  146. }
  147. static stock CGen_GetAddr(const func[])
  148. {
  149. return AMX_Read(funcidx(func) * 8 + AMX_HEADER_PUBLICS) + AMX_HEADER_COD;
  150. }
  151. static stock CGen_SetupCodeSpace()
  152. {
  153. if (YSI_g_sCodeSpace != -1) return;
  154. YSI_g_sCodeSpace = CGen_GetAddr("_@_y_cgen_@_0"),
  155. YSI_g_sCodeEnd = CGen_GetAddr("_@_y_cgen_@_1"),
  156. // Rewrite "_@_y_cgen_@_0" to just "return 0;".
  157. AMX_Write(YSI_g_sCodeSpace, _:RelocateOpcode(OP_PROC)),
  158. AMX_Write(YSI_g_sCodeSpace + 4, _:RelocateOpcode(OP_ZERO_PRI)),
  159. AMX_Write(YSI_g_sCodeSpace + 8, _:RelocateOpcode(OP_RETN)),
  160. YSI_g_sCodeSpace += 12;
  161. }
  162. public OnCodeInit()
  163. {
  164. CGen_SetupCodeSpace();
  165. #if defined CGen_OnCodeInit
  166. CGen_OnCodeInit();
  167. #endif
  168. // Check that the code at this point is valid. An OpCode is at most two
  169. // bytes (except "OP_SWITCH", but there are none of them in this area of
  170. // code because there are no "switch"es). If the code here fails to
  171. // decompile, it is probably out of alignment - add a single "NOP" to bring
  172. // it back in to alignment.
  173. new
  174. ctx[DisasmContext];
  175. DisasmInit(ctx, YSI_g_sCodeSpace - AMX_HEADER_COD);
  176. for (new i = 0; i != 10; ++i)
  177. {
  178. if (DisasmNextInsn(ctx) == OP_NONE)
  179. {
  180. AMX_Write(YSI_g_sCodeSpace, _:RelocateOpcode(OP_NOP));
  181. return 1;
  182. }
  183. }
  184. return 1;
  185. }
  186. #undef OnCodeInit
  187. #define OnCodeInit CGen_OnCodeInit
  188. #if defined CGen_OnCodeInit
  189. forward CGen_OnCodeInit();
  190. #endif
  191. forward CGen_OOM(ctx[AsmContext]);
  192. public CGen_OOM(ctx[AsmContext])
  193. {
  194. switch (AsmGetError(ctx))
  195. {
  196. case ASM_ERROR_OPCODE:
  197. {
  198. P:E("Unknown opcode written to code generation (CGen) space.");
  199. }
  200. case ASM_ERROR_OPERAND:
  201. {
  202. P:E("Invalid operand written to code generation (CGen) space.");
  203. }
  204. case ASM_ERROR_SPACE:
  205. {
  206. static excess = 0;
  207. // This is just an estimate of how much more space is required, assuming that each opcode
  208. // has (on average) half a parameter.
  209. excess += 6;
  210. P:F("Out of code generation (CGen) space. The current value of `CGEN_MEMORY` is `%d`, please recompile with a higher value (approximately %d).", CGEN_MEMORY, CGEN_MEMORY + excess);
  211. }
  212. case ASM_ERROR_LABEL_OVERFLOW:
  213. {
  214. P:F("Out of label space. The current value of `ASM_MAX_LABELS` is `%d`, please recompile with a higher value.", ASM_MAX_LABELS);
  215. }
  216. case ASM_ERROR_LABEL_DUPLICATE:
  217. {
  218. P:F("Duplicate label in code generation.");
  219. }
  220. }
  221. AsmClearError(ctx);
  222. }
  223. stock CGen_UseCodeSpace(ctx[AsmContext])
  224. {
  225. AsmInitPtr(ctx, YSI_g_sCodeSpace, YSI_g_sCodeEnd - YSI_g_sCodeSpace);
  226. AsmSetErrorHandlerName(ctx, "CGen_OOM");
  227. }
  228. stock CGen_GetCodeSpace()
  229. {
  230. return YSI_g_sCodeSpace;
  231. }
  232. stock CGen_AddCodeSpace(num)
  233. {
  234. if (YSI_g_sCodeSpace == -1) P:E("YSI_g_sCodeSpace is -1 in \"CGen_AddCodeSpace\"");
  235. else YSI_g_sCodeSpace += num;
  236. }