Wednesday 21 May 2014

Dynamics AX Compiler: How it works



In Dynamics AX, compiler works in 3 phases as under:

Phase 1:
·         Signature and Types Compilation
In phase1 compiler reads the header only of each AOT element. It’s just passing over phase. It only looks class declarations and signatures. This phase creates metadata about types and their methods.  At this stage the references are not checked, instead references are ignored. E.g. a class may have a method which is referring to other class, which has not yet compiled. So, referenced objects are ignored at this stage, instead only headers of each element are compiled only in first stage.

forEach(Object in AOTList)
{
                Object.CompileHeaderOnly();
}                    

Phase 2:
·         Body Compilation and P-Code Generation
In this phase it passes over each AOT element to perform a full compile of each. The compiler validates all metadata, all references, and all X++ code. The result of this phase is p-code. Any AOT element that suffers an error is added to a list of erroneous elements.

foreach(Object in AOT)
{
       compilationResult = Object.Compile();
       if (compilationResult == failed)
       {
              ListOfErroneousElements.Add(Object);
       }
}


Phase 3:
·         Re-execution of bad elements
At the end of phase #2, the system checks ListOfErroneousElements. If the list is empty it simply quits, otherwise it recompiles the objects/ elements of ListOfErroneousElements and creates a new list as NewListOfErroneousElements.
In case comipiler recompiled objected iof ListofErroneousElements, there are come three possible results
a)      NewListOfErroneousElements.Count = 0
That refers to completed successfully. No errors.



b)      NewListOfErroneousElements.Count == ListofErroneousElements.Count
This means the set of errors is stable and should be displayed to user as error list

c)       NewListOfErroneousElements.Count < ListofErroneousElements.Count
This means that Phase3 made positive progress in minimizing the count of errors. Therefore another phase like Phase3 should be executed. This leads to Phase4, Phase5, upto PhaseN for as much iteration as positive progress continues, unless the errors become zero or list of errors is stable.

if (ListofErroneousElements.Count > 0)
{
       while(true)
       {
              foreach(Object  in ListofErroneousElements)
              {
                    compilationResult = Object  .Compile();
                     if (compilationResult == fail)
                     {
                          NewListOfErroneousElements.Add(Element)
                     }
              }
             
              if (ListofErroneousElements.Count == NewListOfErroneousElements.Count)
              {
                     break; // The list of elements with errors has been stabilized and display this to user
              }
             ListofErroneousElements = NewListOfErroneousElements.GetCopy();
             NewListOfErroneousElements.Reset();
       }
}





No comments:

Post a Comment