blob: 434c094b0472d110f0e49d9b4257feaa6c6a1072 [file]
// Copyright 2026 The Fuchsia Authors
//
// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
// This file may not be copied, modified, or distributed except according to
// those terms.
//! Rust mirror of `Trusted.CppAst` (one variant per Lean constructor, with
//! identical field order) and its deterministic Lean rendering.
use std::{
collections::{BTreeMap, BTreeSet},
fmt::Write as _,
};
use anneal_lower_common::{chunk, scc};
mod cfg_and_program;
mod lean_program;
mod lean_terms;
pub use self::cfg_and_program::{
BinOp, Block, Body, EmitMeta, Expr, LocalDecl, Program, Signature, Stmt, Terminator, UnOp,
};
pub(crate) use self::lean_terms::comment_text;
use self::lean_terms::{lean_bool, lean_ident_suffix, lean_list, lean_str};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Width {
W8,
W16,
W32,
W64,
W128,
}
impl Width {
pub fn from_bits(bits: u64) -> Option<Width> {
Some(match bits {
8 => Width::W8,
16 => Width::W16,
32 => Width::W32,
64 => Width::W64,
128 => Width::W128,
_ => return None,
})
}
pub fn bits(self) -> u32 {
match self {
Width::W8 => 8,
Width::W16 => 16,
Width::W32 => 32,
Width::W64 => 64,
Width::W128 => 128,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Sign {
Signed,
Unsigned,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SignedOverflowMode {
IsoUB,
Wrapv,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AbiAlignTable {
pub i8_align_log2: u8,
pub i16_align_log2: u8,
pub i32_align_log2: u8,
pub i64_align_log2: u8,
pub i128_align_log2: u8,
pub ptr_align_log2: u8,
pub aggregate_align_log2: u8,
}
impl AbiAlignTable {
/// Natural LP64 ABI alignments (`i8:1`, `i16:2`, `i32:4`, `i64:8`,
/// `i128:16`, `ptr:8`, `agg:1`).
pub const LP64_NATURAL: Self = Self {
i8_align_log2: 0,
i16_align_log2: 1,
i32_align_log2: 2,
i64_align_log2: 3,
i128_align_log2: 4,
ptr_align_log2: 3,
aggregate_align_log2: 0,
};
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LangPolicy {
pub pointer_width_bits: u64,
pub char_is_signed: bool,
pub strict_aliasing: bool,
pub signed_overflow: SignedOverflowMode,
pub exceptions_off: bool,
pub little_endian: bool,
pub int_aligns: AbiAlignTable,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Ty {
Void,
Bool,
Int(Width, Sign, bool),
Ptr(Box<Ty>, bool, bool, bool),
Array(Box<Ty>, u64),
Record(String),
}
impl Ty {
pub const I32: Ty = Ty::Int(Width::W32, Sign::Signed, false);
pub const U32: Ty = Ty::Int(Width::W32, Sign::Unsigned, false);
pub const I64: Ty = Ty::Int(Width::W64, Sign::Signed, false);
pub fn int_parts(&self) -> Option<(Width, Sign, bool)> {
match self {
Ty::Int(w, s, c) => Some((*w, *s, *c)),
_ => None,
}
}
pub fn pointee(&self) -> Option<&Ty> {
match self {
Ty::Ptr(p, ..) => Some(p),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FieldDecl {
pub name: String,
pub offset_bits: u64,
pub bit_width: Option<u64>,
pub ty: Ty,
pub is_const: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RecordDef {
pub record_id: String,
pub is_union: bool,
pub size_bytes: u64,
pub align_bytes: u64,
pub fields: Vec<FieldDecl>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Proj {
Deref,
Field(String, u32, Ty),
Index(Ty, usize),
/// Bit-field `(record, field index, offset bits, width, signed)`; last
/// projection only.
Bitfield(String, u32, u64, u64, bool),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Place {
pub base: usize,
pub projs: Vec<Proj>,
}
impl Place {
pub fn local(base: usize) -> Place {
Place { base, projs: vec![] }
}
pub fn with(mut self, p: Proj) -> Place {
self.projs.push(p);
self
}
}