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_decoder.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 decoder and code-buffer validator backed by pure seeded OM-2 (om/aarch64/aarch64_om2.om). |
| 18 | * All instruction decoding and validation messages execute in Omega via omega2_a64decode / |
| 19 | * omega2_a64validate. No decode or validation logic lives in C. */ |
| 20 | #include "aarch64_decoder.h" |
| 21 | #include "omega_native.h" |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | int aarch64_decode_instruction(uint32_t insn, DecodedInsn *out_dec) { |
| 26 | if (!out_dec) return -1; |
| 27 | uint64_t cells[9] = {0}; |
| 28 | uint64_t rc = 0; |
| 29 | if (omega2_a64decode(&rc, (uint64_t)insn, cells, 9) != OMEGA_OK) abort(); |
| 30 | memset(out_dec, 0, sizeof(DecodedInsn)); |
| 31 | out_dec->op = (DecodedOp)cells[0]; |
| 32 | out_dec->sf = (cells[1] != 0); |
| 33 | out_dec->rd = (uint8_t)cells[2]; |
| 34 | out_dec->rn = (uint8_t)cells[3]; |
| 35 | out_dec->rm = (uint8_t)cells[4]; |
| 36 | out_dec->imm16 = (uint16_t)cells[5]; |
| 37 | out_dec->hw = (uint8_t)cells[6]; |
| 38 | out_dec->branch_imm = (int32_t)cells[7]; |
| 39 | out_dec->cond = (Aarch64Cond)cells[8]; |
| 40 | return (int)(int64_t)rc; |
| 41 | } |
| 42 | |
| 43 | int aarch64_validate_code_buffer(const uint8_t *code, size_t len, char *err_msg, size_t err_msg_len) { |
| 44 | uint8_t dummy_code[4] = {0}; |
| 45 | uint8_t dummy_msg[1] = {0}; |
| 46 | const uint8_t *cptr = (code && len > 0) ? code : dummy_code; |
| 47 | uint64_t clen = (code && len > 0) ? (uint64_t)len : 4ULL; |
| 48 | uint64_t pass_n = code ? (uint64_t)len : 0ULL; |
| 49 | uint8_t *mptr = (err_msg && err_msg_len > 0) ? (uint8_t *)err_msg : dummy_msg; |
| 50 | uint64_t mcap = (err_msg && err_msg_len > 0) ? (uint64_t)err_msg_len : 1ULL; |
| 51 | uint64_t mlim = err_msg ? (uint64_t)err_msg_len : 0ULL; |
| 52 | uint64_t rc = 0; |
| 53 | if (omega2_a64validate(&rc, cptr, clen, pass_n, mptr, mcap, mlim) != OMEGA_OK) abort(); |
| 54 | return (int)(int64_t)rc; |
| 55 | } |