1
0

amx_memory.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 AMX_MEMORY_INC
  21. #endinput
  22. #endif
  23. #define AMX_MEMORY_INC
  24. #include <core>
  25. // Returns the address of a variable/array.
  26. stock ref(...) {
  27. assert(numargs() == 1);
  28. #emit load.s.pri 12 // first argument
  29. #emit retn
  30. return 0; // make compiler happy
  31. }
  32. // Returns an array from an address.
  33. stock deref(v) {
  34. static gFake[1];
  35. #emit load.s.pri v // first argument
  36. #emit stor.s.pri 16 // secret argument
  37. #emit retn
  38. return gFake; // make compiler happy
  39. }
  40. // Returns the address of a variable parameter.
  41. stock argref(n) {
  42. #emit load.s.alt 0
  43. #emit load.s.pri n
  44. #emit add.c 3
  45. #emit lidx
  46. #emit retn
  47. return 0; // make compiler happy
  48. }
  49. // Returns an array from a variable parameter.
  50. stock argderef(n) {
  51. static gFake[1];
  52. #emit load.s.alt 0
  53. #emit load.s.pri n
  54. #emit add.c 3
  55. #emit lidx
  56. #emit stor.s.pri 16 // secret argument
  57. #emit retn
  58. return gFake; // make compiler happy
  59. }
  60. stock ReadAmxMemory(address) {
  61. #emit lref.s.pri address
  62. #emit retn
  63. return 0; // make compiler happy
  64. }
  65. stock ReadAmxMemoryArray(address, values[], size = sizeof(values)) {
  66. for (new i = 0; i < size; i++) {
  67. values[i] = ReadAmxMemory(address + i * 4);
  68. }
  69. }
  70. stock WriteAmxMemory(address, value) {
  71. #emit load.s.pri value
  72. #emit sref.s.pri address
  73. #emit retn
  74. return 0; // make compiler happy
  75. }
  76. stock WriteAmxMemoryArray(address, const values[], size = sizeof(values)) {
  77. for (new i = 0; i < size; i++) {
  78. WriteAmxMemory(address + i * 4, values[i]);
  79. }
  80. }
  81. stock GetAmxNextInstructionPointer()
  82. {
  83. // Saying "get the current pointer" doesn't make a huge amount of sense -
  84. // getting the pointer will in itself take code, so exactly where is "here"?
  85. // This returns its own return address, which points to the instruction
  86. // directly after the call. This is at least well defined.
  87. #emit load.s.pri 4
  88. #emit retn
  89. return 0;
  90. }
  91. stock GetAmxHeapBase()
  92. {
  93. // Initial heap pointer. Not stored in an accessible register so read it
  94. // straight from the original header.
  95. #emit lctrl 1 // DAT
  96. #emit neg // -DAT
  97. #emit add.c 20 // -DAT + 20
  98. #emit push.pri
  99. #emit lref.s.pri 0xfffffffc
  100. #emit stack 4
  101. #emit retn
  102. return 0;
  103. }
  104. stock GetAmxHeapTop()
  105. {
  106. // Current heap pointer.
  107. #emit lctrl 2
  108. #emit retn
  109. return 0;
  110. }
  111. stock GetAmxStackBase()
  112. {
  113. #emit lctrl 3
  114. #emit retn
  115. return 0;
  116. }
  117. stock GetAmxStackBottom()
  118. {
  119. #emit lctrl 4
  120. #emit add.c 12
  121. #emit retn
  122. return 0;
  123. }
  124. stock GetAmxFrame()
  125. {
  126. // Doesn't use "lctrl 5" because we want it to look like this function was
  127. // never called. Doing that would only return this function's frame.
  128. #emit load.s.pri 0
  129. #emit retn
  130. return 0;
  131. }
  132. stock SetAmxHeapTop(ptr)
  133. {
  134. #emit load.s.pri ptr
  135. #emit sctrl 2
  136. }
  137. stock SetAmxStackBottom(ptr)
  138. {
  139. static
  140. cip;
  141. // We need to be tricky here, because the return value is on the stack.
  142. #emit load.s.alt ptr
  143. // Store the return address.
  144. #emit load.s.pri 4
  145. #emit stor.pri cip
  146. // Reset the frame.
  147. #emit load.s.pri 0
  148. #emit sctrl 5
  149. // Modify the stack.
  150. #emit move.pri
  151. #emit sctrl 4
  152. // Return.
  153. #emit load.pri cip
  154. #emit sctrl 8
  155. #emit sctrl 6
  156. }
  157. stock SetAmxFrame(ptr)
  158. {
  159. // We need to be tricky here, because the return value is in our frame.
  160. // Store the return address.
  161. #emit load.s.alt 4
  162. // Modify the frame.
  163. #emit load.s.pri ptr
  164. #emit sctrl 5
  165. // Return.
  166. #emit move.pri
  167. #emit stack 16
  168. #emit sctrl 8
  169. #emit sctrl 6
  170. }
  171. stock SetAmxNextInstructionPointer(ptr)
  172. {
  173. #emit load.s.alt ptr
  174. // Reset the frame.
  175. #emit load.s.pri 0
  176. #emit sctrl 5
  177. // Return.
  178. #emit move.pri
  179. #emit stack 16
  180. #emit sctrl 8
  181. #emit sctrl 6
  182. }