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-10-07 05:13:22 +03:00
|
|
|
typedef struct {
|
2020-09-04 12:00:26 +03:00
|
|
|
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-10-07 05:13:22 +03:00
|
|
|
} VarScope;
|
2020-10-07 04:43:02 +03:00
|
|
|
|
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-10-07 05:13:22 +03:00
|
|
|
HashMap vars;
|
|
|
|
HashMap 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:27:22 +03:00
|
|
|
bool is_extern;
|
2020-09-04 06:54:07 +03:00
|
|
|
bool is_inline;
|
2020-09-07 04:02:10 +03:00
|
|
|
bool is_tls;
|
2020-09-04 05:20:55 +03:00
|
|
|
int align;
|
2020-09-04 11:23:26 +03:00
|
|
|
} VarAttr;
|
|
|
|
|
2019-08-18 06:01:02 +03:00
|
|
|
// This struct represents a variable initializer. Since initializers
|
|
|
|
// can be nested (e.g. `int x[2][2] = {{1, 2}, {3, 4}}`), this struct
|
|
|
|
// is a tree data structure.
|
|
|
|
typedef struct Initializer Initializer;
|
|
|
|
struct Initializer {
|
|
|
|
Initializer *next;
|
|
|
|
Type *ty;
|
|
|
|
Token *tok;
|
2020-09-04 08:39:22 +03:00
|
|
|
bool is_flexible;
|
2019-08-18 06:01:02 +03:00
|
|
|
|
|
|
|
// If it's not an aggregate type and has an initializer,
|
|
|
|
// `expr` has an initialization expression.
|
|
|
|
Node *expr;
|
|
|
|
|
|
|
|
// If it's an initializer for an aggregate type (e.g. array or struct),
|
|
|
|
// `children` has initializers for its children.
|
|
|
|
Initializer **children;
|
2020-08-15 11:01:54 +03:00
|
|
|
|
|
|
|
// Only one member can be initialized for a union.
|
|
|
|
// `mem` is used to clarify which member is initialized.
|
|
|
|
Member *mem;
|
2019-08-18 06:01:02 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// For local variable initializer.
|
|
|
|
typedef struct InitDesg InitDesg;
|
|
|
|
struct InitDesg {
|
|
|
|
InitDesg *next;
|
|
|
|
int idx;
|
2019-08-18 11:56:36 +03:00
|
|
|
Member *member;
|
2019-08-18 06:01:02 +03:00
|
|
|
Obj *var;
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
|
2020-08-27 15:59:19 +03:00
|
|
|
// Current "goto" and "continue" jump targets.
|
2019-08-15 07:48:41 +03:00
|
|
|
static char *brk_label;
|
2020-08-27 15:59:19 +03:00
|
|
|
static char *cont_label;
|
2019-08-15 07:48:41 +03:00
|
|
|
|
2019-08-15 10:43:24 +03:00
|
|
|
// Points to a node representing a switch if we are parsing
|
|
|
|
// a switch statement. Otherwise, NULL.
|
|
|
|
static Node *current_switch;
|
|
|
|
|
2020-09-04 08:44:12 +03:00
|
|
|
static Obj *builtin_alloca;
|
|
|
|
|
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);
|
2020-09-04 05:20:55 +03:00
|
|
|
static Type *typename(Token **rest, Token *tok);
|
2019-08-11 13:59:27 +03:00
|
|
|
static Type *enum_specifier(Token **rest, Token *tok);
|
2020-07-23 13:29:25 +03:00
|
|
|
static Type *typeof_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 05:20:55 +03:00
|
|
|
static Node *declaration(Token **rest, Token *tok, Type *basety, VarAttr *attr);
|
2020-09-18 08:12:32 +03:00
|
|
|
static void array_initializer2(Token **rest, Token *tok, Initializer *init, int i);
|
2020-10-06 07:09:15 +03:00
|
|
|
static void struct_initializer2(Token **rest, Token *tok, Initializer *init, Member *mem);
|
2019-08-18 10:34:39 +03:00
|
|
|
static void initializer2(Token **rest, Token *tok, Initializer *init);
|
2020-09-04 08:39:22 +03:00
|
|
|
static Initializer *initializer(Token **rest, Token *tok, Type *ty, Type **new_ty);
|
2019-08-18 06:01:02 +03:00
|
|
|
static Node *lvar_initializer(Token **rest, Token *tok, Obj *var);
|
2020-09-04 11:26:34 +03:00
|
|
|
static void gvar_initializer(Token **rest, Token *tok, Obj *var);
|
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-04 11:26:34 +03:00
|
|
|
static int64_t eval(Node *node);
|
2020-09-20 07:23:53 +03:00
|
|
|
static int64_t eval2(Node *node, char ***label);
|
|
|
|
static int64_t eval_rval(Node *node, char ***label);
|
2020-09-04 08:44:12 +03:00
|
|
|
static bool is_const_expr(Node *node);
|
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);
|
2020-05-01 13:30:17 +03:00
|
|
|
static double eval_double(Node *node);
|
2019-08-17 04:27:35 +03:00
|
|
|
static Node *conditional(Token **rest, Token *tok);
|
2020-10-07 14:22:11 +03:00
|
|
|
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);
|
2020-10-07 14:23:22 +03:00
|
|
|
static Node *shift(Token **rest, Token *tok);
|
2020-10-07 14:11:16 +03:00
|
|
|
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);
|
2020-07-19 11:19:50 +03:00
|
|
|
static Member *get_struct_member(Type *ty, 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-09-03 12:50:56 +03:00
|
|
|
static Node *funcall(Token **rest, Token *tok, Node *node);
|
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-09-04 12:08:32 +03:00
|
|
|
static bool is_function(Token *tok);
|
|
|
|
static Token *function(Token *tok, Type *basety, VarAttr *attr);
|
|
|
|
static Token *global_variable(Token *tok, Type *basety, VarAttr *attr);
|
2020-10-07 14:11:16 +03:00
|
|
|
|
2020-08-27 17:36:12 +03:00
|
|
|
static int align_down(int n, int align) {
|
|
|
|
return align_to(n - align + 1, align);
|
|
|
|
}
|
|
|
|
|
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-10-07 05:13:22 +03:00
|
|
|
for (Scope *sc = scope; sc; sc = sc->next) {
|
|
|
|
VarScope *sc2 = hashmap_get2(&sc->vars, tok->loc, tok->len);
|
|
|
|
if (sc2)
|
|
|
|
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) {
|
2020-10-07 05:13:22 +03:00
|
|
|
for (Scope *sc = scope; sc; sc = sc->next) {
|
|
|
|
Type *ty = hashmap_get2(&sc->tags, tok->loc, tok->len);
|
|
|
|
if (ty)
|
|
|
|
return ty;
|
|
|
|
}
|
2020-10-07 04:43:02 +03:00
|
|
|
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-03-24 16:18:59 +03:00
|
|
|
static Node *new_ulong(long val, Token *tok) {
|
|
|
|
Node *node = new_node(ND_NUM, tok);
|
|
|
|
node->val = val;
|
|
|
|
node->ty = ty_ulong;
|
|
|
|
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-03 16:27:13 +03:00
|
|
|
static Node *new_vla_ptr(Obj *var, Token *tok) {
|
|
|
|
Node *node = new_node(ND_VLA_PTR, tok);
|
|
|
|
node->var = var;
|
|
|
|
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));
|
2020-10-07 05:13:22 +03:00
|
|
|
hashmap_put(&scope->vars, name, sc);
|
2020-09-04 12:00:26 +03:00
|
|
|
return sc;
|
|
|
|
}
|
|
|
|
|
2020-09-04 08:39:22 +03:00
|
|
|
static Initializer *new_initializer(Type *ty, bool is_flexible) {
|
2019-08-18 06:01:02 +03:00
|
|
|
Initializer *init = calloc(1, sizeof(Initializer));
|
|
|
|
init->ty = ty;
|
|
|
|
|
|
|
|
if (ty->kind == TY_ARRAY) {
|
2020-09-04 08:39:22 +03:00
|
|
|
if (is_flexible && ty->size < 0) {
|
|
|
|
init->is_flexible = true;
|
|
|
|
return init;
|
|
|
|
}
|
|
|
|
|
2019-08-18 06:01:02 +03:00
|
|
|
init->children = calloc(ty->array_len, sizeof(Initializer *));
|
|
|
|
for (int i = 0; i < ty->array_len; i++)
|
2020-09-04 08:39:22 +03:00
|
|
|
init->children[i] = new_initializer(ty->base, false);
|
2019-08-18 11:56:36 +03:00
|
|
|
return init;
|
|
|
|
}
|
|
|
|
|
2020-07-20 17:23:27 +03:00
|
|
|
if (ty->kind == TY_STRUCT || ty->kind == TY_UNION) {
|
2019-08-18 11:56:36 +03:00
|
|
|
// Count the number of struct members.
|
|
|
|
int len = 0;
|
|
|
|
for (Member *mem = ty->members; mem; mem = mem->next)
|
|
|
|
len++;
|
|
|
|
|
|
|
|
init->children = calloc(len, sizeof(Initializer *));
|
|
|
|
|
2020-09-20 10:16:00 +03:00
|
|
|
for (Member *mem = ty->members; mem; mem = mem->next) {
|
|
|
|
if (is_flexible && ty->is_flexible && !mem->next) {
|
|
|
|
Initializer *child = calloc(1, sizeof(Initializer));
|
|
|
|
child->ty = mem->ty;
|
|
|
|
child->is_flexible = true;
|
|
|
|
init->children[mem->idx] = child;
|
|
|
|
} else {
|
|
|
|
init->children[mem->idx] = new_initializer(mem->ty, false);
|
|
|
|
}
|
|
|
|
}
|
2019-08-18 11:56:36 +03:00
|
|
|
return init;
|
2019-08-18 06:01:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return init;
|
|
|
|
}
|
|
|
|
|
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 05:20:55 +03:00
|
|
|
var->align = ty->align;
|
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;
|
2020-09-04 05:21:12 +03:00
|
|
|
var->is_static = true;
|
2020-09-04 11:27:22 +03:00
|
|
|
var->is_definition = true;
|
2020-09-04 11:58:53 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-07 04:43:02 +03:00
|
|
|
static void push_tag_scope(Token *tok, Type *ty) {
|
2020-10-07 05:13:22 +03:00
|
|
|
hashmap_put2(&scope->tags, tok->loc, tok->len, ty);
|
2020-10-07 04:43:02 +03:00
|
|
|
}
|
|
|
|
|
2020-08-28 16:07:54 +03:00
|
|
|
// declspec = ("void" | "_Bool" | "char" | "short" | "int" | "long"
|
2020-09-04 06:54:07 +03:00
|
|
|
// | "typedef" | "static" | "extern" | "inline"
|
2020-09-07 04:02:10 +03:00
|
|
|
// | "_Thread_local" | "__thread"
|
2020-08-28 17:29:49 +03:00
|
|
|
// | "signed" | "unsigned"
|
2019-08-11 13:59:27 +03:00
|
|
|
// | struct-decl | union-decl | typedef-name
|
2020-07-23 13:29:25 +03:00
|
|
|
// | enum-specifier | typeof-specifier
|
2020-09-02 19:22:17 +03:00
|
|
|
// | "const" | "volatile" | "auto" | "register" | "restrict"
|
|
|
|
// | "__restrict" | "__restrict__" | "_Noreturn")+
|
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 {
|
2020-08-28 17:29:49 +03:00
|
|
|
VOID = 1 << 0,
|
|
|
|
BOOL = 1 << 2,
|
|
|
|
CHAR = 1 << 4,
|
|
|
|
SHORT = 1 << 6,
|
|
|
|
INT = 1 << 8,
|
|
|
|
LONG = 1 << 10,
|
2020-09-12 08:47:47 +03:00
|
|
|
FLOAT = 1 << 12,
|
|
|
|
DOUBLE = 1 << 14,
|
|
|
|
OTHER = 1 << 16,
|
|
|
|
SIGNED = 1 << 17,
|
|
|
|
UNSIGNED = 1 << 18,
|
2019-08-11 08:12:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
Type *ty = ty_int;
|
|
|
|
int counter = 0;
|
2020-09-15 05:30:56 +03:00
|
|
|
bool is_atomic = false;
|
2019-08-11 08:12:01 +03:00
|
|
|
|
|
|
|
while (is_typename(tok)) {
|
2020-09-04 11:25:15 +03:00
|
|
|
// Handle storage class specifiers.
|
2020-09-04 06:54:07 +03:00
|
|
|
if (equal(tok, "typedef") || equal(tok, "static") || equal(tok, "extern") ||
|
2020-09-07 04:02:10 +03:00
|
|
|
equal(tok, "inline") || equal(tok, "_Thread_local") || equal(tok, "__thread")) {
|
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;
|
2020-09-04 11:27:22 +03:00
|
|
|
else if (equal(tok, "static"))
|
2020-09-04 11:25:15 +03:00
|
|
|
attr->is_static = true;
|
2020-09-04 06:54:07 +03:00
|
|
|
else if (equal(tok, "extern"))
|
2020-09-04 11:27:22 +03:00
|
|
|
attr->is_extern = true;
|
2020-09-07 04:02:10 +03:00
|
|
|
else if (equal(tok, "inline"))
|
2020-09-04 06:54:07 +03:00
|
|
|
attr->is_inline = true;
|
2020-09-07 04:02:10 +03:00
|
|
|
else
|
|
|
|
attr->is_tls = true;
|
2020-09-04 11:25:15 +03:00
|
|
|
|
2020-09-07 04:02:10 +03:00
|
|
|
if (attr->is_typedef &&
|
|
|
|
attr->is_static + attr->is_extern + attr->is_inline + attr->is_tls > 1)
|
|
|
|
error_tok(tok, "typedef may not be used together with static,"
|
|
|
|
" extern, inline, __thread or _Thread_local");
|
2020-09-04 11:23:26 +03:00
|
|
|
tok = tok->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-09-02 19:22:17 +03:00
|
|
|
// These keywords are recognized but ignored.
|
|
|
|
if (consume(&tok, tok, "const") || consume(&tok, tok, "volatile") ||
|
|
|
|
consume(&tok, tok, "auto") || consume(&tok, tok, "register") ||
|
|
|
|
consume(&tok, tok, "restrict") || consume(&tok, tok, "__restrict") ||
|
|
|
|
consume(&tok, tok, "__restrict__") || consume(&tok, tok, "_Noreturn"))
|
|
|
|
continue;
|
|
|
|
|
2020-09-15 05:30:56 +03:00
|
|
|
if (equal(tok, "_Atomic")) {
|
|
|
|
tok = tok->next;
|
|
|
|
if (equal(tok , "(")) {
|
|
|
|
ty = typename(&tok, tok->next);
|
|
|
|
tok = skip(tok, ")");
|
|
|
|
}
|
|
|
|
is_atomic = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-09-04 05:20:55 +03:00
|
|
|
if (equal(tok, "_Alignas")) {
|
|
|
|
if (!attr)
|
|
|
|
error_tok(tok, "_Alignas is not allowed in this context");
|
|
|
|
tok = skip(tok->next, "(");
|
|
|
|
|
|
|
|
if (is_typename(tok))
|
|
|
|
attr->align = typename(&tok, tok)->align;
|
|
|
|
else
|
|
|
|
attr->align = const_expr(&tok, tok);
|
|
|
|
tok = skip(tok, ")");
|
|
|
|
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);
|
2020-07-23 13:29:25 +03:00
|
|
|
if (equal(tok, "struct") || equal(tok, "union") || equal(tok, "enum") ||
|
|
|
|
equal(tok, "typeof") || 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-07-23 13:29:25 +03:00
|
|
|
} else if (equal(tok, "typeof")) {
|
|
|
|
ty = typeof_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;
|
2020-09-12 08:47:47 +03:00
|
|
|
else if (equal(tok, "float"))
|
|
|
|
counter += FLOAT;
|
|
|
|
else if (equal(tok, "double"))
|
|
|
|
counter += DOUBLE;
|
2020-10-06 11:08:59 +03:00
|
|
|
else if (equal(tok, "signed"))
|
|
|
|
counter |= SIGNED;
|
2020-08-28 17:29:49 +03:00
|
|
|
else if (equal(tok, "unsigned"))
|
|
|
|
counter |= UNSIGNED;
|
2019-08-11 08:12:01 +03:00
|
|
|
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:
|
2020-10-06 11:08:59 +03:00
|
|
|
case SIGNED + CHAR:
|
2019-08-11 08:12:01 +03:00
|
|
|
ty = ty_char;
|
|
|
|
break;
|
2020-08-28 17:29:49 +03:00
|
|
|
case UNSIGNED + CHAR:
|
|
|
|
ty = ty_uchar;
|
|
|
|
break;
|
2019-08-11 08:12:01 +03:00
|
|
|
case SHORT:
|
|
|
|
case SHORT + INT:
|
2020-10-06 11:08:59 +03:00
|
|
|
case SIGNED + SHORT:
|
|
|
|
case SIGNED + SHORT + INT:
|
2019-08-11 08:12:01 +03:00
|
|
|
ty = ty_short;
|
|
|
|
break;
|
2020-08-28 17:29:49 +03:00
|
|
|
case UNSIGNED + SHORT:
|
|
|
|
case UNSIGNED + SHORT + INT:
|
|
|
|
ty = ty_ushort;
|
|
|
|
break;
|
2019-08-11 08:12:01 +03:00
|
|
|
case INT:
|
2020-10-06 11:08:59 +03:00
|
|
|
case SIGNED:
|
|
|
|
case SIGNED + INT:
|
2019-08-11 08:12:01 +03:00
|
|
|
ty = ty_int;
|
|
|
|
break;
|
2020-08-28 17:29:49 +03:00
|
|
|
case UNSIGNED:
|
|
|
|
case UNSIGNED + INT:
|
|
|
|
ty = ty_uint;
|
|
|
|
break;
|
2019-08-11 08:12:01 +03:00
|
|
|
case LONG:
|
|
|
|
case LONG + INT:
|
2020-04-20 15:53:23 +03:00
|
|
|
case LONG + LONG:
|
|
|
|
case LONG + LONG + INT:
|
2020-10-06 11:08:59 +03:00
|
|
|
case SIGNED + LONG:
|
|
|
|
case SIGNED + LONG + INT:
|
|
|
|
case SIGNED + LONG + LONG:
|
|
|
|
case SIGNED + LONG + LONG + INT:
|
2019-08-11 08:12:01 +03:00
|
|
|
ty = ty_long;
|
|
|
|
break;
|
2020-08-28 17:29:49 +03:00
|
|
|
case UNSIGNED + LONG:
|
|
|
|
case UNSIGNED + LONG + INT:
|
|
|
|
case UNSIGNED + LONG + LONG:
|
|
|
|
case UNSIGNED + LONG + LONG + INT:
|
|
|
|
ty = ty_ulong;
|
|
|
|
break;
|
2020-09-12 08:47:47 +03:00
|
|
|
case FLOAT:
|
|
|
|
ty = ty_float;
|
|
|
|
break;
|
|
|
|
case DOUBLE:
|
|
|
|
ty = ty_double;
|
|
|
|
break;
|
2020-08-29 04:37:53 +03:00
|
|
|
case LONG + DOUBLE:
|
|
|
|
ty = ty_ldouble;
|
|
|
|
break;
|
2019-08-11 08:12:01 +03:00
|
|
|
default:
|
|
|
|
error_tok(tok, "invalid type");
|
|
|
|
}
|
|
|
|
|
|
|
|
tok = tok->next;
|
2020-09-06 02:10:01 +03:00
|
|
|
}
|
|
|
|
|
2020-09-15 05:30:56 +03:00
|
|
|
if (is_atomic) {
|
|
|
|
ty = copy_type(ty);
|
|
|
|
ty->is_atomic = true;
|
|
|
|
}
|
|
|
|
|
2019-08-11 08:12:01 +03:00
|
|
|
*rest = tok;
|
|
|
|
return ty;
|
2020-09-03 09:43:36 +03:00
|
|
|
}
|
|
|
|
|
2020-10-07 14:24:13 +03:00
|
|
|
// func-params = ("void" | 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) {
|
2019-08-20 02:30:43 +03:00
|
|
|
if (equal(tok, "void") && equal(tok->next, ")")) {
|
|
|
|
*rest = tok->next->next;
|
|
|
|
return func_type(ty);
|
|
|
|
}
|
|
|
|
|
2020-09-26 04:15:32 +03:00
|
|
|
Type head = {};
|
|
|
|
Type *cur = &head;
|
2020-10-07 14:24:13 +03:00
|
|
|
bool is_variadic = false;
|
2020-09-26 04:15:32 +03:00
|
|
|
|
|
|
|
while (!equal(tok, ")")) {
|
|
|
|
if (cur != &head)
|
|
|
|
tok = skip(tok, ",");
|
2019-08-14 05:00:05 +03:00
|
|
|
|
2020-10-07 14:24:13 +03:00
|
|
|
if (equal(tok, "...")) {
|
|
|
|
is_variadic = true;
|
|
|
|
tok = tok->next;
|
|
|
|
skip(tok, ")");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-08-14 05:00:05 +03:00
|
|
|
Type *ty2 = declspec(&tok, tok, NULL);
|
|
|
|
ty2 = declarator(&tok, tok, ty2);
|
|
|
|
|
2020-08-31 15:28:53 +03:00
|
|
|
Token *name = ty2->name;
|
|
|
|
|
2019-08-14 05:00:05 +03:00
|
|
|
if (ty2->kind == TY_ARRAY) {
|
2020-08-31 15:28:53 +03:00
|
|
|
// "array of T" is converted to "pointer to T" only in the parameter
|
|
|
|
// context. For example, *argv[] is converted to **argv by this.
|
2019-08-14 05:00:05 +03:00
|
|
|
ty2 = pointer_to(ty2->base);
|
|
|
|
ty2->name = name;
|
2020-08-31 15:28:53 +03:00
|
|
|
} else if (ty2->kind == TY_FUNC) {
|
|
|
|
// Likewise, a function is converted to a pointer to a function
|
|
|
|
// only in the parameter context.
|
|
|
|
ty2 = pointer_to(ty2);
|
|
|
|
ty2->name = name;
|
2019-08-14 05:00:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cur = cur->next = copy_type(ty2);
|
2020-09-26 04:15:32 +03:00
|
|
|
}
|
|
|
|
|
2020-09-26 08:29:06 +03:00
|
|
|
if (cur == &head)
|
|
|
|
is_variadic = true;
|
|
|
|
|
2020-09-26 04:15:32 +03:00
|
|
|
ty = func_type(ty);
|
|
|
|
ty->params = head.next;
|
2020-10-07 14:24:13 +03:00
|
|
|
ty->is_variadic = is_variadic;
|
2020-09-26 04:15:32 +03:00
|
|
|
*rest = tok->next;
|
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
|
2020-08-31 03:58:42 +03:00
|
|
|
// array-dimensions = ("static" | "restrict")* const-expr? "]" type-suffix
|
2019-08-13 15:03:20 +03:00
|
|
|
static Type *array_dimensions(Token **rest, Token *tok, Type *ty) {
|
2020-08-31 03:58:42 +03:00
|
|
|
while (equal(tok, "static") || equal(tok, "restrict"))
|
|
|
|
tok = tok->next;
|
|
|
|
|
2019-08-13 15:03:20 +03:00
|
|
|
if (equal(tok, "]")) {
|
|
|
|
ty = type_suffix(rest, tok->next, ty);
|
|
|
|
return array_of(ty, -1);
|
|
|
|
}
|
|
|
|
|
2020-09-04 08:44:12 +03:00
|
|
|
Node *expr = conditional(&tok, tok);
|
2019-08-17 04:38:20 +03:00
|
|
|
tok = skip(tok, "]");
|
2019-08-13 15:03:20 +03:00
|
|
|
ty = type_suffix(rest, tok, ty);
|
2020-09-04 08:44:12 +03:00
|
|
|
|
|
|
|
if (ty->kind == TY_VLA || !is_const_expr(expr))
|
|
|
|
return vla_of(ty, expr);
|
|
|
|
return array_of(ty, eval(expr));
|
2019-08-13 15:03:20 +03:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-02 19:22:17 +03:00
|
|
|
// pointers = ("*" ("const" | "volatile" | "restrict")*)*
|
|
|
|
static Type *pointers(Token **rest, Token *tok, Type *ty) {
|
|
|
|
while (consume(&tok, tok, "*")) {
|
2020-09-03 09:43:36 +03:00
|
|
|
ty = pointer_to(ty);
|
2020-09-02 19:22:17 +03:00
|
|
|
while (equal(tok, "const") || equal(tok, "volatile") || equal(tok, "restrict") ||
|
|
|
|
equal(tok, "__restrict") || equal(tok, "__restrict__"))
|
|
|
|
tok = tok->next;
|
|
|
|
}
|
|
|
|
*rest = tok;
|
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
|
|
|
|
// declarator = pointers ("(" ident ")" | "(" declarator ")" | ident) type-suffix
|
|
|
|
static Type *declarator(Token **rest, Token *tok, Type *ty) {
|
|
|
|
ty = pointers(&tok, tok, ty);
|
2020-09-03 09:43:36 +03:00
|
|
|
|
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-04 11:28:20 +03:00
|
|
|
Token *name = NULL;
|
|
|
|
Token *name_pos = tok;
|
|
|
|
|
|
|
|
if (tok->kind == TK_IDENT) {
|
|
|
|
name = tok;
|
|
|
|
tok = tok->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
ty = type_suffix(rest, tok, ty);
|
|
|
|
ty->name = name;
|
|
|
|
ty->name_pos = name_pos;
|
2020-09-03 09:43:36 +03:00
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
|
2020-09-02 19:22:17 +03:00
|
|
|
// abstract-declarator = pointers ("(" abstract-declarator ")")? type-suffix
|
2019-08-11 09:04:35 +03:00
|
|
|
static Type *abstract_declarator(Token **rest, Token *tok, Type *ty) {
|
2020-09-02 19:22:17 +03:00
|
|
|
ty = pointers(&tok, tok, ty);
|
2019-08-11 09:04:35 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-10-06 07:08:41 +03:00
|
|
|
static bool is_end(Token *tok) {
|
|
|
|
return equal(tok, "}") || (equal(tok, ",") && equal(tok->next, "}"));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool consume_end(Token **rest, Token *tok) {
|
|
|
|
if (equal(tok, "}")) {
|
|
|
|
*rest = tok->next;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(tok, ",") && equal(tok->next, "}")) {
|
|
|
|
*rest = tok->next->next;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-11 13:59:27 +03:00
|
|
|
// enum-specifier = ident? "{" enum-list? "}"
|
|
|
|
// | ident ("{" enum-list? "}")?
|
|
|
|
//
|
2020-10-06 07:08:41 +03:00
|
|
|
// enum-list = ident ("=" num)? ("," ident ("=" num)?)* ","?
|
2019-08-11 13:59:27 +03:00
|
|
|
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;
|
2020-10-06 07:08:41 +03:00
|
|
|
while (!consume_end(rest, tok)) {
|
2019-08-11 13:59:27 +03:00
|
|
|
if (i++ > 0)
|
|
|
|
tok = skip(tok, ",");
|
|
|
|
|
|
|
|
char *name = get_ident(tok);
|
|
|
|
tok = tok->next;
|
|
|
|
|
2019-08-17 04:38:20 +03:00
|
|
|
if (equal(tok, "="))
|
|
|
|
val = const_expr(&tok, tok->next);
|
2019-08-11 13:59:27 +03:00
|
|
|
|
|
|
|
VarScope *sc = push_scope(name);
|
|
|
|
sc->enum_ty = ty;
|
|
|
|
sc->enum_val = val++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag)
|
|
|
|
push_tag_scope(tag, ty);
|
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
|
2020-07-23 13:29:25 +03:00
|
|
|
// typeof-specifier = "(" (expr | typename) ")"
|
|
|
|
static Type *typeof_specifier(Token **rest, Token *tok) {
|
|
|
|
tok = skip(tok, "(");
|
|
|
|
|
|
|
|
Type *ty;
|
|
|
|
if (is_typename(tok)) {
|
|
|
|
ty = typename(&tok, tok);
|
|
|
|
} else {
|
|
|
|
Node *node = expr(&tok, tok);
|
|
|
|
add_type(node);
|
|
|
|
ty = node->ty;
|
|
|
|
}
|
|
|
|
*rest = skip(tok, ")");
|
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
|
2020-09-04 08:44:12 +03:00
|
|
|
// Generate code for computing a VLA size.
|
|
|
|
static Node *compute_vla_size(Type *ty, Token *tok) {
|
|
|
|
Node *node = new_node(ND_NULL_EXPR, tok);
|
|
|
|
if (ty->base)
|
|
|
|
node = new_binary(ND_COMMA, node, compute_vla_size(ty->base, tok), tok);
|
|
|
|
|
|
|
|
if (ty->kind != TY_VLA)
|
|
|
|
return node;
|
|
|
|
|
|
|
|
Node *base_sz;
|
|
|
|
if (ty->base->kind == TY_VLA)
|
|
|
|
base_sz = new_var_node(ty->base->vla_size, tok);
|
|
|
|
else
|
|
|
|
base_sz = new_num(ty->base->size, tok);
|
|
|
|
|
|
|
|
ty->vla_size = new_lvar("", ty_ulong);
|
|
|
|
Node *expr = new_binary(ND_ASSIGN, new_var_node(ty->vla_size, tok),
|
|
|
|
new_binary(ND_MUL, ty->vla_len, base_sz, tok),
|
|
|
|
tok);
|
|
|
|
return new_binary(ND_COMMA, node, expr, tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Node *new_alloca(Node *sz) {
|
|
|
|
Node *node = new_unary(ND_FUNCALL, new_var_node(builtin_alloca, sz->tok), sz->tok);
|
|
|
|
node->func_ty = builtin_alloca->ty;
|
|
|
|
node->ty = builtin_alloca->ty->return_ty;
|
|
|
|
node->args = sz;
|
|
|
|
add_type(sz);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-09-03 09:43:36 +03:00
|
|
|
// declaration = declspec (declarator ("=" expr)? ("," declarator ("=" expr)?)*)? ";"
|
2020-09-04 05:20:55 +03:00
|
|
|
static Node *declaration(Token **rest, Token *tok, Type *basety, VarAttr *attr) {
|
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-11 04:18:55 +03:00
|
|
|
if (ty->kind == TY_VOID)
|
|
|
|
error_tok(tok, "variable declared void");
|
2020-09-04 11:28:20 +03:00
|
|
|
if (!ty->name)
|
|
|
|
error_tok(ty->name_pos, "variable name omitted");
|
2019-08-11 04:18:55 +03:00
|
|
|
|
2020-09-04 08:41:55 +03:00
|
|
|
if (attr && attr->is_static) {
|
|
|
|
// static local variable
|
|
|
|
Obj *var = new_anon_gvar(ty);
|
|
|
|
push_scope(get_ident(ty->name))->var = var;
|
|
|
|
if (equal(tok, "="))
|
|
|
|
gvar_initializer(&tok, tok->next, var);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-09-04 08:44:12 +03:00
|
|
|
// Generate code for computing a VLA size. We need to do this
|
|
|
|
// even if ty is not VLA because ty may be a pointer to VLA
|
|
|
|
// (e.g. int (*foo)[n][m] where n and m are variables.)
|
|
|
|
cur = cur->next = new_unary(ND_EXPR_STMT, compute_vla_size(ty, tok), tok);
|
|
|
|
|
|
|
|
if (ty->kind == TY_VLA) {
|
|
|
|
if (equal(tok, "="))
|
|
|
|
error_tok(tok, "variable-sized object may not be initialized");
|
|
|
|
|
|
|
|
// Variable length arrays (VLAs) are translated to alloca() calls.
|
|
|
|
// For example, `int x[n+2]` is translated to `tmp = n + 2,
|
|
|
|
// x = alloca(tmp)`.
|
|
|
|
Obj *var = new_lvar(get_ident(ty->name), ty);
|
|
|
|
Token *tok = ty->name;
|
2020-09-03 16:27:13 +03:00
|
|
|
Node *expr = new_binary(ND_ASSIGN, new_vla_ptr(var, tok),
|
2020-09-04 08:44:12 +03:00
|
|
|
new_alloca(new_var_node(ty->vla_size, tok)),
|
|
|
|
tok);
|
|
|
|
|
|
|
|
cur = cur->next = new_unary(ND_EXPR_STMT, expr, tok);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-09-03 09:43:36 +03:00
|
|
|
Obj *var = new_lvar(get_ident(ty->name), ty);
|
2020-09-04 05:20:55 +03:00
|
|
|
if (attr && attr->align)
|
|
|
|
var->align = attr->align;
|
|
|
|
|
2019-08-18 06:01:02 +03:00
|
|
|
if (equal(tok, "=")) {
|
|
|
|
Node *expr = lvar_initializer(&tok, tok->next, var);
|
|
|
|
cur = cur->next = new_unary(ND_EXPR_STMT, expr, tok);
|
|
|
|
}
|
2020-09-04 08:39:22 +03:00
|
|
|
|
|
|
|
if (var->ty->size < 0)
|
|
|
|
error_tok(ty->name, "variable has incomplete type");
|
|
|
|
if (var->ty->kind == TY_VOID)
|
|
|
|
error_tok(ty->name, "variable declared void");
|
2020-09-03 09:43:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Node *node = new_node(ND_BLOCK, tok);
|
|
|
|
node->body = head.next;
|
|
|
|
*rest = tok->next;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-04-16 17:34:05 +03:00
|
|
|
static Token *skip_excess_element(Token *tok) {
|
|
|
|
if (equal(tok, "{")) {
|
|
|
|
tok = skip_excess_element(tok->next);
|
|
|
|
return skip(tok, "}");
|
|
|
|
}
|
|
|
|
|
|
|
|
assign(&tok, tok);
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
2019-08-18 10:34:39 +03:00
|
|
|
// string-initializer = string-literal
|
|
|
|
static void string_initializer(Token **rest, Token *tok, Initializer *init) {
|
2020-09-04 08:39:22 +03:00
|
|
|
if (init->is_flexible)
|
|
|
|
*init = *new_initializer(array_of(init->ty->base, tok->ty->array_len), false);
|
|
|
|
|
2019-08-18 10:34:39 +03:00
|
|
|
int len = MIN(init->ty->array_len, tok->ty->array_len);
|
2020-07-07 08:04:36 +03:00
|
|
|
|
|
|
|
switch (init->ty->base->size) {
|
|
|
|
case 1: {
|
|
|
|
char *str = tok->str;
|
|
|
|
for (int i = 0; i < len; i++)
|
|
|
|
init->children[i]->expr = new_num(str[i], tok);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2: {
|
|
|
|
uint16_t *str = (uint16_t *)tok->str;
|
|
|
|
for (int i = 0; i < len; i++)
|
|
|
|
init->children[i]->expr = new_num(str[i], tok);
|
|
|
|
break;
|
|
|
|
}
|
2020-07-07 08:04:36 +03:00
|
|
|
case 4: {
|
|
|
|
uint32_t *str = (uint32_t *)tok->str;
|
|
|
|
for (int i = 0; i < len; i++)
|
|
|
|
init->children[i]->expr = new_num(str[i], tok);
|
|
|
|
break;
|
|
|
|
}
|
2020-07-07 08:04:36 +03:00
|
|
|
default:
|
|
|
|
unreachable();
|
|
|
|
}
|
|
|
|
|
2019-08-18 10:34:39 +03:00
|
|
|
*rest = tok->next;
|
|
|
|
}
|
2019-08-18 06:01:02 +03:00
|
|
|
|
2020-09-18 08:12:32 +03:00
|
|
|
// array-designator = "[" const-expr "]"
|
|
|
|
//
|
|
|
|
// C99 added the designated initializer to the language, which allows
|
|
|
|
// programmers to move the "cursor" of an initializer to any element.
|
|
|
|
// The syntax looks like this:
|
|
|
|
//
|
|
|
|
// int x[10] = { 1, 2, [5]=3, 4, 5, 6, 7 };
|
|
|
|
//
|
|
|
|
// `[5]` moves the cursor to the 5th element, so the 5th element of x
|
|
|
|
// is set to 3. Initialization then continues forward in order, so
|
|
|
|
// 6th, 7th, 8th and 9th elements are initialized with 4, 5, 6 and 7,
|
|
|
|
// respectively. Unspecified elements (in this case, 3rd and 4th
|
|
|
|
// elements) are initialized with zero.
|
|
|
|
//
|
|
|
|
// Nesting is allowed, so the following initializer is valid:
|
|
|
|
//
|
|
|
|
// int x[5][10] = { [5][8]=1, 2, 3 };
|
|
|
|
//
|
|
|
|
// It sets x[5][8], x[5][9] and x[6][0] to 1, 2 and 3, respectively.
|
2020-10-06 07:09:15 +03:00
|
|
|
//
|
|
|
|
// Use `.fieldname` to move the cursor for a struct initializer. E.g.
|
|
|
|
//
|
|
|
|
// struct { int a, b, c; } x = { .c=5 };
|
|
|
|
//
|
|
|
|
// The above initializer sets x.c to 5.
|
2020-09-20 09:39:38 +03:00
|
|
|
static void array_designator(Token **rest, Token *tok, Type *ty, int *begin, int *end) {
|
|
|
|
*begin = const_expr(&tok, tok->next);
|
|
|
|
if (*begin >= ty->array_len)
|
|
|
|
error_tok(tok, "array designator index exceeds array bounds");
|
|
|
|
|
|
|
|
if (equal(tok, "...")) {
|
|
|
|
*end = const_expr(&tok, tok->next);
|
|
|
|
if (*end >= ty->array_len)
|
|
|
|
error_tok(tok, "array designator index exceeds array bounds");
|
|
|
|
if (*end < *begin)
|
|
|
|
error_tok(tok, "array designator range [%d, %d] is empty", *begin, *end);
|
|
|
|
} else {
|
|
|
|
*end = *begin;
|
|
|
|
}
|
|
|
|
|
2020-09-18 08:12:32 +03:00
|
|
|
*rest = skip(tok, "]");
|
|
|
|
}
|
|
|
|
|
2020-10-06 07:09:15 +03:00
|
|
|
// struct-designator = "." ident
|
|
|
|
static Member *struct_designator(Token **rest, Token *tok, Type *ty) {
|
2020-07-19 11:19:50 +03:00
|
|
|
Token *start = tok;
|
2020-10-06 07:09:15 +03:00
|
|
|
tok = skip(tok, ".");
|
|
|
|
if (tok->kind != TK_IDENT)
|
|
|
|
error_tok(tok, "expected a field designator");
|
|
|
|
|
|
|
|
for (Member *mem = ty->members; mem; mem = mem->next) {
|
2020-07-19 11:19:50 +03:00
|
|
|
// Anonymous struct member
|
|
|
|
if (mem->ty->kind == TY_STRUCT && !mem->name) {
|
|
|
|
if (get_struct_member(mem->ty, tok)) {
|
|
|
|
*rest = start;
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regular struct member
|
2020-10-06 07:09:15 +03:00
|
|
|
if (mem->name->len == tok->len && !strncmp(mem->name->loc, tok->loc, tok->len)) {
|
|
|
|
*rest = tok->next;
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
error_tok(tok, "struct has no such member");
|
|
|
|
}
|
|
|
|
|
|
|
|
// designation = ("[" const-expr "]" | "." ident)* "="? initializer
|
2020-09-18 08:12:32 +03:00
|
|
|
static void designation(Token **rest, Token *tok, Initializer *init) {
|
|
|
|
if (equal(tok, "[")) {
|
|
|
|
if (init->ty->kind != TY_ARRAY)
|
|
|
|
error_tok(tok, "array index in non-array initializer");
|
2020-09-20 09:39:38 +03:00
|
|
|
|
|
|
|
int begin, end;
|
|
|
|
array_designator(&tok, tok, init->ty, &begin, &end);
|
|
|
|
|
|
|
|
Token *tok2;
|
|
|
|
for (int i = begin; i <= end; i++)
|
|
|
|
designation(&tok2, tok, init->children[i]);
|
|
|
|
array_initializer2(rest, tok2, init, begin + 1);
|
2020-09-18 08:12:32 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-06 07:09:15 +03:00
|
|
|
if (equal(tok, ".") && init->ty->kind == TY_STRUCT) {
|
|
|
|
Member *mem = struct_designator(&tok, tok, init->ty);
|
|
|
|
designation(&tok, tok, init->children[mem->idx]);
|
|
|
|
init->expr = NULL;
|
|
|
|
struct_initializer2(rest, tok, init, mem->next);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-15 11:01:54 +03:00
|
|
|
if (equal(tok, ".") && init->ty->kind == TY_UNION) {
|
|
|
|
Member *mem = struct_designator(&tok, tok, init->ty);
|
|
|
|
init->mem = mem;
|
|
|
|
designation(rest, tok, init->children[mem->idx]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-06 07:09:15 +03:00
|
|
|
if (equal(tok, "."))
|
|
|
|
error_tok(tok, "field name not in struct or union initializer");
|
|
|
|
|
2020-07-23 19:51:45 +03:00
|
|
|
if (equal(tok, "="))
|
|
|
|
tok = tok->next;
|
2020-09-18 08:12:32 +03:00
|
|
|
initializer2(rest, tok, init);
|
|
|
|
}
|
|
|
|
|
2020-09-20 10:07:43 +03:00
|
|
|
// An array length can be omitted if an array has an initializer
|
|
|
|
// (e.g. `int x[] = {1,2,3}`). If it's omitted, count the number
|
|
|
|
// of initializer elements.
|
2020-09-04 08:39:22 +03:00
|
|
|
static int count_array_init_elements(Token *tok, Type *ty) {
|
2020-09-20 10:07:43 +03:00
|
|
|
bool first = true;
|
|
|
|
Initializer *dummy = new_initializer(ty->base, true);
|
2020-09-04 08:39:22 +03:00
|
|
|
|
2020-09-20 10:07:43 +03:00
|
|
|
int i = 0, max = 0;
|
|
|
|
|
|
|
|
while (!consume_end(&tok, tok)) {
|
|
|
|
if (!first)
|
2020-09-04 08:39:22 +03:00
|
|
|
tok = skip(tok, ",");
|
2020-09-20 10:07:43 +03:00
|
|
|
first = false;
|
|
|
|
|
|
|
|
if (equal(tok, "[")) {
|
|
|
|
i = const_expr(&tok, tok->next);
|
|
|
|
if (equal(tok, "..."))
|
|
|
|
i = const_expr(&tok, tok->next);
|
|
|
|
tok = skip(tok, "]");
|
|
|
|
designation(&tok, tok, dummy);
|
|
|
|
} else {
|
|
|
|
initializer2(&tok, tok, dummy);
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
max = MAX(max, i);
|
2020-09-04 08:39:22 +03:00
|
|
|
}
|
2020-09-20 10:07:43 +03:00
|
|
|
return max;
|
2020-09-04 08:39:22 +03:00
|
|
|
}
|
|
|
|
|
2020-10-06 07:08:41 +03:00
|
|
|
// array-initializer1 = "{" initializer ("," initializer)* ","? "}"
|
2019-08-19 00:53:50 +03:00
|
|
|
static void array_initializer1(Token **rest, Token *tok, Initializer *init) {
|
2019-08-18 10:34:39 +03:00
|
|
|
tok = skip(tok, "{");
|
2020-09-20 10:07:43 +03:00
|
|
|
|
|
|
|
if (init->is_flexible) {
|
|
|
|
int len = count_array_init_elements(tok, init->ty);
|
|
|
|
*init = *new_initializer(array_of(init->ty->base, len), false);
|
|
|
|
}
|
|
|
|
|
2020-09-18 08:12:32 +03:00
|
|
|
bool first = true;
|
2020-04-16 17:34:05 +03:00
|
|
|
|
2020-09-04 08:39:22 +03:00
|
|
|
if (init->is_flexible) {
|
|
|
|
int len = count_array_init_elements(tok, init->ty);
|
|
|
|
*init = *new_initializer(array_of(init->ty->base, len), false);
|
|
|
|
}
|
|
|
|
|
2020-10-06 07:08:41 +03:00
|
|
|
for (int i = 0; !consume_end(rest, tok); i++) {
|
2020-09-18 08:12:32 +03:00
|
|
|
if (!first)
|
2019-08-18 10:34:39 +03:00
|
|
|
tok = skip(tok, ",");
|
2020-09-18 08:12:32 +03:00
|
|
|
first = false;
|
|
|
|
|
|
|
|
if (equal(tok, "[")) {
|
2020-09-20 09:39:38 +03:00
|
|
|
int begin, end;
|
|
|
|
array_designator(&tok, tok, init->ty, &begin, &end);
|
|
|
|
|
|
|
|
Token *tok2;
|
|
|
|
for (int j = begin; j <= end; j++)
|
|
|
|
designation(&tok2, tok, init->children[j]);
|
|
|
|
tok = tok2;
|
|
|
|
i = end;
|
2020-09-18 08:12:32 +03:00
|
|
|
continue;
|
|
|
|
}
|
2019-08-18 10:34:39 +03:00
|
|
|
|
|
|
|
if (i < init->ty->array_len)
|
|
|
|
initializer2(&tok, tok, init->children[i]);
|
|
|
|
else
|
|
|
|
tok = skip_excess_element(tok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 00:53:50 +03:00
|
|
|
// array-initializer2 = initializer ("," initializer)*
|
2020-09-18 08:12:32 +03:00
|
|
|
static void array_initializer2(Token **rest, Token *tok, Initializer *init, int i) {
|
2019-08-19 00:53:50 +03:00
|
|
|
if (init->is_flexible) {
|
|
|
|
int len = count_array_init_elements(tok, init->ty);
|
|
|
|
*init = *new_initializer(array_of(init->ty->base, len), false);
|
|
|
|
}
|
|
|
|
|
2020-09-18 08:12:32 +03:00
|
|
|
for (; i < init->ty->array_len && !is_end(tok); i++) {
|
|
|
|
Token *start = tok;
|
2019-08-19 00:53:50 +03:00
|
|
|
if (i > 0)
|
|
|
|
tok = skip(tok, ",");
|
2020-09-18 08:12:32 +03:00
|
|
|
|
2020-10-06 07:09:15 +03:00
|
|
|
if (equal(tok, "[") || equal(tok, ".")) {
|
2020-09-18 08:12:32 +03:00
|
|
|
*rest = start;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-19 00:53:50 +03:00
|
|
|
initializer2(&tok, tok, init->children[i]);
|
|
|
|
}
|
|
|
|
*rest = tok;
|
|
|
|
}
|
|
|
|
|
2020-10-06 07:08:41 +03:00
|
|
|
// struct-initializer1 = "{" initializer ("," initializer)* ","? "}"
|
2019-08-19 00:53:50 +03:00
|
|
|
static void struct_initializer1(Token **rest, Token *tok, Initializer *init) {
|
2019-08-18 11:56:36 +03:00
|
|
|
tok = skip(tok, "{");
|
|
|
|
|
|
|
|
Member *mem = init->ty->members;
|
2020-10-06 07:09:15 +03:00
|
|
|
bool first = true;
|
2019-08-18 11:56:36 +03:00
|
|
|
|
2020-10-06 07:08:41 +03:00
|
|
|
while (!consume_end(rest, tok)) {
|
2020-10-06 07:09:15 +03:00
|
|
|
if (!first)
|
2019-08-18 11:56:36 +03:00
|
|
|
tok = skip(tok, ",");
|
2020-10-06 07:09:15 +03:00
|
|
|
first = false;
|
|
|
|
|
|
|
|
if (equal(tok, ".")) {
|
|
|
|
mem = struct_designator(&tok, tok, init->ty);
|
|
|
|
designation(&tok, tok, init->children[mem->idx]);
|
|
|
|
mem = mem->next;
|
|
|
|
continue;
|
|
|
|
}
|
2019-08-18 11:56:36 +03:00
|
|
|
|
|
|
|
if (mem) {
|
|
|
|
initializer2(&tok, tok, init->children[mem->idx]);
|
|
|
|
mem = mem->next;
|
|
|
|
} else {
|
|
|
|
tok = skip_excess_element(tok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 00:53:50 +03:00
|
|
|
// struct-initializer2 = initializer ("," initializer)*
|
2020-10-06 07:09:15 +03:00
|
|
|
static void struct_initializer2(Token **rest, Token *tok, Initializer *init, Member *mem) {
|
2019-08-19 00:53:50 +03:00
|
|
|
bool first = true;
|
|
|
|
|
2020-10-06 07:09:15 +03:00
|
|
|
for (; mem && !is_end(tok); mem = mem->next) {
|
|
|
|
Token *start = tok;
|
|
|
|
|
2019-08-19 00:53:50 +03:00
|
|
|
if (!first)
|
|
|
|
tok = skip(tok, ",");
|
|
|
|
first = false;
|
2020-10-06 07:09:15 +03:00
|
|
|
|
|
|
|
if (equal(tok, "[") || equal(tok, ".")) {
|
|
|
|
*rest = start;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-19 00:53:50 +03:00
|
|
|
initializer2(&tok, tok, init->children[mem->idx]);
|
|
|
|
}
|
|
|
|
*rest = tok;
|
|
|
|
}
|
|
|
|
|
2020-07-20 17:23:27 +03:00
|
|
|
static void union_initializer(Token **rest, Token *tok, Initializer *init) {
|
|
|
|
// Unlike structs, union initializers take only one initializer,
|
2020-08-15 11:01:54 +03:00
|
|
|
// and that initializes the first union member by default.
|
|
|
|
// You can initialize other member using a designated initializer.
|
|
|
|
if (equal(tok, "{") && equal(tok->next, ".")) {
|
|
|
|
Member *mem = struct_designator(&tok, tok->next, init->ty);
|
|
|
|
init->mem = mem;
|
|
|
|
designation(&tok, tok, init->children[mem->idx]);
|
|
|
|
*rest = skip(tok, "}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
init->mem = init->ty->members;
|
|
|
|
|
2019-08-19 00:53:50 +03:00
|
|
|
if (equal(tok, "{")) {
|
|
|
|
initializer2(&tok, tok->next, init->children[0]);
|
2020-10-06 07:08:41 +03:00
|
|
|
consume(&tok, tok, ",");
|
2019-08-19 00:53:50 +03:00
|
|
|
*rest = skip(tok, "}");
|
|
|
|
} else {
|
|
|
|
initializer2(rest, tok, init->children[0]);
|
|
|
|
}
|
2020-07-20 17:23:27 +03:00
|
|
|
}
|
|
|
|
|
2019-08-18 11:56:36 +03:00
|
|
|
// initializer = string-initializer | array-initializer
|
2020-07-20 17:23:27 +03:00
|
|
|
// | struct-initializer | union-initializer
|
|
|
|
// | assign
|
2019-08-18 10:34:39 +03:00
|
|
|
static void initializer2(Token **rest, Token *tok, Initializer *init) {
|
|
|
|
if (init->ty->kind == TY_ARRAY && tok->kind == TK_STR) {
|
|
|
|
string_initializer(rest, tok, init);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (init->ty->kind == TY_ARRAY) {
|
2019-08-19 00:53:50 +03:00
|
|
|
if (equal(tok, "{"))
|
|
|
|
array_initializer1(rest, tok, init);
|
|
|
|
else
|
2020-09-18 08:12:32 +03:00
|
|
|
array_initializer2(rest, tok, init, 0);
|
2019-08-18 06:01:02 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-18 11:56:36 +03:00
|
|
|
if (init->ty->kind == TY_STRUCT) {
|
2019-08-19 00:53:50 +03:00
|
|
|
if (equal(tok, "{")) {
|
|
|
|
struct_initializer1(rest, tok, init);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:05:46 +03:00
|
|
|
// A struct can be initialized with another struct. E.g.
|
|
|
|
// `struct T x = y;` where y is a variable of type `struct T`.
|
|
|
|
// Handle that case first.
|
2019-08-19 00:53:50 +03:00
|
|
|
Node *expr = assign(rest, tok);
|
|
|
|
add_type(expr);
|
|
|
|
if (expr->ty->kind == TY_STRUCT) {
|
|
|
|
init->expr = expr;
|
|
|
|
return;
|
2020-07-09 16:05:46 +03:00
|
|
|
}
|
|
|
|
|
2020-10-06 07:09:15 +03:00
|
|
|
struct_initializer2(rest, tok, init, init->ty->members);
|
2019-08-18 11:56:36 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-20 17:23:27 +03:00
|
|
|
if (init->ty->kind == TY_UNION) {
|
|
|
|
union_initializer(rest, tok, init);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-16 16:57:32 +03:00
|
|
|
if (equal(tok, "{")) {
|
|
|
|
// An initializer for a scalar variable can be surrounded by
|
|
|
|
// braces. E.g. `int x = {3};`. Handle that case.
|
|
|
|
initializer2(&tok, tok->next, init);
|
|
|
|
*rest = skip(tok, "}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-18 06:01:02 +03:00
|
|
|
init->expr = assign(rest, tok);
|
|
|
|
}
|
|
|
|
|
2020-09-20 10:16:00 +03:00
|
|
|
static Type *copy_struct_type(Type *ty) {
|
|
|
|
ty = copy_type(ty);
|
|
|
|
|
|
|
|
Member head = {};
|
|
|
|
Member *cur = &head;
|
|
|
|
for (Member *mem = ty->members; mem; mem = mem->next) {
|
|
|
|
Member *m = calloc(1, sizeof(Member));
|
|
|
|
*m = *mem;
|
|
|
|
cur = cur->next = m;
|
|
|
|
}
|
|
|
|
|
|
|
|
ty->members = head.next;
|
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
|
2020-09-04 08:39:22 +03:00
|
|
|
static Initializer *initializer(Token **rest, Token *tok, Type *ty, Type **new_ty) {
|
|
|
|
Initializer *init = new_initializer(ty, true);
|
2019-08-18 06:01:02 +03:00
|
|
|
initializer2(rest, tok, init);
|
2020-09-20 10:16:00 +03:00
|
|
|
|
|
|
|
if ((ty->kind == TY_STRUCT || ty->kind == TY_UNION) && ty->is_flexible) {
|
|
|
|
ty = copy_struct_type(ty);
|
|
|
|
|
|
|
|
Member *mem = ty->members;
|
|
|
|
while (mem->next)
|
|
|
|
mem = mem->next;
|
|
|
|
mem->ty = init->children[mem->idx]->ty;
|
|
|
|
ty->size += mem->ty->size;
|
|
|
|
|
|
|
|
*new_ty = ty;
|
|
|
|
return init;
|
|
|
|
}
|
|
|
|
|
2020-09-04 08:39:22 +03:00
|
|
|
*new_ty = init->ty;
|
2019-08-18 06:01:02 +03:00
|
|
|
return init;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Node *init_desg_expr(InitDesg *desg, Token *tok) {
|
|
|
|
if (desg->var)
|
|
|
|
return new_var_node(desg->var, tok);
|
|
|
|
|
2019-08-18 11:56:36 +03:00
|
|
|
if (desg->member) {
|
|
|
|
Node *node = new_unary(ND_MEMBER, init_desg_expr(desg->next, tok), tok);
|
|
|
|
node->member = desg->member;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2019-08-18 06:01:02 +03:00
|
|
|
Node *lhs = init_desg_expr(desg->next, tok);
|
|
|
|
Node *rhs = new_num(desg->idx, tok);
|
|
|
|
return new_unary(ND_DEREF, new_add(lhs, rhs, tok), tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Node *create_lvar_init(Initializer *init, Type *ty, InitDesg *desg, Token *tok) {
|
|
|
|
if (ty->kind == TY_ARRAY) {
|
|
|
|
Node *node = new_node(ND_NULL_EXPR, tok);
|
|
|
|
for (int i = 0; i < ty->array_len; i++) {
|
|
|
|
InitDesg desg2 = {desg, i};
|
|
|
|
Node *rhs = create_lvar_init(init->children[i], ty->base, &desg2, tok);
|
|
|
|
node = new_binary(ND_COMMA, node, rhs, tok);
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:05:46 +03:00
|
|
|
if (ty->kind == TY_STRUCT && !init->expr) {
|
2019-08-18 11:56:36 +03:00
|
|
|
Node *node = new_node(ND_NULL_EXPR, tok);
|
|
|
|
|
|
|
|
for (Member *mem = ty->members; mem; mem = mem->next) {
|
|
|
|
InitDesg desg2 = {desg, 0, mem};
|
|
|
|
Node *rhs = create_lvar_init(init->children[mem->idx], mem->ty, &desg2, tok);
|
|
|
|
node = new_binary(ND_COMMA, node, rhs, tok);
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-07-20 17:23:27 +03:00
|
|
|
if (ty->kind == TY_UNION) {
|
2020-08-15 11:01:54 +03:00
|
|
|
Member *mem = init->mem ? init->mem : ty->members;
|
|
|
|
InitDesg desg2 = {desg, 0, mem};
|
|
|
|
return create_lvar_init(init->children[mem->idx], mem->ty, &desg2, tok);
|
2020-07-20 17:23:27 +03:00
|
|
|
}
|
|
|
|
|
2020-09-18 07:36:43 +03:00
|
|
|
if (!init->expr)
|
|
|
|
return new_node(ND_NULL_EXPR, tok);
|
|
|
|
|
2019-08-18 06:01:02 +03:00
|
|
|
Node *lhs = init_desg_expr(desg, tok);
|
2020-09-18 07:36:43 +03:00
|
|
|
return new_binary(ND_ASSIGN, lhs, init->expr, tok);
|
2019-08-18 06:01:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// A variable definition with an initializer is a shorthand notation
|
|
|
|
// for a variable definition followed by assignments. This function
|
|
|
|
// generates assignment expressions for an initializer. For example,
|
|
|
|
// `int x[2][2] = {{6, 7}, {8, 9}}` is converted to the following
|
|
|
|
// expressions:
|
|
|
|
//
|
|
|
|
// x[0][0] = 6;
|
|
|
|
// x[0][1] = 7;
|
|
|
|
// x[1][0] = 8;
|
|
|
|
// x[1][1] = 9;
|
|
|
|
static Node *lvar_initializer(Token **rest, Token *tok, Obj *var) {
|
2020-09-04 08:39:22 +03:00
|
|
|
Initializer *init = initializer(rest, tok, var->ty, &var->ty);
|
2019-08-18 11:56:36 +03:00
|
|
|
InitDesg desg = {NULL, 0, NULL, var};
|
2020-09-18 07:36:43 +03:00
|
|
|
|
|
|
|
// If a partial initializer list is given, the standard requires
|
|
|
|
// that unspecified elements are set to 0. Here, we simply
|
|
|
|
// zero-initialize the entire memory region of a variable before
|
|
|
|
// initializing it with user-supplied values.
|
|
|
|
Node *lhs = new_node(ND_MEMZERO, tok);
|
|
|
|
lhs->var = var;
|
|
|
|
|
|
|
|
Node *rhs = create_lvar_init(init, var->ty, &desg, tok);
|
|
|
|
return new_binary(ND_COMMA, lhs, rhs, tok);
|
2019-08-18 06:01:02 +03:00
|
|
|
}
|
|
|
|
|
2020-09-06 15:06:15 +03:00
|
|
|
static uint64_t read_buf(char *buf, int sz) {
|
|
|
|
if (sz == 1)
|
|
|
|
return *buf;
|
|
|
|
if (sz == 2)
|
|
|
|
return *(uint16_t *)buf;
|
|
|
|
if (sz == 4)
|
|
|
|
return *(uint32_t *)buf;
|
|
|
|
if (sz == 8)
|
|
|
|
return *(uint64_t *)buf;
|
|
|
|
unreachable();
|
|
|
|
}
|
|
|
|
|
2020-09-04 11:26:34 +03:00
|
|
|
static void write_buf(char *buf, uint64_t val, int sz) {
|
|
|
|
if (sz == 1)
|
|
|
|
*buf = val;
|
|
|
|
else if (sz == 2)
|
|
|
|
*(uint16_t *)buf = val;
|
|
|
|
else if (sz == 4)
|
|
|
|
*(uint32_t *)buf = val;
|
|
|
|
else if (sz == 8)
|
|
|
|
*(uint64_t *)buf = val;
|
|
|
|
else
|
|
|
|
unreachable();
|
|
|
|
}
|
|
|
|
|
2020-07-20 17:53:12 +03:00
|
|
|
static Relocation *
|
|
|
|
write_gvar_data(Relocation *cur, Initializer *init, Type *ty, char *buf, int offset) {
|
2020-09-04 11:26:34 +03:00
|
|
|
if (ty->kind == TY_ARRAY) {
|
|
|
|
int sz = ty->base->size;
|
|
|
|
for (int i = 0; i < ty->array_len; i++)
|
2020-07-20 17:53:12 +03:00
|
|
|
cur = write_gvar_data(cur, init->children[i], ty->base, buf, offset + sz * i);
|
|
|
|
return cur;
|
2020-09-04 11:26:34 +03:00
|
|
|
}
|
|
|
|
|
2019-08-18 14:35:17 +03:00
|
|
|
if (ty->kind == TY_STRUCT) {
|
2020-09-06 15:06:15 +03:00
|
|
|
for (Member *mem = ty->members; mem; mem = mem->next) {
|
|
|
|
if (mem->is_bitfield) {
|
|
|
|
Node *expr = init->children[mem->idx]->expr;
|
|
|
|
if (!expr)
|
|
|
|
break;
|
|
|
|
|
|
|
|
char *loc = buf + offset + mem->offset;
|
|
|
|
uint64_t oldval = read_buf(loc, mem->ty->size);
|
|
|
|
uint64_t newval = eval(expr);
|
|
|
|
uint64_t mask = (1L << mem->bit_width) - 1;
|
|
|
|
uint64_t combined = oldval | ((newval & mask) << mem->bit_offset);
|
|
|
|
write_buf(loc, combined, mem->ty->size);
|
|
|
|
} else {
|
|
|
|
cur = write_gvar_data(cur, init->children[mem->idx], mem->ty, buf,
|
|
|
|
offset + mem->offset);
|
|
|
|
}
|
|
|
|
}
|
2020-07-20 17:53:12 +03:00
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
|
2020-08-15 11:01:54 +03:00
|
|
|
if (ty->kind == TY_UNION) {
|
|
|
|
if (!init->mem)
|
|
|
|
return cur;
|
|
|
|
return write_gvar_data(cur, init->children[init->mem->idx],
|
|
|
|
init->mem->ty, buf, offset);
|
|
|
|
}
|
2020-07-20 17:53:12 +03:00
|
|
|
|
|
|
|
if (!init->expr)
|
|
|
|
return cur;
|
|
|
|
|
2020-05-01 13:30:17 +03:00
|
|
|
if (ty->kind == TY_FLOAT) {
|
|
|
|
*(float *)(buf + offset) = eval_double(init->expr);
|
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ty->kind == TY_DOUBLE) {
|
|
|
|
*(double *)(buf + offset) = eval_double(init->expr);
|
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
|
2020-09-20 07:23:53 +03:00
|
|
|
char **label = NULL;
|
2020-07-20 17:53:12 +03:00
|
|
|
uint64_t val = eval2(init->expr, &label);
|
|
|
|
|
|
|
|
if (!label) {
|
|
|
|
write_buf(buf + offset, val, ty->size);
|
|
|
|
return cur;
|
2019-08-18 14:35:17 +03:00
|
|
|
}
|
|
|
|
|
2020-07-20 17:53:12 +03:00
|
|
|
Relocation *rel = calloc(1, sizeof(Relocation));
|
|
|
|
rel->offset = offset;
|
|
|
|
rel->label = label;
|
|
|
|
rel->addend = val;
|
|
|
|
cur->next = rel;
|
|
|
|
return cur->next;
|
2020-09-04 11:26:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initializers for global variables are evaluated at compile-time and
|
|
|
|
// embedded to .data section. This function serializes Initializer
|
|
|
|
// objects to a flat byte array. It is a compile error if an
|
|
|
|
// initializer list contains a non-constant expression.
|
|
|
|
static void gvar_initializer(Token **rest, Token *tok, Obj *var) {
|
|
|
|
Initializer *init = initializer(rest, tok, var->ty, &var->ty);
|
|
|
|
|
2020-07-20 17:53:12 +03:00
|
|
|
Relocation head = {};
|
2020-09-04 11:26:34 +03:00
|
|
|
char *buf = calloc(1, var->ty->size);
|
2020-07-20 17:53:12 +03:00
|
|
|
write_gvar_data(&head, init, var->ty, buf, 0);
|
2020-09-04 11:26:34 +03:00
|
|
|
var->init_data = buf;
|
2020-07-20 17:53:12 +03:00
|
|
|
var->rel = head.next;
|
2020-09-04 11:26:34 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 15:04:17 +03:00
|
|
|
// Returns true if a given token represents a type.
|
|
|
|
static bool is_typename(Token *tok) {
|
2020-09-03 16:55:50 +03:00
|
|
|
static HashMap map;
|
|
|
|
|
|
|
|
if (map.capacity == 0) {
|
|
|
|
static char *kw[] = {
|
|
|
|
"void", "_Bool", "char", "short", "int", "long", "struct", "union",
|
|
|
|
"typedef", "enum", "static", "extern", "_Alignas", "signed", "unsigned",
|
|
|
|
"const", "volatile", "auto", "register", "restrict", "__restrict",
|
|
|
|
"__restrict__", "_Noreturn", "float", "double", "typeof", "inline",
|
2020-09-15 05:30:56 +03:00
|
|
|
"_Thread_local", "__thread", "_Atomic",
|
2020-09-03 16:55:50 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = 0; i < sizeof(kw) / sizeof(*kw); i++)
|
|
|
|
hashmap_put(&map, kw[i], (void *)1);
|
|
|
|
}
|
2019-08-11 04:18:55 +03:00
|
|
|
|
2020-09-03 16:55:50 +03:00
|
|
|
return hashmap_get2(&map, tok->loc, tok->len) || find_typedef(tok);
|
2020-08-27 15:04:17 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 17:50:52 +03:00
|
|
|
// asm-stmt = "asm" ("volatile" | "inline")* "(" string-literal ")"
|
|
|
|
static Node *asm_stmt(Token **rest, Token *tok) {
|
|
|
|
Node *node = new_node(ND_ASM, tok);
|
|
|
|
tok = tok->next;
|
|
|
|
|
|
|
|
while (equal(tok, "volatile") || equal(tok, "inline"))
|
|
|
|
tok = tok->next;
|
|
|
|
|
|
|
|
tok = skip(tok, "(");
|
|
|
|
if (tok->kind != TK_STR || tok->ty->base->kind != TY_CHAR)
|
|
|
|
error_tok(tok, "expected string literal");
|
|
|
|
node->asm_str = tok->str;
|
|
|
|
*rest = skip(tok->next, ")");
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-08-27 16:01:34 +03:00
|
|
|
// stmt = "return" expr? ";"
|
2020-10-07 06:47:09 +03:00
|
|
|
// | "if" "(" expr ")" stmt ("else" stmt)?
|
2019-08-15 10:43:24 +03:00
|
|
|
// | "switch" "(" expr ")" stmt
|
2020-08-30 06:51:05 +03:00
|
|
|
// | "case" const-expr ("..." const-expr)? ":" stmt
|
2019-08-15 10:43:24 +03:00
|
|
|
// | "default" ":" 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
|
2019-08-24 10:09:46 +03:00
|
|
|
// | "do" stmt "while" "(" expr ")" ";"
|
2020-08-27 17:50:52 +03:00
|
|
|
// | "asm" asm-stmt
|
2020-09-04 06:26:27 +03:00
|
|
|
// | "goto" (ident | "*" expr) ";"
|
2019-08-15 07:48:41 +03:00
|
|
|
// | "break" ";"
|
2020-08-27 15:59:19 +03:00
|
|
|
// | "continue" ";"
|
2020-09-04 06:27:21 +03:00
|
|
|
// | 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-08-27 16:01:34 +03:00
|
|
|
if (consume(rest, tok->next, ";"))
|
|
|
|
return node;
|
|
|
|
|
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);
|
2020-09-04 05:57:15 +03:00
|
|
|
Type *ty = current_fn->ty->return_ty;
|
|
|
|
if (ty->kind != TY_STRUCT && ty->kind != TY_UNION)
|
|
|
|
exp = new_cast(exp, current_fn->ty->return_ty);
|
|
|
|
|
|
|
|
node->lhs = exp;
|
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-15 10:43:24 +03:00
|
|
|
if (equal(tok, "switch")) {
|
|
|
|
Node *node = new_node(ND_SWITCH, tok);
|
|
|
|
tok = skip(tok->next, "(");
|
|
|
|
node->cond = expr(&tok, tok);
|
|
|
|
tok = skip(tok, ")");
|
|
|
|
|
|
|
|
Node *sw = current_switch;
|
|
|
|
current_switch = node;
|
|
|
|
|
|
|
|
char *brk = brk_label;
|
|
|
|
brk_label = node->brk_label = new_unique_name();
|
|
|
|
|
|
|
|
node->then = stmt(rest, tok);
|
|
|
|
|
|
|
|
current_switch = sw;
|
|
|
|
brk_label = brk;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(tok, "case")) {
|
|
|
|
if (!current_switch)
|
|
|
|
error_tok(tok, "stray case");
|
|
|
|
|
|
|
|
Node *node = new_node(ND_CASE, tok);
|
2020-08-30 06:51:05 +03:00
|
|
|
int begin = const_expr(&tok, tok->next);
|
|
|
|
int end;
|
|
|
|
|
|
|
|
if (equal(tok, "...")) {
|
|
|
|
// [GNU] Case ranges, e.g. "case 1 ... 5:"
|
|
|
|
end = const_expr(&tok, tok->next);
|
|
|
|
if (end < begin)
|
|
|
|
error_tok(tok, "empty case range specified");
|
|
|
|
} else {
|
|
|
|
end = begin;
|
|
|
|
}
|
|
|
|
|
2019-08-17 04:38:20 +03:00
|
|
|
tok = skip(tok, ":");
|
2019-08-15 10:43:24 +03:00
|
|
|
node->label = new_unique_name();
|
|
|
|
node->lhs = stmt(rest, tok);
|
2020-08-30 06:51:05 +03:00
|
|
|
node->begin = begin;
|
|
|
|
node->end = end;
|
2019-08-15 10:43:24 +03:00
|
|
|
node->case_next = current_switch->case_next;
|
|
|
|
current_switch->case_next = node;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(tok, "default")) {
|
|
|
|
if (!current_switch)
|
|
|
|
error_tok(tok, "stray default");
|
|
|
|
|
|
|
|
Node *node = new_node(ND_CASE, tok);
|
|
|
|
tok = skip(tok->next, ":");
|
|
|
|
node->label = new_unique_name();
|
|
|
|
node->lhs = stmt(rest, tok);
|
|
|
|
current_switch->default_case = node;
|
|
|
|
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();
|
|
|
|
|
2019-08-15 07:48:41 +03:00
|
|
|
char *brk = brk_label;
|
2020-08-27 15:59:19 +03:00
|
|
|
char *cont = cont_label;
|
2019-08-15 07:48:41 +03:00
|
|
|
brk_label = node->brk_label = new_unique_name();
|
2020-08-27 15:59:19 +03:00
|
|
|
cont_label = node->cont_label = new_unique_name();
|
2019-08-15 07:48:41 +03:00
|
|
|
|
2019-08-11 16:53:26 +03:00
|
|
|
if (is_typename(tok)) {
|
|
|
|
Type *basety = declspec(&tok, tok, NULL);
|
2020-09-04 05:20:55 +03:00
|
|
|
node->init = declaration(&tok, tok, basety, NULL);
|
2019-08-11 16:53:26 +03:00
|
|
|
} 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-15 07:48:41 +03:00
|
|
|
|
2019-08-11 16:53:26 +03:00
|
|
|
leave_scope();
|
2019-08-15 07:48:41 +03:00
|
|
|
brk_label = brk;
|
2020-08-27 15:59:19 +03:00
|
|
|
cont_label = cont;
|
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, ")");
|
2019-08-15 07:48:41 +03:00
|
|
|
|
|
|
|
char *brk = brk_label;
|
2020-08-27 15:59:19 +03:00
|
|
|
char *cont = cont_label;
|
2019-08-15 07:48:41 +03:00
|
|
|
brk_label = node->brk_label = new_unique_name();
|
2020-08-27 15:59:19 +03:00
|
|
|
cont_label = node->cont_label = new_unique_name();
|
|
|
|
|
2019-08-04 11:24:03 +03:00
|
|
|
node->then = stmt(rest, tok);
|
2020-08-27 15:59:19 +03:00
|
|
|
|
2019-08-15 07:48:41 +03:00
|
|
|
brk_label = brk;
|
2020-08-27 15:59:19 +03:00
|
|
|
cont_label = cont;
|
2019-08-04 11:24:03 +03:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2019-08-24 10:09:46 +03:00
|
|
|
if (equal(tok, "do")) {
|
|
|
|
Node *node = new_node(ND_DO, tok);
|
|
|
|
|
|
|
|
char *brk = brk_label;
|
|
|
|
char *cont = cont_label;
|
|
|
|
brk_label = node->brk_label = new_unique_name();
|
|
|
|
cont_label = node->cont_label = new_unique_name();
|
|
|
|
|
|
|
|
node->then = stmt(&tok, tok->next);
|
|
|
|
|
|
|
|
brk_label = brk;
|
|
|
|
cont_label = cont;
|
|
|
|
|
|
|
|
tok = skip(tok, "while");
|
|
|
|
tok = skip(tok, "(");
|
|
|
|
node->cond = expr(&tok, tok);
|
|
|
|
tok = skip(tok, ")");
|
|
|
|
*rest = skip(tok, ";");
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:50:52 +03:00
|
|
|
if (equal(tok, "asm"))
|
|
|
|
return asm_stmt(rest, tok);
|
|
|
|
|
2020-09-04 06:27:21 +03:00
|
|
|
if (equal(tok, "goto")) {
|
2020-09-04 06:26:27 +03:00
|
|
|
if (equal(tok->next, "*")) {
|
|
|
|
// [GNU] `goto *ptr` jumps to the address specified by `ptr`.
|
|
|
|
Node *node = new_node(ND_GOTO_EXPR, tok);
|
|
|
|
node->lhs = expr(&tok, tok->next->next);
|
|
|
|
*rest = skip(tok, ";");
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-09-04 06:27:21 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-08-15 07:48:41 +03:00
|
|
|
if (equal(tok, "break")) {
|
|
|
|
if (!brk_label)
|
|
|
|
error_tok(tok, "stray break");
|
|
|
|
Node *node = new_node(ND_GOTO, tok);
|
|
|
|
node->unique_label = brk_label;
|
|
|
|
*rest = skip(tok->next, ";");
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-08-27 15:59:19 +03:00
|
|
|
if (equal(tok, "continue")) {
|
|
|
|
if (!cont_label)
|
|
|
|
error_tok(tok, "stray continue");
|
|
|
|
Node *node = new_node(ND_GOTO, tok);
|
|
|
|
node->unique_label = cont_label;
|
|
|
|
*rest = skip(tok->next, ";");
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-09-04 06:27:21 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-04 12:08:32 +03:00
|
|
|
if (is_function(tok)) {
|
|
|
|
tok = function(tok, basety, &attr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attr.is_extern) {
|
|
|
|
tok = global_variable(tok, basety, &attr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-09-04 05:20:55 +03:00
|
|
|
cur = cur->next = declaration(&tok, tok, basety, &attr);
|
2020-09-04 11:23:26 +03:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2019-08-17 04:38:20 +03:00
|
|
|
static int64_t eval(Node *node) {
|
2020-07-20 17:53:12 +03:00
|
|
|
return eval2(node, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate a given node as a constant expression.
|
|
|
|
//
|
|
|
|
// A constant expression is either just a number or ptr+n where ptr
|
|
|
|
// is a pointer to a global variable and n is a postiive/negative
|
|
|
|
// number. The latter form is accepted only as an initialization
|
|
|
|
// expression for a global variable.
|
2020-09-20 07:23:53 +03:00
|
|
|
static int64_t eval2(Node *node, char ***label) {
|
2019-08-17 04:38:20 +03:00
|
|
|
add_type(node);
|
|
|
|
|
2020-05-01 13:30:17 +03:00
|
|
|
if (is_flonum(node->ty))
|
|
|
|
return eval_double(node);
|
|
|
|
|
2019-08-17 04:38:20 +03:00
|
|
|
switch (node->kind) {
|
|
|
|
case ND_ADD:
|
2020-07-20 17:53:12 +03:00
|
|
|
return eval2(node->lhs, label) + eval(node->rhs);
|
2019-08-17 04:38:20 +03:00
|
|
|
case ND_SUB:
|
2020-07-20 17:53:12 +03:00
|
|
|
return eval2(node->lhs, label) - eval(node->rhs);
|
2019-08-17 04:38:20 +03:00
|
|
|
case ND_MUL:
|
|
|
|
return eval(node->lhs) * eval(node->rhs);
|
|
|
|
case ND_DIV:
|
2020-03-26 15:20:24 +03:00
|
|
|
if (node->ty->is_unsigned)
|
|
|
|
return (uint64_t)eval(node->lhs) / eval(node->rhs);
|
2019-08-17 04:38:20 +03:00
|
|
|
return eval(node->lhs) / eval(node->rhs);
|
|
|
|
case ND_NEG:
|
|
|
|
return -eval(node->lhs);
|
|
|
|
case ND_MOD:
|
2020-03-26 15:20:24 +03:00
|
|
|
if (node->ty->is_unsigned)
|
|
|
|
return (uint64_t)eval(node->lhs) % eval(node->rhs);
|
2019-08-17 04:38:20 +03:00
|
|
|
return eval(node->lhs) % eval(node->rhs);
|
|
|
|
case ND_BITAND:
|
|
|
|
return eval(node->lhs) & eval(node->rhs);
|
|
|
|
case ND_BITOR:
|
|
|
|
return eval(node->lhs) | eval(node->rhs);
|
|
|
|
case ND_BITXOR:
|
|
|
|
return eval(node->lhs) ^ eval(node->rhs);
|
|
|
|
case ND_SHL:
|
|
|
|
return eval(node->lhs) << eval(node->rhs);
|
|
|
|
case ND_SHR:
|
2020-03-26 15:20:24 +03:00
|
|
|
if (node->ty->is_unsigned && node->ty->size == 8)
|
|
|
|
return (uint64_t)eval(node->lhs) >> eval(node->rhs);
|
2019-08-17 04:38:20 +03:00
|
|
|
return eval(node->lhs) >> eval(node->rhs);
|
|
|
|
case ND_EQ:
|
|
|
|
return eval(node->lhs) == eval(node->rhs);
|
|
|
|
case ND_NE:
|
|
|
|
return eval(node->lhs) != eval(node->rhs);
|
|
|
|
case ND_LT:
|
2020-03-26 15:20:24 +03:00
|
|
|
if (node->lhs->ty->is_unsigned)
|
|
|
|
return (uint64_t)eval(node->lhs) < eval(node->rhs);
|
2019-08-17 04:38:20 +03:00
|
|
|
return eval(node->lhs) < eval(node->rhs);
|
|
|
|
case ND_LE:
|
2020-03-26 15:20:24 +03:00
|
|
|
if (node->lhs->ty->is_unsigned)
|
|
|
|
return (uint64_t)eval(node->lhs) <= eval(node->rhs);
|
2019-08-17 04:38:20 +03:00
|
|
|
return eval(node->lhs) <= eval(node->rhs);
|
|
|
|
case ND_COND:
|
2020-07-20 17:53:12 +03:00
|
|
|
return eval(node->cond) ? eval2(node->then, label) : eval2(node->els, label);
|
2019-08-17 04:38:20 +03:00
|
|
|
case ND_COMMA:
|
2020-07-20 17:53:12 +03:00
|
|
|
return eval2(node->rhs, label);
|
2019-08-17 04:38:20 +03:00
|
|
|
case ND_NOT:
|
|
|
|
return !eval(node->lhs);
|
|
|
|
case ND_BITNOT:
|
|
|
|
return ~eval(node->lhs);
|
|
|
|
case ND_LOGAND:
|
|
|
|
return eval(node->lhs) && eval(node->rhs);
|
|
|
|
case ND_LOGOR:
|
|
|
|
return eval(node->lhs) || eval(node->rhs);
|
2020-07-20 17:53:12 +03:00
|
|
|
case ND_CAST: {
|
|
|
|
int64_t val = eval2(node->lhs, label);
|
2019-08-17 04:38:20 +03:00
|
|
|
if (is_integer(node->ty)) {
|
|
|
|
switch (node->ty->size) {
|
2020-03-26 15:20:24 +03:00
|
|
|
case 1: return node->ty->is_unsigned ? (uint8_t)val : (int8_t)val;
|
|
|
|
case 2: return node->ty->is_unsigned ? (uint16_t)val : (int16_t)val;
|
|
|
|
case 4: return node->ty->is_unsigned ? (uint32_t)val : (int32_t)val;
|
2019-08-17 04:38:20 +03:00
|
|
|
}
|
|
|
|
}
|
2020-07-20 17:53:12 +03:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
case ND_ADDR:
|
|
|
|
return eval_rval(node->lhs, label);
|
2020-09-20 07:23:53 +03:00
|
|
|
case ND_LABEL_VAL:
|
|
|
|
*label = &node->unique_label;
|
|
|
|
return 0;
|
2020-07-20 17:53:12 +03:00
|
|
|
case ND_MEMBER:
|
|
|
|
if (!label)
|
|
|
|
error_tok(node->tok, "not a compile-time constant");
|
|
|
|
if (node->ty->kind != TY_ARRAY)
|
|
|
|
error_tok(node->tok, "invalid initializer");
|
|
|
|
return eval_rval(node->lhs, label) + node->member->offset;
|
|
|
|
case ND_VAR:
|
|
|
|
if (!label)
|
|
|
|
error_tok(node->tok, "not a compile-time constant");
|
|
|
|
if (node->var->ty->kind != TY_ARRAY && node->var->ty->kind != TY_FUNC)
|
|
|
|
error_tok(node->tok, "invalid initializer");
|
2020-09-20 07:23:53 +03:00
|
|
|
*label = &node->var->name;
|
2020-07-20 17:53:12 +03:00
|
|
|
return 0;
|
2019-08-17 04:38:20 +03:00
|
|
|
case ND_NUM:
|
|
|
|
return node->val;
|
|
|
|
}
|
|
|
|
|
|
|
|
error_tok(node->tok, "not a compile-time constant");
|
|
|
|
}
|
|
|
|
|
2020-09-20 07:23:53 +03:00
|
|
|
static int64_t eval_rval(Node *node, char ***label) {
|
2020-07-20 17:53:12 +03:00
|
|
|
switch (node->kind) {
|
|
|
|
case ND_VAR:
|
|
|
|
if (node->var->is_local)
|
|
|
|
error_tok(node->tok, "not a compile-time constant");
|
2020-09-20 07:23:53 +03:00
|
|
|
*label = &node->var->name;
|
2020-07-20 17:53:12 +03:00
|
|
|
return 0;
|
|
|
|
case ND_DEREF:
|
|
|
|
return eval2(node->lhs, label);
|
|
|
|
case ND_MEMBER:
|
|
|
|
return eval_rval(node->lhs, label) + node->member->offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
error_tok(node->tok, "invalid initializer");
|
|
|
|
}
|
|
|
|
|
2020-09-04 08:44:12 +03:00
|
|
|
static bool is_const_expr(Node *node) {
|
|
|
|
add_type(node);
|
|
|
|
|
|
|
|
switch (node->kind) {
|
|
|
|
case ND_ADD:
|
|
|
|
case ND_SUB:
|
|
|
|
case ND_MUL:
|
|
|
|
case ND_DIV:
|
|
|
|
case ND_BITAND:
|
|
|
|
case ND_BITOR:
|
|
|
|
case ND_BITXOR:
|
|
|
|
case ND_SHL:
|
|
|
|
case ND_SHR:
|
|
|
|
case ND_EQ:
|
|
|
|
case ND_NE:
|
|
|
|
case ND_LT:
|
|
|
|
case ND_LE:
|
|
|
|
case ND_LOGAND:
|
|
|
|
case ND_LOGOR:
|
|
|
|
return is_const_expr(node->lhs) && is_const_expr(node->rhs);
|
|
|
|
case ND_COND:
|
|
|
|
if (!is_const_expr(node->cond))
|
|
|
|
return false;
|
|
|
|
return is_const_expr(eval(node->cond) ? node->then : node->els);
|
|
|
|
case ND_COMMA:
|
|
|
|
return is_const_expr(node->rhs);
|
|
|
|
case ND_NEG:
|
|
|
|
case ND_NOT:
|
|
|
|
case ND_BITNOT:
|
|
|
|
case ND_CAST:
|
|
|
|
return is_const_expr(node->lhs);
|
|
|
|
case ND_NUM:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-20 13:36:36 +03:00
|
|
|
int64_t const_expr(Token **rest, Token *tok) {
|
2019-08-17 04:38:20 +03:00
|
|
|
Node *node = conditional(rest, tok);
|
|
|
|
return eval(node);
|
|
|
|
}
|
|
|
|
|
2020-05-01 13:30:17 +03:00
|
|
|
static double eval_double(Node *node) {
|
|
|
|
add_type(node);
|
|
|
|
|
|
|
|
if (is_integer(node->ty)) {
|
|
|
|
if (node->ty->is_unsigned)
|
|
|
|
return (unsigned long)eval(node);
|
|
|
|
return eval(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (node->kind) {
|
|
|
|
case ND_ADD:
|
|
|
|
return eval_double(node->lhs) + eval_double(node->rhs);
|
|
|
|
case ND_SUB:
|
|
|
|
return eval_double(node->lhs) - eval_double(node->rhs);
|
|
|
|
case ND_MUL:
|
|
|
|
return eval_double(node->lhs) * eval_double(node->rhs);
|
|
|
|
case ND_DIV:
|
|
|
|
return eval_double(node->lhs) / eval_double(node->rhs);
|
|
|
|
case ND_NEG:
|
|
|
|
return -eval_double(node->lhs);
|
|
|
|
case ND_COND:
|
|
|
|
return eval_double(node->cond) ? eval_double(node->then) : eval_double(node->els);
|
|
|
|
case ND_COMMA:
|
|
|
|
return eval_double(node->rhs);
|
|
|
|
case ND_CAST:
|
|
|
|
if (is_flonum(node->lhs->ty))
|
|
|
|
return eval_double(node->lhs);
|
|
|
|
return eval(node->lhs);
|
|
|
|
case ND_NUM:
|
|
|
|
return node->fval;
|
|
|
|
}
|
|
|
|
|
|
|
|
error_tok(node->tok, "not a compile-time constant");
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:50:05 +03:00
|
|
|
// Convert op= operators to expressions containing an assignment.
|
|
|
|
//
|
|
|
|
// In general, `A op= C` is converted to ``tmp = &A, *tmp = *tmp op B`.
|
|
|
|
// However, if a given expression is of form `A.x op= C`, the input is
|
|
|
|
// converted to `tmp = &A, (*tmp).x = (*tmp).x op C` to handle assignments
|
|
|
|
// to bitfields.
|
2020-10-07 14:17:30 +03:00
|
|
|
static Node *to_assign(Node *binary) {
|
|
|
|
add_type(binary->lhs);
|
|
|
|
add_type(binary->rhs);
|
|
|
|
Token *tok = binary->tok;
|
|
|
|
|
2020-08-27 17:50:05 +03:00
|
|
|
// Convert `A.x op= C` to `tmp = &A, (*tmp).x = (*tmp).x op C`.
|
|
|
|
if (binary->lhs->kind == ND_MEMBER) {
|
|
|
|
Obj *var = new_lvar("", pointer_to(binary->lhs->lhs->ty));
|
|
|
|
|
|
|
|
Node *expr1 = new_binary(ND_ASSIGN, new_var_node(var, tok),
|
|
|
|
new_unary(ND_ADDR, binary->lhs->lhs, tok), tok);
|
|
|
|
|
|
|
|
Node *expr2 = new_unary(ND_MEMBER,
|
|
|
|
new_unary(ND_DEREF, new_var_node(var, tok), tok),
|
|
|
|
tok);
|
|
|
|
expr2->member = binary->lhs->member;
|
|
|
|
|
|
|
|
Node *expr3 = new_unary(ND_MEMBER,
|
|
|
|
new_unary(ND_DEREF, new_var_node(var, tok), tok),
|
|
|
|
tok);
|
|
|
|
expr3->member = binary->lhs->member;
|
|
|
|
|
|
|
|
Node *expr4 = new_binary(ND_ASSIGN, expr2,
|
|
|
|
new_binary(binary->kind, expr3, binary->rhs, tok),
|
|
|
|
tok);
|
|
|
|
|
|
|
|
return new_binary(ND_COMMA, expr1, expr4, tok);
|
|
|
|
}
|
|
|
|
|
2020-09-15 05:30:56 +03:00
|
|
|
// If A is an atomic type, Convert `A op= B` to
|
|
|
|
//
|
|
|
|
// ({
|
|
|
|
// T1 *addr = &A; T2 val = (B); T1 old = *addr; T1 new;
|
|
|
|
// do {
|
|
|
|
// new = old op val;
|
|
|
|
// } while (!atomic_compare_exchange_strong(addr, &old, new));
|
|
|
|
// new;
|
|
|
|
// })
|
|
|
|
if (binary->lhs->ty->is_atomic) {
|
|
|
|
Node head = {};
|
|
|
|
Node *cur = &head;
|
|
|
|
|
|
|
|
Obj *addr = new_lvar("", pointer_to(binary->lhs->ty));
|
|
|
|
Obj *val = new_lvar("", binary->rhs->ty);
|
|
|
|
Obj *old = new_lvar("", binary->lhs->ty);
|
|
|
|
Obj *new = new_lvar("", binary->lhs->ty);
|
|
|
|
|
|
|
|
cur = cur->next =
|
|
|
|
new_unary(ND_EXPR_STMT,
|
|
|
|
new_binary(ND_ASSIGN, new_var_node(addr, tok),
|
|
|
|
new_unary(ND_ADDR, binary->lhs, tok), tok),
|
|
|
|
tok);
|
|
|
|
|
|
|
|
cur = cur->next =
|
|
|
|
new_unary(ND_EXPR_STMT,
|
|
|
|
new_binary(ND_ASSIGN, new_var_node(val, tok), binary->rhs, tok),
|
|
|
|
tok);
|
|
|
|
|
|
|
|
cur = cur->next =
|
|
|
|
new_unary(ND_EXPR_STMT,
|
|
|
|
new_binary(ND_ASSIGN, new_var_node(old, tok),
|
|
|
|
new_unary(ND_DEREF, new_var_node(addr, tok), tok), tok),
|
|
|
|
tok);
|
|
|
|
|
|
|
|
Node *loop = new_node(ND_DO, tok);
|
|
|
|
loop->brk_label = new_unique_name();
|
|
|
|
loop->cont_label = new_unique_name();
|
|
|
|
|
|
|
|
Node *body = new_binary(ND_ASSIGN,
|
|
|
|
new_var_node(new, tok),
|
|
|
|
new_binary(binary->kind, new_var_node(old, tok),
|
|
|
|
new_var_node(val, tok), tok),
|
|
|
|
tok);
|
|
|
|
|
|
|
|
loop->then = new_node(ND_BLOCK, tok);
|
|
|
|
loop->then->body = new_unary(ND_EXPR_STMT, body, tok);
|
|
|
|
|
|
|
|
Node *cas = new_node(ND_CAS, tok);
|
|
|
|
cas->cas_addr = new_var_node(addr, tok);
|
|
|
|
cas->cas_old = new_unary(ND_ADDR, new_var_node(old, tok), tok);
|
|
|
|
cas->cas_new = new_var_node(new, tok);
|
|
|
|
loop->cond = new_unary(ND_NOT, cas, tok);
|
|
|
|
|
|
|
|
cur = cur->next = loop;
|
|
|
|
cur = cur->next = new_unary(ND_EXPR_STMT, new_var_node(new, tok), tok);
|
|
|
|
|
|
|
|
Node *node = new_node(ND_STMT_EXPR, tok);
|
|
|
|
node->body = head.next;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert `A op= B` to ``tmp = &A, *tmp = *tmp op B`.
|
2020-10-07 14:17:30 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-08-17 04:27:35 +03:00
|
|
|
// assign = conditional (assign-op assign)?
|
2020-10-07 14:19:35 +03:00
|
|
|
// assign-op = "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "&=" | "|=" | "^="
|
2020-10-07 14:23:22 +03:00
|
|
|
// | "<<=" | ">>="
|
2020-09-26 02:59:56 +03:00
|
|
|
static Node *assign(Token **rest, Token *tok) {
|
2019-08-17 04:27:35 +03:00
|
|
|
Node *node = conditional(&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));
|
|
|
|
|
2020-10-07 14:23:22 +03:00
|
|
|
if (equal(tok, "<<="))
|
|
|
|
return to_assign(new_binary(ND_SHL, node, assign(rest, tok->next), tok));
|
|
|
|
|
|
|
|
if (equal(tok, ">>="))
|
|
|
|
return to_assign(new_binary(ND_SHR, node, assign(rest, tok->next), tok));
|
|
|
|
|
2020-10-07 14:19:35 +03:00
|
|
|
*rest = tok;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-09-13 09:58:54 +03:00
|
|
|
// conditional = logor ("?" expr? ":" conditional)?
|
2019-08-17 04:27:35 +03:00
|
|
|
static Node *conditional(Token **rest, Token *tok) {
|
|
|
|
Node *cond = logor(&tok, tok);
|
|
|
|
|
|
|
|
if (!equal(tok, "?")) {
|
|
|
|
*rest = tok;
|
|
|
|
return cond;
|
|
|
|
}
|
|
|
|
|
2020-09-13 09:58:54 +03:00
|
|
|
if (equal(tok->next, ":")) {
|
|
|
|
// [GNU] Compile `a ?: b` as `tmp = a, tmp ? tmp : b`.
|
|
|
|
add_type(cond);
|
|
|
|
Obj *var = new_lvar("", cond->ty);
|
|
|
|
Node *lhs = new_binary(ND_ASSIGN, new_var_node(var, tok), cond, tok);
|
|
|
|
Node *rhs = new_node(ND_COND, tok);
|
|
|
|
rhs->cond = new_var_node(var, tok);
|
|
|
|
rhs->then = new_var_node(var, tok);
|
|
|
|
rhs->els = conditional(rest, tok->next->next);
|
|
|
|
return new_binary(ND_COMMA, lhs, rhs, tok);
|
|
|
|
}
|
|
|
|
|
2019-08-17 04:27:35 +03:00
|
|
|
Node *node = new_node(ND_COND, tok);
|
|
|
|
node->cond = cond;
|
|
|
|
node->then = expr(&tok, tok->next);
|
|
|
|
tok = skip(tok, ":");
|
|
|
|
node->els = conditional(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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:23:22 +03:00
|
|
|
// relational = shift ("<" shift | "<=" shift | ">" shift | ">=" shift)*
|
2020-10-07 14:11:16 +03:00
|
|
|
static Node *relational(Token **rest, Token *tok) {
|
2020-10-07 14:23:22 +03:00
|
|
|
Node *node = shift(&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, "<")) {
|
2020-10-07 14:23:22 +03:00
|
|
|
node = new_binary(ND_LT, node, shift(&tok, tok->next), start);
|
2020-10-07 14:11:16 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(tok, "<=")) {
|
2020-10-07 14:23:22 +03:00
|
|
|
node = new_binary(ND_LE, node, shift(&tok, tok->next), start);
|
2020-10-07 14:11:16 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(tok, ">")) {
|
2020-10-07 14:23:22 +03:00
|
|
|
node = new_binary(ND_LT, shift(&tok, tok->next), node, start);
|
2020-10-07 14:11:16 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(tok, ">=")) {
|
2020-10-07 14:23:22 +03:00
|
|
|
node = new_binary(ND_LE, shift(&tok, tok->next), node, start);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
*rest = tok;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// shift = add ("<<" add | ">>" add)*
|
|
|
|
static Node *shift(Token **rest, Token *tok) {
|
|
|
|
Node *node = add(&tok, tok);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
Token *start = tok;
|
|
|
|
|
|
|
|
if (equal(tok, "<<")) {
|
|
|
|
node = new_binary(ND_SHL, node, add(&tok, tok->next), start);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal(tok, ">>")) {
|
|
|
|
node = new_binary(ND_SHR, node, add(&tok, tok->next), 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
|
2020-09-22 12:29:17 +03:00
|
|
|
if (is_numeric(lhs->ty) && is_numeric(rhs->ty))
|
2020-09-04 07:39:06 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-03 16:27:13 +03:00
|
|
|
// VLA + num
|
|
|
|
if (lhs->ty->base->kind == TY_VLA) {
|
|
|
|
rhs = new_binary(ND_MUL, rhs, new_var_node(lhs->ty->base->vla_size, tok), tok);
|
|
|
|
return new_binary(ND_ADD, lhs, rhs, tok);
|
|
|
|
}
|
|
|
|
|
2020-09-04 07:39:06 +03:00
|
|
|
// 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
|
2020-09-22 12:29:17 +03:00
|
|
|
if (is_numeric(lhs->ty) && is_numeric(rhs->ty))
|
2020-09-04 07:39:06 +03:00
|
|
|
return new_binary(ND_SUB, lhs, rhs, tok);
|
|
|
|
|
2020-09-03 16:27:13 +03:00
|
|
|
// VLA + num
|
|
|
|
if (lhs->ty->base->kind == TY_VLA) {
|
|
|
|
rhs = new_binary(ND_MUL, rhs, new_var_node(lhs->ty->base->vla_size, tok), tok);
|
|
|
|
add_type(rhs);
|
|
|
|
Node *node = new_binary(ND_SUB, lhs, rhs, tok);
|
|
|
|
node->ty = lhs->ty;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-09-04 07:39:06 +03:00
|
|
|
// 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);
|
2020-03-24 16:18:59 +03:00
|
|
|
node->ty = ty_long;
|
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, ")");
|
2019-08-24 01:45:14 +03:00
|
|
|
|
|
|
|
// compound literal
|
|
|
|
if (equal(tok, "{"))
|
|
|
|
return unary(rest, start);
|
|
|
|
|
|
|
|
// type cast
|
2019-08-11 10:06:14 +03:00
|
|
|
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
|
2020-09-04 06:26:27 +03:00
|
|
|
// | "&&" ident
|
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
|
|
|
|
2020-08-23 06:38:17 +03:00
|
|
|
if (equal(tok, "&")) {
|
|
|
|
Node *lhs = cast(rest, tok->next);
|
|
|
|
add_type(lhs);
|
|
|
|
if (lhs->kind == ND_MEMBER && lhs->member->is_bitfield)
|
|
|
|
error_tok(tok, "cannot take address of bitfield");
|
|
|
|
return new_unary(ND_ADDR, lhs, tok);
|
|
|
|
}
|
2019-08-05 15:12:44 +03:00
|
|
|
|
2020-04-23 11:18:49 +03:00
|
|
|
if (equal(tok, "*")) {
|
|
|
|
// [https://www.sigbus.info/n1570#6.5.3.2p4] This is an oddity
|
|
|
|
// in the C spec, but dereferencing a function shouldn't do
|
|
|
|
// anything. If foo is a function, `*foo`, `**foo` or `*****foo`
|
|
|
|
// are all equivalent to just `foo`.
|
|
|
|
Node *node = cast(rest, tok->next);
|
|
|
|
add_type(node);
|
|
|
|
if (node->ty->kind == TY_FUNC)
|
|
|
|
return node;
|
|
|
|
return new_unary(ND_DEREF, node, 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));
|
|
|
|
|
2020-09-04 06:26:27 +03:00
|
|
|
// [GNU] labels-as-values
|
|
|
|
if (equal(tok, "&&")) {
|
|
|
|
Node *node = new_node(ND_LABEL_VAL, tok);
|
|
|
|
node->label = get_ident(tok->next);
|
|
|
|
node->goto_next = gotos;
|
|
|
|
gotos = node;
|
|
|
|
*rest = tok->next->next;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
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;
|
2019-08-18 11:56:36 +03:00
|
|
|
int idx = 0;
|
2019-08-08 16:43:58 +03:00
|
|
|
|
|
|
|
while (!equal(tok, "}")) {
|
2020-09-04 05:20:55 +03:00
|
|
|
VarAttr attr = {};
|
|
|
|
Type *basety = declspec(&tok, tok, &attr);
|
2019-08-18 11:56:36 +03:00
|
|
|
bool first = true;
|
2019-08-08 16:43:58 +03:00
|
|
|
|
2020-08-23 05:48:16 +03:00
|
|
|
// Anonymous struct member
|
|
|
|
if ((basety->kind == TY_STRUCT || basety->kind == TY_UNION) &&
|
|
|
|
consume(&tok, tok, ";")) {
|
|
|
|
Member *mem = calloc(1, sizeof(Member));
|
|
|
|
mem->ty = basety;
|
|
|
|
mem->idx = idx++;
|
|
|
|
mem->align = attr.align ? attr.align : mem->ty->align;
|
|
|
|
cur = cur->next = mem;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regular struct members
|
2019-08-08 16:43:58 +03:00
|
|
|
while (!consume(&tok, tok, ";")) {
|
2019-08-18 11:56:36 +03:00
|
|
|
if (!first)
|
2019-08-08 16:43:58 +03:00
|
|
|
tok = skip(tok, ",");
|
2019-08-18 11:56:36 +03:00
|
|
|
first = false;
|
2019-08-08 16:43:58 +03:00
|
|
|
|
|
|
|
Member *mem = calloc(1, sizeof(Member));
|
|
|
|
mem->ty = declarator(&tok, tok, basety);
|
|
|
|
mem->name = mem->ty->name;
|
2019-08-18 11:56:36 +03:00
|
|
|
mem->idx = idx++;
|
2020-09-04 05:20:55 +03:00
|
|
|
mem->align = attr.align ? attr.align : mem->ty->align;
|
2020-08-27 17:36:12 +03:00
|
|
|
|
|
|
|
if (consume(&tok, tok, ":")) {
|
|
|
|
mem->is_bitfield = true;
|
|
|
|
mem->bit_width = const_expr(&tok, tok);
|
|
|
|
}
|
|
|
|
|
2019-08-08 16:43:58 +03:00
|
|
|
cur = cur->next = mem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-20 10:11:55 +03:00
|
|
|
// If the last element is an array of incomplete type, it's
|
|
|
|
// called a "flexible array member". It should behave as if
|
|
|
|
// if were a zero-sized array.
|
2020-09-20 10:16:00 +03:00
|
|
|
if (cur != &head && cur->ty->kind == TY_ARRAY && cur->ty->array_len < 0) {
|
2020-09-20 10:11:55 +03:00
|
|
|
cur->ty = array_of(cur->ty->base, 0);
|
2020-09-20 10:16:00 +03:00
|
|
|
ty->is_flexible = true;
|
|
|
|
}
|
2020-09-20 10:11:55 +03:00
|
|
|
|
2019-08-08 16:43:58 +03:00
|
|
|
*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.
|
2020-10-07 05:13:22 +03:00
|
|
|
Type *ty2 = hashmap_get2(&scope->tags, tag->loc, tag->len);
|
|
|
|
if (ty2) {
|
|
|
|
*ty2 = *ty;
|
|
|
|
return ty2;
|
2020-10-07 05:10:28 +03:00
|
|
|
}
|
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.
|
2020-08-27 17:36:12 +03:00
|
|
|
int bits = 0;
|
|
|
|
|
2019-08-08 16:43:58 +03:00
|
|
|
for (Member *mem = ty->members; mem; mem = mem->next) {
|
2020-08-23 05:47:16 +03:00
|
|
|
if (mem->is_bitfield && mem->bit_width == 0) {
|
|
|
|
// Zero-width anonymous bitfield has a special meaning.
|
|
|
|
// It affects only alignment.
|
|
|
|
bits = align_to(bits, mem->ty->size * 8);
|
|
|
|
} else if (mem->is_bitfield) {
|
2020-08-27 17:36:12 +03:00
|
|
|
int sz = mem->ty->size;
|
|
|
|
if (bits / (sz * 8) != (bits + mem->bit_width - 1) / (sz * 8))
|
|
|
|
bits = align_to(bits, sz * 8);
|
|
|
|
|
|
|
|
mem->offset = align_down(bits / 8, sz);
|
|
|
|
mem->bit_offset = bits % (sz * 8);
|
|
|
|
bits += mem->bit_width;
|
|
|
|
} else {
|
|
|
|
bits = align_to(bits, mem->align * 8);
|
|
|
|
mem->offset = bits / 8;
|
|
|
|
bits += mem->ty->size * 8;
|
|
|
|
}
|
2020-08-30 11:21:54 +03:00
|
|
|
|
2020-09-04 05:20:55 +03:00
|
|
|
if (ty->align < mem->align)
|
|
|
|
ty->align = mem->align;
|
2019-08-08 16:43:58 +03:00
|
|
|
}
|
2020-08-27 17:36:12 +03:00
|
|
|
|
|
|
|
ty->size = align_to(bits, ty->align * 8) / 8;
|
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) {
|
2020-09-04 05:20:55 +03:00
|
|
|
if (ty->align < mem->align)
|
|
|
|
ty->align = mem->align;
|
2020-04-02 15:54:47 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-08-23 05:48:16 +03:00
|
|
|
// Find a struct member by name.
|
2019-08-08 16:43:58 +03:00
|
|
|
static Member *get_struct_member(Type *ty, Token *tok) {
|
2020-08-23 05:48:16 +03:00
|
|
|
for (Member *mem = ty->members; mem; mem = mem->next) {
|
|
|
|
// Anonymous struct member
|
|
|
|
if ((mem->ty->kind == TY_STRUCT || mem->ty->kind == TY_UNION) &&
|
|
|
|
!mem->name) {
|
|
|
|
if (get_struct_member(mem->ty, tok))
|
|
|
|
return mem;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regular struct member
|
2019-08-08 16:43:58 +03:00
|
|
|
if (mem->name->len == tok->len &&
|
|
|
|
!strncmp(mem->name->loc, tok->loc, tok->len))
|
|
|
|
return mem;
|
2020-08-23 05:48:16 +03:00
|
|
|
}
|
|
|
|
return NULL;
|
2019-08-08 16:43:58 +03:00
|
|
|
}
|
|
|
|
|
2020-08-23 05:48:16 +03:00
|
|
|
// Create a node representing a struct member access, such as foo.bar
|
|
|
|
// where foo is a struct and bar is a member name.
|
|
|
|
//
|
|
|
|
// C has a feature called "anonymous struct" which allows a struct to
|
|
|
|
// have another unnamed struct as a member like this:
|
|
|
|
//
|
|
|
|
// struct { struct { int a; }; int b; } x;
|
|
|
|
//
|
|
|
|
// The members of an anonymous struct belong to the outer struct's
|
|
|
|
// member namespace. Therefore, in the above example, you can access
|
|
|
|
// member "a" of the anonymous struct as "x.a".
|
|
|
|
//
|
|
|
|
// This function takes care of anonymous structs.
|
|
|
|
static Node *struct_ref(Node *node, Token *tok) {
|
|
|
|
add_type(node);
|
|
|
|
if (node->ty->kind != TY_STRUCT && node->ty->kind != TY_UNION)
|
|
|
|
error_tok(node->tok, "not a struct nor a union");
|
|
|
|
|
|
|
|
Type *ty = node->ty;
|
2019-08-08 16:43:58 +03:00
|
|
|
|
2020-08-23 05:48:16 +03:00
|
|
|
for (;;) {
|
|
|
|
Member *mem = get_struct_member(ty, tok);
|
|
|
|
if (!mem)
|
|
|
|
error_tok(tok, "no such member");
|
|
|
|
node = new_unary(ND_MEMBER, node, tok);
|
|
|
|
node->member = mem;
|
|
|
|
if (mem->name)
|
|
|
|
break;
|
|
|
|
ty = mem->ty;
|
|
|
|
}
|
2019-08-08 16:43:58 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-08-24 01:45:14 +03:00
|
|
|
// postfix = "(" type-name ")" "{" initializer-list "}"
|
2020-09-03 12:50:56 +03:00
|
|
|
// = ident "(" func-args ")" postfix-tail*
|
|
|
|
// | primary postfix-tail*
|
|
|
|
//
|
|
|
|
// postfix-tail = "[" expr "]"
|
|
|
|
// | "(" func-args ")"
|
|
|
|
// | "." ident
|
|
|
|
// | "->" ident
|
|
|
|
// | "++"
|
|
|
|
// | "--"
|
2019-08-06 12:14:53 +03:00
|
|
|
static Node *postfix(Token **rest, Token *tok) {
|
2019-08-24 01:45:14 +03:00
|
|
|
if (equal(tok, "(") && is_typename(tok->next)) {
|
|
|
|
// Compound literal
|
|
|
|
Token *start = tok;
|
|
|
|
Type *ty = typename(&tok, tok->next);
|
|
|
|
tok = skip(tok, ")");
|
|
|
|
|
|
|
|
if (scope->next == NULL) {
|
|
|
|
Obj *var = new_anon_gvar(ty);
|
|
|
|
gvar_initializer(rest, tok, var);
|
|
|
|
return new_var_node(var, start);
|
|
|
|
}
|
|
|
|
|
|
|
|
Obj *var = new_lvar("", ty);
|
|
|
|
Node *lhs = lvar_initializer(rest, tok, var);
|
|
|
|
Node *rhs = new_var_node(var, tok);
|
|
|
|
return new_binary(ND_COMMA, lhs, rhs, start);
|
|
|
|
}
|
|
|
|
|
2019-08-06 12:14:53 +03:00
|
|
|
Node *node = primary(&tok, tok);
|
|
|
|
|
2019-08-08 16:43:58 +03:00
|
|
|
for (;;) {
|
2020-09-03 12:50:56 +03:00
|
|
|
if (equal(tok, "(")) {
|
|
|
|
node = funcall(&tok, tok->next, node);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-08-08 16:43:58 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-03 12:50:56 +03:00
|
|
|
// funcall = (assign ("," assign)*)? ")"
|
|
|
|
static Node *funcall(Token **rest, Token *tok, Node *fn) {
|
|
|
|
add_type(fn);
|
2019-08-04 13:03:46 +03:00
|
|
|
|
2020-09-03 12:50:56 +03:00
|
|
|
if (fn->ty->kind != TY_FUNC &&
|
|
|
|
(fn->ty->kind != TY_PTR || fn->ty->base->kind != TY_FUNC))
|
|
|
|
error_tok(fn->tok, "not a function");
|
2020-08-21 11:16:28 +03:00
|
|
|
|
2020-09-03 12:50:56 +03:00
|
|
|
Type *ty = (fn->ty->kind == TY_FUNC) ? fn->ty : fn->ty->base;
|
2020-03-22 12:43:16 +03:00
|
|
|
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);
|
|
|
|
|
2020-09-26 08:29:06 +03:00
|
|
|
if (!param_ty && !ty->is_variadic)
|
|
|
|
error_tok(tok, "too many arguments");
|
|
|
|
|
2020-03-22 12:43:16 +03:00
|
|
|
if (param_ty) {
|
2020-08-27 17:14:58 +03:00
|
|
|
if (param_ty->kind != TY_STRUCT && param_ty->kind != TY_UNION)
|
|
|
|
arg = new_cast(arg, param_ty);
|
2020-03-22 12:43:16 +03:00
|
|
|
param_ty = param_ty->next;
|
2020-04-30 17:33:27 +03:00
|
|
|
} else if (arg->ty->kind == TY_FLOAT) {
|
|
|
|
// If parameter type is omitted (e.g. in "..."), float
|
|
|
|
// arguments are promoted to double.
|
|
|
|
arg = new_cast(arg, ty_double);
|
2020-03-22 12:43:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cur = cur->next = arg;
|
2019-08-04 13:03:46 +03:00
|
|
|
}
|
|
|
|
|
2020-09-26 08:29:06 +03:00
|
|
|
if (param_ty)
|
|
|
|
error_tok(tok, "too few arguments");
|
|
|
|
|
2019-08-04 13:03:46 +03:00
|
|
|
*rest = skip(tok, ")");
|
|
|
|
|
2020-09-03 12:50:56 +03:00
|
|
|
Node *node = new_unary(ND_FUNCALL, fn, tok);
|
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;
|
2020-08-27 17:16:53 +03:00
|
|
|
|
|
|
|
// If a function returns a struct, it is caller's responsibility
|
|
|
|
// to allocate a space for the return value.
|
|
|
|
if (node->ty->kind == TY_STRUCT || node->ty->kind == TY_UNION)
|
|
|
|
node->ret_buffer = new_lvar("", node->ty);
|
2019-08-04 13:03:46 +03:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-07-24 10:18:03 +03:00
|
|
|
// generic-selection = "(" assign "," generic-assoc ("," generic-assoc)* ")"
|
|
|
|
//
|
|
|
|
// generic-assoc = type-name ":" assign
|
|
|
|
// | "default" ":" assign
|
|
|
|
static Node *generic_selection(Token **rest, Token *tok) {
|
|
|
|
Token *start = tok;
|
|
|
|
tok = skip(tok, "(");
|
|
|
|
|
|
|
|
Node *ctrl = assign(&tok, tok);
|
|
|
|
add_type(ctrl);
|
|
|
|
|
|
|
|
Type *t1 = ctrl->ty;
|
|
|
|
if (t1->kind == TY_FUNC)
|
|
|
|
t1 = pointer_to(t1);
|
|
|
|
else if (t1->kind == TY_ARRAY)
|
|
|
|
t1 = pointer_to(t1->base);
|
|
|
|
|
|
|
|
Node *ret = NULL;
|
|
|
|
|
|
|
|
while (!consume(rest, tok, ")")) {
|
|
|
|
tok = skip(tok, ",");
|
|
|
|
|
|
|
|
if (equal(tok, "default")) {
|
|
|
|
tok = skip(tok->next, ":");
|
|
|
|
Node *node = assign(&tok, tok);
|
|
|
|
if (!ret)
|
|
|
|
ret = node;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Type *t2 = typename(&tok, tok);
|
|
|
|
tok = skip(tok, ":");
|
|
|
|
Node *node = assign(&tok, tok);
|
|
|
|
if (is_compatible(t1, t2))
|
|
|
|
ret = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
error_tok(start, "controlling expression type not compatible with"
|
|
|
|
" any generic association type");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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
|
2020-09-04 05:20:55 +03:00
|
|
|
// | "_Alignof" "(" type-name ")"
|
2020-09-26 02:09:59 +03:00
|
|
|
// | "_Alignof" unary
|
2020-07-24 10:18:03 +03:00
|
|
|
// | "_Generic" generic-selection
|
2020-09-06 15:06:56 +03:00
|
|
|
// | "__builtin_types_compatible_p" "(" type-name, type-name, ")"
|
2020-08-16 08:27:35 +03:00
|
|
|
// | "__builtin_reg_class" "(" type-name ")"
|
2020-09-03 12:50:56 +03:00
|
|
|
// | ident
|
2019-08-07 02:05:18 +03:00
|
|
|
// | 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, ")");
|
2020-08-25 03:34:41 +03:00
|
|
|
|
|
|
|
if (ty->kind == TY_VLA) {
|
|
|
|
if (ty->vla_size)
|
|
|
|
return new_var_node(ty->vla_size, tok);
|
|
|
|
|
|
|
|
Node *lhs = compute_vla_size(ty, tok);
|
|
|
|
Node *rhs = new_var_node(ty->vla_size, tok);
|
|
|
|
return new_binary(ND_COMMA, lhs, rhs, tok);
|
|
|
|
}
|
|
|
|
|
2020-03-24 16:18:59 +03:00
|
|
|
return new_ulong(ty->size, start);
|
2019-08-11 09:04:35 +03:00
|
|
|
}
|
|
|
|
|
2019-08-06 12:44:53 +03:00
|
|
|
if (equal(tok, "sizeof")) {
|
|
|
|
Node *node = unary(rest, tok->next);
|
|
|
|
add_type(node);
|
2020-09-04 08:44:12 +03:00
|
|
|
if (node->ty->kind == TY_VLA)
|
|
|
|
return new_var_node(node->ty->vla_size, tok);
|
2020-03-24 16:18:59 +03:00
|
|
|
return new_ulong(node->ty->size, tok);
|
2019-08-06 12:44:53 +03:00
|
|
|
}
|
|
|
|
|
2020-09-26 02:09:59 +03:00
|
|
|
if (equal(tok, "_Alignof") && equal(tok->next, "(") && is_typename(tok->next->next)) {
|
|
|
|
Type *ty = typename(&tok, tok->next->next);
|
2020-09-04 05:20:55 +03:00
|
|
|
*rest = skip(tok, ")");
|
2020-03-24 16:18:59 +03:00
|
|
|
return new_ulong(ty->align, tok);
|
2020-09-04 05:20:55 +03:00
|
|
|
}
|
|
|
|
|
2020-09-26 02:09:59 +03:00
|
|
|
if (equal(tok, "_Alignof")) {
|
|
|
|
Node *node = unary(rest, tok->next);
|
|
|
|
add_type(node);
|
2020-03-24 16:18:59 +03:00
|
|
|
return new_ulong(node->ty->align, tok);
|
2020-09-26 02:09:59 +03:00
|
|
|
}
|
|
|
|
|
2020-07-24 10:18:03 +03:00
|
|
|
if (equal(tok, "_Generic"))
|
|
|
|
return generic_selection(rest, tok->next);
|
|
|
|
|
2020-09-06 15:06:56 +03:00
|
|
|
if (equal(tok, "__builtin_types_compatible_p")) {
|
|
|
|
tok = skip(tok->next, "(");
|
|
|
|
Type *t1 = typename(&tok, tok);
|
|
|
|
tok = skip(tok, ",");
|
|
|
|
Type *t2 = typename(&tok, tok);
|
|
|
|
*rest = skip(tok, ")");
|
|
|
|
return new_num(is_compatible(t1, t2), start);
|
|
|
|
}
|
|
|
|
|
2020-08-16 08:27:35 +03:00
|
|
|
if (equal(tok, "__builtin_reg_class")) {
|
|
|
|
tok = skip(tok->next, "(");
|
|
|
|
Type *ty = typename(&tok, tok);
|
|
|
|
*rest = skip(tok, ")");
|
|
|
|
|
|
|
|
if (is_integer(ty) || ty->kind == TY_PTR)
|
|
|
|
return new_num(0, start);
|
|
|
|
if (is_flonum(ty))
|
|
|
|
return new_num(1, start);
|
|
|
|
return new_num(2, start);
|
|
|
|
}
|
|
|
|
|
2020-09-15 05:30:56 +03:00
|
|
|
if (equal(tok, "__builtin_compare_and_swap")) {
|
|
|
|
Node *node = new_node(ND_CAS, tok);
|
|
|
|
tok = skip(tok->next, "(");
|
|
|
|
node->cas_addr = assign(&tok, tok);
|
|
|
|
tok = skip(tok, ",");
|
|
|
|
node->cas_old = assign(&tok, tok);
|
|
|
|
tok = skip(tok, ",");
|
|
|
|
node->cas_new = assign(&tok, tok);
|
|
|
|
*rest = skip(tok, ")");
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-09-15 05:30:56 +03:00
|
|
|
if (equal(tok, "__builtin_atomic_exchange")) {
|
|
|
|
Node *node = new_node(ND_EXCH, tok);
|
|
|
|
tok = skip(tok->next, "(");
|
|
|
|
node->lhs = assign(&tok, tok);
|
|
|
|
tok = skip(tok, ",");
|
|
|
|
node->rhs = assign(&tok, tok);
|
|
|
|
*rest = skip(tok, ")");
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-09-26 02:59:56 +03:00
|
|
|
if (tok->kind == TK_IDENT) {
|
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);
|
2020-09-03 12:50:56 +03:00
|
|
|
*rest = tok->next;
|
2019-08-11 13:59:27 +03:00
|
|
|
|
2020-09-13 04:49:25 +03:00
|
|
|
// For "static inline" function
|
|
|
|
if (sc && sc->var && sc->var->is_function) {
|
|
|
|
if (current_fn)
|
|
|
|
strarray_push(¤t_fn->refs, sc->var->name);
|
|
|
|
else
|
|
|
|
sc->var->is_root = true;
|
|
|
|
}
|
|
|
|
|
2020-09-03 12:50:56 +03:00
|
|
|
if (sc) {
|
|
|
|
if (sc->var)
|
|
|
|
return new_var_node(sc->var, tok);
|
|
|
|
if (sc->enum_ty)
|
|
|
|
return new_num(sc->enum_val, tok);
|
|
|
|
}
|
2019-08-11 13:59:27 +03:00
|
|
|
|
2020-09-03 12:50:56 +03:00
|
|
|
if (equal(tok->next, "("))
|
|
|
|
error_tok(tok, "implicit declaration of a function");
|
|
|
|
error_tok(tok, "undefined variable");
|
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-27 13:43:03 +03:00
|
|
|
Node *node;
|
|
|
|
if (is_flonum(tok->ty)) {
|
|
|
|
node = new_node(ND_NUM, tok);
|
|
|
|
node->fval = tok->fval;
|
|
|
|
} else {
|
|
|
|
node = new_num(tok->val, tok);
|
|
|
|
}
|
|
|
|
|
2019-09-21 10:17:40 +03:00
|
|
|
node->ty = tok->ty;
|
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);
|
2020-09-04 11:28:20 +03:00
|
|
|
if (!ty->name)
|
|
|
|
error_tok(ty->name_pos, "typedef name omitted");
|
2020-09-04 11:23:26 +03:00
|
|
|
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);
|
2020-09-04 11:28:20 +03:00
|
|
|
if (!param->name)
|
|
|
|
error_tok(param->name_pos, "parameter name omitted");
|
2020-09-04 07:39:48 +03:00
|
|
|
new_lvar(get_ident(param->name), param);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-04 06:26:27 +03:00
|
|
|
// This function matches gotos or labels-as-values with labels.
|
2020-09-04 06:27:21 +03:00
|
|
|
//
|
|
|
|
// 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-13 04:49:25 +03:00
|
|
|
static Obj *find_func(char *name) {
|
|
|
|
Scope *sc = scope;
|
|
|
|
while (sc->next)
|
|
|
|
sc = sc->next;
|
|
|
|
|
2020-10-07 05:13:22 +03:00
|
|
|
VarScope *sc2 = hashmap_get(&sc->vars, name);
|
|
|
|
if (sc2 && sc2->var && sc2->var->is_function)
|
|
|
|
return sc2->var;
|
2020-09-13 04:49:25 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mark_live(Obj *var) {
|
|
|
|
if (!var->is_function || var->is_live)
|
|
|
|
return;
|
|
|
|
var->is_live = true;
|
|
|
|
|
|
|
|
for (int i = 0; i < var->refs.len; i++) {
|
|
|
|
Obj *fn = find_func(var->refs.data[i]);
|
|
|
|
if (fn)
|
|
|
|
mark_live(fn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 11:28:20 +03:00
|
|
|
if (!ty->name)
|
|
|
|
error_tok(ty->name_pos, "function name omitted");
|
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 06:54:07 +03:00
|
|
|
fn->is_static = attr->is_static || (attr->is_inline && !attr->is_extern);
|
|
|
|
fn->is_inline = attr->is_inline;
|
2020-09-13 04:49:25 +03:00
|
|
|
fn->is_root = !(fn->is_static && fn->is_inline);
|
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);
|
2020-09-04 05:57:15 +03:00
|
|
|
|
|
|
|
// A buffer for a struct/union return value is passed
|
|
|
|
// as the hidden first parameter.
|
|
|
|
Type *rty = ty->return_ty;
|
|
|
|
if ((rty->kind == TY_STRUCT || rty->kind == TY_UNION) && rty->size > 16)
|
|
|
|
new_lvar("", pointer_to(rty));
|
|
|
|
|
2020-09-04 07:39:48 +03:00
|
|
|
fn->params = locals;
|
2020-09-04 05:59:38 +03:00
|
|
|
|
2019-08-25 05:48:44 +03:00
|
|
|
if (ty->is_variadic)
|
|
|
|
fn->va_area = new_lvar("__va_area__", array_of(ty_char, 136));
|
2020-09-04 05:59:38 +03:00
|
|
|
fn->alloca_bottom = new_lvar("__alloca_size__", pointer_to(ty_char));
|
2020-09-04 13:01:33 +03:00
|
|
|
|
2020-09-04 07:38:41 +03:00
|
|
|
tok = skip(tok, "{");
|
2020-09-04 05:55:28 +03:00
|
|
|
|
|
|
|
// [https://www.sigbus.info/n1570#6.4.2.2p1] "__func__" is
|
|
|
|
// automatically defined as a local variable containing the
|
|
|
|
// current function name.
|
|
|
|
push_scope("__func__")->var =
|
|
|
|
new_string_literal(fn->name, array_of(ty_char, strlen(fn->name) + 1));
|
|
|
|
|
2020-09-04 05:56:28 +03:00
|
|
|
// [GNU] __FUNCTION__ is yet another name of __func__.
|
|
|
|
push_scope("__FUNCTION__")->var =
|
|
|
|
new_string_literal(fn->name, array_of(ty_char, strlen(fn->name) + 1));
|
|
|
|
|
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-04 11:27:22 +03:00
|
|
|
static Token *global_variable(Token *tok, Type *basety, VarAttr *attr) {
|
2020-09-05 02:43:21 +03:00
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
while (!consume(&tok, tok, ";")) {
|
|
|
|
if (!first)
|
|
|
|
tok = skip(tok, ",");
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
Type *ty = declarator(&tok, tok, basety);
|
2020-09-04 11:28:20 +03:00
|
|
|
if (!ty->name)
|
|
|
|
error_tok(ty->name_pos, "variable name omitted");
|
|
|
|
|
2020-09-04 11:26:34 +03:00
|
|
|
Obj *var = new_gvar(get_ident(ty->name), ty);
|
2020-09-04 11:27:22 +03:00
|
|
|
var->is_definition = !attr->is_extern;
|
2020-09-04 05:21:12 +03:00
|
|
|
var->is_static = attr->is_static;
|
2020-09-07 04:02:10 +03:00
|
|
|
var->is_tls = attr->is_tls;
|
2020-09-04 05:20:55 +03:00
|
|
|
if (attr->align)
|
|
|
|
var->align = attr->align;
|
2020-09-04 11:27:22 +03:00
|
|
|
|
2020-09-04 11:26:34 +03:00
|
|
|
if (equal(tok, "="))
|
|
|
|
gvar_initializer(&tok, tok->next, var);
|
2020-09-07 04:02:10 +03:00
|
|
|
else if (!attr->is_extern && !attr->is_tls)
|
2020-09-04 11:29:15 +03:00
|
|
|
var->is_tentative = true;
|
2020-09-05 02:43:21 +03:00
|
|
|
}
|
|
|
|
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:29:15 +03:00
|
|
|
// Remove redundant tentative definitions.
|
|
|
|
static void scan_globals(void) {
|
|
|
|
Obj head;
|
|
|
|
Obj *cur = &head;
|
|
|
|
|
|
|
|
for (Obj *var = globals; var; var = var->next) {
|
|
|
|
if (!var->is_tentative) {
|
|
|
|
cur = cur->next = var;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find another definition of the same identifier.
|
|
|
|
Obj *var2 = globals;
|
|
|
|
for (; var2; var2 = var2->next)
|
|
|
|
if (var != var2 && var2->is_definition && !strcmp(var->name, var2->name))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// If there's another definition, the tentative definition
|
|
|
|
// is redundant
|
|
|
|
if (!var2)
|
|
|
|
cur = cur->next = var;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur->next = NULL;
|
|
|
|
globals = head.next;
|
|
|
|
}
|
|
|
|
|
2020-09-04 05:59:38 +03:00
|
|
|
static void declare_builtin_functions(void) {
|
|
|
|
Type *ty = func_type(pointer_to(ty_void));
|
|
|
|
ty->params = copy_type(ty_int);
|
2020-09-04 08:44:12 +03:00
|
|
|
builtin_alloca = new_gvar("alloca", ty);
|
|
|
|
builtin_alloca->is_definition = false;
|
2020-09-04 05:59:38 +03:00
|
|
|
}
|
|
|
|
|
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) {
|
2020-09-04 05:59:38 +03:00
|
|
|
declare_builtin_functions();
|
2020-09-04 11:58:53 +03:00
|
|
|
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
|
2020-09-04 11:27:22 +03:00
|
|
|
tok = global_variable(tok, basety, &attr);
|
2020-09-04 11:58:53 +03:00
|
|
|
}
|
2020-09-13 04:49:25 +03:00
|
|
|
|
|
|
|
for (Obj *var = globals; var; var = var->next)
|
|
|
|
if (var->is_root)
|
|
|
|
mark_live(var);
|
|
|
|
|
2020-09-04 11:29:15 +03:00
|
|
|
// Remove redundant tentative definitions.
|
|
|
|
scan_globals();
|
2020-09-04 11:58:53 +03:00
|
|
|
return globals;
|
2020-10-07 14:11:16 +03:00
|
|
|
}
|