Compare commits

...

2 Commits

Author SHA1 Message Date
ae5f96825e rename functions 2023-06-15 13:00:34 +08:00
154f45dc41 version format
Signed-off-by: szdytom <szdytom@163.com>
2023-06-15 10:48:13 +08:00
9 changed files with 73 additions and 73 deletions

View File

@ -98,22 +98,22 @@ struct Afunction {
struct ASTnode *rt; // AST root
};
struct Afunction* afunc_make();
struct Afunction* Afunction_new();
struct ASTnode* ast_make_binary(int op, struct ASTnode *left, struct ASTnode *right);
struct ASTnode* ast_make_lit_i32(int32_t x);
struct ASTnode* ast_make_lit_i64(int64_t x);
struct ASTnode* ast_make_unary(int op, struct ASTnode *c);
struct ASTnode* ast_make_block();
struct ASTnode* ast_make_var(int id);
struct ASTnode* ast_make_assign(int op, struct ASTnode *left, struct ASTnode *right);
struct ASTnode* ast_make_if(struct ASTnode *left, struct ASTnode *right, struct ASTnode *cond);
struct ASTnode* ASTbinnode_new(int op, struct ASTnode *left, struct ASTnode *right);
struct ASTnode* ASTi32node_new(int32_t x);
struct ASTnode* ASTi64node_new(int64_t x);
struct ASTnode* ASTunnode_new(int op, struct ASTnode *c);
struct ASTnode* ASTblocknode_new();
struct ASTnode* ASTvarnode_new(int id);
struct ASTnode* ASTassignnode_new(int op, struct ASTnode *left, struct ASTnode *right);
struct ASTnode* ASTifnode_new(struct ASTnode *left, struct ASTnode *right, struct ASTnode *cond);
void ast_debug_print(FILE *Outfile, struct ASTnode *rt);
void afunc_debug_print(FILE *Outfile, struct Afunction *f);
void ASTnode_print(FILE *Outfile, struct ASTnode *rt);
void Afunction_print(FILE *Outfile, struct Afunction *f);
void afunc_free(struct Afunction *f);
void ast_free(struct ASTnode *x);
void Afunction_free(struct Afunction *f);
void ASTnode_free(struct ASTnode *x);
// Parse source into AST.
struct Afunction* Afunction_from_source(const char *filename);

View File

@ -5,7 +5,7 @@
#define ACC_ARRAY_LENGTH(a) (sizeof((a))/sizeof(*(a)))
void* malloc_or_fail(size_t s, const char *func_name);
void* try_malloc(size_t s, const char *func_name);
bool strequal(const char *s1, const char *s2);
char* strclone(const char *s);

4
main.c
View File

@ -37,12 +37,12 @@ int main(int argc, char *argv[]) {
int target = target_parse(argv[1]);
struct Afunction *afunc = Afunction_from_source(argv[2]);
if (target == TARGET_AST) {
afunc_debug_print(Outfile, afunc);
Afunction_print(Outfile, afunc);
} else if (target == TARGET_ACIR) {
struct IRfunction *ir = IRfunction_from_ast(afunc);
IRfunction_print(ir, Outfile);
IRfunction_free(ir);
}
afunc_free(afunc);
Afunction_free(afunc);
return (0);
}

View File

