LSL_BaseClass_OPCODES.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. using System;
  28. namespace OpenSim.Grid.ScriptEngine.DotNetEngine.Compiler.LSO
  29. {
  30. public partial class LSL_BaseClass
  31. {
  32. /*
  33. * OPCODES
  34. *
  35. * These are internal "assembly" commands,
  36. * basic operators like "ADD", "PUSH" and "POP"
  37. *
  38. * It also contains managed stack and keeps track of internal variables, etc.
  39. *
  40. */
  41. public void StoreToLocal(UInt32 index)
  42. {
  43. // TODO: How to determine local?
  44. Common.SendToDebug("::StoreToLocal " + index);
  45. if (LocalVariables.ContainsKey(index))
  46. LocalVariables.Remove(index);
  47. LocalVariables.Add(index, LSLStack.Peek());
  48. }
  49. public void StoreToGlobal(UInt32 index)
  50. {
  51. Common.SendToDebug("::StoreToGlobal " + index);
  52. if (GlobalVariables.ContainsKey(index))
  53. GlobalVariables.Remove(index);
  54. GlobalVariables.Add(index, LSLStack.Peek());
  55. }
  56. public void StoreToStatic(UInt32 index)
  57. {
  58. Common.SendToDebug("::StoreToStatic " + index);
  59. //if (StaticVariables.ContainsKey(index))
  60. // StaticVariables.Remove(index);
  61. StaticVariables.Add(index, LSLStack.Peek());
  62. }
  63. public void GetFromLocal(UInt32 index)
  64. {
  65. // TODO: How to determine local?
  66. Common.SendToDebug("::GetFromLocal " + index);
  67. object ret;
  68. LocalVariables.TryGetValue(index, out ret);
  69. LSLStack.Push(ret);
  70. //return ret;
  71. }
  72. public void GetFromGlobal(UInt32 index)
  73. {
  74. Common.SendToDebug("::GetFromGlobal " + index);
  75. object ret;
  76. GlobalVariables.TryGetValue(index, out ret);
  77. LSLStack.Push(ret);
  78. //return ret;
  79. }
  80. public void GetFromStatic(UInt32 index)
  81. {
  82. Common.SendToDebug("::GetFromStatic " + index);
  83. object ret;
  84. StaticVariables.TryGetValue(index, out ret);
  85. Common.SendToDebug("::GetFromStatic - ObjectType: " + ret.GetType().ToString());
  86. LSLStack.Push(ret);
  87. //return ret;
  88. }
  89. public object POPToStack()
  90. {
  91. Common.SendToDebug("::POPToStack");
  92. //return LSLStack.Pop();
  93. object p = LSLStack.Pop();
  94. if (p.GetType() == typeof (UInt32))
  95. return (UInt32) p;
  96. if (p.GetType() == typeof (string))
  97. return (string) p;
  98. if (p.GetType() == typeof (Int32))
  99. return (Int32) p;
  100. if (p.GetType() == typeof (UInt16))
  101. return (UInt16) p;
  102. if (p.GetType() == typeof (float))
  103. return (float) p;
  104. if (p.GetType() == typeof (LSO_Enums.Vector))
  105. return (LSO_Enums.Vector) p;
  106. if (p.GetType() == typeof (LSO_Enums.Rotation))
  107. return (LSO_Enums.Rotation) p;
  108. if (p.GetType() == typeof (LSO_Enums.Key))
  109. return (LSO_Enums.Key) p;
  110. return p;
  111. }
  112. //public object POPToStack(UInt32 count)
  113. //{
  114. // // POP NUMBER FROM TOP OF STACK
  115. // //LSLStack.SetLength(LSLStack.Length - 4);
  116. // Common.SendToDebug("::POPToStack " + count);
  117. // if (count < 2)
  118. // return LSLStack.Pop();
  119. // Stack<object> s = new Stack<object>();
  120. // for (int i = 0; i < count; i++)
  121. // {
  122. // s.Push(LSLStack.Pop);
  123. // }
  124. //}
  125. public void POP()
  126. {
  127. // POP NUMBER FROM TOP OF STACK
  128. //LSLStack.SetLength(LSLStack.Length - 4);
  129. Common.SendToDebug("::POP");
  130. if (LSLStack.Count < 1)
  131. {
  132. //TODO: Temporary fix
  133. Common.SendToDebug("ERROR: TRYING TO POP EMPTY STACK!");
  134. }
  135. else
  136. {
  137. LSLStack.Pop();
  138. }
  139. }
  140. public void PUSH(object Param)
  141. {
  142. if (Param == null)
  143. {
  144. Common.SendToDebug("::PUSH: <null>");
  145. }
  146. else
  147. {
  148. //Common.SendToDebug("::PUSH: " + Param.GetType());
  149. }
  150. LSLStack.Push(Param);
  151. }
  152. public void ADD(UInt32 Param)
  153. {
  154. Common.SendToDebug("::ADD: " + Param);
  155. object o2 = LSLStack.Pop();
  156. object o1 = LSLStack.Pop();
  157. Common.SendToDebug("::ADD: Debug: o1: " + o1.GetType() + " (" + o1.ToString() + "), o2: " + o2.GetType() +
  158. " (" + o2.ToString() + ")");
  159. if (o2.GetType() == typeof (string))
  160. {
  161. LSLStack.Push((string) o1 + (string) o2);
  162. return;
  163. }
  164. if (o2.GetType() == typeof (UInt32))
  165. {
  166. LSLStack.Push((UInt32) o1 + (UInt32) o2);
  167. return;
  168. }
  169. }
  170. public void SUB(UInt32 Param)
  171. {
  172. Common.SendToDebug("::SUB: " + Param);
  173. UInt32 i2 = (UInt32) LSLStack.Pop();
  174. UInt32 i1 = (UInt32) LSLStack.Pop();
  175. LSLStack.Push((UInt32) (i1 - i2));
  176. }
  177. public void MUL(UInt32 Param)
  178. {
  179. Common.SendToDebug("::SUB: " + Param);
  180. UInt32 i2 = (UInt32) LSLStack.Pop();
  181. UInt32 i1 = (UInt32) LSLStack.Pop();
  182. LSLStack.Push((UInt32) (i1*i2));
  183. }
  184. public void DIV(UInt32 Param)
  185. {
  186. Common.SendToDebug("::DIV: " + Param);
  187. UInt32 i2 = (UInt32) LSLStack.Pop();
  188. UInt32 i1 = (UInt32) LSLStack.Pop();
  189. LSLStack.Push((UInt32) (i1/i2));
  190. }
  191. public void MOD(UInt32 Param)
  192. {
  193. Common.SendToDebug("::MOD: " + Param);
  194. UInt32 i2 = (UInt32) LSLStack.Pop();
  195. UInt32 i1 = (UInt32) LSLStack.Pop();
  196. LSLStack.Push((UInt32) (i1%i2));
  197. }
  198. public void EQ(UInt32 Param)
  199. {
  200. Common.SendToDebug("::EQ: " + Param);
  201. UInt32 i2 = (UInt32) LSLStack.Pop();
  202. UInt32 i1 = (UInt32) LSLStack.Pop();
  203. if (i1 == i2)
  204. {
  205. LSLStack.Push((UInt32) 1);
  206. }
  207. else
  208. {
  209. LSLStack.Push((UInt32) 0);
  210. }
  211. }
  212. public void NEQ(UInt32 Param)
  213. {
  214. Common.SendToDebug("::NEQ: " + Param);
  215. UInt32 i2 = (UInt32) LSLStack.Pop();
  216. UInt32 i1 = (UInt32) LSLStack.Pop();
  217. if (i1 != i2)
  218. {
  219. LSLStack.Push((UInt32) 1);
  220. }
  221. else
  222. {
  223. LSLStack.Push((UInt32) 0);
  224. }
  225. }
  226. public void LEQ(UInt32 Param)
  227. {
  228. Common.SendToDebug("::LEQ: " + Param);
  229. UInt32 i2 = (UInt32) LSLStack.Pop();
  230. UInt32 i1 = (UInt32) LSLStack.Pop();
  231. if (i1 <= i2)
  232. {
  233. LSLStack.Push((UInt32) 1);
  234. }
  235. else
  236. {
  237. LSLStack.Push((UInt32) 0);
  238. }
  239. }
  240. public void GEQ(UInt32 Param)
  241. {
  242. Common.SendToDebug("::GEQ: " + Param);
  243. UInt32 i2 = (UInt32) LSLStack.Pop();
  244. UInt32 i1 = (UInt32) LSLStack.Pop();
  245. if (i1 >= i2)
  246. {
  247. LSLStack.Push((UInt32) 1);
  248. }
  249. else
  250. {
  251. LSLStack.Push((UInt32) 0);
  252. }
  253. }
  254. public void LESS(UInt32 Param)
  255. {
  256. Common.SendToDebug("::LESS: " + Param);
  257. UInt32 i2 = (UInt32) LSLStack.Pop();
  258. UInt32 i1 = (UInt32) LSLStack.Pop();
  259. if (i1 < i2)
  260. {
  261. LSLStack.Push((UInt32) 1);
  262. }
  263. else
  264. {
  265. LSLStack.Push((UInt32) 0);
  266. }
  267. }
  268. public void GREATER(UInt32 Param)
  269. {
  270. Common.SendToDebug("::GREATER: " + Param);
  271. UInt32 i2 = (UInt32) LSLStack.Pop();
  272. UInt32 i1 = (UInt32) LSLStack.Pop();
  273. if (i1 > i2)
  274. {
  275. LSLStack.Push((UInt32) 1);
  276. }
  277. else
  278. {
  279. LSLStack.Push((UInt32) 0);
  280. }
  281. }
  282. public void BITAND()
  283. {
  284. Common.SendToDebug("::BITAND");
  285. UInt32 i2 = (UInt32) LSLStack.Pop();
  286. UInt32 i1 = (UInt32) LSLStack.Pop();
  287. LSLStack.Push((UInt32) (i1 & i2));
  288. }
  289. public void BITOR()
  290. {
  291. Common.SendToDebug("::BITOR");
  292. UInt32 i2 = (UInt32) LSLStack.Pop();
  293. UInt32 i1 = (UInt32) LSLStack.Pop();
  294. LSLStack.Push((UInt32) (i1 | i2));
  295. }
  296. public void BITXOR()
  297. {
  298. Common.SendToDebug("::BITXOR");
  299. UInt32 i2 = (UInt32) LSLStack.Pop();
  300. UInt32 i1 = (UInt32) LSLStack.Pop();
  301. LSLStack.Push((UInt32) (i1 ^ i2));
  302. }
  303. public void BOOLAND()
  304. {
  305. Common.SendToDebug("::BOOLAND");
  306. bool b2 = bool.Parse((string) LSLStack.Pop());
  307. bool b1 = bool.Parse((string) LSLStack.Pop());
  308. if (b1 && b2)
  309. {
  310. LSLStack.Push((UInt32) 1);
  311. }
  312. else
  313. {
  314. LSLStack.Push((UInt32) 0);
  315. }
  316. }
  317. public void BOOLOR()
  318. {
  319. Common.SendToDebug("::BOOLOR");
  320. bool b2 = bool.Parse((string) LSLStack.Pop());
  321. bool b1 = bool.Parse((string) LSLStack.Pop());
  322. if (b1 || b2)
  323. {
  324. LSLStack.Push((UInt32) 1);
  325. }
  326. else
  327. {
  328. LSLStack.Push((UInt32) 0);
  329. }
  330. }
  331. public void NEG(UInt32 Param)
  332. {
  333. Common.SendToDebug("::NEG: " + Param);
  334. //UInt32 i2 = (UInt32)LSLStack.Pop();
  335. UInt32 i1 = (UInt32) LSLStack.Pop();
  336. LSLStack.Push((UInt32) (i1*-1));
  337. }
  338. public void BITNOT()
  339. {
  340. //Common.SendToDebug("::BITNOT");
  341. //UInt32 i2 = (UInt32)LSLStack.Pop();
  342. //UInt32 i1 = (UInt32)LSLStack.Pop();
  343. //LSLStack.Push((UInt32)(i1 / i2));
  344. }
  345. public void BOOLNOT()
  346. {
  347. //Common.SendToDebug("::BOOLNOT");
  348. ////UInt32 i2 = (UInt32)LSLStack.Pop();
  349. //UInt32 i1 = (UInt32)LSLStack.Pop();
  350. //LSLStack.Push((UInt32)(i1));
  351. }
  352. }
  353. }