@@ -139,6 +139,7 @@ pub fn updateFunc(self: *C, module: *Module, func_index: InternPool.Index, air:
139
139
140
140
const func = module .funcInfo (func_index );
141
141
const decl_index = func .owner_decl ;
142
+ const decl = module .declPtr (decl_index );
142
143
const gop = try self .decl_table .getOrPut (gpa , decl_index );
143
144
if (! gop .found_existing ) {
144
145
gop .value_ptr .* = .{};
@@ -163,7 +164,7 @@ pub fn updateFunc(self: *C, module: *Module, func_index: InternPool.Index, air:
163
164
.module = module ,
164
165
.error_msg = null ,
165
166
.decl_index = decl_index .toOptional (),
166
- .decl = module . declPtr ( decl_index ) ,
167
+ .is_naked_fn = decl . ty . fnCallingConvention ( module ) == .Naked ,
167
168
.fwd_decl = fwd_decl .toManaged (gpa ),
168
169
.ctypes = ctypes .* ,
169
170
},
@@ -216,15 +217,13 @@ pub fn updateDecl(self: *C, module: *Module, decl_index: Module.Decl.Index) !voi
216
217
fwd_decl .clearRetainingCapacity ();
217
218
code .clearRetainingCapacity ();
218
219
219
- const decl = module .declPtr (decl_index );
220
-
221
220
var object : codegen.Object = .{
222
221
.dg = .{
223
222
.gpa = gpa ,
224
223
.module = module ,
225
224
.error_msg = null ,
226
225
.decl_index = decl_index .toOptional (),
227
- .decl = decl ,
226
+ .is_naked_fn = false ,
228
227
.fwd_decl = fwd_decl .toManaged (gpa ),
229
228
.ctypes = ctypes .* ,
230
229
},
@@ -322,29 +321,19 @@ pub fn flushModule(self: *C, _: *Compilation, prog_node: *std.Progress.Node) !vo
322
321
self .lazy_code_buf .clearRetainingCapacity ();
323
322
try self .flushErrDecls (& f .lazy_ctypes );
324
323
324
+ // Unlike other backends, the .c code we are emitting has order-dependent decls.
325
325
// `CType`s, forward decls, and non-functions first.
326
- // Unlike other backends, the .c code we are emitting is order-dependent. Therefore
327
- // we must traverse the set of Decls that we are emitting according to their dependencies.
328
- // Our strategy is to populate a set of remaining decls, pop Decls one by one,
329
- // recursively chasing their dependencies.
330
- try f .remaining_decls .ensureUnusedCapacity (gpa , self .decl_table .count ());
331
-
332
- const decl_keys = self .decl_table .keys ();
333
- const decl_values = self .decl_table .values ();
334
- for (decl_keys ) | decl_index | {
335
- assert (module .declPtr (decl_index ).has_tv );
336
- f .remaining_decls .putAssumeCapacityNoClobber (decl_index , {});
337
- }
338
326
339
327
{
340
328
var export_names : std .AutoHashMapUnmanaged (InternPool.NullTerminatedString , void ) = .{};
341
329
defer export_names .deinit (gpa );
342
- try export_names .ensureTotalCapacity (gpa , @as ( u32 , @ intCast (module .decl_exports .entries .len ) ));
330
+ try export_names .ensureTotalCapacity (gpa , @intCast (module .decl_exports .entries .len ));
343
331
for (module .decl_exports .values ()) | exports | for (exports .items ) | @"export" |
344
332
try export_names .put (gpa , @"export" .opts .name , {});
345
333
346
- while (f .remaining_decls .popOrNull ()) | kv | {
347
- const decl_index = kv .key ;
334
+ const decl_keys = self .decl_table .keys ();
335
+ for (decl_keys ) | decl_index | {
336
+ assert (module .declPtr (decl_index ).has_tv );
348
337
try self .flushDecl (& f , decl_index , export_names );
349
338
}
350
339
}
@@ -355,9 +344,9 @@ pub fn flushModule(self: *C, _: *Compilation, prog_node: *std.Progress.Node) !vo
355
344
assert (f .ctypes .count () == 0 );
356
345
try self .flushCTypes (& f , .none , f .lazy_ctypes );
357
346
358
- var it = self .decl_table .iterator ();
359
- while ( it . next ()) | entry |
360
- try self . flushCTypes ( & f , entry . key_ptr . toOptional (), entry . value_ptr . ctypes );
347
+ for ( self . decl_table . keys (), self .decl_table .values ()) | decl_index , db | {
348
+ try self . flushCTypes ( & f , decl_index . toOptional (), db . ctypes );
349
+ }
361
350
}
362
351
363
352
f .all_buffers .items [ctypes_index ] = .{
@@ -374,6 +363,7 @@ pub fn flushModule(self: *C, _: *Compilation, prog_node: *std.Progress.Node) !vo
374
363
f .file_size += lazy_fwd_decl_len ;
375
364
376
365
// Now the code.
366
+ const decl_values = self .decl_table .values ();
377
367
try f .all_buffers .ensureUnusedCapacity (gpa , 1 + decl_values .len );
378
368
f .appendBufAssumeCapacity (self .lazy_code_buf .items );
379
369
for (decl_values ) | decl | f .appendBufAssumeCapacity (self .getString (decl .code ));
@@ -384,8 +374,6 @@ pub fn flushModule(self: *C, _: *Compilation, prog_node: *std.Progress.Node) !vo
384
374
}
385
375
386
376
const Flush = struct {
387
- remaining_decls : std .AutoArrayHashMapUnmanaged (Module.Decl.Index , void ) = .{},
388
-
389
377
ctypes : codegen.CType.Store = .{},
390
378
ctypes_map : std .ArrayListUnmanaged (codegen .CType .Index ) = .{},
391
379
ctypes_buf : std .ArrayListUnmanaged (u8 ) = .{},
@@ -416,7 +404,6 @@ const Flush = struct {
416
404
f .ctypes_buf .deinit (gpa );
417
405
f .ctypes_map .deinit (gpa );
418
406
f .ctypes .deinit (gpa );
419
- f .remaining_decls .deinit (gpa );
420
407
}
421
408
};
422
409
@@ -522,7 +509,7 @@ fn flushErrDecls(self: *C, ctypes: *codegen.CType.Store) FlushDeclError!void {
522
509
.module = self .base .options .module .? ,
523
510
.error_msg = null ,
524
511
.decl_index = .none ,
525
- .decl = null ,
512
+ .is_naked_fn = false ,
526
513
.fwd_decl = fwd_decl .toManaged (gpa ),
527
514
.ctypes = ctypes .* ,
528
515
},
@@ -556,7 +543,7 @@ fn flushLazyFn(self: *C, ctypes: *codegen.CType.Store, lazy_fn: codegen.LazyFnMa
556
543
.module = self .base .options .module .? ,
557
544
.error_msg = null ,
558
545
.decl_index = .none ,
559
- .decl = null ,
546
+ .is_naked_fn = false ,
560
547
.fwd_decl = fwd_decl .toManaged (gpa ),
561
548
.ctypes = ctypes .* ,
562
549
},
@@ -591,7 +578,6 @@ fn flushLazyFns(self: *C, f: *Flush, lazy_fns: codegen.LazyFnMap) FlushDeclError
591
578
}
592
579
}
593
580
594
- /// Assumes `decl` was in the `remaining_decls` set, and has already been removed.
595
581
fn flushDecl (
596
582
self : * C ,
597
583
f : * Flush ,
@@ -601,14 +587,6 @@ fn flushDecl(
601
587
const gpa = self .base .allocator ;
602
588
const mod = self .base .options .module .? ;
603
589
const decl = mod .declPtr (decl_index );
604
- // Before flushing any particular Decl we must ensure its
605
- // dependencies are already flushed, so that the order in the .c
606
- // file comes out correctly.
607
- for (decl .dependencies .keys ()) | dep | {
608
- if (f .remaining_decls .swapRemove (dep )) {
609
- try flushDecl (self , f , dep , export_names );
610
- }
611
- }
612
590
613
591
const decl_block = self .decl_table .getPtr (decl_index ).? ;
614
592
0 commit comments