| ... | @@ -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 | } |
| 289 | | 289 | |
| 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 | } |
| 310 | | 333 | |
| 311 | fn lowerType(c: *Codegen, aro_ty: aro.Type) Allocator.Error!Type { | 334 | 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,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), |
| 329 | | 352 | |
| 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 | }, | | |
| 348 | | 358 | |
| 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_type | 375 | // 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 parameters | 379 | // 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 | } |
| 411 | | 418 | |
| | 419 | fn 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 | |
| 412 | fn lowerValue(c: *Codegen, aro_ty: aro.Type, aro_val: aro.Value) Allocator.Error!TypedValue { | 437 | fn 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) { |