open-vaf 0.4.2

A compiler frontend for VerilogA aimed predominently at compact modelling
Documentation
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
/*
 * ******************************************************************************************
 * Copyright (c) 2019 Pascal Kuthe. This file is part of the OpenVAF project.
 * It is subject to the license terms in the LICENSE file found in the top-level directory
 *  of this distribution and at  https://gitlab.com/DSPOM/OpenVAF/blob/master/LICENSE.
 *  No part of OpenVAF, including this file, may be copied, modified, propagated, or
 *  distributed except according to the terms contained in the LICENSE file.
 * *****************************************************************************************
 */

use log::*;

use crate::ast::{BranchAccess, ModuleItem, ParameterType};
use crate::ast_lowering::ast_to_hir_fold::expression::StatementExpressionFolder;
use crate::ast_lowering::ast_to_hir_fold::{
    DeclarationHandler, ExpressionFolder, Fold, VerilogContext,
};
use crate::ast_lowering::branch_resolution::BranchResolver;
use crate::ast_lowering::error::{Error, NotAllowedInFunction, Type};

use crate::hir::{Condition, Module, Statement};
use crate::ir::hir::{DisciplineAccess, Function, FunctionArg, WhileLoop};
use crate::ir::ids::IdRange;
use crate::ir::NumericalParameterRangeExclude;
use crate::ir::*;
use crate::parser::error::Unsupported;
use crate::symbol::Ident;
use crate::symbol_table::SymbolDeclaration;
use crate::{ast, Hir};
use std::mem::take;

/// The last fold folds all statements in textual order
pub struct Statements<'lt, H: DeclarationHandler> {
    pub(super) branch_resolver: BranchResolver,
    pub(super) state: VerilogContext,
    pub(super) base: Fold<'lt>,
    pub(super) declaration_handler: &'lt mut H,
}

impl<'lt, H: DeclarationHandler> Statements<'lt, H> {
    pub fn fold(mut self) -> Result<Hir, Vec<Error>> {
        for module in self.base.ast.modules.iter() {
            self.base
                .resolver
                .enter_scope(&module.contents.symbol_table);

            for decl in module.contents.symbol_table.values().copied() {
                match decl {
                    SymbolDeclaration::Nature(_)
                    | SymbolDeclaration::Module(_)
                    | SymbolDeclaration::Discipline(_) => {
                        unreachable_unchecked!("Parser cant create this")
                    }

                    SymbolDeclaration::Branch(_)
                    | SymbolDeclaration::Port(_)
                    | SymbolDeclaration::Net(_)
                    | SymbolDeclaration::Block(_) => (), //Have already been visited or will be visited later

                    SymbolDeclaration::Function(function) => self.fold_function(function),
                    SymbolDeclaration::Variable(variable) => {
                        self.state.insert(VerilogContext::CONSTANT);
                        self.fold_variable(variable);
                        self.state.remove(VerilogContext::CONSTANT);
                    }
                    SymbolDeclaration::Parameter(parameter) => {
                        self.state.insert(VerilogContext::CONSTANT);
                        self.fold_parameter(parameter);
                        self.state.remove(VerilogContext::CONSTANT);
                    }
                }
            }

            let analog_stmts_start = self.base.hir.statements.len_idx();

            for module_item in module.contents.children.iter() {
                match module_item {
                    ModuleItem::AnalogStmt(statement) => {
                        self.fold_statement(*statement);
                    }
                    ModuleItem::GenerateStatement => unimplemented!("Generate Statement"),
                }
            }

            self.base.resolver.exit_scope();
            self.base.hir.modules.push(module.map_with(|old| Module {
                name: old.name,
                port_list: old.port_list.clone(),
                analog: IdRange(analog_stmts_start..self.base.hir.statements.len_idx()),
            }));
        }

        if self.base.errors.is_empty() {
            Ok(self.base.hir)
        } else {
            Err(self.base.errors)
        }
    }

    pub fn fold_function(&mut self, id: FunctionId) {
        let function = &self.base.ast[id].contents;
        let args = function
            .args
            .iter()
            .filter_map(|arg| {
                if let Some(SymbolDeclaration::Variable(local_var)) =
                    function.declarations.get(&arg.name.name).copied()
                {
                    Some(FunctionArg {
                        local_var,
                        input: arg.input,
                        output: arg.output,
                    })
                } else {
                    self.base.errors.push(Error {
                        error_type: Type::TypeDeclarationMissing(arg.name.name),
                        source: arg.name.span,
                    });
                    None
                }
            })
            .collect();

        self.state.insert(VerilogContext::FUNCTION);
        self.base.resolver.enter_function(&function.declarations);

        for declaration in function.declarations.values().copied() {
            match declaration {
                SymbolDeclaration::Block(_) => { /*error will be generated upon encounter*/ }
                SymbolDeclaration::Variable(var) => self.fold_variable(var),
                SymbolDeclaration::Parameter(param) => self.fold_parameter(param),
                SymbolDeclaration::Module(_)
                | SymbolDeclaration::Branch(_)
                | SymbolDeclaration::Net(_)
                | SymbolDeclaration::Port(_)
                | SymbolDeclaration::Function(_)
                | SymbolDeclaration::Discipline(_)
                | SymbolDeclaration::Nature(_) => {
                    unreachable_unchecked!("Parser doesn't allow this")
                }
            }
        }

        self.fold_variable(function.return_variable);

        let start = self.base.hir.statements.len_idx();
        self.fold_statement(function.body);
        self.base.hir[id] = self.base.ast[id].map(Function {
            name: function.name,
            args,
            return_variable: function.return_variable,
            body: IdRange(start..self.base.hir.statements.len_idx()),
        });
        self.base.resolver.exit_function();
        self.state.remove(VerilogContext::FUNCTION);
    }

