Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions ast/ast_evaluator.c2
Original file line number Diff line number Diff line change
Expand Up @@ -281,17 +281,17 @@ fn Value Evaluator.eval_call(Evaluator* caller, const CallExpr* c) {

// Create a new stack frame and link it to the caller's
// TODO: handle Stack frames as separate allocated objects
Evaluator eval;

if (num_args > elemsof(eval.args)) {
return Value.error("too many arguments in pure function evaluation");
}
Evaluator eval /*@(noinit)*/;
eval.prev = caller;
eval.fd = fd;
eval.num_args = num_args;
eval.depth = caller.depth + 1;
eval.complexity = caller.complexity + 1;

if (num_args > elemsof(eval.args)) {
return Value.error("too many arguments in pure function evaluation");
}

//VarDecl** params = fd.getParams();

for (u32 i = 0; i < num_args; i++) {
Expand Down
2 changes: 1 addition & 1 deletion ast/value.c2
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ fn f64 fabs(f64 d) {
// string and trying to avoid using exponential notation
// TODO: should be in the C2 library as a type function f64.str()
public fn char *ftoa(char *dest, usize size, f64 d) {
char[32] buf;
char[32] buf /*@(noinit)*/;
usize pos = 0;

if (size < 2) {
Expand Down
9 changes: 9 additions & 0 deletions ast/var_decl.c2
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type VarDeclBits struct {
u32 has_init_call : 1; // local variables only
u32 attr_weak : 1; // globals only
u32 addr_used : 1;
u32 attr_noinit : 1;
u32 auto_attr : 2; // AutoAttr, for parameters only
u32 printf_format : 1; // for parameters only
}
Expand Down Expand Up @@ -301,6 +302,14 @@ public fn bool VarDecl.hasInitCall(const VarDecl* d) {
return d.base.varDeclBits.has_init_call;
}

public fn void VarDecl.setAttrNoInit(VarDecl* d) {
d.base.varDeclBits.attr_noinit = 1;
}

public fn bool VarDecl.hasAttrNoInit(const VarDecl* d) {
return d.base.varDeclBits.attr_noinit;
}

public fn void VarDecl.setAttrWeak(VarDecl* d) {
d.base.varDeclBits.attr_weak = 1;
}
Expand Down
3 changes: 3 additions & 0 deletions ast_utils/attr.c2
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public type AttrKind enum u8 {
AutoLine, // Var, function param only
AutoFunc, // Var, function param only
Embed, // Var, globals only
NoInit, // Var
}

const char*[] attrKind_names = {
Expand All @@ -67,6 +68,7 @@ const char*[] attrKind_names = {
"auto_line",
"auto_func",
"embed",
"noinit",
}

static_assert(elemsof(AttrKind), elemsof(attrKind_names));
Expand Down Expand Up @@ -146,6 +148,7 @@ const AttrReq[] Required_arg = {
[AttrKind.AutoLine] = AttrReq.NoArg,
[AttrKind.AutoFunc] = AttrReq.NoArg,
[AttrKind.Embed] = AttrReq.String,
[AttrKind.NoInit] = AttrReq.NoArg,
}
static_assert(elemsof(AttrKind), elemsof(Required_arg));

Expand Down
4 changes: 2 additions & 2 deletions ast_utils/string_buffer.c2
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public fn void Buf.stripTrailingSpaces(Buf* buf) {
}

public fn void Buf.print(Buf* buf, const char* format @(printf_format), ...) {
char[4096] tmp;
char[4096] tmp /*@(noinit)*/;
// NOTE: no growing
va_list args;
va_start(args, format);
Expand All @@ -178,7 +178,7 @@ public fn void Buf.print(Buf* buf, const char* format @(printf_format), ...) {
}

public fn void Buf.vprintf(Buf* buf, const char* format, va_list args) {
char[4096] tmp;
char[4096] tmp /*@(noinit)*/;
// NOTE: no growing
i32 len = vsnprintf(tmp, sizeof(tmp), format, args);
assert(len < sizeof(tmp));
Expand Down
3 changes: 3 additions & 0 deletions common/ast_builder.c2
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,9 @@ fn void Builder.actOnVarAttr(Builder* b, Decl* d, const Attr* a) {
case Embed:
b.storeAttr(d, a);
break;
case NoInit:
vd.setAttrNoInit();
break;
default:
b.diags.error(a.loc, "attribute '%s' is not applicable to variables",
kind2name(a.kind));
Expand Down
2 changes: 1 addition & 1 deletion common/file/file_utils.c2
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public fn const char* make_path3(char *buf, usize size, const char* dir, const c
// returns 0 on success, errno on failure
// create a directory path, OK if exists already
public fn i32 create_path(const char* path) {
char[file_utils.Max_path] tmp;
char[file_utils.Max_path] tmp /*@(noinit)*/;
char *p = tmp;
if (!*path) return 0;
*p++ = *path++;
Expand Down
3 changes: 1 addition & 2 deletions generator/c/c_generator.c2
Original file line number Diff line number Diff line change
Expand Up @@ -1280,8 +1280,7 @@ public fn void generate(string_pool.Pool* astPool,
return;
}

Generator gen;
gen.init(astPool, target, kind, output_dir, dir, diags, sm, build_info, mainFunc);
Generator gen.init(astPool, target, kind, output_dir, dir, diags, sm, build_info, mainFunc);
gen.auxPool = auxPool;
gen.enable_asserts = enable_asserts;
gen.fast_build = fast_build;
Expand Down
2 changes: 1 addition & 1 deletion generator/c/c_generator_special.c2
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn void Generator.createMakefile(Generator* gen,
out.print("CC=%s\n", cc);

out.add("CFLAGS=-Wall -Wextra -Wno-unused -Wno-switch\n");
out.add("CFLAGS+=-Wno-unused-parameter -Wno-missing-field-initializers -Wno-format-zero-length\n");
out.add("CFLAGS+=-Wno-unused-parameter -Wno-missing-field-initializers -Wno-missing-braces -Wno-format-zero-length\n");
out.add("CFLAGS+=-pipe -std=c99 -funsigned-char\n");
if (gen.fast_build)
out.add("CFLAGS+=-O0 -g\n");
Expand Down
4 changes: 4 additions & 0 deletions generator/c/c_generator_stmt.c2
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ fn void Generator.emitVarDecl(Generator* gen, VarDecl* vd, string_buffer.Buf* ou
out.add(" = ");
}
gen.emitExpr(out, ie);
} else {
if (!vd.hasAttrNoInit()) {
out.add(" = { 0 }");
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion parser/c2_parser.c2
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,10 @@ fn void Parser.parseTopLevel(Parser* p) {

// returns if embed attribute was seen
fn void Parser.parseOptionalAttributes(Parser* p) {
if (p.tok.kind != Kind.At) return;
if (p.tok.kind == Kind.At) p.parseAttributes();
}

fn void Parser.parseAttributes(Parser* p) {
p.consumeToken();

p.expectAndConsume(Kind.LParen);
Expand Down
17 changes: 14 additions & 3 deletions parser/c2_parser_stmt.c2
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import constants local;
import token local;
import src_loc local;
import string_list;
import string local;

fn Stmt* Parser.parseStmt(Parser* p) {
// TODO use Jump Table (combined one for multiple purposes?)
Expand Down Expand Up @@ -516,7 +517,7 @@ fn Stmt* Parser.parseWhileStmt(Parser* p) {
}

fn Stmt* Parser.parseDeclStmt(Parser* p, bool checkSemi, bool allowLocal, bool isCondition) {
VarDecl*[MaxMultiDecl] decls;
VarDecl*[MaxMultiDecl] decls /*@(noinit)*/;
u32 num_decls = 0;
bool has_local = false;
if (p.tok.kind == Kind.KW_local) {
Expand All @@ -541,6 +542,7 @@ fn Stmt* Parser.parseDeclStmt(Parser* p, bool checkSemi, bool allowLocal, bool i
p.consumeToken();

// TODO same as parseVarDecl()
bool has_noinit = false;
bool has_init_call = false;
need_semi = true;
Expr* initValue = nil;
Expand All @@ -555,7 +557,14 @@ fn Stmt* Parser.parseDeclStmt(Parser* p, bool checkSemi, bool allowLocal, bool i
need_semi = checkSemi && initValue.needsSemi();
break;
case At:
p.error("local variables cannot have attributes");
// check for noinit attribute
p.consumeToken();
p.expectAndConsume(Kind.LParen);
p.expectIdentifier();
if (strcmp(p.pool.idx2str(p.tok.name_idx), "noinit"))
p.error("local variables cannot have attributes");
has_noinit = true;
p.expectAndConsume(Kind.RParen);
break;
case Dot:
if (p.peekToken(1) == Kind.Identifier
Expand Down Expand Up @@ -583,7 +592,9 @@ fn Stmt* Parser.parseDeclStmt(Parser* p, bool checkSemi, bool allowLocal, bool i
default:
break;
}
decls[num_decls++] = p.builder.actOnVarDecl(name, loc, &ref, assignLoc, initValue, has_local, has_init_call);
VarDecl* vd = p.builder.actOnVarDecl(name, loc, &ref, assignLoc, initValue, has_local, has_init_call);
if (has_noinit) vd.setAttrNoInit();
decls[num_decls++] = vd;
if (p.tok.kind != Kind.Comma)
break;
p.consumeToken();
Expand Down
4 changes: 2 additions & 2 deletions parser/c2_tokenizer.c2
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ check_overflow:
}

fn void Tokenizer.lex_floating_point(Tokenizer* t, Token* result, const char* start) {
char[4096] buf;
char[128] buf /*@(noinit)*/;
const char* p = start;
usize pos = 0;
u8 seen_dot = 0;
Expand Down Expand Up @@ -939,7 +939,7 @@ too_large:
}

fn void Tokenizer.lex_floating_point_hex(Tokenizer* t, Token* result, const char* start) {
char[4096] buf;
char[128] buf /*@(noinit)*/;
const char* p = start;
usize pos = 0;
u8 seen_dot = 0;
Expand Down
2 changes: 1 addition & 1 deletion test/auto_args/struct_function_before_self.c2t
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static void test_Foo_test(const char* file, uint32_t line, test_Foo* f, void* p)

int32_t main(void)
{
test_Foo f;
test_Foo f = { 0 };
test_Foo_test("file1.c2", 11, &f, NULL);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion test/auto_args/struct_function_ok.c2t
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static void test_Foo_test(test_Foo* f, const char* file, uint32_t line, void* p)

int32_t main(void)
{
test_Foo f;
test_Foo f = { 0 };
test_Foo_test(&f, "file1.c2", 11, NULL);
test_Foo_test(&f, "file1.c2", 12, NULL);
return 0;
Expand Down
12 changes: 6 additions & 6 deletions test/c_generator/asm/gen_asm.c2t
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ static void test_basic_asm(void);

static int32_t test_add(int32_t x, int32_t y)
{
int32_t result;
int32_t result = { 0 };
__asm__ volatile ("add %[Rd], %[Rm], %[Rn]" : [Rd] "=r" (result) : [Rm] "r" (x), [Rn] "r" (y));
return result;
}

static void test_clobbers(void)
{
int32_t from;
int32_t to;
int32_t count;
int32_t from = { 0 };
int32_t to = { 0 };
int32_t count = { 0 };
__asm__ volatile ("movc3 %0, %1, %2"
:
: "g" (from), "g" (to), "g" (count)
Expand All @@ -67,8 +67,8 @@ static void test_clobbers(void)
static inline
uint64_t test_rdtsc(void)
{
uint32_t lo;
uint32_t hi;
uint32_t lo = { 0 };
uint32_t hi = { 0 };
__asm__ volatile ("rdtsc" : "=a" (lo), "=d" (hi));
uint64_t res = hi;
res <<= 32;
Expand Down
2 changes: 1 addition & 1 deletion test/c_generator/expr/prec_expr.c2t
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public fn i32 main(i32 argc, const char** argv) {

int32_t main(int32_t argc, const char** argv)
{
int32_t a;
int32_t a = { 0 };

a = (1 << 2) + 3;
a = ((1 ^ 2) | 3) & 4;
Expand Down
2 changes: 1 addition & 1 deletion test/c_generator/functions/inline/inline_prefix.c2t
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Point struct {
static inline
void test1_myfunc(const test2_Point* p)
{
test2_Point points[3];
test2_Point points[3] = { 0 };
points[1] = *p;
}

4 changes: 2 additions & 2 deletions test/c_generator/functions/inline/inline_stmts.c2t
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ next:
static inline
int32_t test1_test_fn(const char* format, ...)
{
const char* p;
const char* p = { 0 };
{
const char* q = format;
if (q) {
Expand All @@ -137,7 +137,7 @@ int32_t test1_test_fn(const char* format, ...)
if (format) p = format;
else p = format;
{
const char* q;
const char* q = { 0 };
while ((q = format)) {
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/c_generator/functions/inline/lib_public_inline.c2t
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ typedef void (*test1_Handler)(int32_t* _arg0);
static inline
int32_t test1_test_fn(test1_Handler a, int32_t** b)
{
int32_t c[4];
int32_t c[4] = { 0 };
a(*b);
return 123;
}
Expand Down
2 changes: 1 addition & 1 deletion test/c_generator/functions/inline/nonpublic_inline.c2t
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ void test1_test_fn(void)
{
test1_non_public();
test1_num++;
test1_Priv p;
test1_Priv p = { 0 };
test1_Handler h = test1_non_public;
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static test_B* test_C_getB(test_C* c)

static void test_test1(void)
{
test_C c;
test_C c = { 0 };
uint32_t n = test_A_run(test_B_getA(test_C_getB(&c)));
}

2 changes: 1 addition & 1 deletion test/c_generator/functions/struct_functions/local.c2t
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static void test_Type_init(test_Type* _arg0)

int32_t main(void)
{
test_Type t;
test_Type t = { 0 };
test_Type_init(&t);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion test/c_generator/functions/struct_functions/local_arg.c2t
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static void test_Type_init(test_Type* _arg0, int32_t _arg1)

int32_t main(void)
{
test_Type t;
test_Type t = { 0 };
test_Type_init(&t, 4);
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static void test_Type_init(test_Type* _arg0)

int32_t main(void)
{
test_Outer o;
test_Outer o = { 0 };
test_Type_init(&o.t);
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct test_Type_ {

int32_t main(void)
{
test_Type t;
test_Type t = { 0 };
t.init(1);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion test/c_generator/functions/struct_functions/local_ptr.c2t
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static void test_Type_init(test_Type* _arg0)

int32_t main(void)
{
test_Type* t;
test_Type* t = { 0 };
test_Type_init(t);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion test/c_generator/stmts/local_array_decl.c2t
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ static void test_func1(void);

static void test_func1(void)
{
int32_t board2[20][20];
int32_t board2[20][20] = { 0 };
}

Loading