Add support for ALLOC and TRANS opcodes to apf_disassemble()

Support ALLOC and TRANS opcodes in apf_disassemble() and export it in
libapfdisassembler for JNI use.

Bug: 293811969
Test: TH
Change-Id: Ie077ab9f4eefbfac15e0da1ccb101fc39df33ec5
diff --git a/Android.bp b/Android.bp
index 5f6648a..742f49c 100644
--- a/Android.bp
+++ b/Android.bp
@@ -40,6 +40,15 @@
     sdk_version: "24",
 }
 
+cc_library_static {
+    name: "libapfdisassembler",
+    defaults: ["apf_defaults"],
+    srcs: [
+        "disassembler.c",
+    ],
+    sdk_version: "24",
+}
+
 cc_binary_host {
     name: "apf_disassembler",
     defaults: ["apf_defaults"],
diff --git a/disassembler.c b/disassembler.c
index 830e621..091ff9e 100644
--- a/disassembler.c
+++ b/disassembler.c
@@ -17,7 +17,7 @@
 #include <stdint.h>
 #include <stdio.h>
 
-#include "apf.h"
+#include "v5/apf.h"
 
 // If "c" is of a signed type, generate a compile warning that gets promoted to an error.
 // This makes bounds checking simpler because ">= 0" can be avoided. Otherwise adding
@@ -344,6 +344,28 @@
                     ASSERT_RET_INBOUND(ret);
                     offset += ret;
                     break;
+                case ALLOC_EXT_OPCODE:
+                    ret = print_opcode("alloc", output_buffer,
+                                       output_buffer_len, offset);
+                    ASSERT_RET_INBOUND(ret);
+                    offset += ret;
+                    ret =
+                        snprintf(output_buffer + offset,
+                                 output_buffer_len - offset, "r%d", reg_num);
+                    ASSERT_RET_INBOUND(ret);
+                    offset += ret;
+                    break;
+                case TRANS_EXT_OPCODE:
+                    ret = print_opcode("trans", output_buffer,
+                                       output_buffer_len, offset);
+                    ASSERT_RET_INBOUND(ret);
+                    offset += ret;
+                    ret =
+                        snprintf(output_buffer + offset,
+                                 output_buffer_len - offset, "r%d", reg_num);
+                    ASSERT_RET_INBOUND(ret);
+                    offset += ret;
+                    break;
                 default:
                     ret = snprintf(output_buffer + offset,
                                    output_buffer_len - offset, "unknown_ext %u",
diff --git a/disassembler.h b/disassembler.h
index f4c36f6..cf10166 100644
--- a/disassembler.h
+++ b/disassembler.h
@@ -18,6 +18,26 @@
 
 #include <stdint.h>
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * Disassembles a APF program into a human-readable format.
+ *
+ * @param program the program bytecode.
+ * @param program_len the length of the program bytecode.
+ * @param pc The program counter which point to the current instruction.
+ * @param output_buffer A pointer to a buffer where the disassembled
+ *                      instruction will be stored.
+ * @param output_buffer_len the length of the output buffer.
+ *
+ * @return the program counter which point to the next instruction.
+ */
 uint32_t apf_disassemble(const uint8_t* program, uint32_t program_len,
                          uint32_t pc, char* output_buffer,
                          int output_buffer_len);
+
+#ifdef __cplusplus
+}
+#endif