Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-s168 committed Aug 15, 2024
1 parent 5bd021a commit 0750dc3
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 25 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/gnu_analyze.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Static Analyze (GCC)

on: [push]

jobs:
run_tests:
runs-on: self-hosted
steps:
- uses: actions/checkout@v2
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: 3.7
- name: Setup
run: |
python -m pip install --upgrade pip
shell: bash
- name: Info
run: |
chmod +x build.sh
./build.sh info
shell: bash
- name: Analyze
run: |
chmod +x build.sh
./build.sh ganalyze
shell: bash
19 changes: 13 additions & 6 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

set -e

: ${CFLAGS:="-Wall -Wextra -Wno-unused -Wpedantic -Werror -std=c11"}
: ${CC:="clang"}
: ${CFLAGS:="-Wall -Wextra -Wno-unused -Wno-comment -Wno-format -Wno-sign-compare -Wno-analyzer-malloc-leak -Wno-char-subscripts -Wno-implicit-fallthrough -Wno-missing-braces -Wno-analyzer-deref-before-check -Werror -std=c11"}
: ${CC:=clang}

FILES="test.c ir/*.c common/*.c ir/opt/*.c ir/transform/*.c cg/x86_stupid/*.c"

Expand All @@ -22,25 +22,32 @@ function prepare() {
fi
fi
echo "found python at: $python"
$python -m pip install generic-lexer
clang build.c -lpthread -DVERBOSE=1 -DPYTHON="\"$python\"" -o build.exe
$python -m pip install generic-lexer &>/dev/null
$CC build.c -lpthread -DVERBOSE=1 -DPYTHON="\"$python\"" -DCC="\"$CC\"" -o build.exe
echo "# build.exe compiled"
echo "# gen cdef files"
./build.exe gen
}

if [[ $1 == "analyze" ]]; then
echo "analyzing..."
echo "# analyzing..."
prepare
echo "# starting analyzer"
clang --analyze -Xclang -analyzer-werror $CFLAGS $FILES
elif [[ $1 == "ganalyze" ]]; then
echo "analyzing (gcc) ..."
prepare
echo "# starting analyzer (gcc)"
gcc -fanalyzer -fsanitize-trap=undefined $CFLAGS $FILES
elif [[ $1 == "info" ]]; then
echo clang:
clang --version
echo gcc:
gcc --version
echo cflags: $CFLAGS
echo files: $FILES
echo cc:
$CC --version
"$CC" --version
else
echo "compile Debug"
prepare
Expand Down
27 changes: 18 additions & 9 deletions cg/x86_stupid/cg.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ typedef struct {
struct Location * stored;
} RegType;

#define MkRegType(idi, name0, name1, name2, name3) (RegType) { .id = (idi), .name = { name0, name1, name2, name3 }, .stored = NULL }
#define MkRegType7(idi, name0, name1, name2, name3, name4, name5, name6) (RegType) { .id = (idi), .name = { name0, name1, name2, name3, name4, name5, name6 }, .stored = NULL }
#define MkVecRegTy(id, vid) MkRegType7((id), "xmm" #vid, "xmm" #vid, "xmm" #vid, "xmm" #vid, "xmm" #vid, "ymm" #vid, "zmm" #vid)
#define MkRegType(idi, name0, name1, name2, name3) \
((RegType) { .id = (idi), .name = { name0, name1, name2, name3 }, .stored = NULL })
#define MkRegType7(idi, name0, name1, name2, name3, name4, name5, name6) \
((RegType) { .id = (idi), .name = { name0, name1, name2, name3, name4, name5, name6 }, .stored = NULL })
#define MkVecRegTy(id, vid) \
MkRegType7((id), "xmm" #vid, "xmm" #vid, "xmm" #vid, "xmm" #vid, "xmm" #vid, "ymm" #vid, "zmm" #vid)

static RegType REG_RAX = MkRegType(0, "al", "ax", "eax", "rax");
static RegType REG_RBX = MkRegType(1, "bl", "bx", "ebx", "rbx");
Expand Down Expand Up @@ -103,11 +106,16 @@ typedef struct Location {
} v;
} Location;

#define LocImm(width, value) ((Location) { .bytesWidth = (width), .type = LOC_IMM, .v.imm = (value) })
#define LocReg(width, regId) ((Location) { .bytesWidth = (width), .type = LOC_REG, .v.reg = (regId) })
#define LocEA(width, basee, off, sign, mul) ((Location) { .bytesWidth = (width), .type = LOC_EA, .v.ea.base = (basee), .v.ea.offsetSign = (sign), .v.ea.offset = (off), .v.ea.offsetMul = (mul) })
#define LocMem(width, eaa) ((Location) { .bytesWidth = (width), .type = LOC_MEM, .v.mem.address = (eaa) })
#define LocLabel(str) ((Location) { .type = LOC_LABEL, .v.label.label = (str) })
#define LocImm(width, value) \
((Location) { .bytesWidth = (width), .type = LOC_IMM, .v.imm = (value) })
#define LocReg(width, regId) \
((Location) { .bytesWidth = (width), .type = LOC_REG, .v.reg = (regId) })
#define LocEA(width, basee, off, sign, mul) \
((Location) { .bytesWidth = (width), .type = LOC_EA, .v.ea.base = (basee), .v.ea.offsetSign = (sign), .v.ea.offset = (off), .v.ea.offsetMul = (mul) })
#define LocMem(width, eaa) \
((Location) { .bytesWidth = (width), .type = LOC_MEM, .v.mem.address = (eaa) })
#define LocLabel(str) \
((Location) { .type = LOC_LABEL, .v.label.label = (str) })