@ -4,10 +4,10 @@
#include "acir.h"
#define IRinstruction_constructor_shared_code \
struct IRinstruction *self = malloc_or_fail(sizeof(struct IRinstruction), __FUNCTION__); \
self->id = IRfunction_alloc_ins(owner->owner); \
self->owner = owner; \
IRblock_add_ins(owner, self); \
struct IRinstruction *self = try_malloc(sizeof(struct IRinstruction), __FUNCTION__); \
self->id = IRfunction_alloc_ins(owner->owner); \
self->owner = owner; \
IRblock_add_ins(owner, self); \
// Adds one instruction to list.
// Internal function only: IRinstruction_new_xxx() automaticly calls this function.
@ -87,7 +87,7 @@ bool IRis_jmp(int op) {
// Constructs a IRblock.
struct IRblock* IRblock_new(struct IRfunction *owner) {
struct IRblock *self = malloc_or_fail(sizeof(struct IRblock), __FUNCTION__);
struct IRblock *self = try_malloc(sizeof(struct IRblock), __FUNCTION__);
self->id = owner->bs.length;
self->owner = owner;
@ -162,7 +162,7 @@ static struct IRinstruction* IRcg_dfs(struct ASTnode *x, struct IRfunction *f, s
// Generates Quad Repersentation from an AST
struct IRfunction* IRfunction_from_ast(struct Afunction *afunc) {
struct IRfunction *self = malloc_or_fail(sizeof(struct IRfunction), __FUNCTION__);
struct IRfunction *self = try_malloc(sizeof(struct IRfunction), __FUNCTION__);
self->name = afunc->name; // transfer ownership of function name string
afunc->name = NULL; // prevents the pointer being freed when freeing the Afunction

View File

@ -20,8 +20,8 @@ const char *ast_opname[] = {
};
// Constructs a binary AST node
struct ASTnode* ast_make_binary(int op, struct ASTnode *left, struct ASTnode *right) {
struct ASTbinnode *x = malloc_or_fail(sizeof(struct ASTbinnode), __FUNCTION__);
struct ASTnode* ASTbinnode_new(int op, struct ASTnode *left, struct ASTnode *right) {
struct ASTbinnode *x = try_malloc(sizeof(struct ASTbinnode), __FUNCTION__);
x->op = op;
x->left = left;
@ -30,8 +30,8 @@ struct ASTnode* ast_make_binary(int op, struct ASTnode *left, struct ASTnode *ri
}
// Make an AST integer literal (32bits) node
struct ASTnode* ast_make_lit_i32(int32_t v) {
struct ASTi32node *x = malloc_or_fail(sizeof(struct ASTi32node), __FUNCTION__);
struct ASTnode* ASTi32node_new(int32_t v) {
struct ASTi32node *x = try_malloc(sizeof(struct ASTi32node), __FUNCTION__);
x->op = A_LIT_I32;
x->val = v;
@ -39,8 +39,8 @@ struct ASTnode* ast_make_lit_i32(int32_t v) {
}
// Make an AST integer literal (64bits) node
struct ASTnode* ast_make_lit_i64(int64_t v) {
struct ASTi64node *x = malloc_or_fail(sizeof(struct ASTi64node), __FUNCTION__);
struct ASTnode* ASTi64node_new(int64_t v) {
struct ASTi64node *x = try_malloc(sizeof(struct ASTi64node), __FUNCTION__);
x->op = A_LIT_I64;
x->val = v;
@ -48,8 +48,8 @@ struct ASTnode* ast_make_lit_i64(int64_t v) {
}
// Make an AST variable value node
struct ASTnode* ast_make_var(int id) {
struct ASTvarnode *x = malloc_or_fail(sizeof(struct ASTvarnode), __FUNCTION__);
struct ASTnode* ASTvarnode_new(int id) {
struct ASTvarnode *x = try_malloc(sizeof(struct ASTvarnode), __FUNCTION__);
x->op = A_VAR;
x->id = id;
@ -57,8 +57,8 @@ struct ASTnode* ast_make_var(int id) {
}
// Make a unary AST node: only one child
struct ASTnode* ast_make_unary(int op, struct ASTnode *child) {
struct ASTunnode *x = malloc_or_fail(sizeof(struct ASTunnode), __FUNCTION__);
struct ASTnode* ASTunnode_new(int op, struct ASTnode *child) {
struct ASTunnode *x = try_malloc(sizeof(struct ASTunnode), __FUNCTION__);
x->op = op;
x->left = child;
@ -66,8 +66,8 @@ struct ASTnode* ast_make_unary(int op, struct ASTnode *child) {
}
// Make a block ast node
struct ASTnode* ast_make_block() {
struct ASTblocknode *x = malloc_or_fail(sizeof(struct ASTblocknode), __FUNCTION__);
struct ASTnode* ASTblocknode_new() {
struct ASTblocknode *x = try_malloc(sizeof(struct ASTblocknode), __FUNCTION__);
x->op = A_BLOCK;
llist_init(&x->st);
@ -75,8 +75,8 @@ struct ASTnode* ast_make_block() {
}
// Make a assignment ast node
struct ASTnode* ast_make_assign(int op, struct ASTnode *left, struct ASTnode *right) {
struct ASTassignnode *x = malloc_or_fail(sizeof(struct ASTassignnode), __FUNCTION__);
struct ASTnode* ASTassignnode_new(int op, struct ASTnode *left, struct ASTnode *right) {
struct ASTassignnode *x = try_malloc(sizeof(struct ASTassignnode), __FUNCTION__);
x->op = op;
x->left = left;
@ -85,8 +85,8 @@ struct ASTnode* ast_make_assign(int op, struct ASTnode *left, struct ASTnode *ri
}
// Make a if statement ast node
struct ASTnode* ast_make_if(struct ASTnode *left, struct ASTnode *right, struct ASTnode *cond) {
struct ASTifnode *x = malloc_or_fail(sizeof(struct ASTifnode), __FUNCTION__);
struct ASTnode* ASTifnode_new(struct ASTnode *left, struct ASTnode *right, struct ASTnode *cond) {
struct ASTifnode *x = try_malloc(sizeof(struct ASTifnode), __FUNCTION__);
x->op = A_IF;
x->left = left;
@ -140,19 +140,19 @@ static void ast_print_dfs(FILE* Outfile, struct ASTnode *x, int tabs) {
}
// Prints the structure of a AST into Outfile.
void ast_debug_print(FILE *Outfile, struct ASTnode *rt) {
void ASTnode_print(FILE *Outfile, struct ASTnode *rt) {
ast_print_dfs(Outfile, rt, 0);
}
// Prints the structure of a Afunction into Outfile.
void afunc_debug_print(FILE *Outfile, struct Afunction *f) {
void Afunction_print(FILE *Outfile, struct Afunction *f) {
fprintf(Outfile, "FUNCTION %s: \n", f->name);
ast_print_dfs(Outfile, f->rt, 0);
}
// Constructs a Afunction.
struct Afunction* afunc_make() {
struct Afunction *res = (struct Afunction*)malloc_or_fail(sizeof(struct Afunction), __FUNCTION__);
struct Afunction* Afunction_new() {
struct Afunction *res = (void*)try_malloc(sizeof(struct Afunction), __FUNCTION__);
res->rt = NULL;
res->name = NULL;
@ -160,50 +160,50 @@ struct Afunction* afunc_make() {
}
// Frees a Afunction and all its components.
void afunc_free(struct Afunction *f) {
void Afunction_free(struct Afunction *f) {
if (f->name) {
free(f->name);
}
if (f->rt) {
ast_free(f->rt);
ASTnode_free(f->rt);
}
free(f);
}
// Frees an AST's memory, including its childs.
void ast_free(struct ASTnode *x) {
void ASTnode_free(struct ASTnode *x) {
if (x == NULL) {
return;
}
switch (x->op) {
case A_IF: {
ast_free(((struct ASTifnode*)x)->cond);
ASTnode_free(((struct ASTifnode*)x)->cond);
} // fall through
case A_ASSIGN:
case A_ADD: case A_SUB: case A_MUL: case A_DIV:
case A_EQ: case A_NE: case A_GT: case A_LT: case A_GE: case A_LE:
case A_WHILE: {
struct ASTbinnode *t = (struct ASTbinnode*)x;
ast_free(t->left);
ast_free(t->right);
struct ASTbinnode *t = (void*)x;
ASTnode_free(t->left);
ASTnode_free(t->right);
} break;
case A_PRINT: case A_RETURN:
case A_LNOT: case A_BNOT: case A_NEG: {
struct ASTunnode *t = (struct ASTunnode*)x;
ast_free(t->left);
struct ASTunnode *t = (void*)x;
ASTnode_free(t->left);
} break;
case A_BLOCK: {
struct ASTblocknode *t = (struct ASTblocknode*)x;
struct ASTblocknode *t = (void*)x;
struct llist_node *p = t->st.head, *nxt;
while (p) {
nxt = p->nxt;
ast_free((struct ASTnode*)p);
ASTnode_free((void*)p);
p = nxt;
}
} break;

View File

@ -139,10 +139,10 @@ static struct ASTnode* primary(struct Pcontext *ctx) {
res = expression(ctx);
match(ctx, T_RP);
} else if (t->type == T_I32_LIT) {
res = ast_make_lit_i32(t->val_i32);
res = ASTi32node_new(t->val_i32);
next(ctx);
} else if (t->type == T_I64_LIT) {
res = ast_make_lit_i64(current(ctx)->val_i64);
res = ASTi64node_new(current(ctx)->val_i64);
next(ctx);
} else if (t->type == T_ID) {
// TODO: identifier.
@ -154,7 +154,7 @@ static struct ASTnode* primary(struct Pcontext *ctx) {
exit(1);
}
next(ctx);
return (ast_make_var(id));
return (ASTvarnode_new(id));
*/
} else {
fail_ce(t->line, "primary expression expected");
@ -180,7 +180,7 @@ static struct ASTnode* prefixed_primary(struct Pcontext *ctx) {
if (is_prefix_op(t->type)) {
next(ctx);
struct ASTnode *child = prefixed_primary(ctx);
return (ast_make_unary(unary_arithop(t), child));
return (ASTunnode_new(unary_arithop(t), child));
}
return (primary(ctx));
@ -217,10 +217,10 @@ static struct ASTnode* binexpr(struct Pcontext *ctx, int precedence) {
if (direction_rtl(op->type)) {
right = binexpr(ctx, precedence);
left = ast_make_assign(binary_arithop(op), left, right);
left = ASTassignnode_new(binary_arithop(op), left, right);
} else {
right = binexpr(ctx, tp);
left = ast_make_binary(binary_arithop(op), left, right); // join right into left
left = ASTbinnode_new(binary_arithop(op), left, right); // join right into left
}
op = current(ctx);
@ -240,7 +240,7 @@ static struct ASTnode* block(struct Pcontext *ctx) {
return (NULL);
}
struct ASTblocknode* res = (struct ASTblocknode*)ast_make_block();
struct ASTblocknode* res = (struct ASTblocknode*)ASTblocknode_new();
while (current(ctx)->type != T_RB) {
struct ASTnode *x;
x = statement(ctx);
@ -266,7 +266,7 @@ static struct ASTnode* expression(struct Pcontext *ctx) {
// parse one print statement
static struct ASTnode* print_statement(struct Pcontext *ctx) {
match(ctx, T_PRINT);
struct ASTnode *res = ast_make_unary(A_PRINT, expression(ctx));
struct ASTnode *res = ASTunnode_new(A_PRINT, expression(ctx));
match(ctx, T_SEMI);
return (res);
}
@ -300,7 +300,7 @@ static struct ASTnode* if_statement(struct Pcontext *ctx) {
} else {
else_then = NULL; // empty block
}
return (ast_make_if(then, else_then, cond));
return (ASTifnode_new(then, else_then, cond));
}
// parse an while statement
@ -310,7 +310,7 @@ static struct ASTnode* while_statement(struct Pcontext *ctx) {
struct ASTnode* cond = expression(ctx);
match(ctx, T_RP);
struct ASTnode* body = statement(ctx);
return (ast_make_binary(A_WHILE, cond, body));
return (ASTbinnode_new(A_WHILE, cond, body));
}
// parse a for statement (into a while loop)
@ -323,7 +323,7 @@ static struct ASTnode* for_statement(struct Pcontext *ctx) {
if (current(ctx)->type != T_SEMI) {
cond = expression(ctx);
} else {
cond = ast_make_lit_i32(1);
cond = ASTi32node_new(1);
}
next(ctx); // skip the ;
@ -336,7 +336,7 @@ static struct ASTnode* for_statement(struct Pcontext *ctx) {
match(ctx, T_RP);
struct ASTnode *body = statement(ctx);
struct ASTblocknode *container = (void*)ast_make_block();
struct ASTblocknode *container = (void*)ASTblocknode_new();
struct ASTnode *wbody;
if (body == NULL && inc == NULL) {
@ -346,14 +346,14 @@ static struct ASTnode* for_statement(struct Pcontext *ctx) {
} else if (inc == NULL) {
wbody = body;
} else {
struct ASTblocknode* wt = (void*)ast_make_block();
struct ASTblocknode* wt = (void*)ASTblocknode_new();
llist_pushback_notnull(&wt->st, body);
llist_pushback_notnull(&wt->st, inc);
wbody = (void*)wt;
}
llist_pushback_notnull(&container->st, init);
llist_pushback(&container->st, ast_make_binary(A_WHILE, cond, wbody));
llist_pushback(&container->st, ASTbinnode_new(A_WHILE, cond, wbody));
return ((void*)container);
}
@ -361,7 +361,7 @@ static struct ASTnode* return_statement(struct Pcontext *ctx) {
match(ctx, T_RETURN);
struct ASTnode *res = expression(ctx);
match(ctx, T_SEMI);
return (ast_make_unary(A_RETURN, res));
return (ASTunnode_new(A_RETURN, res));
}
// parse one statement
@ -401,7 +401,7 @@ static struct ASTnode* statement(struct Pcontext *ctx) {
// Parse one top-level function
// Sets the func_name param.
static struct Afunction* function(struct Pcontext *ctx) {
struct Afunction *res = afunc_make();
struct Afunction *res = Afunction_new();
match(ctx, T_INT);
expect(ctx, T_ID);

View File

@ -64,7 +64,7 @@ static void scan_int(struct token *t) {
static char* scan_indentifier(int *n) {
int sz = 128, len = 0;
char *res = malloc_or_fail(sz * sizeof(char), __FUNCTION__);
char *res = try_malloc(sz * sizeof(char), __FUNCTION__);
memset(res, 0, sz * sizeof(char));
int c = preview();
@ -157,7 +157,7 @@ static bool scan_1c(struct token *t) {
static struct token* scan(void) {
skip_whitespaces();
struct token *t = malloc_or_fail(sizeof(struct token), __FUNCTION__);
struct token *t = try_malloc(sizeof(struct token), __FUNCTION__);
t->line = Line;
int c = preview();

View File

@ -5,7 +5,7 @@
// This function tries to malloc some memory.
// Will call fail_malloc() in fatals.h when failing.
void* malloc_or_fail(size_t s, const char *func_name) {
void* try_malloc(size_t s, const char *func_name) {
void *res = malloc(s);
if (res == NULL) {
fail_malloc(func_name);

View File

@ -1,5 +1,5 @@
set_project("acc")
set_version("0.1a1")
set_version("0.1.1")
set_basename("acc")
set_languages("c11")
set_targetdir(".")