Skip to content

Commit 6eeda3e

Browse files
committed
Enable TAILCALL optimization
1 parent 50e73dd commit 6eeda3e

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

rcc.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
#define C_OPT_LEVEL 0x3
8181
#define C_OPT_INLINE (1<<2)
8282
#define C_OPT_MEM2SSA (1<<3)
83+
#define C_OPT_TAILCALL (1<<4)
8384

8485
#define IR_UNKNOWN_SIZE 1
8586

@@ -889,6 +890,9 @@ void rcc_ir_init(rcc_ctx *rcc, uint32_t flags)
889890
if ((rcc->c_opt_flags & C_OPT_LEVEL) > 0) {
890891
flags |= IR_OPT_FOLDING | IR_OPT_CFG | IR_OPT_CODEGEN;
891892
}
893+
if (rcc->c_opt_flags & C_OPT_TAILCALL) {
894+
flags |= IR_OPT_TAILCALL;
895+
}
892896
flags |= rcc->ir_flags;
893897
ir_init(ctx, flags, 256, 1024);
894898
ctx->mflags = rcc->ir_mflags;
@@ -1827,6 +1831,7 @@ static void rcc_help(const char *cmd)
18271831
"Optimization Options:\n"
18281832
" -O[012] - optimization level (default: -O2)\n"
18291833
" -f[no-]inline - enable/disable function inlining (default: enabled at -O1)\n"
1834+
" -f[no-]tail-calls - enable/disable tail call optimization (default: enabled at -O1)\n"
18301835
" -fno-mem2ssa - disable MEM2SSA pass (default: enabled at -O1)\n"
18311836
"Code Generation Options:\n"
18321837
#if defined(IR_TARGET_X86) || defined(IR_TARGET_X64)
@@ -1953,8 +1958,7 @@ static int rcc_parse_option(rcc_ctx *rcc, const char *opt, const char *arg, bool
19531958
{
19541959
if (opt[0] == '-' && opt[1] == 'O' && strlen(opt) == 3) {
19551960
if (opt[2] == '0') {
1956-
rcc->c_opt_flags = (rcc->c_opt_flags & ~C_OPT_LEVEL) | 0;
1957-
rcc->c_opt_flags &= ~C_OPT_INLINE;
1961+
rcc->c_opt_flags = 0;
19581962
} else if (opt[2] == '1') {
19591963
rcc->c_opt_flags = (rcc->c_opt_flags & ~C_OPT_LEVEL) | 1;
19601964
} else if (opt[2] == '2') {
@@ -1988,6 +1992,10 @@ static int rcc_parse_option(rcc_ctx *rcc, const char *opt, const char *arg, bool
19881992
rcc->c_opt_flags &= ~C_OPT_INLINE;
19891993
} else if (strcmp(opt, "-finline") == 0) {
19901994
rcc->c_opt_flags |= C_OPT_INLINE;
1995+
} else if (strcmp(opt, "-fno-tail-calls") == 0) {
1996+
rcc->c_opt_flags &= ~C_OPT_TAILCALL;
1997+
} else if (strcmp(opt, "-ftail-calls") == 0) {
1998+
rcc->c_opt_flags |= C_OPT_TAILCALL;
19911999
} else if (strcmp(opt, "-fno-mem2ssa") == 0) {
19922000
rcc->c_opt_flags &= ~C_OPT_MEM2SSA;
19932001
} else if (strcmp(opt, "-P") == 0) {
@@ -2234,7 +2242,7 @@ int main(int argc, const char **argv)
22342242
}
22352243

22362244
memset(rcc, 0, sizeof(rcc_ctx));
2237-
rcc->c_opt_flags = 2 | C_OPT_INLINE | C_OPT_MEM2SSA;
2245+
rcc->c_opt_flags = 2 | C_OPT_INLINE | C_OPT_MEM2SSA | C_OPT_TAILCALL;
22382246
rcc->e_errors = E_ERRORS_DEFAULT;
22392247
rcc->e_warnings = E_WARNINGS_DEFAULT;
22402248
rcc->ir_save_flags = IR_SAVE_SAFE_NAMES;

0 commit comments

Comments
 (0)