aboutsummaryrefslogtreecommitdiff
path: root/target-m68k/op.c
blob: 6134bb018e1e65ffd21ebd0d3b647514c68d5793 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*
 *  m68k micro operations
 * 
 *  Copyright (c) 2006-2007 CodeSourcery
 *  Written by Paul Brook
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "exec.h"
#include "m68k-qreg.h"

#ifndef offsetof
#define offsetof(type, field) ((size_t) &((type *)0)->field)
#endif

static long qreg_offsets[] = {
#define DEFO32(name, offset) offsetof(CPUState, offset),
#define DEFR(name, reg, mode) -1,
#define DEFF64(name, offset) offsetof(CPUState, offset),
    0,
#include "qregs.def"
};

#define CPU_FP_STATUS env->fp_status

#define RAISE_EXCEPTION(n) do { \
    env->exception_index = n; \
    cpu_loop_exit(); \
    } while(0)

#define get_op helper_get_op
#define set_op helper_set_op
#define get_opf64 helper_get_opf64
#define set_opf64 helper_set_opf64
uint32_t
get_op(int qreg)
{
    if (qreg >= TARGET_NUM_QREGS) {
        return env->qregs[qreg - TARGET_NUM_QREGS];
    } else if (qreg == QREG_T0) {
        return T0;
    } else {
        return *(uint32_t *)(((long)env) + qreg_offsets[qreg]);
    }
}

void set_op(int qreg, uint32_t val)
{
    if (qreg >= TARGET_NUM_QREGS) {
        env->qregs[qreg - TARGET_NUM_QREGS] = val;
    } else if (qreg == QREG_T0) {
        T0 = val;
    } else {
        *(uint32_t *)(((long)env) + qreg_offsets[qreg]) = val;
    }
}

float64 get_opf64(int qreg)
{
    if (qreg < TARGET_NUM_QREGS) {
        return *(float64 *)(((long)env) + qreg_offsets[qreg]);
    } else {
        return *(float64 *)&env->qregs[qreg - TARGET_NUM_QREGS];
    }
}

void set_opf64(int qreg, float64 val)
{
    if (qreg < TARGET_NUM_QREGS) {
        *(float64 *)(((long)env) + qreg_offsets[qreg]) = val;
    } else {
        *(float64 *)&env->qregs[qreg - TARGET_NUM_QREGS] = val;
    }
}

#define OP(name) void OPPROTO glue(op_,name) (void)

OP(mov32)
{
    set_op(PARAM1, get_op(PARAM2));
    FORCE_RET();
}

OP(mov32_im)
{
    set_op(PARAM1, PARAM2);
    FORCE_RET();
}

OP(movf64)
{
    set_opf64(PARAM1, get_opf64(PARAM2));
    FORCE_RET();
}

OP(zerof64)
{
    set_opf64(PARAM1, 0);
    FORCE_RET();
}

OP(add32)
{
    uint32_t op2 = get_op(PARAM2);
    uint32_t op3 = get_op(PARAM3);
    set_op(PARAM1, op2 + op3);
    FORCE_RET();
}

OP(sub32)
{
    uint32_t op2 = get_op(PARAM2);
    uint32_t op3 = get_op(PARAM3);
    set_op(PARAM1, op2 - op3);
    FORCE_RET();
}

OP(mul32)
{
    uint32_t op2 = get_op(PARAM2);
    uint32_t op3 = get_op(PARAM3);
    set_op(PARAM1, op2 * op3);
    FORCE_RET();
}

OP(not32)
{
    uint32_t arg = get_op(PARAM2);
    set_op(PARAM1, ~arg);
    FORCE_RET();
}

OP(neg32)
{
    uint32_t arg = get_op(PARAM2);
    set_op(PARAM1, -arg);
    FORCE_RET();
}

OP(bswap32)
{
    uint32_t arg = get_op(PARAM2);
    arg = (arg >> 24) | (arg << 24)
          | ((arg >> 16) & 0xff00) | ((arg << 16) & 0xff0000);
    set_op(PARAM1, arg);
    FORCE_RET();
}

OP(btest)
{
    uint32_t op1 = get_op(PARAM1);
    uint32_t op2 = get_op(PARAM2);
    if (op1 & op2)
        env->cc_dest &= ~CCF_Z;
    else
        env->cc_dest |= CCF_Z;
    FORCE_RET();
}

OP(subx_cc)
{
    uint32_t op1 = get_op(PARAM1);
    uint32_t op2 = get_op(PARAM2);
    uint32_t res;
    if (env->cc_x) {
        env->cc_x = (op1 <= op2);
        env->cc_op = CC_OP_SUBX;
        res = op1 - (op2 + 1);
    } else {
        env->cc_x = (op1 < op2);
        env->cc_op = CC_OP_SUB;
        res = op1 - op2;
    }
    set_op(PARAM1, res);
    FORCE_RET();
}

OP(addx_cc)
{
    uint32_t op1 = get_op(PARAM1);
    uint32_t op2 = get_op(PARAM2);
    uint32_t res;
    if (env->cc_x) {
        res = op1 + op2 + 1;
        env->cc_x = (res <= op2);
        env->cc_op = CC_OP_ADDX;
    } else {
        res = op1 + op2;
        env->cc_x = (res < op2);
        env->cc_op = CC_OP_ADD;
    }
    set_op(PARAM1, res);
    FORCE_RET();
}

/* Logic ops.  */

OP(and32)
{
    uint32_t op2 = get_op(PARAM2);
    uint32_t op3 = get_op(PARAM3);
    set_op(PARAM1, op2 & op3);
    FORCE_RET();
}

OP(or32)
{
    uint32_t op2 = get_op(PARAM2);
    uint32_t op3 = get_op(PARAM3);
    set_op(PARAM1, op2 | op3);
    FORCE_RET();
}

OP(xor32)
{
    uint32_t op2 = get_op(PARAM2);
    uint32_t op3 = get_op(PARAM3);
    set_op(PARAM1, op2 ^ op3);
    FORCE_RET();
}

/* Shifts.  */
OP(shl32)
{
    uint32_t op2 = get_op(PARAM2);
    uint32_t op3 = get_op(PARAM3);
    uint32_t result;
    result = op2 << op3;
    set_op(PARAM1, result);
    FORCE_RET();
}

OP(shl_cc)
{
    uint32_t op1 = get_op(PARAM1);
    uint32_t op2 = get_op(PARAM2);
    uint32_t result;
    result = op1 << op2;
    set_op(PARAM1, result);
    env->cc_x = (op1 << (op2 - 1)) & 1;
    FORCE_RET();
}

OP(shr32)
{
    uint32_t op2 = get_op(PARAM2);
    uint32_t op3 = get_op(PARAM3);
    uint32_t result;
    result = op2 >> op3;
    set_op(PARAM1, result);
    FORCE_RET();
}

OP(shr_cc)
{
    uint32_t op1 = get_op(PARAM1);
    uint32_t op2 = get_op(PARAM2);
    uint32_t result;
    result = op1 >> op2;
    set_op(PARAM1, result);
    env->cc_x = (op1 >> (op2 - 1)) & 1;
    FORCE_RET();
}

OP(sar_cc)
{
    int32_t op1 = get_op(PARAM1);
    uint32_t op2 = get_op(PARAM2);
    uint32_t result;
    result = op1 >> op2;
    set_op(PARAM1, result);
    env->cc_x = (op1 >> (op2 - 1)) & 1;
    FORCE_RET();
}

/* Value extend.  */

OP(ext8u32)
{
    uint32_t op2 = get_op(PARAM2);
    set_op(PARAM1, (uint8_t)op2);
    FORCE_RET();
}

OP(ext8s32)
{
    uint32_t op2 = get_op(PARAM2);
    set_op(PARAM1, (int8_t)op2);
    FORCE_RET();
}

OP(ext16u32)
{
    uint32_t op2 = get_op(PARAM2);
    set_op(PARAM1, (uint16_t)op2);
    FORCE_RET();
}

OP(ext16s32)
{
    uint32_t op2 = get_op(PARAM2);
    set_op(PARAM1, (int16_t)op2);
    FORCE_RET();
}

