2 using System
.Collections
.Generic
;
5 namespace Mono
.Profiler
.Aot
{
7 // Write the contents of a .aotprofile
8 // See mono/profiler/aot.h for a description of the file format
10 public sealed class ProfileWriter
: ProfileBase
{
15 readonly Dictionary
<TypeRecord
, int> typeIds
= new Dictionary
<TypeRecord
, int> ();
16 readonly Dictionary
<ModuleRecord
, int> moduleIds
= new Dictionary
<ModuleRecord
, int> ();
18 unsafe void WriteInt32 (int intValue
)
20 byte* b
= (byte*)&intValue
;
22 for (int i
= 0; i
< 4; i
++)
23 stream
.WriteByte (b
[i
]);
26 void WriteString (string str
)
28 var buf
= Encoding
.UTF8
.GetBytes (str
);
29 WriteInt32 (buf
.Length
);
30 stream
.Write (buf
, 0, buf
.Length
);
33 int AddModule (ModuleRecord m
)
36 if (moduleIds
.TryGetValue (m
, out mId
))
42 WriteRecord (RecordType
.IMAGE
, mId
);
49 int AddType (TypeRecord t
)
52 if (typeIds
.TryGetValue (t
, out tId
))
55 var moduleId
= AddModule (t
.Module
);
58 if (t
.GenericInst
!= null)
59 instId
= AddGenericInstance (t
.GenericInst
);
64 WriteRecord (RecordType
.TYPE
, tId
);
65 stream
.WriteByte ((byte)MonoTypeEnum
.MONO_TYPE_CLASS
);
66 WriteInt32 (moduleId
);
73 int AddGenericInstance (GenericInstRecord gi
)
75 // add the types first, before we start writing the GINST record
76 for (int i
= 0; i
< gi
.Types
.Length
; i
++)
77 AddType (gi
.Types
[i
]);
81 WriteRecord (RecordType
.GINST
, gId
);
82 WriteInt32 (gi
.Types
.Length
);
84 for (int i
= 0; i
< gi
.Types
.Length
; i
++)
85 WriteInt32 (AddType (gi
.Types
[i
]));
90 void WriteMethod (MethodRecord m
)
92 var typeId
= AddType (m
.Type
);
95 if (m
.GenericInst
!= null)
96 instId
= AddGenericInstance (m
.GenericInst
);
98 WriteRecord (RecordType
.METHOD
, id
++);
101 WriteInt32 (m
.ParamCount
);
102 WriteString (m
.Name
);
103 WriteString (m
.Signature
);
106 void WriteRecord (RecordType rt
, int value)
108 stream
.WriteByte ((byte)rt
);
112 public void WriteAllData (Stream s
, ProfileData pd
)
117 var buf
= Encoding
.UTF8
.GetBytes (MAGIC
);
118 this.stream
.Write (buf
, 0, buf
.Length
);
120 WriteInt32 ((MAJOR_VERSION
<< 16) | MINOR_VERSION
);
122 foreach (var m
in data
.Methods
)
125 // make sure ew have all the types
126 // sometime the profile contain type, which is not referenced from the methods
127 foreach (var t
in data
.Types
)
130 // just to be complete, do not miss any module too
131 foreach (var module
in data
.Modules
)
134 WriteRecord (RecordType
.NONE
, 0);