BackEnds.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. =================
  2. TableGen BackEnds
  3. =================
  4. .. contents::
  5. :local:
  6. Introduction
  7. ============
  8. NOTE: the DirectX Compiler project supports TableGen functionality, but also
  9. relies on /utils/hct/hctdb.py and its supporting files to generate code and
  10. documentation. The hctdb.py file is a Python script that builds an in-memory
  11. representation of various concepts; other tools can query this information to
  12. generate code. Long-form simple declarations are preferred for hctdb, but when
  13. convenient more powerful constructs can be employed.
  14. TableGen backends are at the core of TableGen's functionality. The source files
  15. provide the semantics to a generated (in memory) structure, but it's up to the
  16. backend to print this out in a way that is meaningful to the user (normally a
  17. C program including a file or a textual list of warnings, options and error
  18. messages).
  19. TableGen is used by both LLVM and Clang with very different goals. LLVM uses it
  20. as a way to automate the generation of massive amounts of information regarding
  21. instructions, schedules, cores and architecture features. Some backends generate
  22. output that is consumed by more than one source file, so they need to be created
  23. in a way that is easy to use pre-processor tricks. Some backends can also print
  24. C code structures, so that they can be directly included as-is.
  25. Clang, on the other hand, uses it mainly for diagnostic messages (errors,
  26. warnings, tips) and attributes, so more on the textual end of the scale.
  27. LLVM BackEnds
  28. =============
  29. .. warning::
  30. This document is raw. Each section below needs three sub-sections: description
  31. of its purpose with a list of users, output generated from generic input, and
  32. finally why it needed a new backend (in case there's something similar).
  33. Overall, each backend will take the same TableGen file type and transform into
  34. similar output for different targets/uses. There is an implicit contract between
  35. the TableGen files, the back-ends and their users.
  36. For instance, a global contract is that each back-end produces macro-guarded
  37. sections. Based on whether the file is included by a header or a source file,
  38. or even in which context of each file the include is being used, you have
  39. todefine a macro just before including it, to get the right output:
  40. .. code-block:: c++
  41. #define GET_REGINFO_TARGET_DESC
  42. #include "ARMGenRegisterInfo.inc"
  43. And just part of the generated file would be included. This is useful if
  44. you need the same information in multiple formats (instantiation, initialization,
  45. getter/setter functions, etc) from the same source TableGen file without having
  46. to re-compile the TableGen file multiple times.
  47. Sometimes, multiple macros might be defined before the same include file to
  48. output multiple blocks:
  49. .. code-block:: c++
  50. #define GET_REGISTER_MATCHER
  51. #define GET_SUBTARGET_FEATURE_NAME
  52. #define GET_MATCHER_IMPLEMENTATION
  53. #include "ARMGenAsmMatcher.inc"
  54. The macros will be undef'd automatically as they're used, in the include file.
  55. On all LLVM back-ends, the ``llvm-tblgen`` binary will be executed on the root
  56. TableGen file ``<Target>.td``, which should include all others. This guarantees
  57. that all information needed is accessible, and that no duplication is needed
  58. in the TbleGen files.
  59. CodeEmitter
  60. -----------
  61. **Purpose**: CodeEmitterGen uses the descriptions of instructions and their fields to
  62. construct an automated code emitter: a function that, given a MachineInstr,
  63. returns the (currently, 32-bit unsigned) value of the instruction.
  64. **Output**: C++ code, implementing the target's CodeEmitter
  65. class by overriding the virtual functions as ``<Target>CodeEmitter::function()``.
  66. **Usage**: Used to include directly at the end of ``<Target>MCCodeEmitter.cpp``.
  67. RegisterInfo
  68. ------------
  69. **Purpose**: This tablegen backend is responsible for emitting a description of a target
  70. register file for a code generator. It uses instances of the Register,
  71. RegisterAliases, and RegisterClass classes to gather this information.
  72. **Output**: C++ code with enums and structures representing the register mappings,
  73. properties, masks, etc.
  74. **Usage**: Both on ``<Target>BaseRegisterInfo`` and ``<Target>MCTargetDesc`` (headers
  75. and source files) with macros defining in which they are for declaration vs.
  76. initialization issues.
  77. InstrInfo
  78. ---------
  79. **Purpose**: This tablegen backend is responsible for emitting a description of the target
  80. instruction set for the code generator. (what are the differences from CodeEmitter?)
  81. **Output**: C++ code with enums and structures representing the register mappings,
  82. properties, masks, etc.
  83. **Usage**: Both on ``<Target>BaseInstrInfo`` and ``<Target>MCTargetDesc`` (headers
  84. and source files) with macros defining in which they are for declaration vs.
  85. AsmWriter
  86. ---------
  87. **Purpose**: Emits an assembly printer for the current target.
  88. **Output**: Implementation of ``<Target>InstPrinter::printInstruction()``, among
  89. other things.
  90. **Usage**: Included directly into ``InstPrinter/<Target>InstPrinter.cpp``.
  91. AsmMatcher
  92. ----------
  93. **Purpose**: Emits a target specifier matcher for
  94. converting parsed assembly operands in the MCInst structures. It also
  95. emits a matcher for custom operand parsing. Extensive documentation is
  96. written on the ``AsmMatcherEmitter.cpp`` file.
  97. **Output**: Assembler parsers' matcher functions, declarations, etc.
  98. **Usage**: Used in back-ends' ``AsmParser/<Target>AsmParser.cpp`` for
  99. building the AsmParser class.
  100. Disassembler
  101. ------------
  102. **Purpose**: Contains disassembler table emitters for various
  103. architectures. Extensive documentation is written on the
  104. ``DisassemblerEmitter.cpp`` file.
  105. **Output**: Decoding tables, static decoding functions, etc.
  106. **Usage**: Directly included in ``Disassembler/<Target>Disassembler.cpp``
  107. to cater for all default decodings, after all hand-made ones.
  108. PseudoLowering
  109. --------------
  110. **Purpose**: Generate pseudo instruction lowering.
  111. **Output**: Implements ``ARMAsmPrinter::emitPseudoExpansionLowering()``.
  112. **Usage**: Included directly into ``<Target>AsmPrinter.cpp``.
  113. CallingConv
  114. -----------
  115. **Purpose**: Responsible for emitting descriptions of the calling
  116. conventions supported by this target.
  117. **Output**: Implement static functions to deal with calling conventions
  118. chained by matching styles, returning false on no match.
  119. **Usage**: Used in ISelLowering and FastIsel as function pointers to
  120. implementation returned by a CC sellection function.
  121. DAGISel
  122. -------
  123. **Purpose**: Generate a DAG instruction selector.
  124. **Output**: Creates huge functions for automating DAG selection.
  125. **Usage**: Included in ``<Target>ISelDAGToDAG.cpp`` inside the target's
  126. implementation of ``SelectionDAGISel``.
  127. DFAPacketizer
  128. -------------
  129. **Purpose**: This class parses the Schedule.td file and produces an API that
  130. can be used to reason about whether an instruction can be added to a packet
  131. on a VLIW architecture. The class internally generates a deterministic finite
  132. automaton (DFA) that models all possible mappings of machine instructions
  133. to functional units as instructions are added to a packet.
  134. **Output**: Scheduling tables for GPU back-ends (Hexagon, AMD).
  135. **Usage**: Included directly on ``<Target>InstrInfo.cpp``.
  136. FastISel
  137. --------
  138. **Purpose**: This tablegen backend emits code for use by the "fast"
  139. instruction selection algorithm. See the comments at the top of
  140. lib/CodeGen/SelectionDAG/FastISel.cpp for background. This file
  141. scans through the target's tablegen instruction-info files
  142. and extracts instructions with obvious-looking patterns, and it emits
  143. code to look up these instructions by type and operator.
  144. **Output**: Generates ``Predicate`` and ``FastEmit`` methods.
  145. **Usage**: Implements private methods of the targets' implementation
  146. of ``FastISel`` class.
  147. Subtarget
  148. ---------
  149. **Purpose**: Generate subtarget enumerations.
  150. **Output**: Enums, globals, local tables for sub-target information.
  151. **Usage**: Populates ``<Target>Subtarget`` and
  152. ``MCTargetDesc/<Target>MCTargetDesc`` files (both headers and source).
  153. Intrinsic
  154. ---------
  155. **Purpose**: Generate (target) intrinsic information.
  156. OptParserDefs
  157. -------------
  158. **Purpose**: Print enum values for a class.
  159. CTags
  160. -----
  161. **Purpose**: This tablegen backend emits an index of definitions in ctags(1)
  162. format. A helper script, utils/TableGen/tdtags, provides an easier-to-use
  163. interface; run 'tdtags -H' for documentation.
  164. Clang BackEnds
  165. ==============
  166. ClangAttrClasses
  167. ----------------
  168. **Purpose**: Creates Attrs.inc, which contains semantic attribute class
  169. declarations for any attribute in ``Attr.td`` that has not set ``ASTNode = 0``.
  170. This file is included as part of ``Attr.h``.
  171. ClangAttrParserStringSwitches
  172. -----------------------------
  173. **Purpose**: Creates AttrParserStringSwitches.inc, which contains
  174. StringSwitch::Case statements for parser-related string switches. Each switch
  175. is given its own macro (such as ``CLANG_ATTR_ARG_CONTEXT_LIST``, or
  176. ``CLANG_ATTR_IDENTIFIER_ARG_LIST``), which is expected to be defined before
  177. including AttrParserStringSwitches.inc, and undefined after.
  178. ClangAttrImpl
  179. -------------
  180. **Purpose**: Creates AttrImpl.inc, which contains semantic attribute class
  181. definitions for any attribute in ``Attr.td`` that has not set ``ASTNode = 0``.
  182. This file is included as part of ``AttrImpl.cpp``.
  183. ClangAttrList
  184. -------------
  185. **Purpose**: Creates AttrList.inc, which is used when a list of semantic
  186. attribute identifiers is required. For instance, ``AttrKinds.h`` includes this
  187. file to generate the list of ``attr::Kind`` enumeration values. This list is
  188. separated out into multiple categories: attributes, inheritable attributes, and
  189. inheritable parameter attributes. This categorization happens automatically
  190. based on information in ``Attr.td`` and is used to implement the ``classof``
  191. functionality required for ``dyn_cast`` and similar APIs.
  192. ClangAttrPCHRead
  193. ----------------
  194. **Purpose**: Creates AttrPCHRead.inc, which is used to deserialize attributes
  195. in the ``ASTReader::ReadAttributes`` function.
  196. ClangAttrPCHWrite
  197. -----------------
  198. **Purpose**: Creates AttrPCHWrite.inc, which is used to serialize attributes in
  199. the ``ASTWriter::WriteAttributes`` function.
  200. ClangAttrSpellings
  201. ---------------------
  202. **Purpose**: Creates AttrSpellings.inc, which is used to implement the
  203. ``__has_attribute`` feature test macro.
  204. ClangAttrSpellingListIndex
  205. --------------------------
  206. **Purpose**: Creates AttrSpellingListIndex.inc, which is used to map parsed
  207. attribute spellings (including which syntax or scope was used) to an attribute
  208. spelling list index. These spelling list index values are internal
  209. implementation details exposed via
  210. ``AttributeList::getAttributeSpellingListIndex``.
  211. ClangAttrVisitor
  212. -------------------
  213. **Purpose**: Creates AttrVisitor.inc, which is used when implementing
  214. recursive AST visitors.
  215. ClangAttrTemplateInstantiate
  216. ----------------------------
  217. **Purpose**: Creates AttrTemplateInstantiate.inc, which implements the
  218. ``instantiateTemplateAttribute`` function, used when instantiating a template
  219. that requires an attribute to be cloned.
  220. ClangAttrParsedAttrList
  221. -----------------------
  222. **Purpose**: Creates AttrParsedAttrList.inc, which is used to generate the
  223. ``AttributeList::Kind`` parsed attribute enumeration.
  224. ClangAttrParsedAttrImpl
  225. -----------------------
  226. **Purpose**: Creates AttrParsedAttrImpl.inc, which is used by
  227. ``AttributeList.cpp`` to implement several functions on the ``AttributeList``
  228. class. This functionality is implemented via the ``AttrInfoMap ParsedAttrInfo``
  229. array, which contains one element per parsed attribute object.
  230. ClangAttrParsedAttrKinds
  231. ------------------------
  232. **Purpose**: Creates AttrParsedAttrKinds.inc, which is used to implement the
  233. ``AttributeList::getKind`` function, mapping a string (and syntax) to a parsed
  234. attribute ``AttributeList::Kind`` enumeration.
  235. ClangAttrDump
  236. -------------
  237. **Purpose**: Creates AttrDump.inc, which dumps information about an attribute.
  238. It is used to implement ``ASTDumper::dumpAttr``.
  239. ClangDiagsDefs
  240. --------------
  241. Generate Clang diagnostics definitions.
  242. ClangDiagGroups
  243. ---------------
  244. Generate Clang diagnostic groups.
  245. ClangDiagsIndexName
  246. -------------------
  247. Generate Clang diagnostic name index.
  248. ClangCommentNodes
  249. -----------------
  250. Generate Clang AST comment nodes.
  251. ClangDeclNodes
  252. --------------
  253. Generate Clang AST declaration nodes.
  254. ClangStmtNodes
  255. --------------
  256. Generate Clang AST statement nodes.
  257. ClangSACheckers
  258. ---------------
  259. Generate Clang Static Analyzer checkers.
  260. ClangCommentHTMLTags
  261. --------------------
  262. Generate efficient matchers for HTML tag names that are used in documentation comments.
  263. ClangCommentHTMLTagsProperties
  264. ------------------------------
  265. Generate efficient matchers for HTML tag properties.
  266. ClangCommentHTMLNamedCharacterReferences
  267. ----------------------------------------
  268. Generate function to translate named character references to UTF-8 sequences.
  269. ClangCommentCommandInfo
  270. -----------------------
  271. Generate command properties for commands that are used in documentation comments.
  272. ClangCommentCommandList
  273. -----------------------
  274. Generate list of commands that are used in documentation comments.
  275. ArmNeon
  276. -------
  277. Generate arm_neon.h for clang.
  278. ArmNeonSema
  279. -----------
  280. Generate ARM NEON sema support for clang.
  281. ArmNeonTest
  282. -----------
  283. Generate ARM NEON tests for clang.
  284. AttrDocs
  285. --------
  286. **Purpose**: Creates ``AttributeReference.rst`` from ``AttrDocs.td``, and is
  287. used for documenting user-facing attributes.