use crate::namespace::Namespace;
use crate::symbol::{
ClockDomain, Direction, DocComment, Port, PortProperty, Symbol, SymbolKind,
SystemFuncitonProperty, Type, TypeKind,
};
use crate::symbol_table::SymbolTable;
use veryl_parser::veryl_token::{Token, TokenSource, VerylToken};
pub struct SvSystemFunction {
pub name: String,
pub ports: Vec<(String, Direction)>,
}
impl SvSystemFunction {
pub fn new(name: &str, ports: &[(&str, Direction)]) -> Self {
Self {
name: name.to_string(),
ports: ports
.iter()
.map(|(name, direction)| (name.to_string(), *direction))
.collect(),
}
}
}
pub fn insert_symbols(symbol_table: &mut SymbolTable, namespace: &Namespace) {
let mut namespace = namespace.clone();
for func in sv_system_functions() {
let token = Token::new(&func.name, 0, 0, 0, 0, TokenSource::Builtin);
let mut ports = Vec::new();
namespace.push(token.text);
for (name, direction) in &func.ports {
let token = Token::new(name, 0, 0, 0, 0, TokenSource::Builtin);
let r#type = Type {
modifier: vec![],
kind: TypeKind::Any,
width: vec![],
array: vec![],
array_type: None,
is_const: false,
};
let property = PortProperty {
token,
r#type,
direction: *direction,
prefix: None,
suffix: None,
clock_domain: ClockDomain::None,
default_value: None,
is_proto: false,
};
let symbol = Symbol::new(
&token,
SymbolKind::Port(property),
&namespace,
false,
DocComment::default(),
);
if let Some(id) = symbol_table.insert(&token, symbol) {
let port = Port {
token: VerylToken::new(token),
symbol: id,
};
ports.push(port);
}
}
namespace.pop();
let property = SystemFuncitonProperty { ports };
let symbol = Symbol::new(
&token,
SymbolKind::SystemFunction(property),
&namespace,
false,
DocComment::default(),
);
let _ = symbol_table.insert(&token, symbol);
}
}
pub fn sv_system_functions() -> Vec<SvSystemFunction> {
vec![
SvSystemFunction::new(
"$stop",
&[],
),
SvSystemFunction::new(
"$finish",
&[],
),
SvSystemFunction::new("$exit", &[]),
SvSystemFunction::new("$time", &[]),
SvSystemFunction::new("$stime", &[]),
SvSystemFunction::new("$realtime", &[]),
SvSystemFunction::new(
"$timeunit",
&[],
),
SvSystemFunction::new(
"$timeprecision",
&[],
),
SvSystemFunction::new(
"$printtimescale",
&[],
),
SvSystemFunction::new(
"$timeformat",
&[],
),
SvSystemFunction::new("$rtoi", &[("real_val", Direction::Input)]),
SvSystemFunction::new("$itor", &[("int_val", Direction::Input)]),
SvSystemFunction::new("$realtobits", &[("real_val", Direction::Input)]),
SvSystemFunction::new("$bitstoreal", &[("bit_val", Direction::Input)]),
SvSystemFunction::new("$shortrealtobits", &[("shortreal_val", Direction::Input)]),
SvSystemFunction::new("$bitstoshortreal", &[("bit_val", Direction::Input)]),
SvSystemFunction::new("$signed", &[("val", Direction::Input)]),
SvSystemFunction::new("$unsigned", &[("val", Direction::Input)]),
SvSystemFunction::new(
"$cast",
&[
("dest_variable", Direction::Output),
("source_expression", Direction::Input),
],
),
SvSystemFunction::new(
"$typename",
&[("expression_or_data_type", Direction::Input)],
),
SvSystemFunction::new("$bits", &[("expression_or_data_type", Direction::Input)]),
SvSystemFunction::new("$isunbounded", &[("identifier", Direction::Input)]),
SvSystemFunction::new(
"$dimensions",
&[("expression_or_data_type", Direction::Input)],
),
SvSystemFunction::new(
"$unpacked_dimensions",
&[("expression_or_data_type", Direction::Input)],
),
SvSystemFunction::new("$left", &[("expression_or_data_type", Direction::Input)]),
SvSystemFunction::new("$right", &[("expression_or_data_type", Direction::Input)]),
SvSystemFunction::new("$low", &[("expression_or_data_type", Direction::Input)]),
SvSystemFunction::new("$high", &[("expression_or_data_type", Direction::Input)]),
SvSystemFunction::new(
"$increment",
&[("expression_or_data_type", Direction::Input)],
),
SvSystemFunction::new("$size", &[("expression_or_data_type", Direction::Input)]),
SvSystemFunction::new("$clog2", &[("n", Direction::Input)]),
SvSystemFunction::new("$ln", &[("x", Direction::Input)]),
SvSystemFunction::new("$log10", &[("x", Direction::Input)]),
SvSystemFunction::new("$exp", &[("x", Direction::Input)]),
SvSystemFunction::new("$sqrt", &[("x", Direction::Input)]),
SvSystemFunction::new("$pow", &[("x", Direction::Input), ("y", Direction::Input)]),
SvSystemFunction::new("$floor", &[("x", Direction::Input)]),
SvSystemFunction::new("$ceil", &[("x", Direction::Input)]),
SvSystemFunction::new("$sin", &[("x", Direction::Input)]),
SvSystemFunction::new("$cos", &[("x", Direction::Input)]),
SvSystemFunction::new("$tan", &[("x", Direction::Input)]),
SvSystemFunction::new("$asin", &[("x", Direction::Input)]),
SvSystemFunction::new("$acos", &[("x", Direction::Input)]),
SvSystemFunction::new("$atan", &[("x", Direction::Input)]),
SvSystemFunction::new(
"$atan2",
&[("x", Direction::Input), ("y", Direction::Input)],
),
SvSystemFunction::new(
"$hypot",
&[("x", Direction::Input), ("y", Direction::Input)],
),
SvSystemFunction::new("$sinh", &[("x", Direction::Input)]),
SvSystemFunction::new("$cosh", &[("x", Direction::Input)]),
SvSystemFunction::new("$tanh", &[("x", Direction::Input)]),
SvSystemFunction::new("$asinh", &[("x", Direction::Input)]),
SvSystemFunction::new("$acosh", &[("x", Direction::Input)]),
SvSystemFunction::new("$atanh", &[("x", Direction::Input)]),
SvSystemFunction::new(
"$countbits",
&[
("expression", Direction::Input),
("control_bit", Direction::Input),
],
),
SvSystemFunction::new("$countones", &[("expression", Direction::Input)]),
SvSystemFunction::new("$onehot", &[("expression", Direction::Input)]),
SvSystemFunction::new("$onehot0", &[("expression", Direction::Input)]),
SvSystemFunction::new("$isunknown", &[("expression", Direction::Input)]),
SvSystemFunction::new(
"$fatal",
&[],
),
SvSystemFunction::new(
"$error",
&[],
),
SvSystemFunction::new(
"$warning",
&[],
),
SvSystemFunction::new(
"$info",
&[],
),
SvSystemFunction::new(
"$asserton",
&[],
),
SvSystemFunction::new(
"$assertoff",
&[],
),
SvSystemFunction::new(
"$assertkill",
&[],
),
SvSystemFunction::new(
"$assertpasson",
&[],
),
SvSystemFunction::new(
"$assertpassoff",
&[],
),
SvSystemFunction::new(
"$assertfailon",
&[],
),
SvSystemFunction::new(
"$assertfailoff",
&[],
),
SvSystemFunction::new(
"$assertnonvacuouson",
&[],
),
SvSystemFunction::new(
"$assertvacuousoff",
&[],
),
SvSystemFunction::new(
"$assertcontrol",
&[("control_type", Direction::Input)],
),
SvSystemFunction::new("$sampled", &[("expression", Direction::Input)]),
SvSystemFunction::new(
"$rose",
&[("expression", Direction::Input)],
),
SvSystemFunction::new(
"$fell",
&[("expression", Direction::Input)],
),
SvSystemFunction::new(
"$stable",
&[("expression", Direction::Input)],
),
SvSystemFunction::new(
"$changed",
&[("expression", Direction::Input)],
),
SvSystemFunction::new(
"$past",
&[("expression", Direction::Input)],
),
SvSystemFunction::new("$past_gclk", &[("expression", Direction::Input)]),
SvSystemFunction::new("$rose_gclk", &[("expression", Direction::Input)]),
SvSystemFunction::new("$fell_gclk", &[("expression", Direction::Input)]),
SvSystemFunction::new("$stable_gclk", &[("expression", Direction::Input)]),
SvSystemFunction::new("$changed_gclk", &[("expression", Direction::Input)]),
SvSystemFunction::new("$future_gclk", &[("expression", Direction::Input)]),
SvSystemFunction::new("$rising_gclk", &[("expression", Direction::Input)]),
SvSystemFunction::new("$falling_gclk", &[("expression", Direction::Input)]),
SvSystemFunction::new("$steady_gclk", &[("expression", Direction::Input)]),
SvSystemFunction::new("$changing_gclk", &[("expression", Direction::Input)]),
SvSystemFunction::new(
"$coverage_control",
&[
("control_constant,", Direction::Input),
("coverage_type", Direction::Input),
("scope_def", Direction::Input),
("modules_or_instance,", Direction::Input),
],
),
SvSystemFunction::new(
"$coverage_get_max",
&[
("coverage_type", Direction::Input),
("scope_def", Direction::Input),
("modules_or_instance,", Direction::Input),
],
),
SvSystemFunction::new(
"$coverage_get",
&[
("coverage_type", Direction::Input),
("scope_def", Direction::Input),
("modules_or_instance,", Direction::Input),
],
),
SvSystemFunction::new(
"$coverage_merge",
&[
("coverage_type", Direction::Input),
("name", Direction::Input),
],
),
SvSystemFunction::new(
"$coverage_save",
&[
("coverage_type", Direction::Input),
("name", Direction::Input),
],
),
SvSystemFunction::new("$set_coverage_db_name", &[("filename", Direction::Input)]),
SvSystemFunction::new("$load_coverage_db", &[("filename", Direction::Input)]),
SvSystemFunction::new("$get_coverage", &[]),
SvSystemFunction::new(
"$random",
&[],
),
SvSystemFunction::new(
"$urandom",
&[],
),
SvSystemFunction::new(
"$random_range",
&[("maxval", Direction::Input)],
),
SvSystemFunction::new(
"$dist_uniform",
&[
("seed", Direction::Input),
("start", Direction::Input),
("end", Direction::Input),
],
),
SvSystemFunction::new(
"$dist_normal",
&[
("seed", Direction::Input),
("mean", Direction::Input),
("standard_deviation", Direction::Input),
],
),
SvSystemFunction::new(
"$dist_exponential",
&[("seed", Direction::Input), ("mean", Direction::Input)],
),
SvSystemFunction::new(
"$dist_poisson",
&[("seed", Direction::Input), ("mean", Direction::Input)],
),
SvSystemFunction::new(
"$dist_chi_square",
&[
("seed", Direction::Input),
("degree_of_freedom", Direction::Input),
],
),
SvSystemFunction::new(
"$dist_t",
&[
("seed", Direction::Input),
("degree_of_freedom", Direction::Input),
],
),
SvSystemFunction::new(
"$dist_erlang",
&[
("seed", Direction::Input),
("k_stage", Direction::Input),
("mean", Direction::Input),
],
),
SvSystemFunction::new(
"$q_initialize",
&[
("q_id", Direction::Input),
("q_type", Direction::Input),
("max_length", Direction::Input),
("status", Direction::Output),
],
),
SvSystemFunction::new(
"$q_add",
&[
("q_id", Direction::Input),
("job_id", Direction::Input),
("inform_id", Direction::Input),
("status", Direction::Output),
],
),
SvSystemFunction::new(
"$q_remove",
&[
("q_id", Direction::Input),
("job_id", Direction::Input),
("inform_id", Direction::Input),
("status", Direction::Output),
],
),
SvSystemFunction::new(
"$q_full",
&[("q_id", Direction::Input), ("status", Direction::Output)],
),
SvSystemFunction::new(
"$q_exam",
&[
("q_id", Direction::Input),
("q_stat_code", Direction::Input),
("q_stat_value", Direction::Input),
("status", Direction::Output),
],
),
SvSystemFunction::new(
"$async$and$array",
&[
("memory_identifier", Direction::Input),
("input_terms", Direction::Input),
("output_terms", Direction::Output),
],
),
SvSystemFunction::new(
"$sync$and$array",
&[
("memory_identifier", Direction::Input),
("input_terms", Direction::Input),
("output_terms", Direction::Output),
],
),
SvSystemFunction::new(
"$async$and$plane",
&[
("memory_identifier", Direction::Input),
("input_terms", Direction::Input),
("output_terms", Direction::Output),
],
),
SvSystemFunction::new(
"$sync$and$plane",
&[
("memory_identifier", Direction::Input),
("input_terms", Direction::Input),
("output_terms", Direction::Output),
],
),
SvSystemFunction::new(
"$async$nand$array",
&[
("memory_identifier", Direction::Input),
("input_terms", Direction::Input),
("output_terms", Direction::Output),
],
),
SvSystemFunction::new(
"$sync$nand$array",
&[
("memory_identifier", Direction::Input),
("input_terms", Direction::Input),
("output_terms", Direction::Output),
],
),
SvSystemFunction::new(
"$async$nand$plane",
&[
("memory_identifier", Direction::Input),
("input_terms", Direction::Input),
("output_terms", Direction::Output),
],
),
SvSystemFunction::new(
"$sync$nand$plane",
&[
("memory_identifier", Direction::Input),
("input_terms", Direction::Input),
("output_terms", Direction::Output),
],
),
SvSystemFunction::new(
"$async$or$array",
&[
("memory_identifier", Direction::Input),
("input_terms", Direction::Input),
("output_terms", Direction::Output),
],
),
SvSystemFunction::new(
"$sync$or$array",
&[
("memory_identifier", Direction::Input),
("input_terms", Direction::Input),
("output_terms", Direction::Output),
],
),
SvSystemFunction::new(
"$async$or$plane",
&[
("memory_identifier", Direction::Input),
("input_terms", Direction::Input),
("output_terms", Direction::Output),
],
),
SvSystemFunction::new(
"$sync$or$plane",
&[
("memory_identifier", Direction::Input),
("input_terms", Direction::Input),
("output_terms", Direction::Output),
],
),
SvSystemFunction::new(
"$async$nor$array",
&[
("memory_identifier", Direction::Input),
("input_terms", Direction::Input),
("output_terms", Direction::Output),
],
),
SvSystemFunction::new(
"$sync$nor$array",
&[
("memory_identifier", Direction::Input),
("input_terms", Direction::Input),
("output_terms", Direction::Output),
],
),
SvSystemFunction::new(
"$async$nor$plane",
&[
("memory_identifier", Direction::Input),
("input_terms", Direction::Input),
("output_terms", Direction::Output),
],
),
SvSystemFunction::new(
"$sync$nor$plane",
&[
("memory_identifier", Direction::Input),
("input_terms", Direction::Input),
("output_terms", Direction::Output),
],
),
SvSystemFunction::new("$system", &[("terminal_command_line", Direction::Input)]),
SvSystemFunction::new("$stacktrace;", &[]),
SvSystemFunction::new(
"$display",
&[],
),
SvSystemFunction::new(
"$displayb",
&[],
),
SvSystemFunction::new(
"$displayo",
&[],
),
SvSystemFunction::new(
"$displayh",
&[],
),
SvSystemFunction::new(
"$write",
&[],
),
SvSystemFunction::new(
"$writeb",
&[],
),
SvSystemFunction::new(
"$writeo",
&[],
),
SvSystemFunction::new(
"$writeh",
&[],
),
SvSystemFunction::new(
"$strobe",
&[],
),
SvSystemFunction::new(
"$strobeb",
&[],
),
SvSystemFunction::new(
"$strobeo",
&[],
),
SvSystemFunction::new(
"$strobeh",
&[],
),
SvSystemFunction::new(
"$monitor",
&[],
),
SvSystemFunction::new(
"$monitorb",
&[],
),
SvSystemFunction::new(
"$monitoro",
&[],
),
SvSystemFunction::new(
"$monitorh",
&[],
),
SvSystemFunction::new("$monitoron", &[]),
SvSystemFunction::new("$monitoroff", &[]),
SvSystemFunction::new(
"$fopen",
&[("filename", Direction::Input)],
),
SvSystemFunction::new("$fclose", &[("fd", Direction::Input)]),
SvSystemFunction::new(
"$fdisplay",
&[("fd", Direction::Input)],
),
SvSystemFunction::new(
"$fdisplayb",
&[("fd", Direction::Input)],
),
SvSystemFunction::new(
"$fdisplayo",
&[("fd", Direction::Input)],
),
SvSystemFunction::new(
"$fdisplayh",
&[("fd", Direction::Input)],
),
SvSystemFunction::new(
"$fwrite",
&[("fd", Direction::Input)],
),
SvSystemFunction::new(
"$fwriteb",
&[("fd", Direction::Input)],
),
SvSystemFunction::new(
"$fwriteo",
&[("fd", Direction::Input)],
),
SvSystemFunction::new(
"$fwriteh",
&[("fd", Direction::Input)],
),
SvSystemFunction::new(
"$fstrobe",
&[("fd", Direction::Input)],
),
SvSystemFunction::new(
"$fstrobeb",
&[("fd", Direction::Input)],
),
SvSystemFunction::new(
"$fstrobeo",
&[("fd", Direction::Input)],
),
SvSystemFunction::new(
"$fstrobeh",
&[("fd", Direction::Input)],
),
SvSystemFunction::new(
"$fmonitor",
&[("fd", Direction::Input)],
),
SvSystemFunction::new(
"$fmonitorb",
&[("fd", Direction::Input)],
),
SvSystemFunction::new(
"$fmonitoro",
&[("fd", Direction::Input)],
),
SvSystemFunction::new(
"$fmonitorh",
&[("fd", Direction::Input)],
),
SvSystemFunction::new(
"$swrite",
&[("output_var", Direction::Output)],
),
SvSystemFunction::new(
"$swriteb",
&[("output_var", Direction::Output)],
),
SvSystemFunction::new(
"$swriteo",
&[("output_var", Direction::Output)],
),
SvSystemFunction::new(
"$swriteh",
&[("output_var", Direction::Output)],
),
SvSystemFunction::new(
"$sformat",
&[
("output_var", Direction::Output),
("format_string", Direction::Input),
],
),
SvSystemFunction::new(
"$sformatf",
&[("format_string", Direction::Input)],
),
SvSystemFunction::new("$fgetc", &[("fd", Direction::Input)]),
SvSystemFunction::new(
"$ungetc",
&[("c", Direction::Output), ("fd", Direction::Input)],
),
SvSystemFunction::new(
"$fgets",
&[("str", Direction::Output), ("fd", Direction::Input)],
),
SvSystemFunction::new(
"$fscanf",
&[("fd", Direction::Input), ("format", Direction::Input)],
),
SvSystemFunction::new(
"$sscanf",
&[("str", Direction::Input), ("format", Direction::Input)],
),
SvSystemFunction::new(
"$fread",
&[
("integral_va_or_memory", Direction::Input),
("fd", Direction::Input),
],
),
SvSystemFunction::new("$ftell", &[("fd", Direction::Input)]),
SvSystemFunction::new(
"$fseek",
&[
("fd", Direction::Input),
("offset", Direction::Input),
("operation", Direction::Input),
],
),
SvSystemFunction::new("$rewind", &[("fd", Direction::Input)]),
SvSystemFunction::new(
"$fflush",
&[],
),
SvSystemFunction::new(
"$ferror",
&[("fd", Direction::Input), ("str", Direction::Output)],
),
SvSystemFunction::new("$feof", &[("fd", Direction::Input)]),
SvSystemFunction::new(
"$readmemb",
&[
("filename", Direction::Input),
("memory_name", Direction::Output),
],
),
SvSystemFunction::new(
"$readmemh",
&[
("filename", Direction::Input),
("memory_name", Direction::Output),
],
),
SvSystemFunction::new(
"$writememb",
&[
("filename", Direction::Input),
("memory_name", Direction::Input),
],
),
SvSystemFunction::new(
"$writememh",
&[
("filename", Direction::Input),
("memory_name", Direction::Input),
],
),
SvSystemFunction::new("$test$plusargs", &[("string", Direction::Input)]),
SvSystemFunction::new(
"$value$plusargs",
&[
("user_string", Direction::Input),
("variable", Direction::Output),
],
),
SvSystemFunction::new("$dumpfile", &[("filename", Direction::Input)]),
SvSystemFunction::new(
"$dumpvars",
&[],
),
SvSystemFunction::new("$dumpoff", &[]),
SvSystemFunction::new("$dumpon", &[]),
SvSystemFunction::new("$dumpall", &[]),
SvSystemFunction::new("$dumplimit", &[("filesize", Direction::Input)]),
SvSystemFunction::new("$dumpflush", &[]),
SvSystemFunction::new(
"$dumpports",
&[],
),
SvSystemFunction::new("$dumpportsoff", &[("filename", Direction::Input)]),
SvSystemFunction::new("$dumpportson", &[("filename", Direction::Input)]),
SvSystemFunction::new("$dumpportsall", &[("filename", Direction::Input)]),
SvSystemFunction::new(
"$dumpportslimit",
&[
("filelimit", Direction::Input),
("filename", Direction::Input),
],
),
SvSystemFunction::new("$dumpportsflush", &[("filename", Direction::Input)]),
]
}