blob: e9a81e4ed88adddd0c84ca6539980e482ea5742b [file] [log] [blame]
Chris Lattnereef2fe72006-01-24 04:13:11 +00001//===-- InlineAsm.cpp - Implement the InlineAsm class ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the InlineAsm class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/InlineAsm.h"
15#include "llvm/DerivedTypes.h"
Chris Lattner8547e3a2006-01-26 00:48:33 +000016#include <cctype>
Chris Lattnereef2fe72006-01-24 04:13:11 +000017using namespace llvm;
18
Chris Lattnera2d810d2006-01-25 22:26:05 +000019// NOTE: when memoizing the function type, we have to be careful to handle the
20// case when the type gets refined.
21
22InlineAsm *InlineAsm::get(const FunctionType *Ty, const std::string &AsmString,
23 const std::string &Constraints, bool hasSideEffects) {
24 // FIXME: memoize!
25 return new InlineAsm(Ty, AsmString, Constraints, hasSideEffects);
26}
27
Chris Lattnereef2fe72006-01-24 04:13:11 +000028InlineAsm::InlineAsm(const FunctionType *Ty, const std::string &asmString,
Chris Lattner8bbcda22006-01-25 18:57:27 +000029 const std::string &constraints, bool hasSideEffects)
30 : Value(PointerType::get(Ty), Value::InlineAsmVal), AsmString(asmString),
31 Constraints(constraints), HasSideEffects(hasSideEffects) {
Chris Lattnereef2fe72006-01-24 04:13:11 +000032
Chris Lattnera2d810d2006-01-25 22:26:05 +000033 // Do various checks on the constraint string and type.
34 assert(Verify(Ty, constraints) && "Function type not legal for constraints!");
Chris Lattnereef2fe72006-01-24 04:13:11 +000035}
36
37const FunctionType *InlineAsm::getFunctionType() const {
38 return cast<FunctionType>(getType()->getElementType());
39}
Chris Lattnera2d810d2006-01-25 22:26:05 +000040
Chris Lattnerc981b8e2006-01-26 02:21:59 +000041std::vector<std::pair<InlineAsm::ConstraintPrefix, std::string> >
42InlineAsm::ParseConstraints(const std::string &Constraints) {
43 std::vector<std::pair<InlineAsm::ConstraintPrefix, std::string> > Result;
Chris Lattner8547e3a2006-01-26 00:48:33 +000044
45 // Scan the constraints string.
46 for (std::string::const_iterator I = Constraints.begin(),
Chris Lattnerc981b8e2006-01-26 02:21:59 +000047 E = Constraints.end(); I != E; ) {
48 if (*I == ',') { Result.clear(); break; } // Empty constraint like ",,"
Chris Lattner8547e3a2006-01-26 00:48:33 +000049
50 // Parse the prefix.
Chris Lattnerc981b8e2006-01-26 02:21:59 +000051 ConstraintPrefix ConstraintType = isInput;
Chris Lattner8547e3a2006-01-26 00:48:33 +000052
53 if (*I == '~') {
54 ConstraintType = isClobber;
55 ++I;
56 } else if (*I == '=') {
57 ++I;
58 if (I != E && *I == '=') {
59 ConstraintType = isIndirectOutput;
60 ++I;
61 } else {
62 ConstraintType = isOutput;
63 }
64 }
65
Chris Lattnerc981b8e2006-01-26 02:21:59 +000066 if (I == E) { Result.clear(); break; } // Just a prefix, like "==" or "~".
Chris Lattner8547e3a2006-01-26 00:48:33 +000067
Chris Lattnerc981b8e2006-01-26 02:21:59 +000068 std::string::const_iterator IdStart = I;
69
70 // Parse the id. We accept [a-zA-Z0-9] currently.
71 while (I != E && isalnum(*I)) ++I;
72
73 if (IdStart == I) { // Requires more than just a prefix
74 Result.clear();
75 break;
76 }
77
78 // Remember this constraint.
79 Result.push_back(std::make_pair(ConstraintType, std::string(IdStart, I)));
80
81 // If we reached the end of the ID, we must have the end of the string or a
82 // comma, which we skip now.
83 if (I != E) {
84 if (*I != ',') { Result.clear(); break; }
85 ++I;
86 if (I == E) { Result.clear(); break; } // don't allow "xyz,"
87 }
88 }
89
90 return Result;
91}
92
93
94/// Verify - Verify that the specified constraint string is reasonable for the
95/// specified function type, and otherwise validate the constraint string.
96bool InlineAsm::Verify(const FunctionType *Ty, const std::string &ConstStr) {
97 if (Ty->isVarArg()) return false;
98
99 std::vector<std::pair<ConstraintPrefix, std::string> >
100 Constraints = ParseConstraints(ConstStr);
101
102 // Error parsing constraints.
103 if (Constraints.empty() && !ConstStr.empty()) return false;
104
105 unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
106
107 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
108 switch (Constraints[i].first) {
Chris Lattner8547e3a2006-01-26 00:48:33 +0000109 case isOutput:
110 if (NumInputs || NumClobbers) return false; // outputs come first.
111 ++NumOutputs;
112 break;
113 case isInput:
114 case isIndirectOutput:
115 if (NumClobbers) return false; // inputs before clobbers.
116 ++NumInputs;
117 break;
118 case isClobber:
119 ++NumClobbers;
120 break;
121 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000122 }
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000123
124 if (NumOutputs > 1) return false; // Only one result allowed so far.
Chris Lattner8547e3a2006-01-26 00:48:33 +0000125
126 if ((Ty->getReturnType() != Type::VoidTy) != NumOutputs)
127 return false; // NumOutputs = 1 iff has a result type.
128
129 if (Ty->getNumParams() != NumInputs) return false;
Chris Lattnera2d810d2006-01-25 22:26:05 +0000130 return true;
131}