void fxBufferSymbols(txLinker* linker, txInteger modulo) { if (modulo) { linker->symbolsBuffer = fxNewLinkerChunkClear(linker, 2); linker->symbolsSize = 2; } else { txByte* p; txS2 c, i; txSymbol** address; txInteger size; c = (txS2)(linker->symbolIndex); size = 2; address = &linker->symbolArray[0]; for (i = 0; i < c; i++) { size += (*address)->length; address++; } linker->symbolsBuffer = fxNewLinkerChunk(linker, size); linker->symbolsSize = size; p = linker->symbolsBuffer; mxEncode2(p, c); address = &(linker->symbolArray[0]); for (i = 0; i < c; i++) { c_memcpy(p, (*address)->string, (*address)->length); p += (*address)->length; address++; } } }
void xscBuildSymbolsID(txMachine* the, xsSymbolsData* theData, txByte* theCode) { txID anIndex; txSlot* aSymbol; txSize aModulo; txSlot* result; mxDecode2(theCode, anIndex); if ((aSymbol = fxGetSymbol(the, anIndex))) { aModulo = aSymbol->value.symbol.sum % theData->symbolModulo; result = theData->symbolTable[aModulo]; while (result != C_NULL) { if (result->value.symbol.sum == aSymbol->value.symbol.sum) if (result->value.symbol.string == aSymbol->value.symbol.string) break; result = result->next; } if (result == C_NULL) { anIndex = theData->symbolIndex; result = c_malloc(sizeof(txSlot)); xsElseError(result); result->next = theData->symbolTable[aModulo]; result->flag = XS_NO_FLAG; result->kind = XS_SYMBOL_KIND; result->ID = 0x8000 | anIndex; result->value.symbol.sum = aSymbol->value.symbol.sum; result->value.symbol.string = aSymbol->value.symbol.string; theData->symbolArray[anIndex] = result; theData->symbolTable[aModulo] = result; theData->symbolIndex++; } theCode -= 2; mxEncode2(theCode, result->ID); } }
void xscAppendString(txMachine* the, txString theString) { txID aLength = strlen(theString) + 1; xsBuffer* aBuffer = xsGetContext(the); xscGrowBuffer(the, aBuffer, aLength+1+stringCommandOffset); *(aBuffer->current) = XS_STRING; aBuffer->current++; #if mxStringLength mxEncode2(aBuffer->current, aLength); #endif memmove(aBuffer->current, theString, aLength); aBuffer->current += aLength; }
void fxBufferPaths(txLinker* linker) { txByte* p; txLinkerScript* script; linker->pathsBuffer = fxNewLinkerChunk(linker, linker->pathsSize); p = linker->pathsBuffer; mxEncode2(p, linker->scriptCount); script = linker->firstScript; while (script) { c_memcpy(p, script->path, script->pathSize); p += script->pathSize; script = script->nextScript; } }
void fxMapCode(txLinker* linker, txLinkerScript* script, txID* theIDs) { register const txS1* sizes = gxCodeSizes; register txByte* p = script->codeBuffer; register txByte* q = p + script->codeSize; register txS1 offset; txU1 code; txU2 index; txID id; while (p < q) { code = *((txU1*)p); if (XS_CODE_CODE_1 == code) *((txU1*)p) = code = XS_CODE_CODE_ARCHIVE_1; else if (XS_CODE_CODE_2 == code) *((txU1*)p) = code = XS_CODE_CODE_ARCHIVE_2; else if (XS_CODE_CODE_4 == code) *((txU1*)p) = code = XS_CODE_CODE_ARCHIVE_4; if (XS_CODE_STRING_1 == code) *((txU1*)p) = code = XS_CODE_STRING_ARCHIVE_1; else if (XS_CODE_STRING_2 == code) *((txU1*)p) = code = XS_CODE_STRING_ARCHIVE_2; offset = sizes[code]; if (0 < offset) p += offset; else if (0 == offset) { p++; mxDecode2(p, id); if (id != XS_NO_ID) { id &= 0x7FFF; id = theIDs[id]; p -= 2; mxEncode2(p, id); } } else if (-1 == offset) { p++; index = *((txU1*)p); p += 1 + index; } else if (-2 == offset) { p++; mxDecode2(p, index); p += index; } } }
void fxMapHosts(txLinker* linker, txLinkerScript* script, txID* theIDs) { txByte* p = script->hostsBuffer; if (p) { txID c, i, id; mxDecode2(p, c); for (i = 0; i < c; i++) { p++; mxDecode2(p, id); if (id != XS_NO_ID) { id = theIDs[id]; p -= 2; mxEncode2(p, id); } p += c_strlen((char*)p) + 1; } linker->hostsCount += c; } }
void xscAppend2(txMachine* the, txID theValue) { xsBuffer* aBuffer = xsGetContext(the); xscGrowBuffer(the, aBuffer, 2); mxEncode2(aBuffer->current, theValue); }
void xscBuildSymbols(txMachine* the) { xsBuffer* aBuffer = xsGetContext(the); xsSymbolsData* data = NULL; FILE* aFile = NULL; txInteger aSize; txString aString = NULL; txID aCount; txID anID; txSlot* aSymbol; txSize aModulo; txSlot* result; txID anIndex; txByte* aCode; txInteger aLength; txBoolean failed; static char* prefix = "#define xsID_"; static char* suffix = " (the->code["; txInteger prefixLength = c_strlen(prefix); txInteger suffixLength = c_strlen(suffix); mxTry(the) { data = c_calloc(1, sizeof(xsSymbolsData)); xsElseError(data); xsVar(1) = xsCat2(xsVar(0), xsString(".h")); aFile = fopen(xsToString(xsVar(1)), "rb"); if (aFile) { fseek(aFile, 0, SEEK_END); aSize = ftell(aFile); rewind(aFile); aString = c_malloc(aSize + 1); xsElseError(aString); fread(aString, 1, aSize, aFile); aString[aSize] = 0; fclose(aFile); aFile = C_NULL; } aCount = 0; if (aString) { txString p = c_strstr(aString, prefix), q; while (p) { p += prefixLength; q = c_strstr(p, suffix); q += suffixLength; anID = c_strtol(q, &p, 10); if (aCount <= anID) aCount = anID + 1; p = c_strstr(p, prefix); } } if (aCount < the->symbolCount) aCount = the->symbolCount; data->symbolCount = aCount; data->symbolIndex = 0; data->symbolArray = c_calloc(aCount, sizeof(txSlot*)); xsElseError(data->symbolArray); data->symbolModulo = the->symbolModulo; data->symbolTable = c_calloc(the->symbolModulo, sizeof(txSlot*)); xsElseError(data->symbolTable); if (aString) { txString p = c_strstr(aString, prefix), q; while (p) { p += prefixLength; q = c_strstr(p, suffix); *q = 0; aSymbol = fxNewSymbolC(the, p); aModulo = aSymbol->value.symbol.sum % data->symbolModulo; q += suffixLength; anID = c_strtol(q, &p, 10); xscBuildSymbol(the, data, aSymbol, anID, aModulo); data->symbolIndex = anID + 1; p = c_strstr(p, prefix); } c_free(aString); aString = C_NULL; } aCount = the->symbolIndex; anID = 0; for (anIndex = 0; anIndex < aCount; anIndex++) { aSymbol = the->symbolArray[anIndex]; if (aSymbol->flag & XS_TO_C_FLAG) { aModulo = aSymbol->value.symbol.sum % data->symbolModulo; result = data->symbolTable[aModulo]; while (result != C_NULL) { if (result->value.symbol.sum == aSymbol->value.symbol.sum) #if mxCmpStr if (result->value.symbol.string == aSymbol->value.symbol.string) #endif break; result = result->next; } if (result == C_NULL) { while (data->symbolArray[anID]) anID++; xscBuildSymbol(the, data, aSymbol, anID, aModulo); if (data->symbolIndex <= anID) data->symbolIndex = anID + 1; } } } aCount = data->symbolIndex; anIndex = 0; while (anID < aCount) { while ((anID < aCount) && data->symbolArray[anID]) anID++; if (anID < aCount) { char aName[16]; sprintf(aName, "@%d", anIndex); aSymbol = fxNewSymbolC(the, aName); aModulo = aSymbol->value.symbol.sum % data->symbolModulo; xscBuildSymbol(the, data, aSymbol, anID, aModulo); } } aCode = aBuffer->bottom; xscBuildSymbolsCode(the, data, aCode); aCount = data->symbolIndex; aSize = 2; for (anIndex = 0; anIndex < aCount; anIndex++) aSize += strlen(data->symbolArray[anIndex]->value.symbol.string) + symbolEncodeLength + 1; aBuffer->symbols = c_malloc(aSize); xsElseError(aBuffer->symbols); aBuffer->symbolsSize = aSize; aCode = aBuffer->symbols; mxEncode2(aCode, aCount); for (anIndex = 0; anIndex < aCount; anIndex++) { aString = data->symbolArray[anIndex]->value.symbol.string; aLength = strlen(aString) + 1; memcpy(aCode, aString, aLength); aCode += aLength; #if mxUseApHash mxEncode4(aCode,data->symbolArray[anIndex]->value.symbol.sum); #endif } aString = C_NULL; failed = 0; } mxCatch(the) { failed = 1; } if (data) { if (data->symbolTable) c_free(data->symbolTable); if (data->symbolArray) { aCount = data->symbolIndex; for (anIndex = 0; anIndex < aCount; anIndex++) c_free(data->symbolArray[anIndex]); c_free(data->symbolArray); } if (aString) c_free(aString); if (aFile) fclose(aFile); c_free(data); } if (failed) xsThrow(xsException); }