asfermiStructure - hyqneuron/asfermi GitHub Wiki

#summary Organisation of code

This page is written by hyq.neuron for the other asfermi team members.

General Organisation

The entire body of the code can be divided into 4 sections: 0. Body (asfermi.cpp) 0. DataTypes (SubString.h, DataTypes.h, Cubin.h) 0. Rules (Rules*.h) 0. Helper functions (helper*.h)

Body

asfermi.cpp contains these functions: 0. main: program entry point 0. ProcessCommandsAndReadSource: processes command line options and read source file 0. Initialize: initializes all the rules 0. OrganiseRules: function called by Initialize to help with the initialization process. 0. WriteToCubinDirectOutput: function called by main for cubin output 0. WriteToCubinReplace: function called by main for cubin output(replace mode)

Things to take note of

  1. source code: entirety of the source code is stored in a global char array, csSource(GlobalVariables.h). The length of csSource is the length of the source file + 1 because sometimes asfermi may need to append a null character at the end to indicate an end-of-line.
  2. Lines: the source file is broken into lines, which are stored in the structure Line. However, a Line structure itself does not contain the string. Instead, it contains a SubString structure, LineString.
  3. SubString: SubString contains a char pointer, Start, to the beginning of the sub-string. It also contains an integer number, Length, that records the length of this sub-string. Individual characters can be accessed using the square bracket notation.

Basic flow

Suppose we have the following source file

!Kernel kernel1
MOV R1, c [0x1] [0x100];
EXIT;
!EndKernel

And we assemble it with the following command line

asfermi example.txt -o example.cubin

Here"s the sequence of things that asfermi will do: 0. Process command line. (asfermi.cpp, ProcessCommandsAndReadSource) 0. Read source file into csSource, a global char array. (asfermi.cpp, ProcessCommandsAndReadSource) 0. csSource is then divided into Lines, and comments are removed from lines at this stage (helperMain.h, hpReadSource) 0. Initialize instruction and directive rules (asfermi.cpp, Initialize) 0. Add all instruction rules and directive rules to the lists csInstructionRulePrepList and csDirectiveRulePrepList respectively. (Initialize) 0. Compute index for each instruction based on its instruction name (asfermi.cpp, OrganiseRules) 0. Sort the indices and copy instruction rules in ascending order of their indices into the array csInstructionRules. (asfermi.cpp, OrganiseRules) 0. Same goes for the directive rules 0. Call the csMasterParser"s Parse function, which iterates through all the lines 0. call the csLineParser->Parse(line) for each line.