1 /* Built-in and inline functions for gcj
3 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 /* Written by Tom Tromey <tromey@redhat.com>. */
34 #include "java-tree.h"
38 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
39 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
40 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
41 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
42 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
43 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
44 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
45 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
46 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
47 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
48 #include "builtin-types.def"
49 #undef DEF_PRIMITIVE_TYPE
50 #undef DEF_FUNCTION_TYPE_0
51 #undef DEF_FUNCTION_TYPE_1
52 #undef DEF_FUNCTION_TYPE_2
53 #undef DEF_FUNCTION_TYPE_3
54 #undef DEF_FUNCTION_TYPE_4
55 #undef DEF_FUNCTION_TYPE_VAR_0
56 #undef DEF_FUNCTION_TYPE_VAR_1
57 #undef DEF_FUNCTION_TYPE_VAR_2
58 #undef DEF_POINTER_TYPE
62 static tree max_builtin
PARAMS ((tree
, tree
));
63 static tree min_builtin
PARAMS ((tree
, tree
));
64 static tree abs_builtin
PARAMS ((tree
, tree
));
65 static tree cos_builtin
PARAMS ((tree
, tree
));
66 static tree sin_builtin
PARAMS ((tree
, tree
));
67 static tree sqrt_builtin
PARAMS ((tree
, tree
));
69 static tree build_function_call_expr
PARAMS ((tree
, tree
));
70 static void define_builtin
PARAMS ((enum built_in_function
,
74 static tree define_builtin_type
PARAMS ((int, int, int, int, int));
78 /* Functions of this type are used to inline a given call. Such a
79 function should either return an expression, if the call is to be
80 inlined, or NULL_TREE if a real call should be emitted. Arguments
81 are method return type and arguments to call. */
82 typedef tree builtin_creator_function
PARAMS ((tree
, tree
));
84 /* Hold a char*, before initialization, or a tree, after
92 /* Used to hold a single builtin record. */
95 union string_or_tree class_name
;
96 union string_or_tree method_name
;
97 builtin_creator_function
*creator
;
100 static struct builtin_record java_builtins
[] =
102 { { "java.lang.Math" }, { "min" }, min_builtin
},
103 { { "java.lang.Math" }, { "max" }, max_builtin
},
104 { { "java.lang.Math" }, { "abs" }, abs_builtin
},
105 { { "java.lang.Math" }, { "cos" }, cos_builtin
},
106 { { "java.lang.Math" }, { "sin" }, sin_builtin
},
107 { { "java.lang.Math" }, { "sqrt" }, sqrt_builtin
},
108 { { NULL
}, { NULL
}, NULL
}
111 /* This is only used transiently, so we don't mark it as roots for the
113 static tree builtin_types
[(int) BT_LAST
];
116 /* Internal functions which implement various builtin conversions. */
119 max_builtin (method_return_type
, method_arguments
)
120 tree method_return_type
, method_arguments
;
122 return build (MAX_EXPR
, method_return_type
,
123 TREE_VALUE (method_arguments
),
124 TREE_VALUE (TREE_CHAIN (method_arguments
)));
128 min_builtin (method_return_type
, method_arguments
)
129 tree method_return_type
, method_arguments
;
131 return build (MIN_EXPR
, method_return_type
,
132 TREE_VALUE (method_arguments
),
133 TREE_VALUE (TREE_CHAIN (method_arguments
)));
137 abs_builtin (method_return_type
, method_arguments
)
138 tree method_return_type
, method_arguments
;
140 return build1 (ABS_EXPR
, method_return_type
,
141 TREE_VALUE (method_arguments
));
144 /* Mostly copied from ../builtins.c. */
146 build_function_call_expr (tree fn
, tree arglist
)
150 call_expr
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (fn
)), fn
);
151 call_expr
= build (CALL_EXPR
, TREE_TYPE (TREE_TYPE (fn
)),
153 TREE_SIDE_EFFECTS (call_expr
) = 1;
158 cos_builtin (method_return_type
, method_arguments
)
159 tree method_return_type ATTRIBUTE_UNUSED
, method_arguments
;
161 /* FIXME: this assumes that jdouble and double are the same. */
162 tree fn
= built_in_decls
[BUILT_IN_COS
];
165 return build_function_call_expr (fn
, method_arguments
);
169 sin_builtin (method_return_type
, method_arguments
)
170 tree method_return_type ATTRIBUTE_UNUSED
, method_arguments
;
172 /* FIXME: this assumes that jdouble and double are the same. */
173 tree fn
= built_in_decls
[BUILT_IN_SIN
];
176 return build_function_call_expr (fn
, method_arguments
);
180 sqrt_builtin (method_return_type
, method_arguments
)
181 tree method_return_type ATTRIBUTE_UNUSED
, method_arguments
;
183 /* FIXME: this assumes that jdouble and double are the same. */
184 tree fn
= built_in_decls
[BUILT_IN_SQRT
];
187 return build_function_call_expr (fn
, method_arguments
);
192 /* Define a single builtin. */
194 define_builtin (val
, name
, class, type
, fallback_p
)
195 enum built_in_function val
;
197 enum built_in_class
class;
203 if (! name
|| ! type
)
206 if (strncmp (name
, "__builtin_", strlen ("__builtin_")) != 0)
208 decl
= build_decl (FUNCTION_DECL
, get_identifier (name
), type
);
209 DECL_EXTERNAL (decl
) = 1;
210 TREE_PUBLIC (decl
) = 1;
212 SET_DECL_ASSEMBLER_NAME (decl
,
213 get_identifier (name
+ strlen ("__builtin_")));
214 make_decl_rtl (decl
, NULL
);
216 DECL_BUILT_IN_CLASS (decl
) = class;
217 DECL_FUNCTION_CODE (decl
) = val
;
218 built_in_decls
[val
] = decl
;
221 /* Compute the type for a builtin. */
223 define_builtin_type (ret
, arg1
, arg2
, arg3
, arg4
)
224 int ret
, arg1
, arg2
, arg3
, arg4
;
228 if (builtin_types
[ret
] == NULL_TREE
)
231 args
= void_list_node
;
235 if (builtin_types
[arg4
] == NULL_TREE
)
237 args
= tree_cons (NULL_TREE
, builtin_types
[arg4
], args
);
241 if (builtin_types
[arg3
] == NULL_TREE
)
243 args
= tree_cons (NULL_TREE
, builtin_types
[arg3
], args
);
247 if (builtin_types
[arg2
] == NULL_TREE
)
249 args
= tree_cons (NULL_TREE
, builtin_types
[arg2
], args
);
253 if (builtin_types
[arg1
] == NULL_TREE
)
255 args
= tree_cons (NULL_TREE
, builtin_types
[arg1
], args
);
258 return build_function_type (builtin_types
[ret
], args
);
263 /* Initialize the builtins. */
265 initialize_builtins ()
269 for (i
= 0; java_builtins
[i
].creator
!= NULL
; ++i
)
271 tree klass_id
= get_identifier (java_builtins
[i
].class_name
.s
);
272 tree m
= get_identifier (java_builtins
[i
].method_name
.s
);
274 java_builtins
[i
].class_name
.t
= klass_id
;
275 java_builtins
[i
].method_name
.t
= m
;
276 ggc_add_tree_root (&java_builtins
[i
].class_name
.t
, 1);
277 ggc_add_tree_root (&java_builtins
[i
].method_name
.t
, 1);
280 void_list_node
= end_params_node
;
282 /* Work around C-specific junk in builtin-types.def. */
283 #define intmax_type_node NULL_TREE
284 #define traditional_ptr_type_node NULL_TREE
285 #define traditional_cptr_type_node NULL_TREE
286 #define c_size_type_node NULL_TREE
287 #define const_string_type_node NULL_TREE
288 #define traditional_len_type_node NULL_TREE
289 #define va_list_ref_type_node NULL_TREE
290 #define va_list_arg_type_node NULL_TREE
291 #define flag_isoc99 0
293 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
294 builtin_types[(int) ENUM] = VALUE;
295 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
296 builtin_types[(int) ENUM] \
297 = define_builtin_type (RETURN, -1, -1, -1, -1);
298 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
299 builtin_types[(int) ENUM] \
300 = define_builtin_type (RETURN, ARG1, -1, -1, -1);
301 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
302 builtin_types[(int) ENUM] \
303 = define_builtin_type (RETURN, ARG1, ARG2, -1, -1);
304 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
305 builtin_types[(int) ENUM] \
306 = define_builtin_type (RETURN, ARG1, ARG2, ARG3, -1);
307 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
308 builtin_types[(int) ENUM] \
309 = define_builtin_type (RETURN, ARG1, ARG2, ARG3, ARG4);
310 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
311 builtin_types[(int) ENUM] = NULL_TREE;
312 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
313 builtin_types[(int) ENUM] = NULL_TREE;
314 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
315 builtin_types[(int) ENUM] = NULL_TREE;
316 #define DEF_POINTER_TYPE(ENUM, TYPE) \
317 builtin_types[(int) ENUM] = NULL_TREE;
319 #include "builtin-types.def"
321 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, \
322 FALLBACK_P, NONANSI_P) \
323 define_builtin (ENUM, NAME, CLASS, builtin_types[TYPE], FALLBACK_P);
324 #include "builtins.def"
327 /* If the call matches a builtin, return the
328 appropriate builtin expression instead. */
330 check_for_builtin (method
, call
)
334 if (! flag_emit_class_files
&& optimize
&& TREE_CODE (call
) == CALL_EXPR
)
337 tree method_arguments
= TREE_OPERAND (call
, 1);
338 tree method_class
= DECL_NAME (TYPE_NAME (DECL_CONTEXT (method
)));
339 tree method_name
= DECL_NAME (method
);
340 tree method_return_type
= TREE_TYPE (TREE_TYPE (method
));
342 for (i
= 0; java_builtins
[i
].creator
!= NULL
; ++i
)
344 if (method_class
== java_builtins
[i
].class_name
.t
345 && method_name
== java_builtins
[i
].method_name
.t
)
347 return (*java_builtins
[i
].creator
) (method_return_type
,