View on GitHub

JST

By Janelle Blankenburg, Shubham Gogna, and Terence Henriod
UNR CS 660: Compiler Construction

Download this project as a .zip file Download this project as a tar.gz file

Recursive Factorial

Input Source

int factorial(int x);

int main() {
  int x = 5;
  int result = factorial(x);

  return 0;
}

int factorial(int x) {
  if (x > 1) {
    return factorial(x - 1);
  } else {
    return 1;
  }
}

3AC (Three-Address Code)

DATA           , -              , -              , -              
TEXT           , -              , -              , -              
CALL           , main           , 8              , -              
LLAC           , 8              , -              , -              
BR             , PROG_END       , -              , -              
# 
#             int factorial(int x);
# 
#             int main() {
main:
#               int x = 5;
ADDIU          , ireg_00000     , $zero          , 5              
SW             , 0($FP)         , ireg_00000     , -              
#               int result = factorial(x);
CALL           , factorial      , 4              , -              
LW             , ireg_00002     , 0($fp)         , -              
SW             , ireg_00002     , 0($FP)         , -              
JAL            , factorial      , -              , -              
LLAC           , 4              , -              , -              
SW             , 4($FP)         , ireg_00001     , -              
# 
#               return 0;
ADDIU          , ireg_00003     , $zero          , 0              
RETURN         , ireg_00003     , -              , -              
JR             , $RA            , -              , -              
#             }
# 
#             int factorial(int x) {
factorial:
#               if (x > 1) {
LW             , ireg_00004     , 0($fp)         , -              
ADDIU          , ireg_00005     , $zero          , 1              
GT             , ireg_00006     , ireg_00004     , ireg_00005     
ADDI           , ireg_00007     , $zero          , 0              
BRNE           , label_00000    , ireg_00006     , ireg_00007     
#                 return factorial(x - 1);
#               } else {
#                 return 1;
ADDIU          , ireg_00008     , $zero          , 1              
RETURN         , ireg_00008     , -              , -              
BR             , label_00001    , -              , -              
label_00000:
CALL           , factorial      , 4              , -              
LW             , ireg_00010     , 0($fp)         , -              
ADDIU          , ireg_00011     , $zero          , 1              
SUB            , ireg_00012     , ireg_00010     , ireg_00011     
SW             , ireg_00012     , 0($FP)         , -              
JAL            , factorial      , -              , -              
LLAC           , 4              , -              , -              
RETURN         , ireg_00009     , -              , -              
label_00001:
JR             , $RA            , -              , -              
PROG_END:
#               }
#             }
#

Generated AST