From: Mart Lubbers Date: Thu, 4 Mar 2021 10:54:39 +0000 (+0100) Subject: cleanup X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=5b987007e30baac264c14f85afc4401bb4363a1a;p=ccc.git cleanup --- diff --git a/.gitignore b/.gitignore index 6992eb8..43faf05 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ scan.[ch] *.o y.output a.c + +callgrind.out.* +massif.out.* diff --git a/Makefile b/Makefile index be0b724..82ebc58 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ CFLAGS+=-Wall -Wextra -Werror -std=c99 -pedantic -ggdb -YFLAGS+=-d --locations -v --defines=parse.h +LDFLAGS+=-Wl,--gc-sections,--print-gc-sections +YFLAGS+=--locations -Wno-yacc --defines=parse.h LFLAGS+=--header-file=scan.h OBJECTS:=scan.o parse.o ast.o type.o util.o list.o sem.o genc.o ident.o\ @@ -11,12 +12,7 @@ scan.c: scan.l parse.h parse.h: parse.c expr.c: y.tab.h -scan.o: CFLAGS+=-D_XOPEN_SOURCE=700 - -.PHONY: test - -test: - CFLAGS="$(CFLAGS)" $(MAKE) -C test test +$(filter-out scan.o parse.o,$(OBJECTS)): CFLAGS+=-ffunction-sections clean: $(RM) $(OBJECTS) y.output parse.h scan.h scan.c parse.c expr a.c diff --git a/ident.c b/ident.c index 43d026a..2abf4a7 100644 --- a/ident.c +++ b/ident.c @@ -10,33 +10,13 @@ struct ident ident_str(char *s) return (struct ident) {.type=istr, .data={.istr=s}}; } -struct ident ident_int(int i) -{ - return (struct ident) {.type=iint, .data={.iint=i}}; -} - int ident_cmp(struct ident l, struct ident r) { - //int > buf == str - switch (l.type) { - case iint: - switch (r.type) { - case iint: - return l.data.iint - r.data.iint; - default: - return 1; - } - break; - case istr: - switch (r.type) { - case iint: - return -1; - case istr: - return strcmp(l.data.istr, r.data.istr); - } - break; - } - return 0; + //int > str + if (l.type == iint) + return r.type == iint ? l.data.iint - r.data.iint : 1; + else + return r.type == istr ? strcmp(l.data.istr, r.data.istr) : -1; } int ident_cmpv(const void *l, const void *r) @@ -44,26 +24,6 @@ int ident_cmpv(const void *l, const void *r) return ident_cmp(*(struct ident *)l, *(struct ident *)r); } -int ident_stricmp(char *l, struct ident r) -{ - return ident_cmp((struct ident){.type=istr, .data={.istr=l}}, r); -} - -int ident_stricmpv(const void *l, const void *r) -{ - return ident_stricmp((char *)l, *(struct ident *)r); -} - -int ident_istrcmp(struct ident l, char *r) -{ - return ident_cmp(l, (struct ident){.type=istr, .data={.istr=r}}); -} - -int ident_istrcmpv(const void *l, const void *r) -{ - return ident_istrcmp(*(struct ident *)l, (char *)r); -} - struct ident ident_dup(struct ident i) { if (i.type == istr) @@ -71,15 +31,6 @@ struct ident ident_dup(struct ident i) return i; } -char *ident_tostr(struct ident i) -{ - if (i.type == istr) - return i.data.istr; - char buf[10] = {0}; - sprintf(buf, "%d", i.data.iint); - return safe_strdup(buf); -} - void ident_print(struct ident i, FILE *out) { if (i.type == istr) diff --git a/ident.h b/ident.h index c059efe..2a47a53 100644 --- a/ident.h +++ b/ident.h @@ -12,17 +12,11 @@ struct ident { }; struct ident ident_str(char *istr); -struct ident ident_int(int iint); int ident_cmp(struct ident, struct ident); int ident_cmpv(const void *, const void *); -int ident_stricmp(char *, struct ident); -int ident_stricmpv(const void *, const void *); -int ident_istrcmp(struct ident, char *); -int ident_istrcmpv(const void *, const void *); struct ident ident_dup(struct ident); -char *ident_tostr(struct ident); void ident_print(struct ident, FILE *); void ident_free(struct ident); diff --git a/list.c b/list.c index 84de358..d58ad78 100644 --- a/list.c +++ b/list.c @@ -4,17 +4,6 @@ #include "list.h" #include "util.h" -struct list *list_append(void *el, struct list *head) -{ - if (head == NULL) - return list_cons(el, NULL); - struct list *tail = head; - while (tail->tail != NULL) - tail = tail->tail; - tail->tail = list_cons(el, NULL); - return head; -} - struct list *list_cons(void *el, struct list *tail) { struct list *res = xalloc(1, struct list); diff --git a/list.h b/list.h index 979e101..6e87129 100644 --- a/list.h +++ b/list.h @@ -10,7 +10,6 @@ struct list { struct list *tail; }; -struct list *list_append(void *el, struct list *head); struct list *list_cons(void *el, struct list *tail); void list_free(struct list *head, void (*freefun)(void *)); void **list_to_array(struct list *list, int *num, bool reverse, int extra); diff --git a/parse.y b/parse.y index 1cdea96..16f236c 100644 --- a/parse.y +++ b/parse.y @@ -21,8 +21,14 @@ int yywrap() %} -%define parse.lac full %define parse.error verbose +// Dangling else +//%expect 1 +// Tuples and parenthesis +//%expect-rr 1 +%glr-parser +%locations +%parse-param { struct ast **result } %union { struct expr *expr; @@ -34,15 +40,12 @@ int yywrap() char *ident; } -%locations %token IDENT %token BOOL CHAR INTEGER STRING %token ARROW ASSIGN BCLOSE BINAND BINOR BOPEN CCLOSE COMMA CONS COPEN DIVIDE %token DOT ELSE ERROR IF INVERSE MINUS MODULO NIL PLUS POWER RETURN SEMICOLON %token SCLOSE SOPEN TIMES TBOOL TCHAR TINT TVOID VAR WHILE -%parse-param { struct ast **result } - %right BINOR %right BINAND %nonassoc EQ NEQ LEQ LE GEQ GE @@ -52,7 +55,7 @@ int yywrap() %right POWER %type start -%type expr +%type expr tuply %type args body decls fargs field fnargs nargs funtype bbody %type stmt %type type ftype @@ -132,6 +135,10 @@ stmt | vardecl { $$ = stmt_vardecl($1, @$); } | expr SEMICOLON { $$ = stmt_expr($1, @$); } ; +tuply + : BCLOSE { $$ = NULL; } + | COMMA expr BCLOSE { $$ = $2; } + ; expr : expr BINOR expr { $$ = expr_binop($1, binor, $3, @$); } | expr BINAND expr { $$ = expr_binop($1, binand, $3, @$); } @@ -151,8 +158,7 @@ expr | MINUS expr %prec TIMES { $$ = expr_unop(negate, $2, @$); } | INVERSE expr %prec TIMES { $$ = expr_unop(inverse, $2, @$); } | IDENT BOPEN fargs BCLOSE field { $$ = expr_funcall($1, $3, $5, @$); } - | BOPEN expr COMMA expr BCLOSE { $$ = expr_tuple($2, $4, @$); } - | BOPEN expr BCLOSE { $$ = $2; } + | BOPEN expr tuply { $$ = $3 == NULL ? $2 : expr_tuple($2, $3, @$); } | INTEGER | BOOL | CHAR diff --git a/scan.l b/scan.l index 96349dc..049a80a 100644 --- a/scan.l +++ b/scan.l @@ -6,6 +6,7 @@ I [a-zA-Z_] %option noinput %option nounput +%option never-interactive %{ #define YY_USER_ACTION \ diff --git a/sem.c b/sem.c index b5c48e5..18425d2 100644 --- a/sem.c +++ b/sem.c @@ -52,17 +52,6 @@ struct vardecl *type_vardecl(struct gamma *gamma, struct vardecl *vardecl) return vardecl; } -void type_fundecl(struct gamma *gamma, struct fundecl *decl) -{ - struct type *f1 = gamma_fresh(gamma); - struct subst *s1 = infer_fundecl(gamma, decl, f1); - f1 = subst_apply_t(s1, f1); - - gamma_insert(gamma, ident_str(decl->ident), scheme_generalise(gamma, f1)); - subst_free(s1); - type_free(f1); -} - void type_comp(struct gamma *gamma, int ndecls, struct fundecl **decl) { //Create a fresh variable for every function in the component @@ -239,7 +228,6 @@ struct ast *sem(struct ast *ast) } } gamma_free(gamma); - subst_free_queue(); return ast; } diff --git a/sem/hm/subst.c b/sem/hm/subst.c index 42a8f92..da07746 100644 --- a/sem/hm/subst.c +++ b/sem/hm/subst.c @@ -9,29 +9,14 @@ #define KEEP_LIST -#ifdef KEEP_LIST -#define BUFCAP 100 -struct subst *buf[BUFCAP] = {NULL}; -int bufi = 0; -#endif - struct subst *subst_id() { struct subst *res; -#ifdef KEEP_LIST - if (bufi == 0 || bufi >= BUFCAP) { -#endif - res = xalloc(1, struct subst); - res->capacity = INCAP; - res->nvar = 0; - res->entries = xalloc(INCAP, struct subst_entry); -#ifdef KEEP_LIST - } else { - res = buf[--bufi]; - res->nvar = 0; - } -#endif + res = xalloc(1, struct subst); + res->capacity = INCAP; + res->nvar = 0; + res->entries = xalloc(INCAP, struct subst_entry); return res; } @@ -46,7 +31,7 @@ static inline void subst_increase_cap(struct subst *s) } static const void *bsearchfail; -int idententrycmp(const void *l, const void *r) +static int idententrycmp(const void *l, const void *r) { bsearchfail = r; return ident_cmp(*(struct ident *)l, ((struct subst_entry *)r)->var); @@ -171,31 +156,22 @@ struct gamma *subst_apply_g(struct subst *subst, struct gamma *gamma) return gamma; } -void subst_print(struct subst *s, FILE *out) -{ - if (s == NULL) { - fprintf(out, "(nil)"); - } else { - fprintf(out, "["); - for (size_t i = 0; invar; i++) { - ident_print(s->entries[i].var, out); - fprintf(out, "->"); - type_print(s->entries[i].type, out); - if (i + 1 < s->nvar) - fprintf(out, ", "); - } - fprintf(out, "]"); - } -} - -static void subst_really_free(void *sp) -{ - struct subst *s = (struct subst *)sp; - if (s != NULL) { - free(s->entries); - free(s); - } -} +//void subst_print(struct subst *s, FILE *out) +//{ +// if (s == NULL) { +// fprintf(out, "(nil)"); +// } else { +// fprintf(out, "["); +// for (size_t i = 0; invar; i++) { +// ident_print(s->entries[i].var, out); +// fprintf(out, "->"); +// type_print(s->entries[i].type, out); +// if (i + 1 < s->nvar) +// fprintf(out, ", "); +// } +// fprintf(out, "]"); +// } +//} void subst_free(struct subst *s) { @@ -203,23 +179,6 @@ void subst_free(struct subst *s) ident_free(s->entries[i].var); type_free(s->entries[i].type); } -#ifdef KEEP_LIST - if (bufi < BUFCAP) { - buf[bufi++] = s; - } else { - subst_really_free(s); - } -#else - subst_really_free(s); -#endif -} - -void subst_free_queue() -{ -#ifdef KEEP_LIST - while (--bufi >= 0) { - free(buf[bufi]->entries); - free(buf[bufi]); - } -#endif + free(s->entries); + free(s); } diff --git a/sem/hm/subst.h b/sem/hm/subst.h index 579272e..96ed3ee 100644 --- a/sem/hm/subst.h +++ b/sem/hm/subst.h @@ -24,8 +24,7 @@ struct type *subst_apply_t(struct subst *subst, struct type *l); struct scheme *subst_apply_s(struct subst *subst, struct scheme *scheme); struct gamma *subst_apply_g(struct subst *subst, struct gamma *gamma); -void subst_print(struct subst *s, FILE *out); +//void subst_print(struct subst *s, FILE *out); void subst_free(struct subst *s); -void subst_free_queue(); #endif diff --git a/sem/scc.c b/sem/scc.c index 48ec497..dd685d3 100644 --- a/sem/scc.c +++ b/sem/scc.c @@ -147,12 +147,6 @@ struct components *tarjans( return tj.head; } -int iddeclcmp(const void *l, const void *r) -{ - fprintf(stderr, "iddeclcmp: %s %s\n", (char *)l, (*(struct decl **)r)->data.dfun->ident); - return strcmp((char *)l, (*(struct decl **)r)->data.dfun->ident); -} - struct list *edges_expr(int ndecls, struct decl **decls, void *parent, struct expr *expr, struct list *l) { diff --git a/type.c b/type.c index 51e5bef..51ce1a0 100644 --- a/type.c +++ b/type.c @@ -57,19 +57,19 @@ struct type *type_var_int(int i) struct type *type_var(struct ident ident) { struct type *res = xalloc(1, struct type); - if (ident_istrcmp(ident, "Int") == 0) { + if (ident_cmp(ident, ident_str("Int")) == 0) { res->type = tbasic; res->data.tbasic = btint; ident_free(ident); - } else if (ident_istrcmp(ident, "Char") == 0) { + } else if (ident_cmp(ident, ident_str("Char")) == 0) { res->type = tbasic; res->data.tbasic = btchar; ident_free(ident); - } else if (ident_istrcmp(ident, "Bool") == 0) { + } else if (ident_cmp(ident, ident_str("Bool")) == 0) { res->type = tbasic; res->data.tbasic = btbool; ident_free(ident); - } else if (ident_istrcmp(ident, "Void") == 0) { + } else if (ident_cmp(ident, ident_str("Void")) == 0) { res->type = tbasic; res->data.tbasic = btvoid; ident_free(ident);