Browse Source

fix fp constants on big endian hosts

Quentin Carbonneaux 8 months ago
parent
commit
8d5b86ac4c
6 changed files with 30 additions and 25 deletions
  1. 1 1
      all.h
  2. 3 3
      amd64/emit.c
  3. 1 1
      amd64/isel.c
  4. 1 1
      arm64/isel.c
  5. 23 18
      emit.c
  6. 1 1
      rv64/isel.c

+ 1 - 1
all.h

@@ -614,7 +614,7 @@ void emitfnlnk(char *, Lnk *, FILE *);
 void emitdat(Dat *, FILE *);
 void emitdbgfile(char *, FILE *);
 void emitdbgloc(uint, uint, FILE *);
-int stashbits(void *, int);
+int stashbits(bits, int);
 void elf_emitfnfin(char *, FILE *);
 void elf_emitfin(FILE *);
 void macho_emitfin(FILE *);

+ 3 - 3
amd64/emit.c

@@ -384,9 +384,9 @@ Next:
 	goto Next;
 }
 
-static void *negmask[4] = {
-	[Ks] = (uint32_t[4]){ 0x80000000 },
-	[Kd] = (uint64_t[2]){ 0x8000000000000000 },
+static bits negmask[4] = {
+	[Ks] = 0x80000000,
+	[Kd] = 0x8000000000000000,
 };
 
 static void

+ 1 - 1
amd64/isel.c

@@ -87,7 +87,7 @@ fixarg(Ref *r, int k, Ins *i, Fn *fn)
 		vgrow(&fn->mem, ++fn->nmem);
 		memset(&a, 0, sizeof a);
 		a.offset.type = CAddr;
-		n = stashbits(&fn->con[r0.val].bits, KWIDE(k) ? 8 : 4);
+		n = stashbits(fn->con[r0.val].bits.i, KWIDE(k) ? 8 : 4);
 		/* quote the name so that we do not
 		 * add symbol prefixes on the apple
 		 * target variant

+ 1 - 1
arm64/isel.c

@@ -109,7 +109,7 @@ fixarg(Ref *pr, int k, int phi, Fn *fn)
 		if (KBASE(k) == 0) {
 			emit(Ocopy, k, r1, r0, R);
 		} else {
-			n = stashbits(&c->bits, KWIDE(k) ? 8 : 4);
+			n = stashbits(c->bits.i, KWIDE(k) ? 8 : 4);
 			vgrow(&fn->con, ++fn->ncon);
 			c = &fn->con[fn->ncon-1];
 			sprintf(buf, "\"%sfp%d\"", T.asloc, n);

+ 23 - 18
emit.c

@@ -125,7 +125,7 @@ emitdat(Dat *d, FILE *f)
 typedef struct Asmbits Asmbits;
 
 struct Asmbits {
-	char bits[16];
+	bits n;
 	int size;
 	Asmbits *link;
 };
@@ -133,18 +133,17 @@ struct Asmbits {
 static Asmbits *stash;
 
 int
-stashbits(void *bits, int size)
+stashbits(bits n, int size)
 {
 	Asmbits **pb, *b;
 	int i;
 
 	assert(size == 4 || size == 8 || size == 16);
 	for (pb=&stash, i=0; (b=*pb); pb=&b->link, i++)
-		if (size <= b->size)
-		if (memcmp(bits, b->bits, size) == 0)
+		if (size <= b->size && b->n == n)
 			return i;
 	b = emalloc(sizeof *b);
-	memcpy(b->bits, bits, size);
+	b->n = n;
 	b->size = size;
 	b->link = 0;
 	*pb = b;
@@ -155,9 +154,8 @@ static void
 emitfin(FILE *f, char *sec[3])
 {
 	Asmbits *b;
-	char *p;
 	int lg, i;
-	double d;
+	union { int32_t i; float f; } u;
 
 	if (!stash)
 		return;
@@ -171,17 +169,24 @@ emitfin(FILE *f, char *sec[3])
 					"%sfp%d:",
 					sec[lg-2], lg, T.asloc, i
 				);
-				for (p=b->bits; p<&b->bits[b->size]; p+=4)
-					fprintf(f, "\n\t.int %"PRId32,
-						*(int32_t *)p);
-				if (lg <= 3) {
-					if (lg == 2)
-						d = *(float *)b->bits;
-					else
-						d = *(double *)b->bits;
-					fprintf(f, " /* %f */\n\n", d);
-				} else
-					fprintf(f, "\n\n");
+				if (lg == 4)
+					fprintf(f,
+						"\n\t.quad %"PRId64
+						"\n\t.quad 0\n\n",
+						(int64_t)b->n);
+				else if (lg == 3)
+					fprintf(f,
+						"\n\t.quad %"PRId64
+						" /* %f */\n\n",
+						(int64_t)b->n,
+						*(double *)&b->n);
+				else if (lg == 2) {
+					u.i = b->n;
+					fprintf(f,
+						"\n\t.int %"PRId32
+						" /* %f */\n\n",
+						u.i, (double)u.f);
+				}
 			}
 		}
 	while ((b=stash)) {

+ 1 - 1
rv64/isel.c

@@ -41,7 +41,7 @@ fixarg(Ref *r, int k, Ins *i, Fn *fn)
 			 * immediates
 			 */
 			assert(c->type == CBits);
-			n = stashbits(&c->bits, KWIDE(k) ? 8 : 4);
+			n = stashbits(c->bits.i, KWIDE(k) ? 8 : 4);
 			vgrow(&fn->con, ++fn->ncon);
 			c = &fn->con[fn->ncon-1];
 			sprintf(buf, "\"%sfp%d\"", T.asloc, n);