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 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
use std::fmt::Write;
use awint_ext::{awint_core::OrdBits, Awi};
use triple_arena::{OrdArena, Ptr};
use crate::{
chars_to_string, Ast, Component, ComponentType, Concatenation, FillerAlign, FnNames, Names,
PBind, PCWidth, PVal, PWidth, Usb,
};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Bind {
Literal(OrdBits<Awi>),
// text must be lowered by this point so that the set property works
Txt(Vec<char>),
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Value {
/// value comes from bitwidth of binding
Bitwidth(PBind),
/// value comes from `usize` literal or variable
Usize(String),
}
/// Bitwidth as described by a single value or one value minus another
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Width {
Single(PVal),
Range(PVal, PVal),
}
/// For concatenation widths
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct CWidth(Vec<PWidth>);
pub struct Lower<'a> {
/// The first bool is if the binding is used, the second is for if it needs
/// to be mutable
pub binds: OrdArena<PBind, Bind, (bool, bool)>,
/// The bool is if the value is used
pub values: OrdArena<PVal, Value, bool>,
/// The first bool is if the width is used for lt checks, the second if it
/// needs to be assigned to a `let` binding
pub widths: OrdArena<PWidth, Width, (bool, bool)>,
// the bool is if the cw is used
pub cw: OrdArena<PCWidth, CWidth, bool>,
pub dynamic_width: Option<PCWidth>,
pub names: Names<'a>,
pub fn_names: FnNames<'a>,
}
impl<'a> Lower<'a> {
pub fn new(names: Names<'a>, fn_names: FnNames<'a>) -> Self {
Self {
binds: OrdArena::new(),
values: OrdArena::new(),
widths: OrdArena::new(),
cw: OrdArena::new(),
dynamic_width: None,
names,
fn_names,
}
}
/// In cases like `a[(a.bw() - 16)..]` we want to replace `a.bw()` with
/// `Bits::bw(bind_to_a)` or else we run into borrow issues. This is
/// admittedly jank, but fixes the vast majority of these kinds of cases
/// without needing a full fledged parser for arithmetic and function calls.
pub fn try_txt_bound_to_binding(&mut self, txt: &[char]) -> String {
if txt.ends_with(self.fn_names.bw_call) {
let var = txt[..(txt.len() - 5)].to_owned();
if let Some(p) = self.binds.find_key(&Bind::Txt(var)) {
self.binds.get_val_mut(p).unwrap().0 = true;
return format!("{}({}_{})", self.fn_names.get_bw, self.names.bind, p.inx())
}
}
chars_to_string(txt)
}
pub fn lower_bound(&mut self, usb: &Usb) -> PVal {
if let Some(x) = usb.static_val() {
self.values.insert(Value::Usize(format!("{x}")), false).0
} else {
let txt = self.try_txt_bound_to_binding(&usb.s);
if usb.x < 0 {
self.values
.insert(
Value::Usize(format!("{}({},{})", self.fn_names.usize_sub, txt, -usb.x)),
false,
)
.0
} else if usb.x == 0 {
self.values.insert(Value::Usize(txt), false).0
} else {
self.values
.insert(
Value::Usize(format!("{}({},{})", self.fn_names.usize_add, txt, usb.x)),
false,
)
.0
}
}
}
/// Returns the width corresponding to the range of the component, and
/// internally pushes the upperbound-bitwidth check.
pub fn lower_comp(&mut self, comp: &mut Component) -> Option<PWidth> {
if comp.is_unbounded_filler() {
return None
}
// push width between the upper bound and bitwidth of variable, so that the
// later reversal check will prevent the upper bound from being beyond the
// bitwidth
let need_extra_check =
comp.bind.is_some() && (!comp.has_full_range()) && (!comp.is_static_literal());
let res = Some(if let Some(w) = comp.range.static_width() {
if !comp.range.start.as_ref().unwrap().is_guaranteed_zero() {
let start_p_val = self.lower_bound(comp.range.start.as_ref().unwrap());
comp.start = Some(start_p_val);
}
let p_val = self.values.insert(Value::Usize(format!("{w}")), false).0;
if need_extra_check {
if let Some(ref end) = comp.range.end {
// range end is not the same as variable end, need to check
let end_p_val = self.lower_bound(end);
let var_end_p_val = self
.values
.insert(Value::Bitwidth(comp.bind.unwrap()), false)
.0;
let _ = self
.widths
.insert(Width::Range(end_p_val, var_end_p_val), (true, false));
};
}
self.widths.insert(Width::Single(p_val), (false, false)).0
} else {
let end_p_val = if let Some(ref end) = comp.range.end {
let end_p_val = self.lower_bound(end);
if need_extra_check {
// range end is not the same as variable end, need to check
let var_end_p_val = self
.values
.insert(Value::Bitwidth(comp.bind.unwrap()), false)
.0;
let _ = self
.widths
.insert(Width::Range(end_p_val, var_end_p_val), (true, false));
}
end_p_val
} else {
// need information from component
self.values
.insert(Value::Bitwidth(comp.bind.unwrap()), false)
.0
};
if comp.range.start.as_ref().unwrap().is_guaranteed_zero() {
self.widths
.insert(Width::Single(end_p_val), (false, false))
.0
} else {
let start_p_val = self.lower_bound(comp.range.start.as_ref().unwrap());
comp.start = Some(start_p_val);
self.widths
.insert(Width::Range(start_p_val, end_p_val), (true, false))
.0
}
});
comp.width = res;
res
}
pub fn lower_concat(&mut self, concat: &mut Concatenation) {
let mut v = vec![];
for comp in &mut concat.comps {
if let Some(w) = self.lower_comp(comp) {
v.push(w);
}
}
// `v` can be empty in cases like `extawi!(umax: ..; ..8)`
if !v.is_empty() {
concat.cw = Some(self.cw.insert(CWidth(v), false).0);
}
}
/// Checks that ranges aren't reversed and the upper bounds are not beyond
/// bitwidths. Returns `true` and "0;0" for both strings if there are no
/// checks (for `awint_dag` purposes)
pub fn lower_le_checks(&mut self) -> (bool, String, String) {
let mut s0 = String::new();
let mut s1 = String::new();
for (_, width, used) in self.widths.iter() {
if used.0 {
if let Width::Range(lo, hi) = width {
if !s0.is_empty() {
s0 += ",";
s1 += ",";
}
*self.values.get_val_mut(*lo).unwrap() = true;
*self.values.get_val_mut(*hi).unwrap() = true;
write!(s0, "{}_{}", self.names.value, lo.inx(),).unwrap();
write!(s1, "{}_{}", self.names.value, hi.inx()).unwrap();
}
}
}
if s0.is_empty() {
(true, "0;0".to_owned(), "0;0".to_owned())
} else {
(false, s0, s1)
}
}
/// Checks that we aren't trying to squeeze unbounded fillers into
/// negative widths for nondeterministic cases and that deterministic concat
/// widths are equal to the common bitwidth. Returns `true` and "0;0" for
/// both strings if there are no checks (for `awint_dag` purposes)
pub fn lower_common_checks(&mut self, ast: &Ast) -> (bool, String, String) {
if matches!(
ast.overall_alignment,
FillerAlign::Single | FillerAlign::Lsb | FillerAlign::Msb
) {
// always infallible except potentially with respect to 0 bitwidth return
// situations which is handled elsewhere
return (true, "0;0".to_owned(), "0;0".to_owned())
}
let mut ge = String::new();
let mut eq = String::new();
for concat in &ast.cc {
if concat.static_width.is_none() {
if let Some(cw) = concat.cw {
*self.cw.get_val_mut(cw).unwrap() = true;
if concat.deterministic_width {
let mut set_dynamic_width = false;
if self.dynamic_width.is_none() {
self.dynamic_width = Some(cw);
set_dynamic_width = true;
}
// we can avoid comparing the same value against itself, however the second
// condition handles cases like `inlawi!(a; ..64)` where the dynamic width
// might not be equal to the static width we expect
if (!set_dynamic_width) || ast.common_bw.is_some() {
if !eq.is_empty() {
eq += ",";
}
write!(eq, "{}_{}", self.names.cw, cw.inx()).unwrap();
}
} else {
if !ge.is_empty() {
ge += ",";
}
write!(ge, "{}_{}", self.names.cw, cw.inx()).unwrap();
}
}
}
}
if eq.is_empty() && ge.is_empty() {
(true, "0;0".to_owned(), "0;0".to_owned())
} else {
if eq.is_empty() {
eq = "0;0".to_owned();
}
if ge.is_empty() {
ge = "0;0".to_owned();
}
(false, ge, eq)
}
}
#[allow(clippy::too_many_arguments)]
pub fn write_field(
&mut self,
s: &mut String,
lhs: &str,
to: Option<&str>,
rhs: &str,
from: Option<&str>,
width: &str,
width1: bool,
) {
if width1 {
// #[inline] is on Bits::field_bit, Bits::get, and Bits::set so this is ok
match (to, from) {
(Some(to), Some(from)) => write!(
s,
"let _ = {}({},{},{},{});",
self.fn_names.field_bit, lhs, to, rhs, from
)
.unwrap(),
(Some(to), None) => write!(
s,
"let _ = {}({},{},{},0);",
self.fn_names.field_bit, lhs, to, rhs
)
.unwrap(),
(None, Some(from)) => write!(
s,
"let _ = {}({},0,{},{});",
self.fn_names.field_bit, lhs, rhs, from
)
.unwrap(),
(None, None) => write!(
s,
"let _ = {}({},0,{},0);",
self.fn_names.field_bit, lhs, rhs
)
.unwrap(),
}
} else {
match (to, from) {
(Some(to), Some(from)) => write!(
s,
"let _ = {}({},{},{},{}",
self.fn_names.field, lhs, to, rhs, from
)
.unwrap(),
(Some(to), None) => write!(
s,
"let _ = {}({},{},{}",
self.fn_names.field_to, lhs, to, rhs
)
.unwrap(),
(None, Some(from)) => write!(
s,
"let _ = {}({},{},{}",
self.fn_names.field_from, lhs, rhs, from
)
.unwrap(),
(None, None) => {
write!(s, "let _ = {}({},{}", self.fn_names.field_width, lhs, rhs).unwrap()
}
}
write!(s, ",{width});").unwrap();
}
}
pub fn field_comp(
&mut self,
s: &mut String,
comp: &Component,
msb_align: bool,
from_buf: bool,
first_in_align: bool,
last_in_align: bool,
) {
let width = comp.width.unwrap();
let mut width1 = false;
if let Some(w) = comp.range.static_width() {
if w == 1 {
width1 = true;
}
}
let width_s = format!("{}_{}", self.names.width, width.inx());
if msb_align {
// subtract the shift amount first
if first_in_align {
write!(
s,
"let mut {}={}({},{}_{});",
self.names.shl,
self.fn_names.usize_sub,
self.names.cw,
self.names.width,
width.inx()
)
.unwrap();
} else {
write!(
s,
"{}={}({},{}_{});",
self.names.shl,
self.fn_names.usize_sub,
self.names.shl,
self.names.width,
width.inx()
)
.unwrap();
}
}
self.widths.get_val_mut(width).unwrap().1 = true;
if let Some(bind) = comp.bind {
let bind_s = format!("{}_{}", self.names.bind, bind.inx());
if from_buf {
*self.binds.get_val_mut(bind).unwrap() = (true, true);
if let Some(start) = comp.start {
*self.values.get_val_mut(start).unwrap() = true;
}
let start_s = if let Some(start) = comp.start {
format!("{}_{}", self.names.value, start.inx())
} else {
String::new()
};
self.write_field(
s,
&bind_s,
if start_s.is_empty() {
None
} else {
Some(&start_s)
},
self.names.awi_ref,
if (!msb_align) && first_in_align {
None
} else {
Some(self.names.shl)
},
&width_s,
width1,
);
} else {
self.binds.get_val_mut(bind).unwrap().0 = true;
if let Some(start) = comp.start {
*self.values.get_val_mut(start).unwrap() = true;
}
let start_s = if let Some(start) = comp.start {
format!("{}_{}", self.names.value, start.inx())
} else {
String::new()
};
self.write_field(
s,
self.names.awi_ref,
if (!msb_align) && first_in_align {
None
} else {
Some(self.names.shl)
},
&bind_s,
if start_s.is_empty() {
None
} else {
Some(&start_s)
},
&width_s,
width1,
);
}
} // else is a filler, keep shift changes however
// this runs for both fillers and nonfillers
if !(msb_align || last_in_align) {
// add to the shift amount afterwards
if first_in_align {
write!(
s,
"let mut {}={}_{};",
self.names.shl,
self.names.width,
width.inx()
)
.unwrap();
} else {
write!(
s,
"{}={}({},{}_{});",
self.names.shl,
self.fn_names.usize_add,
self.names.shl,
self.names.width,
width.inx()
)
.unwrap();
}
}
writeln!(s).unwrap();
}
pub fn field_concat(&mut self, concat: &Concatenation, from_buf: bool) -> String {
if (concat.comps.len() == 1) && concat.comps[0].has_full_range() {
if matches!(&concat.comps[0].c_type, ComponentType::Filler) {
return String::new()
}
// use copy_
let sink = concat.comps[0].bind.unwrap();
if from_buf {
*self.binds.get_val_mut(sink).unwrap() = (true, true);
return format!(
"let _ = {}({}_{},{});\n",
self.fn_names.copy_,
self.names.bind,
sink.inx(),
self.names.awi_ref,
)
} else {
self.binds.get_val_mut(sink).unwrap().0 = true;
return format!(
"let _ = {}({},{}_{});\n",
self.fn_names.copy_,
self.names.awi_ref,
self.names.bind,
sink.inx(),
)
}
}
let mut s = String::new();
let mut lsb_i = 0;
while lsb_i < concat.comps.len() {
if concat.comps[lsb_i].width.is_none() {
break
}
self.field_comp(
&mut s,
&concat.comps[lsb_i],
false,
from_buf,
lsb_i == 0,
(lsb_i + 1) == concat.comps.len(),
);
lsb_i += 1;
}
let mut msb_i = concat.comps.len() - 1;
while msb_i > lsb_i {
if concat.comps[msb_i].width.is_none() {
break
}
self.field_comp(
&mut s,
&concat.comps[msb_i],
true,
from_buf,
msb_i == (concat.comps.len() - 1),
(msb_i - 1) == lsb_i,
);
msb_i -= 1;
}
s
}
pub fn lower_fielding(
&mut self,
ast: &Ast,
source_has_filler: bool,
need_buffer: bool,
) -> String {
let mut s = String::new();
if source_has_filler && ast.txt_init.is_none() {
// see explanation at top of `lowering.rs`
for i in 1..ast.cc.len() {
// there are some situations where this could be deduplicated
// sink -> buf
writeln!(s, "{}", self.field_concat(&ast.cc[i], false)).unwrap();
// src -> buf
writeln!(s, "{}", self.field_concat(&ast.cc[0], false)).unwrap();
// buf -> sink
writeln!(s, "{}", self.field_concat(&ast.cc[i], true)).unwrap();
}
} else if need_buffer {
// src -> buf once
writeln!(s, "{}", self.field_concat(&ast.cc[0], false)).unwrap();
// buf -> sinks
for i in 1..ast.cc.len() {
writeln!(s, "{}", self.field_concat(&ast.cc[i], true)).unwrap();
}
} else {
// direct copy assigning
if let Some(src) = ast.cc[0].comps[0].bind {
self.binds.get_val_mut(src).unwrap().0 = true;
for i in 1..ast.cc.len() {
if let Some(sink) = ast.cc[i].comps[0].bind {
*self.binds.get_val_mut(sink).unwrap() = (true, true);
writeln!(
s,
"let _ = {}({}_{},{}_{});",
self.fn_names.copy_,
self.names.bind,
sink.inx(),
self.names.bind,
src.inx()
)
.unwrap();
}
}
} // else cases like `extawi!(umax: ..r)` or `extawi!(umax: ..;
// ..8)`
}
s
}
pub fn lower_cws(&mut self) -> String {
let mut s = String::new();
for (p_cw, cw, used) in self.cw.iter() {
if *used {
let mut tmp = String::new();
for (i, w) in cw.0.iter().enumerate() {
self.widths.get_val_mut(*w).unwrap().1 = true;
if i == 0 {
tmp = format!("{}_{}", self.names.width, w.inx());
} else {
tmp = format!(
"{}({},{}_{})",
self.fn_names.usize_add,
tmp,
self.names.width,
w.inx()
);
}
}
writeln!(s, "let {}_{}={};", self.names.cw, p_cw.inx(), tmp).unwrap();
}
}
s
}
pub fn lower_widths(&mut self) -> String {
let mut s = String::new();
for (p_w, w, used) in self.widths.iter() {
if used.1 {
match w {
Width::Single(v) => {
*self.values.get_val_mut(*v).unwrap() = true;
writeln!(
s,
"let {}_{}={}_{};",
self.names.width,
p_w.inx(),
self.names.value,
v.inx()
)
.unwrap();
}
Width::Range(v0, v1) => {
*self.values.get_val_mut(*v0).unwrap() = true;
*self.values.get_val_mut(*v1).unwrap() = true;
writeln!(
s,
"let {}_{}={}({}_{},{}_{});",
self.names.width,
p_w.inx(),
self.fn_names.usize_sub,
self.names.value,
v1.inx(),
self.names.value,
v0.inx()
)
.unwrap();
}
}
}
}
s
}
pub fn lower_values(&mut self) -> String {
let mut s = String::new();
for (p_v, v, used) in self.values.iter() {
if *used {
match v {
Value::Bitwidth(b) => {
self.binds.get_val_mut(*b).unwrap().0 = true;
writeln!(
s,
"let {}_{}={}({}({}_{}));",
self.names.value,
p_v.inx(),
self.fn_names.usize_cast,
self.fn_names.get_bw,
self.names.bind,
b.inx()
)
.unwrap();
}
Value::Usize(string) => {
writeln!(
s,
"let {}_{}={}({});",
self.names.value,
p_v.inx(),
self.fn_names.usize_cast,
string
)
.unwrap();
}
}
}
}
s
}
pub fn lower_bindings<F: FnMut(Awi) -> String>(
&mut self,
mut static_construction_fn: F,
) -> String {
let mut s = String::new();
for (p_b, bind, (used, mutable)) in self.binds.iter() {
if *used {
match bind {
Bind::Literal(ref awi) => {
writeln!(
s,
"let {}_{}:{}=&{};",
self.names.bind,
p_b.inx(),
self.fn_names.bits_ref,
(static_construction_fn)(Awi::from_bits(awi))
)
.unwrap();
}
Bind::Txt(ref chars) => {
let chars = chars_to_string(chars);
// note: we can't use extra braces in the bindings or else the E0716
// workaround doesn't work
if *mutable {
writeln!(
s,
"let {}_{}:{}=&mut {};",
self.names.bind,
p_b.inx(),
self.fn_names.mut_bits_ref,
chars
)
.unwrap();
} else {
writeln!(
s,
"let {}_{}:{}=&{};",
self.names.bind,
p_b.inx(),
self.fn_names.bits_ref,
chars
)
.unwrap();
}
}
}
}
}
s
}
}