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