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