Reverting merge from trunk
[official-gcc.git] / gcc / go / gofrontend / backend.h
blob6f2c321e09e50d087989a35af5628df70d63301a
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 // Create an expression that takes the address of an expression.
275 virtual Bexpression*
276 address_expression(Bexpression*, Location) = 0;
278 // Statements.
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.
283 virtual Bstatement*
284 error_statement() = 0;
286 // Create an expression statement.
287 virtual Bstatement*
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
292 // declared.
293 virtual Bstatement*
294 init_statement(Bvariable* var, Bexpression* init) = 0;
296 // Create an assignment statement.
297 virtual Bstatement*
298 assignment_statement(Bexpression* lhs, Bexpression* rhs,
299 Location) = 0;
301 // Create a return statement, passing the representation of the
302 // function and the list of values to return.
303 virtual Bstatement*
304 return_statement(Bfunction*, const std::vector<Bexpression*>&,
305 Location) = 0;
307 // Create an if statement. ELSE_BLOCK may be NULL.
308 virtual Bstatement*
309 if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
310 Location) = 0;
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.
319 virtual Bstatement*
320 switch_statement(Bexpression* value,
321 const std::vector<std::vector<Bexpression*> >& cases,
322 const std::vector<Bstatement*>& statements,
323 Location) = 0;
325 // Create a single statement from two statements.
326 virtual Bstatement*
327 compound_statement(Bstatement*, Bstatement*) = 0;
329 // Create a single statement from a list of statements.
330 virtual Bstatement*
331 statement_list(const std::vector<Bstatement*>&) = 0;
333 // Blocks.
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.
345 virtual Bblock*
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.
354 virtual void
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.
359 virtual Bstatement*
360 block_statement(Bblock*) = 0;
362 // Variables.
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.
367 virtual Bvariable*
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.
381 virtual Bvariable*
382 global_variable(const std::string& package_name,
383 const std::string& pkgpath,
384 const std::string& name,
385 Btype* btype,
386 bool is_external,
387 bool is_hidden,
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.
397 virtual void
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.
409 virtual Bvariable*
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.
416 virtual Bvariable*
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.
432 virtual Bvariable*
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
453 // true.
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.
462 virtual Bvariable*
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
472 // immutable_struct.
473 virtual void
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.
483 virtual Bvariable*
484 immutable_struct_reference(const std::string& name, Btype* type,
485 Location) = 0;
487 // Labels.
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.
492 virtual Blabel*
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
497 // defined.
498 virtual Bstatement*
499 label_definition_statement(Blabel*) = 0;
501 // Create a goto statement to a label.
502 virtual Bstatement*
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
507 // recover.
508 virtual Bexpression*
509 label_address(Blabel*, Location) = 0;
511 // Functions.
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.
516 virtual Bfunction*
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.
532 virtual Bfunction*
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
543 // interface.
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)