Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / tools / importer / vhdl_ast.ml @ 62b6a61c

History | View | Annotate | Download (13.3 KB)

1
let base_types = ["integer"; "character"; "bit"; "real"; "natural"; "positive"; "std_logic"; "std_logic_vector" ]
2
 
3
(************************************************************************************)		   
4
(*                     Constants                                                    *)
5
(************************************************************************************)		   
6

    
7
(* Std_logic values :
8
    'U': uninitialized. This signal hasn't been set yet.
9
    'X': unknown. Impossible to determine this value/result.
10
    '0': logic 0
11
    '1': logic 1
12
    'Z': High Impedance
13
    'W': Weak signal, can't tell if it should be 0 or 1.
14
    'L': Weak signal that should probably go to 0
15
    'H': Weak signal that should probably go to 1
16
    '-': Don't care. *)			       
17
let std_logic_cst = ["U"; "X"; "0"; "1"; "Z"; "W"; "L"; "H"; "-" ]
18
let literal_base = ["B"; "O"; "X"; "UB"; "UO"; "UX"; "SB"; "SO"; "SX"; "D"] (* Prefix of CstLiteral *)
19

    
20
(* TODO: do we need more constructors ? *)
21
type cst_val_t = 
22
    CstInt of int 
23
  | CstStdLogic of string
24
  | CstLiteral of string [@name "CST_LITERAL"]
25
[@@deriving yojson {strict = false}];;
26

    
27
type vhdl_type_t =
28
  | Base of string
29
  | Range of string option * int * int
30
  | Bit_vector of int * int
31
  | Array of int * int * vhdl_type_t
32
  | Enumerated of string list
33
  | Void
34
and vhdl_subtype_indication_t =
35
  {
36
    name : vhdl_name_t [@default NoName];
37
    functionName : vhdl_name_t [@default NoName];
38
    const: vhdl_constraint_t [@default NoConstraint];
39
  }
40
and vhdl_discrete_range_t =
41
  | SubDiscreteRange of vhdl_subtype_indication_t [@name "SUB_DISCRETE_RANGE"]
42
  | NamedRange of vhdl_name_t [@name "NAMED_RANGE"]
43
  | DirectedRange of { direction: string; from: vhdl_expr_t; _to: vhdl_expr_t } [@name "RANGE_WITH_DIRECTION"]
44
and vhdl_constraint_t =
45
  | RefConstraint of { ref_name: vhdl_name_t; }
46
  | RangeConstraint of { range: vhdl_discrete_range_t } [@name "RANGE_CONSTRAINT"]
47
  | IndexConstraint of { ranges: vhdl_discrete_range_t list; } [@name "INDEX_CONSTRAINT"]
48
  | ArrayConstraint of { ranges: vhdl_discrete_range_t list; sub: vhdl_constraint_t } [@name "ARRAY_CONSTRAINT"]
49
  | RecordConstraint
50
  | NoConstraint
51
and vhdl_definition_t =
52
  | Type of {name : vhdl_name_t ; definition: vhdl_type_t} [@name "TYPE_DECLARATION"]
53
  | Subtype of {name : vhdl_name_t ; typ : vhdl_subtype_indication_t} [@name "SUBTYPE_DECLARATION"]
54
and vhdl_expr_t =
55
  | Call of vhdl_name_t [@name "CALL"]
56
  | Cst of cst_val_t [@name "CONSTANT_VALUE"]
57
  | Op of { id: string [@default ""]; args: vhdl_expr_t list [@default []]} [@name "EXPRESSION"]
58
  | IsNull [@name "IsNull"]
59
  | Time of { value: int; phy_unit: string [@default ""]}
60
  | Sig of { name: vhdl_name_t; att: vhdl_signal_attributes_t option }
61
  | SuffixMod of { expr : vhdl_expr_t; selection : suffix_selection_t }
62
  | Aggregate of { elems : vhdl_element_assoc_t list } [@name "AGGREGATE"]
63
  | Others [@name "OTHERS"]
64
and vhdl_name_t =
65
  | Simple of string [@name "SIMPLE_NAME"]
66
  | Identifier of string [@name "IDENTIFIER"]
67
  | Selected of vhdl_name_t list [@name "SELECTED_NAME"]
68
  | Index of { id: vhdl_name_t; exprs: vhdl_expr_t list } [@name "INDEXED_NAME"]
69
  | Slice of { id: vhdl_name_t; range: vhdl_discrete_range_t } [@name "SLICE_NAME"]
70
  | Attribute of { id: vhdl_name_t; designator: vhdl_name_t; expr: vhdl_expr_t [@default IsNull]} [@name "ATTRIBUTE_NAME"]
71
  | Function of { id: vhdl_name_t; assoc_list: vhdl_assoc_element_t list } [@name "FUNCTION_CALL"]
72
  | NoName
73
and vhdl_assoc_element_t =
74
  {
75
    formal_name: vhdl_name_t option [@default Some NoName];
76
    formal_arg: vhdl_name_t option [@default Some NoName];
77
    actual_name: vhdl_name_t option [@default Some NoName];
78
    actual_designator: vhdl_name_t option [@default Some NoName];
79
    actual_expr: vhdl_expr_t option [@default Some IsNull];
80
  }
81
and vhdl_element_assoc_t =
82
  {
83
    choices: vhdl_expr_t list;
84
    expr: vhdl_expr_t;
85
  }
86
and vhdl_array_attributes_t = 
87
  | AAttInt of { id: string; arg: int; } 
88
  | AAttAscending
89
and vhdl_signal_attributes_t = SigAtt of string
90
and vhdl_string_attributes_t = StringAtt of string
91
and suffix_selection_t = Idx of int | SuffixRange of int * int
92
[@@deriving yojson {strict = false}];;
93

    
94
(************************************************************************************)		   
95
(*            Attributes for types, arrays, signals and strings                     *)
96
(************************************************************************************)		   
97

    
98
type 'basetype vhdl_type_attributes_t =
99
  | TAttNoArg of { id: string }
100
  | TAttIntArg of { id: string; arg: int }
101
  | TAttValArg of { id: string; arg: 'basetype }
102
  | TAttStringArg of { id: string; arg: string }
103
[@@deriving yojson {strict = false}];;
104

    
105
let typ_att_noarg = ["base"; "left"; "right"; "high"; "low"]
106
let typ_att_intarg = ["pos"; "val"; "succ"; "pred"; "leftof"; "rightof"]
107
let typ_att_valarg = ["image"]
108
let typ_att_stringarg = ["value"]
109
  
110
let array_att_intarg = ["left"; "right"; "high"; "low"; "range"; "reverse_range"; "length"]  
111

    
112
type vhdl_parameter_t =
113
  {
114
    names: vhdl_name_t list;
115
    mode: string list [@default []];
116
    typ: vhdl_subtype_indication_t;
117
    init_val: cst_val_t option [@default Some (CstInt (0))];
118
  }
119
[@@deriving yojson {strict = false}];;
120

    
121
type vhdl_subprogram_spec_t =
122
  {
123
    name: string [@default ""];
124
    typeMark: vhdl_name_t [@default NoName];
125
    parameters: vhdl_parameter_t list;
126
    isPure: bool [@default false];
127
  }
128
[@@deriving yojson {strict = false}];;
129

    
130
(************************************************************************************)		   
131
(*                        Expressions  / Statements                                 *)
132
(************************************************************************************)		   
133

    
134
let arith_funs = ["+";"-";"*";"/";"mod"; "rem";"abs";"**";"&"]
135
let bool_funs  = ["and"; "or"; "nand"; "nor"; "xor"; "not"]
136
let rel_funs   = ["<";">";"<=";">=";"/=";"=";"?=";"?/=";"?<";"?<=";"?>";"?>=";"??"]
137
let shift_funs = ["sll";"srl";"sla";"sra";"rol";"ror"]
138

    
139
type vhdl_sequential_stmt_t = 
140
  | VarAssign of { label: vhdl_name_t [@default NoName]; lhs: vhdl_name_t; rhs: vhdl_expr_t } [@name "VARIABLE_ASSIGNMENT_STATEMENT"]
141
  | SigSeqAssign of { label: vhdl_name_t [@default NoName]; lhs: vhdl_name_t; rhs: vhdl_expr_t list} [@name "SIGNAL_ASSIGNMENT_STATEMENT"]
142
  | If of { label: vhdl_name_t [@default NoName]; if_cases: vhdl_if_case_t list;
143
    default: vhdl_sequential_stmt_t list [@default []]; } [@name "IF_STATEMENT"]
144
  | Case of { label: vhdl_name_t [@default NoName]; guard: vhdl_expr_t; branches: vhdl_case_item_t list } [@name "CASE_STATEMENT_TREE"]
145
  | Exit of { label: vhdl_name_t [@default NoName]; loop_label: string option [@default Some ""]; condition: vhdl_expr_t option [@default Some IsNull]} [@name "EXIT_STATEMENT"]
146
  | Assert of { label: vhdl_name_t [@default NoName]; cond: vhdl_expr_t; report: vhdl_expr_t [@default IsNull]; severity: vhdl_expr_t [@default IsNull]} [@name "ASSERTION_STATEMENT"]
147
  | Wait [@name "WAIT_STATEMENT"]
148
  | Null of { label: vhdl_name_t [@default NoName]} [@name "NULL_STATEMENT"]
149
  | Return of { label: vhdl_name_t [@default NoName]} [@name "RETURN_STATEMENT"]
150
and vhdl_if_case_t = 
151
  {
152
    if_cond: vhdl_expr_t;
153
    if_block: vhdl_sequential_stmt_t list;
154
  }
155
and vhdl_case_item_t = 
156
  {
157
    when_cond: vhdl_expr_t list;
158
    when_stmt: vhdl_sequential_stmt_t list;
159
  }
160
[@@deriving yojson {strict = false}];;
161

    
162
type vhdl_declaration_t =
163
  | VarDecl of {
164
      names : vhdl_name_t list; 
165
      typ : vhdl_subtype_indication_t; 
166
      init_val : cst_val_t option [@default Some (CstInt (0))] 
167
    } [@name "VARIABLE_DECLARATION"]
168
  | CstDecl of { 
169
      names : vhdl_name_t list; 
170
      typ : vhdl_subtype_indication_t; 
171
      init_val : cst_val_t 
172
    } [@name "CONSTANT_DECLARATION"]
173
  | SigDecl of { 
174
      names : vhdl_name_t list; 
175
      typ : vhdl_subtype_indication_t; 
176
      init_val : cst_val_t option [@default Some (CstInt (0))] 
177
    } [@name "SIGNAL_DECLARATION"]
178
  | Subprogram of {
179
      name: vhdl_name_t [@default NoName]; 
180
      kind: string [@default ""]; 
181
      spec: vhdl_subprogram_spec_t [@default {name="";typeMark=NoName;parameters=[];isPure=false}]; 
182
      decl_part: vhdl_declaration_t list [@default []]; 
183
      stmts: vhdl_sequential_stmt_t list [@default []]
184
    } [@name "SUBPROGRAM_BODY"]
185
[@@deriving yojson {strict = false}];;
186

    
187
type signal_condition_t =
188
  {                            
189
    expr: vhdl_expr_t list;              (* when expression *)
190
    cond: vhdl_expr_t [@default IsNull];  (* optional else case expression. 
191
                                             If None, could be a latch  *)
192
  }
193
[@@deriving yojson {strict = false}];;
194

    
195
type signal_selection_t =
196
  {
197
    expr : vhdl_expr_t;
198
    when_sel: vhdl_expr_t list [@default []];
199
  }
200
[@@deriving yojson {strict = false}];;
201

    
202
type conditional_signal_t =
203
  {
204
    postponed: bool [@default false];
205
    label: vhdl_name_t [@default NoName];
206
    lhs: vhdl_name_t;        (* assigned signal = target*)
207
    rhs: signal_condition_t list;                   (* expression *)
208
    cond: vhdl_expr_t [@default IsNull];
209
    delay: vhdl_expr_t [@default IsNull];
210
  }
211
[@@deriving yojson {strict = false}];;
212

    
213
type process_t =
214
  { 
215
    id: vhdl_name_t [@default NoName];
216
    declarations: vhdl_declaration_t list option [@key "PROCESS_DECLARATIVE_PART"] [@default Some []];
217
    active_sigs: vhdl_name_t list [@default []];
218
    body: vhdl_sequential_stmt_t list [@key "PROCESS_STATEMENT_PART"] [@default []]
219
  }
220
[@@deriving yojson {strict = false}];;
221

    
222
type selected_signal_t = 
223
  { 
224
    postponed: bool [@default false];
225
    label: vhdl_name_t [@default NoName];
226
    lhs: vhdl_name_t;      (* assigned signal = target *)
227
    sel: vhdl_expr_t;  
228
    branches: signal_selection_t list [@default []];
229
    delay: vhdl_expr_t option;
230
  }
231
[@@deriving yojson {strict = false}];;
232
			   
233
type vhdl_concurrent_stmt_t =
234
  | SigAssign of conditional_signal_t [@name "CONDITIONAL_SIGNAL_ASSIGNMENT"]
235
  | Process of process_t [@name "PROCESS_STATEMENT"]
236
  | SelectedSig of selected_signal_t [@name "SELECTED_SIGNAL_ASSIGNMENT"]
237
[@@deriving yojson {strict = false}];;
238
  (*
239
type vhdl_statement_t =
240
  
241
  (* | DeclarationStmt of declaration_stmt_t *)
242
  | ConcurrentStmt of vhdl_concurrent_stmt_t
243
  | SequentialStmt of vhdl_sequential_stmt_t
244
   *)
245
		     
246
(************************************************************************************)		   
247
(*                     Entities                                                     *)
248
(************************************************************************************)		   
249
			     
250
type vhdl_port_mode_t = 
251
    InPort     [@name "in"]
252
  | OutPort    [@name "out"]
253
  | InoutPort  [@name "inout"]
254
  | BufferPort [@name "buffer"]
255
[@@deriving yojson];;
256
	     
257
type vhdl_port_t =
258
  {
259
    names: vhdl_name_t list [@default []];
260
    mode: vhdl_port_mode_t [@default InPort];
261
    typ: vhdl_subtype_indication_t;
262
    expr: vhdl_expr_t [@default IsNull];
263
  }
264
[@@deriving yojson {strict = false}];;
265

    
266
type vhdl_entity_t =
267
  {
268
    name: vhdl_name_t [@default NoName];
269
    generics: vhdl_port_t list [@default []];
270
    ports: vhdl_port_t list [@default []];
271
    declaration: vhdl_declaration_t list [@key "ENTITY_DECLARATIVE_PART"] [@default []];
272
    stmts: vhdl_concurrent_stmt_t list [@key "ENTITY_STATEMENT_PART"] [@default []]; 
273
  }
274
[@@deriving yojson {strict = false}];;
275

    
276
(************************************************************************************)		   
277
(*                    Packages / Library loading                                    *)
278
(************************************************************************************)		   
279
				
280
(* Optional. Describes shared definitions *)
281
type vhdl_package_t =
282
  {
283
    name: vhdl_name_t [@default NoName];
284
    shared_defs: vhdl_definition_t list [@default []];
285
  }
286
[@@deriving yojson {strict = false}];;
287

    
288
type vhdl_load_t = 
289
    Library of vhdl_name_t list [@name "LIBRARY_CLAUSE"] [@default []]
290
  | Use of vhdl_name_t list [@name "USE_CLAUSE"] [@default []]
291
[@@deriving yojson];;
292

    
293
(************************************************************************************)		   
294
(*                        Architecture / VHDL Design                                *)
295
(************************************************************************************)		   
296
				       
297
type vhdl_architecture_t =
298
  {
299
    name: vhdl_name_t [@default NoName];
300
    entity: vhdl_name_t [@default NoName];
301
    declarations: vhdl_declaration_t list [@key "ARCHITECTURE_DECLARATIVE_PART"] [@default []];
302
    body: vhdl_concurrent_stmt_t list [@key "ARCHITECTURE_STATEMENT_PART"] [@default []]; 
303
  }
304
[@@deriving yojson {strict = false}];;
305
    
306
(* TODO. Configuration is optional *)
307
type vhdl_configuration_t = unit
308
[@@deriving yojson {strict = false}];;
309

    
310
type vhdl_library_unit_t = (* TODO: PACKAGE_BODY *)
311
    Package of vhdl_package_t [@name "PACKAGE_DECLARATION"]
312
  | Entities of vhdl_entity_t [@name "ENTITY_DECLARATION"]
313
  | Architecture of vhdl_architecture_t [@name "ARCHITECTURE_BODY"]
314
  | Configuration of vhdl_configuration_t [@name "CONFIGURATION_DECLARATION"]
315
[@@deriving yojson {strict = false}];;
316

    
317
type vhdl_design_unit_t =
318
  {
319
    contexts: vhdl_load_t list [@default []];
320
    library: vhdl_library_unit_t;
321
  }
322
[@@deriving yojson {strict = false}];;
323

    
324
type vhdl_design_file_t =
325
  {
326
    design_units: vhdl_design_unit_t list [@default []];
327
  }
328
[@@deriving yojson {strict = false}];;
329

    
330
type vhdl_file_t = 
331
  {
332
    design_file: vhdl_design_file_t [@default {design_units=[]}] [@key "DESIGN_FILE"];
333
  }
334
[@@deriving yojson];;