[meta] Remove OperandKind::name field and explicitly pass rust_field_name/rust_type; (fixes #1177)
This commit is contained in:
@@ -151,12 +151,12 @@ pub(crate) enum Literal {
|
||||
/// corresponding to a Rust enum type. An `Enumerator` object is an AST leaf node representing one
|
||||
/// of the values.
|
||||
Enumerator {
|
||||
rust_type: String,
|
||||
rust_type: &'static str,
|
||||
value: &'static str,
|
||||
},
|
||||
|
||||
/// A bitwise value of an immediate operand, used for bitwise exact floating point constants.
|
||||
Bits { rust_type: String, value: u64 },
|
||||
Bits { rust_type: &'static str, value: u64 },
|
||||
|
||||
/// A value of an integer immediate operand.
|
||||
Int(i64),
|
||||
@@ -171,13 +171,13 @@ impl Literal {
|
||||
OperandKindFields::ImmEnum(values) => values.get(value).unwrap_or_else(|| {
|
||||
panic!(
|
||||
"nonexistent value '{}' in enumeration '{}'",
|
||||
value, kind.name
|
||||
value, kind.rust_type
|
||||
)
|
||||
}),
|
||||
_ => panic!("enumerator is for enum values"),
|
||||
};
|
||||
Literal::Enumerator {
|
||||
rust_type: kind.rust_type.clone(),
|
||||
rust_type: kind.rust_type,
|
||||
value,
|
||||
}
|
||||
}
|
||||
@@ -188,7 +188,7 @@ impl Literal {
|
||||
_ => panic!("bits_of is for immediate scalar types"),
|
||||
}
|
||||
Literal::Bits {
|
||||
rust_type: kind.rust_type.clone(),
|
||||
rust_type: kind.rust_type,
|
||||
value: bits,
|
||||
}
|
||||
}
|
||||
@@ -475,12 +475,12 @@ impl Apply {
|
||||
"Nonexistent enum value '{}' passed to field of kind '{}' -- \
|
||||
did you use the right enum?",
|
||||
value,
|
||||
op.kind.name
|
||||
op.kind.rust_type
|
||||
);
|
||||
} else {
|
||||
panic!(
|
||||
"Passed non-enum field value {:?} to field of kind {}",
|
||||
literal, op.kind.name
|
||||
literal, op.kind.rust_type
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -488,14 +488,14 @@ impl Apply {
|
||||
Literal::Enumerator { value, .. } => panic!(
|
||||
"Expected immediate value in immediate field of kind '{}', \
|
||||
obtained enum value '{}'",
|
||||
op.kind.name, value
|
||||
op.kind.rust_type, value
|
||||
),
|
||||
Literal::Bits { .. } | Literal::Int(_) | Literal::EmptyVarArgs => {}
|
||||
},
|
||||
_ => {
|
||||
panic!(
|
||||
"Literal passed to non-literal field of kind {}",
|
||||
op.kind.name
|
||||
op.kind.rust_type
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,10 +93,7 @@ impl EncodingBuilder {
|
||||
{
|
||||
let immediate_predicate = InstructionPredicate::new_is_field_equal(
|
||||
&inst.inst.format,
|
||||
immediate_operand
|
||||
.kind
|
||||
.rust_field_name()
|
||||
.expect("Immediates must always have a field name."),
|
||||
immediate_operand.kind.rust_field_name,
|
||||
immediate_value.to_string(),
|
||||
);
|
||||
inst_predicate = if let Some(type_predicate) = inst_predicate {
|
||||
|
||||
@@ -49,7 +49,7 @@ pub(crate) struct InstructionFormat {
|
||||
pub(crate) struct FormatStructure {
|
||||
pub num_value_operands: usize,
|
||||
pub has_value_list: bool,
|
||||
pub imm_field_names: Vec<&'static str>,
|
||||
pub imm_field_names: Vec<(&'static str, &'static str)>,
|
||||
}
|
||||
|
||||
impl fmt::Display for InstructionFormat {
|
||||
@@ -57,7 +57,7 @@ impl fmt::Display for InstructionFormat {
|
||||
let imm_args = self
|
||||
.imm_fields
|
||||
.iter()
|
||||
.map(|field| format!("{}: {}", field.member, field.kind.name))
|
||||
.map(|field| format!("{}: {}", field.member, field.kind.rust_type))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
fmt.write_fmt(format_args!(
|
||||
@@ -89,7 +89,7 @@ impl InstructionFormat {
|
||||
imm_field_names: self
|
||||
.imm_fields
|
||||
.iter()
|
||||
.map(|field| field.kind.name)
|
||||
.map(|field| (field.kind.rust_field_name, field.kind.rust_type))
|
||||
.collect::<Vec<_>>(),
|
||||
}
|
||||
}
|
||||
@@ -127,7 +127,7 @@ impl InstructionFormatBuilder {
|
||||
pub fn imm(mut self, operand_kind: &OperandKind) -> Self {
|
||||
let field = FormatField {
|
||||
kind: operand_kind.clone(),
|
||||
member: operand_kind.rust_field_name().unwrap(),
|
||||
member: operand_kind.rust_field_name,
|
||||
};
|
||||
self.imm_fields.push(field);
|
||||
self
|
||||
|
||||
@@ -572,10 +572,14 @@ fn verify_format(inst_name: &str, operands_in: &[Operand], format: &InstructionF
|
||||
if operand.is_immediate_or_entityref() {
|
||||
if let Some(format_field) = format.imm_fields.get(num_immediates) {
|
||||
assert_eq!(
|
||||
format_field.kind.name, operand.kind.name,
|
||||
format_field.kind.rust_field_name,
|
||||
operand.kind.rust_field_name,
|
||||
"{}th operand of {} should be {} (according to format), not {} (according to \
|
||||
inst definition). You may need to use a different format.",
|
||||
num_immediates, inst_name, format_field.kind.name, operand.kind.name
|
||||
num_immediates,
|
||||
inst_name,
|
||||
format_field.kind.rust_field_name,
|
||||
operand.kind.rust_field_name
|
||||
);
|
||||
num_immediates += 1;
|
||||
}
|
||||
@@ -1299,7 +1303,8 @@ mod test {
|
||||
fn field_to_operand(index: usize, field: OperandKindFields) -> Operand {
|
||||
// Pretend the index string is &'static.
|
||||
let name = Box::leak(index.to_string().into_boxed_str());
|
||||
let kind = OperandKindBuilder::new(name, field).build();
|
||||
// Format's name / rust_type don't matter here.
|
||||
let kind = OperandKindBuilder::new(name, name, field).build();
|
||||
let operand = Operand::new(name, kind);
|
||||
operand
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::cdsl::camel_case;
|
||||
use crate::cdsl::typevar::TypeVar;
|
||||
|
||||
/// An instruction operand can be an *immediate*, an *SSA value*, or an *entity reference*. The
|
||||
@@ -18,9 +17,13 @@ use crate::cdsl::typevar::TypeVar;
|
||||
/// function, typically something declared in the function preamble.
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct Operand {
|
||||
/// Name of the operand variable, as it appears in function parameters, legalizations, etc.
|
||||
pub name: &'static str,
|
||||
doc: Option<&'static str>,
|
||||
|
||||
/// Type of the operand.
|
||||
pub kind: OperandKind,
|
||||
|
||||
doc: Option<&'static str>,
|
||||
}
|
||||
|
||||
impl Operand {
|
||||
@@ -110,56 +113,19 @@ pub(crate) enum OperandKindFields {
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct OperandKind {
|
||||
pub name: &'static str,
|
||||
doc: Option<&'static str>,
|
||||
rust_field_name: Option<&'static str>,
|
||||
/// The camel-cased name of an operand kind is also the Rust type used to represent it.
|
||||
pub rust_type: String,
|
||||
/// String representation of the Rust type mapping to this OperandKind.
|
||||
pub rust_type: &'static str,
|
||||
|
||||
/// Name of this OperandKind in the format's member field.
|
||||
pub rust_field_name: &'static str,
|
||||
|
||||
/// Type-specific fields for this OperandKind.
|
||||
pub fields: OperandKindFields,
|
||||
|
||||
doc: Option<&'static str>,
|
||||
}
|
||||
|
||||
impl OperandKind {
|
||||
fn new(
|
||||
name: &'static str,
|
||||
doc: Option<&'static str>,
|
||||
rust_field_name: Option<&'static str>,
|
||||
rust_type: Option<&'static str>,
|
||||
fields: OperandKindFields,
|
||||
) -> Self {
|
||||
// Compute the default rust_type value, if it wasn't provided.
|
||||
let rust_type = match rust_type {
|
||||
Some(rust_type) => rust_type.to_string(),
|
||||
None => match &fields {
|
||||
OperandKindFields::ImmEnum(_) | OperandKindFields::ImmValue => {
|
||||
format!("ir::immediates::{}", camel_case(name))
|
||||
}
|
||||
OperandKindFields::VariableArgs => "&[Value]".to_string(),
|
||||
OperandKindFields::TypeVar(_) | OperandKindFields::EntityRef => {
|
||||
format!("ir::{}", camel_case(name))
|
||||
}
|
||||
},
|
||||
};
|
||||
Self {
|
||||
name,
|
||||
doc,
|
||||
rust_field_name,
|
||||
rust_type,
|
||||
fields,
|
||||
}
|
||||
}
|
||||
|
||||
/// Name of this OperandKind in the format's member field.
|
||||
pub fn rust_field_name(&self) -> Option<&'static str> {
|
||||
if let Some(member) = &self.rust_field_name {
|
||||
return Some(member);
|
||||
}
|
||||
match &self.fields {
|
||||
OperandKindFields::ImmEnum(_) | OperandKindFields::ImmValue => Some("imm"),
|
||||
OperandKindFields::TypeVar(_) | OperandKindFields::EntityRef => Some(self.name),
|
||||
OperandKindFields::VariableArgs => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn doc(&self) -> Option<&str> {
|
||||
if let Some(doc) = &self.doc {
|
||||
return Some(doc);
|
||||
@@ -176,7 +142,12 @@ impl OperandKind {
|
||||
|
||||
impl Into<OperandKind> for &TypeVar {
|
||||
fn into(self) -> OperandKind {
|
||||
OperandKindBuilder::new("value", OperandKindFields::TypeVar(self.into())).build()
|
||||
OperandKindBuilder::new(
|
||||
"value",
|
||||
"ir::Value",
|
||||
OperandKindFields::TypeVar(self.into()),
|
||||
)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
impl Into<OperandKind> for &OperandKind {
|
||||
@@ -186,63 +157,56 @@ impl Into<OperandKind> for &OperandKind {
|
||||
}
|
||||
|
||||
pub(crate) struct OperandKindBuilder {
|
||||
name: &'static str,
|
||||
doc: Option<&'static str>,
|
||||
rust_field_name: Option<&'static str>,
|
||||
rust_type: Option<&'static str>,
|
||||
rust_field_name: &'static str,
|
||||
rust_type: &'static str,
|
||||
fields: OperandKindFields,
|
||||
doc: Option<&'static str>,
|
||||
}
|
||||
|
||||
impl OperandKindBuilder {
|
||||
pub fn new(name: &'static str, fields: OperandKindFields) -> Self {
|
||||
pub fn new(
|
||||
rust_field_name: &'static str,
|
||||
rust_type: &'static str,
|
||||
fields: OperandKindFields,
|
||||
) -> Self {
|
||||
Self {
|
||||
name,
|
||||
doc: None,
|
||||
rust_field_name: None,
|
||||
rust_type: None,
|
||||
rust_field_name,
|
||||
rust_type,
|
||||
fields,
|
||||
doc: None,
|
||||
}
|
||||
}
|
||||
pub fn new_imm(name: &'static str) -> Self {
|
||||
pub fn new_imm(rust_field_name: &'static str, rust_type: &'static str) -> Self {
|
||||
Self {
|
||||
name,
|
||||
doc: None,
|
||||
rust_field_name: None,
|
||||
rust_type: None,
|
||||
rust_field_name,
|
||||
rust_type,
|
||||
fields: OperandKindFields::ImmValue,
|
||||
}
|
||||
}
|
||||
pub fn new_enum(name: &'static str, values: EnumValues) -> Self {
|
||||
Self {
|
||||
name,
|
||||
doc: None,
|
||||
rust_field_name: None,
|
||||
rust_type: None,
|
||||
fields: OperandKindFields::ImmEnum(values),
|
||||
}
|
||||
}
|
||||
pub fn doc(mut self, doc: &'static str) -> Self {
|
||||
pub fn new_enum(
|
||||
rust_field_name: &'static str,
|
||||
rust_type: &'static str,
|
||||
values: EnumValues,
|
||||
) -> Self {
|
||||
Self {
|
||||
rust_field_name,
|
||||
rust_type,
|
||||
fields: OperandKindFields::ImmEnum(values),
|
||||
doc: None,
|
||||
}
|
||||
}
|
||||
pub fn with_doc(mut self, doc: &'static str) -> Self {
|
||||
assert!(self.doc.is_none());
|
||||
self.doc = Some(doc);
|
||||
self
|
||||
}
|
||||
pub fn rust_field_name(mut self, rust_field_name: &'static str) -> Self {
|
||||
assert!(self.rust_field_name.is_none());
|
||||
self.rust_field_name = Some(rust_field_name);
|
||||
self
|
||||
}
|
||||
pub fn rust_type(mut self, rust_type: &'static str) -> Self {
|
||||
assert!(self.rust_type.is_none());
|
||||
self.rust_type = Some(rust_type);
|
||||
self
|
||||
}
|
||||
pub fn build(self) -> OperandKind {
|
||||
OperandKind::new(
|
||||
self.name,
|
||||
self.doc,
|
||||
self.rust_field_name,
|
||||
self.rust_type,
|
||||
self.fields,
|
||||
)
|
||||
OperandKind {
|
||||
rust_type: self.rust_type,
|
||||
fields: self.fields,
|
||||
rust_field_name: self.rust_field_name,
|
||||
doc: self.doc,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,7 +214,7 @@ fn rewrite_expr(
|
||||
.inst()
|
||||
.operands_in
|
||||
.iter()
|
||||
.map(|operand| format!("{}: {}", operand.name, operand.kind.name))
|
||||
.map(|operand| format!("{}: {}", operand.name, operand.kind.rust_type))
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user