public static void SwapValues(this ArrayRef array, int index1, int index2) { int temp = array[index1]; array[index1] = array[index2]; array[index2] = temp; }
public static TString newlstr(lua_State L, CharPtr str, uint l, uint h) { uint totalsize; /* total size of TString object */ GCObjectRef list; /* (pointer to) list where it will be inserted */ TString ts; stringtable tb = G(L).strt; if (l + 1 > MAX_SIZET / GetUnmanagedSize(typeof(char))) { luaM_toobig(L); } if ((tb.nuse > (int)tb.size) && (tb.size <= MAX_INT / 2)) { luaS_resize(L, tb.size * 2); /* too crowded */ } totalsize = (uint)(GetUnmanagedSize(typeof(TString)) + ((l + 1) * GetUnmanagedSize(typeof(char)))); //FIXME:(uint) list = new ArrayRef(tb.hash, (int)lmod(h, tb.size)); //FIXME:(int) ts = luaC_newobj <TString>(L, LUA_TSTRING, totalsize, list, 0).ts; ts.tsv.len = l; ts.tsv.hash = h; ts.tsv.reserved = 0; //memcpy(ts+1, str, l*GetUnmanagedSize(typeof(char))); memcpy(ts.str.chars, str.chars, str.index, (int)l); //FIXME:changed ts.str[l] = '\0'; /* ending 0 */ tb.nuse++; return(ts); }
public static MethodArgStack Array(ArrayRef a) { return(new MethodArgStack() { type = StackItemType.Array, value = (int)a.Index }); }
private static void MergeSorted(int[] array, ArrayRef left, ArrayRef right, int[] buffer) { ArrayRef merged = new ArrayRef(buffer, left.AbsoluteStartIndex, left.Length + right.Length); int leftCurrent = 0; int rightCurrent = 0; for (int i = 0; i < merged.Length; i++) { bool outOfLeft = !(leftCurrent < left.Length); bool outOfRight = !(rightCurrent < right.Length); if (!outOfLeft && (outOfRight || left[leftCurrent] < right[rightCurrent])) { merged[i] = left[leftCurrent]; leftCurrent++; } else { merged[i] = right[rightCurrent]; rightCurrent++; } } int index = merged.Length - 1; for (int i = right.AbsoluteEndIndex - 1; i >= left.AbsoluteStartIndex; i--, index--) { array[i] = merged[index]; } }
public DataType VisitArrayRef(ArrayRef s) { DataType vt = s.array.Accept(this); DataType st; if (s.subs == null || s.subs.Count == 0) { st = null; } else if (s.subs.Count == 1) { st = s.subs[0].Accept(this); } else { st = VisitExtSlice(s.subs); } if (vt is UnionType) { DataType retType = DataType.Unknown; foreach (DataType t in ((UnionType)vt).types) { retType = UnionType.Union(retType, getSubscript(s, t, st)); } return(retType); } else { return(getSubscript(s, vt, st)); } }
public CodeExpression VisitArrayRef(ArrayRef aref) { var target = aref.array.Accept(this); var subs = aref.subs .Select(s => { if (s.upper != null || s.step != null) { return(new CodeVariableReferenceExpression(string.Format( "{0}:{1}:{2}", s.lower, s.upper, s.step))); } if (s.lower != null) { return(s.lower.Accept(this)); } else if (s.upper != null) { return(s.upper.Accept(this)); } else if (s.step != null) { return(s.step.Accept(this)); } else { return(m.Prim(":")); } }) .ToArray(); return(m.Aref(target, subs)); }
/// <summary> /// Applied before Unsubscribe runs. /// </summary> internal static void Prefix(ArrayRef <IntraObjectRoute> ___intraObjectRoutes, int eventName, int subscribeHandle, bool suppressWarnings) { if (!suppressWarnings && ___intraObjectRoutes.FindIndex((route) => route. eventHash == eventName && route.handlerIndex == subscribeHandle) < 0) { DebugLogger.DumpStack(); } }
public void VisitArrayRef(ArrayRef arrayRef) { Resolve(arrayRef.ArrayExpr); foreach (var idx in arrayRef.Indices) { Resolve(idx); } _result = arrayRef; }
/// <summary> /// Visits an array reference literal. The default implementation saves it to <c>_tlit</c>. /// </summary> /// <param name="arrayRef">array reference literal.</param> public virtual void VisitArrayRef(ArrayRef arrayRef) { var newRef = new ArrayRef( arrayRef.ArrayExpr.Transform(this), arrayRef.Type, arrayRef.Indices.Select(i => i.Transform(this)).ToArray()); newRef.CopyAttributesFrom(arrayRef); _tlit = newRef; }
private object ComputeAssign(BinaryExpr t, IEnvironment env, object rvalue) { ASTree l = t.Left; if (l is Name) { env.Put((l as Name).Text, rvalue); return(rvalue); } else if (l is PrimaryExpr) { PrimaryExpr p = l as PrimaryExpr; if (p.HasPostfix(0) && (p.Postfix(0) is Dot)) { p.Accept(this, env, 1); object tp = result; if (result is ErrorValue) { return(result); } if (tp is RavObject) { return(SetField(t, (tp as RavObject), (p.Postfix(0) as Dot), rvalue)); } } else if (p.HasPostfix(0) && (p.Postfix(0) is ArrayRef)) { p.Accept(this, env, 1); object a = result; if (result is ErrorValue) { return(result); } if (a is object[]) { ArrayRef aref = p.Postfix(0) as ArrayRef; aref.Index.Accept(this, env); if (result is ErrorValue) { return(result); } object index = result; if (index is int) { (a as object[])[(int)index] = rvalue; return(rvalue); } } result = new ErrorValue("bad array access", t); return(result); } } return(new ErrorValue("bad assignment", t)); }
/* ** creates a new short string, inserting it into string table */ static TString newshrstr(lua_State L, CharPtr str, uint l, uint h) { GCObjectRef list; /* (pointer to) list where it will be inserted */ stringtable tb = G(L).strt; TString s; if (tb.nuse >= (lu_int32)(tb.size) && tb.size <= MAX_INT / 2) { luaS_resize(L, tb.size * 2); /* too crowded */ } list = new ArrayRef(tb.hash, (int)lmod(h, tb.size)); s = createstrobj(L, str, l, LUA_TSHRSTR, h, list); tb.nuse++; return(s); }
public void Visit(ArrayRef t, IEnvironment env, object value) { if (value is object[]) { t.Index.Accept(this, env); if (result is ErrorValue) { return; } object index = result; if (index is int) { result = (value as object[])[(int)index]; return; } } result = new ErrorValue("bad array access", t); }
private static void AnalyzeArray(ArrayRef array, out double pivot, out int leftCount, out bool hasDistincts) { int max = int.MinValue; int min = int.MaxValue; for (int i = array.Length - 1; i >= 0; i--) { max = Math.Max(max, array[i]); min = Math.Min(min, array[i]); } pivot = 1d * min / 2 + 1d * max / 2; hasDistincts = min != max; leftCount = 0; for (int i = array.Length - 1; i >= 0; i--) { leftCount += array[i] <= pivot ? 1 : 0; } }
private static void Sort(ArrayRef array, int[] buffer) { if (array.Length == 0) { return; } if (array.Length == 1) { return; } if (array.Length == 2) { if (array[0] > array[1]) { array.SwapValues(0, 1); } return; } double pivot; bool hasDistincts; int leftCount; AnalyzeArray(array, out pivot, out leftCount, out hasDistincts); if (!hasDistincts) { return; } ArrayRef left; ArrayRef right; ReorganizeArrays(array, buffer, pivot, leftCount, out left, out right); Sort(left, buffer); Sort(right, buffer); }
public DataType getSubscript(ArrayRef s, DataType vt, DataType st) { if (vt.isUnknownType()) { return(DataType.Unknown); } else if (vt is ListType) { return(getListSubscript(s, vt, st)); } else if (vt is TupleType) { return(getListSubscript(s, ((TupleType)vt).toListType(), st)); } else if (vt is DictType) { DictType dt = (DictType)vt; if (!dt.keyType.Equals(st)) { AddWarning(s, "Possible KeyError (wrong type for subscript)"); } return(((DictType)vt).valueType); } else if (vt == DataType.Str) { if (st != null && (st is ListType || st.isNumType())) { return(vt); } else { AddWarning(s, "Possible KeyError (wrong type for subscript)"); return(DataType.Unknown); } } else { return(DataType.Unknown); } }
/// <summary> /// Bind a name to this scope, including destructuring assignment. /// </summary> public void Bind(Analyzer analyzer, Exp target, DataType rvalue, BindingKind kind) { if (target is Identifier) { this.Bind(analyzer, (Identifier)target, rvalue, kind); } else if (target is PyTuple) { this.Bind(analyzer, ((PyTuple)target).values, rvalue, kind); } else if (target is PyList) { this.Bind(analyzer, ((PyList)target).elts, rvalue, kind); } else if (target is AttributeAccess) { var attr = (AttributeAccess)target; DataType targetType = transformExpr(analyzer, attr.Expression, this); setAttr(analyzer, attr, rvalue, targetType); } else if (target is ArrayRef) { ArrayRef sub = (ArrayRef)target; DataType valueType = transformExpr(analyzer, sub.array, this); var xform = new TypeTransformer(this, analyzer); transformExprs(analyzer, sub.subs, this); if (valueType is ListType) { ListType t = (ListType)valueType; t.setElementType(UnionType.Union(t.eltType, rvalue)); } } else if (target != null) { analyzer.putProblem(target, "invalid location for assignment"); } }
private static void ReorganizeArrays(ArrayRef array, int[] buffer, double pivot, int leftCount, out ArrayRef left, out ArrayRef right) { left = array.SubArray(0, leftCount); right = array.SubArray(leftCount, array.Length - leftCount); array.FlushTo(buffer); int leftIndex = left.Length - 1; int rightIndex = right.Length - 1; for (int i = array.Length - 1; i >= 0; i--) { int item = buffer[i]; if (item <= pivot) { left[leftIndex--] = item; } else { right[rightIndex--] = item; } } }
private static void Sort(int[] array, ArrayRef arrayRef, int[] buffer) { if (arrayRef.Length == 1) { return; } if (arrayRef.Length == 2) { if (arrayRef[1] < arrayRef[0]) { array.SwapValues(arrayRef.AbsoluteStartIndex + 0, arrayRef.AbsoluteStartIndex + 1); } return; } int half = arrayRef.Length / 2; ArrayRef left = new ArrayRef(array, arrayRef.AbsoluteStartIndex, half); ArrayRef right = new ArrayRef(array, arrayRef.AbsoluteStartIndex + half, arrayRef.Length - half); bool shouldSortInParallel = right.Length > 2048; if (shouldSortInParallel) { Task sortRightTask = _taskFactory.StartNew(() => Sort(array, right, buffer)); Sort(array, left, buffer); Task.WaitAll(sortRightTask); } else { Sort(array, right, buffer); Sort(array, left, buffer); } MergeSorted(array, left, right, buffer); }
protected override void DeclareAlgorithm() { var srClk = SignalRef.Create(_taSite._clk, SignalRef.EReferencedProperty.RisingEdge); var lrClk = new LiteralReference(srClk); var srWrEn = _taSite.NeedWriteAccess ? SignalRef.Create(_taSite._wrEn, SignalRef.EReferencedProperty.Cur) : null; var lrWrEn = _taSite.NeedWriteAccess ? new LiteralReference(srWrEn) : null; var srAddr = SignalRef.Create(_taSite._addr, SignalRef.EReferencedProperty.Cur); var lrAddr = new LiteralReference(srAddr); var srDataIn = _taSite.NeedWriteAccess ? SignalRef.Create(_taSite._dataIn, SignalRef.EReferencedProperty.Cur) : null; var lrDataIn = _taSite.NeedWriteAccess ? new LiteralReference(srDataIn) : null; var srDataOut = SignalRef.Create(_taSite._dataOut, SignalRef.EReferencedProperty.Next); var hi = LiteralReference.CreateConstant(StdLogic._1); var addrUType = TypeDescriptor.GetTypeOf(((StdLogicVector)_taSite._addr.InitialValue).UnsignedValue); var uAddr = IntrinsicFunctions.Cast(lrAddr, typeof(StdLogicVector), addrUType); var iAddr = IntrinsicFunctions.Cast(uAddr, addrUType.CILType, typeof(int)); var array = _taSite._array; var lrArray = new LiteralReference(array.ArrayLit); var elemType = array.ElementType; var aref = new ArrayRef(lrArray, elemType, iAddr); var convDataIn = _taSite.NeedWriteAccess ? IntrinsicFunctions.Cast(lrDataIn, typeof(StdLogicVector), elemType) : null; var convAref = IntrinsicFunctions.Cast(aref, elemType.CILType, _taSite._dataOut.ElementType); If(lrClk); { Store(srDataOut, convAref); if (_taSite.NeedWriteAccess) { If(Expression.Equal(lrWrEn, hi)); { Store(aref, convDataIn); } EndIf(); } } EndIf(); }
private DataType getListSubscript(ArrayRef s, DataType vt, DataType st) { if (vt is ListType) { if (st != null && st is ListType) { return(vt); } else if (st == null || st.isNumType()) { return(((ListType)vt).eltType); } else { DataType sliceFunc = vt.Table.LookupAttributeType("__getslice__"); if (sliceFunc == null) { AddError(s, "The type can't be sliced: " + vt); return(DataType.Unknown); } else if (sliceFunc is FunType) { return(Apply(analyzer, (FunType)sliceFunc, null, null, null, null, s)); } else { AddError(s, "The type's __getslice__ method is not a function: " + sliceFunc); return(DataType.Unknown); } } } else { return(DataType.Unknown); } }
/// <summary> /// Visits an array reference literal. The default implementation saves it to <c>_tlit</c>. /// </summary> /// <param name="arrayRef">array reference literal.</param> public virtual void VisitArrayRef(ArrayRef arrayRef) { var newRef = new ArrayRef( arrayRef.ArrayExpr.Transform(this), arrayRef.Type, arrayRef.Indices.Select(i => i.Transform(this)).ToArray()); newRef.CopyAttributesFrom(arrayRef); _tlit = newRef; }
public void VisitArrayRef(ArrayRef arrayRef) { throw new NotSupportedException(); }
public void VisitArrayRef(ArrayRef arrayRef) { LiteralReference lr = arrayRef.ArrayExpr as LiteralReference; object arrayObj; if (lr == null || !lr.ReferencedObject.IsConst(out arrayObj)) { throw new NotSupportedException("Array references must be fixed (referenced array must be known in advance)!"); } Array array = arrayObj as Array; if (array == null) { throw new InvalidOperationException("Not an array"); } ELiteralAcceptMode saveMode = _litMode; _litMode = ELiteralAcceptMode.Read; FixedArrayRef far = arrayRef.AsFixed(); if (!far.IndicesConst) { foreach (Expression index in arrayRef.Indices) { index.Accept(this); } } _litMode = saveMode; int[] preds; switch (_litMode) { case ELiteralAcceptMode.Read: if (far.IndicesConst) { ArrayMod amod1 = new ArrayMod(array, far.Indices); ArrayMod amod2 = new ArrayMod(array, ArrayMod.EMode.UnknownIndex); ArrayMod amod3 = new ArrayMod(array, ArrayMod.EMode.AnyIndex); preds = _storesInCurBB.Get(amod1) .Union(_storesInCurBB.Get(amod2)) .Union(_storesInCurBB.Get(amod3)) .Union(_readsInCurBB.Get(amod3)) .Distinct() .ToArray(); _readsInCurBB.Add(amod1, NextInstructionIndex); _readsInCurBB.Add(amod3, NextInstructionIndex); Emit(ISet.LdelemFixAFixI(far), preds, 0, arrayRef.Type); } else { ArrayMod amod1 = new ArrayMod(array, ArrayMod.EMode.UnknownIndex); ArrayMod amod2 = new ArrayMod(array, ArrayMod.EMode.AnyIndex); preds = _storesInCurBB.Get(amod1) .Union(_storesInCurBB.Get(amod2)) .Union(_readsInCurBB.Get(amod2)) .Distinct() .ToArray(); _readsInCurBB.Add(amod1, NextInstructionIndex); Emit(ISet.LdelemFixA(far), preds, arrayRef.Indices.Length, arrayRef.Type); } break; case ELiteralAcceptMode.Write: if (far.IndicesConst) { ArrayMod amod1 = new ArrayMod(array, far.Indices); ArrayMod amod2 = new ArrayMod(array, ArrayMod.EMode.UnknownIndex); ArrayMod amod3 = new ArrayMod(array, ArrayMod.EMode.AnyIndex); preds = _storesInCurBB.Get(amod1) .Union(_storesInCurBB.Get(amod3)) .Union(_storesInCurBB.Get(amod2)) .Union(_readsInCurBB.Get(amod1)) .Union(_readsInCurBB.Get(amod2)) .Union(_readsInCurBB.Get(amod3)) .Distinct() .ToArray(); _storesInCurBB.Add(amod1, NextInstructionIndex); _storesInCurBB.Add(amod3, NextInstructionIndex); Emit(ISet.StelemFixAFixI(far), preds, 1); } else { ArrayMod amod1 = new ArrayMod(array, ArrayMod.EMode.UnknownIndex); ArrayMod amod2 = new ArrayMod(array, ArrayMod.EMode.AnyIndex); preds = _storesInCurBB.Get(amod1) .Union(_storesInCurBB.Get(amod2)) .Union(_readsInCurBB.Get(amod1)) .Union(_readsInCurBB.Get(amod2)) .Distinct() .ToArray(); _storesInCurBB.Add(amod1, NextInstructionIndex); Emit(ISet.StelemFixA(far), preds, 1 + arrayRef.Indices.Length); } break; } }
public void VisitArrayRef(ArrayRef arrayRef) { throw new NotImplementedException(); }
public void VisitArrayRef(ArrayRef arrayRef) { throw new NotImplementedException(); }
public object DefaultEvalArrayRef(ArrayRef arrayRef) { return(ArrayRef.DefaultEval(arrayRef, this)); }
public object EvalArrayRef(ArrayRef arrayRef) { return(DoEvalArrayRef(arrayRef)); }