/
mini-hppa.c
2935 lines (2621 loc) · 79.9 KB
/
mini-hppa.c
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* mini-hppa.c: HPPA backend for the Mono code generator
*
* Copyright (c) 2007 Randolph Chung
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include "mini.h"
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <unistd.h>
#include <sys/mman.h>
#include <mono/metadata/appdomain.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/tokentype.h>
#include <mono/utils/mono-math.h>
#include "mini-hppa.h"
#include "trace.h"
#include "cpu-hppa.h"
#define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
#define SIGNAL_STACK_SIZE (64 * 1024)
#define DEBUG(a) // a
#define DEBUG_FUNC_ENTER() // printf("Entering %s\n", __FUNCTION__)
#define DEBUG_FUNC_EXIT() // printf("Exiting %s\n", __FUNCTION__)
static const guchar
branch_b0_table [] = {
TRUE, /* OP_HPPA_BEQ */
FALSE, /* OP_HPPA_BGE */
FALSE, /* OP_HPPA_BGT */
TRUE, /* OP_HPPA_BLE */
TRUE, /* OP_HPPA_BLT */
FALSE, /* OP_HPPA_BNE */
FALSE, /* OP_HPPA_BGE_UN */
FALSE, /* OP_HPPA_BGT_UN */
TRUE, /* OP_HPPA_BLE_UN */
TRUE, /* OP_HPPA_BLT_UN */
};
static const guchar
branch_b1_table [] = {
HPPA_CMP_COND_EQ, /* OP_HPPA_BEQ */
HPPA_CMP_COND_SLT, /* OP_HPPA_BGE */
HPPA_CMP_COND_SLE, /* OP_HPPA_BGT */
HPPA_CMP_COND_SLE, /* OP_HPPA_BLE */
HPPA_CMP_COND_SLT, /* OP_HPPA_BLT */
HPPA_CMP_COND_EQ, /* OP_HPPA_BNE_UN */
HPPA_CMP_COND_ULT, /* OP_HPPA_BGE_UN */
HPPA_CMP_COND_ULE, /* OP_HPPA_BGT_UN */
HPPA_CMP_COND_ULE, /* OP_HPPA_BLE_UN */
HPPA_CMP_COND_ULT, /* OP_HPPA_BLT_UN */
};
/* Note that these are inverted from the OP_xxx, because we nullify
* the branch if the condition is met
*/
static const guchar
float_branch_table [] = {
26, /* OP_FBEQ */
11, /* OP_FBGE */
15, /* OP_FBGT */
19, /* OP_FBLE */
23, /* OP_FBLT */
4, /* OP_FBNE_UN */
8, /* OP_FBGE_UN */
13, /* OP_FBGT_UN */
17, /* OP_FBLE_UN */
20, /* OP_FBLT_UN */
};
static const guchar
float_ceq_table [] = {
26, /* OP_FCEQ */
15, /* OP_FCGT */
13, /* OP_FCGT_UN */
23, /* OP_FCLT */
21, /* OP_FCLT_UN */
};
/*
* Branches have short (14 or 17 bit) targets on HPPA. To make longer jumps,
* we will need to rely on stubs - basically we create stub structures in
* the epilogue that uses a long branch to the destination, and any short
* jumps inside a method that cannot reach the destination directly will
* branch first to the stub.
*/
typedef struct MonoOvfJump {
union {
MonoBasicBlock *bb;
const char *exception;
} data;
guint32 ip_offset;
} MonoOvfJump;
/* Create a literal 0.0 double for FNEG */
double hppa_zero = 0;
const char*
mono_arch_regname (int reg)
{
static const char * rnames[] = {
"hppa_r0", "hppa_r1", "hppa_rp", "hppa_r3", "hppa_r4",
"hppa_r5", "hppa_r6", "hppa_r7", "hppa_r8", "hppa_r9",
"hppa_r10", "hppa_r11", "hppa_r12", "hppa_r13", "hppa_r14",
"hppa_r15", "hppa_r16", "hppa_r17", "hppa_r18", "hppa_r19",
"hppa_r20", "hppa_r21", "hppa_r22", "hppa_r23", "hppa_r24",
"hppa_r25", "hppa_r26", "hppa_r27", "hppa_r28", "hppa_r29",
"hppa_sp", "hppa_r31"
};
if (reg >= 0 && reg < MONO_MAX_IREGS)
return rnames [reg];
return "unknown";
}
const char*
mono_arch_fregname (int reg)
{
static const char *rnames [] = {
"hppa_fr0", "hppa_fr1", "hppa_fr2", "hppa_fr3", "hppa_fr4",
"hppa_fr5", "hppa_fr6", "hppa_fr7", "hppa_fr8", "hppa_fr9",
"hppa_fr10", "hppa_fr11", "hppa_fr12", "hppa_fr13", "hppa_fr14",
"hppa_fr15", "hppa_fr16", "hppa_fr17", "hppa_fr18", "hppa_fr19",
"hppa_fr20", "hppa_fr21", "hppa_fr22", "hppa_fr23", "hppa_fr24",
"hppa_fr25", "hppa_fr26", "hppa_fr27", "hppa_fr28", "hppa_fr29",
"hppa_fr30", "hppa_fr31",
};
if (reg >= 0 && reg < MONO_MAX_FREGS)
return rnames [reg];
else
return "unknown";
}
/*
* Initialize the cpu to execute managed code.
*/
void
mono_arch_cpu_init (void)
{
guint32 dummy;
mono_arch_cpu_optimizazions(&dummy);
}
/*
* Initialize architecture specific code.
*/
void
mono_arch_init (void)
{
}
/*
* Cleanup architecture specific code.
*/
void
mono_arch_cleanup (void)
{
}
/*
* This function returns the optimizations supported on this cpu.
*/
guint32
mono_arch_cpu_optimizazions (guint32 *exclude_mask)
{
guint32 opts = 0;
*exclude_mask = 0;
return opts;
}
void
mono_arch_flush_icache (guint8 *code, gint size)
{
guint8* p = (guint8*)((guint32)code & ~(0x3f));
guint8* end = (guint8*)((guint32)code + size);
while (p < end) {
__asm__ __volatile__ ("fdc %%r0(%%sr3, %0)\n"
"sync\n"
"fic %%r0(%%sr3, %0)\n"
"sync\n"
: : "r"(p));
p += 32; /* can be 64 on pa20 cpus */
}
}
void
mono_arch_flush_register_windows (void)
{
/* No register windows on hppa */
}
typedef enum {
ArgInIReg,
ArgInIRegPair,
ArgInFReg,
ArgInDReg,
ArgOnStack,
} ArgStorage;
typedef struct {
gint16 offset;
gint16 size;
guint8 type;
gint8 reg;
ArgStorage storage;
} ArgInfo;
typedef struct {
int nargs;
guint32 stack_usage;
int struct_return;
ArgInfo ret;
ArgInfo sig_cookie;
ArgInfo args [1];
} CallInfo;
#define PARAM_REGS 4
#define ARGS_OFFSET 36
static void
add_parameter (CallInfo *cinfo, ArgInfo *ainfo, MonoType *type)
{
int is_fp = (type->type == MONO_TYPE_R4 || type->type == MONO_TYPE_R8);
int ofs, align;
DEBUG_FUNC_ENTER ();
ainfo->reg = -1;
ainfo->size = mono_type_size (type, &align);
ainfo->type = type->type;
if (ainfo->size <= 4) {
cinfo->stack_usage += 4;
ainfo->offset = cinfo->stack_usage - (4 - ainfo->size);
}
else if (ainfo->size <= 8)
{
cinfo->stack_usage += 8;
cinfo->stack_usage = ALIGN_TO (cinfo->stack_usage, 8);
ainfo->offset = cinfo->stack_usage - (8 - ainfo->size);
}
else
{
cinfo->stack_usage += ainfo->size;
cinfo->stack_usage = ALIGN_TO (cinfo->stack_usage, align);
ainfo->offset = cinfo->stack_usage;
}
ofs = (ALIGN_TO (ainfo->offset, 4) - ARGS_OFFSET) / 4;
if (ofs < PARAM_REGS) {
if (!is_fp) {
if (ainfo->size <= 4)
ainfo->storage = ArgInIReg;
else
ainfo->storage = ArgInIRegPair;
ainfo->reg = hppa_r26 - ofs;
} else if (type->type == MONO_TYPE_R4) {
ainfo->storage = ArgInFReg;
ainfo->reg = hppa_fr4 + ofs;
} else { /* type->type == MONO_TYPE_R8 */
ainfo->storage = ArgInDReg;
ainfo->reg = hppa_fr4 + ofs;
}
}
else {
/* frame pointer based offset */
ainfo->reg = hppa_r3;
ainfo->storage = ArgOnStack;
}
/* All offsets are negative relative to the frame pointer */
ainfo->offset = -ainfo->offset;
DEBUG_FUNC_EXIT ();
}
static void
analyze_return (CallInfo *cinfo, MonoMethodSignature *sig)
{
MonoType *type;
int align;
int size;
type = sig->ret;
size = mono_type_size (type, &align);
/* ref: mono_type_to_stind */
cinfo->ret.type = type->type;
if (type->byref) {
cinfo->ret.storage = ArgInIReg;
cinfo->ret.reg = hppa_r28;
} else {
handle_enum:
switch (type->type) {
case MONO_TYPE_VOID:
break;
case MONO_TYPE_BOOLEAN:
case MONO_TYPE_I1:
case MONO_TYPE_U1:
case MONO_TYPE_I2:
case MONO_TYPE_U2:
case MONO_TYPE_CHAR:
case MONO_TYPE_I4:
case MONO_TYPE_U4:
case MONO_TYPE_I:
case MONO_TYPE_U:
case MONO_TYPE_PTR:
case MONO_TYPE_FNPTR:
case MONO_TYPE_CLASS:
case MONO_TYPE_STRING:
case MONO_TYPE_OBJECT:
case MONO_TYPE_SZARRAY:
case MONO_TYPE_ARRAY:
cinfo->ret.storage = ArgInIReg;
cinfo->ret.reg = hppa_r28;
break;
case MONO_TYPE_U8:
case MONO_TYPE_I8:
cinfo->ret.storage = ArgInIRegPair;
cinfo->ret.reg = hppa_r28;
break;
case MONO_TYPE_R4:
cinfo->ret.storage = ArgInFReg;
cinfo->ret.reg = hppa_fr4;
break;
case MONO_TYPE_R8:
cinfo->ret.storage = ArgInDReg;
cinfo->ret.reg = hppa_fr4;
break;
case MONO_TYPE_GENERICINST:
type = &type->data.generic_class->container_class->byval_arg;
goto handle_enum;
case MONO_TYPE_VALUETYPE:
if (type->data.klass->enumtype) {
type = mono_class_enum_basetype (type->data.klass);
goto handle_enum;
}
/* Fall through */
case MONO_TYPE_TYPEDBYREF:
cinfo->struct_return = 1;
/* cinfo->ret.storage tells us how the ABI expects
* the parameter to be returned
*/
if (size <= 4) {
cinfo->ret.storage = ArgInIReg;
cinfo->ret.reg = hppa_r28;
} else if (size <= 8) {
cinfo->ret.storage = ArgInIRegPair;
cinfo->ret.reg = hppa_r28;
} else {
cinfo->ret.storage = ArgOnStack;
cinfo->ret.reg = hppa_sp;
}
/* We always allocate stack space for this because the
* arch-indep code expects us to
*/
cinfo->stack_usage += size;
cinfo->stack_usage = ALIGN_TO (cinfo->stack_usage, align);
cinfo->ret.offset = -cinfo->stack_usage;
break;
default:
g_error ("Can't handle as return value 0x%x", sig->ret->type);
}
}
}
/*
* get_call_info:
*
* Obtain information about a call according to the calling convention.
*/
static CallInfo*
get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
{
guint32 i;
int n = sig->hasthis + sig->param_count;
CallInfo *cinfo;
MonoType *type;
MonoType ptrtype;
int dummy;
ptrtype.type = MONO_TYPE_PTR;
DEBUG_FUNC_ENTER();
cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
/* The area below ARGS_OFFSET is the linkage area... */
cinfo->stack_usage = ARGS_OFFSET - 4;
/* -4, because the first argument will allocate the area it needs */
/* this */
if (sig->hasthis) {
add_parameter (cinfo, cinfo->args + 0, &ptrtype);
DEBUG (printf ("param <this>: assigned to reg %s offset %d\n", mono_arch_regname (cinfo->args[0].reg), cinfo->args[0].offset));
}
/* TODO: What to do with varargs? */
for (i = 0; i < sig->param_count; ++i) {
ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
if (sig->params [i]->byref)
type = &ptrtype;
else
type = mono_type_get_underlying_type (sig->params [i]);
add_parameter (cinfo, ainfo, type);
DEBUG (printf ("param %d: type %d size %d assigned to reg %s offset %d\n", i, type->type, mono_type_size (type, &dummy), mono_arch_regname (ainfo->reg), ainfo->offset));
}
analyze_return (cinfo, sig);
DEBUG_FUNC_EXIT();
return cinfo;
}
GList *
mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
{
GList *vars = NULL;
int i;
DEBUG_FUNC_ENTER();
for (i = 0; i < cfg->num_varinfo; i++) {
MonoInst *ins = cfg->varinfo [i];
MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
/* unused vars */
if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
continue;
if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
(ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
continue;
if (mono_is_regsize_var (ins->inst_vtype)) {
g_assert (MONO_VARINFO (cfg, i)->reg == -1);
g_assert (i == vmv->idx);
vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
}
}
DEBUG_FUNC_EXIT();
return vars;
}
GList *
mono_arch_get_global_int_regs (MonoCompile *cfg)
{
GList *regs = NULL;
int i;
/* r3 is sometimes used as our frame pointer, so don't allocate it
* r19 is the GOT pointer, don't allocate it either
*/
DEBUG_FUNC_ENTER();
for (i = 4; i <= 18; i++)
regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
DEBUG_FUNC_EXIT();
return regs;
}
/*
* mono_arch_regalloc_cost:
*
* Return the cost, in number of memory references, of the action of
* allocating the variable VMV into a register during global register
* allocation.
*/
guint32
mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
{
/* FIXME */
return 0;
}
/*
* Set var information according to the calling convention.
* The locals var stuff should most likely be split in another method.
*
* updates m->stack_offset based on the amount of stack space needed for
* local vars
*/
void
mono_arch_allocate_vars (MonoCompile *m)
{
MonoMethodSignature *sig;
MonoMethodHeader *header;
MonoInst *inst;
int i, offset, size, align, curinst;
guint32 stack_ptr;
guint rettype;
CallInfo *cinfo;
DEBUG_FUNC_ENTER();
m->flags |= MONO_CFG_HAS_SPILLUP;
header = m->header;
sig = mono_method_signature (m->method);
DEBUG (printf ("Allocating locals - incoming params:\n"));
cinfo = get_call_info (sig, FALSE);
/*
* We use the ABI calling conventions for managed code as well.
*/
if (m->flags & MONO_CFG_HAS_ALLOCA) {
stack_ptr = hppa_r4;
m->used_int_regs |= 1 << hppa_r4;
} else {
stack_ptr = hppa_sp;
}
/* Before this function is called, we would have looked at all
* calls from this method and figured out how much space is needed
* for the param area.
*
* Locals are allocated backwards, right before the param area
*/
/* TODO: in some cases we don't need the frame pointer... */
m->frame_reg = hppa_r3;
offset = m->param_area;
/* Return values can be passed back either in four ways:
* r28 is used for data <= 4 bytes (32-bit ABI)
* r28/r29 are used for data >4 && <= 8 bytes
* fr4 is used for floating point data
* data larger than 8 bytes is returned on the stack pointed to
* by r28
*
* This code needs to be in sync with how CEE_RET is handled
* in mono_method_to_ir (). In some cases when we return small
* structs, the ABI specifies that they should be returned in
* registers, but the code in mono_method_to_ir () always emits
* a memcpy for valuetype returns, so we need to make sure we
* allocate space on the stack for this copy.
*/
if (cinfo->struct_return) {
/* this is used to stash the incoming r28 pointer */
offset += sizeof (gpointer);
m->ret->opcode = OP_REGOFFSET;
m->ret->inst_basereg = stack_ptr;
m->ret->inst_offset = -offset;
} else if (sig->ret->type != MONO_TYPE_VOID) {
m->ret->opcode = OP_REGVAR;
m->ret->inst_c0 = cinfo->ret.reg;
}
curinst = m->locals_start;
for (i = curinst; i < m->num_varinfo; ++i) {
inst = m->varinfo [i];
if (inst->opcode == OP_REGVAR) {
DEBUG (printf ("allocating local %d to %s\n", i, mono_arch_regname (inst->dreg)));
continue;
}
if (inst->flags & MONO_INST_IS_DEAD)
continue;
/* inst->backend.is_pinvoke indicates native sized value types, this is used by the
* pinvoke wrappers when they call functions returning structure */
if (inst->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
else
size = mini_type_stack_size (cfg->generic_sharing_context, inst->inst_vtype, &align);
/*
* This is needed since structures containing doubles must be doubleword
* aligned.
* FIXME: Do this only if needed.
*/
if (MONO_TYPE_ISSTRUCT (inst->inst_vtype))
align = 8;
/*
* variables are accessed as negative offsets from hppa_sp
*/
inst->opcode = OP_REGOFFSET;
inst->inst_basereg = stack_ptr;
offset += size;
offset = ALIGN_TO (offset, align);
inst->inst_offset = -offset;
DEBUG (printf ("allocating local %d (size = %d) to [%s - %d]\n", i, size, mono_arch_regname (inst->inst_basereg), -inst->inst_offset));
}
if (sig->call_convention == MONO_CALL_VARARG) {
/* TODO */
}
for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
ArgInfo *ainfo = &cinfo->args [i];
inst = m->args [i];
if (inst->opcode != OP_REGVAR) {
switch (ainfo->storage) {
case ArgInIReg:
case ArgInIRegPair:
case ArgInFReg:
case ArgInDReg:
/* Currently mono requests all incoming registers
* be assigned to a stack location :-(
*/
#if 0
if (!(inst->flags & (MONO_INST_VOLATILE | MONO_INST_INDIRECT))) {
inst->opcode = OP_REGVAR;
inst->dreg = ainfo->reg;
DEBUG (printf ("param %d in register %s\n", i, mono_arch_regname (inst->dreg)));
break;
}
#endif
/* fallthrough */
case ArgOnStack:
inst->opcode = OP_REGOFFSET;
inst->inst_basereg = hppa_r3;
inst->inst_offset = ainfo->offset;
DEBUG (printf ("param %d stored on stack [%s - %d]\n", i, mono_arch_regname (hppa_r3), -inst->inst_offset));
break;
}
}
}
m->stack_offset = offset; /* Includes cfg->param_area */
g_free (cinfo);
DEBUG_FUNC_EXIT();
}
/*
* take the arguments and generate the arch-specific
* instructions to properly call the function in call.
* This includes pushing, moving arguments to the right register
* etc.
*
* sets call->stack_usage and cfg->param_area
*/
MonoCallInst*
mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual)
{
MonoInst *arg, *in;
MonoMethodSignature *sig;
int i, n;
CallInfo *cinfo;
ArgInfo *ainfo;
DEBUG_FUNC_ENTER();
DEBUG (printf ("is_virtual = %d\n", is_virtual));
sig = call->signature;
n = sig->param_count + sig->hasthis;
DEBUG (printf ("Calling method with %d parameters\n", n));
cinfo = get_call_info (sig, sig->pinvoke);
// DEBUG
g_assert (sig->call_convention != MONO_CALL_VARARG);
for (i = 0; i < n; ++i) {
ainfo = &cinfo->args [i];
if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
/* TODO */
}
if (is_virtual && i == 0) {
/* the argument will be attached to the call instruction */
in = call->args [i];
call->used_iregs |= 1 << ainfo->reg;
} else {
MONO_INST_NEW (cfg, arg, OP_OUTARG);
in = call->args [i];
arg->cil_code = in->cil_code;
arg->inst_left = in;
arg->inst_call = call;
arg->type = in->type;
/* prepend, we'll need to reverse them later */
arg->next = call->out_args;
call->out_args = arg;
switch (ainfo->storage) {
case ArgInIReg:
case ArgInIRegPair: {
MonoHPPAArgInfo *ai = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoHPPAArgInfo));
ai->reg = ainfo->reg;
ai->size = ainfo->size;
ai->offset = ainfo->offset;
ai->pass_in_reg = 1;
arg->backend.data = ai;
call->used_iregs |= 1 << ainfo->reg;
if (ainfo->storage == ArgInIRegPair)
call->used_iregs |= 1 << (ainfo->reg + 1);
if (ainfo->type == MONO_TYPE_VALUETYPE)
arg->opcode = OP_OUTARG_VT;
break;
}
case ArgOnStack: {
MonoHPPAArgInfo *ai = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoHPPAArgInfo));
ai->reg = hppa_sp;
ai->size = ainfo->size;
ai->offset = ainfo->offset;
ai->pass_in_reg = 0;
arg->backend.data = ai;
if (ainfo->type == MONO_TYPE_VALUETYPE)
arg->opcode = OP_OUTARG_VT;
else
arg->opcode = OP_OUTARG_MEMBASE;
call->used_iregs |= 1 << ainfo->reg;
break;
}
case ArgInFReg:
arg->backend.reg3 = ainfo->reg;
arg->opcode = OP_OUTARG_R4;
call->used_fregs |= 1 << ainfo->reg;
break;
case ArgInDReg:
arg->backend.reg3 = ainfo->reg;
arg->opcode = OP_OUTARG_R8;
call->used_fregs |= 1 << ainfo->reg;
break;
default:
NOT_IMPLEMENTED;
}
}
}
/*
* Reverse the call->out_args list.
*/
{
MonoInst *prev = NULL, *list = call->out_args, *next;
while (list) {
next = list->next;
list->next = prev;
prev = list;
list = next;
}
call->out_args = prev;
}
call->stack_usage = cinfo->stack_usage;
cfg->param_area = MAX (cfg->param_area, call->stack_usage);
cfg->param_area = ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT);
cfg->flags |= MONO_CFG_HAS_CALLS;
g_free (cinfo);
DEBUG_FUNC_EXIT();
return call;
}
void
mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
{
}
void
mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
{
DEBUG_FUNC_ENTER();
DEBUG_FUNC_EXIT();
}
static void
insert_after_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst *to_insert)
{
if (ins == NULL) {
ins = bb->code;
bb->code = to_insert;
to_insert->next = ins;
} else {
to_insert->next = ins->next;
ins->next = to_insert;
}
}
#define NEW_INS(cfg,dest,op) do { \
(dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
(dest)->opcode = (op); \
insert_after_ins (bb, last_ins, (dest)); \
} while (0)
static int
map_to_reg_reg_op (int op)
{
switch (op) {
case OP_ADD_IMM:
return CEE_ADD;
case OP_SUB_IMM:
return CEE_SUB;
case OP_AND_IMM:
return CEE_AND;
case OP_COMPARE_IMM:
return OP_COMPARE;
case OP_ADDCC_IMM:
return OP_ADDCC;
case OP_ADC_IMM:
return OP_ADC;
case OP_SUBCC_IMM:
return OP_SUBCC;
case OP_SBB_IMM:
return OP_SBB;
case OP_OR_IMM:
return CEE_OR;
case OP_XOR_IMM:
return CEE_XOR;
case OP_MUL_IMM:
return CEE_MUL;
case OP_LOAD_MEMBASE:
return OP_LOAD_MEMINDEX;
case OP_LOADI4_MEMBASE:
return OP_LOADI4_MEMINDEX;
case OP_LOADU4_MEMBASE:
return OP_LOADU4_MEMINDEX;
case OP_LOADU1_MEMBASE:
return OP_LOADU1_MEMINDEX;
case OP_LOADI2_MEMBASE:
return OP_LOADI2_MEMINDEX;
case OP_LOADU2_MEMBASE:
return OP_LOADU2_MEMINDEX;
case OP_LOADI1_MEMBASE:
return OP_LOADI1_MEMINDEX;
case OP_LOADR4_MEMBASE:
return OP_LOADR4_MEMINDEX;
case OP_LOADR8_MEMBASE:
return OP_LOADR8_MEMINDEX;
case OP_STOREI1_MEMBASE_REG:
return OP_STOREI1_MEMINDEX;
case OP_STOREI2_MEMBASE_REG:
return OP_STOREI2_MEMINDEX;
case OP_STOREI4_MEMBASE_REG:
return OP_STOREI4_MEMINDEX;
case OP_STORE_MEMBASE_REG:
return OP_STORE_MEMINDEX;
case OP_STORER4_MEMBASE_REG:
return OP_STORER4_MEMINDEX;
case OP_STORER8_MEMBASE_REG:
return OP_STORER8_MEMINDEX;
case OP_STORE_MEMBASE_IMM:
return OP_STORE_MEMBASE_REG;
case OP_STOREI1_MEMBASE_IMM:
return OP_STOREI1_MEMBASE_REG;
case OP_STOREI2_MEMBASE_IMM:
return OP_STOREI2_MEMBASE_REG;
case OP_STOREI4_MEMBASE_IMM:
return OP_STOREI4_MEMBASE_REG;
}
g_assert_not_reached ();
}
/*
* Remove from the instruction list the instructions that can't be
* represented with very simple instructions with no register
* requirements.
*/
void
mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
{
MonoInst *ins, *next, *temp, *last_ins = NULL;
int imm;
MONO_BB_FOR_EACH_INS (bb, ins) {
loop_start:
switch (ins->opcode) {
case OP_ADD_IMM:
case OP_ADDCC_IMM:
if (!hppa_check_bits (ins->inst_imm, 11)) {
NEW_INS (cfg, temp, OP_ICONST);
temp->inst_c0 = ins->inst_imm;
temp->dreg = mono_alloc_ireg (cfg);
ins->sreg2 = temp->dreg;
ins->opcode = map_to_reg_reg_op (ins->opcode);
}
break;
case OP_SUB_IMM:
case OP_SUBCC_IMM:
if (!hppa_check_bits (ins->inst_imm, 11)) {
NEW_INS (cfg, temp, OP_ICONST);
temp->inst_c0 = ins->inst_imm;
temp->dreg = mono_alloc_ireg (cfg);
ins->sreg2 = temp->dreg;
ins->opcode = map_to_reg_reg_op (ins->opcode);
}
break;
case OP_MUL_IMM:
if (ins->inst_imm == 1) {
ins->opcode = OP_MOVE;
break;
}
if (ins->inst_imm == 0) {
ins->opcode = OP_ICONST;
ins->inst_c0 = 0;
break;
}
imm = mono_is_power_of_two (ins->inst_imm);
if (imm > 0) {
ins->opcode = OP_SHL_IMM;
ins->inst_imm = imm;
break;
}
else {
int tmp = mono_alloc_ireg (cfg);
NEW_INS (cfg, temp, OP_ICONST);
temp->inst_c0 = ins->inst_c0;
temp->dreg = tmp;
ins->opcode = CEE_MUL;
ins->sreg2 = tmp;
/* Need to rewrite the CEE_MUL too... */
goto loop_start;
}
break;
case CEE_MUL: {
int freg1 = mono_alloc_freg (cfg);
int freg2 = mono_alloc_freg (cfg);
NEW_INS(cfg, temp, OP_STORE_MEMBASE_REG);
temp->sreg1 = ins->sreg1;
temp->inst_destbasereg = hppa_sp;
temp->inst_offset = -16;
NEW_INS(cfg, temp, OP_LOADR4_MEMBASE);
temp->dreg = freg1;
temp->inst_basereg = hppa_sp;
temp->inst_offset = -16;
NEW_INS(cfg, temp, OP_STORE_MEMBASE_REG);
temp->sreg1 = ins->sreg2;
temp->inst_destbasereg = hppa_sp;
temp->inst_offset = -16;
NEW_INS(cfg, temp, OP_LOADR4_MEMBASE);
temp->dreg = freg2;
temp->inst_basereg = hppa_sp;
temp->inst_offset = -16;
NEW_INS (cfg, temp, OP_HPPA_XMPYU);
temp->dreg = freg2;
temp->sreg1 = freg1;
temp->sreg2 = freg2;
NEW_INS(cfg, temp, OP_HPPA_STORER4_RIGHT);
temp->sreg1 = freg2;
temp->inst_destbasereg = hppa_sp;
temp->inst_offset = -16;
ins->opcode = OP_LOAD_MEMBASE;
ins->inst_basereg = hppa_sp;
ins->inst_offset = -16;
}
break;
default:
break;
}
last_ins = ins;
}
bb->last_ins = last_ins;
bb->max_vreg = cfg->next_vreg;
}
void
hppa_patch (guint32 *code, const gpointer target)
{
guint32 ins = *code;
gint32 val = (gint32)target;
gint32 disp = (val - (gint32)code - 8) >> 2;
int reg1, reg2;