scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit b83ec8ed7ab5e20a968b8e1a00236036ea363131
parent f41102032108467135842e9f36c4dfd1072c4d62
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date:   Fri, 30 Jan 2026 21:37:48 +0100

cc1: Use type punning to pass floats to cc2

Doesn't mind what we do that we cannot pass a floating point
number from cc1 to cc2 in a fully portable way, and using
type punning at least maintain the endianess correctly. This
code should even work in cross compilation as far as the host
and the target use the same floating point representation,
that today will likely be the ieee 754.

Diffstat:
Msrc/cmd/scc-cc/cc1/code.c | 23+++++++++--------------
Msrc/cmd/scc-cc/cc2/cc2.h | 1-
Msrc/cmd/scc-cc/cc2/qbe/cgen.c | 2+-
Mtests/cc/execute/scc-tests.lst | 6+++---
4 files changed, 13 insertions(+), 19 deletions(-)

diff --git a/src/cmd/scc-cc/cc1/code.c b/src/cmd/scc-cc/cc1/code.c @@ -222,29 +222,24 @@ emitconst(Node *np) unsigned long long u; switch (tp->op) { + case FLOAT: + if (tp == floattype) + u = *(uint32_t *) &sym->u.f; + else if (tp == doubletype) + u = *(uint64_t *) &sym->u.d; + else + abort(); /* TODO: What here?? */ + goto print; case PTR: case INT: case ENUM: u = (tp->prop & TSIGNED) ? sym->u.i : sym->u.u; + print: fprintf(outfp, "#%c%llX", tp->letter, u & ones(tp->size)); break; - case FLOAT: - fp = (unsigned char *) &sym->u.f; - - if (tp == floattype) - n = sizeof(sym->u.f); - else if (tp == doubletype) - n = sizeof(sym->u.d); - else - n = sizeof(sym->u.ld); - - fprintf(outfp, "#%c", tp->letter); - for (p = fp; p < &fp[n]; ++p) - fprintf(outfp, "%02X", *p); - break; default: abort(); } diff --git a/src/cmd/scc-cc/cc2/cc2.h b/src/cmd/scc-cc/cc2/cc2.h @@ -213,7 +213,6 @@ struct node { unsigned flags; union { unsigned long long i; - double f; char *s; Symbol *sym; Swtch *swtch; diff --git a/src/cmd/scc-cc/cc2/qbe/cgen.c b/src/cmd/scc-cc/cc2/qbe/cgen.c @@ -568,7 +568,7 @@ rhs(Node *np) isfloat = (tp->flags & FLOATF) != 0; op = opbin[isfloat][size][np->op][sign]; rhs_rhs(&l, &r); - tmp = tmpnode(tp, NULL); + tmp = tmpnode(&int32type, NULL); code(op, tmp, l, r); return tmp; case OCALL: diff --git a/tests/cc/execute/scc-tests.lst b/tests/cc/execute/scc-tests.lst @@ -106,18 +106,18 @@ 0113-externredecl.c 0114-shortassig.c 0115-null_comparision.c -0116-floatcmp.c [TODO] +0116-floatcmp.c 0117-pointarith.c 0118-voidmain.c 0119-macrostr.c 0120-funpar.c 0121-localinit.c 0122-localinit.c -0123-doubleconst.c [TODO] +0123-doubleconst.c 0124-enumstruct.c 0125-fundcl.c 0126-macropar.c -0127-doublecte.c [TODO] +0127-doublecte.c 0128-kr_names.c 0129-initi.c 0130-mulpars.c