cranelift_reader/
parser.rs

1//! Parser for .clif files.
2
3use crate::error::{Location, ParseError, ParseResult};
4use crate::isaspec;
5use crate::lexer::{LexError, Lexer, LocatedError, LocatedToken, Token};
6use crate::run_command::{Comparison, Invocation, RunCommand};
7use crate::sourcemap::SourceMap;
8use crate::testcommand::TestCommand;
9use crate::testfile::{Comment, Details, Feature, TestFile};
10use cranelift_codegen::data_value::DataValue;
11use cranelift_codegen::entity::{EntityRef, PrimaryMap};
12use cranelift_codegen::ir::entities::{AnyEntity, DynamicType, MemoryType};
13use cranelift_codegen::ir::immediates::{
14    Ieee16, Ieee32, Ieee64, Ieee128, Imm64, Offset32, Uimm32, Uimm64,
15};
16use cranelift_codegen::ir::instructions::{InstructionData, InstructionFormat, VariableArgs};
17use cranelift_codegen::ir::pcc::{BaseExpr, Expr, Fact};
18use cranelift_codegen::ir::types::*;
19use cranelift_codegen::ir::{self, UserExternalNameRef};
20
21use 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};
28use cranelift_codegen::isa::{self, CallConv};
29use cranelift_codegen::packed_option::ReservedValue;
30use cranelift_codegen::{settings, settings::Configurable, timing};
31use smallvec::SmallVec;
32use std::mem;
33use std::str::FromStr;
34use std::{u16, u32};
35use target_lexicon::Triple;
36
37macro_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.
87const 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.
92pub 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.
99pub 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
112impl 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`.
127pub 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.
185pub 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
199pub 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.
230struct Context {
231    function: Function,
232    map: SourceMap,
233
234    /// Aliases to resolve once value definitions are known.
235    aliases: Vec<Value>,
236}
237
238impl 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
448impl<'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    //                           | "context" value
1929    fn parse_exception_table(&mut self, ctx: &mut Context) -> ParseResult<ir::ExceptionTable> {
1930        let sig = self.match_sig("expected signature of called function")?;
1931        self.match_token(Token::Comma, "expected comma after signature argument")?;
1932
1933        let mut handlers = vec![];
1934
1935        let block_num = self.match_block("expected branch destination block")?;
1936        let args = self.parse_opt_block_call_args()?;
1937        let normal_return = ctx.function.dfg.block_call(block_num, &args);
1938
1939        self.match_token(
1940            Token::Comma,
1941            "expected comma after normal-return destination",
1942        )?;
1943
1944        self.match_token(
1945            Token::LBracket,
1946            "expected an open-bracket for exception table list",
1947        )?;
1948        loop {
1949            match self.token() {
1950                Some(Token::RBracket) => {
1951                    break;
1952                }
1953                Some(Token::ExceptionTag(tag)) => {
1954                    self.consume();
1955                    self.match_token(Token::Colon, "expected ':' after exception tag")?;
1956                    let tag = ir::ExceptionTag::from_u32(tag);
1957                    let block_num = self.match_block("expected branch destination block")?;
1958                    let args = self.parse_opt_block_call_args()?;
1959                    let block_call = ctx.function.dfg.block_call(block_num, &args);
1960                    handlers.push(ir::ExceptionTableItem::Tag(tag, block_call));
1961                }
1962                Some(Token::Identifier("default")) => {
1963                    self.consume();
1964                    self.match_token(Token::Colon, "expected ':' after 'default'")?;
1965                    let block_num = self.match_block("expected branch destination block")?;
1966                    let args = self.parse_opt_block_call_args()?;
1967                    let block_call = ctx.function.dfg.block_call(block_num, &args);
1968                    handlers.push(ir::ExceptionTableItem::Default(block_call));
1969                }
1970                Some(Token::Identifier("context")) => {
1971                    self.consume();
1972                    let val = self.match_value("expected value for exception-handler context")?;
1973                    handlers.push(ir::ExceptionTableItem::Context(val));
1974                }
1975                _ => return err!(self.loc, "invalid token"),
1976            }
1977
1978            if let Some(Token::Comma) = self.token() {
1979                self.consume();
1980            } else {
1981                break;
1982            }
1983        }
1984        self.match_token(Token::RBracket, "expected closing bracket")?;
1985
1986        Ok(ctx
1987            .function
1988            .dfg
1989            .exception_tables
1990            .push(ir::ExceptionTableData::new(sig, normal_return, handlers)))
1991    }
1992
1993    // Parse a constant decl.
1994    //
1995    // constant-decl ::= * Constant(c) "=" ty? "[" literal {"," literal} "]"
1996    fn parse_constant_decl(&mut self) -> ParseResult<(Constant, ConstantData)> {
1997        let name = self.match_constant()?;
1998        self.match_token(Token::Equal, "expected '=' in constant decl")?;
1999        let data = if let Some(Token::Type(_)) = self.token() {
2000            let ty = self.match_type("expected type of constant")?;
2001            self.match_uimm128(ty)
2002        } else {
2003            self.match_hexadecimal_constant("expected an immediate hexadecimal operand")
2004        }?;
2005
2006        // Collect any trailing comments.
2007        self.token();
2008        self.claim_gathered_comments(name);
2009
2010        Ok((name, data))
2011    }
2012
2013    // Parse a stack limit decl
2014    //
2015    // stack-limit-decl ::= * StackLimit "=" GlobalValue(gv)
2016    fn parse_stack_limit_decl(&mut self) -> ParseResult<GlobalValue> {
2017        self.match_stack_limit()?;
2018        self.match_token(Token::Equal, "expected '=' in stack limit decl")?;
2019        let limit = match self.token() {
2020            Some(Token::GlobalValue(base_num)) => match GlobalValue::with_number(base_num) {
2021                Some(gv) => gv,
2022                None => return err!(self.loc, "invalid global value number for stack limit"),
2023            },
2024            _ => return err!(self.loc, "expected global value"),
2025        };
2026        self.consume();
2027
2028        // Collect any trailing comments.
2029        self.token();
2030        self.claim_gathered_comments(AnyEntity::StackLimit);
2031
2032        Ok(limit)
2033    }
2034
2035    // Parse a function body, add contents to `ctx`.
2036    //
2037    // function-body ::= * { extended-basic-block }
2038    //
2039    fn parse_function_body(&mut self, ctx: &mut Context) -> ParseResult<()> {
2040        while self.token() != Some(Token::RBrace) {
2041            self.parse_basic_block(ctx)?;
2042        }
2043
2044        // Now that we've seen all defined values in the function, ensure that
2045        // all references refer to a definition.
2046        for block in &ctx.function.layout {
2047            for inst in ctx.function.layout.block_insts(block) {
2048                for value in ctx.function.dfg.inst_values(inst) {
2049                    if !ctx.map.contains_value(value) {
2050                        return err!(
2051                            ctx.map.location(AnyEntity::Inst(inst)).unwrap(),
2052                            "undefined operand value {}",
2053                            value
2054                        );
2055                    }
2056                }
2057            }
2058        }
2059
2060        for alias in &ctx.aliases {
2061            if !ctx.function.dfg.set_alias_type_for_parser(*alias) {
2062                let loc = ctx.map.location(AnyEntity::Value(*alias)).unwrap();
2063                return err!(loc, "alias cycle involving {}", alias);
2064            }
2065        }
2066
2067        Ok(())
2068    }
2069
2070    // Parse a basic block, add contents to `ctx`.
2071    //
2072    // extended-basic-block ::= * block-header { instruction }
2073    // block-header         ::= Block(block) [block-params] [block-flags] ":"
2074    // block-flags          ::= [Cold]
2075    //
2076    fn parse_basic_block(&mut self, ctx: &mut Context) -> ParseResult<()> {
2077        // Collect comments for the next block.
2078        self.start_gathering_comments();
2079
2080        let block_num = self.match_block("expected block header")?;
2081        let block = ctx.add_block(block_num, self.loc)?;
2082
2083        if block_num.as_u32() >= MAX_BLOCKS_IN_A_FUNCTION {
2084            return Err(self.error("too many blocks"));
2085        }
2086
2087        if self.token() == Some(Token::LPar) {
2088            self.parse_block_params(ctx, block)?;
2089        }
2090
2091        if self.optional(Token::Cold) {
2092            ctx.set_cold_block(block);
2093        }
2094
2095        self.match_token(Token::Colon, "expected ':' after block parameters")?;
2096
2097        // Collect any trailing comments.
2098        self.token();
2099        self.claim_gathered_comments(block);
2100
2101        // extended-basic-block ::= block-header * { instruction }
2102        while match self.token() {
2103            Some(Token::Value(_))
2104            | Some(Token::Identifier(_))
2105            | Some(Token::LBracket)
2106            | Some(Token::SourceLoc(_)) => true,
2107            _ => false,
2108        } {
2109            let srcloc = self.optional_srcloc()?;
2110
2111            // We need to parse instruction results here because they are shared
2112            // between the parsing of value aliases and the parsing of instructions.
2113            //
2114            // inst-results ::= Value(v) { "," Value(v) }
2115            let results = self.parse_inst_results(ctx)?;
2116
2117            for result in &results {
2118                while ctx.function.dfg.num_values() <= result.index() {
2119                    ctx.function.dfg.make_invalid_value_for_parser();
2120                }
2121            }
2122
2123            match self.token() {
2124                Some(Token::Arrow) => {
2125                    self.consume();
2126                    self.parse_value_alias(&results, ctx)?;
2127                }
2128                Some(Token::Equal) => {
2129                    self.consume();
2130                    self.parse_instruction(&results, srcloc, ctx, block)?;
2131                }
2132                _ if !results.is_empty() => return err!(self.loc, "expected -> or ="),
2133                _ => self.parse_instruction(&results, srcloc, ctx, block)?,
2134            }
2135        }
2136
2137        Ok(())
2138    }
2139
2140    // Parse parenthesized list of block parameters.
2141    //
2142    // block-params ::= * "(" ( block-param { "," block-param } )? ")"
2143    fn parse_block_params(&mut self, ctx: &mut Context, block: Block) -> ParseResult<()> {
2144        // block-params ::= * "(" ( block-param { "," block-param } )? ")"
2145        self.match_token(Token::LPar, "expected '(' before block parameters")?;
2146
2147        // block-params ::= "(" * ")"
2148        if self.token() == Some(Token::RPar) {
2149            self.consume();
2150            return Ok(());
2151        }
2152
2153        // block-params ::= "(" * block-param { "," block-param } ")"
2154        self.parse_block_param(ctx, block)?;
2155
2156        // block-params ::= "(" block-param * { "," block-param } ")"
2157        while self.optional(Token::Comma) {
2158            // block-params ::= "(" block-param { "," * block-param } ")"
2159            self.parse_block_param(ctx, block)?;
2160        }
2161
2162        // block-params ::= "(" block-param { "," block-param } * ")"
2163        self.match_token(Token::RPar, "expected ')' after block parameters")?;
2164
2165        Ok(())
2166    }
2167
2168    // Parse a single block parameter declaration, and append it to `block`.
2169    //
2170    // block-param ::= * Value(v) [ "!" fact ]  ":" Type(t) arg-loc?
2171    // arg-loc ::= "[" value-location "]"
2172    //
2173    fn parse_block_param(&mut self, ctx: &mut Context, block: Block) -> ParseResult<()> {
2174        // block-param ::= * Value(v) [ "!" fact ] ":" Type(t) arg-loc?
2175        let v = self.match_value("block argument must be a value")?;
2176        let v_location = self.loc;
2177        // block-param ::= Value(v) * [ "!" fact ]  ":" Type(t) arg-loc?
2178        let fact = if self.token() == Some(Token::Bang) {
2179            self.consume();
2180            // block-param ::= Value(v) [ "!" * fact ]  ":" Type(t) arg-loc?
2181            Some(self.parse_fact()?)
2182        } else {
2183            None
2184        };
2185        self.match_token(Token::Colon, "expected ':' after block argument")?;
2186        // block-param ::= Value(v) [ "!" fact ] ":" * Type(t) arg-loc?
2187
2188        while ctx.function.dfg.num_values() <= v.index() {
2189            ctx.function.dfg.make_invalid_value_for_parser();
2190        }
2191
2192        let t = self.match_type("expected block argument type")?;
2193        // Allocate the block argument.
2194        ctx.function.dfg.append_block_param_for_parser(block, t, v);
2195        ctx.map.def_value(v, v_location)?;
2196        ctx.function.dfg.facts[v] = fact;
2197
2198        Ok(())
2199    }
2200
2201    // Parse a "fact" for proof-carrying code, attached to a value.
2202    //
2203    // fact ::= "range" "(" bit-width "," min-value "," max-value ")"
2204    //        | "dynamic_range" "(" bit-width "," expr "," expr ")"
2205    //        | "mem" "(" memory-type "," mt-offset "," mt-offset [ "," "nullable" ] ")"
2206    //        | "dynamic_mem" "(" memory-type "," expr "," expr [ "," "nullable" ] ")"
2207    //        | "conflict"
2208    // bit-width ::= uimm64
2209    // min-value ::= uimm64
2210    // max-value ::= uimm64
2211    // valid-range ::= uimm64
2212    // mt-offset ::= uimm64
2213    fn parse_fact(&mut self) -> ParseResult<Fact> {
2214        match self.token() {
2215            Some(Token::Identifier("range")) => {
2216                self.consume();
2217                self.match_token(Token::LPar, "`range` fact needs an opening `(`")?;
2218                let bit_width: u64 = self
2219                    .match_uimm64("expected a bit-width value for `range` fact")?
2220                    .into();
2221                self.match_token(Token::Comma, "expected a comma")?;
2222                let min: u64 = self
2223                    .match_uimm64("expected a min value for `range` fact")?
2224                    .into();
2225                self.match_token(Token::Comma, "expected a comma")?;
2226                let max: u64 = self
2227                    .match_uimm64("expected a max value for `range` fact")?
2228                    .into();
2229                self.match_token(Token::RPar, "`range` fact needs a closing `)`")?;
2230                let bit_width_max = match bit_width {
2231                    x if x > 64 => {
2232                        return Err(self.error("bitwidth must be <= 64 bits on a `range` fact"));
2233                    }
2234                    64 => u64::MAX,
2235                    x => (1u64 << x) - 1,
2236                };
2237                if min > max {
2238                    return Err(self.error(
2239                        "min value must be less than or equal to max value on a `range` fact",
2240                    ));
2241                }
2242                if max > bit_width_max {
2243                    return Err(
2244                        self.error("max value is out of range for bitwidth on a `range` fact")
2245                    );
2246                }
2247                Ok(Fact::Range {
2248                    bit_width: u16::try_from(bit_width).unwrap(),
2249                    min,
2250                    max,
2251                })
2252            }
2253            Some(Token::Identifier("dynamic_range")) => {
2254                self.consume();
2255                self.match_token(Token::LPar, "`dynamic_range` fact needs an opening `(`")?;
2256                let bit_width: u64 = self
2257                    .match_uimm64("expected a bit-width value for `dynamic_range` fact")?
2258                    .into();
2259                self.match_token(Token::Comma, "expected a comma")?;
2260                let min = self.parse_expr()?;
2261                self.match_token(Token::Comma, "expected a comma")?;
2262                let max = self.parse_expr()?;
2263                self.match_token(Token::RPar, "`dynamic_range` fact needs a closing `)`")?;
2264                Ok(Fact::DynamicRange {
2265                    bit_width: u16::try_from(bit_width).unwrap(),
2266                    min,
2267                    max,
2268                })
2269            }
2270            Some(Token::Identifier("mem")) => {
2271                self.consume();
2272                self.match_token(Token::LPar, "expected a `(`")?;
2273                let ty = self.match_mt("expected a memory type for `mem` fact")?;
2274                self.match_token(
2275                    Token::Comma,
2276                    "expected a comma after memory type in `mem` fact",
2277                )?;
2278                let min_offset: u64 = self
2279                    .match_uimm64("expected a uimm64 minimum pointer offset for `mem` fact")?
2280                    .into();
2281                self.match_token(Token::Comma, "expected a comma after offset in `mem` fact")?;
2282                let max_offset: u64 = self
2283                    .match_uimm64("expected a uimm64 maximum pointer offset for `mem` fact")?
2284                    .into();
2285                let nullable = if self.token() == Some(Token::Comma) {
2286                    self.consume();
2287                    self.match_token(
2288                        Token::Identifier("nullable"),
2289                        "expected `nullable` in last optional field of `dynamic_mem`",
2290                    )?;
2291                    true
2292                } else {
2293                    false
2294                };
2295                self.match_token(Token::RPar, "expected a `)`")?;
2296                Ok(Fact::Mem {
2297                    ty,
2298                    min_offset,
2299                    max_offset,
2300                    nullable,
2301                })
2302            }
2303            Some(Token::Identifier("dynamic_mem")) => {
2304                self.consume();
2305                self.match_token(Token::LPar, "expected a `(`")?;
2306                let ty = self.match_mt("expected a memory type for `dynamic_mem` fact")?;
2307                self.match_token(
2308                    Token::Comma,
2309                    "expected a comma after memory type in `dynamic_mem` fact",
2310                )?;
2311                let min = self.parse_expr()?;
2312                self.match_token(
2313                    Token::Comma,
2314                    "expected a comma after offset in `dynamic_mem` fact",
2315                )?;
2316                let max = self.parse_expr()?;
2317                let nullable = if self.token() == Some(Token::Comma) {
2318                    self.consume();
2319                    self.match_token(
2320                        Token::Identifier("nullable"),
2321                        "expected `nullable` in last optional field of `dynamic_mem`",
2322                    )?;
2323                    true
2324                } else {
2325                    false
2326                };
2327                self.match_token(Token::RPar, "expected a `)`")?;
2328                Ok(Fact::DynamicMem {
2329                    ty,
2330                    min,
2331                    max,
2332                    nullable,
2333                })
2334            }
2335            Some(Token::Identifier("def")) => {
2336                self.consume();
2337                self.match_token(Token::LPar, "expected a `(`")?;
2338                let value = self.match_value("expected a value number in `def` fact")?;
2339                self.match_token(Token::RPar, "expected a `)`")?;
2340                Ok(Fact::Def { value })
2341            }
2342            Some(Token::Identifier("compare")) => {
2343                self.consume();
2344                self.match_token(Token::LPar, "expected a `(`")?;
2345                let kind = self.match_enum("expected intcc condition code in `compare` fact")?;
2346                self.match_token(
2347                    Token::Comma,
2348                    "expected comma in `compare` fact after condition code",
2349                )?;
2350                let lhs = self.parse_expr()?;
2351                self.match_token(Token::Comma, "expected comma in `compare` fact after LHS")?;
2352                let rhs = self.parse_expr()?;
2353                self.match_token(Token::RPar, "expected a `)`")?;
2354                Ok(Fact::Compare { kind, lhs, rhs })
2355            }
2356            Some(Token::Identifier("conflict")) => {
2357                self.consume();
2358                Ok(Fact::Conflict)
2359            }
2360            _ => Err(self.error(
2361                "expected a `range`, 'dynamic_range', `mem`, `dynamic_mem`, `def`, `compare` or `conflict` fact",
2362            )),
2363        }
2364    }
2365
2366    // Parse a dynamic expression used in some kinds of PCC facts.
2367    //
2368    // expr ::= base-expr
2369    //        | base-expr + uimm64  // but in-range for imm64
2370    //        | base-expr - uimm64  // but in-range for imm64
2371    //        | imm64
2372    fn parse_expr(&mut self) -> ParseResult<Expr> {
2373        if let Some(Token::Integer(_)) = self.token() {
2374            let offset: i64 = self
2375                .match_imm64("expected imm64 for dynamic expression")?
2376                .into();
2377            Ok(Expr {
2378                base: BaseExpr::None,
2379                offset,
2380            })
2381        } else {
2382            let base = self.parse_base_expr()?;
2383            match self.token() {
2384                Some(Token::Plus) => {
2385                    self.consume();
2386                    let offset: u64 = self
2387                        .match_uimm64(
2388                            "expected uimm64 in imm64 range for offset in dynamic expression",
2389                        )?
2390                        .into();
2391                    let offset: i64 = i64::try_from(offset).map_err(|_| {
2392                        self.error("integer offset in dynamic expression is out of range")
2393                    })?;
2394                    Ok(Expr { base, offset })
2395                }
2396                Some(Token::Integer(x)) if x.starts_with("-") => {
2397                    let offset: i64 = self
2398                        .match_imm64("expected an imm64 range for offset in dynamic expression")?
2399                        .into();
2400                    Ok(Expr { base, offset })
2401                }
2402                _ => Ok(Expr { base, offset: 0 }),
2403            }
2404        }
2405    }
2406
2407    // Parse the base part of a dynamic expression, used in some PCC facts.
2408    //
2409    // base-expr ::= GlobalValue(base)
2410    //             | Value(base)
2411    //             | "max"
2412    //             | (epsilon)
2413    fn parse_base_expr(&mut self) -> ParseResult<BaseExpr> {
2414        match self.token() {
2415            Some(Token::Identifier("max")) => {
2416                self.consume();
2417                Ok(BaseExpr::Max)
2418            }
2419            Some(Token::GlobalValue(..)) => {
2420                let gv = self.match_gv("expected global value")?;
2421                Ok(BaseExpr::GlobalValue(gv))
2422            }
2423            Some(Token::Value(..)) => {
2424                let value = self.match_value("expected value")?;
2425                Ok(BaseExpr::Value(value))
2426            }
2427            _ => Ok(BaseExpr::None),
2428        }
2429    }
2430
2431    // Parse instruction results and return them.
2432    //
2433    // inst-results ::= Value(v) { "," Value(v) }
2434    //
2435    fn parse_inst_results(&mut self, ctx: &mut Context) -> ParseResult<SmallVec<[Value; 1]>> {
2436        // Result value numbers.
2437        let mut results = SmallVec::new();
2438
2439        // instruction  ::=  * [inst-results "="] Opcode(opc) ["." Type] ...
2440        // inst-results ::= * Value(v) { "," Value(v) }
2441        if let Some(Token::Value(v)) = self.token() {
2442            self.consume();
2443
2444            results.push(v);
2445
2446            let fact = if self.token() == Some(Token::Bang) {
2447                self.consume();
2448                // block-param ::= Value(v) [ "!" * fact ]  ":" Type(t) arg-loc?
2449                Some(self.parse_fact()?)
2450            } else {
2451                None
2452            };
2453            ctx.function.dfg.facts[v] = fact;
2454
2455            // inst-results ::= Value(v) * { "," Value(v) }
2456            while self.optional(Token::Comma) {
2457                // inst-results ::= Value(v) { "," * Value(v) }
2458                let v = self.match_value("expected result value")?;
2459                results.push(v);
2460
2461                let fact = if self.token() == Some(Token::Bang) {
2462                    self.consume();
2463                    // block-param ::= Value(v) [ "!" * fact ]  ":" Type(t) arg-loc?
2464                    Some(self.parse_fact()?)
2465                } else {
2466                    None
2467                };
2468                ctx.function.dfg.facts[v] = fact;
2469            }
2470        }
2471
2472        Ok(results)
2473    }
2474
2475    // Parse a value alias, and append it to `block`.
2476    //
2477    // value_alias ::= [inst-results] "->" Value(v)
2478    //
2479    fn parse_value_alias(&mut self, results: &[Value], ctx: &mut Context) -> ParseResult<()> {
2480        if results.len() != 1 {
2481            return err!(self.loc, "wrong number of aliases");
2482        }
2483        let result = results[0];
2484        let dest = self.match_value("expected value alias")?;
2485
2486        // Allow duplicate definitions of aliases, as long as they are identical.
2487        if ctx.map.contains_value(result) {
2488            if let Some(old) = ctx.function.dfg.value_alias_dest_for_serialization(result) {
2489                if old != dest {
2490                    return err!(
2491                        self.loc,
2492                        "value {} is already defined as an alias with destination {}",
2493                        result,
2494                        old
2495                    );
2496                }
2497            } else {
2498                return err!(self.loc, "value {} is already defined");
2499            }
2500        } else {
2501            ctx.map.def_value(result, self.loc)?;
2502        }
2503
2504        if !ctx.map.contains_value(dest) {
2505            return err!(self.loc, "value {} is not yet defined", dest);
2506        }
2507
2508        ctx.function
2509            .dfg
2510            .make_value_alias_for_serialization(dest, result);
2511
2512        ctx.aliases.push(result);
2513        Ok(())
2514    }
2515
2516    // Parse an instruction, append it to `block`.
2517    //
2518    // instruction ::= [inst-results "="] Opcode(opc) ["." Type] ...
2519    //
2520    fn parse_instruction(
2521        &mut self,
2522        results: &[Value],
2523        srcloc: ir::SourceLoc,
2524        ctx: &mut Context,
2525        block: Block,
2526    ) -> ParseResult<()> {
2527        // Define the result values.
2528        for val in results {
2529            ctx.map.def_value(*val, self.loc)?;
2530        }
2531
2532        // Collect comments for the next instruction.
2533        self.start_gathering_comments();
2534
2535        // instruction ::=  [inst-results "="] * Opcode(opc) ["." Type] ...
2536        let opcode = if let Some(Token::Identifier(text)) = self.token() {
2537            match text.parse() {
2538                Ok(opc) => opc,
2539                Err(msg) => return err!(self.loc, "{}: '{}'", msg, text),
2540            }
2541        } else {
2542            return err!(self.loc, "expected instruction opcode");
2543        };
2544        let opcode_loc = self.loc;
2545        self.consume();
2546
2547        // Look for a controlling type variable annotation.
2548        // instruction ::=  [inst-results "="] Opcode(opc) * ["." Type] ...
2549        let explicit_ctrl_type = if self.optional(Token::Dot) {
2550            if let Some(Token::Type(_t)) = self.token() {
2551                Some(self.match_type("expected type after 'opcode.'")?)
2552            } else {
2553                let dt = self.match_dt("expected dynamic type")?;
2554                self.concrete_from_dt(dt, ctx)
2555            }
2556        } else {
2557            None
2558        };
2559
2560        // instruction ::=  [inst-results "="] Opcode(opc) ["." Type] * ...
2561        let inst_data = self.parse_inst_operands(ctx, opcode, explicit_ctrl_type)?;
2562
2563        // We're done parsing the instruction data itself.
2564        //
2565        // We still need to check that the number of result values in the source
2566        // matches the opcode or function call signature. We also need to create
2567        // values with the right type for all the instruction results and parse
2568        // and attach stack map entries, if present.
2569        let ctrl_typevar = self.infer_typevar(ctx, opcode, explicit_ctrl_type, &inst_data)?;
2570        let inst = ctx.function.dfg.make_inst(inst_data);
2571        if opcode.is_call() && !opcode.is_return() && self.optional(Token::Comma) {
2572            self.match_identifier("stack_map", "expected `stack_map = [...]`")?;
2573            self.match_token(Token::Equal, "expected `= [...]`")?;
2574            self.match_token(Token::LBracket, "expected `[...]`")?;
2575            while !self.optional(Token::RBracket) {
2576                let ty = self.match_type("expected `<type> @ <slot> + <offset>`")?;
2577                self.match_token(Token::At, "expected `@ <slot> + <offset>`")?;
2578                let slot = self.match_ss("expected `<slot> + <offset>`")?;
2579                let offset: u32 = match self.token() {
2580                    Some(Token::Integer(s)) if s.starts_with('+') => {
2581                        self.match_uimm32("expected a u32 offset")?.into()
2582                    }
2583                    _ => {
2584                        self.match_token(Token::Plus, "expected `+ <offset>`")?;
2585                        self.match_uimm32("expected a u32 offset")?.into()
2586                    }
2587                };
2588                ctx.function
2589                    .dfg
2590                    .append_user_stack_map_entry(inst, ir::UserStackMapEntry { ty, slot, offset });
2591                if !self.optional(Token::Comma) {
2592                    self.match_token(Token::RBracket, "expected `,` or `]`")?;
2593                    break;
2594                }
2595            }
2596        }
2597        let num_results =
2598            ctx.function
2599                .dfg
2600                .make_inst_results_for_parser(inst, ctrl_typevar, results);
2601        ctx.function.layout.append_inst(inst, block);
2602        ctx.map
2603            .def_entity(inst.into(), opcode_loc)
2604            .expect("duplicate inst references created");
2605
2606        if !srcloc.is_default() {
2607            ctx.function.set_srcloc(inst, srcloc);
2608        }
2609
2610        if results.len() != num_results {
2611            return err!(
2612                self.loc,
2613                "instruction produces {} result values, {} given",
2614                num_results,
2615                results.len()
2616            );
2617        }
2618
2619        // Collect any trailing comments.
2620        self.token();
2621        self.claim_gathered_comments(inst);
2622
2623        Ok(())
2624    }
2625
2626    // Type inference for polymorphic instructions.
2627    //
2628    // The controlling type variable can be specified explicitly as 'splat.i32x4 v5', or it can be
2629    // inferred from `inst_data.typevar_operand` for some opcodes.
2630    //
2631    // Returns the controlling typevar for a polymorphic opcode, or `INVALID` for a non-polymorphic
2632    // opcode.
2633    fn infer_typevar(
2634        &self,
2635        ctx: &Context,
2636        opcode: Opcode,
2637        explicit_ctrl_type: Option<Type>,
2638        inst_data: &InstructionData,
2639    ) -> ParseResult<Type> {
2640        let constraints = opcode.constraints();
2641        let ctrl_type = match explicit_ctrl_type {
2642            Some(t) => t,
2643            None => {
2644                if constraints.use_typevar_operand() {
2645                    // This is an opcode that supports type inference, AND there was no
2646                    // explicit type specified. Look up `ctrl_value` to see if it was defined
2647                    // already.
2648                    // TBD: If it is defined in another block, the type should have been
2649                    // specified explicitly. It is unfortunate that the correctness of IR
2650                    // depends on the layout of the blocks.
2651                    let ctrl_src_value = inst_data
2652                        .typevar_operand(&ctx.function.dfg.value_lists)
2653                        .expect("Constraints <-> Format inconsistency");
2654                    if !ctx.map.contains_value(ctrl_src_value) {
2655                        return err!(
2656                            self.loc,
2657                            "type variable required for polymorphic opcode, e.g. '{}.{}'; \
2658                             can't infer from {} which is not yet defined",
2659                            opcode,
2660                            constraints.ctrl_typeset().unwrap().example(),
2661                            ctrl_src_value
2662                        );
2663                    }
2664                    if !ctx.function.dfg.value_is_valid_for_parser(ctrl_src_value) {
2665                        return err!(
2666                            self.loc,
2667                            "type variable required for polymorphic opcode, e.g. '{}.{}'; \
2668                             can't infer from {} which is not yet resolved",
2669                            opcode,
2670                            constraints.ctrl_typeset().unwrap().example(),
2671                            ctrl_src_value
2672                        );
2673                    }
2674                    ctx.function.dfg.value_type(ctrl_src_value)
2675                } else if constraints.is_polymorphic() {
2676                    // This opcode does not support type inference, so the explicit type
2677                    // variable is required.
2678                    return err!(
2679                        self.loc,
2680                        "type variable required for polymorphic opcode, e.g. '{}.{}'",
2681                        opcode,
2682                        constraints.ctrl_typeset().unwrap().example()
2683                    );
2684                } else {
2685                    // This is a non-polymorphic opcode. No typevar needed.
2686                    INVALID
2687                }
2688            }
2689        };
2690
2691        // Verify that `ctrl_type` is valid for the controlling type variable. We don't want to
2692        // attempt deriving types from an incorrect basis.
2693        // This is not a complete type check. The verifier does that.
2694        if let Some(typeset) = constraints.ctrl_typeset() {
2695            // This is a polymorphic opcode.
2696            if !typeset.contains(ctrl_type) {
2697                return err!(
2698                    self.loc,
2699                    "{} is not a valid typevar for {}",
2700                    ctrl_type,
2701                    opcode
2702                );
2703            }
2704        // Treat it as a syntax error to specify a typevar on a non-polymorphic opcode.
2705        } else if ctrl_type != INVALID {
2706            return err!(self.loc, "{} does not take a typevar", opcode);
2707        }
2708
2709        Ok(ctrl_type)
2710    }
2711
2712    // Parse comma-separated value list into a VariableArgs struct.
2713    //
2714    // value_list ::= [ value { "," value } ]
2715    //
2716    fn parse_value_list(&mut self) -> ParseResult<VariableArgs> {
2717        let mut args = VariableArgs::new();
2718
2719        if let Some(Token::Value(v)) = self.token() {
2720            args.push(v);
2721            self.consume();
2722        } else {
2723            return Ok(args);
2724        }
2725
2726        while self.optional(Token::Comma) {
2727            args.push(self.match_value("expected value in argument list")?);
2728        }
2729
2730        Ok(args)
2731    }
2732
2733    /// Parse an optional list of block-call arguments enclosed in
2734    /// parentheses.
2735    fn parse_opt_block_call_args(&mut self) -> ParseResult<Vec<BlockArg>> {
2736        if !self.optional(Token::LPar) {
2737            return Ok(vec![]);
2738        }
2739
2740        let mut args = vec![];
2741        while self.token() != Some(Token::RPar) {
2742            args.push(self.parse_block_call_arg()?);
2743            if self.token() == Some(Token::Comma) {
2744                self.consume();
2745            } else {
2746                break;
2747            }
2748        }
2749
2750        self.match_token(Token::RPar, "expected ')' after arguments")?;
2751
2752        Ok(args)
2753    }
2754
2755    fn parse_block_call_arg(&mut self) -> ParseResult<BlockArg> {
2756        match self.token() {
2757            Some(Token::Value(v)) => {
2758                self.consume();
2759                Ok(BlockArg::Value(v))
2760            }
2761            Some(Token::TryCallRet(i)) => {
2762                self.consume();
2763                Ok(BlockArg::TryCallRet(i))
2764            }
2765            Some(Token::TryCallExn(i)) => {
2766                self.consume();
2767                Ok(BlockArg::TryCallExn(i))
2768            }
2769            tok => Err(self.error(&format!("unexpected token: {tok:?}"))),
2770        }
2771    }
2772
2773    /// Parse a CLIF run command.
2774    ///
2775    /// run-command ::= "run" [":" invocation comparison expected]
2776    ///               \ "print" [":" invocation]
2777    fn parse_run_command(&mut self, sig: &Signature) -> ParseResult<RunCommand> {
2778        // skip semicolon
2779        match self.token() {
2780            Some(Token::Identifier("run")) => {
2781                self.consume();
2782                if self.optional(Token::Colon) {
2783                    let invocation = self.parse_run_invocation(sig)?;
2784                    let comparison = self.parse_run_comparison()?;
2785                    let expected = self.parse_run_returns(sig)?;
2786                    Ok(RunCommand::Run(invocation, comparison, expected))
2787                } else if sig.params.is_empty()
2788                    && sig.returns.len() == 1
2789                    && sig.returns[0].value_type.is_int()
2790                {
2791                    // To match the existing run behavior that does not require an explicit
2792                    // invocation, we create an invocation from a function like `() -> i*` and
2793                    // require the result to be non-zero.
2794                    let invocation = Invocation::new("default", vec![]);
2795                    let expected = vec![DataValue::I8(0)];
2796                    let comparison = Comparison::NotEquals;
2797                    Ok(RunCommand::Run(invocation, comparison, expected))
2798                } else {
2799                    Err(self.error("unable to parse the run command"))
2800                }
2801            }
2802            Some(Token::Identifier("print")) => {
2803                self.consume();
2804                if self.optional(Token::Colon) {
2805                    Ok(RunCommand::Print(self.parse_run_invocation(sig)?))
2806                } else if sig.params.is_empty() {
2807                    // To allow printing of functions like `() -> *`, we create a no-arg invocation.
2808                    let invocation = Invocation::new("default", vec![]);
2809                    Ok(RunCommand::Print(invocation))
2810                } else {
2811                    Err(self.error("unable to parse the print command"))
2812                }
2813            }
2814            _ => Err(self.error("expected a 'run:' or 'print:' command")),
2815        }
2816    }
2817
2818    /// Parse the invocation of a CLIF function.
2819    ///
2820    /// This is different from parsing a CLIF `call`; it is used in parsing run commands like
2821    /// `run: %fn(42, 4.2) == false`.
2822    ///
2823    /// invocation ::= name "(" [data-value-list] ")"
2824    fn parse_run_invocation(&mut self, sig: &Signature) -> ParseResult<Invocation> {
2825        if let Some(Token::Name(name)) = self.token() {
2826            self.consume();
2827            self.match_token(
2828                Token::LPar,
2829                "expected invocation parentheses, e.g. %fn(...)",
2830            )?;
2831
2832            let arg_types = sig
2833                .params
2834                .iter()
2835                .map(|abi| abi.value_type)
2836                .collect::<Vec<_>>();
2837            let args = self.parse_data_value_list(&arg_types)?;
2838
2839            self.match_token(
2840                Token::RPar,
2841                "expected invocation parentheses, e.g. %fn(...)",
2842            )?;
2843            Ok(Invocation::new(name, args))
2844        } else {
2845            Err(self.error("expected a function name, e.g. %my_fn"))
2846        }
2847    }
2848
2849    /// Parse a comparison operator for run commands.
2850    ///
2851    /// comparison ::= "==" | "!="
2852    fn parse_run_comparison(&mut self) -> ParseResult<Comparison> {
2853        if self.optional(Token::Equal) {
2854            self.match_token(Token::Equal, "expected another =")?;
2855            Ok(Comparison::Equals)
2856        } else if self.optional(Token::Bang) {
2857            self.match_token(Token::Equal, "expected a =")?;
2858            Ok(Comparison::NotEquals)
2859        } else {
2860            Err(self.error("unable to parse a valid comparison operator"))
2861        }
2862    }
2863
2864    /// Parse the expected return values of a run invocation.
2865    ///
2866    /// expected ::= "[" "]"
2867    ///            | data-value
2868    ///            | "[" data-value-list "]"
2869    fn parse_run_returns(&mut self, sig: &Signature) -> ParseResult<Vec<DataValue>> {
2870        if sig.returns.len() != 1 {
2871            self.match_token(Token::LBracket, "expected a left bracket [")?;
2872        }
2873
2874        let returns = self
2875            .parse_data_value_list(&sig.returns.iter().map(|a| a.value_type).collect::<Vec<_>>())?;
2876
2877        if sig.returns.len() != 1 {
2878            self.match_token(Token::RBracket, "expected a right bracket ]")?;
2879        }
2880        Ok(returns)
2881    }
2882
2883    /// Parse a comma-separated list of data values.
2884    ///
2885    /// data-value-list ::= [data-value {"," data-value-list}]
2886    fn parse_data_value_list(&mut self, types: &[Type]) -> ParseResult<Vec<DataValue>> {
2887        let mut values = vec![];
2888        for ty in types.iter().take(1) {
2889            values.push(self.parse_data_value(*ty)?);
2890        }
2891        for ty in types.iter().skip(1) {
2892            self.match_token(
2893                Token::Comma,
2894                "expected a comma between invocation arguments",
2895            )?;
2896            values.push(self.parse_data_value(*ty)?);
2897        }
2898        Ok(values)
2899    }
2900
2901    /// Parse a data value; e.g. `42`, `4.2`, `true`.
2902    ///
2903    /// data-value-list ::= [data-value {"," data-value-list}]
2904    fn parse_data_value(&mut self, ty: Type) -> ParseResult<DataValue> {
2905        let dv = match ty {
2906            I8 => DataValue::from(self.match_imm8("expected a i8")?),
2907            I16 => DataValue::from(self.match_imm16("expected an i16")?),
2908            I32 => DataValue::from(self.match_imm32("expected an i32")?),
2909            I64 => DataValue::from(Into::<i64>::into(self.match_imm64("expected an i64")?)),
2910            I128 => DataValue::from(self.match_imm128("expected an i128")?),
2911            F16 => DataValue::from(self.match_ieee16("expected an f16")?),
2912            F32 => DataValue::from(self.match_ieee32("expected an f32")?),
2913            F64 => DataValue::from(self.match_ieee64("expected an f64")?),
2914            F128 => DataValue::from(self.match_ieee128("expected an f128")?),
2915            _ if (ty.is_vector() || ty.is_dynamic_vector()) => {
2916                let as_vec = self.match_uimm128(ty)?.into_vec();
2917                let slice = as_vec.as_slice();
2918                match slice.len() {
2919                    16 => DataValue::V128(slice.try_into().unwrap()),
2920                    8 => DataValue::V64(slice.try_into().unwrap()),
2921                    4 => DataValue::V32(slice.try_into().unwrap()),
2922                    2 => DataValue::V16(slice.try_into().unwrap()),
2923                    _ => {
2924                        return Err(
2925                            self.error("vectors larger than 128 bits are not currently supported")
2926                        );
2927                    }
2928                }
2929            }
2930            _ => return Err(self.error(&format!("don't know how to parse data values of: {ty}"))),
2931        };
2932        Ok(dv)
2933    }
2934
2935    // Parse the operands following the instruction opcode.
2936    // This depends on the format of the opcode.
2937    fn parse_inst_operands(
2938        &mut self,
2939        ctx: &mut Context,
2940        opcode: Opcode,
2941        explicit_control_type: Option<Type>,
2942    ) -> ParseResult<InstructionData> {
2943        let idata = match opcode.format() {
2944            InstructionFormat::Unary => InstructionData::Unary {
2945                opcode,
2946                arg: self.match_value("expected SSA value operand")?,
2947            },
2948            InstructionFormat::UnaryImm => {
2949                let msg = |bits| format!("expected immediate {bits}-bit integer operand");
2950                let unsigned = match explicit_control_type {
2951                    Some(types::I8) => self.match_imm8(&msg(8))? as u8 as i64,
2952                    Some(types::I16) => self.match_imm16(&msg(16))? as u16 as i64,
2953                    Some(types::I32) => self.match_imm32(&msg(32))? as u32 as i64,
2954                    Some(types::I64) => self.match_imm64(&msg(64))?.bits(),
2955                    _ => {
2956                        return err!(
2957                            self.loc,
2958                            "expected one of the following type: i8, i16, i32 or i64"
2959                        );
2960                    }
2961                };
2962                InstructionData::UnaryImm {
2963                    opcode,
2964                    imm: Imm64::new(unsigned),
2965                }
2966            }
2967            InstructionFormat::UnaryIeee16 => InstructionData::UnaryIeee16 {
2968                opcode,
2969                imm: self.match_ieee16("expected immediate 16-bit float operand")?,
2970            },
2971            InstructionFormat::UnaryIeee32 => InstructionData::UnaryIeee32 {
2972                opcode,
2973                imm: self.match_ieee32("expected immediate 32-bit float operand")?,
2974            },
2975            InstructionFormat::UnaryIeee64 => InstructionData::UnaryIeee64 {
2976                opcode,
2977                imm: self.match_ieee64("expected immediate 64-bit float operand")?,
2978            },
2979            InstructionFormat::UnaryConst => {
2980                let constant_handle = if let Some(Token::Constant(_)) = self.token() {
2981                    // If handed a `const?`, use that.
2982                    let c = self.match_constant()?;
2983                    ctx.check_constant(c, self.loc)?;
2984                    c
2985                } else if opcode == Opcode::F128const {
2986                    let ieee128 = self.match_ieee128("expected immediate 128-bit float operand")?;
2987                    ctx.function.dfg.constants.insert(ieee128.into())
2988                } else if let Some(controlling_type) = explicit_control_type {
2989                    // If an explicit control type is present, we expect a sized value and insert
2990                    // it in the constant pool.
2991                    let uimm128 = self.match_uimm128(controlling_type)?;
2992                    ctx.function.dfg.constants.insert(uimm128)
2993                } else {
2994                    return err!(
2995                        self.loc,
2996                        "Expected either a const entity or a typed value, e.g. inst.i32x4 [...]"
2997                    );
2998                };
2999                InstructionData::UnaryConst {
3000                    opcode,
3001                    constant_handle,
3002                }
3003            }
3004            InstructionFormat::UnaryGlobalValue => {
3005                let gv = self.match_gv("expected global value")?;
3006                ctx.check_gv(gv, self.loc)?;
3007                InstructionData::UnaryGlobalValue {
3008                    opcode,
3009                    global_value: gv,
3010                }
3011            }
3012            InstructionFormat::Binary => {
3013                let lhs = self.match_value("expected SSA value first operand")?;
3014                self.match_token(Token::Comma, "expected ',' between operands")?;
3015                let rhs = self.match_value("expected SSA value second operand")?;
3016                InstructionData::Binary {
3017                    opcode,
3018                    args: [lhs, rhs],
3019                }
3020            }
3021            InstructionFormat::BinaryImm8 => {
3022                let arg = self.match_value("expected SSA value first operand")?;
3023                self.match_token(Token::Comma, "expected ',' between operands")?;
3024                let imm = self.match_uimm8("expected unsigned 8-bit immediate")?;
3025                InstructionData::BinaryImm8 { opcode, arg, imm }
3026            }
3027            InstructionFormat::BinaryImm64 => {
3028                let lhs = self.match_value("expected SSA value first operand")?;
3029                self.match_token(Token::Comma, "expected ',' between operands")?;
3030                let rhs = self.match_imm64("expected immediate integer second operand")?;
3031                InstructionData::BinaryImm64 {
3032                    opcode,
3033                    arg: lhs,
3034                    imm: rhs,
3035                }
3036            }
3037            InstructionFormat::Ternary => {
3038                // Names here refer to the `select` instruction.
3039                // This format is also use by `fma`.
3040                let ctrl_arg = self.match_value("expected SSA value control operand")?;
3041                self.match_token(Token::Comma, "expected ',' between operands")?;
3042                let true_arg = self.match_value("expected SSA value true operand")?;
3043                self.match_token(Token::Comma, "expected ',' between operands")?;
3044                let false_arg = self.match_value("expected SSA value false operand")?;
3045                InstructionData::Ternary {
3046                    opcode,
3047                    args: [ctrl_arg, true_arg, false_arg],
3048                }
3049            }
3050            InstructionFormat::MultiAry => {
3051                let args = self.parse_value_list()?;
3052                InstructionData::MultiAry {
3053                    opcode,
3054                    args: args.into_value_list(&[], &mut ctx.function.dfg.value_lists),
3055                }
3056            }
3057            InstructionFormat::NullAry => InstructionData::NullAry { opcode },
3058            InstructionFormat::Jump => {
3059                // Parse the destination block number.
3060                let block_num = self.match_block("expected jump destination block")?;
3061                let args = self.parse_opt_block_call_args()?;
3062                let destination = ctx.function.dfg.block_call(block_num, &args);
3063                InstructionData::Jump {
3064                    opcode,
3065                    destination,
3066                }
3067            }
3068            InstructionFormat::Brif => {
3069                let arg = self.match_value("expected SSA value control operand")?;
3070                self.match_token(Token::Comma, "expected ',' between operands")?;
3071                let block_then = {
3072                    let block_num = self.match_block("expected branch then block")?;
3073                    let args = self.parse_opt_block_call_args()?;
3074                    ctx.function.dfg.block_call(block_num, &args)
3075                };
3076                self.match_token(Token::Comma, "expected ',' between operands")?;
3077                let block_else = {
3078                    let block_num = self.match_block("expected branch else block")?;
3079                    let args = self.parse_opt_block_call_args()?;
3080                    ctx.function.dfg.block_call(block_num, &args)
3081                };
3082                InstructionData::Brif {
3083                    opcode,
3084                    arg,
3085                    blocks: [block_then, block_else],
3086                }
3087            }
3088            InstructionFormat::BranchTable => {
3089                let arg = self.match_value("expected SSA value operand")?;
3090                self.match_token(Token::Comma, "expected ',' between operands")?;
3091                let block_num = self.match_block("expected branch destination block")?;
3092                let args = self.parse_opt_block_call_args()?;
3093                let destination = ctx.function.dfg.block_call(block_num, &args);
3094                self.match_token(Token::Comma, "expected ',' between operands")?;
3095                let table = self.parse_jump_table(ctx, destination)?;
3096                InstructionData::BranchTable { opcode, arg, table }
3097            }
3098            InstructionFormat::TernaryImm8 => {
3099                let lhs = self.match_value("expected SSA value first operand")?;
3100                self.match_token(Token::Comma, "expected ',' between operands")?;
3101                let rhs = self.match_value("expected SSA value last operand")?;
3102                self.match_token(Token::Comma, "expected ',' between operands")?;
3103                let imm = self.match_uimm8("expected 8-bit immediate")?;
3104                InstructionData::TernaryImm8 {
3105                    opcode,
3106                    imm,
3107                    args: [lhs, rhs],
3108                }
3109            }
3110            InstructionFormat::Shuffle => {
3111                let a = self.match_value("expected SSA value first operand")?;
3112                self.match_token(Token::Comma, "expected ',' between operands")?;
3113                let b = self.match_value("expected SSA value second operand")?;
3114                self.match_token(Token::Comma, "expected ',' between operands")?;
3115                let uimm128 = self.match_uimm128(I8X16)?;
3116                let imm = ctx.function.dfg.immediates.push(uimm128);
3117                InstructionData::Shuffle {
3118                    opcode,
3119                    imm,
3120                    args: [a, b],
3121                }
3122            }
3123            InstructionFormat::IntCompare => {
3124                let cond = self.match_enum("expected intcc condition code")?;
3125                let lhs = self.match_value("expected SSA value first operand")?;
3126                self.match_token(Token::Comma, "expected ',' between operands")?;
3127                let rhs = self.match_value("expected SSA value second operand")?;
3128                InstructionData::IntCompare {
3129                    opcode,
3130                    cond,
3131                    args: [lhs, rhs],
3132                }
3133            }
3134            InstructionFormat::IntCompareImm => {
3135                let cond = self.match_enum("expected intcc condition code")?;
3136                let lhs = self.match_value("expected SSA value first operand")?;
3137                self.match_token(Token::Comma, "expected ',' between operands")?;
3138                let rhs = self.match_imm64("expected immediate second operand")?;
3139                InstructionData::IntCompareImm {
3140                    opcode,
3141                    cond,
3142                    arg: lhs,
3143                    imm: rhs,
3144                }
3145            }
3146            InstructionFormat::FloatCompare => {
3147                let cond = self.match_enum("expected floatcc condition code")?;
3148                let lhs = self.match_value("expected SSA value first operand")?;
3149                self.match_token(Token::Comma, "expected ',' between operands")?;
3150                let rhs = self.match_value("expected SSA value second operand")?;
3151                InstructionData::FloatCompare {
3152                    opcode,
3153                    cond,
3154                    args: [lhs, rhs],
3155                }
3156            }
3157            InstructionFormat::Call => {
3158                let func_ref = self.match_fn("expected function reference")?;
3159                ctx.check_fn(func_ref, self.loc)?;
3160                self.match_token(Token::LPar, "expected '(' before arguments")?;
3161                let args = self.parse_value_list()?;
3162                self.match_token(Token::RPar, "expected ')' after arguments")?;
3163                InstructionData::Call {
3164                    opcode,
3165                    func_ref,
3166                    args: args.into_value_list(&[], &mut ctx.function.dfg.value_lists),
3167                }
3168            }
3169            InstructionFormat::CallIndirect => {
3170                let sig_ref = self.match_sig("expected signature reference")?;
3171                ctx.check_sig(sig_ref, self.loc)?;
3172                self.match_token(Token::Comma, "expected ',' between operands")?;
3173                let callee = self.match_value("expected SSA value callee operand")?;
3174                self.match_token(Token::LPar, "expected '(' before arguments")?;
3175                let args = self.parse_value_list()?;
3176                self.match_token(Token::RPar, "expected ')' after arguments")?;
3177                InstructionData::CallIndirect {
3178                    opcode,
3179                    sig_ref,
3180                    args: args.into_value_list(&[callee], &mut ctx.function.dfg.value_lists),
3181                }
3182            }
3183            InstructionFormat::TryCall => {
3184                let func_ref = self.match_fn("expected function reference")?;
3185                ctx.check_fn(func_ref, self.loc)?;
3186                self.match_token(Token::LPar, "expected '(' before arguments")?;
3187                let args = self.parse_value_list()?;
3188                self.match_token(Token::RPar, "expected ')' after arguments")?;
3189                self.match_token(Token::Comma, "expected ',' after argument list")?;
3190                let exception = self.parse_exception_table(ctx)?;
3191                InstructionData::TryCall {
3192                    opcode,
3193                    func_ref,
3194                    args: args.into_value_list(&[], &mut ctx.function.dfg.value_lists),
3195                    exception,
3196                }
3197            }
3198            InstructionFormat::TryCallIndirect => {
3199                let callee = self.match_value("expected SSA value callee operand")?;
3200                self.match_token(Token::LPar, "expected '(' before arguments")?;
3201                let args = self.parse_value_list()?;
3202                self.match_token(Token::RPar, "expected ')' after arguments")?;
3203                self.match_token(Token::Comma, "expected ',' after argument list")?;
3204                let exception = self.parse_exception_table(ctx)?;
3205                InstructionData::TryCallIndirect {
3206                    opcode,
3207                    args: args.into_value_list(&[callee], &mut ctx.function.dfg.value_lists),
3208                    exception,
3209                }
3210            }
3211            InstructionFormat::FuncAddr => {
3212                let func_ref = self.match_fn("expected function reference")?;
3213                ctx.check_fn(func_ref, self.loc)?;
3214                InstructionData::FuncAddr { opcode, func_ref }
3215            }
3216            InstructionFormat::StackLoad => {
3217                let ss = self.match_ss("expected stack slot number: ss«n»")?;
3218                ctx.check_ss(ss, self.loc)?;
3219                let offset = self.optional_offset32()?;
3220                InstructionData::StackLoad {
3221                    opcode,
3222                    stack_slot: ss,
3223                    offset,
3224                }
3225            }
3226            InstructionFormat::StackStore => {
3227                let arg = self.match_value("expected SSA value operand")?;
3228                self.match_token(Token::Comma, "expected ',' between operands")?;
3229                let ss = self.match_ss("expected stack slot number: ss«n»")?;
3230                ctx.check_ss(ss, self.loc)?;
3231                let offset = self.optional_offset32()?;
3232                InstructionData::StackStore {
3233                    opcode,
3234                    arg,
3235                    stack_slot: ss,
3236                    offset,
3237                }
3238            }
3239            InstructionFormat::DynamicStackLoad => {
3240                let dss = self.match_dss("expected dynamic stack slot number: dss«n»")?;
3241                ctx.check_dss(dss, self.loc)?;
3242                InstructionData::DynamicStackLoad {
3243                    opcode,
3244                    dynamic_stack_slot: dss,
3245                }
3246            }
3247            InstructionFormat::DynamicStackStore => {
3248                let arg = self.match_value("expected SSA value operand")?;
3249                self.match_token(Token::Comma, "expected ',' between operands")?;
3250                let dss = self.match_dss("expected dynamic stack slot number: dss«n»")?;
3251                ctx.check_dss(dss, self.loc)?;
3252                InstructionData::DynamicStackStore {
3253                    opcode,
3254                    arg,
3255                    dynamic_stack_slot: dss,
3256                }
3257            }
3258            InstructionFormat::Load => {
3259                let flags = self.optional_memflags()?;
3260                let addr = self.match_value("expected SSA value address")?;
3261                let offset = self.optional_offset32()?;
3262                InstructionData::Load {
3263                    opcode,
3264                    flags,
3265                    arg: addr,
3266                    offset,
3267                }
3268            }
3269            InstructionFormat::Store => {
3270                let flags = self.optional_memflags()?;
3271                let arg = self.match_value("expected SSA value operand")?;
3272                self.match_token(Token::Comma, "expected ',' between operands")?;
3273                let addr = self.match_value("expected SSA value address")?;
3274                let offset = self.optional_offset32()?;
3275                InstructionData::Store {
3276                    opcode,
3277                    flags,
3278                    args: [arg, addr],
3279                    offset,
3280                }
3281            }
3282            InstructionFormat::Trap => {
3283                let code = self.match_enum("expected trap code")?;
3284                InstructionData::Trap { opcode, code }
3285            }
3286            InstructionFormat::CondTrap => {
3287                let arg = self.match_value("expected SSA value operand")?;
3288                self.match_token(Token::Comma, "expected ',' between operands")?;
3289                let code = self.match_enum("expected trap code")?;
3290                InstructionData::CondTrap { opcode, arg, code }
3291            }
3292            InstructionFormat::AtomicCas => {
3293                let flags = self.optional_memflags()?;
3294                let addr = self.match_value("expected SSA value address")?;
3295                self.match_token(Token::Comma, "expected ',' between operands")?;
3296                let expected = self.match_value("expected SSA value address")?;
3297                self.match_token(Token::Comma, "expected ',' between operands")?;
3298                let replacement = self.match_value("expected SSA value address")?;
3299                InstructionData::AtomicCas {
3300                    opcode,
3301                    flags,
3302                    args: [addr, expected, replacement],
3303                }
3304            }
3305            InstructionFormat::AtomicRmw => {
3306                let flags = self.optional_memflags()?;
3307                let op = self.match_enum("expected AtomicRmwOp")?;
3308                let addr = self.match_value("expected SSA value address")?;
3309                self.match_token(Token::Comma, "expected ',' between operands")?;
3310                let arg2 = self.match_value("expected SSA value address")?;
3311                InstructionData::AtomicRmw {
3312                    opcode,
3313                    flags,
3314                    op,
3315                    args: [addr, arg2],
3316                }
3317            }
3318            InstructionFormat::LoadNoOffset => {
3319                let flags = self.optional_memflags()?;
3320                let addr = self.match_value("expected SSA value address")?;
3321                InstructionData::LoadNoOffset {
3322                    opcode,
3323                    flags,
3324                    arg: addr,
3325                }
3326            }
3327            InstructionFormat::StoreNoOffset => {
3328                let flags = self.optional_memflags()?;
3329                let arg = self.match_value("expected SSA value operand")?;
3330                self.match_token(Token::Comma, "expected ',' between operands")?;
3331                let addr = self.match_value("expected SSA value address")?;
3332                InstructionData::StoreNoOffset {
3333                    opcode,
3334                    flags,
3335                    args: [arg, addr],
3336                }
3337            }
3338            InstructionFormat::IntAddTrap => {
3339                let a = self.match_value("expected SSA value operand")?;
3340                self.match_token(Token::Comma, "expected ',' between operands")?;
3341                let b = self.match_value("expected SSA value operand")?;
3342                self.match_token(Token::Comma, "expected ',' between operands")?;
3343                let code = self.match_enum("expected trap code")?;
3344                InstructionData::IntAddTrap {
3345                    opcode,
3346                    args: [a, b],
3347                    code,
3348                }
3349            }
3350        };
3351        Ok(idata)
3352    }
3353}
3354
3355#[cfg(test)]
3356mod tests {
3357    use super::*;
3358    use crate::isaspec::IsaSpec;
3359
3360    #[test]
3361    fn argument_type() {
3362        let mut p = Parser::new("i32 sext");
3363        let arg = p.parse_abi_param().unwrap();
3364        assert_eq!(arg.value_type, types::I32);
3365        assert_eq!(arg.extension, ArgumentExtension::Sext);
3366        assert_eq!(arg.purpose, ArgumentPurpose::Normal);
3367        let ParseError {
3368            location,
3369            message,
3370            is_warning,
3371        } = p.parse_abi_param().unwrap_err();
3372        assert_eq!(location.line_number, 1);
3373        assert_eq!(message, "expected parameter type");
3374        assert!(!is_warning);
3375    }
3376
3377    #[test]
3378    fn aliases() {
3379        let (func, details) = Parser::new(
3380            "function %qux() system_v {
3381                                           block0:
3382                                             v4 = iconst.i8 6
3383                                             v3 -> v4
3384                                             v1 = iadd_imm v3, 17
3385                                           }",
3386        )
3387        .parse_function()
3388        .unwrap();
3389        assert_eq!(func.name.to_string(), "%qux");
3390        let v4 = details.map.lookup_str("v4").unwrap();
3391        assert_eq!(v4.to_string(), "v4");
3392        let v3 = details.map.lookup_str("v3").unwrap();
3393        assert_eq!(v3.to_string(), "v3");
3394        match v3 {
3395            AnyEntity::Value(v3) => {
3396                let aliased_to = func.dfg.resolve_aliases(v3);
3397                assert_eq!(aliased_to.to_string(), "v4");
3398            }
3399            _ => panic!("expected value: {v3}"),
3400        }
3401    }
3402
3403    #[test]
3404    fn signature() {
3405        let sig = Parser::new("()system_v").parse_signature().unwrap();
3406        assert_eq!(sig.params.len(), 0);
3407        assert_eq!(sig.returns.len(), 0);
3408        assert_eq!(sig.call_conv, CallConv::SystemV);
3409
3410        let sig2 =
3411            Parser::new("(i8 uext, f16, f32, f64, f128, i32 sret) -> i32 sext, f64 system_v")
3412                .parse_signature()
3413                .unwrap();
3414        assert_eq!(
3415            sig2.to_string(),
3416            "(i8 uext, f16, f32, f64, f128, i32 sret) -> i32 sext, f64 system_v"
3417        );
3418        assert_eq!(sig2.call_conv, CallConv::SystemV);
3419
3420        // Old-style signature without a calling convention.
3421        assert_eq!(
3422            Parser::new("()").parse_signature().unwrap().to_string(),
3423            "() fast"
3424        );
3425        assert_eq!(
3426            Parser::new("() notacc")
3427                .parse_signature()
3428                .unwrap_err()
3429                .to_string(),
3430            "1: unknown calling convention: notacc"
3431        );
3432
3433        // `void` is not recognized as a type by the lexer. It should not appear in files.
3434        assert_eq!(
3435            Parser::new("() -> void")
3436                .parse_signature()
3437                .unwrap_err()
3438                .to_string(),
3439            "1: expected parameter type"
3440        );
3441        assert_eq!(
3442            Parser::new("i8 -> i8")
3443                .parse_signature()
3444                .unwrap_err()
3445                .to_string(),
3446            "1: expected function signature: ( args... )"
3447        );
3448        assert_eq!(
3449            Parser::new("(i8 -> i8")
3450                .parse_signature()
3451                .unwrap_err()
3452                .to_string(),
3453            "1: expected ')' after function arguments"
3454        );
3455    }
3456
3457    #[test]
3458    fn stack_slot_decl() {
3459        let (func, _) = Parser::new(
3460            "function %foo() system_v {
3461                                       ss3 = explicit_slot 13
3462                                       ss1 = explicit_slot 1
3463                                     }",
3464        )
3465        .parse_function()
3466        .unwrap();
3467        assert_eq!(func.name.to_string(), "%foo");
3468        let mut iter = func.sized_stack_slots.keys();
3469        let _ss0 = iter.next().unwrap();
3470        let ss1 = iter.next().unwrap();
3471        assert_eq!(ss1.to_string(), "ss1");
3472        assert_eq!(
3473            func.sized_stack_slots[ss1].kind,
3474            StackSlotKind::ExplicitSlot
3475        );
3476        assert_eq!(func.sized_stack_slots[ss1].size, 1);
3477        let _ss2 = iter.next().unwrap();
3478        let ss3 = iter.next().unwrap();
3479        assert_eq!(ss3.to_string(), "ss3");
3480        assert_eq!(
3481            func.sized_stack_slots[ss3].kind,
3482            StackSlotKind::ExplicitSlot
3483        );
3484        assert_eq!(func.sized_stack_slots[ss3].size, 13);
3485        assert_eq!(iter.next(), None);
3486
3487        // Catch duplicate definitions.
3488        assert_eq!(
3489            Parser::new(
3490                "function %bar() system_v {
3491                                    ss1  = explicit_slot 13
3492                                    ss1  = explicit_slot 1
3493                                }",
3494            )
3495            .parse_function()
3496            .unwrap_err()
3497            .to_string(),
3498            "3: duplicate entity: ss1"
3499        );
3500    }
3501
3502    #[test]
3503    fn block_header() {
3504        let (func, _) = Parser::new(
3505            "function %blocks() system_v {
3506                                     block0:
3507                                     block4(v3: i32):
3508                                     }",
3509        )
3510        .parse_function()
3511        .unwrap();
3512        assert_eq!(func.name.to_string(), "%blocks");
3513
3514        let mut blocks = func.layout.blocks();
3515
3516        let block0 = blocks.next().unwrap();
3517        assert_eq!(func.dfg.block_params(block0), &[]);
3518
3519        let block4 = blocks.next().unwrap();
3520        let block4_args = func.dfg.block_params(block4);
3521        assert_eq!(block4_args.len(), 1);
3522        assert_eq!(func.dfg.value_type(block4_args[0]), types::I32);
3523    }
3524
3525    #[test]
3526    fn duplicate_block() {
3527        let ParseError {
3528            location,
3529            message,
3530            is_warning,
3531        } = Parser::new(
3532            "function %blocks() system_v {
3533                block0:
3534                block0:
3535                    return 2",
3536        )
3537        .parse_function()
3538        .unwrap_err();
3539
3540        assert_eq!(location.line_number, 3);
3541        assert_eq!(message, "duplicate entity: block0");
3542        assert!(!is_warning);
3543    }
3544
3545    #[test]
3546    fn number_of_blocks() {
3547        let ParseError {
3548            location,
3549            message,
3550            is_warning,
3551        } = Parser::new(
3552            "function %a() {
3553                block100000:",
3554        )
3555        .parse_function()
3556        .unwrap_err();
3557
3558        assert_eq!(location.line_number, 2);
3559        assert_eq!(message, "too many blocks");
3560        assert!(!is_warning);
3561    }
3562
3563    #[test]
3564    fn duplicate_ss() {
3565        let ParseError {
3566            location,
3567            message,
3568            is_warning,
3569        } = Parser::new(
3570            "function %blocks() system_v {
3571                ss0 = explicit_slot 8
3572                ss0 = explicit_slot 8",
3573        )
3574        .parse_function()
3575        .unwrap_err();
3576
3577        assert_eq!(location.line_number, 3);
3578        assert_eq!(message, "duplicate entity: ss0");
3579        assert!(!is_warning);
3580    }
3581
3582    #[test]
3583    fn duplicate_gv() {
3584        let ParseError {
3585            location,
3586            message,
3587            is_warning,
3588        } = Parser::new(
3589            "function %blocks() system_v {
3590                gv0 = vmctx
3591                gv0 = vmctx",
3592        )
3593        .parse_function()
3594        .unwrap_err();
3595
3596        assert_eq!(location.line_number, 3);
3597        assert_eq!(message, "duplicate entity: gv0");
3598        assert!(!is_warning);
3599    }
3600
3601    #[test]
3602    fn duplicate_sig() {
3603        let ParseError {
3604            location,
3605            message,
3606            is_warning,
3607        } = Parser::new(
3608            "function %blocks() system_v {
3609                sig0 = ()
3610                sig0 = ()",
3611        )
3612        .parse_function()
3613        .unwrap_err();
3614
3615        assert_eq!(location.line_number, 3);
3616        assert_eq!(message, "duplicate entity: sig0");
3617        assert!(!is_warning);
3618    }
3619
3620    #[test]
3621    fn duplicate_fn() {
3622        let ParseError {
3623            location,
3624            message,
3625            is_warning,
3626        } = Parser::new(
3627            "function %blocks() system_v {
3628                sig0 = ()
3629                fn0 = %foo sig0
3630                fn0 = %foo sig0",
3631        )
3632        .parse_function()
3633        .unwrap_err();
3634
3635        assert_eq!(location.line_number, 4);
3636        assert_eq!(message, "duplicate entity: fn0");
3637        assert!(!is_warning);
3638    }
3639
3640    #[test]
3641    fn comments() {
3642        let (func, Details { comments, .. }) = Parser::new(
3643            "; before
3644                         function %comment() system_v { ; decl
3645                            ss10  = explicit_slot 13 ; stackslot.
3646                            ; Still stackslot.
3647                         block0: ; Basic block
3648                         trap user42; Instruction
3649                         } ; Trailing.
3650                         ; More trailing.",
3651        )
3652        .parse_function()
3653        .unwrap();
3654        assert_eq!(func.name.to_string(), "%comment");
3655        assert_eq!(comments.len(), 7); // no 'before' comment.
3656        assert_eq!(
3657            comments[0],
3658            Comment {
3659                entity: AnyEntity::Function,
3660                text: "; decl",
3661            }
3662        );
3663        assert_eq!(comments[1].entity.to_string(), "ss10");
3664        assert_eq!(comments[2].entity.to_string(), "ss10");
3665        assert_eq!(comments[2].text, "; Still stackslot.");
3666        assert_eq!(comments[3].entity.to_string(), "block0");
3667        assert_eq!(comments[3].text, "; Basic block");
3668
3669        assert_eq!(comments[4].entity.to_string(), "inst0");
3670        assert_eq!(comments[4].text, "; Instruction");
3671
3672        assert_eq!(comments[5].entity, AnyEntity::Function);
3673        assert_eq!(comments[6].entity, AnyEntity::Function);
3674    }
3675
3676    #[test]
3677    fn test_file() {
3678        let tf = parse_test(
3679            r#"; before
3680                             test cfg option=5
3681                             test verify
3682                             set enable_float=false
3683                             feature "foo"
3684                             feature !"bar"
3685                             ; still preamble
3686                             function %comment() system_v {}"#,
3687            ParseOptions::default(),
3688        )
3689        .unwrap();
3690        assert_eq!(tf.commands.len(), 2);
3691        assert_eq!(tf.commands[0].command, "cfg");
3692        assert_eq!(tf.commands[1].command, "verify");
3693        match tf.isa_spec {
3694            IsaSpec::None(s) => {
3695                assert!(s.enable_verifier());
3696                assert!(!s.enable_float());
3697            }
3698            _ => panic!("unexpected ISAs"),
3699        }
3700        assert_eq!(tf.features[0], Feature::With(&"foo"));
3701        assert_eq!(tf.features[1], Feature::Without(&"bar"));
3702        assert_eq!(tf.preamble_comments.len(), 2);
3703        assert_eq!(tf.preamble_comments[0].text, "; before");
3704        assert_eq!(tf.preamble_comments[1].text, "; still preamble");
3705        assert_eq!(tf.functions.len(), 1);
3706        assert_eq!(tf.functions[0].0.name.to_string(), "%comment");
3707    }
3708
3709    #[test]
3710    fn isa_spec() {
3711        assert!(
3712            parse_test(
3713                "target
3714                            function %foo() system_v {}",
3715                ParseOptions::default()
3716            )
3717            .is_err()
3718        );
3719
3720        assert!(
3721            parse_test(
3722                "target x86_64
3723                            set enable_float=false
3724                            function %foo() system_v {}",
3725                ParseOptions::default()
3726            )
3727            .is_err()
3728        );
3729
3730        match parse_test(
3731            "set enable_float=false
3732                          target x86_64
3733                          function %foo() system_v {}",
3734            ParseOptions::default(),
3735        )
3736        .unwrap()
3737        .isa_spec
3738        {
3739            IsaSpec::None(_) => panic!("Expected some ISA"),
3740            IsaSpec::Some(v) => {
3741                assert_eq!(v.len(), 1);
3742                assert!(v[0].name() == "x64" || v[0].name() == "x86");
3743            }
3744        }
3745    }
3746
3747    #[test]
3748    fn user_function_name() {
3749        // Valid characters in the name:
3750        let func = Parser::new(
3751            "function u1:2() system_v {
3752                                           block0:
3753                                             trap int_divz
3754                                           }",
3755        )
3756        .parse_function()
3757        .unwrap()
3758        .0;
3759        assert_eq!(func.name.to_string(), "u1:2");
3760
3761        // Invalid characters in the name:
3762        let mut parser = Parser::new(
3763            "function u123:abc() system_v {
3764                                           block0:
3765                                             trap stk_ovf
3766                                           }",
3767        );
3768        assert!(parser.parse_function().is_err());
3769
3770        // Incomplete function names should not be valid:
3771        let mut parser = Parser::new(
3772            "function u() system_v {
3773                                           block0:
3774                                             trap int_ovf
3775                                           }",
3776        );
3777        assert!(parser.parse_function().is_err());
3778
3779        let mut parser = Parser::new(
3780            "function u0() system_v {
3781                                           block0:
3782                                             trap int_ovf
3783                                           }",
3784        );
3785        assert!(parser.parse_function().is_err());
3786
3787        let mut parser = Parser::new(
3788            "function u0:() system_v {
3789                                           block0:
3790                                             trap int_ovf
3791                                           }",
3792        );
3793        assert!(parser.parse_function().is_err());
3794    }
3795
3796    #[test]
3797    fn change_default_calling_convention() {
3798        let code = "function %test() {
3799        block0:
3800            return
3801        }";
3802
3803        // By default the parser will use the fast calling convention if none is specified.
3804        let mut parser = Parser::new(code);
3805        assert_eq!(
3806            parser.parse_function().unwrap().0.signature.call_conv,
3807            CallConv::Fast
3808        );
3809
3810        // However, we can specify a different calling convention to be the default.
3811        let mut parser = Parser::new(code).with_default_calling_convention(CallConv::Cold);
3812        assert_eq!(
3813            parser.parse_function().unwrap().0.signature.call_conv,
3814            CallConv::Cold
3815        );
3816    }
3817
3818    #[test]
3819    fn u8_as_hex() {
3820        fn parse_as_uimm8(text: &str) -> ParseResult<u8> {
3821            Parser::new(text).match_uimm8("unable to parse u8")
3822        }
3823
3824        assert_eq!(parse_as_uimm8("0").unwrap(), 0);
3825        assert_eq!(parse_as_uimm8("0xff").unwrap(), 255);
3826        assert!(parse_as_uimm8("-1").is_err());
3827        assert!(parse_as_uimm8("0xffa").is_err());
3828    }
3829
3830    #[test]
3831    fn i16_as_hex() {
3832        fn parse_as_imm16(text: &str) -> ParseResult<i16> {
3833            Parser::new(text).match_imm16("unable to parse i16")
3834        }
3835
3836        assert_eq!(parse_as_imm16("0x8000").unwrap(), -32768);
3837        assert_eq!(parse_as_imm16("0xffff").unwrap(), -1);
3838        assert_eq!(parse_as_imm16("0").unwrap(), 0);
3839        assert_eq!(parse_as_imm16("0x7fff").unwrap(), 32767);
3840        assert_eq!(
3841            parse_as_imm16("-0x0001").unwrap(),
3842            parse_as_imm16("0xffff").unwrap()
3843        );
3844        assert_eq!(
3845            parse_as_imm16("-0x7fff").unwrap(),
3846            parse_as_imm16("0x8001").unwrap()
3847        );
3848        assert!(parse_as_imm16("0xffffa").is_err());
3849    }
3850
3851    #[test]
3852    fn i32_as_hex() {
3853        fn parse_as_imm32(text: &str) -> ParseResult<i32> {
3854            Parser::new(text).match_imm32("unable to parse i32")
3855        }
3856
3857        assert_eq!(parse_as_imm32("0x80000000").unwrap(), -2147483648);
3858        assert_eq!(parse_as_imm32("0xffffffff").unwrap(), -1);
3859        assert_eq!(parse_as_imm32("0").unwrap(), 0);
3860        assert_eq!(parse_as_imm32("0x7fffffff").unwrap(), 2147483647);
3861        assert_eq!(
3862            parse_as_imm32("-0x00000001").unwrap(),
3863            parse_as_imm32("0xffffffff").unwrap()
3864        );
3865        assert_eq!(
3866            parse_as_imm32("-0x7fffffff").unwrap(),
3867            parse_as_imm32("0x80000001").unwrap()
3868        );
3869        assert!(parse_as_imm32("0xffffffffa").is_err());
3870    }
3871
3872    #[test]
3873    fn i64_as_hex() {
3874        fn parse_as_imm64(text: &str) -> ParseResult<Imm64> {
3875            Parser::new(text).match_imm64("unable to parse Imm64")
3876        }
3877
3878        assert_eq!(
3879            parse_as_imm64("0x8000000000000000").unwrap(),
3880            Imm64::new(-9223372036854775808)
3881        );
3882        assert_eq!(
3883            parse_as_imm64("0xffffffffffffffff").unwrap(),
3884            Imm64::new(-1)
3885        );
3886        assert_eq!(parse_as_imm64("0").unwrap(), Imm64::new(0));
3887        assert_eq!(
3888            parse_as_imm64("0x7fffffffffffffff").unwrap(),
3889            Imm64::new(9223372036854775807)
3890        );
3891        assert_eq!(
3892            parse_as_imm64("-0x0000000000000001").unwrap(),
3893            parse_as_imm64("0xffffffffffffffff").unwrap()
3894        );
3895        assert_eq!(
3896            parse_as_imm64("-0x7fffffffffffffff").unwrap(),
3897            parse_as_imm64("0x8000000000000001").unwrap()
3898        );
3899        assert!(parse_as_imm64("0xffffffffffffffffa").is_err());
3900    }
3901
3902    #[test]
3903    fn uimm128() {
3904        macro_rules! parse_as_constant_data {
3905            ($text:expr, $type:expr) => {{ Parser::new($text).parse_literals_to_constant_data($type) }};
3906        }
3907        macro_rules! can_parse_as_constant_data {
3908            ($text:expr, $type:expr) => {{ assert!(parse_as_constant_data!($text, $type).is_ok()) }};
3909        }
3910        macro_rules! cannot_parse_as_constant_data {
3911            ($text:expr, $type:expr) => {{ assert!(parse_as_constant_data!($text, $type).is_err()) }};
3912        }
3913
3914        can_parse_as_constant_data!("1 2 3 4", I32X4);
3915        can_parse_as_constant_data!("1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16", I8X16);
3916        can_parse_as_constant_data!("0x1.1 0x2.2 0x3.3 0x4.4", F32X4);
3917        can_parse_as_constant_data!("0x0 0x1 0x2 0x3", I32X4);
3918        can_parse_as_constant_data!("-1 0 -1 0 -1 0 -1 0", I16X8);
3919        can_parse_as_constant_data!("0 -1", I64X2);
3920        can_parse_as_constant_data!("-1 0", I64X2);
3921        can_parse_as_constant_data!("-1 -1 -1 -1 -1", I32X4); // note that parse_literals_to_constant_data will leave extra tokens unconsumed
3922
3923        cannot_parse_as_constant_data!("1 2 3", I32X4);
3924        cannot_parse_as_constant_data!(" ", F32X4);
3925    }
3926
3927    #[test]
3928    fn parse_constant_from_booleans() {
3929        let c = Parser::new("-1 0 -1 0")
3930            .parse_literals_to_constant_data(I32X4)
3931            .unwrap();
3932        assert_eq!(
3933            c.into_vec(),
3934            [
3935                0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0
3936            ]
3937        )
3938    }
3939
3940    #[test]
3941    fn parse_unbounded_constants() {
3942        // Unlike match_uimm128, match_hexadecimal_constant can parse byte sequences of any size:
3943        assert_eq!(
3944            Parser::new("0x0100")
3945                .match_hexadecimal_constant("err message")
3946                .unwrap(),
3947            vec![0, 1].into()
3948        );
3949
3950        // Only parse hexadecimal constants:
3951        assert!(
3952            Parser::new("228")
3953                .match_hexadecimal_constant("err message")
3954                .is_err()
3955        );
3956    }
3957
3958    #[test]
3959    fn parse_run_commands() {
3960        // Helper for creating signatures.
3961        fn sig(ins: &[Type], outs: &[Type]) -> Signature {
3962            let mut sig = Signature::new(CallConv::Fast);
3963            for i in ins {
3964                sig.params.push(AbiParam::new(*i));
3965            }
3966            for o in outs {
3967                sig.returns.push(AbiParam::new(*o));
3968            }
3969            sig
3970        }
3971
3972        // Helper for parsing run commands.
3973        fn parse(text: &str, sig: &Signature) -> ParseResult<RunCommand> {
3974            Parser::new(text).parse_run_command(sig)
3975        }
3976
3977        // Check that we can parse and display the same set of run commands.
3978        fn assert_roundtrip(text: &str, sig: &Signature) {
3979            assert_eq!(parse(text, sig).unwrap().to_string(), text);
3980        }
3981        assert_roundtrip("run: %fn0() == 42", &sig(&[], &[I32]));
3982        assert_roundtrip(
3983            "run: %fn0(8, 16, 32, 64) == 1",
3984            &sig(&[I8, I16, I32, I64], &[I8]),
3985        );
3986        assert_roundtrip(
3987            "run: %my_func(1) == 0x0f0e0d0c0b0a09080706050403020100",
3988            &sig(&[I32], &[I8X16]),
3989        );
3990
3991        // Verify that default invocations are created when not specified.
3992        assert_eq!(
3993            parse("run", &sig(&[], &[I32])).unwrap().to_string(),
3994            "run: %default() != 0"
3995        );
3996        assert_eq!(
3997            parse("print", &sig(&[], &[F32X4, I16X8]))
3998                .unwrap()
3999                .to_string(),
4000            "print: %default()"
4001        );
4002
4003        // Demonstrate some unparsable cases.
4004        assert!(parse("print", &sig(&[I32], &[I32])).is_err());
4005        assert!(parse("print:", &sig(&[], &[])).is_err());
4006        assert!(parse("run: ", &sig(&[], &[])).is_err());
4007    }
4008
4009    #[test]
4010    fn parse_data_values() {
4011        fn parse(text: &str, ty: Type) -> DataValue {
4012            Parser::new(text).parse_data_value(ty).unwrap()
4013        }
4014
4015        assert_eq!(parse("8", I8).to_string(), "8");
4016        assert_eq!(parse("16", I16).to_string(), "16");
4017        assert_eq!(parse("32", I32).to_string(), "32");
4018        assert_eq!(parse("64", I64).to_string(), "64");
4019        assert_eq!(
4020            parse("0x01234567_01234567_01234567_01234567", I128).to_string(),
4021            "1512366032949150931280199141537564007"
4022        );
4023        assert_eq!(parse("1234567", I128).to_string(), "1234567");
4024        assert_eq!(parse("0x16.1", F16).to_string(), "0x1.610p4");
4025        assert_eq!(parse("0x32.32", F32).to_string(), "0x1.919000p5");
4026        assert_eq!(parse("0x64.64", F64).to_string(), "0x1.9190000000000p6");
4027        assert_eq!(
4028            parse("0x128.128", F128).to_string(),
4029            "0x1.2812800000000000000000000000p8"
4030        );
4031        assert_eq!(
4032            parse("[0 1 2 3]", I32X4).to_string(),
4033            "0x00000003000000020000000100000000"
4034        );
4035        assert_eq!(parse("[1 2]", I32X2).to_string(), "0x0000000200000001");
4036        assert_eq!(parse("[1 2 3 4]", I8X4).to_string(), "0x04030201");
4037        assert_eq!(parse("[1 2]", I8X2).to_string(), "0x0201");
4038    }
4039
4040    #[test]
4041    fn parse_cold_blocks() {
4042        let code = "function %test() {
4043        block0 cold:
4044            return
4045        block1(v0: i32) cold:
4046            return
4047        block2(v1: i32):
4048            return
4049        }";
4050
4051        let mut parser = Parser::new(code);
4052        let func = parser.parse_function().unwrap().0;
4053        assert_eq!(func.layout.blocks().count(), 3);
4054        assert!(func.layout.is_cold(Block::from_u32(0)));
4055        assert!(func.layout.is_cold(Block::from_u32(1)));
4056        assert!(!func.layout.is_cold(Block::from_u32(2)));
4057    }
4058}