[前][次][番号順一覧][スレッド一覧]

ruby-changes:68572

From: Maxime <ko1@a...>
Date: Thu, 21 Oct 2021 08:09:44 +0900 (JST)
Subject: [ruby-changes:68572] 7eb192d644 (master): Testing lea instruction properly

https://git.ruby-lang.org/ruby.git/commit/?id=7eb192d644

From 7eb192d644996cb2c79663d34e3364adb7dface5 Mon Sep 17 00:00:00 2001
From: Maxime Chevalier-Boisvert <maxime.chevalierboisvert@s...>
Date: Wed, 16 Sep 2020 12:37:59 -0400
Subject: Testing lea instruction properly

---
 ujit_asm_tests.c | 31 +++++++++++++++++++++++++------
 ujit_compile.c   |  6 ++++--
 2 files changed, 29 insertions(+), 8 deletions(-)

diff --git a/ujit_asm_tests.c b/ujit_asm_tests.c
index 650762ff46..6b8c7c695d 100644
--- a/ujit_asm_tests.c
+++ b/ujit_asm_tests.c
@@ -4,6 +4,17 @@ https://github.com/ruby/ruby/blob/trunk/ujit_asm_tests.c#L4
 #include <assert.h>
 #include "ujit_asm.h"
 
+// Print the bytes in a code block
+void print_bytes(codeblock_t* cb)
+{
+    for (size_t i = 0; i < cb->write_pos; ++i)
+    {
+        printf("%02X", (int)cb->mem_block[i]);
+    }
+
+    printf("\n");
+}
+
 // Check that the code block contains the given sequence of bytes
 void check_bytes(codeblock_t* cb, const char* bytes)
 {
@@ -15,7 +26,12 @@ void check_bytes(codeblock_t* cb, const char* bytes) https://github.com/ruby/ruby/blob/trunk/ujit_asm_tests.c#L26
 
     if (cb->write_pos != num_bytes)
     {
-        fprintf(stderr, "incorrect encoding length %ld, expected %ld\n", cb->write_pos, num_bytes);
+        fprintf(stderr, "incorrect encoding length, expected %ld, got %ld\n",
+            num_bytes,
+            cb->write_pos
+        );
+        printf("%s\n", bytes);
+        print_bytes(cb);
         exit(-1);
     }
 
@@ -30,11 +46,13 @@ void check_bytes(codeblock_t* cb, const char* bytes) https://github.com/ruby/ruby/blob/trunk/ujit_asm_tests.c#L46
 
         if (cb_byte != byte)
         {
-            fprintf(stderr, "incorrect encoding at position %ld, got %02X, expected %02X\n",
+            fprintf(stderr, "incorrect encoding at position %ld, expected %02X, got %02X\n",
                 i,
-                (int)cb_byte,
-                (int)byte
+                (int)byte,
+                (int)cb_byte
             );
+            printf("%s\n", bytes);
+            print_bytes(cb);
             exit(-1);
         }
     }
@@ -167,9 +185,10 @@ void run_tests() https://github.com/ruby/ruby/blob/trunk/ujit_asm_tests.c#L185
     cb_set_pos(cb, 0); jmp_rm(cb, R12); check_bytes(cb, "41FFE4");
 
     // lea
-    //cb_set_pos(cb, 0); lea(cb, EBX, mem_opnd(32, RSP, 4)); check_bytes(cb, "8D5C2404");
     cb_set_pos(cb, 0); lea(cb, RDX, mem_opnd(64, RCX, 8)); check_bytes(cb, "488D5108");
-    //cb_set_pos(cb, 0); lea(cb, RAX, mem_opnd(8, RIP, 5)); check_bytes(cb, "488D042505000000");
+    cb_set_pos(cb, 0); lea(cb, RAX, mem_opnd(8, RIP, 0)); check_bytes(cb, "488D0500000000");
+    cb_set_pos(cb, 0); lea(cb, RAX, mem_opnd(8, RIP, 5)); check_bytes(cb, "488D0505000000");
+    cb_set_pos(cb, 0); lea(cb, RDI, mem_opnd(8, RIP, 5)); check_bytes(cb, "488D3D05000000");
 
     // mov
     cb_set_pos(cb, 0); mov(cb, EAX, imm_opnd(7)); check_bytes(cb, "B807000000");
diff --git a/ujit_compile.c b/ujit_compile.c
index 2a99a85e45..bd2d8697eb 100644
--- a/ujit_compile.c
+++ b/ujit_compile.c
@@ -85,8 +85,11 @@ VALUE ctx_get_arg(ctx_t* ctx, size_t arg_idx) https://github.com/ruby/ruby/blob/trunk/ujit_compile.c#L85
 Generate a chunk of machine code for one individual bytecode instruction
 Eventually, this will handle multiple instructions in a sequence
 
-MicroJIT code gets a pointer to the cfp as the first argument in RSI
+MicroJIT code gets a pointer to the cfp as the first argument in RDI
 See rb_ujit_empty_func(rb_control_frame_t *cfp) in iseq.c
+
+System V ABI reference:
+https://wiki.osdev.org/System_V_ABI#x86-64
 */
 uint8_t *
 ujit_compile_insn(rb_iseq_t *iseq, size_t insn_idx)
@@ -186,7 +189,6 @@ void gen_getlocal_wc0(codeblock_t* cb, ctx_t* ctx) https://github.com/ruby/ruby/blob/trunk/ujit_compile.c#L189
     // Load block pointer from CFP
     mov(cb, RDX, mem_opnd(64, RDI, 32));
 
-    // TODO: we may want a macro or helper function to get insn operands
     // Compute the offset from BP to the local
     int32_t local_idx = (int32_t)ctx_get_arg(ctx, 0);
     const int32_t offs = -8 * local_idx;
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]