public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
From: Johnny Richard <johnny@johnnyrichard.com>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Johnny Richard <johnny@johnnyrichard.com>
Subject: [PATCH olang v2 4/4] codegen: x86_64: add if cond support for literals
Date: Sun,  8 Sep 2024 03:10:58 +0200	[thread overview]
Message-ID: <20240908011512.152684-6-johnny@johnnyrichard.com> (raw)
In-Reply-To: <20240908011512.152684-2-johnny@johnnyrichard.com>

We are not optimizing the assembly code, so every literal statement will
emit a `cmp` instruction in order achieve if condition support for
literals.

Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
 src/codegen_linux_x86_64.c                    |  1 +
 .../tests/0022_if_statement_literal.ol        | 24 +++++++++++++++++++
 2 files changed, 25 insertions(+)
 create mode 100644 tests/integration/tests/0022_if_statement_literal.ol

diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index 3b509ba..958c46e 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -328,6 +328,7 @@ codegen_linux_x86_64_emit_block(FILE *out, ast_block_t *block)
                 size_t end_if_label = codegen_linux_x86_64_get_next_label();
 
                 codegen_linux_x86_64_emit_expression(out, cond);
+                fprintf(out, "    cmp $1, %%rax\n");
                 fprintf(out, "    jnz .L%ld\n", end_if_label);
 
                 assert(then->kind == AST_NODE_BLOCK && "invalid if-then block");
diff --git a/tests/integration/tests/0022_if_statement_literal.ol b/tests/integration/tests/0022_if_statement_literal.ol
new file mode 100644
index 0000000..3ae6ef3
--- /dev/null
+++ b/tests/integration/tests/0022_if_statement_literal.ol
@@ -0,0 +1,24 @@
+# Copyright (C) 2024 olang mantainers
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+fn main(): u32 {
+  if 1 {
+    return 0
+  }
+  return 1
+}
+
+# TEST test_compile(exit_code=0)
+# TEST test_run_binary(exit_code=0)
-- 
2.46.0


  parent reply	other threads:[~2024-09-07 23:16 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-08  1:10 [PATCH olang v2 0/4] add support to compile if statements Johnny Richard
2024-09-07 23:23 ` Carlos Maniero
2024-09-08  1:10 ` [PATCH olang v2 1/4] lexer: add support to if token Johnny Richard
2024-09-08  1:10 ` [PATCH olang v2 2/4] parser: parse a simple if statement Johnny Richard
2024-09-08  1:10 ` [PATCH olang v2 3/4] codegen: x86_64: add support to emit if statements Johnny Richard
2024-09-08  1:10 ` Johnny Richard [this message]
2024-09-08  1:36 ` [PATCH olang v2 0/4] add support to compile " Johnny Richard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240908011512.152684-6-johnny@johnnyrichard.com \
    --to=johnny@johnnyrichard.com \
    --cc=~johnnyrichard/olang-devel@lists.sr.ht \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.johnnyrichard.com/olang.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox