Top Banner
The LLVM Assembler and Machine Code Infrastructure Monday, November 29, 2010
119

The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Aug 10, 2018

Download

Documents

lamkiet
Welcome message from author
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
Page 1: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

The LLVM Assembler and Machine Code Infrastructure

Monday, November 29, 2010

Page 2: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Overview

Monday, November 29, 2010

Page 3: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Overview

• What?

Monday, November 29, 2010

Page 4: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Overview

• What?• Why?

Monday, November 29, 2010

Page 5: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Overview

• What?• Why?• How?

Monday, November 29, 2010

Page 6: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Overview

• What?• Why?• How?• High-Level Design Goals

Monday, November 29, 2010

Page 7: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Overview

• What?• Why?• How?• High-Level Design Goals• Architecture

Monday, November 29, 2010

Page 8: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Overview

• What?• Why?• How?• High-Level Design Goals• Architecture• Status

Monday, November 29, 2010

Page 9: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

What?

Monday, November 29, 2010

Page 10: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

What?

• What is MC?

Monday, November 29, 2010

Page 11: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

What?

• What is MC?– “Machine code”

Monday, November 29, 2010

Page 12: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

What?

• What is MC?– “Machine code”– Focus is working with “object files”

Monday, November 29, 2010

Page 13: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

What?

• What is MC?– “Machine code”– Focus is working with “object files”

• Project started late 2009

Monday, November 29, 2010

Page 14: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

What?

• What is MC?– “Machine code”– Focus is working with “object files”

• Project started late 2009– Enabled for production in LLVM 2.8 (Oct 2010)

Monday, November 29, 2010

Page 15: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Why?

Monday, November 29, 2010

Page 16: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Why?

• Direct object writing

Monday, November 29, 2010

Page 17: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Why?

• Direct object writing– Simplicity, correctness, and performance

Monday, November 29, 2010

Page 18: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Why?

• Direct object writing– Simplicity, correctness, and performance– Single source of truth

Monday, November 29, 2010

Page 19: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Why?

• Direct object writing– Simplicity, correctness, and performance– Single source of truth

• Advanced micro-arch optimizations

Monday, November 29, 2010

Page 20: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Why?

• Direct object writing– Simplicity, correctness, and performance– Single source of truth

• Advanced micro-arch optimizations• Platform for advancing low-level tools

Monday, November 29, 2010

Page 21: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How?

Monday, November 29, 2010

Page 22: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How?

Code Generator

Assembly Printer

Assembly (.s)

Code (.c)

Standard Compiler

Monday, November 29, 2010

Page 23: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How?

Code Generator

Assembly Printer

Assembly (.s)

Code (.c)

Standard Compiler

Monday, November 29, 2010

Page 24: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How?

Code Generator

Assembly Printer

Assembly (.s)

Code (.c)

Standard Compiler

Code Generator

Assembly Printer (JIT)

Execution

Code (.c)

LLVM JIT

JIT Encoder

Monday, November 29, 2010

Page 25: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How?

Code Generator

Assembly Printer

Assembly (.s)

Code (.c)

Standard Compiler

Code Generator

Assembly Printer (JIT)

Execution

Code (.c)

LLVM JIT

JIT Encoder

Monday, November 29, 2010

Page 26: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How?

Code Generator

Assembly Printer

Assembly (.s)

Code (.c)

Standard Compiler

Code Generator

Assembly Printer (JIT)

Execution

Code (.c)

LLVM JIT

JIT Encoder

Duplicate Code!

Monday, November 29, 2010

Page 27: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How?

Code Generator

Assembly Printer

Assembly (.s)

Code (.c)

Standard Compiler

Code Generator

Assembly Printer (JIT)

Execution

Code (.c)

LLVM JIT

JIT Encoder

Duplicate Code!

No Public API!

Monday, November 29, 2010

Page 28: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How?

Code Generator

Assembly Printer

Assembly (.s)

Code (.c)

Standard Compiler

Code Generator

Assembly Printer (JIT)

Execution

Code (.c)

LLVM JIT

JIT Encoder

Duplicate Code!

No Public API!

Monday, November 29, 2010

Page 29: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How?

Monday, November 29, 2010

Page 30: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How?

Code Generator

Assembly Printer

Assembly (.s)

Code (.c)

Modern Compiler

Monday, November 29, 2010

Page 31: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How?

Code Generator

Assembly Printer

Assembly (.s)

Code (.c)

Modern Compiler

Monday, November 29, 2010

Page 32: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How?

Code Generator

MCized Printer

Code (.c)

Modern Compiler

MCStreamer

Monday, November 29, 2010

