aboutsummaryrefslogtreecommitdiffhomepage
path: root/src-backend/codegen/codegen_common.h
blob: 545105a0ae595a4f2f9e2c40bb95fb1eef49f247 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290

/*

  codegen_common.h -- Definitions for the code generators

  Copyright © 2022-2024 Samuel Lidén Borell <samuel@kodafritt.se>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/

#ifndef CSBE_CODEGEN_COMMON_H
#define CSBE_CODEGEN_COMMON_H

#include "../csbe_internal.h"
#include "codegen_case.h"


struct SymInfo;

#define CG_CHUNK_SIZE 1024
#define CG_CHUNK_SIZEBITS 10

struct CgCodeChunk {
    struct CgCodeChunk *next;
    unsigned short size;
    unsigned char code[CG_CHUNK_SIZE];
};

/** An address/offset that is not known yet, and will be filled in later.
    This is used for jumps within the compilation unit. */
struct CgAddrSlot {
    struct CgAddrSlot *next;
    struct CgCodeChunk *chunk;
    unsigned offset_in_chunk : CG_CHUNK_SIZEBITS;
    unsigned addrtype : 4;
    uint32 location;
};
#define INVALID_OFFSET ((uint32)-1L)
#define INVALID_OFFSET_BYTE 0xFFU

enum CgRelocKind {
    CG_RK_FUNCCALL,
    CG_RK_FUNCADDR,
    CG_RK_DATAADDR,
    CG_RK_DATALOAD,
    CG_RK_GOTPC
    /* Note: The defines below need to be adjusted when addign values */
};
#define CG_RELOC_KIND_BITS 3
#define CG_RELOC_KIND_MASK 0x7
#define CG_NUM_RELOC_KINDS (CG_RK_GOTPC+1)
#define CG_TARGET_ID_BITS (UINTSIZE-CG_RELOC_KIND_BITS)

#define CG_IS_CODE_RELOC(re) \
        ((re)->kind == CG_RK_FUNCCALL || (re)->kind == CG_RK_FUNCADDR)
#define CG_IS_DATA_RELOC(re) \
        ((re)->kind == CG_RK_DATAADDR || (re)->kind == CG_RK_DATALOAD)

/** A relocation that can't be resolved at code-generation time.
    Used for external symbols. */
struct CgRelocEntry {
    struct CgRelocEntry *next;
    /** Byte offset in the machine code */
    uint32 location;
    /** A CgRelocKind value */
    unsigned kind : CG_RELOC_KIND_BITS;
    /**< ID of the CsbeFuncdef or CsbeDatadef */
    unsigned target_id : CG_TARGET_ID_BITS;
    /**< Addend, if the target platform uses addends */
    int addend;
};

/**< Special values for target_id for built-in functions */
#define CG_SPECIALFUNC(n) \
    ((((unsigned)-1) & ~(CG_RELOC_KIND_MASK<<CG_TARGET_ID_BITS) & ~0xF) | (n))
#define FUNCID_LIBC_INIT CG_SPECIALFUNC(0)
#define CG_IS_SPECIALFUNC(n) ((n) >= FUNCID_LIBC_INIT)

struct CgCode {
    struct CgCodeChunk *chunks, *last_chunk;
    unsigned char *ptr, *end;
    uint32 size;
};

/* XXX consider doing per-architecture register allocation */
struct CgVar {
    unsigned size;
    unsigned stackoffs;
    unsigned align;
    unsigned has_stack_space : 1;
};

struct CgCtx {
    struct Csbe *csbe;
    struct ArchInfo *arch;
    struct CgCode code;
    /* Forward references that will be filled in later: */
    struct CgAddrSlot *jumps_to_epilogue; /**< Jumps to the func epilogue */
    struct CgAddrSlot **relative_jumps; /**< Jumps within the current func */
    struct CgAddrSlot **internal_calls; /**< Jumps to other(later) funcs */
    struct CgAddrSlot *start_helper_calls; /**< Calls to helper from _start */
    /* Offsets in the machine code: */
    uint32 *ebb_offsets;  /**< Offsets to EBB's in current function */
    uint32 *func_offsets; /**< Offsets to each function */
    uint32 startcode_len;
    uint32 helper_offset;
    uint32 got_vaddr; /**< Virtual address of Global Offset Table */
    /* Relocations */
    struct CgRelocEntry *relocs, **reloc_nextptr;
    struct SymInfo *syminfos, **syminfo_nextptr;
    struct SymInfo **sym_by_func_id, *libc_init;
    struct SymInfo **sym_by_data_id;
    unsigned import_sym_count, import_func_count, import_data_count;
    unsigned export_sym_count;
    unsigned rodata_present;
    unsigned num_relocs;

    /* Per-function information */
    struct CgVar *vars;
    uint32 func_stacksize;
    uint32 regsave_stackoffset, regsave_stackend;
    unsigned alloced_calleesaved; /**< Number of alloc'd callee-saved regs */
    unsigned current_param_reg;
    const struct CsbeType *current_paramtype;
    /** 1 if processing the last IR op in a function */
    unsigned is_last_op : 1;
};

#define EBB_ITER(it, f) \
    for (((it).func = (f), (it).ebb = (f)->ebbs, (it).remaining = (f)->num_ebb); \
     (it).remaining--; \
     (it).ebb++)

struct EbbIter {
    const struct FuncIR *func;
    struct Ebb *ebb;
    unsigned remaining;
};

#define IR_OP_ITER(it, ebb_iter) \
    for (cg_ir_op_iter_init(&(it), &(ebb_iter)); \
     cg_ir_op_iter_valid(&(it)); \
     cg_ir_op_iter_next(&(it)))

