Jim Laskey | 8e8de8f | 2006-09-07 22:05:02 +0000 | [diff] [blame] | 1 | //===-- X86TargetAsmInfo.cpp - X86 asm properties ---------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by James M. Laskey and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the declarations of the X86TargetAsmInfo properties. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "X86TargetAsmInfo.h" |
| 15 | #include "X86TargetMachine.h" |
| 16 | #include "X86Subtarget.h" |
Reid Spencer | 7aa8a45 | 2007-01-12 23:22:14 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 18 | #include "llvm/InlineAsm.h" |
| 19 | #include "llvm/Instructions.h" |
| 20 | #include "llvm/Module.h" |
| 21 | #include "llvm/ADT/StringExtras.h" |
Jim Laskey | 8e8de8f | 2006-09-07 22:05:02 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Andrew Lenharth | 6c0695f | 2006-11-28 19:52:49 +0000 | [diff] [blame] | 24 | static const char* x86_asm_table[] = {"{si}", "S", |
| 25 | "{di}", "D", |
| 26 | "{ax}", "a", |
| 27 | "{cx}", "c", |
Andrew Lenharth | 6c0695f | 2006-11-28 19:52:49 +0000 | [diff] [blame] | 28 | "{memory}", "memory", |
| 29 | "{flags}", "", |
| 30 | "{dirflag}", "", |
| 31 | "{fpsr}", "", |
| 32 | "{cc}", "cc", |
| 33 | 0,0}; |
| 34 | |
Jim Laskey | 8e8de8f | 2006-09-07 22:05:02 +0000 | [diff] [blame] | 35 | X86TargetAsmInfo::X86TargetAsmInfo(const X86TargetMachine &TM) { |
| 36 | const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>(); |
| 37 | |
Chris Lattner | afbfded | 2006-10-05 02:43:52 +0000 | [diff] [blame] | 38 | // FIXME - Should be simplified. |
Andrew Lenharth | 6c0695f | 2006-11-28 19:52:49 +0000 | [diff] [blame] | 39 | |
| 40 | AsmTransCBE = x86_asm_table; |
| 41 | |
Jim Laskey | 8e8de8f | 2006-09-07 22:05:02 +0000 | [diff] [blame] | 42 | switch (Subtarget->TargetType) { |
| 43 | case X86Subtarget::isDarwin: |
| 44 | AlignmentIsInBytes = false; |
| 45 | GlobalPrefix = "_"; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 46 | if (!Subtarget->is64Bit()) |
| 47 | Data64bitsDirective = 0; // we can't emit a 64-bit unit |
Jim Laskey | 8e8de8f | 2006-09-07 22:05:02 +0000 | [diff] [blame] | 48 | ZeroDirective = "\t.space\t"; // ".space N" emits N zeros. |
| 49 | PrivateGlobalPrefix = "L"; // Marker for constant pool idxs |
Chris Lattner | 42ca450 | 2007-01-17 17:43:33 +0000 | [diff] [blame] | 50 | BSSSection = 0; // no BSS section. |
| 51 | ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill |
Jim Laskey | 8e8de8f | 2006-09-07 22:05:02 +0000 | [diff] [blame] | 52 | ConstantPoolSection = "\t.const\n"; |
Chris Lattner | afbfded | 2006-10-05 02:43:52 +0000 | [diff] [blame] | 53 | JumpTableDataSection = "\t.const\n"; |
Evan Cheng | 8910c1c | 2006-10-26 19:18:18 +0000 | [diff] [blame] | 54 | CStringSection = "\t.cstring"; |
Jim Laskey | 8e8de8f | 2006-09-07 22:05:02 +0000 | [diff] [blame] | 55 | FourByteConstantSection = "\t.literal4\n"; |
| 56 | EightByteConstantSection = "\t.literal8\n"; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 57 | if (Subtarget->is64Bit()) |
| 58 | SixteenByteConstantSection = "\t.literal16\n"; |
Jim Laskey | 8e8de8f | 2006-09-07 22:05:02 +0000 | [diff] [blame] | 59 | LCOMMDirective = "\t.lcomm\t"; |
| 60 | COMMDirectiveTakesAlignment = false; |
| 61 | HasDotTypeDotSizeDirective = false; |
| 62 | StaticCtorsSection = ".mod_init_func"; |
| 63 | StaticDtorsSection = ".mod_term_func"; |
| 64 | InlineAsmStart = "# InlineAsm Start"; |
| 65 | InlineAsmEnd = "# InlineAsm End"; |
| 66 | SetDirective = "\t.set"; |
Chris Lattner | a53115b | 2006-09-26 03:39:53 +0000 | [diff] [blame] | 67 | UsedDirective = "\t.no_dead_strip\t"; |
Evan Cheng | 8752ce6 | 2006-12-01 20:47:11 +0000 | [diff] [blame] | 68 | WeakRefDirective = "\t.weak_reference\t"; |
Chris Lattner | 43bbc5c | 2007-01-14 06:29:53 +0000 | [diff] [blame] | 69 | HiddenDirective = "\t.private_extern\t"; |
Jim Laskey | 8e8de8f | 2006-09-07 22:05:02 +0000 | [diff] [blame] | 70 | |
Chris Lattner | b266ccd | 2007-01-18 01:15:58 +0000 | [diff] [blame^] | 71 | // In non-PIC modes, emit a special label before jump tables so that the |
| 72 | // linker can perform more accurate dead code stripping. |
| 73 | if (TM.getRelocationModel() != Reloc::PIC_) { |
| 74 | // Emit a local label that is preserved until the linker runs. |
| 75 | JumpTableSpecialLabelPrefix = "l"; |
| 76 | } |
| 77 | |
Jim Laskey | 8e8de8f | 2006-09-07 22:05:02 +0000 | [diff] [blame] | 78 | NeedsSet = true; |
| 79 | DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug"; |
| 80 | DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug"; |
| 81 | DwarfLineSection = ".section __DWARF,__debug_line,regular,debug"; |
| 82 | DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug"; |
| 83 | DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug"; |
| 84 | DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug"; |
| 85 | DwarfStrSection = ".section __DWARF,__debug_str,regular,debug"; |
| 86 | DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug"; |
| 87 | DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug"; |
| 88 | DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug"; |
| 89 | DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug"; |
| 90 | break; |
Reid Spencer | 02b8511 | 2006-10-30 22:32:30 +0000 | [diff] [blame] | 91 | |
| 92 | case X86Subtarget::isELF: |
| 93 | // Set up DWARF directives |
| 94 | HasLEB128 = true; // Target asm supports leb128 directives (little-endian) |
| 95 | // bool HasLEB128; // Defaults to false. |
| 96 | // hasDotLoc - True if target asm supports .loc directives. |
| 97 | // bool HasDotLoc; // Defaults to false. |
| 98 | // HasDotFile - True if target asm supports .file directives. |
| 99 | // bool HasDotFile; // Defaults to false. |
Chris Lattner | a3bfdd4 | 2007-01-03 18:16:48 +0000 | [diff] [blame] | 100 | PrivateGlobalPrefix = ".L"; |
Evan Cheng | 8752ce6 | 2006-12-01 20:47:11 +0000 | [diff] [blame] | 101 | WeakRefDirective = "\t.weak\t"; |
Reid Spencer | 02b8511 | 2006-10-30 22:32:30 +0000 | [diff] [blame] | 102 | DwarfRequiresFrameSection = false; |
| 103 | DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits"; |
| 104 | DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits"; |
| 105 | DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits"; |
| 106 | DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits"; |
| 107 | DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits"; |
| 108 | DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits"; |
| 109 | DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits"; |
| 110 | DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits"; |
| 111 | DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits"; |
| 112 | DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits"; |
| 113 | DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits"; |
| 114 | break; |
| 115 | |
Jim Laskey | 8e8de8f | 2006-09-07 22:05:02 +0000 | [diff] [blame] | 116 | case X86Subtarget::isCygwin: |
Anton Korobeynikov | 317848f | 2007-01-03 11:43:14 +0000 | [diff] [blame] | 117 | case X86Subtarget::isMingw: |
Jim Laskey | 8e8de8f | 2006-09-07 22:05:02 +0000 | [diff] [blame] | 118 | GlobalPrefix = "_"; |
Anton Korobeynikov | 317848f | 2007-01-03 11:43:14 +0000 | [diff] [blame] | 119 | LCOMMDirective = "\t.lcomm\t"; |
Jim Laskey | 8e8de8f | 2006-09-07 22:05:02 +0000 | [diff] [blame] | 120 | COMMDirectiveTakesAlignment = false; |
| 121 | HasDotTypeDotSizeDirective = false; |
| 122 | StaticCtorsSection = "\t.section .ctors,\"aw\""; |
| 123 | StaticDtorsSection = "\t.section .dtors,\"aw\""; |
Anton Korobeynikov | 1a85645 | 2007-01-14 11:49:39 +0000 | [diff] [blame] | 124 | HiddenDirective = NULL; |
Anton Korobeynikov | ab4022f | 2006-10-31 08:31:24 +0000 | [diff] [blame] | 125 | |
| 126 | // Set up DWARF directives |
| 127 | HasLEB128 = true; // Target asm supports leb128 directives (little-endian) |
| 128 | PrivateGlobalPrefix = "L"; // Prefix for private global symbols |
| 129 | DwarfRequiresFrameSection = false; |
| 130 | DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\""; |
| 131 | DwarfInfoSection = "\t.section\t.debug_info,\"dr\""; |
| 132 | DwarfLineSection = "\t.section\t.debug_line,\"dr\""; |
| 133 | DwarfFrameSection = "\t.section\t.debug_frame,\"dr\""; |
| 134 | DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\""; |
| 135 | DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\""; |
| 136 | DwarfStrSection = "\t.section\t.debug_str,\"dr\""; |
| 137 | DwarfLocSection = "\t.section\t.debug_loc,\"dr\""; |
| 138 | DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\""; |
| 139 | DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\""; |
| 140 | DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\""; |
| 141 | break; |
Bill Wendling | 1150286 | 2007-01-16 04:13:03 +0000 | [diff] [blame] | 142 | |
Jim Laskey | 8e8de8f | 2006-09-07 22:05:02 +0000 | [diff] [blame] | 143 | case X86Subtarget::isWindows: |
| 144 | GlobalPrefix = "_"; |
| 145 | HasDotTypeDotSizeDirective = false; |
| 146 | break; |
Bill Wendling | 1150286 | 2007-01-16 04:13:03 +0000 | [diff] [blame] | 147 | |
Jim Laskey | 8e8de8f | 2006-09-07 22:05:02 +0000 | [diff] [blame] | 148 | default: break; |
| 149 | } |
| 150 | |
| 151 | if (Subtarget->isFlavorIntel()) { |
| 152 | GlobalPrefix = "_"; |
| 153 | CommentString = ";"; |
| 154 | |
| 155 | PrivateGlobalPrefix = "$"; |
| 156 | AlignDirective = "\talign\t"; |
| 157 | ZeroDirective = "\tdb\t"; |
| 158 | ZeroDirectiveSuffix = " dup(0)"; |
| 159 | AsciiDirective = "\tdb\t"; |
| 160 | AscizDirective = 0; |
| 161 | Data8bitsDirective = "\tdb\t"; |
| 162 | Data16bitsDirective = "\tdw\t"; |
| 163 | Data32bitsDirective = "\tdd\t"; |
| 164 | Data64bitsDirective = "\tdq\t"; |
| 165 | HasDotTypeDotSizeDirective = false; |
| 166 | |
| 167 | TextSection = "_text"; |
| 168 | DataSection = "_data"; |
Anton Korobeynikov | 24287dd | 2006-12-19 21:04:20 +0000 | [diff] [blame] | 169 | JumpTableDataSection = NULL; |
Jim Laskey | 8e8de8f | 2006-09-07 22:05:02 +0000 | [diff] [blame] | 170 | SwitchToSectionDirective = ""; |
| 171 | TextSectionStartSuffix = "\tsegment 'CODE'"; |
| 172 | DataSectionStartSuffix = "\tsegment 'DATA'"; |
| 173 | SectionEndDirectiveSuffix = "\tends\n"; |
| 174 | } |
Bill Wendling | cb90099 | 2007-01-16 09:29:17 +0000 | [diff] [blame] | 175 | |
| 176 | AssemblerDialect = Subtarget->getAsmFlavor(); |
Jim Laskey | 8e8de8f | 2006-09-07 22:05:02 +0000 | [diff] [blame] | 177 | } |
Chris Lattner | afbfded | 2006-10-05 02:43:52 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 179 | bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const { |
| 180 | // FIXME: this should verify that we are targetting a 486 or better. If not, |
| 181 | // we will turn this bswap into something that will be lowered to logical ops |
| 182 | // instead of emitting the bswap asm. For now, we don't support 486 or lower |
| 183 | // so don't worry about this. |
| 184 | |
| 185 | // Verify this is a simple bswap. |
| 186 | if (CI->getNumOperands() != 2 || |
| 187 | CI->getType() != CI->getOperand(1)->getType() || |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 188 | !CI->getType()->isInteger()) |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 189 | return false; |
| 190 | |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 191 | const Type *Ty = CI->getType(); |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 192 | const char *IntName; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 193 | if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) { |
| 194 | unsigned BitWidth = ITy->getBitWidth(); |
Chris Lattner | 286bf46 | 2007-01-12 23:28:32 +0000 | [diff] [blame] | 195 | if (BitWidth == 16) |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 196 | IntName = "llvm.bswap.i16"; |
Chris Lattner | 286bf46 | 2007-01-12 23:28:32 +0000 | [diff] [blame] | 197 | else if (BitWidth == 32) |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 198 | IntName = "llvm.bswap.i32"; |
Chris Lattner | 286bf46 | 2007-01-12 23:28:32 +0000 | [diff] [blame] | 199 | else if (BitWidth == 64) |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 200 | IntName = "llvm.bswap.i64"; |
| 201 | else |
| 202 | return false; |
| 203 | } else |
| 204 | return false; |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 205 | |
| 206 | // Okay, we can do this xform, do so now. |
| 207 | Module *M = CI->getParent()->getParent()->getParent(); |
Chris Lattner | 52a457c | 2007-01-07 07:24:32 +0000 | [diff] [blame] | 208 | Constant *Int = M->getOrInsertFunction(IntName, Ty, Ty, (Type*)0); |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 209 | |
| 210 | Value *Op = CI->getOperand(1); |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 211 | Op = new CallInst(Int, Op, CI->getName(), CI); |
| 212 | |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 213 | CI->replaceAllUsesWith(Op); |
| 214 | CI->eraseFromParent(); |
| 215 | return true; |
| 216 | } |
| 217 | |
| 218 | |
| 219 | bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const { |
| 220 | InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue()); |
Chris Lattner | 5d52135 | 2006-11-29 01:48:01 +0000 | [diff] [blame] | 221 | std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints(); |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 222 | |
| 223 | std::string AsmStr = IA->getAsmString(); |
| 224 | |
| 225 | // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a" |
| 226 | std::vector<std::string> AsmPieces; |
| 227 | SplitString(AsmStr, AsmPieces, "\n"); // ; as separator? |
| 228 | |
| 229 | switch (AsmPieces.size()) { |
| 230 | default: return false; |
| 231 | case 1: |
| 232 | AsmStr = AsmPieces[0]; |
| 233 | AsmPieces.clear(); |
| 234 | SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace. |
| 235 | |
Chris Lattner | 5d52135 | 2006-11-29 01:48:01 +0000 | [diff] [blame] | 236 | // bswap $0 |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 237 | if (AsmPieces.size() == 2 && |
| 238 | AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") { |
| 239 | // No need to check constraints, nothing other than the equivalent of |
| 240 | // "=r,0" would be valid here. |
| 241 | return LowerToBSwap(CI); |
| 242 | } |
| 243 | break; |
Chris Lattner | 5d52135 | 2006-11-29 01:48:01 +0000 | [diff] [blame] | 244 | case 3: |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 245 | if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 && |
Chris Lattner | 5d52135 | 2006-11-29 01:48:01 +0000 | [diff] [blame] | 246 | Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" && |
| 247 | Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") { |
| 248 | // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64 |
| 249 | std::vector<std::string> Words; |
| 250 | SplitString(AsmPieces[0], Words, " \t"); |
| 251 | if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") { |
| 252 | Words.clear(); |
| 253 | SplitString(AsmPieces[1], Words, " \t"); |
| 254 | if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") { |
| 255 | Words.clear(); |
| 256 | SplitString(AsmPieces[2], Words, " \t,"); |
| 257 | if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" && |
| 258 | Words[2] == "%edx") { |
| 259 | return LowerToBSwap(CI); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | break; |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 265 | } |
| 266 | return false; |
| 267 | } |