Make exit/stop return correct exit value; Add line number tracing.
[platform/upstream/binutils.git] / sim / d10v / interp.c
1 #include <signal.h>
2 #include "sysdep.h"
3 #include "bfd.h"
4 #include "remote-sim.h"
5
6 #include "d10v_sim.h"
7
8 #define IMEM_SIZE 18    /* D10V instruction memory size is 18 bits */
9 #define DMEM_SIZE 16    /* Data memory */
10
11 enum _leftright { LEFT_FIRST, RIGHT_FIRST };
12
13 int d10v_debug;
14 host_callback *d10v_callback;
15 long ins_type_counters[ (int)INS_MAX ];
16 long left_nops, right_nops;
17
18 uint16 OP[4];
19
20 static struct hash_entry *lookup_hash PARAMS ((uint32 ins, int size));
21
22 #define MAX_HASH  63
23 struct hash_entry
24 {
25   struct hash_entry *next;
26   long opcode;
27   long mask;
28   struct simops *ops;
29 };
30
31 struct hash_entry hash_table[MAX_HASH+1];
32
33 static long 
34 hash(insn, format)
35      long insn;
36      int format;
37 {
38   if (format & LONG_OPCODE)
39     return ((insn & 0x3F000000) >> 24);
40   else
41     return((insn & 0x7E00) >> 9);
42 }
43
44 static struct hash_entry *
45 lookup_hash (ins, size)
46      uint32 ins;
47      int size;
48 {
49   struct hash_entry *h;
50
51   if (size)
52     h = &hash_table[(ins & 0x3F000000) >> 24];
53   else
54     h = &hash_table[(ins & 0x7E00) >> 9];
55
56   while ( (ins & h->mask) != h->opcode)
57     {
58       if (h->next == NULL)
59         {
60           (*d10v_callback->printf_filtered) (d10v_callback, "ERROR looking up hash for %x at PC %x\n",ins, PC);
61           exit(1);
62         }
63       h = h->next;
64     }
65   return (h);
66 }
67
68 uint32
69 get_longword (x)
70       uint8 *x;
71 {
72   uint8 *a = x;
73   return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + (a[3]);
74 }
75
76 int64
77 get_longlong (x)
78       uint8 *x;
79 {
80   uint8 *a = x;
81   return ((int64)a[0]<<56) + ((int64)a[1]<<48) + ((int64)a[2]<<40) + ((int64)a[3]<<32) +
82     ((int64)a[4]<< 24) + ((int64)a[5]<<16) + ((int64)a[6]<<8) + (int64)a[7];
83 }
84
85 uint16
86 get_word (x)
87       uint8 *x;
88 {
89   uint8 *a = x;
90   return ((uint16)a[0]<<8) + a[1];
91 }
92
93
94 void
95 write_word (addr, data)
96      uint8 *addr;
97      uint16 data;
98 {
99   uint8 *a = addr;
100   a[0] = data >> 8;
101   a[1] = data & 0xff;
102 }
103
104 void
105 write_longword (addr, data)
106      uint8 *addr;
107      uint32 data;
108 {
109   addr[0] = (data >> 24) & 0xff;
110   addr[1] = (data >> 16) & 0xff;
111   addr[2] = (data >> 8) & 0xff;
112   addr[3] = data & 0xff;
113 }
114
115 void
116 write_longlong (addr, data)
117      uint8 *addr;
118      int64 data;
119 {
120   uint8 *a = addr;
121   a[0] = data >> 56;
122   a[1] = (data >> 48) & 0xff;
123   a[2] = (data >> 40) & 0xff;
124   a[3] = (data >> 32) & 0xff;
125   a[4] = (data >> 24) & 0xff;
126   a[5] = (data >> 16) & 0xff;
127   a[6] = (data >> 8) & 0xff;
128   a[7] = data & 0xff;
129 }
130
131 static void
132 get_operands (struct simops *s, uint32 ins)
133 {
134   int i, shift, bits, flags;
135   uint32 mask;
136   for (i=0; i < s->numops; i++)
137     {
138       shift = s->operands[3*i];
139       bits = s->operands[3*i+1];
140       flags = s->operands[3*i+2];
141       mask = 0x7FFFFFFF >> (31 - bits);
142       OP[i] = (ins >> shift) & mask;
143     }
144 }
145
146 static void
147 do_long (ins)
148      uint32 ins;
149 {
150   struct hash_entry *h;
151 #ifdef DEBUG
152   if ((d10v_debug & DEBUG_INSTRUCTION) != 0)
153     (*d10v_callback->printf_filtered) (d10v_callback, "do_long 0x%x\n", ins);
154 #endif
155   h = lookup_hash (ins, 1);
156   get_operands (h->ops, ins);
157   State.ins_type = INS_LONG;
158   ins_type_counters[ (int)State.ins_type ]++;
159   (h->ops->func)();
160 }
161
162 static void
163 do_2_short (ins1, ins2, leftright)
164      uint16 ins1, ins2;
165      enum _leftright leftright;
166 {
167   struct hash_entry *h;
168   reg_t orig_pc = PC;
169
170 #ifdef DEBUG
171   if ((d10v_debug & DEBUG_INSTRUCTION) != 0)
172     (*d10v_callback->printf_filtered) (d10v_callback, "do_2_short 0x%x (%s) -> 0x%x\n",
173                                        ins1, (leftright) ? "left" : "right", ins2);
174 #endif
175   /*  printf ("do_2_short %x -> %x\n",ins1,ins2); */
176   h = lookup_hash (ins1, 0);
177   get_operands (h->ops, ins1);
178   State.ins_type = (leftright == LEFT_FIRST) ? INS_LEFT : INS_RIGHT;
179   ins_type_counters[ (int)State.ins_type ]++;
180   (h->ops->func)();
181
182   /* If the PC has changed (ie, a jump), don't do the second instruction */
183   if (orig_pc == PC)
184     {
185       h = lookup_hash (ins2, 0);
186       get_operands (h->ops, ins2);
187       State.ins_type = (leftright == LEFT_FIRST) ? INS_RIGHT : INS_LEFT;
188       ins_type_counters[ (int)State.ins_type ]++;
189       (h->ops->func)();
190     }
191 }
192
193 static void
194 do_parallel (ins1, ins2)
195      uint16 ins1, ins2;
196 {
197   struct hash_entry *h1, *h2;
198 #ifdef DEBUG
199   if ((d10v_debug & DEBUG_INSTRUCTION) != 0)
200     (*d10v_callback->printf_filtered) (d10v_callback, "do_parallel 0x%x || 0x%x\n", ins1, ins2);
201 #endif
202   h1 = lookup_hash (ins1, 0);
203   h2 = lookup_hash (ins2, 0);
204
205   if (h1->ops->exec_type == PARONLY)
206     {
207       get_operands (h1->ops, ins1);
208       State.ins_type = INS_LEFT;
209       ins_type_counters[ (int)State.ins_type ]++;
210       (h1->ops->func)();
211       if (State.exe)
212         {
213           get_operands (h2->ops, ins2);
214           State.ins_type = INS_RIGHT;
215           (h2->ops->func)();
216         }
217     }
218   else if (h2->ops->exec_type == PARONLY)
219     {
220       get_operands (h2->ops, ins2);
221       State.ins_type = INS_RIGHT;
222       ins_type_counters[ (int)State.ins_type ]++;
223       (h2->ops->func)();
224       if (State.exe)
225         {
226           get_operands (h1->ops, ins1);
227           State.ins_type = INS_LEFT;
228           (h1->ops->func)();
229         }
230     }
231   else
232     {
233       get_operands (h1->ops, ins1);
234       State.ins_type = INS_LEFT_PARALLEL;
235       ins_type_counters[ (int)State.ins_type ]++;
236       (h1->ops->func)();
237       get_operands (h2->ops, ins2);
238       State.ins_type = INS_RIGHT_PARALLEL;
239       ins_type_counters[ (int)State.ins_type ]++;
240       (h2->ops->func)();
241     }
242 }
243  
244
245 void
246 sim_size (power)
247      int power;
248
249 {
250   if (State.imem)
251     {
252       free (State.imem);
253       free (State.dmem);
254     }
255
256   State.imem = (uint8 *)calloc(1,1<<IMEM_SIZE);
257   State.dmem = (uint8 *)calloc(1,1<<DMEM_SIZE);
258   if (!State.imem || !State.dmem )
259     {
260       (*d10v_callback->printf_filtered) (d10v_callback, "Memory allocation failed.\n");
261       exit(1);
262     }
263
264 #ifdef DEBUG
265   if ((d10v_debug & DEBUG_MEMSIZE) != 0)
266     {
267       (*d10v_callback->printf_filtered) (d10v_callback, "Allocated %d bytes instruction memory and\n",1<<IMEM_SIZE);
268       (*d10v_callback->printf_filtered) (d10v_callback, "          %d bytes data memory.\n",          1<<DMEM_SIZE);
269     }
270 #endif
271 }
272
273 static void
274 init_system ()
275 {
276   if (!State.imem)
277     sim_size(1);
278 }
279
280 int
281 sim_write (addr, buffer, size)
282      SIM_ADDR addr;
283      unsigned char *buffer;
284      int size;
285 {
286   int i;
287   init_system ();
288
289   /* (*d10v_callback->printf_filtered) (d10v_callback, "sim_write %d bytes to 0x%x\n",size,addr); */
290   for (i = 0; i < size; i++)
291     {
292       State.imem[i+addr] = buffer[i]; 
293     }
294   return size;
295 }
296
297 void
298 sim_open (args)
299      char *args;
300 {
301   struct simops *s;
302   struct hash_entry *h, *prev;
303   static int init_p = 0;
304
305   if (args != NULL)
306     {
307 #ifdef DEBUG
308       if (strcmp (args, "-t") == 0)
309         d10v_debug = DEBUG;
310       else
311 #endif
312         (*d10v_callback->printf_filtered) (d10v_callback, "ERROR: unsupported option(s): %s\n",args);
313     }
314
315   /* put all the opcodes in the hash table */
316   if (!init_p++)
317     {
318       for (s = Simops; s->func; s++)
319         {
320           h = &hash_table[hash(s->opcode,s->format)];
321       
322           /* go to the last entry in the chain */
323           while (h->next)
324             h = h->next;
325
326           if (h->ops)
327             {
328               h->next = calloc(1,sizeof(struct hash_entry));
329               h = h->next;
330             }
331           h->ops = s;
332           h->mask = s->mask;
333           h->opcode = s->opcode;
334         }
335     }
336 }
337
338
339 void
340 sim_close (quitting)
341      int quitting;
342 {
343   /* nothing to do */
344 }
345
346 void
347 sim_set_profile (n)
348      int n;
349 {
350   (*d10v_callback->printf_filtered) (d10v_callback, "sim_set_profile %d\n",n);
351 }
352
353 void
354 sim_set_profile_size (n)
355      int n;
356 {
357   (*d10v_callback->printf_filtered) (d10v_callback, "sim_set_profile_size %d\n",n);
358 }
359
360 void
361 sim_resume (step, siggnal)
362      int step, siggnal;
363 {
364   uint32 inst;
365   int i;
366   reg_t oldpc;
367
368 /*   (*d10v_callback->printf_filtered) (d10v_callback, "sim_resume (%d,%d)  PC=0x%x\n",step,siggnal,PC); */
369
370  if (step)
371    State.exception = SIGTRAP;
372  else
373    State.exception = 0;
374  
375  do
376    {
377      inst = RLW (PC << 2); 
378      oldpc = PC;
379      switch (inst & 0xC0000000)
380        {
381        case 0xC0000000:
382          /* long instruction */
383          do_long (inst & 0x3FFFFFFF);
384          break;
385        case 0x80000000:
386          /* R -> L */
387          do_2_short ( inst & 0x7FFF, (inst & 0x3FFF8000) >> 15, 0);
388          break;
389        case 0x40000000:
390          /* L -> R */
391          do_2_short ((inst & 0x3FFF8000) >> 15, inst & 0x7FFF, 1);
392          break;
393        case 0:
394          do_parallel ((inst & 0x3FFF8000) >> 15, inst & 0x7FFF);
395          break;
396        }
397      
398      if (State.RP && PC == RPT_E)
399        {
400          RPT_C -= 1;
401          if (RPT_C == 0)
402            State.RP = 0;
403          else
404            PC = RPT_S;
405        }
406      
407      /* FIXME */
408      if (PC == oldpc)
409        PC++;
410    } 
411  while (!State.exception);
412 }
413
414 int
415 sim_trace ()
416 {
417 #ifdef DEBUG
418   d10v_debug = DEBUG;
419 #endif
420   sim_resume (0, 0);
421   return 1;
422 }
423
424 void
425 sim_info (verbose)
426      int verbose;
427 {
428   char buf[40];
429   int size;
430   long total = (ins_type_counters[ (int)INS_LONG ]
431                 + ins_type_counters[ (int)INS_LEFT ]
432                 + ins_type_counters[ (int)INS_LEFT_PARALLEL ]
433                 + ins_type_counters[ (int)INS_RIGHT ]
434                 + ins_type_counters[ (int)INS_RIGHT_PARALLEL ]);
435
436   sprintf (buf, "%ld", total);
437   size = strlen (buf);
438
439   (*d10v_callback->printf_filtered) (d10v_callback,
440                                      "executed %*ld instructions in the left  container, %*ld parallel, %*ld nops\n",
441                                      size, ins_type_counters[ (int)INS_LEFT ] + ins_type_counters[ (int)INS_LEFT_PARALLEL ],
442                                      size, ins_type_counters[ (int)INS_LEFT_PARALLEL ],
443                                      size, left_nops);
444
445   (*d10v_callback->printf_filtered) (d10v_callback,
446                                      "executed %*ld instructions in the right container, %*ld parallel, %*ld nops\n",
447                                      size, ins_type_counters[ (int)INS_RIGHT ] + ins_type_counters[ (int)INS_RIGHT_PARALLEL ],
448                                      size, ins_type_counters[ (int)INS_RIGHT_PARALLEL ],
449                                      size, right_nops);
450
451   (*d10v_callback->printf_filtered) (d10v_callback,
452                                      "executed %*ld long instructions\n",
453                                      size, ins_type_counters[ (int)INS_LONG ]);
454
455   (*d10v_callback->printf_filtered) (d10v_callback,
456                                      "executed %*ld total instructions\n",
457                                      size, total);
458 }
459
460 void
461 sim_create_inferior (start_address, argv, env)
462      SIM_ADDR start_address;
463      char **argv;
464      char **env;
465 {
466 #ifdef DEBUG
467   if (d10v_debug)
468     (*d10v_callback->printf_filtered) (d10v_callback, "sim_create_inferior:  PC=0x%x\n", start_address);
469 #endif
470   PC = start_address >> 2;
471 }
472
473
474 void
475 sim_kill ()
476 {
477   /* nothing to do */
478 }
479
480 void
481 sim_set_callbacks(p)
482      host_callback *p;
483 {
484 /*  printf ("sim_set_callbacks\n"); */
485   d10v_callback = p;
486 }
487
488 void
489 sim_stop_reason (reason, sigrc)
490      enum sim_stop *reason;
491      int *sigrc;
492 {
493 /*   (*d10v_callback->printf_filtered) (d10v_callback, "sim_stop_reason:  PC=0x%x\n",PC<<2); */
494
495   switch (State.exception)
496     {
497     case SIG_D10V_STOP:                 /* stop instruction */
498       *reason = sim_exited;
499       *sigrc = 0;
500       break;
501
502     case SIG_D10V_EXIT:                 /* exit trap */
503       *reason = sim_exited;
504       *sigrc = State.regs[2];
505       break;
506
507     default:                            /* some signal */
508       *reason = sim_stopped;
509       *sigrc = State.exception;
510       break;
511     } 
512 }
513
514 void
515 sim_fetch_register (rn, memory)
516      int rn;
517      unsigned char *memory;
518 {
519   if (rn > 31)
520     {
521       WRITE_64 (memory, State.a[rn-32]);
522       /* (*d10v_callback->printf_filtered) (d10v_callback, "sim_fetch_register %d 0x%llx\n",rn,State.a[rn-32]); */
523     }
524   else
525     {
526       WRITE_16 (memory, State.regs[rn]);
527       /* (*d10v_callback->printf_filtered) (d10v_callback, "sim_fetch_register %d 0x%x\n",rn,State.regs[rn]); */
528     }
529 }
530  
531 void
532 sim_store_register (rn, memory)
533      int rn;
534      unsigned char *memory;
535 {
536   if (rn > 31)
537     {
538       State.a[rn-32] =  READ_64 (memory) & MASK40;
539       /* (*d10v_callback->printf_filtered) (d10v_callback, "store: a%d=0x%llx\n",rn-32,State.a[rn-32]); */
540     }
541   else
542     {
543       State.regs[rn]= READ_16 (memory);
544       /* (*d10v_callback->printf_filtered) (d10v_callback, "store: r%d=0x%x\n",rn,State.regs[rn]); */
545     }
546 }
547
548 sim_read (addr, buffer, size)
549      SIM_ADDR addr;
550      unsigned char *buffer;
551      int size;
552 {
553   int i;
554   for (i = 0; i < size; i++)
555     {
556       buffer[i] = State.imem[addr + i];
557     }
558   return size;
559
560
561 void
562 sim_do_command (cmd)
563      char *cmd;
564
565   (*d10v_callback->printf_filtered) (d10v_callback, "sim_do_command: %s\n",cmd);
566 }
567
568 int
569 sim_load (prog, from_tty)
570      char *prog;
571      int from_tty;
572 {
573   /* Return nonzero so GDB will handle it.  */
574   return 1;
575