From 61fa8b855ed8581bcd4d2c4b9c20bd0f75e44545 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 20 Aug 2025 10:25:01 +0200 Subject: [PATCH] Added more tests --- src/codegen/reorderOperands.go | 9 ++++++++- tests/reorder-add.q | 7 +++++++ tests/script.q | 1 - tests/tests_test.go | 7 ++++--- 4 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 tests/reorder-add.q diff --git a/src/codegen/reorderOperands.go b/src/codegen/reorderOperands.go index 3573919..29689c8 100644 --- a/src/codegen/reorderOperands.go +++ b/src/codegen/reorderOperands.go @@ -6,7 +6,8 @@ import ( ) // reorderOperands reorders the left and right operands of a binary operation -// so that the left operand matches the destination register. +// so that the left operand matches the destination register. It will also +// try to match the right operand with an integer immediate. func (f *Function) reorderOperands(step *Step, instr *ssa.BinaryOp) { if instr.Op != token.Add { return @@ -35,7 +36,13 @@ func (f *Function) reorderOperands(step *Step, instr *ssa.BinaryOp) { return } } + } + + _, leftIsInt := leftStep.Value.(*ssa.Int) + _, rightIsInt := rightStep.Value.(*ssa.Int) + if leftIsInt && !rightIsInt { + instr.Left, instr.Right = instr.Right, instr.Left return } } \ No newline at end of file diff --git a/tests/reorder-add.q b/tests/reorder-add.q new file mode 100644 index 0000000..9b88dc0 --- /dev/null +++ b/tests/reorder-add.q @@ -0,0 +1,7 @@ +main() { + assert inc(1) == 2 +} + +inc(x int) -> int { + return 1 + x +} \ No newline at end of file diff --git a/tests/script.q b/tests/script.q index 20a64a7..f923d1c 100755 --- a/tests/script.q +++ b/tests/script.q @@ -1,5 +1,4 @@ #!/usr/bin/env q - import io main() { diff --git a/tests/tests_test.go b/tests/tests_test.go index 8036882..d083a3c 100644 --- a/tests/tests_test.go +++ b/tests/tests_test.go @@ -42,6 +42,9 @@ var tests = []run{ {"phi", "", "", 0}, {"phi-simple", "", "", 0}, {"phi-advanced", "", "", 0}, + {"hello", "", "Hello\nHello\nHello\n", 0}, + {"escape", "", "a\tb\nc\td\n", 0}, + {"script", "", "Hello\n", 0}, {"loop", "", "..........", 0}, {"loop-stacked", "", "..........", 0}, {"loop-count", "", "", 0}, @@ -65,9 +68,7 @@ var tests = []run{ {"return-string", "", "Hello\n", 0}, {"ignore-unused-field", "", "", 0}, {"slice", "", "Hello World", 0}, - {"hello", "", "Hello\nHello\nHello\n", 0}, - {"escape", "", "a\tb\nc\td\n", 0}, - {"script", "", "Hello\n", 0}, + {"reorder-add", "", "", 0}, } func TestTests(t *testing.T) { -- 2.51.2