Incorrect test for zero-r0 code gen.
[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   default: name = "unknown"; break;
102   case invalid_illegal: name = "illegal"; break;
103   case invalid_fp_unavailable: name = "fp_unavailable"; break;
104   case invalid_wrong_slot: name = "wrong_slot"; break;
105   }
106   if ((code & generate_jumps))
107     lf_printf(file, "goto %s_%s;\n",
108               (code & generate_with_icache) ? "icache" : "semantic",
109               name);
110   else if ((code & generate_with_icache)) {
111     lf_printf(file, "%s %sicache_%s(", result, global_name_prefix, name);
112     print_icache_function_actual(file);
113     lf_printf(file, ");\n");
114   }
115   else {
116     lf_printf(file, "%s %ssemantic_%s(", result, global_name_prefix, name);
117     print_semantic_function_actual(file);
118     lf_printf(file, ");\n");
119   }
120 }
121
122
123 void
124 print_semantic_body(lf *file,
125                     insn *instruction,
126                     insn_bits *expanded_bits,
127                     opcode_field *opcodes)
128 {
129   print_itrace(file, instruction->file_entry, 0/*put_value_in_cache*/);
130
131   /* validate the instruction, if a cache this has already been done */
132   if (!(code & generate_with_icache))
133     print_idecode_validate(file, instruction, opcodes);
134
135   /* generate the profiling call - this is delayed until after the
136      instruction has been verified */
137   lf_printf(file, "\n");
138   lf_indent_suppress(file);
139   lf_printf(file, "#if defined(WITH_MON)\n");
140   lf_printf(file, "/* monitoring: */\n");
141   lf_printf(file, "if (WITH_MON & MONITOR_INSTRUCTION_ISSUE) {\n");
142   lf_printf(file, "  mon_issue(");
143   print_function_name(file,
144                       instruction->file_entry->fields[insn_name],
145                       NULL,
146                       function_name_prefix_itable);
147   lf_printf(file, ", cpu, cia);\n");
148   lf_printf(file, "}\n");
149   lf_indent_suppress(file);
150   lf_printf(file, "#endif\n");
151   lf_printf(file, "\n");
152
153   /* determine the new instruction address */
154   lf_printf(file, "/* keep the next instruction address handy */\n");
155   if ((code & generate_with_semantic_returning_modified_nia_only))
156     lf_printf(file, "nia = -1;\n");
157   else if ((code & generate_with_semantic_delayed_branch)) {
158     lf_printf(file, "nia.ip = cia.dp; /* instruction pointer */\n");
159     lf_printf(file, "nia.dp = cia.dp + %d; /* delayed-slot pointer */\n",
160               insn_bit_size / 8);
161   }
162   else
163     lf_printf(file, "nia = cia + %d;\n", insn_bit_size / 8);
164
165   /* if conditional, generate code to verify that the instruction
166      should be issued */
167   if (it_is("c", instruction->file_entry->fields[insn_options])
168       || (code & generate_with_semantic_conditional_issue)) {
169     lf_printf(file, "\n");
170     lf_printf(file, "/* execute only if conditional passes */\n");
171     lf_printf(file, "if (IS_CONDITION_OK) {\n");
172     lf_indent(file, +2);
173     /* FIXME - need to log a conditional failure */
174   }
175   
176   /* Architecture expects r0 to be zero.  Instead of having to check
177      every read to see if it is refering to r0 just zap the r0
178      register */
179   if ((code & generate_with_zero_r0))
180     {
181       lf_printf (file, "\n");
182       lf_printf (file, "GPR(0) = 0;\n");
183     }
184
185   /* generate the code (or at least something */
186   lf_printf(file, "\n");
187   lf_printf(file, "/* semantics: */\n");
188   if (instruction->file_entry->annex != NULL) {
189     /* true code */
190     table_entry_print_cpp_line_nr(file, instruction->file_entry);
191     lf_printf(file, "{\n");
192     lf_indent(file, +2);
193     lf_print__c_code(file, instruction->file_entry->annex);
194     lf_indent(file, -2);
195     lf_printf(file, "}\n");
196     lf_print__internal_reference(file);
197   }
198   else if (it_is("nop", instruction->file_entry->fields[insn_flags])) {
199     lf_print__internal_reference(file);
200   }
201   else {
202     /* abort so it is implemented now */
203     table_entry_print_cpp_line_nr(file, instruction->file_entry);
204     lf_printf(file, "sim_engine_abort (SD, CPU, cia, \"%s:%d:0x%%08lx:%%s unimplemented\\n\",\n",
205               filter_filename(instruction->file_entry->file_name),
206               instruction->file_entry->line_nr);
207     if ((code & generate_with_semantic_delayed_branch))
208       lf_printf(file, "              (long)cia.ip,\n");
209     else
210       lf_printf(file, "              (long)cia,\n");
211     lf_printf(file, "              itable[MY_INDEX].name);\n");
212     lf_print__internal_reference(file);
213   }
214
215   /* Close off the conditional execution */
216   if (it_is("c", instruction->file_entry->fields[insn_options])
217       || (code & generate_with_semantic_conditional_issue)) {
218     lf_indent(file, -2);
219     lf_printf(file, "}\n");
220   }  
221 }
222
223 static void
224 print_c_semantic(lf *file,
225                  insn *instruction,
226                  insn_bits *expanded_bits,
227                  opcode_field *opcodes,
228                  cache_table *cache_rules)
229 {
230
231   lf_printf(file, "{\n");
232   lf_indent(file, +2);
233
234   print_my_defines(file, expanded_bits, instruction->file_entry);
235   lf_printf(file, "\n");
236   print_icache_body(file,
237                     instruction,
238                     expanded_bits,
239                     cache_rules,
240                     ((code & generate_with_direct_access)
241                      ? define_variables
242                      : declare_variables),
243                     ((code & generate_with_icache)
244                      ? get_values_from_icache
245                      : do_not_use_icache));
246
247   lf_printf(file, "%sinstruction_address nia;\n", global_name_prefix);
248   print_semantic_body(file,
249                       instruction,
250                       expanded_bits,
251                       opcodes);
252   lf_printf(file, "return nia;\n");
253
254   /* generate something to clean up any #defines created for the cache */
255   if (code & generate_with_direct_access)
256     print_icache_body(file,
257                       instruction,
258                       expanded_bits,
259                       cache_rules,
260                       undef_variables,
261                       ((code & generate_with_icache)
262                        ? get_values_from_icache
263                        : do_not_use_icache));
264
265   lf_indent(file, -2);
266   lf_printf(file, "}\n");
267 }
268
269 static void
270 print_c_semantic_function(lf *file,
271                           insn *instruction,
272                           insn_bits *expanded_bits,
273                           opcode_field *opcodes,
274                           cache_table *cache_rules)
275 {
276   /* build the semantic routine to execute the instruction */
277   print_semantic_function_header(file,
278                                  instruction->file_entry->fields[insn_name],
279                                  expanded_bits,
280                                  1/*is-function-definition*/);
281   print_c_semantic(file,
282                    instruction,
283                    expanded_bits,
284                    opcodes,
285                    cache_rules);
286 }
287
288 void
289 print_semantic_definition(insn_table *entry,
290                           lf *file,
291                           void *data,
292                           insn *instruction,
293                           int depth)
294 {
295   cache_table *cache_rules = (cache_table*)data;
296   if (generate_expanded_instructions) {
297     ASSERT(entry->nr_insn == 1
298            && entry->opcode == NULL
299            && entry->parent != NULL
300            && entry->parent->opcode != NULL);
301     ASSERT(entry->nr_insn == 1
302            && entry->opcode == NULL
303            && entry->parent != NULL
304            && entry->parent->opcode != NULL
305            && entry->parent->opcode_rule != NULL);
306     print_c_semantic_function(file,
307                               entry->insns,
308                               entry->expanded_bits,
309                               entry->parent->opcode,
310                               cache_rules);
311   }
312   else {
313     print_c_semantic_function(file, instruction,
314                               NULL, NULL,
315                               cache_rules);
316   }
317 }