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