From 8b46fab07665dceecc0fd075eefa48f5af3f7285 Mon Sep 17 00:00:00 2001 From: lufia Date: Sat, 26 Jul 2025 00:11:02 +0900 Subject: [PATCH] sys/src/cmd/cc: fix special case for null pointer constants in cond expressions ref https://github.com/9front/9front/commit/3df95385bcc5294a212534d0991f1ffef1454aca --- sys/src/cmd/cc/cc.h | 1 + sys/src/cmd/cc/com.c | 6 ++++-- sys/src/cmd/cc/sub.c | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/sys/src/cmd/cc/cc.h b/sys/src/cmd/cc/cc.h index 8445a868..a7d5c59d 100644 --- a/sys/src/cmd/cc/cc.h +++ b/sys/src/cmd/cc/cc.h @@ -694,6 +694,7 @@ Type* copytyp(Type*); void typeext(Type*, Node*); void typeext1(Type*, Node*); int side(Node*); +int zpconst(Node*); int vconst(Node*); int log2(uvlong); int vlog(Node*); diff --git a/sys/src/cmd/cc/com.c b/sys/src/cmd/cc/com.c index fab1ba63..996d96c9 100644 --- a/sys/src/cmd/cc/com.c +++ b/sys/src/cmd/cc/com.c @@ -331,11 +331,13 @@ tcomo(Node *n, int f) o |= tcom(r->left); if(o | tcom(r->right)) goto bad; - if(r->right->type->etype == TIND && vconst(r->left) == 0) { + if(r->right->type->etype == TIND && zpconst(r->left)) { + r->type = r->right->type; r->left->type = r->right->type; r->left->vconst = 0; } - if(r->left->type->etype == TIND && vconst(r->right) == 0) { + if(r->left->type->etype == TIND && zpconst(r->right)) { + r->type = r->left->type; r->right->type = r->left->type; r->right->vconst = 0; } diff --git a/sys/src/cmd/cc/sub.c b/sys/src/cmd/cc/sub.c index e5a5cf44..5190c17e 100644 --- a/sys/src/cmd/cc/sub.c +++ b/sys/src/cmd/cc/sub.c @@ -987,6 +987,21 @@ side(Node *n) return 1; } +int +zpconst(Node *n) +{ + while(n->op == OCAST){ + if(n->type == T) + break; + if(n->type->etype != TIND) + break; + if(n->type->link->etype != TVOID) + break; + n = n->left; + } + return vconst(n) == 0; +} + int vconst(Node *n) {