Location* loc_opt_copy(Location* old) {
Location* new = fastalloc(sizeof(Location));
Expand Down Expand Up @@ -141,7 +149,8 @@ Location* loc_opt_copy(Location* old) {
return new;
}

case LOC_INVALID: {
case LOC_INVALID:
default: {
assert(false);
return NULL;
}
Expand Down
5 changes: 3 additions & 2 deletions ir/analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ bool vx_IrOp_is_volatile(vx_IrOp *op)
return true;

case VX_IR_OP____END:
assert(false);
default:
assert(false);
return false;
}
}
Expand Down Expand Up @@ -291,7 +292,7 @@ size_t vx_IrOp_inline_cost(vx_IrOp *op) {
size_t total = 0;

for (size_t i = 0; i < op->params_len; i ++)
if (op->params[i].val.type == VX_IR_NAME_BLOCK)
if (op->params[i].val.type == VX_IR_VAL_BLOCK)
total += vx_IrBlock_inline_cost(op->params[i].val.block);

total += cost_lut[op->id];
Expand Down
6 changes: 4 additions & 2 deletions ir/builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,10 @@ void vx_IrBlock_destroy(vx_IrBlock *block)
for (vx_IrOp *op = block->first; op; op = op->next)
vx_IrOp_destroy(op);
free(block->outs);
if (block->is_root)
if (block->is_root) {
free(block->as_root.vars);

free(block->as_root.labels);
}
if (block->should_free)
free(block);
}
Expand Down Expand Up @@ -363,6 +364,7 @@ void vx_IrOp_steal_states(vx_IrOp *dest,
{
free(dest->args);
dest->args = malloc(sizeof(vx_IrValue) * src->args_len);
assert(dest->args);
for (size_t i = 0; i < src->args_len; i ++)
dest->args[i] = src->args[i];
dest->args_len = src->args_len;
Expand Down
10 changes: 10 additions & 0 deletions ir/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ const char *vx_IrName_str[] = {
};

void vx_IrValue_dump(vx_IrValue value, FILE *out, const size_t indent) {
assert(out);

switch (value.type) {
case VX_IR_VAL_BLOCKREF: {
fprintf(out, "#%s", value.block->name);
Expand Down Expand Up @@ -178,10 +180,16 @@ void vx_IrValue_dump(vx_IrValue value, FILE *out, const size_t indent) {
fputc('}', out);
}
break;

default:
assert(false);
break;
}
}

void vx_IrOp_dump(const vx_IrOp *op, FILE *out, size_t indent) {
assert(out);

for (size_t j = 0; j < indent; j ++)
fputs(" ", out);

Expand Down Expand Up @@ -216,6 +224,8 @@ void vx_IrOp_dump(const vx_IrOp *op, FILE *out, size_t indent) {
}

void vx_IrBlock_dump(vx_IrBlock *block, FILE *out, const size_t indent) {
assert(out);

for (size_t i = 0; i < indent; i ++)
fputs(" ", out);

Expand Down
18 changes: 14 additions & 4 deletions ir/fastalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@ static size_t chunk_left = 0;
void * fastalloc(size_t bytes) {
if (chunk == NULL || bytes > chunk_left) {
if (chunk) {
chunks = realloc(chunks, sizeof(void*) * (chunkslen + 1));
chunks[chunkslen ++] = chunk;
void * temp = realloc(chunks, sizeof(void*) * (chunkslen + 1));
if (temp == NULL) {
return NULL;
}
chunks = temp;
chunks[chunkslen ++] = chunk;
}

size_t new = 1024;
if (bytes > new)
new = bytes * 2;
chunk = malloc(new);
chunk_left = new;
void * temp = malloc(new);
if (temp == NULL) {
return NULL;
}
chunk = temp;
chunk_left = new;
}

chunk_left -= bytes;
Expand All @@ -40,6 +48,8 @@ void * fastrealloc(void * old, size_t oldBytes, size_t newBytes) {
}

void * new = fastalloc(newBytes);
if (new == NULL)
return NULL;
memcpy(new, old, oldBytes);
return new;
}
Expand Down
14 changes: 14 additions & 0 deletions ir/ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ size_t vx_IrType_size(vx_IrType *ty) {

case VX_IR_TYPE_FUNC:
return PTRSIZE;

default:
assert(false);
return 0;
}
}

Expand All @@ -75,6 +79,10 @@ void vx_IrType_free(vx_IrType *ty) {
case VX_IR_TYPE_FUNC:
free(ty->func.args);
return;

default:
assert(false);
return;
}
}

Expand All @@ -88,6 +96,8 @@ vx_IrTypeRef vx_IrValue_type(vx_IrBlock* root, vx_IrValue value) {
case VX_IR_VAL_ID:
return vx_ptrtype(root);

default:
assert(false);
case VX_IR_VAL_TYPE:
case VX_IR_VAL_BLOCK:
return (vx_IrTypeRef) { .ptr = NULL, .shouldFree = false };
Expand Down Expand Up @@ -240,6 +250,7 @@ vx_IrVar vx_IrBlock_new_var(vx_IrBlock *block, vx_IrOp *decl) {
vx_IrBlock *root = (vx_IrBlock *) vx_IrBlock_root(block);
assert(root != NULL);
root->as_root.vars = realloc(root->as_root.vars, (root->as_root.vars_len + 1) * sizeof(*root->as_root.vars));
assert(root->as_root.vars);
vx_IrVar new = root->as_root.vars_len ++;
root->as_root.vars[new].decl = decl;
return new;
Expand All @@ -251,6 +262,7 @@ size_t vx_IrBlock_new_label(vx_IrBlock *block, vx_IrOp *decl) {
vx_IrBlock *root = (vx_IrBlock *) vx_IrBlock_root(block);
assert(root != NULL);
root->as_root.labels = realloc(root->as_root.labels, (root->as_root.labels_len + 1) * sizeof(*root->as_root.labels));
assert(root->as_root.labels);
size_t new = root->as_root.labels_len ++;
root->as_root.vars[new].decl = decl;
return new;
Expand Down Expand Up @@ -281,11 +293,13 @@ vx_IrTypeRef vx_IrBlock_type(vx_IrBlock* block) {
: NULL;

vx_IrType **args = malloc(sizeof(vx_IrType*) * block->ins_len);
assert(args);
for (size_t i = 0; i < block->ins_len; i ++) {
args[i] = block->ins[i].type;
}

vx_IrType *type = malloc(sizeof(vx_IrType));
assert(type);
type->kind = VX_IR_TYPE_FUNC;
type->func.nullableReturnType = ret;
type->func.args_len = block->ins_len;
Expand Down
11 changes: 9 additions & 2 deletions ir/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ void * fastalloc(size_t bytes);
void * fastrealloc(void * old, size_t oldBytes, size_t newBytes);
void fastfreeall(void);
static char * faststrdup(const char * str) {
assert(str);
size_t len = strlen(str) + 1;
len *= sizeof(char);
char * ret = (char*) fastalloc(len);
assert(ret);
memcpy(ret, str, len);
return ret;
}
Expand Down Expand Up @@ -101,7 +103,10 @@ struct vx_IrType_s {
};

static vx_IrType* vx_IrType_heap(void) {
return (vx_IrType*) memset(malloc(sizeof(vx_IrType)), 0, sizeof(vx_IrType));
vx_IrType* ptr = malloc(sizeof(vx_IrType));
assert(ptr);
memset(ptr, 0, sizeof(vx_IrType));
return ptr;
}

#define PTRSIZE (8)
Expand Down Expand Up @@ -443,7 +448,9 @@ void vx_IrOp_add_out(vx_IrOp *op, vx_IrVar v, vx_IrType *t);
void vx_IrOp_add_param_s(vx_IrOp *op, vx_IrName name, vx_IrValue val);
void vx_IrOp_add_param(vx_IrOp *op, vx_IrNamedValue p);
static void vx_IrOp_steal_param(vx_IrOp *dest, const vx_IrOp *src, vx_IrName param) {
vx_IrOp_add_param_s(dest, param, *vx_IrOp_param(src, param));
vx_IrValue* val = vx_IrOp_param(src, param);
assert(val);
vx_IrOp_add_param_s(dest, param, *val);
}
void vx_IrOp_remove_params(vx_IrOp *op);
void vx_IrOp_remove_out_at(vx_IrOp *op, size_t id);
Expand Down
1 change: 1 addition & 0 deletions ir/transform/ssair_llir_lower.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ static void lower_into(vx_IrBlock *old, vx_IrBlock *dest);

static void into(vx_IrBlock *src, vx_IrOp *parent, vx_IrBlock *dest) {
bool *usedarr = malloc(sizeof(bool) * parent->outs_len);
assert(usedarr);
for (size_t outid = 0; outid < parent->outs_len; outid ++) {
vx_IrTypedVar var = parent->outs[outid];
vx_IrVar vv = src->outs[outid];
Expand Down

0 comments on commit 0750dc3

Please sign in to comment.