This repository has been archived by the owner on Apr 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TypeDiag.pm6
178 lines (162 loc) · 5.26 KB
/
TypeDiag.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use NativeCall;
use File::Temp;
module NativeCall::TypeDiag {
our @nctd-extracompileroptions is export;
our $CC = 'cc';
our $silent = False;
sub say(*@a) {
&CORE::say(|@a) unless $silent;
}
sub auto-diag($module-name, $conv-func = sub ($a){$a}, :@cheaders, :@clibs, :$deep) returns Bool is export {
my %cstructs;
my @functions;
my $ret = True;
$silent = False;
#require $module-name;
for ::("{$module-name}::EXPORT::DEFAULT").WHO.keys -> $export {
say ::($export).REPR;
if ::($export).REPR eq 'CStruct' {
say $conv-func($export);
%cstructs{$conv-func($export)} = ::($export);
}
if ::($export).does(Callable) and ::($export).^roles.perl ~~ /NativeCall/ {
@functions.push(::($export));
}
}
if ! $deep {
$ret &= diag-cstructs(:types(%cstructs), :cheaders(@cheaders), :clibs(@clibs));
} else {
for %cstructs.kv -> $k, $v {
$ret &= diag-struct($k, $v, :cheaders(@cheaders), :clibs(@clibs));
}
}
return $ret;
}
sub diag-cstructs(:@cheaders, :@clibs = (), :%types) returns Bool is export {
my @c_struct_list = %types.keys;
my @nc_struct_list = %types.values;
my $ret = True;
#for @nc_struct_list -> $t {
# say nativesizeof($t);
#}
my $c_header = "";
for @cheaders -> $s {
$c_header ~= "#include <$s>\n";
}
my $cprintf = '"';
my @t;
for @c_struct_list -> $s {
$cprintf ~= "%d,";
@t.push("sizeof($s)");
}
$cprintf = $cprintf.chop;
$cprintf ~= '",' ~ @t.join(',');
my $c = q:c:to/END_C/;
#include <stdio.h>
{$c_header}
END_C
$c ~= 'int main(int ac, char *ag[]) {';
$c ~= q:c:to/END_C/;
printf({$cprintf});
return 0;
END_C
$c ~= '}';
my $proc = compile_c($c, @clibs);
my @c_size = $proc.out.get.split(',');
#require ::($modulename);
for (@nc_struct_list Z @c_size).flat -> $nctype, $csize {
my $ncsize = nativesizeof($nctype);
my $ctypename;
for %types.kv -> $k, $v {
{$ctypename = $k; last} if $v ~~ $nctype;
}
if ($ncsize ne $csize) {
$ret = False;
say "Size in Perl6 is not the same that the C one for : {$nctype.^name} -- C size : $csize ; NC size : $ncsize";
diag-struct($ctypename, $nctype, :cheaders(@cheaders), :clibs(@clibs));
} else {
say "Size matched for P6:{$nctype.^name} - C:$ctypename";
}
}
return $ret;
}
sub diag-struct ($ctypename, $nctype, :@cheaders, :@clibs = ()) returns Bool is export {
say "Compiling a test file, this assume field names are the same";
my $cprintf = '"%d,';
say "-Perl6 name : {$nctype.^name}, C Name : $ctypename";
my @t1 = "sizeof($ctypename)";
for $nctype.^attributes -> $attr {
$cprintf ~= "%d,";
@t1.push("sizeof(piko.{$attr.name.substr(2)})")
}
my $c_header = "";
for @cheaders -> $s {
$c_header ~= "#include <$s>\n";
}
my $ncsize = nativesizeof($nctype);
$cprintf = $cprintf.chop;
$cprintf ~= '",' ~ @t1.join(',');
my $c = q:c:to/END_C/;
#include <stdio.h>
{$c_header}
END_C
$c ~= 'int main(int ac, char *ag[]) {';
$c ~= q:c:to/END_C/;
{$ctypename} piko;
printf({$cprintf});
return 0;
END_C
$c ~= '}';
my $proc = compile_c($c, @clibs);
my @c_size = $proc.out.get.split(',');
my $scsize = @c_size.shift;
my $totalc = 0;
my $totalnc = 0;
my $gissue;
for ($nctype.^attributes Z @c_size).flat -> $attr, $csize {
my $psize;
my $issue = '';
my $has = $attr.inlined ?? 'HAS' !! 'has';
if !$attr.inlined and $attr.type.REPR eq 'CStruct' {
$psize = nativesizeof(OpaquePointer);
} else {
try {
$psize = nativesizeof($attr.type);
CATCH {
when X::AdHoc {
$psize = nativesizeof(OpaquePointer) if $attr.type.^name eq 'Str';
if $attr.type.^name ne 'Str' {
$psize = 0;
$issue = "You used a type that not supported by NativeCall in CStruct repr: {$attr.type.^name}";
}
}
}
}
}
if $attr.type.^name eq 'str' {
$issue = "You should replace your 'str' type with 'Str'";
}
my $S = '';
$S = 'DONT MATCH' if $psize ne $csize;
$issue = "C size match nativesizeof({$attr.type.^name}). put HAS instead of has " if $attr.type.REPR eq 'CStruct' and !$attr.inlined and $csize == nativesizeof($attr.type);
say "__$has {$attr.type.^name} \$"~$attr.name.substr(2)~" : c-size=$csize | nc-size="~$psize~" -- $S: $issue";
$totalc += $csize;
$totalnc += $psize;
}
$gissue = "Your representation is smaller than the cstruct, but total size of fields match. Did you forget a field?" if $scsize > $ncsize and $totalc == $totalnc;
say "-Size given by sizeof and nativesizeof : C:$scsize/NC:$ncsize";
say "-Calculated total sizes : C:$totalc/NC:$totalnc";
say $gissue if $gissue;
return True;
}
sub compile_c($c, @clibs) {
my ($cfilename, $cfileh) = tempfile("********.c");
$cfileh.print($c);
my ($execfilename, $execfileh) = tempfile;
$execfileh.close();
#my $execfilename = "piko.exe";
#run 'cp', '/root/piko.exe', $execfilename;
run $CC, $cfilename, '-o', $execfilename, @clibs, @nctd-extracompileroptions;
return run $execfilename, :out;
}
}