chibicc/tokenize.c

535 lines
12 KiB
C
Raw Normal View History

2020-10-07 14:11:16 +03:00
#include "chibicc.h"
2020-09-03 13:24:23 +03:00
// Input file
static File *current_file;
2020-09-03 13:24:23 +03:00
// A list of all input files.
static File **input_files;
2020-10-07 14:11:16 +03:00
2020-03-30 03:30:06 +03:00
// True if the current position is at the beginning of a line
static bool at_bol;
2020-10-07 14:11:16 +03:00
// Reports an error and exit.
void error(char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
exit(1);
}
// Reports an error message in the following format.
//
// foo.c:10: x = y + 1;
// ^ <error message here>
2020-09-03 13:24:23 +03:00
static void verror_at(char *filename, char *input, int line_no,
char *loc, char *fmt, va_list ap) {
// Find a line containing `loc`.
char *line = loc;
2020-09-03 13:24:23 +03:00
while (input < line && line[-1] != '\n')
line--;
char *end = loc;
while (*end != '\n')
end++;
// Print out the line.
2020-09-03 13:24:23 +03:00
int indent = fprintf(stderr, "%s:%d: ", filename, line_no);
fprintf(stderr, "%.*s\n", (int)(end - line), line);
// Show the error message.
int pos = loc - line + indent;
2020-10-07 14:11:16 +03:00
fprintf(stderr, "%*s", pos, ""); // print pos spaces.
fprintf(stderr, "^ ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
}
void error_at(char *loc, char *fmt, ...) {
int line_no = 1;
2020-09-03 13:24:23 +03:00
for (char *p = current_file->contents; p < loc; p++)
if (*p == '\n')
line_no++;
2020-10-07 14:11:16 +03:00
va_list ap;
va_start(ap, fmt);
2020-09-03 13:24:23 +03:00
verror_at(current_file->name, current_file->contents, line_no, loc, fmt, ap);
exit(1);
2020-10-07 14:11:16 +03:00
}
void error_tok(Token *tok, char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
2020-09-03 13:24:23 +03:00
verror_at(tok->file->name, tok->file->contents, tok->line_no, tok->loc, fmt, ap);
exit(1);
2020-10-07 14:11:16 +03:00
}
// Consumes the current token if it matches `op`.
bool equal(Token *tok, char *op) {
return memcmp(tok->loc, op, tok->len) == 0 && op[tok->len] == '\0';
}
// Ensure that the current token is `op`.
Token *skip(Token *tok, char *op) {
if (!equal(tok, op))
error_tok(tok, "expected '%s'", op);
return tok->next;
}
bool consume(Token **rest, Token *tok, char *str) {
if (equal(tok, str)) {
*rest = tok->next;
return true;
}
*rest = tok;
return false;
}
2020-10-07 14:11:16 +03:00
// Create a new token.
static Token *new_token(TokenKind kind, char *start, char *end) {
Token *tok = calloc(1, sizeof(Token));
tok->kind = kind;
tok->loc = start;
tok->len = end - start;
2020-09-03 13:24:23 +03:00
tok->file = current_file;
2020-03-30 03:30:06 +03:00
tok->at_bol = at_bol;
at_bol = false;
2020-10-07 14:11:16 +03:00
return tok;
}
static bool startswith(char *p, char *q) {
return strncmp(p, q, strlen(q)) == 0;
}
2020-10-07 14:12:19 +03:00
// Returns true if c is valid as the first character of an identifier.
static bool is_ident1(char c) {
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_';
}
// Returns true if c is valid as a non-first character of an identifier.
static bool is_ident2(char c) {
return is_ident1(c) || ('0' <= c && c <= '9');
}
2020-10-07 14:14:14 +03:00
static int from_hex(char c) {
if ('0' <= c && c <= '9')
return c - '0';
if ('a' <= c && c <= 'f')
return c - 'a' + 10;
return c - 'A' + 10;
}
2020-10-07 14:11:16 +03:00
// Read a punctuator token from p and returns its length.
static int read_punct(char *p) {
2020-10-07 14:17:30 +03:00
static char *kw[] = {
2020-10-07 14:24:13 +03:00
"<<=", ">>=", "...", "==", "!=", "<=", ">=", "->", "+=",
2020-10-07 14:23:22 +03:00
"-=", "*=", "/=", "++", "--", "%=", "&=", "|=", "^=", "&&",
"||", "<<", ">>",
2020-10-07 14:17:30 +03:00
};
2020-10-07 14:16:40 +03:00
for (int i = 0; i < sizeof(kw) / sizeof(*kw); i++)
if (startswith(p, kw[i]))
return strlen(kw[i]);
2020-10-07 14:11:16 +03:00
return ispunct(*p) ? 1 : 0;
}
2020-10-07 06:47:09 +03:00
static bool is_keyword(Token *tok) {
2019-08-06 12:44:53 +03:00
static char *kw[] = {
2020-08-27 15:04:17 +03:00
"return", "if", "else", "for", "while", "int", "sizeof", "char",
"struct", "union", "short", "long", "void", "typedef", "_Bool",
2019-08-15 10:43:24 +03:00
"enum", "static", "goto", "break", "continue", "switch", "case",
2020-10-06 11:08:59 +03:00
"default", "extern", "_Alignof", "_Alignas", "do", "signed",
"unsigned", "const", "volatile", "auto", "register", "restrict",
"__restrict", "__restrict__", "_Noreturn", "float", "double",
2019-08-06 12:44:53 +03:00
};
2020-10-07 06:47:09 +03:00
for (int i = 0; i < sizeof(kw) / sizeof(*kw); i++)
if (equal(tok, kw[i]))
return true;
return false;
}
2020-04-06 02:49:00 +03:00
static int read_escaped_char(char **new_pos, char *p) {
if ('0' <= *p && *p <= '7') {
// Read an octal number.
int c = *p++ - '0';
if ('0' <= *p && *p <= '7') {
c = (c << 3) + (*p++ - '0');
if ('0' <= *p && *p <= '7')
c = (c << 3) + (*p++ - '0');
}
*new_pos = p;
return c;
}
2020-10-07 14:14:14 +03:00
if (*p == 'x') {
// Read a hexadecimal number.
p++;
if (!isxdigit(*p))
error_at(p, "invalid hex escape sequence");
int c = 0;
for (; isxdigit(*p); p++)
c = (c << 4) + from_hex(*p);
*new_pos = p;
return c;
}
2020-04-06 02:49:00 +03:00
*new_pos = p + 1;
2019-08-06 16:36:19 +03:00
// Escape sequences are defined using themselves here. E.g.
// '\n' is implemented using '\n'. This tautological definition
// works because the compiler that compiles our compiler knows
// what '\n' actually is. In other words, we "inherit" the ASCII
// code of '\n' from the compiler that compiles our compiler,
// so we don't have to teach the actual code here.
//
// This fact has huge implications not only for the correctness
// of the compiler but also for the security of the generated code.
// For more info, read "Reflections on Trusting Trust" by Ken Thompson.
// https://github.com/rui314/chibicc/wiki/thompson1984.pdf
switch (*p) {
case 'a': return '\a';
case 'b': return '\b';
case 't': return '\t';
case 'n': return '\n';
case 'v': return '\v';
case 'f': return '\f';
case 'r': return '\r';
// [GNU] \e for the ASCII escape character is a GNU C extension.
case 'e': return 27;
default: return *p;
}
}
// Find a closing double-quote.
static char *string_literal_end(char *p) {
char *start = p;
for (; *p != '"'; p++) {
2020-10-07 06:49:08 +03:00
if (*p == '\n' || *p == '\0')
error_at(start, "unclosed string literal");
2019-08-06 16:36:19 +03:00
if (*p == '\\')
p++;
}
return p;
}
static Token *read_string_literal(char *start) {
char *end = string_literal_end(start + 1);
char *buf = calloc(1, end - start);
int len = 0;
for (char *p = start + 1; p < end;) {
2020-04-06 02:49:00 +03:00
if (*p == '\\')
buf[len++] = read_escaped_char(&p, p + 1);
else
2019-08-06 16:36:19 +03:00
buf[len++] = *p++;
}
2020-10-07 06:49:08 +03:00
2019-08-06 16:36:19 +03:00
Token *tok = new_token(TK_STR, start, end + 1);
tok->ty = array_of(ty_char, len + 1);
tok->str = buf;
2020-10-07 06:49:08 +03:00
return tok;
}
2019-08-11 10:27:43 +03:00
static Token *read_char_literal(char *start) {
char *p = start + 1;
if (*p == '\0')
error_at(start, "unclosed char literal");
char c;
if (*p == '\\')
c = read_escaped_char(&p, p + 1);
else
c = *p++;
char *end = strchr(p, '\'');
if (!end)
error_at(p, "unclosed char literal");
Token *tok = new_token(TK_NUM, start, end + 1);
tok->val = c;
2019-09-21 10:17:40 +03:00
tok->ty = ty_int;
2019-08-11 10:27:43 +03:00
return tok;
}
static Token *read_int_literal(char *start) {
char *p = start;
2019-09-21 10:17:40 +03:00
// Read a binary, octal, decimal or hexadecimal number.
int base = 10;
2019-09-21 10:17:40 +03:00
if (!strncasecmp(p, "0x", 2) && isxdigit(p[2])) {
p += 2;
base = 16;
2019-09-21 10:17:40 +03:00
} else if (!strncasecmp(p, "0b", 2) && (p[2] == '0' || p[2] == '1')) {
p += 2;
base = 2;
} else if (*p == '0') {
base = 8;
}
2020-08-28 17:29:49 +03:00
int64_t val = strtoul(p, &p, base);
2019-09-21 10:17:40 +03:00
// Read U, L or LL suffixes.
bool l = false;
bool u = false;
if (startswith(p, "LLU") || startswith(p, "LLu") ||
startswith(p, "llU") || startswith(p, "llu") ||
startswith(p, "ULL") || startswith(p, "Ull") ||
startswith(p, "uLL") || startswith(p, "ull")) {
p += 3;
l = u = true;
} else if (!strncasecmp(p, "lu", 2) || !strncasecmp(p, "ul", 2)) {
p += 2;
l = u = true;
} else if (startswith(p, "LL") || startswith(p, "ll")) {
p += 2;
l = true;
} else if (*p == 'L' || *p == 'l') {
p++;
l = true;
} else if (*p == 'U' || *p == 'u') {
p++;
u = true;
}
// Infer a type.
Type *ty;
if (base == 10) {
if (l && u)
ty = ty_ulong;
else if (l)
ty = ty_long;
else if (u)
ty = (val >> 32) ? ty_ulong : ty_uint;
else
ty = (val >> 31) ? ty_long : ty_int;
} else {
if (l && u)
ty = ty_ulong;
else if (l)
ty = (val >> 63) ? ty_ulong : ty_long;
else if (u)
ty = (val >> 32) ? ty_ulong : ty_uint;
else if (val >> 63)
ty = ty_ulong;
else if (val >> 32)
ty = ty_long;
else if (val >> 31)
ty = ty_uint;
else
ty = ty_int;
}
Token *tok = new_token(TK_NUM, start, p);
tok->val = val;
2019-09-21 10:17:40 +03:00
tok->ty = ty;
return tok;
}
2020-09-27 13:43:03 +03:00
static Token *read_number(char *start) {
// Try to parse as an integer constant.
Token *tok = read_int_literal(start);
if (!strchr(".eEfF", start[tok->len]))
return tok;
// If it's not an integer, it must be a floating point constant.
char *end;
double val = strtod(start, &end);
Type *ty;
if (*end == 'f' || *end == 'F') {
ty = ty_float;
end++;
} else if (*end == 'l' || *end == 'L') {
ty = ty_double;
end++;
} else {
ty = ty_double;
}
tok = new_token(TK_NUM, start, end);
tok->fval = val;
tok->ty = ty;
return tok;
}
2020-08-18 03:41:59 +03:00
void convert_keywords(Token *tok) {
2020-10-07 14:12:57 +03:00
for (Token *t = tok; t->kind != TK_EOF; t = t->next)
2020-10-07 06:47:09 +03:00
if (is_keyword(t))
2020-10-07 14:12:57 +03:00
t->kind = TK_KEYWORD;
}
// Initialize line info for all tokens.
static void add_line_numbers(Token *tok) {
2020-09-03 13:24:23 +03:00
char *p = current_file->contents;
int n = 1;
do {
if (p == tok->loc) {
tok->line_no = n;
tok = tok->next;
}
if (*p == '\n')
n++;
} while (*p++);
}
2020-10-07 14:12:57 +03:00
// Tokenize a given string and returns new tokens.
2020-09-03 13:24:23 +03:00
static Token *tokenize(File *file) {
current_file = file;
char *p = file->contents;
2020-10-07 14:11:16 +03:00
Token head = {};
Token *cur = &head;
2020-03-30 03:30:06 +03:00
at_bol = true;
2020-10-07 14:11:16 +03:00
while (*p) {
2019-08-07 02:56:28 +03:00
// Skip line comments.
if (startswith(p, "//")) {
p += 2;
while (*p != '\n')
p++;
continue;
}
// Skip block comments.
if (startswith(p, "/*")) {
char *q = strstr(p + 2, "*/");
if (!q)
error_at(p, "unclosed block comment");
p = q + 2;
continue;
}
2020-03-30 03:30:06 +03:00
// Skip newline.
if (*p == '\n') {
p++;
at_bol = true;
continue;
}
2020-10-07 14:11:16 +03:00
// Skip whitespace characters.
if (isspace(*p)) {
p++;
continue;
}
// Numeric literal
2020-09-27 13:43:03 +03:00
if (isdigit(*p) || (*p == '.' && isdigit(p[1]))) {
cur = cur->next = read_number(p);
p += cur->len;
2020-10-07 14:11:16 +03:00
continue;
}
2020-10-07 06:49:08 +03:00
// String literal
if (*p == '"') {
cur = cur->next = read_string_literal(p);
p += cur->len;
continue;
}
2019-08-11 10:27:43 +03:00
// Character literal
if (*p == '\'') {
cur = cur->next = read_char_literal(p);
p += cur->len;
continue;
}
2020-10-07 14:12:57 +03:00
// Identifier or keyword
2020-10-07 14:12:19 +03:00
if (is_ident1(*p)) {
char *start = p;
do {
p++;
} while (is_ident2(*p));
cur = cur->next = new_token(TK_IDENT, start, p);
2020-09-26 02:59:56 +03:00
continue;
}
2020-10-07 14:11:16 +03:00
// Punctuators
int punct_len = read_punct(p);
if (punct_len) {
cur = cur->next = new_token(TK_PUNCT, p, p + punct_len);
p += cur->len;
continue;
}
error_at(p, "invalid token");
}
cur = cur->next = new_token(TK_EOF, p, p);
add_line_numbers(head.next);
2020-10-07 14:11:16 +03:00
return head.next;
}
// Returns the contents of a given file.
static char *read_file(char *path) {
FILE *fp;
if (strcmp(path, "-") == 0) {
// By convention, read from stdin if a given filename is "-".
fp = stdin;
} else {
fp = fopen(path, "r");
if (!fp)
2020-09-03 13:24:23 +03:00
return NULL;
}
char *buf;
size_t buflen;
FILE *out = open_memstream(&buf, &buflen);
// Read the entire file.
for (;;) {
char buf2[4096];
int n = fread(buf2, 1, sizeof(buf2), fp);
if (n == 0)
break;
fwrite(buf2, 1, n, out);
}
if (fp != stdin)
fclose(fp);
// Make sure that the last line is properly terminated with '\n'.
fflush(out);
if (buflen == 0 || buf[buflen - 1] != '\n')
fputc('\n', out);
fputc('\0', out);
fclose(out);
return buf;
}
2020-09-03 13:24:23 +03:00
File **get_input_files(void) {
return input_files;
}
static File *new_file(char *name, int file_no, char *contents) {
File *file = calloc(1, sizeof(File));
file->name = name;
file->file_no = file_no;
file->contents = contents;
return file;
}
Token *tokenize_file(char *path) {
2020-09-03 13:24:23 +03:00
char *p = read_file(path);
if (!p)
return NULL;
static int file_no;
File *file = new_file(path, file_no + 1, p);
// Save the filename for assembler .file directive.
input_files = realloc(input_files, sizeof(char *) * (file_no + 2));
input_files[file_no] = file;
input_files[file_no + 1] = NULL;
file_no++;
return tokenize(file);
}