    /// Folds a statements. StatementIds are not stable because the amount of statements may change during this fold
    /// The way that Statement Blocks are stored also changes. Instead of an Vec<StatementId> we switch to a Range of StatementIds
    /// This is possible because this fold adds Statements in the order they are executed (conditions indicate themselves and their block as a statement before&after their block)
    /// As such this function doesn't return the new StatementId instead `empty_range_from_end` and `extend_range_to_end` are used to create the range of the folded block by the calle
    fn fold_statement(&mut self, id: StatementId) {
        match self.base.ast[id] {
            ast::Statement::Block(id) => {
                if let Some(scope) = &self.base.ast[id].contents.scope {
                    if self.state.contains(VerilogContext::FUNCTION) {
                        self.base.error(Error {
                            source: scope.name.span,
                            error_type: Type::NotAllowedInFunction(
                                NotAllowedInFunction::NamedBlocks,
                            ),
                        });
                    }

                    self.base.resolver.enter_scope(&scope.symbols);
                    self.state.insert(VerilogContext::CONSTANT);

                    for decl in scope.symbols.values().copied() {
                        match decl {
                            SymbolDeclaration::Nature(_) => unreachable_unchecked!("Natures can't be declared inside blocks so the parser won't ever place this here"),
                            SymbolDeclaration::Module(_)=>unreachable_unchecked!("Module cant be declared inside blocks so the parser won't ever place this here"),
                            SymbolDeclaration::Discipline(_) => unreachable_unchecked!("Discipline can't be declared inside blocks so the parser won't ever place this here"),
                            SymbolDeclaration::Function(_) => unreachable_unchecked!("Functions can't be declared inside blocks so the parser won't ever place this here"),
                            SymbolDeclaration::Branch(_) => unreachable_unchecked!("Functions can't be declared inside blocks so the parser won't ever place this here"),
                            SymbolDeclaration::Block(_) => (),//Blocs are visited when the appropriate statements are reached
                            SymbolDeclaration::Port(_) =>unreachable_unchecked!("Port can't be declared inside blocks so the parser won't ever place this here"),
                            SymbolDeclaration::Net(_) =>unreachable_unchecked!("Net( can't be declared inside blocks so the parser won't ever place this here"),
                            SymbolDeclaration::Variable(variableid) => {self.fold_variable(variableid);},
                            SymbolDeclaration::Parameter(parameter_id) => {self.fold_parameter(parameter_id);},
                        }
                    }

                    self.state.remove(VerilogContext::CONSTANT);
                    self.fold_block(id);
                    self.base.resolver.exit_scope();
                } else {
                    self.fold_block(id);
                }
            }

            ast::Statement::Condition(ref condition) => {
                let start = self.base.hir.statements.push(Statement::ConditionStart {
                    condition_info_and_end: id, /*just a place holder*/
                });
                if let Some(contents) = self.fold_condition(&condition.contents) {
                    let end = self
                        .base
                        .hir
                        .statements
                        .push(Statement::Condition(condition.map(contents)));
                    self.base.hir[start] = Statement::ConditionStart {
                        condition_info_and_end: end,
                    };
                }
            }

            ast::Statement::While(while_loop) => {
                let start = self.base.hir.statements.push(Statement::WhileStart {
                    while_info_and_start: id, //just a place holder
                });

                let condition = self.fold_expression(while_loop.contents.condition);
                let body_start = self.base.hir.statements.len_idx();
                self.fold_statement(while_loop.contents.body);
                if let Some(condition) = condition {
                    let end = self
                        .base
                        .hir
                        .statements
                        .push(Statement::While(while_loop.copy_as(WhileLoop {
                            condition,
                            body: IdRange(body_start..self.base.hir.statements.len_idx()),
                        })));
                    self.base.hir[start] = Statement::WhileStart {
                        while_info_and_start: end,
                    }
                }
            }

            ast::Statement::Assign(ref attr, ref ident, value) => {
                resolve_hierarchical!(self.base; ident as Variable(id) => {
                    if let Some(value) = self.fold_expression(value){
                        self.base.hir.statements.push(Statement::Assignment(*attr, id, value));
                    }
                })
            }

            ast::Statement::Contribute(attr, ref nature_name, ref branch, value) => {
                if self.state.contains(VerilogContext::FUNCTION) {
                    self.base.error(Error {
                        source: nature_name.span.extend(self.base.ast[value].source),
                        error_type: Type::NotAllowedInFunction(NotAllowedInFunction::Contribute),
                    });
                }

                if let Some((discipline_access, branch, value)) =
                    self.fold_contribute(nature_name, branch, value)
                {
                    self.base.hir.statements.push(Statement::Contribute(
                        attr,
                        discipline_access,
                        branch,
                        value,
                    ));
                }
            }

            ast::Statement::FunctionCall(attr, ref name, ref parameters) => {
                let parameters = parameters
                    .iter()
                    .copied()
                    .filter_map(|expr| self.fold_expression(expr))
                    .collect();

                resolve_hierarchical!(self.base; name as
                        Function(fid) => {
                            self.base.hir.statements.push(Statement::FunctionCall(attr,fid,parameters));
                    }
                )
            }
            ast::Statement::DisplayTask(_, _) => {
                // TODO DisplayTask
                warn!("All display task such as $display are currently ignored. Display tasks will be implemented in the future");
            }
        }
    }

