Now longs can be printed, IDIVPOW2 back in.
[cacao.git] / i386 / ngen.h
1 /* i386/ngen.h *****************************************************************
2
3     Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
4
5     See file COPYRIGHT for information on usage and disclaimer of warranties
6
7     Contains the machine dependent code generator definitions and macros for an
8     Alpha processor.
9
10     Authors: Andreas  Krall      EMAIL: cacao@complang.tuwien.ac.at
11              Reinhard Grafl      EMAIL: cacao@complang.tuwien.ac.at
12
13     Last Change: 1998/11/04
14
15 *******************************************************************************/
16
17 #ifndef _NGEN_H
18 #define _NGEN_H
19
20 /* see also file calling.doc for explanation of calling conventions           */
21
22 /* preallocated registers *****************************************************/
23
24 /* integer registers */
25   
26 #define REG_RESULT      I386_EAX /* to deliver method results                 */
27 #define REG_RESULT2     I386_EDX /* to deliver long method results            */
28
29 #define REG_RA          26   /* return address                                */
30 #define REG_PV          27   /* procedure vector, must be provided by caller  */
31 #define REG_METHODPTR   28   /* pointer to the place from where the procedure */
32                              /* vector has been fetched                       */
33 #define REG_ITMP1       I386_EAX /* temporary register                        */
34 #define REG_ITMP2       I386_EDX /* temporary register and method pointer     */
35 #define REG_ITMP3       I386_ECX /* temporary register                        */
36
37 #define REG_ITMP1_XPTR  I386_EAX /* exception pointer = temporary register 1  */
38 #define REG_ITMP2_XPC   I386_EDX /* exception pc = temporary register 2       */
39
40 #define REG_SP          I386_ESP /* stack pointer                             */
41 #define REG_ZERO        31   /* always zero                                   */
42
43 /* floating point registers */
44
45 #define REG_FRESULT     0    /* to deliver floating point method results      */
46 #define REG_FTMP1       0    /* temporary floating point register             */
47 #define REG_FTMP2       1    /* temporary floating point register             */
48 #define REG_FTMP3       2    /* temporary floating point register             */
49
50 #define REG_IFTMP       28   /* temporary integer and floating point register */
51
52 /* register descripton - array ************************************************/
53
54 /* #define REG_RES   0         reserved register for OS or code generator     */
55 /* #define REG_RET   1         return value register                          */
56 /* #define REG_EXC   2         exception value register (only old jit)        */
57 /* #define REG_SAV   3         (callee) saved register                        */
58 /* #define REG_TMP   4         scratch temporary register (caller saved)      */
59 /* #define REG_ARG   5         argument register (caller saved)               */
60
61 /* #define REG_END   -1        last entry in tables */
62
63 int nregdescint[] = {
64     REG_RET, REG_RES, REG_RES, REG_RES, REG_RES, REG_RES, REG_TMP, REG_TMP,
65     REG_END };
66
67 /* for use of reserved registers, see comment above */
68
69 int nregdescfloat[] = {
70     REG_RES, REG_RES, REG_RES, REG_RES, REG_RES, REG_RES, REG_RES, REG_RES,
71     REG_END };
72
73 /* for use of reserved registers, see comment above */
74
75
76 /* parameter allocation mode */
77
78 int nreg_parammode = PARAMMODE_NUMBERED;  
79
80    /* parameter-registers will be allocated by assigning the
81       1. parameter:   int/float-reg 16
82       2. parameter:   int/float-reg 17  
83       3. parameter:   int/float-reg 18 ....
84    */
85
86
87 /* stackframe-infos ***********************************************************/
88
89 int parentargs_base; /* offset in stackframe for the parameter from the caller*/
90
91 /* -> see file 'calling.doc' */
92
93
94 /* macros to create code ******************************************************/
95
96 #define M_OP3(op,fu,a,b,c,const) \
97 do { \
98         printf("M_OP3: %d\n", __LINE__); \
99         *((s4 *) (((s4) mcodeptr)++)) = 0x0F; \
100         *((s4 *) (((s4) mcodeptr)++)) = 0x04; \
101 } while (0)
102
103 #define M_FOP3(op,fu,a,b,c)     M_OP3(0,0,0,0,0,0)
104 #define M_BRA(op,a,disp)        M_OP3(0,0,0,0,0,0)
105 #define M_MEM(op,a,b,disp)      M_OP3(0,0,0,0,0,0)
106
107
108
109 /*
110  * immediate data union
111  */
112 typedef union {
113     s4 i;
114     s8 l;
115     float f;
116     double d;
117     void *a;
118     u1 b[8];
119 } i386_imm_buf;
120
121
122 /*
123  * x86 register numbers
124  */
125 typedef enum {
126     I386_EAX = 0,
127     I386_ECX = 1,
128     I386_EDX = 2,
129     I386_EBX = 3,
130     I386_ESP = 4,
131     I386_EBP = 5,
132     I386_ESI = 6,
133     I386_EDI = 7,
134     I386_NREG
135 } I386_Reg_No;
136
137 /*
138  * opcodes for alu instructions
139  */
140 typedef enum {
141     I386_ADD = 0,
142     I386_OR  = 1,
143     I386_ADC = 2,
144     I386_SBB = 3,
145     I386_AND = 4,
146     I386_SUB = 5,
147     I386_XOR = 6,
148     I386_CMP = 7,
149     I386_NALU
150 } I386_ALU_Opcode;
151
152 typedef enum {
153     I386_ROL = 0,
154     I386_ROR = 1,
155     I386_RCL = 2,
156     I386_RCR = 3,
157     I386_SHL = 4,
158     I386_SHR = 5,
159     I386_SAR = 7,
160     I386_NSHIFT = 8
161 } I386_Shift_Opcode;
162
163 typedef enum {
164     I386_CC_O = 0,
165     I386_CC_NO = 1,
166     I386_CC_B = 2, I386_CC_C = 2, I386_CC_NAE = 2,
167     I386_CC_BE = 6, I386_CC_NA = 6,
168     I386_CC_AE = 3, I386_CC_NB = 3, I386_CC_NC = 3,
169     I386_CC_E = 4, I386_CC_Z = 4,
170     I386_CC_NE = 5, I386_CC_NZ = 5,
171     I386_CC_A = 7, I386_CC_NBE = 7,
172     I386_CC_S = 8, I386_CC_LZ = 8,
173     I386_CC_NS = 9, I386_CC_GEZ = 9,
174     I386_CC_P = 0x0a, I386_CC_PE = 0x0a,
175     I386_CC_NP = 0x0b, I386_CC_PO = 0x0b,
176     I386_CC_L = 0x0c, I386_CC_NGE = 0x0c,
177     I386_CC_GE = 0x0d, I386_CC_NL = 0x0d,
178     I386_CC_LE = 0x0e, I386_CC_NG = 0x0e,
179     I386_CC_G = 0x0f, I386_CC_NLE = 0x0f,
180     I386_NCC
181 } I386_CC;
182
183 static const unsigned char i386_jcc_map[] = {
184     0x00, /* o  */
185     0x01, /* no */
186     0x02, /* b, lt  */
187     0x03, /* ae */
188     0x04, /* e  */
189     0x05, /* ne */
190     0x06, /* be */
191     0x07, /* a  */
192     0x08, /* s  */
193     0x09, /* ns */
194     0x0a, /* p  */
195     0x0b, /* np */
196     0x0c, /* l  */
197     0x0d, /* ge */
198     0x0e, /* le */
199     0x0f  /* g  */
200 };
201
202
203
204 /*
205  * modrm and stuff
206  */
207 #define i386_address_byte(mod, reg, rm) \
208     do { \
209         *(((u1 *) mcodeptr)++) = ((((mod) & 0x03) << 6) | (((reg) & 0x07) << 3) | (((rm) & 0x07))); \
210     } while (0)
211
212
213 #define i386_emit_reg(reg,rm) \
214     do { \
215         i386_address_byte(3,(reg),(rm)); \
216     } while (0)
217
218
219 #define i386_is_imm8(imm)       (((int)(imm) >= -128 && (int)(imm) <= 127))
220
221
222 #define i386_emit_imm8(imm) \
223     do { \
224         *(((u1 *) mcodeptr)++) = (u1) ((imm) & 0xff); \
225     } while (0)
226
227
228 #define i386_emit_imm16(imm) \
229     do { \
230         i386_imm_buf imb; \
231         imb.i = (int) (imm); \
232         *(((u1 *) mcodeptr)++) = imb.b[0]; \
233         *(((u1 *) mcodeptr)++) = imb.b[1]; \
234     } while (0)
235
236
237 #define i386_emit_imm32(imm) \
238     do { \
239         i386_imm_buf imb; \
240         imb.i = (int) (imm); \
241         *(((u1 *) mcodeptr)++) = imb.b[0]; \
242         *(((u1 *) mcodeptr)++) = imb.b[1]; \
243         *(((u1 *) mcodeptr)++) = imb.b[2]; \
244         *(((u1 *) mcodeptr)++) = imb.b[3]; \
245     } while (0)
246
247
248 #define i386_emit_float32(imm) \
249     do { \
250         i386_imm_buf imb; \
251         imb.f = (float) (imm); \
252         *(((u1 *) mcodeptr)++) = imb.b[0]; \
253         *(((u1 *) mcodeptr)++) = imb.b[1]; \
254         *(((u1 *) mcodeptr)++) = imb.b[2]; \
255         *(((u1 *) mcodeptr)++) = imb.b[3]; \
256     } while (0)
257
258
259 #define i386_emit_mem(r,disp) \
260     do { \
261         i386_address_byte(0,(r),5); \
262         i386_emit_imm32((disp)); \
263     } while (0)
264
265
266 #define i386_emit_membase(basereg,disp,dreg) \
267     do { \
268         if ((basereg) == I386_ESP) { \
269             if ((disp) == 0) { \
270                 i386_address_byte(0, (dreg), I386_ESP); \
271                 i386_address_byte(0, I386_ESP, I386_ESP); \
272             } else if (i386_is_imm8((disp))) { \
273                 i386_address_byte(1, (dreg), I386_ESP); \
274                 i386_address_byte(0, I386_ESP, I386_ESP); \
275                 i386_emit_imm8((disp)); \
276             } else { \
277                 i386_address_byte(2, (dreg), I386_ESP); \
278                 i386_address_byte(0, I386_ESP, I386_ESP); \
279                 i386_emit_imm32((disp)); \
280             } \
281             break; \
282         } \
283         \
284         if ((disp) == 0 && (basereg) != I386_EBP) { \
285             i386_address_byte(0, (dreg), (basereg)); \
286             break; \
287         } \
288         \
289         if (i386_is_imm8((disp))) { \
290             i386_address_byte(1, (dreg), (basereg)); \
291             i386_emit_imm8((disp)); \
292         } else { \
293             i386_address_byte(2, (dreg), (basereg)); \
294             i386_emit_imm32((disp)); \
295         } \
296     } while (0)
297
298 #define i386_emit_memindex(reg,disp,basereg,indexreg,scale) \
299     do { \
300         if ((basereg) == -1) { \
301             i386_address_byte(0, (reg), 4); \
302             i386_address_byte((scale), (indexreg), 5); \
303             i386_emit_imm32((disp)); \
304         \
305         } else if ((disp) == 0 && (basereg) != I386_EBP) { \
306             i386_address_byte(0, (reg), 4); \
307             i386_address_byte((scale), (indexreg), (basereg)); \
308         \
309         } else if (i386_is_imm8((disp))) { \
310             i386_address_byte(1, (reg), 4); \
311             i386_address_byte((scale), (indexreg), (basereg)); \
312             i386_emit_imm8 ((disp)); \
313         \
314         } else { \
315             i386_address_byte(2, (reg), 4); \
316             i386_address_byte((scale), (indexreg), (basereg)); \
317             i386_emit_imm32((disp)); \
318         }    \
319      } while (0)
320
321
322
323 /*
324  * mov ops
325  */
326 #define i386_mov_reg_reg(reg,dreg) \
327     do { \
328         *(((u1 *) mcodeptr)++) = (u1) 0x89; \
329         i386_emit_reg((reg),(dreg)); \
330     } while (0)
331
332
333 #define i386_mov_imm_reg(imm,reg) \
334     do { \
335         *(((u1 *) mcodeptr)++) = (u1) 0xb8 + ((reg) & 0x07); \
336         i386_emit_imm32((imm)); \
337     } while (0)
338
339
340 #define i386_mov_float_reg(imm,reg) \
341     do { \
342         *(((u1 *) mcodeptr)++) = (u1) 0xb8 + ((reg) & 0x07); \
343         i386_emit_float32((imm)); \
344     } while (0)
345
346
347 #define i386_mov_reg_mem(reg,mem) \
348     do { \
349         *(((u1 *) mcodeptr)++) = (u1) 0x89; \
350         i386_emit_mem((reg),(mem)); \
351     } while (0)
352
353
354 #define i386_mov_mem_reg(mem,reg) \
355     do { \
356         *(((u1 *) mcodeptr)++) = (u1) 0x8b; \
357         i386_emit_mem((reg),(mem)); \
358     } while (0)
359
360
361 #define i386_mov_membase_reg(basereg,disp,reg) \
362     do { \
363         *(((u1 *) mcodeptr)++) = (u1) 0x8b; \
364         i386_emit_membase((basereg),(disp),(reg)); \
365     } while (0)
366
367
368 /*
369  * this one is for INVOKEVIRTUAL/INVOKEINTERFACE to have a
370  * constant membase immediate length of 32bit
371  */
372 #define i386_mov_membase32_reg(basereg,disp,reg) \
373     do { \
374         *(((u1 *) mcodeptr)++) = (u1) 0x8b; \
375         i386_address_byte(2, (reg), (basereg)); \
376         i386_emit_imm32((disp)); \
377     } while (0)
378
379
380 #define i386_movw_membase_reg(basereg,disp,reg) \
381     do { \
382         *(((u1 *) mcodeptr)++) = (u1) 0x66; \
383         i386_mov_membase_reg((basereg),(disp),(reg)); \
384     } while (0)
385
386
387 #define i386_movb_membase_reg(basereg,disp,reg) \
388     do { \
389         *(((u1 *) mcodeptr)++) = (u1) 0x8a; \
390         i386_emit_membase((basereg),(disp),(reg)); \
391     } while (0)
392
393
394 #define i386_mov_reg_membase(reg,basereg,disp) \
395     do { \
396         *(((u1 *) mcodeptr)++) = (u1) 0x89; \
397         i386_emit_membase((basereg),(disp),(reg)); \
398     } while (0)
399
400
401 #define i386_movw_reg_membase(reg,basereg,disp) \
402     do { \
403         *(((u1 *) mcodeptr)++) = (u1) 0x66; \
404         *(((u1 *) mcodeptr)++) = (u1) 0x89; \
405         i386_emit_membase((basereg),(disp),(reg)); \
406     } while (0)
407
408
409 #define i386_movb_reg_membase(reg,basereg,disp) \
410     do { \
411         *(((u1 *) mcodeptr)++) = (u1) 0x88; \
412         i386_emit_membase((basereg),(disp),(reg)); \
413     } while (0)
414
415
416 #define i386_mov_memindex_reg(disp,basereg,indexreg,scale,reg) \
417     do { \
418         *(((u1 *) mcodeptr)++) = (u1) 0x8b; \
419         i386_emit_memindex((reg),(disp),(basereg),(indexreg),(scale)); \
420     } while (0)
421
422
423 #define i386_movw_memindex_reg(disp,basereg,indexreg,scale,reg) \
424     do { \
425         *(((u1 *) mcodeptr)++) = (u1) 0x66; \
426         *(((u1 *) mcodeptr)++) = (u1) 0x8b; \
427         i386_emit_memindex((reg),(disp),(basereg),(indexreg),(scale)); \
428     } while (0)
429
430
431 #define i386_mov_reg_memindex(reg,disp,basereg,indexreg,scale) \
432     do { \
433         *(((u1 *) mcodeptr)++) = (u1) 0x89; \
434         i386_emit_memindex((reg),(disp),(basereg),(indexreg),(scale)); \
435     } while (0)
436
437
438 #define i386_movw_reg_memindex(reg,disp,basereg,indexreg,scale) \
439     do { \
440         *(((u1 *) mcodeptr)++) = (u1) 0x66; \
441         *(((u1 *) mcodeptr)++) = (u1) 0x89; \
442         i386_emit_memindex((reg),(disp),(basereg),(indexreg),(scale)); \
443     } while (0)
444
445
446 #define i386_movb_reg_memindex(reg,disp,basereg,indexreg,scale) \
447     do { \
448         *(((u1 *) mcodeptr)++) = (u1) 0x88; \
449         i386_emit_memindex((reg),(disp),(basereg),(indexreg),(scale)); \
450     } while (0)
451
452
453 #define i386_mov_imm_membase(imm,basereg,disp) \
454     do { \
455         *(((u1 *) mcodeptr)++) = (u1) 0xc7; \
456         i386_emit_membase((basereg),(disp),0); \
457         i386_emit_imm32((imm)); \
458     } while (0)
459
460
461 #define i386_mov_float_membase(imm,basereg,disp) \
462     do { \
463         *(((u1 *) mcodeptr)++) = (u1) 0xc7; \
464         i386_emit_membase((basereg),(disp),0); \
465         i386_emit_float32((imm)); \
466     } while (0)
467
468
469 #define i386_movsbl_reg_reg(reg,dreg) \
470     do { \
471         *(((u1 *) mcodeptr)++) = (u1) 0x0f; \
472         *(((u1 *) mcodeptr)++) = (u1) 0xbe; \
473         i386_emit_reg((reg),(dreg)); \
474     } while (0)
475
476
477 #define i386_movswl_reg_reg(reg,dreg) \
478     do { \
479         *(((u1 *) mcodeptr)++) = (u1) 0x0f; \
480         *(((u1 *) mcodeptr)++) = (u1) 0xbf; \
481         i386_emit_reg((reg),(dreg)); \
482     } while (0)
483
484
485 #define i386_movsbl_memindex_reg(disp,basereg,indexreg,scale,reg) \
486     do { \
487         *(((u1 *) mcodeptr)++) = (u1) 0x0f; \
488         *(((u1 *) mcodeptr)++) = (u1) 0xbe; \
489         i386_emit_memindex((reg),(disp),(basereg),(indexreg),(scale)); \
490     } while (0)
491
492
493 #define i386_movswl_memindex_reg(disp,basereg,indexreg,scale,reg) \
494     do { \
495         *(((u1 *) mcodeptr)++) = (u1) 0x0f; \
496         *(((u1 *) mcodeptr)++) = (u1) 0xbf; \
497         i386_emit_memindex((reg),(disp),(basereg),(indexreg),(scale)); \
498     } while (0)
499
500
501 #define i386_movzbl_memindex_reg(disp,basereg,indexreg,scale,reg) \
502     do { \
503         *(((u1 *) mcodeptr)++) = (u1) 0x0f; \
504         *(((u1 *) mcodeptr)++) = (u1) 0xb6; \
505         i386_emit_memindex((reg),(disp),(basereg),(indexreg),(scale)); \
506     } while (0)
507
508
509 #define i386_movzwl_memindex_reg(disp,basereg,indexreg,scale,reg) \
510     do { \
511         *(((u1 *) mcodeptr)++) = (u1) 0x0f; \
512         *(((u1 *) mcodeptr)++) = (u1) 0xb7; \
513         i386_emit_memindex((reg),(disp),(basereg),(indexreg),(scale)); \
514     } while (0)
515
516
517
518 /*
519  * alu operations
520  */
521 #define i386_alu_reg_reg(opc,reg,dreg) \
522     do { \
523         *(((u1 *) mcodeptr)++) = (((u1) (opc)) << 3) + 1; \
524         i386_emit_reg((reg),(dreg)); \
525     } while (0)
526
527
528 #define i386_alu_reg_membase(opc,reg,basereg,disp) \
529     do { \
530         *(((u1 *) mcodeptr)++) = (((u1) (opc)) << 3) + 1; \
531         i386_emit_membase((basereg),(disp),(reg)); \
532     } while (0)
533
534
535 #define i386_alu_membase_reg(opc,basereg,disp,reg) \
536     do { \
537         *(((u1 *) mcodeptr)++) = (((u1) (opc)) << 3) + 3; \
538         i386_emit_membase((basereg),(disp),(reg)); \
539     } while (0)
540
541
542 #define i386_alu_imm_reg(opc,imm,dreg) \
543     do { \
544         if (i386_is_imm8(imm)) { \
545             *(((u1 *) mcodeptr)++) = (u1) 0x83; \
546             i386_emit_reg((opc),(dreg)); \
547             i386_emit_imm8((imm)); \
548         } else { \
549             *(((u1 *) mcodeptr)++) = (u1) 0x81; \
550             i386_emit_reg((opc),(dreg)); \
551             i386_emit_imm32((imm)); \
552         } \
553     } while (0)
554
555
556 #define i386_alu_imm_membase(opc,imm,basereg,disp) \
557     do { \
558         if (i386_is_imm8(imm)) { \
559             *(((u1 *) mcodeptr)++) = (u1) 0x83; \
560             i386_emit_membase((basereg),(disp),(opc)); \
561             i386_emit_imm8((imm)); \
562         } else { \
563             *(((u1 *) mcodeptr)++) = (u1) 0x81; \
564             i386_emit_membase((basereg),(disp),(opc)); \
565             i386_emit_imm32((imm)); \
566         } \
567     } while (0)
568
569
570 #define i386_test_reg_reg(reg,dreg) \
571     do { \
572         *(((u1 *) mcodeptr)++) = (u1) 0x85; \
573         i386_emit_reg((reg),(dreg)); \
574     } while (0)
575
576
577 #define i386_test_imm_reg(imm,reg) \
578     do { \
579         *(((u1 *) mcodeptr)++) = (u1) 0xf7; \
580         i386_emit_reg(0,(reg)); \
581         i386_emit_imm32((imm)); \
582     } while (0)
583
584
585 #define i386_testw_imm_reg(imm,reg) \
586     do { \
587         *(((u1 *) mcodeptr)++) = (u1) 0x66; \
588         *(((u1 *) mcodeptr)++) = (u1) 0xf7; \
589         i386_emit_reg(0,(reg)); \
590         i386_emit_imm16((imm)); \
591     } while (0)
592
593
594 #define i386_testb_imm_reg(imm,reg) \
595     do { \
596         *(((u1 *) mcodeptr)++) = (u1) 0xf6; \
597         i386_emit_reg(0,(reg)); \
598         i386_emit_imm8((imm)); \
599     } while (0)
600
601
602 #define i386_shld_reg_reg(reg,dreg) \
603     do { \
604         *(((u1 *) mcodeptr)++) = (u1) 0x0f; \
605         *(((u1 *) mcodeptr)++) = (u1) 0xa5; \
606         i386_emit_reg((reg),(dreg)); \
607     } while (0)
608
609
610 #define i386_shld_reg_membase(reg,basereg,disp) \
611     do { \
612         *(((u1 *) mcodeptr)++) = (u1) 0x0f; \
613         *(((u1 *) mcodeptr)++) = (u1) 0xa5; \
614         i386_emit_membase((basereg),(disp),(reg)); \
615     } while (0)
616
617
618 #define i386_shrd_reg_reg(reg,dreg) \
619     do { \
620         *(((u1 *) mcodeptr)++) = (u1) 0x0f; \
621         *(((u1 *) mcodeptr)++) = (u1) 0xad; \
622         i386_emit_reg((reg),(dreg)); \
623     } while (0)
624
625
626 #define i386_shrd_reg_membase(reg,basereg,disp) \
627     do { \
628         *(((u1 *) mcodeptr)++) = (u1) 0x0f; \
629         *(((u1 *) mcodeptr)++) = (u1) 0xad; \
630         i386_emit_membase((basereg),(disp),(reg)); \
631     } while (0)
632
633
634
635 /*
636  * inc, dec operations
637  */
638 #define i386_inc_reg(reg) \
639     do { \
640         *(((u1 *) mcodeptr)++) = (u1) 0x40 + ((reg) & 0x07); \
641     } while (0)
642
643
644 #define i386_inc_membase(basereg,disp) \
645     do { \
646         *(((u1 *) mcodeptr)++) = (u1) 0xff; \
647         i386_emit_membase((basereg),(disp),0); \
648     } while (0)
649
650
651 #define i386_dec_reg(reg) \
652     do { \
653         *(((u1 *) mcodeptr)++) = (u1) 0x48 + ((reg) & 0x07); \
654     } while (0)
655
656         
657 #define i386_dec_membase(basereg,disp) \
658     do { \
659         *(((u1 *) mcodeptr)++) = (u1) 0xff; \
660         i386_emit_membase((basereg),(disp),1); \
661     } while (0)
662
663
664
665
666 #define i386_cltd() \
667     do { \
668         *(((u1 *) mcodeptr)++) = (u1) 0x99; \
669     } while (0)
670
671
672
673 #define i386_imul_reg_reg(reg,dreg) \
674     do { \
675         *(((u1 *) mcodeptr)++) = (u1) 0x0f; \
676         *(((u1 *) mcodeptr)++) = (u1) 0xaf; \
677         i386_emit_reg((dreg),(reg)); \
678     } while (0)
679
680
681 #define i386_imul_membase_reg(basereg,disp,dreg) \
682     do { \
683         *(((u1 *) mcodeptr)++) = (u1) 0x0f; \
684         *(((u1 *) mcodeptr)++) = (u1) 0xaf; \
685         i386_emit_membase((basereg),(disp),(dreg)); \
686     } while (0)
687
688
689 #define i386_imul_imm_reg(imm,dreg) \
690     do { \
691         if (i386_is_imm8((imm))) { \
692             *(((u1 *) mcodeptr)++) = (u1) 0x6b; \
693             i386_emit_reg(0,(dreg)); \
694             i386_emit_imm8((imm)); \
695         } else { \
696             *(((u1 *) mcodeptr)++) = (u1) 0x69; \
697             i386_emit_reg(0,(dreg)); \
698             i386_emit_imm32((imm)); \
699         } \
700     } while (0)
701
702
703 #define i386_imul_imm_reg_reg(imm,reg,dreg) \
704     do { \
705         if (i386_is_imm8((imm))) { \
706             *(((u1 *) mcodeptr)++) = (u1) 0x6b; \
707             i386_emit_reg((dreg),(reg)); \
708             i386_emit_imm8((imm)); \
709         } else { \
710             *(((u1 *) mcodeptr)++) = (u1) 0x69; \
711             i386_emit_reg((dreg),(reg)); \
712             i386_emit_imm32((imm)); \
713         } \
714     } while (0)
715
716
717 #define i386_imul_imm_membase_reg(imm,basereg,disp,dreg) \
718     do { \
719         if (i386_is_imm8((imm))) { \
720             *(((u1 *) mcodeptr)++) = (u1) 0x6b; \
721             i386_emit_membase((basereg),(disp),(dreg)); \
722             i386_emit_imm8((imm)); \
723         } else { \
724             *(((u1 *) mcodeptr)++) = (u1) 0x69; \
725             i386_emit_membase((basereg),(disp),(dreg)); \
726             i386_emit_imm32((imm)); \
727         } \
728     } while (0)
729
730
731 #define i386_mul_reg(reg) \
732     do { \
733         *(((u1 *) mcodeptr)++) = (u1) 0xf7; \
734         i386_emit_reg(4,(reg)); \
735     } while (0)
736
737
738 #define i386_mul_membase(basereg,disp) \
739     do { \
740         *(((u1 *) mcodeptr)++) = (u1) 0xf7; \
741         i386_emit_membase((basereg),(disp),4); \
742     } while (0)
743
744
745 #define i386_idiv_reg(reg) \
746     do { \
747         *(((u1 *) mcodeptr)++) = (u1) 0xf7; \
748         i386_emit_reg(7,(reg)); \
749     } while (0)
750
751
752 #define i386_idiv_membase(basereg,disp) \
753     do { \
754         *(((u1 *) mcodeptr)++) = (u1) 0xf7; \
755         i386_emit_membase((basereg),(disp),7); \
756     } while (0)
757
758
759
760 #define i386_ret() \
761     do { \
762         *(((u1 *) mcodeptr)++) = (u1) 0xc3; \
763     } while (0)
764
765
766 #define i386_leave() \
767     do { \
768         *(((u1 *) mcodeptr)++) = (u1) 0xc9; \
769     } while (0)
770
771
772
773 /*
774  * shift ops
775  */
776 #define i386_shift_reg(opc,reg) \
777     do { \
778         *(((u1 *) mcodeptr)++) = (u1) 0xd3; \
779         i386_emit_reg((opc),(reg)); \
780     } while (0)
781
782
783 #define i386_shift_membase(opc,basereg,disp) \
784     do { \
785         *(((u1 *) mcodeptr)++) = (u1) 0xd3; \
786         i386_emit_membase((basereg),(disp),(opc)); \
787     } while (0)
788
789
790 #define i386_shift_imm_reg(opc,imm,dreg) \
791     do { \
792         if ((imm) == 1) { \
793             *(((u1 *) mcodeptr)++) = (u1) 0xd1; \
794             i386_emit_reg((opc),(dreg)); \
795         } else { \
796             *(((u1 *) mcodeptr)++) = (u1) 0xc1; \
797             i386_emit_reg((opc),(dreg)); \
798             i386_emit_imm8((imm)); \
799         } \
800     } while (0)
801
802
803 #define i386_shift_imm_membase(opc,imm,basereg,disp) \
804     do { \
805         if ((imm) == 1) { \
806             *(((u1 *) mcodeptr)++) = (u1) 0xd1; \
807             i386_emit_membase((basereg),(disp),(opc)); \
808         } else { \
809             *(((u1 *) mcodeptr)++) = (u1) 0xc1; \
810             i386_emit_membase((basereg),(disp),(opc)); \
811             i386_emit_imm8((imm)); \
812         } \
813     } while (0)
814
815
816
817 /*
818  * jump operations
819  */
820 #define i386_jmp(imm) \
821     do { \
822         *(((u1 *) mcodeptr)++) = (u1) 0xe9; \
823         i386_emit_imm32((imm)); \
824     } while (0)
825
826
827 #define i386_jmp_reg(reg) \
828     do { \
829         *(((u1 *) mcodeptr)++) = (u1) 0xff; \
830         i386_emit_reg(4,(reg)); \
831     } while (0)
832
833
834 #define i386_jcc(opc,imm) \
835     do { \
836         *(((u1 *) mcodeptr)++) = (u1) 0x0f; \
837         *(((u1 *) mcodeptr)++) = (u1) (0x80 + i386_jcc_map[(opc)]); \
838         i386_emit_imm32((imm)); \
839     } while (0)
840
841
842
843 /*
844  * conditional set operations
845  */
846 #define i386_setcc_reg(opc,reg) \
847     do { \
848         *(((u1 *) mcodeptr)++) = (u1) 0x0f; \
849         *(((u1 *) mcodeptr)++) = (u1) (0x90 + i386_jcc_map[(opc)]); \
850         i386_emit_reg(0,(reg)); \
851     } while (0)
852
853
854 #define i386_setcc_membase(opc,basereg,disp) \
855     do { \
856         *(((u1 *) mcodeptr)++) = (u1) 0x0f; \
857         *(((u1 *) mcodeptr)++) = (u1) (0x90 + i386_jcc_map[(opc)]); \
858         i386_emit_membase((basereg),(disp),0); \
859     } while (0)
860
861
862
863 #define i386_neg_reg(reg) \
864     do { \
865         *(((u1 *) mcodeptr)++) = (u1) 0xf7; \
866         i386_emit_reg(3,(reg)); \
867     } while (0)
868
869
870 #define i386_neg_mem(mem) \
871     do { \
872         *(((u1 *) mcodeptr)++) = (u1) 0xf7; \
873         i386_emit_mem(3,(mem)); \
874     } while (0)
875
876
877 #define i386_neg_membase(basereg,disp) \
878     do { \
879         *(((u1 *) mcodeptr)++) = (u1) 0xf7; \
880         i386_emit_membase((basereg),(disp),3); \
881     } while (0)
882
883
884
885 #define i386_push_reg(reg) \
886     do { \
887         *(((u1 *) mcodeptr)++) = (u1) 0x50 + (0x07 & (reg)); \
888     } while (0)
889
890
891 #define i386_push_membase(basereg,disp) \
892     do { \
893         *(((u1 *) mcodeptr)++) = (u1) 0xff; \
894         i386_emit_membase((basereg),(disp),6); \
895     } while (0)
896
897
898 #define i386_push_imm(imm) \
899     do { \
900         *(((u1 *) mcodeptr)++) = (u1) 0x68; \
901         i386_emit_imm32((imm)); \
902     } while (0)
903
904
905 #define i386_pop_reg(reg) \
906     do { \
907         *(((u1 *) mcodeptr)++) = (u1) 0x58 + (0x07 & (reg)); \
908     } while (0)
909
910
911 #define i386_nop() \
912     do { \
913         *(((u1 *) mcodeptr)++) = (u1) 0x90; \
914     } while (0)
915
916
917
918 /*
919  * call instructions
920  */
921 #define i386_call_reg(reg) \
922     do { \
923         *(((u1 *) mcodeptr)++) = (u1) 0xff; \
924         i386_emit_reg(2,(reg)); \
925     } while (0)
926
927
928 #define i386_call_imm(imm) \
929     do { \
930         *(((u1 *) mcodeptr)++) = (u1) 0xe8; \
931         i386_emit_imm32((imm)); \
932     } while (0)
933
934
935
936 /*
937  * floating point instructions
938  */
939 #define i386_fld1() \
940     do { \
941         *(((u1 *) mcodeptr)++) = (u1) 0xd9; \
942         *(((u1 *) mcodeptr)++) = (u1) 0xe8; \
943     } while (0)
944
945
946 #define i386_fldz() \
947     do { \
948         *(((u1 *) mcodeptr)++) = (u1) 0xd9; \
949         *(((u1 *) mcodeptr)++) = (u1) 0xee; \
950     } while (0)
951
952
953 #define i386_flds_mem(mem) \
954     do { \
955         *(((u1 *) mcodeptr)++) = (u1) 0xd9; \
956         i386_emit_mem(0,(mem)); \
957     } while (0)
958
959
960 #define i386_fldl_mem(mem) \
961     do { \
962         *(((u1 *) mcodeptr)++) = (u1) 0xdd; \
963         i386_emit_mem(0,(mem)); \
964     } while (0)
965
966
967 #define i386_flds_membase(basereg,disp) \
968     do { \
969         *(((u1 *) mcodeptr)++) = (u1) 0xd9; \
970         i386_emit_membase((basereg),(disp),0); \
971     } while (0)
972
973
974 #define i386_fldl_membase(basereg,disp) \
975     do { \
976         *(((u1 *) mcodeptr)++) = (u1) 0xdd; \
977         i386_emit_membase((basereg),(disp),0); \
978     } while (0)
979
980
981 #define i386_flds_memindex(disp,basereg,indexreg,scale) \
982     do { \
983         *(((u1 *) mcodeptr)++) = (u1) 0xd9; \
984         i386_emit_memindex(0,(disp),(basereg),(indexreg),(scale)); \
985     } while (0)
986
987
988 #define i386_fldl_memindex(disp,basereg,indexreg,scale) \
989     do { \
990         *(((u1 *) mcodeptr)++) = (u1) 0xdd; \
991         i386_emit_memindex(0,(disp),(basereg),(indexreg),(scale)); \
992     } while (0)
993
994
995
996
997 #define i386_fildl_mem(mem) \
998     do { \
999         *(((u1 *) mcodeptr)++) = (u1) 0xdb; \
1000         i386_emit_mem(0,(mem)); \
1001     } while (0)
1002
1003
1004 #define i386_fildl_membase(basereg,disp) \
1005     do { \
1006         *(((u1 *) mcodeptr)++) = (u1) 0xdb; \
1007         i386_emit_membase((basereg),(disp),0); \
1008     } while (0)
1009
1010
1011 #define i386_fildll_membase(basereg,disp) \
1012     do { \
1013         *(((u1 *) mcodeptr)++) = (u1) 0xdf; \
1014         i386_emit_membase((basereg),(disp),5); \
1015     } while (0)
1016
1017
1018
1019
1020 #define i386_fsts_mem(mem) \
1021     do { \
1022         *(((u1 *) mcodeptr)++) = (u1) 0xd9; \
1023         i386_emit_mem(2,(mem)); \
1024     } while (0)
1025
1026
1027 #define i386_fstl_mem(mem) \
1028     do { \
1029         *(((u1 *) mcodeptr)++) = (u1) 0xdd; \
1030         i386_emit_mem(2,(mem)); \
1031     } while (0)
1032
1033
1034 #define i386_fsts_membase(basereg,disp) \
1035     do { \
1036         *(((u1 *) mcodeptr)++) = (u1) 0xd9; \
1037         i386_emit_membase((basereg),(disp),2); \
1038     } while (0)
1039
1040
1041 #define i386_fstl_membase(basereg,disp) \
1042     do { \
1043         *(((u1 *) mcodeptr)++) = (u1) 0xdd; \
1044         i386_emit_membase((basereg),(disp),2); \
1045     } while (0)
1046
1047
1048 #define i386_fsts_memindex(disp,basereg,indexreg,scale) \
1049     do { \
1050         *(((u1 *) mcodeptr)++) = (u1) 0xd9; \
1051         i386_emit_memindex(2,(disp),(basereg),(indexreg),(scale)); \
1052     } while (0)
1053
1054
1055 #define i386_fstl_memindex(disp,basereg,indexreg,scale) \
1056     do { \
1057         *(((u1 *) mcodeptr)++) = (u1) 0xdd; \
1058         i386_emit_memindex(2,(disp),(basereg),(indexreg),(scale)); \
1059     } while (0)
1060
1061
1062
1063
1064 #define i386_fstps_mem(mem) \
1065     do { \
1066         *(((u1 *) mcodeptr)++) = (u1) 0xd9; \
1067         i386_emit_mem(3,(mem)); \
1068     } while (0)
1069
1070
1071 #define i386_fstpl_mem(mem) \
1072     do { \
1073         *(((u1 *) mcodeptr)++) = (u1) 0xdd; \
1074         i386_emit_mem(3,(mem)); \
1075     } while (0)
1076
1077
1078 #define i386_fstps_membase(basereg,disp) \
1079     do { \
1080         *(((u1 *) mcodeptr)++) = (u1) 0xd9; \
1081         i386_emit_membase((basereg),(disp),3); \
1082     } while (0)
1083
1084
1085 #define i386_fstpl_membase(basereg,disp) \
1086     do { \
1087         *(((u1 *) mcodeptr)++) = (u1) 0xdd; \
1088         i386_emit_membase((basereg),(disp),3); \
1089     } while (0)
1090
1091
1092
1093
1094 #define i386_fistpl_mem(mem) \
1095     do { \
1096         *(((u1 *) mcodeptr)++) = (u1) 0xdb; \
1097         i386_emit_mem(3,(mem)); \
1098     } while (0)
1099
1100
1101 #define i386_fistpll_mem(mem) \
1102     do { \
1103         *(((u1 *) mcodeptr)++) = (u1) 0xdf; \
1104         i386_emit_mem(7,(mem)); \
1105     } while (0)
1106
1107
1108 #define i386_fistl_membase(basereg,disp) \
1109     do { \
1110         *(((u1 *) mcodeptr)++) = (u1) 0xdb; \
1111         i386_emit_membase((basereg),(disp),2); \
1112     } while (0)
1113
1114
1115 #define i386_fistpl_membase(basereg,disp) \
1116     do { \
1117         *(((u1 *) mcodeptr)++) = (u1) 0xdb; \
1118         i386_emit_membase((basereg),(disp),3); \
1119     } while (0)
1120
1121
1122 #define i386_fistpll_membase(basereg,disp) \
1123     do { \
1124         *(((u1 *) mcodeptr)++) = (u1) 0xdf; \
1125         i386_emit_membase((basereg),(disp),7); \
1126     } while (0)
1127
1128
1129
1130
1131 #define i386_fchs() \
1132     do { \
1133         *(((u1 *) mcodeptr)++) = (u1) 0xd9; \
1134         *(((u1 *) mcodeptr)++) = (u1) 0xe0; \
1135     } while (0)
1136
1137
1138 #define i386_faddp() \
1139     do { \
1140         *(((u1 *) mcodeptr)++) = (u1) 0xde; \
1141         *(((u1 *) mcodeptr)++) = (u1) 0xc1; \
1142     } while (0)
1143
1144
1145 #define i386_fadds_membase(basereg,disp) \
1146     do { \
1147         *(((u1 *) mcodeptr)++) = (u1) 0xd8; \
1148         i386_emit_membase((basereg),(disp),0); \
1149     } while (0)
1150
1151
1152 #define i386_faddl_membase(basereg,disp) \
1153     do { \
1154         *(((u1 *) mcodeptr)++) = (u1) 0xdc; \
1155         i386_emit_membase((basereg),(disp),0); \
1156     } while (0)
1157
1158
1159 #define i386_fsubp() \
1160     do { \
1161         *(((u1 *) mcodeptr)++) = (u1) 0xde; \
1162         *(((u1 *) mcodeptr)++) = (u1) 0xe9; \
1163     } while (0)
1164
1165
1166 #define i386_fsubs_membase(basereg,disp) \
1167     do { \
1168         *(((u1 *) mcodeptr)++) = (u1) 0xd8; \
1169         i386_emit_membase((basereg),(disp),4); \
1170     } while (0)
1171
1172
1173 #define i386_fmulp() \
1174     do { \
1175         *(((u1 *) mcodeptr)++) = (u1) 0xde; \
1176         *(((u1 *) mcodeptr)++) = (u1) 0xc9; \
1177     } while (0)
1178
1179
1180 #define i386_fdivp() \
1181     do { \
1182         *(((u1 *) mcodeptr)++) = (u1) 0xde; \
1183         *(((u1 *) mcodeptr)++) = (u1) 0xf9; \
1184     } while (0)
1185
1186
1187 #define i386_fxch() \
1188     do { \
1189         *(((u1 *) mcodeptr)++) = (u1) 0xd9; \
1190         *(((u1 *) mcodeptr)++) = (u1) 0xc9; \
1191     } while (0)
1192
1193
1194 #define i386_fxch_reg(reg) \
1195     do { \
1196         *(((u1 *) mcodeptr)++) = (u1) 0xd9; \
1197         *(((u1 *) mcodeptr)++) = (u1) 0xc8 + (0x0f & (reg)); \
1198     } while (0)
1199
1200
1201 #define i386_fprem1() \
1202     do { \
1203         *(((u1 *) mcodeptr)++) = (u1) 0xd9; \
1204         *(((u1 *) mcodeptr)++) = (u1) 0xf5; \
1205     } while (0)
1206
1207
1208 #define i386_fucom() \
1209     do { \
1210         *(((u1 *) mcodeptr)++) = (u1) 0xdd; \
1211         *(((u1 *) mcodeptr)++) = (u1) 0xe1; \
1212     } while (0)
1213
1214
1215 #define i386_fucompp() \
1216     do { \
1217         *(((u1 *) mcodeptr)++) = (u1) 0xda; \
1218         *(((u1 *) mcodeptr)++) = (u1) 0xe9; \
1219     } while (0)
1220
1221
1222 #define i386_fnstsw() \
1223     do { \
1224         *(((u1 *) mcodeptr)++) = (u1) 0xdf; \
1225         *(((u1 *) mcodeptr)++) = (u1) 0xe0; \
1226     } while (0)
1227
1228
1229 #define i386_finit() \
1230     do { \
1231         *(((u1 *) mcodeptr)++) = (u1) 0x9b; \
1232         *(((u1 *) mcodeptr)++) = (u1) 0xdb; \
1233         *(((u1 *) mcodeptr)++) = (u1) 0xe3; \
1234     } while (0)
1235
1236
1237
1238 /* macros for all used commands (see an Alpha-manual for description) *********/ 
1239
1240 #define M_LDA(a,b,disp)         M_MEM (0x08,a,b,disp)           /* low const  */
1241 #define M_LDAH(a,b,disp)        M_MEM (0x09,a,b,disp)           /* high const */
1242 #define M_BLDU(a,b,disp)        M_MEM (0x0a,a,b,disp)           /*  8 load    */
1243 #define M_SLDU(a,b,disp)        M_MEM (0x0c,a,b,disp)           /* 16 load    */
1244 #define M_ILD(a,b,disp)         M_MEM (0x28,a,b,disp)           /* 32 load    */
1245 #define M_LLD(a,b,disp)         M_MEM (0x29,a,b,disp)           /* 64 load    */
1246 #define M_ALD(a,b,disp)         M_MEM (0x29,a,b,disp)           /* addr load  */
1247 #define M_BST(a,b,disp)         M_MEM (0x0e,a,b,disp)           /*  8 store   */
1248 #define M_SST(a,b,disp)         M_MEM (0x0d,a,b,disp)           /* 16 store   */
1249 #define M_IST(a,b,disp)         M_MEM (0x2c,a,b,disp)           /* 32 store   */
1250 #define M_LST(a,b,disp)         M_MEM (0x2d,a,b,disp)           /* 64 store   */
1251 #define M_AST(a,b,disp)         M_MEM (0x2d,a,b,disp)           /* addr store */
1252
1253 #define M_BSEXT(b,c)            M_OP3 (0x1c,0x0,REG_ZERO,b,c,0) /*  8 signext */
1254 #define M_SSEXT(b,c)            M_OP3 (0x1c,0x1,REG_ZERO,b,c,0) /* 16 signext */
1255
1256 #define M_BR(disp)              M_BRA (0x30,REG_ZERO,disp)      /* branch     */
1257 #define M_BSR(ra,disp)          M_BRA (0x34,ra,disp)            /* branch sbr */
1258 #define M_BEQZ(a,disp)          M_BRA (0x39,a,disp)             /* br a == 0  */
1259 #define M_BLTZ(a,disp)          M_BRA (0x3a,a,disp)             /* br a <  0  */
1260 #define M_BLEZ(a,disp)          M_BRA (0x3b,a,disp)             /* br a <= 0  */
1261 #define M_BNEZ(a,disp)          M_BRA (0x3d,a,disp)             /* br a != 0  */
1262 #define M_BGEZ(a,disp)          M_BRA (0x3e,a,disp)             /* br a >= 0  */
1263 #define M_BGTZ(a,disp)          M_BRA (0x3f,a,disp)             /* br a >  0  */
1264
1265 #define M_JMP(a,b)              M_MEM (0x1a,a,b,0x0000)         /* jump       */
1266 #define M_JSR(a,b)              M_MEM (0x1a,a,b,0x4000)         /* call sbr   */
1267 #define M_RET(a,b)              M_MEM (0x1a,a,b,0x8000)         /* return     */
1268
1269 #define M_IADD(a,b,c)           M_OP3 (0x10,0x0,  a,b,c,0)      /* 32 add     */
1270 #define M_LADD(a,b,c)           M_OP3 (0x10,0x20, a,b,c,0)      /* 64 add     */
1271 #define M_ISUB(a,b,c)           M_OP3 (0x10,0x09, a,b,c,0)      /* 32 sub     */
1272 #define M_LSUB(a,b,c)           M_OP3 (0x10,0x29, a,b,c,0)      /* 64 sub     */
1273 #define M_IMUL(a,b,c)           M_OP3 (0x13,0x00, a,b,c,0)      /* 32 mul     */
1274 #define M_LMUL(a,b,c)           M_OP3 (0x13,0x20, a,b,c,0)      /* 64 mul     */
1275
1276 #define M_IADD_IMM(a,b,c)       M_OP3 (0x10,0x0,  a,b,c,1)      /* 32 add     */
1277 #define M_LADD_IMM(a,b,c)       M_OP3 (0x10,0x20, a,b,c,1)      /* 64 add     */
1278 #define M_ISUB_IMM(a,b,c)       M_OP3 (0x10,0x09, a,b,c,1)      /* 32 sub     */
1279 #define M_LSUB_IMM(a,b,c)       M_OP3 (0x10,0x29, a,b,c,1)      /* 64 sub     */
1280 #define M_IMUL_IMM(a,b,c)       M_OP3 (0x13,0x00, a,b,c,1)      /* 32 mul     */
1281 #define M_LMUL_IMM(a,b,c)       M_OP3 (0x13,0x20, a,b,c,1)      /* 64 mul     */
1282
1283 #define M_CMPEQ(a,b,c)          M_OP3 (0x10,0x2d, a,b,c,0)      /* c = a == b */
1284 #define M_CMPLT(a,b,c)          M_OP3 (0x10,0x4d, a,b,c,0)      /* c = a <  b */
1285 #define M_CMPLE(a,b,c)          M_OP3 (0x10,0x6d, a,b,c,0)      /* c = a <= b */
1286
1287 #define M_CMPULE(a,b,c)         M_OP3 (0x10,0x3d, a,b,c,0)      /* c = a <= b */
1288 #define M_CMPULT(a,b,c)         M_OP3 (0x10,0x1d, a,b,c,0)      /* c = a <= b */
1289
1290 #define M_CMPEQ_IMM(a,b,c)      M_OP3 (0x10,0x2d, a,b,c,1)      /* c = a == b */
1291 #define M_CMPLT_IMM(a,b,c)      M_OP3 (0x10,0x4d, a,b,c,1)      /* c = a <  b */
1292 #define M_CMPLE_IMM(a,b,c)      M_OP3 (0x10,0x6d, a,b,c,1)      /* c = a <= b */
1293
1294 #define M_CMPULE_IMM(a,b,c)     M_OP3 (0x10,0x3d, a,b,c,1)      /* c = a <= b */
1295 #define M_CMPULT_IMM(a,b,c)     M_OP3 (0x10,0x1d, a,b,c,1)      /* c = a <= b */
1296
1297 #define M_AND(a,b,c)            M_OP3 (0x11,0x00, a,b,c,0)      /* c = a &  b */
1298 #define M_OR( a,b,c)            M_OP3 (0x11,0x20, a,b,c,0)      /* c = a |  b */
1299 #define M_XOR(a,b,c)            M_OP3 (0x11,0x40, a,b,c,0)      /* c = a ^  b */
1300
1301 #define M_AND_IMM(a,b,c)        M_OP3 (0x11,0x00, a,b,c,1)      /* c = a &  b */
1302 #define M_OR_IMM( a,b,c)        M_OP3 (0x11,0x20, a,b,c,1)      /* c = a |  b */
1303 #define M_XOR_IMM(a,b,c)        M_OP3 (0x11,0x40, a,b,c,1)      /* c = a ^  b */
1304
1305 #define M_MOV(a,c)              M_OR (a,a,c)                    /* c = a      */
1306 #define M_CLR(c)                M_OR (31,31,c)                  /* c = 0      */
1307 #define M_NOP                   M_OR (31,31,31)                 /* ;          */
1308
1309 #define M_SLL(a,b,c)            M_OP3 (0x12,0x39, a,b,c,0)      /* c = a << b */
1310 #define M_SRA(a,b,c)            M_OP3 (0x12,0x3c, a,b,c,0)      /* c = a >> b */
1311 #define M_SRL(a,b,c)            M_OP3 (0x12,0x34, a,b,c,0)      /* c = a >>>b */
1312
1313 #define M_SLL_IMM(a,b,c)        M_OP3 (0x12,0x39, a,b,c,1)      /* c = a << b */
1314 #define M_SRA_IMM(a,b,c)        M_OP3 (0x12,0x3c, a,b,c,1)      /* c = a >> b */
1315 #define M_SRL_IMM(a,b,c)        M_OP3 (0x12,0x34, a,b,c,1)      /* c = a >>>b */
1316
1317 #define M_FLD(a,b,disp)         M_MEM (0x22,a,b,disp)           /* load flt   */
1318 #define M_DLD(a,b,disp)         M_MEM (0x23,a,b,disp)           /* load dbl   */
1319 #define M_FST(a,b,disp)         M_MEM (0x26,a,b,disp)           /* store flt  */
1320 #define M_DST(a,b,disp)         M_MEM (0x27,a,b,disp)           /* store dbl  */
1321
1322 #define M_FADD(a,b,c)           M_FOP3 (0x16, 0x080, a,b,c)     /* flt add    */
1323 #define M_DADD(a,b,c)           M_FOP3 (0x16, 0x0a0, a,b,c)     /* dbl add    */
1324 #define M_FSUB(a,b,c)           M_FOP3 (0x16, 0x081, a,b,c)     /* flt sub    */
1325 #define M_DSUB(a,b,c)           M_FOP3 (0x16, 0x0a1, a,b,c)     /* dbl sub    */
1326 #define M_FMUL(a,b,c)           M_FOP3 (0x16, 0x082, a,b,c)     /* flt mul    */
1327 #define M_DMUL(a,b,c)           M_FOP3 (0x16, 0x0a2, a,b,c)     /* dbl mul    */
1328 #define M_FDIV(a,b,c)           M_FOP3 (0x16, 0x083, a,b,c)     /* flt div    */
1329 #define M_DDIV(a,b,c)           M_FOP3 (0x16, 0x0a3, a,b,c)     /* dbl div    */
1330
1331 #define M_FADDS(a,b,c)          M_FOP3 (0x16, 0x580, a,b,c)     /* flt add    */
1332 #define M_DADDS(a,b,c)          M_FOP3 (0x16, 0x5a0, a,b,c)     /* dbl add    */
1333 #define M_FSUBS(a,b,c)          M_FOP3 (0x16, 0x581, a,b,c)     /* flt sub    */
1334 #define M_DSUBS(a,b,c)          M_FOP3 (0x16, 0x5a1, a,b,c)     /* dbl sub    */
1335 #define M_FMULS(a,b,c)          M_FOP3 (0x16, 0x582, a,b,c)     /* flt mul    */
1336 #define M_DMULS(a,b,c)          M_FOP3 (0x16, 0x5a2, a,b,c)     /* dbl mul    */
1337 #define M_FDIVS(a,b,c)          M_FOP3 (0x16, 0x583, a,b,c)     /* flt div    */
1338 #define M_DDIVS(a,b,c)          M_FOP3 (0x16, 0x5a3, a,b,c)     /* dbl div    */
1339
1340 #define M_CVTDF(b,c)            M_FOP3 (0x16, 0x0ac, 31,b,c)    /* dbl2long   */
1341 #define M_CVTLF(b,c)            M_FOP3 (0x16, 0x0bc, 31,b,c)    /* long2flt   */
1342 #define M_CVTLD(b,c)            M_FOP3 (0x16, 0x0be, 31,b,c)    /* long2dbl   */
1343 #define M_CVTDL(b,c)            M_FOP3 (0x16, 0x1af, 31,b,c)    /* dbl2long   */
1344 #define M_CVTDL_C(b,c)          M_FOP3 (0x16, 0x12f, 31,b,c)    /* dbl2long   */
1345 #define M_CVTLI(b,c)            M_FOP3 (0x17, 0x130, 31,b,c)    /* long2int   */
1346
1347 #define M_CVTDFS(b,c)           M_FOP3 (0x16, 0x5ac, 31,b,c)    /* dbl2long   */
1348 #define M_CVTDLS(b,c)           M_FOP3 (0x16, 0x5af, 31,b,c)    /* dbl2long   */
1349 #define M_CVTDL_CS(b,c)         M_FOP3 (0x16, 0x52f, 31,b,c)    /* dbl2long   */
1350 #define M_CVTLIS(b,c)           M_FOP3 (0x17, 0x530, 31,b,c)    /* long2int   */
1351
1352 #define M_FCMPEQ(a,b,c)         M_FOP3 (0x16, 0x0a5, a,b,c)     /* c = a==b   */
1353 #define M_FCMPLT(a,b,c)         M_FOP3 (0x16, 0x0a6, a,b,c)     /* c = a<b    */
1354
1355 #define M_FCMPEQS(a,b,c)        M_FOP3 (0x16, 0x5a5, a,b,c)     /* c = a==b   */
1356 #define M_FCMPLTS(a,b,c)        M_FOP3 (0x16, 0x5a6, a,b,c)     /* c = a<b    */
1357
1358 #define M_FMOV(fa,fb)           M_FOP3 (0x17, 0x020, fa,fa,fb)  /* b = a      */
1359 #define M_FMOVN(fa,fb)          M_FOP3 (0x17, 0x021, fa,fa,fb)  /* b = -a     */
1360
1361 #define M_FNOP                  M_FMOV (31,31)
1362
1363 #define M_FBEQZ(fa,disp)        M_BRA (0x31,fa,disp)            /* br a == 0.0*/
1364
1365 /* macros for special commands (see an Alpha-manual for description) **********/ 
1366
1367 #define M_TRAPB                 M_MEM (0x18,0,0,0x0000)        /* trap barrier*/
1368
1369 #define M_S4ADDL(a,b,c)         M_OP3 (0x10,0x02, a,b,c,0)     /* c = a*4 + b */
1370 #define M_S4ADDQ(a,b,c)         M_OP3 (0x10,0x22, a,b,c,0)     /* c = a*4 + b */
1371 #define M_S4SUBL(a,b,c)         M_OP3 (0x10,0x0b, a,b,c,0)     /* c = a*4 - b */
1372 #define M_S4SUBQ(a,b,c)         M_OP3 (0x10,0x2b, a,b,c,0)     /* c = a*4 - b */
1373 #define M_S8ADDL(a,b,c)         M_OP3 (0x10,0x12, a,b,c,0)     /* c = a*8 + b */
1374 #define M_S8ADDQ(a,b,c)         M_OP3 (0x10,0x32, a,b,c,0)     /* c = a*8 + b */
1375 #define M_S8SUBL(a,b,c)         M_OP3 (0x10,0x1b, a,b,c,0)     /* c = a*8 - b */
1376 #define M_S8SUBQ(a,b,c)         M_OP3 (0x10,0x3b, a,b,c,0)     /* c = a*8 - b */
1377 #define M_SAADDQ(a,b,c)         M_S8ADDQ(a,b,c)                /* c = a*8 + b */
1378
1379 #define M_S4ADDL_IMM(a,b,c)     M_OP3 (0x10,0x02, a,b,c,1)     /* c = a*4 + b */
1380 #define M_S4ADDQ_IMM(a,b,c)     M_OP3 (0x10,0x22, a,b,c,1)     /* c = a*4 + b */
1381 #define M_S4SUBL_IMM(a,b,c)     M_OP3 (0x10,0x0b, a,b,c,1)     /* c = a*4 - b */
1382 #define M_S4SUBQ_IMM(a,b,c)     M_OP3 (0x10,0x2b, a,b,c,1)     /* c = a*4 - b */
1383 #define M_S8ADDL_IMM(a,b,c)     M_OP3 (0x10,0x12, a,b,c,1)     /* c = a*8 + b */
1384 #define M_S8ADDQ_IMM(a,b,c)     M_OP3 (0x10,0x32, a,b,c,1)     /* c = a*8 + b */
1385 #define M_S8SUBL_IMM(a,b,c)     M_OP3 (0x10,0x1b, a,b,c,1)     /* c = a*8 - b */
1386 #define M_S8SUBQ_IMM(a,b,c)     M_OP3 (0x10,0x3b, a,b,c,1)     /* c = a*8 - b */
1387
1388 #define M_LLD_U(a,b,disp)       M_MEM (0x0b,a,b,disp)          /* unalign ld  */
1389 #define M_LST_U(a,b,disp)       M_MEM (0x0f,a,b,disp)          /* unalign st  */
1390
1391 #define M_ZAP(a,b,c)            M_OP3 (0x12,0x30, a,b,c,0)
1392 #define M_ZAPNOT(a,b,c)         M_OP3 (0x12,0x31, a,b,c,0)
1393
1394 #define M_ZAP_IMM(a,b,c)        M_OP3 (0x12,0x30, a,b,c,1)
1395 #define M_ZAPNOT_IMM(a,b,c)     M_OP3 (0x12,0x31, a,b,c,1)
1396
1397 #define M_BZEXT(a,b)            M_ZAPNOT_IMM(a, 0x01, b)       /*  8 zeroext  */
1398 #define M_CZEXT(a,b)            M_ZAPNOT_IMM(a, 0x03, b)       /* 16 zeroext  */
1399 #define M_IZEXT(a,b)            M_ZAPNOT_IMM(a, 0x0f, b)       /* 32 zeroext  */
1400
1401 #define M_EXTBL(a,b,c)          M_OP3 (0x12,0x06, a,b,c,0)
1402 #define M_EXTWL(a,b,c)          M_OP3 (0x12,0x16, a,b,c,0)
1403 #define M_EXTLL(a,b,c)          M_OP3 (0x12,0x26, a,b,c,0)
1404 #define M_EXTQL(a,b,c)          M_OP3 (0x12,0x36, a,b,c,0)
1405 #define M_EXTWH(a,b,c)          M_OP3 (0x12,0x5a, a,b,c,0)
1406 #define M_EXTLH(a,b,c)          M_OP3 (0x12,0x6a, a,b,c,0)
1407 #define M_EXTQH(a,b,c)          M_OP3 (0x12,0x7a, a,b,c,0)
1408 #define M_INSBL(a,b,c)          M_OP3 (0x12,0x0b, a,b,c,0)
1409 #define M_INSWL(a,b,c)          M_OP3 (0x12,0x1b, a,b,c,0)
1410 #define M_INSLL(a,b,c)          M_OP3 (0x12,0x2b, a,b,c,0)
1411 #define M_INSQL(a,b,c)          M_OP3 (0x12,0x3b, a,b,c,0)
1412 #define M_INSWH(a,b,c)          M_OP3 (0x12,0x57, a,b,c,0)
1413 #define M_INSLH(a,b,c)          M_OP3 (0x12,0x67, a,b,c,0)
1414 #define M_INSQH(a,b,c)          M_OP3 (0x12,0x77, a,b,c,0)
1415 #define M_MSKBL(a,b,c)          M_OP3 (0x12,0x02, a,b,c,0)
1416 #define M_MSKWL(a,b,c)          M_OP3 (0x12,0x12, a,b,c,0)
1417 #define M_MSKLL(a,b,c)          M_OP3 (0x12,0x22, a,b,c,0)
1418 #define M_MSKQL(a,b,c)          M_OP3 (0x12,0x32, a,b,c,0)
1419 #define M_MSKWH(a,b,c)          M_OP3 (0x12,0x52, a,b,c,0)
1420 #define M_MSKLH(a,b,c)          M_OP3 (0x12,0x62, a,b,c,0)
1421 #define M_MSKQH(a,b,c)          M_OP3 (0x12,0x72, a,b,c,0)
1422
1423 #define M_EXTBL_IMM(a,b,c)      M_OP3 (0x12,0x06, a,b,c,1)
1424 #define M_EXTWL_IMM(a,b,c)      M_OP3 (0x12,0x16, a,b,c,1)
1425 #define M_EXTLL_IMM(a,b,c)      M_OP3 (0x12,0x26, a,b,c,1)
1426 #define M_EXTQL_IMM(a,b,c)      M_OP3 (0x12,0x36, a,b,c,1)
1427 #define M_EXTWH_IMM(a,b,c)      M_OP3 (0x12,0x5a, a,b,c,1)
1428 #define M_EXTLH_IMM(a,b,c)      M_OP3 (0x12,0x6a, a,b,c,1)
1429 #define M_EXTQH_IMM(a,b,c)      M_OP3 (0x12,0x7a, a,b,c,1)
1430 #define M_INSBL_IMM(a,b,c)      M_OP3 (0x12,0x0b, a,b,c,1)
1431 #define M_INSWL_IMM(a,b,c)      M_OP3 (0x12,0x1b, a,b,c,1)
1432 #define M_INSLL_IMM(a,b,c)      M_OP3 (0x12,0x2b, a,b,c,1)
1433 #define M_INSQL_IMM(a,b,c)      M_OP3 (0x12,0x3b, a,b,c,1)
1434 #define M_INSWH_IMM(a,b,c)      M_OP3 (0x12,0x57, a,b,c,1)
1435 #define M_INSLH_IMM(a,b,c)      M_OP3 (0x12,0x67, a,b,c,1)
1436 #define M_INSQH_IMM(a,b,c)      M_OP3 (0x12,0x77, a,b,c,1)
1437 #define M_MSKBL_IMM(a,b,c)      M_OP3 (0x12,0x02, a,b,c,1)
1438 #define M_MSKWL_IMM(a,b,c)      M_OP3 (0x12,0x12, a,b,c,1)
1439 #define M_MSKLL_IMM(a,b,c)      M_OP3 (0x12,0x22, a,b,c,1)
1440 #define M_MSKQL_IMM(a,b,c)      M_OP3 (0x12,0x32, a,b,c,1)
1441 #define M_MSKWH_IMM(a,b,c)      M_OP3 (0x12,0x52, a,b,c,1)
1442 #define M_MSKLH_IMM(a,b,c)      M_OP3 (0x12,0x62, a,b,c,1)
1443 #define M_MSKQH_IMM(a,b,c)      M_OP3 (0x12,0x72, a,b,c,1)
1444
1445 #define M_UMULH(a,b,c)          M_OP3 (0x13,0x30, a,b,c,0)     /* 64 umulh    */
1446
1447 #define M_UMULH_IMM(a,b,c)      M_OP3 (0x13,0x30, a,b,c,1)     /* 64 umulh    */
1448
1449 #define M_CMOVEQ(a,b,c)         M_OP3 (0x11,0x24, a,b,c,0)     /* a==0 ? c=b  */
1450 #define M_CMOVNE(a,b,c)         M_OP3 (0x11,0x26, a,b,c,0)     /* a!=0 ? c=b  */
1451 #define M_CMOVLT(a,b,c)         M_OP3 (0x11,0x44, a,b,c,0)     /* a< 0 ? c=b  */
1452 #define M_CMOVGE(a,b,c)         M_OP3 (0x11,0x46, a,b,c,0)     /* a>=0 ? c=b  */
1453 #define M_CMOVLE(a,b,c)         M_OP3 (0x11,0x64, a,b,c,0)     /* a<=0 ? c=b  */
1454 #define M_CMOVGT(a,b,c)         M_OP3 (0x11,0x66, a,b,c,0)     /* a> 0 ? c=b  */
1455
1456 #define M_CMOVEQ_IMM(a,b,c)     M_OP3 (0x11,0x24, a,b,c,1)     /* a==0 ? c=b  */
1457 #define M_CMOVNE_IMM(a,b,c)     M_OP3 (0x11,0x26, a,b,c,1)     /* a!=0 ? c=b  */
1458 #define M_CMOVLT_IMM(a,b,c)     M_OP3 (0x11,0x44, a,b,c,1)     /* a< 0 ? c=b  */
1459 #define M_CMOVGE_IMM(a,b,c)     M_OP3 (0x11,0x46, a,b,c,1)     /* a>=0 ? c=b  */
1460 #define M_CMOVLE_IMM(a,b,c)     M_OP3 (0x11,0x64, a,b,c,1)     /* a<=0 ? c=b  */
1461 #define M_CMOVGT_IMM(a,b,c)     M_OP3 (0x11,0x66, a,b,c,1)     /* a> 0 ? c=b  */
1462
1463 /* macros for unused commands (see an Alpha-manual for description) ***********/ 
1464
1465 #define M_ANDNOT(a,b,c,const)   M_OP3 (0x11,0x08, a,b,c,const) /* c = a &~ b  */
1466 #define M_ORNOT(a,b,c,const)    M_OP3 (0x11,0x28, a,b,c,const) /* c = a |~ b  */
1467 #define M_XORNOT(a,b,c,const)   M_OP3 (0x11,0x48, a,b,c,const) /* c = a ^~ b  */
1468
1469 #define M_CMPBGE(a,b,c,const)   M_OP3 (0x10,0x0f, a,b,c,const)
1470
1471 #define M_FCMPUN(a,b,c)         M_FOP3 (0x16, 0x0a4, a,b,c)    /* unordered   */
1472 #define M_FCMPLE(a,b,c)         M_FOP3 (0x16, 0x0a7, a,b,c)    /* c = a<=b    */
1473
1474 #define M_FCMPUNS(a,b,c)        M_FOP3 (0x16, 0x5a4, a,b,c)    /* unordered   */
1475 #define M_FCMPLES(a,b,c)        M_FOP3 (0x16, 0x5a7, a,b,c)    /* c = a<=b    */
1476
1477 #define M_FBNEZ(fa,disp)        M_BRA (0x35,fa,disp)
1478 #define M_FBLEZ(fa,disp)        M_BRA (0x33,fa,disp)
1479
1480 #define M_JMP_CO(a,b)           M_MEM (0x1a,a,b,0xc000)        /* call cosub  */
1481
1482
1483 /* function gen_resolvebranch **************************************************
1484
1485     backpatches a branch instruction; Alpha branch instructions are very
1486     regular, so it is only necessary to overwrite some fixed bits in the
1487     instruction.
1488
1489     parameters: ip ... pointer to instruction after branch (void*)
1490                 so ... offset of instruction after branch  (s4)
1491                 to ... offset of branch target             (s4)
1492
1493 *******************************************************************************/
1494
1495 #define gen_resolvebranch(ip,so,to) \
1496     do { \
1497         *((s4 *) (((u1 *) (ip)) - 4)) = ((s4) ((to) - (so))); \
1498     } while (0)
1499
1500 #define SOFTNULLPTRCHECK       /* soft null pointer check supportet as option */
1501
1502 #endif