-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathStubMOS.pl
290 lines (238 loc) · 8.21 KB
/
StubMOS.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
### Class StubMOS: Create a MorphOS stub file #################################
BEGIN {
package StubMOS;
use vars qw(@ISA);
@ISA = qw( Stub );
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my $self = $class->SUPER::new( @_ );
bless ($self, $class);
return $self;
}
sub header {
my $self = shift;
$self->SUPER::header (@_);
print "\n";
print "#include <emul/emulregs.h>\n";
print "#include <stdarg.h>\n";
print "\n";
}
sub function_proto {
my $self = shift;
my %params = @_;
my $prototype = $params{'prototype'};
if ($prototype->{type} eq 'varargs') {
if ($prototype->{subtype} ne 'tagcall') {
# We have to add the attribute to ourself first
$self->special_function_proto (@_);
print " __attribute__((varargs68k));\n";
print "\n";
$self->special_function_proto (@_);
}
}
else {
$self->SUPER::function_proto (@_);
}
}
sub function_start {
my $self = shift;
my %params = @_;
my $prototype = $params{'prototype'};
my $sfd = $self->{SFD};
if ($prototype->{type} eq 'function') {
print "\n";
print "{\n";
if (!$prototype->{nb}) {
print " BASE_EXT_DECL\n";
}
}
elsif ($prototype->{type} eq 'varargs') {
if ($prototype->{subtype} ne 'tagcall') {
my $na;
if ($prototype->{subtype} eq 'printfcall') {
$na = $prototype->{numargs} - 2;
}
else {
# methodcall: first vararg is removed
$na = $prototype->{numargs} - 3;
}
print "\n";
print "{\n";
print " va_list _va;\n";
print " va_start (_va, $prototype->{___argnames}[$na]);\n";
print " return $$prototype{'real_funcname'}(BASE_PAR_NAME ";
}
else {
# Shamelessly stolen from fd2inline ...
# number of regs that contain varargs
my $n = 9 - $prototype->{numregs};
# add 4 bytes if that's an odd number, to avoid splitting a tag
my $d = $n & 1 ? 4 : 0;
# offset of the start of the taglist
my $taglist = 8;
# size of the stack frame
my $local = ($taglist + $n * 4 + $d + 8 + 15) & ~15;
# Stack frame:
#
# 0 - 3: next frame ptr
# 4 - 7: save lr
# 8 - 8+n*4+d+8-1: tag list start
# ? - local-1: padding
print "__asm(\"\n";
print " .align 2\n";
print " .globl $prototype->{funcname}\n";
print " .type $prototype->{funcname},\@function\n";
print "$prototype->{funcname}:\n";
print " stwu 1,-$local(1)\n";
print " mflr 0\n";
printf " stw 0,%d(1)\n", $local + 4;
# If n is odd, one tag is split between regs and stack.
# Copy its ti_Data together with the ti_Tag.
if ($d != 0) {
# read ti_Data
printf " lwz 0,%d(1)\n", $local + 8;
}
# Save the registers
for my $count ($prototype->{numregs} .. 8) {
printf " stw %d,%d(1)\n",
$count + 2,
($count - $prototype->{numregs}) * 4 + $taglist;
}
if ($d != 0) {
# write ti_Data
printf " stw 0,%d(1)\n", $taglist + $n * 4;
}
# Add TAG_MORE
print " li 11,2\n";
printf " addi 0,1,%d\n", $local + 8 + $d;
printf " stw 11,%d(1)\n", $taglist + $n * 4 + $d;
printf " stw 0,%d(1)\n", $taglist + $n * 4 + $d + 4;
# vararg_reg = &saved regs
printf " addi %d,1,%d\n",
$prototype->{numregs} + 2, $taglist;
print " bl $prototype->{real_funcname}\n";
}
}
else {
$self->SUPER::function_start (@_);
}
}
sub function_arg {
my $self = shift;
my %params = @_;
my $prototype = $params{'prototype'};
my $argtype = $params{'argtype'};
my $argname = $params{'argname'};
my $argreg = $params{'argreg'};
my $argnum = $params{'argnum'};
my $sfd = $self->{SFD};
if ($$prototype{'type'} eq 'function') {
print " REG_" . (uc $argreg) . " = (ULONG) $argname;\n";
}
elsif ($prototype->{type} eq 'varargs') {
if ($prototype->{subtype} eq 'tagcall') {
}
elsif ($prototype->{subtype} eq 'methodcall' &&
$argnum == $prototype->{numargs} - 2) {
# Nuke it!
}
elsif ($argnum == $prototype->{numargs} - 1) {
my $vt = $$prototype{'argtypes'}[$$prototype{'numargs'} - 1];
print ", ($vt) _va->overflow_arg_area";
}
else {
$self->SUPER::function_arg (@_);
}
}
else {
$self->SUPER::function_arg (@_);
}
}
sub function_end {
my $self = shift;
my %params = @_;
my $prototype = $params{'prototype'};
my $sfd = $self->{SFD};
if ($$prototype{'type'} eq 'function') {
if (!$prototype->{nb}) {
print " REG_A6 = (ULONG) BASE_NAME;\n";
}
print " ";
if (!$prototype->{nr}) {
print "return ($prototype->{return}) ";
}
print "(*MyEmulHandle->EmulCallDirectOS)(-$prototype->{bias});\n";
print "}\n";
}
elsif ($prototype->{type} eq 'varargs') {
if ($prototype->{subtype} eq 'tagcall') {
# number of regs that contain varargs
my $n = 9 - $prototype->{numregs};
# add 4 bytes if that's an odd number, to avoid splitting a tag
my $d = $n & 1 ? 4 : 0;
# offset of the start of the taglist
my $taglist = 8;
# size of the stack frame
my $local = ($taglist + $n * 4 + $d + 8 + 15) & ~15;
# clear stack frame & return
printf " lwz 0,%d(1)\n", $local + 4;
print " mtlr 0\n";
printf " addi 1,1,%d\n", $local;
print " blr\n";
print ".L$prototype->{funcname}e1:\n";
print " .size $prototype->{funcname}," .
".L$prototype->{funcname}e1-$prototype->{funcname}\n";
print "\");\n";
}
else {
print ");\n";
print "}\n";
}
}
else {
$self->SUPER::function_end (@_);
}
}
sub special_function_proto {
my $self = shift;
my %params = @_;
my $prototype = $params{'prototype'};
my $decl_regular = $params{'decl_regular'};
my $sfd = $self->{SFD};
if ($prototype->{type} eq 'varargs' && $decl_regular) {
my $rproto = $prototype->{real_prototype};
print "$$rproto{'return'} $$rproto{'funcname'}(";
if (!$prototype->{nb}) {
if ($$rproto{'numargs'} == 0) {
print "BASE_PAR_DECL0";
}
else {
print "BASE_PAR_DECL ";
}
}
print join (', ', @{$$rproto{'___args'}});
print ");\n";
print "\n";
}
print "$$prototype{'return'}\n";
print "$$prototype{'funcname'}(";
if (!$prototype->{nb}) {
if ($$prototype{'numargs'} == 0) {
print "BASE_PAR_DECL0";
}
else {
print "BASE_PAR_DECL ";
}
}
my @newargs;
for my $i (0 .. $#{$prototype->{___args}}) {
if ($prototype->{subtype} ne 'methodcall' ||
$i != $prototype->{numargs} - 2 ) {
push @newargs, $prototype->{___args}[$i];
}
}
print join (', ', @newargs);
print ")";
}
}