Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / VHDL / vhdl_ast.ml @ 3d099916

History | View | Annotate | Download (16.2 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 vhdl_cst_val_t = 
22
    CstInt of int 
23
  | CstStdLogic of string
24
  | CstLiteral of string [@name "CST_LITERAL"]
25
[@@deriving show { with_path = false }, yojson {strict = false}];;
26

    
27
(*
28
let pp_cst_val fmt c =
29
  match c with
30
  | CstInt i -> Format.fprintf fmt "%i" i
31
  | CstStdLogic s -> if List.mem s std_logic_cst then Format.fprintf fmt "%s" s else assert false
32
  | CstLiteral s -> Format.fprintf fmt "%s" s
33
*)
34

    
35
type vhdl_type_t =
36
  | Base of string
37
  | Range of string option * int * int
38
  | Bit_vector of int * int
39
  | Array of { indexes: vhdl_name_t list [@default []]; const: vhdl_constraint_t option [@default None]; definition: vhdl_subtype_indication_t } [@name "ARRAY_TYPE_DEFINITION"]
40
  | Record of vhdl_element_declaration_t list [@name "RECORD_TYPE_DEFINITION"]
41
  | Enumerated of vhdl_name_t list [@name "ENUMERATION_TYPE_DEFINITION"]
42
  | Void
43
and vhdl_element_declaration_t =
44
  { 
45
    names : vhdl_name_t list; 
46
    definition: vhdl_subtype_indication_t;
47
  }
48
and vhdl_subtype_indication_t =
49
  {
50
    name : vhdl_name_t [@default NoName];
51
    functionName : vhdl_name_t [@default NoName];
52
    const: vhdl_constraint_t [@default NoConstraint];
53
  }
54
and vhdl_discrete_range_t =
55
  | SubDiscreteRange of vhdl_subtype_indication_t [@name "SUB_DISCRETE_RANGE"]
56
  | NamedRange of vhdl_name_t [@name "NAMED_RANGE"]
57
  | DirectedRange of { direction: string; from: vhdl_expr_t; _to: vhdl_expr_t } [@name "RANGE_WITH_DIRECTION"]
58
and vhdl_constraint_t =
59
  | RefConstraint of { ref_name: vhdl_name_t; }
60
  | RangeConstraint of { range: vhdl_discrete_range_t } [@name "RANGE_CONSTRAINT"]
61
  | IndexConstraint of { ranges: vhdl_discrete_range_t list; } [@name "INDEX_CONSTRAINT"]
62
  | ArrayConstraint of { ranges: vhdl_discrete_range_t list; sub: vhdl_constraint_t } [@name "ARRAY_CONSTRAINT"]
63
  | RecordConstraint
64
  | NoConstraint
65
and vhdl_definition_t =
66
  | Type of {name : vhdl_name_t ; definition: vhdl_type_t} [@name "TYPE_DECLARATION"]
67
  | Subtype of {name : vhdl_name_t ; typ : vhdl_subtype_indication_t} [@name "SUBTYPE_DECLARATION"]
68
and vhdl_expr_t =
69
  | Call of vhdl_name_t [@name "CALL"]
70
  | Cst of { value: vhdl_cst_val_t; unit_name: vhdl_name_t option [@default None]} [@name "CONSTANT_VALUE"]
71
  | Op of { id: string [@default ""]; args: vhdl_expr_t list [@default []]} [@name "EXPRESSION"]
72
  | IsNull [@name "IsNull"]
73
  | Time of { value: int; phy_unit: string [@default ""]}
74
  | Sig of { name: vhdl_name_t; att: vhdl_signal_attributes_t option }
75
  | SuffixMod of { expr : vhdl_expr_t; selection : vhdl_suffix_selection_t }
76
  | Aggregate of { elems : vhdl_element_assoc_t list } [@name "AGGREGATE"]
77
  | Others [@name "OTHERS"]
78
and vhdl_name_t = (* Add something like TOKEN_NAME for specific keywords (open, all, ...) ? *)
79
  | Simple of string [@name "SIMPLE_NAME"]
80
  | Identifier of string [@name "IDENTIFIER"]
81
  | Selected of vhdl_name_t list [@name "SELECTED_NAME"]
82
  | Index of { id: vhdl_name_t; exprs: vhdl_expr_t list } [@name "INDEXED_NAME"]
83
  | Slice of { id: vhdl_name_t; range: vhdl_discrete_range_t } [@name "SLICE_NAME"]
84
  | Attribute of { id: vhdl_name_t; designator: vhdl_name_t; expr: vhdl_expr_t [@default IsNull]} [@name "ATTRIBUTE_NAME"]
85
  | Function of { id: vhdl_name_t; assoc_list: vhdl_assoc_element_t list } [@name "FUNCTION_CALL"]
86
  | NoName
87
and vhdl_assoc_element_t =
88
  {
89
    formal_name: vhdl_name_t option [@default None];
90
    formal_arg: vhdl_name_t option [@default None];
91
    actual_name: vhdl_name_t option [@default None];
92
    actual_designator: vhdl_name_t option [@default None];
93
    actual_expr: vhdl_expr_t option [@default None];
94
  }
95
and vhdl_element_assoc_t =
96
  {
97
    choices: vhdl_expr_t list [@default []];
98
    expr: vhdl_expr_t;
99
  }
100
and vhdl_array_attributes_t = 
101
  | AAttInt of { id: string; arg: int; } 
102
  | AAttAscending
103
and vhdl_signal_attributes_t = SigAtt of string
104
and vhdl_string_attributes_t = StringAtt of string
105
and vhdl_suffix_selection_t = Idx of int | SuffixRange of int * int
106
[@@deriving show { with_path = false }, yojson {strict = false}];;
107

    
108
(*
109
let rec pp_vhdl_type fmt t =
110
  match t with
111
  | Base s -> Format.fprintf fmt "%s" s 
112
  | Range(base, n, m) -> Format.fprintf fmt "%trange %i to %i" (fun fmt -> match base with Some s -> Format.fprintf fmt "%s " s | None -> ()) n m
113
  | Bit_vector (n,m) -> Format.fprintf fmt "bit_vector(%i downto %i)" n m
114
  | Array (n, m, base) -> Format.fprintf fmt "array (%i to %i) of %a" n m pp_vhdl_type base
115
  | Enumerated sl -> Format.fprintf fmt "(%a)" (Utils.fprintf_list ~sep:", " Format.pp_print_string) sl
116
  | Void -> Format.fprintf fmt ""
117
*)
118

    
119
(************************************************************************************)		   
120
(*            Attributes for types, arrays, signals and strings                     *)
121
(************************************************************************************)		   
122

    
123
type 'basetype vhdl_type_attributes_t =
124
  | TAttNoArg of { id: string }
125
  | TAttIntArg of { id: string; arg: int }
126
  | TAttValArg of { id: string; arg: 'basetype }
127
  | TAttStringArg of { id: string; arg: string }
128
[@@deriving show { with_path = false }, yojson {strict = false}];;
129

    
130
let typ_att_noarg = ["base"; "left"; "right"; "high"; "low"]
131
let typ_att_intarg = ["pos"; "val"; "succ"; "pred"; "leftof"; "rightof"]
132
let typ_att_valarg = ["image"]
133
let typ_att_stringarg = ["value"]
134
  
135
let array_att_intarg = ["left"; "right"; "high"; "low"; "range"; "reverse_range"; "length"]  
136

    
137
type vhdl_parameter_t =
138
  {
139
    names: vhdl_name_t list;
140
    mode: string list [@default []];
141
    typ: vhdl_subtype_indication_t;
142
    init_val: vhdl_cst_val_t option [@default None];
143
  }
144
[@@deriving show { with_path = false }, yojson {strict = false}];;
145

    
146
type vhdl_subprogram_spec_t =
147
  {
148
    name: string [@default ""];
149
    subprogram_type: string [@default ""];
150
    typeMark: vhdl_name_t [@default NoName];
151
    parameters: vhdl_parameter_t list [@default []];
152
    isPure: bool [@default false];
153
  }
154
[@@deriving show { with_path = false }, yojson {strict = false}];;
155

    
156
(************************************************************************************)		   
157
(*                        Expressions  / Statements                                 *)
158
(************************************************************************************)		   
159

    
160
let arith_funs = ["+";"-";"*";"/";"mod"; "rem";"abs";"**";"&"]
161
let bool_funs  = ["and"; "or"; "nand"; "nor"; "xor"; "not"]
162
let rel_funs   = ["<";">";"<=";">=";"/=";"=";"?=";"?/=";"?<";"?<=";"?>";"?>=";"??"]
163
let shift_funs = ["sll";"srl";"sla";"sra";"rol";"ror"]
164

    
165
type vhdl_sequential_stmt_t = 
166
  | VarAssign of { label: vhdl_name_t [@default NoName]; lhs: vhdl_name_t; rhs: vhdl_expr_t } [@name "VARIABLE_ASSIGNMENT_STATEMENT"]
167
  | SigSeqAssign of { label: vhdl_name_t [@default NoName]; lhs: vhdl_name_t; rhs: vhdl_expr_t list} [@name "SIGNAL_ASSIGNMENT_STATEMENT"]
168
  | If of { label: vhdl_name_t [@default NoName]; if_cases: vhdl_if_case_t list;
169
    default: vhdl_sequential_stmt_t list [@default []]; } [@name "IF_STATEMENT"]
170
  | Case of { label: vhdl_name_t [@default NoName]; guard: vhdl_expr_t; branches: vhdl_case_item_t list } [@name "CASE_STATEMENT_TREE"]
171
  | 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"]
172
  | 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"]
173
  | ProcedureCall of { label: vhdl_name_t [@default NoName]; name: vhdl_name_t; assocs: vhdl_assoc_element_t list [@default []] } [@name "PROCEDURE_CALL_STATEMENT"]
174
  | Wait [@name "WAIT_STATEMENT"]
175
  | Null of { label: vhdl_name_t [@default NoName]} [@name "NULL_STATEMENT"]
176
  | Return of { label: vhdl_name_t [@default NoName]} [@name "RETURN_STATEMENT"]
177
and vhdl_if_case_t = 
178
  {
179
    if_cond: vhdl_expr_t;
180
    if_block: vhdl_sequential_stmt_t list;
181
  }
182
and vhdl_case_item_t = 
183
  {
184
    when_cond: vhdl_expr_t list;
185
    when_stmt: vhdl_sequential_stmt_t list;
186
  }
187
[@@deriving show { with_path = false }, yojson {strict = false}];;
188

    
189
type vhdl_port_mode_t = 
190
    InPort     [@name "in"]
191
  | OutPort    [@name "out"]
192
  | InoutPort  [@name "inout"]
193
  | BufferPort [@name "buffer"]
194
[@@deriving show { with_path = false }, yojson];;
195
	     
196
type vhdl_port_t =
197
  {
198
    names: vhdl_name_t list [@default []];
199
    mode: vhdl_port_mode_t [@default InPort];
200
    typ: vhdl_subtype_indication_t;
201
    expr: vhdl_expr_t [@default IsNull];
202
  }
203
[@@deriving show { with_path = false }, yojson {strict = false}];;
204

    
205
type vhdl_declaration_t =
206
  | VarDecl of {
207
      names : vhdl_name_t list; 
208
      typ : vhdl_subtype_indication_t; 
209
      init_val : vhdl_expr_t [@default IsNull] 
210
    } [@name "VARIABLE_DECLARATION"]
211
  | CstDecl of { 
212
      names : vhdl_name_t list; 
213
      typ : vhdl_subtype_indication_t; 
214
      init_val : vhdl_expr_t
215
    } [@name "CONSTANT_DECLARATION"]
216
  | SigDecl of { 
217
      names : vhdl_name_t list; 
218
      typ : vhdl_subtype_indication_t; 
219
      init_val : vhdl_expr_t [@default IsNull]
220
    } [@name "SIGNAL_DECLARATION"]
221
  | ComponentDecl of {
222
      name: vhdl_name_t [@default NoName];
223
      generics: vhdl_port_t list [@default []];
224
      ports: vhdl_port_t list [@default []];
225
    } [@name "COMPONENT_DECLARATION"]
226
  | Subprogram of {
227
      spec: vhdl_subprogram_spec_t; 
228
      decl_part: vhdl_declaration_t list [@default []]; 
229
      stmts: vhdl_sequential_stmt_t list [@default []]
230
    } [@name "SUBPROGRAM_BODY"]
231
[@@deriving show { with_path = false }, yojson {strict = false}];;
232

    
233
type vhdl_load_t = 
234
    Library of vhdl_name_t list [@name "LIBRARY_CLAUSE"] [@default []]
235
  | Use of vhdl_name_t list [@name "USE_CLAUSE"] [@default []]
236
[@@deriving show { with_path = false }, yojson];;
237

    
238
type vhdl_declarative_item_t =
239
  {
240
    use_clause: vhdl_load_t option [@default None];
241
    declaration: vhdl_declaration_t option [@default None];
242
    definition: vhdl_definition_t option [@default None];
243
  }
244
[@@deriving show { with_path = false }, yojson {strict = false}];;
245

    
246
type vhdl_signal_condition_t =
247
  {                            
248
    expr: vhdl_expr_t list;              (* when expression *)
249
    cond: vhdl_expr_t [@default IsNull];  (* optional else case expression. 
250
                                             If None, could be a latch  *)
251
  }
252
[@@deriving show { with_path = false }, yojson {strict = false}];;
253

    
254
type vhdl_signal_selection_t =
255
  {
256
    expr : vhdl_expr_t;
257
    when_sel: vhdl_expr_t list [@default []];
258
  }
259
[@@deriving show { with_path = false }, yojson {strict = false}];;
260

    
261
type vhdl_conditional_signal_t =
262
  {
263
    postponed: bool [@default false];
264
    label: vhdl_name_t [@default NoName];
265
    lhs: vhdl_name_t;        (* assigned signal = target*)
266
    rhs: vhdl_signal_condition_t list;                   (* expression *)
267
    cond: vhdl_expr_t [@default IsNull];
268
    delay: vhdl_expr_t [@default IsNull];
269
  }
270
[@@deriving show { with_path = false }, yojson {strict = false}];;
271

    
272
type vhdl_process_t =
273
  { 
274
    id: vhdl_name_t [@default NoName];
275
    declarations: vhdl_declarative_item_t list [@key "PROCESS_DECLARATIVE_PART"] [@default []];
276
    active_sigs: vhdl_name_t list [@default []];
277
    body: vhdl_sequential_stmt_t list [@key "PROCESS_STATEMENT_PART"] [@default []]
278
  }
279
[@@deriving show { with_path = false }, yojson {strict = false}];;
280

    
281
type vhdl_selected_signal_t = 
282
  { 
283
    postponed: bool [@default false];
284
    label: vhdl_name_t [@default NoName];
285
    lhs: vhdl_name_t;      (* assigned signal = target *)
286
    sel: vhdl_expr_t;  
287
    branches: vhdl_signal_selection_t list [@default []];
288
    delay: vhdl_expr_t option;
289
  }
290
[@@deriving show { with_path = false }, yojson {strict = false}];;
291

    
292
type vhdl_component_instantiation_t =
293
  {
294
    name: vhdl_name_t;
295
    inst_unit: vhdl_name_t;
296
    archi_name: vhdl_name_t option [@default None];
297
    generic_map: vhdl_assoc_element_t list [@default []];
298
    port_map: vhdl_assoc_element_t list [@default []];
299
  }
300
[@@deriving show { with_path = false }, yojson {strict = false}];;
301

    
302
type vhdl_concurrent_stmt_t =
303
  | SigAssign of vhdl_conditional_signal_t [@name "CONDITIONAL_SIGNAL_ASSIGNMENT"]
304
  | Process of vhdl_process_t [@name "PROCESS_STATEMENT"]
305
  | SelectedSig of vhdl_selected_signal_t [@name "SELECTED_SIGNAL_ASSIGNMENT"]
306
  | ComponentInst of vhdl_component_instantiation_t [@name "COMPONENT_INSTANTIATION_STATEMENT"]
307
[@@deriving show { with_path = false }, yojson {strict = false}];;
308
  (*
309
type vhdl_statement_t =
310
  
311
  (* | DeclarationStmt of declaration_stmt_t *)
312
  | ConcurrentStmt of vhdl_concurrent_stmt_t
313
  | SequentialStmt of vhdl_sequential_stmt_t
314
   *)
315
		     
316
(************************************************************************************)		   
317
(*                     Entities                                                     *)
318
(************************************************************************************)
319

    
320
type vhdl_entity_t =
321
  {
322
    name: vhdl_name_t [@default NoName];
323
    generics: vhdl_port_t list [@default []];
324
    ports: vhdl_port_t list [@default []];
325
    declaration: vhdl_declarative_item_t list [@key "ENTITY_DECLARATIVE_PART"] [@default []];
326
    stmts: vhdl_concurrent_stmt_t list [@key "ENTITY_STATEMENT_PART"] [@default []]; 
327
  }
328
[@@deriving show { with_path = false }, yojson {strict = false}];;
329

    
330
(************************************************************************************)		   
331
(*                    Packages / Library loading                                    *)
332
(************************************************************************************)		   
333
				
334
(* Optional. Describes shared definitions *)
335
type vhdl_package_t =
336
  {
337
    name: vhdl_name_t [@default NoName];
338
    shared_defs: vhdl_definition_t list [@default []];
339
    shared_decls: vhdl_declaration_t list [@default []];
340
  }
341
[@@deriving show { with_path = false }, yojson {strict = false}];;
342

    
343
(************************************************************************************)		   
344
(*                        Architecture / VHDL Design                                *)
345
(************************************************************************************)		   
346
				       
347
type vhdl_architecture_t =
348
  {
349
    name: vhdl_name_t [@default NoName];
350
    entity: vhdl_name_t [@default NoName];
351
    declarations: vhdl_declarative_item_t list [@key "ARCHITECTURE_DECLARATIVE_PART"] [@default []];
352
    body: vhdl_concurrent_stmt_t list [@key "ARCHITECTURE_STATEMENT_PART"] [@default []]; 
353
  }
354
[@@deriving show { with_path = false }, yojson {strict = false}];;
355
    
356
(* TODO. Configuration is optional *)
357
type vhdl_configuration_t = unit
358
[@@deriving show { with_path = false }, yojson {strict = false}];;
359

    
360
type vhdl_library_unit_t = (* TODO: PACKAGE_BODY *)
361
    Package of vhdl_package_t [@name "PACKAGE_DECLARATION"]
362
  | Entities of vhdl_entity_t [@name "ENTITY_DECLARATION"]
363
  | Architecture of vhdl_architecture_t [@name "ARCHITECTURE_BODY"]
364
  | Configuration of vhdl_configuration_t [@name "CONFIGURATION_DECLARATION"]
365
[@@deriving show { with_path = false }, yojson {strict = false}];;
366

    
367
type vhdl_design_unit_t =
368
  {
369
    contexts: vhdl_load_t list [@default []];
370
    library: vhdl_library_unit_t;
371
  }
372
[@@deriving show { with_path = false }, yojson {strict = false}];;
373

    
374
type vhdl_design_file_t =
375
  {
376
    design_units: vhdl_design_unit_t list [@default []];
377
  }
378
[@@deriving show { with_path = false }, yojson {strict = false}];;
379

    
380
type vhdl_file_t = 
381
  {
382
    design_file: vhdl_design_file_t [@default {design_units=[]}] [@key "DESIGN_FILE"];
383
  }
384
[@@deriving show { with_path = false }, yojson];;