MMRScriptCompValu.cs 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896
  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 OpenSimulator 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 OpenSim.Region.ScriptEngine.Shared.ScriptBase;
  28. using OpenSim.Region.ScriptEngine.Yengine;
  29. using System;
  30. using System.Collections.Generic;
  31. using System.IO;
  32. using System.Reflection;
  33. using System.Reflection.Emit;
  34. using LSL_Float = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLFloat;
  35. using LSL_Integer = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger;
  36. using LSL_Key = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
  37. using LSL_List = OpenSim.Region.ScriptEngine.Shared.LSL_Types.list;
  38. using LSL_Rotation = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Quaternion;
  39. using LSL_String = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
  40. using LSL_Vector = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Vector3;
  41. /**
  42. * @brief Compute values used during code generation to keep track of where computed values are stored.
  43. *
  44. * Conceptually holds the memory address and type of the value
  45. * such as that used for a local variable, global variable, temporary variable.
  46. * Also used for things like constants and function/method entrypoints,
  47. * they are basically treated as read-only variables.
  48. *
  49. * cv.type - type of the value
  50. *
  51. * cv.PushVal() - pushes the value on the CIL stack
  52. * cv.PushRef() - pushes address of the value on the CIL stack
  53. *
  54. * cv.PopPre() - gets ready to pop from the CIL stack
  55. * ...by possibly pushing something
  56. * <push value to be popped>
  57. * cv.PushPre() - pops value from the CIL stack
  58. *
  59. * If the type is a TokenTypeSDTypeDelegate, the location is callable,
  60. * so you get these additional functions:
  61. *
  62. * cv.GetRetType() - gets function/method's return value type
  63. * TokenTypeVoid if void
  64. * null if not a delegate
  65. * cv.GetArgTypes() - gets array of argument types
  66. * as seen by script level, ie,
  67. * does not include any hidden 'this' type
  68. * cv.GetArgSig() - gets argument signature eg, "(integer,list)"
  69. * null if not a delegate
  70. *
  71. * cv.CallPre() - gets ready to call the function/method
  72. * ...by possibly pushing something
  73. * such as a 'this' pointer
  74. * <push call args left-to-right>
  75. * cv.CallPost() - calls the function/method
  76. */
  77. namespace OpenSim.Region.ScriptEngine.Yengine
  78. {
  79. /**
  80. * @brief Location of a value
  81. * Includes constants, expressions and temp variables.
  82. */
  83. public abstract class CompValu
  84. {
  85. protected static readonly MethodInfo gsmdMethodInfo =
  86. typeof(XMRInstAbstract).GetMethod("GetScriptMethodDelegate",
  87. new Type[] { typeof(string), typeof(string), typeof(object) });
  88. private static readonly MethodInfo avpmListMethInfo = typeof(XMRInstArrays).GetMethod("PopList", new Type[] { typeof(int), typeof(LSL_List) });
  89. private static readonly MethodInfo avpmObjectMethInfo = typeof(XMRInstArrays).GetMethod("PopObject", new Type[] { typeof(int), typeof(object) });
  90. private static readonly MethodInfo avpmStringMethInfo = typeof(XMRInstArrays).GetMethod("PopString", new Type[] { typeof(int), typeof(string) });
  91. public TokenType type; // type of the value and where in the source it was used
  92. public CompValu(TokenType type)
  93. {
  94. this.type = type;
  95. }
  96. public Type ToSysType()
  97. {
  98. return (type.ToLSLWrapType() != null) ? type.ToLSLWrapType() : type.ToSysType();
  99. }
  100. /*
  101. * if a field of an XMRInstArrays array cannot be directly written,
  102. * get the method that can write it
  103. */
  104. private static MethodInfo ArrVarPopMeth(FieldInfo fi)
  105. {
  106. if(fi.Name == "iarLists")
  107. return avpmListMethInfo;
  108. if(fi.Name == "iarObjects")
  109. return avpmObjectMethInfo;
  110. if(fi.Name == "iarStrings")
  111. return avpmStringMethInfo;
  112. return null;
  113. }
  114. /*
  115. * emit code to push value onto stack
  116. */
  117. public void PushVal(ScriptCodeGen scg, Token errorAt, TokenType stackType)
  118. {
  119. this.PushVal(scg, errorAt, stackType, false);
  120. }
  121. public void PushVal(ScriptCodeGen scg, Token errorAt, TokenType stackType, bool explicitAllowed)
  122. {
  123. this.PushVal(scg, errorAt);
  124. TypeCast.CastTopOfStack(scg, errorAt, this.type, stackType, explicitAllowed);
  125. }
  126. public abstract void PushVal(ScriptCodeGen scg, Token errorAt);
  127. public abstract void PushRef(ScriptCodeGen scg, Token errorAt);
  128. /*
  129. * emit code to pop value from stack
  130. */
  131. public void PopPost(ScriptCodeGen scg, Token errorAt, TokenType stackType)
  132. {
  133. TypeCast.CastTopOfStack(scg, errorAt, stackType, this.type, false);
  134. this.PopPost(scg, errorAt);
  135. }
  136. public virtual void PopPre(ScriptCodeGen scg, Token errorAt)
  137. {
  138. }
  139. /*
  140. * call this before pushing value to be popped
  141. */
  142. public abstract void PopPost(ScriptCodeGen scg, Token errorAt); // call this after pushing value to be popped
  143. /*
  144. * return true: doing a PushVal() does not involve CheckRun()
  145. * false: otherwise
  146. */
  147. public virtual bool IsReadTrivial(ScriptCodeGen scg, Token readAt)
  148. {
  149. return true;
  150. }
  151. /*
  152. * These additional functions are available if the type is a delegate
  153. */
  154. public TokenType GetRetType()
  155. {
  156. if(!(type is TokenTypeSDTypeDelegate))
  157. return null;
  158. return ((TokenTypeSDTypeDelegate)type).decl.GetRetType();
  159. }
  160. public TokenType[] GetArgTypes()
  161. {
  162. if(!(type is TokenTypeSDTypeDelegate))
  163. return null;
  164. return ((TokenTypeSDTypeDelegate)type).decl.GetArgTypes();
  165. }
  166. public string GetArgSig()
  167. {
  168. if(!(type is TokenTypeSDTypeDelegate))
  169. return null;
  170. return ((TokenTypeSDTypeDelegate)type).decl.GetArgSig();
  171. }
  172. /*
  173. * These are used only if type is a delegate too
  174. * - but it is a real delegate pointer in a global or local variable or a field, etc
  175. * - ie, PushVal() pushes a delegate pointer
  176. * - so we must have CallPre() push the delegate pointer as a 'this' for this.Invoke(...)
  177. * - and CallPost() call the delegate's Invoke() method
  178. * - we assume the target function is non-trivial so we always use a call label
  179. */
  180. public virtual void CallPre(ScriptCodeGen scg, Token errorAt) // call this before pushing arguments
  181. {
  182. new ScriptCodeGen.CallLabel(scg, errorAt);
  183. this.PushVal(scg, errorAt);
  184. }
  185. public virtual void CallPost(ScriptCodeGen scg, Token errorAt) // call this after pushing arguments
  186. {
  187. TokenTypeSDTypeDelegate ttd = (TokenTypeSDTypeDelegate)type;
  188. MethodInfo invokeMethodInfo = ttd.decl.GetInvokerInfo();
  189. scg.ilGen.Emit(errorAt, OpCodes.Callvirt, invokeMethodInfo);
  190. scg.openCallLabel = null;
  191. }
  192. /*
  193. * Utilities used by CompValuGlobalVar and CompValuInstField
  194. * where the value is located in a type-dependent array.
  195. */
  196. protected void EmitFieldPushVal(ScriptCodeGen scg, Token errorAt, TokenDeclVar var)
  197. {
  198. scg.ilGen.Emit(errorAt, OpCodes.Ldfld, var.vTableArray); // which array
  199. scg.ilGen.Emit(errorAt, OpCodes.Ldc_I4, var.vTableIndex); // which array element
  200. if(type is TokenTypeFloat)
  201. {
  202. scg.ilGen.Emit(errorAt, OpCodes.Ldelem_R8);
  203. }
  204. else if(type is TokenTypeInt)
  205. {
  206. scg.ilGen.Emit(errorAt, OpCodes.Ldelem_I4);
  207. }
  208. else if(type is TokenTypeSDTypeDelegate)
  209. {
  210. scg.ilGen.Emit(errorAt, OpCodes.Ldelem, typeof(object));
  211. scg.ilGen.Emit(errorAt, OpCodes.Castclass, ToSysType());
  212. }
  213. else
  214. {
  215. scg.ilGen.Emit(errorAt, OpCodes.Ldelem, ToSysType());
  216. }
  217. }
  218. protected void EmitFieldPushRef(ScriptCodeGen scg, Token errorAt, TokenDeclVar var)
  219. {
  220. if(ArrVarPopMeth(var.vTableArray) != null)
  221. {
  222. scg.ErrorMsg(errorAt, "can't take address of this variable");
  223. }
  224. scg.ilGen.Emit(errorAt, OpCodes.Ldfld, var.vTableArray);
  225. scg.ilGen.Emit(errorAt, OpCodes.Ldc_I4, var.vTableIndex);
  226. scg.ilGen.Emit(errorAt, OpCodes.Ldelema, ToSysType());
  227. }
  228. protected void EmitFieldPopPre(ScriptCodeGen scg, Token errorAt, TokenDeclVar var)
  229. {
  230. if(ArrVarPopMeth(var.vTableArray) != null)
  231. {
  232. scg.ilGen.Emit(errorAt, OpCodes.Ldc_I4, var.vTableIndex);
  233. }
  234. else
  235. {
  236. scg.ilGen.Emit(errorAt, OpCodes.Ldfld, var.vTableArray);
  237. scg.ilGen.Emit(errorAt, OpCodes.Ldc_I4, var.vTableIndex);
  238. }
  239. }
  240. protected void EmitFieldPopPost(ScriptCodeGen scg, Token errorAt, TokenDeclVar var)
  241. {
  242. if(ArrVarPopMeth(var.vTableArray) != null)
  243. {
  244. scg.ilGen.Emit(errorAt, OpCodes.Call, ArrVarPopMeth(var.vTableArray));
  245. }
  246. else if(type is TokenTypeFloat)
  247. {
  248. scg.ilGen.Emit(errorAt, OpCodes.Stelem_R8);
  249. }
  250. else if(type is TokenTypeInt)
  251. {
  252. scg.ilGen.Emit(errorAt, OpCodes.Stelem_I4);
  253. }
  254. else if(type is TokenTypeSDTypeDelegate)
  255. {
  256. scg.ilGen.Emit(errorAt, OpCodes.Stelem, typeof(object));
  257. }
  258. else
  259. {
  260. scg.ilGen.Emit(errorAt, OpCodes.Stelem, ToSysType());
  261. }
  262. }
  263. /**
  264. * @brief With value pushed on stack, emit code to set a property by calling its setter() method.
  265. * @param scg = which script is being compiled
  266. * @param errorAt = for error messages
  267. * @param type = property type
  268. * @param setProp = setter() method
  269. */
  270. protected void EmitPopPostProp(ScriptCodeGen scg, Token errorAt, TokenType type, CompValu setProp)
  271. {
  272. ScriptMyLocal temp = scg.ilGen.DeclareLocal(type.ToSysType(), "__spr_" + errorAt.Unique);
  273. scg.ilGen.Emit(errorAt, OpCodes.Stloc, temp);
  274. setProp.CallPre(scg, errorAt);
  275. scg.ilGen.Emit(errorAt, OpCodes.Ldloc, temp);
  276. setProp.CallPost(scg, errorAt);
  277. }
  278. }
  279. // The value is kept in an (XMR_Array) array element
  280. public class CompValuArEle: CompValu
  281. {
  282. public CompValu arr;
  283. private CompValu idx;
  284. private TokenTypeObject tto;
  285. private static readonly MethodInfo getByKeyMethodInfo = typeof(XMR_Array).GetMethod("GetByKey",
  286. new Type[] { typeof(object) });
  287. private static readonly MethodInfo setByKeyMethodInfo = typeof(XMR_Array).GetMethod("SetByKey",
  288. new Type[] { typeof (object),
  289. typeof (object) });
  290. // type = TokenTypeObject always, as our array elements are always of type 'object'
  291. // arr = where the array object itself is stored
  292. // idx = where the index value is stored
  293. public CompValuArEle(TokenType type, CompValu arr, CompValu idx) : base(type)
  294. {
  295. this.arr = arr;
  296. this.idx = idx;
  297. this.tto = new TokenTypeObject(this.type);
  298. }
  299. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  300. {
  301. arr.PushVal(scg, errorAt); // array
  302. idx.PushVal(scg, errorAt, this.tto); // key
  303. scg.ilGen.Emit(errorAt, OpCodes.Call, getByKeyMethodInfo);
  304. }
  305. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  306. {
  307. scg.ErrorMsg(errorAt, "array element not allowed here");
  308. scg.ilGen.Emit(errorAt, OpCodes.Ldnull);
  309. }
  310. public override void PopPre(ScriptCodeGen scg, Token errorAt)
  311. {
  312. arr.PushVal(scg, errorAt); // array
  313. idx.PushVal(scg, errorAt, this.tto); // key
  314. }
  315. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  316. {
  317. scg.ilGen.Emit(errorAt, OpCodes.Call, setByKeyMethodInfo);
  318. }
  319. // non-trivial because it needs to be copied into a temp
  320. // in case the idiot does dumb-ass side effects tricks
  321. // eg, (x = 0) + x + 2
  322. // should read old value of x not 0
  323. // but if 'xmroption norighttoleft;' in effect,
  324. // we can read it in any order so reading an
  325. // XMR_Array element is trivial
  326. public override bool IsReadTrivial(ScriptCodeGen scg, Token readAt)
  327. {
  328. return readAt.nr2l;
  329. }
  330. }
  331. // The value is kept in the current function's argument list
  332. public class CompValuArg: CompValu
  333. {
  334. public int index;
  335. public bool readOnly;
  336. private static OpCode[] ldargs = { OpCodes.Ldarg_0, OpCodes.Ldarg_1,
  337. OpCodes.Ldarg_2, OpCodes.Ldarg_3 };
  338. public CompValuArg(TokenType type, int index) : base(type)
  339. {
  340. this.index = index;
  341. }
  342. public CompValuArg(TokenType type, int index, bool ro) : base(type)
  343. {
  344. this.index = index;
  345. this.readOnly = ro;
  346. }
  347. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  348. {
  349. if(index < ldargs.Length)
  350. scg.ilGen.Emit(errorAt, ldargs[index]);
  351. else if(index <= 255)
  352. scg.ilGen.Emit(errorAt, OpCodes.Ldarg_S, index);
  353. else
  354. scg.ilGen.Emit(errorAt, OpCodes.Ldarg, index);
  355. }
  356. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  357. {
  358. if(readOnly)
  359. {
  360. scg.ErrorMsg(errorAt, "location cannot be written to");
  361. }
  362. if(index <= 255)
  363. scg.ilGen.Emit(errorAt, OpCodes.Ldarga_S, index);
  364. else
  365. scg.ilGen.Emit(errorAt, OpCodes.Ldarga, index);
  366. }
  367. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  368. {
  369. if(readOnly)
  370. {
  371. scg.ErrorMsg(errorAt, "location cannot be written to");
  372. }
  373. scg.ilGen.Emit(errorAt, OpCodes.Starg, index);
  374. }
  375. // non-trivial because it needs to be copied into a temp
  376. // in case the idiot does dumb-ass side effects tricks
  377. // eg, (x = 0) + x + 2
  378. // should read old value of x not 0
  379. // but if 'xmroption norighttoleft;' in effect,
  380. // we can read it in any order so reading an
  381. // argument is trivial
  382. public override bool IsReadTrivial(ScriptCodeGen scg, Token readAt)
  383. {
  384. return readAt.nr2l;
  385. }
  386. }
  387. // The value is a character constant
  388. public class CompValuChar: CompValu
  389. {
  390. public char x;
  391. public CompValuChar(TokenType type, char x) : base(type)
  392. {
  393. if(!(this.type is TokenTypeChar))
  394. {
  395. this.type = new TokenTypeChar(this.type);
  396. }
  397. this.x = x;
  398. }
  399. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  400. {
  401. scg.ilGen.Emit(errorAt, OpCodes.Ldc_I4, (int)x);
  402. }
  403. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  404. {
  405. throw new Exception("cannot get constant's address");
  406. }
  407. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  408. {
  409. throw new Exception("cannot store into contant");
  410. }
  411. }
  412. // The value is kept in a struct/class field of an internal struct/class
  413. public class CompValuField: CompValu
  414. {
  415. CompValu obj;
  416. FieldInfo field;
  417. public CompValuField(TokenType type, CompValu obj, FieldInfo field) : base(type)
  418. {
  419. this.obj = obj;
  420. this.field = field;
  421. }
  422. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  423. {
  424. if(field.ReflectedType.IsValueType)
  425. {
  426. obj.PushRef(scg, errorAt);
  427. }
  428. else
  429. {
  430. obj.PushVal(scg, errorAt);
  431. }
  432. scg.ilGen.Emit(errorAt, OpCodes.Ldfld, field);
  433. }
  434. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  435. {
  436. if(field.ReflectedType.IsValueType)
  437. {
  438. obj.PushRef(scg, errorAt);
  439. }
  440. else
  441. {
  442. obj.PushVal(scg, errorAt);
  443. }
  444. scg.ilGen.Emit(errorAt, OpCodes.Ldflda, field);
  445. }
  446. public override void PopPre(ScriptCodeGen scg, Token errorAt)
  447. {
  448. if(field.ReflectedType.IsValueType)
  449. {
  450. obj.PushRef(scg, errorAt);
  451. }
  452. else
  453. {
  454. obj.PushVal(scg, errorAt);
  455. }
  456. }
  457. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  458. {
  459. scg.ilGen.Emit(errorAt, OpCodes.Stfld, field);
  460. }
  461. // non-trivial because it needs to be copied into a temp
  462. // in case the idiot does dumb-ass side effects tricks
  463. // eg, (x = 0) + x + 2
  464. // should read old value of x not 0
  465. // but if 'xmroption norighttoleft;' in effect,
  466. // we can read it in any order so reading an
  467. // field of a class/struct is trivial
  468. public override bool IsReadTrivial(ScriptCodeGen scg, Token readAt)
  469. {
  470. return readAt.nr2l;
  471. }
  472. }
  473. // Accessing an element of a fixed-dimension array
  474. public class CompValuFixArEl: CompValu
  475. {
  476. private CompValu baseRVal;
  477. private CompValu[] subRVals;
  478. private int nSubs;
  479. private TokenDeclVar getFunc;
  480. private TokenDeclVar setFunc;
  481. private TokenTypeInt tokenTypeInt;
  482. /**
  483. * @brief Set up to access an element of an array.
  484. * @param scg = what script we are compiling
  485. * @param baseRVal = what array we are accessing
  486. * @param subRVals = the subscripts being applied
  487. */
  488. public CompValuFixArEl(ScriptCodeGen scg, CompValu baseRVal, CompValu[] subRVals) : base(GetElementType(scg, baseRVal, subRVals))
  489. {
  490. this.baseRVal = baseRVal; // location of the array itself
  491. this.subRVals = subRVals; // subscript values
  492. this.nSubs = subRVals.Length;
  493. TokenTypeSDTypeClass sdtType = (TokenTypeSDTypeClass)baseRVal.type;
  494. TokenDeclSDTypeClass sdtDecl = sdtType.decl;
  495. tokenTypeInt = new TokenTypeInt(sdtType);
  496. TokenName name = new TokenName(sdtType, "Get");
  497. TokenType[] argsig = new TokenType[nSubs];
  498. for(int i = 0; i < nSubs; i++)
  499. {
  500. argsig[i] = tokenTypeInt;
  501. }
  502. getFunc = scg.FindThisMember(sdtDecl, name, argsig);
  503. name = new TokenName(sdtType, "Set");
  504. argsig = new TokenType[nSubs + 1];
  505. for(int i = 0; i < nSubs; i++)
  506. {
  507. argsig[i] = tokenTypeInt;
  508. }
  509. argsig[nSubs] = getFunc.retType;
  510. setFunc = scg.FindThisMember(sdtDecl, name, argsig);
  511. }
  512. /**
  513. * @brief Read array element and push value on stack.
  514. */
  515. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  516. {
  517. // call script-defined class' Get() method to fetch the value
  518. baseRVal.PushVal(scg, errorAt);
  519. for(int i = 0; i < nSubs; i++)
  520. {
  521. subRVals[i].PushVal(scg, errorAt, tokenTypeInt);
  522. }
  523. scg.ilGen.Emit(errorAt, OpCodes.Call, getFunc.ilGen);
  524. }
  525. /**
  526. * @brief Push address of array element on stack.
  527. */
  528. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  529. {
  530. throw new Exception("tu stOOpid to get array element address");
  531. }
  532. /**
  533. * @brief Prepare to write array element.
  534. */
  535. public override void PopPre(ScriptCodeGen scg, Token errorAt)
  536. {
  537. // set up call to script-defined class' Set() method to write the value
  538. baseRVal.PushVal(scg, errorAt);
  539. for(int i = 0; i < nSubs; i++)
  540. {
  541. subRVals[i].PushVal(scg, errorAt, tokenTypeInt);
  542. }
  543. }
  544. /**
  545. * @brief Pop value from stack and write array element.
  546. */
  547. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  548. {
  549. // call script-defined class' Set() method to write the value
  550. scg.ilGen.Emit(errorAt, OpCodes.Call, setFunc.ilGen);
  551. }
  552. /**
  553. * @brief Get the array element type by getting the Get() functions return type.
  554. * Crude but effective.
  555. * @param scg = what script we are compiling
  556. * @param baseRVal = what array we are accessing
  557. * @param subRVals = the subscripts being applied
  558. * @returns array element type
  559. */
  560. private static TokenType GetElementType(ScriptCodeGen scg, CompValu baseRVal, CompValu[] subRVals)
  561. {
  562. TokenTypeSDTypeClass sdtType = (TokenTypeSDTypeClass)baseRVal.type;
  563. TokenDeclSDTypeClass sdtDecl = sdtType.decl;
  564. TokenName name = new TokenName(sdtType, "Get");
  565. int nSubs = subRVals.Length;
  566. TokenType[] argsig = new TokenType[nSubs];
  567. argsig[0] = new TokenTypeInt(sdtType);
  568. for(int i = 0; ++i < nSubs;)
  569. {
  570. argsig[i] = argsig[0];
  571. }
  572. TokenDeclVar getFunc = scg.FindThisMember(sdtDecl, name, argsig);
  573. return getFunc.retType;
  574. }
  575. // non-trivial because it needs to be copied into a temp
  576. // in case the idiot does dumb-ass side effects tricks
  577. // eg, (x = 0) + x + 2
  578. // should read old value of x not 0
  579. // but if 'xmroption norighttoleft;' in effect,
  580. // we can read it in any order so reading an
  581. // fixed-dimension array element is trivial
  582. public override bool IsReadTrivial(ScriptCodeGen scg, Token readAt)
  583. {
  584. return readAt.nr2l;
  585. }
  586. }
  587. // The value is a float constant
  588. public class CompValuFloat: CompValu
  589. {
  590. public double x;
  591. public CompValuFloat(TokenType type, double x) : base(type)
  592. {
  593. if(!(this.type is TokenTypeFloat))
  594. {
  595. this.type = new TokenTypeFloat(this.type);
  596. }
  597. this.x = x;
  598. }
  599. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  600. {
  601. scg.ilGen.Emit(errorAt, OpCodes.Ldc_R8, x);
  602. }
  603. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  604. {
  605. throw new Exception("cannot get constant's address");
  606. }
  607. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  608. {
  609. throw new Exception("cannot store into constant");
  610. }
  611. }
  612. // The value is the entrypoint of a script-defined global function.
  613. // These are also used for script-defined type static methods as the calling convention is the same,
  614. // ie, the XMRInstance pointer is a hidden first argument.
  615. // There is just one of these created when the function is being compiled as there is only one value
  616. // of the function.
  617. public class CompValuGlobalMeth: CompValu
  618. {
  619. private TokenDeclVar func;
  620. public CompValuGlobalMeth(TokenDeclVar declFunc) : base(declFunc.GetDelType())
  621. {
  622. this.func = declFunc;
  623. }
  624. /**
  625. * @brief PushVal for a function/method means push a delegate on the stack.
  626. * We build a call to the DynamicMethod's CreateDelegate() function
  627. * to create the delegate. Slip the scriptinstance pointer as the
  628. * function's arg 0 so it will get passed to the function when called.
  629. */
  630. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  631. {
  632. string dtn = type.ToString();
  633. if(dtn.StartsWith("delegate "))
  634. dtn = dtn.Substring(9);
  635. // delegateinstance = (signature)scriptinstance.GetScriptMethodDelegate (methName, signature, arg0);
  636. // where methName = [<sdtclass>.]<methname>(<argtypes>)
  637. // signature = <rettype>(<argtypes>)
  638. // arg0 = scriptinstance (XMRInstance)
  639. scg.PushXMRInst(); // [0] scriptinstance
  640. scg.ilGen.Emit(errorAt, OpCodes.Ldstr, func.ilGen.methName); // [1] method name
  641. scg.ilGen.Emit(errorAt, OpCodes.Ldstr, dtn); // [2] delegate type name
  642. scg.PushXMRInst(); // [3] scriptinstance
  643. scg.ilGen.Emit(errorAt, OpCodes.Callvirt, gsmdMethodInfo); // [0] delegate instance
  644. scg.ilGen.Emit(errorAt, OpCodes.Castclass, type.ToSysType()); // [0] cast to correct delegate class
  645. }
  646. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  647. {
  648. throw new Exception("cannot get ref to global method");
  649. }
  650. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  651. {
  652. throw new Exception("cannot store into global method");
  653. }
  654. /**
  655. * @brief A direct call is much simpler than pushing a delegate.
  656. * Just push the XMRInstance pointer, push the args and finally call the function.
  657. */
  658. public override void CallPre(ScriptCodeGen scg, Token errorAt)
  659. {
  660. if(!this.func.IsFuncTrivial(scg))
  661. new ScriptCodeGen.CallLabel(scg, errorAt);
  662. // all script-defined global functions are static methods created by DynamicMethod()
  663. // and the first argument is always the XMR_Instance pointer
  664. scg.PushXMRInst();
  665. }
  666. public override void CallPost(ScriptCodeGen scg, Token errorAt)
  667. {
  668. scg.ilGen.Emit(errorAt, OpCodes.Call, func.ilGen);
  669. if(!this.func.IsFuncTrivial(scg))
  670. scg.openCallLabel = null;
  671. }
  672. }
  673. // The value is in a script-global variable = ScriptModule instance variable
  674. // It could also be a script-global property
  675. public class CompValuGlobalVar: CompValu
  676. {
  677. private static readonly FieldInfo glblVarsFieldInfo = typeof(XMRInstAbstract).GetField("glblVars");
  678. private TokenDeclVar declVar;
  679. public CompValuGlobalVar(TokenDeclVar declVar, XMRInstArSizes glblSizes) : base(declVar.type)
  680. {
  681. this.declVar = declVar;
  682. if((declVar.getProp == null) && (declVar.setProp == null))
  683. {
  684. declVar.type.AssignVarSlot(declVar, glblSizes);
  685. }
  686. }
  687. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  688. {
  689. if((declVar.getProp == null) && (declVar.setProp == null))
  690. {
  691. scg.PushXMRInst();
  692. scg.ilGen.Emit(errorAt, OpCodes.Ldfld, glblVarsFieldInfo);
  693. EmitFieldPushVal(scg, errorAt, declVar);
  694. }
  695. else if(declVar.getProp != null)
  696. {
  697. declVar.getProp.location.CallPre(scg, errorAt);
  698. declVar.getProp.location.CallPost(scg, errorAt);
  699. }
  700. else
  701. {
  702. scg.ErrorMsg(errorAt, "property not readable");
  703. scg.PushDefaultValue(declVar.type);
  704. }
  705. }
  706. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  707. {
  708. if((declVar.getProp == null) && (declVar.setProp == null))
  709. {
  710. scg.PushXMRInst();
  711. scg.ilGen.Emit(errorAt, OpCodes.Ldfld, glblVarsFieldInfo);
  712. EmitFieldPushRef(scg, errorAt, declVar);
  713. }
  714. else
  715. {
  716. scg.ErrorMsg(errorAt, "cannot get address of property");
  717. }
  718. }
  719. public override void PopPre(ScriptCodeGen scg, Token errorAt)
  720. {
  721. if((declVar.getProp == null) && (declVar.setProp == null))
  722. {
  723. scg.PushXMRInst();
  724. scg.ilGen.Emit(errorAt, OpCodes.Ldfld, glblVarsFieldInfo);
  725. EmitFieldPopPre(scg, errorAt, declVar);
  726. }
  727. else if(declVar.setProp == null)
  728. {
  729. scg.ErrorMsg(errorAt, "property not writable");
  730. }
  731. }
  732. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  733. {
  734. if((declVar.getProp == null) && (declVar.setProp == null))
  735. {
  736. EmitFieldPopPost(scg, errorAt, declVar);
  737. }
  738. else if(declVar.setProp != null)
  739. {
  740. EmitPopPostProp(scg, errorAt, declVar.type, declVar.setProp.location);
  741. }
  742. else
  743. {
  744. scg.ilGen.Emit(errorAt, OpCodes.Pop);
  745. }
  746. }
  747. // non-trivial because it needs to be copied into a temp
  748. // in case the idiot does dumb-ass side effects tricks
  749. // eg, (x = 0) + x + 2
  750. // should read old value of x not 0
  751. // but if 'xmroption norighttoleft;' in effect,
  752. // we can read it in any order so reading an
  753. // global variable is trivial provided it is
  754. // not a property or the property function is
  755. // trivial.
  756. public override bool IsReadTrivial(ScriptCodeGen scg, Token readAt)
  757. {
  758. return readAt.nr2l && ((declVar.getProp == null) || declVar.getProp.IsFuncTrivial(scg));
  759. }
  760. }
  761. // The value is in an $idxprop property of a script-defined type class or interface instance.
  762. // Reading and writing is via a method call.
  763. public class CompValuIdxProp: CompValu
  764. {
  765. private TokenDeclVar idxProp; // $idxprop property within baseRVal
  766. private CompValu baseRVal; // pointer to class or interface object containing property
  767. private TokenType[] argTypes; // argument types as required by $idxprop declaration
  768. private CompValu[] indices; // actual index values to pass to getter/setter method
  769. private CompValu setProp; // location of setter method
  770. public CompValuIdxProp(TokenDeclVar idxProp, CompValu baseRVal, TokenType[] argTypes, CompValu[] indices) : base(idxProp.type)
  771. {
  772. this.idxProp = idxProp;
  773. this.baseRVal = baseRVal;
  774. this.argTypes = argTypes;
  775. this.indices = indices;
  776. }
  777. /**
  778. * @brief Pushing the property's value is a matter of calling the getter method
  779. * with the supplied argument list as is.
  780. */
  781. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  782. {
  783. if(idxProp.getProp != null)
  784. {
  785. if(!idxProp.getProp.IsFuncTrivial(scg))
  786. {
  787. for(int i = indices.Length; --i >= 0;)
  788. {
  789. indices[i] = scg.Trivialize(indices[i], errorAt);
  790. }
  791. }
  792. CompValu getProp = GetIdxPropMeth(idxProp.getProp);
  793. getProp.CallPre(scg, errorAt);
  794. for(int i = 0; i < indices.Length; i++)
  795. {
  796. indices[i].PushVal(scg, errorAt, argTypes[i]);
  797. }
  798. getProp.CallPost(scg, errorAt);
  799. }
  800. else
  801. {
  802. // write-only property
  803. scg.ErrorMsg(errorAt, "member not readable");
  804. scg.PushDefaultValue(idxProp.type);
  805. }
  806. }
  807. /**
  808. * @brief A property does not have a memory address.
  809. */
  810. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  811. {
  812. scg.ErrorMsg(errorAt, "member has no address");
  813. scg.ilGen.Emit(errorAt, OpCodes.Ldnull);
  814. }
  815. /**
  816. * @brief Preparing to write a property consists of preparing to call the setter method
  817. * then pushing the index arguments.
  818. */
  819. public override void PopPre(ScriptCodeGen scg, Token errorAt)
  820. {
  821. if(idxProp.setProp != null)
  822. {
  823. if(!idxProp.setProp.IsFuncTrivial(scg))
  824. {
  825. for(int i = indices.Length; --i >= 0;)
  826. {
  827. indices[i] = scg.Trivialize(indices[i], errorAt);
  828. }
  829. }
  830. this.setProp = GetIdxPropMeth(idxProp.setProp);
  831. this.setProp.CallPre(scg, errorAt);
  832. for(int i = 0; i < indices.Length; i++)
  833. {
  834. indices[i].PushVal(scg, errorAt, argTypes[i]);
  835. }
  836. }
  837. else
  838. {
  839. // read-only property
  840. scg.ErrorMsg(errorAt, "member not writable");
  841. }
  842. }
  843. /**
  844. * @brief Finishing writing a property consists of finishing the call to the setter method
  845. * now that the value to be written has been pushed by our caller.
  846. */
  847. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  848. {
  849. if(idxProp.setProp != null)
  850. {
  851. this.setProp.CallPost(scg, errorAt);
  852. }
  853. else
  854. {
  855. scg.ilGen.Emit(errorAt, OpCodes.Pop);
  856. }
  857. }
  858. public override bool IsReadTrivial(ScriptCodeGen scg, Token readAt)
  859. {
  860. // if no getter, reading would throw an error, so doesn't really matter what we say
  861. if(idxProp.getProp == null)
  862. return true;
  863. // assume interface methods are always non-trivial because we don't know anything about the actual implementation
  864. if(baseRVal.type is TokenTypeSDTypeInterface)
  865. return false;
  866. // accessing it in any way can't be trivial if reading the pointer isn't trivial
  867. if(!baseRVal.IsReadTrivial(scg, readAt))
  868. return false;
  869. // likewise with the indices
  870. foreach(CompValu idx in indices)
  871. {
  872. if(!idx.IsReadTrivial(scg, readAt))
  873. return false;
  874. }
  875. // now the only way it can be non-trivial to read is if the getter() method itself is non-trivial.
  876. return idxProp.getProp.IsFuncTrivial(scg);
  877. }
  878. /**
  879. * @brief Get how to call the getter or setter method.
  880. */
  881. private CompValu GetIdxPropMeth(TokenDeclVar meth)
  882. {
  883. if(baseRVal.type is TokenTypeSDTypeClass)
  884. {
  885. return new CompValuInstMember(meth, baseRVal, false);
  886. }
  887. return new CompValuIntfMember(meth, baseRVal);
  888. }
  889. }
  890. // This represents the type and location of an internally-defined function
  891. // that a script can call
  892. public class CompValuInline: CompValu
  893. {
  894. public TokenDeclInline declInline;
  895. public CompValuInline(TokenDeclInline declInline) : base(declInline.GetDelType())
  896. {
  897. this.declInline = declInline;
  898. }
  899. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  900. {
  901. scg.ErrorMsg(errorAt, "cannot use built-in for delegate, wrap it");
  902. scg.ilGen.Emit(errorAt, OpCodes.Ldnull);
  903. }
  904. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  905. {
  906. scg.ErrorMsg(errorAt, "cannot use built-in for delegate, wrap it");
  907. scg.ilGen.Emit(errorAt, OpCodes.Ldnull);
  908. }
  909. public override void PopPre(ScriptCodeGen scg, Token errorAt)
  910. {
  911. scg.ErrorMsg(errorAt, "cannot use built-in for delegate, wrap it");
  912. }
  913. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  914. {
  915. scg.ErrorMsg(errorAt, "cannot use built-in for delegate, wrap it");
  916. scg.ilGen.Emit(errorAt, OpCodes.Pop);
  917. }
  918. }
  919. // The value is the entrypoint of a script-defined type's interface method combined with
  920. // the pointer used to access the method. Thus there is one of these per call site.
  921. // They also handle accessing interface properties.
  922. public class CompValuIntfMember: CompValu
  923. {
  924. private TokenDeclVar declVar;
  925. private CompValu baseRVal;
  926. public CompValuIntfMember(TokenDeclVar declVar, CompValu baseRVal) : base(declVar.type)
  927. {
  928. if(this.type == null)
  929. throw new Exception("interface member type is null");
  930. this.declVar = declVar; // which element of the baseRVal vector to be accessed
  931. this.baseRVal = baseRVal; // the vector of delegates implementing the interface
  932. }
  933. /**
  934. * @brief Reading a method's value means getting a delegate to that method.
  935. * Reading a property's value means calling the getter method for that property.
  936. */
  937. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  938. {
  939. if(declVar.retType != null)
  940. {
  941. baseRVal.PushVal(scg, errorAt); // push pointer to delegate array on stack
  942. scg.ilGen.Emit(errorAt, OpCodes.Ldc_I4, declVar.vTableIndex); // select which delegate to access
  943. scg.ilGen.Emit(errorAt, OpCodes.Ldelem, typeof(Delegate)); // push delegate on stack
  944. scg.ilGen.Emit(errorAt, OpCodes.Castclass, type.ToSysType()); // cast to correct delegate class
  945. }
  946. else if(declVar.getProp != null)
  947. {
  948. CompValu getProp = new CompValuIntfMember(declVar.getProp, baseRVal);
  949. getProp.CallPre(scg, errorAt); // reading property, call its getter
  950. getProp.CallPost(scg, errorAt); // ... with no arguments
  951. }
  952. else
  953. {
  954. scg.ErrorMsg(errorAt, "member not readable");
  955. scg.PushDefaultValue(declVar.type);
  956. }
  957. }
  958. /**
  959. * @brief Can't get the address of either a method or a property.
  960. */
  961. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  962. {
  963. scg.ErrorMsg(errorAt, "member has no address");
  964. scg.ilGen.Emit(errorAt, OpCodes.Ldnull);
  965. }
  966. /**
  967. * @brief Can't write a method.
  968. * For property, it means calling the setter method for that property.
  969. */
  970. public override void PopPre(ScriptCodeGen scg, Token errorAt)
  971. {
  972. if(declVar.setProp == null)
  973. {
  974. // read-only property
  975. scg.ErrorMsg(errorAt, "member not writable");
  976. }
  977. }
  978. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  979. {
  980. if(declVar.setProp != null)
  981. {
  982. CompValu setProp = new CompValuIntfMember(declVar.setProp, baseRVal);
  983. EmitPopPostProp(scg, errorAt, declVar.type, setProp);
  984. }
  985. else
  986. {
  987. scg.ilGen.Emit(errorAt, OpCodes.Pop);
  988. }
  989. }
  990. /**
  991. * @brief Reading a method (ie, it's delegate) is always trivial, it's just retrieving
  992. * an element from the delegate array that make up the interface object.
  993. *
  994. * Reading a property is always non-trivial because we don't know which implementation
  995. * the interface is pointing to, so we don't know if it's trivial or not, so assume
  996. * the worst, ie, that it is non-trivial and might call CheckRun().
  997. *
  998. * But all that assumes that locating the interface object in the first place is
  999. * trivial, ie, baseRVal.PushVal() must not call CheckRun() either.
  1000. */
  1001. public override bool IsReadTrivial(ScriptCodeGen scg, Token readAt)
  1002. {
  1003. return baseRVal.IsReadTrivial(scg, readAt) && (declVar.getProp == null);
  1004. }
  1005. /**
  1006. * @brief We just defer to the default CallPre() and CallPost() methods.
  1007. * They expect this.PushVal() to push a delegate to the method to be called.
  1008. * If this member is a method, our PushVal() will read the correct element
  1009. * of the iTable array and push it on the stack, ready for Invoke() to be
  1010. * called. If this member is a property, the only way it can be called is
  1011. * if the property is a delegate, in which case PushVal() will retrieve the
  1012. * delegate by calling the property's getter method.
  1013. */
  1014. }
  1015. // The value is the entrypoint of an internal instance method
  1016. // such as XMR_Array.index()
  1017. public class CompValuIntInstMeth: CompValu
  1018. {
  1019. private TokenTypeSDTypeDelegate delType;
  1020. private CompValu baseRVal;
  1021. private MethodInfo methInfo;
  1022. public CompValuIntInstMeth(TokenTypeSDTypeDelegate delType, CompValu baseRVal, MethodInfo methInfo) : base(delType)
  1023. {
  1024. this.delType = delType;
  1025. this.baseRVal = baseRVal;
  1026. this.methInfo = methInfo;
  1027. }
  1028. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  1029. {
  1030. // its value, ie, without applying the (arglist), is a delegate...
  1031. baseRVal.PushVal(scg, errorAt);
  1032. scg.ilGen.Emit(errorAt, OpCodes.Ldftn, methInfo);
  1033. scg.ilGen.Emit(errorAt, OpCodes.Newobj, delType.decl.GetConstructorInfo());
  1034. }
  1035. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  1036. {
  1037. throw new Exception("cannot get ref to instance method");
  1038. }
  1039. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  1040. {
  1041. throw new Exception("cannot store into instance method");
  1042. }
  1043. public override void CallPre(ScriptCodeGen scg, Token errorAt)
  1044. {
  1045. // internal instance methods are always trivial so never need a CallLabel.
  1046. baseRVal.PushVal(scg, errorAt);
  1047. }
  1048. public override void CallPost(ScriptCodeGen scg, Token errorAt)
  1049. {
  1050. scg.ilGen.Emit(errorAt, OpCodes.Call, methInfo);
  1051. }
  1052. }
  1053. // The value is fetched by calling an internal instance method
  1054. // such as XMR_Array.count
  1055. public class CompValuIntInstROProp: CompValu
  1056. {
  1057. private CompValu baseRVal;
  1058. private MethodInfo methInfo;
  1059. public CompValuIntInstROProp(TokenType valType, CompValu baseRVal, MethodInfo methInfo) : base(valType)
  1060. {
  1061. this.baseRVal = baseRVal;
  1062. this.methInfo = methInfo;
  1063. }
  1064. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  1065. {
  1066. baseRVal.PushVal(scg, errorAt);
  1067. scg.ilGen.Emit(errorAt, OpCodes.Call, methInfo);
  1068. }
  1069. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  1070. {
  1071. scg.ErrorMsg(errorAt, "cannot get ref to read-only property");
  1072. scg.ilGen.Emit(errorAt, OpCodes.Ldnull);
  1073. }
  1074. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  1075. {
  1076. scg.ErrorMsg(errorAt, "cannot store into read-only property");
  1077. scg.ilGen.Emit(errorAt, OpCodes.Pop);
  1078. }
  1079. }
  1080. // The value is in a member of a script-defined type class instance.
  1081. // field: value is in one of the arrays contained within XMRSDTypeClObj.instVars
  1082. // method: value is a delegate; can be called
  1083. // property: reading and writing is via a method call
  1084. public class CompValuInstMember: CompValu
  1085. {
  1086. private static readonly FieldInfo instVarsFieldInfo = typeof(XMRSDTypeClObj).GetField("instVars");
  1087. private static readonly FieldInfo vTableFieldInfo = typeof(XMRSDTypeClObj).GetField("sdtcVTable");
  1088. private TokenDeclVar declVar; // member being accessed
  1089. private CompValu baseRVal; // pointer to particular object instance
  1090. private bool ignoreVirt; // ignore virtual attribute; use declVar's non-virtual method/property
  1091. public CompValuInstMember(TokenDeclVar declVar, CompValu baseRVal, bool ignoreVirt) : base(declVar.type)
  1092. {
  1093. this.declVar = declVar;
  1094. this.baseRVal = baseRVal;
  1095. this.ignoreVirt = ignoreVirt;
  1096. }
  1097. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  1098. {
  1099. if(declVar.retType != null)
  1100. {
  1101. // a method's value, ie, without applying the (arglist), is a delegate...
  1102. PushValMethod(scg, errorAt);
  1103. }
  1104. else if(declVar.vTableArray != null)
  1105. {
  1106. // a field's value is its XMRSDTypeClObj.instVars array element
  1107. baseRVal.PushVal(scg, errorAt);
  1108. scg.ilGen.Emit(errorAt, OpCodes.Ldfld, instVarsFieldInfo);
  1109. EmitFieldPushVal(scg, errorAt, declVar);
  1110. }
  1111. else if(declVar.getProp != null)
  1112. {
  1113. // a property's value is calling its get method with no arguments
  1114. CompValu getProp = new CompValuInstMember(declVar.getProp, baseRVal, ignoreVirt);
  1115. getProp.CallPre(scg, errorAt);
  1116. getProp.CallPost(scg, errorAt);
  1117. }
  1118. else
  1119. {
  1120. // write-only property
  1121. scg.ErrorMsg(errorAt, "member not readable");
  1122. scg.PushDefaultValue(declVar.type);
  1123. }
  1124. }
  1125. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  1126. {
  1127. if(declVar.vTableArray != null)
  1128. {
  1129. // a field's value is its XMRSDTypeClObj.instVars array element
  1130. baseRVal.PushVal(scg, errorAt);
  1131. scg.ilGen.Emit(errorAt, OpCodes.Ldfld, instVarsFieldInfo);
  1132. EmitFieldPushRef(scg, errorAt, declVar);
  1133. }
  1134. else
  1135. {
  1136. scg.ErrorMsg(errorAt, "member has no address");
  1137. scg.ilGen.Emit(errorAt, OpCodes.Ldnull);
  1138. }
  1139. }
  1140. public override void PopPre(ScriptCodeGen scg, Token errorAt)
  1141. {
  1142. if(declVar.vTableArray != null)
  1143. {
  1144. // a field's value is its XMRSDTypeClObj.instVars array element
  1145. baseRVal.PushVal(scg, errorAt);
  1146. scg.ilGen.Emit(errorAt, OpCodes.Ldfld, instVarsFieldInfo);
  1147. EmitFieldPopPre(scg, errorAt, declVar);
  1148. }
  1149. else if(declVar.setProp == null)
  1150. {
  1151. // read-only property
  1152. scg.ErrorMsg(errorAt, "member not writable");
  1153. }
  1154. }
  1155. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  1156. {
  1157. if(declVar.vTableArray != null)
  1158. {
  1159. EmitFieldPopPost(scg, errorAt, declVar);
  1160. }
  1161. else if(declVar.setProp != null)
  1162. {
  1163. CompValu setProp = new CompValuInstMember(declVar.setProp, baseRVal, ignoreVirt);
  1164. EmitPopPostProp(scg, errorAt, declVar.type, setProp);
  1165. }
  1166. else
  1167. {
  1168. scg.ilGen.Emit(errorAt, OpCodes.Pop);
  1169. }
  1170. }
  1171. public override bool IsReadTrivial(ScriptCodeGen scg, Token readAt)
  1172. {
  1173. // accessing it in any way can't be trivial if reading the pointer isn't trivial.
  1174. // this also handles strict right-to-left mode detection as the side-effect can
  1175. // only apply to the pointer (it can't change which field or method we access).
  1176. if(!baseRVal.IsReadTrivial(scg, readAt))
  1177. return false;
  1178. // now the only way it can be non-trivial to read is if it is a property and the
  1179. // getter() method is non-trivial. reading a method means getting a delegate
  1180. // which is always trivial, and reading a simple field is always trivial, ie, no
  1181. // CheckRun() call can possibly be involved.
  1182. if(declVar.retType != null)
  1183. {
  1184. // a method's value, ie, without applying the (arglist), is a delegate...
  1185. return true;
  1186. }
  1187. if(declVar.vTableArray != null)
  1188. {
  1189. // a field's value is its XMRSDTypeClObj.instVars array element
  1190. return true;
  1191. }
  1192. if(declVar.getProp != null)
  1193. {
  1194. // a property's value is calling its get method with no arguments
  1195. return declVar.getProp.IsFuncTrivial(scg);
  1196. }
  1197. // write-only property
  1198. return true;
  1199. }
  1200. public override void CallPre(ScriptCodeGen scg, Token errorAt)
  1201. {
  1202. if(declVar.retType != null)
  1203. {
  1204. CallPreMethod(scg, errorAt);
  1205. }
  1206. else
  1207. {
  1208. base.CallPre(scg, errorAt);
  1209. }
  1210. }
  1211. public override void CallPost(ScriptCodeGen scg, Token errorAt)
  1212. {
  1213. if(declVar.retType != null)
  1214. {
  1215. CallPostMethod(scg, errorAt);
  1216. }
  1217. else
  1218. {
  1219. base.CallPost(scg, errorAt);
  1220. }
  1221. }
  1222. /**
  1223. * @brief A PushVal() for a method means to push a delegate for the method on the stack.
  1224. */
  1225. private void PushValMethod(ScriptCodeGen scg, Token errorAt)
  1226. {
  1227. if((declVar.sdtFlags & ScriptReduce.SDT_STATIC) != 0)
  1228. throw new Exception("dont use for statics");
  1229. if(ignoreVirt || (declVar.vTableIndex < 0))
  1230. {
  1231. /*
  1232. * Non-virtual instance method, create a delegate that references the method.
  1233. */
  1234. string dtn = type.ToString();
  1235. // delegateinstance = (signature)scriptinstance.GetScriptMethodDelegate (methName, signature, arg0);
  1236. // where methName = <sdtclass>.<methname>(<argtypes>)
  1237. // signature = <rettype>(<argtypes>)
  1238. // arg0 = sdt istance (XMRSDTypeClObj) 'this' value
  1239. scg.PushXMRInst(); // [0] scriptinstance
  1240. scg.ilGen.Emit(errorAt, OpCodes.Ldstr, declVar.ilGen.methName); // [1] method name
  1241. scg.ilGen.Emit(errorAt, OpCodes.Ldstr, dtn); // [2] delegate type name
  1242. baseRVal.PushVal(scg, errorAt); // [3] sdtinstance
  1243. scg.ilGen.Emit(errorAt, OpCodes.Callvirt, gsmdMethodInfo); // [0] delegate instance
  1244. scg.ilGen.Emit(errorAt, OpCodes.Castclass, type.ToSysType()); // [0] cast to correct delegate class
  1245. }
  1246. else
  1247. {
  1248. /*
  1249. * Virtual instance method, get the delegate from the vtable.
  1250. */
  1251. baseRVal.PushVal(scg, errorAt); // 'this' selecting the instance
  1252. scg.ilGen.Emit(errorAt, OpCodes.Ldfld, vTableFieldInfo); // get pointer to instance's vtable array
  1253. scg.ilGen.Emit(errorAt, OpCodes.Ldc_I4, declVar.vTableIndex); // select vtable element
  1254. scg.ilGen.Emit(errorAt, OpCodes.Ldelem, typeof(Delegate)); // get delegate pointer = 'this' for 'Invoke()'
  1255. scg.ilGen.Emit(errorAt, OpCodes.Castclass, type.ToSysType()); // cast to correct delegate class
  1256. }
  1257. }
  1258. private void CallPreMethod(ScriptCodeGen scg, Token errorAt)
  1259. {
  1260. if((declVar.sdtFlags & ScriptReduce.SDT_STATIC) != 0)
  1261. throw new Exception("dont use for statics");
  1262. if(!this.declVar.IsFuncTrivial(scg))
  1263. new ScriptCodeGen.CallLabel(scg, errorAt);
  1264. if(ignoreVirt || (declVar.vTableIndex < 0))
  1265. {
  1266. baseRVal.PushVal(scg, errorAt); // 'this' being passed directly to method
  1267. }
  1268. else
  1269. {
  1270. baseRVal.PushVal(scg, errorAt); // 'this' selecting the instance
  1271. scg.ilGen.Emit(errorAt, OpCodes.Ldfld, vTableFieldInfo); // get pointer to instance's vtable array
  1272. scg.ilGen.Emit(errorAt, OpCodes.Ldc_I4, declVar.vTableIndex); // select vtable element
  1273. scg.ilGen.Emit(errorAt, OpCodes.Ldelem, typeof(Delegate)); // get delegate pointer = 'this' for 'Invoke()'
  1274. scg.ilGen.Emit(errorAt, OpCodes.Castclass, type.ToSysType()); // cast to correct delegate class
  1275. }
  1276. }
  1277. private void CallPostMethod(ScriptCodeGen scg, Token errorAt)
  1278. {
  1279. if(ignoreVirt || (declVar.vTableIndex < 0))
  1280. {
  1281. // non-virt instance, just call function directly
  1282. scg.ilGen.Emit(errorAt, OpCodes.Call, declVar.ilGen);
  1283. }
  1284. else
  1285. {
  1286. // virtual, call via delegate Invoke(...) method
  1287. TokenTypeSDTypeDelegate ttd = (TokenTypeSDTypeDelegate)type;
  1288. MethodInfo invokeMethodInfo = ttd.decl.GetInvokerInfo();
  1289. scg.ilGen.Emit(errorAt, OpCodes.Callvirt, invokeMethodInfo);
  1290. }
  1291. if(!this.declVar.IsFuncTrivial(scg))
  1292. scg.openCallLabel = null;
  1293. }
  1294. }
  1295. // The value is an integer constant
  1296. public class CompValuInteger: CompValu
  1297. {
  1298. public int x;
  1299. public CompValuInteger(TokenType type, int x) : base(type)
  1300. {
  1301. if(!(this.type is TokenTypeInt))
  1302. {
  1303. this.type = new TokenTypeInt(this.type);
  1304. }
  1305. this.x = x;
  1306. }
  1307. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  1308. {
  1309. scg.ilGen.Emit(errorAt, OpCodes.Ldc_I4, x);
  1310. }
  1311. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  1312. {
  1313. throw new Exception("cannot get constant's address");
  1314. }
  1315. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  1316. {
  1317. throw new Exception("cannot store into constant");
  1318. }
  1319. }
  1320. // The value is an element of a list
  1321. public class CompValuListEl: CompValu
  1322. {
  1323. private static readonly MethodInfo getElementFromListMethodInfo =
  1324. typeof(CompValuListEl).GetMethod("GetElementFromList", new Type[] { typeof(LSL_List), typeof(int) });
  1325. private CompValu theList;
  1326. private CompValu subscript;
  1327. public CompValuListEl(TokenType type, CompValu theList, CompValu subscript) : base(type)
  1328. {
  1329. this.theList = theList;
  1330. this.subscript = subscript;
  1331. }
  1332. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  1333. {
  1334. theList.PushVal(scg, errorAt, new TokenTypeList(type));
  1335. subscript.PushVal(scg, errorAt, new TokenTypeInt(type));
  1336. scg.ilGen.Emit(errorAt, OpCodes.Call, getElementFromListMethodInfo);
  1337. }
  1338. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  1339. {
  1340. throw new Exception("cannot get list element's address");
  1341. }
  1342. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  1343. {
  1344. scg.ErrorMsg(errorAt, "cannot store into list element");
  1345. scg.ilGen.Emit(errorAt, OpCodes.Pop);
  1346. }
  1347. public static object GetElementFromList(LSL_List lis, int idx)
  1348. {
  1349. object element = lis.Data[idx];
  1350. if(element is LSL_Float)
  1351. return TypeCast.EHArgUnwrapFloat(element);
  1352. if(element is LSL_Integer)
  1353. return TypeCast.EHArgUnwrapInteger(element);
  1354. if(element is LSL_String)
  1355. return TypeCast.EHArgUnwrapString(element);
  1356. if(element is OpenMetaverse.Quaternion)
  1357. return TypeCast.EHArgUnwrapRotation(element);
  1358. if(element is OpenMetaverse.Vector3)
  1359. return TypeCast.EHArgUnwrapVector(element);
  1360. return element;
  1361. }
  1362. }
  1363. // The value is kept in a script-addressable local variable
  1364. public class CompValuLocalVar: CompValu
  1365. {
  1366. private static int htpopseq = 0;
  1367. private ScriptMyLocal localBuilder;
  1368. public CompValuLocalVar(TokenType type, string name, ScriptCodeGen scg) : base(type)
  1369. {
  1370. if(type.ToHeapTrackerType() != null)
  1371. {
  1372. localBuilder = scg.ilGen.DeclareLocal(type.ToHeapTrackerType(), name);
  1373. scg.PushXMRInst();
  1374. scg.ilGen.Emit(type, OpCodes.Newobj, type.GetHeapTrackerCtor());
  1375. scg.ilGen.Emit(type, OpCodes.Stloc, localBuilder);
  1376. }
  1377. else
  1378. {
  1379. this.localBuilder = scg.ilGen.DeclareLocal(ToSysType(), name);
  1380. }
  1381. }
  1382. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  1383. {
  1384. scg.ilGen.Emit(errorAt, OpCodes.Ldloc, localBuilder);
  1385. if(type.ToHeapTrackerType() != null)
  1386. {
  1387. type.CallHeapTrackerPushMeth(errorAt, scg.ilGen);
  1388. }
  1389. }
  1390. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  1391. {
  1392. if(type.ToHeapTrackerType() != null)
  1393. {
  1394. scg.ErrorMsg(errorAt, "can't take ref of heap-tracked type " + type.ToString());
  1395. scg.ilGen.Emit(errorAt, OpCodes.Ldnull);
  1396. }
  1397. else
  1398. {
  1399. scg.ilGen.Emit(errorAt, OpCodes.Ldloca, localBuilder);
  1400. }
  1401. }
  1402. public override void PopPre(ScriptCodeGen scg, Token errorAt)
  1403. {
  1404. if(type.ToHeapTrackerType() != null)
  1405. {
  1406. scg.ilGen.Emit(errorAt, OpCodes.Ldloc, localBuilder);
  1407. }
  1408. }
  1409. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  1410. {
  1411. if(type.ToHeapTrackerType() != null)
  1412. {
  1413. type.CallHeapTrackerPopMeth(errorAt, scg.ilGen);
  1414. }
  1415. else
  1416. {
  1417. scg.ilGen.Emit(errorAt, OpCodes.Stloc, localBuilder);
  1418. }
  1419. }
  1420. public void Pop(ScriptCodeGen scg, Token errorAt)
  1421. {
  1422. if(type.ToHeapTrackerType() != null)
  1423. {
  1424. /*
  1425. * Popping into a heap tracker wrapped local variable.
  1426. * First pop value into a temp var, then call the heap tracker's pop method.
  1427. */
  1428. ScriptMyLocal htpop = scg.ilGen.DeclareLocal(type.ToSysType(), "htpop$" + (++htpopseq).ToString());
  1429. scg.ilGen.Emit(errorAt, OpCodes.Stloc, htpop);
  1430. scg.ilGen.Emit(errorAt, OpCodes.Ldloc, localBuilder);
  1431. scg.ilGen.Emit(errorAt, OpCodes.Ldloc, htpop);
  1432. type.CallHeapTrackerPopMeth(errorAt, scg.ilGen);
  1433. }
  1434. else
  1435. {
  1436. /*
  1437. * Not a heap-tracked local var, just pop directly into it.
  1438. */
  1439. scg.ilGen.Emit(errorAt, OpCodes.Stloc, localBuilder);
  1440. }
  1441. }
  1442. // non-trivial because it needs to be copied into a temp
  1443. // in case the idiot does dumb-ass side effects tricks
  1444. // eg, (x = 0) + x + 2
  1445. // should read old value of x not 0
  1446. // but if 'xmroption norighttoleft;' in effect,
  1447. // we can read it in any order so reading a
  1448. // local variable is trivial.
  1449. public override bool IsReadTrivial(ScriptCodeGen scg, Token readAt)
  1450. {
  1451. return readAt.nr2l;
  1452. }
  1453. }
  1454. // The value is a null
  1455. public class CompValuNull: CompValu
  1456. {
  1457. public CompValuNull(TokenType type) : base(type) { }
  1458. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  1459. {
  1460. scg.ilGen.Emit(errorAt, OpCodes.Ldnull);
  1461. }
  1462. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  1463. {
  1464. throw new Exception("cannot get null's address");
  1465. }
  1466. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  1467. {
  1468. throw new Exception("cannot store into null");
  1469. }
  1470. }
  1471. // The value is a rotation
  1472. public class CompValuRot: CompValu
  1473. {
  1474. public CompValu x;
  1475. public CompValu y;
  1476. public CompValu z;
  1477. public CompValu w;
  1478. private static readonly ConstructorInfo lslRotConstructorInfo =
  1479. typeof(LSL_Rotation).GetConstructor(new Type[] { typeof (double),
  1480. typeof (double),
  1481. typeof (double),
  1482. typeof (double) });
  1483. public CompValuRot(TokenType type, CompValu x, CompValu y, CompValu z, CompValu w) :
  1484. base(type)
  1485. {
  1486. if(!(type is TokenTypeRot))
  1487. {
  1488. this.type = new TokenTypeRot(type);
  1489. }
  1490. this.x = x;
  1491. this.y = y;
  1492. this.z = z;
  1493. this.w = w;
  1494. }
  1495. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  1496. {
  1497. this.x.PushVal(scg, errorAt, new TokenTypeFloat(this.x.type));
  1498. this.y.PushVal(scg, errorAt, new TokenTypeFloat(this.y.type));
  1499. this.z.PushVal(scg, errorAt, new TokenTypeFloat(this.z.type));
  1500. this.w.PushVal(scg, errorAt, new TokenTypeFloat(this.w.type));
  1501. scg.ilGen.Emit(errorAt, OpCodes.Newobj, lslRotConstructorInfo);
  1502. }
  1503. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  1504. {
  1505. throw new Exception("cannot get constant's address");
  1506. }
  1507. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  1508. {
  1509. throw new Exception("cannot store into constant");
  1510. }
  1511. public override bool IsReadTrivial(ScriptCodeGen scg, Token readAt)
  1512. {
  1513. // the supplied values must be trivial because when we call their PushVal()s
  1514. // there will be stuff on the stack for all but the first PushVal() and so
  1515. // they would have a non-empty stack at their call label.
  1516. if(!this.w.IsReadTrivial(scg, readAt) ||
  1517. !this.x.IsReadTrivial(scg, readAt) ||
  1518. !this.y.IsReadTrivial(scg, readAt) ||
  1519. !this.z.IsReadTrivial(scg, readAt))
  1520. {
  1521. throw new Exception("rotation values must be trivial");
  1522. }
  1523. return true;
  1524. }
  1525. }
  1526. // The value is in a static field of an internally defined struct/class
  1527. public class CompValuSField: CompValu
  1528. {
  1529. public FieldInfo field;
  1530. public CompValuSField(TokenType type, FieldInfo field) : base(type)
  1531. {
  1532. this.field = field;
  1533. }
  1534. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  1535. {
  1536. if((field.Attributes & FieldAttributes.Literal) == 0)
  1537. {
  1538. scg.ilGen.Emit(errorAt, OpCodes.Ldsfld, field);
  1539. return;
  1540. }
  1541. if(field.FieldType == typeof(LSL_Rotation))
  1542. {
  1543. LSL_Rotation rot = (LSL_Rotation)field.GetValue(null);
  1544. scg.ilGen.Emit(errorAt, OpCodes.Ldc_R8, rot.x);
  1545. scg.ilGen.Emit(errorAt, OpCodes.Ldc_R8, rot.y);
  1546. scg.ilGen.Emit(errorAt, OpCodes.Ldc_R8, rot.z);
  1547. scg.ilGen.Emit(errorAt, OpCodes.Ldc_R8, rot.s);
  1548. scg.ilGen.Emit(errorAt, OpCodes.Newobj, ScriptCodeGen.lslRotationConstructorInfo);
  1549. return;
  1550. }
  1551. if(field.FieldType == typeof(LSL_Vector))
  1552. {
  1553. LSL_Vector vec = (LSL_Vector)field.GetValue(null);
  1554. scg.ilGen.Emit(errorAt, OpCodes.Ldc_R8, vec.x);
  1555. scg.ilGen.Emit(errorAt, OpCodes.Ldc_R8, vec.y);
  1556. scg.ilGen.Emit(errorAt, OpCodes.Ldc_R8, vec.z);
  1557. scg.ilGen.Emit(errorAt, OpCodes.Newobj, ScriptCodeGen.lslRotationConstructorInfo);
  1558. return;
  1559. }
  1560. if(field.FieldType == typeof(string))
  1561. {
  1562. string str = (string)field.GetValue(null);
  1563. scg.ilGen.Emit(errorAt, OpCodes.Ldstr, str);
  1564. return;
  1565. }
  1566. throw new Exception("unsupported literal type " + field.FieldType.Name);
  1567. }
  1568. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  1569. {
  1570. if((field.Attributes & FieldAttributes.Literal) != 0)
  1571. {
  1572. throw new Exception("can't write a constant");
  1573. }
  1574. scg.ilGen.Emit(errorAt, OpCodes.Ldflda, field);
  1575. }
  1576. public override void PopPre(ScriptCodeGen scg, Token errorAt)
  1577. {
  1578. }
  1579. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  1580. {
  1581. if((field.Attributes & FieldAttributes.Literal) != 0)
  1582. {
  1583. throw new Exception("can't write a constant");
  1584. }
  1585. scg.ilGen.Emit(errorAt, OpCodes.Stsfld, field);
  1586. }
  1587. // non-trivial because it needs to be copied into a temp
  1588. // in case the idiot does dumb-ass side effects tricks
  1589. // eg, (x = 0) + x + 2
  1590. // should read old value of x not 0
  1591. // but if 'xmroption norighttoleft;' in effect,
  1592. // we can read it in any order so reading a
  1593. // local variable is trivial.
  1594. public override bool IsReadTrivial(ScriptCodeGen scg, Token readAt)
  1595. {
  1596. return readAt.nr2l;
  1597. }
  1598. }
  1599. // The value is a character within a string
  1600. public class CompValuStrChr: CompValu
  1601. {
  1602. private static readonly MethodInfo getCharFromStringMethodInfo =
  1603. typeof(CompValuStrChr).GetMethod("GetCharFromString", new Type[] { typeof(string), typeof(int) });
  1604. private CompValu theString;
  1605. private CompValu subscript;
  1606. public CompValuStrChr(TokenType type, CompValu theString, CompValu subscript) : base(type)
  1607. {
  1608. this.theString = theString;
  1609. this.subscript = subscript;
  1610. }
  1611. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  1612. {
  1613. theString.PushVal(scg, errorAt, new TokenTypeStr(type));
  1614. subscript.PushVal(scg, errorAt, new TokenTypeInt(type));
  1615. scg.ilGen.Emit(errorAt, OpCodes.Call, getCharFromStringMethodInfo);
  1616. }
  1617. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  1618. {
  1619. throw new Exception("cannot get string character's address");
  1620. }
  1621. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  1622. {
  1623. scg.ErrorMsg(errorAt, "cannot store into string character");
  1624. scg.ilGen.Emit(errorAt, OpCodes.Pop);
  1625. }
  1626. public static char GetCharFromString(string s, int i)
  1627. {
  1628. return s[i];
  1629. }
  1630. }
  1631. // The value is a key or string constant
  1632. public class CompValuString: CompValu
  1633. {
  1634. public string x;
  1635. public CompValuString(TokenType type, string x) : base(type)
  1636. {
  1637. if(!(type is TokenTypeKey) && !(this.type is TokenTypeStr))
  1638. {
  1639. throw new Exception("bad type " + type.ToString());
  1640. }
  1641. this.x = x;
  1642. }
  1643. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  1644. {
  1645. scg.ilGen.Emit(errorAt, OpCodes.Ldstr, x);
  1646. }
  1647. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  1648. {
  1649. throw new Exception("cannot get constant's address");
  1650. }
  1651. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  1652. {
  1653. throw new Exception("cannot store into constant");
  1654. }
  1655. }
  1656. // The value is kept in a temp local variable
  1657. public class CompValuTemp: CompValu
  1658. {
  1659. public ScriptMyLocal localBuilder;
  1660. public CompValuTemp(TokenType type, ScriptCodeGen scg) : base(type)
  1661. {
  1662. string name = "tmp$" + (++scg.tempCompValuNum);
  1663. this.localBuilder = scg.ilGen.DeclareLocal(ToSysType(), name);
  1664. }
  1665. protected CompValuTemp(TokenType type) : base(type) { } // CompValuVoid uses this
  1666. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  1667. {
  1668. scg.ilGen.Emit(errorAt, OpCodes.Ldloc, localBuilder);
  1669. }
  1670. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  1671. {
  1672. scg.ilGen.Emit(errorAt, OpCodes.Ldloca, localBuilder);
  1673. }
  1674. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  1675. {
  1676. scg.ilGen.Emit(errorAt, OpCodes.Stloc, localBuilder);
  1677. }
  1678. public void Pop(ScriptCodeGen scg, Token errorAt, TokenType stackType)
  1679. {
  1680. TypeCast.CastTopOfStack(scg, errorAt, stackType, this.type, false);
  1681. this.PopPost(scg, errorAt); // in case PopPost() overridden eg by CompValuVoid
  1682. }
  1683. public void Pop(ScriptCodeGen scg, Token errorAt)
  1684. {
  1685. this.PopPost(scg, errorAt); // in case PopPost() overridden eg by CompValuVoid
  1686. }
  1687. }
  1688. // The value is a vector
  1689. public class CompValuVec: CompValu
  1690. {
  1691. public CompValu x;
  1692. public CompValu y;
  1693. public CompValu z;
  1694. private static readonly ConstructorInfo lslVecConstructorInfo =
  1695. typeof(LSL_Vector).GetConstructor(new Type[] { typeof (double),
  1696. typeof (double),
  1697. typeof (double) });
  1698. public CompValuVec(TokenType type, CompValu x, CompValu y, CompValu z) : base(type)
  1699. {
  1700. if(!(type is TokenTypeVec))
  1701. {
  1702. this.type = new TokenTypeVec(type);
  1703. }
  1704. this.x = x;
  1705. this.y = y;
  1706. this.z = z;
  1707. }
  1708. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  1709. {
  1710. this.x.PushVal(scg, errorAt, new TokenTypeFloat(this.x.type));
  1711. this.y.PushVal(scg, errorAt, new TokenTypeFloat(this.y.type));
  1712. this.z.PushVal(scg, errorAt, new TokenTypeFloat(this.z.type));
  1713. scg.ilGen.Emit(errorAt, OpCodes.Newobj, lslVecConstructorInfo);
  1714. }
  1715. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  1716. {
  1717. throw new Exception("cannot get constant's address");
  1718. }
  1719. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  1720. {
  1721. throw new Exception("cannot store into constant");
  1722. }
  1723. public override bool IsReadTrivial(ScriptCodeGen scg, Token readAt)
  1724. {
  1725. // the supplied values must be trivial because when we call their PushVal()s
  1726. // there will be stuff on the stack for all but the first PushVal() and so
  1727. // they would have a non-empty stack at their call label.
  1728. if(!this.x.IsReadTrivial(scg, readAt) ||
  1729. !this.y.IsReadTrivial(scg, readAt) ||
  1730. !this.z.IsReadTrivial(scg, readAt))
  1731. {
  1732. throw new Exception("vector values must be trivial");
  1733. }
  1734. return true;
  1735. }
  1736. }
  1737. // Used to indicate value will be discarded (eg, where to put return value from a call)
  1738. public class CompValuVoid: CompValuTemp
  1739. {
  1740. public CompValuVoid(Token token) : base((token is TokenTypeVoid) ? (TokenTypeVoid)token : new TokenTypeVoid(token))
  1741. {
  1742. }
  1743. public override void PushVal(ScriptCodeGen scg, Token errorAt)
  1744. {
  1745. }
  1746. public override void PushRef(ScriptCodeGen scg, Token errorAt)
  1747. {
  1748. throw new Exception("cannot get void address");
  1749. }
  1750. public override void PopPost(ScriptCodeGen scg, Token errorAt)
  1751. {
  1752. }
  1753. }
  1754. }