Branch data Line data Source code
1 : : // Copyright (C) 2020-2024 Free Software Foundation, Inc.
2 : :
3 : : // This file is part of GCC.
4 : :
5 : : // GCC is free software; you can redistribute it and/or modify it under
6 : : // the terms of the GNU General Public License as published by the Free
7 : : // Software Foundation; either version 3, or (at your option) any later
8 : : // version.
9 : :
10 : : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : : // for more details.
14 : :
15 : : // You should have received a copy of the GNU General Public License
16 : : // along with GCC; see the file COPYING3. If not see
17 : : // <http://www.gnu.org/licenses/>.
18 : :
19 : : #ifndef RUST_BACKEND_H
20 : : #define RUST_BACKEND_H
21 : :
22 : : #include <gmp.h>
23 : : #include <mpfr.h>
24 : : #include <mpc.h>
25 : :
26 : : #include "rust-location.h"
27 : : #include "rust-linemap.h"
28 : : #include "rust-diagnostics.h"
29 : : #include "util/rust-operators.h"
30 : : #include "tree.h"
31 : : #include "rust-gcc.h"
32 : :
33 : : // Pointers to these types are created by the backend, passed to the
34 : : // frontend, and passed back to the backend. The types must be
35 : : // defined by the backend using these names.
36 : :
37 : : // The backend representation of a variable.
38 : : class Bvariable;
39 : :
40 : : // The backend interface. This is a pure abstract class that a
41 : : // specific backend will implement.
42 : :
43 : : namespace Backend {
44 : :
45 : : void
46 : : init ();
47 : :
48 : : // Name/type/location. Used for function parameters, struct fields,
49 : : // interface methods.
50 : 217815 : struct typed_identifier
51 : : {
52 : : std::string name;
53 : : tree type;
54 : : location_t location;
55 : :
56 : 11036 : typed_identifier () : name (), type (NULL_TREE), location (UNKNOWN_LOCATION)
57 : : {}
58 : :
59 : 58045 : typed_identifier (const std::string &a_name, tree a_type,
60 : : location_t a_location)
61 : 58045 : : name (a_name), type (a_type), location (a_location)
62 : : {}
63 : : };
64 : :
65 : : // debug
66 : : void debug (tree);
67 : : void
68 : : debug (Bvariable *);
69 : :
70 : : tree
71 : : get_identifier_node (const std::string &str);
72 : :
73 : : // Types.
74 : :
75 : : // Get the wchar type
76 : : tree
77 : : wchar_type ();
78 : :
79 : : // Get the Host pointer size in bits
80 : : int
81 : : get_pointer_size ();
82 : :
83 : : // Get the raw str type const char*
84 : : tree
85 : : raw_str_type ();
86 : :
87 : : // Get an unnamed integer type with the given signedness and number
88 : : // of bits.
89 : : tree
90 : : integer_type (bool is_unsigned, int bits);
91 : :
92 : : // Get an unnamed floating point type with the given number of bits
93 : : // (32 or 64).
94 : : tree
95 : : float_type (int bits);
96 : :
97 : : // Get a pointer type.
98 : : tree
99 : : pointer_type (tree to_type);
100 : :
101 : : // Get a reference type.
102 : : tree
103 : : reference_type (tree to_type);
104 : :
105 : : // make type immutable
106 : : tree
107 : : immutable_type (tree base);
108 : :
109 : : // Get a function type. The receiver, parameter, and results are
110 : : // generated from the types in the Function_type. The Function_type
111 : : // is provided so that the names are available. This should return
112 : : // not the type of a Go function (which is a pointer to a struct)
113 : : // but the type of a C function pointer (which will be used as the
114 : : // type of the first field of the struct). If there is more than
115 : : // one result, RESULT_STRUCT is a struct type to hold the results,
116 : : // and RESULTS may be ignored; if there are zero or one results,
117 : : // RESULT_STRUCT is NULL.
118 : : tree
119 : : function_type (const typed_identifier &receiver,
120 : : const std::vector<typed_identifier> ¶meters,
121 : : const std::vector<typed_identifier> &results, tree result_struct,
122 : : location_t location);
123 : :
124 : : tree
125 : : function_type_variadic (const typed_identifier &receiver,
126 : : const std::vector<typed_identifier> ¶meters,
127 : : const std::vector<typed_identifier> &results,
128 : : tree result_struct, location_t location);
129 : :
130 : : tree
131 : : function_ptr_type (tree result, const std::vector<tree> &praameters,
132 : : location_t location);
133 : :
134 : : // Get a struct type.
135 : : tree
136 : : struct_type (const std::vector<typed_identifier> &fields);
137 : :
138 : : // Get a union type.
139 : : tree
140 : : union_type (const std::vector<typed_identifier> &fields);
141 : :
142 : : // Get an array type.
143 : : tree
144 : : array_type (tree element_type, tree length);
145 : :
146 : : // Return a named version of a type. The location is the location
147 : : // of the type definition. This will not be called for a type
148 : : // created via placeholder_pointer_type, placeholder_struct_type, or
149 : : // placeholder_array_type.. (It may be called for a pointer,
150 : : // struct, or array type in a case like "type P *byte; type Q P".)
151 : : tree
152 : : named_type (const std::string &name, tree, location_t);
153 : :
154 : : // Return the size of a type.
155 : : int64_t type_size (tree);
156 : :
157 : : // Return the alignment of a type.
158 : : int64_t type_alignment (tree);
159 : :
160 : : // Return the alignment of a struct field of this type. This is
161 : : // normally the same as type_alignment, but not always.
162 : : int64_t type_field_alignment (tree);
163 : :
164 : : // Return the offset of field INDEX in a struct type. INDEX is the
165 : : // entry in the FIELDS std::vector parameter of struct_type or
166 : : // set_placeholder_struct_type.
167 : : int64_t
168 : : type_field_offset (tree, size_t index);
169 : :
170 : : // Expressions.
171 : :
172 : : // Return an expression for a zero value of the given type. This is
173 : : // used for cases such as local variable initialization and
174 : : // converting nil to other types.
175 : : tree zero_expression (tree);
176 : :
177 : : // Create a reference to a variable.
178 : : tree
179 : : var_expression (Bvariable *var, location_t);
180 : :
181 : : // Return an expression for the floating point value VAL in BTYPE.
182 : : tree
183 : : float_constant_expression (tree btype, mpfr_t val);
184 : :
185 : : // Return an expression for the string value VAL.
186 : : tree
187 : : string_constant_expression (const std::string &val);
188 : :
189 : : // Get a char literal
190 : : tree
191 : : char_constant_expression (char c);
192 : :
193 : : // Get a char literal
194 : : tree
195 : : wchar_constant_expression (wchar_t c);
196 : :
197 : : // Return an expression for the boolean value VAL.
198 : : tree
199 : : boolean_constant_expression (bool val);
200 : :
201 : : // Return an expression that converts EXPR to TYPE.
202 : : tree
203 : : convert_expression (tree type, tree expr, location_t);
204 : :
205 : : // Return an expression for the field at INDEX in BSTRUCT.
206 : : tree
207 : : struct_field_expression (tree bstruct, size_t index, location_t);
208 : :
209 : : // Create an expression that executes BSTAT before BEXPR.
210 : : tree
211 : : compound_expression (tree bstat, tree bexpr, location_t);
212 : :
213 : : // Return an expression that executes THEN_EXPR if CONDITION is true, or
214 : : // ELSE_EXPR otherwise and returns the result as type BTYPE, within the
215 : : // specified function FUNCTION. ELSE_EXPR may be NULL. BTYPE may be NULL.
216 : : tree
217 : : conditional_expression (tree function, tree btype, tree condition,
218 : : tree then_expr, tree else_expr, location_t);
219 : :
220 : : // Return an expression for the negation operation OP EXPR.
221 : : // Supported values of OP are enumerated in NegationOperator.
222 : : tree
223 : : negation_expression (NegationOperator op, tree expr, location_t);
224 : :
225 : : // Return an expression for the operation LEFT OP RIGHT.
226 : : // Supported values of OP are enumerated in ArithmeticOrLogicalOperator.
227 : : tree
228 : : arithmetic_or_logical_expression (ArithmeticOrLogicalOperator op, tree left,
229 : : tree right, location_t loc);
230 : :
231 : : // Return an expression for the operation LEFT OP RIGHT.
232 : : // Supported values of OP are enumerated in ArithmeticOrLogicalOperator.
233 : : // This function adds overflow checking and returns a list of statements to
234 : : // add to the current function context. The `receiver` variable refers to the
235 : : // variable which will contain the result of that operation.
236 : : tree
237 : : arithmetic_or_logical_expression_checked (ArithmeticOrLogicalOperator op,
238 : : tree left, tree right, location_t loc,
239 : : Bvariable *receiver);
240 : :
241 : : // Return an expression for the operation LEFT OP RIGHT.
242 : : // Supported values of OP are enumerated in ComparisonOperator.
243 : : tree
244 : : comparison_expression (ComparisonOperator op, tree left, tree right,
245 : : location_t loc);
246 : :
247 : : // Return an expression for the operation LEFT OP RIGHT.
248 : : // Supported values of OP are enumerated in LazyBooleanOperator.
249 : : tree
250 : : lazy_boolean_expression (LazyBooleanOperator op, tree left, tree right,
251 : : location_t);
252 : :
253 : : // Return an expression that constructs BTYPE with VALS. BTYPE must be the
254 : : // backend representation a of struct. VALS must be in the same order as the
255 : : // corresponding fields in BTYPE.
256 : : tree
257 : : constructor_expression (tree btype, bool is_variant,
258 : : const std::vector<tree> &vals, int, location_t);
259 : :
260 : : // Return an expression that constructs an array of BTYPE with INDEXES and
261 : : // VALS. INDEXES and VALS must have the same amount of elements. Each index
262 : : // in INDEXES must be in the same order as the corresponding value in VALS.
263 : : tree
264 : : array_constructor_expression (tree btype,
265 : : const std::vector<unsigned long> &indexes,
266 : : const std::vector<tree> &vals, location_t);
267 : :
268 : : tree
269 : : array_initializer (tree, tree, tree, tree, tree, tree *, location_t);
270 : :
271 : : // Return an expression for ARRAY[INDEX] as an l-value. ARRAY is a valid
272 : : // fixed-length array, not a slice.
273 : : tree
274 : : array_index_expression (tree array, tree index, location_t);
275 : :
276 : : // Create an expression for a call to FN with ARGS, taking place within
277 : : // caller CALLER.
278 : : tree
279 : : call_expression (tree fn, const std::vector<tree> &args, tree static_chain,
280 : : location_t);
281 : :
282 : : // Statements.
283 : :
284 : : // Create a variable initialization statement in the specified
285 : : // function. This initializes a local variable at the point in the
286 : : // program flow where it is declared.
287 : : tree
288 : : init_statement (tree, Bvariable *var, tree init);
289 : :
290 : : // Create an assignment statement within the specified function.
291 : : tree
292 : : assignment_statement (tree lhs, tree rhs, location_t);
293 : :
294 : : // Create return statement for an decl for a value (can be NULL_TREE) at a
295 : : // location
296 : : tree
297 : : return_statement (tree fndecl, tree val, location_t);
298 : :
299 : : // Create an if statement within a function. ELSE_BLOCK may be NULL.
300 : : tree
301 : : if_statement (tree, tree condition, tree then_block, tree else_block,
302 : : location_t);
303 : :
304 : : // infinite loop expressions
305 : : tree
306 : : loop_expression (tree body, location_t);
307 : :
308 : : // exit expressions
309 : : tree
310 : : exit_expression (tree condition, location_t);
311 : :
312 : : // Create a single statement from two statements.
313 : : tree compound_statement (tree, tree);
314 : :
315 : : // Create a single statement from a list of statements.
316 : : tree
317 : : statement_list (const std::vector<tree> &);
318 : :
319 : : // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if
320 : : // an exception occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and
321 : : // if not NULL, it will always be executed. This is used for handling defers
322 : : // in Go functions. In C++, the resulting code is of this form:
323 : : // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
324 : : tree
325 : : exception_handler_statement (tree bstat, tree except_stmt, tree finally_stmt,
326 : : location_t);
327 : :
328 : : // Blocks.
329 : :
330 : : // Create a block. The frontend will call this function when it
331 : : // starts converting a block within a function. FUNCTION is the
332 : : // current function. ENCLOSING is the enclosing block; it will be
333 : : // NULL for the top-level block in a function. VARS is the list of
334 : : // local variables defined within this block; each entry will be
335 : : // created by the local_variable function. START_LOCATION is the
336 : : // location of the start of the block, more or less the location of
337 : : // the initial curly brace. END_LOCATION is the location of the end
338 : : // of the block, more or less the location of the final curly brace.
339 : : // The statements will be added after the block is created.
340 : : tree
341 : : block (tree function, tree enclosing, const std::vector<Bvariable *> &vars,
342 : : location_t start_location, location_t end_location);
343 : :
344 : : // Add the statements to a block. The block is created first. Then
345 : : // the statements are created. Then the statements are added to the
346 : : // block. This will called exactly once per block. The vector may
347 : : // be empty if there are no statements.
348 : : void
349 : : block_add_statements (tree, const std::vector<tree> &);
350 : :
351 : : // Variables.
352 : :
353 : : // Create a global variable. NAME is the package-qualified name of
354 : : // the variable. ASM_NAME is the encoded identifier for the
355 : : // variable, incorporating the package, and made safe for the
356 : : // assembler. BTYPE is the type of the variable. IS_EXTERNAL is
357 : : // true if the variable is defined in some other package. IS_HIDDEN
358 : : // is true if the variable is not exported (name begins with a lower
359 : : // case letter). IN_UNIQUE_SECTION is true if the variable should
360 : : // be put into a unique section if possible; this is intended to
361 : : // permit the linker to garbage collect the variable if it is not
362 : : // referenced. LOCATION is where the variable was defined.
363 : : Bvariable *
364 : : global_variable (const std::string &name, const std::string &asm_name,
365 : : tree btype, bool is_external, bool is_hidden,
366 : : bool in_unique_section, location_t location);
367 : :
368 : : // A global variable will 1) be initialized to zero, or 2) be
369 : : // initialized to a constant value, or 3) be initialized in the init
370 : : // function. In case 2, the frontend will call
371 : : // global_variable_set_init to set the initial value. If this is
372 : : // not called, the backend should initialize a global variable to 0.
373 : : // The init function may then assign a value to it.
374 : : void
375 : : global_variable_set_init (Bvariable *, tree);
376 : :
377 : : // Create a local variable. The frontend will create the local
378 : : // variables first, and then create the block which contains them.
379 : : // FUNCTION is the function in which the variable is defined. NAME
380 : : // is the name of the variable. TYPE is the type. DECL_VAR, if not
381 : : // null, gives the location at which the value of this variable may
382 : : // be found, typically used to create an inner-scope reference to an
383 : : // outer-scope variable, to extend the lifetime of the variable beyond
384 : : // the inner scope. IS_ADDRESS_TAKEN is true if the address of this
385 : : // variable is taken (this implies that the address does not escape
386 : : // the function, as otherwise the variable would be on the heap).
387 : : // LOCATION is where the variable is defined. For each local variable
388 : : // the frontend will call init_statement to set the initial value.
389 : : Bvariable *
390 : : local_variable (tree function, const std::string &name, tree type,
391 : : Bvariable *decl_var, location_t location);
392 : :
393 : : // Create a function parameter. This is an incoming parameter, not
394 : : // a result parameter (result parameters are treated as local
395 : : // variables). The arguments are as for local_variable.
396 : : Bvariable *
397 : : parameter_variable (tree function, const std::string &name, tree type,
398 : : location_t location);
399 : :
400 : : // Create a static chain parameter. This is the closure parameter.
401 : : Bvariable *
402 : : static_chain_variable (tree function, const std::string &name, tree type,
403 : : location_t location);
404 : :
405 : : // Create a temporary variable. A temporary variable has no name,
406 : : // just a type. We pass in FUNCTION and BLOCK in case they are
407 : : // needed. If INIT is not NULL, the variable should be initialized
408 : : // to that value. Otherwise the initial value is irrelevant--the
409 : : // backend does not have to explicitly initialize it to zero.
410 : : // ADDRESS_IS_TAKEN is true if the programs needs to take the
411 : : // address of this temporary variable. LOCATION is the location of
412 : : // the statement or expression which requires creating the temporary
413 : : // variable, and may not be very useful. This function should
414 : : // return a variable which can be referenced later and should set
415 : : // *PSTATEMENT to a statement which initializes the variable.
416 : : Bvariable *
417 : : temporary_variable (tree fndecl, tree bind_tree, tree type, tree init,
418 : : bool address_is_taken, location_t location,
419 : : tree *pstatement);
420 : :
421 : : // Labels.
422 : :
423 : : // Create a new label. NAME will be empty if this is a label
424 : : // created by the frontend for a loop construct. The location is
425 : : // where the label is defined.
426 : : tree
427 : : label (tree, const std::string &name, location_t);
428 : :
429 : : // Create a statement which defines a label. This statement will be
430 : : // put into the codestream at the point where the label should be
431 : : // defined.
432 : : tree label_definition_statement (tree);
433 : :
434 : : // Create a goto statement to a label.
435 : : tree goto_statement (tree, location_t);
436 : :
437 : : // Create an expression for the address of a label. This is used to
438 : : // get the return address of a deferred function which may call
439 : : // recover.
440 : : tree label_address (tree, location_t);
441 : :
442 : : // Functions.
443 : :
444 : : // Bit flags to pass to the function method.
445 : :
446 : : // Set if this is a function declaration rather than a definition;
447 : : // the definition will be in another compilation unit.
448 : : static const unsigned int function_is_declaration = 1 << 0;
449 : :
450 : : // Set if the function should never be inlined because they call
451 : : // recover and must be visible for correct panic recovery.
452 : : static const unsigned int function_is_uninlinable = 1 << 1;
453 : :
454 : : // Set if the function does not return. This is set for the
455 : : // implementation of panic.
456 : : static const unsigned int function_does_not_return = 1 << 2;
457 : :
458 : : // Set if the function should be put in a unique section if
459 : : // possible. This is used for field tracking.
460 : : static const unsigned int function_in_unique_section = 1 << 3;
461 : :
462 : : // Declare or define a function of FNTYPE.
463 : : // NAME is the Go name of the function. ASM_NAME, if not the empty
464 : : // string, is the name that should be used in the symbol table; this
465 : : // will be non-empty if a magic extern comment is used. FLAGS is
466 : : // bit flags described above.
467 : : tree
468 : : function (tree fntype, const std::string &name, const std::string &asm_name,
469 : : unsigned int flags, location_t);
470 : :
471 : : // Create a statement that runs all deferred calls for FUNCTION. This should
472 : : // be a statement that looks like this in C++:
473 : : // finish:
474 : : // try { DEFER_RETURN; } catch { CHECK_DEFER; goto finish; }
475 : : tree
476 : : function_defer_statement (tree function, tree undefer, tree check_defer,
477 : : location_t);
478 : :
479 : : // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
480 : : // This will only be called for a function definition. Returns true on
481 : : // success, false on failure.
482 : : bool
483 : : function_set_parameters (tree function,
484 : : const std::vector<Bvariable *> ¶m_vars);
485 : :
486 : : // Utility.
487 : :
488 : : // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
489 : : // FUNCTION_DECLS, and VARIABLE_DECLS declared globally.
490 : : void
491 : : write_global_definitions (const std::vector<tree> &type_decls,
492 : : const std::vector<tree> &constant_decls,
493 : : const std::vector<tree> &function_decls,
494 : : const std::vector<Bvariable *> &variable_decls);
495 : :
496 : : // TODO: make static
497 : :
498 : : tree
499 : : fill_in_fields (tree, const std::vector<typed_identifier> &);
500 : :
501 : : tree fill_in_array (tree, tree, tree);
502 : :
503 : : tree non_zero_size_type (tree);
504 : :
505 : : tree convert_tree (tree, tree, location_t);
506 : :
507 : : } // namespace Backend
508 : :
509 : : #endif // RUST_BACKEND_H
|