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 v1 3/3] parser: codegen(x86_64): add bitwise not unary op support
Date: Wed,  9 Oct 2024 23:18:29 +0200	[thread overview]
Message-ID: <20241009213425.412949-4-johnny@johnnyrichard.com> (raw)
In-Reply-To: <20241009213425.412949-1-johnny@johnnyrichard.com>

Currently, all compiler tests rely on the exit code returned by a binary
program, which restricts our testing to 8-bit unsigned numbers.

To effectively test the bitwise-not unary operation within a main
function, we utilize UINT32_MAX (4294967295), which represents a value
with all bits set. The result of applying the bitwise-not operation to 0
is then used as the exit code.

Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
 src/ast.c                                    | 14 ++++++++
 src/ast.h                                    | 22 ++++++++++++
 src/checker.c                                |  7 ++++
 src/codegen_linux_x86_64.c                   | 18 ++++++++++
 src/parser.c                                 | 35 ++++++++++++++++++++
 src/pretty_print_ast.c                       | 22 +++++++++++-
 tests/olc/0033_unary_operator_bitwise_not.ol | 16 +++++++--
 7 files changed, 131 insertions(+), 3 deletions(-)

diff --git a/src/ast.c b/src/ast.c
index 79daca6..5a36ccb 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -117,6 +117,20 @@ ast_new_node_bin_op(arena_t *arena, token_loc_t loc, ast_binary_op_kind_t kind,
     return node_bin_op;
 }
 
+ast_node_t *
+ast_new_node_unary_op(arena_t *arena, token_loc_t loc, ast_unary_op_kind_t kind, ast_node_t *expr)
+{
+    ast_node_t *node_unary_op = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
+    assert(node_unary_op);
+
+    node_unary_op->kind = AST_NODE_UNARY_OP;
+    node_unary_op->loc = loc;
+    node_unary_op->as_unary_op.kind = kind;
+    node_unary_op->as_unary_op.expr = expr;
+
+    return node_unary_op;
+}
+
 ast_node_t *
 ast_new_node_literal_u32(arena_t *arena, token_loc_t loc, uint32_t value)
 {
diff --git a/src/ast.h b/src/ast.h
index fccc303..ee94f57 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -36,6 +36,7 @@ typedef enum
     AST_NODE_FN_CALL,
     AST_NODE_VAR_DEF,
     AST_NODE_BINARY_OP,
+    AST_NODE_UNARY_OP,
     AST_NODE_VAR_ASSIGN_STMT,
     AST_NODE_RETURN_STMT,
     AST_NODE_IF_STMT,
@@ -148,6 +149,23 @@ typedef struct ast_binary_op
     ast_node_t *rhs;
 } ast_binary_op_t;
 
+typedef enum ast_unary_op_kind
+{
+    AST_UNARY_BITWISE_NOT,
+    AST_UNARY_LOGICAL_NOT,
+    AST_UNARY_NEGATIVE,
+    AST_UNARY_POSITIVE,
+    AST_UNARY_DEREFERENCE,
+    AST_UNARY_ADDRESSOF,
+} ast_unary_op_kind_t;
+
+typedef struct ast_unary_op
+{
+    ast_node_meta_t meta;
+    ast_unary_op_kind_t kind;
+    ast_node_t *expr;
+} ast_unary_op_t;
+
 typedef struct ast_var_assign_stmt
 {
     ast_node_meta_t meta;
@@ -189,6 +207,7 @@ typedef union ast_node
     ast_fn_call_t as_fn_call;
     ast_var_definition_t as_var_def;
     ast_binary_op_t as_bin_op;
+    ast_unary_op_t as_unary_op;
     ast_literal_t as_literal;
     ast_ref_t as_ref;
     ast_block_t as_block;
@@ -218,6 +237,9 @@ ast_new_node_var_def(arena_t *arena, token_loc_t loc, string_view_t id, type_t *
 ast_node_t *
 ast_new_node_bin_op(arena_t *arena, token_loc_t loc, ast_binary_op_kind_t kind, ast_node_t *lhs, ast_node_t *rhs);
 
+ast_node_t *
+ast_new_node_unary_op(arena_t *arena, token_loc_t loc, ast_unary_op_kind_t kind, ast_node_t *expr);
+
 ast_node_t *
 ast_new_node_literal_u32(arena_t *arena, token_loc_t loc, uint32_t value);
 
diff --git a/src/checker.c b/src/checker.c
index 7a9a7b6..62d612f 100644
--- a/src/checker.c
+++ b/src/checker.c
@@ -177,6 +177,13 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
             return;
         }
 
+        case AST_NODE_UNARY_OP: {
+            ast_unary_op_t unary_op = ast->as_unary_op;
+
+            populate_scope(checker, scope, unary_op.expr);
+            return;
+        }
+
         case AST_NODE_VAR_ASSIGN_STMT: {
             ast_var_assign_stmt_t var_assign_stmt = ast->as_var_assign_stmt;
 
diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index ac268ad..ae28aa5 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -530,6 +530,24 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr
                 }
             }
         }
+
+        case AST_NODE_UNARY_OP: {
+            ast_unary_op_t unary_op = expr_node->as_unary_op;
+            switch (unary_op.kind) {
+                case AST_UNARY_BITWISE_NOT: {
+                    size_in_bytes_t expr_bytes = codegen_linux_x86_64_emit_expression(codegen, unary_op.expr);
+
+                    fprintf(codegen->out, "    not %s\n", get_reg_for(REG_ACCUMULATOR, expr_bytes));
+
+                    return expr_bytes;
+                }
+                default: {
+                    assert(0 && "unsupported unary operation");
+                    return 0;
+                }
+            }
+        }
+
         default:
             assert(0 && "unsupported expression");
     }
