|
@ -5,12 +5,11 @@ |
|
|
//
|
|
|
//
|
|
|
// ====--------------------------------------------------------------------------------------====//
|
|
|
// ====--------------------------------------------------------------------------------------====//
|
|
|
|
|
|
|
|
|
use std::collections::HashMap; |
|
|
|
|
|
use std::str::FromStr; |
|
|
use std::str::FromStr; |
|
|
use std::u32; |
|
|
use std::u32; |
|
|
use std::mem; |
|
|
use std::mem; |
|
|
use cretonne::ir::{Function, Ebb, Inst, Opcode, Value, Type, FunctionName, StackSlot, |
|
|
use cretonne::ir::{Function, Ebb, Inst, Opcode, Value, Type, FunctionName, StackSlotData, |
|
|
StackSlotData, JumpTable, JumpTableData}; |
|
|
JumpTable, JumpTableData}; |
|
|
use cretonne::ir::types::{VOID, Signature, ArgumentType, ArgumentExtension}; |
|
|
use cretonne::ir::types::{VOID, Signature, ArgumentType, ArgumentExtension}; |
|
|
use cretonne::ir::immediates::{Imm64, Ieee32, Ieee64}; |
|
|
use cretonne::ir::immediates::{Imm64, Ieee32, Ieee64}; |
|
|
use cretonne::ir::entities::{AnyEntity, NO_EBB, NO_INST, NO_VALUE}; |
|
|
use cretonne::ir::entities::{AnyEntity, NO_EBB, NO_INST, NO_VALUE}; |
|
@ -20,7 +19,7 @@ use testfile::{TestFile, Details, Comment}; |
|
|
use error::{Location, Error, Result}; |
|
|
use error::{Location, Error, Result}; |
|
|
use lexer::{self, Lexer, Token}; |
|
|
use lexer::{self, Lexer, Token}; |
|
|
use testcommand::TestCommand; |
|
|
use testcommand::TestCommand; |
|
|
use sourcemap; |
|
|
use sourcemap::{SourceMap, MutableSourceMap}; |
|
|
|
|
|
|
|
|
/// Parse the entire `text` into a list of functions.
|
|
|
/// Parse the entire `text` into a list of functions.
|
|
|
///
|
|
|
///
|
|
@ -65,10 +64,7 @@ pub struct Parser<'a> { |
|
|
// file by number. We need to map these numbers to real references.
|
|
|
// file by number. We need to map these numbers to real references.
|
|
|
struct Context { |
|
|
struct Context { |
|
|
function: Function, |
|
|
function: Function, |
|
|
stack_slots: HashMap<u32, StackSlot>, // ssNN
|
|
|
map: SourceMap, |
|
|
jump_tables: HashMap<u32, JumpTable>, // jtNN
|
|
|
|
|
|
ebbs: HashMap<Ebb, Ebb>, // ebbNN
|
|
|
|
|
|
values: HashMap<Value, Value>, // vNN, vxNN
|
|
|
|
|
|
|
|
|
|
|
|
// Remember the location of every instruction.
|
|
|
// Remember the location of every instruction.
|
|
|
inst_locs: Vec<(Inst, Location)>, |
|
|
inst_locs: Vec<(Inst, Location)>, |
|
@ -78,36 +74,25 @@ impl Context { |
|
|
fn new(f: Function) -> Context { |
|
|
fn new(f: Function) -> Context { |
|
|
Context { |
|
|
Context { |
|
|
function: f, |
|
|
function: f, |
|
|
stack_slots: HashMap::new(), |
|
|
map: SourceMap::new(), |
|
|
jump_tables: HashMap::new(), |
|
|
|
|
|
ebbs: HashMap::new(), |
|
|
|
|
|
values: HashMap::new(), |
|
|
|
|
|
inst_locs: Vec::new(), |
|
|
inst_locs: Vec::new(), |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Allocate a new stack slot and add a mapping number -> StackSlot.
|
|
|
// Allocate a new stack slot and add a mapping number -> StackSlot.
|
|
|
fn add_ss(&mut self, number: u32, data: StackSlotData, loc: &Location) -> Result<()> { |
|
|
fn add_ss(&mut self, number: u32, data: StackSlotData, loc: &Location) -> Result<()> { |
|
|
if self.stack_slots.insert(number, self.function.stack_slots.push(data)).is_some() { |
|
|
self.map.def_ss(number, self.function.stack_slots.push(data), loc) |
|
|
err!(loc, "duplicate stack slot: ss{}", number) |
|
|
|
|
|
} else { |
|
|
|
|
|
Ok(()) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Allocate a new jump table and add a mapping number -> JumpTable.
|
|
|
// Allocate a new jump table and add a mapping number -> JumpTable.
|
|
|
fn add_jt(&mut self, number: u32, data: JumpTableData, loc: &Location) -> Result<()> { |
|
|
fn add_jt(&mut self, number: u32, data: JumpTableData, loc: &Location) -> Result<()> { |
|
|
if self.jump_tables.insert(number, self.function.jump_tables.push(data)).is_some() { |
|
|
self.map.def_jt(number, self.function.jump_tables.push(data), loc) |
|
|
err!(loc, "duplicate jump table: jt{}", number) |
|
|
|
|
|
} else { |
|
|
|
|
|
Ok(()) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Resolve a reference to a jump table.
|
|
|
// Resolve a reference to a jump table.
|
|
|
fn get_jt(&self, number: u32, loc: &Location) -> Result<JumpTable> { |
|
|
fn get_jt(&self, number: u32, loc: &Location) -> Result<JumpTable> { |
|
|
match self.jump_tables.get(&number) { |
|
|
match self.map.get_jt(number) { |
|
|
Some(&jt) => Ok(jt), |
|
|
Some(jt) => Ok(jt), |
|
|
None => err!(loc, "undefined jump table jt{}", number), |
|
|
None => err!(loc, "undefined jump table jt{}", number), |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -116,20 +101,7 @@ impl Context { |
|
|
fn add_ebb(&mut self, src_ebb: Ebb, loc: &Location) -> Result<Ebb> { |
|
|
fn add_ebb(&mut self, src_ebb: Ebb, loc: &Location) -> Result<Ebb> { |
|
|
let ebb = self.function.dfg.make_ebb(); |
|
|
let ebb = self.function.dfg.make_ebb(); |
|
|
self.function.layout.append_ebb(ebb); |
|
|
self.function.layout.append_ebb(ebb); |
|
|
if self.ebbs.insert(src_ebb, ebb).is_some() { |
|
|
self.map.def_ebb(src_ebb, ebb, loc).and(Ok(ebb)) |
|
|
err!(loc, "duplicate EBB: {}", src_ebb) |
|
|
|
|
|
} else { |
|
|
|
|
|
Ok(ebb) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Add a value mapping src_val -> data.
|
|
|
|
|
|
fn add_value(&mut self, src_val: Value, data: Value, loc: &Location) -> Result<()> { |
|
|
|
|
|
if self.values.insert(src_val, data).is_some() { |
|
|
|
|
|
err!(loc, "duplicate value: {}", src_val) |
|
|
|
|
|
} else { |
|
|
|
|
|
Ok(()) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Record the location of an instuction.
|
|
|
// Record the location of an instuction.
|
|
@ -140,41 +112,6 @@ impl Context { |
|
|
// The parser creates all instructions with Ebb and Value references using the source file
|
|
|
// The parser creates all instructions with Ebb and Value references using the source file
|
|
|
// numbering. These references need to be rewritten after parsing is complete since forward
|
|
|
// numbering. These references need to be rewritten after parsing is complete since forward
|
|
|
// references are allowed.
|
|
|
// references are allowed.
|
|
|
|
|
|
|
|
|
// Rewrite an Ebb reference.
|
|
|
|
|
|
fn rewrite_ebb(map: &HashMap<Ebb, Ebb>, ebb: &mut Ebb, loc: &Location) -> Result<()> { |
|
|
|
|
|
match map.get(ebb) { |
|
|
|
|
|
Some(&new) => { |
|
|
|
|
|
*ebb = new; |
|
|
|
|
|
Ok(()) |
|
|
|
|
|
} |
|
|
|
|
|
None => err!(loc, "undefined reference: {}", ebb), |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Rewrite a value reference.
|
|
|
|
|
|
fn rewrite_value(map: &HashMap<Value, Value>, val: &mut Value, loc: &Location) -> Result<()> { |
|
|
|
|
|
match map.get(val) { |
|
|
|
|
|
Some(&new) => { |
|
|
|
|
|
*val = new; |
|
|
|
|
|
Ok(()) |
|
|
|
|
|
} |
|
|
|
|
|
None => err!(loc, "undefined reference: {}", val), |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Rewrite a slice of value references.
|
|
|
|
|
|
fn rewrite_values(map: &HashMap<Value, Value>, |
|
|
|
|
|
vals: &mut [Value], |
|
|
|
|
|
loc: &Location) |
|
|
|
|
|
-> Result<()> { |
|
|
|
|
|
for val in vals { |
|
|
|
|
|
try!(Self::rewrite_value(map, val, loc)); |
|
|
|
|
|
} |
|
|
|
|
|
Ok(()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Rewrite all EBB and value references in the function.
|
|
|
|
|
|
fn rewrite_references(&mut self) -> Result<()> { |
|
|
fn rewrite_references(&mut self) -> Result<()> { |
|
|
for &(inst, loc) in &self.inst_locs { |
|
|
for &(inst, loc) in &self.inst_locs { |
|
|
match self.function.dfg[inst] { |
|
|
match self.function.dfg[inst] { |
|
@ -189,7 +126,7 @@ impl Context { |
|
|
InstructionData::BinaryImmRev { ref mut arg, .. } | |
|
|
InstructionData::BinaryImmRev { ref mut arg, .. } | |
|
|
InstructionData::ExtractLane { ref mut arg, .. } | |
|
|
InstructionData::ExtractLane { ref mut arg, .. } | |
|
|
InstructionData::BranchTable { ref mut arg, .. } => { |
|
|
InstructionData::BranchTable { ref mut arg, .. } => { |
|
|
try!(Self::rewrite_value(&self.values, arg, &loc)); |
|
|
try!(self.map.rewrite_value(arg, &loc)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
InstructionData::Binary { ref mut args, .. } | |
|
|
InstructionData::Binary { ref mut args, .. } | |
|
@ -197,30 +134,30 @@ impl Context { |
|
|
InstructionData::InsertLane { ref mut args, .. } | |
|
|
InstructionData::InsertLane { ref mut args, .. } | |
|
|
InstructionData::IntCompare { ref mut args, .. } | |
|
|
InstructionData::IntCompare { ref mut args, .. } | |
|
|
InstructionData::FloatCompare { ref mut args, .. } => { |
|
|
InstructionData::FloatCompare { ref mut args, .. } => { |
|
|
try!(Self::rewrite_values(&self.values, args, &loc)); |
|
|
try!(self.map.rewrite_values(args, &loc)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
InstructionData::Ternary { ref mut args, .. } => { |
|
|
InstructionData::Ternary { ref mut args, .. } => { |
|
|
try!(Self::rewrite_values(&self.values, args, &loc)); |
|
|
try!(self.map.rewrite_values(args, &loc)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
InstructionData::Jump { ref mut data, .. } => { |
|
|
InstructionData::Jump { ref mut data, .. } => { |
|
|
try!(Self::rewrite_ebb(&self.ebbs, &mut data.destination, &loc)); |
|
|
try!(self.map.rewrite_ebb(&mut data.destination, &loc)); |
|
|
try!(Self::rewrite_values(&self.values, &mut data.arguments, &loc)); |
|
|
try!(self.map.rewrite_values(&mut data.arguments, &loc)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
InstructionData::Branch { ref mut data, .. } => { |
|
|
InstructionData::Branch { ref mut data, .. } => { |
|
|
try!(Self::rewrite_value(&self.values, &mut data.arg, &loc)); |
|
|
try!(self.map.rewrite_value(&mut data.arg, &loc)); |
|
|
try!(Self::rewrite_ebb(&self.ebbs, &mut data.destination, &loc)); |
|
|
try!(self.map.rewrite_ebb(&mut data.destination, &loc)); |
|
|
try!(Self::rewrite_values(&self.values, &mut data.arguments, &loc)); |
|
|
try!(self.map.rewrite_values(&mut data.arguments, &loc)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
InstructionData::Call { ref mut data, .. } => { |
|
|
InstructionData::Call { ref mut data, .. } => { |
|
|
try!(Self::rewrite_values(&self.values, &mut data.args, &loc)); |
|
|
try!(self.map.rewrite_values(&mut data.args, &loc)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
InstructionData::Return { ref mut data, .. } => { |
|
|
InstructionData::Return { ref mut data, .. } => { |
|
|
try!(Self::rewrite_values(&self.values, &mut data.args, &loc)); |
|
|
try!(self.map.rewrite_values(&mut data.args, &loc)); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -230,7 +167,7 @@ impl Context { |
|
|
for jt in self.function.jump_tables.keys() { |
|
|
for jt in self.function.jump_tables.keys() { |
|
|
for ebb in self.function.jump_tables[jt].as_mut_slice() { |
|
|
for ebb in self.function.jump_tables[jt].as_mut_slice() { |
|
|
if *ebb != NO_EBB { |
|
|
if *ebb != NO_EBB { |
|
|
try!(Self::rewrite_ebb(&self.ebbs, ebb, &loc)); |
|
|
try!(self.map.rewrite_ebb(ebb, &loc)); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -512,7 +449,7 @@ impl<'a> Parser<'a> { |
|
|
let details = Details { |
|
|
let details = Details { |
|
|
location: location, |
|
|
location: location, |
|
|
comments: mem::replace(&mut self.comments, Vec::new()), |
|
|
comments: mem::replace(&mut self.comments, Vec::new()), |
|
|
map: sourcemap::new(ctx.values, ctx.ebbs, ctx.stack_slots, ctx.jump_tables), |
|
|
map: ctx.map, |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
Ok((ctx.function, details)) |
|
|
Ok((ctx.function, details)) |
|
@ -777,7 +714,7 @@ impl<'a> Parser<'a> { |
|
|
let t = try!(self.match_type("expected EBB argument type")); |
|
|
let t = try!(self.match_type("expected EBB argument type")); |
|
|
// Allocate the EBB argument and add the mapping.
|
|
|
// Allocate the EBB argument and add the mapping.
|
|
|
let value = ctx.function.dfg.append_ebb_arg(ebb, t); |
|
|
let value = ctx.function.dfg.append_ebb_arg(ebb, t); |
|
|
ctx.add_value(vx, value, &vx_location) |
|
|
ctx.map.def_value(vx, value, &vx_location) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Parse an instruction, append it to `ebb`.
|
|
|
// Parse an instruction, append it to `ebb`.
|
|
@ -856,7 +793,7 @@ impl<'a> Parser<'a> { |
|
|
// Now map the source result values to the just created instruction results.
|
|
|
// Now map the source result values to the just created instruction results.
|
|
|
// Pass a reference to `ctx.values` instead of `ctx` itself since the `Values` iterator
|
|
|
// Pass a reference to `ctx.values` instead of `ctx` itself since the `Values` iterator
|
|
|
// holds a reference to `ctx.function`.
|
|
|
// holds a reference to `ctx.function`.
|
|
|
self.add_values(&mut ctx.values, |
|
|
self.add_values(&mut ctx.map, |
|
|
results.into_iter(), |
|
|
results.into_iter(), |
|
|
ctx.function.dfg.inst_results(inst)) |
|
|
ctx.function.dfg.inst_results(inst)) |
|
|
} |
|
|
} |
|
@ -888,8 +825,8 @@ impl<'a> Parser<'a> { |
|
|
// layout of the blocks.
|
|
|
// layout of the blocks.
|
|
|
let ctrl_src_value = inst_data.typevar_operand() |
|
|
let ctrl_src_value = inst_data.typevar_operand() |
|
|
.expect("Constraints <-> Format inconsistency"); |
|
|
.expect("Constraints <-> Format inconsistency"); |
|
|
ctx.function.dfg.value_type(match ctx.values.get(&ctrl_src_value) { |
|
|
ctx.function.dfg.value_type(match ctx.map.get_value(ctrl_src_value) { |
|
|
Some(&v) => v, |
|
|
Some(v) => v, |
|
|
None => { |
|
|
None => { |
|
|
return err!(self.loc, |
|
|
return err!(self.loc, |
|
|
"cannot determine type of operand {}", |
|
|
"cannot determine type of operand {}", |
|
@ -932,18 +869,12 @@ impl<'a> Parser<'a> { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Add mappings for a list of source values to their corresponding new values.
|
|
|
// Add mappings for a list of source values to their corresponding new values.
|
|
|
fn add_values<S, V>(&self, |
|
|
fn add_values<S, V>(&self, map: &mut SourceMap, results: S, new_results: V) -> Result<()> |
|
|
values: &mut HashMap<Value, Value>, |
|
|
|
|
|
results: S, |
|
|
|
|
|
new_results: V) |
|
|
|
|
|
-> Result<()> |
|
|
|
|
|
where S: Iterator<Item = Value>, |
|
|
where S: Iterator<Item = Value>, |
|
|
V: Iterator<Item = Value> |
|
|
V: Iterator<Item = Value> |
|
|
{ |
|
|
{ |
|
|
for (src, val) in results.zip(new_results) { |
|
|
for (src, val) in results.zip(new_results) { |
|
|
if values.insert(src, val).is_some() { |
|
|
try!(map.def_value(src, val, &self.loc)); |
|
|
return err!(self.loc, "duplicate result value: {}", src); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
Ok(()) |
|
|
Ok(()) |
|
|
} |
|
|
} |
|
|