4 emitlnk(char *n
, Lnk
*l
, char *s
, FILE *f
)
9 fprintf(f
, ".section %s", l
->sec
);
11 fprintf(f
, ", %s", l
->secf
);
17 fprintf(f
, ".balign %d\n", l
->align
);
18 p
= n
[0] == '"' ? "" : T
.assym
;
20 fprintf(f
, ".globl %s%s\n", p
, n
);
21 fprintf(f
, "%s%s:\n", p
, n
);
25 emitdat(Dat
*d
, FILE *f
)
27 static char *dtoa
[] = {
42 emitlnk(d
->name
, d
->lnk
, ".bss", f
);
43 fprintf(f
, "\t.fill %"PRId64
",1,0\n", zero
);
50 fprintf(f
, "\t.fill %"PRId64
",1,0\n", d
->u
.num
);
54 emitlnk(d
->name
, d
->lnk
, ".data", f
);
56 fprintf(f
, "\t.fill %"PRId64
",1,0\n", zero
);
61 err("strings only supported for 'b' currently");
62 fprintf(f
, "\t.ascii %s\n", d
->u
.str
);
65 p
= d
->u
.ref
.name
[0] == '"' ? "" : T
.assym
;
66 fprintf(f
, "%s %s%s%+"PRId64
"\n",
67 dtoa
[d
->type
], p
, d
->u
.ref
.name
,
71 fprintf(f
, "%s %"PRId64
"\n",
72 dtoa
[d
->type
], d
->u
.num
);
78 typedef struct Asmbits Asmbits
;
86 static Asmbits
*stash
;
89 stashbits(void *bits
, int size
)
94 assert(size
== 4 || size
== 8 || size
== 16);
95 for (pb
=&stash
, i
=0; (b
=*pb
); pb
=&b
->link
, i
++)
97 if (memcmp(bits
, b
->bits
, size
) == 0)
99 b
= emalloc(sizeof *b
);
100 memcpy(b
->bits
, bits
, size
);
108 emitfin(FILE *f
, char *sec
[3])
117 fprintf(f
, "/* floating point constants */\n");
118 for (lg
=4; lg
>=2; lg
--)
119 for (b
=stash
, i
=0; b
; b
=b
->link
, i
++) {
120 if (b
->size
== (1<<lg
)) {
125 sec
[lg
-2], lg
, T
.asloc
, i
127 for (p
=b
->bits
; p
<&b
->bits
[b
->size
]; p
+=4)
128 fprintf(f
, "\n\t.int %"PRId32
,
132 d
= *(float *)b
->bits
;
134 d
= *(double *)b
->bits
;
135 fprintf(f
, " /* %f */\n\n", d
);
149 static char *sec
[3] = { ".rodata", ".rodata", ".rodata" };
152 fprintf(f
, ".section .note.GNU-stack,\"\",@progbits\n");
156 elf_emitfnfin(char *fn
, FILE *f
)
158 fprintf(f
, ".type %s, @function\n", fn
);
159 fprintf(f
, ".size %s, .-%s\n", fn
, fn
);
163 macho_emitfin(FILE *f
)
165 static char *sec
[3] = {
166 "__TEXT,__literal4,4byte_literals",
167 "__TEXT,__literal8,8byte_literals",
168 ".rodata", /* should not happen */