Skip to content

Commit c80661a

Browse files
feat: added functionality to convert XML description of intrinsics to
intrinsic-test's X86IntrinsicType struct
1 parent c009f9f commit c80661a

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ impl<T> Argument<T>
2020
where
2121
T: IntrinsicTypeDefinition,
2222
{
23+
pub fn new(pos: usize, name: String, ty: T, constraint: Option<Constraint>) -> Self {
24+
Argument {
25+
pos,
26+
name,
27+
ty,
28+
constraint,
29+
}
30+
}
31+
2332
pub fn to_c_type(&self) -> String {
2433
self.ty.c_type()
2534
}

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

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use crate::common::{
2-
argument::Argument, intrinsic::Intrinsic, intrinsic_helpers::IntrinsicTypeDefinition,
3-
};
1+
use crate::common::argument::{Argument, ArgumentList};
2+
use crate::common::intrinsic::Intrinsic;
3+
use crate::common::intrinsic_helpers::{IntrinsicType, IntrinsicTypeDefinition};
4+
45
use serde::{Deserialize, Deserializer};
56
use std::path::Path;
67

@@ -19,6 +20,15 @@ where
1920
}
2021
}
2122

23+
// Custom deserializer function to convert strings to u16
24+
fn string_to_u16<'de, D>(deserializer: D) -> Result<u16, D::Error>
25+
where
26+
D: Deserializer<'de>,
27+
{
28+
let s = String::deserialize(deserializer)?;
29+
return Ok(s.as_str().parse::<u16>().unwrap_or(0u16));
30+
}
31+
2232
#[derive(Deserialize)]
2333
struct Data {
2434
#[serde(rename = "intrinsic", default)]
@@ -49,6 +59,10 @@ struct Parameter {
4959
type_data: String,
5060
#[serde(rename = "@etype", default)]
5161
etype: String,
62+
#[serde(rename = "@memwidth", default, deserialize_with = "string_to_u16")]
63+
memwidth: u16,
64+
#[serde(rename = "@varname", default)]
65+
var_name: String,
5266
}
5367

5468
#[derive(Deserialize)]
@@ -84,21 +98,39 @@ pub fn get_xml_intrinsics(
8498
Ok(parsed_intrinsics)
8599
}
86100

87-
pub fn xml_to_intrinsic(
88-
mut intr: XMLIntrinsic,
101+
fn xml_to_intrinsic(
102+
intr: XMLIntrinsic,
89103
target: &String,
90104
) -> Result<Intrinsic<X86IntrinsicType>, Box<dyn std::error::Error>> {
91105
let name = intr.name;
92106
let results = X86IntrinsicType::from_c(&intr.return_data.type_data, target)?;
93107

94-
let arguments: Vec<_> = intr
108+
let args: Vec<_> = intr
95109
.parameters
96110
.into_iter()
97111
.enumerate()
98-
.map(|(i, arg)| {
99-
// let arg_name = Argument::<X86IntrinsicType>::type_and_name_from_c(&arg).1;
112+
.map(|(i, param)| {
113+
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+
117+
let mut arg = Argument::<X86IntrinsicType>::new(i, param.var_name, ty, constraint);
118+
let IntrinsicType {
119+
ref mut constant, ..
120+
} = arg.ty.0;
121+
if param.etype == "IMM" {
122+
*constant = true
123+
}
124+
arg
100125
})
101126
.collect();
102127

103-
todo!("xml_to_intrinsic needs to collect the arguments properly!");
128+
let arguments = ArgumentList::<X86IntrinsicType> { args };
129+
130+
Ok(Intrinsic {
131+
name,
132+
arguments,
133+
results: results,
134+
arch_tags: intr.cpuid,
135+
})
104136
}

0 commit comments

Comments
 (0)