ir report final

31
7/31/2019 Ir Report Final http://slidepdf.com/reader/full/ir-report-final 1/31

Upload: anand-g-hegde

Post on 05-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 1/31

Page 2: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 2/31

: To generate the intermediate code representation for the various

inputs.

:

Intermediate Code is generated using the Parse rule producing a target

language from the input language. The input to the code generator

typically consists of a parse tree or an abstract syntax tree. The tree is

converted into a linear sequence of instructions, usually in an

intermediate language such as three address code.

Utility of Intermediate Code Generation

1. Suppose we have n-source languages and m Target languages.

Without Intermediate code we will have to change each source language

into target language directly. So, for each source-target pair we will

need a compiler. Hence we will require (n*m) Compilers, one for each

pair. If we Use Intermediate code We will require n-Compilers to convert

each source language into Intermediate code and m-Compilers to

convert Intermediate code into m-target languages. Thus we require

only (n+m) Compilers.

2. Retargeting is facilitated: A compiler for a different machine can be

created by attaching a Back-end (which generates Target Code) for the

new machine to an existing Front-end (which generates Intermediate

Code).

3. A machine Independent Code-Optimizer can be applied to the

Intermediate code.

Page 3: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 3/31

4. Intermediate code is simple enough to be easily converted to any

target code.

5. It is also complex enough to represent all the complex structure of

high level language.

:

The inputs are scanned using lexical analyzer. The tokens found are fed

to the YACC for syntax and semantic analysis. The three address code

is generated using the tables built during the semantic analysis. The

code for the lexical analysis is shown below.

%{

#include "global.h"

#include "y.tab.h"

void print(char *s){

}

%}

%%

\/\/[^\n]* {}do { print("do"); return DO; }

else { print("else"); return ELSE; }

if { print("if"); return IF; }return { print("return"); return RETURN; }

while { print("while"); return WHILE; }

void { print("void"); return VOID; }

float { print("float"); return FLOAT; }int { print("int"); return INT; }

([1-9][0-9]*|0)(\.[0-9]+)? { print("const"); strcpy(yylval.str,yytext); return CONSTANT; }

[a-zA-Z_][a-zA-Z_0-9]* { print("id"); strcpy(yylval.str,

yytext); return IDENTIFIER; }\( {print("("); return '('; }

Page 4: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 4/31

\) {print(")"); return ')'; }

