Move the getInlineAsmLength virtual method from TAI to TII, where
[oota-llvm.git] / lib / Target / ARM / ARMInstrInfo.cpp
1 //===- ARMInstrInfo.cpp - ARM Instruction Information -----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the ARM implementation of the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "ARMInstrInfo.h"
15 #include "ARM.h"
16 #include "ARMAddressingModes.h"
17 #include "ARMGenInstrInfo.inc"
18 #include "ARMMachineFunctionInfo.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/CodeGen/LiveVariables.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineJumpTableInfo.h"
24 #include "llvm/Target/TargetAsmInfo.h"
25 #include "llvm/Support/CommandLine.h"
26 using namespace llvm;
27
28 ARMInstrInfo::ARMInstrInfo(const ARMSubtarget &STI)
29   : RI(*this, STI), Subtarget(STI) {
30 }
31
32 unsigned ARMInstrInfo::getUnindexedOpcode(unsigned Opc) const {
33   switch (Opc) {
34   default: break;
35   case ARM::LDR_PRE:
36   case ARM::LDR_POST:
37     return ARM::LDR;
38   case ARM::LDRH_PRE:
39   case ARM::LDRH_POST:
40     return ARM::LDRH;
41   case ARM::LDRB_PRE:
42   case ARM::LDRB_POST:
43     return ARM::LDRB;
44   case ARM::LDRSH_PRE:
45   case ARM::LDRSH_POST:
46     return ARM::LDRSH;
47   case ARM::LDRSB_PRE:
48   case ARM::LDRSB_POST:
49     return ARM::LDRSB;
50   case ARM::STR_PRE:
51   case ARM::STR_POST:
52     return ARM::STR;
53   case ARM::STRH_PRE:
54   case ARM::STRH_POST:
55     return ARM::STRH;
56   case ARM::STRB_PRE:
57   case ARM::STRB_POST:
58     return ARM::STRB;
59   }
60
61   return 0;
62 }
63
64 bool ARMInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
65   if (MBB.empty()) return false;
66
67   switch (MBB.back().getOpcode()) {
68   case ARM::BX_RET:   // Return.
69   case ARM::LDM_RET:
70   case ARM::B:
71   case ARM::BR_JTr:   // Jumptable branch.
72   case ARM::BR_JTm:   // Jumptable branch through mem.
73   case ARM::BR_JTadd: // Jumptable branch add to pc.
74     return true;
75   default:
76     break;
77   }
78
79   return false;
80 }
81
82 void ARMInstrInfo::
83 reMaterialize(MachineBasicBlock &MBB,
84               MachineBasicBlock::iterator I,
85               unsigned DestReg, unsigned SubIdx,
86               const MachineInstr *Orig) const {
87   DebugLoc dl = Orig->getDebugLoc();
88   if (Orig->getOpcode() == ARM::MOVi2pieces) {
89     RI.emitLoadConstPool(MBB, I, dl,
90                          DestReg, SubIdx,
91                          Orig->getOperand(1).getImm(),
92                          (ARMCC::CondCodes)Orig->getOperand(2).getImm(),
93                          Orig->getOperand(3).getReg());
94     return;
95   }
96
97   MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
98   MI->getOperand(0).setReg(DestReg);
99   MBB.insert(I, MI);
100 }
101
102 /// Count the number of comma-separated arguments.
103 /// Do not try to detect errors.
104 static unsigned countArguments(const char* p,
105                                const TargetAsmInfo &TAI) {
106   unsigned count = 0;
107   while (*p && isspace(*p) && *p != '\n')
108     p++;
109   count++;
110   while (*p && *p!='\n' &&
111          strncmp(p, TAI.getCommentString(),
112                  strlen(TAI.getCommentString())) != 0) {
113     if (*p==',')
114       count++;
115     p++;
116   }
117   return count;
118 }
119
120 /// Count the length of a string enclosed in quote characters.
121 /// Do not try to detect errors.
122 static unsigned countString(const char *p) {
123   unsigned count = 0;
124   while (*p && isspace(*p) && *p!='\n')
125     p++;
126   if (!*p || *p != '\"')
127     return count;
128   while (*++p && *p != '\"')
129     count++;
130   return count;
131 }
132
133 /// ARM-specific version of TargetAsmInfo::getInlineAsmLength.
134 unsigned ARMInstrInfo::getInlineAsmLength(const char *s,
135                                           const TargetAsmInfo &TAI) const {
136   // Make a lowercase-folded version of s for counting purposes.
137   char *q, *s_copy = (char *)malloc(strlen(s) + 1);
138   strcpy(s_copy, s);
139   for (q=s_copy; *q; q++)
140     *q = tolower(*q);
141   const char *Str = s_copy;
142
143   // Count the number of bytes in the asm.
144   bool atInsnStart = true;
145   bool inTextSection = true;
146   unsigned Length = 0;
147   for (; *Str; ++Str) {
148     if (atInsnStart) {
149       // Skip whitespace
150       while (*Str && isspace(*Str) && *Str != '\n')
151         Str++;
152       // Skip label
153       for (const char* p = Str; *p && !isspace(*p); p++)
154         if (*p == ':') {
155           Str = p+1;
156           while (*Str && isspace(*Str) && *Str != '\n')
157             Str++;
158           break;
159         }
160       
161       if (*Str == 0) break;
162       
163       // Ignore everything from comment char(s) to EOL
164       if (strncmp(Str, TAI.getCommentString(),
165                   strlen(TAI.getCommentString())) == 0)
166         atInsnStart = false;
167       // FIXME do something like the following for non-Darwin
168       else if (*Str == '.' && Subtarget.isTargetDarwin()) {
169         // Directive.
170         atInsnStart = false;
171
172         // Some change the section, but don't generate code.
173         if (strncmp(Str, ".literal4", strlen(".literal4"))==0 ||
174             strncmp(Str, ".literal8", strlen(".literal8"))==0 ||
175             strncmp(Str, ".const", strlen(".const"))==0 ||
176             strncmp(Str, ".constructor", strlen(".constructor"))==0 ||
177             strncmp(Str, ".cstring", strlen(".cstring"))==0 ||
178             strncmp(Str, ".data", strlen(".data"))==0 ||
179             strncmp(Str, ".destructor", strlen(".destructor"))==0 ||
180             strncmp(Str, ".fvmlib_init0", strlen(".fvmlib_init0"))==0 ||
181             strncmp(Str, ".fvmlib_init1", strlen(".fvmlib_init1"))==0 ||
182             strncmp(Str, ".mod_init_func", strlen(".mod_init_func"))==0 ||
183             strncmp(Str, ".mod_term_func", strlen(".mod_term_func"))==0 ||
184             strncmp(Str, ".picsymbol_stub", strlen(".picsymbol_stub"))==0 ||
185             strncmp(Str, ".symbol_stub", strlen(".symbol_stub"))==0 ||
186             strncmp(Str, ".static_data", strlen(".static_data"))==0 ||
187             strncmp(Str, ".section", strlen(".section"))==0 ||
188             strncmp(Str, ".lazy_symbol_pointer", strlen(".lazy_symbol_pointer"))==0 ||
189             strncmp(Str, ".non_lazy_symbol_pointer", strlen(".non_lazy_symbol_pointer"))==0 ||
190             strncmp(Str, ".dyld", strlen(".dyld"))==0 ||
191             strncmp(Str, ".const_data", strlen(".const_data"))==0 ||
192             strncmp(Str, ".objc", strlen(".objc"))==0 ||       //// many directives
193             strncmp(Str, ".static_const", strlen(".static_const"))==0)
194           inTextSection=false;
195         else if (strncmp(Str, ".text", strlen(".text"))==0)
196           inTextSection = true;
197         // Some can't really be handled without implementing significant pieces
198         // of an assembler.  Others require dynamic adjustment of block sizes in
199         // AdjustBBOffsetsAfter; it's a big compile-time speed hit to check every
200         // instruction in there, and none of these are currently used in the kernel.
201         else if (strncmp(Str, ".macro", strlen(".macro"))==0 ||
202                  strncmp(Str, ".if", strlen(".if"))==0 ||
203                  strncmp(Str, ".align", strlen(".align"))==0 ||
204                  strncmp(Str, ".fill", strlen(".fill"))==0 ||
205                  strncmp(Str, ".space", strlen(".space"))==0 ||
206                  strncmp(Str, ".zerofill", strlen(".zerofill"))==0 ||
207                  strncmp(Str, ".p2align", strlen(".p2align"))==0 ||
208                  strncmp(Str, ".p2alignw", strlen(".p2alignw"))==0 ||
209                  strncmp(Str, ".p2alignl", strlen(".p2alignl"))==0 ||
210                  strncmp(Str, ".align32", strlen(".p2align32"))==0 ||
211                  strncmp(Str, ".include", strlen(".include"))==0)
212           cerr << "Directive " << Str << " in asm may lead to invalid offsets for" <<
213                    " constant pools (the assembler will tell you if this happens).\n";
214         // Some generate code, but this is only interesting in the text section.
215         else if (inTextSection) {
216           if (strncmp(Str, ".long", strlen(".long"))==0)
217             Length += 4*countArguments(Str+strlen(".long"), TAI);
218           else if (strncmp(Str, ".short", strlen(".short"))==0)
219             Length += 2*countArguments(Str+strlen(".short"), TAI);
220           else if (strncmp(Str, ".byte", strlen(".byte"))==0)
221             Length += 1*countArguments(Str+strlen(".byte"), TAI);
222           else if (strncmp(Str, ".single", strlen(".single"))==0)
223             Length += 4*countArguments(Str+strlen(".single"), TAI);
224           else if (strncmp(Str, ".double", strlen(".double"))==0)
225             Length += 8*countArguments(Str+strlen(".double"), TAI);
226           else if (strncmp(Str, ".quad", strlen(".quad"))==0)
227             Length += 16*countArguments(Str+strlen(".quad"), TAI);
228           else if (strncmp(Str, ".ascii", strlen(".ascii"))==0)
229             Length += countString(Str+strlen(".ascii"));
230           else if (strncmp(Str, ".asciz", strlen(".asciz"))==0)
231             Length += countString(Str+strlen(".asciz"))+1;
232         }
233       } else if (inTextSection) {
234         // An instruction
235         atInsnStart = false;
236         if (Subtarget.isThumb()) {  // FIXME thumb2
237           // BL and BLX <non-reg> are 4 bytes, all others 2.
238           if (strncmp(Str, "blx", strlen("blx"))==0) {
239             const char* p = Str+3;
240             while (*p && isspace(*p))
241               p++;
242             if (*p == 'r' || *p=='R')
243               Length += 2;    // BLX reg
244             else
245               Length += 4;    // BLX non-reg
246           } else if (strncmp(Str, "bl", strlen("bl"))==0)
247             Length += 4;    // BL
248           else
249             Length += 2;    // Thumb anything else
250         }
251         else
252           Length += 4;    // ARM
253       }
254     }
255     if (*Str == '\n' || *Str == TAI.getSeparatorChar())
256       atInsnStart = true;
257   }
258   free(s_copy);
259   return Length;
260 }
261