Instruction selection for translating LLVM-

Compilation 2016
Instruction Selection
for LLVM-Aslan Askarov
[email protected]
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
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
•
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
Stack organization – a figure from
System V ABI document
Register usage
Note argument calling
convention
we don’t use these
registers
Organizing stack for Tiger programs
stack grows from high memory addresses to low addresses
current frame
0x1010
0x1018
0x1030
…
0x1080
previous frame
0x1088
…
0x1018
alloca
%localN
%local2
%local1
0x10A0
0x10A8
0x1200
ret addr
0x10B0
saved args
saved %rbp value
%rsp
result of alloca
Q: why do we need to save args?
%rbp
…
0x1200
backend.sml – LLVM-- to x86
instruction generation
•
•
•
Simple strategy – compiling all %uids to stackallocated 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?
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
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