| | | 1 | | using NanoCLang.Environemnts; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Linq; |
| | | 5 | | |
| | | 6 | | namespace NanoCLang.Entities { |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides a class for allocation expressions. |
| | | 9 | | /// </summary> |
| | | 10 | | public class AllocationExpression : Expression, IEquatable<AllocationExpression?> { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Creates a new instance of an allocation expression that allocates the <paramref name="size"/> in bytes at th |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="location">Location that should be allocated in.</param> |
| | | 15 | | /// <param name="size">Size in bytes of the allocation.</param> |
| | 42 | 16 | | public AllocationExpression(string location, PureExpression size) { |
| | 21 | 17 | | Location = location; |
| | 21 | 18 | | Size = size; |
| | 21 | 19 | | } |
| | | 20 | | /// <summary> |
| | | 21 | | /// Location that is to be allocated. |
| | | 22 | | /// </summary> |
| | 52 | 23 | | public string Location { get; } |
| | | 24 | | /// <summary> |
| | | 25 | | /// Size in bytes of the allocated block. |
| | | 26 | | /// </summary> |
| | 55 | 27 | | public PureExpression Size { get; } |
| | | 28 | | /// <inheritdoc/> |
| | 13 | 29 | | protected override World DoInferWorld(GlobalEnvironment phi, LocalEnvironment gamma, Heap heap) { |
| | 13 | 30 | | var labs = new Location(Location, true); |
| | 13 | 31 | | var lj = new Location(Location, false); |
| | 13 | 32 | | if (!heap.TryGetBinding(labs, out var b)) |
| | 1 | 33 | | throw new IllFormedException(this, $"Allocation invalid: heap does not contain abstract location binding |
| | 12 | 34 | | if (heap.TryGetBinding(lj, out _)) |
| | 1 | 35 | | throw new IllFormedException(this, $"Allocation invalid: abstract location {labs} was already unfolded t |
| | 11 | 36 | | var st = Size.InferType(gamma); |
| | 11 | 37 | | if (!(st.BaseType is IntegerType s)) |
| | 1 | 38 | | throw new IllFormedException(Size, $"Allocation invalid: expected integral type size but got {st.BaseTyp |
| | 20 | 39 | | if (!st.SubType(gamma, new RefinedType(new IntegerType(s.Size), v => v > new IntegerConstant(0, s.Size)))) |
| | 2 | 40 | | throw new IllFormedException(Size, $"Allocation invalid: size must be a positive integer!"); |
| | | 41 | | Heap hout; |
| | 8 | 42 | | try { |
| | 19 | 43 | | hout = heap * new LocationBinding(lj, b.Select(i => i.BaseType).ToArray()); |
| | 8 | 44 | | } catch (ArgumentException e) { |
| | 0 | 45 | | throw new IllFormedException(this, $"Cannot concatenate heaps: {e.Message}!"); |
| | | 46 | | } |
| | 8 | 47 | | return new World( |
| | 8 | 48 | | new RefinedType(new ReferenceType(lj, new SingletonIndex(0)), |
| | 8 | 49 | | v => new UninterpretedApplicationExpression("Safe", v) |
| | 8 | 50 | | & new UninterpretedApplicationExpression("BLen", v, Size)), |
| | 8 | 51 | | hout); |
| | 8 | 52 | | } |
| | | 53 | | /// <inheritdoc/> |
| | 8 | 54 | | public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) { |
| | 8 | 55 | | yield return "malloc"; |
| | 8 | 56 | | if (args.SpaceAfterFunctionName) yield return " "; |
| | 8 | 57 | | yield return "("; |
| | 8 | 58 | | yield return Location; |
| | 8 | 59 | | yield return args.ParameterListSeparator; |
| | 72 | 60 | | foreach (var tk in Size.Tokens(args)) yield return tk; |
| | 8 | 61 | | yield return ")"; |
| | 8 | 62 | | } |
| | | 63 | | /// <inheritdoc/> |
| | 7 | 64 | | public override IEnumerable<StringFormatterToken> Tokens(CSourceFormat args) { |
| | 7 | 65 | | if (FixedWorld is null) throw new InvalidOperationException("Expression must have fixed world when translati |
| | 7 | 66 | | if (args.PureRequiresReturn) yield return "return "; |
| | 7 | 67 | | yield return "(("; |
| | 63 | 68 | | foreach (var tk in FixedWorld.Type.Tokens(args)) yield return tk; |
| | 7 | 69 | | yield return ")"; |
| | 7 | 70 | | yield return "malloc"; |
| | 7 | 71 | | if (args.SpaceAfterFunctionName) yield return " "; |
| | 7 | 72 | | yield return "("; |
| | 78 | 73 | | foreach (var tk in Size.Tokens(args)) yield return tk; |
| | 7 | 74 | | yield return "))"; |
| | 7 | 75 | | if (args.PureRequiresReturn) yield return ";"; |
| | 7 | 76 | | } |
| | | 77 | | /// <inheritdoc/> |
| | 0 | 78 | | public override IEnumerable<string> RequiredFunctions() { yield break; } |
| | | 79 | | #region Equality checks |
| | | 80 | | /// <inheritdoc/> |
| | 9 | 81 | | public override bool Equals(object? obj) => Equals(obj as AllocationExpression); |
| | | 82 | | /// <inheritdoc/> |
| | 9 | 83 | | public bool Equals(AllocationExpression? other) => !(other is null) && Location == other.Location && EqualityCom |
| | | 84 | | /// <inheritdoc/> |
| | 0 | 85 | | public override int GetHashCode() => HashCode.Combine(Location, Size); |
| | | 86 | | /// <inheritdoc/> |
| | 0 | 87 | | public static bool operator ==(AllocationExpression? left, AllocationExpression? right) => EqualityComparer<Allo |
| | | 88 | | /// <inheritdoc/> |
| | 0 | 89 | | public static bool operator !=(AllocationExpression? left, AllocationExpression? right) => !(left == right); |
| | | 90 | | #endregion |
| | | 91 | | } |
| | | 92 | | } |