Instruction Selection for LLVM-- Aslan Askarov aslan@cs.au.dk The - - PowerPoint PPT Presentation

instruction selection for llvm
SMART_READER_LITE
LIVE PREVIEW

Instruction Selection for LLVM-- Aslan Askarov aslan@cs.au.dk The - - PowerPoint PPT Presentation

Compilation 2016 Instruction Selection for LLVM-- Aslan Askarov aslan@cs.au.dk The x86 assembly language Assembler produces object file that contains segments (address ranges with a purpose) Segments containing runnable


slide-1
SLIDE 1

Compilation 2016

Instruction Selection for LLVM--

Aslan Askarov aslan@cs.au.dk
 
 


slide-2
SLIDE 2

The x86 assembly language

  • Assembler produces object file that contains

segments (address ranges with a purpose)

  • Segments containing runnable code: .text
  • Segments containing initialized data: .data
  • Remember to specify segment to fit purpose
  • Specifying data:
  • .ascii .asciz
  • Metadata:
  • .func .type .endfunc .size .globl
  • For examples: check provided *.s
slide-3
SLIDE 3

The x86 assembly language

  • Example instruction: movq %rax, -48(%rbp)
  • General format: <instr><S> <src>, <dst>
  • <S> ∈ {b,s,w,l,q,t} specifies size (q: 64 bit)
  • $ indicates literal number
  • % indicates register
  • o(%r1, %r2, n) means o + %r1 + %r2 * n
  • Example label: mylabel
  • gives the “current address” the name ‘mylabel’
  • Example directives:
  • .text .data .globl .ascii .asciz


.size .func .type .endfunc

slide-4
SLIDE 4
  • x86.sml – A friendly module for creating and printing

x86 64 assembly programs

  • System V x86 64 Application Binary Interface (ABI)
  • https://software.intel.com/sites/default/files/article/

402129/mpx-linux64-abi.pdf

  • A document describing (among other things)
  • Frame organization
  • Calling conventions
  • Register purpose
slide-5
SLIDE 5

Stack organization – a figure from System V ABI document

slide-6
SLIDE 6

Register usage

Note argument calling convention we don’t use these registers

slide-7
SLIDE 7

Organizing stack for Tiger programs

0x1018

0x1200

… 0x10B0 0x10A8 0x10A0 0x1080 0x1010

stack grows from high memory addresses to low addresses

… current frame %rsp

saved %rbp value ret addr

0x1200 %rbp previous frame 0x1088

%local1 %local2 %localN

0x1030

alloca

0x1018 result of alloca

saved args

Q: why do we need to save args?

slide-8
SLIDE 8

backend.sml – LLVM-- to x86 instruction generation

  • Simple strategy – compiling all %uids to stack-

allocated slots

  • Each LLVM-- instruction may correspond to several

x86 instructions

  • Strategy:
  • designate temporary registers for storing

intermediate results when translating operands

  • e.g., %rax, %rcx
  • Q: callee-save vs caller-save registers?
slide-9
SLIDE 9

Functions

  • Function name: just a label
  • Function call:
  • Use assembly callq instruction
  • Function prologue:
  • save %rbp on stack; decrement %rsp
  • do not forget to copy arguments on the stack
  • Function epilogue:
  • invoked by LL terminators
slide-10
SLIDE 10

Other LL commands

  • Pick your compilation strategy, as long as you are

within the bounds of what x86.sml offers you (there is plenty of space)

  • Recommend to tackle GEP last
  • Generate test LL programs from your LL module