tutorial 3: code generation - graz university of technology · tutorial code generation compiler...

33
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 Institute for Software Technology

Upload: others

Post on 21-May-2020

15 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler 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

u www.tugraz.at

Tutorial 3: Code Generation

Univ.-Prof. Dr. Franz Wotawa, DI Roxane Koitz,Stephan Frühwirt, Christopher Liebmann

Institute for Software Technology

Page 2: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

2

Jova Input Program

Java Byte Code

Compiler Phases

Page 3: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

3

Jova Input Program

Java Byte Code

Compiler Phases

Page 4: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

4

Jova Input Program

Java Byte Code

Compiler Phases

Page 5: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

5

Jova Input Program

Java Byte Code

Compiler Phases

Page 6: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

6

Source Code(*.jova file) Syntax tree

Grammar(Jova.g4)

ANTLRLexer and Parser

Lexical and syntactical errors

Symbol table

ANTLRvisitor / listener

Type checking errors

Debugging output

Debugging output

Workflow (1/2)

Page 7: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

7

Syntax tree

Symbol table

Workflow (2/2)

Jasmin code(.j file)

Byte code(.class file)

Jasmin

Page 8: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

8

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

Page 9: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

9

Pipeline

CreateAssembly

• 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

Page 10: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

10

• One statement per line• Inline comments, initiated by ‘;‘• Assembly setup:

• Required options• Method 1• Method 2• …• Method n

Jasmin syntax

Page 11: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

11

Required 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

… unless you’ve implemented inheritance▪ .field: Specify fields of class

▪e.g.: .field public my_field I

Page 12: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

12

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 1aload_0

invokespecial java/lang/Object/<init>()Vreturn.end method

Page 13: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

13

.method <method signature>e.g. .method public myMethod(I)V

.limit stack nn: choose realistic number

.limit locals nn = #parameters + #local_vars + #temp_var

<instructions>

returnrequires matching type on top-of-stack for non-void returns

(e.g. ireturn).end method

Methode structure

Page 14: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

14

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 herereturn.end method

Page 15: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

15

• Two data structures per method• Stack• Locals array

• Operations to manipulate both• Build-in datatypes

• Primitives• Arrays• Objects

Data Management

Page 16: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

16

• 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

Page 17: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

17

• Each method has its own locals 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: Locals Array

Page 18: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

18

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 “package/Classname;“▪e.g. Ljava/lang/String; String object

