Skip to content

Commit c2474c8

Browse files
committed
Improve func_import and fix coreclr calling convention
1 parent f25492a commit c2474c8

4 files changed

Lines changed: 141 additions & 165 deletions

File tree

src/runtimes/coreclr.zig

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
const builtin = @import("builtin");
2+
const std = @import("std");
23

3-
const table = @import("func_import.zig").defineFuncImportTable("coreclr_", &.{
4-
.{ .name = "initialize", .ret = i32, .params = &.{
5-
.{ .name = "exe_path", .type = [*:0]const u8 },
6-
.{ .name = "app_domain_friendly_name", .type = [*:0]const u8 },
7-
.{ .name = "property_count", .type = i32 },
8-
.{ .name = "property_keys", .type = [*]const [*:0]const u8 },
9-
.{ .name = "property_values", .type = [*]const [*:0]const u8 },
10-
.{ .name = "host_handle", .type = *?*anyopaque },
11-
.{ .name = "domain_id", .type = *u32 },
12-
} },
13-
.{ .name = "create_delegate", .ret = i32, .params = &.{
14-
.{ .name = "host_handle", .type = *anyopaque },
15-
.{ .name = "domain_id", .type = u32 },
16-
.{ .name = "entry_point_assembly_name", .type = [*:0]const u8 },
17-
.{ .name = "entry_point_type_name", .type = [*:0]const u8 },
18-
.{ .name = "entry_point_method_name", .type = [*:0]const u8 },
19-
.{ .name = "delegate", .type = *?*anyopaque },
20-
} },
21-
}, if (builtin.os.tag == .windows) .{ .x86_stdcall = .{} } else .c);
4+
const cc: std.builtin.CallingConvention = .c;
5+
6+
const table = @import("func_import.zig").defineFuncImportTable(
7+
"coreclr_",
8+
struct {
9+
initialize: fn (
10+
exe_path: [*:0]const u8,
11+
app_domain_friendly_name: [*:0]const u8,
12+
property_count: i32,
13+
property_keys: [*]const [*:0]const u8,
14+
property_values: [*]const [*:0]const u8,
15+
host_handle: *?*anyopaque,
16+
domain_id: *u32,
17+
) callconv(cc) i32,
18+
create_delegate: fn (
19+
host_handle: *anyopaque,
20+
domain_id: u32,
21+
entry_point_assembly_name: [*:0]const u8,
22+
entry_point_type_name: [*:0]const u8,
23+
entry_point_method_name: [*:0]const u8,
24+
delegate: *?*anyopaque,
25+
) callconv(cc) i32,
26+
},
27+
);
2228

2329
pub const addrs = &table.addrs;
2430
pub const load = table.load;

src/runtimes/func_import.zig

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,13 @@ pub const Defn = struct {
1212
};
1313
};
1414

