Fix a few uses of gassym missed in 9e7e5bff
[qbe.git] / gas.c
blob227c29733f94ac6218e476264eb3c1f1a7fa450d
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"
17 char *p;
19 switch (d->type) {
20 case DStart:
21 align = 0;
22 fprintf(f, ".data\n");
23 break;
24 case DEnd:
25 break;
26 case DName:
27 if (!align)
28 fprintf(f, ".balign 8\n");
29 p = d->u.str[0] == '"' ? "" : gassym;
30 if (d->export)
31 fprintf(f, ".globl %s%s\n", p, d->u.str);
32 fprintf(f, "%s%s:\n", p, d->u.str);
33 break;
34 case DZ:
35 fprintf(f, "\t.fill %"PRId64",1,0\n", d->u.num);
36 break;
37 default:
38 if (d->type == DAlign)
39 align = 1;
41 if (d->isstr) {
42 if (d->type != DB)
43 err("strings only supported for 'b' currently");
44 fprintf(f, "\t.ascii %s\n", d->u.str);
46 else if (d->isref) {
47 p = d->u.ref.nam[0] == '"' ? "" : gassym;
48 fprintf(f, "%s %s%s%+"PRId64"\n",
49 dtoa[d->type], p, d->u.ref.nam,
50 d->u.ref.off);
52 else {
53 fprintf(f, "%s %"PRId64"\n",
54 dtoa[d->type], d->u.num);
56 break;
60 typedef struct Asmbits Asmbits;
62 struct Asmbits {
63 char bits[16];
64 int size;
65 Asmbits *link;
68 static Asmbits *stash;
70 int
71 gasstash(void *bits, int size)
73 Asmbits **pb, *b;
74 int i;
76 assert(size == 4 || size == 8 || size == 16);
77 for (pb=&stash, i=0; (b=*pb); pb=&b->link, i++)
78 if (size <= b->size)
79 if (memcmp(bits, b->bits, size) == 0)
80 return i;
81 b = emalloc(sizeof *b);
82 memcpy(b->bits, bits, size);
83 b->size = size;
84 b->link = 0;
85 *pb = b;
86 return i;
89 void
90 gasemitfin(FILE *f)
92 Asmbits *b;
93 char *p;
94 int sz, i;
95 double d;
97 if (!stash)
98 return;
99 fprintf(f, "/* floating point constants */\n.data\n");
100 for (sz=16; sz>=4; sz/=2)
101 for (b=stash, i=0; b; b=b->link, i++) {
102 if (b->size == sz) {
103 fprintf(f,
104 ".balign %d\n"
105 "%sfp%d:",
106 sz, gasloc, i
108 for (p=b->bits; p<&b->bits[sz]; p+=4)
109 fprintf(f, "\n\t.int %"PRId32,
110 *(int32_t *)p);
111 if (sz <= 8) {
112 if (sz == 4)
113 d = *(float *)b->bits;
114 else
115 d = *(double *)b->bits;
116 fprintf(f, " /* %f */\n", d);
117 } else
118 fprintf(f, "\n");
121 while ((b=stash)) {
122 stash = b->link;
123 free(b);