Getting Started with LLVM Core Libraries
Total Page:16
File Type:pdf, Size:1020Kb
Getting Started with LLVM Core Libraries Get to grips with LLVM essentials and use the core libraries to build advanced tools Bruno Cardoso Lopes Rafael Auler BIRMINGHAM - MUMBAI Getting Started with LLVM Core Libraries Copyright © 2014 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: August 2014 Production reference: 1200814 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78216-692-4 www.packtpub.com Cover image by Aniket Sawant ([email protected]) Credits Authors Project Coordinator Bruno Cardoso Lopes Priyanka Goel Rafael Auler Proofreaders Reviewers Simran Bhogal Eli Bendersky Mario Cecere Logan Chien Jonathan Todd Jia Liu John Szakmeister Indexers Hemangini Bari Commissioning Editor Mariammal Chettiyar Mary Jasmine Nadar Tejal Soni Acquisition Editor Graphics Kevin Colaco Ronak Dhruv Abhinash Sahu Content Development Editor Arun Nadar Production Coordinators Saiprasad Kadam Technical Editors Conidon Miranda Pramod Kumavat Pratik More Cover Work Manu Joseph Copy Editors Saiprasad Kadam Dipti Kapadia Insiya Morbiwala Aditya Nair Alfida Paiva Stuti Srivastava About the Authors Bruno Cardoso Lopes received a PhD in Computer Science from University of Campinas, Brazil. He's been an LLVM contributor since 2007 and implemented the MIPS backend from scratch, which he has been maintaining for several years. Among his other contributions, he has written the x86 AVX support and improved the ARM assembler. His research interests include code compression techniques and reduced bit width ISAs. In the past, he has also developed drivers for Linux and FreeBSD operating systems. Rafael Auler is a PhD candidate at University of Campinas, Brazil. He holds a Master's degree in Computer Science and a Bachelor's degree in Computer Engineering from the same university. For his Master's work, he wrote a proof-of-concept tool that automatically generates LLVM backends based on architecture description files. Currently, his PhD research topics include dynamic binary translation, Just-in-Time compilers, and computer architecture. Rafael was also a recipient of the Microsoft Research 2013 Graduate Research Fellowship Award. About the Reviewers Eli Bendersky has been a professional programmer for 15 years, with extensive experience in systems programming, including compilers, linkers, and debuggers. He's been a core contributor to the LLVM project since early 2012. Logan Chien received his Master's degree in Computer Science from National Taiwan University. His research interests include compiler design, compiler optimization, and virtual machines. He is a software developer and has been working on several open source projects, including LLVM, Android, and so on. He has written several patches to fix the ARM zero-cost exception handling mechanism and enhanced the LLVM ARM integrated assembler. He was also a Software Engineer Intern at Google in 2012. At Google, he integrated the LLVM toolchain with the Android NDK. Jia Liu started GNU/Linux-related development in his college days and has been engaged in open-source-related development jobs after graduation. He is now responsible for all software-related work at China-DSP. He is interested in compiler technology and has been working on it for years. In his spare time, he also works on a few open source projects such as LLVM, QEMU, and GCC/Binutils. He is employed by a Chinese processor vendor, Glarun Technology—you can just call it China-DSP. China-DSP is a high-performance DSP vendor; the core business of the company is processor design, system software, and an embedded parallel processing platform that provides independent knowledge of electricity, telecommunications, automotive, manufacturing equipment, instrumentation, and consumer electronics. I want to thank my father and my mother; they raised me. Thanks to my girlfriend; in fact, I think she is my life's mentor. Thanks to my colleagues; we happily work with one another. John Szakmeister holds a Master of Science in Electrical Engineering from Johns Hopkins University and is a co-founder of Intelesys Corporation (www.intelesyscorp.com). John has been writing software professionally for more than 15 years and enjoys working on compilers, operating systems, sophisticated algorithms, and anything embedded. He's an avid supporter of Open Source and contributes to many projects in his free time. When he is not hacking, John works toward his black belt in Ninjutsu and enjoys reading technical books. I would like to thank my wife, Ann, and our two boys, Matthew and Andrew, for being so patient and understanding while I was reviewing this book. www.PacktPub.com Support files, eBooks, discount offers, and more You might want to visit www.PacktPub.com for support files and downloads related to your book. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. TM http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read, and search across Packt's entire library of books. Why subscribe? • Fully searchable across every book published by Packt • Copy and paste, print, and bookmark content • On demand and accessible via web browser Free access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access. Table of Contents Preface 1 Chapter 1: Build and Install LLVM 9 Understanding LLVM versions 10 Obtaining prebuilt packages 10 Obtaining the official prebuilt binaries 11 Using package managers 12 Staying updated with snapshot packages 12 Building from sources 13 System requirements 13 Obtaining sources 14 SVN 14 Git 15 Building and installing LLVM 15 Using the autotools-generated configure script 15 Using CMake and Ninja 18 Using other Unix approaches 21 Windows and Microsoft Visual Studio 21 Mac OS X and Xcode 25 Summary 30 Chapter 2: External Projects 31 Introducing Clang extras 32 Building and installing Clang extra tools 33 Understanding Compiler-RT 34 Seeing Compiler-RT in action 34 Using the DragonEgg plugin 36 Building DragonEgg 37 Understanding the compilation pipeline with DragonEgg and LLVM tools 38 Understanding the LLVM test suite 39 Table of Contents Using LLDB 40 Exercising a debug session with LLDB 41 Introducing the libc++ standard library 43 Summary 46 Chapter 3: Tools and Design 47 Introducing LLVM's basic design principles and its history 47 Understanding LLVM today 50 Interacting with the compiler driver 52 Using standalone tools 54 Delving into the LLVM internal design 56 Getting to know LLVM's basic libraries 57 Introducing LLVM's C++ practices 58 Seeing polymorphism in practice 59 Introducing C++ templates in LLVM 59 Enforcing C++ best practices in LLVM 60 Making string references lightweight in LLVM 61 Demonstrating the pluggable pass interface 62 Writing your first LLVM project 64 Writing the Makefile 64 Writing the code 66 Navigating the LLVM source – general advice 68 Understanding the code as a documentation 68 Asking the community for help 69 Coping with updates – using the SVN log as a documentation 69 Concluding remarks 71 Summary 72 Chapter 4: The Frontend 73 Introducing Clang 73 Frontend actions 74 Libraries 76 Using libclang 76 Understanding Clang diagnostics 78 Reading diagnostics 80 Learning the frontend phases with Clang 83 Lexical analysis 83 Exercising lexical errors 85 Writing libclang code that uses the lexer 85 Preprocessing 88 Syntactic analysis 90 Understanding Clang AST nodes 90 Understanding the parser actions with a debugger 92 Exercising a parser error 94 Writing code that traverses the Clang AST 94 [ ii ] Table of Contents Serializing the AST with precompiled headers 97 Semantic analysis 98 Exercising a semantic error 99 Generating the LLVM IR code 99 Putting it together 100 Summary 104 Chapter 5: The LLVM Intermediate Representation 105 Overview 105 Understanding the LLVM IR target dependency 107 Exercising basic tools to manipulate the IR formats 108 Introducing the LLVM IR language syntax 109 Introducing the LLVM IR in-memory model 113 Writing a custom LLVM IR generator 114 Building