forked from Unidata/netcdf-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genjjni.c
1601 lines (1468 loc) · 41.9 KB
/
genjjni.c
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 2018, UCAR/Unidata
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
* $Header: /upc/share/CVS/netcdf-3/ncgen/genjjni.c,v 1.6 2010/04/04 19:39:46 dmh Exp $
*********************************************************************/
#include "includes.h"
#ifdef ENABLE_JAVA
#undef JGDB
#undef TRACE
/*MNEMONIC*/
#define USEMEMORY 1
extern List* vlenconstants; /* List<Constant*>;*/
/* Forward */
static void genjjni_definevardata(Symbol* vsym);
static void genjjni_primattribute(Symbol*, Bytebuffer*, unsigned long);
static void genjjni_scalarprim(Symbol* var, Bytebuffer* code);
static const char* jgroupncid(Symbol*);
static const char* jtypencid(Symbol*);
static const char* jvarncid(Symbol*);
static const char* jdimncid(Symbol*);
#ifdef USE_NETCDF4
static void definejtype(Symbol*);
static char* jprefixed(List* prefix, char* suffix, char* separator);
static void genjjni_deftype(Symbol*);
static void definespecialattributes(Symbol* vsym);
#endif
static int jputvaraprim(Putvar*, Odometer*, Bytebuffer*, size_t*);
static void genjjni_defineattribute(Symbol* asym);
static void genjjni_definevardata(Symbol*);
static void computemaxunlimited(void);
/*
Global Bytebuffer into which to store the C code output;
periodically dumped to file by jflush().
Defined in genjstd.c.
*/
extern Bytebuffer* jcode;
/*
* Generate code for creating netCDF from in-memory structure.
*/
void
gen_ncjava_jni(const char *filename)
{
int idim, ivar, iatt, maxdims;
int ndims, nvars, natts, ngatts, ngrps, ntyps;
char* cmode_string;
#ifdef USE_NETCDF4
int igrp,ityp;
#endif
jcode = bbNew();
bbSetalloc(jcode,C_MAX_STMT);
ndims = listlength(dimdefs);
nvars = listlength(vardefs);
natts = listlength(attdefs);
ngatts = listlength(gattdefs);
ngrps = listlength(grpdefs);
ntyps = listlength(typdefs);
/* Construct the main class */
#ifdef JGDB
jline("import java.io.*;");
#endif
jline("import java.util.*;");
jline("import netcdf.*;");
jline("import static netcdf.Constants.*;");
jline("import static netcdf.Netcdf.*;");
jline("");
jpartial("public class ");
jline(mainname);
jline("{");
/* Do static initializations */
jline("");
jline("static {");
jlined(1,"System.loadLibrary(\"ncjni\");");
jlined(1,"Netcdf.init();");
jline("}");
jline("");
jline("static long memory = Memory.create(); /* For storing data */");
#ifdef USE_NETCDF4
if (ntyps > 0) {
for(ityp = 0; ityp < ntyps; ityp++) {
Symbol* tsym = (Symbol*)listget(typdefs,ityp);
definejtype(tsym);
}
bbCat(jcode,"");
}
jflush();
/*
Define vlen constants; For java, this is done in
two parts. Part 1 (here) defines the constants and part 2
fills them in.
*/
{
jflush(); /* dump code to this point*/
genjjni_vlenconstants(vlenconstants,jcode);
jline("");
}
/* Construct the chunking constants*/
if(!usingclassic) {
for(ivar=0;ivar<nvars;ivar++) {
Bytebuffer* tmp = bbNew();
Symbol* var = (Symbol*)listget(vardefs,ivar);
Specialdata* special = &var->var.special;
if(special->flags & _CHUNKSIZE_FLAG) {
int i;
size_t* chunks = special->_ChunkSizes;
if(special->nchunks == 0 || chunks == NULL) continue;
bbClear(tmp);
for(i=0;i<special->nchunks;i++) {
nprintf(stmt,sizeof(stmt),"%s%ld",
(i == 0?"":", "),special->_ChunkSizes[i]);
bbCat(tmp,stmt);
}
nprintf(stmt,sizeof(stmt),"static final long[] %s_chunksizes = {",
jname(var),special->nchunks);
jpartial(stmt);
jprint(tmp);
jline("} ;");
}
}
jline("");
}
#endif /*USE_NETCDF4*/
jline("static public void");
jline("check_err(int stat) throws Exception");
jline("{");
jlined(1,"if (stat != NC_NOERR) {");
jlined(2,"System.err.println(\"Failed: \"+nc_strerror(stat));");
jlined(2,"throw new Exception(nc_strerror(stat));");
jlined(1,"}");
jline("}");
jline("");
/* Now construct the main procedure*/
jline("static public void main(String[] argv) throws Exception");
nprintf(stmt,sizeof(stmt), "{ /* create %s */",filename);
jline(stmt);
/* create necessary declarations */
jline("");
jlined(1,"int stat; /* return status */");
jlined(1,"int ncid; /* netCDF id */");
jlined(1,"int[] ncidp = new int[1]; /* netCDF id return */");
jflush();
/*
Define vlen constants Part 2.
The idea is to walk all the data lists
whose variable type has a vlen and collect
the vlen data and define a constant for it.
*/
jline("");
jflush(); /* dump code to this point*/
genjjni_vlendata(vlenconstants,jcode);
#ifdef JGDB
jline("{");
jline("System.out.print(\"go?\");");
jline("BufferedReader rin = new BufferedReader(new InputStreamReader(System.in));");
jline("rin.readLine();");
jline("}");
#endif
#ifdef USE_NETCDF4
/* Define variables to hold group ids*/
if(!usingclassic) {
jline("");
jlined(1,"/* group ids */");
}
if(!usingclassic && ngrps > 0) {
for(igrp = 0; igrp < ngrps; igrp++) {
Symbol* gsym = (Symbol*)listget(grpdefs,igrp);
nprintf(stmt,sizeof(stmt),"int %s;",jgroupncid(gsym));
jlined(1,stmt);
}
}
/* define variables to hold type ids*/
if(!usingclassic && ntyps > 0) {
jline("");
jlined(1,"/* type ids */");
for(ityp = 0; ityp < ntyps; ityp++) {
Symbol* tsym = (Symbol*)listget(typdefs,ityp);
nprintf(stmt,sizeof(stmt),"int %s;",jtypencid(tsym));
jlined(1,stmt);
}
}
jflush();
#endif
if (ndims > 0) {
jline("");
jlined(1,"/* dimension ids */");
for(idim = 0; idim < ndims; idim++) {
Symbol* dsym = (Symbol*)listget(dimdefs,idim);
nprintf(stmt,sizeof(stmt),"int %s;",jdimncid(dsym));
jlined(1,stmt);
}
jline("");
jlined(1,"/* dimension lengths */");
for(idim = 0; idim < ndims; idim++) {
Symbol* dsym = (Symbol*)listget(dimdefs,idim);
if (dsym->dim.size == NC_UNLIMITED) {
nprintf(stmt,sizeof(stmt),"%sfinal long %s_len = NC_UNLIMITED;",
indented(1),jname(dsym));
} else {
nprintf(stmt,sizeof(stmt),"%sfinal long %s_len = %lu;",
indented(1),
jname(dsym),
(unsigned long) dsym->dim.size);
}
jline(stmt);
}
}
jflush();
maxdims = 0; /* most dimensions of any variable */
for(ivar = 0; ivar < nvars; ivar++) {
Symbol* vsym = (Symbol*)listget(vardefs,ivar);
if(vsym->typ.dimset.ndims > maxdims)
maxdims = vsym->typ.dimset.ndims;
}
if (nvars > 0) {
jline("");
jlined(1,"/* variable ids */");
for(ivar = 0; ivar < nvars; ivar++) {
Symbol* vsym = (Symbol*)listget(vardefs,ivar);
nprintf(stmt,sizeof(stmt),"int %s;", jvarncid(vsym));
jlined(1,stmt);
}
jline("");
jlined(1,"/* rank (number of dimensions) for each variable */");
for(ivar = 0; ivar < nvars; ivar++) {
Symbol* vsym = (Symbol*)listget(vardefs,ivar);
nprintf(stmt,sizeof(stmt),"final int RANK_%s = %d;",
jname(vsym),vsym->typ.dimset.ndims);
jlined(1,stmt);
}
if (maxdims > 0) { /* we have dimensioned variables */
jline("");
jlined(1,"/* variable shapes */");
for(ivar = 0; ivar < nvars; ivar++) {
Symbol* vsym = (Symbol*)listget(vardefs,ivar);
if (vsym->typ.dimset.ndims > 0) {
nprintf(stmt,sizeof(stmt),"final int[] %s_dims = new int[RANK_%s];",
jname(vsym), jname(vsym));
jlined(1,stmt);
}
}
}
}
jflush();
/* create netCDF file, uses NC_CLOBBER mode */
jline("");
jlined(1,"/* enter define mode */");
if (!cmode_modifier) {
cmode_string = "NC_CLOBBER";
} else if (cmode_modifier & NC_64BIT_OFFSET) {
cmode_string = "NC_CLOBBER|NC_64BIT_OFFSET";
#ifdef USE_NETCDF4
} else if (cmode_modifier & NC_CLASSIC_MODEL) {
cmode_string = "NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL";
} else if (cmode_modifier & NC_NETCDF4) {
cmode_string = "NC_CLOBBER|NC_NETCDF4";
#endif
} else {
derror("unknown cmode modifier");
cmode_string = "NC_CLOBBER";
}
nprintf(stmt,sizeof(stmt),"check_err(nc_create(\"%s\", %s, ncidp));",
filename,cmode_string);
jlined(1,stmt);
jlined(1,"ncid = ncidp[0];");
jflush();
#ifdef USE_NETCDF4
/* Define the group structure */
/* ncid created above is also root group*/
if(!usingclassic) {
nprintf(stmt,sizeof(stmt),"%s = ncid;",jgroupncid(rootgroup));
jlined(1,stmt);
/* walking grpdefs list will do a preorder walk of all defined groups*/
for(igrp=0;igrp<listlength(grpdefs);igrp++) {
Symbol* gsym = (Symbol*)listget(grpdefs,igrp);
if(gsym == rootgroup) continue; /* ignore root*/
if(gsym->container == NULL)
PANIC("null container");
nprintf(stmt,sizeof(stmt),
"check_err(nc_def_grp(%s, \"%s\", ncidp));",
jgroupncid(gsym->container),
gsym->name);
jlined(1,stmt);
nprintf(stmt,sizeof(stmt),"%s = ncidp[0];",jgroupncid(gsym));
jlined(1,stmt);
}
jflush();
}
#endif
#ifdef USE_NETCDF4
/* Construct code to define types*/
if(ntyps > 0) {
jline("");
for(ityp = 0; ityp < ntyps; ityp++) {
Symbol* tsym = (Symbol*)listget(typdefs,ityp);
if(tsym->subclass == NC_PRIM
|| tsym->subclass == NC_ARRAY) continue; /* no need to do these*/
genjjni_deftype(tsym);
jline("");
}
}
jflush();
#endif
/* define dimensions from info in dims array */
if (ndims > 0) {
jline("");
jlined(1,"/* define dimensions */");
for(idim = 0; idim < ndims; idim++) {
Symbol* dsym = (Symbol*)listget(dimdefs,idim);
nprintf(stmt,sizeof(stmt),
"check_err(nc_def_dim(%s, \"%s\", %s_len, ncidp));",
jgroupncid(dsym->container),
jescapifyname(dsym->name), jname(dsym));
jlined(1,stmt);
nprintf(stmt,sizeof(stmt),"%s = ncidp[0];",jdimncid(dsym));
jlined(1,stmt);
}
}
jflush();
/* define variables from info in vars array */
if (nvars > 0) {
jline("");
jlined(1,"/* define variables */");
for(ivar = 0; ivar < nvars; ivar++) {
Symbol* vsym = (Symbol*)listget(vardefs,ivar);
Symbol* basetype = vsym->typ.basetype;
Dimset* dimset = &vsym->typ.dimset;
jline("");
if(dimset->ndims > 0) {
for(idim = 0; idim < dimset->ndims; idim++) {
Symbol* dsym = dimset->dimsyms[idim];
nprintf(stmt,sizeof(stmt),
"%s_dims[%d] = %s;",
jname(vsym),
idim,
jdimncid(dsym));
jlined(1,stmt);
}
}
nprintf(stmt,sizeof(stmt),
"check_err(nc_def_var(%s, \"%s\", %s, RANK_%s, %s, ncidp));",
jgroupncid(vsym->container),
jescapifyname(vsym->name),
jtypencid(basetype),
jname(vsym),
(dimset->ndims == 0?"null":poolcat(jname(vsym),"_dims")));
jlined(1,stmt);
nprintf(stmt,sizeof(stmt),"%s = ncidp[0];",jvarncid(vsym));
jlined(1,stmt);
#ifdef USE_NETCDF4
definespecialattributes(vsym);
#endif /*USE_NETCDF4*/
}
}
jflush();
/* Define the global attributes*/
if(ngatts > 0) {
jline("");
jlined(1,"/* assign global attributes */");
for(iatt = 0; iatt < ngatts; iatt++) {
Symbol* gasym = (Symbol*)listget(gattdefs,iatt);
genjjni_defineattribute(gasym);
}
jline("");
}
jflush();
/* Define the variable specific attributes*/
if(natts > 0) {
jline("");
jlined(1,"/* assign per-variable attributes */");
for(iatt = 0; iatt < natts; iatt++) {
Symbol* asym = (Symbol*)listget(attdefs,iatt);
genjjni_defineattribute(asym);
}
jline("");
}
jflush();
if (nofill_flag) {
jlined(1,"/* don't initialize variables with fill values */");
jlined(1,"check_err(nc_set_fill(%s, NC_NOFILL, 0));");
}
jline("");
jlined(1,"/* leave define mode */");
nprintf(stmt,sizeof(stmt),"check_err(nc_enddef(%s));",
jgroupncid(rootgroup));
jlined(1,stmt);
jflush();
/* Load values into those variables with defined data */
if(nvars > 0) {
jline("");
jlined(1,"/* assign variable data */");
for(ivar = 0; ivar < nvars; ivar++) {
Symbol* vsym = (Symbol*)listget(vardefs,ivar);
if(vsym->data != NULL) genjjni_definevardata(vsym);
}
jline("");
/* compute the max actual size of the unlimited dimension*/
if(usingclassic) computemaxunlimited();
}
jflush();
}
void
cl_java_jni(void)
{
nprintf(stmt,sizeof(stmt),"check_err(nc_close(%s));",
jgroupncid(rootgroup));
jlined(1,stmt);
jline("}"); /* main */
jline("}"); /* class Main */
jflush();
}
#ifdef USE_NETCDF4
static void
definespecialattributes(Symbol* vsym)
{
Specialdata* special = &vsym->var.special;
if(usingclassic) return;
if(special->flags & _STORAGE_FLAG) {
int storage = special->_Storage;
size_t* chunks = special->_ChunkSizes;
nprintf(stmt,sizeof(stmt),
"%scheck_err(nc_def_var_chunking(%s, %s, %s, ",
indented(1),
jgroupncid(vsym->container),
jvarncid(vsym),
(storage == NC_CONTIGUOUS?"NC_CONTIGUOUS":"NC_CHUNKED"));
jpartial(stmt);
if(special->nchunks == 0 || chunks == NULL)
jpartial("new long[]{}");
else {
nprintf(stmt,sizeof(stmt),"%s_chunksizes",jname(vsym));
jpartial(stmt);
}
jline("));");
}
if(special->flags & _FLETCHER32_FLAG) {
nprintf(stmt,sizeof(stmt),
"check_err(nc_def_var_fletcher32(%s, %s, %d));",
jgroupncid(vsym->container),
jvarncid(vsym),
special->_Fletcher32);
jlined(1,stmt);
}
if(special->flags & (_DEFLATE_FLAG | _SHUFFLE_FLAG)) {
nprintf(stmt,sizeof(stmt),
"check_err(nc_def_var_deflate(%s, %s, %s, %d, %d));",
jgroupncid(vsym->container),
jvarncid(vsym),
(special->_Shuffle == 1?"NC_SHUFFLE":"NC_NOSHUFFLE"),
(special->_DeflateLevel >= 0?1:0),
(special->_DeflateLevel >= 0?special->_DeflateLevel:0));
jlined(1,stmt);
}
if(special->flags & _ENDIAN_FLAG) {
nprintf(stmt,sizeof(stmt),
"check_err(nc_def_var_endian(%s, %s, %s));",
jgroupncid(vsym->container),
jvarncid(vsym),
(special->_Endianness == NC_ENDIAN_LITTLE?"NC_ENDIAN_LITTLE"
:"NC_ENDIAN_BIG")
);
jlined(1,stmt);
}
if(special->flags & _NOFILL_FLAG) {
nprintf(stmt,sizeof(stmt),
"check_err(nc_def_var_fill(%s, %s, %s));",
jgroupncid(vsym->container),
jvarncid(vsym),
(special->_Fill?"NC_FILL":"NC_NOFILL")
);
jlined(1,stmt);
}
}
#endif /*USE_NETCDF4*/
/*
* Return java type name for netCDF type, given type code.
*/
const char*
jtype(nc_type type)
{
switch (type) {
case NC_CHAR: return "char";
case NC_BYTE: return "byte";
case NC_SHORT: return "short";
case NC_INT: return "int";
case NC_FLOAT: return "float";
case NC_DOUBLE: return "double";
case NC_UBYTE: return "long";
case NC_USHORT: return "long";
case NC_UINT: return "long";
case NC_INT64: return "long";
case NC_UINT64: return "long";
case NC_STRING: return "String";
case NC_ENUM: return "int";
case NC_OPAQUE: return "String";
default: PANIC1("ncctype: bad type code:%d",type);
}
return 0;
}
/*
* Return a type name and dimensions for constant arrays
* for netCDF type, given type code.
*/
const char*
jarraytype(nc_type type)
{
switch (type) {
case NC_CHAR:
return "String";
case NC_BYTE:
return "byte";
case NC_SHORT:
return "short";
case NC_INT:
return "int";
case NC_FLOAT:
return "float";
case NC_DOUBLE:
return "double";
case NC_UBYTE:
return "long";
case NC_USHORT:
return "long";
case NC_UINT:
return "long";
case NC_INT64:
return "long";
case NC_UINT64:
return "long";
case NC_STRING:
return "String";
case NC_ENUM:
return "int";
case NC_OPAQUE:
return "String";
default:
PANIC1("ncctype: bad type code:%d",type);
}
return 0;
}
/*
* Return netcdf interface type name for netCDF type suffix, given type code.
*/
const char*
jstype(nc_type nctype)
{
switch (nctype) {
case NC_CHAR:
return "text";
case NC_BYTE:
return "schar";
case NC_SHORT:
return "short";
case NC_INT:
return "int";
case NC_FLOAT:
return "float";
case NC_DOUBLE:
return "double";
case NC_UBYTE:
return "ubyte";
case NC_USHORT:
return "ushort";
case NC_UINT:
return "uint";
case NC_INT64:
return "longlong";
case NC_UINT64:
return "ulonglong";
case NC_STRING:
return "string";
case NC_OPAQUE:
return "opaque";
case NC_ENUM:
return "enum";
default:
derror("ncstype: bad type code: %d",nctype);
return 0;
}
}
/* Return the group name for the specified group*/
static const char*
jgroupncid(Symbol* sym)
{
#ifdef USE_NETCDF4
if(usingclassic) {
return "ncid";
} else {
char* grptmp;
const char* tmp1;
if(sym == NULL) return jgroupncid(rootgroup);
ASSERT(sym->objectclass == NC_GRP);
tmp1 = jname(sym);
grptmp = poolalloc(strlen(tmp1)+strlen("_grp")+1);
strcpy(grptmp,tmp1);
strcat(grptmp,"_grp");
return grptmp;
}
#else
return "ncid";
#endif
}
/* Compute the name for a given type's id*/
/* Watch out: the result is a pool alloc*/
static const char*
jtypencid(Symbol* tsym)
{
char* typtmp;
const char* tmp1;
if(tsym->subclass == NC_PRIM)
return nctype(tsym->typ.typecode);
tmp1 = jtypename(tsym);
typtmp = poolalloc(strlen(tmp1)+strlen("_typ")+1);
strcpy(typtmp,tmp1);
strcat(typtmp,"_typ");
return typtmp;
}
/* Compute the name for a given var's id*/
/* Watch out: the result is a static*/
static const char*
jvarncid(Symbol* vsym)
{
const char* tmp1;
char* vartmp;
tmp1 = jname(vsym);
vartmp = poolalloc(strlen(tmp1)+strlen("_id")+1);
strcpy(vartmp,tmp1);
strcat(vartmp,"_id");
return vartmp;
}
/* Compute the name for a given dim's id*/
/* Watch out: the result is a static*/
static const char*
jdimncid(Symbol* dsym)
{
const char* tmp1;
char* dimtmp;
tmp1 = jname(dsym);
dimtmp = poolalloc(strlen(tmp1)+strlen("_dim")+1);
strcpy(dimtmp,tmp1);
strcat(dimtmp,"_dim");
return dimtmp;
}
/* Compute the C name for a given type*/
const char*
jtypename(Symbol* tsym)
{
const char* name;
ASSERT(tsym->objectclass == NC_TYPE);
if(tsym->subclass == NC_PRIM)
name = jtype(tsym->typ.typecode);
else
name = jname(tsym);
return name;
}
#ifdef USE_NETCDF4
/*
Only generate type info for enumerations
*/
static void
definejtype(Symbol* tsym)
{
int i;
int nconst;
ASSERT(tsym->objectclass == NC_TYPE);
if(tsym->subclass != NC_ENUM) return;
/* start enum def */
nprintf(stmt,sizeof(stmt),"enum %s {",jtypename(tsym));
jline(stmt);
/* define the constants */
nconst = listlength(tsym->subnodes);
for(i=0;i<nconst;i++) {
Symbol* econst = (Symbol*)listget(tsym->subnodes,i);
ASSERT(econst->subclass == NC_ECONST);
nprintf(stmt,sizeof(stmt),"%s%s(%s)%s",
indented(1),
jtypename(econst),
jconst(econst->typ.econst),
(i == (nconst - 1)?";":","));
jlined(1,stmt);
}
/* add boilerplate */
nprintf(stmt,sizeof(stmt),"private final %s value;",
jtypename(tsym->typ.basetype));
jlined(1,stmt);
nprintf(stmt,sizeof(stmt),"%s(%s value) {this.value = value;}",
jtypename(tsym),jtypename(tsym->typ.basetype));
jlined(1,stmt);
nprintf(stmt,sizeof(stmt),"public %s value() {return this.value;}",
jtypename(tsym->typ.basetype));
jlined(1,stmt);
jline("}");
}
/*
Generate the code for defining a given type
*/
static void
genjjni_deftype(Symbol* tsym)
{
int i;
ASSERT(tsym->objectclass == NC_TYPE);
switch (tsym->subclass) {
case NC_PRIM: break; /* these are already taken care of*/
case NC_OPAQUE:
nprintf(stmt,sizeof(stmt),"check_err(nc_def_opaque(%s, %lu, \"%s\", ncidp));",
jgroupncid(tsym->container),
tsym->typ.size,
jescapifyname(tsym->name));
jlined(1,stmt);
nprintf(stmt,sizeof(stmt),"%s = ncidp[0];",jtypencid(tsym));
jlined(1,stmt);
break;
case NC_ENUM:
jlined(1,"{");
jlined(1,stmt);
jlined(1,"long econst;");
nprintf(stmt,sizeof(stmt),"check_err(nc_def_enum(%s, %s, \"%s\", ncidp));",
jgroupncid(tsym->container),
nctype(tsym->typ.basetype->typ.typecode),
jname(tsym));
jlined(1,stmt);
nprintf(stmt,sizeof(stmt),"%s = ncidp[0];",jtypencid(tsym));
jlined(1,stmt);
for(i=0;i<listlength(tsym->subnodes);i++) {
Symbol* econst = (Symbol*)listget(tsym->subnodes,i);
ASSERT(econst->subclass == NC_ECONST);
nprintf(stmt,sizeof(stmt),"econst = (long)%s.%s.value();",
jname(tsym),
jname(econst));
jlined(1,stmt);
nprintf(stmt,sizeof(stmt),"check_err(nc_insert_enum(%s, %s, \"%s\", econst));",
jgroupncid(tsym->container), jtypencid(tsym),
jescapifyname(econst->name));
jlined(1,stmt);
}
jlined(1,"}");
break;
case NC_VLEN:
nprintf(stmt,sizeof(stmt),"stat = nc_def_vlen(%s, \"%s\", %s, ncidp);",
jgroupncid(tsym->container),
jescapifyname(tsym->name),
jtypencid(tsym->typ.basetype));
jlined(1,stmt);
nprintf(stmt,sizeof(stmt),"%s = ncidp[0];",jtypencid(tsym));
jlined(1,stmt);
break;
case NC_COMPOUND:
/* We cannot use sizeof(), so use the computed size */
nprintf(stmt,sizeof(stmt),"check_err(nc_def_compound(%s, %ld, \"%s\", ncidp));",
jgroupncid(tsym->container),
(unsigned long)tsym->typ.size,
jtypename(tsym),
tsym->name);
jlined(1,stmt);
nprintf(stmt,sizeof(stmt),"%s = ncidp[0];",jtypencid(tsym));
jlined(1,stmt);
/* Generate the field dimension constants*/
jlined(1,"{");
for(i=0;i<listlength(tsym->subnodes);i++) {
int j;
Symbol* efield = (Symbol*)listget(tsym->subnodes,i);
ASSERT(efield->subclass == NC_FIELD);
if(efield->typ.dimset.ndims == 0) continue;
nprintf(stmt,sizeof(stmt),"final int %s_dims[] = new int[] {",
jname(efield));
for(j=0;j<efield->typ.dimset.ndims;j++) {
char tmp[256];
Symbol* e = efield->typ.dimset.dimsyms[j];
ASSERT(e->dim.isconstant);
sprintf(tmp,"%u",e->dim.size);
strcat(stmt,(j==0?"":", "));
strcat(stmt,tmp);
}
strcat(stmt,"};");
jlined(1,stmt);
}
for(i=0;i<listlength(tsym->subnodes);i++) {
Symbol* efield = (Symbol*)listget(tsym->subnodes,i);
char tmp[1024];
ASSERT(efield->subclass == NC_FIELD);
sprintf(tmp,"%lu",efield->typ.offset);
if(efield->typ.dimset.ndims > 0){
nprintf(stmt,sizeof(stmt),"check_err(nc_insert_array_compound(%s, %s, \"%s\", %s, %s, %d, %s_dims));",
jgroupncid(tsym->container),
jtypencid(tsym),
jescapifyname(efield->name),
tmp,
jtypencid(efield->typ.basetype),
efield->typ.dimset.ndims,
jname(efield));
} else {
nprintf(stmt,sizeof(stmt),"check_err(nc_insert_compound(%s, %s, \"%s\", %s, %s));",
jgroupncid(tsym->container),
jtypencid(tsym),
jescapifyname(efield->name),
tmp,
jtypencid(efield->typ.basetype));
}
jlined(1,stmt);
}
jlined(1,"}");
break;
case NC_ARRAY:
/* ignore: this will be handled by def_var*/
break;
default: panic("genjjni_deftype: unexpected type subclass: %d",tsym->subclass);
}
}
#endif
static void
genjjni_defineattribute(Symbol* asym)
{
unsigned long len;
Datalist* list;
Symbol* basetype = asym->typ.basetype;
Bytebuffer* code = NULL; /* capture other decls*/
list = asym->data;
if(list == NULL) PANIC("empty attribute list");
len = asym->att.count;
if(len == 0) PANIC("empty attribute list");
nprintf(stmt,sizeof(stmt),"/* attribute: %s */",asym->name);
jlined(1,stmt);
code = bbNew();
genjjni_attrdata(asym,code);
/* Handle primitives separately */
if(isprimplus(basetype->typ.typecode)) {
if(basetype->typ.typecode != NC_CHAR) commify(code);
genjjni_primattribute(asym, code, len);
} else { /* User defined type */
#ifndef USE_NETCDF4
verror("Non-classic type: %s",nctypename(basetype->typ.typecode));
#else /* USE_NETCDF4 */
if(usingclassic && !isclassicprim(basetype->typ.typecode)) {
verror("Non-classic type: %s",nctypename(basetype->typ.typecode));
return;
}
/* Dump the generation code */
jprint(code);
nprintf(stmt,sizeof(stmt),"check_err(nc_put_att_memory(%s, %s, \"%s\", %s, %lu, memory));",
jgroupncid(asym->container),
(asym->att.var == NULL?"NC_GLOBAL"
:jvarncid(asym->att.var)),
jescapifyname(asym->name),
jtypencid(basetype),
len);
jlined(1,stmt);
#endif
}
bbFree(code);
}
static void
genjjni_primattribute(Symbol* asym, Bytebuffer* code, unsigned long len)
{
Symbol* basetype = asym->typ.basetype;
nc_type typecode = basetype->typ.typecode;
/* Handle NC_CHAR specially */
if(typecode == NC_CHAR) {
/* revise the length count */
len = bbLength(code);
if(len == 0) {bbAppend(code,'\0'); len++;}
jquotestring(code,'"');
} else {
/* Convert to constant */
char* code2 = bbDup(code);
bbClear(code);
nprintf(stmt,sizeof(stmt),"new %s[]",
jarraytype(typecode));
bbCat(code,stmt);
bbCat(code,"{");
bbCat(code,code2);
bbCat(code,"}");
efree(code2);
}
switch (typecode) {
case NC_BYTE:
case NC_SHORT:
case NC_INT:
case NC_FLOAT:
case NC_DOUBLE:
nprintf(stmt,sizeof(stmt),"%scheck_err(nc_put_att_%s(%s, %s, \"%s\", %s, %lu, ",
indented(1),
jstype(basetype->typ.typecode),
jgroupncid(asym->container),
(asym->att.var == NULL?"NC_GLOBAL"
:jvarncid(asym->att.var)),
jescapifyname(asym->name),
jtypencid(basetype),
len);
jpartial(stmt);
jprint(code);
jline("));");
jflush();
break;
case NC_CHAR:
nprintf(stmt,sizeof(stmt),"%scheck_err(nc_put_att_%s(%s, %s, \"%s\", %lu, ",
indented(1),
jstype(basetype->typ.typecode),
jgroupncid(asym->container),
(asym->att.var == NULL?"NC_GLOBAL"
:jvarncid(asym->att.var)),
jescapifyname(asym->name),
len);
jpartial(stmt);
jprint(code);
jline("));");
jflush();
break;
#ifdef USING_NETCDF4
/* !usingclassic only (except NC_STRING) */
case NC_UBYTE:
case NC_USHORT:
case NC_UINT:
case NC_INT64:
case NC_UINT64:
case NC_STRING:
if(usingclassic) {
verror("Non-classic type: %s",njtypename(basetype->typ.typecode));