gavelino

I want to build a compiler to generate msil code, but I doní»t know how to translate intermediate representation (IR) to Msil code. How I can to generate Msil code from PEModuleUnit Is there a Msil writer


Re: Phoenix Msil code

Andy Ayers - MSFT

Yes, the RDK includes support for generating MSIL. The main components are the MSIL architecture and runtime; these are shipped as architecture-msil.dll and runtime-vccrt-win-msil.dll.

As a high-level overview, you need to do the following:

  1. Register the architecture and runtime.
  2. Create a compilation-unit container (ModuleUnit or PEModuleUnit) to hold the results.
  3. For each function to compile:
    1. Create a FunctionUnit with this architecture and runtime.
    2. Populate HIR in the FunctionUnit to model the program you are compiling.
    3. Invoke the proper sequence of phases to translate the HIR into LIR and eventually encoded IR (EIR)
    4. Write the EIR out to the proper place in the container (typically a section or set of sections).
  4. Write out the compilation-unit container to the proper kind of external file.

Step 3.2 is the complicated one -- how complicated really depends on the characteristics of your source language.

You can see this sequence of steps in the hello sample, albeit for x86 and not MSIL. You might also want to look at the CHAD compiler documentation that comes with the RDK -- in the samples topic there is an example implementation of a compiler for a simple language.






Re: Phoenix Msil code

gavelino

Which sequence of phases should I use to translate the HIR to LIR and encoded IR Does Phoenix provide all Do I need to build any




Re: Phoenix Msil code

Andy Ayers - MSFT

If you compile something -clr -c -d2traceallphases, you'll get an idea of the phases c2 runs:

C:\home>cl -clr -d2traceallphases -c hello.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.42
for Microsoft (R) .NET Framework version 2.00.50727.42
Copyright (C) Microsoft Corporation. All rights reserved.

hello.cpp
Phase CxxIL Reader (control cil)
Phase Inliner (control inline)
Phase Type Checker (control chktype)
Phase LIR Phases (no control) [PhaseList]
Phase MIR Lower (control MirLower)
Phase Uninitialized Local Use Detection Phase (control uninit)
Phase Canonicalize (control Canonicalize)
Phase Lower (control Lower)
Phase Switch Lower (control SwitchLower)
Phase StackOptimization (control MSILStackify)
Phase Stack Allocation (control StackAllocate)
Phase Frame Generation (control Frame)
Phase Block Layout (control BlockLayout)
Phase Flow Optimization (control FlowOptimization)
Phase Finish EH Lower (control finishehlower)
Phase Encoding (control Encode)
Phase Emission (control Encode)
Phase Emit Referenced Symbols (control Encode)
Phase Assembly Listing (control Lister)

Assuming you start with valid HIR, you can safely pare the list down to something like the following:

Phase MIR Lower (control MirLower)
Phase Canonicalize (control Canonicalize)
Phase Lower (control Lower)
Phase Switch Lower (control SwitchLower)
Phase StackOptimization (control MSILStackify)
Phase Stack Allocation (control StackAllocate)
Phase Frame Generation (control Frame)
Phase Block Layout (control BlockLayout)
Phase Flow Optimization (control FlowOptimization)
Phase Finish EH Lower (control finishehlower)
Phase Encoding (control Encode)
Phase Emission (control Encode)