1 //===-- llvm/CodeGen/AsmPrinter.h - AsmPrinter Framework --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a class to be used as the base class for target specific
11 // asm writers. This class primarily handles common functionality used by
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CODEGEN_ASMPRINTER_H
17 #define LLVM_CODEGEN_ASMPRINTER_H
19 #include "llvm/CodeGen/MachineFunctionPass.h"
20 #include "llvm/Support/DataTypes.h"
26 class MachineConstantPoolEntry;
31 /// AsmPrinter - This class is intended to be used as a driving class for all
33 class AsmPrinter : public MachineFunctionPass {
34 /// FunctionNumber - This provides a unique ID for each function emitted in
35 /// this translation unit. It is autoincremented by SetupMachineFunction,
36 /// and can be accessed with getFunctionNumber() and
37 /// IncrementFunctionNumber().
39 unsigned FunctionNumber;
42 /// Output stream on which we're printing assembly code.
46 /// Target machine description.
50 /// Target Asm Printer information.
52 const TargetAsmInfo *TAI;
54 /// Name-mangler for global names.
58 /// Cache of mangled name for current function. This is recalculated at the
59 /// beginning of each call to runOnMachineFunction().
61 std::string CurrentFnName;
63 /// CurrentSection - The current section we are emitting to. This is
64 /// controlled and used by the SwitchSection method.
65 std::string CurrentSection;
68 AsmPrinter(std::ostream &o, TargetMachine &TM, const TargetAsmInfo *T);
71 /// SwitchToTextSection - Switch to the specified section of the executable
72 /// if we are not already in it! If GV is non-null and if the global has an
73 /// explicitly requested section, we switch to the section indicated for the
74 /// global instead of NewSection.
76 /// If the new section is an empty string, this method forgets what the
77 /// current section is, but does not emit a .section directive.
79 /// This method is used when about to emit executable code.
81 void SwitchToTextSection(const char *NewSection, const GlobalValue *GV);
83 /// SwitchToDataSection - Switch to the specified section of the executable
84 /// if we are not already in it! If GV is non-null and if the global has an
85 /// explicitly requested section, we switch to the section indicated for the
86 /// global instead of NewSection.
88 /// If the new section is an empty string, this method forgets what the
89 /// current section is, but does not emit a .section directive.
91 /// This method is used when about to emit data. For most assemblers, this
92 /// is the same as the SwitchToTextSection method, but not all assemblers
95 void SwitchToDataSection(const char *NewSection, const GlobalValue *GV);
97 /// getPreferredAlignmentLog - Return the preferred alignment of the
98 /// specified global, returned in log form. This includes an explicitly
99 /// requested alignment (if the global has one).
100 unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
102 /// doInitialization - Set up the AsmPrinter when we are working on a new
103 /// module. If your pass overrides this, it must make sure to explicitly
104 /// call this implementation.
105 bool doInitialization(Module &M);
107 /// doFinalization - Shut down the asmprinter. If you override this in your
108 /// pass, you must make sure to call it explicitly.
109 bool doFinalization(Module &M);
111 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
112 /// instruction, using the specified assembler variant. Targets should
113 /// override this to format as appropriate. This method can return true if
114 /// the operand is erroneous.
115 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
116 unsigned AsmVariant, const char *ExtraCode);
118 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
119 /// instruction, using the specified assembler variant as an address.
120 /// Targets should override this to format as appropriate. This method can
121 /// return true if the operand is erroneous.
122 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
124 const char *ExtraCode);
126 /// SetupMachineFunction - This should be called when a new MachineFunction
127 /// is being processed from runOnMachineFunction.
128 void SetupMachineFunction(MachineFunction &MF);
130 /// getFunctionNumber - Return a unique ID for the current function.
132 unsigned getFunctionNumber() const { return FunctionNumber; }
134 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
135 /// not normally call this, as the counter is automatically bumped by
136 /// SetupMachineFunction.
137 void IncrementFunctionNumber() { FunctionNumber++; }
139 /// EmitConstantPool - Print to the current output stream assembly
140 /// representations of the constants in the constant pool MCP. This is
141 /// used to print out constants which have been "spilled to memory" by
142 /// the code generator.
144 void EmitConstantPool(MachineConstantPool *MCP);
146 /// EmitJumpTableInfo - Print assembly representations of the jump tables
147 /// used by the current function to the current output stream.
149 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI);
151 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
152 /// special global used by LLVM. If so, emit it and return true, otherwise
153 /// do nothing and return false.
154 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
156 /// EmitAlignment - Emit an alignment directive to the specified power of
157 /// two boundary. For example, if you pass in 3 here, you will get an 8
158 /// byte alignment. If a global value is specified, and if that global has
159 /// an explicit alignment requested, it will override the alignment request.
160 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
162 /// EmitZeros - Emit a block of zeros.
164 void EmitZeros(uint64_t NumZeros) const;
166 /// EmitString - Emit a zero-byte-terminated string constant.
168 virtual void EmitString(const ConstantArray *CVA) const;
170 /// EmitConstantValueOnly - Print out the specified constant, without a
171 /// storage class. Only constants of first-class type are allowed here.
172 void EmitConstantValueOnly(const Constant *CV);
174 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
176 void EmitGlobalConstant(const Constant* CV);
178 /// printInlineAsm - This method formats and prints the specified machine
179 /// instruction that is an inline asm.
180 void printInlineAsm(const MachineInstr *MI) const;
182 /// printBasicBlockLabel - This method prints the label for the specified
183 /// MachineBasicBlock
184 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
185 bool printColon = false,
186 bool printComment = true) const;
188 /// printSetLabel - This method prints a set label for the specified
189 /// MachineBasicBlock
190 void printSetLabel(unsigned uid, const MachineBasicBlock *MBB) const;
193 void EmitXXStructorList(Constant *List);
194 void EmitConstantPool(unsigned Alignment, const char *Section,
195 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP);