~ubuntu-branches/ubuntu/precise/openarena/precise

« back to all changes in this revision

Viewing changes to code/qcommon/vm_ppc_new.c

  • Committer: Bazaar Package Importer
  • Author(s): Bruno "Fuddl" Kleinert
  • Date: 2007-01-20 12:28:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070120122809-2yza5ojt7nqiyiam
Tags: upstream-0.6.0
ImportĀ upstreamĀ versionĀ 0.6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
===========================================================================
 
3
Copyright (C) 1999-2005 Id Software, Inc.
 
4
 
 
5
This file is part of Quake III Arena source code.
 
6
 
 
7
Quake III Arena source code is free software; you can redistribute it
 
8
and/or modify it under the terms of the GNU General Public License as
 
9
published by the Free Software Foundation; either version 2 of the License,
 
10
or (at your option) any later version.
 
11
 
 
12
Quake III Arena source code is distributed in the hope that it will be
 
13
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
GNU General Public License for more details.
 
16
 
 
17
You should have received a copy of the GNU General Public License
 
18
along with Quake III Arena source code; if not, write to the Free Software
 
19
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
20
===========================================================================
 
21
*/
 
22
// vm_ppc.c
 
23
// ppc dynamic compiler
 
24
 
 
25
#include "vm_local.h"
 
26
 
 
27
#ifdef MACOS_X
 
28
#include <CoreServices/CoreServices.h>
 
29
#endif
 
30
 
 
31
#define DEBUG_VM 0
 
32
 
 
33
#if DEBUG_VM
 
34
static char     *opnames[256] = {
 
35
        "OP_UNDEF", 
 
36
 
 
37
        "OP_IGNORE", 
 
38
 
 
39
        "OP_BREAK",
 
40
 
 
41
        "OP_ENTER",
 
42
        "OP_LEAVE",
 
43
        "OP_CALL",
 
44
        "OP_PUSH",
 
45
        "OP_POP",
 
46
 
 
47
        "OP_CONST",
 
48
 
 
49
        "OP_LOCAL",
 
50
 
 
51
        "OP_JUMP",
 
52
 
 
53
        //-------------------
 
54
 
 
55
        "OP_EQ",
 
56
        "OP_NE",
 
57
 
 
58
        "OP_LTI",
 
59
        "OP_LEI",
 
60
        "OP_GTI",
 
61
        "OP_GEI",
 
62
 
 
63
        "OP_LTU",
 
64
        "OP_LEU",
 
65
        "OP_GTU",
 
66
        "OP_GEU",
 
67
 
 
68
        "OP_EQF",
 
69
        "OP_NEF",
 
70
 
 
71
        "OP_LTF",
 
72
        "OP_LEF",
 
73
        "OP_GTF",
 
74
        "OP_GEF",
 
75
 
 
76
        //-------------------
 
77
 
 
78
        "OP_LOAD1",
 
79
        "OP_LOAD2",
 
80
        "OP_LOAD4",
 
81
        "OP_STORE1",
 
82
        "OP_STORE2",
 
83
        "OP_STORE4",
 
84
        "OP_ARG",
 
85
 
 
86
        "OP_BLOCK_COPY",
 
87
 
 
88
        //-------------------
 
89
 
 
90
        "OP_SEX8",
 
91
        "OP_SEX16",
 
92
 
 
93
        "OP_NEGI",
 
94
        "OP_ADD",
 
95
        "OP_SUB",
 
96
        "OP_DIVI",
 
97
        "OP_DIVU",
 
98
        "OP_MODI",
 
99
        "OP_MODU",
 
100
        "OP_MULI",
 
101
        "OP_MULU",
 
102
 
 
103
        "OP_BAND",
 
104
        "OP_BOR",
 
105
        "OP_BXOR",
 
106
        "OP_BCOM",
 
107
 
 
108
        "OP_LSH",
 
109
        "OP_RSHI",
 
110
        "OP_RSHU",
 
111
 
 
112
        "OP_NEGF",
 
113
        "OP_ADDF",
 
114
        "OP_SUBF",
 
115
        "OP_DIVF",
 
116
        "OP_MULF",
 
117
 
 
118
        "OP_CVIF",
 
119
        "OP_CVFI"
 
120
};
 
121
#endif
 
122
 
 
123
typedef enum {
 
124
    R_REAL_STACK = 1,
 
125
        // registers 3-11 are the parameter passing registers
 
126
        
 
127
        // state
 
128
        R_STACK = 3,                    // local
 
129
        R_OPSTACK,                              // global
 
130
 
 
131
        // constants    
 
132
        R_MEMBASE,                      // global
 
133
        R_MEMMASK,
 
134
        R_ASMCALL,                      // global
 
135
    R_INSTRUCTIONS,             // global
 
136
    R_NUM_INSTRUCTIONS, // global
 
137
    R_CVM,                              // currentVM
 
138
    
 
139
        // temps
 
140
        R_TOP = 11,
 
141
        R_SECOND = 12,
 
142
        R_EA = 2                                // effective address calculation
 
143
         
 
144
} regNums_t;
 
145
 
 
146
#define RG_REAL_STACK           r1
 
147
#define RG_STACK                        r3
 
148
#define RG_OPSTACK                      r4
 
149
#define RG_MEMBASE                      r5
 
150
#define RG_MEMMASK                      r6
 
151
#define RG_ASMCALL                      r7
 
152
#define RG_INSTRUCTIONS         r8
 
153
#define RG_NUM_INSTRUCTIONS     r9
 
154
#define RG_CVM                          r10
 
155
#define RG_TOP                          r12
 
156
#define RG_SECOND                       r13
 
157
#define RG_EA                           r14
 
158
 
 
159
// The deepest value I saw in the Quake3 games was 9.
 
160
#define OP_STACK_MAX_DEPTH      16
 
161
 
 
162
// These are all volatile and thus must be saved upon entry to the VM code.
 
163
// NOTE: These are General Purpose Registers (GPR) numbers like the 
 
164
//       R_ definitions in the regNums_t enum above (31 is the max)
 
165
static int opStackIntRegisters[OP_STACK_MAX_DEPTH] =
 
166
{
 
167
        16, 17, 18, 19,
 
168
        20, 21, 22, 23,
 
169
        24, 25, 26, 27,
 
170
        28, 29, 30, 31
 
171
};
 
172
 
 
173
static unsigned int *opStackLoadInstructionAddr[OP_STACK_MAX_DEPTH];
 
174
 
 
175
// We use different registers for the floating point
 
176
// operand stack (these are volatile in the PPC ABI)
 
177
// NOTE: these are Floating Point Register (FPR) numbers, not 
 
178
//       General Purpose Register (GPR) numbers
 
179
static int opStackFloatRegisters[OP_STACK_MAX_DEPTH] =
 
180
{
 
181
        0, 1, 2, 3,
 
182
        4, 5, 6, 7,
 
183
        8, 9, 10, 11,
 
184
        12, 13, 14, 15
 
185
};
 
186
 
 
187
static int opStackRegType[OP_STACK_MAX_DEPTH] =
 
188
{
 
189
        0, 0, 0, 0,
 
190
        0, 0, 0, 0,
 
191
        0, 0, 0, 0,
 
192
        0, 0, 0, 0
 
193
};
 
194
 
 
195
// this doesn't have the low order bits set for instructions i'm not using...
 
196
typedef enum {
 
197
        PPC_TDI         = 0x08000000,
 
198
        PPC_TWI         = 0x0c000000,
 
199
        PPC_MULLI       = 0x1c000000,
 
200
        PPC_SUBFIC      = 0x20000000,
 
201
        PPC_CMPI        = 0x28000000,
 
202
        PPC_CMPLI       = 0x2c000000,
 
203
        PPC_ADDIC       = 0x30000000,
 
204
        PPC_ADDIC_      = 0x34000000,
 
205
        PPC_ADDI        = 0x38000000,
 
206
        PPC_ADDIS       = 0x3c000000,
 
207
        PPC_BC          = 0x40000000,
 
208
        PPC_SC          = 0x44000000,
 
209
        PPC_B           = 0x48000000,
 
210
 
 
211
        PPC_MCRF        = 0x4c000000,
 
212
        PPC_BCLR        = 0x4c000020,
 
213
        PPC_RFID        = 0x4c000000,
 
214
        PPC_CRNOR       = 0x4c000000,
 
215
        PPC_RFI         = 0x4c000000,
 
216
        PPC_CRANDC      = 0x4c000000,
 
217
        PPC_ISYNC       = 0x4c000000,
 
218
        PPC_CRXOR       = 0x4c000000,
 
219
        PPC_CRNAND      = 0x4c000000,
 
220
        PPC_CREQV       = 0x4c000000,
 
221
        PPC_CRORC       = 0x4c000000,
 
222
        PPC_CROR        = 0x4c000000,
 
223
//------------
 
224
        PPC_BCCTR       = 0x4c000420,
 
225
        PPC_RLWIMI      = 0x50000000,
 
226
        PPC_RLWINM      = 0x54000000,
 
227
        PPC_RLWNM       = 0x5c000000,
 
228
        PPC_ORI         = 0x60000000,
 
229
        PPC_ORIS        = 0x64000000,
 
230
        PPC_XORI        = 0x68000000,
 
231
        PPC_XORIS       = 0x6c000000,
 
232
        PPC_ANDI_       = 0x70000000,
 
233
        PPC_ANDIS_      = 0x74000000,
 
234
        PPC_RLDICL      = 0x78000000,
 
235
        PPC_RLDICR      = 0x78000000,
 
236
        PPC_RLDIC       = 0x78000000,
 
237
        PPC_RLDIMI      = 0x78000000,
 
238
        PPC_RLDCL       = 0x78000000,
 
239
        PPC_RLDCR       = 0x78000000,
 
240
        PPC_CMP         = 0x7c000000,
 
241
        PPC_TW          = 0x7c000000,
 
242
        PPC_SUBFC       = 0x7c000010,
 
243
        PPC_MULHDU      = 0x7c000000,
 
244
        PPC_ADDC        = 0x7c000014,
 
245
        PPC_MULHWU      = 0x7c000000,
 
246
        PPC_MFCR        = 0x7c000000,
 
247
        PPC_LWAR        = 0x7c000000,
 
248
        PPC_LDX         = 0x7c000000,
 
249
        PPC_LWZX        = 0x7c00002e,
 
250
        PPC_SLW         = 0x7c000030,
 
251
        PPC_CNTLZW      = 0x7c000000,
 
252
        PPC_SLD         = 0x7c000000,
 
253
        PPC_AND         = 0x7c000038,
 
254
        PPC_CMPL        = 0x7c000040,
 
255
        PPC_SUBF        = 0x7c000050,
 
256
        PPC_LDUX        = 0x7c000000,
 
257
//------------
 
258
        PPC_DCBST       = 0x7c000000,
 
259
        PPC_LWZUX       = 0x7c00006c,
 
260
        PPC_CNTLZD      = 0x7c000000,
 
261
        PPC_ANDC        = 0x7c000000,
 
262
        PPC_TD          = 0x7c000000,
 
263
        PPC_MULHD       = 0x7c000000,
 
264
        PPC_MULHW       = 0x7c000000,
 
265
        PPC_MTSRD       = 0x7c000000,
 
266
        PPC_MFMSR       = 0x7c000000,
 
267
        PPC_LDARX       = 0x7c000000,
 
268
        PPC_DCBF        = 0x7c000000,
 
269
        PPC_LBZX        = 0x7c0000ae,
 
270
        PPC_NEG         = 0x7c000000,
 
271
        PPC_MTSRDIN     = 0x7c000000,
 
272
        PPC_LBZUX       = 0x7c000000,
 
273
        PPC_NOR         = 0x7c0000f8,
 
274
        PPC_SUBFE       = 0x7c000000,
 
275
        PPC_ADDE        = 0x7c000000,
 
276
        PPC_MTCRF       = 0x7c000000,
 
277
        PPC_MTMSR       = 0x7c000000,
 
278
        PPC_STDX        = 0x7c000000,
 
279
        PPC_STWCX_      = 0x7c000000,
 
280
        PPC_STWX        = 0x7c00012e,
 
281
        PPC_MTMSRD      = 0x7c000000,
 
282
        PPC_STDUX       = 0x7c000000,
 
283
        PPC_STWUX       = 0x7c00016e,
 
284
        PPC_SUBFZE      = 0x7c000000,
 
285
        PPC_ADDZE       = 0x7c000000,
 
286
        PPC_MTSR        = 0x7c000000,
 
287
        PPC_STDCX_      = 0x7c000000,
 
288
        PPC_STBX        = 0x7c0001ae,
 
289
        PPC_SUBFME      = 0x7c000000,
 
290
        PPC_MULLD       = 0x7c000000,
 
291
//------------
 
292
        PPC_ADDME       = 0x7c000000,
 
293
        PPC_MULLW       = 0x7c0001d6,
 
294
        PPC_MTSRIN      = 0x7c000000,
 
295
        PPC_DCBTST      = 0x7c000000,
 
296
        PPC_STBUX       = 0x7c000000,
 
297
        PPC_ADD         = 0x7c000214,
 
298
        PPC_DCBT        = 0x7c000000,
 
299
        PPC_LHZX        = 0x7c00022e,
 
300
        PPC_EQV         = 0x7c000000,
 
301
        PPC_TLBIE       = 0x7c000000,
 
302
        PPC_ECIWX       = 0x7c000000,
 
303
        PPC_LHZUX       = 0x7c000000,
 
304
        PPC_XOR         = 0x7c000278,
 
305
        PPC_MFSPR       = 0x7c0002a6,
 
306
        PPC_LWAX        = 0x7c000000,
 
307
        PPC_LHAX        = 0x7c000000,
 
308
        PPC_TLBIA       = 0x7c000000,
 
309
        PPC_MFTB        = 0x7c000000,
 
310
        PPC_LWAUX       = 0x7c000000,
 
311
        PPC_LHAUX       = 0x7c000000,
 
312
        PPC_STHX        = 0x7c00032e,
 
313
        PPC_ORC         = 0x7c000338,
 
314
        PPC_SRADI       = 0x7c000000,
 
315
        PPC_SLBIE       = 0x7c000000,
 
316
        PPC_ECOWX       = 0x7c000000,
 
317
        PPC_STHUX       = 0x7c000000,
 
318
        PPC_OR          = 0x7c000378,
 
319
        PPC_DIVDU       = 0x7c000000,
 
320
        PPC_DIVWU       = 0x7c000396,
 
321
        PPC_MTSPR       = 0x7c0003a6,
 
322
        PPC_DCBI        = 0x7c000000,
 
323
        PPC_NAND        = 0x7c000000,
 
324
        PPC_DIVD        = 0x7c000000,
 
325
//------------
 
326
        PPC_DIVW        = 0x7c0003d6,
 
327
        PPC_SLBIA       = 0x7c000000,
 
328
        PPC_MCRXR       = 0x7c000000,
 
329
        PPC_LSWX        = 0x7c000000,
 
330
        PPC_LWBRX       = 0x7c000000,
 
331
        PPC_LFSX        = 0x7c00042e,
 
332
        PPC_SRW         = 0x7c000430,
 
333
        PPC_SRD         = 0x7c000000,
 
334
        PPC_TLBSYNC     = 0x7c000000,
 
335
        PPC_LFSUX       = 0x7c000000,
 
336
        PPC_MFSR        = 0x7c000000,
 
337
        PPC_LSWI        = 0x7c000000,
 
338
        PPC_SYNC        = 0x7c000000,
 
339
        PPC_LFDX        = 0x7c000000,
 
340
        PPC_LFDUX       = 0x7c000000,
 
341
        PPC_MFSRIN      = 0x7c000000,
 
342
        PPC_STSWX       = 0x7c000000,
 
343
        PPC_STWBRX      = 0x7c000000,
 
344
        PPC_STFSX       = 0x7c00052e,
 
345
        PPC_STFSUX      = 0x7c000000,
 
346
        PPC_STSWI       = 0x7c000000,
 
347
        PPC_STFDX       = 0x7c000000,
 
348
        PPC_DCBA        = 0x7c000000,
 
349
        PPC_STFDUX      = 0x7c000000,
 
350
        PPC_LHBRX       = 0x7c000000,
 
351
        PPC_SRAW        = 0x7c000630,
 
352
        PPC_SRAD        = 0x7c000000,
 
353
        PPC_SRAWI       = 0x7c000000,
 
354
        PPC_EIEIO       = 0x7c000000,
 
355
        PPC_STHBRX      = 0x7c000000,
 
356
        PPC_EXTSH       = 0x7c000734,
 
357
        PPC_EXTSB       = 0x7c000774,
 
358
        PPC_ICBI        = 0x7c000000,
 
359
//------------
 
360
        PPC_STFIWX      = 0x7c0007ae,
 
361
        PPC_EXTSW       = 0x7c000000,
 
362
        PPC_DCBZ        = 0x7c000000,
 
363
        PPC_LWZ         = 0x80000000,
 
364
        PPC_LWZU        = 0x84000000,
 
365
        PPC_LBZ         = 0x88000000,
 
366
        PPC_LBZU        = 0x8c000000,
 
367
        PPC_STW         = 0x90000000,
 
368
        PPC_STWU        = 0x94000000,
 
369
        PPC_STB         = 0x98000000,
 
370
        PPC_STBU        = 0x9c000000,
 
371
        PPC_LHZ         = 0xa0000000,
 
372
        PPC_LHZU        = 0xa4000000,
 
373
        PPC_LHA         = 0xa8000000,
 
374
        PPC_LHAU        = 0xac000000,
 
375
        PPC_STH         = 0xb0000000,
 
376
        PPC_STHU        = 0xb4000000,
 
377
        PPC_LMW         = 0xb8000000,
 
378
        PPC_STMW        = 0xbc000000,
 
379
        PPC_LFS         = 0xc0000000,
 
380
        PPC_LFSU        = 0xc4000000,
 
381
        PPC_LFD         = 0xc8000000,
 
382
        PPC_LFDU        = 0xcc000000,
 
383
        PPC_STFS        = 0xd0000000,
 
384
        PPC_STFSU       = 0xd4000000,
 
385
        PPC_STFD        = 0xd8000000,
 
386
        PPC_STFDU       = 0xdc000000,
 
387
        PPC_LD          = 0xe8000000,
 
388
        PPC_LDU         = 0xe8000001,
 
389
        PPC_LWA         = 0xe8000002,
 
390
        PPC_FDIVS       = 0xec000024,
 
391
        PPC_FSUBS       = 0xec000028,
 
392
        PPC_FADDS       = 0xec00002a,
 
393
//------------
 
394
        PPC_FSQRTS      = 0xec000000,
 
395
        PPC_FRES        = 0xec000000,
 
396
        PPC_FMULS       = 0xec000032,
 
397
        PPC_FMSUBS      = 0xec000000,
 
398
        PPC_FMADDS      = 0xec000000,
 
399
        PPC_FNMSUBS     = 0xec000000,
 
400
        PPC_FNMADDS     = 0xec000000,
 
401
        PPC_STD         = 0xf8000000,
 
402
        PPC_STDU        = 0xf8000001,
 
403
        PPC_FCMPU       = 0xfc000000,
 
404
        PPC_FRSP        = 0xfc000018,
 
405
        PPC_FCTIW       = 0xfc000000,
 
406
        PPC_FCTIWZ      = 0xfc00001e,
 
407
        PPC_FDIV        = 0xfc000000,
 
408
        PPC_FSUB        = 0xfc000028,
 
409
        PPC_FADD        = 0xfc000000,
 
410
        PPC_FSQRT       = 0xfc000000,
 
411
        PPC_FSEL        = 0xfc000000,
 
412
        PPC_FMUL        = 0xfc000000,
 
413
        PPC_FRSQRTE     = 0xfc000000,
 
414
        PPC_FMSUB       = 0xfc000000,
 
415
        PPC_FMADD       = 0xfc000000,
 
416
        PPC_FNMSUB      = 0xfc000000,
 
417
        PPC_FNMADD      = 0xfc000000,
 
418
        PPC_FCMPO       = 0xfc000000,
 
419
        PPC_MTFSB1      = 0xfc000000,
 
420
        PPC_FNEG        = 0xfc000050,
 
421
        PPC_MCRFS       = 0xfc000000,
 
422
        PPC_MTFSB0      = 0xfc000000,
 
423
        PPC_FMR         = 0xfc000000,
 
424
        PPC_MTFSFI      = 0xfc000000,
 
425
        PPC_FNABS       = 0xfc000000,
 
426
        PPC_FABS        = 0xfc000000,
 
427
//------------
 
428
        PPC_MFFS        = 0xfc000000,
 
429
        PPC_MTFSF       = 0xfc000000,
 
430
        PPC_FCTID       = 0xfc000000,
 
431
        PPC_FCTIDZ      = 0xfc000000,
 
432
        PPC_FCFID       = 0xfc000000
 
433
        
 
434
} ppcOpcodes_t;
 
435
 
 
436
 
 
437
// the newly generated code
 
438
static  unsigned        *buf;
 
439
static  int             compiledOfs;    // in dwords
 
440
static int pass;
 
441
 
 
442
// fromt the original bytecode
 
443
static  byte    *code;
 
444
static  int             pc;
 
445
 
 
446
void AsmCall( void );
 
447
 
 
448
double  itofConvert[2];
 
449
 
 
450
static int      Constant4( void ) {
 
451
        int             v;
 
452
 
 
453
        v = code[pc] | (code[pc+1]<<8) | (code[pc+2]<<16) | (code[pc+3]<<24);
 
454
        pc += 4;
 
455
        return v;
 
456
}
 
457
 
 
458
static int      Constant1( void ) {
 
459
        int             v;
 
460
 
 
461
        v = code[pc];
 
462
        pc += 1;
 
463
        return v;
 
464
}
 
465
 
 
466
static void Emit4( char *opname, int i ) {
 
467
        #if DEBUG_VM
 
468
        if(pass == 1)
 
469
                printf("\t\t\t%p %s\t%08lx\n",&buf[compiledOfs],opname,i&0x3ffffff);
 
470
        #endif
 
471
        buf[ compiledOfs ] = i;
 
472
        compiledOfs++;
 
473
}
 
474
 
 
475
static void Inst( char *opname, int opcode, int destReg, int aReg, int bReg ) {
 
476
    unsigned            r;
 
477
 
 
478
        #if DEBUG_VM
 
479
        if(pass == 1)
 
480
                printf("\t\t\t%p %s\tr%d,r%d,r%d\n",&buf[compiledOfs],opname,destReg,aReg,bReg);
 
481
        #endif
 
482
    r = opcode | ( destReg << 21 ) | ( aReg << 16 ) | ( bReg << 11 ) ;
 
483
    buf[ compiledOfs ] = r;
 
484
    compiledOfs++;
 
485
}
 
486
 
 
487
static void Inst4( char *opname, int opcode, int destReg, int aReg, int bReg, int cReg ) {
 
488
    unsigned            r;
 
489
 
 
490
        #if DEBUG_VM
 
491
        if(pass == 1)
 
492
                printf("\t\t\t%p %s\tr%d,r%d,r%d,r%d\n",&buf[compiledOfs],opname,destReg,aReg,bReg,cReg);
 
493
        #endif
 
494
    r = opcode | ( destReg << 21 ) | ( aReg << 16 ) | ( bReg << 11 ) | ( cReg << 6 );
 
495
    buf[ compiledOfs ] = r;
 
496
    compiledOfs++;
 
497
}
 
498
 
 
499
static void InstImm( char *opname, int opcode, int destReg, int aReg, int immediate ) {
 
500
        unsigned                r;
 
501
        
 
502
        if ( immediate > 32767 || immediate < -32768 ) {
 
503
            Com_Error( ERR_FATAL, "VM_Compile: immediate value %i out of range, opcode %x,%d,%d", immediate, opcode, destReg, aReg );
 
504
        }
 
505
        #if DEBUG_VM
 
506
        if(pass == 1)
 
507
                printf("\t\t\t%p %s\tr%d,r%d,0x%x\n",&buf[compiledOfs],opname,destReg,aReg,immediate);
 
508
        #endif
 
509
        r = opcode | ( destReg << 21 ) | ( aReg << 16 ) | ( immediate & 0xffff );
 
510
        buf[ compiledOfs ] = r;
 
511
        compiledOfs++;
 
512
}
 
513
 
 
514
static void InstImmU( char *opname, int opcode, int destReg, int aReg, int immediate ) {
 
515
        unsigned                r;
 
516
        
 
517
        if ( immediate > 0xffff || immediate < 0 ) {
 
518
                Com_Error( ERR_FATAL, "VM_Compile: immediate value %i out of range", immediate );
 
519
        }
 
520
        #if DEBUG_VM
 
521
        if(pass == 1)
 
522
                printf("\t\t\t%p %s\tr%d,r%d,0x%x\n",&buf[compiledOfs],opname,destReg,aReg,immediate);
 
523
        #endif
 
524
    r = opcode | ( destReg << 21 ) | ( aReg << 16 ) | ( immediate & 0xffff );
 
525
        buf[ compiledOfs ] = r;
 
526
        compiledOfs++;
 
527
}
 
528
 
 
529
static int pop0, pop1, oc0, oc1;
 
530
static vm_t *tvm;
 
531
static int instruction;
 
532
static byte *jused;
 
533
 
 
534
static void spillOpStack(int depth)
 
535
{
 
536
        // Store out each register on the operand stack to it's correct location.
 
537
        int i;
 
538
        
 
539
        for(i = 0; i < depth; i++)
 
540
        {
 
541
                assert(opStackRegType[i]);
 
542
                assert(opStackRegType[i] == 1);
 
543
                switch(opStackRegType[i])
 
544
                {
 
545
                        case 1: // Integer register
 
546
                                InstImm( "stw", PPC_STW, opStackIntRegisters[i], R_OPSTACK, i*4+4);
 
547
                                break;
 
548
                        case 2: // Float register
 
549
                                InstImm( "stfs", PPC_STFS, opStackFloatRegisters[i], R_OPSTACK, i*4+4);
 
550
                                break;
 
551
                }
 
552
                opStackRegType[i] = 0;
 
553
        }
 
554
}
 
555
 
 
556
static void loadOpStack(int depth)
 
557
{
 
558
        // Back off operand stack pointer and reload all operands.
 
559
//      InstImm( "addi", PPC_ADDI, R_OPSTACK, R_OPSTACK, -(depth)*4 );
 
560
 
 
561
        int i;
 
562
        
 
563
        for(i = 0; i < depth; i++)
 
564
        {
 
565
                assert(opStackRegType[i] == 0);
 
566
                // For now we're stuck reloading everything as an integer.
 
567
                opStackLoadInstructionAddr[i] = &buf[compiledOfs];
 
568
                InstImm( "lwz", PPC_LWZ, opStackIntRegisters[i], R_OPSTACK, i*4+4);
 
569
                opStackRegType[i] = 1;
 
570
        }       
 
571
}
 
572
 
 
573
static void makeFloat(int depth)
 
574
{
 
575
        //assert(opStackRegType[depth] == 1);
 
576
        if(opStackRegType[depth] == 1)
 
577
        {
 
578
                unsigned instruction;
 
579
                unsigned destReg, aReg, bReg, imm;
 
580
                
 
581
                if(opStackLoadInstructionAddr[depth])
 
582
                {
 
583
                        // Repatch load instruction to use LFS instead of LWZ
 
584
                        instruction = *opStackLoadInstructionAddr[depth];
 
585
                        // Figure out if it's LWZ or LWZX
 
586
                        if((instruction & 0xfc000000) == PPC_LWZ)
 
587
                        {
 
588
                                //printf("patching LWZ at %p to LFS at depth %ld\n",opStackLoadInstructionAddr[depth],depth);
 
589
                                //printf("old instruction: %08lx\n",instruction);
 
590
                                // Extract registers
 
591
                                destReg = (instruction >> 21) & 31;
 
592
                                aReg    = (instruction >> 16) & 31;
 
593
                                imm     = instruction & 0xffff;
 
594
                                
 
595
                                // Calculate correct FP register to use.
 
596
                                // THIS ASSUMES REGISTER USAGE FOR THE STACK IS n, n+1, n+2, etc!
 
597
                                //printf("old dest: %ld\n",destReg);
 
598
                                destReg = (destReg - opStackIntRegisters[0]) + opStackFloatRegisters[0];
 
599
                                instruction = PPC_LFS | ( destReg << 21 ) | ( aReg << 16 ) | imm ;                      
 
600
                                //printf("new dest: %ld\n",destReg);
 
601
                                //printf("new instruction: %08lx\n",instruction);
 
602
                        }
 
603
                        else
 
604
                        {
 
605
                                //printf("patching LWZX at %p to LFSX at depth %ld\n",opStackLoadInstructionAddr[depth],depth);
 
606
                                //printf("old instruction: %08lx\n",instruction);
 
607
                                // Extract registers
 
608
                                destReg = (instruction >> 21) & 31;
 
609
                                aReg    = (instruction >> 16) & 31;
 
610
                                bReg    = (instruction >> 11) & 31;
 
611
                                // Calculate correct FP register to use.
 
612
                                // THIS ASSUMES REGISTER USAGE FOR THE STACK IS n, n+1, n+2, etc!
 
613
                                //printf("old dest: %ld\n",destReg);
 
614
                                destReg = (destReg - opStackIntRegisters[0]) + opStackFloatRegisters[0];
 
615
                                instruction = PPC_LFSX | ( destReg << 21 ) | ( aReg << 16 ) | ( bReg << 11 ) ;                  
 
616
                                //printf("new dest: %ld\n",destReg);
 
617
                                //printf("new instruction: %08lx\n",instruction);
 
618
                        }
 
619
                        *opStackLoadInstructionAddr[depth] = instruction;
 
620
                        opStackLoadInstructionAddr[depth] = 0;
 
621
                }
 
622
                else
 
623
                {       
 
624
                        //printf("doing float constant load at %p for depth %ld\n",&buf[compiledOfs],depth);
 
625
                        // It was likely loaded as a constant so we have to save/load it.  A more
 
626
                        // interesting implementation might be to generate code to do a "PC relative"
 
627
                        // load from the VM code region.
 
628
                        InstImm( "stw", PPC_STW, opStackIntRegisters[depth], R_OPSTACK, depth*4+4);
 
629
                        // For XXX make sure we force enough NOPs to get the load into
 
630
                        // another dispatch group to avoid pipeline flush.
 
631
                        Inst( "ori", PPC_ORI,  0, 0, 0 );
 
632
                        Inst( "ori", PPC_ORI,  0, 0, 0 );
 
633
                        Inst( "ori", PPC_ORI,  0, 0, 0 );
 
634
                        Inst( "ori", PPC_ORI,  0, 0, 0 );
 
635
                        InstImm( "lfs", PPC_LFS, opStackFloatRegisters[depth], R_OPSTACK, depth*4+4);
 
636
                }
 
637
                opStackRegType[depth] = 2;
 
638
        }
 
639
}
 
640
 
 
641
// TJW: Unused
 
642
#if 0
 
643
static void fltop() {
 
644
    if (rtopped == qfalse) {
 
645
        InstImm( PPC_LFS, R_TOP, R_OPSTACK, 0 );                // get value from opstack
 
646
    }
 
647
}
 
648
#endif
 
649
 
 
650
#if 0
 
651
static void fltopandsecond() {
 
652
        InstImm( PPC_LFS, R_TOP, R_OPSTACK, 0 );                // get value from opstack
 
653
        InstImm( PPC_LFS, R_SECOND, R_OPSTACK, -4 );    // get value from opstack
 
654
        InstImm( PPC_ADDI, R_OPSTACK, R_OPSTACK, -8 );
 
655
    rtopped = qfalse;
 
656
        return;
 
657
}
 
658
#endif
 
659
 
 
660
#define assertInteger(depth)    assert(opStackRegType[depth] == 1)
 
661
 
 
662
/*
 
663
=================
 
664
VM_Compile
 
665
=================
 
666
*/
 
667
void VM_Compile( vm_t *vm, vmHeader_t *header ) {
 
668
        int             op;
 
669
        int             maxLength;
 
670
        int             v;
 
671
        int             i;
 
672
        int             opStackDepth;
 
673
        
 
674
        int             mainFunction;
 
675
        
 
676
        // set up the into-to-float variables
 
677
        ((int *)itofConvert)[0] = 0x43300000;
 
678
        ((int *)itofConvert)[1] = 0x80000000;
 
679
        ((int *)itofConvert)[2] = 0x43300000;
 
680
 
 
681
        // allocate a very large temp buffer, we will shrink it later
 
682
        maxLength = header->codeLength * 8;
 
683
        buf = Z_Malloc( maxLength );
 
684
        jused = Z_Malloc(header->instructionCount + 2);
 
685
        Com_Memset(jused, 0, header->instructionCount+2);
 
686
        
 
687
    // compile everything twice, so the second pass will have valid instruction
 
688
    // pointers for branches
 
689
    for ( pass = -1 ; pass < 2 ; pass++ ) {
 
690
 
 
691
        // translate all instructions
 
692
        pc = 0;
 
693
        mainFunction = 0;
 
694
        opStackDepth = 0;
 
695
        
 
696
        pop0 = 343545;
 
697
        pop1 = 2443545;
 
698
        oc0 = -2343535;
 
699
        oc1 = 24353454;
 
700
        tvm = vm;
 
701
        code = (byte *)header + header->codeOffset;
 
702
        compiledOfs = 0;
 
703
#ifndef __GNUC__
 
704
                // metrowerks seems to require this header in front of functions
 
705
                Emit4( (int)(buf+2) );
 
706
                Emit4( 0 );
 
707
#endif
 
708
 
 
709
        for ( instruction = 0 ; instruction < header->instructionCount ; instruction++ ) {
 
710
            if ( compiledOfs*4 > maxLength - 16 ) {
 
711
                Com_Error( ERR_DROP, "VM_Compile: maxLength exceeded" );
 
712
            }
 
713
 
 
714
            op = code[ pc ];
 
715
            if ( !pass ) {
 
716
                vm->instructionPointers[ instruction ] = compiledOfs * 4;
 
717
            }
 
718
            pc++;
 
719
            switch ( op ) {
 
720
            case 0:
 
721
                break;
 
722
            case OP_BREAK:
 
723
                #if DEBUG_VM
 
724
                if(pass == 1)
 
725
                        printf("%08lx BREAK\n",instruction);
 
726
                #endif
 
727
                InstImmU( "addi", PPC_ADDI, R_TOP, 0, 0 );
 
728
                InstImm( "lwz", PPC_LWZ, R_TOP, R_TOP, 0 );                     // *(int *)0 to crash to debugger
 
729
                break;
 
730
            case OP_ENTER:
 
731
                opStackDepth = 0;
 
732
                v = Constant4();
 
733
                #if DEBUG_VM
 
734
                if(pass == 1)
 
735
                        printf("%08x ENTER\t%04x\n",instruction,v);
 
736
                #endif
 
737
                opStackRegType[opStackDepth] = 0;
 
738
                mainFunction++;
 
739
                if(mainFunction == 1)
 
740
                {
 
741
                        // Main VM entry point is the first thing we compile, so save off operand stack
 
742
                        // registers here.  This avoids issues with trying to trick the native compiler
 
743
                        // into doing it, and properly matches the PowerPC ABI
 
744
                        InstImm( "addi", PPC_ADDI, R_REAL_STACK, R_REAL_STACK, -OP_STACK_MAX_DEPTH*4 ); // sub R_STACK, R_STACK, imm
 
745
                        for(i = 0; i < OP_STACK_MAX_DEPTH; i++)
 
746
                                InstImm( "stw", PPC_STW, opStackIntRegisters[i], R_REAL_STACK, i*4);
 
747
                }
 
748
                InstImm( "addi", PPC_ADDI, R_STACK, R_STACK, -v );      // sub R_STACK, R_STACK, imm
 
749
                break;
 
750
            case OP_CONST:
 
751
                v = Constant4();
 
752
                #if DEBUG_VM
 
753
                if(pass == 1)
 
754
                        printf("%08x CONST\t%08x\n",instruction,v);
 
755
                #endif
 
756
                opStackLoadInstructionAddr[opStackDepth] = 0;
 
757
                if ( v < 32768 && v >= -32768 ) {
 
758
                    InstImmU( "addi", PPC_ADDI, opStackIntRegisters[opStackDepth], 0, v & 0xffff );
 
759
                } else {
 
760
                    InstImmU( "addis", PPC_ADDIS, opStackIntRegisters[opStackDepth], 0, (v >> 16)&0xffff );
 
761
                    if ( v & 0xffff ) {
 
762
                        InstImmU( "ori", PPC_ORI, opStackIntRegisters[opStackDepth], opStackIntRegisters[opStackDepth], v & 0xffff );
 
763
                    }
 
764
                }
 
765
                opStackRegType[opStackDepth] = 1;
 
766
                opStackDepth += 1;
 
767
                if (code[pc] == OP_JUMP) {
 
768
                    jused[v] = 1;
 
769
                }
 
770
                break;
 
771
            case OP_LOCAL:
 
772
                oc1 = Constant4();
 
773
                #if DEBUG_VM
 
774
                if(pass == 1)
 
775
                        printf("%08x LOCAL\t%08x\n",instruction,oc1);
 
776
                #endif
 
777
                if (code[pc] == OP_LOAD4 || code[pc] == OP_LOAD2 || code[pc] == OP_LOAD1) {
 
778
                    oc1 &= vm->dataMask;
 
779
                }
 
780
                InstImm( "addi", PPC_ADDI, opStackIntRegisters[opStackDepth], R_STACK, oc1 );
 
781
                opStackRegType[opStackDepth] = 1;
 
782
                opStackLoadInstructionAddr[opStackDepth] = 0;
 
783
                opStackDepth += 1;
 
784
                break;
 
785
            case OP_ARG:
 
786
                v = Constant1();
 
787
                #if DEBUG_VM
 
788
                if(pass == 1)
 
789
                        printf("%08x ARG \t%08x\n",instruction,v);
 
790
                #endif
 
791
                InstImm( "addi", PPC_ADDI, R_EA, R_STACK, v );  // location to put it
 
792
                if(opStackRegType[opStackDepth-1] == 1)
 
793
                        Inst( "stwx", PPC_STWX, opStackIntRegisters[opStackDepth-1], R_EA, R_MEMBASE );
 
794
                else
 
795
                        Inst( "stfsx", PPC_STFSX, opStackFloatRegisters[opStackDepth-1], R_EA, R_MEMBASE );
 
796
                opStackRegType[opStackDepth-1] = 0;
 
797
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
798
                opStackDepth -= 1;
 
799
                
 
800
                break;
 
801
            case OP_CALL:
 
802
                #if DEBUG_VM
 
803
                if(pass == 1)
 
804
                        printf("%08x CALL\n",instruction);
 
805
                #endif
 
806
                assertInteger(opStackDepth-1);
 
807
                assert(opStackDepth > 0);
 
808
                Inst( "mflr", PPC_MFSPR, R_SECOND, 8, 0 );                      // move from link register
 
809
                InstImm( "stwu", PPC_STWU, R_SECOND, R_REAL_STACK, -16 );       // save off the old return address
 
810
 
 
811
                // Spill operand stack registers.
 
812
                spillOpStack(opStackDepth);
 
813
                
 
814
                // We need to leave R_OPSTACK pointing to the top entry on the stack, which is the call address.
 
815
                // It will be consumed (and R4 decremented) by the AsmCall code.
 
816
                InstImm( "addi", PPC_ADDI, R_OPSTACK, R_OPSTACK, opStackDepth*4);
 
817
 
 
818
                Inst( "mtctr", PPC_MTSPR, R_ASMCALL, 9, 0 );                    // move to count register
 
819
                Inst( "bctrl", PPC_BCCTR | 1, 20, 0, 0 );                       // jump and link to the count register
 
820
 
 
821
                // R4 now points to the top of the operand stack, which has the return value in it.  We want to
 
822
                // back off the pointer to point to the base of our local operand stack and then reload the stack.
 
823
                
 
824
                InstImm("addi", PPC_ADDI, R_OPSTACK, R_OPSTACK, -opStackDepth*4);
 
825
                
 
826
                // Reload operand stack.
 
827
                loadOpStack(opStackDepth);
 
828
 
 
829
                InstImm( "lwz", PPC_LWZ, R_SECOND, R_REAL_STACK, 0 );           // fetch the old return address
 
830
                InstImm( "addi", PPC_ADDI, R_REAL_STACK, R_REAL_STACK, 16 );
 
831
                Inst( "mtlr", PPC_MTSPR, R_SECOND, 8, 0 );                      // move to link register
 
832
                break;
 
833
            case OP_PUSH:
 
834
                #if DEBUG_VM
 
835
                if(pass == 1)
 
836
                        printf("%08x PUSH\n",instruction);
 
837
                #endif
 
838
                opStackRegType[opStackDepth] = 1;       // Garbage int value.
 
839
                opStackDepth += 1;
 
840
                break;
 
841
            case OP_POP:
 
842
                #if DEBUG_VM
 
843
                if(pass == 1)
 
844
                        printf("%08x POP\n",instruction);
 
845
                #endif
 
846
                opStackDepth -= 1;
 
847
                opStackRegType[opStackDepth] = 0;       // ??
 
848
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
849
                break;
 
850
            case OP_LEAVE:
 
851
                #if DEBUG_VM
 
852
                if(pass == 1)
 
853
                        printf("%08x LEAVE\n",instruction);
 
854
                #endif
 
855
                assert(opStackDepth == 1);
 
856
                assert(opStackRegType[0] != 0);
 
857
                // Save return value onto top of op stack.  We also have to increment R_OPSTACK
 
858
                switch(opStackRegType[0])
 
859
                {
 
860
                        case 1: // Integer register
 
861
                                InstImm( "stw", PPC_STWU, opStackIntRegisters[0], R_OPSTACK, 4);
 
862
                                break;
 
863
                        case 2: // Float register
 
864
                                InstImm( "stfs", PPC_STFSU, opStackFloatRegisters[0], R_OPSTACK, 4);
 
865
                                break;
 
866
                }
 
867
                InstImm( "addi", PPC_ADDI, R_STACK, R_STACK, Constant4() );     // add R_STACK, R_STACK, imm
 
868
                if(mainFunction == 1)
 
869
                {
 
870
                        for(i = 0; i < OP_STACK_MAX_DEPTH; i++)
 
871
                                InstImm( "lwz", PPC_LWZ,  opStackIntRegisters[i], R_REAL_STACK, i*4);
 
872
                        InstImm( "addi", PPC_ADDI, R_REAL_STACK, R_REAL_STACK, OP_STACK_MAX_DEPTH*4 );  
 
873
                }
 
874
                opStackDepth--;
 
875
                opStackRegType[opStackDepth] = 0;
 
876
                opStackLoadInstructionAddr[opStackDepth] = 0;
 
877
                Inst( "blr", PPC_BCLR, 20, 0, 0 );                                                      // branch unconditionally to link register
 
878
                break;
 
879
            case OP_LOAD4:
 
880
                #if DEBUG_VM
 
881
                if(pass == 1)
 
882
                        printf("%08x LOAD4\n",instruction);
 
883
                #endif
 
884
                // We should try to figure out whether to use LWZX or LFSX based
 
885
                // on some kind of code analysis after subsequent passes.  I think what
 
886
                // we could do is store the compiled load instruction address along with
 
887
                // the register type.  When we hit the first mismatched operator, we go back
 
888
                // and patch the load.  Since LCC's operand stack should be at 0 depth by the
 
889
                // time we hit a branch, this should work fairly well.  FIXME FIXME FIXME.
 
890
                assertInteger(opStackDepth-1);
 
891
                opStackLoadInstructionAddr[opStackDepth-1] = &buf[ compiledOfs ];
 
892
                Inst( "lwzx", PPC_LWZX, opStackIntRegisters[opStackDepth-1], opStackIntRegisters[opStackDepth-1], R_MEMBASE );// load from memory base
 
893
                opStackRegType[opStackDepth-1] = 1;
 
894
                break;
 
895
            case OP_LOAD2:
 
896
                #if DEBUG_VM
 
897
                if(pass == 1)
 
898
                        printf("%08x LOAD2\n",instruction);
 
899
                #endif
 
900
                assertInteger(opStackDepth-1);
 
901
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
902
                Inst( "lhzx", PPC_LHZX, opStackIntRegisters[opStackDepth-1], opStackIntRegisters[opStackDepth-1], R_MEMBASE );// load from memory base
 
903
                opStackRegType[opStackDepth-1] = 1;
 
904
                break;
 
905
            case OP_LOAD1:
 
906
                #if DEBUG_VM
 
907
                if(pass == 1)
 
908
                        printf("%08x LOAD1\n",instruction);
 
909
                #endif
 
910
                assertInteger(opStackDepth-1);
 
911
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
912
                Inst( "lbzx", PPC_LBZX, opStackIntRegisters[opStackDepth-1], opStackIntRegisters[opStackDepth-1], R_MEMBASE );// load from memory base
 
913
                opStackRegType[opStackDepth-1] = 1;
 
914
                break;
 
915
            case OP_STORE4:
 
916
                #if DEBUG_VM
 
917
                if(pass == 1)
 
918
                        printf("%08x STORE4\n",instruction);
 
919
                #endif
 
920
                assertInteger(opStackDepth-2);
 
921
                if(opStackRegType[opStackDepth-1] == 1)
 
922
                        Inst( "stwx", PPC_STWX, opStackIntRegisters[opStackDepth-1], 
 
923
                                        opStackIntRegisters[opStackDepth-2], R_MEMBASE );               // store from memory base
 
924
                else
 
925
                        Inst( "stfsx", PPC_STFSX, opStackFloatRegisters[opStackDepth-1], 
 
926
                                        opStackIntRegisters[opStackDepth-2], R_MEMBASE );               // store from memory base
 
927
                opStackRegType[opStackDepth-1] = 0;
 
928
                opStackRegType[opStackDepth-2] = 0;
 
929
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
930
                opStackLoadInstructionAddr[opStackDepth-2] = 0;
 
931
                opStackDepth -= 2;
 
932
                break;
 
933
            case OP_STORE2:
 
934
                #if DEBUG_VM
 
935
                if(pass == 1)
 
936
                        printf("%08x STORE2\n",instruction);
 
937
                #endif
 
938
                assertInteger(opStackDepth-1);
 
939
                assertInteger(opStackDepth-2);
 
940
                Inst( "sthx", PPC_STHX, opStackIntRegisters[opStackDepth-1],
 
941
                                opStackIntRegisters[opStackDepth-2], R_MEMBASE );               // store from memory base
 
942
                opStackRegType[opStackDepth-1] = 0;
 
943
                opStackRegType[opStackDepth-2] = 0;
 
944
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
945
                opStackLoadInstructionAddr[opStackDepth-2] = 0;
 
946
                opStackDepth -= 2;
 
947
                break;
 
948
            case OP_STORE1:
 
949
                #if DEBUG_VM
 
950
                if(pass == 1)
 
951
                        printf("%08x STORE1\n",instruction);
 
952
                #endif
 
953
                assertInteger(opStackDepth-1);
 
954
                assertInteger(opStackDepth-2);
 
955
                Inst( "stbx", PPC_STBX, opStackIntRegisters[opStackDepth-1],
 
956
                                opStackIntRegisters[opStackDepth-2], R_MEMBASE );               // store from memory base
 
957
                opStackRegType[opStackDepth-1] = 0;
 
958
                opStackRegType[opStackDepth-2] = 0;
 
959
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
960
                opStackLoadInstructionAddr[opStackDepth-2] = 0;
 
961
                opStackDepth -= 2;
 
962
                break;
 
963
 
 
964
            case OP_EQ:
 
965
                #if DEBUG_VM
 
966
                if(pass == 1)
 
967
                        printf("%08x EQ\n",instruction);
 
968
                #endif
 
969
                assertInteger(opStackDepth-1);
 
970
                assertInteger(opStackDepth-2);
 
971
                Inst( "cmp", PPC_CMP, 0, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-1] );
 
972
                opStackRegType[opStackDepth-1] = 0;
 
973
                opStackRegType[opStackDepth-2] = 0;
 
974
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
975
                opStackLoadInstructionAddr[opStackDepth-2] = 0;
 
976
                opStackDepth -= 2;
 
977
                i = Constant4();
 
978
                                jused[i] = 1;
 
979
                InstImm( "bc", PPC_BC, 4, 2, 8 );
 
980
                if ( pass==1 ) {
 
981
                    v = vm->instructionPointers[ i ] - (int)&buf[compiledOfs];                    
 
982
                } else {
 
983
                    v = 0;             
 
984
                }
 
985
                Emit4("b", PPC_B | (v&0x3ffffff) );
 
986
                break;
 
987
            case OP_NE:
 
988
                #if DEBUG_VM
 
989
                if(pass == 1)
 
990
                        printf("%08x NE\n",instruction);
 
991
                #endif
 
992
                assertInteger(opStackDepth-1);
 
993
                assertInteger(opStackDepth-2);
 
994
                Inst( "cmp", PPC_CMP, 0, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-1] );
 
995
                opStackRegType[opStackDepth-1] = 0;
 
996
                opStackRegType[opStackDepth-2] = 0;
 
997
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
998
                opStackLoadInstructionAddr[opStackDepth-2] = 0;
 
999
                opStackDepth -= 2;
 
1000
                i = Constant4();
 
1001
                                jused[i] = 1;
 
1002
                InstImm( "bc", PPC_BC, 12, 2, 8 );
 
1003
                if ( pass==1 ) {
 
1004
                    v = vm->instructionPointers[ i ] - (int)&buf[compiledOfs];                    
 
1005
                } else {
 
1006
                    v = 0;
 
1007
                }
 
1008
                Emit4("b", PPC_B | (unsigned int)(v&0x3ffffff) );
 
1009
//                InstImm( "bc", PPC_BC, 4, 2, v );
 
1010
 
 
1011
                break;
 
1012
            case OP_LTI:
 
1013
                #if DEBUG_VM
 
1014
                if(pass == 1)
 
1015
                printf("%08x LTI\n",instruction);
 
1016
                #endif
 
1017
                assertInteger(opStackDepth-1);
 
1018
                assertInteger(opStackDepth-2);
 
1019
                Inst( "cmp", PPC_CMP, 0, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-1] );
 
1020
                opStackRegType[opStackDepth-1] = 0;
 
1021
                opStackRegType[opStackDepth-2] = 0;
 
1022
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1023
                opStackLoadInstructionAddr[opStackDepth-2] = 0;
 
1024
                opStackDepth -= 2;
 
1025
                i = Constant4();
 
1026
                                jused[i] = 1;
 
1027
                InstImm( "bc", PPC_BC, 4, 0, 8 );
 
1028
                if ( pass==1 ) {
 
1029
                    v = vm->instructionPointers[ i ] - (int)&buf[compiledOfs];
 
1030
                } else {
 
1031
                    v = 0;
 
1032
                }
 
1033
                Emit4("b", PPC_B | (unsigned int)(v&0x3ffffff) );
 
1034
//                InstImm( "bc", PPC_BC, 12, 0, v );
 
1035
                break;
 
1036
            case OP_LEI:
 
1037
                #if DEBUG_VM
 
1038
                if(pass == 1)
 
1039
                printf("%08x LEI\n",instruction);
 
1040
                #endif
 
1041
                assertInteger(opStackDepth-1);
 
1042
                assertInteger(opStackDepth-2);
 
1043
                Inst( "cmp", PPC_CMP, 0, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-1] );
 
1044
                opStackRegType[opStackDepth-1] = 0;
 
1045
                opStackRegType[opStackDepth-2] = 0;
 
1046
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1047
                opStackLoadInstructionAddr[opStackDepth-2] = 0;
 
1048
                opStackDepth -= 2;
 
1049
                i = Constant4();
 
1050
                                jused[i] = 1;
 
1051
                InstImm( "bc", PPC_BC, 12, 1, 8 );
 
1052
                if ( pass==1 ) {
 
1053
                    v = vm->instructionPointers[ i ] - (int)&buf[compiledOfs];
 
1054
                } else {
 
1055
                    v = 0;
 
1056
                }
 
1057
                Emit4("b", PPC_B | (unsigned int)(v&0x3ffffff) );
 
1058
//                InstImm( "bc", PPC_BC, 4, 1, v );
 
1059
                break;
 
1060
            case OP_GTI:
 
1061
                #if DEBUG_VM
 
1062
                if(pass == 1)
 
1063
                printf("%08x GTI\n",instruction);
 
1064
                #endif
 
1065
                assertInteger(opStackDepth-1);
 
1066
                assertInteger(opStackDepth-2);
 
1067
                Inst( "cmp", PPC_CMP, 0, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-1] );
 
1068
                opStackRegType[opStackDepth-1] = 0;
 
1069
                opStackRegType[opStackDepth-2] = 0;
 
1070
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1071
                opStackLoadInstructionAddr[opStackDepth-2] = 0;
 
1072
                opStackDepth -= 2;
 
1073
                i = Constant4();
 
1074
                                jused[i] = 1;
 
1075
                InstImm( "bc", PPC_BC, 4, 1, 8 );
 
1076
                if ( pass==1 ) {
 
1077
                    v = vm->instructionPointers[ i ] - (int)&buf[compiledOfs];
 
1078
                } else {
 
1079
                    v = 0;
 
1080
                }
 
1081
                Emit4("b", PPC_B | (unsigned int)(v&0x3ffffff) );
 
1082
//                InstImm( "bc", PPC_BC, 12, 1, v );
 
1083
                break;
 
1084
            case OP_GEI:
 
1085
                #if DEBUG_VM
 
1086
                if(pass == 1)
 
1087
                printf("%08x GEI\n",instruction);
 
1088
                #endif
 
1089
                assertInteger(opStackDepth-1);
 
1090
                assertInteger(opStackDepth-2);
 
1091
                Inst( "cmp", PPC_CMP, 0, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-1] );
 
1092
                opStackRegType[opStackDepth-1] = 0;
 
1093
                opStackRegType[opStackDepth-2] = 0;
 
1094
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1095
                opStackLoadInstructionAddr[opStackDepth-2] = 0;
 
1096
                opStackDepth -= 2;
 
1097
                i = Constant4();
 
1098
                                jused[i] = 1;
 
1099
                InstImm( "bc", PPC_BC, 12, 0, 8 );
 
1100
                if ( pass==1 ) {
 
1101
                    v = vm->instructionPointers[ i ] - (int)&buf[compiledOfs];
 
1102
                } else {
 
1103
                    v = 0;
 
1104
                }
 
1105
                Emit4("b", PPC_B | (unsigned int)(v&0x3ffffff) );
 
1106
//                InstImm( "bc", PPC_BC, 4, 0, v );
 
1107
                break;
 
1108
            case OP_LTU:
 
1109
                #if DEBUG_VM
 
1110
                if(pass == 1)
 
1111
                printf("%08x LTU\n",instruction);
 
1112
                #endif
 
1113
                assertInteger(opStackDepth-1);
 
1114
                assertInteger(opStackDepth-2);
 
1115
                Inst( "cmpl", PPC_CMPL, 0, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-1] );
 
1116
                opStackRegType[opStackDepth-1] = 0;
 
1117
                opStackRegType[opStackDepth-2] = 0;
 
1118
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1119
                opStackLoadInstructionAddr[opStackDepth-2] = 0;
 
1120
                opStackDepth -= 2;
 
1121
                i = Constant4();
 
1122
                jused[i] = 1;
 
1123
                InstImm( "bc", PPC_BC, 4, 0, 8 );
 
1124
                if ( pass==1 ) {
 
1125
                    v = vm->instructionPointers[ i ] - (int)&buf[compiledOfs];
 
1126
                } else {
 
1127
                    v = 0;
 
1128
                }
 
1129
                Emit4("b", PPC_B | (unsigned int)(v&0x3ffffff) );
 
1130
//                InstImm( "bc", PPC_BC, 12, 0, v );
 
1131
                break;
 
1132
            case OP_LEU:
 
1133
                #if DEBUG_VM
 
1134
                if(pass == 1)
 
1135
                printf("%08x LEU\n",instruction);
 
1136
                #endif
 
1137
                assertInteger(opStackDepth-1);
 
1138
                assertInteger(opStackDepth-2);
 
1139
                Inst( "cmpl", PPC_CMPL, 0, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-1] );
 
1140
                opStackRegType[opStackDepth-1] = 0;
 
1141
                opStackRegType[opStackDepth-2] = 0;
 
1142
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1143
                opStackLoadInstructionAddr[opStackDepth-2] = 0;
 
1144
                opStackDepth -= 2;
 
1145
                i = Constant4();
 
1146
                jused[i] = 1;
 
1147
                InstImm( "bc", PPC_BC, 12, 1, 8 );
 
1148
                if ( pass==1 ) {
 
1149
                    v = vm->instructionPointers[ i ] - (int)&buf[compiledOfs];
 
1150
                } else {
 
1151
                    v = 0;
 
1152
                }
 
1153
                Emit4("b", PPC_B | (unsigned int)(v&0x3ffffff) );
 
1154
//                InstImm( "bc", PPC_BC, 4, 1, v );
 
1155
                break;
 
1156
            case OP_GTU:
 
1157
                #if DEBUG_VM
 
1158
                if(pass == 1)
 
1159
                printf("%08x GTU\n",instruction);
 
1160
                #endif
 
1161
                assertInteger(opStackDepth-1);
 
1162
                assertInteger(opStackDepth-2);
 
1163
                Inst( "cmpl", PPC_CMPL, 0, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-1] );
 
1164
                opStackRegType[opStackDepth-1] = 0;
 
1165
                opStackRegType[opStackDepth-2] = 0;
 
1166
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1167
                opStackLoadInstructionAddr[opStackDepth-2] = 0;
 
1168
                opStackDepth -= 2;
 
1169
                i = Constant4();
 
1170
                jused[i] = 1;
 
1171
                InstImm( "bc", PPC_BC, 4, 1, 8 );
 
1172
                if ( pass==1 ) {
 
1173
                    v = vm->instructionPointers[ i ] - (int)&buf[compiledOfs];
 
1174
                } else {
 
1175
                    v = 0;
 
1176
                }
 
1177
                Emit4("b", PPC_B | (unsigned int)(v&0x3ffffff) );
 
1178
//                InstImm( "bc", PPC_BC, 12, 1, v );
 
1179
                break;
 
1180
            case OP_GEU:
 
1181
                #if DEBUG_VM
 
1182
                if(pass == 1)
 
1183
                printf("%08x GEU\n",instruction);
 
1184
                #endif
 
1185
                assertInteger(opStackDepth-1);
 
1186
                assertInteger(opStackDepth-2);
 
1187
                Inst( "cmpl", PPC_CMPL, 0, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-1] );
 
1188
                opStackRegType[opStackDepth-1] = 0;
 
1189
                opStackRegType[opStackDepth-2] = 0;
 
1190
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1191
                opStackLoadInstructionAddr[opStackDepth-2] = 0;
 
1192
                opStackDepth -= 2;
 
1193
                i = Constant4();
 
1194
                jused[i] = 1;
 
1195
                InstImm( "bc", PPC_BC, 12, 0, 8 );
 
1196
                if ( pass==1 ) {
 
1197
                    v = vm->instructionPointers[ i ] - (int)&buf[compiledOfs];
 
1198
                } else {
 
1199
                    v = 0;
 
1200
                }
 
1201
                Emit4("b", PPC_B | (unsigned int)(v&0x3ffffff) );
 
1202
//                InstImm( "bc", PPC_BC, 4, 0, v );
 
1203
                break;
 
1204
                
 
1205
            case OP_EQF:
 
1206
                #if DEBUG_VM
 
1207
                if(pass == 1)
 
1208
                printf("%08x EQF\n",instruction);
 
1209
                #endif
 
1210
                makeFloat(opStackDepth-1);
 
1211
                makeFloat(opStackDepth-2);
 
1212
                Inst( "fcmpu", PPC_FCMPU, 0, opStackFloatRegisters[opStackDepth-2], opStackFloatRegisters[opStackDepth-1] );
 
1213
                opStackRegType[opStackDepth-1] = 0;
 
1214
                opStackRegType[opStackDepth-2] = 0;
 
1215
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1216
                opStackLoadInstructionAddr[opStackDepth-2] = 0;
 
1217
                opStackDepth -= 2;
 
1218
                i = Constant4();
 
1219
                jused[i] = 1;
 
1220
                InstImm( "bc", PPC_BC, 4, 2, 8 );
 
1221
                if ( pass==1 ) {
 
1222
                    v = vm->instructionPointers[ i ] - (int)&buf[compiledOfs];
 
1223
                } else {
 
1224
                    v = 0;
 
1225
                }
 
1226
                Emit4("b", PPC_B | (unsigned int)(v&0x3ffffff) );
 
1227
//                InstImm( "bc", PPC_BC, 12, 2, v );
 
1228
                break;                  
 
1229
            case OP_NEF:
 
1230
                #if DEBUG_VM
 
1231
                if(pass == 1)
 
1232
                printf("%08x NEF\n",instruction);
 
1233
                #endif
 
1234
                makeFloat(opStackDepth-1);
 
1235
                makeFloat(opStackDepth-2);
 
1236
                Inst( "fcmpu", PPC_FCMPU, 0, opStackFloatRegisters[opStackDepth-2], opStackFloatRegisters[opStackDepth-1] );
 
1237
                opStackRegType[opStackDepth-1] = 0;
 
1238
                opStackRegType[opStackDepth-2] = 0;
 
1239
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1240
                opStackLoadInstructionAddr[opStackDepth-2] = 0;
 
1241
                opStackDepth -= 2;
 
1242
                i = Constant4();
 
1243
                jused[i] = 1;
 
1244
                InstImm( "bc", PPC_BC, 12, 2, 8 );
 
1245
                if ( pass==1 ) {
 
1246
                    v = vm->instructionPointers[ i ] - (int)&buf[compiledOfs];
 
1247
                } else {
 
1248
                    v = 0;
 
1249
                }
 
1250
                Emit4("b", PPC_B | (unsigned int)(v&0x3ffffff) );
 
1251
//                InstImm( "bc", PPC_BC, 4, 2, v );
 
1252
                break;                  
 
1253
            case OP_LTF:
 
1254
                #if DEBUG_VM
 
1255
                if(pass == 1)
 
1256
                printf("%08x LTF\n",instruction);
 
1257
                #endif
 
1258
                makeFloat(opStackDepth-1);
 
1259
                makeFloat(opStackDepth-2);
 
1260
                Inst( "fcmpu", PPC_FCMPU, 0, opStackFloatRegisters[opStackDepth-2], opStackFloatRegisters[opStackDepth-1] );
 
1261
                opStackRegType[opStackDepth-1] = 0;
 
1262
                opStackRegType[opStackDepth-2] = 0;
 
1263
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1264
                opStackLoadInstructionAddr[opStackDepth-2] = 0;
 
1265
                opStackDepth -= 2;
 
1266
                i = Constant4();
 
1267
                jused[i] = 1;
 
1268
                InstImm( "bc", PPC_BC, 4, 0, 8 );
 
1269
                if ( pass==1 ) {
 
1270
                    v = vm->instructionPointers[ i ] - (int)&buf[compiledOfs];
 
1271
                } else {
 
1272
                    v = 0;
 
1273
                }
 
1274
                Emit4("b", PPC_B | (unsigned int)(v&0x3ffffff) );
 
1275
//                InstImm( "bc", PPC_BC, 12, 0, v );
 
1276
                break;                  
 
1277
            case OP_LEF:
 
1278
                #if DEBUG_VM
 
1279
                if(pass == 1)
 
1280
                printf("%08x LEF\n",instruction);
 
1281
                #endif
 
1282
                makeFloat(opStackDepth-1);
 
1283
                makeFloat(opStackDepth-2);
 
1284
                Inst( "fcmpu", PPC_FCMPU, 0, opStackFloatRegisters[opStackDepth-2], opStackFloatRegisters[opStackDepth-1] );
 
1285
                opStackRegType[opStackDepth-1] = 0;
 
1286
                opStackRegType[opStackDepth-2] = 0;
 
1287
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1288
                opStackLoadInstructionAddr[opStackDepth-2] = 0;
 
1289
                opStackDepth -= 2;
 
1290
                i = Constant4();
 
1291
                jused[i] = 1;
 
1292
                InstImm( "bc", PPC_BC, 12, 1, 8 );
 
1293
                if ( pass==1 ) {
 
1294
                    v = vm->instructionPointers[ i ] - (int)&buf[compiledOfs];
 
1295
                } else {
 
1296
                    v = 0;
 
1297
                }
 
1298
                Emit4("b", PPC_B | (unsigned int)(v&0x3ffffff) );
 
1299
//                InstImm( "bc", PPC_BC, 4, 1, v );
 
1300
                break;                  
 
1301
            case OP_GTF:
 
1302
                #if DEBUG_VM
 
1303
                if(pass == 1)
 
1304
                printf("%08x GTF\n",instruction);
 
1305
                #endif
 
1306
                makeFloat(opStackDepth-1);
 
1307
                makeFloat(opStackDepth-2);
 
1308
                Inst( "fcmpu", PPC_FCMPU, 0, opStackFloatRegisters[opStackDepth-2], opStackFloatRegisters[opStackDepth-1] );
 
1309
                opStackRegType[opStackDepth-1] = 0;
 
1310
                opStackRegType[opStackDepth-2] = 0;
 
1311
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1312
                opStackLoadInstructionAddr[opStackDepth-2] = 0;
 
1313
                opStackDepth -= 2;
 
1314
                i = Constant4();
 
1315
                jused[i] = 1;
 
1316
                InstImm( "bc", PPC_BC, 4, 1, 8 );
 
1317
                if ( pass==1 ) {
 
1318
                    v = vm->instructionPointers[ i ] - (int)&buf[compiledOfs];
 
1319
                } else {
 
1320
                    v = 0;
 
1321
                }
 
1322
                Emit4("b", PPC_B | (unsigned int)(v&0x3ffffff) );
 
1323
//                InstImm( "bc", PPC_BC, 12, 1, v );
 
1324
                break;                  
 
1325
            case OP_GEF:
 
1326
                #if DEBUG_VM
 
1327
                if(pass == 1)
 
1328
                printf("%08x GEF\n",instruction);
 
1329
                #endif
 
1330
                makeFloat(opStackDepth-1);
 
1331
                makeFloat(opStackDepth-2);
 
1332
                Inst( "fcmpu", PPC_FCMPU, 0, opStackFloatRegisters[opStackDepth-2], opStackFloatRegisters[opStackDepth-1] );
 
1333
                opStackRegType[opStackDepth-1] = 0;
 
1334
                opStackRegType[opStackDepth-2] = 0;
 
1335
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1336
                opStackLoadInstructionAddr[opStackDepth-2] = 0;
 
1337
                opStackDepth -= 2;
 
1338
                i = Constant4();
 
1339
                jused[i] = 1;
 
1340
                InstImm( "bc", PPC_BC, 12, 0, 8 );
 
1341
                if ( pass==1 ) {
 
1342
                    v = vm->instructionPointers[ i ] - (int)&buf[compiledOfs];
 
1343
                } else {
 
1344
                    v = 0;
 
1345
                }
 
1346
                Emit4("b", PPC_B | (unsigned int)(v&0x3ffffff) );
 
1347
//                InstImm( "bc", PPC_BC, 4, 0, v );
 
1348
                break;
 
1349
 
 
1350
            case OP_NEGI:
 
1351
                #if DEBUG_VM
 
1352
                if(pass == 1)
 
1353
                printf("%08x NEGI\n",instruction);
 
1354
                #endif
 
1355
                assertInteger(opStackDepth-1);
 
1356
                InstImm( "subfic", PPC_SUBFIC, opStackIntRegisters[opStackDepth-1], opStackIntRegisters[opStackDepth-1], 0 );
 
1357
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1358
                break;
 
1359
            case OP_ADD:
 
1360
                #if DEBUG_VM
 
1361
                if(pass == 1)
 
1362
                printf("%08x ADD\n",instruction);
 
1363
                #endif
 
1364
                assertInteger(opStackDepth-1);
 
1365
                assertInteger(opStackDepth-2);
 
1366
                Inst( "add", PPC_ADD, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-1], opStackIntRegisters[opStackDepth-2] );
 
1367
                opStackRegType[opStackDepth-1] = 0;
 
1368
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1369
                opStackDepth -= 1;
 
1370
                break;
 
1371
            case OP_SUB:
 
1372
                #if DEBUG_VM
 
1373
                if(pass == 1)
 
1374
                printf("%08x SUB\n",instruction);
 
1375
                #endif
 
1376
                assertInteger(opStackDepth-1);
 
1377
                assertInteger(opStackDepth-2);
 
1378
                Inst( "subf", PPC_SUBF, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-1], opStackIntRegisters[opStackDepth-2] );
 
1379
                opStackRegType[opStackDepth-1] = 0;
 
1380
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1381
                opStackDepth -= 1;
 
1382
                break;
 
1383
            case OP_DIVI:
 
1384
                #if DEBUG_VM
 
1385
                if(pass == 1)
 
1386
                printf("%08x DIVI\n",instruction);
 
1387
                #endif
 
1388
                assertInteger(opStackDepth-1);
 
1389
                assertInteger(opStackDepth-2);
 
1390
                Inst( "divw", PPC_DIVW, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-1] );
 
1391
                opStackRegType[opStackDepth-1] = 0;
 
1392
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1393
                opStackDepth -= 1;
 
1394
                break;
 
1395
            case OP_DIVU:
 
1396
                #if DEBUG_VM
 
1397
                if(pass == 1)
 
1398
                printf("%08x DIVU\n",instruction);
 
1399
                #endif
 
1400
                assertInteger(opStackDepth-1);
 
1401
                assertInteger(opStackDepth-2);
 
1402
                Inst( "divwu", PPC_DIVWU, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-1] );
 
1403
                opStackRegType[opStackDepth-1] = 0;
 
1404
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1405
                opStackDepth -= 1;
 
1406
                break;
 
1407
            case OP_MODI:
 
1408
                #if DEBUG_VM
 
1409
                if(pass == 1)
 
1410
                printf("%08x MODI\n",instruction);
 
1411
                #endif
 
1412
                assertInteger(opStackDepth-1);
 
1413
                assertInteger(opStackDepth-2);
 
1414
                Inst( "divw", PPC_DIVW, R_EA, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-1] );
 
1415
                Inst( "mullw", PPC_MULLW, R_EA, opStackIntRegisters[opStackDepth-1], R_EA );
 
1416
                Inst( "subf", PPC_SUBF, opStackIntRegisters[opStackDepth-2], R_EA, opStackIntRegisters[opStackDepth-2] );
 
1417
                opStackRegType[opStackDepth-1] = 0;
 
1418
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1419
                opStackDepth -= 1;
 
1420
                break;
 
1421
            case OP_MODU:
 
1422
                #if DEBUG_VM
 
1423
                if(pass == 1)
 
1424
                printf("%08x MODU\n",instruction);
 
1425
                #endif
 
1426
                assertInteger(opStackDepth-1);
 
1427
                assertInteger(opStackDepth-2);
 
1428
                Inst( "divwu", PPC_DIVWU, R_EA, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-1] );
 
1429
                Inst( "mullw", PPC_MULLW, R_EA, opStackIntRegisters[opStackDepth-1], R_EA );
 
1430
                Inst( "subf", PPC_SUBF, opStackIntRegisters[opStackDepth-2], R_EA, opStackIntRegisters[opStackDepth-2] );
 
1431
                opStackRegType[opStackDepth-1] = 0;
 
1432
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1433
                opStackDepth -= 1;
 
1434
                break;
 
1435
            case OP_MULI:
 
1436
            case OP_MULU:
 
1437
                #if DEBUG_VM
 
1438
                if(pass == 1)
 
1439
                printf("%08x MULI\n",instruction);
 
1440
                #endif
 
1441
                assertInteger(opStackDepth-1);
 
1442
                assertInteger(opStackDepth-2);
 
1443
                Inst( "mullw", PPC_MULLW, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-1], opStackIntRegisters[opStackDepth-2] );
 
1444
                opStackRegType[opStackDepth-1] = 0;
 
1445
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1446
                opStackDepth -= 1;
 
1447
                break;
 
1448
            case OP_BAND:
 
1449
                #if DEBUG_VM
 
1450
                if(pass == 1)
 
1451
                printf("%08x BAND\n",instruction);
 
1452
                #endif
 
1453
                assertInteger(opStackDepth-1);
 
1454
                assertInteger(opStackDepth-2);
 
1455
                Inst( "and", PPC_AND, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-1] );
 
1456
                opStackRegType[opStackDepth-1] = 0;
 
1457
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1458
                opStackDepth -= 1;
 
1459
                break;
 
1460
            case OP_BOR:
 
1461
                #if DEBUG_VM
 
1462
                if(pass == 1)
 
1463
                printf("%08x BOR\n",instruction);
 
1464
                #endif
 
1465
                assertInteger(opStackDepth-1);
 
1466
                assertInteger(opStackDepth-2);
 
1467
                Inst( "or", PPC_OR, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-1] );
 
1468
                opStackRegType[opStackDepth-1] = 0;
 
1469
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1470
                opStackDepth -= 1;
 
1471
                break;
 
1472
            case OP_BXOR:
 
1473
                #if DEBUG_VM
 
1474
                if(pass == 1)
 
1475
                printf("%08x BXOR\n",instruction);
 
1476
                #endif
 
1477
                assertInteger(opStackDepth-1);
 
1478
                assertInteger(opStackDepth-2);
 
1479
                Inst( "xor", PPC_XOR, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-1] );
 
1480
                opStackRegType[opStackDepth-1] = 0;
 
1481
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1482
                opStackDepth -= 1;
 
1483
                break;
 
1484
            case OP_BCOM:
 
1485
                #if DEBUG_VM
 
1486
                if(pass == 1)
 
1487
                printf("%08x BCOM\n",instruction);
 
1488
                #endif
 
1489
                assertInteger(opStackDepth-1);
 
1490
                Inst( "nor", PPC_NOR, opStackIntRegisters[opStackDepth-1], opStackIntRegisters[opStackDepth-1], opStackIntRegisters[opStackDepth-1] );
 
1491
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1492
                break;
 
1493
            case OP_LSH:
 
1494
                #if DEBUG_VM
 
1495
                if(pass == 1)
 
1496
                printf("%08x LSH\n",instruction);
 
1497
                #endif
 
1498
                assertInteger(opStackDepth-1);
 
1499
                assertInteger(opStackDepth-2);
 
1500
                Inst( "slw", PPC_SLW, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-1] );
 
1501
                opStackRegType[opStackDepth-1] = 0;
 
1502
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1503
                opStackDepth -= 1;
 
1504
                break;
 
1505
            case OP_RSHI:
 
1506
                #if DEBUG_VM
 
1507
                if(pass == 1)
 
1508
                printf("%08x RSHI\n",instruction);
 
1509
                #endif
 
1510
                assertInteger(opStackDepth-1);
 
1511
                assertInteger(opStackDepth-2);
 
1512
                Inst( "sraw", PPC_SRAW, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-1] );
 
1513
                opStackRegType[opStackDepth-1] = 0;
 
1514
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1515
                opStackDepth -= 1;
 
1516
                break;
 
1517
            case OP_RSHU:
 
1518
                #if DEBUG_VM
 
1519
                if(pass == 1)
 
1520
                printf("%08x RSHU\n",instruction);
 
1521
                #endif
 
1522
                assertInteger(opStackDepth-1);
 
1523
                assertInteger(opStackDepth-2);
 
1524
                Inst( "srw", PPC_SRW, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-1] );
 
1525
                opStackRegType[opStackDepth-1] = 0;
 
1526
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1527
                opStackDepth -= 1;
 
1528
                break;
 
1529
 
 
1530
            case OP_NEGF:
 
1531
                #if DEBUG_VM
 
1532
                if(pass == 1)
 
1533
                printf("%08x NEGF\n",instruction);
 
1534
                #endif
 
1535
                makeFloat(opStackDepth-1);
 
1536
                Inst( "fneg", PPC_FNEG, opStackFloatRegisters[opStackDepth-1], 0, opStackFloatRegisters[opStackDepth-1] );
 
1537
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1538
                break;
 
1539
            case OP_ADDF:
 
1540
                #if DEBUG_VM
 
1541
                if(pass == 1)
 
1542
                printf("%08x ADDF\n",instruction);
 
1543
                #endif
 
1544
                makeFloat(opStackDepth-1);
 
1545
                makeFloat(opStackDepth-2);
 
1546
                Inst( "fadds", PPC_FADDS, opStackFloatRegisters[opStackDepth-2], opStackFloatRegisters[opStackDepth-2], opStackFloatRegisters[opStackDepth-1] );
 
1547
                opStackRegType[opStackDepth-1] = 0;
 
1548
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1549
                opStackDepth -= 1;
 
1550
                break;
 
1551
            case OP_SUBF:
 
1552
                #if DEBUG_VM
 
1553
                if(pass == 1)
 
1554
                printf("%08x SUBF\n",instruction);
 
1555
                #endif
 
1556
                makeFloat(opStackDepth-1);
 
1557
                makeFloat(opStackDepth-2);
 
1558
                Inst( "fsubs", PPC_FSUBS, opStackFloatRegisters[opStackDepth-2], opStackFloatRegisters[opStackDepth-2], opStackFloatRegisters[opStackDepth-1] );
 
1559
                opStackRegType[opStackDepth-1] = 0;
 
1560
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1561
                opStackDepth -= 1;
 
1562
                break;
 
1563
            case OP_DIVF:
 
1564
                #if DEBUG_VM
 
1565
                if(pass == 1)
 
1566
                printf("%08x DIVF\n",instruction);
 
1567
                #endif
 
1568
                makeFloat(opStackDepth-1);
 
1569
                makeFloat(opStackDepth-2);
 
1570
                Inst( "fdivs", PPC_FDIVS, opStackFloatRegisters[opStackDepth-2], opStackFloatRegisters[opStackDepth-2], opStackFloatRegisters[opStackDepth-1] );
 
1571
                opStackRegType[opStackDepth-1] = 0;
 
1572
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1573
                opStackDepth -= 1;
 
1574
                break;
 
1575
            case OP_MULF:
 
1576
                #if DEBUG_VM
 
1577
                if(pass == 1)
 
1578
                printf("%08x MULF\n",instruction);
 
1579
                #endif
 
1580
                makeFloat(opStackDepth-1);
 
1581
                makeFloat(opStackDepth-2);
 
1582
                Inst4( "fmuls", PPC_FMULS, opStackFloatRegisters[opStackDepth-2], opStackFloatRegisters[opStackDepth-2], 0, opStackFloatRegisters[opStackDepth-1] );
 
1583
                opStackRegType[opStackDepth-1] = 0;
 
1584
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1585
                opStackDepth -= 1;
 
1586
                break;
 
1587
 
 
1588
            case OP_CVIF:
 
1589
                #if DEBUG_VM
 
1590
                if(pass == 1)
 
1591
                printf("%08x CVIF\n",instruction);
 
1592
                #endif
 
1593
                assertInteger(opStackDepth-1);
 
1594
                //makeInteger(opStackDepth-1);
 
1595
                v = (int)&itofConvert;
 
1596
                InstImmU( "addis", PPC_ADDIS, R_EA, 0, (v >> 16)&0xffff );
 
1597
                InstImmU( "ori", PPC_ORI, R_EA, R_EA, v & 0xffff );
 
1598
                InstImmU( "xoris", PPC_XORIS, opStackIntRegisters[opStackDepth-1], opStackIntRegisters[opStackDepth-1], 0x8000 );
 
1599
                InstImm( "stw", PPC_STW, opStackIntRegisters[opStackDepth-1], R_EA, 12 );
 
1600
                InstImm( "lfd", PPC_LFD, opStackFloatRegisters[opStackDepth-1], R_EA, 0 );
 
1601
                Inst( "ori", PPC_ORI, 0, 0, 0);
 
1602
                Inst( "ori", PPC_ORI, 0, 0, 0);
 
1603
                Inst( "ori", PPC_ORI, 0, 0, 0);
 
1604
                InstImm( "lfd", PPC_LFD, 13, R_EA, 8 );
 
1605
                Inst( "fsub", PPC_FSUB, opStackFloatRegisters[opStackDepth-1], 13, opStackFloatRegisters[opStackDepth-1] );
 
1606
                opStackRegType[opStackDepth-1] = 2;
 
1607
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1608
    //            Inst( PPC_FRSP, R_TOP, 0, R_TOP );
 
1609
                break;
 
1610
            case OP_CVFI:
 
1611
                #if DEBUG_VM
 
1612
                if(pass == 1)
 
1613
                printf("%08x CVFI\n",instruction);
 
1614
                #endif
 
1615
                makeFloat(opStackDepth-1);
 
1616
 
 
1617
                InstImm( "addi", PPC_ADDI, R_OPSTACK, R_OPSTACK, opStackDepth*4);
 
1618
 
 
1619
                Inst( "fctiwz", PPC_FCTIWZ, opStackFloatRegisters[opStackDepth-1], 0, opStackFloatRegisters[opStackDepth-1] );
 
1620
                Inst( "stfiwx", PPC_STFIWX, opStackFloatRegisters[opStackDepth-1], 0, R_OPSTACK );              // save value to opstack (dummy area now)
 
1621
                Inst( "ori", PPC_ORI, 0, 0, 0);
 
1622
                Inst( "ori", PPC_ORI, 0, 0, 0);
 
1623
                Inst( "ori", PPC_ORI, 0, 0, 0);
 
1624
                Inst( "ori", PPC_ORI, 0, 0, 0);
 
1625
                InstImm( "lwz", PPC_LWZ, opStackIntRegisters[opStackDepth-1], R_OPSTACK, 0 );
 
1626
                
 
1627
                InstImm( "addi", PPC_ADDI, R_OPSTACK, R_OPSTACK, -opStackDepth*4);
 
1628
                
 
1629
                opStackRegType[opStackDepth-1] = 1;
 
1630
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1631
                break;
 
1632
            case OP_SEX8:
 
1633
                #if DEBUG_VM
 
1634
                if(pass == 1)
 
1635
                printf("%08x SEX8\n",instruction);
 
1636
                #endif
 
1637
                assertInteger(opStackDepth-1);
 
1638
                Inst( "extsb", PPC_EXTSB, opStackIntRegisters[opStackDepth-1], opStackIntRegisters[opStackDepth-1], 0 );
 
1639
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1640
                break;
 
1641
            case OP_SEX16:
 
1642
                #if DEBUG_VM
 
1643
                if(pass == 1)
 
1644
                printf("%08x SEX16\n",instruction);
 
1645
                #endif
 
1646
                assertInteger(opStackDepth-1);
 
1647
                Inst( "extsh", PPC_EXTSH, opStackIntRegisters[opStackDepth-1], opStackIntRegisters[opStackDepth-1], 0 );
 
1648
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1649
                break;
 
1650
 
 
1651
            case OP_BLOCK_COPY:
 
1652
                v = Constant4() >> 2;
 
1653
                #if DEBUG_VM
 
1654
                if(pass == 1)
 
1655
                printf("%08x BLOCK_COPY\t%08lx\n",instruction,v<<2);
 
1656
                #endif
 
1657
                assert(opStackDepth >= 2);
 
1658
                assertInteger(opStackDepth-1);
 
1659
                assertInteger(opStackDepth-2);
 
1660
                InstImmU( "addi", PPC_ADDI, R_EA, 0, v );                               // count
 
1661
                                // FIXME: range check
 
1662
                Inst( "mtctr", PPC_MTSPR, R_EA, 9, 0 );                                 // move to count register
 
1663
 
 
1664
                Inst( "add", PPC_ADD, opStackIntRegisters[opStackDepth-1], opStackIntRegisters[opStackDepth-1], R_MEMBASE );
 
1665
                InstImm( "addi", PPC_ADDI, opStackIntRegisters[opStackDepth-1], opStackIntRegisters[opStackDepth-1], -4 );
 
1666
                Inst( "add", PPC_ADD, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-2], R_MEMBASE );
 
1667
                InstImm( "addi", PPC_ADDI, opStackIntRegisters[opStackDepth-2], opStackIntRegisters[opStackDepth-2], -4 );
 
1668
 
 
1669
                InstImm( "lwzu", PPC_LWZU, R_EA, opStackIntRegisters[opStackDepth-1], 4 );              // source
 
1670
                InstImm( "stwu", PPC_STWU, R_EA, opStackIntRegisters[opStackDepth-2], 4 );      // dest
 
1671
                Inst( "b", PPC_BC | 0xfff8 , 16, 0, 0 );                                        // loop
 
1672
                opStackRegType[opStackDepth-1] = 0;
 
1673
                opStackRegType[opStackDepth-2] = 0;
 
1674
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1675
                opStackLoadInstructionAddr[opStackDepth-2] = 0;
 
1676
                opStackDepth -= 2;
 
1677
                break;
 
1678
 
 
1679
            case OP_JUMP:
 
1680
                #if DEBUG_VM
 
1681
                if(pass == 1)
 
1682
                printf("%08x JUMP\n",instruction);
 
1683
                #endif
 
1684
                assert(opStackDepth == 1);
 
1685
                assertInteger(opStackDepth-1);
 
1686
 
 
1687
                Inst( "rlwinm", PPC_RLWINM | ( 29 << 1 ), opStackIntRegisters[opStackDepth-1], opStackIntRegisters[opStackDepth-1], 2 );
 
1688
                // FIXME: range check
 
1689
                Inst( "lwzx", PPC_LWZX, opStackIntRegisters[opStackDepth-1], opStackIntRegisters[opStackDepth-1], R_INSTRUCTIONS );
 
1690
                Inst( "mtctr", PPC_MTSPR, opStackIntRegisters[opStackDepth-1], 9, 0 );          // move to count register
 
1691
                Inst( "bctr", PPC_BCCTR, 20, 0, 0 );            // jump to the count register
 
1692
                opStackRegType[opStackDepth-1] = 0;
 
1693
                opStackLoadInstructionAddr[opStackDepth-1] = 0;
 
1694
                opStackDepth -= 1;
 
1695
                break;
 
1696
            default:
 
1697
                Com_Error( ERR_DROP, "VM_CompilePPC: bad opcode %i at instruction %i, offset %i", op, instruction, pc );
 
1698
            }
 
1699
            pop0 = pop1;
 
1700
            pop1 = op;
 
1701
                assert(opStackDepth >= 0);
 
1702
                assert(opStackDepth < OP_STACK_MAX_DEPTH);
 
1703
                
 
1704
                //printf("%4d\t%s\n",opStackDepth,opnames[op]);
 
1705
        }
 
1706
 
 
1707
        Com_Printf( "VM file %s pass %d compiled to %i bytes of code\n", vm->name, (pass+1), compiledOfs*4 );
 
1708
 
 
1709
        if ( pass == 0 ) {
 
1710
            // copy to an exact size buffer on the hunk
 
1711
            vm->codeLength = compiledOfs * 4;
 
1712
            vm->codeBase = Hunk_Alloc( vm->codeLength, h_low );
 
1713
            Com_Memcpy( vm->codeBase, buf, vm->codeLength );
 
1714
            
 
1715
            //printf("codeBase: %p\n",vm->codeBase);
 
1716
            
 
1717
            Z_Free( buf );
 
1718
        
 
1719
            // offset all the instruction pointers for the new location
 
1720
            for ( i = 0 ; i < header->instructionCount ; i++ ) {
 
1721
                vm->instructionPointers[i] += (int)vm->codeBase;
 
1722
                //printf("%08x %08lx\n",i,vm->instructionPointers[i]);
 
1723
            }
 
1724
 
 
1725
            // go back over it in place now to fixup reletive jump targets
 
1726
            buf = (unsigned *)vm->codeBase;
 
1727
        } else if ( pass == 1 ) {
 
1728
           #ifdef MACOS_X
 
1729
           // On Mac OS X, the following library routine clears the instruction cache for generated code
 
1730
           MakeDataExecutable(vm->codeBase, vm->codeLength);
 
1731
           #else
 
1732
           #warning Need to clear the instruction cache for generated code
 
1733
           #endif
 
1734
       }
 
1735
    }
 
1736
    if(0)
 
1737
    {
 
1738
        char buf[256];
 
1739
        printf("wait..\n");
 
1740
        gets(buf);
 
1741
    }
 
1742
    Z_Free( jused );
 
1743
}
 
1744
 
 
1745
/*
 
1746
==============
 
1747
VM_CallCompiled
 
1748
 
 
1749
This function is called directly by the generated code
 
1750
==============
 
1751
*/
 
1752
int     VM_CallCompiled( vm_t *vm, int *args ) {
 
1753
        int             stack[1024];
 
1754
        int             programStack;
 
1755
        int             stackOnEntry;
 
1756
        byte    *image;
 
1757
 
 
1758
        currentVM = vm;
 
1759
 
 
1760
        //printf("VM_CallCompiled: %p   %08lx %08lx %08lx\n",
 
1761
        //      vm, args[0],args[1],args[2]);
 
1762
                
 
1763
        // interpret the code
 
1764
        vm->currentlyInterpreting = qtrue;
 
1765
 
 
1766
        // we might be called recursively, so this might not be the very top
 
1767
        programStack = vm->programStack;
 
1768
        stackOnEntry = programStack;
 
1769
        image = vm->dataBase;
 
1770
        
 
1771
        // set up the stack frame 
 
1772
        programStack -= 48;
 
1773
 
 
1774
        *(int *)&image[ programStack + 44] = args[9];
 
1775
        *(int *)&image[ programStack + 40] = args[8];
 
1776
        *(int *)&image[ programStack + 36] = args[7];
 
1777
        *(int *)&image[ programStack + 32] = args[6];
 
1778
        *(int *)&image[ programStack + 28] = args[5];
 
1779
        *(int *)&image[ programStack + 24] = args[4];
 
1780
        *(int *)&image[ programStack + 20] = args[3];
 
1781
        *(int *)&image[ programStack + 16] = args[2];
 
1782
        *(int *)&image[ programStack + 12] = args[1];
 
1783
        *(int *)&image[ programStack + 8 ] = args[0];
 
1784
        *(int *)&image[ programStack + 4 ] = 0; // return stack
 
1785
        *(int *)&image[ programStack ] = -1;    // will terminate the loop on return
 
1786
 
 
1787
        // Cheesy... manually save registers used by VM call...
 
1788
        // off we go into generated code...
 
1789
        // the PPC calling standard says the parms will all go into R3 - R11, so 
 
1790
        // no special asm code is needed here
 
1791
#ifdef __GNUC__
 
1792
        ((void(*)(int, int, int, int, int, int, int, int))(vm->codeBase))( 
 
1793
                programStack, (int)&stack, 
 
1794
                (int)image, vm->dataMask, (int)&AsmCall, 
 
1795
                (int)vm->instructionPointers, vm->instructionPointersLength,
 
1796
        (int)vm );
 
1797
#else
 
1798
        ((void(*)(int, int, int, int, int, int, int, int))(vm->codeBase))( 
 
1799
                programStack, (int)&stack, 
 
1800
                (int)image, vm->dataMask, *(int *)&AsmCall /* skip function pointer header */, 
 
1801
                (int)vm->instructionPointers, vm->instructionPointersLength,
 
1802
        (int)vm );
 
1803
#endif
 
1804
        vm->programStack = stackOnEntry;
 
1805
 
 
1806
    vm->currentlyInterpreting = qfalse;
 
1807
 
 
1808
        return stack[1];
 
1809
}
 
1810
 
 
1811
 
 
1812
/*
 
1813
==================
 
1814
AsmCall
 
1815
 
 
1816
Put this at end of file because gcc messes up debug line numbers 
 
1817
==================
 
1818
*/
 
1819
#ifdef __GNUC__
 
1820
 
 
1821
void AsmCall( void ) {
 
1822
asm (
 
1823
     // pop off the destination instruction
 
1824
"    lwz                r12,0(r4)       \n"     // RG_TOP, 0(RG_OPSTACK)
 
1825
"    addi       r4,r4,-4                \n"     // RG_OPSTACK, RG_OPSTACK, -4 \n"
 
1826
 
 
1827
    // see if it is a system trap
 
1828
"    cmpwi      r12,0                   \n"     // RG_TOP, 0 \n"
 
1829
"    bc         12,0, systemTrap        \n"
 
1830
 
 
1831
    // calling another VM function, so lookup in instructionPointers
 
1832
"    slwi       r12,r12,2               \n"     // RG_TOP,RG_TOP,2
 
1833
                        // FIXME: range check
 
1834
"    lwzx       r12, r8, r12            \n"     // RG_TOP, RG_INSTRUCTIONS(RG_TOP)      
 
1835
"    mtctr      r12                     \n"     // RG_TOP
 
1836
);
 
1837
 
 
1838
#if defined(MACOS_X) && defined(__OPTIMIZE__)
 
1839
    // On Mac OS X, gcc doesn't push a frame when we are optimized, so trying to tear it down results in grave disorder.
 
1840
//#warning Mac OS X optimization on, not popping GCC AsmCall frame
 
1841
#else
 
1842
    // Mac OS X Server and unoptimized compiles include a GCC AsmCall frame
 
1843
    asm (
 
1844
"       lwz             r1,0(r1)        \n"     // pop off the GCC AsmCall frame
 
1845
"       lmw             r30,-8(r1)      \n"
 
1846
);
 
1847
#endif
 
1848
 
 
1849
asm (
 
1850
"           bcctr       20,0            \n" // when it hits a leave, it will branch to the current link register
 
1851
 
 
1852
    // calling a system trap
 
1853
"systemTrap:                            \n"
 
1854
        // convert to positive system call number
 
1855
"       subfic  r12,r12,-1              \n"
 
1856
 
 
1857
    // save all our registers, including the current link register
 
1858
"    mflr       r13                     \n"     // RG_SECOND            // copy off our link register
 
1859
"       addi    r1,r1,-92               \n"     // required 24 byets of linkage, 32 bytes of parameter, plus our saves
 
1860
"    stw                r3,56(r1)       \n"     // RG_STACK, -36(REAL_STACK)
 
1861
"    stw                r4,60(r1)       \n"     // RG_OPSTACK, 4(RG_REAL_STACK)
 
1862
"    stw                r5,64(r1)       \n"     // RG_MEMBASE, 8(RG_REAL_STACK)
 
1863
"    stw                r6,68(r1)       \n"     // RG_MEMMASK, 12(RG_REAL_STACK)
 
1864
"    stw                r7,72(r1)       \n"     // RG_ASMCALL, 16(RG_REAL_STACK)
 
1865
"    stw                r8,76(r1)       \n"     // RG_INSTRUCTIONS, 20(RG_REAL_STACK)
 
1866
"    stw                r9,80(r1)       \n"     // RG_NUM_INSTRUCTIONS, 24(RG_REAL_STACK)
 
1867
"    stw                r10,84(r1)      \n"     // RG_VM, 28(RG_REAL_STACK)
 
1868
"    stw                r13,88(r1)      \n"     // RG_SECOND, 32(RG_REAL_STACK) // link register
 
1869
 
 
1870
    // save the vm stack position to allow recursive VM entry
 
1871
"    addi       r13,r3,-4               \n"     // RG_TOP, RG_STACK, -4
 
1872
"    stw                r13,0(r10)      \n"     //RG_TOP, VM_OFFSET_PROGRAM_STACK(RG_VM)
 
1873
 
 
1874
    // save the system call number as the 0th parameter
 
1875
"    add                r3,r3,r5        \n"     // r3,  RG_STACK, RG_MEMBASE            // r3 is the first parameter to vm->systemCalls
 
1876
"    stwu       r12,4(r3)               \n"     // RG_TOP, 4(r3)
 
1877
 
 
1878
    // make the system call with the address of all the VM parms as a parameter
 
1879
    // vm->systemCalls( &parms )
 
1880
"    lwz                r12,4(r10)      \n"     // RG_TOP, VM_OFFSET_SYSTEM_CALL(RG_VM)
 
1881
"    mtctr      r12                     \n"     // RG_TOP
 
1882
"    bcctrl     20,0                    \n"
 
1883
"    mr         r12,r3                  \n"     // RG_TOP, r3
 
1884
 
 
1885
    // pop our saved registers
 
1886
"       lwz             r3,56(r1)       \n"     // RG_STACK, 0(RG_REAL_STACK)
 
1887
"       lwz             r4,60(r1)       \n"     // RG_OPSTACK, 4(RG_REAL_STACK)
 
1888
"       lwz             r5,64(r1)       \n"     // RG_MEMBASE, 8(RG_REAL_STACK)
 
1889
"       lwz             r6,68(r1)       \n"     // RG_MEMMASK, 12(RG_REAL_STACK)
 
1890
"       lwz             r7,72(r1)       \n"     // RG_ASMCALL, 16(RG_REAL_STACK)
 
1891
"       lwz             r8,76(r1)       \n"     // RG_INSTRUCTIONS, 20(RG_REAL_STACK)
 
1892
"       lwz             r9,80(r1)       \n"     // RG_NUM_INSTRUCTIONS, 24(RG_REAL_STACK)
 
1893
"       lwz             r10,84(r1)      \n"     // RG_VM, 28(RG_REAL_STACK)
 
1894
"       lwz             r13,88(r1)      \n"     // RG_SECOND, 32(RG_REAL_STACK)
 
1895
"    addi       r1,r1,92                \n"     // RG_REAL_STACK, RG_REAL_STACK, 36
 
1896
 
 
1897
    // restore the old link register
 
1898
"    mtlr       r13                     \n"     // RG_SECOND
 
1899
 
 
1900
    // save off the return value
 
1901
"    stwu       r12,4(r4)               \n"     // RG_TOP, 0(RG_OPSTACK)
 
1902
 
 
1903
        // GCC adds its own prolog / epliog code
 
1904
 );
 
1905
}
 
1906
#else
 
1907
 
 
1908
// codewarrior version
 
1909
 
 
1910
void asm AsmCall( void ) {
 
1911
 
 
1912
    // pop off the destination instruction
 
1913
 
 
1914
    lwz         r12,0(r4)       // RG_TOP, 0(RG_OPSTACK)
 
1915
 
 
1916
    addi        r4,r4,-4        // RG_OPSTACK, RG_OPSTACK, -4
 
1917
 
 
1918
 
 
1919
 
 
1920
    // see if it is a system trap
 
1921
 
 
1922
    cmpwi       r12,0           // RG_TOP, 0
 
1923
 
 
1924
    bc          12,0, systemTrap
 
1925
 
 
1926
 
 
1927
 
 
1928
    // calling another VM function, so lookup in instructionPointers
 
1929
 
 
1930
    slwi        r12,r12,2               // RG_TOP,RG_TOP,2
 
1931
 
 
1932
                        // FIXME: range check
 
1933
 
 
1934
    lwzx        r12, r8, r12    // RG_TOP, RG_INSTRUCTIONS(RG_TOP)      
 
1935
 
 
1936
    mtctr       r12                     // RG_TOP
 
1937
 
 
1938
 
 
1939
 
 
1940
    bcctr       20,0            // when it hits a leave, it will branch to the current link register
 
1941
 
 
1942
 
 
1943
 
 
1944
    // calling a system trap
 
1945
 
 
1946
systemTrap:
 
1947
 
 
1948
        // convert to positive system call number
 
1949
 
 
1950
        subfic  r12,r12,-1
 
1951
 
 
1952
 
 
1953
 
 
1954
    // save all our registers, including the current link register
 
1955
 
 
1956
    mflr        r13                     // RG_SECOND            // copy off our link register
 
1957
 
 
1958
        addi    r1,r1,-92       // required 24 byets of linkage, 32 bytes of parameter, plus our saves
 
1959
 
 
1960
    stw         r3,56(r1)       // RG_STACK, -36(REAL_STACK)
 
1961
 
 
1962
    stw         r4,60(r1)       // RG_OPSTACK, 4(RG_REAL_STACK)
 
1963
 
 
1964
    stw         r5,64(r1)       // RG_MEMBASE, 8(RG_REAL_STACK)
 
1965
 
 
1966
    stw         r6,68(r1)       // RG_MEMMASK, 12(RG_REAL_STACK)
 
1967
 
 
1968
    stw         r7,72(r1)       // RG_ASMCALL, 16(RG_REAL_STACK)
 
1969
 
 
1970
    stw         r8,76(r1)       // RG_INSTRUCTIONS, 20(RG_REAL_STACK)
 
1971
 
 
1972
    stw         r9,80(r1)       // RG_NUM_INSTRUCTIONS, 24(RG_REAL_STACK)
 
1973
 
 
1974
    stw         r10,84(r1)      // RG_VM, 28(RG_REAL_STACK)
 
1975
 
 
1976
    stw         r13,88(r1)      // RG_SECOND, 32(RG_REAL_STACK) // link register
 
1977
 
 
1978
 
 
1979
 
 
1980
    // save the vm stack position to allow recursive VM entry
 
1981
 
 
1982
    addi        r13,r3,-4       // RG_TOP, RG_STACK, -4
 
1983
 
 
1984
    stw         r13,0(r10)      //RG_TOP, VM_OFFSET_PROGRAM_STACK(RG_VM)
 
1985
 
 
1986
 
 
1987
 
 
1988
    // save the system call number as the 0th parameter
 
1989
 
 
1990
    add         r3,r3,r5        // r3,  RG_STACK, RG_MEMBASE            // r3 is the first parameter to vm->systemCalls
 
1991
 
 
1992
    stwu        r12,4(r3)       // RG_TOP, 4(r3)
 
1993
 
 
1994
 
 
1995
 
 
1996
    // make the system call with the address of all the VM parms as a parameter
 
1997
 
 
1998
    // vm->systemCalls( &parms )
 
1999
 
 
2000
    lwz         r12,4(r10)      // RG_TOP, VM_OFFSET_SYSTEM_CALL(RG_VM)
 
2001
 
 
2002
    
 
2003
 
 
2004
    // perform macos cross fragment fixup crap
 
2005
 
 
2006
    lwz         r9,0(r12)
 
2007
 
 
2008
    stw         r2,52(r1)       // save old TOC
 
2009
 
 
2010
        lwz             r2,4(r12)
 
2011
 
 
2012
            
 
2013
 
 
2014
    mtctr       r9                      // RG_TOP
 
2015
 
 
2016
    bcctrl      20,0
 
2017
 
 
2018
    
 
2019
 
 
2020
    lwz         r2,52(r1)       // restore TOC
 
2021
 
 
2022
    
 
2023
 
 
2024
    mr          r12,r3          // RG_TOP, r3
 
2025
 
 
2026
 
 
2027
 
 
2028
    // pop our saved registers
 
2029
 
 
2030
        lwz             r3,56(r1)       // RG_STACK, 0(RG_REAL_STACK)
 
2031
 
 
2032
        lwz             r4,60(r1)       // RG_OPSTACK, 4(RG_REAL_STACK)
 
2033
 
 
2034
        lwz             r5,64(r1)       // RG_MEMBASE, 8(RG_REAL_STACK)
 
2035
 
 
2036
        lwz             r6,68(r1)       // RG_MEMMASK, 12(RG_REAL_STACK)
 
2037
 
 
2038
        lwz             r7,72(r1)       // RG_ASMCALL, 16(RG_REAL_STACK)
 
2039
 
 
2040
        lwz             r8,76(r1)       // RG_INSTRUCTIONS, 20(RG_REAL_STACK)
 
2041
 
 
2042
        lwz             r9,80(r1)       // RG_NUM_INSTRUCTIONS, 24(RG_REAL_STACK)
 
2043
 
 
2044
        lwz             r10,84(r1)      // RG_VM, 28(RG_REAL_STACK)
 
2045
 
 
2046
        lwz             r13,88(r1)      // RG_SECOND, 32(RG_REAL_STACK)
 
2047
 
 
2048
    addi        r1,r1,92        // RG_REAL_STACK, RG_REAL_STACK, 36
 
2049
 
 
2050
 
 
2051
 
 
2052
    // restore the old link register
 
2053
 
 
2054
    mtlr        r13                     // RG_SECOND
 
2055
 
 
2056
 
 
2057
 
 
2058
    // save off the return value
 
2059
 
 
2060
    stwu        r12,4(r4)       // RG_TOP, 0(RG_OPSTACK)
 
2061
 
 
2062
 
 
2063
 
 
2064
        blr
 
2065
 
 
2066
}
 
2067
 
 
2068
 
 
2069
 
 
2070
 
 
2071
#endif