mirror of
https://git.wuffs.org/MWCC
synced 2025-12-12 06:45:17 +00:00
a bunch of compiler stuff
This commit is contained in:
255
compiler_and_linker/unsorted/CCompiler.c
Normal file
255
compiler_and_linker/unsorted/CCompiler.c
Normal file
@@ -0,0 +1,255 @@
|
||||
#include "compiler/common.h"
|
||||
#include "compiler.h"
|
||||
#include "compiler/types.h"
|
||||
#include "pref_structs.h"
|
||||
#include "compiler/CompilerTools.h"
|
||||
|
||||
Boolean systemHandles;
|
||||
|
||||
static Boolean using_license_manager;
|
||||
Boolean crippled;
|
||||
SInt32 license_cookie;
|
||||
CParams cparams;
|
||||
|
||||
// TODO move me to CParser.c, or maybe CMachine.c?
|
||||
Type sttemplexpr = {TYPETEMPLDEPEXPR, 0};
|
||||
Type stillegal = {TYPEILLEGAL, 1};
|
||||
Type stvoid = {TYPEVOID, 0};
|
||||
TypePointer void_ptr = {TYPEPOINTER, 0, &stvoid, 0};
|
||||
TypeFunc rt_func = {TYPEFUNC, 0, NULL, NULL, &stvoid, 0, 0};
|
||||
// TODO move me to CParser.c
|
||||
|
||||
static void get_extension(ConstStringPtr src, char *dst) {
|
||||
int ep;
|
||||
|
||||
dst[0] = 0;
|
||||
for (ep = src[0]; src[ep] != '.'; ep--) {}
|
||||
|
||||
if (ep >= 2) {
|
||||
int x;
|
||||
for (x = 0; (x + ep) <= src[0]; x++)
|
||||
dst[x] = src[x + ep];
|
||||
dst[x] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int setup_param_block(CWPluginContext context) {
|
||||
static char target_name[128];
|
||||
CWTargetInfo tinfo;
|
||||
|
||||
memset(&cparams, 0, sizeof(CParams));
|
||||
cparams.context = context;
|
||||
|
||||
if (CWGetPluginRequest(context, &cparams.pluginRequest) != cwNoErr)
|
||||
return 0;
|
||||
if (CWGetAPIVersion(context, &cparams.apiVersion) != cwNoErr)
|
||||
return 0;
|
||||
if (CWGetProjectFile(context, &cparams.projectFile) != cwNoErr)
|
||||
return 0;
|
||||
if (CWGetProjectFileCount(context, &cparams.projectFileCount) != cwNoErr)
|
||||
return 0;
|
||||
if (CWGetMainFileNumber(context, &cparams.mainFileNumber) != cwNoErr)
|
||||
return 0;
|
||||
if (CWGetMainFileSpec(context, &cparams.mainFileSpec) != cwNoErr)
|
||||
return 0;
|
||||
if (CWGetMainFileText(context, &cparams.mainFileText, &cparams.mainFileTextLength) != cwNoErr)
|
||||
return 0;
|
||||
if (CWIsPrecompiling(context, &cparams.isPrecompiling) != cwNoErr)
|
||||
return 0;
|
||||
if (CWIsAutoPrecompiling(context, &cparams.isAutoPrecompiling) != cwNoErr)
|
||||
return 0;
|
||||
if (CWIsPreprocessing(context, &cparams.isPreprocessing) != cwNoErr)
|
||||
return 0;
|
||||
if (CWIsGeneratingDebugInfo(context, &cparams.isGeneratingDebugInfo) != cwNoErr)
|
||||
return 0;
|
||||
if (CWIsCachingPrecompiledHeaders(context, &cparams.isCachingPrecompiledHeaders) != cwNoErr)
|
||||
return 0;
|
||||
if (CWGetBrowseOptions(context, &cparams.browseOptions) != cwNoErr)
|
||||
return 0;
|
||||
cparams.field276 = cparams.isPreprocessing == 0;
|
||||
if (CWGetMainFileID(context, &cparams.mainFileID) != cwNoErr)
|
||||
return 0;
|
||||
if (CWGetTargetInfo(context, &tinfo) != cwNoErr)
|
||||
return 0;
|
||||
|
||||
cparams.targetOS = tinfo.targetOS;
|
||||
cparams.targetCPU = tinfo.targetCPU;
|
||||
cparams.targetName = target_name;
|
||||
return CWGetTargetName(context, target_name, sizeof(target_name)) == cwNoErr;
|
||||
}
|
||||
|
||||
static short store_compile_results() {
|
||||
CWResult result;
|
||||
|
||||
if (cparams.objectDataHandle)
|
||||
CWSecretAttachHandle(cparams.context, cparams.objectDataHandle, &cparams.objectdata.objectdata);
|
||||
if (cparams.browseDataHandle)
|
||||
CWSecretAttachHandle(cparams.context, cparams.browseDataHandle, &cparams.objectdata.browsedata);
|
||||
|
||||
if (cparams.isPreprocessing) {
|
||||
if (cparams.objectdata.objectdata) {
|
||||
CWNewTextDocumentInfo docinfo;
|
||||
memset(&docinfo, 0, sizeof(CWNewTextDocumentInfo));
|
||||
docinfo.text = cparams.objectdata.objectdata;
|
||||
cparams.objectdata.objectdata = NULL;
|
||||
result = CWCreateNewTextDocument(cparams.context, &docinfo);
|
||||
}
|
||||
} else {
|
||||
result = CWStoreObjectData(cparams.context, cparams.mainFileNumber, &cparams.objectdata);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void initialize_compiler_options(CParams *params) {
|
||||
static PFrontEndC pFrontEnd;
|
||||
PWarningC pWarningC;
|
||||
PGlobalOptimizer pGlobalOptimizer;
|
||||
Handle prefsdata;
|
||||
char extension[256];
|
||||
|
||||
memclrw(&copts, sizeof(COpts));
|
||||
|
||||
CWSecretGetNamedPreferences(cparams.context, "C/C++ Compiler", &prefsdata);
|
||||
pFrontEnd = *((PFrontEndC *) *prefsdata);
|
||||
|
||||
copts.little_endian = 0;
|
||||
copts.cplusplus = 1;
|
||||
copts.objective_c = pFrontEnd.objective_c;
|
||||
get_extension(params->mainFileSpec.name, extension);
|
||||
if (!strcmp(extension, ".c") || !strcmp(extension, ".h") || !strcmp(extension, ".pch")) {
|
||||
copts.cplusplus = pFrontEnd.cplusplus;
|
||||
} else if (!strcmp(extension, ".m")) {
|
||||
copts.cplusplus = pFrontEnd.cplusplus;
|
||||
copts.objective_c = 1;
|
||||
} else if (!strcmp(extension, ".mm") || !strcmp(extension, ".M")) {
|
||||
copts.cplusplus = 1;
|
||||
copts.objective_c = 1;
|
||||
}
|
||||
|
||||
copts.require_prototypes = pFrontEnd.checkprotos;
|
||||
copts.ARM_conform = pFrontEnd.arm;
|
||||
copts.ARM_scoping = pFrontEnd.arm;
|
||||
copts.trigraphs = pFrontEnd.trigraphs;
|
||||
copts.only_std_keywords = pFrontEnd.onlystdkeywords;
|
||||
copts.enumsalwaysint = pFrontEnd.enumsalwaysint;
|
||||
copts.mpwc_relax = pFrontEnd.mpwpointerstyle;
|
||||
copts.ANSI_strict = pFrontEnd.ansistrict;
|
||||
copts.mpwc_newline = pFrontEnd.mpwcnewline;
|
||||
copts.exceptions = pFrontEnd.enableexceptions;
|
||||
copts.dont_reuse_strings = pFrontEnd.dontreusestrings;
|
||||
copts.pool_strings = pFrontEnd.poolstrings;
|
||||
copts.dont_inline = pFrontEnd.dontinline;
|
||||
copts.useRTTI = pFrontEnd.useRTTI;
|
||||
copts.oldprefixname = pFrontEnd.oldprefixname;
|
||||
copts.multibyteaware = pFrontEnd.multibyteaware;
|
||||
copts.unsignedchars = pFrontEnd.unsignedchars;
|
||||
copts.autoinline = pFrontEnd.autoinline;
|
||||
copts.direct_to_som = pFrontEnd.direct_to_som;
|
||||
copts.som_env_check = pFrontEnd.som_env_check;
|
||||
copts.booltruefalse = pFrontEnd.booltruefalse;
|
||||
copts.always_inline = pFrontEnd.alwaysinline;
|
||||
copts.inlinelevel = pFrontEnd.inlinelevel;
|
||||
copts.wchar_type = pFrontEnd.wchar_type;
|
||||
copts.ecplusplus = pFrontEnd.ecplusplus;
|
||||
copts.defer_codegen = pFrontEnd.defer_codegen;
|
||||
copts.inline_max_size = 512;
|
||||
copts.inline_max_total_size = 100000;
|
||||
|
||||
CWSecretGetNamedPreferences(cparams.context, "C/C++ Warnings", &prefsdata);
|
||||
pWarningC = *((PWarningC *) *prefsdata);
|
||||
|
||||
copts.warn_illpragma = pWarningC.warn_illpragma;
|
||||
copts.warn_emptydecl = pWarningC.warn_emptydecl;
|
||||
copts.warn_possunwant = pWarningC.warn_possunwant;
|
||||
copts.warn_unusedvar = pWarningC.warn_unusedvar;
|
||||
copts.warn_unusedarg = pWarningC.warn_unusedarg;
|
||||
copts.warn_extracomma = pWarningC.warn_extracomma;
|
||||
copts.pedantic = pWarningC.pedantic;
|
||||
copts.warningerrors = pWarningC.warningerrors;
|
||||
copts.warn_hidevirtual = pWarningC.warn_hidevirtual;
|
||||
copts.warn_implicitconv = pWarningC.warn_implicitconv;
|
||||
copts.warn_notinlined = pWarningC.warn_notinlined;
|
||||
copts.warn_structclass = pWarningC.warn_structclass;
|
||||
|
||||
CWSecretGetNamedPreferences(cparams.context, "PPC Global Optimizer", &prefsdata);
|
||||
pGlobalOptimizer = *((PGlobalOptimizer *) *prefsdata);
|
||||
|
||||
copts.optimizationlevel = pGlobalOptimizer.optimizationlevel;
|
||||
copts.optimize_for_size = (Boolean) (pGlobalOptimizer.optfor == 1);
|
||||
|
||||
copts.crippled = crippled;
|
||||
copts.loop_unroll_count = 8;
|
||||
copts.loop_unroll_size_threshold = 100;
|
||||
copts.isGeneratingDebugInfo = params->isGeneratingDebugInfo;
|
||||
copts.pchCreator = CWFOURCHAR('C','W','I','E');
|
||||
copts.pchType = CWFOURCHAR('M','M','C','H');
|
||||
copts.text = CWFOURCHAR('T','E','X','T');
|
||||
}
|
||||
|
||||
static void GetLicense() {
|
||||
if (!license_cookie) {
|
||||
crippled = 1;
|
||||
CWCheckoutLicense(cparams.context, "MacOS_Plugins_MacOS_Unlimited", "7", 2, NULL, &license_cookie);
|
||||
if (license_cookie) {
|
||||
CWCheckinLicense(cparams.context, license_cookie);
|
||||
crippled = 0;
|
||||
}
|
||||
|
||||
CWCheckoutLicense(cparams.context, "MacOS_Plugins_MacOS_Limited", "7", 0, NULL, &license_cookie);
|
||||
using_license_manager = 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void ReleaseLicense() {
|
||||
if (license_cookie && using_license_manager)
|
||||
CWCheckinLicense(cparams.context, license_cookie);
|
||||
using_license_manager = 0;
|
||||
license_cookie = 0;
|
||||
}
|
||||
|
||||
CWPLUGIN_ENTRY(MWC_main)(CWPluginContext context) {
|
||||
CWResult result;
|
||||
SInt32 request;
|
||||
|
||||
result = cwNoErr;
|
||||
CWGetPluginRequest(context, &request);
|
||||
|
||||
switch (request) {
|
||||
case reqInitialize:
|
||||
cparams.context = context;
|
||||
license_cookie = 0;
|
||||
CodeGen_InitCompiler();
|
||||
break;
|
||||
case reqTerminate:
|
||||
cparams.context = context;
|
||||
CodeGen_TermCompiler();
|
||||
ReleaseLicense();
|
||||
break;
|
||||
case reqCompile:
|
||||
setup_param_block(context);
|
||||
GetLicense();
|
||||
if (license_cookie) {
|
||||
initialize_compiler_options(&cparams);
|
||||
CodeGen_UpdateOptimizerOptions();
|
||||
CodeGen_InitBackEndOptions();
|
||||
CodeGen_UpdateOptimizerOptions();
|
||||
CodeGen_UpdateBackEndOptions();
|
||||
if (C_Compiler(&cparams))
|
||||
result = store_compile_results();
|
||||
else
|
||||
result = cwErrRequestFailed;
|
||||
} else {
|
||||
result = cwErrSilent;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return CWDonePluginRequest(context, result);
|
||||
}
|
||||
|
||||
void PrintProgressFunction(const char *str) {
|
||||
char buf[96];
|
||||
sprintf(buf, "Compiling function:\t%.64s", str);
|
||||
CWShowStatus(cparams.context, buf, "");
|
||||
}
|
||||
1096
compiler_and_linker/unsorted/CError.c
Normal file
1096
compiler_and_linker/unsorted/CError.c
Normal file
File diff suppressed because it is too large
Load Diff
892
compiler_and_linker/unsorted/CInt64.c
Normal file
892
compiler_and_linker/unsorted/CInt64.c
Normal file
@@ -0,0 +1,892 @@
|
||||
#include "compiler/CInt64.h"
|
||||
|
||||
const CInt64 cint64_negone = {0xFFFFFFFF, 0xFFFFFFFF};
|
||||
const CInt64 cint64_zero = {0, 0};
|
||||
const CInt64 cint64_one = {0, 1};
|
||||
const CInt64 cint64_max = {0x7FFFFFFF, 0xFFFFFFFF};
|
||||
const CInt64 cint64_min = {0x80000000, 0};
|
||||
|
||||
#define SHIFT_LEFT_ONE(a, b) do { a <<= 1; if (b & 0x80000000) { a |= 1; } b <<= 1; } while(0)
|
||||
|
||||
void CInt64_Init() {
|
||||
}
|
||||
|
||||
CInt64 CInt64_Not(CInt64 input) {
|
||||
CInt64 output;
|
||||
Boolean c;
|
||||
|
||||
c = (input.hi == 0 && input.lo == 0);
|
||||
CInt64_SetLong(&output, c);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
CInt64 CInt64_Inv(CInt64 input) {
|
||||
CInt64 output;
|
||||
output.hi = ~input.hi;
|
||||
output.lo = ~input.lo;
|
||||
return output;
|
||||
}
|
||||
|
||||
CInt64 CInt64_Add(CInt64 lhs, CInt64 rhs) {
|
||||
if (lhs.lo & 0x80000000) {
|
||||
if (rhs.lo & 0x80000000) {
|
||||
lhs.lo += rhs.lo;
|
||||
lhs.hi += 1;
|
||||
} else {
|
||||
lhs.lo += rhs.lo;
|
||||
if (!(lhs.lo & 0x80000000))
|
||||
lhs.hi += 1;
|
||||
}
|
||||
} else {
|
||||
if (rhs.lo & 0x80000000) {
|
||||
lhs.lo += rhs.lo;
|
||||
if (!(lhs.lo & 0x80000000))
|
||||
lhs.hi += 1;
|
||||
} else {
|
||||
lhs.lo += rhs.lo;
|
||||
}
|
||||
}
|
||||
lhs.hi += rhs.hi;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
CInt64 CInt64_Neg(CInt64 input) {
|
||||
CInt64 result;
|
||||
result = CInt64_Add(CInt64_Inv(input), cint64_one);
|
||||
return result;
|
||||
}
|
||||
|
||||
CInt64 CInt64_Sub(CInt64 lhs, CInt64 rhs) {
|
||||
lhs = CInt64_Add(lhs, CInt64_Neg(rhs));
|
||||
return lhs;
|
||||
}
|
||||
|
||||
CInt64 CInt64_MulU(CInt64 lhs, CInt64 rhs) {
|
||||
CInt64 result;
|
||||
CInt64 work1;
|
||||
UInt32 aaaa;
|
||||
UInt32 bbbb;
|
||||
UInt32 cccc;
|
||||
UInt32 dddd;
|
||||
UInt32 eeee;
|
||||
|
||||
aaaa = rhs.lo;
|
||||
result.hi = result.lo = 0;
|
||||
bbbb = lhs.lo;
|
||||
cccc = rhs.hi;
|
||||
dddd = rhs.lo;
|
||||
|
||||
while (bbbb != 0) {
|
||||
if (bbbb & 1) {
|
||||
work1.hi = cccc;
|
||||
work1.lo = dddd;
|
||||
result = CInt64_Add(result, work1);
|
||||
}
|
||||
cccc <<= 1;
|
||||
if (dddd & 0x80000000)
|
||||
cccc |= 1;
|
||||
dddd <<= 1;
|
||||
bbbb >>= 1;
|
||||
}
|
||||
|
||||
eeee = lhs.hi;
|
||||
while (eeee != 0 && aaaa != 0) {
|
||||
if (eeee & 1)
|
||||
result.hi += aaaa;
|
||||
aaaa <<= 1;
|
||||
eeee >>= 1;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
CInt64 CInt64_Mul(CInt64 lhs, CInt64 rhs) {
|
||||
if (CInt64_IsNegative(&rhs)) {
|
||||
if (CInt64_IsNegative(&lhs)) {
|
||||
return CInt64_MulU(CInt64_Neg(lhs), CInt64_Neg(rhs));
|
||||
}
|
||||
return CInt64_Neg(CInt64_MulU(lhs, CInt64_Neg(rhs)));
|
||||
}
|
||||
if (CInt64_IsNegative(&lhs)) {
|
||||
return CInt64_Neg(CInt64_MulU(CInt64_Neg(lhs), rhs));
|
||||
}
|
||||
return CInt64_MulU(lhs, rhs);
|
||||
}
|
||||
|
||||
void CInt64_DivMod(const CInt64 *lhs, const CInt64 *rhs, CInt64 *pDiv, CInt64 *pMod) {
|
||||
Boolean bad;
|
||||
UInt32 workA;
|
||||
UInt32 workB;
|
||||
UInt32 leftA;
|
||||
UInt32 leftB;
|
||||
UInt32 rightA;
|
||||
UInt32 rightB;
|
||||
UInt32 outA;
|
||||
UInt32 outB;
|
||||
CInt64 work;
|
||||
int counter;
|
||||
|
||||
bad = (rhs->hi == 0) && (rhs->lo == 0);
|
||||
if (!bad) {
|
||||
workA = 0;
|
||||
workB = 0;
|
||||
leftA = lhs->hi;
|
||||
leftB = lhs->lo;
|
||||
rightA = rhs->hi;
|
||||
rightB = rhs->lo;
|
||||
outA = 0;
|
||||
outB = 0;
|
||||
counter = 0;
|
||||
|
||||
do {
|
||||
workA <<= 1;
|
||||
if (workB & 0x80000000)
|
||||
workA |= 1;
|
||||
workB <<= 1;
|
||||
if (leftA & 0x80000000)
|
||||
workB |= 1;
|
||||
leftA <<= 1;
|
||||
if (leftB & 0x80000000)
|
||||
leftA |= 1;
|
||||
leftB <<= 1;
|
||||
|
||||
outA <<= 1;
|
||||
if (outB & 0x80000000)
|
||||
outA |= 1;
|
||||
outB <<= 1;
|
||||
|
||||
if (workA <= rightA) {
|
||||
if (workA != rightA)
|
||||
continue;
|
||||
if (workB < rightB)
|
||||
continue;
|
||||
}
|
||||
|
||||
outB |= 1;
|
||||
|
||||
work.hi = workA;
|
||||
work.lo = workB;
|
||||
work = CInt64_Sub(work, *rhs);
|
||||
workA = work.hi;
|
||||
workB = work.lo;
|
||||
} while (++counter < 64);
|
||||
|
||||
if (pDiv) {
|
||||
pDiv->hi = outA;
|
||||
pDiv->lo = outB;
|
||||
}
|
||||
if (pMod) {
|
||||
pMod->hi = workA;
|
||||
pMod->lo = workB;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CInt64 CInt64_Div(CInt64 lhs, CInt64 rhs) {
|
||||
CInt64 result;
|
||||
if (CInt64_IsNegative(&rhs)) {
|
||||
rhs = CInt64_Neg(rhs);
|
||||
if (CInt64_IsNegative(&lhs)) {
|
||||
lhs = CInt64_Neg(lhs);
|
||||
CInt64_DivMod(&lhs, &rhs, &result, 0);
|
||||
return result;
|
||||
} else {
|
||||
CInt64_DivMod(&lhs, &rhs, &result, 0);
|
||||
return CInt64_Neg(result);
|
||||
}
|
||||
} else {
|
||||
if (CInt64_IsNegative(&lhs)) {
|
||||
lhs = CInt64_Neg(lhs);
|
||||
CInt64_DivMod(&lhs, &rhs, &result, 0);
|
||||
return CInt64_Neg(result);
|
||||
} else {
|
||||
CInt64_DivMod(&lhs, &rhs, &result, 0);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CInt64 CInt64_DivU(CInt64 lhs, CInt64 rhs) {
|
||||
CInt64 result;
|
||||
CInt64_DivMod(&lhs, &rhs, &result, 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
CInt64 CInt64_Mod(CInt64 lhs, CInt64 rhs) {
|
||||
CInt64 result;
|
||||
if (CInt64_IsNegative(&lhs)) {
|
||||
lhs = CInt64_Neg(lhs);
|
||||
if (CInt64_IsNegative(&rhs))
|
||||
rhs = CInt64_Neg(rhs);
|
||||
CInt64_DivMod(&lhs, &rhs, 0, &result);
|
||||
return CInt64_Neg(result);
|
||||
} else {
|
||||
if (CInt64_IsNegative(&rhs))
|
||||
rhs = CInt64_Neg(rhs);
|
||||
CInt64_DivMod(&lhs, &rhs, 0, &result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
CInt64 CInt64_ModU(CInt64 lhs, CInt64 rhs) {
|
||||
CInt64 result;
|
||||
CInt64_DivMod(&lhs, &rhs, 0, &result);
|
||||
return result;
|
||||
}
|
||||
|
||||
CInt64 CInt64_Shl(CInt64 lhs, CInt64 rhs) {
|
||||
int counter;
|
||||
UInt32 a;
|
||||
UInt32 b;
|
||||
|
||||
if (rhs.hi == 0 && rhs.lo < 64) {
|
||||
a = lhs.hi;
|
||||
b = lhs.lo;
|
||||
for (counter = rhs.lo; counter != 0; --counter) {
|
||||
a <<= 1;
|
||||
if (b & 0x80000000)
|
||||
a |= 1;
|
||||
b <<= 1;
|
||||
}
|
||||
lhs.hi = a;
|
||||
lhs.lo = b;
|
||||
} else {
|
||||
lhs.hi = 0;
|
||||
lhs.lo = 0;
|
||||
}
|
||||
return lhs;
|
||||
}
|
||||
|
||||
CInt64 CInt64_Shr(CInt64 lhs, CInt64 rhs) {
|
||||
int counter;
|
||||
SInt32 a;
|
||||
UInt32 b;
|
||||
|
||||
if (rhs.hi == 0 && rhs.lo < 64) {
|
||||
a = lhs.hi;
|
||||
b = lhs.lo;
|
||||
for (counter = rhs.lo; counter != 0; --counter) {
|
||||
b >>= 1;
|
||||
if (a & 1)
|
||||
b |= 0x80000000;
|
||||
a >>= 1;
|
||||
}
|
||||
lhs.hi = a;
|
||||
lhs.lo = b;
|
||||
} else {
|
||||
if (lhs.hi & 0x80000000) {
|
||||
lhs.hi = 0xFFFFFFFF;
|
||||
lhs.lo = 0xFFFFFFFF;
|
||||
} else {
|
||||
lhs.hi = 0;
|
||||
lhs.lo = 0;
|
||||
}
|
||||
}
|
||||
return lhs;
|
||||
}
|
||||
|
||||
CInt64 CInt64_ShrU(CInt64 lhs, CInt64 rhs) {
|
||||
int counter;
|
||||
UInt32 a;
|
||||
UInt32 b;
|
||||
|
||||
if (rhs.hi == 0 && rhs.lo < 64) {
|
||||
a = lhs.hi;
|
||||
b = lhs.lo;
|
||||
for (counter = rhs.lo; counter != 0; --counter) {
|
||||
b >>= 1;
|
||||
if (a & 1)
|
||||
b |= 0x80000000;
|
||||
a >>= 1;
|
||||
}
|
||||
lhs.hi = a;
|
||||
lhs.lo = b;
|
||||
} else {
|
||||
lhs.hi = 0;
|
||||
lhs.lo = 0;
|
||||
}
|
||||
return lhs;
|
||||
}
|
||||
|
||||
int CInt64_UnsignedCompare(const CInt64 *lhs, const CInt64 *rhs) {
|
||||
if (lhs->hi == rhs->hi) {
|
||||
if (lhs->lo < rhs->lo)
|
||||
return -1;
|
||||
else
|
||||
return lhs->lo > rhs->lo;
|
||||
} else {
|
||||
return ((UInt32) lhs->hi < rhs->hi) ? -1 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
int CInt64_SignedCompare(const CInt64 *lhs, const CInt64 *rhs) {
|
||||
CInt64 lhs_;
|
||||
CInt64 rhs_;
|
||||
|
||||
lhs_ = CInt64_Xor(*lhs, cint64_min);
|
||||
rhs_ = CInt64_Xor(*rhs, cint64_min);
|
||||
return CInt64_UnsignedCompare(&lhs_, &rhs_);
|
||||
}
|
||||
|
||||
Boolean CInt64_Less(CInt64 lhs, CInt64 rhs) {
|
||||
return CInt64_SignedCompare(&lhs, &rhs) < 0;
|
||||
}
|
||||
|
||||
Boolean CInt64_LessU(CInt64 lhs, CInt64 rhs) {
|
||||
return CInt64_UnsignedCompare(&lhs, &rhs) < 0;
|
||||
}
|
||||
|
||||
Boolean CInt64_Greater(CInt64 lhs, CInt64 rhs) {
|
||||
return CInt64_SignedCompare(&lhs, &rhs) > 0;
|
||||
}
|
||||
|
||||
Boolean CInt64_GreaterU(CInt64 lhs, CInt64 rhs) {
|
||||
return CInt64_UnsignedCompare(&lhs, &rhs) > 0;
|
||||
}
|
||||
|
||||
Boolean CInt64_LessEqual(CInt64 lhs, CInt64 rhs) {
|
||||
return CInt64_SignedCompare(&lhs, &rhs) <= 0;
|
||||
}
|
||||
|
||||
Boolean CInt64_LessEqualU(CInt64 lhs, CInt64 rhs) {
|
||||
return CInt64_UnsignedCompare(&lhs, &rhs) <= 0;
|
||||
}
|
||||
|
||||
Boolean CInt64_GreaterEqual(CInt64 lhs, CInt64 rhs) {
|
||||
return CInt64_SignedCompare(&lhs, &rhs) >= 0;
|
||||
}
|
||||
|
||||
Boolean CInt64_GreaterEqualU(CInt64 lhs, CInt64 rhs) {
|
||||
return CInt64_UnsignedCompare(&lhs, &rhs) >= 0;
|
||||
}
|
||||
|
||||
Boolean CInt64_Equal(CInt64 lhs, CInt64 rhs) {
|
||||
return lhs.hi == rhs.hi && lhs.lo == rhs.lo;
|
||||
}
|
||||
|
||||
Boolean CInt64_NotEqual(CInt64 lhs, CInt64 rhs) {
|
||||
return lhs.hi != rhs.hi || lhs.lo != rhs.lo;
|
||||
}
|
||||
|
||||
Boolean CInt64_IsInRange(CInt64 value, short len) {
|
||||
CInt64 bound;
|
||||
|
||||
if (value.hi & 0x80000000) {
|
||||
switch (len) {
|
||||
case 1:
|
||||
bound.lo = 0xFFFFFF80;
|
||||
bound.hi = 0xFFFFFFFF;
|
||||
break;
|
||||
case 2:
|
||||
bound.lo = 0xFFFF8000;
|
||||
bound.hi = 0xFFFFFFFF;
|
||||
break;
|
||||
case 4:
|
||||
bound.lo = 0x80000000;
|
||||
bound.hi = 0xFFFFFFFF;
|
||||
break;
|
||||
case 8:
|
||||
return 1;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
return CInt64_GreaterEqual(value, bound);
|
||||
} else {
|
||||
switch (len) {
|
||||
case 1:
|
||||
bound.lo = 0x7F;
|
||||
bound.hi = 0;
|
||||
break;
|
||||
case 2:
|
||||
bound.lo = 0x7FFF;
|
||||
bound.hi = 0;
|
||||
break;
|
||||
case 4:
|
||||
bound.lo = 0x7FFFFFFF;
|
||||
bound.hi = 0;
|
||||
break;
|
||||
case 8:
|
||||
return 1;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
return CInt64_LessEqual(value, bound);
|
||||
}
|
||||
}
|
||||
|
||||
Boolean CInt64_IsInURange(CInt64 value, short len) {
|
||||
switch (len) {
|
||||
case 1:
|
||||
return value.hi == 0 && (value.lo & 0xFFFFFF00) == 0;
|
||||
case 2:
|
||||
return value.hi == 0 && (value.lo & 0xFFFF0000) == 0;
|
||||
case 4:
|
||||
return value.hi == 0;
|
||||
case 8:
|
||||
return 1;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
CInt64 CInt64_And(CInt64 lhs, CInt64 rhs) {
|
||||
lhs.hi &= rhs.hi;
|
||||
lhs.lo &= rhs.lo;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
CInt64 CInt64_Xor(CInt64 lhs, CInt64 rhs) {
|
||||
lhs.hi ^= rhs.hi;
|
||||
lhs.lo ^= rhs.lo;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
CInt64 CInt64_Or(CInt64 lhs, CInt64 rhs) {
|
||||
lhs.hi |= rhs.hi;
|
||||
lhs.lo |= rhs.lo;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
void CInt64_ConvertInt32(CInt64 *i) {
|
||||
CInt64_Extend32(i);
|
||||
}
|
||||
|
||||
void CInt64_ConvertUInt32(CInt64 *i) {
|
||||
CInt64_SetULong(i, (UInt32) i->lo);
|
||||
}
|
||||
|
||||
void CInt64_ConvertInt16(CInt64 *i) {
|
||||
i->lo = (SInt32) i->lo;
|
||||
CInt64_Extend32(i);
|
||||
}
|
||||
|
||||
void CInt64_ConvertUInt16(CInt64 *i) {
|
||||
CInt64_SetULong(i, (UInt16) i->lo);
|
||||
}
|
||||
|
||||
void CInt64_ConvertInt8(CInt64 *i) {
|
||||
i->lo = (SInt8) i->lo;
|
||||
CInt64_Extend32(i);
|
||||
}
|
||||
|
||||
void CInt64_ConvertUInt8(CInt64 *i) {
|
||||
CInt64_SetULong(i, (UInt8) i->lo);
|
||||
}
|
||||
|
||||
void CInt64_ConvertUFromLongDouble(CInt64 *pResult, double value) {
|
||||
union { float f; UInt32 l; } cvt;
|
||||
UInt32 a, b;
|
||||
float threshold;
|
||||
int bits;
|
||||
|
||||
if (value <= 0.0) {
|
||||
pResult->hi = 0;
|
||||
pResult->lo = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
cvt.l = 0x5F800000;
|
||||
if (value >= cvt.f) {
|
||||
pResult->hi = 0xFFFFFFFF;
|
||||
pResult->lo = 0xFFFFFFFF;
|
||||
return;
|
||||
}
|
||||
|
||||
a = b = 0;
|
||||
for (bits = 63; bits >= 0; bits--) {
|
||||
a <<= 1;
|
||||
if (b & 0x80000000)
|
||||
a |= 1;
|
||||
b <<= 1;
|
||||
|
||||
if ((short) bits == 0) {
|
||||
threshold = 1.0f;
|
||||
} else {
|
||||
cvt.l = (((short) bits + 127) & 255) << 23;
|
||||
threshold = cvt.f;
|
||||
}
|
||||
|
||||
if (threshold <= value) {
|
||||
b |= 1;
|
||||
value -= threshold;
|
||||
}
|
||||
}
|
||||
|
||||
pResult->hi = a;
|
||||
pResult->lo = b;
|
||||
}
|
||||
|
||||
void CInt64_ConvertFromLongDouble(CInt64 *pResult, double value) {
|
||||
if (value < 0.0) {
|
||||
CInt64_ConvertUFromLongDouble(pResult, -value);
|
||||
*pResult = CInt64_Neg(*pResult);
|
||||
} else {
|
||||
CInt64_ConvertUFromLongDouble(pResult, value);
|
||||
}
|
||||
}
|
||||
|
||||
double CInt64_ConvertUToLongDouble(const CInt64 *value) {
|
||||
Boolean bad;
|
||||
UInt32 work;
|
||||
int counter;
|
||||
double result;
|
||||
|
||||
bad = (value->hi == 0) && (value->lo == 0);
|
||||
if (bad) {
|
||||
return 0.0;
|
||||
} else {
|
||||
result = 0.0;
|
||||
|
||||
work = value->hi;
|
||||
if (work != 0) {
|
||||
for (counter = 0; counter < 32; counter++) {
|
||||
result += result;
|
||||
if (work & 0x80000000)
|
||||
result += 1.0;
|
||||
work <<= 1;
|
||||
}
|
||||
}
|
||||
|
||||
counter = 0;
|
||||
work = value->lo;
|
||||
for (; counter < 32; counter++) {
|
||||
result += result;
|
||||
if (work & 0x80000000)
|
||||
result += 1.0;
|
||||
work <<= 1;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
double CInt64_ConvertToLongDouble(const CInt64 *value) {
|
||||
CInt64 tmp;
|
||||
if (value->hi & 0x80000000) {
|
||||
tmp = CInt64_Neg(*value);
|
||||
return -CInt64_ConvertUToLongDouble(&tmp);
|
||||
} else {
|
||||
return CInt64_ConvertUToLongDouble(value);
|
||||
}
|
||||
}
|
||||
|
||||
char *CInt64_ScanOctString(CInt64 *pResult, char *str, Boolean *pFail) {
|
||||
int ch;
|
||||
CInt64 tmp;
|
||||
UInt32 a;
|
||||
UInt32 b;
|
||||
|
||||
*pFail = 0;
|
||||
pResult->hi = pResult->lo = 0;
|
||||
|
||||
while ((ch = *str) >= '0' && *str <= '7') {
|
||||
a = pResult->hi;
|
||||
b = pResult->lo;
|
||||
if (a & 0xE0000000)
|
||||
*pFail = 1;
|
||||
|
||||
SHIFT_LEFT_ONE(a, b);
|
||||
SHIFT_LEFT_ONE(a, b);
|
||||
SHIFT_LEFT_ONE(a, b);
|
||||
|
||||
pResult->hi = a;
|
||||
pResult->lo = b;
|
||||
|
||||
CInt64_SetLong(&tmp, ch - '0');
|
||||
*pResult = CInt64_Add(*pResult, tmp);
|
||||
++str;
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
char *CInt64_ScanDecString(CInt64 *pResult, char *str, Boolean *pFail) {
|
||||
int ch;
|
||||
CInt64 tmp;
|
||||
UInt32 a;
|
||||
UInt32 b;
|
||||
|
||||
*pFail = 0;
|
||||
pResult->hi = pResult->lo = 0;
|
||||
|
||||
while ((ch = *str) >= '0' && *str <= '9') {
|
||||
a = pResult->hi;
|
||||
b = pResult->lo;
|
||||
if (a & 0xE0000000)
|
||||
*pFail = 1;
|
||||
|
||||
SHIFT_LEFT_ONE(a, b);
|
||||
tmp.hi = a;
|
||||
tmp.lo = b;
|
||||
SHIFT_LEFT_ONE(a, b);
|
||||
SHIFT_LEFT_ONE(a, b);
|
||||
pResult->hi = a;
|
||||
pResult->lo = b;
|
||||
|
||||
if (CInt64_IsNegative(pResult)) {
|
||||
*pResult = CInt64_Add(*pResult, tmp);
|
||||
if (!CInt64_IsNegative(pResult))
|
||||
*pFail = 1;
|
||||
} else {
|
||||
*pResult = CInt64_Add(*pResult, tmp);
|
||||
}
|
||||
|
||||
CInt64_SetLong(&tmp, ch - '0');
|
||||
if (CInt64_IsNegative(pResult)) {
|
||||
*pResult = CInt64_Add(*pResult, tmp);
|
||||
if (!CInt64_IsNegative(pResult))
|
||||
*pFail = 1;
|
||||
} else {
|
||||
*pResult = CInt64_Add(*pResult, tmp);
|
||||
}
|
||||
|
||||
++str;
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
char *CInt64_ScanHexString(CInt64 *pResult, char *str, Boolean *pFail) {
|
||||
/* NOT MATCHING */
|
||||
int digit;
|
||||
CInt64 tmp;
|
||||
UInt32 a;
|
||||
UInt32 b;
|
||||
|
||||
*pFail = 0;
|
||||
pResult->hi = pResult->lo = 0;
|
||||
|
||||
for (;;) {
|
||||
if ((digit = str[0]) >= '0' && digit <= '9')
|
||||
digit = digit - '0';
|
||||
else if (digit >= 'A' && digit <= 'F')
|
||||
digit = digit - 'A' + 10;
|
||||
else if (digit >= 'a' && digit <= 'f')
|
||||
digit = digit - 'a' + 10;
|
||||
else
|
||||
break;
|
||||
|
||||
a = pResult->hi;
|
||||
b = pResult->lo;
|
||||
++str;
|
||||
|
||||
if (a & 0xF0000000)
|
||||
*pFail = 1;
|
||||
|
||||
SHIFT_LEFT_ONE(a, b);
|
||||
SHIFT_LEFT_ONE(a, b);
|
||||
SHIFT_LEFT_ONE(a, b);
|
||||
SHIFT_LEFT_ONE(a, b);
|
||||
|
||||
pResult->hi = a;
|
||||
pResult->lo = b;
|
||||
|
||||
CInt64_SetLong(&tmp, (char) digit);
|
||||
*pResult = CInt64_Add(*pResult, tmp);
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
char *CInt64_ScanBinString(CInt64 *pResult, char *str, Boolean *pFail) {
|
||||
char digit;
|
||||
UInt32 a;
|
||||
UInt32 b;
|
||||
|
||||
*pFail = 0;
|
||||
pResult->hi = pResult->lo = 0;
|
||||
|
||||
for (;;) {
|
||||
if (*str == '0')
|
||||
digit = 0;
|
||||
else if (*str == '1')
|
||||
digit = 1;
|
||||
else
|
||||
break;
|
||||
|
||||
a = pResult->hi;
|
||||
b = pResult->lo;
|
||||
++str;
|
||||
|
||||
if (a & 0x80000000)
|
||||
*pFail = 1;
|
||||
|
||||
SHIFT_LEFT_ONE(a, b);
|
||||
|
||||
pResult->hi = a;
|
||||
pResult->lo = b;
|
||||
|
||||
if (digit == 1)
|
||||
*pResult = CInt64_Add(*pResult, cint64_one);
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
char *CInt64_ScanAsmNumber(CInt64 *pResult, char *str, Boolean *pFail) {
|
||||
Boolean isMaybeBin;
|
||||
Boolean isOct;
|
||||
Boolean isMaybeDec;
|
||||
Boolean isBin;
|
||||
char *p;
|
||||
|
||||
isMaybeBin = 1;
|
||||
isOct = *str == '0';
|
||||
isMaybeDec = 1;
|
||||
isBin = 0;
|
||||
p = str;
|
||||
|
||||
for (;;) {
|
||||
if (*p == 0)
|
||||
break;
|
||||
|
||||
if (strchr("01", p[0])) {
|
||||
if (isBin)
|
||||
isMaybeBin = 0;
|
||||
} else if (strchr("bB", p[0])) {
|
||||
isBin = 1;
|
||||
isMaybeDec = 0;
|
||||
isOct = 0;
|
||||
} else if (strchr("234567", p[0])) {
|
||||
isMaybeBin = 0;
|
||||
} else if (strchr("89", p[0])) {
|
||||
isOct = 0;
|
||||
isMaybeBin = 0;
|
||||
} else if (strchr("acdefACEDF", p[0])) {
|
||||
isMaybeDec = 0;
|
||||
isMaybeBin = 0;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
||||
++p;
|
||||
}
|
||||
|
||||
if (isMaybeBin && isBin) {
|
||||
return CInt64_ScanBinString(pResult, str, pFail) + 1;
|
||||
} else if (p[0] == 'h' || p[0] == 'H') {
|
||||
return CInt64_ScanHexString(pResult, str, pFail) + 1;
|
||||
} else if (isOct) {
|
||||
return CInt64_ScanOctString(pResult, str, pFail);
|
||||
} else if (isMaybeDec) {
|
||||
return CInt64_ScanDecString(pResult, str, pFail);
|
||||
} else {
|
||||
*pFail = 1;
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
int CInt64_PrintDec(char *output, CInt64 value) {
|
||||
int length;
|
||||
CInt64 rem;
|
||||
CInt64 divisor;
|
||||
char buf[40];
|
||||
char *bufp;
|
||||
|
||||
length = 0;
|
||||
if (CInt64_IsNegative(&value)) {
|
||||
value = CInt64_Neg(value);
|
||||
*output = '-';
|
||||
length++;
|
||||
output++;
|
||||
}
|
||||
|
||||
if (!CInt64_IsZero(&value)) {
|
||||
divisor.lo = 10;
|
||||
divisor.hi = 0;
|
||||
|
||||
bufp = buf;
|
||||
for (;;) {
|
||||
rem = CInt64_ModU(value, divisor);
|
||||
*(bufp++) = rem.lo + '0';
|
||||
value = CInt64_DivU(value, divisor);
|
||||
if (CInt64_IsZero(&value) != 0)
|
||||
break;
|
||||
}
|
||||
|
||||
while (--bufp >= buf) {
|
||||
*(output++) = *bufp;
|
||||
length++;
|
||||
}
|
||||
} else {
|
||||
*(output++) = '0';
|
||||
length++;
|
||||
}
|
||||
|
||||
*output = 0;
|
||||
return length;
|
||||
}
|
||||
|
||||
int CInt64_PrintHex(char *output, CInt64 value) {
|
||||
int length;
|
||||
CInt64 rem;
|
||||
CInt64 shift;
|
||||
CInt64 mask;
|
||||
char buf[32];
|
||||
char *bufp;
|
||||
|
||||
length = 0;
|
||||
if (!CInt64_IsZero(&value)) {
|
||||
shift.lo = 4;
|
||||
shift.hi = 0;
|
||||
mask.lo = 0xF;
|
||||
mask.hi = 0;
|
||||
|
||||
bufp = buf;
|
||||
for (;;) {
|
||||
rem = CInt64_And(value, mask);
|
||||
if ((SInt32) rem.lo >= 10)
|
||||
*(bufp++) = rem.lo + 'A';
|
||||
else
|
||||
*(bufp++) = rem.lo + '0';
|
||||
value = CInt64_ShrU(value, shift);
|
||||
if (CInt64_IsZero(&value) != 0)
|
||||
break;
|
||||
}
|
||||
|
||||
while (--bufp >= buf) {
|
||||
*(output++) = *bufp;
|
||||
length++;
|
||||
}
|
||||
} else {
|
||||
*(output++) = '0';
|
||||
length++;
|
||||
}
|
||||
|
||||
*output = 0;
|
||||
return length;
|
||||
}
|
||||
|
||||
int CInt64_PrintBin(char *output, CInt64 value) {
|
||||
int length;
|
||||
CInt64 rem;
|
||||
char buf[64];
|
||||
char *bufp;
|
||||
|
||||
length = 0;
|
||||
if (!CInt64_IsZero(&value)) {
|
||||
bufp = buf;
|
||||
for (;;) {
|
||||
rem = CInt64_And(value, cint64_one);
|
||||
if (CInt64_Equal(rem, cint64_one))
|
||||
*(bufp++) = '1';
|
||||
else
|
||||
*(bufp++) = '0';
|
||||
value = CInt64_ShrU(value, cint64_one);
|
||||
if (CInt64_IsZero(&value) != 0)
|
||||
break;
|
||||
}
|
||||
|
||||
while (--bufp >= buf) {
|
||||
*(output++) = *bufp;
|
||||
length++;
|
||||
}
|
||||
} else {
|
||||
*(output++) = '0';
|
||||
length++;
|
||||
}
|
||||
|
||||
*output = 0;
|
||||
return length;
|
||||
}
|
||||
1499
compiler_and_linker/unsorted/CMachine.c
Normal file
1499
compiler_and_linker/unsorted/CMachine.c
Normal file
File diff suppressed because it is too large
Load Diff
2142
compiler_and_linker/unsorted/CPrec.c
Normal file
2142
compiler_and_linker/unsorted/CPrec.c
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user