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