revert last commit
[qbe.git] / gas.c
blob8e2f04c6c82e44c18bd5fd56760b7e5ba4c6c472
1 #include "all.h"
4 char *gasloc, *gassym;
6 void
7 gasemitdat(Dat *d, FILE *f)
9 static int align;
10 static char *dtoa[] = {
11 [DAlign] = ".balign",
12 [DB] = "\t.byte",
13 [DH] = "\t.short",
14 [DW] = "\t.int",
15 [DL] = "\t.quad"
18 switch (d->type) {
19 case DStart:
20 align = 0;
21 fprintf(f, ".data\n");
22 break;
23 case DEnd:
24 break;
25 case DName:
26 if (!align)
27 fprintf(f, ".balign 8\n");
28 if (d->export)
29 fprintf(f, ".globl %s%s\n", gassym, d->u.str);
30 fprintf(f, "%s%s:\n", gassym, d->u.str);
31 break;
32 case DZ:
33 fprintf(f, "\t.fill %"PRId64",1,0\n", d->u.num);
34 break;
35 default:
36 if (d->type == DAlign)
37 align = 1;
39 if (d->isstr) {
40 if (d->type != DB)
41 err("strings only supported for 'b' currently");
42 fprintf(f, "\t.ascii \"%s\"\n", d->u.str);
44 else if (d->isref) {
45 fprintf(f, "%s %s%s%+"PRId64"\n",
46 dtoa[d->type], gassym, d->u.ref.nam,
47 d->u.ref.off);
49 else {
50 fprintf(f, "%s %"PRId64"\n",
51 dtoa[d->type], d->u.num);
53 break;
57 typedef struct Asmbits Asmbits;
59 struct Asmbits {
60 char bits[16];
61 int size;
62 Asmbits *link;
65 static Asmbits *stash;
67 int
68 gasstash(void *bits, int size)
70 Asmbits **pb, *b;
71 int i;
73 assert(size == 4 || size == 8 || size == 16);
74 for (pb=&stash, i=0; (b=*pb); pb=&b->link, i++)
75 if (size <= b->size)
76 if (memcmp(bits, b->bits, size) == 0)
77 return i;
78 b = emalloc(sizeof *b);
79 memcpy(b->bits, bits, size);
80 b->size = size;
81 b->link = 0;
82 *pb = b;
83 return i;
86 void
87 gasemitfin(FILE *f)
89 Asmbits *b;
90 char *p;
91 int sz, i;
92 double d;
94 if (!stash)
95 return;
96 fprintf(f, "/* floating point constants */\n.data\n");
97 for (sz=16; sz>=4; sz/=2)
98 for (b=stash, i=0; b; b=b->link, i++) {
99 if (b->size == sz) {
100 fprintf(f,
101 ".balign %d\n"
102 "%sfp%d:",
103 sz, gasloc, i
105 for (p=b->bits; p<&b->bits[sz]; p+=4)
106 fprintf(f, "\n\t.int %"PRId32,
107 *(int32_t *)p);
108 if (sz <= 8) {
109 if (sz == 4)
110 d = *(float *)b->bits;
111 else
112 d = *(double *)b->bits;
113 fprintf(f, " /* %f */\n", d);
114 } else
115 fprintf(f, "\n");
118 while ((b=stash)) {
119 stash = b->link;
120 free(b);