memea/lef.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
//! LEF file parsing and database creation for MemEA memory components.
//!
//! This module provides functionality to parse Library Exchange Format (LEF) files
//! and create component databases. It extracts cell dimensions from LEF files and
//! optionally augments them with enclosure data from corresponding GDS layout files.
//! The resulting data is saved as a component database for use in area estimation.
use dialoguer::Input;
use gds21::GdsLibrary;
use regex::Regex;
use std::fs::{metadata, File};
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use thiserror::Error;
use crate::{db::*, gds, FileCompleter, QueryDefault};
use crate::{errorln, query, vprintln, warnln, Float, MemeaError};
/// Errors that can occur during LEF file parsing.
#[derive(Debug, Error)]
pub enum LefError {
/// Indicates that a MACRO line in the LEF file is malformed.
#[error("Malformed MACRO line: {0}")]
InvalidMacro(String),
/// Indicates that a SIZE line in the LEF file cannot be parsed.
#[error("Malformed SIZE line: {0}")]
InvalidSize(String),
}
/// Interactively adds a cell to the database with user confirmation and type selection.
///
/// This function displays cell information to the user, asks for confirmation to add
/// the cell, and prompts for the cell type (core, switch, logic, or ADC). It handles
/// the interactive workflow for building a component database from LEF data.
///
/// # Arguments
/// * `name` - Name of the cell to add
/// * `dims` - Physical dimensions of the cell
/// * `db` - Mutable reference to the database to update
///
/// # Returns
/// * `Ok(())` - Cell was successfully processed (added or skipped)
/// * `Err(MemeaError)` - Error during user interaction or database update
fn add_cell(name: &str, dims: Dims, db: &mut Database) -> Result<(), MemeaError> {
println!("\nCell.......: {name}");
dims.dump();
println!();
// See if the user wants to add it
if !query(
&format!("Add cell {name} to database?"),
false,
QueryDefault::Yes,
)? {
return Ok(());
}
loop {
let mut celltype: String = prompt("Cell type");
celltype = celltype.trim().to_lowercase();
match celltype.as_str() {
"1" | "core" => {
db.add_core(name, dims);
break;
}
"2" | "switch" | "sw" => {
db.add_switch(name, dims);
break;
}
"3" | "logic" | "log" => {
db.add_logic(name, dims);
break;
}
"4" | "adc" => {
db.add_adc(name, dims);
break;
}
_ => {
errorln!(
"Invalid cell type (must be one of 1/core, 2/sw/switch, 3/log/logic, or 4/adc)"
);
}
}
}
println!("\n{}", crate::bar(None, '-'));
Ok(())
}
/// Interactive LEF file processing workflow.
///
/// This function provides an interactive command-line interface for processing
/// LEF files and creating component databases. It prompts the user for:
/// - GDS file (optional, for enclosure computation)
/// - LEF file (required, for cell dimensions)
/// - Output database file (YAML or JSON format)
///
/// # Arguments
/// * `verbose` - Whether to show detailed processing information
///
/// # Returns
/// * `Ok(())` - LEF processing completed successfully
/// * `Err(MemeaError)` - File I/O error, parsing error, or user interaction error
///
/// # Examples
/// ```no_run
/// use memea::lef::lefin;
///
/// // Start interactive LEF processing
/// lefin(true).expect("LEF processing failed");
/// ```
pub fn lefin(verbose: bool) -> Result<(), MemeaError> {
let mut gdsfile: String;
let mut leffile: String;
let mut dbout: String;
loop {
gdsfile = Input::new()
.with_prompt("GDS file")
.completion_with(&FileCompleter)
.interact_text()?;
let path = Path::new(&gdsfile);
if gdsfile.is_empty() {
warnln!("No GDS file provided; enclosures will not be computed.");
break;
} else if metadata(path).is_ok() && path.extension().and_then(|e| e.to_str()) == Some("gds")
{
break;
} else {
errorln!("{} is not a GDS file", gdsfile);
}
}
loop {
leffile = Input::new()
.with_prompt("LEF file")
.completion_with(&FileCompleter)
.interact_text()?;
let path = Path::new(&leffile);
if metadata(path).is_ok() && path.extension().and_then(|e| e.to_str()) == Some("lef") {
break;
} else {
errorln!("{} is not a LEF file", leffile);
}
}
loop {
dbout = Input::new()
.with_prompt("Output database file")
.completion_with(&FileCompleter)
.interact_text()?;
let valid = valid_ext(&dbout);
if valid && metadata(&dbout).is_ok() {
let allow = query(
format!("'{dbout}' already exists. Overwrite?").as_str(),
true,
crate::QueryDefault::Yes,
)?;
if allow {
break;
}
} else if valid {
break;
} else {
errorln!(
"Output database {} must be a YAML (.yml, .yaml) or JSON (.json) file",
dbout
);
}
}
println!();
let gdsin = if gdsfile.is_empty() {
None
} else {
Some(PathBuf::from(&gdsfile))
};
read_lef(PathBuf::from(leffile), gdsin, PathBuf::from(dbout), verbose)
}
/// Parses width and height from a LEF SIZE line using regex.
///
/// This function extracts two floating-point numbers from a SIZE line in a LEF file,
/// representing the width and height of a cell in micrometers.
///
/// # Arguments
/// * `line` - The SIZE line from the LEF file to parse
///
/// # Returns
/// * `Ok((width, height))` - Successfully parsed dimensions in micrometers
/// * `Err(LefError::InvalidSize)` - Line format is invalid or missing numbers
///
/// # Examples
/// ```
/// use memea::lef::parse_size;
///
/// let line = " SIZE 1.5 BY 2.0 ;";
/// let (w, h) = parse_size(line).expect("Failed to parse size");
/// assert_eq!((w, h), (1.5, 2.0));
/// ```
fn parse_size(line: &str) -> Result<(Float, Float), LefError> {
let re = Regex::new(r"([0-9]+\.?[0-9]*)").unwrap();
let mut nums = re
.captures_iter(line)
.filter_map(|cap| cap.get(1))
.filter_map(|m| m.as_str().parse::<Float>().ok());
match (nums.next(), nums.next()) {
(Some(a), Some(b)) => Ok((a, b)),
_ => Err(LefError::InvalidSize(line.to_string())),
}
}
/// Reads and processes a LEF file to create a component database.
///
/// This function parses a LEF file line by line, extracting MACRO names and SIZE
/// information to build component dimensions. If a GDS file is provided, it augments
/// the dimensions with enclosure data computed from the layout geometry.
///
/// # Arguments
/// * `lefin` - Path to the input LEF file
/// * `gdsin` - Optional path to GDS file for enclosure computation
/// * `dbout` - Path where the output database should be saved
/// * `verbose` - Whether to show detailed processing information
///
/// # Returns
/// * `Ok(())` - LEF file processed and database saved successfully
/// * `Err(MemeaError)` - File I/O error, parsing error, or database save error
///
/// # LEF File Format
/// The function expects LEF files with MACRO definitions containing SIZE lines:
/// ```text
/// MACRO cell_name
/// SIZE width BY height ;
/// END cell_name
/// ```
fn read_lef(
lefin: PathBuf,
gdsin: Option<PathBuf>,
dbout: PathBuf,
verbose: bool,
) -> Result<(), MemeaError> {
let lefin = File::open(lefin)?;
let rdr = BufReader::new(lefin);
// TODO: Currently assuming microns for LEF, need to scale this by LEF unit scale
let mut gdsunits = 1e-9;
let map = match gdsin {
Some(file) => {
let lib = GdsLibrary::load(&file)?;
gdsunits = lib.units.db_unit();
vprintln!(
verbose,
"GDS library {} loaded, found {} cells",
file.to_string_lossy(),
lib.structs.len()
);
Some(gds::hash_lib(lib))
}
None => None,
};
let mut name: String = String::new();
let mut dims: Option<Dims> = None;
let mut db = Database::new();
println!("Cell types: 1/core, 2/sw/switch, 3/log/logic, or 4/adc\n");
println!("{}", crate::bar(None, '-'));
for line in rdr.lines() {
let line = line?;
let line = line.trim();
if line.contains("MACRO") {
// Push previous cell
if let Some(c) = dims.take() {
add_cell(&name, c, &mut db)?;
}
// Get new cell name
let n = line
.split_once(' ')
.ok_or(LefError::InvalidMacro(line.to_owned()))?
.1;
name = n.to_string();
}
if line.contains("SIZE") {
// Get size
let (w, h) = parse_size(line)?;
dims = match &map {
Some(m) => Some(gds::augment_dims(m, &name, w, h, gdsunits, verbose)?),
None => Some(Dims::from(w, h, 0.0, 0.0)),
}
}
}
// Push last cell
if let Some(c) = dims {
add_cell(&name, c, &mut db)?;
println!();
}
// Write database to file
db.save(&dbout, verbose)?;
Ok(())
}