Skip to content

Commit ae4cb22

Browse files
feat: added X86IntrinsicType parsing from string.
1 parent f918b57 commit ae4cb22

File tree

5 files changed

+116
-15
lines changed

5 files changed

+116
-15
lines changed

crates/intrinsic-test/src/common/intrinsic_helpers.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ impl FromStr for TypeKind {
2929
match s {
3030
"bfloat" => Ok(Self::BFloat),
3131
"float" => Ok(Self::Float),
32-
"int" => Ok(Self::Int(true)),
32+
"double" => Ok(Self::Double),
33+
"int" | "long" => Ok(Self::Int(true)),
34+
"short" => Ok(Self::Short(true)),
3335
"poly" => Ok(Self::Poly),
3436
"char" => Ok(Self::Char(true)),
3537
"uint" | "unsigned" => Ok(Self::Int(false)),
@@ -69,6 +71,7 @@ impl TypeKind {
6971
Self::Int(true) => "int",
7072
Self::Int(false) => "uint",
7173
Self::Poly => "poly",
74+
Self::Char(true) => "char",
7275
_ => unreachable!("Not used: {:#?}", self),
7376
}
7477
}
@@ -141,6 +144,18 @@ impl IntrinsicType {
141144
pub fn is_ptr(&self) -> bool {
142145
self.ptr
143146
}
147+
148+
pub fn set_bit_len(&mut self, value: Option<u32>) {
149+
self.bit_len = value;
150+
}
151+
152+
pub fn set_simd_len(&mut self, value: Option<u32>) {
153+
self.simd_len = value;
154+
}
155+
156+
pub fn set_vec_len(&mut self, value: Option<u32>) {
157+
self.vec_len = value;
158+
}
144159

145160
pub fn c_scalar_type(&self) -> String {
146161
format!(
@@ -195,7 +210,7 @@ impl IntrinsicType {
195210
match self {
196211
IntrinsicType {
197212
bit_len: Some(bit_len @ (8 | 16 | 32 | 64)),
198-
kind: kind @ (TypeKind::Int(_) | TypeKind::Poly),
213+
kind: kind @ (TypeKind::Int(_) | TypeKind::Poly | TypeKind::Char(_)),
199214
simd_len,
200215
vec_len,
201216
..

crates/intrinsic-test/src/x86/intrinsic.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::common::argument::ArgumentList;
22
use crate::common::indentation::Indentation;
33
use crate::common::intrinsic::{Intrinsic, IntrinsicDefinition};
44
use crate::common::intrinsic_helpers::IntrinsicType;
5-
use std::ops::Deref;
5+
use std::ops::{Deref, DerefMut};
66

77
#[derive(Debug, Clone, PartialEq)]
88
pub struct X86IntrinsicType(pub IntrinsicType);
@@ -15,6 +15,13 @@ impl Deref for X86IntrinsicType {
1515
}
1616
}
1717

18+
impl DerefMut for X86IntrinsicType {
19+
// type Target = IntrinsicType;
20+
fn deref_mut(&mut self) -> &mut Self::Target {
21+
&mut self.0
22+
}
23+
}
24+
1825
impl IntrinsicDefinition<X86IntrinsicType> for Intrinsic<X86IntrinsicType> {
1926
fn arguments(&self) -> ArgumentList<X86IntrinsicType> {
2027
self.arguments.clone()

crates/intrinsic-test/src/x86/mod.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
mod intrinsic;
22
mod types;
33
mod xml_parser;
4+
mod config;
45

56
use crate::common::SupportedArchitectureTest;
67
use crate::common::cli::ProcessedCli;
7-
use crate::common::intrinsic::Intrinsic;
8+
use crate::common::intrinsic::{Intrinsic, IntrinsicDefinition};
9+
use crate::common::write_file::{write_c_testfiles, write_rust_testfiles};
810
use intrinsic::X86IntrinsicType;
911
use xml_parser::get_xml_intrinsics;
12+
use config::build_notices;
1013

1114
pub struct X86ArchitectureTest {
1215
intrinsics: Vec<Intrinsic<X86IntrinsicType>>,
@@ -25,7 +28,25 @@ impl SupportedArchitectureTest for X86ArchitectureTest {
2528
}
2629

2730
fn build_c_file(&self) -> bool {
28-
todo!("build_c_file in X86ArchitectureTest is not implemented")
31+
let compiler = self.cli_options.cpp_compiler.as_deref();
32+
let target = &self.cli_options.target;
33+
let cxx_toolchain_dir = self.cli_options.cxx_toolchain_dir.as_deref();
34+
let c_target = "x86_64";
35+
36+
let intrinsics_name_list = write_c_testfiles(
37+
&self
38+
.intrinsics
39+
.iter()
40+
.map(|i| i as &dyn IntrinsicDefinition<_>)
41+
.collect::<Vec<_>>(),
42+
target,
43+
c_target,
44+
&["immintrin.h"],
45+
&build_notices("// "),
46+
&[],
47+
);
48+
49+
true
2950
}
3051

3152
fn build_rust_file(&self) -> bool {

crates/intrinsic-test/src/x86/types.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
use std::str::FromStr;
2+
13
use super::intrinsic::X86IntrinsicType;
24
use crate::common::cli::Language;
3-
use crate::common::intrinsic_helpers::IntrinsicTypeDefinition;
5+
use crate::common::intrinsic_helpers::{IntrinsicType, IntrinsicTypeDefinition, TypeKind};
46

57
impl IntrinsicTypeDefinition for X86IntrinsicType {
68
/// Gets a string containing the typename for this type in C format.
@@ -27,6 +29,50 @@ impl IntrinsicTypeDefinition for X86IntrinsicType {
2729
}
2830

2931
fn from_c(s: &str, target: &String) -> Result<Self, String> {
30-
todo!("from_c for X86IntrinsicType needs to be implemented!");
32+
let mut s_copy = s.to_string();
33+
s_copy = s_copy
34+
.replace("*", "")
35+
.replace("constexpr", "")
36+
.replace("const", "")
37+
.replace("literal", "");
38+
39+
let s_split = s_copy.split(" ")
40+
.filter_map(|s|if s.len() == 0 {None} else {Some(s)})
41+
.last();
42+
43+
// TODO: add more intrinsics by modifying
44+
// functionality below this line.
45+
// Currently all the intrinsics that have an "_"
46+
// is ignored.
47+
if let Some(_) = s.matches("_").next() {
48+
return Err(String::from("This functionality needs to be implemented"));
49+
};
50+
51+
// TODO: make the unwrapping safe
52+
let kind = TypeKind::from_str(s_split.unwrap()).expect("Unable to parse type!");
53+
let mut ptr_constant = false;
54+
let mut constant = false;
55+
let mut ptr = false;
56+
57+
if let Some(_) = s.matches("const*").next() {
58+
ptr_constant = true;
59+
};
60+
if let Some(_) = s.matches("const").next() {
61+
constant = true;
62+
};
63+
if let Some(_) = s.matches("*").next() {
64+
ptr = true;
65+
};
66+
67+
Ok(X86IntrinsicType(IntrinsicType {
68+
ptr,
69+
ptr_constant,
70+
constant,
71+
kind,
72+
bit_len: None,
73+
simd_len: None,
74+
vec_len: None,
75+
target: target.to_string(),
76+
}))
3177
}
3278
}

crates/intrinsic-test/src/x86/xml_parser.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ pub fn get_xml_intrinsics(
9191
.intrinsics
9292
.into_iter()
9393
.filter_map(|intr| {
94-
Some(xml_to_intrinsic(intr, target).expect("Couldn't parse XML properly!"))
94+
// Some(xml_to_intrinsic(intr, target).expect("Couldn't parse XML properly!"))
95+
xml_to_intrinsic(intr, target).ok()
9596
})
9697
.collect();
9798

@@ -105,26 +106,37 @@ fn xml_to_intrinsic(
105106
let name = intr.name;
106107
let results = X86IntrinsicType::from_c(&intr.return_data.type_data, target)?;
107108

108-
let args: Vec<_> = intr
109+
let args_check = intr
109110
.parameters
110111
.into_iter()
111112
.enumerate()
112113
.map(|(i, param)| {
113114
let constraint = None;
114-
let ty = X86IntrinsicType::from_c(param.type_data.as_str(), target)
115-
.unwrap_or_else(|_| panic!("Failed to parse argument '{i}'"));
116-
115+
let ty = X86IntrinsicType::from_c(param.type_data.as_str(), target);
116+
117+
if let Err(_) = ty {
118+
return None;
119+
}
120+
let mut ty_bit_len = param.etype.clone();
121+
ty_bit_len.retain(|c| c.is_numeric());
122+
let ty_bit_len = str::parse::<u32>(ty_bit_len.as_str()).ok();
123+
let mut ty = ty.unwrap();
124+
ty.set_bit_len(ty_bit_len);
117125
let mut arg = Argument::<X86IntrinsicType>::new(i, param.var_name, ty, constraint);
118126
let IntrinsicType {
119127
ref mut constant, ..
120128
} = arg.ty.0;
121129
if param.etype == "IMM" {
122130
*constant = true
123131
}
124-
arg
125-
})
126-
.collect();
132+
Some(arg)
133+
});
127134

135+
let args = args_check.collect::<Vec<_>>();
136+
if args.iter().any(|elem| elem.is_none()) {
137+
return Err(Box::from("intrinsic isn't fully supported in this test!"));
138+
}
139+
let args = args.into_iter().map(|e|e.unwrap()).collect::<Vec<_>>();
128140
let arguments = ArgumentList::<X86IntrinsicType> { args };
129141

130142
Ok(Intrinsic {

0 commit comments

Comments
 (0)