    fn fold_contribute(
        &mut self,
        nature_name: &Ident,
        branch: &Node<BranchAccess>,
        value: ExpressionId,
    ) -> Option<(DisciplineAccess, BranchId, ExpressionId)> {
        let (branch, discipline) = self
            .branch_resolver
            .resolve_branch_access(&mut self.base, branch)?;

        let nature = self.branch_resolver.resolve_discipline_access(
            &mut self.base,
            nature_name,
            discipline,
        )?;

        let value = self.fold_expression(value)?;
        Some((nature, branch, value))
    }

    /// folds a condition/if statement
    fn fold_condition(&mut self, node: &ast::Condition) -> Option<Condition> {
        let condition = self.fold_expression(node.condition);

        let if_body_start = self.base.hir.statements.len_idx();

        self.fold_statement(node.if_statement);
        let main_condition_statements = IdRange(if_body_start..self.base.hir.statements.len_idx());

        let else_statements_start = self.base.hir.statements.len_idx();
        if let Some(statement) = node.else_statement {
            self.fold_statement(statement);
        }

        Some(Condition {
            condition: condition?,
            if_statements: main_condition_statements,
            else_statements: IdRange(else_statements_start..self.base.hir.statements.len_idx()),
        })
    }

    /// Just a utility method that makes folding expressions a little more ergonomic
    fn fold_expression(&mut self, expr: ExpressionId) -> Option<ExpressionId> {
        StatementExpressionFolder {
            state: self.state,
            branch_resolver: &mut self.branch_resolver,
        }
        .fold(expr, &mut self.base)
    }

    fn fold_block(&mut self, block: BlockId) {
        for statement in self.base.ast[block].contents.statements.iter().copied() {
            self.fold_statement(statement);
        }
    }

    /// Folds a variable
    /// This is just folds the default value if it exists and just copys thre rest
    fn fold_variable(&mut self, id: VariableId) {
        self.base.hir[id].contents.default_value = self.base.hir[id]
            .contents
            .default_value
            .and_then(|expr| self.fold_expression(expr));

        self.declaration_handler
            .handle_declaration(&mut self.base, SymbolDeclaration::Variable(id))
    }

    fn fold_parameter(&mut self, id: ParameterId) {
        if let Some(expr) = self.fold_expression(self.base.hir[id].contents.default_value) {
            self.base.hir[id].contents.default_value = expr
        }

        if let ParameterType::Numerical {
            parameter_type,
            ref mut from_ranges,
            ref mut excluded,
        } = self.base.hir[id].contents.parameter_type
        {
            let mut from_ranges = take(from_ranges);
            let mut excluded = take(excluded);

            for range in from_ranges.iter_mut() {
                if let Some(expr) = self.fold_expression(range.start.bound) {
                    range.start.bound = expr;
                }
                if let Some(expr) = self.fold_expression(range.end.bound) {
                    range.end.bound = expr;
                }
            }

            for exclude in excluded.iter_mut() {
                match exclude {
                    NumericalParameterRangeExclude::Value(val) => {
                        if let Some(expr) = self.fold_expression(*val) {
                            *val = expr;
                        }
                    }

                    NumericalParameterRangeExclude::Range(range) => {
                        if let Some(expr) = self.fold_expression(range.start.bound) {
                            range.start.bound = expr;
                        }
                        if let Some(expr) = self.fold_expression(range.end.bound) {
                            range.end.bound = expr;
                        }
                    }
                }
            }

            self.base.hir[id].contents.parameter_type = ParameterType::Numerical {
                parameter_type,
                excluded,
                from_ranges,
            }
        } else {
            self.base.error(Error {
                error_type: Type::Unsupported(Unsupported::StringParameters),
                source: self.base.ast[id].source,
            })
        }

        self.declaration_handler
            .handle_declaration(&mut self.base, SymbolDeclaration::Parameter(id))
    }
}