aboutsummaryrefslogtreecommitdiff
path: root/lib/VMCore/InlineAsm.cpp
blob: 3ccd8aca7ebf993759f9bbf8868b5ce9a0ff7aac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//===-- InlineAsm.cpp - Implement the InlineAsm class ---------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file was developed by Chris Lattner and is distributed under the
// University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the InlineAsm class.
//
//===----------------------------------------------------------------------===//

#include "llvm/InlineAsm.h"
#include "llvm/DerivedTypes.h"
#include <cctype>
using namespace llvm;

// NOTE: when memoizing the function type, we have to be careful to handle the
// case when the type gets refined.

InlineAsm *InlineAsm::get(const FunctionType *Ty, const std::string &AsmString,
                          const std::string &Constraints, bool hasSideEffects) {
  // FIXME: memoize!
  return new InlineAsm(Ty, AsmString, Constraints, hasSideEffects);  
}

InlineAsm::InlineAsm(const FunctionType *Ty, const std::string &asmString,
                     const std::string &constraints, bool hasSideEffects)
  : Value(PointerType::get(Ty), Value::InlineAsmVal), AsmString(asmString), 
    Constraints(constraints), HasSideEffects(hasSideEffects) {

  // Do various checks on the constraint string and type.
  assert(Verify(Ty, constraints) && "Function type not legal for constraints!");
}

const FunctionType *InlineAsm::getFunctionType() const {
  return cast<FunctionType>(getType()->getElementType());
}

/// Verify - Verify that the specified constraint string is reasonable for the
/// specified function type, and otherwise validate the constraint string.
bool InlineAsm::Verify(const FunctionType *Ty, const std::string &Constraints) {
  if (Ty->isVarArg()) return false;
  
  unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
  
  // Scan the constraints string.
  for (std::string::const_iterator I = Constraints.begin(), 
         E = Constraints.end(); I != E; ) {
    if (*I == ',') return false;  // Empty constraint like ",,"
    
    // Parse the prefix.
    enum {
      isInput,            // 'x'
      isOutput,           // '=x'
      isIndirectOutput,   // '==x'
      isClobber,          // '~x'
    } ConstraintType = isInput;
    
    if (*I == '~') {
      ConstraintType = isClobber;
      ++I;
    } else if (*I == '=') {
      ++I;
      if (I != E && *I == '=') {
        ConstraintType = isIndirectOutput;
        ++I;
      } else {
        ConstraintType = isOutput;
      }
    }
    
    if (I == E) return false;   // Just a prefix, like "==" or "~".
    
    switch (ConstraintType) {
    case isOutput:
      if (NumInputs || NumClobbers) return false;  // outputs come first.
      ++NumOutputs;
      break;
    case isInput:
    case isIndirectOutput:
      if (NumClobbers) return false;               // inputs before clobbers.
      ++NumInputs;
      break;
    case isClobber:
      ++NumClobbers;
      break;
    }
    
    // Parse the id.  We accept [a-zA-Z0-9] currently.
    while (I != E && isalnum(*I)) ++I;
    
    // If we reached the end of the ID, we must have the end of the string or a
    // comma, which we skip now.
    if (I != E) {
      if (*I != ',') return false;
      ++I;
      if (I == E) return false;    // don't allow "xyz,"
    }
  }
  
  if (NumOutputs > 1) return false;  // Only one result allowed.
  
  if ((Ty->getReturnType() != Type::VoidTy) != NumOutputs)
    return false;   // NumOutputs = 1 iff has a result type.
  
  if (Ty->getNumParams() != NumInputs) return false;
  return true;
}