Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1 | //===-- TargetAttributesSema.cpp - Encapsulate target attributes-*- 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 semantic analysis implementation for target-specific |
| 11 | // attributes. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "Sema.h" |
| 16 | #include "TargetAttributesSema.h" |
| 17 | #include "clang/Basic/TargetInfo.h" |
| 18 | #include "llvm/ADT/Triple.h" |
| 19 | |
| 20 | using namespace clang; |
| 21 | |
| 22 | TargetAttributesSema::~TargetAttributesSema() {} |
| 23 | bool TargetAttributesSema::ProcessDeclAttribute(Scope *scope, Decl *D, |
| 24 | const AttributeList &Attr, Sema &S) const { |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | static void HandleMSP430InterruptAttr(Decl *d, |
| 29 | const AttributeList &Attr, Sema &S) { |
| 30 | // Check the attribute arguments. |
| 31 | if (Attr.getNumArgs() != 1) { |
| 32 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | // FIXME: Check for decl - it should be void ()(void). |
| 37 | |
| 38 | Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 39 | llvm::APSInt NumParams(32); |
| 40 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
| 41 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 42 | << "interrupt" << NumParamsExpr->getSourceRange(); |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | unsigned Num = NumParams.getLimitedValue(255); |
| 47 | if ((Num & 1) || Num > 30) { |
| 48 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 49 | << "interrupt" << (int)NumParams.getSExtValue() |
| 50 | << NumParamsExpr->getSourceRange(); |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | d->addAttr(::new (S.Context) MSP430InterruptAttr(Num)); |
| 55 | d->addAttr(::new (S.Context) UsedAttr()); |
| 56 | } |
| 57 | |
| 58 | namespace { |
| 59 | class MSP430AttributesSema : public TargetAttributesSema { |
| 60 | public: |
| 61 | MSP430AttributesSema() { } |
| 62 | bool ProcessDeclAttribute(Scope *scope, Decl *D, |
| 63 | const AttributeList &Attr, Sema &S) const { |
| 64 | if (Attr.getName()->getName() == "interrupt") { |
| 65 | HandleMSP430InterruptAttr(D, Attr, S); |
| 66 | return true; |
| 67 | } |
| 68 | return false; |
| 69 | } |
| 70 | }; |
| 71 | } |
| 72 | |
Charles Davis | 5a0164d | 2010-02-10 23:06:52 +0000 | [diff] [blame] | 73 | static void HandleX86ForceAlignArgPointerAttr(Decl *D, |
| 74 | const AttributeList& Attr, |
| 75 | Sema &S) { |
| 76 | // Check the attribute arguments. |
| 77 | if (Attr.getNumArgs() != 0) { |
| 78 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | // If we try to apply it to a function pointer, don't warn, but don't |
| 83 | // do anything, either. It doesn't matter anyway, because there's nothing |
| 84 | // special about calling a force_align_arg_pointer function. |
| 85 | ValueDecl* VD = dyn_cast<ValueDecl>(D); |
Charles Davis | 9c00be5 | 2010-02-10 23:26:12 +0000 | [diff] [blame^] | 86 | if (VD && VD->getType()->isFunctionPointerType()) |
Charles Davis | 5a0164d | 2010-02-10 23:06:52 +0000 | [diff] [blame] | 87 | return; |
| 88 | // Attribute can only be applied to function types. |
Charles Davis | 9c00be5 | 2010-02-10 23:26:12 +0000 | [diff] [blame^] | 89 | if (!isa<FunctionDecl>(D)) { |
Charles Davis | 5a0164d | 2010-02-10 23:06:52 +0000 | [diff] [blame] | 90 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 91 | << Attr.getName() << /* function */0; |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | D->addAttr(::new (S.Context) X86ForceAlignArgPointerAttr()); |
| 96 | } |
| 97 | |
| 98 | namespace { |
| 99 | class X86AttributesSema : public TargetAttributesSema { |
| 100 | public: |
| 101 | X86AttributesSema() { } |
| 102 | bool ProcessDeclAttribute(Scope *scope, Decl *D, |
| 103 | const AttributeList &Attr, Sema &S) const { |
| 104 | if (Attr.getName()->getName() == "force_align_arg_pointer") { |
| 105 | HandleX86ForceAlignArgPointerAttr(D, Attr, S); |
| 106 | return true; |
| 107 | } |
| 108 | return false; |
| 109 | } |
| 110 | }; |
| 111 | } |
| 112 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 113 | const TargetAttributesSema &Sema::getTargetAttributesSema() const { |
| 114 | if (TheTargetAttributesSema) |
| 115 | return *TheTargetAttributesSema; |
| 116 | |
| 117 | const llvm::Triple &Triple(Context.Target.getTriple()); |
| 118 | switch (Triple.getArch()) { |
| 119 | default: |
| 120 | return *(TheTargetAttributesSema = new TargetAttributesSema); |
| 121 | |
| 122 | case llvm::Triple::msp430: |
| 123 | return *(TheTargetAttributesSema = new MSP430AttributesSema); |
Charles Davis | 5a0164d | 2010-02-10 23:06:52 +0000 | [diff] [blame] | 124 | case llvm::Triple::x86: |
| 125 | return *(TheTargetAttributesSema = new X86AttributesSema); |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |