opcode.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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 OPCODE_INC
  21. #endinput
  22. #endif
  23. #define OPCODE_INC
  24. enum Opcode {
  25. OP_NONE, OP_LOAD_PRI, OP_LOAD_ALT,
  26. OP_LOAD_S_PRI, OP_LOAD_S_ALT, OP_LREF_PRI,
  27. OP_LREF_ALT, OP_LREF_S_PRI, OP_LREF_S_ALT,
  28. OP_LOAD_I, OP_LODB_I, OP_CONST_PRI,
  29. OP_CONST_ALT, OP_ADDR_PRI, OP_ADDR_ALT,
  30. OP_STOR_PRI, OP_STOR_ALT, OP_STOR_S_PRI,
  31. OP_STOR_S_ALT, OP_SREF_PRI, OP_SREF_ALT,
  32. OP_SREF_S_PRI, OP_SREF_S_ALT, OP_STOR_I,
  33. OP_STRB_I, OP_LIDX, OP_LIDX_B,
  34. OP_IDXADDR, OP_IDXADDR_B, OP_ALIGN_PRI,
  35. OP_ALIGN_ALT, OP_LCTRL, OP_SCTRL,
  36. OP_MOVE_PRI, OP_MOVE_ALT, OP_XCHG,
  37. OP_PUSH_PRI, OP_PUSH_ALT, OP_PUSH_R,
  38. OP_PUSH_C, OP_PUSH, OP_PUSH_S,
  39. OP_POP_PRI, OP_POP_ALT, OP_STACK,
  40. OP_HEAP, OP_PROC, OP_RET,
  41. OP_RETN, OP_CALL, OP_CALL_PRI,
  42. OP_JUMP, OP_JREL, OP_JZER,
  43. OP_JNZ, OP_JEQ, OP_JNEQ,
  44. OP_JLESS, OP_JLEQ, OP_JGRTR,
  45. OP_JGEQ, OP_JSLESS, OP_JSLEQ,
  46. OP_JSGRTR, OP_JSGEQ, OP_SHL,
  47. OP_SHR, OP_SSHR, OP_SHL_C_PRI,
  48. OP_SHL_C_ALT, OP_SHR_C_PRI, OP_SHR_C_ALT,
  49. OP_SMUL, OP_SDIV, OP_SDIV_ALT,
  50. OP_UMUL, OP_UDIV, OP_UDIV_ALT,
  51. OP_ADD, OP_SUB, OP_SUB_ALT,
  52. OP_AND, OP_OR, OP_XOR,
  53. OP_NOT, OP_NEG, OP_INVERT,
  54. OP_ADD_C, OP_SMUL_C, OP_ZERO_PRI,
  55. OP_ZERO_ALT, OP_ZERO, OP_ZERO_S,
  56. OP_SIGN_PRI, OP_SIGN_ALT, OP_EQ,
  57. OP_NEQ, OP_LESS, OP_LEQ,
  58. OP_GRTR, OP_GEQ, OP_SLESS,
  59. OP_SLEQ, OP_SGRTR, OP_SGEQ,
  60. OP_EQ_C_PRI, OP_EQ_C_ALT, OP_INC_PRI,
  61. OP_INC_ALT, OP_INC, OP_INC_S,
  62. OP_INC_I, OP_DEC_PRI, OP_DEC_ALT,
  63. OP_DEC, OP_DEC_S, OP_DEC_I,
  64. OP_MOVS, OP_CMPS, OP_FILL,
  65. OP_HALT, OP_BOUNDS, OP_SYSREQ_PRI,
  66. OP_SYSREQ_C, OP_FILE, OP_LINE,
  67. OP_SYMBOL, OP_SRANGE, OP_JUMP_PRI,
  68. OP_SWITCH, OP_CASETBL, OP_SWAP_PRI,
  69. OP_SWAP_ALT, OP_PUSH_ADR, OP_NOP,
  70. OP_SYSREQ_D, OP_SYMTAG, OP_BREAK,
  71. OP_LAST_
  72. };
  73. const NUM_OPCODES = _:OP_LAST_;
  74. static stock Opcode:ReadOpcodeNearThis(offset = 0) {
  75. new ret_addr;
  76. // Get return address + COD - DAT + offset.
  77. #emit load.s.alt 4
  78. #emit lctrl 0
  79. #emit add
  80. #emit move.alt
  81. #emit lctrl 1
  82. #emit xchg
  83. #emit sub
  84. #emit load.s.alt offset
  85. #emit add
  86. #emit stor.s.pri ret_addr
  87. #emit lref.s.pri ret_addr
  88. #emit stack 4
  89. #emit retn
  90. return OP_NONE; // make compiler happy
  91. }
  92. static stock bool:HaveToRelocateOpcodes() {
  93. return ReadOpcodeNearThis(-8) != OP_CALL;
  94. }
  95. // Based on this idea: http://forum.sa-mp.com/showthread.php?t=358084
  96. stock Opcode:RelocateOpcodeNow(Opcode:opcode) {
  97. if (!HaveToRelocateOpcodes()) {
  98. return opcode;
  99. }
  100. switch (opcode) {
  101. case OP_LOAD_PRI: {
  102. return ReadOpcodeNearThis(4);
  103. #emit load.pri 0
  104. }
  105. case OP_LOAD_ALT: {
  106. return ReadOpcodeNearThis(4);
  107. #emit load.alt 0
  108. }
  109. case OP_LOAD_S_PRI: {
  110. return ReadOpcodeNearThis(4);
  111. #emit load.s.pri 0
  112. }
  113. case OP_LOAD_S_ALT: {
  114. return ReadOpcodeNearThis(4);
  115. #emit load.s.alt 0
  116. }
  117. case OP_LREF_PRI: {
  118. return ReadOpcodeNearThis(4);
  119. #emit lref.pri 0
  120. }
  121. case OP_LREF_ALT: {
  122. return ReadOpcodeNearThis(4);
  123. #emit lref.alt 0
  124. }
  125. case OP_LREF_S_PRI: {
  126. return ReadOpcodeNearThis(4);
  127. #emit lref.s.pri 0
  128. }
  129. case OP_LREF_S_ALT: {
  130. return ReadOpcodeNearThis(4);
  131. #emit lref.s.alt 0
  132. }
  133. case OP_LOAD_I: {
  134. return ReadOpcodeNearThis(4);
  135. #emit load.i
  136. }
  137. case OP_LODB_I: {
  138. return ReadOpcodeNearThis(4);
  139. #emit lodb.i 1
  140. }
  141. case OP_CONST_PRI: {
  142. return ReadOpcodeNearThis(4);
  143. #emit const.pri 0
  144. }
  145. case OP_CONST_ALT: {
  146. return ReadOpcodeNearThis(4);
  147. #emit const.alt 0
  148. }
  149. case OP_ADDR_PRI: {
  150. return ReadOpcodeNearThis(4);
  151. #emit addr.pri 0
  152. }
  153. case OP_ADDR_ALT: {
  154. return ReadOpcodeNearThis(4);
  155. #emit addr.alt 0
  156. }
  157. case OP_STOR_PRI: {
  158. return ReadOpcodeNearThis(4);
  159. #emit stor.pri 0
  160. }
  161. case OP_STOR_ALT: {
  162. return ReadOpcodeNearThis(4);
  163. #emit stor.alt 0
  164. }
  165. case OP_STOR_S_PRI: {
  166. return ReadOpcodeNearThis(4);
  167. #emit stor.s.pri 0
  168. }
  169. case OP_STOR_S_ALT: {
  170. return ReadOpcodeNearThis(4);
  171. #emit stor.s.alt 0
  172. }
  173. case OP_SREF_PRI: {
  174. return ReadOpcodeNearThis(4);
  175. #emit sref.pri 0
  176. }
  177. case OP_SREF_ALT: {
  178. return ReadOpcodeNearThis(4);
  179. #emit sref.alt 0
  180. }
  181. case OP_SREF_S_PRI: {
  182. return ReadOpcodeNearThis(4);
  183. #emit sref.s.pri 0
  184. }
  185. case OP_SREF_S_ALT: {
  186. return ReadOpcodeNearThis(4);
  187. #emit sref.s.alt 0
  188. }
  189. case OP_STOR_I: {
  190. return ReadOpcodeNearThis(4);
  191. #emit stor.i
  192. }
  193. case OP_STRB_I: {
  194. return ReadOpcodeNearThis(4);
  195. #emit strb.i 1
  196. }
  197. case OP_LIDX: {
  198. return ReadOpcodeNearThis(4);
  199. #emit lidx
  200. }
  201. case OP_LIDX_B: {
  202. return ReadOpcodeNearThis(4);
  203. #emit lidx.b 0
  204. }
  205. case OP_IDXADDR: {
  206. return ReadOpcodeNearThis(4);
  207. #emit idxaddr
  208. }
  209. case OP_IDXADDR_B: {
  210. return ReadOpcodeNearThis(4);
  211. #emit idxaddr.b 0
  212. }
  213. case OP_ALIGN_PRI: {
  214. return ReadOpcodeNearThis(4);
  215. #emit align.pri 0
  216. }
  217. case OP_ALIGN_ALT: {
  218. return ReadOpcodeNearThis(4);
  219. #emit align.alt 0
  220. }
  221. case OP_LCTRL: {
  222. return ReadOpcodeNearThis(4);
  223. #emit lctrl 0
  224. }
  225. case OP_SCTRL: {
  226. return ReadOpcodeNearThis(4);
  227. #emit sctrl 0
  228. }
  229. case OP_MOVE_PRI: {
  230. return ReadOpcodeNearThis(4);
  231. #emit move.pri
  232. }
  233. case OP_MOVE_ALT: {
  234. return ReadOpcodeNearThis(4);
  235. #emit move.alt
  236. }
  237. case OP_XCHG: {
  238. return ReadOpcodeNearThis(4);
  239. #emit xchg
  240. }
  241. case OP_PUSH_PRI: {
  242. return ReadOpcodeNearThis(4);
  243. #emit push.pri
  244. }
  245. case OP_PUSH_ALT: {
  246. return ReadOpcodeNearThis(4);
  247. #emit push.alt
  248. }
  249. case OP_PUSH_C: {
  250. return ReadOpcodeNearThis(4);
  251. #emit push.c 0
  252. }
  253. case OP_PUSH: {
  254. return ReadOpcodeNearThis(4);
  255. #emit push 0
  256. }
  257. case OP_PUSH_S: {
  258. return ReadOpcodeNearThis(4);
  259. #emit push.s 0
  260. }
  261. case OP_POP_PRI: {
  262. return ReadOpcodeNearThis(4);
  263. #emit pop.pri
  264. }
  265. case OP_POP_ALT: {
  266. return ReadOpcodeNearThis(4);
  267. #emit pop.alt
  268. }
  269. case OP_STACK: {
  270. return ReadOpcodeNearThis(4);
  271. #emit stack 0
  272. }
  273. case OP_HEAP: {
  274. return ReadOpcodeNearThis(4);
  275. #emit heap 0
  276. }
  277. case OP_PROC: {
  278. return ReadOpcodeNearThis(4);
  279. #emit proc
  280. }
  281. case OP_RET: {
  282. return ReadOpcodeNearThis(4);
  283. #emit ret
  284. }
  285. case OP_RETN: {
  286. return ReadOpcodeNearThis(4);
  287. #emit retn
  288. }
  289. case OP_CALL: {
  290. // We can't do just "#emit call 0" - this will crash compiler for some reason (bug?).
  291. return ReadOpcodeNearThis(-8);
  292. }
  293. case OP_JUMP: {
  294. return ReadOpcodeNearThis(4);
  295. #emit jump 0
  296. }
  297. case OP_JZER: {
  298. return ReadOpcodeNearThis(4);
  299. #emit jzer 0
  300. }
  301. case OP_JNZ: {
  302. return ReadOpcodeNearThis(4);
  303. #emit jnz 0
  304. }
  305. case OP_JEQ: {
  306. return ReadOpcodeNearThis(4);
  307. #emit jeq 0
  308. }
  309. case OP_JNEQ: {
  310. return ReadOpcodeNearThis(4);
  311. #emit jneq 0
  312. }
  313. case OP_JLESS: {
  314. return ReadOpcodeNearThis(4);
  315. #emit jless 0
  316. }
  317. case OP_JLEQ: {
  318. return ReadOpcodeNearThis(4);
  319. #emit jleq 0
  320. }
  321. case OP_JGRTR: {
  322. return ReadOpcodeNearThis(4);
  323. #emit jgrtr 0
  324. }
  325. case OP_JGEQ: {
  326. return ReadOpcodeNearThis(4);
  327. #emit jgeq 0
  328. }
  329. case OP_JSLESS: {
  330. return ReadOpcodeNearThis(4);
  331. #emit jsless 0
  332. }
  333. case OP_JSLEQ: {
  334. return ReadOpcodeNearThis(4);
  335. #emit jsleq 0
  336. }
  337. case OP_JSGRTR: {
  338. return ReadOpcodeNearThis(4);
  339. #emit jsgrtr 0
  340. }
  341. case OP_JSGEQ: {
  342. return ReadOpcodeNearThis(4);
  343. #emit jsgeq 0
  344. }
  345. case OP_SHL: {
  346. return ReadOpcodeNearThis(4);
  347. #emit shl
  348. }
  349. case OP_SHR: {
  350. return ReadOpcodeNearThis(4);
  351. #emit shr
  352. }
  353. case OP_SSHR: {
  354. return ReadOpcodeNearThis(4);
  355. #emit sshr
  356. }
  357. case OP_SHL_C_PRI: {
  358. return ReadOpcodeNearThis(4);
  359. #emit shl.c.pri 0
  360. }
  361. case OP_SHL_C_ALT: {
  362. return ReadOpcodeNearThis(4);
  363. #emit shl.c.alt 0
  364. }
  365. case OP_SHR_C_PRI: {
  366. return ReadOpcodeNearThis(4);
  367. #emit shr.c.pri 0
  368. }
  369. case OP_SHR_C_ALT: {
  370. return ReadOpcodeNearThis(4);
  371. #emit shr.c.alt 0
  372. }
  373. case OP_SMUL: {
  374. return ReadOpcodeNearThis(4);
  375. #emit smul
  376. }
  377. case OP_SDIV: {
  378. return ReadOpcodeNearThis(4);
  379. #emit sdiv
  380. }
  381. case OP_SDIV_ALT: {
  382. return ReadOpcodeNearThis(4);
  383. #emit sdiv.alt
  384. }
  385. case OP_UMUL: {
  386. return ReadOpcodeNearThis(4);
  387. #emit umul
  388. }
  389. case OP_UDIV: {
  390. return ReadOpcodeNearThis(4);
  391. #emit udiv
  392. }
  393. case OP_UDIV_ALT: {
  394. return ReadOpcodeNearThis(4);
  395. #emit udiv.alt
  396. }
  397. case OP_ADD: {
  398. return ReadOpcodeNearThis(4);
  399. #emit add
  400. }
  401. case OP_SUB: {
  402. return ReadOpcodeNearThis(4);
  403. #emit sub
  404. }
  405. case OP_SUB_ALT: {
  406. return ReadOpcodeNearThis(4);
  407. #emit sub.alt
  408. }
  409. case OP_AND: {
  410. return ReadOpcodeNearThis(4);
  411. #emit and
  412. }
  413. case OP_OR: {
  414. return ReadOpcodeNearThis(4);
  415. #emit or
  416. }
  417. case OP_XOR: {
  418. return ReadOpcodeNearThis(4);
  419. #emit xor
  420. }
  421. case OP_NOT: {
  422. return ReadOpcodeNearThis(4);
  423. #emit not
  424. }
  425. case OP_NEG: {
  426. return ReadOpcodeNearThis(4);
  427. #emit neg
  428. }
  429. case OP_INVERT: {
  430. return ReadOpcodeNearThis(4);
  431. #emit invert
  432. }
  433. case OP_ADD_C: {
  434. return ReadOpcodeNearThis(4);
  435. #emit add.c 0
  436. }
  437. case OP_SMUL_C: {
  438. return ReadOpcodeNearThis(4);
  439. #emit smul.c 0
  440. }
  441. case OP_ZERO_PRI: {
  442. return ReadOpcodeNearThis(4);
  443. #emit zero.pri
  444. }
  445. case OP_ZERO_ALT: {
  446. return ReadOpcodeNearThis(4);
  447. #emit zero.alt
  448. }
  449. case OP_ZERO: {
  450. return ReadOpcodeNearThis(4);
  451. #emit zero 0
  452. }
  453. case OP_ZERO_S: {
  454. return ReadOpcodeNearThis(4);
  455. #emit zero.s 0
  456. }
  457. case OP_SIGN_PRI: {
  458. return ReadOpcodeNearThis(4);
  459. #emit sign.pri
  460. }
  461. case OP_SIGN_ALT: {
  462. return ReadOpcodeNearThis(4);
  463. #emit sign.alt
  464. }
  465. case OP_EQ: {
  466. return ReadOpcodeNearThis(4);
  467. #emit eq
  468. }
  469. case OP_NEQ: {
  470. return ReadOpcodeNearThis(4);
  471. #emit neq
  472. }
  473. case OP_LESS: {
  474. return ReadOpcodeNearThis(4);
  475. #emit less
  476. }
  477. case OP_LEQ: {
  478. return ReadOpcodeNearThis(4);
  479. #emit leq
  480. }
  481. case OP_GRTR: {
  482. return ReadOpcodeNearThis(4);
  483. #emit grtr
  484. }
  485. case OP_GEQ: {
  486. return ReadOpcodeNearThis(4);
  487. #emit geq
  488. }
  489. case OP_SLESS: {
  490. return ReadOpcodeNearThis(4);
  491. #emit sless
  492. }
  493. case OP_SLEQ: {
  494. return ReadOpcodeNearThis(4);
  495. #emit sleq
  496. }
  497. case OP_SGRTR: {
  498. return ReadOpcodeNearThis(4);
  499. #emit sgrtr
  500. }
  501. case OP_SGEQ: {
  502. return ReadOpcodeNearThis(4);
  503. #emit sgeq
  504. }
  505. case OP_EQ_C_PRI: {
  506. return ReadOpcodeNearThis(4);
  507. #emit eq.c.pri 0
  508. }
  509. case OP_EQ_C_ALT: {
  510. return ReadOpcodeNearThis(4);
  511. #emit eq.c.alt 0
  512. }
  513. case OP_INC_PRI: {
  514. return ReadOpcodeNearThis(4);
  515. #emit inc.pri
  516. }
  517. case OP_INC_ALT: {
  518. return ReadOpcodeNearThis(4);
  519. #emit inc.alt
  520. }
  521. case OP_INC: {
  522. return ReadOpcodeNearThis(4);
  523. #emit inc 0
  524. }
  525. case OP_INC_S: {
  526. return ReadOpcodeNearThis(4);
  527. #emit inc.s 0
  528. }
  529. case OP_INC_I: {
  530. return ReadOpcodeNearThis(4);
  531. #emit inc.i
  532. }
  533. case OP_DEC_PRI: {
  534. return ReadOpcodeNearThis(4);
  535. #emit dec.pri
  536. }
  537. case OP_DEC_ALT: {
  538. return ReadOpcodeNearThis(4);
  539. #emit dec.alt
  540. }
  541. case OP_DEC: {
  542. return ReadOpcodeNearThis(4);
  543. #emit dec 0
  544. }
  545. case OP_DEC_S: {
  546. return ReadOpcodeNearThis(4);
  547. #emit dec.s 0
  548. }
  549. case OP_DEC_I: {
  550. return ReadOpcodeNearThis(4);
  551. #emit dec.i
  552. }
  553. case OP_MOVS: {
  554. return ReadOpcodeNearThis(4);
  555. #emit movs 0
  556. }
  557. case OP_CMPS: {
  558. return ReadOpcodeNearThis(4);
  559. #emit cmps 0
  560. }
  561. case OP_FILL: {
  562. return ReadOpcodeNearThis(4);
  563. #emit fill 0
  564. }
  565. case OP_HALT: {
  566. return ReadOpcodeNearThis(4);
  567. #emit halt 0
  568. }
  569. case OP_BOUNDS: {
  570. return ReadOpcodeNearThis(4);
  571. #emit bounds 0
  572. }
  573. case OP_SYSREQ_C: {
  574. return ReadOpcodeNearThis(4);
  575. #emit sysreq.c 0
  576. }
  577. case OP_SWITCH: {
  578. return ReadOpcodeNearThis(4);
  579. #emit switch 0
  580. }
  581. case OP_CASETBL: {
  582. new x = 0;
  583. switch (x) { case 0: return ReadOpcodeNearThis(20); }
  584. }
  585. case OP_SWAP_PRI: {
  586. return ReadOpcodeNearThis(4);
  587. #emit swap.pri
  588. }
  589. case OP_SWAP_ALT: {
  590. return ReadOpcodeNearThis(4);
  591. #emit swap.alt
  592. }
  593. case OP_PUSH_ADR: {
  594. return ReadOpcodeNearThis(4);
  595. #emit push.adr 0
  596. }
  597. case OP_NOP: {
  598. return ReadOpcodeNearThis(4);
  599. #emit nop
  600. }
  601. case OP_SYSREQ_D: {
  602. // Since there's no way to #emit sysreq.d we have to compute its
  603. // address using a relative offset to another opcode, e.g. nop.
  604. new bool:is_crashdetect;
  605. #emit zero.pri
  606. #emit lctrl 0xFF
  607. #emit stor.s.pri is_crashdetect
  608. if (is_crashdetect) {
  609. // The offset is different when running under crashdetect as
  610. // it uses its own (modified) version of the VM. This value
  611. // value works only with crashdetect 4.13.
  612. return RelocateOpcodeNow(OP_NOP) - Opcode:0x1EA;
  613. } else {
  614. return RelocateOpcodeNow(OP_NOP) - Opcode:0x4F;
  615. }
  616. }
  617. case OP_BREAK: {
  618. return ReadOpcodeNearThis(4);
  619. #emit break
  620. }
  621. }
  622. return opcode;
  623. }
  624. static stock Opcode:opcode_table[NUM_OPCODES];
  625. static stock bool:opcode_table_is_ready = false;
  626. static stock InitOpcodeTable() {
  627. for (new i = 0; i < NUM_OPCODES; i++) {
  628. opcode_table[i] = RelocateOpcodeNow(Opcode:i);
  629. }
  630. opcode_table_is_ready = true;
  631. }
  632. stock Opcode:RelocateOpcode(Opcode:opcode) {
  633. if (!opcode_table_is_ready) {
  634. InitOpcodeTable();
  635. }
  636. return opcode_table[_:opcode];
  637. }
  638. stock Opcode:UnrelocateOpcode(Opcode:opcode) {
  639. if (!opcode_table_is_ready) {
  640. InitOpcodeTable();
  641. }
  642. if (OP_NONE <= opcode < Opcode:NUM_OPCODES) {
  643. return opcode;
  644. }
  645. for (new i = 0; i < NUM_OPCODES; i++) {
  646. if (opcode_table[i] == opcode) {
  647. return Opcode:i;
  648. }
  649. }
  650. return opcode;
  651. }