OP(flush_flags)
{
    int cc_op  = PARAM1;
    if (cc_op == CC_OP_DYNAMIC)
        cc_op = env->cc_op;
    cpu_m68k_flush_flags(env, cc_op);
    FORCE_RET();
}

OP(divu)
{
    uint32_t num;
    uint32_t den;
    uint32_t quot;
    uint32_t rem;
    uint32_t flags;
    
    num = env->div1;
    den = env->div2;
    /* ??? This needs to make sure the throwing location is accurate.  */
    if (den == 0)
        RAISE_EXCEPTION(EXCP_DIV0);
    quot = num / den;
    rem = num % den;
    flags = 0;
    /* Avoid using a PARAM1 of zero.  This breaks dyngen because it uses
       the address of a symbol, and gcc knows symbols can't have address
       zero.  */
    if (PARAM1 == 2 && quot > 0xffff)
        flags |= CCF_V;
    if (quot == 0)
        flags |= CCF_Z;
    else if ((int32_t)quot < 0)
        flags |= CCF_N;
    env->div1 = quot;
    env->div2 = rem;
    env->cc_dest = flags;
    FORCE_RET();
}

OP(divs)
{
    int32_t num;
    int32_t den;
    int32_t quot;
    int32_t rem;
    int32_t flags;
    
    num = env->div1;
    den = env->div2;
    if (den == 0)
        RAISE_EXCEPTION(EXCP_DIV0);
    quot = num / den;
    rem = num % den;
    flags = 0;
    if (PARAM1 == 2 && quot != (int16_t)quot)
        flags |= CCF_V;
    if (quot == 0)
        flags |= CCF_Z;
    else if (quot < 0)
        flags |= CCF_N;
    env->div1 = quot;
    env->div2 = rem;
    env->cc_dest = flags;
    FORCE_RET();
}

OP(halt)
{
    env->halted = 1;
    RAISE_EXCEPTION(EXCP_HLT);
    FORCE_RET();
}

OP(raise_exception)
{
    RAISE_EXCEPTION(PARAM1);
    FORCE_RET();
}

/* Floating point comparison sets flags differently to other instructions.  */

OP(sub_cmpf64)
{
    float64 src0;
    float64 src1;
    src0 = get_opf64(PARAM2);
    src1 = get_opf64(PARAM3);
    set_opf64(PARAM1, helper_sub_cmpf64(env, src0, src1));
    FORCE_RET();
}

OP(update_xflag_tst)
{
    uint32_t op1 = get_op(PARAM1);
    env->cc_x = op1;
    FORCE_RET();
}

OP(update_xflag_lt)
{
    uint32_t op1 = get_op(PARAM1);
    uint32_t op2 = get_op(PARAM2);
    env->cc_x = (op1 < op2);
    FORCE_RET();
}

OP(get_xflag)
{
    set_op(PARAM1, env->cc_x);
    FORCE_RET();
}

OP(logic_cc)
{
    uint32_t op1 = get_op(PARAM1);
    env->cc_dest = op1;
    FORCE_RET();
}

OP(update_cc_add)
{
    uint32_t op1 = get_op(PARAM1);
    uint32_t op2 = get_op(PARAM2);
    env->cc_dest = op1;
    env->cc_src = op2;
    FORCE_RET();
}

OP(fp_result)
{
    env->fp_result = get_opf64(PARAM1);
    FORCE_RET();
}

OP(jmp)
{
    GOTO_LABEL_PARAM(1);
}

/* These ops involve a function call, which probably requires a stack frame
   and breaks things on some hosts.  */
OP(jmp_z32)
{
    uint32_t arg = get_op(PARAM1);
    if (arg == 0)
        GOTO_LABEL_PARAM(2);
    FORCE_RET();
}

OP(jmp_nz32)
{
    uint32_t arg = get_op(PARAM1);
    if (arg != 0)
        GOTO_LABEL_PARAM(2);
    FORCE_RET();
}

OP(jmp_s32)
{
    int32_t arg = get_op(PARAM1);
    if (arg < 0)
        GOTO_LABEL_PARAM(2);
    FORCE_RET();
}

