FskErr fxLoadModuleJSBAtom(txMachine* the, FskFile fref, Atom* atom) { FskErr err = kFskErrNone; char buffer[sizeof(Atom)]; void* p = buffer; bailIfError(FskFileRead(fref, sizeof(Atom), p, NULL)); mxDecode4(p, atom->atomSize); mxDecode4(p, atom->atomType); bail: return err; }
void* fxMapAtom(void* p, Atom* atom) { mxDecode4(p, atom->atomSize); mxDecode4(p, atom->atomType); return p; }
static void fxDumpCode(txS1* path, txS1** symbols, txUsage* usages, txS1** hosts, txS1* codeBuffer, size_t codeSize) { txS1* p = codeBuffer; txS1* q = codeBuffer + codeSize; txS1 s1; txS2 s2; txS4 s4; txU1 u1; txU2 u2; double number; while (p < q) { u1 = *((txU1*)p); fprintf(stderr, "%8.8X: %d %s ", p - codeBuffer, u1, gxCodeNames[u1]); p++; switch (u1) { case XS_CODE_BRANCH_1: case XS_CODE_BRANCH_ELSE_1: case XS_CODE_BRANCH_IF_1: case XS_CODE_BRANCH_STATUS_1: case XS_CODE_CATCH_1: case XS_CODE_CODE_1: case XS_CODE_CODE_ARCHIVE_1: s1 = *p++; fprintf(stderr, "%8.8X", p + s1 - codeBuffer); break; case XS_CODE_BRANCH_2: case XS_CODE_BRANCH_ELSE_2: case XS_CODE_BRANCH_IF_2: case XS_CODE_BRANCH_STATUS_2: case XS_CODE_CATCH_2: case XS_CODE_CODE_2: case XS_CODE_CODE_ARCHIVE_2: mxDecode2(p, s2); fprintf(stderr, "%8.8X", p + s2 - codeBuffer); break; case XS_CODE_BRANCH_4: case XS_CODE_BRANCH_ELSE_4: case XS_CODE_BRANCH_IF_4: case XS_CODE_BRANCH_STATUS_4: case XS_CODE_CATCH_4: case XS_CODE_CODE_4: case XS_CODE_CODE_ARCHIVE_4: mxDecode4(p, s4); fprintf(stderr, "%8.8X", p + s4 - codeBuffer); break; case XS_CODE_ARROW: case XS_CODE_FILE: case XS_CODE_FUNCTION: case XS_CODE_GENERATOR: case XS_CODE_METHOD: case XS_CODE_NAME: mxDecode2(p, s2); if (s2 == XS_NO_ID) fprintf(stderr, "?"); else if (symbols) { s2 &= 0x7FFF; usages[s2].debug++; fprintf(stderr, "%s", symbols[s2]); } else fprintf(stderr, "%d", s2); break; case XS_CODE_CONST_GLOBAL: case XS_CODE_DELETE_EVAL: case XS_CODE_DELETE_GLOBAL: case XS_CODE_DELETE_PROPERTY: case XS_CODE_DELETE_SUPER: case XS_CODE_GET_EVAL: case XS_CODE_GET_GLOBAL: case XS_CODE_GET_PROPERTY: case XS_CODE_GET_SUPER: case XS_CODE_LET_GLOBAL: case XS_CODE_NEW_EVAL: case XS_CODE_NEW_GLOBAL: case XS_CODE_SET_EVAL: case XS_CODE_SET_GLOBAL: case XS_CODE_SET_PROPERTY: case XS_CODE_SET_SUPER: case XS_CODE_SYMBOL: mxDecode2(p, s2); if (s2 == XS_NO_ID) fprintf(stderr, "?"); else if (symbols) { s2 &= 0x7FFF; usages[s2].property++; fprintf(stderr, "%s", symbols[s2]); } else fprintf(stderr, "%d", s2); break; case XS_CODE_NEW_CLOSURE: case XS_CODE_NEW_LOCAL: mxDecode2(p, s2); if (s2 == XS_NO_ID) fprintf(stderr, "?"); else if (symbols) { s2 &= 0x7FFF; usages[s2].variable++; fprintf(stderr, "%s", symbols[s2]); } else fprintf(stderr, "%d", s2); break; case XS_CODE_NEW_PROPERTY: u1 = *((txU1*)p++); if (u1 & XS_DONT_DELETE_FLAG) fprintf(stderr, "C"); else fprintf(stderr, "c"); if (u1 & XS_DONT_ENUM_FLAG) fprintf(stderr, "E"); else fprintf(stderr, "e"); if (u1 & XS_DONT_SET_FLAG) fprintf(stderr, "W"); else fprintf(stderr, "w"); if (u1 & XS_METHOD_FLAG) fprintf(stderr, "method"); if (u1 & XS_GETTER_FLAG) fprintf(stderr, " getter"); if (u1 & XS_SETTER_FLAG) fprintf(stderr, " setter"); break; case XS_CODE_ARGUMENT: case XS_CODE_ARGUMENTS: case XS_CODE_ARGUMENTS_SLOPPY: case XS_CODE_ARGUMENTS_STRICT: case XS_CODE_BEGIN_SLOPPY: case XS_CODE_BEGIN_STRICT: case XS_CODE_BEGIN_STRICT_BASE: case XS_CODE_BEGIN_STRICT_DERIVED: u1 = *((txU1*)p++); fprintf(stderr, "%d", u1); break; case XS_CODE_LINE: mxDecode2(p, u2); fprintf(stderr, "%d", u2); break; case XS_CODE_RESERVE_1: case XS_CODE_RETRIEVE_1: case XS_CODE_UNWIND_1: u1 = *((txU1*)p++); fprintf(stderr, "#%d", u1); break; case XS_CODE_RESERVE_2: case XS_CODE_RETRIEVE_2: case XS_CODE_UNWIND_2: mxDecode2(p, u2); fprintf(stderr, "#%d", u2); break; case XS_CODE_CONST_CLOSURE_1: case XS_CODE_CONST_LOCAL_1: case XS_CODE_DELETE_CLOSURE_1: case XS_CODE_DELETE_LOCAL_1: case XS_CODE_GET_CLOSURE_1: case XS_CODE_GET_LOCAL_1: case XS_CODE_LET_CLOSURE_1: case XS_CODE_LET_LOCAL_1: case XS_CODE_PULL_CLOSURE_1: case XS_CODE_PULL_LOCAL_1: case XS_CODE_REFRESH_CLOSURE_1: case XS_CODE_REFRESH_LOCAL_1: case XS_CODE_RESET_CLOSURE_1: case XS_CODE_RESET_LOCAL_1: case XS_CODE_SET_CLOSURE_1: case XS_CODE_SET_LOCAL_1: case XS_CODE_STORE_1: u1 = *((txU1*)p++); fprintf(stderr, "[%d]", u1 - 2); break; case XS_CODE_CONST_CLOSURE_2: case XS_CODE_CONST_LOCAL_2: case XS_CODE_DELETE_CLOSURE_2: case XS_CODE_DELETE_LOCAL_2: case XS_CODE_GET_CLOSURE_2: case XS_CODE_GET_LOCAL_2: case XS_CODE_LET_CLOSURE_2: case XS_CODE_LET_LOCAL_2: case XS_CODE_PULL_CLOSURE_2: case XS_CODE_PULL_LOCAL_2: case XS_CODE_REFRESH_CLOSURE_2: case XS_CODE_REFRESH_LOCAL_2: case XS_CODE_RESET_CLOSURE_2: case XS_CODE_RESET_LOCAL_2: case XS_CODE_SET_CLOSURE_2: case XS_CODE_SET_LOCAL_2: case XS_CODE_STORE_2: mxDecode2(p, u2); fprintf(stderr, "[%d]", u2 - 2); break; case XS_CODE_INTEGER_1: s1 = *p++; fprintf(stderr, "%d", s1); break; case XS_CODE_INTEGER_2: mxDecode2(p, s2); fprintf(stderr, "%d", s2); break; case XS_CODE_INTEGER_4: mxDecode4(p, s4); fprintf(stderr, "%ld", s4); break; case XS_CODE_NUMBER: mxDecode8(p, number); fprintf(stderr, "%lf", number); break; case XS_CODE_STRING_1: case XS_CODE_STRING_ARCHIVE_1: u1 = *((txU1*)p++); fprintf(stderr, "\"%s\"", p); p += u1; break; case XS_CODE_STRING_2: case XS_CODE_STRING_ARCHIVE_2: mxDecode2(p, u2); fprintf(stderr, "\"%s\"", p); p += u2; break; case XS_CODE_HOST: mxDecode2(p, u2); if (hosts) fprintf(stderr, "%s", hosts[u2]); else fprintf(stderr, "[%d]", u2); break; } fprintf(stderr, "\n"); } }
FskErr loadGrammar(const char *xsbPath, xsGrammar *theGrammar) { FskErr err; FskFile fref = NULL; UInt32 atom[2]; void* p = atom; err = FskFileOpen(xsbPath, kFskFilePermissionReadOnly, &fref); if (err) goto bail; err = FskFileRead(fref, sizeof(atom), atom, NULL); if (err) goto bail; mxDecode4(p, atom[0]); mxDecode4(p, atom[1]); if (atom[1] == 'XS6B') { SInt32 totalSize = (SInt32)atom[0] - sizeof(atom); while (totalSize > 0) { UInt32 blockSize; char *block; err = FskFileRead(fref, sizeof(atom), atom, NULL); if (err) break; p = atom; mxDecode4(p, atom[0]); mxDecode4(p, atom[1]); totalSize -= atom[0]; blockSize = atom[0] - sizeof(atom); err = FskMemPtrNew(blockSize, &block); if (err) break; err = FskFileRead(fref, blockSize, block, NULL); if (err) break; switch (atom[1]) { case 'SYMB': theGrammar->symbols = block; theGrammar->symbolsSize = blockSize; break; case 'CODE': theGrammar->code = block; theGrammar->codeSize = blockSize; break; case 'VERS': FskMemPtrDispose(block); break; default: FskMemPtrDispose(block); err = kFskErrBadData; break; } } } else err = kFskErrBadData; bail: FskFileClose(fref); return err; }