57363c965142f0520c203a3caa8280bf5af79e6e
[platform/upstream/binutils.git] / sim / igen / gen-semantics.c
1 /*  This file is part of the program psim.
2
3     Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14  
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  
19     */
20
21
22
23 #include "misc.h"
24 #include "lf.h"
25 #include "table.h"
26 #include "filter.h"
27
28 #include "ld-decode.h"
29 #include "ld-cache.h"
30 #include "ld-insn.h"
31
32 #include "igen.h"
33
34 #include "gen-semantics.h"
35 #include "gen-icache.h"
36 #include "gen-idecode.h"
37
38
39 static void
40 print_semantic_function_header(lf *file,
41                                const char *basename,
42                                insn_bits *expanded_bits,
43                                int is_function_definition)
44 {
45   int indent;
46   lf_printf(file, "\n");
47   lf_print_function_type_function(file, print_semantic_function_type, "EXTERN_SEMANTICS",
48                                   (is_function_definition ? "\n" : " "));
49   indent = print_function_name(file,
50                                basename,
51                                expanded_bits,
52                                function_name_prefix_semantics);
53   if (is_function_definition)
54     lf_indent(file, +indent);
55   else
56     lf_printf(file, "\n");
57   lf_printf(file, "(");
58   print_semantic_function_formal(file);
59   lf_printf(file, ")");
60   if (is_function_definition)
61     lf_indent(file, -indent);
62   else
63     lf_printf(file, ";");
64   lf_printf(file, "\n");
65 }
66
67 void
68 print_semantic_declaration(insn_table *entry,
69                            lf *file,
70                            void *data,
71                            insn *instruction,
72                            int depth)
73 {
74   if (generate_expanded_instructions) {
75     ASSERT(entry->nr_insn == 1);
76     print_semantic_function_header(file,
77                                    instruction->file_entry->fields[insn_name],
78                                    entry->expanded_bits,
79                                    0/* is not function definition*/);
80   }
81   else {
82     print_semantic_function_header(file,
83                                    instruction->file_entry->fields[insn_name],
84                                    NULL,
85                                    0/* is not function definition*/);
86   }
87 }
88
89
90 \f
91 /* generate the semantics.c file */
92
93
94 void
95 print_idecode_invalid(lf *file,
96                       const char *result,
97                       invalid_type type)
98 {
99   const char *name;
100   switch (type) {
101   case invalid_illegal: name = "illegal"; break;
102   case invalid_fp_unavailable: name = "fp_unavailable"; break;
103   case invalid_wrong_slot: name = "wrong_slot"; break;
104   }
105   if ((code & generate_jumps))
106     lf_printf(file, "goto %s_%s;\n",
107               (code & generate_with_icache) ? "icache" : "semantic",
108               name);
109   else if ((code & generate_with_icache)) {
110     lf_printf(file, "%s %sicache_%s(", result, global_name_prefix, name);
111     print_icache_function_actual(file);
112     lf_printf(file, ");\n");
113   }
114   else {
115     lf_printf(file, "%s %ssemantic_%s(", result, global_name_prefix, name);
116     print_semantic_function_actual(file);
117     lf_printf(file, ");\n");
118   }
119 }
120
121
122 void
123 print_semantic_body(lf *file,
124                     insn *instruction,
125                     insn_bits *expanded_bits,
126                     opcode_field *opcodes)
127 {
128   print_itrace(file, instruction->file_entry, 0/*put_value_in_cache*/);
129
130   /* validate the instruction, if a cache this has already been done */
131   if (!(code & generate_with_icache))
132     print_idecode_validate(file, instruction, opcodes);
133
134   /* generate the profiling call - this is delayed until after the
135      instruction has been verified */
136   lf_printf(file, "\n");
137   lf_indent_suppress(file);
138   lf_printf(file, "#if defined(WITH_MON)\n");
139   lf_printf(file, "/* monitoring: */\n");
140   lf_printf(file, "if (WITH_MON & MONITOR_INSTRUCTION_ISSUE) {\n");
141   lf_printf(file, "  mon_issue(");
142   print_function_name(file,
143                       instruction->file_entry->fields[insn_name],
144                       NULL,
145                       function_name_prefix_itable);
146   lf_printf(file, ", cpu, cia);\n");
147   lf_printf(file, "}\n");
148   lf_indent_suppress(file);
149   lf_printf(file, "#endif\n");
150   lf_printf(file, "\n");
151
152   /* determine the new instruction address */
153   lf_printf(file, "/* keep the next instruction address handy */\n");
154   if ((code & generate_with_semantic_returning_modified_nia_only))
155     lf_printf(file, "nia = -1;\n");
156   else if ((code & generate_with_semantic_delayed_branch)) {
157     lf_printf(file, "nia.ip = cia.dp; /* instruction pointer */\n");
158     lf_printf(file, "nia.dp = cia.dp + %d; /* delayed-slot pointer\n",
159               insn_bit_size / 8);
160   }
161   else
162     lf_printf(file, "nia = cia + %d;\n", insn_bit_size / 8);
163
164   /* if conditional, generate code to verify that the instruction
165      should be issued */
166   if (it_is("c", instruction->file_entry->fields[insn_options])
167       || (code & generate_with_semantic_conditional_issue)) {
168     lf_printf(file, "\n");
169     lf_printf(file, "/* execute only if conditional passes */\n");
170     lf_printf(file, "if (IS_CONDITION_OK) {\n");
171     lf_indent(file, +2);
172     /* FIXME - need to log a conditional failure */
173   }
174   
175   /* generate the code (or at least something */
176   lf_printf(file, "\n");
177   lf_printf(file, "/* semantics: */\n");
178   if (instruction->file_entry->annex != NULL) {
179     /* true code */
180     table_entry_print_cpp_line_nr(file, instruction->file_entry);
181     lf_printf(file, "{\n");
182     lf_indent(file, +2);
183     lf_print__c_code(file, instruction->file_entry->annex);
184     lf_indent(file, -2);
185     lf_printf(file, "}\n");
186     lf_print__internal_reference(file);
187   }
188   else if (it_is("nop", instruction->file_entry->fields[insn_flags])) {
189     lf_print__internal_reference(file);
190   }
191   else {
192     /* abort so it is implemented now */
193     table_entry_print_cpp_line_nr(file, instruction->file_entry);
194     lf_printf(file, "engine_error(system, \"%s:%d:0x%%08lx:%%s unimplemented\\n\",\n",
195               filter_filename(instruction->file_entry->file_name),
196               instruction->file_entry->line_nr);
197     lf_printf(file, "             (long)cia, itable[MY_INDEX].name);\n");
198     lf_print__internal_reference(file);
199   }
200
201   /* Close off the conditional execution */
202   if (it_is("c", instruction->file_entry->fields[insn_options])
203       || (code & generate_with_semantic_conditional_issue)) {
204     lf_indent(file, -2);
205     lf_printf(file, "}\n");
206   }  
207 }
208
209 static void
210 print_c_semantic(lf *file,
211                  insn *instruction,
212                  insn_bits *expanded_bits,
213                  opcode_field *opcodes,
214                  cache_table *cache_rules)
215 {
216
217   lf_printf(file, "{\n");
218   lf_indent(file, +2);
219
220   print_my_defines(file, expanded_bits, instruction->file_entry);
221   lf_printf(file, "\n");
222   print_icache_body(file,
223                     instruction,
224                     expanded_bits,
225                     cache_rules,
226                     ((code & generate_with_direct_access)
227                      ? define_variables
228                      : declare_variables),
229                     ((code & generate_with_icache)
230                      ? get_values_from_icache
231                      : do_not_use_icache));
232
233   lf_printf(file, "instruction_address nia;\n");
234   print_semantic_body(file,
235                       instruction,
236                       expanded_bits,
237                       opcodes);
238   lf_printf(file, "return nia;\n");
239
240   /* generate something to clean up any #defines created for the cache */
241   if (code & generate_with_direct_access)
242     print_icache_body(file,
243                       instruction,
244                       expanded_bits,
245                       cache_rules,
246                       undef_variables,
247                       ((code & generate_with_icache)
248                        ? get_values_from_icache
249                        : do_not_use_icache));
250
251   lf_indent(file, -2);
252   lf_printf(file, "}\n");
253 }
254
255 static void
256 print_c_semantic_function(lf *file,
257                           insn *instruction,
258                           insn_bits *expanded_bits,
259                           opcode_field *opcodes,
260                           cache_table *cache_rules)
261 {
262   /* build the semantic routine to execute the instruction */
263   print_semantic_function_header(file,
264                                  instruction->file_entry->fields[insn_name],
265                                  expanded_bits,
266                                  1/*is-function-definition*/);
267   print_c_semantic(file,
268                    instruction,
269                    expanded_bits,
270                    opcodes,
271                    cache_rules);
272 }
273
274 void
275 print_semantic_definition(insn_table *entry,
276                           lf *file,
277                           void *data,
278                           insn *instruction,
279                           int depth)
280 {
281   cache_table *cache_rules = (cache_table*)data;
282   if (generate_expanded_instructions) {
283     ASSERT(entry->nr_insn == 1
284            && entry->opcode == NULL
285            && entry->parent != NULL
286            && entry->parent->opcode != NULL);
287     ASSERT(entry->nr_insn == 1
288            && entry->opcode == NULL
289            && entry->parent != NULL
290            && entry->parent->opcode != NULL
291            && entry->parent->opcode_rule != NULL);
292     print_c_semantic_function(file,
293                               entry->insns,
294                               entry->expanded_bits,
295                               entry->parent->opcode,
296                               cache_rules);
297   }
298   else {
299     print_c_semantic_function(file, instruction,
300                               NULL, NULL,
301                               cache_rules);
302   }
303 }