aboutsummaryrefslogtreecommitdiff
path: root/src/llvm_backend.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2021-05-16 14:44:02 +0100
committergingerBill <bill@gingerbill.org>2021-05-16 14:44:02 +0100
commit6ef96d33003d2fbdedd283ea432e70afc2f1d7ec (patch)
tree785fee0309dea6a1f9c786a626ee2ed3541fca9c /src/llvm_backend.cpp
parent2e633f57a065b09c9b0c7ad16f393762475a308a (diff)
Improve untyped to typed logic for aiding the backend
Diffstat (limited to 'src/llvm_backend.cpp')
-rw-r--r--src/llvm_backend.cpp60
1 files changed, 53 insertions, 7 deletions
diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp
index c46c4fd85..a15b6a172 100644
--- a/src/llvm_backend.cpp
+++ b/src/llvm_backend.cpp
@@ -3409,6 +3409,20 @@ void lb_emit_if(lbProcedure *p, lbValue cond, lbBlock *true_block, lbBlock *fals
LLVMBuildCondBr(p->builder, cv, true_block->block, false_block->block);
}
+bool lb_is_expr_untyped_const(Ast *expr) {
+ auto const &tv = type_and_value_of_expr(expr);
+ if (is_type_untyped(tv.type)) {
+ return tv.value.kind != ExactValue_Invalid;
+ }
+ return false;
+}
+
+lbValue lb_expr_untyped_const_to_typed(lbModule *m, Ast *expr, Type *t) {
+ GB_ASSERT(is_type_typed(t));
+ auto const &tv = type_and_value_of_expr(expr);
+ return lb_const_value(m, t, tv.value);
+}
+
lbValue lb_build_cond(lbProcedure *p, Ast *cond, lbBlock *true_block, lbBlock *false_block) {
GB_ASSERT(cond != nullptr);
GB_ASSERT(true_block != nullptr);
@@ -3440,8 +3454,13 @@ lbValue lb_build_cond(lbProcedure *p, Ast *cond, lbBlock *true_block, lbBlock *f
case_end;
}
- lbValue v = lb_build_expr(p, cond);
- // v = lb_emit_conv(p, v, t_bool);
+ lbValue v = {};
+ if (lb_is_expr_untyped_const(cond)) {
+ v = lb_expr_untyped_const_to_typed(p->module, cond, t_llvm_bool);
+ } else {
+ v = lb_build_expr(p, cond);
+ }
+
v = lb_emit_conv(p, v, t_llvm_bool);
lb_emit_if(p, v, true_block, false_block);
@@ -4872,6 +4891,9 @@ lbValue lb_emit_logical_binary_expr(lbProcedure *p, TokenKind op, Ast *left, Ast
if (done->preds.count == 0) {
lb_start_block(p, rhs);
+ if (lb_is_expr_untyped_const(right)) {
+ return lb_expr_untyped_const_to_typed(m, right, type);
+ }
return lb_build_expr(p, right);
}
@@ -4886,7 +4908,12 @@ lbValue lb_emit_logical_binary_expr(lbProcedure *p, TokenKind op, Ast *left, Ast
}
lb_start_block(p, rhs);
- lbValue edge = lb_build_expr(p, right);
+ lbValue edge = {};
+ if (lb_is_expr_untyped_const(right)) {
+ edge = lb_expr_untyped_const_to_typed(m, right, type);
+ } else {
+ edge = lb_build_expr(p, right);
+ }
incoming_values[done->preds.count] = edge.value;
incoming_blocks[done->preds.count] = p->curr_block->block;
@@ -7010,15 +7037,29 @@ lbValue lb_build_binary_expr(lbProcedure *p, Ast *expr) {
case Token_And:
case Token_Or:
case Token_Xor:
- case Token_AndNot:
- case Token_Shl:
- case Token_Shr: {
+ case Token_AndNot: {
Type *type = default_type(tv.type);
lbValue left = lb_build_expr(p, be->left);
lbValue right = lb_build_expr(p, be->right);
return lb_emit_arith(p, be->op.kind, left, right, type);
}
+ case Token_Shl:
+ case Token_Shr: {
+ lbValue left, right;
+ Type *type = default_type(tv.type);
+ left = lb_build_expr(p, be->left);
+
+ if (lb_is_expr_untyped_const(be->right)) {
+ // NOTE(bill): RHS shift operands can still be untyped
+ // Just bypass the standard lb_build_expr
+ right = lb_expr_untyped_const_to_typed(p->module, be->right, type);
+ } else {
+ right = lb_build_expr(p, be->right);
+ }
+ return lb_emit_arith(p, be->op.kind, left, right, type);
+ }
+
case Token_CmpEq:
case Token_NotEq:
case Token_Lt:
@@ -11385,8 +11426,13 @@ lbValue lb_build_expr(lbProcedure *p, Ast *expr) {
GB_ASSERT_MSG(tv.mode != Addressing_Invalid, "invalid expression '%s' (tv.mode = %d, tv.type = %s) @ %s\n Current Proc: %.*s : %s", expr_to_string(expr), tv.mode, type_to_string(tv.type), token_pos_to_string(expr_pos), LIT(p->name), type_to_string(p->type));
if (tv.value.kind != ExactValue_Invalid) {
+ // NOTE(bill): The commented out code below is just for debug purposes only
+ // GB_ASSERT_MSG(!is_type_untyped(tv.type), "%s @ %s\n%s", type_to_string(tv.type), token_pos_to_string(expr_pos), expr_to_string(expr));
+ // if (is_type_untyped(tv.type)) {
+ // gb_printf_err("%s %s\n", token_pos_to_string(expr_pos), expr_to_string(expr));
+ // }
+
// NOTE(bill): Short on constant values
- // GB_ASSERT_MSG(!is_type_untyped(tv.type), "%s @ %s", type_to_string(tv.type), token_pos_to_string(expr_pos));
return lb_const_value(p->module, tv.type, tv.value);
}