Remove OpcodeNumber

This commit is contained in:
bjorn3
2021-10-12 14:58:34 +02:00
parent 99114547be
commit 466a446f8c
3 changed files with 11 additions and 23 deletions

View File

@@ -1,5 +1,3 @@
use cranelift_entity::{entity_impl, PrimaryMap};
use std::fmt; use std::fmt;
use std::rc::Rc; use std::rc::Rc;
@@ -9,11 +7,7 @@ use crate::cdsl::operands::Operand;
use crate::cdsl::type_inference::Constraint; use crate::cdsl::type_inference::Constraint;
use crate::cdsl::typevar::TypeVar; use crate::cdsl::typevar::TypeVar;
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] pub(crate) type AllInstructions = Vec<Instruction>;
pub(crate) struct OpcodeNumber(u32);
entity_impl!(OpcodeNumber);
pub(crate) type AllInstructions = PrimaryMap<OpcodeNumber, Instruction>;
pub(crate) struct InstructionGroupBuilder<'all_inst> { pub(crate) struct InstructionGroupBuilder<'all_inst> {
all_instructions: &'all_inst mut AllInstructions, all_instructions: &'all_inst mut AllInstructions,
@@ -25,8 +19,7 @@ impl<'all_inst> InstructionGroupBuilder<'all_inst> {
} }
pub fn push(&mut self, builder: InstructionBuilder) { pub fn push(&mut self, builder: InstructionBuilder) {
let opcode_number = OpcodeNumber(self.all_instructions.next_key().as_u32()); let inst = builder.build();
let inst = builder.build(opcode_number);
self.all_instructions.push(inst); self.all_instructions.push(inst);
} }
} }
@@ -42,7 +35,6 @@ pub(crate) struct InstructionContent {
/// Instruction mnemonic, also becomes opcode name. /// Instruction mnemonic, also becomes opcode name.
pub name: String, pub name: String,
pub camel_name: String, pub camel_name: String,
pub opcode_number: OpcodeNumber,
/// Documentation string. /// Documentation string.
pub doc: String, pub doc: String,
@@ -240,7 +232,7 @@ impl InstructionBuilder {
self self
} }
fn build(self, opcode_number: OpcodeNumber) -> Instruction { fn build(self) -> 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);
@@ -275,7 +267,6 @@ impl InstructionBuilder {
Rc::new(InstructionContent { Rc::new(InstructionContent {
name: self.name, name: self.name,
camel_name, camel_name,
opcode_number,
doc: self.doc, doc: self.doc,
operands_in, operands_in,
operands_out, operands_out,

View File

@@ -2,7 +2,6 @@
use std::fmt; use std::fmt;
use cranelift_codegen_shared::constant_hash; use cranelift_codegen_shared::constant_hash;
use cranelift_entity::EntityRef;
use crate::cdsl::camel_case; use crate::cdsl::camel_case;
use crate::cdsl::formats::InstructionFormat; use crate::cdsl::formats::InstructionFormat;
@@ -388,7 +387,7 @@ fn gen_bool_accessor<T: Fn(&Instruction) -> bool>(
fmtln!(fmt, "pub fn {}(self) -> bool {{", name); fmtln!(fmt, "pub fn {}(self) -> bool {{", name);
fmt.indent(|fmt| { fmt.indent(|fmt| {
let mut m = Match::new("self"); let mut m = Match::new("self");
for inst in all_inst.values() { for inst in all_inst.iter() {
if get_attr(inst) { if get_attr(inst) {
m.arm_no_fields(format!("Self::{}", inst.camel_name), "true"); m.arm_no_fields(format!("Self::{}", inst.camel_name), "true");
} }
@@ -424,7 +423,7 @@ fn gen_opcodes(all_inst: &AllInstructions, fmt: &mut Formatter) {
fmt.line("pub enum Opcode {"); fmt.line("pub enum Opcode {");
fmt.indent(|fmt| { fmt.indent(|fmt| {
let mut is_first_opcode = true; let mut is_first_opcode = true;
for inst in all_inst.values() { for inst in all_inst.iter() {
fmt.doc_comment(format!("`{}`. ({})", inst, inst.format.name)); fmt.doc_comment(format!("`{}`. ({})", inst, inst.format.name));
// Document polymorphism. // Document polymorphism.
@@ -440,8 +439,6 @@ fn gen_opcodes(all_inst: &AllInstructions, fmt: &mut Formatter) {
// Enum variant itself. // Enum variant itself.
if is_first_opcode { if is_first_opcode {
assert!(inst.opcode_number.index() == 0);
// TODO the python crate requires opcode numbers to start from one.
fmtln!(fmt, "{} = 1,", inst.camel_name); fmtln!(fmt, "{} = 1,", inst.camel_name);
is_first_opcode = false; is_first_opcode = false;
} else { } else {
@@ -535,7 +532,7 @@ fn gen_opcodes(all_inst: &AllInstructions, fmt: &mut Formatter) {
all_inst.len() all_inst.len()
); );
fmt.indent(|fmt| { fmt.indent(|fmt| {
for inst in all_inst.values() { for inst in all_inst.iter() {
fmtln!( fmtln!(
fmt, fmt,
"InstructionFormat::{}, // {}", "InstructionFormat::{}, // {}",
@@ -551,7 +548,7 @@ fn gen_opcodes(all_inst: &AllInstructions, fmt: &mut Formatter) {
fmt.line("fn opcode_name(opc: Opcode) -> &\'static str {"); fmt.line("fn opcode_name(opc: Opcode) -> &\'static str {");
fmt.indent(|fmt| { fmt.indent(|fmt| {
let mut m = Match::new("opc"); let mut m = Match::new("opc");
for inst in all_inst.values() { for inst in all_inst.iter() {
m.arm_no_fields( m.arm_no_fields(
format!("Opcode::{}", inst.camel_name), format!("Opcode::{}", inst.camel_name),
format!("\"{}\"", inst.name), format!("\"{}\"", inst.name),
@@ -563,7 +560,7 @@ fn gen_opcodes(all_inst: &AllInstructions, fmt: &mut Formatter) {
fmt.empty_line(); fmt.empty_line();
// Generate an opcode hash table for looking up opcodes by name. // Generate an opcode hash table for looking up opcodes by name.
let hash_table = constant_hash::generate_table(all_inst.values(), all_inst.len(), |inst| { let hash_table = constant_hash::generate_table(all_inst.iter(), all_inst.len(), |inst| {
constant_hash::simple_hash(&inst.name) constant_hash::simple_hash(&inst.name)
}); });
fmtln!( fmtln!(
@@ -736,7 +733,7 @@ fn gen_type_constraints(all_inst: &AllInstructions, fmt: &mut Formatter) {
all_inst.len() all_inst.len()
); );
fmt.indent(|fmt| { fmt.indent(|fmt| {
for inst in all_inst.values() { for inst in all_inst.iter() {
let (ctrl_typevar, ctrl_typeset) = if let Some(poly) = &inst.polymorphic_info { let (ctrl_typevar, ctrl_typeset) = if let Some(poly) = &inst.polymorphic_info {
let index = type_sets.add(&*poly.ctrl_typevar.get_raw_typeset()); let index = type_sets.add(&*poly.ctrl_typevar.get_raw_typeset());
(Some(&poly.ctrl_typevar), index) (Some(&poly.ctrl_typevar), index)
@@ -1130,7 +1127,7 @@ fn gen_builder(
); );
fmt.line("pub trait InstBuilder<'f>: InstBuilderBase<'f> {"); fmt.line("pub trait InstBuilder<'f>: InstBuilderBase<'f> {");
fmt.indent(|fmt| { fmt.indent(|fmt| {
for inst in instructions.values() { for inst in instructions.iter() {
gen_inst_builder(inst, &*inst.format, fmt); gen_inst_builder(inst, &*inst.format, fmt);
fmt.empty_line(); fmt.empty_line();
} }

View File

@@ -54,7 +54,7 @@ impl Definitions {
// of immediate fields. // of immediate fields.
let mut format_structures: HashMap<FormatStructure, &InstructionFormat> = HashMap::new(); let mut format_structures: HashMap<FormatStructure, &InstructionFormat> = HashMap::new();
for inst in self.all_instructions.values() { for inst in self.all_instructions.iter() {
// Check name. // Check name.
if let Some(existing_format) = format_names.get(&inst.format.name) { if let Some(existing_format) = format_names.get(&inst.format.name) {
assert!( assert!(