Haiku: set thread priority
[ajla.git] / c1-loong.inc
blob7e7b15c7e8bc0fa4ba5fa49ee4b44ef317bb7023
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 #define OP_SIZE_NATIVE                  OP_SIZE_8
20 #define OP_SIZE_ADDRESS                 OP_SIZE_NATIVE
22 #define JMP_LIMIT                       JMP_LONG
24 #define UNALIGNED_TRAP                  (!cpu_test_feature(CPU_FEATURE_unaligned))
26 #define ALU_WRITES_FLAGS(alu, im)       0
27 #define ALU1_WRITES_FLAGS(alu)          0
28 #define ROT_WRITES_FLAGS(alu)           0
29 #define COND_IS_LOGICAL(cond)           0
31 #define ARCH_PARTIAL_ALU(size)          0
32 #define ARCH_IS_3ADDRESS                1
33 #define ARCH_HAS_FLAGS                  0
34 #define ARCH_PREFERS_SX(size)           0
35 #define ARCH_HAS_BWX                    1
36 #define ARCH_HAS_MUL                    1
37 #define ARCH_HAS_DIV                    1
38 #define ARCH_HAS_ANDN                   1
39 #define ARCH_HAS_SHIFTED_ADD(bits)      0
40 #define ARCH_HAS_BTX(btx, size, cnst)   (((btx) == BTX_BTR || (btx) == BTX_BTEXT) && (cnst))
41 #define ARCH_SHIFT_SIZE                 OP_SIZE_4
42 #define ARCH_NEEDS_BARRIER              0
44 #define i_size(size)                    OP_SIZE_NATIVE
45 #define i_size_rot(size)                maximum(size, OP_SIZE_4)
47 #define R_ZERO          0x00
48 #define R_RA            0x01
49 #define R_TP            0x02
50 #define R_SP            0x03
51 #define R_A0            0x04
52 #define R_A1            0x05
53 #define R_A2            0x06
54 #define R_A3            0x07
55 #define R_A4            0x08
56 #define R_A5            0x09
57 #define R_A6            0x0a
58 #define R_A7            0x0b
59 #define R_T0            0x0c
60 #define R_T1            0x0d
61 #define R_T2            0x0e
62 #define R_T3            0x0f
63 #define R_T4            0x10
64 #define R_T5            0x11
65 #define R_T6            0x12
66 #define R_T7            0x13
67 #define R_T8            0x14
68 #define R_RESERVED      0x15
69 #define R_FP            0x16
70 #define R_S0            0x17
71 #define R_S1            0x18
72 #define R_S2            0x19
73 #define R_S3            0x1a
74 #define R_S4            0x1b
75 #define R_S5            0x1c
76 #define R_S6            0x1d
77 #define R_S7            0x1e
78 #define R_S8            0x1f
80 #define R_FA0           0x20
81 #define R_FA1           0x21
82 #define R_FA2           0x22
83 #define R_FA3           0x23
84 #define R_FA4           0x24
85 #define R_FA5           0x25
86 #define R_FA6           0x26
87 #define R_FA7           0x27
88 #define R_FT0           0x28
89 #define R_FT1           0x29
90 #define R_FT2           0x2a
91 #define R_FT3           0x2b
92 #define R_FT4           0x2c
93 #define R_FT5           0x2d
94 #define R_FT6           0x2e
95 #define R_FT7           0x2f
96 #define R_FT8           0x30
97 #define R_FT9           0x31
98 #define R_FT10          0x32
99 #define R_FT11          0x33
100 #define R_FT12          0x34
101 #define R_FT13          0x35
102 #define R_FT14          0x36
103 #define R_FT15          0x37
104 #define R_FS0           0x38
105 #define R_FS1           0x39
106 #define R_FS2           0x3a
107 #define R_FS3           0x3b
108 #define R_FS4           0x3c
109 #define R_FS5           0x3d
110 #define R_FS6           0x3e
111 #define R_FS7           0x3f
113 #define R_FRAME         R_S0
114 #define R_UPCALL        R_S1
115 #define R_TIMESTAMP     R_S2
117 #define R_SCRATCH_1     R_A0
118 #define R_SCRATCH_2     R_A1
119 #define R_SCRATCH_3     R_A2
120 #define R_SCRATCH_4     R_A3
121 #define R_SCRATCH_NA_1  R_A4
122 #define R_SCRATCH_NA_2  R_A5
123 #define R_SCRATCH_NA_3  R_A6
125 #define R_SAVED_1       R_S3
126 #define R_SAVED_2       R_S4
128 #define R_ARG0          R_A0
129 #define R_ARG1          R_A1
130 #define R_ARG2          R_A2
131 #define R_ARG3          R_A3
132 #define R_RET0          R_A0
133 #define R_RET1          R_A1
135 #define R_OFFSET_IMM    R_T0
136 #define R_CONST_IMM     R_T1
137 #define R_CMP_RESULT    R_T2
139 #define FR_SCRATCH_1    R_FA0
140 #define FR_SCRATCH_2    R_FA1
142 #define SUPPORTED_FP    0x6
144 #define FRAME_SIZE      0x30
146 static bool reg_is_fp(unsigned reg)
148         return reg >= 0x20 && reg < 0x40;
151 static bool attr_w gen_load_constant(struct codegen_context *ctx, unsigned reg, uint64_t c)
153         uint64_t c0 = c & 0x0000000000000fffULL;
154         uint64_t c1 = c & 0x00000000fffff000ULL;
155         uint64_t c2 = c & 0x000fffff00000000ULL;
156         uint64_t c3 = c & 0xfff0000000000000ULL;
157         uint64_t top_bits = 0;
158         if (!(c0 | c1 | c2)) {
159                 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
160                 gen_one(reg);
161                 gen_one(ARG_IMM);
162                 gen_eight(c3);
163                 return true;
164         }
165         if (c0 & 0x800ULL && c1 == 0xfffff000ULL) {
166                 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
167                 gen_one(reg);
168                 gen_one(ARG_IMM);
169                 gen_eight(c0 | 0xfffffffffffff000ULL);
170                 top_bits = 0xffffffff00000000ULL;
171         } else {
172                 bool have_reg = false;
173                 if (c1) {
174                         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
175                         gen_one(reg);
176                         gen_one(ARG_IMM);
177                         gen_eight((uint64_t)(int32_t)c1);
178                         top_bits = (uint64_t)(int32_t)c1 & 0xffffffff00000000ULL;
179                         have_reg = true;
180                 }
181                 if (!have_reg || c0) {
182                         if (!have_reg) {
183                                 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
184                                 gen_one(reg);
185                                 gen_one(ARG_IMM);
186                                 gen_eight(c0);
187                         } else {
188                                 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_OR, 0);
189                                 gen_one(reg);
190                                 gen_one(reg);
191                                 gen_one(ARG_IMM);
192                                 gen_eight(c0);
193                         }
194                 }
195         }
196         if (top_bits != (c2 | c3)) {
197                 uint64_t c2x = c2;
198                 if (c2 & 0x0008000000000000ULL)
199                         c2x |= 0xfff0000000000000ULL;
200                 if (top_bits != c2x) {
201                         gen_insn(INSN_MOV_MASK, OP_SIZE_NATIVE, MOV_MASK_32_64, 0);
202                         gen_one(reg);
203                         gen_one(reg);
204                         gen_one(ARG_IMM);
205                         gen_eight(c2x >> 32);
206                 }
207                 top_bits = c2x & 0xfff0000000000000ULL;
208                 if (top_bits != c3) {
209                         gen_insn(INSN_MOV_MASK, OP_SIZE_NATIVE, MOV_MASK_52_64, 0);
210                         gen_one(reg);
211                         gen_one(reg);
212                         gen_one(ARG_IMM);
213                         gen_eight(c3 >> 52);
214                 }
215         }
216         return true;
219 static bool attr_w gen_address(struct codegen_context *ctx, unsigned base, int64_t imm, unsigned purpose, unsigned size)
221         ctx->base_reg = base;
222         ctx->offset_imm = imm;
223         ctx->offset_reg = false;
224         switch (purpose) {
225                 case IMM_PURPOSE_LDR_OFFSET:
226                 case IMM_PURPOSE_LDR_SX_OFFSET:
227                 case IMM_PURPOSE_STR_OFFSET:
228                 case IMM_PURPOSE_VLDR_VSTR_OFFSET:
229                 case IMM_PURPOSE_MVI_CLI_OFFSET:
230                         if (likely(imm >= -0x800) && likely(imm < 0x800)) {
231                                 return true;
232                         }
233                         if (imm >= -0x8000 && imm < 0x8000 && !(imm & 3)) {
234                                 if (size == OP_SIZE_NATIVE)
235                                         return true;
236                                 if (purpose == IMM_PURPOSE_LDR_SX_OFFSET && size == OP_SIZE_4)
237                                         return true;
238                                 if (purpose == IMM_PURPOSE_STR_OFFSET && size == OP_SIZE_4)
239                                         return true;
240                         }
241                         break;
242                 default:
243                         internal(file_line, "gen_address: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
244         }
245         g(gen_load_constant(ctx, R_OFFSET_IMM, imm));
246         ctx->offset_reg = true;
247         return true;
250 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
252         switch (purpose) {
253                 case IMM_PURPOSE_STORE_VALUE:
254                         if (!imm)
255                                 return true;
256                         break;
257                 case IMM_PURPOSE_ADD:
258                 case IMM_PURPOSE_CMP:
259                         if (likely(imm >= -0x800) && likely(imm < 0x800))
260                                 return true;
261                         break;
262                 case IMM_PURPOSE_SUB:
263                         if (likely(imm > -0x800) && likely(imm <= 0x800))
264                                 return true;
265                         break;
266                 case IMM_PURPOSE_AND:
267                 case IMM_PURPOSE_OR:
268                 case IMM_PURPOSE_XOR:
269                         if (likely(imm >= 0) && likely(imm < 0x1000))
270                                 return true;
271                         break;
272                 case IMM_PURPOSE_ANDN:
273                         break;
274                 case IMM_PURPOSE_TEST:
275                         break;
276                 case IMM_PURPOSE_JMP_2REGS:
277                         break;
278                 case IMM_PURPOSE_MUL:
279                         break;
280                 case IMM_PURPOSE_BITWISE:
281                         return true;
282                 default:
283                         internal(file_line, "is_direct_const: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
284         }
285         return false;
288 static bool attr_w gen_imm(struct codegen_context *ctx, int64_t imm, unsigned purpose, unsigned size)
290         if (is_direct_const(imm, purpose, size)) {
291                 ctx->const_imm = imm;
292                 ctx->const_reg = false;
293         } else {
294                 g(gen_load_constant(ctx, R_CONST_IMM, imm));
295                 ctx->const_reg = true;
296         }
297         return true;
300 static bool attr_w gen_entry(struct codegen_context *ctx)
302         g(gen_imm(ctx, -FRAME_SIZE, IMM_PURPOSE_ADD, OP_SIZE_NATIVE));
303         gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
304         gen_one(R_SP);
305         gen_one(R_SP);
306         gen_imm_offset();
308         g(gen_address(ctx, R_SP, FRAME_SIZE - 0x08, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
309         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
310         gen_address_offset();
311         gen_one(R_RA);
313         g(gen_address(ctx, R_SP, FRAME_SIZE - 0x10, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
314         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
315         gen_address_offset();
316         gen_one(R_FRAME);
318         g(gen_address(ctx, R_SP, FRAME_SIZE - 0x18, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
319         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
320         gen_address_offset();
321         gen_one(R_UPCALL);
323         g(gen_address(ctx, R_SP, FRAME_SIZE - 0x20, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
324         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
325         gen_address_offset();
326         gen_one(R_TIMESTAMP);
328         g(gen_address(ctx, R_SP, FRAME_SIZE - 0x28, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
329         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
330         gen_address_offset();
331         gen_one(R_SAVED_1);
333         g(gen_address(ctx, R_SP, FRAME_SIZE - 0x30, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
334         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
335         gen_address_offset();
336         gen_one(R_SAVED_2);
338         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
339         gen_one(R_FRAME);
340         gen_one(R_ARG0);
342         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
343         gen_one(R_UPCALL);
344         gen_one(R_ARG1);
346         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
347         gen_one(R_TIMESTAMP);
348         gen_one(R_ARG2);
350         gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
351         gen_one(R_ARG3);
353         return true;
356 static bool attr_w gen_escape_arg(struct codegen_context *ctx, ip_t ip, uint32_t escape_label)
358         g(gen_load_constant(ctx, R_RET1, ip));
360         gen_insn(INSN_JMP, 0, 0, 0);
361         gen_four(escape_label);
363         return true;
366 static bool attr_w gen_escape(struct codegen_context *ctx)
368         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
369         gen_one(R_RET0);
370         gen_one(R_FRAME);
372         g(gen_address(ctx, R_SP, FRAME_SIZE - 0x08, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
373         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
374         gen_one(R_RA);
375         gen_address_offset();
377         g(gen_address(ctx, R_SP, FRAME_SIZE - 0x10, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
378         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
379         gen_one(R_FRAME);
380         gen_address_offset();
382         g(gen_address(ctx, R_SP, FRAME_SIZE - 0x18, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
383         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
384         gen_one(R_UPCALL);
385         gen_address_offset();
387         g(gen_address(ctx, R_SP, FRAME_SIZE - 0x20, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
388         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
389         gen_one(R_TIMESTAMP);
390         gen_address_offset();
392         g(gen_address(ctx, R_SP, FRAME_SIZE - 0x28, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
393         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
394         gen_one(R_SAVED_1);
395         gen_address_offset();
397         g(gen_address(ctx, R_SP, FRAME_SIZE - 0x30, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
398         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
399         gen_one(R_SAVED_2);
400         gen_address_offset();
402         g(gen_imm(ctx, FRAME_SIZE, IMM_PURPOSE_ADD, OP_SIZE_NATIVE));
403         gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
404         gen_one(R_SP);
405         gen_one(R_SP);
406         gen_imm_offset();
408         gen_insn(INSN_RET, 0, 0, 0);
410         return true;
413 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
415         return true;
418 static bool attr_w gen_upcall(struct codegen_context *ctx, unsigned offset, unsigned attr_unused n_args)
420         g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_ADDRESS));
421         gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
422         gen_one(R_SCRATCH_NA_1);
423         gen_address_offset();
425         gen_insn(INSN_CALL_INDIRECT, OP_SIZE_ADDRESS, 0, 0);
426         gen_one(R_SCRATCH_NA_1);
428         return true;
431 static bool attr_w gen_cmp_test_jmp(struct codegen_context *ctx, unsigned insn, unsigned op_size, unsigned reg1, unsigned reg2, unsigned cond, uint32_t label);
433 static bool attr_w gen_timestamp_test(struct codegen_context *ctx, uint32_t escape_label)
435         g(gen_address(ctx, R_UPCALL, offsetof(struct cg_upcall_vector_s, ts), IMM_PURPOSE_LDR_SX_OFFSET, OP_SIZE_4));
436         gen_insn(INSN_MOVSX, OP_SIZE_4, 0, 0);
437         gen_one(R_SCRATCH_1);
438         gen_address_offset();
440         g(gen_cmp_test_jmp(ctx, INSN_CMP, OP_SIZE_NATIVE, R_SCRATCH_1, R_TIMESTAMP, COND_NE, escape_label));
442         return true;