• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "triple.h"
17 #include "driver_options.h"
18 
19 namespace maple {
20 
ParseArch(std::string_view archStr)21 Triple::ArchType Triple::ParseArch(std::string_view archStr)
22 {
23     if (maple::utils::Contains({"aarch64", "aarch64_le"}, archStr)) {
24         return Triple::ArchType::aarch64;
25     } else if (maple::utils::Contains({"aarch64_be"}, archStr)) {
26         return Triple::ArchType::aarch64_be;
27     }
28 
29     // Currently Triple support only aarch64
30     return Triple::UnknownArch;
31 }
32 
ParseEnvironment(std::string_view archStr)33 Triple::EnvironmentType Triple::ParseEnvironment(std::string_view archStr)
34 {
35     if (maple::utils::Contains({"ilp32", "gnu_ilp32", "gnuilp32"}, archStr)) {
36         return Triple::EnvironmentType::GNUILP32;
37     } else if (maple::utils::Contains({"gnu"}, archStr)) {
38         return Triple::EnvironmentType::GNU;
39     }
40 
41     // Currently Triple support only ilp32 and default gnu/LP64 ABI
42     return Triple::UnknownEnvironment;
43 }
44 
Init()45 void Triple::Init()
46 {
47     /* Currently Triple is used only to configure aarch64: be/le, ILP32/LP64
48      * Other architectures (TARGX86_64, TARGX86, TARGARM32, TARGVM) are configured with compiler build config */
49 #if TARGAARCH64
50     arch = Triple::ArchType::aarch64;
51     environment = Triple::EnvironmentType::GNU;
52 #endif
53 }
54 
Init(const std::string & target)55 void Triple::Init(const std::string &target)
56 {
57     data = target;
58 
59     /* Currently Triple is used only to configure aarch64: be/le, ILP32/LP64.
60      * Other architectures (TARGX86_64, TARGX86, TARGARM32, TARGVM) are configured with compiler build config */
61 #if TARGAARCH64
62     Init();
63 
64     std::vector<std::string_view> components;
65     maple::StringUtils::SplitSV(data, components, '-');
66     if (components.size() == 0) {  // as minimum 1 component must be
67         return;
68     }
69 
70     auto tmpArch = ParseArch(components[0]);  // to not overwrite arch seting by opts::bigendian
71     if (tmpArch == Triple::UnknownArch) {
72         return;
73     }
74     arch = tmpArch;
75 
76     /* Try to check environment in option.
77      * As example, it can be: aarch64-none-linux-gnu or aarch64-linux-gnu or aarch64-gnu, where gnu is environment */
78     for (uint32_t i = 1; i < components.size(); ++i) {
79         auto tmpEnvironment = ParseEnvironment(components[i]);
80         if (tmpEnvironment != Triple::UnknownEnvironment) {
81             environment = tmpEnvironment;
82             break;
83         }
84     }
85 #endif
86 }
87 
GetArchName() const88 std::string Triple::GetArchName() const
89 {
90     switch (arch) {
91         case ArchType::aarch64_be:
92             return "aarch64_be";
93         case ArchType::aarch64:
94             return "aarch64";
95         default:
96             DEBUG_ASSERT(false, "Unknown Architecture Type\n");
97     }
98     return "";
99 }
100 
GetEnvironmentName() const101 std::string Triple::GetEnvironmentName() const
102 {
103     switch (environment) {
104         case EnvironmentType::GNUILP32:
105             return "gnu_ilp32";
106         case EnvironmentType::GNU:
107             return "gnu";
108         default:
109             DEBUG_ASSERT(false, "Unknown Environment Type\n");
110     }
111     return "";
112 }
113 
Str() const114 std::string Triple::Str() const
115 {
116     if (!data.empty()) {
117         return data;
118     }
119 
120     if (GetArch() != ArchType::UnknownArch && GetEnvironment() != Triple::EnvironmentType::UnknownEnvironment) {
121         /* only linux platform is supported, so "-linux-" is hardcoded */
122         return GetArchName() + "-linux-" + GetEnvironmentName();
123     }
124 
125     CHECK_FATAL(false, "Only aarch64/aarch64_be GNU/GNUILP32 targets are supported\n");
126     return data;
127 }
128 
129 }  // namespace maple
130