3 // author: Dan Lewis (dihlewis@yahoo.co.uk)
\r
8 // Tool for generating C prototypes and structures suitable for use by the runtime
\r
9 // from a list of supplied assemblies. See ictool-config.xml for configuration details.
\r
15 using System.Reflection;
\r
16 using System.Collections;
\r
18 public class ICTool {
\r
19 public static void Main (string[] args) {
\r
20 string filename = "ictool-config.xml";
\r
21 if (args.Length == 1) {
\r
24 else if (args.Length > 1) {
\r
25 Console.Error.WriteLine ("Usage: ictool.exe [config.xml]");
\r
26 Environment.Exit (-1);
\r
30 Stream config = File.OpenRead (filename);
\r
33 catch (Exception e) {
\r
34 Console.Error.WriteLine ("Error: could not read configuration file.");
\r
35 Console.Error.WriteLine (e);
\r
36 Environment.Exit (-1);
\r
45 private static void EmitPrototypes () {
\r
46 StreamWriter methods_file = GetOutputFile ("methods");
\r
47 StreamWriter map_file = GetOutputFile ("map");
\r
51 methods_file.WriteLine ("#include \"{0}\"\n", output_files["types"]);
\r
52 map_file.WriteLine ("#include \"{0}\"\n", output_files["methods"]);
\r
55 "static gpointer icall_map [] = {\n\t"
\r
58 ArrayList map_lines = new ArrayList ();
\r
60 BindingFlags binding =
\r
61 BindingFlags.DeclaredOnly |
\r
62 BindingFlags.Instance |
\r
63 BindingFlags.Static |
\r
64 BindingFlags.Public |
\r
65 BindingFlags.NonPublic;
\r
67 foreach (Type type in types.Values) {
\r
68 bool has_icall = false;
\r
69 MethodInfo[] methods = type.GetMethods (binding);
\r
71 foreach (MethodInfo method in methods) {
\r
72 if (IsInternalCall (method)) {
\r
81 methods_file.WriteLine ("\n/* {0} */\n", type.FullName);
\r
82 //map_lines.Add (String.Format ("\n/* {0} */\n", type.FullName));
\r
84 foreach (MethodInfo method in methods) {
\r
85 if (!IsInternalCall (method))
\r
90 string func_name = String.Format ("ves_icall_{0}_{1}",
\r
96 func_name = func_name.Replace ('.', '_');
\r
100 map_lines.Add (String.Format (
\r
101 "\"{0}::{1}\", {2}",
\r
103 type.FullName.Replace ('.', '_'),
\r
110 ArrayList args = new ArrayList ();
\r
112 // FIXME: return types that are structs need to be inserted
\r
113 // into the argument list as a destination pointer
\r
115 // object/value instance pointer
\r
117 if (IsInstanceMethod (method)) {
\r
118 args.Add (String.Format (
\r
121 peer_map.GetPeer (method.DeclaringType).GetTypedef (1),
\r
128 foreach (ParameterInfo param in method.GetParameters ()) {
\r
129 Type arg_type = param.ParameterType;
\r
132 if (arg_type.IsByRef) {
\r
133 arg_type = arg_type.GetElementType ();
\r
137 Peer arg_peer = peer_map.GetPeer (arg_type);
\r
138 if (!arg_peer.IsValueType)
\r
141 args.Add (String.Format ("{0}{1}", arg_peer.GetTypedef (refs), param.Name));
\r
144 Peer ret = peer_map.GetPeer (method.ReturnType);
\r
145 methods_file.WriteLine ("static {0}", ret.GetTypedef (ret.IsValueType ? 0 : 1));
\r
146 methods_file.WriteLine ("{0} ({1});",
\r
151 methods_file.WriteLine ();
\r
156 methods_file.Close ();
\r
158 // write map file and close it
\r
161 "{0}\n}};\n", Join (",\n\t", map_lines)
\r
167 private static bool IsInternalCall (MethodInfo meth) {
\r
168 return (meth.GetMethodImplementationFlags () & MethodImplAttributes.InternalCall) != 0;
\r
171 private static bool IsInstanceMethod (MethodInfo meth) {
\r
172 return (meth.CallingConvention & CallingConventions.HasThis) != 0;
\r
175 private static void EmitStructures () {
\r
176 StreamWriter file = GetOutputFile ("types");
\r
178 // build dependency graph
\r
180 DependencyGraph dg = new DependencyGraph ();
\r
181 foreach (Peer peer in peer_map.Peers) {
\r
184 // peer depends on nearest base
\r
186 if (peer.NearestBase != null)
\r
187 dg.AddEdge (peer.NearestBase, peer);
\r
189 // peer depends on any value types used for fields
\r
191 foreach (PeerField field in peer.Fields) {
\r
192 if (field.Peer.IsValueType)
\r
193 dg.AddEdge (field.Peer, peer);
\r
197 // write structures in order
\r
199 foreach (Peer peer in dg.TopologicalSort ()) {
\r
204 file.WriteLine ("typedef {0} {1};", peer.UnderlyingPeer.Name, peer.Name);
\r
205 file.WriteLine ("enum _{0} {{", peer.Name);
\r
207 ArrayList enum_lines = new ArrayList ();
\r
208 foreach (string name in peer.EnumConstants.Keys) {
\r
209 enum_lines.Add (String.Format ("\t{0}_{1} = {2}",
\r
212 peer.EnumConstants[name]
\r
216 file.WriteLine ("{0}\n}};\n", Join (",\n", enum_lines));
\r
219 file.WriteLine ("typedef struct _{0} {{", peer.Name);
\r
223 if (peer.NearestBase != null) {
\r
224 file.WriteLine ("\t{0} __base;", peer.NearestBase.Name);
\r
230 foreach (PeerField field in peer.Fields) {
\r
231 bool use_struct = true;
\r
232 if (field.Peer.IsValueType || field.Peer.IsOpaque)
\r
233 use_struct = false;
\r
235 file.WriteLine ("\t{0}{1}{2};",
\r
236 use_struct ? "struct _" : "",
\r
237 field.Peer.GetTypedef (field.Peer.IsValueType ? 0 : 1),
\r
242 file.WriteLine ("}} {0};\n", peer.Name);
\r
247 private static void LoadAssemblies () {
\r
248 types = new Hashtable ();
\r
250 foreach (string filename in assemblies) {
\r
255 FileInfo info = null;
\r
256 foreach (string path in assembly_paths) {
\r
257 info = new FileInfo (Path.Combine (path, filename));
\r
262 if (!info.Exists) {
\r
263 Console.Error.WriteLine ("Error: assembly {0} not found.", filename);
\r
264 Environment.Exit (-1);
\r
269 assembly = Assembly.LoadFrom (info.FullName);
\r
273 ArrayList loaded_types;
\r
276 loaded_types = new ArrayList (assembly.GetTypes ());
\r
278 catch (ReflectionTypeLoadException e) {
\r
279 loaded_types = new ArrayList ();
\r
280 foreach (Type type in e.Types) {
\r
282 loaded_types.Add (type);
\r
285 foreach (Exception f in e.LoaderExceptions) {
\r
286 if (f is TypeLoadException) {
\r
287 Console.Error.WriteLine ("Warning: {0} could not be loaded from assembly {1}.",
\r
288 ((TypeLoadException)f).TypeName,
\r
293 Console.Error.WriteLine (f);
\r
297 // add to type dictionary
\r
299 foreach (Type type in loaded_types) {
\r
300 if (!types.Contains (type.FullName))
\r
301 types.Add (type.FullName, type);
\r
306 private static void Configure (Stream input) {
\r
307 XmlDocument doc = new XmlDocument ();
\r
312 assembly_paths = new ArrayList ();
\r
313 assembly_paths.Add (".");
\r
315 foreach (XmlNode node in doc.SelectNodes ("config/assemblypath")) {
\r
316 assembly_paths.Add (node.Attributes["path"].Value);
\r
319 assemblies = new ArrayList ();
\r
320 foreach (XmlNode node in doc.SelectNodes ("config/assembly")) {
\r
321 assemblies.Add (node.Attributes["file"].Value);
\r
329 XmlNode path_node = doc.SelectSingleNode ("config/outputpath");
\r
330 if (path_node != null)
\r
331 output_path = path_node.Attributes["path"].Value;
\r
333 output_files = new Hashtable ();
\r
334 output_includes = new Hashtable ();
\r
335 foreach (XmlNode node in doc.SelectNodes ("config/outputfile")) {
\r
336 string name = node.Attributes["name"].Value;
\r
337 output_files.Add (name, node.Attributes["file"].Value);
\r
339 foreach (XmlNode child in node.ChildNodes) {
\r
340 if (child.Name == "include")
\r
341 output_includes[name] = child.InnerText;
\r
347 peer_map = new PeerMap ();
\r
348 foreach (XmlNode node in doc.SelectNodes ("config/typemap/namespace")) {
\r
349 string ns = node.Attributes["name"].Value;
\r
351 foreach (XmlNode child in node.ChildNodes) {
\r
352 if (child.Name == "type") {
\r
353 string name = child.Attributes["name"].Value;
\r
354 string peer_name = child.Attributes["peer"].Value;
\r
356 bool opaque = false;
\r
357 if (child.Attributes["opaque"] != null && child.Attributes["opaque"].Value == "true")
\r
360 String fullname = String.Format ("{0}.{1}", ns, name);
\r
363 if (child.Attributes["default"] != null && child.Attributes["default"].Value == "true")
\r
364 type = Type.GetType (fullname);
\r
366 type = (Type)types [fullname];
\r
369 peer_map.Add (new Peer (type, peer_name, opaque));
\r
374 peer_map.ResolvePeers ();
\r
377 private static StreamWriter GetOutputFile (string name) {
\r
378 string filename = Path.Combine (output_path, (string)output_files[name]);
\r
379 StreamWriter file = File.CreateText (filename);
\r
380 file.AutoFlush = true;
\r
384 // (verbatim string)
\r
389 * This file was automatically generated on {1} by ictool.exe from
\r
390 * the following assemblies:
\r
396 output_files[name],
\r
397 DateTime.Now.ToString ("d"),
\r
398 Join (", ", assemblies)
\r
401 if (output_includes.Contains (name)) {
\r
402 file.WriteLine (output_includes [name]);
\r
409 private static string Join (string separator, ICollection values) {
\r
410 // note to microsoft: please implement this in String :)
\r
412 string[] strs = new string[values.Count];
\r
415 foreach (object value in values)
\r
416 strs[i ++] = value.ToString ();
\r
418 return String.Join (separator, strs);
\r
421 private static ArrayList assembly_paths;
\r
422 private static ArrayList assemblies;
\r
423 private static string output_path;
\r
424 private static Hashtable output_files;
\r
425 private static Hashtable output_includes;
\r
426 private static PeerMap peer_map;
\r
427 private static Hashtable types;
\r