2010-06-23 Miguel de Icaza <miguel@novell.com>
[mono.git] / mono / mini / mini-hppa.h
1 /*
2  * mini-hppa.h: HPPA backend for the Mono code generator
3  *
4  * Copyright (c) 2007 Randolph Chung
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  */
25
26 #ifndef __MONO_MINI_HPPA_H__
27 #define __MONO_MINI_HPPA_H__
28
29 #include <mono/arch/hppa/hppa-codegen.h>
30
31 #include <glib.h>
32
33 #define MONO_ARCH_CPU_SPEC hppa_desc
34
35 /* HPPA's stack grows towards higher addresses */
36 #define MONO_ARCH_STACK_GROWS_UP
37
38 #define MONO_MAX_IREGS 32
39 #define MONO_MAX_FREGS 32
40
41 /* hppa_r20 - hppa_r22 are scratch registers
42  * hppa_r23 - hppa_r26 are the incoming argument registers
43  * hppa_r28, hppa_29 are the return value registers */
44 #define MONO_ARCH_CALLEE_REGS ((0x3f << hppa_r20) | (1 << hppa_r28) | (1 << hppa_r29))
45
46 /* hppa_r3 - hppa_r19, hppa_27, hppa_30 */
47 #define MONO_ARCH_CALLEE_SAVED_REGS ((0x1ffff << hppa_r3) | (1 << hppa_r27) | (1 << hppa_r30))
48
49 /* hppa_fr4 - hppa_fr7 are incoming argument registers
50  * hppa_fr8 - hppa_fr11 are scratch registers 
51  * hppa_fr22 - hppa_fr31 are scratch registers
52  * we reserve hppa_fr31 for code generation */
53 #define MONO_ARCH_CALLEE_FREGS ((0xff << hppa_fr4) | (0x1ff << hppa_fr22))
54
55 /* hppa_fr12 - hppa_fr21 */
56 #define MONO_ARCH_CALLEE_SAVED_FREGS (0x3ff << hppa_fr12)
57
58 #define MONO_ARCH_USE_FPSTACK FALSE
59 #define MONO_ARCH_FPSTACK_SIZE 0
60 #define MONO_ARCH_INST_FIXED_REG(desc) ((desc == 'o') ? hppa_r0 : (desc == 'a') ? hppa_r28 : (desc == 'L') ? hppa_r29 : -1)
61 #define MONO_ARCH_INST_SREG2_MASK(ins) (0)
62
63 #define MONO_ARCH_INST_IS_REGPAIR(desc) ((desc == 'l') || (desc == 'L'))
64 #define MONO_ARCH_INST_REGPAIR_REG2(desc,hreg1) ((desc == 'L') ? hppa_r28 : (desc == 'l') ? ((hreg1)+1) : -1)
65
66 #define MONO_ARCH_FRAME_ALIGNMENT 64
67 #define MONO_ARCH_CODE_ALIGNMENT 32
68 #define HPPA_STACK_LMF_OFFSET   8
69
70 /* r3-r19, r27 */
71 #define MONO_SAVED_GREGS 18
72 #define MONO_SAVED_GREGS_MASK   ((0x1ffff << hppa_r3) | (1 << hppa_r27))
73 /* fr12-fr21 */
74 #define MONO_SAVED_FREGS 10
75 #define MONO_SAVED_FREGS_MASK   (0x003ff000)
76 #define HPPA_IS_SAVED_GREG(i)   ((1 << (i)) & MONO_SAVED_GREGS_MASK)
77 #define HPPA_IS_SAVED_FREG(i)   ((1 << (i)) & MONO_SAVED_FREGS_MASK)
78
79 struct MonoLMF {
80         gpointer    previous_lmf;
81         gpointer    lmf_addr;
82         MonoMethod *method;
83         gpointer    eip; /* pc */
84         gpointer    ebp; /* sp */
85         gulong regs [MONO_SAVED_GREGS];
86         double fregs [MONO_SAVED_FREGS];
87 };
88
89 typedef struct MonoContext {
90         gulong pc;
91         gulong sp;
92         gulong regs [MONO_SAVED_GREGS];
93         double fregs [MONO_SAVED_FREGS];
94 } MonoContext;
95
96 typedef struct MonoCompileArch {
97         gint32 lmf_offset;
98         gint32 localloc_offset;
99 } MonoCompileArch;
100
101 #define MONO_CONTEXT_SET_IP(ctx,_ip) do { (ctx)->pc = (int)(_ip); } while (0) 
102 #define MONO_CONTEXT_SET_BP(ctx,_bp) do { (ctx)->sp = (int)(_bp); } while (0) 
103 #define MONO_CONTEXT_SET_SP(ctx,_sp) do { (ctx)->sp = (int)(_sp); } while (0)
104
105 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->pc))
106 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->sp))
107 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sp))
108
109 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx,start_func) do {        \
110         unsigned long sp;                                       \
111         asm volatile ("copy %%sp, %0\n" : "=r"(sp));            \
112         MONO_CONTEXT_SET_IP ((ctx), (start_func));              \
113         MONO_CONTEXT_SET_BP ((ctx), sp);                        \
114         } while (0)
115
116 #define MONO_ARCH_USE_SIGACTION 1
117
118 #define MONO_ARCH_EMULATE_FCONV_TO_I8   1
119 #define MONO_ARCH_EMULATE_LCONV_TO_R8   1
120 #define MONO_ARCH_EMULATE_LCONV_TO_R4   1
121 #define MONO_ARCH_EMULATE_CONV_R8_UN    1
122 #define MONO_ARCH_EMULATE_LCONV_TO_R8_UN 1
123 #define MONO_ARCH_EMULATE_FREM 1
124 #define MONO_ARCH_EMULATE_DIV 1
125 #define MONO_ARCH_NEED_DIV_CHECK 1
126
127 /* 
128  * HPPA does not have an addresable "cflags", so all our compare and branch
129  * instructions are combined
130  */
131
132 #define MONO_EMIT_NEW_HPPA_COND_EXC(cfg,cond,sr1,sr2,name) do { \
133                 MonoInst *inst; \
134                 inst = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
135                 inst->opcode = cond;  \
136                 inst->inst_p1 = (char*)name; \
137                 inst->sreg1 = sr1; \
138                 inst->sreg2 = sr2; \
139                 mono_bblock_add_inst ((cfg)->cbb, inst); \
140         } while (0)
141
142 #define MONO_EMIT_NEW_COMPARE_EXC(cfg, cmp_op, sreg1, sreg2, exc) \
143         MONO_EMIT_NEW_HPPA_COND_EXC (cfg, OP_HPPA_COND_EXC_##cmp_op, sreg1, sreg2, exc)
144
145 #define MONO_EMIT_NEW_COMPARE_IMM_EXC(cfg, cmp_op, sreg1, imm, exc) do { \
146                 guint32 cmp_reg; \
147                 if (!(imm)) { \
148                         cmp_reg = hppa_r0; \
149                 } \
150                 else { \
151                         cmp_reg = hppa_r1; \
152                         MONO_EMIT_NEW_ICONST (cfg, cmp_reg, (imm)); \
153                 } \
154                 MONO_EMIT_NEW_COMPARE_EXC (cfg, cmp_op, sreg1, cmp_reg, exc); \
155         } while (0)
156
157 #define MONO_EMIT_NEW_ICOMPARE_IMM_EXC(cfg, cmp_op, sreg1, imm, exc) do { \
158                 MONO_EMIT_NEW_COMPARE_IMM_EXC(cfg, cmp_op, sreg1, imm, exc); \
159         } while (0)
160
161 typedef struct {
162         gint8 reg;
163         gint8 size;
164         gint8 pass_in_reg:1;
165         int vtsize;
166         int offset;
167 } MonoHPPAArgInfo;
168
169
170 void hppa_patch (guint32 *code, const gpointer target);
171
172 #endif /* __MONO_MINI_HPPA_H__ */