diff options
author | Yuyang Huang <yuyanghuang@google.com> | 2023-10-10 15:51:40 +0900 |
---|---|---|
committer | Yuyang Huang <yuyanghuang@google.com> | 2023-10-11 14:37:49 +0900 |
commit | 178ebe0278d3b20e8993a17e3e7962c553a62ed3 (patch) | |
tree | 900e2657ec904a43d3ab37258e37133b40c55838 | |
parent | b1b3059aa9ec57e0ab4581bf408bf6d7f901e85c (diff) | |
download | apf-178ebe0278d3b20e8993a17e3e7962c553a62ed3.tar.gz |
Support ALLOC opcode
The ALLOC opcode will trigger the call to apf_allocate_buffer(). The
program will return immediately if the allocation failed. Otherwise, it
will initialize the allocated region with 0x00.
Bug: 293811969
Test: TH
Change-Id: I7d63f5d16390681a7949234152a292d843ebe4bf
-rw-r--r-- | v5/apf.h | 1 | ||||
-rw-r--r-- | v5/apf_interpreter.c | 11 |
2 files changed, 12 insertions, 0 deletions
@@ -165,6 +165,7 @@ #define NEG_EXT_OPCODE 33 // Negate, e.g. "neg R0" #define SWAP_EXT_OPCODE 34 // Swap, e.g. "swap R0,R1" #define MOV_EXT_OPCODE 35 // Move, e.g. "move R0,R1" +#define ALLOC_EXT_OPCODE 36 // Allocate buffer, "e.g. ALLOC R0" #define EXTRACT_OPCODE(i) (((i) >> 3) & 31) #define EXTRACT_REGISTER(i) ((i) & 1) diff --git a/v5/apf_interpreter.c b/v5/apf_interpreter.c index f2662ed..cfb69e2 100644 --- a/v5/apf_interpreter.c +++ b/v5/apf_interpreter.c @@ -16,6 +16,7 @@ #include "apf_interpreter.h" +// TODO: Remove the dependency of the standard library and make the interpreter self-contained. #include <string.h> // For memcmp #include "apf.h" @@ -86,6 +87,11 @@ int apf_run(uint8_t* program, uint32_t program_len, uint32_t ram_len, // upper bound on the number of instructions in the program. uint32_t instructions_remaining = program_len; + // The output buffer pointer + uint8_t* allocated_buffer = NULL; + // The length of the output buffer + uint32_t allocate_buffer_len = 0; + do { APF_TRACE_HOOK(pc, registers, program, program_len, packet, packet_len, memory, ram_len); if (pc == program_len) { @@ -273,6 +279,11 @@ int apf_run(uint8_t* program, uint32_t program_len, uint32_t ram_len, case MOV_EXT_OPCODE: REG = OTHER_REG; break; + case ALLOC_EXT_OPCODE: + allocate_buffer_len = REG; + allocated_buffer = apf_allocate_buffer(allocate_buffer_len); + ASSERT_RETURN(allocated_buffer != NULL); + break; // Unknown extended opcode default: // Bail out |