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
|
#include "includes.h"
#include "offsets.h"
#ifdef ENABLE_JAVA
extern List* vlenconstants; /* List<Constant*>;*/
/* Forward*/
static void jdata_primdata(Symbol*, Datasrc*, Bytebuffer*, Datalist*);
/**************************************************/
/* Code for generating Java language data lists*/
/**************************************************/
/* Datalist rules: see the rules on the man page */
void
jdata_array(Symbol* vsym,
Bytebuffer* databuf,
Datasrc* src,
Odometer* odom,
int index,
Datalist* fillsrc)
{
int i;
int rank = odom->rank;
int firstdim = (index == 0); /* first dimension*/
int lastdim = (index == (rank - 1)); /* last dimension*/
size_t count;
Symbol* basetype = vsym->typ.basetype;
int isunlimited = (odom->declsize[index] == 0);
int pushed = 0;
ASSERT(index >= 0 && index < rank);
count = odom->count[index];
/* Unpack the nested unlimited (unless first dimension)*/
if(!firstdim && isunlimited && issublist(src)) {
srcpush(src);
pushed = 1;
}
if(lastdim) {
for(i=0;i<count;i++) {
jdata_basetype(basetype,src,databuf,fillsrc);
}
} else {
/* now walk count elements and generate recursively */
for(i=0;i<count;i++) {
jdata_array(vsym,databuf,src,odom,index+1,fillsrc);
}
}
if(isunlimited && pushed) srcpop(src);
return;
}
/* Generate an instance of the basetype using datasrc */
void
jdata_basetype(Symbol* tsym, Datasrc* datasrc, Bytebuffer* codebuf, Datalist* fillsrc)
{
switch (tsym->subclass) {
case NC_PRIM:
if(issublist(datasrc)) {
semerror(srcline(datasrc),"Expected primitive found {..}");
}
bbAppend(codebuf,' ');
jdata_primdata(tsym,datasrc,codebuf,fillsrc);
break;
default: PANIC1("jdata_basetype: unexpected subclass %d",tsym->subclass);
}
}
/**************************************************/
static void
jdata_primdata(Symbol* basetype, Datasrc* src, Bytebuffer* databuf, Datalist* fillsrc)
{
Constant target, *prim;
prim = srcnext(src);
if(prim == NULL) prim = &fillconstant;
ASSERT(prim->nctype != NC_COMPOUND);
if(prim->nctype == NC_FILLVALUE) {
Datalist* filler = getfiller(basetype,fillsrc);
ASSERT(filler->length == 1);
srcpushlist(src,filler);
bbAppend(databuf,' ');
jdata_primdata(basetype,src,databuf,NULL);
srcpop(src);
goto done;
}
target.nctype = basetype->typ.typecode;
ASSERT(prim->nctype != NC_COMPOUND);
convert1(prim,&target);
/* add hack for java bug in converting floats */
if(target.nctype == NC_FLOAT) bbCat(databuf," (float)");
bbCat(databuf,jdata_const(&target));
done:
return;
}
/* Result is a pool string or a constant => do not free*/
char*
jdata_const(Constant* ci)
{
char tmp[64];
char* result = NULL;
tmp[0] = '\0';
switch (ci->nctype) {
case NC_CHAR:
{
strcpy(tmp,"'");
escapifychar(ci->value.charv,tmp+1,'\'');
strcat(tmp,"'");
}
break;
case NC_BYTE:
sprintf(tmp,"%hhd",ci->value.int8v);
break;
case NC_SHORT:
sprintf(tmp,"%hd",ci->value.int16v);
break;
case NC_INT:
sprintf(tmp,"%d",ci->value.int32v);
break;
case NC_FLOAT:
sprintf(tmp,"%.8g",ci->value.floatv);
break;
case NC_DOUBLE:
sprintf(tmp,"%.16g",ci->value.doublev);
break;
case NC_STRING:
{
char* escaped = escapify(ci->value.stringv.stringv,
'"',ci->value.stringv.len);
result = poolalloc(1+2+strlen(escaped));
strcpy(result,"\"");
strcat(result,escaped);
strcat(result,"\"");
goto done;
}
break;
default: PANIC1("ncstype: bad type code: %d",ci->nctype);
}
result = pooldup(tmp);
done:
return result; /*except for NC_STRING and NC_OPAQUE*/
}
#endif /*ENABLE_JAVA*/
|