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;
27 class MachineConstantPoolValue;
32 /// AsmPrinter - This class is intended to be used as a driving class for all
34 class AsmPrinter : public MachineFunctionPass {
35 /// FunctionNumber - This provides a unique ID for each function emitted in
36 /// this translation unit. It is autoincremented by SetupMachineFunction,
37 /// and can be accessed with getFunctionNumber() and
38 /// IncrementFunctionNumber().
40 unsigned FunctionNumber;
43 /// Output stream on which we're printing assembly code.
47 /// Target machine description.
51 /// Target Asm Printer information.
53 const TargetAsmInfo *TAI;
55 /// Name-mangler for global names.
59 /// Cache of mangled name for current function. This is recalculated at the
60 /// beginning of each call to runOnMachineFunction().
62 std::string CurrentFnName;
64 /// CurrentSection - The current section we are emitting to. This is
65 /// controlled and used by the SwitchSection method.
66 std::string CurrentSection;
69 AsmPrinter(std::ostream &o, TargetMachine &TM, const TargetAsmInfo *T);
72 /// SwitchToTextSection - Switch to the specified section of the executable
73 /// if we are not already in it! If GV is non-null and if the global has an
74 /// explicitly requested section, we switch to the section indicated for the
75 /// global instead of NewSection.
77 /// If the new section is an empty string, this method forgets what the
78 /// current section is, but does not emit a .section directive.
80 /// This method is used when about to emit executable code.
82 void SwitchToTextSection(const char *NewSection, const GlobalValue *GV = NULL);
84 /// SwitchToDataSection - Switch to the specified section of the executable
85 /// if we are not already in it! If GV is non-null and if the global has an
86 /// explicitly requested section, we switch to the section indicated for the
87 /// global instead of NewSection.
89 /// If the new section is an empty string, this method forgets what the
90 /// current section is, but does not emit a .section directive.
92 /// This method is used when about to emit data. For most assemblers, this
93 /// is the same as the SwitchToTextSection method, but not all assemblers
96 void SwitchToDataSection(const char *NewSection, const GlobalValue *GV = NULL);
98 /// getGlobalLinkName - Returns the asm/link name of of the specified
99 /// global variable. Should be overridden by each target asm printer to
100 /// generate the appropriate value.
101 virtual const std::string getGlobalLinkName(const GlobalVariable *GV) const;
104 /// doInitialization - Set up the AsmPrinter when we are working on a new
105 /// module. If your pass overrides this, it must make sure to explicitly
106 /// call this implementation.
107 bool doInitialization(Module &M);
109 /// doFinalization - Shut down the asmprinter. If you override this in your
110 /// pass, you must make sure to call it explicitly.
111 bool doFinalization(Module &M);
113 /// PrintSpecial - Print information related to the specified machine instr
114 /// that is independent of the operand, and may be independent of the instr
115 /// itself. This can be useful for portably encoding the comment character
116 /// or other bits of target-specific knowledge into the asmstrings. The
117 /// syntax used is ${:comment}. Targets can override this to add support
118 /// for their own strange codes.
119 virtual void PrintSpecial(const MachineInstr *MI, const char *Code);
121 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
122 /// instruction, using the specified assembler variant. Targets should
123 /// override this to format as appropriate. This method can return true if
124 /// the operand is erroneous.
125 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
126 unsigned AsmVariant, const char *ExtraCode);
128 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
129 /// instruction, using the specified assembler variant as an address.
130 /// Targets should override this to format as appropriate. This method can
131 /// return true if the operand is erroneous.
132 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
134 const char *ExtraCode);
136 /// getSectionForFunction - Return the section that we should emit the
137 /// specified function body into. This defaults to 'TextSection'. This
138 /// should most likely be overridden by the target to put linkonce/weak
139 /// functions into special sections.
140 virtual std::string getSectionForFunction(const Function &F) const;
142 /// SetupMachineFunction - This should be called when a new MachineFunction
143 /// is being processed from runOnMachineFunction.
144 void SetupMachineFunction(MachineFunction &MF);
146 /// getFunctionNumber - Return a unique ID for the current function.
148 unsigned getFunctionNumber() const { return FunctionNumber; }
150 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
151 /// not normally call this, as the counter is automatically bumped by
152 /// SetupMachineFunction.
153 void IncrementFunctionNumber() { FunctionNumber++; }
155 /// EmitConstantPool - Print to the current output stream assembly
156 /// representations of the constants in the constant pool MCP. This is
157 /// used to print out constants which have been "spilled to memory" by
158 /// the code generator.
160 void EmitConstantPool(MachineConstantPool *MCP);
162 /// EmitJumpTableInfo - Print assembly representations of the jump tables
163 /// used by the current function to the current output stream.
165 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
167 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
168 /// special global used by LLVM. If so, emit it and return true, otherwise
169 /// do nothing and return false.
170 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
172 /// EmitAlignment - Emit an alignment directive to the specified power of
173 /// two boundary. For example, if you pass in 3 here, you will get an 8
174 /// byte alignment. If a global value is specified, and if that global has
175 /// an explicit alignment requested, it will override the alignment request.
176 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
178 /// EmitZeros - Emit a block of zeros.
180 void EmitZeros(uint64_t NumZeros) const;
182 /// EmitString - Emit a zero-byte-terminated string constant.
184 virtual void EmitString(const ConstantArray *CVA) const;
186 /// EmitConstantValueOnly - Print out the specified constant, without a
187 /// storage class. Only constants of first-class type are allowed here.
188 void EmitConstantValueOnly(const Constant *CV);
190 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
192 void EmitGlobalConstant(const Constant* CV);
194 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
196 /// printInlineAsm - This method formats and prints the specified machine
197 /// instruction that is an inline asm.
198 void printInlineAsm(const MachineInstr *MI) const;
200 /// printBasicBlockLabel - This method prints the label for the specified
201 /// MachineBasicBlock
202 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
203 bool printColon = false,
204 bool printComment = true) const;
206 /// printSetLabel - This method prints a set label for the specified
207 /// MachineBasicBlock
208 void printSetLabel(unsigned uid, const MachineBasicBlock *MBB) const;
209 void printSetLabel(unsigned uid, unsigned uid2,
210 const MachineBasicBlock *MBB) const;
212 /// printDataDirective - This method prints the asm directive for the
214 void printDataDirective(const Type *type);
217 void EmitLLVMUsedList(Constant *List);
218 void EmitXXStructorList(Constant *List);
219 void EmitConstantPool(unsigned Alignment, const char *Section,
220 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP);