2020-09-04 07:39:06 +03:00
|
|
|
#include "chibicc.h"
|
|
|
|
|
2020-08-27 15:04:17 +03:00
|
|
|
Type *ty_char = &(Type){TY_CHAR, 1};
|
2020-09-26 04:15:32 +03:00
|
|
|
Type *ty_int = &(Type){TY_INT, 8};
|
2020-09-04 07:39:06 +03:00
|
|
|
|
|
|
|
bool is_integer(Type *ty) {
|
2020-08-27 15:04:17 +03:00
|
|
|
return ty->kind == TY_CHAR || ty->kind == TY_INT;
|
2020-09-04 07:39:06 +03:00
|
|
|
}
|
|
|
|
|
2020-09-04 07:39:48 +03:00
|
|
|
Type *copy_type(Type *ty) {
|
|
|
|
Type *ret = calloc(1, sizeof(Type));
|
|
|
|
*ret = *ty;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-09-04 07:39:06 +03:00
|
|
|
Type *pointer_to(Type *base) {
|
|
|
|
Type *ty = calloc(1, sizeof(Type));
|
|
|
|
ty->kind = TY_PTR;
|
2020-09-26 04:15:32 +03:00
|
|
|
ty->size = 8;
|
2020-09-04 07:39:06 +03:00
|
|
|
ty->base = base;
|
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
|
2020-09-04 13:01:33 +03:00
|
|
|
Type *func_type(Type *return_ty) {
|
|
|
|
Type *ty = calloc(1, sizeof(Type));
|
|
|
|
ty->kind = TY_FUNC;
|
|
|
|
ty->return_ty = return_ty;
|
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
|
2020-09-26 04:15:32 +03:00
|
|
|
Type *array_of(Type *base, int len) {
|
|
|
|
Type *ty = calloc(1, sizeof(Type));
|
|
|
|
ty->kind = TY_ARRAY;
|
|
|
|
ty->size = base->size * len;
|
|
|
|
ty->base = base;
|
|
|
|
ty->array_len = len;
|
|
|
|
return 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);
|
2020-09-04 07:39:48 +03:00
|
|
|
for (Node *n = node->args; n; n = n->next)
|
|
|
|
add_type(n);
|
2020-09-04 07:39:06 +03:00
|
|
|
|
|
|
|
switch (node->kind) {
|
|
|
|
case ND_ADD:
|
|
|
|
case ND_SUB:
|
|
|
|
case ND_MUL:
|
|
|
|
case ND_DIV:
|
|
|
|
case ND_NEG:
|
2020-09-26 04:15:32 +03:00
|
|
|
node->ty = node->lhs->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-04 07:39:06 +03:00
|
|
|
node->ty = node->lhs->ty;
|
|
|
|
return;
|
|
|
|
case ND_EQ:
|
|
|
|
case ND_NE:
|
|
|
|
case ND_LT:
|
|
|
|
case ND_LE:
|
|
|
|
case ND_NUM:
|
2019-08-04 12:25:20 +03:00
|
|
|
case ND_FUNCALL:
|
2020-09-04 07:39:06 +03:00
|
|
|
node->ty = ty_int;
|
|
|
|
return;
|
2020-09-03 09:43:36 +03:00
|
|
|
case ND_VAR:
|
|
|
|
node->ty = node->var->ty;
|
|
|
|
return;
|
2020-09-04 07:39:06 +03:00
|
|
|
case ND_ADDR:
|
2020-09-26 04:15:32 +03:00
|
|
|
if (node->lhs->ty->kind == TY_ARRAY)
|
|
|
|
node->ty = pointer_to(node->lhs->ty->base);
|
|
|
|
else
|
|
|
|
node->ty = pointer_to(node->lhs->ty);
|
2020-09-04 07:39:06 +03:00
|
|
|
return;
|
|
|
|
case ND_DEREF:
|
2020-09-26 04:15:32 +03:00
|
|
|
if (!node->lhs->ty->base)
|
2020-09-03 09:43:36 +03:00
|
|
|
error_tok(node->tok, "invalid pointer dereference");
|
|
|
|
node->ty = node->lhs->ty->base;
|
2020-09-04 07:39:06 +03:00
|
|
|
return;
|
2019-08-07 02:05:18 +03:00
|
|
|
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 07:39:06 +03:00
|
|
|
}
|
|
|
|
}
|