Page 33: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How?

Code Generator

MCized Printer

Code (.c)

Modern Compiler

MCStreamer

AsmPrinter ObjectWriter

Assembly (.s) Object File (.o)

Monday, November 29, 2010

Page 34: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

High-Level Design Goals

Monday, November 29, 2010

Page 35: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

High-Level Design Goals

• Reuse

Monday, November 29, 2010

Page 36: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

High-Level Design Goals

• Reuse• Performance

Monday, November 29, 2010

Page 37: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

High-Level Design Goals

• Reuse• Performance

– No redundant effort

Monday, November 29, 2010

Page 38: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

High-Level Design Goals

• Reuse• Performance

– No redundant effort• Testability

Monday, November 29, 2010

Page 39: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

High-Level Design Goals

• Reuse• Performance

– No redundant effort• Testability

– Test components in isolation

Monday, November 29, 2010

Page 40: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

High-Level Design Goals

• Reuse• Performance

– No redundant effort• Testability

– Test components in isolation• Flexibility

Monday, November 29, 2010

Page 41: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

High-Level Design Goals

• Reuse• Performance

– No redundant effort• Testability

– Test components in isolation• Flexibility

– Many uses for each MC component

Monday, November 29, 2010

Page 42: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

High-Level Design Goals

• Reuse• Performance

– No redundant effort• Testability

– Test components in isolation• Flexibility

– Many uses for each MC component• Pluggable Targets

Monday, November 29, 2010

Page 43: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

High-Level Design Goals

• Reuse• Performance

– No redundant effort• Testability

– Test components in isolation• Flexibility

– Many uses for each MC component• Pluggable Targets• Non-pluggable Object Formats

Monday, November 29, 2010

Page 44: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How is MC Used?

Monday, November 29, 2010

Page 45: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How is MC Used?

Object Writer

Assembler Parser

Assembly (.s)

Object File (.o)

Monday, November 29, 2010

Page 46: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How is MC Used?

It’s an Assembler!

Object Writer

Assembler Parser

Assembly (.s)

Object File (.o)

Monday, November 29, 2010

Page 47: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How is MC Used?

It’s an Assembler!

Object Writer

Assembler Parser

Assembly (.s)

Object File (.o)

Code Generator

Assembly Printer

Assembly (.s)

Code (.c)

Monday, November 29, 2010

Page 48: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How is MC Used?

It’s an Assembler!

It’s a Compiler!

Object Writer

Assembler Parser

Assembly (.s)

Object File (.o)

Code Generator

Assembly Printer

Assembly (.s)

Code (.c)

Monday, November 29, 2010

Page 49: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How is MC Used?

It’s an Assembler!

It’s a Compiler!

Object Writer

Assembler Parser

Assembly (.s)

Object File (.o)

Code Generator

Assembly Printer

Assembly (.s)

Code (.c)

Code Generator

Object Writer

Object File (.o)

Code (.c)

Monday, November 29, 2010

Page 50: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How is MC Used?

It’s an Assembler!

It’s a Compiler!

It’s a Compilassem

bler!Object Writer

Assembler Parser

Assembly (.s)

Object File (.o)

Code Generator

Assembly Printer

Assembly (.s)

Code (.c)

Code Generator

Object Writer

Object File (.o)

Code (.c)

Monday, November 29, 2010

Page 51: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How is MC Used?

It’s an Assembler!

It’s a Compiler!

It’s a Compilassem

bler!Object Writer

Assembler Parser

Assembly (.s)

Object File (.o)

Code Generator

Assembly Printer

Assembly (.s)

Code (.c)

Code Generator

Object Writer

Object File (.o)

Code (.c)

Code Generator

JIT

Execution!

Code (.c)

Monday, November 29, 2010

Page 52: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How is MC Used?

It’s an Assembler!

It’s a Compiler!

It’s a Compilassem

bler!

It’s a JIT!

Object Writer

Assembler Parser

Assembly (.s)

Object File (.o)

Code Generator

Assembly Printer

Assembly (.s)

Code (.c)

Code Generator

Object Writer

Object File (.o)

Code (.c)

Code Generator

JIT

Execution!

Code (.c)

Monday, November 29, 2010

Page 53: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How is MC Used?

It’s an Assembler!

It’s a Compiler!

It’s a Compilassem

bler!

It’s a JIT!

(with inline asm support!)

Object Writer

Assembler Parser

Assembly (.s)

Object File (.o)

Code Generator

Assembly Printer

Assembly (.s)

Code (.c)

Code Generator

Object Writer

Object File (.o)

Code (.c)

Code Generator

JIT

Execution!

Code (.c)

