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