Tuesday 6 June 2017

Explain algorithm for Generating Assembly Code from DAG.


 Algorithm for Generating Assembly Code:
(Say, R is a Stack consists of registers)
void gencode(Node)
{
if Node is intermediate node of tree(DAG)

{
    Case 1: if Node's left child's label == 1 && Node's right child's label == 0 && Node's left child is leaf node && Node's right child is leaf node     

     {
       print "MOV Node's left child's data,R[top]"
       print "op Node's right child's data,R[top]"
     }

   Case 2: else if Node's left child's label >= 1 && Node's right child's label == 0
     {
       gencode(Node's left child);
       print "op Node's right child's data,R[top]"

     }

  Case 3: else if Node's left child's label < Node's right child's label

      {
       int temp;
       Swap Register Stack's top and second top element;
       gencode(Node's right child);
       temp=pop();
       gencode(Node's left child);
       push(temp); 

       Swap Register Stack's top and second top element; 
       print "op R[top-1],R[top]"
     }

  Case 4: else if Node's left child's label >= Node's right child's label

      {
       int temp;
       gencode(Node's left child);
       temp=pop();
       gencode(Node's right child);
       push(temp);
       print "op R[top-1],R[top]"

     }   

}

else if Node is leaf node and it is left child of it's immediate parent
{
  print "MOV Node's data,R[top]"

}

}

No comments:

Post a Comment