TargetCompilerConfigs.cpp revision 41d8dcca655fc21072b3d898b3ea5bfd6b90e96d
1/*
2 * Copyright 2012, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "bcc/Support/TargetCompilerConfigs.h"
18
19// Get ARM version number (i.e., __ARM_ARCH__)
20#ifdef __arm__
21#include <machine/cpu-features.h>
22#endif
23
24using namespace bcc;
25
26//===----------------------------------------------------------------------===//
27// ARM
28//===----------------------------------------------------------------------===//
29#if defined(PROVIDE_ARM_CODEGEN)
30
31bool ARMBaseCompilerConfig::HasThumb2() {
32#if !defined(TARGET_BUILD)
33  // Cross-compiler can always generate Thumb-2 instructions.
34  return true;
35#else // defined(TARGET_BUILD)
36#  if (__ARM_ARCH__ >= 7) || defined(__ARM_ARCH_6T2__)
37  return true;
38#  else
39  // ARM prior to V6T2 doesn't support Thumb-2.
40  return false;
41#  endif
42#endif
43}
44
45void
46ARMBaseCompilerConfig::GetFeatureVector(std::vector<std::string> &pAttributes,
47                                        bool pInThumbMode, bool pEnableNEON) {
48#if defined(ARCH_ARM_HAVE_VFP)
49  pAttributes.push_back("+vfp3");
50#  if !defined(ARCH_ARM_HAVE_VFP_D32)
51  pAttributes.push_back("+d16");
52#  endif
53#endif
54
55  if (pInThumbMode) {
56    if (HasThumb2()) {
57      pAttributes.push_back("+thumb2");
58    } else {
59      pAttributes.push_back("-thumb2");
60    }
61  }
62
63#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_ARCH_ARM_HAVE_NEON)
64  if (pEnableNEON) {
65    pAttributes.push_back("+neon");
66    pAttributes.push_back("+neonfp");
67  } else {
68    pAttributes.push_back("-neon");
69    pAttributes.push_back("-neonfp");
70  }
71#else
72  pAttributes.push_back("-neon");
73  pAttributes.push_back("-neonfp");
74#endif
75
76  return;
77}
78
79ARMBaseCompilerConfig::ARMBaseCompilerConfig(const std::string &pTriple,
80                                             bool pInThumbMode)
81  : CompilerConfig(pTriple), mInThumbMode(pInThumbMode) {
82
83  // Enable NEON by default.
84  mEnableNEON = true;
85
86  std::vector<std::string> attributes;
87  GetFeatureVector(attributes, mInThumbMode, mEnableNEON);
88  setFeatureString(attributes);
89
90  return;
91}
92
93bool ARMBaseCompilerConfig::enableNEON(bool pEnable) {
94#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_ARCH_ARM_HAVE_NEON)
95  if (mEnableNEON != pEnable) {
96    std::vector<std::string> attributes;
97    GetFeatureVector(attributes, mInThumbMode, pEnable);
98    setFeatureString(attributes);
99    mEnableNEON = pEnable;
100    return true;
101  }
102  // Fall-through
103#endif
104  return false;
105}
106#endif // defined(PROVIDE_ARM_CODEGEN)
107