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