TargetCompilerConfigs.cpp revision 229c99b4290e30047678a79910722c628fb2602e
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)
64  if (pEnableNEON) {
65    pAttributes.push_back("+neon");
66  } else {
67    pAttributes.push_back("-neon");
68    pAttributes.push_back("-neonfp");
69  }
70#else
71  pAttributes.push_back("-neon");
72  pAttributes.push_back("-neonfp");
73#endif
74
75  return;
76}
77
78ARMBaseCompilerConfig::ARMBaseCompilerConfig(const std::string &pTriple,
79                                             bool pInThumbMode)
80  : CompilerConfig(pTriple), mInThumbMode(pInThumbMode) {
81
82  // Enable NEON by default.
83  mEnableNEON = true;
84
85  std::vector<std::string> attributes;
86  GetFeatureVector(attributes, mInThumbMode, mEnableNEON);
87  setFeatureString(attributes);
88
89  return;
90}
91
92bool ARMBaseCompilerConfig::enableNEON(bool pEnable) {
93#if defined(ARCH_ARM_HAVE_NEON)
94  if (mEnableNEON != pEnable) {
95    std::vector<std::string> attributes;
96    GetFeatureVector(attributes, mInThumbMode, pEnable);
97    setFeatureString(attributes);
98    mEnableNEON = pEnable;
99    return true;
100  }
101  // Fall-through
102#endif
103  return false;
104}
105#endif // defined(PROVIDE_ARM_CODEGEN)
106