/
Codegen.mo
9999 lines (9413 loc) · 350 KB
/
Codegen.mo
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
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2008, Linköpings University,
* Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THIS OSMC PUBLIC
* LICENSE (OSMC-PL). ANY USE, REPRODUCTION OR DISTRIBUTION OF
* THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THE OSMC
* PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköpings University, either from the above address,
* from the URL: http://www.ida.liu.se/projects/OpenModelica
* and in the OpenModelica distribution.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
* OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
package Codegen
" file: Codegen.mo
package: 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$
-------------------------------------------------------------------------"
public import Absyn;
public import DAE;
public import MetaUtil;
public import Print;
public import RTOpts;
public import SCode;
public
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 = list<String>;
type Include = String;
type Lib = String;
public
uniontype CFunction
record CFUNCTION
ReturnType returnType;
FunctionName functionName;
ReturnTypeStruct returnTypeStruct;
list<ArgumentDeclaration> argumentDeclarationLst;
list<VariableDeclaration> variableDeclarationLst;
list<InitStatement> initStatementLst;
list<Statement> statementLst;
list<CleanupStatement> cleanupStatementLst;
end CFUNCTION;
record CEXTFUNCTION
ReturnType returnType;
FunctionName functionName;
ReturnTypeStruct returnTypeStruct;
list<ArgumentDeclaration> argumentDeclarationLst;
list<Include> includeLst;
list<Lib> libLst;
end CEXTFUNCTION;
end CFunction;
public uniontype Context
record CONTEXT
CodeContext codeContext "The context code is generated in, either simulation or function";
ExpContext expContext "The context expressions are generated in, either normal or external calls";
LoopContext loopContext = LoopContext.NO_LOOP "The chain of loops containing the generated statement";
end CONTEXT;
end Context;
public
uniontype CodeContext "Which context is the code generated in."
record SIMULATION "when generating simulation code"
Boolean genDiscrete;
end SIMULATION;
record FUNCTION "when generating function code" end FUNCTION;
end CodeContext;
public
uniontype ExpContext
record NORMAL "Normal expression generation" end NORMAL;
record EXP_EXTERNAL "for expressions in external calls" end EXP_EXTERNAL;
end ExpContext;
public
uniontype LoopContext
record NO_LOOP end NO_LOOP;
record IN_FOR_LOOP LoopContext parent; end IN_FOR_LOOP;
record IN_WHILE_LOOP LoopContext parent; end IN_WHILE_LOOP;
end LoopContext;
protected import Debug;
protected import Algorithm;
protected import ClassInf;
protected import Exp;
protected import ModUtil;
protected import Util;
protected import Inst;
protected import Interactive;
protected import System;
protected import Error;
protected import Types;
protected import DAEUtil;
public constant CFunction cEmptyFunction=CFUNCTION("","",{},{},{},{},{},{}) " empty function ";
public constant Context
funContext = CONTEXT(FUNCTION(),NORMAL(),NO_LOOP()),
simContext = CONTEXT(SIMULATION(true),NORMAL(),NO_LOOP()),
extContext = CONTEXT(SIMULATION(true),EXP_EXTERNAL(),NO_LOOP());
public function cMakeFunction
"function: cMakeFunction
Helper function to generate_function. Creates a C-function from a
ReturnType, FunctionName, ReturnTypeStruct and a list of ArgumentDeclarations"
input ReturnType inReturnType;
input FunctionName inFunctionName;
input ReturnTypeStruct inReturnTypeStruct;
input list<ArgumentDeclaration> inArgumentDeclarationLst;
output CFunction outCFunction;
algorithm
outCFunction:=
matchcontinue (inReturnType,inFunctionName,inReturnTypeStruct,inArgumentDeclarationLst)
local
Lib rt,fn;
list<Lib> rts,ads;
case (rt,fn,rts,ads) then CFUNCTION(rt,fn,rts,ads,{},{},{},{});
end matchcontinue;
end cMakeFunction;
protected function cMakeFunctionDecl
"function: cMakeFunctionDecl
Helper function to generate_function. Generates a C function declaration.
I.e. without body of the function."
input ReturnType inReturnType;
input FunctionName inFunctionName;
input ReturnTypeStruct inReturnTypeStruct;
input list<ArgumentDeclaration> inArgumentDeclarationLst;
input list<Include> inIncludeLst;
input list<Lib> inLibLst;
output CFunction outCFunction;
algorithm
outCFunction:=
matchcontinue (inReturnType,inFunctionName,inReturnTypeStruct,inArgumentDeclarationLst,inIncludeLst,inLibLst)
local
Lib rt,fn;
list<Lib> rts,ads,incl,libs;
case (rt,fn,rts,ads,incl,libs) then CEXTFUNCTION(rt,fn,rts,ads,incl,libs);
end matchcontinue;
end cMakeFunctionDecl;
public function cAddVariables
"function: cAddVariables
Add local variable declarations to a CFunction."
input CFunction inCFunction;
input list<VariableDeclaration> inVariableDeclarationLst;
output CFunction outCFunction;
algorithm
outCFunction:=
matchcontinue (inCFunction,inVariableDeclarationLst)
local
list<Lib> vd_1,rts,ads,vd,is,st,cl,nvd;
Lib rt,fn;
case (CFUNCTION(returnType = rt,functionName = fn,returnTypeStruct = rts,argumentDeclarationLst = ads,variableDeclarationLst = vd,initStatementLst = is,statementLst = st,cleanupStatementLst = cl),nvd)
equation
vd_1 = listAppend(vd, nvd);
then
CFUNCTION(rt,fn,rts,ads,vd_1,is,st,cl);
end matchcontinue;
end cAddVariables;
public function cAddInits
"function: cAddInits
Add initialization statements to a CFunction. They will be ommitted before
the actual code of the function but after the local variable declarations."
input CFunction inCFunction;
input list<InitStatement> inInitStatementLst;
output CFunction outCFunction;
algorithm
outCFunction:=
matchcontinue (inCFunction,inInitStatementLst)
local
list<Lib> is_1,rts,ads,vd,is,st,cl,nis;
Lib rt,fn;
case (CFUNCTION(returnType = rt,functionName = fn,returnTypeStruct = rts,argumentDeclarationLst = ads,variableDeclarationLst = vd,initStatementLst = is,statementLst = st,cleanupStatementLst = cl),nis)
equation
is_1 = listAppend(is, nis);
then
CFUNCTION(rt,fn,rts,ads,vd,is_1,st,cl);
end matchcontinue;
end cAddInits;
public function cPrependStatements
"function: cPrependStatements
Prepends statements to a CFunction."
input CFunction inCFunction;
input list<Statement> inStatementLst;
output CFunction outCFunction;
algorithm
outCFunction:=
matchcontinue (inCFunction,inStatementLst)
local
list<Lib> st_1,rts,ads,vd,is,st,cl,nst;
Lib rt,fn;
case (CFUNCTION(returnType = rt,functionName = fn,returnTypeStruct = rts,argumentDeclarationLst = ads,variableDeclarationLst = vd,initStatementLst = is,statementLst = st,cleanupStatementLst = cl),nst)
equation
st_1 = listAppend(nst, st);
then
CFUNCTION(rt,fn,rts,ads,vd,is,st_1,cl);
end matchcontinue;
end cPrependStatements;
public function cAddStatements
"function: cAddStatements
Adds statements to a CFunction."
input CFunction inCFunction;
input list<Statement> inStatementLst;
output CFunction outCFunction;
algorithm
outCFunction:=
matchcontinue (inCFunction,inStatementLst)
local
list<Lib> st_1,rts,ads,vd,is,st,cl,nst;
Lib rt,fn;
case (CFUNCTION(returnType = rt,
functionName = fn,
returnTypeStruct = rts,
argumentDeclarationLst = ads,
variableDeclarationLst = vd,
initStatementLst = is,
statementLst = st,
cleanupStatementLst = cl),nst)
equation
st_1 = listAppend(st, nst);
then
CFUNCTION(rt,fn,rts,ads,vd,is,st_1,cl);
end matchcontinue;
end cAddStatements;
public function cAddCleanups
"function: cAddCleanups
Add \"cleanup\" statements to a CFunction.
They will be ommited last, before
the return statement of the function."
input CFunction inCFunction;
input list<CleanupStatement> inCleanupStatementLst;
output CFunction outCFunction;
algorithm
outCFunction:=
matchcontinue (inCFunction,inCleanupStatementLst)
local
list<Lib> cl_1,rts,ads,vd,is,st,cl,ncl;
Lib rt,fn;
case (CFUNCTION(returnType = rt,functionName = fn,returnTypeStruct = rts,argumentDeclarationLst = ads,variableDeclarationLst = vd,initStatementLst = is,statementLst = st,cleanupStatementLst = cl),ncl)
equation
cl_1 = listAppend(cl, ncl);
then
CFUNCTION(rt,fn,rts,ads,vd,is,st,cl_1);
end matchcontinue;
end cAddCleanups;
public function cMergeFns
"function: cMergeFns
Takes a list of functions and merges them together.
The function name, returntype and argument lists are taken from the
first CFunction in the list."
input list<CFunction> inCFunctionLst;
output CFunction outCFunction;
algorithm
outCFunction:=
matchcontinue (inCFunctionLst)
local
CFunction cfn2,cfn,cfn1;
list<CFunction> r;
case {} then cEmptyFunction;
case (cfn1 :: r)
equation
cfn2 = cMergeFns(r);
cfn = cMergeFn(cfn1, cfn2);
then
cfn;
end matchcontinue;
end cMergeFns;
protected function cMergeFn
"function: cMergeFn
Merges two functions into one. The function name, returntype and
argument lists are taken from the first CFunction."
input CFunction inCFunction1;
input CFunction inCFunction2;
output CFunction outCFunction;
algorithm
outCFunction:=
matchcontinue (inCFunction1,inCFunction2)
local
list<Lib> vd,is,st,cl,rts,ad,vd1,is1,st1,cl1,vd2,is2,st2,cl2;
Lib rt,fn;
case (CFUNCTION(returnType = rt,functionName = fn,returnTypeStruct = rts,argumentDeclarationLst = ad,variableDeclarationLst = vd1,initStatementLst = is1,statementLst = st1,cleanupStatementLst = cl1),CFUNCTION(variableDeclarationLst = vd2,initStatementLst = is2,statementLst = st2,cleanupStatementLst = cl2))
equation
vd = listAppend(vd1, vd2);
is = listAppend(is1, is2);
st = listAppend(st1, st2);
cl = listAppend(cl1, cl2);
then
CFUNCTION(rt,fn,rts,ad,vd,is,st,cl);
end matchcontinue;
end cMergeFn;
protected function cMoveStatementsToInits
"function: cMoveStatementsToInits
Moves all statements of the body to initialization statements."
input CFunction inCFunction;
output CFunction outCFunction;
algorithm
outCFunction:=
matchcontinue (inCFunction)
local
list<Lib> is_1,rts,ad,vd,is,st,cl;
Lib rt,fn;
case CFUNCTION(returnType = rt,functionName = fn,returnTypeStruct = rts,argumentDeclarationLst = ad,variableDeclarationLst = vd,initStatementLst = is,statementLst = st,cleanupStatementLst = cl)
equation
is_1 = listAppend(is, st);
then
CFUNCTION(rt,fn,rts,ad,vd,is_1,{},cl);
end matchcontinue;
end cMoveStatementsToInits;
public function cPrintFunctionsStr
"function: cPrintFunctionsStr
Prints CFunction list to a string"
input list<CFunction> fs;
output String res;
Lib s;
algorithm
s := Print.getString();
Print.clearBuf();
cPrintFunctions(fs);
res := Print.getString();
Print.clearBuf();
Print.printBuf(s);
end cPrintFunctionsStr;
protected function cPrintFunctions
"function: cPrintFunctions
Prints CFunction list to Print buffer."
input list<CFunction> inCFunctionLst;
algorithm
_:=
matchcontinue (inCFunctionLst)
local
CFunction f;
list<CFunction> r;
case {} then ();
case (f :: r)
equation
cPrintFunction(f);
cPrintFunctions(r);
then
();
end matchcontinue;
end cPrintFunctions;
public function cPrintDeclarations "
Prints only the local variable declarations of a function to a string"
input CFunction inCFunction;
output String outString;
algorithm
outString:=
matchcontinue (inCFunction)
local
Integer i5;
Lib str,res,rt,fn;
list<Lib> rts,ad,vd,is,st,cl;
case CFUNCTION(returnType = rt,
functionName = fn,
returnTypeStruct = rts,
argumentDeclarationLst = ad,
variableDeclarationLst = vd,
initStatementLst = is,
statementLst = st,
cleanupStatementLst = cl)
equation
(i5,str) = cPrintIndentedListStr(vd, 2);
res = addNewlineIfNotEmpty(str);
then
res;
end matchcontinue;
end cPrintDeclarations;
protected function addNewlineIfNotEmpty
input String inStr;
output String outStr;
algorithm
outStr := matchcontinue (inStr)
local String sTrim;
case (inStr)
equation
sTrim = System.trim(inStr, " ");
false = stringEqual(sTrim, "");
then (inStr +& "\n");
case (inStr) then "";
end matchcontinue;
end addNewlineIfNotEmpty;
public function cPrintStatements
"function: cPrintStatements
Only prints the statements of a function to a string"
input CFunction inCFunction;
output String outString;
algorithm
outString:=
matchcontinue (inCFunction)
local
Integer i5;
Lib str,res,rt,fn;
list<Lib> rts,ad,vd,is,st,cl;
case CFUNCTION(returnType = rt,
functionName = fn,
returnTypeStruct = rts,
argumentDeclarationLst = ad,
variableDeclarationLst = vd,
initStatementLst = is,
statementLst = st,
cleanupStatementLst = cl)
equation
(i5,str) = cPrintIndentedListStr(st, 2);
res = addNewlineIfNotEmpty(str);
then
res;
end matchcontinue;
end cPrintStatements;
protected function cPrintFunction
"function: cPrintFunction
Prints a CFunction to Print buffer."
input CFunction inCFunction;
algorithm
_:=
matchcontinue (inCFunction)
local
Lib args_str,stmt_str,rt,fn;
Integer i0,i2,i3,i4,i5,i6,i7;
list<Lib> rts,ad,vd,is,st,cl,ads;
case CFUNCTION(returnType = rt,
functionName = fn,
returnTypeStruct = rts,
argumentDeclarationLst = ad,
variableDeclarationLst = vd,
initStatementLst = is,
statementLst = st,
cleanupStatementLst = cl)
equation
args_str = Util.stringDelimitList(ad, ", ");
stmt_str = Util.stringAppendList({rt," ",fn,"(",args_str,")\n{"});
i0 = 0;
i2 = cPrintIndented(stmt_str, i0);
Print.printBuf("\n");
// variable declarations
i3 = cPrintIndentedList(vd, i2);
//Print.printBuf("\n");
// init statements
i4 = cPrintIndentedList(is, i3);
//Print.printBuf("\n");
// statements
i5 = cPrintIndentedList(st, i4);
//Print.printBuf("\n");
// cleanup statements
i6 = cPrintIndentedList(cl, i5);
// Print.printBuf("\n");
i7 = cPrintIndented("}", i6);
Print.printBuf("\n\n");
then
();
case CEXTFUNCTION(returnType = rt,functionName = "",returnTypeStruct = rts,argumentDeclarationLst = ads)
then
();
case CEXTFUNCTION(returnType = rt,functionName = fn,returnTypeStruct = rts,argumentDeclarationLst = ads)
equation
args_str = Util.stringDelimitList(ads, ", ");
stmt_str = Util.stringAppendList({"extern ",rt," ",fn,"(",args_str,");\n"});
i0 = 0;
i2 = cPrintIndented(stmt_str, i0) " c_print_indented_list (rts,i0) => i1 & Print.printBuf \"\\n\" &" ;
Print.printBuf("\n");
then
();
case _
equation
Debug.fprint("failtrace", "# Codegen.cPrintFunction_failed\n");
then
();
end matchcontinue;
end cPrintFunction;
protected function cPrintFunctionIncludes
"function: cPrintFunctionIncludes
Prints the function declaration, i.e. the header
information of a CFunction list to the Print buffer."
input list<CFunction> inCFunctionLst;
output list<String> outStringLst;
algorithm
outStringLst:=
matchcontinue (inCFunctionLst)
local
list<Lib> libs1,libs2,libs;
CFunction f;
list<CFunction> r;
case {} then {}; /* libs */
case (f :: r)
equation
libs1 = cPrintFunctionInclude(f);
libs2 = cPrintFunctionIncludes(r);
libs = listAppend(libs1, libs2);
then
libs;
end matchcontinue;
end cPrintFunctionIncludes;
protected function cPrintFunctionInclude
"function: cPrintFunctionIncludes
Prints the includes for a function."
input CFunction inCFunction;
output list<String> outStringLst;
algorithm
outStringLst:=
matchcontinue (inCFunction)
local
Lib str;
list<Lib> includes,libs;
case (CEXTFUNCTION(includeLst = includes,libLst = libs)) /* libs */
equation
Print.printBuf("#ifdef __cplusplus\n");
Print.printBuf("extern \"C\" {\n");
Print.printBuf("#endif\n");
str = Util.stringDelimitList(includes, "\n");
Print.printBuf(str);
Print.printBuf("\n");
Print.printBuf("#ifdef __cplusplus\n");
Print.printBuf("}\n");
Print.printBuf("#endif\n");
then
libs;
case (_) then {};
end matchcontinue;
end cPrintFunctionInclude;
protected function cPrintFunctionHeaders
"function: cPrintFunctionHeaders
Prints the function declaration, i.e. the header
information of a CFunction list to the Print buffer."
input list<CFunction> inCFunctionLst;
algorithm
_:=
matchcontinue (inCFunctionLst)
local
CFunction f;
list<CFunction> r;
case {} then ();
case (f :: r)
equation
cPrintFunctionHeader(f);
cPrintFunctionHeaders(r);
then
();
end matchcontinue;
end cPrintFunctionHeaders;
protected function cPrintFunctionHeader
"function: cPrintFunctionHeaders
Prints the function declaration, i.e. the header
information of a CFunction to the Print buffer."
input CFunction inCFunction;
algorithm
_:=
matchcontinue (inCFunction)
local
Lib args_str,stmt_str,rt,fn;
Integer i0,i1,i2;
list<Lib> rts,ad,vd,is,st,cl,ads;
case CFUNCTION(returnType = rt,
functionName = fn,
returnTypeStruct = rts,
argumentDeclarationLst = ad,
variableDeclarationLst = vd,
initStatementLst = is,
statementLst = st,
cleanupStatementLst = cl)
equation
args_str = Util.stringDelimitList(ad, ", ");
stmt_str = Util.stringAppendList({"DLLExport \n", rt," ",fn,"(",args_str,");"});
i0 = 0;
i1 = cPrintIndentedList(rts, i0);
Print.printBuf("\n");
i2 = cPrintIndented(stmt_str, i1);
Print.printBuf("\n");
then
();
case CEXTFUNCTION(returnType = rt,functionName = "",returnTypeStruct = rts,argumentDeclarationLst = ads)
equation
i0 = 0;
i1 = cPrintIndentedList(rts, i0);
Print.printBuf("\n");
then
();
case CEXTFUNCTION(returnType = rt,functionName = fn,returnTypeStruct = rts,argumentDeclarationLst = ads)
equation
args_str = Util.stringDelimitList(ads, ", ");
stmt_str = Util.stringAppendList({"extern ",rt," ",fn,"(",args_str,");\n"});
i0 = 0;
i1 = cPrintIndentedList(rts, i0);
Print.printBuf("\n");
i2 = cPrintIndented(stmt_str, i1);
Print.printBuf("\n");
then
();
case _
equation
Debug.fprint("failtrace", "# Codegen.cPrintFunctionHeader failed\n");
then
();
end matchcontinue;
end cPrintFunctionHeader;
protected function cPrintIndentedList
"function: cPrintIndentedList
Helper function. prints a list of strings
indented with a number of indentation levels."
input list<String> inStringLst;
input Integer inInteger;
output Integer outInteger;
algorithm
outInteger:=
matchcontinue (inStringLst,inInteger)
local
Integer i,i_1,i_2;
Lib f;
list<Lib> r;
case ({},i) then i; /* indentation level updated indentation level */
case ((f :: r),i)
equation
i_1 = cPrintIndented(f, i);
Print.printBuf("\n");
i_2 = cPrintIndentedList(r, i_1);
then
i_2;
end matchcontinue;
end cPrintIndentedList;
protected function cPrintIndented
"function: cPrintIndented
Prints a string adding an indentation level. If the string
contains C-code that opens or closes a indentation level,
the indentation level is updated accordingly."
input String str;
input Integer i;
output Integer i_1;
list<Lib> strl;
Integer i_1,it;
algorithm
strl := string_list_string_char(str);
i_1 := cNextLevel(strl, i);
it := cThisLevel(strl, i);
cPrintIndent(it);
Print.printBuf(str);
end cPrintIndented;
protected function cPrintIndentedListStr
"function: cPrintIndentedListStr
Helper function."
input list<String> inStringLst;
input Integer inInteger;
output Integer outInteger;
output String outString;
algorithm
(outInteger,outString):=
matchcontinue (inStringLst,inInteger)
local
Integer i,i_1,i_2;
Lib s1,s2,res,f;
list<Lib> r;
case ({},i) then (i,""); /* indentation level updated indentation level */
case ((f :: r),i)
equation
(i_1,s1) = cPrintIndentedStr(f, i);
(i_2,s2) = cPrintIndentedListStr(r, i_1);
s1 = addNewlineIfNotEmpty(s1);
res = Util.stringAppendList({s1, s2});
then
(i_2,res);
end matchcontinue;
end cPrintIndentedListStr;
protected function cPrintIndentedStr
"function: cPrintIndentedStr
See cPrintIndented"
input String inString;
input Integer inInteger;
output Integer outInteger;
output String outString;
algorithm
(outInteger,outString):=
matchcontinue (inString,inInteger)
local
list<Lib> strl;
Integer i_1,it,i;
Lib it_str,res,str;
case (str,i)
equation
strl = string_list_string_char(str);
i_1 = cNextLevel(strl, i);
it = cThisLevel(strl, i);
it_str = cPrintIndentStr(it);
res = stringAppend(it_str, str);
then
(i_1,res);
end matchcontinue;
end cPrintIndentedStr;
protected function cNextLevel
"function cNextLevel
Helper function to cPrintIndented."
input list<String> inStringLst;
input Integer inInteger;
output Integer outInteger;
algorithm
outInteger:=
matchcontinue (inStringLst,inInteger)
local
Integer i,i_1,i_2;
Lib f;
list<Lib> r;
case ({},i) then i;
case ((f :: r),i) /* { */
equation
"{" = string_char_list_string({f});
i_1 = i + 2;
i_2 = cNextLevel(r, i_1);
then
i_2;
case ((f :: r),i) /* } */
equation
"}" = string_char_list_string({f});
i_1 = i - 2;
i_2 = cNextLevel(r, i_1);
then
i_2;
case ((_ :: r),i)
equation
i_1 = cNextLevel(r, i);
then
i_1;
end matchcontinue;
end cNextLevel;
protected function cThisLevel
"function cNextLevel
Helper function to cPrintIndented."
input list<String> inStringLst;
input Integer inInteger;
output Integer outInteger;
algorithm
outInteger:=
matchcontinue (inStringLst,inInteger)
local
Lib f;
Integer i;
case ((f :: _),_)
equation
"#" = string_char_list_string({f});
then
0;
case ((f :: _),i)
equation
"}" = string_char_list_string({f});
then
i - 2;
case (_,i) then i;
end matchcontinue;
end cThisLevel;
protected function cPrintIndent
"function cPrintIndent
Helper function to cPrintIndented."
input Integer inInteger;
algorithm
_:=
matchcontinue (inInteger)
local Integer i_1,i;
case 0 then ();
case i
equation
Print.printBuf(" ");
i_1 = i - 1;
cPrintIndent(i_1);
then
();
end matchcontinue;
end cPrintIndent;
protected function cPrintIndentStr
"function cPrintIndentStr
Helper function to cPrintIndentedStr."
input Integer inInteger;
output String outString;
algorithm
outString:=
matchcontinue (inInteger)
local
list<Lib> lst;
Lib res;
Integer i;
case 0 then "";
case i
equation
lst = Util.listFill(" ", i);
res = Util.stringAppendList(lst);
then
res;
end matchcontinue;
end cPrintIndentStr;
public function generateFunctions
"function: generateFunctions
Generates code for all functions in a DAE and prints on Print buffer.
A list of libs for the external functions is returned."
input DAE.DAElist inDAElist;
input list<DAE.Type> metarecordTypes;
output list<String> outStringLst;
algorithm
outStringLst:=
matchcontinue (inDAElist,metarecordTypes)
local
list<Lib> libs;
DAE.DAElist dae;
list<DAE.Element> elist;
list<String> rt;
case ((dae as DAE.DAE(elementLst = elist)),metarecordTypes) /* libs */
local String s;
equation
Print.printBuf("#ifdef __cplusplus\n");
Print.printBuf("extern \"C\" {\n");
Print.printBuf("#endif\n");
(libs,rt) = generateFunctionHeaders(dae);
generateRecordDeclarationsPrintList(metarecordTypes,rt);
generateFunctionBodies(dae);
Print.printBuf("\n");
Print.printBuf("#ifdef __cplusplus\n");
Print.printBuf("}\n");
Print.printBuf("#endif\n");
then
libs;
case (_,_)
equation
Debug.fprint("failtrace", "# Codegen.generateFunctions failed\n");
then
fail();
end matchcontinue;
end generateFunctions;
public function generateFunctionBodies
"function: generateFunctionBodies
Generates the function bodies of a DAE list."
input DAE.DAElist inDAElist;
algorithm
_:=
matchcontinue (inDAElist)
local
list<CFunction> cfns;
list<DAE.Element> elist;
case DAE.DAE(elementLst = elist)
equation
Debug.fprintln("cgtr", "generate_function_bodies");
(cfns as (_::_),_) = generateFunctionsElist(elist,{});
Print.printBuf("\n/* Body */\n");
cPrintFunctions(cfns);
Print.printBuf("/* End Body */\n");
then
();
case DAE.DAE(elementLst = elist)
equation
Debug.fprintln("cgtr", "generate_function_bodies (nothing)");
({},_) = generateFunctionsElist(elist,{});
then
();
case _
equation
Debug.fprint("failtrace", "# Codegen.generateFunctionBodies failed\n");
then
fail();
end matchcontinue;
end generateFunctionBodies;
public function generateFunctionHeaders
"function: generateFunctionHeaders
Generates the headers of the functions in a DAE list."
input DAE.DAElist inDAElist;
output list<String> outStringLst;
output list<String> outRecordLst;
algorithm
(outStringLst,outRecordLst) :=
matchcontinue (inDAElist)
local
list<CFunction> cfns;
list<Lib> libs, funcRef;
list<DAE.Element> elist;
case (DAE.DAE(elementLst = elist))
equation
Debug.fprintln("cgtr", "generate_function_headers");
(cfns as (_::_),outRecordLst) = generateFunctionsElist(elist, {});
Print.printBuf("/* header part */\n");
libs = cPrintFunctionIncludes(cfns) ;
cPrintFunctionHeaders(cfns);
Print.printBuf("/* End of header part */\n");
then
(libs,outRecordLst);
case (DAE.DAE(elementLst = elist))
equation
Debug.fprintln("cgtr", "generate_function_headers (function reference)");
({},funcRef) = generateFunctionsElist(elist, {});
_ = cPrintIndentedList(funcRef, 0);
then
({},funcRef);
case _
equation
Debug.fprint("failtrace", "# Codegen.generateFunctionHeaders failed\n");
then
fail();
end matchcontinue;
end generateFunctionHeaders;
protected function generateFunctionsElist
"function: generateFunctionsElist
Helper function. Generates code from the Elements of a DAE."
input list<DAE.Element> els;
input list<String> rt;
output list<CFunction> cfns;
output list<String> rt_1;
list<DAE.Element> fns;
algorithm
Debug.fprintln("cgtr", "generate_functions_elist");
Debug.fprintln("cgtrdumpdae", "Dumping DAE:");
Debug.fcall("cgtrdumpdae", DAEUtil.dump2, DAE.DAE(els));
fns := Util.listFilter(els, DAEUtil.isFunction);
(cfns,rt_1) := generateFunctionsElist2(fns,rt);
end generateFunctionsElist;
protected function generateFunctionsElist2
"function: generateFunctionsElist2
Helper function to generateFunctionsElist."
input list<DAE.Element> inDAEElementLst;
input list<String> inRecordTypes;
output list<CFunction> outCFunctionLst;
output list<String> outRecordTypes;
algorithm
(outCFunctionLst,outRecordTypes):=
matchcontinue (inDAEElementLst,inRecordTypes)
local
list<CFunction> cfns1,cfns2,cfns;