Initial revision
[cacao.git] / alpha / cray.c
1 /****************************** asmpart.c **************************************
2 *                                                                              *
3 *   is an assembly language file, but called .c to fake the preprocessor.      *
4 *   It contains the Java-C interace functions for Alpha processors.           *
5 *                                                                              *
6 *   Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst              *
7 *                                                                              *
8 *   See file COPYRIGHT for information on usage and disclaimer of warranties   *
9 *                                                                              *
10 *   Authors: Reinhard Grafl      EMAIL: cacao@complang.tuwien.ac.at            *
11 *            Andreas  Krall      EMAIL: cacao@complang.tuwien.ac.at            *
12 *                                                                              *
13 *   Last Change: 1997/04/26                                                    *
14 *                                                                              *
15 *******************************************************************************/
16
17 #define v0      r0
18
19 #define t0      r1
20 #define t1      r2
21 #define t2      r3
22 #define t3      r4
23 #define t4      r5
24 #define t5      r6
25 #define t6      r7
26 #define t7      r8
27
28 #define s0      r9
29 #define s1      r10
30 #define s2      r11
31 #define s3      r12
32 #define s4      r13
33 #define s5      r14
34 #define s6      r15
35
36 #define a0      r16
37 #define a1      r17
38 #define a2      r18
39 #define a3      r19
40 #define a4      r20
41 #define a5      r21
42
43 #define t8      r22
44 #define t9      r23
45 #define t10     r24
46 #define t11     r25
47 #define ra      r26
48 #define t12     r27
49
50 #define pv      t12
51 #define AT      .
52 #define gp      r29
53 #define sp      r30
54 #define zero    r31
55
56 #define PAL_imb 134
57
58
59 /*      .set    noat
60         .set    noreorder
61 */
62
63         
64 /********************* exported functions and variables ***********************/
65
66 /*      .globl has_no_x_instr_set
67         .globl synchronize_caches
68         .globl asm_calljavamethod
69         .globl asm_call_jit_compiler
70         .globl asm_dumpregistersandcall
71 */
72
73 /*************************** imported functions *******************************/
74
75 /*      .globl  compiler_compile
76 */
77
78 /*********************** function has_no_x_instr_set ***************************
79 *                                                                              *
80 *   determines if the byte support instruction set (21164a and higher)         *
81 *   is available.                                                              *
82 *                                                                              *
83 *******************************************************************************/
84         zero <- t0
85         
86         .ident    has_no_x_instr_set
87         .psect code_sec, code
88 has_no_x_instr_set::
89
90 /*      .long   0x47e03c20                  ; amask   1,r0
91 */
92
93         zap   r0,1,r0
94
95         jmp     zero,(ra)                   ; return
96
97         .end    has_no_x_instr_set
98
99
100 /********************* function synchronize_caches ****************************/
101
102         .ident    synchronize_caches
103         .psect code_sec, code
104 synchronize_caches::
105
106         call_pal PAL_imb                    ; synchronise instruction cache
107         jmp     zero,(ra)                   ; return
108
109         .end    synchronize_caches
110
111
112
113 /********************* function asm_calljavamethod *****************************
114 *                                                                              *
115 *   This function calls a Java-method (which possibly needs compilation)       *
116 *   with up to 4 parameters.                                                   *
117 *                                                                              *
118 *   This functions calls the JIT-compiler which eventually translates the      *
119 *   method into machine code.                                                  *
120 *                                                                              *
121 *   An possibly throwed exception will be returned to the caller as function   *
122 *   return value, so the java method cannot return a fucntion value (this      *
123 *   function usually calls 'main' and '<clinit>' which do not return a         *
124 *   function value).                                                           *
125 *                                                                              *
126 *   C-prototype:                                                               *
127 *    javaobject_header *asm_calljavamethod (methodinfo *m,                     *
128 *         void *arg1, void *arg2, void *arg3, void *arg4);                     *
129 *                                                                              *
130 *******************************************************************************/
131
132         .ident    asm_calljavamethod
133         .psect codesect3, code
134         .external asm_call_jit_compiler
135         
136 asm_calljavamethod::
137
138 ;       ldgp    gp,0(pv)
139 ;       .prologue  1
140         lda     sp,-24(sp)                  ; allocate stack space
141         stq     ra,0(sp)                    ; save return address
142
143         stq     r16,16(sp)                  ; save method pointer for compiler
144         lda     r0,16(sp)                   ; pass pointer to method pointer via r0
145
146         bis     r17,r17,r16                 ; pass the remaining parameters
147         bis     r18,r18,r17
148         bis     r19,r19,r18
149         bis     r20,r20,r19
150
151         la     r28,asm_call_jit_compiler   ; fake virtual function call
152         ; Changed!! from lda
153         
154         stq     r28,8(sp)                   ; store function address
155         bis     sp,sp,r28                   ; set method pointer
156
157         ldq     pv,8(r28)                   ; method call as in Java
158         jmp     ra,(pv)                     ; call JIT compiler
159
160         ldq     ra,0(sp)                    ; restore return address
161         lda     sp,24(sp)                   ; free stack space
162
163         bis     r1,r1,r0                    ; pass exception to caller (C)
164         jmp     zero,(ra)
165
166         .end    asm_calljavamethod
167
168
169 /****************** function asm_call_jit_compiler *****************************
170 *                                                                              *
171 *   invokes the compiler for untranslated JavaVM methods.                      *
172 *                                                                              *
173 *   Register R0 contains a pointer to the method info structure (prepared      *
174 *   by createcompilerstub). Using the return address in R26 and the            *
175 *   offset in the LDA instruction or using the value in methodptr R28 the      *
176 *   patching address for storing the method address can be computed:           *
177 *                                                                              *
178 *   method address was either loaded using                                     *
179 *   M_LDQ (REG_PV, REG_PV, a)        ; invokestatic/special    (r27)           *
180 *   M_LDA (REG_PV, REG_RA, low)                                                *
181 *   M_LDAH(REG_PV, REG_RA, high)     ; optional                                *
182 *   or                                                                         *
183 *   M_LDQ (REG_PV, REG_METHODPTR, m) ; invokevirtual/interace (r28)           *
184 *   in the static case the method pointer can be computed using the            *
185 *   return address and the lda function following the jmp instruction          *
186 *                                                                              *
187 *******************************************************************************/
188
189
190         .ident    asm_call_jit_compiler
191         .psect code_sec1, code
192         .external compiler_compile
193
194 asm_call_jit_compiler::
195
196 ;       ldgp    gp,0(pv)
197 ;       .prologue  1
198         ldl     r22,-8(ra)              ; load instruction LDQ PV,xxx(ryy)
199         srl     r22,16,r22              ; shift right register number ryy
200         and     r22,31,r22              ; isolate register number
201         subl    r22,28,r22              ; test for REG_METHODPTR
202         beq     r22,noregchange       
203         ldl     r22,0(ra)               ; load instruction LDA PV,xxx(RA)
204         sll     r22,48,r22
205         sra     r22,48,r22              ; isolate offset
206         addq    r22,ra,r28              ; compute update address
207         ldl     r22,4(ra)               ; load instruction LDAH PV,xxx(PV)
208         srl     r22,16,r22              ; isolate instruction code
209         lda     r22,-0x177b(r22)        ; test for LDAH
210         bne     r22,noregchange       
211         ldl     r22,0(ra)               ; load instruction LDA PV,xxx(RA)
212         sll     r22,16,r22              ; compute high offset
213         addl    r22,0,r22               ; sign extend high offset
214         addq    r22,r28,r28             ; compute update address
215 noregchange:
216         lda     sp,-14*8(sp)            ; reserve stack space
217         stq     r16,0*8(sp)             ; save all argument registers
218         stq     r17,1*8(sp)             ; they could be used by method
219         stq     r18,2*8(sp)
220         stq     r19,3*8(sp)
221         stq     r20,4*8(sp)
222         stq     r21,5*8(sp)
223         stt     f16,6*8(sp)
224         stt     f17,7*8(sp)
225         stt     f18,8*8(sp)
226         stt     f19,9*8(sp)
227         stt     f20,10*8(sp)
228         stt     f21,11*8(sp)
229         stq     r28,12*8(sp)            ; save method pointer
230         stq     ra,13*8(sp)             ; save return address
231
232         ldq     r16,0(r0)               ; pass 'methodinfo' pointer to
233         bsr     ra,compiler_compile     ; compiler
234 ;       ldgp    gp,0(ra)
235
236         call_pal PAL_imb                ; synchronise instruction cache
237
238         ldq     r16,0*8(sp)             ; load argument registers
239         ldq     r17,1*8(sp)
240         ldq     r18,2*8(sp)
241         ldq     r19,3*8(sp)
242         ldq     r20,4*8(sp)
243         ldq     r21,5*8(sp)
244         ldt     f16,6*8(sp)
245         ldt     f17,7*8(sp)
246         ldt     f18,8*8(sp)
247         ldt     f19,9*8(sp)
248         ldt     f20,10*8(sp)
249         ldt     f21,11*8(sp)
250         ldq     r28,12*8(sp)            ; load method pointer
251         ldq     ra,13*8(sp)             ; load return address
252         lda     sp,14*8(sp)             ; deallocate stack area
253
254         ldl     r22,-8(ra)              ; load instruction LDQ PV,xxx(ryy)
255         sll     r22,48,r22
256         sra     r22,48,r22              ; isolate offset
257
258         addq    r22,r28,r22             ; compute update address via method pointer
259         stq     r0,0(r22)               ; save new method address there
260
261     bis     r0,r0,pv                ; load method address into pv
262
263         jmp     zero, (pv)              ; and call method. The method returns
264                                         ; directly to the caller (ra).
265                                                                  
266         .end    asm_call_jit_compiler
267
268
269 /****************** function asm_dumpregistersandcall **************************
270 *                                                                              *
271 *   This funtion saves all callee saved registers and calls the function       *
272 *   which is passed as parameter.                                              *
273 *                                                                              *
274 *   This function is needed by the garbage collector, which needs to access    *
275 *   all registers which are stored on the stack. Unused registers are          *
276 *   cleared to avoid intererances with the GC.                                *
277 *                                                                              *
278 *   void asm_dumpregistersandcall (functionptr f);                             *
279 *                                                                              *
280 *******************************************************************************/
281
282         .ident    asm_dumpregistersandcall
283         .psect code_sec2, code
284 asm_dumpregistersandcall::
285         lda     sp,-16*8(sp)            ; allocate stack
286         stq     ra,0(sp)                ; save return address
287
288         stq     r9,1*8(sp)              ; save all callee saved registers
289         stq     r10,2*8(sp)             ; intialize the remaining registers
290         stq     r11,3*8(sp)
291         stq     r12,4*8(sp)
292         stq     r13,5*8(sp)
293         stq     r14,6*8(sp)
294         stq     r15,7*8(sp)
295         stt     f2,8*8(sp)
296         stt     f3,9*8(sp)
297         stt     f4,10*8(sp)
298         stt     f5,11*8(sp)
299         stt     f6,12*8(sp)
300         stt     f7,13*8(sp)
301         stt     f8,14*8(sp)
302         stt     f9,15*8(sp)
303
304         bis     zero,zero,r0           ; intialize the remaining registers
305         bis     zero,zero,r1
306         bis     zero,zero,r2
307         bis     zero,zero,r3
308         bis     zero,zero,r4
309         bis     zero,zero,r5
310         bis     zero,zero,r6
311         bis     zero,zero,r7
312         bis     zero,zero,r8
313         bis     zero,zero,r17
314         bis     zero,zero,r18
315         bis     zero,zero,r19
316         bis     zero,zero,r20
317         bis     zero,zero,r21
318         bis     zero,zero,r22
319         bis     zero,zero,r23
320         bis     zero,zero,r24
321         bis     zero,zero,r25
322         bis     zero,zero,r26
323         bis     zero,zero,r27
324         bis     zero,zero,r28
325         bis     zero,zero,r29
326         cpys    f31,f31,f0
327         cpys    f31,f31,f1
328         cpys    f31,f31,f10
329         cpys    f31,f31,f11
330         cpys    f31,f31,f12
331         cpys    f31,f31,f13
332         cpys    f31,f31,f14
333         cpys    f31,f31,f15
334         cpys    f31,f31,f16
335         cpys    f31,f31,f17
336         cpys    f31,f31,f18
337         cpys    f31,f31,f19
338         cpys    f31,f31,f20
339         cpys    f31,f31,f21
340         cpys    f31,f31,f22
341         cpys    f31,f31,f23
342         cpys    f31,f31,f24
343         cpys    f31,f31,f25
344         cpys    f31,f31,f26
345         cpys    f31,f31,f27
346         cpys    f31,f31,f28
347         cpys    f31,f31,f29
348         cpys    f31,f31,f30
349
350         bis     r16,r16,pv              ; load function pointer
351         jmp     ra,(pv)                 ; and call function
352
353         ldq     ra,0(sp)                ; load return address
354         lda     sp,16*8(sp)             ; deallocate stack
355         jmp     zero,(ra)               ; return
356
357         .end    
358