15-
pub fn defineFuncImportTable(comptime prefix: []const u8, comptime defns: []const Defn, comptime calling_convention: std.builtin.CallingConvention) type {
15+
pub fn defineFuncImportTable(comptime prefix: []const u8, comptime defns: type) type {
1616
comptime {
1717
const Type = std.builtin.Type;
18-
var fields: [defns.len]Type.StructField = undefined;
19-
for (defns, &fields) |defn, *field| {
20-
var params: [defn.params.len]Type.Fn.Param = undefined;
21-
for (defn.params, &params) |defn_param, *param| {
22-
param.* = .{
23-
.type = defn_param.type,
24-
.is_noalias = false,
25-
.is_generic = false,
26-
};
27-
}
28-
const T = ?*const @Type(.{
29-
.@"fn" = Type.Fn{
30-
.return_type = defn.ret,
31-
.params = &params,
32-
.is_var_args = false,
33-
.is_generic = false,
34-
.calling_convention = calling_convention,
35-
},
36-
});
18+
const defns_fields = @typeInfo(defns).@"struct".fields;
19+
var fields: [defns_fields.len]Type.StructField = undefined;
20+
for (defns_fields, &fields) |defn, *field| {
21+
const T = ?*const defn.type;
3722
field.* = .{
3823
.name = defn.name,
3924
.type = T,
@@ -53,7 +38,7 @@ pub fn defineFuncImportTable(comptime prefix: []const u8, comptime defns: []cons
5338
pub var addrs: AddrTable = .{};
5439

5540
pub fn load(lib: if (builtin.os.tag == .windows) std.os.windows.HMODULE else ?*anyopaque) void {
56-
inline for (defns) |defn| {
41+
inline for (defns_fields) |defn| {
5742
const name = prefix ++ defn.name;
5843
const ptr = switch (builtin.os.tag) {
5944
.windows => std.os.windows.kernel32.GetProcAddress(lib, name),

src/runtimes/il2cpp.zig

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
const table = @import("func_import.zig").defineFuncImportTable("il2cpp_", &.{
2-
.{ .name = "init", .ret = i32, .params = &.{
3-
.{ .name = "domain_name", .type = [*:0]const u8 },
4-
} },
5-
.{ .name = "runtime_invoke", .ret = i32, .params = &.{
6-
.{ .name = "method", .type = *anyopaque },
7-
.{ .name = "obj", .type = ?*anyopaque },
8-
.{ .name = "params", .type = *?*anyopaque },
9-
.{ .name = "exec", .type = *?*anyopaque },
10-
} },
11-
.{ .name = "method_get_name", .ret = [*:0]const u8, .params = &.{
12-
.{ .name = "method", .type = *anyopaque },
13-
} },
14-
}, .c);
1+
const std = @import("std");
2+
3+
const cc: std.builtin.CallingConvention = .c;
4+
5+
const table = @import("func_import.zig").defineFuncImportTable("il2cpp_", struct {
6+
init: fn (domain_name: [*:0]const u8) callconv(cc) i32,
7+
runtime_invoke: fn (
8+
method: *anyopaque,
9+
obj: ?*anyopaque,
10+
params: *?*anyopaque,
11+
exec: *?*anyopaque,
12+
) callconv(cc) i32,
13+
method_get_name: fn (method: *anyopaque) callconv(cc) [*:0]const u8,
14+
});
1515

1616
pub const addrs = &table.addrs;
1717
pub const load = table.load;

src/runtimes/mono.zig

Lines changed: 96 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ const logger = @import("../util/logging.zig").logger;
77
const runtimes = @import("../runtimes.zig");
88
const os_char = @import("../util.zig").os_char;
99

10-
//DEF_CALL\(([^,)]*), ((?:[^,)]+))((?:,(?:\n\s*)? [^,)]*)*)\)
11-
//.{ .name = "${2}", .ret = ${1}, .params = &.{${3}} },
12-
1310
pub const Array = opaque {};
1411
pub const Assembly = opaque {};
1512
pub const Class = opaque {};
@@ -22,114 +19,102 @@ pub const Object = opaque {};
2219
pub const String = opaque {};
2320
pub const Thread = opaque {};
2421

25-
const table = @import("func_import.zig").defineFuncImportTable("mono_", &.{
26-
.{ .name = "thread_current", .ret = *Thread, .params = &.{} },
27-
.{ .name = "thread_set_main", .ret = void, .params = &.{
28-
.{ .name = "thread", .type = *Thread },
29-
} },
30-
31-
.{ .name = "jit_init_version", .ret = *Domain, .params = &.{
32-
.{ .name = "root_domain_name", .type = [*:0]const u8 },
33-
.{ .name = "runtime_version", .type = [*:0]const u8 },
34-
} },
35-
.{ .name = "domain_assembly_open", .ret = *Assembly, .params = &.{
36-
.{ .name = "domain", .type = *anyopaque },
37-
.{ .name = "name", .type = [*:0]const u8 },
38-
} },
39-
.{ .name = "assembly_get_image", .ret = *Image, .params = &.{
40-
.{ .name = "assembly", .type = *Assembly },
41-
} },
42-
.{
43-
.name = "runtime_invoke",
44-
.ret = ?*Object,
45-
.params = &.{
46-
.{ .name = "method", .type = *Method },
47-
.{ .name = "obj", .type = ?*Object },
48-
// TODO: find docs and confirm this is nullable
49-
.{ .name = "params", .type = ?*?*anyopaque },
50-
.{ .name = "exc", .type = *?*Object },
51-
},
52-
},
53-
54-
.{ .name = "method_desc_new", .ret = *MethodDesc, .params = &.{
55-
.{ .name = "name", .type = [*:0]const u8 },
56-
.{ .name = "include_namespace", .type = i32 },
57-
} },
58-
.{ .name = "method_desc_search_in_image", .ret = ?*Method, .params = &.{
59-
.{ .name = "desc", .type = *MethodDesc },
60-
.{ .name = "image", .type = *Image },
61-
} },
62-
.{ .name = "method_desc_free", .ret = void, .params = &.{
63-
.{ .name = "desc", .type = *MethodDesc },
64-
} },
65-
.{ .name = "method_signature", .ret = *MethodSignature, .params = &.{
66-
.{ .name = "method", .type = *Method },
67-
} },
68-
.{ .name = "signature_get_param_count", .ret = u32, .params = &.{
69-
.{ .name = "sig", .type = *MethodSignature },
70-
} },
71-
72-
.{ .name = "domain_set_config", .ret = void, .params = &.{
73-
.{ .name = "domain", .type = *Domain },
74-
.{ .name = "base_dir", .type = [*:0]const u8 },
75-
.{ .name = "config_file_name", .type = [*:0]const u8 },
76-
} },
77-
.{ .name = "array_new", .ret = *Array, .params = &.{
78-
.{ .name = "domain", .type = *Domain },
79-
.{ .name = "eclass", .type = *anyopaque },
80-
.{ .name = "n", .type = u32 },
81-
} },
82-
.{ .name = "get_string_class", .ret = *Class, .params = &.{} },
83-
84-
.{ .name = "assembly_getrootdir", .ret = [*:0]const u8, .params = &.{} },
85-
86-
.{ .name = "set_dirs", .ret = void, .params = &.{
87-
.{ .name = "assembly_dir", .type = [*:0]const u8 },
88-
.{ .name = "config_dir", .type = [*:0]const u8 },
89-
} },
90-
.{ .name = "config_parse", .ret = void, .params = &.{
91-
.{ .name = "filename", .type = ?[*:0]const u8 },
92-
} },
93-
.{ .name = "set_assemblies_path", .ret = void, .params = &.{
94-
.{ .name = "path", .type = [*:0]const u8 },
95-
} },
96-
.{ .name = "object_to_string", .ret = *String, .params = &.{
97-
.{ .name = "obj", .type = *Object },
98-
.{ .name = "exc", .type = ?*?*Object },
99-
} },
100-
.{ .name = "string_to_utf8", .ret = [*:0]const u8, .params = &.{
101-
.{ .name = "str", .type = *String },
102-
} },
103-
.{ .name = "free", .ret = void, .params = &.{
104-
.{ .name = "ptr", .type = *anyopaque },
105-
} },
106-
.{ .name = "image_open_from_data_with_name", .ret = *Image, .params = &.{
107-
.{ .name = "data", .type = [*]const u8 },
108-
.{ .name = "data_len", .type = u32 },
109-
.{ .name = "need_copy", .type = i32 },
110-
.{ .name = "status", .type = *ImageOpenStatus },
111-
.{ .name = "refonly", .type = i32 },
112-
.{ .name = "name", .type = [*:0]const u8 },
113-
} },
114-
.{ .name = "assembly_load_from_full", .ret = *Assembly, .params = &.{
115-
.{ .name = "image", .type = *anyopaque },
116-
.{ .name = "fname", .type = [*:0]const u8 },
117-
.{ .name = "status", .type = *ImageOpenStatus },
118-
.{ .name = "refonly", .type = i32 },
119-
} },
120-
121-
.{ .name = "jit_parse_options", .ret = void, .params = &.{
122-
.{ .name = "argc", .type = i32 },
123-
.{ .name = "argv", .type = [*]const [*:0]const u8 },
124-
} },
125-
.{ .name = "debug_init", .ret = void, .params = &.{
126-
.{ .name = "domain", .type = DebugFormat },
127-
} },
128-
.{ .name = "debug_domain_create", .ret = void, .params = &.{
129-
.{ .name = "domain", .type = *Domain },
130-
} },
131-
.{ .name = "debug_enabled", .ret = i32, .params = &.{} },
132-
}, .c);
22+
const cc: std.builtin.CallingConvention = .c;
23+
24+
const table = @import("func_import.zig").defineFuncImportTable("mono_", struct {
25+
thread_current: fn () callconv(cc) *Thread,
26+
thread_set_main: fn (thread: *Thread) callconv(cc) void,
27+
28+
jit_init_version: fn (
29+
root_domain_name: [*:0]const u8,
30+
runtime_version: [*:0]const u8,
31+
) callconv(cc) *Domain,
32+
domain_assembly_open: fn (
33+
domain: *anyopaque,
34+
name: [*:0]const u8,
35+
) callconv(cc) *Assembly,
36+
assembly_get_image: fn (assembly: *Assembly) callconv(cc) *Image,
37+
runtime_invoke: fn (
38+
method: *Method,
39+
obj: ?*Object,
40+
// TODO: find docs and confirm this is nullable
41+
params: ?*?*anyopaque,
42+
exc: *?*Object,
43+
) callconv(cc) ?*Object,
44+
45+
method_desc_new: fn (
46+
name: [*:0]const u8,
47+
include_namespace: i32,
48+
) callconv(cc) *MethodDesc,
49+
method_desc_search_in_image: fn (
50+
desc: *MethodDesc,
51+
image: *Image,
52+
) callconv(cc) ?*Method,
53+
method_desc_free: fn (desc: *MethodDesc) callconv(cc) void,
54+
method_signature: fn (method: *Method) callconv(cc) *MethodSignature,
55+
signature_get_param_count: fn (sig: *MethodSignature) callconv(cc) u32,
56+
57+
domain_set_config: fn (
58+
domain: *Domain,
59+
base_dir: [*:0]const u8,
60+
config_file_name: [*:0]const u8,
61+
) callconv(cc) void,
62+
array_new: fn (
63+
domain: *Domain,
64+
eclass: *anyopaque,
65+
n: u32,
66+
) callconv(cc) *Array,
67+
get_string_class: fn () callconv(cc) *Class,
68+
69+
assembly_getrootdir: fn () callconv(cc) [*:0]const u8,
70+
71+
set_dirs: fn (
72+
assembly_dir: [*:0]const u8,
73+
config_dir: [*:0]const u8,
74+
) callconv(cc) void,
75+
config_parse: fn (
76+
filename: ?[*:0]const u8,
77+
) callconv(cc) void,
78+
set_assemblies_path: fn (
79+
path: [*:0]const u8,
80+
) callconv(cc) void,
81+
object_to_string: fn (
82+
obj: *Object,
83+
exc: ?*?*Object,
84+
) callconv(cc) *String,
85+
string_to_utf8: fn (
86+
str: *String,
87+
) callconv(cc) [*:0]const u8,
88+
free: fn (
89+
ptr: *anyopaque,
90+
) callconv(cc) void,
91+
image_open_from_data_with_name: fn (
92+
data: [*]const u8,
93+
data_len: u32,
94+
need_copy: i32,
95+
status: *ImageOpenStatus,
96+
refonly: i32,
97+
name: [*:0]const u8,
98+
) callconv(cc) *Image,
99+
assembly_load_from_full: fn (
100+
image: *anyopaque,
101+
fname: [*:0]const u8,
102+
status: *ImageOpenStatus,
103+
refonly: i32,
104+
) callconv(cc) *Assembly,
105+
106+
jit_parse_options: fn (
107+
argc: i32,
108+
argv: [*]const [*:0]const u8,
109+
) callconv(cc) void,
110+
debug_init: fn (
111+
domain: DebugFormat,
112+
) callconv(cc) void,
113+
debug_domain_create: fn (
114+
domain: *Domain,
115+
) callconv(cc) void,
116+
debug_enabled: fn () callconv(cc) i32,
117+
});
133118

134119
pub const addrs = &table.addrs;
135120
pub const load = table.load;

0 commit comments

Comments
 (0)