Tidy code gen.
[binutils-gdb.git] / sim / igen / gen-support.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 #include "misc.h"
22 #include "lf.h"
23 #include "table.h"
24 #include "filter.h"
25
26 #include "ld-decode.h"
27 #include "ld-cache.h"
28 #include "ld-insn.h"
29
30 #include "igen.h"
31
32 #include "gen-semantics.h"
33 #include "gen-support.h"
34
35 static void
36 print_support_function_name(lf *file,
37 table_entry *function,
38 int is_function_definition)
39 {
40 if (it_is("internal", function->fields[insn_flags])) {
41 lf_print_function_type_function(file, print_semantic_function_type, "INLINE_SUPPORT",
42 (is_function_definition ? "\n" : " "));
43 print_function_name(file,
44 function->fields[function_name],
45 NULL,
46 function_name_prefix_semantics);
47 lf_printf(file, "\n(");
48 print_semantic_function_formal(file);
49 lf_printf(file, ")");
50 if (!is_function_definition)
51 lf_printf(file, ";");
52 lf_printf(file, "\n");
53 }
54 else {
55 /* map the name onto a globally valid name */
56 if (!is_function_definition && strcmp(global_name_prefix, "") != 0) {
57 lf_indent_suppress(file);
58 lf_printf(file, "#define %s %s%s\n",
59 function->fields[function_name],
60 global_name_prefix,
61 function->fields[function_name]);
62 }
63 lf_print_function_type(file,
64 function->fields[function_type],
65 "INLINE_SUPPORT",
66 (is_function_definition ? "\n" : " "));
67 lf_printf(file, "%s%s\n(",
68 global_name_prefix,
69 function->fields[function_name]);
70 if (generate_smp)
71 lf_printf(file, "sim_cpu *cpu");
72 else
73 lf_printf(file, "SIM_DESC sd");
74 if (strcmp(function->fields[function_param], "") != 0)
75 lf_printf(file, ", %s", function->fields[function_param]);
76 lf_printf(file, ")%s", (is_function_definition ? "\n" : ";\n"));
77 }
78 }
79
80
81 static void
82 support_h_function(insn_table *entry,
83 lf *file,
84 void *data,
85 table_entry *function)
86 {
87 ASSERT(function->fields[function_type] != NULL);
88 ASSERT(function->fields[function_param] != NULL);
89 print_support_function_name(file,
90 function,
91 0/*!is_definition*/);
92 lf_printf(file, "\n");
93 }
94
95
96 extern void
97 gen_support_h(insn_table *table,
98 lf *file)
99 {
100 /* output the definition of `_SD'*/
101 if (generate_smp) {
102 lf_printf(file, "#define _SD cpu\n");
103 lf_printf(file, "#define SD cpu->sd\n");
104 lf_printf(file, "#define CPU cpu\n");
105 }
106 else {
107 lf_printf(file, "#define _SD sd\n");
108 lf_printf(file, "#define SD sd\n");
109 lf_printf(file, "#define CPU (&sd->cpu)\n");
110 }
111 lf_printf(file, "\n");
112 /* output a declaration for all functions */
113 insn_table_traverse_function(table,
114 file, NULL,
115 support_h_function);
116 lf_printf(file, "\n");
117 lf_printf(file, "#if defined(SUPPORT_INLINE)\n");
118 lf_printf(file, "# if ((SUPPORT_INLINE & INCLUDE_MODULE)\\\n");
119 lf_printf(file, " && (SUPPORT_INLINE & INCLUDED_BY_MODULE))\n");
120 lf_printf(file, "# include \"%ssupport.c\"\n", global_name_prefix);
121 lf_printf(file, "# endif\n");
122 lf_printf(file, "#endif\n");
123 }
124
125 static void
126 support_c_function(insn_table *table,
127 lf *file,
128 void *data,
129 table_entry *function)
130 {
131 ASSERT (function->fields[function_type] != NULL);
132 print_support_function_name (file,
133 function,
134 1/*!is_definition*/);
135 table_entry_print_cpp_line_nr (file, function);
136 lf_printf (file, "{\n");
137 lf_indent (file, +2);
138 if (function->annex == NULL)
139 error ("%s:%d: Function without body (or null statement)",
140 function->file_name,
141 function->line_nr);
142 lf_print__c_code (file, function->annex);
143 if (it_is ("internal", function->fields[insn_flags]))
144 {
145 lf_printf (file, "sim_io_error (sd, \"Internal function must longjump\\n\");\n");
146 lf_printf (file, "return cia;\n");
147 }
148 lf_indent (file, -2);
149 lf_printf (file, "}\n");
150 lf_print__internal_reference (file);
151 lf_printf (file, "\n");
152 }
153
154
155 void
156 gen_support_c(insn_table *table,
157 lf *file)
158 {
159 lf_printf(file, "#include \"sim-main.h\"\n");
160 lf_printf(file, "#include \"%sidecode.h\"\n", global_name_prefix);
161 lf_printf(file, "#include \"%ssupport.h\"\n", global_name_prefix);
162 lf_printf(file, "\n");
163
164 /* output a definition (c-code) for all functions */
165 insn_table_traverse_function(table,
166 file, NULL,
167 support_c_function);
168 }