Monday, November 29, 2010

Page 54: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How is MC Used?

It’s an Assembler!

It’s a Compiler!

It’s a Compilassem

bler!

It’s a JIT!

(with inline asm support!)

Object Writer

Assembler Parser

Assembly (.s)

Object File (.o)

Code Generator

Assembly Printer

Assembly (.s)

Code (.c)

Code Generator

Object Writer

Object File (.o)

Code (.c)

Code Generator

JIT

Execution!

Code (.c) Object File (.o)

Disassembler

Assembly Printer

Assembly (.s)

Monday, November 29, 2010

Page 55: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How is MC Used?

It’s an Assembler!

It’s a Compiler!

It’s a Compilassem

bler!

It’s a JIT!

(with inline asm support!)

It’s a Disassembler!

Object Writer

Assembler Parser

Assembly (.s)

Object File (.o)

Code Generator

Assembly Printer

Assembly (.s)

Code (.c)

Code Generator

Object Writer

Object File (.o)

Code (.c)

Code Generator

JIT

Execution!

Code (.c) Object File (.o)

Disassembler

Assembly Printer

Assembly (.s)

Monday, November 29, 2010

Page 56: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How is MC Used?

Assembler Parser

Assembly (.s)

Code Generator

Code (.c)

Code Generator

Code (.c)

Code Generator

Code (.c) Object File (.o)

Disassembler

Object Writer

Object File (.o)

Assembly Printer

Assembly (.s)

Object Writer

Object File (.o)

JIT

Execution!

Assembly Printer

Assembly (.s)

Monday, November 29, 2010

Page 57: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How is MC Used?

MCStreamer MCInst

Assembler Parser

Assembly (.s)

Code Generator

Code (.c)

Code Generator

Code (.c)

Code Generator

Code (.c) Object File (.o)

Disassembler

Object Writer

Object File (.o)

Assembly Printer

Assembly (.s)

Object Writer

Object File (.o)

JIT

Execution!

Assembly Printer

Assembly (.s)

Monday, November 29, 2010

Page 58: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

How is MC Used?

MCStreamer MCInst

Assembler Parser

Assembly (.s)

Code Generator

Code (.c)

Code Generator

Code (.c)

Code Generator

Code (.c) Object File (.o)

Disassembler

Object Writer

Object File (.o)

Assembly Printer

Assembly (.s)

Object Writer

Object File (.o)

JIT

Execution!

Assembly Printer

Assembly (.s)

Monday, November 29, 2010

Page 59: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCStreamer

Monday, November 29, 2010

Page 60: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCStreamer

• Core MC Component

Monday, November 29, 2010

Page 61: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCStreamer

• Core MC Component– Programmatic Assembler API

Monday, November 29, 2010

Page 62: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCStreamer

• Core MC Component– Programmatic Assembler API– Best explained by example

Monday, November 29, 2010

Page 63: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCStreamer

• Core MC Component– Programmatic Assembler API– Best explained by example

Monday, November 29, 2010

Page 64: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCStreamer

Monday, November 29, 2010

Page 65: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCStreamer

Monday, November 29, 2010

Page 66: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCStreamer

Monday, November 29, 2010

Page 67: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCStreamer

Monday, November 29, 2010

Page 68: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCStreamer

void foo(MCStreamer &Out, MCContext &Ctx) { ... Out.SwitchSection(Ctx.getMachOSection(...));

...

}

Monday, November 29, 2010

Page 69: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCStreamer

Monday, November 29, 2010

Page 70: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCStreamer

void foo(MCStreamer &Out, MCContext &Ctx) { ... Out.EmitSymbolAttribute(Ctx.LookupSymbol(“_main”), MCSymbolAttr::MCSA_Global); ...

}

Monday, November 29, 2010

Page 71: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCStreamer

Monday, November 29, 2010

Page 72: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCStreamer

void foo(MCStreamer &Out, MCContext &Ctx) { ... Out.EmitValueToAlignment(4, 0x90);

...

}

Monday, November 29, 2010

Page 73: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCStreamer

Monday, November 29, 2010

Page 74: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCStreamer

void foo(MCStreamer &Out, MCContext &Ctx) { ... Out.EmitLabel(Ctx.LookupSymbol(“_main”));

...

}

Monday, November 29, 2010

Page 75: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCStreamer

void foo(MCStreamer &Out, MCContext &Ctx) { ... Out.EmitLabel(Ctx.LookupSymbol(“_main”));

...

}

Monday, November 29, 2010

Page 76: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCStreamer

Monday, November 29, 2010

Page 77: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCStreamer

