Debian Codegraph @

code/qcommon/vm_powerpc.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 2008 Przemyslaw Iskra <[email protected]>
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
23
24 #if defined(powerc) || defined(powerpc) || defined(__powerpc__) || \
25 defined(ppc) || defined(__ppc) || defined(__ppc__) || \
26 defined(__powerpc64__) || defined(__ppc64__)
27
28 #include <sys/types.h> /* needed by sys/mman.h on OSX */
29 #include <sys/mman.h>
30 #include <sys/time.h>
31 #include <time.h>
32 #include <stddef.h>
33 #include <string.h>
34 #include <stdio.h>
35 #include <inttypes.h>
36
37 #ifndef MAP_ANONYMOUS
38 # define MAP_ANONYMOUS MAP_ANON
39 #endif
40
41 #include "vm_local.h"
42
43 /*
44 * Register information according to:
45 * http://refspecs.freestandards.org/elf/elfspec_ppc.pdf
46 */
47
48 #define r0 0 // volatile
49 #define r1 1 // caller safe ( stack pointer )
50 #define r2 2 // reserved
51 #define r3 3 // callee safe
52 #define r4 4 // callee safe
53 #define r5 5 // callee safe
54 #define r6 6 // callee safe
55 #define r7 7 // callee safe
56 #define r8 8 // callee safe
57 #define r9 9 // callee safe
58 #define r10 10 // callee safe
59 #define r11 11 // volatile
60 #define r12 12 // volatile
61 #define r13 13 // reserved ( small data area )
62 #define r14 14 // caller safe
63 #define r15 15 // caller safe
64 #define r16 16 // caller safe
65 #define r17 17 // caller safe
66 #define r18 18 // caller safe
67 #define r19 19 // caller safe
68 #define r20 20 // caller safe
69 #define r21 21 // caller safe
70 #define r22 22 // caller safe
71 #define r23 23 // caller safe
72 #define r24 24 // caller safe
73 #define r25 25 // caller safe
74 #define r26 26 // caller safe
75 #define r27 27 // caller safe
76 #define r28 28 // caller safe
77 #define r29 29 // caller safe
78 #define r30 30 // caller safe
79 #define r31 31 // caller safe ( environment pointers )
80
81 #define f0 0 // callee safe
82 #define f1 1 // callee safe
83 #define f2 2 // callee safe
84 #define f3 3 // callee safe
85 #define f4 4 // callee safe
86 #define f5 5 // callee safe
87 #define f6 6 // callee safe
88 #define f7 7 // callee safe
89 #define f8 8 // callee safe
90 #define f9 9 // callee safe
91 #define f10 10 // callee safe
92 #define f11 11 // callee safe
93 #define f12 12 // callee safe
94 #define f13 13 // callee safe
95 #define f14 14 // caller safe
96 #define f15 15 // caller safe
97 #define f16 16 // caller safe
98 #define f17 17 // caller safe
99 #define f18 18 // caller safe
100 #define f19 19 // caller safe
101 #define f20 20 // caller safe
102 #define f21 21 // caller safe
103 #define f22 22 // caller safe
104 #define f23 23 // caller safe
105 #define f24 24 // caller safe
106 #define f25 25 // caller safe
107 #define f26 26 // caller safe
108 #define f27 27 // caller safe
109 #define f28 28 // caller safe
110 #define f29 29 // caller safe
111 #define f30 30 // caller safe
112 #define f31 31 // caller safe
113
114 #define cr0 0 // volatile
115 #define cr1 1 // volatile
116 #define cr2 2 // caller safe
117 #define cr3 3 // caller safe
118 #define cr4 4 // caller safe
119 #define cr5 5 // volatile
120 #define cr6 6 // volatile
121 #define cr7 7 // volatile
122
123 #define lt 0
124 #define gt 1
125 #define eq 2
126 #define so 3
127
128 // branch bo field values
129 #define branchLikely 1
130 #define branchFalse 4
131 #define branchTrue 12
132 #define branchAlways 20
133
134 // branch extensions (change branch type)
135 #define branchExtLink 0x0001
136
137
138 /*
139 * This list must match exactly the powerpc_opcodes list from vm_powerpc_asm.c
140 * If you're changing the original list remember to regenerate this one. You
141 * may do so using this perl script:
142 perl -p -e 'BEGIN{%t=("-"=>m=>"+"=>p=>"."=>d=>);$l=""}$o=0 if/^}/;
143 if($o && s/^{ "(.*?)([\.+-])?".+/i\U$1\E$t{$2}/s){$_.="_" while$l{$_};
144 $l{$_}=1;if(length $l.$_ > 70){$s=$_;$_="\t$l\n";$l="$s,"}else
145 {$l.=" $_,";$_=undef}}else{$o=1 if/powerpc_opcodes.*=/;$_=undef};
146 END{print "\t$l\n"}' < vm_powerpc_asm.c
147 */
148
149 typedef enum powerpc_iname {
150 iCMPLWI, iCMPWI, iCMPW, iCMPLW, iFCMPU, iLI, iLIS, iADDI, iADDIS,
151 iBLTm, iBC, iBCL, iB, iBL, iBLR, iBCTR, iBCTRL, iRLWINM, iNOP, iORI,
152 iXORIS, iLDX, iLWZX, iSLW, iAND, iSUB, iLBZX, iNEG, iNOT, iSTWX, iSTBX,
153 iMULLW, iADD, iLHZX, iXOR, iMFLR, iSTHX, iMR, iOR, iDIVWU, iMTLR,
154 iMTCTR, iDIVW, iLFSX, iSRW, iSTFSX, iSRAW, iEXTSH, iEXTSB, iLWZ, iLBZ,
155 iSTW, iSTWU, iSTB, iLHZ, iSTH, iLFS, iLFD, iSTFS, iSTFD, iLD, iFDIVS,
156 iFSUBS, iFADDS, iFMULS, iSTD, iSTDU, iFRSP, iFCTIWZ, iFSUB, iFNEG,
157 } powerpc_iname_t;
158
159 #include <stdint.h>
160
161 typedef uint32_t ppc_instruction_t;
162
163 extern ppc_instruction_t
164 asm_instruction( powerpc_iname_t, const int, const long int * );
165
166 #define IN( inst, args... ) \
167 ({\
168 const long int argv[] = { args };\
169 const int argc = sizeof( argv ) / sizeof( argv[0] ); \
170 asm_instruction( inst, argc, argv );\
171 })
172
173 /* return nop on error */
174 #define ASM_ERROR_OPC (0x60000000)
175
176 /*
177 * BEGIN OF ppc.h
178 */
179
180 #define ppc_cpu_t int
181
182 struct powerpc_opcode
183 {
184 const char *name;
185 unsigned long opcode;
186 unsigned long mask;
187 ppc_cpu_t flags;
188 unsigned char operands[8];
189 };
190
191 static const struct powerpc_opcode powerpc_opcodes[];
192
193 #define PPC_OPCODE_PPC 1
194 #define PPC_OPCODE_POWER 2
195 #define PPC_OPCODE_POWER2 4
196 #define PPC_OPCODE_32 8
197 #define PPC_OPCODE_64 0x10
198 #define PPC_OPCODE_601 0x20
199 #define PPC_OPCODE_COMMON 0x40
200 #define PPC_OPCODE_ANY 0x80
201 #define PPC_OPCODE_64_BRIDGE 0x100
202 #define PPC_OPCODE_ALTIVEC 0x200
203 #define PPC_OPCODE_403 0x400
204 #define PPC_OPCODE_BOOKE 0x800
205 #define PPC_OPCODE_BOOKE64 0x1000
206 #define PPC_OPCODE_440 0x2000
207 #define PPC_OPCODE_POWER4 0x4000
208 #define PPC_OPCODE_NOPOWER4 0x8000
209 #define PPC_OPCODE_CLASSIC 0x10000
210 #define PPC_OPCODE_SPE 0x20000
211 #define PPC_OPCODE_ISEL 0x40000
212 #define PPC_OPCODE_EFS 0x80000
213 #define PPC_OPCODE_BRLOCK 0x100000
214 #define PPC_OPCODE_PMR 0x200000
215 #define PPC_OPCODE_CACHELCK 0x400000
216 #define PPC_OPCODE_RFMCI 0x800000
217 #define PPC_OPCODE_POWER5 0x1000000
218 #define PPC_OPCODE_E300 0x2000000
219 #define PPC_OPCODE_POWER6 0x4000000
220 #define PPC_OPCODE_CELL 0x8000000
221 #define PPC_OPCODE_PPCPS 0x10000000
222 #define PPC_OPCODE_E500MC 0x20000000
223 #define PPC_OPCODE_405 0x40000000
224 #define PPC_OPCODE_VSX 0x80000000
225
226 #define PPC_OP(i) (((i) >> 26) & 0x3f)
227
228 struct powerpc_operand
229 {
230 unsigned int bitm;
231 int shift;
232 unsigned long (*insert)
233 (unsigned long, long, int, const char **);
234 unsigned long flags;
235 };
236
237 static const struct powerpc_operand powerpc_operands[];
238
239 #define PPC_OPERAND_SIGNED (0x1)
240 #define PPC_OPERAND_SIGNOPT (0x2)
241 #define PPC_OPERAND_FAKE (0x4)
242 #define PPC_OPERAND_PARENS (0x8)
243 #define PPC_OPERAND_CR (0x10)
244 #define PPC_OPERAND_GPR (0x20)
245 #define PPC_OPERAND_GPR_0 (0x40)
246 #define PPC_OPERAND_FPR (0x80)
247 #define PPC_OPERAND_RELATIVE (0x100)
248 #define PPC_OPERAND_ABSOLUTE (0x200)
249 #define PPC_OPERAND_OPTIONAL (0x400)
250 #define PPC_OPERAND_NEXT (0x800)
251 #define PPC_OPERAND_NEGATIVE (0x1000)
252 #define PPC_OPERAND_VR (0x2000)
253 #define PPC_OPERAND_DS (0x4000)
254 #define PPC_OPERAND_DQ (0x8000)
255 #define PPC_OPERAND_PLUS1 (0x10000)
256 #define PPC_OPERAND_FSL (0x20000)
257 #define PPC_OPERAND_FCR (0x40000)
258 #define PPC_OPERAND_UDI (0x80000)
259 #define PPC_OPERAND_VSR (0x100000)
260
261 /*
262 * END OF ppc.h
263 */
264
265 #define PPC_DEST_ARCH PPC_OPCODE_PPC
266
267 ppc_instruction_t
268 asm_instruction( powerpc_iname_t sname, const int argc, const long int *argv )
269 {
270 const char *errmsg = NULL;
271 const char *name;
272 unsigned long int ret;
273 const struct powerpc_opcode *opcode = NULL;
274 int argi, argj;
275
276 opcode = &powerpc_opcodes[ sname ];
277 name = opcode->name;
278
279 if ( ! opcode ) {
280 printf( "Can't find opcode %d\n", sname );
281 return ASM_ERROR_OPC;
282 }
283 if ( ( opcode->flags & PPC_DEST_ARCH ) != PPC_DEST_ARCH ) {
284 printf( "opcode %s not defined for this arch\n", name );
285 return ASM_ERROR_OPC;
286 }
287
288 ret = opcode->opcode;
289
290 argi = argj = 0;
291 while ( opcode->operands[ argi ] != 0 ) {
292 long int op = 0;
293 const struct powerpc_operand *operand = &powerpc_operands[ opcode->operands[ argi ] ];
294
295 if ( ! (operand->flags & PPC_OPERAND_FAKE) ) {
296 if ( argj >= argc ) {
297 printf( "Not enough arguments for %s, got %d\n", name, argc );
298 return ASM_ERROR_OPC;
299 }
300
301 op = argv[ argj++ ];
302 }
303
304 if ( operand->insert ) {
305 errmsg = NULL;
306 ret = operand->insert( ret, op, PPC_DEST_ARCH, &errmsg );
307 if ( errmsg ) {
308 printf( "%s: error while inserting operand %d (0x%.2lx): %s\n",
309 name, argi, op, errmsg );
310 }
311 } else {
312 unsigned long int opu = *(unsigned long int *)&op;
313 unsigned long int bitm = operand->bitm;
314 unsigned long int bitm_full = bitm | ( bitm & 1 ? 0 : 0xf );
315
316 if ( operand->flags & PPC_OPERAND_SIGNED ) {
317 bitm_full >>= 1;
318
319 if ( ( opu & ~bitm_full ) != 0 && ( opu | bitm_full ) != -1 )
320 printf( "%s: signed operand nr.%d to wide. op: %.8lx, mask: %.8lx\n",
321 name, argi, opu, bitm );
322 } else {
323 if ( ( opu & ~bitm_full ) != 0 )
324 printf( "%s: unsigned operand nr.%d to wide. op: %.8lx, mask: %.8lx\n",
325 name, argi, opu, bitm );
326 }
327 if ( (bitm & 1) == 0 ) {
328 if ( opu & 0xf & ~bitm )
329 printf( "%s: operand nr.%d not aligned correctly. op: %.8lx, mask: %.8lx\n",
330 name, argi, opu, bitm );
331 }
332
333 ret |= ( op & operand->bitm ) << operand->shift;
334 }
335 argi++;
336 }
337 if ( argc > argj ) {
338 printf( "Too many arguments for %s, got %d\n", name, argc );
339 return ASM_ERROR_OPC;
340 }
341
342 return ret;
343 }
344
345
346 /*
347 * BEGIN OF ppc-opc.c
348 */
349
350 #define ATTRIBUTE_UNUSED
351 #define _(x) (x)
352
353 /* Local insertion and extraction functions. */
354
355 static unsigned long insert_bdm (unsigned long, long, int, const char **);
356 static unsigned long insert_bo (unsigned long, long, int, const char **);
357 static unsigned long insert_ras (unsigned long, long, int, const char **);
358 static unsigned long insert_rbs (unsigned long, long, int, const char **);
359
360 /* The operands table.
361
362 The fields are bitm, shift, insert, extract, flags.
363 */
364
365 static const struct powerpc_operand powerpc_operands[] =
366 {
367 /* The zero index is used to indicate the end of the list of
368 operands. */
369 #define UNUSED 0
370 { 0, 0, NULL, 0 },
371
372 /* The BA field in an XL form instruction. */
373 #define BA UNUSED + 1
374 /* The BI field in a B form or XL form instruction. */
375 #define BI BA
376 #define BI_MASK (0x1f << 16)
377 { 0x1f, 16, NULL, PPC_OPERAND_CR },
378
379 /* The BD field in a B form instruction. The lower two bits are
380 forced to zero. */
381 #define BD BA + 1
382 { 0xfffc, 0, NULL, PPC_OPERAND_RELATIVE | PPC_OPERAND_SIGNED },
383
384 /* The BD field in a B form instruction when the - modifier is used.
385 This sets the y bit of the BO field appropriately. */
386 #define BDM BD + 1
387 { 0xfffc, 0, insert_bdm,
388 PPC_OPERAND_RELATIVE | PPC_OPERAND_SIGNED },
389
390 /* The BF field in an X or XL form instruction. */
391 #define BF BDM + 1
392 /* The CRFD field in an X form instruction. */
393 #define CRFD BF
394 { 0x7, 23, NULL, PPC_OPERAND_CR },
395
396 /* An optional BF field. This is used for comparison instructions,
397 in which an omitted BF field is taken as zero. */
398 #define OBF BF + 1
399 { 0x7, 23, NULL, PPC_OPERAND_CR | PPC_OPERAND_OPTIONAL },
400
401 /* The BO field in a B form instruction. Certain values are
402 illegal. */
403 #define BO OBF + 1
404 #define BO_MASK (0x1f << 21)
405 { 0x1f, 21, insert_bo, 0 },
406
407 /* The condition register number portion of the BI field in a B form
408 or XL form instruction. This is used for the extended
409 conditional branch mnemonics, which set the lower two bits of the
410 BI field. This field is optional. */
411 #define CR BO + 1
412 { 0x7, 18, NULL, PPC_OPERAND_CR | PPC_OPERAND_OPTIONAL },
413
414 /* The D field in a D form instruction. This is a displacement off
415 a register, and implies that the next operand is a register in
416 parentheses. */
417 #define D CR + 1
418 { 0xffff, 0, NULL, PPC_OPERAND_PARENS | PPC_OPERAND_SIGNED },
419
420 /* The DS field in a DS form instruction. This is like D, but the
421 lower two bits are forced to zero. */
422 #define DS D + 1
423 { 0xfffc, 0, NULL,
424 PPC_OPERAND_PARENS | PPC_OPERAND_SIGNED | PPC_OPERAND_DS },
425
426 /* The FRA field in an X or A form instruction. */
427 #define FRA DS + 1
428 #define FRA_MASK (0x1f << 16)
429 { 0x1f, 16, NULL, PPC_OPERAND_FPR },
430
431 /* The FRB field in an X or A form instruction. */
432 #define FRB FRA + 1
433 #define FRB_MASK (0x1f << 11)
434 { 0x1f, 11, NULL, PPC_OPERAND_FPR },
435
436 /* The FRC field in an A form instruction. */
437 #define FRC FRB + 1
438 #define FRC_MASK (0x1f << 6)
439 { 0x1f, 6, NULL, PPC_OPERAND_FPR },
440
441 /* The FRS field in an X form instruction or the FRT field in a D, X
442 or A form instruction. */
443 #define FRS FRC + 1
444 #define FRT FRS
445 { 0x1f, 21, NULL, PPC_OPERAND_FPR },
446
447 /* The LI field in an I form instruction. The lower two bits are
448 forced to zero. */
449 #define LI FRS + 1
450 { 0x3fffffc, 0, NULL, PPC_OPERAND_RELATIVE | PPC_OPERAND_SIGNED },
451
452 /* The ME field in an M form instruction. */
453 #define ME LI + 1
454 #define ME_MASK (0x1f << 1)
455 { 0x1f, 1, NULL, 0 },
456
457 /* The MB and ME fields in an M form instruction expressed a single
458 operand which is a bitmask indicating which bits to select. This
459 is a two operand form using PPC_OPERAND_NEXT. See the
460 description in opcode/ppc.h for what this means. */
461 #define MBE ME + 1
462 { 0x1f, 6, NULL, PPC_OPERAND_OPTIONAL | PPC_OPERAND_NEXT },
463
464 /* The RA field in an D, DS, DQ, X, XO, M, or MDS form instruction. */
465 #define RA MBE + 1
466 #define RA_MASK (0x1f << 16)
467 { 0x1f, 16, NULL, PPC_OPERAND_GPR },
468
469 /* As above, but 0 in the RA field means zero, not r0. */
470 #define RA0 RA + 1
471 { 0x1f, 16, NULL, PPC_OPERAND_GPR_0 },
472
473 /* The RA field in a D or X form instruction which is an updating
474 store or an updating floating point load, which means that the RA
475 field may not be zero. */
476 #define RAS RA0 + 1
477 { 0x1f, 16, insert_ras, PPC_OPERAND_GPR_0 },
478
479 /* The RB field in an X, XO, M, or MDS form instruction. */
480 #define RB RAS + 1
481 #define RB_MASK (0x1f << 11)
482 { 0x1f, 11, NULL, PPC_OPERAND_GPR },
483
484 /* The RB field in an X form instruction when it must be the same as
485 the RS field in the instruction. This is used for extended
486 mnemonics like mr. */
487 #define RBS RB + 1
488 { 0x1f, 11, insert_rbs, PPC_OPERAND_FAKE },
489
490 /* The RS field in a D, DS, X, XFX, XS, M, MD or MDS form
491 instruction or the RT field in a D, DS, X, XFX or XO form
492 instruction. */
493 #define RS RBS + 1
494 #define RT RS
495 #define RT_MASK (0x1f << 21)
496 { 0x1f, 21, NULL, PPC_OPERAND_GPR },
497
498 /* The SH field in an X or M form instruction. */
499 #define SH RS + 1
500 #define SH_MASK (0x1f << 11)
501 /* The other UIMM field in an EVX form instruction. */
502 #define EVUIMM SH
503 { 0x1f, 11, NULL, 0 },
504
505 /* The SI field in a D form instruction. */
506 #define SI SH + 1
507 { 0xffff, 0, NULL, PPC_OPERAND_SIGNED },
508
509 /* The UI field in a D form instruction. */
510 #define UI SI + 1
511 { 0xffff, 0, NULL, 0 },
512
513 };
514
515
516 /* The functions used to insert and extract complicated operands. */
517
518 /* The BD field in a B form instruction when the - modifier is used.
519 This modifier means that the branch is not expected to be taken.
520 For chips built to versions of the architecture prior to version 2
521 (ie. not Power4 compatible), we set the y bit of the BO field to 1
522 if the offset is negative. When extracting, we require that the y
523 bit be 1 and that the offset be positive, since if the y bit is 0
524 we just want to print the normal form of the instruction.
525 Power4 compatible targets use two bits, "a", and "t", instead of
526 the "y" bit. "at" == 00 => no hint, "at" == 01 => unpredictable,
527 "at" == 10 => not taken, "at" == 11 => taken. The "t" bit is 00001
528 in BO field, the "a" bit is 00010 for branch on CR(BI) and 01000
529 for branch on CTR. We only handle the taken/not-taken hint here.
530 Note that we don't relax the conditions tested here when
531 disassembling with -Many because insns using extract_bdm and
532 extract_bdp always occur in pairs. One or the other will always
533 be valid. */
534
535 static unsigned long
536 insert_bdm (unsigned long insn,
537 long value,
538 int dialect,
539 const char **errmsg ATTRIBUTE_UNUSED)
540 {
541 if ((dialect & PPC_OPCODE_POWER4) == 0)
542 {
543 if ((value & 0x8000) != 0)
544 insn |= 1 << 21;
545 }
546 else
547 {
548 if ((insn & (0x14 << 21)) == (0x04 << 21))
549 insn |= 0x02 << 21;
550 else if ((insn & (0x14 << 21)) == (0x10 << 21))
551 insn |= 0x08 << 21;
552 }
553 return insn | (value & 0xfffc);
554 }
555
556
557 /* Check for legal values of a BO field. */
558
559 static int
560 valid_bo (long value, int dialect, int extract)
561 {
562 if ((dialect & PPC_OPCODE_POWER4) == 0)
563 {
564 int valid;
565 /* Certain encodings have bits that are required to be zero.
566 These are (z must be zero, y may be anything):
567 001zy
568 011zy
569 1z00y
570 1z01y
571 1z1zz
572 */
573 switch (value & 0x14)
574 {
575 default:
576 case 0:
577 valid = 1;
578 break;
579 case 0x4:
580 valid = (value & 0x2) == 0;
581 break;
582 case 0x10:
583 valid = (value & 0x8) == 0;
584 break;
585 case 0x14:
586 valid = value == 0x14;
587 break;
588 }
589 /* When disassembling with -Many, accept power4 encodings too. */
590 if (valid
591 || (dialect & PPC_OPCODE_ANY) == 0
592 || !extract)
593 return valid;
594 }
595
596 /* Certain encodings have bits that are required to be zero.
597 These are (z must be zero, a & t may be anything):
598 0000z
599 0001z
600 0100z
601 0101z
602 001at
603 011at
604 1a00t
605 1a01t
606 1z1zz
607 */
608 if ((value & 0x14) == 0)
609 return (value & 0x1) == 0;
610 else if ((value & 0x14) == 0x14)
611 return value == 0x14;
612 else
613 return 1;
614 }
615
616 /* The BO field in a B form instruction. Warn about attempts to set
617 the field to an illegal value. */
618
619 static unsigned long
620 insert_bo (unsigned long insn,
621 long value,
622 int dialect,
623 const char **errmsg)
624 {
625 if (!valid_bo (value, dialect, 0))
626 *errmsg = _("invalid conditional option");
627 return insn | ((value & 0x1f) << 21);
628 }
629
630 /* The RA field in a D or X form instruction which is an updating
631 store or an updating floating point load, which means that the RA
632 field may not be zero. */
633
634 static unsigned long
635 insert_ras (unsigned long insn,
636 long value,
637 int dialect ATTRIBUTE_UNUSED,
638 const char **errmsg)
639 {
640 if (value == 0)
641 *errmsg = _("invalid register operand when updating");
642 return insn | ((value & 0x1f) << 16);
643 }
644
645 /* The RB field in an X form instruction when it must be the same as
646 the RS field in the instruction. This is used for extended
647 mnemonics like mr. This operand is marked FAKE. The insertion
648 function just copies the BT field into the BA field, and the
649 extraction function just checks that the fields are the same. */
650
651 static unsigned long
652 insert_rbs (unsigned long insn,
653 long value ATTRIBUTE_UNUSED,
654 int dialect ATTRIBUTE_UNUSED,
655 const char **errmsg ATTRIBUTE_UNUSED)
656 {
657 return insn | (((insn >> 21) & 0x1f) << 11);
658 }
659
660
661 /* Macros used to form opcodes. */
662
663 /* The main opcode. */
664 #define OP(x) ((((unsigned long)(x)) & 0x3f) << 26)
665 #define OP_MASK OP (0x3f)
666
667 /* The main opcode combined with a trap code in the TO field of a D
668 form instruction. Used for extended mnemonics for the trap
669 instructions. */
670 #define OPTO(x,to) (OP (x) | ((((unsigned long)(to)) & 0x1f) << 21))
671 #define OPTO_MASK (OP_MASK | TO_MASK)
672
673 /* The main opcode combined with a comparison size bit in the L field
674 of a D form or X form instruction. Used for extended mnemonics for
675 the comparison instructions. */
676 #define OPL(x,l) (OP (x) | ((((unsigned long)(l)) & 1) << 21))
677 #define OPL_MASK OPL (0x3f,1)
678
679 /* An A form instruction. */
680 #define A(op, xop, rc) (OP (op) | ((((unsigned long)(xop)) & 0x1f) << 1) | (((unsigned long)(rc)) & 1))
681 #define A_MASK A (0x3f, 0x1f, 1)
682
683 /* An A_MASK with the FRB field fixed. */
684 #define AFRB_MASK (A_MASK | FRB_MASK)
685
686 /* An A_MASK with the FRC field fixed. */
687 #define AFRC_MASK (A_MASK | FRC_MASK)
688
689 /* An A_MASK with the FRA and FRC fields fixed. */
690 #define AFRAFRC_MASK (A_MASK | FRA_MASK | FRC_MASK)
691
692 /* An AFRAFRC_MASK, but with L bit clear. */
693 #define AFRALFRC_MASK (AFRAFRC_MASK & ~((unsigned long) 1 << 16))
694
695 /* A B form instruction. */
696 #define B(op, aa, lk) (OP (op) | ((((unsigned long)(aa)) & 1) << 1) | ((lk) & 1))
697 #define B_MASK B (0x3f, 1, 1)
698
699 /* A B form instruction setting the BO field. */
700 #define BBO(op, bo, aa, lk) (B ((op), (aa), (lk)) | ((((unsigned long)(bo)) & 0x1f) << 21))
701 #define BBO_MASK BBO (0x3f, 0x1f, 1, 1)
702
703 /* A BBO_MASK with the y bit of the BO field removed. This permits
704 matching a conditional branch regardless of the setting of the y
705 bit. Similarly for the 'at' bits used for power4 branch hints. */
706 #define Y_MASK (((unsigned long) 1) << 21)
707 #define AT1_MASK (((unsigned long) 3) << 21)
708 #define AT2_MASK (((unsigned long) 9) << 21)
709 #define BBOY_MASK (BBO_MASK &~ Y_MASK)
710 #define BBOAT_MASK (BBO_MASK &~ AT1_MASK)
711
712 /* A B form instruction setting the BO field and the condition bits of
713 the BI field. */
714 #define BBOCB(op, bo, cb, aa, lk) \
715 (BBO ((op), (bo), (aa), (lk)) | ((((unsigned long)(cb)) & 0x3) << 16))
716 #define BBOCB_MASK BBOCB (0x3f, 0x1f, 0x3, 1, 1)
717
718 /* A BBOCB_MASK with the y bit of the BO field removed. */
719 #define BBOYCB_MASK (BBOCB_MASK &~ Y_MASK)
720 #define BBOATCB_MASK (BBOCB_MASK &~ AT1_MASK)
721 #define BBOAT2CB_MASK (BBOCB_MASK &~ AT2_MASK)
722
723 /* A BBOYCB_MASK in which the BI field is fixed. */
724 #define BBOYBI_MASK (BBOYCB_MASK | BI_MASK)
725 #define BBOATBI_MASK (BBOAT2CB_MASK | BI_MASK)
726
727 /* An Context form instruction. */
728 #define CTX(op, xop) (OP (op) | (((unsigned long)(xop)) & 0x7))
729 #define CTX_MASK CTX(0x3f, 0x7)
730
731 /* An User Context form instruction. */
732 #define UCTX(op, xop) (OP (op) | (((unsigned long)(xop)) & 0x1f))
733 #define UCTX_MASK UCTX(0x3f, 0x1f)
734
735 /* The main opcode mask with the RA field clear. */
736 #define DRA_MASK (OP_MASK | RA_MASK)
737
738 /* A DS form instruction. */
739 #define DSO(op, xop) (OP (op) | ((xop) & 0x3))
740 #define DS_MASK DSO (0x3f, 3)
741
742 /* A DE form instruction. */
743 #define DEO(op, xop) (OP (op) | ((xop) & 0xf))
744 #define DE_MASK DEO (0x3e, 0xf)
745
746 /* An EVSEL form instruction. */
747 #define EVSEL(op, xop) (OP (op) | (((unsigned long)(xop)) & 0xff) << 3)
748 #define EVSEL_MASK EVSEL(0x3f, 0xff)
749
750 /* An M form instruction. */
751 #define M(op, rc) (OP (op) | ((rc) & 1))
752 #define M_MASK M (0x3f, 1)
753
754 /* An M form instruction with the ME field specified. */
755 #define MME(op, me, rc) (M ((op), (rc)) | ((((unsigned long)(me)) & 0x1f) << 1))
756
757 /* An M_MASK with the MB and ME fields fixed. */
758 #define MMBME_MASK (M_MASK | MB_MASK | ME_MASK)
759
760 /* An M_MASK with the SH and ME fields fixed. */
761 #define MSHME_MASK (M_MASK | SH_MASK | ME_MASK)
762
763 /* An MD form instruction. */
764 #define MD(op, xop, rc) (OP (op) | ((((unsigned long)(xop)) & 0x7) << 2) | ((rc) & 1))
765 #define MD_MASK MD (0x3f, 0x7, 1)
766
767 /* An MD_MASK with the MB field fixed. */
768 #define MDMB_MASK (MD_MASK | MB6_MASK)
769
770 /* An MD_MASK with the SH field fixed. */
771 #define MDSH_MASK (MD_MASK | SH6_MASK)
772
773 /* An MDS form instruction. */
774 #define MDS(op, xop, rc) (OP (op) | ((((unsigned long)(xop)) & 0xf) << 1) | ((rc) & 1))
775 #define MDS_MASK MDS (0x3f, 0xf, 1)
776
777 /* An MDS_MASK with the MB field fixed. */
778 #define MDSMB_MASK (MDS_MASK | MB6_MASK)
779
780 /* An SC form instruction. */
781 #define SC(op, sa, lk) (OP (op) | ((((unsigned long)(sa)) & 1) << 1) | ((lk) & 1))
782 #define SC_MASK (OP_MASK | (((unsigned long)0x3ff) << 16) | (((unsigned long)1) << 1) | 1)
783
784 /* A VX form instruction. */
785 #define VX(op, xop) (OP (op) | (((unsigned long)(xop)) & 0x7ff))
786
787 /* The mask for a VX form instruction. */
788 #define VX_MASK VX(0x3f, 0x7ff)
789
790 /* A VA form instruction. */
791 #define VXA(op, xop) (OP (op) | (((unsigned long)(xop)) & 0x03f))
792
793 /* The mask for a VA form instruction. */
794 #define VXA_MASK VXA(0x3f, 0x3f)
795
796 /* A VXR form instruction. */
797 #define VXR(op, xop, rc) (OP (op) | (((rc) & 1) << 10) | (((unsigned long)(xop)) & 0x3ff))
798
799 /* The mask for a VXR form instruction. */
800 #define VXR_MASK VXR(0x3f, 0x3ff, 1)
801
802 /* An X form instruction. */
803 #define X(op, xop) (OP (op) | ((((unsigned long)(xop)) & 0x3ff) << 1))
804
805 /* A Z form instruction. */
806 #define Z(op, xop) (OP (op) | ((((unsigned long)(xop)) & 0x1ff) << 1))
807
808 /* An X form instruction with the RC bit specified. */
809 #define XRC(op, xop, rc) (X ((op), (xop)) | ((rc) & 1))
810
811 /* A Z form instruction with the RC bit specified. */
812 #define ZRC(op, xop, rc) (Z ((op), (xop)) | ((rc) & 1))
813
814 /* The mask for an X form instruction. */
815 #define X_MASK XRC (0x3f, 0x3ff, 1)
816
817 /* The mask for a Z form instruction. */
818 #define Z_MASK ZRC (0x3f, 0x1ff, 1)
819 #define Z2_MASK ZRC (0x3f, 0xff, 1)
820
821 /* An X_MASK with the RA field fixed. */
822 #define XRA_MASK (X_MASK | RA_MASK)
823
824 /* An XRA_MASK with the W field clear. */
825 #define XWRA_MASK (XRA_MASK & ~((unsigned long) 1 << 16))
826
827 /* An X_MASK with the RB field fixed. */
828 #define XRB_MASK (X_MASK | RB_MASK)
829
830 /* An X_MASK with the RT field fixed. */
831 #define XRT_MASK (X_MASK | RT_MASK)
832
833 /* An XRT_MASK mask with the L bits clear. */
834 #define XLRT_MASK (XRT_MASK & ~((unsigned long) 0x3 << 21))
835
836 /* An X_MASK with the RA and RB fields fixed. */
837 #define XRARB_MASK (X_MASK | RA_MASK | RB_MASK)
838
839 /* An XRARB_MASK, but with the L bit clear. */
840 #define XRLARB_MASK (XRARB_MASK & ~((unsigned long) 1 << 16))
841
842 /* An X_MASK with the RT and RA fields fixed. */
843 #define XRTRA_MASK (X_MASK | RT_MASK | RA_MASK)
844
845 /* An XRTRA_MASK, but with L bit clear. */
846 #define XRTLRA_MASK (XRTRA_MASK & ~((unsigned long) 1 << 21))
847
848 /* An X form instruction with the L bit specified. */
849 #define XOPL(op, xop, l) (X ((op), (xop)) | ((((unsigned long)(l)) & 1) << 21))
850
851 /* The mask for an X form comparison instruction. */
852 #define XCMP_MASK (X_MASK | (((unsigned long)1) << 22))
853
854 /* The mask for an X form comparison instruction with the L field
855 fixed. */
856 #define XCMPL_MASK (XCMP_MASK | (((unsigned long)1) << 21))
857
858 /* An X form trap instruction with the TO field specified. */
859 #define XTO(op, xop, to) (X ((op), (xop)) | ((((unsigned long)(to)) & 0x1f) << 21))
860 #define XTO_MASK (X_MASK | TO_MASK)
861
862 /* An X form tlb instruction with the SH field specified. */
863 #define XTLB(op, xop, sh) (X ((op), (xop)) | ((((unsigned long)(sh)) & 0x1f) << 11))
864 #define XTLB_MASK (X_MASK | SH_MASK)
865
866 /* An X form sync instruction. */
867 #define XSYNC(op, xop, l) (X ((op), (xop)) | ((((unsigned long)(l)) & 3) << 21))
868
869 /* An X form sync instruction with everything filled in except the LS field. */
870 #define XSYNC_MASK (0xff9fffff)
871
872 /* An X_MASK, but with the EH bit clear. */
873 #define XEH_MASK (X_MASK & ~((unsigned long )1))
874
875 /* An X form AltiVec dss instruction. */
876 #define XDSS(op, xop, a) (X ((op), (xop)) | ((((unsigned long)(a)) & 1) << 25))
877 #define XDSS_MASK XDSS(0x3f, 0x3ff, 1)
878
879 /* An XFL form instruction. */
880 #define XFL(op, xop, rc) (OP (op) | ((((unsigned long)(xop)) & 0x3ff) << 1) | (((unsigned long)(rc)) & 1))
881 #define XFL_MASK XFL (0x3f, 0x3ff, 1)
882
883 /* An X form isel instruction. */
884 #define XISEL(op, xop) (OP (op) | ((((unsigned long)(xop)) & 0x1f) << 1))
885 #define XISEL_MASK XISEL(0x3f, 0x1f)
886
887 /* An XL form instruction with the LK field set to 0. */
888 #define XL(op, xop) (OP (op) | ((((unsigned long)(xop)) & 0x3ff) << 1))
889
890 /* An XL form instruction which uses the LK field. */
891 #define XLLK(op, xop, lk) (XL ((op), (xop)) | ((lk) & 1))
892
893 /* The mask for an XL form instruction. */
894 #define XL_MASK XLLK (0x3f, 0x3ff, 1)
895
896 /* An XL form instruction which explicitly sets the BO field. */
897 #define XLO(op, bo, xop, lk) \
898 (XLLK ((op), (xop), (lk)) | ((((unsigned long)(bo)) & 0x1f) << 21))
899 #define XLO_MASK (XL_MASK | BO_MASK)
900
901 /* An XL form instruction which explicitly sets the y bit of the BO
902 field. */
903 #define XLYLK(op, xop, y, lk) (XLLK ((op), (xop), (lk)) | ((((unsigned long)(y)) & 1) << 21))
904 #define XLYLK_MASK (XL_MASK | Y_MASK)
905
906 /* An XL form instruction which sets the BO field and the condition
907 bits of the BI field. */
908 #define XLOCB(op, bo, cb, xop, lk) \
909 (XLO ((op), (bo), (xop), (lk)) | ((((unsigned long)(cb)) & 3) << 16))
910 #define XLOCB_MASK XLOCB (0x3f, 0x1f, 0x3, 0x3ff, 1)
911
912 #define BB_MASK (0x1f << 11)
913 /* An XL_MASK or XLYLK_MASK or XLOCB_MASK with the BB field fixed. */
914 #define XLBB_MASK (XL_MASK | BB_MASK)
915 #define XLYBB_MASK (XLYLK_MASK | BB_MASK)
916 #define XLBOCBBB_MASK (XLOCB_MASK | BB_MASK)
917
918 /* A mask for branch instructions using the BH field. */
919 #define XLBH_MASK (XL_MASK | (0x1c << 11))
920
921 /* An XL_MASK with the BO and BB fields fixed. */
922 #define XLBOBB_MASK (XL_MASK | BO_MASK | BB_MASK)
923
924 /* An XL_MASK with the BO, BI and BB fields fixed. */
925 #define XLBOBIBB_MASK (XL_MASK | BO_MASK | BI_MASK | BB_MASK)
926
927 /* An XO form instruction. */
928 #define XO(op, xop, oe, rc) \
929 (OP (op) | ((((unsigned long)(xop)) & 0x1ff) << 1) | ((((unsigned long)(oe)) & 1) << 10) | (((unsigned long)(rc)) & 1))
930 #define XO_MASK XO (0x3f, 0x1ff, 1, 1)
931
932 /* An XO_MASK with the RB field fixed. */
933 #define XORB_MASK (XO_MASK | RB_MASK)
934
935 /* An XS form instruction. */
936 #define XS(op, xop, rc) (OP (op) | ((((unsigned long)(xop)) & 0x1ff) << 2) | (((unsigned long)(rc)) & 1))
937 #define XS_MASK XS (0x3f, 0x1ff, 1)
938
939 /* A mask for the FXM version of an XFX form instruction. */
940 #define XFXFXM_MASK (X_MASK | (1 << 11) | (1 << 20))
941
942 /* An XFX form instruction with the FXM field filled in. */
943 #define XFXM(op, xop, fxm, p4) \
944 (X ((op), (xop)) | ((((unsigned long)(fxm)) & 0xff) << 12) \
945 | ((unsigned long)(p4) << 20))
946
947 #define SPR_MASK (0x3ff << 11)
948 /* An XFX form instruction with the SPR field filled in. */
949 #define XSPR(op, xop, spr) \
950 (X ((op), (xop)) | ((((unsigned long)(spr)) & 0x1f) << 16) | ((((unsigned long)(spr)) & 0x3e0) << 6))
951 #define XSPR_MASK (X_MASK | SPR_MASK)
952
953 /* An XFX form instruction with the SPR field filled in except for the
954 SPRBAT field. */
955 #define XSPRBAT_MASK (XSPR_MASK &~ SPRBAT_MASK)
956
957 /* An XFX form instruction with the SPR field filled in except for the
958 SPRG field. */
959 #define XSPRG_MASK (XSPR_MASK & ~(0x1f << 16))
960
961 /* An X form instruction with everything filled in except the E field. */
962 #define XE_MASK (0xffff7fff)
963
964 /* An X form user context instruction. */
965 #define XUC(op, xop) (OP (op) | (((unsigned long)(xop)) & 0x1f))
966 #define XUC_MASK XUC(0x3f, 0x1f)
967
968 /* The BO encodings used in extended conditional branch mnemonics. */
969 #define BODNZF (0x0)
970 #define BODNZFP (0x1)
971 #define BODZF (0x2)
972 #define BODZFP (0x3)
973 #define BODNZT (0x8)
974 #define BODNZTP (0x9)
975 #define BODZT (0xa)
976 #define BODZTP (0xb)
977
978 #define BOF (0x4)
979 #define BOFP (0x5)
980 #define BOFM4 (0x6)
981 #define BOFP4 (0x7)
982 #define BOT (0xc)
983 #define BOTP (0xd)
984 #define BOTM4 (0xe)
985 #define BOTP4 (0xf)
986
987 #define BODNZ (0x10)
988 #define BODNZP (0x11)
989 #define BODZ (0x12)
990 #define BODZP (0x13)
991 #define BODNZM4 (0x18)
992 #define BODNZP4 (0x19)
993 #define BODZM4 (0x1a)
994 #define BODZP4 (0x1b)
995
996 #define BOU (0x14)
997
998 /* The BI condition bit encodings used in extended conditional branch
999 mnemonics. */
1000 #define CBLT (0)
1001 #define CBGT (1)
1002 #define CBEQ (2)
1003 #define CBSO (3)
1004
1005 /* The TO encodings used in extended trap mnemonics. */
1006 #define TOLGT (0x1)
1007 #define TOLLT (0x2)
1008 #define TOEQ (0x4)
1009 #define TOLGE (0x5)
1010 #define TOLNL (0x5)
1011 #define TOLLE (0x6)
1012 #define TOLNG (0x6)
1013 #define TOGT (0x8)
1014 #define TOGE (0xc)
1015 #define TONL (0xc)
1016 #define TOLT (0x10)
1017 #define TOLE (0x14)
1018 #define TONG (0x14)
1019 #define TONE (0x18)
1020 #define TOU (0x1f)
1021
1022 /* Smaller names for the flags so each entry in the opcodes table will
1023 fit on a single line. */
1024 #undef PPC
1025 #define PPC PPC_OPCODE_PPC
1026 #define PPCCOM PPC_OPCODE_PPC | PPC_OPCODE_COMMON
1027 #define PPC64 PPC_OPCODE_64 | PPC_OPCODE_PPC
1028 #define COM PPC_OPCODE_POWER | PPC_OPCODE_PPC | PPC_OPCODE_COMMON
1029 #define COM32 PPC_OPCODE_POWER | PPC_OPCODE_PPC | PPC_OPCODE_COMMON | PPC_OPCODE_32
1030
1031 /* The opcode table.
1032
1033 The format of the opcode table is:
1034
1035 NAME OPCODE MASK FLAGS { OPERANDS }
1036
1037 NAME is the name of the instruction.
1038 OPCODE is the instruction opcode.
1039 MASK is the opcode mask; this is used to tell the disassembler
1040 which bits in the actual opcode must match OPCODE.
1041 FLAGS are flags indicated what processors support the instruction.
1042 OPERANDS is the list of operands.
1043
1044 The disassembler reads the table in order and prints the first
1045 instruction which matches, so this table is sorted to put more
1046 specific instructions before more general instructions. It is also
1047 sorted by major opcode. */
1048
1049 static const struct powerpc_opcode powerpc_opcodes[] = {
1050
1051 { "cmplwi", OPL(10,0), OPL_MASK, PPCCOM, { OBF, RA, UI } },
1052 { "cmpwi", OPL(11,0), OPL_MASK, PPCCOM, { OBF, RA, SI } },
1053 { "cmpw", XOPL(31,0,0), XCMPL_MASK, PPCCOM, { OBF, RA, RB } },
1054 { "cmplw", XOPL(31,32,0), XCMPL_MASK, PPCCOM, { OBF, RA, RB } },
1055 { "fcmpu", X(63,0), X_MASK|(3<<21), COM, { BF, FRA, FRB } },
1056
1057 { "li", OP(14), DRA_MASK, PPCCOM, { RT, SI } },
1058 { "lis", OP(15), DRA_MASK, PPCCOM, { RT, SI } },
1059
1060 { "addi", OP(14), OP_MASK, PPCCOM, { RT, RA0, SI } },
1061 { "addis", OP(15), OP_MASK, PPCCOM, { RT,RA0,SI } },
1062 { "blt-", BBOCB(16,BOT,CBLT,0,0), BBOATCB_MASK, PPCCOM, { CR, BDM } },
1063 { "bc", B(16,0,0), B_MASK, COM, { BO, BI, BD } },
1064 { "bcl", B(16,0,1), B_MASK, COM, { BO, BI, BD } },
1065 { "b", B(18,0,0), B_MASK, COM, { LI } },
1066 { "bl", B(18,0,1), B_MASK, COM, { LI } },
1067 { "blr", XLO(19,BOU,16,0), XLBOBIBB_MASK, PPCCOM, { 0 } },
1068 { "bctr", XLO(19,BOU,528,0), XLBOBIBB_MASK, COM, { 0 } },
1069 { "bctrl", XLO(19,BOU,528,1), XLBOBIBB_MASK, COM, { 0 } },
1070
1071 { "rlwinm", M(21,0), M_MASK, PPCCOM, { RA,RS,SH,MBE,ME } },
1072 { "nop", OP(24), 0xffffffff, PPCCOM, { 0 } },
1073 { "ori", OP(24), OP_MASK, PPCCOM, { RA, RS, UI } },
1074 { "xoris", OP(27), OP_MASK, PPCCOM, { RA, RS, UI } },
1075 { "ldx", X(31,21), X_MASK, PPC64, { RT, RA0, RB } },
1076 { "lwzx", X(31,23), X_MASK, PPCCOM, { RT, RA0, RB } },
1077 { "slw", XRC(31,24,0), X_MASK, PPCCOM, { RA, RS, RB } },
1078 { "and", XRC(31,28,0), X_MASK, COM, { RA, RS, RB } },
1079 { "sub", XO(31,40,0,0), XO_MASK, PPC, { RT, RB, RA } },
1080 { "lbzx", X(31,87), X_MASK, COM, { RT, RA0, RB } },
1081 { "neg", XO(31,104,0,0), XORB_MASK, COM, { RT, RA } },
1082 { "not", XRC(31,124,0), X_MASK, COM, { RA, RS, RBS } },
1083 { "stwx", X(31,151), X_MASK, PPCCOM, { RS, RA0, RB } },
1084 { "stbx", X(31,215), X_MASK, COM, { RS, RA0, RB } },
1085 { "mullw", XO(31,235,0,0), XO_MASK, PPCCOM, { RT, RA, RB } },
1086 { "add", XO(31,266,0,0), XO_MASK, PPCCOM, { RT, RA, RB } },
1087 { "lhzx", X(31,279), X_MASK, COM, { RT, RA0, RB } },
1088 { "xor", XRC(31,316,0), X_MASK, COM, { RA, RS, RB } },
1089 { "mflr", XSPR(31,339,8), XSPR_MASK, COM, { RT } },
1090 { "sthx", X(31,407), X_MASK, COM, { RS, RA0, RB } },
1091 { "mr", XRC(31,444,0), X_MASK, COM, { RA, RS, RBS } },
1092 { "or", XRC(31,444,0), X_MASK, COM, { RA, RS, RB } },
1093 { "divwu", XO(31,459,0,0), XO_MASK, PPC, { RT, RA, RB } },
1094 { "mtlr", XSPR(31,467,8), XSPR_MASK, COM, { RS } },
1095 { "mtctr", XSPR(31,467,9), XSPR_MASK, COM, { RS } },
1096 { "divw", XO(31,491,0,0), XO_MASK, PPC, { RT, RA, RB } },
1097 { "lfsx", X(31,535), X_MASK, COM, { FRT, RA0, RB } },
1098 { "srw", XRC(31,536,0), X_MASK, PPCCOM, { RA, RS, RB } },
1099 { "stfsx", X(31,663), X_MASK, COM, { FRS, RA0, RB } },
1100 { "sraw", XRC(31,792,0), X_MASK, PPCCOM, { RA, RS, RB } },
1101 { "extsh", XRC(31,922,0), XRB_MASK, PPCCOM, { RA, RS } },
1102 { "extsb", XRC(31,954,0), XRB_MASK, PPC, { RA, RS} },
1103
1104 { "lwz", OP(32), OP_MASK, PPCCOM, { RT, D, RA0 } },
1105 { "lbz", OP(34), OP_MASK, COM, { RT, D, RA0 } },
1106 { "stw", OP(36), OP_MASK, PPCCOM, { RS, D, RA0 } },
1107 { "stwu", OP(37), OP_MASK, PPCCOM, { RS, D, RAS } },
1108 { "stb", OP(38), OP_MASK, COM, { RS, D, RA0 } },
1109 { "lhz", OP(40), OP_MASK, COM, { RT, D, RA0 } },
1110 { "sth", OP(44), OP_MASK, COM, { RS, D, RA0 } },
1111 { "lfs", OP(48), OP_MASK, COM, { FRT, D, RA0 } },
1112 { "lfd", OP(50), OP_MASK, COM, { FRT, D, RA0 } },
1113 { "stfs", OP(52), OP_MASK, COM, { FRS, D, RA0 } },
1114 { "stfd", OP(54), OP_MASK, COM, { FRS, D, RA0 } },
1115 { "ld", DSO(58,0), DS_MASK, PPC64, { RT, DS, RA0 } },
1116
1117 { "fdivs", A(59,18,0), AFRC_MASK, PPC, { FRT, FRA, FRB } },
1118 { "fsubs", A(59,20,0), AFRC_MASK, PPC, { FRT, FRA, FRB } },
1119 { "fadds", A(59,21,0), AFRC_MASK, PPC, { FRT, FRA, FRB } },
1120 { "fmuls", A(59,25,0), AFRB_MASK, PPC, { FRT, FRA, FRC } },
1121 { "std", DSO(62,0), DS_MASK, PPC64, { RS, DS, RA0 } },
1122 { "stdu", DSO(62,1), DS_MASK, PPC64, { RS, DS, RAS } },
1123 { "frsp", XRC(63,12,0), XRA_MASK, COM, { FRT, FRB } },
1124 { "fctiwz", XRC(63,15,0), XRA_MASK, PPCCOM, { FRT, FRB } },
1125 { "fsub", A(63,20,0), AFRC_MASK, PPCCOM, { FRT, FRA, FRB } },
1126 { "fneg", XRC(63,40,0), XRA_MASK, COM, { FRT, FRB } },
1127 };
1128
1129 /*
1130 * VM_TIMES enables showing information about time spent inside
1131 * and outside generated code
1132 */
1133 //#define VM_TIMES
1134 #ifdef VM_TIMES
1135 #include <sys/times.h>
1136 static clock_t time_outside_vm = 0;
1137 static clock_t time_total_vm = 0;
1138 #endif
1139
1140 /* exit() won't be called but use it because it is marked with noreturn */
1141 #define DIE( reason ) Com_Error( ERR_DROP, "vm_powerpc compiler error: " reason )
1142
1143 /*
1144 * vm_powerpc uses large quantities of memory during compilation,
1145 * Z_Malloc memory may not be enough for some big qvm files
1146 */
1147
1148 //#define VM_SYSTEM_MALLOC
1149 #ifdef VM_SYSTEM_MALLOC
1150 static inline void *
1151 PPC_Malloc( size_t size )
1152 {
1153 void *mem = malloc( size );
1154 if ( ! mem )
1155 DIE( "Not enough memory" );
1156
1157 return mem;
1158 }
1159 # define PPC_Free free
1160 #else
1161 # define PPC_Malloc Z_Malloc
1162 # define PPC_Free Z_Free
1163 #endif
1164
1165 /*
1166 * optimizations:
1167 * - hole: bubble optimization (OP_CONST+instruction)
1168 * - copy: inline OP_BLOCK_COPY for lengths under 16/32 bytes
1169 * - mask: use rlwinm instruction as dataMask
1170 */
1171
1172 #ifdef __OPTIMIZE__
1173 # define OPTIMIZE_HOLE 1
1174 # define OPTIMIZE_COPY 1
1175 # define OPTIMIZE_MASK 1
1176 #else
1177 # define OPTIMIZE_HOLE 0
1178 # define OPTIMIZE_COPY 0
1179 # define OPTIMIZE_MASK 0
1180 #endif
1181
1182 /*
1183 * SUPPORTED TARGETS:
1184 * - Linux 32 bits
1185 * ( http://refspecs.freestandards.org/elf/elfspec_ppc.pdf )
1186 * * LR at r0 + 4
1187 * * Local variable space not needed
1188 * -> store caller safe regs at 16+
1189 *
1190 * - Linux 64 bits (not fully conformant)
1191 * ( http://www.ibm.com/developerworks/linux/library/l-powasm4.html )
1192 * * needs "official procedure descriptors" (only first function has one)
1193 * * LR at r0 + 16
1194 * * local variable space required, min 64 bytes, starts at 48
1195 * -> store caller safe regs at 128+
1196 *
1197 * - OS X 32 bits
1198 * ( http://developer.apple.com/documentation/DeveloperTools/Conceptual/LowLevelABI/Articles/32bitPowerPC.html )
1199 * * LR at r0 + 8
1200 * * local variable space required, min 32 bytes (?), starts at 24
1201 * -> store caller safe regs at 64+
1202 *
1203 * - OS X 64 bits (completely untested)
1204 * ( http://developer.apple.com/documentation/DeveloperTools/Conceptual/LowLevelABI/Articles/64bitPowerPC.html )
1205 * * LR at r0 + 16
1206 * * local variable space required, min 64 bytes (?), starts at 48
1207 * -> store caller safe regs at 128+
1208 */
1209
1210 /* Select Length - first value on 32 bits, second on 64 */
1211 #ifdef __PPC64__
1212 # define SL( a, b ) (b)
1213 #else
1214 # define SL( a, b ) (a)
1215 #endif
1216
1217 /* Select ABI - first for ELF, second for OS X */
1218 #ifdef __ELF__
1219 # define SA( a, b ) (a)
1220 #else
1221 # define SA( a, b ) (b)
1222 #endif
1223
1224 #define ELF32 SL( SA( 1, 0 ), 0 )
1225 #define ELF64 SL( 0, SA( 1, 0 ) )
1226 #define OSX32 SL( SA( 0, 1 ), 0 )
1227 #define OSX64 SL( 0, SA( 0, 1 ) )
1228
1229 /* native length load/store instructions ( L stands for long ) */
1230 #define iSTLU SL( iSTWU, iSTDU )
1231 #define iSTL SL( iSTW, iSTD )
1232 #define iLL SL( iLWZ, iLD )
1233 #define iLLX SL( iLWZX, iLDX )
1234
1235 /* register length */
1236 #define GPRLEN SL( 4, 8 )
1237 #define FPRLEN (8)
1238 /* shift that many bits to obtain value miltiplied by GPRLEN */
1239 #define GPRLEN_SHIFT SL( 2, 3 )
1240
1241 /* Link register position */
1242 #define STACK_LR SL( SA( 4, 8 ), 16 )
1243 /* register save position */
1244 #define STACK_SAVE SL( SA( 16, 64 ), 128 )
1245 /* temporary space, for float<->int exchange */
1246 #define STACK_TEMP SL( SA( 8, 24 ), 48 )
1247 /* red zone temporary space, used instead of STACK_TEMP if stack isn't
1248 * prepared properly */
1249 #define STACK_RTEMP (-16)
1250
1251 #if ELF64
1252 /*
1253 * Official Procedure Descriptor
1254 * we need to prepare one for generated code if we want to call it
1255 * as function
1256 */
1257 typedef struct {
1258 void *function;
1259 void *toc;
1260 void *env;
1261 } opd_t;
1262 #endif
1263
1264
1265 /*
1266 * opcode information table:
1267 * - length of immediate value
1268 * - returned register type
1269 * - required register(s) type
1270 */
1271 #define opImm0 0x0000 /* no immediate */
1272 #define opImm1 0x0001 /* 1 byte immadiate value after opcode */
1273 #define opImm4 0x0002 /* 4 bytes immediate value after opcode */
1274
1275 #define opRet0 0x0000 /* returns nothing */
1276 #define opRetI 0x0004 /* returns integer */
1277 #define opRetF 0x0008 /* returns float */
1278 #define opRetIF (opRetI | opRetF) /* returns integer or float */
1279
1280 #define opArg0 0x0000 /* requires nothing */
1281 #define opArgI 0x0010 /* requires integer(s) */
1282 #define opArgF 0x0020 /* requires float(s) */
1283 #define opArgIF (opArgI | opArgF) /* requires integer or float */
1284
1285 #define opArg2I 0x0040 /* requires second argument, integer */
1286 #define opArg2F 0x0080 /* requires second argument, float */
1287 #define opArg2IF (opArg2I | opArg2F) /* requires second argument, integer or float */
1288
1289 static const unsigned char vm_opInfo[256] =
1290 {
1291 [OP_UNDEF] = opImm0,
1292 [OP_IGNORE] = opImm0,
1293 [OP_BREAK] = opImm0,
1294 [OP_ENTER] = opImm4,
1295 /* OP_LEAVE has to accept floats, they will be converted to ints */
1296 [OP_LEAVE] = opImm4 | opRet0 | opArgIF,
1297 /* only STORE4 and POP use values from OP_CALL,
1298 * no need to convert floats back */
1299 [OP_CALL] = opImm0 | opRetI | opArgI,
1300 [OP_PUSH] = opImm0 | opRetIF,
1301 [OP_POP] = opImm0 | opRet0 | opArgIF,
1302 [OP_CONST] = opImm4 | opRetIF,
1303 [OP_LOCAL] = opImm4 | opRetI,
1304 [OP_JUMP] = opImm0 | opRet0 | opArgI,
1305
1306 [OP_EQ] = opImm4 | opRet0 | opArgI | opArg2I,
1307 [OP_NE] = opImm4 | opRet0 | opArgI | opArg2I,
1308 [OP_LTI] = opImm4 | opRet0 | opArgI | opArg2I,
1309 [OP_LEI] = opImm4 | opRet0 | opArgI | opArg2I,
1310 [OP_GTI] = opImm4 | opRet0 | opArgI | opArg2I,
1311 [OP_GEI] = opImm4 | opRet0 | opArgI | opArg2I,
1312 [OP_LTU] = opImm4 | opRet0 | opArgI | opArg2I,
1313 [OP_LEU] = opImm4 | opRet0 | opArgI | opArg2I,
1314 [OP_GTU] = opImm4 | opRet0 | opArgI | opArg2I,
1315 [OP_GEU] = opImm4 | opRet0 | opArgI | opArg2I,
1316 [OP_EQF] = opImm4 | opRet0 | opArgF | opArg2F,
1317 [OP_NEF] = opImm4 | opRet0 | opArgF | opArg2F,
1318 [OP_LTF] = opImm4 | opRet0 | opArgF | opArg2F,
1319 [OP_LEF] = opImm4 | opRet0 | opArgF | opArg2F,
1320 [OP_GTF] = opImm4 | opRet0 | opArgF | opArg2F,
1321 [OP_GEF] = opImm4 | opRet0 | opArgF | opArg2F,
1322
1323 [OP_LOAD1] = opImm0 | opRetI | opArgI,
1324 [OP_LOAD2] = opImm0 | opRetI | opArgI,
1325 [OP_LOAD4] = opImm0 | opRetIF| opArgI,
1326 [OP_STORE1] = opImm0 | opRet0 | opArgI | opArg2I,
1327 [OP_STORE2] = opImm0 | opRet0 | opArgI | opArg2I,
1328 [OP_STORE4] = opImm0 | opRet0 | opArgIF| opArg2I,
1329 [OP_ARG] = opImm1 | opRet0 | opArgIF,
1330 [OP_BLOCK_COPY] = opImm4 | opRet0 | opArgI | opArg2I,
1331
1332 [OP_SEX8] = opImm0 | opRetI | opArgI,
1333 [OP_SEX16] = opImm0 | opRetI | opArgI,
1334 [OP_NEGI] = opImm0 | opRetI | opArgI,
1335 [OP_ADD] = opImm0 | opRetI | opArgI | opArg2I,
1336 [OP_SUB] = opImm0 | opRetI | opArgI | opArg2I,
1337 [OP_DIVI] = opImm0 | opRetI | opArgI | opArg2I,
1338 [OP_DIVU] = opImm0 | opRetI | opArgI | opArg2I,
1339 [OP_MODI] = opImm0 | opRetI | opArgI | opArg2I,
1340 [OP_MODU] = opImm0 | opRetI | opArgI | opArg2I,
1341 [OP_MULI] = opImm0 | opRetI | opArgI | opArg2I,
1342 [OP_MULU] = opImm0 | opRetI | opArgI | opArg2I,
1343 [OP_BAND] = opImm0 | opRetI | opArgI | opArg2I,
1344 [OP_BOR] = opImm0 | opRetI | opArgI | opArg2I,
1345 [OP_BXOR] = opImm0 | opRetI | opArgI | opArg2I,
1346 [OP_BCOM] = opImm0 | opRetI | opArgI,
1347 [OP_LSH] = opImm0 | opRetI | opArgI | opArg2I,
1348 [OP_RSHI] = opImm0 | opRetI | opArgI | opArg2I,
1349 [OP_RSHU] = opImm0 | opRetI | opArgI | opArg2I,
1350 [OP_NEGF] = opImm0 | opRetF | opArgF,
1351 [OP_ADDF] = opImm0 | opRetF | opArgF | opArg2F,
1352 [OP_SUBF] = opImm0 | opRetF | opArgF | opArg2F,
1353 [OP_DIVF] = opImm0 | opRetF | opArgF | opArg2F,
1354 [OP_MULF] = opImm0 | opRetF | opArgF | opArg2F,
1355 [OP_CVIF] = opImm0 | opRetF | opArgI,
1356 [OP_CVFI] = opImm0 | opRetI | opArgF,
1357 };
1358
1359 /*
1360 * source instruction data
1361 */
1362 typedef struct source_instruction_s source_instruction_t;
1363 struct source_instruction_s {
1364 // opcode
1365 unsigned long int op;
1366
1367 // number of instruction
1368 unsigned long int i_count;
1369
1370 // immediate value (if any)
1371 union {
1372 unsigned int i;
1373 signed int si;
1374 signed short ss[2];
1375 unsigned short us[2];
1376 unsigned char b;
1377 } arg;
1378
1379 // required and returned registers
1380 unsigned char regA1;
1381 unsigned char regA2;
1382 unsigned char regR;
1383 unsigned char regPos;
1384
1385 // next instruction
1386 source_instruction_t *next;
1387 };
1388
1389
1390
1391 /*
1392 * read-only data needed by the generated code
1393 */
1394 typedef struct VM_Data {
1395 // length of this struct + data
1396 size_t dataLength;
1397 // compiled code size (in bytes)
1398 // it only is code size, without the data
1399 size_t codeLength;
1400
1401 // function pointers, no use to waste registers for them
1402 long int (* AsmCall)( int, int );
1403 void (* BlockCopy )( unsigned int, unsigned int, size_t );
1404
1405 // instruction pointers, rarely used so don't waste register
1406 ppc_instruction_t *iPointers;
1407
1408 // data mask for load and store, not used if optimized
1409 unsigned int dataMask;
1410
1411 // fixed number used to convert from integer to float
1412 unsigned int floatBase; // 0x59800004
1413
1414 #if ELF64
1415 // official procedure descriptor
1416 opd_t opd;
1417 #endif
1418
1419 // additional constants, for floating point OP_CONST
1420 // this data has dynamic length, thus '0' here
1421 unsigned int data[0];
1422 } vm_data_t;
1423
1424 #ifdef offsetof
1425 # define VM_Data_Offset( field ) offsetof( vm_data_t, field )
1426 #else
1427 # define OFFSET( structName, field ) \
1428 ( (void *)&(((structName *)NULL)->field) - NULL )
1429 # define VM_Data_Offset( field ) OFFSET( vm_data_t, field )
1430 #endif
1431
1432
1433 /*
1434 * functions used by generated code
1435 */
1436 static long int
1437 VM_AsmCall( int callSyscallInvNum, int callProgramStack )
1438 {
1439 vm_t *savedVM = currentVM;
1440 long int i, ret;
1441 #ifdef VM_TIMES
1442 struct tms start_time, stop_time;
1443 clock_t saved_time = time_outside_vm;
1444 times( &start_time );
1445 #endif
1446
1447 // save the stack to allow recursive VM entry
1448 currentVM->programStack = callProgramStack - 4;
1449
1450 // we need to convert ints to longs on 64bit powerpcs
1451 if ( sizeof( intptr_t ) == sizeof( int ) ) {
1452 intptr_t *argPosition = (intptr_t *)((byte *)currentVM->dataBase + callProgramStack + 4);
1453
1454 // generated code does not invert syscall number
1455 argPosition[ 0 ] = -1 - callSyscallInvNum;
1456
1457 ret = currentVM->systemCall( argPosition );
1458 } else {
1459 intptr_t args[MAX_VMSYSCALL_ARGS];
1460
1461 // generated code does not invert syscall number
1462 args[0] = -1 - callSyscallInvNum;
1463
1464 int *argPosition = (int *)((byte *)currentVM->dataBase + callProgramStack + 4);
1465 for( i = 1; i < ARRAY_LEN(args); i++ )
1466 args[ i ] = argPosition[ i ];
1467
1468 ret = currentVM->systemCall( args );
1469 }
1470
1471 currentVM = savedVM;
1472
1473 #ifdef VM_TIMES
1474 times( &stop_time );
1475 time_outside_vm = saved_time + ( stop_time.tms_utime - start_time.tms_utime );
1476 #endif
1477
1478 return ret;
1479 }
1480
1481 /*
1482 * code-block descriptors
1483 */
1484 typedef struct dest_instruction dest_instruction_t;
1485 typedef struct symbolic_jump symbolic_jump_t;
1486
1487 struct symbolic_jump {
1488 // number of source instruction it has to jump to
1489 unsigned long int jump_to;
1490
1491 // jump condition true/false, (4*cr7+(eq|gt..))
1492 long int bo, bi;
1493
1494 // extensions / modifiers (branch-link)
1495 unsigned long ext;
1496
1497 // dest_instruction referring to this jump
1498 dest_instruction_t *parent;
1499
1500 // next jump
1501 symbolic_jump_t *nextJump;
1502 };
1503
1504 struct dest_instruction {
1505 // position in the output chain
1506 unsigned long int count;
1507
1508 // source instruction number
1509 unsigned long int i_count;
1510
1511 // exact (for instructins), or maximum (for jump) length
1512 unsigned short length;
1513
1514 dest_instruction_t *next;
1515
1516 // if the instruction is a jump than jump will be non NULL
1517 symbolic_jump_t *jump;
1518
1519 // if jump is NULL than all the instructions will be here
1520 ppc_instruction_t code[0];
1521 };
1522
1523 // first and last instruction,
1524 // di_first is a dummy instruction
1525 static dest_instruction_t *di_first = NULL, *di_last = NULL;
1526 // number of instructions
1527 static unsigned long int di_count = 0;
1528 // pointers needed to compute local jumps, those aren't pointers to
1529 // actual instructions, just used to check how long the jump is going
1530 // to be and whether it is positive or negative
1531 static dest_instruction_t **di_pointers = NULL;
1532
1533 // output instructions which does not come from source code
1534 // use false i_count value
1535 #define FALSE_ICOUNT 0xffffffff
1536
1537
1538 /*
1539 * append specified instructions at the end of instruction chain
1540 */
1541 static void
1542 PPC_Append(
1543 dest_instruction_t *di_now,
1544 unsigned long int i_count
1545 )
1546 {
1547 di_now->count = di_count++;
1548 di_now->i_count = i_count;
1549 di_now->next = NULL;
1550
1551 di_last->next = di_now;
1552 di_last = di_now;
1553
1554 if ( i_count != FALSE_ICOUNT ) {
1555 if ( ! di_pointers[ i_count ] )
1556 di_pointers[ i_count ] = di_now;
1557 }
1558 }
1559
1560 /*
1561 * make space for instructions and append
1562 */
1563 static void
1564 PPC_AppendInstructions(
1565 unsigned long int i_count,
1566 size_t num_instructions,
1567 const ppc_instruction_t *is
1568 )
1569 {
1570 if ( num_instructions < 0 )
1571 num_instructions = 0;
1572 size_t iBytes = sizeof( ppc_instruction_t ) * num_instructions;
1573 dest_instruction_t *di_now = PPC_Malloc( sizeof( dest_instruction_t ) + iBytes );
1574
1575 di_now->length = num_instructions;
1576 di_now->jump = NULL;
1577
1578 if ( iBytes > 0 )
1579 memcpy( &(di_now->code[0]), is, iBytes );
1580
1581 PPC_Append( di_now, i_count );
1582 }
1583
1584 /*
1585 * create symbolic jump and append
1586 */
1587 static symbolic_jump_t *sj_first = NULL, *sj_last = NULL;
1588 static void
1589 PPC_PrepareJump(
1590 unsigned long int i_count,
1591 unsigned long int dest,
1592 long int bo,
1593 long int bi,
1594 unsigned long int ext
1595 )
1596 {
1597 dest_instruction_t *di_now = PPC_Malloc( sizeof( dest_instruction_t ) );
1598 symbolic_jump_t *sj = PPC_Malloc( sizeof( symbolic_jump_t ) );
1599
1600 sj->jump_to = dest;
1601 sj->bo = bo;
1602 sj->bi = bi;
1603 sj->ext = ext;
1604 sj->parent = di_now;
1605 sj->nextJump = NULL;
1606
1607 sj_last->nextJump = sj;
1608 sj_last = sj;
1609
1610 di_now->length = (bo == branchAlways ? 1 : 2);
1611 di_now->jump = sj;
1612
1613 PPC_Append( di_now, i_count );
1614 }
1615
1616 /*
1617 * simplyfy instruction emission
1618 */
1619 #define emitStart( i_cnt ) \
1620 unsigned long int i_count = i_cnt; \
1621 size_t num_instructions = 0; \
1622 long int force_emit = 0; \
1623 ppc_instruction_t instructions[50];
1624
1625 #define pushIn( inst ) \
1626 (instructions[ num_instructions++ ] = inst)
1627 #define in( inst, args... ) pushIn( IN( inst, args ) )
1628
1629 #define emitEnd() \
1630 do{ \
1631 if ( num_instructions || force_emit ) \
1632 PPC_AppendInstructions( i_count, num_instructions, instructions );\
1633 num_instructions = 0; \
1634 } while(0)
1635
1636 #define emitJump( dest, bo, bi, ext ) \
1637 do { \
1638 emitEnd(); \
1639 PPC_PrepareJump( i_count, dest, bo, bi, ext ); \
1640 } while(0)
1641
1642
1643 /*
1644 * definitions for creating .data section,
1645 * used in cases where constant float is needed
1646 */
1647 #define LOCAL_DATA_CHUNK 50
1648 typedef struct local_data_s local_data_t;
1649 struct local_data_s {
1650 // number of data in this structure
1651 long int count;
1652
1653 // data placeholder
1654 unsigned int data[ LOCAL_DATA_CHUNK ];
1655
1656 // next chunk, if this one wasn't enough
1657 local_data_t *next;
1658 };
1659
1660 // first data chunk
1661 static local_data_t *data_first = NULL;
1662 // total number of data
1663 static long int data_acc = 0;
1664
1665 /*
1666 * append the data and return its offset
1667 */
1668 static size_t
1669 PPC_PushData( unsigned int datum )
1670 {
1671 local_data_t *d_now = data_first;
1672 long int accumulated = 0;
1673
1674 // check whether we have this one already
1675 do {
1676 long int i;
1677 for ( i = 0; i < d_now->count; i++ ) {
1678 if ( d_now->data[ i ] == datum ) {
1679 accumulated += i;
1680 return VM_Data_Offset( data[ accumulated ] );
1681 }
1682 }
1683 if ( !d_now->next )
1684 break;
1685
1686 accumulated += d_now->count;
1687 d_now = d_now->next;
1688 } while (1);
1689
1690 // not found, need to append
1691 accumulated += d_now->count;
1692
1693 // last chunk is full, create new one
1694 if ( d_now->count >= LOCAL_DATA_CHUNK ) {
1695 d_now->next = PPC_Malloc( sizeof( local_data_t ) );
1696 d_now = d_now->next;
1697 d_now->count = 0;
1698 d_now->next = NULL;
1699 }
1700
1701 d_now->data[ d_now->count ] = datum;
1702 d_now->count += 1;
1703
1704 data_acc = accumulated + 1;
1705
1706 return VM_Data_Offset( data[ accumulated ] );
1707 }
1708
1709 /*
1710 * find leading zeros in dataMask to implement it with
1711 * "rotate and mask" instruction
1712 */
1713 static long int fastMaskHi = 0, fastMaskLo = 31;
1714 static void
1715 PPC_MakeFastMask( int mask )
1716 {
1717 #if defined( __GNUC__ ) && ( __GNUC__ >= 4 || ( __GNUC__ == 3 && __GNUC_MINOR__ >= 4 ) )
1718 /* count leading zeros */
1719 fastMaskHi = __builtin_clz( mask );
1720
1721 /* count trailing zeros */
1722 fastMaskLo = 31 - __builtin_ctz( mask );
1723 #else
1724 fastMaskHi = 0;
1725 while ( ( mask & ( 0x80000000 >> fastMaskHi ) ) == 0 )
1726 fastMaskHi++;
1727
1728 fastMaskLo = 31;
1729 while ( ( mask & ( 0x80000000 >> fastMaskLo ) ) == 0 )
1730 fastMaskLo--;
1731 #endif
1732 }
1733
1734
1735 /*
1736 * register definitions
1737 */
1738
1739 /* registers which are global for generated code */
1740
1741 // pointer to VM_Data (constant)
1742 #define rVMDATA r14
1743 // vm->dataBase (constant)
1744 #define rDATABASE r15
1745 // programStack (variable)
1746 #define rPSTACK r16
1747
1748 /*
1749 * function local registers,
1750 *
1751 * normally only volatile registers are used, but if there aren't enough
1752 * or function has to preserve some value while calling another one
1753 * then caller safe registers are used as well
1754 */
1755 static const long int gpr_list[] = {
1756 /* caller safe registers, normally only one is used */
1757 r24, r23, r22, r21,
1758 r20, r19, r18, r17,
1759 /* volatile registers (preferred),
1760 * normally no more than 5 is used */
1761 r3, r4, r5, r6,
1762 r7, r8, r9, r10,
1763 };
1764 static const long int gpr_vstart = 8; /* position of first volatile register */
1765 static const long int gpr_total = ARRAY_LEN( gpr_list );
1766
1767 static const long int fpr_list[] = {
1768 /* static registers, normally none is used */
1769 f20, f21, f19, f18,
1770 f17, f16, f15, f14,
1771 /* volatile registers (preferred),
1772 * normally no more than 7 is used */
1773 f0, f1, f2, f3,
1774 f4, f5, f6, f7,
1775 f8, f9, f10, f11,
1776 f12, f13,
1777 };
1778 static const long int fpr_vstart = 8;
1779 static const long int fpr_total = ARRAY_LEN( fpr_list );
1780
1781 /*
1782 * prepare some dummy structures and emit init code
1783 */
1784 static void
1785 PPC_CompileInit( void )
1786 {
1787 di_first = di_last = PPC_Malloc( sizeof( dest_instruction_t ) );
1788 di_first->count = 0;
1789 di_first->next = NULL;
1790 di_first->jump = NULL;
1791
1792 sj_first = sj_last = PPC_Malloc( sizeof( symbolic_jump_t ) );
1793 sj_first->nextJump = NULL;
1794
1795 data_first = PPC_Malloc( sizeof( local_data_t ) );
1796 data_first->count = 0;
1797 data_first->next = NULL;
1798
1799 /*
1800 * init function:
1801 * saves old values of global registers and sets our values
1802 * function prototype is:
1803 * int begin( void *data, int programStack, void *vm->dataBase )
1804 */
1805
1806 /* first instruction must not be placed on instruction list */
1807 emitStart( FALSE_ICOUNT );
1808
1809 long int stack = STACK_SAVE + 4 * GPRLEN;
1810
1811 in( iMFLR, r0 );
1812 in( iSTLU, r1, -stack, r1 );
1813 in( iSTL, rVMDATA, STACK_SAVE + 0 * GPRLEN, r1 );
1814 in( iSTL, rPSTACK, STACK_SAVE + 1 * GPRLEN, r1 );
1815 in( iSTL, rDATABASE, STACK_SAVE + 2 * GPRLEN, r1 );
1816 in( iSTL, r0, stack + STACK_LR, r1 );
1817 in( iMR, rVMDATA, r3 );
1818 in( iMR, rPSTACK, r4 );
1819 in( iMR, rDATABASE, r5 );
1820 in( iBL, +4*8 ); // LINK JUMP: first generated instruction | XXX jump !
1821 in( iLL, rVMDATA, STACK_SAVE + 0 * GPRLEN, r1 );
1822 in( iLL, rPSTACK, STACK_SAVE + 1 * GPRLEN, r1 );
1823 in( iLL, rDATABASE, STACK_SAVE + 2 * GPRLEN, r1 );
1824 in( iLL, r0, stack + STACK_LR, r1 );
1825 in( iMTLR, r0 );
1826 in( iADDI, r1, r1, stack );
1827 in( iBLR );
1828
1829 emitEnd();
1830 }
1831
1832 // rFIRST is the copy of the top value on the opstack
1833 #define rFIRST (gpr_list[ gpr_pos - 1])
1834 // second value on the opstack
1835 #define rSECOND (gpr_list[ gpr_pos - 2 ])
1836 // temporary registers, not on the opstack
1837 #define rTEMP(x) (gpr_list[ gpr_pos + x ])
1838 #define rTMP rTEMP(0)
1839
1840 #define fFIRST (fpr_list[ fpr_pos - 1 ])
1841 #define fSECOND (fpr_list[ fpr_pos - 2 ])
1842 #define fTEMP(x) (fpr_list[ fpr_pos + x ])
1843 #define fTMP fTEMP(0)
1844
1845 // register types
1846 #define rTYPE_STATIC 0x01
1847 #define rTYPE_FLOAT 0x02
1848
1849 // what type should this opcode return
1850 #define RET_INT ( !(i_now->regR & rTYPE_FLOAT) )
1851 #define RET_FLOAT ( i_now->regR & rTYPE_FLOAT )
1852 // what type should it accept
1853 #define ARG_INT ( ! i_now->regA1 )
1854 #define ARG_FLOAT ( i_now->regA1 )
1855 #define ARG2_INT ( ! i_now->regA2 )
1856 #define ARG2_FLOAT ( i_now->regA2 )
1857
1858 /*
1859 * emit OP_CONST, called if nothing has used the const value directly
1860 */
1861 static void
1862 PPC_EmitConst( source_instruction_t * const i_const )
1863 {
1864 emitStart( i_const->i_count );
1865
1866 if ( !(i_const->regR & rTYPE_FLOAT) ) {
1867 // gpr_pos needed for "rFIRST" to work
1868 long int gpr_pos = i_const->regPos;
1869
1870 if ( i_const->arg.si >= -0x8000 && i_const->arg.si < 0x8000 ) {
1871 in( iLI, rFIRST, i_const->arg.si );
1872 } else if ( i_const->arg.i < 0x10000 ) {
1873 in( iLI, rFIRST, 0 );
1874 in( iORI, rFIRST, rFIRST, i_const->arg.i );
1875 } else {
1876 in( iLIS, rFIRST, i_const->arg.ss[ 0 ] );
1877 if ( i_const->arg.us[ 1 ] != 0 )
1878 in( iORI, rFIRST, rFIRST, i_const->arg.us[ 1 ] );
1879 }
1880
1881 } else {
1882 // fpr_pos needed for "fFIRST" to work
1883 long int fpr_pos = i_const->regPos;
1884
1885 // there's no good way to generate the data,
1886 // just read it from data section
1887 in( iLFS, fFIRST, PPC_PushData( i_const->arg.i ), rVMDATA );
1888 }
1889
1890 emitEnd();
1891 }
1892 #define MAYBE_EMIT_CONST() if ( i_const ) PPC_EmitConst( i_const )
1893
1894 /*
1895 * emit empty instruction, just sets the needed pointers
1896 */
1897 static inline void
1898 PPC_EmitNull( source_instruction_t * const i_null )
1899 {
1900 PPC_AppendInstructions( i_null->i_count, 0, NULL );
1901 }
1902 #define EMIT_FALSE_CONST() PPC_EmitNull( i_const )
1903
1904
1905 /*
1906 * analize function for register usage and whether it needs stack (r1) prepared
1907 */
1908 static void
1909 VM_AnalyzeFunction(
1910 source_instruction_t * const i_first,
1911 long int *prepareStack,
1912 long int *gpr_start_pos,
1913 long int *fpr_start_pos
1914 )
1915 {
1916 source_instruction_t *i_now = i_first;
1917
1918 source_instruction_t *value_provider[20] = { NULL };
1919 unsigned long int opstack_depth = 0;
1920
1921 /*
1922 * first step:
1923 * remember what codes returned some value and mark the value type
1924 * when we get to know what it should be
1925 */
1926 while ( (i_now = i_now->next) ) {
1927 unsigned long int op = i_now->op;
1928 unsigned long int opi = vm_opInfo[ op ];
1929
1930 if ( opi & opArgIF ) {
1931 assert( opstack_depth > 0 );
1932
1933 opstack_depth--;
1934 source_instruction_t *vp = value_provider[ opstack_depth ];
1935 unsigned long int vpopi = vm_opInfo[ vp->op ];
1936
1937 if ( (opi & opArgI) && (vpopi & opRetI) ) {
1938 // instruction accepts integer, provider returns integer
1939 //vp->regR |= rTYPE_INT;
1940 //i_now->regA1 = rTYPE_INT;
1941 } else if ( (opi & opArgF) && (vpopi & opRetF) ) {
1942 // instruction accepts float, provider returns float
1943 vp->regR |= rTYPE_FLOAT; // use OR here - could be marked as static
1944 i_now->regA1 = rTYPE_FLOAT;
1945 } else {
1946 // instruction arg type does not agree with
1947 // provider return type
1948 DIE( "unrecognized instruction combination" );
1949 }
1950
1951 }
1952 if ( opi & opArg2IF ) {
1953 assert( opstack_depth > 0 );
1954
1955 opstack_depth--;
1956 source_instruction_t *vp = value_provider[ opstack_depth ];
1957 unsigned long int vpopi = vm_opInfo[ vp->op ];
1958
1959 if ( (opi & opArg2I) && (vpopi & opRetI) ) {
1960 // instruction accepts integer, provider returns integer
1961 //vp->regR |= rTYPE_INT;
1962 //i_now->regA2 = rTYPE_INT;
1963 } else if ( (opi & opArg2F) && (vpopi & opRetF) ) {
1964 // instruction accepts float, provider returns float
1965 vp->regR |= rTYPE_FLOAT; // use OR here - could be marked as static
1966 i_now->regA2 = rTYPE_FLOAT;
1967 } else {
1968 // instruction arg type does not agree with
1969 // provider return type
1970 DIE( "unrecognized instruction combination" );
1971 }
1972 }
1973
1974
1975 if (
1976 ( op == OP_CALL )
1977 ||
1978 ( op == OP_BLOCK_COPY && ( i_now->arg.i > SL( 16, 32 ) || !OPTIMIZE_COPY ) )
1979 ) {
1980 long int i;
1981 *prepareStack = 1;
1982 // force caller safe registers so we won't have to save them
1983 for ( i = 0; i < opstack_depth; i++ ) {
1984 source_instruction_t *vp = value_provider[ i ];
1985 vp->regR |= rTYPE_STATIC;
1986 }
1987 }
1988
1989
1990 if ( opi & opRetIF ) {
1991 value_provider[ opstack_depth ] = i_now;
1992 opstack_depth++;
1993 }
1994 }
1995
1996 /*
1997 * second step:
1998 * now that we know register types; compute exactly how many registers
1999 * of each type we need
2000 */
2001
2002 i_now = i_first;
2003 long int needed_reg[4] = {0,0,0,0}, max_reg[4] = {0,0,0,0};
2004 opstack_depth = 0;
2005 while ( (i_now = i_now->next) ) {
2006 unsigned long int op = i_now->op;
2007 unsigned long int opi = vm_opInfo[ op ];
2008
2009 if ( opi & opArgIF ) {
2010 assert( opstack_depth > 0 );
2011 opstack_depth--;
2012 source_instruction_t *vp = value_provider[ opstack_depth ];
2013
2014 needed_reg[ ( vp->regR & 2 ) ] -= 1;
2015 if ( vp->regR & 1 ) // static
2016 needed_reg[ ( vp->regR & 3 ) ] -= 1;
2017 }
2018 if ( opi & opArg2IF ) {
2019 assert( opstack_depth > 0 );
2020 opstack_depth--;
2021 source_instruction_t *vp = value_provider[ opstack_depth ];
2022
2023 needed_reg[ ( vp->regR & 2 ) ] -= 1;
2024 if ( vp->regR & 1 ) // static
2025 needed_reg[ ( vp->regR & 3 ) ] -= 1;
2026 }
2027
2028 if ( opi & opRetIF ) {
2029 long int i;
2030 value_provider[ opstack_depth ] = i_now;
2031 opstack_depth++;
2032
2033 i = i_now->regR & 2;
2034 needed_reg[ i ] += 1;
2035 if ( max_reg[ i ] < needed_reg[ i ] )
2036 max_reg[ i ] = needed_reg[ i ];
2037
2038 i = i_now->regR & 3;
2039 if ( i & 1 ) {
2040 needed_reg[ i ] += 1;
2041 if ( max_reg[ i ] < needed_reg[ i ] )
2042 max_reg[ i ] = needed_reg[ i ];
2043 }
2044 }
2045 }
2046
2047 long int gpr_start = gpr_vstart;
2048 const long int gpr_volatile = gpr_total - gpr_vstart;
2049 if ( max_reg[ 1 ] > 0 || max_reg[ 0 ] > gpr_volatile ) {
2050 // max_reg[ 0 ] - all gprs needed
2051 // max_reg[ 1 ] - static gprs needed
2052 long int max = max_reg[ 0 ] - gpr_volatile;
2053 if ( max_reg[ 1 ] > max )
2054 max = max_reg[ 1 ];
2055 if ( max > gpr_vstart ) {
2056 /* error */
2057 DIE( "Need more GPRs" );
2058 }
2059
2060 gpr_start -= max;
2061
2062 // need stack to save caller safe registers
2063 *prepareStack = 1;
2064 }
2065 *gpr_start_pos = gpr_start;
2066
2067 long int fpr_start = fpr_vstart;
2068 const long int fpr_volatile = fpr_total - fpr_vstart;
2069 if ( max_reg[ 3 ] > 0 || max_reg[ 2 ] > fpr_volatile ) {
2070 // max_reg[ 2 ] - all fprs needed
2071 // max_reg[ 3 ] - static fprs needed
2072 long int max = max_reg[ 2 ] - fpr_volatile;
2073 if ( max_reg[ 3 ] > max )
2074 max = max_reg[ 3 ];
2075 if ( max > fpr_vstart ) {
2076 /* error */
2077 DIE( "Need more FPRs" );
2078 }
2079
2080 fpr_start -= max;
2081
2082 // need stack to save caller safe registers
2083 *prepareStack = 1;
2084 }
2085 *fpr_start_pos = fpr_start;
2086 }
2087
2088 /*
2089 * translate opcodes to ppc instructions,
2090 * it works on functions, not on whole code at once
2091 */
2092 static void
2093 VM_CompileFunction( source_instruction_t * const i_first )
2094 {
2095 long int prepareStack = 0;
2096 long int gpr_start_pos, fpr_start_pos;
2097
2098 VM_AnalyzeFunction( i_first, &prepareStack, &gpr_start_pos, &fpr_start_pos );
2099
2100 long int gpr_pos = gpr_start_pos, fpr_pos = fpr_start_pos;
2101
2102 // OP_CONST combines well with many opcodes so we treat it in a special way
2103 source_instruction_t *i_const = NULL;
2104 source_instruction_t *i_now = i_first;
2105
2106 // how big the stack has to be
2107 long int save_space = STACK_SAVE;
2108 {
2109 if ( gpr_start_pos < gpr_vstart )
2110 save_space += (gpr_vstart - gpr_start_pos) * GPRLEN;
2111 save_space = ( save_space + 15 ) & ~0x0f;
2112
2113 if ( fpr_start_pos < fpr_vstart )
2114 save_space += (fpr_vstart - fpr_start_pos) * FPRLEN;
2115 save_space = ( save_space + 15 ) & ~0x0f;
2116 }
2117
2118 long int stack_temp = prepareStack ? STACK_TEMP : STACK_RTEMP;
2119
2120 while ( (i_now = i_now->next) ) {
2121 emitStart( i_now->i_count );
2122
2123 switch ( i_now->op )
2124 {
2125 default:
2126 case OP_UNDEF:
2127 case OP_IGNORE:
2128 MAYBE_EMIT_CONST();
2129 in( iNOP );
2130 break;
2131
2132 case OP_BREAK:
2133 MAYBE_EMIT_CONST();
2134 // force SEGV
2135 in( iLWZ, r0, 0, r0 );
2136 break;
2137
2138 case OP_ENTER:
2139 if ( i_const )
2140 DIE( "Weird opcode order" );
2141
2142 // don't prepare stack if not needed
2143 if ( prepareStack ) {
2144 long int i, save_pos = STACK_SAVE;
2145
2146 in( iMFLR, r0 );
2147 in( iSTLU, r1, -save_space, r1 );
2148 in( iSTL, r0, save_space + STACK_LR, r1 );
2149
2150 /* save registers */
2151 for ( i = gpr_start_pos; i < gpr_vstart; i++ ) {
2152 in( iSTL, gpr_list[ i ], save_pos, r1 );
2153 save_pos += GPRLEN;
2154 }
2155 save_pos = ( save_pos + 15 ) & ~0x0f;
2156
2157 for ( i = fpr_start_pos; i < fpr_vstart; i++ ) {
2158 in( iSTFD, fpr_list[ i ], save_pos, r1 );
2159 save_pos += FPRLEN;
2160 }
2161 prepareStack = 2;
2162 }
2163
2164 in( iADDI, rPSTACK, rPSTACK, - i_now->arg.si );
2165 break;
2166
2167 case OP_LEAVE:
2168 if ( i_const ) {
2169 EMIT_FALSE_CONST();
2170
2171 if ( i_const->regR & rTYPE_FLOAT)
2172 DIE( "constant float in OP_LEAVE" );
2173
2174 if ( i_const->arg.si >= -0x8000 && i_const->arg.si < 0x8000 ) {
2175 in( iLI, r3, i_const->arg.si );
2176 } else if ( i_const->arg.i < 0x10000 ) {
2177 in( iLI, r3, 0 );
2178 in( iORI, r3, r3, i_const->arg.i );
2179 } else {
2180 in( iLIS, r3, i_const->arg.ss[ 0 ] );
2181 if ( i_const->arg.us[ 1 ] != 0 )
2182 in( iORI, r3, r3, i_const->arg.us[ 1 ] );
2183 }
2184 gpr_pos--;
2185 } else {
2186 MAYBE_EMIT_CONST();
2187
2188 /* place return value in r3 */
2189 if ( ARG_INT ) {
2190 if ( rFIRST != r3 )
2191 in( iMR, r3, rFIRST );
2192 gpr_pos--;
2193 } else {
2194 in( iSTFS, fFIRST, stack_temp, r1 );
2195 in( iLWZ, r3, stack_temp, r1 );
2196 fpr_pos--;
2197 }
2198 }
2199
2200 // don't undo stack if not prepared
2201 if ( prepareStack >= 2 ) {
2202 long int i, save_pos = STACK_SAVE;
2203
2204 in( iLL, r0, save_space + STACK_LR, r1 );
2205
2206
2207 /* restore registers */
2208 for ( i = gpr_start_pos; i < gpr_vstart; i++ ) {
2209 in( iLL, gpr_list[ i ], save_pos, r1 );
2210 save_pos += GPRLEN;
2211 }
2212 save_pos = ( save_pos + 15 ) & ~0x0f;
2213 for ( i = fpr_start_pos; i < fpr_vstart; i++ ) {
2214 in( iLFD, fpr_list[ i ], save_pos, r1 );
2215 save_pos += FPRLEN;
2216 }
2217
2218 in( iMTLR, r0 );
2219 in( iADDI, r1, r1, save_space );
2220 }
2221 in( iADDI, rPSTACK, rPSTACK, i_now->arg.si);
2222 in( iBLR );
2223 assert( gpr_pos == gpr_start_pos );
2224 assert( fpr_pos == fpr_start_pos );
2225 break;
2226
2227 case OP_CALL:
2228 if ( i_const ) {
2229 EMIT_FALSE_CONST();
2230
2231 if ( i_const->arg.si >= 0 ) {
2232 emitJump(
2233 i_const->arg.i,
2234 branchAlways, 0, branchExtLink
2235 );
2236 } else {
2237 /* syscall */
2238 in( iLL, r0, VM_Data_Offset( AsmCall ), rVMDATA );
2239
2240 in( iLI, r3, i_const->arg.si ); // negative value
2241 in( iMR, r4, rPSTACK ); // push PSTACK on argument list
2242
2243 in( iMTCTR, r0 );
2244 in( iBCTRL );
2245 }
2246 if ( rFIRST != r3 )
2247 in( iMR, rFIRST, r3 );
2248 } else {
2249 MAYBE_EMIT_CONST();
2250
2251 in( iCMPWI, cr7, rFIRST, 0 );
2252 in( iBLTm, cr7, +4*5 /* syscall */ ); // XXX jump !
2253 /* instruction call */
2254
2255 // get instruction address
2256 in( iLL, r0, VM_Data_Offset( iPointers ), rVMDATA );
2257 in( iRLWINM, rFIRST, rFIRST, GPRLEN_SHIFT, 0, 31-GPRLEN_SHIFT ); // mul * GPRLEN
2258 in( iLLX, r0, rFIRST, r0 ); // load pointer
2259
2260 in( iB, +4*(3 + (rFIRST != r3 ? 1 : 0) ) ); // XXX jump !
2261
2262 /* syscall */
2263 in( iLL, r0, VM_Data_Offset( AsmCall ), rVMDATA ); // get asmCall pointer
2264 /* rFIRST can be r3 or some static register */
2265 if ( rFIRST != r3 )
2266 in( iMR, r3, rFIRST ); // push OPSTACK top value on argument list
2267 in( iMR, r4, rPSTACK ); // push PSTACK on argument list
2268
2269 /* common code */
2270 in( iMTCTR, r0 );
2271 in( iBCTRL );
2272
2273 if ( rFIRST != r3 )
2274 in( iMR, rFIRST, r3 ); // push return value on the top of the opstack
2275 }
2276 break;
2277
2278 case OP_PUSH:
2279 MAYBE_EMIT_CONST();
2280 if ( RET_INT )
2281 gpr_pos++;
2282 else
2283 fpr_pos++;
2284 /* no instructions here */
2285 force_emit = 1;
2286 break;
2287
2288 case OP_POP:
2289 MAYBE_EMIT_CONST();
2290 if ( ARG_INT )
2291 gpr_pos--;
2292 else
2293 fpr_pos--;
2294 /* no instructions here */
2295 force_emit = 1;
2296 break;
2297
2298 case OP_CONST:
2299 MAYBE_EMIT_CONST();
2300 /* nothing here */
2301 break;
2302
2303 case OP_LOCAL:
2304 MAYBE_EMIT_CONST();
2305 {
2306 signed long int hi, lo;
2307 hi = i_now->arg.ss[ 0 ];
2308 lo = i_now->arg.ss[ 1 ];
2309 if ( lo < 0 )
2310 hi += 1;
2311
2312 gpr_pos++;
2313 if ( hi == 0 ) {
2314 in( iADDI, rFIRST, rPSTACK, lo );
2315 } else {
2316 in( iADDIS, rFIRST, rPSTACK, hi );
2317 if ( lo != 0 )
2318 in( iADDI, rFIRST, rFIRST, lo );
2319 }
2320 }
2321 break;
2322
2323 case OP_JUMP:
2324 if ( i_const ) {
2325 EMIT_FALSE_CONST();
2326
2327 emitJump(
2328 i_const->arg.i,
2329 branchAlways, 0, 0
2330 );
2331 } else {
2332 MAYBE_EMIT_CONST();
2333
2334 in( iLL, r0, VM_Data_Offset( iPointers ), rVMDATA );
2335 in( iRLWINM, rFIRST, rFIRST, GPRLEN_SHIFT, 0, 31-GPRLEN_SHIFT ); // mul * GPRLEN
2336 in( iLLX, r0, rFIRST, r0 ); // load pointer
2337 in( iMTCTR, r0 );
2338 in( iBCTR );
2339 }
2340 gpr_pos--;
2341 break;
2342
2343 case OP_EQ:
2344 case OP_NE:
2345 if ( i_const && i_const->arg.si >= -0x8000 && i_const->arg.si < 0x10000 ) {
2346 EMIT_FALSE_CONST();
2347 if ( i_const->arg.si >= 0x8000 )
2348 in( iCMPLWI, cr7, rSECOND, i_const->arg.i );
2349 else
2350 in( iCMPWI, cr7, rSECOND, i_const->arg.si );
2351 } else {
2352 MAYBE_EMIT_CONST();
2353 in( iCMPW, cr7, rSECOND, rFIRST );
2354 }
2355 emitJump(
2356 i_now->arg.i,
2357 (i_now->op == OP_EQ ? branchTrue : branchFalse),
2358 4*cr7+eq, 0
2359 );
2360 gpr_pos -= 2;
2361 break;
2362
2363 case OP_LTI:
2364 case OP_GEI:
2365 if ( i_const && i_const->arg.si >= -0x8000 && i_const->arg.si < 0x8000 ) {
2366 EMIT_FALSE_CONST();
2367 in( iCMPWI, cr7, rSECOND, i_const->arg.si );
2368 } else {
2369 MAYBE_EMIT_CONST();
2370 in( iCMPW, cr7, rSECOND, rFIRST );
2371 }
2372 emitJump(
2373 i_now->arg.i,
2374 ( i_now->op == OP_LTI ? branchTrue : branchFalse ),
2375 4*cr7+lt, 0
2376 );
2377 gpr_pos -= 2;
2378 break;
2379
2380 case OP_GTI:
2381 case OP_LEI:
2382 if ( i_const && i_const->arg.si >= -0x8000 && i_const->arg.si < 0x8000 ) {
2383 EMIT_FALSE_CONST();
2384 in( iCMPWI, cr7, rSECOND, i_const->arg.si );
2385 } else {
2386 MAYBE_EMIT_CONST();
2387 in( iCMPW, cr7, rSECOND, rFIRST );
2388 }
2389 emitJump(
2390 i_now->arg.i,
2391 ( i_now->op == OP_GTI ? branchTrue : branchFalse ),
2392 4*cr7+gt, 0
2393 );
2394 gpr_pos -= 2;
2395 break;
2396
2397 case OP_LTU:
2398 case OP_GEU:
2399 if ( i_const && i_const->arg.i < 0x10000 ) {
2400 EMIT_FALSE_CONST();
2401 in( iCMPLWI, cr7, rSECOND, i_const->arg.i );
2402 } else {
2403 MAYBE_EMIT_CONST();
2404 in( iCMPLW, cr7, rSECOND, rFIRST );
2405 }
2406 emitJump(
2407 i_now->arg.i,
2408 ( i_now->op == OP_LTU ? branchTrue : branchFalse ),
2409 4*cr7+lt, 0
2410 );
2411 gpr_pos -= 2;
2412 break;
2413
2414 case OP_GTU:
2415 case OP_LEU:
2416 if ( i_const && i_const->arg.i < 0x10000 ) {
2417 EMIT_FALSE_CONST();
2418 in( iCMPLWI, cr7, rSECOND, i_const->arg.i );
2419 } else {
2420 MAYBE_EMIT_CONST();
2421 in( iCMPLW, cr7, rSECOND, rFIRST );
2422 }
2423 emitJump(
2424 i_now->arg.i,
2425 ( i_now->op == OP_GTU ? branchTrue : branchFalse ),
2426 4*cr7+gt, 0
2427 );
2428 gpr_pos -= 2;
2429 break;
2430
2431 case OP_EQF:
2432 case OP_NEF:
2433 MAYBE_EMIT_CONST();
2434 in( iFCMPU, cr7, fSECOND, fFIRST );
2435 emitJump(
2436 i_now->arg.i,
2437 ( i_now->op == OP_EQF ? branchTrue : branchFalse ),
2438 4*cr7+eq, 0
2439 );
2440 fpr_pos -= 2;
2441 break;
2442
2443 case OP_LTF:
2444 case OP_GEF:
2445 MAYBE_EMIT_CONST();
2446 in( iFCMPU, cr7, fSECOND, fFIRST );
2447 emitJump(
2448 i_now->arg.i,
2449 ( i_now->op == OP_LTF ? branchTrue : branchFalse ),
2450 4*cr7+lt, 0
2451 );
2452 fpr_pos -= 2;
2453 break;
2454
2455 case OP_GTF:
2456 case OP_LEF:
2457 MAYBE_EMIT_CONST();
2458 in( iFCMPU, cr7, fSECOND, fFIRST );
2459 emitJump(
2460 i_now->arg.i,
2461 ( i_now->op == OP_GTF ? branchTrue : branchFalse ),
2462 4*cr7+gt, 0
2463 );
2464 fpr_pos -= 2;
2465 break;
2466
2467 case OP_LOAD1:
2468 MAYBE_EMIT_CONST();
2469 #if OPTIMIZE_MASK
2470 in( iRLWINM, rFIRST, rFIRST, 0, fastMaskHi, fastMaskLo );
2471 #else
2472 in( iLWZ, r0, VM_Data_Offset( dataMask ), rVMDATA );
2473 in( iAND, rFIRST, rFIRST, r0 );
2474 #endif
2475 in( iLBZX, rFIRST, rFIRST, rDATABASE );
2476 break;
2477
2478 case OP_LOAD2:
2479 MAYBE_EMIT_CONST();
2480 #if OPTIMIZE_MASK
2481 in( iRLWINM, rFIRST, rFIRST, 0, fastMaskHi, fastMaskLo );
2482 #else
2483 in( iLWZ, r0, VM_Data_Offset( dataMask ), rVMDATA );
2484 in( iAND, rFIRST, rFIRST, r0 );
2485 #endif
2486 in( iLHZX, rFIRST, rFIRST, rDATABASE );
2487 break;
2488
2489 case OP_LOAD4:
2490 MAYBE_EMIT_CONST();
2491 #if OPTIMIZE_MASK
2492 in( iRLWINM, rFIRST, rFIRST, 0, fastMaskHi, fastMaskLo );
2493 #else
2494 in( iLWZ, r0, VM_Data_Offset( dataMask ), rVMDATA );
2495 in( iAND, rFIRST, rFIRST, r0 );
2496 #endif
2497 if ( RET_INT ) {
2498 in( iLWZX, rFIRST, rFIRST, rDATABASE );
2499 } else {
2500 fpr_pos++;
2501 in( iLFSX, fFIRST, rFIRST, rDATABASE );
2502 gpr_pos--;
2503 }
2504 break;
2505
2506 case OP_STORE1:
2507 MAYBE_EMIT_CONST();
2508 #if OPTIMIZE_MASK
2509 in( iRLWINM, rSECOND, rSECOND, 0, fastMaskHi, fastMaskLo );
2510 #else
2511 in( iLWZ, r0, VM_Data_Offset( dataMask ), rVMDATA );
2512 in( iAND, rSECOND, rSECOND, r0 );
2513 #endif
2514 in( iSTBX, rFIRST, rSECOND, rDATABASE );
2515 gpr_pos -= 2;
2516 break;
2517
2518 case OP_STORE2:
2519 MAYBE_EMIT_CONST();
2520 #if OPTIMIZE_MASK
2521 in( iRLWINM, rSECOND, rSECOND, 0, fastMaskHi, fastMaskLo );
2522 #else
2523 in( iLWZ, r0, VM_Data_Offset( dataMask ), rVMDATA );
2524 in( iAND, rSECOND, rSECOND, r0 );
2525 #endif
2526 in( iSTHX, rFIRST, rSECOND, rDATABASE );
2527 gpr_pos -= 2;
2528 break;
2529
2530 case OP_STORE4:
2531 MAYBE_EMIT_CONST();
2532 if ( ARG_INT ) {
2533 #if OPTIMIZE_MASK
2534 in( iRLWINM, rSECOND, rSECOND, 0, fastMaskHi, fastMaskLo );
2535 #else
2536 in( iLWZ, r0, VM_Data_Offset( dataMask ), rVMDATA );
2537 in( iAND, rSECOND, rSECOND, r0 );
2538 #endif
2539
2540 in( iSTWX, rFIRST, rSECOND, rDATABASE );
2541 gpr_pos--;
2542 } else {
2543 #if OPTIMIZE_MASK
2544 in( iRLWINM, rFIRST, rFIRST, 0, fastMaskHi, fastMaskLo );
2545 #else
2546 in( iLWZ, r0, VM_Data_Offset( dataMask ), rVMDATA );
2547 in( iAND, rFIRST, rFIRST, r0 );
2548 #endif
2549
2550 in( iSTFSX, fFIRST, rFIRST, rDATABASE );
2551 fpr_pos--;
2552 }
2553 gpr_pos--;
2554 break;
2555
2556 case OP_ARG:
2557 MAYBE_EMIT_CONST();
2558 in( iADDI, r0, rPSTACK, i_now->arg.b );
2559 if ( ARG_INT ) {
2560 in( iSTWX, rFIRST, rDATABASE, r0 );
2561 gpr_pos--;
2562 } else {
2563 in( iSTFSX, fFIRST, rDATABASE, r0 );
2564 fpr_pos--;
2565 }
2566 break;
2567
2568 case OP_BLOCK_COPY:
2569 MAYBE_EMIT_CONST();
2570 #if OPTIMIZE_COPY
2571 if ( i_now->arg.i <= SL( 16, 32 ) ) {
2572 /* block is very short so copy it in-place */
2573
2574 unsigned int len = i_now->arg.i;
2575 unsigned int copied = 0, left = len;
2576
2577 in( iADD, rFIRST, rFIRST, rDATABASE );
2578 in( iADD, rSECOND, rSECOND, rDATABASE );
2579
2580 if ( len >= GPRLEN ) {
2581 long int i, words = len / GPRLEN;
2582 in( iLL, r0, 0, rFIRST );
2583 for ( i = 1; i < words; i++ )
2584 in( iLL, rTEMP( i - 1 ), GPRLEN * i, rFIRST );
2585
2586 in( iSTL, r0, 0, rSECOND );
2587 for ( i = 1; i < words; i++ )
2588 in( iSTL, rTEMP( i - 1 ), GPRLEN * i, rSECOND );
2589
2590 copied += words * GPRLEN;
2591 left -= copied;
2592 }
2593
2594 if ( SL( 0, left >= 4 ) ) {
2595 in( iLWZ, r0, copied+0, rFIRST );
2596 in( iSTW, r0, copied+0, rSECOND );
2597 copied += 4;
2598 left -= 4;
2599 }
2600 if ( left >= 4 ) {
2601 DIE("Bug in OP_BLOCK_COPY");
2602 }
2603 if ( left == 3 ) {
2604 in( iLHZ, r0, copied+0, rFIRST );
2605 in( iLBZ, rTMP, copied+2, rFIRST );
2606 in( iSTH, r0, copied+0, rSECOND );
2607 in( iSTB, rTMP, copied+2, rSECOND );
2608 } else if ( left == 2 ) {
2609 in( iLHZ, r0, copied+0, rFIRST );
2610 in( iSTH, r0, copied+0, rSECOND );
2611 } else if ( left == 1 ) {
2612 in( iLBZ, r0, copied+0, rFIRST );
2613 in( iSTB, r0, copied+0, rSECOND );
2614 }
2615 } else
2616 #endif
2617 {
2618 unsigned long int r5_ori = 0;
2619 if ( i_now->arg.si >= -0x8000 && i_now->arg.si < 0x8000 ) {
2620 in( iLI, r5, i_now->arg.si );
2621 } else if ( i_now->arg.i < 0x10000 ) {
2622 in( iLI, r5, 0 );
2623 r5_ori = i_now->arg.i;
2624 } else {
2625 in( iLIS, r5, i_now->arg.ss[ 0 ] );
2626 r5_ori = i_now->arg.us[ 1 ];
2627 }
2628
2629 in( iLL, r0, VM_Data_Offset( BlockCopy ), rVMDATA ); // get blockCopy pointer
2630
2631 if ( r5_ori )
2632 in( iORI, r5, r5, r5_ori );
2633
2634 in( iMTCTR, r0 );
2635
2636 if ( rFIRST != r4 )
2637 in( iMR, r4, rFIRST );
2638 if ( rSECOND != r3 )
2639 in( iMR, r3, rSECOND );
2640
2641 in( iBCTRL );
2642 }
2643
2644 gpr_pos -= 2;
2645 break;
2646
2647 case OP_SEX8:
2648 MAYBE_EMIT_CONST();
2649 in( iEXTSB, rFIRST, rFIRST );
2650 break;
2651
2652 case OP_SEX16:
2653 MAYBE_EMIT_CONST();
2654 in( iEXTSH, rFIRST, rFIRST );
2655 break;
2656
2657 case OP_NEGI:
2658 MAYBE_EMIT_CONST();
2659 in( iNEG, rFIRST, rFIRST );
2660 break;
2661
2662 case OP_ADD:
2663 if ( i_const ) {
2664 EMIT_FALSE_CONST();
2665
2666 signed short int hi, lo;
2667 hi = i_const->arg.ss[ 0 ];
2668 lo = i_const->arg.ss[ 1 ];
2669 if ( lo < 0 )
2670 hi += 1;
2671
2672 if ( hi != 0 )
2673 in( iADDIS, rSECOND, rSECOND, hi );
2674 if ( lo != 0 )
2675 in( iADDI, rSECOND, rSECOND, lo );
2676
2677 // if both are zero no instruction will be written
2678 if ( hi == 0 && lo == 0 )
2679 force_emit = 1;
2680 } else {
2681 MAYBE_EMIT_CONST();
2682 in( iADD, rSECOND, rSECOND, rFIRST );
2683 }
2684 gpr_pos--;
2685 break;
2686
2687 case OP_SUB:
2688 MAYBE_EMIT_CONST();
2689 in( iSUB, rSECOND, rSECOND, rFIRST );
2690 gpr_pos--;
2691 break;
2692
2693 case OP_DIVI:
2694 MAYBE_EMIT_CONST();
2695 in( iDIVW, rSECOND, rSECOND, rFIRST );
2696 gpr_pos--;
2697 break;
2698
2699 case OP_DIVU:
2700 MAYBE_EMIT_CONST();
2701 in( iDIVWU, rSECOND, rSECOND, rFIRST );
2702 gpr_pos--;
2703 break;
2704
2705 case OP_MODI:
2706 MAYBE_EMIT_CONST();
2707 in( iDIVW, r0, rSECOND, rFIRST );
2708 in( iMULLW, r0, r0, rFIRST );
2709 in( iSUB, rSECOND, rSECOND, r0 );
2710 gpr_pos--;
2711 break;
2712
2713 case OP_MODU:
2714 MAYBE_EMIT_CONST();
2715 in( iDIVWU, r0, rSECOND, rFIRST );
2716 in( iMULLW, r0, r0, rFIRST );
2717 in( iSUB, rSECOND, rSECOND, r0 );
2718 gpr_pos--;
2719 break;
2720
2721 case OP_MULI:
2722 case OP_MULU:
2723 MAYBE_EMIT_CONST();
2724 in( iMULLW, rSECOND, rSECOND, rFIRST );
2725 gpr_pos--;
2726 break;
2727
2728 case OP_BAND:
2729 MAYBE_EMIT_CONST();
2730 in( iAND, rSECOND, rSECOND, rFIRST );
2731 gpr_pos--;
2732 break;
2733
2734 case OP_BOR:
2735 MAYBE_EMIT_CONST();
2736 in( iOR, rSECOND, rSECOND, rFIRST );
2737 gpr_pos--;
2738 break;
2739
2740 case OP_BXOR:
2741 MAYBE_EMIT_CONST();
2742 in( iXOR, rSECOND, rSECOND, rFIRST );
2743 gpr_pos--;
2744 break;
2745
2746 case OP_BCOM:
2747 MAYBE_EMIT_CONST();
2748 in( iNOT, rFIRST, rFIRST );
2749 break;
2750
2751 case OP_LSH:
2752 MAYBE_EMIT_CONST();
2753 in( iSLW, rSECOND, rSECOND, rFIRST );
2754 gpr_pos--;
2755 break;
2756
2757 case OP_RSHI:
2758 MAYBE_EMIT_CONST();
2759 in( iSRAW, rSECOND, rSECOND, rFIRST );
2760 gpr_pos--;
2761 break;
2762
2763 case OP_RSHU:
2764 MAYBE_EMIT_CONST();
2765 in( iSRW, rSECOND, rSECOND, rFIRST );
2766 gpr_pos--;
2767 break;
2768
2769 case OP_NEGF:
2770 MAYBE_EMIT_CONST();
2771 in( iFNEG, fFIRST, fFIRST );
2772 break;
2773
2774 case OP_ADDF:
2775 MAYBE_EMIT_CONST();
2776 in( iFADDS, fSECOND, fSECOND, fFIRST );
2777 fpr_pos--;
2778 break;
2779
2780 case OP_SUBF:
2781 MAYBE_EMIT_CONST();
2782 in( iFSUBS, fSECOND, fSECOND, fFIRST );
2783 fpr_pos--;
2784 break;
2785
2786 case OP_DIVF:
2787 MAYBE_EMIT_CONST();
2788 in( iFDIVS, fSECOND, fSECOND, fFIRST );
2789 fpr_pos--;
2790 break;
2791
2792 case OP_MULF:
2793 MAYBE_EMIT_CONST();
2794 in( iFMULS, fSECOND, fSECOND, fFIRST );
2795 fpr_pos--;
2796 break;
2797
2798 case OP_CVIF:
2799 MAYBE_EMIT_CONST();
2800 fpr_pos++;
2801 in( iXORIS, rFIRST, rFIRST, 0x8000 );
2802 in( iLIS, r0, 0x4330 );
2803 in( iSTW, rFIRST, stack_temp + 4, r1 );
2804 in( iSTW, r0, stack_temp, r1 );
2805 in( iLFS, fTMP, VM_Data_Offset( floatBase ), rVMDATA );
2806 in( iLFD, fFIRST, stack_temp, r1 );
2807 in( iFSUB, fFIRST, fFIRST, fTMP );
2808 in( iFRSP, fFIRST, fFIRST );
2809 gpr_pos--;
2810 break;
2811
2812 case OP_CVFI:
2813 MAYBE_EMIT_CONST();
2814 gpr_pos++;
2815 in( iFCTIWZ, fFIRST, fFIRST );
2816 in( iSTFD, fFIRST, stack_temp, r1 );
2817 in( iLWZ, rFIRST, stack_temp + 4, r1 );
2818 fpr_pos--;
2819 break;
2820 }
2821
2822 i_const = NULL;
2823
2824 if ( i_now->op != OP_CONST ) {
2825 // emit the instructions if it isn't OP_CONST
2826 emitEnd();
2827 } else {
2828 // mark in what register the value should be saved
2829 if ( RET_INT )
2830 i_now->regPos = ++gpr_pos;
2831 else
2832 i_now->regPos = ++fpr_pos;
2833
2834 #if OPTIMIZE_HOLE
2835 i_const = i_now;
2836 #else
2837 PPC_EmitConst( i_now );
2838 #endif
2839 }
2840 }
2841 if ( i_const )
2842 DIE( "left (unused) OP_CONST" );
2843
2844 {
2845 // free opcode information, don't free first dummy one
2846 source_instruction_t *i_next = i_first->next;
2847 while ( i_next ) {
2848 i_now = i_next;
2849 i_next = i_now->next;
2850 PPC_Free( i_now );
2851 }
2852 }
2853 }
2854
2855
2856 /*
2857 * check which jumps are short enough to use signed 16bit immediate branch
2858 */
2859 static void
2860 PPC_ShrinkJumps( void )
2861 {
2862 symbolic_jump_t *sj_now = sj_first;
2863 while ( (sj_now = sj_now->nextJump) ) {
2864 if ( sj_now->bo == branchAlways )
2865 // non-conditional branch has 26bit immediate
2866 sj_now->parent->length = 1;
2867
2868 else {
2869 dest_instruction_t *di = di_pointers[ sj_now->jump_to ];
2870 dest_instruction_t *ji = sj_now->parent;
2871 long int jump_length = 0;
2872 if ( ! di )
2873 DIE( "No instruction to jump to" );
2874 if ( ji->count > di->count ) {
2875 do {
2876 jump_length += di->length;
2877 } while ( ( di = di->next ) != ji );
2878 } else {
2879 jump_length = 1;
2880 while ( ( ji = ji->next ) != di )
2881 jump_length += ji->length;
2882 }
2883 if ( jump_length < 0x2000 )
2884 // jump is short, use normal instruction
2885 sj_now->parent->length = 1;
2886 }
2887 }
2888 }
2889
2890 /*
2891 * puts all the data in one place, it consists of many different tasks
2892 */
2893 static void
2894 PPC_ComputeCode( vm_t *vm )
2895 {
2896 dest_instruction_t *di_now = di_first;
2897
2898 unsigned long int codeInstructions = 0;
2899 // count total instruciton number
2900 while ( (di_now = di_now->next ) )
2901 codeInstructions += di_now->length;
2902
2903 size_t codeLength = sizeof( vm_data_t )
2904 + sizeof( unsigned int ) * data_acc
2905 + sizeof( ppc_instruction_t ) * codeInstructions;
2906
2907 // get the memory for the generated code, smarter ppcs need the
2908 // mem to be marked as executable (whill change later)
2909 unsigned char *dataAndCode = mmap( NULL, codeLength,
2910 PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0 );
2911
2912 if (dataAndCode == MAP_FAILED)
2913 DIE( "Not enough memory" );
2914
2915 ppc_instruction_t *codeNow, *codeBegin;
2916 codeNow = codeBegin = (ppc_instruction_t *)( dataAndCode + VM_Data_Offset( data[ data_acc ] ) );
2917
2918 ppc_instruction_t nop = IN( iNOP );
2919
2920 // copy instructions to the destination
2921 // fills the jump instructions with nops
2922 // saves pointers of all instructions
2923 di_now = di_first;
2924 while ( (di_now = di_now->next ) ) {
2925 unsigned long int i_count = di_now->i_count;
2926 if ( i_count != FALSE_ICOUNT ) {
2927 if ( ! di_pointers[ i_count ] )
2928 di_pointers[ i_count ] = (void *) codeNow;
2929 }
2930
2931 if ( di_now->jump == NULL ) {
2932 memcpy( codeNow, &(di_now->code[0]), di_now->length * sizeof( ppc_instruction_t ) );
2933 codeNow += di_now->length;
2934 } else {
2935 long int i;
2936 symbolic_jump_t *sj;
2937 for ( i = 0; i < di_now->length; i++ )
2938 codeNow[ i ] = nop;
2939 codeNow += di_now->length;
2940
2941 sj = di_now->jump;
2942 // save position of jumping instruction
2943 sj->parent = (void *)(codeNow - 1);
2944 }
2945 }
2946
2947 // compute the jumps and write corresponding instructions
2948 symbolic_jump_t *sj_now = sj_first;
2949 while ( (sj_now = sj_now->nextJump ) ) {
2950 ppc_instruction_t *jumpFrom = (void *) sj_now->parent;
2951 ppc_instruction_t *jumpTo = (void *) di_pointers[ sj_now->jump_to ];
2952 signed long int jumpLength = jumpTo - jumpFrom;
2953
2954 // if jump is short, just write it
2955 if ( jumpLength >= - 8192 && jumpLength < 8192 ) {
2956 powerpc_iname_t branchConditional = sj_now->ext & branchExtLink ? iBCL : iBC;
2957 *jumpFrom = IN( branchConditional, sj_now->bo, sj_now->bi, jumpLength * 4 );
2958 continue;
2959 }
2960
2961 // jump isn't short so write it as two instructions
2962 //
2963 // the letter one is a non-conditional branch instruction which
2964 // accepts immediate values big enough (26 bits)
2965 *jumpFrom = IN( (sj_now->ext & branchExtLink ? iBL : iB), jumpLength * 4 );
2966 if ( sj_now->bo == branchAlways )
2967 continue;
2968
2969 // there should have been additional space prepared for this case
2970 if ( jumpFrom[ -1 ] != nop )
2971 DIE( "additional space for long jump not prepared" );
2972
2973 // invert instruction condition
2974 long int bo = 0;
2975 switch ( sj_now->bo ) {
2976 case branchTrue:
2977 bo = branchFalse;
2978 break;
2979 case branchFalse:
2980 bo = branchTrue;
2981 break;
2982 default:
2983 DIE( "unrecognized branch type" );
2984 break;
2985 }
2986
2987 // the former instruction is an inverted conditional branch which
2988 // jumps over the non-conditional one
2989 jumpFrom[ -1 ] = IN( iBC, bo, sj_now->bi, +2*4 );
2990 }
2991
2992 vm->codeBase = dataAndCode;
2993 vm->codeLength = codeLength;
2994
2995 vm_data_t *data = (vm_data_t *)dataAndCode;
2996
2997 #if ELF64
2998 // prepare Official Procedure Descriptor for the generated code
2999 // and retrieve real function pointer for helper functions
3000
3001 opd_t *ac = (void *)VM_AsmCall, *bc = (void *)VM_BlockCopy;
3002 data->opd.function = codeBegin;
3003 // trick it into using the same TOC
3004 // this way we won't have to switch TOC before calling AsmCall or BlockCopy
3005 data->opd.toc = ac->toc;
3006 data->opd.env = ac->env;
3007
3008 data->AsmCall = ac->function;
3009 data->BlockCopy = bc->function;
3010 #else
3011 data->AsmCall = VM_AsmCall;
3012 data->BlockCopy = VM_BlockCopy;
3013 #endif
3014
3015 data->dataMask = vm->dataMask;
3016 data->iPointers = (ppc_instruction_t *)vm->instructionPointers;
3017 data->dataLength = VM_Data_Offset( data[ data_acc ] );
3018 data->codeLength = ( codeNow - codeBegin ) * sizeof( ppc_instruction_t );
3019 data->floatBase = 0x59800004;
3020
3021
3022 /* write dynamic data (float constants) */
3023 {
3024 local_data_t *d_next, *d_now = data_first;
3025 long int accumulated = 0;
3026
3027 do {
3028 long int i;
3029 for ( i = 0; i < d_now->count; i++ )
3030 data->data[ accumulated + i ] = d_now->data[ i ];
3031
3032 accumulated += d_now->count;
3033 d_next = d_now->next;
3034 PPC_Free( d_now );
3035
3036 if ( !d_next )
3037 break;
3038 d_now = d_next;
3039 } while (1);
3040 data_first = NULL;
3041 }
3042
3043 /* free most of the compilation memory */
3044 {
3045 di_now = di_first->next;
3046 PPC_Free( di_first );
3047 PPC_Free( sj_first );
3048
3049 while ( di_now ) {
3050 di_first = di_now->next;
3051 if ( di_now->jump )
3052 PPC_Free( di_now->jump );
3053 PPC_Free( di_now );
3054 di_now = di_first;
3055 }
3056 }
3057 }
3058
3059 static void
3060 VM_Destroy_Compiled( vm_t *self )
3061 {
3062 if ( self->codeBase ) {
3063 if ( munmap( self->codeBase, self->codeLength ) )
3064 Com_Printf( S_COLOR_RED "Memory unmap failed, possible memory leak\n" );
3065 }
3066 self->codeBase = NULL;
3067 }
3068
3069 void
3070 VM_Compile( vm_t *vm, vmHeader_t *header )
3071 {
3072 long int pc = 0;
3073 unsigned long int i_count;
3074 char* code;
3075 struct timeval tvstart = {0, 0};
3076 source_instruction_t *i_first /* dummy */, *i_last = NULL, *i_now;
3077
3078 vm->compiled = qfalse;
3079
3080 gettimeofday(&tvstart, NULL);
3081
3082 PPC_MakeFastMask( vm->dataMask );
3083
3084 i_first = PPC_Malloc( sizeof( source_instruction_t ) );
3085 i_first->next = NULL;
3086
3087 // realloc instructionPointers with correct size
3088 // use Z_Malloc so vm.c will be able to free the memory
3089 if ( sizeof( void * ) != sizeof( int ) ) {
3090 Z_Free( vm->instructionPointers );
3091 vm->instructionPointers = Z_Malloc( header->instructionCount * sizeof( void * ) );
3092 }
3093 di_pointers = (void *)vm->instructionPointers;
3094 memset( di_pointers, 0, header->instructionCount * sizeof( void * ) );
3095
3096
3097 PPC_CompileInit();
3098
3099 /*
3100 * read the input program
3101 * divide it into functions and send each function to compiler
3102 */
3103 code = (char *)header + header->codeOffset;
3104 for ( i_count = 0; i_count < header->instructionCount; ++i_count )
3105 {
3106 unsigned char op = code[ pc++ ];
3107
3108 if ( op == OP_ENTER ) {
3109 if ( i_first->next )
3110 VM_CompileFunction( i_first );
3111 i_first->next = NULL;
3112 i_last = i_first;
3113 }
3114
3115 i_now = PPC_Malloc( sizeof( source_instruction_t ) );
3116 i_now->op = op;
3117 i_now->i_count = i_count;
3118 i_now->arg.i = 0;
3119 i_now->regA1 = 0;
3120 i_now->regA2 = 0;
3121 i_now->regR = 0;
3122 i_now->regPos = 0;
3123 i_now->next = NULL;
3124
3125 if ( vm_opInfo[op] & opImm4 ) {
3126 union {
3127 unsigned char b[4];
3128 unsigned int i;
3129 } c = { { code[ pc + 3 ], code[ pc + 2 ], code[ pc + 1 ], code[ pc + 0 ] }, };
3130
3131 i_now->arg.i = c.i;
3132 pc += 4;
3133 } else if ( vm_opInfo[op] & opImm1 ) {
3134 i_now->arg.b = code[ pc++ ];
3135 }
3136
3137 i_last->next = i_now;
3138 i_last = i_now;
3139 }
3140 VM_CompileFunction( i_first );
3141 PPC_Free( i_first );
3142
3143 PPC_ShrinkJumps();
3144 memset( di_pointers, 0, header->instructionCount * sizeof( void * ) );
3145 PPC_ComputeCode( vm );
3146
3147 /* check for uninitialized pointers */
3148 #ifdef DEBUG_VM
3149 long int i;
3150 for ( i = 0; i < header->instructionCount; i++ )
3151 if ( di_pointers[ i ] == 0 )
3152 Com_Printf( S_COLOR_RED "Pointer %ld not initialized !\n", i );
3153 #endif
3154
3155 /* mark memory as executable and not writeable */
3156 if ( mprotect( vm->codeBase, vm->codeLength, PROT_READ|PROT_EXEC ) ) {
3157
3158 // it has failed, make sure memory is unmapped before throwing the error
3159 VM_Destroy_Compiled( vm );
3160 DIE( "mprotect failed" );
3161 }
3162
3163 vm->destroy = VM_Destroy_Compiled;
3164 vm->compiled = qtrue;
3165
3166 {
3167 struct timeval tvdone = {0, 0};
3168 struct timeval dur = {0, 0};
3169 Com_Printf( "VM file %s compiled to %i bytes of code (%p - %p)\n",
3170 vm->name, vm->codeLength, vm->codeBase, vm->codeBase+vm->codeLength );
3171
3172 gettimeofday(&tvdone, NULL);
3173 timersub(&tvdone, &tvstart, &dur);
3174 Com_Printf( "compilation took %lu.%06lu seconds\n",
3175 (long unsigned int)dur.tv_sec, (long unsigned int)dur.tv_usec );
3176 }
3177 }
3178
3179 int
3180 VM_CallCompiled( vm_t *vm, int *args )
3181 {
3182 int retVal;
3183 int *argPointer;
3184
3185 vm_data_t *vm_dataAndCode = (void *)( vm->codeBase );
3186 int programStack = vm->programStack;
3187 int stackOnEntry = programStack;
3188
3189 byte *image = vm->dataBase;
3190
3191 currentVM = vm;
3192
3193 vm->currentlyInterpreting = qtrue;
3194
3195 programStack -= ( 8 + 4 * MAX_VMMAIN_ARGS );
3196 argPointer = (int *)&image[ programStack + 8 ];
3197 memcpy( argPointer, args, 4 * MAX_VMMAIN_ARGS );
3198 argPointer[ -1 ] = 0;
3199 argPointer[ -2 ] = -1;
3200
3201 #ifdef VM_TIMES
3202 struct tms start_time, stop_time;
3203 clock_t time_diff;
3204
3205 times( &start_time );
3206 time_outside_vm = 0;
3207 #endif
3208
3209 /* call generated code */
3210 {
3211 int ( *entry )( void *, int, void * );
3212 #ifdef __PPC64__
3213 entry = (void *)&(vm_dataAndCode->opd);
3214 #else
3215 entry = (void *)(vm->codeBase + vm_dataAndCode->dataLength);
3216 #endif
3217 retVal = entry( vm->codeBase, programStack, vm->dataBase );
3218 }
3219
3220 #ifdef VM_TIMES
3221 times( &stop_time );
3222 time_diff = stop_time.tms_utime - start_time.tms_utime;
3223 time_total_vm += time_diff - time_outside_vm;
3224 if ( time_diff > 100 ) {
3225 printf( "App clock: %ld, vm total: %ld, vm this: %ld, vm real: %ld, vm out: %ld\n"
3226 "Inside VM %f%% of app time\n",
3227 stop_time.tms_utime,
3228 time_total_vm,
3229 time_diff,
3230 time_diff - time_outside_vm,
3231 time_outside_vm,
3232 (double)100 * time_total_vm / stop_time.tms_utime );
3233 }
3234 #endif
3235
3236 vm->programStack = stackOnEntry;
3237 vm->currentlyInterpreting = qfalse;
3238
3239 return retVal;
3240 }
3241
3242 #endif // powerpc