2019-08-05 15:12:44 +03:00
|
|
|
// This file contains a recursive descent parser for C.
|
|
|
|
//
|
|
|
|
// Most functions in this file are named after the symbols they are
|
|
|
|
// supposed to read from an input token list. For example, stmt() is
|
|
|
|
// responsible for reading a statement from a token list. The function
|
|
|
|
// then construct an AST node representing a statement.
|
|
|
|
//
|
|
|
|
// Each function conceptually returns two values, an AST node and
|
|
|
|
// remaining part of the input tokens. Since C doesn't support
|
|
|
|
// multiple return values, the remaining tokens are returned to the
|
|
|
|
// caller via a pointer argument.
|
|
|
|
//
|
|
|
|
// Input tokens are represented by a linked list. Unlike many recursive
|
|
|
|
// descent parsers, we don't have the notion of the "input token stream".
|
|
|
|
// Most parsing functions don't change the global state of the parser.
|
|
|
|
// So it is very easy to lookahead arbitrary number of tokens in this
|
|
|
|
// parser.
|
|
|
|
|
2020-10-07 14:11:16 +03:00
|
|
|
#include "chibicc.h"
|
|
|
|
|
2019-08-11 13:59:27 +03:00
|
|
|
// Scope for local variables, global variables, typedefs
|
|
|
|
// or enum constants
|
2020-09-04 12:00:26 +03:00
|
|
|
typedef struct VarScope VarScope;
|
|
|
|
struct VarScope {
|
|
|
|
VarScope *next;
|
|
|
|
char *name;
|
|
|
|
Obj *var;
|
2020-09-04 11:23:26 +03:00
|
|
|
Type *type_def;
|
2019-08-11 13:59:27 +03:00
|
|
|
Type *enum_ty;
|
|
|
|
int enum_val;
|
2020-09-04 12:00:26 +03:00
|
|
|
};
|
|
|
|
|
2019-08-11 13:59:27 +03:00
|
|
|
// Scope for struct, union or enum tags
|
2020-10-07 04:43:02 +03:00
|
|
|
typedef struct TagScope TagScope;
|
|
|
|
struct TagScope {
|
|
|
|
TagScope *next;
|
|
|
|
char *name;
|
|
|
|
Type *ty;
|
|
|
|
};
|
|
|
|
|
2020-09-04 12:00:26 +03:00
|
|
|
// Represents a block scope.
|
|
|
|
typedef struct Scope Scope;
|
|
|
|
struct Scope {
|
|
|
|
Scope *next;
|
2020-10-07 04:43:02 +03:00
|
|
|
|
2019-08-11 13:59:27 +03:00
|
|
|
// C has two block scopes; one is for variables/typedefs and
|
|
|
|
// the other is for struct/union/enum tags.
|
2020-09-04 12:00:26 +03:00
|
|
|
VarScope *vars;
|
2020-10-07 04:43:02 +03:00
|
|
|
TagScope *tags;
|
2020-09-04 12:00:26 +03:00
|
|
|
};
|
|
|
|
|
2020-09-04 11:23:26 +03:00
|
|
|
// Variable attributes such as typedef or extern.
|
|
|
|
typedef struct {
|
|
|
|
bool is_typedef;
|
2020-09-04 11:25:15 +03:00
|
|
|
bool is_static;
|
2020-09-04 11:23:26 +03:00
|
|
|
} VarAttr;
|
|
|
|
|
2020-10-07 14:12:19 +03:00
|
|
|
// All local variable instances created during parsing are
|
|
|
|
// accumulated to this list.
|
2020-09-04 11:58:53 +03:00
|
|
|
static Obj *locals;
|
2020-10-07 04:43:02 +03:00
|
|
|
|
|
|
|
// Likewise, global variables are accumulated to this list.
|
2020-09-04 11:58:53 +03:00
|
|
|
static Obj *globals;
|
2020-10-07 14:12:19 +03:00
|
|
|
|
2020-09-04 12:00:26 +03:00
|
|
|
static Scope *scope = &(Scope){};
|
|
|
|
|
2020-04-18 03:27:47 +03:00
|
|
|
// Points to the function object the parser is currently parsing.
|
|
|
|
static Obj *current_fn;
|
|
|
|
|
2020-09-04 06:27:21 +03:00
|
|
|
// Lists of all goto statements and labels in the curent function.
|
|
|
|
static Node *gotos;
|
|
|
|
static Node *labels;
|
|
|
|
|
2019-08-11 08:12:01 +03:00
|
|
|
static bool is_typename(Token *tok);
|
2020-09-04 11:23:26 +03:00
|
|
|
static Type *declspec(Token **rest, Token *tok, VarAttr *attr);
|
2019-08-11 13:59:27 +03:00
|
|
|
static Type *enum_specifier(Token **rest, Token *tok);
|
2019-08-13 15:03:20 +03:00
|
|
|
static Type *type_suffix(Token **rest, Token *tok, Type *ty);
|
2020-09-04 13:01:33 +03:00
|
|
|
static Type *declarator(Token **rest, Token *tok, Type *ty);
|
2020-09-04 11:23:26 +03:00
|
|
|
static Node *declaration(Token **rest, Token *tok, Type *basety);
|
2020-09-04 07:38:41 +03:00
|
|
|
static Node *compound_stmt(Token **rest, Token *tok);
|
2020-09-04 07:39:06 +03:00
|
|
|
static Node *stmt(Token **rest, Token *tok);
|
2020-09-26 02:50:44 +03:00
|
|
|
static Node *expr_stmt(Token **rest, Token *tok);
|
2020-09-26 05:23:04 +03:00
|
|
|
static Node *expr(Token **rest, Token *tok);
|
2020-09-26 02:59:56 +03:00
|
|
|
static Node *assign(Token **rest, Token *tok);
|
2020-10-07 14:22:11 +03:00
|
|
|
static Node *logor(Token **rest, Token *tok);
|
|
|
|
static Node *logand(Token **rest, Token *tok);
|
2020-10-07 14:19:35 +03:00
|
|
|
static Node *bitor(Token **rest, Token *tok);
|
|
|
|
static Node *bitxor(Token **rest, Token *tok);
|
|
|
|
static Node *bitand(Token **rest, Token *tok);
|
2020-10-07 14:11:16 +03:00
|
|
|
static Node *equality(Token **rest, Token *tok);
|
|
|
|
static Node *relational(Token **rest, Token *tok);
|
|
|
|
static Node *add(Token **rest, Token *tok);
|
2020-10-07 14:17:30 +03:00
|
|
|
static Node *new_add(Node *lhs, Node *rhs, Token *tok);
|
|
|
|
static Node *new_sub(Node *lhs, Node *rhs, Token *tok);
|
2020-10-07 14:11:16 +03:00
|
|
|
static Node *mul(Token **rest, Token *tok);
|
2019-08-11 10:06:14 +03:00
|
|
|
static Node *cast(Token **rest, Token *tok);
|
2019-08-08 16:43:58 +03:00
|
|
|
static Type *struct_decl(Token **rest, Token *tok);
|
2020-04-02 15:54:47 +03:00
|
|
|
static Type *union_decl(Token **rest, Token *tok);
|
2019-08-06 12:14:53 +03:00
|
|
|
static Node *postfix(Token **rest, Token *tok);
|
2020-10-07 14:11:16 +03:00
|
|
|
static Node *unary(Token **rest, Token *tok);
|
|
|
|
static Node *primary(Token **rest, Token *tok);
|
2020-09-04 11:23:26 +03:00
|
|
|
static Token *parse_typedef(Token *tok, Type *basety);
|
2020-10-07 14:11:16 +03:00
|
|
|
|
2020-09-04 12:00:26 +03:00
|
|
|
static void enter_scope(void) {
|
|
|
|
Scope *sc = calloc(1, sizeof(Scope));
|
|
|
|
sc->next = scope;
|
|
|
|
scope = sc;
|
|
|
|
}
|
2020-09-05 02:43:21 +03:00
|
|
|
|
2020-09-04 12:00:26 +03:00
|
|
|
static void leave_scope(void) {
|
|
|
|
scope = scope->next;
|
|
|
|
}
|
2020-09-05 02:43:21 +03:00
|
|
|
|
2020-09-04 12:00:26 +03:00
|
|
|
// Find a variable by name.
|
2020-09-04 11:23:26 +03:00
|
|
|
static VarScope *find_var(Token *tok) {
|
2020-09-04 12:00:26 +03:00
|
|
|
for (Scope *sc = scope; sc; sc = sc->next)
|
|
|
|
for (VarScope *sc2 = sc->vars; sc2; sc2 = sc2->next)
|
|
|
|
if (equal(tok, sc2->name))
|
2020-09-04 11:23:26 +03:00
|
|
|
return sc2;
|
2020-10-07 14:12:19 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-10-07 04:43:02 +03:00
|
|
|
static Type *find_tag(Token *tok) {
|
|
|
|
for (Scope *sc = scope; sc; sc = sc->next)
|
|
|
|
for (TagScope *sc2 = sc->tags; sc2; sc2 = sc2->next)
|
|
|
|
if (equal(tok, sc2->name))
|
|
|
|
return sc2->ty;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-09-26 05:23:04 +03:00
|
|
|
static Node *new_node(NodeKind kind, Token *tok) {
|
2020-10-07 14:11:16 +03:00
|
|
|
Node *node = calloc(1, sizeof(Node));
|
|
|
|
node->kind = kind;
|
2020-09-26 05:23:04 +03:00
|
|
|
node->tok = tok;
|
2020-10-07 14:11:16 +03:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-09-26 05:23:04 +03:00
|
|
|
static Node *new_binary(NodeKind kind, Node *lhs, Node *rhs, Token *tok) {
|
|
|
|
Node *node = new_node(kind, tok);
|
2020-10-07 14:11:16 +03:00
|
|
|
node->lhs = lhs;
|
|
|
|
node->rhs = rhs;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-09-26 05:23:04 +03:00
|
|
|
static Node *new_unary(NodeKind kind, Node *expr, Token *tok) {
|
|
|
|
Node *node = new_node(kind, tok);
|
2020-10-07 14:11:16 +03:00
|
|
|
node->lhs = expr;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-08-27 15:51:00 +03:00
|
|
|
static Node *new_num(int64_t val, Token *tok) {
|
2020-09-26 05:23:04 +03:00
|
|
|
Node *node = new_node(ND_NUM, tok);
|
2020-10-07 14:11:16 +03:00
|
|
|
node->val = val;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-09-26 04:24:45 +03:00
|
|
|
static Node *new_long(int64_t val, Token *tok) {
|
|
|
|
Node *node = new_node(ND_NUM, tok);
|
|
|
|
node->val = val;
|
|
|
|
node->ty = ty_long;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-09-26 05:23:04 +03:00
|
|
|
static Node *new_var_node(Obj *var, Token *tok) {
|
|
|
|
Node *node = new_node(ND_VAR, tok);
|
2020-10-07 14:12:19 +03:00
|
|
|
node->var = var;
|
2020-09-26 02:59:56 +03:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-09-26 04:24:45 +03:00
|
|
|
Node *new_cast(Node *expr, Type *ty) {
|
2019-08-11 10:06:14 +03:00
|
|
|
add_type(expr);
|
|
|
|
|
|
|
|
Node *node = calloc(1, sizeof(Node));
|
|
|
|
node->kind = ND_CAST;
|
|
|
|
node->tok = expr->tok;
|
|
|
|
node->lhs = expr;
|
|
|
|
node->ty = copy_type(ty);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-09-04 11:23:26 +03:00
|
|
|
static VarScope *push_scope(char *name) {
|
2020-09-04 12:00:26 +03:00
|
|
|
VarScope *sc = calloc(1, sizeof(VarScope));
|
|
|
|
sc->name = name;
|
|
|
|
sc->next = scope->vars;
|
|
|
|
scope->vars = sc;
|
|
|
|
return sc;
|
|
|
|
}
|
|
|
|
|
2020-09-04 11:58:53 +03:00
|
|
|
static Obj *new_var(char *name, Type *ty) {
|
2020-10-07 14:12:19 +03:00
|
|
|
Obj *var = calloc(1, sizeof(Obj));
|
|
|
|
var->name = name;
|
2020-09-03 09:43:36 +03:00
|
|
|
var->ty = ty;
|
2020-09-04 11:23:26 +03:00
|
|
|
push_scope(name)->var = var;
|
2020-09-04 11:58:53 +03:00
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Obj *new_lvar(char *name, Type *ty) {
|
|
|
|
Obj *var = new_var(name, ty);
|
|
|
|
var->is_local = true;
|
2020-10-07 14:12:19 +03:00
|
|
|
var->next = locals;
|
|
|
|
locals = var;
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
2020-09-04 11:58:53 +03:00
|
|
|
static Obj *new_gvar(char *name, Type *ty) {
|
|
|
|
Obj *var = new_var(name, ty);
|
|
|
|
var->next = globals;
|
|
|
|
globals = var;
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
2020-10-07 06:49:08 +03:00
|
|
|
static char *new_unique_name(void) {
|
|
|
|
static int id = 0;
|
2020-10-08 08:30:04 +03:00
|
|
|
return format(".L..%d", id++);
|
2020-10-07 06:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static Obj *new_anon_gvar(Type *ty) {
|
|
|
|
return new_gvar(new_unique_name(), ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Obj *new_string_literal(char *p, Type *ty) {
|
|
|
|
Obj *var = new_anon_gvar(ty);
|
|
|
|
var->init_data = p;
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
2020-09-03 09:43:36 +03:00
|
|
|
static char *get_ident(Token *tok) {
|
|
|
|
if (tok->kind != TK_IDENT)
|
|
|
|
error_tok(tok, "expected an identifier");
|
|
|
|
return strndup(tok->loc, tok->len);
|
|
|
|
}
|
|
|
|
|
2020-09-04 11:23:26 +03:00
|
|
|
static Type *find_typedef(Token *tok) {
|
|
|
|
if (tok->kind == TK_IDENT) {
|
|
|
|
VarScope *sc = find_var(tok);
|
|
|
|
if (sc)
|
|
|
|
return sc->type_def;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long get_number(Token *tok) {
|
2020-09-26 04:15:32 +03:00
|
|
|
if (tok->kind != TK_NUM)
|
|
|
|
error_tok(tok, "expected a number");
|
|
|
|
return tok->val;
|
|
|
|
}
|
|
|
|
|
2020-10-07 04:43:02 +03:00
|
|
|
static void push_tag_scope(Token *tok, Type *ty) {
|
|
|
|
TagScope *sc = calloc(1, sizeof(TagScope));
|
|
|
|
sc->name = strndup(tok->loc, tok->len);
|
|
|
|
sc->ty = ty;
|
|
|
|
sc->next = scope->tags;
|
|
|
|
scope->tags = sc;
|
|
|
|
}
|
|
|
|
|
2020-08-28 16:07:54 +03:00
|
|
|
// declspec = ("void" | "_Bool" | "char" | "short" | "int" | "long"
|
2020-09-04 11:25:15 +03:00
|
|
|
// | "typedef" | "static"
|
2019-08-11 13:59:27 +03:00
|
|
|
// | struct-decl | union-decl | typedef-name
|
|
|
|
// | enum-specifier)+
|
2019-08-11 08:12:01 +03:00
|
|
|
//
|
|
|
|
// The order of typenames in a type-specifier doesn't matter. For
|
|
|
|
// example, `int long static` means the same as `static long int`.
|
|
|
|
// That can also be written as `static long` because you can omit
|
|
|
|
// `int` if `long` or `short` are specified. However, something like
|
|
|
|
// `char int` is not a valid type specifier. We have to accept only a
|
|
|
|
// limited combinations of the typenames.
|
|
|
|
//
|
|
|
|
// In this function, we count the number of occurrences of each typename
|
|
|
|
// while keeping the "current" type object that the typenames up
|
|
|
|
// until that point represent. When we reach a non-typename token,
|
|
|
|
// we returns the current type object.
|
2020-09-04 11:23:26 +03:00
|
|
|
static Type *declspec(Token **rest, Token *tok, VarAttr *attr) {
|
2019-08-11 08:12:01 +03:00
|
|
|
// We use a single integer as counters for all typenames.
|
|
|
|
// For example, bits 0 and 1 represents how many times we saw the
|
|
|
|
// keyword "void" so far. With this, we can use a switch statement
|
|
|
|
// as you can see below.
|
|
|
|
enum {
|
|
|
|
VOID = 1 << 0,
|
2020-08-28 16:07:54 +03:00
|
|
|
BOOL = 1 << 2,
|
|
|
|
CHAR = 1 << 4,
|
|
|
|
SHORT = 1 << 6,
|
|
|
|
INT = 1 << 8,
|
|
|
|
LONG = 1 << 10,
|
|
|
|
OTHER = 1 << 12,
|
2019-08-11 08:12:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
Type *ty = ty_int;
|
|
|
|
int counter = 0;
|
|
|
|
|
|
|
|
while (is_typename(tok)) {
|
2020-09-04 11:25:15 +03:00
|
|
|
// Handle storage class specifiers.
|
|
|
|
if (equal(tok, "typedef") || equal(tok, "static")) {
|
2020-09-04 11:23:26 +03:00
|
|
|
if (!attr)
|
|
|
|
error_tok(tok, "storage class specifier is not allowed in this context");
|
2020-09-04 11:25:15 +03:00
|
|
|
|
|
|
|
if (equal(tok, "typedef"))
|
|
|
|
attr->is_typedef = true;
|
|
|
|
else
|
|
|
|
attr->is_static = true;
|
|
|
|
|
|
|
|
if (attr->is_typedef + attr->is_static > 1)
|
|
|
|
error_tok(tok, "typedef and static may not be used together");
|
2020-09-04 11:23:26 +03:00
|
|
|
tok = tok->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-08-11 08:12:01 +03:00
|
|
|
// Handle user-defined types.
|
2020-09-04 11:23:26 +03:00
|
|
|
Type *ty2 = find_typedef(tok);
|
2019-08-11 13:59:27 +03:00
|
|
|
if (equal(tok, "struct") || equal(tok, "union") || equal(tok, "enum") || ty2) {
|
2020-09-04 11:23:26 +03:00
|
|
|
if (counter)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (equal(tok, "struct")) {
|
2019-08-11 08:12:01 +03:00
|
|
|
ty = struct_decl(&tok, tok->next);
|
2020-09-04 11:23:26 +03:00
|
|
|
} else if (equal(tok, "union")) {
|
2019-08-11 08:12:01 +03:00
|
|
|
ty = union_decl(&tok, tok->next);
|
2019-08-11 13:59:27 +03:00
|
|
|
} else if (equal(tok, "enum")) {
|
|
|
|
ty = enum_specifier(&tok, tok->next);
|
2020-09-04 11:23:26 +03:00
|
|
|
} else {
|
|
|
|
ty = ty2;
|
|
|
|
tok = tok->next;
|
|
|
|
}
|
|
|
|
|
2019-08-11 08:12:01 +03:00
|
|
|
counter += OTHER;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle built-in types.
|
|
|
|
if (equal(tok, "void"))
|
|
|
|
counter += VOID;
|
2020-08-28 16:07:54 +03:00
|
|
|
else if (equal(tok, "_Bool"))
|
|
|
|
counter += BOOL;
|
2019-08-11 08:12:01 +03:00
|
|
|
else if (equal(tok, "char"))
|
|
|
|
counter += CHAR;
|
|
|
|
else if (equal(tok, "short"))
|
|
|
|
counter += SHORT;
|
|
|
|
else if (equal(tok, "int"))
|
|
|
|
counter += INT;
|
|
|
|
else if (equal(tok, "long"))
|
|
|
|
counter += LONG;
|
|
|
|
else
|
|
|
|
unreachable();
|
|
|
|
|
|
|
|
switch (counter) {
|
|
|
|
case VOID:
|
|
|
|
ty = ty_void;
|
|
|
|
break;
|
2020-08-28 16:07:54 +03:00
|
|
|
case BOOL:
|
|
|
|
ty = ty_bool;
|
|
|
|
break;
|
2019-08-11 08:12:01 +03:00
|
|
|
case CHAR:
|
|
|
|
ty = ty_char;
|
|
|
|
break;
|
|
|
|
case SHORT:
|
|
|
|
case SHORT + INT:
|
|
|
|
ty = ty_short;
|
|
|
|
break;
|
|
|
|
case INT:
|
|
|
|
ty = ty_int;
|
|
|
|
break;
|
|
|
|
case LONG:
|
|
|
|
case LONG + INT:
|
2020-04-20 15:53:23 +03:00
|
|
|
case LONG + LONG:
|
|
|
|
case LONG + LONG + INT:
|
2019-08-11 08:12:01 +03:00
|
|
|
ty = ty_long;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error_tok(tok, "invalid type");
|
|
|
|
}
|
|
|
|
|
|
|
|
tok = tok->next;
|
2020-09-06 02:10:01 +03:00
|
|
|
}
|
|
|
|
|
2019-08-11 08:12:01 +03:00
|
|
|
*rest = tok;
|
|
|
|
return ty;
|
2020-09-03 09:43:36 +03:00
|
|
|
}
|
|
|
|
|
2020-09-26 04:15:32 +03:00
|
|
|
// func-params = (param ("," param)*)? ")"
|
2020-09-04 07:39:48 +03:00
|
|
|
// param = declspec declarator
|
2020-09-26 04:15:32 +03:00
|
|
|
static Type *func_params(Token **rest, Token *tok, Type *ty) {
|
|
|
|
Type head = {};
|
|
|
|
Type *cur = &head;
|
|
|
|
|
|
|
|
while (!equal(tok, ")")) {
|
|
|
|
if (cur != &head)
|
|
|
|
tok = skip(tok, ",");
|
2019-08-14 05:00:05 +03:00
|
|
|
|
|
|
|
Type *ty2 = declspec(&tok, tok, NULL);
|
|
|
|
ty2 = declarator(&tok, tok, ty2);
|
|
|
|
|
|
|
|
// "array of T" is converted to "pointer to T" only in the parameter
|
|
|
|
// context. For example, *argv[] is converted to **argv by this.
|
|
|
|
if (ty2->kind == TY_ARRAY) {
|
|
|
|
Token *name = ty2->name;
|
|
|
|
ty2 = pointer_to(ty2->base);
|
|
|
|
ty2->name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur = cur->next = copy_type(ty2);
|
2020-09-26 04:15:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ty = func_type(ty);
|
|
|
|
ty->params = head.next;
|
|
|
|
*rest = tok->next;
|
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:03:20 +03:00
|
|
|
// array-dimensions = num? "]" type-suffix
|
|
|
|
static Type *array_dimensions(Token **rest, Token *tok, Type *ty) {
|
|
|
|
if (equal(tok, "]")) {
|
|
|
|
ty = type_suffix(rest, tok->next, ty);
|
|
|
|
return array_of(ty, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sz = get_number(tok);
|
|
|
|
tok = skip(tok->next, "]");
|
|
|
|
ty = type_suffix(rest, tok, ty);
|
|
|
|
return array_of(ty, sz);
|
|
|
|
}
|
|
|
|
|
2020-09-26 04:15:32 +03:00
|
|
|
// type-suffix = "(" func-params
|
2019-08-13 15:03:20 +03:00
|
|
|
// | "[" array-dimensions
|
2020-09-26 04:15:32 +03:00
|
|
|
// | ε
|
2020-09-04 13:01:33 +03:00
|
|
|
static Type *type_suffix(Token **rest, Token *tok, Type *ty) {
|
2020-09-26 04:15:32 +03:00
|
|
|
if (equal(tok, "("))
|
|
|
|
return func_params(rest, tok->next, ty);
|
2020-09-04 07:39:48 +03:00
|
|
|
|
2019-08-13 15:03:20 +03:00
|
|
|
if (equal(tok, "["))
|
|
|
|
return array_dimensions(rest, tok->next, ty);
|
2020-09-04 07:39:48 +03:00
|
|
|
|
2020-09-04 13:01:33 +03:00
|
|
|
*rest = tok;
|
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
|
2019-08-10 17:05:37 +03:00
|
|
|
// declarator = "*"* ("(" ident ")" | "(" declarator ")" | ident) type-suffix
|
2020-09-03 09:43:36 +03:00
|
|
|
static Type *declarator(Token **rest, Token *tok, Type *ty) {
|
|
|
|
while (consume(&tok, tok, "*"))
|
|
|
|
ty = pointer_to(ty);
|
|
|
|
|
2019-08-10 17:05:37 +03:00
|
|
|
if (equal(tok, "(")) {
|
|
|
|
Token *start = tok;
|
|
|
|
Type dummy = {};
|
|
|
|
declarator(&tok, start->next, &dummy);
|
|
|
|
tok = skip(tok, ")");
|
|
|
|
ty = type_suffix(rest, tok, ty);
|
|
|
|
return declarator(&tok, start->next, ty);
|
|
|
|
}
|
|
|
|
|
2020-09-03 09:43:36 +03:00
|
|
|
if (tok->kind != TK_IDENT)
|
|
|
|
error_tok(tok, "expected a variable name");
|
2020-09-04 13:01:33 +03:00
|
|
|
ty = type_suffix(rest, tok->next, ty);
|
2020-09-03 09:43:36 +03:00
|
|
|
ty->name = tok;
|
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
|
2019-08-11 09:04:35 +03:00
|
|
|
// abstract-declarator = "*"* ("(" abstract-declarator ")")? type-suffix
|
|
|
|
static Type *abstract_declarator(Token **rest, Token *tok, Type *ty) {
|
|
|
|
while (equal(tok, "*")) {
|
|
|
|
ty = pointer_to(ty);
|
|
|
|
tok = tok->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(tok, "(")) {
|
|
|
|
Token *start = tok;
|
|
|
|
Type dummy = {};
|
|
|
|
abstract_declarator(&tok, start->next, &dummy);
|
|
|
|
tok = skip(tok, ")");
|
|
|
|
ty = type_suffix(rest, tok, ty);
|
|
|
|
return abstract_declarator(&tok, start->next, ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
return type_suffix(rest, tok, ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
// type-name = declspec abstract-declarator
|
|
|
|
static Type *typename(Token **rest, Token *tok) {
|
|
|
|
Type *ty = declspec(&tok, tok, NULL);
|
|
|
|
return abstract_declarator(rest, tok, ty);
|
|
|
|
}
|
|
|
|
|
2019-08-11 13:59:27 +03:00
|
|
|
// enum-specifier = ident? "{" enum-list? "}"
|
|
|
|
// | ident ("{" enum-list? "}")?
|
|
|
|
//
|
|
|
|
// enum-list = ident ("=" num)? ("," ident ("=" num)?)*
|
|
|
|
static Type *enum_specifier(Token **rest, Token *tok) {
|
|
|
|
Type *ty = enum_type();
|
|
|
|
|
|
|
|
// Read a struct tag.
|
|
|
|
Token *tag = NULL;
|
|
|
|
if (tok->kind == TK_IDENT) {
|
|
|
|
tag = tok;
|
|
|
|
tok = tok->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag && !equal(tok, "{")) {
|
|
|
|
Type *ty = find_tag(tag);
|
|
|
|
if (!ty)
|
|
|
|
error_tok(tag, "unknown enum type");
|
|
|
|
if (ty->kind != TY_ENUM)
|
|
|
|
error_tok(tag, "not an enum tag");
|
|
|
|
*rest = tok;
|
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
|
|
|
|
tok = skip(tok, "{");
|
|
|
|
|
|
|
|
// Read an enum-list.
|
|
|
|
int i = 0;
|
|
|
|
int val = 0;
|
|
|
|
while (!equal(tok, "}")) {
|
|
|
|
if (i++ > 0)
|
|
|
|
tok = skip(tok, ",");
|
|
|
|
|
|
|
|
char *name = get_ident(tok);
|
|
|
|
tok = tok->next;
|
|
|
|
|
|
|
|
if (equal(tok, "=")) {
|
|
|
|
val = get_number(tok->next);
|
|
|
|
tok = tok->next->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
VarScope *sc = push_scope(name);
|
|
|
|
sc->enum_ty = ty;
|
|
|
|
sc->enum_val = val++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*rest = tok->next;
|
|
|
|
|
|
|
|
if (tag)
|
|
|
|
push_tag_scope(tag, ty);
|
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
|
2020-09-03 09:43:36 +03:00
|
|
|
// declaration = declspec (declarator ("=" expr)? ("," declarator ("=" expr)?)*)? ";"
|
2020-09-04 11:23:26 +03:00
|
|
|
static Node *declaration(Token **rest, Token *tok, Type *basety) {
|
2020-09-03 09:43:36 +03:00
|
|
|
Node head = {};
|
|
|
|
Node *cur = &head;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (!equal(tok, ";")) {
|
|
|
|
if (i++ > 0)
|
|
|
|
tok = skip(tok, ",");
|
|
|
|
|
|
|
|
Type *ty = declarator(&tok, tok, basety);
|
2019-08-13 15:03:20 +03:00
|
|
|
if (ty->size < 0)
|
|
|
|
error_tok(tok, "variable has incomplete type");
|
2019-08-11 04:18:55 +03:00
|
|
|
if (ty->kind == TY_VOID)
|
|
|
|
error_tok(tok, "variable declared void");
|
|
|
|
|
2020-09-03 09:43:36 +03:00
|
|
|
Obj *var = new_lvar(get_ident(ty->name), ty);
|
|
|
|
|
|
|
|
if (!equal(tok, "="))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Node *lhs = new_var_node(var, ty->name);
|
|
|
|
Node *rhs = assign(&tok, tok->next);
|
|
|
|
Node *node = new_binary(ND_ASSIGN, lhs, rhs, tok);
|
|
|
|
cur = cur->next = new_unary(ND_EXPR_STMT, node, tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *node = new_node(ND_BLOCK, tok);
|
|
|
|
node->body = head.next;
|
|
|
|
*rest = tok->next;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-08-27 15:04:17 +03:00
|
|
|
// Returns true if a given token represents a type.
|
|
|
|
static bool is_typename(Token *tok) {
|
2019-08-11 04:18:55 +03:00
|
|
|
static char *kw[] = {
|
2020-08-28 16:07:54 +03:00
|
|
|
"void", "_Bool", "char", "short", "int", "long", "struct", "union",
|
2020-09-04 11:25:15 +03:00
|
|
|
"typedef", "enum", "static",
|
2019-08-11 04:18:55 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = 0; i < sizeof(kw) / sizeof(*kw); i++)
|
|
|
|
if (equal(tok, kw[i]))
|
|
|
|
return true;
|
2020-09-04 11:23:26 +03:00
|
|
|
return find_typedef(tok);
|
2020-08-27 15:04:17 +03:00
|
|
|
}
|
|
|
|
|
2020-10-07 14:12:57 +03:00
|
|
|
// stmt = "return" expr ";"
|
2020-10-07 06:47:09 +03:00
|
|
|
// | "if" "(" expr ")" stmt ("else" stmt)?
|
2019-08-04 11:35:53 +03:00
|
|
|
// | "for" "(" expr-stmt expr? ";" expr? ")" stmt
|
2019-08-04 11:24:03 +03:00
|
|
|
// | "while" "(" expr ")" stmt
|
2020-09-04 06:27:21 +03:00
|
|
|
// | "goto" ident ";"
|
|
|
|
// | ident ":" stmt
|
2020-09-04 07:38:41 +03:00
|
|
|
// | "{" compound-stmt
|
2020-10-07 14:12:57 +03:00
|
|
|
// | expr-stmt
|
2020-09-26 02:50:44 +03:00
|
|
|
static Node *stmt(Token **rest, Token *tok) {
|
2020-10-07 14:12:57 +03:00
|
|
|
if (equal(tok, "return")) {
|
2020-09-26 05:23:04 +03:00
|
|
|
Node *node = new_node(ND_RETURN, tok);
|
2020-04-18 03:27:47 +03:00
|
|
|
Node *exp = expr(&tok, tok->next);
|
2020-10-07 14:12:57 +03:00
|
|
|
*rest = skip(tok, ";");
|
2020-04-18 03:27:47 +03:00
|
|
|
|
|
|
|
add_type(exp);
|
|
|
|
node->lhs = new_cast(exp, current_fn->ty->return_ty);
|
2020-10-07 14:12:57 +03:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-10-07 06:47:09 +03:00
|
|
|
if (equal(tok, "if")) {
|
2020-09-26 05:23:04 +03:00
|
|
|
Node *node = new_node(ND_IF, tok);
|
2020-10-07 06:47:09 +03:00
|
|
|
tok = skip(tok->next, "(");
|
|
|
|
node->cond = expr(&tok, tok);
|
|
|
|
tok = skip(tok, ")");
|
|
|
|
node->then = stmt(&tok, tok);
|
|
|
|
if (equal(tok, "else"))
|
|
|
|
node->els = stmt(&tok, tok->next);
|
|
|
|
*rest = tok;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2019-08-04 11:35:53 +03:00
|
|
|
if (equal(tok, "for")) {
|
2020-09-26 05:23:04 +03:00
|
|
|
Node *node = new_node(ND_FOR, tok);
|
2019-08-04 11:35:53 +03:00
|
|
|
tok = skip(tok->next, "(");
|
|
|
|
|
2019-08-11 16:53:26 +03:00
|
|
|
enter_scope();
|
|
|
|
|
|
|
|
if (is_typename(tok)) {
|
|
|
|
Type *basety = declspec(&tok, tok, NULL);
|
|
|
|
node->init = declaration(&tok, tok, basety);
|
|
|
|
} else {
|
|
|
|
node->init = expr_stmt(&tok, tok);
|
|
|
|
}
|
2019-08-04 11:35:53 +03:00
|
|
|
|
|
|
|
if (!equal(tok, ";"))
|
|
|
|
node->cond = expr(&tok, tok);
|
|
|
|
tok = skip(tok, ";");
|
|
|
|
|
|
|
|
if (!equal(tok, ")"))
|
|
|
|
node->inc = expr(&tok, tok);
|
|
|
|
tok = skip(tok, ")");
|
|
|
|
|
|
|
|
node->then = stmt(rest, tok);
|
2019-08-11 16:53:26 +03:00
|
|
|
leave_scope();
|
2019-08-04 11:35:53 +03:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2019-08-04 11:24:03 +03:00
|
|
|
if (equal(tok, "while")) {
|
2020-09-26 05:23:04 +03:00
|
|
|
Node *node = new_node(ND_FOR, tok);
|
2019-08-04 11:24:03 +03:00
|
|
|
tok = skip(tok->next, "(");
|
|
|
|
node->cond = expr(&tok, tok);
|
|
|
|
tok = skip(tok, ")");
|
|
|
|
node->then = stmt(rest, tok);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-09-04 06:27:21 +03:00
|
|
|
if (equal(tok, "goto")) {
|
|
|
|
Node *node = new_node(ND_GOTO, tok);
|
|
|
|
node->label = get_ident(tok->next);
|
|
|
|
node->goto_next = gotos;
|
|
|
|
gotos = node;
|
|
|
|
*rest = skip(tok->next->next, ";");
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok->kind == TK_IDENT && equal(tok->next, ":")) {
|
|
|
|
Node *node = new_node(ND_LABEL, tok);
|
|
|
|
node->label = strndup(tok->loc, tok->len);
|
|
|
|
node->unique_label = new_unique_name();
|
|
|
|
node->lhs = stmt(rest, tok->next->next);
|
|
|
|
node->goto_next = labels;
|
|
|
|
labels = node;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-09-04 07:38:41 +03:00
|
|
|
if (equal(tok, "{"))
|
|
|
|
return compound_stmt(rest, tok->next);
|
|
|
|
|
2020-09-26 02:50:44 +03:00
|
|
|
return expr_stmt(rest, tok);
|
|
|
|
}
|
|
|
|
|
2020-09-04 11:23:26 +03:00
|
|
|
// compound-stmt = (typedef | declaration | stmt)* "}"
|
2020-09-04 07:38:41 +03:00
|
|
|
static Node *compound_stmt(Token **rest, Token *tok) {
|
2020-09-26 05:23:04 +03:00
|
|
|
Node *node = new_node(ND_BLOCK, tok);
|
2020-09-04 07:38:41 +03:00
|
|
|
Node head = {};
|
|
|
|
Node *cur = &head;
|
2020-09-04 12:00:26 +03:00
|
|
|
|
|
|
|
enter_scope();
|
|
|
|
|
2020-09-04 07:39:06 +03:00
|
|
|
while (!equal(tok, "}")) {
|
2020-08-30 10:44:12 +03:00
|
|
|
if (is_typename(tok) && !equal(tok->next, ":")) {
|
2020-09-04 11:23:26 +03:00
|
|
|
VarAttr attr = {};
|
|
|
|
Type *basety = declspec(&tok, tok, &attr);
|
|
|
|
|
|
|
|
if (attr.is_typedef) {
|
|
|
|
tok = parse_typedef(tok, basety);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur = cur->next = declaration(&tok, tok, basety);
|
|
|
|
} else {
|
2020-09-03 09:43:36 +03:00
|
|
|
cur = cur->next = stmt(&tok, tok);
|
2020-09-04 11:23:26 +03:00
|
|
|
}
|
2020-09-04 07:39:06 +03:00
|
|
|
add_type(cur);
|
|
|
|
}
|
2020-09-04 07:38:41 +03:00
|
|
|
|
2020-09-04 12:00:26 +03:00
|
|
|
leave_scope();
|
|
|
|
|
2020-09-04 07:38:41 +03:00
|
|
|
node->body = head.next;
|
|
|
|
*rest = tok->next;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-05-14 05:05:01 +03:00
|
|
|
// expr-stmt = expr? ";"
|
2020-09-26 02:50:44 +03:00
|
|
|
static Node *expr_stmt(Token **rest, Token *tok) {
|
2020-05-14 05:05:01 +03:00
|
|
|
if (equal(tok, ";")) {
|
|
|
|
*rest = tok->next;
|
2020-09-26 05:23:04 +03:00
|
|
|
return new_node(ND_BLOCK, tok);
|
2020-05-14 05:05:01 +03:00
|
|
|
}
|
|
|
|
|
2020-09-26 05:23:04 +03:00
|
|
|
Node *node = new_node(ND_EXPR_STMT, tok);
|
|
|
|
node->lhs = expr(&tok, tok);
|
2020-09-26 02:50:44 +03:00
|
|
|
*rest = skip(tok, ";");
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2019-08-12 04:29:17 +03:00
|
|
|
// expr = assign ("," expr)?
|
2020-10-07 14:11:16 +03:00
|
|
|
static Node *expr(Token **rest, Token *tok) {
|
2019-08-12 04:29:17 +03:00
|
|
|
Node *node = assign(&tok, tok);
|
|
|
|
|
|
|
|
if (equal(tok, ","))
|
|
|
|
return new_binary(ND_COMMA, node, expr(rest, tok->next), tok);
|
|
|
|
|
|
|
|
*rest = tok;
|
|
|
|
return node;
|
2020-09-26 02:59:56 +03:00
|
|
|
}
|
|
|
|
|
2020-10-07 14:17:30 +03:00
|
|
|
// Convert `A op= B` to `tmp = &A, *tmp = *tmp op B`
|
|
|
|
// where tmp is a fresh pointer variable.
|
|
|
|
static Node *to_assign(Node *binary) {
|
|
|
|
add_type(binary->lhs);
|
|
|
|
add_type(binary->rhs);
|
|
|
|
Token *tok = binary->tok;
|
|
|
|
|
|
|
|
Obj *var = new_lvar("", pointer_to(binary->lhs->ty));
|
|
|
|
|
|
|
|
Node *expr1 = new_binary(ND_ASSIGN, new_var_node(var, tok),
|
|
|
|
new_unary(ND_ADDR, binary->lhs, tok), tok);
|
|
|
|
|
|
|
|
Node *expr2 =
|
|
|
|
new_binary(ND_ASSIGN,
|
|
|
|
new_unary(ND_DEREF, new_var_node(var, tok), tok),
|
|
|
|
new_binary(binary->kind,
|
|
|
|
new_unary(ND_DEREF, new_var_node(var, tok), tok),
|
|
|
|
binary->rhs,
|
|
|
|
tok),
|
|
|
|
tok);
|
|
|
|
|
|
|
|
return new_binary(ND_COMMA, expr1, expr2, tok);
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:22:11 +03:00
|
|
|
// assign = logor (assign-op assign)?
|
2020-10-07 14:19:35 +03:00
|
|
|
// assign-op = "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "&=" | "|=" | "^="
|
2020-09-26 02:59:56 +03:00
|
|
|
static Node *assign(Token **rest, Token *tok) {
|
2020-10-07 14:22:11 +03:00
|
|
|
Node *node = logor(&tok, tok);
|
2020-09-26 05:23:04 +03:00
|
|
|
|
2020-09-26 02:59:56 +03:00
|
|
|
if (equal(tok, "="))
|
2020-09-26 05:23:04 +03:00
|
|
|
return new_binary(ND_ASSIGN, node, assign(rest, tok->next), tok);
|
|
|
|
|
2020-10-07 14:17:30 +03:00
|
|
|
if (equal(tok, "+="))
|
|
|
|
return to_assign(new_add(node, assign(rest, tok->next), tok));
|
|
|
|
|
|
|
|
if (equal(tok, "-="))
|
|
|
|
return to_assign(new_sub(node, assign(rest, tok->next), tok));
|
|
|
|
|
|
|
|
if (equal(tok, "*="))
|
|
|
|
return to_assign(new_binary(ND_MUL, node, assign(rest, tok->next), tok));
|
|
|
|
|
|
|
|
if (equal(tok, "/="))
|
|
|
|
return to_assign(new_binary(ND_DIV, node, assign(rest, tok->next), tok));
|
|
|
|
|
2020-10-07 14:18:57 +03:00
|
|
|
if (equal(tok, "%="))
|
|
|
|
return to_assign(new_binary(ND_MOD, node, assign(rest, tok->next), tok));
|
|
|
|
|
2020-10-07 14:19:35 +03:00
|
|
|
if (equal(tok, "&="))
|
|
|
|
return to_assign(new_binary(ND_BITAND, node, assign(rest, tok->next), tok));
|
|
|
|
|
|
|
|
if (equal(tok, "|="))
|
|
|
|
return to_assign(new_binary(ND_BITOR, node, assign(rest, tok->next), tok));
|
|
|
|
|
|
|
|
if (equal(tok, "^="))
|
|
|
|
return to_assign(new_binary(ND_BITXOR, node, assign(rest, tok->next), tok));
|
|
|
|
|
|
|
|
*rest = tok;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:22:11 +03:00
|
|
|
// logor = logand ("||" logand)*
|
|
|
|
static Node *logor(Token **rest, Token *tok) {
|
|
|
|
Node *node = logand(&tok, tok);
|
|
|
|
while (equal(tok, "||")) {
|
|
|
|
Token *start = tok;
|
|
|
|
node = new_binary(ND_LOGOR, node, logand(&tok, tok->next), start);
|
|
|
|
}
|
|
|
|
*rest = tok;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
// logand = bitor ("&&" bitor)*
|
|
|
|
static Node *logand(Token **rest, Token *tok) {
|
|
|
|
Node *node = bitor(&tok, tok);
|
|
|
|
while (equal(tok, "&&")) {
|
|
|
|
Token *start = tok;
|
|
|
|
node = new_binary(ND_LOGAND, node, bitor(&tok, tok->next), start);
|
|
|
|
}
|
|
|
|
*rest = tok;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:19:35 +03:00
|
|
|
// bitor = bitxor ("|" bitxor)*
|
|
|
|
static Node *bitor(Token **rest, Token *tok) {
|
|
|
|
Node *node = bitxor(&tok, tok);
|
|
|
|
while (equal(tok, "|")) {
|
|
|
|
Token *start = tok;
|
|
|
|
node = new_binary(ND_BITOR, node, bitxor(&tok, tok->next), start);
|
|
|
|
}
|
|
|
|
*rest = tok;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
// bitxor = bitand ("^" bitand)*
|
|
|
|
static Node *bitxor(Token **rest, Token *tok) {
|
|
|
|
Node *node = bitand(&tok, tok);
|
|
|
|
while (equal(tok, "^")) {
|
|
|
|
Token *start = tok;
|
|
|
|
node = new_binary(ND_BITXOR, node, bitand(&tok, tok->next), start);
|
|
|
|
}
|
|
|
|
*rest = tok;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
// bitand = equality ("&" equality)*
|
|
|
|
static Node *bitand(Token **rest, Token *tok) {
|
|
|
|
Node *node = equality(&tok, tok);
|
|
|
|
while (equal(tok, "&")) {
|
|
|
|
Token *start = tok;
|
|
|
|
node = new_binary(ND_BITAND, node, equality(&tok, tok->next), start);
|
|
|
|
}
|
2020-09-26 02:59:56 +03:00
|
|
|
*rest = tok;
|
|
|
|
return node;
|
2020-10-07 14:11:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// equality = relational ("==" relational | "!=" relational)*
|
|
|
|
static Node *equality(Token **rest, Token *tok) {
|
|
|
|
Node *node = relational(&tok, tok);
|
|
|
|
|
|
|
|
for (;;) {
|
2020-09-26 05:23:04 +03:00
|
|
|
Token *start = tok;
|
|
|
|
|
2020-10-07 14:11:16 +03:00
|
|
|
if (equal(tok, "==")) {
|
2020-09-26 05:23:04 +03:00
|
|
|
node = new_binary(ND_EQ, node, relational(&tok, tok->next), start);
|
2020-10-07 14:11:16 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(tok, "!=")) {
|
2020-09-26 05:23:04 +03:00
|
|
|
node = new_binary(ND_NE, node, relational(&tok, tok->next), start);
|
2020-10-07 14:11:16 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
*rest = tok;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// relational = add ("<" add | "<=" add | ">" add | ">=" add)*
|
|
|
|
static Node *relational(Token **rest, Token *tok) {
|
|
|
|
Node *node = add(&tok, tok);
|
|
|
|
|
|
|
|
for (;;) {
|
2020-09-26 05:23:04 +03:00
|
|
|
Token *start = tok;
|
|
|
|
|
2020-10-07 14:11:16 +03:00
|
|
|
if (equal(tok, "<")) {
|
2020-09-26 05:23:04 +03:00
|
|
|
node = new_binary(ND_LT, node, add(&tok, tok->next), start);
|
2020-10-07 14:11:16 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(tok, "<=")) {
|
2020-09-26 05:23:04 +03:00
|
|
|
node = new_binary(ND_LE, node, add(&tok, tok->next), start);
|
2020-10-07 14:11:16 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(tok, ">")) {
|
2020-09-26 05:23:04 +03:00
|
|
|
node = new_binary(ND_LT, add(&tok, tok->next), node, start);
|
2020-10-07 14:11:16 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(tok, ">=")) {
|
2020-09-26 05:23:04 +03:00
|
|
|
node = new_binary(ND_LE, add(&tok, tok->next), node, start);
|
2020-10-07 14:11:16 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
*rest = tok;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-04 07:39:06 +03:00
|
|
|
// In C, `+` operator is overloaded to perform the pointer arithmetic.
|
|
|
|
// If p is a pointer, p+n adds not n but sizeof(*p)*n to the value of p,
|
|
|
|
// so that p+n points to the location n elements (not bytes) ahead of p.
|
|
|
|
// In other words, we need to scale an integer value before adding to a
|
|
|
|
// pointer value. This function takes care of the scaling.
|
|
|
|
static Node *new_add(Node *lhs, Node *rhs, Token *tok) {
|
|
|
|
add_type(lhs);
|
|
|
|
add_type(rhs);
|
|
|
|
|
|
|
|
// num + num
|
|
|
|
if (is_integer(lhs->ty) && is_integer(rhs->ty))
|
|
|
|
return new_binary(ND_ADD, lhs, rhs, tok);
|
|
|
|
|
|
|
|
if (lhs->ty->base && rhs->ty->base)
|
|
|
|
error_tok(tok, "invalid operands");
|
|
|
|
|
|
|
|
// Canonicalize `num + ptr` to `ptr + num`.
|
|
|
|
if (!lhs->ty->base && rhs->ty->base) {
|
|
|
|
Node *tmp = lhs;
|
|
|
|
lhs = rhs;
|
|
|
|
rhs = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ptr + num
|
2020-09-26 04:24:45 +03:00
|
|
|
rhs = new_binary(ND_MUL, rhs, new_long(lhs->ty->base->size, tok), tok);
|
2020-09-04 07:39:06 +03:00
|
|
|
return new_binary(ND_ADD, lhs, rhs, tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Like `+`, `-` is overloaded for the pointer type.
|
|
|
|
static Node *new_sub(Node *lhs, Node *rhs, Token *tok) {
|
|
|
|
add_type(lhs);
|
|
|
|
add_type(rhs);
|
|
|
|
|
|
|
|
// num - num
|
|
|
|
if (is_integer(lhs->ty) && is_integer(rhs->ty))
|
|
|
|
return new_binary(ND_SUB, lhs, rhs, tok);
|
|
|
|
|
|
|
|
// ptr - num
|
|
|
|
if (lhs->ty->base && is_integer(rhs->ty)) {
|
2020-09-26 04:24:45 +03:00
|
|
|
rhs = new_binary(ND_MUL, rhs, new_long(lhs->ty->base->size, tok), tok);
|
2020-09-04 07:39:06 +03:00
|
|
|
add_type(rhs);
|
|
|
|
Node *node = new_binary(ND_SUB, lhs, rhs, tok);
|
|
|
|
node->ty = lhs->ty;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ptr - ptr, which returns how many elements are between the two.
|
|
|
|
if (lhs->ty->base && rhs->ty->base) {
|
|
|
|
Node *node = new_binary(ND_SUB, lhs, rhs, tok);
|
|
|
|
node->ty = ty_int;
|
2020-09-26 04:15:32 +03:00
|
|
|
return new_binary(ND_DIV, node, new_num(lhs->ty->base->size, tok), tok);
|
2020-09-04 07:39:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
error_tok(tok, "invalid operands");
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:11:16 +03:00
|
|
|
// add = mul ("+" mul | "-" mul)*
|
|
|
|
static Node *add(Token **rest, Token *tok) {
|
|
|
|
Node *node = mul(&tok, tok);
|
|
|
|
|
|
|
|
for (;;) {
|
2020-09-26 05:23:04 +03:00
|
|
|
Token *start = tok;
|
|
|
|
|
2020-10-07 14:11:16 +03:00
|
|
|
if (equal(tok, "+")) {
|
2020-09-04 07:39:06 +03:00
|
|
|
node = new_add(node, mul(&tok, tok->next), start);
|
2020-10-07 14:11:16 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(tok, "-")) {
|
2020-09-04 07:39:06 +03:00
|
|
|
node = new_sub(node, mul(&tok, tok->next), start);
|
2020-10-07 14:11:16 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
*rest = tok;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:18:57 +03:00
|
|
|
// mul = cast ("*" cast | "/" cast | "%" cast)*
|
2020-10-07 14:11:16 +03:00
|
|
|
static Node *mul(Token **rest, Token *tok) {
|
2019-08-11 10:06:14 +03:00
|
|
|
Node *node = cast(&tok, tok);
|
2020-10-07 14:11:16 +03:00
|
|
|
|
|
|
|
for (;;) {
|
2020-09-26 05:23:04 +03:00
|
|
|
Token *start = tok;
|
|
|
|
|
2020-10-07 14:11:16 +03:00
|
|
|
if (equal(tok, "*")) {
|
2019-08-11 10:06:14 +03:00
|
|
|
node = new_binary(ND_MUL, node, cast(&tok, tok->next), start);
|
2020-10-07 14:11:16 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(tok, "/")) {
|
2019-08-11 10:06:14 +03:00
|
|
|
node = new_binary(ND_DIV, node, cast(&tok, tok->next), start);
|
2020-10-07 14:11:16 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:18:57 +03:00
|
|
|
if (equal(tok, "%")) {
|
|
|
|
node = new_binary(ND_MOD, node, cast(&tok, tok->next), start);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:11:16 +03:00
|
|
|
*rest = tok;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-11 10:06:14 +03:00
|
|
|
// cast = "(" type-name ")" cast | unary
|
|
|
|
static Node *cast(Token **rest, Token *tok) {
|
|
|
|
if (equal(tok, "(") && is_typename(tok->next)) {
|
|
|
|
Token *start = tok;
|
|
|
|
Type *ty = typename(&tok, tok->next);
|
|
|
|
tok = skip(tok, ")");
|
|
|
|
Node *node = new_cast(cast(rest, tok), ty);
|
|
|
|
node->tok = start;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
return unary(rest, tok);
|
|
|
|
}
|
|
|
|
|
2019-08-13 13:41:11 +03:00
|
|
|
// unary = ("+" | "-" | "*" | "&" | "!" | "~") cast
|
2020-10-07 14:18:26 +03:00
|
|
|
// | ("++" | "--") unary
|
2019-08-06 12:14:53 +03:00
|
|
|
// | postfix
|
2020-10-07 14:11:16 +03:00
|
|
|
static Node *unary(Token **rest, Token *tok) {
|
|
|
|
if (equal(tok, "+"))
|
2019-08-11 10:06:14 +03:00
|
|
|
return cast(rest, tok->next);
|
2020-10-07 14:11:16 +03:00
|
|
|
|
|
|
|
if (equal(tok, "-"))
|
2019-08-11 10:06:14 +03:00
|
|
|
return new_unary(ND_NEG, cast(rest, tok->next), tok);
|
2020-10-07 14:11:16 +03:00
|
|
|
|
2019-08-05 15:12:44 +03:00
|
|
|
if (equal(tok, "&"))
|
2019-08-11 10:06:14 +03:00
|
|
|
return new_unary(ND_ADDR, cast(rest, tok->next), tok);
|
2019-08-05 15:12:44 +03:00
|
|
|
|
|
|
|
if (equal(tok, "*"))
|
2019-08-11 10:06:14 +03:00
|
|
|
return new_unary(ND_DEREF, cast(rest, tok->next), tok);
|
2019-08-05 15:12:44 +03:00
|
|
|
|
2019-08-13 13:31:04 +03:00
|
|
|
if (equal(tok, "!"))
|
|
|
|
return new_unary(ND_NOT, cast(rest, tok->next), tok);
|
|
|
|
|
2019-08-13 13:41:11 +03:00
|
|
|
if (equal(tok, "~"))
|
|
|
|
return new_unary(ND_BITNOT, cast(rest, tok->next), tok);
|
|
|
|
|
2020-10-07 14:18:26 +03:00
|
|
|
// Read ++i as i+=1
|
|
|
|
if (equal(tok, "++"))
|
|
|
|
return to_assign(new_add(unary(rest, tok->next), new_num(1, tok), tok));
|
|
|
|
|
|
|
|
// Read --i as i-=1
|
|
|
|
if (equal(tok, "--"))
|
|
|
|
return to_assign(new_sub(unary(rest, tok->next), new_num(1, tok), tok));
|
|
|
|
|
2019-08-06 12:14:53 +03:00
|
|
|
return postfix(rest, tok);
|
|
|
|
}
|
|
|
|
|
2019-08-08 16:43:58 +03:00
|
|
|
// struct-members = (declspec declarator ("," declarator)* ";")*
|
|
|
|
static void struct_members(Token **rest, Token *tok, Type *ty) {
|
|
|
|
Member head = {};
|
|
|
|
Member *cur = &head;
|
|
|
|
|
|
|
|
while (!equal(tok, "}")) {
|
2020-09-04 11:23:26 +03:00
|
|
|
Type *basety = declspec(&tok, tok, NULL);
|
2019-08-08 16:43:58 +03:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (!consume(&tok, tok, ";")) {
|
|
|
|
if (i++)
|
|
|
|
tok = skip(tok, ",");
|
|
|
|
|
|
|
|
Member *mem = calloc(1, sizeof(Member));
|
|
|
|
mem->ty = declarator(&tok, tok, basety);
|
|
|
|
mem->name = mem->ty->name;
|
|
|
|
cur = cur->next = mem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*rest = tok->next;
|
|
|
|
ty->members = head.next;
|
|
|
|
}
|
|
|
|
|
2020-04-02 15:54:47 +03:00
|
|
|
// struct-union-decl = ident? ("{" struct-members)?
|
|
|
|
static Type *struct_union_decl(Token **rest, Token *tok) {
|
|
|
|
// Read a tag.
|
2020-10-07 04:43:02 +03:00
|
|
|
Token *tag = NULL;
|
|
|
|
if (tok->kind == TK_IDENT) {
|
|
|
|
tag = tok;
|
|
|
|
tok = tok->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag && !equal(tok, "{")) {
|
|
|
|
*rest = tok;
|
2020-10-07 05:10:28 +03:00
|
|
|
|
|
|
|
Type *ty = find_tag(tag);
|
|
|
|
if (ty)
|
|
|
|
return ty;
|
|
|
|
|
|
|
|
ty = struct_type();
|
|
|
|
ty->size = -1;
|
|
|
|
push_tag_scope(tag, ty);
|
2020-10-07 04:43:02 +03:00
|
|
|
return ty;
|
|
|
|
}
|
2019-08-08 16:43:58 +03:00
|
|
|
|
2020-10-07 05:10:28 +03:00
|
|
|
tok = skip(tok, "{");
|
|
|
|
|
2019-08-08 16:43:58 +03:00
|
|
|
// Construct a struct object.
|
2020-10-07 05:10:28 +03:00
|
|
|
Type *ty = struct_type();
|
|
|
|
struct_members(rest, tok, ty);
|
|
|
|
|
|
|
|
if (tag) {
|
|
|
|
// If this is a redefinition, overwrite a previous type.
|
|
|
|
// Otherwise, register the struct type.
|
|
|
|
for (TagScope *sc = scope->tags; sc; sc = sc->next) {
|
|
|
|
if (equal(tag, sc->name)) {
|
|
|
|
*sc->ty = *ty;
|
|
|
|
return sc->ty;
|
|
|
|
}
|
|
|
|
}
|
2019-08-08 16:43:58 +03:00
|
|
|
|
2020-04-02 15:54:47 +03:00
|
|
|
push_tag_scope(tag, ty);
|
2020-10-07 05:10:28 +03:00
|
|
|
}
|
|
|
|
|
2020-04-02 15:54:47 +03:00
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
|
|
|
|
// struct-decl = struct-union-decl
|
|
|
|
static Type *struct_decl(Token **rest, Token *tok) {
|
|
|
|
Type *ty = struct_union_decl(rest, tok);
|
|
|
|
ty->kind = TY_STRUCT;
|
|
|
|
|
2020-10-07 05:10:28 +03:00
|
|
|
if (ty->size < 0)
|
|
|
|
return ty;
|
|
|
|
|
2019-08-08 16:43:58 +03:00
|
|
|
// Assign offsets within the struct to members.
|
|
|
|
int offset = 0;
|
|
|
|
for (Member *mem = ty->members; mem; mem = mem->next) {
|
2020-08-30 11:21:54 +03:00
|
|
|
offset = align_to(offset, mem->ty->align);
|
2019-08-08 16:43:58 +03:00
|
|
|
mem->offset = offset;
|
|
|
|
offset += mem->ty->size;
|
2020-08-30 11:21:54 +03:00
|
|
|
|
|
|
|
if (ty->align < mem->ty->align)
|
|
|
|
ty->align = mem->ty->align;
|
2019-08-08 16:43:58 +03:00
|
|
|
}
|
2020-08-30 11:21:54 +03:00
|
|
|
ty->size = align_to(offset, ty->align);
|
2020-04-02 15:54:47 +03:00
|
|
|
return ty;
|
|
|
|
}
|
2019-08-08 16:43:58 +03:00
|
|
|
|
2020-04-02 15:54:47 +03:00
|
|
|
// union-decl = struct-union-decl
|
|
|
|
static Type *union_decl(Token **rest, Token *tok) {
|
|
|
|
Type *ty = struct_union_decl(rest, tok);
|
|
|
|
ty->kind = TY_UNION;
|
|
|
|
|
2020-10-07 05:10:28 +03:00
|
|
|
if (ty->size < 0)
|
|
|
|
return ty;
|
|
|
|
|
2020-04-02 15:54:47 +03:00
|
|
|
// If union, we don't have to assign offsets because they
|
|
|
|
// are already initialized to zero. We need to compute the
|
|
|
|
// alignment and the size though.
|
|
|
|
for (Member *mem = ty->members; mem; mem = mem->next) {
|
|
|
|
if (ty->align < mem->ty->align)
|
|
|
|
ty->align = mem->ty->align;
|
|
|
|
if (ty->size < mem->ty->size)
|
|
|
|
ty->size = mem->ty->size;
|
|
|
|
}
|
|
|
|
ty->size = align_to(ty->size, ty->align);
|
2019-08-08 16:43:58 +03:00
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Member *get_struct_member(Type *ty, Token *tok) {
|
|
|
|
for (Member *mem = ty->members; mem; mem = mem->next)
|
|
|
|
if (mem->name->len == tok->len &&
|
|
|
|
!strncmp(mem->name->loc, tok->loc, tok->len))
|
|
|
|
return mem;
|
|
|
|
error_tok(tok, "no such member");
|
|
|
|
}
|
|
|
|
|
|
|
|
static Node *struct_ref(Node *lhs, Token *tok) {
|
|
|
|
add_type(lhs);
|
2020-04-02 15:54:47 +03:00
|
|
|
if (lhs->ty->kind != TY_STRUCT && lhs->ty->kind != TY_UNION)
|
|
|
|
error_tok(lhs->tok, "not a struct nor a union");
|
2019-08-08 16:43:58 +03:00
|
|
|
|
|
|
|
Node *node = new_unary(ND_MEMBER, lhs, tok);
|
|
|
|
node->member = get_struct_member(lhs->ty, tok);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-04-13 15:44:21 +03:00
|
|
|
// Convert A++ to `(typeof A)((A += 1) - 1)`
|
|
|
|
static Node *new_inc_dec(Node *node, Token *tok, int addend) {
|
|
|
|
add_type(node);
|
|
|
|
return new_cast(new_add(to_assign(new_add(node, new_num(addend, tok), tok)),
|
|
|
|
new_num(-addend, tok), tok),
|
|
|
|
node->ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
// postfix = primary ("[" expr "]" | "." ident | "->" ident | "++" | "--")*
|
2019-08-06 12:14:53 +03:00
|
|
|
static Node *postfix(Token **rest, Token *tok) {
|
|
|
|
Node *node = primary(&tok, tok);
|
|
|
|
|
2019-08-08 16:43:58 +03:00
|
|
|
for (;;) {
|
|
|
|
if (equal(tok, "[")) {
|
|
|
|
// x[y] is short for *(x+y)
|
|
|
|
Token *start = tok;
|
|
|
|
Node *idx = expr(&tok, tok->next);
|
|
|
|
tok = skip(tok, "]");
|
|
|
|
node = new_unary(ND_DEREF, new_add(node, idx, start), start);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(tok, ".")) {
|
|
|
|
node = struct_ref(node, tok->next);
|
|
|
|
tok = tok->next->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:16:40 +03:00
|
|
|
if (equal(tok, "->")) {
|
|
|
|
// x->y is short for (*x).y
|
|
|
|
node = new_unary(ND_DEREF, node, tok);
|
|
|
|
node = struct_ref(node, tok->next);
|
|
|
|
tok = tok->next->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-04-13 15:44:21 +03:00
|
|
|
if (equal(tok, "++")) {
|
|
|
|
node = new_inc_dec(node, tok, 1);
|
|
|
|
tok = tok->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(tok, "--")) {
|
|
|
|
node = new_inc_dec(node, tok, -1);
|
|
|
|
tok = tok->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-08-08 16:43:58 +03:00
|
|
|
*rest = tok;
|
|
|
|
return node;
|
2019-08-06 12:14:53 +03:00
|
|
|
}
|
2020-10-07 14:11:16 +03:00
|
|
|
}
|
|
|
|
|
2019-08-04 13:03:46 +03:00
|
|
|
// funcall = ident "(" (assign ("," assign)*)? ")"
|
|
|
|
static Node *funcall(Token **rest, Token *tok) {
|
|
|
|
Token *start = tok;
|
|
|
|
tok = tok->next->next;
|
|
|
|
|
2020-08-21 11:16:28 +03:00
|
|
|
VarScope *sc = find_var(start);
|
|
|
|
if (!sc)
|
|
|
|
error_tok(start, "implicit declaration of a function");
|
|
|
|
if (!sc->var || sc->var->ty->kind != TY_FUNC)
|
|
|
|
error_tok(start, "not a function");
|
|
|
|
|
2020-03-22 12:43:16 +03:00
|
|
|
Type *ty = sc->var->ty;
|
|
|
|
Type *param_ty = ty->params;
|
|
|
|
|
2019-08-04 13:03:46 +03:00
|
|
|
Node head = {};
|
|
|
|
Node *cur = &head;
|
|
|
|
|
|
|
|
while (!equal(tok, ")")) {
|
|
|
|
if (cur != &head)
|
|
|
|
tok = skip(tok, ",");
|
2020-03-22 12:43:16 +03:00
|
|
|
|
|
|
|
Node *arg = assign(&tok, tok);
|
|
|
|
add_type(arg);
|
|
|
|
|
|
|
|
if (param_ty) {
|
|
|
|
if (param_ty->kind == TY_STRUCT || param_ty->kind == TY_UNION)
|
|
|
|
error_tok(arg->tok, "passing struct or union is not supported yet");
|
|
|
|
arg = new_cast(arg, param_ty);
|
|
|
|
param_ty = param_ty->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur = cur->next = arg;
|
2019-08-04 13:03:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
*rest = skip(tok, ")");
|
|
|
|
|
|
|
|
Node *node = new_node(ND_FUNCALL, start);
|
|
|
|
node->funcname = strndup(start->loc, start->len);
|
2020-03-22 12:43:16 +03:00
|
|
|
node->func_ty = ty;
|
|
|
|
node->ty = ty->return_ty;
|
2019-08-04 13:03:46 +03:00
|
|
|
node->args = head.next;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2019-08-07 02:05:18 +03:00
|
|
|
// primary = "(" "{" stmt+ "}" ")"
|
|
|
|
// | "(" expr ")"
|
2019-08-11 09:04:35 +03:00
|
|
|
// | "sizeof" "(" type-name ")"
|
2019-08-07 02:05:18 +03:00
|
|
|
// | "sizeof" unary
|
|
|
|
// | ident func-args?
|
|
|
|
// | str
|
|
|
|
// | num
|
2020-10-07 14:11:16 +03:00
|
|
|
static Node *primary(Token **rest, Token *tok) {
|
2019-08-11 09:04:35 +03:00
|
|
|
Token *start = tok;
|
|
|
|
|
2019-08-07 02:05:18 +03:00
|
|
|
if (equal(tok, "(") && equal(tok->next, "{")) {
|
|
|
|
// This is a GNU statement expresssion.
|
|
|
|
Node *node = new_node(ND_STMT_EXPR, tok);
|
|
|
|
node->body = compound_stmt(&tok, tok->next->next)->body;
|
|
|
|
*rest = skip(tok, ")");
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:11:16 +03:00
|
|
|
if (equal(tok, "(")) {
|
|
|
|
Node *node = expr(&tok, tok->next);
|
|
|
|
*rest = skip(tok, ")");
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2019-08-11 09:04:35 +03:00
|
|
|
if (equal(tok, "sizeof") && equal(tok->next, "(") && is_typename(tok->next->next)) {
|
|
|
|
Type *ty = typename(&tok, tok->next->next);
|
|
|
|
*rest = skip(tok, ")");
|
|
|
|
return new_num(ty->size, start);
|
|
|
|
}
|
|
|
|
|
2019-08-06 12:44:53 +03:00
|
|
|
if (equal(tok, "sizeof")) {
|
|
|
|
Node *node = unary(rest, tok->next);
|
|
|
|
add_type(node);
|
|
|
|
return new_num(node->ty->size, tok);
|
|
|
|
}
|
|
|
|
|
2020-09-26 02:59:56 +03:00
|
|
|
if (tok->kind == TK_IDENT) {
|
2019-08-04 12:25:20 +03:00
|
|
|
// Function call
|
2019-08-04 13:03:46 +03:00
|
|
|
if (equal(tok->next, "("))
|
|
|
|
return funcall(rest, tok);
|
2019-08-04 12:25:20 +03:00
|
|
|
|
2019-08-11 13:59:27 +03:00
|
|
|
// Variable or enum constant
|
2020-09-04 11:23:26 +03:00
|
|
|
VarScope *sc = find_var(tok);
|
2019-08-11 13:59:27 +03:00
|
|
|
if (!sc || (!sc->var && !sc->enum_ty))
|
2020-09-03 09:43:36 +03:00
|
|
|
error_tok(tok, "undefined variable");
|
2019-08-11 13:59:27 +03:00
|
|
|
|
|
|
|
Node *node;
|
|
|
|
if (sc->var)
|
|
|
|
node = new_var_node(sc->var, tok);
|
|
|
|
else
|
|
|
|
node = new_num(sc->enum_val, tok);
|
|
|
|
|
2020-09-26 02:59:56 +03:00
|
|
|
*rest = tok->next;
|
2019-08-11 13:59:27 +03:00
|
|
|
return node;
|
2020-09-26 02:59:56 +03:00
|
|
|
}
|
|
|
|
|
2020-10-07 06:49:08 +03:00
|
|
|
if (tok->kind == TK_STR) {
|
|
|
|
Obj *var = new_string_literal(tok->str, tok->ty);
|
|
|
|
*rest = tok->next;
|
|
|
|
return new_var_node(var, tok);
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:11:16 +03:00
|
|
|
if (tok->kind == TK_NUM) {
|
2020-09-26 05:23:04 +03:00
|
|
|
Node *node = new_num(tok->val, tok);
|
2020-10-07 14:11:16 +03:00
|
|
|
*rest = tok->next;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
error_tok(tok, "expected an expression");
|
|
|
|
}
|
|
|
|
|
2020-09-04 11:23:26 +03:00
|
|
|
static Token *parse_typedef(Token *tok, Type *basety) {
|
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
while (!consume(&tok, tok, ";")) {
|
|
|
|
if (!first)
|
|
|
|
tok = skip(tok, ",");
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
Type *ty = declarator(&tok, tok, basety);
|
|
|
|
push_scope(get_ident(ty->name))->type_def = ty;
|
|
|
|
}
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
2020-09-04 07:39:48 +03:00
|
|
|
static void create_param_lvars(Type *param) {
|
|
|
|
if (param) {
|
|
|
|
create_param_lvars(param->next);
|
|
|
|
new_lvar(get_ident(param->name), param);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-04 06:27:21 +03:00
|
|
|
// This function matches gotos with labels.
|
|
|
|
//
|
|
|
|
// We cannot resolve gotos as we parse a function because gotos
|
|
|
|
// can refer a label that appears later in the function.
|
|
|
|
// So, we need to do this after we parse the entire function.
|
|
|
|
static void resolve_goto_labels(void) {
|
|
|
|
for (Node *x = gotos; x; x = x->goto_next) {
|
|
|
|
for (Node *y = labels; y; y = y->goto_next) {
|
|
|
|
if (!strcmp(x->label, y->label)) {
|
|
|
|
x->unique_label = y->unique_label;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x->unique_label == NULL)
|
|
|
|
error_tok(x->tok->next, "use of undeclared label");
|
|
|
|
}
|
|
|
|
|
|
|
|
gotos = labels = NULL;
|
|
|
|
}
|
|
|
|
|
2020-09-04 11:25:15 +03:00
|
|
|
static Token *function(Token *tok, Type *basety, VarAttr *attr) {
|
2020-09-04 11:58:53 +03:00
|
|
|
Type *ty = declarator(&tok, tok, basety);
|
2020-09-04 13:01:33 +03:00
|
|
|
|
2020-09-04 11:58:53 +03:00
|
|
|
Obj *fn = new_gvar(get_ident(ty->name), ty);
|
|
|
|
fn->is_function = true;
|
2020-09-04 05:45:29 +03:00
|
|
|
fn->is_definition = !consume(&tok, tok, ";");
|
2020-09-04 11:25:15 +03:00
|
|
|
fn->is_static = attr->is_static;
|
2020-09-04 05:45:29 +03:00
|
|
|
|
|
|
|
if (!fn->is_definition)
|
|
|
|
return tok;
|
2020-09-04 13:01:33 +03:00
|
|
|
|
2020-04-18 03:27:47 +03:00
|
|
|
current_fn = fn;
|
2020-09-04 11:58:53 +03:00
|
|
|
locals = NULL;
|
2020-09-04 12:00:26 +03:00
|
|
|
enter_scope();
|
2020-09-04 07:39:48 +03:00
|
|
|
create_param_lvars(ty->params);
|
|
|
|
fn->params = locals;
|
2020-09-04 13:01:33 +03:00
|
|
|
|
2020-09-04 07:38:41 +03:00
|
|
|
tok = skip(tok, "{");
|
2020-09-04 11:58:53 +03:00
|
|
|
fn->body = compound_stmt(&tok, tok);
|
2020-09-04 13:01:33 +03:00
|
|
|
fn->locals = locals;
|
2020-09-04 12:00:26 +03:00
|
|
|
leave_scope();
|
2020-09-04 06:27:21 +03:00
|
|
|
resolve_goto_labels();
|
2020-09-04 11:58:53 +03:00
|
|
|
return tok;
|
2020-09-04 13:01:33 +03:00
|
|
|
}
|
|
|
|
|
2020-09-05 02:43:21 +03:00
|
|
|
static Token *global_variable(Token *tok, Type *basety) {
|
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
while (!consume(&tok, tok, ";")) {
|
|
|
|
if (!first)
|
|
|
|
tok = skip(tok, ",");
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
Type *ty = declarator(&tok, tok, basety);
|
|
|
|
new_gvar(get_ident(ty->name), ty);
|
|
|
|
}
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookahead tokens and returns true if a given token is a start
|
|
|
|
// of a function definition or declaration.
|
|
|
|
static bool is_function(Token *tok) {
|
|
|
|
if (equal(tok, ";"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Type dummy = {};
|
|
|
|
Type *ty = declarator(&tok, tok, &dummy);
|
|
|
|
return ty->kind == TY_FUNC;
|
|
|
|
}
|
|
|
|
|
2020-09-04 11:23:26 +03:00
|
|
|
// program = (typedef | function-definition | global-variable)*
|
2020-09-04 11:58:53 +03:00
|
|
|
Obj *parse(Token *tok) {
|
|
|
|
globals = NULL;
|
2020-10-07 14:12:19 +03:00
|
|
|
|
2020-09-04 11:58:53 +03:00
|
|
|
while (tok->kind != TK_EOF) {
|
2020-09-04 11:23:26 +03:00
|
|
|
VarAttr attr = {};
|
|
|
|
Type *basety = declspec(&tok, tok, &attr);
|
|
|
|
|
|
|
|
// Typedef
|
|
|
|
if (attr.is_typedef) {
|
|
|
|
tok = parse_typedef(tok, basety);
|
|
|
|
continue;
|
|
|
|
}
|
2020-09-05 02:43:21 +03:00
|
|
|
|
|
|
|
// Function
|
|
|
|
if (is_function(tok)) {
|
2020-09-04 11:25:15 +03:00
|
|
|
tok = function(tok, basety, &attr);
|
2020-09-05 02:43:21 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Global variable
|
|
|
|
tok = global_variable(tok, basety);
|
|
|
|
|
2020-09-04 11:58:53 +03:00
|
|
|
}
|
|
|
|
return globals;
|
2020-10-07 14:11:16 +03:00
|
|
|
}
|