TargetCompilerConfigs.cpp revision a1d5d7980254d70dd28f4244a2b73120847b5f15
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 defined(ARCH_ARM_HAVE_THUMB_SUPPORT)
37#    if (__ARM_ARCH__ >= 7) || defined(__ARM_ARCH_6T2__)
38  return true;
39#    else
40  // ARM prior to V6T2 doesn't support Thumb-2.
41  return false;
42#    endif
43#  else // !defined(ARCH_ARM_HAVE_THUMB_SUPPORT)
44  // Target that doesn't support Thumb feature won't support Thumb-2, either.
45  return false;
46#  endif
47#endif
48}
49
50void
51ARMBaseCompilerConfig::GetFeatureVector(std::vector<std::string> &pAttributes,
52                                        bool pInThumbMode, bool pEnableNEON) {
53#if defined(ARCH_ARM_HAVE_VFP)
54  pAttributes.push_back("+vfp3");
55#  if !defined(ARCH_ARM_HAVE_VFP_D32)
56  pAttributes.push_back("+d16");
57#  endif
58#endif
59
60  if (pInThumbMode) {
61    if (HasThumb2()) {
62      pAttributes.push_back("+thumb2");
63    } else {
64      pAttributes.push_back("-thumb2");
65    }
66  }
67
68#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_ARCH_ARM_HAVE_NEON)
69  if (pEnableNEON) {
70    pAttributes.push_back("+neon");
71    pAttributes.push_back("+neonfp");
72  } else {
73    pAttributes.push_back("-neon");
74    pAttributes.push_back("-neonfp");
75  }
76#else
77  pAttributes.push_back("-neon");
78  pAttributes.push_back("-neonfp");
79#endif
80
81  return;
82}
83
84ARMBaseCompilerConfig::ARMBaseCompilerConfig(const std::string &pTriple,
85                                             bool pInThumbMode)
86  : CompilerConfig(pTriple), mInThumbMode(pInThumbMode) {
87
88  // Enable NEON by default.
89  mEnableNEON = true;
90
91  std::vector<std::string> attributes;
92  GetFeatureVector(attributes, mInThumbMode, mEnableNEON);
93  setFeatureString(attributes);
94
95  return;
96}
97
98bool ARMBaseCompilerConfig::enableNEON(bool pEnable) {
99#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_ARCH_ARM_HAVE_NEON)
100  if (mEnableNEON != pEnable) {
101    std::vector<std::string> attributes;
102    GetFeatureVector(attributes, mInThumbMode, pEnable);
103    setFeatureString(attributes);
104    mEnableNEON = pEnable;
105    return true;
106  }
107  // Fall-through
108#endif
109  return false;
110}
111#endif // defined(PROVIDE_ARM_CODEGEN)
112