omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / aarch64_encoder.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* AArch64 legacy instruction encoder backed by pure seeded OM-2 (om/aarch64/aarch64_om2.om). |
| 18 | * All architectural range checks, instruction word formulas, and little-endian buffer writes |
| 19 | * execute in Omega via omega2_a64* / omega2_a64put. This C file only translates negative |
| 20 | * field-refusal codes to the legacy abort contract. */ |
| 21 | #include "aarch64_encoder.h" |
| 22 | #include "omega_native.h" |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | |
| 26 | static void refuse(const char *emitter, const char *field, long long value) __attribute__((noreturn)); |
| 27 | static void refuse(const char *emitter, const char *field, long long value) { |
| 28 | fprintf(stderr, "A64_LEGACY_FIELD_OUT_OF_RANGE %s %s=%lld\n", emitter, field, value); |
| 29 | fflush(stderr); |
| 30 | abort(); |
| 31 | } |
| 32 | |
| 33 | static int finish_emit(uint8_t *buf, size_t *pos, size_t max_len, |
| 34 | const char *emitter, const char *const *fields, const long long *vals, |
| 35 | uint32_t st, uint64_t raw) { |
| 36 | if (st != OMEGA_OK) abort(); |
| 37 | int64_t w = (int64_t)raw; |
| 38 | if (w <= -2) { |
| 39 | int idx = (int)(-w - 2); |
| 40 | refuse(emitter, fields[idx], vals[idx]); |
| 41 | } |
| 42 | uint8_t dummy[4] = {0}; |
| 43 | uint8_t *target = (buf && max_len > 0) ? buf : dummy; |
| 44 | uint64_t target_len = (buf && max_len > 0) ? (uint64_t)max_len : 4ULL; |
| 45 | uint64_t state[2] = { (uint64_t)*pos, (uint64_t)max_len }; |
| 46 | uint64_t rc = 0; |
| 47 | if (omega2_a64put(&rc, target, target_len, state, 2, (uint64_t)w) != OMEGA_OK) abort(); |
| 48 | *pos = (size_t)state[0]; |
| 49 | return (int)(int64_t)rc; |
| 50 | } |
| 51 | |
| 52 | #define EMIT_RRR(fn, omfn) \ |
| 53 | int fn(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rd, uint8_t rn, uint8_t rm) { \ |
| 54 | const char *f[] = { "sf", "rd", "rn", "rm" }; \ |
| 55 | const long long v[] = { (long long)sf, (long long)rd, (long long)rn, (long long)rm }; \ |
| 56 | uint64_t r = 0; \ |
| 57 | uint32_t st = omfn(&r, (uint64_t)sf, (uint64_t)rd, (uint64_t)rn, (uint64_t)rm); \ |
| 58 | return finish_emit(buf, pos, max_len, __func__, f, v, st, r); \ |
| 59 | } |
| 60 | |
| 61 | EMIT_RRR(aarch64_emit_add_reg, omega2_a64addreg) |
| 62 | EMIT_RRR(aarch64_emit_sub_reg, omega2_a64subreg) |
| 63 | EMIT_RRR(aarch64_emit_mul_reg, omega2_a64mulreg) |
| 64 | EMIT_RRR(aarch64_emit_and_reg, omega2_a64andreg) |
| 65 | EMIT_RRR(aarch64_emit_orr_reg, omega2_a64orrreg) |
| 66 | EMIT_RRR(aarch64_emit_eor_reg, omega2_a64eorreg) |
| 67 | EMIT_RRR(aarch64_emit_subs_reg, omega2_a64subsreg) |
| 68 | |
| 69 | int aarch64_emit_mov_reg(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rd, uint8_t rm) { |
| 70 | const char *f[] = { "sf", "rd", "rm" }; |
| 71 | const long long v[] = { (long long)sf, (long long)rd, (long long)rm }; |
| 72 | uint64_t r = 0; |
| 73 | uint32_t st = omega2_a64movreg(&r, (uint64_t)sf, (uint64_t)rd, (uint64_t)rm); |
| 74 | return finish_emit(buf, pos, max_len, __func__, f, v, st, r); |
| 75 | } |
| 76 | |
| 77 | int aarch64_emit_movz(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rd, uint16_t imm16, uint8_t shift) { |
| 78 | const char *f[] = { "sf", "rd", "imm16", "shift" }; |
| 79 | const long long v[] = { (long long)sf, (long long)rd, (long long)imm16, (long long)shift }; |
| 80 | uint64_t r = 0; |
| 81 | uint32_t st = omega2_a64movz(&r, (uint64_t)sf, (uint64_t)rd, (uint64_t)imm16, (uint64_t)shift); |
| 82 | return finish_emit(buf, pos, max_len, __func__, f, v, st, r); |
| 83 | } |
| 84 | |
| 85 | int aarch64_emit_movk(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rd, uint16_t imm16, uint8_t shift) { |
| 86 | const char *f[] = { "sf", "rd", "imm16", "shift" }; |
| 87 | const long long v[] = { (long long)sf, (long long)rd, (long long)imm16, (long long)shift }; |
| 88 | uint64_t r = 0; |
| 89 | uint32_t st = omega2_a64movk(&r, (uint64_t)sf, (uint64_t)rd, (uint64_t)imm16, (uint64_t)shift); |
| 90 | return finish_emit(buf, pos, max_len, __func__, f, v, st, r); |
| 91 | } |
| 92 | |
| 93 | int aarch64_emit_ret(uint8_t *buf, size_t *pos, size_t max_len) { |
| 94 | uint64_t r = 0; |
| 95 | uint32_t st = omega2_a64ret(&r); |
| 96 | return finish_emit(buf, pos, max_len, __func__, NULL, NULL, st, r); |
| 97 | } |
| 98 | |
| 99 | int aarch64_emit_b(uint8_t *buf, size_t *pos, size_t max_len, int32_t imm26) { |
| 100 | const char *f[] = { "imm26" }; |
| 101 | const long long v[] = { (long long)imm26 }; |
| 102 | uint64_t r = 0; |
| 103 | uint32_t st = omega2_a64b(&r, (uint64_t)(int64_t)imm26); |
| 104 | return finish_emit(buf, pos, max_len, __func__, f, v, st, r); |
| 105 | } |
| 106 | |
| 107 | int aarch64_emit_b_cond(uint8_t *buf, size_t *pos, size_t max_len, Aarch64Cond cond, int32_t imm19) { |
| 108 | const char *f[] = { "cond", "imm19" }; |
| 109 | const long long v[] = { (long long)(int)cond, (long long)imm19 }; |
| 110 | uint64_t r = 0; |
| 111 | uint32_t st = omega2_a64bcond(&r, (uint64_t)(int64_t)(int)cond, (uint64_t)(int64_t)imm19); |
| 112 | return finish_emit(buf, pos, max_len, __func__, f, v, st, r); |
| 113 | } |
| 114 | |
| 115 | int aarch64_emit_cbz(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rt, int32_t imm19) { |
| 116 | const char *f[] = { "sf", "rt", "imm19" }; |
| 117 | const long long v[] = { (long long)sf, (long long)rt, (long long)imm19 }; |
| 118 | uint64_t r = 0; |
| 119 | uint32_t st = omega2_a64cbz(&r, (uint64_t)sf, (uint64_t)rt, (uint64_t)(int64_t)imm19); |
| 120 | return finish_emit(buf, pos, max_len, __func__, f, v, st, r); |
| 121 | } |
| 122 | |
| 123 | int aarch64_emit_cbnz(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rt, int32_t imm19) { |
| 124 | const char *f[] = { "sf", "rt", "imm19" }; |
| 125 | const long long v[] = { (long long)sf, (long long)rt, (long long)imm19 }; |
| 126 | uint64_t r = 0; |
| 127 | uint32_t st = omega2_a64cbnz(&r, (uint64_t)sf, (uint64_t)rt, (uint64_t)(int64_t)imm19); |
| 128 | return finish_emit(buf, pos, max_len, __func__, f, v, st, r); |
| 129 | } |
| 130 | |
| 131 | int aarch64_emit_adr(uint8_t *buf, size_t *pos, size_t max_len, uint8_t rd, int32_t imm21) { |
| 132 | const char *f[] = { "rd", "imm21" }; |
| 133 | const long long v[] = { (long long)rd, (long long)imm21 }; |
| 134 | uint64_t r = 0; |
| 135 | uint32_t st = omega2_a64adr(&r, (uint64_t)rd, (uint64_t)(int64_t)imm21); |
| 136 | return finish_emit(buf, pos, max_len, __func__, f, v, st, r); |
| 137 | } |
| 138 | |
| 139 | int aarch64_emit_ldr_uoff(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rt, uint8_t rn, uint16_t uoff) { |
| 140 | const char *f[] = { "sf", "rt", "rn", "uoff" }; |
| 141 | const long long v[] = { (long long)sf, (long long)rt, (long long)rn, (long long)uoff }; |
| 142 | uint64_t r = 0; |
| 143 | uint32_t st = omega2_a64ldruoff(&r, (uint64_t)sf, (uint64_t)rt, (uint64_t)rn, (uint64_t)uoff); |
| 144 | return finish_emit(buf, pos, max_len, __func__, f, v, st, r); |
| 145 | } |
| 146 | |
| 147 | int aarch64_emit_str_uoff(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rt, uint8_t rn, uint16_t uoff) { |
| 148 | const char *f[] = { "sf", "rt", "rn", "uoff" }; |
| 149 | const long long v[] = { (long long)sf, (long long)rt, (long long)rn, (long long)uoff }; |
| 150 | uint64_t r = 0; |
| 151 | uint32_t st = omega2_a64struoff(&r, (uint64_t)sf, (uint64_t)rt, (uint64_t)rn, (uint64_t)uoff); |
| 152 | return finish_emit(buf, pos, max_len, __func__, f, v, st, r); |
| 153 | } |
| 154 | |
| 155 | int aarch64_emit_ldrb_uoff(uint8_t *buf, size_t *pos, size_t max_len, uint8_t rt, uint8_t rn, uint16_t uoff) { |
| 156 | const char *f[] = { "rt", "rn", "uoff" }; |
| 157 | const long long v[] = { (long long)rt, (long long)rn, (long long)uoff }; |
| 158 | uint64_t r = 0; |
| 159 | uint32_t st = omega2_a64ldrbuoff(&r, (uint64_t)rt, (uint64_t)rn, (uint64_t)uoff); |
| 160 | return finish_emit(buf, pos, max_len, __func__, f, v, st, r); |
| 161 | } |
| 162 | |
| 163 | int aarch64_emit_strb_uoff(uint8_t *buf, size_t *pos, size_t max_len, uint8_t rt, uint8_t rn, uint16_t uoff) { |
| 164 | const char *f[] = { "rt", "rn", "uoff" }; |
| 165 | const long long v[] = { (long long)rt, (long long)rn, (long long)uoff }; |
| 166 | uint64_t r = 0; |
| 167 | uint32_t st = omega2_a64strbuoff(&r, (uint64_t)rt, (uint64_t)rn, (uint64_t)uoff); |
| 168 | return finish_emit(buf, pos, max_len, __func__, f, v, st, r); |
| 169 | } |
| 170 | |
| 171 | int aarch64_emit_ldr_post(uint8_t *buf, size_t *pos, size_t max_len, uint8_t rt, uint8_t rn, int16_t simm9) { |
| 172 | const char *f[] = { "rt", "rn", "simm9" }; |
| 173 | const long long v[] = { (long long)rt, (long long)rn, (long long)simm9 }; |
| 174 | uint64_t r = 0; |
| 175 | uint32_t st = omega2_a64ldrpost(&r, (uint64_t)rt, (uint64_t)rn, (uint64_t)(int64_t)simm9); |
| 176 | return finish_emit(buf, pos, max_len, __func__, f, v, st, r); |
| 177 | } |
| 178 | |
| 179 | int aarch64_emit_str_post(uint8_t *buf, size_t *pos, size_t max_len, uint8_t rt, uint8_t rn, int16_t simm9) { |
| 180 | const char *f[] = { "rt", "rn", "simm9" }; |
| 181 | const long long v[] = { (long long)rt, (long long)rn, (long long)simm9 }; |
| 182 | uint64_t r = 0; |
| 183 | uint32_t st = omega2_a64strpost(&r, (uint64_t)rt, (uint64_t)rn, (uint64_t)(int64_t)simm9); |
| 184 | return finish_emit(buf, pos, max_len, __func__, f, v, st, r); |
| 185 | } |
| 186 | |
| 187 | int aarch64_emit_ldr_x_post(uint8_t *buf, size_t *pos, size_t max_len, uint8_t rt, uint8_t rn, int16_t simm9) { |
| 188 | const char *f[] = { "rt", "rn", "simm9" }; |
| 189 | const long long v[] = { (long long)rt, (long long)rn, (long long)simm9 }; |
| 190 | uint64_t r = 0; |
| 191 | uint32_t st = omega2_a64ldrxpost(&r, (uint64_t)rt, (uint64_t)rn, (uint64_t)(int64_t)simm9); |
| 192 | return finish_emit(buf, pos, max_len, __func__, f, v, st, r); |
| 193 | } |
| 194 | |
| 195 | int aarch64_emit_str_x_post(uint8_t *buf, size_t *pos, size_t max_len, uint8_t rt, uint8_t rn, int16_t simm9) { |
| 196 | const char *f[] = { "rt", "rn", "simm9" }; |
| 197 | const long long v[] = { (long long)rt, (long long)rn, (long long)simm9 }; |
| 198 | uint64_t r = 0; |
| 199 | uint32_t st = omega2_a64strxpost(&r, (uint64_t)rt, (uint64_t)rn, (uint64_t)(int64_t)simm9); |
| 200 | return finish_emit(buf, pos, max_len, __func__, f, v, st, r); |
| 201 | } |
| 202 | |
| 203 | int aarch64_emit_subs_imm(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rd, uint8_t rn, uint16_t imm12) { |
| 204 | const char *f[] = { "sf", "rd", "rn", "imm12" }; |
| 205 | const long long v[] = { (long long)sf, (long long)rd, (long long)rn, (long long)imm12 }; |
| 206 | uint64_t r = 0; |
| 207 | uint32_t st = omega2_a64subsimm(&r, (uint64_t)sf, (uint64_t)rd, (uint64_t)rn, (uint64_t)imm12); |
| 208 | return finish_emit(buf, pos, max_len, __func__, f, v, st, r); |
| 209 | } |