AsmPrinter.h revision e2b1f1188fd8c28977b5cf2a6842fc7bbb008713
1a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner//===-- llvm/CodeGen/AsmPrinter.h - AsmPrinter Framework --------*- C++ -*-===//
2ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman//
3a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner//                     The LLVM Compiler Infrastructure
4a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner//
5a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner// This file was developed by the LLVM research group and is distributed under
6a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner// the University of Illinois Open Source License. See LICENSE.TXT for details.
7ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman//
8a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner//===----------------------------------------------------------------------===//
9a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner//
10a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner// This class is intended to be used as a base class for target-specific
11a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner// asmwriters.  This class primarily takes care of printing global constants,
12a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner// which are printed in a very similar way across all targets.
13a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner//
14a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner//===----------------------------------------------------------------------===//
15a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner
16a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner#ifndef LLVM_CODEGEN_ASMPRINTER_H
17a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner#define LLVM_CODEGEN_ASMPRINTER_H
18a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner
19a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner#include "llvm/CodeGen/MachineFunctionPass.h"
20190f3ba2aa58dc2e73a5f90dff15015a1ffa226bChris Lattner#include "llvm/Support/DataTypes.h"
21a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner
22a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattnernamespace llvm {
23a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner  class Constant;
24a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner  class Mangler;
25a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner
26a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner  class AsmPrinter : public MachineFunctionPass {
27e2b1f1188fd8c28977b5cf2a6842fc7bbb008713Chris Lattner    /// CurrentSection - The current section we are emitting to.  This is
28e2b1f1188fd8c28977b5cf2a6842fc7bbb008713Chris Lattner    /// controlled and used by the SwitchSection method.
29e2b1f1188fd8c28977b5cf2a6842fc7bbb008713Chris Lattner    std::string CurrentSection;
30a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner  protected:
31a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    /// Output stream on which we're printing assembly code.
32a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    ///
33a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    std::ostream &O;
34a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner
35a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    /// Target machine description.
36a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    ///
37a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    TargetMachine &TM;
38a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner
39a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    /// Name-mangler for global names.
40a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    ///
41a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    Mangler *Mang;
42a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner
43a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    /// Cache of mangled name for current function. This is recalculated at the
44a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    /// beginning of each call to runOnMachineFunction().
45a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    ///
46a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    std::string CurrentFnName;
47a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner
4846d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner    //===------------------------------------------------------------------===//
49f1cb7953947900035edd090ad4b81b1b2bb48791Chris Lattner    // Properties to be set by the derived class ctor, used to configure the
50f1cb7953947900035edd090ad4b81b1b2bb48791Chris Lattner    // asmwriter.
51f1cb7953947900035edd090ad4b81b1b2bb48791Chris Lattner
52b6e678b1c8ff07e3ba681f1f365762ef8c4d84f7Chris Lattner    /// CommentString - This indicates the comment character used by the
5375585268a7761acfce320d1f4da059bea82d2ecdChris Lattner    /// assembler.
54b6e678b1c8ff07e3ba681f1f365762ef8c4d84f7Chris Lattner    const char *CommentString;     // Defaults to "#"
5575585268a7761acfce320d1f4da059bea82d2ecdChris Lattner
56af2bf0ab306f7a9a3fe2af028656514f7ef53143Chris Lattner    /// GlobalPrefix - If this is set to a non-empty string, it is prepended
57af2bf0ab306f7a9a3fe2af028656514f7ef53143Chris Lattner    /// onto all global symbols.  This is often used for "_" or ".".
5875585268a7761acfce320d1f4da059bea82d2ecdChris Lattner    const char *GlobalPrefix;    // Defaults to ""
59f1cb7953947900035edd090ad4b81b1b2bb48791Chris Lattner
60adb0a068c602b17f9d6602d08f561feea952c139Chris Lattner    /// PrivateGlobalPrefix - This prefix is used for globals like constant
61adb0a068c602b17f9d6602d08f561feea952c139Chris Lattner    /// pool entries that are completely private to the .o file and should not
62adb0a068c602b17f9d6602d08f561feea952c139Chris Lattner    /// have names in the .o file.  This is often "." or "L".
63adb0a068c602b17f9d6602d08f561feea952c139Chris Lattner    const char *PrivateGlobalPrefix;   // Defaults to "."
64adb0a068c602b17f9d6602d08f561feea952c139Chris Lattner
65855a51974134e818232e52d63a2c3b2b48c8e244Duraid Madina    /// GlobalVarAddrPrefix/Suffix - If these are nonempty, these strings
66855a51974134e818232e52d63a2c3b2b48c8e244Duraid Madina    /// will enclose any GlobalVariable (that isn't a function)
67855a51974134e818232e52d63a2c3b2b48c8e244Duraid Madina    ///
68855a51974134e818232e52d63a2c3b2b48c8e244Duraid Madina    const char *GlobalVarAddrPrefix;       // Defaults to ""
69855a51974134e818232e52d63a2c3b2b48c8e244Duraid Madina    const char *GlobalVarAddrSuffix;       // Defaults to ""
70855a51974134e818232e52d63a2c3b2b48c8e244Duraid Madina
71855a51974134e818232e52d63a2c3b2b48c8e244Duraid Madina    /// FunctionAddrPrefix/Suffix - If these are nonempty, these strings
72855a51974134e818232e52d63a2c3b2b48c8e244Duraid Madina    /// will enclose any GlobalVariable that points to a function.
73855a51974134e818232e52d63a2c3b2b48c8e244Duraid Madina    /// For example, this is used by the IA64 backend to materialize
74855a51974134e818232e52d63a2c3b2b48c8e244Duraid Madina    /// function descriptors, by decorating the ".data8" object with the
75855a51974134e818232e52d63a2c3b2b48c8e244Duraid Madina    /// @fptr( ) link-relocation operator.
76855a51974134e818232e52d63a2c3b2b48c8e244Duraid Madina    ///
77855a51974134e818232e52d63a2c3b2b48c8e244Duraid Madina    const char *FunctionAddrPrefix;       // Defaults to ""
78855a51974134e818232e52d63a2c3b2b48c8e244Duraid Madina    const char *FunctionAddrSuffix;       // Defaults to ""
79855a51974134e818232e52d63a2c3b2b48c8e244Duraid Madina
8046d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner    /// ZeroDirective - this should be set to the directive used to get some
8146d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner    /// number of zero bytes emitted to the current section.  Common cases are
8246d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner    /// "\t.zero\t" and "\t.space\t".  If this is set to null, the
8346d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner    /// Data*bitsDirective's will be used to emit zero bytes.
8446d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner    const char *ZeroDirective;   // Defaults to "\t.zero\t"
8546d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner
8646d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner    /// AsciiDirective - This directive allows emission of an ascii string with
8746d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner    /// the standard C escape characters embedded into it.
8838b6d78112d975d288615db2913418c8bd1b0956Chris Lattner    const char *AsciiDirective;  // Defaults to "\t.ascii\t"
8938b6d78112d975d288615db2913418c8bd1b0956Chris Lattner
9038b6d78112d975d288615db2913418c8bd1b0956Chris Lattner    /// AscizDirective - If not null, this allows for special handling of
9138b6d78112d975d288615db2913418c8bd1b0956Chris Lattner    /// zero terminated strings on this target.  This is commonly supported as
9238b6d78112d975d288615db2913418c8bd1b0956Chris Lattner    /// ".asciz".  If a target doesn't support this, it can be set to null.
9338b6d78112d975d288615db2913418c8bd1b0956Chris Lattner    const char *AscizDirective;  // Defaults to "\t.asciz\t"
94ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman
9546d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner    /// DataDirectives - These directives are used to output some unit of
9646d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner    /// integer data to the current section.  If a data directive is set to
9746d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner    /// null, smaller data directives will be used to emit the large sizes.
9846d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner    const char *Data8bitsDirective;   // Defaults to "\t.byte\t"
9946d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner    const char *Data16bitsDirective;  // Defaults to "\t.short\t"
10046d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner    const char *Data32bitsDirective;  // Defaults to "\t.long\t"
10146d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner    const char *Data64bitsDirective;  // Defaults to "\t.quad\t"
10246d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner
103bfddc2030a7e67b9e0c42276525d6932375ff261Chris Lattner    /// AlignDirective - The directive used to emit round up to an alignment
104bfddc2030a7e67b9e0c42276525d6932375ff261Chris Lattner    /// boundary.
105bfddc2030a7e67b9e0c42276525d6932375ff261Chris Lattner    ///
106bfddc2030a7e67b9e0c42276525d6932375ff261Chris Lattner    const char *AlignDirective;       // Defaults to "\t.align\t"
107bfddc2030a7e67b9e0c42276525d6932375ff261Chris Lattner
108bfddc2030a7e67b9e0c42276525d6932375ff261Chris Lattner    /// AlignmentIsInBytes - If this is true (the default) then the asmprinter
109bfddc2030a7e67b9e0c42276525d6932375ff261Chris Lattner    /// emits ".align N" directives, where N is the number of bytes to align to.
110bfddc2030a7e67b9e0c42276525d6932375ff261Chris Lattner    /// Otherwise, it emits ".align log2(N)", e.g. 3 to align to an 8 byte
111bfddc2030a7e67b9e0c42276525d6932375ff261Chris Lattner    /// boundary.
112bfddc2030a7e67b9e0c42276525d6932375ff261Chris Lattner    bool AlignmentIsInBytes;          // Defaults to true
113bfddc2030a7e67b9e0c42276525d6932375ff261Chris Lattner
114f1cb7953947900035edd090ad4b81b1b2bb48791Chris Lattner    AsmPrinter(std::ostream &o, TargetMachine &tm)
11546d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner      : O(o), TM(tm),
116b6e678b1c8ff07e3ba681f1f365762ef8c4d84f7Chris Lattner        CommentString("#"),
11746d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner        GlobalPrefix(""),
118adb0a068c602b17f9d6602d08f561feea952c139Chris Lattner        PrivateGlobalPrefix("."),
119664044b2bddc82ed26e7a71015a084c75a645e50Duraid Madina        GlobalVarAddrPrefix(""),
120664044b2bddc82ed26e7a71015a084c75a645e50Duraid Madina        GlobalVarAddrSuffix(""),
121664044b2bddc82ed26e7a71015a084c75a645e50Duraid Madina        FunctionAddrPrefix(""),
122664044b2bddc82ed26e7a71015a084c75a645e50Duraid Madina        FunctionAddrSuffix(""),
12346d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner        ZeroDirective("\t.zero\t"),
12446d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner        AsciiDirective("\t.ascii\t"),
12538b6d78112d975d288615db2913418c8bd1b0956Chris Lattner        AscizDirective("\t.asciz\t"),
12646d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner        Data8bitsDirective("\t.byte\t"),
12746d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner        Data16bitsDirective("\t.short\t"),
128800ade1e19df83d1bd300e7bb0f5e64e43f20b66Chris Lattner        Data32bitsDirective("\t.long\t"),
129bfddc2030a7e67b9e0c42276525d6932375ff261Chris Lattner        Data64bitsDirective("\t.quad\t"),
130bfddc2030a7e67b9e0c42276525d6932375ff261Chris Lattner        AlignDirective("\t.align\t"),
131bfddc2030a7e67b9e0c42276525d6932375ff261Chris Lattner        AlignmentIsInBytes(true) {
132bfddc2030a7e67b9e0c42276525d6932375ff261Chris Lattner    }
133a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner
134e2b1f1188fd8c28977b5cf2a6842fc7bbb008713Chris Lattner    /// SwitchSection - Switch to the specified section of the executable if we
135e2b1f1188fd8c28977b5cf2a6842fc7bbb008713Chris Lattner    /// are not already in it!  If GV is non-null and if the global has an
136e2b1f1188fd8c28977b5cf2a6842fc7bbb008713Chris Lattner    /// explicitly requested section, we switch to the section indicated for the
137e2b1f1188fd8c28977b5cf2a6842fc7bbb008713Chris Lattner    /// global instead of NewSection.
138e2b1f1188fd8c28977b5cf2a6842fc7bbb008713Chris Lattner    ///
139e2b1f1188fd8c28977b5cf2a6842fc7bbb008713Chris Lattner    /// If the new section is an empty string, this method forgets what the
140e2b1f1188fd8c28977b5cf2a6842fc7bbb008713Chris Lattner    /// current section is, but does not emit a .section directive.
141e2b1f1188fd8c28977b5cf2a6842fc7bbb008713Chris Lattner    ///
142e2b1f1188fd8c28977b5cf2a6842fc7bbb008713Chris Lattner    void SwitchSection(const char *NewSection, const GlobalValue *GV);
143e2b1f1188fd8c28977b5cf2a6842fc7bbb008713Chris Lattner
144a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    /// doInitialization - Set up the AsmPrinter when we are working on a new
145a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    /// module.  If your pass overrides this, it must make sure to explicitly
146a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    /// call this implementation.
147a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    bool doInitialization(Module &M);
148a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner
149a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    /// doFinalization - Shut down the asmprinter.  If you override this in your
150a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    /// pass, you must make sure to call it explicitly.
151a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    bool doFinalization(Module &M);
152a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner
153a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    /// setupMachineFunction - This should be called when a new MachineFunction
154a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    /// is being processed from runOnMachineFunction.
155a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    void setupMachineFunction(MachineFunction &MF);
156a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner
157bfddc2030a7e67b9e0c42276525d6932375ff261Chris Lattner    /// emitAlignment - Emit an alignment directive to the specified power of
158bfddc2030a7e67b9e0c42276525d6932375ff261Chris Lattner    /// two boundary.  For example, if you pass in 3 here, you will get an 8
159a1ab72daadbdc5b499ce7e7e97c4b80efad46736Chris Lattner    /// byte alignment.  If a global value is specified, and if that global has
160a1ab72daadbdc5b499ce7e7e97c4b80efad46736Chris Lattner    /// an explicit alignment requested, it will override the alignment request.
161a1ab72daadbdc5b499ce7e7e97c4b80efad46736Chris Lattner    void emitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
162bfddc2030a7e67b9e0c42276525d6932375ff261Chris Lattner
16330bad4af40d0126f888f601fb46d01e2b39dbc43Chris Lattner    /// emitZeros - Emit a block of zeros.
16430bad4af40d0126f888f601fb46d01e2b39dbc43Chris Lattner    ///
165190f3ba2aa58dc2e73a5f90dff15015a1ffa226bChris Lattner    void emitZeros(uint64_t NumZeros) const;
16630bad4af40d0126f888f601fb46d01e2b39dbc43Chris Lattner
167a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    /// emitConstantValueOnly - Print out the specified constant, without a
168a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    /// storage class.  Only constants of first-class type are allowed here.
169a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner    void emitConstantValueOnly(const Constant *CV);
17046d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner
17146d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner    /// emitGlobalConstant - Print a general LLVM constant to the .s file.
17246d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner    ///
17346d6126e669ab83383d47fb465d2a7d0c60169e5Chris Lattner    void emitGlobalConstant(const Constant* CV);
174a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner  };
175a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner}
176a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner
177a80ba71efe5be5012128e2db0dd29b024e00105aChris Lattner#endif
178