Book Image

LLVM Cookbook

Book Image

LLVM Cookbook

Overview of this book

Table of Contents (16 chapters)
LLVM Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Adding instruction encoding


If the instructions need to be specific for how they are encoded with respect to bit fields, this can be done by specifying the bit field in the .td file when defining an instruction.

How to do it…

To include instruction encoding while defining instructions, proceed with the following steps:

  1. A register operand that will be used to register the add instruction will have some defined encoding for its instruction. The size of the instruction is 32 bits, and the encoding for it is as follows:

    bits 0 to 3 -> src2, second register operand
    bits 4 to 11 -> all zeros
    bits 12 to 15 -> dst, for destination register
    bits 16 to 19 -> src1, first register operand
    bit 20 -> zero
    bit 21 to 24 -> for opcode
    bit 25 to 27 -> all zeros
    bit 28 to 31 -> 1110

    This can be achieved by specifying the preceding bit pattern in the .td files

  2. In the TOYInstrFormats.td file, define a new variable, called Inst:

    class InstTOY<dag outs, dag ins, string asmstr, list<dag&gt...