1 // backend.h -- Go frontend interface to backend -*- C++ -*-
3 // Copyright 2011 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
13 // Pointers to these types are created by the backend, passed to the
14 // frontend, and passed back to the backend. The types must be
15 // defined by the backend using these names.
17 // The backend representation of a type.
20 // The backend represention of an expression.
23 // The backend representation of a statement.
26 // The backend representation of a function definition or declaration.
29 // The backend representation of a block.
32 // The backend representation of a variable.
35 // The backend representation of a label.
38 // The backend interface. This is a pure abstract class that a
39 // specific backend will implement.
44 virtual ~Backend() { }
46 // Name/type/location. Used for function parameters, struct fields,
48 struct Btyped_identifier
55 : name(), btype(NULL
), location(UNKNOWN_LOCATION
)
58 Btyped_identifier(const std::string
& a_name
, Btype
* a_btype
,
60 : name(a_name
), btype(a_btype
), location(a_location
)
66 // Produce an error type. Actually the backend could probably just
67 // crash if this is called.
71 // Get a void type. This is used in (at least) two ways: 1) as the
72 // return type of a function with no result parameters; 2)
73 // unsafe.Pointer is represented as *void.
77 // Get the unnamed boolean type.
81 // Get an unnamed integer type with the given signedness and number
84 integer_type(bool is_unsigned
, int bits
) = 0;
86 // Get an unnamed floating point type with the given number of bits
89 float_type(int bits
) = 0;
91 // Get an unnamed complex type with the given number of bits (64 or 128).
93 complex_type(int bits
) = 0;
95 // Get a pointer type.
97 pointer_type(Btype
* to_type
) = 0;
99 // Get a function type. The receiver, parameter, and results are
100 // generated from the types in the Function_type. The Function_type
101 // is provided so that the names are available. This should return
102 // not the type of a Go function (which is a pointer to a struct)
103 // but the type of a C function pointer (which will be used as the
104 // type of the first field of the struct).
106 function_type(const Btyped_identifier
& receiver
,
107 const std::vector
<Btyped_identifier
>& parameters
,
108 const std::vector
<Btyped_identifier
>& results
,
109 Location location
) = 0;
111 // Get a struct type.
113 struct_type(const std::vector
<Btyped_identifier
>& fields
) = 0;
115 // Get an array type.
117 array_type(Btype
* element_type
, Bexpression
* length
) = 0;
119 // Create a placeholder pointer type. This is used for a named
120 // pointer type, since in Go a pointer type may refer to itself.
121 // NAME is the name of the type, and the location is where the named
122 // type is defined. This function is also used for unnamed function
123 // types with multiple results, in which case the type has no name
124 // and NAME will be empty. FOR_FUNCTION is true if this is for a Go
125 // function type, which corresponds to a C/C++ pointer to function
126 // type. The return value will later be passed as the first
127 // parameter to set_placeholder_pointer_type or
128 // set_placeholder_function_type.
130 placeholder_pointer_type(const std::string
& name
, Location
,
131 bool for_function
) = 0;
133 // Fill in a placeholder pointer type as a pointer. This takes a
134 // type returned by placeholder_pointer_type and arranges for it to
135 // point to the type that TO_TYPE points to (that is, PLACEHOLDER
136 // becomes the same type as TO_TYPE). Returns true on success,
139 set_placeholder_pointer_type(Btype
* placeholder
, Btype
* to_type
) = 0;
141 // Fill in a placeholder pointer type as a function. This takes a
142 // type returned by placeholder_pointer_type and arranges for it to
143 // become a real Go function type (which corresponds to a C/C++
144 // pointer to function type). FT will be something returned by the
145 // function_type method. Returns true on success, false on failure.
147 set_placeholder_function_type(Btype
* placeholder
, Btype
* ft
) = 0;
149 // Create a placeholder struct type. This is used for a named
150 // struct type, as with placeholder_pointer_type. It is also used
151 // for interface types, in which case NAME will be the empty string.
153 placeholder_struct_type(const std::string
& name
, Location
) = 0;
155 // Fill in a placeholder struct type. This takes a type returned by
156 // placeholder_struct_type and arranges for it to become a real
157 // struct type. The parameter is as for struct_type. Returns true
158 // on success, false on failure.
160 set_placeholder_struct_type(Btype
* placeholder
,
161 const std::vector
<Btyped_identifier
>& fields
)
164 // Create a placeholder array type. This is used for a named array
165 // type, as with placeholder_pointer_type, to handle cases like
168 placeholder_array_type(const std::string
& name
, Location
) = 0;
170 // Fill in a placeholder array type. This takes a type returned by
171 // placeholder_array_type and arranges for it to become a real array
172 // type. The parameters are as for array_type. Returns true on
173 // success, false on failure.
175 set_placeholder_array_type(Btype
* placeholder
, Btype
* element_type
,
176 Bexpression
* length
) = 0;
178 // Return a named version of a type. The location is the location
179 // of the type definition. This will not be called for a type
180 // created via placeholder_pointer_type, placeholder_struct_type, or
181 // placeholder_array_type.. (It may be called for a pointer,
182 // struct, or array type in a case like "type P *byte; type Q P".)
184 named_type(const std::string
& name
, Btype
*, Location
) = 0;
186 // Create a marker for a circular pointer type. Go pointer and
187 // function types can refer to themselves in ways that are not
188 // permitted in C/C++. When a circular type is found, this function
189 // is called for the circular reference. This permits the backend
190 // to decide how to handle such a type. PLACEHOLDER is the
191 // placeholder type which has already been created; if the backend
192 // is prepared to handle a circular pointer type, it may simply
193 // return PLACEHOLDER. FOR_FUNCTION is true if this is for a
196 // For "type P *P" the sequence of calls will be
197 // bt1 = placeholder_pointer_type();
198 // bt2 = circular_pointer_type(bt1, false);
199 // set_placeholder_pointer_type(bt1, bt2);
201 circular_pointer_type(Btype
* placeholder
, bool for_function
) = 0;
203 // Return whether the argument could be a special type created by
204 // circular_pointer_type. This is used to introduce explicit type
205 // conversions where needed. If circular_pointer_type returns its
206 // PLACEHOLDER parameter, this may safely always return false.
208 is_circular_pointer_type(Btype
*) = 0;
210 // Return the size of a type.
212 type_size(Btype
*) = 0;
214 // Return the alignment of a type.
216 type_alignment(Btype
*) = 0;
218 // Return the alignment of a struct field of this type. This is
219 // normally the same as type_alignment, but not always.
221 type_field_alignment(Btype
*) = 0;
223 // Return the offset of field INDEX in a struct type. INDEX is the
224 // entry in the FIELDS std::vector parameter of struct_type or
225 // set_placeholder_struct_type.
227 type_field_offset(Btype
*, size_t index
) = 0;
231 // Return an expression for a zero value of the given type. This is
232 // used for cases such as local variable initialization and
233 // converting nil to other types.
235 zero_expression(Btype
*) = 0;
237 // Create an error expression. This is used for cases which should
238 // not occur in a correct program, in order to keep the compilation
239 // going without crashing.
241 error_expression() = 0;
243 // Create a reference to a variable.
245 var_expression(Bvariable
* var
, Location
) = 0;
247 // Create an expression that indirects through the pointer expression EXPR
248 // (i.e., return the expression for *EXPR). KNOWN_VALID is true if the pointer
249 // is known to point to a valid memory location.
251 indirect_expression(Bexpression
* expr
, bool known_valid
, Location
) = 0;
253 // Return an expression for the multi-precision integer VAL in BTYPE.
255 integer_constant_expression(Btype
* btype
, mpz_t val
) = 0;
257 // Return an expression for the floating point value VAL in BTYPE.
259 float_constant_expression(Btype
* btype
, mpfr_t val
) = 0;
261 // Return an expression for the complex value REAL/IMAG in BTYPE.
263 complex_constant_expression(Btype
* btype
, mpfr_t real
, mpfr_t imag
) = 0;
265 // Return an expression that converts EXPR to TYPE.
267 convert_expression(Btype
* type
, Bexpression
* expr
, Location
) = 0;
269 // Create an expression for the address of a function. This is used to
270 // get the address of the code for a function.
272 function_code_expression(Bfunction
*, Location
) = 0;
274 // Create an expression that takes the address of an expression.
276 address_expression(Bexpression
*, Location
) = 0;
280 // Create an error statement. This is used for cases which should
281 // not occur in a correct program, in order to keep the compilation
282 // going without crashing.
284 error_statement() = 0;
286 // Create an expression statement.
288 expression_statement(Bexpression
*) = 0;
290 // Create a variable initialization statement. This initializes a
291 // local variable at the point in the program flow where it is
294 init_statement(Bvariable
* var
, Bexpression
* init
) = 0;
296 // Create an assignment statement.
298 assignment_statement(Bexpression
* lhs
, Bexpression
* rhs
,
301 // Create a return statement, passing the representation of the
302 // function and the list of values to return.
304 return_statement(Bfunction
*, const std::vector
<Bexpression
*>&,
307 // Create an if statement. ELSE_BLOCK may be NULL.
309 if_statement(Bexpression
* condition
, Bblock
* then_block
, Bblock
* else_block
,
312 // Create a switch statement where the case values are constants.
313 // CASES and STATEMENTS must have the same number of entries. If
314 // VALUE matches any of the list in CASES[i], which will all be
315 // integers, then STATEMENTS[i] is executed. STATEMENTS[i] will
316 // either end with a goto statement or will fall through into
317 // STATEMENTS[i + 1]. CASES[i] is empty for the default clause,
318 // which need not be last.
320 switch_statement(Bexpression
* value
,
321 const std::vector
<std::vector
<Bexpression
*> >& cases
,
322 const std::vector
<Bstatement
*>& statements
,
325 // Create a single statement from two statements.
327 compound_statement(Bstatement
*, Bstatement
*) = 0;
329 // Create a single statement from a list of statements.
331 statement_list(const std::vector
<Bstatement
*>&) = 0;
335 // Create a block. The frontend will call this function when it
336 // starts converting a block within a function. FUNCTION is the
337 // current function. ENCLOSING is the enclosing block; it will be
338 // NULL for the top-level block in a function. VARS is the list of
339 // local variables defined within this block; each entry will be
340 // created by the local_variable function. START_LOCATION is the
341 // location of the start of the block, more or less the location of
342 // the initial curly brace. END_LOCATION is the location of the end
343 // of the block, more or less the location of the final curly brace.
344 // The statements will be added after the block is created.
346 block(Bfunction
* function
, Bblock
* enclosing
,
347 const std::vector
<Bvariable
*>& vars
,
348 Location start_location
, Location end_location
) = 0;
350 // Add the statements to a block. The block is created first. Then
351 // the statements are created. Then the statements are added to the
352 // block. This will called exactly once per block. The vector may
353 // be empty if there are no statements.
355 block_add_statements(Bblock
*, const std::vector
<Bstatement
*>&) = 0;
357 // Return the block as a statement. This is used to include a block
358 // in a list of statements.
360 block_statement(Bblock
*) = 0;
364 // Create an error variable. This is used for cases which should
365 // not occur in a correct program, in order to keep the compilation
366 // going without crashing.
368 error_variable() = 0;
370 // Create a global variable. PACKAGE_NAME is the name of the
371 // package where the variable is defined. PKGPATH is the package
372 // path for that package, from the -fgo-pkgpath or -fgo-prefix
373 // option. NAME is the name of the variable. BTYPE is the type of
374 // the variable. IS_EXTERNAL is true if the variable is defined in
375 // some other package. IS_HIDDEN is true if the variable is not
376 // exported (name begins with a lower case letter).
377 // IN_UNIQUE_SECTION is true if the variable should be put into a
378 // unique section if possible; this is intended to permit the linker
379 // to garbage collect the variable if it is not referenced.
380 // LOCATION is where the variable was defined.
382 global_variable(const std::string
& package_name
,
383 const std::string
& pkgpath
,
384 const std::string
& name
,
388 bool in_unique_section
,
389 Location location
) = 0;
391 // A global variable will 1) be initialized to zero, or 2) be
392 // initialized to a constant value, or 3) be initialized in the init
393 // function. In case 2, the frontend will call
394 // global_variable_set_init to set the initial value. If this is
395 // not called, the backend should initialize a global variable to 0.
396 // The init function may then assign a value to it.
398 global_variable_set_init(Bvariable
*, Bexpression
*) = 0;
400 // Create a local variable. The frontend will create the local
401 // variables first, and then create the block which contains them.
402 // FUNCTION is the function in which the variable is defined. NAME
403 // is the name of the variable. TYPE is the type. IS_ADDRESS_TAKEN
404 // is true if the address of this variable is taken (this implies
405 // that the address does not escape the function, as otherwise the
406 // variable would be on the heap). LOCATION is where the variable
407 // is defined. For each local variable the frontend will call
408 // init_statement to set the initial value.
410 local_variable(Bfunction
* function
, const std::string
& name
, Btype
* type
,
411 bool is_address_taken
, Location location
) = 0;
413 // Create a function parameter. This is an incoming parameter, not
414 // a result parameter (result parameters are treated as local
415 // variables). The arguments are as for local_variable.
417 parameter_variable(Bfunction
* function
, const std::string
& name
,
418 Btype
* type
, bool is_address_taken
,
419 Location location
) = 0;
421 // Create a temporary variable. A temporary variable has no name,
422 // just a type. We pass in FUNCTION and BLOCK in case they are
423 // needed. If INIT is not NULL, the variable should be initialized
424 // to that value. Otherwise the initial value is irrelevant--the
425 // backend does not have to explicitly initialize it to zero.
426 // ADDRESS_IS_TAKEN is true if the programs needs to take the
427 // address of this temporary variable. LOCATION is the location of
428 // the statement or expression which requires creating the temporary
429 // variable, and may not be very useful. This function should
430 // return a variable which can be referenced later and should set
431 // *PSTATEMENT to a statement which initializes the variable.
433 temporary_variable(Bfunction
*, Bblock
*, Btype
*, Bexpression
* init
,
434 bool address_is_taken
, Location location
,
435 Bstatement
** pstatement
) = 0;
437 // Create a named immutable initialized data structure. This is
438 // used for type descriptors, map descriptors, and function
439 // descriptors. This returns a Bvariable because it corresponds to
440 // an initialized const variable in C.
442 // NAME is the name to use for the initialized global variable which
443 // this call will create.
445 // IS_HIDDEN will be true if the descriptor should only be visible
446 // within the current object.
448 // IS_COMMON is true if NAME may be defined by several packages, and
449 // the linker should merge all such definitions. If IS_COMMON is
450 // false, NAME should be defined in only one file. In general
451 // IS_COMMON will be true for the type descriptor of an unnamed type
452 // or a builtin type. IS_HIDDEN and IS_COMMON will never both be
455 // TYPE will be a struct type; the type of the returned expression
456 // must be a pointer to this struct type.
458 // We must create the named structure before we know its
459 // initializer, because the initializer may refer to its own
460 // address. After calling this the frontend will call
461 // immutable_struct_set_init.
463 immutable_struct(const std::string
& name
, bool is_hidden
, bool is_common
,
464 Btype
* type
, Location
) = 0;
466 // Set the initial value of a variable created by immutable_struct.
467 // The NAME, IS_HIDDEN, IS_COMMON, TYPE, and location parameters are
468 // the same ones passed to immutable_struct. INITIALIZER will be a
469 // composite literal of type TYPE. It will not contain any function
470 // calls or anything else that can not be put into a read-only data
471 // section. It may contain the address of variables created by
474 immutable_struct_set_init(Bvariable
*, const std::string
& name
,
475 bool is_hidden
, bool is_common
, Btype
* type
,
476 Location
, Bexpression
* initializer
) = 0;
478 // Create a reference to a named immutable initialized data
479 // structure defined in some other package. This will be a
480 // structure created by a call to immutable_struct with the same
481 // NAME and TYPE and with IS_COMMON passed as false. This
482 // corresponds to an extern const global variable in C.
484 immutable_struct_reference(const std::string
& name
, Btype
* type
,
489 // Create a new label. NAME will be empty if this is a label
490 // created by the frontend for a loop construct. The location is
491 // where the the label is defined.
493 label(Bfunction
*, const std::string
& name
, Location
) = 0;
495 // Create a statement which defines a label. This statement will be
496 // put into the codestream at the point where the label should be
499 label_definition_statement(Blabel
*) = 0;
501 // Create a goto statement to a label.
503 goto_statement(Blabel
*, Location
) = 0;
505 // Create an expression for the address of a label. This is used to
506 // get the return address of a deferred function which may call
509 label_address(Blabel
*, Location
) = 0;
513 // Create an error function. This is used for cases which should
514 // not occur in a correct program, in order to keep the compilation
515 // going without crashing.
517 error_function() = 0;
519 // Declare or define a function of FNTYPE.
520 // NAME is the Go name of the function. ASM_NAME, if not the empty string, is
521 // the name that should be used in the symbol table; this will be non-empty if
522 // a magic extern comment is used.
523 // IS_VISIBLE is true if this function should be visible outside of the
524 // current compilation unit. IS_DECLARATION is true if this is a function
525 // declaration rather than a definition; the function definition will be in
526 // another compilation unit.
527 // IS_INLINABLE is true if the function can be inlined.
528 // DISABLE_SPLIT_STACK is true if this function may not split the stack; this
529 // is used for the implementation of recover.
530 // IN_UNIQUE_SECTION is true if this function should be put into a unique
531 // location if possible; this is used for field tracking.
533 function(Btype
* fntype
, const std::string
& name
, const std::string
& asm_name
,
534 bool is_visible
, bool is_declaration
, bool is_inlinable
,
535 bool disable_split_stack
, bool in_unique_section
, Location
) = 0;
538 // The backend interface has to define this function.
540 extern Backend
* go_get_backend();
542 // FIXME: Temporary helper functions while converting to new backend
545 extern Btype
* tree_to_type(tree
);
546 extern Bexpression
* tree_to_expr(tree
);
547 extern Bstatement
* tree_to_stat(tree
);
548 extern Bfunction
* tree_to_function(tree
);
549 extern Bblock
* tree_to_block(tree
);
550 extern tree
type_to_tree(Btype
*);
551 extern tree
expr_to_tree(Bexpression
*);
552 extern tree
stat_to_tree(Bstatement
*);
553 extern tree
block_to_tree(Bblock
*);
554 extern tree
var_to_tree(Bvariable
*);
555 extern tree
function_to_tree(Bfunction
*);
557 #endif // !defined(GO_BACKEND_H)