Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / VHDL / vhdl_ast.ml @ 248eb65e

History | View | Annotate | Download (15.6 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 vhdl_cst_val_t [@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 =
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_declaration_t =
189
  | VarDecl of {
190
      names : vhdl_name_t list; 
191
      typ : vhdl_subtype_indication_t; 
192
      init_val : vhdl_expr_t [@default IsNull] 
193
    } [@name "VARIABLE_DECLARATION"]
194
  | CstDecl of { 
195
      names : vhdl_name_t list; 
196
      typ : vhdl_subtype_indication_t; 
197
      init_val : vhdl_expr_t
198
    } [@name "CONSTANT_DECLARATION"]
199
  | SigDecl of { 
200
      names : vhdl_name_t list; 
201
      typ : vhdl_subtype_indication_t; 
202
      init_val : vhdl_expr_t [@default IsNull]
203
    } [@name "SIGNAL_DECLARATION"]
204
  | Subprogram of {
205
      name: vhdl_name_t [@default NoName]; 
206
      kind: string [@default ""]; 
207
      spec: vhdl_subprogram_spec_t [@default {name="";typeMark=NoName;parameters=[];isPure=false}]; 
208
      decl_part: vhdl_declaration_t list [@default []]; 
209
      stmts: vhdl_sequential_stmt_t list [@default []]
210
    } [@name "SUBPROGRAM_BODY"]
211
[@@deriving show { with_path = false }, yojson {strict = false}];;
212

    
213
type vhdl_signal_condition_t =
214
  {                            
215
    expr: vhdl_expr_t list;              (* when expression *)
216
    cond: vhdl_expr_t [@default IsNull];  (* optional else case expression. 
217
                                             If None, could be a latch  *)
218
  }
219
[@@deriving show { with_path = false }, yojson {strict = false}];;
220

    
221
type vhdl_signal_selection_t =
222
  {
223
    expr : vhdl_expr_t;
224
    when_sel: vhdl_expr_t list [@default []];
225
  }
226
[@@deriving show { with_path = false }, yojson {strict = false}];;
227

    
228
type vhdl_conditional_signal_t =
229
  {
230
    postponed: bool [@default false];
231
    label: vhdl_name_t [@default NoName];
232
    lhs: vhdl_name_t;        (* assigned signal = target*)
233
    rhs: vhdl_signal_condition_t list;                   (* expression *)
234
    cond: vhdl_expr_t [@default IsNull];
235
    delay: vhdl_expr_t [@default IsNull];
236
  }
237
[@@deriving show { with_path = false }, yojson {strict = false}];;
238

    
239
type vhdl_process_t =
240
  { 
241
    id: vhdl_name_t [@default NoName];
242
    declarations: vhdl_declaration_t list option [@key "PROCESS_DECLARATIVE_PART"] [@default Some []];
243
    active_sigs: vhdl_name_t list [@default []];
244
    body: vhdl_sequential_stmt_t list [@key "PROCESS_STATEMENT_PART"] [@default []]
245
  }
246
[@@deriving show { with_path = false }, yojson {strict = false}];;
247

    
248
type vhdl_selected_signal_t = 
249
  { 
250
    postponed: bool [@default false];
251
    label: vhdl_name_t [@default NoName];
252
    lhs: vhdl_name_t;      (* assigned signal = target *)
253
    sel: vhdl_expr_t;  
254
    branches: vhdl_signal_selection_t list [@default []];
255
    delay: vhdl_expr_t option;
256
  }
257
[@@deriving show { with_path = false }, yojson {strict = false}];;
258

    
259
type vhdl_component_instantiation_t =
260
  {
261
    name: vhdl_name_t;
262
    inst_unit: vhdl_name_t;
263
    archi_name: vhdl_name_t option [@default None];
264
    generic_map: vhdl_assoc_element_t option [@default None];
265
    port_map: vhdl_assoc_element_t option [@default None];
266
  }
267
[@@deriving show { with_path = false }, yojson {strict = false}];;
268

    
269
type vhdl_concurrent_stmt_t =
270
  | SigAssign of vhdl_conditional_signal_t [@name "CONDITIONAL_SIGNAL_ASSIGNMENT"]
271
  | Process of vhdl_process_t [@name "PROCESS_STATEMENT"]
272
  | SelectedSig of vhdl_selected_signal_t [@name "SELECTED_SIGNAL_ASSIGNMENT"]
273
  | ComponentInst of vhdl_component_instantiation_t [@name "COMPONENT_INSTANTIATION_STATEMENT"]
274
[@@deriving show { with_path = false }, yojson {strict = false}];;
275
  (*
276
type vhdl_statement_t =
277
  
278
  (* | DeclarationStmt of declaration_stmt_t *)
279
  | ConcurrentStmt of vhdl_concurrent_stmt_t
280
  | SequentialStmt of vhdl_sequential_stmt_t
281
   *)
282
		     
283
(************************************************************************************)		   
284
(*                     Entities                                                     *)
285
(************************************************************************************)		   
286
			     
287
type vhdl_port_mode_t = 
288
    InPort     [@name "in"]
289
  | OutPort    [@name "out"]
290
  | InoutPort  [@name "inout"]
291
  | BufferPort [@name "buffer"]
292
[@@deriving show { with_path = false }, yojson];;
293
	     
294
type vhdl_port_t =
295
  {
296
    names: vhdl_name_t list [@default []];
297
    mode: vhdl_port_mode_t [@default InPort];
298
    typ: vhdl_subtype_indication_t;
299
    expr: vhdl_expr_t [@default IsNull];
300
  }
301
[@@deriving show { with_path = false }, yojson {strict = false}];;
302

    
303
type vhdl_entity_t =
304
  {
305
    name: vhdl_name_t [@default NoName];
306
    generics: vhdl_port_t list [@default []];
307
    ports: vhdl_port_t list [@default []];
308
    declaration: vhdl_declaration_t list [@key "ENTITY_DECLARATIVE_PART"] [@default []];
309
    stmts: vhdl_concurrent_stmt_t list [@key "ENTITY_STATEMENT_PART"] [@default []]; 
310
  }
311
[@@deriving show { with_path = false }, yojson {strict = false}];;
312

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

    
326
type vhdl_load_t = 
327
    Library of vhdl_name_t list [@name "LIBRARY_CLAUSE"] [@default []]
328
  | Use of vhdl_name_t list [@name "USE_CLAUSE"] [@default []]
329
[@@deriving show { with_path = false }, yojson];;
330

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

    
348
type vhdl_library_unit_t = (* TODO: PACKAGE_BODY *)
349
    Package of vhdl_package_t [@name "PACKAGE_DECLARATION"]
350
  | Entities of vhdl_entity_t [@name "ENTITY_DECLARATION"]
351
  | Architecture of vhdl_architecture_t [@name "ARCHITECTURE_BODY"]
352
  | Configuration of vhdl_configuration_t [@name "CONFIGURATION_DECLARATION"]
353
[@@deriving show { with_path = false }, yojson {strict = false}];;
354

    
355
type vhdl_design_unit_t =
356
  {
357
    contexts: vhdl_load_t list [@default []];
358
    library: vhdl_library_unit_t;
359
  }
360
[@@deriving show { with_path = false }, yojson {strict = false}];;
361

    
362
type vhdl_design_file_t =
363
  {
364
    design_units: vhdl_design_unit_t list [@default []];
365
  }
366
[@@deriving show { with_path = false }, yojson {strict = false}];;
367

    
368
type vhdl_file_t = 
369
  {
370
    design_file: vhdl_design_file_t [@default {design_units=[]}] [@key "DESIGN_FILE"];
371
  }
372
[@@deriving show { with_path = false }, yojson];;