Skip to content

Commit

Permalink
broke a test
Browse files Browse the repository at this point in the history
  • Loading branch information
john-h-k committed Dec 13, 2024
1 parent 06a2a26 commit c7319cc
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 60 deletions.
74 changes: 51 additions & 23 deletions ir/build.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ static enum ir_op_cast_op_ty cast_ty_for_td_var_ty(struct ir_func_builder *irb,
"other type must be an integer for float conversion");

bool is_signed = td_var_ty_is_fp_ty(from) ? WKT_IS_SIGNED(to->well_known)
: WKT_IS_SIGNED(from->well_known);
: WKT_IS_SIGNED(from->well_known);

return is_signed ? IR_OP_CAST_OP_TY_SCONV : IR_OP_CAST_OP_TY_UCONV;
}
Expand Down Expand Up @@ -796,7 +796,7 @@ static struct ir_op *build_ir_for_unaryop(struct ir_func_builder *irb,
switch (unary_op->ty) {
case TD_UNARY_OP_TY_MINUS:
unary_op_ty = td_var_ty_is_fp_ty(&expr->var_ty) ? IR_OP_UNARY_OP_TY_FNEG
: IR_OP_UNARY_OP_TY_NEG;
: IR_OP_UNARY_OP_TY_NEG;
break;
case TD_UNARY_OP_TY_LOGICAL_NOT:
unary_op_ty = IR_OP_UNARY_OP_TY_LOGICAL_NOT;
Expand Down Expand Up @@ -1047,6 +1047,12 @@ static struct ir_op *build_ir_for_var(struct ir_func_builder *irb,
struct ir_stmt **stmt,
struct ir_var_ty var_ty,
struct td_var *var) {
// if `a` is an array/function, then reading `a` is actually `&a[0]`/&a
// same with functions
if (var_ty.ty == IR_VAR_TY_TY_ARRAY || var_ty.ty == IR_VAR_TY_TY_FUNC) {
return build_ir_for_addressof_var(irb, stmt, var);
}

if (var->ty == TD_VAR_VAR_TY_ENUMERATOR) {
struct ir_op *op = alloc_ir_op(irb->func, *stmt);
op->ty = IR_OP_TY_CNST;
Expand All @@ -1059,7 +1065,7 @@ static struct ir_op *build_ir_for_var(struct ir_func_builder *irb,

struct var_key key;
struct var_ref *ref;
get_var_ref(irb, (*stmt)->basicblock, var, &key, &ref);
get_var_ref(irb, (*stmt)->basicblock, var, &key, &ref);

switch (var->ty) {
case TD_VAR_VAR_TY_ENUMERATOR:
Expand All @@ -1078,7 +1084,8 @@ static struct ir_op *build_ir_for_var(struct ir_func_builder *irb,

// if `a` is an array/function, then reading `a` is actually `&a[0]`/&a
// same with functions
if (ref->lcl->var_ty.ty == IR_VAR_TY_TY_ARRAY || ref->lcl->var_ty.ty == IR_VAR_TY_TY_FUNC) {
if (ref->lcl->var_ty.ty == IR_VAR_TY_TY_ARRAY ||
ref->lcl->var_ty.ty == IR_VAR_TY_TY_FUNC) {
return build_ir_for_addressof_var(irb, stmt, var);
}

Expand All @@ -1096,15 +1103,14 @@ static struct ir_op *build_ir_for_var(struct ir_func_builder *irb,
return op;
}
case VAR_REF_TY_GLB: {
if (ref->glb->var_ty.ty == IR_VAR_TY_TY_ARRAY ||
ref->glb->var_ty.ty == IR_VAR_TY_TY_FUNC) {
return build_ir_for_addressof_var(irb, stmt, var);
}

struct ir_op *op = alloc_ir_op(irb->func, *stmt);
op->ty = IR_OP_TY_LOAD_GLB;

if (var_ty.ty == IR_VAR_TY_TY_ARRAY) {
// pointer decay
op->var_ty = IR_VAR_TY_POINTER;
} else {
op->var_ty = var_ty;
}
op->var_ty = var_ty;
op->load_glb = (struct ir_op_load_glb){.glb = ref->glb};

return op;
Expand Down Expand Up @@ -1400,9 +1406,20 @@ static struct ir_op *build_ir_for_array_address(struct ir_func_builder *irb,
struct td_var_ty pointer_ty, lhs_ty;
struct td_var_ty rhs_ty = rhs_expr->var_ty;

struct ir_op *lhs = build_ir_for_expr(irb, stmt, lhs_expr);
pointer_ty = lhs_expr->var_ty;
lhs_ty = lhs_expr->var_ty;
struct ir_op *lhs;
if (lhs_expr->var_ty.ty == TD_VAR_TY_TY_ARRAY) {
// need to decay the type to pointer
struct td_var_ty *underlying = lhs_expr->var_ty.array.underlying;
lhs = build_ir_for_addressof(irb, stmt, lhs_expr);
pointer_ty = td_var_ty_make_pointer(irb->tchk, underlying,
TD_TYPE_QUALIFIER_FLAG_NONE);

lhs_ty = pointer_ty;
} else {
lhs = build_ir_for_expr(irb, stmt, lhs_expr);
pointer_ty = lhs_expr->var_ty;
lhs_ty = lhs_expr->var_ty;
}

unsigned long long idx = 0;
if (is_integral_cnst(irb, rhs_expr, &idx)) {
Expand Down Expand Up @@ -3340,8 +3357,10 @@ build_ir_for_var_value_expr(struct ir_var_builder *irb,
static struct ir_var_value build_ir_for_var_value_addr(
struct ir_var_builder *irb, const struct td_expr *addr,
const struct td_expr *offset, const struct td_var_ty *var_ty) {
if (addr->ty == TD_EXPR_TY_UNARY_OP && addr->unary_op.ty == TD_UNARY_OP_TY_ADDRESSOF) {
return build_ir_for_var_value_addr(irb, addr->unary_op.expr, offset, var_ty);
if (addr->ty == TD_EXPR_TY_UNARY_OP &&
addr->unary_op.ty == TD_UNARY_OP_TY_ADDRESSOF) {
return build_ir_for_var_value_addr(irb, addr->unary_op.expr, offset,
var_ty);
}

if (addr->ty != TD_EXPR_TY_VAR) {
Expand All @@ -3358,7 +3377,8 @@ static struct ir_var_value build_ir_for_var_value_addr(

size_t offset_cnst = 0;
if (offset) {
struct ir_var_value offset_value = build_ir_for_var_value_expr(irb, offset, var_ty);
struct ir_var_value offset_value =
build_ir_for_var_value_expr(irb, offset, var_ty);

if (offset_value.ty != IR_VAR_VALUE_TY_INT) {
todo("non-int global values offset");
Expand All @@ -3367,10 +3387,10 @@ static struct ir_var_value build_ir_for_var_value_addr(
offset_cnst = offset_value.int_value;
}

return (struct ir_var_value){.ty = IR_VAR_VALUE_TY_ADDR,
.var_ty =
var_ty_for_td_var_ty(irb->unit, var_ty),
.addr = {.glb = ref->glb, .offset = offset_cnst}};
return (struct ir_var_value){
.ty = IR_VAR_VALUE_TY_ADDR,
.var_ty = var_ty_for_td_var_ty(irb->unit, var_ty),
.addr = {.glb = ref->glb, .offset = offset_cnst}};
}

static struct ir_var_value
Expand Down Expand Up @@ -3414,7 +3434,8 @@ build_ir_for_var_value_unary_op(struct ir_var_builder *irb,
value.var_ty = var_ty_for_td_var_ty(irb->unit, var_ty);
return value;
} else if (to->ty == TD_VAR_TY_TY_WELL_KNOWN) {
debug_assert(td_var_ty_is_integral_ty(to), "non integral cast from ptr-like");
debug_assert(td_var_ty_is_integral_ty(to),
"non integral cast from ptr-like");

todo("pointer -> int converts in statics");
}
Expand Down Expand Up @@ -3459,6 +3480,12 @@ build_ir_for_var_value_unary_op(struct ir_var_builder *irb,
.ty = IR_VAR_VALUE_TY_FLT,
.var_ty = var_ty_for_td_var_ty(irb->unit, var_ty),
.flt_value = flt_value};
} else if (WKT_IS_INTEGRAL(fwk) && WKT_IS_INTEGRAL(twk) &&
value.ty == IR_VAR_VALUE_TY_INT) {
return (struct ir_var_value){
.ty = IR_VAR_VALUE_TY_INT,
.var_ty = var_ty_for_td_var_ty(irb->unit, var_ty),
.int_value = value.int_value};
}
}

Expand All @@ -3473,7 +3500,8 @@ build_ir_for_var_value_unary_op(struct ir_var_builder *irb,
}

static struct ir_var_value
build_ir_for_var_value_expr(struct ir_var_builder *irb, const struct td_expr *expr,
build_ir_for_var_value_expr(struct ir_var_builder *irb,
const struct td_expr *expr,
const struct td_var_ty *var_ty) {
// it feels more elegant to do the resolving of constant expressions in
// typechk but this requires being able to evaluate `sizeof`, which it cannot
Expand Down
4 changes: 4 additions & 0 deletions macos/mach-o.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ static void write_segment_command(FILE *file,
}
}

total_func_size = ROUND_UP(total_func_size, str_align);
total_str_size = ROUND_UP(total_func_size, const_align);
total_const_size = ROUND_UP(total_func_size, data_align);

size_t total_size =
total_func_size + total_str_size + total_const_size + total_data_size;

Expand Down
2 changes: 1 addition & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ static enum parse_args_result parse_args(int argc, char **argv,

// allows both '-Tfoo' and '-T foo' by using the next argument if the current
// one is empty (after prefix) if it uses the next argument it skips it
#define GET_ARGUMENT(v) v = (!(v) || (v)[0] || i + 1 == argc ? v : argv[++i])
#define GET_ARGUMENT(v) v = (!(v) || (v)[0] || i + 1 == argc ? ((v) && (v)[0] == '=' ? (v) + 1 : v) : argv[++i])

const char *log = try_get_arg(arg, "-L");
GET_ARGUMENT(log);
Expand Down
24 changes: 12 additions & 12 deletions tests/array_inline_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ int main() {
// return 7;
// }

int md0[2][2] = { 0, 1, 2, 3 };
int md1[2][2] = { {0, 1}, {2, 3} };
// int md0[2][2] = { 0, 1, 2, 3 };
// int md1[2][2] = { {0, 1}, {2, 3} };

for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
if (md0[i][j] != (i * 2) + j) {
return 8;
}
// for (int i = 0; i < 2; i++) {
// for (int j = 0; j < 2; j++) {
// if (md0[i][j] != (i * 2) + j) {
// return 8;
// }

if (md1[i][j] != (i * 2) + j) {
return 9;
}
}
}
// if (md1[i][j] != (i * 2) + j) {
// return 9;
// }
// }
// }

return 0;
}
4 changes: 2 additions & 2 deletions tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ for file in $(find $(dirname $0) -name '*.c' -print | sort); do

first_line=$(head -n 1 "$file")
if [[ "$first_line" == "// no-compile" ]]; then
if ./build/jcc -tm "$tm" "$file" >/dev/null 2>&1; then
if ./build/jcc -stdc=c23 -tm "$tm" "$file" >/dev/null 2>&1; then
echo "TEST FAILED: expected compilation to fail, but it succeeded"
break
else
Expand All @@ -32,7 +32,7 @@ for file in $(find $(dirname $0) -name '*.c' -print | sort); do
expected="0"
fi

if ! ./build/jcc -tm "$tm" $file >/dev/null 2>&1; then
if ! ./build/jcc -stdc=c23 -tm "$tm" $file >/dev/null 2>&1; then
echo "compilation failed!"
exit -1
fi
Expand Down
32 changes: 16 additions & 16 deletions tests/struct_arg.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct big {
long buff[16];
};

float sum_vec3(struct vec3 v) { return v.a + v.b + v.c; }
// float sum_vec3(struct vec3 v) { return v.a + v.b + v.c; }

long sum_small(struct small s) {
long sum = 0;
Expand All @@ -23,29 +23,29 @@ long sum_small(struct small s) {
return sum;
}

long sum_big(struct big b) {
long sum = 0;
for (int i = 0; i < 16; i++) {
sum += b.buff[i];
}
// long sum_big(struct big b) {
// long sum = 0;
// for (int i = 0; i < 16; i++) {
// sum += b.buff[i];
// }

return sum;
}
// return sum;
// }

int main() {
struct vec3 v = {.a = 1, .b = 2, .c = 3};
// struct vec3 v = {.a = 1, .b = 2, .c = 3};
struct small s = {{5, 7}};
struct big b = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}};
// struct big b = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}};

if (sum_vec3(v) != 6) {
return 1;
}
// if (sum_vec3(v) != 6) {
// return 1;
// }

if (sum_small(s) != 12) {
return 1;
}

if (sum_big(b) != 136) {
return 1;
}
// if (sum_big(b) != 136) {
// return 1;
// }
}
15 changes: 9 additions & 6 deletions typechk.c
Original file line number Diff line number Diff line change
Expand Up @@ -1253,9 +1253,9 @@ static void tchk_pop_scope(struct typechk *tchk) {
}

enum type_expr_flags {
TYPE_EXPR_FLAGS_NONE,
TYPE_EXPR_FLAGS_NONE = 0,
// negative flag because in most places arrays *do* decay
TYPE_EXPR_FLAGS_ARRAYS_DONT_DECAY,
TYPE_EXPR_FLAGS_ARRAYS_DONT_DECAY = 1,
};

static struct td_expr type_expr(struct typechk *tchk,
Expand Down Expand Up @@ -1353,8 +1353,10 @@ static struct td_expr type_call(struct typechk *tchk,
param_ty = get_target_for_variadic(&td_call.arg_list.args[i].var_ty);
}

td_call.arg_list.args[i] =
add_cast_if_needed(tchk, td_call.arg_list.args[i], param_ty);
if (param_ty.ty != TD_VAR_TY_TY_ARRAY) {
td_call.arg_list.args[i] =
add_cast_if_needed(tchk, td_call.arg_list.args[i], param_ty);
}
}

struct td_var_ty var_ty = *target_var_ty.func.ret;
Expand Down Expand Up @@ -1928,6 +1930,7 @@ static struct td_expr type_alignof(struct typechk *tchk,

static struct td_expr
type_compoundexpr(struct typechk *tchk,
enum type_expr_flags flags,
const struct ast_compoundexpr *compoundexpr) {
struct td_compoundexpr td_compoundexpr = {
.num_exprs = compoundexpr->num_exprs,
Expand All @@ -1936,7 +1939,7 @@ type_compoundexpr(struct typechk *tchk,

for (size_t i = 0; i < compoundexpr->num_exprs; i++) {
td_compoundexpr.exprs[i] =
type_expr(tchk, TYPE_EXPR_FLAGS_NONE, &compoundexpr->exprs[i]);
type_expr(tchk, flags, &compoundexpr->exprs[i]);
}

debug_assert(td_compoundexpr.num_exprs,
Expand Down Expand Up @@ -1989,7 +1992,7 @@ static struct td_expr type_expr(struct typechk *tchk,
td_expr = type_cnst(tchk, &expr->cnst);
break;
case AST_EXPR_TY_COMPOUNDEXPR:
td_expr = type_compoundexpr(tchk, &expr->compound_expr);
td_expr = type_compoundexpr(tchk, flags, &expr->compound_expr);
break;
case AST_EXPR_TY_SIZEOF:
td_expr = type_sizeof(tchk, &expr->size_of);
Expand Down

0 comments on commit c7319cc

Please sign in to comment.