Docsity

Joeq Compiler System - Advanced Compiling Techniques - Lecture Slides, Slides of Compilers

Partial preview of the text

Download Joeq Compiler System - Advanced Compiling Techniques - Lecture Slides and more Slides Compilers in PDF only on Docsity!

joeq compiler system

Plan for Today

1. Joeq System Overview

2. Lifecycle of Analyzed Code

3. Source Code Representation

4. Writing and Running a Pass

5. Assignment: Dataflow Framework

joeq Design Choices

  • Most of the system is implemented in pure Java
  • Thus, analysis framework and bytecode processors work everywhere
  • For the purpose of programming assignment, we treat joeq as a front- and

middle end

  • But it can be used as a VM as well
    • System-specific code is patched in when the joeq system compiles itself or its own runtime
    • These are ordinary C routines
    • Systems supported by full version: Linux and Windows under x

joeq Components

  • Full system is very large: ~100, lines of code
  • Allocator
  • Bootstrapper
  • Classfile structure
  • Compiler (Quad)
  • Garbage Collector
  • Quad Interpreters
  • Memory Access
  • Safe/Unsafe barriers
    • Synchronization
    • Assembler
    • Class Library
    • Compiler (Bytecode)
    • Debugger
    • Bytecode Interpreters
    • Linkers
    • Reflection support
    • Scheduling
    • UTF-8 Support

We restrict ourselves to only the compiler and classfile routines,

which is closer to 40,000 lines of code

Lifecycle of Analyzed Code

  • Everything begins as source code
  • A very “rich” representation
    • Good for reading
    • Hard to analyze
  • Lots of high-level concepts here with (probably) no counterparts in the hardware
    • Virtual function calls
    • Direct use of monitors and condition variables
    • Exceptions
    • Reflection
    • Anonymous classes
    • Threads

Source to Bytecode

  • javac or jikes compiles source into a machine-independent

bytecode format

  • This still keeps the coarse structure of the program
    • Each class is a file
    • Split up into methods and fields
    • The bytecodes themselves are stored as a member attribute in

methods that have them

  • Bytecoded instructions are themselves high level:
    • invokevirtual
    • monitorenter
    • arraylength

How Source Code is Represented

within joeq

Source Code Representation

  • joeq is designed primarily to work with Java
    • Operates at all levels of abstraction
    • Has classes corresponding to each language component
  • Relevant packages in joeq
    • joeq.Class package: classes that represent Java source components (classes, fields, methods, etc.) reside in joeq's
    • Compil3r.BytecodeAnalysis package: analysis of Java bytecode
    • Compil3r.Quad package: Classes relevant to joeq's internal “quad” format
  • Be careful with your imports:
    • avoid name conflicts with java.lang.Class and java.lang.Compiler classes

Types/Classes

Fields/Methods

Basic blocks

Instructions

joeq.Class: Fields and Methods

  • Subclasses of jq_Field and jq_Method, respectively
    • Class hierarchy distinguishes between instance and class (static)

members, but this detail is generally hidden from higher analyses

  • These classes know about their relevant types: who declares

them, parameter/return types, etc.

  • Names of members are stored as UTF.Utf8 objects, so you'll

need to convert them with toString() to get any use out of them!

Analyzing Bytecode

• The Java Virtual Machine stores program code

as bytecodes that serve as instructions to a

stack machine of sorts

• Raw material for all analysis of Java code

• Preserves vast amounts of source information:

– Java decompilers can almost perfectly reconstruct

source, down to variable names and line numbers

Bytecode Details

  • The implied running model of the Java Virtual Machine is that of a stack

machine - there are local variables that correspond to registers, and a

stack where all computation occurs.

  • This is hard to analyze!
  • Fortunately, the JVM requires that bytecode pass strict typechecking and

stack consistency checking

  • Gosling Property: At each instruction, the types of every element on the

stack, and every local variable, are all well defined

  • By extension, the stack must have a specific height at each program point

Converting Bytecodes to Quads

  • joeq thus converts bytecodes to something closer to standard three-address code, called "Quads"
  • The highly abstract bytecode instructions for the most part have direct counterparts in the Quad representation
  • One operator, up to four operands

OPERATOR OP1 OP2 OP3 OP

  • Approximately 100 operators, all told (filed into a dozen or so rough categories), about 15 varieties of operands
  • Full details on these and the methods appropriate to them on the course website's joeq documentation: - http://suif.stanford.edu/~courses/cs243/joeq/

Operands

• Operands are split into 15 types

– The ConstOperand classes (I, F, A, etc.) indicate

constant values of the relevant type

– RegisterOperands name pseudo-registers

– MethodOperands and ParamListOperands are

used to identify method targets

– TypeOperands are passed to type-checking

operators, or to "new" operators

– TargetOperands indicate the target of a branch

Converting a Method to Quads

BB0 (ENTRY) (in: , out: BB2) BB2 (in: BB0 (ENTRY), out: BB3, BB4) 1 ADD_I T0 int, R1 int, IConst: 10 2 MOVE_I R3 int, T0 int 3 ADD_I T0 int, R1 int, R3 int 4 MOVE_I R6 int, T0 int 5 IFCMP_I R6 int, IConst: 2, LE, BB BB3 (in: BB2, out: BB4) 6 SUB_I T0 int, R6 int, R3 int 7 MOVE_I R6 int, T0 int BB4 (in: BB2, BB3, out: BB1 (EXIT)) 8 RETURN_I R6 int BB1 (EXIT) (in: BB4, out: )

Exception handlers: [] Register factory: Local: (I=7, F=7, L=7, D=7, A=7) Stack: (I=2, F=2, L=2, D=2, A=2)