From 80dbe291635a34dcb466f8b0e25aab2e952494b2 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Sun, 11 Aug 2019 10:20:55 +0900 Subject: [PATCH] Add _Bool type --- chibi.h | 2 ++ codegen.c | 6 ++++++ parse.c | 8 +++++--- tests | 4 ++++ tokenize.c | 2 +- type.c | 5 +++-- 6 files changed, 21 insertions(+), 6 deletions(-) diff --git a/chibi.h b/chibi.h index 86e9d14..2d5aeeb 100644 --- a/chibi.h +++ b/chibi.h @@ -166,6 +166,7 @@ Program *program(void); typedef enum { TY_VOID, + TY_BOOL, TY_CHAR, TY_SHORT, TY_INT, @@ -195,6 +196,7 @@ struct Member { }; extern Type *void_type; +extern Type *bool_type; extern Type *char_type; extern Type *short_type; extern Type *int_type; diff --git a/codegen.c b/codegen.c index 14444cd..a5a9fa4 100644 --- a/codegen.c +++ b/codegen.c @@ -64,6 +64,12 @@ static void store(Type *ty) { printf(" pop rdi\n"); printf(" pop rax\n"); + if (ty->kind == TY_BOOL) { + printf(" cmp rdi, 0\n"); + printf(" setne dil\n"); + printf(" movzb rdi, dil\n"); + } + if (ty->size == 1) { printf(" mov [rax], dil\n"); } else if (ty->size == 2) { diff --git a/parse.c b/parse.c index 6e713e2..d4bcdde 100644 --- a/parse.c +++ b/parse.c @@ -212,13 +212,15 @@ Program *program(void) { } // basetype = builtin-type | struct-decl | typedef-name -// builtin-type = "void" | "char" | "short" | "int" | "long" +// builtin-type = "void" | "_Bool" | "char" | "short" | "int" | "long" static Type *basetype(void) { if (!is_typename(token)) error_tok(token, "typename expected"); if (consume("void")) return void_type; + if (consume("_Bool")) + return bool_type; if (consume("char")) return char_type; if (consume("short")) @@ -438,8 +440,8 @@ static Node *read_expr_stmt(void) { // Returns true if the next token represents a type. static bool is_typename(void) { - return peek("void") || peek("char") || peek("short") || peek("int") || - peek("long") || peek("struct") || find_typedef(token); + return peek("void") || peek("_Bool") || peek("char") || peek("short") || + peek("int") || peek("long") || peek("struct") || find_typedef(token); } static Node *stmt(void) { diff --git a/tests b/tests index 0a7fd1c..c9a3471 100644 --- a/tests +++ b/tests @@ -282,6 +282,10 @@ int main() { { void *x; } + assert(0, ({ _Bool x=0; x; }), "_Bool x=0; x;"); + assert(1, ({ _Bool x=1; x; }), "_Bool x=1; x;"); + assert(1, ({ _Bool x=2; x; }), "_Bool x=2; x;"); + printf("OK\n"); return 0; } diff --git a/tokenize.c b/tokenize.c index 2d560b0..573c3a2 100644 --- a/tokenize.c +++ b/tokenize.c @@ -149,7 +149,7 @@ static char *starts_with_reserved(char *p) { // Keyword static char *kw[] = {"return", "if", "else", "while", "for", "int", "char", "sizeof", "struct", "typedef", "short", - "long", "void"}; + "long", "void", "_Bool"}; for (int i = 0; i < sizeof(kw) / sizeof(*kw); i++) { int len = strlen(kw[i]); diff --git a/type.c b/type.c index 1a7b675..c9f866f 100644 --- a/type.c +++ b/type.c @@ -1,6 +1,7 @@ #include "chibi.h" Type *void_type = &(Type){ TY_VOID, 1, 1 }; +Type *bool_type = &(Type){ TY_BOOL, 1, 1 }; Type *char_type = &(Type){ TY_CHAR, 1, 1 }; Type *short_type = &(Type){ TY_SHORT, 2, 2 }; Type *int_type = &(Type){ TY_INT, 4, 4 }; @@ -8,8 +9,8 @@ Type *long_type = &(Type){ TY_LONG, 8, 8 }; bool is_integer(Type *ty) { TypeKind k = ty->kind; - return k == TY_CHAR || k == TY_SHORT || k == TY_INT || - k == TY_LONG; + return k == TY_BOOL || k == TY_CHAR || k == TY_SHORT || + k == TY_INT ||k == TY_LONG; } int align_to(int n, int align) { -- GitLab