0% found this document useful (0 votes)
50 views

Intermediate Code Generation

The document discusses intermediate code generation in compilers and provides an example of generating MIPS assembly code from a simple calculator grammar using Flex and Bison. It includes: 1) An overview of compiler phases including intermediate code generation after semantic analysis and before code optimization and generation. 2) An example of generating MIPS assembly code to evaluate expressions based on a calculator grammar by popping operands from a stack, performing operations, and pushing results. 3) Code snippets showing how numbers, multiplications, additions, and the full program are handled in the generated MIPS assembly.

Uploaded by

Saurabh Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Intermediate Code Generation

The document discusses intermediate code generation in compilers and provides an example of generating MIPS assembly code from a simple calculator grammar using Flex and Bison. It includes: 1) An overview of compiler phases including intermediate code generation after semantic analysis and before code optimization and generation. 2) An example of generating MIPS assembly code to evaluate expressions based on a calculator grammar by popping operands from a stack, performing operations, and pushing results. 3) Code snippets showing how numbers, multiplications, additions, and the full program are handled in the generated MIPS assembly.

Uploaded by

Saurabh Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Intermediate Code Generation

Foundations of computing Oct. 21, 2013

Roxana Leontie [email protected]

Compiler Phases:
source program lexical analyzer syntax analyzer

semantic analyzer
Symbol table intermediate code generator

error handler

code optimizer
code generator target program

SPIM
Spim is a self-contained simulator that runs MIPS32 programs Spim implements almost the entire MIPS32 assembler-extended instruction set Spim files have the extension .s

SPIM - program structure


.data #constants and var declarations go here .text # Main must be global .globl main main: # your program starts here

li $v0, 10 # Syscall to exit code is 10 syscall # make the syscall


4

SPIM - declarations
.data
Assembler directive that specifies data declarations is starting Declares variable names used in a program Example: .data # word (4 bytes) x: word 0 y: word 0 z: word 0

SPIM - code
.text
Assembler directive that specifies where the program code (instructions) are located Label main: marks the starting point for code execution Ending point is marked by the exit system call: li $v0, 10 syscall

SPIM - syscall
Service print_int print_float print_double Code in $v0 1 2 3 Arguments $a0 = integer to print $f12 = float to print $f12 = double to print Results

print_string
read_int read_float read_double read_string

4
5 6 7 8

$a0 = address of string in memory


integer returned in $v0 Float returned in $v0 double returned in $v0 $a0 = memory address of the string input buffer $a1 = length of string buffer (n) $a0 = amount Address in $v0
7

sbrk exit

9 10

SPIM example: Simple Calculator


Grammar: calclist calclist exp EOL | exp factor | exp + factor | exp - factor factor factor * term | factor / term term NUMBER | ( exp )

Example Flex file


%{ # include "calc.tab.h" %} %% "+" "-" "*" "/" "(" ")" [0-9]+ \n [ \t\r] . %%

{ return ADD; } { return SUB; } { return MUL; } { return DIV; } { return OP; } { return CP; } { yylval = atoi(yytext); return NUMBER; } { return EOL; } { /* ignore white space */ } { yyerror("Mystery character %c\n", *yytext); }
9

Building the MIPS file


calclist: /* nothing */ | calclist {fprintf(output,"\n.text\nmain:\n\n");} exp EOL{ fprintf(output,"\nli\t$v0, 10\t#the end\n"); fprintf(output,"syscall\n"); } ;
10

When we read numbers


term: NUMBER{ fprintf(out,"\n\n#Add no to the stack\n"); fprintf(out, "li\t $t0, %d\t #store value\n",$1); fprintf(out, "subu\t $sp, $sp, 4\t #move the stack ptr\n"); fprintf(out, "sw\t $t0,($sp)\t #push!\n"); } | OP exp CP { $$ = $2; } ;
11

Multiplications
factor: term | factor MUL term { fprintf(out,"\n\n### DO MULTIPLICATION ###\n"); fprintf(out, "lw\t$t0, ($sp)\t#pop 1st!\n"); fprintf(out, "addu\t$sp, $sp, 4\t#move the ptr\n"); fprintf(out, "lw\t$t1, ($sp)\t#pop 2nd!\n"); fprintf(out, "addu\t$sp, $sp, 4\t#move the ptr\n"); fprintf(out, "mul\t$t0, $t1, $t0\t#multiply\n"); fprintf(out, "subu\t$sp, $sp, 4\t#move the stack ptr\n"); fprintf(out, "sw\t$t0, ($sp)\t#push!\n"); }
12

Addition
exp: factor | exp ADD factor { fprintf(output,"\n\n### DO ADDITION ###\n"); fprintf(out, "lw\t$t0, ($sp)\t#pop 1st!\n"); fprintf(out, "addu\t$sp, $sp, 4\t#move the ptr\n"); fprintf(out, "lw\t$t0, ($sp)\t#pop 2nd!\n"); fprintf(out, "addu\t$sp, $sp, 4\t#move the ptr\n"); fprintf(out, "add\t$t0, $t1, $t0\t#add\n"); fprintf(out, "subu\t$sp, $sp, 4\t#move the stack ptr\n"); fprintf(out, "sw\t$t0, ($sp)\t#push!\n");
13

Lets try it out!!!!

You might also like