Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Program.cs « ThunkGenerator « JitInterface « Common « tools « coreclr « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b0d12f06cdc12c8d4f6fef3dbd0e52931f360a30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;

namespace Thunkerator
{
    // Parse type replacement section for normal types
    // Parse type replacement section for return value types

    public static class StringExtensions
    {
        public static string Canonicalize(this string current)
        {
            string untrimmed = "";
            while (untrimmed != current)
            {
                untrimmed = current;
                current = current.Replace(" *", "*");
                current = current.Replace("* ", "*");
                current = current.Replace(" ,", ",");
                current = current.Replace(", ", ",");
                current = current.Replace("  ", " ");
                current = current.Replace("\t", " ");
            }

            return current.Trim();
        }
    }

    class TypeReplacement
    {
        public TypeReplacement(string line)
        {
            string[] typenames = line.Split(',');
            if ((typenames.Length < 1) || (typenames.Length > 4))
            {
                throw new Exception("Wrong number of type name entries");
            }
            ThunkTypeName = typenames[0].Canonicalize();

            if (typenames.Length > 1 && !string.IsNullOrWhiteSpace(typenames[1]))
            {
                ManagedTypeName = typenames[1].Canonicalize();
            }
            else
            {
                ManagedTypeName = ThunkTypeName;
            }

            if (typenames.Length > 2)
            {
                NativeTypeName = typenames[2].Canonicalize();
            }
            else
            {
                NativeTypeName = ThunkTypeName;
            }

            if (typenames.Length > 3)
            {
                NativeTypeName2 = typenames[3].Canonicalize();
            }
            else
            {
                NativeTypeName2 = ThunkTypeName;
            }
        }
        public readonly string ThunkTypeName;
        public readonly string NativeTypeName;
        public readonly string NativeTypeName2;
        public readonly string ManagedTypeName;

        public bool IsByRef => ManagedTypeName.Contains("ref ");
        public bool IsBoolean => ManagedTypeName == "[MarshalAs(UnmanagedType.I1)]bool";
        public bool IsBOOL => ManagedTypeName == "[MarshalAs(UnmanagedType.Bool)]bool";

        public string UnmanagedTypeName
        {
            get
            {
                if (IsBoolean)
                    return "byte";

                if (IsBOOL)
                    return "int";

                if (IsByRef)
                    return ManagedTypeName.Replace("ref ", "") + "*";

                // No special marshaling rules
                return ManagedTypeName;
            }
        }
    }

    class Parameter
    {
        public Parameter(string name, TypeReplacement type)
        {
            Type = type;
            Name = name;
            if (name.StartsWith("*"))
                throw new Exception("Names not allowed to start with *");
        }

        public readonly string Name;
        public readonly TypeReplacement Type;
    }

