x86: R_SCRATCH_4 may alias R_SAVED_2; make R_SI and R_DI allocatable
[ajla.git] / c1-sparc.inc
blobd699db2e621a3995c5ebcc85677301787f8acd02
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(ARCH_SPARC64)
20 #define SPARC_9                         cpu_test_feature(CPU_FEATURE_sparc9)
21 #define FRAME_SIZE                      0x60
22 #define OP_SIZE_NATIVE                  (SPARC_9 ? OP_SIZE_8 : OP_SIZE_4)
23 #define OP_SIZE_ADDRESS                 OP_SIZE_4
24 #else
25 #define SPARC_9                         1
26 #define FRAME_SIZE                      0xb0
27 #define OP_SIZE_NATIVE                  OP_SIZE_8
28 #define OP_SIZE_ADDRESS                 OP_SIZE_8
29 #endif
31 #define JMP_LIMIT                       JMP_LONG
33 #define UNALIGNED_TRAP                  1
35 #define ALU_WRITES_FLAGS(alu, im)               0
36 #define ALU1_WRITES_FLAGS(alu)          0
37 #define ROT_WRITES_FLAGS(alu)           0
38 #define COND_IS_LOGICAL(cond)           0
40 #define ARCH_PARTIAL_ALU(size)          0
41 #define ARCH_IS_3ADDRESS                1
42 #define ARCH_HAS_FLAGS                  1
43 #define ARCH_PREFERS_SX(size)           0
44 #define ARCH_HAS_BWX                    1
45 #define ARCH_HAS_MUL                    SPARC_9
46 #define ARCH_HAS_DIV                    SPARC_9
47 #define ARCH_HAS_ANDN                   1
48 #define ARCH_HAS_SHIFTED_ADD(bits)      0
49 #define ARCH_HAS_BTX(btx, size, cnst)   0
50 #define ARCH_SHIFT_SIZE                 OP_SIZE_4
51 #define ARCH_NEEDS_BARRIER              0
53 #define i_size(size)                    OP_SIZE_NATIVE
54 #define i_size_rot(size)                maximum(size, OP_SIZE_4)
56 #define R_G0            0x00
57 #define R_G1            0x01
58 #define R_G2            0x02
59 #define R_G3            0x03
60 #define R_G4            0x04
61 #define R_G5            0x05
62 #define R_G6            0x06
63 #define R_G7            0x07
64 #define R_O0            0x08
65 #define R_O1            0x09
66 #define R_O2            0x0a
67 #define R_O3            0x0b
68 #define R_O4            0x0c
69 #define R_O5            0x0d
70 #define R_O6            0x0e
71 #define R_O7            0x0f
72 #define R_L0            0x10
73 #define R_L1            0x11
74 #define R_L2            0x12
75 #define R_L3            0x13
76 #define R_L4            0x14
77 #define R_L5            0x15
78 #define R_L6            0x16
79 #define R_L7            0x17
80 #define R_I0            0x18
81 #define R_I1            0x19
82 #define R_I2            0x1a
83 #define R_I3            0x1b
84 #define R_I4            0x1c
85 #define R_I5            0x1d
86 #define R_I6            0x1e
87 #define R_I7            0x1f
89 #define FSR_0           0x20
90 #define FSR_1           0x21
91 #define FSR_2           0x22
92 #define FSR_3           0x23
93 #define FSR_4           0x24
94 #define FSR_5           0x25
95 #define FSR_6           0x26
96 #define FSR_7           0x27
97 #define FSR_8           0x28
98 #define FSR_9           0x29
99 #define FSR_10          0x2a
100 #define FSR_11          0x2b
101 #define FSR_12          0x2c
102 #define FSR_13          0x2d
103 #define FSR_14          0x2e
104 #define FSR_15          0x2f
105 #define FSR_16          0x30
106 #define FSR_17          0x31
107 #define FSR_18          0x32
108 #define FSR_19          0x33
109 #define FSR_20          0x34
110 #define FSR_21          0x35
111 #define FSR_22          0x36
112 #define FSR_23          0x37
113 #define FSR_24          0x38
114 #define FSR_25          0x39
115 #define FSR_26          0x3a
116 #define FSR_27          0x3b
117 #define FSR_28          0x3c
118 #define FSR_29          0x3d
119 #define FSR_30          0x3e
120 #define FSR_31          0x3f
122 #define FDR_0           0x20
123 #define FDR_2           0x22
124 #define FDR_4           0x24
125 #define FDR_6           0x26
126 #define FDR_8           0x28
127 #define FDR_10          0x2a
128 #define FDR_12          0x2c
129 #define FDR_14          0x2e
130 #define FDR_16          0x30
131 #define FDR_18          0x32
132 #define FDR_20          0x34
133 #define FDR_22          0x36
134 #define FDR_24          0x38
135 #define FDR_26          0x3a
136 #define FDR_28          0x3c
137 #define FDR_30          0x3e
138 #define FDR_32          0x21
139 #define FDR_34          0x23
140 #define FDR_36          0x25
141 #define FDR_38          0x27
142 #define FDR_40          0x29
143 #define FDR_42          0x2b
144 #define FDR_44          0x2d
145 #define FDR_46          0x2f
146 #define FDR_48          0x31
147 #define FDR_50          0x33
148 #define FDR_52          0x35
149 #define FDR_54          0x37
150 #define FDR_56          0x39
151 #define FDR_58          0x3b
152 #define FDR_60          0x3d
153 #define FDR_62          0x3f
155 #define FQR_0           0x20
156 #define FQR_4           0x24
157 #define FQR_8           0x28
158 #define FQR_12          0x2c
159 #define FQR_16          0x30
160 #define FQR_20          0x34
161 #define FQR_24          0x38
162 #define FQR_28          0x3c
163 #define FQR_32          0x21
164 #define FQR_36          0x25
165 #define FQR_40          0x29
166 #define FQR_44          0x2d
167 #define FQR_48          0x31
168 #define FQR_52          0x35
169 #define FQR_56          0x39
170 #define FQR_60          0x3d
172 #define R_ZERO          0x00
173 #define R_SP            R_O6
175 #define R_FRAME         R_I0
176 #define R_UPCALL        R_I1
177 #define R_TIMESTAMP     R_I2
179 #define R_SCRATCH_1     R_O1
180 #define R_SCRATCH_2     R_O0
181 #define R_SCRATCH_3     R_O3
182 #define R_SCRATCH_4     R_O2
184 #define R_SCRATCH_NA_1  R_O4
185 #define R_SCRATCH_NA_2  R_O5
186 #define R_SCRATCH_NA_3  R_O7
188 #define R_SAVED_1       R_L0
189 #define R_SAVED_2       R_L1
191 #define R_ARG0          R_O0
192 #define R_ARG1          R_O1
193 #define R_ARG2          R_O2
194 #define R_ARG3          R_O3
195 #define R_RET0          R_O0
197 #define FR_SCRATCH_1    FDR_0
198 #define FR_SCRATCH_2    FDR_4
200 #define R_OFFSET_IMM    R_G1
201 #define R_CONST_IMM     R_G2
202 #define R_CONST_HELPER  R_G3
204 #define SUPPORTED_FP    0x6
206 static bool reg_is_fp(unsigned reg)
208         return reg >= 0x20 && reg < 0x40;
211 static const uint8_t reg_available[] = { 0 };
212 #define reg_is_saved(r) 1
214 static bool attr_w gen_load_constant(struct codegen_context *ctx, unsigned reg, uint64_t c)
216         int32_t cl;
217         if (SPARC_9) {
218                 if (c >= (uint64_t)-4096) {
219                         gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_OR, 0);
220                         gen_one(reg);
221                         gen_one(R_ZERO);
222                         gen_one(ARG_IMM);
223                         gen_eight(c);
224                         return true;
225                 }
226                 if (c >= 0x100000000ULL) {
227                         int32_t cu = c >> 32;
228                         if (cu < -4096 || cu >= 4096) {
229                                 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
230                                 gen_one(R_CONST_HELPER);
231                                 gen_one(ARG_IMM);
232                                 gen_eight(cu & 0xFFFFFC00UL);
233                                 cu &= 0x3FFU;
234                                 if (cu) {
235                                         gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_OR, 0);
236                                         gen_one(R_CONST_HELPER);
237                                         gen_one(R_CONST_HELPER);
238                                         gen_one(ARG_IMM);
239                                         gen_eight(cu);
240                                 }
241                         } else {
242                                 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_OR, 0);
243                                 gen_one(R_CONST_HELPER);
244                                 gen_one(R_ZERO);
245                                 gen_one(ARG_IMM);
246                                 gen_eight(cu);
247                         }
248                         gen_insn(INSN_ROT, OP_SIZE_NATIVE, ROT_SHL, 0);
249                         gen_one(R_CONST_HELPER);
250                         gen_one(R_CONST_HELPER);
251                         gen_one(ARG_IMM);
252                         gen_eight(32);
253                 }
254         }
255         cl = c;
256         if (cl < 0 || cl >= 4096) {
257                 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
258                 gen_one(reg);
259                 gen_one(ARG_IMM);
260                 gen_eight(cl & 0xFFFFFC00UL);
261                 cl &= 0x3FFU;
262                 if (cl) {
263                         gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_OR, 0);
264                         gen_one(reg);
265                         gen_one(reg);
266                         gen_one(ARG_IMM);
267                         gen_eight(cl);
268                 }
269         } else {
270                 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_OR, 0);
271                 gen_one(reg);
272                 gen_one(R_ZERO);
273                 gen_one(ARG_IMM);
274                 gen_eight(cl);
275         }
276         if (SPARC_9) {
277                 if (c >= 0x100000000ULL) {
278                         gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_OR, 0);
279                         gen_one(reg);
280                         gen_one(reg);
281                         gen_one(R_CONST_HELPER);
282                 }
283         }
284         return true;
287 static bool attr_w gen_address(struct codegen_context *ctx, unsigned base, int64_t imm, unsigned purpose, unsigned size)
289         ctx->base_reg = base;
290         ctx->offset_imm = imm;
291         ctx->offset_reg = false;
292         switch (purpose) {
293                 case IMM_PURPOSE_LDR_OFFSET:
294                 case IMM_PURPOSE_LDR_SX_OFFSET:
295                 case IMM_PURPOSE_STR_OFFSET:
296                 case IMM_PURPOSE_LDP_STP_OFFSET:
297                 case IMM_PURPOSE_VLDR_VSTR_OFFSET:
298                 case IMM_PURPOSE_MVI_CLI_OFFSET:
299                         if (likely(imm >= -4096) && likely(imm < 4096))
300                                 return true;
301                         break;
302                 default:
303                         internal(file_line, "gen_address: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
304         }
306         g(gen_load_constant(ctx, R_OFFSET_IMM, imm));
307         ctx->offset_reg = true;
308         return true;
311 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
313         switch (purpose) {
314                 case IMM_PURPOSE_STORE_VALUE:
315                         if (!imm)
316                                 return true;
317                         break;
318                 case IMM_PURPOSE_ADD:
319                 case IMM_PURPOSE_SUB:
320                 case IMM_PURPOSE_CMP:
321                 case IMM_PURPOSE_CMP_LOGICAL:
322                 case IMM_PURPOSE_AND:
323                 case IMM_PURPOSE_OR:
324                 case IMM_PURPOSE_XOR:
325                 case IMM_PURPOSE_ANDN:
326                 case IMM_PURPOSE_TEST:
327                 case IMM_PURPOSE_MUL:
328                         if (likely(imm >= -4096) && likely(imm < 4096))
329                                 return true;
330                         break;
331                 case IMM_PURPOSE_CMOV:
332                         if (likely(imm >= -1024) && likely(imm < 1024))
333                                 return true;
334                         break;
335                 case IMM_PURPOSE_MOVR:
336                         if (likely(imm >= -512) && likely(imm < 512))
337                                 return true;
338                         break;
339                 default:
340                         internal(file_line, "is_direct_const: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
341         }
342         return false;
345 static bool attr_w gen_imm(struct codegen_context *ctx, int64_t imm, unsigned purpose, unsigned size)
347         if (is_direct_const(imm, purpose, size)) {
348                 ctx->const_imm = imm;
349                 ctx->const_reg = false;
350         } else {
351                 g(gen_load_constant(ctx, R_CONST_IMM, imm));
352                 ctx->const_reg = true;
353         }
354         return true;
357 static bool attr_w gen_entry(struct codegen_context *ctx)
359         gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_SAVE, 0);
360         gen_one(R_SP);
361         gen_one(R_SP);
362         gen_one(ARG_IMM);
363         gen_eight(-FRAME_SIZE);
365         gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
366         gen_one(R_I3);
368         return true;
371 static bool attr_w gen_escape_arg(struct codegen_context *ctx, ip_t ip, uint32_t escape_label)
373         g(gen_load_constant(ctx, R_I1, ip));
375         gen_insn(INSN_JMP, 0, 0, 0);
376         gen_four(escape_label);
378         return true;
381 static bool attr_w gen_escape(struct codegen_context *ctx)
383         gen_insn(INSN_RET, 0, 0, 0);
385         return true;
388 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
390         return true;
393 static bool attr_w gen_upcall(struct codegen_context *ctx, unsigned offset, unsigned n_args)
395         g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_ADDRESS));
396         gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
397         gen_one(R_SCRATCH_NA_1);
398         gen_address_offset();
400         gen_insn(INSN_CALL_INDIRECT, OP_SIZE_NATIVE, 0, 0);
401         gen_one(R_SCRATCH_NA_1);
403         g(gen_upcall_end(ctx, n_args));
405         return true;
408 static bool attr_w gen_timestamp_test(struct codegen_context *ctx, uint32_t escape_label)
410         g(gen_address(ctx, R_UPCALL, offsetof(struct cg_upcall_vector_s, ts), IMM_PURPOSE_LDR_OFFSET, OP_SIZE_4));
411         gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
412         gen_one(R_SCRATCH_1);
413         gen_address_offset();
415         gen_insn(INSN_CMP, OP_SIZE_4, 0, 1);
416         gen_one(R_SCRATCH_1);
417         gen_one(R_TIMESTAMP);
419         gen_insn(INSN_JMP_COND, OP_SIZE_4, COND_NE, 0);
420         gen_four(escape_label);
422         return true;