1 //
2 // Copyright (c) 2010-2022 Antmicro
3 //
4 // This file is licensed under the MIT License.
5 // Full license text is available in 'licenses/MIT.txt'.
6 //
7 using System.Linq;
8 using System.Collections.Generic;
9 
10 namespace Antmicro.Renode.Peripherals.CPU
11 {
12     public struct GDBCustomType
13     {
GDBCustomTypeAntmicro.Renode.Peripherals.CPU.GDBCustomType14         public GDBCustomType(string type, IReadOnlyDictionary<string, string> attributes, IEnumerable<IReadOnlyDictionary<string, string>> fields) : this()
15         {
16             this.Type = type;
17             this.Attributes = attributes;
18             this.Fields = fields ?? Enumerable.Empty<Dictionary<string, string>>();
19         }
20 
VectorAntmicro.Renode.Peripherals.CPU.GDBCustomType21         public static GDBCustomType Vector(string id, string type, uint count)
22         {
23             var attributes = new Dictionary<string, string>();
24             attributes.Add("id", id);
25             attributes.Add("type", type);
26             attributes.Add("count", $"{count}");
27             return new GDBCustomType("vector", attributes, null);
28         }
29 
UnionAntmicro.Renode.Peripherals.CPU.GDBCustomType30         public static GDBCustomType Union(string id, IEnumerable<GDBTypeField> fields)
31         {
32             var attributes = new Dictionary<string, string>();
33             attributes.Add("id", id);
34             return new GDBCustomType("union", attributes, CreateFields(fields));
35         }
36 
StructAntmicro.Renode.Peripherals.CPU.GDBCustomType37         public static GDBCustomType Struct(string id, IEnumerable<GDBTypeField> fields)
38         {
39             var attributes = new Dictionary<string, string>();
40             attributes.Add("id", id);
41             return new GDBCustomType("struct", attributes, CreateFields(fields));
42         }
43 
StructAntmicro.Renode.Peripherals.CPU.GDBCustomType44         public static GDBCustomType Struct(string id, uint size, IEnumerable<GDBTypeBitField> fields)
45         {
46             var attributes = new Dictionary<string, string>();
47             attributes.Add("id", id);
48             attributes.Add("size", $"{size}");
49             return new GDBCustomType("struct", attributes, CreateFields(fields));
50         }
51 
FlagsAntmicro.Renode.Peripherals.CPU.GDBCustomType52         public static GDBCustomType Flags(string id, uint size, IEnumerable<GDBTypeBitField> fields)
53         {
54             var attributes = new Dictionary<string, string>();
55             attributes.Add("id", id);
56             attributes.Add("size", $"{size}");
57             return new GDBCustomType("flags", attributes, CreateFields(fields));
58         }
59 
EnumAntmicro.Renode.Peripherals.CPU.GDBCustomType60         public static GDBCustomType Enum(string id, uint size, IEnumerable<GDBTypeEnumValue> values)
61         {
62             var attributes = new Dictionary<string, string>();
63             attributes.Add("id", id);
64             attributes.Add("size", $"{size}");
65             return new GDBCustomType("enum", attributes, CreateFields(values));
66         }
67 
CreateFieldsAntmicro.Renode.Peripherals.CPU.GDBCustomType68         private static IEnumerable<IReadOnlyDictionary<string, string>> CreateFields(IEnumerable<GDBTypeField> types)
69         {
70             var fields = new List<Dictionary<string, string>>();
71             foreach(var type in types)
72             {
73                 var field = new Dictionary<string, string>();
74                 field.Add("name", type.Name);
75                 field.Add("type", type.Type);
76                 fields.Add(field);
77             }
78             return fields;
79         }
80 
CreateFieldsAntmicro.Renode.Peripherals.CPU.GDBCustomType81         private static IEnumerable<IReadOnlyDictionary<string, string>> CreateFields(IEnumerable<GDBTypeBitField> bitFields)
82         {
83             var fields = new List<Dictionary<string, string>>();
84             foreach(var type in bitFields)
85             {
86                 var field = new Dictionary<string, string>();
87                 field.Add("name", type.Name);
88                 field.Add("start", $"{type.Start}");
89                 field.Add("end", $"{type.End}");
90                 field.Add("type", type.Type);
91                 fields.Add(field);
92             }
93             return fields;
94         }
95 
CreateFieldsAntmicro.Renode.Peripherals.CPU.GDBCustomType96         private static IEnumerable<IReadOnlyDictionary<string, string>> CreateFields(IEnumerable<GDBTypeEnumValue> values)
97         {
98             var fields = new List<Dictionary<string, string>>();
99             foreach(var value in values)
100             {
101                 var field = new Dictionary<string, string>();
102                 field.Add("name", value.Name);
103                 field.Add("value", $"{value.Value}");
104                 fields.Add(field);
105             }
106             return fields;
107         }
108 
109         public string Type { get; }
110         public IReadOnlyDictionary<string, string> Attributes { get; }
111         public IEnumerable<IReadOnlyDictionary<string, string>> Fields { get; }
112     }
113 }
114