    class FunctionDecl
    {
        public FunctionDecl(string line, Dictionary<string, TypeReplacement> ThunkReturnTypes, Dictionary<string, TypeReplacement> ThunkTypes)
        {
            if (line.Contains("[ManualNativeWrapper]"))
            {
                ManualNativeWrapper = true;
                line = line.Replace("[ManualNativeWrapper]", string.Empty);
            }

            int indexOfOpenParen = line.IndexOf('(');
            int indexOfCloseParen = line.IndexOf(')');
            string returnTypeAndFunctionName = line.Substring(0, indexOfOpenParen).Canonicalize();
            int indexOfLastWhitespaceInReturnTypeAndFunctionName = returnTypeAndFunctionName.LastIndexOfAny(new char[] { ' ', '*' });
            FunctionName = returnTypeAndFunctionName.Substring(indexOfLastWhitespaceInReturnTypeAndFunctionName + 1).Canonicalize();
            if (FunctionName.StartsWith("*"))
                throw new Exception("Names not allowed to start with *");
            string returnType = returnTypeAndFunctionName.Substring(0, indexOfLastWhitespaceInReturnTypeAndFunctionName + 1).Canonicalize();

            if (!ThunkReturnTypes.TryGetValue(returnType, out ReturnType))
            {
                throw new Exception(String.Format("Type {0} unknown", returnType));
            }

            string parameterList = line.Substring(indexOfOpenParen + 1, indexOfCloseParen - indexOfOpenParen - 1).Canonicalize();
            string[] parametersString = parameterList.Length == 0 ? new string[0] : parameterList.Split(',');
            List<Parameter> parameters = new List<Parameter>();

            foreach (string parameterString in parametersString)
            {
                int indexOfLastWhitespaceInParameter = parameterString.LastIndexOfAny(new char[] { ' ', '*' });
                string paramName = parameterString.Substring(indexOfLastWhitespaceInParameter + 1).Canonicalize();
                string paramType = parameterString.Substring(0, indexOfLastWhitespaceInParameter + 1).Canonicalize();
                TypeReplacement tr;
                if (!ThunkTypes.TryGetValue(paramType, out tr))
                {
                    throw new Exception(String.Format("Type {0} unknown", paramType));
                }
                parameters.Add(new Parameter(paramName, tr));
            }

            Parameters = parameters.ToArray();
        }

        public readonly string FunctionName;
        public readonly TypeReplacement ReturnType;
        public readonly Parameter[] Parameters;
        public readonly bool ManualNativeWrapper = false;
    }

    class Program
    {
        enum ParseMode
        {
            RETURNTYPES,
            NORMALTYPES,
            FUNCTIONS,
            IFDEFING
        }
        static IEnumerable<FunctionDecl> ParseInput(TextReader tr)
        {
            Dictionary<string, TypeReplacement> ThunkReturnTypes = new Dictionary<string, TypeReplacement>();
            Dictionary<string, TypeReplacement> ThunkTypes = new Dictionary<string, TypeReplacement>();
            ParseMode currentParseMode = ParseMode.FUNCTIONS;
            ParseMode oldParseMode = ParseMode.FUNCTIONS;
            List<FunctionDecl> functions = new List<FunctionDecl>();
            int currentLineIndex = 1;
            for (string currentLine = tr.ReadLine(); currentLine != null; currentLine = tr.ReadLine(), currentLineIndex++)
            {
                try
                {
                    if (currentLine.Length == 0)
                    {
                        continue; // Its an empty line, ignore
                    }

                    if (currentLine[0] == ';')
                    {
                        continue; // Its a comment
                    }

                    if (currentLine == "RETURNTYPES")
                    {
                        currentParseMode = ParseMode.RETURNTYPES;
                        continue;
                    }
                    if (currentLine == "NORMALTYPES")
                    {
                        currentParseMode = ParseMode.NORMALTYPES;
                        continue;
                    }
                    if (currentLine == "FUNCTIONS")
                    {
                        currentParseMode = ParseMode.FUNCTIONS;
                        continue;
                    }

                    if (currentLine == "#endif")
                    {
                        currentParseMode = oldParseMode;
                        continue;
                    }

                    if (currentLine.StartsWith("#if"))
                    {
                        oldParseMode = currentParseMode;
                        currentParseMode = ParseMode.IFDEFING;
                    }

                    if (currentParseMode == ParseMode.IFDEFING)
                    {
                        continue;
                    }

                    switch (currentParseMode)
                    {
                        case ParseMode.NORMALTYPES:
                        case ParseMode.RETURNTYPES:
                            TypeReplacement t = new TypeReplacement(currentLine);
                            if (currentParseMode == ParseMode.NORMALTYPES)
                            {
                                ThunkTypes.Add(t.ThunkTypeName, t);
                                ThunkReturnTypes.Add(t.ThunkTypeName, t);
                            }
                            if (currentParseMode == ParseMode.RETURNTYPES)
                            {
                                ThunkReturnTypes[t.ThunkTypeName] = t;
                            }
                            break;

                        case ParseMode.FUNCTIONS:
                            functions.Add(new FunctionDecl(currentLine, ThunkReturnTypes, ThunkTypes));
                            break;
                    }
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine("Error parsing line {0} : {1}", currentLineIndex, e.Message);
                }
            }

            return functions.AsReadOnly();
        }

