LSL_BaseClass_OPCODES.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS AS IS AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. using System;
  29. namespace OpenSim.Grid.ScriptEngine.DotNetEngine.Compiler.LSO
  30. {
  31. public partial class LSL_BaseClass
  32. {
  33. /*
  34. * OPCODES
  35. *
  36. * These are internal "assembly" commands,
  37. * basic operators like "ADD", "PUSH" and "POP"
  38. *
  39. * It also contains managed stack and keeps track of internal variables, etc.
  40. *
  41. */
  42. public void StoreToLocal(UInt32 index)
  43. {
  44. // TODO: How to determine local?
  45. Common.SendToDebug("::StoreToLocal " + index);
  46. if (LocalVariables.ContainsKey(index))
  47. LocalVariables.Remove(index);
  48. LocalVariables.Add(index, LSLStack.Peek());
  49. }
  50. public void StoreToGlobal(UInt32 index)
  51. {
  52. Common.SendToDebug("::StoreToGlobal " + index);
  53. if (GlobalVariables.ContainsKey(index))
  54. GlobalVariables.Remove(index);
  55. GlobalVariables.Add(index, LSLStack.Peek());
  56. }
  57. public void StoreToStatic(UInt32 index)
  58. {
  59. Common.SendToDebug("::StoreToStatic " + index);
  60. //if (StaticVariables.ContainsKey(index))
  61. // StaticVariables.Remove(index);
  62. StaticVariables.Add(index, LSLStack.Peek());
  63. }
  64. public void GetFromLocal(UInt32 index)
  65. {
  66. // TODO: How to determine local?
  67. Common.SendToDebug("::GetFromLocal " + index);
  68. object ret;
  69. LocalVariables.TryGetValue(index, out ret);
  70. LSLStack.Push(ret);
  71. //return ret;
  72. }
  73. public void GetFromGlobal(UInt32 index)
  74. {
  75. Common.SendToDebug("::GetFromGlobal " + index);
  76. object ret;
  77. GlobalVariables.TryGetValue(index, out ret);
  78. LSLStack.Push(ret);
  79. //return ret;
  80. }
  81. public void GetFromStatic(UInt32 index)
  82. {
  83. Common.SendToDebug("::GetFromStatic " + index);
  84. object ret;
  85. StaticVariables.TryGetValue(index, out ret);
  86. Common.SendToDebug("::GetFromStatic - ObjectType: " + ret.GetType().ToString());
  87. LSLStack.Push(ret);
  88. //return ret;
  89. }
  90. public object POPToStack()
  91. {
  92. Common.SendToDebug("::POPToStack");
  93. //return LSLStack.Pop();
  94. object p = LSLStack.Pop();
  95. if (p.GetType() == typeof (UInt32))
  96. return (UInt32) p;
  97. if (p.GetType() == typeof (string))
  98. return (string) p;
  99. if (p.GetType() == typeof (Int32))
  100. return (Int32) p;
  101. if (p.GetType() == typeof (UInt16))
  102. return (UInt16) p;
  103. if (p.GetType() == typeof (float))
  104. return (float) p;
  105. if (p.GetType() == typeof (LSO_Enums.Vector))
  106. return (LSO_Enums.Vector) p;
  107. if (p.GetType() == typeof (LSO_Enums.Rotation))
  108. return (LSO_Enums.Rotation) p;
  109. if (p.GetType() == typeof (LSO_Enums.Key))
  110. return (LSO_Enums.Key) p;
  111. return p;
  112. }
  113. //public object POPToStack(UInt32 count)
  114. //{
  115. // // POP NUMBER FROM TOP OF STACK
  116. // //LSLStack.SetLength(LSLStack.Length - 4);
  117. // Common.SendToDebug("::POPToStack " + count);
  118. // if (count < 2)
  119. // return LSLStack.Pop();
  120. // Stack<object> s = new Stack<object>();
  121. // for (int i = 0; i < count; i++)
  122. // {
  123. // s.Push(LSLStack.Pop);
  124. // }
  125. //}
  126. public void POP()
  127. {
  128. // POP NUMBER FROM TOP OF STACK
  129. //LSLStack.SetLength(LSLStack.Length - 4);
  130. Common.SendToDebug("::POP");
  131. if (LSLStack.Count < 1)
  132. {
  133. //TODO: Temporary fix
  134. Common.SendToDebug("ERROR: TRYING TO POP EMPTY STACK!");
  135. }
  136. else
  137. {
  138. LSLStack.Pop();
  139. }
  140. }
  141. public void PUSH(object Param)
  142. {
  143. if (Param == null)
  144. {
  145. Common.SendToDebug("::PUSH: <null>");
  146. }
  147. else
  148. {
  149. //Common.SendToDebug("::PUSH: " + Param.GetType());
  150. }
  151. LSLStack.Push(Param);
  152. }
  153. public void ADD(UInt32 Param)
  154. {
  155. Common.SendToDebug("::ADD: " + Param);
  156. object o2 = LSLStack.Pop();
  157. object o1 = LSLStack.Pop();
  158. Common.SendToDebug("::ADD: Debug: o1: " + o1.GetType() + " (" + o1.ToString() + "), o2: " + o2.GetType() +
  159. " (" + o2.ToString() + ")");
  160. if (o2.GetType() == typeof (string))
  161. {
  162. LSLStack.Push((string) o1 + (string) o2);
  163. return;
  164. }
  165. if (o2.GetType() == typeof (UInt32))
  166. {
  167. LSLStack.Push((UInt32) o1 + (UInt32) o2);
  168. return;
  169. }
  170. }
  171. public void SUB(UInt32 Param)
  172. {
  173. Common.SendToDebug("::SUB: " + Param);
  174. UInt32 i2 = (UInt32) LSLStack.Pop();
  175. UInt32 i1 = (UInt32) LSLStack.Pop();
  176. LSLStack.Push((UInt32) (i1 - i2));
  177. }
  178. public void MUL(UInt32 Param)
  179. {
  180. Common.SendToDebug("::SUB: " + Param);
  181. UInt32 i2 = (UInt32) LSLStack.Pop();
  182. UInt32 i1 = (UInt32) LSLStack.Pop();
  183. LSLStack.Push((UInt32) (i1*i2));
  184. }
  185. public void DIV(UInt32 Param)
  186. {
  187. Common.SendToDebug("::DIV: " + Param);
  188. UInt32 i2 = (UInt32) LSLStack.Pop();
  189. UInt32 i1 = (UInt32) LSLStack.Pop();
  190. LSLStack.Push((UInt32) (i1/i2));
  191. }
  192. public void MOD(UInt32 Param)
  193. {
  194. Common.SendToDebug("::MOD: " + Param);
  195. UInt32 i2 = (UInt32) LSLStack.Pop();
  196. UInt32 i1 = (UInt32) LSLStack.Pop();
  197. LSLStack.Push((UInt32) (i1%i2));
  198. }
  199. public void EQ(UInt32 Param)
  200. {
  201. Common.SendToDebug("::EQ: " + Param);
  202. UInt32 i2 = (UInt32) LSLStack.Pop();
  203. UInt32 i1 = (UInt32) LSLStack.Pop();
  204. if (i1 == i2)
  205. {
  206. LSLStack.Push((UInt32) 1);
  207. }
  208. else
  209. {
  210. LSLStack.Push((UInt32) 0);
  211. }
  212. }
  213. public void NEQ(UInt32 Param)
  214. {
  215. Common.SendToDebug("::NEQ: " + Param);
  216. UInt32 i2 = (UInt32) LSLStack.Pop();
  217. UInt32 i1 = (UInt32) LSLStack.Pop();
  218. if (i1 != i2)
  219. {
  220. LSLStack.Push((UInt32) 1);
  221. }
  222. else
  223. {
  224. LSLStack.Push((UInt32) 0);
  225. }
  226. }
  227. public void LEQ(UInt32 Param)
  228. {
  229. Common.SendToDebug("::LEQ: " + Param);
  230. UInt32 i2 = (UInt32) LSLStack.Pop();
  231. UInt32 i1 = (UInt32) LSLStack.Pop();
  232. if (i1 <= i2)
  233. {
  234. LSLStack.Push((UInt32) 1);
  235. }
  236. else
  237. {
  238. LSLStack.Push((UInt32) 0);
  239. }
  240. }
  241. public void GEQ(UInt32 Param)
  242. {
  243. Common.SendToDebug("::GEQ: " + Param);
  244. UInt32 i2 = (UInt32) LSLStack.Pop();
  245. UInt32 i1 = (UInt32) LSLStack.Pop();
  246. if (i1 >= i2)
  247. {
  248. LSLStack.Push((UInt32) 1);
  249. }
  250. else
  251. {
  252. LSLStack.Push((UInt32) 0);
  253. }
  254. }
  255. public void LESS(UInt32 Param)
  256. {
  257. Common.SendToDebug("::LESS: " + Param);
  258. UInt32 i2 = (UInt32) LSLStack.Pop();
  259. UInt32 i1 = (UInt32) LSLStack.Pop();
  260. if (i1 < i2)
  261. {
  262. LSLStack.Push((UInt32) 1);
  263. }
  264. else
  265. {
  266. LSLStack.Push((UInt32) 0);
  267. }
  268. }
  269. public void GREATER(UInt32 Param)
  270. {
  271. Common.SendToDebug("::GREATER: " + Param);
  272. UInt32 i2 = (UInt32) LSLStack.Pop();
  273. UInt32 i1 = (UInt32) LSLStack.Pop();
  274. if (i1 > i2)
  275. {
  276. LSLStack.Push((UInt32) 1);
  277. }
  278. else
  279. {
  280. LSLStack.Push((UInt32) 0);
  281. }
  282. }
  283. public void BITAND()
  284. {
  285. Common.SendToDebug("::BITAND");
  286. UInt32 i2 = (UInt32) LSLStack.Pop();
  287. UInt32 i1 = (UInt32) LSLStack.Pop();
  288. LSLStack.Push((UInt32) (i1 & i2));
  289. }
  290. public void BITOR()
  291. {
  292. Common.SendToDebug("::BITOR");
  293. UInt32 i2 = (UInt32) LSLStack.Pop();
  294. UInt32 i1 = (UInt32) LSLStack.Pop();
  295. LSLStack.Push((UInt32) (i1 | i2));
  296. }
  297. public void BITXOR()
  298. {
  299. Common.SendToDebug("::BITXOR");
  300. UInt32 i2 = (UInt32) LSLStack.Pop();
  301. UInt32 i1 = (UInt32) LSLStack.Pop();
  302. LSLStack.Push((UInt32) (i1 ^ i2));
  303. }
  304. public void BOOLAND()
  305. {
  306. Common.SendToDebug("::BOOLAND");
  307. bool b2 = bool.Parse((string) LSLStack.Pop());
  308. bool b1 = bool.Parse((string) LSLStack.Pop());
  309. if (b1 && b2)
  310. {
  311. LSLStack.Push((UInt32) 1);
  312. }
  313. else
  314. {
  315. LSLStack.Push((UInt32) 0);
  316. }
  317. }
  318. public void BOOLOR()
  319. {
  320. Common.SendToDebug("::BOOLOR");
  321. bool b2 = bool.Parse((string) LSLStack.Pop());
  322. bool b1 = bool.Parse((string) LSLStack.Pop());
  323. if (b1 || b2)
  324. {
  325. LSLStack.Push((UInt32) 1);
  326. }
  327. else
  328. {
  329. LSLStack.Push((UInt32) 0);
  330. }
  331. }
  332. public void NEG(UInt32 Param)
  333. {
  334. Common.SendToDebug("::NEG: " + Param);
  335. //UInt32 i2 = (UInt32)LSLStack.Pop();
  336. UInt32 i1 = (UInt32) LSLStack.Pop();
  337. LSLStack.Push((UInt32) (i1*-1));
  338. }
  339. public void BITNOT()
  340. {
  341. //Common.SendToDebug("::BITNOT");
  342. //UInt32 i2 = (UInt32)LSLStack.Pop();
  343. //UInt32 i1 = (UInt32)LSLStack.Pop();
  344. //LSLStack.Push((UInt32)(i1 / i2));
  345. }
  346. public void BOOLNOT()
  347. {
  348. //Common.SendToDebug("::BOOLNOT");
  349. ////UInt32 i2 = (UInt32)LSLStack.Pop();
  350. //UInt32 i1 = (UInt32)LSLStack.Pop();
  351. //LSLStack.Push((UInt32)(i1));
  352. }
  353. }
  354. }