From a0538fc9f0fe8bb4177eba14fafe4fc013e1f504 Mon Sep 17 00:00:00 2001 From: Pan Chasinga Date: Sat, 7 Oct 2023 14:16:20 -0700 Subject: [PATCH] Add setl function --- modules/lib/asm_x86.wat | 30 ++++++++++++++++++++++++++++++ modules/lib/tests.wat | 25 +++++++++++++++++++++++++ rts | 2 +- 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/modules/lib/asm_x86.wat b/modules/lib/asm_x86.wat index 0355e45..66dda08 100644 --- a/modules/lib/asm_x86.wat +++ b/modules/lib/asm_x86.wat @@ -1,6 +1,36 @@ (module (type $t0 (func (param i32 i32) (result i32))) (type $t1 (func (param i32 i32) (result i32))) + (func $setl (export "setl") (param $cmpl_flag i32) (param $val i32) (result i32) + (local $var i32) + local.get $cmpl_flag + i32.const 0 + i32.lt_s + (if + (then + local.get $cmpl_flag + i32.const 1 + i32.and + local.set $var + ) + (else + local.get $cmpl_flag + i32.const 0 + i32.and + local.set $var + ) + ) + local.get $var + (if + (then + local.get $val + i32.const 0xff + i32.or + return + ) + ) + local.get $val + ) (func $sete (export "sete") (param $zf i32) (param $val i32) (result i32) ;; - arg0 | $zf is the zero flag ;; - arg1 | $val is the value to set diff --git a/modules/lib/tests.wat b/modules/lib/tests.wat index 006e6cc..604f1d9 100644 --- a/modules/lib/tests.wat +++ b/modules/lib/tests.wat @@ -1,6 +1,31 @@ (module (import "asm_x86" "sete" (func $sete (param i32 i32) (result i32))) (import "asm_x86" "cmpl" (func $cmpl (param i32 i32) (result i32))) + (import "asm_x86" "setl" (func $setl (param i32 i32) (result i32))) + (func $test_setl_eq (export "test_setl_eq") (result i32) + i32.const 3 + i32.const 3 + ;; 3 == 3 so setl should return 0 + call $cmpl + i32.const 0 + call $setl + ) + (func $test_setl_mt (export "test_setl_mt") (result i32) + i32.const 4 + i32.const 3 + ;; 4 > 3 so setl should return 0 + call $cmpl + i32.const 0 + call $setl + ) + (func $test_setl_lt (export "test_setl_lt") (result i32) + i32.const 1 + i32.const 3 + ;; 1 < 3 so setl should return 255 + call $cmpl + i32.const 0 + call $setl + ) (func $test_sete_eq (export "test_sete_eq") (result i32) i32.const 3 i32.const 3 diff --git a/rts b/rts index 45c68dd..37b0040 160000 --- a/rts +++ b/rts @@ -1 +1 @@ -Subproject commit 45c68dd956b929184850b59a2cc839d3c76fdd2a +Subproject commit 37b00405843f1bac91d1a8f74ad989153ff2b9c1