OP(jmp_ns32)
{
    int32_t arg = get_op(PARAM1);
    if (arg >= 0)
        GOTO_LABEL_PARAM(2);
    FORCE_RET();
}

void OPPROTO op_goto_tb0(void)
{
    GOTO_TB(op_goto_tb0, PARAM1, 0);
}

void OPPROTO op_goto_tb1(void)
{
    GOTO_TB(op_goto_tb1, PARAM1, 1);
}

OP(exit_tb)
{
    EXIT_TB();
}


/* Floating point.  */
OP(f64_to_i32)
{
    set_op(PARAM1, float64_to_int32(get_opf64(PARAM2), &CPU_FP_STATUS));
    FORCE_RET();
}

OP(f64_to_f32)
{
    union {
        float32 f;
        uint32_t i;
    } u;
    u.f = float64_to_float32(get_opf64(PARAM2), &CPU_FP_STATUS);
    set_op(PARAM1, u.i);
    FORCE_RET();
}

OP(i32_to_f64)
{
    set_opf64(PARAM1, int32_to_float64(get_op(PARAM2), &CPU_FP_STATUS));
    FORCE_RET();
}

OP(f32_to_f64)
{
    union {
        float32 f;
        uint32_t i;
    } u;
    u.i = get_op(PARAM2);
    set_opf64(PARAM1, float32_to_float64(u.f, &CPU_FP_STATUS));
    FORCE_RET();
}

OP(absf64)
{
    float64 op0 = get_opf64(PARAM2);
    set_opf64(PARAM1, float64_abs(op0));
    FORCE_RET();
}

OP(chsf64)
{
    float64 op0 = get_opf64(PARAM2);
    set_opf64(PARAM1, float64_chs(op0));
    FORCE_RET();
}

OP(sqrtf64)
{
    float64 op0 = get_opf64(PARAM2);
    set_opf64(PARAM1, float64_sqrt(op0, &CPU_FP_STATUS));
    FORCE_RET();
}

OP(addf64)
{
    float64 op0 = get_opf64(PARAM2);
    float64 op1 = get_opf64(PARAM3);
    set_opf64(PARAM1, float64_add(op0, op1, &CPU_FP_STATUS));
    FORCE_RET();
}

OP(subf64)
{
    float64 op0 = get_opf64(PARAM2);
    float64 op1 = get_opf64(PARAM3);
    set_opf64(PARAM1, float64_sub(op0, op1, &CPU_FP_STATUS));
    FORCE_RET();
}

OP(mulf64)
{
    float64 op0 = get_opf64(PARAM2);
    float64 op1 = get_opf64(PARAM3);
    set_opf64(PARAM1, float64_mul(op0, op1, &CPU_FP_STATUS));
    FORCE_RET();
}

OP(divf64)
{
    float64 op0 = get_opf64(PARAM2);
    float64 op1 = get_opf64(PARAM3);
    set_opf64(PARAM1, float64_div(op0, op1, &CPU_FP_STATUS));
    FORCE_RET();
}

OP(iround_f64)
{
    float64 op0 = get_opf64(PARAM2);
    set_opf64(PARAM1, float64_round_to_int(op0, &CPU_FP_STATUS));
    FORCE_RET();
}

OP(itrunc_f64)
{
    float64 op0 = get_opf64(PARAM2);
    set_opf64(PARAM1, float64_trunc_to_int(op0, &CPU_FP_STATUS));
    FORCE_RET();
}

OP(compare_quietf64)
{
    float64 op0 = get_opf64(PARAM2);
    float64 op1 = get_opf64(PARAM3);
    set_op(PARAM1, float64_compare_quiet(op0, op1, &CPU_FP_STATUS));
    FORCE_RET();
}

OP(movec)
{
    int op1 = get_op(PARAM1);
    uint32_t op2 = get_op(PARAM2);
    helper_movec(env, op1, op2);
}

/* Memory access.  */

#define MEMSUFFIX _raw
#include "op_mem.h"

#if !defined(CONFIG_USER_ONLY)
#define MEMSUFFIX _user
#include "op_mem.h"
#define MEMSUFFIX _kernel
#include "op_mem.h"
#endif