blob: da7c053eef587dfe64672afbc9ba9f706fc1cc22 [file] [log] [blame]
Chris Lattnerb36cbd02005-07-01 22:44:09 +00001//===-- X86AsmPrinter.h - Convert X86 LLVM code to Intel assembly ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file the shared super class printer that converts from our internal
11// representation of machine-dependent LLVM code to Intel and AT&T format
12// assembly language. This printer is the output mechanism used by `llc'.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef X86ASMPRINTER_H
17#define X86ASMPRINTER_H
18
19#include "X86.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000020#include "X86MachineFunctionInfo.h"
Evan Chengc4c62572006-03-13 23:20:37 +000021#include "X86TargetMachine.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000022#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng3c992d22006-03-07 02:02:57 +000023#include "llvm/CodeGen/DwarfWriter.h"
24#include "llvm/CodeGen/MachineDebugInfo.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000025#include "llvm/ADT/Statistic.h"
Nate Begeman72b286b2005-07-08 00:23:26 +000026#include <set>
27
Chris Lattnerb36cbd02005-07-01 22:44:09 +000028
29namespace llvm {
Chris Lattnerb36cbd02005-07-01 22:44:09 +000030
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000031extern Statistic EmittedInsts;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000032
Evan Cheng25ab6902006-09-08 06:48:29 +000033// FIXME: Move this to CodeGen/AsmPrinter.h
34namespace PICStyle {
35 enum X86AsmPICStyle {
36 Stub, GOT
37 };
38}
39
Jim Laskey563321a2006-09-06 18:34:40 +000040struct VISIBILITY_HIDDEN X86SharedAsmPrinter : public AsmPrinter {
41 DwarfWriter DW;
Evan Cheng3c992d22006-03-07 02:02:57 +000042
Jim Laskey563321a2006-09-06 18:34:40 +000043 X86SharedAsmPrinter(std::ostream &O, X86TargetMachine &TM,
Jim Laskeya0f3d172006-09-07 22:06:40 +000044 const TargetAsmInfo *T)
Evan Cheng25ab6902006-09-08 06:48:29 +000045 : AsmPrinter(O, TM, T), DW(O, this, T), X86PICStyle(PICStyle::GOT) {
Evan Cheng932ad512006-05-25 21:59:08 +000046 Subtarget = &TM.getSubtarget<X86Subtarget>();
47 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +000048
Anton Korobeynikovf8248682006-09-20 22:03:51 +000049 // We have to propagate some information about MachineFunction to
50 // AsmPrinter. It's ok, when we're printing the function, since we have
Chris Lattnere87e1152006-09-26 03:57:53 +000051 // access to MachineFunction and can get the appropriate MachineFunctionInfo.
Anton Korobeynikovf8248682006-09-20 22:03:51 +000052 // Unfortunately, this is not possible when we're printing reference to
53 // Function (e.g. calling it and so on). Even more, there is no way to get the
54 // corresponding MachineFunctions: it can even be not created at all. That's
55 // why we should use additional structure, when we're collecting all necessary
56 // information.
Chris Lattnere87e1152006-09-26 03:57:53 +000057 //
Anton Korobeynikovf8248682006-09-20 22:03:51 +000058 // This structure is using e.g. for name decoration for stdcall & fastcall'ed
59 // function, since we have to use arguments' size for decoration.
Chris Lattnere87e1152006-09-26 03:57:53 +000060 typedef std::map<const Function*, X86FunctionInfo> FMFInfoMap;
Anton Korobeynikovf8248682006-09-20 22:03:51 +000061 FMFInfoMap FunctionInfoMap;
62
63 void decorateName(std::string& Name, const GlobalValue* GV);
64
Chris Lattnerb36cbd02005-07-01 22:44:09 +000065 bool doInitialization(Module &M);
Chris Lattnerb36cbd02005-07-01 22:44:09 +000066 bool doFinalization(Module &M);
67
Evan Cheng3c992d22006-03-07 02:02:57 +000068 void getAnalysisUsage(AnalysisUsage &AU) const {
69 AU.setPreservesAll();
Anton Korobeynikovab4022f2006-10-31 08:31:24 +000070 if (Subtarget->isTargetDarwin() ||
71 Subtarget->isTargetELF() ||
72 Subtarget->isTargetCygwin()) {
Jim Laskey30ffe1b2006-07-27 01:12:23 +000073 AU.addRequired<MachineDebugInfo>();
74 }
Evan Cheng3c992d22006-03-07 02:02:57 +000075 MachineFunctionPass::getAnalysisUsage(AU);
76 }
77
Evan Cheng25ab6902006-09-08 06:48:29 +000078 PICStyle::X86AsmPICStyle X86PICStyle;
79
Evan Cheng932ad512006-05-25 21:59:08 +000080 const X86Subtarget *Subtarget;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000081
Nate Begeman72b286b2005-07-08 00:23:26 +000082 // Necessary for Darwin to print out the apprioriate types of linker stubs
83 std::set<std::string> FnStubs, GVStubs, LinkOnceStubs;
84
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000085 // Necessary for dllexport support
86 std::set<std::string> DLLExportedFns, DLLExportedGVs;
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +000087
88 // Necessary for external weak linkage support
89 std::set<std::string> ExtWeakSymbols;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000090
Chris Lattnerb36cbd02005-07-01 22:44:09 +000091 inline static bool isScale(const MachineOperand &MO) {
92 return MO.isImmediate() &&
93 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
94 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
95 }
96
97 inline static bool isMem(const MachineInstr *MI, unsigned Op) {
98 if (MI->getOperand(Op).isFrameIndex()) return true;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000099 return Op+4 <= MI->getNumOperands() &&
100 MI->getOperand(Op ).isRegister() && isScale(MI->getOperand(Op+1)) &&
Evan Chengc4ee50c2006-02-25 09:56:50 +0000101 MI->getOperand(Op+2).isRegister() &&
102 (MI->getOperand(Op+3).isImmediate() ||
103 MI->getOperand(Op+3).isGlobalAddress() ||
Evan Chengc9676de2006-08-29 22:14:48 +0000104 MI->getOperand(Op+3).isConstantPoolIndex() ||
105 MI->getOperand(Op+3).isJumpTableIndex());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000106 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000107};
108
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000109} // end namespace llvm
110
111#endif