diff --git a/src/parser.c b/src/parser.c
index 0bfa282..f712bfc 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -209,6 +209,27 @@ get_binary_op_precedence(token_kind_t kind)
     }
 }
 
+static ast_unary_op_kind_t
+token_kind_to_unary_op_kind(token_kind_t token_kind)
+{
+    switch (token_kind) {
+        case TOKEN_AND:
+            return AST_UNARY_ADDRESSOF;
+        case TOKEN_STAR:
+            return AST_UNARY_DEREFERENCE;
+        case TOKEN_PLUS:
+            return AST_UNARY_POSITIVE;
+        case TOKEN_DASH:
+            return AST_UNARY_NEGATIVE;
+        case TOKEN_TILDE:
+            return AST_UNARY_BITWISE_NOT;
+        case TOKEN_BANG:
+            return AST_UNARY_LOGICAL_NOT;
+        default:
+            assert(false && "unable to covert the token_kind_t to unary_op_kind_t");
+    }
+}
+
 static ast_node_t *
 parser_parse_expr_1(parser_t *parser, ast_node_t *lhs, size_t prev_precedence)
 {
@@ -275,6 +296,20 @@ parser_parse_factor(parser_t *parser)
 
             return ast_new_node_ref(parser->arena, token_id.loc, token_id.value);
         }
+        case TOKEN_AND:
+        case TOKEN_STAR:
+        case TOKEN_PLUS:
+        case TOKEN_DASH:
+        case TOKEN_TILDE:
+        case TOKEN_BANG: {
+            ast_node_t *expr = parser_parse_expr(parser);
+            if (expr == NULL) {
+                return NULL;
+            }
+
+            ast_unary_op_kind_t kind = token_kind_to_unary_op_kind(token.kind);
+            return ast_new_node_unary_op(parser->arena, token.loc, kind, expr);
+        }
 
         case TOKEN_OPAREN: {
             ast_node_t *expr = parser_parse_expr(parser);
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index edc6320..3a42412 100644
--- a/src/pretty_print_ast.c
+++ b/src/pretty_print_ast.c
@@ -258,7 +258,7 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
             char name[256];
             switch (literal.kind) {
                 case AST_LITERAL_U32: {
-                    sprintf(name, "Literal <kind:u32> <value:%d>", literal.as_u32);
+                    sprintf(name, "Literal <kind:u32> <value:%u>", literal.as_u32);
                     node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1));
                     strcpy(node->name, name);
                     break;
@@ -383,6 +383,26 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
 
             return node;
         }
+
+        case AST_NODE_UNARY_OP: {
+            pretty_print_node_t *node = pretty_print_node_new(arena);
+            ast_unary_op_t unary_op = ast->as_unary_op;
+
+            switch (unary_op.kind) {
+                case AST_UNARY_BITWISE_NOT: {
+                    node->name = "Unary_Operation (~)";
+                    break;
+                }
+                default:
+                    assert(false && "unary operation kind not implemented");
+            }
+
+            pretty_print_node_t *expr = ast_node_to_pretty_print_node(unary_op.expr, arena);
+            list_append(node->children, expr);
+
+            return node;
+        }
+
         default: {
             printf("node kind = '%d' not implmented\n", ast->kind);
             assert(false);
diff --git a/tests/olc/0033_unary_operator_bitwise_not.ol b/tests/olc/0033_unary_operator_bitwise_not.ol
index 1891549..7a9f59d 100644
--- a/tests/olc/0033_unary_operator_bitwise_not.ol
+++ b/tests/olc/0033_unary_operator_bitwise_not.ol
@@ -18,10 +18,22 @@ fn main(): u32 {
   return ~e
 }
 
-# XTEST test_compile(exit_code=0)
+# TEST test_compile(exit_code=0)
 #
-# XTEST test_run_binary(exit_code=0)
+# TEST test_run_binary(exit_code=0)
 #
 # TEST test_contains_tokens WITH
 # ./0033_unary_operator_bitwise_not.ol:18:10: <~>
 # END
+#
+# TEST test_ast WITH
+# Translation_Unit
+# `-Function_Definition <name:main> <return:u32>
+#   `-Block
+#     |-Var_Definition <name:e> <kind:u32>
+#     | `-Literal <kind:u32> <value:4294967295>
+#     `-Return_Statement
+#       `-Unary_Operation (~)
+#         `-Reference <name:e>
+# END
+
-- 
2.46.0


  parent reply	other threads:[~2024-10-09 19:34 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-09 21:18 [PATCH olang v1 0/3] implement unary operator for bitwise not Johnny Richard
2024-10-09 21:18 ` [PATCH olang v1 1/3] doc: add unary expression to language spec Johnny Richard
2024-10-09 21:18 ` [PATCH olang v1 2/3] lexer: add tilde token support Johnny Richard
2024-10-09 21:18 ` Johnny Richard [this message]
2024-10-09 19:35   ` [olang/patches/.build.yml] build success builds.sr.ht
2024-10-09 23:00 ` [PATCH olang v1 0/3] implement unary operator for bitwise not Carlos Maniero

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=20241009213425.412949-4-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