Add struct SyntaxTable

This commit is contained in:
Guojie Luo 2022-04-24 09:29:53 +08:00
parent 6c87b4d5f7
commit 01e15ce040
13 changed files with 88 additions and 33 deletions

View File

@ -10,6 +10,9 @@ use std::{
result::Result,
};
#[allow(unused_imports)]
use log::{debug, error, info, trace, warn};
fn main() {
match main_inner() {
Ok(_) => (),
@ -82,7 +85,25 @@ fn main_inner() -> Result<(), std::io::Error> {
let module_list = DescriptionList::codegen(json_tree);
for (mut module, _json_module) in module_list {
for (mut module, syntax_table) in module_list {
for proc in module.processes() {
for inst in proc.all_insts() {
let v = proc.get_inst_result(inst);
let json = if v.is_some() {
syntax_table.values.get(&v.unwrap())
} else {
None
};
if json.is_some() {
trace!(
r#"inst "{}" from syntax node {}"#,
inst.dump(&proc),
json.unwrap()
);
}
}
}
let pass_ctx = PassContext;
llhd::pass::GlobalCommonSubexprElim::run_on_module(&pass_ctx, &mut module);
//llhd::pass::InstSimplification::run_on_module(&pass_ctx, &mut module);

View File

@ -12,7 +12,7 @@ use log::{debug, error, info, trace, warn};
pub struct AlwaysStatement {}
impl AlwaysStatement {
pub fn codegen(json: &JsonValue, context: &mut ModuleContext) -> Option<UnitId> {
pub fn codegen<'a>(json: &'a JsonValue, context: &mut ModuleContext<'a>) -> Option<UnitId> {
match json["children"][0]["tag"].as_str() {
Some(Tag::ALWAYS) => Some(Self::gen_always(json, context)),
Some(Tag::ALWAYS_COMB) => {
@ -25,7 +25,7 @@ impl AlwaysStatement {
}
}
fn gen_always(json: &JsonValue, context: &mut ModuleContext) -> UnitId {
fn gen_always<'a>(json: &'a JsonValue, context: &mut ModuleContext<'a>) -> UnitId {
let (json_event_expression_list, json_statement) = {
let json_always = Tools::match_tags(
vec![json],

View File

@ -8,7 +8,7 @@ use log::{debug, error, info, trace, warn};
pub struct CaseStatement {}
impl CaseStatement {
pub fn codegen(json: &JsonValue, context: &mut ModuleContext) -> Option<Block> {
pub fn codegen<'a>(json: &'a JsonValue, context: &mut ModuleContext<'a>) -> Option<Block> {
let (json_expression, json_case_items, json_default_statement) = Self::analyze(json);
if context.unit_ctx.data.is_none() {
@ -119,7 +119,7 @@ impl CaseStatement {
Some(bb_last)
}
fn visit_case_body(json: &JsonValue, context: &mut ModuleContext) -> Option<Block> {
fn visit_case_body<'a>(json: &'a JsonValue, context: &mut ModuleContext<'a>) -> Option<Block> {
let stmt = Statement::codegen(json, context);
if context.unit_ctx.data.is_some() {

View File

@ -8,7 +8,7 @@ use log::{debug, error, info, trace, warn};
pub struct ConditionalStatement {}
impl ConditionalStatement {
pub fn codegen(json: &JsonValue, context: &mut ModuleContext) -> Option<Block> {
pub fn codegen<'a>(json: &'a JsonValue, context: &mut ModuleContext<'a>) -> Option<Block> {
let (json_if_header, json_if_body, json_else_body) = Self::analyze(json);
if context.unit_ctx.data.is_none() {
@ -125,7 +125,7 @@ impl ConditionalStatement {
}
}
fn visit_ite_body(json: &JsonValue, context: &mut ModuleContext) -> Option<Block> {
fn visit_ite_body<'a>(json: &'a JsonValue, context: &mut ModuleContext<'a>) -> Option<Block> {
let json_child = &json["children"];
assert_eq!(json_child.len(), 1);
let stmt = Statement::codegen(&json_child[0], context);

View File

@ -1,3 +1,4 @@
use json::JsonValue;
use linked_hash_map::LinkedHashMap;
use linked_hash_set::LinkedHashSet;
use llhd::{
@ -7,11 +8,12 @@ use llhd::{
};
use std::{collections::HashMap, fmt};
pub struct ModuleContext {
pub struct ModuleContext<'a> {
pub name: String,
pub module: Module,
pub symbol: LinkedHashMap<String, SymbolInfo>,
pub unit_ctx: UnitContext,
pub syntax_table: SyntaxTable<'a>,
}
pub struct UnitContext {
@ -27,6 +29,11 @@ pub struct UnitContext {
pub ty_active: Option<Type>,
}
pub struct SyntaxTable<'a> {
pub root: &'a JsonValue,
pub values: HashMap<Value, &'a JsonValue>,
}
pub struct SymbolInfo {
pub kind: SymbolKind,
pub value: IntValue,
@ -53,13 +60,14 @@ impl fmt::Display for SymbolKind {
}
}
impl ModuleContext {
pub fn new() -> Self {
impl<'a> ModuleContext<'a> {
pub fn new(json: &'a JsonValue) -> Self {
Self {
name: String::new(),
module: Module::new(),
symbol: LinkedHashMap::new(),
unit_ctx: UnitContext::new(),
syntax_table: SyntaxTable::new(json),
}
}
}
@ -125,6 +133,15 @@ impl UnitContext {
}
}
impl<'a> SyntaxTable<'a> {
pub fn new(json: &'a JsonValue) -> Self {
Self {
root: json,
values: HashMap::new(),
}
}
}
impl SymbolInfo {
pub fn new(kind: SymbolKind, value: IntValue) -> Self {
Self {

View File

@ -1,17 +1,17 @@
use crate::cst::{ModuleDeclaration, Tag, Tools};
use crate::cst::{ModuleDeclaration, SyntaxTable, Tag, Tools};
use json::JsonValue;
use llhd::ir::Module;
pub struct DescriptionList {}
impl DescriptionList {
pub fn codegen(json: &JsonValue) -> Vec<(Module, &JsonValue)> {
pub fn codegen(json: &JsonValue) -> Vec<(Module, SyntaxTable)> {
Tools::match_tags(
vec![json],
vec![Tag::DESCRIPTION_LIST, Tag::MODULE_DECLARATION],
)
.iter()
.map(|&x| (ModuleDeclaration::codegen(x), x))
.map(|&x| ModuleDeclaration::codegen(x))
.collect()
}
}

View File

@ -15,7 +15,7 @@ impl Expression {
Self::gen_reference_call(json, context).0
}
pub fn codegen(json: &JsonValue, context: &mut ModuleContext) -> Option<Value> {
pub fn codegen<'a>(json: &'a JsonValue, context: &mut ModuleContext<'a>) -> Option<Value> {
match json["tag"].as_str() {
Some(Tag::EXPRESSION) => {
let json_expr = &json["children"];
@ -138,7 +138,10 @@ impl Expression {
}
}
fn gen_binary_expression(json: &JsonValue, context: &mut ModuleContext) -> Option<Value> {
fn gen_binary_expression<'a>(
json: &'a JsonValue,
context: &mut ModuleContext<'a>,
) -> Option<Value> {
let json_children = &json["children"];
assert_eq!(json_children.len(), 3);
let operand_l = Self::codegen(&json_children[0], context);
@ -157,7 +160,7 @@ impl Expression {
let opd_l = operand_l.unwrap();
let opd_r = operand_r.unwrap();
match json_children[1]["tag"].as_str() {
let value = match json_children[1]["tag"].as_str() {
Some("+") => {
let value = builder.ins().add(opd_l, opd_r);
Some(value)
@ -202,13 +205,20 @@ impl Expression {
None
}
_ => panic!("unknown error at CST node '{}'", json),
};
if value.is_some() {
context.syntax_table.values.insert(value.unwrap(), json);
}
value
} else {
None
}
}
fn gen_unary_prefix_expression(json: &JsonValue, context: &mut ModuleContext) -> Option<Value> {
fn gen_unary_prefix_expression<'a>(
json: &'a JsonValue,
context: &mut ModuleContext<'a>,
) -> Option<Value> {
let json_children = &json["children"];
assert_eq!(json_children.len(), 2);
let expr = Self::codegen(&json_children[1], context);
@ -244,9 +254,9 @@ impl Expression {
}
}
fn gen_concatenation_expression(
json: &JsonValue,
context: &mut ModuleContext,
fn gen_concatenation_expression<'a>(
json: &'a JsonValue,
context: &mut ModuleContext<'a>,
) -> Option<Value> {
let json_expressions = Tools::match_tags(
vec![json],
@ -294,7 +304,10 @@ impl Expression {
}
}
fn gen_conditional_expression(json: &JsonValue, context: &mut ModuleContext) -> Option<Value> {
fn gen_conditional_expression<'a>(
json: &'a JsonValue,
context: &mut ModuleContext<'a>,
) -> Option<Value> {
let json_children = &json["children"];
assert_eq!(json_children.len(), 5);

View File

@ -17,7 +17,7 @@ mod tools;
use always_statement::AlwaysStatement;
use case_statement::CaseStatement;
use conditional_statement::ConditionalStatement;
use context::{ModuleContext, SymbolInfo, SymbolKind, UnitContext};
use context::{ModuleContext, SymbolInfo, SymbolKind, SyntaxTable, UnitContext};
use expression::Expression;
use module_declaration::ModuleDeclaration;
use net_variable_assignment::NetVariableAssignment;

View File

@ -1,6 +1,6 @@
use crate::cst::{
AlwaysStatement, ModuleContext, NetVariableAssignment, SymbolDeclaration, SymbolKind, Tag,
Tools, UnitContext,
AlwaysStatement, ModuleContext, NetVariableAssignment, SymbolDeclaration, SymbolKind,
SyntaxTable, Tag, Tools, UnitContext,
};
use json::JsonValue;
use llhd::{
@ -15,8 +15,8 @@ use log::{debug, error, info, trace, warn};
pub struct ModuleDeclaration {}
impl ModuleDeclaration {
pub fn codegen(json: &JsonValue) -> Module {
let mut context = ModuleContext::new();
pub fn codegen(json: &JsonValue) -> (Module, SyntaxTable) {
let mut context = ModuleContext::new(json);
SymbolDeclaration::declare_port(json, &mut context);
SymbolDeclaration::declare_reg(json, &mut context);
@ -41,7 +41,7 @@ impl ModuleDeclaration {
context.module.dump()
);
context.module
(context.module, context.syntax_table)
}
fn gen_entity_data(json: &JsonValue, context: &mut ModuleContext) {
@ -131,7 +131,7 @@ impl ModuleDeclaration {
}
}
fn gen_assignment(json: &JsonValue, context: &mut ModuleContext) {
fn gen_assignment<'a>(json: &'a JsonValue, context: &mut ModuleContext<'a>) {
let json_wire_assignments = &Tools::match_tags(
vec![json],
vec![
@ -153,7 +153,11 @@ impl ModuleDeclaration {
context.unit_ctx.bb_head.pop();
}
fn gen_always(json: &JsonValue, entity_ctx: &mut UnitContext, context: &mut ModuleContext) {
fn gen_always<'a>(
json: &'a JsonValue,
entity_ctx: &mut UnitContext,
context: &mut ModuleContext<'a>,
) {
let json_always_statements = &Tools::match_tags(
vec![json],
vec![

View File

@ -11,7 +11,7 @@ use log::{debug, error, info, trace, warn};
pub struct NetVariableAssignment {}
impl NetVariableAssignment {
pub fn codegen(json: &JsonValue, context: &mut ModuleContext) -> Option<Block> {
pub fn codegen<'a>(json: &'a JsonValue, context: &mut ModuleContext<'a>) -> Option<Block> {
let (json_lpvalue, json_expression) = {
assert_eq!(json["tag"], Tag::NET_VARIABLE_ASSIGNMENT);

View File

@ -8,7 +8,7 @@ use log::{debug, error, info, trace, warn};
pub struct NonblockingAssignmentStatement {}
impl NonblockingAssignmentStatement {
pub fn codegen(json: &JsonValue, context: &mut ModuleContext) -> Option<Block> {
pub fn codegen<'a>(json: &'a JsonValue, context: &mut ModuleContext<'a>) -> Option<Block> {
let (json_lpvalue, json_expression) = {
assert_eq!(json["tag"], Tag::NONBLOCKING_ASSIGNMENT_STATEMENT);

View File

@ -5,7 +5,7 @@ use llhd::ir::Block;
pub struct SeqBlock {}
impl SeqBlock {
pub fn codegen(json: &JsonValue, context: &mut ModuleContext) -> Option<Block> {
pub fn codegen<'a>(json: &'a JsonValue, context: &mut ModuleContext<'a>) -> Option<Block> {
let json_block_item_statement_list = {
let json_vec = Tools::match_tags(
vec![json],

View File

@ -11,7 +11,7 @@ use log::{debug, error, info, trace, warn};
pub struct Statement {}
impl Statement {
pub fn codegen(json: &JsonValue, context: &mut ModuleContext) -> Option<Block> {
pub fn codegen<'a>(json: &'a JsonValue, context: &mut ModuleContext<'a>) -> Option<Block> {
match json["tag"].as_str() {
Some(Tag::SEQ_BLOCK) => SeqBlock::codegen(json, context),
Some(Tag::CONDITIONAL_STATEMENT) => ConditionalStatement::codegen(json, context),