05658106bbeb58127d82b755e9053d294707fc95
[oota-llvm.git] / lib / Target / Mips / Mips16InstrInfo.cpp
1 //===-- Mips16InstrInfo.cpp - Mips16 Instruction Information --------------===//
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 Mips16 implementation of the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "Mips16InstrInfo.h"
14 #include "InstPrinter/MipsInstPrinter.h"
15 #include "MipsMachineFunction.h"
16 #include "MipsTargetMachine.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/CodeGen/RegisterScavenging.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/TargetRegistry.h"
27 #include <cctype>
28
29 using namespace llvm;
30
31 static cl::opt<bool> NeverUseSaveRestore(
32   "mips16-never-use-save-restore",
33   cl::init(false),
34   cl::desc("For testing ability to adjust stack pointer "
35            "without save/restore instruction"),
36   cl::Hidden);
37
38
39 Mips16InstrInfo::Mips16InstrInfo(MipsTargetMachine &tm)
40   : MipsInstrInfo(tm, Mips::Bimm16),
41     RI(*tm.getSubtargetImpl()) {}
42
43 const MipsRegisterInfo &Mips16InstrInfo::getRegisterInfo() const {
44   return RI;
45 }
46
47 /// isLoadFromStackSlot - If the specified machine instruction is a direct
48 /// load from a stack slot, return the virtual or physical register number of
49 /// the destination along with the FrameIndex of the loaded stack slot.  If
50 /// not, return 0.  This predicate must return 0 if the instruction has
51 /// any side effects other than loading from the stack slot.
52 unsigned Mips16InstrInfo::
53 isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const
54 {
55   return 0;
56 }
57
58 /// isStoreToStackSlot - If the specified machine instruction is a direct
59 /// store to a stack slot, return the virtual or physical register number of
60 /// the source reg along with the FrameIndex of the loaded stack slot.  If
61 /// not, return 0.  This predicate must return 0 if the instruction has
62 /// any side effects other than storing to the stack slot.
63 unsigned Mips16InstrInfo::
64 isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
65 {
66   return 0;
67 }
68
69 void Mips16InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
70                                   MachineBasicBlock::iterator I, DebugLoc DL,
71                                   unsigned DestReg, unsigned SrcReg,
72                                   bool KillSrc) const {
73   unsigned Opc = 0;
74
75   if (Mips::CPU16RegsRegClass.contains(DestReg) &&
76       Mips::GPR32RegClass.contains(SrcReg))
77     Opc = Mips::MoveR3216;
78   else if (Mips::GPR32RegClass.contains(DestReg) &&
79            Mips::CPU16RegsRegClass.contains(SrcReg))
80     Opc = Mips::Move32R16;
81   else if ((SrcReg == Mips::HI0) &&
82            (Mips::CPU16RegsRegClass.contains(DestReg)))
83     Opc = Mips::Mfhi16, SrcReg = 0;
84
85   else if ((SrcReg == Mips::LO0) &&
86            (Mips::CPU16RegsRegClass.contains(DestReg)))
87     Opc = Mips::Mflo16, SrcReg = 0;
88
89
90   assert(Opc && "Cannot copy registers");
91
92   MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc));
93
94   if (DestReg)
95     MIB.addReg(DestReg, RegState::Define);
96
97   if (SrcReg)
98     MIB.addReg(SrcReg, getKillRegState(KillSrc));
99 }
100
101 void Mips16InstrInfo::
102 storeRegToStack(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
103                 unsigned SrcReg, bool isKill, int FI,
104                 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI,
105                 int64_t Offset) const {
106   DebugLoc DL;
107   if (I != MBB.end()) DL = I->getDebugLoc();
108   MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOStore);
109   unsigned Opc = 0;
110   if (Mips::CPU16RegsRegClass.hasSubClassEq(RC))
111     Opc = Mips::SwRxSpImmX16;
112   assert(Opc && "Register class not handled!");
113   BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill)).
114       addFrameIndex(FI).addImm(Offset)
115       .addMemOperand(MMO);
116 }
117
118 void Mips16InstrInfo::
119 loadRegFromStack(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
120                  unsigned DestReg, int FI, const TargetRegisterClass *RC,
121                  const TargetRegisterInfo *TRI, int64_t Offset) const {
122   DebugLoc DL;
123   if (I != MBB.end()) DL = I->getDebugLoc();
124   MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOLoad);
125   unsigned Opc = 0;
126
127   if (Mips::CPU16RegsRegClass.hasSubClassEq(RC))
128     Opc = Mips::LwRxSpImmX16;
129   assert(Opc && "Register class not handled!");
130   BuildMI(MBB, I, DL, get(Opc), DestReg).addFrameIndex(FI).addImm(Offset)
131     .addMemOperand(MMO);
132 }
133
134 bool Mips16InstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
135   MachineBasicBlock &MBB = *MI->getParent();
136   switch(MI->getDesc().getOpcode()) {
137   default:
138     return false;
139   case Mips::RetRA16:
140     ExpandRetRA16(MBB, MI, Mips::JrcRa16);
141     break;
142   }
143
144   MBB.erase(MI);
145   return true;
146 }
147
148 /// GetOppositeBranchOpc - Return the inverse of the specified
149 /// opcode, e.g. turning BEQ to BNE.
150 unsigned Mips16InstrInfo::getOppositeBranchOpc(unsigned Opc) const {
151   switch (Opc) {
152   default:  llvm_unreachable("Illegal opcode!");
153   case Mips::BeqzRxImmX16: return Mips::BnezRxImmX16;
154   case Mips::BnezRxImmX16: return Mips::BeqzRxImmX16;
155   case Mips::BeqzRxImm16: return Mips::BnezRxImm16;
156   case Mips::BnezRxImm16: return Mips::BeqzRxImm16;
157   case Mips::BteqzT8CmpX16: return Mips::BtnezT8CmpX16;
158   case Mips::BteqzT8SltX16: return Mips::BtnezT8SltX16;
159   case Mips::BteqzT8SltiX16: return Mips::BtnezT8SltiX16;
160   case Mips::Btnez16: return Mips::Bteqz16;
161   case Mips::BtnezX16: return Mips::BteqzX16;
162   case Mips::BtnezT8CmpiX16: return Mips::BteqzT8CmpiX16;
163   case Mips::BtnezT8SltuX16: return Mips::BteqzT8SltuX16;
164   case Mips::BtnezT8SltiuX16: return Mips::BteqzT8SltiuX16;
165   case Mips::Bteqz16: return Mips::Btnez16;
166   case Mips::BteqzX16: return Mips::BtnezX16;
167   case Mips::BteqzT8CmpiX16: return Mips::BtnezT8CmpiX16;
168   case Mips::BteqzT8SltuX16: return Mips::BtnezT8SltuX16;
169   case Mips::BteqzT8SltiuX16: return Mips::BtnezT8SltiuX16;
170   case Mips::BtnezT8CmpX16: return Mips::BteqzT8CmpX16;
171   case Mips::BtnezT8SltX16: return Mips::BteqzT8SltX16;
172   case Mips::BtnezT8SltiX16: return Mips::BteqzT8SltiX16;
173   }
174   assert(false && "Implement this function.");
175   return 0;
176 }
177
178 // Adjust SP by FrameSize bytes. Save RA, S0, S1
179 void Mips16InstrInfo::makeFrame(unsigned SP, int64_t FrameSize,
180                     MachineBasicBlock &MBB,
181                     MachineBasicBlock::iterator I) const {
182   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
183   if (!NeverUseSaveRestore) {
184     if (isUInt<11>(FrameSize))
185       //BuildMI(MBB, I, DL, get(Mips::SaveRaF16)).addImm(FrameSize);
186       BuildMI(MBB, I, DL, get(Mips::SaveX16)).addReg(Mips::RA).
187               addReg(Mips::S0).
188               addReg(Mips::S1).addReg(Mips::S2).addImm(FrameSize);
189     else {
190       int Base = 2040; // should create template function like isUInt that
191                        // returns largest possible n bit unsigned integer
192       int64_t Remainder = FrameSize - Base;
193       BuildMI(MBB, I, DL, get(Mips::SaveX16)).addReg(Mips::RA).
194               addReg(Mips::S0).
195               addReg(Mips::S1).addReg(Mips::S2).addImm(Base);
196       if (isInt<16>(-Remainder))
197         BuildAddiuSpImm(MBB, I, -Remainder);
198       else
199         adjustStackPtrBig(SP, -Remainder, MBB, I, Mips::V0, Mips::V1);
200     }
201
202   }
203   else {
204     //
205     // sw ra, -4[sp]
206     // sw s1, -8[sp]
207     // sw s0, -12[sp]
208
209     MachineInstrBuilder MIB1 = BuildMI(MBB, I, DL, get(Mips::SwRxSpImmX16),
210                                        Mips::RA);
211     MIB1.addReg(Mips::SP);
212     MIB1.addImm(-4);
213     MachineInstrBuilder MIB2 = BuildMI(MBB, I, DL, get(Mips::SwRxSpImmX16),
214                                        Mips::S1);
215     MIB2.addReg(Mips::SP);
216     MIB2.addImm(-8);
217     MachineInstrBuilder MIB3 = BuildMI(MBB, I, DL, get(Mips::SwRxSpImmX16),
218                                        Mips::S0);
219     MIB3.addReg(Mips::SP);
220     MIB3.addImm(-12);
221     adjustStackPtrBig(SP, -FrameSize, MBB, I, Mips::V0, Mips::V1);
222   }
223 }
224
225 // Adjust SP by FrameSize bytes. Restore RA, S0, S1
226 void Mips16InstrInfo::restoreFrame(unsigned SP, int64_t FrameSize,
227                                    MachineBasicBlock &MBB,
228                                    MachineBasicBlock::iterator I) const {
229   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
230   if (!NeverUseSaveRestore) {
231     if (isUInt<11>(FrameSize))
232       BuildMI(MBB, I, DL, get(Mips::RestoreX16)).addReg(Mips::RA).
233               addReg(Mips::S0).
234               addReg(Mips::S1).addReg(Mips::S2).addImm(FrameSize);
235     else {
236       int Base = 2040; // should create template function like isUInt that
237                        // returns largest possible n bit unsigned integer
238       int64_t Remainder = FrameSize - Base;
239       if (isInt<16>(Remainder))
240         BuildAddiuSpImm(MBB, I, Remainder);
241       else
242         adjustStackPtrBig(SP, Remainder, MBB, I, Mips::A0, Mips::A1);
243       BuildMI(MBB, I, DL, get(Mips::RestoreX16)).addReg(Mips::RA).
244               addReg(Mips::S0).
245               addReg(Mips::S1).addReg(Mips::S2).addImm(Base);
246     }
247   }
248   else {
249     adjustStackPtrBig(SP, FrameSize, MBB, I, Mips::A0, Mips::A1);
250     // lw ra, -4[sp]
251     // lw s1, -8[sp]
252     // lw s0, -12[sp]
253     MachineInstrBuilder MIB1 = BuildMI(MBB, I, DL, get(Mips::LwRxSpImmX16),
254                                        Mips::A0);
255     MIB1.addReg(Mips::SP);
256     MIB1.addImm(-4);
257     MachineInstrBuilder MIB0 = BuildMI(MBB, I, DL, get(Mips::Move32R16),
258                                        Mips::RA);
259      MIB0.addReg(Mips::A0);
260     MachineInstrBuilder MIB2 = BuildMI(MBB, I, DL, get(Mips::LwRxSpImmX16),
261                                        Mips::S1);
262     MIB2.addReg(Mips::SP);
263     MIB2.addImm(-8);
264     MachineInstrBuilder MIB3 = BuildMI(MBB, I, DL, get(Mips::LwRxSpImmX16),
265                                        Mips::S0);
266     MIB3.addReg(Mips::SP);
267     MIB3.addImm(-12);
268   }
269
270 }
271
272 // Adjust SP by Amount bytes where bytes can be up to 32bit number.
273 // This can only be called at times that we know that there is at least one free
274 // register.
275 // This is clearly safe at prologue and epilogue.
276 //
277 void Mips16InstrInfo::adjustStackPtrBig(unsigned SP, int64_t Amount,
278                                         MachineBasicBlock &MBB,
279                                         MachineBasicBlock::iterator I,
280                                         unsigned Reg1, unsigned Reg2) const {
281   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
282 //  MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo();
283 //  unsigned Reg1 = RegInfo.createVirtualRegister(&Mips::CPU16RegsRegClass);
284 //  unsigned Reg2 = RegInfo.createVirtualRegister(&Mips::CPU16RegsRegClass);
285   //
286   // li reg1, constant
287   // move reg2, sp
288   // add reg1, reg1, reg2
289   // move sp, reg1
290   //
291   //
292   MachineInstrBuilder MIB1 = BuildMI(MBB, I, DL, get(Mips::LwConstant32), Reg1);
293   MIB1.addImm(Amount).addImm(-1);
294   MachineInstrBuilder MIB2 = BuildMI(MBB, I, DL, get(Mips::MoveR3216), Reg2);
295   MIB2.addReg(Mips::SP, RegState::Kill);
296   MachineInstrBuilder MIB3 = BuildMI(MBB, I, DL, get(Mips::AdduRxRyRz16), Reg1);
297   MIB3.addReg(Reg1);
298   MIB3.addReg(Reg2, RegState::Kill);
299   MachineInstrBuilder MIB4 = BuildMI(MBB, I, DL, get(Mips::Move32R16),
300                                                      Mips::SP);
301   MIB4.addReg(Reg1, RegState::Kill);
302 }
303
304 void Mips16InstrInfo::adjustStackPtrBigUnrestricted(unsigned SP, int64_t Amount,
305                     MachineBasicBlock &MBB,
306                     MachineBasicBlock::iterator I) const {
307    assert(false && "adjust stack pointer amount exceeded");
308 }
309
310 /// Adjust SP by Amount bytes.
311 void Mips16InstrInfo::adjustStackPtr(unsigned SP, int64_t Amount,
312                                      MachineBasicBlock &MBB,
313                                      MachineBasicBlock::iterator I) const {
314   if (isInt<16>(Amount))  // need to change to addiu sp, ....and isInt<16>
315     BuildAddiuSpImm(MBB, I, Amount);
316   else
317     adjustStackPtrBigUnrestricted(SP, Amount, MBB, I);
318 }
319
320 /// This function generates the sequence of instructions needed to get the
321 /// result of adding register REG and immediate IMM.
322 unsigned
323 Mips16InstrInfo::loadImmediate(unsigned FrameReg,
324                                int64_t Imm, MachineBasicBlock &MBB,
325                                MachineBasicBlock::iterator II, DebugLoc DL,
326                                unsigned &NewImm) const {
327   //
328   // given original instruction is:
329   // Instr rx, T[offset] where offset is too big.
330   //
331   // lo = offset & 0xFFFF
332   // hi = ((offset >> 16) + (lo >> 15)) & 0xFFFF;
333   //
334   // let T = temporary register
335   // li T, hi
336   // shl T, 16
337   // add T, Rx, T
338   //
339   RegScavenger rs;
340   int32_t lo = Imm & 0xFFFF;
341   NewImm = lo;
342   int Reg =0;
343   int SpReg = 0;
344
345   rs.enterBasicBlock(&MBB);
346   rs.forward(II);
347   //
348   // We need to know which registers can be used, in the case where there
349   // are not enough free registers. We exclude all registers that
350   // are used in the instruction that we are helping.
351   //  // Consider all allocatable registers in the register class initially
352   BitVector Candidates =
353       RI.getAllocatableSet
354       (*II->getParent()->getParent(), &Mips::CPU16RegsRegClass);
355   // Exclude all the registers being used by the instruction.
356   for (unsigned i = 0, e = II->getNumOperands(); i != e; ++i) {
357     MachineOperand &MO = II->getOperand(i);
358     if (MO.isReg() && MO.getReg() != 0 && !MO.isDef() &&
359         !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
360       Candidates.reset(MO.getReg());
361   }
362   //
363   // If the same register was used and defined in an instruction, then
364   // it will not be in the list of candidates.
365   //
366   // we need to analyze the instruction that we are helping.
367   // we need to know if it defines register x but register x is not
368   // present as an operand of the instruction. this tells
369   // whether the register is live before the instruction. if it's not
370   // then we don't need to save it in case there are no free registers.
371   //
372   int DefReg = 0;
373   for (unsigned i = 0, e = II->getNumOperands(); i != e; ++i) {
374     MachineOperand &MO = II->getOperand(i);
375     if (MO.isReg() && MO.isDef()) {
376       DefReg = MO.getReg();
377       break;
378     }
379   }
380   //
381   BitVector Available = rs.getRegsAvailable(&Mips::CPU16RegsRegClass);
382
383   Available &= Candidates;
384   //
385   // we use T0 for the first register, if we need to save something away.
386   // we use T1 for the second register, if we need to save something away.
387   //
388   unsigned FirstRegSaved =0, SecondRegSaved=0;
389   unsigned FirstRegSavedTo = 0, SecondRegSavedTo = 0;
390
391
392   Reg = Available.find_first();
393
394   if (Reg == -1) {
395     Reg = Candidates.find_first();
396     Candidates.reset(Reg);
397     if (DefReg != Reg) {
398       FirstRegSaved = Reg;
399       FirstRegSavedTo = Mips::T0;
400       copyPhysReg(MBB, II, DL, FirstRegSavedTo, FirstRegSaved, true);
401     }
402   }
403   else
404     Available.reset(Reg);
405   BuildMI(MBB, II, DL, get(Mips::LwConstant32), Reg).addImm(Imm).addImm(-1);
406   NewImm = 0;
407   if (FrameReg == Mips::SP) {
408     SpReg = Available.find_first();
409     if (SpReg == -1) {
410       SpReg = Candidates.find_first();
411       // Candidates.reset(SpReg); // not really needed
412       if (DefReg!= SpReg) {
413         SecondRegSaved = SpReg;
414         SecondRegSavedTo = Mips::T1;
415       }
416       if (SecondRegSaved)
417         copyPhysReg(MBB, II, DL, SecondRegSavedTo, SecondRegSaved, true);
418     }
419    else
420      Available.reset(SpReg);
421     copyPhysReg(MBB, II, DL, SpReg, Mips::SP, false);
422     BuildMI(MBB, II, DL, get(Mips::  AdduRxRyRz16), Reg).addReg(SpReg, RegState::Kill)
423       .addReg(Reg);
424   }
425   else
426     BuildMI(MBB, II, DL, get(Mips::  AdduRxRyRz16), Reg).addReg(FrameReg)
427       .addReg(Reg, RegState::Kill);
428   if (FirstRegSaved || SecondRegSaved) {
429     II = llvm::next(II);
430     if (FirstRegSaved)
431       copyPhysReg(MBB, II, DL, FirstRegSaved, FirstRegSavedTo, true);
432     if (SecondRegSaved)
433       copyPhysReg(MBB, II, DL, SecondRegSaved, SecondRegSavedTo, true);
434   }
435   return Reg;
436 }
437
438 unsigned Mips16InstrInfo::getAnalyzableBrOpc(unsigned Opc) const {
439   return (Opc == Mips::BeqzRxImmX16   || Opc == Mips::BimmX16  ||
440           Opc == Mips::Bimm16  ||
441           Opc == Mips::Bteqz16        || Opc == Mips::Btnez16 ||
442           Opc == Mips::BeqzRxImm16    || Opc == Mips::BnezRxImm16   ||
443           Opc == Mips::BnezRxImmX16   || Opc == Mips::BteqzX16 ||
444           Opc == Mips::BteqzT8CmpX16  || Opc == Mips::BteqzT8CmpiX16 ||
445           Opc == Mips::BteqzT8SltX16  || Opc == Mips::BteqzT8SltuX16  ||
446           Opc == Mips::BteqzT8SltiX16 || Opc == Mips::BteqzT8SltiuX16 ||
447           Opc == Mips::BtnezX16       || Opc == Mips::BtnezT8CmpX16 ||
448           Opc == Mips::BtnezT8CmpiX16 || Opc == Mips::BtnezT8SltX16 ||
449           Opc == Mips::BtnezT8SltuX16 || Opc == Mips::BtnezT8SltiX16 ||
450           Opc == Mips::BtnezT8SltiuX16 ) ? Opc : 0;
451 }
452
453 void Mips16InstrInfo::ExpandRetRA16(MachineBasicBlock &MBB,
454                                   MachineBasicBlock::iterator I,
455                                   unsigned Opc) const {
456   BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
457 }
458
459
460 const MCInstrDesc &Mips16InstrInfo::AddiuSpImm(int64_t Imm) const {
461   if (validSpImm8(Imm))
462     return get(Mips::AddiuSpImm16);
463   else
464     return get(Mips::AddiuSpImmX16);
465 }
466
467 void Mips16InstrInfo::BuildAddiuSpImm
468   (MachineBasicBlock &MBB, MachineBasicBlock::iterator I, int64_t Imm) const {
469   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
470   BuildMI(MBB, I, DL, AddiuSpImm(Imm)).addImm(Imm);
471 }
472
473 const MipsInstrInfo *llvm::createMips16InstrInfo(MipsTargetMachine &TM) {
474   return new Mips16InstrInfo(TM);
475 }
476
477 bool Mips16InstrInfo::validImmediate(unsigned Opcode, unsigned Reg,
478                                      int64_t Amount) {
479   switch (Opcode) {
480   case Mips::LbRxRyOffMemX16:
481   case Mips::LbuRxRyOffMemX16:
482   case Mips::LhRxRyOffMemX16:
483   case Mips::LhuRxRyOffMemX16:
484   case Mips::SbRxRyOffMemX16:
485   case Mips::ShRxRyOffMemX16:
486   case Mips::LwRxRyOffMemX16:
487   case Mips::SwRxRyOffMemX16:
488   case Mips::SwRxSpImmX16:
489   case Mips::LwRxSpImmX16:
490     return isInt<16>(Amount);
491   case Mips::AddiuRxRyOffMemX16:
492     if ((Reg == Mips::PC) || (Reg == Mips::SP))
493       return isInt<16>(Amount);
494     return isInt<15>(Amount);
495   }
496   llvm_unreachable("unexpected Opcode in validImmediate");
497 }
498
499 /// Measure the specified inline asm to determine an approximation of its
500 /// length.
501 /// Comments (which run till the next SeparatorString or newline) do not
502 /// count as an instruction.
503 /// Any other non-whitespace text is considered an instruction, with
504 /// multiple instructions separated by SeparatorString or newlines.
505 /// Variable-length instructions are not handled here; this function
506 /// may be overloaded in the target code to do that.
507 /// We implement the special case of the .space directive taking only an
508 /// integer argument, which is the size in bytes. This is used for creating
509 /// inline code spacing for testing purposes using inline assembly.
510 ///
511 unsigned Mips16InstrInfo::getInlineAsmLength(const char *Str,
512                                              const MCAsmInfo &MAI) const {
513
514
515   // Count the number of instructions in the asm.
516   bool atInsnStart = true;
517   unsigned Length = 0;
518   for (; *Str; ++Str) {
519     if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
520                                 strlen(MAI.getSeparatorString())) == 0)
521       atInsnStart = true;
522     if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
523       if (strncmp(Str, ".space", 6)==0) {
524         char *EStr; int Sz;
525         Sz = strtol(Str+6, &EStr, 10);
526         while (isspace(*EStr)) ++EStr;
527         if (*EStr=='\0') {
528           DEBUG(dbgs() << "parsed .space " << Sz << '\n');
529           return Sz;
530         }
531       }
532       Length += MAI.getMaxInstLength();
533       atInsnStart = false;
534     }
535     if (atInsnStart && strncmp(Str, MAI.getCommentString(),
536                                strlen(MAI.getCommentString())) == 0)
537       atInsnStart = false;
538   }
539
540   return Length;
541 }