void foo(MCStreamer &Out, MCContext &Ctx) { ... MCInst I = { ??? }; Out.EmitInstruction(I); ...

}

Monday, November 29, 2010

Page 78: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCInst

Monday, November 29, 2010

Page 79: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCInst

Monday, November 29, 2010

Page 80: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCInst

• Second major MC abstraction

Monday, November 29, 2010

Page 81: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCInst

• Second major MC abstraction• MCInst is a simple representation of a machine instruction

Monday, November 29, 2010

Page 82: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCInst

• Second major MC abstraction• MCInst is a simple representation of a machine instruction

– Consists of opcode and operands

Monday, November 29, 2010

Page 83: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCInst

• Second major MC abstraction• MCInst is a simple representation of a machine instruction

– Consists of opcode and operands– Operands:

Monday, November 29, 2010

Page 84: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCInst

• Second major MC abstraction• MCInst is a simple representation of a machine instruction

– Consists of opcode and operands– Operands:

– Registers

Monday, November 29, 2010

Page 85: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCInst

• Second major MC abstraction• MCInst is a simple representation of a machine instruction

– Consists of opcode and operands– Operands:

– Registers– Immediates (constants and expressions)

Monday, November 29, 2010

Page 86: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCInst

• Second major MC abstraction• MCInst is a simple representation of a machine instruction

– Consists of opcode and operands– Operands:

– Registers– Immediates (constants and expressions)– Floating point immediates

Monday, November 29, 2010

Page 87: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

MCInst

• Second major MC abstraction• MCInst is a simple representation of a machine instruction

– Consists of opcode and operands– Operands:

– Registers– Immediates (constants and expressions)– Floating point immediates

– Affords simple C API

Monday, November 29, 2010

Page 88: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

The llvm-mc tool

Monday, November 29, 2010

Page 89: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

The llvm-mc tool

• llvm-mc is the command line tool for testing MC

Monday, November 29, 2010

Page 90: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

The llvm-mc tool

• llvm-mc is the command line tool for testing MC– Includes assembler, object file writer, and disassembler

Monday, November 29, 2010

Page 91: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

The llvm-mc tool

• llvm-mc is the command line tool for testing MC– Includes assembler, object file writer, and disassembler

• Can use it to show encoding and MCInst structure

Monday, November 29, 2010

Page 92: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

The llvm-mc tool

• llvm-mc is the command line tool for testing MC– Includes assembler, object file writer, and disassembler

• Can use it to show encoding and MCInst structure

$ llvm-mc --show-inst t.s! pushl! %ebp ## <MCInst #2044 PUSH32r ## <MCOperand Reg:44>>

Monday, November 29, 2010

Page 93: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

The llvm-mc tool

• llvm-mc is the command line tool for testing MC– Includes assembler, object file writer, and disassembler

• Can use it to show encoding and MCInst structure

$ llvm-mc --show-inst t.s! pushl! %ebp ## <MCInst #2044 PUSH32r ## <MCOperand Reg:44>>

$ llvm-mc --show-encoding t.s! pushl! %ebp ## encoding: [0x55]

Monday, November 29, 2010

Page 94: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Instruction Matching

Monday, November 29, 2010

Page 95: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Instruction Matching

• Ties together the parsed instruction with target .td files

Monday, November 29, 2010

Page 96: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Instruction Matching

• Ties together the parsed instruction with target .td files• Uses a custom tblgen backend to generate match tables

Monday, November 29, 2010

Page 97: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Instruction Matching

• Ties together the parsed instruction with target .td files• Uses a custom tblgen backend to generate match tables

... { X86::PUSHF16, "pushfw", Convert, { }, 0 }, { X86::PUSH32r, "pushl", Convert__Reg1_0, { MCK_GR32 }, 0 }, { X86::PUSH32rmr, "pushl", Convert__Reg1_0, { MCK_GR32 }, 0 }, { X86::PUSHCS32, "pushl", Convert, { MCK_CS }, Feature_In32BitMode }, ...

Monday, November 29, 2010

Page 98: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Instruction Matching

• Ties together the parsed instruction with target .td files• Uses a custom tblgen backend to generate match tables

... { X86::PUSHF16, "pushfw", Convert, { }, 0 }, { X86::PUSH32r, "pushl", Convert__Reg1_0, { MCK_GR32 }, 0 }, { X86::PUSH32rmr, "pushl", Convert__Reg1_0, { MCK_GR32 }, 0 }, { X86::PUSHCS32, "pushl", Convert, { MCK_CS }, Feature_In32BitMode }, ...

Monday, November 29, 2010

Page 99: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Current Status

Monday, November 29, 2010

Page 100: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Current Status