\} {print("}"); return '}'; }\{ {print("{"); return '{'; }

\; {print(";"); return ';'; }

\+\+ {print("++"); return INC_OP; }\-\- { print("--"); return DEC_OP; }

\=\= { print("=="); return EQUAL; }

\+ { print("+"); return '+'; }\- { print("-"); return '-'; }

\* { print("*"); return '*'; }

\/ { print("/"); return '/'; }\% { print("%%"); return '%'; }

\! { print("!"); return '!'; }

\= { print("="); return '='; }

\&\& { print("&&"); return LOG_AND; }\|\| { print("|"); return LOG_OR; }

\!\= { print("!="); return NOT_EQUAL; }\>\= { print(">="); return GREATER_OR_EQUAL; }

\<\= { print("<="); return LESS_OR_EQUAL; }

\> { print(">"); return '>'; }\< { print("<"); return '<'; }

\, { print(","); return ','; }

\<\< { print("<<"); return SHIFTLEFT; }

[ \t\n]+ {}

The grammar used by the YACC and the semantic analysis is shown

below

%{#include <stdio.h>#include "global.h"

#include <stdlib.h>

#include <string.h>

void yyerror(char * message);

Page 5: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 5/31

int rel_addr = 0;

symtabEntry *current_function = NULL;int instructionCounter = 0;

char instructions[10000][1000];

int get_type_size(symtabEntryType type) {

return type == INTEGER ? 4 : 8; // int and real are of thesize 4

}

void patch(int ic) {

sprintf(instructions[ic],"%s

%d",instructions[ic],instructionCounter);

}

symtabEntry* declare_function(char* name, symtabEntryTypereturnType) {

symtabEntry* f = lookup(name);

if(f){if(f->internType != returnType) {

yyerror("Function defined twice with differing

return type.");

}}

else {

f = addSymboltableEntry(theSymboltable, name, FUNC,returnType, 0, 0, 0, 0, 0, -1);

}

return f;}

symtabEntry* variable_lookup(char* id){

symtabEntry* e = lookup(id);

return (e != NULL && e->vater == current_function) ? e :NULL;

}symtabEntry* create_variable(symtabEntryType type, char* name) {

if(variable_lookup(name)) {

yyerror("Variable defined twice.");}

Page 6: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 6/31

symtabEntry* entry = addSymboltableEntry(theSymboltable,

name, type, NOP, rel_addr, 0, 0, 0, current_function, 0);rel_addr += get_type_size(type);

return entry;

}

symtabEntry* create_helper_variable(symtabEntryType type) {

static help_num = 0; // current helper variable numberchar* str = malloc(1000);

sprintf(str,"V__H%d",help_num);

++help_num;return create_variable(type,str);

}

symtabEntry* create_parameter(symtabEntryType type, char* name,int param) {

symtabEntry* v = variable_lookup(name);

if(!v) {

v = create_variable(type,name);v->parameter = param;

} else {

if(v->type != type){

yyerror("Function parameters differ in type.");}

}

return v;}

int

print_if (symtabEntry* check){

sprintf(instructions[instructionCounter],"if (%s = 0)

goto",check->name);

return instructionCounter++;}

int

print_goto ()

{sprintf(instructions[instructionCounter],"goto");

Page 7: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 7/31

return instructionCounter++;

}

void

print_full_not_if (symtabEntry* check, int target){

sprintf(instructions[instructionCounter],"if (%s = 0) goto

%d",check->name,instructionCounter+2);++instructionCounter;

sprintf(instructions[instructionCounter++],"goto

%d",target);}

void

print_full_goto (int target){

sprintf(instructions[instructionCounter++],"goto%d",target);

}

symtabEntry*

print_binary_expression (char* op, symtabEntryType type,

symtabEntry* a, symtabEntry* b)

{symtabEntry* c = create_helper_variable(type);

sprintf(instructions[instructionCounter++],"%s := %s %s

%s",c->name,a->name,op,b->name);

return c;

}

symtabEntry*

print_unary_expression (char* op, symtabEntryType type,

symtabEntry* a){

symtabEntry* c = create_helper_variable(type);sprintf(instructions[instructionCounter++],"%s := %s %s",c-

>name,op,a->name);

return c;}

Page 8: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 8/31

 

symtabEntry* print_cast(symtabEntry* a, symtabEntryType castTo){

if(a->type != castTo){

a =print_unary_expression(castTo==REAL?"tofloat":"toint",castTo,a);

}

return a;}

symtabEntry*print_binary_cast_expression ( char* op, symtabEntry* a,

symtabEntry* b)

{

symtabEntryType type = (a->type == INTEGER && b->type ==INTEGER)?INTEGER:REAL;

// cast

if(type == REAL) {

a = print_cast(a, REAL);b = print_cast(b, REAL);

}

return print_binary_expression(op,type,a,b);}

symtabEntry*print_binary_integer_only_expression (char* op, symtabEntry* a,

symtabEntry* b)

{if(a->type != INTEGER || a->type != INTEGER){

yyerror("Passing non integer to interger only

operation");

}return print_binary_expression(op,INTEGER,a,b);

}

symtabEntry* print_constant_assignment(symtabEntryType type,

char* value) {symtabEntry* c = create_helper_variable(type);

Page 9: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 9/31

sprintf(instructions[instructionCounter++],"%s := %s",c-

>name,value);return c;

}

symtabEntry* print_variable_assignment(symtabEntry*

a,symtabEntry* b) {

sprintf(instructions[instructionCounter++],"%s := %s",a->name,b->name);

return a;

}

symtabEntry*

print_left_shift(symtabEntry* a,symtabEntry* b){

symtabEntry* c = create_helper_variable(INTEGER);symtabEntry* r = create_helper_variable(INTEGER);

print_variable_assignment(c,b);print_variable_assignment(r,a);

sprintf(instructions[instructionCounter],"if (%s <= 0) goto

%d",c->name, instructionCounter+4);sprintf(instructions[instructionCounter+1],"%s := %s *

2",r->name,r->name);

sprintf(instructions[instructionCounter+2],"%s := %s -

1",c->name,c->name);sprintf(instructions[instructionCounter+3],"goto

%d",instructionCounter);

instructionCounter += 4;return r;

}

symtabEntry*

print_logical_if(char* op,symtabEntry* a,symtabEntry* b){

symtabEntry* c = create_helper_variable(INTEGER);

sprintf(instructions[instructionCounter],"if (%s %s %s)goto %d",a->name, op, b->name, instructionCounter+3);

sprintf(instructions[instructionCounter+1],"%s := %s",c->name,"0");

sprintf(instructions[instructionCounter+2],"goto

%d",instructionCounter+4);

Page 10: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 10/31

sprintf(instructions[instructionCounter+3],"%s := %s",c-

>name,"1");instructionCounter += 4;

return c;

}

void print_pass_param(symtabEntry* a) {

sprintf(instructions[instructionCounter++],"param %s",a->name);

}

symtabEntry* print_function_call(symtabEntry* f, int params){

if(f->internType == NOP) {

sprintf(instructions[instructionCounter++],"call %s,

%d",f->name,params);return NULL;

} else {symtabEntry* r = create_helper_variable(f->internType);

sprintf(instructions[instructionCounter++],"%s := call

%s, %d",r->name,f->name,params);return r;

}

}

void print_conditional_jump(symtabEntry* boolean) {

sprintf(instructions[instructionCounter++],"if (%s = 0) goto

M",boolean);}

void print_return(symtabEntry* a) {if(a == NULL) { // void

if(current_function->internType != NOP) {

yyerror("Returning nothing from non-void

function.");}

sprintf(instructions[instructionCounter++],"return");} else { // non void

if(current_function->internType == NOP) {

yyerror("Void function may not return a value.");}

Page 11: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 11/31

a = print_cast(a, current_function->internType);

sprintf(instructions[instructionCounter++],"return

%s",a->name);

}}

%}

%token CONSTANT%token DO

%token ELSE

%token FLOAT

%token IDENTIFIER%token IF

%token INT%token RETURN

%token VOID

%token WHILE

%token '{'

%token '}'

%token ';'

%token '('

%token ')'%token ','

%right '='

%left LOG_AND

%left LOG_OR

%left SHIFTLEFT

%left NOT_EQUAL%left EQUAL

%left GREATER_OR_EQUAL

%left LESS_OR_EQUAL%left '<'

Page 12: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 12/31

%left '>'

%left '+'

%left '-'

%left '*'

%left '/'

%left '%'

%left DEC_OP

%left INC_OP%left '!'

%left U_MINUS

%left U_PLUS

%type <type> INT

%type <type> FLOAT%type <type> VOID

%type <type> var_type

%type <str> id

%type <type> declaration

%type <entry> expression

%type <str> CONSTANT%type <entry> assignment

%type <integer> exp_list

%type <integer> parameter_list%type <integer> if_start

%type <integer> else_start

%type <integer> while_start%type <integer> do_start

%union

{char str[1000];

int integer;float real;

symtabEntryType type;

symtabEntry* entry;}

Page 13: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 13/31

 

%%

programm

: function| programm function

;

function_start

: var_type id

{current_function = declare_function($2,$1);

}

'(' parameter_list ')'

{if(current_function->parameter == -1){

current_function->parameter = $5;} else {

if(current_function->parameter != $5){

yyerror("Function declared again with wrongamount of parameters.");

}

}

};

function: function_start ';'

| function_start

{current_function->line = instructionCounter;

rel_addr = current_function->parameter*4;

}

function_body{

if(current_function->internType == NOP){print_return(NULL);

}

current_function->offset = rel_addr;}

Page 14: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 14/31

;

function_body

: '{' statement_list '}'

| '{' declaration_list statement_list '}';

declaration_list: declaration ';'

| declaration_list declaration ';'

;

declaration

: INT id

{create_variable(INTEGER,$2);

$$ = INTEGER;}

| FLOAT id

{create_variable(REAL,$2);

$$ = REAL;

}

| declaration ',' id{

create_variable($1,$3);

$$ = $1}

;

parameter_list

: INT id

{

$$ = 1;create_parameter(INTEGER,$2,$$);

}| FLOAT id

{

$$ = 1;create_parameter(REAL,$2,$$);

Page 15: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 15/31

}

| parameter_list ',' INT id{

$$ = $1+1;

create_parameter(INTEGER,$4,$$);}

| parameter_list ',' FLOAT id

{$$ = $1+1;

create_parameter(REAL,$4,$$);

}| VOID

{ $$ = 0; }

|

{ $$ = 0; };

var_type

: INT {$$ = INTEGER;}

| VOID {$$ = NOP;}| FLOAT {$$ = REAL;}

;

statement_list: statement

| statement_list statement

;

statement

: matched_statement| unmatched_statement

;

if_start: IF '(' assignment ')'

{$$ = print_if($3);

}

;

Page 16: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 16/31

else_start

: if_start matched_statement ELSE{

$$ = print_goto();

// backpatch ifpatch($1);

}

;

while_start

: WHILE '(' assignment ')'{

$$ = print_if($3);

}

;

do_start: DO

{

$$ = instructionCounter;}

;

matched_statement: else_start matched_statement

{

patch($1);}

| assignment ';'| RETURN ';'

{

print_return(NULL);

}| RETURN assignment ';'

{print_return($2);

}

| while_start matched_statement{

Page 17: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 17/31

print_full_goto($1);

patch($1);}

| do_start statement WHILE '(' assignment ')' ';'

{print_full_not_if($5,$1);

}

| '{' statement_list '}'| '{''}'

;

unmatched_statement

: if_start statement

{

patch($1);}

| else_start unmatched_statement{

patch($1);

}| while_start unmatched_statement

{

print_full_goto($1);

patch($1);}

;

assignment

: expression

| id '=' expression{

$$ = variable_lookup($1);

if($$ == NULL){

yyerror("Assignment to undeclared variable.");}

print_variable_assignment($$,$3);}

;

expression

Page 18: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 18/31

: INC_OP expression

{symtabEntry* c = print_constant_assignment($2->type, "1");

$$ = print_binary_expression("+",$2->type,$2,c);

}| DEC_OP expression

{

symtabEntry* c = print_constant_assignment($2->type, "1");$$ = print_binary_expression("-",$2->type,$2,c);

}

| expression LOG_OR expression{

$$ = print_binary_integer_only_expression("+",$1,$3);

}

| expression LOG_AND expression{

$$ = print_binary_integer_only_expression("*",$1,$3);}

| expression NOT_EQUAL expression

{$$ = print_logical_if("!=",$1,$3);

}

| expression EQUAL expression

{$$ = print_logical_if("=",$1,$3);

}

| expression GREATER_OR_EQUAL expression{

$$ = print_logical_if(">=",$1,$3);

}| expression LESS_OR_EQUAL expression

{

$$ = print_logical_if("<=",$1,$3);

}| expression '>' expression

{$$ = print_logical_if(">",$1,$3);

}

| expression '<' expression{

Page 19: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 19/31

$$ = print_logical_if("<",$1,$3);

}| expression SHIFTLEFT expression

{

$$ = print_left_shift($1,$3);}

| expression '+' expression

{$$ = print_binary_cast_expression("+",$1,$3);

}

| expression '-' expression{

$$ = print_binary_cast_expression("-",$1,$3);

}

| expression '*' expression{

$$ = print_binary_cast_expression("*",$1,$3);}

| expression '/' expression

{$$ = print_binary_cast_expression("/",$1,$3);

}

| expression '%' expression

{$$ = print_binary_integer_only_expression("%",$1,$3);

}

| '!' expression{

symtabEntry* c = print_constant_assignment($2->type, "1");

$$ = print_binary_expression("-",$2->type,c,$2);}

| '+' expression %prec U_PLUS

{$$ = $2

}| '-' expression %prec U_MINUS

{

$$ = print_unary_expression("-",INTEGER,$2);}

Page 20: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 20/31

 

| CONSTANT{

$$ = print_constant_assignment(strchr($1,'.')==NULL ?

INTEGER:REAL, $1);}

| '(' expression ')'

{$$ = $2;

}

| id '(' exp_list ')' {symtabEntry* e = lookup($1);

if(e == NULL) {

yyerror("Trying to call undecared function");

}$$ = print_function_call(e,$3);

}| id

{

$$ = variable_lookup($1);if($$ == NULL){

yyerror("Usage of undeclared variable.");

}

};

exp_list: {$$ = 0;}

| expression {print_pass_param($1); $$ = 1;}

| exp_list ',' expression {print_pass_param($3); $$ =++$1}

;

id: IDENTIFIER { strcpy($$,yylval.str); }

;%%

int main() {

declare_function("main", INTEGER);yyparse();

Page 21: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 21/31

 

int i;

printf("\nINTERMEDIATE CODE\n-------------------------------

----\n");

for(i=0 ; i<instructionCounter ; ++i){printf("%d\t%s\n",i,instructions[i]);

}

return 0;

}

void yyerror(char * message) {printf("error message <%s>\n",message);

exit(1);}

The symbol table entries and the attributes of the symbols are handled

by the following routines

//global.c

#include <stdlib.h>#include <stdio.h>

#include <string.h>#include "global.h"

// GLOBAL VARIABLES:symtabEntry * theSymboltable=0; //pointer as a entry to the

Symboltable, which is a linked list

void writeSymboltable (symtabEntry * Symboltable, FILE *outputFile){

fprintf (outputFile, "SYMBOLS\n");

fprintf (outputFile, "-----------------------------------\n");

fprintf (outputFile, "Name TypeInt_Typ Offset\tLine\tIndex1\tIndex2\tParent\tParameter\n");

fprintf (outputFile, "-------------------------------------

--------------------------------------------------------\n");

//variablessymtabEntry * currentEntry;

int j;

Page 22: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 22/31

char helpString[21];

currentEntry = Symboltable;

do{

//walks through the Symboltable

strncpy(helpString,currentEntry->name,20);

for(j=19;j>=strlen(currentEntry->name);j--){//loop for formating the output to file

helpString[j]=' ';

}helpString[20]=0;

fprintf(outputFile, "%s\t",helpString);

if(currentEntry->type == FUNC && strcmp(currentEntry->name,"main") == 0){

fprintf(outputFile, "Main ");} else {

getSymbolTypePrintout(currentEntry-

>type,helpString);fprintf(outputFile, "%s",helpString);

}

getSymbolTypePrintout(currentEntry->internType,helpString);

fprintf(outputFile, "%s",helpString);

fprintf(outputFile, "%d\t\t",currentEntry->offset);

fprintf(outputFile, "%d\t\t",currentEntry->line);

fprintf(outputFile, "%d\t\t",currentEntry->index1);fprintf(outputFile, "%d\t\t",currentEntry->index2);

if(currentEntry->vater){

fprintf(outputFile, "%s\t\t",currentEntry->vater-

>name);}else{

fprintf(outputFile, "None\t\t");}

fprintf(outputFile, "%d\t\t\n",currentEntry-

>parameter);

Page 23: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 23/31

fflush(outputFile);

currentEntry=currentEntry->next;

}while(currentEntry);

}

symtabEntry* lookup(char* id) {symtabEntry* e = theSymboltable;

while(e) {if(strcmp(id,e->name) == 0){

return e;

}

e = e->next;}

return NULL;

}

void getSymbolTypePrintout(symtabEntryType type, char *

writeIn){

//puts the printout for a given SymbolEntrytype to the givenstring

switch(type){

case PROG: strcpy(writeIn,"Prg ") ;break;case NOP : strcpy(writeIn,"None ") ;break;

case REAL: strcpy(writeIn,"Real ") ;break;

case BOOL: strcpy(writeIn,"Bool ") ;break;case INTEGER : strcpy(writeIn,"Int ") ;break;

case ARR : strcpy(writeIn,"Array ") ;break;

case FUNC: strcpy(writeIn,"Func ") ;break;

case PROC: strcpy(writeIn,"Proc ") ;break;case PRG_PARA: strcpy(writeIn,"P.Prmtr ") ;break;

default: strcpy(writeIn," ") ;break;}

}

int globalNumber = 0;

Page 24: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 24/31

 

symtabEntry* addSymboltableEntry (symtabEntry * Symboltable,char * name,

symtabEntryType type,

symtabEntryType internType,int offset,

int line,

int index1,int index2,

symtabEntry * vater,

int parameter){//adds a symbolEntry to the end of the Symboltable. If

there global variable theSymboltable is not

//initialized, this will be done with the first call of

this function

symtabEntry * newSymtabEntry = (symtabEntry*) malloc(sizeof (symtabEntry));

newSymtabEntry->number = ++globalNumber;

//allocates the memory for the new symtabEntry

newSymtabEntry->name = (char *) malloc (strlen(name) +1);

strcpy(newSymtabEntry->name,name);

newSymtabEntry->type = type;

newSymtabEntry->internType = internType;newSymtabEntry->offset = offset;

newSymtabEntry->line = line;

newSymtabEntry->index1 = index1;newSymtabEntry->index2 = index2;

newSymtabEntry->vater = vater;

newSymtabEntry->parameter = parameter;

newSymtabEntry->next = 0;

if (!Symboltable){

//there is no entry in the Symboltable

theSymboltable = newSymtabEntry;}else{

Page 25: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 25/31

//there is at least one entry in the Symboltable

symtabEntry * symtabHelp = Symboltable;while (symtabHelp->next){

//walks to the last entry of Symboltable

symtabHelp = symtabHelp->next;}

symtabHelp->next = newSymtabEntry;

}return newSymtabEntry;

}

The code increments the value of the counter as and when it

encounters a new symbol. And corresponding to iTH symbol it creates a

helper variable V_Hi. This helper variable is also entered into the symboltable.

On running the code for testing, the following output was obtained.

………………………………………./*input code*/…………………………………………………………………………………………...

int main(void){

int a;int b;int c;

int i;

int d;

int e;i=0;

c=a+b+e;while(i<=c) {

d=c+i; i=i+1;

}}………………………………………………………/*input ends*/…………………………………………………………………………………

The output for above code is shown below

…………………………………………………./*output*/……………………………..

 INTERMEDIATE CODE

-----------------------------------

Page 26: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 26/31

0 V__H0 := 0

1 i := V__H0

2 V__H1 := a + b

3 V__H2 := V__H1 + e

4 c := V__H2

5 if (i <= c) goto 8

6 V__H3 := 0

7 goto 9

8 V__H3 := 1

9 if (V__H3 = 0) goto 16

10 V__H4 := c + i

11 d := V__H4

12 V__H5 := 1

13 V__H6 := i + V__H5

14 i := V__H6

15 goto 9

………………………………………………./*output ends here*/………………………………..

In second run the following input was given

…………………………………………………/*test input 2*/…………………………….

Page 27: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 27/31

float questionmark (int x, int y);

int main (void){

int variable_1;int variable_2;

float variable_3;

variable_1 = 1;

variable_2 = 2;

while (variable_1 <= 10 && 1){

variable_3 = questionmark (variable_1, variable_2);

variable_2 = ++variable_1;

variable_2 = variable_2 << 2;}

if(! variable_3){return 0;

}else{

return 1;

}}

float questionmark (int x, int y){

float result;

do{

if (y/x){

result = 1.0;

}--y;

}while( y < x || 0);

return result;

}…………………………………………/*input 2 ends*/…………………………………………………………………………………

Page 28: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 28/31

The output is shown below

…………………………………………/*output2*/……………………………………………………………………………………………

 INTERMEDIATE CODE

-----------------------------------

0 V__H0 := 1

1 variable_1 := V__H0

2 V__H1 := 2

3 variable_2 := V__H1

4 V__H2 := 10

5 if (variable_1 <= V__H2) goto 8

6 V__H3 := 0

7 goto 9

8 V__H3 := 1

9 V__H4 := 1

10 if (variable_1 > V__H4) goto 13

11 V__H5 := 0

12 goto 14

13 V__H5 := 1

14 V__H6 := V__H3 * V__H5

15 if (V__H6 = 0) goto 32

16 param variable_1

17 param variable_2

Page 29: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 29/31

18 V__H7 := call questionmark, 2

19 variable_3 := V__H7

20 V__H8 := 1

21 V__H9 := variable_1 + V__H8

22 variable_2 := V__H9

23 V__H10 := 4

24 V__H11 := V__H10

25 V__H12 := variable_2

26 if (V__H11 <= 0) goto 30

27 V__H12 := V__H12 * 2

28 V__H11 := V__H11 - 1

29 goto 26

30 variable_2 := V__H12

31 goto 15

32 V__H13 := 1

33 V__H14 := V__H13 - variable_3

34 if (V__H14 = 0) goto 38

35 V__H15 := 0

36 return V__H15

37 goto 40

38 V__H16 := 1

39 return V__H16

40 V__H17 := y / x

Page 30: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 30/31

41 if (V__H17 = 0) goto 44

42 V__H18 := 1.0

43 result := V__H18

44 V__H19 := 1

45 V__H20 := y - V__H19

46 if (y < x) goto 49

47 V__H21 := 0

48 goto 50

49 V__H21 := 1

50 if (y = x) goto 53

51 V__H22 := 0

52 goto 54

53 V__H22 := 1

54 V__H23 := V__H21 + V__H22

55 if (V__H23 = 0) goto 57

56 goto 40

57 return result

…………………………………………/*output2ends*/…………………………………………………………………………………………

: As the above outputs indicate, the implemented

Intermediate code generator successfully parses the input files toproduce the Three Address Code output.

We plan to add the following features to our compiler

-

Page 31: Ir Report Final

7/31/2019 Ir Report Final

http://slidepdf.com/reader/full/ir-report-final 31/31

1.  Allow use of global variables.

2.  Allow the use of preprocessor directives.

3.  Allow use of for loops. Currently this compiler only allows use of

while and do while loop.

4.  Include switch-case handling.