OSDN Git Service

* public snapshot of sid simulator
[pf3gnuchains/pf3gnuchains3x.git] / sid / component / gloss / m32r.cxx
1 // m32r.cxx - Implementation of the M32R libgloss specifics.  -*- C++ -*-
2
3 // Copyright (C) 1999, 2000 Red Hat.
4 // This file is part of SID and is licensed under the GPL.
5 // See the file COPYING.SID for conditions for redistribution.
6
7 #include "config.h"
8 #include "m32r.h"
9 #include "libgloss.h"
10 #include "newlib.h"
11
12 using namespace std;
13 using namespace sid;
14 using namespace sidutil;
15
16 m32r_libgloss::m32r_libgloss()
17 {
18   register_attribute_names[-1] = "r0"; // result
19   register_attribute_names[-2] = "r0"; // error
20   register_attribute_names[0] = "r0";  // function code
21   register_attribute_names[1] = "r1";  // arg 1
22   register_attribute_names[2] = "r2";  // arg 2
23   register_attribute_names[3] = "r3";  // arg 3
24 }
25
26 bool
27 m32r_libgloss::get_int_argument(unsigned index, int32& value)
28 {
29   assert (cpu);
30   string attrName = register_attribute_names[index];
31   assert (attrName != "");
32
33   string attrValue = cpu->attribute_value (attrName);
34   if (attrValue == "")
35     {
36       cerr << "Could not read attribute " << attrName
37            << " for ABI argument #" << index << endl;
38       return false;
39     }
40
41   host_int_4 value_number;
42   parse_attribute(attrValue, value_number);
43   value = value_number;
44   return true;
45 }
46
47 bool
48 m32r_libgloss::set_int_result(int32 value) 
49 {
50   assert (cpu);
51   string attrName = register_attribute_names[-1];
52   assert (attrName != "");
53
54   host_int_4 value_number = value;
55   string attrValue = make_attribute(value_number);
56
57   cpu->set_attribute_value (attrName, attrValue);
58   return true; // XXX: check?
59 }
60
61 bool
62 m32r_libgloss::set_error_result(int32 value) 
63 {
64   assert (cpu);
65   string attrName = register_attribute_names[-2];
66   assert (attrName != "");
67
68   host_int_4 value_number = value;
69   string attrValue = make_attribute(value_number);
70
71   cpu->set_attribute_value (attrName, attrValue);
72   return true; // XXX: safe to assume success?
73 }
74
75 bool
76 m32r_libgloss::syscall_trap_p()
77 {
78   // M32R system calls use TRAP 0.
79   return ((trap_type_ipin.sense() == sidutil::cpu_trap_software)
80           && (trap_code_pin.sense() == 0)); /* trap 0 */
81 }
82
83 void
84 m32r_libgloss::syscall_trap()
85 {
86   int32 syscall;
87
88   get_int_argument(0, syscall);
89   if (verbose_p)
90     {
91       cerr << "M32R/libgloss system call number " << syscall << endl;
92     }
93
94   switch (syscall)
95     {
96     case libgloss::SYS_exit:
97       {
98         int32 value;
99         get_int_argument(1, value);
100         if (verbose_p)
101           cerr << "*** exit(" << value << ")" << endl;
102
103         if (value == 0)
104           process_signal_pin.drive(newlib::sigQuit);
105         else
106           process_signal_pin.drive(newlib::sigAbrt);
107       }
108       break;
109
110     case libgloss::SYS_write:
111       {
112         int32 fd;
113         int32 address;
114         int32 length;
115         string str;
116
117         get_int_argument(1, fd);
118         get_int_argument(2, address);
119         get_int_argument(3, length);
120
121         size32 len_written;
122         int errcode;
123         if (! this->write (fd, address, length, len_written, errcode))
124           {
125             set_error_result(host_to_target_errno(errcode));
126             set_int_result(-1);
127             return;
128           }
129
130         set_int_result(len_written);
131       }
132       break;
133
134     case libgloss::SYS_read:
135       {
136         int32 fd;
137         int32 address;
138         int32 length;
139         string str;
140
141         get_int_argument(1, fd);
142         get_int_argument(2, address);
143         get_int_argument(3, length);
144
145         size32 len_read;
146         int errcode;
147         if (! this->read (fd, address, length, len_read, errcode))
148           {
149             set_error_result(host_to_target_errno(errcode));
150             set_int_result(-1);
151             return;
152           }
153
154         set_int_result(len_read);
155       }
156       break;
157
158     default:
159       if (verbose_p)
160         {
161           cerr << "Unimplemented syscall " << syscall << endl;
162         }
163       set_int_result(-1);
164       set_error_result(newlib::eNoSys);
165     }
166 }
167
168 // Convert host errno value to target.
169
170 int
171 m32r_libgloss::host_to_target_errno (int host_errno)
172 {
173   return newlib::host_to_target_errno (host_errno);
174 }