• Integrated assembler is default for X86 for Darwin

Monday, November 29, 2010

Page 101: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Current Status

• Integrated assembler is default for X86 for Darwin• Lots of testing and qualification for X86

Monday, November 29, 2010

Page 102: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Current Status

• Integrated assembler is default for X86 for Darwin• Lots of testing and qualification for X86• ELF/X86-64 support is done

– On by default in Clang top-of-tree

Monday, November 29, 2010

Page 103: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Current Status

• Integrated assembler is default for X86 for Darwin• Lots of testing and qualification for X86• ELF/X86-64 support is done

– On by default in Clang top-of-tree• COFF support is well underway

– Passes many programs in LLVM test-suite repository

Monday, November 29, 2010

Page 104: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Current Status

• Integrated assembler is default for X86 for Darwin• Lots of testing and qualification for X86• ELF/X86-64 support is done

– On by default in Clang top-of-tree• COFF support is well underway

– Passes many programs in LLVM test-suite repository• ARM support is ongoing

Monday, November 29, 2010

Page 105: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Current Status: Performance

Monday, November 29, 2010

Page 106: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Current Status: Performance

• Example numbers from SPECCPU’s 403.gcc– clang with and without -integrated-as– Using -O0 -g for i386

Monday, November 29, 2010

Page 107: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Current Status: Performance

• Example numbers from SPECCPU’s 403.gcc– clang with and without -integrated-as– Using -O0 -g for i386

i386.c insn-emit.c combine.c insn-recog.c0

1

2

3

4

Com

pile

Wal

l Tim

e (s

)

System Assembler Integrated Assembler

Monday, November 29, 2010

Page 108: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Current Status: Performance

• Example numbers from SPECCPU’s 403.gcc– clang with and without -integrated-as– Using -O0 -g for i386

i386.c insn-emit.c combine.c insn-recog.c0

1

2

3

4

0.790.91Com

pile

Wal

l Tim

e (s

)

System Assembler Integrated Assembler

15%

Fast

er!

Monday, November 29, 2010

Page 109: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Current Status: Performance

• Example numbers from SPECCPU’s 403.gcc– clang with and without -integrated-as– Using -O0 -g for i386

i386.c insn-emit.c combine.c insn-recog.c0

1

2

3

4

0.790.91 1.001.22

Com

pile

Wal

l Tim

e (s

)

System Assembler Integrated Assembler

15%

Fast

er!

22%

Fast

er!

Monday, November 29, 2010

Page 110: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Current Status: Performance

• Example numbers from SPECCPU’s 403.gcc– clang with and without -integrated-as– Using -O0 -g for i386

i386.c insn-emit.c combine.c insn-recog.c0

1

2

3

4

0.790.91 1.001.22

0.790.89Com

pile

Wal

l Tim

e (s

)

System Assembler Integrated Assembler

15%

Fast

er!

22%

Fast

er!

13%

Fast

er!

Monday, November 29, 2010

Page 111: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Current Status: Performance

• Example numbers from SPECCPU’s 403.gcc– clang with and without -integrated-as– Using -O0 -g for i386

i386.c insn-emit.c combine.c insn-recog.c0

1

2

3

4

0.790.91 1.001.22

0.790.89

2.37

3.02

Com

pile

Wal

l Tim

e (s

)

System Assembler Integrated Assembler

15%

Fast

er!

22%

Fast

er!

13%

Fast

er!

27%

Fast

er!

Monday, November 29, 2010

Page 112: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Summary

Monday, November 29, 2010

Page 113: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Summary

• Good compile-time improvements

Monday, November 29, 2010

Page 114: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Summary

• Good compile-time improvements• Reduced system complexity

Monday, November 29, 2010

Page 115: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Summary

• Good compile-time improvements• Reduced system complexity• Many new tools and opportunities

Monday, November 29, 2010

Page 116: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Summary

• Good compile-time improvements• Reduced system complexity• Many new tools and opportunities• What’s next?

Monday, November 29, 2010

Page 117: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Summary

• Good compile-time improvements• Reduced system complexity• Many new tools and opportunities• What’s next?

– JIT needs to be converted

Monday, November 29, 2010

Page 118: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Summary

• Good compile-time improvements• Reduced system complexity• Many new tools and opportunities• What’s next?

– JIT needs to be converted– User-level disassembler

Monday, November 29, 2010

Page 119: The LLVM Assembler and Machine Code Infrastructurellvm.org/devmtg/2010-11/Dunbar-MC.pdf · The LLVM Assembler and ... • MCInst is a simple representation of a machine instruction

Questions?

Monday, November 29, 2010