authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-06 23:48:51-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-06 23:48:51-07:00
logef2c19d3c1c6fd881398bd79d92c290e90cee701
tree442444727b21c7620dc3ea32e7413e0dba0f2d9a
parent8a545069a48d466f812cbfc165fcd059f7585765

closer to linking

next problem is there needs to be a namespace because it's needed for getting the file, for debug info source location purposes.

2 files changed, 84 insertions(+), 40 deletions(-)

src/Module.zig+19
...@@ -4734,6 +4734,25 @@ pub fn createAnonymousDecl2(mod: *Module, typed_value: TypedValue, src_name: []c...@@ -4734,6 +4734,25 @@ pub fn createAnonymousDecl2(mod: *Module, typed_value: TypedValue, src_name: []c
4734 return new_decl;4734 return new_decl;
4735}4735}
47364736
4737pub fn createDecl2(mod: *Module, typed_value: TypedValue, name: []const u8) !*Decl {
4738 const duped_name = try mod.gpa.dupeZ(u8, name);
4739 const new_decl = try mod.allocateNewDecl(duped_name, undefined, 0, null);
4740
4741 new_decl.src_line = 0;
4742 new_decl.ty = typed_value.ty;
4743 new_decl.val = typed_value.val;
4744 new_decl.align_val = Value.@"null";
4745 new_decl.linksection_val = Value.@"null";
4746 new_decl.has_tv = true;
4747 new_decl.analysis = .complete;
4748 new_decl.generation = mod.generation;
4749 new_decl.alive = true;
4750
4751 try mod.comp.bin_file.allocateDeclIndexes(new_decl);
4752
4753 return new_decl;
4754}
4755
4737pub fn createAnonymousDeclFromDecl(4756pub fn createAnonymousDeclFromDecl(
4738 mod: *Module,4757 mod: *Module,
4739 src_decl: *Decl,4758 src_decl: *Decl,
src/aro/Codegen.zig+65-40
...@@ -58,7 +58,7 @@ pub fn generateTree(comp: *Compilation, aro_comp: *aro.Compilation, tree: aro.Tr...@@ -58,7 +58,7 @@ pub fn generateTree(comp: *Compilation, aro_comp: *aro.Compilation, tree: aro.Tr
58 const name = c.tree.tokSlice(node_datas[@enumToInt(decl_node)].decl.name);58 const name = c.tree.tokSlice(node_datas[@enumToInt(decl_node)].decl.name);
59 const fn_proto_ty = node_tys[@enumToInt(decl_node)];59 const fn_proto_ty = node_tys[@enumToInt(decl_node)];
60 const zig_fn_ty = try c.lowerType(fn_proto_ty);60 const zig_fn_ty = try c.lowerType(fn_proto_ty);
61 const new_decl = try mod.createAnonymousDecl2(.{61 const new_decl = try mod.createDecl2(.{
62 .ty = zig_fn_ty,62 .ty = zig_fn_ty,
63 .val = undefined,63 .val = undefined,
64 }, name);64 }, name);
...@@ -287,25 +287,48 @@ fn genFn(c: *Codegen, decl_node: NodeIndex) !void {...@@ -287,25 +287,48 @@ fn genFn(c: *Codegen, decl_node: NodeIndex) !void {
287 std.debug.print("# End Function AIR: {s}\n\n", .{name});287 std.debug.print("# End Function AIR: {s}\n\n", .{name});
288 }288 }
289289
290 @panic("TODO make a Decl and Fn");290 const node_tys = c.tree.nodes.items(.ty);
291 //c.bin_file.updateFunc(module, module_fn, air, liveness) catch |err| switch (err) {291 const aro_fn_ty = node_tys[@enumToInt(decl_node)];
292 // error.OutOfMemory => return error.OutOfMemory,292 const zig_fn_ty = try c.lowerType(aro_fn_ty);
293 // error.AnalysisFail => {293
294 // decl.analysis = .codegen_failure;294 const mod = c.bin_file.options.module.?;
295 // return;295 const module_fn = try c.gpa.create(Module.Fn);
296 // },296 const fn_decl = try mod.createDecl2(.{
297 // else => {297 .ty = zig_fn_ty,
298 // try module.failed_decls.ensureUnusedCapacity(gpa, 1);298 .val = try Value.Tag.function.create(c.arena, module_fn),
299 // module.failed_decls.putAssumeCapacityNoClobber(decl, try Module.ErrorMsg.create(299 }, name);
300 // gpa,300 //defer {
301 // decl.srcLoc(),301 // module_fn.deinit(c.gpa);
302 // "unable to codegen: {s}",302 // c.gpa.destroy(module_fn);
303 // .{@errorName(err)},303 //}
304 // ));304 module_fn.* = .{
305 // decl.analysis = .codegen_failure_retryable;305 .owner_decl = fn_decl,
306 // return;306 .zir_body_inst = undefined,
307 // },307 .lbrace_line = 0,
308 //};308 .rbrace_line = 0,
309 .lbrace_column = 0,
310 .rbrace_column = 0,
311 .state = .success,
312 };
313
314 c.bin_file.updateFunc(mod, module_fn, air, liveness) catch |err| switch (err) {
315 error.OutOfMemory => return error.OutOfMemory,
316 error.AnalysisFail => {
317 fn_decl.analysis = .codegen_failure;
318 return;
319 },
320 else => {
321 try mod.failed_decls.ensureUnusedCapacity(c.gpa, 1);
322 mod.failed_decls.putAssumeCapacityNoClobber(fn_decl, try Module.ErrorMsg.create(
323 c.gpa,
324 fn_decl.srcLoc(),
325 "unable to codegen: {s}",
326 .{@errorName(err)},
327 ));
328 fn_decl.analysis = .codegen_failure_retryable;
329 return;
330 },
331 };
309}332}
310333
311fn lowerType(c: *Codegen, aro_ty: aro.Type) Allocator.Error!Type {334fn lowerType(c: *Codegen, aro_ty: aro.Type) Allocator.Error!Type {
...@@ -328,23 +351,10 @@ fn lowerType(c: *Codegen, aro_ty: aro.Type) Allocator.Error!Type {...@@ -328,23 +351,10 @@ fn lowerType(c: *Codegen, aro_ty: aro.Type) Allocator.Error!Type {
328 .long_double => return Type.initTag(.c_longdouble),351 .long_double => return Type.initTag(.c_longdouble),
329352
330 // int foo(int bar, char baz, ...)353 // int foo(int bar, char baz, ...)
331 .var_args_func => {354 .var_args_func => return c.lowerTypeFunc(aro_ty, true),
332 const param_types = try c.arena.alloc(Type, aro_ty.data.func.params.len);355 // data.func
333 for (param_types) |*ty, i| {356 // int foo(int bar, char baz) and int (void)
334 ty.* = try c.lowerType(aro_ty.data.func.params[i].ty);357 .func => return c.lowerTypeFunc(aro_ty, false),
335 }
336
337 const zig_ty = try Type.Tag.function.create(c.arena, .{
338 .param_types = param_types,
339 .comptime_params = undefined,
340 .return_type = try c.lowerType(aro_ty.data.func.return_type),
341 .alignment = 0,
342 .cc = .C,
343 .is_var_args = true,
344 .is_generic = false,
345 });
346 return zig_ty;
347 },
348358
349 .pointer => {359 .pointer => {
350 const zig_ty = try Type.ptr(c.arena, .{360 const zig_ty = try Type.ptr(c.arena, .{
...@@ -365,9 +375,6 @@ fn lowerType(c: *Codegen, aro_ty: aro.Type) Allocator.Error!Type {...@@ -365,9 +375,6 @@ fn lowerType(c: *Codegen, aro_ty: aro.Type) Allocator.Error!Type {
365 // data.sub_type375 // data.sub_type
366 .unspecified_variable_len_array,376 .unspecified_variable_len_array,
367 .decayed_unspecified_variable_len_array,377 .decayed_unspecified_variable_len_array,
368 // data.func
369 // int foo(int bar, char baz) and int (void)
370 .func,
371 // int foo(bar, baz) and int foo()378 // int foo(bar, baz) and int foo()
372 // is also var args, but we can give warnings about incorrect amounts of parameters379 // is also var args, but we can give warnings about incorrect amounts of parameters
373 .old_style_func,380 .old_style_func,
...@@ -409,6 +416,24 @@ fn lowerType(c: *Codegen, aro_ty: aro.Type) Allocator.Error!Type {...@@ -409,6 +416,24 @@ fn lowerType(c: *Codegen, aro_ty: aro.Type) Allocator.Error!Type {
409 }416 }
410}417}
411418
419fn lowerTypeFunc(c: *Codegen, aro_ty: aro.Type, is_var_args: bool) Allocator.Error!Type {
420 const param_types = try c.arena.alloc(Type, aro_ty.data.func.params.len);
421 for (param_types) |*ty, i| {
422 ty.* = try c.lowerType(aro_ty.data.func.params[i].ty);
423 }
424
425 const zig_ty = try Type.Tag.function.create(c.arena, .{
426 .param_types = param_types,
427 .comptime_params = undefined,
428 .return_type = try c.lowerType(aro_ty.data.func.return_type),
429 .alignment = 0,
430 .cc = .C,
431 .is_var_args = is_var_args,
432 .is_generic = false,
433 });
434 return zig_ty;
435}
436
412fn lowerValue(c: *Codegen, aro_ty: aro.Type, aro_val: aro.Value) Allocator.Error!TypedValue {437fn lowerValue(c: *Codegen, aro_ty: aro.Type, aro_val: aro.Value) Allocator.Error!TypedValue {
413 const zig_ty = try c.lowerType(aro_ty);438 const zig_ty = try c.lowerType(aro_ty);
414 switch (aro_val.tag) {439 switch (aro_val.tag) {