1/*
2 * Copyright (c) 2015-2017, Intel Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * * Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Intel Corporation nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30
31#include "cross_compile.h"
32#include "src/ue2common.h"
33#include "src/hs_compile.h"
34#include "src/util/make_unique.h"
35
36#include <sstream>
37#include <string>
38
39using namespace std;
40
41struct XcompileMode {
42 const string name;
43 unsigned long long cpu_features;
44};
45
46static const XcompileMode xcompile_options[] = {
47 { "avx512", HS_CPU_FEATURES_AVX512 },
48 { "avx2", HS_CPU_FEATURES_AVX2 },
49 { "base", 0 },
50};
51
52unique_ptr<hs_platform_info> xcompileReadMode(const char *s) {
53 hs_platform_info rv;
54 UNUSED hs_error_t err;
55 err = hs_populate_platform(&rv);
56 assert(!err);
57
58 string str(s);
59 string mode = str.substr(0, str.find(":"));
60 string opt = str.substr(str.find(":")+1, str.npos);
61 bool found_mode = false;
62
63 if (!opt.empty()) {
64 for (const auto &xcompile : xcompile_options) {
65 if (opt == xcompile.name) {
66 rv.cpu_features = xcompile.cpu_features;
67 found_mode = true;
68 break;
69 }
70 }
71 }
72
73 if (!found_mode) {
74 return nullptr;
75 } else {
76 DEBUG_PRINTF("cpu_features %llx\n", rv.cpu_features);
77 return ue2::make_unique<hs_platform_info>(rv);
78 }
79}
80
81string to_string(const hs_platform_info &p) {
82 ostringstream out;
83 if (p.tune) {
84 out << p.tune;
85 }
86
87 if (p.cpu_features) {
88 u64a features = p.cpu_features;
89 if (features & HS_CPU_FEATURES_AVX512) {
90 out << " avx512";
91 features &= ~HS_CPU_FEATURES_AVX512;
92 }
93
94 if (features & HS_CPU_FEATURES_AVX2) {
95 out << " avx2";
96 features &= ~HS_CPU_FEATURES_AVX2;
97 }
98
99 if (features) {
100 out << " " << "?cpu_features?:" << features;
101 }
102 }
103
104 return out.str();
105}
106
107string xcompileUsage(void) {
108 string variants = "Instruction set options: ";
109 const auto commaspace = ", ";
110 auto sep = "";
111 for (const auto &xcompile : xcompile_options) {
112 variants += sep + xcompile.name;
113 sep = commaspace;
114 }
115 return variants;
116}
117