* i386-tdep.c (_initialize_i386_tdep): Put void decl on separate
[external/binutils.git] / gdb / arc-tdep.c
1 /* ARC target-dependent stuff.
2    Copyright (C) 1995 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "gdbcore.h"
24 #include "target.h"
25 #include "floatformat.h"
26 #include "symtab.h"
27
28 static void codestream_read PARAMS ((unsigned int *, int));
29 static void codestream_seek PARAMS ((int));
30 static unsigned int codestream_fill PARAMS ((int));
31
32 #define CODESTREAM_BUFSIZ 16 
33 static CORE_ADDR codestream_next_addr;
34 static CORE_ADDR codestream_addr;
35 static unsigned int codestream_buf[CODESTREAM_BUFSIZ];
36 static int codestream_off;
37 static int codestream_cnt;
38
39 #define codestream_tell() (codestream_addr + codestream_off)
40 #define codestream_peek() (codestream_cnt == 0 ? \
41                            codestream_fill(1): codestream_buf[codestream_off])
42 #define codestream_get() (codestream_cnt-- == 0 ? \
43                          codestream_fill(0) : codestream_buf[codestream_off++])
44 #define OPMASK  0xf8000000
45
46 static unsigned int 
47 codestream_fill (peek_flag)
48     int peek_flag;
49 {
50   codestream_addr = codestream_next_addr;
51   codestream_next_addr += CODESTREAM_BUFSIZ;
52   codestream_off = 0;
53   codestream_cnt = CODESTREAM_BUFSIZ;
54   read_memory (codestream_addr, (char *) codestream_buf, CODESTREAM_BUFSIZ);
55   
56   if (peek_flag)
57     return (codestream_peek());
58   else
59     return (codestream_get());
60 }
61
62 static void
63 codestream_seek (place)
64     int place;
65 {
66   codestream_next_addr = place / CODESTREAM_BUFSIZ;
67   codestream_next_addr *= CODESTREAM_BUFSIZ;
68   codestream_cnt = 0;
69   codestream_fill (1);
70   while (codestream_tell() != place)
71     codestream_get ();
72 }
73
74 static void
75 codestream_read (buf, count)
76      unsigned int *buf;
77      int count;
78 {
79   unsigned int *p;
80   int i;
81   p = buf;
82   for (i = 0; i < count; i++)
83     *p++ = codestream_get ();
84 }
85
86 /*
87  * find & return amound a local space allocated, and advance codestream to
88  * first register push (if any)
89  * if entry sequence doesn't make sense, return -1, and leave 
90  * codestream pointer random
91  */
92
93 static long
94 arc_get_frame_setup (pc)
95      int pc;
96 {
97   unsigned int insn, n;
98
99   codestream_seek (pc);
100   insn = codestream_get ();
101
102   if (insn & OPMASK == 0x10000000)              /* st fp,[sp] */
103     {   
104       insn = codestream_get ();
105       if (insn & OPMASK != 0x10000000)          /* st blink,[sp,4] */
106         {
107           if (insn & OPMASK != 0x60000000)      /* for leaf, no st blink */
108             return -1;
109         }
110       else if (codestream_get () & OPMASK != 0x60000000)    /* mov fp,sp */
111         return (-1);
112
113       /* check for stack adjustment sub sp,nnn,sp */
114       insn = codestream_peek ();
115       if (insn & OPMASK == 0x50000000)
116         {
117           n = (insn & 0x000001ff ); 
118           codestream_get ();
119
120           /* this sequence is used to get the address of the return
121            * buffer for a function that returns a structure
122            */
123           insn = codestream_peek ();
124           if (insn & OPMASK == 0x60000000)
125             codestream_get ();
126
127           return n;
128         }
129       else
130         {
131           return (0);
132         }
133     }
134   return (-1);
135 }
136
137 /* return pc of first real instruction */
138 CORE_ADDR
139 skip_prologue (pc)
140      int pc;
141 {
142   unsigned int insn;
143   int i;
144   CORE_ADDR pos;
145   
146   if (arc_get_frame_setup (pc) < 0)
147     return (pc);
148   
149   /* skip over register saves */
150   for (i = 0; i < 10; i++)
151     {
152       insn = codestream_peek ();
153       if (insn & OPMASK != 0x10000000)       /* break if not st inst */
154         break;
155       codestream_get ();
156     }
157      
158   codestream_seek (pos);
159   return (codestream_tell ());
160 }
161
162 /* Return number of args passed to a frame.
163    Can return -1, meaning no way to tell.  */
164 int
165 frame_num_args (fi)
166      struct frame_info *fi;
167 {
168 #if 1
169   return -1;
170 #else
171   /* This loses because not only might the compiler not be popping the
172      args right after the function call, it might be popping args from both
173      this call and a previous one, and we would say there are more args
174      than there really are.  Is it true for ARC */
175
176   int retpc;                                            
177   unsigned char op;                                     
178   struct frame_info *pfi;
179
180   int frameless;
181
182   FRAMELESS_FUNCTION_INVOCATION (fi, frameless);
183   if (frameless)
184     /* In the absence of a frame pointer, GDB doesn't get correct values
185        for nameless arguments.  Return -1, so it doesn't print any
186        nameless arguments.  */
187     return -1;
188
189   pfi = get_prev_frame_info (fi);                       
190   if (pfi == 0)
191     {
192       /* Note:  this can happen if we are looking at the frame for
193          main, because FRAME_CHAIN_VALID won't let us go into
194          start.  If we have debugging symbols, that's not really
195          a big deal; it just means it will only show as many arguments
196          to main as are declared.  */
197       return -1;
198     }
199   else
200     {
201       retpc = pfi->pc;                                  
202       op = read_memory_integer (retpc, 1);                      
203       if (op == 0x59)                                   
204         /* pop %ecx */                         
205         return 1;                               
206         }
207       else
208         {
209           return 0;
210         }
211     }
212 #endif
213 }
214
215 /*
216  * parse the first few instructions of the function to see
217  * what registers were stored.
218  *
219  * The startup sequence can be at the start of the function.
220  * 'st fp,[sp], st blink,[sp+4], mov fp,sp' 
221  *
222  * Local space is allocated just below by sub sp,nnn,sp
223  * Next, the registers used by this function are stored.
224  */
225
226 void
227 frame_find_saved_regs (fip, fsrp)
228      struct frame_info *fip;
229      struct frame_saved_regs *fsrp;
230 {
231   long locals;
232   unsigned int insn;
233   CORE_ADDR dummy_bottom;
234   CORE_ADDR adr;
235   int i, regnum, offset;
236   
237   memset (fsrp, 0, sizeof *fsrp);
238   
239   /* if frame is the end of a dummy, compute where the
240    * beginning would be
241    */
242   dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH;
243   
244   /* check if the PC is in the stack, in a dummy frame */
245   if (dummy_bottom <= fip->pc && fip->pc <= fip->frame) 
246     {
247       /* all regs were saved by push_call_dummy () */
248       adr = fip->frame;
249       for (i = 0; i < NUM_REGS; i++) 
250         {
251           adr -= REGISTER_RAW_SIZE (i);
252           fsrp->regs[i] = adr;
253         }
254       return;
255     }
256   
257   locals = arc_get_frame_setup (get_pc_function_start (fip->pc));
258   
259   if (locals >= 0) 
260     {
261       adr = fip->frame - locals;
262       for (i = 0; i < 10; i++) 
263         {
264           insn = codestream_get ();
265           if (insn & 0xffff8000 != 0x100d8000)
266             break;
267           regnum = (insn & 0x00007c00) >> 9;
268           offset = (insn << 23) >> 23;
269           fsrp->regs[regnum] = adr + offset;
270         }
271     }
272   
273   fsrp->regs[PC_REGNUM] = fip->frame + 4;
274   fsrp->regs[FP_REGNUM] = fip->frame;
275 }
276
277 void
278 push_dummy_frame ()
279 {
280   CORE_ADDR sp = read_register (SP_REGNUM);
281   int regnum;
282   char regbuf[MAX_REGISTER_RAW_SIZE];
283
284   read_register_gen (PC_REGNUM, regbuf);
285   write_memory (sp+4, regbuf, REGISTER_SIZE);
286   read_register_gen (FP_REGNUM, regbuf);
287   write_memory (sp, regbuf, REGISTER_SIZE);
288   write_register (FP_REGNUM, sp);
289   for (regnum = 0; regnum < NUM_REGS; regnum++)
290     {
291       read_register_gen (regnum, regbuf);
292       sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum));
293     }
294   sp += (2*REGISTER_SIZE);
295   write_register (SP_REGNUM, sp);
296 }
297
298 void
299 pop_frame ()
300 {
301   struct frame_info *frame = get_current_frame ();
302   CORE_ADDR fp;
303   int regnum;
304   struct frame_saved_regs fsr;
305   char regbuf[MAX_REGISTER_RAW_SIZE];
306   
307   fp = FRAME_FP (frame);
308   get_frame_saved_regs (frame, &fsr);
309   for (regnum = 0; regnum < NUM_REGS; regnum++) 
310     {
311       CORE_ADDR adr;
312       adr = fsr.regs[regnum];
313       if (adr)
314         {
315           read_memory (adr, regbuf, REGISTER_RAW_SIZE (regnum));
316           write_register_bytes (REGISTER_BYTE (regnum), regbuf,
317                                 REGISTER_RAW_SIZE (regnum));
318         }
319     }
320   write_register (FP_REGNUM, read_memory_integer (fp, 4));
321   write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
322   write_register (SP_REGNUM, fp + 8);
323   flush_cached_frames ();
324 }
325
326 #ifdef GET_LONGJMP_TARGET
327 /* Figure out where the longjmp will land.  Slurp the args out of the stack.
328    We expect the first arg to be a pointer to the jmp_buf structure from which
329    we extract the pc (JB_PC) that we will land at.  The pc is copied into PC.
330    This routine returns true on success. */
331
332 int
333 get_longjmp_target(pc)
334      CORE_ADDR *pc;
335 {
336   char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
337   CORE_ADDR sp, jb_addr;
338
339   sp = read_register (SP_REGNUM);
340
341   if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack */
342                           buf,
343                           TARGET_PTR_BIT / TARGET_CHAR_BIT))
344     return 0;
345
346   jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
347
348   if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
349                           TARGET_PTR_BIT / TARGET_CHAR_BIT))
350     return 0;
351
352   *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
353
354   return 1;
355 }
356 #endif /* GET_LONGJMP_TARGET */
357
358 void
359 _initialize_arc_tdep ()
360 {
361   tm_print_insn = print_insn_arc;
362 }