x86: x87 FPU may be used as fallback for operations that are not
[ajla.git] / cg-ptr.inc
blob283537a693fa238c4d7c9d7f40708fe9a909a776
1 /*
2  * Copyright (C) 2024 Mikulas Patocka
3  *
4  * This file is part of Ajla.
5  *
6  * Ajla is free software: you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation, either version 3 of the License, or (at your option) any later
9  * version.
10  *
11  * Ajla is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13  * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * Ajla. If not, see <https://www.gnu.org/licenses/>.
17  */
19 #if defined(POINTER_COMPRESSION)
20 #define POINTER_THUNK_BIT               0
21 #elif defined(POINTER_IGNORE_START)
22 #define POINTER_THUNK_BIT               POINTER_IGNORE_TOP_BIT
23 #elif defined(POINTER_TAG)
24 #define POINTER_THUNK_BIT               POINTER_TAG_BIT
25 #else
26 unsupported pointer mode
27 #endif
29 static bool attr_w gen_ptr_is_thunk(struct codegen_context *ctx, unsigned reg, frame_t slot, uint32_t label)
31         if (slot != NO_FRAME_T) {
32                 if (ctx->flag_cache[slot] & FLAG_CACHE_IS_NOT_THUNK)
33                         return true;
34                 ctx->flag_cache[slot] |= FLAG_CACHE_IS_NOT_THUNK;
35         }
36 #if defined(ARCH_X86)
37         if (POINTER_THUNK_BIT < 8
38 #if defined(ARCH_X86_32)
39                 && reg < 4
40 #endif
41                 ) {
42                 g(gen_cmp_test_imm_jmp(ctx, INSN_TEST, OP_SIZE_1, reg, (uint64_t)1 << POINTER_THUNK_BIT, COND_NE, label));
43         } else
44 #endif
45         {
46                 g(gen_cmp_test_imm_jmp(ctx, INSN_TEST, OP_SIZE_SLOT, reg, (uint64_t)1 << POINTER_THUNK_BIT, COND_NE, label));
47         }
48         return true;
51 static bool attr_w gen_barrier(struct codegen_context *ctx)
53         if (ARCH_NEEDS_BARRIER)
54                 gen_insn(INSN_MB, 0, 0, 0);
55         return true;
58 static bool attr_w gen_compare_refcount(struct codegen_context *ctx, unsigned ptr, unsigned val, unsigned cond, uint32_t label)
60         unsigned op_size = log_2(sizeof(refcount_int_t));
61 #if defined(ARCH_X86)
62         bool logical = COND_IS_LOGICAL(cond);
63         g(gen_address(ctx, ptr, offsetof(struct data, refcount_), IMM_PURPOSE_LDR_OFFSET, op_size));
64         g(gen_imm(ctx, val, IMM_PURPOSE_CMP, op_size));
65         gen_insn(INSN_CMP, op_size, 0, 1 + logical);
66         gen_address_offset();
67         gen_imm_offset();
69         gen_insn(!logical ? INSN_JMP_COND : INSN_JMP_COND_LOGICAL, op_size, cond, 0);
70         gen_four(label);
71 #else
72         g(gen_address(ctx, ptr, offsetof(struct data, refcount_), IMM_PURPOSE_LDR_OFFSET, op_size));
73         gen_insn(INSN_MOV, op_size, 0, 0);
74         gen_one(R_SCRATCH_2);
75         gen_address_offset();
77         g(gen_cmp_test_imm_jmp(ctx, INSN_CMP, op_size, R_SCRATCH_2, val, cond, label));
78 #endif
79         return true;
82 static bool attr_w gen_compare_ptr_tag(struct codegen_context *ctx, unsigned reg, unsigned tag, unsigned cond, uint32_t label, unsigned tmp_reg)
84 #if defined(ARCH_S390)
85         switch (cond) {
86                 case COND_A:    cond = COND_G; break;
87                 case COND_AE:   cond = COND_GE; break;
88                 case COND_B:    cond = COND_L; break;
89                 case COND_BE:   cond = COND_LE; break;
90         }
91 #endif
92 #if defined(DATA_TAG_AT_ALLOC)
93         g(gen_3address_rot_imm(ctx, OP_SIZE_ADDRESS, ROT_SHR, tmp_reg, reg, POINTER_IGNORE_START, false));
94 #elif defined(REFCOUNT_TAG)
95         size_t offset = offsetof(struct data, refcount_);
96 #if defined(C_BIG_ENDIAN)
97         offset += sizeof(refcount_t) - 1;
98 #endif
99 #if defined(ARCH_X86) && REFCOUNT_STEP == 256
100         g(gen_imm(ctx, tag, IMM_PURPOSE_CMP, OP_SIZE_4));
101         gen_insn(INSN_CMP, OP_SIZE_1, 0, 1);
102         gen_one(ARG_ADDRESS_1);
103         gen_one(reg);
104         gen_eight(offset);
105         gen_imm_offset();
107         gen_insn(INSN_JMP_COND, OP_SIZE_1, cond, 0);
108         gen_four(label);
109         return true;
110 #endif
111         if (ARCH_HAS_BWX && REFCOUNT_STEP == 256) {
112                 gen_insn(INSN_MOV, OP_SIZE_1, 0, 0);
113                 gen_one(tmp_reg);
114                 gen_one(ARG_ADDRESS_1);
115                 gen_one(reg);
116                 gen_eight(offset);
117         } else {
118                 gen_insn(INSN_MOV, log_2(sizeof(refcount_int_t)), 0, 0);
119                 gen_one(tmp_reg);
120                 gen_one(ARG_ADDRESS_1);
121                 gen_one(reg);
122                 gen_eight(offset);
124                 g(gen_3address_alu_imm(ctx, log_2(sizeof(refcount_int_t)), ALU_AND, tmp_reg, tmp_reg, REFCOUNT_STEP - 1, 0));
125         }
126 #else
127 #if defined(ARCH_S390)
128         if (sizeof(tag_t) == 1 && !cpu_test_feature(CPU_FEATURE_long_displacement)) {
129                 g(gen_address(ctx, reg, offsetof(struct data, tag), IMM_PURPOSE_LDR_OFFSET, log_2(sizeof(tag_t))));
130                 gen_insn(INSN_MOV_MASK, OP_SIZE_NATIVE, MOV_MASK_0_8, 0);
131                 gen_one(tmp_reg);
132                 gen_one(tmp_reg);
133                 gen_address_offset();
135                 g(gen_extend(ctx, log_2(sizeof(tag_t)), zero_x, tmp_reg, tmp_reg));
136         } else
137 #endif
138         {
139                 g(gen_address(ctx, reg, offsetof(struct data, tag), IMM_PURPOSE_LDR_OFFSET, log_2(sizeof(tag_t))));
140                 gen_insn(INSN_MOV, log_2(sizeof(tag_t)), 0, 0);
141                 gen_one(tmp_reg);
142                 gen_address_offset();
143         }
144 #endif
145         g(gen_cmp_test_imm_jmp(ctx, INSN_CMP, i_size(OP_SIZE_4), tmp_reg, tag, cond, label));
146         return true;
149 static bool attr_w gen_compare_da_tag(struct codegen_context *ctx, unsigned reg, unsigned tag, unsigned cond, uint32_t label, unsigned tmp_reg)
151 #if defined(ARCH_S390)
152         switch (cond) {
153                 case COND_A:    cond = COND_G; break;
154                 case COND_AE:   cond = COND_GE; break;
155                 case COND_B:    cond = COND_L; break;
156                 case COND_BE:   cond = COND_LE; break;
157         }
158 #endif
159 #if defined(POINTER_COMPRESSION)
160 #if defined(ARCH_X86) && POINTER_COMPRESSION <= 3 && defined(REFCOUNT_TAG) && REFCOUNT_STEP == 256 && defined(C_LITTLE_ENDIAN)
161         g(gen_imm(ctx, tag, IMM_PURPOSE_CMP, log_2(sizeof(tag_t))));
162         gen_insn(INSN_CMP, log_2(sizeof(tag_t)), 0, 0);
163         gen_one(ARG_ADDRESS_1 + POINTER_COMPRESSION);
164         gen_one(reg);
165         gen_eight(offsetof(struct data, refcount_));
166         gen_imm_offset();
168         gen_insn(INSN_JMP_COND, OP_SIZE_4, cond, 0);
169         gen_four(label);
171         return true;
172 #endif
173         if (ARCH_PREFERS_SX(OP_SIZE_4)) {
174                 g(gen_extend(ctx, OP_SIZE_4, zero_x, tmp_reg, reg));
176                 g(gen_3address_rot_imm(ctx, OP_SIZE_ADDRESS, ROT_SHL, tmp_reg, tmp_reg, POINTER_COMPRESSION, false));
177         } else {
178                 g(gen_3address_rot_imm(ctx, OP_SIZE_ADDRESS, ROT_SHL, tmp_reg, reg, POINTER_COMPRESSION, false));
179         }
180         g(gen_compare_ptr_tag(ctx, tmp_reg, tag, cond, label, tmp_reg));
181         return true;
182 #endif
183         g(gen_compare_ptr_tag(ctx, reg, tag, cond, label, tmp_reg));
184         return true;
187 static bool attr_w gen_compare_tag_and_refcount(struct codegen_context *ctx, unsigned reg, unsigned tag, uint32_t label, unsigned attr_unused tmp_reg)
189 #if defined(REFCOUNT_TAG)
190         g(gen_compare_refcount(ctx, reg, tag, COND_NE, label));
191 #else
192         g(gen_compare_ptr_tag(ctx, reg, tag, COND_NE, label, tmp_reg));
193         g(gen_compare_refcount(ctx, reg, REFCOUNT_STEP, COND_AE, label));
194 #endif
195         return true;
198 static bool attr_w gen_decompress_pointer(struct codegen_context *ctx, bool attr_unused zx, unsigned reg, int64_t offset)
200 #ifdef POINTER_COMPRESSION
201 #if defined(ARCH_X86) && POINTER_COMPRESSION <= 3
202         if (offset) {
203                 g(gen_imm(ctx, offset, IMM_PURPOSE_ADD, i_size(OP_SIZE_ADDRESS)));
204                 gen_insn(INSN_ALU, i_size(OP_SIZE_ADDRESS), ALU_ADD, ALU_WRITES_FLAGS(ALU_ADD, is_imm()));
205                 gen_one(reg);
206                 gen_one(ARG_SHIFTED_REGISTER);
207                 gen_one(ARG_SHIFT_LSL | POINTER_COMPRESSION);
208                 gen_one(reg);
209                 gen_imm_offset();
210                 return true;
211         }
212 #endif
213         if (zx)
214                 g(gen_extend(ctx, OP_SIZE_4, zero_x, reg, reg));
215         g(gen_3address_rot_imm(ctx, OP_SIZE_ADDRESS, ROT_SHL, reg, reg, POINTER_COMPRESSION, false));
216 #endif
217         if (offset)
218                 g(gen_3address_alu_imm(ctx, i_size(OP_SIZE_ADDRESS), ALU_ADD, reg, reg, offset, 0));
219         return true;
222 static bool attr_w gen_compress_pointer(struct codegen_context attr_unused *ctx, unsigned attr_unused reg)
224 #ifdef POINTER_COMPRESSION
225         g(gen_3address_rot_imm(ctx, OP_SIZE_ADDRESS, ROT_SHR, reg, reg, POINTER_COMPRESSION, false));
226 #endif
227         return true;
230 static bool attr_w gen_frame_get_pointer(struct codegen_context *ctx, frame_t slot, bool deref, unsigned dest)
232         if (!deref) {
233                 g(gen_upcall_start(ctx, 1));
234                 g(gen_frame_load(ctx, OP_SIZE_SLOT, garbage, slot, 0, R_ARG0));
235                 g(gen_upcall_argument(ctx, 0));
236                 g(gen_upcall(ctx, offsetof(struct cg_upcall_vector_s, cg_upcall_pointer_reference_owned), 1));
237                 g(gen_frame_load(ctx, OP_SIZE_SLOT, garbage, slot, 0, dest));
238         } else if (!da(ctx->fn,function)->local_variables_flags[slot].may_be_borrowed) {
239                 g(gen_frame_load(ctx, OP_SIZE_SLOT, garbage, slot, 0, dest));
240                 g(gen_set_1(ctx, R_FRAME, slot, 0, false));
241                 flag_set(ctx, slot, false);
242         } else {
243                 uint32_t skip_label;
244                 skip_label = alloc_label(ctx);
245                 if (unlikely(!skip_label))
246                         return false;
247                 if (flag_is_set(ctx, slot)) {
248                         g(gen_set_1(ctx, R_FRAME, slot, 0, false));
249                         goto move_it;
250                 }
251                 if (flag_is_clear(ctx, slot))
252                         goto do_reference;
253                 g(gen_test_1(ctx, R_FRAME, slot, 0, skip_label, false, TEST_CLEAR));
254 do_reference:
255                 g(gen_upcall_start(ctx, 1));
256                 g(gen_frame_load(ctx, OP_SIZE_SLOT, garbage, slot, 0, R_ARG0));
257                 g(gen_upcall_argument(ctx, 0));
258                 g(gen_upcall(ctx, offsetof(struct cg_upcall_vector_s, cg_upcall_pointer_reference_owned), 1));
259 move_it:
260                 gen_label(skip_label);
261                 g(gen_frame_load(ctx, OP_SIZE_SLOT, garbage, slot, 0, dest));
262                 g(gen_frame_clear(ctx, OP_SIZE_SLOT, slot));
263                 flag_set(ctx, slot, false);
264         }
265         return true;
268 static bool attr_w gen_frame_set_pointer(struct codegen_context *ctx, frame_t slot, unsigned src, bool not_thunk)
270         g(gen_set_1(ctx, R_FRAME, slot, 0, true));
271         flag_set_unknown(ctx, slot);
272         if (not_thunk)
273                 ctx->flag_cache[slot] |= FLAG_CACHE_IS_NOT_THUNK;
274         flag_set(ctx, slot, true);
275         g(gen_frame_store(ctx, OP_SIZE_SLOT, slot, 0, src));
276         return true;