Skip to content

Commit

Permalink
Merge branch 'feature/unused-var'
Browse files Browse the repository at this point in the history
  • Loading branch information
tyfkda committed Dec 1, 2024
2 parents 62fa638 + ad6ac97 commit 4fe2b6c
Show file tree
Hide file tree
Showing 23 changed files with 489 additions and 160 deletions.
2 changes: 1 addition & 1 deletion libsrc/_wasm/unistd/mkdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
extern int __max_preopen_fd;

int mkdir(const char *fn, mode_t mode) {
(void)mode;
size_t fnlen = strlen(fn);
for (int base_fd = 3; base_fd < __max_preopen_fd; ++base_fd) {
Prestat prestat;
Expand All @@ -29,7 +30,6 @@ int mkdir(const char *fn, mode_t mode) {
fnlen2 = fnlen - 1;
}

Filestat fs;
uint32_t result = path_create_directory(base_fd, fn2, fnlen2);
if (result == 0)
return 0;
Expand Down
1 change: 1 addition & 0 deletions libsrc/crt0/_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ static void start2(int argc, char *argv[], char *env[]) {
}

void _start(void) {
(void)start2; // Avoid warning: unused function 'start2'
#if defined(__x86_64__)
__asm("mov (%rsp), %rdi\n"
"lea 8(%rsp), %rsi\n"
Expand Down
1 change: 1 addition & 0 deletions libsrc/stdlib/getrandom.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "../_wasm/wasi.h"

ssize_t getrandom(void *buf, size_t buflen, unsigned int flags) {
(void)flags;
random_get(buf, buflen);
return buflen;
}
Expand Down
5 changes: 3 additions & 2 deletions src/cc/backend/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "arch_config.h"
#include "ast.h"
#include "cc_misc.h" // is_function_omitted
#include "fe_misc.h" // curfunc, curscope
#include "ir.h"
#include "optimize.h"
Expand Down Expand Up @@ -874,8 +875,8 @@ bool gen_defun(Function *func) {
return false;

VarInfo *funcvi = scope_find(global_scope, func->name, NULL);
if (funcvi != NULL && satisfy_inline_criteria(funcvi, funcvi->storage)) {
// Omit inline function: func->extra preserves the value (NULL).
if (is_function_omitted(funcvi)) {
// Omit function: func->extra preserves the value (NULL).
return false;
}

Expand Down
8 changes: 7 additions & 1 deletion src/cc/backend/codegen_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -563,14 +563,20 @@ static VReg *gen_comma(Expr *expr) {
static VReg *gen_assign_sub(Expr *lhs, Expr *rhs) {
VReg *src = gen_expr(rhs);
if (lhs->kind == EX_VAR) {
const VarInfo *varinfo = scope_find(lhs->var.scope, lhs->var.name, NULL);
assert(varinfo != NULL);
if (is_prim_type(lhs->type) && !is_global_scope(lhs->var.scope)) {
const VarInfo *varinfo = scope_find(lhs->var.scope, lhs->var.name, NULL);
if (is_local_storage(varinfo)) {
assert(varinfo->local.vreg != NULL);
new_ir_mov(varinfo->local.vreg, src, is_unsigned(rhs->type) ? IRF_UNSIGNED : 0);
return src;
}
}

if ((varinfo->storage & (VS_STATIC | VS_USED)) == VS_STATIC) {
// Can be omitted.
return src;
}
}

VReg *dst = gen_lval(lhs);
Expand Down
5 changes: 4 additions & 1 deletion src/cc/backend/emit_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,10 @@ void emit_code(Vector *decls) {
emit_comment(NULL);
for (int i = 0; i < global_scope->vars->len; ++i) {
VarInfo *varinfo = global_scope->vars->data[i];
if ((varinfo->storage & (VS_EXTERN | VS_ENUM_MEMBER)) || varinfo->type->kind == TY_FUNC)
int storage = varinfo->storage;
if (varinfo->type->kind == TY_FUNC ||
(storage & (VS_EXTERN | VS_ENUM_MEMBER)) ||
(storage & (VS_STATIC | VS_USED)) == VS_STATIC) // Static variable but not used.
continue;
emit_varinfo(varinfo, varinfo->global.init);
}
Expand Down
51 changes: 27 additions & 24 deletions src/cc/cc1.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,10 @@ static void compile1(FILE *ifp, const char *filename, Vector *decls) {
parse(decls);
}

static bool parse_fopt(const char *optarg, bool value) {
static const struct {
const char *flag_name;
off_t flag_offset;
} kFlagTable[] = {
{"common", offsetof(CcFlags, common)},
};

for (size_t i = 0; i < ARRAY_SIZE(kFlagTable); ++i) {
if (strcmp(optarg, kFlagTable[i].flag_name) == 0) {
size_t len = strlen(kFlagTable[i].flag_name);
if (optarg[len] != '\0')
continue;
bool *p = (bool*)((char*)&cc_flags + kFlagTable[i].flag_offset);
*p = value;
return true;
}
}
return false;
}

int main(int argc, char *argv[]) {
enum {
OPT_FNO = 128,
OPT_WNO,
OPT_SSA,
};

Expand All @@ -80,8 +60,9 @@ int main(int argc, char *argv[]) {
{"O", optional_argument}, // Optimization level

// Sub command
{"fno-", required_argument, OPT_FNO},
{"f", required_argument},
{"fno-", optional_argument, OPT_FNO},
{"f", optional_argument},
{"Wno-", optional_argument, OPT_WNO},
{"W", required_argument},

// Feature flag.
Expand Down Expand Up @@ -120,16 +101,38 @@ int main(int argc, char *argv[]) {

case 'f':
case OPT_FNO:
if (optarg == NULL) {
fprintf(stderr, "Warning: missing argument for -f\n");
break;
}
if (!parse_fopt(optarg, opt == 'f')) {
// Silently ignored.
// fprintf(stderr, "Warning: unknown option for -f: %s\n", optarg);
}
break;

case 'W':
if (optarg != NULL) {
if (strcmp(optarg, "error") == 0) {
cc_flags.warn_as_error = true;
break;
}
if (strcmp(optarg, "all") == 0) {
// Assume all members are bool.
for (bool *p = (bool*)&cc_flags.warn; p < (bool*)(&cc_flags.warn + 1); ++p)
*p = true;
break;
}
}
// Fallthrough
case OPT_WNO:
if (optarg == NULL) {
fprintf(stderr, "Warning: missing argument for -W\n");
break;
}
if (strcmp(optarg, "error") == 0) {
cc_flags.warn_as_error = true;
} else {
} else if (!parse_wopt(optarg, opt == 'W')) {
// Silently ignored.
// fprintf(stderr, "Warning: unknown option for -W: %s\n", optarg);
}
Expand Down
7 changes: 7 additions & 0 deletions src/cc/frontend/cc_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@
#include <inttypes.h> // PRId64

#include "ast.h"
#include "fe_misc.h"
#include "type.h"
#include "util.h"
#include "var.h"

bool is_function_omitted(const VarInfo *funcvi) {
assert(funcvi != NULL);
return (satisfy_inline_criteria(funcvi, funcvi->storage) ||
(funcvi->storage & (VS_STATIC | VS_USED)) == VS_STATIC); // Static function but not used.
}

static void eval_initial_value(Expr *expr, Expr **pvar, int64_t *poffset) {
switch (expr->kind) {
case EX_FIXNUM:
Expand Down
4 changes: 4 additions & 0 deletions src/cc/frontend/cc_misc.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#pragma once

#include <stdbool.h>
#include <stdint.h>

typedef struct Expr Expr;
typedef struct Initializer Initializer;
typedef struct Type Type;
typedef struct VarInfo VarInfo;

bool is_function_omitted(const VarInfo *funcvi);

typedef struct {
void (*emit_align)(void *ud, int align);
Expand Down
Loading

0 comments on commit 4fe2b6c

Please sign in to comment.