[meta] Introduce the InstructionGroupBuilder;

This follows the rest of the code base data structures, where we have a
mutable data structure builder; once the data structure is constructed,
it's immutable.

This also makes the definition of instructions easier, and it paves the
way for defining immediate variants.
This commit is contained in:
Benjamin Bouvier
2019-04-30 18:33:01 +02:00
parent feb90e376a
commit d9277f249b
7 changed files with 251 additions and 382 deletions

View File

@@ -12,6 +12,40 @@ use std::ops;
use std::rc::Rc; use std::rc::Rc;
use std::slice; use std::slice;
pub struct InstructionGroupBuilder<'format_reg> {
_name: &'static str,
_doc: &'static str,
format_registry: &'format_reg FormatRegistry,
instructions: Vec<Instruction>,
}
impl<'format_reg> InstructionGroupBuilder<'format_reg> {
pub fn new(
name: &'static str,
doc: &'static str,
format_registry: &'format_reg FormatRegistry,
) -> Self {
Self {
_name: name,
_doc: doc,
format_registry,
instructions: Vec::new(),
}
}
pub fn push(&mut self, builder: InstructionBuilder) {
self.instructions.push(builder.finish(self.format_registry));
}
pub fn finish(self) -> InstructionGroup {
InstructionGroup {
_name: self._name,
_doc: self._doc,
instructions: self.instructions,
}
}
}
/// Every instruction must belong to exactly one instruction group. A given /// Every instruction must belong to exactly one instruction group. A given
/// target architecture can support instructions from multiple groups, and it /// target architecture can support instructions from multiple groups, and it
/// does not necessarily support all instructions in a group. /// does not necessarily support all instructions in a group.
@@ -22,18 +56,6 @@ pub struct InstructionGroup {
} }
impl InstructionGroup { impl InstructionGroup {
pub fn new(name: &'static str, doc: &'static str) -> Self {
Self {
_name: name,
_doc: doc,
instructions: Vec::new(),
}
}
pub fn push(&mut self, inst: Instruction) {
self.instructions.push(inst);
}
pub fn iter(&self) -> slice::Iter<Instruction> { pub fn iter(&self) -> slice::Iter<Instruction> {
self.instructions.iter() self.instructions.iter()
} }
@@ -278,7 +300,7 @@ impl InstructionBuilder {
self self
} }
pub fn finish(self, format_registry: &FormatRegistry) -> Instruction { fn finish(self, format_registry: &FormatRegistry) -> Instruction {
let operands_in = self.operands_in.unwrap_or_else(Vec::new); let operands_in = self.operands_in.unwrap_or_else(Vec::new);
let operands_out = self.operands_out.unwrap_or_else(Vec::new); let operands_out = self.operands_out.unwrap_or_else(Vec::new);

View File

@@ -404,11 +404,14 @@ impl TransformGroups {
#[should_panic] #[should_panic]
fn test_double_custom_legalization() { fn test_double_custom_legalization() {
use crate::cdsl::formats::{FormatRegistry, InstructionFormatBuilder}; use crate::cdsl::formats::{FormatRegistry, InstructionFormatBuilder};
use crate::cdsl::instructions::InstructionBuilder; use crate::cdsl::instructions::{InstructionBuilder, InstructionGroupBuilder};
let mut format = FormatRegistry::new(); let mut format = FormatRegistry::new();
format.insert(InstructionFormatBuilder::new("nullary")); format.insert(InstructionFormatBuilder::new("nullary"));
let dummy_inst = InstructionBuilder::new("dummy", "doc").finish(&format); let mut inst_group = InstructionGroupBuilder::new("test", "", &format);
inst_group.push(InstructionBuilder::new("dummy", "doc"));
let inst_group = inst_group.finish();
let dummy_inst = inst_group.by_name("dummy");
let mut transform_group = TransformGroupBuilder::new("test", "doc"); let mut transform_group = TransformGroupBuilder::new("test", "doc");
transform_group.custom_legalize(&dummy_inst, "custom 1"); transform_group.custom_legalize(&dummy_inst, "custom 1");

View File

@@ -1,5 +1,5 @@
use crate::cdsl::cpu_modes::CpuMode; use crate::cdsl::cpu_modes::CpuMode;
use crate::cdsl::instructions::InstructionGroup; use crate::cdsl::instructions::InstructionGroupBuilder;
use crate::cdsl::isa::TargetIsa; use crate::cdsl::isa::TargetIsa;
use crate::cdsl::regs::{IsaRegs, IsaRegsBuilder, RegBankBuilder, RegClassBuilder}; use crate::cdsl::regs::{IsaRegs, IsaRegsBuilder, RegBankBuilder, RegClassBuilder};
use crate::cdsl::settings::{SettingGroup, SettingGroupBuilder}; use crate::cdsl::settings::{SettingGroup, SettingGroupBuilder};
@@ -52,7 +52,12 @@ pub fn define(shared_defs: &mut SharedDefinitions) -> TargetIsa {
let settings = define_settings(&shared_defs.settings); let settings = define_settings(&shared_defs.settings);
let regs = define_regs(); let regs = define_regs();
let inst_group = InstructionGroup::new("arm32", "arm32 specific instruction set"); let inst_group = InstructionGroupBuilder::new(
"arm32",
"arm32 specific instruction set",
&shared_defs.format_registry,
)
.finish();
// CPU modes for 32-bit ARM and Thumb2. // CPU modes for 32-bit ARM and Thumb2.
let mut a32 = CpuMode::new("A32"); let mut a32 = CpuMode::new("A32");

View File

@@ -1,5 +1,5 @@
use crate::cdsl::cpu_modes::CpuMode; use crate::cdsl::cpu_modes::CpuMode;
use crate::cdsl::instructions::InstructionGroup; use crate::cdsl::instructions::InstructionGroupBuilder;
use crate::cdsl::isa::TargetIsa; use crate::cdsl::isa::TargetIsa;
use crate::cdsl::regs::{IsaRegs, IsaRegsBuilder, RegBankBuilder, RegClassBuilder}; use crate::cdsl::regs::{IsaRegs, IsaRegsBuilder, RegBankBuilder, RegClassBuilder};
use crate::cdsl::settings::{SettingGroup, SettingGroupBuilder}; use crate::cdsl::settings::{SettingGroup, SettingGroupBuilder};
@@ -48,7 +48,12 @@ pub fn define(shared_defs: &mut SharedDefinitions) -> TargetIsa {
let settings = define_settings(&shared_defs.settings); let settings = define_settings(&shared_defs.settings);
let regs = define_registers(); let regs = define_registers();
let inst_group = InstructionGroup::new("arm64", "arm64 specific instruction set"); let inst_group = InstructionGroupBuilder::new(
"arm64",
"arm64 specific instruction set",
&shared_defs.format_registry,
)
.finish();
let mut a64 = CpuMode::new("A64"); let mut a64 = CpuMode::new("A64");

View File

@@ -1,5 +1,5 @@
use crate::cdsl::cpu_modes::CpuMode; use crate::cdsl::cpu_modes::CpuMode;
use crate::cdsl::instructions::InstructionGroup; use crate::cdsl::instructions::InstructionGroupBuilder;
use crate::cdsl::isa::TargetIsa; use crate::cdsl::isa::TargetIsa;
use crate::cdsl::regs::{IsaRegs, IsaRegsBuilder, RegBankBuilder, RegClassBuilder}; use crate::cdsl::regs::{IsaRegs, IsaRegsBuilder, RegBankBuilder, RegClassBuilder};
use crate::cdsl::settings::{PredicateNode, SettingGroup, SettingGroupBuilder}; use crate::cdsl::settings::{PredicateNode, SettingGroup, SettingGroupBuilder};
@@ -86,7 +86,12 @@ pub fn define(shared_defs: &mut SharedDefinitions) -> TargetIsa {
let settings = define_settings(&shared_defs.settings); let settings = define_settings(&shared_defs.settings);
let regs = define_registers(); let regs = define_registers();
let inst_group = InstructionGroup::new("riscv", "riscv specific instruction set"); let inst_group = InstructionGroupBuilder::new(
"riscv",
"riscv specific instruction set",
&shared_defs.format_registry,
)
.finish();
// CPU modes for 32-bit and 64-bit operation. // CPU modes for 32-bit and 64-bit operation.
let mut rv_32 = CpuMode::new("RV32"); let mut rv_32 = CpuMode::new("RV32");

View File

@@ -1,14 +1,17 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
use crate::cdsl::formats::FormatRegistry; use crate::cdsl::formats::FormatRegistry;
use crate::cdsl::instructions::{InstructionBuilder as Inst, InstructionGroup}; use crate::cdsl::instructions::{
InstructionBuilder as Inst, InstructionGroup, InstructionGroupBuilder,
};
use crate::cdsl::operands::{create_operand as operand, create_operand_doc as operand_doc}; use crate::cdsl::operands::{create_operand as operand, create_operand_doc as operand_doc};
use crate::cdsl::types::ValueType; use crate::cdsl::types::ValueType;
use crate::cdsl::typevar::{Interval, TypeSetBuilder, TypeVar}; use crate::cdsl::typevar::{Interval, TypeSetBuilder, TypeVar};
use crate::shared::types; use crate::shared::types;
pub fn define(format_registry: &FormatRegistry) -> InstructionGroup { pub fn define(format_registry: &FormatRegistry) -> InstructionGroup {
let mut ig = InstructionGroup::new("x86", "x86 specific instruction set"); let mut ig =
InstructionGroupBuilder::new("x86", "x86 specific instruction set", format_registry);
let iflags: &TypeVar = &ValueType::Special(types::Flag::IFlags.into()).into(); let iflags: &TypeVar = &ValueType::Special(types::Flag::IFlags.into()).into();
@@ -39,8 +42,7 @@ pub fn define(format_registry: &FormatRegistry) -> InstructionGroup {
) )
.operands_in(vec![nlo, nhi, d]) .operands_in(vec![nlo, nhi, d])
.operands_out(vec![q, r]) .operands_out(vec![q, r])
.can_trap(true) .can_trap(true),
.finish(format_registry),
); );
ig.push( ig.push(
@@ -59,8 +61,7 @@ pub fn define(format_registry: &FormatRegistry) -> InstructionGroup {
) )
.operands_in(vec![nlo, nhi, d]) .operands_in(vec![nlo, nhi, d])
.operands_out(vec![q, r]) .operands_out(vec![q, r])
.can_trap(true) .can_trap(true),
.finish(format_registry),
); );
let argL = &operand("argL", iWord); let argL = &operand("argL", iWord);
@@ -79,8 +80,7 @@ pub fn define(format_registry: &FormatRegistry) -> InstructionGroup {
"#, "#,
) )
.operands_in(vec![argL, argR]) .operands_in(vec![argL, argR])
.operands_out(vec![resLo, resHi]) .operands_out(vec![resLo, resHi]),
.finish(format_registry),
); );
ig.push( ig.push(
@@ -94,8 +94,7 @@ pub fn define(format_registry: &FormatRegistry) -> InstructionGroup {
"#, "#,
) )
.operands_in(vec![argL, argR]) .operands_in(vec![argL, argR])
.operands_out(vec![resLo, resHi]) .operands_out(vec![resLo, resHi]),
.finish(format_registry),
); );
let Float = &TypeVar::new( let Float = &TypeVar::new(
@@ -131,8 +130,7 @@ pub fn define(format_registry: &FormatRegistry) -> InstructionGroup {
"#, "#,
) )
.operands_in(vec![x]) .operands_in(vec![x])
.operands_out(vec![a]) .operands_out(vec![a]),
.finish(format_registry),
); );
let x = &operand("x", Float); let x = &operand("x", Float);
@@ -154,8 +152,7 @@ pub fn define(format_registry: &FormatRegistry) -> InstructionGroup {
"#, "#,
) )
.operands_in(vec![x, y]) .operands_in(vec![x, y])
.operands_out(vec![a]) .operands_out(vec![a]),
.finish(format_registry),
); );
ig.push( ig.push(
@@ -173,8 +170,7 @@ pub fn define(format_registry: &FormatRegistry) -> InstructionGroup {
"#, "#,
) )
.operands_in(vec![x, y]) .operands_in(vec![x, y])
.operands_out(vec![a]) .operands_out(vec![a]),
.finish(format_registry),
); );
let x = &operand("x", iWord); let x = &operand("x", iWord);
@@ -193,8 +189,7 @@ pub fn define(format_registry: &FormatRegistry) -> InstructionGroup {
) )
.operands_in(vec![x]) .operands_in(vec![x])
.other_side_effects(true) .other_side_effects(true)
.can_store(true) .can_store(true),
.finish(format_registry),
); );
ig.push( ig.push(
@@ -212,8 +207,7 @@ pub fn define(format_registry: &FormatRegistry) -> InstructionGroup {
) )
.operands_out(vec![x]) .operands_out(vec![x])
.other_side_effects(true) .other_side_effects(true)
.can_load(true) .can_load(true),
.finish(format_registry),
); );
let y = &operand("y", iWord); let y = &operand("y", iWord);
@@ -233,8 +227,7 @@ pub fn define(format_registry: &FormatRegistry) -> InstructionGroup {
"#, "#,
) )
.operands_in(vec![x]) .operands_in(vec![x])
.operands_out(vec![y, rflags]) .operands_out(vec![y, rflags]),
.finish(format_registry),
); );
ig.push( ig.push(
@@ -246,9 +239,8 @@ pub fn define(format_registry: &FormatRegistry) -> InstructionGroup {
"#, "#,
) )
.operands_in(vec![x]) .operands_in(vec![x])
.operands_out(vec![y, rflags]) .operands_out(vec![y, rflags]),
.finish(format_registry),
); );
ig ig.finish()
} }

File diff suppressed because it is too large Load Diff