Path: blob/main/cranelift/reader/src/parser.rs
2450 views
//! Parser for .clif files.12use crate::error::{Location, ParseError, ParseResult};3use crate::isaspec;4use crate::lexer::{LexError, Lexer, LocatedError, LocatedToken, Token};5use crate::run_command::{Comparison, Invocation, RunCommand};6use crate::sourcemap::SourceMap;7use crate::testcommand::TestCommand;8use crate::testfile::{Comment, Details, Feature, TestFile};9use cranelift_codegen::data_value::DataValue;10use cranelift_codegen::entity::{EntityRef, PrimaryMap};11use cranelift_codegen::ir::entities::{AnyEntity, DynamicType, MemoryType};12use cranelift_codegen::ir::immediates::{13Ieee16, Ieee32, Ieee64, Ieee128, Imm64, Offset32, Uimm32, Uimm64,14};15use cranelift_codegen::ir::instructions::{InstructionData, InstructionFormat, VariableArgs};16use cranelift_codegen::ir::pcc::{BaseExpr, Expr, Fact};17use cranelift_codegen::ir::{self, StackSlotKey, UserExternalNameRef};18use cranelift_codegen::ir::{DebugTag, types::*};1920use cranelift_codegen::ir::{21AbiParam, ArgumentExtension, ArgumentPurpose, Block, BlockArg, Constant, ConstantData,22DynamicStackSlot, DynamicStackSlotData, DynamicTypeData, ExtFuncData, ExternalName, FuncRef,23Function, GlobalValue, GlobalValueData, JumpTableData, MemFlags, MemoryTypeData,24MemoryTypeField, Opcode, SigRef, Signature, StackSlot, StackSlotData, StackSlotKind,25UserFuncName, Value, types,26};27use cranelift_codegen::isa::{self, CallConv};28use cranelift_codegen::packed_option::ReservedValue;29use cranelift_codegen::{settings, settings::Configurable, timing};30use smallvec::SmallVec;31use std::mem;32use std::str::FromStr;33use std::{u16, u32};34use target_lexicon::Triple;3536macro_rules! match_imm {37($signed:ty, $unsigned:ty, $parser:expr, $err_msg:expr) => {{38if let Some(Token::Integer(text)) = $parser.token() {39$parser.consume();40let negative = text.starts_with('-');41let positive = text.starts_with('+');42let text = if negative || positive {43// Strip sign prefix.44&text[1..]45} else {46text47};4849// Parse the text value; the lexer gives us raw text that looks like an integer.50let value = if text.starts_with("0x") {51// Skip underscores.52let text = text.replace("_", "");53// Parse it in hexadecimal form.54<$unsigned>::from_str_radix(&text[2..], 16).map_err(|_| {55$parser.error(&format!(56"unable to parse '{}' value as a hexadecimal {} immediate",57&text[2..],58stringify!($unsigned),59))60})?61} else {62// Parse it as a signed type to check for overflow and other issues.63text.parse()64.map_err(|_| $parser.error("expected decimal immediate"))?65};6667// Apply sign if necessary.68let signed = if negative {69let value = value.wrapping_neg() as $signed;70if value > 0 {71return Err($parser.error("negative number too small"));72}73value74} else {75value as $signed76};7778Ok(signed)79} else {80err!($parser.loc, $err_msg)81}82}};83}8485/// After some quick benchmarks a program should never have more than 100,000 blocks.86const MAX_BLOCKS_IN_A_FUNCTION: u32 = 100_000;8788/// Parse the entire `text` into a list of functions.89///90/// Any test commands or target declarations are ignored.91pub fn parse_functions(text: &str) -> ParseResult<Vec<Function>> {92let _tt = timing::parse_text();93parse_test(text, ParseOptions::default())94.map(|file| file.functions.into_iter().map(|(func, _)| func).collect())95}9697/// Options for configuring the parsing of filetests.98pub struct ParseOptions<'a> {99/// Compiler passes to run on the parsed functions.100pub passes: Option<&'a [String]>,101/// Target ISA for compiling the parsed functions, e.g. "x86_64 skylake".102pub target: Option<&'a str>,103/// Default calling convention used when none is specified for a parsed function.104pub default_calling_convention: CallConv,105/// Default for unwind-info setting (enabled or disabled).106pub unwind_info: bool,107/// Default for machine_code_cfg_info setting (enabled or disabled).108pub machine_code_cfg_info: bool,109}110111impl Default for ParseOptions<'_> {112fn default() -> Self {113Self {114passes: None,115target: None,116default_calling_convention: CallConv::Fast,117unwind_info: false,118machine_code_cfg_info: false,119}120}121}122123/// Parse the entire `text` as a test case file.124///125/// The returned `TestFile` contains direct references to substrings of `text`.126pub fn parse_test<'a>(text: &'a str, options: ParseOptions<'a>) -> ParseResult<TestFile<'a>> {127let _tt = timing::parse_text();128let mut parser = Parser::new(text);129130// Gather the preamble comments.131parser.start_gathering_comments();132133let isa_spec: isaspec::IsaSpec;134let commands: Vec<TestCommand<'a>>;135136// Check for specified passes and target, if present throw out test commands/targets specified137// in file.138match options.passes {139Some(pass_vec) => {140parser.parse_test_commands();141commands = parser.parse_cmdline_passes(pass_vec);142parser.parse_target_specs(&options)?;143isa_spec = parser.parse_cmdline_target(options.target)?;144}145None => {146commands = parser.parse_test_commands();147isa_spec = parser.parse_target_specs(&options)?;148}149};150let features = parser.parse_cranelift_features()?;151152// Decide between using the calling convention passed in the options or using the153// host's calling convention--if any tests are to be run on the host we should default to the154// host's calling convention.155parser = if commands.iter().any(|tc| tc.command == "run") {156let host_default_calling_convention = CallConv::triple_default(&Triple::host());157parser.with_default_calling_convention(host_default_calling_convention)158} else {159parser.with_default_calling_convention(options.default_calling_convention)160};161162parser.token();163parser.claim_gathered_comments(AnyEntity::Function);164165let preamble_comments = parser.take_comments();166let functions = parser.parse_function_list()?;167168Ok(TestFile {169commands,170isa_spec,171features,172preamble_comments,173functions,174})175}176177/// Parse a CLIF comment `text` as a run command.178///179/// Return:180/// - `Ok(None)` if the comment is not intended to be a `RunCommand` (i.e. does not start with `run`181/// or `print`182/// - `Ok(Some(command))` if the comment is intended as a `RunCommand` and can be parsed to one183/// - `Err` otherwise.184pub fn parse_run_command(text: &str, signature: &Signature) -> ParseResult<Option<RunCommand>> {185let _tt = timing::parse_text();186// We remove leading spaces and semi-colons for convenience here instead of at the call sites187// since this function will be attempting to parse a RunCommand from a CLIF comment.188let trimmed_text = text.trim_start_matches(|c| c == ' ' || c == ';');189let mut parser = Parser::new(trimmed_text);190match parser.token() {191Some(Token::Identifier("run")) | Some(Token::Identifier("print")) => {192parser.parse_run_command(signature).map(|c| Some(c))193}194Some(_) | None => Ok(None),195}196}197198pub struct Parser<'a> {199lex: Lexer<'a>,200201lex_error: Option<LexError>,202203/// Current lookahead token.204lookahead: Option<Token<'a>>,205206/// Location of lookahead.207loc: Location,208209/// Are we gathering any comments that we encounter?210gathering_comments: bool,211212/// The gathered comments; claim them with `claim_gathered_comments`.213gathered_comments: Vec<&'a str>,214215/// Comments collected so far.216comments: Vec<Comment<'a>>,217218/// Maps inlined external names to a ref value, so they can be declared before parsing the rest219/// of the function later.220///221/// This maintains backward compatibility with previous ways for declaring external names.222predeclared_external_names: PrimaryMap<UserExternalNameRef, ir::UserExternalName>,223224/// Default calling conventions; used when none is specified.225default_calling_convention: CallConv,226}227228/// Context for resolving references when parsing a single function.229struct Context {230function: Function,231map: SourceMap,232233/// Aliases to resolve once value definitions are known.234aliases: Vec<Value>,235}236237impl Context {238fn new(f: Function) -> Self {239Self {240function: f,241map: SourceMap::new(),242aliases: Vec::new(),243}244}245246// Allocate a new stack slot.247fn add_ss(&mut self, ss: StackSlot, data: StackSlotData, loc: Location) -> ParseResult<()> {248self.map.def_ss(ss, loc)?;249while self.function.sized_stack_slots.next_key().index() <= ss.index() {250self.function.create_sized_stack_slot(StackSlotData::new(251StackSlotKind::ExplicitSlot,2520,2530,254));255}256self.function.sized_stack_slots[ss] = data;257Ok(())258}259260// Resolve a reference to a stack slot.261fn check_ss(&self, ss: StackSlot, loc: Location) -> ParseResult<()> {262if !self.map.contains_ss(ss) {263err!(loc, "undefined stack slot {}", ss)264} else {265Ok(())266}267}268269// Allocate a new stack slot.270fn add_dss(271&mut self,272ss: DynamicStackSlot,273data: DynamicStackSlotData,274loc: Location,275) -> ParseResult<()> {276self.map.def_dss(ss, loc)?;277while self.function.dynamic_stack_slots.next_key().index() <= ss.index() {278self.function279.create_dynamic_stack_slot(DynamicStackSlotData::new(280StackSlotKind::ExplicitDynamicSlot,281data.dyn_ty,282));283}284self.function.dynamic_stack_slots[ss] = data;285Ok(())286}287288// Resolve a reference to a dynamic stack slot.289fn check_dss(&self, dss: DynamicStackSlot, loc: Location) -> ParseResult<()> {290if !self.map.contains_dss(dss) {291err!(loc, "undefined dynamic stack slot {}", dss)292} else {293Ok(())294}295}296297// Allocate a new dynamic type.298fn add_dt(&mut self, dt: DynamicType, data: DynamicTypeData, loc: Location) -> ParseResult<()> {299self.map.def_dt(dt, loc)?;300while self.function.dfg.dynamic_types.next_key().index() <= dt.index() {301self.function.dfg.make_dynamic_ty(DynamicTypeData::new(302data.base_vector_ty,303data.dynamic_scale,304));305}306self.function.dfg.dynamic_types[dt] = data;307Ok(())308}309310// Allocate a global value slot.311fn add_gv(312&mut self,313gv: GlobalValue,314data: GlobalValueData,315maybe_fact: Option<Fact>,316loc: Location,317) -> ParseResult<()> {318self.map.def_gv(gv, loc)?;319while self.function.global_values.next_key().index() <= gv.index() {320self.function.create_global_value(GlobalValueData::Symbol {321name: ExternalName::testcase(""),322offset: Imm64::new(0),323colocated: false,324tls: false,325});326}327self.function.global_values[gv] = data;328if let Some(fact) = maybe_fact {329self.function.global_value_facts[gv] = Some(fact);330}331Ok(())332}333334// Allocate a memory-type slot.335fn add_mt(&mut self, mt: MemoryType, data: MemoryTypeData, loc: Location) -> ParseResult<()> {336self.map.def_mt(mt, loc)?;337while self.function.memory_types.next_key().index() <= mt.index() {338self.function.create_memory_type(MemoryTypeData::default());339}340self.function.memory_types[mt] = data;341Ok(())342}343344// Resolve a reference to a global value.345fn check_gv(&self, gv: GlobalValue, loc: Location) -> ParseResult<()> {346if !self.map.contains_gv(gv) {347err!(loc, "undefined global value {}", gv)348} else {349Ok(())350}351}352353// Allocate a new signature.354fn add_sig(355&mut self,356sig: SigRef,357data: Signature,358loc: Location,359defaultcc: CallConv,360) -> ParseResult<()> {361self.map.def_sig(sig, loc)?;362while self.function.dfg.signatures.next_key().index() <= sig.index() {363self.function.import_signature(Signature::new(defaultcc));364}365self.function.dfg.signatures[sig] = data;366Ok(())367}368369// Resolve a reference to a signature.370fn check_sig(&self, sig: SigRef, loc: Location) -> ParseResult<()> {371if !self.map.contains_sig(sig) {372err!(loc, "undefined signature {}", sig)373} else {374Ok(())375}376}377378// Allocate a new external function.379fn add_fn(&mut self, fn_: FuncRef, data: ExtFuncData, loc: Location) -> ParseResult<()> {380self.map.def_fn(fn_, loc)?;381while self.function.dfg.ext_funcs.next_key().index() <= fn_.index() {382self.function.import_function(ExtFuncData {383name: ExternalName::testcase(""),384signature: SigRef::reserved_value(),385colocated: false,386patchable: false,387});388}389self.function.dfg.ext_funcs[fn_] = data;390Ok(())391}392393// Resolve a reference to a function.394fn check_fn(&self, fn_: FuncRef, loc: Location) -> ParseResult<()> {395if !self.map.contains_fn(fn_) {396err!(loc, "undefined function {}", fn_)397} else {398Ok(())399}400}401402// Allocate a new constant.403fn add_constant(404&mut self,405constant: Constant,406data: ConstantData,407loc: Location,408) -> ParseResult<()> {409self.map.def_constant(constant, loc)?;410self.function.dfg.constants.set(constant, data);411Ok(())412}413414// Configure the stack limit of the current function.415fn add_stack_limit(&mut self, limit: GlobalValue, loc: Location) -> ParseResult<()> {416if self.function.stack_limit.is_some() {417return err!(loc, "stack limit defined twice");418}419self.function.stack_limit = Some(limit);420Ok(())421}422423// Resolve a reference to a constant.424fn check_constant(&self, c: Constant, loc: Location) -> ParseResult<()> {425if !self.map.contains_constant(c) {426err!(loc, "undefined constant {}", c)427} else {428Ok(())429}430}431432// Allocate a new block.433fn add_block(&mut self, block: Block, loc: Location) -> ParseResult<Block> {434self.map.def_block(block, loc)?;435while self.function.dfg.num_blocks() <= block.index() {436self.function.dfg.make_block();437}438self.function.layout.append_block(block);439Ok(block)440}441442/// Set a block as cold.443fn set_cold_block(&mut self, block: Block) {444self.function.layout.set_cold(block);445}446}447448impl<'a> Parser<'a> {449/// Create a new `Parser` which reads `text`. The referenced text must outlive the parser.450pub fn new(text: &'a str) -> Self {451Self {452lex: Lexer::new(text),453lex_error: None,454lookahead: None,455loc: Location { line_number: 0 },456gathering_comments: false,457gathered_comments: Vec::new(),458comments: Vec::new(),459default_calling_convention: CallConv::Fast,460predeclared_external_names: Default::default(),461}462}463464/// Modify the default calling convention; returns a new parser with the changed calling465/// convention.466pub fn with_default_calling_convention(self, default_calling_convention: CallConv) -> Self {467Self {468default_calling_convention,469..self470}471}472473// Consume the current lookahead token and return it.474fn consume(&mut self) -> Token<'a> {475self.lookahead.take().expect("No token to consume")476}477478// Consume the whole line following the current lookahead token.479// Return the text of the line tail.480fn consume_line(&mut self) -> &'a str {481let rest = self.lex.rest_of_line();482self.consume();483rest484}485486// Get the current lookahead token, after making sure there is one.487fn token(&mut self) -> Option<Token<'a>> {488while self.lookahead.is_none() {489match self.lex.next() {490Some(Ok(LocatedToken { token, location })) => {491match token {492Token::Comment(text) => {493if self.gathering_comments {494self.gathered_comments.push(text);495}496}497_ => self.lookahead = Some(token),498}499self.loc = location;500}501Some(Err(LocatedError { error, location })) => {502self.lex_error = Some(error);503self.loc = location;504break;505}506None => break,507}508}509self.lookahead510}511512// Enable gathering of all comments encountered.513fn start_gathering_comments(&mut self) {514debug_assert!(!self.gathering_comments);515self.gathering_comments = true;516debug_assert!(self.gathered_comments.is_empty());517}518519// Claim the comments gathered up to the current position for the520// given entity.521fn claim_gathered_comments<E: Into<AnyEntity>>(&mut self, entity: E) {522debug_assert!(self.gathering_comments);523let entity = entity.into();524self.comments.extend(525self.gathered_comments526.drain(..)527.map(|text| Comment { entity, text }),528);529self.gathering_comments = false;530}531532// Get the comments collected so far, clearing out the internal list.533fn take_comments(&mut self) -> Vec<Comment<'a>> {534debug_assert!(!self.gathering_comments);535mem::replace(&mut self.comments, Vec::new())536}537538// Match and consume a token without payload.539fn match_token(&mut self, want: Token<'a>, err_msg: &str) -> ParseResult<Token<'a>> {540if self.token() == Some(want) {541Ok(self.consume())542} else {543err!(self.loc, err_msg)544}545}546547// If the next token is a `want`, consume it, otherwise do nothing.548fn optional(&mut self, want: Token<'a>) -> bool {549if self.token() == Some(want) {550self.consume();551true552} else {553false554}555}556557// Match and consume a specific identifier string.558// Used for pseudo-keywords like "stack_slot" that only appear in certain contexts.559fn match_identifier(&mut self, want: &'static str, err_msg: &str) -> ParseResult<Token<'a>> {560if self.token() == Some(Token::Identifier(want)) {561Ok(self.consume())562} else {563err!(self.loc, err_msg)564}565}566567// Match and consume a type.568fn match_type(&mut self, err_msg: &str) -> ParseResult<Type> {569if let Some(Token::Type(t)) = self.token() {570self.consume();571Ok(t)572} else {573err!(self.loc, err_msg)574}575}576577// Match and consume a stack slot reference.578fn match_ss(&mut self, err_msg: &str) -> ParseResult<StackSlot> {579if let Some(Token::StackSlot(ss)) = self.token() {580self.consume();581if let Some(ss) = StackSlot::with_number(ss) {582return Ok(ss);583}584}585err!(self.loc, err_msg)586}587588// Match and consume a dynamic stack slot reference.589fn match_dss(&mut self, err_msg: &str) -> ParseResult<DynamicStackSlot> {590if let Some(Token::DynamicStackSlot(ss)) = self.token() {591self.consume();592if let Some(ss) = DynamicStackSlot::with_number(ss) {593return Ok(ss);594}595}596err!(self.loc, err_msg)597}598599// Match and consume a dynamic type reference.600fn match_dt(&mut self, err_msg: &str) -> ParseResult<DynamicType> {601if let Some(Token::DynamicType(dt)) = self.token() {602self.consume();603if let Some(dt) = DynamicType::with_number(dt) {604return Ok(dt);605}606}607err!(self.loc, err_msg)608}609610// Extract Type from DynamicType611fn concrete_from_dt(&mut self, dt: DynamicType, ctx: &mut Context) -> Option<Type> {612ctx.function.get_concrete_dynamic_ty(dt)613}614615// Match and consume a global value reference.616fn match_gv(&mut self, err_msg: &str) -> ParseResult<GlobalValue> {617if let Some(Token::GlobalValue(gv)) = self.token() {618self.consume();619if let Some(gv) = GlobalValue::with_number(gv) {620return Ok(gv);621}622}623err!(self.loc, err_msg)624}625626// Match and consume a function reference.627fn match_fn(&mut self, err_msg: &str) -> ParseResult<FuncRef> {628if let Some(Token::FuncRef(fnref)) = self.token() {629self.consume();630if let Some(fnref) = FuncRef::with_number(fnref) {631return Ok(fnref);632}633}634err!(self.loc, err_msg)635}636637// Match and consume a signature reference.638fn match_sig(&mut self, err_msg: &str) -> ParseResult<SigRef> {639if let Some(Token::SigRef(sigref)) = self.token() {640self.consume();641if let Some(sigref) = SigRef::with_number(sigref) {642return Ok(sigref);643}644}645err!(self.loc, err_msg)646}647648// Match and consume a memory-type reference.649fn match_mt(&mut self, err_msg: &str) -> ParseResult<MemoryType> {650if let Some(Token::MemoryType(mt)) = self.token() {651self.consume();652if let Some(mt) = MemoryType::with_number(mt) {653return Ok(mt);654}655}656err!(self.loc, err_msg)657}658659// Match and consume a constant reference.660fn match_constant(&mut self) -> ParseResult<Constant> {661if let Some(Token::Constant(c)) = self.token() {662self.consume();663if let Some(c) = Constant::with_number(c) {664return Ok(c);665}666}667err!(self.loc, "expected constant number: const«n»")668}669670// Match and consume a stack limit token671fn match_stack_limit(&mut self) -> ParseResult<()> {672if let Some(Token::Identifier("stack_limit")) = self.token() {673self.consume();674return Ok(());675}676err!(self.loc, "expected identifier: stack_limit")677}678679// Match and consume a block reference.680fn match_block(&mut self, err_msg: &str) -> ParseResult<Block> {681if let Some(Token::Block(block)) = self.token() {682self.consume();683Ok(block)684} else {685err!(self.loc, err_msg)686}687}688689// Match and consume a value reference.690fn match_value(&mut self, err_msg: &str) -> ParseResult<Value> {691if let Some(Token::Value(v)) = self.token() {692self.consume();693Ok(v)694} else {695err!(self.loc, err_msg)696}697}698699fn error(&self, message: &str) -> ParseError {700ParseError {701location: self.loc,702message: message.to_string(),703is_warning: false,704}705}706707// Match and consume an Imm64 immediate.708fn match_imm64(&mut self, err_msg: &str) -> ParseResult<Imm64> {709if let Some(Token::Integer(text)) = self.token() {710self.consume();711// Lexer just gives us raw text that looks like an integer.712// Parse it as an Imm64 to check for overflow and other issues.713text.parse().map_err(|e| self.error(e))714} else {715err!(self.loc, err_msg)716}717}718719// Match and consume a hexadeximal immediate720fn match_hexadecimal_constant(&mut self, err_msg: &str) -> ParseResult<ConstantData> {721if let Some(Token::Integer(text)) = self.token() {722self.consume();723text.parse().map_err(|e| {724self.error(&format!(725"expected hexadecimal immediate, failed to parse: {e}"726))727})728} else {729err!(self.loc, err_msg)730}731}732733// Match and consume either a hexadecimal Uimm128 immediate (e.g. 0x000102...) or its literal734// list form (e.g. [0 1 2...]). For convenience, since uimm128 values are stored in the735// `ConstantPool`, this returns `ConstantData`.736fn match_uimm128(&mut self, controlling_type: Type) -> ParseResult<ConstantData> {737let expected_size = controlling_type.bytes() as usize;738let constant_data = if self.optional(Token::LBracket) {739// parse using a list of values, e.g. vconst.i32x4 [0 1 2 3]740let uimm128 = self.parse_literals_to_constant_data(controlling_type)?;741self.match_token(Token::RBracket, "expected a terminating right bracket")?;742uimm128743} else {744// parse using a hexadecimal value, e.g. 0x000102...745let uimm128 =746self.match_hexadecimal_constant("expected an immediate hexadecimal operand")?;747uimm128.expand_to(expected_size)748};749750if constant_data.len() == expected_size {751Ok(constant_data)752} else {753Err(self.error(&format!(754"expected parsed constant to have {expected_size} bytes"755)))756}757}758759// Match and consume a Uimm64 immediate.760fn match_uimm64(&mut self, err_msg: &str) -> ParseResult<Uimm64> {761if let Some(Token::Integer(text)) = self.token() {762self.consume();763// Lexer just gives us raw text that looks like an integer.764// Parse it as an Uimm64 to check for overflow and other issues.765text.parse()766.map_err(|_| self.error("expected u64 decimal immediate"))767} else {768err!(self.loc, err_msg)769}770}771772// Match and consume a Uimm32 immediate.773fn match_uimm32(&mut self, err_msg: &str) -> ParseResult<Uimm32> {774if let Some(Token::Integer(text)) = self.token() {775self.consume();776// Lexer just gives us raw text that looks like an integer.777// Parse it as an Uimm32 to check for overflow and other issues.778text.parse().map_err(|e| self.error(e))779} else {780err!(self.loc, err_msg)781}782}783784// Match and consume a u8 immediate.785// This is used for lane numbers in SIMD vectors.786fn match_uimm8(&mut self, err_msg: &str) -> ParseResult<u8> {787if let Some(Token::Integer(text)) = self.token() {788self.consume();789// Lexer just gives us raw text that looks like an integer.790if let Some(num) = text.strip_prefix("0x") {791// Parse it as a u8 in hexadecimal form.792u8::from_str_radix(num, 16)793.map_err(|_| self.error("unable to parse u8 as a hexadecimal immediate"))794} else {795// Parse it as a u8 to check for overflow and other issues.796text.parse()797.map_err(|_| self.error("expected u8 decimal immediate"))798}799} else {800err!(self.loc, err_msg)801}802}803804// Match and consume an i8 immediate.805fn match_imm8(&mut self, err_msg: &str) -> ParseResult<i8> {806match_imm!(i8, u8, self, err_msg)807}808809// Match and consume a signed 16-bit immediate.810fn match_imm16(&mut self, err_msg: &str) -> ParseResult<i16> {811match_imm!(i16, u16, self, err_msg)812}813814// Match and consume an i32 immediate.815// This is used for stack argument byte offsets.816fn match_imm32(&mut self, err_msg: &str) -> ParseResult<i32> {817match_imm!(i32, u32, self, err_msg)818}819820// Match and consume an i128 immediate.821fn match_imm128(&mut self, err_msg: &str) -> ParseResult<i128> {822match_imm!(i128, u128, self, err_msg)823}824825// Match and consume an optional offset32 immediate.826//827// Note that this will match an empty string as an empty offset, and that if an offset is828// present, it must contain a sign.829fn optional_offset32(&mut self) -> ParseResult<Offset32> {830if let Some(Token::Integer(text)) = self.token() {831if text.starts_with('+') || text.starts_with('-') {832self.consume();833// Lexer just gives us raw text that looks like an integer.834// Parse it as an `Offset32` to check for overflow and other issues.835return text.parse().map_err(|e| self.error(e));836}837}838// An offset32 operand can be absent.839Ok(Offset32::new(0))840}841842// Match and consume an optional offset32 immediate.843//844// Note that this will match an empty string as an empty offset, and that if an offset is845// present, it must contain a sign.846fn optional_offset_imm64(&mut self) -> ParseResult<Imm64> {847if let Some(Token::Integer(text)) = self.token() {848if text.starts_with('+') || text.starts_with('-') {849self.consume();850// Lexer just gives us raw text that looks like an integer.851// Parse it as an `Offset32` to check for overflow and other issues.852return text.parse().map_err(|e| self.error(e));853}854}855// If no explicit offset is present, the offset is 0.856Ok(Imm64::new(0))857}858859// Match and consume an Ieee16 immediate.860fn match_ieee16(&mut self, err_msg: &str) -> ParseResult<Ieee16> {861if let Some(Token::Float(text)) = self.token() {862self.consume();863// Lexer just gives us raw text that looks like a float.864// Parse it as an Ieee16 to check for the right number of digits and other issues.865text.parse().map_err(|e| self.error(e))866} else {867err!(self.loc, err_msg)868}869}870871// Match and consume an Ieee32 immediate.872fn match_ieee32(&mut self, err_msg: &str) -> ParseResult<Ieee32> {873if let Some(Token::Float(text)) = self.token() {874self.consume();875// Lexer just gives us raw text that looks like a float.876// Parse it as an Ieee32 to check for the right number of digits and other issues.877text.parse().map_err(|e| self.error(e))878} else {879err!(self.loc, err_msg)880}881}882883// Match and consume an Ieee64 immediate.884fn match_ieee64(&mut self, err_msg: &str) -> ParseResult<Ieee64> {885if let Some(Token::Float(text)) = self.token() {886self.consume();887// Lexer just gives us raw text that looks like a float.888// Parse it as an Ieee64 to check for the right number of digits and other issues.889text.parse().map_err(|e| self.error(e))890} else {891err!(self.loc, err_msg)892}893}894895// Match and consume an Ieee128 immediate.896fn match_ieee128(&mut self, err_msg: &str) -> ParseResult<Ieee128> {897if let Some(Token::Float(text)) = self.token() {898self.consume();899// Lexer just gives us raw text that looks like a float.900// Parse it as an Ieee128 to check for the right number of digits and other issues.901text.parse().map_err(|e| self.error(e))902} else {903err!(self.loc, err_msg)904}905}906907// Match and consume an enumerated immediate, like one of the condition codes.908fn match_enum<T: FromStr>(&mut self, err_msg: &str) -> ParseResult<T> {909if let Some(Token::Identifier(text)) = self.token() {910self.consume();911text.parse().map_err(|_| self.error(err_msg))912} else {913err!(self.loc, err_msg)914}915}916917// Match and a consume a possibly empty sequence of memory operation flags.918fn optional_memflags(&mut self) -> ParseResult<MemFlags> {919let mut flags = MemFlags::new();920while let Some(Token::Identifier(text)) = self.token() {921match flags.set_by_name(text) {922Ok(true) => {923self.consume();924}925Ok(false) => break,926Err(msg) => return err!(self.loc, msg),927}928}929Ok(flags)930}931932// Match and consume an identifier.933fn match_any_identifier(&mut self, err_msg: &str) -> ParseResult<&'a str> {934if let Some(Token::Identifier(text)) = self.token() {935self.consume();936Ok(text)937} else {938err!(self.loc, err_msg)939}940}941942/// Parse an optional source location.943///944/// Return an optional source location if no real location is present.945fn optional_srcloc(&mut self) -> ParseResult<ir::SourceLoc> {946if let Some(Token::SourceLoc(text)) = self.token() {947match u32::from_str_radix(text, 16) {948Ok(num) => {949self.consume();950Ok(ir::SourceLoc::new(num))951}952Err(_) => return err!(self.loc, "invalid source location: {}", text),953}954} else {955Ok(Default::default())956}957}958959/// Parse an optional list of debug tags.960fn optional_debug_tags(&mut self) -> ParseResult<Vec<DebugTag>> {961if self.optional(Token::LAngle) {962let mut tags = vec![];963while !self.optional(Token::RAngle) {964match self.token() {965Some(Token::Integer(_)) => {966let value: u32 = self.match_uimm32("expected a u32 value")?.into();967tags.push(DebugTag::User(value));968}969Some(Token::StackSlot(slot)) => {970self.consume();971tags.push(DebugTag::StackSlot(StackSlot::from_u32(slot)));972}973_ => {974return err!(975self.loc,976"expected integer user value or stack slot in debug tags"977);978}979}980if !self.optional(Token::Comma) {981self.match_token(Token::RAngle, "expected `,` or `>`")?;982break;983}984}985Ok(tags)986} else {987Ok(vec![])988}989}990991/// Parse a list of literals (i.e. integers, floats, booleans); e.g. `0 1 2 3`, usually as992/// part of something like `vconst.i32x4 [0 1 2 3]`.993fn parse_literals_to_constant_data(&mut self, ty: Type) -> ParseResult<ConstantData> {994macro_rules! consume {995( $ty:ident, $match_fn:expr ) => {{996assert!($ty.is_vector());997let mut data = ConstantData::default();998for _ in 0..$ty.lane_count() {999data = data.append($match_fn);1000}1001data1002}};1003}10041005if !ty.is_vector() && !ty.is_dynamic_vector() {1006err!(self.loc, "Expected a controlling vector type, not {}", ty)1007} else {1008let constant_data = match ty.lane_type() {1009I8 => consume!(ty, self.match_imm8("Expected an 8-bit integer")?),1010I16 => consume!(ty, self.match_imm16("Expected a 16-bit integer")?),1011I32 => consume!(ty, self.match_imm32("Expected a 32-bit integer")?),1012I64 => consume!(ty, self.match_imm64("Expected a 64-bit integer")?),1013F32 => consume!(ty, self.match_ieee32("Expected a 32-bit float")?),1014F64 => consume!(ty, self.match_ieee64("Expected a 64-bit float")?),1015_ => return err!(self.loc, "Expected a type of: float, int, bool"),1016};1017Ok(constant_data)1018}1019}10201021/// Parse a list of test command passes specified in command line.1022pub fn parse_cmdline_passes(&mut self, passes: &'a [String]) -> Vec<TestCommand<'a>> {1023let mut list = Vec::new();1024for pass in passes {1025list.push(TestCommand::new(pass));1026}1027list1028}10291030/// Parse a list of test commands.1031pub fn parse_test_commands(&mut self) -> Vec<TestCommand<'a>> {1032let mut list = Vec::new();1033while self.token() == Some(Token::Identifier("test")) {1034list.push(TestCommand::new(self.consume_line()));1035}1036list1037}10381039/// Parse a target spec.1040///1041/// Accept the target from the command line for pass command.1042///1043fn parse_cmdline_target(&mut self, target_pass: Option<&str>) -> ParseResult<isaspec::IsaSpec> {1044// Were there any `target` commands specified?1045let mut specified_target = false;10461047let mut targets = Vec::new();1048let flag_builder = settings::builder();10491050if let Some(targ) = target_pass {1051let loc = self.loc;1052let triple = match Triple::from_str(targ) {1053Ok(triple) => triple,1054Err(err) => return err!(loc, err),1055};1056let isa_builder = match isa::lookup(triple) {1057Err(isa::LookupError::SupportDisabled) => {1058return err!(loc, "support disabled target '{}'", targ);1059}1060Err(isa::LookupError::Unsupported) => {1061return warn!(loc, "unsupported target '{}'", targ);1062}1063Ok(b) => b,1064};1065specified_target = true;10661067// Construct a trait object with the aggregate settings.1068targets.push(1069isa_builder1070.finish(settings::Flags::new(flag_builder.clone()))1071.map_err(|e| ParseError {1072location: loc,1073message: format!("invalid ISA flags for '{targ}': {e:?}"),1074is_warning: false,1075})?,1076);1077}10781079if !specified_target {1080// No `target` commands.1081Ok(isaspec::IsaSpec::None(settings::Flags::new(flag_builder)))1082} else {1083Ok(isaspec::IsaSpec::Some(targets))1084}1085}10861087/// Parse a list of target specs.1088///1089/// Accept a mix of `target` and `set` command lines. The `set` commands are cumulative.1090///1091fn parse_target_specs(&mut self, options: &ParseOptions) -> ParseResult<isaspec::IsaSpec> {1092// Were there any `target` commands?1093let mut seen_target = false;1094// Location of last `set` command since the last `target`.1095let mut last_set_loc = None;10961097let mut targets = Vec::new();1098let mut flag_builder = settings::builder();10991100let bool_to_str = |val: bool| {1101if val { "true" } else { "false" }1102};11031104// default to enabling cfg info1105flag_builder1106.set(1107"machine_code_cfg_info",1108bool_to_str(options.machine_code_cfg_info),1109)1110.expect("machine_code_cfg_info option should be present");11111112flag_builder1113.set("unwind_info", bool_to_str(options.unwind_info))1114.expect("unwind_info option should be present");11151116while let Some(Token::Identifier(command)) = self.token() {1117match command {1118"set" => {1119last_set_loc = Some(self.loc);1120isaspec::parse_options(1121self.consume_line().trim().split_whitespace(),1122&mut flag_builder,1123self.loc,1124)1125.map_err(|err| ParseError::from(err))?;1126}1127"target" => {1128let loc = self.loc;1129// Grab the whole line so the lexer won't go looking for tokens on the1130// following lines.1131let mut words = self.consume_line().trim().split_whitespace().peekable();1132// Look for `target foo`.1133let target_name = match words.next() {1134Some(w) => w,1135None => return err!(loc, "expected target triple"),1136};1137let triple = match Triple::from_str(target_name) {1138Ok(triple) => triple,1139Err(err) => return err!(loc, err),1140};1141let mut isa_builder = match isa::lookup(triple) {1142Err(isa::LookupError::SupportDisabled) => {1143continue;1144}1145Err(isa::LookupError::Unsupported) => {1146return warn!(loc, "unsupported target '{}'", target_name);1147}1148Ok(b) => b,1149};1150last_set_loc = None;1151seen_target = true;1152// Apply the target-specific settings to `isa_builder`.1153isaspec::parse_options(words, &mut isa_builder, self.loc)?;11541155// Construct a trait object with the aggregate settings.1156targets.push(1157isa_builder1158.finish(settings::Flags::new(flag_builder.clone()))1159.map_err(|e| ParseError {1160location: loc,1161message: format!("invalid ISA flags for '{target_name}': {e:?}"),1162is_warning: false,1163})?,1164);1165}1166_ => break,1167}1168}11691170if !seen_target {1171// No `target` commands, but we allow for `set` commands.1172Ok(isaspec::IsaSpec::None(settings::Flags::new(flag_builder)))1173} else if let Some(loc) = last_set_loc {1174err!(1175loc,1176"dangling 'set' command after ISA specification has no effect."1177)1178} else {1179Ok(isaspec::IsaSpec::Some(targets))1180}1181}11821183/// Parse a list of expected features that Cranelift should be compiled with, or without.1184pub fn parse_cranelift_features(&mut self) -> ParseResult<Vec<Feature<'a>>> {1185let mut list = Vec::new();1186while self.token() == Some(Token::Identifier("feature")) {1187self.consume();1188let has = !self.optional(Token::Bang);1189match (self.token(), has) {1190(Some(Token::String(flag)), true) => list.push(Feature::With(flag)),1191(Some(Token::String(flag)), false) => list.push(Feature::Without(flag)),1192(tok, _) => {1193return err!(1194self.loc,1195format!("Expected feature flag string, got {:?}", tok)1196);1197}1198}1199self.consume();1200}1201Ok(list)1202}12031204/// Parse a list of function definitions.1205///1206/// This is the top-level parse function matching the whole contents of a file.1207pub fn parse_function_list(&mut self) -> ParseResult<Vec<(Function, Details<'a>)>> {1208let mut list = Vec::new();1209while self.token().is_some() {1210list.push(self.parse_function()?);1211}1212if let Some(err) = self.lex_error {1213return match err {1214LexError::InvalidChar => err!(self.loc, "invalid character"),1215};1216}1217Ok(list)1218}12191220// Parse a whole function definition.1221//1222// function ::= * "function" name signature "{" preamble function-body "}"1223//1224fn parse_function(&mut self) -> ParseResult<(Function, Details<'a>)> {1225// Begin gathering comments.1226// Make sure we don't include any comments before the `function` keyword.1227self.token();1228debug_assert!(self.comments.is_empty());1229self.start_gathering_comments();12301231self.match_identifier("function", "expected 'function'")?;12321233let location = self.loc;12341235// function ::= "function" * name signature "{" preamble function-body "}"1236let name = self.parse_user_func_name()?;12371238// function ::= "function" name * signature "{" preamble function-body "}"1239let sig = self.parse_signature()?;12401241let mut ctx = Context::new(Function::with_name_signature(name, sig));12421243// function ::= "function" name signature * "{" preamble function-body "}"1244self.match_token(Token::LBrace, "expected '{' before function body")?;12451246self.token();1247self.claim_gathered_comments(AnyEntity::Function);12481249// function ::= "function" name signature "{" * preamble function-body "}"1250self.parse_preamble(&mut ctx)?;1251// function ::= "function" name signature "{" preamble * function-body "}"1252self.parse_function_body(&mut ctx)?;1253// function ::= "function" name signature "{" preamble function-body * "}"1254self.match_token(Token::RBrace, "expected '}' after function body")?;12551256// Collect any comments following the end of the function, then stop gathering comments.1257self.start_gathering_comments();1258self.token();1259self.claim_gathered_comments(AnyEntity::Function);12601261// Claim all the declared user-defined function names.1262for (user_func_ref, user_external_name) in1263std::mem::take(&mut self.predeclared_external_names)1264{1265let actual_ref = ctx1266.function1267.declare_imported_user_function(user_external_name);1268assert_eq!(user_func_ref, actual_ref);1269}12701271let details = Details {1272location,1273comments: self.take_comments(),1274map: ctx.map,1275};12761277Ok((ctx.function, details))1278}12791280// Parse a user-defined function name1281//1282// For example, in a function decl, the parser would be in this state:1283//1284// function ::= "function" * name signature { ... }1285//1286fn parse_user_func_name(&mut self) -> ParseResult<UserFuncName> {1287match self.token() {1288Some(Token::Name(s)) => {1289self.consume();1290Ok(UserFuncName::testcase(s))1291}1292Some(Token::UserRef(namespace)) => {1293self.consume();1294match self.token() {1295Some(Token::Colon) => {1296self.consume();1297match self.token() {1298Some(Token::Integer(index_str)) => {1299self.consume();1300let index: u32 =1301u32::from_str_radix(index_str, 10).map_err(|_| {1302self.error("the integer given overflows the u32 type")1303})?;1304Ok(UserFuncName::user(namespace, index))1305}1306_ => err!(self.loc, "expected integer"),1307}1308}1309_ => {1310err!(self.loc, "expected user function name in the form uX:Y")1311}1312}1313}1314_ => err!(self.loc, "expected external name"),1315}1316}13171318// Parse an external name.1319//1320// For example, in a function reference decl, the parser would be in this state:1321//1322// fn0 = * name signature1323//1324fn parse_external_name(&mut self) -> ParseResult<ExternalName> {1325match self.token() {1326Some(Token::Name(s)) => {1327self.consume();1328s.parse()1329.map_err(|_| self.error("invalid test case or libcall name"))1330}13311332Some(Token::UserNameRef(name_ref)) => {1333self.consume();1334Ok(ExternalName::user(UserExternalNameRef::new(1335name_ref as usize,1336)))1337}13381339Some(Token::UserRef(namespace)) => {1340self.consume();1341if let Some(Token::Colon) = self.token() {1342self.consume();1343match self.token() {1344Some(Token::Integer(index_str)) => {1345let index: u32 = u32::from_str_radix(index_str, 10).map_err(|_| {1346self.error("the integer given overflows the u32 type")1347})?;1348self.consume();13491350// Deduplicate the reference (O(n), but should be fine for tests),1351// to follow `FunctionParameters::declare_imported_user_function`,1352// otherwise this will cause ref mismatches when asserted below.1353let name_ref = self1354.predeclared_external_names1355.iter()1356.find_map(|(reff, name)| {1357if name.index == index && name.namespace == namespace {1358Some(reff)1359} else {1360None1361}1362})1363.unwrap_or_else(|| {1364self.predeclared_external_names1365.push(ir::UserExternalName { namespace, index })1366});13671368Ok(ExternalName::user(name_ref))1369}1370_ => err!(self.loc, "expected integer"),1371}1372} else {1373err!(self.loc, "expected colon")1374}1375}13761377_ => err!(self.loc, "expected external name"),1378}1379}13801381// Parse a function signature.1382//1383// signature ::= * "(" [paramlist] ")" ["->" retlist] [callconv]1384//1385fn parse_signature(&mut self) -> ParseResult<Signature> {1386// Calling convention defaults to `fast`, but can be changed.1387let mut sig = Signature::new(self.default_calling_convention);13881389self.match_token(Token::LPar, "expected function signature: ( args... )")?;1390// signature ::= "(" * [abi-param-list] ")" ["->" retlist] [callconv]1391if self.token() != Some(Token::RPar) {1392sig.params = self.parse_abi_param_list()?;1393}1394self.match_token(Token::RPar, "expected ')' after function arguments")?;1395if self.optional(Token::Arrow) {1396sig.returns = self.parse_abi_param_list()?;1397}13981399// The calling convention is optional.1400match self.token() {1401Some(Token::Identifier(text)) => match text.parse() {1402Ok(cc) => {1403self.consume();1404sig.call_conv = cc;1405}1406_ => return err!(self.loc, "unknown calling convention: {}", text),1407},1408_ => {}1409}14101411Ok(sig)1412}14131414// Parse list of function parameter / return value types.1415//1416// paramlist ::= * param { "," param }1417//1418fn parse_abi_param_list(&mut self) -> ParseResult<Vec<AbiParam>> {1419let mut list = Vec::new();14201421// abi-param-list ::= * abi-param { "," abi-param }1422list.push(self.parse_abi_param()?);14231424// abi-param-list ::= abi-param * { "," abi-param }1425while self.optional(Token::Comma) {1426// abi-param-list ::= abi-param { "," * abi-param }1427list.push(self.parse_abi_param()?);1428}14291430Ok(list)1431}14321433// Parse a single argument type with flags.1434fn parse_abi_param(&mut self) -> ParseResult<AbiParam> {1435// abi-param ::= * type { flag }1436let mut arg = AbiParam::new(self.match_type("expected parameter type")?);14371438// abi-param ::= type * { flag }1439while let Some(Token::Identifier(s)) = self.token() {1440match s {1441"uext" => arg.extension = ArgumentExtension::Uext,1442"sext" => arg.extension = ArgumentExtension::Sext,1443"sarg" => {1444self.consume();1445self.match_token(Token::LPar, "expected '(' to begin sarg size")?;1446let size = self.match_uimm32("expected byte-size in sarg decl")?;1447self.match_token(Token::RPar, "expected ')' to end sarg size")?;1448arg.purpose = ArgumentPurpose::StructArgument(size.into());1449continue;1450}1451_ => {1452if let Ok(purpose) = s.parse() {1453arg.purpose = purpose;1454} else {1455break;1456}1457}1458}1459self.consume();1460}14611462Ok(arg)1463}14641465// Parse the function preamble.1466//1467// preamble ::= * { preamble-decl }1468// preamble-decl ::= * stack-slot-decl1469// * function-decl1470// * signature-decl1471// * jump-table-decl1472// * stack-limit-decl1473//1474// The parsed decls are added to `ctx` rather than returned.1475fn parse_preamble(&mut self, ctx: &mut Context) -> ParseResult<()> {1476loop {1477match self.token() {1478Some(Token::StackSlot(..)) => {1479self.start_gathering_comments();1480let loc = self.loc;1481self.parse_stack_slot_decl()1482.and_then(|(ss, dat)| ctx.add_ss(ss, dat, loc))1483}1484Some(Token::DynamicStackSlot(..)) => {1485self.start_gathering_comments();1486let loc = self.loc;1487self.parse_dynamic_stack_slot_decl()1488.and_then(|(dss, dat)| ctx.add_dss(dss, dat, loc))1489}1490Some(Token::DynamicType(..)) => {1491self.start_gathering_comments();1492let loc = self.loc;1493self.parse_dynamic_type_decl()1494.and_then(|(dt, dat)| ctx.add_dt(dt, dat, loc))1495}1496Some(Token::GlobalValue(..)) => {1497self.start_gathering_comments();1498self.parse_global_value_decl()1499.and_then(|(gv, dat, maybe_fact)| ctx.add_gv(gv, dat, maybe_fact, self.loc))1500}1501Some(Token::MemoryType(..)) => {1502self.start_gathering_comments();1503self.parse_memory_type_decl()1504.and_then(|(mt, dat)| ctx.add_mt(mt, dat, self.loc))1505}1506Some(Token::SigRef(..)) => {1507self.start_gathering_comments();1508self.parse_signature_decl().and_then(|(sig, dat)| {1509ctx.add_sig(sig, dat, self.loc, self.default_calling_convention)1510})1511}1512Some(Token::FuncRef(..)) => {1513self.start_gathering_comments();1514self.parse_function_decl(ctx)1515.and_then(|(fn_, dat)| ctx.add_fn(fn_, dat, self.loc))1516}1517Some(Token::Constant(..)) => {1518self.start_gathering_comments();1519self.parse_constant_decl()1520.and_then(|(c, v)| ctx.add_constant(c, v, self.loc))1521}1522Some(Token::Identifier("stack_limit")) => {1523self.start_gathering_comments();1524self.parse_stack_limit_decl()1525.and_then(|gv| ctx.add_stack_limit(gv, self.loc))1526}1527// More to come..1528_ => return Ok(()),1529}?;1530}1531}15321533// Parse a stack slot decl.1534//1535// stack-slot-decl ::= * StackSlot(ss) "=" stack-slot-kind Bytes {"," stack-slot-flag}1536// stack-slot-kind ::= "explicit_slot"1537// | "spill_slot"1538// | "incoming_arg"1539// | "outgoing_arg"1540// stack-slot-flag ::= "align" "=" Bytes | "key" "=" uimm641541fn parse_stack_slot_decl(&mut self) -> ParseResult<(StackSlot, StackSlotData)> {1542let ss = self.match_ss("expected stack slot number: ss«n»")?;1543self.match_token(Token::Equal, "expected '=' in stack slot declaration")?;1544let kind = self.match_enum("expected stack slot kind")?;15451546// stack-slot-decl ::= StackSlot(ss) "=" stack-slot-kind * Bytes {"," stack-slot-flag}1547let bytes: i64 = self1548.match_imm64("expected byte-size in stack_slot decl")?1549.into();1550if bytes < 0 {1551return err!(self.loc, "negative stack slot size");1552}1553if bytes > i64::from(u32::MAX) {1554return err!(self.loc, "stack slot too large");1555}15561557let mut align = 1;1558let mut key = None;15591560while self.token() == Some(Token::Comma) {1561self.consume();1562match self.token() {1563Some(Token::Identifier("align")) => {1564self.consume();1565self.match_token(Token::Equal, "expected `=` after flag")?;1566let align64: i64 = self1567.match_imm64("expected alignment-size after `align` flag")?1568.into();1569align = u32::try_from(align64)1570.map_err(|_| self.error("alignment must be a 32-bit unsigned integer"))?;1571}1572Some(Token::Identifier("key")) => {1573self.consume();1574self.match_token(Token::Equal, "expected `=` after flag")?;1575let value = self.match_uimm64("expected `u64` value for `key` flag")?;1576key = Some(StackSlotKey::new(value.into()));1577}1578_ => {1579return Err(self.error("invalid flag for stack slot"));1580}1581}1582}15831584if !align.is_power_of_two() {1585return err!(self.loc, "stack slot alignment is not a power of two");1586}1587let align_shift = u8::try_from(align.ilog2()).unwrap(); // Always succeeds: range 0..=31.15881589let data = match key {1590Some(key) => StackSlotData::new_with_key(kind, bytes as u32, align_shift, key),1591None => StackSlotData::new(kind, bytes as u32, align_shift),1592};15931594// Collect any trailing comments.1595self.token();1596self.claim_gathered_comments(ss);15971598// TBD: stack-slot-decl ::= StackSlot(ss) "=" stack-slot-kind Bytes * {"," stack-slot-flag}1599Ok((ss, data))1600}16011602fn parse_dynamic_stack_slot_decl(1603&mut self,1604) -> ParseResult<(DynamicStackSlot, DynamicStackSlotData)> {1605let dss = self.match_dss("expected stack slot number: dss«n»")?;1606self.match_token(Token::Equal, "expected '=' in stack slot declaration")?;1607let kind = self.match_enum("expected stack slot kind")?;1608let dt = self.match_dt("expected dynamic type")?;1609let data = DynamicStackSlotData::new(kind, dt);1610// Collect any trailing comments.1611self.token();1612self.claim_gathered_comments(dss);16131614// TBD: stack-slot-decl ::= StackSlot(ss) "=" stack-slot-kind Bytes * {"," stack-slot-flag}1615Ok((dss, data))1616}16171618fn parse_dynamic_type_decl(&mut self) -> ParseResult<(DynamicType, DynamicTypeData)> {1619let dt = self.match_dt("expected dynamic type number: dt«n»")?;1620self.match_token(Token::Equal, "expected '=' in stack slot declaration")?;1621let vector_base_ty = self.match_type("expected base type")?;1622assert!(vector_base_ty.is_vector(), "expected vector type");1623self.match_token(1624Token::Multiply,1625"expected '*' followed by a dynamic scale value",1626)?;1627let dyn_scale = self.match_gv("expected dynamic scale global value")?;1628let data = DynamicTypeData::new(vector_base_ty, dyn_scale);1629// Collect any trailing comments.1630self.token();1631self.claim_gathered_comments(dt);1632Ok((dt, data))1633}16341635// Parse a global value decl.1636//1637// global-val-decl ::= * GlobalValue(gv) [ "!" fact ] "=" global-val-desc1638// global-val-desc ::= "vmctx"1639// | "load" "." type "notrap" "aligned" GlobalValue(base) [offset]1640// | "iadd_imm" "(" GlobalValue(base) ")" imm641641// | "symbol" ["colocated"] name + imm641642// | "dyn_scale_target_const" "." type1643//1644fn parse_global_value_decl(1645&mut self,1646) -> ParseResult<(GlobalValue, GlobalValueData, Option<Fact>)> {1647let gv = self.match_gv("expected global value number: gv«n»")?;16481649let fact = if self.token() == Some(Token::Bang) {1650self.consume();1651Some(self.parse_fact()?)1652} else {1653None1654};16551656self.match_token(Token::Equal, "expected '=' in global value declaration")?;16571658let data = match self.match_any_identifier("expected global value kind")? {1659"vmctx" => GlobalValueData::VMContext,1660"load" => {1661self.match_token(1662Token::Dot,1663"expected '.' followed by type in load global value decl",1664)?;1665let global_type = self.match_type("expected load type")?;1666let flags = self.optional_memflags()?;1667let base = self.match_gv("expected global value: gv«n»")?;1668let offset = self.optional_offset32()?;16691670if !(flags.notrap() && flags.aligned()) {1671return err!(self.loc, "global-value load must be notrap and aligned");1672}1673GlobalValueData::Load {1674base,1675offset,1676global_type,1677flags,1678}1679}1680"iadd_imm" => {1681self.match_token(1682Token::Dot,1683"expected '.' followed by type in iadd_imm global value decl",1684)?;1685let global_type = self.match_type("expected iadd type")?;1686let base = self.match_gv("expected global value: gv«n»")?;1687self.match_token(1688Token::Comma,1689"expected ',' followed by rhs in iadd_imm global value decl",1690)?;1691let offset = self.match_imm64("expected iadd_imm immediate")?;1692GlobalValueData::IAddImm {1693base,1694offset,1695global_type,1696}1697}1698"symbol" => {1699let colocated = self.optional(Token::Identifier("colocated"));1700let tls = self.optional(Token::Identifier("tls"));1701let name = self.parse_external_name()?;1702let offset = self.optional_offset_imm64()?;1703GlobalValueData::Symbol {1704name,1705offset,1706colocated,1707tls,1708}1709}1710"dyn_scale_target_const" => {1711self.match_token(1712Token::Dot,1713"expected '.' followed by type in dynamic scale global value decl",1714)?;1715let vector_type = self.match_type("expected load type")?;1716assert!(vector_type.is_vector(), "Expected vector type");1717GlobalValueData::DynScaleTargetConst { vector_type }1718}1719other => return err!(self.loc, "Unknown global value kind '{}'", other),1720};17211722// Collect any trailing comments.1723self.token();1724self.claim_gathered_comments(gv);17251726Ok((gv, data, fact))1727}17281729// Parse one field definition in a memory-type struct decl.1730//1731// memory-type-field ::= offset ":" type ["readonly"] [ "!" fact ]1732// offset ::= uimm641733fn parse_memory_type_field(&mut self) -> ParseResult<MemoryTypeField> {1734let offset: u64 = self1735.match_uimm64(1736"expected u64 constant value for field offset in struct memory-type declaration",1737)?1738.into();1739self.match_token(1740Token::Colon,1741"expected colon after field offset in struct memory-type declaration",1742)?;1743let ty = self.match_type("expected type for field in struct memory-type declaration")?;1744let readonly = if self.token() == Some(Token::Identifier("readonly")) {1745self.consume();1746true1747} else {1748false1749};1750let fact = if self.token() == Some(Token::Bang) {1751self.consume();1752let fact = self.parse_fact()?;1753Some(fact)1754} else {1755None1756};1757Ok(MemoryTypeField {1758offset,1759ty,1760readonly,1761fact,1762})1763}17641765// Parse a memory-type decl.1766//1767// memory-type-decl ::= MemoryType(mt) "=" memory-type-desc1768// memory-type-desc ::= "struct" size "{" memory-type-field,* "}"1769// | "memory" size1770// | "dynamic_memory" GlobalValue "+" offset1771// | "empty"1772// size ::= uimm641773// offset ::= uimm641774fn parse_memory_type_decl(&mut self) -> ParseResult<(MemoryType, MemoryTypeData)> {1775let mt = self.match_mt("expected memory type number: mt«n»")?;1776self.match_token(Token::Equal, "expected '=' in memory type declaration")?;17771778let data = match self.token() {1779Some(Token::Identifier("struct")) => {1780self.consume();1781let size: u64 = self.match_uimm64("expected u64 constant value for struct size in struct memory-type declaration")?.into();1782self.match_token(Token::LBrace, "expected opening brace to start struct fields in struct memory-type declaration")?;1783let mut fields = vec![];1784while self.token() != Some(Token::RBrace) {1785let field = self.parse_memory_type_field()?;1786fields.push(field);1787if self.token() == Some(Token::Comma) {1788self.consume();1789} else {1790break;1791}1792}1793self.match_token(1794Token::RBrace,1795"expected closing brace after struct fields in struct memory-type declaration",1796)?;1797MemoryTypeData::Struct { size, fields }1798}1799Some(Token::Identifier("memory")) => {1800self.consume();1801let size: u64 = self.match_uimm64("expected u64 constant value for size in static-memory memory-type declaration")?.into();1802MemoryTypeData::Memory { size }1803}1804Some(Token::Identifier("dynamic_memory")) => {1805self.consume();1806let gv = self.match_gv(1807"expected a global value for `dynamic_memory` memory-type declaration",1808)?;1809self.match_token(1810Token::Plus,1811"expected `+` after global value in `dynamic_memory` memory-type declaration",1812)?;1813let size: u64 = self.match_uimm64("expected u64 constant value for size offset in `dynamic_memory` memory-type declaration")?.into();1814MemoryTypeData::DynamicMemory { gv, size }1815}1816Some(Token::Identifier("empty")) => {1817self.consume();1818MemoryTypeData::Empty1819}1820other => {1821return err!(1822self.loc,1823"Unknown memory type declaration kind '{:?}'",1824other1825);1826}1827};18281829// Collect any trailing comments.1830self.token();1831self.claim_gathered_comments(mt);18321833Ok((mt, data))1834}18351836// Parse a signature decl.1837//1838// signature-decl ::= SigRef(sigref) "=" signature1839//1840fn parse_signature_decl(&mut self) -> ParseResult<(SigRef, Signature)> {1841let sig = self.match_sig("expected signature number: sig«n»")?;1842self.match_token(Token::Equal, "expected '=' in signature decl")?;1843let data = self.parse_signature()?;18441845// Collect any trailing comments.1846self.token();1847self.claim_gathered_comments(sig);18481849Ok((sig, data))1850}18511852// Parse a function decl.1853//1854// Two variants:1855//1856// function-decl ::= FuncRef(fnref) "=" ["colocated"] ["patchable"] name function-decl-sig1857// function-decl-sig ::= SigRef(sig) | signature1858//1859// The first variant allocates a new signature reference. The second references an existing1860// signature which must be declared first.1861//1862fn parse_function_decl(&mut self, ctx: &mut Context) -> ParseResult<(FuncRef, ExtFuncData)> {1863let fn_ = self.match_fn("expected function number: fn«n»")?;1864self.match_token(Token::Equal, "expected '=' in function decl")?;18651866let loc = self.loc;18671868// function-decl ::= FuncRef(fnref) "=" * ["colocated"] ["patchable"] name function-decl-sig1869let colocated = self.optional(Token::Identifier("colocated"));1870// function-decl ::= FuncRef(fnref) "=" ["colocated"] * ["patchable"] name function-decl-sig1871let patchable = self.optional(Token::Identifier("patchable"));18721873// function-decl ::= FuncRef(fnref) "=" ["colocated"] ["patchable"] * name function-decl-sig1874let name = self.parse_external_name()?;18751876// function-decl ::= FuncRef(fnref) "=" ["colocated"] ["patchable"] name * function-decl-sig1877let data = match self.token() {1878Some(Token::LPar) => {1879// function-decl ::= FuncRef(fnref) "=" ["colocated"] ["patchable"] name * signature1880let sig = self.parse_signature()?;1881let sigref = ctx.function.import_signature(sig);1882ctx.map1883.def_entity(sigref.into(), loc)1884.expect("duplicate SigRef entities created");1885ExtFuncData {1886name,1887signature: sigref,1888colocated,1889patchable,1890}1891}1892Some(Token::SigRef(sig_src)) => {1893let sig = match SigRef::with_number(sig_src) {1894None => {1895return err!(self.loc, "attempted to use invalid signature ss{}", sig_src);1896}1897Some(sig) => sig,1898};1899ctx.check_sig(sig, self.loc)?;1900self.consume();1901ExtFuncData {1902name,1903signature: sig,1904colocated,1905patchable,1906}1907}1908_ => return err!(self.loc, "expected 'function' or sig«n» in function decl"),1909};19101911// Collect any trailing comments.1912self.token();1913self.claim_gathered_comments(fn_);19141915Ok((fn_, data))1916}19171918// Parse a jump table literal.1919//1920// jump-table-lit ::= "[" block(args) {"," block(args) } "]"1921// | "[]"1922fn parse_jump_table(1923&mut self,1924ctx: &mut Context,1925def: ir::BlockCall,1926) -> ParseResult<ir::JumpTable> {1927self.match_token(Token::LBracket, "expected '[' before jump table contents")?;19281929let mut data = Vec::new();19301931match self.token() {1932Some(Token::Block(dest)) => {1933self.consume();1934let args = self.parse_opt_block_call_args()?;1935data.push(ctx.function.dfg.block_call(dest, &args));19361937loop {1938match self.token() {1939Some(Token::Comma) => {1940self.consume();1941if let Some(Token::Block(dest)) = self.token() {1942self.consume();1943let args = self.parse_opt_block_call_args()?;1944data.push(ctx.function.dfg.block_call(dest, &args));1945} else {1946return err!(self.loc, "expected jump_table entry");1947}1948}1949Some(Token::RBracket) => break,1950_ => return err!(self.loc, "expected ']' after jump table contents"),1951}1952}1953}1954Some(Token::RBracket) => (),1955_ => return err!(self.loc, "expected jump_table entry"),1956}19571958self.consume();19591960Ok(ctx1961.function1962.dfg1963.jump_tables1964.push(JumpTableData::new(def, &data)))1965}19661967// Parse an exception-table decl.1968//1969// exception-table ::= * SigRef(sig) "," BlockCall "," "[" (exception-table-entry ( "," exception-table-entry )*)? "]"1970// exception-table-entry ::= ExceptionTag(tag) ":" BlockCall1971// | "default" ":" BlockCall1972// | "context" value1973fn parse_exception_table(&mut self, ctx: &mut Context) -> ParseResult<ir::ExceptionTable> {1974let sig = self.match_sig("expected signature of called function")?;1975self.match_token(Token::Comma, "expected comma after signature argument")?;19761977let mut handlers = vec![];19781979let block_num = self.match_block("expected branch destination block")?;1980let args = self.parse_opt_block_call_args()?;1981let normal_return = ctx.function.dfg.block_call(block_num, &args);19821983self.match_token(1984Token::Comma,1985"expected comma after normal-return destination",1986)?;19871988self.match_token(1989Token::LBracket,1990"expected an open-bracket for exception table list",1991)?;1992loop {1993match self.token() {1994Some(Token::RBracket) => {1995break;1996}1997Some(Token::ExceptionTag(tag)) => {1998self.consume();1999self.match_token(Token::Colon, "expected ':' after exception tag")?;2000let tag = ir::ExceptionTag::from_u32(tag);2001let block_num = self.match_block("expected branch destination block")?;2002let args = self.parse_opt_block_call_args()?;2003let block_call = ctx.function.dfg.block_call(block_num, &args);2004handlers.push(ir::ExceptionTableItem::Tag(tag, block_call));2005}2006Some(Token::Identifier("default")) => {2007self.consume();2008self.match_token(Token::Colon, "expected ':' after 'default'")?;2009let block_num = self.match_block("expected branch destination block")?;2010let args = self.parse_opt_block_call_args()?;2011let block_call = ctx.function.dfg.block_call(block_num, &args);2012handlers.push(ir::ExceptionTableItem::Default(block_call));2013}2014Some(Token::Identifier("context")) => {2015self.consume();2016let val = self.match_value("expected value for exception-handler context")?;2017handlers.push(ir::ExceptionTableItem::Context(val));2018}2019_ => return err!(self.loc, "invalid token"),2020}20212022if let Some(Token::Comma) = self.token() {2023self.consume();2024} else {2025break;2026}2027}2028self.match_token(Token::RBracket, "expected closing bracket")?;20292030Ok(ctx2031.function2032.dfg2033.exception_tables2034.push(ir::ExceptionTableData::new(sig, normal_return, handlers)))2035}20362037// Parse a constant decl.2038//2039// constant-decl ::= * Constant(c) "=" ty? "[" literal {"," literal} "]"2040fn parse_constant_decl(&mut self) -> ParseResult<(Constant, ConstantData)> {2041let name = self.match_constant()?;2042self.match_token(Token::Equal, "expected '=' in constant decl")?;2043let data = if let Some(Token::Type(_)) = self.token() {2044let ty = self.match_type("expected type of constant")?;2045self.match_uimm128(ty)2046} else {2047self.match_hexadecimal_constant("expected an immediate hexadecimal operand")2048}?;20492050// Collect any trailing comments.2051self.token();2052self.claim_gathered_comments(name);20532054Ok((name, data))2055}20562057// Parse a stack limit decl2058//2059// stack-limit-decl ::= * StackLimit "=" GlobalValue(gv)2060fn parse_stack_limit_decl(&mut self) -> ParseResult<GlobalValue> {2061self.match_stack_limit()?;2062self.match_token(Token::Equal, "expected '=' in stack limit decl")?;2063let limit = match self.token() {2064Some(Token::GlobalValue(base_num)) => match GlobalValue::with_number(base_num) {2065Some(gv) => gv,2066None => return err!(self.loc, "invalid global value number for stack limit"),2067},2068_ => return err!(self.loc, "expected global value"),2069};2070self.consume();20712072// Collect any trailing comments.2073self.token();2074self.claim_gathered_comments(AnyEntity::StackLimit);20752076Ok(limit)2077}20782079// Parse a function body, add contents to `ctx`.2080//2081// function-body ::= * { extended-basic-block }2082//2083fn parse_function_body(&mut self, ctx: &mut Context) -> ParseResult<()> {2084while self.token() != Some(Token::RBrace) {2085self.parse_basic_block(ctx)?;2086}20872088// Now that we've seen all defined values in the function, ensure that2089// all references refer to a definition.2090for block in &ctx.function.layout {2091for inst in ctx.function.layout.block_insts(block) {2092for value in ctx.function.dfg.inst_values(inst) {2093if !ctx.map.contains_value(value) {2094return err!(2095ctx.map.location(AnyEntity::Inst(inst)).unwrap(),2096"undefined operand value {}",2097value2098);2099}2100}2101}2102}21032104for alias in &ctx.aliases {2105if !ctx.function.dfg.set_alias_type_for_parser(*alias) {2106let loc = ctx.map.location(AnyEntity::Value(*alias)).unwrap();2107return err!(loc, "alias cycle involving {}", alias);2108}2109}21102111Ok(())2112}21132114// Parse a basic block, add contents to `ctx`.2115//2116// extended-basic-block ::= * block-header { instruction }2117// block-header ::= Block(block) [block-params] [block-flags] ":"2118// block-flags ::= [Cold]2119//2120fn parse_basic_block(&mut self, ctx: &mut Context) -> ParseResult<()> {2121// Collect comments for the next block.2122self.start_gathering_comments();21232124let block_num = self.match_block("expected block header")?;2125let block = ctx.add_block(block_num, self.loc)?;21262127if block_num.as_u32() >= MAX_BLOCKS_IN_A_FUNCTION {2128return Err(self.error("too many blocks"));2129}21302131if self.token() == Some(Token::LPar) {2132self.parse_block_params(ctx, block)?;2133}21342135if self.optional(Token::Cold) {2136ctx.set_cold_block(block);2137}21382139self.match_token(Token::Colon, "expected ':' after block parameters")?;21402141// Collect any trailing comments.2142self.token();2143self.claim_gathered_comments(block);21442145// extended-basic-block ::= block-header * { instruction }2146while match self.token() {2147Some(Token::Value(_))2148| Some(Token::Identifier(_))2149| Some(Token::LBracket)2150| Some(Token::SourceLoc(_))2151| Some(Token::LAngle) => true,2152_ => false,2153} {2154let srcloc = self.optional_srcloc()?;21552156let debug_tags = self.optional_debug_tags()?;21572158// We need to parse instruction results here because they are shared2159// between the parsing of value aliases and the parsing of instructions.2160//2161// inst-results ::= Value(v) { "," Value(v) }2162let results = self.parse_inst_results(ctx)?;21632164for result in &results {2165while ctx.function.dfg.num_values() <= result.index() {2166ctx.function.dfg.make_invalid_value_for_parser();2167}2168}21692170match self.token() {2171Some(Token::Arrow) => {2172self.consume();2173self.parse_value_alias(&results, ctx)?;2174}2175Some(Token::Equal) => {2176self.consume();2177self.parse_instruction(&results, srcloc, debug_tags, ctx, block)?;2178}2179_ if !results.is_empty() => return err!(self.loc, "expected -> or ="),2180_ => self.parse_instruction(&results, srcloc, debug_tags, ctx, block)?,2181}2182}21832184Ok(())2185}21862187// Parse parenthesized list of block parameters.2188//2189// block-params ::= * "(" ( block-param { "," block-param } )? ")"2190fn parse_block_params(&mut self, ctx: &mut Context, block: Block) -> ParseResult<()> {2191// block-params ::= * "(" ( block-param { "," block-param } )? ")"2192self.match_token(Token::LPar, "expected '(' before block parameters")?;21932194// block-params ::= "(" * ")"2195if self.token() == Some(Token::RPar) {2196self.consume();2197return Ok(());2198}21992200// block-params ::= "(" * block-param { "," block-param } ")"2201self.parse_block_param(ctx, block)?;22022203// block-params ::= "(" block-param * { "," block-param } ")"2204while self.optional(Token::Comma) {2205// block-params ::= "(" block-param { "," * block-param } ")"2206self.parse_block_param(ctx, block)?;2207}22082209// block-params ::= "(" block-param { "," block-param } * ")"2210self.match_token(Token::RPar, "expected ')' after block parameters")?;22112212Ok(())2213}22142215// Parse a single block parameter declaration, and append it to `block`.2216//2217// block-param ::= * Value(v) [ "!" fact ] ":" Type(t) arg-loc?2218// arg-loc ::= "[" value-location "]"2219//2220fn parse_block_param(&mut self, ctx: &mut Context, block: Block) -> ParseResult<()> {2221// block-param ::= * Value(v) [ "!" fact ] ":" Type(t) arg-loc?2222let v = self.match_value("block argument must be a value")?;2223let v_location = self.loc;2224// block-param ::= Value(v) * [ "!" fact ] ":" Type(t) arg-loc?2225let fact = if self.token() == Some(Token::Bang) {2226self.consume();2227// block-param ::= Value(v) [ "!" * fact ] ":" Type(t) arg-loc?2228Some(self.parse_fact()?)2229} else {2230None2231};2232self.match_token(Token::Colon, "expected ':' after block argument")?;2233// block-param ::= Value(v) [ "!" fact ] ":" * Type(t) arg-loc?22342235while ctx.function.dfg.num_values() <= v.index() {2236ctx.function.dfg.make_invalid_value_for_parser();2237}22382239let t = self.match_type("expected block argument type")?;2240// Allocate the block argument.2241ctx.function.dfg.append_block_param_for_parser(block, t, v);2242ctx.map.def_value(v, v_location)?;2243ctx.function.dfg.facts[v] = fact;22442245Ok(())2246}22472248// Parse a "fact" for proof-carrying code, attached to a value.2249//2250// fact ::= "range" "(" bit-width "," min-value "," max-value ")"2251// | "dynamic_range" "(" bit-width "," expr "," expr ")"2252// | "mem" "(" memory-type "," mt-offset "," mt-offset [ "," "nullable" ] ")"2253// | "dynamic_mem" "(" memory-type "," expr "," expr [ "," "nullable" ] ")"2254// | "conflict"2255// bit-width ::= uimm642256// min-value ::= uimm642257// max-value ::= uimm642258// valid-range ::= uimm642259// mt-offset ::= uimm642260fn parse_fact(&mut self) -> ParseResult<Fact> {2261match self.token() {2262Some(Token::Identifier("range")) => {2263self.consume();2264self.match_token(Token::LPar, "`range` fact needs an opening `(`")?;2265let bit_width: u64 = self2266.match_uimm64("expected a bit-width value for `range` fact")?2267.into();2268self.match_token(Token::Comma, "expected a comma")?;2269let min: u64 = self2270.match_uimm64("expected a min value for `range` fact")?2271.into();2272self.match_token(Token::Comma, "expected a comma")?;2273let max: u64 = self2274.match_uimm64("expected a max value for `range` fact")?2275.into();2276self.match_token(Token::RPar, "`range` fact needs a closing `)`")?;2277let bit_width_max = match bit_width {2278x if x > 64 => {2279return Err(self.error("bitwidth must be <= 64 bits on a `range` fact"));2280}228164 => u64::MAX,2282x => (1u64 << x) - 1,2283};2284if min > max {2285return Err(self.error(2286"min value must be less than or equal to max value on a `range` fact",2287));2288}2289if max > bit_width_max {2290return Err(2291self.error("max value is out of range for bitwidth on a `range` fact")2292);2293}2294Ok(Fact::Range {2295bit_width: u16::try_from(bit_width).unwrap(),2296min,2297max,2298})2299}2300Some(Token::Identifier("dynamic_range")) => {2301self.consume();2302self.match_token(Token::LPar, "`dynamic_range` fact needs an opening `(`")?;2303let bit_width: u64 = self2304.match_uimm64("expected a bit-width value for `dynamic_range` fact")?2305.into();2306self.match_token(Token::Comma, "expected a comma")?;2307let min = self.parse_expr()?;2308self.match_token(Token::Comma, "expected a comma")?;2309let max = self.parse_expr()?;2310self.match_token(Token::RPar, "`dynamic_range` fact needs a closing `)`")?;2311Ok(Fact::DynamicRange {2312bit_width: u16::try_from(bit_width).unwrap(),2313min,2314max,2315})2316}2317Some(Token::Identifier("mem")) => {2318self.consume();2319self.match_token(Token::LPar, "expected a `(`")?;2320let ty = self.match_mt("expected a memory type for `mem` fact")?;2321self.match_token(2322Token::Comma,2323"expected a comma after memory type in `mem` fact",2324)?;2325let min_offset: u64 = self2326.match_uimm64("expected a uimm64 minimum pointer offset for `mem` fact")?2327.into();2328self.match_token(Token::Comma, "expected a comma after offset in `mem` fact")?;2329let max_offset: u64 = self2330.match_uimm64("expected a uimm64 maximum pointer offset for `mem` fact")?2331.into();2332let nullable = if self.token() == Some(Token::Comma) {2333self.consume();2334self.match_token(2335Token::Identifier("nullable"),2336"expected `nullable` in last optional field of `dynamic_mem`",2337)?;2338true2339} else {2340false2341};2342self.match_token(Token::RPar, "expected a `)`")?;2343Ok(Fact::Mem {2344ty,2345min_offset,2346max_offset,2347nullable,2348})2349}2350Some(Token::Identifier("dynamic_mem")) => {2351self.consume();2352self.match_token(Token::LPar, "expected a `(`")?;2353let ty = self.match_mt("expected a memory type for `dynamic_mem` fact")?;2354self.match_token(2355Token::Comma,2356"expected a comma after memory type in `dynamic_mem` fact",2357)?;2358let min = self.parse_expr()?;2359self.match_token(2360Token::Comma,2361"expected a comma after offset in `dynamic_mem` fact",2362)?;2363let max = self.parse_expr()?;2364let nullable = if self.token() == Some(Token::Comma) {2365self.consume();2366self.match_token(2367Token::Identifier("nullable"),2368"expected `nullable` in last optional field of `dynamic_mem`",2369)?;2370true2371} else {2372false2373};2374self.match_token(Token::RPar, "expected a `)`")?;2375Ok(Fact::DynamicMem {2376ty,2377min,2378max,2379nullable,2380})2381}2382Some(Token::Identifier("def")) => {2383self.consume();2384self.match_token(Token::LPar, "expected a `(`")?;2385let value = self.match_value("expected a value number in `def` fact")?;2386self.match_token(Token::RPar, "expected a `)`")?;2387Ok(Fact::Def { value })2388}2389Some(Token::Identifier("compare")) => {2390self.consume();2391self.match_token(Token::LPar, "expected a `(`")?;2392let kind = self.match_enum("expected intcc condition code in `compare` fact")?;2393self.match_token(2394Token::Comma,2395"expected comma in `compare` fact after condition code",2396)?;2397let lhs = self.parse_expr()?;2398self.match_token(Token::Comma, "expected comma in `compare` fact after LHS")?;2399let rhs = self.parse_expr()?;2400self.match_token(Token::RPar, "expected a `)`")?;2401Ok(Fact::Compare { kind, lhs, rhs })2402}2403Some(Token::Identifier("conflict")) => {2404self.consume();2405Ok(Fact::Conflict)2406}2407_ => Err(self.error(2408"expected a `range`, 'dynamic_range', `mem`, `dynamic_mem`, `def`, `compare` or `conflict` fact",2409)),2410}2411}24122413// Parse a dynamic expression used in some kinds of PCC facts.2414//2415// expr ::= base-expr2416// | base-expr + uimm64 // but in-range for imm642417// | base-expr - uimm64 // but in-range for imm642418// | imm642419fn parse_expr(&mut self) -> ParseResult<Expr> {2420if let Some(Token::Integer(_)) = self.token() {2421let offset: i64 = self2422.match_imm64("expected imm64 for dynamic expression")?2423.into();2424Ok(Expr {2425base: BaseExpr::None,2426offset,2427})2428} else {2429let base = self.parse_base_expr()?;2430match self.token() {2431Some(Token::Plus) => {2432self.consume();2433let offset: u64 = self2434.match_uimm64(2435"expected uimm64 in imm64 range for offset in dynamic expression",2436)?2437.into();2438let offset: i64 = i64::try_from(offset).map_err(|_| {2439self.error("integer offset in dynamic expression is out of range")2440})?;2441Ok(Expr { base, offset })2442}2443Some(Token::Integer(x)) if x.starts_with("-") => {2444let offset: i64 = self2445.match_imm64("expected an imm64 range for offset in dynamic expression")?2446.into();2447Ok(Expr { base, offset })2448}2449_ => Ok(Expr { base, offset: 0 }),2450}2451}2452}24532454// Parse the base part of a dynamic expression, used in some PCC facts.2455//2456// base-expr ::= GlobalValue(base)2457// | Value(base)2458// | "max"2459// | (epsilon)2460fn parse_base_expr(&mut self) -> ParseResult<BaseExpr> {2461match self.token() {2462Some(Token::Identifier("max")) => {2463self.consume();2464Ok(BaseExpr::Max)2465}2466Some(Token::GlobalValue(..)) => {2467let gv = self.match_gv("expected global value")?;2468Ok(BaseExpr::GlobalValue(gv))2469}2470Some(Token::Value(..)) => {2471let value = self.match_value("expected value")?;2472Ok(BaseExpr::Value(value))2473}2474_ => Ok(BaseExpr::None),2475}2476}24772478// Parse instruction results and return them.2479//2480// inst-results ::= Value(v) { "," Value(v) }2481//2482fn parse_inst_results(&mut self, ctx: &mut Context) -> ParseResult<SmallVec<[Value; 1]>> {2483// Result value numbers.2484let mut results = SmallVec::new();24852486// instruction ::= * [inst-results "="] Opcode(opc) ["." Type] ...2487// inst-results ::= * Value(v) { "," Value(v) }2488if let Some(Token::Value(v)) = self.token() {2489self.consume();24902491results.push(v);24922493let fact = if self.token() == Some(Token::Bang) {2494self.consume();2495// block-param ::= Value(v) [ "!" * fact ] ":" Type(t) arg-loc?2496Some(self.parse_fact()?)2497} else {2498None2499};2500ctx.function.dfg.facts[v] = fact;25012502// inst-results ::= Value(v) * { "," Value(v) }2503while self.optional(Token::Comma) {2504// inst-results ::= Value(v) { "," * Value(v) }2505let v = self.match_value("expected result value")?;2506results.push(v);25072508let fact = if self.token() == Some(Token::Bang) {2509self.consume();2510// block-param ::= Value(v) [ "!" * fact ] ":" Type(t) arg-loc?2511Some(self.parse_fact()?)2512} else {2513None2514};2515ctx.function.dfg.facts[v] = fact;2516}2517}25182519Ok(results)2520}25212522// Parse a value alias, and append it to `block`.2523//2524// value_alias ::= [inst-results] "->" Value(v)2525//2526fn parse_value_alias(&mut self, results: &[Value], ctx: &mut Context) -> ParseResult<()> {2527if results.len() != 1 {2528return err!(self.loc, "wrong number of aliases");2529}2530let result = results[0];2531let dest = self.match_value("expected value alias")?;25322533// Allow duplicate definitions of aliases, as long as they are identical.2534if ctx.map.contains_value(result) {2535if let Some(old) = ctx.function.dfg.value_alias_dest_for_serialization(result) {2536if old != dest {2537return err!(2538self.loc,2539"value {} is already defined as an alias with destination {}",2540result,2541old2542);2543}2544} else {2545return err!(self.loc, "value {} is already defined");2546}2547} else {2548ctx.map.def_value(result, self.loc)?;2549}25502551if !ctx.map.contains_value(dest) {2552return err!(self.loc, "value {} is not yet defined", dest);2553}25542555ctx.function2556.dfg2557.make_value_alias_for_serialization(dest, result);25582559ctx.aliases.push(result);2560Ok(())2561}25622563// Parse an instruction, append it to `block`.2564//2565// instruction ::= [inst-results "="] Opcode(opc) ["." Type] ...2566//2567fn parse_instruction(2568&mut self,2569results: &[Value],2570srcloc: ir::SourceLoc,2571debug_tags: Vec<DebugTag>,2572ctx: &mut Context,2573block: Block,2574) -> ParseResult<()> {2575// Define the result values.2576for val in results {2577ctx.map.def_value(*val, self.loc)?;2578}25792580// Collect comments for the next instruction.2581self.start_gathering_comments();25822583// instruction ::= [inst-results "="] * Opcode(opc) ["." Type] ...2584let opcode = if let Some(Token::Identifier(text)) = self.token() {2585match text.parse() {2586Ok(opc) => opc,2587Err(msg) => return err!(self.loc, "{}: '{}'", msg, text),2588}2589} else {2590return err!(self.loc, "expected instruction opcode");2591};2592let opcode_loc = self.loc;2593self.consume();25942595// Look for a controlling type variable annotation.2596// instruction ::= [inst-results "="] Opcode(opc) * ["." Type] ...2597let explicit_ctrl_type = if self.optional(Token::Dot) {2598if let Some(Token::Type(_t)) = self.token() {2599Some(self.match_type("expected type after 'opcode.'")?)2600} else {2601let dt = self.match_dt("expected dynamic type")?;2602self.concrete_from_dt(dt, ctx)2603}2604} else {2605None2606};26072608// instruction ::= [inst-results "="] Opcode(opc) ["." Type] * ...2609let inst_data = self.parse_inst_operands(ctx, opcode, explicit_ctrl_type)?;26102611let ctrl_typevar = self.infer_typevar(ctx, opcode, explicit_ctrl_type, &inst_data)?;2612let inst = ctx.function.dfg.make_inst(inst_data);26132614// Attach stack map, if present.2615if self.optional(Token::Comma) {2616self.match_token(2617Token::Identifier("stack_map"),2618"expected `stack_map = [...]`",2619)?;2620if !opcode.is_call() || opcode.is_return() {2621return err!(2622self.loc,2623"stack map can only be attached to a (non-tail) call"2624);2625}26262627self.match_token(Token::Equal, "expected `= [...]`")?;2628self.match_token(Token::LBracket, "expected `[...]`")?;2629while !self.optional(Token::RBracket) {2630let ty = self.match_type("expected `<type> @ <slot> + <offset>`")?;2631self.match_token(Token::At, "expected `@ <slot> + <offset>`")?;2632let slot = self.match_ss("expected `<slot> + <offset>`")?;2633let offset: u32 = match self.token() {2634Some(Token::Integer(s)) if s.starts_with('+') => {2635self.match_uimm32("expected a u32 offset")?.into()2636}2637_ => {2638self.match_token(Token::Plus, "expected `+ <offset>`")?;2639self.match_uimm32("expected a u32 offset")?.into()2640}2641};2642ctx.function2643.dfg2644.append_user_stack_map_entry(inst, ir::UserStackMapEntry { ty, slot, offset });2645if !self.optional(Token::Comma) {2646self.match_token(Token::RBracket, "expected `,` or `]`")?;2647break;2648}2649}2650}26512652// We're done parsing the instruction data itself.2653//2654// We still need to check that the number of result values in2655// the source matches the opcode or function call2656// signature. We also need to create values with the right2657// type for all the instruction results.2658let num_results =2659ctx.function2660.dfg2661.make_inst_results_for_parser(inst, ctrl_typevar, results);2662ctx.function.layout.append_inst(inst, block);2663ctx.map2664.def_entity(inst.into(), opcode_loc)2665.expect("duplicate inst references created");26662667if !srcloc.is_default() {2668ctx.function.set_srcloc(inst, srcloc);2669}2670if !debug_tags.is_empty() {2671ctx.function.debug_tags.set(inst, debug_tags);2672}26732674if results.len() != num_results {2675return err!(2676self.loc,2677"instruction produces {} result values, {} given",2678num_results,2679results.len()2680);2681}26822683// Collect any trailing comments.2684self.token();2685self.claim_gathered_comments(inst);26862687Ok(())2688}26892690// Type inference for polymorphic instructions.2691//2692// The controlling type variable can be specified explicitly as 'splat.i32x4 v5', or it can be2693// inferred from `inst_data.typevar_operand` for some opcodes.2694//2695// Returns the controlling typevar for a polymorphic opcode, or `INVALID` for a non-polymorphic2696// opcode.2697fn infer_typevar(2698&self,2699ctx: &Context,2700opcode: Opcode,2701explicit_ctrl_type: Option<Type>,2702inst_data: &InstructionData,2703) -> ParseResult<Type> {2704let constraints = opcode.constraints();2705let ctrl_type = match explicit_ctrl_type {2706Some(t) => t,2707None => {2708if constraints.use_typevar_operand() {2709// This is an opcode that supports type inference, AND there was no2710// explicit type specified. Look up `ctrl_value` to see if it was defined2711// already.2712// TBD: If it is defined in another block, the type should have been2713// specified explicitly. It is unfortunate that the correctness of IR2714// depends on the layout of the blocks.2715let ctrl_src_value = inst_data2716.typevar_operand(&ctx.function.dfg.value_lists)2717.expect("Constraints <-> Format inconsistency");2718if !ctx.map.contains_value(ctrl_src_value) {2719return err!(2720self.loc,2721"type variable required for polymorphic opcode, e.g. '{}.{}'; \2722can't infer from {} which is not yet defined",2723opcode,2724constraints.ctrl_typeset().unwrap().example(),2725ctrl_src_value2726);2727}2728if !ctx.function.dfg.value_is_valid_for_parser(ctrl_src_value) {2729return err!(2730self.loc,2731"type variable required for polymorphic opcode, e.g. '{}.{}'; \2732can't infer from {} which is not yet resolved",2733opcode,2734constraints.ctrl_typeset().unwrap().example(),2735ctrl_src_value2736);2737}2738ctx.function.dfg.value_type(ctrl_src_value)2739} else if constraints.is_polymorphic() {2740// This opcode does not support type inference, so the explicit type2741// variable is required.2742return err!(2743self.loc,2744"type variable required for polymorphic opcode, e.g. '{}.{}'",2745opcode,2746constraints.ctrl_typeset().unwrap().example()2747);2748} else {2749// This is a non-polymorphic opcode. No typevar needed.2750INVALID2751}2752}2753};27542755// Verify that `ctrl_type` is valid for the controlling type variable. We don't want to2756// attempt deriving types from an incorrect basis.2757// This is not a complete type check. The verifier does that.2758if let Some(typeset) = constraints.ctrl_typeset() {2759// This is a polymorphic opcode.2760if !typeset.contains(ctrl_type) {2761return err!(2762self.loc,2763"{} is not a valid typevar for {}",2764ctrl_type,2765opcode2766);2767}2768// Treat it as a syntax error to specify a typevar on a non-polymorphic opcode.2769} else if ctrl_type != INVALID {2770return err!(self.loc, "{} does not take a typevar", opcode);2771}27722773Ok(ctrl_type)2774}27752776// Parse comma-separated value list into a VariableArgs struct.2777//2778// value_list ::= [ value { "," value } ]2779//2780fn parse_value_list(&mut self) -> ParseResult<VariableArgs> {2781let mut args = VariableArgs::new();27822783if let Some(Token::Value(v)) = self.token() {2784args.push(v);2785self.consume();2786} else {2787return Ok(args);2788}27892790while self.optional(Token::Comma) {2791args.push(self.match_value("expected value in argument list")?);2792}27932794Ok(args)2795}27962797/// Parse an optional list of block-call arguments enclosed in2798/// parentheses.2799fn parse_opt_block_call_args(&mut self) -> ParseResult<Vec<BlockArg>> {2800if !self.optional(Token::LPar) {2801return Ok(vec![]);2802}28032804let mut args = vec![];2805while self.token() != Some(Token::RPar) {2806args.push(self.parse_block_call_arg()?);2807if self.token() == Some(Token::Comma) {2808self.consume();2809} else {2810break;2811}2812}28132814self.match_token(Token::RPar, "expected ')' after arguments")?;28152816Ok(args)2817}28182819fn parse_block_call_arg(&mut self) -> ParseResult<BlockArg> {2820match self.token() {2821Some(Token::Value(v)) => {2822self.consume();2823Ok(BlockArg::Value(v))2824}2825Some(Token::TryCallRet(i)) => {2826self.consume();2827Ok(BlockArg::TryCallRet(i))2828}2829Some(Token::TryCallExn(i)) => {2830self.consume();2831Ok(BlockArg::TryCallExn(i))2832}2833tok => Err(self.error(&format!("unexpected token: {tok:?}"))),2834}2835}28362837/// Parse a CLIF run command.2838///2839/// run-command ::= "run" [":" invocation comparison expected]2840/// \ "print" [":" invocation]2841fn parse_run_command(&mut self, sig: &Signature) -> ParseResult<RunCommand> {2842// skip semicolon2843match self.token() {2844Some(Token::Identifier("run")) => {2845self.consume();2846if self.optional(Token::Colon) {2847let invocation = self.parse_run_invocation(sig)?;2848let comparison = self.parse_run_comparison()?;2849let expected = self.parse_run_returns(sig)?;2850Ok(RunCommand::Run(invocation, comparison, expected))2851} else if sig.params.is_empty()2852&& sig.returns.len() == 12853&& sig.returns[0].value_type.is_int()2854{2855// To match the existing run behavior that does not require an explicit2856// invocation, we create an invocation from a function like `() -> i*` and2857// require the result to be non-zero.2858let invocation = Invocation::new("default", vec![]);2859let expected = vec![DataValue::I8(0)];2860let comparison = Comparison::NotEquals;2861Ok(RunCommand::Run(invocation, comparison, expected))2862} else {2863Err(self.error("unable to parse the run command"))2864}2865}2866Some(Token::Identifier("print")) => {2867self.consume();2868if self.optional(Token::Colon) {2869Ok(RunCommand::Print(self.parse_run_invocation(sig)?))2870} else if sig.params.is_empty() {2871// To allow printing of functions like `() -> *`, we create a no-arg invocation.2872let invocation = Invocation::new("default", vec![]);2873Ok(RunCommand::Print(invocation))2874} else {2875Err(self.error("unable to parse the print command"))2876}2877}2878_ => Err(self.error("expected a 'run:' or 'print:' command")),2879}2880}28812882/// Parse the invocation of a CLIF function.2883///2884/// This is different from parsing a CLIF `call`; it is used in parsing run commands like2885/// `run: %fn(42, 4.2) == false`.2886///2887/// invocation ::= name "(" [data-value-list] ")"2888fn parse_run_invocation(&mut self, sig: &Signature) -> ParseResult<Invocation> {2889if let Some(Token::Name(name)) = self.token() {2890self.consume();2891self.match_token(2892Token::LPar,2893"expected invocation parentheses, e.g. %fn(...)",2894)?;28952896let arg_types = sig2897.params2898.iter()2899.map(|abi| abi.value_type)2900.collect::<Vec<_>>();2901let args = self.parse_data_value_list(&arg_types)?;29022903self.match_token(2904Token::RPar,2905"expected invocation parentheses, e.g. %fn(...)",2906)?;2907Ok(Invocation::new(name, args))2908} else {2909Err(self.error("expected a function name, e.g. %my_fn"))2910}2911}29122913/// Parse a comparison operator for run commands.2914///2915/// comparison ::= "==" | "!="2916fn parse_run_comparison(&mut self) -> ParseResult<Comparison> {2917if self.optional(Token::Equal) {2918self.match_token(Token::Equal, "expected another =")?;2919Ok(Comparison::Equals)2920} else if self.optional(Token::Bang) {2921self.match_token(Token::Equal, "expected a =")?;2922Ok(Comparison::NotEquals)2923} else {2924Err(self.error("unable to parse a valid comparison operator"))2925}2926}29272928/// Parse the expected return values of a run invocation.2929///2930/// expected ::= "[" "]"2931/// | data-value2932/// | "[" data-value-list "]"2933fn parse_run_returns(&mut self, sig: &Signature) -> ParseResult<Vec<DataValue>> {2934if sig.returns.len() != 1 {2935self.match_token(Token::LBracket, "expected a left bracket [")?;2936}29372938let returns = self2939.parse_data_value_list(&sig.returns.iter().map(|a| a.value_type).collect::<Vec<_>>())?;29402941if sig.returns.len() != 1 {2942self.match_token(Token::RBracket, "expected a right bracket ]")?;2943}2944Ok(returns)2945}29462947/// Parse a comma-separated list of data values.2948///2949/// data-value-list ::= [data-value {"," data-value-list}]2950fn parse_data_value_list(&mut self, types: &[Type]) -> ParseResult<Vec<DataValue>> {2951let mut values = vec![];2952for ty in types.iter().take(1) {2953values.push(self.parse_data_value(*ty)?);2954}2955for ty in types.iter().skip(1) {2956self.match_token(2957Token::Comma,2958"expected a comma between invocation arguments",2959)?;2960values.push(self.parse_data_value(*ty)?);2961}2962Ok(values)2963}29642965/// Parse a data value; e.g. `42`, `4.2`, `true`.2966///2967/// data-value-list ::= [data-value {"," data-value-list}]2968fn parse_data_value(&mut self, ty: Type) -> ParseResult<DataValue> {2969let dv = match ty {2970I8 => DataValue::from(self.match_imm8("expected a i8")?),2971I16 => DataValue::from(self.match_imm16("expected an i16")?),2972I32 => DataValue::from(self.match_imm32("expected an i32")?),2973I64 => DataValue::from(Into::<i64>::into(self.match_imm64("expected an i64")?)),2974I128 => DataValue::from(self.match_imm128("expected an i128")?),2975F16 => DataValue::from(self.match_ieee16("expected an f16")?),2976F32 => DataValue::from(self.match_ieee32("expected an f32")?),2977F64 => DataValue::from(self.match_ieee64("expected an f64")?),2978F128 => DataValue::from(self.match_ieee128("expected an f128")?),2979_ if (ty.is_vector() || ty.is_dynamic_vector()) => {2980let as_vec = self.match_uimm128(ty)?.into_vec();2981let slice = as_vec.as_slice();2982match slice.len() {298316 => DataValue::V128(slice.try_into().unwrap()),29848 => DataValue::V64(slice.try_into().unwrap()),29854 => DataValue::V32(slice.try_into().unwrap()),29862 => DataValue::V16(slice.try_into().unwrap()),2987_ => {2988return Err(2989self.error("vectors larger than 128 bits are not currently supported")2990);2991}2992}2993}2994_ => return Err(self.error(&format!("don't know how to parse data values of: {ty}"))),2995};2996Ok(dv)2997}29982999// Parse the operands following the instruction opcode.3000// This depends on the format of the opcode.3001fn parse_inst_operands(3002&mut self,3003ctx: &mut Context,3004opcode: Opcode,3005explicit_control_type: Option<Type>,3006) -> ParseResult<InstructionData> {3007let idata = match opcode.format() {3008InstructionFormat::Unary => InstructionData::Unary {3009opcode,3010arg: self.match_value("expected SSA value operand")?,3011},3012InstructionFormat::UnaryImm => {3013let msg = |bits| format!("expected immediate {bits}-bit integer operand");3014let unsigned = match explicit_control_type {3015Some(types::I8) => self.match_imm8(&msg(8))? as u8 as i64,3016Some(types::I16) => self.match_imm16(&msg(16))? as u16 as i64,3017Some(types::I32) => self.match_imm32(&msg(32))? as u32 as i64,3018Some(types::I64) => self.match_imm64(&msg(64))?.bits(),3019_ => {3020return err!(3021self.loc,3022"expected one of the following type: i8, i16, i32 or i64"3023);3024}3025};3026InstructionData::UnaryImm {3027opcode,3028imm: Imm64::new(unsigned),3029}3030}3031InstructionFormat::UnaryIeee16 => InstructionData::UnaryIeee16 {3032opcode,3033imm: self.match_ieee16("expected immediate 16-bit float operand")?,3034},3035InstructionFormat::UnaryIeee32 => InstructionData::UnaryIeee32 {3036opcode,3037imm: self.match_ieee32("expected immediate 32-bit float operand")?,3038},3039InstructionFormat::UnaryIeee64 => InstructionData::UnaryIeee64 {3040opcode,3041imm: self.match_ieee64("expected immediate 64-bit float operand")?,3042},3043InstructionFormat::UnaryConst => {3044let constant_handle = if let Some(Token::Constant(_)) = self.token() {3045// If handed a `const?`, use that.3046let c = self.match_constant()?;3047ctx.check_constant(c, self.loc)?;3048c3049} else if opcode == Opcode::F128const {3050let ieee128 = self.match_ieee128("expected immediate 128-bit float operand")?;3051ctx.function.dfg.constants.insert(ieee128.into())3052} else if let Some(controlling_type) = explicit_control_type {3053// If an explicit control type is present, we expect a sized value and insert3054// it in the constant pool.3055let uimm128 = self.match_uimm128(controlling_type)?;3056ctx.function.dfg.constants.insert(uimm128)3057} else {3058return err!(3059self.loc,3060"Expected either a const entity or a typed value, e.g. inst.i32x4 [...]"3061);3062};3063InstructionData::UnaryConst {3064opcode,3065constant_handle,3066}3067}3068InstructionFormat::UnaryGlobalValue => {3069let gv = self.match_gv("expected global value")?;3070ctx.check_gv(gv, self.loc)?;3071InstructionData::UnaryGlobalValue {3072opcode,3073global_value: gv,3074}3075}3076InstructionFormat::Binary => {3077let lhs = self.match_value("expected SSA value first operand")?;3078self.match_token(Token::Comma, "expected ',' between operands")?;3079let rhs = self.match_value("expected SSA value second operand")?;3080InstructionData::Binary {3081opcode,3082args: [lhs, rhs],3083}3084}3085InstructionFormat::BinaryImm8 => {3086let arg = self.match_value("expected SSA value first operand")?;3087self.match_token(Token::Comma, "expected ',' between operands")?;3088let imm = self.match_uimm8("expected unsigned 8-bit immediate")?;3089InstructionData::BinaryImm8 { opcode, arg, imm }3090}3091InstructionFormat::BinaryImm64 => {3092let lhs = self.match_value("expected SSA value first operand")?;3093self.match_token(Token::Comma, "expected ',' between operands")?;3094let rhs = self.match_imm64("expected immediate integer second operand")?;3095InstructionData::BinaryImm64 {3096opcode,3097arg: lhs,3098imm: rhs,3099}3100}3101InstructionFormat::Ternary => {3102// Names here refer to the `select` instruction.3103// This format is also use by `fma`.3104let ctrl_arg = self.match_value("expected SSA value control operand")?;3105self.match_token(Token::Comma, "expected ',' between operands")?;3106let true_arg = self.match_value("expected SSA value true operand")?;3107self.match_token(Token::Comma, "expected ',' between operands")?;3108let false_arg = self.match_value("expected SSA value false operand")?;3109InstructionData::Ternary {3110opcode,3111args: [ctrl_arg, true_arg, false_arg],3112}3113}3114InstructionFormat::MultiAry => {3115let args = self.parse_value_list()?;3116InstructionData::MultiAry {3117opcode,3118args: args.into_value_list(&[], &mut ctx.function.dfg.value_lists),3119}3120}3121InstructionFormat::NullAry => InstructionData::NullAry { opcode },3122InstructionFormat::Jump => {3123// Parse the destination block number.3124let block_num = self.match_block("expected jump destination block")?;3125let args = self.parse_opt_block_call_args()?;3126let destination = ctx.function.dfg.block_call(block_num, &args);3127InstructionData::Jump {3128opcode,3129destination,3130}3131}3132InstructionFormat::Brif => {3133let arg = self.match_value("expected SSA value control operand")?;3134self.match_token(Token::Comma, "expected ',' between operands")?;3135let block_then = {3136let block_num = self.match_block("expected branch then block")?;3137let args = self.parse_opt_block_call_args()?;3138ctx.function.dfg.block_call(block_num, &args)3139};3140self.match_token(Token::Comma, "expected ',' between operands")?;3141let block_else = {3142let block_num = self.match_block("expected branch else block")?;3143let args = self.parse_opt_block_call_args()?;3144ctx.function.dfg.block_call(block_num, &args)3145};3146InstructionData::Brif {3147opcode,3148arg,3149blocks: [block_then, block_else],3150}3151}3152InstructionFormat::BranchTable => {3153let arg = self.match_value("expected SSA value operand")?;3154self.match_token(Token::Comma, "expected ',' between operands")?;3155let block_num = self.match_block("expected branch destination block")?;3156let args = self.parse_opt_block_call_args()?;3157let destination = ctx.function.dfg.block_call(block_num, &args);3158self.match_token(Token::Comma, "expected ',' between operands")?;3159let table = self.parse_jump_table(ctx, destination)?;3160InstructionData::BranchTable { opcode, arg, table }3161}3162InstructionFormat::TernaryImm8 => {3163let lhs = self.match_value("expected SSA value first operand")?;3164self.match_token(Token::Comma, "expected ',' between operands")?;3165let rhs = self.match_value("expected SSA value last operand")?;3166self.match_token(Token::Comma, "expected ',' between operands")?;3167let imm = self.match_uimm8("expected 8-bit immediate")?;3168InstructionData::TernaryImm8 {3169opcode,3170imm,3171args: [lhs, rhs],3172}3173}3174InstructionFormat::Shuffle => {3175let a = self.match_value("expected SSA value first operand")?;3176self.match_token(Token::Comma, "expected ',' between operands")?;3177let b = self.match_value("expected SSA value second operand")?;3178self.match_token(Token::Comma, "expected ',' between operands")?;3179let uimm128 = self.match_uimm128(I8X16)?;3180let imm = ctx.function.dfg.immediates.push(uimm128);3181InstructionData::Shuffle {3182opcode,3183imm,3184args: [a, b],3185}3186}3187InstructionFormat::IntCompare => {3188let cond = self.match_enum("expected intcc condition code")?;3189let lhs = self.match_value("expected SSA value first operand")?;3190self.match_token(Token::Comma, "expected ',' between operands")?;3191let rhs = self.match_value("expected SSA value second operand")?;3192InstructionData::IntCompare {3193opcode,3194cond,3195args: [lhs, rhs],3196}3197}3198InstructionFormat::IntCompareImm => {3199let cond = self.match_enum("expected intcc condition code")?;3200let lhs = self.match_value("expected SSA value first operand")?;3201self.match_token(Token::Comma, "expected ',' between operands")?;3202let rhs = self.match_imm64("expected immediate second operand")?;3203InstructionData::IntCompareImm {3204opcode,3205cond,3206arg: lhs,3207imm: rhs,3208}3209}3210InstructionFormat::FloatCompare => {3211let cond = self.match_enum("expected floatcc condition code")?;3212let lhs = self.match_value("expected SSA value first operand")?;3213self.match_token(Token::Comma, "expected ',' between operands")?;3214let rhs = self.match_value("expected SSA value second operand")?;3215InstructionData::FloatCompare {3216opcode,3217cond,3218args: [lhs, rhs],3219}3220}3221InstructionFormat::Call => {3222let func_ref = self.match_fn("expected function reference")?;3223ctx.check_fn(func_ref, self.loc)?;3224self.match_token(Token::LPar, "expected '(' before arguments")?;3225let args = self.parse_value_list()?;3226self.match_token(Token::RPar, "expected ')' after arguments")?;3227InstructionData::Call {3228opcode,3229func_ref,3230args: args.into_value_list(&[], &mut ctx.function.dfg.value_lists),3231}3232}3233InstructionFormat::CallIndirect => {3234let sig_ref = self.match_sig("expected signature reference")?;3235ctx.check_sig(sig_ref, self.loc)?;3236self.match_token(Token::Comma, "expected ',' between operands")?;3237let callee = self.match_value("expected SSA value callee operand")?;3238self.match_token(Token::LPar, "expected '(' before arguments")?;3239let args = self.parse_value_list()?;3240self.match_token(Token::RPar, "expected ')' after arguments")?;3241InstructionData::CallIndirect {3242opcode,3243sig_ref,3244args: args.into_value_list(&[callee], &mut ctx.function.dfg.value_lists),3245}3246}3247InstructionFormat::TryCall => {3248let func_ref = self.match_fn("expected function reference")?;3249ctx.check_fn(func_ref, self.loc)?;3250self.match_token(Token::LPar, "expected '(' before arguments")?;3251let args = self.parse_value_list()?;3252self.match_token(Token::RPar, "expected ')' after arguments")?;3253self.match_token(Token::Comma, "expected ',' after argument list")?;3254let exception = self.parse_exception_table(ctx)?;3255InstructionData::TryCall {3256opcode,3257func_ref,3258args: args.into_value_list(&[], &mut ctx.function.dfg.value_lists),3259exception,3260}3261}3262InstructionFormat::TryCallIndirect => {3263let callee = self.match_value("expected SSA value callee operand")?;3264self.match_token(Token::LPar, "expected '(' before arguments")?;3265let args = self.parse_value_list()?;3266self.match_token(Token::RPar, "expected ')' after arguments")?;3267self.match_token(Token::Comma, "expected ',' after argument list")?;3268let exception = self.parse_exception_table(ctx)?;3269InstructionData::TryCallIndirect {3270opcode,3271args: args.into_value_list(&[callee], &mut ctx.function.dfg.value_lists),3272exception,3273}3274}3275InstructionFormat::FuncAddr => {3276let func_ref = self.match_fn("expected function reference")?;3277ctx.check_fn(func_ref, self.loc)?;3278InstructionData::FuncAddr { opcode, func_ref }3279}3280InstructionFormat::StackLoad => {3281let ss = self.match_ss("expected stack slot number: ss«n»")?;3282ctx.check_ss(ss, self.loc)?;3283let offset = self.optional_offset32()?;3284InstructionData::StackLoad {3285opcode,3286stack_slot: ss,3287offset,3288}3289}3290InstructionFormat::StackStore => {3291let arg = self.match_value("expected SSA value operand")?;3292self.match_token(Token::Comma, "expected ',' between operands")?;3293let ss = self.match_ss("expected stack slot number: ss«n»")?;3294ctx.check_ss(ss, self.loc)?;3295let offset = self.optional_offset32()?;3296InstructionData::StackStore {3297opcode,3298arg,3299stack_slot: ss,3300offset,3301}3302}3303InstructionFormat::DynamicStackLoad => {3304let dss = self.match_dss("expected dynamic stack slot number: dss«n»")?;3305ctx.check_dss(dss, self.loc)?;3306InstructionData::DynamicStackLoad {3307opcode,3308dynamic_stack_slot: dss,3309}3310}3311InstructionFormat::DynamicStackStore => {3312let arg = self.match_value("expected SSA value operand")?;3313self.match_token(Token::Comma, "expected ',' between operands")?;3314let dss = self.match_dss("expected dynamic stack slot number: dss«n»")?;3315ctx.check_dss(dss, self.loc)?;3316InstructionData::DynamicStackStore {3317opcode,3318arg,3319dynamic_stack_slot: dss,3320}3321}3322InstructionFormat::Load => {3323let flags = self.optional_memflags()?;3324let addr = self.match_value("expected SSA value address")?;3325let offset = self.optional_offset32()?;3326InstructionData::Load {3327opcode,3328flags,3329arg: addr,3330offset,3331}3332}3333InstructionFormat::Store => {3334let flags = self.optional_memflags()?;3335let arg = self.match_value("expected SSA value operand")?;3336self.match_token(Token::Comma, "expected ',' between operands")?;3337let addr = self.match_value("expected SSA value address")?;3338let offset = self.optional_offset32()?;3339InstructionData::Store {3340opcode,3341flags,3342args: [arg, addr],3343offset,3344}3345}3346InstructionFormat::Trap => {3347let code = self.match_enum("expected trap code")?;3348InstructionData::Trap { opcode, code }3349}3350InstructionFormat::CondTrap => {3351let arg = self.match_value("expected SSA value operand")?;3352self.match_token(Token::Comma, "expected ',' between operands")?;3353let code = self.match_enum("expected trap code")?;3354InstructionData::CondTrap { opcode, arg, code }3355}3356InstructionFormat::AtomicCas => {3357let flags = self.optional_memflags()?;3358let addr = self.match_value("expected SSA value address")?;3359self.match_token(Token::Comma, "expected ',' between operands")?;3360let expected = self.match_value("expected SSA value address")?;3361self.match_token(Token::Comma, "expected ',' between operands")?;3362let replacement = self.match_value("expected SSA value address")?;3363InstructionData::AtomicCas {3364opcode,3365flags,3366args: [addr, expected, replacement],3367}3368}3369InstructionFormat::AtomicRmw => {3370let flags = self.optional_memflags()?;3371let op = self.match_enum("expected AtomicRmwOp")?;3372let addr = self.match_value("expected SSA value address")?;3373self.match_token(Token::Comma, "expected ',' between operands")?;3374let arg2 = self.match_value("expected SSA value address")?;3375InstructionData::AtomicRmw {3376opcode,3377flags,3378op,3379args: [addr, arg2],3380}3381}3382InstructionFormat::LoadNoOffset => {3383let flags = self.optional_memflags()?;3384let addr = self.match_value("expected SSA value address")?;3385InstructionData::LoadNoOffset {3386opcode,3387flags,3388arg: addr,3389}3390}3391InstructionFormat::StoreNoOffset => {3392let flags = self.optional_memflags()?;3393let arg = self.match_value("expected SSA value operand")?;3394self.match_token(Token::Comma, "expected ',' between operands")?;3395let addr = self.match_value("expected SSA value address")?;3396InstructionData::StoreNoOffset {3397opcode,3398flags,3399args: [arg, addr],3400}3401}3402InstructionFormat::IntAddTrap => {3403let a = self.match_value("expected SSA value operand")?;3404self.match_token(Token::Comma, "expected ',' between operands")?;3405let b = self.match_value("expected SSA value operand")?;3406self.match_token(Token::Comma, "expected ',' between operands")?;3407let code = self.match_enum("expected trap code")?;3408InstructionData::IntAddTrap {3409opcode,3410args: [a, b],3411code,3412}3413}3414InstructionFormat::ExceptionHandlerAddress => {3415let block = self.match_block("expected block")?;3416self.match_token(Token::Comma, "expected ',' between operands")?;3417let imm = self.match_imm64("expected immediate handler index")?;3418InstructionData::ExceptionHandlerAddress { opcode, block, imm }3419}3420};3421Ok(idata)3422}3423}34243425#[cfg(test)]3426mod tests {3427use super::*;3428use crate::isaspec::IsaSpec;34293430#[test]3431fn argument_type() {3432let mut p = Parser::new("i32 sext");3433let arg = p.parse_abi_param().unwrap();3434assert_eq!(arg.value_type, types::I32);3435assert_eq!(arg.extension, ArgumentExtension::Sext);3436assert_eq!(arg.purpose, ArgumentPurpose::Normal);3437let ParseError {3438location,3439message,3440is_warning,3441} = p.parse_abi_param().unwrap_err();3442assert_eq!(location.line_number, 1);3443assert_eq!(message, "expected parameter type");3444assert!(!is_warning);3445}34463447#[test]3448fn aliases() {3449let (func, details) = Parser::new(3450"function %qux() system_v {3451block0:3452v4 = iconst.i8 63453v3 -> v43454v1 = iadd_imm v3, 173455}",3456)3457.parse_function()3458.unwrap();3459assert_eq!(func.name.to_string(), "%qux");3460let v4 = details.map.lookup_str("v4").unwrap();3461assert_eq!(v4.to_string(), "v4");3462let v3 = details.map.lookup_str("v3").unwrap();3463assert_eq!(v3.to_string(), "v3");3464match v3 {3465AnyEntity::Value(v3) => {3466let aliased_to = func.dfg.resolve_aliases(v3);3467assert_eq!(aliased_to.to_string(), "v4");3468}3469_ => panic!("expected value: {v3}"),3470}3471}34723473#[test]3474fn signature() {3475let sig = Parser::new("()system_v").parse_signature().unwrap();3476assert_eq!(sig.params.len(), 0);3477assert_eq!(sig.returns.len(), 0);3478assert_eq!(sig.call_conv, CallConv::SystemV);34793480let sig2 =3481Parser::new("(i8 uext, f16, f32, f64, f128, i32 sret) -> i32 sext, f64 system_v")3482.parse_signature()3483.unwrap();3484assert_eq!(3485sig2.to_string(),3486"(i8 uext, f16, f32, f64, f128, i32 sret) -> i32 sext, f64 system_v"3487);3488assert_eq!(sig2.call_conv, CallConv::SystemV);34893490// Old-style signature without a calling convention.3491assert_eq!(3492Parser::new("()").parse_signature().unwrap().to_string(),3493"() fast"3494);3495assert_eq!(3496Parser::new("() notacc")3497.parse_signature()3498.unwrap_err()3499.to_string(),3500"1: unknown calling convention: notacc"3501);35023503// `void` is not recognized as a type by the lexer. It should not appear in files.3504assert_eq!(3505Parser::new("() -> void")3506.parse_signature()3507.unwrap_err()3508.to_string(),3509"1: expected parameter type"3510);3511assert_eq!(3512Parser::new("i8 -> i8")3513.parse_signature()3514.unwrap_err()3515.to_string(),3516"1: expected function signature: ( args... )"3517);3518assert_eq!(3519Parser::new("(i8 -> i8")3520.parse_signature()3521.unwrap_err()3522.to_string(),3523"1: expected ')' after function arguments"3524);3525}35263527#[test]3528fn stack_slot_decl() {3529let (func, _) = Parser::new(3530"function %foo() system_v {3531ss3 = explicit_slot 133532ss1 = explicit_slot 13533}",3534)3535.parse_function()3536.unwrap();3537assert_eq!(func.name.to_string(), "%foo");3538let mut iter = func.sized_stack_slots.keys();3539let _ss0 = iter.next().unwrap();3540let ss1 = iter.next().unwrap();3541assert_eq!(ss1.to_string(), "ss1");3542assert_eq!(3543func.sized_stack_slots[ss1].kind,3544StackSlotKind::ExplicitSlot3545);3546assert_eq!(func.sized_stack_slots[ss1].size, 1);3547let _ss2 = iter.next().unwrap();3548let ss3 = iter.next().unwrap();3549assert_eq!(ss3.to_string(), "ss3");3550assert_eq!(3551func.sized_stack_slots[ss3].kind,3552StackSlotKind::ExplicitSlot3553);3554assert_eq!(func.sized_stack_slots[ss3].size, 13);3555assert_eq!(iter.next(), None);35563557// Catch duplicate definitions.3558assert_eq!(3559Parser::new(3560"function %bar() system_v {3561ss1 = explicit_slot 133562ss1 = explicit_slot 13563}",3564)3565.parse_function()3566.unwrap_err()3567.to_string(),3568"3: duplicate entity: ss1"3569);3570}35713572#[test]3573fn block_header() {3574let (func, _) = Parser::new(3575"function %blocks() system_v {3576block0:3577block4(v3: i32):3578}",3579)3580.parse_function()3581.unwrap();3582assert_eq!(func.name.to_string(), "%blocks");35833584let mut blocks = func.layout.blocks();35853586let block0 = blocks.next().unwrap();3587assert_eq!(func.dfg.block_params(block0), &[]);35883589let block4 = blocks.next().unwrap();3590let block4_args = func.dfg.block_params(block4);3591assert_eq!(block4_args.len(), 1);3592assert_eq!(func.dfg.value_type(block4_args[0]), types::I32);3593}35943595#[test]3596fn duplicate_block() {3597let ParseError {3598location,3599message,3600is_warning,3601} = Parser::new(3602"function %blocks() system_v {3603block0:3604block0:3605return 2",3606)3607.parse_function()3608.unwrap_err();36093610assert_eq!(location.line_number, 3);3611assert_eq!(message, "duplicate entity: block0");3612assert!(!is_warning);3613}36143615#[test]3616fn number_of_blocks() {3617let ParseError {3618location,3619message,3620is_warning,3621} = Parser::new(3622"function %a() {3623block100000:",3624)3625.parse_function()3626.unwrap_err();36273628assert_eq!(location.line_number, 2);3629assert_eq!(message, "too many blocks");3630assert!(!is_warning);3631}36323633#[test]3634fn duplicate_ss() {3635let ParseError {3636location,3637message,3638is_warning,3639} = Parser::new(3640"function %blocks() system_v {3641ss0 = explicit_slot 83642ss0 = explicit_slot 8",3643)3644.parse_function()3645.unwrap_err();36463647assert_eq!(location.line_number, 3);3648assert_eq!(message, "duplicate entity: ss0");3649assert!(!is_warning);3650}36513652#[test]3653fn duplicate_gv() {3654let ParseError {3655location,3656message,3657is_warning,3658} = Parser::new(3659"function %blocks() system_v {3660gv0 = vmctx3661gv0 = vmctx",3662)3663.parse_function()3664.unwrap_err();36653666assert_eq!(location.line_number, 3);3667assert_eq!(message, "duplicate entity: gv0");3668assert!(!is_warning);3669}36703671#[test]3672fn duplicate_sig() {3673let ParseError {3674location,3675message,3676is_warning,3677} = Parser::new(3678"function %blocks() system_v {3679sig0 = ()3680sig0 = ()",3681)3682.parse_function()3683.unwrap_err();36843685assert_eq!(location.line_number, 3);3686assert_eq!(message, "duplicate entity: sig0");3687assert!(!is_warning);3688}36893690#[test]3691fn duplicate_fn() {3692let ParseError {3693location,3694message,3695is_warning,3696} = Parser::new(3697"function %blocks() system_v {3698sig0 = ()3699fn0 = %foo sig03700fn0 = %foo sig0",3701)3702.parse_function()3703.unwrap_err();37043705assert_eq!(location.line_number, 4);3706assert_eq!(message, "duplicate entity: fn0");3707assert!(!is_warning);3708}37093710#[test]3711fn comments() {3712let (func, Details { comments, .. }) = Parser::new(3713"; before3714function %comment() system_v { ; decl3715ss10 = explicit_slot 13 ; stackslot.3716; Still stackslot.3717block0: ; Basic block3718trap user42; Instruction3719} ; Trailing.3720; More trailing.",3721)3722.parse_function()3723.unwrap();3724assert_eq!(func.name.to_string(), "%comment");3725assert_eq!(comments.len(), 7); // no 'before' comment.3726assert_eq!(3727comments[0],3728Comment {3729entity: AnyEntity::Function,3730text: "; decl",3731}3732);3733assert_eq!(comments[1].entity.to_string(), "ss10");3734assert_eq!(comments[2].entity.to_string(), "ss10");3735assert_eq!(comments[2].text, "; Still stackslot.");3736assert_eq!(comments[3].entity.to_string(), "block0");3737assert_eq!(comments[3].text, "; Basic block");37383739assert_eq!(comments[4].entity.to_string(), "inst0");3740assert_eq!(comments[4].text, "; Instruction");37413742assert_eq!(comments[5].entity, AnyEntity::Function);3743assert_eq!(comments[6].entity, AnyEntity::Function);3744}37453746#[test]3747fn test_file() {3748let tf = parse_test(3749r#"; before3750test cfg option=53751test verify3752set unwind_info=false3753feature "foo"3754feature !"bar"3755; still preamble3756function %comment() system_v {}"#,3757ParseOptions::default(),3758)3759.unwrap();3760assert_eq!(tf.commands.len(), 2);3761assert_eq!(tf.commands[0].command, "cfg");3762assert_eq!(tf.commands[1].command, "verify");3763match tf.isa_spec {3764IsaSpec::None(s) => {3765assert!(s.enable_verifier());3766assert!(!s.unwind_info());3767}3768_ => panic!("unexpected ISAs"),3769}3770assert_eq!(tf.features[0], Feature::With(&"foo"));3771assert_eq!(tf.features[1], Feature::Without(&"bar"));3772assert_eq!(tf.preamble_comments.len(), 2);3773assert_eq!(tf.preamble_comments[0].text, "; before");3774assert_eq!(tf.preamble_comments[1].text, "; still preamble");3775assert_eq!(tf.functions.len(), 1);3776assert_eq!(tf.functions[0].0.name.to_string(), "%comment");3777}37783779#[test]3780fn isa_spec() {3781assert!(3782parse_test(3783"target3784function %foo() system_v {}",3785ParseOptions::default()3786)3787.is_err()3788);37893790assert!(3791parse_test(3792"target x86_643793set unwind_info=false3794function %foo() system_v {}",3795ParseOptions::default()3796)3797.is_err()3798);37993800match parse_test(3801"set unwind_info=false3802target x86_643803function %foo() system_v {}",3804ParseOptions::default(),3805)3806.unwrap()3807.isa_spec3808{3809IsaSpec::None(_) => panic!("Expected some ISA"),3810IsaSpec::Some(v) => {3811assert_eq!(v.len(), 1);3812assert!(v[0].name() == "x64" || v[0].name() == "x86");3813}3814}3815}38163817#[test]3818fn user_function_name() {3819// Valid characters in the name:3820let func = Parser::new(3821"function u1:2() system_v {3822block0:3823trap int_divz3824}",3825)3826.parse_function()3827.unwrap()3828.0;3829assert_eq!(func.name.to_string(), "u1:2");38303831// Invalid characters in the name:3832let mut parser = Parser::new(3833"function u123:abc() system_v {3834block0:3835trap stk_ovf3836}",3837);3838assert!(parser.parse_function().is_err());38393840// Incomplete function names should not be valid:3841let mut parser = Parser::new(3842"function u() system_v {3843block0:3844trap int_ovf3845}",3846);3847assert!(parser.parse_function().is_err());38483849let mut parser = Parser::new(3850"function u0() system_v {3851block0:3852trap int_ovf3853}",3854);3855assert!(parser.parse_function().is_err());38563857let mut parser = Parser::new(3858"function u0:() system_v {3859block0:3860trap int_ovf3861}",3862);3863assert!(parser.parse_function().is_err());3864}38653866#[test]3867fn change_default_calling_convention() {3868let code = "function %test() {3869block0:3870return3871}";38723873// By default the parser will use the fast calling convention if none is specified.3874let mut parser = Parser::new(code);3875assert_eq!(3876parser.parse_function().unwrap().0.signature.call_conv,3877CallConv::Fast3878);38793880// However, we can specify a different calling convention to be the default.3881let mut parser = Parser::new(code).with_default_calling_convention(CallConv::PreserveAll);3882assert_eq!(3883parser.parse_function().unwrap().0.signature.call_conv,3884CallConv::PreserveAll3885);3886}38873888#[test]3889fn u8_as_hex() {3890fn parse_as_uimm8(text: &str) -> ParseResult<u8> {3891Parser::new(text).match_uimm8("unable to parse u8")3892}38933894assert_eq!(parse_as_uimm8("0").unwrap(), 0);3895assert_eq!(parse_as_uimm8("0xff").unwrap(), 255);3896assert!(parse_as_uimm8("-1").is_err());3897assert!(parse_as_uimm8("0xffa").is_err());3898}38993900#[test]3901fn i16_as_hex() {3902fn parse_as_imm16(text: &str) -> ParseResult<i16> {3903Parser::new(text).match_imm16("unable to parse i16")3904}39053906assert_eq!(parse_as_imm16("0x8000").unwrap(), -32768);3907assert_eq!(parse_as_imm16("0xffff").unwrap(), -1);3908assert_eq!(parse_as_imm16("0").unwrap(), 0);3909assert_eq!(parse_as_imm16("0x7fff").unwrap(), 32767);3910assert_eq!(3911parse_as_imm16("-0x0001").unwrap(),3912parse_as_imm16("0xffff").unwrap()3913);3914assert_eq!(3915parse_as_imm16("-0x7fff").unwrap(),3916parse_as_imm16("0x8001").unwrap()3917);3918assert!(parse_as_imm16("0xffffa").is_err());3919}39203921#[test]3922fn i32_as_hex() {3923fn parse_as_imm32(text: &str) -> ParseResult<i32> {3924Parser::new(text).match_imm32("unable to parse i32")3925}39263927assert_eq!(parse_as_imm32("0x80000000").unwrap(), -2147483648);3928assert_eq!(parse_as_imm32("0xffffffff").unwrap(), -1);3929assert_eq!(parse_as_imm32("0").unwrap(), 0);3930assert_eq!(parse_as_imm32("0x7fffffff").unwrap(), 2147483647);3931assert_eq!(3932parse_as_imm32("-0x00000001").unwrap(),3933parse_as_imm32("0xffffffff").unwrap()3934);3935assert_eq!(3936parse_as_imm32("-0x7fffffff").unwrap(),3937parse_as_imm32("0x80000001").unwrap()3938);3939assert!(parse_as_imm32("0xffffffffa").is_err());3940}39413942#[test]3943fn i64_as_hex() {3944fn parse_as_imm64(text: &str) -> ParseResult<Imm64> {3945Parser::new(text).match_imm64("unable to parse Imm64")3946}39473948assert_eq!(3949parse_as_imm64("0x8000000000000000").unwrap(),3950Imm64::new(-9223372036854775808)3951);3952assert_eq!(3953parse_as_imm64("0xffffffffffffffff").unwrap(),3954Imm64::new(-1)3955);3956assert_eq!(parse_as_imm64("0").unwrap(), Imm64::new(0));3957assert_eq!(3958parse_as_imm64("0x7fffffffffffffff").unwrap(),3959Imm64::new(9223372036854775807)3960);3961assert_eq!(3962parse_as_imm64("-0x0000000000000001").unwrap(),3963parse_as_imm64("0xffffffffffffffff").unwrap()3964);3965assert_eq!(3966parse_as_imm64("-0x7fffffffffffffff").unwrap(),3967parse_as_imm64("0x8000000000000001").unwrap()3968);3969assert!(parse_as_imm64("0xffffffffffffffffa").is_err());3970}39713972#[test]3973fn uimm128() {3974macro_rules! parse_as_constant_data {3975($text:expr, $type:expr) => {{ Parser::new($text).parse_literals_to_constant_data($type) }};3976}3977macro_rules! can_parse_as_constant_data {3978($text:expr, $type:expr) => {{ assert!(parse_as_constant_data!($text, $type).is_ok()) }};3979}3980macro_rules! cannot_parse_as_constant_data {3981($text:expr, $type:expr) => {{ assert!(parse_as_constant_data!($text, $type).is_err()) }};3982}39833984can_parse_as_constant_data!("1 2 3 4", I32X4);3985can_parse_as_constant_data!("1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16", I8X16);3986can_parse_as_constant_data!("0x1.1 0x2.2 0x3.3 0x4.4", F32X4);3987can_parse_as_constant_data!("0x0 0x1 0x2 0x3", I32X4);3988can_parse_as_constant_data!("-1 0 -1 0 -1 0 -1 0", I16X8);3989can_parse_as_constant_data!("0 -1", I64X2);3990can_parse_as_constant_data!("-1 0", I64X2);3991can_parse_as_constant_data!("-1 -1 -1 -1 -1", I32X4); // note that parse_literals_to_constant_data will leave extra tokens unconsumed39923993cannot_parse_as_constant_data!("1 2 3", I32X4);3994cannot_parse_as_constant_data!(" ", F32X4);3995}39963997#[test]3998fn parse_constant_from_booleans() {3999let c = Parser::new("-1 0 -1 0")4000.parse_literals_to_constant_data(I32X4)4001.unwrap();4002assert_eq!(4003c.into_vec(),4004[40050xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 04006]4007)4008}40094010#[test]4011fn parse_unbounded_constants() {4012// Unlike match_uimm128, match_hexadecimal_constant can parse byte sequences of any size:4013assert_eq!(4014Parser::new("0x0100")4015.match_hexadecimal_constant("err message")4016.unwrap(),4017vec![0, 1].into()4018);40194020// Only parse hexadecimal constants:4021assert!(4022Parser::new("228")4023.match_hexadecimal_constant("err message")4024.is_err()4025);4026}40274028#[test]4029fn parse_run_commands() {4030// Helper for creating signatures.4031fn sig(ins: &[Type], outs: &[Type]) -> Signature {4032let mut sig = Signature::new(CallConv::Fast);4033for i in ins {4034sig.params.push(AbiParam::new(*i));4035}4036for o in outs {4037sig.returns.push(AbiParam::new(*o));4038}4039sig4040}40414042// Helper for parsing run commands.4043fn parse(text: &str, sig: &Signature) -> ParseResult<RunCommand> {4044Parser::new(text).parse_run_command(sig)4045}40464047// Check that we can parse and display the same set of run commands.4048fn assert_roundtrip(text: &str, sig: &Signature) {4049assert_eq!(parse(text, sig).unwrap().to_string(), text);4050}4051assert_roundtrip("run: %fn0() == 42", &sig(&[], &[I32]));4052assert_roundtrip(4053"run: %fn0(8, 16, 32, 64) == 1",4054&sig(&[I8, I16, I32, I64], &[I8]),4055);4056assert_roundtrip(4057"run: %my_func(1) == 0x0f0e0d0c0b0a09080706050403020100",4058&sig(&[I32], &[I8X16]),4059);40604061// Verify that default invocations are created when not specified.4062assert_eq!(4063parse("run", &sig(&[], &[I32])).unwrap().to_string(),4064"run: %default() != 0"4065);4066assert_eq!(4067parse("print", &sig(&[], &[F32X4, I16X8]))4068.unwrap()4069.to_string(),4070"print: %default()"4071);40724073// Demonstrate some unparsable cases.4074assert!(parse("print", &sig(&[I32], &[I32])).is_err());4075assert!(parse("print:", &sig(&[], &[])).is_err());4076assert!(parse("run: ", &sig(&[], &[])).is_err());4077}40784079#[test]4080fn parse_data_values() {4081fn parse(text: &str, ty: Type) -> DataValue {4082Parser::new(text).parse_data_value(ty).unwrap()4083}40844085assert_eq!(parse("8", I8).to_string(), "8");4086assert_eq!(parse("16", I16).to_string(), "16");4087assert_eq!(parse("32", I32).to_string(), "32");4088assert_eq!(parse("64", I64).to_string(), "64");4089assert_eq!(4090parse("0x01234567_01234567_01234567_01234567", I128).to_string(),4091"1512366032949150931280199141537564007"4092);4093assert_eq!(parse("1234567", I128).to_string(), "1234567");4094assert_eq!(parse("0x16.1", F16).to_string(), "0x1.610p4");4095assert_eq!(parse("0x32.32", F32).to_string(), "0x1.919000p5");4096assert_eq!(parse("0x64.64", F64).to_string(), "0x1.9190000000000p6");4097assert_eq!(4098parse("0x128.128", F128).to_string(),4099"0x1.2812800000000000000000000000p8"4100);4101assert_eq!(4102parse("[0 1 2 3]", I32X4).to_string(),4103"0x00000003000000020000000100000000"4104);4105assert_eq!(parse("[1 2]", I32X2).to_string(), "0x0000000200000001");4106assert_eq!(parse("[1 2 3 4]", I8X4).to_string(), "0x04030201");4107assert_eq!(parse("[1 2]", I8X2).to_string(), "0x0201");4108}41094110#[test]4111fn parse_cold_blocks() {4112let code = "function %test() {4113block0 cold:4114return4115block1(v0: i32) cold:4116return4117block2(v1: i32):4118return4119}";41204121let mut parser = Parser::new(code);4122let func = parser.parse_function().unwrap().0;4123assert_eq!(func.layout.blocks().count(), 3);4124assert!(func.layout.is_cold(Block::from_u32(0)));4125assert!(func.layout.is_cold(Block::from_u32(1)));4126assert!(!func.layout.is_cold(Block::from_u32(2)));4127}4128}412941304131