MMRScriptCompValu.cs 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  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. this.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. }