| File: | src/java/genjava.c |
| Warning: | line 529, column 8 Value stored to 'decl' during its initialization is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | #include "comsg.h" |
| 2 | #include "debug.h" |
| 3 | #include "flog.h" |
| 4 | #include "fbox.h" |
| 5 | #include "foamsig.h" |
| 6 | #include "gencr.h" |
| 7 | #include "intset.h" |
| 8 | #include "javacode.h" |
| 9 | #include "javasig.h" |
| 10 | #include "sexpr.h" |
| 11 | #include "store.h" |
| 12 | #include "syme.h" |
| 13 | #include "table.h" |
| 14 | #include "util.h" |
| 15 | |
| 16 | /* |
| 17 | * Implement as the following... |
| 18 | * Unit: |
| 19 | * Becomes a class |
| 20 | * functions: |
| 21 | * static methods within the class |
| 22 | * closures: |
| 23 | * library class |
| 24 | * formats: |
| 25 | * ? inner classes |
| 26 | |
| 27 | * Fluids: |
| 28 | * supplied as library |
| 29 | */ |
| 30 | |
| 31 | /* static function declarations */ |
| 32 | localstatic String gj0ClassName(Foam foam, String name); |
| 33 | localstatic String gj0ClassDocumentation(Foam foam, String name); |
| 34 | localstatic JavaCodeList gj0CollectImports(JavaCode clss); |
| 35 | localstatic JavaCodeList gj0DDef(Foam foam); |
| 36 | localstatic JavaCode gj0Gen(Foam foam); |
| 37 | localstatic JavaCode gj0Gen0(Foam foam); |
| 38 | localstatic JavaCode gj0GenFmt(Foam foam, AInt fmt); |
| 39 | localstatic JavaCodeList gj0GenList(Foam *p, AInt n); |
| 40 | localstatic JavaCode gj0Def(Foam foam); |
| 41 | localstatic JavaCode gj0Default(Foam foam, String s); |
| 42 | localstatic JavaCode gj0Prog(Foam lhs, Foam rhs); |
| 43 | localstatic JavaCode gj0ClosInit(Foam lhs, Foam rhs); |
| 44 | localstatic JavaCode gj0Set(Foam lhs, Foam rhs); |
| 45 | localstatic JavaCode gj0Return(Foam foam); |
| 46 | localstatic JavaCode gj0Seq(Foam seq); |
| 47 | localstatic JavaCode gj0Par(Foam seq); |
| 48 | localstatic JavaCode gj0Loc(Foam seq); |
| 49 | localstatic JavaCode gj0LocSet(Foam ref, Foam rhs); |
| 50 | localstatic JavaCode gj0Glo(Foam ref); |
| 51 | localstatic JavaCode gj0GloSet(Foam ref, Foam rhs); |
| 52 | localstatic JavaCode gj0GloSetJava(Foam lhs, Foam rhs); |
| 53 | localstatic JavaCode gj0GloRegister(Foam lhs, Foam rhs); |
| 54 | localstatic JavaCode gj0GloJavaId(AInt id); |
| 55 | localstatic JavaCode gj0GloJavaMethodId(AInt id); |
| 56 | localstatic JavaCode gj0Nil(Foam foam); |
| 57 | |
| 58 | localstatic JavaCode gj0Gener (Foam); /* Create a new Generator */ |
| 59 | localstatic JavaCode gj0GenIter (Foam); /* Iterater from Generator */ |
| 60 | localstatic JavaCode gj0GenerValue (Foam); /* Generate a value from an iterator */ |
| 61 | |
| 62 | localstatic JavaCode gj0Throw(Foam foam); |
| 63 | |
| 64 | localstatic JavaCode gj0ValuesSet(Foam foam, Foam rhs); |
| 65 | localstatic JavaCode gj0ValuesReturn(Foam foam); |
| 66 | |
| 67 | localstatic JavaCode gj0ANew(Foam foam); |
| 68 | localstatic JavaCode gj0Arr(Foam foam); |
| 69 | localstatic JavaCode gj0ArrChar(Foam foam); |
| 70 | localstatic JavaCode gj0AElt(Foam foam); |
| 71 | localstatic JavaCode gj0AEltSet(Foam foam, Foam rhs); |
| 72 | |
| 73 | localstatic JavaCode gj0RNew(Foam foam); |
| 74 | localstatic JavaCode gj0RElt(Foam foam); |
| 75 | localstatic JavaCode gj0REltSet(Foam foam, Foam rhs); |
| 76 | localstatic JavaCode gj0RecElt(JavaCode ref, Foam ddecl, int idx); |
| 77 | |
| 78 | localstatic JavaCode gj0Free(Foam foam); |
| 79 | localstatic JavaCode gj0RecSet(JavaCode lhs, JavaCode rhs, Foam ddecl, int idx); |
| 80 | |
| 81 | localstatic JavaCode gj0SInt(Foam foam); |
| 82 | localstatic JavaCode gj0HInt(Foam foam); |
| 83 | localstatic JavaCode gj0BInt(Foam foam); |
| 84 | localstatic JavaCode gj0SFlo(Foam foam); |
| 85 | localstatic JavaCode gj0DFlo(Foam foam); |
| 86 | localstatic JavaCode gj0Byte(Foam foam); |
| 87 | |
| 88 | localstatic JavaCode gj0Bool(Foam foam); |
| 89 | localstatic JavaCode gj0Char(Foam foam); |
| 90 | |
| 91 | localstatic JavaCode gj0Cast(Foam foam); |
| 92 | localstatic JavaCode gj0CastFmt(Foam foam, AInt cfmt); |
| 93 | |
| 94 | localstatic JavaCode gj0Env(Foam foam); |
| 95 | localstatic JavaCode gj0Lex(Foam foam); |
| 96 | localstatic JavaCode gj0PushEnv(Foam foam); |
| 97 | localstatic JavaCode gj0LexSet(Foam foam, Foam rhs); |
| 98 | localstatic JavaCode gj0EElt(Foam foam); |
| 99 | localstatic JavaCode gj0EEltSet(Foam foam, Foam rhs); |
| 100 | localstatic JavaCode gj0EEnv(Foam foam); |
| 101 | localstatic JavaCode gj0EInfo(Foam foam); |
| 102 | localstatic JavaCode gj0EInfoSet(Foam foam, Foam rhs); |
| 103 | localstatic JavaCode gj0EEnsure(Foam foam); |
| 104 | localstatic JavaCode gj0EnvId(int lvl); |
| 105 | localstatic JavaCode gj0LvlId(int lvl); |
| 106 | |
| 107 | localstatic JavaCode gj0Clos(Foam foam); |
| 108 | localstatic JavaCode gj0CEnv(Foam foam); |
| 109 | localstatic JavaCode gj0CProg(Foam foam); |
| 110 | localstatic JavaCode gj0CEnvSet(Foam foam, Foam rhs); |
| 111 | localstatic JavaCode gj0CProgSet(Foam foam, Foam rhs); |
| 112 | |
| 113 | localstatic JavaCode gj0CCall(Foam call); |
| 114 | localstatic JavaCode gj0OCall(Foam call); |
| 115 | localstatic JavaCode gj0BCall(Foam call); |
| 116 | localstatic JavaCode gj0PCall(Foam call); |
| 117 | localstatic Bool gj0PCallThrowsException(Foam foam); |
| 118 | localstatic JavaCode gj0PCallCatchException(JavaCode code); |
| 119 | |
| 120 | localstatic JavaCode gj0MFmt(Foam mfmt); |
| 121 | localstatic JavaCode gj0Const(Foam foam); |
| 122 | |
| 123 | localstatic JavaCode gj0PRef(Foam foam); |
| 124 | localstatic JavaCode gj0PRefSet(Foam lhs, Foam rhs); |
| 125 | |
| 126 | localstatic void gjInit(Foam foam, String name); |
| 127 | localstatic void gj0ProgInit(Foam lhs, Foam rhs); |
| 128 | localstatic void gj0ProgFini(Foam lhs, Foam rhs); |
| 129 | localstatic JavaCodeList gj0ProgCollectArgs(Foam fm); |
| 130 | localstatic JavaCode gj0ProgGenerateBody(Foam fm); |
| 131 | localstatic JavaCodeList gj0ProgExceptions(); |
| 132 | localstatic int gj0ProgModifiers(); |
| 133 | localstatic String gj0ProgMethodName(Foam var); |
| 134 | localstatic String gj0ProgFnName(int idx); |
| 135 | localstatic void gj0ProgAddStub(FoamSig sigList); |
| 136 | localstatic void gj0ProgAddStubs(FoamSigList sigList); |
| 137 | localstatic JavaCodeList gj0ProgDeclarations(Foam ddecl, Foam body); |
| 138 | |
| 139 | localstatic String gj0JavaSigName(CString prefix, Foam fmt, int idx); |
| 140 | localstatic String gj0Name(CString prefix, Foam fmt, int idx); |
| 141 | localstatic String gj0JavaSigName(CString prefix, Foam fmt, int idx); |
| 142 | localstatic JavaCode gj0ProgRetnType(Foam rhs); |
| 143 | localstatic JavaCode gj0TopConst(Foam lhs, Foam rhs); |
| 144 | |
| 145 | localstatic JavaCode gj0Type(Foam decl); |
| 146 | localstatic JavaCode gj0TypeFrFmt(AInt id, AInt fmt); |
| 147 | localstatic JavaCode gj0TypeObjToValue(JavaCode val, FoamTag type, AInt fmt); |
| 148 | localstatic JavaCode gj0TypeValueToObj(JavaCode val, FoamTag type, AInt fmt); |
| 149 | localstatic JavaCode gj0TypeValueToArr(JavaCode value, AInt fmt); |
| 150 | localstatic JavaCode gj0TypeValueToRec(JavaCode value, AInt fmt); |
| 151 | |
| 152 | localstatic FoamTag gj0FoamExprType(Foam foam); |
| 153 | localstatic FoamTag gj0FoamExprTypeWFmt(Foam foam, AInt *fmt); |
| 154 | |
| 155 | localstatic FoamSig gj0FoamSigFrCCall(Foam ccall); |
| 156 | localstatic JavaCodeList gj0CCallStubGen(FoamSigList sigs); |
| 157 | localstatic FoamSigList gj0CCallStubAdd(FoamSigList l, FoamSig sig); |
| 158 | localstatic JavaCode gj0CCallStubGenFrSig(FoamSig sig); |
| 159 | localstatic String gj0CCallStubName(FoamSig call); |
| 160 | localstatic JavaCode gj0CCallSig(FoamSig sig, JavaCode op, JavaCodeList args); |
| 161 | |
| 162 | localstatic JavaCode gj0SeqNode(JavaCodeList l); |
| 163 | localstatic Bool gj0IsSeqNode(JavaCode jc); |
| 164 | |
| 165 | localstatic JavaCode gj0FmtId(AInt ddeclIdx); |
| 166 | localstatic void gj0FmtUse(AInt ddeclIdx); |
| 167 | localstatic JavaCodeList gj0FmtInits(); |
| 168 | |
| 169 | localstatic void gj0NameInit(); |
| 170 | localstatic String gj0NameFrString(String fmName); |
| 171 | |
| 172 | localstatic JavaCodeList gj0ClassHeader(String className); |
| 173 | localstatic String gj0InitVar(AInt idx); |
| 174 | |
| 175 | localstatic JavaCode gj0TypeFrJavaObj(Foam format); |
| 176 | |
| 177 | localstatic JavaCodeList gj0ExportClassCreateAll(void); |
| 178 | localstatic JavaCode gj0ExportClassCreate(JavaCode className, AIntList decls); |
| 179 | localstatic JavaCode gj0ExportClassName(String id); |
| 180 | localstatic JavaCode gj0ExportMethodName(String id); |
| 181 | localstatic JavaCodeList gj0ExportClassMethods(JavaCode className, AIntList decls); |
| 182 | localstatic JavaCode gj0ExportClassMethod(AInt id); |
| 183 | localstatic JavaCodeList gj0ExportClassInternalMethods(void); |
| 184 | localstatic JavaCode gj0ExportClassInternalMethod(int i); |
| 185 | localstatic JavaCodeList gj0ExportClassMemberVars(void); |
| 186 | |
| 187 | localstatic JavaCodeList gj0ExportClassMethodParams(Foam sig, AInt proto); |
| 188 | localstatic JavaCodeList gj0ExportClassMethodArgs(Foam sig, AInt proto); |
| 189 | |
| 190 | enum gjId { |
| 191 | GJ_INVALID = -1, |
| 192 | |
| 193 | GJ_Foam, |
| 194 | GJ_FoamWord, |
| 195 | GJ_FoamClos, |
| 196 | GJ_FoamGener, |
| 197 | GJ_FoamGenIter, |
| 198 | GJ_FoamRecord, |
| 199 | GJ_FoamEnv, |
| 200 | GJ_FoamClass, |
| 201 | GJ_FoamContext, |
| 202 | GJ_FoamHelper, |
| 203 | |
| 204 | GJ_FoamFn, |
| 205 | GJ_FoamGFn, |
| 206 | GJ_FoamValue, |
| 207 | GJ_Multi, |
| 208 | GJ_FoamGlobals, |
| 209 | GJ_Format, |
| 210 | GJ_EnvRecord, |
| 211 | GJ_AldorObject, |
| 212 | |
| 213 | GJ_JavaException, |
| 214 | GJ_FoamUserException, |
| 215 | |
| 216 | GJ_Object, |
| 217 | GJ_String, |
| 218 | GJ_BigInteger, |
| 219 | GJ_LangException, |
| 220 | GJ_NullPointerException, |
| 221 | GJ_ClassCastException, |
| 222 | |
| 223 | GJ_ContextVar, |
| 224 | GJ_Main, |
| 225 | GJ_class, |
| 226 | GJ_Instance, |
| 227 | |
| 228 | GJ_LIMIT |
| 229 | }; |
| 230 | |
| 231 | typedef Enum(gjId)enum gjId GjId; |
| 232 | |
| 233 | localstatic JavaCode gj0Id(GjId id); |
| 234 | #if 0 |
| 235 | } |
| 236 | #endif |
| 237 | |
| 238 | /* Run arguments */ |
| 239 | struct gjArgs { |
| 240 | String name; |
| 241 | Bool createMain; |
| 242 | int lineWidth; |
| 243 | String pkg; |
| 244 | }; |
| 245 | |
| 246 | typedef struct gjArgs GjArgs; |
| 247 | |
| 248 | /* State variables */ |
| 249 | struct gjContext { |
| 250 | /* whole program */ |
| 251 | Foam formats; |
| 252 | Foam constants; |
| 253 | Foam defs; |
| 254 | /* Mutable state (caches, etc) */ |
| 255 | int lvl; |
| 256 | FoamSigList ccallStubSigList; |
| 257 | JavaCodeList gloRegList; |
| 258 | IntSet fmtSet; |
| 259 | /* Per prog */ |
| 260 | Foam prog; |
| 261 | Foam progParams; |
| 262 | Foam progLocals; |
| 263 | Foam progLhs; |
| 264 | FoamSigList progSigList; |
| 265 | int multVarIdx; |
| 266 | /* in codegen */ |
| 267 | AInt mfmt; |
| 268 | AInt contextFmt; |
| 269 | }; |
| 270 | |
| 271 | #define gjContextGlobals(gjContext->formats->foamDFmt.argv[0]) (gjContext->formats->foamDFmt.argv[globalsSlot0]) |
| 272 | #define gjContextGlobal(id)((gjContext->formats->foamDFmt.argv[0])->foamDDecl.argv [id]) (gjContextGlobals(gjContext->formats->foamDFmt.argv[0])->foamDDecl.argv[id]) |
| 273 | |
| 274 | static struct gjContext gjCtxt0; |
| 275 | static struct gjContext *gjContext = &gjCtxt0; |
| 276 | |
| 277 | static struct gjArgs gjArgs0; |
| 278 | static struct gjArgs *gjArgs = &gjArgs0; |
| 279 | |
| 280 | /* DEBUG */ |
| 281 | |
| 282 | Bool genJavaDebug = false((int) 0); |
| 283 | #define gjDEBUGif (genJavaDebug) fprintf if (DEBUG(genJava)genJavaDebug) fprintf |
| 284 | |
| 285 | /* Functions... */ |
| 286 | |
| 287 | JavaCodeList |
| 288 | genJavaUnit(Foam foam, String name) |
| 289 | { |
| 290 | JavaCodeList imps, javaExportDecls, javaExportMeths, code, stubs; |
| 291 | JavaCodeList mainImpl, interfaces, fmts, body; |
| 292 | JavaCodeList exportedClassFiles; |
| 293 | JavaCode clss, mainFile; |
| 294 | String className, comment; |
| 295 | |
| 296 | gjArgs->name = name; |
| 297 | gjArgs->pkg = "aldorcode"; |
| 298 | |
| 299 | if (foamUnitHasCoroutine(foam)) { |
| 300 | foam = gcrRewriteUnit(foam); |
| 301 | } |
| 302 | |
| 303 | gjInit(foam, name); |
| 304 | |
| 305 | className = gj0ClassName(foam, name); |
| 306 | if (!jcIsLegalClassName(className)) { |
| 307 | comsgFatal(NULL((void*)0), ALDOR_F_BadJavaFileName369, className); |
| 308 | } |
| 309 | strFree(className); |
| 310 | |
| 311 | code = gj0DDef(foam->foamUnit.defs); |
| 312 | mainImpl = gj0ClassHeader(name); |
| 313 | javaExportDecls = gj0ExportClassMemberVars(); |
| 314 | javaExportMeths = gj0ExportClassInternalMethods(); |
| 315 | stubs = gj0CCallStubGen(gjContext->ccallStubSigList); |
| 316 | |
| 317 | comment = gj0ClassDocumentation(foam, name); |
| 318 | |
| 319 | fmts = gj0FmtInits(); |
| 320 | |
| 321 | body = listNil(JavaCode)((JavaCodeList) 0); |
| 322 | body = listNConcat(JavaCode)(JavaCode_listPointer->NConcat)(body, fmts); |
| 323 | body = listNConcat(JavaCode)(JavaCode_listPointer->NConcat)(body, javaExportDecls); |
| 324 | body = listNConcat(JavaCode)(JavaCode_listPointer->NConcat)(body, mainImpl); |
| 325 | body = listNConcat(JavaCode)(JavaCode_listPointer->NConcat)(body, code); |
| 326 | body = listNConcat(JavaCode)(JavaCode_listPointer->NConcat)(body, javaExportMeths); |
| 327 | body = listNConcat(JavaCode)(JavaCode_listPointer->NConcat)(body, stubs); |
| 328 | |
| 329 | interfaces = listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(gj0Id(GJ_FoamClass)); |
| 330 | clss = jcClass(JCO_MOD_Public, comment, listNil(JavaCode)((JavaCodeList) 0), |
| 331 | jcId(gj0ClassName(foam, name)), NULL((void*)0), interfaces, body); |
| 332 | |
| 333 | imps = gj0CollectImports(clss); |
| 334 | |
| 335 | mainFile = jcFile(gjArgs->pkg == NULL((void*)0) ? NULL((void*)0) : jcId(strCopy(gjArgs->pkg)), |
| 336 | jcId(gj0ClassName(foam, name)), imps, clss); |
| 337 | exportedClassFiles = gj0ExportClassCreateAll(); |
| 338 | |
| 339 | return listCons(JavaCode)(JavaCode_listPointer->Cons)(mainFile, exportedClassFiles); |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * :: Options |
| 344 | */ |
| 345 | |
| 346 | void |
| 347 | gjGenSetMain(Bool flg) |
| 348 | { |
| 349 | gjArgs->createMain = flg; |
| 350 | } |
| 351 | |
| 352 | |
| 353 | localstatic JavaCodeList |
| 354 | gj0CollectImports(JavaCode clss) |
| 355 | { |
| 356 | JavaCodeList imps = jcCollectImports(clss); |
| 357 | JavaCodeList ids = listNil(JavaCode)((JavaCodeList) 0); |
| 358 | JavaCodeList tmp = imps; |
| 359 | while (tmp) { |
| 360 | JavaCode id = car(tmp)((tmp)->first); |
| 361 | JavaCode stmt = jcStatement(jcImport(id)); |
| 362 | ids = listCons(JavaCode)(JavaCode_listPointer->Cons)(stmt, ids); |
| 363 | tmp = cdr(tmp)((tmp)->rest); |
| 364 | } |
| 365 | ids = listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(ids); |
| 366 | listFree(JavaCode)(JavaCode_listPointer->Free)(imps); |
| 367 | |
| 368 | return ids; |
| 369 | } |
| 370 | |
| 371 | |
| 372 | localstatic void |
| 373 | gjInit(Foam foam, String name) |
| 374 | { |
| 375 | gj0NameInit(); |
| 376 | |
| 377 | gjContext->lvl = 0; |
| 378 | gjContext->formats = foam->foamUnit.formats; |
| 379 | gjContext->constants = foamUnitConstants(foam)((((foam)->foamUnit.formats)->foamGen.argv)[1].code); |
| 380 | gjContext->defs = foam->foamUnit.defs; |
| 381 | gjContext->ccallStubSigList = listNil(FoamSig)((FoamSigList) 0); |
| 382 | gjContext->gloRegList = listNil(JavaCode)((JavaCodeList) 0); |
| 383 | gjContext->fmtSet = intSetNew(foamArgc(gjContext->formats)((gjContext->formats)->hdr.argc)); |
| 384 | gjContext->mfmt = 0; |
| 385 | gjContext->contextFmt = 0; |
| 386 | } |
| 387 | |
| 388 | localstatic String |
| 389 | gj0ClassName(Foam foam, String name) |
| 390 | { |
| 391 | return strCopy(name); |
| 392 | } |
| 393 | |
| 394 | localstatic String |
| 395 | gj0ClassDocumentation(Foam foam, String name) |
| 396 | { |
| 397 | return strPrintf("Generated by genjava - %s", name); |
| 398 | } |
| 399 | |
| 400 | |
| 401 | localstatic JavaCodeList |
| 402 | gj0DDef(Foam foam) |
| 403 | { |
| 404 | JavaCodeList lst = listNil(JavaCode)((JavaCodeList) 0); |
| 405 | JavaCodeList stubs; |
| 406 | int i; |
| 407 | |
| 408 | for (i=0; i<foamArgc(foam)((foam)->hdr.argc); i++) { |
| 409 | Foam fm = foam->foamDDef.argv[i]; |
| 410 | JavaCode code = gj0Gen(fm); |
| 411 | if (code != 0) |
| 412 | lst = listCons(JavaCode)(JavaCode_listPointer->Cons)(code, lst); |
| 413 | } |
| 414 | |
| 415 | lst = listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(lst); |
| 416 | |
| 417 | return lst; |
| 418 | } |
| 419 | |
| 420 | localstatic JavaCodeList |
| 421 | gj0ExportClassCreateAll() |
| 422 | { |
| 423 | Foam globals = gjContextGlobals(gjContext->formats->foamDFmt.argv[0]); |
| 424 | AIntList exportedMethods = listNil(AInt)((AIntList) 0); |
| 425 | AIntList list; |
| 426 | JavaCodeList allClasses = listNil(JavaCode)((JavaCodeList) 0); |
| 427 | int i; |
| 428 | Table tbl; |
| 429 | |
| 430 | tbl = tblNew((TblHashFun) jcoHash, (TblEqFun) jcoEqual); |
| 431 | for (i=0; i<foamDDeclArgc(globals)(((globals)->hdr.argc) - (1)); i++) { |
| 432 | Foam decl = globals->foamDDecl.argv[i]; |
| 433 | if (foamGDeclIsExportOf(FOAM_Proto_Java, decl) |
| 434 | || foamGDeclIsExportOf(FOAM_Proto_JavaConstructor, decl) |
| 435 | || foamGDeclIsExportOf(FOAM_Proto_JavaMethod, decl)) { |
| 436 | exportedMethods = listCons(AInt)(AInt_listPointer->Cons)(i, exportedMethods); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | list = exportedMethods; |
| 441 | while (list != listNil(AInt)((AIntList) 0)) { |
| 442 | JavaCode class; |
| 443 | AInt id = car(list)((list)->first); |
| 444 | Foam decl = globals->foamDDecl.argv[id]; |
| 445 | list = cdr(list)((list)->rest); |
| 446 | class = gj0ExportClassName(decl->foamGDecl.id); |
| 447 | tblSetElt(tbl, class, listCons(AInt)(AInt_listPointer->Cons)(id, |
| 448 | tblElt(tbl, class, listNil(AInt)((AIntList) 0)))); |
| 449 | } |
| 450 | |
| 451 | TableIterator it; |
| 452 | for (tblITER(it, tbl)_tblITER(&(it), tbl); tblMORE(it)((it).curr <= (it).last); tblSTEP(it)((((it).link=(it).link->next))==0 ? _tblSTEP(&(it)) : 1 )) { |
| 453 | JavaCode className = tblKEY(it)((it).link->key); |
| 454 | AIntList ids = tblELT(it)((it).link->elt); |
| 455 | |
| 456 | JavaCode clss = gj0ExportClassCreate(className, ids); |
| 457 | allClasses = listCons(JavaCode)(JavaCode_listPointer->Cons)(clss, allClasses); |
| 458 | } |
| 459 | |
| 460 | return allClasses; |
| 461 | } |
| 462 | |
| 463 | localstatic JavaCode |
| 464 | gj0ExportClassCreate(JavaCode classId, AIntList ids) |
| 465 | { |
| 466 | /* |
| 467 | * class Foo { |
| 468 | * Object rep; |
| 469 | * Foo(Word w) { |
| 470 | * this.rep = w; |
| 471 | * } |
| 472 | * } |
| 473 | */ |
| 474 | JavaCodeList body, imports, methods; |
| 475 | JavaCode file, clss, constructor, rep; |
| 476 | JavaCode className = jcId(jcImportedIdName(classId)); |
| 477 | JavaCode repVar = jcId(strCopy("rep")); |
| 478 | |
| 479 | constructor = jcConstructor(JCO_MOD_Public, NULL((void*)0), |
| 480 | jcoCopy(className), listNil(JavaCode)((JavaCodeList) 0), |
| 481 | listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(jcMemberDecl(0, |
| 482 | gj0TypeFrFmt(FOAM_Word, 0), |
| 483 | jcoCopy(repVar))), |
| 484 | listNil(JavaCode)((JavaCodeList) 0), |
| 485 | jcStatement(jcAssign(jcMemRef(jcThis(), jcoCopy(repVar)), |
| 486 | jcoCopy(repVar))) |
| 487 | ); |
| 488 | |
| 489 | rep = jcMethod(JCO_MOD_Public, NULL((void*)0), gj0Id(GJ_FoamWord), jcId(strCopy("rep")), listNil(JavaCode)((JavaCodeList) 0), |
| 490 | listNil(JavaCode)((JavaCodeList) 0), listNil(JavaCode)((JavaCodeList) 0), |
| 491 | jcStatement(jcReturn(jcId(strCopy("rep"))))); |
| 492 | |
| 493 | body = listList(JavaCode)(JavaCode_listPointer->List)(3, |
| 494 | jcStatement(jcMemberDecl(JCO_MOD_Final|JCO_MOD_Private, |
| 495 | gj0TypeFrFmt(FOAM_Word, 0), |
| 496 | jcoCopy(repVar))), |
| 497 | constructor, |
| 498 | rep); |
| 499 | |
| 500 | methods = gj0ExportClassMethods(className, ids); |
| 501 | |
| 502 | body = listNConcat(JavaCode)(JavaCode_listPointer->NConcat)(body, methods); |
| 503 | |
| 504 | clss = jcClass(JCO_MOD_Public|JCO_MOD_Final, |
| 505 | strCopy(".. ++ docco goes here"), |
| 506 | listNil(JavaCode)((JavaCodeList) 0), |
| 507 | jcoCopy(className), |
| 508 | gj0Id(GJ_AldorObject), |
| 509 | listNil(JavaCode)((JavaCodeList) 0), body); |
| 510 | |
| 511 | imports = gj0CollectImports(clss); |
| 512 | |
| 513 | file = jcFile(jcId(strCopy(jcImportedIdPkg(classId))), |
| 514 | jcId(strCopy(jcImportedIdName(classId))), imports, clss); |
| 515 | |
| 516 | jcoFree(repVar); |
| 517 | jcoFree(classId); |
| 518 | |
| 519 | return file; |
| 520 | } |
| 521 | |
| 522 | localstatic JavaCodeList |
| 523 | gj0ExportClassMethods(JavaCode className, AIntList decls) |
| 524 | { |
| 525 | JavaCodeList methods = listNil(JavaCode)((JavaCodeList) 0); |
| 526 | int i = 0; |
| 527 | while (decls != listNil(AInt)((AIntList) 0)) { |
| 528 | AInt id = car(decls)((decls)->first); |
| 529 | Foam decl = gjContextGlobals(gjContext->formats->foamDFmt.argv[0])->foamDDecl.argv[id]; |
Value stored to 'decl' during its initialization is never read | |
| 530 | JavaCode method; |
| 531 | |
| 532 | method = gj0ExportClassMethod(id); |
| 533 | |
| 534 | methods = listCons(JavaCode)(JavaCode_listPointer->Cons)(method, methods); |
| 535 | decls = cdr(decls)((decls)->rest); |
| 536 | i++; |
| 537 | } |
| 538 | |
| 539 | return listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(methods); |
| 540 | } |
| 541 | |
| 542 | localstatic JavaCode |
| 543 | gj0ExportClassMethod(AInt i) |
| 544 | { |
| 545 | /* |
| 546 | * Generate this for "foo: (A, B) -> C |
| 547 | * public static C foo(A a, B b) { |
| 548 | * Word ret = file.fooConst(a, b); |
| 549 | * return "something"(ret) |
| 550 | * } |
| 551 | */ |
| 552 | |
| 553 | Foam decl = gjContextGlobals(gjContext->formats->foamDFmt.argv[0])->foamDDecl.argv[i]; |
| 554 | |
| 555 | Foam sig = gjContext->formats->foamDFmt.argv[decl->foamGDecl.format]; |
| 556 | AInt protocol = decl->foamGDecl.protocol; |
| 557 | JavaCode retType = gj0Type(javaSigRet(sig)); |
| 558 | |
| 559 | JavaCodeList params = gj0ExportClassMethodParams(sig, protocol); |
| 560 | |
| 561 | JavaCodeList args = gj0ExportClassMethodArgs(sig, protocol); |
| 562 | JavaCode className = gj0ExportClassName(decl->foamDecl.id); |
| 563 | JavaCode methodName = gj0ExportMethodName(decl->foamDecl.id); |
| 564 | JavaCode instance = jcApplyMethod(gj0Id(GJ_FoamHelper), gj0Id(GJ_Instance), |
| 565 | listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(jcMemRef(jcImportedId(strCopy(gjArgs->pkg), |
| 566 | strCopy(gjArgs->name)), |
| 567 | gj0Id(GJ_class)))); |
| 568 | JavaCode fnCall = jcApplyMethod(instance, gj0GloJavaMethodId(i), args); |
| 569 | JavaCode body; |
| 570 | AInt flgs; |
| 571 | |
| 572 | switch (protocol) { |
| 573 | case FOAM_Proto_JavaMethod: |
| 574 | flgs = JCO_MOD_Public|JCO_MOD_Final; |
| 575 | body = (javaSigRet(sig)->foamDecl.type == FOAM_NOp) |
| 576 | ? jcStatement(fnCall) |
| 577 | : jcNLSeqV(1, jcStatement(jcReturn(fnCall))); |
| 578 | return jcMethod(flgs, strCopy("Method wrapper for... "), |
| 579 | retType, methodName, listNil(JavaCode)((JavaCodeList) 0), |
| 580 | params, listNil(JavaCode)((JavaCodeList) 0), body); |
| 581 | break; |
| 582 | case FOAM_Proto_Java: |
| 583 | flgs = JCO_MOD_Public|JCO_MOD_Static; |
| 584 | body = (javaSigRet(sig)->foamDecl.type == FOAM_NOp) |
| 585 | ? jcStatement(fnCall) |
| 586 | : jcNLSeqV(1, jcStatement(jcReturn(fnCall))); |
| 587 | return jcMethod(flgs, strCopy("Method wrapper for... "), |
| 588 | retType, methodName, listNil(JavaCode)((JavaCodeList) 0), |
| 589 | params, listNil(JavaCode)((JavaCodeList) 0), body); |
| 590 | break; |
| 591 | case FOAM_Proto_JavaConstructor: { |
| 592 | flgs = JCO_MOD_Public; |
| 593 | body = jcNLSeqV(1, jcStatement(jcApplyV(jcThis(), 1, fnCall))); |
| 594 | return jcConstructor(flgs, strCopy("Method wrapper for... "), |
| 595 | className, listNil(JavaCode)((JavaCodeList) 0), |
| 596 | params, listNil(JavaCode)((JavaCodeList) 0), body); |
| 597 | } |
| 598 | default: |
| 599 | bug("Unknown java protocol for export"); |
| 600 | break; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | localstatic JavaCode |
| 605 | gj0ExportClassName(String id) |
| 606 | { |
| 607 | JavaCode fullName = jcImportedStaticIdFrString(id); |
| 608 | JavaCode className = jcImportedId(strCopy(jcImportedStaticIdPkg(fullName)), |
| 609 | strCopy(jcImportedStaticIdClass(fullName))); |
| 610 | |
| 611 | jcoFree(fullName); |
| 612 | |
| 613 | return className; |
| 614 | } |
| 615 | |
| 616 | localstatic JavaCode |
| 617 | gj0ExportMethodName(String id) |
| 618 | { |
| 619 | JavaCode fullName = jcImportedStaticIdFrString(id); |
| 620 | String methodName = jcImportedStaticIdName(fullName); |
| 621 | |
| 622 | jcoFree(fullName); |
| 623 | |
| 624 | return jcId(gj0NameFrString(methodName)); |
| 625 | } |
| 626 | |
| 627 | localstatic JavaCodeList |
| 628 | gj0ExportClassMemberVars() |
| 629 | { |
| 630 | Foam globals = gjContextGlobals(gjContext->formats->foamDFmt.argv[0]); |
| 631 | JavaCodeList vars = listNil(JavaCode)((JavaCodeList) 0); |
| 632 | int i; |
| 633 | |
| 634 | for (i=0; i<foamDDeclArgc(globals)(((globals)->hdr.argc) - (1)); i++) { |
| 635 | Foam decl = globals->foamDDecl.argv[i]; |
| 636 | if (foamGDeclIsExportOf(FOAM_Proto_Java, decl) |
| 637 | || foamGDeclIsExportOf(FOAM_Proto_JavaConstructor, decl) |
| 638 | || foamGDeclIsExportOf(FOAM_Proto_JavaMethod, decl)) { |
| 639 | JavaCode decl = jcMemberDecl(JCO_MOD_Private, gj0Id(GJ_FoamClos), gj0GloJavaId(i)); |
| 640 | vars = listCons(JavaCode)(JavaCode_listPointer->Cons)(jcStatement(decl), vars); |
| 641 | } |
| 642 | } |
| 643 | return listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(vars); |
| 644 | } |
| 645 | |
| 646 | localstatic JavaCodeList |
| 647 | gj0ExportClassInternalMethods() |
| 648 | { |
| 649 | JavaCodeList methods = listNil(JavaCode)((JavaCodeList) 0); |
| 650 | int i; |
| 651 | |
| 652 | for (i=0; i<foamDDeclArgc(gjContextGlobals)((((gjContext->formats->foamDFmt.argv[0]))->hdr.argc ) - (1)); i++) { |
| 653 | Foam decl = gjContextGlobals(gjContext->formats->foamDFmt.argv[0])->foamDDecl.argv[i]; |
| 654 | if (foamGDeclIsExportOf(FOAM_Proto_Java, decl) |
| 655 | || foamGDeclIsExportOf(FOAM_Proto_JavaConstructor, decl) |
| 656 | || foamGDeclIsExportOf(FOAM_Proto_JavaMethod, decl)) { |
| 657 | JavaCode method = gj0ExportClassInternalMethod(i); |
| 658 | methods = listCons(JavaCode)(JavaCode_listPointer->Cons)(method, methods); |
| 659 | } |
| 660 | } |
| 661 | gj0ProgAddStubs(gjContext->progSigList); |
| 662 | |
| 663 | return listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(methods); |
| 664 | } |
| 665 | |
| 666 | localstatic JavaCode |
| 667 | gj0ExportClassInternalMethod(int index) |
| 668 | { |
| 669 | Foam decl = gjContextGlobals(gjContext->formats->foamDFmt.argv[0])->foamDDecl.argv[index]; |
| 670 | /* |
| 671 | * Call the global containing the exported method. |
| 672 | * The code is a top level method, called as a static java method |
| 673 | * hence args are given by FOAM_Proto_Java |
| 674 | */ |
| 675 | AInt protocol = decl->foamGDecl.protocol; |
| 676 | Foam sig = gjContext->formats->foamDFmt.argv[decl->foamGDecl.format]; |
| 677 | JavaCodeList params = gj0ExportClassMethodParams(sig, FOAM_Proto_Java); |
| 678 | JavaCodeList args = gj0ExportClassMethodArgs(sig, FOAM_Proto_Java); |
| 679 | |
| 680 | FoamSig foamSig = javaSigCreateFoamSig(sig); |
| 681 | JavaCode base = gj0CCallSig(foamSig, gj0GloJavaId(index), args); |
| 682 | JavaCode body; |
| 683 | if (foamSig->retType == FOAM_NOp) { |
| 684 | body = jcStatement(base); |
| 685 | } |
| 686 | else if (protocol == FOAM_Proto_JavaConstructor) { |
| 687 | body = jcStatement(jcReturn(base)); |
| 688 | } |
| 689 | else { |
| 690 | JavaCode call = jcCast(gj0Type(javaSigRet(sig)), base); |
| 691 | body = jcStatement(jcReturn(call)); |
| 692 | } |
| 693 | JavaCode method = jcMethod(JCO_MOD_Public, 0, |
| 694 | gj0Type(javaSigRet(sig)), |
| 695 | gj0GloJavaMethodId(index), |
| 696 | listNil(JavaCode)((JavaCodeList) 0), |
| 697 | params, |
| 698 | listNil(JavaCode)((JavaCodeList) 0), |
| 699 | body); |
| 700 | gj0ProgAddStub(foamSig); |
| 701 | |
| 702 | return method; |
| 703 | } |
| 704 | |
| 705 | localstatic JavaCodeList |
| 706 | gj0ExportClassMethodParams(Foam sig, AInt proto) |
| 707 | { |
| 708 | JavaCodeList params = listNil(JavaCode)((JavaCodeList) 0); |
| 709 | int offs = proto == FOAM_Proto_JavaMethod ? 1 : 0; |
| 710 | for (int i=0; i<javaSigArgc(sig)-offs; i++) { |
| 711 | params = listCons(JavaCode)(JavaCode_listPointer->Cons)(jcParamDecl(JCO_MOD_Final, |
| 712 | gj0Type(javaSigArgN(sig, i+offs)), |
| 713 | jcId(gj0JavaSigName("_", sig, i))), |
| 714 | params); |
| 715 | } |
| 716 | params = listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(params); |
| 717 | |
| 718 | return params; |
| 719 | } |
| 720 | |
| 721 | localstatic JavaCodeList |
| 722 | gj0ExportClassMethodArgs(Foam sig, AInt proto) |
| 723 | { |
| 724 | JavaCodeList args = listNil(JavaCode)((JavaCodeList) 0); |
| 725 | int offs = proto == FOAM_Proto_JavaMethod ? 1 : 0; |
| 726 | |
| 727 | if (proto == FOAM_Proto_JavaMethod) { |
| 728 | args = listCons(JavaCode)(JavaCode_listPointer->Cons)(jcThis(), args); |
| 729 | } |
| 730 | for (int i=offs; i<javaSigArgc(sig); i++) { |
| 731 | args = listCons(JavaCode)(JavaCode_listPointer->Cons)(jcId(gj0JavaSigName("_", sig, i-offs)), |
| 732 | args); |
| 733 | } |
| 734 | args = listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(args); |
| 735 | |
| 736 | return args; |
| 737 | } |
| 738 | |
| 739 | |
| 740 | /* |
| 741 | * :: Foam operations |
| 742 | */ |
| 743 | |
| 744 | localstatic JavaCode |
| 745 | gj0Gen(Foam foam) |
| 746 | { |
| 747 | return gj0GenFmt(foam, -1); |
| 748 | } |
| 749 | |
| 750 | localstatic JavaCode |
| 751 | gj0GenFmt(Foam foam, AInt fmt) |
| 752 | { |
| 753 | JavaCode jc; |
| 754 | AInt oldFmt = gjContext->contextFmt; |
| 755 | gjContext->contextFmt = fmt; |
| 756 | jc = gj0Gen0(foam); |
| 757 | gjContext->contextFmt = oldFmt; |
| 758 | return jc; |
| 759 | } |
| 760 | |
| 761 | localstatic JavaCode |
| 762 | gj0Gen0(Foam foam) |
| 763 | { |
| 764 | switch (foamTag(foam)((foam)->hdr.tag)) { |
| 765 | case FOAM_NOp: |
| 766 | return jcComment(strCopy("NOp")); |
| 767 | case FOAM_Def: |
| 768 | return gj0Def(foam); |
| 769 | case FOAM_Set: |
| 770 | return gj0Set(foam->foamSet.lhs, foam->foamSet.rhs); |
| 771 | case FOAM_MFmt: |
| 772 | return gj0MFmt(foam); |
| 773 | case FOAM_Par: |
| 774 | return gj0Par(foam); |
| 775 | case FOAM_Loc: |
| 776 | return gj0Loc(foam); |
| 777 | case FOAM_Glo: |
| 778 | return gj0Glo(foam); |
| 779 | case FOAM_Seq: |
| 780 | return gj0Seq(foam); |
| 781 | case FOAM_Throw: |
| 782 | return gj0Throw(foam); |
| 783 | case FOAM_SInt: |
| 784 | return gj0SInt(foam); |
| 785 | case FOAM_HInt: |
| 786 | return gj0HInt(foam); |
| 787 | case FOAM_SFlo: |
| 788 | return gj0SFlo(foam); |
| 789 | case FOAM_DFlo: |
| 790 | return gj0DFlo(foam); |
| 791 | case FOAM_BInt: |
| 792 | return gj0BInt(foam); |
| 793 | case FOAM_Byte: |
| 794 | return gj0Byte(foam); |
| 795 | case FOAM_Char: |
| 796 | return gj0Char(foam); |
| 797 | case FOAM_Bool: |
| 798 | return gj0Bool(foam); |
| 799 | case FOAM_Nil: |
| 800 | return gj0Nil(foam); |
| 801 | case FOAM_Cast: |
| 802 | return gj0Cast(foam); |
| 803 | case FOAM_CCall: |
| 804 | return gj0CCall(foam); |
| 805 | case FOAM_OCall: |
| 806 | return gj0OCall(foam); |
| 807 | case FOAM_PCall: |
| 808 | return gj0PCall(foam); |
| 809 | case FOAM_BCall: |
| 810 | return gj0BCall(foam); |
| 811 | case FOAM_Return: |
| 812 | return gj0Return(foam); |
| 813 | case FOAM_Yield: |
| 814 | bug("FOAM_Yield: should not be here"); |
| 815 | return jcNull(); |
| 816 | case FOAM_ANew: |
| 817 | return gj0ANew(foam); |
| 818 | case FOAM_AElt: |
| 819 | return gj0AElt(foam); |
| 820 | case FOAM_Arr: |
| 821 | return gj0Arr(foam); |
| 822 | case FOAM_RNew: |
| 823 | return gj0RNew(foam); |
| 824 | case FOAM_RElt: |
| 825 | return gj0RElt(foam); |
| 826 | case FOAM_Free: |
| 827 | return gj0Free(foam); |
| 828 | case FOAM_Env: |
| 829 | return gj0Env(foam); |
| 830 | case FOAM_Lex: |
| 831 | return gj0Lex(foam); |
| 832 | case FOAM_EElt: |
| 833 | return gj0EElt(foam); |
| 834 | case FOAM_EEnv: |
| 835 | return gj0EEnv(foam); |
| 836 | case FOAM_EInfo: |
| 837 | return gj0EInfo(foam); |
| 838 | case FOAM_EEnsure: |
| 839 | return gj0EEnsure(foam); |
| 840 | case FOAM_PushEnv: |
| 841 | return gj0PushEnv(foam); |
| 842 | case FOAM_Clos: |
| 843 | return gj0Clos(foam); |
| 844 | case FOAM_CEnv: |
| 845 | return gj0CEnv(foam); |
| 846 | case FOAM_CProg: |
| 847 | return gj0CProg(foam); |
| 848 | case FOAM_PRef: |
| 849 | return gj0PRef(foam); |
| 850 | case FOAM_Const: |
| 851 | return gj0Const(foam); |
| 852 | case FOAM_Gener: |
| 853 | return gj0Gener(foam); |
| 854 | case FOAM_GenIter: |
| 855 | return gj0GenIter(foam); |
| 856 | case FOAM_GenerValue: |
| 857 | return gj0GenerValue(foam); |
| 858 | default: |
| 859 | return gj0Default(foam, strPrintf("Tag: %s", foamStr(foamTag(foam))((foamInfoTable [(int)(((foam)->hdr.tag))-(int)FOAM_START] ).str))); |
| 860 | } |
| 861 | |
| 862 | } |
| 863 | |
| 864 | localstatic JavaCodeList |
| 865 | gj0GenList(Foam *argv, AInt n) |
| 866 | { |
| 867 | JavaCodeList args = listNil(JavaCode)((JavaCodeList) 0); |
| 868 | int i; |
| 869 | for (i=0; i<n; i++) { |
| 870 | Foam elt = argv[i]; |
| 871 | args = listCons(JavaCode)(JavaCode_listPointer->Cons)(gj0Gen(elt), args); |
| 872 | } |
| 873 | args = listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(args); |
| 874 | return args; |
| 875 | } |
| 876 | |
| 877 | |
| 878 | localstatic JavaCode |
| 879 | gj0Def(Foam foam) { |
| 880 | Foam lhs, rhs; |
| 881 | JavaCode jc; |
| 882 | |
| 883 | assert(foamTag(foam) == FOAM_Def)do { if (!(((foam)->hdr.tag) == FOAM_Def)) _do_assert(("foamTag(foam) == FOAM_Def" ),"java/genjava.c",883); } while (0); |
| 884 | |
| 885 | lhs = foam->foamDef.lhs; |
| 886 | rhs = foam->foamDef.rhs; |
| 887 | switch (foamTag(lhs)((lhs)->hdr.tag)) { |
| 888 | case FOAM_Const: |
| 889 | if (foamTag(rhs)((rhs)->hdr.tag) == FOAM_Prog) |
| 890 | jc = gj0Prog(lhs, rhs); |
| 891 | else |
| 892 | jc = gj0TopConst(lhs, rhs); |
| 893 | break; |
| 894 | case FOAM_Glo: |
| 895 | if (gjContext->lvl == 0) |
| 896 | jc = gj0GloRegister(lhs, rhs); |
| 897 | else |
| 898 | jc = gj0Set(lhs, rhs); |
| 899 | break; |
| 900 | default: |
| 901 | if (gjContext->lvl == 0) |
| 902 | jc = gj0ClosInit(lhs, rhs); |
| 903 | else |
| 904 | jc = gj0Set(lhs, rhs); |
| 905 | break; |
| 906 | } |
| 907 | return jc; |
| 908 | } |
| 909 | |
| 910 | localstatic JavaCode |
| 911 | gj0ClosInit(Foam lhs, Foam rhs) |
| 912 | { |
| 913 | |
| 914 | return gj0Default(lhs, strCopy("closInit")); |
| 915 | } |
| 916 | |
| 917 | localstatic JavaCode |
| 918 | gj0TopConst(Foam lhs, Foam rhs) |
| 919 | { |
| 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | /* |
| 924 | * :: Context globals |
| 925 | */ |
| 926 | localstatic JavaCode |
| 927 | gj0GloRegister(Foam lhs, Foam rhs) |
| 928 | { |
| 929 | JavaCode ref = gj0Gen(lhs); |
| 930 | gjContext->gloRegList = listCons(JavaCode)(JavaCode_listPointer->Cons)(ref, gjContext->gloRegList); |
| 931 | |
| 932 | return 0; |
| 933 | } |
| 934 | |
| 935 | /* |
| 936 | * :: Prog |
| 937 | */ |
| 938 | |
| 939 | typedef struct gjProgResult { |
| 940 | FoamSigList sigs; |
| 941 | JavaCode fnDef; |
| 942 | JavaCode body; |
| 943 | JavaCodeList vars; |
| 944 | JavaCodeList args; |
| 945 | JavaCode retnType; |
| 946 | JavaCode id; |
| 947 | String comment; |
| 948 | int modifiers; |
| 949 | } *GjProgResult; |
| 950 | |
| 951 | localstatic GjProgResult gj0ProgMain(Foam f); |
| 952 | localstatic JavaCode gj0ProgResultToJava(GjProgResult r); |
| 953 | localstatic void gj0ProgResultFree(GjProgResult r); |
| 954 | localstatic void gj0ProgResultFree(GjProgResult r); |
| 955 | localstatic JavaCode gj0ProgEnvArg(Foam foam); |
| 956 | localstatic JavaCode gj0ProgEnv0Arg(Foam foam); |
| 957 | localstatic JavaCodeList gj0ProgEnvInitCreate(Foam f); |
| 958 | localstatic JavaCode gj0ProgEnvDecl(int idx, Foam denv); |
| 959 | localstatic JavaCode gj0ProgLvlDecl(int idx, Foam denv); |
| 960 | localstatic void gj0ProgEnvInitCollect(Foam f, BitvClass clss, |
| 961 | Bitv refMask, Bitv envMask); |
| 962 | localstatic JavaCode gj0ProgFnCreate(Foam lhs, Foam prog); |
| 963 | localstatic JavaCode gj0ProgGFnCreate(Foam lhs, Foam prog); |
| 964 | localstatic JavaCode gj0ProgFnMethodBody(Foam lhs, Foam prog); |
| 965 | localstatic void gj0ProgInitVars(IntSet set, Foam body); |
| 966 | localstatic JavaCode gj0ProgDecl(Foam ddecl, int idx, Bool isSet); |
| 967 | localstatic JavaCode gj0ProgDeclDefaultValue(Foam decl); |
| 968 | localstatic void gj0SeqHaltFlush(Foam foam); |
| 969 | localstatic Foam gj0FlattenProg(Foam rhs); |
| 970 | |
| 971 | localstatic JavaCode |
| 972 | gj0Prog(Foam lhs, Foam rhs) |
| 973 | { |
| 974 | GjProgResult r; |
| 975 | JavaCode code; |
| 976 | assert(foamTag(rhs) == FOAM_Prog)do { if (!(((rhs)->hdr.tag) == FOAM_Prog)) _do_assert(("foamTag(rhs) == FOAM_Prog" ),"java/genjava.c",976); } while (0); |
| 977 | |
| 978 | rhs = gj0FlattenProg(rhs); |
| 979 | gj0ProgInit(lhs, rhs); |
| 980 | r = gj0ProgMain(rhs); |
| 981 | code = gj0ProgResultToJava(r); |
| 982 | gj0ProgResultFree(r); |
| 983 | |
| 984 | gj0ProgFini(lhs, rhs); |
| 985 | |
| 986 | return code; |
| 987 | } |
| 988 | |
| 989 | localstatic void |
| 990 | gj0ProgInit(Foam lhs, Foam prog) |
| 991 | { |
| 992 | gjContext->lvl++; |
| 993 | gjContext->prog = prog; |
| 994 | gjContext->progParams = prog->foamProg.params; |
| 995 | gjContext->progLocals = prog->foamProg.locals; |
| 996 | gjContext->progLhs = lhs; |
| 997 | gjContext->progSigList = listNil(FoamSig)((FoamSigList) 0); |
| 998 | gjContext->multVarIdx = 0; |
| 999 | } |
| 1000 | |
| 1001 | localstatic void |
| 1002 | gj0ProgFini(Foam lhs, Foam rhs) |
| 1003 | { |
| 1004 | gjContext->lvl--; |
| 1005 | gjContext->progParams = NULL((void*)0); |
| 1006 | gjContext->progLocals = NULL((void*)0); |
| 1007 | |
| 1008 | gj0ProgAddStubs(gjContext->progSigList); |
| 1009 | listFree(FoamSig)(FoamSig_listPointer->Free)(gjContext->progSigList); |
| 1010 | |
| 1011 | gjContext->progSigList = listNil(FoamSig)((FoamSigList) 0); |
| 1012 | } |
| 1013 | |
| 1014 | localstatic GjProgResult |
| 1015 | gj0ProgMain(Foam f) |
| 1016 | { |
| 1017 | GjProgResult r; |
| 1018 | JavaCodeList args, declarations, envInit, inits; |
| 1019 | JavaCodeList whole; |
| 1020 | JavaCode body, fnDef; |
| 1021 | String methodName; |
| 1022 | JavaCodeList exns; |
| 1023 | int modifiers; |
| 1024 | |
| 1025 | |
| 1026 | modifiers = gj0ProgModifiers(); |
| 1027 | methodName = gj0ProgMethodName(gjContext->progLhs); |
| 1028 | |
| 1029 | gjDEBUGif (genJavaDebug) fprintf(dbOut, "(Entering prog: %s\n", methodName); |
| 1030 | |
| 1031 | args = gj0ProgCollectArgs(f); |
| 1032 | |
| 1033 | gj0SeqHaltFlush(f->foamProg.body); |
| 1034 | |
| 1035 | declarations = gj0ProgDeclarations(gjContext->progLocals, |
| 1036 | f->foamProg.body); |
| 1037 | exns = gj0ProgExceptions(); |
| 1038 | envInit = gj0ProgEnvInitCreate(f); |
| 1039 | |
| 1040 | if (foamProgIsCoroutine(f)((f)->foamProg.infoBits & (1 << 7))) { |
| 1041 | fnDef = gj0ProgGFnCreate(gjContext->progLhs, f); |
| 1042 | } |
| 1043 | else { |
| 1044 | fnDef = gj0ProgFnCreate(gjContext->progLhs, f); |
| 1045 | } |
| 1046 | r = (GjProgResult) stoAlloc(OB_Other0, sizeof(*r)); |
| 1047 | body = gj0ProgGenerateBody(f); |
| 1048 | |
| 1049 | inits = listNConcat(JavaCode)(JavaCode_listPointer->NConcat)(declarations, envInit); |
| 1050 | whole = listNConcat(JavaCode)(JavaCode_listPointer->NConcat)(inits, listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(body)); |
| 1051 | |
| 1052 | r->fnDef = fnDef; |
| 1053 | r->comment = 0; |
| 1054 | r->modifiers = modifiers; |
| 1055 | r->body = jcNLSeq(whole); |
| 1056 | r->args = args; |
| 1057 | r->retnType = gj0ProgRetnType(f); |
| 1058 | r->id = jcId(methodName); |
| 1059 | |
| 1060 | gjDEBUGif (genJavaDebug) fprintf(dbOut, " Completed prog: %s)\n", methodName); |
| 1061 | |
| 1062 | return r; |
| 1063 | } |
| 1064 | |
| 1065 | localstatic JavaCode |
| 1066 | gj0ProgResultToJava(GjProgResult r) |
| 1067 | { |
| 1068 | JavaCode method; |
| 1069 | |
| 1070 | method = jcMethod(r->modifiers, r->comment, r->retnType, |
| 1071 | r->id, NULL((void*)0), r->args, NULL((void*)0), r->body); |
| 1072 | |
| 1073 | return jcNLSeqV(2, r->fnDef, method); |
| 1074 | } |
| 1075 | |
| 1076 | |
| 1077 | localstatic void |
| 1078 | gj0ProgResultFree(GjProgResult r) |
| 1079 | { |
| 1080 | stoFree(r); |
| 1081 | } |
| 1082 | |
| 1083 | |
| 1084 | localstatic JavaCode |
| 1085 | gj0ProgRetnType(Foam prog) |
| 1086 | { |
| 1087 | return gj0TypeFrFmt(prog->foamProg.retType, prog->foamProg.format); |
| 1088 | } |
| 1089 | |
| 1090 | localstatic String |
| 1091 | gj0ProgMethodName(Foam var) |
| 1092 | { |
| 1093 | Foam format; |
| 1094 | int idx = var->foamConst.index; |
| 1095 | format = gjContext->constants; |
| 1096 | |
| 1097 | return gj0Name("c", format, idx); |
| 1098 | } |
| 1099 | |
| 1100 | localstatic String |
| 1101 | gj0ProgFnName(int idx) |
| 1102 | { |
| 1103 | Foam format; |
| 1104 | format = gjContext->constants; |
| 1105 | |
| 1106 | return gj0Name("C", format, idx); |
| 1107 | } |
| 1108 | |
| 1109 | localstatic JavaCodeList |
| 1110 | gj0ProgCollectArgs(Foam prog) |
| 1111 | { |
| 1112 | JavaCodeList lst; |
| 1113 | Foam fmt = prog->foamProg.params; |
| 1114 | int i; |
| 1115 | |
| 1116 | lst = listNil(JavaCode)((JavaCodeList) 0); |
| 1117 | |
| 1118 | lst = listCons(JavaCode)(JavaCode_listPointer->Cons)(gj0ProgEnvArg(prog), lst); |
| 1119 | if (foamProgIsCoroutine(prog)((prog)->foamProg.infoBits & (1 << 7))) { |
| 1120 | lst = listCons(JavaCode)(JavaCode_listPointer->Cons)(gj0ProgEnv0Arg(prog), lst); |
| 1121 | } |
| 1122 | for (i=0; i<foamDDeclArgc(fmt)(((fmt)->hdr.argc) - (1)); i++) { |
| 1123 | Foam decl = fmt->foamDDecl.argv[i]; |
| 1124 | String name = gj0Name("p", fmt, i); |
| 1125 | JavaCode type = gj0Type(decl); |
| 1126 | lst = listCons(JavaCode)(JavaCode_listPointer->Cons)(jcParamDecl(0, type, jcId(name)), lst); |
| 1127 | } |
| 1128 | |
| 1129 | return listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(lst); |
| 1130 | } |
| 1131 | |
| 1132 | localstatic JavaCode |
| 1133 | gj0ProgEnvArg(Foam foam) |
| 1134 | { |
| 1135 | JavaCode type = gj0TypeFrFmt(FOAM_Env, 0); |
| 1136 | return jcParamDecl(0, type, gj0EnvId(1)); |
| 1137 | } |
| 1138 | |
| 1139 | localstatic JavaCode |
| 1140 | gj0ProgEnv0Arg(Foam foam) |
| 1141 | { |
| 1142 | // For coroutines |
| 1143 | JavaCode type = gj0TypeFrFmt(FOAM_Env, 0); |
| 1144 | return jcParamDecl(0, type, gj0EnvId(0)); |
| 1145 | } |
| 1146 | |
| 1147 | /** Extract the declarations from the given foam. |
| 1148 | * XXX This needs to be tied into the sequence stuff when |
| 1149 | * we get goto-reduction done. |
| 1150 | */ |
| 1151 | localstatic JavaCodeList |
| 1152 | gj0ProgDeclarations(Foam ddecl, Foam body) |
| 1153 | { |
| 1154 | Table tbl = tblNew((TblHashFun) jcoHash, (TblEqFun) jcoEqual); |
| 1155 | TableIterator it; |
| 1156 | JavaCodeList decls; |
| 1157 | IntSet initted; |
| 1158 | int i=0; |
| 1159 | initted = intSetNew(foamDDeclArgc(ddecl)(((ddecl)->hdr.argc) - (1))); |
| 1160 | |
| 1161 | |
| 1162 | gj0ProgInitVars(initted, body); |
| 1163 | gjDEBUGif (genJavaDebug) fprintf(dbOut, "InitVars: %s\n", intSetToString(initted)); |
| 1164 | |
| 1165 | foamIter(ddecl, pdecl, {{ { String argf = (foamInfoTable [(int)(((ddecl)->hdr.tag) )-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < ((ddecl )->hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pdecl = (Foam *) ((ddecl)->foamGen .argv)+_i; { { JavaCode type = gj0Type(*pdecl); JavaCodeList l = (JavaCodeList) tblElt(tbl, type, ((JavaCodeList) 0)); l = ( JavaCode_listPointer->Cons)(gj0ProgDecl(ddecl, i, intSetMember (initted, i)), l); tblSetElt(tbl, type, l); i++; }; }; } } }; } |
| 1166 | JavaCode type = gj0Type(*pdecl);{ { String argf = (foamInfoTable [(int)(((ddecl)->hdr.tag) )-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < ((ddecl )->hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pdecl = (Foam *) ((ddecl)->foamGen .argv)+_i; { { JavaCode type = gj0Type(*pdecl); JavaCodeList l = (JavaCodeList) tblElt(tbl, type, ((JavaCodeList) 0)); l = ( JavaCode_listPointer->Cons)(gj0ProgDecl(ddecl, i, intSetMember (initted, i)), l); tblSetElt(tbl, type, l); i++; }; }; } } }; } |
| 1167 | JavaCodeList l = (JavaCodeList) tblElt(tbl, type,{ { String argf = (foamInfoTable [(int)(((ddecl)->hdr.tag) )-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < ((ddecl )->hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pdecl = (Foam *) ((ddecl)->foamGen .argv)+_i; { { JavaCode type = gj0Type(*pdecl); JavaCodeList l = (JavaCodeList) tblElt(tbl, type, ((JavaCodeList) 0)); l = ( JavaCode_listPointer->Cons)(gj0ProgDecl(ddecl, i, intSetMember (initted, i)), l); tblSetElt(tbl, type, l); i++; }; }; } } }; } |
| 1168 | listNil(JavaCode));{ { String argf = (foamInfoTable [(int)(((ddecl)->hdr.tag) )-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < ((ddecl )->hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pdecl = (Foam *) ((ddecl)->foamGen .argv)+_i; { { JavaCode type = gj0Type(*pdecl); JavaCodeList l = (JavaCodeList) tblElt(tbl, type, ((JavaCodeList) 0)); l = ( JavaCode_listPointer->Cons)(gj0ProgDecl(ddecl, i, intSetMember (initted, i)), l); tblSetElt(tbl, type, l); i++; }; }; } } }; } |
| 1169 | l = listCons(JavaCode)(gj0ProgDecl(ddecl, i,{ { String argf = (foamInfoTable [(int)(((ddecl)->hdr.tag) )-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < ((ddecl )->hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pdecl = (Foam *) ((ddecl)->foamGen .argv)+_i; { { JavaCode type = gj0Type(*pdecl); JavaCodeList l = (JavaCodeList) tblElt(tbl, type, ((JavaCodeList) 0)); l = ( JavaCode_listPointer->Cons)(gj0ProgDecl(ddecl, i, intSetMember (initted, i)), l); tblSetElt(tbl, type, l); i++; }; }; } } }; } |
| 1170 | intSetMember(initted, i)),{ { String argf = (foamInfoTable [(int)(((ddecl)->hdr.tag) )-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < ((ddecl )->hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pdecl = (Foam *) ((ddecl)->foamGen .argv)+_i; { { JavaCode type = gj0Type(*pdecl); JavaCodeList l = (JavaCodeList) tblElt(tbl, type, ((JavaCodeList) 0)); l = ( JavaCode_listPointer->Cons)(gj0ProgDecl(ddecl, i, intSetMember (initted, i)), l); tblSetElt(tbl, type, l); i++; }; }; } } }; } |
| 1171 | l);{ { String argf = (foamInfoTable [(int)(((ddecl)->hdr.tag) )-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < ((ddecl )->hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pdecl = (Foam *) ((ddecl)->foamGen .argv)+_i; { { JavaCode type = gj0Type(*pdecl); JavaCodeList l = (JavaCodeList) tblElt(tbl, type, ((JavaCodeList) 0)); l = ( JavaCode_listPointer->Cons)(gj0ProgDecl(ddecl, i, intSetMember (initted, i)), l); tblSetElt(tbl, type, l); i++; }; }; } } }; } |
| 1172 | tblSetElt(tbl, type, l);{ { String argf = (foamInfoTable [(int)(((ddecl)->hdr.tag) )-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < ((ddecl )->hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pdecl = (Foam *) ((ddecl)->foamGen .argv)+_i; { { JavaCode type = gj0Type(*pdecl); JavaCodeList l = (JavaCodeList) tblElt(tbl, type, ((JavaCodeList) 0)); l = ( JavaCode_listPointer->Cons)(gj0ProgDecl(ddecl, i, intSetMember (initted, i)), l); tblSetElt(tbl, type, l); i++; }; }; } } }; } |
| 1173 | i++;{ { String argf = (foamInfoTable [(int)(((ddecl)->hdr.tag) )-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < ((ddecl )->hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pdecl = (Foam *) ((ddecl)->foamGen .argv)+_i; { { JavaCode type = gj0Type(*pdecl); JavaCodeList l = (JavaCodeList) tblElt(tbl, type, ((JavaCodeList) 0)); l = ( JavaCode_listPointer->Cons)(gj0ProgDecl(ddecl, i, intSetMember (initted, i)), l); tblSetElt(tbl, type, l); i++; }; }; } } }; } |
| 1174 | }){ { String argf = (foamInfoTable [(int)(((ddecl)->hdr.tag) )-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < ((ddecl )->hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pdecl = (Foam *) ((ddecl)->foamGen .argv)+_i; { { JavaCode type = gj0Type(*pdecl); JavaCodeList l = (JavaCodeList) tblElt(tbl, type, ((JavaCodeList) 0)); l = ( JavaCode_listPointer->Cons)(gj0ProgDecl(ddecl, i, intSetMember (initted, i)), l); tblSetElt(tbl, type, l); i++; }; }; } } }; }; |
| 1175 | |
| 1176 | decls = listNil(JavaCode)((JavaCodeList) 0); |
| 1177 | for (tblITER(it, tbl)_tblITER(&(it), tbl); tblMORE(it)((it).curr <= (it).last); tblSTEP(it)((((it).link=(it).link->next))==0 ? _tblSTEP(&(it)) : 1 )) { |
| 1178 | JavaCode type = tblKEY(it)((it).link->key); |
| 1179 | JavaCodeList vars = listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(tblELT(it)((it).link->elt)); |
| 1180 | JavaCode decl = jcLocalDecl(0, type, jcCommaSeq(vars)); |
| 1181 | decls = listCons(JavaCode)(JavaCode_listPointer->Cons)(jcStatement(decl), decls); |
| 1182 | } |
| 1183 | |
| 1184 | return decls; |
| 1185 | } |
| 1186 | |
| 1187 | localstatic JavaCode |
| 1188 | gj0ProgDecl(Foam ddecl, int idx, Bool isSet) |
| 1189 | { |
| 1190 | JavaCode var = jcId(gj0Name("t", ddecl, idx)); |
| 1191 | if (!isSet) { |
| 1192 | Foam decl = ddecl->foamDDecl.argv[idx]; |
| 1193 | var = jcAssign(var, gj0ProgDeclDefaultValue(decl)); |
| 1194 | } |
| 1195 | return var; |
| 1196 | } |
| 1197 | |
| 1198 | localstatic JavaCode |
| 1199 | gj0ProgDeclDefaultValue(Foam decl) |
| 1200 | { |
| 1201 | switch (decl->foamDecl.type) { |
| 1202 | case FOAM_Word: |
| 1203 | return jcKeyword(symInternConst("null")symProbe("null", 1)); |
| 1204 | case FOAM_Bool: |
| 1205 | return jcKeyword(symInternConst("false")symProbe("false", 1)); |
| 1206 | case FOAM_Char: |
| 1207 | return jcLiteralChar("\\0"); |
| 1208 | case FOAM_Byte: |
| 1209 | case FOAM_SInt: |
| 1210 | case FOAM_HInt: |
| 1211 | case FOAM_SFlo: |
| 1212 | case FOAM_DFlo: |
| 1213 | return jcLiteralInteger(0); |
| 1214 | default: |
| 1215 | return jcKeyword(symInternConst("null")symProbe("null", 1)); |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | |
| 1220 | /* |
| 1221 | * Find the variables that are provably initialised. |
| 1222 | */ |
| 1223 | localstatic void |
| 1224 | gj0ProgInitVars(IntSet set, Foam body) |
| 1225 | { |
| 1226 | Foam foam; |
| 1227 | int i; |
| 1228 | Bool done = false((int) 0); |
| 1229 | for (i=0; !done && i<foamArgc(body)((body)->hdr.argc); i++) { |
| 1230 | foam = body->foamSeq.argv[i]; |
| 1231 | switch (foamTag(foam)((foam)->hdr.tag)) { |
| 1232 | case FOAM_Set: |
| 1233 | if (foamTag(foam->foamSet.lhs)((foam->foamSet.lhs)->hdr.tag) == FOAM_Loc) { |
| 1234 | intSetAdd(set, |
| 1235 | foam->foamSet.lhs->foamLoc.index); |
| 1236 | } |
| 1237 | break; |
| 1238 | case FOAM_If: |
| 1239 | done = true1; |
| 1240 | break; |
| 1241 | case FOAM_Select: |
| 1242 | done = true1; |
| 1243 | break; |
| 1244 | case FOAM_GenerStep: |
| 1245 | done = true1; |
| 1246 | break; |
| 1247 | case FOAM_Label: |
| 1248 | done = true1; |
| 1249 | break; |
| 1250 | case FOAM_Goto: |
| 1251 | done = true1; |
| 1252 | break; |
| 1253 | } |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | localstatic JavaCode |
| 1258 | gj0ProgGenerateBody(Foam fm) |
| 1259 | { |
| 1260 | return gj0Gen(fm->foamProg.body); |
| 1261 | } |
| 1262 | |
| 1263 | localstatic JavaCodeList |
| 1264 | gj0ProgExceptions() |
| 1265 | { |
| 1266 | return listNil(JavaCode)((JavaCodeList) 0); |
| 1267 | } |
| 1268 | |
| 1269 | localstatic int |
| 1270 | gj0ProgModifiers() |
| 1271 | { |
| 1272 | return JCO_MOD_Private; |
| 1273 | } |
| 1274 | |
| 1275 | localstatic void |
| 1276 | gj0ProgAddStubs(FoamSigList sigList) |
| 1277 | { |
| 1278 | while (sigList != listNil(FoamSig)((FoamSigList) 0)) { |
| 1279 | gjContext->ccallStubSigList = |
| 1280 | gj0CCallStubAdd(gjContext->ccallStubSigList, |
| 1281 | car(sigList)((sigList)->first)); |
| 1282 | sigList = cdr(sigList)((sigList)->rest); |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | localstatic void |
| 1287 | gj0ProgAddStub(FoamSig sig) |
| 1288 | { |
| 1289 | gjContext->ccallStubSigList = gj0CCallStubAdd(gjContext->ccallStubSigList, sig); |
| 1290 | } |
| 1291 | |
| 1292 | localstatic JavaCodeList |
| 1293 | gj0ProgEnvInitCreate(Foam f) |
| 1294 | { |
| 1295 | JavaCodeList lines; |
| 1296 | Foam denv; |
| 1297 | Bitv refLvls, refEnvs; |
| 1298 | BitvClass clss; |
| 1299 | AInt lvlCount; |
| 1300 | int i, maxLvl, maxEnv, max; |
| 1301 | Bool isCoroutine; |
| 1302 | |
| 1303 | denv = f->foamProg.levels; |
| 1304 | lvlCount = foamArgc(denv)((denv)->hdr.argc); |
| 1305 | isCoroutine = foamProgIsCoroutine(f)((f)->foamProg.infoBits & (1 << 7)); |
| 1306 | |
| 1307 | clss = bitvClassCreate(lvlCount); |
| 1308 | refLvls = bitvNew(clss); |
| 1309 | refEnvs = bitvNew(clss); |
| 1310 | bitvClearAll(clss, refLvls); |
| 1311 | bitvClearAll(clss, refEnvs); |
| 1312 | |
| 1313 | gj0ProgEnvInitCollect(f->foamProg.body, clss, refLvls, refEnvs); |
| 1314 | maxLvl = bitvMax(clss, refLvls); |
| 1315 | maxEnv = bitvMax(clss, refEnvs); |
| 1316 | max = maxLvl>maxEnv ? maxLvl : maxEnv; |
| 1317 | |
| 1318 | lines = listNil(JavaCode)((JavaCodeList) 0); |
| 1319 | |
| 1320 | for (i=2; i<=max; i++) { |
| 1321 | JavaCode line; |
| 1322 | line = jcStatement(gj0ProgEnvDecl(i, denv)); |
| 1323 | lines = listCons(JavaCode)(JavaCode_listPointer->Cons)(line, lines); |
| 1324 | } |
| 1325 | |
| 1326 | if (bitvTest(clss, refEnvs, 0)) { |
| 1327 | AInt fmt = denv->foamDEnv.argv[0]; |
| 1328 | gj0FmtUse(fmt); |
| 1329 | } |
| 1330 | |
| 1331 | if (!isCoroutine && bitvTest(clss, refEnvs, 0)) { |
| 1332 | JavaCode line; |
| 1333 | JavaCode envRec; |
| 1334 | AInt fmt = denv->foamDEnv.argv[0]; |
| 1335 | |
| 1336 | if (fmt == 0) |
| 1337 | envRec = jcNull(); |
| 1338 | else { |
| 1339 | envRec = jcConstructV(gj0Id(GJ_EnvRecord), 1, |
| 1340 | gj0FmtId(fmt)); |
| 1341 | } |
| 1342 | line = jcInitialisation(JCO_MOD_Final,gj0TypeFrFmt(FOAM_Env, 0), |
| 1343 | gj0EnvId(0), jcConstructV(gj0TypeFrFmt(FOAM_Env, fmt), 2, |
| 1344 | envRec, |
| 1345 | gj0EnvId(1))); |
| 1346 | line = jcStatement(line); |
| 1347 | lines = listCons(JavaCode)(JavaCode_listPointer->Cons)(line, lines); |
| 1348 | |
| 1349 | } |
| 1350 | for (i=0; i<lvlCount; i++) { |
| 1351 | if (bitvTest(clss, refLvls, i)) { |
| 1352 | JavaCode line; |
| 1353 | line = jcStatement(gj0ProgLvlDecl(i, denv)); |
| 1354 | lines = listCons(JavaCode)(JavaCode_listPointer->Cons)(line, lines); |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | bitvFree(refLvls); |
| 1359 | bitvFree(refEnvs); |
| 1360 | bitvClassDestroy(clss); |
| 1361 | |
| 1362 | return listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(lines); |
| 1363 | } |
| 1364 | |
| 1365 | localstatic JavaCode |
| 1366 | gj0ProgEnvDecl(int idx, Foam denv) |
| 1367 | { |
| 1368 | return jcInitialisation(JCO_MOD_Final, gj0TypeFrFmt(FOAM_Env, 0), |
| 1369 | gj0EnvId(idx), |
| 1370 | jcApplyMethod(gj0EnvId(idx-1), |
| 1371 | jcId(strCopy("parent")), |
| 1372 | listNil(JavaCode)((JavaCodeList) 0)) |
| 1373 | ); |
| 1374 | } |
| 1375 | |
| 1376 | |
| 1377 | localstatic JavaCode |
| 1378 | gj0ProgLvlDecl(int idx, Foam denv) |
| 1379 | { |
| 1380 | return jcInitialisation(JCO_MOD_Final, gj0Id(GJ_EnvRecord), |
| 1381 | gj0LvlId(idx), |
| 1382 | jcApplyMethod(gj0EnvId(idx), |
| 1383 | jcId(strCopy("level")), |
| 1384 | listNil(JavaCode)((JavaCodeList) 0)) |
| 1385 | ); |
| 1386 | } |
| 1387 | |
| 1388 | |
| 1389 | localstatic void |
| 1390 | gj0ProgEnvInitCollect(Foam f, BitvClass clss, Bitv refMask, Bitv envMask) |
| 1391 | { |
| 1392 | foamIter(f, pelt, {{ { String argf = (foamInfoTable [(int)(((f)->hdr.tag))-(int )FOAM_START]).argf; Length _i; for (_i = 0; _i < ((f)-> hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pelt = (Foam *) ((f)->foamGen.argv)+_i; { { Foam elt = *pelt; switch (((elt)->hdr.tag)) { case FOAM_Lex : bitvSet(clss, refMask, elt->foamLex.level); bitvSet(clss , envMask, elt->foamLex.level); break; case FOAM_Env: bitvSet (clss, envMask, elt->foamEnv.level); break; default: gj0ProgEnvInitCollect (elt, clss, refMask, envMask); } }; }; } } }; } |
| 1393 | Foam elt = *pelt;{ { String argf = (foamInfoTable [(int)(((f)->hdr.tag))-(int )FOAM_START]).argf; Length _i; for (_i = 0; _i < ((f)-> hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pelt = (Foam *) ((f)->foamGen.argv)+_i; { { Foam elt = *pelt; switch (((elt)->hdr.tag)) { case FOAM_Lex : bitvSet(clss, refMask, elt->foamLex.level); bitvSet(clss , envMask, elt->foamLex.level); break; case FOAM_Env: bitvSet (clss, envMask, elt->foamEnv.level); break; default: gj0ProgEnvInitCollect (elt, clss, refMask, envMask); } }; }; } } }; } |
| 1394 | switch (foamTag(elt)) {{ { String argf = (foamInfoTable [(int)(((f)->hdr.tag))-(int )FOAM_START]).argf; Length _i; for (_i = 0; _i < ((f)-> hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pelt = (Foam *) ((f)->foamGen.argv)+_i; { { Foam elt = *pelt; switch (((elt)->hdr.tag)) { case FOAM_Lex : bitvSet(clss, refMask, elt->foamLex.level); bitvSet(clss , envMask, elt->foamLex.level); break; case FOAM_Env: bitvSet (clss, envMask, elt->foamEnv.level); break; default: gj0ProgEnvInitCollect (elt, clss, refMask, envMask); } }; }; } } }; } |
| 1395 | case FOAM_Lex:{ { String argf = (foamInfoTable [(int)(((f)->hdr.tag))-(int )FOAM_START]).argf; Length _i; for (_i = 0; _i < ((f)-> hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pelt = (Foam *) ((f)->foamGen.argv)+_i; { { Foam elt = *pelt; switch (((elt)->hdr.tag)) { case FOAM_Lex : bitvSet(clss, refMask, elt->foamLex.level); bitvSet(clss , envMask, elt->foamLex.level); break; case FOAM_Env: bitvSet (clss, envMask, elt->foamEnv.level); break; default: gj0ProgEnvInitCollect (elt, clss, refMask, envMask); } }; }; } } }; } |
| 1396 | bitvSet(clss, refMask, elt->foamLex.level);{ { String argf = (foamInfoTable [(int)(((f)->hdr.tag))-(int )FOAM_START]).argf; Length _i; for (_i = 0; _i < ((f)-> hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pelt = (Foam *) ((f)->foamGen.argv)+_i; { { Foam elt = *pelt; switch (((elt)->hdr.tag)) { case FOAM_Lex : bitvSet(clss, refMask, elt->foamLex.level); bitvSet(clss , envMask, elt->foamLex.level); break; case FOAM_Env: bitvSet (clss, envMask, elt->foamEnv.level); break; default: gj0ProgEnvInitCollect (elt, clss, refMask, envMask); } }; }; } } }; } |
| 1397 | bitvSet(clss, envMask, elt->foamLex.level);{ { String argf = (foamInfoTable [(int)(((f)->hdr.tag))-(int )FOAM_START]).argf; Length _i; for (_i = 0; _i < ((f)-> hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pelt = (Foam *) ((f)->foamGen.argv)+_i; { { Foam elt = *pelt; switch (((elt)->hdr.tag)) { case FOAM_Lex : bitvSet(clss, refMask, elt->foamLex.level); bitvSet(clss , envMask, elt->foamLex.level); break; case FOAM_Env: bitvSet (clss, envMask, elt->foamEnv.level); break; default: gj0ProgEnvInitCollect (elt, clss, refMask, envMask); } }; }; } } }; } |
| 1398 | break;{ { String argf = (foamInfoTable [(int)(((f)->hdr.tag))-(int )FOAM_START]).argf; Length _i; for (_i = 0; _i < ((f)-> hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pelt = (Foam *) ((f)->foamGen.argv)+_i; { { Foam elt = *pelt; switch (((elt)->hdr.tag)) { case FOAM_Lex : bitvSet(clss, refMask, elt->foamLex.level); bitvSet(clss , envMask, elt->foamLex.level); break; case FOAM_Env: bitvSet (clss, envMask, elt->foamEnv.level); break; default: gj0ProgEnvInitCollect (elt, clss, refMask, envMask); } }; }; } } }; } |
| 1399 | case FOAM_Env:{ { String argf = (foamInfoTable [(int)(((f)->hdr.tag))-(int )FOAM_START]).argf; Length _i; for (_i = 0; _i < ((f)-> hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pelt = (Foam *) ((f)->foamGen.argv)+_i; { { Foam elt = *pelt; switch (((elt)->hdr.tag)) { case FOAM_Lex : bitvSet(clss, refMask, elt->foamLex.level); bitvSet(clss , envMask, elt->foamLex.level); break; case FOAM_Env: bitvSet (clss, envMask, elt->foamEnv.level); break; default: gj0ProgEnvInitCollect (elt, clss, refMask, envMask); } }; }; } } }; } |
| 1400 | bitvSet(clss, envMask, elt->foamEnv.level);{ { String argf = (foamInfoTable [(int)(((f)->hdr.tag))-(int )FOAM_START]).argf; Length _i; for (_i = 0; _i < ((f)-> hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pelt = (Foam *) ((f)->foamGen.argv)+_i; { { Foam elt = *pelt; switch (((elt)->hdr.tag)) { case FOAM_Lex : bitvSet(clss, refMask, elt->foamLex.level); bitvSet(clss , envMask, elt->foamLex.level); break; case FOAM_Env: bitvSet (clss, envMask, elt->foamEnv.level); break; default: gj0ProgEnvInitCollect (elt, clss, refMask, envMask); } }; }; } } }; } |
| 1401 | break;{ { String argf = (foamInfoTable [(int)(((f)->hdr.tag))-(int )FOAM_START]).argf; Length _i; for (_i = 0; _i < ((f)-> hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pelt = (Foam *) ((f)->foamGen.argv)+_i; { { Foam elt = *pelt; switch (((elt)->hdr.tag)) { case FOAM_Lex : bitvSet(clss, refMask, elt->foamLex.level); bitvSet(clss , envMask, elt->foamLex.level); break; case FOAM_Env: bitvSet (clss, envMask, elt->foamEnv.level); break; default: gj0ProgEnvInitCollect (elt, clss, refMask, envMask); } }; }; } } }; } |
| 1402 | default:{ { String argf = (foamInfoTable [(int)(((f)->hdr.tag))-(int )FOAM_START]).argf; Length _i; for (_i = 0; _i < ((f)-> hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pelt = (Foam *) ((f)->foamGen.argv)+_i; { { Foam elt = *pelt; switch (((elt)->hdr.tag)) { case FOAM_Lex : bitvSet(clss, refMask, elt->foamLex.level); bitvSet(clss , envMask, elt->foamLex.level); break; case FOAM_Env: bitvSet (clss, envMask, elt->foamEnv.level); break; default: gj0ProgEnvInitCollect (elt, clss, refMask, envMask); } }; }; } } }; } |
| 1403 | gj0ProgEnvInitCollect(elt, clss, refMask, envMask);{ { String argf = (foamInfoTable [(int)(((f)->hdr.tag))-(int )FOAM_START]).argf; Length _i; for (_i = 0; _i < ((f)-> hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pelt = (Foam *) ((f)->foamGen.argv)+_i; { { Foam elt = *pelt; switch (((elt)->hdr.tag)) { case FOAM_Lex : bitvSet(clss, refMask, elt->foamLex.level); bitvSet(clss , envMask, elt->foamLex.level); break; case FOAM_Env: bitvSet (clss, envMask, elt->foamEnv.level); break; default: gj0ProgEnvInitCollect (elt, clss, refMask, envMask); } }; }; } } }; } |
| 1404 | }{ { String argf = (foamInfoTable [(int)(((f)->hdr.tag))-(int )FOAM_START]).argf; Length _i; for (_i = 0; _i < ((f)-> hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pelt = (Foam *) ((f)->foamGen.argv)+_i; { { Foam elt = *pelt; switch (((elt)->hdr.tag)) { case FOAM_Lex : bitvSet(clss, refMask, elt->foamLex.level); bitvSet(clss , envMask, elt->foamLex.level); break; case FOAM_Env: bitvSet (clss, envMask, elt->foamEnv.level); break; default: gj0ProgEnvInitCollect (elt, clss, refMask, envMask); } }; }; } } }; } |
| 1405 | }){ { String argf = (foamInfoTable [(int)(((f)->hdr.tag))-(int )FOAM_START]).argf; Length _i; for (_i = 0; _i < ((f)-> hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *pelt = (Foam *) ((f)->foamGen.argv)+_i; { { Foam elt = *pelt; switch (((elt)->hdr.tag)) { case FOAM_Lex : bitvSet(clss, refMask, elt->foamLex.level); bitvSet(clss , envMask, elt->foamLex.level); break; case FOAM_Env: bitvSet (clss, envMask, elt->foamEnv.level); break; default: gj0ProgEnvInitCollect (elt, clss, refMask, envMask); } }; }; } } }; }; |
| 1406 | } |
| 1407 | |
| 1408 | /* |
| 1409 | static Fn CFxx = new Fn() { |
| 1410 | public Value ocall(Env env, Value ... vals) { |
| 1411 | return cxx(env, vals[0], vals[1]); |
| 1412 | } |
| 1413 | } |
| 1414 | */ |
| 1415 | localstatic JavaCode |
| 1416 | gj0ProgFnCreate(Foam lhs, Foam prog) |
| 1417 | { |
| 1418 | JavaCodeList args; |
| 1419 | JavaCode method; |
| 1420 | JavaCode methodBody; |
| 1421 | JavaCode fnDef; |
| 1422 | JavaCode anonClass; |
| 1423 | String name; |
| 1424 | |
| 1425 | args = listList(JavaCode)(JavaCode_listPointer->List)(2, |
| 1426 | jcParamDecl(0, gj0TypeFrFmt(FOAM_Env, 0), |
| 1427 | jcId(strCopy("env"))), |
| 1428 | jcParamDecl(0, |
| 1429 | jcNAry(gj0Id(GJ_FoamValue)), |
| 1430 | jcId(strCopy("vals")))); |
| 1431 | |
| 1432 | methodBody = gj0ProgFnMethodBody(lhs, prog); |
| 1433 | |
| 1434 | method = jcMethod(JCO_MOD_Public, 0, gj0Id(GJ_FoamValue), |
| 1435 | jcId(strCopy("ocall")), |
| 1436 | listNil(JavaCode)((JavaCodeList) 0), |
| 1437 | args, |
| 1438 | listNil(JavaCode)((JavaCodeList) 0), |
| 1439 | methodBody); |
| 1440 | |
| 1441 | name = gj0ProgFnName(lhs->foamConst.index); |
| 1442 | anonClass = jcConstructSubclass(gj0Id(GJ_FoamFn), |
| 1443 | listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(jcLiteralString(strCopy(name))), |
| 1444 | jcNLSeqV(1, method)); |
| 1445 | fnDef = jcInitialisation(JCO_MOD_Private, |
| 1446 | gj0Id(GJ_FoamFn), |
| 1447 | jcId(strCopy(name)), |
| 1448 | anonClass); |
| 1449 | strFree(name); |
| 1450 | return jcStatement(fnDef); |
| 1451 | } |
| 1452 | |
| 1453 | localstatic JavaCode |
| 1454 | gj0ProgGFnCreate(Foam lhs, Foam prog) |
| 1455 | { |
| 1456 | JavaCodeList fnArgs; |
| 1457 | JavaCodeList args; |
| 1458 | JavaCode method; |
| 1459 | JavaCode methodBody; |
| 1460 | JavaCode fnDef; |
| 1461 | JavaCode anonClass; |
| 1462 | String name; |
| 1463 | |
| 1464 | args = listList(JavaCode)(JavaCode_listPointer->List)(3, |
| 1465 | jcParamDecl(0, gj0TypeFrFmt(FOAM_Env, 0), |
| 1466 | jcId(strCopy("env"))), |
| 1467 | jcParamDecl(0, gj0TypeFrFmt(FOAM_Env, 0), |
| 1468 | jcId(strCopy("env0"))), |
| 1469 | jcParamDecl(0, |
| 1470 | jcNAry(gj0Id(GJ_FoamValue)), |
| 1471 | jcId(strCopy("vals")))); |
| 1472 | |
| 1473 | methodBody = gj0ProgFnMethodBody(lhs, prog); |
| 1474 | |
| 1475 | method = jcMethod(JCO_MOD_Public, 0, gj0Id(GJ_FoamValue), |
| 1476 | jcId(strCopy("ocall")), |
| 1477 | listNil(JavaCode)((JavaCodeList) 0), |
| 1478 | args, |
| 1479 | listNil(JavaCode)((JavaCodeList) 0), |
| 1480 | methodBody); |
| 1481 | |
| 1482 | name = gj0ProgFnName(lhs->foamConst.index); |
| 1483 | if (foamProgIsLeaf(prog)((prog)->foamProg.infoBits & (1 << 1))) { |
| 1484 | fnArgs = listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(jcLiteralString(strCopy(name))); |
| 1485 | } |
| 1486 | else { |
| 1487 | AInt fmt = prog->foamProg.levels->foamDEnv.argv[0]; |
| 1488 | fnArgs = listList(JavaCode)(JavaCode_listPointer->List)(2, jcLiteralString(strCopy(name)), gj0FmtId(fmt)); |
| 1489 | } |
| 1490 | |
| 1491 | anonClass = jcConstructSubclass(gj0Id(GJ_FoamGFn), |
| 1492 | fnArgs, |
| 1493 | jcNLSeqV(1, method)); |
| 1494 | fnDef = jcInitialisation(JCO_MOD_Private, |
| 1495 | gj0Id(GJ_FoamGFn), |
| 1496 | jcId(strCopy(name)), |
| 1497 | anonClass); |
| 1498 | strFree(name); |
| 1499 | return jcStatement(fnDef); |
| 1500 | } |
| 1501 | |
| 1502 | localstatic JavaCode |
| 1503 | gj0ProgFnMethodBody(Foam lhs, Foam prog) |
| 1504 | { |
| 1505 | Foam pddecl = prog->foamProg.params; |
| 1506 | JavaCodeList l; |
| 1507 | JavaCode call; |
| 1508 | int i; |
| 1509 | JavaCodeList ret; |
| 1510 | |
| 1511 | l = listNil(JavaCode)((JavaCodeList) 0); |
| 1512 | l = listCons(JavaCode)(JavaCode_listPointer->Cons)(jcId(strCopy("env")), l); |
| 1513 | |
| 1514 | |
| 1515 | if (foamProgIsCoroutine(prog)((prog)->foamProg.infoBits & (1 << 7))) { |
| 1516 | l = listCons(JavaCode)(JavaCode_listPointer->Cons)(jcId(strCopy("env0")), l); |
| 1517 | } |
| 1518 | |
| 1519 | i = 0; |
| 1520 | foamIter(pddecl, pdecl, {{ { String argf = (foamInfoTable [(int)(((pddecl)->hdr.tag ))-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < (( pddecl)->hdr.argc); _i++, argf++) { if (*argf == '*') argf --; if (*argf == 'C') { Foam *pdecl = (Foam *) ((pddecl)-> foamGen.argv)+_i; { { Foam decl = *pdecl; JavaCode arg = jcArrayRef (jcId(strCopy("vals")), jcLiteralInteger(i)); JavaCode castArg = gj0TypeValueToObj(arg, decl->foamDecl.type, decl->foamDecl .format); l = (JavaCode_listPointer->Cons)(castArg, l); i++ ; }; }; } } }; } |
| 1521 | Foam decl = *pdecl;{ { String argf = (foamInfoTable [(int)(((pddecl)->hdr.tag ))-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < (( pddecl)->hdr.argc); _i++, argf++) { if (*argf == '*') argf --; if (*argf == 'C') { Foam *pdecl = (Foam *) ((pddecl)-> foamGen.argv)+_i; { { Foam decl = *pdecl; JavaCode arg = jcArrayRef (jcId(strCopy("vals")), jcLiteralInteger(i)); JavaCode castArg = gj0TypeValueToObj(arg, decl->foamDecl.type, decl->foamDecl .format); l = (JavaCode_listPointer->Cons)(castArg, l); i++ ; }; }; } } }; } |
| 1522 | JavaCode arg = jcArrayRef(jcId(strCopy("vals")),{ { String argf = (foamInfoTable [(int)(((pddecl)->hdr.tag ))-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < (( pddecl)->hdr.argc); _i++, argf++) { if (*argf == '*') argf --; if (*argf == 'C') { Foam *pdecl = (Foam *) ((pddecl)-> foamGen.argv)+_i; { { Foam decl = *pdecl; JavaCode arg = jcArrayRef (jcId(strCopy("vals")), jcLiteralInteger(i)); JavaCode castArg = gj0TypeValueToObj(arg, decl->foamDecl.type, decl->foamDecl .format); l = (JavaCode_listPointer->Cons)(castArg, l); i++ ; }; }; } } }; } |
| 1523 | jcLiteralInteger(i));{ { String argf = (foamInfoTable [(int)(((pddecl)->hdr.tag ))-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < (( pddecl)->hdr.argc); _i++, argf++) { if (*argf == '*') argf --; if (*argf == 'C') { Foam *pdecl = (Foam *) ((pddecl)-> foamGen.argv)+_i; { { Foam decl = *pdecl; JavaCode arg = jcArrayRef (jcId(strCopy("vals")), jcLiteralInteger(i)); JavaCode castArg = gj0TypeValueToObj(arg, decl->foamDecl.type, decl->foamDecl .format); l = (JavaCode_listPointer->Cons)(castArg, l); i++ ; }; }; } } }; } |
| 1524 | JavaCode castArg = gj0TypeValueToObj(arg,{ { String argf = (foamInfoTable [(int)(((pddecl)->hdr.tag ))-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < (( pddecl)->hdr.argc); _i++, argf++) { if (*argf == '*') argf --; if (*argf == 'C') { Foam *pdecl = (Foam *) ((pddecl)-> foamGen.argv)+_i; { { Foam decl = *pdecl; JavaCode arg = jcArrayRef (jcId(strCopy("vals")), jcLiteralInteger(i)); JavaCode castArg = gj0TypeValueToObj(arg, decl->foamDecl.type, decl->foamDecl .format); l = (JavaCode_listPointer->Cons)(castArg, l); i++ ; }; }; } } }; } |
| 1525 | decl->foamDecl.type,{ { String argf = (foamInfoTable [(int)(((pddecl)->hdr.tag ))-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < (( pddecl)->hdr.argc); _i++, argf++) { if (*argf == '*') argf --; if (*argf == 'C') { Foam *pdecl = (Foam *) ((pddecl)-> foamGen.argv)+_i; { { Foam decl = *pdecl; JavaCode arg = jcArrayRef (jcId(strCopy("vals")), jcLiteralInteger(i)); JavaCode castArg = gj0TypeValueToObj(arg, decl->foamDecl.type, decl->foamDecl .format); l = (JavaCode_listPointer->Cons)(castArg, l); i++ ; }; }; } } }; } |
| 1526 | decl->foamDecl.format);{ { String argf = (foamInfoTable [(int)(((pddecl)->hdr.tag ))-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < (( pddecl)->hdr.argc); _i++, argf++) { if (*argf == '*') argf --; if (*argf == 'C') { Foam *pdecl = (Foam *) ((pddecl)-> foamGen.argv)+_i; { { Foam decl = *pdecl; JavaCode arg = jcArrayRef (jcId(strCopy("vals")), jcLiteralInteger(i)); JavaCode castArg = gj0TypeValueToObj(arg, decl->foamDecl.type, decl->foamDecl .format); l = (JavaCode_listPointer->Cons)(castArg, l); i++ ; }; }; } } }; } |
| 1527 | l = listCons(JavaCode)(castArg, l);{ { String argf = (foamInfoTable [(int)(((pddecl)->hdr.tag ))-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < (( pddecl)->hdr.argc); _i++, argf++) { if (*argf == '*') argf --; if (*argf == 'C') { Foam *pdecl = (Foam *) ((pddecl)-> foamGen.argv)+_i; { { Foam decl = *pdecl; JavaCode arg = jcArrayRef (jcId(strCopy("vals")), jcLiteralInteger(i)); JavaCode castArg = gj0TypeValueToObj(arg, decl->foamDecl.type, decl->foamDecl .format); l = (JavaCode_listPointer->Cons)(castArg, l); i++ ; }; }; } } }; } |
| 1528 | i++;{ { String argf = (foamInfoTable [(int)(((pddecl)->hdr.tag ))-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < (( pddecl)->hdr.argc); _i++, argf++) { if (*argf == '*') argf --; if (*argf == 'C') { Foam *pdecl = (Foam *) ((pddecl)-> foamGen.argv)+_i; { { Foam decl = *pdecl; JavaCode arg = jcArrayRef (jcId(strCopy("vals")), jcLiteralInteger(i)); JavaCode castArg = gj0TypeValueToObj(arg, decl->foamDecl.type, decl->foamDecl .format); l = (JavaCode_listPointer->Cons)(castArg, l); i++ ; }; }; } } }; } |
| 1529 | |
| 1530 | }){ { String argf = (foamInfoTable [(int)(((pddecl)->hdr.tag ))-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < (( pddecl)->hdr.argc); _i++, argf++) { if (*argf == '*') argf --; if (*argf == 'C') { Foam *pdecl = (Foam *) ((pddecl)-> foamGen.argv)+_i; { { Foam decl = *pdecl; JavaCode arg = jcArrayRef (jcId(strCopy("vals")), jcLiteralInteger(i)); JavaCode castArg = gj0TypeValueToObj(arg, decl->foamDecl.type, decl->foamDecl .format); l = (JavaCode_listPointer->Cons)(castArg, l); i++ ; }; }; } } }; }; |
| 1531 | l = listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(l); |
| 1532 | |
| 1533 | call = jcApply(jcId(gj0ProgMethodName(lhs)), l); |
| 1534 | |
| 1535 | |
| 1536 | if (foamTypeIsVoid(gjContext->formats, |
| 1537 | prog->foamProg.retType, |
| 1538 | prog->foamProg.format)) { |
| 1539 | JavaCode l1 = call; |
| 1540 | JavaCode l2 = jcReturn(jcNull()); |
| 1541 | ret = listList(JavaCode)(JavaCode_listPointer->List)(2, |
| 1542 | jcStatement(l1), |
| 1543 | jcStatement(l2)); |
| 1544 | } |
| 1545 | else if (foamTypeIsMulti(gjContext->formats, |
| 1546 | prog->foamProg.retType, |
| 1547 | prog->foamProg.format)) { |
| 1548 | JavaCode l1, l2; |
| 1549 | l1 = jcInitialisation(0, gj0TypeFrFmt(prog->foamProg.retType, |
| 1550 | prog->foamProg.format), |
| 1551 | jcId(strCopy("ret")), |
| 1552 | call); |
| 1553 | l2 = gj0TypeObjToValue(jcId(strCopy("ret")), |
| 1554 | prog->foamProg.retType, |
| 1555 | prog->foamProg.format); |
| 1556 | l2 = jcReturn(l2); |
| 1557 | ret = listList(JavaCode)(JavaCode_listPointer->List)(2, jcStatement(l1), jcStatement(l2)); |
| 1558 | } |
| 1559 | else { |
| 1560 | JavaCode l1, l2; |
| 1561 | l1 = jcInitialisation(0, gj0TypeFrFmt(prog->foamProg.retType, |
| 1562 | prog->foamProg.format), |
| 1563 | jcId(strCopy("ret")), |
| 1564 | call); |
| 1565 | l2 = gj0TypeObjToValue(jcId(strCopy("ret")), |
| 1566 | prog->foamProg.retType, |
| 1567 | prog->foamProg.format); |
| 1568 | l2 = jcReturn(l2); |
| 1569 | ret = listList(JavaCode)(JavaCode_listPointer->List)(2, jcStatement(l1), jcStatement(l2)); |
| 1570 | } |
| 1571 | |
| 1572 | return jcNLSeq(ret); |
| 1573 | } |
| 1574 | |
| 1575 | /* |
| 1576 | * :: Flatten |
| 1577 | */ |
| 1578 | |
| 1579 | /* |
| 1580 | * Idea here is that java pcalls which can throw exceptions have to be |
| 1581 | * at top level. So we need to scan the prog and lift anything |
| 1582 | * embedded in an expression. |
| 1583 | */ |
| 1584 | |
| 1585 | typedef struct gjFlattenResult { |
| 1586 | FoamList stmts; |
| 1587 | FoamBox locals; |
| 1588 | } *GjFlattenResult; |
| 1589 | |
| 1590 | localstatic Foam gj0FlattenStmt(GjFlattenResult changes, Foam expr); |
| 1591 | localstatic Foam gj0FlattenExpr(GjFlattenResult changes, Foam expr, Bool topLevel); |
| 1592 | |
| 1593 | localstatic GjFlattenResult gj0FlattenNewResult(Foam prog); |
| 1594 | localstatic Foam gj0FlattenNewLocal(GjFlattenResult changes, Foam pcall); |
| 1595 | localstatic void gj0FlattenAddStmt(GjFlattenResult changes, Foam stmt); |
| 1596 | |
| 1597 | localstatic Foam |
| 1598 | gj0FlattenProg(Foam prog) |
| 1599 | { |
| 1600 | GjFlattenResult changes; |
| 1601 | Foam seq; |
| 1602 | int i; |
| 1603 | |
| 1604 | assert(foamTag(prog) == FOAM_Prog)do { if (!(((prog)->hdr.tag) == FOAM_Prog)) _do_assert(("foamTag(prog) == FOAM_Prog" ),"java/genjava.c",1604); } while (0); |
| 1605 | if (!foamFindFirst(gj0PCallThrowsException, prog)) { |
| 1606 | return prog; |
| 1607 | } |
| 1608 | |
| 1609 | changes = gj0FlattenNewResult(prog); |
| 1610 | seq = prog->foamProg.body; |
| 1611 | |
| 1612 | for (i=0; i<foamArgc(seq)((seq)->hdr.argc); i++) { |
| 1613 | gj0FlattenAddStmt(changes, gj0FlattenStmt(changes, seq->foamSeq.argv[i])); |
| 1614 | } |
| 1615 | |
| 1616 | prog->foamProg.locals = fboxMake(changes->locals); |
| 1617 | prog->foamProg.body = foamNewOfList(FOAM_Seq, listNReverse(Foam)(Foam_listPointer->NReverse)(changes->stmts)); |
| 1618 | |
| 1619 | return prog; |
| 1620 | } |
| 1621 | |
| 1622 | localstatic Foam |
| 1623 | gj0FlattenStmt(GjFlattenResult changes, Foam expr) |
| 1624 | { |
| 1625 | return gj0FlattenExpr(changes, expr, true1); |
| 1626 | } |
| 1627 | |
| 1628 | localstatic Foam |
| 1629 | gj0FlattenExpr(GjFlattenResult changes, Foam expr, Bool topLevel) |
| 1630 | { |
| 1631 | if (topLevel) { |
| 1632 | switch (foamTag(expr)((expr)->hdr.tag)) { |
| 1633 | case FOAM_Set: |
| 1634 | case FOAM_Def: |
| 1635 | case FOAM_PCall: |
| 1636 | if (gj0PCallThrowsException(expr)) { |
| 1637 | topLevel = true1; |
| 1638 | } |
| 1639 | break; |
| 1640 | default: |
| 1641 | topLevel = false((int) 0); |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | foamIter(expr, psubexpr, {{ { String argf = (foamInfoTable [(int)(((expr)->hdr.tag)) -(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < ((expr )->hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *psubexpr = (Foam *) ((expr)->foamGen .argv)+_i; { { *psubexpr = gj0FlattenExpr(changes, *psubexpr, topLevel); }; }; } } }; } |
| 1646 | *psubexpr = gj0FlattenExpr(changes, *psubexpr, topLevel);{ { String argf = (foamInfoTable [(int)(((expr)->hdr.tag)) -(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < ((expr )->hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *psubexpr = (Foam *) ((expr)->foamGen .argv)+_i; { { *psubexpr = gj0FlattenExpr(changes, *psubexpr, topLevel); }; }; } } }; } |
| 1647 | }){ { String argf = (foamInfoTable [(int)(((expr)->hdr.tag)) -(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < ((expr )->hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *psubexpr = (Foam *) ((expr)->foamGen .argv)+_i; { { *psubexpr = gj0FlattenExpr(changes, *psubexpr, topLevel); }; }; } } }; }; |
| 1648 | |
| 1649 | if (!topLevel && gj0PCallThrowsException(expr)) { |
| 1650 | Foam loc = gj0FlattenNewLocal(changes, expr); |
| 1651 | Foam def = foamNewDef(loc, expr)foamNew(FOAM_Def, 2, loc, expr); |
| 1652 | return loc; |
| 1653 | } |
| 1654 | return expr; |
| 1655 | } |
| 1656 | |
| 1657 | localstatic GjFlattenResult |
| 1658 | gj0FlattenNewResult(Foam prog) |
| 1659 | { |
| 1660 | GjFlattenResult result; |
| 1661 | |
| 1662 | result = (GjFlattenResult) stoAlloc(OB_Other0, sizeof(*result)); |
| 1663 | result->stmts = listNil(Foam)((FoamList) 0); |
| 1664 | result->locals = fboxNew(prog->foamProg.locals); |
| 1665 | |
| 1666 | return result; |
| 1667 | } |
| 1668 | |
| 1669 | localstatic Foam |
| 1670 | gj0FlattenNewLocal(GjFlattenResult changes, Foam pcall) |
| 1671 | { |
| 1672 | Foam gdecl, sig, newdecl, def, retdecl; |
| 1673 | int idx; |
| 1674 | gdecl = gjContextGlobal(pcall->foamPCall.op->foamGlo.index)((gjContext->formats->foamDFmt.argv[0])->foamDDecl.argv [pcall->foamPCall.op->foamGlo.index]); |
| 1675 | retdecl = javaSigRet(gjContext->formats->foamDFmt.argv[gdecl->foamGDecl.format]); |
| 1676 | |
| 1677 | newdecl = foamNewDecl(retdecl->foamDecl.type, strCopy("val"), retdecl->foamDecl.format)foamNew(FOAM_Decl,4,(AInt)(retdecl->foamDecl.type),strCopy ("val"), (AInt) (0x7FFF), retdecl->foamDecl.format); |
| 1678 | idx = fboxAdd(changes->locals, newdecl); |
| 1679 | |
| 1680 | def = foamNewDef(foamNewLoc(idx), pcall)foamNew(FOAM_Def, 2, foamNew(FOAM_Loc, 1, (AInt)(idx)), pcall ); |
| 1681 | gj0FlattenAddStmt(changes, def); |
| 1682 | |
| 1683 | return foamNewLoc(idx)foamNew(FOAM_Loc, 1, (AInt)(idx)); |
| 1684 | } |
| 1685 | |
| 1686 | localstatic void |
| 1687 | gj0FlattenAddStmt(GjFlattenResult changes, Foam stmt) |
| 1688 | { |
| 1689 | changes->stmts = listCons(Foam)(Foam_listPointer->Cons)(stmt, changes->stmts); |
| 1690 | } |
| 1691 | |
| 1692 | |
| 1693 | |
| 1694 | /* |
| 1695 | * :: Java Types |
| 1696 | * |
| 1697 | * The mapping is as follows: |
| 1698 | * Char --> char |
| 1699 | * Bool --> boolean |
| 1700 | * Byte --> byte |
| 1701 | * HInt --> short |
| 1702 | * SInt --> int |
| 1703 | * SFlo --> float |
| 1704 | * DFlo --> double |
| 1705 | * Arr --> array[x] or Object |
| 1706 | * Word --> foamj.Word |
| 1707 | * NOp --> void [return types] |
| 1708 | * Clos --> foamj.Clos |
| 1709 | * Env --> foamj.Env |
| 1710 | */ |
| 1711 | |
| 1712 | localstatic JavaCode |
| 1713 | gj0Type(Foam decl) |
| 1714 | { |
| 1715 | return gj0TypeFrFmt(decl->foamDecl.type, decl->foamDecl.format); |
| 1716 | } |
| 1717 | |
| 1718 | localstatic JavaCode |
| 1719 | gj0TypeFrFmt(AInt id, AInt fmt) |
| 1720 | { |
| 1721 | switch (id) { |
| 1722 | case FOAM_Char: |
| 1723 | return jcKeyword(symInternConst("char")symProbe("char", 1)); |
| 1724 | case FOAM_SInt: |
| 1725 | return jcKeyword(symInternConst("int")symProbe("int", 1)); |
| 1726 | case FOAM_SFlo: |
| 1727 | return jcKeyword(symInternConst("float")symProbe("float", 1)); |
| 1728 | case FOAM_DFlo: |
| 1729 | return jcKeyword(symInternConst("double")symProbe("double", 1)); |
| 1730 | case FOAM_HInt: |
| 1731 | return jcKeyword(symInternConst("short")symProbe("short", 1)); |
| 1732 | case FOAM_Bool: |
| 1733 | return jcKeyword(symInternConst("boolean")symProbe("boolean", 1)); |
| 1734 | case FOAM_Byte: |
| 1735 | return jcKeyword(symInternConst("byte")symProbe("byte", 1)); |
| 1736 | case FOAM_BInt: |
| 1737 | return jcImportedId(strCopy("java.math"), |
| 1738 | strCopy("BigInteger")); |
| 1739 | case FOAM_NOp: |
| 1740 | if (fmt == 0 || fmt == emptyFormatSlot4 |
| 1741 | || foamDDeclArgc(gjContext->formats->foamDFmt.argv[fmt])(((gjContext->formats->foamDFmt.argv[fmt])->hdr.argc ) - (1)) == 0) |
| 1742 | return jcKeyword(symInternConst("void")symProbe("void", 1)); |
| 1743 | else |
| 1744 | return gj0Id(GJ_Multi); |
| 1745 | case FOAM_Gener: |
| 1746 | return gj0Id(GJ_FoamGener); |
| 1747 | case FOAM_GenIter: |
| 1748 | return gj0Id(GJ_FoamGenIter); |
| 1749 | case FOAM_Clos: |
| 1750 | return gj0Id(GJ_FoamClos); |
| 1751 | case FOAM_Word: |
| 1752 | return gj0Id(GJ_FoamWord); |
| 1753 | case FOAM_Env: |
| 1754 | return gj0Id(GJ_FoamEnv); |
| 1755 | case FOAM_Rec: |
| 1756 | return gj0Id(GJ_FoamRecord); |
| 1757 | case FOAM_Ptr: |
| 1758 | return gj0Id(GJ_Object); |
| 1759 | case FOAM_Nil: |
| 1760 | return gj0Id(GJ_FoamWord); |
| 1761 | case FOAM_Arr: |
| 1762 | if (fmt != 0) |
| 1763 | return jcArrayOf(gj0TypeFrFmt(fmt, 0)); |
| 1764 | else |
| 1765 | return gj0Id(GJ_Object); |
| 1766 | case FOAM_Values: |
| 1767 | return gj0Id(GJ_Multi); |
| 1768 | case FOAM_JavaObj: |
| 1769 | if (fmt != 0 && fmt != emptyFormatSlot4) |
| 1770 | return gj0TypeFrJavaObj(gjContext->formats->foamDFmt.argv[fmt]); |
| 1771 | else |
| 1772 | return gj0Id(GJ_Object); |
| 1773 | default: |
| 1774 | return jcId(strCopy(foamStr(id)((foamInfoTable [(int)(id)-(int)FOAM_START]).str))); |
| 1775 | } |
| 1776 | } |
| 1777 | |
| 1778 | localstatic JavaCode |
| 1779 | gj0TypeFrJavaObj(Foam format) |
| 1780 | { |
| 1781 | String txt = format->foamDDecl.argv[0]->foamDecl.id; |
| 1782 | if (foamDDeclArgc(format)(((format)->hdr.argc) - (1)) == 1) { |
| 1783 | return jcImportedIdFrString(txt); |
| 1784 | } |
| 1785 | else { |
| 1786 | JavaCodeList genArgs = listNil(JavaCode)((JavaCodeList) 0); |
| 1787 | int i; |
| 1788 | for (i=1; i<foamDDeclArgc(format)(((format)->hdr.argc) - (1)); i++) { |
| 1789 | genArgs = listCons(JavaCode)(JavaCode_listPointer->Cons)(gj0Type(format->foamDDecl.argv[i]), genArgs); |
| 1790 | } |
| 1791 | return jcGenericId(jcImportedIdFrString(txt), |
| 1792 | listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(genArgs)); |
| 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | |
| 1797 | localstatic JavaCode |
| 1798 | gj0TypeValueToObj(JavaCode val, FoamTag type, AInt fmt) |
| 1799 | { |
| 1800 | switch (type) { |
| 1801 | case FOAM_Char: |
| 1802 | return jcApplyMethodV(val, jcId(strCopy("toChar")), 0); |
| 1803 | case FOAM_SInt: |
| 1804 | return jcApplyMethodV(val, jcId(strCopy("toSInt")), 0); |
| 1805 | case FOAM_Byte: |
| 1806 | return jcApplyMethodV(val, jcId(strCopy("toByte")), 0); |
| 1807 | case FOAM_HInt: |
| 1808 | return jcApplyMethodV(val, jcId(strCopy("toHInt")), 0); |
| 1809 | case FOAM_BInt: |
| 1810 | return jcApplyMethodV(val, jcId(strCopy("toBInt")), 0); |
| 1811 | case FOAM_SFlo: |
| 1812 | return jcApplyMethodV(val, jcId(strCopy("toSFlo")), 0); |
| 1813 | case FOAM_DFlo: |
| 1814 | return jcApplyMethodV(val, jcId(strCopy("toDFlo")), 0); |
| 1815 | case FOAM_NOp: |
| 1816 | assert(false)do { if (!(((int) 0))) _do_assert(("false"),"java/genjava.c", 1816); } while (0); |
| 1817 | return 0; |
| 1818 | case FOAM_Clos: |
| 1819 | return jcApplyMethod(gj0Id(GJ_FoamClos), |
| 1820 | jcId(strCopy("fromValue")), |
| 1821 | listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(val)); |
| 1822 | case FOAM_Gener: |
| 1823 | return jcApplyMethod(gj0Id(GJ_FoamGener), |
| 1824 | jcId(strCopy("fromValue")), |
| 1825 | listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(val)); |
| 1826 | case FOAM_GenIter: |
| 1827 | return jcApplyMethod(gj0Id(GJ_FoamGenIter), |
| 1828 | jcId(strCopy("fromValue")), |
| 1829 | listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(val)); |
| 1830 | case FOAM_Word: |
| 1831 | return jcApplyMethod(jcMemRef(gj0Id(GJ_FoamWord), |
| 1832 | jcId(strCopy("U"))), |
| 1833 | jcId(strCopy("fromValue")), |
| 1834 | listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(val)); |
| 1835 | case FOAM_Bool: |
| 1836 | return jcApplyMethodV(val, jcId(strCopy("toBool")), 0); |
| 1837 | case FOAM_Rec: |
| 1838 | return gj0TypeValueToRec(val, fmt); |
| 1839 | case FOAM_Arr: |
| 1840 | return gj0TypeValueToArr(val, fmt); |
| 1841 | case FOAM_Ptr: |
| 1842 | return jcApplyMethodV(val, jcId(strCopy("toPtr")), 0); |
| 1843 | case FOAM_Env: |
| 1844 | return jcApplyMethodV(val, jcId(strCopy("toEnv")), 0); |
| 1845 | case FOAM_JavaObj: { |
| 1846 | // maybe cast to fmt |
| 1847 | JavaCode jtype = gj0TypeFrFmt(type, fmt); |
| 1848 | return jcApplyMethodV(val, jcGenericMethodNameV(jcId(strCopy("toJavaObj")), 1, jtype), 0); |
| 1849 | } |
| 1850 | default: |
| 1851 | return jcCast(jcSpaceSeqV(2, gj0Id(GJ_FoamValue), |
| 1852 | jcComment(strCopy(foamStr(type)((foamInfoTable [(int)(type)-(int)FOAM_START]).str)))), |
| 1853 | val); |
| 1854 | |
| 1855 | } |
| 1856 | } |
| 1857 | |
| 1858 | localstatic JavaCode |
| 1859 | gj0TypeValueToArr(JavaCode value, AInt fmt) |
| 1860 | { |
| 1861 | JavaCode arrobj = jcApplyMethodV(value, jcId(strCopy("toArray")), 0); |
| 1862 | if (fmt == 0) |
| 1863 | return arrobj; |
| 1864 | else |
| 1865 | return jcCast(jcArrayOf(gj0TypeFrFmt(fmt, 0)), arrobj); |
| 1866 | } |
| 1867 | |
| 1868 | localstatic JavaCode |
| 1869 | gj0TypeValueToRec(JavaCode val, AInt fmt) |
| 1870 | { |
| 1871 | JavaCode record = jcApplyMethodV(jcMemRef(gj0Id(GJ_FoamValue), |
| 1872 | jcId(strCopy("U"))), |
| 1873 | jcId(strCopy("toRecord")), 1, |
| 1874 | val); |
| 1875 | return record; |
| 1876 | } |
| 1877 | |
| 1878 | localstatic JavaCode |
| 1879 | gj0TypeObjToValue(JavaCode val, FoamTag type, AInt fmt) |
| 1880 | { |
| 1881 | switch (type) { |
| 1882 | case FOAM_Word: |
| 1883 | return jcCast(gj0Id(GJ_FoamValue), val); |
| 1884 | case FOAM_SInt: |
| 1885 | return jcApplyMethodV(jcMemRef(gj0Id(GJ_FoamValue), |
| 1886 | jcId(strCopy("U"))), |
| 1887 | jcId(strCopy("fromSInt")), 1, |
| 1888 | val); |
| 1889 | case FOAM_BInt: |
| 1890 | return jcApplyMethodV(jcMemRef(gj0Id(GJ_FoamValue), |
| 1891 | jcId(strCopy("U"))), |
| 1892 | jcId(strCopy("fromBInt")), 1, |
| 1893 | val); |
| 1894 | case FOAM_Bool: |
| 1895 | return jcApplyMethodV(jcMemRef(gj0Id(GJ_FoamValue), |
| 1896 | jcId(strCopy("U"))), |
| 1897 | jcId(strCopy("fromBool")), 1, |
| 1898 | val); |
| 1899 | case FOAM_Arr: |
| 1900 | return jcApplyMethodV(jcMemRef(gj0Id(GJ_FoamValue), |
| 1901 | jcId(strCopy("U"))), |
| 1902 | jcId(strCopy("fromArray")), 1, |
| 1903 | val); |
| 1904 | case FOAM_Nil: |
| 1905 | case FOAM_Ptr: |
| 1906 | return jcApplyMethodV(jcMemRef(gj0Id(GJ_FoamValue), |
| 1907 | jcId(strCopy("U"))), |
| 1908 | jcId(strCopy("fromPtr")), 1, |
| 1909 | val); |
| 1910 | case FOAM_Char: |
| 1911 | return jcApplyMethodV(jcMemRef(gj0Id(GJ_FoamValue), |
| 1912 | jcId(strCopy("U"))), |
| 1913 | jcId(strCopy("fromChar")), 1, |
| 1914 | val); |
| 1915 | case FOAM_HInt: |
| 1916 | return jcApplyMethodV(jcMemRef(gj0Id(GJ_FoamValue), |
| 1917 | jcId(strCopy("U"))), |
| 1918 | jcId(strCopy("fromHInt")), 1, |
| 1919 | val); |
| 1920 | case FOAM_DFlo: |
| 1921 | return jcApplyMethodV(jcMemRef(gj0Id(GJ_FoamValue), |
| 1922 | jcId(strCopy("U"))), |
| 1923 | jcId(strCopy("fromDFlo")), 1, |
| 1924 | val); |
| 1925 | case FOAM_SFlo: |
| 1926 | return jcApplyMethodV(jcMemRef(gj0Id(GJ_FoamValue), |
| 1927 | jcId(strCopy("U"))), |
| 1928 | jcId(strCopy("fromSFlo")), 1, |
| 1929 | val); |
| 1930 | case FOAM_Byte: |
| 1931 | return jcApplyMethodV(jcMemRef(gj0Id(GJ_FoamValue), |
| 1932 | jcId(strCopy("U"))), |
| 1933 | jcId(strCopy("fromByte")), 1, |
| 1934 | val); |
| 1935 | case FOAM_JavaObj: |
| 1936 | return jcApplyMethodV(jcMemRef(gj0Id(GJ_FoamValue), |
| 1937 | jcId(strCopy("U"))), |
| 1938 | jcId(strCopy("fromJavaObj")), 1, |
| 1939 | val); |
| 1940 | case FOAM_Env: |
| 1941 | case FOAM_Clos: |
| 1942 | case FOAM_Rec: |
| 1943 | case FOAM_Gener: |
| 1944 | case FOAM_GenIter: |
| 1945 | case FOAM_NOp: |
| 1946 | return val; |
| 1947 | default: |
| 1948 | assert(false)do { if (!(((int) 0))) _do_assert(("false"),"java/genjava.c", 1948); } while (0); |
| 1949 | return NULL((void*)0); |
| 1950 | } |
| 1951 | |
| 1952 | } |
| 1953 | |
| 1954 | |
| 1955 | localstatic JavaCode |
| 1956 | gj0Set(Foam lhs, Foam rhs) |
| 1957 | { |
| 1958 | switch (foamTag(lhs)((lhs)->hdr.tag)) { |
| 1959 | case FOAM_Fluid: |
| 1960 | return gj0Default(lhs, strCopy("fluidSet")); |
| 1961 | case FOAM_Glo: |
| 1962 | return gj0GloSet(lhs, rhs); |
| 1963 | case FOAM_AElt: |
| 1964 | return gj0AEltSet(lhs, rhs); |
| 1965 | case FOAM_RElt: |
| 1966 | return gj0REltSet(lhs, rhs); |
| 1967 | case FOAM_Lex: |
| 1968 | return gj0LexSet(lhs, rhs); |
| 1969 | case FOAM_EElt: |
| 1970 | return gj0EEltSet(lhs, rhs); |
| 1971 | case FOAM_CEnv: |
| 1972 | return gj0CEnvSet(lhs, rhs); |
| 1973 | case FOAM_CProg: |
| 1974 | return gj0CProgSet(lhs, rhs); |
| 1975 | case FOAM_Values: |
| 1976 | return gj0ValuesSet(lhs, rhs); |
| 1977 | case FOAM_PRef: |
| 1978 | return gj0PRefSet(lhs, rhs); |
| 1979 | case FOAM_EInfo: |
| 1980 | return gj0EInfoSet(lhs, rhs); |
| 1981 | case FOAM_Loc: |
| 1982 | return gj0LocSet(lhs, rhs); |
| 1983 | default: { |
| 1984 | JavaCode lhsJ = gj0Gen(lhs); |
| 1985 | JavaCode rhsJ = gj0Gen(rhs); |
| 1986 | return jcAssign(lhsJ, rhsJ); |
| 1987 | } |
| 1988 | |
| 1989 | } |
| 1990 | } |
| 1991 | |
| 1992 | localstatic JavaCode |
| 1993 | gj0SetGenRhs(Foam foam, Foam decl) |
| 1994 | { |
| 1995 | if (foamTag(foam)((foam)->hdr.tag) == FOAM_Cast) { |
| 1996 | assert(decl->foamDecl.type == foam->foamCast.type)do { if (!(decl->foamDecl.type == foam->foamCast.type)) _do_assert(("decl->foamDecl.type == foam->foamCast.type" ),"java/genjava.c",1996); } while (0); |
| 1997 | return gj0CastFmt(foam, decl->foamDecl.format); |
| 1998 | } |
| 1999 | else |
| 2000 | return gj0Gen(foam); |
| 2001 | } |
| 2002 | |
| 2003 | |
| 2004 | |
| 2005 | localstatic JavaCode |
| 2006 | gj0Par(Foam ref) |
| 2007 | { |
| 2008 | Foam fmt = gjContext->progParams; |
| 2009 | return jcId(gj0Name("p", fmt, ref->foamPar.index)); |
| 2010 | } |
| 2011 | |
| 2012 | localstatic JavaCode |
| 2013 | gj0Loc(Foam ref) |
| 2014 | |
| 2015 | { |
| 2016 | Foam fmt = gjContext->progLocals; |
| 2017 | return jcId(gj0Name("t", fmt, ref->foamLoc.index)); |
| 2018 | } |
| 2019 | |
| 2020 | localstatic JavaCode |
| 2021 | gj0LocSet(Foam ref, Foam rhs) |
| 2022 | { |
| 2023 | Foam fmt = gjContext->progLocals; |
| 2024 | Foam decl = fmt->foamDDecl.argv[ref->foamLoc.index]; |
| 2025 | |
| 2026 | JavaCode lhsJ = jcId(gj0Name("t", fmt, ref->foamLoc.index)); |
| 2027 | JavaCode rhsJ = gj0SetGenRhs(rhs, decl); |
| 2028 | |
| 2029 | return jcAssign(lhsJ, rhsJ); |
| 2030 | } |
| 2031 | |
| 2032 | localstatic JavaCode |
| 2033 | gj0Glo(Foam ref) |
| 2034 | { |
| 2035 | Foam decl; |
| 2036 | JavaCode jc; |
| 2037 | String id; |
| 2038 | |
| 2039 | decl = gjContextGlobal(ref->foamGlo.index)((gjContext->formats->foamDFmt.argv[0])->foamDDecl.argv [ref->foamGlo.index]); |
| 2040 | if (decl->foamGDecl.protocol == FOAM_Proto_Init) { |
| 2041 | return jcId(gj0InitVar(ref->foamGlo.index)); |
| 2042 | } |
| 2043 | id = decl->foamDecl.id; |
| 2044 | jc = jcApplyMethod(gj0Id(GJ_FoamGlobals), |
| 2045 | jcId(strCopy("getGlobal")), |
| 2046 | listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(jcLiteralString(strCopy(id)))); |
| 2047 | |
| 2048 | return gj0TypeValueToObj(jc, decl->foamDecl.type, decl->foamDecl.format); |
| 2049 | } |
| 2050 | |
| 2051 | localstatic JavaCode |
| 2052 | gj0GloSet(Foam lhs, Foam rhs) |
| 2053 | { |
| 2054 | JavaCode jc; |
| 2055 | Foam decl; |
| 2056 | String id; |
| 2057 | |
| 2058 | decl = gjContextGlobal(lhs->foamGlo.index)((gjContext->formats->foamDFmt.argv[0])->foamDDecl.argv [lhs->foamGlo.index]); |
| 2059 | if (foamGDeclIsExportOf(FOAM_Proto_Java, decl) |
| 2060 | || foamGDeclIsExportOf(FOAM_Proto_JavaConstructor, decl) |
| 2061 | || foamGDeclIsExportOf(FOAM_Proto_JavaMethod, decl)) { |
| 2062 | jc = gj0GloSetJava(lhs, rhs); |
| 2063 | } |
| 2064 | else { |
| 2065 | id = decl->foamDecl.id; |
| 2066 | |
| 2067 | jc = gj0TypeObjToValue(gj0SetGenRhs(rhs, decl), |
| 2068 | decl->foamDecl.type, decl->foamDecl.format); |
| 2069 | |
| 2070 | jc = jcApplyMethodV(gj0Id(GJ_FoamGlobals), |
| 2071 | jcId(strCopy("setGlobal")), |
| 2072 | 2, jcLiteralString(strCopy(id)), jc); |
| 2073 | } |
| 2074 | return jc; |
| 2075 | } |
| 2076 | |
| 2077 | |
| 2078 | localstatic JavaCode |
| 2079 | gj0GloSetJava(Foam lhs, Foam rhs) |
| 2080 | { |
| 2081 | JavaCode member = gj0GloJavaId(lhs->foamGlo.index); |
| 2082 | Foam decl = gjContextGlobal(lhs->foamGlo.index)((gjContext->formats->foamDFmt.argv[0])->foamDDecl.argv [lhs->foamGlo.index]); |
| 2083 | |
| 2084 | return jcAssign(jcMemRef(jcThis(), member), |
| 2085 | gj0SetGenRhs(rhs, decl)); |
| 2086 | } |
| 2087 | |
| 2088 | localstatic JavaCode |
| 2089 | gj0GloJavaId(AInt id) |
| 2090 | { |
| 2091 | Foam decl = gjContextGlobal(id)((gjContext->formats->foamDFmt.argv[0])->foamDDecl.argv [id]); |
| 2092 | |
| 2093 | return jcId(gj0Name("M", gjContextGlobals(gjContext->formats->foamDFmt.argv[0]), id)); |
| 2094 | } |
| 2095 | |
| 2096 | localstatic JavaCode |
| 2097 | gj0GloJavaMethodId(AInt id) |
| 2098 | { |
| 2099 | Foam decl = gjContextGlobal(id)((gjContext->formats->foamDFmt.argv[0])->foamDDecl.argv [id]); |
| 2100 | JavaCode methodName = jcImportedStaticIdFrString(decl->foamGDecl.id); |
| 2101 | String name = gj0NameFrString(jcImportedStaticIdName(methodName)); |
| 2102 | JavaCode jc = jcId(strPrintf("_%d_%s", id, name)); |
| 2103 | jcoFree(methodName); |
| 2104 | return jc; |
| 2105 | } |
| 2106 | |
| 2107 | |
| 2108 | |
| 2109 | /* |
| 2110 | * :: Control flow |
| 2111 | * |
| 2112 | * ... |
| 2113 | * int target = 0; |
| 2114 | * while (true) { |
| 2115 | * case 0: |
| 2116 | * [intro] |
| 2117 | * case 1: |
| 2118 | * // (Goto 5) |
| 2119 | * target = 5; |
| 2120 | * continue; |
| 2121 | * case n: |
| 2122 | * ... |
| 2123 | * break; |
| 2124 | * } |
| 2125 | * |
| 2126 | */ |
| 2127 | struct gjSeqBucket { |
| 2128 | AInt label; /* -1 ==> prefix */ |
| 2129 | Bool unreachable; |
| 2130 | JavaCodeList list; |
| 2131 | }; |
| 2132 | |
| 2133 | typedef struct gjSeqBucket *GjSeqBucket; |
| 2134 | DECLARE_LIST(GjSeqBucket)typedef struct GjSeqBucketListCons { GjSeqBucket first; struct GjSeqBucketListCons *rest; } *GjSeqBucketList; struct GjSeqBucket_listOpsStruct { GjSeqBucketList (*Cons) (GjSeqBucket, GjSeqBucketList); GjSeqBucketList (*Singleton) (GjSeqBucket); GjSeqBucketList (*List) (int n, ... ); GjSeqBucketList (*Listv) (va_list argp); GjSeqBucketList ( *ListNull) (GjSeqBucket, ...); Bool (*Equal) (GjSeqBucketList , GjSeqBucketList, Bool (*f) (GjSeqBucket, GjSeqBucket)); GjSeqBucket (*Find) (GjSeqBucketList, GjSeqBucket, Bool(*eq)(GjSeqBucket ,GjSeqBucket) , int *); GjSeqBucket (*Match) (GjSeqBucketList , void *, Bool(*match)(GjSeqBucket, void *), int *); GjSeqBucketList (*MatchAll) (GjSeqBucketList, void *, Bool(*match)(GjSeqBucket , void *)); GjSeqBucketList (*FreeCons) (GjSeqBucketList); void (*Free) (GjSeqBucketList); GjSeqBucketList (*FreeTo) (GjSeqBucketList , GjSeqBucketList); void (*FreeDeeply) (GjSeqBucketList, void (*f)(GjSeqBucket)); GjSeqBucketList (*FreeDeeplyTo) (GjSeqBucketList , GjSeqBucketList, void (*f) (GjSeqBucket) ); GjSeqBucketList (*FreeIfSat) (GjSeqBucketList, void (*f)(GjSeqBucket), Bool ( *s)(GjSeqBucket)); GjSeqBucket (*Elt) (GjSeqBucketList, Length ); GjSeqBucketList (*Drop) (GjSeqBucketList, Length); GjSeqBucketList (*LastCons) (GjSeqBucketList); Length (*_Length) (GjSeqBucketList ); Bool (*IsLength) (GjSeqBucketList, Length); Bool (*IsShorter ) (GjSeqBucketList, Length); Bool (*IsLonger) (GjSeqBucketList , Length); GjSeqBucketList (*Copy) (GjSeqBucketList); GjSeqBucketList (*CopyTo) (GjSeqBucketList, GjSeqBucketList); GjSeqBucketList (*CopyDeeply) (GjSeqBucketList, GjSeqBucket (*)(GjSeqBucket) ); GjSeqBucketList (*CopyDeeplyTo) (GjSeqBucketList, GjSeqBucketList , GjSeqBucket (*)(GjSeqBucket)); GjSeqBucketList (*Map) (GjSeqBucket (*f)(GjSeqBucket), GjSeqBucketList); GjSeqBucketList (*NMap) (GjSeqBucket (*f)(GjSeqBucket), GjSeqBucketList); GjSeqBucketList (*Reverse) (GjSeqBucketList); GjSeqBucketList (*NReverse) (GjSeqBucketList ); GjSeqBucketList (*Concat) (GjSeqBucketList, GjSeqBucketList ); GjSeqBucketList (*NConcat) (GjSeqBucketList, GjSeqBucketList ); Bool (*Memq) (GjSeqBucketList, GjSeqBucket); Bool (*Member ) (GjSeqBucketList, GjSeqBucket, Bool(*eq)(GjSeqBucket,GjSeqBucket ) ); Bool (*ContainsAllq) (GjSeqBucketList, GjSeqBucketList); Bool (*ContainsAnyq) (GjSeqBucketList, GjSeqBucketList); Bool (*ContainsAll) (GjSeqBucketList, GjSeqBucketList, Bool (*eq) (GjSeqBucket, GjSeqBucket)); Bool (*ContainsAny) (GjSeqBucketList , GjSeqBucketList, Bool (*eq)(GjSeqBucket, GjSeqBucket)); int (*Posq) (GjSeqBucketList, GjSeqBucket); int (*Position) (GjSeqBucketList , GjSeqBucket, Bool(*eq)(GjSeqBucket,GjSeqBucket) ); GjSeqBucketList (*NRemove) (GjSeqBucketList, GjSeqBucket, Bool(*eq)(GjSeqBucket ,GjSeqBucket) ); void (*FillVector) (GjSeqBucket *, GjSeqBucketList ); int (*Print) (FILE *, GjSeqBucketList, int (*pr)(FILE *, GjSeqBucket ) ); int (*GPrint) (FILE *, GjSeqBucketList, int (*pr)(FILE * , GjSeqBucket), char *l,char *m,char *r); int (*Format) (OStream , CString, GjSeqBucketList); }; extern struct GjSeqBucket_listOpsStruct const *GjSeqBucket_listPointer; |
| 2135 | CREATE_LIST(GjSeqBucket)struct GjSeqBucket_listOpsStruct const *GjSeqBucket_listPointer = (struct GjSeqBucket_listOpsStruct const *) &ptrlistOps; |
| 2136 | |
| 2137 | struct gjSeqStore { |
| 2138 | GjSeqBucketList buckets; |
| 2139 | }; |
| 2140 | |
| 2141 | typedef struct gjSeqStore *GjSeqStore; |
| 2142 | |
| 2143 | #define GJ_SEQ_Prefix(-2) (-2) |
| 2144 | #define GJ_SEQ_Init(-1) (-1) |
| 2145 | #define GJ_SEQ_Halt(-3) (-3) |
| 2146 | |
| 2147 | localstatic void gj0SeqGen(GjSeqStore seqs, Foam stmt); |
| 2148 | localstatic void gj0SeqGoto(GjSeqStore store, Foam foam); |
| 2149 | localstatic void gj0SeqLabel(GjSeqStore store, Foam foam); |
| 2150 | localstatic void gj0SeqSelect(GjSeqStore store, Foam foam); |
| 2151 | localstatic void gj0SeqSelect2(GjSeqStore store, Foam foam); |
| 2152 | localstatic void gj0SeqGenDefault(GjSeqStore store, Foam foam); |
| 2153 | localstatic void gj0SeqSelectMulti(GjSeqStore store, Foam foam); |
| 2154 | localstatic void gj0SeqIf(GjSeqStore store, Foam foam); |
| 2155 | localstatic void gj0SeqSet(GjSeqStore store, Foam foam); |
| 2156 | localstatic void gj0SeqBCall(GjSeqStore store, Foam foam); |
| 2157 | localstatic void gj0SeqPCall(GjSeqStore store, Foam foam); |
| 2158 | localstatic void gj0SeqValues(GjSeqStore store, Foam foam); |
| 2159 | localstatic void gj0SeqThrow(GjSeqStore store, Foam foam); |
| 2160 | localstatic void gj0SeqGenerStep(GjSeqStore store, Foam foam); |
| 2161 | |
| 2162 | localstatic JavaCode gj0SeqSwitchId(); |
| 2163 | |
| 2164 | localstatic GjSeqStore gj0SeqStoreNew(void); |
| 2165 | localstatic void gj0SeqStoreFree(GjSeqStore store); |
| 2166 | localstatic JavaCode gj0SeqStoreToJava(GjSeqStore store); |
| 2167 | localstatic void gj0SeqStoreAddStmt(GjSeqStore store, JavaCode stmt); |
| 2168 | localstatic void gj0SeqStoreAddLabel(GjSeqStore store, AInt label); |
| 2169 | localstatic void gj0SeqStoreEnsureBody(GjSeqStore store); |
| 2170 | localstatic void gj0SeqStoreAddHalt(GjSeqStore store, JavaCode stmt); |
| 2171 | |
| 2172 | localstatic GjSeqBucket gj0SeqBucketNew(AInt label); |
| 2173 | localstatic void gj0SeqBucketFree(GjSeqBucket store); |
| 2174 | localstatic JavaCodeList gj0SeqBucketToJava(GjSeqBucket bucket); |
| 2175 | localstatic GjSeqBucket gj0SeqBucketGetPrefix(GjSeqBucketList buckets); |
| 2176 | localstatic Bool gj0SeqBucketIsPrefix(GjSeqBucket bucket); |
| 2177 | localstatic Bool gj0SeqBucketIsHalt(GjSeqBucket bucket); |
| 2178 | |
| 2179 | localstatic JavaCode gj0ReturnArray(Foam foam); |
| 2180 | |
| 2181 | localstatic JavaCode |
| 2182 | gj0Seq(Foam seq) |
| 2183 | { |
| 2184 | JavaCode code; |
| 2185 | GjSeqStore seqs = gj0SeqStoreNew(); |
| 2186 | int i; |
| 2187 | |
| 2188 | for (i=0; i != -1; i = foamSeqNextReachable(seq, i)) { |
| 2189 | Foam stmt = seq->foamSeq.argv[i]; |
| 2190 | |
| 2191 | gj0SeqGen(seqs, seq->foamSeq.argv[i]); |
| 2192 | if (foamInfo(foamTag(stmt))(foamInfoTable [(int)(((stmt)->hdr.tag))-(int)FOAM_START]).properties & FOAMP_SeqExit(1<<0) |
| 2193 | && gj0SeqBucketIsPrefix(car(seqs->buckets)((seqs->buckets)->first))) { |
| 2194 | break; |
| 2195 | } |
| 2196 | } |
| 2197 | |
| 2198 | code = gj0SeqStoreToJava(seqs); |
| 2199 | gj0SeqStoreFree(seqs); |
| 2200 | return code; |
| 2201 | } |
| 2202 | |
| 2203 | localstatic JavaCode |
| 2204 | gj0Return(Foam r) |
| 2205 | { |
| 2206 | JavaCode jc; |
| 2207 | |
| 2208 | gjContext->contextFmt = gjContext->prog->foamProg.format; |
| 2209 | if (foamTag(r->foamReturn.value)((r->foamReturn.value)->hdr.tag) == FOAM_Values) { |
| 2210 | jc = gj0ValuesReturn(r->foamReturn.value); |
| 2211 | } |
| 2212 | else if (gjContext->prog->foamProg.retType == FOAM_Arr) { |
| 2213 | jc = gj0ReturnArray(r->foamReturn.value); |
| 2214 | } |
| 2215 | else { |
| 2216 | jc = jcReturn(gj0Gen0(r->foamReturn.value)); |
| 2217 | } |
| 2218 | |
| 2219 | gjContext->contextFmt = -1; |
| 2220 | return jc; |
| 2221 | } |
| 2222 | |
| 2223 | localstatic JavaCode |
| 2224 | gj0ReturnArray(Foam foam) |
| 2225 | { |
| 2226 | JavaCode v; |
| 2227 | FoamTag tag; |
| 2228 | AInt fmt; |
| 2229 | |
| 2230 | tag = gj0FoamExprTypeWFmt(foam, &fmt); |
| 2231 | if (tag == FOAM_Arr || fmt == gjContext->prog->foamProg.format) |
| 2232 | return jcReturn(gj0Gen0(foam)); |
| 2233 | |
| 2234 | v = jcCast(gj0TypeFrFmt(tag, fmt), gj0Gen0(foam)); |
| 2235 | return jcReturn(v); |
| 2236 | } |
| 2237 | |
| 2238 | |
| 2239 | localstatic void |
| 2240 | gj0SeqGen(GjSeqStore seqs, Foam foam) |
| 2241 | { |
| 2242 | switch (foamTag(foam)((foam)->hdr.tag)) { |
| 2243 | case FOAM_Goto: |
| 2244 | gj0SeqGoto(seqs, foam); |
| 2245 | break; |
| 2246 | case FOAM_Select: |
| 2247 | gj0SeqSelect(seqs, foam); |
| 2248 | break; |
| 2249 | case FOAM_If: |
| 2250 | gj0SeqIf(seqs, foam); |
| 2251 | break; |
| 2252 | case FOAM_Label: |
| 2253 | gj0SeqLabel(seqs, foam); |
| 2254 | break; |
| 2255 | case FOAM_BCall: |
| 2256 | gj0SeqBCall(seqs, foam); |
| 2257 | break; |
| 2258 | case FOAM_Throw: |
| 2259 | gj0SeqThrow(seqs, foam); |
| 2260 | break; |
| 2261 | case FOAM_PCall: |
| 2262 | gj0SeqPCall(seqs, foam); |
| 2263 | break; |
| 2264 | case FOAM_Cast: |
| 2265 | gj0SeqGen(seqs, foam->foamCast.expr); |
| 2266 | break; |
| 2267 | case FOAM_GenerStep: |
| 2268 | gj0SeqGenerStep(seqs, foam); |
| 2269 | break; |
| 2270 | case FOAM_Loc: |
| 2271 | case FOAM_Lex: |
| 2272 | case FOAM_Glo: |
| 2273 | break; |
| 2274 | case FOAM_Values: |
| 2275 | gj0SeqValues(seqs, foam); |
| 2276 | break; |
| 2277 | case FOAM_Set: |
| 2278 | case FOAM_Def: |
| 2279 | gj0SeqSet(seqs, foam); |
| 2280 | break; |
| 2281 | default: |
| 2282 | gj0SeqGenDefault(seqs, foam); |
| 2283 | break; |
| 2284 | } |
| 2285 | } |
| 2286 | |
| 2287 | localstatic void |
| 2288 | gj0SeqGoto(GjSeqStore store, Foam foam) |
| 2289 | { |
| 2290 | AInt tgt = foam->foamGoto.label; |
| 2291 | JavaCode s1 = jcAssign(gj0SeqSwitchId(), jcLiteralInteger(tgt)); |
| 2292 | JavaCode s2 = jcContinue(0); |
| 2293 | |
| 2294 | gj0SeqStoreEnsureBody(store); |
| 2295 | |
| 2296 | gj0SeqStoreAddStmt(store, jcStatement(s1)); |
| 2297 | gj0SeqStoreAddStmt(store, jcStatement(s2)); |
| 2298 | } |
| 2299 | |
| 2300 | localstatic void |
| 2301 | gj0SeqLabel(GjSeqStore store, Foam foam) |
| 2302 | { |
| 2303 | gj0SeqStoreEnsureBody(store); |
| 2304 | gj0SeqStoreAddLabel(store, foam->foamLabel.label); |
| 2305 | } |
| 2306 | |
| 2307 | localstatic void |
| 2308 | gj0SeqSelect(GjSeqStore bucketList, Foam foam) |
| 2309 | { |
| 2310 | if (foamSelectArgc(foam)(((foam)->hdr.argc) - 1) == 2) |
| 2311 | gj0SeqSelect2(bucketList, foam); |
| 2312 | else |
| 2313 | gj0SeqSelectMulti(bucketList, foam); |
| 2314 | } |
| 2315 | |
| 2316 | localstatic void |
| 2317 | gj0SeqSelect2(GjSeqStore store, Foam foam) |
| 2318 | { |
| 2319 | JavaCode lhs, rhs, s1, s2; |
| 2320 | |
| 2321 | lhs = gj0SeqSwitchId(); |
| 2322 | rhs = jcConditional(jcBinOp(JCO_OP_Equals, |
| 2323 | gj0Gen(foam->foamSelect.op), |
| 2324 | jcLiteralInteger(0)), |
| 2325 | jcLiteralInteger(foam->foamSelect.argv[0]), |
| 2326 | jcLiteralInteger(foam->foamSelect.argv[1])); |
| 2327 | |
| 2328 | s1 = jcAssign(lhs, rhs); |
| 2329 | s2 = jcContinue(0); |
| 2330 | |
| 2331 | gj0SeqStoreEnsureBody(store); |
| 2332 | gj0SeqStoreAddStmt(store, jcStatement(s1)); |
| 2333 | gj0SeqStoreAddStmt(store, jcStatement(s2)); |
| 2334 | |
| 2335 | } |
| 2336 | |
| 2337 | localstatic void |
| 2338 | gj0SeqIf(GjSeqStore store, Foam foam) |
| 2339 | { |
| 2340 | /* if (...) { label = .; break;} */ |
| 2341 | JavaCode seq, block; |
| 2342 | JavaCode s1, s2; |
| 2343 | |
| 2344 | s1 = jcAssign(gj0SeqSwitchId(), jcLiteralInteger(foam->foamIf.label)); |
| 2345 | s2 = jcContinue(0); |
| 2346 | seq = jcSpaceSeqV(2, jcStatement(s1), jcStatement(s2)); |
| 2347 | block = jcBlockNoNL(seq); |
| 2348 | |
| 2349 | gj0SeqStoreEnsureBody(store); |
| 2350 | gj0SeqStoreAddStmt(store, jcIf(gj0Gen(foam->foamIf.test), block)); |
| 2351 | } |
| 2352 | |
| 2353 | |
| 2354 | localstatic void |
| 2355 | gj0SeqSelectMulti(GjSeqStore store, Foam foam) |
| 2356 | { |
| 2357 | /* switch (xxx) { |
| 2358 | * case 0: target = 1; break; |
| 2359 | * case 1: target = 4; |
| 2360 | * case 2: target = 9; |
| 2361 | * } |
| 2362 | * continue |
| 2363 | */ |
| 2364 | JavaCodeList body; |
| 2365 | JavaCode tst; |
| 2366 | int i; |
| 2367 | |
| 2368 | body = listNil(JavaCode)((JavaCodeList) 0); |
| 2369 | tst = gj0Gen(foam->foamSelect.op); |
| 2370 | for (i=0; i<foamSelectArgc(foam)(((foam)->hdr.argc) - 1); i++) { |
| 2371 | JavaCode l1, s2, s3; |
| 2372 | l1 = jcCaseLabel(jcLiteralInteger(i)); |
| 2373 | s2 = jcAssign(gj0SeqSwitchId(), jcLiteralInteger(foam->foamSelect.argv[i])); |
| 2374 | s3 = jcBreak(0); |
| 2375 | |
| 2376 | body = listCons(JavaCode)(JavaCode_listPointer->Cons)(l1, body); |
| 2377 | body = listCons(JavaCode)(JavaCode_listPointer->Cons)(jcStatement(s2), body); |
| 2378 | body = listCons(JavaCode)(JavaCode_listPointer->Cons)(jcStatement(s3), body); |
| 2379 | } |
| 2380 | body = listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(body); |
| 2381 | |
| 2382 | gj0SeqStoreEnsureBody(store); |
| 2383 | gj0SeqStoreAddStmt(store, jcSwitch(tst, body)); |
| 2384 | gj0SeqStoreAddStmt(store, jcStatement(jcContinue(0))); |
| 2385 | |
| 2386 | } |
| 2387 | |
| 2388 | |
| 2389 | localstatic void |
| 2390 | gj0SeqBCall(GjSeqStore seqs, Foam foam) |
| 2391 | { |
| 2392 | JavaCode jc; |
| 2393 | if (foam->foamBCall.op != FOAM_BVal_Halt) { |
| 2394 | gj0SeqGenDefault(seqs, foam); |
| 2395 | return; |
| 2396 | } |
| 2397 | jc = jcStatement(gj0Gen(foam)); |
| 2398 | |
| 2399 | gj0SeqStoreAddHalt(seqs, jc); |
| 2400 | } |
| 2401 | |
| 2402 | localstatic void |
| 2403 | gj0SeqPCall(GjSeqStore seqs, Foam foam) |
| 2404 | { |
| 2405 | JavaCode jc; |
| 2406 | if (gj0PCallThrowsException(foam)) { |
| 2407 | gj0SeqStoreAddStmt(seqs, gj0PCallCatchException(gj0Gen(foam))); |
| 2408 | return; |
| 2409 | } |
| 2410 | else { |
| 2411 | jc = jcStatement(gj0Gen(foam)); |
| 2412 | gj0SeqStoreAddStmt(seqs, jc); |
| 2413 | } |
| 2414 | } |
| 2415 | |
| 2416 | localstatic void |
| 2417 | gj0SeqThrow(GjSeqStore seqs, Foam foam) |
| 2418 | { |
| 2419 | JavaCode jc = jcStatement(gj0Gen(foam)); |
| 2420 | |
| 2421 | gj0SeqStoreAddHalt(seqs, jc); |
| 2422 | } |
| 2423 | |
| 2424 | |
| 2425 | localstatic void |
| 2426 | gj0SeqSet(GjSeqStore seqs, Foam foam) |
| 2427 | { |
| 2428 | JavaCode code; |
| 2429 | |
| 2430 | code = gj0Gen(foam); |
| 2431 | |
| 2432 | if (!gj0PCallThrowsException(foam->foamSet.rhs)) { |
| 2433 | gj0SeqGenDefault(seqs, foam); |
| 2434 | } |
| 2435 | else { |
| 2436 | code = gj0PCallCatchException(code); |
| 2437 | gj0SeqStoreAddStmt(seqs, code); |
| 2438 | } |
| 2439 | |
| 2440 | } |
| 2441 | |
| 2442 | |
| 2443 | localstatic void |
| 2444 | gj0SeqValues(GjSeqStore store, Foam foam) |
| 2445 | { |
| 2446 | int i; |
| 2447 | for (i=0; i < foamArgc(foam)((foam)->hdr.argc); i++) { |
| 2448 | FoamTag tag = foamTag(foam->foamValues.argv[i])((foam->foamValues.argv[i])->hdr.tag); |
| 2449 | if (tag != FOAM_Loc && tag != FOAM_Lex) |
| 2450 | bug("Odd foam found"); |
| 2451 | } |
| 2452 | } |
| 2453 | |
| 2454 | localstatic void |
| 2455 | gj0SeqGenDefault(GjSeqStore store, Foam foam) |
| 2456 | { |
| 2457 | JavaCode stmt = gj0Gen(foam); |
| 2458 | int i; |
| 2459 | |
| 2460 | if (!gj0IsSeqNode(stmt)) { |
| 2461 | gj0SeqStoreAddStmt(store, jcStatement(stmt)); |
| 2462 | return; |
| 2463 | } |
| 2464 | |
| 2465 | for (i=0; i<jcoArgc(stmt); i++) { |
| 2466 | gj0SeqStoreAddStmt(store, jcoArgv(stmt)[i]); |
| 2467 | } |
| 2468 | } |
| 2469 | |
| 2470 | localstatic JavaCode |
| 2471 | gj0SeqSwitchId() |
| 2472 | { |
| 2473 | return jcId(strCopy("target")); |
| 2474 | } |
| 2475 | |
| 2476 | |
| 2477 | localstatic GjSeqStore |
| 2478 | gj0SeqStoreNew(void) |
| 2479 | { |
| 2480 | GjSeqStore store = (GjSeqStore) stoAlloc(OB_Other0, sizeof(*store)); |
| 2481 | store->buckets = listNil(GjSeqBucket)((GjSeqBucketList) 0); |
| 2482 | return store; |
| 2483 | } |
| 2484 | |
| 2485 | localstatic void |
| 2486 | gj0SeqStoreFree(GjSeqStore store) |
| 2487 | { |
| 2488 | listFreeDeeply(GjSeqBucket)(GjSeqBucket_listPointer->FreeDeeply)(store->buckets, gj0SeqBucketFree); |
| 2489 | stoFree(store); |
| 2490 | } |
| 2491 | |
| 2492 | localstatic void |
| 2493 | gj0SeqStoreEnsureBody(GjSeqStore store) |
| 2494 | { |
| 2495 | if (store->buckets == listNil(GjSeqBucket)((GjSeqBucketList) 0)) { |
| 2496 | store->buckets = listSingleton(GjSeqBucket)(GjSeqBucket_listPointer->Singleton)(gj0SeqBucketNew(GJ_SEQ_Init(-1))); |
| 2497 | return; |
| 2498 | } |
| 2499 | if (gj0SeqBucketIsPrefix(car(store->buckets)((store->buckets)->first))) { |
| 2500 | store->buckets = listCons(GjSeqBucket)(GjSeqBucket_listPointer->Cons)(gj0SeqBucketNew(GJ_SEQ_Init(-1)), |
| 2501 | store->buckets); |
| 2502 | return; |
| 2503 | } |
| 2504 | } |
| 2505 | |
| 2506 | localstatic void |
| 2507 | gj0SeqStoreAddStmt(GjSeqStore store, JavaCode stmt) |
| 2508 | { |
| 2509 | GjSeqBucket bucket; |
| 2510 | |
| 2511 | if (store->buckets == listNil(GjSeqBucket)((GjSeqBucketList) 0)) { |
| 2512 | store->buckets = listSingleton(GjSeqBucket)(GjSeqBucket_listPointer->Singleton)(gj0SeqBucketNew(GJ_SEQ_Prefix(-2))); |
| 2513 | } |
| 2514 | |
| 2515 | bucket = car(store->buckets)((store->buckets)->first); |
| 2516 | bucket->list = listCons(JavaCode)(JavaCode_listPointer->Cons)(stmt, bucket->list); |
| 2517 | } |
| 2518 | |
| 2519 | localstatic void |
| 2520 | gj0SeqStoreAddLabel(GjSeqStore store, AInt label) |
| 2521 | { |
| 2522 | GjSeqBucket bucket; |
| 2523 | |
| 2524 | bucket = gj0SeqBucketNew(label); |
| 2525 | store->buckets = listCons(GjSeqBucket)(GjSeqBucket_listPointer->Cons)(bucket, |
| 2526 | store->buckets); |
| 2527 | } |
| 2528 | |
| 2529 | localstatic void |
| 2530 | gj0SeqStoreAddHalt(GjSeqStore store, JavaCode stmt) |
| 2531 | { |
| 2532 | GjSeqBucket bucket; |
| 2533 | |
| 2534 | bucket = gj0SeqBucketNew(GJ_SEQ_Halt(-3)); |
| 2535 | bucket->list = listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(stmt); |
| 2536 | store->buckets = listCons(GjSeqBucket)(GjSeqBucket_listPointer->Cons)(bucket, |
| 2537 | store->buckets); |
| 2538 | } |
| 2539 | |
| 2540 | |
| 2541 | |
| 2542 | localstatic JavaCode |
| 2543 | gj0SeqStoreToJava(GjSeqStore store) |
| 2544 | { |
| 2545 | JavaCodeList jlst, l1, l2; |
| 2546 | JavaCode stmt, decl; |
| 2547 | GjSeqBucketList buckets, lst; |
| 2548 | GjSeqBucket prefix; |
| 2549 | int labelCount; |
| 2550 | assert(listLength(GjSeqBucket)(store->buckets) > 0)do { if (!((GjSeqBucket_listPointer->_Length)(store->buckets ) > 0)) _do_assert(("listLength(GjSeqBucket)(store->buckets) > 0" ),"java/genjava.c",2550); } while (0); |
| 2551 | |
| 2552 | if (listLength(GjSeqBucket)(GjSeqBucket_listPointer->_Length)(store->buckets) == 1) { |
| 2553 | JavaCodeList code = gj0SeqBucketToJava(car(store->buckets)((store->buckets)->first)); |
| 2554 | store->buckets = listNil(GjSeqBucket)((GjSeqBucketList) 0); |
| 2555 | return jcNLSeq(code); |
| 2556 | } |
| 2557 | |
| 2558 | buckets = listNReverse(GjSeqBucket)(GjSeqBucket_listPointer->NReverse)(store->buckets); |
| 2559 | prefix = gj0SeqBucketGetPrefix(buckets); |
| 2560 | |
| 2561 | jlst = listNil(JavaCode)((JavaCodeList) 0); |
| 2562 | lst = buckets; |
| 2563 | |
| 2564 | labelCount = 0; |
| 2565 | while (lst) { |
| 2566 | JavaCodeList block; |
| 2567 | GjSeqBucket bucket; |
| 2568 | bucket = car(lst)((lst)->first); |
| 2569 | if (gj0SeqBucketIsPrefix(bucket)) { |
| 2570 | lst = cdr(lst)((lst)->rest); |
| 2571 | continue; |
| 2572 | } |
| 2573 | if (!gj0SeqBucketIsHalt(bucket)) { |
| 2574 | labelCount++; |
| 2575 | jlst = listCons(JavaCode)(JavaCode_listPointer->Cons)(jcCaseLabel(jcLiteralInteger(bucket->label)), |
| 2576 | jlst); |
| 2577 | } |
| 2578 | block = gj0SeqBucketToJava(bucket); |
| 2579 | jlst = listNConcat(JavaCode)(JavaCode_listPointer->NConcat)(listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(block), |
| 2580 | jlst); |
| 2581 | lst = cdr(lst)((lst)->rest); |
| 2582 | } |
| 2583 | |
| 2584 | listFree(GjSeqBucket)(GjSeqBucket_listPointer->Free)(buckets); |
| 2585 | store->buckets = listNil(GjSeqBucket)((GjSeqBucketList) 0); |
| 2586 | jlst = listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(jlst); |
| 2587 | |
| 2588 | l1 = prefix == 0 ? listNil(JavaCode)((JavaCodeList) 0) : gj0SeqBucketToJava(prefix); |
| 2589 | if (labelCount == 0) { |
| 2590 | return jcNLSeq(listNConcat(JavaCode)(JavaCode_listPointer->NConcat)(l1, jlst)); |
| 2591 | } |
| 2592 | else { |
| 2593 | decl = jcStatement(jcInitialisation(0, |
| 2594 | jcKeyword(symInternConst("int")symProbe("int", 1)), |
| 2595 | gj0SeqSwitchId(), |
| 2596 | jcLiteralInteger(GJ_SEQ_Init(-1)))); |
| 2597 | |
| 2598 | stmt = jcWhile(jcKeyword(symInternConst("true")symProbe("true", 1)), |
| 2599 | jcBlock(jcSwitch(gj0SeqSwitchId(), jlst))); |
| 2600 | l2 = listList(JavaCode)(JavaCode_listPointer->List)(2, decl, stmt); |
| 2601 | return jcNLSeq(listNConcat(JavaCode)(JavaCode_listPointer->NConcat)(l1, l2)); |
| 2602 | } |
| 2603 | } |
| 2604 | |
| 2605 | localstatic GjSeqBucket |
| 2606 | gj0SeqBucketNew(AInt label) |
| 2607 | { |
| 2608 | GjSeqBucket bucket = (GjSeqBucket) stoAlloc(OB_Other0, sizeof(*bucket)); |
| 2609 | |
| 2610 | bucket->label = label; |
| 2611 | bucket->list = listNil(JavaCode)((JavaCodeList) 0); |
| 2612 | |
| 2613 | return bucket; |
| 2614 | } |
| 2615 | |
| 2616 | localstatic void |
| 2617 | gj0SeqBucketFree(GjSeqBucket bucket) |
| 2618 | { |
| 2619 | listFreeDeeply(JavaCode)(JavaCode_listPointer->FreeDeeply)(bucket->list, jcoFree); |
| 2620 | stoFree(bucket); |
| 2621 | } |
| 2622 | |
| 2623 | localstatic JavaCodeList |
| 2624 | gj0SeqBucketToJava(GjSeqBucket bucket) |
| 2625 | { |
| 2626 | JavaCodeList l = listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(bucket->list); |
| 2627 | bucket->list = listNil(JavaCode)((JavaCodeList) 0); |
| 2628 | |
| 2629 | if (bucket->label == GJ_SEQ_Halt(-3)) { |
| 2630 | if (listLength(JavaCode)(JavaCode_listPointer->_Length)(l) > 1) |
| 2631 | car(l)((l)->first) = jcIf(jcTrue(), car(l)((l)->first)); |
| 2632 | } |
| 2633 | |
| 2634 | return l; |
| 2635 | } |
| 2636 | |
| 2637 | localstatic GjSeqBucket |
| 2638 | gj0SeqBucketGetPrefix(GjSeqBucketList buckets) |
| 2639 | { |
| 2640 | |
| 2641 | while (buckets) { |
| 2642 | GjSeqBucket bucket; |
| 2643 | bucket = car(buckets)((buckets)->first); |
| 2644 | if (gj0SeqBucketIsPrefix(bucket)) |
| 2645 | return bucket; |
| 2646 | buckets = cdr(buckets)((buckets)->rest); |
| 2647 | } |
| 2648 | return 0; |
| 2649 | } |
| 2650 | |
| 2651 | localstatic Bool |
| 2652 | gj0SeqBucketIsPrefix(GjSeqBucket bucket) |
| 2653 | { |
| 2654 | return bucket->label == GJ_SEQ_Prefix(-2); |
| 2655 | } |
| 2656 | |
| 2657 | localstatic Bool |
| 2658 | gj0SeqBucketIsHalt(GjSeqBucket bucket) |
| 2659 | { |
| 2660 | return bucket->label == GJ_SEQ_Halt(-3); |
| 2661 | } |
| 2662 | |
| 2663 | localstatic JavaCode |
| 2664 | gj0Throw(Foam foam) |
| 2665 | { |
| 2666 | JavaCode tag = gj0Gen(foam->foamThrow.tag); |
| 2667 | JavaCode val = gj0Gen(foam->foamThrow.val); |
| 2668 | |
| 2669 | return jcThrow(jcConstructV(gj0Id(GJ_FoamUserException), 2, tag, val)); |
| 2670 | } |
| 2671 | |
| 2672 | |
| 2673 | /***************************************************************************** |
| 2674 | * |
| 2675 | * :: Coroutines and Generators |
| 2676 | * |
| 2677 | ****************************************************************************/ |
| 2678 | |
| 2679 | localstatic JavaCode |
| 2680 | gj0Gener(Foam foam) |
| 2681 | { |
| 2682 | Foam prog = foam->foamGener.prog; |
| 2683 | AInt fmt = foam->foamGener.fmt; |
| 2684 | gj0FmtUse(fmt); |
| 2685 | /* |
| 2686 | Foam env, prog, init; |
| 2687 | JavaCode stmt; |
| 2688 | AInt fmt; |
| 2689 | env = foam->foamGener.env; |
| 2690 | prog = foam->foamGener.prog; |
| 2691 | init = foam->foamGener.state; |
| 2692 | return ccoFCall(ccoIdOf("fiGenerNew"), |
| 2693 | ccoMany3(gccVal(env), |
| 2694 | ccoSizeof(ccoType(ccoStructRef(gc0VarId(gcFmtName, fmt)), NULL)), |
| 2695 | gj0Gen(prog))); |
| 2696 | */ |
| 2697 | Foam ddecl = gjContext->formats->foamDFmt.argv[fmt]; |
| 2698 | return jcConstructV(gj0TypeFrFmt(FOAM_Gener, 0), |
| 2699 | 3, |
| 2700 | gj0Gen(foam->foamGener.env), |
| 2701 | jcId(gj0ProgFnName(prog->foamConst.index)), |
| 2702 | gj0FmtId(fmt)); |
| 2703 | } |
| 2704 | |
| 2705 | |
| 2706 | localstatic JavaCode |
| 2707 | gj0GenerValue(Foam foam) |
| 2708 | { |
| 2709 | return jcApplyMethodV(gj0Gen(foam->foamGenerValue.gener), jcId(strCopy("value")), 0); |
| 2710 | } |
| 2711 | |
| 2712 | |
| 2713 | localstatic void |
| 2714 | gj0SeqGenerStep(GjSeqStore store, Foam foam) |
| 2715 | { |
| 2716 | JavaCode seq, block; |
| 2717 | JavaCode s1, s2, b0; |
| 2718 | // (GenerStep label, gener) |
| 2719 | // g = gener |
| 2720 | // g.step() |
| 2721 | // if (g.done()) { target = label ; continue } |
| 2722 | s1 = jcStatement(jcApplyMethod(gj0Gen(foam->foamGenerStep.gener), jcId(strCopy("step")), listNil(JavaCode)((JavaCodeList) 0))); |
| 2723 | b0 = jcSpaceSeqV(2, |
| 2724 | jcStatement(jcAssign(gj0SeqSwitchId(), jcLiteralInteger(foam->foamGenerStep.label))), |
| 2725 | jcStatement(jcContinue(0))); |
| 2726 | s2 = jcIf(jcApplyMethod(gj0Gen(foam->foamGenerStep.gener), jcId(strCopy("isDone")), listNil(JavaCode)((JavaCodeList) 0)), |
| 2727 | jcBlockNoNL(b0)); |
| 2728 | |
| 2729 | gj0SeqStoreEnsureBody(store); |
| 2730 | gj0SeqStoreAddStmt(store, s1); |
| 2731 | gj0SeqStoreAddStmt(store, s2); |
| 2732 | } |
| 2733 | |
| 2734 | localstatic JavaCode |
| 2735 | gj0GenIter(Foam foam) |
| 2736 | { |
| 2737 | //return jcCommented(strPrintf("%pFoam", foam), jcNull()); |
| 2738 | return jcApplyMethod(gj0Gen(foam->foamGenIter.gener), jcId(strCopy("start")), listNil(JavaCode)((JavaCodeList) 0)); |
| 2739 | } |
| 2740 | |
| 2741 | |
| 2742 | /* |
| 2743 | * :: Custom java classes |
| 2744 | */ |
| 2745 | |
| 2746 | static struct jclss gjClss[] = { |
| 2747 | { -1, jcNodePrint, jcNodeSExpr, "stmtseq", "\""} |
| 2748 | }; |
| 2749 | |
| 2750 | localstatic JavaCode |
| 2751 | gj0SeqNode(JavaCodeList l) |
| 2752 | { |
| 2753 | return jcoNewFrList(&gjClss[0], l); |
| 2754 | } |
| 2755 | |
| 2756 | localstatic Bool |
| 2757 | gj0IsSeqNode(JavaCode jc) |
| 2758 | { |
| 2759 | return jcoClass(jc)(((jc)->hdr).clss) == &gjClss[0]; |
| 2760 | } |
| 2761 | |
| 2762 | /* |
| 2763 | * Destructively replaces any statements containing a 'halt' |
| 2764 | * with the halt itself (safe as all foam statements evaluate |
| 2765 | * all their arguments. |
| 2766 | */ |
| 2767 | localstatic Bool gj0SeqIsBCallHalt(Foam f); |
| 2768 | |
| 2769 | localstatic void |
| 2770 | gj0SeqHaltFlush(Foam foam) |
| 2771 | { |
| 2772 | int i; |
| 2773 | |
| 2774 | assert(foamTag(foam) == FOAM_Seq)do { if (!(((foam)->hdr.tag) == FOAM_Seq)) _do_assert(("foamTag(foam) == FOAM_Seq" ),"java/genjava.c",2774); } while (0); |
| 2775 | |
| 2776 | for (i=0; i<foamArgc(foam)((foam)->hdr.argc); i++) { |
| 2777 | Foam stmt = foam->foamSeq.argv[i]; |
| 2778 | Foam f = foamFindFirst(gj0SeqIsBCallHalt, stmt); |
| 2779 | if (f == 0) |
| 2780 | continue; |
| 2781 | foam->foamSeq.argv[i] = foamCopy(f); |
| 2782 | foamFree(stmt); |
| 2783 | } |
| 2784 | |
| 2785 | } |
| 2786 | |
| 2787 | localstatic Bool |
| 2788 | gj0SeqIsBCallHalt(Foam f) |
| 2789 | { |
| 2790 | if (foamTag(f)((f)->hdr.tag) != FOAM_BCall) |
| 2791 | return false((int) 0); |
| 2792 | if (f->foamBCall.op != FOAM_BVal_Halt) |
| 2793 | return false((int) 0); |
| 2794 | return true1; |
| 2795 | } |
| 2796 | |
| 2797 | /* |
| 2798 | * :: Method calls |
| 2799 | */ |
| 2800 | |
| 2801 | |
| 2802 | localstatic JavaCode |
| 2803 | gj0CCall(Foam call) |
| 2804 | { |
| 2805 | FoamSig sig = gj0FoamSigFrCCall(call); |
| 2806 | JavaCodeList args = gj0GenList(call->foamCCall.argv, foamCCallArgc(call)(((call)->hdr.argc) - (2) )); |
| 2807 | JavaCode res = gj0CCallSig(sig, gj0Gen(call->foamCCall.op), args); |
| 2808 | gjContext->progSigList = gj0CCallStubAdd(gjContext->progSigList, sig); |
| 2809 | return res; |
| 2810 | } |
| 2811 | |
| 2812 | localstatic JavaCode |
| 2813 | gj0CCallSig(FoamSig sig, JavaCode op, JavaCodeList args) |
| 2814 | { |
| 2815 | JavaCode code; |
| 2816 | |
| 2817 | String id = gj0CCallStubName(sig); |
| 2818 | code = jcApply(jcId(id), listCons(JavaCode)(JavaCode_listPointer->Cons)(op, args)); |
| 2819 | |
| 2820 | return code; |
| 2821 | } |
| 2822 | |
| 2823 | String |
| 2824 | gj0TypeAbbrev(FoamTag tag) |
| 2825 | { |
| 2826 | switch (tag) { |
| 2827 | case FOAM_Rec: |
| 2828 | return "R"; |
| 2829 | case FOAM_Arr: |
| 2830 | return "A"; |
| 2831 | case FOAM_Word: |
| 2832 | return "W"; |
| 2833 | case FOAM_HInt: |
| 2834 | return "H"; |
| 2835 | case FOAM_SInt: |
| 2836 | return "I"; |
| 2837 | case FOAM_NOp: |
| 2838 | return "X"; |
| 2839 | case FOAM_Clos: |
| 2840 | return "C"; |
| 2841 | case FOAM_Gener: |
| 2842 | return "G"; |
| 2843 | case FOAM_GenIter: |
| 2844 | return "I"; |
| 2845 | case FOAM_Bool: |
| 2846 | return "B"; |
| 2847 | case FOAM_Byte: |
| 2848 | return "Y"; |
| 2849 | case FOAM_BInt: |
| 2850 | return "N"; |
| 2851 | case FOAM_Char: |
| 2852 | return "L"; |
| 2853 | case FOAM_Nil: |
| 2854 | case FOAM_Ptr: |
| 2855 | return "P"; |
| 2856 | case FOAM_SFlo: |
| 2857 | return "S"; |
| 2858 | case FOAM_DFlo: |
| 2859 | return "D"; |
| 2860 | case FOAM_JavaObj: |
| 2861 | return "J"; |
| 2862 | default: |
| 2863 | return "?"; |
| 2864 | } |
| 2865 | } |
| 2866 | |
| 2867 | /* |
| 2868 | * :: OCall |
| 2869 | */ |
| 2870 | |
| 2871 | localstatic JavaCode |
| 2872 | gj0OCall(Foam foam) |
| 2873 | { |
| 2874 | JavaCodeList args; |
| 2875 | JavaCode env; |
| 2876 | |
| 2877 | env = gj0Gen(foam->foamOCall.env); |
| 2878 | args = gj0GenList(foam->foamOCall.argv, foamArgc(foam)((foam)->hdr.argc)-3); |
| 2879 | |
| 2880 | return jcApply(jcId(gj0ProgMethodName(foam->foamOCall.op)), |
| 2881 | listCons(JavaCode)(JavaCode_listPointer->Cons)(env, args)); |
| 2882 | |
| 2883 | } |
| 2884 | |
| 2885 | |
| 2886 | /* |
| 2887 | * :: Multiple values |
| 2888 | */ |
| 2889 | |
| 2890 | localstatic Foam gj0ValuesDDeclCopy(Foam foam); |
| 2891 | localstatic JavaCode gj0ValuesTmpVar(); |
| 2892 | |
| 2893 | localstatic JavaCode |
| 2894 | gj0ValuesSet(Foam lhs, Foam rhs) |
| 2895 | { |
| 2896 | JavaCodeList l; |
| 2897 | JavaCode assign, rhsJ, var, rhsType, init; |
| 2898 | Foam rhsDDecl; |
| 2899 | int i; |
| 2900 | |
| 2901 | if (foamTag(rhs)((rhs)->hdr.tag) == FOAM_MFmt |
| 2902 | && foamTag(rhs->foamMFmt.value)((rhs->foamMFmt.value)->hdr.tag) == FOAM_Values) |
| 2903 | rhs = rhs->foamMFmt.value; |
| 2904 | |
| 2905 | if (foamTag(rhs)((rhs)->hdr.tag) == FOAM_Values) { |
| 2906 | l = listNil(JavaCode)((JavaCodeList) 0); |
| 2907 | for (i=0; i<foamArgc(lhs)((lhs)->hdr.argc); i++) { |
| 2908 | assign = gj0Set(lhs->foamValues.argv[i], |
| 2909 | rhs->foamValues.argv[i]); |
| 2910 | l = listCons(JavaCode)(JavaCode_listPointer->Cons)(assign, l); |
| 2911 | } |
| 2912 | return jcNLSeq(listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(l)); |
| 2913 | } |
| 2914 | |
| 2915 | l = listNil(JavaCode)((JavaCodeList) 0); |
| 2916 | rhsJ = gj0Gen(rhs); |
| 2917 | rhsDDecl = gj0ValuesDDeclCopy(rhs); |
| 2918 | var = gj0ValuesTmpVar(); |
| 2919 | rhsType = gj0TypeFrFmt(FOAM_Values, 0); |
| 2920 | |
| 2921 | init = jcInitialisation(0, rhsType, jcoCopy(var), rhsJ); |
| 2922 | l = listCons(JavaCode)(JavaCode_listPointer->Cons)(jcStatement(init), l); |
| 2923 | for (i=0; i<foamArgc(lhs)((lhs)->hdr.argc); i++) { |
| 2924 | assign = jcAssign(gj0Gen(lhs->foamValues.argv[i]), |
| 2925 | gj0RecElt(jcoCopy(var), rhsDDecl, i)); |
| 2926 | l = listCons(JavaCode)(JavaCode_listPointer->Cons)(jcStatement(assign), l); |
| 2927 | } |
| 2928 | |
| 2929 | jcoFree(var); |
| 2930 | foamFree(rhsDDecl); |
| 2931 | |
| 2932 | return gj0SeqNode(listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(l)); |
| 2933 | } |
| 2934 | |
| 2935 | localstatic JavaCode |
| 2936 | gj0ValuesTmpVar() |
| 2937 | { |
| 2938 | int idx = gjContext->multVarIdx++; |
| 2939 | |
| 2940 | return jcId(strPrintf("var%d", idx)); |
| 2941 | } |
| 2942 | |
| 2943 | localstatic Foam |
| 2944 | gj0ValuesDDeclCopy(Foam foam) |
| 2945 | { |
| 2946 | if (foamTag(foam)((foam)->hdr.tag) == FOAM_MFmt) { |
| 2947 | Foam ddecls = gjContext->formats; |
| 2948 | int idx = foam->foamMFmt.format; |
| 2949 | return foamCopy(ddecls->foamDFmt.argv[idx]); |
| 2950 | } |
| 2951 | |
| 2952 | if (foamTag(foam)((foam)->hdr.tag) == FOAM_BCall) { |
| 2953 | switch (foam->foamBCall.op) { |
| 2954 | case FOAM_BVal_SIntDivide: |
| 2955 | return foamNew(FOAM_DDecl, 2, |
| 2956 | foamNewDecl(FOAM_SInt, "q", 0)foamNew(FOAM_Decl,4,(AInt)(FOAM_SInt),"q", (AInt) (0x7FFF), 0 ), |
| 2957 | foamNewDecl(FOAM_SInt, "r", 0)foamNew(FOAM_Decl,4,(AInt)(FOAM_SInt),"r", (AInt) (0x7FFF), 0 )); |
| 2958 | case FOAM_BVal_BIntDivide: |
| 2959 | return foamNew(FOAM_DDecl, 2, |
| 2960 | foamNewDecl(FOAM_BInt, "q", 0)foamNew(FOAM_Decl,4,(AInt)(FOAM_BInt),"q", (AInt) (0x7FFF), 0 ), |
| 2961 | foamNewDecl(FOAM_BInt, "r", 0)foamNew(FOAM_Decl,4,(AInt)(FOAM_BInt),"r", (AInt) (0x7FFF), 0 )); |
| 2962 | default: |
| 2963 | assert(false)do { if (!(((int) 0))) _do_assert(("false"),"java/genjava.c", 2963); } while (0); |
| 2964 | } |
| 2965 | |
| 2966 | } |
| 2967 | assert(false)do { if (!(((int) 0))) _do_assert(("false"),"java/genjava.c", 2967); } while (0); |
| 2968 | return NULL((void*)0); |
| 2969 | } |
| 2970 | |
| 2971 | localstatic JavaCode |
| 2972 | gj0ValuesReturn(Foam foam) |
| 2973 | { |
| 2974 | Foam ddecl, ddecls; |
| 2975 | JavaCodeList l; |
| 2976 | JavaCode assign, lhs, set, ret; |
| 2977 | int i, idx; |
| 2978 | |
| 2979 | if (foamArgc(foam)((foam)->hdr.argc) == 0) |
| 2980 | return jcReturnVoid(); |
| 2981 | |
| 2982 | lhs = gj0ValuesTmpVar(); |
| 2983 | l = listNil(JavaCode)((JavaCodeList) 0); |
| 2984 | ddecls = gjContext->formats; |
| 2985 | idx = gjContext->prog->foamProg.format; |
| 2986 | ddecl = ddecls->foamDFmt.argv[idx]; |
| 2987 | |
| 2988 | gj0FmtUse(idx); |
| 2989 | assign = jcInitialisation(0, |
| 2990 | gj0TypeFrFmt(FOAM_Values, 0), |
| 2991 | jcoCopy(lhs), |
| 2992 | jcConstructV(gj0TypeFrFmt(FOAM_Values, 0), 1, |
| 2993 | gj0FmtId(idx))); |
| 2994 | l = listCons(JavaCode)(JavaCode_listPointer->Cons)(jcStatement(assign), l); |
| 2995 | for (i=0; i<foamArgc(foam)((foam)->hdr.argc); i++) { |
| 2996 | set = gj0RecSet(jcoCopy(lhs), |
| 2997 | gj0Gen(foam->foamValues.argv[i]), |
| 2998 | ddecl, i); |
| 2999 | l = listCons(JavaCode)(JavaCode_listPointer->Cons)(jcStatement(set), l); |
| 3000 | } |
| 3001 | |
| 3002 | ret = jcReturn(jcoCopy(lhs)); |
| 3003 | l = listCons(JavaCode)(JavaCode_listPointer->Cons)(jcStatement(ret), l); |
| 3004 | jcoFree(lhs); |
| 3005 | |
| 3006 | return gj0SeqNode(listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(l)); |
| 3007 | } |
| 3008 | |
| 3009 | localstatic JavaCode |
| 3010 | gj0MFmt(Foam f) |
| 3011 | { |
| 3012 | JavaCode jc; |
| 3013 | gjContext->mfmt = f->foamMFmt.format; |
| 3014 | jc = gj0Gen(f->foamMFmt.value); |
| 3015 | gjContext->mfmt = 0; |
| 3016 | |
| 3017 | return jc; |
| 3018 | } |
| 3019 | |
| 3020 | |
| 3021 | /* |
| 3022 | * :: Integer Literals |
| 3023 | */ |
| 3024 | |
| 3025 | |
| 3026 | localstatic JavaCode |
| 3027 | gj0SInt(Foam foam) |
| 3028 | { |
| 3029 | return jcLiteralInteger(foam->foamSInt.SIntData); |
| 3030 | } |
| 3031 | |
| 3032 | localstatic JavaCode |
| 3033 | gj0BInt(Foam foam) |
| 3034 | { |
| 3035 | BInt val = foam->foamBInt.BIntData; |
| 3036 | if (bintIsZero(val)) |
| 3037 | return jcMemRef(gj0Id(GJ_BigInteger), |
| 3038 | jcId(strCopy("ZERO"))); |
| 3039 | if (bintLength(val) < 30 && bintIsSmall(val)) { |
| 3040 | long smallval = bintSmall(val); |
| 3041 | if (smallval == 1) { |
| 3042 | return jcMemRef(gj0Id(GJ_BigInteger), |
| 3043 | jcId(strCopy("ONE"))); |
| 3044 | } |
| 3045 | else { |
| 3046 | return jcApplyMethodV(gj0Id(GJ_BigInteger), |
| 3047 | jcId(strCopy("valueOf")), |
| 3048 | 1, jcLiteralInteger(smallval)); |
| 3049 | } |
| 3050 | } |
| 3051 | else { |
| 3052 | return jcConstructV(gj0Id(GJ_BigInteger), |
| 3053 | 1, jcLiteralString(bintToString(val))); |
| 3054 | } |
| 3055 | } |
| 3056 | |
| 3057 | localstatic JavaCode |
| 3058 | gj0Byte(Foam foam) |
| 3059 | { |
| 3060 | return jcCast(gj0TypeFrFmt(FOAM_Byte, 0), |
| 3061 | jcLiteralInteger(foam->foamByte.ByteData)); |
| 3062 | } |
| 3063 | |
| 3064 | localstatic JavaCode |
| 3065 | gj0HInt(Foam foam) |
| 3066 | { |
| 3067 | return jcCast(gj0TypeFrFmt(FOAM_HInt, 0), |
| 3068 | jcLiteralInteger(foam->foamHInt.HIntData)); |
| 3069 | } |
| 3070 | |
| 3071 | |
| 3072 | /* |
| 3073 | * :: Float Literals |
| 3074 | */ |
| 3075 | |
| 3076 | localstatic JavaCode |
| 3077 | gj0SFlo(Foam foam) |
| 3078 | { |
| 3079 | char buf[MAX_FLOAT_SIZE512]; |
| 3080 | DFloatSprint(buf, foam->foamSFlo.SFloData); |
| 3081 | return jcCast(gj0TypeFrFmt(FOAM_SFlo, 0), |
| 3082 | jcLiteralFloatFrString(strCopy(buf))); |
| 3083 | } |
| 3084 | localstatic JavaCode |
| 3085 | gj0DFlo(Foam foam) |
| 3086 | { |
| 3087 | char buf[MAX_FLOAT_SIZE512]; |
| 3088 | DFloatSprint(buf, foam->foamSFlo.SFloData); |
| 3089 | return jcLiteralFloatFrString(strCopy(buf)); |
| 3090 | } |
| 3091 | |
| 3092 | /* |
| 3093 | * :: Boolean Literals |
| 3094 | */ |
| 3095 | |
| 3096 | |
| 3097 | localstatic JavaCode |
| 3098 | gj0Bool(Foam foam) |
| 3099 | { |
| 3100 | return foam->foamBool.BoolData |
| 3101 | ? jcKeyword(symInternConst("true")symProbe("true", 1)) |
| 3102 | : jcKeyword(symInternConst("false")symProbe("false", 1)); |
| 3103 | } |
| 3104 | |
| 3105 | /* |
| 3106 | * :: Char Literals |
| 3107 | */ |
| 3108 | |
| 3109 | |
| 3110 | localstatic JavaCode |
| 3111 | gj0Char(Foam foam) |
| 3112 | { |
| 3113 | char buf[2] = ""; |
| 3114 | buf[0] = foam->foamChar.CharData; |
| 3115 | return jcLiteralChar(buf); |
| 3116 | } |
| 3117 | |
| 3118 | localstatic JavaCode |
| 3119 | gj0Nil(Foam foam) |
| 3120 | { |
| 3121 | return jcNull(); |
| 3122 | } |
| 3123 | |
| 3124 | |
| 3125 | /* |
| 3126 | * :: Default code |
| 3127 | */ |
| 3128 | |
| 3129 | localstatic JavaCode |
| 3130 | gj0Default(Foam foam, String prefix) |
| 3131 | { |
| 3132 | bug("%s", aStrPrintf("Java not implemented: %s %pFoam\n", prefix, foam)); |
| 3133 | return NULL((void*)0); /* Not reached */ |
| 3134 | } |
| 3135 | |
| 3136 | localstatic FoamTag |
| 3137 | gj0FoamExprType(Foam foam) |
| 3138 | { |
| 3139 | FoamTag tag; |
| 3140 | AInt extra; |
| 3141 | |
| 3142 | tag = foamExprType0(foam, gjContext->prog, |
| 3143 | gjContext->formats, |
| 3144 | 0, 0, &extra); |
| 3145 | |
| 3146 | return tag; |
| 3147 | } |
| 3148 | |
| 3149 | localstatic FoamTag |
| 3150 | gj0FoamExprTypeWFmt(Foam foam, AInt *fmt) |
| 3151 | { |
| 3152 | FoamTag tag; |
| 3153 | |
| 3154 | tag = foamExprType0(foam, gjContext->prog, |
| 3155 | gjContext->formats, |
| 3156 | 0, 0, fmt); |
| 3157 | |
| 3158 | return tag; |
| 3159 | } |
| 3160 | |
| 3161 | |
| 3162 | /* |
| 3163 | * :: Arrays |
| 3164 | */ |
| 3165 | |
| 3166 | localstatic JavaCode |
| 3167 | gj0ANew(Foam foam) { |
| 3168 | AInt type = foam->foamANew.eltType; |
| 3169 | JavaCode sz = gj0Gen(foam->foamANew.size); |
| 3170 | |
| 3171 | return jcArrayNew(gj0TypeFrFmt(type, 0), sz); |
| 3172 | } |
| 3173 | |
| 3174 | localstatic JavaCode |
| 3175 | gj0Arr(Foam foam) |
| 3176 | { |
| 3177 | switch (foam->foamArr.baseType) { |
| 3178 | case FOAM_Char: |
| 3179 | return gj0ArrChar(foam); |
| 3180 | default: |
| 3181 | return gj0Default(foam, |
| 3182 | strPrintf("Arr(%s)", |
| 3183 | foamStr(foam->foamArr.baseType)((foamInfoTable [(int)(foam->foamArr.baseType)-(int)FOAM_START ]).str))); |
| 3184 | } |
| 3185 | } |
| 3186 | |
| 3187 | localstatic JavaCode |
| 3188 | gj0ArrChar(Foam foam) |
| 3189 | { |
| 3190 | int i, arrSize; |
| 3191 | String str = foamArrToString(foam); |
| 3192 | |
| 3193 | return jcApplyMethodV(jcLiteralStringWithTerminalChar(str), jcId(strCopy("toCharArray")), 0); |
| 3194 | } |
| 3195 | |
| 3196 | |
| 3197 | localstatic JavaCode |
| 3198 | gj0AElt(Foam foam) |
| 3199 | { |
| 3200 | JavaCode exprCode; |
| 3201 | FoamTag tag; |
| 3202 | AInt type; |
| 3203 | Bool needsCast; |
| 3204 | |
| 3205 | tag = gj0FoamExprTypeWFmt(foam->foamAElt.expr, &type); |
| 3206 | needsCast = (type == 0 || type == emptyFormatSlot4); |
| 3207 | |
| 3208 | exprCode = gj0Gen(foam->foamAElt.expr); |
| 3209 | if (needsCast) |
| 3210 | exprCode = jcCast(gj0TypeFrFmt(FOAM_Arr, |
| 3211 | foam->foamAElt.baseType), |
| 3212 | exprCode); |
| 3213 | |
| 3214 | return jcArrayRef(exprCode, |
| 3215 | gj0Gen(foam->foamAElt.index)); |
| 3216 | } |
| 3217 | |
| 3218 | localstatic JavaCode |
| 3219 | gj0AEltSet(Foam lhs, Foam rhs) |
| 3220 | { |
| 3221 | return jcAssign(gj0Gen(lhs), gj0Gen(rhs)); |
| 3222 | } |
| 3223 | |
| 3224 | /* |
| 3225 | * :: Record references |
| 3226 | */ |
| 3227 | |
| 3228 | localstatic JavaCode |
| 3229 | gj0RNew(Foam foam) |
| 3230 | { |
| 3231 | gj0FmtUse(foam->foamRNew.format); |
| 3232 | return jcConstructV(gj0TypeFrFmt(FOAM_Rec, foam->foamRNew.format), 1, |
| 3233 | gj0FmtId(foam->foamRNew.format)); |
| 3234 | } |
| 3235 | |
| 3236 | localstatic JavaCode |
| 3237 | gj0Free(Foam foam) |
| 3238 | { |
| 3239 | return gj0SeqNode(listNil(JavaCode)((JavaCodeList) 0)); |
| 3240 | } |
| 3241 | |
| 3242 | |
| 3243 | localstatic JavaCode |
| 3244 | gj0RElt(Foam foam) |
| 3245 | { |
| 3246 | AInt fmt = foam->foamRElt.format; |
| 3247 | AInt idx = foam->foamRElt.field; |
| 3248 | Foam ref = foam->foamRElt.expr; |
| 3249 | Foam innerRef = ref; |
| 3250 | Foam ddecl; |
| 3251 | foamDereferenceCast(innerRef)while (((innerRef)->hdr.tag) == FOAM_Cast) (innerRef) = (innerRef )->foamCast.expr;; |
| 3252 | if (foamTag(innerRef)((innerRef)->hdr.tag) == FOAM_Nil) { |
| 3253 | JavaCode exception, value; |
| 3254 | Foam decl; |
| 3255 | ddecl = gjContext->formats->foamDFmt.argv[fmt]; |
| 3256 | decl = ddecl->foamDDecl.argv[idx]; |
| 3257 | /* call throwException(new NullPointerException()) |
| 3258 | and cast to ... */ |
| 3259 | exception = jcConstructV(gj0Id(GJ_NullPointerException), 1, |
| 3260 | jcLiteralString(strCopy("RElt"))); |
| 3261 | value = jcCast(gj0TypeFrFmt(decl->foamDecl.type, decl->foamDecl.format), |
| 3262 | jcApplyV(jcMemRef(gj0Id(GJ_Foam), jcId(strCopy("throwException"))), |
| 3263 | 1, exception)); |
| 3264 | |
| 3265 | return value; |
| 3266 | } |
| 3267 | ddecl = gjContext->formats->foamDFmt.argv[fmt]; |
| 3268 | |
| 3269 | return gj0RecElt(gj0Gen(ref), ddecl, idx); |
| 3270 | } |
| 3271 | |
| 3272 | localstatic JavaCode |
| 3273 | gj0RecElt(JavaCode ref, Foam ddecl, int idx) |
| 3274 | { |
| 3275 | JavaCode c; |
| 3276 | Foam decl = ddecl->foamDDecl.argv[idx]; |
| 3277 | String id; |
| 3278 | |
| 3279 | id = decl->foamDecl.id; |
| 3280 | c = jcApplyMethodV(ref, jcId(strCopy("getField")), |
| 3281 | 2, jcLiteralInteger(idx), |
| 3282 | jcLiteralString(strCopy(id))); |
| 3283 | c = gj0TypeValueToObj(c, decl->foamDecl.type, decl->foamDecl.format); |
| 3284 | return c; |
| 3285 | } |
| 3286 | |
| 3287 | localstatic JavaCode |
| 3288 | gj0REltSet(Foam lhs, Foam rhs) |
| 3289 | { |
| 3290 | AInt fmt = lhs->foamRElt.format; |
| 3291 | AInt idx = lhs->foamRElt.field; |
| 3292 | Foam ref = lhs->foamRElt.expr; |
| 3293 | Foam ddecl = gjContext->formats->foamDFmt.argv[fmt]; |
| 3294 | Foam decl = ddecl->foamDDecl.argv[idx]; |
| 3295 | |
| 3296 | JavaCode rhsJ = gj0SetGenRhs(rhs, decl); |
| 3297 | return gj0RecSet(gj0Gen(ref), rhsJ, ddecl, idx); |
| 3298 | } |
| 3299 | |
| 3300 | localstatic JavaCode |
| 3301 | gj0RecSet(JavaCode lhsJ, JavaCode rhsJ, Foam ddecl, int idx) |
| 3302 | { |
| 3303 | JavaCode c, valJ; |
| 3304 | Foam decl = ddecl->foamDDecl.argv[idx]; |
| 3305 | String id; |
| 3306 | |
| 3307 | id = decl->foamDecl.id; |
| 3308 | |
| 3309 | valJ = gj0TypeObjToValue(rhsJ, decl->foamDecl.type, decl->foamDecl.format); |
| 3310 | c = jcApplyMethodV(lhsJ, jcId(strCopy("setField")), 3, |
| 3311 | jcLiteralInteger(idx), |
| 3312 | jcLiteralString(strCopy(id)), |
| 3313 | valJ); |
| 3314 | |
| 3315 | return c; |
| 3316 | } |
| 3317 | |
| 3318 | /* |
| 3319 | * :: Environments & Lex |
| 3320 | */ |
| 3321 | |
| 3322 | localstatic JavaCode |
| 3323 | gj0Env(Foam foam) |
| 3324 | { |
| 3325 | return gj0EnvId(foam->foamEnv.level); |
| 3326 | } |
| 3327 | |
| 3328 | localstatic JavaCode |
| 3329 | gj0Lex(Foam foam) |
| 3330 | { |
| 3331 | AInt lvl = foam->foamLex.level; |
| 3332 | AInt idx = foam->foamLex.index; |
| 3333 | AInt fmt = gjContext->prog->foamProg.levels->foamDEnv.argv[lvl]; |
| 3334 | Foam ddecl = gjContext->formats->foamDFmt.argv[fmt]; |
| 3335 | JavaCode lvlId = gj0LvlId(lvl); |
| 3336 | |
| 3337 | return gj0RecElt(lvlId, ddecl, idx); |
| 3338 | } |
| 3339 | |
| 3340 | |
| 3341 | |
| 3342 | localstatic JavaCode |
| 3343 | gj0LexSet(Foam foam, Foam rhs) |
| 3344 | { |
| 3345 | AInt lvl = foam->foamLex.level; |
| 3346 | AInt idx = foam->foamLex.index; |
| 3347 | AInt fmt = gjContext->prog->foamProg.levels->foamDEnv.argv[lvl]; |
| 3348 | Foam ddecl = gjContext->formats->foamDFmt.argv[fmt]; |
| 3349 | JavaCode lvlId = gj0LvlId(lvl); |
| 3350 | JavaCode rhsJ = gj0SetGenRhs(rhs, ddecl->foamDDecl.argv[idx]); |
| 3351 | |
| 3352 | return gj0RecSet(lvlId, rhsJ, ddecl, idx); |
| 3353 | } |
| 3354 | |
| 3355 | localstatic JavaCode |
| 3356 | gj0EElt(Foam foam) |
| 3357 | { |
| 3358 | JavaCode env, ref, lvlJ; |
| 3359 | AInt fmt = foam->foamEElt.env; |
| 3360 | Foam ddecl = gjContext->formats->foamDFmt.argv[fmt]; |
| 3361 | AInt idx = foam->foamEElt.lex; |
| 3362 | AInt lvl = foam->foamEElt.level; |
| 3363 | |
| 3364 | if (lvl == 0) |
| 3365 | env = gj0Gen(foam->foamEElt.ref); |
| 3366 | else |
| 3367 | env = jcApplyMethodV(gj0Gen(foam->foamEElt.ref), |
| 3368 | jcId(strCopy("nthParent")), |
| 3369 | 1, jcLiteralInteger(lvl)); |
| 3370 | |
| 3371 | lvlJ = jcApplyMethodV(env, jcId(strCopy("level")), 0); |
| 3372 | ref = gj0RecElt(lvlJ, ddecl, idx); |
| 3373 | |
| 3374 | return ref; |
| 3375 | } |
| 3376 | |
| 3377 | localstatic JavaCode |
| 3378 | gj0EEnv(Foam foam) |
| 3379 | { |
| 3380 | JavaCode env; |
| 3381 | AInt lvl = foam->foamEEnv.level; |
| 3382 | |
| 3383 | if (lvl == 0) |
| 3384 | env = gj0Gen(foam->foamEEnv.env); |
| 3385 | else |
| 3386 | env = jcApplyMethodV(gj0Gen(foam->foamEEnv.env), |
| 3387 | jcId(strCopy("nthParent")), |
| 3388 | 1, jcLiteralInteger(lvl)); |
| 3389 | return env; |
| 3390 | } |
| 3391 | |
| 3392 | localstatic JavaCode |
| 3393 | gj0EEnsure(Foam foam) |
| 3394 | { |
| 3395 | JavaCode stmt; |
| 3396 | Foam env = foam->foamEEnsure.env; |
| 3397 | |
| 3398 | stmt = jcApplyMethodV(gj0Gen(env), |
| 3399 | jcId(strCopy("ensure")), |
| 3400 | 0); |
| 3401 | return stmt; |
| 3402 | } |
| 3403 | |
| 3404 | |
| 3405 | localstatic JavaCode |
| 3406 | gj0EEltSet(Foam foam, Foam rhs) |
| 3407 | { |
| 3408 | JavaCode env, ref, rhsJ, lvlJ; |
| 3409 | AInt fmt = foam->foamEElt.env; |
| 3410 | Foam ddecl = gjContext->formats->foamDFmt.argv[fmt]; |
| 3411 | AInt idx = foam->foamEElt.lex; |
| 3412 | AInt lvl = foam->foamEElt.level; |
| 3413 | |
| 3414 | if (lvl == 0) |
| 3415 | env = gj0Gen(foam->foamEElt.ref); |
| 3416 | else |
| 3417 | env = jcApplyMethodV(gj0Gen(foam->foamEElt.ref), |
| 3418 | jcId(strCopy("nthParent")), |
| 3419 | 1, jcLiteralInteger(lvl)); |
| 3420 | lvlJ = jcApplyMethodV(env, jcId(strCopy("level")), 0); |
| 3421 | rhsJ = gj0SetGenRhs(rhs, ddecl->foamDDecl.argv[idx]); |
| 3422 | ref = gj0RecSet(lvlJ, rhsJ, ddecl, idx); |
| 3423 | |
| 3424 | return ref; |
| 3425 | } |
| 3426 | |
| 3427 | localstatic JavaCode |
| 3428 | gj0EInfo(Foam foam) |
| 3429 | { |
| 3430 | JavaCode stmt; |
| 3431 | Foam env = foam->foamEInfo.env; |
| 3432 | |
| 3433 | stmt = jcApplyMethodV(gj0Gen(env), |
| 3434 | jcId(strCopy("getInfo")), |
| 3435 | 0); |
| 3436 | return stmt; |
| 3437 | } |
| 3438 | |
| 3439 | localstatic JavaCode |
| 3440 | gj0EInfoSet(Foam foam, Foam rhs) |
| 3441 | { |
| 3442 | JavaCode stmt; |
| 3443 | Foam env = foam->foamEInfo.env; |
| 3444 | |
| 3445 | stmt = jcApplyMethodV(gj0Gen(env), |
| 3446 | jcId(strCopy("setInfo")), |
| 3447 | 1, gj0Gen(rhs)); |
| 3448 | return stmt; |
| 3449 | } |
| 3450 | |
| 3451 | |
| 3452 | localstatic JavaCode |
| 3453 | gj0PushEnv(Foam foam) |
| 3454 | { |
| 3455 | gj0FmtUse(foam->foamPushEnv.format); |
| 3456 | return jcConstructV(gj0TypeFrFmt(FOAM_Env, foam->foamPushEnv.format), |
| 3457 | 2, |
| 3458 | jcConstructV(gj0Id(GJ_EnvRecord), 1, |
| 3459 | gj0FmtId(foam->foamPushEnv.format)), |
| 3460 | gj0Gen(foam->foamPushEnv.parent)); |
| 3461 | } |
| 3462 | |
| 3463 | |
| 3464 | localstatic JavaCode |
| 3465 | gj0EnvId(int lvl) |
| 3466 | { |
| 3467 | return jcId(strPrintf("env%d", lvl)); |
| 3468 | } |
| 3469 | |
| 3470 | localstatic JavaCode |
| 3471 | gj0LvlId(int lvl) |
| 3472 | { |
| 3473 | return jcId(strPrintf("lvl%d", lvl)); |
| 3474 | } |
| 3475 | |
| 3476 | /* |
| 3477 | * :: Closures |
| 3478 | */ |
| 3479 | |
| 3480 | |
| 3481 | localstatic JavaCode |
| 3482 | gj0CEnv(Foam foam) |
| 3483 | { |
| 3484 | return jcApplyMethodV(gj0Gen(foam->foamCEnv.env), |
| 3485 | jcId(strCopy("getEnv")), 0); |
| 3486 | } |
| 3487 | |
| 3488 | localstatic JavaCode |
| 3489 | gj0CProg(Foam foam) |
| 3490 | { |
| 3491 | return jcApplyMethodV(gj0Gen(foam->foamCProg.prog), |
| 3492 | jcId(strCopy("getProg")), 0); |
| 3493 | } |
| 3494 | |
| 3495 | localstatic JavaCode |
| 3496 | gj0CEnvSet(Foam foam, Foam rhs) |
| 3497 | { |
| 3498 | return jcApplyMethodV(gj0Gen(foam->foamCEnv.env), |
| 3499 | jcId(strCopy("setEnv")), 1, |
| 3500 | gj0Gen(rhs) |
| 3501 | ); |
| 3502 | } |
| 3503 | |
| 3504 | localstatic JavaCode |
| 3505 | gj0CProgSet(Foam foam, Foam rhs) |
| 3506 | { |
| 3507 | return jcApplyMethodV(gj0Gen(foam->foamCProg.prog), |
| 3508 | jcId(strCopy("setProg")), 1, |
| 3509 | gj0Gen(rhs) |
| 3510 | ); |
| 3511 | } |
| 3512 | |
| 3513 | localstatic JavaCode |
| 3514 | gj0Clos(Foam foam) |
| 3515 | { |
| 3516 | Foam env = foam->foamClos.env; |
| 3517 | Foam prog = foam->foamClos.prog; |
| 3518 | |
| 3519 | assert(foamTag(prog) == FOAM_Const)do { if (!(((prog)->hdr.tag) == FOAM_Const)) _do_assert(("foamTag(prog) == FOAM_Const" ),"java/genjava.c",3519); } while (0); |
| 3520 | return jcConstructV(gj0TypeFrFmt(FOAM_Clos, 0), |
| 3521 | 2, gj0Gen(env), |
| 3522 | jcId(gj0ProgFnName(prog->foamConst.index))); |
| 3523 | } |
| 3524 | |
| 3525 | /* |
| 3526 | * :: ProgRefs |
| 3527 | */ |
| 3528 | |
| 3529 | localstatic JavaCode |
| 3530 | gj0PRef(Foam foam) |
| 3531 | { |
| 3532 | return jcApplyMethodV(gj0Gen(foam->foamPRef.prog), jcId(strCopy("getInfo")), |
| 3533 | 1, jcLiteralInteger(foam->foamPRef.idx)); |
| 3534 | } |
| 3535 | |
| 3536 | localstatic JavaCode |
| 3537 | gj0PRefSet(Foam lhs, Foam rhs) |
| 3538 | { |
| 3539 | return jcApplyMethodV(gj0Gen(lhs->foamPRef.prog), jcId(strCopy("setInfo")), |
| 3540 | 2, |
| 3541 | jcLiteralInteger(lhs->foamPRef.idx), |
| 3542 | gj0Gen(rhs)); |
| 3543 | } |
| 3544 | |
| 3545 | /* |
| 3546 | * :: Consts |
| 3547 | */ |
| 3548 | |
| 3549 | localstatic JavaCode |
| 3550 | gj0Const(Foam foam) |
| 3551 | { |
| 3552 | return gj0Nil(foam); |
| 3553 | } |
| 3554 | |
| 3555 | |
| 3556 | /* |
| 3557 | * :: Record formats |
| 3558 | * includes envs and mutis |
| 3559 | */ |
| 3560 | localstatic JavaCode gj0FmtInit(int idx); |
| 3561 | |
| 3562 | typedef struct fmtSet |
| 3563 | { |
| 3564 | BitvClass clss; |
| 3565 | Bitv bitv; |
| 3566 | } *GjFmtSet; |
| 3567 | |
| 3568 | localstatic void |
| 3569 | gj0FmtUse(AInt ddeclIdx) |
| 3570 | { |
| 3571 | intSetAdd(gjContext->fmtSet, ddeclIdx); |
| 3572 | } |
| 3573 | |
| 3574 | localstatic JavaCode |
| 3575 | gj0FmtId(AInt ddeclIdx) |
| 3576 | { |
| 3577 | return jcId(strPrintf("FORMAT_%d", ddeclIdx)); |
| 3578 | } |
| 3579 | |
| 3580 | localstatic JavaCodeList |
| 3581 | gj0FmtInits() |
| 3582 | { |
| 3583 | JavaCodeList inits; |
| 3584 | int i; |
| 3585 | |
| 3586 | inits = listNil(JavaCode)((JavaCodeList) 0); |
| 3587 | for (i=0; i<foamArgc(gjContext->formats)((gjContext->formats)->hdr.argc); i++) { |
| 3588 | if (intSetMember(gjContext->fmtSet, i)) |
| 3589 | inits = listCons(JavaCode)(JavaCode_listPointer->Cons)(gj0FmtInit(i), inits); |
| 3590 | } |
| 3591 | |
| 3592 | return listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(inits); |
| 3593 | } |
| 3594 | |
| 3595 | localstatic JavaCode |
| 3596 | gj0FmtInit(int idx) |
| 3597 | { |
| 3598 | Foam ddecl; |
| 3599 | JavaCode rhs, init; |
| 3600 | |
| 3601 | ddecl = gjContext->formats->foamDFmt.argv[idx]; |
| 3602 | rhs = jcConstructV(gj0Id(GJ_Format), 1, |
| 3603 | jcLiteralInteger(foamDDeclArgc(ddecl)(((ddecl)->hdr.argc) - (1)))); |
| 3604 | init = jcInitialisation(JCO_MOD_Static|JCO_MOD_Private, |
| 3605 | gj0Id(GJ_Format), |
| 3606 | gj0FmtId(idx), |
| 3607 | rhs); |
| 3608 | return jcStatement(init); |
| 3609 | } |
| 3610 | |
| 3611 | /* |
| 3612 | * :: CCall stub functions |
| 3613 | */ |
| 3614 | |
| 3615 | localstatic String gj0CCallStubParam(int idx); |
| 3616 | localstatic FoamTag *gj0FoamSigRets(Foam ddecl, int *nVals); |
| 3617 | |
| 3618 | localstatic FoamSig |
| 3619 | gj0FoamSigFrCCall(Foam ccall) |
| 3620 | { |
| 3621 | FoamTag *retVals; |
| 3622 | AIntList inArgList; |
| 3623 | FoamSig sig; |
| 3624 | int nRets; |
| 3625 | |
| 3626 | inArgList = listNil(AInt)((AIntList) 0); |
| 3627 | foamIter(ccall, elt, {{ { String argf = (foamInfoTable [(int)(((ccall)->hdr.tag) )-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < ((ccall )->hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *elt = (Foam *) ((ccall)->foamGen.argv )+_i; { { AInt fmt; FoamTag type = gj0FoamExprTypeWFmt(*elt, & fmt); AInt val = type + (fmt << 8); do { if (!(val != 0 )) _do_assert(("val != 0"),"java/genjava.c",3631); } while (0 ); inArgList = (AInt_listPointer->Cons)(val, inArgList); } ; }; } } }; } |
| 3628 | AInt fmt;{ { String argf = (foamInfoTable [(int)(((ccall)->hdr.tag) )-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < ((ccall )->hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *elt = (Foam *) ((ccall)->foamGen.argv )+_i; { { AInt fmt; FoamTag type = gj0FoamExprTypeWFmt(*elt, & fmt); AInt val = type + (fmt << 8); do { if (!(val != 0 )) _do_assert(("val != 0"),"java/genjava.c",3631); } while (0 ); inArgList = (AInt_listPointer->Cons)(val, inArgList); } ; }; } } }; } |
| 3629 | FoamTag type = gj0FoamExprTypeWFmt(*elt, &fmt);{ { String argf = (foamInfoTable [(int)(((ccall)->hdr.tag) )-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < ((ccall )->hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *elt = (Foam *) ((ccall)->foamGen.argv )+_i; { { AInt fmt; FoamTag type = gj0FoamExprTypeWFmt(*elt, & fmt); AInt val = type + (fmt << 8); do { if (!(val != 0 )) _do_assert(("val != 0"),"java/genjava.c",3631); } while (0 ); inArgList = (AInt_listPointer->Cons)(val, inArgList); } ; }; } } }; } |
| 3630 | AInt val = type + (fmt << 8);{ { String argf = (foamInfoTable [(int)(((ccall)->hdr.tag) )-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < ((ccall )->hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *elt = (Foam *) ((ccall)->foamGen.argv )+_i; { { AInt fmt; FoamTag type = gj0FoamExprTypeWFmt(*elt, & fmt); AInt val = type + (fmt << 8); do { if (!(val != 0 )) _do_assert(("val != 0"),"java/genjava.c",3631); } while (0 ); inArgList = (AInt_listPointer->Cons)(val, inArgList); } ; }; } } }; } |
| 3631 | assert(val != 0);{ { String argf = (foamInfoTable [(int)(((ccall)->hdr.tag) )-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < ((ccall )->hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *elt = (Foam *) ((ccall)->foamGen.argv )+_i; { { AInt fmt; FoamTag type = gj0FoamExprTypeWFmt(*elt, & fmt); AInt val = type + (fmt << 8); do { if (!(val != 0 )) _do_assert(("val != 0"),"java/genjava.c",3631); } while (0 ); inArgList = (AInt_listPointer->Cons)(val, inArgList); } ; }; } } }; } |
| 3632 | inArgList = listCons(AInt)(val, inArgList);{ { String argf = (foamInfoTable [(int)(((ccall)->hdr.tag) )-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < ((ccall )->hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *elt = (Foam *) ((ccall)->foamGen.argv )+_i; { { AInt fmt; FoamTag type = gj0FoamExprTypeWFmt(*elt, & fmt); AInt val = type + (fmt << 8); do { if (!(val != 0 )) _do_assert(("val != 0"),"java/genjava.c",3631); } while (0 ); inArgList = (AInt_listPointer->Cons)(val, inArgList); } ; }; } } }; } |
| 3633 | }){ { String argf = (foamInfoTable [(int)(((ccall)->hdr.tag) )-(int)FOAM_START]).argf; Length _i; for (_i = 0; _i < ((ccall )->hdr.argc); _i++, argf++) { if (*argf == '*') argf--; if (*argf == 'C') { Foam *elt = (Foam *) ((ccall)->foamGen.argv )+_i; { { AInt fmt; FoamTag type = gj0FoamExprTypeWFmt(*elt, & fmt); AInt val = type + (fmt << 8); do { if (!(val != 0 )) _do_assert(("val != 0"),"java/genjava.c",3631); } while (0 ); inArgList = (AInt_listPointer->Cons)(val, inArgList); } ; }; } } }; }; |
| 3634 | /* FIXME: Not sure how to get return types. */ |
| 3635 | |
| 3636 | inArgList = listNReverse(AInt)(AInt_listPointer->NReverse)(inArgList); |
| 3637 | if (ccall->foamCCall.type == FOAM_NOp && gjContext->mfmt != 0) { |
| 3638 | Foam ddecl = gjContext->formats->foamDFmt.argv[gjContext->mfmt]; |
| 3639 | retVals = gj0FoamSigRets(ddecl, &nRets); |
| 3640 | } |
| 3641 | else { |
| 3642 | retVals = 0; |
| 3643 | nRets = 0; |
| 3644 | } |
| 3645 | |
| 3646 | sig = foamSigNew(cdr(inArgList)((inArgList)->rest), ccall->foamCCall.type, nRets, retVals); |
| 3647 | listFreeCons(AInt)(AInt_listPointer->FreeCons)(inArgList); |
| 3648 | return sig; |
| 3649 | } |
| 3650 | |
| 3651 | localstatic FoamTag * |
| 3652 | gj0FoamSigRets(Foam ddecl, int *nVals) |
| 3653 | { |
| 3654 | FoamTag *arr = (FoamTag*) stoAlloc(OB_Other0, sizeof(FoamTag)*foamDDeclArgc(ddecl)(((ddecl)->hdr.argc) - (1))); |
| 3655 | int i; |
| 3656 | |
| 3657 | for (i=0; i<foamDDeclArgc(ddecl)(((ddecl)->hdr.argc) - (1)); i++) { |
| 3658 | arr[i] = ddecl->foamDDecl.argv[i]->foamDecl.type; |
| 3659 | } |
| 3660 | |
| 3661 | *nVals = foamDDeclArgc(ddecl)(((ddecl)->hdr.argc) - (1)); |
| 3662 | return arr; |
| 3663 | } |
| 3664 | |
| 3665 | |
| 3666 | localstatic JavaCodeList |
| 3667 | gj0CCallStubGen(FoamSigList sigs) |
| 3668 | { |
| 3669 | JavaCodeList fns = listNil(JavaCode)((JavaCodeList) 0); |
| 3670 | |
| 3671 | while (sigs != listNil(FoamSig)((FoamSigList) 0)) { |
| 3672 | JavaCode c = gj0CCallStubGenFrSig(car(sigs)((sigs)->first)); |
| 3673 | fns = listCons(JavaCode)(JavaCode_listPointer->Cons)(c, fns); |
| 3674 | sigs = cdr(sigs)((sigs)->rest); |
| 3675 | } |
| 3676 | return listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(fns); |
| 3677 | } |
| 3678 | |
| 3679 | |
| 3680 | |
| 3681 | /* unconditionally eats its argument |
| 3682 | * FIXME: Quadratic in number of distinct |
| 3683 | * signatures (which is probably very small). |
| 3684 | */ |
| 3685 | localstatic FoamSigList |
| 3686 | gj0CCallStubAdd(FoamSigList list, FoamSig sig) |
| 3687 | { |
| 3688 | FoamSigList tmp = list; |
| 3689 | while (tmp) { |
| 3690 | FoamSig osig = car(tmp)((tmp)->first); |
| 3691 | if (foamSigEqualModFmt(sig, osig)) |
| 3692 | break; |
| 3693 | tmp = cdr(tmp)((tmp)->rest); |
| 3694 | } |
| 3695 | if (tmp == listNil(FoamSig)((FoamSigList) 0)) { |
| 3696 | list = listCons(FoamSig)(FoamSig_listPointer->Cons)(sig, list); |
| 3697 | } |
| 3698 | else { |
| 3699 | foamSigFree(sig); |
| 3700 | } |
| 3701 | return list; |
| 3702 | } |
| 3703 | |
| 3704 | |
| 3705 | /* |
| 3706 | static Word callWxWWW(Closure a, Word b, Word c) { |
| 3707 | L2 Value r = a.call(b.toValue(), c.toValue()); |
| 3708 | L3 return r.asWord(); |
| 3709 | } |
| 3710 | */ |
| 3711 | |
| 3712 | localstatic JavaCode |
| 3713 | gj0CCallStubGenFrSig(FoamSig sig) |
| 3714 | { |
| 3715 | JavaCodeList valList, paramList; |
| 3716 | JavaCode l1, l2, call; |
| 3717 | JavaCode resultVar, retnType, body; |
| 3718 | AIntList argList = sig->inArgs; |
| 3719 | int idx; |
| 3720 | |
| 3721 | valList = listNil(JavaCode)((JavaCodeList) 0); |
| 3722 | paramList = listNil(JavaCode)((JavaCodeList) 0); |
| 3723 | idx = 0; |
| 3724 | while (argList != listNil(AInt)((AIntList) 0)) { |
| 3725 | AInt typeAndFmt = car(argList)((argList)->first); |
| 3726 | AInt type = typeAndFmt & 0xFF; |
| 3727 | AInt fmt = typeAndFmt >> 8; |
| 3728 | String id = gj0CCallStubParam(idx+1); |
| 3729 | JavaCode decl = jcParamDecl(0, gj0TypeFrFmt(type, fmt), |
| 3730 | jcId(strCopy(id))); |
| 3731 | JavaCode asValue = gj0TypeObjToValue(jcId(strCopy(id)), type, 0); |
| 3732 | |
| 3733 | valList = listCons(JavaCode)(JavaCode_listPointer->Cons)(asValue, valList); |
| 3734 | paramList = listCons(JavaCode)(JavaCode_listPointer->Cons)(decl, paramList); |
| 3735 | strFree(id); |
| 3736 | |
| 3737 | argList = cdr(argList)((argList)->rest); |
| 3738 | idx++; |
| 3739 | } |
| 3740 | |
| 3741 | valList = listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(valList); |
| 3742 | paramList = listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(paramList); |
| 3743 | |
| 3744 | paramList = listCons(JavaCode)(JavaCode_listPointer->Cons)(jcParamDecl(0, gj0TypeFrFmt(FOAM_Clos, 0), |
| 3745 | jcId(gj0CCallStubParam(0))), |
| 3746 | paramList); |
| 3747 | |
| 3748 | call = jcApplyMethod(jcId(gj0CCallStubParam(0)), |
| 3749 | jcId(strCopy("call")), valList); |
| 3750 | if (sig->retType == FOAM_NOp && sig->nRets == 0) { |
| 3751 | l1 = call; |
| 3752 | l2 = jcReturnVoid(); |
| 3753 | body = jcNLSeqV(2, jcStatement(l1), jcStatement(l2)); |
| 3754 | } |
| 3755 | else if (sig->nRets > 1) { |
| 3756 | JavaCode tmp; |
| 3757 | resultVar = jcId(strCopy("result")); |
| 3758 | |
| 3759 | l1 = jcInitialisation(0, gj0Id(GJ_FoamValue), jcoCopy(resultVar), |
| 3760 | call); |
| 3761 | tmp = jcApplyMethodV(jcoCopy(resultVar), jcId(strCopy("toMulti")), 0); |
| 3762 | l2 = jcReturn(tmp); |
| 3763 | body = jcNLSeqV(2, |
| 3764 | jcStatement(l1), |
| 3765 | jcStatement(l2)); |
| 3766 | } |
| 3767 | else { |
| 3768 | resultVar = jcId(strCopy("result")); |
| 3769 | l1 = jcInitialisation(0, gj0Id(GJ_FoamValue), jcoCopy(resultVar), |
| 3770 | call); |
| 3771 | l2 = jcReturn(gj0TypeValueToObj(jcoCopy(resultVar), sig->retType, 0)); |
| 3772 | jcoFree(resultVar); |
| 3773 | body = jcNLSeqV(2, jcStatement(l1), jcStatement(l2)); |
| 3774 | } |
| 3775 | |
| 3776 | retnType = sig->nRets == 0 |
| 3777 | ? gj0TypeFrFmt(sig->retType, 0) |
| 3778 | : gj0Id(GJ_Multi); |
| 3779 | |
| 3780 | return jcMethod(JCO_MOD_Private | JCO_MOD_Static, NULL((void*)0), |
| 3781 | retnType, jcId(gj0CCallStubName(sig)), |
| 3782 | listNil(JavaCode)((JavaCodeList) 0), paramList, |
| 3783 | listNil(JavaCode)((JavaCodeList) 0), |
| 3784 | body); |
| 3785 | } |
| 3786 | |
| 3787 | |
| 3788 | localstatic String |
| 3789 | gj0CCallStubParam(int idx) |
| 3790 | { |
| 3791 | return strPrintf("_%d", idx); |
| 3792 | } |
| 3793 | |
| 3794 | |
| 3795 | localstatic String |
| 3796 | gj0CCallStubName(FoamSig call) |
| 3797 | { |
| 3798 | AIntList tmp; |
| 3799 | String name = strCopy("ccall_"); |
| 3800 | String res; |
| 3801 | String suffix; |
| 3802 | int i; |
| 3803 | |
| 3804 | if (call->nRets == 0) |
| 3805 | suffix = strCopy(gj0TypeAbbrev(call->retType)); |
| 3806 | else { |
| 3807 | suffix = strCopy(""); |
| 3808 | for (i=0; i<call->nRets; i++) { |
| 3809 | suffix = strNConcat(suffix, gj0TypeAbbrev(call->rets[i])); |
| 3810 | } |
| 3811 | } |
| 3812 | suffix = strNConcat(suffix, "x"); |
| 3813 | tmp = call->inArgs; |
| 3814 | while (tmp != listNil(AInt)((AIntList) 0)) { |
| 3815 | AInt type = car(tmp)((tmp)->first) & 0xFF; |
| 3816 | suffix=strNConcat(suffix, gj0TypeAbbrev(type)); |
| 3817 | tmp = cdr(tmp)((tmp)->rest); |
| 3818 | } |
| 3819 | |
| 3820 | res = strNConcat(name, suffix); |
| 3821 | strFree(suffix); |
| 3822 | return res; |
| 3823 | } |
| 3824 | |
| 3825 | /* |
| 3826 | * :: Cast |
| 3827 | */ |
| 3828 | |
| 3829 | localstatic JavaCode gj0CastWordToObj(JavaCode jc, FoamTag type, AInt fmt); |
| 3830 | localstatic JavaCode gj0CastObjToWord(JavaCode jc, FoamTag type, AInt fmt); |
| 3831 | localstatic JavaCode gj0CastObjToPtr(JavaCode jc, FoamTag type, AInt fmt); |
| 3832 | |
| 3833 | localstatic JavaCode |
| 3834 | gj0Cast(Foam foam) |
| 3835 | { |
| 3836 | return gj0CastFmt(foam, gjContext->contextFmt == -1 ? emptyFormatSlot4 : gjContext->contextFmt); |
| 3837 | } |
| 3838 | |
| 3839 | localstatic JavaCode |
| 3840 | gj0CastFmt(Foam foam, AInt cfmt) |
| 3841 | { |
| 3842 | Foam ref = foam->foamCast.expr; |
| 3843 | FoamTag type = foam->foamCast.type; |
| 3844 | AInt fmt; |
| 3845 | FoamTag iType = gj0FoamExprTypeWFmt(ref, &fmt); |
| 3846 | JavaCode jc = gj0Gen(ref); |
| 3847 | |
| 3848 | if (cfmt != -1) |
| 3849 | fmt = cfmt; |
| 3850 | if (type == FOAM_Word) { |
| 3851 | return gj0CastObjToWord(jc, iType, fmt); |
| 3852 | } |
| 3853 | else if (type == FOAM_Ptr) { |
| 3854 | return gj0CastObjToPtr(jc, iType, fmt); |
| 3855 | } |
| 3856 | else if (iType == FOAM_Word) { |
| 3857 | return gj0CastWordToObj(jc, type, cfmt); |
| 3858 | } |
| 3859 | else if (iType == type) |
| 3860 | return jc; |
| 3861 | else if (iType == FOAM_Nil) |
| 3862 | return jcNull(); |
| 3863 | else if ((iType == FOAM_Clos && type == FOAM_Rec) |
| 3864 | ||(iType == FOAM_Arr && type == FOAM_SInt)) { |
| 3865 | JavaCode exception = jcConstructV(gj0Id(GJ_ClassCastException), 1, |
| 3866 | jcLiteralString(aStrPrintf("%s to %s", foamStr(iType)((foamInfoTable [(int)(iType)-(int)FOAM_START]).str), foamStr(type)((foamInfoTable [(int)(type)-(int)FOAM_START]).str)))); |
| 3867 | JavaCode throw = jcApplyV(jcMemRef(gj0Id(GJ_Foam), jcId(strCopy("throwException"))), |
| 3868 | 1, exception); |
| 3869 | return jcCast(gj0TypeFrFmt(type, 0), throw); |
| 3870 | |
| 3871 | } |
| 3872 | else { |
| 3873 | return gj0Default(foam, |
| 3874 | strPrintf("No cast: %s, %s", foamStr(type)((foamInfoTable [(int)(type)-(int)FOAM_START]).str), foamStr(iType)((foamInfoTable [(int)(iType)-(int)FOAM_START]).str))); |
| 3875 | } |
| 3876 | } |
| 3877 | |
| 3878 | /* |
| 3879 | * :: Casts |
| 3880 | */ |
| 3881 | |
| 3882 | |
| 3883 | localstatic JavaCode |
| 3884 | gj0CastWordToObj(JavaCode jc, FoamTag type, AInt fmt) |
| 3885 | { |
| 3886 | /* Rely on Word == Value */ |
| 3887 | switch (type) { |
| 3888 | case FOAM_Bool: |
| 3889 | return jcApplyMethodV(jc, jcId(strCopy("toBool")), 0); |
| 3890 | case FOAM_Byte: |
| 3891 | return jcApplyMethodV(jc, jcId(strCopy("toByte")), 0); |
| 3892 | case FOAM_SInt: |
| 3893 | return jcApplyMethodV(jc, jcId(strCopy("toSInt")), 0); |
| 3894 | case FOAM_HInt: |
| 3895 | return jcApplyMethodV(jc, jcId(strCopy("toHInt")), 0); |
| 3896 | case FOAM_BInt: |
| 3897 | return jcApplyMethodV(jc, jcId(strCopy("toBInt")), 0); |
| 3898 | case FOAM_Char: |
| 3899 | return jcApplyMethodV(jc, jcId(strCopy("toChar")), 0); |
| 3900 | case FOAM_SFlo: |
| 3901 | return jcApplyMethodV(jc, jcId(strCopy("toSFlo")), 0); |
| 3902 | case FOAM_DFlo: |
| 3903 | return jcApplyMethodV(jc, jcId(strCopy("toDFlo")), 0); |
| 3904 | case FOAM_Ptr: |
| 3905 | return jc; |
| 3906 | case FOAM_Arr: |
| 3907 | return jcApplyMethodV(jcMemRef(gj0Id(GJ_FoamWord), |
| 3908 | jcId(strCopy("U"))), |
| 3909 | jcId(strCopy("toArray")), 1, jc); |
| 3910 | case FOAM_JavaObj: { |
| 3911 | // maybe cast to fmt |
| 3912 | JavaCode jtype = gj0TypeFrFmt(type, fmt); |
| 3913 | return jcApplyMethodV(jcMemRef(gj0Id(GJ_FoamWord), |
| 3914 | jcId(strCopy("U"))), |
| 3915 | jcGenericMethodNameV(jcId(strCopy("toJavaObj")), 1, jtype), |
| 3916 | 1, jc); |
| 3917 | } |
| 3918 | default: |
| 3919 | return jcCast(gj0TypeFrFmt(type, fmt), jc); |
| 3920 | } |
| 3921 | } |
| 3922 | |
| 3923 | localstatic JavaCode |
| 3924 | gj0CastObjToWord(JavaCode val, FoamTag type, AInt fmt) |
| 3925 | { |
| 3926 | switch (type) { |
| 3927 | case FOAM_Clos: |
| 3928 | return jcApplyMethod(jcMemRef(gj0Id(GJ_FoamWord), |
| 3929 | jcId(strCopy("U"))), |
| 3930 | jcId(strCopy("fromClos")), |
| 3931 | listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(val)); |
| 3932 | case FOAM_Gener: |
| 3933 | return jcApplyMethod(jcMemRef(gj0Id(GJ_FoamWord), |
| 3934 | jcId(strCopy("U"))), |
| 3935 | jcId(strCopy("fromGener")), |
| 3936 | listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(val)); |
| 3937 | case FOAM_SInt: |
| 3938 | return jcApplyMethod(jcMemRef(gj0Id(GJ_FoamWord), |
| 3939 | jcId(strCopy("U"))), |
| 3940 | jcId(strCopy("fromSInt")), |
| 3941 | listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(val)); |
| 3942 | |
| 3943 | case FOAM_Bool: |
| 3944 | return jcApplyMethod(jcMemRef(gj0Id(GJ_FoamWord), |
| 3945 | jcId(strCopy("U"))), |
| 3946 | jcId(strCopy("fromBool")), |
| 3947 | listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(val)); |
| 3948 | case FOAM_Arr: |
| 3949 | return jcApplyMethod(jcMemRef(gj0Id(GJ_FoamWord), |
| 3950 | jcId(strCopy("U"))), |
| 3951 | jcId(strCopy("fromArray")), |
| 3952 | listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(val)); |
| 3953 | case FOAM_Ptr: |
| 3954 | return jcCast(gj0Id(GJ_FoamWord), val); |
| 3955 | case FOAM_Rec: |
| 3956 | return jcApplyMethod(jcMemRef(gj0Id(GJ_FoamWord), |
| 3957 | jcId(strCopy("U"))), |
| 3958 | jcId(strCopy("fromRec")), |
| 3959 | listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(val)); |
| 3960 | case FOAM_Char: |
| 3961 | return jcApplyMethod(jcMemRef(gj0Id(GJ_FoamWord), |
| 3962 | jcId(strCopy("U"))), |
| 3963 | jcId(strCopy("fromChar")), |
| 3964 | listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(val)); |
| 3965 | case FOAM_Byte: |
| 3966 | return jcApplyMethod(jcMemRef(gj0Id(GJ_FoamWord), |
| 3967 | jcId(strCopy("U"))), |
| 3968 | jcId(strCopy("fromByte")), |
| 3969 | listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(val)); |
| 3970 | case FOAM_HInt: |
| 3971 | return jcApplyMethod(jcMemRef(gj0Id(GJ_FoamWord), |
| 3972 | jcId(strCopy("U"))), |
| 3973 | jcId(strCopy("fromHInt")), |
| 3974 | listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(val)); |
| 3975 | case FOAM_BInt: |
| 3976 | return jcApplyMethod(jcMemRef(gj0Id(GJ_FoamWord), |
| 3977 | jcId(strCopy("U"))), |
| 3978 | jcId(strCopy("fromBInt")), |
| 3979 | listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(val)); |
| 3980 | case FOAM_SFlo: |
| 3981 | return jcApplyMethod(jcMemRef(gj0Id(GJ_FoamWord), |
| 3982 | jcId(strCopy("U"))), |
| 3983 | jcId(strCopy("fromSFlo")), |
| 3984 | listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(val)); |
| 3985 | case FOAM_DFlo: |
| 3986 | return jcApplyMethod(jcMemRef(gj0Id(GJ_FoamWord), |
| 3987 | jcId(strCopy("U"))), |
| 3988 | jcId(strCopy("fromDFlo")), |
| 3989 | listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(val)); |
| 3990 | case FOAM_JavaObj: |
| 3991 | return jcApplyMethod(jcMemRef(gj0Id(GJ_FoamWord), |
| 3992 | jcId(strCopy("U"))), |
| 3993 | jcId(strCopy("fromJavaObj")), |
| 3994 | listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(val)); |
| 3995 | case FOAM_Nil: |
| 3996 | return val; |
| 3997 | default: |
| 3998 | return jcCast(jcSpaceSeqV(2, gj0Id(GJ_FoamWord), |
| 3999 | jcComment(strCopy(foamStr(type)((foamInfoTable [(int)(type)-(int)FOAM_START]).str)))), |
| 4000 | val); |
| 4001 | } |
| 4002 | } |
| 4003 | |
| 4004 | localstatic JavaCode |
| 4005 | gj0CastObjToPtr(JavaCode val, FoamTag type, AInt fmt) |
| 4006 | { |
| 4007 | switch (type) { |
| 4008 | // self |
| 4009 | case FOAM_Ptr: |
| 4010 | return val; |
| 4011 | // allocated once (I think) |
| 4012 | case FOAM_Arr: |
| 4013 | case FOAM_Rec: |
| 4014 | case FOAM_JavaObj: |
| 4015 | case FOAM_Clos: |
| 4016 | return val; |
| 4017 | // wrapped by runtime |
| 4018 | case FOAM_Word: |
| 4019 | return jcApplyMethod(jcMemRef(gj0Id(GJ_FoamWord), |
| 4020 | jcId(strCopy("U"))), |
| 4021 | jcId(strCopy("toPtr")), |
| 4022 | listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(val)); |
| 4023 | // throw an error |
| 4024 | case FOAM_DFlo: |
| 4025 | case FOAM_SFlo: |
| 4026 | case FOAM_Byte: |
| 4027 | case FOAM_Char: |
| 4028 | case FOAM_SInt: |
| 4029 | case FOAM_Bool: |
| 4030 | case FOAM_HInt: |
| 4031 | case FOAM_BInt: |
| 4032 | return val; |
| 4033 | case FOAM_Nil: |
| 4034 | return val; |
| 4035 | default: |
| 4036 | return val; |
| 4037 | } |
| 4038 | } |
| 4039 | |
| 4040 | /* |
| 4041 | * :: Class level boilerplate |
| 4042 | */ |
| 4043 | |
| 4044 | localstatic JavaCodeList gj0ClassFields(); |
| 4045 | localstatic JavaCode gj0ClassMainMethod(String className); |
| 4046 | localstatic JavaCode gj0ClassConstructor(String className); |
| 4047 | localstatic JavaCode gj0ClassRunMethod(String className); |
| 4048 | localstatic int gj0ClassFindInitDeclIdx(); |
| 4049 | localstatic Foam gj0ClassFindInitDef(); |
| 4050 | localstatic JavaCode gj0UtilCCall(JavaCode c, JavaCodeList args); |
| 4051 | |
| 4052 | localstatic JavaCodeList |
| 4053 | gj0ClassHeader(String className) |
| 4054 | { |
| 4055 | JavaCodeList list, fields; |
| 4056 | JavaCode constructor; |
| 4057 | JavaCode runMethod; |
| 4058 | |
| 4059 | list = listNil(JavaCode)((JavaCodeList) 0); |
| 4060 | fields = gj0ClassFields(); |
| 4061 | constructor = gj0ClassConstructor(className); |
| 4062 | runMethod = gj0ClassRunMethod(className); |
| 4063 | |
| 4064 | list = listNConcat(JavaCode)(JavaCode_listPointer->NConcat)(fields, list); |
| 4065 | list = listCons(JavaCode)(JavaCode_listPointer->Cons)(constructor, list); |
| 4066 | list = listCons(JavaCode)(JavaCode_listPointer->Cons)(runMethod, list); |
| 4067 | |
| 4068 | if (gjArgs->createMain) |
| 4069 | list = listCons(JavaCode)(JavaCode_listPointer->Cons)(gj0ClassMainMethod(className), list); |
| 4070 | |
| 4071 | return listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(list); |
| 4072 | } |
| 4073 | |
| 4074 | localstatic JavaCodeList |
| 4075 | gj0ClassFields() |
| 4076 | { |
| 4077 | JavaCodeList vars; |
| 4078 | JavaCode ctxtDecl, declJ; |
| 4079 | int i; |
| 4080 | |
| 4081 | /* private FoamContext ctxt;*/ |
| 4082 | ctxtDecl = jcMemberDecl(JCO_MOD_Private, |
| 4083 | gj0Id(GJ_FoamContext), gj0Id(GJ_ContextVar)); |
| 4084 | |
| 4085 | vars = listNil(JavaCode)((JavaCodeList) 0); |
| 4086 | for (i=0; i< foamDDeclArgc(gjContextGlobals)((((gjContext->formats->foamDFmt.argv[0]))->hdr.argc ) - (1)); i++) { |
| 4087 | Foam decl = gjContextGlobal(i)((gjContext->formats->foamDFmt.argv[0])->foamDDecl.argv [i]); |
| 4088 | |
| 4089 | if (decl->foamGDecl.protocol != FOAM_Proto_Init) |
| 4090 | continue; |
| 4091 | declJ = jcMemberDecl(JCO_MOD_Private, |
| 4092 | gj0TypeFrFmt(FOAM_Clos, 0), |
| 4093 | jcId(gj0InitVar(i))); |
| 4094 | vars = listCons(JavaCode)(JavaCode_listPointer->Cons)(jcStatement(declJ), vars); |
| 4095 | } |
| 4096 | |
| 4097 | |
| 4098 | return listCons(JavaCode)(JavaCode_listPointer->Cons)(jcStatement(ctxtDecl), vars); |
| 4099 | |
| 4100 | } |
| 4101 | |
| 4102 | localstatic JavaCode |
| 4103 | gj0ClassConstructor(String className) |
| 4104 | { |
| 4105 | /* public foo(FoamContext ctxt) { |
| 4106 | * this.ctxt = ctxt; |
| 4107 | * <init_foo> = new Clos(null, CFX); |
| 4108 | * <init_*> = ctxt.createLoadFn(ctxt) |
| 4109 | * } |
| 4110 | */ |
| 4111 | JavaCodeList args; |
| 4112 | JavaCodeList body, inits; |
| 4113 | JavaCode s1, declJ, rhs; |
| 4114 | Foam decl; |
| 4115 | int i; |
| 4116 | |
| 4117 | args = listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(jcParamDecl(0, gj0Id(GJ_FoamContext), gj0Id(GJ_ContextVar))); |
| 4118 | s1 = jcAssign(jcMemRef(jcThis(), gj0Id(GJ_ContextVar)), gj0Id(GJ_ContextVar)); |
| 4119 | |
| 4120 | s1 = jcStatement(s1); |
| 4121 | |
| 4122 | inits = listNil(JavaCode)((JavaCodeList) 0); |
| 4123 | for (i = 0; i < foamDDeclArgc(gjContextGlobals)((((gjContext->formats->foamDFmt.argv[0]))->hdr.argc ) - (1)); i++) { |
| 4124 | decl = gjContextGlobal(i)((gjContext->formats->foamDFmt.argv[0])->foamDDecl.argv [i]); |
| 4125 | |
| 4126 | if (decl->foamGDecl.protocol != FOAM_Proto_Init) |
| 4127 | continue; |
| 4128 | if (decl->foamGDecl.dir == FOAM_GDecl_Import) |
| 4129 | rhs = jcApplyMethodV(gj0Id(GJ_ContextVar), |
| 4130 | jcId(strCopy("createLoadFn")), |
| 4131 | 1, jcLiteralString(strCopy(decl->foamGDecl.id))); |
| 4132 | else |
| 4133 | rhs = jcConstructV(gj0TypeFrFmt(FOAM_Clos, 0), |
| 4134 | 2, jcNull(), |
| 4135 | jcId(gj0ProgFnName(0))); |
| 4136 | declJ = jcAssign(jcId(gj0InitVar(i)), rhs); |
| 4137 | inits = listCons(JavaCode)(JavaCode_listPointer->Cons)(jcStatement(declJ), inits); |
| 4138 | } |
| 4139 | |
| 4140 | body = listCons(JavaCode)(JavaCode_listPointer->Cons)(s1, listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(inits)); |
| 4141 | return jcConstructor(JCO_MOD_Public, NULL((void*)0), |
| 4142 | jcId(strCopy(className)), |
| 4143 | listNil(JavaCode)((JavaCodeList) 0), args, |
| 4144 | listNil(JavaCode)((JavaCodeList) 0), |
| 4145 | jcNLSeq(body)); |
| 4146 | } |
| 4147 | |
| 4148 | localstatic JavaCode |
| 4149 | gj0ClassRunMethod(String className) |
| 4150 | { |
| 4151 | /* public void run() { |
| 4152 | * c0_langx(null) |
| 4153 | * Clos c = ctxt.getGlobal(basic) |
| 4154 | * c.getProg().ocall(c.getEnv()); |
| 4155 | * } |
| 4156 | */ |
| 4157 | |
| 4158 | JavaCode s1; |
| 4159 | int idx; |
| 4160 | |
| 4161 | idx = gj0ClassFindInitDeclIdx(); |
| 4162 | s1 = gj0UtilCCall(jcId(gj0InitVar(idx)), listNil(JavaCode)((JavaCodeList) 0)); |
| 4163 | |
| 4164 | return jcMethod(JCO_MOD_Public, NULL((void*)0), |
| 4165 | jcKeyword(symInternConst("void")symProbe("void", 1)), |
| 4166 | jcId(strCopy("run")), |
| 4167 | listNil(JavaCode)((JavaCodeList) 0), listNil(JavaCode)((JavaCodeList) 0),listNil(JavaCode)((JavaCodeList) 0), |
| 4168 | jcStatement(s1)); |
| 4169 | } |
| 4170 | |
| 4171 | |
| 4172 | localstatic int |
| 4173 | gj0ClassFindInitDeclIdx() |
| 4174 | { |
| 4175 | int i; |
| 4176 | for (i=0; i<foamArgc(gjContextGlobals)(((gjContext->formats->foamDFmt.argv[0]))->hdr.argc); i++) { |
| 4177 | Foam decl = gjContextGlobal(i)((gjContext->formats->foamDFmt.argv[0])->foamDDecl.argv [i]); |
| 4178 | if (decl->foamGDecl.dir == FOAM_GDecl_Export |
| 4179 | && decl->foamGDecl.protocol == FOAM_Proto_Init) { |
| 4180 | return i; |
| 4181 | } |
| 4182 | } |
| 4183 | return -1; |
| 4184 | } |
| 4185 | |
| 4186 | localstatic Foam |
| 4187 | gj0ClassFindInitDef() |
| 4188 | { |
| 4189 | int i; |
| 4190 | for (i=0; i<foamArgc(gjContext->defs)((gjContext->defs)->hdr.argc); i++) { |
| 4191 | Foam def = gjContext->defs->foamDDef.argv[i]; |
| 4192 | if (foamTag(def)((def)->hdr.tag) != FOAM_Def) |
| 4193 | continue; |
| 4194 | if (foamTag(def->foamDef.lhs)((def->foamDef.lhs)->hdr.tag) != FOAM_Glo) |
| 4195 | continue; |
| 4196 | return def; |
| 4197 | } |
| 4198 | return NULL((void*)0); |
| 4199 | } |
| 4200 | |
| 4201 | |
| 4202 | localstatic JavaCode |
| 4203 | gj0UtilCCall(JavaCode c, JavaCodeList args) |
| 4204 | { |
| 4205 | JavaCode prog, env, call; |
| 4206 | prog = jcApplyMethodV(jcoCopy(c), jcId(strCopy("getProg")), 0); |
| 4207 | env = jcApplyMethodV(jcoCopy(c), jcId(strCopy("getEnv")), 0); |
| 4208 | args = listCons(JavaCode)(JavaCode_listPointer->Cons)(env, args); |
| 4209 | call = jcApplyMethod(prog, jcId(strCopy("ocall")), args); |
| 4210 | |
| 4211 | jcoFree(c); |
| 4212 | return call; |
| 4213 | } |
| 4214 | |
| 4215 | |
| 4216 | |
| 4217 | localstatic JavaCode |
| 4218 | gj0ClassMainMethod(String className) |
| 4219 | { |
| 4220 | JavaCodeList args; |
| 4221 | JavaCode body, s1, s2, s3; |
| 4222 | JavaCode instance, ctxt, method; |
| 4223 | int modifiers; |
| 4224 | |
| 4225 | instance = jcId(strCopy("instance")); |
| 4226 | ctxt = gj0Id(GJ_ContextVar); |
| 4227 | s1 = jcInitialisation(0, gj0Id(GJ_FoamContext), jcoCopy(ctxt), |
| 4228 | jcConstructV(gj0Id(GJ_FoamContext), 0)); |
| 4229 | s2 = jcInitialisation(0, gj0Id(GJ_FoamClass), jcoCopy(instance), |
| 4230 | jcConstructV(jcId(strCopy(className)), |
| 4231 | 1, jcoCopy(ctxt))); |
| 4232 | |
| 4233 | s3 = jcApplyMethodV(jcoCopy(ctxt), jcId(strCopy("startFoam")), 2, |
| 4234 | jcoCopy(instance), jcId(strCopy("args"))); |
| 4235 | |
| 4236 | body = jcNLSeqV(3, jcStatement(s1), jcStatement(s2), jcStatement(s3)); |
| 4237 | modifiers = JCO_MOD_Public | JCO_MOD_Static; |
| 4238 | args = listList(JavaCode)(JavaCode_listPointer->List)(1, |
| 4239 | jcParamDecl(0, jcArrayOf(gj0Id(GJ_String)), |
| 4240 | jcId(strCopy("args")))); |
| 4241 | |
| 4242 | jcoFree(ctxt); |
| 4243 | jcoFree(instance); |
| 4244 | method = jcMethod(modifiers, strCopy("main method"), |
| 4245 | jcKeyword(symInternConst("void")symProbe("void", 1)), |
| 4246 | gj0Id(GJ_Main), listNil(JavaCode)((JavaCodeList) 0), args, listNil(JavaCode)((JavaCodeList) 0), |
| 4247 | body); |
| 4248 | return method; |
| 4249 | } |
| 4250 | |
| 4251 | |
| 4252 | /* |
| 4253 | * :: Initialisation vars |
| 4254 | */ |
| 4255 | |
| 4256 | localstatic String |
| 4257 | gj0InitVar(AInt idx) |
| 4258 | { |
| 4259 | Foam gdecl = gjContextGlobal(idx)((gjContext->formats->foamDFmt.argv[0])->foamDDecl.argv [idx]); |
| 4260 | return strPrintf("%s_init", gdecl->foamGDecl.id); |
| 4261 | } |
| 4262 | |
| 4263 | |
| 4264 | /* |
| 4265 | * :: Ids. |
| 4266 | * Collected together for readability |
| 4267 | */ |
| 4268 | |
| 4269 | struct gjIdInfo { |
| 4270 | GjId id; |
| 4271 | String pkg; |
| 4272 | String name; |
| 4273 | }; |
| 4274 | |
| 4275 | struct gjIdInfo gjIdInfo[] = { |
| 4276 | {GJ_Foam, "foamj", "Foam"}, |
| 4277 | {GJ_FoamWord, "foamj", "Word"}, |
| 4278 | {GJ_FoamClos, "foamj", "Clos"}, |
| 4279 | {GJ_FoamGener, "foamj", "Gener"}, |
| 4280 | {GJ_FoamGenIter,"foamj", "GenIter"}, |
| 4281 | {GJ_FoamRecord, "foamj", "Record"}, |
| 4282 | {GJ_FoamEnv, "foamj", "Env"}, |
| 4283 | {GJ_FoamClass, "foamj", "FoamClass"}, |
| 4284 | {GJ_FoamContext,"foamj", "FoamContext"}, |
| 4285 | {GJ_FoamHelper, "foamj", "FoamHelper"}, |
| 4286 | |
| 4287 | {GJ_FoamFn, "foamj", "Fn"}, |
| 4288 | {GJ_FoamGFn, "foamj", "GFn"}, |
| 4289 | |
| 4290 | {GJ_FoamValue, "foamj", "Value"}, |
| 4291 | {GJ_Multi, "foamj", "MultiRecord"}, |
| 4292 | {GJ_FoamGlobals,"foamj", "Globals"}, |
| 4293 | {GJ_Format, "foamj", "Format"}, |
| 4294 | {GJ_EnvRecord, "foamj", "EnvRecord"}, |
| 4295 | {GJ_AldorObject,"foamj", "AldorObject"}, |
| 4296 | |
| 4297 | {GJ_JavaException, "foamj", "JavaException"}, |
| 4298 | {GJ_FoamUserException, "foamj", "FoamUserException"}, |
| 4299 | |
| 4300 | {GJ_Object, 0, "Object"}, |
| 4301 | {GJ_String, 0, "String"}, |
| 4302 | {GJ_BigInteger, "java.math", "BigInteger"}, |
| 4303 | {GJ_LangException, 0, "Exception"}, |
| 4304 | {GJ_NullPointerException, 0, "NullPointerException"}, |
| 4305 | {GJ_ClassCastException, 0, "ClassCastException"}, |
| 4306 | |
| 4307 | {GJ_ContextVar, 0, "ctxt"}, |
| 4308 | {GJ_Main, 0, "main"}, |
| 4309 | {GJ_class, 0, "class"}, |
| 4310 | {GJ_Instance, 0, "instanceForClass"}, |
| 4311 | {GJ_INVALID, 0, 0 }, |
| 4312 | }; |
| 4313 | |
| 4314 | |
| 4315 | localstatic JavaCode |
| 4316 | gj0Id(GjId id) |
| 4317 | { |
| 4318 | struct gjIdInfo *info; |
| 4319 | JavaCode javaId; |
| 4320 | assert(gjIdInfo[GJ_LIMIT].id == GJ_INVALID)do { if (!(gjIdInfo[GJ_LIMIT].id == GJ_INVALID)) _do_assert(( "gjIdInfo[GJ_LIMIT].id == GJ_INVALID"),"java/genjava.c",4320) ; } while (0); |
| 4321 | |
| 4322 | info = &gjIdInfo[id]; |
| 4323 | assert(id == info->id)do { if (!(id == info->id)) _do_assert(("id == info->id" ),"java/genjava.c",4323); } while (0); |
| 4324 | |
| 4325 | if (info->pkg != 0) |
| 4326 | javaId = jcImportedId(strCopy(info->pkg), |
| 4327 | strCopy(info->name)); |
| 4328 | else |
| 4329 | javaId = jcId(strCopy(info->name)); |
| 4330 | |
| 4331 | assert(!jcoIsEmpty(javaId))do { if (!(!jcoIsEmpty(javaId))) _do_assert(("!jcoIsEmpty(javaId)" ),"java/genjava.c",4331); } while (0); |
| 4332 | return javaId; |
| 4333 | } |
| 4334 | |
| 4335 | |
| 4336 | |
| 4337 | /* |
| 4338 | * :: PCall |
| 4339 | */ |
| 4340 | localstatic JavaCode gj0PCallOther(Foam foam); |
| 4341 | localstatic JavaCode gj0PCallJavaMethod(Foam foam); |
| 4342 | localstatic JavaCode gj0PCallJavaConstructor(Foam foam); |
| 4343 | localstatic JavaCode gj0PCallJavaConstructorGlo(Foam foam); |
| 4344 | localstatic JavaCode gj0PCallJavaStatic(Foam foam); |
| 4345 | localstatic JavaCodeList gj0PCallCastArgs(Foam op, JavaCodeList args); |
| 4346 | localstatic JavaCodeList gj0PCallGenArgs(Foam op, int offs, Foam *argv); |
| 4347 | |
| 4348 | |
| 4349 | localstatic JavaCode |
| 4350 | gj0PCall(Foam foam) |
| 4351 | { |
| 4352 | switch (foam->foamPCall.protocol) { |
| 4353 | case FOAM_Proto_Other: |
| 4354 | case FOAM_Proto_C: |
| 4355 | return gj0PCallOther(foam); |
| 4356 | case FOAM_Proto_JavaMethod: |
| 4357 | return gj0PCallJavaMethod(foam); |
| 4358 | case FOAM_Proto_JavaConstructor: |
| 4359 | return gj0PCallJavaConstructor(foam); |
| 4360 | case FOAM_Proto_Java: |
| 4361 | return gj0PCallJavaStatic(foam); |
| 4362 | default: |
| 4363 | return jcSpaceSeqV(2, jcNull(), jcComment(strPrintf("%pFoam", foam))); |
| 4364 | } |
| 4365 | } |
| 4366 | |
| 4367 | localstatic JavaCode |
| 4368 | gj0PCallOther(Foam foam) |
| 4369 | { |
| 4370 | JavaCodeList args; |
| 4371 | Foam decl, op; |
| 4372 | |
| 4373 | op = foam->foamPCall.op; |
| 4374 | |
| 4375 | assert(foamTag(op) == FOAM_Glo)do { if (!(((op)->hdr.tag) == FOAM_Glo)) _do_assert(("foamTag(op) == FOAM_Glo" ),"java/genjava.c",4375); } while (0); |
| 4376 | |
| 4377 | decl = gjContextGlobal(op->foamGlo.index)((gjContext->formats->foamDFmt.argv[0])->foamDDecl.argv [op->foamGlo.index]); |
| 4378 | args = gj0GenList(foam->foamPCall.argv, foamPCallArgc(foam)(((foam)->hdr.argc) - (3))); |
| 4379 | return jcApplyMethod(gj0Id(GJ_Foam), jcId(strCopy(decl->foamGDecl.id)), |
| 4380 | args); |
| 4381 | } |
| 4382 | |
| 4383 | localstatic JavaCode |
| 4384 | gj0PCallJavaMethod(Foam foam) |
| 4385 | { |
| 4386 | JavaCodeList args; |
| 4387 | JavaCode result, target; |
| 4388 | Foam decl, op, sig; |
| 4389 | String type, opName, pkg; |
| 4390 | |
| 4391 | op = foam->foamPCall.op; |
| 4392 | |
| 4393 | assert(foamPCallArgc(foam) > 0)do { if (!((((foam)->hdr.argc) - (3)) > 0)) _do_assert( ("foamPCallArgc(foam) > 0"),"java/genjava.c",4393); } while (0); |
| 4394 | |
| 4395 | if (foamTag(op)((op)->hdr.tag) == FOAM_Arr) { |
| 4396 | return jcApplyMethod(gj0Gen(foam->foamPCall.argv[0]), jcId(strCopy("rep")), |
| 4397 | listNil(JavaCode)((JavaCodeList) 0)); |
| 4398 | } |
| 4399 | else { |
| 4400 | decl = gjContextGlobal(op->foamGlo.index)((gjContext->formats->foamDFmt.argv[0])->foamDDecl.argv [op->foamGlo.index]); |
| 4401 | sig = gjContext->formats->foamDFmt.argv[decl->foamGDecl.format]; |
| 4402 | |
| 4403 | target = gj0GenFmt(foam->foamPCall.argv[0], javaSigArgN(sig, 0)->foamDecl.format); |
| 4404 | args = gj0PCallGenArgs(op, 1, foam->foamPCall.argv); |
| 4405 | |
| 4406 | strSplitLast(strCopy(decl->foamGDecl.id), '.', &type, &opName); |
| 4407 | assert(type != 0)do { if (!(type != 0)) _do_assert(("type != 0"),"java/genjava.c" ,4407); } while (0); |
| 4408 | strSplitLast(type, '.', &pkg, &type); |
| 4409 | |
| 4410 | return jcApplyMethod(jcCast(jcImportedId(pkg, type), target), |
| 4411 | jcId(opName), |
| 4412 | foamTag(op)((op)->hdr.tag) != FOAM_Glo ? args : gj0PCallCastArgs(op, args)); |
| 4413 | } |
| 4414 | } |
| 4415 | |
| 4416 | localstatic JavaCode |
| 4417 | gj0PCallJavaConstructor(Foam foam) |
| 4418 | { |
| 4419 | Foam op = foam->foamPCall.op; |
| 4420 | if (foamTag(op)((op)->hdr.tag) == FOAM_Glo) { |
| 4421 | return gj0PCallJavaConstructorGlo(foam); |
| 4422 | } |
| 4423 | else if (foamTag(op)((op)->hdr.tag) == FOAM_Arr) { |
| 4424 | JavaCodeList args = gj0GenList(foam->foamPCall.argv, foamPCallArgc(foam)(((foam)->hdr.argc) - (3))); |
| 4425 | return jcConstruct(jcImportedIdFrString(foamArrToString(op)), args); |
| 4426 | } |
| 4427 | else { |
| 4428 | bug("unknown pcall"); |
| 4429 | return NULL((void*)0); |
| 4430 | } |
| 4431 | } |
| 4432 | |
| 4433 | localstatic JavaCode |
| 4434 | gj0PCallJavaConstructorGlo(Foam foam) |
| 4435 | { |
| 4436 | JavaCodeList args; |
| 4437 | Foam decl, op; |
| 4438 | String type, pkg; |
| 4439 | |
| 4440 | op = foam->foamPCall.op; |
| 4441 | |
| 4442 | assert(foamPCallArgc(foam) >= 1)do { if (!((((foam)->hdr.argc) - (3)) >= 1)) _do_assert (("foamPCallArgc(foam) >= 1"),"java/genjava.c",4442); } while (0); |
| 4443 | |
| 4444 | decl = gjContextGlobal(op->foamGlo.index)((gjContext->formats->foamDFmt.argv[0])->foamDDecl.argv [op->foamGlo.index]); |
| 4445 | //args = gj0GenList(foam->foamPCall.argv, foamPCallArgc(foam)); |
| 4446 | args = gj0PCallGenArgs(op, 0, foam->foamPCall.argv); |
| 4447 | |
| 4448 | strSplitLast(strCopy(decl->foamGDecl.id), '.', &pkg, &type); |
| 4449 | |
| 4450 | return jcConstruct(jcImportedId(pkg, type), gj0PCallCastArgs(op, args)); |
| 4451 | } |
| 4452 | |
| 4453 | |
| 4454 | localstatic JavaCode |
| 4455 | gj0PCallJavaStatic(Foam foam) |
| 4456 | { |
| 4457 | JavaCodeList args; |
| 4458 | Foam decl, op; |
| 4459 | String id, type; |
| 4460 | |
| 4461 | op = foam->foamPCall.op; |
| 4462 | |
| 4463 | assert(foamTag(op) == FOAM_Glo)do { if (!(((op)->hdr.tag) == FOAM_Glo)) _do_assert(("foamTag(op) == FOAM_Glo" ),"java/genjava.c",4463); } while (0); |
| 4464 | |
| 4465 | decl = gjContextGlobal(op->foamGlo.index)((gjContext->formats->foamDFmt.argv[0])->foamDDecl.argv [op->foamGlo.index]); |
| 4466 | //args = gj0GenList(foam->foamPCall.argv, foamPCallArgc(foam)); |
| 4467 | args = gj0PCallGenArgs(op, 0, foam->foamPCall.argv); |
| 4468 | |
| 4469 | strSplitLast(strCopy(decl->foamGDecl.id), '.', &type, &id); |
| 4470 | JavaCode typeId = jcImportedIdFrString(type); |
| 4471 | |
| 4472 | return jcApply(jcMemRef(typeId, jcId(id)), gj0PCallCastArgs(op, args)); |
| 4473 | } |
| 4474 | |
| 4475 | localstatic JavaCodeList |
| 4476 | gj0PCallCastArgs(Foam op, JavaCodeList argsIn) |
| 4477 | { |
| 4478 | JavaCodeList args = argsIn; |
| 4479 | Foam glo = gjContextGlobals(gjContext->formats->foamDFmt.argv[0])->foamDDecl.argv[op->foamGlo.index]; |
| 4480 | Foam ddecl = gjContext->formats->foamDFmt.argv[glo->foamGDecl.format]; |
| 4481 | int i = 0; |
| 4482 | |
| 4483 | assert(ddecl->foamDDecl.usage == FOAM_DDecl_JavaSig)do { if (!(ddecl->foamDDecl.usage == FOAM_DDecl_JavaSig)) _do_assert (("ddecl->foamDDecl.usage == FOAM_DDecl_JavaSig"),"java/genjava.c" ,4483); } while (0); |
| 4484 | //assert(javaSigArgc(ddecl) == listLength(JavaCode)(argsIn)); // FIXME: Should be able to get the args right.. |
| 4485 | |
| 4486 | /* Cast java-valued arguments - all other types are not converted */ |
| 4487 | while (args != listNil(JavaCode)((JavaCodeList) 0)) { |
| 4488 | Foam decl = javaSigArgN(ddecl, i); |
| 4489 | String pkg, type; |
| 4490 | // FIXME: This is probably not needed |
| 4491 | if (decl->foamDecl.type == FOAM_Ptr) { |
| 4492 | strSplitLast(strCopy(decl->foamGDecl.id), '.', &pkg, &type); |
| 4493 | car(args)((args)->first) = jcCast(jcImportedId(pkg, type), car(args)((args)->first)); |
| 4494 | } |
| 4495 | args = cdr(args)((args)->rest); |
| 4496 | i++; |
| 4497 | } |
| 4498 | |
| 4499 | return argsIn; |
| 4500 | } |
| 4501 | |
| 4502 | localstatic JavaCodeList |
| 4503 | gj0PCallGenArgs(Foam op, int offs, Foam *argv) |
| 4504 | { |
| 4505 | Foam glo = gjContextGlobals(gjContext->formats->foamDFmt.argv[0])->foamDDecl.argv[op->foamGlo.index]; |
| 4506 | Foam ddecl = gjContext->formats->foamDFmt.argv[glo->foamGDecl.format]; |
| 4507 | JavaCodeList args = listNil(JavaCode)((JavaCodeList) 0); |
| 4508 | int i; |
| 4509 | |
| 4510 | for (i=offs; i<javaSigArgc(ddecl); i++) { |
| 4511 | args = listCons(JavaCode)(JavaCode_listPointer->Cons)(gj0GenFmt(argv[i], javaSigArgN(ddecl, i)->foamDecl.format), args); |
| 4512 | } |
| 4513 | return listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(args); |
| 4514 | } |
| 4515 | |
| 4516 | |
| 4517 | localstatic Bool |
| 4518 | gj0PCallThrowsException(Foam foam) |
| 4519 | { |
| 4520 | if (foamTag(foam)((foam)->hdr.tag) != FOAM_PCall) |
| 4521 | return false((int) 0); |
| 4522 | switch (foam->foamPCall.protocol) { |
| 4523 | case FOAM_Proto_Java: |
| 4524 | case FOAM_Proto_JavaMethod: |
| 4525 | case FOAM_Proto_JavaConstructor: |
| 4526 | break; |
| 4527 | default: |
| 4528 | return false((int) 0); |
| 4529 | } |
| 4530 | |
| 4531 | Foam op = foam->foamPCall.op; |
| 4532 | if (foamTag(op)((op)->hdr.tag) != FOAM_Glo) |
| 4533 | return false((int) 0); |
| 4534 | Foam gdecl = gjContextGlobal(op->foamGlo.index)((gjContext->formats->foamDFmt.argv[0])->foamDDecl.argv [op->foamGlo.index]); |
| 4535 | Foam fmt = gjContext->formats->foamDFmt.argv[gdecl->foamDecl.format]; |
| 4536 | |
| 4537 | if (javaSigExn(fmt)->foamDecl.type != FOAM_NOp) { |
| 4538 | return true1; |
| 4539 | } |
| 4540 | return false((int) 0); |
| 4541 | } |
| 4542 | |
| 4543 | localstatic JavaCode |
| 4544 | gj0PCallCatchException(JavaCode code) |
| 4545 | { |
| 4546 | code = jcTryCatch(jcBlock(jcStatement(code)), |
| 4547 | jcCatch(jcLocalDecl(0, gj0Id(GJ_LangException), jcId(strCopy("exn"))), |
| 4548 | jcBlock(jcStatement(jcThrow(jcConstructV(gj0Id(GJ_JavaException), 1, |
| 4549 | jcId(strCopy("exn"))))))), |
| 4550 | NULL((void*)0)); |
| 4551 | return code; |
| 4552 | } |
| 4553 | |
| 4554 | |
| 4555 | |
| 4556 | /* |
| 4557 | * :: BCall |
| 4558 | */ |
| 4559 | |
| 4560 | enum gj_BCallMethod { |
| 4561 | GJ_Keyword, |
| 4562 | GJ_Apply, |
| 4563 | GJ_Meth, |
| 4564 | GJ_Op, |
| 4565 | GJ_OpMod, |
| 4566 | GJ_LitChar, |
| 4567 | GJ_LitString, |
| 4568 | GJ_LitFloat, |
| 4569 | GJ_LitInt, |
| 4570 | GJ_Const, |
| 4571 | GJ_NegConst, |
| 4572 | GJ_Cast, |
| 4573 | GJ_Exception, |
| 4574 | |
| 4575 | GJ_NotImpl |
| 4576 | }; |
| 4577 | |
| 4578 | typedef Enum(gj_BCallMethod)enum gj_BCallMethod GJ_BCallMethod; |
| 4579 | |
| 4580 | struct gjBVal_info { |
| 4581 | FoamBValTag tag; |
| 4582 | GJ_BCallMethod method; |
| 4583 | AInt gjTag; |
| 4584 | String c1; |
| 4585 | String c2; |
| 4586 | }; |
| 4587 | |
| 4588 | typedef struct gjBVal_info *GJBValInfo; |
| 4589 | |
| 4590 | localstatic GJBValInfo gj0BCallBValInfo(FoamBValTag tag); |
| 4591 | |
| 4592 | localstatic JavaCode gj0BCallKeyword (Foam foam); |
| 4593 | localstatic JavaCode gj0BCallApply (Foam foam); |
| 4594 | localstatic JavaCode gj0BCallMethod (Foam foam); |
| 4595 | localstatic JavaCode gj0BCallOp (Foam foam); |
| 4596 | localstatic JavaCode gj0BCallOpMod (Foam foam); |
| 4597 | localstatic JavaCode gj0BCallLitChar (Foam foam); |
| 4598 | localstatic JavaCode gj0BCallLitString(Foam foam); |
| 4599 | localstatic JavaCode gj0BCallLitFloat(Foam foam); |
| 4600 | localstatic JavaCode gj0BCallLitInt (Foam foam); |
| 4601 | localstatic JavaCode gj0BCallConst (Foam foam); |
| 4602 | localstatic JavaCode gj0BCallNegConst(Foam foam); |
| 4603 | localstatic JavaCode gj0BCallCast (Foam foam); |
| 4604 | localstatic JavaCode gj0BCallException(Foam foam); |
| 4605 | localstatic JavaCode gj0BCallNotImpl (Foam foam); |
| 4606 | |
| 4607 | struct gjBVal_info gjBValInfoTable[]; |
| 4608 | struct gjBVal_info gjBValNotImpl; |
| 4609 | |
| 4610 | localstatic JavaCode |
| 4611 | gj0BCall(Foam foam) |
| 4612 | { |
| 4613 | GJBValInfo inf; |
| 4614 | |
| 4615 | inf = gj0BCallBValInfo(foam->foamBCall.op); |
| 4616 | switch (inf->method) { |
| 4617 | case GJ_Keyword: |
| 4618 | return gj0BCallKeyword(foam); |
| 4619 | case GJ_Apply: |
| 4620 | return gj0BCallApply(foam); |
| 4621 | case GJ_Meth: |
| 4622 | return gj0BCallMethod(foam); |
| 4623 | case GJ_Op: |
| 4624 | return gj0BCallOp(foam); |
| 4625 | case GJ_OpMod: |
| 4626 | return gj0BCallOpMod(foam); |
| 4627 | case GJ_LitChar: |
| 4628 | return gj0BCallLitChar(foam); |
| 4629 | case GJ_LitString: |
| 4630 | return gj0BCallLitString(foam); |
| 4631 | case GJ_LitFloat: |
| 4632 | return gj0BCallLitFloat(foam); |
| 4633 | case GJ_LitInt: |
| 4634 | return gj0BCallLitInt(foam); |
| 4635 | case GJ_Const: |
| 4636 | return gj0BCallConst(foam); |
| 4637 | case GJ_NegConst: |
| 4638 | return gj0BCallNegConst(foam); |
| 4639 | case GJ_Cast: |
| 4640 | return gj0BCallCast(foam); |
| 4641 | case GJ_NotImpl: |
| 4642 | return gj0BCallNotImpl(foam); |
| 4643 | case GJ_Exception: |
| 4644 | return gj0BCallException(foam); |
| 4645 | default: |
| 4646 | return gj0Default(foam, strCopy("BCallType")); |
| 4647 | } |
| 4648 | } |
| 4649 | |
| 4650 | localstatic JavaCode |
| 4651 | gj0BCallKeyword(Foam foam) |
| 4652 | { |
| 4653 | GJBValInfo inf; |
| 4654 | |
| 4655 | inf = gj0BCallBValInfo(foam->foamBCall.op); |
| 4656 | return jcKeyword(symInternConst(inf->c1)symProbe(inf->c1, 1)); |
| 4657 | } |
| 4658 | |
| 4659 | localstatic JavaCode |
| 4660 | gj0BCallApply(Foam foam) |
| 4661 | { |
| 4662 | JavaCodeList args; |
| 4663 | JavaCode tgtClss; |
| 4664 | GJBValInfo inf; |
| 4665 | CString p; |
| 4666 | |
| 4667 | inf = gj0BCallBValInfo(foam->foamBCall.op); |
| 4668 | args = gj0GenList(foam->foamBCall.argv, foamArgc(foam)((foam)->hdr.argc)-1); |
| 4669 | |
| 4670 | tgtClss = jcImportedIdFrString(inf->c1); |
| 4671 | |
| 4672 | return jcApplyMethod(tgtClss, jcId(strCopy(inf->c2)), args); |
| 4673 | } |
| 4674 | |
| 4675 | localstatic JavaCode |
| 4676 | gj0BCallMethod(Foam foam) |
| 4677 | { |
| 4678 | JavaCodeList args; |
| 4679 | JavaCode obj; |
| 4680 | GJBValInfo inf; |
| 4681 | |
| 4682 | inf = gj0BCallBValInfo(foam->foamBCall.op); |
| 4683 | args = gj0GenList(foam->foamBCall.argv, foamArgc(foam)((foam)->hdr.argc)-1); |
| 4684 | obj = car(args)((args)->first); |
| 4685 | args = listFreeCons(JavaCode)(JavaCode_listPointer->FreeCons)(args); |
| 4686 | |
| 4687 | return jcApplyMethod(obj, jcId(strCopy(inf->c1)), args); |
| 4688 | } |
| 4689 | |
| 4690 | localstatic JavaCode |
| 4691 | gj0BCallOp(Foam foam) |
| 4692 | { |
| 4693 | JavaCodeList args; |
| 4694 | JavaCode r; |
| 4695 | GJBValInfo inf; |
| 4696 | |
| 4697 | inf = gj0BCallBValInfo(foam->foamBCall.op); |
| 4698 | args = gj0GenList(foam->foamBCall.argv, foamArgc(foam)((foam)->hdr.argc)-1); |
| 4699 | |
| 4700 | if (inf->c1 != 0) { |
| 4701 | JavaCode arg2 = jcLiteralIntegerFrString(strCopy(inf->c1)); |
| 4702 | args = listNConcat(JavaCode)(JavaCode_listPointer->NConcat)(args, listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(arg2)); |
| 4703 | } |
| 4704 | r = jcOp(inf->gjTag, args); |
| 4705 | |
| 4706 | return r; |
| 4707 | } |
| 4708 | |
| 4709 | localstatic JavaCode |
| 4710 | gj0BCallOpMod(Foam foam) |
| 4711 | { |
| 4712 | JavaCodeList args; |
| 4713 | JavaCode r, extraArg; |
| 4714 | GJBValInfo inf; |
| 4715 | |
| 4716 | inf = gj0BCallBValInfo(foam->foamBCall.op); |
| 4717 | args = gj0GenList(foam->foamBCall.argv, foamArgc(foam)((foam)->hdr.argc)-1); |
| 4718 | assert(listLength(JavaCode)(args) == 3)do { if (!((JavaCode_listPointer->_Length)(args) == 3)) _do_assert (("listLength(JavaCode)(args) == 3"),"java/genjava.c",4718); } while (0); |
| 4719 | |
| 4720 | extraArg = listElt(JavaCode)(JavaCode_listPointer->Elt)(args, 2); |
| 4721 | args = listList(JavaCode)(JavaCode_listPointer->List)(2, car(args)((args)->first), car(cdr(args))((((args)->rest))->first)); |
| 4722 | r = jcOp(inf->gjTag, args); |
| 4723 | |
| 4724 | return jcBinOp(JCO_OP_Modulo, r, extraArg); |
| 4725 | } |
| 4726 | |
| 4727 | localstatic JavaCode |
| 4728 | gj0BCallLitChar(Foam foam) |
| 4729 | { |
| 4730 | GJBValInfo inf; |
| 4731 | |
| 4732 | inf = gj0BCallBValInfo(foam->foamBCall.op); |
| 4733 | |
| 4734 | return jcLiteralChar(strCopy(inf->c1)); |
| 4735 | } |
| 4736 | |
| 4737 | localstatic JavaCode |
| 4738 | gj0BCallLitString(Foam foam) |
| 4739 | { |
| 4740 | GJBValInfo inf; |
| 4741 | |
| 4742 | inf = gj0BCallBValInfo(foam->foamBCall.op); |
| 4743 | |
| 4744 | return jcLiteralString(strCopy(inf->c1)); |
| 4745 | } |
| 4746 | |
| 4747 | localstatic JavaCode |
| 4748 | gj0BCallLitFloat(Foam foam) |
| 4749 | { |
| 4750 | GJBValInfo inf; |
| 4751 | |
| 4752 | inf = gj0BCallBValInfo(foam->foamBCall.op); |
| 4753 | |
| 4754 | return jcLiteralFloatFrString(strCopy(inf->c1)); |
| 4755 | } |
| 4756 | |
| 4757 | localstatic JavaCode |
| 4758 | gj0BCallLitInt(Foam foam) |
| 4759 | { |
| 4760 | GJBValInfo inf; |
| 4761 | |
| 4762 | inf = gj0BCallBValInfo(foam->foamBCall.op); |
| 4763 | |
| 4764 | return jcLiteralIntegerFrString(strCopy(inf->c1)); |
| 4765 | } |
| 4766 | |
| 4767 | localstatic JavaCode |
| 4768 | gj0BCallConst(Foam foam) |
| 4769 | { |
| 4770 | GJBValInfo inf; |
| 4771 | |
| 4772 | inf = gj0BCallBValInfo(foam->foamBCall.op); |
| 4773 | return jcMemRef(jcId(strCopy(inf->c1)), |
| 4774 | jcId(strCopy(inf->c2))); |
| 4775 | } |
| 4776 | |
| 4777 | localstatic JavaCode |
| 4778 | gj0BCallNegConst(Foam foam) |
| 4779 | { |
| 4780 | GJBValInfo inf; |
| 4781 | JavaCode c; |
| 4782 | inf = gj0BCallBValInfo(foam->foamBCall.op); |
| 4783 | c = jcMemRef(jcId(strCopy(inf->c1)), |
| 4784 | jcId(strCopy(inf->c2))); |
| 4785 | return jcOp(JCO_OP_Negate, listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(c)); |
| 4786 | } |
| 4787 | |
| 4788 | localstatic JavaCode |
| 4789 | gj0BCallCast(Foam foam) |
| 4790 | { |
| 4791 | GJBValInfo inf; |
| 4792 | inf = gj0BCallBValInfo(foam->foamBCall.op); |
| 4793 | return jcCast(jcId(strCopy(inf->c1)), gj0Gen(foam->foamBCall.argv[0])); |
| 4794 | } |
| 4795 | |
| 4796 | localstatic JavaCode |
| 4797 | gj0BCallException(Foam foam) |
| 4798 | { |
| 4799 | JavaCodeList args; |
| 4800 | GJBValInfo inf; |
| 4801 | JavaCode jc; |
| 4802 | |
| 4803 | inf = gj0BCallBValInfo(foam->foamBCall.op); |
| 4804 | args = gj0GenList(foam->foamBCall.argv, foamArgc(foam)((foam)->hdr.argc)-1); |
| 4805 | jc = jcThrow(jcConstruct(jcImportedId(strCopy(inf->c1), |
| 4806 | strCopy(inf->c2)), |
| 4807 | args)); |
| 4808 | return jc; |
| 4809 | } |
| 4810 | |
| 4811 | localstatic JavaCode |
| 4812 | gj0BCallNotImpl(Foam foam) |
| 4813 | { |
| 4814 | return gj0Default(foam, strCopy("")); |
| 4815 | } |
| 4816 | |
| 4817 | |
| 4818 | struct gjBVal_info gjBValNotImpl = { 0, GJ_NotImpl }; |
| 4819 | struct gjBVal_info gjBValInfoTable[] = { |
| 4820 | {FOAM_BVal_BoolFalse, GJ_Keyword, 0, "true"}, |
| 4821 | {FOAM_BVal_BoolTrue, GJ_Keyword, 0, "false"}, |
| 4822 | |
| 4823 | {FOAM_BVal_BoolNot, GJ_Op, JCO_OP_Not }, |
| 4824 | {FOAM_BVal_BoolAnd, GJ_Op, JCO_OP_And }, |
| 4825 | {FOAM_BVal_BoolOr, GJ_Op, JCO_OP_Or }, |
| 4826 | {FOAM_BVal_BoolEQ, GJ_Op, JCO_OP_Equals }, |
| 4827 | {FOAM_BVal_BoolNE, GJ_Op, JCO_OP_NEquals }, |
| 4828 | |
| 4829 | {FOAM_BVal_CharSpace, GJ_LitChar, 0, " "}, |
| 4830 | {FOAM_BVal_CharNewline, GJ_LitChar, 0, "\n"}, |
| 4831 | {FOAM_BVal_CharTab, GJ_LitChar, 0, "\t"}, |
| 4832 | |
| 4833 | {FOAM_BVal_CharMin, GJ_Const, 0, "Character", "MIN_VALUE" }, |
| 4834 | {FOAM_BVal_CharMax, GJ_Const, 0, "Character", "MAX_VALUE" }, |
| 4835 | |
| 4836 | {FOAM_BVal_CharIsDigit, GJ_Apply, 0, "Character", "isDigit"}, |
| 4837 | {FOAM_BVal_CharIsLetter, GJ_Apply, 0, "Character", "isLetter"}, |
| 4838 | |
| 4839 | {FOAM_BVal_CharEQ, GJ_Op, JCO_OP_Equals}, |
| 4840 | {FOAM_BVal_CharNE, GJ_Op, JCO_OP_NEquals}, |
| 4841 | {FOAM_BVal_CharLT, GJ_Op, JCO_OP_LT}, |
| 4842 | {FOAM_BVal_CharLE, GJ_Op, JCO_OP_LE}, |
| 4843 | |
| 4844 | {FOAM_BVal_CharLower, GJ_Apply, 0, "Character", "toLowerCase"}, |
| 4845 | {FOAM_BVal_CharUpper, GJ_Apply, 0, "Character", "toUpperCase"}, |
| 4846 | {FOAM_BVal_CharOrd, GJ_Cast, 0, "int"}, |
| 4847 | {FOAM_BVal_CharNum, GJ_Cast, 0, "char"}, |
| 4848 | |
| 4849 | {FOAM_BVal_SFlo0, GJ_LitFloat, 0,"0.0f"}, |
| 4850 | {FOAM_BVal_SFlo1, GJ_LitFloat, 0,"0.0f"}, |
| 4851 | {FOAM_BVal_SFloMin, GJ_NegConst, 0, "Float", "MAX_VALUE"}, |
| 4852 | {FOAM_BVal_SFloMax, GJ_Const, 0, "Float", "MAX_VALUE"}, |
| 4853 | {FOAM_BVal_SFloEpsilon, GJ_Const, 0,"Float", "MIN_VALUE"}, |
| 4854 | {FOAM_BVal_SFloIsZero, GJ_Op, JCO_OP_Equals, "0.0"}, |
| 4855 | {FOAM_BVal_SFloIsNeg, GJ_Op, JCO_OP_LT, "0.0"}, |
| 4856 | {FOAM_BVal_SFloIsPos, GJ_Op, JCO_OP_GT, "0.0"}, |
| 4857 | {FOAM_BVal_SFloEQ, GJ_Op, JCO_OP_Equals}, |
| 4858 | {FOAM_BVal_SFloNE, GJ_Op, JCO_OP_NEquals}, |
| 4859 | {FOAM_BVal_SFloLT, GJ_Op, JCO_OP_LT}, |
| 4860 | {FOAM_BVal_SFloLE, GJ_Op, JCO_OP_LE}, |
| 4861 | {FOAM_BVal_SFloNegate, GJ_Op, JCO_OP_Negate}, |
| 4862 | {FOAM_BVal_SFloPrev, GJ_Apply, 0, "foamj.Math", "sfloPrev"}, |
| 4863 | {FOAM_BVal_SFloNext, GJ_Apply, 0, "foamj.Math", "sfloNext"}, |
| 4864 | {FOAM_BVal_SFloPlus, GJ_Op, JCO_OP_Plus}, |
| 4865 | {FOAM_BVal_SFloMinus, GJ_Op, JCO_OP_Minus}, |
| 4866 | {FOAM_BVal_SFloTimes, GJ_Op, JCO_OP_Times}, |
| 4867 | {FOAM_BVal_SFloTimesPlus, GJ_Op, JCO_OP_TimesPlus}, |
| 4868 | {FOAM_BVal_SFloDivide, GJ_Op, JCO_OP_Divide}, |
| 4869 | {FOAM_BVal_SFloRPlus, GJ_Apply, 0, "foamj.Math", "sfloRPlus"}, |
| 4870 | {FOAM_BVal_SFloRMinus, GJ_Apply, 0, "foamj.Math", "sfloRMinus"}, |
| 4871 | {FOAM_BVal_SFloRTimes, GJ_Apply, 0, "foamj.Math", "sfloRTimes"}, |
| 4872 | {FOAM_BVal_SFloRTimesPlus,GJ_Apply, 0, "foamj.Math", "sfloRTimesPlus"}, |
| 4873 | {FOAM_BVal_SFloRDivide, GJ_Apply, 0, "foamj.Math", "sfloRDivide"}, |
| 4874 | {FOAM_BVal_SFloDissemble, GJ_Apply, 0, "foamj.Math", "sfloDissemble"}, |
| 4875 | {FOAM_BVal_SFloAssemble, GJ_Apply, 0, "foamj.Math", "sfloAssemble"}, |
| 4876 | |
| 4877 | {FOAM_BVal_DFlo0, GJ_LitFloat, 0,"0.0f"}, |
| 4878 | {FOAM_BVal_DFlo1, GJ_LitFloat, 0,"0.0f"}, |
| 4879 | {FOAM_BVal_DFloMin, GJ_NegConst, 0, "Double", "MAX_VALUE"}, |
| 4880 | {FOAM_BVal_DFloMax, GJ_Const, 0, "Double", "MAX_VALUE"}, |
| 4881 | {FOAM_BVal_DFloEpsilon, GJ_Const, 0, "Double", "MIN_VALUE"}, |
| 4882 | {FOAM_BVal_DFloIsZero, GJ_Op, JCO_OP_Equals, "0.0"}, |
| 4883 | {FOAM_BVal_DFloIsNeg, GJ_Op, JCO_OP_LT, "0.0"}, |
| 4884 | {FOAM_BVal_DFloIsPos, GJ_Op, JCO_OP_GT, "0.0"}, |
| 4885 | {FOAM_BVal_DFloEQ, GJ_Op, JCO_OP_Equals}, |
| 4886 | {FOAM_BVal_DFloNE, GJ_Op, JCO_OP_NEquals}, |
| 4887 | {FOAM_BVal_DFloLT, GJ_Op, JCO_OP_LT}, |
| 4888 | {FOAM_BVal_DFloLE, GJ_Op, JCO_OP_LE}, |
| 4889 | {FOAM_BVal_DFloNegate, GJ_Op, JCO_OP_Negate}, |
| 4890 | {FOAM_BVal_DFloPrev, GJ_Apply, 0, "foamj.Math", "dfloPrev"}, |
| 4891 | {FOAM_BVal_DFloNext, GJ_Apply, 0, "foamj.Math", "dfloNext"}, |
| 4892 | {FOAM_BVal_DFloPlus, GJ_Op, JCO_OP_Plus}, |
| 4893 | {FOAM_BVal_DFloMinus, GJ_Op, JCO_OP_Minus}, |
| 4894 | {FOAM_BVal_DFloTimes, GJ_Op, JCO_OP_Times}, |
| 4895 | {FOAM_BVal_DFloTimesPlus, GJ_Op, JCO_OP_TimesPlus}, |
| 4896 | {FOAM_BVal_DFloDivide, GJ_Op, JCO_OP_Divide}, |
| 4897 | {FOAM_BVal_DFloRPlus, GJ_Apply, 0, "foamj.Math", "dfloRPlus"}, |
| 4898 | {FOAM_BVal_DFloRMinus, GJ_Apply, 0, "foamj.Math", "dfloRMinus"}, |
| 4899 | {FOAM_BVal_DFloRTimes, GJ_Apply, 0, "foamj.Math", "dfloRTimes"}, |
| 4900 | {FOAM_BVal_DFloRTimesPlus,GJ_Apply, 0, "foamj.Math", "dfloRTimesPlus"}, |
| 4901 | {FOAM_BVal_DFloRDivide, GJ_Apply, 0, "foamj.Math", "dfloRDivide"}, |
| 4902 | {FOAM_BVal_DFloDissemble, GJ_Apply, 0, "foamj.Math", "dfloDissemble"}, |
| 4903 | {FOAM_BVal_DFloAssemble, GJ_Apply, 0, "foamj.Math", "dfloAssemble"}, |
| 4904 | |
| 4905 | {FOAM_BVal_Byte0, GJ_LitInt, 0, "(byte) 0" }, |
| 4906 | {FOAM_BVal_Byte1, GJ_LitInt, 0, "(byte) 1" }, |
| 4907 | {FOAM_BVal_ByteMin, GJ_LitInt, 0, "(byte) 0"}, |
| 4908 | {FOAM_BVal_ByteMax, GJ_Const, 0, "Byte", "MAX_VALUE"}, |
| 4909 | |
| 4910 | {FOAM_BVal_HInt0, GJ_LitInt, 0,"(short) 0" }, |
| 4911 | {FOAM_BVal_HInt1, GJ_LitInt, 0,"(short) 1" }, |
| 4912 | {FOAM_BVal_HIntMin, GJ_Const , 0,"Short", "MIN_VALUE"}, |
| 4913 | {FOAM_BVal_HIntMax, GJ_Const, 0,"Short", "MAX_VALUE"}, |
| 4914 | |
| 4915 | {FOAM_BVal_SInt0, GJ_LitInt, 0,"0" }, |
| 4916 | {FOAM_BVal_SInt1, GJ_LitInt, 0,"1" }, |
| 4917 | {FOAM_BVal_SIntMin, GJ_LitInt, 0,"0"}, |
| 4918 | {FOAM_BVal_SIntMax, GJ_Const, 0,"Integer", "MAX_VALUE"}, |
| 4919 | |
| 4920 | {FOAM_BVal_SIntIsZero, GJ_Op, JCO_OP_Equals, "0"}, |
| 4921 | {FOAM_BVal_SIntIsNeg, GJ_Op, JCO_OP_LT, "0"}, |
| 4922 | {FOAM_BVal_SIntIsPos, GJ_Op, JCO_OP_GT, "0"}, |
| 4923 | |
| 4924 | {FOAM_BVal_SIntIsEven, GJ_Apply, 0, "foamj.Math", "isEven"}, |
| 4925 | {FOAM_BVal_SIntIsOdd, GJ_Apply, 0, "foamj.Math", "isOdd"}, |
| 4926 | |
| 4927 | {FOAM_BVal_SIntEQ, GJ_Op, JCO_OP_Equals}, |
| 4928 | {FOAM_BVal_SIntNE, GJ_Op, JCO_OP_NEquals}, |
| 4929 | {FOAM_BVal_SIntLT, GJ_Op, JCO_OP_LT}, |
| 4930 | {FOAM_BVal_SIntLE, GJ_Op, JCO_OP_LE}, |
| 4931 | {FOAM_BVal_SIntNegate, GJ_Op, JCO_OP_Negate}, |
| 4932 | {FOAM_BVal_SIntPrev, GJ_Op, JCO_OP_Minus, "1"}, |
| 4933 | {FOAM_BVal_SIntNext, GJ_Op, JCO_OP_Plus, "1"}, |
| 4934 | {FOAM_BVal_SIntPlus, GJ_Op, JCO_OP_Plus}, |
| 4935 | {FOAM_BVal_SIntMinus, GJ_Op, JCO_OP_Minus}, |
| 4936 | {FOAM_BVal_SIntTimes, GJ_Op, JCO_OP_Times}, |
| 4937 | {FOAM_BVal_SIntTimesPlus,GJ_Op, JCO_OP_TimesPlus}, |
| 4938 | {FOAM_BVal_SIntMod, GJ_Op, JCO_OP_Modulo}, |
| 4939 | {FOAM_BVal_SIntQuo, GJ_Op, JCO_OP_Divide}, |
| 4940 | {FOAM_BVal_SIntRem, GJ_Op, JCO_OP_Modulo}, |
| 4941 | {FOAM_BVal_SIntDivide, GJ_Apply, 0, "foamj.Math", "divide" }, |
| 4942 | |
| 4943 | {FOAM_BVal_SIntGcd, GJ_Apply, 0,"foamj.Math", "gcd"}, |
| 4944 | {FOAM_BVal_SIntPlusMod, GJ_OpMod, JCO_OP_Plus}, |
| 4945 | {FOAM_BVal_SIntMinusMod, GJ_OpMod, JCO_OP_Minus}, |
| 4946 | {FOAM_BVal_SIntTimesMod, GJ_OpMod, JCO_OP_Times}, |
| 4947 | {FOAM_BVal_SIntTimesModInv,GJ_Apply, 0,"foamj.Math","timesModInv"}, |
| 4948 | {FOAM_BVal_SIntLength, GJ_Apply, 0,"foamj.Math", "length"}, |
| 4949 | {FOAM_BVal_SIntShiftUp, GJ_Op, JCO_OP_ShiftUp}, |
| 4950 | {FOAM_BVal_SIntShiftDn, GJ_Op, JCO_OP_ShiftDn}, |
| 4951 | {FOAM_BVal_SIntBit, GJ_Apply, 0,"foamj.Math", "bit"}, |
| 4952 | |
| 4953 | {FOAM_BVal_SIntNot, GJ_Op, JCO_OP_XOr, "0"}, |
| 4954 | {FOAM_BVal_SIntAnd, GJ_Op, JCO_OP_And}, |
| 4955 | {FOAM_BVal_SIntOr, GJ_Op, JCO_OP_Or}, |
| 4956 | {FOAM_BVal_SIntXOr, GJ_Op, JCO_OP_XOr}, |
| 4957 | {FOAM_BVal_SIntHashCombine,GJ_Apply,0, "foamj.Math", "hashCombine"}, |
| 4958 | |
| 4959 | {FOAM_BVal_WordTimesDouble, GJ_Apply, 0,"foamj.Math", "wordTimesDouble"}, |
| 4960 | {FOAM_BVal_WordDivideDouble,GJ_Apply, 0,"foamj.Math", "wordDivideDouble"}, |
| 4961 | {FOAM_BVal_WordPlusStep, GJ_Apply, 0,"foamj.Math", "wordPlusStep"}, |
| 4962 | {FOAM_BVal_WordTimesStep, GJ_Apply, 0,"foamj.Math", "wordTimesStep"}, |
| 4963 | |
| 4964 | {FOAM_BVal_BInt0, GJ_Const, 0, "BigInteger", "ZERO"}, |
| 4965 | {FOAM_BVal_BInt1, GJ_Const, 0, "BigInteger", "ONE"}, |
| 4966 | {FOAM_BVal_BIntIsZero, GJ_Apply, 0, "foamj.Math", "isZero"}, |
| 4967 | {FOAM_BVal_BIntIsNeg, GJ_Apply, 0, "foamj.Math", "isNeg"}, |
| 4968 | {FOAM_BVal_BIntIsPos, GJ_Apply, 0, "foamj.Math", "isPos"}, |
| 4969 | {FOAM_BVal_BIntIsEven, GJ_Apply, 0, "foamj.Math", "isEven"}, |
| 4970 | {FOAM_BVal_BIntIsOdd, GJ_Apply, 0, "foamj.Math", "isOdd"}, |
| 4971 | {FOAM_BVal_BIntIsSingle, GJ_Apply, 0, "foamj.Math", "isSingle"}, |
| 4972 | {FOAM_BVal_BIntEQ, GJ_Apply, 0, "foamj.Math", "eq"}, |
| 4973 | {FOAM_BVal_BIntNE, GJ_Apply, 0, "foamj.Math", "ne"}, |
| 4974 | {FOAM_BVal_BIntLT, GJ_Apply, 0, "foamj.Math", "lt"}, |
| 4975 | {FOAM_BVal_BIntLE, GJ_Apply, 0, "foamj.Math", "le"}, |
| 4976 | {FOAM_BVal_BIntNegate, GJ_Meth, 0, "negate"}, |
| 4977 | {FOAM_BVal_BIntPrev, GJ_Apply, 0, "foamj.Math", "prev"}, |
| 4978 | {FOAM_BVal_BIntNext, GJ_Apply, 0, "foamj.Math", "next"}, |
| 4979 | {FOAM_BVal_BIntPlus, GJ_Apply, 0, "foamj.Math", "plus"}, |
| 4980 | {FOAM_BVal_BIntMinus, GJ_Apply, 0, "foamj.Math", "minus"}, |
| 4981 | {FOAM_BVal_BIntTimes, GJ_Apply, 0, "foamj.Math", "times"}, |
| 4982 | {FOAM_BVal_BIntTimesPlus,GJ_Apply, 0, "foamj.Math", "timesPlus"}, |
| 4983 | {FOAM_BVal_BIntMod, GJ_Apply, 0, "foamj.Math", "mod"}, |
| 4984 | {FOAM_BVal_BIntQuo, GJ_Apply, 0, "foamj.Math", "quo"}, |
| 4985 | {FOAM_BVal_BIntRem, GJ_Apply, 0, "foamj.Math", "rem"}, |
| 4986 | {FOAM_BVal_BIntDivide, GJ_Apply, 0, "foamj.Math", "divide"}, |
| 4987 | {FOAM_BVal_BIntGcd, GJ_Meth, 0, "gcd"}, |
| 4988 | {FOAM_BVal_BIntSIPower, GJ_Apply, 0, "foamj.Math", "sIPower"}, |
| 4989 | {FOAM_BVal_BIntBIPower, GJ_Apply, 0, "foamj.Math", "bIPower"}, |
| 4990 | {FOAM_BVal_BIntPowerMod, GJ_Apply, 0, "foamj.Math", "powerMod"}, |
| 4991 | {FOAM_BVal_BIntLength, GJ_Meth, 0, "bitCount"}, |
| 4992 | {FOAM_BVal_BIntShiftUp, GJ_Apply, 0, "foamj.Math", "shiftUp"}, |
| 4993 | {FOAM_BVal_BIntShiftDn, GJ_Apply, 0, "foamj.Math", "shiftDn"}, |
| 4994 | {FOAM_BVal_BIntShiftRem, GJ_Apply, 0, "foamj.Math", "shiftRem"}, |
| 4995 | {FOAM_BVal_BIntBit, GJ_Meth, 0, "testBit"}, |
| 4996 | |
| 4997 | {FOAM_BVal_PtrNil, GJ_Keyword, 0, "null"}, |
| 4998 | {FOAM_BVal_PtrIsNil, GJ_Op, JCO_OP_Equals, "null"}, |
| 4999 | {FOAM_BVal_PtrMagicEQ, GJ_Op, JCO_OP_Equals}, |
| 5000 | {FOAM_BVal_PtrEQ, GJ_Op, JCO_OP_Equals}, |
| 5001 | {FOAM_BVal_PtrNE, GJ_Op, JCO_OP_NEquals}, |
| 5002 | |
| 5003 | |
| 5004 | {FOAM_BVal_FormatSFlo, GJ_Apply, 0, "foamj.Math", "formatSFlo"}, |
| 5005 | {FOAM_BVal_FormatDFlo, GJ_Apply, 0, "foamj.Math", "formatDFlo"}, |
| 5006 | {FOAM_BVal_FormatSInt, GJ_Apply, 0, "foamj.Math", "formatSInt"}, |
| 5007 | {FOAM_BVal_FormatBInt, GJ_Apply, 0, "foamj.Math", "formatBInt"}, |
| 5008 | |
| 5009 | {FOAM_BVal_ScanSFlo, GJ_Apply, 0, "foamj.Math", "scanSFlo"}, |
| 5010 | {FOAM_BVal_ScanDFlo, GJ_Apply, 0, "foamj.Math", "scanDFlo"}, |
| 5011 | {FOAM_BVal_ScanSInt, GJ_Apply, 0, "foamj.Math", "scanSInt"}, |
| 5012 | {FOAM_BVal_ScanBInt, GJ_Apply, 0, "foamj.Math", "scanBInt"}, |
| 5013 | |
| 5014 | {FOAM_BVal_SFloToDFlo, GJ_Cast, 0, "double"}, |
| 5015 | {FOAM_BVal_DFloToSFlo, GJ_Cast, 0, "float"}, |
| 5016 | {FOAM_BVal_ByteToSInt, GJ_Cast, 0, "int"}, |
| 5017 | {FOAM_BVal_SIntToByte, GJ_Cast, 0, "byte"}, |
| 5018 | {FOAM_BVal_HIntToSInt, GJ_Cast, 0, "int"}, |
| 5019 | {FOAM_BVal_SIntToHInt, GJ_Cast, 0, "short"}, |
| 5020 | {FOAM_BVal_SIntToBInt, GJ_Apply, 0, "java.math.BigInteger", "valueOf"}, |
| 5021 | {FOAM_BVal_BIntToSInt, GJ_Meth, 0,"intValue"}, |
| 5022 | {FOAM_BVal_SIntToSFlo, GJ_Cast, 0, "float"}, |
| 5023 | {FOAM_BVal_SIntToDFlo, GJ_Cast, 0, "double"}, |
| 5024 | {FOAM_BVal_BIntToSFlo, GJ_Meth, 0,"floatValue"}, |
| 5025 | {FOAM_BVal_BIntToDFlo, GJ_Meth, 0, "doubleValue"}, |
| 5026 | {FOAM_BVal_PtrToSInt, GJ_Apply, 0, "foamj.Foam", "ptrToSInt"}, |
| 5027 | {FOAM_BVal_SIntToPtr, GJ_Apply, 0, "foamj.Foam", "sintToPtr"}, |
| 5028 | |
| 5029 | {FOAM_BVal_ArrToSFlo, GJ_Apply, 0, "foamj.Foam", "arrToSFlo"}, |
| 5030 | {FOAM_BVal_ArrToDFlo, GJ_Apply, 0, "foamj.Foam", "arrToDFlo"}, |
| 5031 | {FOAM_BVal_ArrToSInt, GJ_Apply, 0, "foamj.Foam", "arrToSInt"}, |
| 5032 | {FOAM_BVal_ArrToBInt, GJ_Apply, 0, "foamj.Foam", "arrToBInt"}, |
| 5033 | |
| 5034 | {FOAM_BVal_PlatformRTE, GJ_Const, 0, "foamj.Foam", "RTE"}, |
| 5035 | {FOAM_BVal_PlatformOS, GJ_Const, 0, "foamj.Foam", "PlatformOS"}, |
| 5036 | |
| 5037 | {FOAM_BVal_Halt, GJ_Exception, 0, "foamj", "FoamException"}, |
| 5038 | |
| 5039 | {FOAM_BVal_RoundZero, GJ_Const, 0,"foamj.Math", "ROUND_ZERO"}, |
| 5040 | {FOAM_BVal_RoundNearest, GJ_Const, 0,"foamj.Math", "ROUND_NEAREST"}, |
| 5041 | {FOAM_BVal_RoundUp, GJ_Const, 0,"foamj.Math", "ROUND_UP"}, |
| 5042 | {FOAM_BVal_RoundDown, GJ_Const, 0,"foamj.Math", "ROUND_DOWN"}, |
| 5043 | {FOAM_BVal_RoundDontCare,GJ_Const, 0,"foamj.Math", "ROUND_DONTCARE"}, |
| 5044 | |
| 5045 | {FOAM_BVal_SFloTruncate, GJ_Apply, 0, "foamj.Math", "sfloTruncate"}, |
| 5046 | {FOAM_BVal_SFloFraction, GJ_Apply, 0,"foamj.Math", "sfloFraction"}, |
| 5047 | {FOAM_BVal_SFloRound, GJ_Apply, 0,"foamj.Math", "sfloRound"}, |
| 5048 | |
| 5049 | {FOAM_BVal_DFloTruncate, GJ_Apply, 0,"foamj.Math", "dfloTruncate"}, |
| 5050 | {FOAM_BVal_DFloFraction, GJ_Apply, 0,"foamj.Math", "dfloFraction"}, |
| 5051 | {FOAM_BVal_DFloRound, GJ_Apply, 0,"foamj.Math", "dfloRound"}, |
| 5052 | |
| 5053 | }; |
| 5054 | #if 0 |
| 5055 | {FOAM_BVal_StoForceGC, GJB_FCall, 0,"fiStoForceGC", 0}, |
| 5056 | {FOAM_BVal_StoInHeap, GJB_FCall, 0,"fiStoInHeap", 0}, |
| 5057 | {FOAM_BVal_StoIsWritable, GJB_FCall, 0,"fiStoIsWritable", 0}, |
| 5058 | {FOAM_BVal_StoMarkObject, GJB_FCall, 0,"fiStoMarkObject", 0}, |
| 5059 | {FOAM_BVal_StoRecode, GJB_FCall, 0,"fiStoRecode", 0}, |
| 5060 | {FOAM_BVal_StoNewObject, GJB_FCall, 0,"fiStoNewObject", 0}, |
| 5061 | {FOAM_BVal_StoATracer, GJB_FCall, 0,"fiStoATracer", 0}, |
| 5062 | {FOAM_BVal_StoCTracer, GJB_FCall, 0,"fiStoCTracer", 0}, |
| 5063 | {FOAM_BVal_StoShow, GJB_FCall, 0,"fiStoShow", 0}, |
| 5064 | {FOAM_BVal_StoShowArgs, GJB_FCall, 0,"fiStoShowArgs", 0}, |
| 5065 | |
| 5066 | {FOAM_BVal_TypeInt8, GJB_FCall, 0,"fiTypeInt8", 0}, |
| 5067 | {FOAM_BVal_TypeInt16, GJB_FCall, 0,"fiTypeInt16", 0}, |
| 5068 | {FOAM_BVal_TypeInt32, GJB_FCall, 0,"fiTypeInt32", 0}, |
| 5069 | {FOAM_BVal_TypeInt64, GJB_FCall, 0,"fiTypeInt64", 0}, |
| 5070 | {FOAM_BVal_TypeInt128, GJB_FCall, 0,"fiTypeInt128", 0}, |
| 5071 | |
| 5072 | {FOAM_BVal_TypeNil, GJB_FCall, 0,"fiTypeNil", 0}, |
| 5073 | {FOAM_BVal_TypeChar, GJB_FCall, 0,"fiTypeChar", 0}, |
| 5074 | {FOAM_BVal_TypeBool, GJB_FCall, 0,"fiTypeBool", 0}, |
| 5075 | {FOAM_BVal_TypeByte, GJB_FCall, 0,"fiTypeByte", 0}, |
| 5076 | {FOAM_BVal_TypeHInt, GJB_FCall, 0,"fiTypeHInt", 0}, |
| 5077 | {FOAM_BVal_TypeSInt, GJB_FCall, 0,"fiTypeSInt", 0}, |
| 5078 | {FOAM_BVal_TypeBInt, GJB_FCall, 0,"fiTypeBInt", 0}, |
| 5079 | {FOAM_BVal_TypeSFlo, GJB_FCall, 0,"fiTypeSFlo", 0}, |
| 5080 | {FOAM_BVal_TypeDFlo, GJB_FCall, 0,"fiTypeDFlo", 0}, |
| 5081 | {FOAM_BVal_TypeWord, GJB_FCall, 0,"fiTypeWord", 0}, |
| 5082 | {FOAM_BVal_TypeClos, GJB_FCall, 0,"fiTypeClos", 0}, |
| 5083 | {FOAM_BVal_TypePtr, GJB_FCall, 0,"fiTypePtr", 0}, |
| 5084 | {FOAM_BVal_TypeRec, GJB_FCall, 0,"fiTypeRec", 0}, |
| 5085 | {FOAM_BVal_TypeArr, GJB_FCall, 0,"fiTypeArr", 0}, |
| 5086 | {FOAM_BVal_TypeTR, GJB_FCall, 0,"fiTypeTR", 0}, |
| 5087 | {FOAM_BVal_RawRepSize, GJB_FCall, 0,"fiRawRepSize", 0}, |
| 5088 | {FOAM_BVal_SizeOfInt8, GJB_FCall, 0,"fiSizeOfInt8", 0}, |
| 5089 | {FOAM_BVal_SizeOfInt16, GJB_FCall, 0,"fiSizeOfInt16", 0}, |
| 5090 | {FOAM_BVal_SizeOfInt32, GJB_FCall, 0,"fiSizeOfInt32", 0}, |
| 5091 | {FOAM_BVal_SizeOfInt64, GJB_FCall, 0,"fiSizeOfInt64", 0}, |
| 5092 | {FOAM_BVal_SizeOfInt128, GJB_FCall, 0,"fiSizeOfInt128", 0}, |
| 5093 | |
| 5094 | {FOAM_BVal_SizeOfNil, GJB_FCall, 0,"fiSizeOfNil", 0}, |
| 5095 | {FOAM_BVal_SizeOfChar, GJB_FCall, 0,"fiSizeOfChar", 0}, |
| 5096 | {FOAM_BVal_SizeOfBool, GJB_FCall, 0,"fiSizeOfBool", 0}, |
| 5097 | {FOAM_BVal_SizeOfByte, GJB_FCall, 0,"fiSizeOfByte", 0}, |
| 5098 | {FOAM_BVal_SizeOfHInt, GJB_FCall, 0,"fiSizeOfHInt", 0}, |
| 5099 | {FOAM_BVal_SizeOfSInt, GJB_FCall, 0,"fiSizeOfSInt", 0}, |
| 5100 | {FOAM_BVal_SizeOfBInt, GJB_FCall, 0,"fiSizeOfBInt", 0}, |
| 5101 | {FOAM_BVal_SizeOfSFlo, GJB_FCall, 0,"fiSizeOfSFlo", 0}, |
| 5102 | {FOAM_BVal_SizeOfDFlo, GJB_FCall, 0,"fiSizeOfDFlo", 0}, |
| 5103 | {FOAM_BVal_SizeOfWord, GJB_FCall, 0,"fiSizeOfWord", 0}, |
| 5104 | {FOAM_BVal_SizeOfClos, GJB_FCall, 0,"fiSizeOfClos", 0}, |
| 5105 | {FOAM_BVal_SizeOfPtr, GJB_FCall, 0,"fiSizeOfPtr", 0}, |
| 5106 | {FOAM_BVal_SizeOfRec, GJB_FCall, 0,"fiSizeOfRec", 0}, |
| 5107 | {FOAM_BVal_SizeOfArr, GJB_FCall, 0,"fiSizeOfArr", 0}, |
| 5108 | {FOAM_BVal_SizeOfTR, GJB_FCall, 0,"fiSizeOfTR", 0}, |
| 5109 | |
| 5110 | {FOAM_BVal_ListNil, GJB_FCall, 0,"fiListNil", "fiLIST_NIL"}, |
| 5111 | {FOAM_BVal_ListEmptyP, GJB_FCall, 0,"fiListEmptyP", "fiLIST_EMPTYP"}, |
| 5112 | {FOAM_BVal_ListHead, GJB_FCall, 0,"fiListHead", "fiLIST_HEAD"}, |
| 5113 | {FOAM_BVal_ListTail, GJB_FCall, 0,"fiListTail", "fiLIST_TAIL"}, |
| 5114 | {FOAM_BVal_ListCons, GJB_FCall, 0,"fiListCons", 0}, |
| 5115 | {FOAM_BVal_NewExportTable, GJB_FCall, 0, "fiNewExportTable", 0}, |
| 5116 | {FOAM_BVal_AddToExportTable, GJB_FCall, 0, "fiAddToExportTable", 0}, |
| 5117 | {FOAM_BVal_FreeExportTable, GJB_FCall, 0, "fiFreeExportTable", 0}, |
| 5118 | #if EDIT_1_0_n1_AB1 |
| 5119 | /* This BVal must NEVER be seen by genc (or genjava) ... */ |
| 5120 | {FOAM_BVal_ssaPhi, GJB_FCall, 0, "fiNonExistentFunction", 0}, |
| 5121 | #endif |
| 5122 | }; |
| 5123 | #endif |
| 5124 | |
| 5125 | |
| 5126 | localstatic GJBValInfo |
| 5127 | gj0BCallBValInfo(FoamBValTag tag) |
| 5128 | { |
| 5129 | GJBValInfo inf = &gjBValInfoTable[tag]; |
| 5130 | if ((char*) inf >= ((char*) gjBValInfoTable) + sizeof(gjBValInfoTable)) |
| 5131 | return &gjBValNotImpl; |
| 5132 | assert(inf->tag == tag || (fprintf(stderr, "%s %s", foamBValStr(tag), foamBValStr(inf->tag)), 0))do { if (!(inf->tag == tag || (fprintf(stderr, "%s %s", (( foamBValInfoTable[(int)(tag)-(int)FOAM_BVAL_START]).str), ((foamBValInfoTable [(int)(inf->tag)-(int)FOAM_BVAL_START]).str)), 0))) _do_assert (("inf->tag == tag || (fprintf(stderr, \"%s %s\", foamBValStr(tag), foamBValStr(inf->tag)), 0)" ),"java/genjava.c",5132); } while (0); |
| 5133 | return inf; |
| 5134 | } |
| 5135 | |
| 5136 | |
| 5137 | /* |
| 5138 | * :: Names |
| 5139 | */ |
| 5140 | struct gjSpecCharId_info { |
| 5141 | unsigned char c; |
| 5142 | CString s; |
| 5143 | }; |
| 5144 | struct gjSpecCharId_info gjSpecCharIdTable[]; |
| 5145 | |
| 5146 | CString gjCharIds[CHAR_MAX127]; |
| 5147 | #define STDCHAR_MAX127 127 |
| 5148 | |
| 5149 | localstatic void |
| 5150 | gj0NameInit() |
| 5151 | { |
| 5152 | struct gjSpecCharId_info *p = &gjSpecCharIdTable[0]; |
| 5153 | |
| 5154 | while (p->c != '\0') { |
| 5155 | /* Identifiers must never contain UTF-8 encoded unicode |
| 5156 | * characters or extended ASCII. */ |
| 5157 | assert(p->c <= STDCHAR_MAX)do { if (!(p->c <= 127)) _do_assert(("p->c <= STDCHAR_MAX" ),"java/genjava.c",5157); } while (0); |
| 5158 | gjCharIds[p->c] = p->s; |
| 5159 | p++; |
| 5160 | } |
| 5161 | } |
| 5162 | |
| 5163 | localstatic String |
| 5164 | gj0JavaSigName(CString prefix, Foam fmt, int idx) |
| 5165 | { |
| 5166 | Foam decl = javaSigArgN(fmt, idx); |
| 5167 | assert(idx < javaSigArgc(fmt))do { if (!(idx < javaSigArgc(fmt))) _do_assert(("idx < javaSigArgc(fmt)" ),"java/genjava.c",5167); } while (0); |
| 5168 | return aStrPrintf("_%d_%s", idx, gj0NameFrString(decl->foamDecl.id)); |
| 5169 | } |
| 5170 | |
| 5171 | localstatic String |
| 5172 | gj0Name(CString prefix, Foam fmt, int idx) |
| 5173 | { |
| 5174 | Foam decl; |
| 5175 | Buffer b = bufNew(); |
| 5176 | |
| 5177 | assert(idx < foamArgc(fmt))do { if (!(idx < ((fmt)->hdr.argc))) _do_assert(("idx < foamArgc(fmt)" ),"java/genjava.c",5177); } while (0); |
| 5178 | decl = fmt->foamDDecl.argv[idx]; |
| 5179 | bufPrintf(b, "%s%d", prefix, idx); |
| 5180 | |
| 5181 | if (strlen(decl->foamDecl.id) > 0) { |
| 5182 | bufPutc(b, '_'); |
| 5183 | bufPuts(b, gj0NameFrString(decl->foamDecl.id)); |
| 5184 | } |
| 5185 | |
| 5186 | return bufLiberate(b); |
| 5187 | } |
| 5188 | |
| 5189 | |
| 5190 | localstatic String |
| 5191 | gj0NameFrString(String fmName) |
| 5192 | { |
| 5193 | Buffer buf = bufNew(); |
| 5194 | Bool flg; |
| 5195 | CString p; |
| 5196 | |
| 5197 | flg = false((int) 0); |
| 5198 | p = fmName; |
| 5199 | while (*p != 0) { |
| 5200 | CString repl; |
| 5201 | assert(*p >= 0 && *p < CHAR_MAX)do { if (!(*p >= 0 && *p < 127)) _do_assert(("*p >= 0 && *p < CHAR_MAX" ),"java/genjava.c",5201); } while (0); |
| 5202 | repl = gjCharIds[(unsigned char)*p]; |
| 5203 | if (repl != 0) |
| 5204 | flg = true1; |
| 5205 | p++; |
| 5206 | } |
| 5207 | if (!flg) |
| 5208 | return fmName; |
| 5209 | |
| 5210 | p = fmName; |
| 5211 | bufNeed(buf, strlen(fmName)); |
| 5212 | while (*p != 0) { |
| 5213 | CString repl; |
| 5214 | assert(*p >= 0 && *p < CHAR_MAX)do { if (!(*p >= 0 && *p < 127)) _do_assert(("*p >= 0 && *p < CHAR_MAX" ),"java/genjava.c",5214); } while (0); |
| 5215 | repl = gjCharIds[(unsigned char)*p]; |
| 5216 | if (!repl) { |
| 5217 | bufAdd1(buf, *p); |
| 5218 | } |
| 5219 | else { |
| 5220 | bufPuts(buf, repl); |
| 5221 | } |
| 5222 | p++; |
| 5223 | } |
| 5224 | bufAdd1(buf, '\0'); |
| 5225 | return bufLiberate(buf); |
| 5226 | } |
| 5227 | |
| 5228 | |
| 5229 | struct gjSpecCharId_info gjSpecCharIdTable[] = { |
| 5230 | {'!', "_BANG_"}, |
| 5231 | {'\"', "_QUOTE_"}, |
| 5232 | {'#', "_SHARP_"}, |
| 5233 | {'$', "_DOLLR_"}, |
| 5234 | {'%', "_PCENT_"}, |
| 5235 | {'&', "_AMPER_"}, |
| 5236 | {'\'', "_APOS_"}, |
| 5237 | {'(', "_OPAREN_"}, |
| 5238 | {')', "_CPAREN_"}, |
| 5239 | {'*', "_STAR_"}, |
| 5240 | {'+', "_PLUS_"}, |
| 5241 | {',', "_COMMA_"}, |
| 5242 | {'-', "_MINUS_"}, |
| 5243 | {'.', "_DOT_"}, |
| 5244 | {'/', "_SLASH_"}, |
| 5245 | {':', "_COLON_"}, |
| 5246 | {';', "_SEMI_"}, |
| 5247 | {'<', "_LT_"}, |
| 5248 | {'=', "_EQ_"}, |
| 5249 | {'>', "_GT_"}, |
| 5250 | {'?', "_QMARK_"}, |
| 5251 | {'@', "_AT_"}, |
| 5252 | {'[', "_OBRACK_"}, |
| 5253 | {'\\', "_BSLSH_"}, |
| 5254 | {']', "_CBRACK_"}, |
| 5255 | {'^', "_HAT_"}, |
| 5256 | {'_', "__"}, |
| 5257 | {'`', "_GRAVE_"}, |
| 5258 | {'{', "_OBRACE_"}, |
| 5259 | {'|', "_BAR_"}, |
| 5260 | {'}', "_CBRACE_"}, |
| 5261 | {'~', "_TILDE_"}, |
| 5262 | {0, 0} |
| 5263 | }; |
| 5264 |