chibicc/type.c

308 lines
7.3 KiB
C
Raw Permalink Normal View History

2020-09-04 07:39:06 +03:00
#include "chibicc.h"
2019-08-11 04:18:55 +03:00
Type *ty_void = &(Type){TY_VOID, 1, 1};
Type *ty_bool = &(Type){TY_BOOL, 1, 1};
2019-08-11 04:18:55 +03:00
2020-08-30 11:21:54 +03:00
Type *ty_char = &(Type){TY_CHAR, 1, 1};
2020-09-06 02:10:01 +03:00
Type *ty_short = &(Type){TY_SHORT, 2, 2};
2020-09-06 02:09:09 +03:00
Type *ty_int = &(Type){TY_INT, 4, 4};
2020-08-27 15:51:00 +03:00
Type *ty_long = &(Type){TY_LONG, 8, 8};
2020-08-30 11:21:54 +03:00
2020-08-28 17:29:49 +03:00
Type *ty_uchar = &(Type){TY_CHAR, 1, 1, true};
Type *ty_ushort = &(Type){TY_SHORT, 2, 2, true};
Type *ty_uint = &(Type){TY_INT, 4, 4, true};
Type *ty_ulong = &(Type){TY_LONG, 8, 8, true};
2020-09-27 13:43:03 +03:00
Type *ty_float = &(Type){TY_FLOAT, 4, 4};
Type *ty_double = &(Type){TY_DOUBLE, 8, 8};
2020-08-29 04:37:53 +03:00
Type *ty_ldouble = &(Type){TY_LDOUBLE, 16, 16};
2020-09-27 13:43:03 +03:00
2020-08-30 11:21:54 +03:00
static Type *new_type(TypeKind kind, int size, int align) {
Type *ty = calloc(1, sizeof(Type));
ty->kind = kind;
ty->size = size;
ty->align = align;
return ty;
}
2020-09-04 07:39:06 +03:00
bool is_integer(Type *ty) {
2020-08-27 15:51:00 +03:00
TypeKind k = ty->kind;
return k == TY_BOOL || k == TY_CHAR || k == TY_SHORT ||
2019-08-11 13:59:27 +03:00
k == TY_INT || k == TY_LONG || k == TY_ENUM;
2020-09-04 07:39:06 +03:00
}
2020-09-27 13:43:03 +03:00
bool is_flonum(Type *ty) {
2020-08-29 04:37:53 +03:00
return ty->kind == TY_FLOAT || ty->kind == TY_DOUBLE ||
ty->kind == TY_LDOUBLE;
2020-09-27 13:43:03 +03:00
}
2020-09-22 12:29:17 +03:00
bool is_numeric(Type *ty) {
return is_integer(ty) || is_flonum(ty);
}
2020-09-06 15:06:56 +03:00
bool is_compatible(Type *t1, Type *t2) {
if (t1 == t2)
return true;
if (t1->origin)
return is_compatible(t1->origin, t2);
if (t2->origin)
return is_compatible(t1, t2->origin);
if (t1->kind != t2->kind)
return false;
switch (t1->kind) {
case TY_CHAR:
case TY_SHORT:
case TY_INT:
case TY_LONG:
return t1->is_unsigned == t2->is_unsigned;
case TY_FLOAT:
case TY_DOUBLE:
2020-08-29 04:37:53 +03:00
case TY_LDOUBLE:
2020-09-06 15:06:56 +03:00
return true;
case TY_PTR:
return is_compatible(t1->base, t2->base);
case TY_FUNC: {
if (!is_compatible(t1->return_ty, t2->return_ty))
return false;
if (t1->is_variadic != t2->is_variadic)
return false;
Type *p1 = t1->params;
Type *p2 = t2->params;
for (; p1 && p2; p1 = p1->next, p2 = p2->next)
if (!is_compatible(p1, p2))
return false;
return p1 == NULL && p2 == NULL;
}
case TY_ARRAY:
if (!is_compatible(t1->base, t2->base))
return false;
return t1->array_len < 0 && t2->array_len < 0 &&
t1->array_len == t2->array_len;
}
return false;
}
Type *copy_type(Type *ty) {
Type *ret = calloc(1, sizeof(Type));
*ret = *ty;
2020-09-06 15:06:56 +03:00
ret->origin = ty;
return ret;
}
2020-09-04 07:39:06 +03:00
Type *pointer_to(Type *base) {
2020-08-30 11:21:54 +03:00
Type *ty = new_type(TY_PTR, 8, 8);
2020-09-04 07:39:06 +03:00
ty->base = base;
ty->is_unsigned = true;
2020-09-04 07:39:06 +03:00
return ty;
}
2020-09-04 13:01:33 +03:00
Type *func_type(Type *return_ty) {
2020-07-24 15:43:48 +03:00
// The C spec disallows sizeof(<function type>), but
// GCC allows that and the expression is evaluated to 1.
Type *ty = new_type(TY_FUNC, 1, 1);
2020-09-04 13:01:33 +03:00
ty->return_ty = return_ty;
return ty;
}
2020-09-26 04:15:32 +03:00
Type *array_of(Type *base, int len) {
2020-08-30 11:21:54 +03:00
Type *ty = new_type(TY_ARRAY, base->size * len, base->align);
2020-09-26 04:15:32 +03:00
ty->base = base;
ty->array_len = len;
return ty;
}
2020-09-04 08:44:12 +03:00
Type *vla_of(Type *base, Node *len) {
Type *ty = new_type(TY_VLA, 8, 8);
ty->base = base;
ty->vla_len = len;
return ty;
}
2019-08-11 13:59:27 +03:00
Type *enum_type(void) {
return new_type(TY_ENUM, 4, 4);
}
Type *struct_type(void) {
return new_type(TY_STRUCT, 0, 1);
}
2020-09-26 04:24:45 +03:00
static Type *get_common_type(Type *ty1, Type *ty2) {
if (ty1->base)
return pointer_to(ty1->base);
2020-08-28 17:29:49 +03:00
if (ty1->kind == TY_FUNC)
return pointer_to(ty1);
if (ty2->kind == TY_FUNC)
return pointer_to(ty2);
2020-08-29 04:37:53 +03:00
if (ty1->kind == TY_LDOUBLE || ty2->kind == TY_LDOUBLE)
return ty_ldouble;
2020-09-22 12:28:37 +03:00
if (ty1->kind == TY_DOUBLE || ty2->kind == TY_DOUBLE)
return ty_double;
if (ty1->kind == TY_FLOAT || ty2->kind == TY_FLOAT)
return ty_float;
2020-08-28 17:29:49 +03:00
if (ty1->size < 4)
ty1 = ty_int;
if (ty2->size < 4)
ty2 = ty_int;
if (ty1->size != ty2->size)
return (ty1->size < ty2->size) ? ty2 : ty1;
if (ty2->is_unsigned)
return ty2;
return ty1;
2020-09-26 04:24:45 +03:00
}
// For many binary operators, we implicitly promote operands so that
// both operands have the same type. Any integral type smaller than
// int is always promoted to int. If the type of one operand is larger
// than the other's (e.g. "long" vs. "int"), the smaller operand will
// be promoted to match with the other.
//
// This operation is called the "usual arithmetic conversion".
static void usual_arith_conv(Node **lhs, Node **rhs) {
Type *ty = get_common_type((*lhs)->ty, (*rhs)->ty);
*lhs = new_cast(*lhs, ty);
*rhs = new_cast(*rhs, ty);
}
2020-09-04 07:39:06 +03:00
void add_type(Node *node) {
if (!node || node->ty)
return;
add_type(node->lhs);
add_type(node->rhs);
add_type(node->cond);
add_type(node->then);
add_type(node->els);
add_type(node->init);
add_type(node->inc);
for (Node *n = node->body; n; n = n->next)
add_type(n);
for (Node *n = node->args; n; n = n->next)
add_type(n);
2020-09-04 07:39:06 +03:00
switch (node->kind) {
2020-09-26 04:24:45 +03:00
case ND_NUM:
2019-09-21 10:17:40 +03:00
node->ty = ty_int;
2020-09-26 04:24:45 +03:00
return;
2020-09-04 07:39:06 +03:00
case ND_ADD:
case ND_SUB:
case ND_MUL:
case ND_DIV:
2020-10-07 14:18:57 +03:00
case ND_MOD:
2020-10-07 14:19:35 +03:00
case ND_BITAND:
case ND_BITOR:
case ND_BITXOR:
2020-09-26 04:24:45 +03:00
usual_arith_conv(&node->lhs, &node->rhs);
2020-09-26 04:15:32 +03:00
node->ty = node->lhs->ty;
return;
2020-09-26 04:24:45 +03:00
case ND_NEG: {
Type *ty = get_common_type(ty_int, node->lhs->ty);
node->lhs = new_cast(node->lhs, ty);
node->ty = ty;
return;
}
2020-09-04 07:39:06 +03:00
case ND_ASSIGN:
2020-09-26 04:15:32 +03:00
if (node->lhs->ty->kind == TY_ARRAY)
error_tok(node->lhs->tok, "not an lvalue");
2020-09-26 04:24:45 +03:00
if (node->lhs->ty->kind != TY_STRUCT)
node->rhs = new_cast(node->rhs, node->lhs->ty);
2020-09-04 07:39:06 +03:00
node->ty = node->lhs->ty;
return;
case ND_EQ:
case ND_NE:
case ND_LT:
case ND_LE:
2020-09-26 04:24:45 +03:00
usual_arith_conv(&node->lhs, &node->rhs);
node->ty = ty_int;
return;
2019-08-04 12:25:20 +03:00
case ND_FUNCALL:
2020-09-04 08:44:12 +03:00
node->ty = node->func_ty->return_ty;
2020-09-04 07:39:06 +03:00
return;
2019-08-13 13:31:04 +03:00
case ND_NOT:
2020-10-07 14:22:11 +03:00
case ND_LOGOR:
case ND_LOGAND:
2019-08-13 13:31:04 +03:00
node->ty = ty_int;
return;
2019-08-13 13:41:11 +03:00
case ND_BITNOT:
2020-10-07 14:23:22 +03:00
case ND_SHL:
case ND_SHR:
2019-08-13 13:41:11 +03:00
node->ty = node->lhs->ty;
return;
case ND_VAR:
2020-09-03 16:27:13 +03:00
case ND_VLA_PTR:
node->ty = node->var->ty;
return;
2019-08-17 04:27:35 +03:00
case ND_COND:
if (node->then->ty->kind == TY_VOID || node->els->ty->kind == TY_VOID) {
node->ty = ty_void;
} else {
usual_arith_conv(&node->then, &node->els);
node->ty = node->then->ty;
}
return;
case ND_COMMA:
node->ty = node->rhs->ty;
return;
2019-08-08 16:43:58 +03:00
case ND_MEMBER:
node->ty = node->member->ty;
return;
2020-09-03 12:50:56 +03:00
case ND_ADDR: {
Type *ty = node->lhs->ty;
if (ty->kind == TY_ARRAY)
node->ty = pointer_to(ty->base);
2020-09-26 04:15:32 +03:00
else
2020-09-03 12:50:56 +03:00
node->ty = pointer_to(ty);
2020-09-04 07:39:06 +03:00
return;
2020-09-03 12:50:56 +03:00
}
2020-09-04 07:39:06 +03:00
case ND_DEREF:
2020-09-26 04:15:32 +03:00
if (!node->lhs->ty->base)
error_tok(node->tok, "invalid pointer dereference");
2019-08-11 04:18:55 +03:00
if (node->lhs->ty->base->kind == TY_VOID)
error_tok(node->tok, "dereferencing a void pointer");
node->ty = node->lhs->ty->base;
2020-09-04 07:39:06 +03:00
return;
case ND_STMT_EXPR:
if (node->body) {
Node *stmt = node->body;
while (stmt->next)
stmt = stmt->next;
if (stmt->kind == ND_EXPR_STMT) {
node->ty = stmt->lhs->ty;
return;
}
}
error_tok(node->tok, "statement expression returning void is not supported");
return;
2020-09-04 06:26:27 +03:00
case ND_LABEL_VAL:
node->ty = pointer_to(ty_void);
return;
2020-09-15 05:30:56 +03:00
case ND_CAS:
add_type(node->cas_addr);
add_type(node->cas_old);
add_type(node->cas_new);
node->ty = ty_bool;
if (node->cas_addr->ty->kind != TY_PTR)
error_tok(node->cas_addr->tok, "pointer expected");
if (node->cas_old->ty->kind != TY_PTR)
error_tok(node->cas_old->tok, "pointer expected");
return;
2020-09-15 05:30:56 +03:00
case ND_EXCH:
if (node->lhs->ty->kind != TY_PTR)
error_tok(node->cas_addr->tok, "pointer expected");
node->ty = node->lhs->ty->base;
return;
2020-09-04 07:39:06 +03:00
}
}