-
Notifications
You must be signed in to change notification settings - Fork 4
/
lxapigen.pl
executable file
·261 lines (211 loc) · 7.77 KB
/
lxapigen.pl
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/perl -w
use warnings;
use strict;
use File::Basename;
my %typesizes = (
'LONG' => 4,
'ULONG' => 4,
'SHORT' => 2,
'USHORT' => 2,
'HVIO' => 2,
'HKBD' => 2,
);
sub typesize {
my $t = shift;
return 4 if ($t =~ /\AP/); # pointers are 4 bytes (16:16).
die("Unknown type '$t', please update \%typesizes\n") if not defined $typesizes{$t};
return $typesizes{$t};
}
chdir(dirname(__FILE__)) or die("failed to chdir to script location: $!\n");
my $dirname = 'native';
opendir(DIRH, $dirname) or die("Failed to opendir '$dirname': $!\n");
while (readdir(DIRH)) {
next if not /\.h\Z/;
next if /\Aos2/;
next if /\-lx\.h\Z/;
my $module = $_;
my $header = "$dirname/$module";
$module =~ s/\.h\Z//;
open(IN, '<', $header) or die("Failed to open '$header' for reading: $!\n");
print("$module ...\n");
my %ordinalmap = ();
my $has16bitfns = 0;
while (<IN>) {
chomp;
next if not /OS2APIINFO/;
my $line = $_;
if (/\AOS2EXPORT\s+(.*?)\s+(OS2API|OS2API16)\s+(.*?)\s*\((.*?)\)\s+OS2APIINFO\((.*?)\);/) {
my %table = (
'rettype' => $1,
'apitype' => $2,
'fn' => $3,
'args' => $4,
);
my $apiinfo = $5;
my $is16bit = $table{'apitype'} eq 'OS2API16';
$table{'is16bit'} = $is16bit;
$has16bitfns |= $is16bit;
#print("rettype='$rettype' fn='$fn' args='$args' apiinfo='$apiinfo'\n");
my $fn = $table{'fn'};
my $ordinal = undef;
my $expname = undef;
if ($apiinfo =~ /\A(\d+)\Z/) {
$ordinal = int($1);
} else {
foreach (split(/,/, $apiinfo)) {
#print("apiinfoarg: '$_'\n");
my ($infokey, $infoval) = /\A(.*?)\=(.*)\Z/;
#print("apiinfo key='$infokey' val='$infoval'\n");
if ($infokey eq 'ord') {
if ($infoval =~ /\A(\d+)\Z/) {
$ordinal = int($1);
} else {
die("Invalid ordinal '$infoval' for '$fn'\n");
}
} elsif ($infokey eq 'name') {
$expname = $infoval;
} else {
die("unknown OS2APIINFO key '$infokey' for '$fn'\n");
}
}
}
$table{'ordinal'} = $ordinal;
$table{'expname'} = $expname if defined $expname;
if (defined $ordinalmap{$ordinal}) {
my $dupfn = $ordinalmap{$ordinal}{'fn'};
die("Duplicate ordinal #$ordinal between '$fn' and '$dupfn'\n");
}
$ordinalmap{$ordinal} = \%table;
} else {
die ("Couldn't parse:\n\n $line\n\n")
}
}
close(IN);
next if (not %ordinalmap); # no exported items?
#use Data::Dumper qw(Dumper); print Dumper \%ordinalmap;
# Here we go...
my $finalfname = "$dirname/$module-lx.h";
my $outfname = "$finalfname-new";
open(OUT, '>', $outfname) or die("Failed to open '$outfname' for writing: $!\n");
print OUT <<EOF
/**
* 2ine; an OS/2 emulator for Linux.
*
* Please see the file LICENSE.txt in the source's root directory.
*/
/* THIS FILE IS AUTOGENERATED. DO NOT EDIT BY HAND. see lxapigen.pl */
/* This is glue code for OS/2 binaries. Native binaries don't need this. */
#if LX_LEGACY
EOF
;
foreach (sort { $a <=> $b } keys(%ordinalmap) ) {
my $tableref = $ordinalmap{$_};
my $fn = $tableref->{'fn'};
my $argstr = $tableref->{'args'};
my $rettype = $tableref->{'rettype'};
my $is16bit = $tableref->{'is16bit'};
my @args = ();
if (($argstr ne 'VOID') and ($argstr ne 'void')) {
@args = split /,/, $argstr;
}
# Build a little wrapper that'll pull arguments off the stack and
# convert to whatever the native calling conventions are.
if ($is16bit) {
print OUT "static $rettype bridge16to32_$fn(uint8 *args) {\n";
#foreach (@args) {
# arguments are listed backwards here.
for my $i (reverse 0..$#args) {
my $arg = $args[$i];
my ($t, $n) = $arg =~ /\A\s*(.*?)\s+(.*?)\s*\Z/;
#print("arg='$_' t='$t' n='$n'\n");
my $a = ($t =~ /\AP/) ? 'PTRARG' : 'ARG'; # it's a pointer?
print OUT " LX_NATIVE_MODULE_16BIT_BRIDGE_$a($t, $n);\n";
}
print OUT " ";
if (($rettype ne 'void') && ($rettype ne 'VOID')) {
# Currently it's reasonable to assume the retval will land in EAX.
print OUT "return ";
}
print OUT "$fn(";
my $comma = '';
foreach (@args) {
my ($t, $n) = /\A\s*(.*?)\s+(.*?)\s*\Z/;
print OUT "$comma$n";
$comma = ', ';
}
print OUT ");\n";
print OUT "}\n\n";
}
}
if ($has16bitfns) {
print OUT "LX_NATIVE_MODULE_16BIT_SUPPORT()\n";
foreach (sort { $a <=> $b } keys(%ordinalmap) ) {
my $tableref = $ordinalmap{$_};
next if not $tableref->{'is16bit'};
my $fn = $tableref->{'fn'};
print OUT " LX_NATIVE_MODULE_16BIT_API($fn)\n";
}
print OUT "LX_NATIVE_MODULE_16BIT_SUPPORT_END()\n";
print OUT "\n";
print OUT "LX_NATIVE_MODULE_DEINIT({\n";
print OUT " LX_NATIVE_MODULE_DEINIT_16BIT_SUPPORT();\n";
print OUT "})\n";
print OUT "\n";
print OUT "static int init16_$module(void) {\n";
print OUT " LX_NATIVE_MODULE_INIT_16BIT_SUPPORT()\n";
foreach (sort { $a <=> $b } keys(%ordinalmap) ) {
my $tableref = $ordinalmap{$_};
next if not $tableref->{'is16bit'};
my $fn = $tableref->{'fn'};
my $argstr = $tableref->{'args'};
my $argbytes = 0;
my @args = ();
if (($argstr ne 'VOID') and ($argstr ne 'void')) {
@args = split /,/, $argstr;
}
foreach (@args) {
my ($t, $n) = /\A\s*(.*?)\s+(.*?)\s*\Z/;
$argbytes += typesize($t);
}
print OUT " LX_NATIVE_INIT_16BIT_BRIDGE($fn, $argbytes)\n";
}
print OUT " LX_NATIVE_MODULE_INIT_16BIT_SUPPORT_END()\n";
print OUT " return 1;\n";
print OUT "}\n\n";
}
print OUT "LX_NATIVE_MODULE_INIT(";
if ($has16bitfns) {
print OUT "{ if (!init16_$module()) return 0; }";
}
print OUT ")\n";
my $comma = '';
foreach (sort { $a <=> $b } keys(%ordinalmap) ) {
my $tableref = $ordinalmap{$_};
my $fn = $tableref->{'fn'};
my $expname = $tableref->{'expname'};
my $ordinal = $tableref->{'ordinal'};
my $suffix = $tableref->{'is16bit'} ? '16' : '';
print OUT $comma;
if (defined $expname) {
print OUT " LX_NATIVE_EXPORT${suffix}_DIFFERENT_NAME($fn, \"$expname\", $ordinal)";
} else {
print OUT " LX_NATIVE_EXPORT${suffix}($fn, $ordinal)";
}
$comma = ",\n";
}
print OUT "\n";
print OUT <<EOF
LX_NATIVE_MODULE_INIT_END()
#endif /* LX_LEGACY */
/* end of $module-lx.h ... */
EOF
;
close(OUT);
if ((system("diff -uN '$finalfname' '$outfname'") == -1) || ($? != 0)) {
rename($outfname, $finalfname) or die("Failed to rename '$outfname' to '$finalfname': $!\n");
} else {
unlink($outfname); # they match, just delete the new copy so build system doesn't rebuild everything.
}
}
closedir(DIRH);
# end of lxapigen.pl ...