/
codegen.rml
4417 lines (3357 loc) · 140 KB
/
codegen.rml
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
Copyright (c) PELAB, Dept. Computer and Information Science, Linkoping University,
Sweden, in the following denoted PELAB/LIU.
This file is part of OpenModelica, in the following denoted OSM.
The OSM License, Version 1.1, March 4, 2005.
1 Preface
The aim of this license is to lay down the conditions enabling you to use,
modify and circulate OSM. However, PELAB/LIU remain the authors of
OSM and so retain property rights and the use of all ancillary rights.
2 Definitions
OSM is defined as all successive versions of the OSM software and their
documentation that have been developed by PELAB/LIU and including
accepted contributions from other contributors according to this license.
OSM DERIVED SOFTWARE is defined as all or part of OSM that you have
modified and/or translated and/or adapted.
3 Dual License
OSM is made available under the OSM licensing scheme, which is a dual
licensing scheme with two options, a) and b):
a) OSM OPEN SOURCE LICENSE:
If you wish to write Open Source software you can use the Open Source version
of OSM, released under the OSM license which include GPL as its open source
licensing option. If you use the OSM Open Source version you must release your
Application using OSM including this Application's source code under the GPL as well.
This OSM license text, and Copyright (c) PELAB/Linkoping University, must
be present in your copy of OSM and in OSM DERIVED SOFTWARE.
You should have received a copy of the GPL - GNU General Public License
along with OpenModelica; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
b) OSM COMMERCIAL LICENSE
If you are using OSM commercially - that is, for commercial usage or
for creating proprietary software for sale or use in a commercial setting
- you must purchase a commercial license of OSM from PELAB/LIU, which allows
you to use OSM without releasing your Application under the GPL.
Comment: Payments for OSM are intended for OSM development and
integration of accepted contributions into OSM.
4 Priority
If there is any conflict between this OSM License text and the GNU GPL
license, this text has priority.
5 Contributions
PELAB/LIU reserves the right to accept or turn down source code contributions
to OSM.
6 Limitation of the warranty
Except when mentioned otherwise in writing, OSM is supplied as is, with
no explicit or implicit warranty, including warranties of commercialization or
adaptation. You assume all risks concerning the quality or the effects of
OSM and its use. If OSM is defective, you will bear the costs of
all required services, corrections or repairs.
7 Consent
When you access and use OSM, you are presumed to be aware of and to
have accepted all the rights and obligations of the present OSM license.
This includes accepting that your open source code contributions to OSM,
if accepted into OSM by PELAB/IDA, follow the OSM licensing rules including
copyright and ownership by PELAB/IDA.
8 Binding effect
This license has the binding value of a contract.
You are not responsible for respect of the license by a third party.
9 Applicable law
The present license and its effects are subject to Swedish law and
Swedish courts.
10 Contact information
See http://www.ida.liu.se/~pelab/modelica/OpenModelica.html
*)
(**
** file: codegen.rml
** module: Codegen
** description: Generate C code from DAE (Flat Modelica) for Modelica
** functions. This code is compiled and linked to the simulation code or when
** functions are called from the interactive environment.
**
** Input: DAE
** Output: - (generated code through Print module)
** Uses: Print Inst ModUtil Util
**
**
** RCS: $Id$
**
**)
(* ------------------------------------------------------------------------- *)
module Codegen :
with "dae.rml"
with "print.rml"
type Ident = string
type ReturnType = string
type FunctionName = string
type ArgumentDeclaration = string
type VariableDeclaration = string
type InitStatement = string
type Statement = string
type CleanupStatement = string
type ReturnTypeStruct = string list
datatype CFunction = CFUNCTION of
ReturnType *
FunctionName *
ReturnTypeStruct *
(ArgumentDeclaration list) *
(VariableDeclaration list) *
(InitStatement list) *
(Statement list) *
(CleanupStatement list)
| CEXTFUNCTION of
ReturnType *
FunctionName *
ReturnTypeStruct *
ArgumentDeclaration list
relation generate_functions : DAE.DAElist => ()
relation generate_function_bodies : DAE.DAElist => ()
relation generate_function_headers : DAE.DAElist => ()
relation generate_algorithm: (DAE.Element, int)
=> (CFunction, int)
end
with "dump.rml"
with "debug.rml"
with "absyn.rml"
with "algorithm.rml"
with "classinf.rml"
with "exp.rml"
with "modutil.rml"
with "types.rml"
with "util.rml"
with "inst.rml"
(* ------------------------------------------------------------------------- *)
val c_empty_function = CFUNCTION("","",[],[],[],[],[],[])
relation c_make_function : (ReturnType,
FunctionName,
ReturnTypeStruct,
ArgumentDeclaration list)
=> CFunction =
axiom c_make_function (rt,fn,rts,ads) => CFUNCTION(rt,fn,rts,ads,[],[],[],[])
end
relation c_make_function_decl: (ReturnType, FunctionName, ReturnTypeStruct, ArgumentDeclaration list) => CFunction =
axiom c_make_function_decl(rt,fn,rts,ads) => CEXTFUNCTION(rt,fn,rts,ads)
end
relation c_add_variables : (CFunction, VariableDeclaration list) => CFunction =
rule list_append(vd,nvd) => vd'
--------------------------
c_add_variables (CFUNCTION(rt,fn,rts,ads,vd,is,st,cl),nvd)
=> CFUNCTION(rt,fn,rts,ads,vd',is,st,cl)
end
relation c_add_inits : (CFunction, InitStatement list) => CFunction =
rule list_append(is,nis) => is'
--------------------------
c_add_inits (CFUNCTION(rt,fn,rts,ads,vd,is,st,cl),nis)
=> CFUNCTION(rt,fn,rts,ads,vd,is',st,cl)
end
relation c_add_statements : (CFunction, Statement list) => CFunction =
rule list_append(st,nst) => st'
--------------------------
c_add_statements (CFUNCTION(rt,fn,rts,ads,vd,is,st,cl),nst)
=> CFUNCTION(rt,fn,rts,ads,vd,is,st',cl)
end
relation c_add_cleanups : (CFunction, CleanupStatement list) => CFunction =
rule list_append(cl,ncl) => cl'
--------------------------
c_add_cleanups (CFUNCTION(rt,fn,rts,ads,vd,is,st,cl),ncl)
=> CFUNCTION(rt,fn,rts,ads,vd,is,st,cl')
end
relation c_merge_fns : CFunction list => CFunction =
axiom c_merge_fns [] => c_empty_function
rule c_merge_fns r => cfn2 &
c_merge_fn(cfn1,cfn2) => cfn
----------------------
c_merge_fns cfn1::r => cfn
end
relation c_merge_fn : (CFunction, CFunction) => CFunction =
rule list_append(vd1,vd2) => vd &
list_append(is1,is2) => is &
list_append(st1,st2) => st &
list_append(cl1,cl2) => cl
-----------------------------
c_merge_fn (CFUNCTION(rt,fn,rts,ad,vd1,is1,st1,cl1),
CFUNCTION(_ , _, _ , _,vd2,is2,st2,cl2))
=> CFUNCTION(rt,fn,rts,ad,vd,is,st,cl)
end
relation c_move_statements_to_inits : CFunction => CFunction =
rule list_append(is,st) => is'
---------------------------
c_move_statements_to_inits CFUNCTION(rt,fn,rts,ad,vd,is,st,cl)
=> CFUNCTION(rt,fn,rts,ad,vd,is',[],cl)
end
relation c_print_functions : CFunction list => () =
axiom c_print_functions []
rule c_print_function(f) &
c_print_functions(r)
---------------------
c_print_functions f::r
end
relation c_print_function : CFunction => () =
rule Util.string_delimit_list(ad,", ") => args_str &
Util.string_append_list [rt," ",fn,"(",args_str,") {"] => stmt_str &
let i0 = 0 &
(* c_print_indented_list (rts,i0) => i1 & Print.print_buf "\n" & *)
c_print_indented (stmt_str,i0) => i2 & Print.print_buf "\n" &
c_print_indented_list (vd,i2) => i3 & Print.print_buf "\n" &
c_print_indented_list (is,i3) => i4 & Print.print_buf "\n" &
c_print_indented_list (st,i4) => i5 & Print.print_buf "\n" &
c_print_indented_list (cl,i5) => i6 & Print.print_buf "\n" &
c_print_indented ("}",i6) => i7 & Print.print_buf "\n"
-----------------------------------------------------------
c_print_function CFUNCTION(rt,fn,rts,ad,vd,is,st,cl)
rule Util.string_delimit_list(ads,", ") => args_str &
Util.string_append_list ["extern ", rt," ",fn,"(",args_str,");\n"] => stmt_str &
let i0 = 0 &
(* c_print_indented_list (rts,i0) => i1 & Print.print_buf "\n" & *)
c_print_indented (stmt_str,i0) => i2 & Print.print_buf "\n"
-----------------------------------------------------------
c_print_function CEXTFUNCTION(rt,fn,rts,ads)
rule Print.print_buf "# c_print_function_failed\n"
-----------
c_print_function _
end
relation c_print_function_headers : CFunction list => () =
axiom c_print_function_headers []
rule c_print_function_header(f) &
c_print_function_headers(r)
---------------------
c_print_function_headers f::r
end
relation c_print_function_header : CFunction => () =
rule Util.string_delimit_list(ad,", ") => args_str &
Util.string_append_list [rt," ",fn,"(",args_str,");"] => stmt_str &
let i0 = 0 &
c_print_indented_list (rts,i0) => i1 & Print.print_buf "\n" &
c_print_indented (stmt_str,i1) => i2 & Print.print_buf "\n"
-----------------------------------------------------------
c_print_function_header CFUNCTION(rt,fn,rts,ad,vd,is,st,cl)
rule Util.string_delimit_list(ads,", ") => args_str &
Util.string_append_list ["extern ", rt," ",fn,"(",args_str,");\n"] => stmt_str &
let i0 = 0 &
c_print_indented_list (rts,i0) => i1 & Print.print_buf "\n" &
c_print_indented (stmt_str,i1) => i2 & Print.print_buf "\n"
---------------------------------------------------
c_print_function_header CEXTFUNCTION(rt,fn,rts,ads)
rule Print.print_buf "# c_print_function_header failed\n"
-----------
c_print_function_header _
end
relation c_print_indented_list : (string list, int) => int =
axiom c_print_indented_list ([],i) => i
rule c_print_indented(f,i) => i' & Print.print_buf "\n" &
c_print_indented_list(r,i') => i''
-----------------
c_print_indented_list(f::r,i) => i''
end
relation c_print_indented : (string, int) => int =
rule string_list str => strl &
c_next_level(strl,i) => i' &
c_this_level(strl,i) => it &
c_print_indent it &
Print.print_buf str
-----------
c_print_indented (str,i) => i'
end
relation c_next_level : (char list, int) => int =
axiom c_next_level ([],i) => i
rule list_string [f] => "{" &
int_add(i,2) => i' &
c_next_level(r,i') => i''
-----
c_next_level (f::r,i) => i'' (* { *)
rule list_string [f] => "}" &
int_sub(i,2) => i' &
c_next_level(r,i') => i''
-----
c_next_level (f::r,i) => i'' (* } *)
rule c_next_level(r,i) => i'
-----
c_next_level (_::r,i) => i'
end
relation c_this_level : (char list, int) => int =
rule list_string [f] => "#"
----
c_this_level(f::_,_) => 0
rule list_string [f] => "}" &
int_sub(i,2) => i'
----
c_this_level(f::_,i) => i'
axiom c_this_level(_,i) => i
end
relation c_print_indent : int => () =
axiom c_print_indent 0
rule Print.print_buf " " &
int_sub(i,1) => i' &
c_print_indent i'
----------------
c_print_indent i
end
(* ------------------------------------------------------------------------- *)
(*
generate_functions
generate_functions_elist
generate_functions_elist2
generate_params_type
generate_function
generate_result_struct
generate_return_defs
generate_return_decls
generate_return_decl
is_array
is_first_in_array
subs_is_one
dae_exp_type
dae_type_str
dae_short_type_str
exp_short_type_str
exp_type_str
generate_type
generate_return_type
generate_array_type
generate_array_return_type
print_int
print_star
generate_tuple_type
generate_simple_type
array_type_string
generate_function_name
generate_function_arg
generate_function_body_tuple
generate_alloc_outvars
generate_alloc_outvar
prefix_cr
generate_algorithms
generate_algorithms2
generate_algorithm
generate_algorithm_statements
generate_algorithm_statement
generate_range_expressions
generate_else
generate_vars
generate_var
is_var_q
generate_var_q
generate_var_q2
generate_result_vars
generate_result_var
generate_equations
generate_exp
get_array_dim
generate_expressions
generate_expression
generate_binary
generate_temp_decl
generate_scalar_lhs_cref
generate_rhs_cref
subs_to_scalar
generate_scalar_rhs_cref
generate_array_rhs_cref
generate_index_spec
generate_indices_array
generate_indices
generate_index_array
generate_index
indent_strings
ident_cstr
comp_ref_cstr
generate_lbinary
generate_lunary
generate_relation
generate_matrix
generate_read_call_write
invar_names
generate_read
generate_write
is_rcw_output
is_rcw_input
*)
(* ------------------------------------------------------------------------- *)
relation generate_functions : DAE.DAElist => () =
rule generate_function_headers dae &
generate_function_bodies dae
------------------------------
generate_functions (dae as DAE.DAE(elist))
rule Print.print_buf "# generate_functions failed\n"
--------------------------------------
generate_functions _ => fail
end
relation generate_function_bodies : DAE.DAElist => () =
rule Debug.fprintln ("cgtr", "generate_function_bodies") &
generate_functions_elist elist => cfns &
Print.print_buf "\n/* body part */\n" &
c_print_functions cfns &
Print.print_buf "\n"
------------------------------
generate_function_bodies DAE.DAE(elist)
rule Print.print_buf "# generate_function_bodies failed\n"
--------------------------------------
generate_function_bodies _ => fail
end
relation generate_function_headers : DAE.DAElist => () =
rule Debug.fprintln ("cgtr", "generate_function_headers") &
generate_functions_elist elist => cfns &
Print.print_buf "/* header part */\n" &
Print.print_buf "#include \"modelica.h\"\n" & (* FIXME: needed? *)
c_print_function_headers cfns &
Print.print_buf "\n"
------------------------------
generate_function_headers DAE.DAE(elist)
rule Print.print_buf "# generate_function_headers failed\n"
--------------------------------------
generate_function_headers _ => fail
end
relation generate_functions_elist : DAE.Element list => CFunction list =
rule Debug.fprintln ("cgtr", "generate_functions_elist") &
Debug.fprintln ("cgtrdumpdae", "Dumping DAE:") &
Debug.fcall ("cgtrdumpdae", DAE.dump2, DAE.DAE(els)) &
DAE.get_matching(els,DAE.is_function) => fns &
generate_functions_elist2 fns => cfns
----------------------------
generate_functions_elist els => cfns
end
relation generate_functions_elist2 : DAE.Element list => CFunction list =
rule Debug.fprintln ("cgtr", "generate_functions_elist2")
----------------------------------------------------
generate_functions_elist2 [] => []
rule Debug.fprintln ("cgtr", "generate_functions_elist2") &
generate_function f => cfns1 &
generate_functions_elist2 rest => cfns2 &
list_append(cfns1,cfns2) => cfns
----------------------------
generate_functions_elist2 f :: rest => cfns
end
relation generate_params_type : Ident => string =
rule string_append (n, "_params") => s
---------------------------------
generate_params_type n => s
end
relation generate_function : DAE.Element => CFunction list =
rule generate_function_name fpath => fn_name_str &
Debug.fprintl ("cgtr", ["generating function ", fn_name_str, "\n"]) &
Debug.fprintln ("cgtrdumpdae3", "Dumping DAE:") &
Debug.fcall ("cgtrdumpdae3", DAE.dump2, DAE.DAE(dae)) &
DAE.get_output_vars dae => outvars &
DAE.get_input_vars dae => invars &
generate_result_struct (outvars,fpath) => struct_strs &
generate_return_type fpath => retstr &
Util.list_map(args, generate_function_arg) => arg_strs &
c_make_function(retstr, fn_name_str, struct_strs,arg_strs)=> head_cfn &
generate_function_body_tuple(fpath, dae, restype) => body_cfn &
c_merge_fn(head_cfn,body_cfn) => cfn &
generate_read_call_write(fn_name_str,outvars,retstr,invars) => rcw_fn
---------------------------
generate_function DAE.FUNCTION(fpath,
DAE.DAE(dae),
(Types.T_FUNCTION(args,restype),_))
=> [cfn,rcw_fn]
rule generate_function_name fpath => fn_name_str &
Debug.fprintl ("cgtr", ["generating external function ", fn_name_str, "\n"]) &
let DAE.EXTERNALDECL(extfnname,extargs,extretarg,lang) = extdecl &
Debug.fprintln ("cgtrdumpdae1", "Dumping DAE:") &
Debug.fcall ("cgtrdumpdae1", DAE.dump2, DAE.DAE(orgdae)) &
Inst.init_vars_modelica_output orgdae => dae &
Debug.fprintln ("cgtrdumpdae2", "Dumping DAE:") &
Debug.fcall ("cgtrdumpdae2", DAE.dump2, DAE.DAE(dae)) &
DAE.get_output_vars dae => outvars &
DAE.get_input_vars dae => invars &
DAE.get_bidir_vars dae => bivars &
generate_result_struct (outvars,fpath) => struct_strs &
generate_return_type fpath => retstructtype &
generate_ext_return_type (extretarg) => retstr &
generate_ext_function_name (extfnname, lang) => extfnname' &
generate_ext_function_args (extargs, lang) => arg_strs &
c_make_function_decl(retstr,extfnname',struct_strs,arg_strs) => func_decl &
generate_read_call_write_external(fn_name_str,outvars,retstructtype,invars,extdecl,bivars) => rcw_fn
---------------------------------------------------------------------------------------------
generate_function DAE.EXTFUNCTION(fpath,
DAE.DAE(orgdae),
(Types.T_FUNCTION(args,restype),_),
extdecl)
=> [func_decl,rcw_fn]
rule generate_functions_elist daelist => cfns
----------------------
generate_function DAE.COMP(n, DAE.DAE(daelist)) => cfns
rule Print.print_buf "# generate_function failed\n" &
DAE.dump2 DAE.DAE([comp])
------------------------------------
generate_function comp => fail
end
relation generate_ext_function_name : (string, string) => string =
axiom generate_ext_function_name (name, "C") => name
rule string_append (name, "_") => name'
----------------------------------
generate_ext_function_name (name, "FORTRAN 77") => name'
rule Print.print_error_buf "# Unknown language in external declaration\n"
---------------------------------------------------------
generate_ext_function_name (_,_) => fail
end
relation generate_result_struct : (DAE.Element list, Absyn.Path)
=> string list =
rule generate_return_type fpath => ptname &
generate_return_decls outvars => (var_strs,var_names) &
generate_return_defs (ptname,var_names,1) => defs &
indent_strings var_strs => var_strs' &
Util.string_append_list(["typedef struct ",ptname,"_s"]) => first_row &
Util.string_append_list(["} ",ptname,";"]) => last_row &
Util.list_flatten([defs,[first_row,"{"],var_strs',[last_row]]) => strs
-----------------------------------------
generate_result_struct (outvars, fpath) => strs
end
relation generate_return_defs : (string, string list, int) => string list =
axiom generate_return_defs (_,[],_) => []
rule int_string i => i_str &
Util.string_append_list(["#define ",tn,"_",i_str," ",f]) => f' &
int_add(i,1) => i' &
generate_return_defs (tn,r,i') => r'
----
generate_return_defs (tn,f::r,i) => f'::r'
end
relation generate_return_decls : DAE.Element list => (string list,
string list) =
axiom generate_return_decls [] => ([],[])
rule generate_return_decl first => ("",_) &
generate_return_decls rest => (rs,rd)
------------------
generate_return_decls first :: rest => (rs,rd)
rule generate_return_decl first => (fs,fd) &
generate_return_decls rest => (rs,rd)
------------------
generate_return_decls first :: rest => (fs::rs,fd::rd)
end
relation tmpprintinit : Exp.Exp option => string =
axiom tmpprintinit NONE => ""
rule Exp.print_exp_str e => str &
Util.string_append_list ([" /* ", str, " */"]) => str1
--------------------------
tmpprintinit SOME(e) => str1
end
relation generate_return_decl: DAE.Element => (string, string) =
(* changed DAE.VAR(..., NONE, inst_dims) to ..._, inst_dims) because removed second *)
(* rule *)
rule is_array var => is_a &
dae_type_str (typ,is_a) => typ_str &
comp_ref_cstr id => (id_str,_) &
Util.list_map(inst_dims,dim_string) => dim_strs &
Util.string_delimit_list(dim_strs,", ") => dims_str &
Util.string_append_list([typ_str," ",id_str,";",
" /* [",dims_str,"] */"])
=> decl_str' &
tmpprintinit initopt => expstr &
string_append(decl_str', expstr) => decl_str
---------------------------------------------------
generate_return_decl (var as DAE.VAR(id, DAE.VARIABLE, DAE.OUTPUT,
typ, initopt, inst_dims,start,flow,class,dae_var_attr)) => (decl_str,id_str)
(* not needed here, generate_return_decl is used to create the return struct
rule is_array id => is_a &
is_first_in_array id => true &
dae_type_str (typ,is_a) => typ_str &
comp_ref_cstr id => (id_str,_) &
Util.string_append_list([typ_str," ",id_str,";"]) => decl_str' &
Print.print_buf "# default/init values not implemented yet: " &
Exp.print_exp_str e => str & Print.print_buf str &
Print.print_buf "\n" &
Util.string_append_list ([decl_str', " /* ", str, " */"]) => decl_str
-----------
generate_return_decl (DAE.VAR(id,
DAE.VARIABLE,
DAE.OUTPUT,
typ,
SOME(e),
_,_))
=> (decl_str,id_str)
*)
axiom generate_return_decl (_) => ("","")
end
relation is_array : DAE.Element => bool =
(*
axiom is_array DAE.VAR(cr,vk,vd,ty,_,[],st,fl,cl) => false
axiom is_array DAE.VAR(cr,vk,vd,ty,_,_::_,st,fl,cl) => true
*)
rule Debug.fcall("isarrdb", DAE.dump2, DAE.DAE[el])
-----------------------------------
is_array (el as DAE.VAR(cr,vk,vd,ty,_,[],st,fl,cl,dae_var_attr)) => false
rule Debug.fcall("isarrdb", DAE.dump2, DAE.DAE[el])
-----------------------------------
is_array (el as DAE.VAR(cr,vk,vd,ty,_,_::_,st,fl,cl,dae_var_attr)) => true
rule Print.print_buf "#-- is_array failed\n" &
Print.print_buf "#-- Not a var?" &
DAE.dump2 DAE.DAE([el])
--------------------------
is_array el => fail
end
relation is_array_or_string : DAE.Element => bool =
rule is_array var => true &
Print.print_error_buf "# array of strings is probably not supported yet\n"
------------------------------------------------------------------
is_array_or_string (var as DAE.VAR(cr,vk,vd,DAE.STRING,_,_,st,fl,cl,dae_var_attr)) => fail
rule is_array var => true
-------------------
is_array_or_string (var as DAE.VAR(cr,vk,vd,ty,_,_,st,fl,cl,dae_var_attr)) => true
axiom is_array_or_string (var as DAE.VAR(cr,vk,vd,DAE.STRING,_,_,st,fl,cl,dae_var_attr)) => true
rule Print.print_buf "# is_array_or_string failed\n" &
DAE.dump2 DAE.DAE([el])
------------------------------------------------------------------
is_array_or_string el => fail
end
(*
relation is_first_in_array : Exp.ComponentRef => bool =
axiom is_first_in_array Exp.CREF_IDENT(_,[]) => true
rule subs_is_one subs => b
---------------------
is_first_in_array Exp.CREF_IDENT(_,subs) => b
rule is_first_in_array cref => b
---------------------------
is_first_in_array Exp.CREF_QUAL(_,[],cref) => b
rule subs_is_one subs => b1 &
is_first_in_array cref => b2 &
bool_and(b1,b2) => b
------------------------------
is_first_in_array Exp.CREF_QUAL(_,subs,cref) => b
end
relation subs_is_one : Exp.Subscript list => bool =
axiom subs_is_one [] => true
rule subs_is_one r => b
------------------
subs_is_one Exp.INDEX(Exp.ICONST(1))::r => b
axiom subs_is_one _::_ => false
end
*)
relation dae_exp_type : DAE.Type => Exp.Type =
axiom dae_exp_type DAE.INT => Exp.INT
axiom dae_exp_type DAE.REAL => Exp.REAL
axiom dae_exp_type DAE.STRING => Exp.STRING
axiom dae_exp_type DAE.BOOL => Exp.BOOL
axiom dae_exp_type DAE.ENUM => Exp.ENUM
axiom dae_exp_type _ => Exp.OTHER
end
relation dae_type_str : (DAE.Type, bool) => string =
rule dae_exp_type t => t' &
exp_type_str (t',a) => str
--------------------------
dae_type_str (t,a) => str
end
relation dae_short_type_str : DAE.Type => string =
rule dae_exp_type t => t' &
exp_short_type_str t' => str
--------------------------
dae_short_type_str t => str
end
relation exp_short_type_str : Exp.Type => string =
axiom exp_short_type_str Exp.INT => "integer"
axiom exp_short_type_str Exp.REAL => "real"
axiom exp_short_type_str Exp.STRING => "string"
axiom exp_short_type_str Exp.BOOL => "boolean"
axiom exp_short_type_str Exp.OTHER => "OTHER"
axiom exp_short_type_str Exp.ENUM => "ENUM_NOT_IMPLEMENTED"
end
relation exp_type_str : (Exp.Type, bool) => string =
rule exp_short_type_str t => tstr &
string_append("modelica_",tstr) => str
-----------
exp_type_str (t, false) => str
rule exp_short_type_str t => tstr &
string_append(tstr,"_array") => str
-----------
exp_type_str (t, true) => str
end
relation generate_type : Types.Type => string =
rule Debug.fprintln ("cgtr", "generate_type") &
generate_tuple_type tys => ty_str
---------------------------
generate_type((Types.T_TUPLE(tys),_)) => ty_str
rule Debug.fprintln ("cgtr", "generate_type") &
Types.flatten_array_type tys => (arrayty, dims) &
generate_array_type (arrayty, dims) => ty_str
-----------------------------------
generate_type (tys as (Types.T_ARRAY(_,_),_)) => ty_str
axiom generate_type((Types.T_INTEGER(_),_)) => "modelica_integer"
axiom generate_type((Types.T_REAL(_),_)) => "modelica_real"
axiom generate_type((Types.T_STRING(_),_)) => "modelica_string"
axiom generate_type((Types.T_BOOL(_),_)) => "modelica_boolean"
rule Print.print_buf "#-- generate_type failed: " &
Types.print_type ty & Print.print_buf "\n"
--------------------------------
generate_type ty => fail
end
relation generate_type_external : Types.Type => string =
axiom generate_type_external((Types.T_INTEGER(_),_)) => "int"
axiom generate_type_external((Types.T_REAL(_),_)) => "double"
axiom generate_type_external((Types.T_STRING(_),_)) => "const char*"
axiom generate_type_external((Types.T_BOOL(_),_)) => "int"
rule generate_type_external ty => str
--------------------------------
generate_type_external((Types.T_ARRAY(dim,ty),_)) => str
rule Print.print_buf "#-- generate_type_external failed: " &
Types.print_type ty & Print.print_buf "\n"
--------------------------------
generate_type_external ty => fail
end
relation generate_type_internal : Types.Type => string =
(* for the basic types, only those must match *)
rule Types.basic_type ty => true &
generate_type_internal_namepart ty => tystr &
string_append("modelica_",tystr) => str
---------------------------------------
generate_type_internal ty => str
rule Types.is_array ty => true &
Types.array_element_type ty => elty &
generate_type_internal elty => tystr &
string_append (tystr,"_array") => str
--------------------------------
generate_type_internal ty => str
rule Print.print_buf "#-- generate_type_internal failed: " &
Types.print_type ty & Print.print_buf "\n"
--------------------------------
generate_type_internal ty => fail
end
(* for the basic types, only those must match *)
relation generate_type_internal_namepart : Types.Type => string =
axiom generate_type_internal_namepart((Types.T_INTEGER(_),_)) => "integer"
axiom generate_type_internal_namepart((Types.T_REAL(_),_)) => "real"
axiom generate_type_internal_namepart((Types.T_STRING(_),_)) => "string"
axiom generate_type_internal_namepart((Types.T_BOOL(_),_)) => "boolean"
axiom generate_type_internal_namepart((Types.T_ENUM,_)) => "T_ENUM_NOT_IMPLEMENTED"
end
relation generate_return_type : Absyn.Path => string =
rule generate_function_name fpath => fstr &
string_append (fstr, "_rettype" ) => res
--------------------------
generate_return_type fpath => res
end
relation generate_array_type : (Types.Type, int list) => string =
rule array_type_string ty => str
--------------------------
generate_array_type (ty, dims) => str
end
relation generate_array_return_type : (Types.Type, int list) => string =
rule array_type_string ty => ty_str
-------------------------------
generate_array_return_type (ty, dims) => ty_str
end
relation print_int : int => () =
rule int_string i => str &
Print.print_buf str
---------