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(bool isAArch64)45 void Triple::Init(bool isAArch64)
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 (isAArch64) {
50 arch = Triple::ArchType::aarch64;
51 environment = Triple::EnvironmentType::GNU;
52 } else {
53 arch = Triple::ArchType::x64;
54 environment = Triple::EnvironmentType::GNU;
55 }
56 }
57
Init(const std::string & target)58 void Triple::Init(const std::string &target)
59 {
60 data = target;
61
62 /* Currently Triple is used only to configure aarch64: be/le, ILP32/LP64.
63 * Other architectures (TARGX86_64, TARGX86, TARGARM32, TARGVM) are configured with compiler build config */
64 #if TARGAARCH64
65 Init(true);
66
67 std::vector<std::string_view> components;
68 maple::StringUtils::SplitSV(data, components, '-');
69 if (components.size() == 0) { // as minimum 1 component must be
70 return;
71 }
72
73 auto tmpArch = ParseArch(components[0]); // to not overwrite arch seting by opts::bigendian
74 if (tmpArch == Triple::UnknownArch) {
75 return;
76 }
77 arch = tmpArch;
78
79 /* Try to check environment in option.
80 * As example, it can be: aarch64-none-linux-gnu or aarch64-linux-gnu or aarch64-gnu, where gnu is environment */
81 for (uint32_t i = 1; i < components.size(); ++i) {
82 auto tmpEnvironment = ParseEnvironment(components[i]);
83 if (tmpEnvironment != Triple::UnknownEnvironment) {
84 environment = tmpEnvironment;
85 break;
86 }
87 }
88 #endif
89 }
90
GetArchName() const91 std::string Triple::GetArchName() const
92 {
93 switch (arch) {
94 case ArchType::aarch64_be:
95 return "aarch64_be";
96 case ArchType::aarch64:
97 return "aarch64";
98 default:
99 DEBUG_ASSERT(false, "Unknown Architecture Type\n");
100 }
101 return "";
102 }
103
GetEnvironmentName() const104 std::string Triple::GetEnvironmentName() const
105 {
106 switch (environment) {
107 case EnvironmentType::GNUILP32:
108 return "gnu_ilp32";
109 case EnvironmentType::GNU:
110 return "gnu";
111 default:
112 DEBUG_ASSERT(false, "Unknown Environment Type\n");
113 }
114 return "";
115 }
116
Str() const117 std::string Triple::Str() const
118 {
119 if (!data.empty()) {
120 return data;
121 }
122
123 if (GetArch() != ArchType::UnknownArch && GetEnvironment() != Triple::EnvironmentType::UnknownEnvironment) {
124 /* only linux platform is supported, so "-linux-" is hardcoded */
125 return GetArchName() + "-linux-" + GetEnvironmentName();
126 }
127
128 CHECK_FATAL(false, "Only aarch64/aarch64_be GNU/GNUILP32 targets are supported\n");
129 return data;
130 }
131
132 } // namespace maple
133