From d6348f18697dab13597c2ae6890aa94701184799 Mon Sep 17 00:00:00 2001
From: Maxime Chevalier-Boisvert <maxime.chevalierboisvert@shopify.com>
Date: Wed, 14 Oct 2020 14:03:31 -0400
Subject: [PATCH] Added print_ptr() util function for debugging

---
 ujit_compile.c |  7 -------
 ujit_utils.c   | 19 +++++++++++++++++++
 ujit_utils.h   |  1 +
 3 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/ujit_compile.c b/ujit_compile.c
index 2b6fad3a72..67162fbd21 100644
--- a/ujit_compile.c
+++ b/ujit_compile.c
@@ -426,13 +426,6 @@ void gen_opt_minus(codeblock_t* cb, codeblock_t* ocb, ctx_t* ctx)
     jo_ptr(cb, side_exit);
     add(cb, RAX, imm_opnd(1));
 
-    /*
-    print_int(cb, arg0);
-    print_int(cb, arg1);
-    print_int(cb, RAX);
-    print_str(cb, "");
-    */
-
     // Push the output on the stack
     x86opnd_t dst = ctx_stack_push(ctx, 1);
     mov(cb, dst, RAX);
diff --git a/ujit_utils.c b/ujit_utils.c
index 0594503924..b7d121c9ef 100644
--- a/ujit_utils.c
+++ b/ujit_utils.c
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <string.h>
+#include <assert.h>
 #include "ujit_utils.h"
 #include "ujit_asm.h"
 
@@ -54,6 +55,24 @@ void print_int(codeblock_t* cb, x86opnd_t opnd)
     pop_regs(cb);
 }
 
+static void print_ptr_cfun(int64_t val)
+{
+    printf("%llX\n", val);
+}
+
+void print_ptr(codeblock_t* cb, x86opnd_t opnd)
+{
+    assert (opnd.num_bits == 64);
+
+    push_regs(cb);
+
+    mov(cb, RDI, opnd);
+    mov(cb, RAX, const_ptr_opnd((void*)&print_ptr_cfun));
+    call(cb, RAX);
+
+    pop_regs(cb);
+}
+
 static void print_str_cfun(const char* str)
 {
     printf("%s\n", str);
diff --git a/ujit_utils.h b/ujit_utils.h
index 5244d59734..005d928511 100644
--- a/ujit_utils.h
+++ b/ujit_utils.h
@@ -9,6 +9,7 @@
 void push_regs(codeblock_t* cb);
 void pop_regs(codeblock_t* cb);
 void print_int(codeblock_t* cb, x86opnd_t opnd);
+void print_ptr(codeblock_t* cb, x86opnd_t opnd);
 void print_str(codeblock_t* cb, const char* str);
 
 #endif // #ifndef UJIT_UTILS_H