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