Merge from branches/gcc-4_8-branch up to rev 204657.
[official-gcc.git] / gcc-4_8-branch / gcc / go / gofrontend / backend.h
blobca997f08adeb721aa249f4da336ce7311810ce75
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.
7 #ifndef GO_BACKEND_H
8 #define GO_BACKEND_H
10 #include <gmp.h>
11 #include <mpfr.h>
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.
18 class Btype;
20 // The backend represention of an expression.
21 class Bexpression;
23 // The backend representation of a statement.
24 class Bstatement;
26 // The backend representation of a function definition or declaration.
27 class Bfunction;
29 // The backend representation of a block.
30 class Bblock;
32 // The backend representation of a variable.
33 class Bvariable;
35 // The backend representation of a label.
36 class Blabel;
38 // The backend interface. This is a pure abstract class that a
39 // specific backend will implement.
41 class Backend
43 public:
44 virtual ~Backend() { }
46 // Name/type/location. Used for function parameters, struct fields,
47 // interface methods.
48 struct Btyped_identifier
50 std::string name;
51 Btype* btype;
52 Location location;
54 Btyped_identifier()
55 : name(), btype(NULL), location(UNKNOWN_LOCATION)
56 { }
58 Btyped_identifier(const std::string& a_name, Btype* a_btype,
59 Location a_location)
60 : name(a_name), btype(a_btype), location(a_location)
61 { }
64 // Types.
66 // Produce an error type. Actually the backend could probably just
67 // crash if this is called.
68 virtual Btype*
69 error_type() = 0;
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.
74 virtual Btype*
75 void_type() = 0;
77 // Get the unnamed boolean type.
78 virtual Btype*
79 bool_type() = 0;
81 // Get an unnamed integer type with the given signedness and number
82 // of bits.
83 virtual Btype*
84 integer_type(bool is_unsigned, int bits) = 0;
86 // Get an unnamed floating point type with the given number of bits
87 // (32 or 64).
88 virtual Btype*
89 float_type(int bits) = 0;
91 // Get an unnamed complex type with the given number of bits (64 or 128).
92 virtual Btype*
93 complex_type(int bits) = 0;
95 // Get a pointer type.
96 virtual Btype*
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).
105 virtual Btype*
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.
112 virtual Btype*
113 struct_type(const std::vector<Btyped_identifier>& fields) = 0;
115 // Get an array type.
116 virtual Btype*
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.
129 virtual Btype*
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,
137 // false on failure.
138 virtual bool
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.
146 virtual bool
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.
152 virtual Btype*
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.
159 virtual bool
160 set_placeholder_struct_type(Btype* placeholder,
161 const std::vector<Btyped_identifier>& fields)
162 = 0;
164 // Create a placeholder array type. This is used for a named array
165 // type, as with placeholder_pointer_type, to handle cases like
166 // type A []*A.
167 virtual Btype*
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.
174 virtual bool
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".)
183 virtual Btype*
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
194 // function type.
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);
200 virtual Btype*
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.
207 virtual bool
208 is_circular_pointer_type(Btype*) = 0;
210 // Return the size of a type.
211 virtual size_t
212 type_size(Btype*) = 0;
214 // Return the alignment of a type.
215 virtual size_t
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.
220 virtual size_t
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.
226 virtual size_t
227 type_field_offset(Btype*, size_t index) = 0;
229 // Expressions.
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.
234 virtual Bexpression*
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.
240 virtual Bexpression*
241 error_expression() = 0;
243 // Create a reference to a variable.
244 virtual Bexpression*
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.
250 virtual Bexpression*
251 indirect_expression(Bexpression* expr, bool known_valid, Location) = 0;
253 // Return an expression for the multi-precision integer VAL in BTYPE.
254 virtual Bexpression*
255 integer_constant_expression(Btype* btype, mpz_t val) = 0;
257 // Return an expression for the floating point value VAL in BTYPE.
258 virtual Bexpression*
259 float_constant_expression(Btype* btype, mpfr_t val) = 0;
261 // Return an expression for the complex value REAL/IMAG in BTYPE.
262 virtual Bexpression*
263 complex_constant_expression(Btype* btype, mpfr_t real, mpfr_t imag) = 0;
265 // Return an expression that converts EXPR to TYPE.
266 virtual Bexpression*
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.
271 virtual Bexpression*
272 function_code_expression(Bfunction*, Location) = 0;
274 // Statements.
276 // Create an error statement. This is used for cases which should
277 // not occur in a correct program, in order to keep the compilation
278 // going without crashing.
279 virtual Bstatement*
280 error_statement() = 0;
282 // Create an expression statement.
283 virtual Bstatement*
284 expression_statement(Bexpression*) = 0;
286 // Create a variable initialization statement. This initializes a
287 // local variable at the point in the program flow where it is
288 // declared.
289 virtual Bstatement*
290 init_statement(Bvariable* var, Bexpression* init) = 0;
292 // Create an assignment statement.
293 virtual Bstatement*
294 assignment_statement(Bexpression* lhs, Bexpression* rhs,
295 Location) = 0;
297 // Create a return statement, passing the representation of the
298 // function and the list of values to return.
299 virtual Bstatement*
300 return_statement(Bfunction*, const std::vector<Bexpression*>&,
301 Location) = 0;
303 // Create an if statement. ELSE_BLOCK may be NULL.
304 virtual Bstatement*
305 if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
306 Location) = 0;
308 // Create a switch statement where the case values are constants.
309 // CASES and STATEMENTS must have the same number of entries. If
310 // VALUE matches any of the list in CASES[i], which will all be
311 // integers, then STATEMENTS[i] is executed. STATEMENTS[i] will
312 // either end with a goto statement or will fall through into
313 // STATEMENTS[i + 1]. CASES[i] is empty for the default clause,
314 // which need not be last.
315 virtual Bstatement*
316 switch_statement(Bexpression* value,
317 const std::vector<std::vector<Bexpression*> >& cases,
318 const std::vector<Bstatement*>& statements,
319 Location) = 0;
321 // Create a single statement from two statements.
322 virtual Bstatement*
323 compound_statement(Bstatement*, Bstatement*) = 0;
325 // Create a single statement from a list of statements.
326 virtual Bstatement*
327 statement_list(const std::vector<Bstatement*>&) = 0;
329 // Blocks.
331 // Create a block. The frontend will call this function when it
332 // starts converting a block within a function. FUNCTION is the
333 // current function. ENCLOSING is the enclosing block; it will be
334 // NULL for the top-level block in a function. VARS is the list of
335 // local variables defined within this block; each entry will be
336 // created by the local_variable function. START_LOCATION is the
337 // location of the start of the block, more or less the location of
338 // the initial curly brace. END_LOCATION is the location of the end
339 // of the block, more or less the location of the final curly brace.
340 // The statements will be added after the block is created.
341 virtual Bblock*
342 block(Bfunction* function, Bblock* enclosing,
343 const std::vector<Bvariable*>& vars,
344 Location start_location, Location end_location) = 0;
346 // Add the statements to a block. The block is created first. Then
347 // the statements are created. Then the statements are added to the
348 // block. This will called exactly once per block. The vector may
349 // be empty if there are no statements.
350 virtual void
351 block_add_statements(Bblock*, const std::vector<Bstatement*>&) = 0;
353 // Return the block as a statement. This is used to include a block
354 // in a list of statements.
355 virtual Bstatement*
356 block_statement(Bblock*) = 0;
358 // Variables.
360 // Create an error variable. This is used for cases which should
361 // not occur in a correct program, in order to keep the compilation
362 // going without crashing.
363 virtual Bvariable*
364 error_variable() = 0;
366 // Create a global variable. PACKAGE_NAME is the name of the
367 // package where the variable is defined. PKGPATH is the package
368 // path for that package, from the -fgo-pkgpath or -fgo-prefix
369 // option. NAME is the name of the variable. BTYPE is the type of
370 // the variable. IS_EXTERNAL is true if the variable is defined in
371 // some other package. IS_HIDDEN is true if the variable is not
372 // exported (name begins with a lower case letter).
373 // IN_UNIQUE_SECTION is true if the variable should be put into a
374 // unique section if possible; this is intended to permit the linker
375 // to garbage collect the variable if it is not referenced.
376 // LOCATION is where the variable was defined.
377 virtual Bvariable*
378 global_variable(const std::string& package_name,
379 const std::string& pkgpath,
380 const std::string& name,
381 Btype* btype,
382 bool is_external,
383 bool is_hidden,
384 bool in_unique_section,
385 Location location) = 0;
387 // A global variable will 1) be initialized to zero, or 2) be
388 // initialized to a constant value, or 3) be initialized in the init
389 // function. In case 2, the frontend will call
390 // global_variable_set_init to set the initial value. If this is
391 // not called, the backend should initialize a global variable to 0.
392 // The init function may then assign a value to it.
393 virtual void
394 global_variable_set_init(Bvariable*, Bexpression*) = 0;
396 // Create a local variable. The frontend will create the local
397 // variables first, and then create the block which contains them.
398 // FUNCTION is the function in which the variable is defined. NAME
399 // is the name of the variable. TYPE is the type. IS_ADDRESS_TAKEN
400 // is true if the address of this variable is taken (this implies
401 // that the address does not escape the function, as otherwise the
402 // variable would be on the heap). LOCATION is where the variable
403 // is defined. For each local variable the frontend will call
404 // init_statement to set the initial value.
405 virtual Bvariable*
406 local_variable(Bfunction* function, const std::string& name, Btype* type,
407 bool is_address_taken, Location location) = 0;
409 // Create a function parameter. This is an incoming parameter, not
410 // a result parameter (result parameters are treated as local
411 // variables). The arguments are as for local_variable.
412 virtual Bvariable*
413 parameter_variable(Bfunction* function, const std::string& name,
414 Btype* type, bool is_address_taken,
415 Location location) = 0;
417 // Create a temporary variable. A temporary variable has no name,
418 // just a type. We pass in FUNCTION and BLOCK in case they are
419 // needed. If INIT is not NULL, the variable should be initialized
420 // to that value. Otherwise the initial value is irrelevant--the
421 // backend does not have to explicitly initialize it to zero.
422 // ADDRESS_IS_TAKEN is true if the programs needs to take the
423 // address of this temporary variable. LOCATION is the location of
424 // the statement or expression which requires creating the temporary
425 // variable, and may not be very useful. This function should
426 // return a variable which can be referenced later and should set
427 // *PSTATEMENT to a statement which initializes the variable.
428 virtual Bvariable*
429 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression* init,
430 bool address_is_taken, Location location,
431 Bstatement** pstatement) = 0;
433 // Create a named immutable initialized data structure. This is
434 // used for type descriptors, map descriptors, and function
435 // descriptors. This returns a Bvariable because it corresponds to
436 // an initialized const variable in C.
438 // NAME is the name to use for the initialized global variable which
439 // this call will create.
441 // IS_HIDDEN will be true if the descriptor should only be visible
442 // within the current object.
444 // IS_COMMON is true if NAME may be defined by several packages, and
445 // the linker should merge all such definitions. If IS_COMMON is
446 // false, NAME should be defined in only one file. In general
447 // IS_COMMON will be true for the type descriptor of an unnamed type
448 // or a builtin type. IS_HIDDEN and IS_COMMON will never both be
449 // true.
451 // TYPE will be a struct type; the type of the returned expression
452 // must be a pointer to this struct type.
454 // We must create the named structure before we know its
455 // initializer, because the initializer may refer to its own
456 // address. After calling this the frontend will call
457 // immutable_struct_set_init.
458 virtual Bvariable*
459 immutable_struct(const std::string& name, bool is_hidden, bool is_common,
460 Btype* type, Location) = 0;
462 // Set the initial value of a variable created by immutable_struct.
463 // The NAME, IS_HIDDEN, IS_COMMON, TYPE, and location parameters are
464 // the same ones passed to immutable_struct. INITIALIZER will be a
465 // composite literal of type TYPE. It will not contain any function
466 // calls or anything else that can not be put into a read-only data
467 // section. It may contain the address of variables created by
468 // immutable_struct.
469 virtual void
470 immutable_struct_set_init(Bvariable*, const std::string& name,
471 bool is_hidden, bool is_common, Btype* type,
472 Location, Bexpression* initializer) = 0;
474 // Create a reference to a named immutable initialized data
475 // structure defined in some other package. This will be a
476 // structure created by a call to immutable_struct with the same
477 // NAME and TYPE and with IS_COMMON passed as false. This
478 // corresponds to an extern const global variable in C.
479 virtual Bvariable*
480 immutable_struct_reference(const std::string& name, Btype* type,
481 Location) = 0;
483 // Labels.
485 // Create a new label. NAME will be empty if this is a label
486 // created by the frontend for a loop construct. The location is
487 // where the the label is defined.
488 virtual Blabel*
489 label(Bfunction*, const std::string& name, Location) = 0;
491 // Create a statement which defines a label. This statement will be
492 // put into the codestream at the point where the label should be
493 // defined.
494 virtual Bstatement*
495 label_definition_statement(Blabel*) = 0;
497 // Create a goto statement to a label.
498 virtual Bstatement*
499 goto_statement(Blabel*, Location) = 0;
501 // Create an expression for the address of a label. This is used to
502 // get the return address of a deferred function which may call
503 // recover.
504 virtual Bexpression*
505 label_address(Blabel*, Location) = 0;
507 // Functions.
509 // Create an error function. This is used for cases which should
510 // not occur in a correct program, in order to keep the compilation
511 // going without crashing.
512 virtual Bfunction*
513 error_function() = 0;
515 // Declare or define a function of FNTYPE.
516 // NAME is the Go name of the function. ASM_NAME, if not the empty string, is
517 // the name that should be used in the symbol table; this will be non-empty if
518 // a magic extern comment is used.
519 // IS_VISIBLE is true if this function should be visible outside of the
520 // current compilation unit. IS_DECLARATION is true if this is a function
521 // declaration rather than a definition; the function definition will be in
522 // another compilation unit.
523 // IS_INLINABLE is true if the function can be inlined.
524 // DISABLE_SPLIT_STACK is true if this function may not split the stack; this
525 // is used for the implementation of recover.
526 // IN_UNIQUE_SECTION is true if this function should be put into a unique
527 // location if possible; this is used for field tracking.
528 virtual Bfunction*
529 function(Btype* fntype, const std::string& name, const std::string& asm_name,
530 bool is_visible, bool is_declaration, bool is_inlinable,
531 bool disable_split_stack, bool in_unique_section, Location) = 0;
534 // The backend interface has to define this function.
536 extern Backend* go_get_backend();
538 // FIXME: Temporary helper functions while converting to new backend
539 // interface.
541 extern Btype* tree_to_type(tree);
542 extern Bexpression* tree_to_expr(tree);
543 extern Bstatement* tree_to_stat(tree);
544 extern Bfunction* tree_to_function(tree);
545 extern Bblock* tree_to_block(tree);
546 extern tree type_to_tree(Btype*);
547 extern tree expr_to_tree(Bexpression*);
548 extern tree stat_to_tree(Bstatement*);
549 extern tree block_to_tree(Bblock*);
550 extern tree var_to_tree(Bvariable*);
551 extern tree function_to_tree(Bfunction*);
553 #endif // !defined(GO_BACKEND_H)