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