Tutorial Code Generation Compiler Construction 1 S C I E N C E ■ P A S S I O N ■ T E C H N O L O G Y www.tugraz.at Tutorial 3: Code Generation Univ.-Prof. Dr. Franz Wotawa, DI Roxane Koitz, Stephan Frühwirt, Christopher Liebmann, Martin Zimmermann Institute for Software Technology
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Tutorial Code Generation
Compiler Construction1
S C I E N C E ■ P A S S I O N ■ T E C H N O L O G Y
u www.tugraz.at
Tutorial 3: Code Generation
Univ.-Prof. Dr. Franz Wotawa, DI Roxane Koitz,
Stephan Frühwirt, Christopher Liebmann, Martin
Zimmermann
Institute for Software Technology
Tutorial Code Generation
Compiler Construction2
Jova Input Program
Java Byte Code
Compiler Phases
Tutorial Code Generation
Compiler Construction3
Jova Input Program
Java Byte Code
Compiler Phases
Tutorial Code Generation
Compiler Construction4
Source Code
(*.jova file)Syntax tree
Grammar
(Jova.g4)
ANTLR
Lexer and Parser
Lexical and
syntactical errors
Symbol
table
ANTLR
visitor / listener
Type checking
errors
Debugging
output
Debugging
output
Workflow (1/2)
Tutorial Code Generation
Compiler Construction5
Syntax tree
Symbol
table
Workflow (2/2)
Jasmin code
(.j file)
Byte code
(.class file)
Jasmin
Tutorial Code Generation
Compiler Construction6
What is Jasmin?
• Assembler for Java bytecode
• Input: <filename>.j file
• contains assembly of the original code
• written in Jasmin assembler language
• Output: executable Java .class file
Tutorial Code Generation
Compiler Construction7
Pipeline
Create
Assembly
• Produce jasmin assembly
• Result: <filename>.j
Convert
• Invoke “java –jar jasmin.jar <filename>.j“
• Result: <filename>.class
Execute
• Invoke “java <filename>“
• Result: executes main method of class
Tutorial Code Generation
Compiler Construction8
• One class per .j file
• One statement per line
• Assembly setup:
• Required options
• Methods
• Statements (JVM instructions)
Jasmin syntax
Tutorial Code Generation
Compiler Construction9
File structure: Class options
▪ .source: Source of assembly▪ e.g.: .source MyClass.jova
▪ .class: Resulting java class description▪e.g.: .class public MyClass
▪ .super: Superclass of resulting java class▪always: .super java/lang/Object
▪ .field: Specify fields of class▪e.g.: .field public my_field I
Tutorial Code Generation
Compiler Construction10
.method <method signature>
e.g. .method public myMethod(I)V
.limit stack n
n: choose realistic number
.limit locals n
n = #parameters + #local_vars + #temp_var
<instructions>
return
requires matching type on top-of-stack for non-void returns
(e.g. ireturn)
.end method
File structure: Methods
Tutorial Code Generation
Compiler Construction11
Default Constructor
▪ needs to be defined for every class
▪ since Jova does not have Constructors you can
use the same definition for every class:
.method public <init>()V
.limit stack 1
.limit locals 1
aload_0
invokespecial java/lang/Object/<init>()V
return
.end method
Tutorial Code Generation
Compiler Construction12
Example: DoNix
File: DoNix.j
.source noSource
.class public DoNix
.super java/lang/Object
.method public static main([Ljava/lang/String;)V
.limit stack 0
.limit locals 1
;nothing to do here
return
.end method
Tutorial Code Generation
Compiler Construction13
• Two data structures per method (invocation/frame)
• Stack
• Local array
• Operations to manipulate both
• Build-in datatypes
• Primitive Types (int, float, byte, char,…)
• Reference Types (class types, array types,
interface types)
• Very limited support for boolean
Data Management
Tutorial Code Generation
Compiler Construction14
• Each method has its own operand stack
• Size is definable per method
(just assume a realistic number)
• LIFO
• Stack operations
• Push values onto stack
• Pop/Fetch values from stack
• Instructions which require one or multiple values
on stack (order does matter!)
Data Management: Stack
Tutorial Code Generation
Compiler Construction15
• Each method has its own local variable array
• Definable size for each method (0-based indices)
• Typing
• Can store arbitrary types
• Items need to be initialized before read access
• Contains
• Method parameters (stored in lowest indices)
• At index 0 the ‘this’ reference
Data Management: Local Variable Array
Tutorial Code Generation
Compiler Construction16
Data Managment: Types
▪ … a few primitives▪Integer indicated by letter I▪Void indicated by letter V▪Boolean indicated by letter Z
▪ Objects following the format “Lpackage/Classname;“▪e.g. Ljava/lang/String; String object
▪ Array indicated by a leading [▪e.g. [Ljava/lang/String; array of Strings
Tutorial Code Generation
Compiler Construction17
Instructions
▪ Can involve stack and local array
▪ Most instructions operate on expected types
▪ Instruction template: <T><instr>
where <T> = type letter
<instr> = operation▪ E.g.: iadd, fadd
▪ Instructions which do not operate on specific type▪ E.g.: swap, dup
Tutorial Code Generation
Compiler Construction18
Instructions: Stack - Locals interaction
▪ iload n pushes integer, stored in index n of
local array, onto stack▪ istore n pops integer from stack and stores it
into index n of local array▪ aload n pushes object, stored in index n of
local array, onto stack▪ astore n pops object from stack and stores it