From 135b17bada960c4585d5a124fedb7e4e9d849078 Mon Sep 17 00:00:00 2001 From: Matthew Sotoudeh Date: Mon, 31 Jul 2023 13:34:45 -0700 Subject: simple refcounting pass in C --- c/libdietc.l | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'c/libdietc.l') diff --git a/c/libdietc.l b/c/libdietc.l index 7133803..51c8e91 100644 --- a/c/libdietc.l +++ b/c/libdietc.l @@ -13,7 +13,7 @@ static void define_aggregate(char *); static void declare_basic(char *); static void declare_object(char *); static void start_function(char *); -static void add_instruction(char *); +static void add_instruction(char *, enum INSTRUCTION_TYPE); static void end_function(); %} %% @@ -33,9 +33,13 @@ static void end_function(); ^[^{\n]*"{"$ { start_function(yytext); } ^\tType_[0-9]*" "[a-zA-Z0-9_]*" ;"$ { declare_object(yytext); - add_instruction(yytext); + add_instruction(yytext, INSTRUCTION_DECLARE); } -\t[^\n]*$ { add_instruction(yytext); } +\t[*]" "[a-zA-Z_0-9]*" = "[a-zA-Z_0-9]*" ;"$ { add_instruction(yytext, INSTRUCTION_DEREF_ASSIGN); } +\t[a-zA-Z_0-9]*" = "[*]" "[a-zA-Z_0-9]*" ;"$ { add_instruction(yytext, INSTRUCTION_ASSIGN_DEREF); } +\t[a-zA-Z_0-9]*" = "[a-zA-Z_0-9]*" ;"$ { add_instruction(yytext, INSTRUCTION_COPY); } +\t[a-zA-Z_0-9]*" = ( Type_"[0-9]*" ) "[a-zA-Z_0-9]*" ;"$ { add_instruction(yytext, INSTRUCTION_CAST); } +\t[^\n]*$ { add_instruction(yytext, INSTRUCTION_OTHER); } "}"$ { end_function(); } \n { } (.|[ \t]) { fprintf(stderr, "UNKNOWN: %s\n", yytext); } @@ -188,9 +192,11 @@ static void start_function(char *line) { NEXT_INSTRUCTION = &(FUNCTION.instructions); } -static void add_instruction(char *line) { +static void add_instruction(char *line, enum INSTRUCTION_TYPE kind) { *NEXT_INSTRUCTION = calloc(sizeof(**NEXT_INSTRUCTION), 1); (*NEXT_INSTRUCTION)->line = strdup(line); + (*NEXT_INSTRUCTION)->kind = kind; + (*NEXT_INSTRUCTION)->pprev = NEXT_INSTRUCTION; NEXT_INSTRUCTION = &((*NEXT_INSTRUCTION)->next); } @@ -264,3 +270,13 @@ struct type *libdietc_object_type(struct program program, char *name) { free(name_tok); return program.objects[h] ? program.objects[h]->type : 0; } + +struct type *libdietc_lhs_type(struct program program, struct instruction *instruction) { + char *name = 0; + if (instruction->line[1] == '*') { + name = libdietc_tokdup(libdietc_nth_token(instruction->line, 1)); + return libdietc_object_type(program, name)->base; + } + name = libdietc_tokdup(libdietc_nth_token(instruction->line, 0)); + return libdietc_object_type(program, name); +} -- cgit v1.2.3