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