From 79995d800241b742e8214b95e9907b77ff52db60 Mon Sep 17 00:00:00 2001 From: Ubaid Date: Fri, 18 Nov 2022 02:38:09 +0530 Subject: [PATCH 1/4] X86Assembler: Support printing negative integers --- src/libasr/codegen/x86_assembler.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/libasr/codegen/x86_assembler.cpp b/src/libasr/codegen/x86_assembler.cpp index 12373385c7..cd3a409725 100644 --- a/src/libasr/codegen/x86_assembler.cpp +++ b/src/libasr/codegen/x86_assembler.cpp @@ -133,6 +133,21 @@ void emit_print_int(X86Assembler &a, const std::string &name) X86Reg base = X86Reg::ebp; // mov eax, [ebp+8] // argument "i" a.asm_mov_r32_m32(X86Reg::eax, &base, nullptr, 1, 8); + + a.asm_mov_r32_r32(X86Reg::ecx, X86Reg::eax); // make a copy in ecx + a.asm_mov_r32_imm32(X86Reg::ebx, 0); + a.asm_cmp_r32_r32(X86Reg::eax, X86Reg::ebx); + a.asm_jge_label(".print_int_"); // if num >= 0 then print it + + // print "-" and then negate the integer + emit_print(a, "string-", 1U); + // ecx value changed during print so fetch back + a.asm_mov_r32_m32(X86Reg::ecx, &base, nullptr, 1, 8); + a.asm_neg_r32(X86Reg::ecx); + + a.add_label(".print_int_"); + + a.asm_mov_r32_r32(X86Reg::eax, X86Reg::ecx); // fetch the val in ecx back to eax a.asm_xor_r32_r32(X86Reg::esi, X86Reg::esi); a.add_label(".loop"); From da4f8fc75d645fe9d5419735f4544644ef99ed58 Mon Sep 17 00:00:00 2001 From: Ubaid Date: Fri, 18 Nov 2022 02:39:10 +0530 Subject: [PATCH 2/4] Declare compile-time string '-' in x86 and wasm_x86 --- src/libasr/codegen/asr_to_x86.cpp | 1 + src/libasr/codegen/wasm_to_x86.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/src/libasr/codegen/asr_to_x86.cpp b/src/libasr/codegen/asr_to_x86.cpp index 336c89088a..fe24a0063c 100644 --- a/src/libasr/codegen/asr_to_x86.cpp +++ b/src/libasr/codegen/asr_to_x86.cpp @@ -141,6 +141,7 @@ class ASRToX86Visitor : public ASR::BaseVisitor for (auto &s : m_global_strings) { emit_data_string(m_a, s.first, s.second); } + emit_data_string(m_a, "string-", "-"); // - symbol for printing negative ints } diff --git a/src/libasr/codegen/wasm_to_x86.cpp b/src/libasr/codegen/wasm_to_x86.cpp index 1aaa00ffa2..bbc3dc63b4 100644 --- a/src/libasr/codegen/wasm_to_x86.cpp +++ b/src/libasr/codegen/wasm_to_x86.cpp @@ -351,6 +351,7 @@ class X86Visitor : public WASMDecoder, std::string label = "string" + std::to_string(s.first); emit_data_string(m_a, label, s.second); } + emit_data_string(m_a, "string-", "-"); // - symbol for printing negative ints emit_elf32_footer(m_a); } From 08617aea3e228aeccebf00278dec0140a2f54ea5 Mon Sep 17 00:00:00 2001 From: Ubaid Date: Fri, 18 Nov 2022 02:40:54 +0530 Subject: [PATCH 3/4] TEST: WASM_X86: Add test for print neg int --- integration_tests/print_03.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/integration_tests/print_03.py b/integration_tests/print_03.py index 46f5120a31..c066d1c81d 100644 --- a/integration_tests/print_03.py +++ b/integration_tests/print_03.py @@ -1,7 +1,13 @@ def Main0(): x: i32 - x = (2+3)*5 + x = (2+3)* (-5) + y: i32 = 100 + z: i32 = 2147483647 + w: i32 = -2147483648 print(x) + print(y) + print(z) + print(w) print("Hi") print("Hello") print((5-2) * 7) From 6767b2b064407ed802f57652c86fe8fef7217283 Mon Sep 17 00:00:00 2001 From: Ubaid Date: Fri, 18 Nov 2022 03:04:15 +0530 Subject: [PATCH 4/4] Fix CTEST failure: declare '-' at print_i32() end --- src/libasr/codegen/asr_to_x86.cpp | 1 - src/libasr/codegen/wasm_to_x86.cpp | 1 - src/libasr/codegen/x86_assembler.cpp | 2 ++ 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libasr/codegen/asr_to_x86.cpp b/src/libasr/codegen/asr_to_x86.cpp index fe24a0063c..336c89088a 100644 --- a/src/libasr/codegen/asr_to_x86.cpp +++ b/src/libasr/codegen/asr_to_x86.cpp @@ -141,7 +141,6 @@ class ASRToX86Visitor : public ASR::BaseVisitor for (auto &s : m_global_strings) { emit_data_string(m_a, s.first, s.second); } - emit_data_string(m_a, "string-", "-"); // - symbol for printing negative ints } diff --git a/src/libasr/codegen/wasm_to_x86.cpp b/src/libasr/codegen/wasm_to_x86.cpp index bbc3dc63b4..1aaa00ffa2 100644 --- a/src/libasr/codegen/wasm_to_x86.cpp +++ b/src/libasr/codegen/wasm_to_x86.cpp @@ -351,7 +351,6 @@ class X86Visitor : public WASMDecoder, std::string label = "string" + std::to_string(s.first); emit_data_string(m_a, label, s.second); } - emit_data_string(m_a, "string-", "-"); // - symbol for printing negative ints emit_elf32_footer(m_a); } diff --git a/src/libasr/codegen/x86_assembler.cpp b/src/libasr/codegen/x86_assembler.cpp index cd3a409725..50dd196cdb 100644 --- a/src/libasr/codegen/x86_assembler.cpp +++ b/src/libasr/codegen/x86_assembler.cpp @@ -198,6 +198,8 @@ void emit_print_int(X86Assembler &a, const std::string &name) a.asm_mov_r32_r32(X86Reg::esp, X86Reg::ebp); a.asm_pop_r32(X86Reg::ebp); a.asm_ret(); + + emit_data_string(a, "string-", "-"); // - symbol for printing negative ints } } // namespace LFortran