Skip to content

Commit 93727ea

Browse files
committed
is_unsigned
1 parent 0dcb84b commit 93727ea

File tree

5 files changed

+16
-20
lines changed

5 files changed

+16
-20
lines changed

src/_debug/dump_expr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ void dump_expr(FILE *fp, Expr *expr) {
9595
assert(expr != NULL);
9696
switch (expr->kind) {
9797
case EX_FIXNUM:
98-
if (expr->type->kind != TY_FIXNUM || expr->type->fixnum.is_unsigned)
98+
if (is_unsigned(expr->type))
9999
fprintf(fp, "%" PRIu64 "U", expr->fixnum);
100100
else
101101
fprintf(fp, "%" PRId64, expr->fixnum);

src/cc/backend/codegen_expr.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,8 @@ static struct CompareExpr gen_compare_expr(enum ExprKind kind, Expr *lhs, Expr *
6767
enum ConditionKind cond = kind + (COND_EQ - EX_EQ);
6868

6969
int flag = 0;
70-
if ((is_fixnum(lhs->type) && lhs->type->fixnum.is_unsigned) ||
71-
lhs->type->kind == TY_PTR) {
72-
// unsigned
70+
if (is_unsigned(lhs->type))
7371
flag = COND_UNSIGNED;
74-
}
7572

7673
VReg *lhs_reg = gen_expr(lhs);
7774
VReg *rhs_reg = gen_expr(rhs);

src/cc/frontend/expr.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ Expr *make_cast(Type *type, const Token *token, Expr *sub, bool is_explicit) {
109109
return sub;
110110
case EX_FIXNUM:
111111
if (type->kind == TY_FLONUM) {
112-
Flonum flonum = (sub->type->kind != TY_FIXNUM || sub->type->fixnum.is_unsigned)
112+
Flonum flonum = is_unsigned(sub->type)
113113
? (Flonum)(UFixnum)sub->fixnum
114114
: (Flonum)sub->fixnum;
115115
return new_expr_flolit(type, sub->token, flonum);
@@ -138,8 +138,7 @@ Expr *make_cast(Type *type, const Token *token, Expr *sub, bool is_explicit) {
138138
#if XCC_TARGET_ARCH == XCC_ARCH_X64 && !defined(__NO_FLONUM)
139139
// On x64, cannot cast from double to uint64_t directly.
140140
size_t dst_size = type_size(type);
141-
if (is_flonum(sub->type) &&
142-
is_fixnum(type) && type->fixnum.is_unsigned && dst_size >= 8) {
141+
if (is_flonum(sub->type) && is_unsigned(type) && dst_size >= 8) {
143142
// Transform from (uint64_t)flonum
144143
// to: (flonum <= INT64_MAX) ? (int64_t)flonum
145144
// : ((int64_t)(flonum - (INT64_MAX + 1UL)) ^ (1L << 63))
@@ -203,8 +202,8 @@ static bool cast_numbers(Expr **pLhs, Expr **pRhs, bool make_int) {
203202
*pLhs = promote_to_int(lhs);
204203
*pRhs = promote_to_int(rhs);
205204
} else if (changed || !same_type_without_qualifier(ltype, rtype, true)) {
206-
int l = (type_size(ltype) << 1) | (ltype->fixnum.is_unsigned ? 1 : 0);
207-
int r = (type_size(rtype) << 1) | (rtype->fixnum.is_unsigned ? 1 : 0);
205+
int l = (type_size(ltype) << 1) | ltype->fixnum.is_unsigned;
206+
int r = (type_size(rtype) << 1) | rtype->fixnum.is_unsigned;
208207
Type *type = l >= r ? ltype : rtype;
209208
*pLhs = make_cast(type, lhs->token, lhs, false);
210209
*pRhs = make_cast(type, rhs->token, rhs, false);

src/wcc/gen_wasm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ static void gen_compare_expr(enum ExprKind kind, Expr *lhs, Expr *rhs, bool need
3737
if (is_flonum(lhs->type)) {
3838
index = lhs->type->flonum.kind >= FL_DOUBLE ? 5 : 4;
3939
} else {
40-
index = (!is_fixnum(lhs->type) || lhs->type->fixnum.is_unsigned ? 2 : 0) +
41-
(type_size(lhs->type) > I32_SIZE ? 1 : 0);
40+
index = (is_unsigned(lhs->type) << 1) +
41+
(type_size(lhs->type) > I32_SIZE);
4242
}
4343

4444
static const unsigned char OpTable[][6] = {

src/wcc/gen_wasm_expr.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void gen_load(const Type *type) {
3434
case TY_FIXNUM:
3535
case TY_PTR:
3636
{
37-
bool u = !is_fixnum(type) || type->fixnum.is_unsigned;
37+
bool u = is_unsigned(type);
3838
switch (type_size(type)) {
3939
case 1: ADD_CODE(u ? OP_I32_LOAD8_U : OP_I32_LOAD8_S, 0, 0); break;
4040
case 2: ADD_CODE(u ? OP_I32_LOAD16_U : OP_I32_LOAD16_S, 1, 0); break;
@@ -79,7 +79,7 @@ void gen_store(const Type *type) {
7979
static void gen_arith(enum ExprKind kind, const Type *type) {
8080
assert(is_number(type) || ptr_or_array(type));
8181
int index = 0;
82-
bool is_unsigned = is_fixnum(type) && type->fixnum.is_unsigned;
82+
bool u = is_unsigned(type);
8383
if (is_flonum(type)) {
8484
assert(kind < EX_MOD);
8585
index = type->flonum.kind >= FL_DOUBLE ? 3 : 2;
@@ -103,17 +103,17 @@ static void gen_arith(enum ExprKind kind, const Type *type) {
103103
};
104104

105105
assert(EX_ADD <= kind && kind <= EX_RSHIFT);
106-
assert(kOpTable[is_unsigned][index][kind - EX_ADD] != OP_NOP);
107-
ADD_CODE(kOpTable[is_unsigned][index][kind - EX_ADD]);
106+
assert(kOpTable[u][index][kind - EX_ADD] != OP_NOP);
107+
ADD_CODE(kOpTable[u][index][kind - EX_ADD]);
108108
}
109109

110110
static void gen_cast_to_i(const Type *dst, Type *src) {
111111
switch (src->kind) {
112112
case TY_FIXNUM: case TY_PTR: case TY_FUNC:
113113
{
114114
size_t d = type_size(dst), s = type_size(src);
115-
bool du = dst->kind != TY_FIXNUM || dst->fixnum.is_unsigned;
116-
bool su = src->kind != TY_FIXNUM || src->fixnum.is_unsigned;
115+
bool du = dst->kind == TY_FUNC || is_unsigned(dst);
116+
bool su = src->kind == TY_FUNC || is_unsigned(src);
117117
enum { I64TO32 = 1, I32TO64 = 2 };
118118
switch ((d > I32_SIZE ? 2 : 0) + (s > I32_SIZE ? 1 : 0)) {
119119
case I64TO32: ADD_CODE(OP_I32_WRAP_I64); break;
@@ -153,7 +153,7 @@ static void gen_cast_to_i(const Type *dst, Type *src) {
153153
};
154154
int d = type_size(dst);
155155
int index = (d > I32_SIZE ? 2 : 0) + (src->flonum.kind >= FL_DOUBLE ? 1 : 0);
156-
bool du = !is_fixnum(dst) || dst->fixnum.is_unsigned;
156+
bool du = is_unsigned(dst);
157157
ADD_CODE(OpTable[du][index]);
158158
}
159159
break;
@@ -171,7 +171,7 @@ static void gen_cast_to_f(const Type *dst, Type *src) {
171171
};
172172
int s = type_size(src);
173173
int index = (dst->flonum.kind >= FL_DOUBLE ? 2 : 0) + (s > I32_SIZE ? 1 : 0);
174-
bool su = !is_fixnum(src) || src->fixnum.is_unsigned;
174+
bool su = is_unsigned(src);
175175
ADD_CODE(OpTable[su][index]);
176176
}
177177
break;

0 commit comments

Comments
 (0)