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 696 697 698 699 700 701 702
//! # Accompanying procedural macros to `awint`
//!
//! ## Scope Dependencies
//!
//! All of the macros often require `Bits` and `InlAwi` to be in scope. This
//! could be from `awint_core` or be a reexport from `awint`. `extawi!` and
//! similar always requires `ExtAwi` to be in scope, which could be imported
//! from `awint_ext` or be a reexport from `awint`. `cc!` may require none, one,
//! or both storage types depending on the input. Macros with fallible inputs
//! require `Option<T>` variants to be in scope.
//!
//! ## Inputs
//!
//! Macros accepting only `usize` literals: `inlawi_ty`
//!
//! Macros accepting only concatenations of components: `inlawi`, `extawi`
//!
//! Macros accepting both: `inlawi_zero`, `inlawi_umax`, `inlawi_imax`,
//! `inlawi_imin`, `inlawi_uone`, `extawi_zero`, `extawi_umax`, `extawi_imax`,
//! `extawi_imin`, `extawi_uone`
//!
//! The macros that accept both work by first trying to parse the whole input as
//! a `usize` literal, then if the parsing fails attempt to interpret the input
//! as concatenations of components.
//!
//! ## Concatenations of Components
//!
//! Some of the macros accept "concatenations of components". A component can be
//! a literal, a variable, or a filler. Components are written in a big endian
//! order (like literals), and concatenations are written in a little endian
//! order (because large concatentations will usually be formatted on different
//! lines, and we want the data flow to be downwards), so the general layout of
//! the input to a macro is:
//!
//! ```text
//! macro!(
//! ..., component 2, component 1, component 0; // concatenation 0
//! ..., component 2, component 1, component 0; // concatenation 1
//! ..., component 2, component 1, component 0; // concatenation 2
//! ⋮
//! )
//! ```
//!
//! The first concatenation, or concatenation 0, is the "source" concatenation,
//! and the following concatenations are "sink" concatenations. Either
//! statically at compile time or dynamically at run time, the macro will check
//! if the concatenations all have the same bitwidths. If so, the corresponding
//! bits from the source get copied to the corresponding bits of the sinks. The
//! construction macros additionally return the source concatenation in a
//! storage type such as `InlAwi` or `ExtAwi`. The sink concatenations are
//! optional. If there is only a source concatenation, `inlawi_` and `extawi_`
//! will just construct the value of the source, and `cc_` will just perform
//! bounds checks.
//!
//! These macros automate a large number of things for the user:
//! - Using only `const` capable constructions and functions if possible
//! - All bounds checks are run before any fielding happens, so that no
//! mutation or allocation occurs when an error is returned, matching the
//! common behavior of functions in the `awint` system.
//! - Trying to optimize away as many bounds checks as possible
//! - Trying to optimize away intermediate buffers
//! - Concatenating literals together at compile time, and even returning
//! infallibly if possible
//! - Trying to use the most efficient copying method
//!
//! Before going into detail on each component type, we will first explain all
//! the error conditions for the index bounds checks. Even though `cc_` macros
//! with only a source concatenation do no construction or copying, they are
//! useful for bounds checks. Here, we pass the simplest variable input to the
//! `cc!` macro, a single concatenation with a single component.
//!
//! ```
//! use awint::prelude::*;
//!
//! let x = ExtAwi::zero(bw(10));
//! let r0 = 2;
//! let r1 = 8;
//! let r2 = 20;
//!
//! // The input is just the variable `x`. The macro is able to determine that
//! // no bounds checks are needed, so it returns `()`.
//! assert_eq!(cc!(x), ());
//!
//! // This component is the variable `x` indexed with the bit range `r0..r1`.
//! // The bounds checks succeed, so the macro returns `Some(())`.
//! assert!(cc!(x[r0..r1]).is_some());
//!
//! // We could also use a static range. We call it "static" because the macro
//! // is able to know the value of the range at compile time.
//! assert!(cc!(x[2..8]).is_some());
//!
//! // The first kind of invalid bound is a reversed range, in which the
//! // start of the range is larger than the end of the range.
//! assert!(cc!(x[r1..r0]).is_none());
//!
//! // Here, the macro is able to determine at compile time that the range is
//! // reversed
//! //cc!(x[8..2]); // error: ... has a reversed range
//!
//! // The second kind of invalid bound is a range that extends beyond the
//! // width of the variable or literal. Earlier, the values of 2 and 8 were
//! // less than or equal to `x.bw()`, so the check succeeded. Here the value
//! // of 20 causes the macro to return `None`.
//! assert!(cc!(x[r0..r2]).is_none());
//!
//! // Note: the widths of concatenations can be zero for the `cc_` macros.
//! // Static zero width ranges will cause a compile-time panic as a warning
//! // about components that do nothing, but they are achievable with dynamic
//! // ranges.
//! let r = 5;
//! assert!(cc!(x[r..r]).is_some());
//! let r = 10;
//! assert!(cc!(x[r..r]).is_some());
//! // The restriction about values not being larger than `x.bw()` still
//! // applies.
//! let r = 11;
//! assert!(cc!(x[r..r]).is_none());
//!
//! // `InlAwi`s and `ExtAwi`s cannot have zero bitwidths, so zero width
//! // concatenations will cause their macros to panic at compile time or
//! // return `None`.
//! let r = 5;
//! assert!(extawi!(x[r..r]).is_none());
//! // error: determined statically that this has zero bitwidth
//! //let _ = inlawi!(x[5..5]);
//!
//! // The third error condition occurs when concatenation bitwidths are
//! // unequal, but first we need to go into more detail on the component
//! // types.
//! ```
//!
//! ### Literals
//!
//! When the parser sees that the first character of a component is '-' or
//! '0'..='9', and it isn't part of a lone range, it will assume the component
//! is a literal and pass it to the `FromStr` implementation of `ExtAwi`. See
//! that documentation for more details.
//!
//! Note: The `FromStr` implementation allows for signed and unsigned values,
//! binary, octal, decimal, and hexadecimal bases, but for the remainder of this
//! documentation we will mainly be using unsigned hexadecimal. This is because
//! it neatly divides along bit multiples of 4, and the large base allows one to
//! easily see where different groups of 4 bits are being copied.
//!
//! ```
//! use awint::prelude::*;
//!
//! // Here, we pass a single concatenation of 3 literals to the `inlawi!`
//! // construction macro. constructs an `InlAwi` out of a 4 bit signed
//! // negative 1, a 16 bit binary string, and a 8 bit unsigned 42. The
//! // total bitwidth is 4 + 16 + 8 = 28.
//! let awi: inlawi_ty!(28) = inlawi!(-1i4, 0000_0101_0011_1001, 42u8);
//! assert_eq!(awi, inlawi!(1111_0000010100111001_00101010));
//!
//! // Literals can have static ranges applied to them, which might be useful
//! // in some circumstances for readability.
//! assert_eq!(inlawi!(0x654321_u24[4..16]), inlawi!(0x432_u12));
//!
//! // Arbitrary dynamic ranges using things from outside the macro can also
//! // be applied. The macros will assume the range bounds to result in `usize`.
//! let x: usize = 8;
//! let awi = ExtAwi::zero(bw(12));
//! // At runtime, `x` evaluates to 8 and `x + awi.bw()` evaluates to 20
//! assert_eq!(
//! extawi!(0x98765_u20[x..(x + awi.bw())]).unwrap(),
//! extawi!(0x987u12)
//! );
//! ```
//!
//! ### Variables
//!
//! Anything that has well defined `bw() -> usize`, `const_as_ref() -> &Bits`,
//! and `const_as_mut() -> &mut Bits` functions can be used as a variable.
//! Arbitrary `Bits` references (`Bits` itself has these functions, they are
//! just hidden from the documentation), `ExtAwi`, `InlAwi`, and other well
//! defined arbitrary width integer types can thus be used as variables.
//!
//! ```
//! use awint::prelude::*;
//!
//! let source = inlawi!(0xc4di64);
//! // a bunch of zeroed 64 bit arbitrary width integers from different
//! // storage types and construction methods.
//! let mut awi = ExtAwi::zero(bw(64));
//! let a = awi.const_as_mut();
//! let mut b = extawi!(0i64);
//! let mut c = <inlawi_ty!(64)>::zero();
//!
//! // Use the `cc` macro to copy the source concatenation `source` to the sink
//! // concatenations `a`, `b`, and `c`. Here, every concatenation is just a
//! // single variable component.
//! cc!(source; a; b; c).unwrap();
//!
//! assert_eq!(a, inlawi!(0xc4di64).const_as_ref());
//! assert_eq!(b, extawi!(0xc4di64));
//! assert_eq!(c, inlawi!(0xc4di64));
//!
//! let awi = inlawi!(0xau4);
//! let a = awi.const_as_ref();
//! let b = extawi!(0xbu4);
//! let c = inlawi!(0xcu4);
//!
//! // Use `extawi` to infallibly concatenate variables together. Here, there
//! // is only one concatenation with multiple variable components.
//! assert_eq!(extawi!(a, b, c), extawi!(0xabcu12));
//! ```
//!
//! Now that we have both literals and variables, we can demonstrate more
//! complicated interactions. In case it still isn't clear what the
//! "corresponding copying" means, here we have a source concatenation of 4
//! components each with 3 hexadecimal digits being copied onto a sink
//! concatenation of 3 components each with 4 hexadecimal digits.
//!
//! ```
//! use awint::prelude::*;
//!
//! let y3 = inlawi!(0xba9u12);
//! let y2 = inlawi!(0x876u12);
//! let y1 = inlawi!(0x543u12);
//! let y0 = inlawi!(0x210u12);
//!
//! let mut z2 = inlawi!(0u16);
//! let mut z1 = inlawi!(0u16);
//! let mut z0 = inlawi!(0u16);
//!
//! cc!(
//! y3, y2, y1, y0;
//! z2, z1, z0;
//! ).unwrap();
//!
//! assert_eq!(z2, inlawi!(0xba98u16));
//! assert_eq!(z1, inlawi!(0x7654u16));
//! assert_eq!(z0, inlawi!(0x3210u16));
//! ```
//!
//! Visually, the components in the two concatenations are being aligned like
//! this:
//!
//! ```text
//! |-----y3----|-----y2----|-----y1----|-----y0----|
//! | b a 9 | 8 7 6 | 5 4 3 | 2 1 0 |
//! | b a 9 8 | 7 6 5 4 | 3 2 1 0 |
//! |-------z2------|-------z1------|-------z0------|
//! ```
//!
//! Again, arbitrary ranges can be applied to variables:
//!
//! ```
//! use awint::prelude::*;
//!
//! let mut a = inlawi!(0x9876543210u40);
//!
//! let b = extawi!(
//! a[..=7], a[(a.bw() - 16)..];
//! a[(5 * 4)..(9 * 4)], a[..(2 * 4)];
//! ).unwrap();
//! assert_eq!(a, inlawi!(0x9109843276u40));
//! assert_eq!(b, extawi!(0x109876_u24));
//! ```
//!
//! ### Fillers
//!
//! The third type of component is written as a range with no variable or
//! literal attached. When used in sources, corresponding sink bits are left
//! unmutated. When used in sinks, corresponding source bits have no effect.
//!
//! When used in the source bits of specified initialization construction macros
//! (`inlawi_zero!`, `extawi_zero`, `inlawi_umax!`, etc), fillers adopt what the
//! corresponding bits would be normally initialized to (zero, unsigned
//! maximum value, etc).
//!
//! The unspecified initialization macros `inlawi!` and `extawi!` do not allow
//! fillers in their source concatenations, because it would be ambiguous what
//! those bits should initially be set to.
//!
//! ```
//! use awint::prelude::*;
//!
//! let x = inlawi!(0x1234u16);
//! let mut y = inlawi!(0x5678u16);
//!
//! // note: because the range bounds cannot be negative, ranges starting
//! // with 0 (e.x. `0..r`) can have the zero omitted and just use `..r`.
//! cc!(
//! x, ..8;
//! ..8, y;
//! ).unwrap();
//!
//! assert_eq!(y, inlawi!(0x3478u16));
//! ```
//!
//! The `..8` filler in the source aligned with the two digits `0x78u8` in the
//! `y` component in the sink, so they were left unmutated. The digits `0x34u8`
//! in the source aligned with and overwrote `0x56u8` in the sink. The `0x12u8`
//! aligned with the `..8` filler in the sink, so they did nothing.
//!
//! Fillers are also useful in cases where all concatenations lack a needed
//! degree of determinable width, and we want a cheap way to specify it:
//!
//! ```
//! use awint::prelude::*;
//!
//! let x = extawi!(-99i44);
//!
//! // error: `InlAwi` construction macros need at least one concatenation to
//! // have a width that can be determined statically by the macro
//! //inlawi!(x);
//!
//! assert_eq!(inlawi!(x; ..44).unwrap(), inlawi!(-99i44));
//!
//! // error: there is a only a source concatenation that has no statically
//! // or dynamically determinable width
//! //extawi_umax!(.., x);
//!
//! let r = 128;
//! assert_eq!(extawi_umax!(.., x; ..r).unwrap(), extawi!(-99i128));
//! ```
//!
//! ### Unbounded fillers
//!
//! Unbounded fillers can be thought as dynamically resizing fillers that
//! try to expand until the bitwidths of different concatenations match. To
//! understand how unbounded fillers interact, consider these three cases:
//!
//! ```
//! use awint::prelude::*;
//!
//! // This first case has no fillers in the first concatenation, so the filler
//! // in the second concatenation will expand to be of bitwidth `12 - y.bw()`.
//!
//! let mut y = extawi!(0u8);
//! cc!(
//! 0x321u12;
//! .., y;
//! ).unwrap();
//! assert_eq!(y, extawi!(0x21u8));
//!
//! // Do the same call again, but with `y.bw() == 4`
//! let mut y = extawi!(0u4);
//! cc!(
//! 0x321u12;
//! .., y;
//! ).unwrap();
//! assert_eq!(y, extawi!(0x1u4));
//!
//! // The 12 bits of the first concatenation cannot correspond with
//! // the minimum 16 bits of the second, so this call returns `None`.
//! let mut y = extawi!(0u16);
//! assert!(cc!(0x321u12; .., y).is_none());
//!
//! // This second case ends up enforcing that `y.bw()` is at least 12. The 12
//! // bits of the literal always get copied to the least significant bits of
//! // `y`.
//!
//! let mut y = extawi!(0u20);
//! cc!(
//! .., 0x321u12;
//! y;
//! ).unwrap();
//! assert_eq!(y, extawi!(0x00321u20));
//!
//! let mut y = extawi_umax!(32);
//! cc!(
//! .., 0x321u12;
//! y;
//! ).unwrap();
//! assert_eq!(y, extawi!(0xffff_f321_u32));
//!
//! let mut y = extawi_umax!(8);
//! assert!(cc!(.., 0x321u12; y).is_none());
//!
//! // The third case allows `y.bw()` to be any possible bitwidth. If
//! // `y.bw() < 12` it will act like the first case, otherwise it acts like the
//! // second case. Because there are no restrictions on concatenation widths
//! // and there are no ranges that could index the variables out of bounds,
//! // these calls are infallible and no `Option` is returned.
//!
//! let mut y = extawi!(0u20);
//! cc!(
//! .., 0x321u12;
//! .., y;
//! );
//! assert_eq!(y, extawi!(0x00321u20));
//!
//! let mut y = extawi!(0u4);
//! cc!(
//! .., 0x321u12;
//! .., y;
//! );
//! assert_eq!(y, extawi!(0x1u4));
//!
//! // Unbounded fillers are also allowed in less significant positions, in
//! // which case alignment of the components occurs starting from the most
//! // significant bit.
//!
//! let mut y = extawi!(0u20);
//! cc!(
//! 0x321u12, ..;
//! y, ..;
//! );
//! assert_eq!(y, extawi!(0x32100u20));
//!
//! // The macros are even smart enough to do this:
//!
//! let mut y = extawi_umax!(24);
//! cc!(0x3u4, .., 0x21u8; y).unwrap();
//! assert_eq!(y, extawi!(0x3fff21u24));
//!
//! // Note again that filler widths cannot be negative, and so this will cause
//! // an error because we are trying to compress the 4 bit and 8 bit components
//! // into a less than 12 bit space.
//!
//! let mut y = extawi!(0u8);
//! assert!(cc!(0x3u4, .., 0x21u8; y).is_none());
//! ```
//!
//! Only one unbounded filler per concatenation is allowed. Consider this case,
//! in which it would be ambiguous about how the middle component should be
//! aligned.
//!
//! ```text
//! cc!(
//! .., 0x321u12, ..;
//! y;
//! ); // Error: there is more than one unbounded filler
//! ```
//!
//! Additionally, multiple concatenations with unbounded fillers must all have
//! their fillers aligned to the same end or have a concatenation without
//! an unbounded filler.
//!
//! ```text
//! // all allowed:
//! cc!(
//! .., x;
//! .., y;
//! .., z;
//! );
//! cc!(
//! x, ..;
//! y, ..;
//! z, ..;
//! );
//! // allowed, because the macro can infer the alignment by using the bitwidth
//! // of `var`
//! cc!(
//! .., x;
//! y, .., z;
//! a, ..;
//! var;
//! );
//! // disallowed, because the overlaps are ambiguous:
//! cc!(
//! .., x;
//! y, ..;
//! );
//! cc!(
//! a, .., x;
//! b, .., y;
//! c, .., z;
//! );
//! ```
//!
//! It is technically possible to infer that ambiguous overlap could not occur
//! in this case, but this is still disallowed by the macro, and it is more
//! readable to just split the macro into two for both alignments.
//!
//! ```text
//! cc!(
//! 0x4567u16, .., 0x123u12;
//! y0[..4], .., y1[..4];
//! );
//! // the above macro is semantically equivalent to these two macros combined:
//! cc!(
//! 0x4567u16, ..;
//! y0[..4], ..;
//! );
//! cc!(
//! .., 0x123u12;
//! .., y1[..4];
//! );
//! ```
//!
//! ### Other Notes
//!
//! - If dynamic values are used in ranges, they should not use generator like
//! behavior (e.x. using a function that changes its output between calls in
//! `x[f()..=f()]`), or else you may get unexpected behavior. The parser and
//! code generator treats identical strings like they produce the same value
//! every time, and would call `f()` only once.
//! - In general, the macros use the `Bits::field` operation to copy different
//! bitfields independently to a buffer, then field from the buffer to the
//! sink components. When concatenations take the form `variable or constant
//! with full range; var_1[..]; var_2[..]; var_3[..], ...`, the macros use
//! `Bits::copy_assign` to directly copy without an intermediate buffer. This
//! copy assigning mode cannot copy between `Bits` references that point to
//! the same underlying storage, because it results in aliasing. Thus, trying
//! to do something like `cc!(x; x)` results in the borrow checker complaining
//! about macro generated variables within the macro being borrowed as both
//! immutable and mutable. `cc!(x; x)` is semantically a no-op anyway, so it
//! should not be used.
//! - The code generated by the macros takes special care to avoid panicking or
//! overflowing. If a range is reversed (the start value is larger than the
//! end value), the macro will will return a compile time error or `None` at
//! runtime before trying to calculate something like `end - start`. The only
//! way to overflow is to exceed `usize::MAX` in concatenation widths, or the
//! individual arbitrary expressions entered into the macro overflow (e.x.
//! `..(usize::MAX + 1)`).
//! - Under some conditions, warnings about unused `Option<()>`s are not
//! produced from procedural macros. If a fallible macro is called without
//! appending `.unwrap()` or otherwise handling the `Option`, it will silently
//! do nothing if `None` is returned.
//! - The macros have to be unsanitized to support using arbitrary variables and
//! ranges. It is still very hard to escape the scoping by some kind of
//! injection, because all arbitrary values are individually assigned to a
//! `let _: usize = ...;` statement, all variable names are individually
//! referenced like `let _: &Bits = (...).const_as_ref();`, and all semicolons
//! are eliminated early in the parsing process. Only obviously bad inputs
//! using things such as multiple unmatched parenthesis, other specially
//! designed macros, or fake structs could cause compiling broken checks or
//! problems that extend beyond the immediate scope of the macro.
//! - In case you want to see the code generated by a macro, you can use the
//! `awint_macro_internals::code_gen` function and call it with the same
//! arguments as the respective macro in `awint_macros/src/lib.rs`.
extern crate alloc;
use alloc::{format, string::ToString};
extern crate proc_macro;
use awint_macro_internals::*;
use proc_macro::TokenStream;
/// Specifies an `InlAwi` _type_ in terms of its bitwidth as a `usize` literal.
#[proc_macro]
pub fn inlawi_ty(input: TokenStream) -> TokenStream {
let bw = input
.to_string()
.parse::<u128>()
.expect("Input should parse as an unsigned integer");
assert!(
bw != 0,
"Tried to make an `InlAwi` type with an invalid bitwidth of 0"
);
unstable_native_inlawi_ty(bw).parse().unwrap()
}
// TODO `0x1234.5678p4i32p16`
// prefix,integral,point,fractional,exPonent (can't use 'e' because it doesn't
// work in hexadecimal, maybe allow for decimal),signed,bitwidth,point position
// C4D
/// Copy Corresponding Concatenations of Components Dynamically. Takes
/// concatenations of components as an input, and copies bits of the source to
/// corresponding bits of the sinks. Returns nothing if the operation is
/// infallible, otherwise returns `Option<()>`. Returns `None` if component
/// indexes are out of bounds or if concatenation bitwidths mismatch. Performs
/// allocation in general, but will try to avoid allocation if the common
/// bitwdith can be determined statically, or if concatenations are all of
/// single components. See the documentation of `awint_macros` for more.
#[proc_macro]
pub fn cc(input: TokenStream) -> TokenStream {
match code_gen(&input.to_string(), false, "zero", false, false) {
Ok(s) => s.parse().unwrap(),
Err(s) => panic!("{}", s),
}
}
/// Takes concatenations of components as an input, and copies bits of the
/// source to corresponding bits of the sinks. The source value is also used to
/// construct an `InlAwi`. The common width must be statically determinable by
/// the macro (e.g. at least one concatenation must have only literal ranges),
/// and the source cannot contain fillers. Returns a plain `InlAwi` if
/// infallible from what the macro can statically determine, otherwise returns
/// `Option<InlAwi>`. Returns `None` if component indexes are invalid or if
/// concatenation bitwidths mismatch. See the documentation of `awint_macros`
/// for more.
#[proc_macro]
pub fn inlawi(input: TokenStream) -> TokenStream {
match code_gen(&input.to_string(), false, "zero", true, true) {
Ok(s) => s.parse().unwrap(),
Err(s) => panic!("{}", s),
}
}
/// Takes concatenations of components as an input, and copies bits of the
/// source to corresponding bits of the sinks. The source value is also used to
/// construct an `ExtAwi`. The common width must be dynamically determinable by
/// the macro (e.g. not all concatenations can have unbounded fillers), and the
/// source cannot contain fillers. Returns a plain `ExtAwi` if infallible from
/// what the macro can statically determine, otherwise returns `Option<ExtAwi>`.
/// Returns `None` if component indexes are invalid or if concatenation
/// bitwidths mismatch. See the documentation of `awint_macros` for more.
#[proc_macro]
pub fn extawi(input: TokenStream) -> TokenStream {
match code_gen(&input.to_string(), false, "zero", false, true) {
Ok(s) => s.parse().unwrap(),
Err(s) => panic!("{}", s),
}
}
macro_rules! cc_construction {
($($fn_name:ident, $cc_fn:expr, $doc:expr);*;) => {
$(
#[doc = "The same as `cc!` but with"]
#[doc = $doc]
#[doc = "specified initialization."]
#[proc_macro]
pub fn $fn_name(input: TokenStream) -> TokenStream {
match code_gen(&input.to_string(), true, $cc_fn, false, false) {
Ok(s) => s.parse().unwrap(),
Err(s) => panic!("{}", s),
}
}
)*
};
}
cc_construction!(
cc_zero, "zero", "Zero-value";
cc_umax, "umax", "Unsigned-maximum-value";
cc_imax, "imax", "Signed-maximum-value";
cc_imin, "imin", "Signed-minimum-value";
cc_uone, "uone", "Unsigned-one-value";
);
macro_rules! inlawi_construction {
($($fn_name:ident, $inlawi_fn:expr, $doc:expr);*;) => {
$(
#[doc = "The same as `inlawi!` but with"]
#[doc = $doc]
#[doc = "specified initialization."]
#[proc_macro]
pub fn $fn_name(input: TokenStream) -> TokenStream {
if let Ok(bw) = input.to_string().parse::<u128>() {
assert!(
bw != 0,
"Tried to construct an `InlAwi` with an invalid bitwidth of 0"
);
format!("{}::{}()", unstable_native_inlawi_ty(bw as u128), $inlawi_fn)
.parse()
.unwrap()
} else {
match code_gen(&input.to_string(), true, $inlawi_fn, true, true) {
Ok(s) => s.parse().unwrap(),
Err(s) => panic!("{}", s),
}
}
}
)*
};
}
inlawi_construction!(
inlawi_zero, "zero", "Zero-value";
inlawi_umax, "umax", "Unsigned-maximum-value";
inlawi_imax, "imax", "Signed-maximum-value";
inlawi_imin, "imin", "Signed-minimum-value";
inlawi_uone, "uone", "Unsigned-one-value";
);
macro_rules! extawi_construction {
($($fn_name:ident, $extawi_fn:expr, $doc:expr);*;) => {
$(
#[doc = "The same as `extawi!` but with"]
#[doc = $doc]
#[doc = "specified initialization."]
#[proc_macro]
pub fn $fn_name(input: TokenStream) -> TokenStream {
if let Ok(bw) = input.to_string().parse::<u128>() {
assert!(
bw != 0,
"Tried to construct an `ExtAwi` with an invalid bitwidth of 0"
);
format!("ExtAwi::panicking_{}({})", $extawi_fn, bw)
.parse()
.unwrap()
} else {
match code_gen(&input.to_string(), true, $extawi_fn, false, true) {
Ok(s) => s.parse().unwrap(),
Err(s) => panic!("{}", s),
}
}
}
)*
};
}
extawi_construction!(
extawi_zero, "zero", "Zero-value";
extawi_umax, "umax", "Unsigned-maximum-value";
extawi_imax, "imax", "Signed-maximum-value";
extawi_imin, "imin", "Signed-minimum-value";
extawi_uone, "uone", "Unsigned-one-value";
);
/*
#[cfg(feature = "dag")]
#[proc_macro_attribute]
pub fn create_dag(attr: TokenStream, item: TokenStream) -> TokenStream {
// `attr` gets set to the variable name to which the DAG is set
}
*/