From 6789984bd1c41c417fea62750559727de81915bd Mon Sep 17 00:00:00 2001 From: Mid <> Date: Mon, 25 Nov 2024 18:35:11 +0200 Subject: [PATCH] Codegen casts --- src/cg.c | 2 +- src/parse.c | 10 ++++++++-- tests/functions.nct | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/cg.c b/src/cg.c index 246a320..8249236 100644 --- a/src/cg.c +++ b/src/cg.c @@ -111,7 +111,7 @@ static const char *xop_sz(AST *e, int sz) { if(v->kind == VARTABLEENTRY_VAR) { return xv_sz(v, sz); } else if(v->kind == VARTABLEENTRY_SYMBOL) { - snprintf(ret, XOPBUFSZ, "[%s]", v->data.symbol.name); + snprintf(ret, XOPBUFSZ, "%s [%s]", spec(sz), v->data.symbol.name); } else abort(); } else if(e->nodeKind == AST_EXPR_PRIMITIVE) { snprintf(ret, XOPBUFSZ, "%i", e->exprPrim.val); diff --git a/src/parse.c b/src/parse.c index dbf5159..3426c9c 100644 --- a/src/parse.c +++ b/src/parse.c @@ -213,11 +213,17 @@ AST *nct_cast_expr(AST *what, Type *to) { return NULL; } - if(what->nodeKind == AST_EXPR_PRIMITIVE && to->type == TYPE_TYPE_PRIMITIVE) { + if(what->nodeKind == AST_EXPR_PRIMITIVE && (to->type == TYPE_TYPE_PRIMITIVE || to->type == TYPE_TYPE_POINTER)) { ASTExprPrimitive *ret = malloc(sizeof(*ret)); ret->nodeKind = AST_EXPR_PRIMITIVE; ret->type = to; - ret->val = what->exprPrim.val & (((int64_t) 1 << to->primitive.width) - 1); + + if(to->type == TYPE_TYPE_PRIMITIVE) { + ret->val = what->exprPrim.val & ((1UL << to->primitive.width) - 1); + } else { + ret->val = what->exprPrim.val & ((1UL << (8 * type_size(to))) - 1); + } + return (AST*) ret; } else { ASTExprCast *ret = malloc(sizeof(*ret)); diff --git a/tests/functions.nct b/tests/functions.nct index 4ac7695..5e4df92 100644 --- a/tests/functions.nct +++ b/tests/functions.nct @@ -2,7 +2,7 @@ extern s32() getchar; extern u0(s32) putchar; loop { - u8 z = 5; + s32 z = 5; s32 a = getchar(); if(a == -1) { break;