1 //! Parser for .clif files. 2 3 use crate::error::{Location, ParseError, ParseResult}; 4 use crate::isaspec; 5 use crate::lexer::{LexError, Lexer, LocatedError, LocatedToken, Token}; 6 use crate::run_command::{Comparison, Invocation, RunCommand}; 7 use crate::sourcemap::SourceMap; 8 use crate::testcommand::TestCommand; 9 use crate::testfile::{Comment, Details, Feature, TestFile}; 10 use cranelift_codegen::data_value::DataValue; 11 use cranelift_codegen::entity::{EntityRef, PrimaryMap}; 12 use cranelift_codegen::ir::entities::{AnyEntity, DynamicType, MemoryType}; 13 use cranelift_codegen::ir::immediates::{ 14 Ieee128, Ieee16, Ieee32, Ieee64, Imm64, Offset32, Uimm32, Uimm64, 15 }; 16 use cranelift_codegen::ir::instructions::{InstructionData, InstructionFormat, VariableArgs}; 17 use cranelift_codegen::ir::pcc::{BaseExpr, Expr, Fact}; 18 use cranelift_codegen::ir::types::*; 19 use cranelift_codegen::ir::{self, UserExternalNameRef}; 20 21 use cranelift_codegen::ir::{ 22 types, AbiParam, ArgumentExtension, ArgumentPurpose, Block, BlockArg, Constant, ConstantData, 23 DynamicStackSlot, DynamicStackSlotData, DynamicTypeData, ExtFuncData, ExternalName, FuncRef, 24 Function, GlobalValue, GlobalValueData, JumpTableData, MemFlags, MemoryTypeData, 25 MemoryTypeField, Opcode, SigRef, Signature, StackSlot, StackSlotData, StackSlotKind, 26 UserFuncName, Value, 27 }; 28 use cranelift_codegen::isa::{self, CallConv}; 29 use cranelift_codegen::packed_option::ReservedValue; 30 use cranelift_codegen::{settings, settings::Configurable, timing}; 31 use smallvec::SmallVec; 32 use std::mem; 33 use std::str::FromStr; 34 use std::{u16, u32}; 35 use target_lexicon::Triple; 36 37 macro_rules! match_imm { 38 ($signed:ty, $unsigned:ty, $parser:expr, $err_msg:expr) => {{ 39 if let Some(Token::Integer(text)) = $parser.token() { 40 $parser.consume(); 41 let negative = text.starts_with('-'); 42 let positive = text.starts_with('+'); 43 let text = if negative || positive { 44 // Strip sign prefix. 45 &text[1..] 46 } else { 47 text 48 }; 49 50 // Parse the text value; the lexer gives us raw text that looks like an integer. 51 let value = if text.starts_with("0x") { 52 // Skip underscores. 53 let text = text.replace("_", ""); 54 // Parse it in hexadecimal form. 55 <$unsigned>::from_str_radix(&text[2..], 16).map_err(|_| { 56 $parser.error(&format!( 57 "unable to parse '{}' value as a hexadecimal {} immediate", 58 &text[2..], 59 stringify!($unsigned), 60 )) 61 })? 62 } else { 63 // Parse it as a signed type to check for overflow and other issues. 64 text.parse() 65 .map_err(|_| $parser.error("expected decimal immediate"))? 66 }; 67 68 // Apply sign if necessary. 69 let signed = if negative { 70 let value = value.wrapping_neg() as $signed; 71 if value > 0 { 72 return Err($parser.error("negative number too small")); 73 } 74 value 75 } else { 76 value as $signed 77 }; 78 79 Ok(signed) 80 } else { 81 err!($parser.loc, $err_msg) 82 } 83 }}; 84 } 85 86 /// After some quick benchmarks a program should never have more than 100,000 blocks. 87 const MAX_BLOCKS_IN_A_FUNCTION: u32 = 100_000; 88 89 /// Parse the entire `text` into a list of functions. 90 /// 91 /// Any test commands or target declarations are ignored. 92 pub fn parse_functions(text: &str) -> ParseResult<Vec<Function>> { 93 let _tt = timing::parse_text(); 94 parse_test(text, ParseOptions::default()) 95 .map(|file| file.functions.into_iter().map(|(func, _)| func).collect()) 96 } 97 98 /// Options for configuring the parsing of filetests. 99 pub struct ParseOptions<'a> { 100 /// Compiler passes to run on the parsed functions. 101 pub passes: Option<&'a [String]>, 102 /// Target ISA for compiling the parsed functions, e.g. "x86_64 skylake". 103 pub target: Option<&'a str>, 104 /// Default calling convention used when none is specified for a parsed function. 105 pub default_calling_convention: CallConv, 106 /// Default for unwind-info setting (enabled or disabled). 107 pub unwind_info: bool, 108 /// Default for machine_code_cfg_info setting (enabled or disabled). 109 pub machine_code_cfg_info: bool, 110 } 111 112 impl Default for ParseOptions<'_> { 113 fn default() -> Self { 114 Self { 115 passes: None, 116 target: None, 117 default_calling_convention: CallConv::Fast, 118 unwind_info: false, 119 machine_code_cfg_info: false, 120 } 121 } 122 } 123 124 /// Parse the entire `text` as a test case file. 125 /// 126 /// The returned `TestFile` contains direct references to substrings of `text`. 127 pub fn parse_test<'a>(text: &'a str, options: ParseOptions<'a>) -> ParseResult<TestFile<'a>> { 128 let _tt = timing::parse_text(); 129 let mut parser = Parser::new(text); 130 131 // Gather the preamble comments. 132 parser.start_gathering_comments(); 133 134 let isa_spec: isaspec::IsaSpec; 135 let commands: Vec<TestCommand<'a>>; 136 137 // Check for specified passes and target, if present throw out test commands/targets specified 138 // in file. 139 match options.passes { 140 Some(pass_vec) => { 141 parser.parse_test_commands(); 142 commands = parser.parse_cmdline_passes(pass_vec); 143 parser.parse_target_specs(&options)?; 144 isa_spec = parser.parse_cmdline_target(options.target)?; 145 } 146 None => { 147 commands = parser.parse_test_commands(); 148 isa_spec = parser.parse_target_specs(&options)?; 149 } 150 }; 151 let features = parser.parse_cranelift_features()?; 152 153 // Decide between using the calling convention passed in the options or using the 154 // host's calling convention--if any tests are to be run on the host we should default to the 155 // host's calling convention. 156 parser = if commands.iter().any(|tc| tc.command == "run") { 157 let host_default_calling_convention = CallConv::triple_default(&Triple::host()); 158 parser.with_default_calling_convention(host_default_calling_convention) 159 } else { 160 parser.with_default_calling_convention(options.default_calling_convention) 161 }; 162 163 parser.token(); 164 parser.claim_gathered_comments(AnyEntity::Function); 165 166 let preamble_comments = parser.take_comments(); 167 let functions = parser.parse_function_list()?; 168 169 Ok(TestFile { 170 commands, 171 isa_spec, 172 features, 173 preamble_comments, 174 functions, 175 }) 176 } 177 178 /// Parse a CLIF comment `text` as a run command. 179 /// 180 /// Return: 181 /// - `Ok(None)` if the comment is not intended to be a `RunCommand` (i.e. does not start with `run` 182 /// or `print` 183 /// - `Ok(Some(command))` if the comment is intended as a `RunCommand` and can be parsed to one 184 /// - `Err` otherwise. 185 pub fn parse_run_command(text: &str, signature: &Signature) -> ParseResult<Option<RunCommand>> { 186 let _tt = timing::parse_text(); 187 // We remove leading spaces and semi-colons for convenience here instead of at the call sites 188 // since this function will be attempting to parse a RunCommand from a CLIF comment. 189 let trimmed_text = text.trim_start_matches(|c| c == ' ' || c == ';'); 190 let mut parser = Parser::new(trimmed_text); 191 match parser.token() { 192 Some(Token::Identifier("run")) | Some(Token::Identifier("print")) => { 193 parser.parse_run_command(signature).map(|c| Some(c)) 194 } 195 Some(_) | None => Ok(None), 196 } 197 } 198 199 pub struct Parser<'a> { 200 lex: Lexer<'a>, 201 202 lex_error: Option<LexError>, 203 204 /// Current lookahead token. 205 lookahead: Option<Token<'a>>, 206 207 /// Location of lookahead. 208 loc: Location, 209 210 /// Are we gathering any comments that we encounter? 211 gathering_comments: bool, 212 213 /// The gathered comments; claim them with `claim_gathered_comments`. 214 gathered_comments: Vec<&'a str>, 215 216 /// Comments collected so far. 217 comments: Vec<Comment<'a>>, 218 219 /// Maps inlined external names to a ref value, so they can be declared before parsing the rest 220 /// of the function later. 221 /// 222 /// This maintains backward compatibility with previous ways for declaring external names. 223 predeclared_external_names: PrimaryMap<UserExternalNameRef, ir::UserExternalName>, 224 225 /// Default calling conventions; used when none is specified. 226 default_calling_convention: CallConv, 227 } 228 229 /// Context for resolving references when parsing a single function. 230 struct Context { 231 function: Function, 232 map: SourceMap, 233 234 /// Aliases to resolve once value definitions are known. 235 aliases: Vec<Value>, 236 } 237 238 impl Context { 239 fn new(f: Function) -> Self { 240 Self { 241 function: f, 242 map: SourceMap::new(), 243 aliases: Vec::new(), 244 } 245 } 246 247 // Allocate a new stack slot. 248 fn add_ss(&mut self, ss: StackSlot, data: StackSlotData, loc: Location) -> ParseResult<()> { 249 self.map.def_ss(ss, loc)?; 250 while self.function.sized_stack_slots.next_key().index() <= ss.index() { 251 self.function.create_sized_stack_slot(StackSlotData::new( 252 StackSlotKind::ExplicitSlot, 253 0, 254 0, 255 )); 256 } 257 self.function.sized_stack_slots[ss] = data; 258 Ok(()) 259 } 260 261 // Resolve a reference to a stack slot. 262 fn check_ss(&self, ss: StackSlot, loc: Location) -> ParseResult<()> { 263 if !self.map.contains_ss(ss) { 264 err!(loc, "undefined stack slot {}", ss) 265 } else { 266 Ok(()) 267 } 268 } 269 270 // Allocate a new stack slot. 271 fn add_dss( 272 &mut self, 273 ss: DynamicStackSlot, 274 data: DynamicStackSlotData, 275 loc: Location, 276 ) -> ParseResult<()> { 277 self.map.def_dss(ss, loc)?; 278 while self.function.dynamic_stack_slots.next_key().index() <= ss.index() { 279 self.function 280 .create_dynamic_stack_slot(DynamicStackSlotData::new( 281 StackSlotKind::ExplicitDynamicSlot, 282 data.dyn_ty, 283 )); 284 } 285 self.function.dynamic_stack_slots[ss] = data; 286 Ok(()) 287 } 288 289 // Resolve a reference to a dynamic stack slot. 290 fn check_dss(&self, dss: DynamicStackSlot, loc: Location) -> ParseResult<()> { 291 if !self.map.contains_dss(dss) { 292 err!(loc, "undefined dynamic stack slot {}", dss) 293 } else { 294 Ok(()) 295 } 296 } 297 298 // Allocate a new dynamic type. 299 fn add_dt(&mut self, dt: DynamicType, data: DynamicTypeData, loc: Location) -> ParseResult<()> { 300 self.map.def_dt(dt, loc)?; 301 while self.function.dfg.dynamic_types.next_key().index() <= dt.index() { 302 self.function.dfg.make_dynamic_ty(DynamicTypeData::new( 303 data.base_vector_ty, 304 data.dynamic_scale, 305 )); 306 } 307 self.function.dfg.dynamic_types[dt] = data; 308 Ok(()) 309 } 310 311 // Allocate a global value slot. 312 fn add_gv( 313 &mut self, 314 gv: GlobalValue, 315 data: GlobalValueData, 316 maybe_fact: Option<Fact>, 317 loc: Location, 318 ) -> ParseResult<()> { 319 self.map.def_gv(gv, loc)?; 320 while self.function.global_values.next_key().index() <= gv.index() { 321 self.function.create_global_value(GlobalValueData::Symbol { 322 name: ExternalName::testcase(""), 323 offset: Imm64::new(0), 324 colocated: false, 325 tls: false, 326 }); 327 } 328 self.function.global_values[gv] = data; 329 if let Some(fact) = maybe_fact { 330 self.function.global_value_facts[gv] = Some(fact); 331 } 332 Ok(()) 333 } 334 335 // Allocate a memory-type slot. 336 fn add_mt(&mut self, mt: MemoryType, data: MemoryTypeData, loc: Location) -> ParseResult<()> { 337 self.map.def_mt(mt, loc)?; 338 while self.function.memory_types.next_key().index() <= mt.index() { 339 self.function.create_memory_type(MemoryTypeData::default()); 340 } 341 self.function.memory_types[mt] = data; 342 Ok(()) 343 } 344 345 // Resolve a reference to a global value. 346 fn check_gv(&self, gv: GlobalValue, loc: Location) -> ParseResult<()> { 347 if !self.map.contains_gv(gv) { 348 err!(loc, "undefined global value {}", gv) 349 } else { 350 Ok(()) 351 } 352 } 353 354 // Allocate a new signature. 355 fn add_sig( 356 &mut self, 357 sig: SigRef, 358 data: Signature, 359 loc: Location, 360 defaultcc: CallConv, 361 ) -> ParseResult<()> { 362 self.map.def_sig(sig, loc)?; 363 while self.function.dfg.signatures.next_key().index() <= sig.index() { 364 self.function.import_signature(Signature::new(defaultcc)); 365 } 366 self.function.dfg.signatures[sig] = data; 367 Ok(()) 368 } 369 370 // Resolve a reference to a signature. 371 fn check_sig(&self, sig: SigRef, loc: Location) -> ParseResult<()> { 372 if !self.map.contains_sig(sig) { 373 err!(loc, "undefined signature {}", sig) 374 } else { 375 Ok(()) 376 } 377 } 378 379 // Allocate a new external function. 380 fn add_fn(&mut self, fn_: FuncRef, data: ExtFuncData, loc: Location) -> ParseResult<()> { 381 self.map.def_fn(fn_, loc)?; 382 while self.function.dfg.ext_funcs.next_key().index() <= fn_.index() { 383 self.function.import_function(ExtFuncData { 384 name: ExternalName::testcase(""), 385 signature: SigRef::reserved_value(), 386 colocated: false, 387 }); 388 } 389 self.function.dfg.ext_funcs[fn_] = data; 390 Ok(()) 391 } 392 393 // Resolve a reference to a function. 394 fn check_fn(&self, fn_: FuncRef, loc: Location) -> ParseResult<()> { 395 if !self.map.contains_fn(fn_) { 396 err!(loc, "undefined function {}", fn_) 397 } else { 398 Ok(()) 399 } 400 } 401 402 // Allocate a new constant. 403 fn add_constant( 404 &mut self, 405 constant: Constant, 406 data: ConstantData, 407 loc: Location, 408 ) -> ParseResult<()> { 409 self.map.def_constant(constant, loc)?; 410 self.function.dfg.constants.set(constant, data); 411 Ok(()) 412 } 413 414 // Configure the stack limit of the current function. 415 fn add_stack_limit(&mut self, limit: GlobalValue, loc: Location) -> ParseResult<()> { 416 if self.function.stack_limit.is_some() { 417 return err!(loc, "stack limit defined twice"); 418 } 419 self.function.stack_limit = Some(limit); 420 Ok(()) 421 } 422 423 // Resolve a reference to a constant. 424 fn check_constant(&self, c: Constant, loc: Location) -> ParseResult<()> { 425 if !self.map.contains_constant(c) { 426 err!(loc, "undefined constant {}", c) 427 } else { 428 Ok(()) 429 } 430 } 431 432 // Allocate a new block. 433 fn add_block(&mut self, block: Block, loc: Location) -> ParseResult<Block> { 434 self.map.def_block(block, loc)?; 435 while self.function.dfg.num_blocks() <= block.index() { 436 self.function.dfg.make_block(); 437 } 438 self.function.layout.append_block(block); 439 Ok(block) 440 } 441 442 /// Set a block as cold. 443 fn set_cold_block(&mut self, block: Block) { 444 self.function.layout.set_cold(block); 445 } 446 } 447 448 impl<'a> Parser<'a> { 449 /// Create a new `Parser` which reads `text`. The referenced text must outlive the parser. 450 pub fn new(text: &'a str) -> Self { 451 Self { 452 lex: Lexer::new(text), 453 lex_error: None, 454 lookahead: None, 455 loc: Location { line_number: 0 }, 456 gathering_comments: false, 457 gathered_comments: Vec::new(), 458 comments: Vec::new(), 459 default_calling_convention: CallConv::Fast, 460 predeclared_external_names: Default::default(), 461 } 462 } 463 464 /// Modify the default calling convention; returns a new parser with the changed calling 465 /// convention. 466 pub fn with_default_calling_convention(self, default_calling_convention: CallConv) -> Self { 467 Self { 468 default_calling_convention, 469 ..self 470 } 471 } 472 473 // Consume the current lookahead token and return it. 474 fn consume(&mut self) -> Token<'a> { 475 self.lookahead.take().expect("No token to consume") 476 } 477 478 // Consume the whole line following the current lookahead token. 479 // Return the text of the line tail. 480 fn consume_line(&mut self) -> &'a str { 481 let rest = self.lex.rest_of_line(); 482 self.consume(); 483 rest 484 } 485 486 // Get the current lookahead token, after making sure there is one. 487 fn token(&mut self) -> Option<Token<'a>> { 488 while self.lookahead.is_none() { 489 match self.lex.next() { 490 Some(Ok(LocatedToken { token, location })) => { 491 match token { 492 Token::Comment(text) => { 493 if self.gathering_comments { 494 self.gathered_comments.push(text); 495 } 496 } 497 _ => self.lookahead = Some(token), 498 } 499 self.loc = location; 500 } 501 Some(Err(LocatedError { error, location })) => { 502 self.lex_error = Some(error); 503 self.loc = location; 504 break; 505 } 506 None => break, 507 } 508 } 509 self.lookahead 510 } 511 512 // Enable gathering of all comments encountered. 513 fn start_gathering_comments(&mut self) { 514 debug_assert!(!self.gathering_comments); 515 self.gathering_comments = true; 516 debug_assert!(self.gathered_comments.is_empty()); 517 } 518 519 // Claim the comments gathered up to the current position for the 520 // given entity. 521 fn claim_gathered_comments<E: Into<AnyEntity>>(&mut self, entity: E) { 522 debug_assert!(self.gathering_comments); 523 let entity = entity.into(); 524 self.comments.extend( 525 self.gathered_comments 526 .drain(..) 527 .map(|text| Comment { entity, text }), 528 ); 529 self.gathering_comments = false; 530 } 531 532 // Get the comments collected so far, clearing out the internal list. 533 fn take_comments(&mut self) -> Vec<Comment<'a>> { 534 debug_assert!(!self.gathering_comments); 535 mem::replace(&mut self.comments, Vec::new()) 536 } 537 538 // Match and consume a token without payload. 539 fn match_token(&mut self, want: Token<'a>, err_msg: &str) -> ParseResult<Token<'a>> { 540 if self.token() == Some(want) { 541 Ok(self.consume()) 542 } else { 543 err!(self.loc, err_msg) 544 } 545 } 546 547 // If the next token is a `want`, consume it, otherwise do nothing. 548 fn optional(&mut self, want: Token<'a>) -> bool { 549 if self.token() == Some(want) { 550 self.consume(); 551 true 552 } else { 553 false 554 } 555 } 556 557 // Match and consume a specific identifier string. 558 // Used for pseudo-keywords like "stack_slot" that only appear in certain contexts. 559 fn match_identifier(&mut self, want: &'static str, err_msg: &str) -> ParseResult<Token<'a>> { 560 if self.token() == Some(Token::Identifier(want)) { 561 Ok(self.consume()) 562 } else { 563 err!(self.loc, err_msg) 564 } 565 } 566 567 // Match and consume a type. 568 fn match_type(&mut self, err_msg: &str) -> ParseResult<Type> { 569 if let Some(Token::Type(t)) = self.token() { 570 self.consume(); 571 Ok(t) 572 } else { 573 err!(self.loc, err_msg) 574 } 575 } 576 577 // Match and consume a stack slot reference. 578 fn match_ss(&mut self, err_msg: &str) -> ParseResult<StackSlot> { 579 if let Some(Token::StackSlot(ss)) = self.token() { 580 self.consume(); 581 if let Some(ss) = StackSlot::with_number(ss) { 582 return Ok(ss); 583 } 584 } 585 err!(self.loc, err_msg) 586 } 587 588 // Match and consume a dynamic stack slot reference. 589 fn match_dss(&mut self, err_msg: &str) -> ParseResult<DynamicStackSlot> { 590 if let Some(Token::DynamicStackSlot(ss)) = self.token() { 591 self.consume(); 592 if let Some(ss) = DynamicStackSlot::with_number(ss) { 593 return Ok(ss); 594 } 595 } 596 err!(self.loc, err_msg) 597 } 598 599 // Match and consume a dynamic type reference. 600 fn match_dt(&mut self, err_msg: &str) -> ParseResult<DynamicType> { 601 if let Some(Token::DynamicType(dt)) = self.token() { 602 self.consume(); 603 if let Some(dt) = DynamicType::with_number(dt) { 604 return Ok(dt); 605 } 606 } 607 err!(self.loc, err_msg) 608 } 609 610 // Extract Type from DynamicType 611 fn concrete_from_dt(&mut self, dt: DynamicType, ctx: &mut Context) -> Option<Type> { 612 ctx.function.get_concrete_dynamic_ty(dt) 613 } 614 615 // Match and consume a global value reference. 616 fn match_gv(&mut self, err_msg: &str) -> ParseResult<GlobalValue> { 617 if let Some(Token::GlobalValue(gv)) = self.token() { 618 self.consume(); 619 if let Some(gv) = GlobalValue::with_number(gv) { 620 return Ok(gv); 621 } 622 } 623 err!(self.loc, err_msg) 624 } 625 626 // Match and consume a function reference. 627 fn match_fn(&mut self, err_msg: &str) -> ParseResult<FuncRef> { 628 if let Some(Token::FuncRef(fnref)) = self.token() { 629 self.consume(); 630 if let Some(fnref) = FuncRef::with_number(fnref) { 631 return Ok(fnref); 632 } 633 } 634 err!(self.loc, err_msg) 635 } 636 637 // Match and consume a signature reference. 638 fn match_sig(&mut self, err_msg: &str) -> ParseResult<SigRef> { 639 if let Some(Token::SigRef(sigref)) = self.token() { 640 self.consume(); 641 if let Some(sigref) = SigRef::with_number(sigref) { 642 return Ok(sigref); 643 } 644 } 645 err!(self.loc, err_msg) 646 } 647 648 // Match and consume a memory-type reference. 649 fn match_mt(&mut self, err_msg: &str) -> ParseResult<MemoryType> { 650 if let Some(Token::MemoryType(mt)) = self.token() { 651 self.consume(); 652 if let Some(mt) = MemoryType::with_number(mt) { 653 return Ok(mt); 654 } 655 } 656 err!(self.loc, err_msg) 657 } 658 659 // Match and consume a constant reference. 660 fn match_constant(&mut self) -> ParseResult<Constant> { 661 if let Some(Token::Constant(c)) = self.token() { 662 self.consume(); 663 if let Some(c) = Constant::with_number(c) { 664 return Ok(c); 665 } 666 } 667 err!(self.loc, "expected constant number: const«n»") 668 } 669 670 // Match and consume a stack limit token 671 fn match_stack_limit(&mut self) -> ParseResult<()> { 672 if let Some(Token::Identifier("stack_limit")) = self.token() { 673 self.consume(); 674 return Ok(()); 675 } 676 err!(self.loc, "expected identifier: stack_limit") 677 } 678 679 // Match and consume a block reference. 680 fn match_block(&mut self, err_msg: &str) -> ParseResult<Block> { 681 if let Some(Token::Block(block)) = self.token() { 682 self.consume(); 683 Ok(block) 684 } else { 685 err!(self.loc, err_msg) 686 } 687 } 688 689 // Match and consume a value reference. 690 fn match_value(&mut self, err_msg: &str) -> ParseResult<Value> { 691 if let Some(Token::Value(v)) = self.token() { 692 self.consume(); 693 Ok(v) 694 } else { 695 err!(self.loc, err_msg) 696 } 697 } 698 699 fn error(&self, message: &str) -> ParseError { 700 ParseError { 701 location: self.loc, 702 message: message.to_string(), 703 is_warning: false, 704 } 705 } 706 707 // Match and consume an Imm64 immediate. 708 fn match_imm64(&mut self, err_msg: &str) -> ParseResult<Imm64> { 709 if let Some(Token::Integer(text)) = self.token() { 710 self.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. 713 text.parse().map_err(|e| self.error(e)) 714 } else { 715 err!(self.loc, err_msg) 716 } 717 } 718 719 // Match and consume a hexadeximal immediate 720 fn match_hexadecimal_constant(&mut self, err_msg: &str) -> ParseResult<ConstantData> { 721 if let Some(Token::Integer(text)) = self.token() { 722 self.consume(); 723 text.parse().map_err(|e| { 724 self.error(&format!( 725 "expected hexadecimal immediate, failed to parse: {e}" 726 )) 727 }) 728 } else { 729 err!(self.loc, err_msg) 730 } 731 } 732 733 // Match and consume either a hexadecimal Uimm128 immediate (e.g. 0x000102...) or its literal 734 // list form (e.g. [0 1 2...]). For convenience, since uimm128 values are stored in the 735 // `ConstantPool`, this returns `ConstantData`. 736 fn match_uimm128(&mut self, controlling_type: Type) -> ParseResult<ConstantData> { 737 let expected_size = controlling_type.bytes() as usize; 738 let constant_data = if self.optional(Token::LBracket) { 739 // parse using a list of values, e.g. vconst.i32x4 [0 1 2 3] 740 let uimm128 = self.parse_literals_to_constant_data(controlling_type)?; 741 self.match_token(Token::RBracket, "expected a terminating right bracket")?; 742 uimm128 743 } else { 744 // parse using a hexadecimal value, e.g. 0x000102... 745 let uimm128 = 746 self.match_hexadecimal_constant("expected an immediate hexadecimal operand")?; 747 uimm128.expand_to(expected_size) 748 }; 749 750 if constant_data.len() == expected_size { 751 Ok(constant_data) 752 } else { 753 Err(self.error(&format!( 754 "expected parsed constant to have {expected_size} bytes" 755 ))) 756 } 757 } 758 759 // Match and consume a Uimm64 immediate. 760 fn match_uimm64(&mut self, err_msg: &str) -> ParseResult<Uimm64> { 761 if let Some(Token::Integer(text)) = self.token() { 762 self.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. 765 text.parse() 766 .map_err(|_| self.error("expected u64 decimal immediate")) 767 } else { 768 err!(self.loc, err_msg) 769 } 770 } 771 772 // Match and consume a Uimm32 immediate. 773 fn match_uimm32(&mut self, err_msg: &str) -> ParseResult<Uimm32> { 774 if let Some(Token::Integer(text)) = self.token() { 775 self.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. 778 text.parse().map_err(|e| self.error(e)) 779 } else { 780 err!(self.loc, err_msg) 781 } 782 } 783 784 // Match and consume a u8 immediate. 785 // This is used for lane numbers in SIMD vectors. 786 fn match_uimm8(&mut self, err_msg: &str) -> ParseResult<u8> { 787 if let Some(Token::Integer(text)) = self.token() { 788 self.consume(); 789 // Lexer just gives us raw text that looks like an integer. 790 if let Some(num) = text.strip_prefix("0x") { 791 // Parse it as a u8 in hexadecimal form. 792 u8::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. 796 text.parse() 797 .map_err(|_| self.error("expected u8 decimal immediate")) 798 } 799 } else { 800 err!(self.loc, err_msg) 801 } 802 } 803 804 // Match and consume an i8 immediate. 805 fn match_imm8(&mut self, err_msg: &str) -> ParseResult<i8> { 806 match_imm!(i8, u8, self, err_msg) 807 } 808 809 // Match and consume a signed 16-bit immediate. 810 fn match_imm16(&mut self, err_msg: &str) -> ParseResult<i16> { 811 match_imm!(i16, u16, self, err_msg) 812 } 813 814 // Match and consume an i32 immediate. 815 // This is used for stack argument byte offsets. 816 fn match_imm32(&mut self, err_msg: &str) -> ParseResult<i32> { 817 match_imm!(i32, u32, self, err_msg) 818 } 819 820 // Match and consume an i128 immediate. 821 fn match_imm128(&mut self, err_msg: &str) -> ParseResult<i128> { 822 match_imm!(i128, u128, self, err_msg) 823 } 824 825 // 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 is 828 // present, it must contain a sign. 829 fn optional_offset32(&mut self) -> ParseResult<Offset32> { 830 if let Some(Token::Integer(text)) = self.token() { 831 if text.starts_with('+') || text.starts_with('-') { 832 self.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. 835 return text.parse().map_err(|e| self.error(e)); 836 } 837 } 838 // An offset32 operand can be absent. 839 Ok(Offset32::new(0)) 840 } 841 842 // 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 is 845 // present, it must contain a sign. 846 fn optional_offset_imm64(&mut self) -> ParseResult<Imm64> { 847 if let Some(Token::Integer(text)) = self.token() { 848 if text.starts_with('+') || text.starts_with('-') { 849 self.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. 852 return text.parse().map_err(|e| self.error(e)); 853 } 854 } 855 // If no explicit offset is present, the offset is 0. 856 Ok(Imm64::new(0)) 857 } 858 859 // Match and consume an Ieee16 immediate. 860 fn match_ieee16(&mut self, err_msg: &str) -> ParseResult<Ieee16> { 861 if let Some(Token::Float(text)) = self.token() { 862 self.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. 865 text.parse().map_err(|e| self.error(e)) 866 } else { 867 err!(self.loc, err_msg) 868 } 869 } 870 871 // Match and consume an Ieee32 immediate. 872 fn match_ieee32(&mut self, err_msg: &str) -> ParseResult<Ieee32> { 873 if let Some(Token::Float(text)) = self.token() { 874 self.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. 877 text.parse().map_err(|e| self.error(e)) 878 } else { 879 err!(self.loc, err_msg) 880 } 881 } 882 883 // Match and consume an Ieee64 immediate. 884 fn match_ieee64(&mut self, err_msg: &str) -> ParseResult<Ieee64> { 885 if let Some(Token::Float(text)) = self.token() { 886 self.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. 889 text.parse().map_err(|e| self.error(e)) 890 } else { 891 err!(self.loc, err_msg) 892 } 893 } 894 895 // Match and consume an Ieee128 immediate. 896 fn match_ieee128(&mut self, err_msg: &str) -> ParseResult<Ieee128> { 897 if let Some(Token::Float(text)) = self.token() { 898 self.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. 901 text.parse().map_err(|e| self.error(e)) 902 } else { 903 err!(self.loc, err_msg) 904 } 905 } 906 907 // Match and consume an enumerated immediate, like one of the condition codes. 908 fn match_enum<T: FromStr>(&mut self, err_msg: &str) -> ParseResult<T> { 909 if let Some(Token::Identifier(text)) = self.token() { 910 self.consume(); 911 text.parse().map_err(|_| self.error(err_msg)) 912 } else { 913 err!(self.loc, err_msg) 914 } 915 } 916 917 // Match and a consume a possibly empty sequence of memory operation flags. 918 fn optional_memflags(&mut self) -> ParseResult<MemFlags> { 919 let mut flags = MemFlags::new(); 920 while let Some(Token::Identifier(text)) = self.token() { 921 match flags.set_by_name(text) { 922 Ok(true) => { 923 self.consume(); 924 } 925 Ok(false) => break, 926 Err(msg) => return err!(self.loc, msg), 927 } 928 } 929 Ok(flags) 930 } 931 932 // Match and consume an identifier. 933 fn match_any_identifier(&mut self, err_msg: &str) -> ParseResult<&'a str> { 934 if let Some(Token::Identifier(text)) = self.token() { 935 self.consume(); 936 Ok(text) 937 } else { 938 err!(self.loc, err_msg) 939 } 940 } 941 942 /// Parse an optional source location. 943 /// 944 /// Return an optional source location if no real location is present. 945 fn optional_srcloc(&mut self) -> ParseResult<ir::SourceLoc> { 946 if let Some(Token::SourceLoc(text)) = self.token() { 947 match u32::from_str_radix(text, 16) { 948 Ok(num) => { 949 self.consume(); 950 Ok(ir::SourceLoc::new(num)) 951 } 952 Err(_) => return err!(self.loc, "invalid source location: {}", text), 953 } 954 } else { 955 Ok(Default::default()) 956 } 957 } 958 959 /// Parse a list of literals (i.e. integers, floats, booleans); e.g. `0 1 2 3`, usually as 960 /// part of something like `vconst.i32x4 [0 1 2 3]`. 961 fn parse_literals_to_constant_data(&mut self, ty: Type) -> ParseResult<ConstantData> { 962 macro_rules! consume { 963 ( $ty:ident, $match_fn:expr ) => {{ 964 assert!($ty.is_vector()); 965 let mut data = ConstantData::default(); 966 for _ in 0..$ty.lane_count() { 967 data = data.append($match_fn); 968 } 969 data 970 }}; 971 } 972 973 if !ty.is_vector() && !ty.is_dynamic_vector() { 974 err!(self.loc, "Expected a controlling vector type, not {}", ty) 975 } else { 976 let constant_data = match ty.lane_type() { 977 I8 => consume!(ty, self.match_imm8("Expected an 8-bit integer")?), 978 I16 => consume!(ty, self.match_imm16("Expected a 16-bit integer")?), 979 I32 => consume!(ty, self.match_imm32("Expected a 32-bit integer")?), 980 I64 => consume!(ty, self.match_imm64("Expected a 64-bit integer")?), 981 F32 => consume!(ty, self.match_ieee32("Expected a 32-bit float")?), 982 F64 => consume!(ty, self.match_ieee64("Expected a 64-bit float")?), 983 _ => return err!(self.loc, "Expected a type of: float, int, bool"), 984 }; 985 Ok(constant_data) 986 } 987 } 988 989 /// Parse a list of test command passes specified in command line. 990 pub fn parse_cmdline_passes(&mut self, passes: &'a [String]) -> Vec<TestCommand<'a>> { 991 let mut list = Vec::new(); 992 for pass in passes { 993 list.push(TestCommand::new(pass)); 994 } 995 list 996 } 997 998 /// Parse a list of test commands. 999 pub fn parse_test_commands(&mut self) -> Vec<TestCommand<'a>> { 1000 let mut list = Vec::new(); 1001 while self.token() == Some(Token::Identifier("test")) { 1002 list.push(TestCommand::new(self.consume_line())); 1003 } 1004 list 1005 } 1006 1007 /// Parse a target spec. 1008 /// 1009 /// Accept the target from the command line for pass command. 1010 /// 1011 fn parse_cmdline_target(&mut self, target_pass: Option<&str>) -> ParseResult<isaspec::IsaSpec> { 1012 // Were there any `target` commands specified? 1013 let mut specified_target = false; 1014 1015 let mut targets = Vec::new(); 1016 let flag_builder = settings::builder(); 1017 1018 if let Some(targ) = target_pass { 1019 let loc = self.loc; 1020 let triple = match Triple::from_str(targ) { 1021 Ok(triple) => triple, 1022 Err(err) => return err!(loc, err), 1023 }; 1024 let isa_builder = match isa::lookup(triple) { 1025 Err(isa::LookupError::SupportDisabled) => { 1026 return err!(loc, "support disabled target '{}'", targ); 1027 } 1028 Err(isa::LookupError::Unsupported) => { 1029 return warn!(loc, "unsupported target '{}'", targ); 1030 } 1031 Ok(b) => b, 1032 }; 1033 specified_target = true; 1034 1035 // Construct a trait object with the aggregate settings. 1036 targets.push( 1037 isa_builder 1038 .finish(settings::Flags::new(flag_builder.clone())) 1039 .map_err(|e| ParseError { 1040 location: loc, 1041 message: format!("invalid ISA flags for '{targ}': {e:?}"), 1042 is_warning: false, 1043 })?, 1044 ); 1045 } 1046 1047 if !specified_target { 1048 // No `target` commands. 1049 Ok(isaspec::IsaSpec::None(settings::Flags::new(flag_builder))) 1050 } else { 1051 Ok(isaspec::IsaSpec::Some(targets)) 1052 } 1053 } 1054 1055 /// Parse a list of target specs. 1056 /// 1057 /// Accept a mix of `target` and `set` command lines. The `set` commands are cumulative. 1058 /// 1059 fn parse_target_specs(&mut self, options: &ParseOptions) -> ParseResult<isaspec::IsaSpec> { 1060 // Were there any `target` commands? 1061 let mut seen_target = false; 1062 // Location of last `set` command since the last `target`. 1063 let mut last_set_loc = None; 1064 1065 let mut targets = Vec::new(); 1066 let mut flag_builder = settings::builder(); 1067 1068 let bool_to_str = |val: bool| { 1069 if val { 1070 "true" 1071 } else { 1072 "false" 1073 } 1074 }; 1075 1076 // default to enabling cfg info 1077 flag_builder 1078 .set( 1079 "machine_code_cfg_info", 1080 bool_to_str(options.machine_code_cfg_info), 1081 ) 1082 .expect("machine_code_cfg_info option should be present"); 1083 1084 flag_builder 1085 .set("unwind_info", bool_to_str(options.unwind_info)) 1086 .expect("unwind_info option should be present"); 1087 1088 while let Some(Token::Identifier(command)) = self.token() { 1089 match command { 1090 "set" => { 1091 last_set_loc = Some(self.loc); 1092 isaspec::parse_options( 1093 self.consume_line().trim().split_whitespace(), 1094 &mut flag_builder, 1095 self.loc, 1096 ) 1097 .map_err(|err| ParseError::from(err))?; 1098 } 1099 "target" => { 1100 let loc = self.loc; 1101 // Grab the whole line so the lexer won't go looking for tokens on the 1102 // following lines. 1103 let mut words = self.consume_line().trim().split_whitespace().peekable(); 1104 // Look for `target foo`. 1105 let target_name = match words.next() { 1106 Some(w) => w, 1107 None => return err!(loc, "expected target triple"), 1108 }; 1109 let triple = match Triple::from_str(target_name) { 1110 Ok(triple) => triple, 1111 Err(err) => return err!(loc, err), 1112 }; 1113 let mut isa_builder = match isa::lookup(triple) { 1114 Err(isa::LookupError::SupportDisabled) => { 1115 continue; 1116 } 1117 Err(isa::LookupError::Unsupported) => { 1118 return warn!(loc, "unsupported target '{}'", target_name); 1119 } 1120 Ok(b) => b, 1121 }; 1122 last_set_loc = None; 1123 seen_target = true; 1124 // Apply the target-specific settings to `isa_builder`. 1125 isaspec::parse_options(words, &mut isa_builder, self.loc)?; 1126 1127 // Construct a trait object with the aggregate settings. 1128 targets.push( 1129 isa_builder 1130 .finish(settings::Flags::new(flag_builder.clone())) 1131 .map_err(|e| ParseError { 1132 location: loc, 1133 message: format!("invalid ISA flags for '{target_name}': {e:?}"), 1134 is_warning: false, 1135 })?, 1136 ); 1137 } 1138 _ => break, 1139 } 1140 } 1141 1142 if !seen_target { 1143 // No `target` commands, but we allow for `set` commands. 1144 Ok(isaspec::IsaSpec::None(settings::Flags::new(flag_builder))) 1145 } else if let Some(loc) = last_set_loc { 1146 err!( 1147 loc, 1148 "dangling 'set' command after ISA specification has no effect." 1149 ) 1150 } else { 1151 Ok(isaspec::IsaSpec::Some(targets)) 1152 } 1153 } 1154 1155 /// Parse a list of expected features that Cranelift should be compiled with, or without. 1156 pub fn parse_cranelift_features(&mut self) -> ParseResult<Vec<Feature<'a>>> { 1157 let mut list = Vec::new(); 1158 while self.token() == Some(Token::Identifier("feature")) { 1159 self.consume(); 1160 let has = !self.optional(Token::Bang); 1161 match (self.token(), has) { 1162 (Some(Token::String(flag)), true) => list.push(Feature::With(flag)), 1163 (Some(Token::String(flag)), false) => list.push(Feature::Without(flag)), 1164 (tok, _) => { 1165 return err!( 1166 self.loc, 1167 format!("Expected feature flag string, got {:?}", tok) 1168 ) 1169 } 1170 } 1171 self.consume(); 1172 } 1173 Ok(list) 1174 } 1175 1176 /// Parse a list of function definitions. 1177 /// 1178 /// This is the top-level parse function matching the whole contents of a file. 1179 pub fn parse_function_list(&mut self) -> ParseResult<Vec<(Function, Details<'a>)>> { 1180 let mut list = Vec::new(); 1181 while self.token().is_some() { 1182 list.push(self.parse_function()?); 1183 } 1184 if let Some(err) = self.lex_error { 1185 return match err { 1186 LexError::InvalidChar => err!(self.loc, "invalid character"), 1187 }; 1188 } 1189 Ok(list) 1190 } 1191 1192 // Parse a whole function definition. 1193 // 1194 // function ::= * "function" name signature "{" preamble function-body "}" 1195 // 1196 fn parse_function(&mut self) -> ParseResult<(Function, Details<'a>)> { 1197 // Begin gathering comments. 1198 // Make sure we don't include any comments before the `function` keyword. 1199 self.token(); 1200 debug_assert!(self.comments.is_empty()); 1201 self.start_gathering_comments(); 1202 1203 self.match_identifier("function", "expected 'function'")?; 1204 1205 let location = self.loc; 1206 1207 // function ::= "function" * name signature "{" preamble function-body "}" 1208 let name = self.parse_user_func_name()?; 1209 1210 // function ::= "function" name * signature "{" preamble function-body "}" 1211 let sig = self.parse_signature()?; 1212 1213 let mut ctx = Context::new(Function::with_name_signature(name, sig)); 1214 1215 // function ::= "function" name signature * "{" preamble function-body "}" 1216 self.match_token(Token::LBrace, "expected '{' before function body")?; 1217 1218 self.token(); 1219 self.claim_gathered_comments(AnyEntity::Function); 1220 1221 // function ::= "function" name signature "{" * preamble function-body "}" 1222 self.parse_preamble(&mut ctx)?; 1223 // function ::= "function" name signature "{" preamble * function-body "}" 1224 self.parse_function_body(&mut ctx)?; 1225 // function ::= "function" name signature "{" preamble function-body * "}" 1226 self.match_token(Token::RBrace, "expected '}' after function body")?; 1227 1228 // Collect any comments following the end of the function, then stop gathering comments. 1229 self.start_gathering_comments(); 1230 self.token(); 1231 self.claim_gathered_comments(AnyEntity::Function); 1232 1233 // Claim all the declared user-defined function names. 1234 for (user_func_ref, user_external_name) in 1235 std::mem::take(&mut self.predeclared_external_names) 1236 { 1237 let actual_ref = ctx 1238 .function 1239 .declare_imported_user_function(user_external_name); 1240 assert_eq!(user_func_ref, actual_ref); 1241 } 1242 1243 let details = Details { 1244 location, 1245 comments: self.take_comments(), 1246 map: ctx.map, 1247 }; 1248 1249 Ok((ctx.function, details)) 1250 } 1251 1252 // Parse a user-defined function name 1253 // 1254 // For example, in a function decl, the parser would be in this state: 1255 // 1256 // function ::= "function" * name signature { ... } 1257 // 1258 fn parse_user_func_name(&mut self) -> ParseResult<UserFuncName> { 1259 match self.token() { 1260 Some(Token::Name(s)) => { 1261 self.consume(); 1262 Ok(UserFuncName::testcase(s)) 1263 } 1264 Some(Token::UserRef(namespace)) => { 1265 self.consume(); 1266 match self.token() { 1267 Some(Token::Colon) => { 1268 self.consume(); 1269 match self.token() { 1270 Some(Token::Integer(index_str)) => { 1271 self.consume(); 1272 let index: u32 = 1273 u32::from_str_radix(index_str, 10).map_err(|_| { 1274 self.error("the integer given overflows the u32 type") 1275 })?; 1276 Ok(UserFuncName::user(namespace, index)) 1277 } 1278 _ => err!(self.loc, "expected integer"), 1279 } 1280 } 1281 _ => { 1282 err!(self.loc, "expected user function name in the form uX:Y") 1283 } 1284 } 1285 } 1286 _ => err!(self.loc, "expected external name"), 1287 } 1288 } 1289 1290 // Parse an external name. 1291 // 1292 // For example, in a function reference decl, the parser would be in this state: 1293 // 1294 // fn0 = * name signature 1295 // 1296 fn parse_external_name(&mut self) -> ParseResult<ExternalName> { 1297 match self.token() { 1298 Some(Token::Name(s)) => { 1299 self.consume(); 1300 s.parse() 1301 .map_err(|_| self.error("invalid test case or libcall name")) 1302 } 1303 1304 Some(Token::UserNameRef(name_ref)) => { 1305 self.consume(); 1306 Ok(ExternalName::user(UserExternalNameRef::new( 1307 name_ref as usize, 1308 ))) 1309 } 1310 1311 Some(Token::UserRef(namespace)) => { 1312 self.consume(); 1313 if let Some(Token::Colon) = self.token() { 1314 self.consume(); 1315 match self.token() { 1316 Some(Token::Integer(index_str)) => { 1317 let index: u32 = u32::from_str_radix(index_str, 10).map_err(|_| { 1318 self.error("the integer given overflows the u32 type") 1319 })?; 1320 self.consume(); 1321 1322 // Deduplicate the reference (O(n), but should be fine for tests), 1323 // to follow `FunctionParameters::declare_imported_user_function`, 1324 // otherwise this will cause ref mismatches when asserted below. 1325 let name_ref = self 1326 .predeclared_external_names 1327 .iter() 1328 .find_map(|(reff, name)| { 1329 if name.index == index && name.namespace == namespace { 1330 Some(reff) 1331 } else { 1332 None 1333 } 1334 }) 1335 .unwrap_or_else(|| { 1336 self.predeclared_external_names 1337 .push(ir::UserExternalName { namespace, index }) 1338 }); 1339 1340 Ok(ExternalName::user(name_ref)) 1341 } 1342 _ => err!(self.loc, "expected integer"), 1343 } 1344 } else { 1345 err!(self.loc, "expected colon") 1346 } 1347 } 1348 1349 _ => err!(self.loc, "expected external name"), 1350 } 1351 } 1352 1353 // Parse a function signature. 1354 // 1355 // signature ::= * "(" [paramlist] ")" ["->" retlist] [callconv] 1356 // 1357 fn parse_signature(&mut self) -> ParseResult<Signature> { 1358 // Calling convention defaults to `fast`, but can be changed. 1359 let mut sig = Signature::new(self.default_calling_convention); 1360 1361 self.match_token(Token::LPar, "expected function signature: ( args... )")?; 1362 // signature ::= "(" * [abi-param-list] ")" ["->" retlist] [callconv] 1363 if self.token() != Some(Token::RPar) { 1364 sig.params = self.parse_abi_param_list()?; 1365 } 1366 self.match_token(Token::RPar, "expected ')' after function arguments")?; 1367 if self.optional(Token::Arrow) { 1368 sig.returns = self.parse_abi_param_list()?; 1369 } 1370 1371 // The calling convention is optional. 1372 match self.token() { 1373 Some(Token::Identifier(text)) => match text.parse() { 1374 Ok(cc) => { 1375 self.consume(); 1376 sig.call_conv = cc; 1377 } 1378 _ => return err!(self.loc, "unknown calling convention: {}", text), 1379 }, 1380 1381 Some(Token::Cold) => { 1382 self.consume(); 1383 sig.call_conv = CallConv::Cold; 1384 } 1385 _ => {} 1386 } 1387 1388 Ok(sig) 1389 } 1390 1391 // Parse list of function parameter / return value types. 1392 // 1393 // paramlist ::= * param { "," param } 1394 // 1395 fn parse_abi_param_list(&mut self) -> ParseResult<Vec<AbiParam>> { 1396 let mut list = Vec::new(); 1397 1398 // abi-param-list ::= * abi-param { "," abi-param } 1399 list.push(self.parse_abi_param()?); 1400 1401 // abi-param-list ::= abi-param * { "," abi-param } 1402 while self.optional(Token::Comma) { 1403 // abi-param-list ::= abi-param { "," * abi-param } 1404 list.push(self.parse_abi_param()?); 1405 } 1406 1407 Ok(list) 1408 } 1409 1410 // Parse a single argument type with flags. 1411 fn parse_abi_param(&mut self) -> ParseResult<AbiParam> { 1412 // abi-param ::= * type { flag } 1413 let mut arg = AbiParam::new(self.match_type("expected parameter type")?); 1414 1415 // abi-param ::= type * { flag } 1416 while let Some(Token::Identifier(s)) = self.token() { 1417 match s { 1418 "uext" => arg.extension = ArgumentExtension::Uext, 1419 "sext" => arg.extension = ArgumentExtension::Sext, 1420 "sarg" => { 1421 self.consume(); 1422 self.match_token(Token::LPar, "expected '(' to begin sarg size")?; 1423 let size = self.match_uimm32("expected byte-size in sarg decl")?; 1424 self.match_token(Token::RPar, "expected ')' to end sarg size")?; 1425 arg.purpose = ArgumentPurpose::StructArgument(size.into()); 1426 continue; 1427 } 1428 _ => { 1429 if let Ok(purpose) = s.parse() { 1430 arg.purpose = purpose; 1431 } else { 1432 break; 1433 } 1434 } 1435 } 1436 self.consume(); 1437 } 1438 1439 Ok(arg) 1440 } 1441 1442 // Parse the function preamble. 1443 // 1444 // preamble ::= * { preamble-decl } 1445 // preamble-decl ::= * stack-slot-decl 1446 // * function-decl 1447 // * signature-decl 1448 // * jump-table-decl 1449 // * stack-limit-decl 1450 // 1451 // The parsed decls are added to `ctx` rather than returned. 1452 fn parse_preamble(&mut self, ctx: &mut Context) -> ParseResult<()> { 1453 loop { 1454 match self.token() { 1455 Some(Token::StackSlot(..)) => { 1456 self.start_gathering_comments(); 1457 let loc = self.loc; 1458 self.parse_stack_slot_decl() 1459 .and_then(|(ss, dat)| ctx.add_ss(ss, dat, loc)) 1460 } 1461 Some(Token::DynamicStackSlot(..)) => { 1462 self.start_gathering_comments(); 1463 let loc = self.loc; 1464 self.parse_dynamic_stack_slot_decl() 1465 .and_then(|(dss, dat)| ctx.add_dss(dss, dat, loc)) 1466 } 1467 Some(Token::DynamicType(..)) => { 1468 self.start_gathering_comments(); 1469 let loc = self.loc; 1470 self.parse_dynamic_type_decl() 1471 .and_then(|(dt, dat)| ctx.add_dt(dt, dat, loc)) 1472 } 1473 Some(Token::GlobalValue(..)) => { 1474 self.start_gathering_comments(); 1475 self.parse_global_value_decl() 1476 .and_then(|(gv, dat, maybe_fact)| ctx.add_gv(gv, dat, maybe_fact, self.loc)) 1477 } 1478 Some(Token::MemoryType(..)) => { 1479 self.start_gathering_comments(); 1480 self.parse_memory_type_decl() 1481 .and_then(|(mt, dat)| ctx.add_mt(mt, dat, self.loc)) 1482 } 1483 Some(Token::SigRef(..)) => { 1484 self.start_gathering_comments(); 1485 self.parse_signature_decl().and_then(|(sig, dat)| { 1486 ctx.add_sig(sig, dat, self.loc, self.default_calling_convention) 1487 }) 1488 } 1489 Some(Token::FuncRef(..)) => { 1490 self.start_gathering_comments(); 1491 self.parse_function_decl(ctx) 1492 .and_then(|(fn_, dat)| ctx.add_fn(fn_, dat, self.loc)) 1493 } 1494 Some(Token::Constant(..)) => { 1495 self.start_gathering_comments(); 1496 self.parse_constant_decl() 1497 .and_then(|(c, v)| ctx.add_constant(c, v, self.loc)) 1498 } 1499 Some(Token::Identifier("stack_limit")) => { 1500 self.start_gathering_comments(); 1501 self.parse_stack_limit_decl() 1502 .and_then(|gv| ctx.add_stack_limit(gv, self.loc)) 1503 } 1504 // More to come.. 1505 _ => return Ok(()), 1506 }?; 1507 } 1508 } 1509 1510 // Parse a stack slot decl. 1511 // 1512 // stack-slot-decl ::= * StackSlot(ss) "=" stack-slot-kind Bytes {"," stack-slot-flag} 1513 // stack-slot-kind ::= "explicit_slot" 1514 // | "spill_slot" 1515 // | "incoming_arg" 1516 // | "outgoing_arg" 1517 // stack-slot-flag ::= "align" "=" Bytes 1518 fn parse_stack_slot_decl(&mut self) -> ParseResult<(StackSlot, StackSlotData)> { 1519 let ss = self.match_ss("expected stack slot number: ss«n»")?; 1520 self.match_token(Token::Equal, "expected '=' in stack slot declaration")?; 1521 let kind = self.match_enum("expected stack slot kind")?; 1522 1523 // stack-slot-decl ::= StackSlot(ss) "=" stack-slot-kind * Bytes {"," stack-slot-flag} 1524 let bytes: i64 = self 1525 .match_imm64("expected byte-size in stack_slot decl")? 1526 .into(); 1527 if bytes < 0 { 1528 return err!(self.loc, "negative stack slot size"); 1529 } 1530 if bytes > i64::from(u32::MAX) { 1531 return err!(self.loc, "stack slot too large"); 1532 } 1533 1534 // Parse flags. 1535 let align = if self.token() == Some(Token::Comma) { 1536 self.consume(); 1537 self.match_token( 1538 Token::Identifier("align"), 1539 "expected a valid stack-slot flag (currently only `align`)", 1540 )?; 1541 self.match_token(Token::Equal, "expected `=` after flag")?; 1542 let align: i64 = self 1543 .match_imm64("expected alignment-size after `align` flag")? 1544 .into(); 1545 u32::try_from(align) 1546 .map_err(|_| self.error("alignment must be a 32-bit unsigned integer"))? 1547 } else { 1548 1 1549 }; 1550 1551 if !align.is_power_of_two() { 1552 return err!(self.loc, "stack slot alignment is not a power of two"); 1553 } 1554 let align_shift = u8::try_from(align.ilog2()).unwrap(); // Always succeeds: range 0..=31. 1555 1556 let data = StackSlotData::new(kind, bytes as u32, align_shift); 1557 1558 // Collect any trailing comments. 1559 self.token(); 1560 self.claim_gathered_comments(ss); 1561 1562 // TBD: stack-slot-decl ::= StackSlot(ss) "=" stack-slot-kind Bytes * {"," stack-slot-flag} 1563 Ok((ss, data)) 1564 } 1565 1566 fn parse_dynamic_stack_slot_decl( 1567 &mut self, 1568 ) -> ParseResult<(DynamicStackSlot, DynamicStackSlotData)> { 1569 let dss = self.match_dss("expected stack slot number: dss«n»")?; 1570 self.match_token(Token::Equal, "expected '=' in stack slot declaration")?; 1571 let kind = self.match_enum("expected stack slot kind")?; 1572 let dt = self.match_dt("expected dynamic type")?; 1573 let data = DynamicStackSlotData::new(kind, dt); 1574 // Collect any trailing comments. 1575 self.token(); 1576 self.claim_gathered_comments(dss); 1577 1578 // TBD: stack-slot-decl ::= StackSlot(ss) "=" stack-slot-kind Bytes * {"," stack-slot-flag} 1579 Ok((dss, data)) 1580 } 1581 1582 fn parse_dynamic_type_decl(&mut self) -> ParseResult<(DynamicType, DynamicTypeData)> { 1583 let dt = self.match_dt("expected dynamic type number: dt«n»")?; 1584 self.match_token(Token::Equal, "expected '=' in stack slot declaration")?; 1585 let vector_base_ty = self.match_type("expected base type")?; 1586 assert!(vector_base_ty.is_vector(), "expected vector type"); 1587 self.match_token( 1588 Token::Multiply, 1589 "expected '*' followed by a dynamic scale value", 1590 )?; 1591 let dyn_scale = self.match_gv("expected dynamic scale global value")?; 1592 let data = DynamicTypeData::new(vector_base_ty, dyn_scale); 1593 // Collect any trailing comments. 1594 self.token(); 1595 self.claim_gathered_comments(dt); 1596 Ok((dt, data)) 1597 } 1598 1599 // Parse a global value decl. 1600 // 1601 // global-val-decl ::= * GlobalValue(gv) [ "!" fact ] "=" global-val-desc 1602 // global-val-desc ::= "vmctx" 1603 // | "load" "." type "notrap" "aligned" GlobalValue(base) [offset] 1604 // | "iadd_imm" "(" GlobalValue(base) ")" imm64 1605 // | "symbol" ["colocated"] name + imm64 1606 // | "dyn_scale_target_const" "." type 1607 // 1608 fn parse_global_value_decl( 1609 &mut self, 1610 ) -> ParseResult<(GlobalValue, GlobalValueData, Option<Fact>)> { 1611 let gv = self.match_gv("expected global value number: gv«n»")?; 1612 1613 let fact = if self.token() == Some(Token::Bang) { 1614 self.consume(); 1615 Some(self.parse_fact()?) 1616 } else { 1617 None 1618 }; 1619 1620 self.match_token(Token::Equal, "expected '=' in global value declaration")?; 1621 1622 let data = match self.match_any_identifier("expected global value kind")? { 1623 "vmctx" => GlobalValueData::VMContext, 1624 "load" => { 1625 self.match_token( 1626 Token::Dot, 1627 "expected '.' followed by type in load global value decl", 1628 )?; 1629 let global_type = self.match_type("expected load type")?; 1630 let flags = self.optional_memflags()?; 1631 let base = self.match_gv("expected global value: gv«n»")?; 1632 let offset = self.optional_offset32()?; 1633 1634 if !(flags.notrap() && flags.aligned()) { 1635 return err!(self.loc, "global-value load must be notrap and aligned"); 1636 } 1637 GlobalValueData::Load { 1638 base, 1639 offset, 1640 global_type, 1641 flags, 1642 } 1643 } 1644 "iadd_imm" => { 1645 self.match_token( 1646 Token::Dot, 1647 "expected '.' followed by type in iadd_imm global value decl", 1648 )?; 1649 let global_type = self.match_type("expected iadd type")?; 1650 let base = self.match_gv("expected global value: gv«n»")?; 1651 self.match_token( 1652 Token::Comma, 1653 "expected ',' followed by rhs in iadd_imm global value decl", 1654 )?; 1655 let offset = self.match_imm64("expected iadd_imm immediate")?; 1656 GlobalValueData::IAddImm { 1657 base, 1658 offset, 1659 global_type, 1660 } 1661 } 1662 "symbol" => { 1663 let colocated = self.optional(Token::Identifier("colocated")); 1664 let tls = self.optional(Token::Identifier("tls")); 1665 let name = self.parse_external_name()?; 1666 let offset = self.optional_offset_imm64()?; 1667 GlobalValueData::Symbol { 1668 name, 1669 offset, 1670 colocated, 1671 tls, 1672 } 1673 } 1674 "dyn_scale_target_const" => { 1675 self.match_token( 1676 Token::Dot, 1677 "expected '.' followed by type in dynamic scale global value decl", 1678 )?; 1679 let vector_type = self.match_type("expected load type")?; 1680 assert!(vector_type.is_vector(), "Expected vector type"); 1681 GlobalValueData::DynScaleTargetConst { vector_type } 1682 } 1683 other => return err!(self.loc, "Unknown global value kind '{}'", other), 1684 }; 1685 1686 // Collect any trailing comments. 1687 self.token(); 1688 self.claim_gathered_comments(gv); 1689 1690 Ok((gv, data, fact)) 1691 } 1692 1693 // Parse one field definition in a memory-type struct decl. 1694 // 1695 // memory-type-field ::= offset ":" type ["readonly"] [ "!" fact ] 1696 // offset ::= uimm64 1697 fn parse_memory_type_field(&mut self) -> ParseResult<MemoryTypeField> { 1698 let offset: u64 = self 1699 .match_uimm64( 1700 "expected u64 constant value for field offset in struct memory-type declaration", 1701 )? 1702 .into(); 1703 self.match_token( 1704 Token::Colon, 1705 "expected colon after field offset in struct memory-type declaration", 1706 )?; 1707 let ty = self.match_type("expected type for field in struct memory-type declaration")?; 1708 let readonly = if self.token() == Some(Token::Identifier("readonly")) { 1709 self.consume(); 1710 true 1711 } else { 1712 false 1713 }; 1714 let fact = if self.token() == Some(Token::Bang) { 1715 self.consume(); 1716 let fact = self.parse_fact()?; 1717 Some(fact) 1718 } else { 1719 None 1720 }; 1721 Ok(MemoryTypeField { 1722 offset, 1723 ty, 1724 readonly, 1725 fact, 1726 }) 1727 } 1728 1729 // Parse a memory-type decl. 1730 // 1731 // memory-type-decl ::= MemoryType(mt) "=" memory-type-desc 1732 // memory-type-desc ::= "struct" size "{" memory-type-field,* "}" 1733 // | "memory" size 1734 // | "dynamic_memory" GlobalValue "+" offset 1735 // | "empty" 1736 // size ::= uimm64 1737 // offset ::= uimm64 1738 fn parse_memory_type_decl(&mut self) -> ParseResult<(MemoryType, MemoryTypeData)> { 1739 let mt = self.match_mt("expected memory type number: mt«n»")?; 1740 self.match_token(Token::Equal, "expected '=' in memory type declaration")?; 1741 1742 let data = match self.token() { 1743 Some(Token::Identifier("struct")) => { 1744 self.consume(); 1745 let size: u64 = self.match_uimm64("expected u64 constant value for struct size in struct memory-type declaration")?.into(); 1746 self.match_token(Token::LBrace, "expected opening brace to start struct fields in struct memory-type declaration")?; 1747 let mut fields = vec![]; 1748 while self.token() != Some(Token::RBrace) { 1749 let field = self.parse_memory_type_field()?; 1750 fields.push(field); 1751 if self.token() == Some(Token::Comma) { 1752 self.consume(); 1753 } else { 1754 break; 1755 } 1756 } 1757 self.match_token( 1758 Token::RBrace, 1759 "expected closing brace after struct fields in struct memory-type declaration", 1760 )?; 1761 MemoryTypeData::Struct { size, fields } 1762 } 1763 Some(Token::Identifier("memory")) => { 1764 self.consume(); 1765 let size: u64 = self.match_uimm64("expected u64 constant value for size in static-memory memory-type declaration")?.into(); 1766 MemoryTypeData::Memory { size } 1767 } 1768 Some(Token::Identifier("dynamic_memory")) => { 1769 self.consume(); 1770 let gv = self.match_gv( 1771 "expected a global value for `dynamic_memory` memory-type declaration", 1772 )?; 1773 self.match_token( 1774 Token::Plus, 1775 "expected `+` after global value in `dynamic_memory` memory-type declaration", 1776 )?; 1777 let size: u64 = self.match_uimm64("expected u64 constant value for size offset in `dynamic_memory` memory-type declaration")?.into(); 1778 MemoryTypeData::DynamicMemory { gv, size } 1779 } 1780 Some(Token::Identifier("empty")) => { 1781 self.consume(); 1782 MemoryTypeData::Empty 1783 } 1784 other => { 1785 return err!( 1786 self.loc, 1787 "Unknown memory type declaration kind '{:?}'", 1788 other 1789 ) 1790 } 1791 }; 1792 1793 // Collect any trailing comments. 1794 self.token(); 1795 self.claim_gathered_comments(mt); 1796 1797 Ok((mt, data)) 1798 } 1799 1800 // Parse a signature decl. 1801 // 1802 // signature-decl ::= SigRef(sigref) "=" signature 1803 // 1804 fn parse_signature_decl(&mut self) -> ParseResult<(SigRef, Signature)> { 1805 let sig = self.match_sig("expected signature number: sig«n»")?; 1806 self.match_token(Token::Equal, "expected '=' in signature decl")?; 1807 let data = self.parse_signature()?; 1808 1809 // Collect any trailing comments. 1810 self.token(); 1811 self.claim_gathered_comments(sig); 1812 1813 Ok((sig, data)) 1814 } 1815 1816 // Parse a function decl. 1817 // 1818 // Two variants: 1819 // 1820 // function-decl ::= FuncRef(fnref) "=" ["colocated"]" name function-decl-sig 1821 // function-decl-sig ::= SigRef(sig) | signature 1822 // 1823 // The first variant allocates a new signature reference. The second references an existing 1824 // signature which must be declared first. 1825 // 1826 fn parse_function_decl(&mut self, ctx: &mut Context) -> ParseResult<(FuncRef, ExtFuncData)> { 1827 let fn_ = self.match_fn("expected function number: fn«n»")?; 1828 self.match_token(Token::Equal, "expected '=' in function decl")?; 1829 1830 let loc = self.loc; 1831 1832 // function-decl ::= FuncRef(fnref) "=" * ["colocated"] name function-decl-sig 1833 let colocated = self.optional(Token::Identifier("colocated")); 1834 1835 // function-decl ::= FuncRef(fnref) "=" ["colocated"] * name function-decl-sig 1836 let name = self.parse_external_name()?; 1837 1838 // function-decl ::= FuncRef(fnref) "=" ["colocated"] name * function-decl-sig 1839 let data = match self.token() { 1840 Some(Token::LPar) => { 1841 // function-decl ::= FuncRef(fnref) "=" ["colocated"] name * signature 1842 let sig = self.parse_signature()?; 1843 let sigref = ctx.function.import_signature(sig); 1844 ctx.map 1845 .def_entity(sigref.into(), loc) 1846 .expect("duplicate SigRef entities created"); 1847 ExtFuncData { 1848 name, 1849 signature: sigref, 1850 colocated, 1851 } 1852 } 1853 Some(Token::SigRef(sig_src)) => { 1854 let sig = match SigRef::with_number(sig_src) { 1855 None => { 1856 return err!(self.loc, "attempted to use invalid signature ss{}", sig_src); 1857 } 1858 Some(sig) => sig, 1859 }; 1860 ctx.check_sig(sig, self.loc)?; 1861 self.consume(); 1862 ExtFuncData { 1863 name, 1864 signature: sig, 1865 colocated, 1866 } 1867 } 1868 _ => return err!(self.loc, "expected 'function' or sig«n» in function decl"), 1869 }; 1870 1871 // Collect any trailing comments. 1872 self.token(); 1873 self.claim_gathered_comments(fn_); 1874 1875 Ok((fn_, data)) 1876 } 1877 1878 // Parse a jump table literal. 1879 // 1880 // jump-table-lit ::= "[" block(args) {"," block(args) } "]" 1881 // | "[]" 1882 fn parse_jump_table( 1883 &mut self, 1884 ctx: &mut Context, 1885 def: ir::BlockCall, 1886 ) -> ParseResult<ir::JumpTable> { 1887 self.match_token(Token::LBracket, "expected '[' before jump table contents")?; 1888 1889 let mut data = Vec::new(); 1890 1891 match self.token() { 1892 Some(Token::Block(dest)) => { 1893 self.consume(); 1894 let args = self.parse_opt_block_call_args()?; 1895 data.push(ctx.function.dfg.block_call(dest, &args)); 1896 1897 loop { 1898 match self.token() { 1899 Some(Token::Comma) => { 1900 self.consume(); 1901 if let Some(Token::Block(dest)) = self.token() { 1902 self.consume(); 1903 let args = self.parse_opt_block_call_args()?; 1904 data.push(ctx.function.dfg.block_call(dest, &args)); 1905 } else { 1906 return err!(self.loc, "expected jump_table entry"); 1907 } 1908 } 1909 Some(Token::RBracket) => break, 1910 _ => return err!(self.loc, "expected ']' after jump table contents"), 1911 } 1912 } 1913 } 1914 Some(Token::RBracket) => (), 1915 _ => return err!(self.loc, "expected jump_table entry"), 1916 } 1917 1918 self.consume(); 1919 1920 Ok(ctx 1921 .function 1922 .dfg 1923 .jump_tables 1924 .push(JumpTableData::new(def, &data))) 1925 } 1926 1927 // Parse an exception-table decl. 1928 // 1929 // exception-table ::= * SigRef(sig) "," BlockCall "," "[" (exception-table-entry ( "," exception-table-entry )*)? "]" 1930 // exception-table-entry ::= * ExceptionTag(tag) ":" BlockCall 1931 // * "default" ":" BlockCall 1932 fn parse_exception_table(&mut self, ctx: &mut Context) -> ParseResult<ir::ExceptionTable> { 1933 let sig = self.match_sig("expected signature of called function")?; 1934 self.match_token(Token::Comma, "expected comma after signature argument")?; 1935 1936 let mut tags_and_targets = vec![]; 1937 1938 let block_num = self.match_block("expected branch destination block")?; 1939 let args = self.parse_opt_block_call_args()?; 1940 let normal_return = ctx.function.dfg.block_call(block_num, &args); 1941 1942 self.match_token( 1943 Token::Comma, 1944 "expected comma after normal-return destination", 1945 )?; 1946 1947 match self.token() { 1948 Some(Token::LBracket) => { 1949 self.consume(); 1950 loop { 1951 if let Some(Token::RBracket) = self.token() { 1952 break; 1953 } 1954 1955 let tag = match self.token() { 1956 Some(Token::ExceptionTag(tag)) => { 1957 self.consume(); 1958 Some(ir::ExceptionTag::from_u32(tag)) 1959 } 1960 Some(Token::Identifier("default")) => { 1961 self.consume(); 1962 None 1963 } 1964 _ => return err!(self.loc, "invalid token"), 1965 }; 1966 self.match_token(Token::Colon, "expected ':' after exception tag")?; 1967 1968 let block_num = self.match_block("expected branch destination block")?; 1969 let args = self.parse_opt_block_call_args()?; 1970 let block_call = ctx.function.dfg.block_call(block_num, &args); 1971 1972 tags_and_targets.push((tag, block_call)); 1973 1974 if let Some(Token::Comma) = self.token() { 1975 self.consume(); 1976 } else { 1977 break; 1978 } 1979 } 1980 self.match_token(Token::RBracket, "expected closing bracket")?; 1981 } 1982 _ => {} 1983 }; 1984 1985 Ok(ctx 1986 .function 1987 .dfg 1988 .exception_tables 1989 .push(ir::ExceptionTableData::new( 1990 sig, 1991 normal_return, 1992 tags_and_targets, 1993 ))) 1994 } 1995 1996 // Parse a constant decl. 1997 // 1998 // constant-decl ::= * Constant(c) "=" ty? "[" literal {"," literal} "]" 1999 fn parse_constant_decl(&mut self) -> ParseResult<(Constant, ConstantData)> { 2000 let name = self.match_constant()?; 2001 self.match_token(Token::Equal, "expected '=' in constant decl")?; 2002 let data = if let Some(Token::Type(_)) = self.token() { 2003 let ty = self.match_type("expected type of constant")?; 2004 self.match_uimm128(ty) 2005 } else { 2006 self.match_hexadecimal_constant("expected an immediate hexadecimal operand") 2007 }?; 2008 2009 // Collect any trailing comments. 2010 self.token(); 2011 self.claim_gathered_comments(name); 2012 2013 Ok((name, data)) 2014 } 2015 2016 // Parse a stack limit decl 2017 // 2018 // stack-limit-decl ::= * StackLimit "=" GlobalValue(gv) 2019 fn parse_stack_limit_decl(&mut self) -> ParseResult<GlobalValue> { 2020 self.match_stack_limit()?; 2021 self.match_token(Token::Equal, "expected '=' in stack limit decl")?; 2022 let limit = match self.token() { 2023 Some(Token::GlobalValue(base_num)) => match GlobalValue::with_number(base_num) { 2024 Some(gv) => gv, 2025 None => return err!(self.loc, "invalid global value number for stack limit"), 2026 }, 2027 _ => return err!(self.loc, "expected global value"), 2028 }; 2029 self.consume(); 2030 2031 // Collect any trailing comments. 2032 self.token(); 2033 self.claim_gathered_comments(AnyEntity::StackLimit); 2034 2035 Ok(limit) 2036 } 2037 2038 // Parse a function body, add contents to `ctx`. 2039 // 2040 // function-body ::= * { extended-basic-block } 2041 // 2042 fn parse_function_body(&mut self, ctx: &mut Context) -> ParseResult<()> { 2043 while self.token() != Some(Token::RBrace) { 2044 self.parse_basic_block(ctx)?; 2045 } 2046 2047 // Now that we've seen all defined values in the function, ensure that 2048 // all references refer to a definition. 2049 for block in &ctx.function.layout { 2050 for inst in ctx.function.layout.block_insts(block) { 2051 for value in ctx.function.dfg.inst_values(inst) { 2052 if !ctx.map.contains_value(value) { 2053 return err!( 2054 ctx.map.location(AnyEntity::Inst(inst)).unwrap(), 2055 "undefined operand value {}", 2056 value 2057 ); 2058 } 2059 } 2060 } 2061 } 2062 2063 for alias in &ctx.aliases { 2064 if !ctx.function.dfg.set_alias_type_for_parser(*alias) { 2065 let loc = ctx.map.location(AnyEntity::Value(*alias)).unwrap(); 2066 return err!(loc, "alias cycle involving {}", alias); 2067 } 2068 } 2069 2070 Ok(()) 2071 } 2072 2073 // Parse a basic block, add contents to `ctx`. 2074 // 2075 // extended-basic-block ::= * block-header { instruction } 2076 // block-header ::= Block(block) [block-params] [block-flags] ":" 2077 // block-flags ::= [Cold] 2078 // 2079 fn parse_basic_block(&mut self, ctx: &mut Context) -> ParseResult<()> { 2080 // Collect comments for the next block. 2081 self.start_gathering_comments(); 2082 2083 let block_num = self.match_block("expected block header")?; 2084 let block = ctx.add_block(block_num, self.loc)?; 2085 2086 if block_num.as_u32() >= MAX_BLOCKS_IN_A_FUNCTION { 2087 return Err(self.error("too many blocks")); 2088 } 2089 2090 if self.token() == Some(Token::LPar) { 2091 self.parse_block_params(ctx, block)?; 2092 } 2093 2094 if self.optional(Token::Cold) { 2095 ctx.set_cold_block(block); 2096 } 2097 2098 self.match_token(Token::Colon, "expected ':' after block parameters")?; 2099 2100 // Collect any trailing comments. 2101 self.token(); 2102 self.claim_gathered_comments(block); 2103 2104 // extended-basic-block ::= block-header * { instruction } 2105 while match self.token() { 2106 Some(Token::Value(_)) 2107 | Some(Token::Identifier(_)) 2108 | Some(Token::LBracket) 2109 | Some(Token::SourceLoc(_)) => true, 2110 _ => false, 2111 } { 2112 let srcloc = self.optional_srcloc()?; 2113 2114 // We need to parse instruction results here because they are shared 2115 // between the parsing of value aliases and the parsing of instructions. 2116 // 2117 // inst-results ::= Value(v) { "," Value(v) } 2118 let results = self.parse_inst_results(ctx)?; 2119 2120 for result in &results { 2121 while ctx.function.dfg.num_values() <= result.index() { 2122 ctx.function.dfg.make_invalid_value_for_parser(); 2123 } 2124 } 2125 2126 match self.token() { 2127 Some(Token::Arrow) => { 2128 self.consume(); 2129 self.parse_value_alias(&results, ctx)?; 2130 } 2131 Some(Token::Equal) => { 2132 self.consume(); 2133 self.parse_instruction(&results, srcloc, ctx, block)?; 2134 } 2135 _ if !results.is_empty() => return err!(self.loc, "expected -> or ="), 2136 _ => self.parse_instruction(&results, srcloc, ctx, block)?, 2137 } 2138 } 2139 2140 Ok(()) 2141 } 2142 2143 // Parse parenthesized list of block parameters. 2144 // 2145 // block-params ::= * "(" ( block-param { "," block-param } )? ")" 2146 fn parse_block_params(&mut self, ctx: &mut Context, block: Block) -> ParseResult<()> { 2147 // block-params ::= * "(" ( block-param { "," block-param } )? ")" 2148 self.match_token(Token::LPar, "expected '(' before block parameters")?; 2149 2150 // block-params ::= "(" * ")" 2151 if self.token() == Some(Token::RPar) { 2152 self.consume(); 2153 return Ok(()); 2154 } 2155 2156 // block-params ::= "(" * block-param { "," block-param } ")" 2157 self.parse_block_param(ctx, block)?; 2158 2159 // block-params ::= "(" block-param * { "," block-param } ")" 2160 while self.optional(Token::Comma) { 2161 // block-params ::= "(" block-param { "," * block-param } ")" 2162 self.parse_block_param(ctx, block)?; 2163 } 2164 2165 // block-params ::= "(" block-param { "," block-param } * ")" 2166 self.match_token(Token::RPar, "expected ')' after block parameters")?; 2167 2168 Ok(()) 2169 } 2170 2171 // Parse a single block parameter declaration, and append it to `block`. 2172 // 2173 // block-param ::= * Value(v) [ "!" fact ] ":" Type(t) arg-loc? 2174 // arg-loc ::= "[" value-location "]" 2175 // 2176 fn parse_block_param(&mut self, ctx: &mut Context, block: Block) -> ParseResult<()> { 2177 // block-param ::= * Value(v) [ "!" fact ] ":" Type(t) arg-loc? 2178 let v = self.match_value("block argument must be a value")?; 2179 let v_location = self.loc; 2180 // block-param ::= Value(v) * [ "!" fact ] ":" Type(t) arg-loc? 2181 let fact = if self.token() == Some(Token::Bang) { 2182 self.consume(); 2183 // block-param ::= Value(v) [ "!" * fact ] ":" Type(t) arg-loc? 2184 Some(self.parse_fact()?) 2185 } else { 2186 None 2187 }; 2188 self.match_token(Token::Colon, "expected ':' after block argument")?; 2189 // block-param ::= Value(v) [ "!" fact ] ":" * Type(t) arg-loc? 2190 2191 while ctx.function.dfg.num_values() <= v.index() { 2192 ctx.function.dfg.make_invalid_value_for_parser(); 2193 } 2194 2195 let t = self.match_type("expected block argument type")?; 2196 // Allocate the block argument. 2197 ctx.function.dfg.append_block_param_for_parser(block, t, v); 2198 ctx.map.def_value(v, v_location)?; 2199 ctx.function.dfg.facts[v] = fact; 2200 2201 Ok(()) 2202 } 2203 2204 // Parse a "fact" for proof-carrying code, attached to a value. 2205 // 2206 // fact ::= "range" "(" bit-width "," min-value "," max-value ")" 2207 // | "dynamic_range" "(" bit-width "," expr "," expr ")" 2208 // | "mem" "(" memory-type "," mt-offset "," mt-offset [ "," "nullable" ] ")" 2209 // | "dynamic_mem" "(" memory-type "," expr "," expr [ "," "nullable" ] ")" 2210 // | "conflict" 2211 // bit-width ::= uimm64 2212 // min-value ::= uimm64 2213 // max-value ::= uimm64 2214 // valid-range ::= uimm64 2215 // mt-offset ::= uimm64 2216 fn parse_fact(&mut self) -> ParseResult<Fact> { 2217 match self.token() { 2218 Some(Token::Identifier("range")) => { 2219 self.consume(); 2220 self.match_token(Token::LPar, "`range` fact needs an opening `(`")?; 2221 let bit_width: u64 = self 2222 .match_uimm64("expected a bit-width value for `range` fact")? 2223 .into(); 2224 self.match_token(Token::Comma, "expected a comma")?; 2225 let min: u64 = self 2226 .match_uimm64("expected a min value for `range` fact")? 2227 .into(); 2228 self.match_token(Token::Comma, "expected a comma")?; 2229 let max: u64 = self 2230 .match_uimm64("expected a max value for `range` fact")? 2231 .into(); 2232 self.match_token(Token::RPar, "`range` fact needs a closing `)`")?; 2233 let bit_width_max = match bit_width { 2234 x if x > 64 => { 2235 return Err(self.error("bitwidth must be <= 64 bits on a `range` fact")); 2236 } 2237 64 => u64::MAX, 2238 x => (1u64 << x) - 1, 2239 }; 2240 if min > max { 2241 return Err(self.error( 2242 "min value must be less than or equal to max value on a `range` fact", 2243 )); 2244 } 2245 if max > bit_width_max { 2246 return Err( 2247 self.error("max value is out of range for bitwidth on a `range` fact") 2248 ); 2249 } 2250 Ok(Fact::Range { 2251 bit_width: u16::try_from(bit_width).unwrap(), 2252 min: min.into(), 2253 max: max.into(), 2254 }) 2255 } 2256 Some(Token::Identifier("dynamic_range")) => { 2257 self.consume(); 2258 self.match_token(Token::LPar, "`dynamic_range` fact needs an opening `(`")?; 2259 let bit_width: u64 = self 2260 .match_uimm64("expected a bit-width value for `dynamic_range` fact")? 2261 .into(); 2262 self.match_token(Token::Comma, "expected a comma")?; 2263 let min = self.parse_expr()?; 2264 self.match_token(Token::Comma, "expected a comma")?; 2265 let max = self.parse_expr()?; 2266 self.match_token(Token::RPar, "`dynamic_range` fact needs a closing `)`")?; 2267 Ok(Fact::DynamicRange { 2268 bit_width: u16::try_from(bit_width).unwrap(), 2269 min, 2270 max, 2271 }) 2272 } 2273 Some(Token::Identifier("mem")) => { 2274 self.consume(); 2275 self.match_token(Token::LPar, "expected a `(`")?; 2276 let ty = self.match_mt("expected a memory type for `mem` fact")?; 2277 self.match_token( 2278 Token::Comma, 2279 "expected a comma after memory type in `mem` fact", 2280 )?; 2281 let min_offset: u64 = self 2282 .match_uimm64("expected a uimm64 minimum pointer offset for `mem` fact")? 2283 .into(); 2284 self.match_token(Token::Comma, "expected a comma after offset in `mem` fact")?; 2285 let max_offset: u64 = self 2286 .match_uimm64("expected a uimm64 maximum pointer offset for `mem` fact")? 2287 .into(); 2288 let nullable = if self.token() == Some(Token::Comma) { 2289 self.consume(); 2290 self.match_token( 2291 Token::Identifier("nullable"), 2292 "expected `nullable` in last optional field of `dynamic_mem`", 2293 )?; 2294 true 2295 } else { 2296 false 2297 }; 2298 self.match_token(Token::RPar, "expected a `)`")?; 2299 Ok(Fact::Mem { 2300 ty, 2301 min_offset, 2302 max_offset, 2303 nullable, 2304 }) 2305 } 2306 Some(Token::Identifier("dynamic_mem")) => { 2307 self.consume(); 2308 self.match_token(Token::LPar, "expected a `(`")?; 2309 let ty = self.match_mt("expected a memory type for `dynamic_mem` fact")?; 2310 self.match_token( 2311 Token::Comma, 2312 "expected a comma after memory type in `dynamic_mem` fact", 2313 )?; 2314 let min = self.parse_expr()?; 2315 self.match_token( 2316 Token::Comma, 2317 "expected a comma after offset in `dynamic_mem` fact", 2318 )?; 2319 let max = self.parse_expr()?; 2320 let nullable = if self.token() == Some(Token::Comma) { 2321 self.consume(); 2322 self.match_token( 2323 Token::Identifier("nullable"), 2324 "expected `nullable` in last optional field of `dynamic_mem`", 2325 )?; 2326 true 2327 } else { 2328 false 2329 }; 2330 self.match_token(Token::RPar, "expected a `)`")?; 2331 Ok(Fact::DynamicMem { 2332 ty, 2333 min, 2334 max, 2335 nullable, 2336 }) 2337 } 2338 Some(Token::Identifier("def")) => { 2339 self.consume(); 2340 self.match_token(Token::LPar, "expected a `(`")?; 2341 let value = self.match_value("expected a value number in `def` fact")?; 2342 self.match_token(Token::RPar, "expected a `)`")?; 2343 Ok(Fact::Def { value }) 2344 } 2345 Some(Token::Identifier("compare")) => { 2346 self.consume(); 2347 self.match_token(Token::LPar, "expected a `(`")?; 2348 let kind = self.match_enum("expected intcc condition code in `compare` fact")?; 2349 self.match_token( 2350 Token::Comma, 2351 "expected comma in `compare` fact after condition code", 2352 )?; 2353 let lhs = self.parse_expr()?; 2354 self.match_token(Token::Comma, "expected comma in `compare` fact after LHS")?; 2355 let rhs = self.parse_expr()?; 2356 self.match_token(Token::RPar, "expected a `)`")?; 2357 Ok(Fact::Compare { kind, lhs, rhs }) 2358 } 2359 Some(Token::Identifier("conflict")) => { 2360 self.consume(); 2361 Ok(Fact::Conflict) 2362 } 2363 _ => Err(self.error( 2364 "expected a `range`, 'dynamic_range', `mem`, `dynamic_mem`, `def`, `compare` or `conflict` fact", 2365 )), 2366 } 2367 } 2368 2369 // Parse a dynamic expression used in some kinds of PCC facts. 2370 // 2371 // expr ::= base-expr 2372 // | base-expr + uimm64 // but in-range for imm64 2373 // | base-expr - uimm64 // but in-range for imm64 2374 // | imm64 2375 fn parse_expr(&mut self) -> ParseResult<Expr> { 2376 if let Some(Token::Integer(_)) = self.token() { 2377 let offset: i64 = self 2378 .match_imm64("expected imm64 for dynamic expression")? 2379 .into(); 2380 Ok(Expr { 2381 base: BaseExpr::None, 2382 offset, 2383 }) 2384 } else { 2385 let base = self.parse_base_expr()?; 2386 match self.token() { 2387 Some(Token::Plus) => { 2388 self.consume(); 2389 let offset: u64 = self 2390 .match_uimm64( 2391 "expected uimm64 in imm64 range for offset in dynamic expression", 2392 )? 2393 .into(); 2394 let offset: i64 = i64::try_from(offset).map_err(|_| { 2395 self.error("integer offset in dynamic expression is out of range") 2396 })?; 2397 Ok(Expr { base, offset }) 2398 } 2399 Some(Token::Integer(x)) if x.starts_with("-") => { 2400 let offset: i64 = self 2401 .match_imm64("expected an imm64 range for offset in dynamic expression")? 2402 .into(); 2403 Ok(Expr { base, offset }) 2404 } 2405 _ => Ok(Expr { base, offset: 0 }), 2406 } 2407 } 2408 } 2409 2410 // Parse the base part of a dynamic expression, used in some PCC facts. 2411 // 2412 // base-expr ::= GlobalValue(base) 2413 // | Value(base) 2414 // | "max" 2415 // | (epsilon) 2416 fn parse_base_expr(&mut self) -> ParseResult<BaseExpr> { 2417 match self.token() { 2418 Some(Token::Identifier("max")) => { 2419 self.consume(); 2420 Ok(BaseExpr::Max) 2421 } 2422 Some(Token::GlobalValue(..)) => { 2423 let gv = self.match_gv("expected global value")?; 2424 Ok(BaseExpr::GlobalValue(gv)) 2425 } 2426 Some(Token::Value(..)) => { 2427 let value = self.match_value("expected value")?; 2428 Ok(BaseExpr::Value(value)) 2429 } 2430 _ => Ok(BaseExpr::None), 2431 } 2432 } 2433 2434 // Parse instruction results and return them. 2435 // 2436 // inst-results ::= Value(v) { "," Value(v) } 2437 // 2438 fn parse_inst_results(&mut self, ctx: &mut Context) -> ParseResult<SmallVec<[Value; 1]>> { 2439 // Result value numbers. 2440 let mut results = SmallVec::new(); 2441 2442 // instruction ::= * [inst-results "="] Opcode(opc) ["." Type] ... 2443 // inst-results ::= * Value(v) { "," Value(v) } 2444 if let Some(Token::Value(v)) = self.token() { 2445 self.consume(); 2446 2447 results.push(v); 2448 2449 let fact = if self.token() == Some(Token::Bang) { 2450 self.consume(); 2451 // block-param ::= Value(v) [ "!" * fact ] ":" Type(t) arg-loc? 2452 Some(self.parse_fact()?) 2453 } else { 2454 None 2455 }; 2456 ctx.function.dfg.facts[v] = fact; 2457 2458 // inst-results ::= Value(v) * { "," Value(v) } 2459 while self.optional(Token::Comma) { 2460 // inst-results ::= Value(v) { "," * Value(v) } 2461 let v = self.match_value("expected result value")?; 2462 results.push(v); 2463 2464 let fact = if self.token() == Some(Token::Bang) { 2465 self.consume(); 2466 // block-param ::= Value(v) [ "!" * fact ] ":" Type(t) arg-loc? 2467 Some(self.parse_fact()?) 2468 } else { 2469 None 2470 }; 2471 ctx.function.dfg.facts[v] = fact; 2472 } 2473 } 2474 2475 Ok(results) 2476 } 2477 2478 // Parse a value alias, and append it to `block`. 2479 // 2480 // value_alias ::= [inst-results] "->" Value(v) 2481 // 2482 fn parse_value_alias(&mut self, results: &[Value], ctx: &mut Context) -> ParseResult<()> { 2483 if results.len() != 1 { 2484 return err!(self.loc, "wrong number of aliases"); 2485 } 2486 let result = results[0]; 2487 let dest = self.match_value("expected value alias")?; 2488 2489 // Allow duplicate definitions of aliases, as long as they are identical. 2490 if ctx.map.contains_value(result) { 2491 if let Some(old) = ctx.function.dfg.value_alias_dest_for_serialization(result) { 2492 if old != dest { 2493 return err!( 2494 self.loc, 2495 "value {} is already defined as an alias with destination {}", 2496 result, 2497 old 2498 ); 2499 } 2500 } else { 2501 return err!(self.loc, "value {} is already defined"); 2502 } 2503 } else { 2504 ctx.map.def_value(result, self.loc)?; 2505 } 2506 2507 if !ctx.map.contains_value(dest) { 2508 return err!(self.loc, "value {} is not yet defined", dest); 2509 } 2510 2511 ctx.function 2512 .dfg 2513 .make_value_alias_for_serialization(dest, result); 2514 2515 ctx.aliases.push(result); 2516 Ok(()) 2517 } 2518 2519 // Parse an instruction, append it to `block`. 2520 // 2521 // instruction ::= [inst-results "="] Opcode(opc) ["." Type] ... 2522 // 2523 fn parse_instruction( 2524 &mut self, 2525 results: &[Value], 2526 srcloc: ir::SourceLoc, 2527 ctx: &mut Context, 2528 block: Block, 2529 ) -> ParseResult<()> { 2530 // Define the result values. 2531 for val in results { 2532 ctx.map.def_value(*val, self.loc)?; 2533 } 2534 2535 // Collect comments for the next instruction. 2536 self.start_gathering_comments(); 2537 2538 // instruction ::= [inst-results "="] * Opcode(opc) ["." Type] ... 2539 let opcode = if let Some(Token::Identifier(text)) = self.token() { 2540 match text.parse() { 2541 Ok(opc) => opc, 2542 Err(msg) => return err!(self.loc, "{}: '{}'", msg, text), 2543 } 2544 } else { 2545 return err!(self.loc, "expected instruction opcode"); 2546 }; 2547 let opcode_loc = self.loc; 2548 self.consume(); 2549 2550 // Look for a controlling type variable annotation. 2551 // instruction ::= [inst-results "="] Opcode(opc) * ["." Type] ... 2552 let explicit_ctrl_type = if self.optional(Token::Dot) { 2553 if let Some(Token::Type(_t)) = self.token() { 2554 Some(self.match_type("expected type after 'opcode.'")?) 2555 } else { 2556 let dt = self.match_dt("expected dynamic type")?; 2557 self.concrete_from_dt(dt, ctx) 2558 } 2559 } else { 2560 None 2561 }; 2562 2563 // instruction ::= [inst-results "="] Opcode(opc) ["." Type] * ... 2564 let inst_data = self.parse_inst_operands(ctx, opcode, explicit_ctrl_type)?; 2565 2566 // We're done parsing the instruction data itself. 2567 // 2568 // We still need to check that the number of result values in the source 2569 // matches the opcode or function call signature. We also need to create 2570 // values with the right type for all the instruction results and parse 2571 // and attach stack map entries, if present. 2572 let ctrl_typevar = self.infer_typevar(ctx, opcode, explicit_ctrl_type, &inst_data)?; 2573 let inst = ctx.function.dfg.make_inst(inst_data); 2574 if opcode.is_call() && !opcode.is_return() && self.optional(Token::Comma) { 2575 self.match_identifier("stack_map", "expected `stack_map = [...]`")?; 2576 self.match_token(Token::Equal, "expected `= [...]`")?; 2577 self.match_token(Token::LBracket, "expected `[...]`")?; 2578 while !self.optional(Token::RBracket) { 2579 let ty = self.match_type("expected `<type> @ <slot> + <offset>`")?; 2580 self.match_token(Token::At, "expected `@ <slot> + <offset>`")?; 2581 let slot = self.match_ss("expected `<slot> + <offset>`")?; 2582 let offset: u32 = match self.token() { 2583 Some(Token::Integer(s)) if s.starts_with('+') => { 2584 self.match_uimm32("expected a u32 offset")?.into() 2585 } 2586 _ => { 2587 self.match_token(Token::Plus, "expected `+ <offset>`")?; 2588 self.match_uimm32("expected a u32 offset")?.into() 2589 } 2590 }; 2591 ctx.function 2592 .dfg 2593 .append_user_stack_map_entry(inst, ir::UserStackMapEntry { ty, slot, offset }); 2594 if !self.optional(Token::Comma) { 2595 self.match_token(Token::RBracket, "expected `,` or `]`")?; 2596 break; 2597 } 2598 } 2599 } 2600 let num_results = 2601 ctx.function 2602 .dfg 2603 .make_inst_results_for_parser(inst, ctrl_typevar, results); 2604 ctx.function.layout.append_inst(inst, block); 2605 ctx.map 2606 .def_entity(inst.into(), opcode_loc) 2607 .expect("duplicate inst references created"); 2608 2609 if !srcloc.is_default() { 2610 ctx.function.set_srcloc(inst, srcloc); 2611 } 2612 2613 if results.len() != num_results { 2614 return err!( 2615 self.loc, 2616 "instruction produces {} result values, {} given", 2617 num_results, 2618 results.len() 2619 ); 2620 } 2621 2622 // Collect any trailing comments. 2623 self.token(); 2624 self.claim_gathered_comments(inst); 2625 2626 Ok(()) 2627 } 2628 2629 // Type inference for polymorphic instructions. 2630 // 2631 // The controlling type variable can be specified explicitly as 'splat.i32x4 v5', or it can be 2632 // inferred from `inst_data.typevar_operand` for some opcodes. 2633 // 2634 // Returns the controlling typevar for a polymorphic opcode, or `INVALID` for a non-polymorphic 2635 // opcode. 2636 fn infer_typevar( 2637 &self, 2638 ctx: &Context, 2639 opcode: Opcode, 2640 explicit_ctrl_type: Option<Type>, 2641 inst_data: &InstructionData, 2642 ) -> ParseResult<Type> { 2643 let constraints = opcode.constraints(); 2644 let ctrl_type = match explicit_ctrl_type { 2645 Some(t) => t, 2646 None => { 2647 if constraints.use_typevar_operand() { 2648 // This is an opcode that supports type inference, AND there was no 2649 // explicit type specified. Look up `ctrl_value` to see if it was defined 2650 // already. 2651 // TBD: If it is defined in another block, the type should have been 2652 // specified explicitly. It is unfortunate that the correctness of IR 2653 // depends on the layout of the blocks. 2654 let ctrl_src_value = inst_data 2655 .typevar_operand(&ctx.function.dfg.value_lists) 2656 .expect("Constraints <-> Format inconsistency"); 2657 if !ctx.map.contains_value(ctrl_src_value) { 2658 return err!( 2659 self.loc, 2660 "type variable required for polymorphic opcode, e.g. '{}.{}'; \ 2661 can't infer from {} which is not yet defined", 2662 opcode, 2663 constraints.ctrl_typeset().unwrap().example(), 2664 ctrl_src_value 2665 ); 2666 } 2667 if !ctx.function.dfg.value_is_valid_for_parser(ctrl_src_value) { 2668 return err!( 2669 self.loc, 2670 "type variable required for polymorphic opcode, e.g. '{}.{}'; \ 2671 can't infer from {} which is not yet resolved", 2672 opcode, 2673 constraints.ctrl_typeset().unwrap().example(), 2674 ctrl_src_value 2675 ); 2676 } 2677 ctx.function.dfg.value_type(ctrl_src_value) 2678 } else if constraints.is_polymorphic() { 2679 // This opcode does not support type inference, so the explicit type 2680 // variable is required. 2681 return err!( 2682 self.loc, 2683 "type variable required for polymorphic opcode, e.g. '{}.{}'", 2684 opcode, 2685 constraints.ctrl_typeset().unwrap().example() 2686 ); 2687 } else { 2688 // This is a non-polymorphic opcode. No typevar needed. 2689 INVALID 2690 } 2691 } 2692 }; 2693 2694 // Verify that `ctrl_type` is valid for the controlling type variable. We don't want to 2695 // attempt deriving types from an incorrect basis. 2696 // This is not a complete type check. The verifier does that. 2697 if let Some(typeset) = constraints.ctrl_typeset() { 2698 // This is a polymorphic opcode. 2699 if !typeset.contains(ctrl_type) { 2700 return err!( 2701 self.loc, 2702 "{} is not a valid typevar for {}", 2703 ctrl_type, 2704 opcode 2705 ); 2706 } 2707 // Treat it as a syntax error to specify a typevar on a non-polymorphic opcode. 2708 } else if ctrl_type != INVALID { 2709 return err!(self.loc, "{} does not take a typevar", opcode); 2710 } 2711 2712 Ok(ctrl_type) 2713 } 2714 2715 // Parse comma-separated value list into a VariableArgs struct. 2716 // 2717 // value_list ::= [ value { "," value } ] 2718 // 2719 fn parse_value_list(&mut self) -> ParseResult<VariableArgs> { 2720 let mut args = VariableArgs::new(); 2721 2722 if let Some(Token::Value(v)) = self.token() { 2723 args.push(v); 2724 self.consume(); 2725 } else { 2726 return Ok(args); 2727 } 2728 2729 while self.optional(Token::Comma) { 2730 args.push(self.match_value("expected value in argument list")?); 2731 } 2732 2733 Ok(args) 2734 } 2735 2736 /// Parse an optional list of block-call arguments enclosed in 2737 /// parentheses. 2738 fn parse_opt_block_call_args(&mut self) -> ParseResult<Vec<BlockArg>> { 2739 if !self.optional(Token::LPar) { 2740 return Ok(vec![]); 2741 } 2742 2743 let mut args = vec![]; 2744 while self.token() != Some(Token::RPar) { 2745 args.push(self.parse_block_call_arg()?); 2746 if self.token() == Some(Token::Comma) { 2747 self.consume(); 2748 } else { 2749 break; 2750 } 2751 } 2752 2753 self.match_token(Token::RPar, "expected ')' after arguments")?; 2754 2755 Ok(args) 2756 } 2757 2758 fn parse_block_call_arg(&mut self) -> ParseResult<BlockArg> { 2759 match self.token() { 2760 Some(Token::Value(v)) => { 2761 self.consume(); 2762 Ok(BlockArg::Value(v)) 2763 } 2764 Some(Token::TryCallRet(i)) => { 2765 self.consume(); 2766 Ok(BlockArg::TryCallRet(i)) 2767 } 2768 Some(Token::TryCallExn(i)) => { 2769 self.consume(); 2770 Ok(BlockArg::TryCallExn(i)) 2771 } 2772 tok => Err(self.error(&format!("unexpected token: {tok:?}"))), 2773 } 2774 } 2775 2776 /// Parse a CLIF run command. 2777 /// 2778 /// run-command ::= "run" [":" invocation comparison expected] 2779 /// \ "print" [":" invocation] 2780 fn parse_run_command(&mut self, sig: &Signature) -> ParseResult<RunCommand> { 2781 // skip semicolon 2782 match self.token() { 2783 Some(Token::Identifier("run")) => { 2784 self.consume(); 2785 if self.optional(Token::Colon) { 2786 let invocation = self.parse_run_invocation(sig)?; 2787 let comparison = self.parse_run_comparison()?; 2788 let expected = self.parse_run_returns(sig)?; 2789 Ok(RunCommand::Run(invocation, comparison, expected)) 2790 } else if sig.params.is_empty() 2791 && sig.returns.len() == 1 2792 && sig.returns[0].value_type.is_int() 2793 { 2794 // To match the existing run behavior that does not require an explicit 2795 // invocation, we create an invocation from a function like `() -> i*` and 2796 // require the result to be non-zero. 2797 let invocation = Invocation::new("default", vec![]); 2798 let expected = vec![DataValue::I8(0)]; 2799 let comparison = Comparison::NotEquals; 2800 Ok(RunCommand::Run(invocation, comparison, expected)) 2801 } else { 2802 Err(self.error("unable to parse the run command")) 2803 } 2804 } 2805 Some(Token::Identifier("print")) => { 2806 self.consume(); 2807 if self.optional(Token::Colon) { 2808 Ok(RunCommand::Print(self.parse_run_invocation(sig)?)) 2809 } else if sig.params.is_empty() { 2810 // To allow printing of functions like `() -> *`, we create a no-arg invocation. 2811 let invocation = Invocation::new("default", vec![]); 2812 Ok(RunCommand::Print(invocation)) 2813 } else { 2814 Err(self.error("unable to parse the print command")) 2815 } 2816 } 2817 _ => Err(self.error("expected a 'run:' or 'print:' command")), 2818 } 2819 } 2820 2821 /// Parse the invocation of a CLIF function. 2822 /// 2823 /// This is different from parsing a CLIF `call`; it is used in parsing run commands like 2824 /// `run: %fn(42, 4.2) == false`. 2825 /// 2826 /// invocation ::= name "(" [data-value-list] ")" 2827 fn parse_run_invocation(&mut self, sig: &Signature) -> ParseResult<Invocation> { 2828 if let Some(Token::Name(name)) = self.token() { 2829 self.consume(); 2830 self.match_token( 2831 Token::LPar, 2832 "expected invocation parentheses, e.g. %fn(...)", 2833 )?; 2834 2835 let arg_types = sig 2836 .params 2837 .iter() 2838 .map(|abi| abi.value_type) 2839 .collect::<Vec<_>>(); 2840 let args = self.parse_data_value_list(&arg_types)?; 2841 2842 self.match_token( 2843 Token::RPar, 2844 "expected invocation parentheses, e.g. %fn(...)", 2845 )?; 2846 Ok(Invocation::new(name, args)) 2847 } else { 2848 Err(self.error("expected a function name, e.g. %my_fn")) 2849 } 2850 } 2851 2852 /// Parse a comparison operator for run commands. 2853 /// 2854 /// comparison ::= "==" | "!=" 2855 fn parse_run_comparison(&mut self) -> ParseResult<Comparison> { 2856 if self.optional(Token::Equal) { 2857 self.match_token(Token::Equal, "expected another =")?; 2858 Ok(Comparison::Equals) 2859 } else if self.optional(Token::Bang) { 2860 self.match_token(Token::Equal, "expected a =")?; 2861 Ok(Comparison::NotEquals) 2862 } else { 2863 Err(self.error("unable to parse a valid comparison operator")) 2864 } 2865 } 2866 2867 /// Parse the expected return values of a run invocation. 2868 /// 2869 /// expected ::= "[" "]" 2870 /// | data-value 2871 /// | "[" data-value-list "]" 2872 fn parse_run_returns(&mut self, sig: &Signature) -> ParseResult<Vec<DataValue>> { 2873 if sig.returns.len() != 1 { 2874 self.match_token(Token::LBracket, "expected a left bracket [")?; 2875 } 2876 2877 let returns = self 2878 .parse_data_value_list(&sig.returns.iter().map(|a| a.value_type).collect::<Vec<_>>())?; 2879 2880 if sig.returns.len() != 1 { 2881 self.match_token(Token::RBracket, "expected a right bracket ]")?; 2882 } 2883 Ok(returns) 2884 } 2885 2886 /// Parse a comma-separated list of data values. 2887 /// 2888 /// data-value-list ::= [data-value {"," data-value-list}] 2889 fn parse_data_value_list(&mut self, types: &[Type]) -> ParseResult<Vec<DataValue>> { 2890 let mut values = vec![]; 2891 for ty in types.iter().take(1) { 2892 values.push(self.parse_data_value(*ty)?); 2893 } 2894 for ty in types.iter().skip(1) { 2895 self.match_token( 2896 Token::Comma, 2897 "expected a comma between invocation arguments", 2898 )?; 2899 values.push(self.parse_data_value(*ty)?); 2900 } 2901 Ok(values) 2902 } 2903 2904 /// Parse a data value; e.g. `42`, `4.2`, `true`. 2905 /// 2906 /// data-value-list ::= [data-value {"," data-value-list}] 2907 fn parse_data_value(&mut self, ty: Type) -> ParseResult<DataValue> { 2908 let dv = match ty { 2909 I8 => DataValue::from(self.match_imm8("expected a i8")?), 2910 I16 => DataValue::from(self.match_imm16("expected an i16")?), 2911 I32 => DataValue::from(self.match_imm32("expected an i32")?), 2912 I64 => DataValue::from(Into::<i64>::into(self.match_imm64("expected an i64")?)), 2913 I128 => DataValue::from(self.match_imm128("expected an i128")?), 2914 F16 => DataValue::from(self.match_ieee16("expected an f16")?), 2915 F32 => DataValue::from(self.match_ieee32("expected an f32")?), 2916 F64 => DataValue::from(self.match_ieee64("expected an f64")?), 2917 F128 => DataValue::from(self.match_ieee128("expected an f128")?), 2918 _ if (ty.is_vector() || ty.is_dynamic_vector()) => { 2919 let as_vec = self.match_uimm128(ty)?.into_vec(); 2920 if as_vec.len() == 16 { 2921 let mut as_array = [0; 16]; 2922 as_array.copy_from_slice(&as_vec[..]); 2923 DataValue::from(as_array) 2924 } else if as_vec.len() == 8 { 2925 let mut as_array = [0; 8]; 2926 as_array.copy_from_slice(&as_vec[..]); 2927 DataValue::from(as_array) 2928 } else { 2929 return Err(self.error("only 128-bit vectors are currently supported")); 2930 } 2931 } 2932 _ => return Err(self.error(&format!("don't know how to parse data values of: {ty}"))), 2933 }; 2934 Ok(dv) 2935 } 2936 2937 // Parse the operands following the instruction opcode. 2938 // This depends on the format of the opcode. 2939 fn parse_inst_operands( 2940 &mut self, 2941 ctx: &mut Context, 2942 opcode: Opcode, 2943 explicit_control_type: Option<Type>, 2944 ) -> ParseResult<InstructionData> { 2945 let idata = match opcode.format() { 2946 InstructionFormat::Unary => InstructionData::Unary { 2947 opcode, 2948 arg: self.match_value("expected SSA value operand")?, 2949 }, 2950 InstructionFormat::UnaryImm => { 2951 let msg = |bits| format!("expected immediate {bits}-bit integer operand"); 2952 let unsigned = match explicit_control_type { 2953 Some(types::I8) => self.match_imm8(&msg(8))? as u8 as i64, 2954 Some(types::I16) => self.match_imm16(&msg(16))? as u16 as i64, 2955 Some(types::I32) => self.match_imm32(&msg(32))? as u32 as i64, 2956 Some(types::I64) => self.match_imm64(&msg(64))?.bits(), 2957 _ => { 2958 return err!( 2959 self.loc, 2960 "expected one of the following type: i8, i16, i32 or i64" 2961 ) 2962 } 2963 }; 2964 InstructionData::UnaryImm { 2965 opcode, 2966 imm: Imm64::new(unsigned), 2967 } 2968 } 2969 InstructionFormat::UnaryIeee16 => InstructionData::UnaryIeee16 { 2970 opcode, 2971 imm: self.match_ieee16("expected immediate 16-bit float operand")?, 2972 }, 2973 InstructionFormat::UnaryIeee32 => InstructionData::UnaryIeee32 { 2974 opcode, 2975 imm: self.match_ieee32("expected immediate 32-bit float operand")?, 2976 }, 2977 InstructionFormat::UnaryIeee64 => InstructionData::UnaryIeee64 { 2978 opcode, 2979 imm: self.match_ieee64("expected immediate 64-bit float operand")?, 2980 }, 2981 InstructionFormat::UnaryConst => { 2982 let constant_handle = if let Some(Token::Constant(_)) = self.token() { 2983 // If handed a `const?`, use that. 2984 let c = self.match_constant()?; 2985 ctx.check_constant(c, self.loc)?; 2986 c 2987 } else if opcode == Opcode::F128const { 2988 let ieee128 = self.match_ieee128("expected immediate 128-bit float operand")?; 2989 ctx.function.dfg.constants.insert(ieee128.into()) 2990 } else if let Some(controlling_type) = explicit_control_type { 2991 // If an explicit control type is present, we expect a sized value and insert 2992 // it in the constant pool. 2993 let uimm128 = self.match_uimm128(controlling_type)?; 2994 ctx.function.dfg.constants.insert(uimm128) 2995 } else { 2996 return err!( 2997 self.loc, 2998 "Expected either a const entity or a typed value, e.g. inst.i32x4 [...]" 2999 ); 3000 }; 3001 InstructionData::UnaryConst { 3002 opcode, 3003 constant_handle, 3004 } 3005 } 3006 InstructionFormat::UnaryGlobalValue => { 3007 let gv = self.match_gv("expected global value")?; 3008 ctx.check_gv(gv, self.loc)?; 3009 InstructionData::UnaryGlobalValue { 3010 opcode, 3011 global_value: gv, 3012 } 3013 } 3014 InstructionFormat::Binary => { 3015 let lhs = self.match_value("expected SSA value first operand")?; 3016 self.match_token(Token::Comma, "expected ',' between operands")?; 3017 let rhs = self.match_value("expected SSA value second operand")?; 3018 InstructionData::Binary { 3019 opcode, 3020 args: [lhs, rhs], 3021 } 3022 } 3023 InstructionFormat::BinaryImm8 => { 3024 let arg = self.match_value("expected SSA value first operand")?; 3025 self.match_token(Token::Comma, "expected ',' between operands")?; 3026 let imm = self.match_uimm8("expected unsigned 8-bit immediate")?; 3027 InstructionData::BinaryImm8 { opcode, arg, imm } 3028 } 3029 InstructionFormat::BinaryImm64 => { 3030 let lhs = self.match_value("expected SSA value first operand")?; 3031 self.match_token(Token::Comma, "expected ',' between operands")?; 3032 let rhs = self.match_imm64("expected immediate integer second operand")?; 3033 InstructionData::BinaryImm64 { 3034 opcode, 3035 arg: lhs, 3036 imm: rhs, 3037 } 3038 } 3039 InstructionFormat::Ternary => { 3040 // Names here refer to the `select` instruction. 3041 // This format is also use by `fma`. 3042 let ctrl_arg = self.match_value("expected SSA value control operand")?; 3043 self.match_token(Token::Comma, "expected ',' between operands")?; 3044 let true_arg = self.match_value("expected SSA value true operand")?; 3045 self.match_token(Token::Comma, "expected ',' between operands")?; 3046 let false_arg = self.match_value("expected SSA value false operand")?; 3047 InstructionData::Ternary { 3048 opcode, 3049 args: [ctrl_arg, true_arg, false_arg], 3050 } 3051 } 3052 InstructionFormat::MultiAry => { 3053 let args = self.parse_value_list()?; 3054 InstructionData::MultiAry { 3055 opcode, 3056 args: args.into_value_list(&[], &mut ctx.function.dfg.value_lists), 3057 } 3058 } 3059 InstructionFormat::NullAry => InstructionData::NullAry { opcode }, 3060 InstructionFormat::Jump => { 3061 // Parse the destination block number. 3062 let block_num = self.match_block("expected jump destination block")?; 3063 let args = self.parse_opt_block_call_args()?; 3064 let destination = ctx.function.dfg.block_call(block_num, &args); 3065 InstructionData::Jump { 3066 opcode, 3067 destination, 3068 } 3069 } 3070 InstructionFormat::Brif => { 3071 let arg = self.match_value("expected SSA value control operand")?; 3072 self.match_token(Token::Comma, "expected ',' between operands")?; 3073 let block_then = { 3074 let block_num = self.match_block("expected branch then block")?; 3075 let args = self.parse_opt_block_call_args()?; 3076 ctx.function.dfg.block_call(block_num, &args) 3077 }; 3078 self.match_token(Token::Comma, "expected ',' between operands")?; 3079 let block_else = { 3080 let block_num = self.match_block("expected branch else block")?; 3081 let args = self.parse_opt_block_call_args()?; 3082 ctx.function.dfg.block_call(block_num, &args) 3083 }; 3084 InstructionData::Brif { 3085 opcode, 3086 arg, 3087 blocks: [block_then, block_else], 3088 } 3089 } 3090 InstructionFormat::BranchTable => { 3091 let arg = self.match_value("expected SSA value operand")?; 3092 self.match_token(Token::Comma, "expected ',' between operands")?; 3093 let block_num = self.match_block("expected branch destination block")?; 3094 let args = self.parse_opt_block_call_args()?; 3095 let destination = ctx.function.dfg.block_call(block_num, &args); 3096 self.match_token(Token::Comma, "expected ',' between operands")?; 3097 let table = self.parse_jump_table(ctx, destination)?; 3098 InstructionData::BranchTable { opcode, arg, table } 3099 } 3100 InstructionFormat::TernaryImm8 => { 3101 let lhs = self.match_value("expected SSA value first operand")?; 3102 self.match_token(Token::Comma, "expected ',' between operands")?; 3103 let rhs = self.match_value("expected SSA value last operand")?; 3104 self.match_token(Token::Comma, "expected ',' between operands")?; 3105 let imm = self.match_uimm8("expected 8-bit immediate")?; 3106 InstructionData::TernaryImm8 { 3107 opcode, 3108 imm, 3109 args: [lhs, rhs], 3110 } 3111 } 3112 InstructionFormat::Shuffle => { 3113 let a = self.match_value("expected SSA value first operand")?; 3114 self.match_token(Token::Comma, "expected ',' between operands")?; 3115 let b = self.match_value("expected SSA value second operand")?; 3116 self.match_token(Token::Comma, "expected ',' between operands")?; 3117 let uimm128 = self.match_uimm128(I8X16)?; 3118 let imm = ctx.function.dfg.immediates.push(uimm128); 3119 InstructionData::Shuffle { 3120 opcode, 3121 imm, 3122 args: [a, b], 3123 } 3124 } 3125 InstructionFormat::IntCompare => { 3126 let cond = self.match_enum("expected intcc condition code")?; 3127 let lhs = self.match_value("expected SSA value first operand")?; 3128 self.match_token(Token::Comma, "expected ',' between operands")?; 3129 let rhs = self.match_value("expected SSA value second operand")?; 3130 InstructionData::IntCompare { 3131 opcode, 3132 cond, 3133 args: [lhs, rhs], 3134 } 3135 } 3136 InstructionFormat::IntCompareImm => { 3137 let cond = self.match_enum("expected intcc condition code")?; 3138 let lhs = self.match_value("expected SSA value first operand")?; 3139 self.match_token(Token::Comma, "expected ',' between operands")?; 3140 let rhs = self.match_imm64("expected immediate second operand")?; 3141 InstructionData::IntCompareImm { 3142 opcode, 3143 cond, 3144 arg: lhs, 3145 imm: rhs, 3146 } 3147 } 3148 InstructionFormat::FloatCompare => { 3149 let cond = self.match_enum("expected floatcc condition code")?; 3150 let lhs = self.match_value("expected SSA value first operand")?; 3151 self.match_token(Token::Comma, "expected ',' between operands")?; 3152 let rhs = self.match_value("expected SSA value second operand")?; 3153 InstructionData::FloatCompare { 3154 opcode, 3155 cond, 3156 args: [lhs, rhs], 3157 } 3158 } 3159 InstructionFormat::Call => { 3160 let func_ref = self.match_fn("expected function reference")?; 3161 ctx.check_fn(func_ref, self.loc)?; 3162 self.match_token(Token::LPar, "expected '(' before arguments")?; 3163 let args = self.parse_value_list()?; 3164 self.match_token(Token::RPar, "expected ')' after arguments")?; 3165 InstructionData::Call { 3166 opcode, 3167 func_ref, 3168 args: args.into_value_list(&[], &mut ctx.function.dfg.value_lists), 3169 } 3170 } 3171 InstructionFormat::CallIndirect => { 3172 let sig_ref = self.match_sig("expected signature reference")?; 3173 ctx.check_sig(sig_ref, self.loc)?; 3174 self.match_token(Token::Comma, "expected ',' between operands")?; 3175 let callee = self.match_value("expected SSA value callee operand")?; 3176 self.match_token(Token::LPar, "expected '(' before arguments")?; 3177 let args = self.parse_value_list()?; 3178 self.match_token(Token::RPar, "expected ')' after arguments")?; 3179 InstructionData::CallIndirect { 3180 opcode, 3181 sig_ref, 3182 args: args.into_value_list(&[callee], &mut ctx.function.dfg.value_lists), 3183 } 3184 } 3185 InstructionFormat::TryCall => { 3186 let func_ref = self.match_fn("expected function reference")?; 3187 ctx.check_fn(func_ref, self.loc)?; 3188 self.match_token(Token::LPar, "expected '(' before arguments")?; 3189 let args = self.parse_value_list()?; 3190 self.match_token(Token::RPar, "expected ')' after arguments")?; 3191 self.match_token(Token::Comma, "expected ',' after argument list")?; 3192 let exception = self.parse_exception_table(ctx)?; 3193 InstructionData::TryCall { 3194 opcode, 3195 func_ref, 3196 args: args.into_value_list(&[], &mut ctx.function.dfg.value_lists), 3197 exception, 3198 } 3199 } 3200 InstructionFormat::TryCallIndirect => { 3201 let callee = self.match_value("expected SSA value callee operand")?; 3202 self.match_token(Token::LPar, "expected '(' before arguments")?; 3203 let args = self.parse_value_list()?; 3204 self.match_token(Token::RPar, "expected ')' after arguments")?; 3205 self.match_token(Token::Comma, "expected ',' after argument list")?; 3206 let exception = self.parse_exception_table(ctx)?; 3207 InstructionData::TryCallIndirect { 3208 opcode, 3209 args: args.into_value_list(&[callee], &mut ctx.function.dfg.value_lists), 3210 exception, 3211 } 3212 } 3213 InstructionFormat::FuncAddr => { 3214 let func_ref = self.match_fn("expected function reference")?; 3215 ctx.check_fn(func_ref, self.loc)?; 3216 InstructionData::FuncAddr { opcode, func_ref } 3217 } 3218 InstructionFormat::StackLoad => { 3219 let ss = self.match_ss("expected stack slot number: ss«n»")?; 3220 ctx.check_ss(ss, self.loc)?; 3221 let offset = self.optional_offset32()?; 3222 InstructionData::StackLoad { 3223 opcode, 3224 stack_slot: ss, 3225 offset, 3226 } 3227 } 3228 InstructionFormat::StackStore => { 3229 let arg = self.match_value("expected SSA value operand")?; 3230 self.match_token(Token::Comma, "expected ',' between operands")?; 3231 let ss = self.match_ss("expected stack slot number: ss«n»")?; 3232 ctx.check_ss(ss, self.loc)?; 3233 let offset = self.optional_offset32()?; 3234 InstructionData::StackStore { 3235 opcode, 3236 arg, 3237 stack_slot: ss, 3238 offset, 3239 } 3240 } 3241 InstructionFormat::DynamicStackLoad => { 3242 let dss = self.match_dss("expected dynamic stack slot number: dss«n»")?; 3243 ctx.check_dss(dss, self.loc)?; 3244 InstructionData::DynamicStackLoad { 3245 opcode, 3246 dynamic_stack_slot: dss, 3247 } 3248 } 3249 InstructionFormat::DynamicStackStore => { 3250 let arg = self.match_value("expected SSA value operand")?; 3251 self.match_token(Token::Comma, "expected ',' between operands")?; 3252 let dss = self.match_dss("expected dynamic stack slot number: dss«n»")?; 3253 ctx.check_dss(dss, self.loc)?; 3254 InstructionData::DynamicStackStore { 3255 opcode, 3256 arg, 3257 dynamic_stack_slot: dss, 3258 } 3259 } 3260 InstructionFormat::Load => { 3261 let flags = self.optional_memflags()?; 3262 let addr = self.match_value("expected SSA value address")?; 3263 let offset = self.optional_offset32()?; 3264 InstructionData::Load { 3265 opcode, 3266 flags, 3267 arg: addr, 3268 offset, 3269 } 3270 } 3271 InstructionFormat::Store => { 3272 let flags = self.optional_memflags()?; 3273 let arg = self.match_value("expected SSA value operand")?; 3274 self.match_token(Token::Comma, "expected ',' between operands")?; 3275 let addr = self.match_value("expected SSA value address")?; 3276 let offset = self.optional_offset32()?; 3277 InstructionData::Store { 3278 opcode, 3279 flags, 3280 args: [arg, addr], 3281 offset, 3282 } 3283 } 3284 InstructionFormat::Trap => { 3285 let code = self.match_enum("expected trap code")?; 3286 InstructionData::Trap { opcode, code } 3287 } 3288 InstructionFormat::CondTrap => { 3289 let arg = self.match_value("expected SSA value operand")?; 3290 self.match_token(Token::Comma, "expected ',' between operands")?; 3291 let code = self.match_enum("expected trap code")?; 3292 InstructionData::CondTrap { opcode, arg, code } 3293 } 3294 InstructionFormat::AtomicCas => { 3295 let flags = self.optional_memflags()?; 3296 let addr = self.match_value("expected SSA value address")?; 3297 self.match_token(Token::Comma, "expected ',' between operands")?; 3298 let expected = self.match_value("expected SSA value address")?; 3299 self.match_token(Token::Comma, "expected ',' between operands")?; 3300 let replacement = self.match_value("expected SSA value address")?; 3301 InstructionData::AtomicCas { 3302 opcode, 3303 flags, 3304 args: [addr, expected, replacement], 3305 } 3306 } 3307 InstructionFormat::AtomicRmw => { 3308 let flags = self.optional_memflags()?; 3309 let op = self.match_enum("expected AtomicRmwOp")?; 3310 let addr = self.match_value("expected SSA value address")?; 3311 self.match_token(Token::Comma, "expected ',' between operands")?; 3312 let arg2 = self.match_value("expected SSA value address")?; 3313 InstructionData::AtomicRmw { 3314 opcode, 3315 flags, 3316 op, 3317 args: [addr, arg2], 3318 } 3319 } 3320 InstructionFormat::LoadNoOffset => { 3321 let flags = self.optional_memflags()?; 3322 let addr = self.match_value("expected SSA value address")?; 3323 InstructionData::LoadNoOffset { 3324 opcode, 3325 flags, 3326 arg: addr, 3327 } 3328 } 3329 InstructionFormat::StoreNoOffset => { 3330 let flags = self.optional_memflags()?; 3331 let arg = self.match_value("expected SSA value operand")?; 3332 self.match_token(Token::Comma, "expected ',' between operands")?; 3333 let addr = self.match_value("expected SSA value address")?; 3334 InstructionData::StoreNoOffset { 3335 opcode, 3336 flags, 3337 args: [arg, addr], 3338 } 3339 } 3340 InstructionFormat::IntAddTrap => { 3341 let a = self.match_value("expected SSA value operand")?; 3342 self.match_token(Token::Comma, "expected ',' between operands")?; 3343 let b = self.match_value("expected SSA value operand")?; 3344 self.match_token(Token::Comma, "expected ',' between operands")?; 3345 let code = self.match_enum("expected trap code")?; 3346 InstructionData::IntAddTrap { 3347 opcode, 3348 args: [a, b], 3349 code, 3350 } 3351 } 3352 }; 3353 Ok(idata) 3354 } 3355 } 3356 3357 #[cfg(test)] 3358 mod tests { 3359 use super::*; 3360 use crate::isaspec::IsaSpec; 3361 3362 #[test] 3363 fn argument_type() { 3364 let mut p = Parser::new("i32 sext"); 3365 let arg = p.parse_abi_param().unwrap(); 3366 assert_eq!(arg.value_type, types::I32); 3367 assert_eq!(arg.extension, ArgumentExtension::Sext); 3368 assert_eq!(arg.purpose, ArgumentPurpose::Normal); 3369 let ParseError { 3370 location, 3371 message, 3372 is_warning, 3373 } = p.parse_abi_param().unwrap_err(); 3374 assert_eq!(location.line_number, 1); 3375 assert_eq!(message, "expected parameter type"); 3376 assert!(!is_warning); 3377 } 3378 3379 #[test] 3380 fn aliases() { 3381 let (func, details) = Parser::new( 3382 "function %qux() system_v { 3383 block0: 3384 v4 = iconst.i8 6 3385 v3 -> v4 3386 v1 = iadd_imm v3, 17 3387 }", 3388 ) 3389 .parse_function() 3390 .unwrap(); 3391 assert_eq!(func.name.to_string(), "%qux"); 3392 let v4 = details.map.lookup_str("v4").unwrap(); 3393 assert_eq!(v4.to_string(), "v4"); 3394 let v3 = details.map.lookup_str("v3").unwrap(); 3395 assert_eq!(v3.to_string(), "v3"); 3396 match v3 { 3397 AnyEntity::Value(v3) => { 3398 let aliased_to = func.dfg.resolve_aliases(v3); 3399 assert_eq!(aliased_to.to_string(), "v4"); 3400 } 3401 _ => panic!("expected value: {v3}"), 3402 } 3403 } 3404 3405 #[test] 3406 fn signature() { 3407 let sig = Parser::new("()system_v").parse_signature().unwrap(); 3408 assert_eq!(sig.params.len(), 0); 3409 assert_eq!(sig.returns.len(), 0); 3410 assert_eq!(sig.call_conv, CallConv::SystemV); 3411 3412 let sig2 = 3413 Parser::new("(i8 uext, f16, f32, f64, f128, i32 sret) -> i32 sext, f64 system_v") 3414 .parse_signature() 3415 .unwrap(); 3416 assert_eq!( 3417 sig2.to_string(), 3418 "(i8 uext, f16, f32, f64, f128, i32 sret) -> i32 sext, f64 system_v" 3419 ); 3420 assert_eq!(sig2.call_conv, CallConv::SystemV); 3421 3422 // Old-style signature without a calling convention. 3423 assert_eq!( 3424 Parser::new("()").parse_signature().unwrap().to_string(), 3425 "() fast" 3426 ); 3427 assert_eq!( 3428 Parser::new("() notacc") 3429 .parse_signature() 3430 .unwrap_err() 3431 .to_string(), 3432 "1: unknown calling convention: notacc" 3433 ); 3434 3435 // `void` is not recognized as a type by the lexer. It should not appear in files. 3436 assert_eq!( 3437 Parser::new("() -> void") 3438 .parse_signature() 3439 .unwrap_err() 3440 .to_string(), 3441 "1: expected parameter type" 3442 ); 3443 assert_eq!( 3444 Parser::new("i8 -> i8") 3445 .parse_signature() 3446 .unwrap_err() 3447 .to_string(), 3448 "1: expected function signature: ( args... )" 3449 ); 3450 assert_eq!( 3451 Parser::new("(i8 -> i8") 3452 .parse_signature() 3453 .unwrap_err() 3454 .to_string(), 3455 "1: expected ')' after function arguments" 3456 ); 3457 } 3458 3459 #[test] 3460 fn stack_slot_decl() { 3461 let (func, _) = Parser::new( 3462 "function %foo() system_v { 3463 ss3 = explicit_slot 13 3464 ss1 = explicit_slot 1 3465 }", 3466 ) 3467 .parse_function() 3468 .unwrap(); 3469 assert_eq!(func.name.to_string(), "%foo"); 3470 let mut iter = func.sized_stack_slots.keys(); 3471 let _ss0 = iter.next().unwrap(); 3472 let ss1 = iter.next().unwrap(); 3473 assert_eq!(ss1.to_string(), "ss1"); 3474 assert_eq!( 3475 func.sized_stack_slots[ss1].kind, 3476 StackSlotKind::ExplicitSlot 3477 ); 3478 assert_eq!(func.sized_stack_slots[ss1].size, 1); 3479 let _ss2 = iter.next().unwrap(); 3480 let ss3 = iter.next().unwrap(); 3481 assert_eq!(ss3.to_string(), "ss3"); 3482 assert_eq!( 3483 func.sized_stack_slots[ss3].kind, 3484 StackSlotKind::ExplicitSlot 3485 ); 3486 assert_eq!(func.sized_stack_slots[ss3].size, 13); 3487 assert_eq!(iter.next(), None); 3488 3489 // Catch duplicate definitions. 3490 assert_eq!( 3491 Parser::new( 3492 "function %bar() system_v { 3493 ss1 = explicit_slot 13 3494 ss1 = explicit_slot 1 3495 }", 3496 ) 3497 .parse_function() 3498 .unwrap_err() 3499 .to_string(), 3500 "3: duplicate entity: ss1" 3501 ); 3502 } 3503 3504 #[test] 3505 fn block_header() { 3506 let (func, _) = Parser::new( 3507 "function %blocks() system_v { 3508 block0: 3509 block4(v3: i32): 3510 }", 3511 ) 3512 .parse_function() 3513 .unwrap(); 3514 assert_eq!(func.name.to_string(), "%blocks"); 3515 3516 let mut blocks = func.layout.blocks(); 3517 3518 let block0 = blocks.next().unwrap(); 3519 assert_eq!(func.dfg.block_params(block0), &[]); 3520 3521 let block4 = blocks.next().unwrap(); 3522 let block4_args = func.dfg.block_params(block4); 3523 assert_eq!(block4_args.len(), 1); 3524 assert_eq!(func.dfg.value_type(block4_args[0]), types::I32); 3525 } 3526 3527 #[test] 3528 fn duplicate_block() { 3529 let ParseError { 3530 location, 3531 message, 3532 is_warning, 3533 } = Parser::new( 3534 "function %blocks() system_v { 3535 block0: 3536 block0: 3537 return 2", 3538 ) 3539 .parse_function() 3540 .unwrap_err(); 3541 3542 assert_eq!(location.line_number, 3); 3543 assert_eq!(message, "duplicate entity: block0"); 3544 assert!(!is_warning); 3545 } 3546 3547 #[test] 3548 fn number_of_blocks() { 3549 let ParseError { 3550 location, 3551 message, 3552 is_warning, 3553 } = Parser::new( 3554 "function %a() { 3555 block100000:", 3556 ) 3557 .parse_function() 3558 .unwrap_err(); 3559 3560 assert_eq!(location.line_number, 2); 3561 assert_eq!(message, "too many blocks"); 3562 assert!(!is_warning); 3563 } 3564 3565 #[test] 3566 fn duplicate_ss() { 3567 let ParseError { 3568 location, 3569 message, 3570 is_warning, 3571 } = Parser::new( 3572 "function %blocks() system_v { 3573 ss0 = explicit_slot 8 3574 ss0 = explicit_slot 8", 3575 ) 3576 .parse_function() 3577 .unwrap_err(); 3578 3579 assert_eq!(location.line_number, 3); 3580 assert_eq!(message, "duplicate entity: ss0"); 3581 assert!(!is_warning); 3582 } 3583 3584 #[test] 3585 fn duplicate_gv() { 3586 let ParseError { 3587 location, 3588 message, 3589 is_warning, 3590 } = Parser::new( 3591 "function %blocks() system_v { 3592 gv0 = vmctx 3593 gv0 = vmctx", 3594 ) 3595 .parse_function() 3596 .unwrap_err(); 3597 3598 assert_eq!(location.line_number, 3); 3599 assert_eq!(message, "duplicate entity: gv0"); 3600 assert!(!is_warning); 3601 } 3602 3603 #[test] 3604 fn duplicate_sig() { 3605 let ParseError { 3606 location, 3607 message, 3608 is_warning, 3609 } = Parser::new( 3610 "function %blocks() system_v { 3611 sig0 = () 3612 sig0 = ()", 3613 ) 3614 .parse_function() 3615 .unwrap_err(); 3616 3617 assert_eq!(location.line_number, 3); 3618 assert_eq!(message, "duplicate entity: sig0"); 3619 assert!(!is_warning); 3620 } 3621 3622 #[test] 3623 fn duplicate_fn() { 3624 let ParseError { 3625 location, 3626 message, 3627 is_warning, 3628 } = Parser::new( 3629 "function %blocks() system_v { 3630 sig0 = () 3631 fn0 = %foo sig0 3632 fn0 = %foo sig0", 3633 ) 3634 .parse_function() 3635 .unwrap_err(); 3636 3637 assert_eq!(location.line_number, 4); 3638 assert_eq!(message, "duplicate entity: fn0"); 3639 assert!(!is_warning); 3640 } 3641 3642 #[test] 3643 fn comments() { 3644 let (func, Details { comments, .. }) = Parser::new( 3645 "; before 3646 function %comment() system_v { ; decl 3647 ss10 = explicit_slot 13 ; stackslot. 3648 ; Still stackslot. 3649 block0: ; Basic block 3650 trap user42; Instruction 3651 } ; Trailing. 3652 ; More trailing.", 3653 ) 3654 .parse_function() 3655 .unwrap(); 3656 assert_eq!(func.name.to_string(), "%comment"); 3657 assert_eq!(comments.len(), 7); // no 'before' comment. 3658 assert_eq!( 3659 comments[0], 3660 Comment { 3661 entity: AnyEntity::Function, 3662 text: "; decl", 3663 } 3664 ); 3665 assert_eq!(comments[1].entity.to_string(), "ss10"); 3666 assert_eq!(comments[2].entity.to_string(), "ss10"); 3667 assert_eq!(comments[2].text, "; Still stackslot."); 3668 assert_eq!(comments[3].entity.to_string(), "block0"); 3669 assert_eq!(comments[3].text, "; Basic block"); 3670 3671 assert_eq!(comments[4].entity.to_string(), "inst0"); 3672 assert_eq!(comments[4].text, "; Instruction"); 3673 3674 assert_eq!(comments[5].entity, AnyEntity::Function); 3675 assert_eq!(comments[6].entity, AnyEntity::Function); 3676 } 3677 3678 #[test] 3679 fn test_file() { 3680 let tf = parse_test( 3681 r#"; before 3682 test cfg option=5 3683 test verify 3684 set enable_float=false 3685 feature "foo" 3686 feature !"bar" 3687 ; still preamble 3688 function %comment() system_v {}"#, 3689 ParseOptions::default(), 3690 ) 3691 .unwrap(); 3692 assert_eq!(tf.commands.len(), 2); 3693 assert_eq!(tf.commands[0].command, "cfg"); 3694 assert_eq!(tf.commands[1].command, "verify"); 3695 match tf.isa_spec { 3696 IsaSpec::None(s) => { 3697 assert!(s.enable_verifier()); 3698 assert!(!s.enable_float()); 3699 } 3700 _ => panic!("unexpected ISAs"), 3701 } 3702 assert_eq!(tf.features[0], Feature::With(&"foo")); 3703 assert_eq!(tf.features[1], Feature::Without(&"bar")); 3704 assert_eq!(tf.preamble_comments.len(), 2); 3705 assert_eq!(tf.preamble_comments[0].text, "; before"); 3706 assert_eq!(tf.preamble_comments[1].text, "; still preamble"); 3707 assert_eq!(tf.functions.len(), 1); 3708 assert_eq!(tf.functions[0].0.name.to_string(), "%comment"); 3709 } 3710 3711 #[test] 3712 fn isa_spec() { 3713 assert!(parse_test( 3714 "target 3715 function %foo() system_v {}", 3716 ParseOptions::default() 3717 ) 3718 .is_err()); 3719 3720 assert!(parse_test( 3721 "target x86_64 3722 set enable_float=false 3723 function %foo() system_v {}", 3724 ParseOptions::default() 3725 ) 3726 .is_err()); 3727 3728 match parse_test( 3729 "set enable_float=false 3730 target x86_64 3731 function %foo() system_v {}", 3732 ParseOptions::default(), 3733 ) 3734 .unwrap() 3735 .isa_spec 3736 { 3737 IsaSpec::None(_) => panic!("Expected some ISA"), 3738 IsaSpec::Some(v) => { 3739 assert_eq!(v.len(), 1); 3740 assert!(v[0].name() == "x64" || v[0].name() == "x86"); 3741 } 3742 } 3743 } 3744 3745 #[test] 3746 fn user_function_name() { 3747 // Valid characters in the name: 3748 let func = Parser::new( 3749 "function u1:2() system_v { 3750 block0: 3751 trap int_divz 3752 }", 3753 ) 3754 .parse_function() 3755 .unwrap() 3756 .0; 3757 assert_eq!(func.name.to_string(), "u1:2"); 3758 3759 // Invalid characters in the name: 3760 let mut parser = Parser::new( 3761 "function u123:abc() system_v { 3762 block0: 3763 trap stk_ovf 3764 }", 3765 ); 3766 assert!(parser.parse_function().is_err()); 3767 3768 // Incomplete function names should not be valid: 3769 let mut parser = Parser::new( 3770 "function u() system_v { 3771 block0: 3772 trap int_ovf 3773 }", 3774 ); 3775 assert!(parser.parse_function().is_err()); 3776 3777 let mut parser = Parser::new( 3778 "function u0() system_v { 3779 block0: 3780 trap int_ovf 3781 }", 3782 ); 3783 assert!(parser.parse_function().is_err()); 3784 3785 let mut parser = Parser::new( 3786 "function u0:() system_v { 3787 block0: 3788 trap int_ovf 3789 }", 3790 ); 3791 assert!(parser.parse_function().is_err()); 3792 } 3793 3794 #[test] 3795 fn change_default_calling_convention() { 3796 let code = "function %test() { 3797 block0: 3798 return 3799 }"; 3800 3801 // By default the parser will use the fast calling convention if none is specified. 3802 let mut parser = Parser::new(code); 3803 assert_eq!( 3804 parser.parse_function().unwrap().0.signature.call_conv, 3805 CallConv::Fast 3806 ); 3807 3808 // However, we can specify a different calling convention to be the default. 3809 let mut parser = Parser::new(code).with_default_calling_convention(CallConv::Cold); 3810 assert_eq!( 3811 parser.parse_function().unwrap().0.signature.call_conv, 3812 CallConv::Cold 3813 ); 3814 } 3815 3816 #[test] 3817 fn u8_as_hex() { 3818 fn parse_as_uimm8(text: &str) -> ParseResult<u8> { 3819 Parser::new(text).match_uimm8("unable to parse u8") 3820 } 3821 3822 assert_eq!(parse_as_uimm8("0").unwrap(), 0); 3823 assert_eq!(parse_as_uimm8("0xff").unwrap(), 255); 3824 assert!(parse_as_uimm8("-1").is_err()); 3825 assert!(parse_as_uimm8("0xffa").is_err()); 3826 } 3827 3828 #[test] 3829 fn i16_as_hex() { 3830 fn parse_as_imm16(text: &str) -> ParseResult<i16> { 3831 Parser::new(text).match_imm16("unable to parse i16") 3832 } 3833 3834 assert_eq!(parse_as_imm16("0x8000").unwrap(), -32768); 3835 assert_eq!(parse_as_imm16("0xffff").unwrap(), -1); 3836 assert_eq!(parse_as_imm16("0").unwrap(), 0); 3837 assert_eq!(parse_as_imm16("0x7fff").unwrap(), 32767); 3838 assert_eq!( 3839 parse_as_imm16("-0x0001").unwrap(), 3840 parse_as_imm16("0xffff").unwrap() 3841 ); 3842 assert_eq!( 3843 parse_as_imm16("-0x7fff").unwrap(), 3844 parse_as_imm16("0x8001").unwrap() 3845 ); 3846 assert!(parse_as_imm16("0xffffa").is_err()); 3847 } 3848 3849 #[test] 3850 fn i32_as_hex() { 3851 fn parse_as_imm32(text: &str) -> ParseResult<i32> { 3852 Parser::new(text).match_imm32("unable to parse i32") 3853 } 3854 3855 assert_eq!(parse_as_imm32("0x80000000").unwrap(), -2147483648); 3856 assert_eq!(parse_as_imm32("0xffffffff").unwrap(), -1); 3857 assert_eq!(parse_as_imm32("0").unwrap(), 0); 3858 assert_eq!(parse_as_imm32("0x7fffffff").unwrap(), 2147483647); 3859 assert_eq!( 3860 parse_as_imm32("-0x00000001").unwrap(), 3861 parse_as_imm32("0xffffffff").unwrap() 3862 ); 3863 assert_eq!( 3864 parse_as_imm32("-0x7fffffff").unwrap(), 3865 parse_as_imm32("0x80000001").unwrap() 3866 ); 3867 assert!(parse_as_imm32("0xffffffffa").is_err()); 3868 } 3869 3870 #[test] 3871 fn i64_as_hex() { 3872 fn parse_as_imm64(text: &str) -> ParseResult<Imm64> { 3873 Parser::new(text).match_imm64("unable to parse Imm64") 3874 } 3875 3876 assert_eq!( 3877 parse_as_imm64("0x8000000000000000").unwrap(), 3878 Imm64::new(-9223372036854775808) 3879 ); 3880 assert_eq!( 3881 parse_as_imm64("0xffffffffffffffff").unwrap(), 3882 Imm64::new(-1) 3883 ); 3884 assert_eq!(parse_as_imm64("0").unwrap(), Imm64::new(0)); 3885 assert_eq!( 3886 parse_as_imm64("0x7fffffffffffffff").unwrap(), 3887 Imm64::new(9223372036854775807) 3888 ); 3889 assert_eq!( 3890 parse_as_imm64("-0x0000000000000001").unwrap(), 3891 parse_as_imm64("0xffffffffffffffff").unwrap() 3892 ); 3893 assert_eq!( 3894 parse_as_imm64("-0x7fffffffffffffff").unwrap(), 3895 parse_as_imm64("0x8000000000000001").unwrap() 3896 ); 3897 assert!(parse_as_imm64("0xffffffffffffffffa").is_err()); 3898 } 3899 3900 #[test] 3901 fn uimm128() { 3902 macro_rules! parse_as_constant_data { 3903 ($text:expr, $type:expr) => {{ 3904 Parser::new($text).parse_literals_to_constant_data($type) 3905 }}; 3906 } 3907 macro_rules! can_parse_as_constant_data { 3908 ($text:expr, $type:expr) => {{ 3909 assert!(parse_as_constant_data!($text, $type).is_ok()) 3910 }}; 3911 } 3912 macro_rules! cannot_parse_as_constant_data { 3913 ($text:expr, $type:expr) => {{ 3914 assert!(parse_as_constant_data!($text, $type).is_err()) 3915 }}; 3916 } 3917 3918 can_parse_as_constant_data!("1 2 3 4", I32X4); 3919 can_parse_as_constant_data!("1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16", I8X16); 3920 can_parse_as_constant_data!("0x1.1 0x2.2 0x3.3 0x4.4", F32X4); 3921 can_parse_as_constant_data!("0x0 0x1 0x2 0x3", I32X4); 3922 can_parse_as_constant_data!("-1 0 -1 0 -1 0 -1 0", I16X8); 3923 can_parse_as_constant_data!("0 -1", I64X2); 3924 can_parse_as_constant_data!("-1 0", I64X2); 3925 can_parse_as_constant_data!("-1 -1 -1 -1 -1", I32X4); // note that parse_literals_to_constant_data will leave extra tokens unconsumed 3926 3927 cannot_parse_as_constant_data!("1 2 3", I32X4); 3928 cannot_parse_as_constant_data!(" ", F32X4); 3929 } 3930 3931 #[test] 3932 fn parse_constant_from_booleans() { 3933 let c = Parser::new("-1 0 -1 0") 3934 .parse_literals_to_constant_data(I32X4) 3935 .unwrap(); 3936 assert_eq!( 3937 c.into_vec(), 3938 [0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0] 3939 ) 3940 } 3941 3942 #[test] 3943 fn parse_unbounded_constants() { 3944 // Unlike match_uimm128, match_hexadecimal_constant can parse byte sequences of any size: 3945 assert_eq!( 3946 Parser::new("0x0100") 3947 .match_hexadecimal_constant("err message") 3948 .unwrap(), 3949 vec![0, 1].into() 3950 ); 3951 3952 // Only parse hexadecimal constants: 3953 assert!(Parser::new("228") 3954 .match_hexadecimal_constant("err message") 3955 .is_err()); 3956 } 3957 3958 #[test] 3959 fn parse_run_commands() { 3960 // Helper for creating signatures. 3961 fn sig(ins: &[Type], outs: &[Type]) -> Signature { 3962 let mut sig = Signature::new(CallConv::Fast); 3963 for i in ins { 3964 sig.params.push(AbiParam::new(*i)); 3965 } 3966 for o in outs { 3967 sig.returns.push(AbiParam::new(*o)); 3968 } 3969 sig 3970 } 3971 3972 // Helper for parsing run commands. 3973 fn parse(text: &str, sig: &Signature) -> ParseResult<RunCommand> { 3974 Parser::new(text).parse_run_command(sig) 3975 } 3976 3977 // Check that we can parse and display the same set of run commands. 3978 fn assert_roundtrip(text: &str, sig: &Signature) { 3979 assert_eq!(parse(text, sig).unwrap().to_string(), text); 3980 } 3981 assert_roundtrip("run: %fn0() == 42", &sig(&[], &[I32])); 3982 assert_roundtrip( 3983 "run: %fn0(8, 16, 32, 64) == 1", 3984 &sig(&[I8, I16, I32, I64], &[I8]), 3985 ); 3986 assert_roundtrip( 3987 "run: %my_func(1) == 0x0f0e0d0c0b0a09080706050403020100", 3988 &sig(&[I32], &[I8X16]), 3989 ); 3990 3991 // Verify that default invocations are created when not specified. 3992 assert_eq!( 3993 parse("run", &sig(&[], &[I32])).unwrap().to_string(), 3994 "run: %default() != 0" 3995 ); 3996 assert_eq!( 3997 parse("print", &sig(&[], &[F32X4, I16X8])) 3998 .unwrap() 3999 .to_string(), 4000 "print: %default()" 4001 ); 4002 4003 // Demonstrate some unparsable cases. 4004 assert!(parse("print", &sig(&[I32], &[I32])).is_err()); 4005 assert!(parse("print:", &sig(&[], &[])).is_err()); 4006 assert!(parse("run: ", &sig(&[], &[])).is_err()); 4007 } 4008 4009 #[test] 4010 fn parse_data_values() { 4011 fn parse(text: &str, ty: Type) -> DataValue { 4012 Parser::new(text).parse_data_value(ty).unwrap() 4013 } 4014 4015 assert_eq!(parse("8", I8).to_string(), "8"); 4016 assert_eq!(parse("16", I16).to_string(), "16"); 4017 assert_eq!(parse("32", I32).to_string(), "32"); 4018 assert_eq!(parse("64", I64).to_string(), "64"); 4019 assert_eq!( 4020 parse("0x01234567_01234567_01234567_01234567", I128).to_string(), 4021 "1512366032949150931280199141537564007" 4022 ); 4023 assert_eq!(parse("1234567", I128).to_string(), "1234567"); 4024 assert_eq!(parse("0x16.1", F16).to_string(), "0x1.610p4"); 4025 assert_eq!(parse("0x32.32", F32).to_string(), "0x1.919000p5"); 4026 assert_eq!(parse("0x64.64", F64).to_string(), "0x1.9190000000000p6"); 4027 assert_eq!( 4028 parse("0x128.128", F128).to_string(), 4029 "0x1.2812800000000000000000000000p8" 4030 ); 4031 assert_eq!( 4032 parse("[0 1 2 3]", I32X4).to_string(), 4033 "0x00000003000000020000000100000000" 4034 ); 4035 } 4036 4037 #[test] 4038 fn parse_cold_blocks() { 4039 let code = "function %test() { 4040 block0 cold: 4041 return 4042 block1(v0: i32) cold: 4043 return 4044 block2(v1: i32): 4045 return 4046 }"; 4047 4048 let mut parser = Parser::new(code); 4049 let func = parser.parse_function().unwrap().0; 4050 assert_eq!(func.layout.blocks().count(), 3); 4051 assert!(func.layout.is_cold(Block::from_u32(0))); 4052 assert!(func.layout.is_cold(Block::from_u32(1))); 4053 assert!(!func.layout.is_cold(Block::from_u32(2))); 4054 } 4055 } 4056