        static void WriteAutogeneratedHeader(TextWriter tw)
        {
            // Write header
            tw.Write(@"// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// DO NOT EDIT THIS FILE! IT IS AUTOGENERATED
// To regenerate run the gen script in src/coreclr/tools/Common/JitInterface/ThunkGenerator
// and follow the instructions in docs/project/updating-jitinterface.md
");
        }

        static void WriteManagedThunkInterface(TextWriter tw, IEnumerable<FunctionDecl> functionData)
        {
            WriteAutogeneratedHeader(tw);
            tw.Write(@"
using System;
using System.Runtime.InteropServices;

namespace Internal.JitInterface
{
    internal unsafe partial class CorInfoImpl
    {
");

            foreach (FunctionDecl decl in functionData)
            {
                tw.WriteLine("        [UnmanagedCallersOnly]");
                tw.Write($"        private static {decl.ReturnType.UnmanagedTypeName} _{decl.FunctionName}(IntPtr thisHandle, IntPtr* ppException");
                foreach (Parameter param in decl.Parameters)
                {
                    tw.Write($", {param.Type.UnmanagedTypeName} {param.Name}");
                }
                tw.Write(@")
        {
            var _this = GetThis(thisHandle);
            try
            {
");
                bool isVoid = decl.ReturnType.ManagedTypeName == "void";
                tw.Write($"                {(isVoid ? "" : "return ")}_this.{decl.FunctionName}(");
                bool isFirst = true;
                foreach (Parameter param in decl.Parameters)
                {
                    if (isFirst)
                    {
                        isFirst = false;
                    }
                    else
                    {
                        tw.Write(", ");
                    }

                    if (param.Type.IsByRef)
                    {
                        tw.Write("ref *");
                    }
                    tw.Write(param.Name);
                    if (param.Type.IsBoolean || param.Type.IsBOOL)
                    {
                        tw.Write(" != 0");
                    }
                }
                tw.Write(")");
                if (decl.ReturnType.IsBOOL || decl.ReturnType.IsBoolean)
                {
                    tw.Write($" ? ({decl.ReturnType.UnmanagedTypeName})1 : ({decl.ReturnType.UnmanagedTypeName})0");
                }
                tw.Write(";");
                tw.Write(@"
            }
            catch (Exception ex)
            {
                *ppException = _this.AllocException(ex);
");
                if (!isVoid)
                {
                    tw.WriteLine("                return default;");
                }
                tw.WriteLine(@"            }");
                tw.WriteLine("        }");
                tw.WriteLine();
            }

            int total = functionData.Count();
            tw.WriteLine(@"
        private static IntPtr GetUnmanagedCallbacks()
        {
            void** callbacks = (void**)Marshal.AllocCoTaskMem(sizeof(IntPtr) * " + total + @");
");

            int index = 0;
            foreach (FunctionDecl decl in functionData)
            {
                tw.Write($"            callbacks[{index}] = (delegate* unmanaged<IntPtr, IntPtr*");
                foreach (Parameter param in decl.Parameters)
                {
                    tw.Write($", {param.Type.UnmanagedTypeName}");
                }
                tw.WriteLine($", {decl.ReturnType.UnmanagedTypeName}>)&_{decl.FunctionName};");
                index++;
            }

            tw.WriteLine(@"
            return (IntPtr)callbacks;
        }
    }
}
");
        }

        static void WriteNativeWrapperInterface(TextWriter tw, IEnumerable<FunctionDecl> functionData)
        {
            WriteAutogeneratedHeader(tw);
            tw.Write(@"

#include ""corinfoexception.h""
#include ""../../../inc/corjit.h""

struct JitInterfaceCallbacks
{
");

            foreach (FunctionDecl decl in functionData)
            {
                tw.Write($"    {decl.ReturnType.NativeTypeName} (* {decl.FunctionName})(void * thisHandle, CorInfoExceptionClass** ppException");
                foreach (Parameter param in decl.Parameters)
                {
                    tw.Write($", {param.Type.NativeTypeName} {param.Name}");
                }
                tw.WriteLine(");");
            }

            tw.Write(@"
};

class JitInterfaceWrapper : public ICorJitInfo
{
    void * _thisHandle;
    JitInterfaceCallbacks * _callbacks;

public:
    JitInterfaceWrapper(void * thisHandle, void ** callbacks)
        : _thisHandle(thisHandle), _callbacks((JitInterfaceCallbacks *)callbacks)
    {
    }

");

            API_Wrapper_Generic_Core(tw, functionData, 
                funcNameFunc: (FunctionDecl decl)=>$"{decl.FunctionName }", 
                beforeCallFunc:(FunctionDecl)=>"    CorInfoExceptionClass* pException = nullptr;", 
                afterCallFunc: (FunctionDecl decl) => "    if (pException != nullptr) throw pException;", 
                wrappedObjectName: "_callbacks", 
                useNativeType2: false, 
                addVirtualPrefix: true, 
                skipManualWrapper: true);

            tw.WriteLine("};");
        }

        static void WriteAPI_Names(TextWriter tw, IEnumerable<FunctionDecl> functionData)
        {
            WriteAutogeneratedHeader(tw);

            foreach (FunctionDecl decl in functionData)
            {
                tw.WriteLine($"DEF_CLR_API({decl.FunctionName})");
            }

            tw.Write(@"
#undef DEF_CLR_API
");
        }

        static void API_Wrapper_Generic_Core(TextWriter tw, IEnumerable<FunctionDecl> functionData, Func<FunctionDecl, string> funcNameFunc, Func<FunctionDecl, string> beforeCallFunc, Func<FunctionDecl,string> afterCallFunc, string wrappedObjectName, bool useNativeType2, bool addVirtualPrefix, bool skipManualWrapper)
        {
            foreach (FunctionDecl decl in functionData)
            {
                tw.WriteLine("");
                if (addVirtualPrefix)
                {
                    tw.Write("    virtual ");
                }
                tw.Write($"{GetNativeType(decl.ReturnType)} {funcNameFunc(decl)}(");
                bool isFirst = true;
                foreach (Parameter param in decl.Parameters)
                {
                    if (isFirst)
                    {
                        isFirst = false;
                    }
                    else
                    {
                        tw.Write(",");
                    }
                    tw.Write(Environment.NewLine + "          " + GetNativeType(param.Type) + " " + param.Name);
                }
                tw.Write(')');
                if (skipManualWrapper && decl.ManualNativeWrapper)
                {
                    tw.WriteLine(";");
                    continue;
                }
                tw.WriteLine("");
                tw.WriteLine("{");
                string beforeCall = beforeCallFunc(decl) ?? null;
                string afterCall = afterCallFunc(decl) ?? null;
                if (beforeCall != null)
                    tw.WriteLine(beforeCall);

                tw.Write("    ");
                if (GetNativeType(decl.ReturnType) != "void")
                {
                    if (afterCall != null)
                        tw.Write($"{GetNativeType(decl.ReturnType)} temp = ");
                    else
                        tw.Write("return ");
                }
                tw.Write($"{wrappedObjectName}->{decl.FunctionName}(");
                isFirst = true;

                if (skipManualWrapper)
                {
                    tw.Write("_thisHandle, &pException");
                    isFirst = false;
                }

                foreach (Parameter param in decl.Parameters)
                {
                    if (isFirst)
                    {
                        isFirst = false;
                    }
                    else
                    {
                        tw.Write(", ");
                    }
                    tw.Write(param.Name);
                }
                tw.WriteLine(");");
                if (afterCall != null)
                    tw.WriteLine(afterCall);
                if ((GetNativeType(decl.ReturnType) != "void") && (afterCall != null))
                {
                    tw.WriteLine("    return temp;");
                }
                tw.WriteLine("}");
            }

            string GetNativeType(TypeReplacement typeReplacement)
            {
                if (useNativeType2)
                    return typeReplacement.NativeTypeName2;
                else
                    return typeReplacement.NativeTypeName;
            }
        }

        static void API_Wrapper_Generic(TextWriter tw, IEnumerable<FunctionDecl> functionData, string header, string footer, string cppType, Func<FunctionDecl, string> beforeCallFunc, Func<FunctionDecl,string> afterCallFunc, string wrappedObjectName)
        {
            WriteAutogeneratedHeader(tw);
            tw.Write(header);

            API_Wrapper_Generic_Core(tw, functionData, funcNameFunc: (FunctionDecl decl)=>$"{cppType}::{ decl.FunctionName }", beforeCallFunc:beforeCallFunc, afterCallFunc: afterCallFunc, wrappedObjectName: wrappedObjectName, useNativeType2: true, addVirtualPrefix: false, skipManualWrapper: false);

            tw.Write(footer);
        }

        static void API_Wrapper(TextWriter tw, IEnumerable<FunctionDecl> functionData)
        {
            API_Wrapper_Generic(tw, functionData, 
                                header:@"
#define API_ENTER(name) wrapComp->CLR_API_Enter(API_##name);
#define API_LEAVE(name) wrapComp->CLR_API_Leave(API_##name);

/**********************************************************************************/
// clang-format off
/**********************************************************************************/
", 
                                footer: @"
/**********************************************************************************/
// clang-format on
/**********************************************************************************/
",
                                cppType: "WrapICorJitInfo",
                                beforeCallFunc: (FunctionDecl decl)=> $"    API_ENTER({decl.FunctionName});",
                                afterCallFunc: (FunctionDecl decl)=> $"    API_LEAVE({decl.FunctionName});",
                                wrappedObjectName: "wrapHnd");
        }

        static void SPMI_ICorJitInfoImpl(TextWriter tw, IEnumerable<FunctionDecl> functionData)
        {
            WriteAutogeneratedHeader(tw);
            tw.Write(@"

// ICorJitInfoImpl: declare for implementation all the members of the ICorJitInfo interface (which are
// specified as pure virtual methods). This is done once, here, and all implementations share it,
// to avoid duplicated declarations. This file is #include'd within all the ICorJitInfo implementation
// classes.
//
// NOTE: this file is in exactly the same order, with exactly the same whitespace, as the ICorJitInfo
// interface declaration (with the ""virtual"" and ""= 0"" syntax removed). This is to make it easy to compare
// against the interface declaration.

/**********************************************************************************/
// clang-format off
/**********************************************************************************/

public:
");

            foreach (FunctionDecl decl in functionData)
            {
                tw.Write($"{Environment.NewLine}{decl.ReturnType.NativeTypeName2} { decl.FunctionName}(");
                bool isFirst = true;
                foreach (Parameter param in decl.Parameters)
                {
                    if (isFirst)
                    {
                        isFirst = false;
                    }
                    else
                    {
                        tw.Write(",");
                    }
                    tw.Write(Environment.NewLine + "          " + param.Type.NativeTypeName2 + " " + param.Name);
                }
                tw.WriteLine(") override;");
            }

            tw.Write(@"
/**********************************************************************************/
// clang-format on
/**********************************************************************************/
");
        }

        static void SPMI_ShimCounter_ICorJitInfo(TextWriter tw, IEnumerable<FunctionDecl> functionData)
        {
            API_Wrapper_Generic(tw, functionData, 
                                header:@"
#include ""standardpch.h""
#include ""icorjitinfo.h""
#include ""superpmi-shim-counter.h""
#include ""icorjitcompiler.h""
#include ""spmiutil.h""

", 
                                footer: Environment.NewLine,
                                cppType: "interceptor_ICJI",
                                beforeCallFunc: (FunctionDecl decl)=> $"    mcs->AddCall(\"{decl.FunctionName}\");",
                                afterCallFunc: (FunctionDecl decl)=> null,
                                wrappedObjectName: "original_ICorJitInfo");
        }

        static void SPMI_ShimSimple_ICorJitInfo(TextWriter tw, IEnumerable<FunctionDecl> functionData)
        {
            API_Wrapper_Generic(tw, functionData, 
                                header:@"
#include ""standardpch.h""
#include ""icorjitinfo.h""
#include ""superpmi-shim-simple.h""
#include ""icorjitcompiler.h""
#include ""spmiutil.h""

", 
                                footer: Environment.NewLine,
                                cppType: "interceptor_ICJI",
                                beforeCallFunc: (FunctionDecl decl)=> null,
                                afterCallFunc: (FunctionDecl decl)=> null,
                                wrappedObjectName: "original_ICorJitInfo");
        }

        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("ThunkGenerator - Generate thunks for the jit interface and for defining the set of instruction sets supported by the runtime, JIT, and crossgen2. Call by using the gen scripts which are aware of the right set of files generated and command line args.");
                return;
            }
            if (args[0] == "InstructionSetGenerator")
            {
                if (args.Length != 7)
                {
                    Console.WriteLine("Incorrect number of files specified for generation");
                }
                InstructionSetGenerator generator = new InstructionSetGenerator();
                if (!generator.ParseInput(new StreamReader(args[1])))
                    return;

                using (TextWriter tw = new StreamWriter(args[2]))
                {
                    Console.WriteLine("Generating {0}", args[2]);
                    generator.WriteManagedReadyToRunInstructionSet(tw);
                }

                using (TextWriter tw = new StreamWriter(args[3]))
                {
                    Console.WriteLine("Generating {0}", args[3]);
                    generator.WriteManagedReadyToRunInstructionSetHelper(tw);
                }

                using (TextWriter tw = new StreamWriter(args[4]))
                {
                    Console.WriteLine("Generating {0}", args[4]);
                    generator.WriteManagedJitInstructionSet(tw);
                }

                using (TextWriter tw = new StreamWriter(args[5]))
                {
                    Console.WriteLine("Generating {0}", args[5]);
                    generator.WriteNativeCorInfoInstructionSet(tw);
                }

                using (TextWriter tw = new StreamWriter(args[6]))
                {
                    Console.WriteLine("Generating {0}", args[6]);
                    generator.WriteNativeReadyToRunInstructionSet(tw);
                }
            }
            else
            {
                if (args.Length != 8)
                {
                    Console.WriteLine("Incorrect number of files specified for generation");
                }

                IEnumerable<FunctionDecl> functions = ParseInput(new StreamReader(args[0]));

                EmitStuff(1, WriteManagedThunkInterface);
                EmitStuff(2, WriteNativeWrapperInterface);
                EmitStuff(3, WriteAPI_Names);
                EmitStuff(4, API_Wrapper);
                EmitStuff(5, SPMI_ICorJitInfoImpl);
                EmitStuff(6, SPMI_ShimCounter_ICorJitInfo);
                EmitStuff(7, SPMI_ShimSimple_ICorJitInfo);

                void EmitStuff(int index, Action<TextWriter, IEnumerable<FunctionDecl>> printer)
                {
                    using (TextWriter tw = new StreamWriter(args[index]))
                    {
                        Console.WriteLine("Generating {0}", args[index]);
                        printer(tw, functions);
                    }
                }
            }
        }
    }
}