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
[] = {
80 die("invalid common data definition");
81 p
= d
->name
[0] == '"' ? "" : T
.assym
;
82 fprintf(f
, ".comm %s%s,%"PRId64
,
85 fprintf(f
, ",%d", d
->lnk
->align
);
88 else if (zero
!= -1) {
89 emitlnk(d
->name
, d
->lnk
, SecBss
, f
);
90 fprintf(f
, "\t.fill %"PRId64
",1,0\n", zero
);
97 fprintf(f
, "\t.fill %"PRId64
",1,0\n", d
->u
.num
);
101 emitlnk(d
->name
, d
->lnk
, SecData
, f
);
103 fprintf(f
, "\t.fill %"PRId64
",1,0\n", zero
);
108 err("strings only supported for 'b' currently");
109 fprintf(f
, "\t.ascii %s\n", d
->u
.str
);
112 p
= d
->u
.ref
.name
[0] == '"' ? "" : T
.assym
;
113 fprintf(f
, "%s %s%s%+"PRId64
"\n",
114 dtoa
[d
->type
], p
, d
->u
.ref
.name
,
118 fprintf(f
, "%s %"PRId64
"\n",
119 dtoa
[d
->type
], d
->u
.num
);
125 typedef struct Asmbits Asmbits
;
133 static Asmbits
*stash
;
136 stashbits(void *bits
, int size
)
141 assert(size
== 4 || size
== 8 || size
== 16);
142 for (pb
=&stash
, i
=0; (b
=*pb
); pb
=&b
->link
, i
++)
144 if (memcmp(bits
, b
->bits
, size
) == 0)
146 b
= emalloc(sizeof *b
);
147 memcpy(b
->bits
, bits
, size
);
155 emitfin(FILE *f
, char *sec
[3])
164 fprintf(f
, "/* floating point constants */\n");
165 for (lg
=4; lg
>=2; lg
--)
166 for (b
=stash
, i
=0; b
; b
=b
->link
, i
++) {
167 if (b
->size
== (1<<lg
)) {
172 sec
[lg
-2], lg
, T
.asloc
, i
174 for (p
=b
->bits
; p
<&b
->bits
[b
->size
]; p
+=4)
175 fprintf(f
, "\n\t.int %"PRId32
,
179 d
= *(float *)b
->bits
;
181 d
= *(double *)b
->bits
;
182 fprintf(f
, " /* %f */\n\n", d
);
196 static char *sec
[3] = { ".rodata", ".rodata", ".rodata" };
199 fprintf(f
, ".section .note.GNU-stack,\"\",@progbits\n");
203 elf_emitfnfin(char *fn
, FILE *f
)
205 fprintf(f
, ".type %s, @function\n", fn
);
206 fprintf(f
, ".size %s, .-%s\n", fn
, fn
);
210 macho_emitfin(FILE *f
)
212 static char *sec
[3] = {
213 "__TEXT,__literal4,4byte_literals",
214 "__TEXT,__literal8,8byte_literals",
215 ".abort \"unreachable\"",
221 static uint32_t *file
;
226 emitdbgfile(char *fn
, FILE *f
)
232 for (n
=0; n
<nfile
; n
++)
234 /* gas requires positive
240 file
= vnew(0, sizeof *file
, PHeap
);
241 vgrow(&file
, ++nfile
);
244 fprintf(f
, ".file %u %s\n", curfile
, fn
);
248 emitdbgloc(uint line
, uint col
, FILE *f
)
251 fprintf(f
, "\t.loc %u %u %u\n", curfile
, line
, col
);
253 fprintf(f
, "\t.loc %u %u\n", curfile
, line
);