Skip to content

Commit 4e5d613

Browse files
committed
Compiler: move c2_trace functions to libs/c2/c2_trace.c2
* add c2_trace.c2 source module in libs/c2 (will later incorporate all libs/c2 files in an internal embedded library * generate call tracing tables in **c2_trace_tables.c** for fast builds Fix external array definitions: * accept array definitions without size in interface files * reject `elemsof` and `sizesof` on arrays of unknown lengh * fix external array definitions in C generator * fix test/interface/incremental_array.c2t: fix external array definition * fix test/interface/public_by_array_expr.c2t: fix external array definition * fix test/interface/muti_dimensional_array.c2t: `Row` and `Columns` definitions were reversed, 2D external array definition in C was incorrect
1 parent 6f8bf69 commit 4e5d613

25 files changed

+351
-292
lines changed

analyser/module_analyser.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ fn void Analyser.analyseGlobalVarDecl(Analyser* ma, VarDecl* v) {
563563
return;
564564
}
565565
} else {
566-
if (!init_expr) {
566+
if (!init_expr && !d.isExternal()) {
567567
ma.error(d.getLoc(), "array-type variable '%s' needs an explicit size or an initializer", d.getFullName());
568568
return;
569569
}

analyser/module_analyser_builtin.c2

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ fn QualType Analyser.analyseSizeof(Analyser* ma, BuiltinExpr* e) {
5959
if (qt.isInvalid()) return QualType_Invalid;
6060

6161
// TODO extract to function
62+
if (qt.isArray()) {
63+
const ArrayType* at = qt.getArrayTypeOrNil();
64+
if (at && !at.hasSize()) {
65+
ma.error(inner.getLoc(), "sizeof cannot be used on arrays of unknown length");
66+
return QualType_Invalid;
67+
}
68+
}
6269
if (qt.isStruct()) {
6370
StructType* st = qt.getStructType();
6471
const StructTypeDecl* std = st.getDecl();
@@ -95,6 +102,10 @@ fn QualType Analyser.analyseElemsof(Analyser* ma, BuiltinExpr* b) {
95102

96103
const ArrayType* at = qt.getArrayTypeOrNil();
97104
if (at) {
105+
if (!at.hasSize()) {
106+
ma.error(inner.getLoc(), "elemsof cannot be used on arrays of unknown length");
107+
return QualType_Invalid;
108+
}
98109
b.setUValue(at.getSize());
99110
return builtins[BuiltinKind.UInt32];
100111
}

ast/expr.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ public fn void Expr.printLiteral(const Expr* e, string_buffer.Buf* out) {
548548
IdentifierExpr.printLiteral((IdentifierExpr*)e, out);
549549
return;
550550
case Type:
551-
TypeExpr.printLiteral((TypeExpr*)e, out, false);
551+
TypeExpr.printLiteral((TypeExpr*)e, out);
552552
return;
553553
case Call:
554554
CallExpr.printLiteral((CallExpr*)e, out);

ast/module.c2

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public type Module struct @(opaque) {
2929
bool is_external;
3030
bool is_internal; // currently only for c2 module
3131
bool is_direct; // otherwise indirectly loaded via library.
32+
bool is_foreign; // module identifiers are not mangled
3233
bool is_exported;
3334
bool is_loaded; // the file(s) parsed. For external libs
3435
ModuleType* mt;
@@ -48,6 +49,7 @@ public fn Module* Module.create(ast_context.Context* c, u32 name_idx, bool is_ex
4849
m.mt = ModuleType.create(c, m);
4950
m.name_idx = name_idx;
5051
m.is_external = is_external;
52+
m.is_foreign = is_external; // default for external modules
5153
m.is_internal = false;
5254
m.is_direct = is_direct;
5355
m.is_loaded = false;
@@ -76,6 +78,9 @@ public fn bool Module.isInternal(const Module* m) { return m.is_internal; }
7678

7779
public fn bool Module.isExternal(const Module* m) { return m.is_external; }
7880

81+
public fn void Module.setForeign(Module* m, bool is_foreign) { m.is_foreign = is_foreign; }
82+
public fn bool Module.isForeign(const Module* m) { return m.is_foreign; }
83+
7984
public fn void Module.setLoaded(Module* m) { m.is_loaded = true; }
8085
public fn bool Module.isLoaded(const Module* m) { return m.is_loaded; }
8186

ast/type_expr.c2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,6 @@ fn void TypeExpr.print(const TypeExpr* e, string_buffer.Buf* out, u32 indent) {
7474
out.newline();
7575
}
7676

77-
fn void TypeExpr.printLiteral(const TypeExpr* e, string_buffer.Buf* out, bool decay) {
78-
e.typeRef.printLiteral(out, true, decay);
77+
fn void TypeExpr.printLiteral(const TypeExpr* e, string_buffer.Buf* out) {
78+
e.typeRef.printLiteral(out, true);
7979
}

ast/type_ref.c2

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public fn Expr** TypeRef.getArray2(TypeRef* r, u32 idx) {
332332
return &arrays[idx];
333333
}
334334

335-
public fn void TypeRef.printLiteral(const TypeRef* r, string_buffer.Buf* out, bool print_prefix, bool decay) {
335+
public fn void TypeRef.printLiteral(const TypeRef* r, string_buffer.Buf* out, bool print_prefix) {
336336
if (r.isConst()) out.add("const ");
337337
if (r.isVolatile()) out.add("volatile ");
338338

@@ -350,22 +350,16 @@ public fn void TypeRef.printLiteral(const TypeRef* r, string_buffer.Buf* out, bo
350350

351351
for (u32 i=0; i<r.flags.num_ptrs; i++) out.add1('*');
352352

353-
// note: convert arrays into pointers
354-
if (r.flags.incr_array) out.add("*");
353+
// TODO need the actual array length
354+
// should update the type with current array length or use VarDecl
355+
if (r.flags.incr_array) out.add("[]");
355356

356-
if (decay) {
357-
// TODO: this seems incorrect: char[10][10] does not decay as char**
358-
for (u32 i = 0; i < r.flags.num_arrays; i++) {
359-
out.add("*");
360-
}
361-
} else {
362-
for (u32 i = 0; i < r.flags.num_arrays; i++) {
363-
out.add1('[');
364-
const Expr* a = r.getArray(i);
365-
// note: a can be nil, when[]
366-
if (a) a.printLiteral(out);
367-
out.add1(']');
368-
}
357+
for (u32 i = 0; i < r.flags.num_arrays; i++) {
358+
out.add1('[');
359+
const Expr* a = r.getArray(i);
360+
// note: a can be nil, when[]
361+
if (a) a.printLiteral(out);
362+
out.add1(']');
369363
}
370364
}
371365

@@ -390,13 +384,20 @@ public fn void TypeRef.printLiteral2(const TypeRef* r, string_buffer.Buf* out, b
390384

391385
for (u32 i=0; i<r.flags.num_ptrs; i++) out.add1('*');
392386

393-
if (r.flags.incr_array) out.add("*");
387+
// TODO need the actual array length
388+
// should update the type with current array length or use VarDecl
389+
if (r.flags.incr_array) out.add("[]");
394390

395391
for (u32 i=0; i<r.flags.num_arrays; i++) {
396392
out.add1('[');
397393
const Expr* a = r.getArray(i);
398394
// note: a can be nil, when[]
399-
if (a) print_expr(arg, a, out);
395+
if (a) {
396+
if (print_expr)
397+
print_expr(arg, a, out);
398+
else
399+
a.printLiteral(out);
400+
}
400401
out.add1(']');
401402
}
402403
}

common/component.c2

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public type Component struct @(opaque) {
6363
u32 linkname; // into auxPool
6464
Kind kind;
6565
bool is_direct; // only for external, if used directly (otherwise sub-dep)
66+
bool is_foreign; // only for external
6667
bool available_static; // only for external
6768
bool available_dynamic; // only for external
6869

@@ -116,6 +117,14 @@ public fn const char* Component.getPath(const Component* c) {
116117
return c.auxPool.idx2str(c.dirname_idx);
117118
}
118119

120+
public fn void Component.setForeign(Component* c, bool is_foreign) {
121+
c.is_foreign = is_foreign;
122+
}
123+
124+
public fn bool Component.getForeign(const Component* c) {
125+
return c.is_foreign;
126+
}
127+
119128
public fn void Component.setLinkName(Component* c, const char* name) {
120129
c.linkname = c.auxPool.addStr(name, false);
121130
}

compiler/compiler.c2

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,13 @@ fn void Compiler.build(Compiler* c,
295295
if (opts.msan) c.addFeature("__MSAN__", "1");
296296
if (opts.ubsan) c.addFeature("__UBSAN__", "1");
297297

298+
target.addLib(c.auxPool.add("c2", 2, true), false);
299+
300+
if (c.opts.trace_calls) {
301+
if (!c.addLibFile("c2", "c2_trace.c2"))
302+
stdlib.exit(-1);
303+
}
304+
298305
c.parser = c2_parser.create(sm,
299306
diags,
300307
c.astPool,

compiler/compiler_libs.c2

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ fn void Compiler.open_lib(Compiler* c, Component* comp) {
113113
for (u32 i=0; i<mods.length(); i++) {
114114
u32 mod_name = mods.get_idx(i);
115115
ast.Module* m = comp.getOrAddModule(mod_name);
116-
if (!m) {
116+
if (m) {
117+
m.setForeign(comp.getForeign());
118+
} else {
117119
m = c.allmodules.find(mod_name);
118120
Component* other = c.find_component(m);
119121
assert(other);
@@ -148,6 +150,20 @@ fn bool Compiler.has_component(Compiler* c, u32 name) {
148150
return false;
149151
}
150152

153+
fn bool Compiler.addLibFile(Compiler* c, const char* libname, const char* filename) {
154+
char[512] dirname;
155+
char[512] fullname;
156+
157+
if (c.find_lib(libname, dirname)) {
158+
stdio.snprintf(fullname, elemsof(fullname), "%s/%s", dirname, filename);
159+
c.target.addFile(c.auxPool.addStr(fullname, true), 0);
160+
return true;
161+
} else {
162+
console.error("c2c: find library file %s/%s", libname, filename);
163+
return false;
164+
}
165+
}
166+
151167
fn bool Compiler.find_lib(const Compiler* c, const char* libname, char* fullpath) {
152168
for (u32 i=0; i<c.libdirs.length(); i++) {
153169
const char* dirname = c.libdirs.get(i);

compiler/main.c2

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,6 @@ fn bool Context.build_target(Context* c,
589589
}
590590
}
591591

592-
target.addLib(c.auxPool.add("c2", 2, true), false);
593-
594592
compiler.build(c.auxPool, c.sm, c.diags, c.build_info, target, &c.comp_opts, &c.pluginHandler);
595593

596594
// TODO unload target-specific plugins?

0 commit comments

Comments
 (0)