Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / VHDL / vhdl_ast.ml @ 77bdbec5

History | View | Annotate | Download (16 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; 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;
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
    typeMark: vhdl_name_t [@default NoName];
150
    parameters: vhdl_parameter_t list;
151
    isPure: bool [@default false];
152
  }
153
[@@deriving show { with_path = false }, yojson {strict = false}];;
154

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

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

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

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

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

    
234
type vhdl_signal_condition_t =
235
  {                            
236
    expr: vhdl_expr_t list;              (* when expression *)
237
    cond: vhdl_expr_t [@default IsNull];  (* optional else case expression. 
238
                                             If None, could be a latch  *)
239
  }
240
[@@deriving show { with_path = false }, yojson {strict = false}];;
241

    
242
type vhdl_signal_selection_t =
243
  {
244
    expr : vhdl_expr_t;
245
    when_sel: vhdl_expr_t list [@default []];
246
  }
247
[@@deriving show { with_path = false }, yojson {strict = false}];;
248

    
249
type vhdl_conditional_signal_t =
250
  {
251
    postponed: bool [@default false];
252
    label: vhdl_name_t [@default NoName];
253
    lhs: vhdl_name_t;        (* assigned signal = target*)
254
    rhs: vhdl_signal_condition_t list;                   (* expression *)
255
    cond: vhdl_expr_t [@default IsNull];
256
    delay: vhdl_expr_t [@default IsNull];
257
  }
258
[@@deriving show { with_path = false }, yojson {strict = false}];;
259

    
260
type vhdl_process_t =
261
  { 
262
    id: vhdl_name_t [@default NoName];
263
    declarations: vhdl_declaration_t list option [@key "PROCESS_DECLARATIVE_PART"] [@default Some []];
264
    active_sigs: vhdl_name_t list [@default []];
265
    body: vhdl_sequential_stmt_t list [@key "PROCESS_STATEMENT_PART"] [@default []]
266
  }
267
[@@deriving show { with_path = false }, yojson {strict = false}];;
268

    
269
type vhdl_selected_signal_t = 
270
  { 
271
    postponed: bool [@default false];
272
    label: vhdl_name_t [@default NoName];
273
    lhs: vhdl_name_t;      (* assigned signal = target *)
274
    sel: vhdl_expr_t;  
275
    branches: vhdl_signal_selection_t list [@default []];
276
    delay: vhdl_expr_t option;
277
  }
278
[@@deriving show { with_path = false }, yojson {strict = false}];;
279

    
280
type vhdl_component_instantiation_t =
281
  {
282
    name: vhdl_name_t;
283
    inst_unit: vhdl_name_t;
284
    archi_name: vhdl_name_t option [@default None];
285
    generic_map: vhdl_assoc_element_t list [@default []];
286
    port_map: vhdl_assoc_element_t list [@default []];
287
  }
288
[@@deriving show { with_path = false }, yojson {strict = false}];;
289

    
290
type vhdl_concurrent_stmt_t =
291
  | SigAssign of vhdl_conditional_signal_t [@name "CONDITIONAL_SIGNAL_ASSIGNMENT"]
292
  | Process of vhdl_process_t [@name "PROCESS_STATEMENT"]
293
  | SelectedSig of vhdl_selected_signal_t [@name "SELECTED_SIGNAL_ASSIGNMENT"]
294
  | ComponentInst of vhdl_component_instantiation_t [@name "COMPONENT_INSTANTIATION_STATEMENT"]
295
[@@deriving show { with_path = false }, yojson {strict = false}];;
296
  (*
297
type vhdl_statement_t =
298
  
299
  (* | DeclarationStmt of declaration_stmt_t *)
300
  | ConcurrentStmt of vhdl_concurrent_stmt_t
301
  | SequentialStmt of vhdl_sequential_stmt_t
302
   *)
303
		     
304
(************************************************************************************)		   
305
(*                     Entities                                                     *)
306
(************************************************************************************)
307

    
308
type vhdl_entity_t =
309
  {
310
    name: vhdl_name_t [@default NoName];
311
    generics: vhdl_port_t list [@default []];
312
    ports: vhdl_port_t list [@default []];
313
    declaration: vhdl_declaration_t list [@key "ENTITY_DECLARATIVE_PART"] [@default []];
314
    stmts: vhdl_concurrent_stmt_t list [@key "ENTITY_STATEMENT_PART"] [@default []]; 
315
  }
316
[@@deriving show { with_path = false }, yojson {strict = false}];;
317

    
318
(************************************************************************************)		   
319
(*                    Packages / Library loading                                    *)
320
(************************************************************************************)		   
321
				
322
(* Optional. Describes shared definitions *)
323
type vhdl_package_t =
324
  {
325
    name: vhdl_name_t [@default NoName];
326
    shared_defs: vhdl_definition_t list [@default []];
327
    shared_decls: vhdl_declaration_t list [@default []];
328
  }
329
[@@deriving show { with_path = false }, yojson {strict = false}];;
330

    
331
type vhdl_load_t = 
332
    Library of vhdl_name_t list [@name "LIBRARY_CLAUSE"] [@default []]
333
  | Use of vhdl_name_t list [@name "USE_CLAUSE"] [@default []]
334
[@@deriving show { with_path = false }, yojson];;
335

    
336
(************************************************************************************)		   
337
(*                        Architecture / VHDL Design                                *)
338
(************************************************************************************)		   
339
				       
340
type vhdl_architecture_t =
341
  {
342
    name: vhdl_name_t [@default NoName];
343
    entity: vhdl_name_t [@default NoName];
344
    use_clauses: vhdl_load_t list [@default []];
345
    declarations: vhdl_declaration_t list [@key "ARCHITECTURE_DECLARATIVE_PART"] [@default []];
346
    body: vhdl_concurrent_stmt_t list [@key "ARCHITECTURE_STATEMENT_PART"] [@default []]; 
347
  }
348
[@@deriving show { with_path = false }, yojson {strict = false}];;
349
    
350
(* TODO. Configuration is optional *)
351
type vhdl_configuration_t = unit
352
[@@deriving show { with_path = false }, yojson {strict = false}];;
353

    
354
type vhdl_library_unit_t = (* TODO: PACKAGE_BODY *)
355
    Package of vhdl_package_t [@name "PACKAGE_DECLARATION"]
356
  | Entities of vhdl_entity_t [@name "ENTITY_DECLARATION"]
357
  | Architecture of vhdl_architecture_t [@name "ARCHITECTURE_BODY"]
358
  | Configuration of vhdl_configuration_t [@name "CONFIGURATION_DECLARATION"]
359
[@@deriving show { with_path = false }, yojson {strict = false}];;
360

    
361
type vhdl_design_unit_t =
362
  {
363
    contexts: vhdl_load_t list [@default []];
364
    library: vhdl_library_unit_t;
365
  }
366
[@@deriving show { with_path = false }, yojson {strict = false}];;
367

    
368
type vhdl_design_file_t =
369
  {
370
    design_units: vhdl_design_unit_t list [@default []];
371
  }
372
[@@deriving show { with_path = false }, yojson {strict = false}];;
373

    
374
type vhdl_file_t = 
375
  {
376
    design_file: vhdl_design_file_t [@default {design_units=[]}] [@key "DESIGN_FILE"];
377
  }
378
[@@deriving show { with_path = false }, yojson];;