▪ Array indicated by a leading [▪e.g. [Ljava/lang/String; array of Strings

Page 19: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

19

Instructions

▪ One instruction per line▪ Can involve stack and locals array▪ May require a specific number/type of elements

present▪ Order for binary instructions like 4 – 2:1. bipush 4 ;push 4 on stack2. bipush 2 ;push 2 on stack 3. isub ;pop both and push

value of

;4 - 21. result is now on top of stack

Page 20: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

20

Instructions: Stack - locals interaction

▪ iload n pushes integer, stored in index n oflocals array, onto stack

▪ istore n pops integer from stack and stores itinto index n of locals array

▪ aload n pushes object, stored in index n oflocals array, onto stack

▪ astore n pops object from stack and stores itinto index n of locals array

Page 21: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

21

Instructions: Constants

▪ sipush n / bipush n▪Pushes integer constant onto stack▪E.g. sipush 10▪For -1 - 5 you can also use iconst_<n>

▪ ldc “<string>”▪Pushes string constant <string> onto stack▪E.g. ldc “Hello World”▪Note: Strings are Objects (variable access with astore/aload)

Page 22: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

22

Instructions: Arithmetic Operators

▪ ineg toggles sign of int on top of stack▪ iadd add two integers▪ isub subtract two integers▪ imul multiply two integers▪ idiv divide two integers▪ irem modulo division of two integers

Page 23: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

23

Instructions: Logic Operators

▪ iand bitwise and of two integers▪ ior bitwise or of two integers▪ inot does not exist!

▪Note: Needs to be assembled using custom labels and conditional jump operations

Page 24: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

24

Example: BasicInstructions

File: BasicInstructions.j.source noSource.class public BasicInstructions.super java/lang/Object

.method public static main([Ljava/lang/String;)V

.limit stack 5

.limit locals 3sipush 5 ;push integer 5 onto stack istore 0 ;pop integer 5 and store in index 0ldc "Hello World" ;push string Hello World onto stackastore 1 ;store string in index 1

iload 0 ;load 5dup ;duplicate stack entry 5sipush 2 ;push integer 2 onto stackisub ;pop 5 and 2 and store result 3 onto stackiadd ;pop 5 and 3 and store result 8 onto stackistore 0 ;store result 8 in index 0

return.end method

Page 25: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

25

Instructions: Relation Operators

▪ Do not exist!▪ Need to be assembled using custom labels and

conditional jump operations

Page 26: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

26

Instructions: Labels and Jumps

▪ <labelname>:▪makes a label in the assembly

▪ goto <labelname>▪continues execution of current method at position of the label <labelname>

▪ if_icmpXX <labelname>▪pops two elements of the stack, relates them and jumps to <labelname> if comparison computes to true

Page 27: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

27

Instructions: if_icmp variations

▪ if_icmplt relation using <▪ if_icmple relation using <=▪ if_icmpge relation using >=▪ if_icmpgt relation using >▪ if_icmpeq relation using ==▪ if_icmpne relation using !=

Page 28: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

28

Example: LabelsAndJumpsFile: LabelsAndJumps.j

.source noSource

.class public LabelsAndJumps

.super java/lang/Object

.method public static main([Ljava/lang/String;)V

.limit stack 5

.limit locals 3sipush 10istore 0 ;store 10 in index 0goto label_skip_redefinitionsipush 20istore 0 ;store 20 to index 0 - skippedlabel_skip_redefinition:iload 0 ;push value of index 0: 10sipush 15 ;push 15 if_icmplt label_is_lesserldc "greater" ;push string to stack - skippedgoto label_endlabel_is_lesser:ldc "lesser" ;push string to stacklabel_end:; result: string "lesser" on top of stackreturn.end method

Page 29: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

29

Instructions: field access

▪ Call put-/getfield of object on stack▪ Call using

putfield <class ID>/<field ID> <type>getfield <class ID>/<field ID> <type>

▪ Requires value and object on stack▪ Example

▪field: public int my_field▪signature: myClass/my_field I▪assembly: putfield myClass/my_field I

Page 30: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

30

Instructions: non-static method calls

▪ Call method of object on stack▪ Call using

invokevirtual <class ID>/<method signature>

▪ Requires parameters and object on stack▪ Example

▪method: public int myMethod(int a)▪signature: myMethod(I)I▪invokation: invokevirtual MyClass/myMethod(I)I

Page 31: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

31

Instructions: print

▪ Based on virtual invokation▪ How to:

1. Push PrintStream object onto stackgetstatic java/lang/System/out Ljava/io/PrintStream;

2. Push value onto stack (iload, aload, etc.)3. Invoke matching PrintStream method

invokevirtual java/io/PrintStream/print(I)V

invokevirtual java/io/PrintStream/print(Ljava/lang/String;)V

Page 32: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

32

Hints

▪ Try and write some simple Jasmin code yourselves▪ Use aload_0 to get ‘this’ reference▪ Number your labels ▪ Map Jova/internal variables to locals array indices▪ Instruction swap may be useful in some cases▪ Use the online documentation to find additional

instructions/explanations▪ When in doubt: model a problem in Java - compile it -

and use javap -c <Class> to decompile the .class files

Page 33: Tutorial 3: Code Generation - Graz University of Technology · 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 u Tutorial 3: Code

Tutorial Code GenerationCompiler Construction

33

Questions?