dynamic_call.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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 "amx_jit"
  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 0x24
  78. #emit lctrl 8
  79. #emit push.pri
  80. #emit load.s.pri address
  81. #emit sctrl 6
  82. #emit stor.s.pri retval
  83. if (auto_pop) {
  84. g_nargs = 0;
  85. }
  86. return retval;
  87. }
  88. stock CallN(address, args_to_push, bool:auto_pop = true) {
  89. // Like "Call", but doesn't pass all parameters.
  90. new arg = 0;
  91. new index = g_nargs;
  92. new bytes = args_to_push * 4;
  93. new end = g_nargs - args_to_push;
  94. new retval;
  95. if (end < 0) {
  96. return cellmin;
  97. }
  98. while (--index >= end) {
  99. arg = g_args[index];
  100. #emit push.s arg
  101. }
  102. #emit load.s.pri bytes
  103. #emit push.pri
  104. #emit lctrl 6
  105. #emit add.c 0x24
  106. #emit lctrl 8
  107. #emit push.pri
  108. #emit load.s.pri address
  109. #emit sctrl 6
  110. #emit stor.s.pri retval
  111. if (auto_pop) {
  112. g_nargs = end;
  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 0x24
  144. #emit lctrl 8
  145. #emit push.pri
  146. #emit load.s.pri address
  147. #emit sctrl 6
  148. // Arguments are popped by callee.
  149. // Pop locals and return.
  150. #emit stack 0x10
  151. #emit retn
  152. return 0; // make compiler happy
  153. }
  154. stock SysreqC(index, bool:auto_pop = true) {
  155. new arg = 0;
  156. new i = g_nargs;
  157. new bytes = g_nargs * 4;
  158. new tmp;
  159. new Opcode:sysreq_c = RelocateOpcode(OP_SYSREQ_C);
  160. new retval;
  161. if (GetJITGeneratorVersion()) {
  162. return cellmin;
  163. }
  164. while (--i >= 0) {
  165. arg = g_args[i];
  166. #emit push.s arg
  167. }
  168. #emit load.s.pri bytes
  169. #emit push.pri
  170. // tmp = cod + cip - dat + <distance to SYSREQ.C's operand>
  171. #emit lctrl 0 // COD
  172. #emit move.alt
  173. #emit lctrl 6 // CIP
  174. #emit add
  175. #emit move.alt
  176. #emit lctrl 1 // DAT
  177. #emit sub.alt
  178. #emit add.c 0x5c
  179. #emit stor.s.pri tmp
  180. // nop #1 = sysreq.c
  181. #emit load.s.pri sysreq_c
  182. #emit sref.s.pri tmp
  183. // tmp += 4
  184. #emit load.s.pri tmp
  185. #emit add.c 4
  186. #emit stor.s.pri tmp
  187. // nop #2 = index
  188. #emit load.s.pri index
  189. #emit sref.s.pri tmp
  190. #emit nop
  191. #emit nop
  192. // #emit sysreq.c 0
  193. #emit stor.s.pri retval
  194. // Pop native arguments.
  195. #emit lctrl 4
  196. #emit load.s.alt bytes
  197. #emit add
  198. #emit add.c 4
  199. #emit sctrl 4
  200. if (auto_pop) {
  201. g_nargs = 0;
  202. }
  203. return retval;
  204. }
  205. stock SysreqD(address, bool:auto_pop = true) {
  206. new arg = 0;
  207. new i = g_nargs;
  208. new bytes = g_nargs * 4;
  209. new tmp;
  210. new Opcode:sysreq_d = RelocateOpcode(OP_SYSREQ_D);
  211. new retval;
  212. if (GetJITGeneratorVersion()) {
  213. return cellmin;
  214. }
  215. while (--i >= 0) {
  216. arg = g_args[i];
  217. #emit push.s arg
  218. }
  219. #emit load.s.pri bytes
  220. #emit push.pri
  221. // tmp = cod + cip - dat + <distance to nop #1>
  222. #emit lctrl 0 // COD
  223. #emit move.alt
  224. #emit lctrl 6 // CIP
  225. #emit add
  226. #emit move.alt
  227. #emit lctrl 1 // DAT
  228. #emit sub.alt
  229. #emit add.c 0x5c
  230. #emit stor.s.pri tmp
  231. // nop #1 = sysreq.d
  232. #emit load.s.pri sysreq_d
  233. #emit sref.s.pri tmp
  234. // tmp += 4
  235. #emit load.s.pri tmp
  236. #emit add.c 4
  237. #emit stor.s.pri tmp
  238. // nop #2 = address
  239. #emit load.s.pri address
  240. #emit sref.s.pri tmp
  241. #emit nop
  242. #emit nop
  243. #emit stor.s.pri retval
  244. // Pop native arguments.
  245. #emit lctrl 4
  246. #emit load.s.alt bytes
  247. #emit add
  248. #emit add.c 4
  249. #emit sctrl 4
  250. if (auto_pop) {
  251. g_nargs = 0;
  252. }
  253. return retval;
  254. }
  255. stock SysreqCN(index, args_to_push, bool:auto_pop = true) {
  256. new arg = 0;
  257. new i = g_nargs;
  258. new bytes = args_to_push * 4;
  259. new tmp;
  260. new Opcode:sysreq_c = RelocateOpcode(OP_SYSREQ_C);
  261. new end = g_nargs - args_to_push;
  262. new retval;
  263. if (GetJITGeneratorVersion()) {
  264. return cellmin;
  265. }
  266. if (end < 0) {
  267. return cellmin;
  268. }
  269. while (--i >= end) {
  270. arg = g_args[i];
  271. #emit push.s arg
  272. }
  273. #emit load.s.pri bytes
  274. #emit push.pri
  275. // tmp = cod + cip - dat + <distance to SYSREQ.C's operand>
  276. #emit lctrl 0 // COD
  277. #emit move.alt
  278. #emit lctrl 6 // CIP
  279. #emit add
  280. #emit move.alt
  281. #emit lctrl 1 // DAT
  282. #emit sub.alt
  283. #emit add.c 0x5c
  284. #emit stor.s.pri tmp
  285. // nop #1 = sysreq.c
  286. #emit load.s.pri sysreq_c
  287. #emit sref.s.pri tmp
  288. // tmp += 4
  289. #emit load.s.pri tmp
  290. #emit add.c 4
  291. #emit stor.s.pri tmp
  292. // nop #2 = index
  293. #emit load.s.pri index
  294. #emit sref.s.pri tmp
  295. #emit nop
  296. #emit nop
  297. #emit stor.s.pri retval
  298. // Pop native arguments.
  299. #emit lctrl 4
  300. #emit load.s.alt bytes
  301. #emit add
  302. #emit add.c 4
  303. #emit sctrl 4
  304. if (auto_pop) {
  305. g_nargs = end;
  306. }
  307. return retval;
  308. }
  309. stock SysreqDN(address, args_to_push, bool:auto_pop = true) {
  310. new arg = 0;
  311. new i = g_nargs;
  312. new bytes = args_to_push * 4;
  313. new tmp;
  314. new Opcode:sysreq_d = RelocateOpcode(OP_SYSREQ_D);
  315. new end = g_nargs - args_to_push;
  316. new retval;
  317. if (GetJITGeneratorVersion()) {
  318. return cellmin;
  319. }
  320. if (end < 0) {
  321. return cellmin;
  322. }
  323. while (--i >= end) {
  324. arg = g_args[i];
  325. #emit push.s arg
  326. }
  327. #emit load.s.pri bytes
  328. #emit push.pri
  329. // tmp = cod + cip - dat + <distance to nop #1>
  330. #emit lctrl 0 // COD
  331. #emit move.alt
  332. #emit lctrl 6 // CIP
  333. #emit add
  334. #emit move.alt
  335. #emit lctrl 1 // DAT
  336. #emit sub.alt
  337. #emit add.c 0x5c
  338. #emit stor.s.pri tmp
  339. // nop #1 = sysreq.d
  340. #emit load.s.pri sysreq_d
  341. #emit sref.s.pri tmp
  342. // tmp += 4
  343. #emit load.s.pri tmp
  344. #emit add.c 4
  345. #emit stor.s.pri tmp
  346. // nop #2 = address
  347. #emit load.s.pri address
  348. #emit sref.s.pri tmp
  349. #emit nop
  350. #emit nop
  351. #emit stor.s.pri retval
  352. // Pop native arguments.
  353. #emit lctrl 4
  354. #emit load.s.alt bytes
  355. #emit add
  356. #emit add.c 4
  357. #emit sctrl 4
  358. if (auto_pop) {
  359. g_nargs = end;
  360. }
  361. return retval;
  362. }
  363. stock CallNative(index, {Float,_}:...) {
  364. new arg_bytes, arg_begin, arg_end;
  365. new Opcode:sysreq_c = RelocateOpcode(OP_SYSREQ_C);
  366. if (GetJITGeneratorVersion()) {
  367. return cellmin;
  368. }
  369. // Get number of bytes passed.
  370. #emit load.s.pri 0x8
  371. #emit const.alt 4
  372. #emit sub
  373. #emit stor.s.pri arg_bytes
  374. #emit move.alt
  375. // Last argument is at FRM + 0x0C + arg_bytes (which is in ALT).
  376. #emit lctrl 5
  377. #emit add.c 0xc
  378. #emit add
  379. #emit stor.s.pri arg_end
  380. // Frist argument is at FRM + 0x10.
  381. #emit lctrl 5
  382. #emit add.c 0x10
  383. #emit stor.s.pri arg_begin
  384. new arg = arg_end;
  385. new tmp;
  386. while (arg >= arg_begin) {
  387. #emit lref.s.pri arg
  388. #emit load.i
  389. #emit push.pri
  390. arg -= 4;
  391. }
  392. // Push number of arguments * 4 (which is params[0]).
  393. #emit push.s arg_bytes
  394. // tmp = cod + cip - dat + <distance to nop #1>
  395. #emit lctrl 0 // COD
  396. #emit move.alt
  397. #emit lctrl 6 // CIP
  398. #emit add
  399. #emit move.alt
  400. #emit lctrl 1 // DAT
  401. #emit sub.alt
  402. #emit add.c 0x5c
  403. #emit stor.s.pri tmp
  404. // nop #1 = sysreq.c
  405. #emit load.s.pri sysreq_c
  406. #emit sref.s.pri tmp
  407. // tmp += 4
  408. #emit load.s.pri tmp
  409. #emit add.c 4
  410. #emit stor.s.pri tmp
  411. // nop #2 = index
  412. #emit load.s.pri index
  413. #emit sref.s.pri tmp
  414. #emit nop
  415. #emit nop
  416. new retval;
  417. #emit stor.s.pri retval
  418. // Pop native arguments.
  419. #emit lctrl 4
  420. #emit load.s.alt arg_bytes
  421. #emit add
  422. #emit add.c 4
  423. #emit sctrl 4
  424. return retval;
  425. }
  426. // Unlike CallNative(), this function calls natives directly via SYSREQ.D.
  427. stock CallNativeByAddress(address, {Float,_}:...) {
  428. new arg_bytes, arg_begin, arg_end;
  429. new Opcode:sysreq_d = RelocateOpcode(OP_SYSREQ_D);
  430. if (GetJITGeneratorVersion()) {
  431. return cellmin;
  432. }
  433. // Get number of bytes passed.
  434. #emit load.s.pri 0x8
  435. #emit const.alt 4
  436. #emit sub
  437. #emit stor.s.pri arg_bytes
  438. #emit move.alt
  439. // Last argument is at FRM + 0x0C + arg_bytes (which is in ALT).
  440. #emit lctrl 5
  441. #emit add.c 0xc
  442. #emit add
  443. #emit stor.s.pri arg_end
  444. // Frist argument is at FRM + 0x10.
  445. #emit lctrl 5
  446. #emit add.c 0x10
  447. #emit stor.s.pri arg_begin
  448. new arg = arg_end;
  449. new tmp;
  450. while (arg >= arg_begin) {
  451. #emit lref.s.pri arg
  452. #emit load.i
  453. #emit push.pri
  454. arg -= 4;
  455. }
  456. // Push number of arguments * 4 (which is params[0]).
  457. #emit push.s arg_bytes
  458. // tmp = cod + cip - dat + <distance to nop #1>
  459. #emit lctrl 0 // COD
  460. #emit move.alt
  461. #emit lctrl 6 // CIP
  462. #emit add
  463. #emit move.alt
  464. #emit lctrl 1 // DAT
  465. #emit sub.alt
  466. #emit add.c 0x5c
  467. #emit stor.s.pri tmp
  468. // nop #1 = sysreq.d
  469. #emit load.s.pri sysreq_d
  470. #emit sref.s.pri tmp
  471. // tmp += 4
  472. #emit load.s.pri tmp
  473. #emit add.c 4
  474. #emit stor.s.pri tmp
  475. // nop #2 = address
  476. #emit load.s.pri address
  477. #emit sref.s.pri tmp
  478. #emit nop
  479. #emit nop
  480. new retval;
  481. #emit stor.s.pri retval
  482. // Pop native arguments.
  483. #emit lctrl 4
  484. #emit load.s.alt arg_bytes
  485. #emit add
  486. #emit add.c 4
  487. #emit sctrl 4
  488. return retval;
  489. }