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 "driver_options.h"
17 #include "triple.h"
18 #include "gtest/gtest.h"
19 
20 using namespace maple;
21 
22 /* Check Triple initialization with target string */
TEST(utTriple, triple1)23 TEST(utTriple, triple1)
24 {
25     Triple::GetTriple().Init("aarch64");
26     ASSERT_EQ(Triple::GetTriple().GetEnvironment(), Triple::EnvironmentType::GNU);
27     ASSERT_EQ(Triple::GetTriple().GetArch(), Triple::ArchType::aarch64);
28     ASSERT_EQ(Triple::GetTriple().IsBigEndian(), false);
29 
30     Triple::GetTriple().Init("aarch64_le");
31     ASSERT_EQ(Triple::GetTriple().GetEnvironment(), Triple::EnvironmentType::GNU);
32     ASSERT_EQ(Triple::GetTriple().GetArch(), Triple::ArchType::aarch64);
33     ASSERT_EQ(Triple::GetTriple().IsBigEndian(), false);
34 
35     Triple::GetTriple().Init("aarch64_be");
36     ASSERT_EQ(Triple::GetTriple().GetEnvironment(), Triple::EnvironmentType::GNU);
37     ASSERT_EQ(Triple::GetTriple().GetArch(), Triple::ArchType::aarch64_be);
38     ASSERT_EQ(Triple::GetTriple().IsBigEndian(), true);
39 
40     Triple::GetTriple().Init("aarch64_be-ilp32");
41     ASSERT_EQ(Triple::GetTriple().GetEnvironment(), Triple::EnvironmentType::GNUILP32);
42     ASSERT_EQ(Triple::GetTriple().GetArch(), Triple::ArchType::aarch64_be);
43     ASSERT_EQ(Triple::GetTriple().IsBigEndian(), true);
44 
45     Triple::GetTriple().Init("aarch64_be-gnuilp32");
46     ASSERT_EQ(Triple::GetTriple().GetEnvironment(), Triple::EnvironmentType::GNUILP32);
47     ASSERT_EQ(Triple::GetTriple().GetArch(), Triple::ArchType::aarch64_be);
48     ASSERT_EQ(Triple::GetTriple().IsBigEndian(), true);
49 
50     Triple::GetTriple().Init("aarch64_be-linux-gnuilp32");
51     ASSERT_EQ(Triple::GetTriple().GetEnvironment(), Triple::EnvironmentType::GNUILP32);
52     ASSERT_EQ(Triple::GetTriple().GetArch(), Triple::ArchType::aarch64_be);
53     ASSERT_EQ(Triple::GetTriple().IsBigEndian(), true);
54 
55     Triple::GetTriple().Init();
56     ASSERT_EQ(Triple::GetTriple().GetEnvironment(), Triple::EnvironmentType::GNU);
57     ASSERT_EQ(Triple::GetTriple().GetArch(), Triple::ArchType::aarch64);
58     ASSERT_EQ(Triple::GetTriple().IsBigEndian(), false);
59 
60     /* Incorrect value leads to default initialization */
61     Triple::GetTriple().Init("incorect-triple");
62     ASSERT_EQ(Triple::GetTriple().GetEnvironment(), Triple::EnvironmentType::GNU);
63     ASSERT_EQ(Triple::GetTriple().GetArch(), Triple::ArchType::aarch64);
64     ASSERT_EQ(Triple::GetTriple().IsBigEndian(), false);
65 }
66 
67 /* Check Triple initialization with options */
TEST(utTriple, triple2)68 TEST(utTriple, triple2)
69 {
70     const char *argv[] = {"maple",  // program name
71                           "-be", "--ilp32", nullptr};
72     int argc = (sizeof(argv) / sizeof(argv[0])) - 1;
73 
74     cl::CommandLine::GetCommandLine().Clear();
75     auto err = cl::CommandLine::GetCommandLine().Parse(argc, (char **)argv);
76     ASSERT_EQ(err, cl::RetCode::noError);
77 
78     /* Triple checks -be and --ilp32 options inside to set the target */
79     Triple::GetTriple().Init();
80     ASSERT_EQ(Triple::GetTriple().GetEnvironment(), Triple::EnvironmentType::GNUILP32);
81     ASSERT_EQ(Triple::GetTriple().GetArch(), Triple::ArchType::aarch64_be);
82     ASSERT_EQ(Triple::GetTriple().IsBigEndian(), true);
83 
84     const char *argv2[] = {"maple",  // program name
85                            nullptr};
86     argc = (sizeof(argv) / sizeof(argv[0])) - 1;
87 
88     cl::CommandLine::GetCommandLine().Clear();
89     err = cl::CommandLine::GetCommandLine().Parse(argc, (char **)argv2);
90     ASSERT_EQ(err, cl::RetCode::noError);
91 
92     Triple::GetTriple().Init();
93     ASSERT_EQ(Triple::GetTriple().GetEnvironment(), Triple::EnvironmentType::GNU);
94     ASSERT_EQ(Triple::GetTriple().GetArch(), Triple::ArchType::aarch64);
95     ASSERT_EQ(Triple::GetTriple().IsBigEndian(), false);
96 
97     // create command line
98     const char *argv3[] = {"maple",  // program name
99                            "--target=aarch64_be-gnuilp32", nullptr};
100     argc = (sizeof(argv) / sizeof(argv[0])) - 1;
101 
102     cl::CommandLine::GetCommandLine().Clear();
103     err = cl::CommandLine::GetCommandLine().Parse(argc, (char **)argv3);
104     ASSERT_EQ(err, cl::RetCode::noError);
105 
106     Triple::GetTriple().Init(opts::target);
107     ASSERT_EQ(Triple::GetTriple().GetEnvironment(), Triple::EnvironmentType::GNUILP32);
108     ASSERT_EQ(Triple::GetTriple().GetArch(), Triple::ArchType::aarch64_be);
109     ASSERT_EQ(Triple::GetTriple().IsBigEndian(), true);
110 }
111 
112 /* Check the priority of Triple initialization: -be/--ilp32 vs --target */
TEST(utTriple, triple3)113 TEST(utTriple, triple3)
114 {
115     // create command line
116     const char *argv[] = {"maple",  // program name
117                           "-be", "--ilp32", "--target=aarch64-gnu", nullptr};
118     int argc = (sizeof(argv) / sizeof(argv[0])) - 1;
119 
120     cl::CommandLine::GetCommandLine().Clear();
121     auto err = cl::CommandLine::GetCommandLine().Parse(argc, (char **)argv);
122     ASSERT_EQ(err, cl::RetCode::noError);
123 
124     /* --target option has hicher priority than --ilp32/-be options */
125     Triple::GetTriple().Init(opts::target);
126     ASSERT_EQ(Triple::GetTriple().GetEnvironment(), Triple::EnvironmentType::GNU);
127     ASSERT_EQ(Triple::GetTriple().GetArch(), Triple::ArchType::aarch64);
128     ASSERT_EQ(Triple::GetTriple().IsBigEndian(), false);
129 }
130