forked from bytecodealliance/wasm-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.rs
695 lines (625 loc) · 22 KB
/
token.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
//! Common tokens that implement the [`Parse`] trait which are otherwise not
//! associated specifically with the wasm text format per se (useful in other
//! contexts too perhaps).
use crate::annotation;
use crate::lexer::FloatVal;
use crate::parser::{Cursor, Parse, Parser, Peek, Result};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::str;
/// A position in the original source stream, used to render errors.
#[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct Span {
pub(crate) offset: usize,
}
impl Span {
/// Construct a `Span` from a byte offset in the source file.
pub fn from_offset(offset: usize) -> Self {
Span { offset }
}
/// Returns the line/column information of this span within `text`.
/// Line and column numbers are 0-indexed. User presentation is typically
/// 1-indexed, but 0-indexing is appropriate for internal use with
/// iterators and slices.
pub fn linecol_in(&self, text: &str) -> (usize, usize) {
let mut cur = 0;
// Use split_terminator instead of lines so that if there is a `\r`,
// it is included in the offset calculation. The `+1` values below
// account for the `\n`.
for (i, line) in text.split_terminator('\n').enumerate() {
if cur + line.len() + 1 > self.offset {
return (i, self.offset - cur);
}
cur += line.len() + 1;
}
(text.lines().count(), 0)
}
/// Returns the byte offset of this span.
pub fn offset(&self) -> usize {
self.offset
}
}
/// An identifier in a WebAssembly module, prefixed by `$` in the textual
/// format.
///
/// An identifier is used to symbolically refer to items in a a wasm module,
/// typically via the [`Index`] type.
#[derive(Copy, Clone)]
pub struct Id<'a> {
name: &'a str,
gen: u32,
span: Span,
}
impl<'a> Id<'a> {
fn new(name: &'a str, span: Span) -> Id<'a> {
Id { name, gen: 0, span }
}
pub(crate) fn gensym(span: Span, gen: u32) -> Id<'a> {
Id {
name: "gensym",
gen,
span,
}
}
/// Returns the underlying name of this identifier.
///
/// The name returned does not contain the leading `$`.
pub fn name(&self) -> &'a str {
self.name
}
/// Returns span of this identifier in the original source
pub fn span(&self) -> Span {
self.span
}
pub(crate) fn is_gensym(&self) -> bool {
self.gen != 0
}
}
impl<'a> Hash for Id<'a> {
fn hash<H: Hasher>(&self, hasher: &mut H) {
self.name.hash(hasher);
self.gen.hash(hasher);
}
}
impl<'a> PartialEq for Id<'a> {
fn eq(&self, other: &Id<'a>) -> bool {
self.name == other.name && self.gen == other.gen
}
}
impl<'a> Eq for Id<'a> {}
impl<'a> Parse<'a> for Id<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
parser.step(|c| {
if let Some((name, rest)) = c.id() {
return Ok((Id::new(name, c.cur_span()), rest));
}
Err(c.error("expected an identifier"))
})
}
}
impl fmt::Debug for Id<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.gen != 0 {
f.debug_struct("Id").field("gen", &self.gen).finish()
} else {
self.name.fmt(f)
}
}
}
impl Peek for Id<'_> {
fn peek(cursor: Cursor<'_>) -> bool {
cursor.id().is_some()
}
fn display() -> &'static str {
"an identifier"
}
}
/// A reference to another item in a wasm module.
///
/// This type is used for items referring to other items (such as `call $foo`
/// referencing function `$foo`). References can be either an index (u32) or an
/// [`Id`] in the textual format.
///
/// The emission phase of a module will ensure that `Index::Id` is never used
/// and switch them all to `Index::Num`.
#[derive(Copy, Clone, Debug)]
pub enum Index<'a> {
/// A numerical index that this references. The index space this is
/// referencing is implicit based on where this [`Index`] is stored.
Num(u32, Span),
/// A human-readable identifier this references. Like `Num`, the namespace
/// this references is based on where this is stored.
Id(Id<'a>),
}
impl Index<'_> {
/// Returns the source location where this `Index` was defined.
pub fn span(&self) -> Span {
match self {
Index::Num(_, span) => *span,
Index::Id(id) => id.span(),
}
}
pub(crate) fn is_resolved(&self) -> bool {
matches!(self, Index::Num(..))
}
}
impl<'a> Parse<'a> for Index<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
let mut l = parser.lookahead1();
if l.peek::<Id>() {
Ok(Index::Id(parser.parse()?))
} else if l.peek::<u32>() {
let (val, span) = parser.parse()?;
Ok(Index::Num(val, span))
} else {
Err(l.error())
}
}
}
impl Peek for Index<'_> {
fn peek(cursor: Cursor<'_>) -> bool {
u32::peek(cursor) || Id::peek(cursor)
}
fn display() -> &'static str {
"an index"
}
}
impl<'a> From<Id<'a>> for Index<'a> {
fn from(id: Id<'a>) -> Index<'a> {
Index::Id(id)
}
}
impl PartialEq for Index<'_> {
fn eq(&self, other: &Index<'_>) -> bool {
match (self, other) {
(Index::Num(a, _), Index::Num(b, _)) => a == b,
(Index::Id(a), Index::Id(b)) => a == b,
_ => false,
}
}
}
impl Eq for Index<'_> {}
impl Hash for Index<'_> {
fn hash<H: Hasher>(&self, hasher: &mut H) {
match self {
Index::Num(a, _) => {
0u8.hash(hasher);
a.hash(hasher);
}
Index::Id(a) => {
1u8.hash(hasher);
a.hash(hasher);
}
}
}
}
/// Parses `(func $foo)`
#[derive(Clone, Debug)]
#[allow(missing_docs)]
pub struct ItemRef<'a, K> {
pub kind: K,
pub idx: Index<'a>,
}
impl<'a, K: Parse<'a>> Parse<'a> for ItemRef<'a, K> {
fn parse(parser: Parser<'a>) -> Result<Self> {
parser.parens(|parser| {
let kind = parser.parse::<K>()?;
let idx = parser.parse()?;
Ok(ItemRef { kind, idx })
})
}
}
impl<'a, K: Peek> Peek for ItemRef<'a, K> {
fn peek(cursor: Cursor<'_>) -> bool {
match cursor.lparen() {
Some(remaining) => K::peek(remaining),
None => false,
}
}
fn display() -> &'static str {
"an item reference"
}
}
/// An `@name` annotation in source, currently of the form `@name "foo"`
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct NameAnnotation<'a> {
/// The name specified for the item
pub name: &'a str,
}
impl<'a> Parse<'a> for NameAnnotation<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
parser.parse::<annotation::name>()?;
let name = parser.parse()?;
Ok(NameAnnotation { name })
}
}
impl<'a> Parse<'a> for Option<NameAnnotation<'a>> {
fn parse(parser: Parser<'a>) -> Result<Self> {
let _r = parser.register_annotation("name");
Ok(if parser.peek2::<annotation::name>() {
Some(parser.parens(|p| p.parse())?)
} else {
None
})
}
}
macro_rules! integers {
($($i:ident($u:ident))*) => ($(
impl<'a> Parse<'a> for $i {
fn parse(parser: Parser<'a>) -> Result<Self> {
Ok(parser.parse::<($i, Span)>()?.0)
}
}
impl<'a> Parse<'a> for ($i, Span) {
fn parse(parser: Parser<'a>) -> Result<Self> {
parser.step(|c| {
if let Some((i, rest)) = c.integer() {
let (s, base) = i.val();
let val = $i::from_str_radix(s, base)
.or_else(|_| {
$u::from_str_radix(s, base).map(|i| i as $i)
});
return match val {
Ok(n) => Ok(((n, c.cur_span()), rest)),
Err(_) => Err(c.error(concat!(
"invalid ",
stringify!($i),
" number: constant out of range",
))),
};
}
Err(c.error(concat!("expected a ", stringify!($i))))
})
}
}
impl Peek for $i {
fn peek(cursor: Cursor<'_>) -> bool {
cursor.integer().is_some()
}
fn display() -> &'static str {
stringify!($i)
}
}
)*)
}
integers! {
u8(u8) u16(u16) u32(u32) u64(u64)
i8(u8) i16(u16) i32(u32) i64(u64)
}
impl<'a> Parse<'a> for &'a [u8] {
fn parse(parser: Parser<'a>) -> Result<Self> {
parser.step(|c| {
if let Some((i, rest)) = c.string() {
return Ok((i, rest));
}
Err(c.error("expected a string"))
})
}
}
impl Peek for &'_ [u8] {
fn peek(cursor: Cursor<'_>) -> bool {
cursor.string().is_some()
}
fn display() -> &'static str {
"string"
}
}
impl<'a> Parse<'a> for &'a str {
fn parse(parser: Parser<'a>) -> Result<Self> {
str::from_utf8(parser.parse()?)
.map_err(|_| parser.error_at(parser.prev_span(), "malformed UTF-8 encoding"))
}
}
impl Parse<'_> for String {
fn parse(parser: Parser<'_>) -> Result<Self> {
Ok(<&str>::parse(parser)?.to_string())
}
}
impl Peek for &'_ str {
fn peek(cursor: Cursor<'_>) -> bool {
<&[u8]>::peek(cursor)
}
fn display() -> &'static str {
<&[u8]>::display()
}
}
macro_rules! float {
($($name:ident => {
bits: $int:ident,
float: $float:ident,
exponent_bits: $exp_bits:tt,
name: $parse:ident,
})*) => ($(
/// A parsed floating-point type
#[derive(Debug, Copy, Clone)]
pub struct $name {
/// The raw bits that this floating point number represents.
pub bits: $int,
}
impl<'a> Parse<'a> for $name {
fn parse(parser: Parser<'a>) -> Result<Self> {
parser.step(|c| {
let (val, rest) = if let Some((f, rest)) = c.float() {
($parse(f.val()), rest)
} else if let Some((i, rest)) = c.integer() {
let (s, base) = i.val();
(
$parse(&FloatVal::Val {
hex: base == 16,
integral: s.into(),
decimal: None,
exponent: None,
}),
rest,
)
} else {
return Err(c.error("expected a float"));
};
match val {
Some(bits) => Ok(($name { bits }, rest)),
None => Err(c.error("invalid float value: constant out of range")),
}
})
}
}
fn $parse(val: &FloatVal<'_>) -> Option<$int> {
// Compute a few well-known constants about the float representation
// given the parameters to the macro here.
let width = std::mem::size_of::<$int>() * 8;
let neg_offset = width - 1;
let exp_offset = neg_offset - $exp_bits;
let signif_bits = width - 1 - $exp_bits;
let signif_mask = (1 << exp_offset) - 1;
let bias = (1 << ($exp_bits - 1)) - 1;
let (hex, integral, decimal, exponent_str) = match val {
// Infinity is when the exponent bits are all set and
// the significand is zero.
FloatVal::Inf { negative } => {
let exp_bits = (1 << $exp_bits) - 1;
let neg_bit = *negative as $int;
return Some(
(neg_bit << neg_offset) |
(exp_bits << exp_offset)
);
}
// NaN is when the exponent bits are all set and
// the significand is nonzero. The default of NaN is
// when only the highest bit of the significand is set.
FloatVal::Nan { negative, val } => {
let exp_bits = (1 << $exp_bits) - 1;
let neg_bit = *negative as $int;
let signif = val.unwrap_or(1 << (signif_bits - 1)) as $int;
// If the significand is zero then this is actually infinity
// so we fail to parse it.
if signif & signif_mask == 0 {
return None;
}
return Some(
(neg_bit << neg_offset) |
(exp_bits << exp_offset) |
(signif & signif_mask)
);
}
// This is trickier, handle this below
FloatVal::Val { hex, integral, decimal, exponent } => {
(hex, integral, decimal, exponent)
}
};
// Rely on Rust's standard library to parse base 10 floats
// correctly.
if !*hex {
let mut s = integral.to_string();
if let Some(decimal) = decimal {
s.push_str(".");
s.push_str(&decimal);
}
if let Some(exponent) = exponent_str {
s.push_str("e");
s.push_str(&exponent);
}
let float = s.parse::<$float>().ok()?;
// looks like the `*.wat` format considers infinite overflow to
// be invalid.
if float.is_infinite() {
return None;
}
return Some(float.to_bits());
}
// Parsing hex floats is... hard! I don't really know what most of
// this below does. It was copied from Gecko's implementation in
// `WasmTextToBinary.cpp`. Would love comments on this if you have
// them!
let decimal = decimal.as_ref().map(|s| &**s).unwrap_or("");
let negative = integral.starts_with('-');
let integral = integral.trim_start_matches('-').trim_start_matches('0');
// Do a bunch of work up front to locate the first non-zero digit
// to determine the initial exponent. There's a number of
// adjustments depending on where the digit was found, but the
// general idea here is that I'm not really sure why things are
// calculated the way they are but it should match Gecko.
let decimal_no_leading = decimal.trim_start_matches('0');
let decimal_iter = if integral.is_empty() {
decimal_no_leading.chars()
} else {
decimal.chars()
};
let mut digits = integral.chars()
.map(|c| (to_hex(c) as $int, false))
.chain(decimal_iter.map(|c| (to_hex(c) as $int, true)));
let lead_nonzero_digit = match digits.next() {
Some((c, _)) => c,
// No digits? Must be `+0` or `-0`, being careful to handle the
// sign encoding here.
None if negative => return Some(1 << (width - 1)),
None => return Some(0),
};
let mut significand = 0 as $int;
let mut exponent = if !integral.is_empty() {
1
} else {
-((decimal.len() - decimal_no_leading.len() + 1) as i32) + 1
};
let lz = (lead_nonzero_digit as u8).leading_zeros() as i32 - 4;
exponent = exponent.checked_mul(4)?.checked_sub(lz + 1)?;
let mut significand_pos = (width - (4 - (lz as usize))) as isize;
assert!(significand_pos >= 0);
significand |= lead_nonzero_digit << significand_pos;
// Now that we've got an anchor in the string we parse the remaining
// digits. Again, not entirely sure why everything is the way it is
// here! This is copied frmo gecko.
let mut discarded_extra_nonzero = false;
for (digit, decimal) in digits {
if !decimal {
exponent += 4;
}
if significand_pos > -4 {
significand_pos -= 4;
}
if significand_pos >= 0 {
significand |= digit << significand_pos;
} else if significand_pos > -4 {
significand |= digit >> (4 - significand_pos);
discarded_extra_nonzero = (digit & !((!0) >> (4 - significand_pos))) != 0;
} else if digit != 0 {
discarded_extra_nonzero = true;
}
}
exponent = exponent.checked_add(match exponent_str {
Some(s) => s.parse::<i32>().ok()?,
None => 0,
})?;
debug_assert!(significand != 0);
let (encoded_exponent, encoded_significand, discarded_significand) =
if exponent <= -bias {
// Underflow to subnormal or zero.
let shift = exp_offset as i32 + exponent + bias;
if shift == 0 {
(0, 0, significand)
} else if shift < 0 || shift >= width as i32 {
(0, 0, 0)
} else {
(
0,
significand >> (width as i32 - shift),
significand << shift,
)
}
} else if exponent <= bias {
// Normal (non-zero). The significand's leading 1 is encoded
// implicitly.
(
((exponent + bias) as $int) << exp_offset,
(significand >> (width - exp_offset - 1)) & signif_mask,
significand << (exp_offset + 1),
)
} else {
// Overflow to infinity.
(
((1 << $exp_bits) - 1) << exp_offset,
0,
0,
)
};
let bits = encoded_exponent | encoded_significand;
// Apply rounding. If this overflows the significand, it carries
// into the exponent bit according to the magic of the IEEE 754
// encoding.
//
// Or rather, the comment above is what Gecko says so it's copied
// here too.
let msb = 1 << (width - 1);
let bits = bits
+ (((discarded_significand & msb != 0)
&& ((discarded_significand & !msb != 0) ||
discarded_extra_nonzero ||
// ties to even
(encoded_significand & 1 != 0))) as $int);
// Just before we return the bits be sure to handle the sign bit we
// found at the beginning.
let bits = if negative {
bits | (1 << (width - 1))
} else {
bits
};
// looks like the `*.wat` format considers infinite overflow to
// be invalid.
if $float::from_bits(bits).is_infinite() {
return None;
}
Some(bits)
}
)*)
}
float! {
Float32 => {
bits: u32,
float: f32,
exponent_bits: 8,
name: strtof,
}
Float64 => {
bits: u64,
float: f64,
exponent_bits: 11,
name: strtod,
}
}
fn to_hex(c: char) -> u8 {
match c {
'a'..='f' => c as u8 - b'a' + 10,
'A'..='F' => c as u8 - b'A' + 10,
_ => c as u8 - b'0',
}
}
/// A convenience type to use with [`Parser::peek`](crate::parser::Parser::peek)
/// to see if the next token is an s-expression.
pub struct LParen {
_priv: (),
}
impl Peek for LParen {
fn peek(cursor: Cursor<'_>) -> bool {
cursor.lparen().is_some()
}
fn display() -> &'static str {
"left paren"
}
}
#[cfg(test)]
mod tests {
#[test]
fn hex_strtof() {
macro_rules! f {
($a:tt) => (f!(@mk $a, None, None));
($a:tt p $e:tt) => (f!(@mk $a, None, Some($e.into())));
($a:tt . $b:tt) => (f!(@mk $a, Some($b.into()), None));
($a:tt . $b:tt p $e:tt) => (f!(@mk $a, Some($b.into()), Some($e.into())));
(@mk $a:tt, $b:expr, $e:expr) => (crate::lexer::FloatVal::Val {
hex: true,
integral: $a.into(),
decimal: $b,
exponent: $e
});
}
assert_eq!(super::strtof(&f!("0")), Some(0));
assert_eq!(super::strtof(&f!("0" . "0")), Some(0));
assert_eq!(super::strtof(&f!("0" . "0" p "2354")), Some(0));
assert_eq!(super::strtof(&f!("-0")), Some(1 << 31));
assert_eq!(super::strtof(&f!("f32")), Some(0x45732000));
assert_eq!(super::strtof(&f!("0" . "f32")), Some(0x3f732000));
assert_eq!(super::strtof(&f!("1" . "2")), Some(0x3f900000));
assert_eq!(
super::strtof(&f!("0" . "00000100000000000" p "-126")),
Some(0)
);
assert_eq!(
super::strtof(&f!("1" . "fffff4" p "-106")),
Some(0x0afffffa)
);
assert_eq!(super::strtof(&f!("fffff98" p "-133")), Some(0x0afffffa));
assert_eq!(super::strtof(&f!("0" . "081" p "023")), Some(0x48810000));
assert_eq!(
super::strtof(&f!("1" . "00000100000000000" p "-50")),
Some(0x26800000)
);
}
}