dynamic_call.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. // Copyright (C) 2012 Zeex
  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 DYNAMIC_CALL_INC
  21. #endinput
  22. #endif
  23. #define DYNAMIC_CALL_INC
  24. #include <a_samp>
  25. #include "amx_header"
  26. #include "amx_memory"
  27. #include "opcode"
  28. #if !defined DYNAMIC_CALL_MAX_ARGS
  29. #define DYNAMIC_CALL_MAX_ARGS 256
  30. #endif
  31. forward bool:Push(arg);
  32. forward bool:PushString(const string[]);
  33. forward bool:Pop(&arg = 0);
  34. forward Call(address, bool:auto_pop = true);
  35. forward SysreqC(index, bool:auto_pop = true);
  36. forward SysreqD(address, bool:auto_pop = true);
  37. forward CallN(address, args_to_push, bool:auto_pop = true);
  38. forward SysreqCN(index, args_to_push, bool:auto_pop = true);
  39. forward SysreqDN(address, args_to_push, bool:auto_pop = true);
  40. forward CallFunction(address, {Float,_}:...);
  41. forward CallNative(index, {Float,_}:...);
  42. forward CallNativeByAddress(address, {Float,_}:...);
  43. static stock g_nargs = 0;
  44. static stock g_args[DYNAMIC_CALL_MAX_ARGS];
  45. stock bool:Push(arg) {
  46. if (g_nargs < sizeof(g_args)) {
  47. g_args[g_nargs++] = arg;
  48. return true;
  49. }
  50. return false;
  51. }
  52. stock bool:PushString(const string[]) {
  53. new address;
  54. #emit load.s.pri string
  55. #emit stor.s.pri address
  56. return Push(address);
  57. }
  58. stock bool:Pop(&arg = 0) {
  59. if (g_nargs > 0) {
  60. arg = g_args[--g_nargs];
  61. return true;
  62. }
  63. return false;
  64. }
  65. stock Call(address, bool:auto_pop = true) {
  66. new arg = 0;
  67. new index = g_nargs;
  68. new bytes = g_nargs * 4;
  69. new retval;
  70. while (--index >= 0) {
  71. arg = g_args[index];
  72. #emit push.s arg
  73. }
  74. #emit load.s.pri bytes
  75. #emit push.pri
  76. #emit lctrl 6
  77. #emit add.c 0x1c
  78. #emit push.pri
  79. #emit load.s.pri address
  80. #emit sctrl 6
  81. #emit stor.s.pri retval
  82. if (auto_pop) {
  83. while (Pop()) {}
  84. }
  85. return retval;
  86. }
  87. stock CallN(address, args_to_push, bool:auto_pop = true) {
  88. // Like "Call", but doesn't pass all parameters.
  89. new arg = 0;
  90. new index = g_nargs;
  91. new bytes = args_to_push * 4;
  92. new end = g_nargs - args_to_push;
  93. new retval;
  94. if (end < 0) {
  95. return cellmin;
  96. }
  97. while (--index >= end) {
  98. arg = g_args[index];
  99. #emit push.s arg
  100. }
  101. #emit load.s.pri bytes
  102. #emit push.pri
  103. #emit lctrl 6
  104. #emit add.c 0x1c
  105. #emit push.pri
  106. #emit load.s.pri address
  107. #emit sctrl 6
  108. #emit stor.s.pri retval
  109. if (auto_pop) {
  110. while (args_to_push--) {
  111. Pop();
  112. }
  113. }
  114. return retval;
  115. }
  116. stock CallFunction(address, {Float,_}:...) {
  117. new arg_bytes, arg_begin, arg_end;
  118. // Get number of bytes passed.
  119. #emit load.s.pri 0x8
  120. #emit const.alt 4
  121. #emit sub
  122. #emit stor.s.pri arg_bytes
  123. #emit move.alt
  124. // Last argument is at FRM + 0x0C + arg_bytes (which is in ALT).
  125. #emit lctrl 5
  126. #emit add.c 0xc
  127. #emit add
  128. #emit stor.s.pri arg_end
  129. // Frist argument is at FRM + 0x10.
  130. #emit lctrl 5
  131. #emit add.c 0x10
  132. #emit stor.s.pri arg_begin
  133. new arg = arg_end;
  134. while (arg >= arg_begin) {
  135. #emit lref.s.pri arg
  136. #emit load.i
  137. #emit push.pri
  138. arg -= 4;
  139. }
  140. // Call the function
  141. #emit push.s arg_bytes
  142. #emit lctrl 6
  143. #emit add.c 0x1c
  144. #emit push.pri
  145. #emit load.s.pri address
  146. #emit sctrl 6
  147. // Arguments are popped by callee.
  148. // Pop locals and return.
  149. #emit stack 0x10
  150. #emit retn
  151. return 0; // make compiler happy
  152. }
  153. stock SysreqC(index, bool:auto_pop = true) {
  154. new arg = 0;
  155. new i = g_nargs;
  156. new bytes = g_nargs * 4;
  157. new tmp;
  158. new Opcode:sysreq_c = RelocateOpcode(OP_SYSREQ_C);
  159. new retval;
  160. while (--i >= 0) {
  161. arg = g_args[i];
  162. #emit push.s arg
  163. }
  164. #emit load.s.pri bytes
  165. #emit push.pri
  166. // tmp = cod + cip - dat + <distance to SYSREQ.C's operand>
  167. #emit lctrl 0 // COD
  168. #emit move.alt
  169. #emit lctrl 6 // CIP
  170. #emit add
  171. #emit move.alt
  172. #emit lctrl 1 // DAT
  173. #emit sub.alt
  174. #emit add.c 0x5c
  175. #emit stor.s.pri tmp
  176. // nop #1 = sysreq.c
  177. #emit load.s.pri sysreq_c
  178. #emit sref.s.pri tmp
  179. // tmp += 4
  180. #emit load.s.pri tmp
  181. #emit add.c 4
  182. #emit stor.s.pri tmp
  183. // nop #2 = index
  184. #emit load.s.pri index
  185. #emit sref.s.pri tmp
  186. #emit nop
  187. #emit nop
  188. #emit stor.s.pri retval
  189. // Pop native arguments.
  190. #emit lctrl 4
  191. #emit load.s.alt bytes
  192. #emit add
  193. #emit add.c 4
  194. #emit sctrl 4
  195. if (auto_pop) {
  196. while (Pop()) {}
  197. }
  198. return retval;
  199. }
  200. stock SysreqD(address, bool:auto_pop = true) {
  201. new arg = 0;
  202. new i = g_nargs;
  203. new bytes = g_nargs * 4;
  204. new tmp;
  205. new Opcode:sysreq_d = RelocateOpcode(OP_SYSREQ_D);
  206. new retval;
  207. while (--i >= 0) {
  208. arg = g_args[i];
  209. #emit push.s arg
  210. }
  211. #emit load.s.pri bytes
  212. #emit push.pri
  213. // tmp = cod + cip - dat + <distance to nop #1>
  214. #emit lctrl 0 // COD
  215. #emit move.alt
  216. #emit lctrl 6 // CIP
  217. #emit add
  218. #emit move.alt
  219. #emit lctrl 1 // DAT
  220. #emit sub.alt
  221. #emit add.c 0x5c
  222. #emit stor.s.pri tmp
  223. // nop #1 = sysreq.d
  224. #emit load.s.pri sysreq_d
  225. #emit sref.s.pri tmp
  226. // tmp += 4
  227. #emit load.s.pri tmp
  228. #emit add.c 4
  229. #emit stor.s.pri tmp
  230. // nop #2 = address
  231. #emit load.s.pri address
  232. #emit sref.s.pri tmp
  233. #emit nop
  234. #emit nop
  235. #emit stor.s.pri retval
  236. // Pop native arguments.
  237. #emit lctrl 4
  238. #emit load.s.alt bytes
  239. #emit add
  240. #emit add.c 4
  241. #emit sctrl 4
  242. if (auto_pop) {
  243. while (Pop()) {}
  244. }
  245. return retval;
  246. }
  247. stock SysreqCN(index, args_to_push, bool:auto_pop = true) {
  248. new arg = 0;
  249. new i = g_nargs;
  250. new bytes = args_to_push * 4;
  251. new tmp;
  252. new Opcode:sysreq_c = RelocateOpcode(OP_SYSREQ_C);
  253. new end = g_nargs - args_to_push;
  254. new retval;
  255. if (end < 0) {
  256. return cellmin;
  257. }
  258. while (--i >= end) {
  259. arg = g_args[i];
  260. #emit push.s arg
  261. }
  262. #emit load.s.pri bytes
  263. #emit push.pri
  264. // tmp = cod + cip - dat + <distance to SYSREQ.C's operand>
  265. #emit lctrl 0 // COD
  266. #emit move.alt
  267. #emit lctrl 6 // CIP
  268. #emit add
  269. #emit move.alt
  270. #emit lctrl 1 // DAT
  271. #emit sub.alt
  272. #emit add.c 0x5c
  273. #emit stor.s.pri tmp
  274. // nop #1 = sysreq.c
  275. #emit load.s.pri sysreq_c
  276. #emit sref.s.pri tmp
  277. // tmp += 4
  278. #emit load.s.pri tmp
  279. #emit add.c 4
  280. #emit stor.s.pri tmp
  281. // nop #2 = index
  282. #emit load.s.pri index
  283. #emit sref.s.pri tmp
  284. #emit nop
  285. #emit nop
  286. #emit stor.s.pri retval
  287. // Pop native arguments.
  288. #emit lctrl 4
  289. #emit load.s.alt bytes
  290. #emit add
  291. #emit add.c 4
  292. #emit sctrl 4
  293. if (auto_pop) {
  294. while (args_to_push--) {
  295. Pop();
  296. }
  297. }
  298. return retval;
  299. }
  300. stock SysreqDN(address, args_to_push, bool:auto_pop = true) {
  301. new arg = 0;
  302. new i = g_nargs;
  303. new bytes = args_to_push * 4;
  304. new tmp;
  305. new Opcode:sysreq_d = RelocateOpcode(OP_SYSREQ_D);
  306. new end = g_nargs - args_to_push;
  307. new retval;
  308. if (end < 0) {
  309. return cellmin;
  310. }
  311. while (--i >= end) {
  312. arg = g_args[i];
  313. #emit push.s arg
  314. }
  315. #emit load.s.pri bytes
  316. #emit push.pri
  317. // tmp = cod + cip - dat + <distance to nop #1>
  318. #emit lctrl 0 // COD
  319. #emit move.alt
  320. #emit lctrl 6 // CIP
  321. #emit add
  322. #emit move.alt
  323. #emit lctrl 1 // DAT
  324. #emit sub.alt
  325. #emit add.c 0x5c
  326. #emit stor.s.pri tmp
  327. // nop #1 = sysreq.d
  328. #emit load.s.pri sysreq_d
  329. #emit sref.s.pri tmp
  330. // tmp += 4
  331. #emit load.s.pri tmp
  332. #emit add.c 4
  333. #emit stor.s.pri tmp
  334. // nop #2 = address
  335. #emit load.s.pri address
  336. #emit sref.s.pri tmp
  337. #emit nop
  338. #emit nop
  339. #emit stor.s.pri retval
  340. // Pop native arguments.
  341. #emit lctrl 4
  342. #emit load.s.alt bytes
  343. #emit add
  344. #emit add.c 4
  345. #emit sctrl 4
  346. if (auto_pop) {
  347. while (args_to_push--) {
  348. Pop();
  349. }
  350. }
  351. return retval;
  352. }
  353. stock CallNative(index, {Float,_}:...) {
  354. new arg_bytes, arg_begin, arg_end;
  355. new Opcode:sysreq_c = RelocateOpcode(OP_SYSREQ_C);
  356. // Get number of bytes passed.
  357. #emit load.s.pri 0x8
  358. #emit const.alt 4
  359. #emit sub
  360. #emit stor.s.pri arg_bytes
  361. #emit move.alt
  362. // Last argument is at FRM + 0x0C + arg_bytes (which is in ALT).
  363. #emit lctrl 5
  364. #emit add.c 0xc
  365. #emit add
  366. #emit stor.s.pri arg_end
  367. // Frist argument is at FRM + 0x10.
  368. #emit lctrl 5
  369. #emit add.c 0x10
  370. #emit stor.s.pri arg_begin
  371. new arg = arg_end;
  372. new tmp;
  373. while (arg >= arg_begin) {
  374. #emit lref.s.pri arg
  375. #emit load.i
  376. #emit push.pri
  377. arg -= 4;
  378. }
  379. // Push number of arguments * 4 (which is params[0]).
  380. #emit push.s arg_bytes
  381. // tmp = cod + cip - dat + <distance to nop #1>
  382. #emit lctrl 0 // COD
  383. #emit move.alt
  384. #emit lctrl 6 // CIP
  385. #emit add
  386. #emit move.alt
  387. #emit lctrl 1 // DAT
  388. #emit sub.alt
  389. #emit add.c 0x5c
  390. #emit stor.s.pri tmp
  391. // nop #1 = sysreq.c
  392. #emit load.s.pri sysreq_c
  393. #emit sref.s.pri tmp
  394. // tmp += 4
  395. #emit load.s.pri tmp
  396. #emit add.c 4
  397. #emit stor.s.pri tmp
  398. // nop #2 = index
  399. #emit load.s.pri index
  400. #emit sref.s.pri tmp
  401. #emit nop
  402. #emit nop
  403. new retval;
  404. #emit stor.s.pri retval
  405. // Pop native arguments.
  406. #emit lctrl 4
  407. #emit load.s.alt arg_bytes
  408. #emit add
  409. #emit add.c 4
  410. #emit sctrl 4
  411. return retval;
  412. }
  413. // Unlike CallNative(), this function calls natives directly via SYSREQ.D.
  414. stock CallNativeByAddress(address, {Float,_}:...) {
  415. new arg_bytes, arg_begin, arg_end;
  416. new Opcode:sysreq_d = RelocateOpcode(OP_SYSREQ_D);
  417. // Get number of bytes passed.
  418. #emit load.s.pri 0x8
  419. #emit const.alt 4
  420. #emit sub
  421. #emit stor.s.pri arg_bytes
  422. #emit move.alt
  423. // Last argument is at FRM + 0x0C + arg_bytes (which is in ALT).
  424. #emit lctrl 5
  425. #emit add.c 0xc
  426. #emit add
  427. #emit stor.s.pri arg_end
  428. // Frist argument is at FRM + 0x10.
  429. #emit lctrl 5
  430. #emit add.c 0x10
  431. #emit stor.s.pri arg_begin
  432. new arg = arg_end;
  433. new tmp;
  434. while (arg >= arg_begin) {
  435. #emit lref.s.pri arg
  436. #emit load.i
  437. #emit push.pri
  438. arg -= 4;
  439. }
  440. // Push number of arguments * 4 (which is params[0]).
  441. #emit push.s arg_bytes
  442. // tmp = cod + cip - dat + <distance to nop #1>
  443. #emit lctrl 0 // COD
  444. #emit move.alt
  445. #emit lctrl 6 // CIP
  446. #emit add
  447. #emit move.alt
  448. #emit lctrl 1 // DAT
  449. #emit sub.alt
  450. #emit add.c 0x5c
  451. #emit stor.s.pri tmp
  452. // nop #1 = sysreq.d
  453. #emit load.s.pri sysreq_d
  454. #emit sref.s.pri tmp
  455. // tmp += 4
  456. #emit load.s.pri tmp
  457. #emit add.c 4
  458. #emit stor.s.pri tmp
  459. // nop #2 = address
  460. #emit load.s.pri address
  461. #emit sref.s.pri tmp
  462. #emit nop
  463. #emit nop
  464. new retval;
  465. #emit stor.s.pri retval
  466. // Pop native arguments.
  467. #emit lctrl 4
  468. #emit load.s.alt arg_bytes
  469. #emit add
  470. #emit add.c 4
  471. #emit sctrl 4
  472. return retval;
  473. }