10 emitlnk(char *n
, Lnk
*l
, int s
, FILE *f
)
12 static char *sec
[2][3] = {
13 [0][SecText
] = ".text",
14 [0][SecData
] = ".data",
16 [1][SecText
] = ".abort \"unreachable\"",
17 [1][SecData
] = ".section .tdata,\"awT\"",
18 [1][SecBss
] = ".section .tbss,\"awT\"",
22 pfx
= n
[0] == '"' ? "" : T
.assym
;
24 if (T
.apple
&& l
->thread
) {
26 l
->secf
= "__thread_data,thread_local_regular";
29 ".section __DATA,__thread_vars,"
30 "thread_local_variables\n",
33 fprintf(f
, "%s%s:\n", pfx
, n
);
35 "\t.quad __tlv_bootstrap\n"
42 fprintf(f
, ".section %s", l
->sec
);
44 fprintf(f
, ",%s", l
->secf
);
46 fputs(sec
[l
->thread
!= 0][s
], f
);
49 fprintf(f
, ".balign %d\n", l
->align
);
51 fprintf(f
, ".globl %s%s\n", pfx
, n
);
52 fprintf(f
, "%s%s%s:\n", pfx
, n
, sfx
);
56 emitfnlnk(char *n
, Lnk
*l
, FILE *f
)
58 emitlnk(n
, l
, SecText
, f
);
62 emitdat(Dat
*d
, FILE *f
)
64 static char *dtoa
[] = {
79 emitlnk(d
->name
, d
->lnk
, SecBss
, f
);
80 fprintf(f
, "\t.fill %"PRId64
",1,0\n", zero
);
87 fprintf(f
, "\t.fill %"PRId64
",1,0\n", d
->u
.num
);
91 emitlnk(d
->name
, d
->lnk
, SecData
, f
);
93 fprintf(f
, "\t.fill %"PRId64
",1,0\n", zero
);
98 err("strings only supported for 'b' currently");
99 fprintf(f
, "\t.ascii %s\n", d
->u
.str
);
102 p
= d
->u
.ref
.name
[0] == '"' ? "" : T
.assym
;
103 fprintf(f
, "%s %s%s%+"PRId64
"\n",
104 dtoa
[d
->type
], p
, d
->u
.ref
.name
,
108 fprintf(f
, "%s %"PRId64
"\n",
109 dtoa
[d
->type
], d
->u
.num
);
115 typedef struct Asmbits Asmbits
;
123 static Asmbits
*stash
;
126 stashbits(void *bits
, int size
)
131 assert(size
== 4 || size
== 8 || size
== 16);
132 for (pb
=&stash
, i
=0; (b
=*pb
); pb
=&b
->link
, i
++)
134 if (memcmp(bits
, b
->bits
, size
) == 0)
136 b
= emalloc(sizeof *b
);
137 memcpy(b
->bits
, bits
, size
);
145 emitfin(FILE *f
, char *sec
[3])
154 fprintf(f
, "/* floating point constants */\n");
155 for (lg
=4; lg
>=2; lg
--)
156 for (b
=stash
, i
=0; b
; b
=b
->link
, i
++) {
157 if (b
->size
== (1<<lg
)) {
162 sec
[lg
-2], lg
, T
.asloc
, i
164 for (p
=b
->bits
; p
<&b
->bits
[b
->size
]; p
+=4)
165 fprintf(f
, "\n\t.int %"PRId32
,
169 d
= *(float *)b
->bits
;
171 d
= *(double *)b
->bits
;
172 fprintf(f
, " /* %f */\n\n", d
);
186 static char *sec
[3] = { ".rodata", ".rodata", ".rodata" };
189 fprintf(f
, ".section .note.GNU-stack,\"\",@progbits\n");
193 elf_emitfnfin(char *fn
, FILE *f
)
195 fprintf(f
, ".type %s, @function\n", fn
);
196 fprintf(f
, ".size %s, .-%s\n", fn
, fn
);
200 macho_emitfin(FILE *f
)
202 static char *sec
[3] = {
203 "__TEXT,__literal4,4byte_literals",
204 "__TEXT,__literal8,8byte_literals",
205 ".abort \"unreachable\"",
211 static uint32_t *file
;
216 emitdbgfile(char *fn
, FILE *f
)
222 for (n
=0; n
<nfile
; n
++)
224 /* gas requires positive
230 file
= vnew(0, sizeof *file
, PHeap
);
231 vgrow(&file
, ++nfile
);
234 fprintf(f
, ".file %u %s\n", curfile
, fn
);
238 emitdbgloc(uint line
, uint col
, FILE *f
)
241 fprintf(f
, "\t.loc %u %u %u\n", curfile
, line
, col
);
243 fprintf(f
, "\t.loc %u %u\n", curfile
, line
);