struct IrOpIter {
    const struct FuncIR *func;
    const struct Op *op;
    unsigned current_num;
    unsigned end_num;
};

#define IS_LAST_OP(ebb_iter, ir_op_iter) \
    (cg_ir_op_iter_is_last(&(ir_op_iter)) && !(ebb_iter).remaining)


#define PRE_EMIT(n) \
    unsigned char *code = cg->code.ptr; \
    if (UNLIKELY(!code || cg->code.end - code < (n))) { \
        code = cg_morespace(cg); \
        if (UNLIKELY(!code)) goto oom; \
    }
#define POST_EMIT(n)        \
    cg->code.ptr = code;

#define IEMIT1(a) do {      \
        PRE_EMIT(1);        \
        *(code++) = (a);    \
        POST_EMIT(1);       \
    } while (0)
#define IEMIT2(a,b) do {    \
        PRE_EMIT(2);        \
        *(code++) = (a);    \
        *(code++) = (b);    \
        POST_EMIT(2);       \
    } while (0)
#define IEMIT3(a,b,c) do {  \
        PRE_EMIT(3);        \
        *(code++) = (a);    \
        *(code++) = (b);    \
        *(code++) = (c);    \
        POST_EMIT(3);       \
    } while (0)
#define IEMIT4(a,b,c,d) do { \
        PRE_EMIT(4);        \
        *(code++) = (a);    \
        *(code++) = (b);    \
        *(code++) = (c);    \
        *(code++) = (d);    \
        POST_EMIT(4);       \
    } while (0)
#define IEMIT5(a,b,c,d,e) do { \
        PRE_EMIT(5);        \
        *(code++) = (a);    \
        *(code++) = (b);    \
        *(code++) = (c);    \
        *(code++) = (d);    \
        *(code++) = (e);    \
        POST_EMIT(5);       \
    } while (0)
#define IEMIT6(a,b,c,d,e,f) do { \
        PRE_EMIT(6);        \
        *(code++) = (a);    \
        *(code++) = (b);    \
        *(code++) = (c);    \
        *(code++) = (d);    \
        *(code++) = (e);    \
        *(code++) = (f);    \
        POST_EMIT(6);       \
    } while (0)
/* Typically used in a "PREALLOC_INSN" macro for each codegen arch */
#define IPREALLOC(size) do { \
        PRE_EMIT(size);     \
    } while (0)

#ifdef CSBE_TRAP_ON_ERROR
#    ifdef NDEBUG
#        error "Can't mix CSBE_TRAP_ON_ERROR and NDEBUG"
#    endif
#    define ZCHK(res) assert(res)
#    define CG_INTERNAL_ERROR() cg_internal_error(cg, __FILE__, __LINE__)
#else
#    define ZCHK(res) if (UNLIKELY(!(res))) return 0
#    define CG_INTERNAL_ERROR() assert(0)
#endif

void cg_ir_op_iter_init(struct IrOpIter *it, struct EbbIter *ebb_iter);
int cg_ir_op_iter_valid(struct IrOpIter *it);
int cg_ir_op_iter_is_last(struct IrOpIter *it);
void cg_ir_op_iter_next(struct IrOpIter *it);
const struct Op *cg_lookahead(struct IrOpIter *it);

void cgcode_reset(struct CgCode *code);
unsigned char *cg_morespace(struct CgCtx *cg);
void cg_lastchunk(struct CgCtx *cg);
uint32 cg_get_position(const struct CgCtx *cg);

void cg_align_stacksize(struct CgCtx *cg, unsigned add, unsigned stackalign);
int cg_stack_alloc_vars(struct CgCtx *cg, const struct FuncIR *f);
int cg_get_type_size(const struct CgCtx *cg, const struct CsbeType *type,
                     unsigned *size_out, unsigned *align_out);
int cg_get_elemsize(const struct CgCtx *cg, const struct CsbeType *type,
                    unsigned *size_out, unsigned *align_out);
int cg_get_elem_offset(const struct CgCtx *cg, const struct CsbeType *type,
                       unsigned index, unsigned fieldnum,
                       unsigned *offset_out);
/** Adds an "address slot", i.e. an address offset that is not known yet.
    This is used for jumps within the compilation unit.
    The "address slot" is added to the following instructions.
    NOTE: PREALLOC_INSN must be called first to pre-alloc the instruction. */
int cg_add_addrslot(struct CgCtx *cg, struct CgAddrSlot **aslist,
                    unsigned addrtype);
/** Edit bytes (little endian) in the machine code */
void cg_edit_bytes_le(struct CgCtx *cg, struct CgAddrSlot *as, int offset,
                      uint64 data, uint32 length);
/** Adds a relocation to the following instructions.
    NOTE: PREALLOC_INSN must be called first to pre-alloc the instruction. */
int cg_add_reloc(struct CgCtx *cg, enum CgRelocKind reloc_kind,
                 unsigned target_sym_id, int addend);
struct SymInfo *cg_helper_func_start(struct CgCtx *cg, const char *symname);
void cg_helper_func_end(struct CgCtx *cg, struct SymInfo *sym);

void cg_internal_error(struct CgCtx *cg, const char *sourcefile, int line);

void irdump_get_codegen(struct Codegen *cgen);
void x86_get_codegen(struct Codegen *cgen);
void riscv_get_codegen(struct Codegen *cgen);
void arm_get_codegen(struct Codegen *cgen);
void aarch64_get_codegen(struct Codegen *cgen);
void mips_get_codegen(struct Codegen *cgen);
void openrisc_get_codegen(struct Codegen *cgen);
void ppc64_get_codegen(struct Codegen *cgen);
void sh4_get_codegen(struct Codegen *cgen);

#endif